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