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);
161 static void putfmt(char *, char *, FILE *);
162 static void start_headers(void);
163 static void finish_headers(FILE *);
164 static int get_header(char *, struct headers *);
165 static void putadr(char *, struct mailname *);
166 static int putone(char *, int, int);
167 static void insert_fcc(char *, unsigned char *);
168 static void fcc(char *, char *);
171 static void make_bcc_file(void);
176 main(int argc, char **argv)
178 int state, i, pid, compnum;
179 char *cp, *msg = NULL, **argp, **arguments;
180 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
184 setlocale(LC_ALL, "");
186 invo_name = mhbasename(argv[0]);
188 /* foil search of user profile/context */
189 if (context_foil(NULL) == -1)
193 arguments = getarguments(invo_name, argc, argv, 0);
196 while ((cp = *argp++)) {
198 switch (smatch(++cp, switches)) {
200 ambigsw(cp, switches);
203 adios(NULL, "-%s unknown", cp);
206 snprintf(buf, sizeof(buf),
207 "%s [switches] file",
209 print_help(buf, switches, 1);
212 print_version(invo_name);
224 if (!(filter = *argp++) || *filter == '-')
225 adios(NULL, "missing argument to %s",
261 if (!(cp = *argp++) || *cp == '-')
262 adios(NULL, "missing argument to %s",
265 if ((state = alias(cp)) != AK_OK)
266 adios(NULL, "aliasing error in file %s - %s", cp, akerror(state));
273 if (!(cp = *argp++) || *cp == '-')
274 adios(NULL, "missing argument to %s",
276 /* create a minimal context */
277 if (context_foil(cp) == -1)
283 adios(NULL, "only one message at a time!");
289 adios(NULL, "usage: %s [switches] file", invo_name);
291 if ((in = fopen(msg, "r")) == NULL)
292 adios(msg, "unable to open");
300 if ((out = fdopen(mkstemp(tmpfil), "w")) == NULL)
301 adios(tmpfil, "unable to create");
304 if ((out = fopen(tmpfil, "w")) == NULL)
305 adios(tmpfil, "unable to create");
310 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
312 for (compnum = 1, state = FLD;;) {
313 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
316 putfmt(name, buf, out);
322 while (state == FLDPLUS) {
323 state = m_getfld(state, name, buf,
327 putfmt(name, cp, out);
333 fprintf(out, "\n%s", buf);
334 while (state == BODY) {
335 state = m_getfld(state, name, buf,
347 adios(NULL, "message format error in component #%d",
351 adios(NULL, "getfld() returned %d", state);
358 strncpy(buf, m_backup(msg), sizeof(buf));
359 if (rename(msg, buf) == NOTOK)
360 advise(buf, "unable to rename %s to", msg);
371 for (i=0; i<fccind; i++) {
372 fcc(tmpfil, fccfold[i]);
376 ** re-open the temp file, unlink it and exec sendmail, giving it
377 ** the msg temp file as std in.
379 if (!freopen(tmpfil, "r", stdin)) {
380 adios(tmpfil, "can't reopen for sendmail");
385 *argp++ = "send-mail";
386 *argp++ = "-m"; /* send to me too */
387 *argp++ = "-t"; /* read msg for recipients */
388 *argp++ = "-i"; /* don't stop on "." */
389 if (watch || verbose)
393 if (pushflg && !(watch || verbose)) {
394 /* fork to a child to run sendmail */
395 for (i=0; (pid = fork()) == NOTOK && i < 5; i++)
399 fprintf(verbose ? stdout : stderr,
400 "%s: can't fork to %s\n",
401 invo_name, sendmail);
404 /* we're the child .. */
410 execv(sendmail, sargv);
411 adios(sendmail, "can't exec");
412 return 0; /* dead code to satisfy the compiler */
415 /* DRAFT GENERATION */
418 putfmt(char *name, char *str, FILE *out)
424 while (*str == ' ' || *str == '\t')
427 if ((i = get_header(name, hdrtab)) == NOTOK) {
428 /* some boring header: push it through */
429 fprintf(out, "%s: %s", name, str);
433 /* one of the interesting headers */
435 if (hdr->flags & HIGN) {
438 if (hdr->flags & HBAD) {
439 advise(NULL, "illegal header line -- %s:", name);
443 msgflags |= hdr->set;
445 if (hdr->flags & HSUB) {
447 /* append mupliple subject */
448 char *cp = concat(subject, "\t", str, NULL);
452 subject = getcpy(str);
456 if (hdr->flags & HFCC) {
457 if ((cp = strrchr(str, '\n'))) {
460 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
462 insert_fcc(hdr->value, pp);
464 insert_fcc(hdr->value, pp);
469 if (hdr->flags & HBCC) {
475 if (*str != '\n' && *str != '\0') {
476 if (aliasflg && hdr->flags & HTRY) {
478 ** this header contains address(es) that we have to do
479 ** alias expansion on. Because of the saved state in
480 ** getname we have to put all the addresses into a
481 ** list. We then let putadr munch on that list,
482 ** possibly expanding aliases.
484 register struct mailname *f = 0;
485 register struct mailname *mp = 0;
487 while ((cp = getname(str))) {
488 mp = getm(cp, NULL, 0, AD_HOST, NULL);
493 mp->m_next = f->m_next;
498 f = mp->m_next; mp->m_next = 0;
502 ** The author(s) of spost decided that alias
503 ** substitution wasn't necessary for the non-HTRY
504 ** headers. Unfortunately, one of those headers
505 ** is "From:", and having alias substitution
506 ** work on that is extremely useful for someone
507 ** with a lot of POP3 email accounts or aliases.
508 ** post supports aliasing of "From:"...
510 ** Since "From:"-processing is incompletely
511 ** implemented in this unsupported and
512 ** undocumented spost backend, I'm not
513 ** going to take the time to implement my new
514 ** draft-From:-based email address masquerading.
515 ** If I do ever implement it here, I'd almost
516 ** certainly want to implement "From:" line
517 ** alias processing as well.
518 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
520 fprintf(out, "%s: %s", name, str);
527 ** Construct signature name
534 strncpy(from, getusername(), sizeof(from));
535 if ((cp = getfullname()) && *cp) {
536 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
538 snprintf(signature, sizeof(signature), "%s", from);
544 ** Add yet missing headers.
547 finish_headers(FILE *out)
549 char *resentstr = (msgstate == resent) ? "Resent-" : "";
551 if (!(msgflags & MDAT)) {
552 fprintf(out, "%sDate: %s\n", resentstr, dtimenow(0));
554 if (!(msgflags & MFRM)) {
555 fprintf(out, "%sFrom: %s\n", resentstr, signature);
557 /* Add a Sender: header in case the From: header is fake. */
558 fprintf(out, "%sSender: %s\n", resentstr, from);
561 if (!(msgflags & MVIS))
562 fprintf(out, "%sBcc: Blind Distribution List: ;\n", resentstr);
566 adios(NULL, "re-format message and try again");
572 ** Return index of the requested header in the table, or NOTOK if missing.
575 get_header(char *header, struct headers *table)
579 for (h=table; h->value; h++) {
580 if (mh_strcasecmp(header, h->value)==0) {
590 ** output the address list for header "name". The address list
591 ** is a linked list of mailname structs. "nl" points to the head
592 ** of the list. Alias substitution should be done on nl.
595 putadr(char *name, struct mailname *nl)
597 register struct mailname *mp, *mp2;
598 register int linepos;
602 fprintf(out, "%s: ", name);
603 namelen = strlen(name) + 2;
606 for (mp = nl; mp; ) {
607 if (linepos > MAX_SM_FIELD) {
608 fprintf(out, "\n%s: ", name);
612 /* a local name - see if it's an alias */
613 cp = akvalue(mp->m_mbox);
614 if (cp == mp->m_mbox) {
615 /* wasn't an alias - use what the user typed */
616 linepos = putone(mp->m_text, linepos, namelen);
618 /* an alias - expand it */
619 while ((cp = getname(cp))) {
620 if (linepos > MAX_SM_FIELD) {
621 fprintf(out, "\n%s: ", name);
624 mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
626 mp2->m_pers = getcpy(mp->m_mbox);
627 linepos = putone(adrformat(mp2), linepos, namelen);
629 linepos = putone(mp2->m_text,
637 /* not a local name - use what the user typed */
638 linepos = putone(mp->m_text, linepos, namelen);
648 putone(char *adr, int pos, int indent)
656 else if (linepos+len > OUTPUTLINELEN) {
657 fprintf(out, ",\n%*s", indent, "");
673 ** Insert the normalized value from pp into fccfold[].
676 insert_fcc(char *name, unsigned char *pp)
680 for (cp = pp; isspace(*cp); cp++)
682 for (pp += strlen(pp) - 1; pp > cp && isspace(*pp); pp--)
690 adios(NULL, "too many %ss", name);
691 fccfold[fccind++] = getcpy(cp);
706 fd = mkstemp(bccfil);
707 if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
708 adios(bccfil, "unable to create");
711 if ((out = fopen(bccfil, "w")) == NULL)
712 adios(bccfil, "unable to create");
716 fprintf(out, "Date: %s\n", dtimenow(0));
717 if (!(msgflags & MFRM)) {
718 /* Construct a From: header. */
719 fprintf(out, "From: %s\n", signature);
721 /* Add a Sender: header in case the From: header is fake. */
722 fprintf(out, "Sender: %s\n", from);
725 fprintf(out, "Subject: %s", subject);
726 fprintf(out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
730 if ((fd = open(tmpfil, O_RDONLY)) == NOTOK)
731 adios(NULL, "unable to re-open");
732 cpydgst(fd, fileno(out), tmpfil, bccfil);
735 vec[0] = mhbasename(mhlproc);
737 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
741 adios("fork", "unable to");
744 dup2(fileno(out), 1);
747 vec[i++] = "-forward";
753 execvp(mhlproc, vec);
754 adios(mhlproc, "unable to exec");
757 if (status = pidwait(child_id, OK))
758 admonish(NULL, "%s lost (status=0%o)",
764 fseek(out, 0L, SEEK_END);
765 fprintf(out, "\n------- End of Blind-Carbon-Copy\n");
771 /* FCC INTERACTION */
774 fcc(char *file, char *folder)
781 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
785 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
790 printf("Sorry man, but we had no more forks.\n");
792 fprintf(stderr, "Skipped %sFcc %s: unable to fork.\n",
793 msgstate == resent ? "Resent-" : "",
799 snprintf(fold, sizeof(fold), "%s%s",
800 *folder == '+' || *folder == '@' ? "" : "+",
802 execlp(fileproc, mhbasename(fileproc),
803 "-link", "-file", file, fold, NULL);
807 if ((status = pidwait(child_id, OK))) {
809 printf(" errored (0%o)\n", status);
811 fprintf(stderr, " %sFcc %s: errored (0%o)\n",
813 "Resent-" : "", folder,