3 * post.c -- enter messages into the mail transport system
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
13 #include <h/addrsbr.h>
14 #include <h/aliasbr.h>
15 #include <h/dropsbr.h>
25 #ifdef HAVE_SYS_TIME_H
26 # include <sys/time.h>
30 #include <mts/smtp/smtp.h>
33 # define SASLminc(a) (a)
34 #else /* CYRUS_SASL */
35 # define SASLminc(a) 0
36 #endif /* CYRUS_SASL */
39 # define TLSminc(a) (a)
40 #else /* TLS_SUPPORT */
42 #endif /* TLS_SUPPORT */
44 #define FCCS 10 /* max number of fccs allowed */
46 #define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : c)
48 /* In the following array of structures, the numeric second field of the
49 structures (minchars) is apparently used like this:
51 -# : Switch can be abbreviated to # characters; switch hidden in -help.
52 0 : Switch can't be abbreviated; switch shown in -help.
53 # : Switch can be abbreviated to # characters; switch shown in -help. */
55 static struct swit switches[] = {
57 { "alias aliasfile", 0 },
59 { "check", -5 }, /* interface from whom */
61 { "nocheck", -7 }, /* interface from whom */
65 { "dist", -4 }, /* interface from dist */
67 { "filter filterfile", 0 },
75 { "library directory", -7 }, /* interface from send, whom */
93 { "whom", -4 }, /* interface from whom */
95 { "width columns", 0 },
100 #define BITSTUFFSW 22
101 { "dashstuffing", -12 }, /* should we dashstuff BCC messages? */
102 #define NBITSTUFFSW 23
103 { "nodashstuffing", -14 },
105 { "idanno number", -6 }, /* interface from send */
107 { "client host", -6 },
109 { "server host", 6 }, /* specify alternate SMTP server */
111 { "snoop", -5 }, /* snoop the SMTP transaction */
117 { "sasl", SASLminc(-4) },
119 { "nosasl", SASLminc(-6) },
120 #define SASLMXSSFSW 32
121 { "saslmaxssf", SASLminc(-10) },
122 #define SASLMECHSW 33
123 { "saslmech", SASLminc(-5) },
125 { "user", SASLminc(-4) },
127 { "port server port name/number", 4 },
129 { "tls", TLSminc(-3) },
131 { "notls", TLSminc(-5) },
132 #define FILEPROCSW 38
147 * flags for headers->flags
149 #define HNOP 0x0000 /* just used to keep .set around */
150 #define HBAD 0x0001 /* bad header - don't let it through */
151 #define HADR 0x0002 /* header has an address field */
152 #define HSUB 0x0004 /* Subject: header */
153 #define HTRY 0x0008 /* try to send to addrs on header */
154 #define HBCC 0x0010 /* don't output this header */
155 #define HMNG 0x0020 /* munge this header */
156 #define HNGR 0x0040 /* no groups allowed in this header */
157 #define HFCC 0x0080 /* FCC: type header */
158 #define HNIL 0x0100 /* okay for this header not to have addrs */
159 #define HIGN 0x0200 /* ignore this header */
160 #define HDCC 0x0400 /* another undocumented feature */
161 #define HONE 0x0800 /* Only one address allowed */
162 #define HEFM 0x1000 /* Envelope-From: header */
165 * flags for headers->set
167 #define MFRM 0x0001 /* we've seen a From: */
168 #define MDAT 0x0002 /* we've seen a Date: */
169 #define MRFM 0x0004 /* we've seen a Resent-From: */
170 #define MVIS 0x0008 /* we've seen sighted addrs */
171 #define MINV 0x0010 /* we've seen blind addrs */
172 #define MSND 0x0020 /* we've seen a Sender: */
173 #define MRSN 0x0040 /* We've seen a Resent-Sendr:*/
174 #define MEFM 0x0080 /* We've seen Envelope-From: */
177 static struct headers NHeaders[] = {
178 { "Return-Path", HBAD, 0 },
179 { "Received", HBAD, 0 },
180 { "Reply-To", HADR|HNGR, 0 },
181 { "From", HADR|HNGR, MFRM },
182 { "Sender", HADR|HNGR|HONE, MSND },
184 { "Subject", HSUB, 0 },
185 { "To", HADR|HTRY, MVIS },
186 { "cc", HADR|HTRY, MVIS },
187 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
188 { "Dcc", HADR|HTRY|HDCC|HNIL, MVIS }, /* sorta cc & bcc combined */
189 { "Message-ID", HBAD, 0 },
191 { "Envelope-From", HADR|HONE|HEFM, MEFM },
195 static struct headers RHeaders[] = {
196 { "Resent-Reply-To", HADR|HNGR, 0 },
197 { "Resent-From", HADR|HNGR, MRFM },
198 { "Resent-Sender", HADR|HNGR, MRSN },
199 { "Resent-Date", HBAD, 0 },
200 { "Resent-Subject", HSUB, 0 },
201 { "Resent-To", HADR|HTRY, MVIS },
202 { "Resent-cc", HADR|HTRY, MVIS },
203 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
204 { "Resent-Message-ID", HBAD, 0 },
205 { "Resent-Fcc", HFCC, 0 },
206 { "Reply-To", HADR, 0 },
207 { "From", HADR|HNGR, MFRM },
208 { "Sender", HADR|HNGR, MSND },
209 { "Date", HNOP, MDAT },
210 { "To", HADR|HNIL, 0 },
211 { "cc", HADR|HNIL, 0 },
212 { "Bcc", HADR|HTRY|HBCC|HNIL, 0 },
214 { "Envelope-From", HADR|HONE|HEFM, MEFM },
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 */
238 static int sasl=0; /* Use SASL auth for SMTP */
239 static int saslssf=-1; /* Our maximum SSF for SASL */
240 static char *saslmech=NULL; /* Force use of particular SASL mech */
241 static char *user=NULL; /* Authenticate as this user */
242 static char *port="smtp"; /* Name of server port for SMTP */
243 static int tls=0; /* Use TLS for encryption */
244 static int fromcount=0; /* Count of addresses on From: header */
245 static int seensender=0; /* Have we seen a Sender: header? */
247 static unsigned msgflags = 0; /* what we've seen */
251 static int msgstate = NORMAL;
253 static time_t tclock = 0; /* the time we started (more or less) */
255 static SIGNAL_HANDLER hstat, istat, qstat, tstat;
257 static char tmpfil[BUFSIZ];
258 static char bccfil[BUFSIZ];
260 static char from[BUFSIZ]; /* my network address */
261 static char sender[BUFSIZ]; /* my Sender: header */
262 static char efrom[BUFSIZ]; /* my Envelope-From: header */
263 static char signature[BUFSIZ]; /* my signature */
264 static char *filter = NULL; /* the filter for BCC'ing */
265 static char *subject = NULL; /* the subject field for BCC'ing */
266 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
268 static struct headers *hdrtab; /* table for the message we're doing */
270 static struct mailname localaddrs; /* local addrs */
271 static struct mailname netaddrs; /* network addrs */
272 static struct mailname uuaddrs; /* uucp addrs */
273 static struct mailname tmpaddrs; /* temporary queue */
275 static int snoop = 0;
276 static char *clientsw = NULL;
277 static char *serversw = NULL;
279 extern struct smtp sm_reply;
281 static char prefix[] = "----- =_aaaaaaaaaa";
283 static char *partno = NULL;
284 static int queued = 0;
289 static void putfmt (char *, char *, FILE *);
290 static void start_headers (void);
291 static void finish_headers (FILE *);
292 static int get_header (char *, struct headers *);
293 static int putadr (char *, char *, struct mailname *, FILE *, unsigned int);
294 static void putgrp (char *, char *, FILE *, unsigned int);
295 static int insert (struct mailname *);
296 static void pl (void);
297 static void anno (void);
298 static int annoaux (struct mailname *);
299 static void insert_fcc (struct headers *, unsigned char *);
300 static void make_bcc_file (int);
301 static void verify_all_addresses (int, char *);
302 static void chkadr (void);
303 static void sigon (void);
304 static void sigoff (void);
305 static void p_refile (char *);
306 static void fcc (char *, char *);
307 static void die (char *, char *, ...);
308 static void post (char *, int, int, char *);
309 static void do_text (char *file, int fd);
310 static void do_an_address (struct mailname *, int);
311 static void do_addresses (int, int);
312 static int find_prefix (void);
316 main (int argc, char **argv)
318 int state, compnum, dashstuff = 0;
319 char *cp, *msg = NULL, **argp, **arguments, *envelope;
320 char buf[BUFSIZ], name[NAMESZ];
324 setlocale(LC_ALL, "");
326 invo_name = r1bindex (argv[0], '/');
328 /* foil search of user profile/context */
329 if (context_foil (NULL) == -1)
332 mts_init (invo_name);
333 arguments = getarguments (invo_name, argc, argv, 0);
336 while ((cp = *argp++)) {
338 switch (smatch (++cp, switches)) {
340 ambigsw (cp, switches);
343 adios (NULL, "-%s unknown", cp);
346 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
347 print_help (buf, switches, 0);
350 print_version(invo_name);
354 if (!(cp = *argp++) || *cp == '-')
355 adios (NULL, "missing argument to %s", argp[-2]);
356 /* create a minimal context */
357 if (context_foil (cp) == -1)
362 if (!(cp = *argp++) || *cp == '-')
363 adios (NULL, "missing argument to %s", argp[-2]);
364 if ((state = alias (cp)) != AK_OK)
365 adios (NULL, "aliasing error in %s - %s",
366 cp, akerror (state));
385 if (!(filter = *argp++) || *filter == '-')
386 adios (NULL, "missing argument to %s", argp[-2]);
441 if (!(cp = *argp++) || *cp == '-')
442 adios (NULL, "missing argument to %s", argp[-2]);
443 if ((outputlinelen = atoi (cp)) < 10)
444 adios (NULL, "impossible width %d", outputlinelen);
448 if (!(cp = *argp++) || *cp == '-')
449 adios (NULL, "missing argument to %s", argp[-2]);
450 if ((pfd = atoi (cp)) <= 2)
451 adios (NULL, "bad argument %s %s", argp[-2], cp);
455 if (!(clientsw = *argp++) || *clientsw == '-')
456 adios (NULL, "missing argument to %s", argp[-2]);
459 if (!(serversw = *argp++) || *serversw == '-')
460 adios (NULL, "missing argument to %s", argp[-2]);
467 if (!(partno = *argp++) || *partno == '-')
468 adios (NULL, "missing argument to %s", argp[-2]);
484 if (!(cp = *argp++) || *cp == '-')
485 adios (NULL, "missing argument to %s", argp[-2]);
490 if (!(saslmech = *argp++) || *saslmech == '-')
491 adios (NULL, "missing argument to %s", argp[-2]);
495 if (!(user = *argp++) || *user == '-')
496 adios (NULL, "missing argument to %s", argp[-2]);
500 if (!(port = *argp++) || *port == '-')
501 adios (NULL, "missing argument to %s", argp[-2]);
513 if (!(cp = *argp++) || *cp == '-')
514 adios (NULL, "missing argument to %s", argp[-2]);
519 if (!(cp = *argp++) || *cp == '-')
520 adios (NULL, "missing argument to %s", argp[-2]);
526 adios (NULL, "only one message at a time!");
534 adios (NULL, "usage: %s [switches] file", invo_name);
536 if (outputlinelen < 10)
537 adios (NULL, "impossible width %d", outputlinelen);
539 if ((in = fopen (msg, "r")) == NULL)
540 adios (msg, "unable to open");
545 discard (out = stdout); /* XXX: reference discard() to help loader */
548 if ((out = fopen ("/dev/null", "w")) == NULL)
549 adios ("/dev/null", "unable to open");
551 char *cp = m_mktemp(m_maildir(invo_name), NULL, &out);
553 cp = m_mktemp2(NULL, invo_name, NULL, &out);
555 adios ("post", "unable to create temporary file");
558 strncpy(tmpfil, cp, sizeof(tmpfil));
559 chmod (tmpfil, 0600);
563 hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
565 for (compnum = 1, state = FLD;;) {
566 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
571 cp = add (buf, NULL);
572 while (state == FLDPLUS) {
573 state = m_getfld (state, name, buf, sizeof(buf), in);
576 putfmt (name, cp, out);
580 finish_headers (out);
585 finish_headers (out);
588 fprintf (out, "\n%s", buf);
589 while (state == BODY) {
590 state = m_getfld (state, name, buf, sizeof(buf), in);
596 finish_headers (out);
601 adios (NULL, "message format error in component #%d", compnum);
604 adios (NULL, "getfld() returned %d", state);
621 * Here's how we decide which address to use as the envelope-from
624 * - If we were given an Envelope-From header, use that.
625 * - If we were given multiple addresses in the From: header, use
626 * the Sender: address
627 * - Otherwise, use the address on the From: line
630 if (msgflags & MEFM) {
632 } else if (fromcount > 1) {
638 /* If we are doing a "whom" check */
640 verify_all_addresses (1, envelope);
644 if (msgflags & MINV) {
645 make_bcc_file (dashstuff);
646 if (msgflags & MVIS) {
647 verify_all_addresses (verbose, envelope);
648 post (tmpfil, 0, verbose, envelope);
650 post (bccfil, 1, verbose, envelope);
653 post (tmpfil, 0, isatty (1), envelope);
660 printf (partno ? "Partial Message #%s Processed\n" : "Message Processed\n",
672 putfmt (char *name, char *str, FILE *out)
674 int count, grp, i, keep;
677 struct mailname *mp = NULL, *np = NULL;
680 while (*str == ' ' || *str == '\t')
683 if (msgstate == NORMAL && uprf (name, "resent")) {
684 advise (NULL, "illegal header line -- %s:", name);
689 if ((i = get_header (name, hdrtab)) == NOTOK) {
690 fprintf (out, "%s: %s", name, str);
695 if (hdr->flags & HIGN) {
698 if (hdr->flags & HBAD) {
699 advise (NULL, "illegal header line -- %s:", name);
703 msgflags |= (hdr->set & ~(MVIS | MINV));
705 if (hdr->flags & HSUB)
706 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
707 if (hdr->flags & HFCC) {
708 if ((cp = strrchr(str, '\n')))
710 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
712 insert_fcc (hdr, pp);
714 insert_fcc (hdr, pp);
718 if (!(hdr->flags & HADR)) {
719 fprintf (out, "%s: %s", name, str);
723 tmpaddrs.m_next = NULL;
724 for (count = 0; (cp = getname (str)); count++)
725 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
729 tmpaddrs.m_next = mp;
733 if (hdr->flags & HTRY)
739 if (hdr->flags & HNIL)
740 fprintf (out, "%s: %s", name, str);
743 advise (NULL, "%s: field requires at least one address", name);
750 if (count > 1 && (hdr->flags & HONE)) {
751 advise (NULL, "%s: field only permits one address", name);
756 nameoutput = linepos = 0;
757 snprintf (namep, sizeof(namep), "%s%s",
758 (hdr->flags & HMNG) ? "Original-" : "", name);
760 for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
761 if (mp->m_nohost) { /* also used to test (hdr->flags & HTRY) */
762 /* The address doesn't include a host, so it might be an alias. */
763 pp = akvalue (mp->m_mbox); /* do mh alias substitution */
764 qp = akvisible () ? mp->m_mbox : "";
767 putgrp (namep, np->m_gname, out, hdr->flags);
768 while ((cp = getname (pp))) {
769 if (!(mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
775 * If it's a From: or Resent-From: header, save the address
776 * for later possible use (as the envelope address for SMTP)
779 if ((msgstate == RESENT) ? (hdr->set & MRFM)
780 : (hdr->set & MFRM)) {
781 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
782 from[sizeof(from) - 1] = '\0';
787 * Also save the Sender: or Resent-Sender: header as well
790 if ((msgstate == RESENT) ? (hdr->set & MRSN)
791 : (hdr->set & MSND)) {
792 strncpy(sender, auxformat(mp, 0), sizeof(sender) - 1);
793 sender[sizeof(sender) - 1] = '\0';
798 * ALSO ... save Envelope-From
801 if (hdr->set & MEFM) {
802 strncpy(efrom, auxformat(mp, 0), sizeof(efrom) - 1);
803 efrom[sizeof(efrom) - 1] = '\0';
806 if (hdr->flags & HBCC)
809 mp->m_ingrp = np->m_ingrp;
812 putgrp (namep, mp->m_gname, out, hdr->flags);
815 if (putadr (namep, qp, mp, out, hdr->flags))
816 msgflags |= (hdr->set & (MVIS | MINV));
825 /* Address includes a host, so no alias substitution is needed. */
828 * If it's a From: or Resent-From header, save the address
829 * for later possible use (as the envelope address for SMTP)
832 if ((msgstate == RESENT) ? (hdr->set & MRFM)
833 : (hdr->set & MFRM)) {
834 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
839 * Also save the Sender: header as well
842 if ((msgstate == RESENT) ? (hdr->set & MRSN)
843 : (hdr->set & MSND)) {
844 strncpy(sender, auxformat(mp, 0), sizeof(sender) - 1);
845 sender[sizeof(sender) - 1] = '\0';
850 * ALSO ... save Envelope-From
853 if (hdr->set & MEFM) {
854 strncpy(efrom, auxformat(mp, 0), sizeof(efrom) - 1);
855 efrom[sizeof(efrom) - 1] = '\0';
858 if (hdr->flags & HBCC)
861 putgrp (namep, mp->m_gname, out, hdr->flags);
864 keep = putadr (namep, "", mp, out, hdr->flags);
868 msgflags |= (hdr->set & (MVIS | MINV));
874 if (grp > 0 && (hdr->flags & HNGR)) {
875 advise (NULL, "%s: field does not allow groups", name);
888 char myhost[BUFSIZ], sigbuf[BUFSIZ];
895 strncpy (from, getlocaladdr(), sizeof(from));
896 strncpy (myhost, LocalName (0), sizeof(myhost));
898 for (cp = myhost; *cp; cp++)
901 if ((cp = getfullname ()) && *cp) {
902 strncpy (sigbuf, cp, sizeof(sigbuf));
903 snprintf (signature, sizeof(signature), "%s <%s>",
904 sigbuf, getlocaladdr());
905 if ((cp = getname (signature)) == NULL)
906 adios (NULL, "getname () failed -- you lose extraordinarily big");
907 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL)) == NULL)
908 adios (NULL, "bad signature '%s'", sigbuf);
913 strncpy (signature, getlocaladdr(), sizeof(signature));
919 * Now that we've outputted the header fields in the draft
920 * message, we will now output any remaining header fields
921 * that we need to add/create.
925 finish_headers (FILE *out)
929 if (!(msgflags & MFRM)) {
931 * A From: header is now required in the draft.
933 advise (NULL, "message has no From: header");
934 advise (NULL, "See default components files for examples");
938 if (fromcount > 1 && seensender == 0) {
939 advise (NULL, "A Sender: header is required with multiple "
947 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
949 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
950 (int) getpid (), (long) tclock, LocalName (1));
951 if (!(msgflags & MVIS))
952 fprintf (out, "Bcc: Blind Distribution List: ;\n");
956 if (!(msgflags & MDAT)) {
957 advise (NULL, "message has no Date: header");
960 if (!(msgflags & MFRM)) {
961 advise (NULL, "message has no From: header");
964 if (!(msgflags & MRFM)) {
965 advise (NULL, "message has no Resent-From: header");
966 advise (NULL, "See default components files for examples");
969 if (fromcount > 1 && seensender == 0) {
970 advise (NULL, "A Resent-Sender: header is required with "
971 "multiple Resent-From: addresses");
978 fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
980 fprintf (out, "Resent-Message-ID: <%d.%ld@%s>\n",
981 (int) getpid (), (long) tclock, LocalName (1));
982 if (!(msgflags & MVIS))
983 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
988 adios (NULL, "re-format message and try again");
990 adios (NULL, "no addressees");
995 get_header (char *header, struct headers *table)
999 for (h = table; h->value; h++)
1000 if (!mh_strcasecmp (header, h->value))
1008 putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flags)
1012 char buffer[BUFSIZ];
1014 if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
1016 if ((flags & (HBCC | HDCC | HEFM)) || mp->m_ingrp)
1020 fprintf (out, "%s: ", name);
1021 linepos += (nameoutput = strlen (name) + 2);
1024 if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
1025 mp->m_pers = getcpy (aka);
1028 snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
1031 cp = adrformat (mp);
1038 if (linepos != nameoutput) {
1039 if (len + linepos + 2 > outputlinelen)
1040 fprintf (out, ",\n%*s", linepos = nameoutput, "");
1050 return (flags & HTRY);
1055 putgrp (char *name, char *group, FILE *out, unsigned int flags)
1064 fprintf (out, "%s: ", name);
1065 linepos += (nameoutput = strlen (name) + 2);
1068 cp = concat (group, ";", NULL);
1071 if (linepos > nameoutput) {
1072 if (len + linepos + 2 > outputlinelen) {
1073 fprintf (out, ",\n%*s", nameoutput, "");
1074 linepos = nameoutput;
1088 insert (struct mailname *np)
1090 struct mailname *mp;
1092 if (np->m_mbox == NULL)
1095 for (mp = np->m_type == LOCALHOST ? &localaddrs
1096 : np->m_type == UUCPHOST ? &uuaddrs
1100 if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
1101 && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox)
1102 && np->m_bcc == mp->m_next->m_bcc)
1115 struct mailname *mp;
1117 printf ("-------\n\t-- Addresses --\nlocal:\t");
1118 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1119 printf ("%s%s%s", mp->m_mbox,
1120 mp->m_bcc ? "[BCC]" : "",
1121 mp->m_next ? ",\n\t" : "");
1123 printf ("\nnet:\t");
1124 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1125 printf ("%s%s@%s%s%s", mp->m_path ? mp->m_path : "",
1126 mp->m_mbox, mp->m_host,
1127 mp->m_bcc ? "[BCC]" : "",
1128 mp->m_next ? ",\n\t" : "");
1130 printf ("\nuucp:\t");
1131 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1132 printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1133 mp->m_bcc ? "[BCC]" : "",
1134 mp->m_next ? ",\n\t" : "");
1136 printf ("\n\t-- Folder Copies --\nfcc:\t");
1137 for (i = 0; i < fccind; i++)
1138 printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1146 struct mailname *mp;
1148 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1149 if (annoaux (mp) == NOTOK)
1152 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1153 if (annoaux (mp) == NOTOK)
1156 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1157 if (annoaux (mp) == NOTOK)
1167 annoaux (struct mailname *mp)
1170 char buffer[BUFSIZ];
1172 snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1173 i = strlen (buffer);
1175 return (write (pfd, buffer, i) == i ? OK : NOTOK);
1180 insert_fcc (struct headers *hdr, unsigned char *pp)
1184 for (cp = pp; isspace (*cp); cp++)
1186 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
1194 adios (NULL, "too many %ss", hdr->value);
1195 fccfold[fccind++] = getcpy (cp);
1203 make_bcc_file (int dashstuff)
1211 tfile = m_mktemp2(NULL, "bccs", NULL, &out);
1212 if (tfile == NULL) adios("bcc", "unable to create temporary file");
1213 chmod (bccfil, 0600);
1214 strncpy (bccfil, tfile, sizeof(bccfil));
1216 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1218 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
1219 (int) getpid (), (long) tclock, LocalName (1));
1221 fprintf (out, "Subject: %s", subject);
1222 fprintf (out, "BCC:\n");
1225 * Use MIME encapsulation for Bcc messages
1231 * Check if any lines in the message clash with the
1232 * prefix for the MIME multipart separator. If there
1233 * is a clash, increment one of the letters in the
1234 * prefix and check again.
1236 if ((cp = strchr(prefix, 'a')) == NULL)
1237 adios (NULL, "lost prefix start");
1238 while (find_prefix () == NOTOK) {
1243 adios (NULL, "can't find a unique delimiter string");
1248 fprintf (out, "%s: %s\n%s: multipart/digest; boundary=\"",
1249 VRSN_FIELD, VRSN_VALUE, TYPE_FIELD);
1250 fprintf (out, "%s\"\n\n--%s\n\n", prefix, prefix);
1252 fprintf (out, "\n------- Blind-Carbon-Copy\n\n");
1258 * Do mhl filtering of Bcc messages instead
1259 * of MIME encapsulation.
1261 if (filter != NULL) {
1262 vec[0] = r1bindex (mhlproc, '/');
1264 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1268 adios ("fork", "unable to");
1271 dup2 (fileno (out), 1);
1274 vec[i++] = "-forward";
1279 /* was the flag -[no]dashstuffing specified? */
1281 vec[i++] = "-dashstuffing";
1282 else if (dashstuff < 0)
1283 vec[i++] = "-nodashstuffing";
1286 execvp (mhlproc, vec);
1287 fprintf (stderr, "unable to exec ");
1292 pidXwait (child_id, mhlproc);
1296 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
1297 adios (tmpfil, "unable to re-open");
1300 * If using MIME encapsulation, or if the -nodashstuffing
1301 * flag was given, then just copy message. Else do
1302 * RFC934 quoting (dashstuffing).
1304 if (mime || dashstuff < 0)
1305 cpydata (fd, fileno (out), tmpfil, bccfil);
1307 cpydgst (fd, fileno (out), tmpfil, bccfil);
1311 fseek (out, 0L, SEEK_END);
1313 fprintf (out, "\n--%s--\n", prefix);
1315 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
1321 * Scan message to check if any lines clash with
1322 * the prefix of the MIME multipart separator.
1329 unsigned char buffer[BUFSIZ];
1332 if ((in = fopen (tmpfil, "r")) == NULL)
1333 adios (tmpfil, "unable to re-open");
1335 while (fgets (buffer, sizeof(buffer) - 1, in))
1336 if (buffer[0] == '-' && buffer[1] == '-') {
1339 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1343 if (strcmp (buffer + 2, prefix) == 0) {
1354 #define plural(x) (x == 1 ? "" : "s")
1359 if (badadr && unkadr)
1360 die (NULL, "%d address%s unparsable, %d addressee%s undeliverable",
1361 badadr, plural (badadr), unkadr, plural (badadr));
1363 die (NULL, "%d address%s unparsable", badadr, plural (badadr));
1365 die (NULL, "%d addressee%s undeliverable", unkadr, plural (unkadr));
1370 do_addresses (int bccque, int talk)
1374 struct mailname *lp;
1377 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1378 if (lp->m_bcc ? bccque : !bccque) {
1380 printf (" -- Local Recipients --\n");
1381 do_an_address (lp, talk);
1386 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1387 if (lp->m_bcc ? bccque : !bccque) {
1389 printf (" -- UUCP Recipients --\n");
1390 do_an_address (lp, talk);
1395 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1396 if (lp->m_bcc ? bccque : !bccque) {
1398 printf (" -- Network Recipients --\n");
1399 do_an_address (lp, talk);
1405 if (rp_isbad (retval = sm_waend ()))
1406 die (NULL, "problem ending addresses; %s", rp_string (retval));
1411 * MTS-SPECIFIC INTERACTION
1416 * SENDMAIL/SMTP routines
1420 post (char *file, int bccque, int talk, char *envelope)
1425 onex = !(msgflags & MINV) || bccque;
1427 if (msgflags & MINV)
1428 printf (" -- Posting for %s Recipients --\n",
1429 bccque ? "Blind" : "Sighted");
1431 printf (" -- Posting for All Recipients --\n");
1436 if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch, verbose,
1437 snoop, onex, queued, sasl, saslssf,
1438 saslmech, user, tls))
1439 || rp_isbad (retval = sm_winit (envelope)))
1440 die (NULL, "problem initializing server; %s", rp_string (retval));
1442 do_addresses (bccque, talk && verbose);
1443 if ((fd = open (file, O_RDONLY)) == NOTOK)
1444 die (file, "unable to re-open");
1449 sm_end (onex ? OK : DONE);
1453 if (msgflags & MINV)
1454 printf (" -- %s Recipient Copies Posted --\n",
1455 bccque ? "Blind" : "Sighted");
1457 printf (" -- Recipient Copies Posted --\n");
1464 /* Address Verification */
1467 verify_all_addresses (int talk, char *envelope)
1470 struct mailname *lp;
1474 if (!whomsw || checksw)
1475 if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch,
1476 verbose, snoop, 0, queued, sasl,
1477 saslssf, saslmech, user, tls))
1478 || rp_isbad (retval = sm_winit (envelope)))
1479 die (NULL, "problem initializing server; %s", rp_string (retval));
1481 if (talk && !whomsw)
1482 printf (" -- Address Verification --\n");
1483 if (talk && localaddrs.m_next)
1484 printf (" -- Local Recipients --\n");
1485 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1486 do_an_address (lp, talk);
1488 if (talk && uuaddrs.m_next)
1489 printf (" -- UUCP Recipients --\n");
1490 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1491 do_an_address (lp, talk);
1493 if (talk && netaddrs.m_next)
1494 printf (" -- Network Recipients --\n");
1495 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1496 do_an_address (lp, talk);
1499 if (talk && !whomsw)
1500 printf (" -- Address Verification Successful --\n");
1502 if (!whomsw || checksw)
1511 do_an_address (struct mailname *lp, int talk)
1517 switch (lp->m_type) {
1521 strncpy (addr, mbox, sizeof(addr));
1525 mbox = auxformat (lp, 0);
1527 snprintf (addr, sizeof(addr), "%s!%s", lp->m_host, lp->m_mbox);
1530 default: /* let SendMail decide if the host is bad */
1533 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1538 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1540 if (whomsw && !checksw) {
1548 switch (retval = sm_wadr (mbox, host,
1549 lp->m_type != UUCPHOST ? lp->m_path : NULL)) {
1552 printf ("address ok\n");
1558 fprintf (stderr, " %s: ", addr);
1559 fprintf (talk ? stdout : stderr, "loses; %s\n",
1560 rp_string (retval));
1566 fprintf (stderr, " %s: ", addr);
1567 die (NULL, "unexpected response; %s", rp_string (retval));
1575 do_text (char *file, int fd)
1580 lseek (fd, (off_t) 0, SEEK_SET);
1582 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1583 if (rp_isbad (retval = sm_wtxt (buf, state)))
1584 die (NULL, "problem writing text; %s\n", rp_string (retval));
1588 die (file, "problem reading from");
1590 switch (retval = sm_wtend ()) {
1596 die (NULL, "posting failed; %s", rp_string (retval));
1599 die (NULL, "unexpected response; %s", rp_string (retval));
1614 if (msgflags & MINV)
1617 if (!whomsw || checksw)
1630 hstat = SIGNAL2 (SIGHUP, sigser);
1631 istat = SIGNAL2 (SIGINT, sigser);
1632 qstat = SIGNAL2 (SIGQUIT, sigser);
1633 tstat = SIGNAL2 (SIGTERM, sigser);
1643 SIGNAL (SIGHUP, hstat);
1644 SIGNAL (SIGINT, istat);
1645 SIGNAL (SIGQUIT, qstat);
1646 SIGNAL (SIGTERM, tstat);
1654 p_refile (char *file)
1662 printf (" -- Filing Folder Copies --\n");
1663 for (i = 0; i < fccind; i++)
1664 fcc (file, fccfold[i]);
1666 printf (" -- Folder Copies Filed --\n");
1671 * Call the `fileproc' to add the file to the folder.
1675 fcc (char *file, char *folder)
1682 printf (" %sFcc %s: ", msgstate == RESENT ? "Resent-" : "", folder);
1685 for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
1691 fprintf (stderr, " %sFcc %s: ",
1692 msgstate == RESENT ? "Resent-" : "", folder);
1693 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
1697 /* see if we need to add `+' */
1698 snprintf (fold, sizeof(fold), "%s%s",
1699 *folder == '+' || *folder == '@' ? "" : "+", folder);
1701 /* now exec the fileproc */
1702 execlp (fileproc, r1bindex (fileproc, '/'),
1703 "-link", "-file", file, fold, NULL);
1707 if ((status = pidwait (child_id, OK))) {
1709 fprintf (stderr, " %sFcc %s: ",
1710 msgstate == RESENT ? "Resent-" : "", folder);
1711 pidstatus (status, verbose ? stdout : stderr, NULL);
1714 printf ("folder ok\n");
1726 die (char *what, char *fmt, ...)
1731 if (msgflags & MINV)
1734 if (!whomsw || checksw)
1738 advertise (what, NULL, fmt, ap);