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.
13 #include <h/addrsbr.h>
14 #include <h/aliasbr.h>
15 #include <h/dropsbr.h>
16 #include <zotnet/tws/tws.h>
17 #include <zotnet/mts/mts.h>
19 #define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : c)
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 */
64 { "check", -5 }, /* interface from whom */
66 { "nocheck", -7 }, /* interface from whom */
68 { "whom", -4 }, /* interface from whom */
69 #define PUSHSW 22 /* fork to sendmail then exit */
71 #define NPUSHSW 23 /* exec sendmail */
74 { "library directory", -7 },
76 { "idanno number", -6 },
81 /* flags for headers->flags */
82 #define HNOP 0x0000 /* just used to keep .set around */
83 #define HBAD 0x0001 /* bad header - don't let it through */
84 #define HADR 0x0002 /* header has an address field */
85 #define HSUB 0x0004 /* Subject: header */
86 #define HTRY 0x0008 /* try to send to addrs on header */
87 #define HBCC 0x0010 /* don't output this header */
88 #define HMNG 0x0020 /* mung this header */
89 #define HNGR 0x0040 /* no groups allowed in this header */
90 #define HFCC 0x0080 /* FCC: type header */
91 #define HNIL 0x0100 /* okay for this header not to have addrs */
92 #define HIGN 0x0200 /* ignore this header */
94 /* flags for headers->set */
95 #define MFRM 0x0001 /* we've seen a From: */
96 #define MDAT 0x0002 /* we've seen a Date: */
97 #define MRFM 0x0004 /* we've seen a Resent-From: */
98 #define MVIS 0x0008 /* we've seen sighted addrs */
99 #define MINV 0x0010 /* we've seen blind addrs */
100 #define MRDT 0x0020 /* we've seen a Resent-Date: */
109 static struct headers NHeaders[] = {
110 { "Return-Path", HBAD, 0 },
111 { "Received", HBAD, 0 },
112 { "Reply-To", HADR|HNGR, 0 },
113 { "From", HADR|HNGR, MFRM },
114 { "Sender", HADR|HBAD, 0 },
115 { "Date", HNOP, MDAT },
116 { "Subject", HSUB, 0 },
117 { "To", HADR|HTRY, MVIS },
118 { "cc", HADR|HTRY, MVIS },
119 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
120 { "Message-Id", HBAD, 0 },
125 static struct headers RHeaders[] = {
126 { "Resent-Reply-To", HADR|HNGR, 0 },
127 { "Resent-From", HADR|HNGR, MRFM },
128 { "Resent-Sender", HADR|HBAD, 0 },
129 { "Resent-Date", HNOP, MRDT },
130 { "Resent-Subject", HSUB, 0 },
131 { "Resent-To", HADR|HTRY, MVIS },
132 { "Resent-cc", HADR|HTRY, MVIS },
133 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
134 { "Resent-Message-Id", HBAD, 0 },
135 { "Resent-Fcc", HFCC, 0 },
136 { "Reply-To", HADR, 0 },
142 static short fccind = 0; /* index into fccfold[] */
144 static int badmsg = 0; /* message has bad semantics */
145 static int verbose = 0; /* spell it out */
146 static int debug = 0; /* debugging post */
147 static int rmflg = 1; /* remove temporary file when done */
148 static int watch = 0; /* watch the delivery process */
149 static int backflg = 0; /* rename input file as *.bak when done */
150 static int whomflg = 0; /* if just checking addresses */
151 static int pushflg = 0; /* if going to fork to sendmail */
152 static int aliasflg = -1; /* if going to process aliases */
153 static int outputlinelen=72;
155 static unsigned msgflags = 0; /* what we've seen */
161 static char tmpfil[] = "/tmp/pstXXXXXX";
163 static char from[BUFSIZ]; /* my network address */
164 static char signature[BUFSIZ]; /* my signature */
165 static char *filter = NULL; /* the filter for BCC'ing */
166 static char *subject = NULL; /* the subject field for BCC'ing */
167 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
169 static struct headers *hdrtab; /* table for the message we're doing */
170 static FILE *out; /* output (temp) file */
172 extern char *sendmail;
175 * external prototypes
177 extern char *getfullname (void);
178 extern char *getusername (void);
183 static void putfmt (char *, char *, FILE *);
184 static void start_headers (void);
185 static void finish_headers (FILE *);
186 static int get_header (char *, struct headers *);
187 static void putadr (char *, struct mailname *);
188 static int putone (char *, int, int);
189 static void insert_fcc (struct headers *, char *);
190 static void file (char *);
191 static void fcc (char *, char *);
194 static void die (char *, char *, ...);
195 static void make_bcc_file (void);
200 main (int argc, char **argv)
202 int state, i, pid, compnum;
203 char *cp, *msg = NULL, **argp, **arguments;
204 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
208 setlocale(LC_ALL, "");
210 invo_name = r1bindex (argv[0], '/');
212 /* foil search of user profile/context */
213 if (context_foil (NULL) == -1)
216 mts_init (invo_name);
217 arguments = getarguments (invo_name, argc, argv, 0);
220 while ((cp = *argp++)) {
222 switch (smatch (++cp, switches)) {
224 ambigsw (cp, switches);
227 adios (NULL, "-%s unknown", cp);
230 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
231 print_help (buf, switches, 1);
234 print_version(invo_name);
250 if (!(filter = *argp++) || *filter == '-')
251 adios (NULL, "missing argument to %s", argp[-2]);
293 if (!(cp = *argp++) || *cp == '-')
294 adios (NULL, "missing argument to %s", argp[-2]);
296 alias (AliasFile);/* load default aka's */
298 if ((state = alias(cp)) != AK_OK)
299 adios (NULL, "aliasing error in file %s - %s",
300 cp, akerror(state) );
307 if (!(cp = *argp++) || *cp == '-')
308 adios (NULL, "missing argument to %s", argp[-2]);
309 outputlinelen = atoi (cp);
310 if (outputlinelen <= 10)
315 if (!(cp = *argp++) || *cp == '-')
316 adios (NULL, "missing argument to %s", argp[-2]);
317 /* create a minimal context */
318 if (context_foil (cp) == -1)
323 /* -idanno switch ignored */
324 if (!(cp = *argp++) || *cp == '-')
325 adios (NULL, "missing argument to %s", argp[-2]);
330 adios (NULL, "only one message at a time!");
336 alias (AliasFile); /* load default aka's */
339 adios (NULL, "usage: %s [switches] file", invo_name);
341 if ((in = fopen (msg, "r")) == NULL)
342 adios (msg, "unable to open");
351 if ((out = fdopen( mkstemp (tmpfil), "w" )) == NULL )
352 adios (tmpfil, "unable to create");
355 if ((out = fopen (tmpfil, "w")) == NULL)
356 adios (tmpfil, "unable to create");
357 chmod (tmpfil, 0600);
361 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
363 for (compnum = 1, state = FLD;;) {
364 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
367 putfmt (name, buf, out);
373 while (state == FLDPLUS) {
374 state = m_getfld (state, name, buf, sizeof(buf), in);
377 putfmt (name, cp, out);
382 finish_headers (out);
383 fprintf (out, "\n%s", buf);
385 while (state == BODY) {
386 state = m_getfld (state, name, buf, sizeof(buf), in);
392 finish_headers (out);
397 adios (NULL, "message format error in component #%d",
401 adios (NULL, "getfld() returned %d", state);
407 if (backflg && !whomflg) {
408 strncpy (buf, m_backup (msg), sizeof(buf));
409 if (rename (msg, buf) == NOTOK)
410 advise (buf, "unable to rename %s to", msg);
422 * re-open the temp file, unlink it and exec sendmail, giving it
423 * the msg temp file as std in.
425 if ( freopen( tmpfil, "r", stdin) == NULL)
426 adios (tmpfil, "can't reopen for sendmail");
431 *argp++ = "send-mail";
432 *argp++ = "-m"; /* send to me too */
433 *argp++ = "-t"; /* read msg for recipients */
434 *argp++ = "-i"; /* don't stop on "." */
437 if (watch || verbose)
441 if (pushflg && !(watch || verbose)) {
442 /* fork to a child to run sendmail */
443 for (i=0; (pid = vfork()) == NOTOK && i < 5; i++)
447 fprintf (verbose ? stdout : stderr, "%s: can't fork to %s\n",
448 invo_name, sendmail);
451 /* we're the child .. */
457 execv ( sendmail, sargv);
458 adios ( sendmail, "can't exec");
459 return 0; /* dead code to satisfy the compiler */
462 /* DRAFT GENERATION */
465 putfmt (char *name, char *str, FILE *out)
471 while (*str == ' ' || *str == '\t')
474 if ((i = get_header (name, hdrtab)) == NOTOK) {
475 fprintf (out, "%s: %s", name, str);
480 if (hdr->flags & HIGN)
482 if (hdr->flags & HBAD) {
483 advise (NULL, "illegal header line -- %s:", name);
487 msgflags |= hdr->set;
489 if (hdr->flags & HSUB)
490 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
492 if (hdr->flags & HFCC) {
493 if ((cp = strrchr(str, '\n')))
495 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
497 insert_fcc (hdr, pp);
499 insert_fcc (hdr, pp);
504 if (hdr->flags & HBCC) {
510 if (*str != '\n' && *str != '\0') {
511 if (aliasflg && hdr->flags & HTRY) {
512 /* this header contains address(es) that we have to do
513 * alias expansion on. Because of the saved state in
514 * getname we have to put all the addresses into a list.
515 * We then let putadr munch on that list, possibly
518 register struct mailname *f = 0;
519 register struct mailname *mp = 0;
521 while ((cp = getname(str))) {
522 mp = getm( cp, NULL, 0, AD_HOST, NULL);
527 mp->m_next = f->m_next;
532 f = mp->m_next; mp->m_next = 0;
535 /* The author(s) of spost decided that alias substitution wasn't
536 necessary for the non-HTRY headers. Unfortunately, one of those
537 headers is "From:", and having alias substitution work on that is
538 extremely useful for someone with a lot of POP3 email accounts or
539 aliases. post supports aliasing of "From:"...
541 Since "From:"-processing is incompletely implemented in this
542 unsupported and undocumented spost backend, I'm not going to take
543 the time to implement my new draft-From:-based email address
544 masquerading. If I do ever implement it here, I'd almost
545 certainly want to implement "From:" line alias processing as
546 well. -- Dan Harkless <dan-nmh@dilvish.speed.net> */
547 fprintf (out, "%s: %s", name, str );
559 strncpy(from, getusername(), sizeof(from));
561 if ((cp = getfullname ()) && *cp) {
562 strncpy (sigbuf, cp, sizeof(sigbuf));
563 snprintf (signature, sizeof(signature), "%s <%s>", sigbuf, from);
566 snprintf (signature, sizeof(signature), "%s", from);
571 finish_headers (FILE *out)
575 if (!(msgflags & MDAT))
576 fprintf (out, "Date: %s\n", dtimenow (0));
578 fprintf (out, "Sender: %s\n", from);
580 fprintf (out, "From: %s\n", signature);
582 if (!(msgflags & MVIS))
583 fprintf (out, "Bcc: Blind Distribution List: ;\n");
588 if (!(msgflags & MRDT))
589 fprintf (out, "Resent-Date: %s\n", dtimenow(0));
591 fprintf (out, "Resent-Sender: %s\n", from);
593 fprintf (out, "Resent-From: %s\n", signature);
595 if (!(msgflags & MVIS))
596 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
602 adios (NULL, "re-format message and try again");
607 get_header (char *header, struct headers *table)
611 for (h = table; h->value; h++)
612 if (!strcasecmp (header, h->value))
620 * output the address list for header "name". The address list
621 * is a linked list of mailname structs. "nl" points to the head
622 * of the list. Alias substitution should be done on nl.
625 putadr (char *name, struct mailname *nl)
627 register struct mailname *mp, *mp2;
628 register int linepos;
632 fprintf (out, "%s: ", name);
633 namelen = strlen(name) + 2;
636 for (mp = nl; mp; ) {
637 if (linepos > MAX_SM_FIELD) {
638 fprintf (out, "\n%s: ", name);
642 /* a local name - see if it's an alias */
643 cp = akvalue(mp->m_mbox);
644 if (cp == mp->m_mbox)
645 /* wasn't an alias - use what the user typed */
646 linepos = putone( mp->m_text, linepos, namelen );
648 /* an alias - expand it */
649 while ((cp = getname(cp))) {
650 if (linepos > MAX_SM_FIELD) {
651 fprintf (out, "\n%s: ", name);
654 mp2 = getm( cp, NULL, 0, AD_HOST, NULL);
656 mp2->m_pers = getcpy(mp->m_mbox);
657 linepos = putone( adrformat(mp2), linepos, namelen );
659 linepos = putone( mp2->m_text, linepos, namelen );
664 /* not a local name - use what the user typed */
665 linepos = putone( mp->m_text, linepos, namelen );
675 putone (char *adr, int pos, int indent)
683 else if ( linepos+len > outputlinelen ) {
684 fprintf ( out, ",\n%*s", indent, "");
701 insert_fcc (struct headers *hdr, char *pp)
705 for (cp = pp; isspace (*cp); cp++)
707 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
715 adios (NULL, "too many %ss", hdr->value);
716 fccfold[fccind++] = getcpy (cp);
731 if ((out = fopen (bccfil, "w")) == NULL)
732 adios (bccfil, "unable to create");
733 chmod (bccfil, 0600);
735 fprintf (out, "Date: %s\n", dtimenow (0));
736 fprintf (out, "From: %s\n", signature);
738 fprintf (out, "Subject: %s", subject);
739 fprintf (out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
742 if (filter == NULL) {
743 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
744 adios (NULL, "unable to re-open");
745 cpydgst (fd, fileno (out), tmpfil, bccfil);
749 vec[0] = r1bindex (mhlproc, '/');
751 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
755 adios ("vfork", "unable to");
758 dup2 (fileno (out), 1);
761 vec[i++] = "-forward";
767 execvp (mhlproc, vec);
768 adios (mhlproc, "unable to exec");
771 if (status = pidwait(child_id, OK))
772 admonish (NULL, "%s lost (status=0%o)", vec[0], status);
777 fseek (out, 0L, SEEK_END);
778 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
783 /* FCC INTERACTION */
793 for (i = 0; i < fccind; i++)
795 printf ("Fcc: %s\n", fccfold[i]);
797 fcc (path, fccfold[i]);
802 fcc (char *file, char *folder)
809 printf ("%sFcc: %s\n", msgstate == resent ? "Resent-" : "", folder);
812 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
817 fprintf (stderr, " %sFcc %s: ",
818 msgstate == resent ? "Resent-" : "", folder);
819 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
823 snprintf (fold, sizeof(fold), "%s%s",
824 *folder == '+' || *folder == '@' ? "" : "+", folder);
825 execlp (fileproc, r1bindex (fileproc, '/'),
826 "-link", "-file", file, fold, NULL);
830 if ((status = pidwait(child_id, OK))) {
832 fprintf (stderr, " %sFcc %s: ",
833 msgstate == resent ? "Resent-" : "", folder);
834 fprintf (verbose ? stdout : stderr,
835 " errored (0%o)\n", status);
850 die (char *what, char *fmt, ...)
855 advertise (what, NULL, fmt, ap);