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/smtp/smtp.h>
44 # define SASLminc(a) (a)
45 #else /* CYRUS_SASL */
46 # define SASLminc(a) 0
47 #endif /* CYRUS_SASL */
50 # define TLSminc(a) (a)
51 #else /* TLS_SUPPORT */
53 #endif /* TLS_SUPPORT */
55 #define FCCS 10 /* max number of fccs allowed */
57 #define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : c)
59 /* In the following array of structures, the numeric second field of the
60 structures (minchars) is apparently used like this:
62 -# : Switch can be abbreviated to # characters; switch hidden in -help.
63 0 : Switch can't be abbreviated; switch shown in -help.
64 # : Switch can be abbreviated to # characters; switch shown in -help. */
66 static struct swit switches[] = {
68 { "alias aliasfile", 0 },
70 { "check", -5 }, /* interface from whom */
72 { "nocheck", -7 }, /* interface from whom */
76 { "dist", -4 }, /* interface from dist */
78 { "filter filterfile", 0 },
86 { "library directory", -7 }, /* interface from send, whom */
104 { "whom", -4 }, /* interface from whom */
106 { "width columns", 0 },
111 #define BITSTUFFSW 22
112 { "dashstuffing", -12 }, /* should we dashstuff BCC messages? */
113 #define NBITSTUFFSW 23
114 { "nodashstuffing", -14 },
116 { "mail", -4 }, /* specify MAIL smtp mode */
118 { "saml", -4 }, /* specify SAML smtp mode */
120 { "send", -4 }, /* specify SEND smtp mode */
122 { "soml", -4 }, /* specify SOML smtp mode */
124 { "idanno number", -6 }, /* interface from send */
126 { "deliver address-list", -7 },
128 { "client host", -6 },
130 { "server host", -6 }, /* specify alternate SMTP server */
132 { "snoop", -5 }, /* snoop the SMTP transaction */
134 { "fill-in file", -7 },
142 { "sasl", SASLminc(-4) },
143 #define SASLMECHSW 38
144 { "saslmech", SASLminc(-5) },
146 { "user", SASLminc(-4) },
148 { "port server port name/number", 4 },
150 { "tls", TLSminc(-3) },
162 * flags for headers->flags
164 #define HNOP 0x0000 /* just used to keep .set around */
165 #define HBAD 0x0001 /* bad header - don't let it through */
166 #define HADR 0x0002 /* header has an address field */
167 #define HSUB 0x0004 /* Subject: header */
168 #define HTRY 0x0008 /* try to send to addrs on header */
169 #define HBCC 0x0010 /* don't output this header */
170 #define HMNG 0x0020 /* munge this header */
171 #define HNGR 0x0040 /* no groups allowed in this header */
172 #define HFCC 0x0080 /* FCC: type header */
173 #define HNIL 0x0100 /* okay for this header not to have addrs */
174 #define HIGN 0x0200 /* ignore this header */
175 #define HDCC 0x0400 /* another undocumented feature */
178 * flags for headers->set
180 #define MFRM 0x0001 /* we've seen a From: */
181 #define MDAT 0x0002 /* we've seen a Date: */
182 #define MRFM 0x0004 /* we've seen a Resent-From: */
183 #define MVIS 0x0008 /* we've seen sighted addrs */
184 #define MINV 0x0010 /* we've seen blind addrs */
187 static struct headers NHeaders[] = {
188 { "Return-Path", HBAD, 0 },
189 { "Received", HBAD, 0 },
190 { "Reply-To", HADR|HNGR, 0 },
191 { "From", HADR|HNGR, MFRM },
192 { "Sender", HADR|HBAD, 0 },
194 { "Subject", HSUB, 0 },
195 { "To", HADR|HTRY, MVIS },
196 { "cc", HADR|HTRY, MVIS },
197 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
198 { "Dcc", HADR|HTRY|HDCC|HNIL, MVIS }, /* sorta cc & bcc combined */
199 { "Message-ID", HBAD, 0 },
204 static struct headers RHeaders[] = {
205 { "Resent-Reply-To", HADR|HNGR, 0 },
206 { "Resent-From", HADR|HNGR, MRFM },
207 { "Resent-Sender", HADR|HBAD, 0 },
208 { "Resent-Date", HBAD, 0 },
209 { "Resent-Subject", HSUB, 0 },
210 { "Resent-To", HADR|HTRY, MVIS },
211 { "Resent-cc", HADR|HTRY, MVIS },
212 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
213 { "Resent-Message-ID", HBAD, 0 },
214 { "Resent-Fcc", HFCC, 0 },
215 { "Reply-To", HADR, 0 },
216 { "From", HADR|HNGR, MFRM },
217 { "Sender", HADR|HNGR, 0 },
218 { "Date", HNOP, MDAT },
219 { "To", HADR|HNIL, 0 },
220 { "cc", HADR|HNIL, 0 },
221 { "Bcc", HADR|HTRY|HBCC|HNIL, 0 },
226 static short fccind = 0; /* index into fccfold[] */
227 static short outputlinelen = OUTPUTLINELEN;
229 static int pfd = NOTOK; /* fd to write annotation list to */
230 static uid_t myuid= -1; /* my user id */
231 static gid_t mygid= -1; /* my group id */
232 static int recipients = 0; /* how many people will get a copy */
233 static int unkadr = 0; /* how many of those were unknown */
234 static int badadr = 0; /* number of bad addrs */
235 static int badmsg = 0; /* message has bad semantics */
236 static int verbose = 0; /* spell it out */
237 static int format = 1; /* format addresses */
238 static int mime = 0; /* use MIME-style encapsulations for Bcc */
239 static int msgid = 0; /* add msgid */
240 static int debug = 0; /* debugging post */
241 static int watch = 0; /* watch the delivery process */
242 static int whomsw = 0; /* we are whom not post */
243 static int checksw = 0; /* whom -check */
244 static int linepos=0; /* putadr()'s position on the line */
245 static int nameoutput=0; /* putadr() has output header name */
246 static int sasl=0; /* Use SASL auth for SMTP */
247 static char *saslmech=NULL; /* Force use of particular SASL mech */
248 static char *user=NULL; /* Authenticate as this user */
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={NULL}; /* local addrs */
274 static struct mailname netaddrs={NULL}; /* network addrs */
275 static struct mailname uuaddrs={NULL}; /* uucp addrs */
276 static struct mailname tmpaddrs={NULL}; /* 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]);
525 if (!(saslmech = *argp++) || *saslmech == '-')
526 adios (NULL, "missing argument to %s", argp[-2]);
530 if (!(user = *argp++) || *user == '-')
531 adios (NULL, "missing argument to %s", argp[-2]);
535 if (!(port = *argp++) || *port == '-')
536 adios (NULL, "missing argument to %s", argp[-2]);
545 adios (NULL, "only one message at a time!");
553 adios (NULL, "usage: %s [switches] file", invo_name);
555 if (outputlinelen < 10)
556 adios (NULL, "impossible width %d", outputlinelen);
558 if ((in = fopen (msg, "r")) == NULL)
559 adios (msg, "unable to open");
564 discard (out = stdout); /* XXX: reference discard() to help loader */
567 if ((out = fopen (fill_in ? fill_in : "/dev/null", "w")) == NULL)
568 adios ("/dev/null", "unable to open");
570 char *cp = m_mktemp(m_maildir(invo_name), NULL, &out);
572 cp = m_mktemp2(NULL, invo_name, NULL, &out);
574 adios ("post", "unable to create temporary file");
577 strncpy(tmpfil, cp, sizeof(tmpfil));
578 chmod (tmpfil, 0600);
582 hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
584 for (compnum = 1, state = FLD;;) {
585 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
590 cp = add (buf, NULL);
591 while (state == FLDPLUS) {
592 state = m_getfld (state, name, buf, sizeof(buf), in);
595 putfmt (name, cp, out);
599 finish_headers (out);
604 finish_headers (out);
605 if (whomsw && !fill_in)
607 fprintf (out, "\n%s", buf);
608 while (state == BODY) {
609 state = m_getfld (state, name, buf, sizeof(buf), in);
615 finish_headers (out);
620 adios (NULL, "message format error in component #%d", compnum);
623 adios (NULL, "getfld() returned %d", state);
639 /* If we are doing a "whom" check */
642 verify_all_addresses (1);
646 if (msgflags & MINV) {
647 make_bcc_file (dashstuff);
648 if (msgflags & MVIS) {
649 verify_all_addresses (verbose);
650 post (tmpfil, 0, verbose);
652 post (bccfil, 1, verbose);
655 post (tmpfil, 0, isatty (1));
662 printf (partno ? "Partial Message #%s Processed\n" : "Message Processed\n",
674 putfmt (char *name, char *str, FILE *out)
676 int count, grp, i, keep;
679 struct mailname *mp = NULL, *np = NULL;
682 while (*str == ' ' || *str == '\t')
685 if (msgstate == NORMAL && uprf (name, "resent")) {
686 advise (NULL, "illegal header line -- %s:", name);
691 if ((i = get_header (name, hdrtab)) == NOTOK) {
692 fprintf (out, "%s: %s", name, str);
697 if (hdr->flags & HIGN) {
699 fprintf (out, "%s: %s", name, str);
702 if (hdr->flags & HBAD) {
704 fprintf (out, "%s: %s", name, str);
706 advise (NULL, "illegal header line -- %s:", name);
711 msgflags |= (hdr->set & ~(MVIS | MINV));
713 if (hdr->flags & HSUB)
714 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
715 if (hdr->flags & HFCC) {
717 fprintf (out, "%s: %s", name, str);
721 if ((cp = strrchr(str, '\n')))
723 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
725 insert_fcc (hdr, pp);
727 insert_fcc (hdr, pp);
731 if (!(hdr->flags & HADR)) {
732 fprintf (out, "%s: %s", name, str);
736 tmpaddrs.m_next = NULL;
737 for (count = 0; (cp = getname (str)); count++)
738 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
742 tmpaddrs.m_next = mp;
746 if (hdr->flags & HTRY)
752 if (hdr->flags & HNIL)
753 fprintf (out, "%s: %s", name, str);
756 advise (NULL, "%s: field requires at least one address", name);
763 nameoutput = linepos = 0;
764 snprintf (namep, sizeof(namep), "%s%s",
765 !fill_in && (hdr->flags & HMNG) ? "Original-" : "", name);
767 for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
768 if (mp->m_nohost) { /* also used to test (hdr->flags & HTRY) */
769 /* The address doesn't include a host, so it might be an alias. */
770 pp = akvalue (mp->m_mbox); /* do mh alias substitution */
771 qp = akvisible () ? mp->m_mbox : "";
774 putgrp (namep, np->m_gname, out, hdr->flags);
775 while ((cp = getname (pp))) {
776 if (!(mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
781 if (draft_from_masquerading && ((msgstate == RESENT)
783 : (hdr->set & MFRM)))
784 /* The user manually specified a [Resent-]From: address in
785 their draft and the "masquerade:" line in mts.conf
786 doesn't contain "draft_from", so we'll set things up to
787 use the actual email address embedded in the draft
788 [Resent-]From: (after alias substitution, and without the
789 GECOS full name or angle brackets) as the envelope
791 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
793 if (hdr->flags & HBCC)
796 mp->m_ingrp = np->m_ingrp;
799 putgrp (namep, mp->m_gname, out, hdr->flags);
802 if (putadr (namep, qp, mp, out, hdr->flags))
803 msgflags |= (hdr->set & (MVIS | MINV));
812 /* Address includes a host, so no alias substitution is needed. */
813 if (draft_from_masquerading && ((msgstate == RESENT)
815 : (hdr->set & MFRM)))
816 /* The user manually specified a [Resent-]From: address in
817 their draft and the "masquerade:" line in mts.conf
818 doesn't contain "draft_from", so we'll set things up to
819 use the actual email address embedded in the draft
820 [Resent-]From: (after alias substitution, and without the
821 GECOS full name or angle brackets) as the envelope
823 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
825 if (hdr->flags & HBCC)
828 putgrp (namep, mp->m_gname, out, hdr->flags);
831 keep = putadr (namep, "", mp, out, hdr->flags);
835 msgflags |= (hdr->set & (MVIS | MINV));
841 if (grp > 0 && (hdr->flags & HNGR)) {
842 advise (NULL, "%s: field does not allow groups", name);
846 if (fill_in && grp > 0)
857 char myhost[BUFSIZ], sigbuf[BUFSIZ];
864 strncpy (from, adrsprintf (NULL, NULL), sizeof(from));
865 strncpy (myhost, LocalName (), sizeof(myhost));
867 for (cp = myhost; *cp; cp++)
870 if ((cp = getfullname ()) && *cp) {
871 strncpy (sigbuf, cp, sizeof(sigbuf));
872 snprintf (signature, sizeof(signature), "%s <%s>",
873 sigbuf, adrsprintf (NULL, NULL));
874 if ((cp = getname (signature)) == NULL)
875 adios (NULL, "getname () failed -- you lose extraordinarily big");
876 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL)) == NULL)
877 adios (NULL, "bad signature '%s'", sigbuf);
882 strncpy (signature, adrsprintf (NULL, NULL), sizeof(signature));
888 * Now that we've outputted the header fields in the draft
889 * message, we will now output any remaining header fields
890 * that we need to add/create.
894 finish_headers (FILE *out)
898 if (whomsw && !fill_up)
901 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
903 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
904 (int) getpid (), (long) tclock, LocalName ());
905 if (msgflags & MFRM) {
906 /* There was already a From: in the draft. Don't add one. */
907 if (!draft_from_masquerading)
908 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
909 so we'll reveal the user's actual account@thismachine
910 address in a Sender: header (and use it as the envelope
912 fprintf (out, "Sender: %s\n", from);
915 /* Construct a From: header. */
916 fprintf (out, "From: %s\n", signature);
920 if (!(msgflags & MVIS))
921 fprintf (out, "Bcc: Blind Distribution List: ;\n");
925 if (!(msgflags & MDAT)) {
926 advise (NULL, "message has no Date: header");
929 if (!(msgflags & MFRM)) {
930 advise (NULL, "message has no From: header");
933 if (whomsw && !fill_up)
936 fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
938 fprintf (out, "Resent-Message-ID: <%d.%ld@%s>\n",
939 (int) getpid (), (long) tclock, LocalName ());
940 if (msgflags & MRFM) {
941 /* There was already a Resent-From: in draft. Don't add one. */
942 if (!draft_from_masquerading)
943 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
944 so we'll reveal the user's actual account@thismachine
945 address in a Sender: header (and use it as the envelope
947 fprintf (out, "Resent-Sender: %s\n", from);
950 /* Construct a Resent-From: header. */
951 fprintf (out, "Resent-From: %s\n", signature);
954 if (!(msgflags & MVIS))
955 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
960 adios (NULL, "re-format message and try again");
962 adios (NULL, "no addressees");
967 get_header (char *header, struct headers *table)
971 for (h = table; h->value; h++)
972 if (!mh_strcasecmp (header, h->value))
980 putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flags)
986 if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
988 if ((!fill_in && (flags & (HBCC | HDCC))) || mp->m_ingrp)
992 fprintf (out, "%s: ", name);
993 linepos += (nameoutput = strlen (name) + 2);
996 if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
997 mp->m_pers = getcpy (aka);
999 if (mp->m_gname && !fill_in) {
1000 snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
1003 cp = adrformat (mp);
1010 if (linepos != nameoutput) {
1011 if (len + linepos + 2 > outputlinelen)
1012 fprintf (out, ",\n%*s", linepos = nameoutput, "");
1022 return (flags & HTRY);
1027 putgrp (char *name, char *group, FILE *out, unsigned int flags)
1032 if (!fill_in && (flags & HBCC))
1036 fprintf (out, "%s: ", name);
1037 linepos += (nameoutput = strlen (name) + 2);
1039 linepos -= strlen (group);
1042 cp = fill_in ? group : concat (group, ";", NULL);
1045 if (linepos > nameoutput) {
1046 if (len + linepos + 2 > outputlinelen) {
1047 fprintf (out, ",\n%*s", nameoutput, "");
1048 linepos = nameoutput;
1062 insert (struct mailname *np)
1064 struct mailname *mp;
1066 if (np->m_mbox == NULL)
1069 for (mp = np->m_type == LOCALHOST ? &localaddrs
1070 : np->m_type == UUCPHOST ? &uuaddrs
1074 if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
1075 && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox)
1076 && np->m_bcc == mp->m_next->m_bcc)
1089 struct mailname *mp;
1091 printf ("-------\n\t-- Addresses --\nlocal:\t");
1092 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1093 printf ("%s%s%s", mp->m_mbox,
1094 mp->m_bcc ? "[BCC]" : "",
1095 mp->m_next ? ",\n\t" : "");
1097 printf ("\nnet:\t");
1098 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1099 printf ("%s%s@%s%s%s", mp->m_path ? mp->m_path : "",
1100 mp->m_mbox, mp->m_host,
1101 mp->m_bcc ? "[BCC]" : "",
1102 mp->m_next ? ",\n\t" : "");
1104 printf ("\nuucp:\t");
1105 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1106 printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1107 mp->m_bcc ? "[BCC]" : "",
1108 mp->m_next ? ",\n\t" : "");
1110 printf ("\n\t-- Folder Copies --\nfcc:\t");
1111 for (i = 0; i < fccind; i++)
1112 printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1120 struct mailname *mp;
1122 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1123 if (annoaux (mp) == NOTOK)
1126 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1127 if (annoaux (mp) == NOTOK)
1130 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1131 if (annoaux (mp) == NOTOK)
1141 annoaux (struct mailname *mp)
1144 char buffer[BUFSIZ];
1146 snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1147 i = strlen (buffer);
1149 return (write (pfd, buffer, i) == i ? OK : NOTOK);
1154 insert_fcc (struct headers *hdr, unsigned char *pp)
1158 for (cp = pp; isspace (*cp); cp++)
1160 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
1168 adios (NULL, "too many %ss", hdr->value);
1169 fccfold[fccind++] = getcpy (cp);
1177 make_bcc_file (int dashstuff)
1185 tfile = m_mktemp2(NULL, "bccs", NULL, &out);
1186 if (tfile == NULL) adios("bcc", "unable to create temporary file");
1187 chmod (bccfil, 0600);
1188 strncpy (bccfil, tfile, sizeof(bccfil));
1190 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1192 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
1193 (int) getpid (), (long) tclock, LocalName ());
1194 if (msgflags & MFRM) {
1195 /* There was already a From: in the draft. Don't add one. */
1196 if (!draft_from_masquerading)
1197 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
1198 so we'll reveal the user's actual account@thismachine
1199 address in a Sender: header (and use it as the envelope
1201 fprintf (out, "Sender: %s\n", from);
1204 /* Construct a From: header. */
1205 fprintf (out, "From: %s\n", signature);
1207 fprintf (out, "Subject: %s", subject);
1208 fprintf (out, "BCC:\n");
1211 * Use MIME encapsulation for Bcc messages
1217 * Check if any lines in the message clash with the
1218 * prefix for the MIME multipart separator. If there
1219 * is a clash, increment one of the letters in the
1220 * prefix and check again.
1222 if ((cp = strchr(prefix, 'a')) == NULL)
1223 adios (NULL, "lost prefix start");
1224 while (find_prefix () == NOTOK) {
1229 adios (NULL, "can't find a unique delimiter string");
1234 fprintf (out, "%s: %s\n%s: multipart/digest; boundary=\"",
1235 VRSN_FIELD, VRSN_VALUE, TYPE_FIELD);
1236 fprintf (out, "%s\"\n\n--%s\n\n", prefix, prefix);
1238 fprintf (out, "\n------- Blind-Carbon-Copy\n\n");
1244 * Do mhl filtering of Bcc messages instead
1245 * of MIME encapsulation.
1247 if (filter != NULL) {
1248 vec[0] = r1bindex (mhlproc, '/');
1250 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1254 adios ("fork", "unable to");
1257 dup2 (fileno (out), 1);
1260 vec[i++] = "-forward";
1265 /* was the flag -[no]dashstuffing specified? */
1267 vec[i++] = "-dashstuffing";
1268 else if (dashstuff < 0)
1269 vec[i++] = "-nodashstuffing";
1272 execvp (mhlproc, vec);
1273 fprintf (stderr, "unable to exec ");
1278 pidXwait (child_id, mhlproc);
1282 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
1283 adios (tmpfil, "unable to re-open");
1286 * If using MIME encapsulation, or if the -nodashstuffing
1287 * flag was given, then just copy message. Else do
1288 * RFC934 quoting (dashstuffing).
1290 if (mime || dashstuff < 0)
1291 cpydata (fd, fileno (out), tmpfil, bccfil);
1293 cpydgst (fd, fileno (out), tmpfil, bccfil);
1297 fseek (out, 0L, SEEK_END);
1299 fprintf (out, "\n--%s--\n", prefix);
1301 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
1307 * Scan message to check if any lines clash with
1308 * the prefix of the MIME multipart separator.
1315 unsigned char buffer[BUFSIZ];
1318 if ((in = fopen (tmpfil, "r")) == NULL)
1319 adios (tmpfil, "unable to re-open");
1321 len = strlen (prefix);
1324 while (fgets (buffer, sizeof(buffer) - 1, in))
1325 if (buffer[0] == '-' && buffer[1] == '-') {
1328 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1332 if (strcmp (buffer + 2, prefix) == 0) {
1343 #define plural(x) (x == 1 ? "" : "s")
1348 if (badadr && unkadr)
1349 die (NULL, "%d address%s unparsable, %d addressee%s undeliverable",
1350 badadr, plural (badadr), unkadr, plural (badadr));
1352 die (NULL, "%d address%s unparsable", badadr, plural (badadr));
1354 die (NULL, "%d addressee%s undeliverable", unkadr, plural (unkadr));
1359 do_addresses (int bccque, int talk)
1363 struct mailname *lp;
1366 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1367 if (lp->m_bcc ? bccque : !bccque) {
1369 printf (" -- Local Recipients --\n");
1370 do_an_address (lp, talk);
1375 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1376 if (lp->m_bcc ? bccque : !bccque) {
1378 printf (" -- UUCP Recipients --\n");
1379 do_an_address (lp, talk);
1384 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1385 if (lp->m_bcc ? bccque : !bccque) {
1387 printf (" -- Network Recipients --\n");
1388 do_an_address (lp, talk);
1395 if (rp_isbad (retval = sm_waend ()))
1396 die (NULL, "problem ending addresses; %s", rp_string (retval));
1397 #endif /* SMTPMTS */
1402 * MTS-SPECIFIC INTERACTION
1407 * SENDMAIL/SMTP routines
1413 post (char *file, int bccque, int talk)
1418 onex = !(msgflags & MINV) || bccque;
1420 if (msgflags & MINV)
1421 printf (" -- Posting for %s Recipients --\n",
1422 bccque ? "Blind" : "Sighted");
1424 printf (" -- Posting for All Recipients --\n");
1429 if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch, verbose,
1430 snoop, onex, queued, sasl, saslmech,
1432 || rp_isbad (retval = sm_winit (smtpmode, from)))
1433 die (NULL, "problem initializing server; %s", rp_string (retval));
1435 do_addresses (bccque, talk && verbose);
1436 if ((fd = open (file, O_RDONLY)) == NOTOK)
1437 die (file, "unable to re-open");
1442 sm_end (onex ? OK : DONE);
1446 if (msgflags & MINV)
1447 printf (" -- %s Recipient Copies Posted --\n",
1448 bccque ? "Blind" : "Sighted");
1450 printf (" -- Recipient Copies Posted --\n");
1457 /* Address Verification */
1460 verify_all_addresses (int talk)
1463 struct mailname *lp;
1467 if (!whomsw || checksw)
1468 if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch,
1469 verbose, snoop, 0, queued, sasl,
1470 saslmech, user, tls))
1471 || rp_isbad (retval = sm_winit (smtpmode, from)))
1472 die (NULL, "problem initializing server; %s", rp_string (retval));
1474 if (talk && !whomsw)
1475 printf (" -- Address Verification --\n");
1476 if (talk && localaddrs.m_next)
1477 printf (" -- Local Recipients --\n");
1478 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1479 do_an_address (lp, talk);
1481 if (talk && uuaddrs.m_next)
1482 printf (" -- UUCP Recipients --\n");
1483 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1484 do_an_address (lp, talk);
1486 if (talk && netaddrs.m_next)
1487 printf (" -- Network Recipients --\n");
1488 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1489 do_an_address (lp, talk);
1492 if (talk && !whomsw)
1493 printf (" -- Address Verification Successful --\n");
1495 if (!whomsw || checksw)
1504 do_an_address (struct mailname *lp, int talk)
1510 switch (lp->m_type) {
1514 strncpy (addr, mbox, sizeof(addr));
1518 mbox = auxformat (lp, 0);
1520 snprintf (addr, sizeof(addr), "%s!%s", lp->m_host, lp->m_mbox);
1523 default: /* let SendMail decide if the host is bad */
1526 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1531 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1533 if (whomsw && !checksw) {
1541 switch (retval = sm_wadr (mbox, host,
1542 lp->m_type != UUCPHOST ? lp->m_path : NULL)) {
1545 printf ("address ok\n");
1551 fprintf (stderr, " %s: ", addr);
1552 fprintf (talk ? stdout : stderr, "loses; %s\n",
1553 rp_string (retval));
1559 fprintf (stderr, " %s: ", addr);
1560 die (NULL, "unexpected response; %s", rp_string (retval));
1568 do_text (char *file, int fd)
1573 lseek (fd, (off_t) 0, SEEK_SET);
1575 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1576 if (rp_isbad (retval = sm_wtxt (buf, state)))
1577 die (NULL, "problem writing text; %s\n", rp_string (retval));
1581 die (file, "problem reading from");
1583 switch (retval = sm_wtend ()) {
1589 die (NULL, "posting failed; %s", rp_string (retval));
1592 die (NULL, "unexpected response; %s", rp_string (retval));
1596 #endif /* SMTPMTS */
1606 #ifndef RELIABLE_SIGNALS
1607 SIGNAL (i, SIG_IGN);
1611 if (msgflags & MINV)
1615 if (!whomsw || checksw)
1617 #endif /* SMTPMTS */
1629 hstat = SIGNAL2 (SIGHUP, sigser);
1630 istat = SIGNAL2 (SIGINT, sigser);
1631 qstat = SIGNAL2 (SIGQUIT, sigser);
1632 tstat = SIGNAL2 (SIGTERM, sigser);
1642 SIGNAL (SIGHUP, hstat);
1643 SIGNAL (SIGINT, istat);
1644 SIGNAL (SIGQUIT, qstat);
1645 SIGNAL (SIGTERM, tstat);
1653 p_refile (char *file)
1661 printf (" -- Filing Folder Copies --\n");
1662 for (i = 0; i < fccind; i++)
1663 fcc (file, fccfold[i]);
1665 printf (" -- Folder Copies Filed --\n");
1670 * Call the `fileproc' to add the file to the folder.
1674 fcc (char *file, char *folder)
1681 printf (" %sFcc %s: ", msgstate == RESENT ? "Resent-" : "", folder);
1684 for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
1690 fprintf (stderr, " %sFcc %s: ",
1691 msgstate == RESENT ? "Resent-" : "", folder);
1692 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
1696 /* see if we need to add `+' */
1697 snprintf (fold, sizeof(fold), "%s%s",
1698 *folder == '+' || *folder == '@' ? "" : "+", folder);
1700 /* now exec the fileproc */
1701 execlp (fileproc, r1bindex (fileproc, '/'),
1702 "-link", "-file", file, fold, NULL);
1706 if ((status = pidwait (child_id, OK))) {
1708 fprintf (stderr, " %sFcc %s: ",
1709 msgstate == RESENT ? "Resent-" : "", folder);
1710 pidstatus (status, verbose ? stdout : stderr, NULL);
1713 printf ("folder ok\n");
1725 die (char *what, char *fmt, ...)
1730 if (msgflags & MINV)
1734 if (!whomsw || checksw)
1736 #endif /* SMTPMTS */
1739 advertise (what, NULL, fmt, ap);