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 fccs[BUFSIZ] = "";
132 struct mailname *bccs = NULL;
134 static struct headers *hdrtab; /* table for the message we're doing */
135 static FILE *out; /* output (temp) file */
137 extern char *sendmail;
142 static void putfmt(char *, char *, FILE *);
143 static void finish_headers(FILE *);
144 static int get_header(char *, struct headers *);
145 static void putadr(char *, struct mailname *);
146 static int putone(char *, int, int);
147 static void fcc(char *, char *);
148 static void make_bcc_draft(char *);
152 main(int argc, char **argv)
154 int state, i, pid, compnum;
155 char *cp, *msg = NULL, **argp, **arguments;
156 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
160 setlocale(LC_ALL, "");
162 invo_name = mhbasename(argv[0]);
164 /* foil search of user profile/context */
165 if (context_foil(NULL) == -1)
168 arguments = getarguments(invo_name, argc, argv, 0);
171 while ((cp = *argp++)) {
173 switch (smatch(++cp, switches)) {
175 ambigsw(cp, switches);
178 adios(NULL, "-%s unknown", cp);
181 snprintf(buf, sizeof(buf),
182 "%s [switches] file",
184 print_help(buf, switches, 1);
187 print_version(invo_name);
220 if (!(cp = *argp++) || *cp == '-')
221 adios(NULL, "missing argument to %s",
224 if ((state = alias(cp)) != AK_OK)
225 adios(NULL, "aliasing error in file %s - %s", cp, akerror(state));
232 if (!(cp = *argp++) || *cp == '-')
233 adios(NULL, "missing argument to %s",
235 /* create a minimal context */
236 if (context_foil(cp) == -1)
242 adios(NULL, "only one message at a time!");
248 adios(NULL, "usage: %s [switches] file", invo_name);
250 if ((in = fopen(msg, "r")) == NULL)
251 adios(msg, "unable to open");
258 if ((out = fdopen(mkstemp(tmpfil), "w")) == NULL)
259 adios(tmpfil, "unable to create");
262 if ((out = fopen(tmpfil, "w")) == NULL)
263 adios(tmpfil, "unable to create");
268 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
270 for (compnum = 1, state = FLD;;) {
271 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
274 putfmt(name, buf, out);
280 while (state == FLDPLUS) {
281 state = m_getfld(state, name, buf,
285 putfmt(name, cp, out);
291 fprintf(out, "\n%s", buf);
292 while (state == BODY) {
293 state = m_getfld(state, name, buf,
305 adios(NULL, "message format error in component #%d",
309 adios(NULL, "getfld() returned %d", state);
329 make_bcc_draft(tmpfil);
333 ** re-open the temp file, unlink it and exec sendmail, giving it
334 ** the msg temp file as std in.
336 if (!freopen(tmpfil, "r", stdin)) {
337 adios(tmpfil, "can't reopen for sendmail");
342 *argp++ = "send-mail";
343 *argp++ = "-m"; /* send to me too */
344 *argp++ = "-t"; /* read msg for recipients */
345 *argp++ = "-i"; /* don't stop on "." */
346 if (watch || verbose) {
353 /* fork to a child to run sendmail */
354 for (i=0; (pid = fork()) == NOTOK && i < 5; i++)
358 fprintf(verbose ? stdout : stderr,
359 "%s: can't fork to %s\n",
360 invo_name, sendmail);
363 /* we're the child .. */
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 .. */
388 execv(sendmail, sargv);
389 adios(sendmail, "can't exec");
390 return 0; /* dead code to satisfy the compiler */
393 /* DRAFT GENERATION */
396 putfmt(char *name, char *str, FILE *out)
402 while (*str == ' ' || *str == '\t')
405 if ((i = get_header(name, hdrtab)) == NOTOK) {
406 /* some boring header: push it through */
407 fprintf(out, "%s: %s", name, str);
410 /* one of the interesting headers */
413 if (hdr->flags & HIGN) {
416 if (hdr->flags & HBAD) {
417 advise(NULL, "illegal header line -- %s:", name);
421 msgflags |= hdr->set;
423 if (hdr->flags & HSUB) {
425 /* concatenate mupliple subject */
426 char *cp = concat(subject, "\t", str, NULL);
430 subject = getcpy(str);
434 if (hdr->flags & HFCC) {
435 if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
436 adios(NULL, "Too much Fcc data");
438 /* todo: better have three states: SEPARATOR, PLUS, WORD */
440 for (cp=pp=str; *cp; cp++) {
449 if (*pp=='+' || *pp=='@') {
468 if (*str != '\n' && *str != '\0') {
469 if (hdr->flags & HBCC) {
470 struct mailname *mp = NULL;
472 /* Create list of Bcc adds. */
473 while ((cp = getname(str))) {
474 mp = getm(cp, NULL, 0, AD_HOST, NULL);
475 mp->m_next = bccs; /* push */
478 } else 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 ** Add yet missing headers.
532 finish_headers(FILE *out)
535 char from[BUFSIZ]; /* my network address */
536 char signature[BUFSIZ]; /* my signature */
537 char *resentstr = (msgstate == resent) ? "Resent-" : "";
539 if (!(msgflags & MDAT)) {
540 fprintf(out, "%sDate: %s\n", resentstr, dtimenow(0));
543 strncpy(from, getusername(), sizeof(from));
544 if ((cp = getfullname()) && *cp) {
545 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
547 snprintf(signature, sizeof(signature), "%s", from);
549 if (!(msgflags & MFRM)) {
550 fprintf(out, "%sFrom: %s\n", resentstr, signature);
552 /* In case the From: header contains multiple addresses. */
553 fprintf(out, "%sSender: %s\n", resentstr, from);
555 if (!(msgflags & MVIS)) {
556 fprintf(out, "%sBcc: Blind Distribution List: ;\n", resentstr);
559 adios(NULL, "re-format message and try again");
565 ** Return index of the requested header in the table, or NOTOK if missing.
568 get_header(char *header, struct headers *table)
572 for (h=table; h->value; h++) {
573 if (mh_strcasecmp(header, h->value)==0) {
583 ** output the address list for header "name". The address list
584 ** is a linked list of mailname structs. "nl" points to the head
585 ** of the list. Alias substitution should be done on nl.
588 putadr(char *name, struct mailname *nl)
590 struct mailname *mp, *mp2;
595 fprintf(out, "%s: ", name);
596 namelen = strlen(name) + 2;
599 for (mp = nl; mp; ) {
600 if (linepos > MAX_SM_FIELD) {
601 fprintf(out, "\n%s: ", name);
605 /* a local name - see if it's an alias */
606 cp = akvalue(mp->m_mbox);
607 if (cp == mp->m_mbox) {
608 /* wasn't an alias - use what the user typed */
609 linepos = putone(mp->m_text, linepos, namelen);
611 /* an alias - expand it */
612 while ((cp = getname(cp))) {
613 if (linepos > MAX_SM_FIELD) {
614 fprintf(out, "\n%s: ", name);
617 mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
619 mp2->m_pers = getcpy(mp->m_mbox);
620 linepos = putone(adrformat(mp2), linepos, namelen);
622 linepos = putone(mp2->m_text,
630 /* not a local name - use what the user typed */
631 linepos = putone(mp->m_text, linepos, namelen);
641 putone(char *adr, int pos, int indent)
649 else if (linepos+len > OUTPUTLINELEN) {
650 fprintf(out, ",\n%*s", indent, "");
666 fcc(char *file, char *folders)
672 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
676 if (strlen(fileproc)+strlen(file)+strlen(folders)+100 > sizeof cmd) {
677 adios(NULL, "Too much Fcc data");
679 /* hack: read from /dev/null and refile(1) won't question us */
680 snprintf(cmd, sizeof cmd, "</dev/null %s -link -file %s %s",
681 fileproc, file, folders);
682 status = system(cmd);
684 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
685 msgstate == resent ? "Resent-" : "", folders);
686 } else if (status != 0) {
687 fprintf(stderr, "%sFcc %s: Problems occured.\n",
688 msgstate == resent ? "Resent-" : "", folders);
696 make_bcc_draft(char *draft)
702 fd = mkstemp(bccfil);
703 if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
704 adios(bccfil, "unable to create");
707 if ((out = fopen(bccfil, "w")) == NULL)
708 adios(bccfil, "unable to create");
713 fprintf(out, "Subject: BCC: %s", subject);
715 fprintf(out, "Subject: BCC: message without subject");
717 fprintf(out, "%s: %s\n", attach_hdr, draft);
718 fprintf(out, "------------\n");