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
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, unless MTS_PIPE */
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 */
159 #define HONE 0x0800 /* Only (zero or) one address allowed */
160 #define HEFM 0x1000 /* Envelope-From: header */
163 * flags for headers->set
165 #define MFRM 0x0001 /* we've seen a From: */
166 #define MDAT 0x0002 /* we've seen a Date: */
167 #define MRFM 0x0004 /* we've seen a Resent-From: */
168 #define MVIS 0x0008 /* we've seen sighted addrs */
169 #define MINV 0x0010 /* we've seen blind addrs */
170 #define MSND 0x0020 /* we've seen a Sender: */
171 #define MRSN 0x0040 /* We've seen a Resent-Sendr:*/
172 #define MEFM 0x0080 /* We've seen Envelope-From: */
175 static struct headers NHeaders[] = {
176 { "Return-Path", HBAD, 0 },
177 { "Received", HBAD, 0 },
178 { "Reply-To", HADR|HNGR, 0 },
179 { "From", HADR|HNGR, MFRM },
180 { "Sender", HADR|HNGR|HONE, MSND },
182 { "Subject", HSUB, 0 },
183 { "To", HADR|HTRY, MVIS },
184 { "cc", HADR|HTRY, MVIS },
185 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
186 { "Dcc", HADR|HTRY|HDCC|HNIL, MVIS }, /* sorta cc & bcc combined */
187 { "Message-ID", HBAD, 0 },
189 { "Envelope-From", HADR|HONE|HEFM, MEFM },
193 static struct headers RHeaders[] = {
194 { "Resent-Reply-To", HADR|HNGR, 0 },
195 { "Resent-From", HADR|HNGR, MRFM },
196 { "Resent-Sender", HADR|HNGR, MRSN },
197 { "Resent-Date", HBAD, 0 },
198 { "Resent-Subject", HSUB, 0 },
199 { "Resent-To", HADR|HTRY, MVIS },
200 { "Resent-cc", HADR|HTRY, MVIS },
201 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
202 { "Resent-Message-ID", HBAD, 0 },
203 { "Resent-Fcc", HFCC, 0 },
204 { "Reply-To", HADR, 0 },
205 { "From", HADR|HNGR, MFRM },
206 { "Sender", HADR|HNGR, MSND },
207 { "Date", HNOP, MDAT },
208 { "To", HADR|HNIL, 0 },
209 { "cc", HADR|HNIL, 0 },
210 { "Bcc", HADR|HTRY|HBCC|HNIL, 0 },
212 { "Envelope-From", HADR|HONE|HEFM, MEFM },
216 static short fccind = 0; /* index into fccfold[] */
217 static short outputlinelen = OUTPUTLINELEN;
219 static int pfd = NOTOK; /* fd to write annotation list to */
220 static uid_t myuid= -1; /* my user id */
221 static gid_t mygid= -1; /* my group id */
222 static int recipients = 0; /* how many people will get a copy */
223 static int unkadr = 0; /* how many of those were unknown */
224 static int badadr = 0; /* number of bad addrs */
225 static int badmsg = 0; /* message has bad semantics */
226 static int verbose = 0; /* spell it out */
227 static int format = 1; /* format addresses */
228 static int mime = 0; /* use MIME-style encapsulations for Bcc */
229 static int msgid = 0; /* add msgid */
230 static int debug = 0; /* debugging post */
231 static int watch = 0; /* watch the delivery process */
232 static int whomsw = 0; /* we are whom not post */
233 static int checksw = 0; /* whom -check */
234 static int linepos=0; /* putadr()'s position on the line */
235 static int nameoutput=0; /* putadr() has output header name */
236 static int sasl=0; /* Use SASL auth for SMTP */
237 static int saslssf=-1; /* Our maximum SSF for SASL */
238 static char *saslmech=NULL; /* Force use of particular SASL mech */
239 static char *user=NULL; /* Authenticate as this user */
240 static char *port="smtp"; /* Name of server port for SMTP */
241 static int tls=0; /* Use TLS for encryption */
242 static int fromcount=0; /* Count of addresses on From: header */
243 static int seensender=0; /* Have we seen a Sender: header? */
245 static unsigned msgflags = 0; /* what we've seen */
249 static int msgstate = NORMAL;
251 static time_t tclock = 0; /* the time we started (more or less) */
253 static SIGNAL_HANDLER hstat, istat, qstat, tstat;
255 static char tmpfil[BUFSIZ];
256 static char bccfil[BUFSIZ];
258 static char from[BUFSIZ]; /* my network address */
259 static char sender[BUFSIZ]; /* my Sender: header */
260 static char efrom[BUFSIZ]; /* my Envelope-From: header */
261 static char signature[BUFSIZ]; /* my signature */
262 static char *filter = NULL; /* the filter for BCC'ing */
263 static char *subject = NULL; /* the subject field for BCC'ing */
264 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
266 static struct headers *hdrtab; /* table for the message we're doing */
268 static struct mailname localaddrs; /* local addrs */
269 static struct mailname netaddrs; /* network addrs */
270 static struct mailname uuaddrs; /* uucp addrs */
271 static struct mailname tmpaddrs; /* temporary queue */
273 static int snoop = 0;
274 static char *clientsw = NULL;
275 static char *serversw = NULL;
277 extern struct smtp sm_reply;
279 static char prefix[] = "----- =_aaaaaaaaaa";
281 static char *partno = NULL;
282 static int queued = 0;
287 static void putfmt (char *, char *, FILE *);
288 static void start_headers (void);
289 static void finish_headers (FILE *);
290 static int get_header (char *, struct headers *);
291 static int putadr (char *, char *, struct mailname *, FILE *, unsigned int);
292 static void putgrp (char *, char *, FILE *, unsigned int);
293 static int insert (struct mailname *);
294 static void pl (void);
295 static void anno (void);
296 static int annoaux (struct mailname *);
297 static void insert_fcc (struct headers *, unsigned char *);
298 static void make_bcc_file (int);
299 static void verify_all_addresses (int, char *);
300 static void chkadr (void);
301 static void sigon (void);
302 static void sigoff (void);
303 static void p_refile (char *);
304 static void fcc (char *, char *);
305 static void die (char *, char *, ...);
306 static void post (char *, int, int, char *);
307 static void do_text (char *file, int fd);
308 static void do_an_address (struct mailname *, int);
309 static void do_addresses (int, int);
310 static int find_prefix (void);
314 main (int argc, char **argv)
316 int state, compnum, dashstuff = 0;
317 char *cp, *msg = NULL, **argp, **arguments, *envelope;
318 char buf[BUFSIZ], name[NAMESZ];
322 setlocale(LC_ALL, "");
324 invo_name = r1bindex (argv[0], '/');
326 /* foil search of user profile/context */
327 if (context_foil (NULL) == -1)
330 mts_init (invo_name);
331 arguments = getarguments (invo_name, argc, argv, 0);
334 while ((cp = *argp++)) {
336 switch (smatch (++cp, switches)) {
338 ambigsw (cp, switches);
341 adios (NULL, "-%s unknown", cp);
344 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
345 print_help (buf, switches, 0);
348 print_version(invo_name);
352 if (!(cp = *argp++) || *cp == '-')
353 adios (NULL, "missing argument to %s", argp[-2]);
354 /* create a minimal context */
355 if (context_foil (cp) == -1)
360 if (!(cp = *argp++) || *cp == '-')
361 adios (NULL, "missing argument to %s", argp[-2]);
362 if ((state = alias (cp)) != AK_OK)
363 adios (NULL, "aliasing error in %s - %s",
364 cp, akerror (state));
383 if (!(filter = *argp++) || *filter == '-')
384 adios (NULL, "missing argument to %s", argp[-2]);
439 if (!(cp = *argp++) || *cp == '-')
440 adios (NULL, "missing argument to %s", argp[-2]);
441 if ((outputlinelen = atoi (cp)) < 10)
442 adios (NULL, "impossible width %d", outputlinelen);
446 if (!(cp = *argp++) || *cp == '-')
447 adios (NULL, "missing argument to %s", argp[-2]);
448 if ((pfd = atoi (cp)) <= 2)
449 adios (NULL, "bad argument %s %s", argp[-2], cp);
453 if (!(clientsw = *argp++) || *clientsw == '-')
454 adios (NULL, "missing argument to %s", argp[-2]);
457 if (!(serversw = *argp++) || *serversw == '-')
458 adios (NULL, "missing argument to %s", argp[-2]);
465 if (!(partno = *argp++) || *partno == '-')
466 adios (NULL, "missing argument to %s", argp[-2]);
482 if (!(cp = *argp++) || *cp == '-')
483 adios (NULL, "missing argument to %s", argp[-2]);
488 if (!(saslmech = *argp++) || *saslmech == '-')
489 adios (NULL, "missing argument to %s", argp[-2]);
493 if (!(user = *argp++) || *user == '-')
494 adios (NULL, "missing argument to %s", argp[-2]);
498 if (!(port = *argp++) || *port == '-')
499 adios (NULL, "missing argument to %s", argp[-2]);
511 if (!(cp = *argp++) || *cp == '-')
512 adios (NULL, "missing argument to %s", argp[-2]);
517 if (!(cp = *argp++) || *cp == '-')
518 adios (NULL, "missing argument to %s", argp[-2]);
524 adios (NULL, "only one message at a time!");
532 adios (NULL, "usage: %s [switches] file", invo_name);
534 if (outputlinelen < 10)
535 adios (NULL, "impossible width %d", outputlinelen);
537 if ((in = fopen (msg, "r")) == NULL)
538 adios (msg, "unable to open");
543 discard (out = stdout); /* XXX: reference discard() to help loader */
546 if ((out = fopen ("/dev/null", "w")) == NULL)
547 adios ("/dev/null", "unable to open");
549 char *cp = m_mktemp(m_maildir(invo_name), NULL, &out);
551 cp = m_mktemp2(NULL, invo_name, NULL, &out);
553 adios ("post", "unable to create temporary file");
556 strncpy(tmpfil, cp, sizeof(tmpfil));
557 chmod (tmpfil, 0600);
561 hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
563 for (compnum = 1, state = FLD;;) {
564 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
569 cp = add (buf, NULL);
570 while (state == FLDPLUS) {
571 state = m_getfld (state, name, buf, sizeof(buf), in);
574 putfmt (name, cp, out);
578 finish_headers (out);
583 finish_headers (out);
586 fprintf (out, "\n%s", buf);
587 while (state == BODY) {
588 state = m_getfld (state, name, buf, sizeof(buf), in);
594 finish_headers (out);
599 adios (NULL, "message format error in component #%d", compnum);
602 adios (NULL, "getfld() returned %d", state);
619 * Here's how we decide which address to use as the envelope-from
622 * - If we were given an Envelope-From header, use that.
623 * - If we were given a Sender: address, use that.
624 * - Otherwise, use the address on the From: line
627 if (msgflags & MEFM) {
629 } else if (seensender) {
635 /* If we are doing a "whom" check */
637 /* This won't work with MTS_PIPE. */
638 verify_all_addresses (1, envelope);
642 if (msgflags & MINV) {
643 make_bcc_file (dashstuff);
644 if (msgflags & MVIS) {
645 if (sm_mts != MTS_PIPE) {
646 /* It would be nice to have support to call
647 verify_all_addresses with MTS_PIPE, but that might
648 require running sendmail as root. Note that spost
649 didn't verify addresses. */
650 verify_all_addresses (verbose, envelope);
652 post (tmpfil, 0, verbose, envelope);
654 post (bccfil, 1, verbose, envelope);
657 post (tmpfil, 0, isatty (1), envelope);
664 printf (partno ? "Partial Message #%s Processed\n" : "Message Processed\n",
676 putfmt (char *name, char *str, FILE *out)
678 int count, grp, i, keep;
681 struct mailname *mp = NULL, *np = NULL;
684 while (*str == ' ' || *str == '\t')
687 if (msgstate == NORMAL && uprf (name, "resent")) {
688 advise (NULL, "illegal header line -- %s:", name);
693 if ((i = get_header (name, hdrtab)) == NOTOK) {
694 fprintf (out, "%s: %s", name, str);
699 if (hdr->flags & HIGN) {
702 if (hdr->flags & HBAD) {
703 advise (NULL, "illegal header line -- %s:", name);
707 msgflags |= (hdr->set & ~(MVIS | MINV));
709 if (hdr->flags & HSUB)
710 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
711 if (hdr->flags & HFCC) {
712 if ((cp = strrchr(str, '\n')))
714 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
716 insert_fcc (hdr, pp);
718 insert_fcc (hdr, pp);
722 if (!(hdr->flags & HADR)) {
723 fprintf (out, "%s: %s", name, str);
727 tmpaddrs.m_next = NULL;
728 for (count = 0; (cp = getname (str)); count++)
729 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
733 tmpaddrs.m_next = mp;
737 if (hdr->flags & HTRY)
743 if (hdr->flags & HNIL)
744 fprintf (out, "%s: %s", name, str);
747 * Sender (or Resent-Sender) can have only one address
749 if ((msgstate == RESENT) ? (hdr->set & MRSN)
750 : (hdr->set & MSND)) {
751 advise (NULL, "%s: field requires one address", name);
755 advise (NULL, "%s: field requires at least one address", name);
762 if (count > 1 && (hdr->flags & HONE)) {
763 advise (NULL, "%s: field only permits one address", name);
768 nameoutput = linepos = 0;
769 snprintf (namep, sizeof(namep), "%s%s",
770 (hdr->flags & HMNG) ? "Original-" : "", name);
772 for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
773 if (mp->m_nohost) { /* also used to test (hdr->flags & HTRY) */
774 /* The address doesn't include a host, so it might be an alias. */
775 pp = akvalue (mp->m_mbox); /* do mh alias substitution */
776 qp = akvisible () ? mp->m_mbox : "";
779 putgrp (namep, np->m_gname, out, hdr->flags);
780 while ((cp = getname (pp))) {
781 if (!(mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
787 * If it's a From: or Resent-From: header, save the address
788 * for later possible use (as the envelope address for SMTP)
791 if ((msgstate == RESENT) ? (hdr->set & MRFM)
792 : (hdr->set & MFRM)) {
793 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
794 from[sizeof(from) - 1] = '\0';
799 * Also save the Sender: or Resent-Sender: header as well
802 if ((msgstate == RESENT) ? (hdr->set & MRSN)
803 : (hdr->set & MSND)) {
804 strncpy(sender, auxformat(mp, 0), sizeof(sender) - 1);
805 sender[sizeof(sender) - 1] = '\0';
810 * ALSO ... save Envelope-From
813 if (hdr->set & MEFM) {
814 strncpy(efrom, auxformat(mp, 0), sizeof(efrom) - 1);
815 efrom[sizeof(efrom) - 1] = '\0';
818 if (hdr->flags & HBCC)
821 mp->m_ingrp = np->m_ingrp;
824 putgrp (namep, mp->m_gname, out, hdr->flags);
827 if (putadr (namep, qp, mp, out, hdr->flags))
828 msgflags |= (hdr->set & (MVIS | MINV));
837 /* Address includes a host, so no alias substitution is needed. */
840 * If it's a From: or Resent-From header, save the address
841 * for later possible use (as the envelope address for SMTP)
844 if ((msgstate == RESENT) ? (hdr->set & MRFM)
845 : (hdr->set & MFRM)) {
846 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
851 * Also save the Sender: header as well
854 if ((msgstate == RESENT) ? (hdr->set & MRSN)
855 : (hdr->set & MSND)) {
856 strncpy(sender, auxformat(mp, 0), sizeof(sender) - 1);
857 sender[sizeof(sender) - 1] = '\0';
862 * ALSO ... save Envelope-From
865 if (hdr->set & MEFM) {
866 strncpy(efrom, auxformat(mp, 0), sizeof(efrom) - 1);
867 efrom[sizeof(efrom) - 1] = '\0';
870 if (hdr->flags & HBCC)
873 putgrp (namep, mp->m_gname, out, hdr->flags);
876 keep = putadr (namep, "", mp, out, hdr->flags);
880 msgflags |= (hdr->set & (MVIS | MINV));
886 if (grp > 0 && (hdr->flags & HNGR)) {
887 advise (NULL, "%s: field does not allow groups", name);
908 * Probably not necessary, but just in case ...
915 if ((cp = getfullname ()) && *cp) {
916 strncpy (sigbuf, cp, sizeof(sigbuf));
917 snprintf (signature, sizeof(signature), "%s <%s>",
918 sigbuf, getlocaladdr());
919 if ((cp = getname (signature)) == NULL)
920 adios (NULL, "getname () failed -- you lose extraordinarily big");
921 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL)) == NULL)
922 adios (NULL, "bad signature '%s'", sigbuf);
927 strncpy (signature, getlocaladdr(), sizeof(signature));
933 * Now that we've outputted the header fields in the draft
934 * message, we will now output any remaining header fields
935 * that we need to add/create.
939 finish_headers (FILE *out)
943 if (!(msgflags & MFRM)) {
945 * A From: header is now required in the draft.
947 advise (NULL, "message has no From: header");
948 advise (NULL, "See default components files for examples");
953 if (fromcount > 1 && (seensender == 0 && !(msgflags & MEFM))) {
954 advise (NULL, "A Sender: or Envelope-From: header is required "
955 "with multiple\nFrom: addresses");
963 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
965 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
966 (int) getpid (), (long) tclock, LocalName (1));
968 * If we have multiple From: addresses, make sure we have an
969 * Sender: header. If we don't have one, then generate one
970 * from Envelope-From: (which in this case, cannot be blank)
973 if (fromcount > 1 && seensender == 0) {
974 if (efrom[0] == '\0') {
975 advise (NULL, "Envelope-From cannot be blank when there "
976 "is multiple From: addresses\nand no Sender: "
980 fprintf (out, "Sender: %s\n", efrom);
984 if (!(msgflags & MVIS))
985 fprintf (out, "Bcc: Blind Distribution List: ;\n");
989 if (!(msgflags & MDAT)) {
990 advise (NULL, "message has no Date: header");
993 if (!(msgflags & MFRM)) {
994 advise (NULL, "message has no From: header");
997 if (!(msgflags & MRFM)) {
998 advise (NULL, "message has no Resent-From: header");
999 advise (NULL, "See default components files for examples");
1003 if (fromcount > 1 && (seensender == 0 && !(msgflags & MEFM))) {
1004 advise (NULL, "A Resent-Sender: or Envelope-From: header is "
1005 "required with multiple\nResent-From: addresses");
1013 fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
1015 fprintf (out, "Resent-Message-ID: <%d.%ld@%s>\n",
1016 (int) getpid (), (long) tclock, LocalName (1));
1018 * If we have multiple Resent-From: addresses, make sure we have an
1019 * Resent-Sender: header. If we don't have one, then generate one
1020 * from Envelope-From (which in this case, cannot be blank)
1023 if (fromcount > 1 && seensender == 0) {
1024 if (efrom[0] == '\0') {
1025 advise (NULL, "Envelope-From cannot be blank when there "
1026 "is multiple Resent-From: addresses and no "
1027 "Resent-Sender: header");
1030 fprintf (out, "Resent-Sender: %s\n", efrom);
1034 if (!(msgflags & MVIS))
1035 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
1040 adios (NULL, "re-format message and try again");
1042 adios (NULL, "no addressees");
1047 get_header (char *header, struct headers *table)
1051 for (h = table; h->value; h++)
1052 if (!mh_strcasecmp (header, h->value))
1060 putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flags)
1064 char buffer[BUFSIZ];
1066 if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
1068 if (sm_mts != MTS_PIPE && ((flags & (HBCC | HDCC | HEFM)) || mp->m_ingrp))
1072 fprintf (out, "%s: ", name);
1073 linepos += (nameoutput = strlen (name) + 2);
1076 if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
1077 mp->m_pers = getcpy (aka);
1080 snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
1083 cp = adrformat (mp);
1090 if (linepos != nameoutput) {
1091 if (len + linepos + 2 > outputlinelen)
1092 fprintf (out, ",\n%*s", linepos = nameoutput, "");
1102 return (flags & HTRY);
1107 putgrp (char *name, char *group, FILE *out, unsigned int flags)
1112 if (sm_mts != MTS_PIPE && (flags & HBCC))
1116 fprintf (out, "%s: ", name);
1117 linepos += (nameoutput = strlen (name) + 2);
1120 cp = concat (group, ";", NULL);
1123 if (linepos > nameoutput) {
1124 if (len + linepos + 2 > outputlinelen) {
1125 fprintf (out, ",\n%*s", nameoutput, "");
1126 linepos = nameoutput;
1140 insert (struct mailname *np)
1142 struct mailname *mp;
1144 if (np->m_mbox == NULL)
1147 for (mp = np->m_type == LOCALHOST ? &localaddrs
1148 : np->m_type == UUCPHOST ? &uuaddrs
1152 if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
1153 && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox)
1154 && np->m_bcc == mp->m_next->m_bcc)
1167 struct mailname *mp;
1169 printf ("-------\n\t-- Addresses --\nlocal:\t");
1170 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1171 printf ("%s%s%s", mp->m_mbox,
1172 mp->m_bcc ? "[BCC]" : "",
1173 mp->m_next ? ",\n\t" : "");
1175 printf ("\nnet:\t");
1176 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1177 printf ("%s%s@%s%s%s", mp->m_path ? mp->m_path : "",
1178 mp->m_mbox, mp->m_host,
1179 mp->m_bcc ? "[BCC]" : "",
1180 mp->m_next ? ",\n\t" : "");
1182 printf ("\nuucp:\t");
1183 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1184 printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1185 mp->m_bcc ? "[BCC]" : "",
1186 mp->m_next ? ",\n\t" : "");
1188 printf ("\n\t-- Folder Copies --\nfcc:\t");
1189 for (i = 0; i < fccind; i++)
1190 printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1198 struct mailname *mp;
1200 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1201 if (annoaux (mp) == NOTOK)
1204 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1205 if (annoaux (mp) == NOTOK)
1208 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1209 if (annoaux (mp) == NOTOK)
1219 annoaux (struct mailname *mp)
1222 char buffer[BUFSIZ];
1224 snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1225 i = strlen (buffer);
1227 return (write (pfd, buffer, i) == i ? OK : NOTOK);
1232 insert_fcc (struct headers *hdr, unsigned char *pp)
1236 for (cp = pp; isspace (*cp); cp++)
1238 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
1246 adios (NULL, "too many %ss", hdr->value);
1247 fccfold[fccind++] = getcpy (cp);
1255 make_bcc_file (int dashstuff)
1263 tfile = m_mktemp2(NULL, "bccs", NULL, &out);
1264 if (tfile == NULL) adios("bcc", "unable to create temporary file");
1265 chmod (bccfil, 0600);
1266 strncpy (bccfil, tfile, sizeof(bccfil));
1268 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1270 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
1271 (int) getpid (), (long) tclock, LocalName (1));
1273 fprintf (out, "Subject: %s", subject);
1274 fprintf (out, "BCC:\n");
1277 * Use MIME encapsulation for Bcc messages
1283 * Check if any lines in the message clash with the
1284 * prefix for the MIME multipart separator. If there
1285 * is a clash, increment one of the letters in the
1286 * prefix and check again.
1288 if ((cp = strchr(prefix, 'a')) == NULL)
1289 adios (NULL, "lost prefix start");
1290 while (find_prefix () == NOTOK) {
1295 adios (NULL, "can't find a unique delimiter string");
1300 fprintf (out, "%s: %s\n%s: multipart/digest; boundary=\"",
1301 VRSN_FIELD, VRSN_VALUE, TYPE_FIELD);
1302 fprintf (out, "%s\"\n\n--%s\n\n", prefix, prefix);
1304 fprintf (out, "\n------- Blind-Carbon-Copy\n\n");
1310 * Do mhl filtering of Bcc messages instead
1311 * of MIME encapsulation.
1313 if (filter != NULL) {
1314 vec[0] = r1bindex (mhlproc, '/');
1316 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1320 adios ("fork", "unable to");
1323 dup2 (fileno (out), 1);
1326 vec[i++] = "-forward";
1331 /* was the flag -[no]dashstuffing specified? */
1333 vec[i++] = "-dashstuffing";
1334 else if (dashstuff < 0)
1335 vec[i++] = "-nodashstuffing";
1338 execvp (mhlproc, vec);
1339 fprintf (stderr, "unable to exec ");
1344 pidXwait (child_id, mhlproc);
1348 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
1349 adios (tmpfil, "unable to re-open");
1352 * If using MIME encapsulation, or if the -nodashstuffing
1353 * flag was given, then just copy message. Else do
1354 * RFC934 quoting (dashstuffing).
1356 if (mime || dashstuff < 0)
1357 cpydata (fd, fileno (out), tmpfil, bccfil);
1359 cpydgst (fd, fileno (out), tmpfil, bccfil);
1363 fseek (out, 0L, SEEK_END);
1365 fprintf (out, "\n--%s--\n", prefix);
1367 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
1373 * Scan message to check if any lines clash with
1374 * the prefix of the MIME multipart separator.
1381 unsigned char buffer[BUFSIZ];
1384 if ((in = fopen (tmpfil, "r")) == NULL)
1385 adios (tmpfil, "unable to re-open");
1387 while (fgets (buffer, sizeof(buffer) - 1, in))
1388 if (buffer[0] == '-' && buffer[1] == '-') {
1391 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1395 if (strcmp (buffer + 2, prefix) == 0) {
1406 #define plural(x) (x == 1 ? "" : "s")
1411 if (badadr && unkadr)
1412 die (NULL, "%d address%s unparsable, %d addressee%s undeliverable",
1413 badadr, plural (badadr), unkadr, plural (badadr));
1415 die (NULL, "%d address%s unparsable", badadr, plural (badadr));
1417 die (NULL, "%d addressee%s undeliverable", unkadr, plural (unkadr));
1422 do_addresses (int bccque, int talk)
1426 struct mailname *lp;
1429 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1430 if (lp->m_bcc ? bccque : !bccque) {
1432 printf (" -- Local Recipients --\n");
1433 do_an_address (lp, talk);
1438 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1439 if (lp->m_bcc ? bccque : !bccque) {
1441 printf (" -- UUCP Recipients --\n");
1442 do_an_address (lp, talk);
1447 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1448 if (lp->m_bcc ? bccque : !bccque) {
1450 printf (" -- Network Recipients --\n");
1451 do_an_address (lp, talk);
1457 if (rp_isbad (retval = sm_waend ()))
1458 die (NULL, "problem ending addresses; %s", rp_string (retval));
1463 * MTS-SPECIFIC INTERACTION
1468 * SENDMAIL/SMTP routines
1472 post (char *file, int bccque, int talk, char *envelope)
1478 onex = !(msgflags & MINV) || bccque;
1480 if (msgflags & MINV)
1481 printf (" -- Posting for %s Recipients --\n",
1482 bccque ? "Blind" : "Sighted");
1484 printf (" -- Posting for All Recipients --\n");
1489 if (sm_mts == MTS_PIPE) {
1490 char *sargv[16], **argp;
1492 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1496 adios ("fork", "unable to");
1499 if (freopen( file, "r", stdin) == NULL) {
1500 adios (file, "can't reopen for sendmail");
1504 *argp++ = "sendmail";
1505 *argp++ = "-m"; /* send to me too */
1506 *argp++ = "-t"; /* read msg for recipients */
1507 *argp++ = "-i"; /* don't stop on "." */
1514 execv (sendmail, sargv);
1515 adios (sendmail, "can't exec");
1518 pidXwait (child_id, NULL);
1522 if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch,
1523 verbose, snoop, onex, queued, sasl,
1524 saslssf, saslmech, user, tls)) ||
1525 rp_isbad (retval = sm_winit (envelope)))
1526 die (NULL, "problem initializing server; %s", rp_string (retval));
1528 do_addresses (bccque, talk && verbose);
1529 if ((fd = open (file, O_RDONLY)) == NOTOK)
1530 die (file, "unable to re-open");
1535 sm_end (onex ? OK : DONE);
1539 if (msgflags & MINV)
1540 printf (" -- %s Recipient Copies Posted --\n",
1541 bccque ? "Blind" : "Sighted");
1543 printf (" -- Recipient Copies Posted --\n");
1551 /* Address Verification */
1554 verify_all_addresses (int talk, char *envelope)
1557 struct mailname *lp;
1561 if (!whomsw || checksw)
1562 if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch,
1563 verbose, snoop, 0, queued, sasl,
1564 saslssf, saslmech, user, tls))
1565 || rp_isbad (retval = sm_winit (envelope)))
1566 die (NULL, "problem initializing server; %s", rp_string (retval));
1568 if (talk && !whomsw)
1569 printf (" -- Address Verification --\n");
1570 if (talk && localaddrs.m_next)
1571 printf (" -- Local Recipients --\n");
1572 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1573 do_an_address (lp, talk);
1575 if (talk && uuaddrs.m_next)
1576 printf (" -- UUCP Recipients --\n");
1577 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1578 do_an_address (lp, talk);
1580 if (talk && netaddrs.m_next)
1581 printf (" -- Network Recipients --\n");
1582 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1583 do_an_address (lp, talk);
1586 if (talk && !whomsw)
1587 printf (" -- Address Verification Successful --\n");
1589 if (!whomsw || checksw)
1598 do_an_address (struct mailname *lp, int talk)
1604 switch (lp->m_type) {
1608 strncpy (addr, mbox, sizeof(addr));
1612 mbox = auxformat (lp, 0);
1614 snprintf (addr, sizeof(addr), "%s!%s", lp->m_host, lp->m_mbox);
1617 default: /* let SendMail decide if the host is bad */
1620 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1625 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1627 if (whomsw && !checksw) {
1635 switch (retval = sm_wadr (mbox, host,
1636 lp->m_type != UUCPHOST ? lp->m_path : NULL)) {
1639 printf ("address ok\n");
1645 fprintf (stderr, " %s: ", addr);
1646 fprintf (talk ? stdout : stderr, "loses; %s\n",
1647 rp_string (retval));
1653 fprintf (stderr, " %s: ", addr);
1654 die (NULL, "unexpected response; %s", rp_string (retval));
1662 do_text (char *file, int fd)
1667 lseek (fd, (off_t) 0, SEEK_SET);
1669 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1670 if (rp_isbad (retval = sm_wtxt (buf, state)))
1671 die (NULL, "problem writing text; %s\n", rp_string (retval));
1675 die (file, "problem reading from");
1677 switch (retval = sm_wtend ()) {
1683 die (NULL, "posting failed; %s", rp_string (retval));
1686 die (NULL, "unexpected response; %s", rp_string (retval));
1701 if (msgflags & MINV)
1704 if (!whomsw || checksw)
1717 hstat = SIGNAL2 (SIGHUP, sigser);
1718 istat = SIGNAL2 (SIGINT, sigser);
1719 qstat = SIGNAL2 (SIGQUIT, sigser);
1720 tstat = SIGNAL2 (SIGTERM, sigser);
1730 SIGNAL (SIGHUP, hstat);
1731 SIGNAL (SIGINT, istat);
1732 SIGNAL (SIGQUIT, qstat);
1733 SIGNAL (SIGTERM, tstat);
1741 p_refile (char *file)
1749 printf (" -- Filing Folder Copies --\n");
1750 for (i = 0; i < fccind; i++)
1751 fcc (file, fccfold[i]);
1753 printf (" -- Folder Copies Filed --\n");
1758 * Call the `fileproc' to add the file to the folder.
1762 fcc (char *file, char *folder)
1769 printf (" %sFcc %s: ", msgstate == RESENT ? "Resent-" : "", folder);
1772 for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
1778 fprintf (stderr, " %sFcc %s: ",
1779 msgstate == RESENT ? "Resent-" : "", folder);
1780 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
1784 /* see if we need to add `+' */
1785 snprintf (fold, sizeof(fold), "%s%s",
1786 *folder == '+' || *folder == '@' ? "" : "+", folder);
1788 /* now exec the fileproc */
1789 execlp (fileproc, r1bindex (fileproc, '/'),
1790 "-link", "-file", file, fold, NULL);
1794 if ((status = pidwait (child_id, OK))) {
1796 fprintf (stderr, " %sFcc %s: ",
1797 msgstate == RESENT ? "Resent-" : "", folder);
1798 pidstatus (status, verbose ? stdout : stderr, NULL);
1801 printf ("folder ok\n");
1813 die (char *what, char *fmt, ...)
1818 if (msgflags & MINV)
1821 if (!whomsw || checksw)
1825 advertise (what, NULL, fmt, ap);