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>
20 #define MAX_SM_FIELD 1476 /* < largest hdr field sendmail will accept */
21 #define FCCS 10 /* max number of fccs allowed */
23 struct swit switches[] = {
33 { "alias aliasfile", 0 },
43 { "dist", -4 }, /* interface from dist */
44 #define PUSHSW 10 /* fork to sendmail then exit */
46 #define NPUSHSW 11 /* exec sendmail */
49 { "library directory", -7 },
54 /* flags for headers->flags */
55 #define HNOP 0x0000 /* just used to keep .set around */
56 #define HBAD 0x0001 /* bad header - don't let it through */
57 #define HADR 0x0002 /* header has an address field */
58 #define HSUB 0x0004 /* Subject: header */
59 #define HTRY 0x0008 /* try to send to addrs on header */
60 #define HBCC 0x0010 /* don't output this header */
61 #define HNGR 0x0020 /* no groups allowed in this header */
62 #define HFCC 0x0040 /* FCC: type header */
63 #define HNIL 0x0080 /* okay for this header not to have addrs */
64 #define HIGN 0x0100 /* ignore this header */
66 /* flags for headers->set */
67 #define MFRM 0x0001 /* we've seen a From: */
68 #define MDAT 0x0002 /* we've seen a Date: */
69 #define MRFM 0x0004 /* we've seen a Resent-From: */
70 #define MVIS 0x0008 /* we've seen sighted addrs */
71 #define MINV 0x0010 /* we've seen blind addrs */
72 #define MRDT 0x0020 /* we've seen a Resent-Date: */
81 static struct headers NHeaders[] = {
82 { "Return-Path", HBAD, 0 },
83 { "Received", HBAD, 0 },
84 { "Reply-To", HADR|HNGR, 0 },
85 { "From", HADR|HNGR, MFRM },
86 { "Sender", HADR|HBAD, 0 },
87 { "Date", HNOP, MDAT },
88 { "Subject", HSUB, 0 },
89 { "To", HADR|HTRY, MVIS },
90 { "Cc", HADR|HTRY, MVIS },
91 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
92 { "Message-Id", HBAD, 0 },
97 static struct headers RHeaders[] = {
98 { "Resent-Reply-To", HADR|HNGR, 0 },
99 { "Resent-From", HADR|HNGR, MRFM },
100 { "Resent-Sender", HADR|HBAD, 0 },
101 { "Resent-Date", HNOP, MRDT },
102 { "Resent-Subject", HSUB, 0 },
103 { "Resent-To", HADR|HTRY, MVIS },
104 { "Resent-Cc", HADR|HTRY, MVIS },
105 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
106 { "Resent-Message-Id", HBAD, 0 },
107 { "Resent-Fcc", HFCC, 0 },
108 { "Reply-To", HADR, 0 },
114 static int badmsg = 0; /* message has bad semantics */
115 static int verbose = 0; /* spell it out */
116 static int debug = 0; /* debugging post */
117 static int watch = 0; /* watch the delivery process */
118 static int pushflg = 0; /* if going to fork to sendmail */
119 static int aliasflg = 0; /* if going to process aliases */
121 static unsigned msgflags = 0; /* what we've seen */
127 static char tmpfil[] = "/tmp/spost-XXXXXX";
128 static char bccfil[] = "/tmp/spost-bcc-XXXXXX";
130 static char *subject = NULL; /* the subject field for BCC'ing */
131 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
132 static short fccind = 0; /* index into fccfold[] */
133 struct mailname *bccs = NULL;
135 static struct headers *hdrtab; /* table for the message we're doing */
136 static FILE *out; /* output (temp) file */
138 extern char *sendmail;
143 static void putfmt(char *, char *, FILE *);
144 static void finish_headers(FILE *);
145 static int get_header(char *, struct headers *);
146 static void putadr(char *, struct mailname *);
147 static int putone(char *, int, int);
148 static void insert_fcc(char *, unsigned char *);
149 static void fcc(char *, char *);
150 static void make_bcc_draft(char *);
154 main(int argc, char **argv)
156 int state, i, pid, compnum;
157 char *cp, *msg = NULL, **argp, **arguments;
158 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
162 setlocale(LC_ALL, "");
164 invo_name = mhbasename(argv[0]);
166 /* foil search of user profile/context */
167 if (context_foil(NULL) == -1)
170 arguments = getarguments(invo_name, argc, argv, 0);
173 while ((cp = *argp++)) {
175 switch (smatch(++cp, switches)) {
177 ambigsw(cp, switches);
180 adios(NULL, "-%s unknown", cp);
183 snprintf(buf, sizeof(buf),
184 "%s [switches] file",
186 print_help(buf, switches, 1);
189 print_version(invo_name);
222 if (!(cp = *argp++) || *cp == '-')
223 adios(NULL, "missing argument to %s",
226 if ((state = alias(cp)) != AK_OK)
227 adios(NULL, "aliasing error in file %s - %s", cp, akerror(state));
234 if (!(cp = *argp++) || *cp == '-')
235 adios(NULL, "missing argument to %s",
237 /* create a minimal context */
238 if (context_foil(cp) == -1)
244 adios(NULL, "only one message at a time!");
250 adios(NULL, "usage: %s [switches] file", invo_name);
252 if ((in = fopen(msg, "r")) == NULL)
253 adios(msg, "unable to open");
260 if ((out = fdopen(mkstemp(tmpfil), "w")) == NULL)
261 adios(tmpfil, "unable to create");
264 if ((out = fopen(tmpfil, "w")) == NULL)
265 adios(tmpfil, "unable to create");
270 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
272 for (compnum = 1, state = FLD;;) {
273 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
276 putfmt(name, buf, out);
282 while (state == FLDPLUS) {
283 state = m_getfld(state, name, buf,
287 putfmt(name, cp, out);
293 fprintf(out, "\n%s", buf);
294 while (state == BODY) {
295 state = m_getfld(state, name, buf,
307 adios(NULL, "message format error in component #%d",
311 adios(NULL, "getfld() returned %d", state);
325 for (i=0; i<fccind; i++) {
326 fcc(tmpfil, fccfold[i]);
331 make_bcc_draft(tmpfil);
335 ** re-open the temp file, unlink it and exec sendmail, giving it
336 ** the msg temp file as std in.
338 if (!freopen(tmpfil, "r", stdin)) {
339 adios(tmpfil, "can't reopen for sendmail");
344 *argp++ = "send-mail";
345 *argp++ = "-m"; /* send to me too */
346 *argp++ = "-t"; /* read msg for recipients */
347 *argp++ = "-i"; /* don't stop on "." */
348 if (watch || verbose) {
355 /* fork to a child to run sendmail */
356 for (i=0; (pid = fork()) == NOTOK && i < 5; i++)
360 fprintf(verbose ? stdout : stderr,
361 "%s: can't fork to %s\n",
362 invo_name, sendmail);
365 /* we're the child .. */
373 /* fork to a child to run sendmail */
374 for (i=0; (pid = fork()) == NOTOK && i < 5; i++)
378 fprintf(verbose ? stdout : stderr,
379 "%s: can't fork to %s\n",
380 invo_name, sendmail);
383 /* we're the child .. */
390 execv(sendmail, sargv);
391 adios(sendmail, "can't exec");
392 return 0; /* dead code to satisfy the compiler */
395 /* DRAFT GENERATION */
398 putfmt(char *name, char *str, FILE *out)
404 while (*str == ' ' || *str == '\t')
407 if ((i = get_header(name, hdrtab)) == NOTOK) {
408 /* some boring header: push it through */
409 fprintf(out, "%s: %s", name, str);
412 /* one of the interesting headers */
415 if (hdr->flags & HIGN) {
418 if (hdr->flags & HBAD) {
419 advise(NULL, "illegal header line -- %s:", name);
423 msgflags |= hdr->set;
425 if (hdr->flags & HSUB) {
427 /* concatenate mupliple subject */
428 char *cp = concat(subject, "\t", str, NULL);
432 subject = getcpy(str);
436 if (hdr->flags & HFCC) {
437 if ((cp = strrchr(str, '\n'))) {
440 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
442 insert_fcc(hdr->value, pp);
444 insert_fcc(hdr->value, pp);
448 if (*str != '\n' && *str != '\0') {
449 if (hdr->flags & HBCC) {
450 struct mailname *mp = NULL;
452 /* Create list of Bcc adds. */
453 while ((cp = getname(str))) {
454 mp = getm(cp, NULL, 0, AD_HOST, NULL);
455 mp->m_next = bccs; /* push */
458 } else if (aliasflg && hdr->flags & HTRY) {
460 ** this header contains address(es) that we have to do
461 ** alias expansion on. Because of the saved state in
462 ** getname we have to put all the addresses into a
463 ** list. We then let putadr munch on that list,
464 ** possibly expanding aliases.
466 register struct mailname *f = 0;
467 register struct mailname *mp = 0;
469 while ((cp = getname(str))) {
470 mp = getm(cp, NULL, 0, AD_HOST, NULL);
475 mp->m_next = f->m_next;
480 f = mp->m_next; mp->m_next = 0;
484 ** The author(s) of spost decided that alias
485 ** substitution wasn't necessary for the non-HTRY
486 ** headers. Unfortunately, one of those headers
487 ** is "From:", and having alias substitution
488 ** work on that is extremely useful for someone
489 ** with a lot of POP3 email accounts or aliases.
490 ** post supports aliasing of "From:"...
492 ** Since "From:"-processing is incompletely
493 ** implemented in this unsupported and
494 ** undocumented spost backend, I'm not
495 ** going to take the time to implement my new
496 ** draft-From:-based email address masquerading.
497 ** If I do ever implement it here, I'd almost
498 ** certainly want to implement "From:" line
499 ** alias processing as well.
500 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
502 fprintf(out, "%s: %s", name, str);
509 ** Add yet missing headers.
512 finish_headers(FILE *out)
515 char from[BUFSIZ]; /* my network address */
516 char signature[BUFSIZ]; /* my signature */
517 char *resentstr = (msgstate == resent) ? "Resent-" : "";
519 if (!(msgflags & MDAT)) {
520 fprintf(out, "%sDate: %s\n", resentstr, dtimenow(0));
523 strncpy(from, getusername(), sizeof(from));
524 if ((cp = getfullname()) && *cp) {
525 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
527 snprintf(signature, sizeof(signature), "%s", from);
529 if (!(msgflags & MFRM)) {
530 fprintf(out, "%sFrom: %s\n", resentstr, signature);
532 /* In case the From: header contains multiple addresses. */
533 fprintf(out, "%sSender: %s\n", resentstr, from);
535 if (!(msgflags & MVIS)) {
536 fprintf(out, "%sBcc: Blind Distribution List: ;\n", resentstr);
539 adios(NULL, "re-format message and try again");
545 ** Return index of the requested header in the table, or NOTOK if missing.
548 get_header(char *header, struct headers *table)
552 for (h=table; h->value; h++) {
553 if (mh_strcasecmp(header, h->value)==0) {
563 ** output the address list for header "name". The address list
564 ** is a linked list of mailname structs. "nl" points to the head
565 ** of the list. Alias substitution should be done on nl.
568 putadr(char *name, struct mailname *nl)
570 struct mailname *mp, *mp2;
575 fprintf(out, "%s: ", name);
576 namelen = strlen(name) + 2;
579 for (mp = nl; mp; ) {
580 if (linepos > MAX_SM_FIELD) {
581 fprintf(out, "\n%s: ", name);
585 /* a local name - see if it's an alias */
586 cp = akvalue(mp->m_mbox);
587 if (cp == mp->m_mbox) {
588 /* wasn't an alias - use what the user typed */
589 linepos = putone(mp->m_text, linepos, namelen);
591 /* an alias - expand it */
592 while ((cp = getname(cp))) {
593 if (linepos > MAX_SM_FIELD) {
594 fprintf(out, "\n%s: ", name);
597 mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
599 mp2->m_pers = getcpy(mp->m_mbox);
600 linepos = putone(adrformat(mp2), linepos, namelen);
602 linepos = putone(mp2->m_text,
610 /* not a local name - use what the user typed */
611 linepos = putone(mp->m_text, linepos, namelen);
621 putone(char *adr, int pos, int indent)
629 else if (linepos+len > OUTPUTLINELEN) {
630 fprintf(out, ",\n%*s", indent, "");
646 ** Insert the normalized value from pp into fccfold[].
649 insert_fcc(char *name, unsigned char *pp)
653 for (cp = pp; isspace(*cp); cp++)
655 for (pp += strlen(pp) - 1; pp > cp && isspace(*pp); pp--)
663 adios(NULL, "too many %ss", name);
664 fccfold[fccind++] = getcpy(cp);
669 fcc(char *file, char *folder)
676 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
680 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
685 printf("Sorry man, but we had no more forks.\n");
687 fprintf(stderr, "Skipped %sFcc %s: unable to fork.\n",
688 msgstate == resent ? "Resent-" : "",
694 snprintf(fold, sizeof(fold), "%s%s",
695 *folder == '+' || *folder == '@' ? "" : "+",
697 execlp(fileproc, mhbasename(fileproc),
698 "-link", "-file", file, fold, NULL);
702 if ((status = pidwait(child_id, OK))) {
704 printf(" errored (0%o)\n", status);
706 fprintf(stderr, " %sFcc %s: errored (0%o)\n",
708 "Resent-" : "", folder,
721 make_bcc_draft(char *draft)
727 fd = mkstemp(bccfil);
728 if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
729 adios(bccfil, "unable to create");
732 if ((out = fopen(bccfil, "w")) == NULL)
733 adios(bccfil, "unable to create");
738 fprintf(out, "Subject: BCC: %s", subject);
740 fprintf(out, "Subject: BCC: message without subject");
742 fprintf(out, "%s: %s\n", attach_hdr, draft);
743 fprintf(out, "------------\n");