3 * post.c -- enter messages into the mail transport system
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 #include <h/signals.h>
15 #include <h/addrsbr.h>
16 #include <h/aliasbr.h>
17 #include <h/dropsbr.h>
27 #ifdef TIME_WITH_SYS_TIME
28 # include <sys/time.h>
31 # ifdef TM_IN_SYS_TIME
32 # include <sys/time.h>
39 # include <mts/mmdf/util.h>
40 # include <mts/mmdf/mmdf.h>
44 # include <mts/smtp/smtp.h>
48 # define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : (c))
52 # define SASLminc(a) (a)
53 #else /* CYRUS_SASL */
54 # define SASLminc(a) 0
55 #endif /* CYRUS_SASL */
57 #define FCCS 10 /* max number of fccs allowed */
59 /* In the following array of structures, the numeric second field of the
60 structures (minchars) is apparently used like this:
62 -# : Switch can be abbreviated to # characters; switch hidden in -help.
63 0 : Switch can't be abbreviated; switch shown in -help.
64 # : Switch can be abbreviated to # characters; switch shown in -help. */
66 static struct swit switches[] = {
68 { "alias aliasfile", 0 },
70 { "check", -5 }, /* interface from whom */
72 { "nocheck", -7 }, /* interface from whom */
76 { "dist", -4 }, /* interface from dist */
78 { "filter filterfile", 0 },
86 { "library directory", -7 }, /* interface from send, whom */
104 { "whom", -4 }, /* interface from whom */
106 { "width columns", 0 },
111 #define BITSTUFFSW 22
112 { "dashstuffing", -12 }, /* should we dashstuff BCC messages? */
113 #define NBITSTUFFSW 23
114 { "nodashstuffing", -14 },
116 { "mail", -4 }, /* specify MAIL smtp mode */
118 { "saml", -4 }, /* specify SAML smtp mode */
120 { "send", -4 }, /* specify SEND smtp mode */
122 { "soml", -4 }, /* specify SOML smtp mode */
124 { "idanno number", -6 }, /* interface from send */
126 { "deliver address-list", -7 },
128 { "client host", -6 },
130 { "server host", -6 }, /* specify alternate SMTP server */
132 { "snoop", -5 }, /* snoop the SMTP transaction */
134 { "fill-in file", -7 },
142 { "sasl", SASLminc(-4) },
143 #define SASLMECHSW 38
144 { "saslmech", SASLminc(-5) },
146 { "user", SASLminc(-4) },
158 * flags for headers->flags
160 #define HNOP 0x0000 /* just used to keep .set around */
161 #define HBAD 0x0001 /* bad header - don't let it through */
162 #define HADR 0x0002 /* header has an address field */
163 #define HSUB 0x0004 /* Subject: header */
164 #define HTRY 0x0008 /* try to send to addrs on header */
165 #define HBCC 0x0010 /* don't output this header */
166 #define HMNG 0x0020 /* munge this header */
167 #define HNGR 0x0040 /* no groups allowed in this header */
168 #define HFCC 0x0080 /* FCC: type header */
169 #define HNIL 0x0100 /* okay for this header not to have addrs */
170 #define HIGN 0x0200 /* ignore this header */
171 #define HDCC 0x0400 /* another undocumented feature */
174 * flags for headers->set
176 #define MFRM 0x0001 /* we've seen a From: */
177 #define MDAT 0x0002 /* we've seen a Date: */
178 #define MRFM 0x0004 /* we've seen a Resent-From: */
179 #define MVIS 0x0008 /* we've seen sighted addrs */
180 #define MINV 0x0010 /* we've seen blind addrs */
183 static struct headers NHeaders[] = {
184 { "Return-Path", HBAD, 0 },
185 { "Received", HBAD, 0 },
186 { "Reply-To", HADR|HNGR, 0 },
187 { "From", HADR|HNGR, MFRM },
188 { "Sender", HADR|HBAD, 0 },
190 { "Subject", HSUB, 0 },
191 { "To", HADR|HTRY, MVIS },
192 { "cc", HADR|HTRY, MVIS },
193 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
194 { "Dcc", HADR|HTRY|HDCC|HNIL, MVIS }, /* sorta cc & bcc combined */
195 { "Message-ID", HBAD, 0 },
200 static struct headers RHeaders[] = {
201 { "Resent-Reply-To", HADR|HNGR, 0 },
202 { "Resent-From", HADR|HNGR, MRFM },
203 { "Resent-Sender", HADR|HBAD, 0 },
204 { "Resent-Date", HBAD, 0 },
205 { "Resent-Subject", HSUB, 0 },
206 { "Resent-To", HADR|HTRY, MVIS },
207 { "Resent-cc", HADR|HTRY, MVIS },
208 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
209 { "Resent-Message-ID", HBAD, 0 },
210 { "Resent-Fcc", HFCC, 0 },
211 { "Reply-To", HADR, 0 },
212 { "From", HADR|HNGR, MFRM },
214 { "Sender", HADR|HNGR|HMNG, 0 },
216 { "Sender", HADR|HNGR, 0 },
218 { "Date", HNOP, MDAT },
219 { "To", HADR|HNIL, 0 },
220 { "cc", HADR|HNIL, 0 },
221 { "Bcc", HADR|HTRY|HBCC|HNIL, 0 },
226 static short fccind = 0; /* index into fccfold[] */
227 static short outputlinelen = OUTPUTLINELEN;
229 static int pfd = NOTOK; /* fd to write annotation list to */
230 static uid_t myuid= -1; /* my user id */
231 static gid_t mygid= -1; /* my group id */
232 static int recipients = 0; /* how many people will get a copy */
233 static int unkadr = 0; /* how many of those were unknown */
234 static int badadr = 0; /* number of bad addrs */
235 static int badmsg = 0; /* message has bad semantics */
236 static int verbose = 0; /* spell it out */
237 static int format = 1; /* format addresses */
238 static int mime = 0; /* use MIME-style encapsulations for Bcc */
239 static int msgid = 0; /* add msgid */
240 static int debug = 0; /* debugging post */
241 static int watch = 0; /* watch the delivery process */
242 static int whomsw = 0; /* we are whom not post */
243 static int checksw = 0; /* whom -check */
244 static int linepos=0; /* putadr()'s position on the line */
245 static int nameoutput=0; /* putadr() has output header name */
246 static int sasl=0; /* Use SASL auth for SMTP */
247 static char *saslmech=NULL; /* Force use of particular SASL mech */
248 static char *user=NULL; /* Authenticate as this user */
250 static unsigned msgflags = 0; /* what we've seen */
254 static int msgstate = NORMAL;
256 static time_t tclock = 0; /* the time we started (more or less) */
258 static SIGNAL_HANDLER hstat, istat, qstat, tstat;
260 static char tmpfil[BUFSIZ];
261 static char bccfil[BUFSIZ];
263 static char from[BUFSIZ]; /* my network address */
264 static char signature[BUFSIZ]; /* my signature */
265 static char *filter = NULL; /* the filter for BCC'ing */
266 static char *subject = NULL; /* the subject field for BCC'ing */
267 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
269 static struct headers *hdrtab; /* table for the message we're doing */
271 static struct mailname localaddrs={NULL}; /* local addrs */
272 static struct mailname netaddrs={NULL}; /* network addrs */
273 static struct mailname uuaddrs={NULL}; /* uucp addrs */
274 static struct mailname tmpaddrs={NULL}; /* temporary queue */
277 static char *submitmode = "m"; /* deliver to mailbox only */
278 static char submitopts[6] = "vl"; /* initial options for submit */
282 static int snoop = 0;
283 static int smtpmode = S_MAIL;
284 static char *clientsw = NULL;
285 static char *serversw = NULL;
287 extern struct smtp sm_reply;
290 static char prefix[] = "----- =_aaaaaaaaaa";
292 static int fill_up = 0;
293 static char *fill_in = NULL;
294 static char *partno = NULL;
295 static int queued = 0;
297 extern boolean draft_from_masquerading; /* defined in mts.c */
302 static void putfmt (char *, char *, FILE *);
303 static void start_headers (void);
304 static void finish_headers (FILE *);
305 static int get_header (char *, struct headers *);
306 static int putadr (char *, char *, struct mailname *, FILE *, unsigned int);
307 static void putgrp (char *, char *, FILE *, unsigned int);
308 static int insert (struct mailname *);
309 static void pl (void);
310 static void anno (void);
311 static int annoaux (struct mailname *);
312 static void insert_fcc (struct headers *, char *);
313 static void make_bcc_file (int);
314 static void verify_all_addresses (int);
315 static void chkadr (void);
316 static void sigon (void);
317 static void sigoff (void);
318 static void p_refile (char *);
319 static void fcc (char *, char *);
320 static void die (char *, char *, ...);
321 static void post (char *, int, int);
322 static void do_text (char *file, int fd);
323 static void do_an_address (struct mailname *, int);
324 static void do_addresses (int, int);
325 static int find_prefix (void);
329 main (int argc, char **argv)
331 int state, compnum, dashstuff = 0;
332 char *cp, *msg = NULL, **argp, **arguments;
333 char buf[BUFSIZ], name[NAMESZ];
337 setlocale(LC_ALL, "");
339 invo_name = r1bindex (argv[0], '/');
341 /* foil search of user profile/context */
342 if (context_foil (NULL) == -1)
345 mts_init (invo_name);
346 arguments = getarguments (invo_name, argc, argv, 0);
349 #if defined(MMDFMTS) && defined(MMDFII)
350 mmdf_init (invo_name);
351 #endif /* MMDFMTS and MMDFII */
353 while ((cp = *argp++)) {
355 switch (smatch (++cp, switches)) {
357 ambigsw (cp, switches);
360 adios (NULL, "-%s unknown", cp);
363 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
364 print_help (buf, switches, 0);
367 print_version(invo_name);
371 if (!(cp = *argp++) || *cp == '-')
372 adios (NULL, "missing argument to %s", argp[-2]);
373 /* create a minimal context */
374 if (context_foil (cp) == -1)
379 if (!(cp = *argp++) || *cp == '-')
380 adios (NULL, "missing argument to %s", argp[-2]);
381 if ((state = alias (cp)) != AK_OK)
382 adios (NULL, "aliasing error in %s - %s",
383 cp, akerror (state));
402 if (!(filter = *argp++) || *filter == '-')
403 adios (NULL, "missing argument to %s", argp[-2]);
458 if (!(cp = *argp++) || *cp == '-')
459 adios (NULL, "missing argument to %s", argp[-2]);
460 if ((outputlinelen = atoi (cp)) < 10)
461 adios (NULL, "impossible width %d", outputlinelen);
465 if (!(cp = *argp++) || *cp == '-')
466 adios (NULL, "missing argument to %s", argp[-2]);
467 if ((pfd = atoi (cp)) <= 2)
468 adios (NULL, "bad argument %s %s", argp[-2], cp);
475 case SOMLSW: /* for right now, sigh... */
485 if (!(cp = *argp++) || *cp == '-')
486 adios (NULL, "missing argument to %s", argp[-2]);
492 if (!(cp = *argp++) || *cp == '-')
493 adios (NULL, "missing argument to %s", argp[-2]);
512 if (!(clientsw = *argp++) || *clientsw == '-')
513 adios (NULL, "missing argument to %s", argp[-2]);
516 if (!(serversw = *argp++) || *serversw == '-')
517 adios (NULL, "missing argument to %s", argp[-2]);
525 if (!(fill_in = *argp++) || *fill_in == '-')
526 adios (NULL, "missing argument to %s", argp[-2]);
532 if (!(partno = *argp++) || *partno == '-')
533 adios (NULL, "missing argument to %s", argp[-2]);
545 if (!(saslmech = *argp++) || *saslmech == '-')
546 adios (NULL, "missing argument to %s", argp[-2]);
550 if (!(user = *argp++) || *user == '-')
551 adios (NULL, "missing argument to %s", argp[-2]);
556 adios (NULL, "only one message at a time!");
564 adios (NULL, "usage: %s [switches] file", invo_name);
566 if (outputlinelen < 10)
567 adios (NULL, "impossible width %d", outputlinelen);
569 if ((in = fopen (msg, "r")) == NULL)
570 adios (msg, "unable to open");
575 discard (out = stdout); /* XXX: reference discard() to help loader */
578 if ((out = fopen (fill_in ? fill_in : "/dev/null", "w")) == NULL)
579 adios ("/dev/null", "unable to open");
581 strncpy (tmpfil, m_scratch ("", m_maildir (invo_name)),
583 if ((out = fopen (tmpfil, "w")) == NULL) {
584 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
585 if ((out = fopen (tmpfil, "w")) == NULL)
586 adios (tmpfil, "unable to create");
588 chmod (tmpfil, 0600);
592 hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
594 for (compnum = 1, state = FLD;;) {
595 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
600 cp = add (buf, NULL);
601 while (state == FLDPLUS) {
602 state = m_getfld (state, name, buf, sizeof(buf), in);
605 putfmt (name, cp, out);
609 finish_headers (out);
614 finish_headers (out);
615 if (whomsw && !fill_in)
617 fprintf (out, "\n%s", buf);
618 while (state == BODY) {
619 state = m_getfld (state, name, buf, sizeof(buf), in);
625 finish_headers (out);
630 adios (NULL, "message format error in component #%d", compnum);
633 adios (NULL, "getfld() returned %d", state);
649 /* If we are doing a "whom" check */
652 verify_all_addresses (1);
657 strcat (submitopts, submitmode);
659 strcat (submitopts, "nw");
662 if (msgflags & MINV) {
663 make_bcc_file (dashstuff);
664 if (msgflags & MVIS) {
665 verify_all_addresses (verbose);
666 post (tmpfil, 0, verbose);
668 post (bccfil, 1, verbose);
671 post (tmpfil, 0, isatty (1));
678 printf (partno ? "Partial Message #%s Processed\n" : "Message Processed\n",
689 putfmt (char *name, char *str, FILE *out)
691 int count, grp, i, keep;
694 struct mailname *mp, *np;
697 while (*str == ' ' || *str == '\t')
700 if (msgstate == NORMAL && uprf (name, "resent")) {
701 advise (NULL, "illegal header line -- %s:", name);
706 if ((i = get_header (name, hdrtab)) == NOTOK) {
707 fprintf (out, "%s: %s", name, str);
712 if (hdr->flags & HIGN) {
714 fprintf (out, "%s: %s", name, str);
717 if (hdr->flags & HBAD) {
719 fprintf (out, "%s: %s", name, str);
721 advise (NULL, "illegal header line -- %s:", name);
726 msgflags |= (hdr->set & ~(MVIS | MINV));
728 if (hdr->flags & HSUB)
729 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
730 if (hdr->flags & HFCC) {
732 fprintf (out, "%s: %s", name, str);
736 if ((cp = strrchr(str, '\n')))
738 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
740 insert_fcc (hdr, pp);
742 insert_fcc (hdr, pp);
746 if (!(hdr->flags & HADR)) {
747 fprintf (out, "%s: %s", name, str);
751 tmpaddrs.m_next = NULL;
752 for (count = 0; (cp = getname (str)); count++)
753 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
757 tmpaddrs.m_next = mp;
761 if (hdr->flags & HTRY)
767 if (hdr->flags & HNIL)
768 fprintf (out, "%s: %s", name, str);
771 advise (NULL, "%s: field requires at least one address", name);
778 nameoutput = linepos = 0;
779 snprintf (namep, sizeof(namep), "%s%s",
780 !fill_in && (hdr->flags & HMNG) ? "Original-" : "", name);
782 for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
783 if (mp->m_nohost) { /* also used to test (hdr->flags & HTRY) */
784 /* The address doesn't include a host, so it might be an alias. */
785 pp = akvalue (mp->m_mbox); /* do mh alias substitution */
786 qp = akvisible () ? mp->m_mbox : "";
789 putgrp (namep, np->m_gname, out, hdr->flags);
790 while ((cp = getname (pp))) {
791 if (!(mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
796 if (draft_from_masquerading && ((msgstate == RESENT)
798 : (hdr->set & MFRM)))
799 /* The user manually specified a [Resent-]From: address in
800 their draft and the "masquerade:" line in mts.conf
801 doesn't contain "draft_from", so we'll set things up to
802 use the actual email address embedded in the draft
803 [Resent-]From: (after alias substitution, and without the
804 GECOS full name or angle brackets) as the envelope
806 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
808 if (hdr->flags & HBCC)
811 mp->m_ingrp = np->m_ingrp;
814 putgrp (namep, mp->m_gname, out, hdr->flags);
817 if (putadr (namep, qp, mp, out, hdr->flags))
818 msgflags |= (hdr->set & (MVIS | MINV));
827 /* Address includes a host, so no alias substitution is needed. */
828 if (draft_from_masquerading && ((msgstate == RESENT)
830 : (hdr->set & MFRM)))
831 /* The user manually specified a [Resent-]From: address in
832 their draft and the "masquerade:" line in mts.conf
833 doesn't contain "draft_from", so we'll set things up to
834 use the actual email address embedded in the draft
835 [Resent-]From: (after alias substitution, and without the
836 GECOS full name or angle brackets) as the envelope
838 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
840 if (hdr->flags & HBCC)
843 putgrp (namep, mp->m_gname, out, hdr->flags);
846 keep = putadr (namep, "", mp, out, hdr->flags);
850 msgflags |= (hdr->set & (MVIS | MINV));
856 if (grp > 0 && (hdr->flags & HNGR)) {
857 advise (NULL, "%s: field does not allow groups", name);
861 if (fill_in && grp > 0)
872 char myhost[BUFSIZ], sigbuf[BUFSIZ];
879 strncpy (from, adrsprintf (NULL, NULL), sizeof(from));
880 strncpy (myhost, LocalName (), sizeof(myhost));
882 for (cp = myhost; *cp; cp++)
885 if ((cp = getfullname ()) && *cp) {
886 strncpy (sigbuf, cp, sizeof(sigbuf));
887 snprintf (signature, sizeof(signature), "%s <%s>",
888 sigbuf, adrsprintf (NULL, NULL));
889 if ((cp = getname (signature)) == NULL)
890 adios (NULL, "getname () failed -- you lose extraordinarily big");
891 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL)) == NULL)
892 adios (NULL, "bad signature '%s'", sigbuf);
897 strncpy (signature, adrsprintf (NULL, NULL), sizeof(signature));
903 * Now that we've outputted the header fields in the draft
904 * message, we will now output any remaining header fields
905 * that we need to add/create.
909 finish_headers (FILE *out)
913 if (whomsw && !fill_up)
916 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
918 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
919 (int) getpid (), (long) tclock, LocalName ());
920 if (msgflags & MFRM) {
921 /* There was already a From: in the draft. Don't add one. */
922 if (!draft_from_masquerading)
923 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
924 so we'll reveal the user's actual account@thismachine
925 address in a Sender: header (and use it as the envelope
927 fprintf (out, "Sender: %s\n", from);
930 /* Construct a From: header. */
931 fprintf (out, "From: %s\n", signature);
935 if (!(msgflags & MVIS))
936 fprintf (out, "Bcc: Blind Distribution List: ;\n");
940 if (!(msgflags & MDAT)) {
941 advise (NULL, "message has no Date: header");
944 if (!(msgflags & MFRM)) {
945 advise (NULL, "message has no From: header");
948 if (whomsw && !fill_up)
951 #ifdef MMDFI /* sigh */
952 fprintf (out, "Sender: %s\n", from);
955 fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
957 fprintf (out, "Resent-Message-ID: <%d.%ld@%s>\n",
958 (int) getpid (), (long) tclock, LocalName ());
959 if (msgflags & MRFM) {
960 /* There was already a Resent-From: in draft. Don't add one. */
961 if (!draft_from_masquerading)
962 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
963 so we'll reveal the user's actual account@thismachine
964 address in a Sender: header (and use it as the envelope
966 fprintf (out, "Resent-Sender: %s\n", from);
969 /* Construct a Resent-From: header. */
970 fprintf (out, "Resent-From: %s\n", signature);
973 if (!(msgflags & MVIS))
974 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
979 adios (NULL, "re-format message and try again");
981 adios (NULL, "no addressees");
986 get_header (char *header, struct headers *table)
990 for (h = table; h->value; h++)
991 if (!strcasecmp (header, h->value))
999 putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flags)
1003 char buffer[BUFSIZ];
1005 if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
1007 if ((!fill_in && (flags & (HBCC | HDCC))) || mp->m_ingrp)
1011 fprintf (out, "%s: ", name);
1012 linepos += (nameoutput = strlen (name) + 2);
1015 if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
1016 mp->m_pers = getcpy (aka);
1018 if (mp->m_gname && !fill_in) {
1019 snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
1022 cp = adrformat (mp);
1029 if (linepos != nameoutput) {
1030 if (len + linepos + 2 > outputlinelen)
1031 fprintf (out, ",\n%*s", linepos = nameoutput, "");
1041 return (flags & HTRY);
1046 putgrp (char *name, char *group, FILE *out, unsigned int flags)
1051 if (!fill_in && (flags & HBCC))
1055 fprintf (out, "%s: ", name);
1056 linepos += (nameoutput = strlen (name) + 2);
1058 linepos -= strlen (group);
1061 cp = fill_in ? group : concat (group, ";", NULL);
1064 if (linepos > nameoutput) {
1065 if (len + linepos + 2 > outputlinelen) {
1066 fprintf (out, ",\n%*s", nameoutput, "");
1067 linepos = nameoutput;
1081 insert (struct mailname *np)
1083 struct mailname *mp;
1085 if (np->m_mbox == NULL)
1088 for (mp = np->m_type == LOCALHOST ? &localaddrs
1089 : np->m_type == UUCPHOST ? &uuaddrs
1093 if (!strcasecmp (np->m_host, mp->m_next->m_host)
1094 && !strcasecmp (np->m_mbox, mp->m_next->m_mbox)
1095 && np->m_bcc == mp->m_next->m_bcc)
1108 struct mailname *mp;
1110 printf ("-------\n\t-- Addresses --\nlocal:\t");
1111 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1112 printf ("%s%s%s", mp->m_mbox,
1113 mp->m_bcc ? "[BCC]" : "",
1114 mp->m_next ? ",\n\t" : "");
1116 printf ("\nnet:\t");
1117 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1118 printf ("%s%s@%s%s%s", mp->m_path ? mp->m_path : "",
1119 mp->m_mbox, mp->m_host,
1120 mp->m_bcc ? "[BCC]" : "",
1121 mp->m_next ? ",\n\t" : "");
1123 printf ("\nuucp:\t");
1124 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1125 printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1126 mp->m_bcc ? "[BCC]" : "",
1127 mp->m_next ? ",\n\t" : "");
1129 printf ("\n\t-- Folder Copies --\nfcc:\t");
1130 for (i = 0; i < fccind; i++)
1131 printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1139 struct mailname *mp;
1141 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1142 if (annoaux (mp) == NOTOK)
1145 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1146 if (annoaux (mp) == NOTOK)
1149 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1150 if (annoaux (mp) == NOTOK)
1160 annoaux (struct mailname *mp)
1163 char buffer[BUFSIZ];
1165 snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1166 i = strlen (buffer);
1168 return (write (pfd, buffer, i) == i ? OK : NOTOK);
1173 insert_fcc (struct headers *hdr, char *pp)
1177 for (cp = pp; isspace (*cp); cp++)
1179 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
1187 adios (NULL, "too many %ss", hdr->value);
1188 fccfold[fccind++] = getcpy (cp);
1196 make_bcc_file (int dashstuff)
1203 strncpy (bccfil, m_tmpfil ("bccs"), sizeof(bccfil));
1204 if ((out = fopen (bccfil, "w")) == NULL)
1205 adios (bccfil, "unable to create");
1206 chmod (bccfil, 0600);
1208 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1210 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
1211 (int) getpid (), (long) tclock, LocalName ());
1212 fprintf (out, "From: %s\n", signature);
1214 fprintf (out, "Subject: %s", subject);
1215 fprintf (out, "BCC:\n");
1218 * Use MIME encapsulation for Bcc messages
1224 * Check if any lines in the message clash with the
1225 * prefix for the MIME multipart separator. If there
1226 * is a clash, increment one of the letters in the
1227 * prefix and check again.
1229 if ((cp = strchr(prefix, 'a')) == NULL)
1230 adios (NULL, "lost prefix start");
1231 while (find_prefix () == NOTOK) {
1236 adios (NULL, "can't find a unique delimiter string");
1241 fprintf (out, "%s: %s\n%s: multipart/digest; boundary=\"",
1242 VRSN_FIELD, VRSN_VALUE, TYPE_FIELD);
1243 fprintf (out, "%s\"\n\n--%s\n\n", prefix, prefix);
1245 fprintf (out, "\n------- Blind-Carbon-Copy\n\n");
1251 * Do mhl filtering of Bcc messages instead
1252 * of MIME encapsulation.
1254 if (filter != NULL) {
1255 vec[0] = r1bindex (mhlproc, '/');
1257 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1261 adios ("fork", "unable to");
1264 dup2 (fileno (out), 1);
1267 vec[i++] = "-forward";
1272 /* was the flag -[no]dashstuffing specified? */
1274 vec[i++] = "-dashstuffing";
1275 else if (dashstuff < 0)
1276 vec[i++] = "-nodashstuffing";
1279 execvp (mhlproc, vec);
1280 fprintf (stderr, "unable to exec ");
1285 pidXwait (child_id, mhlproc);
1289 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
1290 adios (tmpfil, "unable to re-open");
1293 * If using MIME encapsulation, or if the -nodashstuffing
1294 * flag was given, then just copy message. Else do
1295 * RFC934 quoting (dashstuffing).
1297 if (mime || dashstuff < 0)
1298 cpydata (fd, fileno (out), tmpfil, bccfil);
1300 cpydgst (fd, fileno (out), tmpfil, bccfil);
1304 fseek (out, 0L, SEEK_END);
1306 fprintf (out, "\n--%s--\n", prefix);
1308 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
1314 * Scan message to check if any lines clash with
1315 * the prefix of the MIME multipart separator.
1322 char buffer[BUFSIZ];
1325 if ((in = fopen (tmpfil, "r")) == NULL)
1326 adios (tmpfil, "unable to re-open");
1328 len = strlen (prefix);
1331 while (fgets (buffer, sizeof(buffer) - 1, in))
1332 if (buffer[0] == '-' && buffer[1] == '-') {
1335 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1339 if (strcmp (buffer + 2, prefix) == 0) {
1350 #define plural(x) (x == 1 ? "" : "s")
1355 if (badadr && unkadr)
1356 die (NULL, "%d address%s unparsable, %d addressee%s undeliverable",
1357 badadr, plural (badadr), unkadr, plural (badadr));
1359 die (NULL, "%d address%s unparsable", badadr, plural (badadr));
1361 die (NULL, "%d addressee%s undeliverable", unkadr, plural (unkadr));
1366 do_addresses (int bccque, int talk)
1370 struct mailname *lp;
1373 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1374 if (lp->m_bcc ? bccque : !bccque) {
1376 printf (" -- Local Recipients --\n");
1377 do_an_address (lp, talk);
1382 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1383 if (lp->m_bcc ? bccque : !bccque) {
1385 printf (" -- UUCP Recipients --\n");
1386 do_an_address (lp, talk);
1391 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1392 if (lp->m_bcc ? bccque : !bccque) {
1394 printf (" -- Network Recipients --\n");
1395 do_an_address (lp, talk);
1402 if (rp_isbad (retval = mm_waend ()))
1403 die (NULL, "problem ending addresses [%s]\n", rp_valstr (retval));
1404 #endif /* MMDFMTS */
1407 if (rp_isbad (retval = sm_waend ()))
1408 die (NULL, "problem ending addresses; %s", rp_string (retval));
1409 #endif /* SMTPMTS */
1414 * MTS-SPECIFIC INTERACTION
1419 * SENDMAIL/SMTP routines
1425 post (char *file, int bccque, int talk)
1430 onex = !(msgflags & MINV) || bccque;
1432 if (msgflags & MINV)
1433 printf (" -- Posting for %s Recipients --\n",
1434 bccque ? "Blind" : "Sighted");
1436 printf (" -- Posting for All Recipients --\n");
1441 if (rp_isbad (retval = sm_init (clientsw, serversw, watch, verbose,
1442 snoop, onex, queued, sasl, saslmech,
1444 || rp_isbad (retval = sm_winit (smtpmode, from)))
1445 die (NULL, "problem initializing server; %s", rp_string (retval));
1447 do_addresses (bccque, talk && verbose);
1448 if ((fd = open (file, O_RDONLY)) == NOTOK)
1449 die (file, "unable to re-open");
1454 sm_end (onex ? OK : DONE);
1458 if (msgflags & MINV)
1459 printf (" -- %s Recipient Copies Posted --\n",
1460 bccque ? "Blind" : "Sighted");
1462 printf (" -- Recipient Copies Posted --\n");
1469 /* Address Verification */
1472 verify_all_addresses (int talk)
1475 struct mailname *lp;
1479 if (!whomsw || checksw)
1480 if (rp_isbad (retval = sm_init (clientsw, serversw, 0, 0, snoop, 0,
1482 || rp_isbad (retval = sm_winit (smtpmode, from)))
1483 die (NULL, "problem initializing server; %s", rp_string (retval));
1485 if (talk && !whomsw)
1486 printf (" -- Address Verification --\n");
1487 if (talk && localaddrs.m_next)
1488 printf (" -- Local Recipients --\n");
1489 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1490 do_an_address (lp, talk);
1492 if (talk && uuaddrs.m_next)
1493 printf (" -- UUCP Recipients --\n");
1494 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1495 do_an_address (lp, talk);
1497 if (talk && netaddrs.m_next)
1498 printf (" -- Network Recipients --\n");
1499 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1500 do_an_address (lp, talk);
1503 if (talk && !whomsw)
1504 printf (" -- Address Verification Successful --\n");
1506 if (!whomsw || checksw)
1515 do_an_address (struct mailname *lp, int talk)
1521 switch (lp->m_type) {
1525 strncpy (addr, mbox, sizeof(addr));
1529 mbox = auxformat (lp, 0);
1531 snprintf (addr, sizeof(addr), "%s!%s", lp->m_host, lp->m_mbox);
1534 default: /* let SendMail decide if the host is bad */
1537 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1542 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1544 if (whomsw && !checksw) {
1552 switch (retval = sm_wadr (mbox, host,
1553 lp->m_type != UUCPHOST ? lp->m_path : NULL)) {
1556 printf ("address ok\n");
1562 fprintf (stderr, " %s: ", addr);
1563 fprintf (talk ? stdout : stderr, "loses; %s\n",
1564 rp_string (retval));
1570 fprintf (stderr, " %s: ", addr);
1571 die (NULL, "unexpected response; %s", rp_string (retval));
1579 do_text (char *file, int fd)
1584 lseek (fd, (off_t) 0, SEEK_SET);
1586 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1587 if (rp_isbad (retval = sm_wtxt (buf, state)))
1588 die (NULL, "problem writing text; %s\n", rp_string (retval));
1592 die (file, "problem reading from");
1594 switch (retval = sm_wtend ()) {
1600 die (NULL, "posting failed; %s", rp_string (retval));
1603 die (NULL, "unexpected response; %s", rp_string (retval));
1607 #endif /* SMTPMTS */
1616 post (char *file, int bccque, int talk)
1622 struct rp_bufstruct reply;
1625 onex = !(msgflags & MINV) || bccque;
1627 if (msgflags & MINV)
1628 printf (" -- Posting for %s Recipients --\n",
1629 bccque ? "Blind" : "Sighted");
1631 printf (" -- Posting for All Recipients --\n");
1636 if (rp_isbad (retval = mm_init ())
1637 || rp_isbad (retval = mm_sbinit ())
1638 || rp_isbad (retval = mm_winit (NULL, submitopts, from)))
1639 die (NULL, "problem initializing MMDF system [%s]",
1640 rp_valstr (retval));
1642 if (rp_isbad (retval = mm_rrply (&reply, &len)))
1643 die (NULL, "problem with sender address [%s]",
1644 rp_valstr (retval));
1647 do_addresses (bccque, talk && verbose);
1648 if ((fd = open (file, O_RDONLY)) == NOTOK)
1649 die (file, "unable to re-open");
1659 if (msgflags & MINV)
1660 printf (" -- %s Recipient Copies Posted --\n",
1661 bccque ? "Blind" : "Sighted");
1663 printf (" -- Recipient Copies Posted --\n");
1668 /* Address Verification */
1671 verify_all_addresses (int talk)
1674 struct mailname *lp;
1678 struct rp_bufstruct reply;
1683 if (!whomsw || checksw) {
1684 if (rp_isbad (retval = mm_init ())
1685 || rp_isbad (retval = mm_sbinit ())
1686 || rp_isbad (retval = mm_winit (NULL, submitopts, from)))
1687 die (NULL, "problem initializing MMDF system [%s]",
1688 rp_valstr (retval));
1690 if (rp_isbad (retval = mm_rrply (&reply, &len)))
1691 die (NULL, "problem with sender address [%s]", rp_valstr (retval));
1695 if (talk && !whomsw)
1696 printf (" -- Address Verification --\n");
1697 if (talk && localaddrs.m_next)
1698 printf (" -- Local Recipients --\n");
1699 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1700 do_an_address (lp, talk);
1702 if (talk && uuaddrs.m_next)
1703 printf (" -- UUCP Recipients --\n");
1704 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1705 do_an_address (lp, talk);
1707 if (talk && netaddrs.m_next)
1708 printf (" -- Network Recipients --\n");
1709 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1710 do_an_address (lp, talk);
1713 if (talk && !whomsw)
1714 printf (" -- Address Verification Successful --\n");
1716 if (!whomsw || checksw)
1725 do_an_address (struct mailname *lp, int talk)
1728 char *mbox, *host, *text, *path;
1730 struct rp_bufstruct reply;
1732 switch (lp->m_type) {
1735 host = LocalName ();
1736 strncpy (addr, mbox, sizeof(addr));
1740 fprintf (talk ? stdout : stderr, " %s!%s: %s\n",
1741 lp->m_host, lp->m_mbox, "not supported; UUCP address");
1746 default: /* let MMDF decide if the host is bad */
1749 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1754 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1756 if (whomsw && !checksw) {
1766 path = concat (lp->m_path, mbox, "@", host, NULL);
1770 if (rp_isbad (retval = mm_wadr (path ? NULL : host, path ? path : mbox))
1771 || rp_isbad (retval = mm_rrply (&reply, &len)))
1772 die (NULL, "problem submitting address [%s]", rp_valstr (retval));
1774 switch (rp_gval (reply.rp_val)) {
1777 printf ("address ok\n");
1784 printf ("nameserver timeout - queued for checking\n");
1795 text = "temporary nameserver failure";
1802 text = "not deliverable";
1806 text = "try again later";
1810 text = "nothing done";
1815 fprintf (stderr, " %s: ", addr);
1816 text = "unexpected response";
1817 die (NULL, "%s;\n [%s] -- %s", text,
1818 rp_valstr (reply.rp_val), reply.rp_line);
1822 fprintf (stderr, " %s: ", addr);
1823 fprintf (talk ? stdout : stderr, "%s;\n %s\n", text, reply.rp_line);
1831 do_text (char *file, int fd)
1835 struct rp_bufstruct reply;
1837 lseek (fd, (off_t) 0, SEEK_SET);
1839 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1840 if (rp_isbad (mm_wtxt (buf, state)))
1841 die (NULL, "problem writing text [%s]\n", rp_valstr (retval));
1845 die (file, "problem reading from");
1847 if (rp_isbad (retval = mm_wtend ()))
1848 die (NULL, "problem ending text [%s]\n", rp_valstr (retval));
1850 if (rp_isbad (retval = mm_rrply (&reply, &state)))
1851 die (NULL, "problem getting submission status [%s]\n",
1852 rp_valstr (retval));
1854 switch (rp_gval (reply.rp_val)) {
1860 die (NULL, "you lose; %s", reply.rp_line);
1863 die (NULL, "no delivery occurred; %s", reply.rp_line);
1866 die (NULL, "try again later; %s", reply.rp_line);
1869 die (NULL, "nothing done; %s", reply.rp_line);
1872 die (NULL, "unexpected response;\n\t[%s] -- %s",
1873 rp_valstr (reply.rp_val), reply.rp_line);
1877 #endif /* MMDFMTS */
1887 #ifndef RELIABLE_SIGNALS
1888 SIGNAL (i, SIG_IGN);
1892 if (msgflags & MINV)
1896 if (!whomsw || checksw)
1898 #endif /* MMDFMTS */
1901 if (!whomsw || checksw)
1903 #endif /* SMTPMTS */
1915 hstat = SIGNAL2 (SIGHUP, sigser);
1916 istat = SIGNAL2 (SIGINT, sigser);
1917 qstat = SIGNAL2 (SIGQUIT, sigser);
1918 tstat = SIGNAL2 (SIGTERM, sigser);
1928 SIGNAL (SIGHUP, hstat);
1929 SIGNAL (SIGINT, istat);
1930 SIGNAL (SIGQUIT, qstat);
1931 SIGNAL (SIGTERM, tstat);
1939 p_refile (char *file)
1947 printf (" -- Filing Folder Copies --\n");
1948 for (i = 0; i < fccind; i++)
1949 fcc (file, fccfold[i]);
1951 printf (" -- Folder Copies Filed --\n");
1956 * Call the `fileproc' to add the file to the folder.
1960 fcc (char *file, char *folder)
1967 printf (" %sFcc %s: ", msgstate == RESENT ? "Resent-" : "", folder);
1970 for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
1976 fprintf (stderr, " %sFcc %s: ",
1977 msgstate == RESENT ? "Resent-" : "", folder);
1978 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
1982 /* see if we need to add `+' */
1983 snprintf (fold, sizeof(fold), "%s%s",
1984 *folder == '+' || *folder == '@' ? "" : "+", folder);
1986 /* now exec the fileproc */
1987 execlp (fileproc, r1bindex (fileproc, '/'),
1988 "-link", "-file", file, fold, NULL);
1992 if ((status = pidwait (child_id, OK))) {
1994 fprintf (stderr, " %sFcc %s: ",
1995 msgstate == RESENT ? "Resent-" : "", folder);
1996 pidstatus (status, verbose ? stdout : stderr, NULL);
1999 printf ("folder ok\n");
2011 die (char *what, char *fmt, ...)
2016 if (msgflags & MINV)
2020 if (!whomsw || checksw)
2022 #endif /* MMDFMTS */
2025 if (!whomsw || checksw)
2027 #endif /* SMTPMTS */
2030 advertise (what, NULL, fmt, ap);
2038 * err_abrt() is used by the mm_ routines
2039 * do not, under *ANY* circumstances, remove it from post,
2040 * or you will lose *BIG*
2044 err_abrt (int code, char *fmt, ...)
2046 char buffer[BUFSIZ];
2049 snprintf (buffer, sizeof(buffer), "[%s]", rp_valstr (code));
2052 advertise (buffer, NULL, fmt, ap);
2057 #endif /* MMDFMTS */