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>
31 # include <mts/smtp/smtp.h>
35 # define SASLminc(a) (a)
36 #else /* CYRUS_SASL */
37 # define SASLminc(a) 0
38 #endif /* CYRUS_SASL */
41 # define TLSminc(a) (a)
42 #else /* TLS_SUPPORT */
44 #endif /* TLS_SUPPORT */
46 #define FCCS 10 /* max number of fccs allowed */
48 #define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : c)
50 /* In the following array of structures, the numeric second field of the
51 structures (minchars) is apparently used like this:
53 -# : Switch can be abbreviated to # characters; switch hidden in -help.
54 0 : Switch can't be abbreviated; switch shown in -help.
55 # : Switch can be abbreviated to # characters; switch shown in -help. */
57 static struct swit switches[] = {
59 { "alias aliasfile", 0 },
61 { "check", -5 }, /* interface from whom */
63 { "nocheck", -7 }, /* interface from whom */
67 { "dist", -4 }, /* interface from dist */
69 { "filter filterfile", 0 },
77 { "library directory", -7 }, /* interface from send, whom */
95 { "whom", -4 }, /* interface from whom */
97 { "width columns", 0 },
102 #define BITSTUFFSW 22
103 { "dashstuffing", -12 }, /* should we dashstuff BCC messages? */
104 #define NBITSTUFFSW 23
105 { "nodashstuffing", -14 },
107 { "mail", -4 }, /* specify MAIL smtp mode */
109 { "saml", -4 }, /* specify SAML smtp mode */
111 { "send", -4 }, /* specify SEND smtp mode */
113 { "soml", -4 }, /* specify SOML smtp mode */
115 { "idanno number", -6 }, /* interface from send */
117 { "deliver address-list", -7 },
119 { "client host", -6 },
121 { "server host", -6 }, /* specify alternate SMTP server */
123 { "snoop", -5 }, /* snoop the SMTP transaction */
125 { "fill-in file", -7 },
133 { "sasl", SASLminc(-4) },
135 { "nosasl", SASLminc(-6) },
136 #define SASLMXSSFSW 39
137 { "saslmaxssf", SASLminc(-10) },
138 #define SASLMECHSW 40
139 { "saslmech", SASLminc(-5) },
141 { "user", SASLminc(-4) },
143 { "port server port name/number", 4 },
145 { "tls", TLSminc(-3) },
146 #define FILEPROCSW 44
161 * flags for headers->flags
163 #define HNOP 0x0000 /* just used to keep .set around */
164 #define HBAD 0x0001 /* bad header - don't let it through */
165 #define HADR 0x0002 /* header has an address field */
166 #define HSUB 0x0004 /* Subject: header */
167 #define HTRY 0x0008 /* try to send to addrs on header */
168 #define HBCC 0x0010 /* don't output this header */
169 #define HMNG 0x0020 /* munge this header */
170 #define HNGR 0x0040 /* no groups allowed in this header */
171 #define HFCC 0x0080 /* FCC: type header */
172 #define HNIL 0x0100 /* okay for this header not to have addrs */
173 #define HIGN 0x0200 /* ignore this header */
174 #define HDCC 0x0400 /* another undocumented feature */
177 * flags for headers->set
179 #define MFRM 0x0001 /* we've seen a From: */
180 #define MDAT 0x0002 /* we've seen a Date: */
181 #define MRFM 0x0004 /* we've seen a Resent-From: */
182 #define MVIS 0x0008 /* we've seen sighted addrs */
183 #define MINV 0x0010 /* we've seen blind addrs */
186 static struct headers NHeaders[] = {
187 { "Return-Path", HBAD, 0 },
188 { "Received", HBAD, 0 },
189 { "Reply-To", HADR|HNGR, 0 },
190 { "From", HADR|HNGR, MFRM },
191 { "Sender", HADR|HBAD, 0 },
193 { "Subject", HSUB, 0 },
194 { "To", HADR|HTRY, MVIS },
195 { "cc", HADR|HTRY, MVIS },
196 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
197 { "Dcc", HADR|HTRY|HDCC|HNIL, MVIS }, /* sorta cc & bcc combined */
198 { "Message-ID", HBAD, 0 },
203 static struct headers RHeaders[] = {
204 { "Resent-Reply-To", HADR|HNGR, 0 },
205 { "Resent-From", HADR|HNGR, MRFM },
206 { "Resent-Sender", HADR|HBAD, 0 },
207 { "Resent-Date", HBAD, 0 },
208 { "Resent-Subject", HSUB, 0 },
209 { "Resent-To", HADR|HTRY, MVIS },
210 { "Resent-cc", HADR|HTRY, MVIS },
211 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
212 { "Resent-Message-ID", HBAD, 0 },
213 { "Resent-Fcc", HFCC, 0 },
214 { "Reply-To", HADR, 0 },
215 { "From", HADR|HNGR, MFRM },
216 { "Sender", HADR|HNGR, 0 },
217 { "Date", HNOP, MDAT },
218 { "To", HADR|HNIL, 0 },
219 { "cc", HADR|HNIL, 0 },
220 { "Bcc", HADR|HTRY|HBCC|HNIL, 0 },
225 static short fccind = 0; /* index into fccfold[] */
226 static short outputlinelen = OUTPUTLINELEN;
228 static int pfd = NOTOK; /* fd to write annotation list to */
229 static uid_t myuid= -1; /* my user id */
230 static gid_t mygid= -1; /* my group id */
231 static int recipients = 0; /* how many people will get a copy */
232 static int unkadr = 0; /* how many of those were unknown */
233 static int badadr = 0; /* number of bad addrs */
234 static int badmsg = 0; /* message has bad semantics */
235 static int verbose = 0; /* spell it out */
236 static int format = 1; /* format addresses */
237 static int mime = 0; /* use MIME-style encapsulations for Bcc */
238 static int msgid = 0; /* add msgid */
239 static int debug = 0; /* debugging post */
240 static int watch = 0; /* watch the delivery process */
241 static int whomsw = 0; /* we are whom not post */
242 static int checksw = 0; /* whom -check */
243 static int linepos=0; /* putadr()'s position on the line */
244 static int nameoutput=0; /* putadr() has output header name */
245 static int sasl=0; /* Use SASL auth for SMTP */
246 static int saslssf=-1; /* Our maximum SSF for SASL */
247 static char *saslmech=NULL; /* Force use of particular SASL mech */
248 static char *user=NULL; /* Authenticate as this user */
249 static char *port="smtp"; /* Name of server port for SMTP */
250 static int tls=0; /* Use TLS for encryption */
252 static unsigned msgflags = 0; /* what we've seen */
256 static int msgstate = NORMAL;
258 static time_t tclock = 0; /* the time we started (more or less) */
260 static SIGNAL_HANDLER hstat, istat, qstat, tstat;
262 static char tmpfil[BUFSIZ];
263 static char bccfil[BUFSIZ];
265 static char from[BUFSIZ]; /* my network address */
266 static char signature[BUFSIZ]; /* my signature */
267 static char *filter = NULL; /* the filter for BCC'ing */
268 static char *subject = NULL; /* the subject field for BCC'ing */
269 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
271 static struct headers *hdrtab; /* table for the message we're doing */
273 static struct mailname localaddrs; /* local addrs */
274 static struct mailname netaddrs; /* network addrs */
275 static struct mailname uuaddrs; /* uucp addrs */
276 static struct mailname tmpaddrs; /* temporary queue */
279 static int snoop = 0;
280 static int smtpmode = S_MAIL;
281 static char *clientsw = NULL;
282 static char *serversw = NULL;
284 extern struct smtp sm_reply;
287 static char prefix[] = "----- =_aaaaaaaaaa";
289 static int fill_up = 0;
290 static char *fill_in = NULL;
291 static char *partno = NULL;
292 static int queued = 0;
294 extern boolean draft_from_masquerading; /* defined in mts.c */
299 static void putfmt (char *, char *, FILE *);
300 static void start_headers (void);
301 static void finish_headers (FILE *);
302 static int get_header (char *, struct headers *);
303 static int putadr (char *, char *, struct mailname *, FILE *, unsigned int);
304 static void putgrp (char *, char *, FILE *, unsigned int);
305 static int insert (struct mailname *);
306 static void pl (void);
307 static void anno (void);
308 static int annoaux (struct mailname *);
309 static void insert_fcc (struct headers *, unsigned char *);
310 static void make_bcc_file (int);
311 static void verify_all_addresses (int);
312 static void chkadr (void);
313 static void sigon (void);
314 static void sigoff (void);
315 static void p_refile (char *);
316 static void fcc (char *, char *);
317 static void die (char *, char *, ...);
318 static void post (char *, int, int);
319 static void do_text (char *file, int fd);
320 static void do_an_address (struct mailname *, int);
321 static void do_addresses (int, int);
322 static int find_prefix (void);
326 main (int argc, char **argv)
328 int state, compnum, dashstuff = 0;
329 char *cp, *msg = NULL, **argp, **arguments;
330 char buf[BUFSIZ], name[NAMESZ];
334 setlocale(LC_ALL, "");
336 invo_name = r1bindex (argv[0], '/');
338 /* foil search of user profile/context */
339 if (context_foil (NULL) == -1)
342 mts_init (invo_name);
343 arguments = getarguments (invo_name, argc, argv, 0);
346 while ((cp = *argp++)) {
348 switch (smatch (++cp, switches)) {
350 ambigsw (cp, switches);
353 adios (NULL, "-%s unknown", cp);
356 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
357 print_help (buf, switches, 0);
360 print_version(invo_name);
364 if (!(cp = *argp++) || *cp == '-')
365 adios (NULL, "missing argument to %s", argp[-2]);
366 /* create a minimal context */
367 if (context_foil (cp) == -1)
372 if (!(cp = *argp++) || *cp == '-')
373 adios (NULL, "missing argument to %s", argp[-2]);
374 if ((state = alias (cp)) != AK_OK)
375 adios (NULL, "aliasing error in %s - %s",
376 cp, akerror (state));
395 if (!(filter = *argp++) || *filter == '-')
396 adios (NULL, "missing argument to %s", argp[-2]);
451 if (!(cp = *argp++) || *cp == '-')
452 adios (NULL, "missing argument to %s", argp[-2]);
453 if ((outputlinelen = atoi (cp)) < 10)
454 adios (NULL, "impossible width %d", outputlinelen);
458 if (!(cp = *argp++) || *cp == '-')
459 adios (NULL, "missing argument to %s", argp[-2]);
460 if ((pfd = atoi (cp)) <= 2)
461 adios (NULL, "bad argument %s %s", argp[-2], cp);
465 if (!(cp = *argp++) || *cp == '-')
466 adios (NULL, "missing argument to %s", argp[-2]);
472 if (!(cp = *argp++) || *cp == '-')
473 adios (NULL, "missing argument to %s", argp[-2]);
492 if (!(clientsw = *argp++) || *clientsw == '-')
493 adios (NULL, "missing argument to %s", argp[-2]);
496 if (!(serversw = *argp++) || *serversw == '-')
497 adios (NULL, "missing argument to %s", argp[-2]);
505 if (!(fill_in = *argp++) || *fill_in == '-')
506 adios (NULL, "missing argument to %s", argp[-2]);
512 if (!(partno = *argp++) || *partno == '-')
513 adios (NULL, "missing argument to %s", argp[-2]);
529 if (!(cp = *argp++) || *cp == '-')
530 adios (NULL, "missing argument to %s", argp[-2]);
535 if (!(saslmech = *argp++) || *saslmech == '-')
536 adios (NULL, "missing argument to %s", argp[-2]);
540 if (!(user = *argp++) || *user == '-')
541 adios (NULL, "missing argument to %s", argp[-2]);
545 if (!(port = *argp++) || *port == '-')
546 adios (NULL, "missing argument to %s", argp[-2]);
554 if (!(cp = *argp++) || *cp == '-')
555 adios (NULL, "missing argument to %s", argp[-2]);
560 if (!(cp = *argp++) || *cp == '-')
561 adios (NULL, "missing argument to %s", argp[-2]);
567 adios (NULL, "only one message at a time!");
575 adios (NULL, "usage: %s [switches] file", invo_name);
577 if (outputlinelen < 10)
578 adios (NULL, "impossible width %d", outputlinelen);
580 if ((in = fopen (msg, "r")) == NULL)
581 adios (msg, "unable to open");
586 discard (out = stdout); /* XXX: reference discard() to help loader */
589 if ((out = fopen (fill_in ? fill_in : "/dev/null", "w")) == NULL)
590 adios ("/dev/null", "unable to open");
592 char *cp = m_mktemp(m_maildir(invo_name), NULL, &out);
594 cp = m_mktemp2(NULL, invo_name, NULL, &out);
596 adios ("post", "unable to create temporary file");
599 strncpy(tmpfil, cp, sizeof(tmpfil));
600 chmod (tmpfil, 0600);
604 hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
606 for (compnum = 1, state = FLD;;) {
607 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
612 cp = add (buf, NULL);
613 while (state == FLDPLUS) {
614 state = m_getfld (state, name, buf, sizeof(buf), in);
617 putfmt (name, cp, out);
621 finish_headers (out);
626 finish_headers (out);
627 if (whomsw && !fill_in)
629 fprintf (out, "\n%s", buf);
630 while (state == BODY) {
631 state = m_getfld (state, name, buf, sizeof(buf), in);
637 finish_headers (out);
642 adios (NULL, "message format error in component #%d", compnum);
645 adios (NULL, "getfld() returned %d", state);
661 /* If we are doing a "whom" check */
664 verify_all_addresses (1);
668 if (msgflags & MINV) {
669 make_bcc_file (dashstuff);
670 if (msgflags & MVIS) {
671 verify_all_addresses (verbose);
672 post (tmpfil, 0, verbose);
674 post (bccfil, 1, verbose);
677 post (tmpfil, 0, isatty (1));
684 printf (partno ? "Partial Message #%s Processed\n" : "Message Processed\n",
696 putfmt (char *name, char *str, FILE *out)
698 int count, grp, i, keep;
701 struct mailname *mp = NULL, *np = NULL;
704 while (*str == ' ' || *str == '\t')
707 if (msgstate == NORMAL && uprf (name, "resent")) {
708 advise (NULL, "illegal header line -- %s:", name);
713 if ((i = get_header (name, hdrtab)) == NOTOK) {
714 fprintf (out, "%s: %s", name, str);
719 if (hdr->flags & HIGN) {
721 fprintf (out, "%s: %s", name, str);
724 if (hdr->flags & HBAD) {
726 fprintf (out, "%s: %s", name, str);
728 advise (NULL, "illegal header line -- %s:", name);
733 msgflags |= (hdr->set & ~(MVIS | MINV));
735 if (hdr->flags & HSUB)
736 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
737 if (hdr->flags & HFCC) {
739 fprintf (out, "%s: %s", name, str);
743 if ((cp = strrchr(str, '\n')))
745 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
747 insert_fcc (hdr, pp);
749 insert_fcc (hdr, pp);
753 if (!(hdr->flags & HADR)) {
754 fprintf (out, "%s: %s", name, str);
758 tmpaddrs.m_next = NULL;
759 for (count = 0; (cp = getname (str)); count++)
760 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
764 tmpaddrs.m_next = mp;
768 if (hdr->flags & HTRY)
774 if (hdr->flags & HNIL)
775 fprintf (out, "%s: %s", name, str);
778 advise (NULL, "%s: field requires at least one address", name);
785 nameoutput = linepos = 0;
786 snprintf (namep, sizeof(namep), "%s%s",
787 !fill_in && (hdr->flags & HMNG) ? "Original-" : "", name);
789 for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
790 if (mp->m_nohost) { /* also used to test (hdr->flags & HTRY) */
791 /* The address doesn't include a host, so it might be an alias. */
792 pp = akvalue (mp->m_mbox); /* do mh alias substitution */
793 qp = akvisible () ? mp->m_mbox : "";
796 putgrp (namep, np->m_gname, out, hdr->flags);
797 while ((cp = getname (pp))) {
798 if (!(mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
803 if (draft_from_masquerading && ((msgstate == RESENT)
805 : (hdr->set & MFRM)))
806 /* The user manually specified a [Resent-]From: address in
807 their draft and the "masquerade:" line in mts.conf
808 doesn't contain "draft_from", so we'll set things up to
809 use the actual email address embedded in the draft
810 [Resent-]From: (after alias substitution, and without the
811 GECOS full name or angle brackets) as the envelope
813 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
815 if (hdr->flags & HBCC)
818 mp->m_ingrp = np->m_ingrp;
821 putgrp (namep, mp->m_gname, out, hdr->flags);
824 if (putadr (namep, qp, mp, out, hdr->flags))
825 msgflags |= (hdr->set & (MVIS | MINV));
834 /* Address includes a host, so no alias substitution is needed. */
835 if (draft_from_masquerading && ((msgstate == RESENT)
837 : (hdr->set & MFRM)))
838 /* The user manually specified a [Resent-]From: address in
839 their draft and the "masquerade:" line in mts.conf
840 doesn't contain "draft_from", so we'll set things up to
841 use the actual email address embedded in the draft
842 [Resent-]From: (after alias substitution, and without the
843 GECOS full name or angle brackets) as the envelope
845 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
847 if (hdr->flags & HBCC)
850 putgrp (namep, mp->m_gname, out, hdr->flags);
853 keep = putadr (namep, "", mp, out, hdr->flags);
857 msgflags |= (hdr->set & (MVIS | MINV));
863 if (grp > 0 && (hdr->flags & HNGR)) {
864 advise (NULL, "%s: field does not allow groups", name);
868 if (fill_in && grp > 0)
879 char myhost[BUFSIZ], sigbuf[BUFSIZ];
886 strncpy (from, getlocaladdr(), sizeof(from));
887 strncpy (myhost, LocalName (0), sizeof(myhost));
889 for (cp = myhost; *cp; cp++)
892 if ((cp = getfullname ()) && *cp) {
893 strncpy (sigbuf, cp, sizeof(sigbuf));
894 snprintf (signature, sizeof(signature), "%s <%s>",
895 sigbuf, getlocaladdr());
896 if ((cp = getname (signature)) == NULL)
897 adios (NULL, "getname () failed -- you lose extraordinarily big");
898 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL)) == NULL)
899 adios (NULL, "bad signature '%s'", sigbuf);
904 strncpy (signature, getlocaladdr(), sizeof(signature));
910 * Now that we've outputted the header fields in the draft
911 * message, we will now output any remaining header fields
912 * that we need to add/create.
916 finish_headers (FILE *out)
920 if (whomsw && !fill_up)
923 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
925 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
926 (int) getpid (), (long) tclock, LocalName (1));
927 if (msgflags & MFRM) {
928 /* There was already a From: in the draft. Don't add one. */
929 if (!draft_from_masquerading)
930 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
931 so we'll reveal the user's actual account@thismachine
932 address in a Sender: header (and use it as the envelope
934 fprintf (out, "Sender: %s\n", from);
937 /* Construct a From: header. */
938 fprintf (out, "From: %s\n", signature);
942 if (!(msgflags & MVIS))
943 fprintf (out, "Bcc: Blind Distribution List: ;\n");
947 if (!(msgflags & MDAT)) {
948 advise (NULL, "message has no Date: header");
951 if (!(msgflags & MFRM)) {
952 advise (NULL, "message has no From: header");
955 if (whomsw && !fill_up)
958 fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
960 fprintf (out, "Resent-Message-ID: <%d.%ld@%s>\n",
961 (int) getpid (), (long) tclock, LocalName (1));
962 if (msgflags & MRFM) {
963 /* There was already a Resent-From: in draft. Don't add one. */
964 if (!draft_from_masquerading)
965 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
966 so we'll reveal the user's actual account@thismachine
967 address in a Sender: header (and use it as the envelope
969 fprintf (out, "Resent-Sender: %s\n", from);
972 /* Construct a Resent-From: header. */
973 fprintf (out, "Resent-From: %s\n", signature);
976 if (!(msgflags & MVIS))
977 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
982 adios (NULL, "re-format message and try again");
984 adios (NULL, "no addressees");
989 get_header (char *header, struct headers *table)
993 for (h = table; h->value; h++)
994 if (!mh_strcasecmp (header, h->value))
1002 putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flags)
1006 char buffer[BUFSIZ];
1008 if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
1010 if ((!fill_in && (flags & (HBCC | HDCC))) || mp->m_ingrp)
1014 fprintf (out, "%s: ", name);
1015 linepos += (nameoutput = strlen (name) + 2);
1018 if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
1019 mp->m_pers = getcpy (aka);
1021 if (mp->m_gname && !fill_in) {
1022 snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
1025 cp = adrformat (mp);
1032 if (linepos != nameoutput) {
1033 if (len + linepos + 2 > outputlinelen)
1034 fprintf (out, ",\n%*s", linepos = nameoutput, "");
1044 return (flags & HTRY);
1049 putgrp (char *name, char *group, FILE *out, unsigned int flags)
1054 if (!fill_in && (flags & HBCC))
1058 fprintf (out, "%s: ", name);
1059 linepos += (nameoutput = strlen (name) + 2);
1061 linepos -= strlen (group);
1064 cp = fill_in ? group : concat (group, ";", NULL);
1067 if (linepos > nameoutput) {
1068 if (len + linepos + 2 > outputlinelen) {
1069 fprintf (out, ",\n%*s", nameoutput, "");
1070 linepos = nameoutput;
1084 insert (struct mailname *np)
1086 struct mailname *mp;
1088 if (np->m_mbox == NULL)
1091 for (mp = np->m_type == LOCALHOST ? &localaddrs
1092 : np->m_type == UUCPHOST ? &uuaddrs
1096 if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
1097 && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox)
1098 && np->m_bcc == mp->m_next->m_bcc)
1111 struct mailname *mp;
1113 printf ("-------\n\t-- Addresses --\nlocal:\t");
1114 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1115 printf ("%s%s%s", mp->m_mbox,
1116 mp->m_bcc ? "[BCC]" : "",
1117 mp->m_next ? ",\n\t" : "");
1119 printf ("\nnet:\t");
1120 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1121 printf ("%s%s@%s%s%s", mp->m_path ? mp->m_path : "",
1122 mp->m_mbox, mp->m_host,
1123 mp->m_bcc ? "[BCC]" : "",
1124 mp->m_next ? ",\n\t" : "");
1126 printf ("\nuucp:\t");
1127 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1128 printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1129 mp->m_bcc ? "[BCC]" : "",
1130 mp->m_next ? ",\n\t" : "");
1132 printf ("\n\t-- Folder Copies --\nfcc:\t");
1133 for (i = 0; i < fccind; i++)
1134 printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1142 struct mailname *mp;
1144 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1145 if (annoaux (mp) == NOTOK)
1148 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1149 if (annoaux (mp) == NOTOK)
1152 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1153 if (annoaux (mp) == NOTOK)
1163 annoaux (struct mailname *mp)
1166 char buffer[BUFSIZ];
1168 snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1169 i = strlen (buffer);
1171 return (write (pfd, buffer, i) == i ? OK : NOTOK);
1176 insert_fcc (struct headers *hdr, unsigned char *pp)
1180 for (cp = pp; isspace (*cp); cp++)
1182 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
1190 adios (NULL, "too many %ss", hdr->value);
1191 fccfold[fccind++] = getcpy (cp);
1199 make_bcc_file (int dashstuff)
1207 tfile = m_mktemp2(NULL, "bccs", NULL, &out);
1208 if (tfile == NULL) adios("bcc", "unable to create temporary file");
1209 chmod (bccfil, 0600);
1210 strncpy (bccfil, tfile, sizeof(bccfil));
1212 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1214 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
1215 (int) getpid (), (long) tclock, LocalName (1));
1216 if (msgflags & MFRM) {
1217 /* There was already a From: in the draft. Don't add one. */
1218 if (!draft_from_masquerading)
1219 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
1220 so we'll reveal the user's actual account@thismachine
1221 address in a Sender: header (and use it as the envelope
1223 fprintf (out, "Sender: %s\n", from);
1226 /* Construct a From: header. */
1227 fprintf (out, "From: %s\n", signature);
1229 fprintf (out, "Subject: %s", subject);
1230 fprintf (out, "BCC:\n");
1233 * Use MIME encapsulation for Bcc messages
1239 * Check if any lines in the message clash with the
1240 * prefix for the MIME multipart separator. If there
1241 * is a clash, increment one of the letters in the
1242 * prefix and check again.
1244 if ((cp = strchr(prefix, 'a')) == NULL)
1245 adios (NULL, "lost prefix start");
1246 while (find_prefix () == NOTOK) {
1251 adios (NULL, "can't find a unique delimiter string");
1256 fprintf (out, "%s: %s\n%s: multipart/digest; boundary=\"",
1257 VRSN_FIELD, VRSN_VALUE, TYPE_FIELD);
1258 fprintf (out, "%s\"\n\n--%s\n\n", prefix, prefix);
1260 fprintf (out, "\n------- Blind-Carbon-Copy\n\n");
1266 * Do mhl filtering of Bcc messages instead
1267 * of MIME encapsulation.
1269 if (filter != NULL) {
1270 vec[0] = r1bindex (mhlproc, '/');
1272 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1276 adios ("fork", "unable to");
1279 dup2 (fileno (out), 1);
1282 vec[i++] = "-forward";
1287 /* was the flag -[no]dashstuffing specified? */
1289 vec[i++] = "-dashstuffing";
1290 else if (dashstuff < 0)
1291 vec[i++] = "-nodashstuffing";
1294 execvp (mhlproc, vec);
1295 fprintf (stderr, "unable to exec ");
1300 pidXwait (child_id, mhlproc);
1304 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
1305 adios (tmpfil, "unable to re-open");
1308 * If using MIME encapsulation, or if the -nodashstuffing
1309 * flag was given, then just copy message. Else do
1310 * RFC934 quoting (dashstuffing).
1312 if (mime || dashstuff < 0)
1313 cpydata (fd, fileno (out), tmpfil, bccfil);
1315 cpydgst (fd, fileno (out), tmpfil, bccfil);
1319 fseek (out, 0L, SEEK_END);
1321 fprintf (out, "\n--%s--\n", prefix);
1323 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
1329 * Scan message to check if any lines clash with
1330 * the prefix of the MIME multipart separator.
1337 unsigned char buffer[BUFSIZ];
1340 if ((in = fopen (tmpfil, "r")) == NULL)
1341 adios (tmpfil, "unable to re-open");
1343 while (fgets (buffer, sizeof(buffer) - 1, in))
1344 if (buffer[0] == '-' && buffer[1] == '-') {
1347 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1351 if (strcmp (buffer + 2, prefix) == 0) {
1362 #define plural(x) (x == 1 ? "" : "s")
1367 if (badadr && unkadr)
1368 die (NULL, "%d address%s unparsable, %d addressee%s undeliverable",
1369 badadr, plural (badadr), unkadr, plural (badadr));
1371 die (NULL, "%d address%s unparsable", badadr, plural (badadr));
1373 die (NULL, "%d addressee%s undeliverable", unkadr, plural (unkadr));
1378 do_addresses (int bccque, int talk)
1382 struct mailname *lp;
1385 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1386 if (lp->m_bcc ? bccque : !bccque) {
1388 printf (" -- Local Recipients --\n");
1389 do_an_address (lp, talk);
1394 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1395 if (lp->m_bcc ? bccque : !bccque) {
1397 printf (" -- UUCP Recipients --\n");
1398 do_an_address (lp, talk);
1403 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1404 if (lp->m_bcc ? bccque : !bccque) {
1406 printf (" -- Network Recipients --\n");
1407 do_an_address (lp, talk);
1414 if (rp_isbad (retval = sm_waend ()))
1415 die (NULL, "problem ending addresses; %s", rp_string (retval));
1416 #endif /* SMTPMTS */
1421 * MTS-SPECIFIC INTERACTION
1426 * SENDMAIL/SMTP routines
1432 post (char *file, int bccque, int talk)
1437 onex = !(msgflags & MINV) || bccque;
1439 if (msgflags & MINV)
1440 printf (" -- Posting for %s Recipients --\n",
1441 bccque ? "Blind" : "Sighted");
1443 printf (" -- Posting for All Recipients --\n");
1448 if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch, verbose,
1449 snoop, onex, queued, sasl, saslssf,
1450 saslmech, user, tls))
1451 || rp_isbad (retval = sm_winit (smtpmode, from)))
1452 die (NULL, "problem initializing server; %s", rp_string (retval));
1454 do_addresses (bccque, talk && verbose);
1455 if ((fd = open (file, O_RDONLY)) == NOTOK)
1456 die (file, "unable to re-open");
1461 sm_end (onex ? OK : DONE);
1465 if (msgflags & MINV)
1466 printf (" -- %s Recipient Copies Posted --\n",
1467 bccque ? "Blind" : "Sighted");
1469 printf (" -- Recipient Copies Posted --\n");
1476 /* Address Verification */
1479 verify_all_addresses (int talk)
1482 struct mailname *lp;
1486 if (!whomsw || checksw)
1487 if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch,
1488 verbose, snoop, 0, queued, sasl,
1489 saslssf, saslmech, user, tls))
1490 || rp_isbad (retval = sm_winit (smtpmode, from)))
1491 die (NULL, "problem initializing server; %s", rp_string (retval));
1493 if (talk && !whomsw)
1494 printf (" -- Address Verification --\n");
1495 if (talk && localaddrs.m_next)
1496 printf (" -- Local Recipients --\n");
1497 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1498 do_an_address (lp, talk);
1500 if (talk && uuaddrs.m_next)
1501 printf (" -- UUCP Recipients --\n");
1502 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1503 do_an_address (lp, talk);
1505 if (talk && netaddrs.m_next)
1506 printf (" -- Network Recipients --\n");
1507 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1508 do_an_address (lp, talk);
1511 if (talk && !whomsw)
1512 printf (" -- Address Verification Successful --\n");
1514 if (!whomsw || checksw)
1523 do_an_address (struct mailname *lp, int talk)
1529 switch (lp->m_type) {
1533 strncpy (addr, mbox, sizeof(addr));
1537 mbox = auxformat (lp, 0);
1539 snprintf (addr, sizeof(addr), "%s!%s", lp->m_host, lp->m_mbox);
1542 default: /* let SendMail decide if the host is bad */
1545 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1550 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1552 if (whomsw && !checksw) {
1560 switch (retval = sm_wadr (mbox, host,
1561 lp->m_type != UUCPHOST ? lp->m_path : NULL)) {
1564 printf ("address ok\n");
1570 fprintf (stderr, " %s: ", addr);
1571 fprintf (talk ? stdout : stderr, "loses; %s\n",
1572 rp_string (retval));
1578 fprintf (stderr, " %s: ", addr);
1579 die (NULL, "unexpected response; %s", rp_string (retval));
1587 do_text (char *file, int fd)
1592 lseek (fd, (off_t) 0, SEEK_SET);
1594 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1595 if (rp_isbad (retval = sm_wtxt (buf, state)))
1596 die (NULL, "problem writing text; %s\n", rp_string (retval));
1600 die (file, "problem reading from");
1602 switch (retval = sm_wtend ()) {
1608 die (NULL, "posting failed; %s", rp_string (retval));
1611 die (NULL, "unexpected response; %s", rp_string (retval));
1615 #endif /* SMTPMTS */
1628 if (msgflags & MINV)
1632 if (!whomsw || checksw)
1634 #endif /* SMTPMTS */
1646 hstat = SIGNAL2 (SIGHUP, sigser);
1647 istat = SIGNAL2 (SIGINT, sigser);
1648 qstat = SIGNAL2 (SIGQUIT, sigser);
1649 tstat = SIGNAL2 (SIGTERM, sigser);
1659 SIGNAL (SIGHUP, hstat);
1660 SIGNAL (SIGINT, istat);
1661 SIGNAL (SIGQUIT, qstat);
1662 SIGNAL (SIGTERM, tstat);
1670 p_refile (char *file)
1678 printf (" -- Filing Folder Copies --\n");
1679 for (i = 0; i < fccind; i++)
1680 fcc (file, fccfold[i]);
1682 printf (" -- Folder Copies Filed --\n");
1687 * Call the `fileproc' to add the file to the folder.
1691 fcc (char *file, char *folder)
1698 printf (" %sFcc %s: ", msgstate == RESENT ? "Resent-" : "", folder);
1701 for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
1707 fprintf (stderr, " %sFcc %s: ",
1708 msgstate == RESENT ? "Resent-" : "", folder);
1709 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
1713 /* see if we need to add `+' */
1714 snprintf (fold, sizeof(fold), "%s%s",
1715 *folder == '+' || *folder == '@' ? "" : "+", folder);
1717 /* now exec the fileproc */
1718 execlp (fileproc, r1bindex (fileproc, '/'),
1719 "-link", "-file", file, fold, NULL);
1723 if ((status = pidwait (child_id, OK))) {
1725 fprintf (stderr, " %sFcc %s: ",
1726 msgstate == RESENT ? "Resent-" : "", folder);
1727 pidstatus (status, verbose ? stdout : stderr, NULL);
1730 printf ("folder ok\n");
1742 die (char *what, char *fmt, ...)
1747 if (msgflags & MINV)
1751 if (!whomsw || checksw)
1753 #endif /* SMTPMTS */
1756 advertise (what, NULL, fmt, ap);