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