3 * post.c -- enter messages into the mail transport system
10 #include <h/signals.h>
11 #include <h/addrsbr.h>
12 #include <h/aliasbr.h>
13 #include <h/dropsbr.h>
17 #include <zotnet/mts/mts.h>
23 #ifdef TIME_WITH_SYS_TIME
24 # include <sys/time.h>
27 # ifdef TM_IN_SYS_TIME
28 # include <sys/time.h>
35 # include <mts/mmdf/util.h>
36 # include <mts/mmdf/mmdf.h>
40 * Currently smtp and sendmail use
41 * the same interface for posting.
48 # include <mts/smtp/smtp.h>
52 # define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : (c))
55 #define FCCS 10 /* max number of fccs allowed */
57 /* In the following array of structures, the numeric second field of the
58 structures (minchars) is apparently used like this:
60 -# : Switch can be abbreviated to # characters; switch hidden in -help.
61 0 : Switch can't be abbreviated; switch shown in -help.
62 # : Switch can be abbreviated to # characters; switch shown in -help. */
64 static struct swit switches[] = {
66 { "alias aliasfile", 0 },
68 { "check", -5 }, /* interface from whom */
70 { "nocheck", -7 }, /* interface from whom */
74 { "dist", -4 }, /* interface from dist */
76 { "filter filterfile", 0 },
84 { "library directory", -7 }, /* interface from send, whom */
102 { "whom", -4 }, /* interface from whom */
104 { "width columns", 0 },
109 #define BITSTUFFSW 22
110 { "dashstuffing", -12 }, /* should we dashstuff BCC messages? */
111 #define NBITSTUFFSW 23
112 { "nodashstuffing", -14 },
114 { "mail", -4 }, /* specify MAIL smtp mode */
116 { "saml", -4 }, /* specify SAML smtp mode */
118 { "send", -4 }, /* specify SEND smtp mode */
120 { "soml", -4 }, /* specify SOML smtp mode */
122 { "idanno number", -6 }, /* interface from send */
124 { "deliver address-list", -7 },
126 { "client host", -6 },
128 { "server host", -6 }, /* specify alternate SMTP server */
130 { "snoop", -5 }, /* snoop the SMTP transaction */
132 { "fill-in file", -7 },
150 * flags for headers->flags
152 #define HNOP 0x0000 /* just used to keep .set around */
153 #define HBAD 0x0001 /* bad header - don't let it through */
154 #define HADR 0x0002 /* header has an address field */
155 #define HSUB 0x0004 /* Subject: header */
156 #define HTRY 0x0008 /* try to send to addrs on header */
157 #define HBCC 0x0010 /* don't output this header */
158 #define HMNG 0x0020 /* munge this header */
159 #define HNGR 0x0040 /* no groups allowed in this header */
160 #define HFCC 0x0080 /* FCC: type header */
161 #define HNIL 0x0100 /* okay for this header not to have addrs */
162 #define HIGN 0x0200 /* ignore this header */
163 #define HDCC 0x0400 /* another undocumented feature */
166 * flags for headers->set
168 #define MFRM 0x0001 /* we've seen a From: */
169 #define MDAT 0x0002 /* we've seen a Date: */
170 #define MRFM 0x0004 /* we've seen a Resent-From: */
171 #define MVIS 0x0008 /* we've seen sighted addrs */
172 #define MINV 0x0010 /* we've seen blind addrs */
175 static struct headers NHeaders[] = {
176 { "Return-Path", HBAD, 0 },
177 { "Received", HBAD, 0 },
178 { "Reply-To", HADR|HNGR, 0 },
179 { "From", HADR|HNGR, MFRM },
180 { "Sender", HADR|HBAD, 0 },
182 { "Subject", HSUB, 0 },
183 { "To", HADR|HTRY, MVIS },
184 { "cc", HADR|HTRY, MVIS },
185 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
186 { "Dcc", HADR|HTRY|HDCC|HNIL, MVIS }, /* sorta cc & bcc combined */
187 { "Message-ID", HBAD, 0 },
192 static struct headers RHeaders[] = {
193 { "Resent-Reply-To", HADR|HNGR, 0 },
194 { "Resent-From", HADR|HNGR, MRFM },
195 { "Resent-Sender", HADR|HBAD, 0 },
196 { "Resent-Date", HBAD, 0 },
197 { "Resent-Subject", HSUB, 0 },
198 { "Resent-To", HADR|HTRY, MVIS },
199 { "Resent-cc", HADR|HTRY, MVIS },
200 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
201 { "Resent-Message-ID", HBAD, 0 },
202 { "Resent-Fcc", HFCC, 0 },
203 { "Reply-To", HADR, 0 },
204 { "From", HADR|HNGR, MFRM },
206 { "Sender", HADR|HNGR|HMNG, 0 },
208 { "Sender", HADR|HNGR, 0 },
210 { "Date", HNOP, MDAT },
211 { "To", HADR|HNIL, 0 },
212 { "cc", HADR|HNIL, 0 },
213 { "Bcc", HADR|HTRY|HBCC|HNIL, 0 },
218 static short fccind = 0; /* index into fccfold[] */
219 static short outputlinelen = OUTPUTLINELEN;
221 static int pfd = NOTOK; /* fd to write annotation list to */
222 static uid_t myuid= -1; /* my user id */
223 static gid_t mygid= -1; /* my group id */
224 static int recipients = 0; /* how many people will get a copy */
225 static int unkadr = 0; /* how many of those were unknown */
226 static int badadr = 0; /* number of bad addrs */
227 static int badmsg = 0; /* message has bad semantics */
228 static int verbose = 0; /* spell it out */
229 static int format = 1; /* format addresses */
230 static int mime = 0; /* use MIME-style encapsulations for Bcc */
231 static int msgid = 0; /* add msgid */
232 static int debug = 0; /* debugging post */
233 static int watch = 0; /* watch the delivery process */
234 static int whomsw = 0; /* we are whom not post */
235 static int checksw = 0; /* whom -check */
236 static int linepos=0; /* putadr()'s position on the line */
237 static int nameoutput=0; /* putadr() has output header name */
239 static unsigned msgflags = 0; /* what we've seen */
243 static int msgstate = NORMAL;
245 static time_t tclock = 0; /* the time we started (more or less) */
247 static SIGNAL_HANDLER hstat, istat, qstat, tstat;
249 static char tmpfil[BUFSIZ];
250 static char bccfil[BUFSIZ];
252 static char from[BUFSIZ]; /* my network address */
253 static char signature[BUFSIZ]; /* my signature */
254 static char *filter = NULL; /* the filter for BCC'ing */
255 static char *subject = NULL; /* the subject field for BCC'ing */
256 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
258 static struct headers *hdrtab; /* table for the message we're doing */
260 static struct mailname localaddrs={NULL}; /* local addrs */
261 static struct mailname netaddrs={NULL}; /* network addrs */
262 static struct mailname uuaddrs={NULL}; /* uucp addrs */
263 static struct mailname tmpaddrs={NULL}; /* temporary queue */
266 static char *submitmode = "m"; /* deliver to mailbox only */
267 static char submitopts[6] = "vl"; /* initial options for submit */
271 static int snoop = 0;
272 static int smtpmode = S_MAIL;
273 static char *clientsw = NULL;
274 static char *serversw = NULL;
276 extern struct smtp sm_reply;
279 static char prefix[] = "----- =_aaaaaaaaaa";
281 static int fill_up = 0;
282 static char *fill_in = NULL;
283 static char *partno = NULL;
284 static int queued = 0;
286 extern boolean draft_from_masquerading; /* defined in mts.c */
291 static void putfmt (char *, char *, FILE *);
292 static void start_headers (void);
293 static void finish_headers (FILE *);
294 static int get_header (char *, struct headers *);
295 static int putadr (char *, char *, struct mailname *, FILE *, unsigned int);
296 static void putgrp (char *, char *, FILE *, unsigned int);
297 static int insert (struct mailname *);
298 static void pl (void);
299 static void anno (void);
300 static int annoaux (struct mailname *);
301 static void insert_fcc (struct headers *, char *);
302 static void make_bcc_file (int);
303 static void verify_all_addresses (int);
304 static void chkadr (void);
305 static void sigon (void);
306 static void sigoff (void);
307 static void p_refile (char *);
308 static void fcc (char *, char *);
309 static void die (char *, char *, ...);
310 static void post (char *, int, int);
311 static void do_text (char *file, int fd);
312 static void do_an_address (struct mailname *, int);
313 static void do_addresses (int, int);
314 static int find_prefix (void);
318 main (int argc, char **argv)
320 int state, compnum, dashstuff = 0;
321 char *cp, *msg = NULL, **argp, **arguments;
322 char buf[BUFSIZ], name[NAMESZ];
326 setlocale(LC_ALL, "");
328 invo_name = r1bindex (argv[0], '/');
330 /* foil search of user profile/context */
331 if (context_foil (NULL) == -1)
334 mts_init (invo_name);
335 arguments = getarguments (invo_name, argc, argv, 0);
338 #if defined(MMDFMTS) && defined(MMDFII)
339 mmdf_init (invo_name);
340 #endif /* MMDFMTS and MMDFII */
342 while ((cp = *argp++)) {
344 switch (smatch (++cp, switches)) {
346 ambigsw (cp, switches);
349 adios (NULL, "-%s unknown", cp);
352 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
353 print_help (buf, switches, 0);
356 print_version(invo_name);
360 if (!(cp = *argp++) || *cp == '-')
361 adios (NULL, "missing argument to %s", argp[-2]);
362 /* create a minimal context */
363 if (context_foil (cp) == -1)
368 if (!(cp = *argp++) || *cp == '-')
369 adios (NULL, "missing argument to %s", argp[-2]);
370 if ((state = alias (cp)) != AK_OK)
371 adios (NULL, "aliasing error in %s - %s",
372 cp, akerror (state));
391 if (!(filter = *argp++) || *filter == '-')
392 adios (NULL, "missing argument to %s", argp[-2]);
447 if (!(cp = *argp++) || *cp == '-')
448 adios (NULL, "missing argument to %s", argp[-2]);
449 if ((outputlinelen = atoi (cp)) < 10)
450 adios (NULL, "impossible width %d", outputlinelen);
454 if (!(cp = *argp++) || *cp == '-')
455 adios (NULL, "missing argument to %s", argp[-2]);
456 if ((pfd = atoi (cp)) <= 2)
457 adios (NULL, "bad argument %s %s", argp[-2], cp);
464 case SOMLSW: /* for right now, sigh... */
474 if (!(cp = *argp++) || *cp == '-')
475 adios (NULL, "missing argument to %s", argp[-2]);
481 if (!(cp = *argp++) || *cp == '-')
482 adios (NULL, "missing argument to %s", argp[-2]);
501 if (!(clientsw = *argp++) || *clientsw == '-')
502 adios (NULL, "missing argument to %s", argp[-2]);
505 if (!(serversw = *argp++) || *serversw == '-')
506 adios (NULL, "missing argument to %s", argp[-2]);
514 if (!(fill_in = *argp++) || *fill_in == '-')
515 adios (NULL, "missing argument to %s", argp[-2]);
521 if (!(partno = *argp++) || *partno == '-')
522 adios (NULL, "missing argument to %s", argp[-2]);
531 adios (NULL, "only one message at a time!");
539 adios (NULL, "usage: %s [switches] file", invo_name);
541 if (outputlinelen < 10)
542 adios (NULL, "impossible width %d", outputlinelen);
544 if ((in = fopen (msg, "r")) == NULL)
545 adios (msg, "unable to open");
550 discard (out = stdout); /* XXX: reference discard() to help loader */
553 if ((out = fopen (fill_in ? fill_in : "/dev/null", "w")) == NULL)
554 adios ("/dev/null", "unable to open");
556 strncpy (tmpfil, m_scratch ("", m_maildir (invo_name)),
558 if ((out = fopen (tmpfil, "w")) == NULL) {
559 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
560 if ((out = fopen (tmpfil, "w")) == NULL)
561 adios (tmpfil, "unable to create");
563 chmod (tmpfil, 0600);
567 hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
569 for (compnum = 1, state = FLD;;) {
570 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
575 cp = add (buf, NULL);
576 while (state == FLDPLUS) {
577 state = m_getfld (state, name, buf, sizeof(buf), in);
580 putfmt (name, cp, out);
584 finish_headers (out);
589 finish_headers (out);
590 if (whomsw && !fill_in)
592 fprintf (out, "\n%s", buf);
593 while (state == BODY) {
594 state = m_getfld (state, name, buf, sizeof(buf), in);
600 finish_headers (out);
605 adios (NULL, "message format error in component #%d", compnum);
608 adios (NULL, "getfld() returned %d", state);
624 /* If we are doing a "whom" check */
627 verify_all_addresses (1);
632 strcat (submitopts, submitmode);
634 strcat (submitopts, "nw");
637 if (msgflags & MINV) {
638 make_bcc_file (dashstuff);
639 if (msgflags & MVIS) {
640 verify_all_addresses (verbose);
641 post (tmpfil, 0, verbose);
643 post (bccfil, 1, verbose);
646 post (tmpfil, 0, isatty (1));
653 printf (partno ? "Partial Message #%s Processed\n" : "Message Processed\n",
664 putfmt (char *name, char *str, FILE *out)
666 int count, grp, i, keep;
669 struct mailname *mp, *np;
672 while (*str == ' ' || *str == '\t')
675 if (msgstate == NORMAL && uprf (name, "resent")) {
676 advise (NULL, "illegal header line -- %s:", name);
681 if ((i = get_header (name, hdrtab)) == NOTOK) {
682 fprintf (out, "%s: %s", name, str);
687 if (hdr->flags & HIGN) {
689 fprintf (out, "%s: %s", name, str);
692 if (hdr->flags & HBAD) {
694 fprintf (out, "%s: %s", name, str);
696 advise (NULL, "illegal header line -- %s:", name);
701 msgflags |= (hdr->set & ~(MVIS | MINV));
703 if (hdr->flags & HSUB)
704 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
705 if (hdr->flags & HFCC) {
707 fprintf (out, "%s: %s", name, str);
711 if ((cp = strrchr(str, '\n')))
713 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
715 insert_fcc (hdr, pp);
717 insert_fcc (hdr, pp);
721 if (!(hdr->flags & HADR)) {
722 fprintf (out, "%s: %s", name, str);
726 tmpaddrs.m_next = NULL;
727 for (count = 0; (cp = getname (str)); count++)
728 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
732 tmpaddrs.m_next = mp;
736 if (hdr->flags & HTRY)
742 if (hdr->flags & HNIL)
743 fprintf (out, "%s: %s", name, str);
746 advise (NULL, "%s: field requires at least one address", name);
753 nameoutput = linepos = 0;
754 snprintf (namep, sizeof(namep), "%s%s",
755 !fill_in && (hdr->flags & HMNG) ? "Original-" : "", name);
757 for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
758 if (mp->m_nohost) { /* also used to test (hdr->flags & HTRY) */
759 /* The address doesn't include a host, so it might be an alias. */
760 pp = akvalue (mp->m_mbox); /* do mh alias substitution */
761 qp = akvisible () ? mp->m_mbox : "";
764 putgrp (namep, np->m_gname, out, hdr->flags);
765 while ((cp = getname (pp))) {
766 if (!(mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
771 if (draft_from_masquerading && ((msgstate == RESENT)
773 : (hdr->set & MFRM)))
774 /* The user manually specified a [Resent-]From: address in
775 their draft and the "masquerade:" line in mts.conf
776 doesn't contain "draft_from", so we'll set things up to
777 use the actual email address embedded in the draft
778 [Resent-]From: (after alias substitution, and without the
779 GECOS full name or angle brackets) as the envelope
781 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
783 if (hdr->flags & HBCC)
786 mp->m_ingrp = np->m_ingrp;
789 putgrp (namep, mp->m_gname, out, hdr->flags);
792 if (putadr (namep, qp, mp, out, hdr->flags))
793 msgflags |= (hdr->set & (MVIS | MINV));
802 /* Address includes a host, so no alias substitution is needed. */
803 if (draft_from_masquerading && ((msgstate == RESENT)
805 : (hdr->set & MFRM)))
806 /* The user manually specified a [Resent-]From: address in
807 their draft and the "masquerade:" line in mts.conf
808 doesn't contain "draft_from", so we'll set things up to
809 use the actual email address embedded in the draft
810 [Resent-]From: (after alias substitution, and without the
811 GECOS full name or angle brackets) as the envelope
813 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
815 if (hdr->flags & HBCC)
818 putgrp (namep, mp->m_gname, out, hdr->flags);
821 keep = putadr (namep, "", mp, out, hdr->flags);
825 msgflags |= (hdr->set & (MVIS | MINV));
831 if (grp > 0 && (hdr->flags & HNGR)) {
832 advise (NULL, "%s: field does not allow groups", name);
836 if (fill_in && grp > 0)
847 char myhost[BUFSIZ], sigbuf[BUFSIZ];
854 strncpy (from, adrsprintf (NULL, NULL), sizeof(from));
855 strncpy (myhost, LocalName (), sizeof(myhost));
857 for (cp = myhost; *cp; cp++)
860 if ((cp = getfullname ()) && *cp) {
861 strncpy (sigbuf, cp, sizeof(sigbuf));
862 snprintf (signature, sizeof(signature), "%s <%s>",
863 sigbuf, adrsprintf (NULL, NULL));
864 if ((cp = getname (signature)) == NULL)
865 adios (NULL, "getname () failed -- you lose extraordinarily big");
866 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL)) == NULL)
867 adios (NULL, "bad signature '%s'", sigbuf);
872 strncpy (signature, adrsprintf (NULL, NULL), sizeof(signature));
878 * Now that we've outputted the header fields in the draft
879 * message, we will now output any remaining header fields
880 * that we need to add/create.
884 finish_headers (FILE *out)
888 if (whomsw && !fill_up)
891 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
893 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
894 (int) getpid (), (long) tclock, LocalName ());
895 if (msgflags & MFRM) {
896 /* There was already a From: in the draft. Don't add one. */
897 if (!draft_from_masquerading)
898 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
899 so we'll reveal the user's actual account@thismachine
900 address in a Sender: header (and use it as the envelope
902 fprintf (out, "Sender: %s\n", from);
905 /* Construct a From: header. */
906 fprintf (out, "From: %s\n", signature);
910 if (!(msgflags & MVIS))
911 fprintf (out, "Bcc: Blind Distribution List: ;\n");
915 if (!(msgflags & MDAT)) {
916 advise (NULL, "message has no Date: header");
919 if (!(msgflags & MFRM)) {
920 advise (NULL, "message has no From: header");
923 if (whomsw && !fill_up)
926 #ifdef MMDFI /* sigh */
927 fprintf (out, "Sender: %s\n", from);
930 fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
932 fprintf (out, "Resent-Message-ID: <%d.%ld@%s>\n",
933 (int) getpid (), (long) tclock, LocalName ());
934 if (msgflags & MRFM) {
935 /* There was already a Resent-From: in draft. Don't add one. */
936 if (!draft_from_masquerading)
937 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
938 so we'll reveal the user's actual account@thismachine
939 address in a Sender: header (and use it as the envelope
941 fprintf (out, "Resent-Sender: %s\n", from);
944 /* Construct a Resent-From: header. */
945 fprintf (out, "Resent-From: %s\n", signature);
948 if (!(msgflags & MVIS))
949 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
954 adios (NULL, "re-format message and try again");
956 adios (NULL, "no addressees");
961 get_header (char *header, struct headers *table)
965 for (h = table; h->value; h++)
966 if (!strcasecmp (header, h->value))
974 putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flags)
980 if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
982 if ((!fill_in && (flags & (HBCC | HDCC))) || mp->m_ingrp)
986 fprintf (out, "%s: ", name);
987 linepos += (nameoutput = strlen (name) + 2);
990 if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
991 mp->m_pers = getcpy (aka);
993 if (mp->m_gname && !fill_in) {
994 snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
1004 if (linepos != nameoutput) {
1005 if (len + linepos + 2 > outputlinelen)
1006 fprintf (out, ",\n%*s", linepos = nameoutput, "");
1016 return (flags & HTRY);
1021 putgrp (char *name, char *group, FILE *out, unsigned int flags)
1026 if (!fill_in && (flags & HBCC))
1030 fprintf (out, "%s: ", name);
1031 linepos += (nameoutput = strlen (name) + 2);
1033 linepos -= strlen (group);
1036 cp = fill_in ? group : concat (group, ";", NULL);
1039 if (linepos > nameoutput) {
1040 if (len + linepos + 2 > outputlinelen) {
1041 fprintf (out, ",\n%*s", nameoutput, "");
1042 linepos = nameoutput;
1056 insert (struct mailname *np)
1058 struct mailname *mp;
1060 if (np->m_mbox == NULL)
1063 for (mp = np->m_type == LOCALHOST ? &localaddrs
1064 : np->m_type == UUCPHOST ? &uuaddrs
1068 if (!strcasecmp (np->m_host, mp->m_next->m_host)
1069 && !strcasecmp (np->m_mbox, mp->m_next->m_mbox)
1070 && np->m_bcc == mp->m_next->m_bcc)
1083 struct mailname *mp;
1085 printf ("-------\n\t-- Addresses --\nlocal:\t");
1086 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1087 printf ("%s%s%s", mp->m_mbox,
1088 mp->m_bcc ? "[BCC]" : "",
1089 mp->m_next ? ",\n\t" : "");
1091 printf ("\nnet:\t");
1092 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1093 printf ("%s%s@%s%s%s", mp->m_path ? mp->m_path : "",
1094 mp->m_mbox, mp->m_host,
1095 mp->m_bcc ? "[BCC]" : "",
1096 mp->m_next ? ",\n\t" : "");
1098 printf ("\nuucp:\t");
1099 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1100 printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1101 mp->m_bcc ? "[BCC]" : "",
1102 mp->m_next ? ",\n\t" : "");
1104 printf ("\n\t-- Folder Copies --\nfcc:\t");
1105 for (i = 0; i < fccind; i++)
1106 printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1114 struct mailname *mp;
1116 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1117 if (annoaux (mp) == NOTOK)
1120 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1121 if (annoaux (mp) == NOTOK)
1124 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1125 if (annoaux (mp) == NOTOK)
1135 annoaux (struct mailname *mp)
1138 char buffer[BUFSIZ];
1140 snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1141 i = strlen (buffer);
1143 return (write (pfd, buffer, i) == i ? OK : NOTOK);
1148 insert_fcc (struct headers *hdr, char *pp)
1152 for (cp = pp; isspace (*cp); cp++)
1154 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
1162 adios (NULL, "too many %ss", hdr->value);
1163 fccfold[fccind++] = getcpy (cp);
1171 make_bcc_file (int dashstuff)
1178 strncpy (bccfil, m_tmpfil ("bccs"), sizeof(bccfil));
1179 if ((out = fopen (bccfil, "w")) == NULL)
1180 adios (bccfil, "unable to create");
1181 chmod (bccfil, 0600);
1183 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1185 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
1186 (int) getpid (), (long) tclock, LocalName ());
1187 fprintf (out, "From: %s\n", signature);
1189 fprintf (out, "Subject: %s", subject);
1190 fprintf (out, "BCC:\n");
1193 * Use MIME encapsulation for Bcc messages
1199 * Check if any lines in the message clash with the
1200 * prefix for the MIME multipart separator. If there
1201 * is a clash, increment one of the letters in the
1202 * prefix and check again.
1204 if ((cp = strchr(prefix, 'a')) == NULL)
1205 adios (NULL, "lost prefix start");
1206 while (find_prefix () == NOTOK) {
1211 adios (NULL, "can't find a unique delimiter string");
1216 fprintf (out, "%s: %s\n%s: multipart/digest; boundary=\"",
1217 VRSN_FIELD, VRSN_VALUE, TYPE_FIELD);
1218 fprintf (out, "%s\"\n\n--%s\n\n", prefix, prefix);
1220 fprintf (out, "\n------- Blind-Carbon-Copy\n\n");
1226 * Do mhl filtering of Bcc messages instead
1227 * of MIME encapsulation.
1229 if (filter != NULL) {
1230 vec[0] = r1bindex (mhlproc, '/');
1232 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1236 adios ("fork", "unable to");
1239 dup2 (fileno (out), 1);
1242 vec[i++] = "-forward";
1247 /* was the flag -[no]dashstuffing specified? */
1249 vec[i++] = "-dashstuffing";
1250 else if (dashstuff < 0)
1251 vec[i++] = "-nodashstuffing";
1254 execvp (mhlproc, vec);
1255 fprintf (stderr, "unable to exec ");
1260 pidXwait (child_id, mhlproc);
1264 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
1265 adios (tmpfil, "unable to re-open");
1268 * If using MIME encapsulation, or if the -nodashstuffing
1269 * flag was given, then just copy message. Else do
1270 * RFC934 quoting (dashstuffing).
1272 if (mime || dashstuff < 0)
1273 cpydata (fd, fileno (out), tmpfil, bccfil);
1275 cpydgst (fd, fileno (out), tmpfil, bccfil);
1279 fseek (out, 0L, SEEK_END);
1281 fprintf (out, "\n--%s--\n", prefix);
1283 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
1289 * Scan message to check if any lines clash with
1290 * the prefix of the MIME multipart separator.
1297 char buffer[BUFSIZ];
1300 if ((in = fopen (tmpfil, "r")) == NULL)
1301 adios (tmpfil, "unable to re-open");
1303 len = strlen (prefix);
1306 while (fgets (buffer, sizeof(buffer) - 1, in))
1307 if (buffer[0] == '-' && buffer[1] == '-') {
1310 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1314 if (strcmp (buffer + 2, prefix) == 0) {
1325 #define plural(x) (x == 1 ? "" : "s")
1330 if (badadr && unkadr)
1331 die (NULL, "%d address%s unparsable, %d addressee%s undeliverable",
1332 badadr, plural (badadr), unkadr, plural (badadr));
1334 die (NULL, "%d address%s unparsable", badadr, plural (badadr));
1336 die (NULL, "%d addressee%s undeliverable", unkadr, plural (unkadr));
1341 do_addresses (int bccque, int talk)
1345 struct mailname *lp;
1348 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1349 if (lp->m_bcc ? bccque : !bccque) {
1351 printf (" -- Local Recipients --\n");
1352 do_an_address (lp, talk);
1357 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1358 if (lp->m_bcc ? bccque : !bccque) {
1360 printf (" -- UUCP Recipients --\n");
1361 do_an_address (lp, talk);
1366 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1367 if (lp->m_bcc ? bccque : !bccque) {
1369 printf (" -- Network Recipients --\n");
1370 do_an_address (lp, talk);
1377 if (rp_isbad (retval = mm_waend ()))
1378 die (NULL, "problem ending addresses [%s]\n", rp_valstr (retval));
1379 #endif /* MMDFMTS */
1382 if (rp_isbad (retval = sm_waend ()))
1383 die (NULL, "problem ending addresses; %s", rp_string (retval));
1384 #endif /* SENDMTS */
1389 * MTS-SPECIFIC INTERACTION
1394 * SENDMAIL/SMTP routines
1400 post (char *file, int bccque, int talk)
1405 onex = !(msgflags & MINV) || bccque;
1407 if (msgflags & MINV)
1408 printf (" -- Posting for %s Recipients --\n",
1409 bccque ? "Blind" : "Sighted");
1411 printf (" -- Posting for All Recipients --\n");
1416 if (rp_isbad (retval = sm_init (clientsw, serversw, watch, verbose,
1417 snoop, onex, queued))
1418 || rp_isbad (retval = sm_winit (smtpmode, from)))
1419 die (NULL, "problem initializing server; %s", rp_string (retval));
1421 do_addresses (bccque, talk && verbose);
1422 if ((fd = open (file, O_RDONLY)) == NOTOK)
1423 die (file, "unable to re-open");
1428 sm_end (onex ? OK : DONE);
1432 if (msgflags & MINV)
1433 printf (" -- %s Recipient Copies Posted --\n",
1434 bccque ? "Blind" : "Sighted");
1436 printf (" -- Recipient Copies Posted --\n");
1443 /* Address Verification */
1446 verify_all_addresses (int talk)
1449 struct mailname *lp;
1453 if (!whomsw || checksw)
1454 if (rp_isbad (retval = sm_init (clientsw, serversw, 0, 0, snoop, 0, 0))
1455 || rp_isbad (retval = sm_winit (smtpmode, from)))
1456 die (NULL, "problem initializing server; %s", rp_string (retval));
1458 if (talk && !whomsw)
1459 printf (" -- Address Verification --\n");
1460 if (talk && localaddrs.m_next)
1461 printf (" -- Local Recipients --\n");
1462 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1463 do_an_address (lp, talk);
1465 if (talk && uuaddrs.m_next)
1466 printf (" -- UUCP Recipients --\n");
1467 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1468 do_an_address (lp, talk);
1470 if (talk && netaddrs.m_next)
1471 printf (" -- Network Recipients --\n");
1472 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1473 do_an_address (lp, talk);
1476 if (talk && !whomsw)
1477 printf (" -- Address Verification Successful --\n");
1479 if (!whomsw || checksw)
1488 do_an_address (struct mailname *lp, int talk)
1494 switch (lp->m_type) {
1498 strncpy (addr, mbox, sizeof(addr));
1502 mbox = auxformat (lp, 0);
1504 snprintf (addr, sizeof(addr), "%s!%s", lp->m_host, lp->m_mbox);
1507 default: /* let SendMail decide if the host is bad */
1510 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1515 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1517 if (whomsw && !checksw) {
1525 switch (retval = sm_wadr (mbox, host,
1526 lp->m_type != UUCPHOST ? lp->m_path : NULL)) {
1529 printf ("address ok\n");
1535 fprintf (stderr, " %s: ", addr);
1536 fprintf (talk ? stdout : stderr, "loses; %s\n",
1537 rp_string (retval));
1543 fprintf (stderr, " %s: ", addr);
1544 die (NULL, "unexpected response; %s", rp_string (retval));
1552 do_text (char *file, int fd)
1557 lseek (fd, (off_t) 0, SEEK_SET);
1559 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1560 if (rp_isbad (retval = sm_wtxt (buf, state)))
1561 die (NULL, "problem writing text; %s\n", rp_string (retval));
1565 die (file, "problem reading from");
1567 switch (retval = sm_wtend ()) {
1573 die (NULL, "posting failed; %s", rp_string (retval));
1576 die (NULL, "unexpected response; %s", rp_string (retval));
1580 #endif /* SENDMTS */
1589 post (char *file, int bccque, int talk)
1595 struct rp_bufstruct reply;
1598 onex = !(msgflags & MINV) || bccque;
1600 if (msgflags & MINV)
1601 printf (" -- Posting for %s Recipients --\n",
1602 bccque ? "Blind" : "Sighted");
1604 printf (" -- Posting for All Recipients --\n");
1609 if (rp_isbad (retval = mm_init ())
1610 || rp_isbad (retval = mm_sbinit ())
1611 || rp_isbad (retval = mm_winit (NULL, submitopts, from)))
1612 die (NULL, "problem initializing MMDF system [%s]",
1613 rp_valstr (retval));
1615 if (rp_isbad (retval = mm_rrply (&reply, &len)))
1616 die (NULL, "problem with sender address [%s]",
1617 rp_valstr (retval));
1620 do_addresses (bccque, talk && verbose);
1621 if ((fd = open (file, O_RDONLY)) == NOTOK)
1622 die (file, "unable to re-open");
1632 if (msgflags & MINV)
1633 printf (" -- %s Recipient Copies Posted --\n",
1634 bccque ? "Blind" : "Sighted");
1636 printf (" -- Recipient Copies Posted --\n");
1641 /* Address Verification */
1644 verify_all_addresses (int talk)
1647 struct mailname *lp;
1651 struct rp_bufstruct reply;
1656 if (!whomsw || checksw) {
1657 if (rp_isbad (retval = mm_init ())
1658 || rp_isbad (retval = mm_sbinit ())
1659 || rp_isbad (retval = mm_winit (NULL, submitopts, from)))
1660 die (NULL, "problem initializing MMDF system [%s]",
1661 rp_valstr (retval));
1663 if (rp_isbad (retval = mm_rrply (&reply, &len)))
1664 die (NULL, "problem with sender address [%s]", rp_valstr (retval));
1668 if (talk && !whomsw)
1669 printf (" -- Address Verification --\n");
1670 if (talk && localaddrs.m_next)
1671 printf (" -- Local Recipients --\n");
1672 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1673 do_an_address (lp, talk);
1675 if (talk && uuaddrs.m_next)
1676 printf (" -- UUCP Recipients --\n");
1677 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1678 do_an_address (lp, talk);
1680 if (talk && netaddrs.m_next)
1681 printf (" -- Network Recipients --\n");
1682 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1683 do_an_address (lp, talk);
1686 if (talk && !whomsw)
1687 printf (" -- Address Verification Successful --\n");
1689 if (!whomsw || checksw)
1698 do_an_address (struct mailname *lp, int talk)
1701 char *mbox, *host, *text, *path;
1703 struct rp_bufstruct reply;
1705 switch (lp->m_type) {
1708 host = LocalName ();
1709 strncpy (addr, mbox, sizeof(addr));
1713 fprintf (talk ? stdout : stderr, " %s!%s: %s\n",
1714 lp->m_host, lp->m_mbox, "not supported; UUCP address");
1719 default: /* let MMDF decide if the host is bad */
1722 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1727 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1729 if (whomsw && !checksw) {
1739 path = concat (lp->m_path, mbox, "@", host, NULL);
1743 if (rp_isbad (retval = mm_wadr (path ? NULL : host, path ? path : mbox))
1744 || rp_isbad (retval = mm_rrply (&reply, &len)))
1745 die (NULL, "problem submitting address [%s]", rp_valstr (retval));
1747 switch (rp_gval (reply.rp_val)) {
1750 printf ("address ok\n");
1757 printf ("nameserver timeout - queued for checking\n");
1768 text = "temporary nameserver failure";
1775 text = "not deliverable";
1779 text = "try again later";
1783 text = "nothing done";
1788 fprintf (stderr, " %s: ", addr);
1789 text = "unexpected response";
1790 die (NULL, "%s;\n [%s] -- %s", text,
1791 rp_valstr (reply.rp_val), reply.rp_line);
1795 fprintf (stderr, " %s: ", addr);
1796 fprintf (talk ? stdout : stderr, "%s;\n %s\n", text, reply.rp_line);
1804 do_text (char *file, int fd)
1808 struct rp_bufstruct reply;
1810 lseek (fd, (off_t) 0, SEEK_SET);
1812 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1813 if (rp_isbad (mm_wtxt (buf, state)))
1814 die (NULL, "problem writing text [%s]\n", rp_valstr (retval));
1818 die (file, "problem reading from");
1820 if (rp_isbad (retval = mm_wtend ()))
1821 die (NULL, "problem ending text [%s]\n", rp_valstr (retval));
1823 if (rp_isbad (retval = mm_rrply (&reply, &state)))
1824 die (NULL, "problem getting submission status [%s]\n",
1825 rp_valstr (retval));
1827 switch (rp_gval (reply.rp_val)) {
1833 die (NULL, "you lose; %s", reply.rp_line);
1836 die (NULL, "no delivery occurred; %s", reply.rp_line);
1839 die (NULL, "try again later; %s", reply.rp_line);
1842 die (NULL, "nothing done; %s", reply.rp_line);
1845 die (NULL, "unexpected response;\n\t[%s] -- %s",
1846 rp_valstr (reply.rp_val), reply.rp_line);
1850 #endif /* MMDFMTS */
1860 #ifndef RELIABLE_SIGNALS
1861 SIGNAL (i, SIG_IGN);
1865 if (msgflags & MINV)
1869 if (!whomsw || checksw)
1871 #endif /* MMDFMTS */
1874 if (!whomsw || checksw)
1876 #endif /* SENDMTS */
1888 hstat = SIGNAL2 (SIGHUP, sigser);
1889 istat = SIGNAL2 (SIGINT, sigser);
1890 qstat = SIGNAL2 (SIGQUIT, sigser);
1891 tstat = SIGNAL2 (SIGTERM, sigser);
1901 SIGNAL (SIGHUP, hstat);
1902 SIGNAL (SIGINT, istat);
1903 SIGNAL (SIGQUIT, qstat);
1904 SIGNAL (SIGTERM, tstat);
1912 p_refile (char *file)
1920 printf (" -- Filing Folder Copies --\n");
1921 for (i = 0; i < fccind; i++)
1922 fcc (file, fccfold[i]);
1924 printf (" -- Folder Copies Filed --\n");
1929 * Call the `fileproc' to add the file to the folder.
1933 fcc (char *file, char *folder)
1940 printf (" %sFcc %s: ", msgstate == RESENT ? "Resent-" : "", folder);
1943 for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
1949 fprintf (stderr, " %sFcc %s: ",
1950 msgstate == RESENT ? "Resent-" : "", folder);
1951 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
1955 /* see if we need to add `+' */
1956 snprintf (fold, sizeof(fold), "%s%s",
1957 *folder == '+' || *folder == '@' ? "" : "+", folder);
1959 /* now exec the fileproc */
1960 execlp (fileproc, r1bindex (fileproc, '/'),
1961 "-link", "-file", file, fold, NULL);
1965 if ((status = pidwait (child_id, OK))) {
1967 fprintf (stderr, " %sFcc %s: ",
1968 msgstate == RESENT ? "Resent-" : "", folder);
1969 pidstatus (status, verbose ? stdout : stderr, NULL);
1972 printf ("folder ok\n");
1984 die (char *what, char *fmt, ...)
1989 if (msgflags & MINV)
1993 if (!whomsw || checksw)
1995 #endif /* MMDFMTS */
1998 if (!whomsw || checksw)
2000 #endif /* SENDMTS */
2003 advertise (what, NULL, fmt, ap);
2011 * err_abrt() is used by the mm_ routines
2012 * do not, under *ANY* circumstances, remove it from post,
2013 * or you will lose *BIG*
2017 err_abrt (int code, char *fmt, ...)
2019 char buffer[BUFSIZ];
2022 snprintf (buffer, sizeof(buffer), "[%s]", rp_valstr (code));
2025 advertise (buffer, NULL, fmt, ap);
2030 #endif /* MMDFMTS */