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 */
63 #define PUSHSW 19 /* fork to sendmail then exit */
65 #define NPUSHSW 20 /* exec sendmail */
68 { "library directory", -7 },
70 { "idanno number", -6 },
75 /* flags for headers->flags */
76 #define HNOP 0x0000 /* just used to keep .set around */
77 #define HBAD 0x0001 /* bad header - don't let it through */
78 #define HADR 0x0002 /* header has an address field */
79 #define HSUB 0x0004 /* Subject: header */
80 #define HTRY 0x0008 /* try to send to addrs on header */
81 #define HBCC 0x0010 /* don't output this header */
82 #define HMNG 0x0020 /* mung this header */
83 #define HNGR 0x0040 /* no groups allowed in this header */
84 #define HFCC 0x0080 /* FCC: type header */
85 #define HNIL 0x0100 /* okay for this header not to have addrs */
86 #define HIGN 0x0200 /* ignore this header */
88 /* flags for headers->set */
89 #define MFRM 0x0001 /* we've seen a From: */
90 #define MDAT 0x0002 /* we've seen a Date: */
91 #define MRFM 0x0004 /* we've seen a Resent-From: */
92 #define MVIS 0x0008 /* we've seen sighted addrs */
93 #define MINV 0x0010 /* we've seen blind addrs */
94 #define MRDT 0x0020 /* we've seen a Resent-Date: */
103 static struct headers NHeaders[] = {
104 { "Return-Path", HBAD, 0 },
105 { "Received", HBAD, 0 },
106 { "Reply-To", HADR|HNGR, 0 },
107 { "From", HADR|HNGR, MFRM },
108 { "Sender", HADR|HBAD, 0 },
109 { "Date", HNOP, MDAT },
110 { "Subject", HSUB, 0 },
111 { "To", HADR|HTRY, MVIS },
112 { "cc", HADR|HTRY, MVIS },
113 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
114 { "Message-Id", HBAD, 0 },
119 static struct headers RHeaders[] = {
120 { "Resent-Reply-To", HADR|HNGR, 0 },
121 { "Resent-From", HADR|HNGR, MRFM },
122 { "Resent-Sender", HADR|HBAD, 0 },
123 { "Resent-Date", HNOP, MRDT },
124 { "Resent-Subject", HSUB, 0 },
125 { "Resent-To", HADR|HTRY, MVIS },
126 { "Resent-cc", HADR|HTRY, MVIS },
127 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
128 { "Resent-Message-Id", HBAD, 0 },
129 { "Resent-Fcc", HFCC, 0 },
130 { "Reply-To", HADR, 0 },
136 static short fccind = 0; /* index into fccfold[] */
138 static int badmsg = 0; /* message has bad semantics */
139 static int verbose = 0; /* spell it out */
140 static int debug = 0; /* debugging post */
141 static int rmflg = 1; /* remove temporary file when done */
142 static int watch = 0; /* watch the delivery process */
143 static int backflg = 0; /* rename input file as *.bak when done */
144 static int pushflg = 0; /* if going to fork to sendmail */
145 static int aliasflg = -1; /* if going to process aliases */
146 static int outputlinelen=72;
148 static unsigned msgflags = 0; /* what we've seen */
154 static char tmpfil[] = "/tmp/pstXXXXXX";
156 static char from[BUFSIZ]; /* my network address */
157 static char signature[BUFSIZ]; /* my signature */
158 static char *filter = NULL; /* the filter for BCC'ing */
159 static char *subject = NULL; /* the subject field for BCC'ing */
160 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
162 static struct headers *hdrtab; /* table for the message we're doing */
163 static FILE *out; /* output (temp) file */
165 extern char *sendmail;
168 * external prototypes
170 extern char *getfullname (void);
171 extern char *getusername (void);
173 extern boolean draft_from_masquerading; /* defined in mts.c */
178 static void putfmt (char *, char *, FILE *);
179 static void start_headers (void);
180 static void finish_headers (FILE *);
181 static int get_header (char *, struct headers *);
182 static void putadr (char *, struct mailname *);
183 static int putone (char *, int, int);
184 static void insert_fcc (struct headers *, unsigned char *);
185 static void file (char *);
186 static void fcc (char *, char *);
189 static void die (char *, char *, ...);
190 static void make_bcc_file (void);
195 main (int argc, char **argv)
197 int state, i, pid, compnum;
198 char *cp, *msg = NULL, **argp, **arguments;
199 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
203 setlocale(LC_ALL, "");
205 invo_name = r1bindex (argv[0], '/');
207 /* foil search of user profile/context */
208 if (context_foil (NULL) == -1)
211 mts_init (invo_name);
212 arguments = getarguments (invo_name, argc, argv, 0);
215 while ((cp = *argp++)) {
217 switch (smatch (++cp, switches)) {
219 ambigsw (cp, switches);
222 adios (NULL, "-%s unknown", cp);
225 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
226 print_help (buf, switches, 1);
229 print_version(invo_name);
241 if (!(filter = *argp++) || *filter == '-')
242 adios (NULL, "missing argument to %s", argp[-2]);
284 if (!(cp = *argp++) || *cp == '-')
285 adios (NULL, "missing argument to %s", argp[-2]);
287 alias (AliasFile);/* load default aka's */
289 if ((state = alias(cp)) != AK_OK)
290 adios (NULL, "aliasing error in file %s - %s",
291 cp, akerror(state) );
298 if (!(cp = *argp++) || *cp == '-')
299 adios (NULL, "missing argument to %s", argp[-2]);
300 outputlinelen = atoi (cp);
301 if (outputlinelen <= 10)
306 if (!(cp = *argp++) || *cp == '-')
307 adios (NULL, "missing argument to %s", argp[-2]);
308 /* create a minimal context */
309 if (context_foil (cp) == -1)
314 /* -idanno switch ignored */
315 if (!(cp = *argp++) || *cp == '-')
316 adios (NULL, "missing argument to %s", argp[-2]);
321 adios (NULL, "only one message at a time!");
327 alias (AliasFile); /* load default aka's */
330 adios (NULL, "usage: %s [switches] file", invo_name);
332 if ((in = fopen (msg, "r")) == NULL)
333 adios (msg, "unable to open");
342 if ((out = fdopen( mkstemp (tmpfil), "w" )) == NULL )
343 adios (tmpfil, "unable to create");
346 if ((out = fopen (tmpfil, "w")) == NULL)
347 adios (tmpfil, "unable to create");
348 chmod (tmpfil, 0600);
352 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
354 for (compnum = 1, state = FLD;;) {
355 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
358 putfmt (name, buf, out);
364 while (state == FLDPLUS) {
365 state = m_getfld (state, name, buf, sizeof(buf), in);
368 putfmt (name, cp, out);
373 finish_headers (out);
374 fprintf (out, "\n%s", buf);
375 while (state == BODY) {
376 state = m_getfld (state, name, buf, sizeof(buf), in);
382 finish_headers (out);
387 adios (NULL, "message format error in component #%d",
391 adios (NULL, "getfld() returned %d", state);
398 strncpy (buf, m_backup (msg), sizeof(buf));
399 if (rename (msg, buf) == NOTOK)
400 advise (buf, "unable to rename %s to", msg);
412 * re-open the temp file, unlink it and exec sendmail, giving it
413 * the msg temp file as std in.
415 if ( freopen( tmpfil, "r", stdin) == NULL)
416 adios (tmpfil, "can't reopen for sendmail");
421 *argp++ = "send-mail";
422 *argp++ = "-m"; /* send to me too */
423 *argp++ = "-t"; /* read msg for recipients */
424 *argp++ = "-i"; /* don't stop on "." */
425 if (watch || verbose)
429 if (pushflg && !(watch || verbose)) {
430 /* fork to a child to run sendmail */
431 for (i=0; (pid = vfork()) == NOTOK && i < 5; i++)
435 fprintf (verbose ? stdout : stderr, "%s: can't fork to %s\n",
436 invo_name, sendmail);
439 /* we're the child .. */
445 execv ( sendmail, sargv);
446 adios ( sendmail, "can't exec");
447 return 0; /* dead code to satisfy the compiler */
450 /* DRAFT GENERATION */
453 putfmt (char *name, char *str, FILE *out)
459 while (*str == ' ' || *str == '\t')
462 if ((i = get_header (name, hdrtab)) == NOTOK) {
463 fprintf (out, "%s: %s", name, str);
468 if (hdr->flags & HIGN)
470 if (hdr->flags & HBAD) {
471 advise (NULL, "illegal header line -- %s:", name);
475 msgflags |= hdr->set;
477 if (hdr->flags & HSUB)
478 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
480 if (hdr->flags & HFCC) {
481 if ((cp = strrchr(str, '\n')))
483 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
485 insert_fcc (hdr, pp);
487 insert_fcc (hdr, pp);
492 if (hdr->flags & HBCC) {
498 if (*str != '\n' && *str != '\0') {
499 if (aliasflg && hdr->flags & HTRY) {
500 /* this header contains address(es) that we have to do
501 * alias expansion on. Because of the saved state in
502 * getname we have to put all the addresses into a list.
503 * We then let putadr munch on that list, possibly
506 register struct mailname *f = 0;
507 register struct mailname *mp = 0;
509 while ((cp = getname(str))) {
510 mp = getm( cp, NULL, 0, AD_HOST, NULL);
515 mp->m_next = f->m_next;
520 f = mp->m_next; mp->m_next = 0;
524 * The author(s) of spost decided that alias
525 * substitution wasn't necessary for the non-HTRY
526 * headers. Unfortunately, one of those headers
527 * is "From:", and having alias substitution
528 * work on that is extremely useful for someone
529 * with a lot of POP3 email accounts or aliases.
530 * post supports aliasing of "From:"...
532 * Since "From:"-processing is incompletely
533 * implemented in this unsupported and
534 * undocumented spost backend, I'm not
535 * going to take the time to implement my new
536 * draft-From:-based email address masquerading.
537 * If I do ever implement it here, I'd almost
538 * certainly want to implement "From:" line
539 * alias processing as well.
540 * -- Dan Harkless <dan-nmh@dilvish.speed.net>
542 fprintf (out, "%s: %s", name, str );
554 strncpy(from, getusername(), sizeof(from));
556 if ((cp = getfullname ()) && *cp) {
557 strncpy (sigbuf, cp, sizeof(sigbuf));
558 snprintf (signature, sizeof(signature), "%s <%s>", sigbuf, from);
560 snprintf (signature, sizeof(signature), "%s", from);
565 finish_headers (FILE *out)
569 if (!(msgflags & MDAT))
570 fprintf (out, "Date: %s\n", dtimenow (0));
572 if (msgflags & MFRM) {
573 /* There was already a From: in the draft. Don't add one. */
574 if (!draft_from_masquerading)
576 * mts.conf didn't contain
577 * "masquerade:[...]draft_from[...]"
578 * so we'll reveal the user's
579 * actual account@thismachine
580 * address in a Sender: header
581 * (and use it as the envelope
584 fprintf (out, "Sender: %s\n", from);
586 fprintf (out, "From: %s\n", signature);
589 if (!(msgflags & MVIS))
590 fprintf (out, "Bcc: Blind Distribution List: ;\n");
595 if (!(msgflags & MRDT))
596 fprintf (out, "Resent-Date: %s\n", dtimenow(0));
597 if (msgflags & MRFM) {
598 /* There was already a Resent-From: in draft. Don't add one. */
599 if (!draft_from_masquerading)
601 * mts.conf didn't contain
602 * "masquerade:[...]draft_from[...]"
603 * so we'll reveal the user's
604 * actual account@thismachine
605 * address in a Sender: header
606 * (and use it as the envelope
609 fprintf (out, "Resent-Sender: %s\n", from);
611 /* Construct a Resent-From: header. */
612 fprintf (out, "Resent-From: %s\n", signature);
614 if (!(msgflags & MVIS))
615 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
621 adios (NULL, "re-format message and try again");
626 get_header (char *header, struct headers *table)
630 for (h = table; h->value; h++)
631 if (!mh_strcasecmp (header, h->value))
639 * output the address list for header "name". The address list
640 * is a linked list of mailname structs. "nl" points to the head
641 * of the list. Alias substitution should be done on nl.
644 putadr (char *name, struct mailname *nl)
646 register struct mailname *mp, *mp2;
647 register int linepos;
651 fprintf (out, "%s: ", name);
652 namelen = strlen(name) + 2;
655 for (mp = nl; mp; ) {
656 if (linepos > MAX_SM_FIELD) {
657 fprintf (out, "\n%s: ", name);
661 /* a local name - see if it's an alias */
662 cp = akvalue(mp->m_mbox);
663 if (cp == mp->m_mbox)
664 /* wasn't an alias - use what the user typed */
665 linepos = putone( mp->m_text, linepos, namelen );
667 /* an alias - expand it */
668 while ((cp = getname(cp))) {
669 if (linepos > MAX_SM_FIELD) {
670 fprintf (out, "\n%s: ", name);
673 mp2 = getm( cp, NULL, 0, AD_HOST, NULL);
675 mp2->m_pers = getcpy(mp->m_mbox);
676 linepos = putone( adrformat(mp2), linepos, namelen );
678 linepos = putone( mp2->m_text, linepos, namelen );
683 /* not a local name - use what the user typed */
684 linepos = putone( mp->m_text, linepos, namelen );
694 putone (char *adr, int pos, int indent)
702 else if ( linepos+len > outputlinelen ) {
703 fprintf ( out, ",\n%*s", indent, "");
720 insert_fcc (struct headers *hdr, unsigned char *pp)
724 for (cp = pp; isspace (*cp); cp++)
726 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
734 adios (NULL, "too many %ss", hdr->value);
735 fccfold[fccind++] = getcpy (cp);
750 fd = mkstemp(bccfil);
751 if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
752 adios (bccfil, "unable to create");
755 if ((out = fopen (bccfil, "w")) == NULL)
756 adios (bccfil, "unable to create");
758 chmod (bccfil, 0600);
760 fprintf (out, "Date: %s\n", dtimenow (0));
761 if (msgflags & MFRM) {
762 /* There was already a From: in the draft. Don't add one. */
763 if (!draft_from_masquerading)
765 * mts.conf didn't contain "masquerade:[...]draft_from[...]"
766 * so we'll reveal the user's actual account@thismachine
767 * address in a Sender: header (and use it as the envelope
770 fprintf (out, "Sender: %s\n", from);
772 /* Construct a From: header. */
773 fprintf (out, "From: %s\n", signature);
775 fprintf (out, "Subject: %s", subject);
776 fprintf (out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
779 if (filter == NULL) {
780 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
781 adios (NULL, "unable to re-open");
782 cpydgst (fd, fileno (out), tmpfil, bccfil);
785 vec[0] = r1bindex (mhlproc, '/');
787 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
791 adios ("vfork", "unable to");
794 dup2 (fileno (out), 1);
797 vec[i++] = "-forward";
803 execvp (mhlproc, vec);
804 adios (mhlproc, "unable to exec");
807 if (status = pidwait(child_id, OK))
808 admonish (NULL, "%s lost (status=0%o)", vec[0], status);
813 fseek (out, 0L, SEEK_END);
814 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
819 /* FCC INTERACTION */
829 for (i = 0; i < fccind; i++)
830 fcc (path, fccfold[i]);
835 fcc (char *file, char *folder)
842 printf ("%sFcc: %s\n", msgstate == resent ? "Resent-" : "", folder);
845 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
850 fprintf (stderr, " %sFcc %s: ",
851 msgstate == resent ? "Resent-" : "", folder);
852 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
856 snprintf (fold, sizeof(fold), "%s%s",
857 *folder == '+' || *folder == '@' ? "" : "+", folder);
858 execlp (fileproc, r1bindex (fileproc, '/'),
859 "-link", "-file", file, fold, NULL);
863 if ((status = pidwait(child_id, OK))) {
865 fprintf (stderr, " %sFcc %s: ",
866 msgstate == resent ? "Resent-" : "", folder);
867 fprintf (verbose ? stdout : stderr,
868 " errored (0%o)\n", status);
883 die (char *what, char *fmt, ...)
888 advertise (what, NULL, fmt, ap);