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 },
54 { "width columns", 0 },
62 { "dist", -4 }, /* interface from dist */
63 #define PUSHSW 19 /* fork to sendmail then exit */
65 #define NPUSHSW 20 /* exec sendmail */
68 { "library directory", -7 },
70 { "idanno number", -6 },
75 /* flags for headers->flags */
76 #define HNOP 0x0000 /* just used to keep .set around */
77 #define HBAD 0x0001 /* bad header - don't let it through */
78 #define HADR 0x0002 /* header has an address field */
79 #define HSUB 0x0004 /* Subject: header */
80 #define HTRY 0x0008 /* try to send to addrs on header */
81 #define HBCC 0x0010 /* don't output this header */
82 #define HMNG 0x0020 /* mung this header */
83 #define HNGR 0x0040 /* no groups allowed in this header */
84 #define HFCC 0x0080 /* FCC: type header */
85 #define HNIL 0x0100 /* okay for this header not to have addrs */
86 #define HIGN 0x0200 /* ignore this header */
88 /* flags for headers->set */
89 #define MFRM 0x0001 /* we've seen a From: */
90 #define MDAT 0x0002 /* we've seen a Date: */
91 #define MRFM 0x0004 /* we've seen a Resent-From: */
92 #define MVIS 0x0008 /* we've seen sighted addrs */
93 #define MINV 0x0010 /* we've seen blind addrs */
94 #define MRDT 0x0020 /* we've seen a Resent-Date: */
103 static struct headers NHeaders[] = {
104 { "Return-Path", HBAD, 0 },
105 { "Received", HBAD, 0 },
106 { "Reply-To", HADR|HNGR, 0 },
107 { "From", HADR|HNGR, MFRM },
108 { "Sender", HADR|HBAD, 0 },
109 { "Date", HNOP, MDAT },
110 { "Subject", HSUB, 0 },
111 { "To", HADR|HTRY, MVIS },
112 { "cc", HADR|HTRY, MVIS },
113 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
114 { "Message-Id", HBAD, 0 },
119 static struct headers RHeaders[] = {
120 { "Resent-Reply-To", HADR|HNGR, 0 },
121 { "Resent-From", HADR|HNGR, MRFM },
122 { "Resent-Sender", HADR|HBAD, 0 },
123 { "Resent-Date", HNOP, MRDT },
124 { "Resent-Subject", HSUB, 0 },
125 { "Resent-To", HADR|HTRY, MVIS },
126 { "Resent-cc", HADR|HTRY, MVIS },
127 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
128 { "Resent-Message-Id", HBAD, 0 },
129 { "Resent-Fcc", HFCC, 0 },
130 { "Reply-To", HADR, 0 },
136 static short fccind = 0; /* index into fccfold[] */
138 static int badmsg = 0; /* message has bad semantics */
139 static int verbose = 0; /* spell it out */
140 static int debug = 0; /* debugging post */
141 static int rmflg = 1; /* remove temporary file when done */
142 static int watch = 0; /* watch the delivery process */
143 static int backflg = 0; /* rename input file as *.bak when done */
144 static int pushflg = 0; /* if going to fork to sendmail */
145 static int aliasflg = -1; /* if going to process aliases */
146 static int outputlinelen=72;
148 static unsigned msgflags = 0; /* what we've seen */
154 static char tmpfil[] = "/tmp/pstXXXXXX";
156 static char from[BUFSIZ]; /* my network address */
157 static char signature[BUFSIZ]; /* my signature */
158 static char *filter = NULL; /* the filter for BCC'ing */
159 static char *subject = NULL; /* the subject field for BCC'ing */
160 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
162 static struct headers *hdrtab; /* table for the message we're doing */
163 static FILE *out; /* output (temp) file */
165 extern char *sendmail;
168 ** external prototypes
170 extern char *getfullname(void);
171 extern char *getusername(void);
173 extern boolean draft_from_masquerading; /* defined in mts.c */
178 static void putfmt(char *, char *, FILE *);
179 static void start_headers(void);
180 static void finish_headers(FILE *);
181 static int get_header(char *, struct headers *);
182 static void putadr(char *, struct mailname *);
183 static int putone(char *, int, int);
184 static void insert_fcc(struct headers *, unsigned char *);
185 static void file(char *);
186 static void fcc(char *, char *);
189 static void die(char *, char *, ...);
190 static void make_bcc_file(void);
195 main(int argc, char **argv)
197 int state, i, pid, compnum;
198 char *cp, *msg = NULL, **argp, **arguments;
199 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
203 setlocale(LC_ALL, "");
205 invo_name = mhbasename(argv[0]);
207 /* foil search of user profile/context */
208 if (context_foil(NULL) == -1)
212 arguments = getarguments(invo_name, argc, argv, 0);
215 while ((cp = *argp++)) {
217 switch (smatch(++cp, switches)) {
219 ambigsw(cp, switches);
222 adios(NULL, "-%s unknown", cp);
225 snprintf(buf, sizeof(buf), "%s [switches] file", invo_name);
226 print_help(buf, switches, 1);
229 print_version(invo_name);
241 if (!(filter = *argp++) ||
243 adios(NULL, "missing argument to %s", argp[-2]);
285 if (!(cp = *argp++) || *cp == '-')
286 adios(NULL, "missing argument to %s", argp[-2]);
288 /* load default aka's */
292 if ((state = alias(cp)) != AK_OK)
293 adios(NULL, "aliasing error in file %s - %s", cp, akerror(state) );
300 if (!(cp = *argp++) || *cp == '-')
301 adios(NULL, "missing argument to %s", argp[-2]);
302 outputlinelen = atoi(cp);
303 if (outputlinelen <= 10)
308 if (!(cp = *argp++) || *cp == '-')
309 adios(NULL, "missing argument to %s", argp[-2]);
310 /* create a minimal context */
311 if (context_foil(cp) == -1)
316 /* -idanno switch ignored */
317 if (!(cp = *argp++) || *cp == '-')
318 adios(NULL, "missing argument to %s", argp[-2]);
323 adios(NULL, "only one message at a time!");
329 alias(AliasFile); /* load default aka's */
332 adios(NULL, "usage: %s [switches] file", invo_name);
334 if ((in = fopen(msg, "r")) == NULL)
335 adios(msg, "unable to open");
344 if ((out = fdopen( mkstemp(tmpfil), "w" )) == NULL )
345 adios(tmpfil, "unable to create");
348 if ((out = fopen(tmpfil, "w")) == NULL)
349 adios(tmpfil, "unable to create");
354 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
356 for (compnum = 1, state = FLD;;) {
357 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
360 putfmt(name, buf, out);
366 while (state == FLDPLUS) {
367 state = m_getfld(state, name, buf,
371 putfmt(name, cp, out);
377 fprintf(out, "\n%s", buf);
378 while (state == BODY) {
379 state = m_getfld(state, name, buf,
391 adios(NULL, "message format error in component #%d", compnum);
394 adios(NULL, "getfld() returned %d", state);
401 strncpy(buf, m_backup(msg), sizeof(buf));
402 if (rename(msg, buf) == NOTOK)
403 advise(buf, "unable to rename %s to", msg);
415 ** re-open the temp file, unlink it and exec sendmail, giving it
416 ** the msg temp file as std in.
418 if (freopen(tmpfil, "r", stdin) == NULL)
419 adios(tmpfil, "can't reopen for sendmail");
424 *argp++ = "send-mail";
425 *argp++ = "-m"; /* send to me too */
426 *argp++ = "-t"; /* read msg for recipients */
427 *argp++ = "-i"; /* don't stop on "." */
428 if (watch || verbose)
432 if (pushflg && !(watch || verbose)) {
433 /* fork to a child to run sendmail */
434 for (i=0; (pid = vfork()) == NOTOK && i < 5; i++)
438 fprintf(verbose ? stdout : stderr,
439 "%s: can't fork to %s\n",
440 invo_name, sendmail);
443 /* we're the child .. */
449 execv(sendmail, sargv);
450 adios(sendmail, "can't exec");
451 return 0; /* dead code to satisfy the compiler */
454 /* DRAFT GENERATION */
457 putfmt(char *name, char *str, FILE *out)
463 while (*str == ' ' || *str == '\t')
466 if ((i = get_header(name, hdrtab)) == NOTOK) {
467 fprintf(out, "%s: %s", name, str);
472 if (hdr->flags & HIGN)
474 if (hdr->flags & HBAD) {
475 advise(NULL, "illegal header line -- %s:", name);
479 msgflags |= hdr->set;
481 if (hdr->flags & HSUB)
482 subject = subject ? add(str, add("\t", subject)) :
485 if (hdr->flags & HFCC) {
486 if ((cp = strrchr(str, '\n')))
488 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
497 if (hdr->flags & HBCC) {
503 if (*str != '\n' && *str != '\0') {
504 if (aliasflg && hdr->flags & HTRY) {
506 ** this header contains address(es) that we have to do
507 ** alias expansion on. Because of the saved state in
508 ** getname we have to put all the addresses into a
509 ** list. We then let putadr munch on that list,
510 ** possibly expanding aliases.
512 register struct mailname *f = 0;
513 register struct mailname *mp = 0;
515 while ((cp = getname(str))) {
516 mp = getm( cp, NULL, 0, AD_HOST, NULL);
521 mp->m_next = f->m_next;
526 f = mp->m_next; mp->m_next = 0;
530 ** The author(s) of spost decided that alias
531 ** substitution wasn't necessary for the non-HTRY
532 ** headers. Unfortunately, one of those headers
533 ** is "From:", and having alias substitution
534 ** work on that is extremely useful for someone
535 ** with a lot of POP3 email accounts or aliases.
536 ** post supports aliasing of "From:"...
538 ** Since "From:"-processing is incompletely
539 ** implemented in this unsupported and
540 ** undocumented spost backend, I'm not
541 ** going to take the time to implement my new
542 ** draft-From:-based email address masquerading.
543 ** If I do ever implement it here, I'd almost
544 ** certainly want to implement "From:" line
545 ** alias processing as well.
546 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
548 fprintf(out, "%s: %s", name, str );
560 strncpy(from, getusername(), sizeof(from));
562 if ((cp = getfullname()) && *cp) {
563 strncpy(sigbuf, cp, sizeof(sigbuf));
564 snprintf(signature, sizeof(signature), "%s <%s>",
567 snprintf(signature, sizeof(signature), "%s", from);
572 finish_headers(FILE *out)
576 if (!(msgflags & MDAT))
577 fprintf(out, "Date: %s\n", dtimenow(0));
579 if (msgflags & MFRM) {
581 ** There was already a From: in the draft.
584 if (!draft_from_masquerading)
586 ** mts.conf didn't contain
587 ** "masquerade:[...]draft_from[...]"
588 ** so we'll reveal the user's
589 ** actual account@thismachine
590 ** address in a Sender: header
591 ** (and use it as the envelope
594 fprintf(out, "Sender: %s\n", from);
596 fprintf(out, "From: %s\n", signature);
599 if (!(msgflags & MVIS))
600 fprintf(out, "Bcc: Blind Distribution List: ;\n");
605 if (!(msgflags & MRDT))
606 fprintf(out, "Resent-Date: %s\n", dtimenow(0));
607 if (msgflags & MRFM) {
609 ** There was already a Resent-From: in draft.
612 if (!draft_from_masquerading)
614 ** mts.conf didn't contain
615 ** "masquerade:[...]draft_from[...]"
616 ** so we'll reveal the user's
617 ** actual account@thismachine
618 ** address in a Sender: header
619 ** (and use it as the envelope
622 fprintf(out, "Resent-Sender: %s\n",
625 /* Construct a Resent-From: header. */
626 fprintf(out, "Resent-From: %s\n", signature);
628 if (!(msgflags & MVIS))
629 fprintf(out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
635 adios(NULL, "re-format message and try again");
640 get_header(char *header, struct headers *table)
644 for (h = table; h->value; h++)
645 if (!mh_strcasecmp(header, h->value))
653 ** output the address list for header "name". The address list
654 ** is a linked list of mailname structs. "nl" points to the head
655 ** of the list. Alias substitution should be done on nl.
658 putadr(char *name, struct mailname *nl)
660 register struct mailname *mp, *mp2;
661 register int linepos;
665 fprintf(out, "%s: ", name);
666 namelen = strlen(name) + 2;
669 for (mp = nl; mp; ) {
670 if (linepos > MAX_SM_FIELD) {
671 fprintf(out, "\n%s: ", name);
675 /* a local name - see if it's an alias */
676 cp = akvalue(mp->m_mbox);
677 if (cp == mp->m_mbox)
678 /* wasn't an alias - use what the user typed */
679 linepos = putone(mp->m_text, linepos, namelen);
681 /* an alias - expand it */
682 while ((cp = getname(cp))) {
683 if (linepos > MAX_SM_FIELD) {
684 fprintf(out, "\n%s: ", name);
687 mp2 = getm( cp, NULL, 0, AD_HOST, NULL);
689 mp2->m_pers = getcpy(mp->m_mbox);
690 linepos = putone( adrformat(mp2), linepos, namelen );
692 linepos = putone( mp2->m_text, linepos, namelen );
697 /* not a local name - use what the user typed */
698 linepos = putone( mp->m_text, linepos, namelen );
708 putone(char *adr, int pos, int indent)
716 else if (linepos+len > outputlinelen) {
717 fprintf( out, ",\n%*s", indent, "");
733 insert_fcc(struct headers *hdr, unsigned char *pp)
737 for (cp = pp; isspace(*cp); cp++)
739 for (pp += strlen(pp) - 1; pp > cp && isspace(*pp); pp--)
747 adios(NULL, "too many %ss", hdr->value);
748 fccfold[fccind++] = getcpy(cp);
763 fd = mkstemp(bccfil);
764 if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
765 adios(bccfil, "unable to create");
768 if ((out = fopen(bccfil, "w")) == NULL)
769 adios(bccfil, "unable to create");
773 fprintf(out, "Date: %s\n", dtimenow(0));
774 if (msgflags & MFRM) {
775 /* There was already a From: in the draft. Don't add one. */
776 if (!draft_from_masquerading)
778 ** mts.conf didn't contain "masquerade:[...]draft_from[...]"
779 ** so we'll reveal the user's actual account@thismachine
780 ** address in a Sender: header (and use it as the envelope
783 fprintf(out, "Sender: %s\n", from);
785 /* Construct a From: header. */
786 fprintf(out, "From: %s\n", signature);
788 fprintf(out, "Subject: %s", subject);
789 fprintf(out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
792 if (filter == NULL) {
793 if ((fd = open(tmpfil, O_RDONLY)) == NOTOK)
794 adios(NULL, "unable to re-open");
795 cpydgst(fd, fileno(out), tmpfil, bccfil);
798 vec[0] = mhbasename(mhlproc);
800 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
804 adios("vfork", "unable to");
807 dup2(fileno(out), 1);
810 vec[i++] = "-forward";
816 execvp(mhlproc, vec);
817 adios(mhlproc, "unable to exec");
820 if (status = pidwait(child_id, OK))
821 admonish(NULL, "%s lost (status=0%o)",
827 fseek(out, 0L, SEEK_END);
828 fprintf(out, "\n------- End of Blind-Carbon-Copy\n");
833 /* FCC INTERACTION */
843 for (i = 0; i < fccind; i++)
844 fcc(path, fccfold[i]);
849 fcc(char *file, char *folder)
856 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
860 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
865 fprintf(stderr, " %sFcc %s: ",
867 "Resent-" : "", folder);
868 fprintf(verbose ? stdout : stderr, "no forks, so not ok\n");
872 snprintf(fold, sizeof(fold), "%s%s",
873 *folder == '+' || *folder == '@' ? "" : "+", folder);
874 execlp(fileproc, mhbasename(fileproc),
875 "-link", "-file", file, fold, NULL);
879 if ((status = pidwait(child_id, OK))) {
881 fprintf(stderr, " %sFcc %s: ",
885 fprintf(verbose ? stdout : stderr,
886 " errored (0%o)\n", status);
901 die(char *what, char *fmt, ...)
906 advertise(what, NULL, fmt, ap);