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