]> diplodocus.org Git - nmh/blob - uip/post.c
vector.c: Move interface to own file.
[nmh] / uip / post.c
1 /* post.c -- enter messages into the mail transport system
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "sbr/pidstatus.h"
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/arglist.h"
13 #include "sbr/error.h"
14 #include <fcntl.h>
15 #include "h/signals.h"
16 #include "h/addrsbr.h"
17 #include "h/aliasbr.h"
18 #include "h/dropsbr.h"
19 #include "h/mime.h"
20 #include "h/done.h"
21 #include "h/utils.h"
22 #include "h/tws.h"
23 #include "h/mts.h"
24 #include "sbr/m_mktemp.h"
25 #include "sbr/message_id.h"
26
27 #ifdef HAVE_SYS_TIME_H
28 # include <sys/time.h>
29 #endif
30 #include <time.h>
31
32 #include "mts/smtp/smtp.h"
33
34 #ifndef CYRUS_SASL
35 # define SASLminc(a) (a)
36 #else /* CYRUS_SASL */
37 # define SASLminc(a) 0
38 #endif /* CYRUS_SASL */
39
40 #ifndef TLS_SUPPORT
41 # define TLSminc(a) (a)
42 #else /* TLS_SUPPORT */
43 # define TLSminc(a) 0
44 #endif /* TLS_SUPPORT */
45
46 #ifndef OAUTH_SUPPORT
47 # define OAUTHminc(a) (a)
48 #else /* OAUTH_SUPPORT */
49 # define OAUTHminc(a) 0
50 #endif /* OAUTH_SUPPORT */
51
52 #define FCCS 10 /* max number of fccs allowed */
53
54 /* In the following array of structures, the numeric second field of the
55 structures (minchars) is apparently used like this:
56
57 -# : Switch can be abbreviated to # characters; switch hidden in -help.
58 0 : Switch can't be abbreviated; switch shown in -help.
59 # : Switch can be abbreviated to # characters; switch shown in -help. */
60
61 #define POST_SWITCHES \
62 X("alias aliasfile", 0, ALIASW) \
63 X("check", -5, CHKSW) /* interface from whom */ \
64 X("nocheck", -7, NCHKSW) /* interface from whom */ \
65 X("debug", -5, DEBUGSW) \
66 X("dist", -4, DISTSW) /* interface from dist */ \
67 X("filter filterfile", 0, FILTSW) \
68 X("nofilter", 0, NFILTSW) \
69 X("format", 0, FRMTSW) \
70 X("noformat", 0, NFRMTSW) \
71 X("library directory", -7, LIBSW) /* interface from send, whom */ \
72 X("mime", 0, MIMESW) \
73 X("nomime", 0, NMIMESW) \
74 X("msgid", 0, MSGDSW) \
75 X("nomsgid", 0, NMSGDSW) \
76 X("verbose", 0, VERBSW) \
77 X("noverbose", 0, NVERBSW) \
78 X("watch", 0, WATCSW) \
79 X("nowatch", 0, NWATCSW) \
80 X("whom", -4, WHOMSW) /* interface from whom */ \
81 X("width columns", 0, WIDTHSW) \
82 X("version", 0, VERSIONSW) \
83 X("help", 0, HELPSW) \
84 X("dashstuffing", -12, BITSTUFFSW) /* should we dashstuff BCC messages? */ \
85 X("nodashstuffing", -14, NBITSTUFFSW) \
86 X("idanno number", -6, ANNOSW) /* interface from send */ \
87 X("client host", -6, CLIESW) \
88 X("server host", 6, SERVSW) /* specify alternate SMTP server */ \
89 X("snoop", -5, SNOOPSW) /* snoop the SMTP transaction */ \
90 X("partno", -6, PARTSW) \
91 X("sasl", SASLminc(4), SASLSW) \
92 X("nosasl", SASLminc(6), NOSASLSW) \
93 X("saslmech", SASLminc(5), SASLMECHSW) \
94 X("user", SASLminc(-4), USERSW) \
95 X("port server submission port name/number", 4, PORTSW) \
96 X("tls", TLSminc(-3), TLSSW) \
97 X("initialtls", TLSminc(-10), INITTLSSW) \
98 X("notls", TLSminc(-5), NTLSSW) \
99 X("certverify", TLSminc(-10), CERTVERSW) \
100 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
101 X("fileproc", -4, FILEPROCSW) \
102 X("mhlproc", -3, MHLPROCSW) \
103 X("sendmail program", 0, MTSSM) \
104 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
105 X("credentials legacy|file:filename", 0, CREDENTIALSSW) \
106 X("messageid localname|random", 2, MESSAGEIDSW) \
107 X("authservice auth-service-name", OAUTHminc(-11), AUTHSERVICESW) \
108 X("oauthcredfile credential-file", OAUTHminc(-7), OAUTHCREDFILESW) \
109 X("oauthclientid client-id", OAUTHminc(-12), OAUTHCLIDSW) \
110 X("oauthclientsecret client-secret", OAUTHminc(-12), OAUTHCLSECSW) \
111 X("oauthauthendpoint authentication-endpoint", OAUTHminc(-6), OAUTHAUTHENDSW) \
112 X("oauthredirect redirect-uri", OAUTHminc(-6), OAUTHREDIRSW) \
113 X("oauthtokenendpoint token-endpoint", OAUTHminc(-6), OAUTHTOKENDSW) \
114 X("oauthscope scope", OAUTHminc(-6), OAUTHSCOPESW) \
115
116 #define X(sw, minchars, id) id,
117 DEFINE_SWITCH_ENUM(POST);
118 #undef X
119
120 #define X(sw, minchars, id) { sw, minchars, id },
121 DEFINE_SWITCH_ARRAY(POST, switches);
122 #undef X
123
124
125 /*
126 * Mapping between command-line switches and profile entries, communicated
127 * from 'send'. We use a service name of 'post' internally.
128 */
129
130 static struct {
131 const char *profname;
132 int switchnum;
133 const char *value;
134 } oauthswitches[] = {
135 { "oauth-%s-credential-file", OAUTHCREDFILESW, NULL },
136 { "oauth-%s-client_id", OAUTHCLIDSW, NULL },
137 { "oauth-%s-client_secret", OAUTHCLSECSW, NULL },
138 { "oauth-%s-auth_endpoint", OAUTHAUTHENDSW, NULL },
139 { "oauth-%s-redirect_uri", OAUTHREDIRSW, NULL },
140 { "oauth-%s-token_endpoint", OAUTHTOKENDSW, NULL },
141 { "oauth-%s-scope", OAUTHSCOPESW, NULL },
142 { NULL, 0, NULL }
143 };
144
145 struct headers {
146 char *value;
147 unsigned int flags;
148 unsigned int set;
149 };
150
151 /*
152 * flags for headers->flags
153 */
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, unless MTS_SENDMAIL_PIPE */
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 */
166 #define HONE 0x0800 /* Only (zero or) one address allowed */
167 #define HEFM 0x1000 /* Envelope-From: header */
168 #define HMIM 0x2000 /* MIME-Version: header */
169 #define HCTE 0x4000 /* Content-Transfer-Encoding: header */
170
171 /*
172 * flags for headers->set
173 */
174 #define MFRM 0x0001 /* we've seen a From: */
175 #define MDAT 0x0002 /* we've seen a Date: */
176 #define MRFM 0x0004 /* we've seen a Resent-From: */
177 #define MVIS 0x0008 /* we've seen sighted addrs */
178 #define MINV 0x0010 /* we've seen blind addrs */
179 #define MSND 0x0020 /* we've seen a Sender: */
180 #define MRSN 0x0040 /* We've seen a Resent-Sender: */
181 #define MEFM 0x0080 /* We've seen Envelope-From: */
182 #define MMIM 0x0100 /* We've seen Mime-Version: */
183
184 static struct headers NHeaders[] = {
185 { "Return-Path", HBAD, 0 },
186 { "Received", HBAD, 0 },
187 { "Reply-To", HADR|HNGR, 0 },
188 { "From", HADR|HNGR, MFRM },
189 { "Sender", HADR|HNGR|HONE, MSND },
190 { "Date", HBAD, 0 },
191 { "Subject", HSUB, 0 },
192 { "To", HADR|HTRY, MVIS },
193 { "cc", HADR|HTRY, MVIS },
194 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
195 { "Dcc", HADR|HTRY|HDCC|HNIL, MVIS }, /* sorta cc & bcc combined */
196 { "Message-ID", HBAD, 0 },
197 { "Fcc", HFCC, 0 },
198 { "Envelope-From", HADR|HONE|HEFM, MEFM },
199 { "MIME-Version", HMIM, MMIM },
200 { "Content-Transfer-Encoding", HCTE, 0 },
201 { NULL, 0, 0 }
202 };
203
204 static struct headers RHeaders[] = {
205 { "Resent-Reply-To", HADR|HNGR, 0 },
206 { "Resent-From", HADR|HNGR, MRFM },
207 { "Resent-Sender", HADR|HNGR, MRSN },
208 { "Resent-Date", HBAD, 0 },
209 { "Resent-Subject", HSUB, 0 },
210 { "Resent-To", HADR|HTRY, MVIS },
211 { "Resent-cc", HADR|HTRY, MVIS },
212 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
213 { "Resent-Message-ID", HBAD, 0 },
214 { "Resent-Fcc", HFCC, 0 },
215 { "Reply-To", HADR, 0 },
216 { "From", HADR|HNGR, MFRM },
217 { "Sender", HADR|HNGR, MSND },
218 { "Date", HNOP, MDAT },
219 { "To", HADR|HNIL, 0 },
220 { "cc", HADR|HNIL, 0 },
221 { "Bcc", HADR|HTRY|HBCC|HNIL, 0 },
222 { "Fcc", HIGN, 0 },
223 { "Envelope-From", HADR|HONE|HEFM, MEFM },
224 { "MIME-Version", HMIM, MMIM },
225 { "Content-Transfer-Encoding", HCTE, 0 },
226 { NULL, 0, 0 }
227 };
228
229 static short fccind = 0; /* index into fccfold[] */
230 static short outputlinelen = OUTPUTLINELEN;
231
232 static int pfd = NOTOK; /* fd to write annotation list to */
233 static bool recipients; /* how many people will get a copy */
234 static int unkadr = 0; /* how many of those were unknown */
235 static int badadr = 0; /* number of bad addrs */
236 static bool badmsg; /* message has bad semantics */
237 static bool verbose; /* spell it out */
238 static bool format = true; /* format addresses */
239 static bool mime; /* use MIME-style encapsulations for Bcc */
240 static bool msgid; /* add msgid */
241 static bool debug; /* debugging post */
242 static bool watch; /* watch the delivery process */
243 static bool whomsw; /* we are whom not post */
244 static bool checksw; /* whom -check */
245 static int linepos=0; /* putadr()'s position on the line */
246 static int nameoutput=0; /* putadr() has output header name */
247 static bool sasl; /* Use SASL auth for SMTP */
248 static char *saslmech=NULL; /* Force use of particular SASL mech */
249 static char *user=NULL; /* Authenticate as this user */
250 static char *port="submission"; /* Name of server port for SMTP submission */
251 static int tlsflag=0; /* Flags to control TLS settings */
252 static int fromcount=0; /* Count of addresses on From: header */
253 static bool seensender; /* Have we seen a Sender: header? */
254
255 static unsigned msgflags = 0; /* what we've seen */
256
257 #define NORMAL 0
258 #define RESENT 1
259 static int msgstate = NORMAL;
260
261 static time_t tclock = 0; /* the time we started (more or less) */
262
263 static SIGNAL_HANDLER hstat, istat, qstat, tstat;
264
265 static char tmpfil[BUFSIZ];
266 static char bccfil[BUFSIZ];
267
268 static char from[BUFSIZ]; /* my network address */
269 static char sender[BUFSIZ]; /* my Sender: header */
270 static char efrom[BUFSIZ]; /* my Envelope-From: header */
271 static char fullfrom[BUFSIZ]; /* full contents of From header */
272 static char *filter = NULL; /* the filter for BCC'ing */
273 static char *subject = NULL; /* the subject field for BCC'ing */
274 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
275 enum encoding { UNKNOWN = 0, BINARY = 1, SEVENBIT = 7, EIGHTBIT = 8 };
276 static enum encoding cte = UNKNOWN;
277
278 static struct headers *hdrtab; /* table for the message we're doing */
279
280 static struct mailname localaddrs; /* local addrs */
281 static struct mailname netaddrs; /* network addrs */
282 static struct mailname uuaddrs; /* uucp addrs */
283 static struct mailname tmpaddrs; /* temporary queue */
284
285 static bool snoop;
286 static char *clientsw = NULL;
287 static char *serversw = NULL;
288
289 static char prefix[] = "----- =_aaaaaaaaaa";
290
291 static char *partno = NULL;
292
293 /*
294 * static prototypes
295 */
296 static void putfmt (char *, char *, int *, FILE *);
297 static void start_headers (void);
298 static void finish_headers (FILE *);
299 static int get_header (char *, struct headers *) PURE;
300 static int putadr (char *, char *, struct mailname *, FILE *, unsigned int,
301 char *, unsigned int);
302 static void putgrp (char *, char *, FILE *, unsigned int);
303 static int insert (struct mailname *);
304 static void pl (void);
305 static void anno (void);
306 static int annoaux (struct mailname *);
307 static void insert_fcc (struct headers *, char *);
308 static void make_bcc_file (int);
309 static void verify_all_addresses (int, int, char *, int, char *);
310 static void chkadr (void) PURE;
311 static void sigon (void);
312 static void sigoff (void);
313 static void p_refile (char *);
314 static void fcc (char *, char *);
315 static void fatal (char *, char *, ...) CHECK_PRINTF(2, 3);
316 static void post (char *, int, int, int, char *, int, char *);
317 static void do_text (char *file, int fd);
318 static void do_an_address (struct mailname *, int);
319 static void do_addresses (int, int);
320 static int find_prefix (void);
321
322
323 int
324 main (int argc, char **argv)
325 {
326 int state, compnum, dashstuff = 0, swnum;
327 bool oauth_flag = false;
328 int tls = -1;
329 bool noverify = false;
330 int eai = 0; /* use Email Address Internationalization (EAI) (SMTPUTF8) */
331 char *cp, *msg = NULL, **argp, **arguments, *envelope;
332 char buf[NMH_BUFSIZ], name[NAMESZ], *auth_svc = NULL;
333 FILE *in, *out;
334 m_getfld_state_t gstate;
335
336 if (nmh_init(argv[0], false, false)) { return 1; }
337
338 mts_init ();
339 arguments = getarguments (invo_name, argc, argv, 0);
340 argp = arguments;
341
342 while ((cp = *argp++)) {
343 if (*cp == '-') {
344 switch ((swnum = smatch (++cp, switches))) {
345 case AMBIGSW:
346 ambigsw (cp, switches);
347 done (1);
348 case UNKWNSW:
349 die("-%s unknown", cp);
350
351 case HELPSW:
352 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
353 print_help (buf, switches, 0);
354 done (0);
355 case VERSIONSW:
356 print_version(invo_name);
357 done (0);
358
359 case LIBSW:
360 if (!(cp = *argp++) || *cp == '-')
361 die("missing argument to %s", argp[-2]);
362 /* create a minimal context */
363 if (context_foil (cp) == -1)
364 done (1);
365 continue;
366
367 case ALIASW:
368 if (!(cp = *argp++) || *cp == '-')
369 die("missing argument to %s", argp[-2]);
370 if ((state = alias (cp)) != AK_OK)
371 die("aliasing error in %s - %s",
372 cp, akerror (state));
373 continue;
374
375 case CHKSW:
376 checksw = true;
377 continue;
378 case NCHKSW:
379 checksw = false;
380 continue;
381
382 case DEBUGSW:
383 debug = true;
384 continue;
385
386 case DISTSW:
387 msgstate = RESENT;
388 continue;
389
390 case FILTSW:
391 if (!(filter = *argp++) || *filter == '-')
392 die("missing argument to %s", argp[-2]);
393 mime = false;
394 continue;
395 case NFILTSW:
396 filter = NULL;
397 continue;
398
399 case FRMTSW:
400 format = true;
401 continue;
402 case NFRMTSW:
403 format = false;
404 continue;
405
406 case BITSTUFFSW:
407 dashstuff = 1;
408 continue;
409 case NBITSTUFFSW:
410 dashstuff = -1;
411 continue;
412
413 case MIMESW:
414 mime = true;
415 filter = NULL;
416 continue;
417 case NMIMESW:
418 mime = false;
419 continue;
420
421 case MSGDSW:
422 msgid = true;
423 continue;
424 case NMSGDSW:
425 msgid = false;
426 continue;
427
428 case VERBSW:
429 verbose = true;
430 continue;
431 case NVERBSW:
432 verbose = false;
433 continue;
434
435 case WATCSW:
436 watch = true;
437 continue;
438 case NWATCSW:
439 watch = false;
440 continue;
441
442 case WHOMSW:
443 whomsw = true;
444 continue;
445
446 case WIDTHSW:
447 if (!(cp = *argp++) || *cp == '-')
448 die("missing argument to %s", argp[-2]);
449 if ((outputlinelen = atoi (cp)) < 10)
450 die("impossible width %d", outputlinelen);
451 continue;
452
453 case ANNOSW:
454 if (!(cp = *argp++) || *cp == '-')
455 die("missing argument to %s", argp[-2]);
456 if ((pfd = atoi (cp)) <= 2)
457 die("bad argument %s %s", argp[-2], cp);
458 continue;
459
460 case CLIESW:
461 if (!(clientsw = *argp++) || *clientsw == '-')
462 die("missing argument to %s", argp[-2]);
463 continue;
464 case SERVSW:
465 if (!(serversw = *argp++) || *serversw == '-')
466 die("missing argument to %s", argp[-2]);
467 continue;
468 case SNOOPSW:
469 snoop = true;
470 continue;
471
472 case PARTSW:
473 if (!(partno = *argp++) || *partno == '-')
474 die("missing argument to %s", argp[-2]);
475 continue;
476
477 case SASLSW:
478 sasl = true;
479 continue;
480
481 case NOSASLSW:
482 sasl = false;
483 continue;
484
485 case SASLMECHSW:
486 if (!(saslmech = *argp++) || *saslmech == '-')
487 die("missing argument to %s", argp[-2]);
488 continue;
489
490 case AUTHSERVICESW:
491 if (!(auth_svc = *argp++) || *auth_svc == '-')
492 die("missing argument to %s", argp[-2]);
493 oauth_flag = true;
494 continue;
495
496 case OAUTHCREDFILESW:
497 case OAUTHCLIDSW:
498 case OAUTHCLSECSW:
499 case OAUTHAUTHENDSW:
500 case OAUTHREDIRSW:
501 case OAUTHTOKENDSW:
502 case OAUTHSCOPESW:
503 {
504 int i;
505
506 if (!(cp = *argp++) || *cp == '-')
507 die("missing argument to %s", argp[-2]);
508
509 for (i = 0; oauthswitches[i].profname != NULL; i++) {
510 if (oauthswitches[i].switchnum == swnum) {
511 oauthswitches[i].value = cp;
512 break;
513 }
514 }
515
516 if (oauthswitches[i].profname == NULL)
517 die("internal error: cannot map switch %s "
518 "to profile entry", argp[-2]);
519
520 oauth_flag = true;
521 continue;
522 }
523
524 case USERSW:
525 if (!(user = *argp++) || *user == '-')
526 die("missing argument to %s", argp[-2]);
527 continue;
528
529 case PORTSW:
530 if (!(port = *argp++) || *port == '-')
531 die("missing argument to %s", argp[-2]);
532 continue;
533
534 case TLSSW:
535 tls = 1;
536 continue;
537
538 case INITTLSSW:
539 tls = 2;
540 continue;
541
542 case NTLSSW:
543 tls = 0;
544 continue;
545
546 case CERTVERSW:
547 noverify = false;
548 continue;
549
550 case NOCERTVERSW:
551 noverify = true;
552 continue;
553
554 case FILEPROCSW:
555 if (!(cp = *argp++) || *cp == '-')
556 die("missing argument to %s", argp[-2]);
557 fileproc = cp;
558 continue;
559
560 case MHLPROCSW:
561 if (!(cp = *argp++) || *cp == '-')
562 die("missing argument to %s", argp[-2]);
563 mhlproc = cp;
564 continue;
565
566 case MTSSM:
567 if (!(cp = *argp++) || *cp == '-')
568 die("missing argument to %s", argp[-2]);
569 sendmail = cp;
570 continue;
571
572 case MTSSW:
573 if (!(cp = *argp++) || *cp == '-')
574 die("missing argument to %s", argp[-2]);
575 save_mts_method (cp);
576 continue;
577
578 case CREDENTIALSSW: {
579 if (!(cp = *argp++) || *cp == '-')
580 die("missing argument to %s", argp[-2]);
581 add_profile_entry ("credentials", cp);
582 continue;
583 }
584
585 case MESSAGEIDSW:
586 if (!(cp = *argp++) || *cp == '-')
587 die("missing argument to %s", argp[-2]);
588 if (save_message_id_style (cp) != 0)
589 die("unsupported messageid \"%s\"", cp);
590 continue;
591 }
592 }
593 if (msg)
594 die("only one message at a time!");
595 msg = cp;
596 }
597
598 alias (AliasFile);
599
600 if (!msg)
601 die("usage: %s [switches] file", invo_name);
602
603 if (outputlinelen < 10)
604 die("impossible width %d", outputlinelen);
605
606 if ((in = fopen (msg, "r")) == NULL)
607 adios (msg, "unable to open");
608
609 start_headers ();
610 if (debug) {
611 verbose = true;
612 out = stdout;
613 } else {
614 if (whomsw) {
615 if ((out = fopen ("/dev/null", "w")) == NULL)
616 adios ("/dev/null", "unable to open");
617 } else {
618 char *cp = m_mktemp2(NULL, invo_name, NULL, &out);
619 if (cp == NULL) {
620 die("unable to create temporary file in %s",
621 get_temp_dir());
622 }
623 strncpy(tmpfil, cp, sizeof(tmpfil));
624 }
625 }
626
627 hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
628
629 gstate = m_getfld_state_init(in);
630 for (compnum = 1;;) {
631 int bufsz = sizeof buf;
632 switch (state = m_getfld2(&gstate, name, buf, &bufsz)) {
633 case FLD:
634 case FLDPLUS:
635 compnum++;
636 cp = mh_xstrdup(buf);
637 while (state == FLDPLUS) {
638 bufsz = sizeof buf;
639 state = m_getfld2(&gstate, name, buf, &bufsz);
640 cp = add (buf, cp);
641 }
642 putfmt (name, cp, &eai, out);
643 free (cp);
644 continue;
645
646 case BODY:
647 finish_headers (out);
648 if (whomsw)
649 break;
650 fprintf (out, "\n%s", buf);
651 while (state == BODY) {
652 bufsz = sizeof buf;
653 state = m_getfld2(&gstate, name, buf, &bufsz);
654 fputs (buf, out);
655 }
656 break;
657
658 case FILEEOF:
659 finish_headers (out);
660 break;
661
662 case LENERR:
663 case FMTERR:
664 die("message format error in component #%d", compnum);
665
666 default:
667 die("getfld() returned %d", state);
668 }
669 break;
670 }
671 m_getfld_state_destroy (&gstate);
672
673 if (pfd != NOTOK)
674 anno ();
675 fclose (in);
676
677 if (debug) {
678 pl ();
679 done (0);
680 }
681 fclose (out);
682
683 /*
684 * Here's how we decide which address to use as the envelope-from
685 * address for SMTP.
686 *
687 * - If we were given an Envelope-From header, use that.
688 * - If we were given a Sender: address, use that.
689 * - Otherwise, use the address on the From: line
690 */
691
692 if (msgflags & MEFM) {
693 envelope = efrom;
694 } else if (seensender) {
695 envelope = sender;
696 } else {
697 envelope = from;
698 }
699
700 if (tls == -1) {
701 #ifdef TLS_SUPPORT
702 /*
703 * The user didn't specify any of the tls switches. Try to
704 * help them by implying -initialtls if they're using port 465
705 * (smtps, until IANA revoked that registration in 1998).
706 */
707 tls = ! strcmp (port, "465") || ! strcasecmp (port, "smtps")
708 ? 2
709 : 0;
710 #else /* ! TLS_SUPPORT */
711 tls = 0;
712 #endif /* ! TLS_SUPPORT */
713 }
714
715 if (tls == 1)
716 tlsflag = S_STARTTLS;
717 else if (tls == 2)
718 tlsflag = S_INITTLS;
719 else
720 tlsflag = 0;
721
722 if (noverify)
723 tlsflag |= S_NOVERIFY;
724
725 /*
726 * If we were given any oauth flags, store the appropriate profile
727 * entries and make sure an authservice was given (we have to do this
728 * here because we aren't guaranteed the authservice will be given on
729 * the command line before the other OAuth flags are given).
730 */
731
732 if (oauth_flag) {
733 int i;
734 char sbuf[128];
735
736 if (auth_svc == NULL) {
737 die("No authentication service given with -authservice");
738 }
739
740 for (i = 0; oauthswitches[i].profname != NULL; i++) {
741 if (oauthswitches[i].value != NULL) {
742 snprintf(sbuf, sizeof(sbuf),
743 oauthswitches[i].profname, auth_svc);
744 sbuf[sizeof(sbuf) - 1] = '\0';
745 add_profile_entry(sbuf, oauthswitches[i].value);
746 }
747 }
748 }
749
750 /* If we are doing a "whom" check */
751 if (whomsw) {
752 /* This won't work with MTS_SENDMAIL_PIPE. */
753 verify_all_addresses (1, eai, envelope, oauth_flag, auth_svc);
754 done (0);
755 }
756
757 if (msgflags & MINV) {
758 make_bcc_file (dashstuff);
759 if (msgflags & MVIS) {
760 if (sm_mts != MTS_SENDMAIL_PIPE) {
761 /* It would be nice to have support to call
762 verify_all_addresses with MTS_SENDMAIL_PIPE, but
763 that might require running sendmail as root. Note
764 that spost didn't verify addresses. */
765 verify_all_addresses (verbose, eai, envelope, oauth_flag,
766 auth_svc);
767 }
768 post (tmpfil, 0, verbose, eai, envelope, oauth_flag, auth_svc);
769 }
770 post (bccfil, 1, verbose, eai, envelope, oauth_flag, auth_svc);
771 (void) m_unlink (bccfil);
772 } else {
773 post (tmpfil, 0, isatty (1), eai, envelope, oauth_flag, auth_svc);
774 }
775
776 p_refile (tmpfil);
777 (void) m_unlink (tmpfil);
778
779 if (verbose) {
780 if (partno)
781 printf ("Partial Message #%s Processed\n", partno);
782 else
783 puts("Message Processed");
784 }
785
786 done (0);
787 return 1;
788 }
789
790
791 /*
792 * DRAFT GENERATION
793 */
794
795 static void
796 putfmt (char *name, char *str, int *eai, FILE *out)
797 {
798 int count;
799 bool grp;
800 int i, keep;
801 char *cp, *pp, *qp;
802 char namep[BUFSIZ], error[BUFSIZ];
803 char *savehdr = NULL;
804 unsigned int savehdrlen = 0;
805 struct mailname *mp = NULL, *np = NULL;
806 struct headers *hdr;
807
808 while (*str == ' ' || *str == '\t')
809 str++;
810
811 if (msgstate == NORMAL && uprf (name, "resent")) {
812 inform("illegal header line -- %s:", name);
813 badmsg = true;
814 return;
815 }
816
817 if (! *eai) {
818 /* Check each header field value to see if it has any 8-bit characters.
819 If it does, enable EAI support. */
820 if (contains8bit(str, NULL)) {
821 if (verbose) {
822 puts("EAI/SMTPUTF8 enabled");
823 }
824
825 /* Enable SMTPUTF8. */
826 *eai = 1;
827
828 /* Enable passing of utf-8 setting to getname()/getadrx(). */
829 enable_eai();
830 }
831 }
832
833 if ((i = get_header (name, hdrtab)) == NOTOK) {
834 if (strncasecmp (name, "nmh-", 4)) {
835 fprintf (out, "%s: %s", name, str);
836 } else {
837 /* Filter out all Nmh-* headers, because Norm asked. They
838 should never have reached this point. Warn about any
839 that are non-empty. */
840 if (strcmp (str, "\n")) {
841 trim_suffix_c(str, '\n');
842 if (! whomsw) {
843 inform("ignoring header line -- %s: %s", name, str);
844 }
845 }
846 }
847
848 return;
849 }
850
851 hdr = &hdrtab[i];
852 if (hdr->flags & HIGN) {
853 return;
854 }
855 if (hdr->flags & HBAD) {
856 inform("illegal header line -- %s:", name);
857 badmsg = true;
858 return;
859 }
860 msgflags |= (hdr->set & ~(MVIS | MINV));
861
862 if (hdr->flags & HSUB)
863 subject = subject ? add (str, add ("\t", subject)) : mh_xstrdup(str);
864 if (hdr->flags & HFCC) {
865 if ((cp = strrchr(str, '\n')))
866 *cp = 0;
867 for (pp = str; (cp = strchr(pp, ',')); pp = cp) {
868 *cp++ = 0;
869 insert_fcc (hdr, pp);
870 }
871 insert_fcc (hdr, pp);
872 return;
873 }
874 if (hdr->flags & HCTE) {
875 if (strncasecmp (str, "7bit", 4) == 0) {
876 cte = SEVENBIT;
877 } else if (strncasecmp (str, "8bit", 4) == 0) {
878 cte = EIGHTBIT;
879 } else if (strncasecmp (str, "binary", 6) == 0) {
880 cte = BINARY;
881 }
882 }
883 if (!(hdr->flags & HADR)) {
884 fprintf (out, "%s: %s", name, str);
885 return;
886 }
887
888 /*
889 * If this is a From:/Resent-From: header, save the full thing for
890 * later in case we need it for use when constructing a Bcc draft message.
891 * Because we want to capture the results of alias expansion, save
892 * the output from putadr().
893 */
894
895 if ((msgstate == RESENT) ? (hdr->set & MRFM) : (hdr->set & MFRM)) {
896 savehdr = fullfrom;
897 savehdr[0] = '\0';
898 savehdrlen = sizeof(fullfrom);
899 }
900
901 tmpaddrs.m_next = NULL;
902
903 for (count = 0; (cp = getname (str)); count++) {
904 if ((mp = getm (cp, NULL, 0, error, sizeof(error)))) {
905 if (tmpaddrs.m_next)
906 np->m_next = mp;
907 else
908 tmpaddrs.m_next = mp;
909 np = mp;
910 }
911 else {
912 admonish(cp, "%s", error);
913 if (hdr->flags & HTRY)
914 badadr++;
915 else
916 badmsg = true;
917 }
918 }
919
920 if (count < 1) {
921 if (hdr->flags & HNIL)
922 fprintf (out, "%s: %s", name, str);
923 else {
924 /*
925 * Sender (or Resent-Sender) can have only one address
926 */
927 if ((msgstate == RESENT) ? (hdr->set & MRSN)
928 : (hdr->set & MSND)) {
929 inform("%s: field requires one address", name);
930 badmsg = true;
931 }
932 #ifdef notdef
933 inform("%s: field requires at least one address", name);
934 badmsg = true;
935 #endif /* notdef */
936 }
937 return;
938 }
939
940 if (count > 1 && (hdr->flags & HONE)) {
941 inform("%s: field only permits one address", name);
942 badmsg = true;
943 return;
944 }
945
946 nameoutput = linepos = 0;
947 snprintf (namep, sizeof(namep), "%s%s",
948 (hdr->flags & HMNG) ? "Original-" : "", name);
949
950 for (grp = false, mp = tmpaddrs.m_next; mp; mp = np)
951 if (mp->m_nohost) { /* also used to test (hdr->flags & HTRY) */
952 /* The address doesn't include a host, so it might be an alias. */
953 pp = akvalue (mp->m_mbox); /* do mh alias substitution */
954 qp = akvisible () ? mp->m_mbox : "";
955 np = mp;
956 if (np->m_gname)
957 putgrp (namep, np->m_gname, out, hdr->flags);
958 while ((cp = getname (pp))) {
959 if (!(mp = getm (cp, NULL, 0, error, sizeof(error)))) {
960 admonish(cp, "%s", error);
961 badadr++;
962 continue;
963 }
964
965 /*
966 * If it's a From: or Resent-From: header, save the address
967 * for later possible use (as the envelope address for SMTP)
968 */
969
970 if ((msgstate == RESENT) ? (hdr->set & MRFM)
971 : (hdr->set & MFRM)) {
972 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
973 from[sizeof(from) - 1] = '\0';
974 fromcount = count;
975 }
976
977 /*
978 * Also save the Sender: or Resent-Sender: header as well
979 */
980
981 if ((msgstate == RESENT) ? (hdr->set & MRSN)
982 : (hdr->set & MSND)) {
983 strncpy(sender, auxformat(mp, 0), sizeof(sender) - 1);
984 sender[sizeof(sender) - 1] = '\0';
985 seensender = true;
986 }
987
988 /*
989 * ALSO ... save Envelope-From
990 */
991
992 if (hdr->set & MEFM) {
993 strncpy(efrom, auxformat(mp, 0), sizeof(efrom) - 1);
994 efrom[sizeof(efrom) - 1] = '\0';
995 }
996
997 if (hdr->flags & HBCC)
998 mp->m_bcc++;
999 if (np->m_ingrp)
1000 mp->m_ingrp = np->m_ingrp;
1001 else if (mp->m_gname)
1002 putgrp (namep, mp->m_gname, out, hdr->flags);
1003 if (mp->m_ingrp) {
1004 if (sm_mts == MTS_SENDMAIL_PIPE) {
1005 /* Catch this before sendmail chokes with:
1006 "553 List:; syntax illegal for recipient
1007 addresses".
1008 If we wanted to, we could expand out blind
1009 aliases and put them in Bcc:, but then
1010 they'd have the Blind-Carbon-Copy
1011 indication. */
1012 die( "blind lists not compatible with"
1013 " sendmail/pipe");
1014 }
1015
1016 grp = true;
1017 }
1018 if (putadr (namep, qp, mp, out, hdr->flags, savehdr,
1019 savehdrlen))
1020 msgflags |= (hdr->set & (MVIS | MINV));
1021 else
1022 mnfree (mp);
1023 }
1024 mp = np;
1025 np = np->m_next;
1026 mnfree (mp);
1027 }
1028 else {
1029 /* Address includes a host, so no alias substitution is needed. */
1030
1031 /*
1032 * If it's a From: or Resent-From header, save the address
1033 * for later possible use (as the envelope address for SMTP)
1034 */
1035
1036 if ((msgstate == RESENT) ? (hdr->set & MRFM)
1037 : (hdr->set & MFRM)) {
1038 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
1039 fromcount = count;
1040 }
1041
1042 /*
1043 * Also save the Sender: header as well
1044 */
1045
1046 if ((msgstate == RESENT) ? (hdr->set & MRSN)
1047 : (hdr->set & MSND)) {
1048 strncpy(sender, auxformat(mp, 0), sizeof(sender) - 1);
1049 sender[sizeof(sender) - 1] = '\0';
1050 seensender = true;
1051 }
1052
1053 /*
1054 * ALSO ... save Envelope-From
1055 */
1056
1057 if (hdr->set & MEFM) {
1058 strncpy(efrom, auxformat(mp, 0), sizeof(efrom) - 1);
1059 efrom[sizeof(efrom) - 1] = '\0';
1060 }
1061
1062 if (hdr->flags & HBCC)
1063 mp->m_bcc++;
1064 if (mp->m_gname)
1065 putgrp (namep, mp->m_gname, out, hdr->flags);
1066 if (mp->m_ingrp)
1067 grp = true;
1068 keep = putadr (namep, "", mp, out, hdr->flags, savehdr, savehdrlen);
1069 np = mp->m_next;
1070 if (keep) {
1071 mp->m_next = NULL;
1072 msgflags |= (hdr->set & (MVIS | MINV));
1073 }
1074 else
1075 mnfree (mp);
1076 }
1077
1078 /*
1079 * If it was a From header, strip off any trailing newlines from
1080 * the alias-expanded From line.
1081 */
1082
1083 if ((msgstate == RESENT) ? (hdr->set & MRFM) : (hdr->set & MFRM)) {
1084 while (*fullfrom && fullfrom[strlen(fullfrom) - 1] == '\n') {
1085 fullfrom[strlen(fullfrom) - 1] = '\0';
1086 }
1087 }
1088
1089 if (grp && (hdr->flags & HNGR)) {
1090 inform("%s: field does not allow groups", name);
1091 badmsg = true;
1092 }
1093 if (linepos) {
1094 putc ('\n', out);
1095 }
1096 }
1097
1098
1099 static void
1100 start_headers (void)
1101 {
1102 time (&tclock);
1103
1104 /*
1105 * Probably not necessary, but just in case ...
1106 */
1107
1108 from[0] = '\0';
1109 efrom[0] = '\0';
1110 sender[0] = '\0';
1111 fullfrom[0] = '\0';
1112 }
1113
1114
1115 /*
1116 * Now that we've outputted the header fields in the draft
1117 * message, we will now output any remaining header fields
1118 * that we need to add/create.
1119 */
1120
1121 static void
1122 finish_headers (FILE *out)
1123 {
1124 switch (msgstate) {
1125 case NORMAL:
1126 if (!(msgflags & MFRM)) {
1127 /*
1128 * A From: header is now required in the draft.
1129 */
1130 inform("message has no From: header");
1131 inform("See default components files for examples");
1132 badmsg = true;
1133 break;
1134 }
1135
1136 if (fromcount > 1 && (!seensender && !(msgflags & MEFM))) {
1137 inform("A Sender: or Envelope-From: header is required "
1138 "with multiple\nFrom: addresses");
1139 badmsg = true;
1140 break;
1141 }
1142
1143 if (whomsw)
1144 break;
1145
1146 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1147 if (msgid)
1148 fprintf (out, "Message-ID: %s\n", message_id (tclock, 0));
1149 /*
1150 * If we have multiple From: addresses, make sure we have an
1151 * Sender: header. If we don't have one, then generate one
1152 * from Envelope-From: (which in this case, cannot be blank)
1153 */
1154
1155 if (fromcount > 1 && !seensender) {
1156 if (efrom[0] == '\0') {
1157 inform("Envelope-From cannot be blank when there "
1158 "is multiple From: addresses\nand no Sender: "
1159 "header");
1160 badmsg = true;
1161 } else {
1162 fprintf (out, "Sender: %s\n", efrom);
1163 }
1164 }
1165
1166 if (!(msgflags & MVIS))
1167 fprintf (out, "Bcc: Blind Distribution List: ;\n");
1168 break;
1169
1170 case RESENT:
1171 if (!(msgflags & MDAT)) {
1172 inform("message has no Date: header");
1173 badmsg = true;
1174 }
1175 if (!(msgflags & MFRM)) {
1176 inform("message has no From: header");
1177 badmsg = true;
1178 }
1179 if (!(msgflags & MRFM)) {
1180 inform("message has no Resent-From: header");
1181 inform("See default components files for examples");
1182 badmsg = true;
1183 break;
1184 }
1185 if (fromcount > 1 && (!seensender && !(msgflags & MEFM))) {
1186 inform("A Resent-Sender: or Envelope-From: header is "
1187 "required with multiple\nResent-From: addresses");
1188 badmsg = true;
1189 break;
1190 }
1191
1192 if (whomsw)
1193 break;
1194
1195 fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
1196 if (msgid)
1197 fprintf (out, "Resent-Message-ID: %s\n",
1198 message_id (tclock, 0));
1199 /*
1200 * If we have multiple Resent-From: addresses, make sure we have an
1201 * Resent-Sender: header. If we don't have one, then generate one
1202 * from Envelope-From (which in this case, cannot be blank)
1203 */
1204
1205 if (fromcount > 1 && !seensender) {
1206 if (efrom[0] == '\0') {
1207 inform("Envelope-From cannot be blank when there "
1208 "is multiple Resent-From: addresses and no "
1209 "Resent-Sender: header");
1210 badmsg = true;
1211 } else {
1212 fprintf (out, "Resent-Sender: %s\n", efrom);
1213 }
1214 }
1215
1216 if (!(msgflags & MVIS))
1217 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
1218 break;
1219 }
1220
1221 if (badmsg)
1222 die("re-format message and try again");
1223 if (!recipients)
1224 die("no addressees");
1225 }
1226
1227
1228 static int
1229 get_header (char *header, struct headers *table)
1230 {
1231 struct headers *h;
1232
1233 header = FENDNULL(header);
1234 for (h = table; h->value; h++)
1235 if (!strcasecmp(FENDNULL(h->value), header))
1236 return h - table;
1237
1238 return NOTOK;
1239 }
1240
1241
1242 static int
1243 putadr (char *name, char *aka, struct mailname *mp, FILE *out,
1244 unsigned int flags, char *savehdr, unsigned int savehdrsize)
1245 {
1246 int len;
1247 bool saveappend = false;
1248 unsigned int shlen;
1249 char *cp;
1250 char buffer[BUFSIZ];
1251
1252 if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
1253 return 0;
1254 if (sm_mts != MTS_SENDMAIL_PIPE &&
1255 ((flags & (HBCC | HDCC | HEFM)) || mp->m_ingrp))
1256 return 1;
1257
1258 if (!nameoutput) {
1259 fprintf (out, "%s: ", name);
1260 linepos += (nameoutput = strlen (name) + 2);
1261 }
1262
1263 if (savehdr) {
1264 shlen = strlen(savehdr);
1265 saveappend = true;
1266 }
1267
1268 if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
1269 mp->m_pers = mh_xstrdup(aka);
1270 if (format) {
1271 if (mp->m_gname) {
1272 snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
1273 cp = buffer;
1274 } else {
1275 cp = adrformat (mp);
1276 }
1277 } else {
1278 cp = mp->m_text;
1279 }
1280 len = strlen (cp);
1281
1282 if (linepos != nameoutput) {
1283 if (len + linepos + 2 > outputlinelen) {
1284 fprintf (out, ",\n%*s", linepos = nameoutput, "");
1285 if (saveappend) {
1286 if (shlen + 2 + nameoutput + len >= savehdrsize) {
1287 saveappend = false;
1288 } else {
1289 snprintf(savehdr + shlen, savehdrsize - shlen, ",\n%*s",
1290 linepos, "");
1291 }
1292 }
1293 } else {
1294 fputs (", ", out);
1295 linepos += 2;
1296 if (saveappend) {
1297 if (shlen + 2 + len >= savehdrsize) {
1298 saveappend = false;
1299 } else {
1300 strncat(savehdr, ", ", savehdrsize - shlen);
1301 }
1302 }
1303 }
1304 }
1305
1306 fputs (cp, out);
1307
1308 if (saveappend && shlen + len < savehdrsize)
1309 strncat(savehdr, cp, savehdrsize - shlen + len);
1310
1311 linepos += len;
1312
1313 return flags & HTRY;
1314 }
1315
1316
1317 static void
1318 putgrp (char *name, char *group, FILE *out, unsigned int flags)
1319 {
1320 int len;
1321 char *cp;
1322
1323 if (sm_mts != MTS_SENDMAIL_PIPE && (flags & HBCC))
1324 return;
1325
1326 if (!nameoutput) {
1327 fprintf (out, "%s: ", name);
1328 linepos += (nameoutput = strlen (name) + 2);
1329 }
1330
1331 cp = concat (group, ";", NULL);
1332 len = strlen (cp);
1333
1334 if (linepos > nameoutput) {
1335 if (len + linepos + 2 > outputlinelen) {
1336 fprintf (out, ",\n%*s", nameoutput, "");
1337 linepos = nameoutput;
1338 }
1339 else {
1340 fputs (", ", out);
1341 linepos += 2;
1342 }
1343 }
1344
1345 fputs (cp, out);
1346 linepos += len;
1347 }
1348
1349
1350 static int
1351 insert (struct mailname *np)
1352 {
1353 struct mailname *mp;
1354
1355 if (np->m_mbox == NULL)
1356 return 0;
1357
1358 for (mp = np->m_type == LOCALHOST ? &localaddrs
1359 : np->m_type == UUCPHOST ? &uuaddrs
1360 : &netaddrs;
1361 mp->m_next;
1362 mp = mp->m_next)
1363 if (!strcasecmp (FENDNULL(np->m_host),
1364 FENDNULL(mp->m_next->m_host)) &&
1365 !strcasecmp (FENDNULL(np->m_mbox),
1366 FENDNULL(mp->m_next->m_mbox)) &&
1367 np->m_bcc == mp->m_next->m_bcc)
1368 return 0;
1369
1370 mp->m_next = np;
1371 recipients = true;
1372 return 1;
1373 }
1374
1375
1376 static void
1377 pl (void)
1378 {
1379 int i;
1380 struct mailname *mp;
1381
1382 fputs("-------\n\t-- Addresses --\nlocal:\t", stdout);
1383 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1384 printf ("%s%s%s", mp->m_mbox,
1385 mp->m_bcc ? "[BCC]" : "",
1386 mp->m_next ? ",\n\t" : "");
1387
1388 fputs("\nnet:\t", stdout);
1389 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1390 printf ("%s%s@%s%s%s", FENDNULL(mp->m_path),
1391 mp->m_mbox, mp->m_host,
1392 mp->m_bcc ? "[BCC]" : "",
1393 mp->m_next ? ",\n\t" : "");
1394
1395 fputs("\nuucp:\t", stdout);
1396 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1397 printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1398 mp->m_bcc ? "[BCC]" : "",
1399 mp->m_next ? ",\n\t" : "");
1400
1401 fputs("\n\t-- Folder Copies --\nfcc:\t", stdout);
1402 for (i = 0; i < fccind; i++)
1403 printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1404 putchar('\n');
1405 }
1406
1407
1408 static void
1409 anno (void)
1410 {
1411 struct mailname *mp;
1412
1413 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1414 if (annoaux (mp) == NOTOK)
1415 goto oops;
1416
1417 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1418 if (annoaux (mp) == NOTOK)
1419 goto oops;
1420
1421 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1422 if (annoaux (mp) == NOTOK)
1423 break;
1424
1425 oops: ;
1426 close (pfd);
1427 pfd = NOTOK;
1428 }
1429
1430
1431 static int
1432 annoaux (struct mailname *mp)
1433 {
1434 int i;
1435 char buffer[BUFSIZ];
1436
1437 snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1438 i = strlen (buffer);
1439
1440 return write(pfd, buffer, i) == i ? OK : NOTOK;
1441 }
1442
1443
1444 static void
1445 insert_fcc (struct headers *hdr, char *pp)
1446 {
1447 char *cp;
1448
1449 for (cp = pp; isspace ((unsigned char) *cp); cp++)
1450 continue;
1451 for (pp += strlen (pp) - 1; pp > cp && isspace ((unsigned char) *pp); pp--)
1452 continue;
1453 if (pp >= cp)
1454 *++pp = 0;
1455 if (*cp == 0)
1456 return;
1457
1458 if (fccind >= FCCS)
1459 die("too many %ss", hdr->value);
1460 fccfold[fccind++] = mh_xstrdup(cp);
1461 }
1462
1463 /*
1464 * BCC GENERATION
1465 */
1466
1467 static void
1468 make_bcc_file (int dashstuff)
1469 {
1470 int fd, i;
1471 pid_t child_id;
1472 char **vec;
1473 FILE *out;
1474 char *tfile = NULL, *program;
1475
1476 if ((tfile = m_mktemp2(NULL, "bccs", NULL, &out)) == NULL) {
1477 die("unable to create temporary file in %s", get_temp_dir());
1478 }
1479 strncpy (bccfil, tfile, sizeof(bccfil));
1480
1481 fprintf (out, "From: %s\n", fullfrom);
1482 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1483 if (msgid)
1484 fprintf (out, "Message-ID: %s\n", message_id (tclock, 0));
1485 if (subject)
1486 fprintf (out, "Subject: %s", subject);
1487 fprintf (out, "BCC:\n");
1488
1489 /*
1490 * Use MIME encapsulation for Bcc messages
1491 */
1492 if (mime) {
1493 char *cp;
1494
1495 /*
1496 * Check if any lines in the message clash with the
1497 * prefix for the MIME multipart separator. If there
1498 * is a clash, increment one of the letters in the
1499 * prefix and check again.
1500 */
1501 if ((cp = strchr(prefix, 'a')) == NULL)
1502 die("lost prefix start");
1503 while (find_prefix () == NOTOK) {
1504 if (*cp < 'z')
1505 (*cp)++;
1506 else if (*++cp == 0)
1507 die("can't find a unique delimiter string");
1508 else
1509 (*cp)++;
1510 }
1511
1512 fprintf (out, "%s: %s\n%s: multipart/digest; boundary=\"",
1513 VRSN_FIELD, VRSN_VALUE, TYPE_FIELD);
1514 fprintf (out, "%s\"\n\n--%s\n\n", prefix, prefix);
1515 } else {
1516 fprintf (out, "\n------- Blind-Carbon-Copy\n\n");
1517 }
1518
1519 fflush (out);
1520
1521 /*
1522 * Do mhl filtering of Bcc messages instead
1523 * of MIME encapsulation.
1524 */
1525 if (filter != NULL) {
1526 child_id = fork();
1527 switch (child_id) {
1528 case NOTOK:
1529 adios ("fork", "unable to");
1530
1531 case OK:
1532 dup2 (fileno (out), 1);
1533
1534 vec = argsplit(mhlproc, &program, &i);
1535 vec[i++] = "-forward";
1536 vec[i++] = "-form";
1537 vec[i++] = filter;
1538 vec[i++] = tmpfil;
1539
1540 /* was the flag -[no]dashstuffing specified? */
1541 if (dashstuff > 0)
1542 vec[i++] = "-dashstuffing";
1543 else if (dashstuff < 0)
1544 vec[i++] = "-nodashstuffing";
1545 vec[i] = NULL;
1546
1547 execvp (program, vec);
1548 fprintf (stderr, "unable to exec ");
1549 perror (mhlproc);
1550 _exit(1);
1551
1552 default:
1553 pidXwait (child_id, mhlproc);
1554 break;
1555 }
1556 } else {
1557 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
1558 adios (tmpfil, "unable to re-open");
1559
1560 /*
1561 * If using MIME encapsulation, or if the -nodashstuffing
1562 * flag was given, then just copy message. Else do
1563 * RFC934 quoting (dashstuffing).
1564 */
1565 if (mime || dashstuff < 0)
1566 cpydata (fd, fileno (out), tmpfil, bccfil);
1567 else
1568 cpydgst (fd, fileno (out), tmpfil, bccfil);
1569 close (fd);
1570 }
1571
1572 fseek (out, 0L, SEEK_END);
1573 if (mime)
1574 fprintf (out, "\n--%s--\n", prefix);
1575 else
1576 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
1577 fclose (out);
1578 }
1579
1580
1581 /*
1582 * Scan message to check if any lines clash with
1583 * the prefix of the MIME multipart separator.
1584 */
1585
1586 static int
1587 find_prefix (void)
1588 {
1589 int result = OK;
1590 char buffer[BUFSIZ];
1591 FILE *in;
1592
1593 if ((in = fopen (tmpfil, "r")) == NULL)
1594 adios (tmpfil, "unable to re-open");
1595
1596 while (fgets (buffer, sizeof buffer, in))
1597 if (buffer[0] == '-' && buffer[1] == '-') {
1598 char *cp;
1599
1600 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1601 if (!isspace ((unsigned char) *cp))
1602 break;
1603 *++cp = '\0';
1604 if (strcmp (buffer + 2, prefix) == 0) {
1605 result = NOTOK;
1606 break;
1607 }
1608 }
1609
1610 fclose (in);
1611 return result;
1612 }
1613
1614
1615 static void
1616 chkadr (void)
1617 {
1618 if (badadr && unkadr)
1619 fatal (NULL, "%d address%s unparsable, %d addressee%s undeliverable",
1620 badadr, PLURALS(badadr), unkadr, PLURALS(badadr));
1621 if (badadr)
1622 fatal (NULL, "%d address%s unparsable", badadr, PLURALS(badadr));
1623 if (unkadr)
1624 fatal (NULL, "%d addressee%s undeliverable", unkadr, PLURALS(unkadr));
1625 }
1626
1627
1628 static void
1629 do_addresses (int bccque, int talk)
1630 {
1631 int retval;
1632 int state;
1633 struct mailname *lp;
1634
1635 state = 0;
1636 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1637 if (lp->m_bcc ? bccque : !bccque) {
1638 if (talk && !state)
1639 puts(" -- Local Recipients --");
1640 do_an_address (lp, talk);
1641 state++;
1642 }
1643
1644 state = 0;
1645 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1646 if (lp->m_bcc ? bccque : !bccque) {
1647 if (talk && !state)
1648 puts(" -- UUCP Recipients --");
1649 do_an_address (lp, talk);
1650 state++;
1651 }
1652
1653 state = 0;
1654 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1655 if (lp->m_bcc ? bccque : !bccque) {
1656 if (talk && !state)
1657 puts(" -- Network Recipients --");
1658 do_an_address (lp, talk);
1659 state++;
1660 }
1661
1662 chkadr ();
1663
1664 if (rp_isbad (retval = sm_waend ()))
1665 fatal (NULL, "problem ending addresses; %s", rp_string (retval));
1666 }
1667
1668
1669 /*
1670 * MTS-SPECIFIC INTERACTION
1671 */
1672
1673
1674 /*
1675 * SENDMAIL/SMTP routines
1676 */
1677
1678 static void
1679 post (char *file, int bccque, int talk, int eai, char *envelope,
1680 int oauth_flag, char *auth_svc)
1681 {
1682 int retval;
1683 pid_t child_id;
1684
1685 if (verbose) {
1686 if (msgflags & MINV)
1687 printf (" -- Posting for %s Recipients --\n",
1688 bccque ? "Blind" : "Sighted");
1689 else
1690 puts(" -- Posting for All Recipients --");
1691 }
1692
1693 sigon ();
1694
1695 if (sm_mts == MTS_SENDMAIL_PIPE) {
1696 char **argp, *program;
1697 int argc;
1698
1699 child_id = fork();
1700 switch (child_id) {
1701 case NOTOK:
1702 adios ("fork", "unable to");
1703
1704 case OK:
1705 if (freopen( file, "r", stdin) == NULL) {
1706 adios (file, "can't reopen for sendmail");
1707 }
1708
1709 argp = argsplit(sendmail, &program, &argc);
1710 argp[argc++] = "-t"; /* read msg for recipients */
1711 argp[argc++] = "-i"; /* don't stop on "." */
1712 if (whomsw)
1713 argp[argc++] = "-bv";
1714 if (snoop)
1715 argp[argc++] = "-v";
1716 argp[argc] = NULL;
1717
1718 execv (program, argp);
1719 adios (sendmail, "can't exec");
1720
1721 default:
1722 pidXwait (child_id, NULL);
1723 break;
1724 }
1725 } else {
1726 const int fd = open (file, O_RDONLY);
1727 int eightbit = 0;
1728
1729 if (fd == NOTOK) {
1730 fatal (file, "unable to re-open");
1731 }
1732
1733 if (msgflags & MMIM && cte != UNKNOWN) {
1734 /* MIME message with C-T-E header. (BINARYMIME isn't
1735 supported, use 8BITMIME instead for binary.) */
1736 eightbit = cte != SEVENBIT;
1737 } else {
1738 if (scan_input (fd, &eightbit) == NOTOK) {
1739 close (fd);
1740 fatal (file, "problem reading from");
1741 }
1742 }
1743
1744 if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch,
1745 verbose, snoop, sasl, saslmech, user,
1746 oauth_flag ? auth_svc : NULL, tlsflag))
1747 || rp_isbad (retval = sm_winit (envelope, eai, eightbit))) {
1748 close (fd);
1749 fatal (NULL, "problem initializing server; %s", rp_string (retval));
1750 }
1751
1752 do_addresses (bccque, talk && verbose);
1753 do_text (file, fd);
1754 close (fd);
1755 fflush (stdout);
1756
1757 sm_end (!(msgflags & MINV) || bccque ? OK : DONE);
1758 sigoff ();
1759
1760 if (verbose) {
1761 if (msgflags & MINV)
1762 printf (" -- %s Recipient Copies Posted --\n",
1763 bccque ? "Blind" : "Sighted");
1764 else
1765 puts(" -- Recipient Copies Posted --");
1766 }
1767
1768 fflush (stdout);
1769 }
1770 }
1771
1772
1773 /* Address Verification */
1774
1775 static void
1776 verify_all_addresses (int talk, int eai, char *envelope, int oauth_flag,
1777 char *auth_svc)
1778 {
1779 int retval;
1780 struct mailname *lp;
1781
1782 sigon ();
1783
1784 if (!whomsw || checksw) {
1785 /* Not sending message body, so don't need to use 8BITMIME. */
1786 const int eightbit = 0;
1787
1788 if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch,
1789 verbose, snoop, sasl, saslmech, user,
1790 oauth_flag ? auth_svc : NULL, tlsflag))
1791 || rp_isbad (retval = sm_winit (envelope, eai, eightbit))) {
1792 fatal (NULL, "problem initializing server; %s", rp_string (retval));
1793 }
1794 }
1795
1796 if (talk && !whomsw)
1797 puts(" -- Address Verification --");
1798 if (talk && localaddrs.m_next)
1799 puts(" -- Local Recipients --");
1800 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1801 do_an_address (lp, talk);
1802
1803 if (talk && uuaddrs.m_next)
1804 puts(" -- UUCP Recipients --");
1805 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1806 do_an_address (lp, talk);
1807
1808 if (talk && netaddrs.m_next)
1809 puts(" -- Network Recipients --");
1810 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1811 do_an_address (lp, talk);
1812
1813 chkadr ();
1814 if (talk && !whomsw)
1815 puts(" -- Address Verification Successful --");
1816
1817 if (!whomsw || checksw)
1818 sm_end (whomsw ? OK : DONE);
1819
1820 fflush (stdout);
1821 sigoff ();
1822 }
1823
1824
1825 static void
1826 do_an_address (struct mailname *lp, int talk)
1827 {
1828 int retval;
1829 char *mbox, *host;
1830 char addr[BUFSIZ];
1831
1832 switch (lp->m_type) {
1833 case LOCALHOST:
1834 mbox = lp->m_mbox;
1835 host = lp->m_host;
1836 strncpy (addr, mbox, sizeof(addr));
1837 break;
1838
1839 case UUCPHOST:
1840 mbox = auxformat (lp, 0);
1841 host = NULL;
1842 snprintf (addr, sizeof(addr), "%s!%s", lp->m_host, lp->m_mbox);
1843 break;
1844
1845 default: /* let SendMail decide if the host is bad */
1846 mbox = lp->m_mbox;
1847 host = lp->m_host;
1848 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1849 break;
1850 }
1851
1852 if (talk)
1853 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1854
1855 if (whomsw && !checksw) {
1856 putchar ('\n');
1857 return;
1858 }
1859 if (talk)
1860 fputs(": ", stdout);
1861 fflush (stdout);
1862
1863 switch (retval = sm_wadr (mbox, host,
1864 lp->m_type != UUCPHOST ? lp->m_path : NULL)) {
1865 case RP_OK:
1866 if (talk)
1867 puts("address ok");
1868 break;
1869
1870 case RP_NO:
1871 case RP_USER:
1872 if (!talk)
1873 fprintf (stderr, " %s: ", addr);
1874 fprintf (talk ? stdout : stderr, "loses; %s\n",
1875 rp_string (retval));
1876 unkadr++;
1877 break;
1878
1879 default:
1880 if (!talk)
1881 fprintf (stderr, " %s: ", addr);
1882 fatal (NULL, "unexpected response; %s", rp_string (retval));
1883 }
1884
1885 fflush (stdout);
1886 }
1887
1888
1889 static void
1890 do_text (char *file, int fd)
1891 {
1892 int retval, state;
1893 char buf[BUFSIZ];
1894
1895 lseek(fd, 0, SEEK_SET);
1896
1897 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1898 if (rp_isbad (retval = sm_wtxt (buf, state)))
1899 fatal (NULL, "problem writing text; %s\n", rp_string (retval));
1900 }
1901
1902 if (state == NOTOK)
1903 fatal (file, "problem reading from");
1904
1905 switch (retval = sm_wtend ()) {
1906 case RP_OK:
1907 break;
1908
1909 case RP_NO:
1910 case RP_NDEL:
1911 fatal (NULL, "posting failed; %s", rp_string (retval));
1912 break;
1913
1914 default:
1915 fatal (NULL, "unexpected response; %s", rp_string (retval));
1916 }
1917 }
1918
1919
1920 /*
1921 * SIGNAL HANDLING
1922 */
1923
1924 static void
1925 sigser (int i)
1926 {
1927 NMH_UNUSED (i);
1928
1929 (void) m_unlink (tmpfil);
1930 if (msgflags & MINV)
1931 (void) m_unlink (bccfil);
1932
1933 if (!whomsw || checksw)
1934 sm_end (NOTOK);
1935
1936 done (1);
1937 }
1938
1939
1940 static void
1941 sigon (void)
1942 {
1943 if (debug)
1944 return;
1945
1946 hstat = SIGNAL2 (SIGHUP, sigser);
1947 istat = SIGNAL2 (SIGINT, sigser);
1948 qstat = SIGNAL2 (SIGQUIT, sigser);
1949 tstat = SIGNAL2 (SIGTERM, sigser);
1950 }
1951
1952
1953 static void
1954 sigoff (void)
1955 {
1956 if (debug)
1957 return;
1958
1959 SIGNAL (SIGHUP, hstat);
1960 SIGNAL (SIGINT, istat);
1961 SIGNAL (SIGQUIT, qstat);
1962 SIGNAL (SIGTERM, tstat);
1963 }
1964
1965 /*
1966 * FCC INTERACTION
1967 */
1968
1969 static void
1970 p_refile (char *file)
1971 {
1972 int i;
1973
1974 if (fccind == 0)
1975 return;
1976
1977 if (verbose)
1978 puts(" -- Filing Folder Copies --");
1979 for (i = 0; i < fccind; i++)
1980 fcc (file, fccfold[i]);
1981 if (verbose)
1982 puts(" -- Folder Copies Filed --");
1983 }
1984
1985
1986 /*
1987 * Call the `fileproc' to add the file to the folder.
1988 */
1989
1990 static void
1991 fcc (char *file, char *folder)
1992 {
1993 pid_t child_id;
1994 int status, argp;
1995 char fold[BUFSIZ];
1996 char **arglist, *program;
1997
1998 if (verbose)
1999 printf (" %sFcc %s: ", msgstate == RESENT ? "Resent-" : "", folder);
2000 fflush (stdout);
2001
2002 child_id = fork();
2003 switch (child_id) {
2004 case NOTOK:
2005 if (!verbose)
2006 fprintf (stderr, " %sFcc %s: ",
2007 msgstate == RESENT ? "Resent-" : "", folder);
2008 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
2009 break;
2010
2011 case OK:
2012 /* see if we need to add `+' */
2013 snprintf (fold, sizeof(fold), "%s%s",
2014 *folder == '+' || *folder == '@' ? "" : "+", folder);
2015
2016 /* now exec the fileproc */
2017
2018 arglist = argsplit(fileproc, &program, &argp);
2019 arglist[argp++] = "-link";
2020 arglist[argp++] = "-file";
2021 arglist[argp++] = file;
2022 arglist[argp++] = fold;
2023 arglist[argp] = NULL;
2024 execvp (program, arglist);
2025 _exit(1);
2026
2027 default:
2028 if ((status = pidwait (child_id, OK))) {
2029 if (!verbose)
2030 fprintf (stderr, " %sFcc %s: ",
2031 msgstate == RESENT ? "Resent-" : "", folder);
2032 pidstatus (status, verbose ? stdout : stderr, fileproc);
2033 } else {
2034 if (verbose)
2035 puts("folder ok");
2036 }
2037 }
2038
2039 fflush (stdout);
2040 }
2041
2042 /*
2043 * TERMINATION
2044 */
2045
2046 static void
2047 fatal (char *what, char *fmt, ...)
2048 {
2049 int err;
2050 va_list ap;
2051
2052 err = errno;
2053
2054 (void) m_unlink (tmpfil);
2055 if (msgflags & MINV)
2056 (void) m_unlink (bccfil);
2057
2058 if (!whomsw || checksw)
2059 sm_end (NOTOK);
2060
2061 va_start(ap, fmt);
2062 errno = err;
2063 advertise (what, NULL, fmt, ap);
2064 va_end(ap);
2065 done (1);
2066 }