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