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