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>
28 #ifdef TIME_WITH_SYS_TIME
29 # include <sys/time.h>
32 # ifdef TM_IN_SYS_TIME
33 # include <sys/time.h>
40 # include <mts/mmdf/util.h>
41 # include <mts/mmdf/mmdf.h>
45 # include <mts/smtp/smtp.h>
49 # define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : (c))
53 # define SASLminc(a) (a)
54 #else /* CYRUS_SASL */
55 # define SASLminc(a) 0
56 #endif /* CYRUS_SASL */
58 #define FCCS 10 /* max number of fccs allowed */
60 /* In the following array of structures, the numeric second field of the
61 structures (minchars) is apparently used like this:
63 -# : Switch can be abbreviated to # characters; switch hidden in -help.
64 0 : Switch can't be abbreviated; switch shown in -help.
65 # : Switch can be abbreviated to # characters; switch shown in -help. */
67 static struct swit switches[] = {
69 { "alias aliasfile", 0 },
71 { "check", -5 }, /* interface from whom */
73 { "nocheck", -7 }, /* interface from whom */
77 { "dist", -4 }, /* interface from dist */
79 { "filter filterfile", 0 },
87 { "library directory", -7 }, /* interface from send, whom */
105 { "whom", -4 }, /* interface from whom */
107 { "width columns", 0 },
112 #define BITSTUFFSW 22
113 { "dashstuffing", -12 }, /* should we dashstuff BCC messages? */
114 #define NBITSTUFFSW 23
115 { "nodashstuffing", -14 },
117 { "mail", -4 }, /* specify MAIL smtp mode */
119 { "saml", -4 }, /* specify SAML smtp mode */
121 { "send", -4 }, /* specify SEND smtp mode */
123 { "soml", -4 }, /* specify SOML smtp mode */
125 { "idanno number", -6 }, /* interface from send */
127 { "deliver address-list", -7 },
129 { "client host", -6 },
131 { "server host", -6 }, /* specify alternate SMTP server */
133 { "snoop", -5 }, /* snoop the SMTP transaction */
135 { "fill-in file", -7 },
143 { "sasl", SASLminc(-4) },
144 #define SASLMECHSW 38
145 { "saslmech", SASLminc(-5) },
147 { "user", SASLminc(-4) },
159 * flags for headers->flags
161 #define HNOP 0x0000 /* just used to keep .set around */
162 #define HBAD 0x0001 /* bad header - don't let it through */
163 #define HADR 0x0002 /* header has an address field */
164 #define HSUB 0x0004 /* Subject: header */
165 #define HTRY 0x0008 /* try to send to addrs on header */
166 #define HBCC 0x0010 /* don't output this header */
167 #define HMNG 0x0020 /* munge this header */
168 #define HNGR 0x0040 /* no groups allowed in this header */
169 #define HFCC 0x0080 /* FCC: type header */
170 #define HNIL 0x0100 /* okay for this header not to have addrs */
171 #define HIGN 0x0200 /* ignore this header */
172 #define HDCC 0x0400 /* another undocumented feature */
175 * flags for headers->set
177 #define MFRM 0x0001 /* we've seen a From: */
178 #define MDAT 0x0002 /* we've seen a Date: */
179 #define MRFM 0x0004 /* we've seen a Resent-From: */
180 #define MVIS 0x0008 /* we've seen sighted addrs */
181 #define MINV 0x0010 /* we've seen blind addrs */
184 static struct headers NHeaders[] = {
185 { "Return-Path", HBAD, 0 },
186 { "Received", HBAD, 0 },
187 { "Reply-To", HADR|HNGR, 0 },
188 { "From", HADR|HNGR, MFRM },
189 { "Sender", HADR|HBAD, 0 },
191 { "Subject", HSUB, 0 },
192 { "To", HADR|HTRY, MVIS },
193 { "cc", HADR|HTRY, MVIS },
194 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
195 { "Dcc", HADR|HTRY|HDCC|HNIL, MVIS }, /* sorta cc & bcc combined */
196 { "Message-ID", HBAD, 0 },
201 static struct headers RHeaders[] = {
202 { "Resent-Reply-To", HADR|HNGR, 0 },
203 { "Resent-From", HADR|HNGR, MRFM },
204 { "Resent-Sender", HADR|HBAD, 0 },
205 { "Resent-Date", HBAD, 0 },
206 { "Resent-Subject", HSUB, 0 },
207 { "Resent-To", HADR|HTRY, MVIS },
208 { "Resent-cc", HADR|HTRY, MVIS },
209 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
210 { "Resent-Message-ID", HBAD, 0 },
211 { "Resent-Fcc", HFCC, 0 },
212 { "Reply-To", HADR, 0 },
213 { "From", HADR|HNGR, MFRM },
215 { "Sender", HADR|HNGR|HMNG, 0 },
217 { "Sender", HADR|HNGR, 0 },
219 { "Date", HNOP, MDAT },
220 { "To", HADR|HNIL, 0 },
221 { "cc", HADR|HNIL, 0 },
222 { "Bcc", HADR|HTRY|HBCC|HNIL, 0 },
227 static short fccind = 0; /* index into fccfold[] */
228 static short outputlinelen = OUTPUTLINELEN;
230 static int pfd = NOTOK; /* fd to write annotation list to */
231 static uid_t myuid= -1; /* my user id */
232 static gid_t mygid= -1; /* my group id */
233 static int recipients = 0; /* how many people will get a copy */
234 static int unkadr = 0; /* how many of those were unknown */
235 static int badadr = 0; /* number of bad addrs */
236 static int badmsg = 0; /* message has bad semantics */
237 static int verbose = 0; /* spell it out */
238 static int format = 1; /* format addresses */
239 static int mime = 0; /* use MIME-style encapsulations for Bcc */
240 static int msgid = 0; /* add msgid */
241 static int debug = 0; /* debugging post */
242 static int watch = 0; /* watch the delivery process */
243 static int whomsw = 0; /* we are whom not post */
244 static int checksw = 0; /* whom -check */
245 static int linepos=0; /* putadr()'s position on the line */
246 static int nameoutput=0; /* putadr() has output header name */
247 static int sasl=0; /* Use SASL auth for SMTP */
248 static char *saslmech=NULL; /* Force use of particular SASL mech */
249 static char *user=NULL; /* Authenticate as this user */
251 static unsigned msgflags = 0; /* what we've seen */
255 static int msgstate = NORMAL;
257 static time_t tclock = 0; /* the time we started (more or less) */
259 static SIGNAL_HANDLER hstat, istat, qstat, tstat;
261 static char tmpfil[BUFSIZ];
262 static char bccfil[BUFSIZ];
264 static char from[BUFSIZ]; /* my network address */
265 static char signature[BUFSIZ]; /* my signature */
266 static char *filter = NULL; /* the filter for BCC'ing */
267 static char *subject = NULL; /* the subject field for BCC'ing */
268 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
270 static struct headers *hdrtab; /* table for the message we're doing */
272 static struct mailname localaddrs={NULL}; /* local addrs */
273 static struct mailname netaddrs={NULL}; /* network addrs */
274 static struct mailname uuaddrs={NULL}; /* uucp addrs */
275 static struct mailname tmpaddrs={NULL}; /* temporary queue */
278 static char *submitmode = "m"; /* deliver to mailbox only */
279 static char submitopts[6] = "vl"; /* initial options for submit */
283 static int snoop = 0;
284 static int smtpmode = S_MAIL;
285 static char *clientsw = NULL;
286 static char *serversw = NULL;
288 extern struct smtp sm_reply;
291 static char prefix[] = "----- =_aaaaaaaaaa";
293 static int fill_up = 0;
294 static char *fill_in = NULL;
295 static char *partno = NULL;
296 static int queued = 0;
298 extern boolean draft_from_masquerading; /* defined in mts.c */
303 static void putfmt (char *, char *, FILE *);
304 static void start_headers (void);
305 static void finish_headers (FILE *);
306 static int get_header (char *, struct headers *);
307 static int putadr (char *, char *, struct mailname *, FILE *, unsigned int);
308 static void putgrp (char *, char *, FILE *, unsigned int);
309 static int insert (struct mailname *);
310 static void pl (void);
311 static void anno (void);
312 static int annoaux (struct mailname *);
313 static void insert_fcc (struct headers *, char *);
314 static void make_bcc_file (int);
315 static void verify_all_addresses (int);
316 static void chkadr (void);
317 static void sigon (void);
318 static void sigoff (void);
319 static void p_refile (char *);
320 static void fcc (char *, char *);
321 static void die (char *, char *, ...);
322 static void post (char *, int, int);
323 static void do_text (char *file, int fd);
324 static void do_an_address (struct mailname *, int);
325 static void do_addresses (int, int);
326 static int find_prefix (void);
330 main (int argc, char **argv)
332 int state, compnum, dashstuff = 0;
333 char *cp, *msg = NULL, **argp, **arguments;
334 char buf[BUFSIZ], name[NAMESZ];
338 setlocale(LC_ALL, "");
340 invo_name = r1bindex (argv[0], '/');
342 /* foil search of user profile/context */
343 if (context_foil (NULL) == -1)
346 mts_init (invo_name);
347 arguments = getarguments (invo_name, argc, argv, 0);
350 #if defined(MMDFMTS) && defined(MMDFII)
351 mmdf_init (invo_name);
352 #endif /* MMDFMTS and MMDFII */
354 while ((cp = *argp++)) {
356 switch (smatch (++cp, switches)) {
358 ambigsw (cp, switches);
361 adios (NULL, "-%s unknown", cp);
364 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
365 print_help (buf, switches, 0);
368 print_version(invo_name);
372 if (!(cp = *argp++) || *cp == '-')
373 adios (NULL, "missing argument to %s", argp[-2]);
374 /* create a minimal context */
375 if (context_foil (cp) == -1)
380 if (!(cp = *argp++) || *cp == '-')
381 adios (NULL, "missing argument to %s", argp[-2]);
382 if ((state = alias (cp)) != AK_OK)
383 adios (NULL, "aliasing error in %s - %s",
384 cp, akerror (state));
403 if (!(filter = *argp++) || *filter == '-')
404 adios (NULL, "missing argument to %s", argp[-2]);
459 if (!(cp = *argp++) || *cp == '-')
460 adios (NULL, "missing argument to %s", argp[-2]);
461 if ((outputlinelen = atoi (cp)) < 10)
462 adios (NULL, "impossible width %d", outputlinelen);
466 if (!(cp = *argp++) || *cp == '-')
467 adios (NULL, "missing argument to %s", argp[-2]);
468 if ((pfd = atoi (cp)) <= 2)
469 adios (NULL, "bad argument %s %s", argp[-2], cp);
476 case SOMLSW: /* for right now, sigh... */
486 if (!(cp = *argp++) || *cp == '-')
487 adios (NULL, "missing argument to %s", argp[-2]);
493 if (!(cp = *argp++) || *cp == '-')
494 adios (NULL, "missing argument to %s", argp[-2]);
513 if (!(clientsw = *argp++) || *clientsw == '-')
514 adios (NULL, "missing argument to %s", argp[-2]);
517 if (!(serversw = *argp++) || *serversw == '-')
518 adios (NULL, "missing argument to %s", argp[-2]);
526 if (!(fill_in = *argp++) || *fill_in == '-')
527 adios (NULL, "missing argument to %s", argp[-2]);
533 if (!(partno = *argp++) || *partno == '-')
534 adios (NULL, "missing argument to %s", argp[-2]);
546 if (!(saslmech = *argp++) || *saslmech == '-')
547 adios (NULL, "missing argument to %s", argp[-2]);
551 if (!(user = *argp++) || *user == '-')
552 adios (NULL, "missing argument to %s", argp[-2]);
557 adios (NULL, "only one message at a time!");
565 adios (NULL, "usage: %s [switches] file", invo_name);
567 if (outputlinelen < 10)
568 adios (NULL, "impossible width %d", outputlinelen);
570 if ((in = fopen (msg, "r")) == NULL)
571 adios (msg, "unable to open");
576 discard (out = stdout); /* XXX: reference discard() to help loader */
579 if ((out = fopen (fill_in ? fill_in : "/dev/null", "w")) == NULL)
580 adios ("/dev/null", "unable to open");
582 strncpy (tmpfil, m_scratch ("", m_maildir (invo_name)),
584 if ((out = fopen (tmpfil, "w")) == NULL) {
585 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
586 if ((out = fopen (tmpfil, "w")) == NULL)
587 adios (tmpfil, "unable to create");
589 chmod (tmpfil, 0600);
593 hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
595 for (compnum = 1, state = FLD;;) {
596 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
601 cp = add (buf, NULL);
602 while (state == FLDPLUS) {
603 state = m_getfld (state, name, buf, sizeof(buf), in);
606 putfmt (name, cp, out);
610 finish_headers (out);
615 finish_headers (out);
616 if (whomsw && !fill_in)
618 fprintf (out, "\n%s", buf);
619 while (state == BODY) {
620 state = m_getfld (state, name, buf, sizeof(buf), in);
626 finish_headers (out);
631 adios (NULL, "message format error in component #%d", compnum);
634 adios (NULL, "getfld() returned %d", state);
650 /* If we are doing a "whom" check */
653 verify_all_addresses (1);
658 strcat (submitopts, submitmode);
660 strcat (submitopts, "nw");
663 if (msgflags & MINV) {
664 make_bcc_file (dashstuff);
665 if (msgflags & MVIS) {
666 verify_all_addresses (verbose);
667 post (tmpfil, 0, verbose);
669 post (bccfil, 1, verbose);
672 post (tmpfil, 0, isatty (1));
679 printf (partno ? "Partial Message #%s Processed\n" : "Message Processed\n",
690 putfmt (char *name, char *str, FILE *out)
692 int count, grp, i, keep;
695 struct mailname *mp, *np;
698 while (*str == ' ' || *str == '\t')
701 if (msgstate == NORMAL && uprf (name, "resent")) {
702 advise (NULL, "illegal header line -- %s:", name);
707 if ((i = get_header (name, hdrtab)) == NOTOK) {
708 fprintf (out, "%s: %s", name, str);
713 if (hdr->flags & HIGN) {
715 fprintf (out, "%s: %s", name, str);
718 if (hdr->flags & HBAD) {
720 fprintf (out, "%s: %s", name, str);
722 advise (NULL, "illegal header line -- %s:", name);
727 msgflags |= (hdr->set & ~(MVIS | MINV));
729 if (hdr->flags & HSUB)
730 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
731 if (hdr->flags & HFCC) {
733 fprintf (out, "%s: %s", name, str);
737 if ((cp = strrchr(str, '\n')))
739 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
741 insert_fcc (hdr, pp);
743 insert_fcc (hdr, pp);
747 if (!(hdr->flags & HADR)) {
748 fprintf (out, "%s: %s", name, str);
752 tmpaddrs.m_next = NULL;
753 for (count = 0; (cp = getname (str)); count++)
754 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
758 tmpaddrs.m_next = mp;
762 if (hdr->flags & HTRY)
768 if (hdr->flags & HNIL)
769 fprintf (out, "%s: %s", name, str);
772 advise (NULL, "%s: field requires at least one address", name);
779 nameoutput = linepos = 0;
780 snprintf (namep, sizeof(namep), "%s%s",
781 !fill_in && (hdr->flags & HMNG) ? "Original-" : "", name);
783 for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
784 if (mp->m_nohost) { /* also used to test (hdr->flags & HTRY) */
785 /* The address doesn't include a host, so it might be an alias. */
786 pp = akvalue (mp->m_mbox); /* do mh alias substitution */
787 qp = akvisible () ? mp->m_mbox : "";
790 putgrp (namep, np->m_gname, out, hdr->flags);
791 while ((cp = getname (pp))) {
792 if (!(mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
797 if (draft_from_masquerading && ((msgstate == RESENT)
799 : (hdr->set & MFRM)))
800 /* The user manually specified a [Resent-]From: address in
801 their draft and the "masquerade:" line in mts.conf
802 doesn't contain "draft_from", so we'll set things up to
803 use the actual email address embedded in the draft
804 [Resent-]From: (after alias substitution, and without the
805 GECOS full name or angle brackets) as the envelope
807 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
809 if (hdr->flags & HBCC)
812 mp->m_ingrp = np->m_ingrp;
815 putgrp (namep, mp->m_gname, out, hdr->flags);
818 if (putadr (namep, qp, mp, out, hdr->flags))
819 msgflags |= (hdr->set & (MVIS | MINV));
828 /* Address includes a host, so no alias substitution is needed. */
829 if (draft_from_masquerading && ((msgstate == RESENT)
831 : (hdr->set & MFRM)))
832 /* The user manually specified a [Resent-]From: address in
833 their draft and the "masquerade:" line in mts.conf
834 doesn't contain "draft_from", so we'll set things up to
835 use the actual email address embedded in the draft
836 [Resent-]From: (after alias substitution, and without the
837 GECOS full name or angle brackets) as the envelope
839 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
841 if (hdr->flags & HBCC)
844 putgrp (namep, mp->m_gname, out, hdr->flags);
847 keep = putadr (namep, "", mp, out, hdr->flags);
851 msgflags |= (hdr->set & (MVIS | MINV));
857 if (grp > 0 && (hdr->flags & HNGR)) {
858 advise (NULL, "%s: field does not allow groups", name);
862 if (fill_in && grp > 0)
873 char myhost[BUFSIZ], sigbuf[BUFSIZ];
880 strncpy (from, adrsprintf (NULL, NULL), sizeof(from));
881 strncpy (myhost, LocalName (), sizeof(myhost));
883 for (cp = myhost; *cp; cp++)
886 if ((cp = getfullname ()) && *cp) {
887 strncpy (sigbuf, cp, sizeof(sigbuf));
888 snprintf (signature, sizeof(signature), "%s <%s>",
889 sigbuf, adrsprintf (NULL, NULL));
890 if ((cp = getname (signature)) == NULL)
891 adios (NULL, "getname () failed -- you lose extraordinarily big");
892 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL)) == NULL)
893 adios (NULL, "bad signature '%s'", sigbuf);
898 strncpy (signature, adrsprintf (NULL, NULL), sizeof(signature));
904 * Now that we've outputted the header fields in the draft
905 * message, we will now output any remaining header fields
906 * that we need to add/create.
910 finish_headers (FILE *out)
914 if (whomsw && !fill_up)
917 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
919 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
920 (int) getpid (), (long) tclock, LocalName ());
921 if (msgflags & MFRM) {
922 /* There was already a From: in the draft. Don't add one. */
923 if (!draft_from_masquerading)
924 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
925 so we'll reveal the user's actual account@thismachine
926 address in a Sender: header (and use it as the envelope
928 fprintf (out, "Sender: %s\n", from);
931 /* Construct a From: header. */
932 fprintf (out, "From: %s\n", signature);
936 if (!(msgflags & MVIS))
937 fprintf (out, "Bcc: Blind Distribution List: ;\n");
941 if (!(msgflags & MDAT)) {
942 advise (NULL, "message has no Date: header");
945 if (!(msgflags & MFRM)) {
946 advise (NULL, "message has no From: header");
949 if (whomsw && !fill_up)
952 #ifdef MMDFI /* sigh */
953 fprintf (out, "Sender: %s\n", from);
956 fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
958 fprintf (out, "Resent-Message-ID: <%d.%ld@%s>\n",
959 (int) getpid (), (long) tclock, LocalName ());
960 if (msgflags & MRFM) {
961 /* There was already a Resent-From: in draft. Don't add one. */
962 if (!draft_from_masquerading)
963 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
964 so we'll reveal the user's actual account@thismachine
965 address in a Sender: header (and use it as the envelope
967 fprintf (out, "Resent-Sender: %s\n", from);
970 /* Construct a Resent-From: header. */
971 fprintf (out, "Resent-From: %s\n", signature);
974 if (!(msgflags & MVIS))
975 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
980 adios (NULL, "re-format message and try again");
982 adios (NULL, "no addressees");
987 get_header (char *header, struct headers *table)
991 for (h = table; h->value; h++)
992 if (!strcasecmp (header, h->value))
1000 putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flags)
1004 char buffer[BUFSIZ];
1006 if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
1008 if ((!fill_in && (flags & (HBCC | HDCC))) || mp->m_ingrp)
1012 fprintf (out, "%s: ", name);
1013 linepos += (nameoutput = strlen (name) + 2);
1016 if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
1017 mp->m_pers = getcpy (aka);
1019 if (mp->m_gname && !fill_in) {
1020 snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
1023 cp = adrformat (mp);
1030 if (linepos != nameoutput) {
1031 if (len + linepos + 2 > outputlinelen)
1032 fprintf (out, ",\n%*s", linepos = nameoutput, "");
1042 return (flags & HTRY);
1047 putgrp (char *name, char *group, FILE *out, unsigned int flags)
1052 if (!fill_in && (flags & HBCC))
1056 fprintf (out, "%s: ", name);
1057 linepos += (nameoutput = strlen (name) + 2);
1059 linepos -= strlen (group);
1062 cp = fill_in ? group : concat (group, ";", NULL);
1065 if (linepos > nameoutput) {
1066 if (len + linepos + 2 > outputlinelen) {
1067 fprintf (out, ",\n%*s", nameoutput, "");
1068 linepos = nameoutput;
1082 insert (struct mailname *np)
1084 struct mailname *mp;
1086 if (np->m_mbox == NULL)
1089 for (mp = np->m_type == LOCALHOST ? &localaddrs
1090 : np->m_type == UUCPHOST ? &uuaddrs
1094 if (!strcasecmp (np->m_host, mp->m_next->m_host)
1095 && !strcasecmp (np->m_mbox, mp->m_next->m_mbox)
1096 && np->m_bcc == mp->m_next->m_bcc)
1109 struct mailname *mp;
1111 printf ("-------\n\t-- Addresses --\nlocal:\t");
1112 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1113 printf ("%s%s%s", mp->m_mbox,
1114 mp->m_bcc ? "[BCC]" : "",
1115 mp->m_next ? ",\n\t" : "");
1117 printf ("\nnet:\t");
1118 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1119 printf ("%s%s@%s%s%s", mp->m_path ? mp->m_path : "",
1120 mp->m_mbox, mp->m_host,
1121 mp->m_bcc ? "[BCC]" : "",
1122 mp->m_next ? ",\n\t" : "");
1124 printf ("\nuucp:\t");
1125 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1126 printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1127 mp->m_bcc ? "[BCC]" : "",
1128 mp->m_next ? ",\n\t" : "");
1130 printf ("\n\t-- Folder Copies --\nfcc:\t");
1131 for (i = 0; i < fccind; i++)
1132 printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1140 struct mailname *mp;
1142 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1143 if (annoaux (mp) == NOTOK)
1146 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1147 if (annoaux (mp) == NOTOK)
1150 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1151 if (annoaux (mp) == NOTOK)
1161 annoaux (struct mailname *mp)
1164 char buffer[BUFSIZ];
1166 snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1167 i = strlen (buffer);
1169 return (write (pfd, buffer, i) == i ? OK : NOTOK);
1174 insert_fcc (struct headers *hdr, char *pp)
1178 for (cp = pp; isspace (*cp); cp++)
1180 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
1188 adios (NULL, "too many %ss", hdr->value);
1189 fccfold[fccind++] = getcpy (cp);
1197 make_bcc_file (int dashstuff)
1204 strncpy (bccfil, m_tmpfil ("bccs"), sizeof(bccfil));
1205 if ((out = fopen (bccfil, "w")) == NULL)
1206 adios (bccfil, "unable to create");
1207 chmod (bccfil, 0600);
1209 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1211 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
1212 (int) getpid (), (long) tclock, LocalName ());
1213 fprintf (out, "From: %s\n", signature);
1215 fprintf (out, "Subject: %s", subject);
1216 fprintf (out, "BCC:\n");
1219 * Use MIME encapsulation for Bcc messages
1225 * Check if any lines in the message clash with the
1226 * prefix for the MIME multipart separator. If there
1227 * is a clash, increment one of the letters in the
1228 * prefix and check again.
1230 if ((cp = strchr(prefix, 'a')) == NULL)
1231 adios (NULL, "lost prefix start");
1232 while (find_prefix () == NOTOK) {
1237 adios (NULL, "can't find a unique delimiter string");
1242 fprintf (out, "%s: %s\n%s: multipart/digest; boundary=\"",
1243 VRSN_FIELD, VRSN_VALUE, TYPE_FIELD);
1244 fprintf (out, "%s\"\n\n--%s\n\n", prefix, prefix);
1246 fprintf (out, "\n------- Blind-Carbon-Copy\n\n");
1252 * Do mhl filtering of Bcc messages instead
1253 * of MIME encapsulation.
1255 if (filter != NULL) {
1256 vec[0] = r1bindex (mhlproc, '/');
1258 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1262 adios ("fork", "unable to");
1265 dup2 (fileno (out), 1);
1268 vec[i++] = "-forward";
1273 /* was the flag -[no]dashstuffing specified? */
1275 vec[i++] = "-dashstuffing";
1276 else if (dashstuff < 0)
1277 vec[i++] = "-nodashstuffing";
1280 execvp (mhlproc, vec);
1281 fprintf (stderr, "unable to exec ");
1286 pidXwait (child_id, mhlproc);
1290 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
1291 adios (tmpfil, "unable to re-open");
1294 * If using MIME encapsulation, or if the -nodashstuffing
1295 * flag was given, then just copy message. Else do
1296 * RFC934 quoting (dashstuffing).
1298 if (mime || dashstuff < 0)
1299 cpydata (fd, fileno (out), tmpfil, bccfil);
1301 cpydgst (fd, fileno (out), tmpfil, bccfil);
1305 fseek (out, 0L, SEEK_END);
1307 fprintf (out, "\n--%s--\n", prefix);
1309 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
1315 * Scan message to check if any lines clash with
1316 * the prefix of the MIME multipart separator.
1323 char buffer[BUFSIZ];
1326 if ((in = fopen (tmpfil, "r")) == NULL)
1327 adios (tmpfil, "unable to re-open");
1329 len = strlen (prefix);
1332 while (fgets (buffer, sizeof(buffer) - 1, in))
1333 if (buffer[0] == '-' && buffer[1] == '-') {
1336 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1340 if (strcmp (buffer + 2, prefix) == 0) {
1351 #define plural(x) (x == 1 ? "" : "s")
1356 if (badadr && unkadr)
1357 die (NULL, "%d address%s unparsable, %d addressee%s undeliverable",
1358 badadr, plural (badadr), unkadr, plural (badadr));
1360 die (NULL, "%d address%s unparsable", badadr, plural (badadr));
1362 die (NULL, "%d addressee%s undeliverable", unkadr, plural (unkadr));
1367 do_addresses (int bccque, int talk)
1371 struct mailname *lp;
1374 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1375 if (lp->m_bcc ? bccque : !bccque) {
1377 printf (" -- Local Recipients --\n");
1378 do_an_address (lp, talk);
1383 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1384 if (lp->m_bcc ? bccque : !bccque) {
1386 printf (" -- UUCP Recipients --\n");
1387 do_an_address (lp, talk);
1392 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1393 if (lp->m_bcc ? bccque : !bccque) {
1395 printf (" -- Network Recipients --\n");
1396 do_an_address (lp, talk);
1403 if (rp_isbad (retval = mm_waend ()))
1404 die (NULL, "problem ending addresses [%s]\n", rp_valstr (retval));
1405 #endif /* MMDFMTS */
1408 if (rp_isbad (retval = sm_waend ()))
1409 die (NULL, "problem ending addresses; %s", rp_string (retval));
1410 #endif /* SMTPMTS */
1415 * MTS-SPECIFIC INTERACTION
1420 * SENDMAIL/SMTP routines
1426 post (char *file, int bccque, int talk)
1431 onex = !(msgflags & MINV) || bccque;
1433 if (msgflags & MINV)
1434 printf (" -- Posting for %s Recipients --\n",
1435 bccque ? "Blind" : "Sighted");
1437 printf (" -- Posting for All Recipients --\n");
1442 if (rp_isbad (retval = sm_init (clientsw, serversw, watch, verbose,
1443 snoop, onex, queued, sasl, saslmech,
1445 || rp_isbad (retval = sm_winit (smtpmode, from)))
1446 die (NULL, "problem initializing server; %s", rp_string (retval));
1448 do_addresses (bccque, talk && verbose);
1449 if ((fd = open (file, O_RDONLY)) == NOTOK)
1450 die (file, "unable to re-open");
1455 sm_end (onex ? OK : DONE);
1459 if (msgflags & MINV)
1460 printf (" -- %s Recipient Copies Posted --\n",
1461 bccque ? "Blind" : "Sighted");
1463 printf (" -- Recipient Copies Posted --\n");
1470 /* Address Verification */
1473 verify_all_addresses (int talk)
1476 struct mailname *lp;
1480 if (!whomsw || checksw)
1481 if (rp_isbad (retval = sm_init (clientsw, serversw, watch, verbose, snoop, 0,
1482 queued, sasl, saslmech, user))
1483 || rp_isbad (retval = sm_winit (smtpmode, from)))
1484 die (NULL, "problem initializing server; %s", rp_string (retval));
1486 if (talk && !whomsw)
1487 printf (" -- Address Verification --\n");
1488 if (talk && localaddrs.m_next)
1489 printf (" -- Local Recipients --\n");
1490 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1491 do_an_address (lp, talk);
1493 if (talk && uuaddrs.m_next)
1494 printf (" -- UUCP Recipients --\n");
1495 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1496 do_an_address (lp, talk);
1498 if (talk && netaddrs.m_next)
1499 printf (" -- Network Recipients --\n");
1500 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1501 do_an_address (lp, talk);
1504 if (talk && !whomsw)
1505 printf (" -- Address Verification Successful --\n");
1507 if (!whomsw || checksw)
1516 do_an_address (struct mailname *lp, int talk)
1522 switch (lp->m_type) {
1526 strncpy (addr, mbox, sizeof(addr));
1530 mbox = auxformat (lp, 0);
1532 snprintf (addr, sizeof(addr), "%s!%s", lp->m_host, lp->m_mbox);
1535 default: /* let SendMail decide if the host is bad */
1538 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1543 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1545 if (whomsw && !checksw) {
1553 switch (retval = sm_wadr (mbox, host,
1554 lp->m_type != UUCPHOST ? lp->m_path : NULL)) {
1557 printf ("address ok\n");
1563 fprintf (stderr, " %s: ", addr);
1564 fprintf (talk ? stdout : stderr, "loses; %s\n",
1565 rp_string (retval));
1571 fprintf (stderr, " %s: ", addr);
1572 die (NULL, "unexpected response; %s", rp_string (retval));
1580 do_text (char *file, int fd)
1585 lseek (fd, (off_t) 0, SEEK_SET);
1587 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1588 if (rp_isbad (retval = sm_wtxt (buf, state)))
1589 die (NULL, "problem writing text; %s\n", rp_string (retval));
1593 die (file, "problem reading from");
1595 switch (retval = sm_wtend ()) {
1601 die (NULL, "posting failed; %s", rp_string (retval));
1604 die (NULL, "unexpected response; %s", rp_string (retval));
1608 #endif /* SMTPMTS */
1617 post (char *file, int bccque, int talk)
1623 struct rp_bufstruct reply;
1626 onex = !(msgflags & MINV) || bccque;
1628 if (msgflags & MINV)
1629 printf (" -- Posting for %s Recipients --\n",
1630 bccque ? "Blind" : "Sighted");
1632 printf (" -- Posting for All Recipients --\n");
1637 if (rp_isbad (retval = mm_init ())
1638 || rp_isbad (retval = mm_sbinit ())
1639 || rp_isbad (retval = mm_winit (NULL, submitopts, from)))
1640 die (NULL, "problem initializing MMDF system [%s]",
1641 rp_valstr (retval));
1643 if (rp_isbad (retval = mm_rrply (&reply, &len)))
1644 die (NULL, "problem with sender address [%s]",
1645 rp_valstr (retval));
1648 do_addresses (bccque, talk && verbose);
1649 if ((fd = open (file, O_RDONLY)) == NOTOK)
1650 die (file, "unable to re-open");
1660 if (msgflags & MINV)
1661 printf (" -- %s Recipient Copies Posted --\n",
1662 bccque ? "Blind" : "Sighted");
1664 printf (" -- Recipient Copies Posted --\n");
1669 /* Address Verification */
1672 verify_all_addresses (int talk)
1675 struct mailname *lp;
1679 struct rp_bufstruct reply;
1684 if (!whomsw || checksw) {
1685 if (rp_isbad (retval = mm_init ())
1686 || rp_isbad (retval = mm_sbinit ())
1687 || rp_isbad (retval = mm_winit (NULL, submitopts, from)))
1688 die (NULL, "problem initializing MMDF system [%s]",
1689 rp_valstr (retval));
1691 if (rp_isbad (retval = mm_rrply (&reply, &len)))
1692 die (NULL, "problem with sender address [%s]", rp_valstr (retval));
1696 if (talk && !whomsw)
1697 printf (" -- Address Verification --\n");
1698 if (talk && localaddrs.m_next)
1699 printf (" -- Local Recipients --\n");
1700 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1701 do_an_address (lp, talk);
1703 if (talk && uuaddrs.m_next)
1704 printf (" -- UUCP Recipients --\n");
1705 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1706 do_an_address (lp, talk);
1708 if (talk && netaddrs.m_next)
1709 printf (" -- Network Recipients --\n");
1710 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1711 do_an_address (lp, talk);
1714 if (talk && !whomsw)
1715 printf (" -- Address Verification Successful --\n");
1717 if (!whomsw || checksw)
1726 do_an_address (struct mailname *lp, int talk)
1729 char *mbox, *host, *text, *path;
1731 struct rp_bufstruct reply;
1733 switch (lp->m_type) {
1736 host = LocalName ();
1737 strncpy (addr, mbox, sizeof(addr));
1741 fprintf (talk ? stdout : stderr, " %s!%s: %s\n",
1742 lp->m_host, lp->m_mbox, "not supported; UUCP address");
1747 default: /* let MMDF decide if the host is bad */
1750 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1755 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1757 if (whomsw && !checksw) {
1767 path = concat (lp->m_path, mbox, "@", host, NULL);
1771 if (rp_isbad (retval = mm_wadr (path ? NULL : host, path ? path : mbox))
1772 || rp_isbad (retval = mm_rrply (&reply, &len)))
1773 die (NULL, "problem submitting address [%s]", rp_valstr (retval));
1775 switch (rp_gval (reply.rp_val)) {
1778 printf ("address ok\n");
1785 printf ("nameserver timeout - queued for checking\n");
1796 text = "temporary nameserver failure";
1803 text = "not deliverable";
1807 text = "try again later";
1811 text = "nothing done";
1816 fprintf (stderr, " %s: ", addr);
1817 text = "unexpected response";
1818 die (NULL, "%s;\n [%s] -- %s", text,
1819 rp_valstr (reply.rp_val), reply.rp_line);
1823 fprintf (stderr, " %s: ", addr);
1824 fprintf (talk ? stdout : stderr, "%s;\n %s\n", text, reply.rp_line);
1832 do_text (char *file, int fd)
1836 struct rp_bufstruct reply;
1838 lseek (fd, (off_t) 0, SEEK_SET);
1840 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1841 if (rp_isbad (mm_wtxt (buf, state)))
1842 die (NULL, "problem writing text [%s]\n", rp_valstr (retval));
1846 die (file, "problem reading from");
1848 if (rp_isbad (retval = mm_wtend ()))
1849 die (NULL, "problem ending text [%s]\n", rp_valstr (retval));
1851 if (rp_isbad (retval = mm_rrply (&reply, &state)))
1852 die (NULL, "problem getting submission status [%s]\n",
1853 rp_valstr (retval));
1855 switch (rp_gval (reply.rp_val)) {
1861 die (NULL, "you lose; %s", reply.rp_line);
1864 die (NULL, "no delivery occurred; %s", reply.rp_line);
1867 die (NULL, "try again later; %s", reply.rp_line);
1870 die (NULL, "nothing done; %s", reply.rp_line);
1873 die (NULL, "unexpected response;\n\t[%s] -- %s",
1874 rp_valstr (reply.rp_val), reply.rp_line);
1878 #endif /* MMDFMTS */
1888 #ifndef RELIABLE_SIGNALS
1889 SIGNAL (i, SIG_IGN);
1893 if (msgflags & MINV)
1897 if (!whomsw || checksw)
1899 #endif /* MMDFMTS */
1902 if (!whomsw || checksw)
1904 #endif /* SMTPMTS */
1916 hstat = SIGNAL2 (SIGHUP, sigser);
1917 istat = SIGNAL2 (SIGINT, sigser);
1918 qstat = SIGNAL2 (SIGQUIT, sigser);
1919 tstat = SIGNAL2 (SIGTERM, sigser);
1929 SIGNAL (SIGHUP, hstat);
1930 SIGNAL (SIGINT, istat);
1931 SIGNAL (SIGQUIT, qstat);
1932 SIGNAL (SIGTERM, tstat);
1940 p_refile (char *file)
1948 printf (" -- Filing Folder Copies --\n");
1949 for (i = 0; i < fccind; i++)
1950 fcc (file, fccfold[i]);
1952 printf (" -- Folder Copies Filed --\n");
1957 * Call the `fileproc' to add the file to the folder.
1961 fcc (char *file, char *folder)
1968 printf (" %sFcc %s: ", msgstate == RESENT ? "Resent-" : "", folder);
1971 for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
1977 fprintf (stderr, " %sFcc %s: ",
1978 msgstate == RESENT ? "Resent-" : "", folder);
1979 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
1983 /* see if we need to add `+' */
1984 snprintf (fold, sizeof(fold), "%s%s",
1985 *folder == '+' || *folder == '@' ? "" : "+", folder);
1987 /* now exec the fileproc */
1988 execlp (fileproc, r1bindex (fileproc, '/'),
1989 "-link", "-file", file, fold, NULL);
1993 if ((status = pidwait (child_id, OK))) {
1995 fprintf (stderr, " %sFcc %s: ",
1996 msgstate == RESENT ? "Resent-" : "", folder);
1997 pidstatus (status, verbose ? stdout : stderr, NULL);
2000 printf ("folder ok\n");
2012 die (char *what, char *fmt, ...)
2017 if (msgflags & MINV)
2021 if (!whomsw || checksw)
2023 #endif /* MMDFMTS */
2026 if (!whomsw || checksw)
2028 #endif /* SMTPMTS */
2031 advertise (what, NULL, fmt, ap);
2039 * err_abrt() is used by the mm_ routines
2040 * do not, under *ANY* circumstances, remove it from post,
2041 * or you will lose *BIG*
2045 err_abrt (int code, char *fmt, ...)
2047 char buffer[BUFSIZ];
2050 snprintf (buffer, sizeof(buffer), "[%s]", rp_valstr (code));
2053 advertise (buffer, NULL, fmt, ap);
2058 #endif /* MMDFMTS */