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) },
152 * flags for headers->flags
154 #define HNOP 0x0000 /* just used to keep .set around */
155 #define HBAD 0x0001 /* bad header - don't let it through */
156 #define HADR 0x0002 /* header has an address field */
157 #define HSUB 0x0004 /* Subject: header */
158 #define HTRY 0x0008 /* try to send to addrs on header */
159 #define HBCC 0x0010 /* don't output this header */
160 #define HMNG 0x0020 /* munge this header */
161 #define HNGR 0x0040 /* no groups allowed in this header */
162 #define HFCC 0x0080 /* FCC: type header */
163 #define HNIL 0x0100 /* okay for this header not to have addrs */
164 #define HIGN 0x0200 /* ignore this header */
165 #define HDCC 0x0400 /* another undocumented feature */
168 * flags for headers->set
170 #define MFRM 0x0001 /* we've seen a From: */
171 #define MDAT 0x0002 /* we've seen a Date: */
172 #define MRFM 0x0004 /* we've seen a Resent-From: */
173 #define MVIS 0x0008 /* we've seen sighted addrs */
174 #define MINV 0x0010 /* we've seen blind addrs */
177 static struct headers NHeaders[] = {
178 { "Return-Path", HBAD, 0 },
179 { "Received", HBAD, 0 },
180 { "Reply-To", HADR|HNGR, 0 },
181 { "From", HADR|HNGR, MFRM },
182 { "Sender", HADR|HBAD, 0 },
184 { "Subject", HSUB, 0 },
185 { "To", HADR|HTRY, MVIS },
186 { "cc", HADR|HTRY, MVIS },
187 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
188 { "Dcc", HADR|HTRY|HDCC|HNIL, MVIS }, /* sorta cc & bcc combined */
189 { "Message-ID", HBAD, 0 },
194 static struct headers RHeaders[] = {
195 { "Resent-Reply-To", HADR|HNGR, 0 },
196 { "Resent-From", HADR|HNGR, MRFM },
197 { "Resent-Sender", HADR|HBAD, 0 },
198 { "Resent-Date", HBAD, 0 },
199 { "Resent-Subject", HSUB, 0 },
200 { "Resent-To", HADR|HTRY, MVIS },
201 { "Resent-cc", HADR|HTRY, MVIS },
202 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
203 { "Resent-Message-ID", HBAD, 0 },
204 { "Resent-Fcc", HFCC, 0 },
205 { "Reply-To", HADR, 0 },
206 { "From", HADR|HNGR, MFRM },
207 { "Sender", HADR|HNGR, 0 },
208 { "Date", HNOP, MDAT },
209 { "To", HADR|HNIL, 0 },
210 { "cc", HADR|HNIL, 0 },
211 { "Bcc", HADR|HTRY|HBCC|HNIL, 0 },
216 static short fccind = 0; /* index into fccfold[] */
217 static short outputlinelen = OUTPUTLINELEN;
219 static int pfd = NOTOK; /* fd to write annotation list to */
220 static uid_t myuid= -1; /* my user id */
221 static gid_t mygid= -1; /* my group id */
222 static int recipients = 0; /* how many people will get a copy */
223 static int unkadr = 0; /* how many of those were unknown */
224 static int badadr = 0; /* number of bad addrs */
225 static int badmsg = 0; /* message has bad semantics */
226 static int verbose = 0; /* spell it out */
227 static int format = 1; /* format addresses */
228 static int mime = 0; /* use MIME-style encapsulations for Bcc */
229 static int msgid = 0; /* add msgid */
230 static int debug = 0; /* debugging post */
231 static int watch = 0; /* watch the delivery process */
232 static int whomsw = 0; /* we are whom not post */
233 static int checksw = 0; /* whom -check */
234 static int linepos=0; /* putadr()'s position on the line */
235 static int nameoutput=0; /* putadr() has output header name */
236 static int sasl=0; /* Use SASL auth for SMTP */
237 static char *saslmech=NULL; /* Force use of particular SASL mech */
238 static char *user=NULL; /* Authenticate as this user */
240 static unsigned msgflags = 0; /* what we've seen */
244 static int msgstate = NORMAL;
246 static time_t tclock = 0; /* the time we started (more or less) */
248 static SIGNAL_HANDLER hstat, istat, qstat, tstat;
250 static char tmpfil[BUFSIZ];
251 static char bccfil[BUFSIZ];
253 static char from[BUFSIZ]; /* my network address */
254 static char signature[BUFSIZ]; /* my signature */
255 static char *filter = NULL; /* the filter for BCC'ing */
256 static char *subject = NULL; /* the subject field for BCC'ing */
257 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
259 static struct headers *hdrtab; /* table for the message we're doing */
261 static struct mailname localaddrs={NULL}; /* local addrs */
262 static struct mailname netaddrs={NULL}; /* network addrs */
263 static struct mailname uuaddrs={NULL}; /* uucp addrs */
264 static struct mailname tmpaddrs={NULL}; /* temporary queue */
267 static int snoop = 0;
268 static int smtpmode = S_MAIL;
269 static char *clientsw = NULL;
270 static char *serversw = NULL;
272 extern struct smtp sm_reply;
275 static char prefix[] = "----- =_aaaaaaaaaa";
277 static int fill_up = 0;
278 static char *fill_in = NULL;
279 static char *partno = NULL;
280 static int queued = 0;
282 extern boolean draft_from_masquerading; /* defined in mts.c */
287 static void putfmt (char *, char *, FILE *);
288 static void start_headers (void);
289 static void finish_headers (FILE *);
290 static int get_header (char *, struct headers *);
291 static int putadr (char *, char *, struct mailname *, FILE *, unsigned int);
292 static void putgrp (char *, char *, FILE *, unsigned int);
293 static int insert (struct mailname *);
294 static void pl (void);
295 static void anno (void);
296 static int annoaux (struct mailname *);
297 static void insert_fcc (struct headers *, char *);
298 static void make_bcc_file (int);
299 static void verify_all_addresses (int);
300 static void chkadr (void);
301 static void sigon (void);
302 static void sigoff (void);
303 static void p_refile (char *);
304 static void fcc (char *, char *);
305 static void die (char *, char *, ...);
306 static void post (char *, int, int);
307 static void do_text (char *file, int fd);
308 static void do_an_address (struct mailname *, int);
309 static void do_addresses (int, int);
310 static int find_prefix (void);
314 main (int argc, char **argv)
316 int state, compnum, dashstuff = 0;
317 char *cp, *msg = NULL, **argp, **arguments;
318 char buf[BUFSIZ], name[NAMESZ];
322 setlocale(LC_ALL, "");
324 invo_name = r1bindex (argv[0], '/');
326 /* foil search of user profile/context */
327 if (context_foil (NULL) == -1)
330 mts_init (invo_name);
331 arguments = getarguments (invo_name, argc, argv, 0);
334 while ((cp = *argp++)) {
336 switch (smatch (++cp, switches)) {
338 ambigsw (cp, switches);
341 adios (NULL, "-%s unknown", cp);
344 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
345 print_help (buf, switches, 0);
348 print_version(invo_name);
352 if (!(cp = *argp++) || *cp == '-')
353 adios (NULL, "missing argument to %s", argp[-2]);
354 /* create a minimal context */
355 if (context_foil (cp) == -1)
360 if (!(cp = *argp++) || *cp == '-')
361 adios (NULL, "missing argument to %s", argp[-2]);
362 if ((state = alias (cp)) != AK_OK)
363 adios (NULL, "aliasing error in %s - %s",
364 cp, akerror (state));
383 if (!(filter = *argp++) || *filter == '-')
384 adios (NULL, "missing argument to %s", argp[-2]);
439 if (!(cp = *argp++) || *cp == '-')
440 adios (NULL, "missing argument to %s", argp[-2]);
441 if ((outputlinelen = atoi (cp)) < 10)
442 adios (NULL, "impossible width %d", outputlinelen);
446 if (!(cp = *argp++) || *cp == '-')
447 adios (NULL, "missing argument to %s", argp[-2]);
448 if ((pfd = atoi (cp)) <= 2)
449 adios (NULL, "bad argument %s %s", argp[-2], cp);
453 if (!(cp = *argp++) || *cp == '-')
454 adios (NULL, "missing argument to %s", argp[-2]);
460 if (!(cp = *argp++) || *cp == '-')
461 adios (NULL, "missing argument to %s", argp[-2]);
480 if (!(clientsw = *argp++) || *clientsw == '-')
481 adios (NULL, "missing argument to %s", argp[-2]);
484 if (!(serversw = *argp++) || *serversw == '-')
485 adios (NULL, "missing argument to %s", argp[-2]);
493 if (!(fill_in = *argp++) || *fill_in == '-')
494 adios (NULL, "missing argument to %s", argp[-2]);
500 if (!(partno = *argp++) || *partno == '-')
501 adios (NULL, "missing argument to %s", argp[-2]);
513 if (!(saslmech = *argp++) || *saslmech == '-')
514 adios (NULL, "missing argument to %s", argp[-2]);
518 if (!(user = *argp++) || *user == '-')
519 adios (NULL, "missing argument to %s", argp[-2]);
524 adios (NULL, "only one message at a time!");
532 adios (NULL, "usage: %s [switches] file", invo_name);
534 if (outputlinelen < 10)
535 adios (NULL, "impossible width %d", outputlinelen);
537 if ((in = fopen (msg, "r")) == NULL)
538 adios (msg, "unable to open");
543 discard (out = stdout); /* XXX: reference discard() to help loader */
546 if ((out = fopen (fill_in ? fill_in : "/dev/null", "w")) == NULL)
547 adios ("/dev/null", "unable to open");
549 strncpy (tmpfil, m_scratch ("", m_maildir (invo_name)),
551 if ((out = fopen (tmpfil, "w")) == NULL) {
552 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
553 if ((out = fopen (tmpfil, "w")) == NULL)
554 adios (tmpfil, "unable to create");
556 chmod (tmpfil, 0600);
560 hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
562 for (compnum = 1, state = FLD;;) {
563 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
568 cp = add (buf, NULL);
569 while (state == FLDPLUS) {
570 state = m_getfld (state, name, buf, sizeof(buf), in);
573 putfmt (name, cp, out);
577 finish_headers (out);
582 finish_headers (out);
583 if (whomsw && !fill_in)
585 fprintf (out, "\n%s", buf);
586 while (state == BODY) {
587 state = m_getfld (state, name, buf, sizeof(buf), in);
593 finish_headers (out);
598 adios (NULL, "message format error in component #%d", compnum);
601 adios (NULL, "getfld() returned %d", state);
617 /* If we are doing a "whom" check */
620 verify_all_addresses (1);
624 if (msgflags & MINV) {
625 make_bcc_file (dashstuff);
626 if (msgflags & MVIS) {
627 verify_all_addresses (verbose);
628 post (tmpfil, 0, verbose);
630 post (bccfil, 1, verbose);
633 post (tmpfil, 0, isatty (1));
640 printf (partno ? "Partial Message #%s Processed\n" : "Message Processed\n",
651 putfmt (char *name, char *str, FILE *out)
653 int count, grp, i, keep;
656 struct mailname *mp, *np;
659 while (*str == ' ' || *str == '\t')
662 if (msgstate == NORMAL && uprf (name, "resent")) {
663 advise (NULL, "illegal header line -- %s:", name);
668 if ((i = get_header (name, hdrtab)) == NOTOK) {
669 fprintf (out, "%s: %s", name, str);
674 if (hdr->flags & HIGN) {
676 fprintf (out, "%s: %s", name, str);
679 if (hdr->flags & HBAD) {
681 fprintf (out, "%s: %s", name, str);
683 advise (NULL, "illegal header line -- %s:", name);
688 msgflags |= (hdr->set & ~(MVIS | MINV));
690 if (hdr->flags & HSUB)
691 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
692 if (hdr->flags & HFCC) {
694 fprintf (out, "%s: %s", name, str);
698 if ((cp = strrchr(str, '\n')))
700 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
702 insert_fcc (hdr, pp);
704 insert_fcc (hdr, pp);
708 if (!(hdr->flags & HADR)) {
709 fprintf (out, "%s: %s", name, str);
713 tmpaddrs.m_next = NULL;
714 for (count = 0; (cp = getname (str)); count++)
715 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
719 tmpaddrs.m_next = mp;
723 if (hdr->flags & HTRY)
729 if (hdr->flags & HNIL)
730 fprintf (out, "%s: %s", name, str);
733 advise (NULL, "%s: field requires at least one address", name);
740 nameoutput = linepos = 0;
741 snprintf (namep, sizeof(namep), "%s%s",
742 !fill_in && (hdr->flags & HMNG) ? "Original-" : "", name);
744 for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
745 if (mp->m_nohost) { /* also used to test (hdr->flags & HTRY) */
746 /* The address doesn't include a host, so it might be an alias. */
747 pp = akvalue (mp->m_mbox); /* do mh alias substitution */
748 qp = akvisible () ? mp->m_mbox : "";
751 putgrp (namep, np->m_gname, out, hdr->flags);
752 while ((cp = getname (pp))) {
753 if (!(mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
758 if (draft_from_masquerading && ((msgstate == RESENT)
760 : (hdr->set & MFRM)))
761 /* The user manually specified a [Resent-]From: address in
762 their draft and the "masquerade:" line in mts.conf
763 doesn't contain "draft_from", so we'll set things up to
764 use the actual email address embedded in the draft
765 [Resent-]From: (after alias substitution, and without the
766 GECOS full name or angle brackets) as the envelope
768 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
770 if (hdr->flags & HBCC)
773 mp->m_ingrp = np->m_ingrp;
776 putgrp (namep, mp->m_gname, out, hdr->flags);
779 if (putadr (namep, qp, mp, out, hdr->flags))
780 msgflags |= (hdr->set & (MVIS | MINV));
789 /* Address includes a host, so no alias substitution is needed. */
790 if (draft_from_masquerading && ((msgstate == RESENT)
792 : (hdr->set & MFRM)))
793 /* The user manually specified a [Resent-]From: address in
794 their draft and the "masquerade:" line in mts.conf
795 doesn't contain "draft_from", so we'll set things up to
796 use the actual email address embedded in the draft
797 [Resent-]From: (after alias substitution, and without the
798 GECOS full name or angle brackets) as the envelope
800 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
802 if (hdr->flags & HBCC)
805 putgrp (namep, mp->m_gname, out, hdr->flags);
808 keep = putadr (namep, "", mp, out, hdr->flags);
812 msgflags |= (hdr->set & (MVIS | MINV));
818 if (grp > 0 && (hdr->flags & HNGR)) {
819 advise (NULL, "%s: field does not allow groups", name);
823 if (fill_in && grp > 0)
834 char myhost[BUFSIZ], sigbuf[BUFSIZ];
841 strncpy (from, adrsprintf (NULL, NULL), sizeof(from));
842 strncpy (myhost, LocalName (), sizeof(myhost));
844 for (cp = myhost; *cp; cp++)
847 if ((cp = getfullname ()) && *cp) {
848 strncpy (sigbuf, cp, sizeof(sigbuf));
849 snprintf (signature, sizeof(signature), "%s <%s>",
850 sigbuf, adrsprintf (NULL, NULL));
851 if ((cp = getname (signature)) == NULL)
852 adios (NULL, "getname () failed -- you lose extraordinarily big");
853 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL)) == NULL)
854 adios (NULL, "bad signature '%s'", sigbuf);
859 strncpy (signature, adrsprintf (NULL, NULL), sizeof(signature));
865 * Now that we've outputted the header fields in the draft
866 * message, we will now output any remaining header fields
867 * that we need to add/create.
871 finish_headers (FILE *out)
875 if (whomsw && !fill_up)
878 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
880 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
881 (int) getpid (), (long) tclock, LocalName ());
882 if (msgflags & MFRM) {
883 /* There was already a From: in the draft. Don't add one. */
884 if (!draft_from_masquerading)
885 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
886 so we'll reveal the user's actual account@thismachine
887 address in a Sender: header (and use it as the envelope
889 fprintf (out, "Sender: %s\n", from);
892 /* Construct a From: header. */
893 fprintf (out, "From: %s\n", signature);
897 if (!(msgflags & MVIS))
898 fprintf (out, "Bcc: Blind Distribution List: ;\n");
902 if (!(msgflags & MDAT)) {
903 advise (NULL, "message has no Date: header");
906 if (!(msgflags & MFRM)) {
907 advise (NULL, "message has no From: header");
910 if (whomsw && !fill_up)
913 fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
915 fprintf (out, "Resent-Message-ID: <%d.%ld@%s>\n",
916 (int) getpid (), (long) tclock, LocalName ());
917 if (msgflags & MRFM) {
918 /* There was already a Resent-From: in draft. Don't add one. */
919 if (!draft_from_masquerading)
920 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
921 so we'll reveal the user's actual account@thismachine
922 address in a Sender: header (and use it as the envelope
924 fprintf (out, "Resent-Sender: %s\n", from);
927 /* Construct a Resent-From: header. */
928 fprintf (out, "Resent-From: %s\n", signature);
931 if (!(msgflags & MVIS))
932 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
937 adios (NULL, "re-format message and try again");
939 adios (NULL, "no addressees");
944 get_header (char *header, struct headers *table)
948 for (h = table; h->value; h++)
949 if (!mh_strcasecmp (header, h->value))
957 putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flags)
963 if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
965 if ((!fill_in && (flags & (HBCC | HDCC))) || mp->m_ingrp)
969 fprintf (out, "%s: ", name);
970 linepos += (nameoutput = strlen (name) + 2);
973 if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
974 mp->m_pers = getcpy (aka);
976 if (mp->m_gname && !fill_in) {
977 snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
987 if (linepos != nameoutput) {
988 if (len + linepos + 2 > outputlinelen)
989 fprintf (out, ",\n%*s", linepos = nameoutput, "");
999 return (flags & HTRY);
1004 putgrp (char *name, char *group, FILE *out, unsigned int flags)
1009 if (!fill_in && (flags & HBCC))
1013 fprintf (out, "%s: ", name);
1014 linepos += (nameoutput = strlen (name) + 2);
1016 linepos -= strlen (group);
1019 cp = fill_in ? group : concat (group, ";", NULL);
1022 if (linepos > nameoutput) {
1023 if (len + linepos + 2 > outputlinelen) {
1024 fprintf (out, ",\n%*s", nameoutput, "");
1025 linepos = nameoutput;
1039 insert (struct mailname *np)
1041 struct mailname *mp;
1043 if (np->m_mbox == NULL)
1046 for (mp = np->m_type == LOCALHOST ? &localaddrs
1047 : np->m_type == UUCPHOST ? &uuaddrs
1051 if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
1052 && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox)
1053 && np->m_bcc == mp->m_next->m_bcc)
1066 struct mailname *mp;
1068 printf ("-------\n\t-- Addresses --\nlocal:\t");
1069 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1070 printf ("%s%s%s", mp->m_mbox,
1071 mp->m_bcc ? "[BCC]" : "",
1072 mp->m_next ? ",\n\t" : "");
1074 printf ("\nnet:\t");
1075 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1076 printf ("%s%s@%s%s%s", mp->m_path ? mp->m_path : "",
1077 mp->m_mbox, mp->m_host,
1078 mp->m_bcc ? "[BCC]" : "",
1079 mp->m_next ? ",\n\t" : "");
1081 printf ("\nuucp:\t");
1082 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1083 printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1084 mp->m_bcc ? "[BCC]" : "",
1085 mp->m_next ? ",\n\t" : "");
1087 printf ("\n\t-- Folder Copies --\nfcc:\t");
1088 for (i = 0; i < fccind; i++)
1089 printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1097 struct mailname *mp;
1099 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1100 if (annoaux (mp) == NOTOK)
1103 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1104 if (annoaux (mp) == NOTOK)
1107 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1108 if (annoaux (mp) == NOTOK)
1118 annoaux (struct mailname *mp)
1121 char buffer[BUFSIZ];
1123 snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1124 i = strlen (buffer);
1126 return (write (pfd, buffer, i) == i ? OK : NOTOK);
1131 insert_fcc (struct headers *hdr, char *pp)
1135 for (cp = pp; isspace (*cp); cp++)
1137 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
1145 adios (NULL, "too many %ss", hdr->value);
1146 fccfold[fccind++] = getcpy (cp);
1154 make_bcc_file (int dashstuff)
1161 strncpy (bccfil, m_tmpfil ("bccs"), sizeof(bccfil));
1162 if ((out = fopen (bccfil, "w")) == NULL)
1163 adios (bccfil, "unable to create");
1164 chmod (bccfil, 0600);
1166 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1168 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
1169 (int) getpid (), (long) tclock, LocalName ());
1170 if (msgflags & MFRM) {
1171 /* There was already a From: in the draft. Don't add one. */
1172 if (!draft_from_masquerading)
1173 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
1174 so we'll reveal the user's actual account@thismachine
1175 address in a Sender: header (and use it as the envelope
1177 fprintf (out, "Sender: %s\n", from);
1180 /* Construct a From: header. */
1181 fprintf (out, "From: %s\n", signature);
1183 fprintf (out, "Subject: %s", subject);
1184 fprintf (out, "BCC:\n");
1187 * Use MIME encapsulation for Bcc messages
1193 * Check if any lines in the message clash with the
1194 * prefix for the MIME multipart separator. If there
1195 * is a clash, increment one of the letters in the
1196 * prefix and check again.
1198 if ((cp = strchr(prefix, 'a')) == NULL)
1199 adios (NULL, "lost prefix start");
1200 while (find_prefix () == NOTOK) {
1205 adios (NULL, "can't find a unique delimiter string");
1210 fprintf (out, "%s: %s\n%s: multipart/digest; boundary=\"",
1211 VRSN_FIELD, VRSN_VALUE, TYPE_FIELD);
1212 fprintf (out, "%s\"\n\n--%s\n\n", prefix, prefix);
1214 fprintf (out, "\n------- Blind-Carbon-Copy\n\n");
1220 * Do mhl filtering of Bcc messages instead
1221 * of MIME encapsulation.
1223 if (filter != NULL) {
1224 vec[0] = r1bindex (mhlproc, '/');
1226 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1230 adios ("fork", "unable to");
1233 dup2 (fileno (out), 1);
1236 vec[i++] = "-forward";
1241 /* was the flag -[no]dashstuffing specified? */
1243 vec[i++] = "-dashstuffing";
1244 else if (dashstuff < 0)
1245 vec[i++] = "-nodashstuffing";
1248 execvp (mhlproc, vec);
1249 fprintf (stderr, "unable to exec ");
1254 pidXwait (child_id, mhlproc);
1258 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
1259 adios (tmpfil, "unable to re-open");
1262 * If using MIME encapsulation, or if the -nodashstuffing
1263 * flag was given, then just copy message. Else do
1264 * RFC934 quoting (dashstuffing).
1266 if (mime || dashstuff < 0)
1267 cpydata (fd, fileno (out), tmpfil, bccfil);
1269 cpydgst (fd, fileno (out), tmpfil, bccfil);
1273 fseek (out, 0L, SEEK_END);
1275 fprintf (out, "\n--%s--\n", prefix);
1277 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
1283 * Scan message to check if any lines clash with
1284 * the prefix of the MIME multipart separator.
1291 char buffer[BUFSIZ];
1294 if ((in = fopen (tmpfil, "r")) == NULL)
1295 adios (tmpfil, "unable to re-open");
1297 len = strlen (prefix);
1300 while (fgets (buffer, sizeof(buffer) - 1, in))
1301 if (buffer[0] == '-' && buffer[1] == '-') {
1304 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1308 if (strcmp (buffer + 2, prefix) == 0) {
1319 #define plural(x) (x == 1 ? "" : "s")
1324 if (badadr && unkadr)
1325 die (NULL, "%d address%s unparsable, %d addressee%s undeliverable",
1326 badadr, plural (badadr), unkadr, plural (badadr));
1328 die (NULL, "%d address%s unparsable", badadr, plural (badadr));
1330 die (NULL, "%d addressee%s undeliverable", unkadr, plural (unkadr));
1335 do_addresses (int bccque, int talk)
1339 struct mailname *lp;
1342 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1343 if (lp->m_bcc ? bccque : !bccque) {
1345 printf (" -- Local Recipients --\n");
1346 do_an_address (lp, talk);
1351 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1352 if (lp->m_bcc ? bccque : !bccque) {
1354 printf (" -- UUCP Recipients --\n");
1355 do_an_address (lp, talk);
1360 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1361 if (lp->m_bcc ? bccque : !bccque) {
1363 printf (" -- Network Recipients --\n");
1364 do_an_address (lp, talk);
1371 if (rp_isbad (retval = sm_waend ()))
1372 die (NULL, "problem ending addresses; %s", rp_string (retval));
1373 #endif /* SMTPMTS */
1378 * MTS-SPECIFIC INTERACTION
1383 * SENDMAIL/SMTP routines
1389 post (char *file, int bccque, int talk)
1394 onex = !(msgflags & MINV) || bccque;
1396 if (msgflags & MINV)
1397 printf (" -- Posting for %s Recipients --\n",
1398 bccque ? "Blind" : "Sighted");
1400 printf (" -- Posting for All Recipients --\n");
1405 if (rp_isbad (retval = sm_init (clientsw, serversw, watch, verbose,
1406 snoop, onex, queued, sasl, saslmech,
1408 || rp_isbad (retval = sm_winit (smtpmode, from)))
1409 die (NULL, "problem initializing server; %s", rp_string (retval));
1411 do_addresses (bccque, talk && verbose);
1412 if ((fd = open (file, O_RDONLY)) == NOTOK)
1413 die (file, "unable to re-open");
1418 sm_end (onex ? OK : DONE);
1422 if (msgflags & MINV)
1423 printf (" -- %s Recipient Copies Posted --\n",
1424 bccque ? "Blind" : "Sighted");
1426 printf (" -- Recipient Copies Posted --\n");
1433 /* Address Verification */
1436 verify_all_addresses (int talk)
1439 struct mailname *lp;
1443 if (!whomsw || checksw)
1444 if (rp_isbad (retval = sm_init (clientsw, serversw, watch, verbose, snoop, 0,
1445 queued, sasl, saslmech, user))
1446 || rp_isbad (retval = sm_winit (smtpmode, from)))
1447 die (NULL, "problem initializing server; %s", rp_string (retval));
1449 if (talk && !whomsw)
1450 printf (" -- Address Verification --\n");
1451 if (talk && localaddrs.m_next)
1452 printf (" -- Local Recipients --\n");
1453 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1454 do_an_address (lp, talk);
1456 if (talk && uuaddrs.m_next)
1457 printf (" -- UUCP Recipients --\n");
1458 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1459 do_an_address (lp, talk);
1461 if (talk && netaddrs.m_next)
1462 printf (" -- Network Recipients --\n");
1463 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1464 do_an_address (lp, talk);
1467 if (talk && !whomsw)
1468 printf (" -- Address Verification Successful --\n");
1470 if (!whomsw || checksw)
1479 do_an_address (struct mailname *lp, int talk)
1485 switch (lp->m_type) {
1489 strncpy (addr, mbox, sizeof(addr));
1493 mbox = auxformat (lp, 0);
1495 snprintf (addr, sizeof(addr), "%s!%s", lp->m_host, lp->m_mbox);
1498 default: /* let SendMail decide if the host is bad */
1501 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1506 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1508 if (whomsw && !checksw) {
1516 switch (retval = sm_wadr (mbox, host,
1517 lp->m_type != UUCPHOST ? lp->m_path : NULL)) {
1520 printf ("address ok\n");
1526 fprintf (stderr, " %s: ", addr);
1527 fprintf (talk ? stdout : stderr, "loses; %s\n",
1528 rp_string (retval));
1534 fprintf (stderr, " %s: ", addr);
1535 die (NULL, "unexpected response; %s", rp_string (retval));
1543 do_text (char *file, int fd)
1548 lseek (fd, (off_t) 0, SEEK_SET);
1550 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1551 if (rp_isbad (retval = sm_wtxt (buf, state)))
1552 die (NULL, "problem writing text; %s\n", rp_string (retval));
1556 die (file, "problem reading from");
1558 switch (retval = sm_wtend ()) {
1564 die (NULL, "posting failed; %s", rp_string (retval));
1567 die (NULL, "unexpected response; %s", rp_string (retval));
1571 #endif /* SMTPMTS */
1581 #ifndef RELIABLE_SIGNALS
1582 SIGNAL (i, SIG_IGN);
1586 if (msgflags & MINV)
1590 if (!whomsw || checksw)
1592 #endif /* SMTPMTS */
1604 hstat = SIGNAL2 (SIGHUP, sigser);
1605 istat = SIGNAL2 (SIGINT, sigser);
1606 qstat = SIGNAL2 (SIGQUIT, sigser);
1607 tstat = SIGNAL2 (SIGTERM, sigser);
1617 SIGNAL (SIGHUP, hstat);
1618 SIGNAL (SIGINT, istat);
1619 SIGNAL (SIGQUIT, qstat);
1620 SIGNAL (SIGTERM, tstat);
1628 p_refile (char *file)
1636 printf (" -- Filing Folder Copies --\n");
1637 for (i = 0; i < fccind; i++)
1638 fcc (file, fccfold[i]);
1640 printf (" -- Folder Copies Filed --\n");
1645 * Call the `fileproc' to add the file to the folder.
1649 fcc (char *file, char *folder)
1656 printf (" %sFcc %s: ", msgstate == RESENT ? "Resent-" : "", folder);
1659 for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
1665 fprintf (stderr, " %sFcc %s: ",
1666 msgstate == RESENT ? "Resent-" : "", folder);
1667 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
1671 /* see if we need to add `+' */
1672 snprintf (fold, sizeof(fold), "%s%s",
1673 *folder == '+' || *folder == '@' ? "" : "+", folder);
1675 /* now exec the fileproc */
1676 execlp (fileproc, r1bindex (fileproc, '/'),
1677 "-link", "-file", file, fold, NULL);
1681 if ((status = pidwait (child_id, OK))) {
1683 fprintf (stderr, " %sFcc %s: ",
1684 msgstate == RESENT ? "Resent-" : "", folder);
1685 pidstatus (status, verbose ? stdout : stderr, NULL);
1688 printf ("folder ok\n");
1700 die (char *what, char *fmt, ...)
1705 if (msgflags & MINV)
1709 if (!whomsw || checksw)
1711 #endif /* SMTPMTS */
1714 advertise (what, NULL, fmt, ap);