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