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