3 * post.c -- enter messages into the mail transport system
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 #include <h/signals.h>
15 #include <h/addrsbr.h>
16 #include <h/aliasbr.h>
17 #include <h/dropsbr.h>
28 #ifdef TIME_WITH_SYS_TIME
29 # include <sys/time.h>
32 # ifdef TM_IN_SYS_TIME
33 # include <sys/time.h>
40 # include <mts/smtp/smtp.h>
44 # define SASLminc(a) (a)
45 #else /* CYRUS_SASL */
46 # define SASLminc(a) 0
47 #endif /* CYRUS_SASL */
49 #define FCCS 10 /* max number of fccs allowed */
51 #define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : c)
53 /* In the following array of structures, the numeric second field of the
54 structures (minchars) is apparently used like this:
56 -# : Switch can be abbreviated to # characters; switch hidden in -help.
57 0 : Switch can't be abbreviated; switch shown in -help.
58 # : Switch can be abbreviated to # characters; switch shown in -help. */
60 static struct swit switches[] = {
62 { "alias aliasfile", 0 },
64 { "check", -5 }, /* interface from whom */
66 { "nocheck", -7 }, /* interface from whom */
70 { "dist", -4 }, /* interface from dist */
72 { "filter filterfile", 0 },
80 { "library directory", -7 }, /* interface from send, whom */
98 { "whom", -4 }, /* interface from whom */
100 { "width columns", 0 },
105 #define BITSTUFFSW 22
106 { "dashstuffing", -12 }, /* should we dashstuff BCC messages? */
107 #define NBITSTUFFSW 23
108 { "nodashstuffing", -14 },
110 { "mail", -4 }, /* specify MAIL smtp mode */
112 { "saml", -4 }, /* specify SAML smtp mode */
114 { "send", -4 }, /* specify SEND smtp mode */
116 { "soml", -4 }, /* specify SOML smtp mode */
118 { "idanno number", -6 }, /* interface from send */
120 { "deliver address-list", -7 },
122 { "client host", -6 },
124 { "server host", -6 }, /* specify alternate SMTP server */
126 { "snoop", -5 }, /* snoop the SMTP transaction */
128 { "fill-in file", -7 },
136 { "sasl", SASLminc(-4) },
137 #define SASLMECHSW 38
138 { "saslmech", SASLminc(-5) },
140 { "user", SASLminc(-4) },
152 * flags for headers->flags
154 #define HNOP 0x0000 /* just used to keep .set around */
155 #define HBAD 0x0001 /* bad header - don't let it through */
156 #define HADR 0x0002 /* header has an address field */
157 #define HSUB 0x0004 /* Subject: header */
158 #define HTRY 0x0008 /* try to send to addrs on header */
159 #define HBCC 0x0010 /* don't output this header */
160 #define HMNG 0x0020 /* munge this header */
161 #define HNGR 0x0040 /* no groups allowed in this header */
162 #define HFCC 0x0080 /* FCC: type header */
163 #define HNIL 0x0100 /* okay for this header not to have addrs */
164 #define HIGN 0x0200 /* ignore this header */
165 #define HDCC 0x0400 /* another undocumented feature */
168 * flags for headers->set
170 #define MFRM 0x0001 /* we've seen a From: */
171 #define MDAT 0x0002 /* we've seen a Date: */
172 #define MRFM 0x0004 /* we've seen a Resent-From: */
173 #define MVIS 0x0008 /* we've seen sighted addrs */
174 #define MINV 0x0010 /* we've seen blind addrs */
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|HBAD, 0 },
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 },
194 static struct headers RHeaders[] = {
195 { "Resent-Reply-To", HADR|HNGR, 0 },
196 { "Resent-From", HADR|HNGR, MRFM },
197 { "Resent-Sender", HADR|HBAD, 0 },
198 { "Resent-Date", HBAD, 0 },
199 { "Resent-Subject", HSUB, 0 },
200 { "Resent-To", HADR|HTRY, MVIS },
201 { "Resent-cc", HADR|HTRY, MVIS },
202 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
203 { "Resent-Message-ID", HBAD, 0 },
204 { "Resent-Fcc", HFCC, 0 },
205 { "Reply-To", HADR, 0 },
206 { "From", HADR|HNGR, MFRM },
207 { "Sender", HADR|HNGR, 0 },
208 { "Date", HNOP, MDAT },
209 { "To", HADR|HNIL, 0 },
210 { "cc", HADR|HNIL, 0 },
211 { "Bcc", HADR|HTRY|HBCC|HNIL, 0 },
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 char *saslmech=NULL; /* Force use of particular SASL mech */
238 static char *user=NULL; /* Authenticate as this user */
240 static unsigned msgflags = 0; /* what we've seen */
244 static int msgstate = NORMAL;
246 static time_t tclock = 0; /* the time we started (more or less) */
248 static SIGNAL_HANDLER hstat, istat, qstat, tstat;
250 static char tmpfil[BUFSIZ];
251 static char bccfil[BUFSIZ];
253 static char from[BUFSIZ]; /* my network address */
254 static char signature[BUFSIZ]; /* my signature */
255 static char *filter = NULL; /* the filter for BCC'ing */
256 static char *subject = NULL; /* the subject field for BCC'ing */
257 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
259 static struct headers *hdrtab; /* table for the message we're doing */
261 static struct mailname localaddrs={NULL}; /* local addrs */
262 static struct mailname netaddrs={NULL}; /* network addrs */
263 static struct mailname uuaddrs={NULL}; /* uucp addrs */
264 static struct mailname tmpaddrs={NULL}; /* temporary queue */
267 static int snoop = 0;
268 static int smtpmode = S_MAIL;
269 static char *clientsw = NULL;
270 static char *serversw = NULL;
272 extern struct smtp sm_reply;
275 static char prefix[] = "----- =_aaaaaaaaaa";
277 static int fill_up = 0;
278 static char *fill_in = NULL;
279 static char *partno = NULL;
280 static int queued = 0;
282 extern boolean draft_from_masquerading; /* defined in mts.c */
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);
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);
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;
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 (!(cp = *argp++) || *cp == '-')
454 adios (NULL, "missing argument to %s", argp[-2]);
460 if (!(cp = *argp++) || *cp == '-')
461 adios (NULL, "missing argument to %s", argp[-2]);
480 if (!(clientsw = *argp++) || *clientsw == '-')
481 adios (NULL, "missing argument to %s", argp[-2]);
484 if (!(serversw = *argp++) || *serversw == '-')
485 adios (NULL, "missing argument to %s", argp[-2]);
493 if (!(fill_in = *argp++) || *fill_in == '-')
494 adios (NULL, "missing argument to %s", argp[-2]);
500 if (!(partno = *argp++) || *partno == '-')
501 adios (NULL, "missing argument to %s", argp[-2]);
513 if (!(saslmech = *argp++) || *saslmech == '-')
514 adios (NULL, "missing argument to %s", argp[-2]);
518 if (!(user = *argp++) || *user == '-')
519 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 (fill_in ? fill_in : "/dev/null", "w")) == NULL)
547 adios ("/dev/null", "unable to open");
549 strncpy (tmpfil, m_scratch ("", m_maildir (invo_name)),
551 if ((out = fopen (tmpfil, "w")) == NULL) {
552 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
553 if ((out = fopen (tmpfil, "w")) == NULL)
554 adios (tmpfil, "unable to create");
556 chmod (tmpfil, 0600);
560 hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
562 for (compnum = 1, state = FLD;;) {
563 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
568 cp = add (buf, NULL);
569 while (state == FLDPLUS) {
570 state = m_getfld (state, name, buf, sizeof(buf), in);
573 putfmt (name, cp, out);
577 finish_headers (out);
582 finish_headers (out);
583 if (whomsw && !fill_in)
585 fprintf (out, "\n%s", buf);
586 while (state == BODY) {
587 state = m_getfld (state, name, buf, sizeof(buf), in);
593 finish_headers (out);
598 adios (NULL, "message format error in component #%d", compnum);
601 adios (NULL, "getfld() returned %d", state);
617 /* If we are doing a "whom" check */
620 verify_all_addresses (1);
624 if (msgflags & MINV) {
625 make_bcc_file (dashstuff);
626 if (msgflags & MVIS) {
627 verify_all_addresses (verbose);
628 post (tmpfil, 0, verbose);
630 post (bccfil, 1, verbose);
633 post (tmpfil, 0, isatty (1));
640 printf (partno ? "Partial Message #%s Processed\n" : "Message Processed\n",
652 putfmt (char *name, char *str, FILE *out)
654 int count, grp, i, keep;
657 struct mailname *mp, *np;
660 while (*str == ' ' || *str == '\t')
663 if (msgstate == NORMAL && uprf (name, "resent")) {
664 advise (NULL, "illegal header line -- %s:", name);
669 if ((i = get_header (name, hdrtab)) == NOTOK) {
670 fprintf (out, "%s: %s", name, str);
675 if (hdr->flags & HIGN) {
677 fprintf (out, "%s: %s", name, str);
680 if (hdr->flags & HBAD) {
682 fprintf (out, "%s: %s", name, str);
684 advise (NULL, "illegal header line -- %s:", name);
689 msgflags |= (hdr->set & ~(MVIS | MINV));
691 if (hdr->flags & HSUB)
692 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
693 if (hdr->flags & HFCC) {
695 fprintf (out, "%s: %s", name, str);
699 if ((cp = strrchr(str, '\n')))
701 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
703 insert_fcc (hdr, pp);
705 insert_fcc (hdr, pp);
709 if (!(hdr->flags & HADR)) {
710 fprintf (out, "%s: %s", name, str);
714 tmpaddrs.m_next = NULL;
715 for (count = 0; (cp = getname (str)); count++)
716 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
720 tmpaddrs.m_next = mp;
724 if (hdr->flags & HTRY)
730 if (hdr->flags & HNIL)
731 fprintf (out, "%s: %s", name, str);
734 advise (NULL, "%s: field requires at least one address", name);
741 nameoutput = linepos = 0;
742 snprintf (namep, sizeof(namep), "%s%s",
743 !fill_in && (hdr->flags & HMNG) ? "Original-" : "", name);
745 for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
746 if (mp->m_nohost) { /* also used to test (hdr->flags & HTRY) */
747 /* The address doesn't include a host, so it might be an alias. */
748 pp = akvalue (mp->m_mbox); /* do mh alias substitution */
749 qp = akvisible () ? mp->m_mbox : "";
752 putgrp (namep, np->m_gname, out, hdr->flags);
753 while ((cp = getname (pp))) {
754 if (!(mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
759 if (draft_from_masquerading && ((msgstate == RESENT)
761 : (hdr->set & MFRM)))
762 /* The user manually specified a [Resent-]From: address in
763 their draft and the "masquerade:" line in mts.conf
764 doesn't contain "draft_from", so we'll set things up to
765 use the actual email address embedded in the draft
766 [Resent-]From: (after alias substitution, and without the
767 GECOS full name or angle brackets) as the envelope
769 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
771 if (hdr->flags & HBCC)
774 mp->m_ingrp = np->m_ingrp;
777 putgrp (namep, mp->m_gname, out, hdr->flags);
780 if (putadr (namep, qp, mp, out, hdr->flags))
781 msgflags |= (hdr->set & (MVIS | MINV));
790 /* Address includes a host, so no alias substitution is needed. */
791 if (draft_from_masquerading && ((msgstate == RESENT)
793 : (hdr->set & MFRM)))
794 /* The user manually specified a [Resent-]From: address in
795 their draft and the "masquerade:" line in mts.conf
796 doesn't contain "draft_from", so we'll set things up to
797 use the actual email address embedded in the draft
798 [Resent-]From: (after alias substitution, and without the
799 GECOS full name or angle brackets) as the envelope
801 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
803 if (hdr->flags & HBCC)
806 putgrp (namep, mp->m_gname, out, hdr->flags);
809 keep = putadr (namep, "", mp, out, hdr->flags);
813 msgflags |= (hdr->set & (MVIS | MINV));
819 if (grp > 0 && (hdr->flags & HNGR)) {
820 advise (NULL, "%s: field does not allow groups", name);
824 if (fill_in && grp > 0)
835 char myhost[BUFSIZ], sigbuf[BUFSIZ];
842 strncpy (from, adrsprintf (NULL, NULL), sizeof(from));
843 strncpy (myhost, LocalName (), sizeof(myhost));
845 for (cp = myhost; *cp; cp++)
848 if ((cp = getfullname ()) && *cp) {
849 strncpy (sigbuf, cp, sizeof(sigbuf));
850 snprintf (signature, sizeof(signature), "%s <%s>",
851 sigbuf, adrsprintf (NULL, NULL));
852 if ((cp = getname (signature)) == NULL)
853 adios (NULL, "getname () failed -- you lose extraordinarily big");
854 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL)) == NULL)
855 adios (NULL, "bad signature '%s'", sigbuf);
860 strncpy (signature, adrsprintf (NULL, NULL), sizeof(signature));
866 * Now that we've outputted the header fields in the draft
867 * message, we will now output any remaining header fields
868 * that we need to add/create.
872 finish_headers (FILE *out)
876 if (whomsw && !fill_up)
879 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
881 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
882 (int) getpid (), (long) tclock, LocalName ());
883 if (msgflags & MFRM) {
884 /* There was already a From: in the draft. Don't add one. */
885 if (!draft_from_masquerading)
886 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
887 so we'll reveal the user's actual account@thismachine
888 address in a Sender: header (and use it as the envelope
890 fprintf (out, "Sender: %s\n", from);
893 /* Construct a From: header. */
894 fprintf (out, "From: %s\n", signature);
898 if (!(msgflags & MVIS))
899 fprintf (out, "Bcc: Blind Distribution List: ;\n");
903 if (!(msgflags & MDAT)) {
904 advise (NULL, "message has no Date: header");
907 if (!(msgflags & MFRM)) {
908 advise (NULL, "message has no From: header");
911 if (whomsw && !fill_up)
914 fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
916 fprintf (out, "Resent-Message-ID: <%d.%ld@%s>\n",
917 (int) getpid (), (long) tclock, LocalName ());
918 if (msgflags & MRFM) {
919 /* There was already a Resent-From: in draft. Don't add one. */
920 if (!draft_from_masquerading)
921 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
922 so we'll reveal the user's actual account@thismachine
923 address in a Sender: header (and use it as the envelope
925 fprintf (out, "Resent-Sender: %s\n", from);
928 /* Construct a Resent-From: header. */
929 fprintf (out, "Resent-From: %s\n", signature);
932 if (!(msgflags & MVIS))
933 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
938 adios (NULL, "re-format message and try again");
940 adios (NULL, "no addressees");
945 get_header (char *header, struct headers *table)
949 for (h = table; h->value; h++)
950 if (!mh_strcasecmp (header, h->value))
958 putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flags)
964 if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
966 if ((!fill_in && (flags & (HBCC | HDCC))) || mp->m_ingrp)
970 fprintf (out, "%s: ", name);
971 linepos += (nameoutput = strlen (name) + 2);
974 if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
975 mp->m_pers = getcpy (aka);
977 if (mp->m_gname && !fill_in) {
978 snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
988 if (linepos != nameoutput) {
989 if (len + linepos + 2 > outputlinelen)
990 fprintf (out, ",\n%*s", linepos = nameoutput, "");
1000 return (flags & HTRY);
1005 putgrp (char *name, char *group, FILE *out, unsigned int flags)
1010 if (!fill_in && (flags & HBCC))
1014 fprintf (out, "%s: ", name);
1015 linepos += (nameoutput = strlen (name) + 2);
1017 linepos -= strlen (group);
1020 cp = fill_in ? group : concat (group, ";", NULL);
1023 if (linepos > nameoutput) {
1024 if (len + linepos + 2 > outputlinelen) {
1025 fprintf (out, ",\n%*s", nameoutput, "");
1026 linepos = nameoutput;
1040 insert (struct mailname *np)
1042 struct mailname *mp;
1044 if (np->m_mbox == NULL)
1047 for (mp = np->m_type == LOCALHOST ? &localaddrs
1048 : np->m_type == UUCPHOST ? &uuaddrs
1052 if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
1053 && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox)
1054 && np->m_bcc == mp->m_next->m_bcc)
1067 struct mailname *mp;
1069 printf ("-------\n\t-- Addresses --\nlocal:\t");
1070 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1071 printf ("%s%s%s", mp->m_mbox,
1072 mp->m_bcc ? "[BCC]" : "",
1073 mp->m_next ? ",\n\t" : "");
1075 printf ("\nnet:\t");
1076 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1077 printf ("%s%s@%s%s%s", mp->m_path ? mp->m_path : "",
1078 mp->m_mbox, mp->m_host,
1079 mp->m_bcc ? "[BCC]" : "",
1080 mp->m_next ? ",\n\t" : "");
1082 printf ("\nuucp:\t");
1083 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1084 printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1085 mp->m_bcc ? "[BCC]" : "",
1086 mp->m_next ? ",\n\t" : "");
1088 printf ("\n\t-- Folder Copies --\nfcc:\t");
1089 for (i = 0; i < fccind; i++)
1090 printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1098 struct mailname *mp;
1100 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1101 if (annoaux (mp) == NOTOK)
1104 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1105 if (annoaux (mp) == NOTOK)
1108 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1109 if (annoaux (mp) == NOTOK)
1119 annoaux (struct mailname *mp)
1122 char buffer[BUFSIZ];
1124 snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1125 i = strlen (buffer);
1127 return (write (pfd, buffer, i) == i ? OK : NOTOK);
1132 insert_fcc (struct headers *hdr, unsigned char *pp)
1136 for (cp = pp; isspace (*cp); cp++)
1138 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
1146 adios (NULL, "too many %ss", hdr->value);
1147 fccfold[fccind++] = getcpy (cp);
1155 make_bcc_file (int dashstuff)
1162 strncpy (bccfil, m_tmpfil ("bccs"), sizeof(bccfil));
1163 if ((out = fopen (bccfil, "w")) == NULL)
1164 adios (bccfil, "unable to create");
1165 chmod (bccfil, 0600);
1167 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1169 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
1170 (int) getpid (), (long) tclock, LocalName ());
1171 if (msgflags & MFRM) {
1172 /* There was already a From: in the draft. Don't add one. */
1173 if (!draft_from_masquerading)
1174 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
1175 so we'll reveal the user's actual account@thismachine
1176 address in a Sender: header (and use it as the envelope
1178 fprintf (out, "Sender: %s\n", from);
1181 /* Construct a From: header. */
1182 fprintf (out, "From: %s\n", signature);
1184 fprintf (out, "Subject: %s", subject);
1185 fprintf (out, "BCC:\n");
1188 * Use MIME encapsulation for Bcc messages
1194 * Check if any lines in the message clash with the
1195 * prefix for the MIME multipart separator. If there
1196 * is a clash, increment one of the letters in the
1197 * prefix and check again.
1199 if ((cp = strchr(prefix, 'a')) == NULL)
1200 adios (NULL, "lost prefix start");
1201 while (find_prefix () == NOTOK) {
1206 adios (NULL, "can't find a unique delimiter string");
1211 fprintf (out, "%s: %s\n%s: multipart/digest; boundary=\"",
1212 VRSN_FIELD, VRSN_VALUE, TYPE_FIELD);
1213 fprintf (out, "%s\"\n\n--%s\n\n", prefix, prefix);
1215 fprintf (out, "\n------- Blind-Carbon-Copy\n\n");
1221 * Do mhl filtering of Bcc messages instead
1222 * of MIME encapsulation.
1224 if (filter != NULL) {
1225 vec[0] = r1bindex (mhlproc, '/');
1227 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1231 adios ("fork", "unable to");
1234 dup2 (fileno (out), 1);
1237 vec[i++] = "-forward";
1242 /* was the flag -[no]dashstuffing specified? */
1244 vec[i++] = "-dashstuffing";
1245 else if (dashstuff < 0)
1246 vec[i++] = "-nodashstuffing";
1249 execvp (mhlproc, vec);
1250 fprintf (stderr, "unable to exec ");
1255 pidXwait (child_id, mhlproc);
1259 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
1260 adios (tmpfil, "unable to re-open");
1263 * If using MIME encapsulation, or if the -nodashstuffing
1264 * flag was given, then just copy message. Else do
1265 * RFC934 quoting (dashstuffing).
1267 if (mime || dashstuff < 0)
1268 cpydata (fd, fileno (out), tmpfil, bccfil);
1270 cpydgst (fd, fileno (out), tmpfil, bccfil);
1274 fseek (out, 0L, SEEK_END);
1276 fprintf (out, "\n--%s--\n", prefix);
1278 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
1284 * Scan message to check if any lines clash with
1285 * the prefix of the MIME multipart separator.
1292 unsigned char buffer[BUFSIZ];
1295 if ((in = fopen (tmpfil, "r")) == NULL)
1296 adios (tmpfil, "unable to re-open");
1298 len = strlen (prefix);
1301 while (fgets (buffer, sizeof(buffer) - 1, in))
1302 if (buffer[0] == '-' && buffer[1] == '-') {
1305 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1309 if (strcmp (buffer + 2, prefix) == 0) {
1320 #define plural(x) (x == 1 ? "" : "s")
1325 if (badadr && unkadr)
1326 die (NULL, "%d address%s unparsable, %d addressee%s undeliverable",
1327 badadr, plural (badadr), unkadr, plural (badadr));
1329 die (NULL, "%d address%s unparsable", badadr, plural (badadr));
1331 die (NULL, "%d addressee%s undeliverable", unkadr, plural (unkadr));
1336 do_addresses (int bccque, int talk)
1340 struct mailname *lp;
1343 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1344 if (lp->m_bcc ? bccque : !bccque) {
1346 printf (" -- Local Recipients --\n");
1347 do_an_address (lp, talk);
1352 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1353 if (lp->m_bcc ? bccque : !bccque) {
1355 printf (" -- UUCP Recipients --\n");
1356 do_an_address (lp, talk);
1361 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1362 if (lp->m_bcc ? bccque : !bccque) {
1364 printf (" -- Network Recipients --\n");
1365 do_an_address (lp, talk);
1372 if (rp_isbad (retval = sm_waend ()))
1373 die (NULL, "problem ending addresses; %s", rp_string (retval));
1374 #endif /* SMTPMTS */
1379 * MTS-SPECIFIC INTERACTION
1384 * SENDMAIL/SMTP routines
1390 post (char *file, int bccque, int talk)
1395 onex = !(msgflags & MINV) || bccque;
1397 if (msgflags & MINV)
1398 printf (" -- Posting for %s Recipients --\n",
1399 bccque ? "Blind" : "Sighted");
1401 printf (" -- Posting for All Recipients --\n");
1406 if (rp_isbad (retval = sm_init (clientsw, serversw, watch, verbose,
1407 snoop, onex, queued, sasl, saslmech,
1409 || rp_isbad (retval = sm_winit (smtpmode, from)))
1410 die (NULL, "problem initializing server; %s", rp_string (retval));
1412 do_addresses (bccque, talk && verbose);
1413 if ((fd = open (file, O_RDONLY)) == NOTOK)
1414 die (file, "unable to re-open");
1419 sm_end (onex ? OK : DONE);
1423 if (msgflags & MINV)
1424 printf (" -- %s Recipient Copies Posted --\n",
1425 bccque ? "Blind" : "Sighted");
1427 printf (" -- Recipient Copies Posted --\n");
1434 /* Address Verification */
1437 verify_all_addresses (int talk)
1440 struct mailname *lp;
1444 if (!whomsw || checksw)
1445 if (rp_isbad (retval = sm_init (clientsw, serversw, watch, verbose, snoop, 0,
1446 queued, sasl, saslmech, user))
1447 || rp_isbad (retval = sm_winit (smtpmode, from)))
1448 die (NULL, "problem initializing server; %s", rp_string (retval));
1450 if (talk && !whomsw)
1451 printf (" -- Address Verification --\n");
1452 if (talk && localaddrs.m_next)
1453 printf (" -- Local Recipients --\n");
1454 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1455 do_an_address (lp, talk);
1457 if (talk && uuaddrs.m_next)
1458 printf (" -- UUCP Recipients --\n");
1459 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1460 do_an_address (lp, talk);
1462 if (talk && netaddrs.m_next)
1463 printf (" -- Network Recipients --\n");
1464 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1465 do_an_address (lp, talk);
1468 if (talk && !whomsw)
1469 printf (" -- Address Verification Successful --\n");
1471 if (!whomsw || checksw)
1480 do_an_address (struct mailname *lp, int talk)
1486 switch (lp->m_type) {
1490 strncpy (addr, mbox, sizeof(addr));
1494 mbox = auxformat (lp, 0);
1496 snprintf (addr, sizeof(addr), "%s!%s", lp->m_host, lp->m_mbox);
1499 default: /* let SendMail decide if the host is bad */
1502 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1507 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1509 if (whomsw && !checksw) {
1517 switch (retval = sm_wadr (mbox, host,
1518 lp->m_type != UUCPHOST ? lp->m_path : NULL)) {
1521 printf ("address ok\n");
1527 fprintf (stderr, " %s: ", addr);
1528 fprintf (talk ? stdout : stderr, "loses; %s\n",
1529 rp_string (retval));
1535 fprintf (stderr, " %s: ", addr);
1536 die (NULL, "unexpected response; %s", rp_string (retval));
1544 do_text (char *file, int fd)
1549 lseek (fd, (off_t) 0, SEEK_SET);
1551 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1552 if (rp_isbad (retval = sm_wtxt (buf, state)))
1553 die (NULL, "problem writing text; %s\n", rp_string (retval));
1557 die (file, "problem reading from");
1559 switch (retval = sm_wtend ()) {
1565 die (NULL, "posting failed; %s", rp_string (retval));
1568 die (NULL, "unexpected response; %s", rp_string (retval));
1572 #endif /* SMTPMTS */
1582 #ifndef RELIABLE_SIGNALS
1583 SIGNAL (i, SIG_IGN);
1587 if (msgflags & MINV)
1591 if (!whomsw || checksw)
1593 #endif /* SMTPMTS */
1605 hstat = SIGNAL2 (SIGHUP, sigser);
1606 istat = SIGNAL2 (SIGINT, sigser);
1607 qstat = SIGNAL2 (SIGQUIT, sigser);
1608 tstat = SIGNAL2 (SIGTERM, sigser);
1618 SIGNAL (SIGHUP, hstat);
1619 SIGNAL (SIGINT, istat);
1620 SIGNAL (SIGQUIT, qstat);
1621 SIGNAL (SIGTERM, tstat);
1629 p_refile (char *file)
1637 printf (" -- Filing Folder Copies --\n");
1638 for (i = 0; i < fccind; i++)
1639 fcc (file, fccfold[i]);
1641 printf (" -- Folder Copies Filed --\n");
1646 * Call the `fileproc' to add the file to the folder.
1650 fcc (char *file, char *folder)
1657 printf (" %sFcc %s: ", msgstate == RESENT ? "Resent-" : "", folder);
1660 for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
1666 fprintf (stderr, " %sFcc %s: ",
1667 msgstate == RESENT ? "Resent-" : "", folder);
1668 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
1672 /* see if we need to add `+' */
1673 snprintf (fold, sizeof(fold), "%s%s",
1674 *folder == '+' || *folder == '@' ? "" : "+", folder);
1676 /* now exec the fileproc */
1677 execlp (fileproc, r1bindex (fileproc, '/'),
1678 "-link", "-file", file, fold, NULL);
1682 if ((status = pidwait (child_id, OK))) {
1684 fprintf (stderr, " %sFcc %s: ",
1685 msgstate == RESENT ? "Resent-" : "", folder);
1686 pidstatus (status, verbose ? stdout : stderr, NULL);
1689 printf ("folder ok\n");
1701 die (char *what, char *fmt, ...)
1706 if (msgflags & MINV)
1710 if (!whomsw || checksw)
1712 #endif /* SMTPMTS */
1715 advertise (what, NULL, fmt, ap);