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