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>
18 #define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : c)
20 #define MAX_SM_FIELD 1476 /* < largest hdr field sendmail will accept */
21 #define FCCS 10 /* max number of fccs allowed */
23 struct swit switches[] = {
25 { "filter filterfile", 0 },
49 { "alias aliasfile", 0 },
53 { "width columns", 0 },
61 { "dist", -4 }, /* interface from dist */
63 { "check", -5 }, /* interface from whom */
65 { "nocheck", -7 }, /* interface from whom */
67 { "whom", -4 }, /* interface from whom */
68 #define PUSHSW 22 /* fork to sendmail then exit */
70 #define NPUSHSW 23 /* exec sendmail */
73 { "library directory", -7 },
75 { "idanno number", -6 },
80 /* flags for headers->flags */
81 #define HNOP 0x0000 /* just used to keep .set around */
82 #define HBAD 0x0001 /* bad header - don't let it through */
83 #define HADR 0x0002 /* header has an address field */
84 #define HSUB 0x0004 /* Subject: header */
85 #define HTRY 0x0008 /* try to send to addrs on header */
86 #define HBCC 0x0010 /* don't output this header */
87 #define HMNG 0x0020 /* mung this header */
88 #define HNGR 0x0040 /* no groups allowed in this header */
89 #define HFCC 0x0080 /* FCC: type header */
90 #define HNIL 0x0100 /* okay for this header not to have addrs */
91 #define HIGN 0x0200 /* ignore this header */
93 /* flags for headers->set */
94 #define MFRM 0x0001 /* we've seen a From: */
95 #define MDAT 0x0002 /* we've seen a Date: */
96 #define MRFM 0x0004 /* we've seen a Resent-From: */
97 #define MVIS 0x0008 /* we've seen sighted addrs */
98 #define MINV 0x0010 /* we've seen blind addrs */
99 #define MRDT 0x0020 /* we've seen a Resent-Date: */
108 static struct headers NHeaders[] = {
109 { "Return-Path", HBAD, 0 },
110 { "Received", HBAD, 0 },
111 { "Reply-To", HADR|HNGR, 0 },
112 { "From", HADR|HNGR, MFRM },
113 { "Sender", HADR|HBAD, 0 },
114 { "Date", HNOP, MDAT },
115 { "Subject", HSUB, 0 },
116 { "To", HADR|HTRY, MVIS },
117 { "cc", HADR|HTRY, MVIS },
118 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
119 { "Message-Id", HBAD, 0 },
124 static struct headers RHeaders[] = {
125 { "Resent-Reply-To", HADR|HNGR, 0 },
126 { "Resent-From", HADR|HNGR, MRFM },
127 { "Resent-Sender", HADR|HBAD, 0 },
128 { "Resent-Date", HNOP, MRDT },
129 { "Resent-Subject", HSUB, 0 },
130 { "Resent-To", HADR|HTRY, MVIS },
131 { "Resent-cc", HADR|HTRY, MVIS },
132 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
133 { "Resent-Message-Id", HBAD, 0 },
134 { "Resent-Fcc", HFCC, 0 },
135 { "Reply-To", HADR, 0 },
141 static short fccind = 0; /* index into fccfold[] */
143 static int badmsg = 0; /* message has bad semantics */
144 static int verbose = 0; /* spell it out */
145 static int debug = 0; /* debugging post */
146 static int rmflg = 1; /* remove temporary file when done */
147 static int watch = 0; /* watch the delivery process */
148 static int backflg = 0; /* rename input file as *.bak when done */
149 static int whomflg = 0; /* if just checking addresses */
150 static int pushflg = 0; /* if going to fork to sendmail */
151 static int aliasflg = -1; /* if going to process aliases */
152 static int outputlinelen=72;
154 static unsigned msgflags = 0; /* what we've seen */
160 static char tmpfil[] = "/tmp/pstXXXXXX";
162 static char from[BUFSIZ]; /* my network address */
163 static char signature[BUFSIZ]; /* my signature */
164 static char *filter = NULL; /* the filter for BCC'ing */
165 static char *subject = NULL; /* the subject field for BCC'ing */
166 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
168 static struct headers *hdrtab; /* table for the message we're doing */
169 static FILE *out; /* output (temp) file */
171 extern char *sendmail;
174 * external prototypes
176 extern char *getfullname (void);
177 extern char *getusername (void);
182 static void putfmt (char *, char *, FILE *);
183 static void start_headers (void);
184 static void finish_headers (FILE *);
185 static int get_header (char *, struct headers *);
186 static void putadr (char *, struct mailname *);
187 static int putone (char *, int, int);
188 static void insert_fcc (struct headers *, char *);
189 static void file (char *);
190 static void fcc (char *, char *);
193 static void die (char *, char *, ...);
194 static void make_bcc_file (void);
199 main (int argc, char **argv)
201 int state, i, pid, compnum;
202 char *cp, *msg = NULL, **argp, **arguments;
203 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
207 setlocale(LC_ALL, "");
209 invo_name = r1bindex (argv[0], '/');
211 /* foil search of user profile/context */
212 if (context_foil (NULL) == -1)
215 mts_init (invo_name);
216 arguments = getarguments (invo_name, argc, argv, 0);
219 while ((cp = *argp++)) {
221 switch (smatch (++cp, switches)) {
223 ambigsw (cp, switches);
226 adios (NULL, "-%s unknown", cp);
229 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
230 print_help (buf, switches, 1);
233 print_version(invo_name);
249 if (!(filter = *argp++) || *filter == '-')
250 adios (NULL, "missing argument to %s", argp[-2]);
292 if (!(cp = *argp++) || *cp == '-')
293 adios (NULL, "missing argument to %s", argp[-2]);
295 alias (AliasFile);/* load default aka's */
297 if ((state = alias(cp)) != AK_OK)
298 adios (NULL, "aliasing error in file %s - %s",
299 cp, akerror(state) );
306 if (!(cp = *argp++) || *cp == '-')
307 adios (NULL, "missing argument to %s", argp[-2]);
308 outputlinelen = atoi (cp);
309 if (outputlinelen <= 10)
314 if (!(cp = *argp++) || *cp == '-')
315 adios (NULL, "missing argument to %s", argp[-2]);
316 /* create a minimal context */
317 if (context_foil (cp) == -1)
322 /* -idanno switch ignored */
323 if (!(cp = *argp++) || *cp == '-')
324 adios (NULL, "missing argument to %s", argp[-2]);
329 adios (NULL, "only one message at a time!");
335 alias (AliasFile); /* load default aka's */
338 adios (NULL, "usage: %s [switches] file", invo_name);
340 if ((in = fopen (msg, "r")) == NULL)
341 adios (msg, "unable to open");
350 if ((out = fopen (tmpfil, "w")) == NULL)
351 adios (tmpfil, "unable to create");
352 chmod (tmpfil, 0600);
355 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
357 for (compnum = 1, state = FLD;;) {
358 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
361 putfmt (name, buf, out);
367 while (state == FLDPLUS) {
368 state = m_getfld (state, name, buf, sizeof(buf), in);
371 putfmt (name, cp, out);
376 finish_headers (out);
377 fprintf (out, "\n%s", buf);
379 while (state == BODY) {
380 state = m_getfld (state, name, buf, sizeof(buf), in);
386 finish_headers (out);
391 adios (NULL, "message format error in component #%d",
395 adios (NULL, "getfld() returned %d", state);
401 if (backflg && !whomflg) {
402 strncpy (buf, m_backup (msg), sizeof(buf));
403 if (rename (msg, buf) == NOTOK)
404 advise (buf, "unable to rename %s to", msg);
416 * re-open the temp file, unlink it and exec sendmail, giving it
417 * the msg temp file as std in.
419 if ( freopen( tmpfil, "r", stdin) == NULL)
420 adios (tmpfil, "can't reopen for sendmail");
425 *argp++ = "send-mail";
426 *argp++ = "-m"; /* send to me too */
427 *argp++ = "-t"; /* read msg for recipients */
428 *argp++ = "-i"; /* don't stop on "." */
431 if (watch || verbose)
435 if (pushflg && !(watch || verbose)) {
436 /* fork to a child to run sendmail */
437 for (i=0; (pid = vfork()) == NOTOK && i < 5; i++)
441 fprintf (verbose ? stdout : stderr, "%s: can't fork to %s\n",
442 invo_name, sendmail);
445 /* we're the child .. */
451 execv ( sendmail, sargv);
452 adios ( sendmail, "can't exec");
455 /* DRAFT GENERATION */
458 putfmt (char *name, char *str, FILE *out)
464 while (*str == ' ' || *str == '\t')
467 if ((i = get_header (name, hdrtab)) == NOTOK) {
468 fprintf (out, "%s: %s", name, str);
473 if (hdr->flags & HIGN)
475 if (hdr->flags & HBAD) {
476 advise (NULL, "illegal header line -- %s:", name);
480 msgflags |= hdr->set;
482 if (hdr->flags & HSUB)
483 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
485 if (hdr->flags & HFCC) {
486 if ((cp = strrchr(str, '\n')))
488 for (cp = pp = str; cp = strchr(pp, ','); pp = cp) {
490 insert_fcc (hdr, pp);
492 insert_fcc (hdr, pp);
497 if (hdr->flags & HBCC) {
503 if (*str != '\n' && *str != '\0')
504 if (aliasflg && hdr->flags & HTRY) {
505 /* this header contains address(es) that we have to do
506 * alias expansion on. Because of the saved state in
507 * getname we have to put all the addresses into a list.
508 * We then let putadr munch on that list, possibly
511 register struct mailname *f = 0;
512 register struct mailname *mp = 0;
514 while ((cp = getname(str))) {
515 mp = getm( cp, NULL, 0, AD_HOST, NULL);
520 mp->m_next = f->m_next;
525 f = mp->m_next; mp->m_next = 0;
528 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>", sigbuf, from);
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 fprintf (out, "Sender: %s\n", from);
560 fprintf (out, "From: %s\n", signature);
562 if (!(msgflags & MVIS))
563 fprintf (out, "Bcc: Blind Distribution List: ;\n");
568 if (!(msgflags & MRDT))
569 fprintf (out, "Resent-Date: %s\n", dtimenow(0));
571 fprintf (out, "Resent-Sender: %s\n", from);
573 fprintf (out, "Resent-From: %s\n", signature);
575 if (!(msgflags & MVIS))
576 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
582 adios (NULL, "re-format message and try again");
587 get_header (char *header, struct headers *table)
591 for (h = table; h->value; h++)
592 if (!strcasecmp (header, h->value))
600 * output the address list for header "name". The address list
601 * is a linked list of mailname structs. "nl" points to the head
602 * of the list. Alias substitution should be done on nl.
605 putadr (char *name, struct mailname *nl)
607 register struct mailname *mp, *mp2;
608 register int linepos;
612 fprintf (out, "%s: ", name);
613 namelen = strlen(name) + 2;
616 for (mp = nl; mp; ) {
617 if (linepos > MAX_SM_FIELD) {
618 fprintf (out, "\n%s: ", name);
622 /* a local name - see if it's an alias */
623 cp = akvalue(mp->m_mbox);
624 if (cp == mp->m_mbox)
625 /* wasn't an alias - use what the user typed */
626 linepos = putone( mp->m_text, linepos, namelen );
628 /* an alias - expand it */
629 while ((cp = getname(cp))) {
630 if (linepos > MAX_SM_FIELD) {
631 fprintf (out, "\n%s: ", name);
634 mp2 = getm( cp, NULL, 0, AD_HOST, NULL);
636 mp2->m_pers = getcpy(mp->m_mbox);
637 linepos = putone( adrformat(mp2), linepos, namelen );
639 linepos = putone( mp2->m_text, linepos, namelen );
644 /* not a local name - use what the user typed */
645 linepos = putone( mp->m_text, linepos, namelen );
655 putone (char *adr, int pos, int indent)
663 else if ( linepos+len > outputlinelen ) {
664 fprintf ( out, ",\n%*s", indent, "");
681 insert_fcc (struct headers *hdr, char *pp)
685 for (cp = pp; isspace (*cp); cp++)
687 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
695 adios (NULL, "too many %ss", hdr->value);
696 fccfold[fccind++] = getcpy (cp);
711 if ((out = fopen (bccfil, "w")) == NULL)
712 adios (bccfil, "unable to create");
713 chmod (bccfil, 0600);
715 fprintf (out, "Date: %s\n", dtimenow (0));
716 fprintf (out, "From: %s\n", signature);
718 fprintf (out, "Subject: %s", subject);
719 fprintf (out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
722 if (filter == NULL) {
723 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
724 adios (NULL, "unable to re-open");
725 cpydgst (fd, fileno (out), tmpfil, bccfil);
729 vec[0] = r1bindex (mhlproc, '/');
731 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
735 adios ("vfork", "unable to");
738 dup2 (fileno (out), 1);
741 vec[i++] = "-forward";
747 execvp (mhlproc, vec);
748 adios (mhlproc, "unable to exec");
751 if (status = pidwait(child_id, OK))
752 admonish (NULL, "%s lost (status=0%o)", vec[0], status);
757 fseek (out, 0L, SEEK_END);
758 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
763 /* FCC INTERACTION */
773 for (i = 0; i < fccind; i++)
775 printf ("Fcc: %s\n", fccfold[i]);
777 fcc (path, fccfold[i]);
782 fcc (char *file, char *folder)
789 printf ("%sFcc: %s\n", msgstate == resent ? "Resent-" : "", folder);
792 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
797 fprintf (stderr, " %sFcc %s: ",
798 msgstate == resent ? "Resent-" : "", folder);
799 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
803 snprintf (fold, sizeof(fold), "%s%s",
804 *folder == '+' || *folder == '@' ? "" : "+", folder);
805 execlp (fileproc, r1bindex (fileproc, '/'),
806 "-link", "-file", file, fold, NULL);
810 if ((status = pidwait(child_id, OK))) {
812 fprintf (stderr, " %sFcc %s: ",
813 msgstate == resent ? "Resent-" : "", folder);
814 fprintf (verbose ? stdout : stderr,
815 " errored (0%o)\n", status);
830 die (char *what, char *fmt, ...)
835 advertise (what, NULL, fmt, ap);