2 * spost.c -- feed messages to sendmail
4 * This is a simpler, faster, replacement for "post" for use
5 * when "sendmail" is the transport system.
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 #include <h/addrsbr.h>
15 #include <h/aliasbr.h>
16 #include <h/dropsbr.h>
21 #define MAX_SM_FIELD 1476 /* < largest hdr field sendmail will accept */
22 #define FCCS 10 /* max number of fccs allowed */
24 struct swit switches[] = {
26 { "filter filterfile", 0 },
50 { "alias aliasfile", 0 },
54 { "width columns", 0 },
62 { "dist", -4 }, /* interface from dist */
64 { "check", -5 }, /* interface from whom */
66 { "nocheck", -7 }, /* interface from whom */
68 { "whom", -4 }, /* interface from whom */
69 #define PUSHSW 22 /* fork to sendmail then exit */
71 #define NPUSHSW 23 /* exec sendmail */
74 { "library directory", -7 },
76 { "idanno number", -6 },
81 /* flags for headers->flags */
82 #define HNOP 0x0000 /* just used to keep .set around */
83 #define HBAD 0x0001 /* bad header - don't let it through */
84 #define HADR 0x0002 /* header has an address field */
85 #define HSUB 0x0004 /* Subject: header */
86 #define HTRY 0x0008 /* try to send to addrs on header */
87 #define HBCC 0x0010 /* don't output this header */
88 #define HMNG 0x0020 /* mung this header */
89 #define HNGR 0x0040 /* no groups allowed in this header */
90 #define HFCC 0x0080 /* FCC: type header */
91 #define HNIL 0x0100 /* okay for this header not to have addrs */
92 #define HIGN 0x0200 /* ignore this header */
94 /* flags for headers->set */
95 #define MFRM 0x0001 /* we've seen a From: */
96 #define MDAT 0x0002 /* we've seen a Date: */
97 #define MRFM 0x0004 /* we've seen a Resent-From: */
98 #define MVIS 0x0008 /* we've seen sighted addrs */
99 #define MINV 0x0010 /* we've seen blind addrs */
100 #define MRDT 0x0020 /* we've seen a Resent-Date: */
109 static struct headers NHeaders[] = {
110 { "Return-Path", HBAD, 0 },
111 { "Received", HBAD, 0 },
112 { "Reply-To", HADR|HNGR, 0 },
113 { "From", HADR|HNGR, MFRM },
114 { "Sender", HADR|HBAD, 0 },
115 { "Date", HNOP, MDAT },
116 { "Subject", HSUB, 0 },
117 { "To", HADR|HTRY, MVIS },
118 { "cc", HADR|HTRY, MVIS },
119 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
120 { "Message-Id", HBAD, 0 },
125 static struct headers RHeaders[] = {
126 { "Resent-Reply-To", HADR|HNGR, 0 },
127 { "Resent-From", HADR|HNGR, MRFM },
128 { "Resent-Sender", HADR|HBAD, 0 },
129 { "Resent-Date", HNOP, MRDT },
130 { "Resent-Subject", HSUB, 0 },
131 { "Resent-To", HADR|HTRY, MVIS },
132 { "Resent-cc", HADR|HTRY, MVIS },
133 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
134 { "Resent-Message-Id", HBAD, 0 },
135 { "Resent-Fcc", HFCC, 0 },
136 { "Reply-To", HADR, 0 },
142 static short fccind = 0; /* index into fccfold[] */
144 static int badmsg = 0; /* message has bad semantics */
145 static int verbose = 0; /* spell it out */
146 static int debug = 0; /* debugging post */
147 static int rmflg = 1; /* remove temporary file when done */
148 static int watch = 0; /* watch the delivery process */
149 static int backflg = 0; /* rename input file as *.bak when done */
150 static int whomflg = 0; /* if just checking addresses */
151 static int pushflg = 0; /* if going to fork to sendmail */
152 static int aliasflg = -1; /* if going to process aliases */
153 static int outputlinelen=72;
155 static unsigned msgflags = 0; /* what we've seen */
161 static char tmpfil[] = "/tmp/pstXXXXXX";
163 static char from[BUFSIZ]; /* my network address */
164 static char signature[BUFSIZ]; /* my signature */
165 static char *filter = NULL; /* the filter for BCC'ing */
166 static char *subject = NULL; /* the subject field for BCC'ing */
167 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
169 static struct headers *hdrtab; /* table for the message we're doing */
170 static FILE *out; /* output (temp) file */
172 extern char *sendmail;
175 * external prototypes
177 extern char *getfullname (void);
178 extern char *getusername (void);
180 extern boolean draft_from_masquerading; /* defined in mts.c */
185 static void putfmt (char *, char *, FILE *);
186 static void start_headers (void);
187 static void finish_headers (FILE *);
188 static int get_header (char *, struct headers *);
189 static void putadr (char *, struct mailname *);
190 static int putone (char *, int, int);
191 static void insert_fcc (struct headers *, unsigned char *);
192 static void file (char *);
193 static void fcc (char *, char *);
196 static void die (char *, char *, ...);
197 static void make_bcc_file (void);
202 main (int argc, char **argv)
204 int state, i, pid, compnum;
205 char *cp, *msg = NULL, **argp, **arguments;
206 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
210 setlocale(LC_ALL, "");
212 invo_name = r1bindex (argv[0], '/');
214 /* foil search of user profile/context */
215 if (context_foil (NULL) == -1)
218 mts_init (invo_name);
219 arguments = getarguments (invo_name, argc, argv, 0);
222 while ((cp = *argp++)) {
224 switch (smatch (++cp, switches)) {
226 ambigsw (cp, switches);
229 adios (NULL, "-%s unknown", cp);
232 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
233 print_help (buf, switches, 1);
236 print_version(invo_name);
252 if (!(filter = *argp++) || *filter == '-')
253 adios (NULL, "missing argument to %s", argp[-2]);
295 if (!(cp = *argp++) || *cp == '-')
296 adios (NULL, "missing argument to %s", argp[-2]);
298 alias (AliasFile);/* load default aka's */
300 if ((state = alias(cp)) != AK_OK)
301 adios (NULL, "aliasing error in file %s - %s",
302 cp, akerror(state) );
309 if (!(cp = *argp++) || *cp == '-')
310 adios (NULL, "missing argument to %s", argp[-2]);
311 outputlinelen = atoi (cp);
312 if (outputlinelen <= 10)
317 if (!(cp = *argp++) || *cp == '-')
318 adios (NULL, "missing argument to %s", argp[-2]);
319 /* create a minimal context */
320 if (context_foil (cp) == -1)
325 /* -idanno switch ignored */
326 if (!(cp = *argp++) || *cp == '-')
327 adios (NULL, "missing argument to %s", argp[-2]);
332 adios (NULL, "only one message at a time!");
338 alias (AliasFile); /* load default aka's */
341 adios (NULL, "usage: %s [switches] file", invo_name);
343 if ((in = fopen (msg, "r")) == NULL)
344 adios (msg, "unable to open");
353 if ((out = fdopen( mkstemp (tmpfil), "w" )) == NULL )
354 adios (tmpfil, "unable to create");
357 if ((out = fopen (tmpfil, "w")) == NULL)
358 adios (tmpfil, "unable to create");
359 chmod (tmpfil, 0600);
363 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
365 for (compnum = 1, state = FLD;;) {
366 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
369 putfmt (name, buf, out);
375 while (state == FLDPLUS) {
376 state = m_getfld (state, name, buf, sizeof(buf), in);
379 putfmt (name, cp, out);
384 finish_headers (out);
385 fprintf (out, "\n%s", buf);
387 while (state == BODY) {
388 state = m_getfld (state, name, buf, sizeof(buf), in);
394 finish_headers (out);
399 adios (NULL, "message format error in component #%d",
403 adios (NULL, "getfld() returned %d", state);
409 if (backflg && !whomflg) {
410 strncpy (buf, m_backup (msg), sizeof(buf));
411 if (rename (msg, buf) == NOTOK)
412 advise (buf, "unable to rename %s to", msg);
424 * re-open the temp file, unlink it and exec sendmail, giving it
425 * the msg temp file as std in.
427 if ( freopen( tmpfil, "r", stdin) == NULL)
428 adios (tmpfil, "can't reopen for sendmail");
433 *argp++ = "send-mail";
434 *argp++ = "-m"; /* send to me too */
435 *argp++ = "-t"; /* read msg for recipients */
436 *argp++ = "-i"; /* don't stop on "." */
439 if (watch || verbose)
443 if (pushflg && !(watch || verbose)) {
444 /* fork to a child to run sendmail */
445 for (i=0; (pid = vfork()) == NOTOK && i < 5; i++)
449 fprintf (verbose ? stdout : stderr, "%s: can't fork to %s\n",
450 invo_name, sendmail);
453 /* we're the child .. */
459 execv ( sendmail, sargv);
460 adios ( sendmail, "can't exec");
461 return 0; /* dead code to satisfy the compiler */
464 /* DRAFT GENERATION */
467 putfmt (char *name, char *str, FILE *out)
473 while (*str == ' ' || *str == '\t')
476 if ((i = get_header (name, hdrtab)) == NOTOK) {
477 fprintf (out, "%s: %s", name, str);
482 if (hdr->flags & HIGN)
484 if (hdr->flags & HBAD) {
485 advise (NULL, "illegal header line -- %s:", name);
489 msgflags |= hdr->set;
491 if (hdr->flags & HSUB)
492 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
494 if (hdr->flags & HFCC) {
495 if ((cp = strrchr(str, '\n')))
497 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
499 insert_fcc (hdr, pp);
501 insert_fcc (hdr, pp);
506 if (hdr->flags & HBCC) {
512 if (*str != '\n' && *str != '\0') {
513 if (aliasflg && hdr->flags & HTRY) {
514 /* this header contains address(es) that we have to do
515 * alias expansion on. Because of the saved state in
516 * getname we have to put all the addresses into a list.
517 * We then let putadr munch on that list, possibly
520 register struct mailname *f = 0;
521 register struct mailname *mp = 0;
523 while ((cp = getname(str))) {
524 mp = getm( cp, NULL, 0, AD_HOST, NULL);
529 mp->m_next = f->m_next;
534 f = mp->m_next; mp->m_next = 0;
538 * The author(s) of spost decided that alias
539 * substitution wasn't necessary for the non-HTRY
540 * headers. Unfortunately, one of those headers
541 * is "From:", and having alias substitution
542 * work on that is extremely useful for someone
543 * with a lot of POP3 email accounts or aliases.
544 * post supports aliasing of "From:"...
546 * Since "From:"-processing is incompletely
547 * implemented in this unsupported and
548 * undocumented spost backend, I'm not
549 * going to take the time to implement my new
550 * draft-From:-based email address masquerading.
551 * If I do ever implement it here, I'd almost
552 * certainly want to implement "From:" line
553 * alias processing as well.
554 * -- Dan Harkless <dan-nmh@dilvish.speed.net>
556 fprintf (out, "%s: %s", name, str );
568 strncpy(from, getusername(), sizeof(from));
570 if ((cp = getfullname ()) && *cp) {
571 strncpy (sigbuf, cp, sizeof(sigbuf));
572 snprintf (signature, sizeof(signature), "%s <%s>", sigbuf, from);
574 snprintf (signature, sizeof(signature), "%s", from);
579 finish_headers (FILE *out)
583 if (!(msgflags & MDAT))
584 fprintf (out, "Date: %s\n", dtimenow (0));
586 if (msgflags & MFRM) {
587 /* There was already a From: in the draft. Don't add one. */
588 if (!draft_from_masquerading)
590 * mts.conf didn't contain
591 * "masquerade:[...]draft_from[...]"
592 * so we'll reveal the user's
593 * actual account@thismachine
594 * address in a Sender: header
595 * (and use it as the envelope
598 fprintf (out, "Sender: %s\n", from);
600 fprintf (out, "From: %s\n", signature);
603 if (!(msgflags & MVIS))
604 fprintf (out, "Bcc: Blind Distribution List: ;\n");
609 if (!(msgflags & MRDT))
610 fprintf (out, "Resent-Date: %s\n", dtimenow(0));
611 if (msgflags & MRFM) {
612 /* There was already a Resent-From: in draft. Don't add one. */
613 if (!draft_from_masquerading)
615 * mts.conf didn't contain
616 * "masquerade:[...]draft_from[...]"
617 * so we'll reveal the user's
618 * actual account@thismachine
619 * address in a Sender: header
620 * (and use it as the envelope
623 fprintf (out, "Resent-Sender: %s\n", from);
625 /* Construct a Resent-From: header. */
626 fprintf (out, "Resent-From: %s\n", signature);
628 if (!(msgflags & MVIS))
629 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
635 adios (NULL, "re-format message and try again");
640 get_header (char *header, struct headers *table)
644 for (h = table; h->value; h++)
645 if (!mh_strcasecmp (header, h->value))
653 * output the address list for header "name". The address list
654 * is a linked list of mailname structs. "nl" points to the head
655 * of the list. Alias substitution should be done on nl.
658 putadr (char *name, struct mailname *nl)
660 register struct mailname *mp, *mp2;
661 register int linepos;
665 fprintf (out, "%s: ", name);
666 namelen = strlen(name) + 2;
669 for (mp = nl; mp; ) {
670 if (linepos > MAX_SM_FIELD) {
671 fprintf (out, "\n%s: ", name);
675 /* a local name - see if it's an alias */
676 cp = akvalue(mp->m_mbox);
677 if (cp == mp->m_mbox)
678 /* wasn't an alias - use what the user typed */
679 linepos = putone( mp->m_text, linepos, namelen );
681 /* an alias - expand it */
682 while ((cp = getname(cp))) {
683 if (linepos > MAX_SM_FIELD) {
684 fprintf (out, "\n%s: ", name);
687 mp2 = getm( cp, NULL, 0, AD_HOST, NULL);
689 mp2->m_pers = getcpy(mp->m_mbox);
690 linepos = putone( adrformat(mp2), linepos, namelen );
692 linepos = putone( mp2->m_text, linepos, namelen );
697 /* not a local name - use what the user typed */
698 linepos = putone( mp->m_text, linepos, namelen );
708 putone (char *adr, int pos, int indent)
716 else if ( linepos+len > outputlinelen ) {
717 fprintf ( out, ",\n%*s", indent, "");
734 insert_fcc (struct headers *hdr, unsigned char *pp)
738 for (cp = pp; isspace (*cp); cp++)
740 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
748 adios (NULL, "too many %ss", hdr->value);
749 fccfold[fccind++] = getcpy (cp);
764 fd = mkstemp(bccfil);
765 if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
766 adios (bccfil, "unable to create");
769 if ((out = fopen (bccfil, "w")) == NULL)
770 adios (bccfil, "unable to create");
772 chmod (bccfil, 0600);
774 fprintf (out, "Date: %s\n", dtimenow (0));
775 if (msgflags & MFRM) {
776 /* There was already a From: in the draft. Don't add one. */
777 if (!draft_from_masquerading)
779 * mts.conf didn't contain "masquerade:[...]draft_from[...]"
780 * so we'll reveal the user's actual account@thismachine
781 * address in a Sender: header (and use it as the envelope
784 fprintf (out, "Sender: %s\n", from);
786 /* Construct a From: header. */
787 fprintf (out, "From: %s\n", signature);
789 fprintf (out, "Subject: %s", subject);
790 fprintf (out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
793 if (filter == NULL) {
794 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
795 adios (NULL, "unable to re-open");
796 cpydgst (fd, fileno (out), tmpfil, bccfil);
799 vec[0] = r1bindex (mhlproc, '/');
801 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
805 adios ("vfork", "unable to");
808 dup2 (fileno (out), 1);
811 vec[i++] = "-forward";
817 execvp (mhlproc, vec);
818 adios (mhlproc, "unable to exec");
821 if (status = pidwait(child_id, OK))
822 admonish (NULL, "%s lost (status=0%o)", vec[0], status);
827 fseek (out, 0L, SEEK_END);
828 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
833 /* FCC INTERACTION */
843 for (i = 0; i < fccind; i++)
845 printf ("Fcc: %s\n", fccfold[i]);
847 fcc (path, fccfold[i]);
852 fcc (char *file, char *folder)
859 printf ("%sFcc: %s\n", msgstate == resent ? "Resent-" : "", folder);
862 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
867 fprintf (stderr, " %sFcc %s: ",
868 msgstate == resent ? "Resent-" : "", folder);
869 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
873 snprintf (fold, sizeof(fold), "%s%s",
874 *folder == '+' || *folder == '@' ? "" : "+", folder);
875 execlp (fileproc, r1bindex (fileproc, '/'),
876 "-link", "-file", file, fold, NULL);
880 if ((status = pidwait(child_id, OK))) {
882 fprintf (stderr, " %sFcc %s: ",
883 msgstate == resent ? "Resent-" : "", folder);
884 fprintf (verbose ? stdout : stderr,
885 " errored (0%o)\n", status);
900 die (char *what, char *fmt, ...)
905 advertise (what, NULL, fmt, ap);