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 = 0; /* 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 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", argp[-2]);
298 outputlinelen = atoi(cp);
299 if (outputlinelen <= 10)
304 if (!(cp = *argp++) || *cp == '-')
305 adios(NULL, "missing argument to %s", argp[-2]);
306 /* create a minimal context */
307 if (context_foil(cp) == -1)
312 /* -idanno switch ignored */
313 if (!(cp = *argp++) || *cp == '-')
314 adios(NULL, "missing argument to %s", argp[-2]);
319 adios(NULL, "only one message at a time!");
325 adios(NULL, "usage: %s [switches] file", invo_name);
327 if ((in = fopen(msg, "r")) == NULL)
328 adios(msg, "unable to open");
337 if ((out = fdopen( mkstemp(tmpfil), "w" )) == NULL )
338 adios(tmpfil, "unable to create");
341 if ((out = fopen(tmpfil, "w")) == NULL)
342 adios(tmpfil, "unable to create");
347 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
349 for (compnum = 1, state = FLD;;) {
350 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
353 putfmt(name, buf, out);
359 while (state == FLDPLUS) {
360 state = m_getfld(state, name, buf,
364 putfmt(name, cp, out);
370 fprintf(out, "\n%s", buf);
371 while (state == BODY) {
372 state = m_getfld(state, name, buf,
384 adios(NULL, "message format error in component #%d", compnum);
387 adios(NULL, "getfld() returned %d", state);
394 strncpy(buf, m_backup(msg), sizeof(buf));
395 if (rename(msg, buf) == NOTOK)
396 advise(buf, "unable to rename %s to", msg);
408 ** re-open the temp file, unlink it and exec sendmail, giving it
409 ** the msg temp file as std in.
411 if (freopen(tmpfil, "r", stdin) == NULL)
412 adios(tmpfil, "can't reopen for sendmail");
417 *argp++ = "send-mail";
418 *argp++ = "-m"; /* send to me too */
419 *argp++ = "-t"; /* read msg for recipients */
420 *argp++ = "-i"; /* don't stop on "." */
421 if (watch || verbose)
425 if (pushflg && !(watch || verbose)) {
426 /* fork to a child to run sendmail */
427 for (i=0; (pid = vfork()) == NOTOK && i < 5; i++)
431 fprintf(verbose ? stdout : stderr,
432 "%s: can't fork to %s\n",
433 invo_name, sendmail);
436 /* we're the child .. */
442 execv(sendmail, sargv);
443 adios(sendmail, "can't exec");
444 return 0; /* dead code to satisfy the compiler */
447 /* DRAFT GENERATION */
450 putfmt(char *name, char *str, FILE *out)
456 while (*str == ' ' || *str == '\t')
459 if ((i = get_header(name, hdrtab)) == NOTOK) {
460 fprintf(out, "%s: %s", name, str);
465 if (hdr->flags & HIGN)
467 if (hdr->flags & HBAD) {
468 advise(NULL, "illegal header line -- %s:", name);
472 msgflags |= hdr->set;
474 if (hdr->flags & HSUB)
475 subject = subject ? add(str, add("\t", subject)) :
478 if (hdr->flags & HFCC) {
479 if ((cp = strrchr(str, '\n')))
481 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
490 if (hdr->flags & HBCC) {
496 if (*str != '\n' && *str != '\0') {
497 if (aliasflg && hdr->flags & HTRY) {
499 ** this header contains address(es) that we have to do
500 ** alias expansion on. Because of the saved state in
501 ** getname we have to put all the addresses into a
502 ** list. We then let putadr munch on that list,
503 ** possibly expanding aliases.
505 register struct mailname *f = 0;
506 register struct mailname *mp = 0;
508 while ((cp = getname(str))) {
509 mp = getm( cp, NULL, 0, AD_HOST, NULL);
514 mp->m_next = f->m_next;
519 f = mp->m_next; mp->m_next = 0;
523 ** The author(s) of spost decided that alias
524 ** substitution wasn't necessary for the non-HTRY
525 ** headers. Unfortunately, one of those headers
526 ** is "From:", and having alias substitution
527 ** work on that is extremely useful for someone
528 ** with a lot of POP3 email accounts or aliases.
529 ** post supports aliasing of "From:"...
531 ** Since "From:"-processing is incompletely
532 ** implemented in this unsupported and
533 ** undocumented spost backend, I'm not
534 ** going to take the time to implement my new
535 ** draft-From:-based email address masquerading.
536 ** If I do ever implement it here, I'd almost
537 ** certainly want to implement "From:" line
538 ** alias processing as well.
539 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
541 fprintf(out, "%s: %s", name, str );
553 strncpy(from, getusername(), sizeof(from));
555 if ((cp = getfullname()) && *cp) {
556 strncpy(sigbuf, cp, sizeof(sigbuf));
557 snprintf(signature, sizeof(signature), "%s <%s>",
560 snprintf(signature, sizeof(signature), "%s", from);
565 finish_headers(FILE *out)
569 if (!(msgflags & MDAT))
570 fprintf(out, "Date: %s\n", dtimenow(0));
572 if (msgflags & MFRM) {
574 ** There was already a From: in the draft.
577 if (!draft_from_masquerading)
579 ** mts.conf didn't contain
580 ** "masquerade:[...]draft_from[...]"
581 ** so we'll reveal the user's
582 ** actual account@thismachine
583 ** address in a Sender: header
584 ** (and use it as the envelope
587 fprintf(out, "Sender: %s\n", from);
589 fprintf(out, "From: %s\n", signature);
592 if (!(msgflags & MVIS))
593 fprintf(out, "Bcc: Blind Distribution List: ;\n");
598 if (!(msgflags & MRDT))
599 fprintf(out, "Resent-Date: %s\n", dtimenow(0));
600 if (msgflags & MRFM) {
602 ** There was already a Resent-From: in draft.
605 if (!draft_from_masquerading)
607 ** mts.conf didn't contain
608 ** "masquerade:[...]draft_from[...]"
609 ** so we'll reveal the user's
610 ** actual account@thismachine
611 ** address in a Sender: header
612 ** (and use it as the envelope
615 fprintf(out, "Resent-Sender: %s\n",
618 /* Construct a Resent-From: header. */
619 fprintf(out, "Resent-From: %s\n", signature);
621 if (!(msgflags & MVIS))
622 fprintf(out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
628 adios(NULL, "re-format message and try again");
633 get_header(char *header, struct headers *table)
637 for (h = table; h->value; h++)
638 if (!mh_strcasecmp(header, h->value))
646 ** output the address list for header "name". The address list
647 ** is a linked list of mailname structs. "nl" points to the head
648 ** of the list. Alias substitution should be done on nl.
651 putadr(char *name, struct mailname *nl)
653 register struct mailname *mp, *mp2;
654 register int linepos;
658 fprintf(out, "%s: ", name);
659 namelen = strlen(name) + 2;
662 for (mp = nl; mp; ) {
663 if (linepos > MAX_SM_FIELD) {
664 fprintf(out, "\n%s: ", name);
668 /* a local name - see if it's an alias */
669 cp = akvalue(mp->m_mbox);
670 if (cp == mp->m_mbox)
671 /* wasn't an alias - use what the user typed */
672 linepos = putone(mp->m_text, linepos, namelen);
674 /* an alias - expand it */
675 while ((cp = getname(cp))) {
676 if (linepos > MAX_SM_FIELD) {
677 fprintf(out, "\n%s: ", name);
680 mp2 = getm( cp, NULL, 0, AD_HOST, NULL);
682 mp2->m_pers = getcpy(mp->m_mbox);
683 linepos = putone( adrformat(mp2), linepos, namelen );
685 linepos = putone( mp2->m_text, linepos, namelen );
690 /* not a local name - use what the user typed */
691 linepos = putone( mp->m_text, linepos, namelen );
701 putone(char *adr, int pos, int indent)
709 else if (linepos+len > outputlinelen) {
710 fprintf( out, ",\n%*s", indent, "");
726 insert_fcc(struct headers *hdr, unsigned char *pp)
730 for (cp = pp; isspace(*cp); cp++)
732 for (pp += strlen(pp) - 1; pp > cp && isspace(*pp); pp--)
740 adios(NULL, "too many %ss", hdr->value);
741 fccfold[fccind++] = getcpy(cp);
756 fd = mkstemp(bccfil);
757 if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
758 adios(bccfil, "unable to create");
761 if ((out = fopen(bccfil, "w")) == NULL)
762 adios(bccfil, "unable to create");
766 fprintf(out, "Date: %s\n", dtimenow(0));
767 if (msgflags & MFRM) {
768 /* There was already a From: in the draft. Don't add one. */
769 if (!draft_from_masquerading)
771 ** mts.conf didn't contain "masquerade:[...]draft_from[...]"
772 ** so we'll reveal the user's actual account@thismachine
773 ** address in a Sender: header (and use it as the envelope
776 fprintf(out, "Sender: %s\n", from);
778 /* Construct a From: header. */
779 fprintf(out, "From: %s\n", signature);
781 fprintf(out, "Subject: %s", subject);
782 fprintf(out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
785 if (filter == NULL) {
786 if ((fd = open(tmpfil, O_RDONLY)) == NOTOK)
787 adios(NULL, "unable to re-open");
788 cpydgst(fd, fileno(out), tmpfil, bccfil);
791 vec[0] = mhbasename(mhlproc);
793 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
797 adios("vfork", "unable to");
800 dup2(fileno(out), 1);
803 vec[i++] = "-forward";
809 execvp(mhlproc, vec);
810 adios(mhlproc, "unable to exec");
813 if (status = pidwait(child_id, OK))
814 admonish(NULL, "%s lost (status=0%o)",
820 fseek(out, 0L, SEEK_END);
821 fprintf(out, "\n------- End of Blind-Carbon-Copy\n");
826 /* FCC INTERACTION */
836 for (i = 0; i < fccind; i++)
837 fcc(path, fccfold[i]);
842 fcc(char *file, char *folder)
849 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
853 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
858 fprintf(stderr, " %sFcc %s: ",
860 "Resent-" : "", folder);
861 fprintf(verbose ? stdout : stderr, "no forks, so not ok\n");
865 snprintf(fold, sizeof(fold), "%s%s",
866 *folder == '+' || *folder == '@' ? "" : "+", folder);
867 execlp(fileproc, mhbasename(fileproc),
868 "-link", "-file", file, fold, NULL);
872 if ((status = pidwait(child_id, OK))) {
874 fprintf(stderr, " %sFcc %s: ",
878 fprintf(verbose ? stdout : stderr,
879 " errored (0%o)\n", status);
894 die(char *what, char *fmt, ...)
899 advertise(what, NULL, fmt, ap);