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 },
147 * flags for headers->flags
149 #define HNOP 0x0000 /* just used to keep .set around */
150 #define HBAD 0x0001 /* bad header - don't let it through */
151 #define HADR 0x0002 /* header has an address field */
152 #define HSUB 0x0004 /* Subject: header */
153 #define HTRY 0x0008 /* try to send to addrs on header */
154 #define HBCC 0x0010 /* don't output this header, unless MTS_SENDMAIL_PIPE */
155 #define HMNG 0x0020 /* munge this header */
156 #define HNGR 0x0040 /* no groups allowed in this header */
157 #define HFCC 0x0080 /* FCC: type header */
158 #define HNIL 0x0100 /* okay for this header not to have addrs */
159 #define HIGN 0x0200 /* ignore this header */
160 #define HDCC 0x0400 /* another undocumented feature */
161 #define HONE 0x0800 /* Only (zero or) one address allowed */
162 #define HEFM 0x1000 /* Envelope-From: header */
165 * flags for headers->set
167 #define MFRM 0x0001 /* we've seen a From: */
168 #define MDAT 0x0002 /* we've seen a Date: */
169 #define MRFM 0x0004 /* we've seen a Resent-From: */
170 #define MVIS 0x0008 /* we've seen sighted addrs */
171 #define MINV 0x0010 /* we've seen blind addrs */
172 #define MSND 0x0020 /* we've seen a Sender: */
173 #define MRSN 0x0040 /* We've seen a Resent-Sendr:*/
174 #define MEFM 0x0080 /* We've seen Envelope-From: */
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|HNGR|HONE, MSND },
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 },
191 { "Envelope-From", HADR|HONE|HEFM, MEFM },
195 static struct headers RHeaders[] = {
196 { "Resent-Reply-To", HADR|HNGR, 0 },
197 { "Resent-From", HADR|HNGR, MRFM },
198 { "Resent-Sender", HADR|HNGR, MRSN },
199 { "Resent-Date", HBAD, 0 },
200 { "Resent-Subject", HSUB, 0 },
201 { "Resent-To", HADR|HTRY, MVIS },
202 { "Resent-cc", HADR|HTRY, MVIS },
203 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
204 { "Resent-Message-ID", HBAD, 0 },
205 { "Resent-Fcc", HFCC, 0 },
206 { "Reply-To", HADR, 0 },
207 { "From", HADR|HNGR, MFRM },
208 { "Sender", HADR|HNGR, MSND },
209 { "Date", HNOP, MDAT },
210 { "To", HADR|HNIL, 0 },
211 { "cc", HADR|HNIL, 0 },
212 { "Bcc", HADR|HTRY|HBCC|HNIL, 0 },
214 { "Envelope-From", HADR|HONE|HEFM, MEFM },
218 static short fccind = 0; /* index into fccfold[] */
219 static short outputlinelen = OUTPUTLINELEN;
221 static int pfd = NOTOK; /* fd to write annotation list to */
222 static uid_t myuid= -1; /* my user id */
223 static gid_t mygid= -1; /* my group id */
224 static int recipients = 0; /* how many people will get a copy */
225 static int unkadr = 0; /* how many of those were unknown */
226 static int badadr = 0; /* number of bad addrs */
227 static int badmsg = 0; /* message has bad semantics */
228 static int verbose = 0; /* spell it out */
229 static int format = 1; /* format addresses */
230 static int mime = 0; /* use MIME-style encapsulations for Bcc */
231 static int msgid = 0; /* add msgid */
232 static int debug = 0; /* debugging post */
233 static int watch = 0; /* watch the delivery process */
234 static int whomsw = 0; /* we are whom not post */
235 static int checksw = 0; /* whom -check */
236 static int linepos=0; /* putadr()'s position on the line */
237 static int nameoutput=0; /* putadr() has output header name */
238 static int sasl=0; /* Use SASL auth for SMTP */
239 static int saslssf=-1; /* Our maximum SSF for SASL */
240 static char *saslmech=NULL; /* Force use of particular SASL mech */
241 static char *user=NULL; /* Authenticate as this user */
242 static char *port="smtp"; /* Name of server port for SMTP */
243 static int tls=0; /* Use TLS for encryption */
244 static int fromcount=0; /* Count of addresses on From: header */
245 static int seensender=0; /* Have we seen a Sender: header? */
247 static unsigned msgflags = 0; /* what we've seen */
251 static int msgstate = NORMAL;
253 static time_t tclock = 0; /* the time we started (more or less) */
255 static SIGNAL_HANDLER hstat, istat, qstat, tstat;
257 static char tmpfil[BUFSIZ];
258 static char bccfil[BUFSIZ];
260 static char from[BUFSIZ]; /* my network address */
261 static char sender[BUFSIZ]; /* my Sender: header */
262 static char efrom[BUFSIZ]; /* my Envelope-From: header */
263 static char signature[BUFSIZ]; /* my signature */
264 static char *filter = NULL; /* the filter for BCC'ing */
265 static char *subject = NULL; /* the subject field for BCC'ing */
266 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
268 static struct headers *hdrtab; /* table for the message we're doing */
270 static struct mailname localaddrs; /* local addrs */
271 static struct mailname netaddrs; /* network addrs */
272 static struct mailname uuaddrs; /* uucp addrs */
273 static struct mailname tmpaddrs; /* temporary queue */
275 static int snoop = 0;
276 static char *clientsw = NULL;
277 static char *serversw = NULL;
279 extern struct smtp sm_reply;
281 static char prefix[] = "----- =_aaaaaaaaaa";
283 static char *partno = NULL;
284 static int queued = 0;
289 static void putfmt (char *, char *, FILE *);
290 static void start_headers (void);
291 static void finish_headers (FILE *);
292 static int get_header (char *, struct headers *);
293 static int putadr (char *, char *, struct mailname *, FILE *, unsigned int);
294 static void putgrp (char *, char *, FILE *, unsigned int);
295 static int insert (struct mailname *);
296 static void pl (void);
297 static void anno (void);
298 static int annoaux (struct mailname *);
299 static void insert_fcc (struct headers *, unsigned char *);
300 static void make_bcc_file (int);
301 static void verify_all_addresses (int, char *);
302 static void chkadr (void);
303 static void sigon (void);
304 static void sigoff (void);
305 static void p_refile (char *);
306 static void fcc (char *, char *);
307 static void die (char *, char *, ...);
308 static void post (char *, int, int, char *);
309 static void do_text (char *file, int fd);
310 static void do_an_address (struct mailname *, int);
311 static void do_addresses (int, int);
312 static int find_prefix (void);
316 main (int argc, char **argv)
318 int state, compnum, dashstuff = 0;
319 char *cp, *msg = NULL, **argp, **arguments, *envelope;
320 char buf[BUFSIZ], name[NAMESZ];
324 setlocale(LC_ALL, "");
326 invo_name = r1bindex (argv[0], '/');
328 /* foil search of user profile/context */
329 if (context_foil (NULL) == -1)
332 mts_init (invo_name);
333 arguments = getarguments (invo_name, argc, argv, 0);
336 while ((cp = *argp++)) {
338 switch (smatch (++cp, switches)) {
340 ambigsw (cp, switches);
343 adios (NULL, "-%s unknown", cp);
346 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
347 print_help (buf, switches, 0);
350 print_version(invo_name);
354 if (!(cp = *argp++) || *cp == '-')
355 adios (NULL, "missing argument to %s", argp[-2]);
356 /* create a minimal context */
357 if (context_foil (cp) == -1)
362 if (!(cp = *argp++) || *cp == '-')
363 adios (NULL, "missing argument to %s", argp[-2]);
364 if ((state = alias (cp)) != AK_OK)
365 adios (NULL, "aliasing error in %s - %s",
366 cp, akerror (state));
385 if (!(filter = *argp++) || *filter == '-')
386 adios (NULL, "missing argument to %s", argp[-2]);
441 if (!(cp = *argp++) || *cp == '-')
442 adios (NULL, "missing argument to %s", argp[-2]);
443 if ((outputlinelen = atoi (cp)) < 10)
444 adios (NULL, "impossible width %d", outputlinelen);
448 if (!(cp = *argp++) || *cp == '-')
449 adios (NULL, "missing argument to %s", argp[-2]);
450 if ((pfd = atoi (cp)) <= 2)
451 adios (NULL, "bad argument %s %s", argp[-2], cp);
455 if (!(clientsw = *argp++) || *clientsw == '-')
456 adios (NULL, "missing argument to %s", argp[-2]);
459 if (!(serversw = *argp++) || *serversw == '-')
460 adios (NULL, "missing argument to %s", argp[-2]);
467 if (!(partno = *argp++) || *partno == '-')
468 adios (NULL, "missing argument to %s", argp[-2]);
484 if (!(cp = *argp++) || *cp == '-')
485 adios (NULL, "missing argument to %s", argp[-2]);
490 if (!(saslmech = *argp++) || *saslmech == '-')
491 adios (NULL, "missing argument to %s", argp[-2]);
495 if (!(user = *argp++) || *user == '-')
496 adios (NULL, "missing argument to %s", argp[-2]);
500 if (!(port = *argp++) || *port == '-')
501 adios (NULL, "missing argument to %s", argp[-2]);
513 if (!(cp = *argp++) || *cp == '-')
514 adios (NULL, "missing argument to %s", argp[-2]);
519 if (!(cp = *argp++) || *cp == '-')
520 adios (NULL, "missing argument to %s", argp[-2]);
525 if (!(cp = *argp++) || *cp == '-')
526 adios (NULL, "missing argument to %s", argp[-2]);
527 save_mts_method (cp);
532 adios (NULL, "only one message at a time!");
540 adios (NULL, "usage: %s [switches] file", invo_name);
542 if (outputlinelen < 10)
543 adios (NULL, "impossible width %d", outputlinelen);
545 if ((in = fopen (msg, "r")) == NULL)
546 adios (msg, "unable to open");
551 discard (out = stdout); /* XXX: reference discard() to help loader */
554 if ((out = fopen ("/dev/null", "w")) == NULL)
555 adios ("/dev/null", "unable to open");
557 char *cp = m_mktemp(m_maildir(invo_name), NULL, &out);
559 cp = m_mktemp2(NULL, invo_name, NULL, &out);
561 adios ("post", "unable to create temporary file");
564 strncpy(tmpfil, cp, sizeof(tmpfil));
565 chmod (tmpfil, 0600);
569 hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
571 for (compnum = 1, state = FLD;;) {
572 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
577 cp = add (buf, NULL);
578 while (state == FLDPLUS) {
579 state = m_getfld (state, name, buf, sizeof(buf), in);
582 putfmt (name, cp, out);
586 finish_headers (out);
591 finish_headers (out);
594 fprintf (out, "\n%s", buf);
595 while (state == BODY) {
596 state = m_getfld (state, name, buf, sizeof(buf), in);
602 finish_headers (out);
607 adios (NULL, "message format error in component #%d", compnum);
610 adios (NULL, "getfld() returned %d", state);
627 * Here's how we decide which address to use as the envelope-from
630 * - If we were given an Envelope-From header, use that.
631 * - If we were given a Sender: address, use that.
632 * - Otherwise, use the address on the From: line
635 if (msgflags & MEFM) {
637 } else if (seensender) {
643 /* If we are doing a "whom" check */
645 /* This won't work with MTS_SENDMAIL_PIPE. */
646 verify_all_addresses (1, envelope);
650 if (msgflags & MINV) {
651 make_bcc_file (dashstuff);
652 if (msgflags & MVIS) {
653 if (sm_mts != MTS_SENDMAIL_PIPE) {
654 /* It would be nice to have support to call
655 verify_all_addresses with MTS_SENDMAIL_PIPE, but
656 that might require running sendmail as root. Note
657 that spost didn't verify addresses. */
658 verify_all_addresses (verbose, envelope);
660 post (tmpfil, 0, verbose, envelope);
662 post (bccfil, 1, verbose, envelope);
665 post (tmpfil, 0, isatty (1), envelope);
672 printf (partno ? "Partial Message #%s Processed\n" : "Message Processed\n",
684 putfmt (char *name, char *str, FILE *out)
686 int count, grp, i, keep;
689 struct mailname *mp = NULL, *np = NULL;
692 while (*str == ' ' || *str == '\t')
695 if (msgstate == NORMAL && uprf (name, "resent")) {
696 advise (NULL, "illegal header line -- %s:", name);
701 if ((i = get_header (name, hdrtab)) == NOTOK) {
702 fprintf (out, "%s: %s", name, str);
707 if (hdr->flags & HIGN) {
710 if (hdr->flags & HBAD) {
711 advise (NULL, "illegal header line -- %s:", name);
715 msgflags |= (hdr->set & ~(MVIS | MINV));
717 if (hdr->flags & HSUB)
718 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
719 if (hdr->flags & HFCC) {
720 if ((cp = strrchr(str, '\n')))
722 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
724 insert_fcc (hdr, pp);
726 insert_fcc (hdr, pp);
730 if (!(hdr->flags & HADR)) {
731 fprintf (out, "%s: %s", name, str);
735 tmpaddrs.m_next = NULL;
736 for (count = 0; (cp = getname (str)); count++)
737 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
741 tmpaddrs.m_next = mp;
745 if (hdr->flags & HTRY)
751 if (hdr->flags & HNIL)
752 fprintf (out, "%s: %s", name, str);
755 * Sender (or Resent-Sender) can have only one address
757 if ((msgstate == RESENT) ? (hdr->set & MRSN)
758 : (hdr->set & MSND)) {
759 advise (NULL, "%s: field requires one address", name);
763 advise (NULL, "%s: field requires at least one address", name);
770 if (count > 1 && (hdr->flags & HONE)) {
771 advise (NULL, "%s: field only permits one address", name);
776 nameoutput = linepos = 0;
777 snprintf (namep, sizeof(namep), "%s%s",
778 (hdr->flags & HMNG) ? "Original-" : "", name);
780 for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
781 if (mp->m_nohost) { /* also used to test (hdr->flags & HTRY) */
782 /* The address doesn't include a host, so it might be an alias. */
783 pp = akvalue (mp->m_mbox); /* do mh alias substitution */
784 qp = akvisible () ? mp->m_mbox : "";
787 putgrp (namep, np->m_gname, out, hdr->flags);
788 while ((cp = getname (pp))) {
789 if (!(mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
795 * If it's a From: or Resent-From: header, save the address
796 * for later possible use (as the envelope address for SMTP)
799 if ((msgstate == RESENT) ? (hdr->set & MRFM)
800 : (hdr->set & MFRM)) {
801 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
802 from[sizeof(from) - 1] = '\0';
807 * Also save the Sender: or Resent-Sender: header as well
810 if ((msgstate == RESENT) ? (hdr->set & MRSN)
811 : (hdr->set & MSND)) {
812 strncpy(sender, auxformat(mp, 0), sizeof(sender) - 1);
813 sender[sizeof(sender) - 1] = '\0';
818 * ALSO ... save Envelope-From
821 if (hdr->set & MEFM) {
822 strncpy(efrom, auxformat(mp, 0), sizeof(efrom) - 1);
823 efrom[sizeof(efrom) - 1] = '\0';
826 if (hdr->flags & HBCC)
829 mp->m_ingrp = np->m_ingrp;
832 putgrp (namep, mp->m_gname, out, hdr->flags);
835 if (putadr (namep, qp, mp, out, hdr->flags))
836 msgflags |= (hdr->set & (MVIS | MINV));
845 /* Address includes a host, so no alias substitution is needed. */
848 * If it's a From: or Resent-From header, save the address
849 * for later possible use (as the envelope address for SMTP)
852 if ((msgstate == RESENT) ? (hdr->set & MRFM)
853 : (hdr->set & MFRM)) {
854 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
859 * Also save the Sender: header as well
862 if ((msgstate == RESENT) ? (hdr->set & MRSN)
863 : (hdr->set & MSND)) {
864 strncpy(sender, auxformat(mp, 0), sizeof(sender) - 1);
865 sender[sizeof(sender) - 1] = '\0';
870 * ALSO ... save Envelope-From
873 if (hdr->set & MEFM) {
874 strncpy(efrom, auxformat(mp, 0), sizeof(efrom) - 1);
875 efrom[sizeof(efrom) - 1] = '\0';
878 if (hdr->flags & HBCC)
881 putgrp (namep, mp->m_gname, out, hdr->flags);
884 keep = putadr (namep, "", mp, out, hdr->flags);
888 msgflags |= (hdr->set & (MVIS | MINV));
894 if (grp > 0 && (hdr->flags & HNGR)) {
895 advise (NULL, "%s: field does not allow groups", name);
916 * Probably not necessary, but just in case ...
923 if ((cp = getfullname ()) && *cp) {
924 strncpy (sigbuf, cp, sizeof(sigbuf));
925 snprintf (signature, sizeof(signature), "%s <%s>",
926 sigbuf, getlocaladdr());
927 if ((cp = getname (signature)) == NULL)
928 adios (NULL, "getname () failed -- you lose extraordinarily big");
929 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL)) == NULL)
930 adios (NULL, "bad signature '%s'", sigbuf);
935 strncpy (signature, getlocaladdr(), sizeof(signature));
941 * Now that we've outputted the header fields in the draft
942 * message, we will now output any remaining header fields
943 * that we need to add/create.
947 finish_headers (FILE *out)
951 if (!(msgflags & MFRM)) {
953 * A From: header is now required in the draft.
955 advise (NULL, "message has no From: header");
956 advise (NULL, "See default components files for examples");
961 if (fromcount > 1 && (seensender == 0 && !(msgflags & MEFM))) {
962 advise (NULL, "A Sender: or Envelope-From: header is required "
963 "with multiple\nFrom: addresses");
971 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
973 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
974 (int) getpid (), (long) tclock, LocalName (1));
976 * If we have multiple From: addresses, make sure we have an
977 * Sender: header. If we don't have one, then generate one
978 * from Envelope-From: (which in this case, cannot be blank)
981 if (fromcount > 1 && seensender == 0) {
982 if (efrom[0] == '\0') {
983 advise (NULL, "Envelope-From cannot be blank when there "
984 "is multiple From: addresses\nand no Sender: "
988 fprintf (out, "Sender: %s\n", efrom);
992 if (!(msgflags & MVIS))
993 fprintf (out, "Bcc: Blind Distribution List: ;\n");
997 if (!(msgflags & MDAT)) {
998 advise (NULL, "message has no Date: header");
1001 if (!(msgflags & MFRM)) {
1002 advise (NULL, "message has no From: header");
1005 if (!(msgflags & MRFM)) {
1006 advise (NULL, "message has no Resent-From: header");
1007 advise (NULL, "See default components files for examples");
1011 if (fromcount > 1 && (seensender == 0 && !(msgflags & MEFM))) {
1012 advise (NULL, "A Resent-Sender: or Envelope-From: header is "
1013 "required with multiple\nResent-From: addresses");
1021 fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
1023 fprintf (out, "Resent-Message-ID: <%d.%ld@%s>\n",
1024 (int) getpid (), (long) tclock, LocalName (1));
1026 * If we have multiple Resent-From: addresses, make sure we have an
1027 * Resent-Sender: header. If we don't have one, then generate one
1028 * from Envelope-From (which in this case, cannot be blank)
1031 if (fromcount > 1 && seensender == 0) {
1032 if (efrom[0] == '\0') {
1033 advise (NULL, "Envelope-From cannot be blank when there "
1034 "is multiple Resent-From: addresses and no "
1035 "Resent-Sender: header");
1038 fprintf (out, "Resent-Sender: %s\n", efrom);
1042 if (!(msgflags & MVIS))
1043 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
1048 adios (NULL, "re-format message and try again");
1050 adios (NULL, "no addressees");
1055 get_header (char *header, struct headers *table)
1059 for (h = table; h->value; h++)
1060 if (!mh_strcasecmp (header, h->value))
1068 putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flags)
1072 char buffer[BUFSIZ];
1074 if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
1076 if (sm_mts != MTS_SENDMAIL_PIPE &&
1077 ((flags & (HBCC | HDCC | HEFM)) || mp->m_ingrp))
1081 fprintf (out, "%s: ", name);
1082 linepos += (nameoutput = strlen (name) + 2);
1085 if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
1086 mp->m_pers = getcpy (aka);
1089 snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
1092 cp = adrformat (mp);
1099 if (linepos != nameoutput) {
1100 if (len + linepos + 2 > outputlinelen)
1101 fprintf (out, ",\n%*s", linepos = nameoutput, "");
1111 return (flags & HTRY);
1116 putgrp (char *name, char *group, FILE *out, unsigned int flags)
1121 if (sm_mts != MTS_SENDMAIL_PIPE && (flags & HBCC))
1125 fprintf (out, "%s: ", name);
1126 linepos += (nameoutput = strlen (name) + 2);
1129 cp = concat (group, ";", NULL);
1132 if (linepos > nameoutput) {
1133 if (len + linepos + 2 > outputlinelen) {
1134 fprintf (out, ",\n%*s", nameoutput, "");
1135 linepos = nameoutput;
1149 insert (struct mailname *np)
1151 struct mailname *mp;
1153 if (np->m_mbox == NULL)
1156 for (mp = np->m_type == LOCALHOST ? &localaddrs
1157 : np->m_type == UUCPHOST ? &uuaddrs
1161 if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
1162 && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox)
1163 && np->m_bcc == mp->m_next->m_bcc)
1176 struct mailname *mp;
1178 printf ("-------\n\t-- Addresses --\nlocal:\t");
1179 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1180 printf ("%s%s%s", mp->m_mbox,
1181 mp->m_bcc ? "[BCC]" : "",
1182 mp->m_next ? ",\n\t" : "");
1184 printf ("\nnet:\t");
1185 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1186 printf ("%s%s@%s%s%s", mp->m_path ? mp->m_path : "",
1187 mp->m_mbox, mp->m_host,
1188 mp->m_bcc ? "[BCC]" : "",
1189 mp->m_next ? ",\n\t" : "");
1191 printf ("\nuucp:\t");
1192 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1193 printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1194 mp->m_bcc ? "[BCC]" : "",
1195 mp->m_next ? ",\n\t" : "");
1197 printf ("\n\t-- Folder Copies --\nfcc:\t");
1198 for (i = 0; i < fccind; i++)
1199 printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1207 struct mailname *mp;
1209 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1210 if (annoaux (mp) == NOTOK)
1213 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1214 if (annoaux (mp) == NOTOK)
1217 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1218 if (annoaux (mp) == NOTOK)
1228 annoaux (struct mailname *mp)
1231 char buffer[BUFSIZ];
1233 snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1234 i = strlen (buffer);
1236 return (write (pfd, buffer, i) == i ? OK : NOTOK);
1241 insert_fcc (struct headers *hdr, unsigned char *pp)
1245 for (cp = pp; isspace (*cp); cp++)
1247 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
1255 adios (NULL, "too many %ss", hdr->value);
1256 fccfold[fccind++] = getcpy (cp);
1264 make_bcc_file (int dashstuff)
1272 tfile = m_mktemp2(NULL, "bccs", NULL, &out);
1273 if (tfile == NULL) adios("bcc", "unable to create temporary file");
1274 chmod (bccfil, 0600);
1275 strncpy (bccfil, tfile, sizeof(bccfil));
1277 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1279 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
1280 (int) getpid (), (long) tclock, LocalName (1));
1282 fprintf (out, "Subject: %s", subject);
1283 fprintf (out, "BCC:\n");
1286 * Use MIME encapsulation for Bcc messages
1292 * Check if any lines in the message clash with the
1293 * prefix for the MIME multipart separator. If there
1294 * is a clash, increment one of the letters in the
1295 * prefix and check again.
1297 if ((cp = strchr(prefix, 'a')) == NULL)
1298 adios (NULL, "lost prefix start");
1299 while (find_prefix () == NOTOK) {
1304 adios (NULL, "can't find a unique delimiter string");
1309 fprintf (out, "%s: %s\n%s: multipart/digest; boundary=\"",
1310 VRSN_FIELD, VRSN_VALUE, TYPE_FIELD);
1311 fprintf (out, "%s\"\n\n--%s\n\n", prefix, prefix);
1313 fprintf (out, "\n------- Blind-Carbon-Copy\n\n");
1319 * Do mhl filtering of Bcc messages instead
1320 * of MIME encapsulation.
1322 if (filter != NULL) {
1323 vec[0] = r1bindex (mhlproc, '/');
1325 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1329 adios ("fork", "unable to");
1332 dup2 (fileno (out), 1);
1335 vec[i++] = "-forward";
1340 /* was the flag -[no]dashstuffing specified? */
1342 vec[i++] = "-dashstuffing";
1343 else if (dashstuff < 0)
1344 vec[i++] = "-nodashstuffing";
1347 execvp (mhlproc, vec);
1348 fprintf (stderr, "unable to exec ");
1353 pidXwait (child_id, mhlproc);
1357 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
1358 adios (tmpfil, "unable to re-open");
1361 * If using MIME encapsulation, or if the -nodashstuffing
1362 * flag was given, then just copy message. Else do
1363 * RFC934 quoting (dashstuffing).
1365 if (mime || dashstuff < 0)
1366 cpydata (fd, fileno (out), tmpfil, bccfil);
1368 cpydgst (fd, fileno (out), tmpfil, bccfil);
1372 fseek (out, 0L, SEEK_END);
1374 fprintf (out, "\n--%s--\n", prefix);
1376 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
1382 * Scan message to check if any lines clash with
1383 * the prefix of the MIME multipart separator.
1390 unsigned char buffer[BUFSIZ];
1393 if ((in = fopen (tmpfil, "r")) == NULL)
1394 adios (tmpfil, "unable to re-open");
1396 while (fgets (buffer, sizeof(buffer) - 1, in))
1397 if (buffer[0] == '-' && buffer[1] == '-') {
1400 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1404 if (strcmp (buffer + 2, prefix) == 0) {
1415 #define plural(x) (x == 1 ? "" : "s")
1420 if (badadr && unkadr)
1421 die (NULL, "%d address%s unparsable, %d addressee%s undeliverable",
1422 badadr, plural (badadr), unkadr, plural (badadr));
1424 die (NULL, "%d address%s unparsable", badadr, plural (badadr));
1426 die (NULL, "%d addressee%s undeliverable", unkadr, plural (unkadr));
1431 do_addresses (int bccque, int talk)
1435 struct mailname *lp;
1438 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1439 if (lp->m_bcc ? bccque : !bccque) {
1441 printf (" -- Local Recipients --\n");
1442 do_an_address (lp, talk);
1447 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1448 if (lp->m_bcc ? bccque : !bccque) {
1450 printf (" -- UUCP Recipients --\n");
1451 do_an_address (lp, talk);
1456 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1457 if (lp->m_bcc ? bccque : !bccque) {
1459 printf (" -- Network Recipients --\n");
1460 do_an_address (lp, talk);
1466 if (rp_isbad (retval = sm_waend ()))
1467 die (NULL, "problem ending addresses; %s", rp_string (retval));
1472 * MTS-SPECIFIC INTERACTION
1477 * SENDMAIL/SMTP routines
1481 post (char *file, int bccque, int talk, char *envelope)
1488 if (msgflags & MINV)
1489 printf (" -- Posting for %s Recipients --\n",
1490 bccque ? "Blind" : "Sighted");
1492 printf (" -- Posting for All Recipients --\n");
1497 if (sm_mts == MTS_SENDMAIL_PIPE) {
1498 char *sargv[16], **argp;
1500 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1504 adios ("fork", "unable to");
1507 if (freopen( file, "r", stdin) == NULL) {
1508 adios (file, "can't reopen for sendmail");
1512 *argp++ = "sendmail";
1513 *argp++ = "-t"; /* read msg for recipients */
1514 *argp++ = "-i"; /* don't stop on "." */
1521 execv (sendmail, sargv);
1522 adios (sendmail, "can't exec");
1525 pidXwait (child_id, NULL);
1529 if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch,
1530 verbose, snoop, queued, sasl,
1531 saslssf, saslmech, user, tls)) ||
1532 rp_isbad (retval = sm_winit (envelope)))
1533 die (NULL, "problem initializing server; %s", rp_string (retval));
1535 do_addresses (bccque, talk && verbose);
1536 if ((fd = open (file, O_RDONLY)) == NOTOK)
1537 die (file, "unable to re-open");
1542 sm_end (!(msgflags & MINV) || bccque ? OK : DONE);
1546 if (msgflags & MINV)
1547 printf (" -- %s Recipient Copies Posted --\n",
1548 bccque ? "Blind" : "Sighted");
1550 printf (" -- Recipient Copies Posted --\n");
1558 /* Address Verification */
1561 verify_all_addresses (int talk, char *envelope)
1564 struct mailname *lp;
1568 if (!whomsw || checksw)
1569 if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch,
1570 verbose, snoop, queued, sasl,
1571 saslssf, saslmech, user, tls))
1572 || rp_isbad (retval = sm_winit (envelope)))
1573 die (NULL, "problem initializing server; %s", rp_string (retval));
1575 if (talk && !whomsw)
1576 printf (" -- Address Verification --\n");
1577 if (talk && localaddrs.m_next)
1578 printf (" -- Local Recipients --\n");
1579 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1580 do_an_address (lp, talk);
1582 if (talk && uuaddrs.m_next)
1583 printf (" -- UUCP Recipients --\n");
1584 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1585 do_an_address (lp, talk);
1587 if (talk && netaddrs.m_next)
1588 printf (" -- Network Recipients --\n");
1589 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1590 do_an_address (lp, talk);
1593 if (talk && !whomsw)
1594 printf (" -- Address Verification Successful --\n");
1596 if (!whomsw || checksw)
1605 do_an_address (struct mailname *lp, int talk)
1611 switch (lp->m_type) {
1615 strncpy (addr, mbox, sizeof(addr));
1619 mbox = auxformat (lp, 0);
1621 snprintf (addr, sizeof(addr), "%s!%s", lp->m_host, lp->m_mbox);
1624 default: /* let SendMail decide if the host is bad */
1627 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1632 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1634 if (whomsw && !checksw) {
1642 switch (retval = sm_wadr (mbox, host,
1643 lp->m_type != UUCPHOST ? lp->m_path : NULL)) {
1646 printf ("address ok\n");
1652 fprintf (stderr, " %s: ", addr);
1653 fprintf (talk ? stdout : stderr, "loses; %s\n",
1654 rp_string (retval));
1660 fprintf (stderr, " %s: ", addr);
1661 die (NULL, "unexpected response; %s", rp_string (retval));
1669 do_text (char *file, int fd)
1674 lseek (fd, (off_t) 0, SEEK_SET);
1676 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1677 if (rp_isbad (retval = sm_wtxt (buf, state)))
1678 die (NULL, "problem writing text; %s\n", rp_string (retval));
1682 die (file, "problem reading from");
1684 switch (retval = sm_wtend ()) {
1690 die (NULL, "posting failed; %s", rp_string (retval));
1693 die (NULL, "unexpected response; %s", rp_string (retval));
1708 if (msgflags & MINV)
1711 if (!whomsw || checksw)
1724 hstat = SIGNAL2 (SIGHUP, sigser);
1725 istat = SIGNAL2 (SIGINT, sigser);
1726 qstat = SIGNAL2 (SIGQUIT, sigser);
1727 tstat = SIGNAL2 (SIGTERM, sigser);
1737 SIGNAL (SIGHUP, hstat);
1738 SIGNAL (SIGINT, istat);
1739 SIGNAL (SIGQUIT, qstat);
1740 SIGNAL (SIGTERM, tstat);
1748 p_refile (char *file)
1756 printf (" -- Filing Folder Copies --\n");
1757 for (i = 0; i < fccind; i++)
1758 fcc (file, fccfold[i]);
1760 printf (" -- Folder Copies Filed --\n");
1765 * Call the `fileproc' to add the file to the folder.
1769 fcc (char *file, char *folder)
1776 printf (" %sFcc %s: ", msgstate == RESENT ? "Resent-" : "", folder);
1779 for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
1785 fprintf (stderr, " %sFcc %s: ",
1786 msgstate == RESENT ? "Resent-" : "", folder);
1787 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
1791 /* see if we need to add `+' */
1792 snprintf (fold, sizeof(fold), "%s%s",
1793 *folder == '+' || *folder == '@' ? "" : "+", folder);
1795 /* now exec the fileproc */
1796 execlp (fileproc, r1bindex (fileproc, '/'),
1797 "-link", "-file", file, fold, NULL);
1801 if ((status = pidwait (child_id, OK))) {
1803 fprintf (stderr, " %sFcc %s: ",
1804 msgstate == RESENT ? "Resent-" : "", folder);
1805 pidstatus (status, verbose ? stdout : stderr, NULL);
1808 printf ("folder ok\n");
1820 die (char *what, char *fmt, ...)
1825 if (msgflags & MINV)
1828 if (!whomsw || checksw)
1832 advertise (what, NULL, fmt, ap);