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 *filter = NULL; /* the filter for BCC'ing */
141 static char *subject = NULL; /* the subject field for BCC'ing */
142 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
143 static short fccind = 0; /* index into fccfold[] */
145 static struct headers *hdrtab; /* table for the message we're doing */
146 static FILE *out; /* output (temp) file */
148 extern char *sendmail;
151 ** external prototypes
153 extern char *getfullname(void);
154 extern char *getusername(void);
159 static void putfmt(char *, char *, FILE *);
160 static void finish_headers(FILE *);
161 static int get_header(char *, struct headers *);
162 static void putadr(char *, struct mailname *);
163 static int putone(char *, int, int);
164 static void insert_fcc(char *, unsigned char *);
165 static void fcc(char *, char *);
168 static void make_bcc_file(void);
173 main(int argc, char **argv)
175 int state, i, pid, compnum;
176 char *cp, *msg = NULL, **argp, **arguments;
177 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
181 setlocale(LC_ALL, "");
183 invo_name = mhbasename(argv[0]);
185 /* foil search of user profile/context */
186 if (context_foil(NULL) == -1)
189 arguments = getarguments(invo_name, argc, argv, 0);
192 while ((cp = *argp++)) {
194 switch (smatch(++cp, switches)) {
196 ambigsw(cp, switches);
199 adios(NULL, "-%s unknown", cp);
202 snprintf(buf, sizeof(buf),
203 "%s [switches] file",
205 print_help(buf, switches, 1);
208 print_version(invo_name);
220 if (!(filter = *argp++) || *filter == '-')
221 adios(NULL, "missing argument to %s",
257 if (!(cp = *argp++) || *cp == '-')
258 adios(NULL, "missing argument to %s",
261 if ((state = alias(cp)) != AK_OK)
262 adios(NULL, "aliasing error in file %s - %s", cp, akerror(state));
269 if (!(cp = *argp++) || *cp == '-')
270 adios(NULL, "missing argument to %s",
272 /* create a minimal context */
273 if (context_foil(cp) == -1)
279 adios(NULL, "only one message at a time!");
285 adios(NULL, "usage: %s [switches] file", invo_name);
287 if ((in = fopen(msg, "r")) == NULL)
288 adios(msg, "unable to open");
295 if ((out = fdopen(mkstemp(tmpfil), "w")) == NULL)
296 adios(tmpfil, "unable to create");
299 if ((out = fopen(tmpfil, "w")) == NULL)
300 adios(tmpfil, "unable to create");
305 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
307 for (compnum = 1, state = FLD;;) {
308 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
311 putfmt(name, buf, out);
317 while (state == FLDPLUS) {
318 state = m_getfld(state, name, buf,
322 putfmt(name, cp, out);
328 fprintf(out, "\n%s", buf);
329 while (state == BODY) {
330 state = m_getfld(state, name, buf,
342 adios(NULL, "message format error in component #%d",
346 adios(NULL, "getfld() returned %d", state);
353 strncpy(buf, m_backup(msg), sizeof(buf));
354 if (rename(msg, buf) == NOTOK)
355 advise(buf, "unable to rename %s to", msg);
366 for (i=0; i<fccind; i++) {
367 fcc(tmpfil, fccfold[i]);
371 ** re-open the temp file, unlink it and exec sendmail, giving it
372 ** the msg temp file as std in.
374 if (!freopen(tmpfil, "r", stdin)) {
375 adios(tmpfil, "can't reopen for sendmail");
380 *argp++ = "send-mail";
381 *argp++ = "-m"; /* send to me too */
382 *argp++ = "-t"; /* read msg for recipients */
383 *argp++ = "-i"; /* don't stop on "." */
384 if (watch || verbose)
388 if (pushflg && !(watch || verbose)) {
389 /* fork to a child to run sendmail */
390 for (i=0; (pid = fork()) == NOTOK && i < 5; i++)
394 fprintf(verbose ? stdout : stderr,
395 "%s: can't fork to %s\n",
396 invo_name, sendmail);
399 /* we're the child .. */
405 execv(sendmail, sargv);
406 adios(sendmail, "can't exec");
407 return 0; /* dead code to satisfy the compiler */
410 /* DRAFT GENERATION */
413 putfmt(char *name, char *str, FILE *out)
419 while (*str == ' ' || *str == '\t')
422 if ((i = get_header(name, hdrtab)) == NOTOK) {
423 /* some boring header: push it through */
424 fprintf(out, "%s: %s", name, str);
428 /* one of the interesting headers */
430 if (hdr->flags & HIGN) {
433 if (hdr->flags & HBAD) {
434 advise(NULL, "illegal header line -- %s:", name);
438 msgflags |= hdr->set;
440 if (hdr->flags & HSUB) {
442 /* append mupliple subject */
443 char *cp = concat(subject, "\t", str, NULL);
447 subject = getcpy(str);
451 if (hdr->flags & HFCC) {
452 if ((cp = strrchr(str, '\n'))) {
455 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
457 insert_fcc(hdr->value, pp);
459 insert_fcc(hdr->value, pp);
464 if (hdr->flags & HBCC) {
470 if (*str != '\n' && *str != '\0') {
471 if (aliasflg && hdr->flags & HTRY) {
473 ** this header contains address(es) that we have to do
474 ** alias expansion on. Because of the saved state in
475 ** getname we have to put all the addresses into a
476 ** list. We then let putadr munch on that list,
477 ** possibly expanding aliases.
479 register struct mailname *f = 0;
480 register struct mailname *mp = 0;
482 while ((cp = getname(str))) {
483 mp = getm(cp, NULL, 0, AD_HOST, NULL);
488 mp->m_next = f->m_next;
493 f = mp->m_next; mp->m_next = 0;
497 ** The author(s) of spost decided that alias
498 ** substitution wasn't necessary for the non-HTRY
499 ** headers. Unfortunately, one of those headers
500 ** is "From:", and having alias substitution
501 ** work on that is extremely useful for someone
502 ** with a lot of POP3 email accounts or aliases.
503 ** post supports aliasing of "From:"...
505 ** Since "From:"-processing is incompletely
506 ** implemented in this unsupported and
507 ** undocumented spost backend, I'm not
508 ** going to take the time to implement my new
509 ** draft-From:-based email address masquerading.
510 ** If I do ever implement it here, I'd almost
511 ** certainly want to implement "From:" line
512 ** alias processing as well.
513 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
515 fprintf(out, "%s: %s", name, str);
522 ** Add yet missing headers.
525 finish_headers(FILE *out)
528 char from[BUFSIZ]; /* my network address */
529 char signature[BUFSIZ]; /* my signature */
530 char *resentstr = (msgstate == resent) ? "Resent-" : "";
532 if (!(msgflags & MDAT)) {
533 fprintf(out, "%sDate: %s\n", resentstr, dtimenow(0));
536 strncpy(from, getusername(), sizeof(from));
537 if ((cp = getfullname()) && *cp) {
538 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
540 snprintf(signature, sizeof(signature), "%s", from);
542 if (!(msgflags & MFRM)) {
543 fprintf(out, "%sFrom: %s\n", resentstr, signature);
545 /* In case the From: header contains multiple addresses. */
546 fprintf(out, "%sSender: %s\n", resentstr, from);
549 if (!(msgflags & MVIS))
550 fprintf(out, "%sBcc: Blind Distribution List: ;\n", resentstr);
554 adios(NULL, "re-format message and try again");
560 ** Return index of the requested header in the table, or NOTOK if missing.
563 get_header(char *header, struct headers *table)
567 for (h=table; h->value; h++) {
568 if (mh_strcasecmp(header, h->value)==0) {
578 ** output the address list for header "name". The address list
579 ** is a linked list of mailname structs. "nl" points to the head
580 ** of the list. Alias substitution should be done on nl.
583 putadr(char *name, struct mailname *nl)
585 register struct mailname *mp, *mp2;
586 register int linepos;
590 fprintf(out, "%s: ", name);
591 namelen = strlen(name) + 2;
594 for (mp = nl; mp; ) {
595 if (linepos > MAX_SM_FIELD) {
596 fprintf(out, "\n%s: ", name);
600 /* a local name - see if it's an alias */
601 cp = akvalue(mp->m_mbox);
602 if (cp == mp->m_mbox) {
603 /* wasn't an alias - use what the user typed */
604 linepos = putone(mp->m_text, linepos, namelen);
606 /* an alias - expand it */
607 while ((cp = getname(cp))) {
608 if (linepos > MAX_SM_FIELD) {
609 fprintf(out, "\n%s: ", name);
612 mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
614 mp2->m_pers = getcpy(mp->m_mbox);
615 linepos = putone(adrformat(mp2), linepos, namelen);
617 linepos = putone(mp2->m_text,
625 /* not a local name - use what the user typed */
626 linepos = putone(mp->m_text, linepos, namelen);
636 putone(char *adr, int pos, int indent)
644 else if (linepos+len > OUTPUTLINELEN) {
645 fprintf(out, ",\n%*s", indent, "");
661 ** Insert the normalized value from pp into fccfold[].
664 insert_fcc(char *name, unsigned char *pp)
668 for (cp = pp; isspace(*cp); cp++)
670 for (pp += strlen(pp) - 1; pp > cp && isspace(*pp); pp--)
678 adios(NULL, "too many %ss", name);
679 fccfold[fccind++] = getcpy(cp);
694 fd = mkstemp(bccfil);
695 if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
696 adios(bccfil, "unable to create");
699 if ((out = fopen(bccfil, "w")) == NULL)
700 adios(bccfil, "unable to create");
704 fprintf(out, "Date: %s\n", dtimenow(0));
705 if (!(msgflags & MFRM)) {
706 /* Construct a From: header. */
707 fprintf(out, "From: %s\n", signature);
709 /* Add a Sender: header in case the From: header is fake. */
710 fprintf(out, "Sender: %s\n", from);
713 fprintf(out, "Subject: %s", subject);
714 fprintf(out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
718 if ((fd = open(tmpfil, O_RDONLY)) == NOTOK)
719 adios(NULL, "unable to re-open");
720 cpydgst(fd, fileno(out), tmpfil, bccfil);
723 vec[0] = mhbasename(mhlproc);
725 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
729 adios("fork", "unable to");
732 dup2(fileno(out), 1);
735 vec[i++] = "-forward";
741 execvp(mhlproc, vec);
742 adios(mhlproc, "unable to exec");
745 if (status = pidwait(child_id, OK))
746 admonish(NULL, "%s lost (status=0%o)",
752 fseek(out, 0L, SEEK_END);
753 fprintf(out, "\n------- End of Blind-Carbon-Copy\n");
759 /* FCC INTERACTION */
762 fcc(char *file, char *folder)
769 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
773 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
778 printf("Sorry man, but we had no more forks.\n");
780 fprintf(stderr, "Skipped %sFcc %s: unable to fork.\n",
781 msgstate == resent ? "Resent-" : "",
787 snprintf(fold, sizeof(fold), "%s%s",
788 *folder == '+' || *folder == '@' ? "" : "+",
790 execlp(fileproc, mhbasename(fileproc),
791 "-link", "-file", file, fold, NULL);
795 if ((status = pidwait(child_id, OK))) {
797 printf(" errored (0%o)\n", status);
799 fprintf(stderr, " %sFcc %s: errored (0%o)\n",
801 "Resent-" : "", folder,