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 },
68 { "idanno number", -6 },
73 /* flags for headers->flags */
74 #define HNOP 0x0000 /* just used to keep .set around */
75 #define HBAD 0x0001 /* bad header - don't let it through */
76 #define HADR 0x0002 /* header has an address field */
77 #define HSUB 0x0004 /* Subject: header */
78 #define HTRY 0x0008 /* try to send to addrs on header */
79 #define HBCC 0x0010 /* don't output this header */
80 #define HMNG 0x0020 /* mung this header */
81 #define HNGR 0x0040 /* no groups allowed in this header */
82 #define HFCC 0x0080 /* FCC: type header */
83 #define HNIL 0x0100 /* okay for this header not to have addrs */
84 #define HIGN 0x0200 /* ignore this header */
86 /* flags for headers->set */
87 #define MFRM 0x0001 /* we've seen a From: */
88 #define MDAT 0x0002 /* we've seen a Date: */
89 #define MRFM 0x0004 /* we've seen a Resent-From: */
90 #define MVIS 0x0008 /* we've seen sighted addrs */
91 #define MINV 0x0010 /* we've seen blind addrs */
92 #define MRDT 0x0020 /* we've seen a Resent-Date: */
101 static struct headers NHeaders[] = {
102 { "Return-Path", HBAD, 0 },
103 { "Received", HBAD, 0 },
104 { "Reply-To", HADR|HNGR, 0 },
105 { "From", HADR|HNGR, MFRM },
106 { "Sender", HADR|HBAD, 0 },
107 { "Date", HNOP, MDAT },
108 { "Subject", HSUB, 0 },
109 { "To", HADR|HTRY, MVIS },
110 { "cc", HADR|HTRY, MVIS },
111 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
112 { "Message-Id", HBAD, 0 },
117 static struct headers RHeaders[] = {
118 { "Resent-Reply-To", HADR|HNGR, 0 },
119 { "Resent-From", HADR|HNGR, MRFM },
120 { "Resent-Sender", HADR|HBAD, 0 },
121 { "Resent-Date", HNOP, MRDT },
122 { "Resent-Subject", HSUB, 0 },
123 { "Resent-To", HADR|HTRY, MVIS },
124 { "Resent-Cc", HADR|HTRY, MVIS },
125 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
126 { "Resent-Message-Id", HBAD, 0 },
127 { "Resent-Fcc", HFCC, 0 },
128 { "Reply-To", HADR, 0 },
134 static short fccind = 0; /* index into fccfold[] */
136 static int badmsg = 0; /* message has bad semantics */
137 static int verbose = 0; /* spell it out */
138 static int debug = 0; /* debugging post */
139 static int rmflg = 1; /* remove temporary file when done */
140 static int watch = 0; /* watch the delivery process */
141 static int backflg = 0; /* rename input file as *.bak when done */
142 static int pushflg = 0; /* if going to fork to sendmail */
143 static int aliasflg = 0; /* if going to process aliases */
145 static unsigned msgflags = 0; /* what we've seen */
151 static char tmpfil[] = "/tmp/pstXXXXXX";
153 static char from[BUFSIZ]; /* my network address */
154 static char signature[BUFSIZ]; /* my signature */
155 static char *filter = NULL; /* the filter for BCC'ing */
156 static char *subject = NULL; /* the subject field for BCC'ing */
157 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
159 static struct headers *hdrtab; /* table for the message we're doing */
160 static FILE *out; /* output (temp) file */
162 extern char *sendmail;
165 ** external prototypes
167 extern char *getfullname(void);
168 extern char *getusername(void);
170 extern boolean draft_from_masquerading; /* defined in mts.c */
175 static void putfmt(char *, char *, FILE *);
176 static void start_headers(void);
177 static void finish_headers(FILE *);
178 static int get_header(char *, struct headers *);
179 static void putadr(char *, struct mailname *);
180 static int putone(char *, int, int);
181 static void insert_fcc(struct headers *, unsigned char *);
182 static void file(char *);
183 static void fcc(char *, char *);
186 static void die(char *, char *, ...);
187 static void make_bcc_file(void);
192 main(int argc, char **argv)
194 int state, i, pid, compnum;
195 char *cp, *msg = NULL, **argp, **arguments;
196 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
200 setlocale(LC_ALL, "");
202 invo_name = mhbasename(argv[0]);
204 /* foil search of user profile/context */
205 if (context_foil(NULL) == -1)
209 arguments = getarguments(invo_name, argc, argv, 0);
212 while ((cp = *argp++)) {
214 switch (smatch(++cp, switches)) {
216 ambigsw(cp, switches);
219 adios(NULL, "-%s unknown", cp);
222 snprintf(buf, sizeof(buf),
223 "%s [switches] file",
225 print_help(buf, switches, 1);
228 print_version(invo_name);
240 if (!(filter = *argp++) || *filter == '-')
241 adios(NULL, "missing argument to %s",
284 if (!(cp = *argp++) || *cp == '-')
285 adios(NULL, "missing argument to %s",
288 if ((state = alias(cp)) != AK_OK)
289 adios(NULL, "aliasing error in file %s - %s", cp, akerror(state) );
296 if (!(cp = *argp++) || *cp == '-')
297 adios(NULL, "missing argument to %s",
299 /* create a minimal context */
300 if (context_foil(cp) == -1)
305 /* -idanno switch ignored */
306 if (!(cp = *argp++) || *cp == '-')
307 adios(NULL, "missing argument to %s",
313 adios(NULL, "only one message at a time!");
319 adios(NULL, "usage: %s [switches] file", invo_name);
321 if ((in = fopen(msg, "r")) == NULL)
322 adios(msg, "unable to open");
331 if ((out = fdopen( mkstemp(tmpfil), "w" )) == NULL )
332 adios(tmpfil, "unable to create");
335 if ((out = fopen(tmpfil, "w")) == NULL)
336 adios(tmpfil, "unable to create");
341 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
343 for (compnum = 1, state = FLD;;) {
344 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
347 putfmt(name, buf, out);
353 while (state == FLDPLUS) {
354 state = m_getfld(state, name, buf,
358 putfmt(name, cp, out);
364 fprintf(out, "\n%s", buf);
365 while (state == BODY) {
366 state = m_getfld(state, name, buf,
378 adios(NULL, "message format error in component #%d",
382 adios(NULL, "getfld() returned %d", state);
389 strncpy(buf, m_backup(msg), sizeof(buf));
390 if (rename(msg, buf) == NOTOK)
391 advise(buf, "unable to rename %s to", msg);
403 ** re-open the temp file, unlink it and exec sendmail, giving it
404 ** the msg temp file as std in.
406 if (freopen(tmpfil, "r", stdin) == NULL)
407 adios(tmpfil, "can't reopen for sendmail");
412 *argp++ = "send-mail";
413 *argp++ = "-m"; /* send to me too */
414 *argp++ = "-t"; /* read msg for recipients */
415 *argp++ = "-i"; /* don't stop on "." */
416 if (watch || verbose)
420 if (pushflg && !(watch || verbose)) {
421 /* fork to a child to run sendmail */
422 for (i=0; (pid = fork()) == NOTOK && i < 5; i++)
426 fprintf(verbose ? stdout : stderr,
427 "%s: can't fork to %s\n",
428 invo_name, sendmail);
431 /* we're the child .. */
437 execv(sendmail, sargv);
438 adios(sendmail, "can't exec");
439 return 0; /* dead code to satisfy the compiler */
442 /* DRAFT GENERATION */
445 putfmt(char *name, char *str, FILE *out)
451 while (*str == ' ' || *str == '\t')
454 if ((i = get_header(name, hdrtab)) == NOTOK) {
455 fprintf(out, "%s: %s", name, str);
460 if (hdr->flags & HIGN)
462 if (hdr->flags & HBAD) {
463 advise(NULL, "illegal header line -- %s:", name);
467 msgflags |= hdr->set;
469 if (hdr->flags & HSUB)
470 subject = subject ? add(str, add("\t", subject)) :
473 if (hdr->flags & HFCC) {
474 if ((cp = strrchr(str, '\n')))
476 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
485 if (hdr->flags & HBCC) {
491 if (*str != '\n' && *str != '\0') {
492 if (aliasflg && hdr->flags & HTRY) {
494 ** this header contains address(es) that we have to do
495 ** alias expansion on. Because of the saved state in
496 ** getname we have to put all the addresses into a
497 ** list. We then let putadr munch on that list,
498 ** possibly expanding aliases.
500 register struct mailname *f = 0;
501 register struct mailname *mp = 0;
503 while ((cp = getname(str))) {
504 mp = getm( cp, NULL, 0, AD_HOST, NULL);
509 mp->m_next = f->m_next;
514 f = mp->m_next; mp->m_next = 0;
518 ** The author(s) of spost decided that alias
519 ** substitution wasn't necessary for the non-HTRY
520 ** headers. Unfortunately, one of those headers
521 ** is "From:", and having alias substitution
522 ** work on that is extremely useful for someone
523 ** with a lot of POP3 email accounts or aliases.
524 ** post supports aliasing of "From:"...
526 ** Since "From:"-processing is incompletely
527 ** implemented in this unsupported and
528 ** undocumented spost backend, I'm not
529 ** going to take the time to implement my new
530 ** draft-From:-based email address masquerading.
531 ** If I do ever implement it here, I'd almost
532 ** certainly want to implement "From:" line
533 ** alias processing as well.
534 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
536 fprintf(out, "%s: %s", name, str );
548 strncpy(from, getusername(), sizeof(from));
550 if ((cp = getfullname()) && *cp) {
551 strncpy(sigbuf, cp, sizeof(sigbuf));
552 snprintf(signature, sizeof(signature), "%s <%s>",
555 snprintf(signature, sizeof(signature), "%s", from);
560 finish_headers(FILE *out)
564 if (!(msgflags & MDAT))
565 fprintf(out, "Date: %s\n", dtimenow(0));
567 if (msgflags & MFRM) {
569 ** There was already a From: in the draft.
572 if (!draft_from_masquerading)
574 ** mts.conf didn't contain
575 ** "masquerade:[...]draft_from[...]"
576 ** so we'll reveal the user's
577 ** actual account@thismachine
578 ** address in a Sender: header
579 ** (and use it as the envelope
582 fprintf(out, "Sender: %s\n", from);
584 fprintf(out, "From: %s\n", signature);
587 if (!(msgflags & MVIS))
588 fprintf(out, "Bcc: Blind Distribution List: ;\n");
593 if (!(msgflags & MRDT))
594 fprintf(out, "Resent-Date: %s\n", dtimenow(0));
595 if (msgflags & MRFM) {
597 ** There was already a Resent-From: in draft.
600 if (!draft_from_masquerading)
602 ** mts.conf didn't contain
603 ** "masquerade:[...]draft_from[...]"
604 ** so we'll reveal the user's
605 ** actual account@thismachine
606 ** address in a Sender: header
607 ** (and use it as the envelope
610 fprintf(out, "Resent-Sender: %s\n",
613 /* Construct a Resent-From: header. */
614 fprintf(out, "Resent-From: %s\n", signature);
616 if (!(msgflags & MVIS))
617 fprintf(out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
623 adios(NULL, "re-format message and try again");
628 get_header(char *header, struct headers *table)
632 for (h = table; h->value; h++)
633 if (!mh_strcasecmp(header, h->value))
641 ** output the address list for header "name". The address list
642 ** is a linked list of mailname structs. "nl" points to the head
643 ** of the list. Alias substitution should be done on nl.
646 putadr(char *name, struct mailname *nl)
648 register struct mailname *mp, *mp2;
649 register int linepos;
653 fprintf(out, "%s: ", name);
654 namelen = strlen(name) + 2;
657 for (mp = nl; mp; ) {
658 if (linepos > MAX_SM_FIELD) {
659 fprintf(out, "\n%s: ", name);
663 /* a local name - see if it's an alias */
664 cp = akvalue(mp->m_mbox);
665 if (cp == mp->m_mbox)
666 /* wasn't an alias - use what the user typed */
667 linepos = putone(mp->m_text, linepos, namelen);
669 /* an alias - expand it */
670 while ((cp = getname(cp))) {
671 if (linepos > MAX_SM_FIELD) {
672 fprintf(out, "\n%s: ", name);
675 mp2 = getm( cp, NULL, 0, AD_HOST, NULL);
677 mp2->m_pers = getcpy(mp->m_mbox);
678 linepos = putone( adrformat(mp2), linepos, namelen );
680 linepos = putone( mp2->m_text, linepos, namelen );
685 /* not a local name - use what the user typed */
686 linepos = putone( mp->m_text, linepos, namelen );
696 putone(char *adr, int pos, int indent)
704 else if (linepos+len > OUTPUTLINELEN) {
705 fprintf( out, ",\n%*s", indent, "");
721 insert_fcc(struct headers *hdr, unsigned char *pp)
725 for (cp = pp; isspace(*cp); cp++)
727 for (pp += strlen(pp) - 1; pp > cp && isspace(*pp); pp--)
735 adios(NULL, "too many %ss", hdr->value);
736 fccfold[fccind++] = getcpy(cp);
751 fd = mkstemp(bccfil);
752 if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
753 adios(bccfil, "unable to create");
756 if ((out = fopen(bccfil, "w")) == NULL)
757 adios(bccfil, "unable to create");
761 fprintf(out, "Date: %s\n", dtimenow(0));
762 if (msgflags & MFRM) {
763 /* There was already a From: in the draft. Don't add one. */
764 if (!draft_from_masquerading)
766 ** mts.conf didn't contain "masquerade:[...]draft_from[...]"
767 ** so we'll reveal the user's actual account@thismachine
768 ** address in a Sender: header (and use it as the envelope
771 fprintf(out, "Sender: %s\n", from);
773 /* Construct a From: header. */
774 fprintf(out, "From: %s\n", signature);
776 fprintf(out, "Subject: %s", subject);
777 fprintf(out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
780 if (filter == NULL) {
781 if ((fd = open(tmpfil, O_RDONLY)) == NOTOK)
782 adios(NULL, "unable to re-open");
783 cpydgst(fd, fileno(out), tmpfil, bccfil);
786 vec[0] = mhbasename(mhlproc);
788 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
792 adios("fork", "unable to");
795 dup2(fileno(out), 1);
798 vec[i++] = "-forward";
804 execvp(mhlproc, vec);
805 adios(mhlproc, "unable to exec");
808 if (status = pidwait(child_id, OK))
809 admonish(NULL, "%s lost (status=0%o)",
815 fseek(out, 0L, SEEK_END);
816 fprintf(out, "\n------- End of Blind-Carbon-Copy\n");
821 /* FCC INTERACTION */
831 for (i = 0; i < fccind; i++)
832 fcc(path, fccfold[i]);
837 fcc(char *file, char *folder)
844 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
848 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
853 fprintf(stderr, " %sFcc %s: ", msgstate == resent ?
854 "Resent-" : "", folder);
855 fprintf(verbose ? stdout : stderr, "no forks, so not ok\n");
859 snprintf(fold, sizeof(fold), "%s%s",
860 *folder == '+' || *folder == '@' ? "" : "+", folder);
861 execlp(fileproc, mhbasename(fileproc),
862 "-link", "-file", file, fold, NULL);
866 if ((status = pidwait(child_id, OK))) {
868 fprintf(stderr, " %sFcc %s: ",
870 "Resent-" : "", folder);
871 fprintf(verbose ? stdout : stderr, " errored (0%o)\n",
887 die(char *what, char *fmt, ...)
892 advertise(what, NULL, fmt, ap);