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 },
46 { "alias aliasfile", 0 },
56 { "dist", -4 }, /* interface from dist */
57 #define PUSHSW 16 /* fork to sendmail then exit */
59 #define NPUSHSW 17 /* exec sendmail */
62 { "library directory", -7 },
67 /* flags for headers->flags */
68 #define HNOP 0x0000 /* just used to keep .set around */
69 #define HBAD 0x0001 /* bad header - don't let it through */
70 #define HADR 0x0002 /* header has an address field */
71 #define HSUB 0x0004 /* Subject: header */
72 #define HTRY 0x0008 /* try to send to addrs on header */
73 #define HBCC 0x0010 /* don't output this header */
74 #define HMNG 0x0020 /* mung this header */
75 #define HNGR 0x0040 /* no groups allowed in this header */
76 #define HFCC 0x0080 /* FCC: type header */
77 #define HNIL 0x0100 /* okay for this header not to have addrs */
78 #define HIGN 0x0200 /* ignore this header */
80 /* flags for headers->set */
81 #define MFRM 0x0001 /* we've seen a From: */
82 #define MDAT 0x0002 /* we've seen a Date: */
83 #define MRFM 0x0004 /* we've seen a Resent-From: */
84 #define MVIS 0x0008 /* we've seen sighted addrs */
85 #define MINV 0x0010 /* we've seen blind addrs */
86 #define MRDT 0x0020 /* we've seen a Resent-Date: */
95 static struct headers NHeaders[] = {
96 { "Return-Path", HBAD, 0 },
97 { "Received", HBAD, 0 },
98 { "Reply-To", HADR|HNGR, 0 },
99 { "From", HADR|HNGR, MFRM },
100 { "Sender", HADR|HBAD, 0 },
101 { "Date", HNOP, MDAT },
102 { "Subject", HSUB, 0 },
103 { "To", HADR|HTRY, MVIS },
104 { "Cc", HADR|HTRY, MVIS },
105 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
106 { "Message-Id", HBAD, 0 },
111 static struct headers RHeaders[] = {
112 { "Resent-Reply-To", HADR|HNGR, 0 },
113 { "Resent-From", HADR|HNGR, MRFM },
114 { "Resent-Sender", HADR|HBAD, 0 },
115 { "Resent-Date", HNOP, MRDT },
116 { "Resent-Subject", HSUB, 0 },
117 { "Resent-To", HADR|HTRY, MVIS },
118 { "Resent-Cc", HADR|HTRY, MVIS },
119 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
120 { "Resent-Message-Id", HBAD, 0 },
121 { "Resent-Fcc", HFCC, 0 },
122 { "Reply-To", HADR, 0 },
128 static int badmsg = 0; /* message has bad semantics */
129 static int verbose = 0; /* spell it out */
130 static int debug = 0; /* debugging post */
131 static int rmflg = 1; /* remove temporary file when done */
132 static int watch = 0; /* watch the delivery process */
133 static int backflg = 0; /* rename input file as *.bak when done */
134 static int pushflg = 0; /* if going to fork to sendmail */
135 static int aliasflg = 0; /* if going to process aliases */
137 static unsigned msgflags = 0; /* what we've seen */
143 static char tmpfil[] = "/tmp/pstXXXXXX";
145 static char from[BUFSIZ]; /* my network address */
146 static char signature[BUFSIZ]; /* my signature */
147 static char *filter = NULL; /* the filter for BCC'ing */
148 static char *subject = NULL; /* the subject field for BCC'ing */
149 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
150 static short fccind = 0; /* index into fccfold[] */
152 static struct headers *hdrtab; /* table for the message we're doing */
153 static FILE *out; /* output (temp) file */
155 extern char *sendmail;
158 ** external prototypes
160 extern char *getfullname(void);
161 extern char *getusername(void);
163 extern boolean draft_from_masquerading; /* defined in mts.c */
168 static void putfmt(char *, char *, FILE *);
169 static void start_headers(void);
170 static void finish_headers(FILE *);
171 static int get_header(char *, struct headers *);
172 static void putadr(char *, struct mailname *);
173 static int putone(char *, int, int);
174 static void insert_fcc(char *, unsigned char *);
175 static void fcc(char *, char *);
178 static void make_bcc_file(void);
183 main(int argc, char **argv)
185 int state, i, pid, compnum;
186 char *cp, *msg = NULL, **argp, **arguments;
187 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
191 setlocale(LC_ALL, "");
193 invo_name = mhbasename(argv[0]);
195 /* foil search of user profile/context */
196 if (context_foil(NULL) == -1)
200 arguments = getarguments(invo_name, argc, argv, 0);
203 while ((cp = *argp++)) {
205 switch (smatch(++cp, switches)) {
207 ambigsw(cp, switches);
210 adios(NULL, "-%s unknown", cp);
213 snprintf(buf, sizeof(buf),
214 "%s [switches] file",
216 print_help(buf, switches, 1);
219 print_version(invo_name);
231 if (!(filter = *argp++) || *filter == '-')
232 adios(NULL, "missing argument to %s",
275 if (!(cp = *argp++) || *cp == '-')
276 adios(NULL, "missing argument to %s",
279 if ((state = alias(cp)) != AK_OK)
280 adios(NULL, "aliasing error in file %s - %s", cp, akerror(state));
287 if (!(cp = *argp++) || *cp == '-')
288 adios(NULL, "missing argument to %s",
290 /* create a minimal context */
291 if (context_foil(cp) == -1)
297 adios(NULL, "only one message at a time!");
303 adios(NULL, "usage: %s [switches] file", invo_name);
305 if ((in = fopen(msg, "r")) == NULL)
306 adios(msg, "unable to open");
314 if ((out = fdopen(mkstemp(tmpfil), "w")) == NULL)
315 adios(tmpfil, "unable to create");
318 if ((out = fopen(tmpfil, "w")) == NULL)
319 adios(tmpfil, "unable to create");
324 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
326 for (compnum = 1, state = FLD;;) {
327 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
330 putfmt(name, buf, out);
336 while (state == FLDPLUS) {
337 state = m_getfld(state, name, buf,
341 putfmt(name, cp, out);
347 fprintf(out, "\n%s", buf);
348 while (state == BODY) {
349 state = m_getfld(state, name, buf,
361 adios(NULL, "message format error in component #%d",
365 adios(NULL, "getfld() returned %d", state);
372 strncpy(buf, m_backup(msg), sizeof(buf));
373 if (rename(msg, buf) == NOTOK)
374 advise(buf, "unable to rename %s to", msg);
384 for (i=0; i<fccind; i++) {
385 fcc(tmpfil, fccfold[i]);
389 ** re-open the temp file, unlink it and exec sendmail, giving it
390 ** the msg temp file as std in.
392 if (freopen(tmpfil, "r", stdin) == NULL)
393 adios(tmpfil, "can't reopen for sendmail");
398 *argp++ = "send-mail";
399 *argp++ = "-m"; /* send to me too */
400 *argp++ = "-t"; /* read msg for recipients */
401 *argp++ = "-i"; /* don't stop on "." */
402 if (watch || verbose)
406 if (pushflg && !(watch || verbose)) {
407 /* fork to a child to run sendmail */
408 for (i=0; (pid = fork()) == NOTOK && i < 5; i++)
412 fprintf(verbose ? stdout : stderr,
413 "%s: can't fork to %s\n",
414 invo_name, sendmail);
417 /* we're the child .. */
423 execv(sendmail, sargv);
424 adios(sendmail, "can't exec");
425 return 0; /* dead code to satisfy the compiler */
428 /* DRAFT GENERATION */
431 putfmt(char *name, char *str, FILE *out)
437 while (*str == ' ' || *str == '\t')
440 if ((i = get_header(name, hdrtab)) == NOTOK) {
441 /* some boring header: push it through */
442 fprintf(out, "%s: %s", name, str);
446 /* one of the interesting headers */
448 if (hdr->flags & HIGN) {
451 if (hdr->flags & HBAD) {
452 advise(NULL, "illegal header line -- %s:", name);
456 msgflags |= hdr->set;
458 if (hdr->flags & HSUB) {
460 /* append mupliple subject */
461 char *cp = concat(subject, "\t", str, NULL);
465 subject = getcpy(str);
469 if (hdr->flags & HFCC) {
470 if ((cp = strrchr(str, '\n'))) {
473 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
475 insert_fcc(hdr->value, pp);
477 insert_fcc(hdr->value, pp);
482 if (hdr->flags & HBCC) {
488 if (*str != '\n' && *str != '\0') {
489 if (aliasflg && hdr->flags & HTRY) {
491 ** this header contains address(es) that we have to do
492 ** alias expansion on. Because of the saved state in
493 ** getname we have to put all the addresses into a
494 ** list. We then let putadr munch on that list,
495 ** possibly expanding aliases.
497 register struct mailname *f = 0;
498 register struct mailname *mp = 0;
500 while ((cp = getname(str))) {
501 mp = getm(cp, NULL, 0, AD_HOST, NULL);
506 mp->m_next = f->m_next;
511 f = mp->m_next; mp->m_next = 0;
515 ** The author(s) of spost decided that alias
516 ** substitution wasn't necessary for the non-HTRY
517 ** headers. Unfortunately, one of those headers
518 ** is "From:", and having alias substitution
519 ** work on that is extremely useful for someone
520 ** with a lot of POP3 email accounts or aliases.
521 ** post supports aliasing of "From:"...
523 ** Since "From:"-processing is incompletely
524 ** implemented in this unsupported and
525 ** undocumented spost backend, I'm not
526 ** going to take the time to implement my new
527 ** draft-From:-based email address masquerading.
528 ** If I do ever implement it here, I'd almost
529 ** certainly want to implement "From:" line
530 ** alias processing as well.
531 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
533 fprintf(out, "%s: %s", name, str);
540 ** Construct signature name
547 strncpy(from, getusername(), sizeof(from));
548 if ((cp = getfullname()) && *cp) {
549 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
551 snprintf(signature, sizeof(signature), "%s", from);
557 ** Add yet missing headers.
560 finish_headers(FILE *out)
562 char *resentstr = (msgstate == resent) ? "Resent-" : "";
564 if (!(msgflags & MDAT)) {
565 fprintf(out, "%sDate: %s\n", resentstr, dtimenow(0));
567 if (!(msgflags & MFRM)) {
568 fprintf(out, "%sFrom: %s\n", resentstr, signature);
569 } else if (!draft_from_masquerading) {
571 ** mts.conf didn't contain "masquerade:[...]draft_from[...]"
572 ** so we'll reveal the user's actual account@thismachine
573 ** address in a Sender: header (and use it as the envelope
576 fprintf(out, "%sSender: %s\n", resentstr, from);
579 if (!(msgflags & MVIS))
580 fprintf(out, "%sBcc: Blind Distribution List: ;\n", resentstr);
584 adios(NULL, "re-format message and try again");
590 ** Return index of the requested header in the table, or NOTOK if missing.
593 get_header(char *header, struct headers *table)
597 for (h=table; h->value; h++) {
598 if (mh_strcasecmp(header, h->value)==0) {
608 ** output the address list for header "name". The address list
609 ** is a linked list of mailname structs. "nl" points to the head
610 ** of the list. Alias substitution should be done on nl.
613 putadr(char *name, struct mailname *nl)
615 register struct mailname *mp, *mp2;
616 register int linepos;
620 fprintf(out, "%s: ", name);
621 namelen = strlen(name) + 2;
624 for (mp = nl; mp; ) {
625 if (linepos > MAX_SM_FIELD) {
626 fprintf(out, "\n%s: ", name);
630 /* a local name - see if it's an alias */
631 cp = akvalue(mp->m_mbox);
632 if (cp == mp->m_mbox) {
633 /* wasn't an alias - use what the user typed */
634 linepos = putone(mp->m_text, linepos, namelen);
636 /* an alias - expand it */
637 while ((cp = getname(cp))) {
638 if (linepos > MAX_SM_FIELD) {
639 fprintf(out, "\n%s: ", name);
642 mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
644 mp2->m_pers = getcpy(mp->m_mbox);
645 linepos = putone(adrformat(mp2), linepos, namelen);
647 linepos = putone(mp2->m_text,
655 /* not a local name - use what the user typed */
656 linepos = putone(mp->m_text, linepos, namelen);
666 putone(char *adr, int pos, int indent)
674 else if (linepos+len > OUTPUTLINELEN) {
675 fprintf(out, ",\n%*s", indent, "");
691 ** Insert the normalized value from pp into fccfold[].
694 insert_fcc(char *name, unsigned char *pp)
698 for (cp = pp; isspace(*cp); cp++)
700 for (pp += strlen(pp) - 1; pp > cp && isspace(*pp); pp--)
708 adios(NULL, "too many %ss", name);
709 fccfold[fccind++] = getcpy(cp);
724 fd = mkstemp(bccfil);
725 if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
726 adios(bccfil, "unable to create");
729 if ((out = fopen(bccfil, "w")) == NULL)
730 adios(bccfil, "unable to create");
734 fprintf(out, "Date: %s\n", dtimenow(0));
735 if (!(msgflags & MFRM)) {
736 /* Construct a From: header. */
737 fprintf(out, "From: %s\n", signature);
738 } else if (!draft_from_masquerading) {
740 ** mts.conf didn't contain "masquerade:[...]draft_from[...]"
741 ** so we'll reveal the user's actual account@thismachine
742 ** address in a Sender: header (and use it as the envelope
745 fprintf(out, "Sender: %s\n", from);
748 fprintf(out, "Subject: %s", subject);
749 fprintf(out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
753 if ((fd = open(tmpfil, O_RDONLY)) == NOTOK)
754 adios(NULL, "unable to re-open");
755 cpydgst(fd, fileno(out), tmpfil, bccfil);
758 vec[0] = mhbasename(mhlproc);
760 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
764 adios("fork", "unable to");
767 dup2(fileno(out), 1);
770 vec[i++] = "-forward";
776 execvp(mhlproc, vec);
777 adios(mhlproc, "unable to exec");
780 if (status = pidwait(child_id, OK))
781 admonish(NULL, "%s lost (status=0%o)",
787 fseek(out, 0L, SEEK_END);
788 fprintf(out, "\n------- End of Blind-Carbon-Copy\n");
794 /* FCC INTERACTION */
797 fcc(char *file, char *folder)
804 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
808 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
813 printf("Sorry man, but we had no more forks.\n");
815 fprintf(stderr, "Skipped %sFcc %s: unable to fork.\n",
816 msgstate == resent ? "Resent-" : "",
822 snprintf(fold, sizeof(fold), "%s%s",
823 *folder == '+' || *folder == '@' ? "" : "+",
825 execlp(fileproc, mhbasename(fileproc),
826 "-link", "-file", file, fold, NULL);
830 if ((status = pidwait(child_id, OK))) {
832 printf(" errored (0%o)\n", status);
834 fprintf(stderr, " %sFcc %s: errored (0%o)\n",
836 "Resent-" : "", folder,