3 * spost.c -- feed messages to sendmail
5 * This is a simpler, faster, replacement for "post" for use
6 * when "sendmail" is the transport system.
8 * This code is Copyright (c) 2002, by the authors of nmh. See the
9 * COPYRIGHT file in the root directory of the nmh distribution for
10 * complete copyright information.
15 #include <h/addrsbr.h>
16 #include <h/aliasbr.h>
17 #include <h/dropsbr.h>
22 #define MAX_SM_FIELD 1476 /* < largest hdr field sendmail will accept */
23 #define FCCS 10 /* max number of fccs allowed */
25 struct swit switches[] = {
27 { "filter filterfile", 0 },
47 { "alias aliasfile", 0 },
51 { "width columns", 0 },
59 { "dist", -4 }, /* interface from dist */
61 { "whom", -4 }, /* interface from whom */
62 #define PUSHSW 18 /* fork to sendmail then exit */
64 #define NPUSHSW 19 /* exec sendmail */
67 { "library directory", -7 },
69 { "idanno number", -6 },
74 /* flags for headers->flags */
75 #define HNOP 0x0000 /* just used to keep .set around */
76 #define HBAD 0x0001 /* bad header - don't let it through */
77 #define HADR 0x0002 /* header has an address field */
78 #define HSUB 0x0004 /* Subject: header */
79 #define HTRY 0x0008 /* try to send to addrs on header */
80 #define HBCC 0x0010 /* don't output this header */
81 /* #define HMNG 0x0020 */ /* munge this header */
82 #define HNGR 0x0040 /* no groups allowed in this header */
83 #define HFCC 0x0080 /* FCC: type header */
84 #define HNIL 0x0100 /* okay for this header not to have addrs */
85 #define HIGN 0x0200 /* ignore this header */
87 /* flags for headers->set */
88 #define MFRM 0x0001 /* we've seen a From: */
89 #define MDAT 0x0002 /* we've seen a Date: */
90 #define MRFM 0x0004 /* we've seen a Resent-From: */
91 #define MVIS 0x0008 /* we've seen sighted addrs */
92 #define MINV 0x0010 /* we've seen blind addrs */
93 #define MRDT 0x0020 /* we've seen a Resent-Date: */
102 static struct headers NHeaders[] = {
103 { "Return-Path", HBAD, 0 },
104 { "Received", HBAD, 0 },
105 { "Reply-To", HADR|HNGR, 0 },
106 { "From", HADR|HNGR, MFRM },
107 { "Sender", HADR|HBAD, 0 },
108 { "Date", HNOP, MDAT },
109 { "Subject", HSUB, 0 },
110 { "To", HADR|HTRY, MVIS },
111 { "cc", HADR|HTRY, MVIS },
112 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
113 { "Message-Id", HBAD, 0 },
118 static struct headers RHeaders[] = {
119 { "Resent-Reply-To", HADR|HNGR, 0 },
120 { "Resent-From", HADR|HNGR, MRFM },
121 { "Resent-Sender", HADR|HBAD, 0 },
122 { "Resent-Date", HNOP, MRDT },
123 { "Resent-Subject", HSUB, 0 },
124 { "Resent-To", HADR|HTRY, MVIS },
125 { "Resent-cc", HADR|HTRY, MVIS },
126 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
127 { "Resent-Message-Id", HBAD, 0 },
128 { "Resent-Fcc", HFCC, 0 },
129 { "Reply-To", HADR, 0 },
135 static short fccind = 0; /* index into fccfold[] */
137 static int badmsg = 0; /* message has bad semantics */
138 static int verbose = 0; /* spell it out */
139 static int debug = 0; /* debugging post */
140 static int rmflg = 1; /* remove temporary file when done */
141 static int watch = 0; /* watch the delivery process */
142 static int backflg = 0; /* rename input file as *.bak when done */
143 static int whomflg = 0; /* if just checking addresses */
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 = r1bindex (argv[0], '/');
207 /* foil search of user profile/context */
208 if (context_foil (NULL) == -1)
211 mts_init (invo_name);
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);
245 if (!(filter = *argp++) || *filter == '-')
246 adios (NULL, "missing argument to %s", argp[-2]);
288 if (!(cp = *argp++) || *cp == '-')
289 adios (NULL, "missing argument to %s", argp[-2]);
291 alias (AliasFile);/* load default aka's */
293 if ((state = alias(cp)) != AK_OK)
294 adios (NULL, "aliasing error in file %s - %s",
295 cp, akerror(state) );
302 if (!(cp = *argp++) || *cp == '-')
303 adios (NULL, "missing argument to %s", argp[-2]);
304 outputlinelen = atoi (cp);
305 if (outputlinelen <= 10)
310 if (!(cp = *argp++) || *cp == '-')
311 adios (NULL, "missing argument to %s", argp[-2]);
312 /* create a minimal context */
313 if (context_foil (cp) == -1)
318 /* -idanno switch ignored */
319 if (!(cp = *argp++) || *cp == '-')
320 adios (NULL, "missing argument to %s", argp[-2]);
325 adios (NULL, "only one message at a time!");
331 alias (AliasFile); /* load default aka's */
334 adios (NULL, "usage: %s [switches] file", invo_name);
336 if ((in = fopen (msg, "r")) == NULL)
337 adios (msg, "unable to open");
345 if ((out = fdopen( mkstemp (tmpfil), "w" )) == NULL )
346 adios (tmpfil, "unable to create");
349 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
351 for (compnum = 1, state = FLD;;) {
352 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
355 putfmt (name, buf, out);
361 while (state == FLDPLUS) {
362 state = m_getfld (state, name, buf, sizeof(buf), in);
365 putfmt (name, cp, out);
370 finish_headers (out);
371 fprintf (out, "\n%s", buf);
373 while (state == BODY) {
374 state = m_getfld (state, name, buf, sizeof(buf), in);
380 finish_headers (out);
385 adios (NULL, "message format error in component #%d",
389 adios (NULL, "getfld() returned %d", state);
395 if (backflg && !whomflg) {
396 strncpy (buf, m_backup (msg), sizeof(buf));
397 if (rename (msg, buf) == NOTOK)
398 advise (buf, "unable to rename %s to", msg);
410 * re-open the temp file, unlink it and exec sendmail, giving it
411 * the msg temp file as std in.
413 if ( freopen( tmpfil, "r", stdin) == NULL)
414 adios (tmpfil, "can't reopen for sendmail");
419 *argp++ = "send-mail";
420 *argp++ = "-m"; /* send to me too */
421 *argp++ = "-t"; /* read msg for recipients */
422 *argp++ = "-i"; /* don't stop on "." */
425 if (watch || verbose)
429 if (pushflg && !(watch || verbose)) {
430 /* fork to a child to run sendmail */
431 for (i=0; (pid = vfork()) == NOTOK && i < 5; i++)
435 fprintf (verbose ? stdout : stderr, "%s: can't fork to %s\n",
436 invo_name, sendmail);
439 /* we're the child .. */
445 execv ( sendmail, sargv);
446 adios ( sendmail, "can't exec");
447 return 0; /* dead code to satisfy the compiler */
450 /* DRAFT GENERATION */
453 putfmt (char *name, char *str, FILE *out)
459 while (*str == ' ' || *str == '\t')
462 if ((i = get_header (name, hdrtab)) == NOTOK) {
463 fprintf (out, "%s: %s", name, str);
468 if (hdr->flags & HIGN)
470 if (hdr->flags & HBAD) {
471 advise (NULL, "illegal header line -- %s:", name);
475 msgflags |= hdr->set;
477 if (hdr->flags & HSUB)
478 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
480 if (hdr->flags & HFCC) {
481 if ((cp = strrchr(str, '\n')))
483 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
485 insert_fcc (hdr, pp);
487 insert_fcc (hdr, pp);
492 if (hdr->flags & HBCC) {
498 if (*str != '\n' && *str != '\0') {
499 if (aliasflg && hdr->flags & HTRY) {
500 /* this header contains address(es) that we have to do
501 * alias expansion on. Because of the saved state in
502 * getname we have to put all the addresses into a list.
503 * We then let putadr munch on that list, possibly
506 register struct mailname *f = 0;
507 register struct mailname *mp = 0;
509 while ((cp = getname(str))) {
510 mp = getm( cp, NULL, 0, AD_HOST, NULL);
515 mp->m_next = f->m_next;
520 f = mp->m_next; mp->m_next = 0;
523 /* The author(s) of spost decided that alias substitution wasn't
524 necessary for the non-HTRY headers. Unfortunately, one of those
525 headers is "From:", and having alias substitution work on that is
526 extremely useful for someone with a lot of POP3 email accounts or
527 aliases. post supports aliasing of "From:"...
529 Since "From:"-processing is incompletely implemented in this
530 unsupported and undocumented spost backend, I'm not going to take
531 the time to implement my new draft-From:-based email address
532 masquerading. If I do ever implement it here, I'd almost
533 certainly want to implement "From:" line alias processing as
534 well. -- Dan Harkless <dan-nmh@dilvish.speed.net> */
535 fprintf (out, "%s: %s", name, str );
547 strncpy(from, getusername(), sizeof(from));
549 if ((cp = getfullname ()) && *cp) {
550 strncpy (sigbuf, cp, sizeof(sigbuf));
551 snprintf (signature, sizeof(signature), "%s <%s>", sigbuf, from);
554 snprintf (signature, sizeof(signature), "%s", from);
559 finish_headers (FILE *out)
563 if (!(msgflags & MDAT))
564 fprintf (out, "Date: %s\n", dtimenow (0));
566 if (msgflags & MFRM) {
567 /* There was already a From: in the draft. Don't add one. */
568 if (!draft_from_masquerading)
569 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
570 so we'll reveal the user's actual account@thismachine
571 address in a Sender: header (and use it as the envelope
573 fprintf (out, "Sender: %s\n", from);
576 fprintf (out, "From: %s\n", signature);
579 if (!(msgflags & MVIS))
580 fprintf (out, "Bcc: Blind Distribution List: ;\n");
585 if (!(msgflags & MRDT))
586 fprintf (out, "Resent-Date: %s\n", dtimenow(0));
587 if (msgflags & MRFM) {
588 /* There was already a Resent-From: in draft. Don't add one. */
589 if (!draft_from_masquerading)
590 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
591 so we'll reveal the user's actual account@thismachine
592 address in a Sender: header (and use it as the envelope
594 fprintf (out, "Resent-Sender: %s\n", from);
597 /* Construct a Resent-From: header. */
598 fprintf (out, "Resent-From: %s\n", signature);
600 if (!(msgflags & MVIS))
601 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
607 adios (NULL, "re-format message and try again");
612 get_header (char *header, struct headers *table)
616 for (h = table; h->value; h++)
617 if (!mh_strcasecmp (header, h->value))
625 * output the address list for header "name". The address list
626 * is a linked list of mailname structs. "nl" points to the head
627 * of the list. Alias substitution should be done on nl.
630 putadr (char *name, struct mailname *nl)
632 register struct mailname *mp, *mp2;
633 register int linepos;
637 fprintf (out, "%s: ", name);
638 namelen = strlen(name) + 2;
641 for (mp = nl; mp; ) {
642 if (linepos > MAX_SM_FIELD) {
643 fprintf (out, "\n%s: ", name);
647 /* a local name - see if it's an alias */
648 cp = akvalue(mp->m_mbox);
649 if (cp == mp->m_mbox)
650 /* wasn't an alias - use what the user typed */
651 linepos = putone( mp->m_text, linepos, namelen );
653 /* an alias - expand it */
654 while ((cp = getname(cp))) {
655 if (linepos > MAX_SM_FIELD) {
656 fprintf (out, "\n%s: ", name);
659 mp2 = getm( cp, NULL, 0, AD_HOST, NULL);
661 mp2->m_pers = getcpy(mp->m_mbox);
662 linepos = putone( adrformat(mp2), linepos, namelen );
664 linepos = putone( mp2->m_text, linepos, namelen );
669 /* not a local name - use what the user typed */
670 linepos = putone( mp->m_text, linepos, namelen );
680 putone (char *adr, int pos, int indent)
688 else if ( linepos+len > outputlinelen ) {
689 fprintf ( out, ",\n%*s", indent, "");
706 insert_fcc (struct headers *hdr, unsigned char *pp)
710 for (cp = pp; isspace (*cp); cp++)
712 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
720 adios (NULL, "too many %ss", hdr->value);
721 fccfold[fccind++] = getcpy (cp);
735 fd = mkstemp(bccfil);
736 if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
737 adios (bccfil, "unable to create");
738 chmod (bccfil, 0600);
740 fprintf (out, "Date: %s\n", dtimenow (0));
741 if (msgflags & MFRM) {
742 /* There was already a From: in the draft. Don't add one. */
743 if (!draft_from_masquerading)
744 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
745 so we'll reveal the user's actual account@thismachine
746 address in a Sender: header (and use it as the envelope
748 fprintf (out, "Sender: %s\n", from);
751 /* Construct a From: header. */
752 fprintf (out, "From: %s\n", signature);
754 fprintf (out, "Subject: %s", subject);
755 fprintf (out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
758 if (filter == NULL) {
759 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
760 adios (NULL, "unable to re-open");
761 cpydgst (fd, fileno (out), tmpfil, bccfil);
765 vec[0] = r1bindex (mhlproc, '/');
767 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
771 adios ("vfork", "unable to");
774 dup2 (fileno (out), 1);
777 vec[i++] = "-forward";
783 execvp (mhlproc, vec);
784 adios (mhlproc, "unable to exec");
787 if (status = pidwait(child_id, OK))
788 admonish (NULL, "%s lost (status=0%o)", vec[0], status);
793 fseek (out, 0L, SEEK_END);
794 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
799 /* FCC INTERACTION */
809 for (i = 0; i < fccind; i++)
811 printf ("Fcc: %s\n", fccfold[i]);
813 fcc (path, fccfold[i]);
818 fcc (char *file, char *folder)
825 printf ("%sFcc: %s\n", msgstate == resent ? "Resent-" : "", folder);
828 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
833 fprintf (stderr, " %sFcc %s: ",
834 msgstate == resent ? "Resent-" : "", folder);
835 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
839 snprintf (fold, sizeof(fold), "%s%s",
840 *folder == '+' || *folder == '@' ? "" : "+", folder);
841 execlp (fileproc, r1bindex (fileproc, '/'),
842 "-link", "-file", file, fold, NULL);
846 if ((status = pidwait(child_id, OK))) {
848 fprintf (stderr, " %sFcc %s: ",
849 msgstate == resent ? "Resent-" : "", folder);
850 fprintf (verbose ? stdout : stderr,
851 " errored (0%o)\n", status);
866 die (char *what, char *fmt, ...)
871 advertise (what, NULL, fmt, ap);