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 { "idanno number", -6 }, /* interface from send */
107 { "client host", -6 },
109 { "server host", 6 }, /* specify alternate SMTP server */
111 { "snoop", -5 }, /* snoop the SMTP transaction */
117 { "sasl", SASLminc(-4) },
119 { "nosasl", SASLminc(-6) },
120 #define SASLMXSSFSW 32
121 { "saslmaxssf", SASLminc(-10) },
122 #define SASLMECHSW 33
123 { "saslmech", SASLminc(-5) },
125 { "user", SASLminc(-4) },
127 { "port server port name/number", 4 },
129 { "tls", TLSminc(-3) },
130 #define FILEPROCSW 37
145 * flags for headers->flags
147 #define HNOP 0x0000 /* just used to keep .set around */
148 #define HBAD 0x0001 /* bad header - don't let it through */
149 #define HADR 0x0002 /* header has an address field */
150 #define HSUB 0x0004 /* Subject: header */
151 #define HTRY 0x0008 /* try to send to addrs on header */
152 #define HBCC 0x0010 /* don't output this header */
153 #define HMNG 0x0020 /* munge this header */
154 #define HNGR 0x0040 /* no groups allowed in this header */
155 #define HFCC 0x0080 /* FCC: type header */
156 #define HNIL 0x0100 /* okay for this header not to have addrs */
157 #define HIGN 0x0200 /* ignore this header */
158 #define HDCC 0x0400 /* another undocumented feature */
161 * flags for headers->set
163 #define MFRM 0x0001 /* we've seen a From: */
164 #define MDAT 0x0002 /* we've seen a Date: */
165 #define MRFM 0x0004 /* we've seen a Resent-From: */
166 #define MVIS 0x0008 /* we've seen sighted addrs */
167 #define MINV 0x0010 /* we've seen blind addrs */
170 static struct headers NHeaders[] = {
171 { "Return-Path", HBAD, 0 },
172 { "Received", HBAD, 0 },
173 { "Reply-To", HADR|HNGR, 0 },
174 { "From", HADR|HNGR, MFRM },
175 { "Sender", HADR|HBAD, 0 },
177 { "Subject", HSUB, 0 },
178 { "To", HADR|HTRY, MVIS },
179 { "cc", HADR|HTRY, MVIS },
180 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
181 { "Dcc", HADR|HTRY|HDCC|HNIL, MVIS }, /* sorta cc & bcc combined */
182 { "Message-ID", HBAD, 0 },
187 static struct headers RHeaders[] = {
188 { "Resent-Reply-To", HADR|HNGR, 0 },
189 { "Resent-From", HADR|HNGR, MRFM },
190 { "Resent-Sender", HADR|HBAD, 0 },
191 { "Resent-Date", HBAD, 0 },
192 { "Resent-Subject", HSUB, 0 },
193 { "Resent-To", HADR|HTRY, MVIS },
194 { "Resent-cc", HADR|HTRY, MVIS },
195 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
196 { "Resent-Message-ID", HBAD, 0 },
197 { "Resent-Fcc", HFCC, 0 },
198 { "Reply-To", HADR, 0 },
199 { "From", HADR|HNGR, MFRM },
200 { "Sender", HADR|HNGR, 0 },
201 { "Date", HNOP, MDAT },
202 { "To", HADR|HNIL, 0 },
203 { "cc", HADR|HNIL, 0 },
204 { "Bcc", HADR|HTRY|HBCC|HNIL, 0 },
209 static short fccind = 0; /* index into fccfold[] */
210 static short outputlinelen = OUTPUTLINELEN;
212 static int pfd = NOTOK; /* fd to write annotation list to */
213 static uid_t myuid= -1; /* my user id */
214 static gid_t mygid= -1; /* my group id */
215 static int recipients = 0; /* how many people will get a copy */
216 static int unkadr = 0; /* how many of those were unknown */
217 static int badadr = 0; /* number of bad addrs */
218 static int badmsg = 0; /* message has bad semantics */
219 static int verbose = 0; /* spell it out */
220 static int format = 1; /* format addresses */
221 static int mime = 0; /* use MIME-style encapsulations for Bcc */
222 static int msgid = 0; /* add msgid */
223 static int debug = 0; /* debugging post */
224 static int watch = 0; /* watch the delivery process */
225 static int whomsw = 0; /* we are whom not post */
226 static int checksw = 0; /* whom -check */
227 static int linepos=0; /* putadr()'s position on the line */
228 static int nameoutput=0; /* putadr() has output header name */
229 static int sasl=0; /* Use SASL auth for SMTP */
230 static int saslssf=-1; /* Our maximum SSF for SASL */
231 static char *saslmech=NULL; /* Force use of particular SASL mech */
232 static char *user=NULL; /* Authenticate as this user */
233 static char *port="smtp"; /* Name of server port for SMTP */
234 static int tls=0; /* Use TLS for encryption */
236 static unsigned msgflags = 0; /* what we've seen */
240 static int msgstate = NORMAL;
242 static time_t tclock = 0; /* the time we started (more or less) */
244 static SIGNAL_HANDLER hstat, istat, qstat, tstat;
246 static char tmpfil[BUFSIZ];
247 static char bccfil[BUFSIZ];
249 static char from[BUFSIZ]; /* my network address */
250 static char signature[BUFSIZ]; /* my signature */
251 static char *filter = NULL; /* the filter for BCC'ing */
252 static char *subject = NULL; /* the subject field for BCC'ing */
253 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
255 static struct headers *hdrtab; /* table for the message we're doing */
257 static struct mailname localaddrs; /* local addrs */
258 static struct mailname netaddrs; /* network addrs */
259 static struct mailname uuaddrs; /* uucp addrs */
260 static struct mailname tmpaddrs; /* temporary queue */
262 static int snoop = 0;
263 static char *clientsw = NULL;
264 static char *serversw = NULL;
266 extern struct smtp sm_reply;
268 static char prefix[] = "----- =_aaaaaaaaaa";
270 static char *partno = NULL;
271 static int queued = 0;
273 extern boolean draft_from_masquerading; /* defined in mts.c */
278 static void putfmt (char *, char *, FILE *);
279 static void start_headers (void);
280 static void finish_headers (FILE *);
281 static int get_header (char *, struct headers *);
282 static int putadr (char *, char *, struct mailname *, FILE *, unsigned int);
283 static void putgrp (char *, char *, FILE *, unsigned int);
284 static int insert (struct mailname *);
285 static void pl (void);
286 static void anno (void);
287 static int annoaux (struct mailname *);
288 static void insert_fcc (struct headers *, unsigned char *);
289 static void make_bcc_file (int);
290 static void verify_all_addresses (int);
291 static void chkadr (void);
292 static void sigon (void);
293 static void sigoff (void);
294 static void p_refile (char *);
295 static void fcc (char *, char *);
296 static void die (char *, char *, ...);
297 static void post (char *, int, int);
298 static void do_text (char *file, int fd);
299 static void do_an_address (struct mailname *, int);
300 static void do_addresses (int, int);
301 static int find_prefix (void);
305 main (int argc, char **argv)
307 int state, compnum, dashstuff = 0;
308 char *cp, *msg = NULL, **argp, **arguments;
309 char buf[BUFSIZ], name[NAMESZ];
313 setlocale(LC_ALL, "");
315 invo_name = r1bindex (argv[0], '/');
317 /* foil search of user profile/context */
318 if (context_foil (NULL) == -1)
321 mts_init (invo_name);
322 arguments = getarguments (invo_name, argc, argv, 0);
325 while ((cp = *argp++)) {
327 switch (smatch (++cp, switches)) {
329 ambigsw (cp, switches);
332 adios (NULL, "-%s unknown", cp);
335 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
336 print_help (buf, switches, 0);
339 print_version(invo_name);
343 if (!(cp = *argp++) || *cp == '-')
344 adios (NULL, "missing argument to %s", argp[-2]);
345 /* create a minimal context */
346 if (context_foil (cp) == -1)
351 if (!(cp = *argp++) || *cp == '-')
352 adios (NULL, "missing argument to %s", argp[-2]);
353 if ((state = alias (cp)) != AK_OK)
354 adios (NULL, "aliasing error in %s - %s",
355 cp, akerror (state));
374 if (!(filter = *argp++) || *filter == '-')
375 adios (NULL, "missing argument to %s", argp[-2]);
430 if (!(cp = *argp++) || *cp == '-')
431 adios (NULL, "missing argument to %s", argp[-2]);
432 if ((outputlinelen = atoi (cp)) < 10)
433 adios (NULL, "impossible width %d", outputlinelen);
437 if (!(cp = *argp++) || *cp == '-')
438 adios (NULL, "missing argument to %s", argp[-2]);
439 if ((pfd = atoi (cp)) <= 2)
440 adios (NULL, "bad argument %s %s", argp[-2], cp);
444 if (!(clientsw = *argp++) || *clientsw == '-')
445 adios (NULL, "missing argument to %s", argp[-2]);
448 if (!(serversw = *argp++) || *serversw == '-')
449 adios (NULL, "missing argument to %s", argp[-2]);
456 if (!(partno = *argp++) || *partno == '-')
457 adios (NULL, "missing argument to %s", argp[-2]);
473 if (!(cp = *argp++) || *cp == '-')
474 adios (NULL, "missing argument to %s", argp[-2]);
479 if (!(saslmech = *argp++) || *saslmech == '-')
480 adios (NULL, "missing argument to %s", argp[-2]);
484 if (!(user = *argp++) || *user == '-')
485 adios (NULL, "missing argument to %s", argp[-2]);
489 if (!(port = *argp++) || *port == '-')
490 adios (NULL, "missing argument to %s", argp[-2]);
498 if (!(cp = *argp++) || *cp == '-')
499 adios (NULL, "missing argument to %s", argp[-2]);
504 if (!(cp = *argp++) || *cp == '-')
505 adios (NULL, "missing argument to %s", argp[-2]);
511 adios (NULL, "only one message at a time!");
519 adios (NULL, "usage: %s [switches] file", invo_name);
521 if (outputlinelen < 10)
522 adios (NULL, "impossible width %d", outputlinelen);
524 if ((in = fopen (msg, "r")) == NULL)
525 adios (msg, "unable to open");
530 discard (out = stdout); /* XXX: reference discard() to help loader */
533 if ((out = fopen ("/dev/null", "w")) == NULL)
534 adios ("/dev/null", "unable to open");
536 char *cp = m_mktemp(m_maildir(invo_name), NULL, &out);
538 cp = m_mktemp2(NULL, invo_name, NULL, &out);
540 adios ("post", "unable to create temporary file");
543 strncpy(tmpfil, cp, sizeof(tmpfil));
544 chmod (tmpfil, 0600);
548 hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
550 for (compnum = 1, state = FLD;;) {
551 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
556 cp = add (buf, NULL);
557 while (state == FLDPLUS) {
558 state = m_getfld (state, name, buf, sizeof(buf), in);
561 putfmt (name, cp, out);
565 finish_headers (out);
570 finish_headers (out);
573 fprintf (out, "\n%s", buf);
574 while (state == BODY) {
575 state = m_getfld (state, name, buf, sizeof(buf), in);
581 finish_headers (out);
586 adios (NULL, "message format error in component #%d", compnum);
589 adios (NULL, "getfld() returned %d", state);
605 /* If we are doing a "whom" check */
607 verify_all_addresses (1);
611 if (msgflags & MINV) {
612 make_bcc_file (dashstuff);
613 if (msgflags & MVIS) {
614 verify_all_addresses (verbose);
615 post (tmpfil, 0, verbose);
617 post (bccfil, 1, verbose);
620 post (tmpfil, 0, isatty (1));
627 printf (partno ? "Partial Message #%s Processed\n" : "Message Processed\n",
639 putfmt (char *name, char *str, FILE *out)
641 int count, grp, i, keep;
644 struct mailname *mp = NULL, *np = NULL;
647 while (*str == ' ' || *str == '\t')
650 if (msgstate == NORMAL && uprf (name, "resent")) {
651 advise (NULL, "illegal header line -- %s:", name);
656 if ((i = get_header (name, hdrtab)) == NOTOK) {
657 fprintf (out, "%s: %s", name, str);
662 if (hdr->flags & HIGN) {
665 if (hdr->flags & HBAD) {
666 advise (NULL, "illegal header line -- %s:", name);
670 msgflags |= (hdr->set & ~(MVIS | MINV));
672 if (hdr->flags & HSUB)
673 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
674 if (hdr->flags & HFCC) {
675 if ((cp = strrchr(str, '\n')))
677 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
679 insert_fcc (hdr, pp);
681 insert_fcc (hdr, pp);
685 if (!(hdr->flags & HADR)) {
686 fprintf (out, "%s: %s", name, str);
690 tmpaddrs.m_next = NULL;
691 for (count = 0; (cp = getname (str)); count++)
692 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
696 tmpaddrs.m_next = mp;
700 if (hdr->flags & HTRY)
706 if (hdr->flags & HNIL)
707 fprintf (out, "%s: %s", name, str);
710 advise (NULL, "%s: field requires at least one address", name);
717 nameoutput = linepos = 0;
718 snprintf (namep, sizeof(namep), "%s%s",
719 (hdr->flags & HMNG) ? "Original-" : "", name);
721 for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
722 if (mp->m_nohost) { /* also used to test (hdr->flags & HTRY) */
723 /* The address doesn't include a host, so it might be an alias. */
724 pp = akvalue (mp->m_mbox); /* do mh alias substitution */
725 qp = akvisible () ? mp->m_mbox : "";
728 putgrp (namep, np->m_gname, out, hdr->flags);
729 while ((cp = getname (pp))) {
730 if (!(mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
735 if (draft_from_masquerading && ((msgstate == RESENT)
737 : (hdr->set & MFRM)))
738 /* The user manually specified a [Resent-]From: address in
739 their draft and the "masquerade:" line in mts.conf
740 doesn't contain "draft_from", so we'll set things up to
741 use the actual email address embedded in the draft
742 [Resent-]From: (after alias substitution, and without the
743 GECOS full name or angle brackets) as the envelope
745 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
747 if (hdr->flags & HBCC)
750 mp->m_ingrp = np->m_ingrp;
753 putgrp (namep, mp->m_gname, out, hdr->flags);
756 if (putadr (namep, qp, mp, out, hdr->flags))
757 msgflags |= (hdr->set & (MVIS | MINV));
766 /* Address includes a host, so no alias substitution is needed. */
767 if (draft_from_masquerading && ((msgstate == RESENT)
769 : (hdr->set & MFRM)))
770 /* The user manually specified a [Resent-]From: address in
771 their draft and the "masquerade:" line in mts.conf
772 doesn't contain "draft_from", so we'll set things up to
773 use the actual email address embedded in the draft
774 [Resent-]From: (after alias substitution, and without the
775 GECOS full name or angle brackets) as the envelope
777 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
779 if (hdr->flags & HBCC)
782 putgrp (namep, mp->m_gname, out, hdr->flags);
785 keep = putadr (namep, "", mp, out, hdr->flags);
789 msgflags |= (hdr->set & (MVIS | MINV));
795 if (grp > 0 && (hdr->flags & HNGR)) {
796 advise (NULL, "%s: field does not allow groups", name);
809 char myhost[BUFSIZ], sigbuf[BUFSIZ];
816 strncpy (from, getlocaladdr(), sizeof(from));
817 strncpy (myhost, LocalName (0), sizeof(myhost));
819 for (cp = myhost; *cp; cp++)
822 if ((cp = getfullname ()) && *cp) {
823 strncpy (sigbuf, cp, sizeof(sigbuf));
824 snprintf (signature, sizeof(signature), "%s <%s>",
825 sigbuf, getlocaladdr());
826 if ((cp = getname (signature)) == NULL)
827 adios (NULL, "getname () failed -- you lose extraordinarily big");
828 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL)) == NULL)
829 adios (NULL, "bad signature '%s'", sigbuf);
834 strncpy (signature, getlocaladdr(), sizeof(signature));
840 * Now that we've outputted the header fields in the draft
841 * message, we will now output any remaining header fields
842 * that we need to add/create.
846 finish_headers (FILE *out)
853 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
855 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
856 (int) getpid (), (long) tclock, LocalName (1));
857 if (msgflags & MFRM) {
858 /* There was already a From: in the draft. Don't add one. */
859 if (!draft_from_masquerading)
860 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
861 so we'll reveal the user's actual account@thismachine
862 address in a Sender: header (and use it as the envelope
864 fprintf (out, "Sender: %s\n", from);
867 /* Construct a From: header. */
868 fprintf (out, "From: %s\n", signature);
872 if (!(msgflags & MVIS))
873 fprintf (out, "Bcc: Blind Distribution List: ;\n");
877 if (!(msgflags & MDAT)) {
878 advise (NULL, "message has no Date: header");
881 if (!(msgflags & MFRM)) {
882 advise (NULL, "message has no From: header");
888 fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
890 fprintf (out, "Resent-Message-ID: <%d.%ld@%s>\n",
891 (int) getpid (), (long) tclock, LocalName (1));
892 if (msgflags & MRFM) {
893 /* There was already a Resent-From: in 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, "Resent-Sender: %s\n", from);
902 /* Construct a Resent-From: header. */
903 fprintf (out, "Resent-From: %s\n", signature);
906 if (!(msgflags & MVIS))
907 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
912 adios (NULL, "re-format message and try again");
914 adios (NULL, "no addressees");
919 get_header (char *header, struct headers *table)
923 for (h = table; h->value; h++)
924 if (!mh_strcasecmp (header, h->value))
932 putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flags)
938 if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
940 if ((flags & (HBCC | HDCC)) || mp->m_ingrp)
944 fprintf (out, "%s: ", name);
945 linepos += (nameoutput = strlen (name) + 2);
948 if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
949 mp->m_pers = getcpy (aka);
952 snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
962 if (linepos != nameoutput) {
963 if (len + linepos + 2 > outputlinelen)
964 fprintf (out, ",\n%*s", linepos = nameoutput, "");
974 return (flags & HTRY);
979 putgrp (char *name, char *group, FILE *out, unsigned int flags)
988 fprintf (out, "%s: ", name);
989 linepos += (nameoutput = strlen (name) + 2);
992 cp = concat (group, ";", NULL);
995 if (linepos > nameoutput) {
996 if (len + linepos + 2 > outputlinelen) {
997 fprintf (out, ",\n%*s", nameoutput, "");
998 linepos = nameoutput;
1012 insert (struct mailname *np)
1014 struct mailname *mp;
1016 if (np->m_mbox == NULL)
1019 for (mp = np->m_type == LOCALHOST ? &localaddrs
1020 : np->m_type == UUCPHOST ? &uuaddrs
1024 if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
1025 && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox)
1026 && np->m_bcc == mp->m_next->m_bcc)
1039 struct mailname *mp;
1041 printf ("-------\n\t-- Addresses --\nlocal:\t");
1042 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1043 printf ("%s%s%s", mp->m_mbox,
1044 mp->m_bcc ? "[BCC]" : "",
1045 mp->m_next ? ",\n\t" : "");
1047 printf ("\nnet:\t");
1048 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1049 printf ("%s%s@%s%s%s", mp->m_path ? mp->m_path : "",
1050 mp->m_mbox, mp->m_host,
1051 mp->m_bcc ? "[BCC]" : "",
1052 mp->m_next ? ",\n\t" : "");
1054 printf ("\nuucp:\t");
1055 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1056 printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1057 mp->m_bcc ? "[BCC]" : "",
1058 mp->m_next ? ",\n\t" : "");
1060 printf ("\n\t-- Folder Copies --\nfcc:\t");
1061 for (i = 0; i < fccind; i++)
1062 printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1070 struct mailname *mp;
1072 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1073 if (annoaux (mp) == NOTOK)
1076 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1077 if (annoaux (mp) == NOTOK)
1080 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1081 if (annoaux (mp) == NOTOK)
1091 annoaux (struct mailname *mp)
1094 char buffer[BUFSIZ];
1096 snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1097 i = strlen (buffer);
1099 return (write (pfd, buffer, i) == i ? OK : NOTOK);
1104 insert_fcc (struct headers *hdr, unsigned char *pp)
1108 for (cp = pp; isspace (*cp); cp++)
1110 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
1118 adios (NULL, "too many %ss", hdr->value);
1119 fccfold[fccind++] = getcpy (cp);
1127 make_bcc_file (int dashstuff)
1135 tfile = m_mktemp2(NULL, "bccs", NULL, &out);
1136 if (tfile == NULL) adios("bcc", "unable to create temporary file");
1137 chmod (bccfil, 0600);
1138 strncpy (bccfil, tfile, sizeof(bccfil));
1140 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1142 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
1143 (int) getpid (), (long) tclock, LocalName (1));
1144 if (msgflags & MFRM) {
1145 /* There was already a From: in the draft. Don't add one. */
1146 if (!draft_from_masquerading)
1147 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
1148 so we'll reveal the user's actual account@thismachine
1149 address in a Sender: header (and use it as the envelope
1151 fprintf (out, "Sender: %s\n", from);
1154 /* Construct a From: header. */
1155 fprintf (out, "From: %s\n", signature);
1157 fprintf (out, "Subject: %s", subject);
1158 fprintf (out, "BCC:\n");
1161 * Use MIME encapsulation for Bcc messages
1167 * Check if any lines in the message clash with the
1168 * prefix for the MIME multipart separator. If there
1169 * is a clash, increment one of the letters in the
1170 * prefix and check again.
1172 if ((cp = strchr(prefix, 'a')) == NULL)
1173 adios (NULL, "lost prefix start");
1174 while (find_prefix () == NOTOK) {
1179 adios (NULL, "can't find a unique delimiter string");
1184 fprintf (out, "%s: %s\n%s: multipart/digest; boundary=\"",
1185 VRSN_FIELD, VRSN_VALUE, TYPE_FIELD);
1186 fprintf (out, "%s\"\n\n--%s\n\n", prefix, prefix);
1188 fprintf (out, "\n------- Blind-Carbon-Copy\n\n");
1194 * Do mhl filtering of Bcc messages instead
1195 * of MIME encapsulation.
1197 if (filter != NULL) {
1198 vec[0] = r1bindex (mhlproc, '/');
1200 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1204 adios ("fork", "unable to");
1207 dup2 (fileno (out), 1);
1210 vec[i++] = "-forward";
1215 /* was the flag -[no]dashstuffing specified? */
1217 vec[i++] = "-dashstuffing";
1218 else if (dashstuff < 0)
1219 vec[i++] = "-nodashstuffing";
1222 execvp (mhlproc, vec);
1223 fprintf (stderr, "unable to exec ");
1228 pidXwait (child_id, mhlproc);
1232 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
1233 adios (tmpfil, "unable to re-open");
1236 * If using MIME encapsulation, or if the -nodashstuffing
1237 * flag was given, then just copy message. Else do
1238 * RFC934 quoting (dashstuffing).
1240 if (mime || dashstuff < 0)
1241 cpydata (fd, fileno (out), tmpfil, bccfil);
1243 cpydgst (fd, fileno (out), tmpfil, bccfil);
1247 fseek (out, 0L, SEEK_END);
1249 fprintf (out, "\n--%s--\n", prefix);
1251 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
1257 * Scan message to check if any lines clash with
1258 * the prefix of the MIME multipart separator.
1265 unsigned char buffer[BUFSIZ];
1268 if ((in = fopen (tmpfil, "r")) == NULL)
1269 adios (tmpfil, "unable to re-open");
1271 while (fgets (buffer, sizeof(buffer) - 1, in))
1272 if (buffer[0] == '-' && buffer[1] == '-') {
1275 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1279 if (strcmp (buffer + 2, prefix) == 0) {
1290 #define plural(x) (x == 1 ? "" : "s")
1295 if (badadr && unkadr)
1296 die (NULL, "%d address%s unparsable, %d addressee%s undeliverable",
1297 badadr, plural (badadr), unkadr, plural (badadr));
1299 die (NULL, "%d address%s unparsable", badadr, plural (badadr));
1301 die (NULL, "%d addressee%s undeliverable", unkadr, plural (unkadr));
1306 do_addresses (int bccque, int talk)
1310 struct mailname *lp;
1313 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1314 if (lp->m_bcc ? bccque : !bccque) {
1316 printf (" -- Local Recipients --\n");
1317 do_an_address (lp, talk);
1322 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1323 if (lp->m_bcc ? bccque : !bccque) {
1325 printf (" -- UUCP Recipients --\n");
1326 do_an_address (lp, talk);
1331 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1332 if (lp->m_bcc ? bccque : !bccque) {
1334 printf (" -- Network Recipients --\n");
1335 do_an_address (lp, talk);
1341 if (rp_isbad (retval = sm_waend ()))
1342 die (NULL, "problem ending addresses; %s", rp_string (retval));
1347 * MTS-SPECIFIC INTERACTION
1352 * SENDMAIL/SMTP routines
1356 post (char *file, int bccque, int talk)
1361 onex = !(msgflags & MINV) || bccque;
1363 if (msgflags & MINV)
1364 printf (" -- Posting for %s Recipients --\n",
1365 bccque ? "Blind" : "Sighted");
1367 printf (" -- Posting for All Recipients --\n");
1372 if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch, verbose,
1373 snoop, onex, queued, sasl, saslssf,
1374 saslmech, user, tls))
1375 || rp_isbad (retval = sm_winit (from)))
1376 die (NULL, "problem initializing server; %s", rp_string (retval));
1378 do_addresses (bccque, talk && verbose);
1379 if ((fd = open (file, O_RDONLY)) == NOTOK)
1380 die (file, "unable to re-open");
1385 sm_end (onex ? OK : DONE);
1389 if (msgflags & MINV)
1390 printf (" -- %s Recipient Copies Posted --\n",
1391 bccque ? "Blind" : "Sighted");
1393 printf (" -- Recipient Copies Posted --\n");
1400 /* Address Verification */
1403 verify_all_addresses (int talk)
1406 struct mailname *lp;
1410 if (!whomsw || checksw)
1411 if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch,
1412 verbose, snoop, 0, queued, sasl,
1413 saslssf, saslmech, user, tls))
1414 || rp_isbad (retval = sm_winit (from)))
1415 die (NULL, "problem initializing server; %s", rp_string (retval));
1417 if (talk && !whomsw)
1418 printf (" -- Address Verification --\n");
1419 if (talk && localaddrs.m_next)
1420 printf (" -- Local Recipients --\n");
1421 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1422 do_an_address (lp, talk);
1424 if (talk && uuaddrs.m_next)
1425 printf (" -- UUCP Recipients --\n");
1426 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1427 do_an_address (lp, talk);
1429 if (talk && netaddrs.m_next)
1430 printf (" -- Network Recipients --\n");
1431 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1432 do_an_address (lp, talk);
1435 if (talk && !whomsw)
1436 printf (" -- Address Verification Successful --\n");
1438 if (!whomsw || checksw)
1447 do_an_address (struct mailname *lp, int talk)
1453 switch (lp->m_type) {
1457 strncpy (addr, mbox, sizeof(addr));
1461 mbox = auxformat (lp, 0);
1463 snprintf (addr, sizeof(addr), "%s!%s", lp->m_host, lp->m_mbox);
1466 default: /* let SendMail decide if the host is bad */
1469 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1474 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1476 if (whomsw && !checksw) {
1484 switch (retval = sm_wadr (mbox, host,
1485 lp->m_type != UUCPHOST ? lp->m_path : NULL)) {
1488 printf ("address ok\n");
1494 fprintf (stderr, " %s: ", addr);
1495 fprintf (talk ? stdout : stderr, "loses; %s\n",
1496 rp_string (retval));
1502 fprintf (stderr, " %s: ", addr);
1503 die (NULL, "unexpected response; %s", rp_string (retval));
1511 do_text (char *file, int fd)
1516 lseek (fd, (off_t) 0, SEEK_SET);
1518 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1519 if (rp_isbad (retval = sm_wtxt (buf, state)))
1520 die (NULL, "problem writing text; %s\n", rp_string (retval));
1524 die (file, "problem reading from");
1526 switch (retval = sm_wtend ()) {
1532 die (NULL, "posting failed; %s", rp_string (retval));
1535 die (NULL, "unexpected response; %s", rp_string (retval));
1550 if (msgflags & MINV)
1553 if (!whomsw || checksw)
1566 hstat = SIGNAL2 (SIGHUP, sigser);
1567 istat = SIGNAL2 (SIGINT, sigser);
1568 qstat = SIGNAL2 (SIGQUIT, sigser);
1569 tstat = SIGNAL2 (SIGTERM, sigser);
1579 SIGNAL (SIGHUP, hstat);
1580 SIGNAL (SIGINT, istat);
1581 SIGNAL (SIGQUIT, qstat);
1582 SIGNAL (SIGTERM, tstat);
1590 p_refile (char *file)
1598 printf (" -- Filing Folder Copies --\n");
1599 for (i = 0; i < fccind; i++)
1600 fcc (file, fccfold[i]);
1602 printf (" -- Folder Copies Filed --\n");
1607 * Call the `fileproc' to add the file to the folder.
1611 fcc (char *file, char *folder)
1618 printf (" %sFcc %s: ", msgstate == RESENT ? "Resent-" : "", folder);
1621 for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
1627 fprintf (stderr, " %sFcc %s: ",
1628 msgstate == RESENT ? "Resent-" : "", folder);
1629 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
1633 /* see if we need to add `+' */
1634 snprintf (fold, sizeof(fold), "%s%s",
1635 *folder == '+' || *folder == '@' ? "" : "+", folder);
1637 /* now exec the fileproc */
1638 execlp (fileproc, r1bindex (fileproc, '/'),
1639 "-link", "-file", file, fold, NULL);
1643 if ((status = pidwait (child_id, OK))) {
1645 fprintf (stderr, " %sFcc %s: ",
1646 msgstate == RESENT ? "Resent-" : "", folder);
1647 pidstatus (status, verbose ? stdout : stderr, NULL);
1650 printf ("folder ok\n");
1662 die (char *what, char *fmt, ...)
1667 if (msgflags & MINV)
1670 if (!whomsw || checksw)
1674 advertise (what, NULL, fmt, ap);