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 },
50 { "alias aliasfile", 0 },
60 { "dist", -4 }, /* interface from dist */
61 #define PUSHSW 18 /* fork to sendmail then exit */
63 #define NPUSHSW 19 /* exec sendmail */
66 { "library directory", -7 },
71 /* flags for headers->flags */
72 #define HNOP 0x0000 /* just used to keep .set around */
73 #define HBAD 0x0001 /* bad header - don't let it through */
74 #define HADR 0x0002 /* header has an address field */
75 #define HSUB 0x0004 /* Subject: header */
76 #define HTRY 0x0008 /* try to send to addrs on header */
77 #define HBCC 0x0010 /* don't output this header */
78 #define HMNG 0x0020 /* mung this header */
79 #define HNGR 0x0040 /* no groups allowed in this header */
80 #define HFCC 0x0080 /* FCC: type header */
81 #define HNIL 0x0100 /* okay for this header not to have addrs */
82 #define HIGN 0x0200 /* ignore this header */
84 /* flags for headers->set */
85 #define MFRM 0x0001 /* we've seen a From: */
86 #define MDAT 0x0002 /* we've seen a Date: */
87 #define MRFM 0x0004 /* we've seen a Resent-From: */
88 #define MVIS 0x0008 /* we've seen sighted addrs */
89 #define MINV 0x0010 /* we've seen blind addrs */
90 #define MRDT 0x0020 /* we've seen a Resent-Date: */
99 static struct headers NHeaders[] = {
100 { "Return-Path", HBAD, 0 },
101 { "Received", HBAD, 0 },
102 { "Reply-To", HADR|HNGR, 0 },
103 { "From", HADR|HNGR, MFRM },
104 { "Sender", HADR|HBAD, 0 },
105 { "Date", HNOP, MDAT },
106 { "Subject", HSUB, 0 },
107 { "To", HADR|HTRY, MVIS },
108 { "Cc", HADR|HTRY, MVIS },
109 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
110 { "Message-Id", HBAD, 0 },
115 static struct headers RHeaders[] = {
116 { "Resent-Reply-To", HADR|HNGR, 0 },
117 { "Resent-From", HADR|HNGR, MRFM },
118 { "Resent-Sender", HADR|HBAD, 0 },
119 { "Resent-Date", HNOP, MRDT },
120 { "Resent-Subject", HSUB, 0 },
121 { "Resent-To", HADR|HTRY, MVIS },
122 { "Resent-Cc", HADR|HTRY, MVIS },
123 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
124 { "Resent-Message-Id", HBAD, 0 },
125 { "Resent-Fcc", HFCC, 0 },
126 { "Reply-To", HADR, 0 },
132 static int badmsg = 0; /* message has bad semantics */
133 static int verbose = 0; /* spell it out */
134 static int debug = 0; /* debugging post */
135 static int rmflg = 1; /* remove temporary file when done */
136 static int watch = 0; /* watch the delivery process */
137 static int backflg = 0; /* rename input file as *.bak when done */
138 static int pushflg = 0; /* if going to fork to sendmail */
139 static int aliasflg = 0; /* if going to process aliases */
141 static unsigned msgflags = 0; /* what we've seen */
147 static char tmpfil[] = "/tmp/pstXXXXXX";
149 static char from[BUFSIZ]; /* my network address */
150 static char signature[BUFSIZ]; /* my signature */
151 static char *filter = NULL; /* the filter for BCC'ing */
152 static char *subject = NULL; /* the subject field for BCC'ing */
153 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
154 static short fccind = 0; /* index into fccfold[] */
156 static struct headers *hdrtab; /* table for the message we're doing */
157 static FILE *out; /* output (temp) file */
159 extern char *sendmail;
162 ** external prototypes
164 extern char *getfullname(void);
165 extern char *getusername(void);
167 extern boolean draft_from_masquerading; /* defined in mts.c */
172 static void putfmt(char *, char *, FILE *);
173 static void start_headers(void);
174 static void finish_headers(FILE *);
175 static int get_header(char *, struct headers *);
176 static void putadr(char *, struct mailname *);
177 static int putone(char *, int, int);
178 static void insert_fcc(char *, unsigned char *);
179 static void fcc(char *, char *);
182 static void make_bcc_file(void);
187 main(int argc, char **argv)
189 int state, i, pid, compnum;
190 char *cp, *msg = NULL, **argp, **arguments;
191 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
195 setlocale(LC_ALL, "");
197 invo_name = mhbasename(argv[0]);
199 /* foil search of user profile/context */
200 if (context_foil(NULL) == -1)
204 arguments = getarguments(invo_name, argc, argv, 0);
207 while ((cp = *argp++)) {
209 switch (smatch(++cp, switches)) {
211 ambigsw(cp, switches);
214 adios(NULL, "-%s unknown", cp);
217 snprintf(buf, sizeof(buf),
218 "%s [switches] file",
220 print_help(buf, switches, 1);
223 print_version(invo_name);
235 if (!(filter = *argp++) || *filter == '-')
236 adios(NULL, "missing argument to %s",
279 if (!(cp = *argp++) || *cp == '-')
280 adios(NULL, "missing argument to %s",
283 if ((state = alias(cp)) != AK_OK)
284 adios(NULL, "aliasing error in file %s - %s", cp, akerror(state) );
291 if (!(cp = *argp++) || *cp == '-')
292 adios(NULL, "missing argument to %s",
294 /* create a minimal context */
295 if (context_foil(cp) == -1)
301 adios(NULL, "only one message at a time!");
307 adios(NULL, "usage: %s [switches] file", invo_name);
309 if ((in = fopen(msg, "r")) == NULL)
310 adios(msg, "unable to open");
319 if ((out = fdopen( mkstemp(tmpfil), "w" )) == NULL )
320 adios(tmpfil, "unable to create");
323 if ((out = fopen(tmpfil, "w")) == NULL)
324 adios(tmpfil, "unable to create");
329 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
331 for (compnum = 1, state = FLD;;) {
332 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
335 putfmt(name, buf, out);
341 while (state == FLDPLUS) {
342 state = m_getfld(state, name, buf,
346 putfmt(name, cp, out);
352 fprintf(out, "\n%s", buf);
353 while (state == BODY) {
354 state = m_getfld(state, name, buf,
366 adios(NULL, "message format error in component #%d",
370 adios(NULL, "getfld() returned %d", state);
377 strncpy(buf, m_backup(msg), sizeof(buf));
378 if (rename(msg, buf) == NOTOK)
379 advise(buf, "unable to rename %s to", msg);
389 for (i=0; i<fccind; i++) {
390 fcc(tmpfil, fccfold[i]);
394 ** re-open the temp file, unlink it and exec sendmail, giving it
395 ** the msg temp file as std in.
397 if (freopen(tmpfil, "r", stdin) == NULL)
398 adios(tmpfil, "can't reopen for sendmail");
403 *argp++ = "send-mail";
404 *argp++ = "-m"; /* send to me too */
405 *argp++ = "-t"; /* read msg for recipients */
406 *argp++ = "-i"; /* don't stop on "." */
407 if (watch || verbose)
411 if (pushflg && !(watch || verbose)) {
412 /* fork to a child to run sendmail */
413 for (i=0; (pid = fork()) == NOTOK && i < 5; i++)
417 fprintf(verbose ? stdout : stderr,
418 "%s: can't fork to %s\n",
419 invo_name, sendmail);
422 /* we're the child .. */
428 execv(sendmail, sargv);
429 adios(sendmail, "can't exec");
430 return 0; /* dead code to satisfy the compiler */
433 /* DRAFT GENERATION */
436 putfmt(char *name, char *str, FILE *out)
442 while (*str == ' ' || *str == '\t')
445 if ((i = get_header(name, hdrtab)) == NOTOK) {
446 /* some boring header: push it through */
447 fprintf(out, "%s: %s", name, str);
451 /* one of the interesting headers */
453 if (hdr->flags & HIGN) {
456 if (hdr->flags & HBAD) {
457 advise(NULL, "illegal header line -- %s:", name);
461 msgflags |= hdr->set;
463 if (hdr->flags & HSUB) {
465 /* append mupliple subject */
466 char *cp = concat(subject, "\t", str, NULL);
470 subject = getcpy(str);
474 if (hdr->flags & HFCC) {
475 if ((cp = strrchr(str, '\n'))) {
478 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
480 insert_fcc(hdr->value, pp);
482 insert_fcc(hdr->value, pp);
487 if (hdr->flags & HBCC) {
493 if (*str != '\n' && *str != '\0') {
494 if (aliasflg && hdr->flags & HTRY) {
496 ** this header contains address(es) that we have to do
497 ** alias expansion on. Because of the saved state in
498 ** getname we have to put all the addresses into a
499 ** list. We then let putadr munch on that list,
500 ** possibly expanding aliases.
502 register struct mailname *f = 0;
503 register struct mailname *mp = 0;
505 while ((cp = getname(str))) {
506 mp = getm(cp, NULL, 0, AD_HOST, NULL);
511 mp->m_next = f->m_next;
516 f = mp->m_next; mp->m_next = 0;
520 ** The author(s) of spost decided that alias
521 ** substitution wasn't necessary for the non-HTRY
522 ** headers. Unfortunately, one of those headers
523 ** is "From:", and having alias substitution
524 ** work on that is extremely useful for someone
525 ** with a lot of POP3 email accounts or aliases.
526 ** post supports aliasing of "From:"...
528 ** Since "From:"-processing is incompletely
529 ** implemented in this unsupported and
530 ** undocumented spost backend, I'm not
531 ** going to take the time to implement my new
532 ** draft-From:-based email address masquerading.
533 ** If I do ever implement it here, I'd almost
534 ** certainly want to implement "From:" line
535 ** alias processing as well.
536 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
538 fprintf(out, "%s: %s", name, str );
545 ** Construct signature name
552 strncpy(from, getusername(), sizeof(from));
553 if ((cp = getfullname()) && *cp) {
554 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
556 snprintf(signature, sizeof(signature), "%s", from);
562 ** Add yet missing headers.
565 finish_headers(FILE *out)
567 char *resentstr = (msgstate == resent) ? "Resent-" : "";
569 if (!(msgflags & MDAT)) {
570 fprintf(out, "%sDate: %s\n", resentstr, dtimenow(0));
572 if (!(msgflags & MFRM)) {
573 fprintf(out, "%sFrom: %s\n", resentstr, signature);
574 } else if (!draft_from_masquerading) {
576 ** mts.conf didn't contain "masquerade:[...]draft_from[...]"
577 ** so we'll reveal the user's actual account@thismachine
578 ** address in a Sender: header (and use it as the envelope
581 fprintf(out, "%sSender: %s\n", resentstr, from);
584 if (!(msgflags & MVIS))
585 fprintf(out, "%sBcc: Blind Distribution List: ;\n", resentstr);
589 adios(NULL, "re-format message and try again");
595 ** Return index of the requested header in the table, or NOTOK if missing.
598 get_header(char *header, struct headers *table)
602 for (h=table; h->value; h++) {
603 if (mh_strcasecmp(header, h->value)==0) {
613 ** output the address list for header "name". The address list
614 ** is a linked list of mailname structs. "nl" points to the head
615 ** of the list. Alias substitution should be done on nl.
618 putadr(char *name, struct mailname *nl)
620 register struct mailname *mp, *mp2;
621 register int linepos;
625 fprintf(out, "%s: ", name);
626 namelen = strlen(name) + 2;
629 for (mp = nl; mp; ) {
630 if (linepos > MAX_SM_FIELD) {
631 fprintf(out, "\n%s: ", name);
635 /* a local name - see if it's an alias */
636 cp = akvalue(mp->m_mbox);
637 if (cp == mp->m_mbox)
638 /* wasn't an alias - use what the user typed */
639 linepos = putone(mp->m_text, linepos, namelen);
641 /* an alias - expand it */
642 while ((cp = getname(cp))) {
643 if (linepos > MAX_SM_FIELD) {
644 fprintf(out, "\n%s: ", name);
647 mp2 = getm( cp, NULL, 0, AD_HOST, NULL);
649 mp2->m_pers = getcpy(mp->m_mbox);
650 linepos = putone( adrformat(mp2), linepos, namelen );
652 linepos = putone( mp2->m_text, linepos, namelen );
657 /* not a local name - use what the user typed */
658 linepos = putone( mp->m_text, linepos, namelen );
668 putone(char *adr, int pos, int indent)
676 else if (linepos+len > OUTPUTLINELEN) {
677 fprintf( out, ",\n%*s", indent, "");
693 ** Insert the normalized value from pp into fccfold[].
696 insert_fcc(char *name, unsigned char *pp)
700 for (cp = pp; isspace(*cp); cp++)
702 for (pp += strlen(pp) - 1; pp > cp && isspace(*pp); pp--)
710 adios(NULL, "too many %ss", name);
711 fccfold[fccind++] = getcpy(cp);
726 fd = mkstemp(bccfil);
727 if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
728 adios(bccfil, "unable to create");
731 if ((out = fopen(bccfil, "w")) == NULL)
732 adios(bccfil, "unable to create");
736 fprintf(out, "Date: %s\n", dtimenow(0));
737 if (msgflags & MFRM) {
738 /* There was already a From: in the draft. Don't add one. */
739 if (!draft_from_masquerading)
741 ** mts.conf didn't contain "masquerade:[...]draft_from[...]"
742 ** so we'll reveal the user's actual account@thismachine
743 ** address in a Sender: header (and use it as the envelope
746 fprintf(out, "Sender: %s\n", from);
748 /* Construct a From: header. */
749 fprintf(out, "From: %s\n", signature);
751 fprintf(out, "Subject: %s", subject);
752 fprintf(out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
755 if (filter == NULL) {
756 if ((fd = open(tmpfil, O_RDONLY)) == NOTOK)
757 adios(NULL, "unable to re-open");
758 cpydgst(fd, fileno(out), tmpfil, bccfil);
761 vec[0] = mhbasename(mhlproc);
763 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
767 adios("fork", "unable to");
770 dup2(fileno(out), 1);
773 vec[i++] = "-forward";
779 execvp(mhlproc, vec);
780 adios(mhlproc, "unable to exec");
783 if (status = pidwait(child_id, OK))
784 admonish(NULL, "%s lost (status=0%o)",
790 fseek(out, 0L, SEEK_END);
791 fprintf(out, "\n------- End of Blind-Carbon-Copy\n");
797 /* FCC INTERACTION */
800 fcc(char *file, char *folder)
807 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
811 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
816 printf("Sorry man, but we had no more forks.\n");
818 fprintf(stderr, "Skipped %sFcc %s: unable to fork.\n",
819 msgstate == resent ? "Resent-" : "",
825 snprintf(fold, sizeof(fold), "%s%s",
826 *folder == '+' || *folder == '@' ? "" : "+",
828 execlp(fileproc, mhbasename(fileproc),
829 "-link", "-file", file, fold, NULL);
833 if ((status = pidwait(child_id, OK))) {
835 printf(" errored (0%o)\n", status);
837 fprintf(stderr, " %sFcc %s: errored (0%o)\n",
839 "Resent-" : "", folder,