2 static char sccsid[] = "@(#)spost.c 1.6 (Berkeley) 11/2/85";
5 static char ident[] = "@(#)$Id: spost.c,v 1.8 1992/12/15 00:20:22 jromine Exp $";
8 /* spost.c - feed messages to sendmail */
10 * (This is a simpler, faster, replacement for "post" for use when "sendmail"
11 * is the transport system)
18 #include "../h/addrsbr.h"
19 #include "../h/aliasbr.h"
20 #include "../h/dropsbr.h"
21 #include "../zotnet/tws.h"
26 extern char *getfullname (), *getusr ();
27 extern char *sendmail;
30 #define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : c)
32 #define MAX_SM_FIELD 1476 /* < largest hdr field sendmail will accept */
33 #define FCCS 10 /* max number of fccs allowed */
37 struct swit switches[] = {
39 "filter filterfile", 0,
70 "dist", -4, /* interface from dist */
78 "check", -5, /* interface from whom */
80 "nocheck", -7, /* interface from whom */
82 "whom", -4, /* interface from whom */
84 #define PUSHSW 18 /* fork to sendmail then exit */
86 #define NPUSHSW 19 /* exec sendmail */
98 "library directory", -7,
112 #define HNOP 0x0000 /* just used to keep .set around */
113 #define HBAD 0x0001 /* bad header - don't let it through */
114 #define HADR 0x0002 /* header has an address field */
115 #define HSUB 0x0004 /* Subject: header */
116 #define HTRY 0x0008 /* try to send to addrs on header */
117 #define HBCC 0x0010 /* don't output this header */
118 #define HMNG 0x0020 /* mung this header */
119 #define HNGR 0x0040 /* no groups allowed in this header */
120 #define HFCC 0x0080 /* FCC: type header */
121 #define HNIL 0x0100 /* okay for this header not to have addrs */
122 #define HIGN 0x0200 /* ignore this header */
125 #define MFRM 0x0001 /* we've seen a From: */
126 #define MDAT 0x0002 /* we've seen a Date: */
127 #define MRFM 0x0004 /* we've seen a Resent-From: */
128 #define MVIS 0x0008 /* we've seen sighted addrs */
129 #define MINV 0x0010 /* we've seen blind addrs */
130 #define MRDT 0x0020 /* we've seen a Resent-Date: */
135 static struct headers NHeaders[] = {
136 "Return-Path", HBAD, 0,
138 "Reply-To", HADR | HNGR, 0,
139 "From", HADR | HNGR, MFRM,
140 "Sender", HADR | HBAD, 0,
143 "To", HADR | HTRY, MVIS,
144 "cc", HADR | HTRY, MVIS,
145 "Bcc", HADR | HTRY | HBCC | HNIL, MINV,
146 "Message-Id", HBAD, 0,
152 static struct headers RHeaders[] = {
153 "Resent-Reply-To", HADR | HNGR, 0,
154 "Resent-From", HADR | HNGR, MRFM,
155 "Resent-Sender", HADR | HBAD, 0,
156 "Resent-Date", HNOP, MRDT,
157 "Resent-Subject", HSUB, 0,
158 "Resent-To", HADR | HTRY, MVIS,
159 "Resent-cc", HADR | HTRY, MVIS,
160 "Resent-Bcc", HADR | HTRY | HBCC, MINV,
161 "Resent-Message-Id", HBAD, 0,
162 "Resent-Fcc", HFCC, 0,
172 static short fccind = 0; /* index into fccfold[] */
174 static int badmsg = 0; /* message has bad semantics */
175 static int verbose = 0; /* spell it out */
176 static int debug = 0; /* debugging post */
177 static int rmflg = 1; /* remove temporary file when done */
178 static int watch = 0; /* watch the delivery process */
179 static int backflg = 0; /* rename input file as *.bak when done */
180 static int whomflg = 0; /* if just checking addresses */
181 static int pushflg = 0; /* if going to fork to sendmail */
182 static int aliasflg = -1; /* if going to process aliases */
183 static int outputlinelen=72;
185 static unsigned msgflags = 0; /* what we've seen */
191 static char tmpfil[] = "/tmp/pstXXXXXX";
193 static char from[BUFSIZ]; /* my network address */
194 static char signature[BUFSIZ]; /* my signature */
195 static char *filter = NULL; /* the filter for BCC'ing */
196 static char *subject = NULL; /* the subject field for BCC'ing */
197 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
199 static struct headers *hdrtab; /* table for the message we're doing */
200 static FILE *out; /* output (temp) file */
202 static putfmt(), start_headers(), finish_headers(), putadr(),
204 static file(), fcc();
205 static int get_header(), putone();
228 setlocale(LC_ALL, "");
230 invo_name = r1bindex (argv[0], '/');
231 mts_init (invo_name);
232 if ((cp = m_find (invo_name)) != NULL) {
233 argp = copyip (brkstring (cp, " ", "\n"), arguments);
234 (void) copyip (argv+1, argp);
240 while (cp = *argp++) {
242 switch (smatch (++cp, switches)) {
244 ambigsw (cp, switches);
247 adios (NULLCP, "-%s unknown", cp);
249 (void)sprintf (buf, "%s [switches] file", invo_name);
250 help (buf, switches);
266 if (!(filter = *argp++) || *filter == '-')
267 adios (NULLCP, "missing argument to %s", argp[-2]);
309 if (!(cp = *argp++) || *cp == '-')
310 adios (NULLCP, "missing argument to %s", argp[-2]);
312 (void) alias (AliasFile);/* load default aka's */
314 if ((state = alias(cp)) != AK_OK)
315 adios (NULLCP, "aliasing error in file %s - %s",
316 cp, akerror(state) );
323 if (!(cp = *argp++) || *cp == '-')
324 adios (NULLCP, "missing argument to %s", argp[-2]);
325 outputlinelen = atoi (cp);
326 if (outputlinelen <= 10)
332 /* -library & -idanno switch ignored */
333 if (!(cp = *argp++) || *cp == '-')
334 adios (NULLCP, "missing argument to %s", argp[-2]);
338 adios (NULLCP, "only one message at a time!");
346 (void) alias (AliasFile); /* load default aka's */
349 adios (NULLCP, "usage: %s [switches] file", invo_name);
351 if ((in = fopen (msg, "r")) == NULL)
352 adios (msg, "unable to open");
360 (void)mktemp (tmpfil);
361 if ((out = fopen (tmpfil, "w")) == NULL)
362 adios (tmpfil, "unable to create");
363 (void)chmod (tmpfil, 0600);
366 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
368 for (compnum = 1, state = FLD;;) {
369 switch (state = m_getfld (state, name, buf, sizeof buf, in)) {
372 putfmt (name, buf, out);
378 while (state == FLDPLUS) {
379 state = m_getfld (state, name, buf, sizeof buf, in);
382 putfmt (name, cp, out);
387 finish_headers (out);
388 fprintf (out, "\n%s", buf);
390 while (state == BODY) {
391 state = m_getfld (state, name, buf, sizeof buf, in);
397 finish_headers (out);
402 adios (NULLCP, "message format error in component #%d",
406 adios (NULLCP, "getfld() returned %d", state);
414 if (backflg && !whomflg) {
415 (void) strcpy (buf, m_backup (msg));
416 if (rename (msg, buf) == NOTOK)
417 advise (buf, "unable to rename %s to", msg);
429 * re-open the temp file, unlink it and exec sendmail, giving it
430 * the msg temp file as std in.
432 if ( freopen( tmpfil, "r", stdin) == NULL)
433 adios (tmpfil, "can't reopen for sendmail");
435 (void)unlink (tmpfil);
438 *argp++ = "send-mail";
439 *argp++ = "-m"; /* send to me too */
440 *argp++ = "-t"; /* read msg for recipients */
441 *argp++ = "-i"; /* don't stop on "." */
444 if (watch || verbose)
448 if (pushflg && !(watch || verbose)) {
449 /* fork to a child to run sendmail */
450 for (i=0; (pid = vfork()) == NOTOK && i < 5; i++)
454 fprintf (verbose ? stdout : stderr, "%s: can't fork to %s\n",
455 invo_name, sendmail);
458 /* we're the child .. */
464 execv ( sendmail, sargv);
465 adios ( sendmail, "can't exec");
468 /*
\f DRAFT GENERATION */
470 static putfmt (name, str, out)
487 while (*str == ' ' || *str == '\t')
490 if ((i = get_header (name, hdrtab)) == NOTOK) {
491 fprintf (out, "%s: %s", name, str);
496 if (hdr -> flags & HIGN)
498 if (hdr -> flags & HBAD) {
499 advise (NULLCP, "illegal header line -- %s:", name);
503 msgflags |= hdr -> set;
505 if (hdr -> flags & HSUB)
506 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
508 if (hdr -> flags & HFCC) {
509 if (cp = rindex (str, '\n'))
511 for (cp = pp = str; cp = index (pp, ','); pp = cp) {
513 insert_fcc (hdr, pp);
515 insert_fcc (hdr, pp);
520 if (hdr -> flags & HBCC) {
526 if (*str != '\n' && *str != '\0')
527 if (aliasflg && hdr->flags & HTRY) {
528 /* this header contains address(es) that we have to do
529 * alias expansion on. Because of the saved state in
530 * getname we have to put all the addresses into a list.
531 * We then let putadr munch on that list, possibly
534 register struct mailname *f = 0;
535 register struct mailname *mp = 0;
537 while (cp = getname( str ) ) {
538 mp = getm( cp, NULLCP, 0, AD_HOST, NULLCP);
543 mp->m_next = f->m_next;
548 f = mp->m_next; mp->m_next = 0;
551 fprintf (out, "%s: %s", name, str );
563 (void)strcpy( from, getusr() );
565 if ((cp = getfullname ()) && *cp) {
566 (void)strcpy (sigbuf, cp);
567 (void)sprintf (signature, "%s <%s>", sigbuf, from);
570 (void)sprintf (signature, "%s", from);
581 if (!(msgflags & MDAT))
582 fprintf (out, "Date: %s\n", dtimenow ());
584 fprintf (out, "Sender: %s\n", from);
586 fprintf (out, "From: %s\n", signature);
588 if (!(msgflags & MVIS))
589 fprintf (out, "Bcc: Blind Distribution List: ;\n");
594 if (!(msgflags & MRDT))
595 fprintf (out, "Resent-Date: %s\n", dtimenow());
597 fprintf (out, "Resent-Sender: %s\n", from);
599 fprintf (out, "Resent-From: %s\n", signature);
601 if (!(msgflags & MVIS))
602 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
608 adios (NULLCP, "re-format message and try again");
614 get_header (header, table)
616 struct headers *table;
620 for (h = table; h -> value; h++)
621 if (uleq (header, h -> value))
629 /* output the address list for header "name". The address list
630 * is a linked list of mailname structs. "nl" points to the head
631 * of the list. Alias substitution should be done on nl.
633 static putadr (name, nl)
637 register struct mailname *mp, *mp2;
638 register int linepos;
642 fprintf (out, "%s: ", name);
643 namelen = strlen(name) + 2;
646 for (mp = nl; mp; ) {
647 if (linepos > MAX_SM_FIELD) {
648 fprintf (out, "\n%s: ", name);
652 /* a local name - see if it's an alias */
653 cp = akvalue(mp->m_mbox);
654 if (cp == mp->m_mbox)
655 /* wasn't an alias - use what the user typed */
656 linepos = putone( mp->m_text, linepos, namelen );
658 /* an alias - expand it */
659 while (cp = getname(cp) ) {
660 if (linepos > MAX_SM_FIELD) {
661 fprintf (out, "\n%s: ", name);
664 mp2 = getm( cp, NULLCP, 0, AD_HOST, NULLCP);
666 mp2->m_pers = getcpy(mp->m_mbox);
667 linepos = putone( adrformat(mp2), linepos, namelen );
669 linepos = putone( mp2->m_text, linepos, namelen );
674 /* not a local name - use what the user typed */
675 linepos = putone( mp->m_text, linepos, namelen );
684 static int putone ( adr, pos, indent )
695 else if ( linepos+len > outputlinelen ) {
696 fprintf ( out, ",\n%*s", indent, "");
713 static insert_fcc (hdr, pp)
719 for (cp = pp; isspace (*cp); cp++)
721 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
729 adios (NULLCP, "too many %ss", hdr -> value);
730 fccfold[fccind++] = getcpy (cp);
734 /*
\f BCC GENERATION */
736 static make_bcc_file () {
744 (void)mktemp (bccfil);
745 if ((out = fopen (bccfil, "w")) == NULL)
746 adios (bccfil, "unable to create");
747 (void)chmod (bccfil, 0600);
749 fprintf (out, "Date: %s\n", dtimenow ());
750 fprintf (out, "From: %s\n", signature);
752 fprintf (out, "Subject: %s", subject);
753 fprintf (out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
756 if (filter == NULL) {
757 if ((fd = open (tmpfil, 0)) == NOTOK)
758 adios (NULLCP, "unable to re-open");
759 cpydgst (fd, fileno (out), tmpfil, bccfil);
763 vec[0] = r1bindex (mhlproc, '/');
765 for (i = 0; (child_id = vfork ()) == NOTOK && i < 5; i++)
769 adios ("vfork", "unable to");
772 dup2 (fileno (out), 1);
775 vec[i++] = "-forward";
781 execvp (mhlproc, vec);
782 adios (mhlproc, "unable to exec");
785 if (status = pidwait (child_id, OK))
786 admonish (NULL, "%s lost (status=0%o)", vec[0], status);
792 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
797 /*
\f FCC INTERACTION */
807 for (i = 0; i < fccind; i++)
809 printf ("Fcc: %s\n", fccfold[i]);
811 fcc (path, fccfold[i]);
815 static fcc (file, folder)
825 printf ("%sFcc: %s\n", msgstate == resent ? "Resent-" : "", folder);
826 (void)fflush (stdout);
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 (void)sprintf (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);
855 (void)fflush (stdout);
862 static die (what, fmt, a, b, c, d)
870 adios (what, fmt, a, b, c, d);