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 /* In the following array of structures, the numeric second field of the
52 structures (minchars) is apparently used like this:
54 -# : Switch can be abbreviated to # characters; switch hidden in -help.
55 0 : Switch can't be abbreviated; switch shown in -help.
56 # : Switch can be abbreviated to # characters; switch shown in -help. */
58 static struct swit switches[] = {
60 { "alias aliasfile", 0 },
62 { "check", -5 }, /* interface from whom */
64 { "nocheck", -7 }, /* interface from whom */
68 { "dist", -4 }, /* interface from dist */
70 { "filter filterfile", 0 },
78 { "library directory", -7 }, /* interface from send, whom */
96 { "whom", -4 }, /* interface from whom */
98 { "width columns", 0 },
103 #define BITSTUFFSW 22
104 { "dashstuffing", -12 }, /* should we dashstuff BCC messages? */
105 #define NBITSTUFFSW 23
106 { "nodashstuffing", -14 },
108 { "mail", -4 }, /* specify MAIL smtp mode */
110 { "saml", -4 }, /* specify SAML smtp mode */
112 { "send", -4 }, /* specify SEND smtp mode */
114 { "soml", -4 }, /* specify SOML smtp mode */
116 { "idanno number", -6 }, /* interface from send */
118 { "deliver address-list", -7 },
120 { "client host", -6 },
122 { "server host", -6 }, /* specify alternate SMTP server */
124 { "snoop", -5 }, /* snoop the SMTP transaction */
126 { "fill-in file", -7 },
134 { "sasl", SASLminc(-4) },
135 #define SASLMECHSW 38
136 { "saslmech", SASLminc(-5) },
138 { "user", SASLminc(-4) },
150 * flags for headers->flags
152 #define HNOP 0x0000 /* just used to keep .set around */
153 #define HBAD 0x0001 /* bad header - don't let it through */
154 #define HADR 0x0002 /* header has an address field */
155 #define HSUB 0x0004 /* Subject: header */
156 #define HTRY 0x0008 /* try to send to addrs on header */
157 #define HBCC 0x0010 /* don't output this header */
158 #define HMNG 0x0020 /* munge this header */
159 #define HNGR 0x0040 /* no groups allowed in this header */
160 #define HFCC 0x0080 /* FCC: type header */
161 #define HNIL 0x0100 /* okay for this header not to have addrs */
162 #define HIGN 0x0200 /* ignore this header */
163 #define HDCC 0x0400 /* another undocumented feature */
166 * flags for headers->set
168 #define MFRM 0x0001 /* we've seen a From: */
169 #define MDAT 0x0002 /* we've seen a Date: */
170 #define MRFM 0x0004 /* we've seen a Resent-From: */
171 #define MVIS 0x0008 /* we've seen sighted addrs */
172 #define MINV 0x0010 /* we've seen blind addrs */
175 static struct headers NHeaders[] = {
176 { "Return-Path", HBAD, 0 },
177 { "Received", HBAD, 0 },
178 { "Reply-To", HADR|HNGR, 0 },
179 { "From", HADR|HNGR, MFRM },
180 { "Sender", HADR|HBAD, 0 },
182 { "Subject", HSUB, 0 },
183 { "To", HADR|HTRY, MVIS },
184 { "cc", HADR|HTRY, MVIS },
185 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
186 { "Dcc", HADR|HTRY|HDCC|HNIL, MVIS }, /* sorta cc & bcc combined */
187 { "Message-ID", HBAD, 0 },
192 static struct headers RHeaders[] = {
193 { "Resent-Reply-To", HADR|HNGR, 0 },
194 { "Resent-From", HADR|HNGR, MRFM },
195 { "Resent-Sender", HADR|HBAD, 0 },
196 { "Resent-Date", HBAD, 0 },
197 { "Resent-Subject", HSUB, 0 },
198 { "Resent-To", HADR|HTRY, MVIS },
199 { "Resent-cc", HADR|HTRY, MVIS },
200 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
201 { "Resent-Message-ID", HBAD, 0 },
202 { "Resent-Fcc", HFCC, 0 },
203 { "Reply-To", HADR, 0 },
204 { "From", HADR|HNGR, MFRM },
205 { "Sender", HADR|HNGR, 0 },
206 { "Date", HNOP, MDAT },
207 { "To", HADR|HNIL, 0 },
208 { "cc", HADR|HNIL, 0 },
209 { "Bcc", HADR|HTRY|HBCC|HNIL, 0 },
214 static short fccind = 0; /* index into fccfold[] */
215 static short outputlinelen = OUTPUTLINELEN;
217 static int pfd = NOTOK; /* fd to write annotation list to */
218 static uid_t myuid= -1; /* my user id */
219 static gid_t mygid= -1; /* my group id */
220 static int recipients = 0; /* how many people will get a copy */
221 static int unkadr = 0; /* how many of those were unknown */
222 static int badadr = 0; /* number of bad addrs */
223 static int badmsg = 0; /* message has bad semantics */
224 static int verbose = 0; /* spell it out */
225 static int format = 1; /* format addresses */
226 static int mime = 0; /* use MIME-style encapsulations for Bcc */
227 static int msgid = 0; /* add msgid */
228 static int debug = 0; /* debugging post */
229 static int watch = 0; /* watch the delivery process */
230 static int whomsw = 0; /* we are whom not post */
231 static int checksw = 0; /* whom -check */
232 static int linepos=0; /* putadr()'s position on the line */
233 static int nameoutput=0; /* putadr() has output header name */
234 static int sasl=0; /* Use SASL auth for SMTP */
235 static char *saslmech=NULL; /* Force use of particular SASL mech */
236 static char *user=NULL; /* Authenticate as this user */
238 static unsigned msgflags = 0; /* what we've seen */
242 static int msgstate = NORMAL;
244 static time_t tclock = 0; /* the time we started (more or less) */
246 static SIGNAL_HANDLER hstat, istat, qstat, tstat;
248 static char tmpfil[BUFSIZ];
249 static char bccfil[BUFSIZ];
251 static char from[BUFSIZ]; /* my network address */
252 static char signature[BUFSIZ]; /* my signature */
253 static char *filter = NULL; /* the filter for BCC'ing */
254 static char *subject = NULL; /* the subject field for BCC'ing */
255 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
257 static struct headers *hdrtab; /* table for the message we're doing */
259 static struct mailname localaddrs={NULL}; /* local addrs */
260 static struct mailname netaddrs={NULL}; /* network addrs */
261 static struct mailname uuaddrs={NULL}; /* uucp addrs */
262 static struct mailname tmpaddrs={NULL}; /* temporary queue */
265 static int snoop = 0;
266 static int smtpmode = S_MAIL;
267 static char *clientsw = NULL;
268 static char *serversw = NULL;
270 extern struct smtp sm_reply;
273 static char prefix[] = "----- =_aaaaaaaaaa";
275 static int fill_up = 0;
276 static char *fill_in = NULL;
277 static char *partno = NULL;
278 static int queued = 0;
280 extern boolean draft_from_masquerading; /* defined in mts.c */
285 static void putfmt (char *, char *, FILE *);
286 static void start_headers (void);
287 static void finish_headers (FILE *);
288 static int get_header (char *, struct headers *);
289 static int putadr (char *, char *, struct mailname *, FILE *, unsigned int);
290 static void putgrp (char *, char *, FILE *, unsigned int);
291 static int insert (struct mailname *);
292 static void pl (void);
293 static void anno (void);
294 static int annoaux (struct mailname *);
295 static void insert_fcc (struct headers *, char *);
296 static void make_bcc_file (int);
297 static void verify_all_addresses (int);
298 static void chkadr (void);
299 static void sigon (void);
300 static void sigoff (void);
301 static void p_refile (char *);
302 static void fcc (char *, char *);
303 static void die (char *, char *, ...);
304 static void post (char *, int, int);
305 static void do_text (char *file, int fd);
306 static void do_an_address (struct mailname *, int);
307 static void do_addresses (int, int);
308 static int find_prefix (void);
312 main (int argc, char **argv)
314 int state, compnum, dashstuff = 0;
315 char *cp, *msg = NULL, **argp, **arguments;
316 char buf[BUFSIZ], name[NAMESZ];
320 setlocale(LC_ALL, "");
322 invo_name = r1bindex (argv[0], '/');
324 /* foil search of user profile/context */
325 if (context_foil (NULL) == -1)
328 mts_init (invo_name);
329 arguments = getarguments (invo_name, argc, argv, 0);
332 while ((cp = *argp++)) {
334 switch (smatch (++cp, switches)) {
336 ambigsw (cp, switches);
339 adios (NULL, "-%s unknown", cp);
342 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
343 print_help (buf, switches, 0);
346 print_version(invo_name);
350 if (!(cp = *argp++) || *cp == '-')
351 adios (NULL, "missing argument to %s", argp[-2]);
352 /* create a minimal context */
353 if (context_foil (cp) == -1)
358 if (!(cp = *argp++) || *cp == '-')
359 adios (NULL, "missing argument to %s", argp[-2]);
360 if ((state = alias (cp)) != AK_OK)
361 adios (NULL, "aliasing error in %s - %s",
362 cp, akerror (state));
381 if (!(filter = *argp++) || *filter == '-')
382 adios (NULL, "missing argument to %s", argp[-2]);
437 if (!(cp = *argp++) || *cp == '-')
438 adios (NULL, "missing argument to %s", argp[-2]);
439 if ((outputlinelen = atoi (cp)) < 10)
440 adios (NULL, "impossible width %d", outputlinelen);
444 if (!(cp = *argp++) || *cp == '-')
445 adios (NULL, "missing argument to %s", argp[-2]);
446 if ((pfd = atoi (cp)) <= 2)
447 adios (NULL, "bad argument %s %s", argp[-2], cp);
451 if (!(cp = *argp++) || *cp == '-')
452 adios (NULL, "missing argument to %s", argp[-2]);
458 if (!(cp = *argp++) || *cp == '-')
459 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]);
491 if (!(fill_in = *argp++) || *fill_in == '-')
492 adios (NULL, "missing argument to %s", argp[-2]);
498 if (!(partno = *argp++) || *partno == '-')
499 adios (NULL, "missing argument to %s", argp[-2]);
511 if (!(saslmech = *argp++) || *saslmech == '-')
512 adios (NULL, "missing argument to %s", argp[-2]);
516 if (!(user = *argp++) || *user == '-')
517 adios (NULL, "missing argument to %s", argp[-2]);
522 adios (NULL, "only one message at a time!");
530 adios (NULL, "usage: %s [switches] file", invo_name);
532 if (outputlinelen < 10)
533 adios (NULL, "impossible width %d", outputlinelen);
535 if ((in = fopen (msg, "r")) == NULL)
536 adios (msg, "unable to open");
541 discard (out = stdout); /* XXX: reference discard() to help loader */
544 if ((out = fopen (fill_in ? fill_in : "/dev/null", "w")) == NULL)
545 adios ("/dev/null", "unable to open");
547 strncpy (tmpfil, m_scratch ("", m_maildir (invo_name)),
549 if ((out = fopen (tmpfil, "w")) == NULL) {
550 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
551 if ((out = fopen (tmpfil, "w")) == NULL)
552 adios (tmpfil, "unable to create");
554 chmod (tmpfil, 0600);
558 hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
560 for (compnum = 1, state = FLD;;) {
561 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
566 cp = add (buf, NULL);
567 while (state == FLDPLUS) {
568 state = m_getfld (state, name, buf, sizeof(buf), in);
571 putfmt (name, cp, out);
575 finish_headers (out);
580 finish_headers (out);
581 if (whomsw && !fill_in)
583 fprintf (out, "\n%s", buf);
584 while (state == BODY) {
585 state = m_getfld (state, name, buf, sizeof(buf), in);
591 finish_headers (out);
596 adios (NULL, "message format error in component #%d", compnum);
599 adios (NULL, "getfld() returned %d", state);
615 /* If we are doing a "whom" check */
618 verify_all_addresses (1);
622 if (msgflags & MINV) {
623 make_bcc_file (dashstuff);
624 if (msgflags & MVIS) {
625 verify_all_addresses (verbose);
626 post (tmpfil, 0, verbose);
628 post (bccfil, 1, verbose);
631 post (tmpfil, 0, isatty (1));
638 printf (partno ? "Partial Message #%s Processed\n" : "Message Processed\n",
649 putfmt (char *name, char *str, FILE *out)
651 int count, grp, i, keep;
654 struct mailname *mp, *np;
657 while (*str == ' ' || *str == '\t')
660 if (msgstate == NORMAL && uprf (name, "resent")) {
661 advise (NULL, "illegal header line -- %s:", name);
666 if ((i = get_header (name, hdrtab)) == NOTOK) {
667 fprintf (out, "%s: %s", name, str);
672 if (hdr->flags & HIGN) {
674 fprintf (out, "%s: %s", name, str);
677 if (hdr->flags & HBAD) {
679 fprintf (out, "%s: %s", name, str);
681 advise (NULL, "illegal header line -- %s:", name);
686 msgflags |= (hdr->set & ~(MVIS | MINV));
688 if (hdr->flags & HSUB)
689 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
690 if (hdr->flags & HFCC) {
692 fprintf (out, "%s: %s", name, str);
696 if ((cp = strrchr(str, '\n')))
698 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
700 insert_fcc (hdr, pp);
702 insert_fcc (hdr, pp);
706 if (!(hdr->flags & HADR)) {
707 fprintf (out, "%s: %s", name, str);
711 tmpaddrs.m_next = NULL;
712 for (count = 0; (cp = getname (str)); count++)
713 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
717 tmpaddrs.m_next = mp;
721 if (hdr->flags & HTRY)
727 if (hdr->flags & HNIL)
728 fprintf (out, "%s: %s", name, str);
731 advise (NULL, "%s: field requires at least one address", name);
738 nameoutput = linepos = 0;
739 snprintf (namep, sizeof(namep), "%s%s",
740 !fill_in && (hdr->flags & HMNG) ? "Original-" : "", name);
742 for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
743 if (mp->m_nohost) { /* also used to test (hdr->flags & HTRY) */
744 /* The address doesn't include a host, so it might be an alias. */
745 pp = akvalue (mp->m_mbox); /* do mh alias substitution */
746 qp = akvisible () ? mp->m_mbox : "";
749 putgrp (namep, np->m_gname, out, hdr->flags);
750 while ((cp = getname (pp))) {
751 if (!(mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
756 if (draft_from_masquerading && ((msgstate == RESENT)
758 : (hdr->set & MFRM)))
759 /* The user manually specified a [Resent-]From: address in
760 their draft and the "masquerade:" line in mts.conf
761 doesn't contain "draft_from", so we'll set things up to
762 use the actual email address embedded in the draft
763 [Resent-]From: (after alias substitution, and without the
764 GECOS full name or angle brackets) as the envelope
766 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
768 if (hdr->flags & HBCC)
771 mp->m_ingrp = np->m_ingrp;
774 putgrp (namep, mp->m_gname, out, hdr->flags);
777 if (putadr (namep, qp, mp, out, hdr->flags))
778 msgflags |= (hdr->set & (MVIS | MINV));
787 /* Address includes a host, so no alias substitution is needed. */
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 putgrp (namep, mp->m_gname, out, hdr->flags);
806 keep = putadr (namep, "", mp, out, hdr->flags);
810 msgflags |= (hdr->set & (MVIS | MINV));
816 if (grp > 0 && (hdr->flags & HNGR)) {
817 advise (NULL, "%s: field does not allow groups", name);
821 if (fill_in && grp > 0)
832 char myhost[BUFSIZ], sigbuf[BUFSIZ];
839 strncpy (from, adrsprintf (NULL, NULL), sizeof(from));
840 strncpy (myhost, LocalName (), sizeof(myhost));
842 for (cp = myhost; *cp; cp++)
845 if ((cp = getfullname ()) && *cp) {
846 strncpy (sigbuf, cp, sizeof(sigbuf));
847 snprintf (signature, sizeof(signature), "%s <%s>",
848 sigbuf, adrsprintf (NULL, NULL));
849 if ((cp = getname (signature)) == NULL)
850 adios (NULL, "getname () failed -- you lose extraordinarily big");
851 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL)) == NULL)
852 adios (NULL, "bad signature '%s'", sigbuf);
857 strncpy (signature, adrsprintf (NULL, NULL), sizeof(signature));
863 * Now that we've outputted the header fields in the draft
864 * message, we will now output any remaining header fields
865 * that we need to add/create.
869 finish_headers (FILE *out)
873 if (whomsw && !fill_up)
876 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
878 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
879 (int) getpid (), (long) tclock, LocalName ());
880 if (msgflags & MFRM) {
881 /* There was already a From: in the draft. Don't add one. */
882 if (!draft_from_masquerading)
883 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
884 so we'll reveal the user's actual account@thismachine
885 address in a Sender: header (and use it as the envelope
887 fprintf (out, "Sender: %s\n", from);
890 /* Construct a From: header. */
891 fprintf (out, "From: %s\n", signature);
895 if (!(msgflags & MVIS))
896 fprintf (out, "Bcc: Blind Distribution List: ;\n");
900 if (!(msgflags & MDAT)) {
901 advise (NULL, "message has no Date: header");
904 if (!(msgflags & MFRM)) {
905 advise (NULL, "message has no From: header");
908 if (whomsw && !fill_up)
911 fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
913 fprintf (out, "Resent-Message-ID: <%d.%ld@%s>\n",
914 (int) getpid (), (long) tclock, LocalName ());
915 if (msgflags & MRFM) {
916 /* There was already a Resent-From: in draft. Don't add one. */
917 if (!draft_from_masquerading)
918 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
919 so we'll reveal the user's actual account@thismachine
920 address in a Sender: header (and use it as the envelope
922 fprintf (out, "Resent-Sender: %s\n", from);
925 /* Construct a Resent-From: header. */
926 fprintf (out, "Resent-From: %s\n", signature);
929 if (!(msgflags & MVIS))
930 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
935 adios (NULL, "re-format message and try again");
937 adios (NULL, "no addressees");
942 get_header (char *header, struct headers *table)
946 for (h = table; h->value; h++)
947 if (!strcasecmp (header, h->value))
955 putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flags)
961 if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
963 if ((!fill_in && (flags & (HBCC | HDCC))) || mp->m_ingrp)
967 fprintf (out, "%s: ", name);
968 linepos += (nameoutput = strlen (name) + 2);
971 if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
972 mp->m_pers = getcpy (aka);
974 if (mp->m_gname && !fill_in) {
975 snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
985 if (linepos != nameoutput) {
986 if (len + linepos + 2 > outputlinelen)
987 fprintf (out, ",\n%*s", linepos = nameoutput, "");
997 return (flags & HTRY);
1002 putgrp (char *name, char *group, FILE *out, unsigned int flags)
1007 if (!fill_in && (flags & HBCC))
1011 fprintf (out, "%s: ", name);
1012 linepos += (nameoutput = strlen (name) + 2);
1014 linepos -= strlen (group);
1017 cp = fill_in ? group : concat (group, ";", NULL);
1020 if (linepos > nameoutput) {
1021 if (len + linepos + 2 > outputlinelen) {
1022 fprintf (out, ",\n%*s", nameoutput, "");
1023 linepos = nameoutput;
1037 insert (struct mailname *np)
1039 struct mailname *mp;
1041 if (np->m_mbox == NULL)
1044 for (mp = np->m_type == LOCALHOST ? &localaddrs
1045 : np->m_type == UUCPHOST ? &uuaddrs
1049 if (!strcasecmp (np->m_host, mp->m_next->m_host)
1050 && !strcasecmp (np->m_mbox, mp->m_next->m_mbox)
1051 && np->m_bcc == mp->m_next->m_bcc)
1064 struct mailname *mp;
1066 printf ("-------\n\t-- Addresses --\nlocal:\t");
1067 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1068 printf ("%s%s%s", mp->m_mbox,
1069 mp->m_bcc ? "[BCC]" : "",
1070 mp->m_next ? ",\n\t" : "");
1072 printf ("\nnet:\t");
1073 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1074 printf ("%s%s@%s%s%s", mp->m_path ? mp->m_path : "",
1075 mp->m_mbox, mp->m_host,
1076 mp->m_bcc ? "[BCC]" : "",
1077 mp->m_next ? ",\n\t" : "");
1079 printf ("\nuucp:\t");
1080 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1081 printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1082 mp->m_bcc ? "[BCC]" : "",
1083 mp->m_next ? ",\n\t" : "");
1085 printf ("\n\t-- Folder Copies --\nfcc:\t");
1086 for (i = 0; i < fccind; i++)
1087 printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1095 struct mailname *mp;
1097 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1098 if (annoaux (mp) == NOTOK)
1101 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1102 if (annoaux (mp) == NOTOK)
1105 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1106 if (annoaux (mp) == NOTOK)
1116 annoaux (struct mailname *mp)
1119 char buffer[BUFSIZ];
1121 snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1122 i = strlen (buffer);
1124 return (write (pfd, buffer, i) == i ? OK : NOTOK);
1129 insert_fcc (struct headers *hdr, char *pp)
1133 for (cp = pp; isspace (*cp); cp++)
1135 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
1143 adios (NULL, "too many %ss", hdr->value);
1144 fccfold[fccind++] = getcpy (cp);
1152 make_bcc_file (int dashstuff)
1159 strncpy (bccfil, m_tmpfil ("bccs"), sizeof(bccfil));
1160 if ((out = fopen (bccfil, "w")) == NULL)
1161 adios (bccfil, "unable to create");
1162 chmod (bccfil, 0600);
1164 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1166 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
1167 (int) getpid (), (long) tclock, LocalName ());
1168 fprintf (out, "From: %s\n", signature);
1170 fprintf (out, "Subject: %s", subject);
1171 fprintf (out, "BCC:\n");
1174 * Use MIME encapsulation for Bcc messages
1180 * Check if any lines in the message clash with the
1181 * prefix for the MIME multipart separator. If there
1182 * is a clash, increment one of the letters in the
1183 * prefix and check again.
1185 if ((cp = strchr(prefix, 'a')) == NULL)
1186 adios (NULL, "lost prefix start");
1187 while (find_prefix () == NOTOK) {
1192 adios (NULL, "can't find a unique delimiter string");
1197 fprintf (out, "%s: %s\n%s: multipart/digest; boundary=\"",
1198 VRSN_FIELD, VRSN_VALUE, TYPE_FIELD);
1199 fprintf (out, "%s\"\n\n--%s\n\n", prefix, prefix);
1201 fprintf (out, "\n------- Blind-Carbon-Copy\n\n");
1207 * Do mhl filtering of Bcc messages instead
1208 * of MIME encapsulation.
1210 if (filter != NULL) {
1211 vec[0] = r1bindex (mhlproc, '/');
1213 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1217 adios ("fork", "unable to");
1220 dup2 (fileno (out), 1);
1223 vec[i++] = "-forward";
1228 /* was the flag -[no]dashstuffing specified? */
1230 vec[i++] = "-dashstuffing";
1231 else if (dashstuff < 0)
1232 vec[i++] = "-nodashstuffing";
1235 execvp (mhlproc, vec);
1236 fprintf (stderr, "unable to exec ");
1241 pidXwait (child_id, mhlproc);
1245 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
1246 adios (tmpfil, "unable to re-open");
1249 * If using MIME encapsulation, or if the -nodashstuffing
1250 * flag was given, then just copy message. Else do
1251 * RFC934 quoting (dashstuffing).
1253 if (mime || dashstuff < 0)
1254 cpydata (fd, fileno (out), tmpfil, bccfil);
1256 cpydgst (fd, fileno (out), tmpfil, bccfil);
1260 fseek (out, 0L, SEEK_END);
1262 fprintf (out, "\n--%s--\n", prefix);
1264 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
1270 * Scan message to check if any lines clash with
1271 * the prefix of the MIME multipart separator.
1278 char buffer[BUFSIZ];
1281 if ((in = fopen (tmpfil, "r")) == NULL)
1282 adios (tmpfil, "unable to re-open");
1284 len = strlen (prefix);
1287 while (fgets (buffer, sizeof(buffer) - 1, in))
1288 if (buffer[0] == '-' && buffer[1] == '-') {
1291 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1295 if (strcmp (buffer + 2, prefix) == 0) {
1306 #define plural(x) (x == 1 ? "" : "s")
1311 if (badadr && unkadr)
1312 die (NULL, "%d address%s unparsable, %d addressee%s undeliverable",
1313 badadr, plural (badadr), unkadr, plural (badadr));
1315 die (NULL, "%d address%s unparsable", badadr, plural (badadr));
1317 die (NULL, "%d addressee%s undeliverable", unkadr, plural (unkadr));
1322 do_addresses (int bccque, int talk)
1326 struct mailname *lp;
1329 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1330 if (lp->m_bcc ? bccque : !bccque) {
1332 printf (" -- Local Recipients --\n");
1333 do_an_address (lp, talk);
1338 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1339 if (lp->m_bcc ? bccque : !bccque) {
1341 printf (" -- UUCP Recipients --\n");
1342 do_an_address (lp, talk);
1347 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1348 if (lp->m_bcc ? bccque : !bccque) {
1350 printf (" -- Network Recipients --\n");
1351 do_an_address (lp, talk);
1358 if (rp_isbad (retval = sm_waend ()))
1359 die (NULL, "problem ending addresses; %s", rp_string (retval));
1360 #endif /* SMTPMTS */
1365 * MTS-SPECIFIC INTERACTION
1370 * SENDMAIL/SMTP routines
1376 post (char *file, int bccque, int talk)
1381 onex = !(msgflags & MINV) || bccque;
1383 if (msgflags & MINV)
1384 printf (" -- Posting for %s Recipients --\n",
1385 bccque ? "Blind" : "Sighted");
1387 printf (" -- Posting for All Recipients --\n");
1392 if (rp_isbad (retval = sm_init (clientsw, serversw, watch, verbose,
1393 snoop, onex, queued, sasl, saslmech,
1395 || rp_isbad (retval = sm_winit (smtpmode, from)))
1396 die (NULL, "problem initializing server; %s", rp_string (retval));
1398 do_addresses (bccque, talk && verbose);
1399 if ((fd = open (file, O_RDONLY)) == NOTOK)
1400 die (file, "unable to re-open");
1405 sm_end (onex ? OK : DONE);
1409 if (msgflags & MINV)
1410 printf (" -- %s Recipient Copies Posted --\n",
1411 bccque ? "Blind" : "Sighted");
1413 printf (" -- Recipient Copies Posted --\n");
1420 /* Address Verification */
1423 verify_all_addresses (int talk)
1426 struct mailname *lp;
1430 if (!whomsw || checksw)
1431 if (rp_isbad (retval = sm_init (clientsw, serversw, watch, verbose, snoop, 0,
1432 queued, sasl, saslmech, user))
1433 || rp_isbad (retval = sm_winit (smtpmode, from)))
1434 die (NULL, "problem initializing server; %s", rp_string (retval));
1436 if (talk && !whomsw)
1437 printf (" -- Address Verification --\n");
1438 if (talk && localaddrs.m_next)
1439 printf (" -- Local Recipients --\n");
1440 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1441 do_an_address (lp, talk);
1443 if (talk && uuaddrs.m_next)
1444 printf (" -- UUCP Recipients --\n");
1445 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1446 do_an_address (lp, talk);
1448 if (talk && netaddrs.m_next)
1449 printf (" -- Network Recipients --\n");
1450 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1451 do_an_address (lp, talk);
1454 if (talk && !whomsw)
1455 printf (" -- Address Verification Successful --\n");
1457 if (!whomsw || checksw)
1466 do_an_address (struct mailname *lp, int talk)
1472 switch (lp->m_type) {
1476 strncpy (addr, mbox, sizeof(addr));
1480 mbox = auxformat (lp, 0);
1482 snprintf (addr, sizeof(addr), "%s!%s", lp->m_host, lp->m_mbox);
1485 default: /* let SendMail decide if the host is bad */
1488 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1493 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1495 if (whomsw && !checksw) {
1503 switch (retval = sm_wadr (mbox, host,
1504 lp->m_type != UUCPHOST ? lp->m_path : NULL)) {
1507 printf ("address ok\n");
1513 fprintf (stderr, " %s: ", addr);
1514 fprintf (talk ? stdout : stderr, "loses; %s\n",
1515 rp_string (retval));
1521 fprintf (stderr, " %s: ", addr);
1522 die (NULL, "unexpected response; %s", rp_string (retval));
1530 do_text (char *file, int fd)
1535 lseek (fd, (off_t) 0, SEEK_SET);
1537 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1538 if (rp_isbad (retval = sm_wtxt (buf, state)))
1539 die (NULL, "problem writing text; %s\n", rp_string (retval));
1543 die (file, "problem reading from");
1545 switch (retval = sm_wtend ()) {
1551 die (NULL, "posting failed; %s", rp_string (retval));
1554 die (NULL, "unexpected response; %s", rp_string (retval));
1558 #endif /* SMTPMTS */
1568 #ifndef RELIABLE_SIGNALS
1569 SIGNAL (i, SIG_IGN);
1573 if (msgflags & MINV)
1577 if (!whomsw || checksw)
1579 #endif /* SMTPMTS */
1591 hstat = SIGNAL2 (SIGHUP, sigser);
1592 istat = SIGNAL2 (SIGINT, sigser);
1593 qstat = SIGNAL2 (SIGQUIT, sigser);
1594 tstat = SIGNAL2 (SIGTERM, sigser);
1604 SIGNAL (SIGHUP, hstat);
1605 SIGNAL (SIGINT, istat);
1606 SIGNAL (SIGQUIT, qstat);
1607 SIGNAL (SIGTERM, tstat);
1615 p_refile (char *file)
1623 printf (" -- Filing Folder Copies --\n");
1624 for (i = 0; i < fccind; i++)
1625 fcc (file, fccfold[i]);
1627 printf (" -- Folder Copies Filed --\n");
1632 * Call the `fileproc' to add the file to the folder.
1636 fcc (char *file, char *folder)
1643 printf (" %sFcc %s: ", msgstate == RESENT ? "Resent-" : "", folder);
1646 for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
1652 fprintf (stderr, " %sFcc %s: ",
1653 msgstate == RESENT ? "Resent-" : "", folder);
1654 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
1658 /* see if we need to add `+' */
1659 snprintf (fold, sizeof(fold), "%s%s",
1660 *folder == '+' || *folder == '@' ? "" : "+", folder);
1662 /* now exec the fileproc */
1663 execlp (fileproc, r1bindex (fileproc, '/'),
1664 "-link", "-file", file, fold, NULL);
1668 if ((status = pidwait (child_id, OK))) {
1670 fprintf (stderr, " %sFcc %s: ",
1671 msgstate == RESENT ? "Resent-" : "", folder);
1672 pidstatus (status, verbose ? stdout : stderr, NULL);
1675 printf ("folder ok\n");
1687 die (char *what, char *fmt, ...)
1692 if (msgflags & MINV)
1696 if (!whomsw || checksw)
1698 #endif /* SMTPMTS */
1701 advertise (what, NULL, fmt, ap);