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>
26 #ifdef TIME_WITH_SYS_TIME
27 # include <sys/time.h>
30 # ifdef TM_IN_SYS_TIME
31 # include <sys/time.h>
38 # include <mts/smtp/smtp.h>
42 # define SASLminc(a) (a)
43 #else /* CYRUS_SASL */
44 # define SASLminc(a) 0
45 #endif /* CYRUS_SASL */
48 # define TLSminc(a) (a)
49 #else /* TLS_SUPPORT */
51 #endif /* TLS_SUPPORT */
53 #define FCCS 10 /* max number of fccs allowed */
55 #define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : c)
57 /* In the following array of structures, the numeric second field of the
58 structures (minchars) is apparently used like this:
60 -# : Switch can be abbreviated to # characters; switch hidden in -help.
61 0 : Switch can't be abbreviated; switch shown in -help.
62 # : Switch can be abbreviated to # characters; switch shown in -help. */
64 static struct swit switches[] = {
66 { "alias aliasfile", 0 },
68 { "check", -5 }, /* interface from whom */
70 { "nocheck", -7 }, /* interface from whom */
74 { "dist", -4 }, /* interface from dist */
76 { "filter filterfile", 0 },
84 { "library directory", -7 }, /* interface from send, whom */
102 { "whom", -4 }, /* interface from whom */
104 { "width columns", 0 },
109 #define BITSTUFFSW 22
110 { "dashstuffing", -12 }, /* should we dashstuff BCC messages? */
111 #define NBITSTUFFSW 23
112 { "nodashstuffing", -14 },
114 { "mail", -4 }, /* specify MAIL smtp mode */
116 { "saml", -4 }, /* specify SAML smtp mode */
118 { "send", -4 }, /* specify SEND smtp mode */
120 { "soml", -4 }, /* specify SOML smtp mode */
122 { "idanno number", -6 }, /* interface from send */
124 { "deliver address-list", -7 },
126 { "client host", -6 },
128 { "server host", -6 }, /* specify alternate SMTP server */
130 { "snoop", -5 }, /* snoop the SMTP transaction */
132 { "fill-in file", -7 },
140 { "sasl", SASLminc(-4) },
141 #define SASLMECHSW 38
142 { "saslmech", SASLminc(-5) },
144 { "user", SASLminc(-4) },
146 { "port server port name/number", 4 },
148 { "tls", TLSminc(-3) },
160 * flags for headers->flags
162 #define HNOP 0x0000 /* just used to keep .set around */
163 #define HBAD 0x0001 /* bad header - don't let it through */
164 #define HADR 0x0002 /* header has an address field */
165 #define HSUB 0x0004 /* Subject: header */
166 #define HTRY 0x0008 /* try to send to addrs on header */
167 #define HBCC 0x0010 /* don't output this header */
168 #define HMNG 0x0020 /* munge this header */
169 #define HNGR 0x0040 /* no groups allowed in this header */
170 #define HFCC 0x0080 /* FCC: type header */
171 #define HNIL 0x0100 /* okay for this header not to have addrs */
172 #define HIGN 0x0200 /* ignore this header */
173 #define HDCC 0x0400 /* another undocumented feature */
176 * flags for headers->set
178 #define MFRM 0x0001 /* we've seen a From: */
179 #define MDAT 0x0002 /* we've seen a Date: */
180 #define MRFM 0x0004 /* we've seen a Resent-From: */
181 #define MVIS 0x0008 /* we've seen sighted addrs */
182 #define MINV 0x0010 /* we've seen blind addrs */
185 static struct headers NHeaders[] = {
186 { "Return-Path", HBAD, 0 },
187 { "Received", HBAD, 0 },
188 { "Reply-To", HADR|HNGR, 0 },
189 { "From", HADR|HNGR, MFRM },
190 { "Sender", HADR|HBAD, 0 },
192 { "Subject", HSUB, 0 },
193 { "To", HADR|HTRY, MVIS },
194 { "cc", HADR|HTRY, MVIS },
195 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
196 { "Dcc", HADR|HTRY|HDCC|HNIL, MVIS }, /* sorta cc & bcc combined */
197 { "Message-ID", HBAD, 0 },
202 static struct headers RHeaders[] = {
203 { "Resent-Reply-To", HADR|HNGR, 0 },
204 { "Resent-From", HADR|HNGR, MRFM },
205 { "Resent-Sender", HADR|HBAD, 0 },
206 { "Resent-Date", HBAD, 0 },
207 { "Resent-Subject", HSUB, 0 },
208 { "Resent-To", HADR|HTRY, MVIS },
209 { "Resent-cc", HADR|HTRY, MVIS },
210 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
211 { "Resent-Message-ID", HBAD, 0 },
212 { "Resent-Fcc", HFCC, 0 },
213 { "Reply-To", HADR, 0 },
214 { "From", HADR|HNGR, MFRM },
215 { "Sender", HADR|HNGR, 0 },
216 { "Date", HNOP, MDAT },
217 { "To", HADR|HNIL, 0 },
218 { "cc", HADR|HNIL, 0 },
219 { "Bcc", HADR|HTRY|HBCC|HNIL, 0 },
224 static short fccind = 0; /* index into fccfold[] */
225 static short outputlinelen = OUTPUTLINELEN;
227 static int pfd = NOTOK; /* fd to write annotation list to */
228 static uid_t myuid= -1; /* my user id */
229 static gid_t mygid= -1; /* my group id */
230 static int recipients = 0; /* how many people will get a copy */
231 static int unkadr = 0; /* how many of those were unknown */
232 static int badadr = 0; /* number of bad addrs */
233 static int badmsg = 0; /* message has bad semantics */
234 static int verbose = 0; /* spell it out */
235 static int format = 1; /* format addresses */
236 static int mime = 0; /* use MIME-style encapsulations for Bcc */
237 static int msgid = 0; /* add msgid */
238 static int debug = 0; /* debugging post */
239 static int watch = 0; /* watch the delivery process */
240 static int whomsw = 0; /* we are whom not post */
241 static int checksw = 0; /* whom -check */
242 static int linepos=0; /* putadr()'s position on the line */
243 static int nameoutput=0; /* putadr() has output header name */
244 static int sasl=0; /* Use SASL auth for SMTP */
245 static char *saslmech=NULL; /* Force use of particular SASL mech */
246 static char *user=NULL; /* Authenticate as this user */
247 static char *port="smtp"; /* Name of server port for SMTP */
248 static int tls=0; /* Use TLS for encryption */
250 static unsigned msgflags = 0; /* what we've seen */
254 static int msgstate = NORMAL;
256 static time_t tclock = 0; /* the time we started (more or less) */
258 static SIGNAL_HANDLER hstat, istat, qstat, tstat;
260 static char tmpfil[BUFSIZ];
261 static char bccfil[BUFSIZ];
263 static char from[BUFSIZ]; /* my network address */
264 static char signature[BUFSIZ]; /* my signature */
265 static char *filter = NULL; /* the filter for BCC'ing */
266 static char *subject = NULL; /* the subject field for BCC'ing */
267 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
269 static struct headers *hdrtab; /* table for the message we're doing */
271 static struct mailname localaddrs={NULL}; /* local addrs */
272 static struct mailname netaddrs={NULL}; /* network addrs */
273 static struct mailname uuaddrs={NULL}; /* uucp addrs */
274 static struct mailname tmpaddrs={NULL}; /* temporary queue */
277 static int snoop = 0;
278 static int smtpmode = S_MAIL;
279 static char *clientsw = NULL;
280 static char *serversw = NULL;
282 extern struct smtp sm_reply;
285 static char prefix[] = "----- =_aaaaaaaaaa";
287 static int fill_up = 0;
288 static char *fill_in = NULL;
289 static char *partno = NULL;
290 static int queued = 0;
292 extern boolean draft_from_masquerading; /* defined in mts.c */
297 static void putfmt (char *, char *, FILE *);
298 static void start_headers (void);
299 static void finish_headers (FILE *);
300 static int get_header (char *, struct headers *);
301 static int putadr (char *, char *, struct mailname *, FILE *, unsigned int);
302 static void putgrp (char *, char *, FILE *, unsigned int);
303 static int insert (struct mailname *);
304 static void pl (void);
305 static void anno (void);
306 static int annoaux (struct mailname *);
307 static void insert_fcc (struct headers *, unsigned char *);
308 static void make_bcc_file (int);
309 static void verify_all_addresses (int);
310 static void chkadr (void);
311 static void sigon (void);
312 static void sigoff (void);
313 static void p_refile (char *);
314 static void fcc (char *, char *);
315 static void die (char *, char *, ...);
316 static void post (char *, int, int);
317 static void do_text (char *file, int fd);
318 static void do_an_address (struct mailname *, int);
319 static void do_addresses (int, int);
320 static int find_prefix (void);
324 main (int argc, char **argv)
326 int state, compnum, dashstuff = 0;
327 char *cp, *msg = NULL, **argp, **arguments;
328 char buf[BUFSIZ], name[NAMESZ];
332 setlocale(LC_ALL, "");
334 invo_name = r1bindex (argv[0], '/');
336 /* foil search of user profile/context */
337 if (context_foil (NULL) == -1)
340 mts_init (invo_name);
341 arguments = getarguments (invo_name, argc, argv, 0);
344 while ((cp = *argp++)) {
346 switch (smatch (++cp, switches)) {
348 ambigsw (cp, switches);
351 adios (NULL, "-%s unknown", cp);
354 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
355 print_help (buf, switches, 0);
358 print_version(invo_name);
362 if (!(cp = *argp++) || *cp == '-')
363 adios (NULL, "missing argument to %s", argp[-2]);
364 /* create a minimal context */
365 if (context_foil (cp) == -1)
370 if (!(cp = *argp++) || *cp == '-')
371 adios (NULL, "missing argument to %s", argp[-2]);
372 if ((state = alias (cp)) != AK_OK)
373 adios (NULL, "aliasing error in %s - %s",
374 cp, akerror (state));
393 if (!(filter = *argp++) || *filter == '-')
394 adios (NULL, "missing argument to %s", argp[-2]);
449 if (!(cp = *argp++) || *cp == '-')
450 adios (NULL, "missing argument to %s", argp[-2]);
451 if ((outputlinelen = atoi (cp)) < 10)
452 adios (NULL, "impossible width %d", outputlinelen);
456 if (!(cp = *argp++) || *cp == '-')
457 adios (NULL, "missing argument to %s", argp[-2]);
458 if ((pfd = atoi (cp)) <= 2)
459 adios (NULL, "bad argument %s %s", argp[-2], cp);
463 if (!(cp = *argp++) || *cp == '-')
464 adios (NULL, "missing argument to %s", argp[-2]);
470 if (!(cp = *argp++) || *cp == '-')
471 adios (NULL, "missing argument to %s", argp[-2]);
490 if (!(clientsw = *argp++) || *clientsw == '-')
491 adios (NULL, "missing argument to %s", argp[-2]);
494 if (!(serversw = *argp++) || *serversw == '-')
495 adios (NULL, "missing argument to %s", argp[-2]);
503 if (!(fill_in = *argp++) || *fill_in == '-')
504 adios (NULL, "missing argument to %s", argp[-2]);
510 if (!(partno = *argp++) || *partno == '-')
511 adios (NULL, "missing argument to %s", argp[-2]);
523 if (!(saslmech = *argp++) || *saslmech == '-')
524 adios (NULL, "missing argument to %s", argp[-2]);
528 if (!(user = *argp++) || *user == '-')
529 adios (NULL, "missing argument to %s", argp[-2]);
533 if (!(port = *argp++) || *port == '-')
534 adios (NULL, "missing argument to %s", argp[-2]);
543 adios (NULL, "only one message at a time!");
551 adios (NULL, "usage: %s [switches] file", invo_name);
553 if (outputlinelen < 10)
554 adios (NULL, "impossible width %d", outputlinelen);
556 if ((in = fopen (msg, "r")) == NULL)
557 adios (msg, "unable to open");
562 discard (out = stdout); /* XXX: reference discard() to help loader */
565 if ((out = fopen (fill_in ? fill_in : "/dev/null", "w")) == NULL)
566 adios ("/dev/null", "unable to open");
568 char *cp = m_mktemp(m_maildir(invo_name), NULL, &out);
570 cp = m_mktemp2(NULL, invo_name, NULL, &out);
572 adios ("post", "unable to create temporary file");
575 strncpy(tmpfil, cp, sizeof(tmpfil));
576 chmod (tmpfil, 0600);
580 hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
582 for (compnum = 1, state = FLD;;) {
583 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
588 cp = add (buf, NULL);
589 while (state == FLDPLUS) {
590 state = m_getfld (state, name, buf, sizeof(buf), in);
593 putfmt (name, cp, out);
597 finish_headers (out);
602 finish_headers (out);
603 if (whomsw && !fill_in)
605 fprintf (out, "\n%s", buf);
606 while (state == BODY) {
607 state = m_getfld (state, name, buf, sizeof(buf), in);
613 finish_headers (out);
618 adios (NULL, "message format error in component #%d", compnum);
621 adios (NULL, "getfld() returned %d", state);
637 /* If we are doing a "whom" check */
640 verify_all_addresses (1);
644 if (msgflags & MINV) {
645 make_bcc_file (dashstuff);
646 if (msgflags & MVIS) {
647 verify_all_addresses (verbose);
648 post (tmpfil, 0, verbose);
650 post (bccfil, 1, verbose);
653 post (tmpfil, 0, isatty (1));
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) {
697 fprintf (out, "%s: %s", name, str);
700 if (hdr->flags & HBAD) {
702 fprintf (out, "%s: %s", name, str);
704 advise (NULL, "illegal header line -- %s:", name);
709 msgflags |= (hdr->set & ~(MVIS | MINV));
711 if (hdr->flags & HSUB)
712 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
713 if (hdr->flags & HFCC) {
715 fprintf (out, "%s: %s", name, str);
719 if ((cp = strrchr(str, '\n')))
721 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
723 insert_fcc (hdr, pp);
725 insert_fcc (hdr, pp);
729 if (!(hdr->flags & HADR)) {
730 fprintf (out, "%s: %s", name, str);
734 tmpaddrs.m_next = NULL;
735 for (count = 0; (cp = getname (str)); count++)
736 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
740 tmpaddrs.m_next = mp;
744 if (hdr->flags & HTRY)
750 if (hdr->flags & HNIL)
751 fprintf (out, "%s: %s", name, str);
754 advise (NULL, "%s: field requires at least one address", name);
761 nameoutput = linepos = 0;
762 snprintf (namep, sizeof(namep), "%s%s",
763 !fill_in && (hdr->flags & HMNG) ? "Original-" : "", name);
765 for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
766 if (mp->m_nohost) { /* also used to test (hdr->flags & HTRY) */
767 /* The address doesn't include a host, so it might be an alias. */
768 pp = akvalue (mp->m_mbox); /* do mh alias substitution */
769 qp = akvisible () ? mp->m_mbox : "";
772 putgrp (namep, np->m_gname, out, hdr->flags);
773 while ((cp = getname (pp))) {
774 if (!(mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
779 if (draft_from_masquerading && ((msgstate == RESENT)
781 : (hdr->set & MFRM)))
782 /* The user manually specified a [Resent-]From: address in
783 their draft and the "masquerade:" line in mts.conf
784 doesn't contain "draft_from", so we'll set things up to
785 use the actual email address embedded in the draft
786 [Resent-]From: (after alias substitution, and without the
787 GECOS full name or angle brackets) as the envelope
789 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
791 if (hdr->flags & HBCC)
794 mp->m_ingrp = np->m_ingrp;
797 putgrp (namep, mp->m_gname, out, hdr->flags);
800 if (putadr (namep, qp, mp, out, hdr->flags))
801 msgflags |= (hdr->set & (MVIS | MINV));
810 /* Address includes a host, so no alias substitution is needed. */
811 if (draft_from_masquerading && ((msgstate == RESENT)
813 : (hdr->set & MFRM)))
814 /* The user manually specified a [Resent-]From: address in
815 their draft and the "masquerade:" line in mts.conf
816 doesn't contain "draft_from", so we'll set things up to
817 use the actual email address embedded in the draft
818 [Resent-]From: (after alias substitution, and without the
819 GECOS full name or angle brackets) as the envelope
821 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
823 if (hdr->flags & HBCC)
826 putgrp (namep, mp->m_gname, out, hdr->flags);
829 keep = putadr (namep, "", mp, out, hdr->flags);
833 msgflags |= (hdr->set & (MVIS | MINV));
839 if (grp > 0 && (hdr->flags & HNGR)) {
840 advise (NULL, "%s: field does not allow groups", name);
844 if (fill_in && grp > 0)
855 char myhost[BUFSIZ], sigbuf[BUFSIZ];
862 strncpy (from, adrsprintf (NULL, NULL), sizeof(from));
863 strncpy (myhost, LocalName (), sizeof(myhost));
865 for (cp = myhost; *cp; cp++)
868 if ((cp = getfullname ()) && *cp) {
869 strncpy (sigbuf, cp, sizeof(sigbuf));
870 snprintf (signature, sizeof(signature), "%s <%s>",
871 sigbuf, adrsprintf (NULL, NULL));
872 if ((cp = getname (signature)) == NULL)
873 adios (NULL, "getname () failed -- you lose extraordinarily big");
874 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL)) == NULL)
875 adios (NULL, "bad signature '%s'", sigbuf);
880 strncpy (signature, adrsprintf (NULL, NULL), sizeof(signature));
886 * Now that we've outputted the header fields in the draft
887 * message, we will now output any remaining header fields
888 * that we need to add/create.
892 finish_headers (FILE *out)
896 if (whomsw && !fill_up)
899 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
901 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
902 (int) getpid (), (long) tclock, LocalName ());
903 if (msgflags & MFRM) {
904 /* There was already a From: in the draft. Don't add one. */
905 if (!draft_from_masquerading)
906 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
907 so we'll reveal the user's actual account@thismachine
908 address in a Sender: header (and use it as the envelope
910 fprintf (out, "Sender: %s\n", from);
913 /* Construct a From: header. */
914 fprintf (out, "From: %s\n", signature);
918 if (!(msgflags & MVIS))
919 fprintf (out, "Bcc: Blind Distribution List: ;\n");
923 if (!(msgflags & MDAT)) {
924 advise (NULL, "message has no Date: header");
927 if (!(msgflags & MFRM)) {
928 advise (NULL, "message has no From: header");
931 if (whomsw && !fill_up)
934 fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
936 fprintf (out, "Resent-Message-ID: <%d.%ld@%s>\n",
937 (int) getpid (), (long) tclock, LocalName ());
938 if (msgflags & MRFM) {
939 /* There was already a Resent-From: in draft. Don't add one. */
940 if (!draft_from_masquerading)
941 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
942 so we'll reveal the user's actual account@thismachine
943 address in a Sender: header (and use it as the envelope
945 fprintf (out, "Resent-Sender: %s\n", from);
948 /* Construct a Resent-From: header. */
949 fprintf (out, "Resent-From: %s\n", signature);
952 if (!(msgflags & MVIS))
953 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
958 adios (NULL, "re-format message and try again");
960 adios (NULL, "no addressees");
965 get_header (char *header, struct headers *table)
969 for (h = table; h->value; h++)
970 if (!mh_strcasecmp (header, h->value))
978 putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flags)
984 if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
986 if ((!fill_in && (flags & (HBCC | HDCC))) || mp->m_ingrp)
990 fprintf (out, "%s: ", name);
991 linepos += (nameoutput = strlen (name) + 2);
994 if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
995 mp->m_pers = getcpy (aka);
997 if (mp->m_gname && !fill_in) {
998 snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
1001 cp = adrformat (mp);
1008 if (linepos != nameoutput) {
1009 if (len + linepos + 2 > outputlinelen)
1010 fprintf (out, ",\n%*s", linepos = nameoutput, "");
1020 return (flags & HTRY);
1025 putgrp (char *name, char *group, FILE *out, unsigned int flags)
1030 if (!fill_in && (flags & HBCC))
1034 fprintf (out, "%s: ", name);
1035 linepos += (nameoutput = strlen (name) + 2);
1037 linepos -= strlen (group);
1040 cp = fill_in ? group : concat (group, ";", NULL);
1043 if (linepos > nameoutput) {
1044 if (len + linepos + 2 > outputlinelen) {
1045 fprintf (out, ",\n%*s", nameoutput, "");
1046 linepos = nameoutput;
1060 insert (struct mailname *np)
1062 struct mailname *mp;
1064 if (np->m_mbox == NULL)
1067 for (mp = np->m_type == LOCALHOST ? &localaddrs
1068 : np->m_type == UUCPHOST ? &uuaddrs
1072 if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
1073 && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox)
1074 && np->m_bcc == mp->m_next->m_bcc)
1087 struct mailname *mp;
1089 printf ("-------\n\t-- Addresses --\nlocal:\t");
1090 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1091 printf ("%s%s%s", mp->m_mbox,
1092 mp->m_bcc ? "[BCC]" : "",
1093 mp->m_next ? ",\n\t" : "");
1095 printf ("\nnet:\t");
1096 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1097 printf ("%s%s@%s%s%s", mp->m_path ? mp->m_path : "",
1098 mp->m_mbox, mp->m_host,
1099 mp->m_bcc ? "[BCC]" : "",
1100 mp->m_next ? ",\n\t" : "");
1102 printf ("\nuucp:\t");
1103 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1104 printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1105 mp->m_bcc ? "[BCC]" : "",
1106 mp->m_next ? ",\n\t" : "");
1108 printf ("\n\t-- Folder Copies --\nfcc:\t");
1109 for (i = 0; i < fccind; i++)
1110 printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1118 struct mailname *mp;
1120 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1121 if (annoaux (mp) == NOTOK)
1124 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1125 if (annoaux (mp) == NOTOK)
1128 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1129 if (annoaux (mp) == NOTOK)
1139 annoaux (struct mailname *mp)
1142 char buffer[BUFSIZ];
1144 snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1145 i = strlen (buffer);
1147 return (write (pfd, buffer, i) == i ? OK : NOTOK);
1152 insert_fcc (struct headers *hdr, unsigned char *pp)
1156 for (cp = pp; isspace (*cp); cp++)
1158 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
1166 adios (NULL, "too many %ss", hdr->value);
1167 fccfold[fccind++] = getcpy (cp);
1175 make_bcc_file (int dashstuff)
1183 tfile = m_mktemp2(NULL, "bccs", NULL, &out);
1184 if (tfile == NULL) adios("bcc", "unable to create temporary file");
1185 chmod (bccfil, 0600);
1186 strncpy (bccfil, tfile, sizeof(bccfil));
1188 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1190 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
1191 (int) getpid (), (long) tclock, LocalName ());
1192 if (msgflags & MFRM) {
1193 /* There was already a From: in the draft. Don't add one. */
1194 if (!draft_from_masquerading)
1195 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
1196 so we'll reveal the user's actual account@thismachine
1197 address in a Sender: header (and use it as the envelope
1199 fprintf (out, "Sender: %s\n", from);
1202 /* Construct a From: header. */
1203 fprintf (out, "From: %s\n", signature);
1205 fprintf (out, "Subject: %s", subject);
1206 fprintf (out, "BCC:\n");
1209 * Use MIME encapsulation for Bcc messages
1215 * Check if any lines in the message clash with the
1216 * prefix for the MIME multipart separator. If there
1217 * is a clash, increment one of the letters in the
1218 * prefix and check again.
1220 if ((cp = strchr(prefix, 'a')) == NULL)
1221 adios (NULL, "lost prefix start");
1222 while (find_prefix () == NOTOK) {
1227 adios (NULL, "can't find a unique delimiter string");
1232 fprintf (out, "%s: %s\n%s: multipart/digest; boundary=\"",
1233 VRSN_FIELD, VRSN_VALUE, TYPE_FIELD);
1234 fprintf (out, "%s\"\n\n--%s\n\n", prefix, prefix);
1236 fprintf (out, "\n------- Blind-Carbon-Copy\n\n");
1242 * Do mhl filtering of Bcc messages instead
1243 * of MIME encapsulation.
1245 if (filter != NULL) {
1246 vec[0] = r1bindex (mhlproc, '/');
1248 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1252 adios ("fork", "unable to");
1255 dup2 (fileno (out), 1);
1258 vec[i++] = "-forward";
1263 /* was the flag -[no]dashstuffing specified? */
1265 vec[i++] = "-dashstuffing";
1266 else if (dashstuff < 0)
1267 vec[i++] = "-nodashstuffing";
1270 execvp (mhlproc, vec);
1271 fprintf (stderr, "unable to exec ");
1276 pidXwait (child_id, mhlproc);
1280 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
1281 adios (tmpfil, "unable to re-open");
1284 * If using MIME encapsulation, or if the -nodashstuffing
1285 * flag was given, then just copy message. Else do
1286 * RFC934 quoting (dashstuffing).
1288 if (mime || dashstuff < 0)
1289 cpydata (fd, fileno (out), tmpfil, bccfil);
1291 cpydgst (fd, fileno (out), tmpfil, bccfil);
1295 fseek (out, 0L, SEEK_END);
1297 fprintf (out, "\n--%s--\n", prefix);
1299 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
1305 * Scan message to check if any lines clash with
1306 * the prefix of the MIME multipart separator.
1313 unsigned char buffer[BUFSIZ];
1316 if ((in = fopen (tmpfil, "r")) == NULL)
1317 adios (tmpfil, "unable to re-open");
1319 len = strlen (prefix);
1322 while (fgets (buffer, sizeof(buffer) - 1, in))
1323 if (buffer[0] == '-' && buffer[1] == '-') {
1326 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1330 if (strcmp (buffer + 2, prefix) == 0) {
1341 #define plural(x) (x == 1 ? "" : "s")
1346 if (badadr && unkadr)
1347 die (NULL, "%d address%s unparsable, %d addressee%s undeliverable",
1348 badadr, plural (badadr), unkadr, plural (badadr));
1350 die (NULL, "%d address%s unparsable", badadr, plural (badadr));
1352 die (NULL, "%d addressee%s undeliverable", unkadr, plural (unkadr));
1357 do_addresses (int bccque, int talk)
1361 struct mailname *lp;
1364 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1365 if (lp->m_bcc ? bccque : !bccque) {
1367 printf (" -- Local Recipients --\n");
1368 do_an_address (lp, talk);
1373 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1374 if (lp->m_bcc ? bccque : !bccque) {
1376 printf (" -- UUCP Recipients --\n");
1377 do_an_address (lp, talk);
1382 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1383 if (lp->m_bcc ? bccque : !bccque) {
1385 printf (" -- Network Recipients --\n");
1386 do_an_address (lp, talk);
1393 if (rp_isbad (retval = sm_waend ()))
1394 die (NULL, "problem ending addresses; %s", rp_string (retval));
1395 #endif /* SMTPMTS */
1400 * MTS-SPECIFIC INTERACTION
1405 * SENDMAIL/SMTP routines
1411 post (char *file, int bccque, int talk)
1416 onex = !(msgflags & MINV) || bccque;
1418 if (msgflags & MINV)
1419 printf (" -- Posting for %s Recipients --\n",
1420 bccque ? "Blind" : "Sighted");
1422 printf (" -- Posting for All Recipients --\n");
1427 if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch, verbose,
1428 snoop, onex, queued, sasl, saslmech,
1430 || rp_isbad (retval = sm_winit (smtpmode, from)))
1431 die (NULL, "problem initializing server; %s", rp_string (retval));
1433 do_addresses (bccque, talk && verbose);
1434 if ((fd = open (file, O_RDONLY)) == NOTOK)
1435 die (file, "unable to re-open");
1440 sm_end (onex ? OK : DONE);
1444 if (msgflags & MINV)
1445 printf (" -- %s Recipient Copies Posted --\n",
1446 bccque ? "Blind" : "Sighted");
1448 printf (" -- Recipient Copies Posted --\n");
1455 /* Address Verification */
1458 verify_all_addresses (int talk)
1461 struct mailname *lp;
1465 if (!whomsw || checksw)
1466 if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch,
1467 verbose, snoop, 0, queued, sasl,
1468 saslmech, user, tls))
1469 || rp_isbad (retval = sm_winit (smtpmode, from)))
1470 die (NULL, "problem initializing server; %s", rp_string (retval));
1472 if (talk && !whomsw)
1473 printf (" -- Address Verification --\n");
1474 if (talk && localaddrs.m_next)
1475 printf (" -- Local Recipients --\n");
1476 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1477 do_an_address (lp, talk);
1479 if (talk && uuaddrs.m_next)
1480 printf (" -- UUCP Recipients --\n");
1481 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1482 do_an_address (lp, talk);
1484 if (talk && netaddrs.m_next)
1485 printf (" -- Network Recipients --\n");
1486 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1487 do_an_address (lp, talk);
1490 if (talk && !whomsw)
1491 printf (" -- Address Verification Successful --\n");
1493 if (!whomsw || checksw)
1502 do_an_address (struct mailname *lp, int talk)
1508 switch (lp->m_type) {
1512 strncpy (addr, mbox, sizeof(addr));
1516 mbox = auxformat (lp, 0);
1518 snprintf (addr, sizeof(addr), "%s!%s", lp->m_host, lp->m_mbox);
1521 default: /* let SendMail decide if the host is bad */
1524 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1529 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1531 if (whomsw && !checksw) {
1539 switch (retval = sm_wadr (mbox, host,
1540 lp->m_type != UUCPHOST ? lp->m_path : NULL)) {
1543 printf ("address ok\n");
1549 fprintf (stderr, " %s: ", addr);
1550 fprintf (talk ? stdout : stderr, "loses; %s\n",
1551 rp_string (retval));
1557 fprintf (stderr, " %s: ", addr);
1558 die (NULL, "unexpected response; %s", rp_string (retval));
1566 do_text (char *file, int fd)
1571 lseek (fd, (off_t) 0, SEEK_SET);
1573 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1574 if (rp_isbad (retval = sm_wtxt (buf, state)))
1575 die (NULL, "problem writing text; %s\n", rp_string (retval));
1579 die (file, "problem reading from");
1581 switch (retval = sm_wtend ()) {
1587 die (NULL, "posting failed; %s", rp_string (retval));
1590 die (NULL, "unexpected response; %s", rp_string (retval));
1594 #endif /* SMTPMTS */
1604 #ifndef RELIABLE_SIGNALS
1605 SIGNAL (i, SIG_IGN);
1609 if (msgflags & MINV)
1613 if (!whomsw || checksw)
1615 #endif /* SMTPMTS */
1627 hstat = SIGNAL2 (SIGHUP, sigser);
1628 istat = SIGNAL2 (SIGINT, sigser);
1629 qstat = SIGNAL2 (SIGQUIT, sigser);
1630 tstat = SIGNAL2 (SIGTERM, sigser);
1640 SIGNAL (SIGHUP, hstat);
1641 SIGNAL (SIGINT, istat);
1642 SIGNAL (SIGQUIT, qstat);
1643 SIGNAL (SIGTERM, tstat);
1651 p_refile (char *file)
1659 printf (" -- Filing Folder Copies --\n");
1660 for (i = 0; i < fccind; i++)
1661 fcc (file, fccfold[i]);
1663 printf (" -- Folder Copies Filed --\n");
1668 * Call the `fileproc' to add the file to the folder.
1672 fcc (char *file, char *folder)
1679 printf (" %sFcc %s: ", msgstate == RESENT ? "Resent-" : "", folder);
1682 for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
1688 fprintf (stderr, " %sFcc %s: ",
1689 msgstate == RESENT ? "Resent-" : "", folder);
1690 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
1694 /* see if we need to add `+' */
1695 snprintf (fold, sizeof(fold), "%s%s",
1696 *folder == '+' || *folder == '@' ? "" : "+", folder);
1698 /* now exec the fileproc */
1699 execlp (fileproc, r1bindex (fileproc, '/'),
1700 "-link", "-file", file, fold, NULL);
1704 if ((status = pidwait (child_id, OK))) {
1706 fprintf (stderr, " %sFcc %s: ",
1707 msgstate == RESENT ? "Resent-" : "", folder);
1708 pidstatus (status, verbose ? stdout : stderr, NULL);
1711 printf ("folder ok\n");
1723 die (char *what, char *fmt, ...)
1728 if (msgflags & MINV)
1732 if (!whomsw || checksw)
1734 #endif /* SMTPMTS */
1737 advertise (what, NULL, fmt, ap);