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