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 /* In the following array of structures, the numeric second field of the
47 structures (minchars) is apparently used like this:
49 -# : Switch can be abbreviated to # characters; switch hidden in -help.
50 0 : Switch can't be abbreviated; switch shown in -help.
51 # : Switch can be abbreviated to # characters; switch shown in -help. */
53 static struct swit switches[] = {
55 { "alias aliasfile", 0 },
57 { "check", -5 }, /* interface from whom */
59 { "nocheck", -7 }, /* interface from whom */
63 { "dist", -4 }, /* interface from dist */
65 { "filter filterfile", 0 },
73 { "library directory", -7 }, /* interface from send, whom */
91 { "whom", -4 }, /* interface from whom */
93 { "width columns", 0 },
99 { "dashstuffing", -12 }, /* should we dashstuff BCC messages? */
100 #define NBITSTUFFSW 23
101 { "nodashstuffing", -14 },
103 { "idanno number", -6 }, /* interface from send */
105 { "client host", -6 },
107 { "server host", 6 }, /* specify alternate SMTP server */
109 { "snoop", -5 }, /* snoop the SMTP transaction */
115 { "sasl", SASLminc(-4) },
117 { "nosasl", SASLminc(-6) },
118 #define SASLMXSSFSW 32
119 { "saslmaxssf", SASLminc(-10) },
120 #define SASLMECHSW 33
121 { "saslmech", SASLminc(-5) },
123 { "user", SASLminc(-4) },
125 { "port server port name/number", 4 },
127 { "tls", TLSminc(-3) },
129 { "notls", TLSminc(-5) },
130 #define FILEPROCSW 38
135 { "mts smtp|sendmail/smtp|sendmail/pipe", 2 },
136 #define MESSAGEIDSW 41
137 { "messageid localname|random", 2 },
149 * flags for headers->flags
151 #define HNOP 0x0000 /* just used to keep .set around */
152 #define HBAD 0x0001 /* bad header - don't let it through */
153 #define HADR 0x0002 /* header has an address field */
154 #define HSUB 0x0004 /* Subject: header */
155 #define HTRY 0x0008 /* try to send to addrs on header */
156 #define HBCC 0x0010 /* don't output this header, unless MTS_SENDMAIL_PIPE */
157 #define HMNG 0x0020 /* munge this header */
158 #define HNGR 0x0040 /* no groups allowed in this header */
159 #define HFCC 0x0080 /* FCC: type header */
160 #define HNIL 0x0100 /* okay for this header not to have addrs */
161 #define HIGN 0x0200 /* ignore this header */
162 #define HDCC 0x0400 /* another undocumented feature */
163 #define HONE 0x0800 /* Only (zero or) one address allowed */
164 #define HEFM 0x1000 /* Envelope-From: header */
167 * flags for headers->set
169 #define MFRM 0x0001 /* we've seen a From: */
170 #define MDAT 0x0002 /* we've seen a Date: */
171 #define MRFM 0x0004 /* we've seen a Resent-From: */
172 #define MVIS 0x0008 /* we've seen sighted addrs */
173 #define MINV 0x0010 /* we've seen blind addrs */
174 #define MSND 0x0020 /* we've seen a Sender: */
175 #define MRSN 0x0040 /* We've seen a Resent-Sendr:*/
176 #define MEFM 0x0080 /* We've seen Envelope-From: */
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|HNGR|HONE, MSND },
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 },
193 { "Envelope-From", HADR|HONE|HEFM, MEFM },
197 static struct headers RHeaders[] = {
198 { "Resent-Reply-To", HADR|HNGR, 0 },
199 { "Resent-From", HADR|HNGR, MRFM },
200 { "Resent-Sender", HADR|HNGR, MRSN },
201 { "Resent-Date", HBAD, 0 },
202 { "Resent-Subject", HSUB, 0 },
203 { "Resent-To", HADR|HTRY, MVIS },
204 { "Resent-cc", HADR|HTRY, MVIS },
205 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
206 { "Resent-Message-ID", HBAD, 0 },
207 { "Resent-Fcc", HFCC, 0 },
208 { "Reply-To", HADR, 0 },
209 { "From", HADR|HNGR, MFRM },
210 { "Sender", HADR|HNGR, MSND },
211 { "Date", HNOP, MDAT },
212 { "To", HADR|HNIL, 0 },
213 { "cc", HADR|HNIL, 0 },
214 { "Bcc", HADR|HTRY|HBCC|HNIL, 0 },
216 { "Envelope-From", HADR|HONE|HEFM, MEFM },
220 static short fccind = 0; /* index into fccfold[] */
221 static short outputlinelen = OUTPUTLINELEN;
223 static int pfd = NOTOK; /* fd to write annotation list to */
224 static uid_t myuid= -1; /* my user id */
225 static gid_t mygid= -1; /* my group id */
226 static int recipients = 0; /* how many people will get a copy */
227 static int unkadr = 0; /* how many of those were unknown */
228 static int badadr = 0; /* number of bad addrs */
229 static int badmsg = 0; /* message has bad semantics */
230 static int verbose = 0; /* spell it out */
231 static int format = 1; /* format addresses */
232 static int mime = 0; /* use MIME-style encapsulations for Bcc */
233 static int msgid = 0; /* add msgid */
234 static int debug = 0; /* debugging post */
235 static int watch = 0; /* watch the delivery process */
236 static int whomsw = 0; /* we are whom not post */
237 static int checksw = 0; /* whom -check */
238 static int linepos=0; /* putadr()'s position on the line */
239 static int nameoutput=0; /* putadr() has output header name */
240 static int sasl=0; /* Use SASL auth for SMTP */
241 static int saslssf=-1; /* Our maximum SSF for SASL */
242 static char *saslmech=NULL; /* Force use of particular SASL mech */
243 static char *user=NULL; /* Authenticate as this user */
244 static char *port="smtp"; /* Name of server port for SMTP */
245 static int tls=0; /* Use TLS for encryption */
246 static int fromcount=0; /* Count of addresses on From: header */
247 static int seensender=0; /* Have we seen a Sender: header? */
249 static unsigned msgflags = 0; /* what we've seen */
253 static int msgstate = NORMAL;
255 static time_t tclock = 0; /* the time we started (more or less) */
257 static SIGNAL_HANDLER hstat, istat, qstat, tstat;
259 static char tmpfil[BUFSIZ];
260 static char bccfil[BUFSIZ];
262 static char from[BUFSIZ]; /* my network address */
263 static char sender[BUFSIZ]; /* my Sender: header */
264 static char efrom[BUFSIZ]; /* my Envelope-From: header */
265 static char signature[BUFSIZ]; /* my signature */
266 static char *filter = NULL; /* the filter for BCC'ing */
267 static char *subject = NULL; /* the subject field for BCC'ing */
268 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
270 static struct headers *hdrtab; /* table for the message we're doing */
272 static struct mailname localaddrs; /* local addrs */
273 static struct mailname netaddrs; /* network addrs */
274 static struct mailname uuaddrs; /* uucp addrs */
275 static struct mailname tmpaddrs; /* temporary queue */
277 static int snoop = 0;
278 static char *clientsw = NULL;
279 static char *serversw = NULL;
281 extern struct smtp sm_reply;
283 static char prefix[] = "----- =_aaaaaaaaaa";
285 static char *partno = NULL;
286 static int queued = 0;
291 static void putfmt (char *, char *, FILE *);
292 static void start_headers (void);
293 static void finish_headers (FILE *);
294 static int get_header (char *, struct headers *);
295 static int putadr (char *, char *, struct mailname *, FILE *, unsigned int);
296 static void putgrp (char *, char *, FILE *, unsigned int);
297 static int insert (struct mailname *);
298 static void pl (void);
299 static void anno (void);
300 static int annoaux (struct mailname *);
301 static void insert_fcc (struct headers *, unsigned char *);
302 static void make_bcc_file (int);
303 static void verify_all_addresses (int, char *);
304 static void chkadr (void);
305 static void sigon (void);
306 static void sigoff (void);
307 static void p_refile (char *);
308 static void fcc (char *, char *);
309 static void die (char *, char *, ...);
310 static void post (char *, int, int, char *);
311 static void do_text (char *file, int fd);
312 static void do_an_address (struct mailname *, int);
313 static void do_addresses (int, int);
314 static int find_prefix (void);
318 main (int argc, char **argv)
320 int state, compnum, dashstuff = 0;
321 char *cp, *msg = NULL, **argp, **arguments, *envelope;
322 char buf[BUFSIZ], name[NAMESZ];
326 setlocale(LC_ALL, "");
328 invo_name = r1bindex (argv[0], '/');
330 /* foil search of user profile/context */
331 if (context_foil (NULL) == -1)
334 mts_init (invo_name);
335 arguments = getarguments (invo_name, argc, argv, 0);
338 while ((cp = *argp++)) {
340 switch (smatch (++cp, switches)) {
342 ambigsw (cp, switches);
345 adios (NULL, "-%s unknown", cp);
348 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
349 print_help (buf, switches, 0);
352 print_version(invo_name);
356 if (!(cp = *argp++) || *cp == '-')
357 adios (NULL, "missing argument to %s", argp[-2]);
358 /* create a minimal context */
359 if (context_foil (cp) == -1)
364 if (!(cp = *argp++) || *cp == '-')
365 adios (NULL, "missing argument to %s", argp[-2]);
366 if ((state = alias (cp)) != AK_OK)
367 adios (NULL, "aliasing error in %s - %s",
368 cp, akerror (state));
387 if (!(filter = *argp++) || *filter == '-')
388 adios (NULL, "missing argument to %s", argp[-2]);
443 if (!(cp = *argp++) || *cp == '-')
444 adios (NULL, "missing argument to %s", argp[-2]);
445 if ((outputlinelen = atoi (cp)) < 10)
446 adios (NULL, "impossible width %d", outputlinelen);
450 if (!(cp = *argp++) || *cp == '-')
451 adios (NULL, "missing argument to %s", argp[-2]);
452 if ((pfd = atoi (cp)) <= 2)
453 adios (NULL, "bad argument %s %s", argp[-2], cp);
457 if (!(clientsw = *argp++) || *clientsw == '-')
458 adios (NULL, "missing argument to %s", argp[-2]);
461 if (!(serversw = *argp++) || *serversw == '-')
462 adios (NULL, "missing argument to %s", argp[-2]);
469 if (!(partno = *argp++) || *partno == '-')
470 adios (NULL, "missing argument to %s", argp[-2]);
486 if (!(cp = *argp++) || *cp == '-')
487 adios (NULL, "missing argument to %s", argp[-2]);
492 if (!(saslmech = *argp++) || *saslmech == '-')
493 adios (NULL, "missing argument to %s", argp[-2]);
497 if (!(user = *argp++) || *user == '-')
498 adios (NULL, "missing argument to %s", argp[-2]);
502 if (!(port = *argp++) || *port == '-')
503 adios (NULL, "missing argument to %s", argp[-2]);
515 if (!(cp = *argp++) || *cp == '-')
516 adios (NULL, "missing argument to %s", argp[-2]);
521 if (!(cp = *argp++) || *cp == '-')
522 adios (NULL, "missing argument to %s", argp[-2]);
527 if (!(cp = *argp++) || *cp == '-')
528 adios (NULL, "missing argument to %s", argp[-2]);
529 save_mts_method (cp);
533 if (!(cp = *argp++) || *cp == '-')
534 adios (NULL, "missing argument to %s", argp[-2]);
535 if (save_message_id_style (cp) != 0)
536 adios (NULL, "unsupported messageid \"%s\"", cp);
541 adios (NULL, "only one message at a time!");
549 adios (NULL, "usage: %s [switches] file", invo_name);
551 if (outputlinelen < 10)
552 adios (NULL, "impossible width %d", outputlinelen);
554 if ((in = fopen (msg, "r")) == NULL)
555 adios (msg, "unable to open");
560 discard (out = stdout); /* XXX: reference discard() to help loader */
563 if ((out = fopen ("/dev/null", "w")) == NULL)
564 adios ("/dev/null", "unable to open");
566 char *cp = m_mktemp(m_maildir(invo_name), NULL, &out);
568 cp = m_mktemp2(NULL, invo_name, NULL, &out);
570 adios ("post", "unable to create temporary file");
573 strncpy(tmpfil, cp, sizeof(tmpfil));
574 chmod (tmpfil, 0600);
578 hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
580 for (compnum = 1, state = FLD;;) {
581 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
586 cp = add (buf, NULL);
587 while (state == FLDPLUS) {
588 state = m_getfld (state, name, buf, sizeof(buf), in);
591 putfmt (name, cp, out);
595 finish_headers (out);
600 finish_headers (out);
603 fprintf (out, "\n%s", buf);
604 while (state == BODY) {
605 state = m_getfld (state, name, buf, sizeof(buf), in);
611 finish_headers (out);
616 adios (NULL, "message format error in component #%d", compnum);
619 adios (NULL, "getfld() returned %d", state);
636 * Here's how we decide which address to use as the envelope-from
639 * - If we were given an Envelope-From header, use that.
640 * - If we were given a Sender: address, use that.
641 * - Otherwise, use the address on the From: line
644 if (msgflags & MEFM) {
646 } else if (seensender) {
652 /* If we are doing a "whom" check */
654 /* This won't work with MTS_SENDMAIL_PIPE. */
655 verify_all_addresses (1, envelope);
659 if (msgflags & MINV) {
660 make_bcc_file (dashstuff);
661 if (msgflags & MVIS) {
662 if (sm_mts != MTS_SENDMAIL_PIPE) {
663 /* It would be nice to have support to call
664 verify_all_addresses with MTS_SENDMAIL_PIPE, but
665 that might require running sendmail as root. Note
666 that spost didn't verify addresses. */
667 verify_all_addresses (verbose, envelope);
669 post (tmpfil, 0, verbose, envelope);
671 post (bccfil, 1, verbose, envelope);
674 post (tmpfil, 0, isatty (1), envelope);
681 printf (partno ? "Partial Message #%s Processed\n" : "Message Processed\n",
693 putfmt (char *name, char *str, FILE *out)
695 int count, grp, i, keep;
698 struct mailname *mp = NULL, *np = NULL;
701 while (*str == ' ' || *str == '\t')
704 if (msgstate == NORMAL && uprf (name, "resent")) {
705 advise (NULL, "illegal header line -- %s:", name);
710 if ((i = get_header (name, hdrtab)) == NOTOK) {
711 fprintf (out, "%s: %s", name, str);
716 if (hdr->flags & HIGN) {
719 if (hdr->flags & HBAD) {
720 advise (NULL, "illegal header line -- %s:", name);
724 msgflags |= (hdr->set & ~(MVIS | MINV));
726 if (hdr->flags & HSUB)
727 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
728 if (hdr->flags & HFCC) {
729 if ((cp = strrchr(str, '\n')))
731 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
733 insert_fcc (hdr, pp);
735 insert_fcc (hdr, pp);
739 if (!(hdr->flags & HADR)) {
740 fprintf (out, "%s: %s", name, str);
744 tmpaddrs.m_next = NULL;
745 for (count = 0; (cp = getname (str)); count++)
746 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
750 tmpaddrs.m_next = mp;
754 if (hdr->flags & HTRY)
760 if (hdr->flags & HNIL)
761 fprintf (out, "%s: %s", name, str);
764 * Sender (or Resent-Sender) can have only one address
766 if ((msgstate == RESENT) ? (hdr->set & MRSN)
767 : (hdr->set & MSND)) {
768 advise (NULL, "%s: field requires one address", name);
772 advise (NULL, "%s: field requires at least one address", name);
779 if (count > 1 && (hdr->flags & HONE)) {
780 advise (NULL, "%s: field only permits one address", name);
785 nameoutput = linepos = 0;
786 snprintf (namep, sizeof(namep), "%s%s",
787 (hdr->flags & HMNG) ? "Original-" : "", name);
789 for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
790 if (mp->m_nohost) { /* also used to test (hdr->flags & HTRY) */
791 /* The address doesn't include a host, so it might be an alias. */
792 pp = akvalue (mp->m_mbox); /* do mh alias substitution */
793 qp = akvisible () ? mp->m_mbox : "";
796 putgrp (namep, np->m_gname, out, hdr->flags);
797 while ((cp = getname (pp))) {
798 if (!(mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
804 * If it's a From: or Resent-From: header, save the address
805 * for later possible use (as the envelope address for SMTP)
808 if ((msgstate == RESENT) ? (hdr->set & MRFM)
809 : (hdr->set & MFRM)) {
810 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
811 from[sizeof(from) - 1] = '\0';
816 * Also save the Sender: or Resent-Sender: header as well
819 if ((msgstate == RESENT) ? (hdr->set & MRSN)
820 : (hdr->set & MSND)) {
821 strncpy(sender, auxformat(mp, 0), sizeof(sender) - 1);
822 sender[sizeof(sender) - 1] = '\0';
827 * ALSO ... save Envelope-From
830 if (hdr->set & MEFM) {
831 strncpy(efrom, auxformat(mp, 0), sizeof(efrom) - 1);
832 efrom[sizeof(efrom) - 1] = '\0';
835 if (hdr->flags & HBCC)
838 mp->m_ingrp = np->m_ingrp;
841 putgrp (namep, mp->m_gname, out, hdr->flags);
844 if (putadr (namep, qp, mp, out, hdr->flags))
845 msgflags |= (hdr->set & (MVIS | MINV));
854 /* Address includes a host, so no alias substitution is needed. */
857 * If it's a From: or Resent-From header, save the address
858 * for later possible use (as the envelope address for SMTP)
861 if ((msgstate == RESENT) ? (hdr->set & MRFM)
862 : (hdr->set & MFRM)) {
863 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
868 * Also save the Sender: header as well
871 if ((msgstate == RESENT) ? (hdr->set & MRSN)
872 : (hdr->set & MSND)) {
873 strncpy(sender, auxformat(mp, 0), sizeof(sender) - 1);
874 sender[sizeof(sender) - 1] = '\0';
879 * ALSO ... save Envelope-From
882 if (hdr->set & MEFM) {
883 strncpy(efrom, auxformat(mp, 0), sizeof(efrom) - 1);
884 efrom[sizeof(efrom) - 1] = '\0';
887 if (hdr->flags & HBCC)
890 putgrp (namep, mp->m_gname, out, hdr->flags);
893 keep = putadr (namep, "", mp, out, hdr->flags);
897 msgflags |= (hdr->set & (MVIS | MINV));
903 if (grp > 0 && (hdr->flags & HNGR)) {
904 advise (NULL, "%s: field does not allow groups", name);
925 * Probably not necessary, but just in case ...
932 if ((cp = getfullname ()) && *cp) {
933 strncpy (sigbuf, cp, sizeof(sigbuf));
934 snprintf (signature, sizeof(signature), "%s <%s>",
935 sigbuf, getlocaladdr());
936 if ((cp = getname (signature)) == NULL)
937 adios (NULL, "getname () failed -- you lose extraordinarily big");
938 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL)) == NULL)
939 adios (NULL, "bad signature '%s'", sigbuf);
944 strncpy (signature, getlocaladdr(), sizeof(signature));
950 * Now that we've outputted the header fields in the draft
951 * message, we will now output any remaining header fields
952 * that we need to add/create.
956 finish_headers (FILE *out)
960 if (!(msgflags & MFRM)) {
962 * A From: header is now required in the draft.
964 advise (NULL, "message has no From: header");
965 advise (NULL, "See default components files for examples");
970 if (fromcount > 1 && (seensender == 0 && !(msgflags & MEFM))) {
971 advise (NULL, "A Sender: or Envelope-From: header is required "
972 "with multiple\nFrom: addresses");
980 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
982 fprintf (out, "Message-ID: %s\n", message_id (tclock, 0));
984 * If we have multiple From: addresses, make sure we have an
985 * Sender: header. If we don't have one, then generate one
986 * from Envelope-From: (which in this case, cannot be blank)
989 if (fromcount > 1 && seensender == 0) {
990 if (efrom[0] == '\0') {
991 advise (NULL, "Envelope-From cannot be blank when there "
992 "is multiple From: addresses\nand no Sender: "
996 fprintf (out, "Sender: %s\n", efrom);
1000 if (!(msgflags & MVIS))
1001 fprintf (out, "Bcc: Blind Distribution List: ;\n");
1005 if (!(msgflags & MDAT)) {
1006 advise (NULL, "message has no Date: header");
1009 if (!(msgflags & MFRM)) {
1010 advise (NULL, "message has no From: header");
1013 if (!(msgflags & MRFM)) {
1014 advise (NULL, "message has no Resent-From: header");
1015 advise (NULL, "See default components files for examples");
1019 if (fromcount > 1 && (seensender == 0 && !(msgflags & MEFM))) {
1020 advise (NULL, "A Resent-Sender: or Envelope-From: header is "
1021 "required with multiple\nResent-From: addresses");
1029 fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
1031 fprintf (out, "Resent-Message-ID: %s\n",
1032 message_id (tclock, 0));
1034 * If we have multiple Resent-From: addresses, make sure we have an
1035 * Resent-Sender: header. If we don't have one, then generate one
1036 * from Envelope-From (which in this case, cannot be blank)
1039 if (fromcount > 1 && seensender == 0) {
1040 if (efrom[0] == '\0') {
1041 advise (NULL, "Envelope-From cannot be blank when there "
1042 "is multiple Resent-From: addresses and no "
1043 "Resent-Sender: header");
1046 fprintf (out, "Resent-Sender: %s\n", efrom);
1050 if (!(msgflags & MVIS))
1051 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
1056 adios (NULL, "re-format message and try again");
1058 adios (NULL, "no addressees");
1063 get_header (char *header, struct headers *table)
1067 for (h = table; h->value; h++)
1068 if (!mh_strcasecmp (header, h->value))
1076 putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flags)
1080 char buffer[BUFSIZ];
1082 if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
1084 if (sm_mts != MTS_SENDMAIL_PIPE &&
1085 ((flags & (HBCC | HDCC | HEFM)) || mp->m_ingrp))
1089 fprintf (out, "%s: ", name);
1090 linepos += (nameoutput = strlen (name) + 2);
1093 if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
1094 mp->m_pers = getcpy (aka);
1097 snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
1100 cp = adrformat (mp);
1107 if (linepos != nameoutput) {
1108 if (len + linepos + 2 > outputlinelen)
1109 fprintf (out, ",\n%*s", linepos = nameoutput, "");
1119 return (flags & HTRY);
1124 putgrp (char *name, char *group, FILE *out, unsigned int flags)
1129 if (sm_mts != MTS_SENDMAIL_PIPE && (flags & HBCC))
1133 fprintf (out, "%s: ", name);
1134 linepos += (nameoutput = strlen (name) + 2);
1137 cp = concat (group, ";", NULL);
1140 if (linepos > nameoutput) {
1141 if (len + linepos + 2 > outputlinelen) {
1142 fprintf (out, ",\n%*s", nameoutput, "");
1143 linepos = nameoutput;
1157 insert (struct mailname *np)
1159 struct mailname *mp;
1161 if (np->m_mbox == NULL)
1164 for (mp = np->m_type == LOCALHOST ? &localaddrs
1165 : np->m_type == UUCPHOST ? &uuaddrs
1169 if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
1170 && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox)
1171 && np->m_bcc == mp->m_next->m_bcc)
1184 struct mailname *mp;
1186 printf ("-------\n\t-- Addresses --\nlocal:\t");
1187 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1188 printf ("%s%s%s", mp->m_mbox,
1189 mp->m_bcc ? "[BCC]" : "",
1190 mp->m_next ? ",\n\t" : "");
1192 printf ("\nnet:\t");
1193 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1194 printf ("%s%s@%s%s%s", mp->m_path ? mp->m_path : "",
1195 mp->m_mbox, mp->m_host,
1196 mp->m_bcc ? "[BCC]" : "",
1197 mp->m_next ? ",\n\t" : "");
1199 printf ("\nuucp:\t");
1200 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1201 printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1202 mp->m_bcc ? "[BCC]" : "",
1203 mp->m_next ? ",\n\t" : "");
1205 printf ("\n\t-- Folder Copies --\nfcc:\t");
1206 for (i = 0; i < fccind; i++)
1207 printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1215 struct mailname *mp;
1217 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1218 if (annoaux (mp) == NOTOK)
1221 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1222 if (annoaux (mp) == NOTOK)
1225 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1226 if (annoaux (mp) == NOTOK)
1236 annoaux (struct mailname *mp)
1239 char buffer[BUFSIZ];
1241 snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1242 i = strlen (buffer);
1244 return (write (pfd, buffer, i) == i ? OK : NOTOK);
1249 insert_fcc (struct headers *hdr, unsigned char *pp)
1253 for (cp = pp; isspace (*cp); cp++)
1255 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
1263 adios (NULL, "too many %ss", hdr->value);
1264 fccfold[fccind++] = getcpy (cp);
1272 make_bcc_file (int dashstuff)
1280 tfile = m_mktemp2(NULL, "bccs", NULL, &out);
1281 if (tfile == NULL) adios("bcc", "unable to create temporary file");
1282 strncpy (bccfil, tfile, sizeof(bccfil));
1284 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1286 fprintf (out, "Message-ID: %s\n", message_id (tclock, 0));
1288 fprintf (out, "Subject: %s", subject);
1289 fprintf (out, "BCC:\n");
1292 * Use MIME encapsulation for Bcc messages
1298 * Check if any lines in the message clash with the
1299 * prefix for the MIME multipart separator. If there
1300 * is a clash, increment one of the letters in the
1301 * prefix and check again.
1303 if ((cp = strchr(prefix, 'a')) == NULL)
1304 adios (NULL, "lost prefix start");
1305 while (find_prefix () == NOTOK) {
1310 adios (NULL, "can't find a unique delimiter string");
1315 fprintf (out, "%s: %s\n%s: multipart/digest; boundary=\"",
1316 VRSN_FIELD, VRSN_VALUE, TYPE_FIELD);
1317 fprintf (out, "%s\"\n\n--%s\n\n", prefix, prefix);
1319 fprintf (out, "\n------- Blind-Carbon-Copy\n\n");
1325 * Do mhl filtering of Bcc messages instead
1326 * of MIME encapsulation.
1328 if (filter != NULL) {
1329 vec[0] = r1bindex (mhlproc, '/');
1331 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1335 adios ("fork", "unable to");
1338 dup2 (fileno (out), 1);
1341 vec[i++] = "-forward";
1346 /* was the flag -[no]dashstuffing specified? */
1348 vec[i++] = "-dashstuffing";
1349 else if (dashstuff < 0)
1350 vec[i++] = "-nodashstuffing";
1353 execvp (mhlproc, vec);
1354 fprintf (stderr, "unable to exec ");
1359 pidXwait (child_id, mhlproc);
1363 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
1364 adios (tmpfil, "unable to re-open");
1367 * If using MIME encapsulation, or if the -nodashstuffing
1368 * flag was given, then just copy message. Else do
1369 * RFC934 quoting (dashstuffing).
1371 if (mime || dashstuff < 0)
1372 cpydata (fd, fileno (out), tmpfil, bccfil);
1374 cpydgst (fd, fileno (out), tmpfil, bccfil);
1378 fseek (out, 0L, SEEK_END);
1380 fprintf (out, "\n--%s--\n", prefix);
1382 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
1388 * Scan message to check if any lines clash with
1389 * the prefix of the MIME multipart separator.
1396 unsigned char buffer[BUFSIZ];
1399 if ((in = fopen (tmpfil, "r")) == NULL)
1400 adios (tmpfil, "unable to re-open");
1402 while (fgets (buffer, sizeof(buffer) - 1, in))
1403 if (buffer[0] == '-' && buffer[1] == '-') {
1406 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1410 if (strcmp (buffer + 2, prefix) == 0) {
1421 #define plural(x) (x == 1 ? "" : "s")
1426 if (badadr && unkadr)
1427 die (NULL, "%d address%s unparsable, %d addressee%s undeliverable",
1428 badadr, plural (badadr), unkadr, plural (badadr));
1430 die (NULL, "%d address%s unparsable", badadr, plural (badadr));
1432 die (NULL, "%d addressee%s undeliverable", unkadr, plural (unkadr));
1437 do_addresses (int bccque, int talk)
1441 struct mailname *lp;
1444 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1445 if (lp->m_bcc ? bccque : !bccque) {
1447 printf (" -- Local Recipients --\n");
1448 do_an_address (lp, talk);
1453 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1454 if (lp->m_bcc ? bccque : !bccque) {
1456 printf (" -- UUCP Recipients --\n");
1457 do_an_address (lp, talk);
1462 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1463 if (lp->m_bcc ? bccque : !bccque) {
1465 printf (" -- Network Recipients --\n");
1466 do_an_address (lp, talk);
1472 if (rp_isbad (retval = sm_waend ()))
1473 die (NULL, "problem ending addresses; %s", rp_string (retval));
1478 * MTS-SPECIFIC INTERACTION
1483 * SENDMAIL/SMTP routines
1487 post (char *file, int bccque, int talk, char *envelope)
1494 if (msgflags & MINV)
1495 printf (" -- Posting for %s Recipients --\n",
1496 bccque ? "Blind" : "Sighted");
1498 printf (" -- Posting for All Recipients --\n");
1503 if (sm_mts == MTS_SENDMAIL_PIPE) {
1504 char *sargv[16], **argp;
1506 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1510 adios ("fork", "unable to");
1513 if (freopen( file, "r", stdin) == NULL) {
1514 adios (file, "can't reopen for sendmail");
1518 *argp++ = "sendmail";
1519 *argp++ = "-t"; /* read msg for recipients */
1520 *argp++ = "-i"; /* don't stop on "." */
1527 execv (sendmail, sargv);
1528 adios (sendmail, "can't exec");
1531 pidXwait (child_id, NULL);
1535 if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch,
1536 verbose, snoop, queued, sasl,
1537 saslssf, saslmech, user, tls)) ||
1538 rp_isbad (retval = sm_winit (envelope)))
1539 die (NULL, "problem initializing server; %s", rp_string (retval));
1541 do_addresses (bccque, talk && verbose);
1542 if ((fd = open (file, O_RDONLY)) == NOTOK)
1543 die (file, "unable to re-open");
1548 sm_end (!(msgflags & MINV) || bccque ? OK : DONE);
1552 if (msgflags & MINV)
1553 printf (" -- %s Recipient Copies Posted --\n",
1554 bccque ? "Blind" : "Sighted");
1556 printf (" -- Recipient Copies Posted --\n");
1564 /* Address Verification */
1567 verify_all_addresses (int talk, char *envelope)
1570 struct mailname *lp;
1574 if (!whomsw || checksw)
1575 if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch,
1576 verbose, snoop, queued, sasl,
1577 saslssf, saslmech, user, tls))
1578 || rp_isbad (retval = sm_winit (envelope)))
1579 die (NULL, "problem initializing server; %s", rp_string (retval));
1581 if (talk && !whomsw)
1582 printf (" -- Address Verification --\n");
1583 if (talk && localaddrs.m_next)
1584 printf (" -- Local Recipients --\n");
1585 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1586 do_an_address (lp, talk);
1588 if (talk && uuaddrs.m_next)
1589 printf (" -- UUCP Recipients --\n");
1590 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1591 do_an_address (lp, talk);
1593 if (talk && netaddrs.m_next)
1594 printf (" -- Network Recipients --\n");
1595 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1596 do_an_address (lp, talk);
1599 if (talk && !whomsw)
1600 printf (" -- Address Verification Successful --\n");
1602 if (!whomsw || checksw)
1611 do_an_address (struct mailname *lp, int talk)
1617 switch (lp->m_type) {
1621 strncpy (addr, mbox, sizeof(addr));
1625 mbox = auxformat (lp, 0);
1627 snprintf (addr, sizeof(addr), "%s!%s", lp->m_host, lp->m_mbox);
1630 default: /* let SendMail decide if the host is bad */
1633 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1638 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1640 if (whomsw && !checksw) {
1648 switch (retval = sm_wadr (mbox, host,
1649 lp->m_type != UUCPHOST ? lp->m_path : NULL)) {
1652 printf ("address ok\n");
1658 fprintf (stderr, " %s: ", addr);
1659 fprintf (talk ? stdout : stderr, "loses; %s\n",
1660 rp_string (retval));
1666 fprintf (stderr, " %s: ", addr);
1667 die (NULL, "unexpected response; %s", rp_string (retval));
1675 do_text (char *file, int fd)
1680 lseek (fd, (off_t) 0, SEEK_SET);
1682 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1683 if (rp_isbad (retval = sm_wtxt (buf, state)))
1684 die (NULL, "problem writing text; %s\n", rp_string (retval));
1688 die (file, "problem reading from");
1690 switch (retval = sm_wtend ()) {
1696 die (NULL, "posting failed; %s", rp_string (retval));
1699 die (NULL, "unexpected response; %s", rp_string (retval));
1714 if (msgflags & MINV)
1717 if (!whomsw || checksw)
1730 hstat = SIGNAL2 (SIGHUP, sigser);
1731 istat = SIGNAL2 (SIGINT, sigser);
1732 qstat = SIGNAL2 (SIGQUIT, sigser);
1733 tstat = SIGNAL2 (SIGTERM, sigser);
1743 SIGNAL (SIGHUP, hstat);
1744 SIGNAL (SIGINT, istat);
1745 SIGNAL (SIGQUIT, qstat);
1746 SIGNAL (SIGTERM, tstat);
1754 p_refile (char *file)
1762 printf (" -- Filing Folder Copies --\n");
1763 for (i = 0; i < fccind; i++)
1764 fcc (file, fccfold[i]);
1766 printf (" -- Folder Copies Filed --\n");
1771 * Call the `fileproc' to add the file to the folder.
1775 fcc (char *file, char *folder)
1782 printf (" %sFcc %s: ", msgstate == RESENT ? "Resent-" : "", folder);
1785 for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
1791 fprintf (stderr, " %sFcc %s: ",
1792 msgstate == RESENT ? "Resent-" : "", folder);
1793 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
1797 /* see if we need to add `+' */
1798 snprintf (fold, sizeof(fold), "%s%s",
1799 *folder == '+' || *folder == '@' ? "" : "+", folder);
1801 /* now exec the fileproc */
1802 execlp (fileproc, r1bindex (fileproc, '/'),
1803 "-link", "-file", file, fold, NULL);
1807 if ((status = pidwait (child_id, OK))) {
1809 fprintf (stderr, " %sFcc %s: ",
1810 msgstate == RESENT ? "Resent-" : "", folder);
1811 pidstatus (status, verbose ? stdout : stderr, NULL);
1814 printf ("folder ok\n");
1826 die (char *what, char *fmt, ...)
1831 if (msgflags & MINV)
1834 if (!whomsw || checksw)
1838 advertise (what, NULL, fmt, ap);