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 short fccind = 0; /* index into fccfold[] */
134 static int badmsg = 0; /* message has bad semantics */
135 static int verbose = 0; /* spell it out */
136 static int debug = 0; /* debugging post */
137 static int rmflg = 1; /* remove temporary file when done */
138 static int watch = 0; /* watch the delivery process */
139 static int backflg = 0; /* rename input file as *.bak when done */
140 static int pushflg = 0; /* if going to fork to sendmail */
141 static int aliasflg = 0; /* if going to process aliases */
143 static unsigned msgflags = 0; /* what we've seen */
149 static char tmpfil[] = "/tmp/pstXXXXXX";
151 static char from[BUFSIZ]; /* my network address */
152 static char signature[BUFSIZ]; /* my signature */
153 static char *filter = NULL; /* the filter for BCC'ing */
154 static char *subject = NULL; /* the subject field for BCC'ing */
155 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
157 static struct headers *hdrtab; /* table for the message we're doing */
158 static FILE *out; /* output (temp) file */
160 extern char *sendmail;
163 ** external prototypes
165 extern char *getfullname(void);
166 extern char *getusername(void);
168 extern boolean draft_from_masquerading; /* defined in mts.c */
173 static void putfmt(char *, char *, FILE *);
174 static void start_headers(void);
175 static void finish_headers(FILE *);
176 static int get_header(char *, struct headers *);
177 static void putadr(char *, struct mailname *);
178 static int putone(char *, int, int);
179 static void insert_fcc(struct headers *, unsigned char *);
180 static void file(char *);
181 static void fcc(char *, char *);
184 static void die(char *, char *, ...);
185 static void make_bcc_file(void);
190 main(int argc, char **argv)
192 int state, i, pid, compnum;
193 char *cp, *msg = NULL, **argp, **arguments;
194 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
198 setlocale(LC_ALL, "");
200 invo_name = mhbasename(argv[0]);
202 /* foil search of user profile/context */
203 if (context_foil(NULL) == -1)
207 arguments = getarguments(invo_name, argc, argv, 0);
210 while ((cp = *argp++)) {
212 switch (smatch(++cp, switches)) {
214 ambigsw(cp, switches);
217 adios(NULL, "-%s unknown", cp);
220 snprintf(buf, sizeof(buf),
221 "%s [switches] file",
223 print_help(buf, switches, 1);
226 print_version(invo_name);
238 if (!(filter = *argp++) || *filter == '-')
239 adios(NULL, "missing argument to %s",
282 if (!(cp = *argp++) || *cp == '-')
283 adios(NULL, "missing argument to %s",
286 if ((state = alias(cp)) != AK_OK)
287 adios(NULL, "aliasing error in file %s - %s", cp, akerror(state) );
294 if (!(cp = *argp++) || *cp == '-')
295 adios(NULL, "missing argument to %s",
297 /* create a minimal context */
298 if (context_foil(cp) == -1)
304 adios(NULL, "only one message at a time!");
310 adios(NULL, "usage: %s [switches] file", invo_name);
312 if ((in = fopen(msg, "r")) == NULL)
313 adios(msg, "unable to open");
322 if ((out = fdopen( mkstemp(tmpfil), "w" )) == NULL )
323 adios(tmpfil, "unable to create");
326 if ((out = fopen(tmpfil, "w")) == NULL)
327 adios(tmpfil, "unable to create");
332 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
334 for (compnum = 1, state = FLD;;) {
335 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
338 putfmt(name, buf, out);
344 while (state == FLDPLUS) {
345 state = m_getfld(state, name, buf,
349 putfmt(name, cp, out);
355 fprintf(out, "\n%s", buf);
356 while (state == BODY) {
357 state = m_getfld(state, name, buf,
369 adios(NULL, "message format error in component #%d",
373 adios(NULL, "getfld() returned %d", state);
380 strncpy(buf, m_backup(msg), sizeof(buf));
381 if (rename(msg, buf) == NOTOK)
382 advise(buf, "unable to rename %s to", msg);
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 fprintf(out, "%s: %s", name, str);
451 if (hdr->flags & HIGN)
453 if (hdr->flags & HBAD) {
454 advise(NULL, "illegal header line -- %s:", name);
458 msgflags |= hdr->set;
460 if (hdr->flags & HSUB)
461 subject = subject ? add(str, add("\t", subject)) :
464 if (hdr->flags & HFCC) {
465 if ((cp = strrchr(str, '\n')))
467 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
476 if (hdr->flags & HBCC) {
482 if (*str != '\n' && *str != '\0') {
483 if (aliasflg && hdr->flags & HTRY) {
485 ** this header contains address(es) that we have to do
486 ** alias expansion on. Because of the saved state in
487 ** getname we have to put all the addresses into a
488 ** list. We then let putadr munch on that list,
489 ** possibly expanding aliases.
491 register struct mailname *f = 0;
492 register struct mailname *mp = 0;
494 while ((cp = getname(str))) {
495 mp = getm( cp, NULL, 0, AD_HOST, NULL);
500 mp->m_next = f->m_next;
505 f = mp->m_next; mp->m_next = 0;
509 ** The author(s) of spost decided that alias
510 ** substitution wasn't necessary for the non-HTRY
511 ** headers. Unfortunately, one of those headers
512 ** is "From:", and having alias substitution
513 ** work on that is extremely useful for someone
514 ** with a lot of POP3 email accounts or aliases.
515 ** post supports aliasing of "From:"...
517 ** Since "From:"-processing is incompletely
518 ** implemented in this unsupported and
519 ** undocumented spost backend, I'm not
520 ** going to take the time to implement my new
521 ** draft-From:-based email address masquerading.
522 ** If I do ever implement it here, I'd almost
523 ** certainly want to implement "From:" line
524 ** alias processing as well.
525 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
527 fprintf(out, "%s: %s", name, str );
539 strncpy(from, getusername(), sizeof(from));
541 if ((cp = getfullname()) && *cp) {
542 strncpy(sigbuf, cp, sizeof(sigbuf));
543 snprintf(signature, sizeof(signature), "%s <%s>",
546 snprintf(signature, sizeof(signature), "%s", from);
551 finish_headers(FILE *out)
555 if (!(msgflags & MDAT))
556 fprintf(out, "Date: %s\n", dtimenow(0));
558 if (msgflags & MFRM) {
560 ** There was already a From: in the draft.
563 if (!draft_from_masquerading)
565 ** mts.conf didn't contain
566 ** "masquerade:[...]draft_from[...]"
567 ** so we'll reveal the user's
568 ** actual account@thismachine
569 ** address in a Sender: header
570 ** (and use it as the envelope
573 fprintf(out, "Sender: %s\n", from);
575 fprintf(out, "From: %s\n", signature);
578 if (!(msgflags & MVIS))
579 fprintf(out, "Bcc: Blind Distribution List: ;\n");
584 if (!(msgflags & MRDT))
585 fprintf(out, "Resent-Date: %s\n", dtimenow(0));
586 if (msgflags & MRFM) {
588 ** There was already a Resent-From: in draft.
591 if (!draft_from_masquerading)
593 ** mts.conf didn't contain
594 ** "masquerade:[...]draft_from[...]"
595 ** so we'll reveal the user's
596 ** actual account@thismachine
597 ** address in a Sender: header
598 ** (and use it as the envelope
601 fprintf(out, "Resent-Sender: %s\n",
604 /* Construct a Resent-From: header. */
605 fprintf(out, "Resent-From: %s\n", signature);
607 if (!(msgflags & MVIS))
608 fprintf(out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
614 adios(NULL, "re-format message and try again");
619 get_header(char *header, struct headers *table)
623 for (h = table; h->value; h++)
624 if (!mh_strcasecmp(header, h->value))
632 ** output the address list for header "name". The address list
633 ** is a linked list of mailname structs. "nl" points to the head
634 ** of the list. Alias substitution should be done on nl.
637 putadr(char *name, struct mailname *nl)
639 register struct mailname *mp, *mp2;
640 register int linepos;
644 fprintf(out, "%s: ", name);
645 namelen = strlen(name) + 2;
648 for (mp = nl; mp; ) {
649 if (linepos > MAX_SM_FIELD) {
650 fprintf(out, "\n%s: ", name);
654 /* a local name - see if it's an alias */
655 cp = akvalue(mp->m_mbox);
656 if (cp == mp->m_mbox)
657 /* wasn't an alias - use what the user typed */
658 linepos = putone(mp->m_text, linepos, namelen);
660 /* an alias - expand it */
661 while ((cp = getname(cp))) {
662 if (linepos > MAX_SM_FIELD) {
663 fprintf(out, "\n%s: ", name);
666 mp2 = getm( cp, NULL, 0, AD_HOST, NULL);
668 mp2->m_pers = getcpy(mp->m_mbox);
669 linepos = putone( adrformat(mp2), linepos, namelen );
671 linepos = putone( mp2->m_text, linepos, namelen );
676 /* not a local name - use what the user typed */
677 linepos = putone( mp->m_text, linepos, namelen );
687 putone(char *adr, int pos, int indent)
695 else if (linepos+len > OUTPUTLINELEN) {
696 fprintf( out, ",\n%*s", indent, "");
712 insert_fcc(struct headers *hdr, unsigned char *pp)
716 for (cp = pp; isspace(*cp); cp++)
718 for (pp += strlen(pp) - 1; pp > cp && isspace(*pp); pp--)
726 adios(NULL, "too many %ss", hdr->value);
727 fccfold[fccind++] = getcpy(cp);
742 fd = mkstemp(bccfil);
743 if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
744 adios(bccfil, "unable to create");
747 if ((out = fopen(bccfil, "w")) == NULL)
748 adios(bccfil, "unable to create");
752 fprintf(out, "Date: %s\n", dtimenow(0));
753 if (msgflags & MFRM) {
754 /* There was already a From: in the draft. Don't add one. */
755 if (!draft_from_masquerading)
757 ** mts.conf didn't contain "masquerade:[...]draft_from[...]"
758 ** so we'll reveal the user's actual account@thismachine
759 ** address in a Sender: header (and use it as the envelope
762 fprintf(out, "Sender: %s\n", from);
764 /* Construct a From: header. */
765 fprintf(out, "From: %s\n", signature);
767 fprintf(out, "Subject: %s", subject);
768 fprintf(out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
771 if (filter == NULL) {
772 if ((fd = open(tmpfil, O_RDONLY)) == NOTOK)
773 adios(NULL, "unable to re-open");
774 cpydgst(fd, fileno(out), tmpfil, bccfil);
777 vec[0] = mhbasename(mhlproc);
779 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
783 adios("fork", "unable to");
786 dup2(fileno(out), 1);
789 vec[i++] = "-forward";
795 execvp(mhlproc, vec);
796 adios(mhlproc, "unable to exec");
799 if (status = pidwait(child_id, OK))
800 admonish(NULL, "%s lost (status=0%o)",
806 fseek(out, 0L, SEEK_END);
807 fprintf(out, "\n------- End of Blind-Carbon-Copy\n");
812 /* FCC INTERACTION */
822 for (i = 0; i < fccind; i++)
823 fcc(path, fccfold[i]);
828 fcc(char *file, char *folder)
835 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
839 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
844 fprintf(stderr, " %sFcc %s: ", msgstate == resent ?
845 "Resent-" : "", folder);
846 fprintf(verbose ? stdout : stderr, "no forks, so not ok\n");
850 snprintf(fold, sizeof(fold), "%s%s",
851 *folder == '+' || *folder == '@' ? "" : "+", folder);
852 execlp(fileproc, mhbasename(fileproc),
853 "-link", "-file", file, fold, NULL);
857 if ((status = pidwait(child_id, OK))) {
859 fprintf(stderr, " %sFcc %s: ",
861 "Resent-" : "", folder);
862 fprintf(verbose ? stdout : stderr, " errored (0%o)\n",
878 die(char *what, char *fmt, ...)
883 advertise(what, NULL, fmt, ap);