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 },
42 { "alias aliasfile", 0 },
52 { "dist", -4 }, /* interface from dist */
53 #define PUSHSW 14 /* fork to sendmail then exit */
55 #define NPUSHSW 15 /* exec sendmail */
58 { "library directory", -7 },
63 /* flags for headers->flags */
64 #define HNOP 0x0000 /* just used to keep .set around */
65 #define HBAD 0x0001 /* bad header - don't let it through */
66 #define HADR 0x0002 /* header has an address field */
67 #define HSUB 0x0004 /* Subject: header */
68 #define HTRY 0x0008 /* try to send to addrs on header */
69 #define HBCC 0x0010 /* don't output this header */
70 #define HMNG 0x0020 /* mung this header */
71 #define HNGR 0x0040 /* no groups allowed in this header */
72 #define HFCC 0x0080 /* FCC: type header */
73 #define HNIL 0x0100 /* okay for this header not to have addrs */
74 #define HIGN 0x0200 /* ignore this header */
76 /* flags for headers->set */
77 #define MFRM 0x0001 /* we've seen a From: */
78 #define MDAT 0x0002 /* we've seen a Date: */
79 #define MRFM 0x0004 /* we've seen a Resent-From: */
80 #define MVIS 0x0008 /* we've seen sighted addrs */
81 #define MINV 0x0010 /* we've seen blind addrs */
82 #define MRDT 0x0020 /* we've seen a Resent-Date: */
91 static struct headers NHeaders[] = {
92 { "Return-Path", HBAD, 0 },
93 { "Received", HBAD, 0 },
94 { "Reply-To", HADR|HNGR, 0 },
95 { "From", HADR|HNGR, MFRM },
96 { "Sender", HADR|HBAD, 0 },
97 { "Date", HNOP, MDAT },
98 { "Subject", HSUB, 0 },
99 { "To", HADR|HTRY, MVIS },
100 { "Cc", HADR|HTRY, MVIS },
101 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
102 { "Message-Id", HBAD, 0 },
107 static struct headers RHeaders[] = {
108 { "Resent-Reply-To", HADR|HNGR, 0 },
109 { "Resent-From", HADR|HNGR, MRFM },
110 { "Resent-Sender", HADR|HBAD, 0 },
111 { "Resent-Date", HNOP, MRDT },
112 { "Resent-Subject", HSUB, 0 },
113 { "Resent-To", HADR|HTRY, MVIS },
114 { "Resent-Cc", HADR|HTRY, MVIS },
115 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
116 { "Resent-Message-Id", HBAD, 0 },
117 { "Resent-Fcc", HFCC, 0 },
118 { "Reply-To", HADR, 0 },
124 static int badmsg = 0; /* message has bad semantics */
125 static int verbose = 0; /* spell it out */
126 static int debug = 0; /* debugging post */
127 static int watch = 0; /* watch the delivery process */
128 static int backflg = 0; /* prepend backup prefix to input file when done */
129 static int pushflg = 0; /* if going to fork to sendmail */
130 static int aliasflg = 0; /* if going to process aliases */
132 static unsigned msgflags = 0; /* what we've seen */
138 static char tmpfil[] = "/tmp/pstXXXXXX";
140 static char from[BUFSIZ]; /* my network address */
141 static char signature[BUFSIZ]; /* my signature */
142 static char *filter = NULL; /* the filter for BCC'ing */
143 static char *subject = NULL; /* the subject field for BCC'ing */
144 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
145 static short fccind = 0; /* index into fccfold[] */
147 static struct headers *hdrtab; /* table for the message we're doing */
148 static FILE *out; /* output (temp) file */
150 extern char *sendmail;
153 ** external prototypes
155 extern char *getfullname(void);
156 extern char *getusername(void);
158 extern boolean draft_from_masquerading; /* defined in mts.c */
163 static void putfmt(char *, char *, FILE *);
164 static void start_headers(void);
165 static void finish_headers(FILE *);
166 static int get_header(char *, struct headers *);
167 static void putadr(char *, struct mailname *);
168 static int putone(char *, int, int);
169 static void insert_fcc(char *, unsigned char *);
170 static void fcc(char *, char *);
173 static void make_bcc_file(void);
178 main(int argc, char **argv)
180 int state, i, pid, compnum;
181 char *cp, *msg = NULL, **argp, **arguments;
182 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
186 setlocale(LC_ALL, "");
188 invo_name = mhbasename(argv[0]);
190 /* foil search of user profile/context */
191 if (context_foil(NULL) == -1)
195 arguments = getarguments(invo_name, argc, argv, 0);
198 while ((cp = *argp++)) {
200 switch (smatch(++cp, switches)) {
202 ambigsw(cp, switches);
205 adios(NULL, "-%s unknown", cp);
208 snprintf(buf, sizeof(buf),
209 "%s [switches] file",
211 print_help(buf, switches, 1);
214 print_version(invo_name);
226 if (!(filter = *argp++) || *filter == '-')
227 adios(NULL, "missing argument to %s",
263 if (!(cp = *argp++) || *cp == '-')
264 adios(NULL, "missing argument to %s",
267 if ((state = alias(cp)) != AK_OK)
268 adios(NULL, "aliasing error in file %s - %s", cp, akerror(state));
275 if (!(cp = *argp++) || *cp == '-')
276 adios(NULL, "missing argument to %s",
278 /* create a minimal context */
279 if (context_foil(cp) == -1)
285 adios(NULL, "only one message at a time!");
291 adios(NULL, "usage: %s [switches] file", invo_name);
293 if ((in = fopen(msg, "r")) == NULL)
294 adios(msg, "unable to open");
302 if ((out = fdopen(mkstemp(tmpfil), "w")) == NULL)
303 adios(tmpfil, "unable to create");
306 if ((out = fopen(tmpfil, "w")) == NULL)
307 adios(tmpfil, "unable to create");
312 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
314 for (compnum = 1, state = FLD;;) {
315 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
318 putfmt(name, buf, out);
324 while (state == FLDPLUS) {
325 state = m_getfld(state, name, buf,
329 putfmt(name, cp, out);
335 fprintf(out, "\n%s", buf);
336 while (state == BODY) {
337 state = m_getfld(state, name, buf,
349 adios(NULL, "message format error in component #%d",
353 adios(NULL, "getfld() returned %d", state);
360 strncpy(buf, m_backup(msg), sizeof(buf));
361 if (rename(msg, buf) == NOTOK)
362 advise(buf, "unable to rename %s to", msg);
373 for (i=0; i<fccind; i++) {
374 fcc(tmpfil, fccfold[i]);
378 ** re-open the temp file, unlink it and exec sendmail, giving it
379 ** the msg temp file as std in.
381 if (!freopen(tmpfil, "r", stdin)) {
382 adios(tmpfil, "can't reopen for sendmail");
387 *argp++ = "send-mail";
388 *argp++ = "-m"; /* send to me too */
389 *argp++ = "-t"; /* read msg for recipients */
390 *argp++ = "-i"; /* don't stop on "." */
391 if (watch || verbose)
395 if (pushflg && !(watch || verbose)) {
396 /* fork to a child to run sendmail */
397 for (i=0; (pid = fork()) == NOTOK && i < 5; i++)
401 fprintf(verbose ? stdout : stderr,
402 "%s: can't fork to %s\n",
403 invo_name, sendmail);
406 /* we're the child .. */
412 execv(sendmail, sargv);
413 adios(sendmail, "can't exec");
414 return 0; /* dead code to satisfy the compiler */
417 /* DRAFT GENERATION */
420 putfmt(char *name, char *str, FILE *out)
426 while (*str == ' ' || *str == '\t')
429 if ((i = get_header(name, hdrtab)) == NOTOK) {
430 /* some boring header: push it through */
431 fprintf(out, "%s: %s", name, str);
435 /* one of the interesting headers */
437 if (hdr->flags & HIGN) {
440 if (hdr->flags & HBAD) {
441 advise(NULL, "illegal header line -- %s:", name);
445 msgflags |= hdr->set;
447 if (hdr->flags & HSUB) {
449 /* append mupliple subject */
450 char *cp = concat(subject, "\t", str, NULL);
454 subject = getcpy(str);
458 if (hdr->flags & HFCC) {
459 if ((cp = strrchr(str, '\n'))) {
462 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
464 insert_fcc(hdr->value, pp);
466 insert_fcc(hdr->value, pp);
471 if (hdr->flags & HBCC) {
477 if (*str != '\n' && *str != '\0') {
478 if (aliasflg && hdr->flags & HTRY) {
480 ** this header contains address(es) that we have to do
481 ** alias expansion on. Because of the saved state in
482 ** getname we have to put all the addresses into a
483 ** list. We then let putadr munch on that list,
484 ** possibly expanding aliases.
486 register struct mailname *f = 0;
487 register struct mailname *mp = 0;
489 while ((cp = getname(str))) {
490 mp = getm(cp, NULL, 0, AD_HOST, NULL);
495 mp->m_next = f->m_next;
500 f = mp->m_next; mp->m_next = 0;
504 ** The author(s) of spost decided that alias
505 ** substitution wasn't necessary for the non-HTRY
506 ** headers. Unfortunately, one of those headers
507 ** is "From:", and having alias substitution
508 ** work on that is extremely useful for someone
509 ** with a lot of POP3 email accounts or aliases.
510 ** post supports aliasing of "From:"...
512 ** Since "From:"-processing is incompletely
513 ** implemented in this unsupported and
514 ** undocumented spost backend, I'm not
515 ** going to take the time to implement my new
516 ** draft-From:-based email address masquerading.
517 ** If I do ever implement it here, I'd almost
518 ** certainly want to implement "From:" line
519 ** alias processing as well.
520 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
522 fprintf(out, "%s: %s", name, str);
529 ** Construct signature name
536 strncpy(from, getusername(), sizeof(from));
537 if ((cp = getfullname()) && *cp) {
538 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
540 snprintf(signature, sizeof(signature), "%s", from);
546 ** Add yet missing headers.
549 finish_headers(FILE *out)
551 char *resentstr = (msgstate == resent) ? "Resent-" : "";
553 if (!(msgflags & MDAT)) {
554 fprintf(out, "%sDate: %s\n", resentstr, dtimenow(0));
556 if (!(msgflags & MFRM)) {
557 fprintf(out, "%sFrom: %s\n", resentstr, signature);
558 } else if (!draft_from_masquerading) {
560 ** mts.conf didn't contain "masquerade:[...]draft_from[...]"
561 ** so we'll reveal the user's actual account@thismachine
562 ** address in a Sender: header (and use it as the envelope
565 fprintf(out, "%sSender: %s\n", resentstr, from);
568 if (!(msgflags & MVIS))
569 fprintf(out, "%sBcc: Blind Distribution List: ;\n", resentstr);
573 adios(NULL, "re-format message and try again");
579 ** Return index of the requested header in the table, or NOTOK if missing.
582 get_header(char *header, struct headers *table)
586 for (h=table; h->value; h++) {
587 if (mh_strcasecmp(header, h->value)==0) {
597 ** output the address list for header "name". The address list
598 ** is a linked list of mailname structs. "nl" points to the head
599 ** of the list. Alias substitution should be done on nl.
602 putadr(char *name, struct mailname *nl)
604 register struct mailname *mp, *mp2;
605 register int linepos;
609 fprintf(out, "%s: ", name);
610 namelen = strlen(name) + 2;
613 for (mp = nl; mp; ) {
614 if (linepos > MAX_SM_FIELD) {
615 fprintf(out, "\n%s: ", name);
619 /* a local name - see if it's an alias */
620 cp = akvalue(mp->m_mbox);
621 if (cp == mp->m_mbox) {
622 /* wasn't an alias - use what the user typed */
623 linepos = putone(mp->m_text, linepos, namelen);
625 /* an alias - expand it */
626 while ((cp = getname(cp))) {
627 if (linepos > MAX_SM_FIELD) {
628 fprintf(out, "\n%s: ", name);
631 mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
633 mp2->m_pers = getcpy(mp->m_mbox);
634 linepos = putone(adrformat(mp2), linepos, namelen);
636 linepos = putone(mp2->m_text,
644 /* not a local name - use what the user typed */
645 linepos = putone(mp->m_text, linepos, namelen);
655 putone(char *adr, int pos, int indent)
663 else if (linepos+len > OUTPUTLINELEN) {
664 fprintf(out, ",\n%*s", indent, "");
680 ** Insert the normalized value from pp into fccfold[].
683 insert_fcc(char *name, unsigned char *pp)
687 for (cp = pp; isspace(*cp); cp++)
689 for (pp += strlen(pp) - 1; pp > cp && isspace(*pp); pp--)
697 adios(NULL, "too many %ss", name);
698 fccfold[fccind++] = getcpy(cp);
713 fd = mkstemp(bccfil);
714 if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
715 adios(bccfil, "unable to create");
718 if ((out = fopen(bccfil, "w")) == NULL)
719 adios(bccfil, "unable to create");
723 fprintf(out, "Date: %s\n", dtimenow(0));
724 if (!(msgflags & MFRM)) {
725 /* Construct a From: header. */
726 fprintf(out, "From: %s\n", signature);
727 } else if (!draft_from_masquerading) {
729 ** mts.conf didn't contain "masquerade:[...]draft_from[...]"
730 ** so we'll reveal the user's actual account@thismachine
731 ** address in a Sender: header (and use it as the envelope
734 fprintf(out, "Sender: %s\n", from);
737 fprintf(out, "Subject: %s", subject);
738 fprintf(out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
742 if ((fd = open(tmpfil, O_RDONLY)) == NOTOK)
743 adios(NULL, "unable to re-open");
744 cpydgst(fd, fileno(out), tmpfil, bccfil);
747 vec[0] = mhbasename(mhlproc);
749 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
753 adios("fork", "unable to");
756 dup2(fileno(out), 1);
759 vec[i++] = "-forward";
765 execvp(mhlproc, vec);
766 adios(mhlproc, "unable to exec");
769 if (status = pidwait(child_id, OK))
770 admonish(NULL, "%s lost (status=0%o)",
776 fseek(out, 0L, SEEK_END);
777 fprintf(out, "\n------- End of Blind-Carbon-Copy\n");
783 /* FCC INTERACTION */
786 fcc(char *file, char *folder)
793 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
797 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
802 printf("Sorry man, but we had no more forks.\n");
804 fprintf(stderr, "Skipped %sFcc %s: unable to fork.\n",
805 msgstate == resent ? "Resent-" : "",
811 snprintf(fold, sizeof(fold), "%s%s",
812 *folder == '+' || *folder == '@' ? "" : "+",
814 execlp(fileproc, mhbasename(fileproc),
815 "-link", "-file", file, fold, NULL);
819 if ((status = pidwait(child_id, OK))) {
821 printf(" errored (0%o)\n", status);
823 fprintf(stderr, " %sFcc %s: errored (0%o)\n",
825 "Resent-" : "", folder,