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 uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : c)
26 #define MAX_SM_FIELD 1476 /* < largest hdr field sendmail will accept */
27 #define FCCS 10 /* max number of fccs allowed */
29 struct swit switches[] = {
31 { "filter filterfile", 0 },
55 { "alias aliasfile", 0 },
59 { "width columns", 0 },
67 { "dist", -4 }, /* interface from dist */
69 { "check", -5 }, /* interface from whom */
71 { "nocheck", -7 }, /* interface from whom */
73 { "whom", -4 }, /* interface from whom */
74 #define PUSHSW 22 /* fork to sendmail then exit */
76 #define NPUSHSW 23 /* exec sendmail */
79 { "library directory", -7 },
81 { "idanno number", -6 },
86 /* flags for headers->flags */
87 #define HNOP 0x0000 /* just used to keep .set around */
88 #define HBAD 0x0001 /* bad header - don't let it through */
89 #define HADR 0x0002 /* header has an address field */
90 #define HSUB 0x0004 /* Subject: header */
91 #define HTRY 0x0008 /* try to send to addrs on header */
92 #define HBCC 0x0010 /* don't output this header */
93 #define HMNG 0x0020 /* mung this header */
94 #define HNGR 0x0040 /* no groups allowed in this header */
95 #define HFCC 0x0080 /* FCC: type header */
96 #define HNIL 0x0100 /* okay for this header not to have addrs */
97 #define HIGN 0x0200 /* ignore this header */
99 /* flags for headers->set */
100 #define MFRM 0x0001 /* we've seen a From: */
101 #define MDAT 0x0002 /* we've seen a Date: */
102 #define MRFM 0x0004 /* we've seen a Resent-From: */
103 #define MVIS 0x0008 /* we've seen sighted addrs */
104 #define MINV 0x0010 /* we've seen blind addrs */
105 #define MRDT 0x0020 /* we've seen a Resent-Date: */
114 static struct headers NHeaders[] = {
115 { "Return-Path", HBAD, 0 },
116 { "Received", HBAD, 0 },
117 { "Reply-To", HADR|HNGR, 0 },
118 { "From", HADR|HNGR, MFRM },
119 { "Sender", HADR|HBAD, 0 },
120 { "Date", HNOP, MDAT },
121 { "Subject", HSUB, 0 },
122 { "To", HADR|HTRY, MVIS },
123 { "cc", HADR|HTRY, MVIS },
124 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
125 { "Message-Id", HBAD, 0 },
130 static struct headers RHeaders[] = {
131 { "Resent-Reply-To", HADR|HNGR, 0 },
132 { "Resent-From", HADR|HNGR, MRFM },
133 { "Resent-Sender", HADR|HBAD, 0 },
134 { "Resent-Date", HNOP, MRDT },
135 { "Resent-Subject", HSUB, 0 },
136 { "Resent-To", HADR|HTRY, MVIS },
137 { "Resent-cc", HADR|HTRY, MVIS },
138 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
139 { "Resent-Message-Id", HBAD, 0 },
140 { "Resent-Fcc", HFCC, 0 },
141 { "Reply-To", HADR, 0 },
147 static short fccind = 0; /* index into fccfold[] */
149 static int badmsg = 0; /* message has bad semantics */
150 static int verbose = 0; /* spell it out */
151 static int debug = 0; /* debugging post */
152 static int rmflg = 1; /* remove temporary file when done */
153 static int watch = 0; /* watch the delivery process */
154 static int backflg = 0; /* rename input file as *.bak when done */
155 static int whomflg = 0; /* if just checking addresses */
156 static int pushflg = 0; /* if going to fork to sendmail */
157 static int aliasflg = -1; /* if going to process aliases */
158 static int outputlinelen=72;
160 static unsigned msgflags = 0; /* what we've seen */
166 static char tmpfil[] = "/tmp/pstXXXXXX";
168 static char from[BUFSIZ]; /* my network address */
169 static char signature[BUFSIZ]; /* my signature */
170 static char *filter = NULL; /* the filter for BCC'ing */
171 static char *subject = NULL; /* the subject field for BCC'ing */
172 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
174 static struct headers *hdrtab; /* table for the message we're doing */
175 static FILE *out; /* output (temp) file */
177 extern char *sendmail;
180 * external prototypes
182 extern char *getfullname (void);
183 extern char *getusername (void);
185 extern boolean draft_from_masquerading; /* defined in mts.c */
190 static void putfmt (char *, char *, FILE *);
191 static void start_headers (void);
192 static void finish_headers (FILE *);
193 static int get_header (char *, struct headers *);
194 static void putadr (char *, struct mailname *);
195 static int putone (char *, int, int);
196 static void insert_fcc (struct headers *, char *);
197 static void file (char *);
198 static void fcc (char *, char *);
201 static void die (char *, char *, ...);
202 static void make_bcc_file (void);
207 main (int argc, char **argv)
209 int state, i, pid, compnum;
210 char *cp, *msg = NULL, **argp, **arguments;
211 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
215 setlocale(LC_ALL, "");
217 invo_name = r1bindex (argv[0], '/');
219 /* foil search of user profile/context */
220 if (context_foil (NULL) == -1)
223 mts_init (invo_name);
224 arguments = getarguments (invo_name, argc, argv, 0);
227 while ((cp = *argp++)) {
229 switch (smatch (++cp, switches)) {
231 ambigsw (cp, switches);
234 adios (NULL, "-%s unknown", cp);
237 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
238 print_help (buf, switches, 1);
241 print_version(invo_name);
257 if (!(filter = *argp++) || *filter == '-')
258 adios (NULL, "missing argument to %s", argp[-2]);
300 if (!(cp = *argp++) || *cp == '-')
301 adios (NULL, "missing argument to %s", argp[-2]);
303 alias (AliasFile);/* load default aka's */
305 if ((state = alias(cp)) != AK_OK)
306 adios (NULL, "aliasing error in file %s - %s",
307 cp, akerror(state) );
314 if (!(cp = *argp++) || *cp == '-')
315 adios (NULL, "missing argument to %s", argp[-2]);
316 outputlinelen = atoi (cp);
317 if (outputlinelen <= 10)
322 if (!(cp = *argp++) || *cp == '-')
323 adios (NULL, "missing argument to %s", argp[-2]);
324 /* create a minimal context */
325 if (context_foil (cp) == -1)
330 /* -idanno switch ignored */
331 if (!(cp = *argp++) || *cp == '-')
332 adios (NULL, "missing argument to %s", argp[-2]);
337 adios (NULL, "only one message at a time!");
343 alias (AliasFile); /* load default aka's */
346 adios (NULL, "usage: %s [switches] file", invo_name);
348 if ((in = fopen (msg, "r")) == NULL)
349 adios (msg, "unable to open");
358 if ((out = fdopen( mkstemp (tmpfil), "w" )) == NULL )
359 adios (tmpfil, "unable to create");
362 if ((out = fopen (tmpfil, "w")) == NULL)
363 adios (tmpfil, "unable to create");
364 chmod (tmpfil, 0600);
368 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
370 for (compnum = 1, state = FLD;;) {
371 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
374 putfmt (name, buf, out);
380 while (state == FLDPLUS) {
381 state = m_getfld (state, name, buf, sizeof(buf), in);
384 putfmt (name, cp, out);
389 finish_headers (out);
390 fprintf (out, "\n%s", buf);
392 while (state == BODY) {
393 state = m_getfld (state, name, buf, sizeof(buf), in);
399 finish_headers (out);
404 adios (NULL, "message format error in component #%d",
408 adios (NULL, "getfld() returned %d", state);
414 if (backflg && !whomflg) {
415 strncpy (buf, m_backup (msg), sizeof(buf));
416 if (rename (msg, buf) == NOTOK)
417 advise (buf, "unable to rename %s to", msg);
429 * re-open the temp file, unlink it and exec sendmail, giving it
430 * the msg temp file as std in.
432 if ( freopen( tmpfil, "r", stdin) == NULL)
433 adios (tmpfil, "can't reopen for sendmail");
438 *argp++ = "send-mail";
439 *argp++ = "-m"; /* send to me too */
440 *argp++ = "-t"; /* read msg for recipients */
441 *argp++ = "-i"; /* don't stop on "." */
444 if (watch || verbose)
448 if (pushflg && !(watch || verbose)) {
449 /* fork to a child to run sendmail */
450 for (i=0; (pid = vfork()) == NOTOK && i < 5; i++)
454 fprintf (verbose ? stdout : stderr, "%s: can't fork to %s\n",
455 invo_name, sendmail);
458 /* we're the child .. */
464 execv ( sendmail, sargv);
465 adios ( sendmail, "can't exec");
466 return 0; /* dead code to satisfy the compiler */
469 /* DRAFT GENERATION */
472 putfmt (char *name, char *str, FILE *out)
478 while (*str == ' ' || *str == '\t')
481 if ((i = get_header (name, hdrtab)) == NOTOK) {
482 fprintf (out, "%s: %s", name, str);
487 if (hdr->flags & HIGN)
489 if (hdr->flags & HBAD) {
490 advise (NULL, "illegal header line -- %s:", name);
494 msgflags |= hdr->set;
496 if (hdr->flags & HSUB)
497 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
499 if (hdr->flags & HFCC) {
500 if ((cp = strrchr(str, '\n')))
502 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
504 insert_fcc (hdr, pp);
506 insert_fcc (hdr, pp);
511 if (hdr->flags & HBCC) {
517 if (*str != '\n' && *str != '\0') {
518 if (aliasflg && hdr->flags & HTRY) {
519 /* this header contains address(es) that we have to do
520 * alias expansion on. Because of the saved state in
521 * getname we have to put all the addresses into a list.
522 * We then let putadr munch on that list, possibly
525 register struct mailname *f = 0;
526 register struct mailname *mp = 0;
528 while ((cp = getname(str))) {
529 mp = getm( cp, NULL, 0, AD_HOST, NULL);
534 mp->m_next = f->m_next;
539 f = mp->m_next; mp->m_next = 0;
542 /* The author(s) of spost decided that alias substitution wasn't
543 necessary for the non-HTRY headers. Unfortunately, one of those
544 headers is "From:", and having alias substitution work on that is
545 extremely useful for someone with a lot of POP3 email accounts or
546 aliases. post supports aliasing of "From:"...
548 Since "From:"-processing is incompletely implemented in this
549 unsupported and undocumented spost backend, I'm not going to take
550 the time to implement my new draft-From:-based email address
551 masquerading. If I do ever implement it here, I'd almost
552 certainly want to implement "From:" line alias processing as
553 well. -- Dan Harkless <dan-nmh@dilvish.speed.net> */
554 fprintf (out, "%s: %s", name, str );
566 strncpy(from, getusername(), sizeof(from));
568 if ((cp = getfullname ()) && *cp) {
569 strncpy (sigbuf, cp, sizeof(sigbuf));
570 snprintf (signature, sizeof(signature), "%s <%s>", sigbuf, from);
573 snprintf (signature, sizeof(signature), "%s", from);
578 finish_headers (FILE *out)
582 if (!(msgflags & MDAT))
583 fprintf (out, "Date: %s\n", dtimenow (0));
585 if (msgflags & MFRM) {
586 /* There was already a From: in the draft. Don't add one. */
587 if (!draft_from_masquerading)
588 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
589 so we'll reveal the user's actual account@thismachine
590 address in a Sender: header (and use it as the envelope
592 fprintf (out, "Sender: %s\n", from);
595 fprintf (out, "From: %s\n", signature);
598 if (!(msgflags & MVIS))
599 fprintf (out, "Bcc: Blind Distribution List: ;\n");
604 if (!(msgflags & MRDT))
605 fprintf (out, "Resent-Date: %s\n", dtimenow(0));
606 if (msgflags & MRFM) {
607 /* There was already a Resent-From: in draft. Don't add one. */
608 if (!draft_from_masquerading)
609 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
610 so we'll reveal the user's actual account@thismachine
611 address in a Sender: header (and use it as the envelope
613 fprintf (out, "Resent-Sender: %s\n", from);
616 /* Construct a Resent-From: header. */
617 fprintf (out, "Resent-From: %s\n", signature);
619 if (!(msgflags & MVIS))
620 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
626 adios (NULL, "re-format message and try again");
631 get_header (char *header, struct headers *table)
635 for (h = table; h->value; h++)
636 if (!strcasecmp (header, h->value))
644 * output the address list for header "name". The address list
645 * is a linked list of mailname structs. "nl" points to the head
646 * of the list. Alias substitution should be done on nl.
649 putadr (char *name, struct mailname *nl)
651 register struct mailname *mp, *mp2;
652 register int linepos;
656 fprintf (out, "%s: ", name);
657 namelen = strlen(name) + 2;
660 for (mp = nl; mp; ) {
661 if (linepos > MAX_SM_FIELD) {
662 fprintf (out, "\n%s: ", name);
666 /* a local name - see if it's an alias */
667 cp = akvalue(mp->m_mbox);
668 if (cp == mp->m_mbox)
669 /* wasn't an alias - use what the user typed */
670 linepos = putone( mp->m_text, linepos, namelen );
672 /* an alias - expand it */
673 while ((cp = getname(cp))) {
674 if (linepos > MAX_SM_FIELD) {
675 fprintf (out, "\n%s: ", name);
678 mp2 = getm( cp, NULL, 0, AD_HOST, NULL);
680 mp2->m_pers = getcpy(mp->m_mbox);
681 linepos = putone( adrformat(mp2), linepos, namelen );
683 linepos = putone( mp2->m_text, linepos, namelen );
688 /* not a local name - use what the user typed */
689 linepos = putone( mp->m_text, linepos, namelen );
699 putone (char *adr, int pos, int indent)
707 else if ( linepos+len > outputlinelen ) {
708 fprintf ( out, ",\n%*s", indent, "");
725 insert_fcc (struct headers *hdr, char *pp)
729 for (cp = pp; isspace (*cp); cp++)
731 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
739 adios (NULL, "too many %ss", hdr->value);
740 fccfold[fccind++] = getcpy (cp);
755 fd = mkstemp(bccfil);
756 if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
757 adios (bccfil, "unable to create");
760 if ((out = fopen (bccfil, "w")) == NULL)
761 adios (bccfil, "unable to create");
763 chmod (bccfil, 0600);
765 fprintf (out, "Date: %s\n", dtimenow (0));
766 fprintf (out, "From: %s\n", signature);
768 fprintf (out, "Subject: %s", subject);
769 fprintf (out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
772 if (filter == NULL) {
773 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
774 adios (NULL, "unable to re-open");
775 cpydgst (fd, fileno (out), tmpfil, bccfil);
779 vec[0] = r1bindex (mhlproc, '/');
781 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
785 adios ("vfork", "unable to");
788 dup2 (fileno (out), 1);
791 vec[i++] = "-forward";
797 execvp (mhlproc, vec);
798 adios (mhlproc, "unable to exec");
801 if (status = pidwait(child_id, OK))
802 admonish (NULL, "%s lost (status=0%o)", vec[0], status);
807 fseek (out, 0L, SEEK_END);
808 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
813 /* FCC INTERACTION */
823 for (i = 0; i < fccind; i++)
825 printf ("Fcc: %s\n", fccfold[i]);
827 fcc (path, fccfold[i]);
832 fcc (char *file, char *folder)
839 printf ("%sFcc: %s\n", msgstate == resent ? "Resent-" : "", folder);
842 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
847 fprintf (stderr, " %sFcc %s: ",
848 msgstate == resent ? "Resent-" : "", folder);
849 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
853 snprintf (fold, sizeof(fold), "%s%s",
854 *folder == '+' || *folder == '@' ? "" : "+", folder);
855 execlp (fileproc, r1bindex (fileproc, '/'),
856 "-link", "-file", file, fold, NULL);
860 if ((status = pidwait(child_id, OK))) {
862 fprintf (stderr, " %sFcc %s: ",
863 msgstate == resent ? "Resent-" : "", folder);
864 fprintf (verbose ? stdout : stderr,
865 " errored (0%o)\n", status);
880 die (char *what, char *fmt, ...)
885 advertise (what, NULL, fmt, ap);