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