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 */
49 #define FCCS 10 /* max number of fccs allowed */
51 #define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : c)
53 /* In the following array of structures, the numeric second field of the
54 structures (minchars) is apparently used like this:
56 -# : Switch can be abbreviated to # characters; switch hidden in -help.
57 0 : Switch can't be abbreviated; switch shown in -help.
58 # : Switch can be abbreviated to # characters; switch shown in -help. */
60 static struct swit switches[] = {
62 { "alias aliasfile", 0 },
64 { "check", -5 }, /* interface from whom */
66 { "nocheck", -7 }, /* interface from whom */
70 { "dist", -4 }, /* interface from dist */
72 { "filter filterfile", 0 },
80 { "library directory", -7 }, /* interface from send, whom */
98 { "whom", -4 }, /* interface from whom */
100 { "width columns", 0 },
105 #define BITSTUFFSW 22
106 { "dashstuffing", -12 }, /* should we dashstuff BCC messages? */
107 #define NBITSTUFFSW 23
108 { "nodashstuffing", -14 },
110 { "mail", -4 }, /* specify MAIL smtp mode */
112 { "saml", -4 }, /* specify SAML smtp mode */
114 { "send", -4 }, /* specify SEND smtp mode */
116 { "soml", -4 }, /* specify SOML smtp mode */
118 { "idanno number", -6 }, /* interface from send */
120 { "deliver address-list", -7 },
122 { "client host", -6 },
124 { "server host", -6 }, /* specify alternate SMTP server */
126 { "snoop", -5 }, /* snoop the SMTP transaction */
128 { "fill-in file", -7 },
136 { "sasl", SASLminc(-4) },
137 #define SASLMECHSW 38
138 { "saslmech", SASLminc(-5) },
140 { "user", SASLminc(-4) },
142 { "port server port name/number", 4 },
154 * flags for headers->flags
156 #define HNOP 0x0000 /* just used to keep .set around */
157 #define HBAD 0x0001 /* bad header - don't let it through */
158 #define HADR 0x0002 /* header has an address field */
159 #define HSUB 0x0004 /* Subject: header */
160 #define HTRY 0x0008 /* try to send to addrs on header */
161 #define HBCC 0x0010 /* don't output this header */
162 #define HMNG 0x0020 /* munge this header */
163 #define HNGR 0x0040 /* no groups allowed in this header */
164 #define HFCC 0x0080 /* FCC: type header */
165 #define HNIL 0x0100 /* okay for this header not to have addrs */
166 #define HIGN 0x0200 /* ignore this header */
167 #define HDCC 0x0400 /* another undocumented feature */
170 * flags for headers->set
172 #define MFRM 0x0001 /* we've seen a From: */
173 #define MDAT 0x0002 /* we've seen a Date: */
174 #define MRFM 0x0004 /* we've seen a Resent-From: */
175 #define MVIS 0x0008 /* we've seen sighted addrs */
176 #define MINV 0x0010 /* we've seen blind addrs */
179 static struct headers NHeaders[] = {
180 { "Return-Path", HBAD, 0 },
181 { "Received", HBAD, 0 },
182 { "Reply-To", HADR|HNGR, 0 },
183 { "From", HADR|HNGR, MFRM },
184 { "Sender", HADR|HBAD, 0 },
186 { "Subject", HSUB, 0 },
187 { "To", HADR|HTRY, MVIS },
188 { "cc", HADR|HTRY, MVIS },
189 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
190 { "Dcc", HADR|HTRY|HDCC|HNIL, MVIS }, /* sorta cc & bcc combined */
191 { "Message-ID", HBAD, 0 },
196 static struct headers RHeaders[] = {
197 { "Resent-Reply-To", HADR|HNGR, 0 },
198 { "Resent-From", HADR|HNGR, MRFM },
199 { "Resent-Sender", HADR|HBAD, 0 },
200 { "Resent-Date", HBAD, 0 },
201 { "Resent-Subject", HSUB, 0 },
202 { "Resent-To", HADR|HTRY, MVIS },
203 { "Resent-cc", HADR|HTRY, MVIS },
204 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
205 { "Resent-Message-ID", HBAD, 0 },
206 { "Resent-Fcc", HFCC, 0 },
207 { "Reply-To", HADR, 0 },
208 { "From", HADR|HNGR, MFRM },
209 { "Sender", HADR|HNGR, 0 },
210 { "Date", HNOP, MDAT },
211 { "To", HADR|HNIL, 0 },
212 { "cc", HADR|HNIL, 0 },
213 { "Bcc", HADR|HTRY|HBCC|HNIL, 0 },
218 static short fccind = 0; /* index into fccfold[] */
219 static short outputlinelen = OUTPUTLINELEN;
221 static int pfd = NOTOK; /* fd to write annotation list to */
222 static uid_t myuid= -1; /* my user id */
223 static gid_t mygid= -1; /* my group id */
224 static int recipients = 0; /* how many people will get a copy */
225 static int unkadr = 0; /* how many of those were unknown */
226 static int badadr = 0; /* number of bad addrs */
227 static int badmsg = 0; /* message has bad semantics */
228 static int verbose = 0; /* spell it out */
229 static int format = 1; /* format addresses */
230 static int mime = 0; /* use MIME-style encapsulations for Bcc */
231 static int msgid = 0; /* add msgid */
232 static int debug = 0; /* debugging post */
233 static int watch = 0; /* watch the delivery process */
234 static int whomsw = 0; /* we are whom not post */
235 static int checksw = 0; /* whom -check */
236 static int linepos=0; /* putadr()'s position on the line */
237 static int nameoutput=0; /* putadr() has output header name */
238 static int sasl=0; /* Use SASL auth for SMTP */
239 static char *saslmech=NULL; /* Force use of particular SASL mech */
240 static char *user=NULL; /* Authenticate as this user */
241 static char *port="smtp"; /* Name of server port for SMTP */
243 static unsigned msgflags = 0; /* what we've seen */
247 static int msgstate = NORMAL;
249 static time_t tclock = 0; /* the time we started (more or less) */
251 static SIGNAL_HANDLER hstat, istat, qstat, tstat;
253 static char tmpfil[BUFSIZ];
254 static char bccfil[BUFSIZ];
256 static char from[BUFSIZ]; /* my network address */
257 static char signature[BUFSIZ]; /* my signature */
258 static char *filter = NULL; /* the filter for BCC'ing */
259 static char *subject = NULL; /* the subject field for BCC'ing */
260 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
262 static struct headers *hdrtab; /* table for the message we're doing */
264 static struct mailname localaddrs={NULL}; /* local addrs */
265 static struct mailname netaddrs={NULL}; /* network addrs */
266 static struct mailname uuaddrs={NULL}; /* uucp addrs */
267 static struct mailname tmpaddrs={NULL}; /* temporary queue */
270 static int snoop = 0;
271 static int smtpmode = S_MAIL;
272 static char *clientsw = NULL;
273 static char *serversw = NULL;
275 extern struct smtp sm_reply;
278 static char prefix[] = "----- =_aaaaaaaaaa";
280 static int fill_up = 0;
281 static char *fill_in = NULL;
282 static char *partno = NULL;
283 static int queued = 0;
285 extern boolean draft_from_masquerading; /* defined in mts.c */
290 static void putfmt (char *, char *, FILE *);
291 static void start_headers (void);
292 static void finish_headers (FILE *);
293 static int get_header (char *, struct headers *);
294 static int putadr (char *, char *, struct mailname *, FILE *, unsigned int);
295 static void putgrp (char *, char *, FILE *, unsigned int);
296 static int insert (struct mailname *);
297 static void pl (void);
298 static void anno (void);
299 static int annoaux (struct mailname *);
300 static void insert_fcc (struct headers *, unsigned char *);
301 static void make_bcc_file (int);
302 static void verify_all_addresses (int);
303 static void chkadr (void);
304 static void sigon (void);
305 static void sigoff (void);
306 static void p_refile (char *);
307 static void fcc (char *, char *);
308 static void die (char *, char *, ...);
309 static void post (char *, int, int);
310 static void do_text (char *file, int fd);
311 static void do_an_address (struct mailname *, int);
312 static void do_addresses (int, int);
313 static int find_prefix (void);
317 main (int argc, char **argv)
319 int state, compnum, dashstuff = 0;
320 char *cp, *msg = NULL, **argp, **arguments;
321 char buf[BUFSIZ], name[NAMESZ];
325 setlocale(LC_ALL, "");
327 invo_name = r1bindex (argv[0], '/');
329 /* foil search of user profile/context */
330 if (context_foil (NULL) == -1)
333 mts_init (invo_name);
334 arguments = getarguments (invo_name, argc, argv, 0);
337 while ((cp = *argp++)) {
339 switch (smatch (++cp, switches)) {
341 ambigsw (cp, switches);
344 adios (NULL, "-%s unknown", cp);
347 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
348 print_help (buf, switches, 0);
351 print_version(invo_name);
355 if (!(cp = *argp++) || *cp == '-')
356 adios (NULL, "missing argument to %s", argp[-2]);
357 /* create a minimal context */
358 if (context_foil (cp) == -1)
363 if (!(cp = *argp++) || *cp == '-')
364 adios (NULL, "missing argument to %s", argp[-2]);
365 if ((state = alias (cp)) != AK_OK)
366 adios (NULL, "aliasing error in %s - %s",
367 cp, akerror (state));
386 if (!(filter = *argp++) || *filter == '-')
387 adios (NULL, "missing argument to %s", argp[-2]);
442 if (!(cp = *argp++) || *cp == '-')
443 adios (NULL, "missing argument to %s", argp[-2]);
444 if ((outputlinelen = atoi (cp)) < 10)
445 adios (NULL, "impossible width %d", outputlinelen);
449 if (!(cp = *argp++) || *cp == '-')
450 adios (NULL, "missing argument to %s", argp[-2]);
451 if ((pfd = atoi (cp)) <= 2)
452 adios (NULL, "bad argument %s %s", argp[-2], cp);
456 if (!(cp = *argp++) || *cp == '-')
457 adios (NULL, "missing argument to %s", argp[-2]);
463 if (!(cp = *argp++) || *cp == '-')
464 adios (NULL, "missing argument to %s", argp[-2]);
483 if (!(clientsw = *argp++) || *clientsw == '-')
484 adios (NULL, "missing argument to %s", argp[-2]);
487 if (!(serversw = *argp++) || *serversw == '-')
488 adios (NULL, "missing argument to %s", argp[-2]);
496 if (!(fill_in = *argp++) || *fill_in == '-')
497 adios (NULL, "missing argument to %s", argp[-2]);
503 if (!(partno = *argp++) || *partno == '-')
504 adios (NULL, "missing argument to %s", argp[-2]);
516 if (!(saslmech = *argp++) || *saslmech == '-')
517 adios (NULL, "missing argument to %s", argp[-2]);
521 if (!(user = *argp++) || *user == '-')
522 adios (NULL, "missing argument to %s", argp[-2]);
526 if (!(port = *argp++) || *port == '-')
527 adios (NULL, "missing argument to %s", argp[-2]);
532 adios (NULL, "only one message at a time!");
540 adios (NULL, "usage: %s [switches] file", invo_name);
542 if (outputlinelen < 10)
543 adios (NULL, "impossible width %d", outputlinelen);
545 if ((in = fopen (msg, "r")) == NULL)
546 adios (msg, "unable to open");
551 discard (out = stdout); /* XXX: reference discard() to help loader */
554 if ((out = fopen (fill_in ? fill_in : "/dev/null", "w")) == NULL)
555 adios ("/dev/null", "unable to open");
557 char *cp = m_mktemp(m_maildir(invo_name), NULL, &out);
559 cp = m_mktemp2(NULL, invo_name, NULL, &out);
561 adios ("post", "unable to create temporary file");
564 strncpy(tmpfil, cp, sizeof(tmpfil));
565 chmod (tmpfil, 0600);
569 hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
571 for (compnum = 1, state = FLD;;) {
572 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
577 cp = add (buf, NULL);
578 while (state == FLDPLUS) {
579 state = m_getfld (state, name, buf, sizeof(buf), in);
582 putfmt (name, cp, out);
586 finish_headers (out);
591 finish_headers (out);
592 if (whomsw && !fill_in)
594 fprintf (out, "\n%s", buf);
595 while (state == BODY) {
596 state = m_getfld (state, name, buf, sizeof(buf), in);
602 finish_headers (out);
607 adios (NULL, "message format error in component #%d", compnum);
610 adios (NULL, "getfld() returned %d", state);
626 /* If we are doing a "whom" check */
629 verify_all_addresses (1);
633 if (msgflags & MINV) {
634 make_bcc_file (dashstuff);
635 if (msgflags & MVIS) {
636 verify_all_addresses (verbose);
637 post (tmpfil, 0, verbose);
639 post (bccfil, 1, verbose);
642 post (tmpfil, 0, isatty (1));
649 printf (partno ? "Partial Message #%s Processed\n" : "Message Processed\n",
661 putfmt (char *name, char *str, FILE *out)
663 int count, grp, i, keep;
666 struct mailname *mp = NULL, *np = NULL;
669 while (*str == ' ' || *str == '\t')
672 if (msgstate == NORMAL && uprf (name, "resent")) {
673 advise (NULL, "illegal header line -- %s:", name);
678 if ((i = get_header (name, hdrtab)) == NOTOK) {
679 fprintf (out, "%s: %s", name, str);
684 if (hdr->flags & HIGN) {
686 fprintf (out, "%s: %s", name, str);
689 if (hdr->flags & HBAD) {
691 fprintf (out, "%s: %s", name, str);
693 advise (NULL, "illegal header line -- %s:", name);
698 msgflags |= (hdr->set & ~(MVIS | MINV));
700 if (hdr->flags & HSUB)
701 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
702 if (hdr->flags & HFCC) {
704 fprintf (out, "%s: %s", name, str);
708 if ((cp = strrchr(str, '\n')))
710 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
712 insert_fcc (hdr, pp);
714 insert_fcc (hdr, pp);
718 if (!(hdr->flags & HADR)) {
719 fprintf (out, "%s: %s", name, str);
723 tmpaddrs.m_next = NULL;
724 for (count = 0; (cp = getname (str)); count++)
725 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
729 tmpaddrs.m_next = mp;
733 if (hdr->flags & HTRY)
739 if (hdr->flags & HNIL)
740 fprintf (out, "%s: %s", name, str);
743 advise (NULL, "%s: field requires at least one address", name);
750 nameoutput = linepos = 0;
751 snprintf (namep, sizeof(namep), "%s%s",
752 !fill_in && (hdr->flags & HMNG) ? "Original-" : "", name);
754 for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
755 if (mp->m_nohost) { /* also used to test (hdr->flags & HTRY) */
756 /* The address doesn't include a host, so it might be an alias. */
757 pp = akvalue (mp->m_mbox); /* do mh alias substitution */
758 qp = akvisible () ? mp->m_mbox : "";
761 putgrp (namep, np->m_gname, out, hdr->flags);
762 while ((cp = getname (pp))) {
763 if (!(mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
768 if (draft_from_masquerading && ((msgstate == RESENT)
770 : (hdr->set & MFRM)))
771 /* The user manually specified a [Resent-]From: address in
772 their draft and the "masquerade:" line in mts.conf
773 doesn't contain "draft_from", so we'll set things up to
774 use the actual email address embedded in the draft
775 [Resent-]From: (after alias substitution, and without the
776 GECOS full name or angle brackets) as the envelope
778 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
780 if (hdr->flags & HBCC)
783 mp->m_ingrp = np->m_ingrp;
786 putgrp (namep, mp->m_gname, out, hdr->flags);
789 if (putadr (namep, qp, mp, out, hdr->flags))
790 msgflags |= (hdr->set & (MVIS | MINV));
799 /* Address includes a host, so no alias substitution is needed. */
800 if (draft_from_masquerading && ((msgstate == RESENT)
802 : (hdr->set & MFRM)))
803 /* The user manually specified a [Resent-]From: address in
804 their draft and the "masquerade:" line in mts.conf
805 doesn't contain "draft_from", so we'll set things up to
806 use the actual email address embedded in the draft
807 [Resent-]From: (after alias substitution, and without the
808 GECOS full name or angle brackets) as the envelope
810 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
812 if (hdr->flags & HBCC)
815 putgrp (namep, mp->m_gname, out, hdr->flags);
818 keep = putadr (namep, "", mp, out, hdr->flags);
822 msgflags |= (hdr->set & (MVIS | MINV));
828 if (grp > 0 && (hdr->flags & HNGR)) {
829 advise (NULL, "%s: field does not allow groups", name);
833 if (fill_in && grp > 0)
844 char myhost[BUFSIZ], sigbuf[BUFSIZ];
851 strncpy (from, adrsprintf (NULL, NULL), sizeof(from));
852 strncpy (myhost, LocalName (), sizeof(myhost));
854 for (cp = myhost; *cp; cp++)
857 if ((cp = getfullname ()) && *cp) {
858 strncpy (sigbuf, cp, sizeof(sigbuf));
859 snprintf (signature, sizeof(signature), "%s <%s>",
860 sigbuf, adrsprintf (NULL, NULL));
861 if ((cp = getname (signature)) == NULL)
862 adios (NULL, "getname () failed -- you lose extraordinarily big");
863 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL)) == NULL)
864 adios (NULL, "bad signature '%s'", sigbuf);
869 strncpy (signature, adrsprintf (NULL, NULL), sizeof(signature));
875 * Now that we've outputted the header fields in the draft
876 * message, we will now output any remaining header fields
877 * that we need to add/create.
881 finish_headers (FILE *out)
885 if (whomsw && !fill_up)
888 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
890 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
891 (int) getpid (), (long) tclock, LocalName ());
892 if (msgflags & MFRM) {
893 /* There was already a From: in the draft. Don't add one. */
894 if (!draft_from_masquerading)
895 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
896 so we'll reveal the user's actual account@thismachine
897 address in a Sender: header (and use it as the envelope
899 fprintf (out, "Sender: %s\n", from);
902 /* Construct a From: header. */
903 fprintf (out, "From: %s\n", signature);
907 if (!(msgflags & MVIS))
908 fprintf (out, "Bcc: Blind Distribution List: ;\n");
912 if (!(msgflags & MDAT)) {
913 advise (NULL, "message has no Date: header");
916 if (!(msgflags & MFRM)) {
917 advise (NULL, "message has no From: header");
920 if (whomsw && !fill_up)
923 fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
925 fprintf (out, "Resent-Message-ID: <%d.%ld@%s>\n",
926 (int) getpid (), (long) tclock, LocalName ());
927 if (msgflags & MRFM) {
928 /* There was already a Resent-From: in 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, "Resent-Sender: %s\n", from);
937 /* Construct a Resent-From: header. */
938 fprintf (out, "Resent-From: %s\n", signature);
941 if (!(msgflags & MVIS))
942 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
947 adios (NULL, "re-format message and try again");
949 adios (NULL, "no addressees");
954 get_header (char *header, struct headers *table)
958 for (h = table; h->value; h++)
959 if (!mh_strcasecmp (header, h->value))
967 putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flags)
973 if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
975 if ((!fill_in && (flags & (HBCC | HDCC))) || mp->m_ingrp)
979 fprintf (out, "%s: ", name);
980 linepos += (nameoutput = strlen (name) + 2);
983 if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
984 mp->m_pers = getcpy (aka);
986 if (mp->m_gname && !fill_in) {
987 snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
997 if (linepos != nameoutput) {
998 if (len + linepos + 2 > outputlinelen)
999 fprintf (out, ",\n%*s", linepos = nameoutput, "");
1009 return (flags & HTRY);
1014 putgrp (char *name, char *group, FILE *out, unsigned int flags)
1019 if (!fill_in && (flags & HBCC))
1023 fprintf (out, "%s: ", name);
1024 linepos += (nameoutput = strlen (name) + 2);
1026 linepos -= strlen (group);
1029 cp = fill_in ? group : concat (group, ";", NULL);
1032 if (linepos > nameoutput) {
1033 if (len + linepos + 2 > outputlinelen) {
1034 fprintf (out, ",\n%*s", nameoutput, "");
1035 linepos = nameoutput;
1049 insert (struct mailname *np)
1051 struct mailname *mp;
1053 if (np->m_mbox == NULL)
1056 for (mp = np->m_type == LOCALHOST ? &localaddrs
1057 : np->m_type == UUCPHOST ? &uuaddrs
1061 if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
1062 && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox)
1063 && np->m_bcc == mp->m_next->m_bcc)
1076 struct mailname *mp;
1078 printf ("-------\n\t-- Addresses --\nlocal:\t");
1079 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1080 printf ("%s%s%s", mp->m_mbox,
1081 mp->m_bcc ? "[BCC]" : "",
1082 mp->m_next ? ",\n\t" : "");
1084 printf ("\nnet:\t");
1085 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1086 printf ("%s%s@%s%s%s", mp->m_path ? mp->m_path : "",
1087 mp->m_mbox, mp->m_host,
1088 mp->m_bcc ? "[BCC]" : "",
1089 mp->m_next ? ",\n\t" : "");
1091 printf ("\nuucp:\t");
1092 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1093 printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1094 mp->m_bcc ? "[BCC]" : "",
1095 mp->m_next ? ",\n\t" : "");
1097 printf ("\n\t-- Folder Copies --\nfcc:\t");
1098 for (i = 0; i < fccind; i++)
1099 printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1107 struct mailname *mp;
1109 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1110 if (annoaux (mp) == NOTOK)
1113 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1114 if (annoaux (mp) == NOTOK)
1117 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1118 if (annoaux (mp) == NOTOK)
1128 annoaux (struct mailname *mp)
1131 char buffer[BUFSIZ];
1133 snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1134 i = strlen (buffer);
1136 return (write (pfd, buffer, i) == i ? OK : NOTOK);
1141 insert_fcc (struct headers *hdr, unsigned char *pp)
1145 for (cp = pp; isspace (*cp); cp++)
1147 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
1155 adios (NULL, "too many %ss", hdr->value);
1156 fccfold[fccind++] = getcpy (cp);
1164 make_bcc_file (int dashstuff)
1172 tfile = m_mktemp2(NULL, "bccs", NULL, &out);
1173 if (tfile == NULL) adios("bcc", "unable to create temporary file");
1174 chmod (bccfil, 0600);
1175 strncpy (bccfil, tfile, sizeof(bccfil));
1177 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1179 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
1180 (int) getpid (), (long) tclock, LocalName ());
1181 if (msgflags & MFRM) {
1182 /* There was already a From: in the draft. Don't add one. */
1183 if (!draft_from_masquerading)
1184 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
1185 so we'll reveal the user's actual account@thismachine
1186 address in a Sender: header (and use it as the envelope
1188 fprintf (out, "Sender: %s\n", from);
1191 /* Construct a From: header. */
1192 fprintf (out, "From: %s\n", signature);
1194 fprintf (out, "Subject: %s", subject);
1195 fprintf (out, "BCC:\n");
1198 * Use MIME encapsulation for Bcc messages
1204 * Check if any lines in the message clash with the
1205 * prefix for the MIME multipart separator. If there
1206 * is a clash, increment one of the letters in the
1207 * prefix and check again.
1209 if ((cp = strchr(prefix, 'a')) == NULL)
1210 adios (NULL, "lost prefix start");
1211 while (find_prefix () == NOTOK) {
1216 adios (NULL, "can't find a unique delimiter string");
1221 fprintf (out, "%s: %s\n%s: multipart/digest; boundary=\"",
1222 VRSN_FIELD, VRSN_VALUE, TYPE_FIELD);
1223 fprintf (out, "%s\"\n\n--%s\n\n", prefix, prefix);
1225 fprintf (out, "\n------- Blind-Carbon-Copy\n\n");
1231 * Do mhl filtering of Bcc messages instead
1232 * of MIME encapsulation.
1234 if (filter != NULL) {
1235 vec[0] = r1bindex (mhlproc, '/');
1237 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1241 adios ("fork", "unable to");
1244 dup2 (fileno (out), 1);
1247 vec[i++] = "-forward";
1252 /* was the flag -[no]dashstuffing specified? */
1254 vec[i++] = "-dashstuffing";
1255 else if (dashstuff < 0)
1256 vec[i++] = "-nodashstuffing";
1259 execvp (mhlproc, vec);
1260 fprintf (stderr, "unable to exec ");
1265 pidXwait (child_id, mhlproc);
1269 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
1270 adios (tmpfil, "unable to re-open");
1273 * If using MIME encapsulation, or if the -nodashstuffing
1274 * flag was given, then just copy message. Else do
1275 * RFC934 quoting (dashstuffing).
1277 if (mime || dashstuff < 0)
1278 cpydata (fd, fileno (out), tmpfil, bccfil);
1280 cpydgst (fd, fileno (out), tmpfil, bccfil);
1284 fseek (out, 0L, SEEK_END);
1286 fprintf (out, "\n--%s--\n", prefix);
1288 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
1294 * Scan message to check if any lines clash with
1295 * the prefix of the MIME multipart separator.
1302 unsigned char buffer[BUFSIZ];
1305 if ((in = fopen (tmpfil, "r")) == NULL)
1306 adios (tmpfil, "unable to re-open");
1308 len = strlen (prefix);
1311 while (fgets (buffer, sizeof(buffer) - 1, in))
1312 if (buffer[0] == '-' && buffer[1] == '-') {
1315 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1319 if (strcmp (buffer + 2, prefix) == 0) {
1330 #define plural(x) (x == 1 ? "" : "s")
1335 if (badadr && unkadr)
1336 die (NULL, "%d address%s unparsable, %d addressee%s undeliverable",
1337 badadr, plural (badadr), unkadr, plural (badadr));
1339 die (NULL, "%d address%s unparsable", badadr, plural (badadr));
1341 die (NULL, "%d addressee%s undeliverable", unkadr, plural (unkadr));
1346 do_addresses (int bccque, int talk)
1350 struct mailname *lp;
1353 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1354 if (lp->m_bcc ? bccque : !bccque) {
1356 printf (" -- Local Recipients --\n");
1357 do_an_address (lp, talk);
1362 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1363 if (lp->m_bcc ? bccque : !bccque) {
1365 printf (" -- UUCP Recipients --\n");
1366 do_an_address (lp, talk);
1371 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1372 if (lp->m_bcc ? bccque : !bccque) {
1374 printf (" -- Network Recipients --\n");
1375 do_an_address (lp, talk);
1382 if (rp_isbad (retval = sm_waend ()))
1383 die (NULL, "problem ending addresses; %s", rp_string (retval));
1384 #endif /* SMTPMTS */
1389 * MTS-SPECIFIC INTERACTION
1394 * SENDMAIL/SMTP routines
1400 post (char *file, int bccque, int talk)
1405 onex = !(msgflags & MINV) || bccque;
1407 if (msgflags & MINV)
1408 printf (" -- Posting for %s Recipients --\n",
1409 bccque ? "Blind" : "Sighted");
1411 printf (" -- Posting for All Recipients --\n");
1416 if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch, verbose,
1417 snoop, onex, queued, sasl, saslmech,
1419 || rp_isbad (retval = sm_winit (smtpmode, from)))
1420 die (NULL, "problem initializing server; %s", rp_string (retval));
1422 do_addresses (bccque, talk && verbose);
1423 if ((fd = open (file, O_RDONLY)) == NOTOK)
1424 die (file, "unable to re-open");
1429 sm_end (onex ? OK : DONE);
1433 if (msgflags & MINV)
1434 printf (" -- %s Recipient Copies Posted --\n",
1435 bccque ? "Blind" : "Sighted");
1437 printf (" -- Recipient Copies Posted --\n");
1444 /* Address Verification */
1447 verify_all_addresses (int talk)
1450 struct mailname *lp;
1454 if (!whomsw || checksw)
1455 if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch,
1456 verbose, snoop, 0, queued, sasl,
1458 || rp_isbad (retval = sm_winit (smtpmode, from)))
1459 die (NULL, "problem initializing server; %s", rp_string (retval));
1461 if (talk && !whomsw)
1462 printf (" -- Address Verification --\n");
1463 if (talk && localaddrs.m_next)
1464 printf (" -- Local Recipients --\n");
1465 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1466 do_an_address (lp, talk);
1468 if (talk && uuaddrs.m_next)
1469 printf (" -- UUCP Recipients --\n");
1470 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1471 do_an_address (lp, talk);
1473 if (talk && netaddrs.m_next)
1474 printf (" -- Network Recipients --\n");
1475 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1476 do_an_address (lp, talk);
1479 if (talk && !whomsw)
1480 printf (" -- Address Verification Successful --\n");
1482 if (!whomsw || checksw)
1491 do_an_address (struct mailname *lp, int talk)
1497 switch (lp->m_type) {
1501 strncpy (addr, mbox, sizeof(addr));
1505 mbox = auxformat (lp, 0);
1507 snprintf (addr, sizeof(addr), "%s!%s", lp->m_host, lp->m_mbox);
1510 default: /* let SendMail decide if the host is bad */
1513 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1518 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1520 if (whomsw && !checksw) {
1528 switch (retval = sm_wadr (mbox, host,
1529 lp->m_type != UUCPHOST ? lp->m_path : NULL)) {
1532 printf ("address ok\n");
1538 fprintf (stderr, " %s: ", addr);
1539 fprintf (talk ? stdout : stderr, "loses; %s\n",
1540 rp_string (retval));
1546 fprintf (stderr, " %s: ", addr);
1547 die (NULL, "unexpected response; %s", rp_string (retval));
1555 do_text (char *file, int fd)
1560 lseek (fd, (off_t) 0, SEEK_SET);
1562 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1563 if (rp_isbad (retval = sm_wtxt (buf, state)))
1564 die (NULL, "problem writing text; %s\n", rp_string (retval));
1568 die (file, "problem reading from");
1570 switch (retval = sm_wtend ()) {
1576 die (NULL, "posting failed; %s", rp_string (retval));
1579 die (NULL, "unexpected response; %s", rp_string (retval));
1583 #endif /* SMTPMTS */
1593 #ifndef RELIABLE_SIGNALS
1594 SIGNAL (i, SIG_IGN);
1598 if (msgflags & MINV)
1602 if (!whomsw || checksw)
1604 #endif /* SMTPMTS */
1616 hstat = SIGNAL2 (SIGHUP, sigser);
1617 istat = SIGNAL2 (SIGINT, sigser);
1618 qstat = SIGNAL2 (SIGQUIT, sigser);
1619 tstat = SIGNAL2 (SIGTERM, sigser);
1629 SIGNAL (SIGHUP, hstat);
1630 SIGNAL (SIGINT, istat);
1631 SIGNAL (SIGQUIT, qstat);
1632 SIGNAL (SIGTERM, tstat);
1640 p_refile (char *file)
1648 printf (" -- Filing Folder Copies --\n");
1649 for (i = 0; i < fccind; i++)
1650 fcc (file, fccfold[i]);
1652 printf (" -- Folder Copies Filed --\n");
1657 * Call the `fileproc' to add the file to the folder.
1661 fcc (char *file, char *folder)
1668 printf (" %sFcc %s: ", msgstate == RESENT ? "Resent-" : "", folder);
1671 for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
1677 fprintf (stderr, " %sFcc %s: ",
1678 msgstate == RESENT ? "Resent-" : "", folder);
1679 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
1683 /* see if we need to add `+' */
1684 snprintf (fold, sizeof(fold), "%s%s",
1685 *folder == '+' || *folder == '@' ? "" : "+", folder);
1687 /* now exec the fileproc */
1688 execlp (fileproc, r1bindex (fileproc, '/'),
1689 "-link", "-file", file, fold, NULL);
1693 if ((status = pidwait (child_id, OK))) {
1695 fprintf (stderr, " %sFcc %s: ",
1696 msgstate == RESENT ? "Resent-" : "", folder);
1697 pidstatus (status, verbose ? stdout : stderr, NULL);
1700 printf ("folder ok\n");
1712 die (char *what, char *fmt, ...)
1717 if (msgflags & MINV)
1721 if (!whomsw || checksw)
1723 #endif /* SMTPMTS */
1726 advertise (what, NULL, fmt, ap);