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 },
38 { "alias aliasfile", 0 },
48 { "dist", -4 }, /* interface from dist */
49 #define PUSHSW 12 /* fork to sendmail then exit */
51 #define NPUSHSW 13 /* exec sendmail */
54 { "library directory", -7 },
59 /* flags for headers->flags */
60 #define HNOP 0x0000 /* just used to keep .set around */
61 #define HBAD 0x0001 /* bad header - don't let it through */
62 #define HADR 0x0002 /* header has an address field */
63 #define HSUB 0x0004 /* Subject: header */
64 #define HTRY 0x0008 /* try to send to addrs on header */
65 #define HBCC 0x0010 /* don't output this header */
66 #define HMNG 0x0020 /* mung this header */
67 #define HNGR 0x0040 /* no groups allowed in this header */
68 #define HFCC 0x0080 /* FCC: type header */
69 #define HNIL 0x0100 /* okay for this header not to have addrs */
70 #define HIGN 0x0200 /* ignore this header */
72 /* flags for headers->set */
73 #define MFRM 0x0001 /* we've seen a From: */
74 #define MDAT 0x0002 /* we've seen a Date: */
75 #define MRFM 0x0004 /* we've seen a Resent-From: */
76 #define MVIS 0x0008 /* we've seen sighted addrs */
77 #define MINV 0x0010 /* we've seen blind addrs */
78 #define MRDT 0x0020 /* we've seen a Resent-Date: */
87 static struct headers NHeaders[] = {
88 { "Return-Path", HBAD, 0 },
89 { "Received", HBAD, 0 },
90 { "Reply-To", HADR|HNGR, 0 },
91 { "From", HADR|HNGR, MFRM },
92 { "Sender", HADR|HBAD, 0 },
93 { "Date", HNOP, MDAT },
94 { "Subject", HSUB, 0 },
95 { "To", HADR|HTRY, MVIS },
96 { "Cc", HADR|HTRY, MVIS },
97 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
98 { "Message-Id", HBAD, 0 },
103 static struct headers RHeaders[] = {
104 { "Resent-Reply-To", HADR|HNGR, 0 },
105 { "Resent-From", HADR|HNGR, MRFM },
106 { "Resent-Sender", HADR|HBAD, 0 },
107 { "Resent-Date", HNOP, MRDT },
108 { "Resent-Subject", HSUB, 0 },
109 { "Resent-To", HADR|HTRY, MVIS },
110 { "Resent-Cc", HADR|HTRY, MVIS },
111 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
112 { "Resent-Message-Id", HBAD, 0 },
113 { "Resent-Fcc", HFCC, 0 },
114 { "Reply-To", HADR, 0 },
120 static int badmsg = 0; /* message has bad semantics */
121 static int verbose = 0; /* spell it out */
122 static int debug = 0; /* debugging post */
123 static int watch = 0; /* watch the delivery process */
124 static int pushflg = 0; /* if going to fork to sendmail */
125 static int aliasflg = 0; /* if going to process aliases */
127 static unsigned msgflags = 0; /* what we've seen */
133 static char tmpfil[] = "/tmp/pstXXXXXX";
135 static char *filter = NULL; /* the filter for BCC'ing */
136 static char *subject = NULL; /* the subject field for BCC'ing */
137 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
138 static short fccind = 0; /* index into fccfold[] */
140 static struct headers *hdrtab; /* table for the message we're doing */
141 static FILE *out; /* output (temp) file */
143 extern char *sendmail;
146 ** external prototypes
148 extern char *getfullname(void);
149 extern char *getusername(void);
154 static void putfmt(char *, char *, FILE *);
155 static void finish_headers(FILE *);
156 static int get_header(char *, struct headers *);
157 static void putadr(char *, struct mailname *);
158 static int putone(char *, int, int);
159 static void insert_fcc(char *, unsigned char *);
160 static void fcc(char *, char *);
163 static void make_bcc_file(void);
168 main(int argc, char **argv)
170 int state, i, pid, compnum;
171 char *cp, *msg = NULL, **argp, **arguments;
172 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
176 setlocale(LC_ALL, "");
178 invo_name = mhbasename(argv[0]);
180 /* foil search of user profile/context */
181 if (context_foil(NULL) == -1)
184 arguments = getarguments(invo_name, argc, argv, 0);
187 while ((cp = *argp++)) {
189 switch (smatch(++cp, switches)) {
191 ambigsw(cp, switches);
194 adios(NULL, "-%s unknown", cp);
197 snprintf(buf, sizeof(buf),
198 "%s [switches] file",
200 print_help(buf, switches, 1);
203 print_version(invo_name);
215 if (!(filter = *argp++) || *filter == '-')
216 adios(NULL, "missing argument to %s",
245 if (!(cp = *argp++) || *cp == '-')
246 adios(NULL, "missing argument to %s",
249 if ((state = alias(cp)) != AK_OK)
250 adios(NULL, "aliasing error in file %s - %s", cp, akerror(state));
257 if (!(cp = *argp++) || *cp == '-')
258 adios(NULL, "missing argument to %s",
260 /* create a minimal context */
261 if (context_foil(cp) == -1)
267 adios(NULL, "only one message at a time!");
273 adios(NULL, "usage: %s [switches] file", invo_name);
275 if ((in = fopen(msg, "r")) == NULL)
276 adios(msg, "unable to open");
283 if ((out = fdopen(mkstemp(tmpfil), "w")) == NULL)
284 adios(tmpfil, "unable to create");
287 if ((out = fopen(tmpfil, "w")) == NULL)
288 adios(tmpfil, "unable to create");
293 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
295 for (compnum = 1, state = FLD;;) {
296 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
299 putfmt(name, buf, out);
305 while (state == FLDPLUS) {
306 state = m_getfld(state, name, buf,
310 putfmt(name, cp, out);
316 fprintf(out, "\n%s", buf);
317 while (state == BODY) {
318 state = m_getfld(state, name, buf,
330 adios(NULL, "message format error in component #%d",
334 adios(NULL, "getfld() returned %d", state);
348 for (i=0; i<fccind; i++) {
349 fcc(tmpfil, fccfold[i]);
353 ** re-open the temp file, unlink it and exec sendmail, giving it
354 ** the msg temp file as std in.
356 if (!freopen(tmpfil, "r", stdin)) {
357 adios(tmpfil, "can't reopen for sendmail");
362 *argp++ = "send-mail";
363 *argp++ = "-m"; /* send to me too */
364 *argp++ = "-t"; /* read msg for recipients */
365 *argp++ = "-i"; /* don't stop on "." */
366 if (watch || verbose)
370 if (pushflg && !(watch || verbose)) {
371 /* fork to a child to run sendmail */
372 for (i=0; (pid = fork()) == NOTOK && i < 5; i++)
376 fprintf(verbose ? stdout : stderr,
377 "%s: can't fork to %s\n",
378 invo_name, sendmail);
381 /* we're the child .. */
387 execv(sendmail, sargv);
388 adios(sendmail, "can't exec");
389 return 0; /* dead code to satisfy the compiler */
392 /* DRAFT GENERATION */
395 putfmt(char *name, char *str, FILE *out)
401 while (*str == ' ' || *str == '\t')
404 if ((i = get_header(name, hdrtab)) == NOTOK) {
405 /* some boring header: push it through */
406 fprintf(out, "%s: %s", name, str);
410 /* one of the interesting headers */
412 if (hdr->flags & HIGN) {
415 if (hdr->flags & HBAD) {
416 advise(NULL, "illegal header line -- %s:", name);
420 msgflags |= hdr->set;
422 if (hdr->flags & HSUB) {
424 /* append mupliple subject */
425 char *cp = concat(subject, "\t", str, NULL);
429 subject = getcpy(str);
433 if (hdr->flags & HFCC) {
434 if ((cp = strrchr(str, '\n'))) {
437 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
439 insert_fcc(hdr->value, pp);
441 insert_fcc(hdr->value, pp);
446 if (hdr->flags & HBCC) {
452 if (*str != '\n' && *str != '\0') {
453 if (aliasflg && hdr->flags & HTRY) {
455 ** this header contains address(es) that we have to do
456 ** alias expansion on. Because of the saved state in
457 ** getname we have to put all the addresses into a
458 ** list. We then let putadr munch on that list,
459 ** possibly expanding aliases.
461 register struct mailname *f = 0;
462 register struct mailname *mp = 0;
464 while ((cp = getname(str))) {
465 mp = getm(cp, NULL, 0, AD_HOST, NULL);
470 mp->m_next = f->m_next;
475 f = mp->m_next; mp->m_next = 0;
479 ** The author(s) of spost decided that alias
480 ** substitution wasn't necessary for the non-HTRY
481 ** headers. Unfortunately, one of those headers
482 ** is "From:", and having alias substitution
483 ** work on that is extremely useful for someone
484 ** with a lot of POP3 email accounts or aliases.
485 ** post supports aliasing of "From:"...
487 ** Since "From:"-processing is incompletely
488 ** implemented in this unsupported and
489 ** undocumented spost backend, I'm not
490 ** going to take the time to implement my new
491 ** draft-From:-based email address masquerading.
492 ** If I do ever implement it here, I'd almost
493 ** certainly want to implement "From:" line
494 ** alias processing as well.
495 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
497 fprintf(out, "%s: %s", name, str);
504 ** Add yet missing headers.
507 finish_headers(FILE *out)
510 char from[BUFSIZ]; /* my network address */
511 char signature[BUFSIZ]; /* my signature */
512 char *resentstr = (msgstate == resent) ? "Resent-" : "";
514 if (!(msgflags & MDAT)) {
515 fprintf(out, "%sDate: %s\n", resentstr, dtimenow(0));
518 strncpy(from, getusername(), sizeof(from));
519 if ((cp = getfullname()) && *cp) {
520 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
522 snprintf(signature, sizeof(signature), "%s", from);
524 if (!(msgflags & MFRM)) {
525 fprintf(out, "%sFrom: %s\n", resentstr, signature);
527 /* In case the From: header contains multiple addresses. */
528 fprintf(out, "%sSender: %s\n", resentstr, from);
531 if (!(msgflags & MVIS))
532 fprintf(out, "%sBcc: Blind Distribution List: ;\n", resentstr);
536 adios(NULL, "re-format message and try again");
542 ** Return index of the requested header in the table, or NOTOK if missing.
545 get_header(char *header, struct headers *table)
549 for (h=table; h->value; h++) {
550 if (mh_strcasecmp(header, h->value)==0) {
560 ** output the address list for header "name". The address list
561 ** is a linked list of mailname structs. "nl" points to the head
562 ** of the list. Alias substitution should be done on nl.
565 putadr(char *name, struct mailname *nl)
567 register struct mailname *mp, *mp2;
568 register int linepos;
572 fprintf(out, "%s: ", name);
573 namelen = strlen(name) + 2;
576 for (mp = nl; mp; ) {
577 if (linepos > MAX_SM_FIELD) {
578 fprintf(out, "\n%s: ", name);
582 /* a local name - see if it's an alias */
583 cp = akvalue(mp->m_mbox);
584 if (cp == mp->m_mbox) {
585 /* wasn't an alias - use what the user typed */
586 linepos = putone(mp->m_text, linepos, namelen);
588 /* an alias - expand it */
589 while ((cp = getname(cp))) {
590 if (linepos > MAX_SM_FIELD) {
591 fprintf(out, "\n%s: ", name);
594 mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
596 mp2->m_pers = getcpy(mp->m_mbox);
597 linepos = putone(adrformat(mp2), linepos, namelen);
599 linepos = putone(mp2->m_text,
607 /* not a local name - use what the user typed */
608 linepos = putone(mp->m_text, linepos, namelen);
618 putone(char *adr, int pos, int indent)
626 else if (linepos+len > OUTPUTLINELEN) {
627 fprintf(out, ",\n%*s", indent, "");
643 ** Insert the normalized value from pp into fccfold[].
646 insert_fcc(char *name, unsigned char *pp)
650 for (cp = pp; isspace(*cp); cp++)
652 for (pp += strlen(pp) - 1; pp > cp && isspace(*pp); pp--)
660 adios(NULL, "too many %ss", name);
661 fccfold[fccind++] = getcpy(cp);
676 fd = mkstemp(bccfil);
677 if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
678 adios(bccfil, "unable to create");
681 if ((out = fopen(bccfil, "w")) == NULL)
682 adios(bccfil, "unable to create");
686 fprintf(out, "Date: %s\n", dtimenow(0));
687 if (!(msgflags & MFRM)) {
688 /* Construct a From: header. */
689 fprintf(out, "From: %s\n", signature);
691 /* Add a Sender: header in case the From: header is fake. */
692 fprintf(out, "Sender: %s\n", from);
695 fprintf(out, "Subject: %s", subject);
696 fprintf(out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
700 if ((fd = open(tmpfil, O_RDONLY)) == NOTOK)
701 adios(NULL, "unable to re-open");
702 cpydgst(fd, fileno(out), tmpfil, bccfil);
705 vec[0] = mhbasename(mhlproc);
707 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
711 adios("fork", "unable to");
714 dup2(fileno(out), 1);
717 vec[i++] = "-forward";
723 execvp(mhlproc, vec);
724 adios(mhlproc, "unable to exec");
727 if (status = pidwait(child_id, OK))
728 admonish(NULL, "%s lost (status=0%o)",
734 fseek(out, 0L, SEEK_END);
735 fprintf(out, "\n------- End of Blind-Carbon-Copy\n");
741 /* FCC INTERACTION */
744 fcc(char *file, char *folder)
751 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
755 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
760 printf("Sorry man, but we had no more forks.\n");
762 fprintf(stderr, "Skipped %sFcc %s: unable to fork.\n",
763 msgstate == resent ? "Resent-" : "",
769 snprintf(fold, sizeof(fold), "%s%s",
770 *folder == '+' || *folder == '@' ? "" : "+",
772 execlp(fileproc, mhbasename(fileproc),
773 "-link", "-file", file, fold, NULL);
777 if ((status = pidwait(child_id, OK))) {
779 printf(" errored (0%o)\n", status);
781 fprintf(stderr, " %sFcc %s: errored (0%o)\n",
783 "Resent-" : "", folder,