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