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 { "mail", -4 }, /* specify MAIL smtp mode */
107 { "saml", -4 }, /* specify SAML smtp mode */
109 { "send", -4 }, /* specify SEND smtp mode */
111 { "soml", -4 }, /* specify SOML smtp mode */
113 { "idanno number", -6 }, /* interface from send */
115 { "deliver address-list", -7 },
117 { "client host", -6 },
119 { "server host", -6 }, /* specify alternate SMTP server */
121 { "snoop", -5 }, /* snoop the SMTP transaction */
123 { "fill-in file", -7 },
131 { "sasl", SASLminc(-4) },
133 { "nosasl", SASLminc(-6) },
134 #define SASLMXSSFSW 39
135 { "saslmaxssf", SASLminc(-10) },
136 #define SASLMECHSW 40
137 { "saslmech", SASLminc(-5) },
139 { "user", SASLminc(-4) },
141 { "port server port name/number", 4 },
143 { "tls", TLSminc(-3) },
144 #define FILEPROCSW 44
159 * flags for headers->flags
161 #define HNOP 0x0000 /* just used to keep .set around */
162 #define HBAD 0x0001 /* bad header - don't let it through */
163 #define HADR 0x0002 /* header has an address field */
164 #define HSUB 0x0004 /* Subject: header */
165 #define HTRY 0x0008 /* try to send to addrs on header */
166 #define HBCC 0x0010 /* don't output this header */
167 #define HMNG 0x0020 /* munge this header */
168 #define HNGR 0x0040 /* no groups allowed in this header */
169 #define HFCC 0x0080 /* FCC: type header */
170 #define HNIL 0x0100 /* okay for this header not to have addrs */
171 #define HIGN 0x0200 /* ignore this header */
172 #define HDCC 0x0400 /* another undocumented feature */
175 * flags for headers->set
177 #define MFRM 0x0001 /* we've seen a From: */
178 #define MDAT 0x0002 /* we've seen a Date: */
179 #define MRFM 0x0004 /* we've seen a Resent-From: */
180 #define MVIS 0x0008 /* we've seen sighted addrs */
181 #define MINV 0x0010 /* we've seen blind addrs */
184 static struct headers NHeaders[] = {
185 { "Return-Path", HBAD, 0 },
186 { "Received", HBAD, 0 },
187 { "Reply-To", HADR|HNGR, 0 },
188 { "From", HADR|HNGR, MFRM },
189 { "Sender", HADR|HBAD, 0 },
191 { "Subject", HSUB, 0 },
192 { "To", HADR|HTRY, MVIS },
193 { "cc", HADR|HTRY, MVIS },
194 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
195 { "Dcc", HADR|HTRY|HDCC|HNIL, MVIS }, /* sorta cc & bcc combined */
196 { "Message-ID", HBAD, 0 },
201 static struct headers RHeaders[] = {
202 { "Resent-Reply-To", HADR|HNGR, 0 },
203 { "Resent-From", HADR|HNGR, MRFM },
204 { "Resent-Sender", HADR|HBAD, 0 },
205 { "Resent-Date", HBAD, 0 },
206 { "Resent-Subject", HSUB, 0 },
207 { "Resent-To", HADR|HTRY, MVIS },
208 { "Resent-cc", HADR|HTRY, MVIS },
209 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
210 { "Resent-Message-ID", HBAD, 0 },
211 { "Resent-Fcc", HFCC, 0 },
212 { "Reply-To", HADR, 0 },
213 { "From", HADR|HNGR, MFRM },
214 { "Sender", HADR|HNGR, 0 },
215 { "Date", HNOP, MDAT },
216 { "To", HADR|HNIL, 0 },
217 { "cc", HADR|HNIL, 0 },
218 { "Bcc", HADR|HTRY|HBCC|HNIL, 0 },
223 static short fccind = 0; /* index into fccfold[] */
224 static short outputlinelen = OUTPUTLINELEN;
226 static int pfd = NOTOK; /* fd to write annotation list to */
227 static uid_t myuid= -1; /* my user id */
228 static gid_t mygid= -1; /* my group id */
229 static int recipients = 0; /* how many people will get a copy */
230 static int unkadr = 0; /* how many of those were unknown */
231 static int badadr = 0; /* number of bad addrs */
232 static int badmsg = 0; /* message has bad semantics */
233 static int verbose = 0; /* spell it out */
234 static int format = 1; /* format addresses */
235 static int mime = 0; /* use MIME-style encapsulations for Bcc */
236 static int msgid = 0; /* add msgid */
237 static int debug = 0; /* debugging post */
238 static int watch = 0; /* watch the delivery process */
239 static int whomsw = 0; /* we are whom not post */
240 static int checksw = 0; /* whom -check */
241 static int linepos=0; /* putadr()'s position on the line */
242 static int nameoutput=0; /* putadr() has output header name */
243 static int sasl=0; /* Use SASL auth for SMTP */
244 static int saslssf=-1; /* Our maximum SSF for SASL */
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; /* local addrs */
272 static struct mailname netaddrs; /* network addrs */
273 static struct mailname uuaddrs; /* uucp addrs */
274 static struct mailname tmpaddrs; /* temporary queue */
276 static int snoop = 0;
277 static int smtpmode = S_MAIL;
278 static char *clientsw = NULL;
279 static char *serversw = NULL;
281 extern struct smtp sm_reply;
283 static char prefix[] = "----- =_aaaaaaaaaa";
285 static int fill_up = 0;
286 static char *fill_in = NULL;
287 static char *partno = NULL;
288 static int queued = 0;
290 extern boolean draft_from_masquerading; /* defined in mts.c */
295 static void putfmt (char *, char *, FILE *);
296 static void start_headers (void);
297 static void finish_headers (FILE *);
298 static int get_header (char *, struct headers *);
299 static int putadr (char *, char *, struct mailname *, FILE *, unsigned int);
300 static void putgrp (char *, char *, FILE *, unsigned int);
301 static int insert (struct mailname *);
302 static void pl (void);
303 static void anno (void);
304 static int annoaux (struct mailname *);
305 static void insert_fcc (struct headers *, unsigned char *);
306 static void make_bcc_file (int);
307 static void verify_all_addresses (int);
308 static void chkadr (void);
309 static void sigon (void);
310 static void sigoff (void);
311 static void p_refile (char *);
312 static void fcc (char *, char *);
313 static void die (char *, char *, ...);
314 static void post (char *, int, int);
315 static void do_text (char *file, int fd);
316 static void do_an_address (struct mailname *, int);
317 static void do_addresses (int, int);
318 static int find_prefix (void);
322 main (int argc, char **argv)
324 int state, compnum, dashstuff = 0;
325 char *cp, *msg = NULL, **argp, **arguments;
326 char buf[BUFSIZ], name[NAMESZ];
330 setlocale(LC_ALL, "");
332 invo_name = r1bindex (argv[0], '/');
334 /* foil search of user profile/context */
335 if (context_foil (NULL) == -1)
338 mts_init (invo_name);
339 arguments = getarguments (invo_name, argc, argv, 0);
342 while ((cp = *argp++)) {
344 switch (smatch (++cp, switches)) {
346 ambigsw (cp, switches);
349 adios (NULL, "-%s unknown", cp);
352 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
353 print_help (buf, switches, 0);
356 print_version(invo_name);
360 if (!(cp = *argp++) || *cp == '-')
361 adios (NULL, "missing argument to %s", argp[-2]);
362 /* create a minimal context */
363 if (context_foil (cp) == -1)
368 if (!(cp = *argp++) || *cp == '-')
369 adios (NULL, "missing argument to %s", argp[-2]);
370 if ((state = alias (cp)) != AK_OK)
371 adios (NULL, "aliasing error in %s - %s",
372 cp, akerror (state));
391 if (!(filter = *argp++) || *filter == '-')
392 adios (NULL, "missing argument to %s", argp[-2]);
447 if (!(cp = *argp++) || *cp == '-')
448 adios (NULL, "missing argument to %s", argp[-2]);
449 if ((outputlinelen = atoi (cp)) < 10)
450 adios (NULL, "impossible width %d", outputlinelen);
454 if (!(cp = *argp++) || *cp == '-')
455 adios (NULL, "missing argument to %s", argp[-2]);
456 if ((pfd = atoi (cp)) <= 2)
457 adios (NULL, "bad argument %s %s", argp[-2], cp);
461 if (!(cp = *argp++) || *cp == '-')
462 adios (NULL, "missing argument to %s", argp[-2]);
478 if (!(clientsw = *argp++) || *clientsw == '-')
479 adios (NULL, "missing argument to %s", argp[-2]);
482 if (!(serversw = *argp++) || *serversw == '-')
483 adios (NULL, "missing argument to %s", argp[-2]);
490 if (!(fill_in = *argp++) || *fill_in == '-')
491 adios (NULL, "missing argument to %s", argp[-2]);
497 if (!(partno = *argp++) || *partno == '-')
498 adios (NULL, "missing argument to %s", argp[-2]);
514 if (!(cp = *argp++) || *cp == '-')
515 adios (NULL, "missing argument to %s", argp[-2]);
520 if (!(saslmech = *argp++) || *saslmech == '-')
521 adios (NULL, "missing argument to %s", argp[-2]);
525 if (!(user = *argp++) || *user == '-')
526 adios (NULL, "missing argument to %s", argp[-2]);
530 if (!(port = *argp++) || *port == '-')
531 adios (NULL, "missing argument to %s", argp[-2]);
539 if (!(cp = *argp++) || *cp == '-')
540 adios (NULL, "missing argument to %s", argp[-2]);
545 if (!(cp = *argp++) || *cp == '-')
546 adios (NULL, "missing argument to %s", argp[-2]);
552 adios (NULL, "only one message at a time!");
560 adios (NULL, "usage: %s [switches] file", invo_name);
562 if (outputlinelen < 10)
563 adios (NULL, "impossible width %d", outputlinelen);
565 if ((in = fopen (msg, "r")) == NULL)
566 adios (msg, "unable to open");
571 discard (out = stdout); /* XXX: reference discard() to help loader */
574 if ((out = fopen (fill_in ? fill_in : "/dev/null", "w")) == NULL)
575 adios ("/dev/null", "unable to open");
577 char *cp = m_mktemp(m_maildir(invo_name), NULL, &out);
579 cp = m_mktemp2(NULL, invo_name, NULL, &out);
581 adios ("post", "unable to create temporary file");
584 strncpy(tmpfil, cp, sizeof(tmpfil));
585 chmod (tmpfil, 0600);
589 hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
591 for (compnum = 1, state = FLD;;) {
592 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
597 cp = add (buf, NULL);
598 while (state == FLDPLUS) {
599 state = m_getfld (state, name, buf, sizeof(buf), in);
602 putfmt (name, cp, out);
606 finish_headers (out);
611 finish_headers (out);
612 if (whomsw && !fill_in)
614 fprintf (out, "\n%s", buf);
615 while (state == BODY) {
616 state = m_getfld (state, name, buf, sizeof(buf), in);
622 finish_headers (out);
627 adios (NULL, "message format error in component #%d", compnum);
630 adios (NULL, "getfld() returned %d", state);
646 /* If we are doing a "whom" check */
649 verify_all_addresses (1);
653 if (msgflags & MINV) {
654 make_bcc_file (dashstuff);
655 if (msgflags & MVIS) {
656 verify_all_addresses (verbose);
657 post (tmpfil, 0, verbose);
659 post (bccfil, 1, verbose);
662 post (tmpfil, 0, isatty (1));
669 printf (partno ? "Partial Message #%s Processed\n" : "Message Processed\n",
681 putfmt (char *name, char *str, FILE *out)
683 int count, grp, i, keep;
686 struct mailname *mp = NULL, *np = NULL;
689 while (*str == ' ' || *str == '\t')
692 if (msgstate == NORMAL && uprf (name, "resent")) {
693 advise (NULL, "illegal header line -- %s:", name);
698 if ((i = get_header (name, hdrtab)) == NOTOK) {
699 fprintf (out, "%s: %s", name, str);
704 if (hdr->flags & HIGN) {
706 fprintf (out, "%s: %s", name, str);
709 if (hdr->flags & HBAD) {
711 fprintf (out, "%s: %s", name, str);
713 advise (NULL, "illegal header line -- %s:", name);
718 msgflags |= (hdr->set & ~(MVIS | MINV));
720 if (hdr->flags & HSUB)
721 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
722 if (hdr->flags & HFCC) {
724 fprintf (out, "%s: %s", name, str);
728 if ((cp = strrchr(str, '\n')))
730 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
732 insert_fcc (hdr, pp);
734 insert_fcc (hdr, pp);
738 if (!(hdr->flags & HADR)) {
739 fprintf (out, "%s: %s", name, str);
743 tmpaddrs.m_next = NULL;
744 for (count = 0; (cp = getname (str)); count++)
745 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
749 tmpaddrs.m_next = mp;
753 if (hdr->flags & HTRY)
759 if (hdr->flags & HNIL)
760 fprintf (out, "%s: %s", name, str);
763 advise (NULL, "%s: field requires at least one address", name);
770 nameoutput = linepos = 0;
771 snprintf (namep, sizeof(namep), "%s%s",
772 !fill_in && (hdr->flags & HMNG) ? "Original-" : "", name);
774 for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
775 if (mp->m_nohost) { /* also used to test (hdr->flags & HTRY) */
776 /* The address doesn't include a host, so it might be an alias. */
777 pp = akvalue (mp->m_mbox); /* do mh alias substitution */
778 qp = akvisible () ? mp->m_mbox : "";
781 putgrp (namep, np->m_gname, out, hdr->flags);
782 while ((cp = getname (pp))) {
783 if (!(mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
788 if (draft_from_masquerading && ((msgstate == RESENT)
790 : (hdr->set & MFRM)))
791 /* The user manually specified a [Resent-]From: address in
792 their draft and the "masquerade:" line in mts.conf
793 doesn't contain "draft_from", so we'll set things up to
794 use the actual email address embedded in the draft
795 [Resent-]From: (after alias substitution, and without the
796 GECOS full name or angle brackets) as the envelope
798 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
800 if (hdr->flags & HBCC)
803 mp->m_ingrp = np->m_ingrp;
806 putgrp (namep, mp->m_gname, out, hdr->flags);
809 if (putadr (namep, qp, mp, out, hdr->flags))
810 msgflags |= (hdr->set & (MVIS | MINV));
819 /* Address includes a host, so no alias substitution is needed. */
820 if (draft_from_masquerading && ((msgstate == RESENT)
822 : (hdr->set & MFRM)))
823 /* The user manually specified a [Resent-]From: address in
824 their draft and the "masquerade:" line in mts.conf
825 doesn't contain "draft_from", so we'll set things up to
826 use the actual email address embedded in the draft
827 [Resent-]From: (after alias substitution, and without the
828 GECOS full name or angle brackets) as the envelope
830 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
832 if (hdr->flags & HBCC)
835 putgrp (namep, mp->m_gname, out, hdr->flags);
838 keep = putadr (namep, "", mp, out, hdr->flags);
842 msgflags |= (hdr->set & (MVIS | MINV));
848 if (grp > 0 && (hdr->flags & HNGR)) {
849 advise (NULL, "%s: field does not allow groups", name);
853 if (fill_in && grp > 0)
864 char myhost[BUFSIZ], sigbuf[BUFSIZ];
871 strncpy (from, getlocaladdr(), sizeof(from));
872 strncpy (myhost, LocalName (0), sizeof(myhost));
874 for (cp = myhost; *cp; cp++)
877 if ((cp = getfullname ()) && *cp) {
878 strncpy (sigbuf, cp, sizeof(sigbuf));
879 snprintf (signature, sizeof(signature), "%s <%s>",
880 sigbuf, getlocaladdr());
881 if ((cp = getname (signature)) == NULL)
882 adios (NULL, "getname () failed -- you lose extraordinarily big");
883 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL)) == NULL)
884 adios (NULL, "bad signature '%s'", sigbuf);
889 strncpy (signature, getlocaladdr(), sizeof(signature));
895 * Now that we've outputted the header fields in the draft
896 * message, we will now output any remaining header fields
897 * that we need to add/create.
901 finish_headers (FILE *out)
905 if (whomsw && !fill_up)
908 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
910 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
911 (int) getpid (), (long) tclock, LocalName (1));
912 if (msgflags & MFRM) {
913 /* There was already a From: in the draft. Don't add one. */
914 if (!draft_from_masquerading)
915 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
916 so we'll reveal the user's actual account@thismachine
917 address in a Sender: header (and use it as the envelope
919 fprintf (out, "Sender: %s\n", from);
922 /* Construct a From: header. */
923 fprintf (out, "From: %s\n", signature);
927 if (!(msgflags & MVIS))
928 fprintf (out, "Bcc: Blind Distribution List: ;\n");
932 if (!(msgflags & MDAT)) {
933 advise (NULL, "message has no Date: header");
936 if (!(msgflags & MFRM)) {
937 advise (NULL, "message has no From: header");
940 if (whomsw && !fill_up)
943 fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
945 fprintf (out, "Resent-Message-ID: <%d.%ld@%s>\n",
946 (int) getpid (), (long) tclock, LocalName (1));
947 if (msgflags & MRFM) {
948 /* There was already a Resent-From: in draft. Don't add one. */
949 if (!draft_from_masquerading)
950 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
951 so we'll reveal the user's actual account@thismachine
952 address in a Sender: header (and use it as the envelope
954 fprintf (out, "Resent-Sender: %s\n", from);
957 /* Construct a Resent-From: header. */
958 fprintf (out, "Resent-From: %s\n", signature);
961 if (!(msgflags & MVIS))
962 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
967 adios (NULL, "re-format message and try again");
969 adios (NULL, "no addressees");
974 get_header (char *header, struct headers *table)
978 for (h = table; h->value; h++)
979 if (!mh_strcasecmp (header, h->value))
987 putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flags)
993 if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
995 if ((!fill_in && (flags & (HBCC | HDCC))) || mp->m_ingrp)
999 fprintf (out, "%s: ", name);
1000 linepos += (nameoutput = strlen (name) + 2);
1003 if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
1004 mp->m_pers = getcpy (aka);
1006 if (mp->m_gname && !fill_in) {
1007 snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
1010 cp = adrformat (mp);
1017 if (linepos != nameoutput) {
1018 if (len + linepos + 2 > outputlinelen)
1019 fprintf (out, ",\n%*s", linepos = nameoutput, "");
1029 return (flags & HTRY);
1034 putgrp (char *name, char *group, FILE *out, unsigned int flags)
1039 if (!fill_in && (flags & HBCC))
1043 fprintf (out, "%s: ", name);
1044 linepos += (nameoutput = strlen (name) + 2);
1046 linepos -= strlen (group);
1049 cp = fill_in ? group : concat (group, ";", NULL);
1052 if (linepos > nameoutput) {
1053 if (len + linepos + 2 > outputlinelen) {
1054 fprintf (out, ",\n%*s", nameoutput, "");
1055 linepos = nameoutput;
1069 insert (struct mailname *np)
1071 struct mailname *mp;
1073 if (np->m_mbox == NULL)
1076 for (mp = np->m_type == LOCALHOST ? &localaddrs
1077 : np->m_type == UUCPHOST ? &uuaddrs
1081 if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
1082 && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox)
1083 && np->m_bcc == mp->m_next->m_bcc)
1096 struct mailname *mp;
1098 printf ("-------\n\t-- Addresses --\nlocal:\t");
1099 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1100 printf ("%s%s%s", mp->m_mbox,
1101 mp->m_bcc ? "[BCC]" : "",
1102 mp->m_next ? ",\n\t" : "");
1104 printf ("\nnet:\t");
1105 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1106 printf ("%s%s@%s%s%s", mp->m_path ? mp->m_path : "",
1107 mp->m_mbox, mp->m_host,
1108 mp->m_bcc ? "[BCC]" : "",
1109 mp->m_next ? ",\n\t" : "");
1111 printf ("\nuucp:\t");
1112 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1113 printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1114 mp->m_bcc ? "[BCC]" : "",
1115 mp->m_next ? ",\n\t" : "");
1117 printf ("\n\t-- Folder Copies --\nfcc:\t");
1118 for (i = 0; i < fccind; i++)
1119 printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1127 struct mailname *mp;
1129 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1130 if (annoaux (mp) == NOTOK)
1133 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1134 if (annoaux (mp) == NOTOK)
1137 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1138 if (annoaux (mp) == NOTOK)
1148 annoaux (struct mailname *mp)
1151 char buffer[BUFSIZ];
1153 snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1154 i = strlen (buffer);
1156 return (write (pfd, buffer, i) == i ? OK : NOTOK);
1161 insert_fcc (struct headers *hdr, unsigned char *pp)
1165 for (cp = pp; isspace (*cp); cp++)
1167 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
1175 adios (NULL, "too many %ss", hdr->value);
1176 fccfold[fccind++] = getcpy (cp);
1184 make_bcc_file (int dashstuff)
1192 tfile = m_mktemp2(NULL, "bccs", NULL, &out);
1193 if (tfile == NULL) adios("bcc", "unable to create temporary file");
1194 chmod (bccfil, 0600);
1195 strncpy (bccfil, tfile, sizeof(bccfil));
1197 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1199 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
1200 (int) getpid (), (long) tclock, LocalName (1));
1201 if (msgflags & MFRM) {
1202 /* There was already a From: in the draft. Don't add one. */
1203 if (!draft_from_masquerading)
1204 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
1205 so we'll reveal the user's actual account@thismachine
1206 address in a Sender: header (and use it as the envelope
1208 fprintf (out, "Sender: %s\n", from);
1211 /* Construct a From: header. */
1212 fprintf (out, "From: %s\n", signature);
1214 fprintf (out, "Subject: %s", subject);
1215 fprintf (out, "BCC:\n");
1218 * Use MIME encapsulation for Bcc messages
1224 * Check if any lines in the message clash with the
1225 * prefix for the MIME multipart separator. If there
1226 * is a clash, increment one of the letters in the
1227 * prefix and check again.
1229 if ((cp = strchr(prefix, 'a')) == NULL)
1230 adios (NULL, "lost prefix start");
1231 while (find_prefix () == NOTOK) {
1236 adios (NULL, "can't find a unique delimiter string");
1241 fprintf (out, "%s: %s\n%s: multipart/digest; boundary=\"",
1242 VRSN_FIELD, VRSN_VALUE, TYPE_FIELD);
1243 fprintf (out, "%s\"\n\n--%s\n\n", prefix, prefix);
1245 fprintf (out, "\n------- Blind-Carbon-Copy\n\n");
1251 * Do mhl filtering of Bcc messages instead
1252 * of MIME encapsulation.
1254 if (filter != NULL) {
1255 vec[0] = r1bindex (mhlproc, '/');
1257 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1261 adios ("fork", "unable to");
1264 dup2 (fileno (out), 1);
1267 vec[i++] = "-forward";
1272 /* was the flag -[no]dashstuffing specified? */
1274 vec[i++] = "-dashstuffing";
1275 else if (dashstuff < 0)
1276 vec[i++] = "-nodashstuffing";
1279 execvp (mhlproc, vec);
1280 fprintf (stderr, "unable to exec ");
1285 pidXwait (child_id, mhlproc);
1289 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
1290 adios (tmpfil, "unable to re-open");
1293 * If using MIME encapsulation, or if the -nodashstuffing
1294 * flag was given, then just copy message. Else do
1295 * RFC934 quoting (dashstuffing).
1297 if (mime || dashstuff < 0)
1298 cpydata (fd, fileno (out), tmpfil, bccfil);
1300 cpydgst (fd, fileno (out), tmpfil, bccfil);
1304 fseek (out, 0L, SEEK_END);
1306 fprintf (out, "\n--%s--\n", prefix);
1308 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
1314 * Scan message to check if any lines clash with
1315 * the prefix of the MIME multipart separator.
1322 unsigned char buffer[BUFSIZ];
1325 if ((in = fopen (tmpfil, "r")) == NULL)
1326 adios (tmpfil, "unable to re-open");
1328 while (fgets (buffer, sizeof(buffer) - 1, in))
1329 if (buffer[0] == '-' && buffer[1] == '-') {
1332 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1336 if (strcmp (buffer + 2, prefix) == 0) {
1347 #define plural(x) (x == 1 ? "" : "s")
1352 if (badadr && unkadr)
1353 die (NULL, "%d address%s unparsable, %d addressee%s undeliverable",
1354 badadr, plural (badadr), unkadr, plural (badadr));
1356 die (NULL, "%d address%s unparsable", badadr, plural (badadr));
1358 die (NULL, "%d addressee%s undeliverable", unkadr, plural (unkadr));
1363 do_addresses (int bccque, int talk)
1367 struct mailname *lp;
1370 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1371 if (lp->m_bcc ? bccque : !bccque) {
1373 printf (" -- Local Recipients --\n");
1374 do_an_address (lp, talk);
1379 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1380 if (lp->m_bcc ? bccque : !bccque) {
1382 printf (" -- UUCP Recipients --\n");
1383 do_an_address (lp, talk);
1388 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1389 if (lp->m_bcc ? bccque : !bccque) {
1391 printf (" -- Network Recipients --\n");
1392 do_an_address (lp, talk);
1398 if (rp_isbad (retval = sm_waend ()))
1399 die (NULL, "problem ending addresses; %s", rp_string (retval));
1404 * MTS-SPECIFIC INTERACTION
1409 * 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, saslssf,
1431 saslmech, user, tls))
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 saslssf, 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));
1607 if (msgflags & MINV)
1610 if (!whomsw || checksw)
1623 hstat = SIGNAL2 (SIGHUP, sigser);
1624 istat = SIGNAL2 (SIGINT, sigser);
1625 qstat = SIGNAL2 (SIGQUIT, sigser);
1626 tstat = SIGNAL2 (SIGTERM, sigser);
1636 SIGNAL (SIGHUP, hstat);
1637 SIGNAL (SIGINT, istat);
1638 SIGNAL (SIGQUIT, qstat);
1639 SIGNAL (SIGTERM, tstat);
1647 p_refile (char *file)
1655 printf (" -- Filing Folder Copies --\n");
1656 for (i = 0; i < fccind; i++)
1657 fcc (file, fccfold[i]);
1659 printf (" -- Folder Copies Filed --\n");
1664 * Call the `fileproc' to add the file to the folder.
1668 fcc (char *file, char *folder)
1675 printf (" %sFcc %s: ", msgstate == RESENT ? "Resent-" : "", folder);
1678 for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
1684 fprintf (stderr, " %sFcc %s: ",
1685 msgstate == RESENT ? "Resent-" : "", folder);
1686 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
1690 /* see if we need to add `+' */
1691 snprintf (fold, sizeof(fold), "%s%s",
1692 *folder == '+' || *folder == '@' ? "" : "+", folder);
1694 /* now exec the fileproc */
1695 execlp (fileproc, r1bindex (fileproc, '/'),
1696 "-link", "-file", file, fold, NULL);
1700 if ((status = pidwait (child_id, OK))) {
1702 fprintf (stderr, " %sFcc %s: ",
1703 msgstate == RESENT ? "Resent-" : "", folder);
1704 pidstatus (status, verbose ? stdout : stderr, NULL);
1707 printf ("folder ok\n");
1719 die (char *what, char *fmt, ...)
1724 if (msgflags & MINV)
1727 if (!whomsw || checksw)
1731 advertise (what, NULL, fmt, ap);