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