3 * spost.c -- feed messages to sendmail
5 * This is a simpler, faster, replacement for "post" for use
6 * when "sendmail" is the transport system.
10 * This code is Copyright (c) 2002, by the authors of nmh. See the
11 * COPYRIGHT file in the root directory of the nmh distribution for
12 * complete copyright information.
17 #include <h/addrsbr.h>
18 #include <h/aliasbr.h>
19 #include <h/dropsbr.h>
24 #define MAX_SM_FIELD 1476 /* < largest hdr field sendmail will accept */
25 #define FCCS 10 /* max number of fccs allowed */
27 struct swit switches
[] = {
29 { "filter filterfile", 0 },
53 { "alias aliasfile", 0 },
57 { "width columns", 0 },
65 { "dist", -4 }, /* interface from dist */
67 { "check", -5 }, /* interface from whom */
69 { "nocheck", -7 }, /* interface from whom */
71 { "whom", -4 }, /* interface from whom */
72 #define PUSHSW 22 /* fork to sendmail then exit */
74 #define NPUSHSW 23 /* exec sendmail */
77 { "library directory", -7 },
79 { "idanno number", -6 },
84 /* flags for headers->flags */
85 #define HNOP 0x0000 /* just used to keep .set around */
86 #define HBAD 0x0001 /* bad header - don't let it through */
87 #define HADR 0x0002 /* header has an address field */
88 #define HSUB 0x0004 /* Subject: header */
89 #define HTRY 0x0008 /* try to send to addrs on header */
90 #define HBCC 0x0010 /* don't output this header */
91 #define HMNG 0x0020 /* mung this header */
92 #define HNGR 0x0040 /* no groups allowed in this header */
93 #define HFCC 0x0080 /* FCC: type header */
94 #define HNIL 0x0100 /* okay for this header not to have addrs */
95 #define HIGN 0x0200 /* ignore this header */
97 /* flags for headers->set */
98 #define MFRM 0x0001 /* we've seen a From: */
99 #define MDAT 0x0002 /* we've seen a Date: */
100 #define MRFM 0x0004 /* we've seen a Resent-From: */
101 #define MVIS 0x0008 /* we've seen sighted addrs */
102 #define MINV 0x0010 /* we've seen blind addrs */
103 #define MRDT 0x0020 /* we've seen a Resent-Date: */
112 static struct headers NHeaders
[] = {
113 { "Return-Path", HBAD
, 0 },
114 { "Received", HBAD
, 0 },
115 { "Reply-To", HADR
|HNGR
, 0 },
116 { "From", HADR
|HNGR
, MFRM
},
117 { "Sender", HADR
|HBAD
, 0 },
118 { "Date", HNOP
, MDAT
},
119 { "Subject", HSUB
, 0 },
120 { "To", HADR
|HTRY
, MVIS
},
121 { "cc", HADR
|HTRY
, MVIS
},
122 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, MINV
},
123 { "Message-Id", HBAD
, 0 },
128 static struct headers RHeaders
[] = {
129 { "Resent-Reply-To", HADR
|HNGR
, 0 },
130 { "Resent-From", HADR
|HNGR
, MRFM
},
131 { "Resent-Sender", HADR
|HBAD
, 0 },
132 { "Resent-Date", HNOP
, MRDT
},
133 { "Resent-Subject", HSUB
, 0 },
134 { "Resent-To", HADR
|HTRY
, MVIS
},
135 { "Resent-cc", HADR
|HTRY
, MVIS
},
136 { "Resent-Bcc", HADR
|HTRY
|HBCC
, MINV
},
137 { "Resent-Message-Id", HBAD
, 0 },
138 { "Resent-Fcc", HFCC
, 0 },
139 { "Reply-To", HADR
, 0 },
145 static short fccind
= 0; /* index into fccfold[] */
147 static int badmsg
= 0; /* message has bad semantics */
148 static int verbose
= 0; /* spell it out */
149 static int debug
= 0; /* debugging post */
150 static int rmflg
= 1; /* remove temporary file when done */
151 static int watch
= 0; /* watch the delivery process */
152 static int backflg
= 0; /* rename input file as *.bak when done */
153 static int whomflg
= 0; /* if just checking addresses */
154 static int pushflg
= 0; /* if going to fork to sendmail */
155 static int aliasflg
= -1; /* if going to process aliases */
156 static int outputlinelen
=72;
158 static unsigned msgflags
= 0; /* what we've seen */
164 static char tmpfil
[] = "/tmp/pstXXXXXX";
166 static char from
[BUFSIZ
]; /* my network address */
167 static char signature
[BUFSIZ
]; /* my signature */
168 static char *filter
= NULL
; /* the filter for BCC'ing */
169 static char *subject
= NULL
; /* the subject field for BCC'ing */
170 static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
172 static struct headers
*hdrtab
; /* table for the message we're doing */
173 static FILE *out
; /* output (temp) file */
175 extern char *sendmail
;
178 * external prototypes
180 extern char *getfullname (void);
181 extern char *getusername (void);
183 extern boolean draft_from_masquerading
; /* defined in mts.c */
188 static void putfmt (char *, char *, FILE *);
189 static void start_headers (void);
190 static void finish_headers (FILE *);
191 static int get_header (char *, struct headers
*);
192 static void putadr (char *, struct mailname
*);
193 static int putone (char *, int, int);
194 static void insert_fcc (struct headers
*, char *);
195 static void file (char *);
196 static void fcc (char *, char *);
199 static void die (char *, char *, ...);
200 static void make_bcc_file (void);
205 main (int argc
, char **argv
)
207 int state
, i
, pid
, compnum
;
208 char *cp
, *msg
= NULL
, **argp
, **arguments
;
209 char *sargv
[16], buf
[BUFSIZ
], name
[NAMESZ
];
213 setlocale(LC_ALL
, "");
215 invo_name
= r1bindex (argv
[0], '/');
217 /* foil search of user profile/context */
218 if (context_foil (NULL
) == -1)
221 mts_init (invo_name
);
222 arguments
= getarguments (invo_name
, argc
, argv
, 0);
225 while ((cp
= *argp
++)) {
227 switch (smatch (++cp
, switches
)) {
229 ambigsw (cp
, switches
);
232 adios (NULL
, "-%s unknown", cp
);
235 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
236 print_help (buf
, switches
, 1);
239 print_version(invo_name
);
255 if (!(filter
= *argp
++) || *filter
== '-')
256 adios (NULL
, "missing argument to %s", argp
[-2]);
298 if (!(cp
= *argp
++) || *cp
== '-')
299 adios (NULL
, "missing argument to %s", argp
[-2]);
301 alias (AliasFile
);/* load default aka's */
303 if ((state
= alias(cp
)) != AK_OK
)
304 adios (NULL
, "aliasing error in file %s - %s",
305 cp
, akerror(state
) );
312 if (!(cp
= *argp
++) || *cp
== '-')
313 adios (NULL
, "missing argument to %s", argp
[-2]);
314 outputlinelen
= atoi (cp
);
315 if (outputlinelen
<= 10)
320 if (!(cp
= *argp
++) || *cp
== '-')
321 adios (NULL
, "missing argument to %s", argp
[-2]);
322 /* create a minimal context */
323 if (context_foil (cp
) == -1)
328 /* -idanno switch ignored */
329 if (!(cp
= *argp
++) || *cp
== '-')
330 adios (NULL
, "missing argument to %s", argp
[-2]);
335 adios (NULL
, "only one message at a time!");
341 alias (AliasFile
); /* load default aka's */
344 adios (NULL
, "usage: %s [switches] file", invo_name
);
346 if ((in
= fopen (msg
, "r")) == NULL
)
347 adios (msg
, "unable to open");
356 if ((out
= fdopen( mkstemp (tmpfil
), "w" )) == NULL
)
357 adios (tmpfil
, "unable to create");
360 if ((out
= fopen (tmpfil
, "w")) == NULL
)
361 adios (tmpfil
, "unable to create");
362 chmod (tmpfil
, 0600);
366 hdrtab
= (msgstate
== normal
) ? NHeaders
: RHeaders
;
368 for (compnum
= 1, state
= FLD
;;) {
369 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
)) {
372 putfmt (name
, buf
, out
);
378 while (state
== FLDPLUS
) {
379 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
382 putfmt (name
, cp
, out
);
387 finish_headers (out
);
388 fprintf (out
, "\n%s", buf
);
390 while (state
== BODY
) {
391 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
397 finish_headers (out
);
402 adios (NULL
, "message format error in component #%d",
406 adios (NULL
, "getfld() returned %d", state
);
412 if (backflg
&& !whomflg
) {
413 strncpy (buf
, m_backup (msg
), sizeof(buf
));
414 if (rename (msg
, buf
) == NOTOK
)
415 advise (buf
, "unable to rename %s to", msg
);
427 * re-open the temp file, unlink it and exec sendmail, giving it
428 * the msg temp file as std in.
430 if ( freopen( tmpfil
, "r", stdin
) == NULL
)
431 adios (tmpfil
, "can't reopen for sendmail");
436 *argp
++ = "send-mail";
437 *argp
++ = "-m"; /* send to me too */
438 *argp
++ = "-t"; /* read msg for recipients */
439 *argp
++ = "-i"; /* don't stop on "." */
442 if (watch
|| verbose
)
446 if (pushflg
&& !(watch
|| verbose
)) {
447 /* fork to a child to run sendmail */
448 for (i
=0; (pid
= vfork()) == NOTOK
&& i
< 5; i
++)
452 fprintf (verbose
? stdout
: stderr
, "%s: can't fork to %s\n",
453 invo_name
, sendmail
);
456 /* we're the child .. */
462 execv ( sendmail
, sargv
);
463 adios ( sendmail
, "can't exec");
464 return 0; /* dead code to satisfy the compiler */
467 /* DRAFT GENERATION */
470 putfmt (char *name
, char *str
, FILE *out
)
476 while (*str
== ' ' || *str
== '\t')
479 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
480 fprintf (out
, "%s: %s", name
, str
);
485 if (hdr
->flags
& HIGN
)
487 if (hdr
->flags
& HBAD
) {
488 advise (NULL
, "illegal header line -- %s:", name
);
492 msgflags
|= hdr
->set
;
494 if (hdr
->flags
& HSUB
)
495 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
497 if (hdr
->flags
& HFCC
) {
498 if ((cp
= strrchr(str
, '\n')))
500 for (cp
= pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
502 insert_fcc (hdr
, pp
);
504 insert_fcc (hdr
, pp
);
509 if (hdr
->flags
& HBCC
) {
515 if (*str
!= '\n' && *str
!= '\0') {
516 if (aliasflg
&& hdr
->flags
& HTRY
) {
517 /* this header contains address(es) that we have to do
518 * alias expansion on. Because of the saved state in
519 * getname we have to put all the addresses into a list.
520 * We then let putadr munch on that list, possibly
523 register struct mailname
*f
= 0;
524 register struct mailname
*mp
= 0;
526 while ((cp
= getname(str
))) {
527 mp
= getm( cp
, NULL
, 0, AD_HOST
, NULL
);
532 mp
->m_next
= f
->m_next
;
537 f
= mp
->m_next
; mp
->m_next
= 0;
540 /* The author(s) of spost decided that alias substitution wasn't
541 necessary for the non-HTRY headers. Unfortunately, one of those
542 headers is "From:", and having alias substitution work on that is
543 extremely useful for someone with a lot of POP3 email accounts or
544 aliases. post supports aliasing of "From:"...
546 Since "From:"-processing is incompletely implemented in this
547 unsupported and undocumented spost backend, I'm not going to take
548 the time to implement my new draft-From:-based email address
549 masquerading. If I do ever implement it here, I'd almost
550 certainly want to implement "From:" line alias processing as
551 well. -- Dan Harkless <dan-nmh@dilvish.speed.net> */
552 fprintf (out
, "%s: %s", name
, str
);
564 strncpy(from
, getusername(), sizeof(from
));
566 if ((cp
= getfullname ()) && *cp
) {
567 strncpy (sigbuf
, cp
, sizeof(sigbuf
));
568 snprintf (signature
, sizeof(signature
), "%s <%s>", sigbuf
, from
);
571 snprintf (signature
, sizeof(signature
), "%s", from
);
576 finish_headers (FILE *out
)
580 if (!(msgflags
& MDAT
))
581 fprintf (out
, "Date: %s\n", dtimenow (0));
583 if (msgflags
& MFRM
) {
584 /* There was already a From: in the draft. Don't add one. */
585 if (!draft_from_masquerading
)
586 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
587 so we'll reveal the user's actual account@thismachine
588 address in a Sender: header (and use it as the envelope
590 fprintf (out
, "Sender: %s\n", from
);
593 fprintf (out
, "From: %s\n", signature
);
596 if (!(msgflags
& MVIS
))
597 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
602 if (!(msgflags
& MRDT
))
603 fprintf (out
, "Resent-Date: %s\n", dtimenow(0));
604 if (msgflags
& MRFM
) {
605 /* There was already a Resent-From: in draft. Don't add one. */
606 if (!draft_from_masquerading
)
607 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
608 so we'll reveal the user's actual account@thismachine
609 address in a Sender: header (and use it as the envelope
611 fprintf (out
, "Resent-Sender: %s\n", from
);
614 /* Construct a Resent-From: header. */
615 fprintf (out
, "Resent-From: %s\n", signature
);
617 if (!(msgflags
& MVIS
))
618 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
624 adios (NULL
, "re-format message and try again");
629 get_header (char *header
, struct headers
*table
)
633 for (h
= table
; h
->value
; h
++)
634 if (!mh_strcasecmp (header
, h
->value
))
642 * output the address list for header "name". The address list
643 * is a linked list of mailname structs. "nl" points to the head
644 * of the list. Alias substitution should be done on nl.
647 putadr (char *name
, struct mailname
*nl
)
649 register struct mailname
*mp
, *mp2
;
650 register int linepos
;
654 fprintf (out
, "%s: ", name
);
655 namelen
= strlen(name
) + 2;
658 for (mp
= nl
; mp
; ) {
659 if (linepos
> MAX_SM_FIELD
) {
660 fprintf (out
, "\n%s: ", name
);
664 /* a local name - see if it's an alias */
665 cp
= akvalue(mp
->m_mbox
);
666 if (cp
== mp
->m_mbox
)
667 /* wasn't an alias - use what the user typed */
668 linepos
= putone( mp
->m_text
, linepos
, namelen
);
670 /* an alias - expand it */
671 while ((cp
= getname(cp
))) {
672 if (linepos
> MAX_SM_FIELD
) {
673 fprintf (out
, "\n%s: ", name
);
676 mp2
= getm( cp
, NULL
, 0, AD_HOST
, NULL
);
678 mp2
->m_pers
= getcpy(mp
->m_mbox
);
679 linepos
= putone( adrformat(mp2
), linepos
, namelen
);
681 linepos
= putone( mp2
->m_text
, linepos
, namelen
);
686 /* not a local name - use what the user typed */
687 linepos
= putone( mp
->m_text
, linepos
, namelen
);
697 putone (char *adr
, int pos
, int indent
)
705 else if ( linepos
+len
> outputlinelen
) {
706 fprintf ( out
, ",\n%*s", indent
, "");
723 insert_fcc (struct headers
*hdr
, char *pp
)
727 for (cp
= pp
; isspace (*cp
); cp
++)
729 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace (*pp
); pp
--)
737 adios (NULL
, "too many %ss", hdr
->value
);
738 fccfold
[fccind
++] = getcpy (cp
);
753 fd
= mkstemp(bccfil
);
754 if (fd
== -1 || (out
= fdopen(fd
, "w")) == NULL
)
755 adios (bccfil
, "unable to create");
758 if ((out
= fopen (bccfil
, "w")) == NULL
)
759 adios (bccfil
, "unable to create");
761 chmod (bccfil
, 0600);
763 fprintf (out
, "Date: %s\n", dtimenow (0));
764 if (msgflags
& MFRM
) {
765 /* There was already a From: in the draft. Don't add one. */
766 if (!draft_from_masquerading
)
767 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
768 so we'll reveal the user's actual account@thismachine
769 address in a Sender: header (and use it as the envelope
771 fprintf (out
, "Sender: %s\n", from
);
774 /* Construct a From: header. */
775 fprintf (out
, "From: %s\n", signature
);
777 fprintf (out
, "Subject: %s", subject
);
778 fprintf (out
, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
781 if (filter
== NULL
) {
782 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
783 adios (NULL
, "unable to re-open");
784 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
788 vec
[0] = r1bindex (mhlproc
, '/');
790 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
794 adios ("vfork", "unable to");
797 dup2 (fileno (out
), 1);
800 vec
[i
++] = "-forward";
806 execvp (mhlproc
, vec
);
807 adios (mhlproc
, "unable to exec");
810 if (status
= pidwait(child_id
, OK
))
811 admonish (NULL
, "%s lost (status=0%o)", vec
[0], status
);
816 fseek (out
, 0L, SEEK_END
);
817 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
822 /* FCC INTERACTION */
832 for (i
= 0; i
< fccind
; i
++)
834 printf ("Fcc: %s\n", fccfold
[i
]);
836 fcc (path
, fccfold
[i
]);
841 fcc (char *file
, char *folder
)
848 printf ("%sFcc: %s\n", msgstate
== resent
? "Resent-" : "", folder
);
851 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
856 fprintf (stderr
, " %sFcc %s: ",
857 msgstate
== resent
? "Resent-" : "", folder
);
858 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
862 snprintf (fold
, sizeof(fold
), "%s%s",
863 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
864 execlp (fileproc
, r1bindex (fileproc
, '/'),
865 "-link", "-file", file
, fold
, NULL
);
869 if ((status
= pidwait(child_id
, OK
))) {
871 fprintf (stderr
, " %sFcc %s: ",
872 msgstate
== resent
? "Resent-" : "", folder
);
873 fprintf (verbose
? stdout
: stderr
,
874 " errored (0%o)\n", status
);
889 die (char *what
, char *fmt
, ...)
894 advertise (what
, NULL
, fmt
, ap
);