]>
diplodocus.org Git - nmh/blob - uip/post.c
0660f17be9267cfa87f890a40bcaf7b22a0cced1
3 * post.c -- enter messages into the mail transport system
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
13 #include <h/addrsbr.h>
14 #include <h/aliasbr.h>
15 #include <h/dropsbr.h>
21 #ifdef HAVE_SYS_TIME_H
22 # include <sys/time.h>
26 #include <mts/smtp/smtp.h>
29 # define SASLminc(a) (a)
30 #else /* CYRUS_SASL */
31 # define SASLminc(a) 0
32 #endif /* CYRUS_SASL */
35 # define TLSminc(a) (a)
36 #else /* TLS_SUPPORT */
38 #endif /* TLS_SUPPORT */
41 # define OAUTHminc(a) (a)
42 #else /* OAUTH_SUPPORT */
43 # define OAUTHmine(a) 0
44 #endif /* OAUTH_SUPPORT */
46 #define FCCS 10 /* max number of fccs allowed */
48 /* In the following array of structures, the numeric second field of the
49 structures (minchars) is apparently used like this:
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. */
55 #define POST_SWITCHES \
56 X("alias aliasfile", 0, ALIASW) \
57 X("check", -5, CHKSW) /* interface from whom */ \
58 X("nocheck", -7, NCHKSW) /* interface from whom */ \
59 X("debug", -5, DEBUGSW) \
60 X("dist", -4, DISTSW) /* interface from dist */ \
61 X("filter filterfile", 0, FILTSW) \
62 X("nofilter", 0, NFILTSW) \
63 X("format", 0, FRMTSW) \
64 X("noformat", 0, NFRMTSW) \
65 X("library directory", -7, LIBSW) /* interface from send, whom */ \
66 X("mime", 0, MIMESW) \
67 X("nomime", 0, NMIMESW) \
68 X("msgid", 0, MSGDSW) \
69 X("nomsgid", 0, NMSGDSW) \
70 X("verbose", 0, VERBSW) \
71 X("noverbose", 0, NVERBSW) \
72 X("watch", 0, WATCSW) \
73 X("nowatch", 0, NWATCSW) \
74 X("whom", -4, WHOMSW) /* interface from whom */ \
75 X("width columns", 0, WIDTHSW) \
76 X("version", 0, VERSIONSW) \
77 X("help", 0, HELPSW) \
78 X("dashstuffing", -12, BITSTUFFSW) /* should we dashstuff BCC messages? */ \
79 X("nodashstuffing", -14, NBITSTUFFSW) \
80 X("idanno number", -6, ANNOSW) /* interface from send */ \
81 X("client host", -6, CLIESW) \
82 X("server host", 6, SERVSW) /* specify alternate SMTP server */ \
83 X("snoop", -5, SNOOPSW) /* snoop the SMTP transaction */ \
84 X("partno", -6, PARTSW) \
85 X("sasl", SASLminc(4), SASLSW) \
86 X("nosasl", SASLminc(6), NOSASLSW) \
87 X("saslmaxssf", SASLminc(10), SASLMXSSFSW) \
88 X("saslmech", SASLminc(5), SASLMECHSW) \
89 X("authservice", SASLminc(0), AUTHSERVICESW) \
90 X("user", SASLminc(-4), USERSW) \
91 X("port server submission port name/number", 4, PORTSW) \
92 X("tls", TLSminc(-3), TLSSW) \
93 X("initialtls", TLSminc(-10), INITTLSSW) \
94 X("notls", TLSminc(-5), NTLSSW) \
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("oauthcredfile", OAUTHminc(-7), OAUTHCREDFILESW) \
102 X("oauthclientid", OAUTHminc(-12), OAUTHCLIDSW) \
103 X("oauthclientsecret", OAUTHminc(-12), OAUTHCLSECSW) \
104 X("oauthauthendpoint", OAUTHminc(-6), OAUTHAUTHENDSW) \
105 X("oauthredirect", OAUTHminc(-6), OAUTHREDIRSW) \
106 X("oauthtokenendpoint", OAUTHminc(-6), OAUTHTOKENDSW) \
107 X("oauthscope", OAUTHminc(-6), OAUTHSCOPESW) \
109 #define X(sw, minchars, id) id,
110 DEFINE_SWITCH_ENUM(POST
);
113 #define X(sw, minchars, id) { sw, minchars, id },
114 DEFINE_SWITCH_ARRAY(POST
, switches
);
125 * flags for headers->flags
127 #define HNOP 0x0000 /* just used to keep .set around */
128 #define HBAD 0x0001 /* bad header - don't let it through */
129 #define HADR 0x0002 /* header has an address field */
130 #define HSUB 0x0004 /* Subject: header */
131 #define HTRY 0x0008 /* try to send to addrs on header */
132 #define HBCC 0x0010 /* don't output this header, unless MTS_SENDMAIL_PIPE */
133 #define HMNG 0x0020 /* munge this header */
134 #define HNGR 0x0040 /* no groups allowed in this header */
135 #define HFCC 0x0080 /* FCC: type header */
136 #define HNIL 0x0100 /* okay for this header not to have addrs */
137 #define HIGN 0x0200 /* ignore this header */
138 #define HDCC 0x0400 /* another undocumented feature */
139 #define HONE 0x0800 /* Only (zero or) one address allowed */
140 #define HEFM 0x1000 /* Envelope-From: header */
143 * flags for headers->set
145 #define MFRM 0x0001 /* we've seen a From: */
146 #define MDAT 0x0002 /* we've seen a Date: */
147 #define MRFM 0x0004 /* we've seen a Resent-From: */
148 #define MVIS 0x0008 /* we've seen sighted addrs */
149 #define MINV 0x0010 /* we've seen blind addrs */
150 #define MSND 0x0020 /* we've seen a Sender: */
151 #define MRSN 0x0040 /* We've seen a Resent-Sendr:*/
152 #define MEFM 0x0080 /* We've seen Envelope-From: */
155 static struct headers NHeaders
[] = {
156 { "Return-Path", HBAD
, 0 },
157 { "Received", HBAD
, 0 },
158 { "Reply-To", HADR
|HNGR
, 0 },
159 { "From", HADR
|HNGR
, MFRM
},
160 { "Sender", HADR
|HNGR
|HONE
, MSND
},
162 { "Subject", HSUB
, 0 },
163 { "To", HADR
|HTRY
, MVIS
},
164 { "cc", HADR
|HTRY
, MVIS
},
165 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, MINV
},
166 { "Dcc", HADR
|HTRY
|HDCC
|HNIL
, MVIS
}, /* sorta cc & bcc combined */
167 { "Message-ID", HBAD
, 0 },
169 { "Envelope-From", HADR
|HONE
|HEFM
, MEFM
},
173 static struct headers RHeaders
[] = {
174 { "Resent-Reply-To", HADR
|HNGR
, 0 },
175 { "Resent-From", HADR
|HNGR
, MRFM
},
176 { "Resent-Sender", HADR
|HNGR
, MRSN
},
177 { "Resent-Date", HBAD
, 0 },
178 { "Resent-Subject", HSUB
, 0 },
179 { "Resent-To", HADR
|HTRY
, MVIS
},
180 { "Resent-cc", HADR
|HTRY
, MVIS
},
181 { "Resent-Bcc", HADR
|HTRY
|HBCC
, MINV
},
182 { "Resent-Message-ID", HBAD
, 0 },
183 { "Resent-Fcc", HFCC
, 0 },
184 { "Reply-To", HADR
, 0 },
185 { "From", HADR
|HNGR
, MFRM
},
186 { "Sender", HADR
|HNGR
, MSND
},
187 { "Date", HNOP
, MDAT
},
188 { "To", HADR
|HNIL
, 0 },
189 { "cc", HADR
|HNIL
, 0 },
190 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, 0 },
192 { "Envelope-From", HADR
|HONE
|HEFM
, MEFM
},
196 static short fccind
= 0; /* index into fccfold[] */
197 static short outputlinelen
= OUTPUTLINELEN
;
199 static int pfd
= NOTOK
; /* fd to write annotation list to */
200 static int recipients
= 0; /* how many people will get a copy */
201 static int unkadr
= 0; /* how many of those were unknown */
202 static int badadr
= 0; /* number of bad addrs */
203 static int badmsg
= 0; /* message has bad semantics */
204 static int verbose
= 0; /* spell it out */
205 static int format
= 1; /* format addresses */
206 static int mime
= 0; /* use MIME-style encapsulations for Bcc */
207 static int msgid
= 0; /* add msgid */
208 static int debug
= 0; /* debugging post */
209 static int watch
= 0; /* watch the delivery process */
210 static int whomsw
= 0; /* we are whom not post */
211 static int checksw
= 0; /* whom -check */
212 static int linepos
=0; /* putadr()'s position on the line */
213 static int nameoutput
=0; /* putadr() has output header name */
214 static int sasl
=0; /* Use SASL auth for SMTP */
215 static int saslssf
=-1; /* Our maximum SSF for SASL */
216 static char *saslmech
=NULL
; /* Force use of particular SASL mech */
217 static char *user
=NULL
; /* Authenticate as this user */
218 static char *port
="submission"; /* Name of server port for SMTP submission */
219 static int tls
=-1; /* Use TLS for encryption */
220 static int fromcount
=0; /* Count of addresses on From: header */
221 static int seensender
=0; /* Have we seen a Sender: header? */
223 static unsigned msgflags
= 0; /* what we've seen */
227 static int msgstate
= NORMAL
;
229 static time_t tclock
= 0; /* the time we started (more or less) */
231 static SIGNAL_HANDLER hstat
, istat
, qstat
, tstat
;
233 static char tmpfil
[BUFSIZ
];
234 static char bccfil
[BUFSIZ
];
236 static char from
[BUFSIZ
]; /* my network address */
237 static char sender
[BUFSIZ
]; /* my Sender: header */
238 static char efrom
[BUFSIZ
]; /* my Envelope-From: header */
239 static char fullfrom
[BUFSIZ
]; /* full contents of From header */
240 static char *filter
= NULL
; /* the filter for BCC'ing */
241 static char *subject
= NULL
; /* the subject field for BCC'ing */
242 static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
244 static struct headers
*hdrtab
; /* table for the message we're doing */
246 static struct mailname localaddrs
; /* local addrs */
247 static struct mailname netaddrs
; /* network addrs */
248 static struct mailname uuaddrs
; /* uucp addrs */
249 static struct mailname tmpaddrs
; /* temporary queue */
251 static int snoop
= 0;
252 static char *clientsw
= NULL
;
253 static char *serversw
= NULL
;
255 static char prefix
[] = "----- =_aaaaaaaaaa";
257 static char *partno
= NULL
;
262 static void putfmt (char *, char *, FILE *);
263 static void start_headers (void);
264 static void finish_headers (FILE *);
265 static int get_header (char *, struct headers
*);
266 static int putadr (char *, char *, struct mailname
*, FILE *, unsigned int);
267 static void putgrp (char *, char *, FILE *, unsigned int);
268 static int insert (struct mailname
*);
269 static void pl (void);
270 static void anno (void);
271 static int annoaux (struct mailname
*);
272 static void insert_fcc (struct headers
*, char *);
273 static void make_bcc_file (int);
274 static void verify_all_addresses (int, char *, const char *);
275 static void chkadr (void);
276 static void sigon (void);
277 static void sigoff (void);
278 static void p_refile (char *);
279 static void fcc (char *, char *);
280 static void die (char *, char *, ...);
281 static void post (char *, int, int, char *, const char *);
282 static void do_text (char *file
, int fd
);
283 static void do_an_address (struct mailname
*, int);
284 static void do_addresses (int, int);
285 static int find_prefix (void);
289 main (int argc
, char **argv
)
291 int state
, compnum
, dashstuff
= 0;
292 char *cp
, *msg
= NULL
, **argp
, **arguments
, *envelope
;
293 char buf
[BUFSIZ
], name
[NAMESZ
];
295 m_getfld_state_t gstate
= 0;
296 char *xoauth_client_res
= NULL
;
298 if (nmh_init(argv
[0], 0 /* use context_foil() */)) { return 1; }
300 mts_init (invo_name
);
301 arguments
= getarguments (invo_name
, argc
, argv
, 0);
304 while ((cp
= *argp
++)) {
306 switch (smatch (++cp
, switches
)) {
308 ambigsw (cp
, switches
);
311 adios (NULL
, "-%s unknown", cp
);
314 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
315 print_help (buf
, switches
, 0);
318 print_version(invo_name
);
322 if (!(cp
= *argp
++) || *cp
== '-')
323 adios (NULL
, "missing argument to %s", argp
[-2]);
324 /* create a minimal context */
325 if (context_foil (cp
) == -1)
330 if (!(cp
= *argp
++) || *cp
== '-')
331 adios (NULL
, "missing argument to %s", argp
[-2]);
332 if ((state
= alias (cp
)) != AK_OK
)
333 adios (NULL
, "aliasing error in %s - %s",
334 cp
, akerror (state
));
353 if (!(filter
= *argp
++) || *filter
== '-')
354 adios (NULL
, "missing argument to %s", argp
[-2]);
409 if (!(cp
= *argp
++) || *cp
== '-')
410 adios (NULL
, "missing argument to %s", argp
[-2]);
411 if ((outputlinelen
= atoi (cp
)) < 10)
412 adios (NULL
, "impossible width %d", outputlinelen
);
416 if (!(cp
= *argp
++) || *cp
== '-')
417 adios (NULL
, "missing argument to %s", argp
[-2]);
418 if ((pfd
= atoi (cp
)) <= 2)
419 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
423 if (!(clientsw
= *argp
++) || *clientsw
== '-')
424 adios (NULL
, "missing argument to %s", argp
[-2]);
427 if (!(serversw
= *argp
++) || *serversw
== '-')
428 adios (NULL
, "missing argument to %s", argp
[-2]);
435 if (!(partno
= *argp
++) || *partno
== '-')
436 adios (NULL
, "missing argument to %s", argp
[-2]);
448 if (!(cp
= *argp
++) || *cp
== '-')
449 adios (NULL
, "missing argument to %s", argp
[-2]);
454 if (!(saslmech
= *argp
++) || *saslmech
== '-')
455 adios (NULL
, "missing argument to %s", argp
[-2]);
460 if (!(xoauth_client_res
= *argp
++) || *xoauth_client_res
== '-')
461 adios (NULL
, "missing argument to %s", argp
[-2]);
463 adios (NULL
, "not built with OAuth support");
468 if (!(user
= *argp
++) || *user
== '-')
469 adios (NULL
, "missing argument to %s", argp
[-2]);
473 if (!(port
= *argp
++) || *port
== '-')
474 adios (NULL
, "missing argument to %s", argp
[-2]);
490 if (!(cp
= *argp
++) || *cp
== '-')
491 adios (NULL
, "missing argument to %s", argp
[-2]);
496 if (!(cp
= *argp
++) || *cp
== '-')
497 adios (NULL
, "missing argument to %s", argp
[-2]);
502 if (!(cp
= *argp
++) || *cp
== '-')
503 adios (NULL
, "missing argument to %s", argp
[-2]);
508 if (!(cp
= *argp
++) || *cp
== '-')
509 adios (NULL
, "missing argument to %s", argp
[-2]);
510 save_mts_method (cp
);
513 case CREDENTIALSSW
: {
514 if (!(cp
= *argp
++) || *cp
== '-')
515 adios (NULL
, "missing argument to %s", argp
[-2]);
516 add_profile_entry ("credentials", cp
);
521 if (!(cp
= *argp
++) || *cp
== '-')
522 adios (NULL
, "missing argument to %s", argp
[-2]);
523 if (save_message_id_style (cp
) != 0)
524 adios (NULL
, "unsupported messageid \"%s\"", cp
);
529 adios (NULL
, "only one message at a time!");
537 adios (NULL
, "usage: %s [switches] file", invo_name
);
539 if (outputlinelen
< 10)
540 adios (NULL
, "impossible width %d", outputlinelen
);
542 if ((in
= fopen (msg
, "r")) == NULL
)
543 adios (msg
, "unable to open");
551 if ((out
= fopen ("/dev/null", "w")) == NULL
)
552 adios ("/dev/null", "unable to open");
554 char *cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
556 adios(NULL
, "unable to create temporary file in %s",
559 strncpy(tmpfil
, cp
, sizeof(tmpfil
));
563 hdrtab
= msgstate
== NORMAL
? NHeaders
: RHeaders
;
565 for (compnum
= 1;;) {
566 int bufsz
= sizeof buf
;
567 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
571 cp
= add (buf
, NULL
);
572 while (state
== FLDPLUS
) {
574 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
577 putfmt (name
, cp
, out
);
582 finish_headers (out
);
585 fprintf (out
, "\n%s", buf
);
586 while (state
== BODY
) {
588 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
594 finish_headers (out
);
599 adios (NULL
, "message format error in component #%d", compnum
);
602 adios (NULL
, "getfld() returned %d", state
);
606 m_getfld_state_destroy (&gstate
);
620 * Here's how we decide which address to use as the envelope-from
623 * - If we were given an Envelope-From header, use that.
624 * - If we were given a Sender: address, use that.
625 * - Otherwise, use the address on the From: line
628 if (msgflags
& MEFM
) {
630 } else if (seensender
) {
639 * The user didn't specify any of the tls switches. Try to
640 * help them by implying -initialtls if they're using port 465
641 * (smtps, until IANA revoked that registration in 1998).
643 tls
= ! strcmp (port
, "465") || ! strcasecmp (port
, "smtps")
646 #else /* ! TLS_SUPPORT */
648 #endif /* ! TLS_SUPPORT */
651 /* If we are doing a "whom" check */
653 /* This won't work with MTS_SENDMAIL_PIPE. */
654 verify_all_addresses (1, envelope
, xoauth_client_res
);
658 if (msgflags
& MINV
) {
659 make_bcc_file (dashstuff
);
660 if (msgflags
& MVIS
) {
661 if (sm_mts
!= MTS_SENDMAIL_PIPE
) {
662 /* It would be nice to have support to call
663 verify_all_addresses with MTS_SENDMAIL_PIPE, but
664 that might require running sendmail as root. Note
665 that spost didn't verify addresses. */
666 verify_all_addresses (verbose
, envelope
, xoauth_client_res
);
668 post (tmpfil
, 0, verbose
, envelope
, xoauth_client_res
);
670 post (bccfil
, 1, verbose
, envelope
, xoauth_client_res
);
671 (void) m_unlink (bccfil
);
673 post (tmpfil
, 0, isatty (1), envelope
, xoauth_client_res
);
677 (void) m_unlink (tmpfil
);
681 printf ("Partial Message #%s Processed\n", partno
);
683 printf ("Message Processed\n");
696 putfmt (char *name
, char *str
, FILE *out
)
698 int count
, grp
, i
, keep
;
700 char namep
[BUFSIZ
], error
[BUFSIZ
];
701 struct mailname
*mp
= NULL
, *np
= NULL
;
704 while (*str
== ' ' || *str
== '\t')
707 if (msgstate
== NORMAL
&& uprf (name
, "resent")) {
708 advise (NULL
, "illegal header line -- %s:", name
);
713 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
714 if (strncasecmp (name
, "nmh-", 4)) {
715 fprintf (out
, "%s: %s", name
, str
);
717 /* Filter out all Nmh-* headers, because Norm asked. They
718 should never have reached this point. Warn about any
719 that are non-empty. */
720 if (strcmp (str
, "\n")) {
721 char *newline
= strchr (str
, '\n');
722 if (newline
) *newline
= '\0';
724 advise (NULL
, "ignoring header line -- %s: %s", name
, str
);
733 if (hdr
->flags
& HIGN
) {
736 if (hdr
->flags
& HBAD
) {
737 advise (NULL
, "illegal header line -- %s:", name
);
741 msgflags
|= (hdr
->set
& ~(MVIS
| MINV
));
743 if (hdr
->flags
& HSUB
)
744 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
745 if (hdr
->flags
& HFCC
) {
746 if ((cp
= strrchr(str
, '\n')))
748 for (pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
750 insert_fcc (hdr
, pp
);
752 insert_fcc (hdr
, pp
);
756 if (!(hdr
->flags
& HADR
)) {
757 fprintf (out
, "%s: %s", name
, str
);
761 tmpaddrs
.m_next
= NULL
;
762 for (count
= 0; (cp
= getname (str
)); count
++)
763 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
)))) {
767 tmpaddrs
.m_next
= mp
;
771 admonish(cp
, "%s", error
);
772 if (hdr
->flags
& HTRY
)
779 if (hdr
->flags
& HNIL
)
780 fprintf (out
, "%s: %s", name
, str
);
783 * Sender (or Resent-Sender) can have only one address
785 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
786 : (hdr
->set
& MSND
)) {
787 advise (NULL
, "%s: field requires one address", name
);
791 advise (NULL
, "%s: field requires at least one address", name
);
798 if (count
> 1 && (hdr
->flags
& HONE
)) {
799 advise (NULL
, "%s: field only permits one address", name
);
804 nameoutput
= linepos
= 0;
805 snprintf (namep
, sizeof(namep
), "%s%s",
806 (hdr
->flags
& HMNG
) ? "Original-" : "", name
);
808 for (grp
= 0, mp
= tmpaddrs
.m_next
; mp
; mp
= np
)
809 if (mp
->m_nohost
) { /* also used to test (hdr->flags & HTRY) */
810 /* The address doesn't include a host, so it might be an alias. */
811 pp
= akvalue (mp
->m_mbox
); /* do mh alias substitution */
812 qp
= akvisible () ? mp
->m_mbox
: "";
815 putgrp (namep
, np
->m_gname
, out
, hdr
->flags
);
816 while ((cp
= getname (pp
))) {
817 if (!(mp
= getm (cp
, NULL
, 0, error
, sizeof(error
)))) {
818 admonish(cp
, "%s", error
);
824 * If it's a From: or Resent-From: header, save the address
825 * for later possible use (as the envelope address for SMTP)
828 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
829 : (hdr
->set
& MFRM
)) {
830 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
831 from
[sizeof(from
) - 1] = '\0';
836 * Also save the Sender: or Resent-Sender: header as well
839 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
840 : (hdr
->set
& MSND
)) {
841 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
842 sender
[sizeof(sender
) - 1] = '\0';
847 * ALSO ... save Envelope-From
850 if (hdr
->set
& MEFM
) {
851 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
852 efrom
[sizeof(efrom
) - 1] = '\0';
855 if (hdr
->flags
& HBCC
)
858 mp
->m_ingrp
= np
->m_ingrp
;
861 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
863 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
864 /* Catch this before sendmail chokes with:
865 "553 List:; syntax illegal for recipient
867 If we wanted to, we could expand out blind
868 aliases and put them in Bcc:, but then
869 they'd have the Blind-Carbon-Copy
872 "blind lists not compatible with"
878 if (putadr (namep
, qp
, mp
, out
, hdr
->flags
))
879 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
888 /* Address includes a host, so no alias substitution is needed. */
891 * If it's a From: or Resent-From header, save the address
892 * for later possible use (as the envelope address for SMTP)
895 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
896 : (hdr
->set
& MFRM
)) {
897 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
902 * Also save the Sender: header as well
905 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
906 : (hdr
->set
& MSND
)) {
907 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
908 sender
[sizeof(sender
) - 1] = '\0';
913 * ALSO ... save Envelope-From
916 if (hdr
->set
& MEFM
) {
917 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
918 efrom
[sizeof(efrom
) - 1] = '\0';
921 if (hdr
->flags
& HBCC
)
924 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
927 keep
= putadr (namep
, "", mp
, out
, hdr
->flags
);
931 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
938 * If this is a From:/Resent-From: header, save the full thing for
939 * later in case we need it for use when constructing a Bcc draft message
942 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
) : (hdr
->set
& MFRM
)) {
943 strncpy(fullfrom
, str
, sizeof(fullfrom
));
944 fullfrom
[sizeof(fullfrom
) - 1] = 0;
946 * Strip off any trailing newlines
949 while (strlen(fullfrom
) > 0 && fullfrom
[strlen(fullfrom
) - 1] == '\n') {
950 fullfrom
[strlen(fullfrom
) - 1] = '\0';
954 if (grp
> 0 && (hdr
->flags
& HNGR
)) {
955 advise (NULL
, "%s: field does not allow groups", name
);
970 * Probably not necessary, but just in case ...
981 * Now that we've outputted the header fields in the draft
982 * message, we will now output any remaining header fields
983 * that we need to add/create.
987 finish_headers (FILE *out
)
991 if (!(msgflags
& MFRM
)) {
993 * A From: header is now required in the draft.
995 advise (NULL
, "message has no From: header");
996 advise (NULL
, "See default components files for examples");
1001 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
1002 advise (NULL
, "A Sender: or Envelope-From: header is required "
1003 "with multiple\nFrom: addresses");
1011 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1013 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
1015 * If we have multiple From: addresses, make sure we have an
1016 * Sender: header. If we don't have one, then generate one
1017 * from Envelope-From: (which in this case, cannot be blank)
1020 if (fromcount
> 1 && seensender
== 0) {
1021 if (efrom
[0] == '\0') {
1022 advise (NULL
, "Envelope-From cannot be blank when there "
1023 "is multiple From: addresses\nand no Sender: "
1027 fprintf (out
, "Sender: %s\n", efrom
);
1031 if (!(msgflags
& MVIS
))
1032 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
1036 if (!(msgflags
& MDAT
)) {
1037 advise (NULL
, "message has no Date: header");
1040 if (!(msgflags
& MFRM
)) {
1041 advise (NULL
, "message has no From: header");
1044 if (!(msgflags
& MRFM
)) {
1045 advise (NULL
, "message has no Resent-From: header");
1046 advise (NULL
, "See default components files for examples");
1050 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
1051 advise (NULL
, "A Resent-Sender: or Envelope-From: header is "
1052 "required with multiple\nResent-From: addresses");
1060 fprintf (out
, "Resent-Date: %s\n", dtime (&tclock
, 0));
1062 fprintf (out
, "Resent-Message-ID: %s\n",
1063 message_id (tclock
, 0));
1065 * If we have multiple Resent-From: addresses, make sure we have an
1066 * Resent-Sender: header. If we don't have one, then generate one
1067 * from Envelope-From (which in this case, cannot be blank)
1070 if (fromcount
> 1 && seensender
== 0) {
1071 if (efrom
[0] == '\0') {
1072 advise (NULL
, "Envelope-From cannot be blank when there "
1073 "is multiple Resent-From: addresses and no "
1074 "Resent-Sender: header");
1077 fprintf (out
, "Resent-Sender: %s\n", efrom
);
1081 if (!(msgflags
& MVIS
))
1082 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
1087 adios (NULL
, "re-format message and try again");
1089 adios (NULL
, "no addressees");
1094 get_header (char *header
, struct headers
*table
)
1098 for (h
= table
; h
->value
; h
++)
1099 if (!strcasecmp (header
? header
: "", h
->value
? h
->value
: ""))
1107 putadr (char *name
, char *aka
, struct mailname
*mp
, FILE *out
, unsigned int flags
)
1111 char buffer
[BUFSIZ
];
1113 if (mp
->m_mbox
== NULL
|| ((flags
& HTRY
) && !insert (mp
)))
1115 if (sm_mts
!= MTS_SENDMAIL_PIPE
&&
1116 ((flags
& (HBCC
| HDCC
| HEFM
)) || mp
->m_ingrp
))
1120 fprintf (out
, "%s: ", name
);
1121 linepos
+= (nameoutput
= strlen (name
) + 2);
1124 if (*aka
&& mp
->m_type
!= UUCPHOST
&& !mp
->m_pers
)
1125 mp
->m_pers
= getcpy (aka
);
1128 snprintf (buffer
, sizeof(buffer
), "%s;", mp
->m_gname
);
1131 cp
= adrformat (mp
);
1138 if (linepos
!= nameoutput
) {
1139 if (len
+ linepos
+ 2 > outputlinelen
)
1140 fprintf (out
, ",\n%*s", linepos
= nameoutput
, "");
1150 return (flags
& HTRY
);
1155 putgrp (char *name
, char *group
, FILE *out
, unsigned int flags
)
1160 if (sm_mts
!= MTS_SENDMAIL_PIPE
&& (flags
& HBCC
))
1164 fprintf (out
, "%s: ", name
);
1165 linepos
+= (nameoutput
= strlen (name
) + 2);
1168 cp
= concat (group
, ";", NULL
);
1171 if (linepos
> nameoutput
) {
1172 if (len
+ linepos
+ 2 > outputlinelen
) {
1173 fprintf (out
, ",\n%*s", nameoutput
, "");
1174 linepos
= nameoutput
;
1188 insert (struct mailname
*np
)
1190 struct mailname
*mp
;
1192 if (np
->m_mbox
== NULL
)
1195 for (mp
= np
->m_type
== LOCALHOST
? &localaddrs
1196 : np
->m_type
== UUCPHOST
? &uuaddrs
1200 if (!strcasecmp (np
->m_host
? np
->m_host
: "",
1201 mp
->m_next
->m_host
? mp
->m_next
->m_host
: "") &&
1202 !strcasecmp (np
->m_mbox
? np
->m_mbox
: "",
1203 mp
->m_next
->m_mbox
? mp
->m_next
->m_mbox
: "") &&
1204 np
->m_bcc
== mp
->m_next
->m_bcc
)
1217 struct mailname
*mp
;
1219 printf ("-------\n\t-- Addresses --\nlocal:\t");
1220 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1221 printf ("%s%s%s", mp
->m_mbox
,
1222 mp
->m_bcc
? "[BCC]" : "",
1223 mp
->m_next
? ",\n\t" : "");
1225 printf ("\nnet:\t");
1226 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1227 printf ("%s%s@%s%s%s", mp
->m_path
? mp
->m_path
: "",
1228 mp
->m_mbox
, mp
->m_host
,
1229 mp
->m_bcc
? "[BCC]" : "",
1230 mp
->m_next
? ",\n\t" : "");
1232 printf ("\nuucp:\t");
1233 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1234 printf ("%s!%s%s%s", mp
->m_host
, mp
->m_mbox
,
1235 mp
->m_bcc
? "[BCC]" : "",
1236 mp
->m_next
? ",\n\t" : "");
1238 printf ("\n\t-- Folder Copies --\nfcc:\t");
1239 for (i
= 0; i
< fccind
; i
++)
1240 printf ("%s%s", fccfold
[i
], i
+ 1 < fccind
? ",\n\t" : "");
1248 struct mailname
*mp
;
1250 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1251 if (annoaux (mp
) == NOTOK
)
1254 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1255 if (annoaux (mp
) == NOTOK
)
1258 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1259 if (annoaux (mp
) == NOTOK
)
1269 annoaux (struct mailname
*mp
)
1272 char buffer
[BUFSIZ
];
1274 snprintf (buffer
, sizeof(buffer
), "%s\n", adrformat (mp
));
1275 i
= strlen (buffer
);
1277 return (write (pfd
, buffer
, i
) == i
? OK
: NOTOK
);
1282 insert_fcc (struct headers
*hdr
, char *pp
)
1286 for (cp
= pp
; isspace ((unsigned char) *cp
); cp
++)
1288 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace ((unsigned char) *pp
); pp
--)
1296 adios (NULL
, "too many %ss", hdr
->value
);
1297 fccfold
[fccind
++] = getcpy (cp
);
1305 make_bcc_file (int dashstuff
)
1311 char *tfile
= NULL
, *program
;
1313 if ((tfile
= m_mktemp2(NULL
, "bccs", NULL
, &out
)) == NULL
) {
1314 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
1316 strncpy (bccfil
, tfile
, sizeof(bccfil
));
1318 fprintf (out
, "From: %s\n", fullfrom
);
1319 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1321 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
1323 fprintf (out
, "Subject: %s", subject
);
1324 fprintf (out
, "BCC:\n");
1327 * Use MIME encapsulation for Bcc messages
1333 * Check if any lines in the message clash with the
1334 * prefix for the MIME multipart separator. If there
1335 * is a clash, increment one of the letters in the
1336 * prefix and check again.
1338 if ((cp
= strchr(prefix
, 'a')) == NULL
)
1339 adios (NULL
, "lost prefix start");
1340 while (find_prefix () == NOTOK
) {
1345 adios (NULL
, "can't find a unique delimiter string");
1350 fprintf (out
, "%s: %s\n%s: multipart/digest; boundary=\"",
1351 VRSN_FIELD
, VRSN_VALUE
, TYPE_FIELD
);
1352 fprintf (out
, "%s\"\n\n--%s\n\n", prefix
, prefix
);
1354 fprintf (out
, "\n------- Blind-Carbon-Copy\n\n");
1360 * Do mhl filtering of Bcc messages instead
1361 * of MIME encapsulation.
1363 if (filter
!= NULL
) {
1364 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1368 adios ("fork", "unable to");
1371 dup2 (fileno (out
), 1);
1373 vec
= argsplit(mhlproc
, &program
, &i
);
1374 vec
[i
++] = "-forward";
1379 /* was the flag -[no]dashstuffing specified? */
1381 vec
[i
++] = "-dashstuffing";
1382 else if (dashstuff
< 0)
1383 vec
[i
++] = "-nodashstuffing";
1386 execvp (program
, vec
);
1387 fprintf (stderr
, "unable to exec ");
1392 pidXwait (child_id
, mhlproc
);
1396 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
1397 adios (tmpfil
, "unable to re-open");
1400 * If using MIME encapsulation, or if the -nodashstuffing
1401 * flag was given, then just copy message. Else do
1402 * RFC934 quoting (dashstuffing).
1404 if (mime
|| dashstuff
< 0)
1405 cpydata (fd
, fileno (out
), tmpfil
, bccfil
);
1407 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
1411 fseek (out
, 0L, SEEK_END
);
1413 fprintf (out
, "\n--%s--\n", prefix
);
1415 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
1421 * Scan message to check if any lines clash with
1422 * the prefix of the MIME multipart separator.
1429 char buffer
[BUFSIZ
];
1432 if ((in
= fopen (tmpfil
, "r")) == NULL
)
1433 adios (tmpfil
, "unable to re-open");
1435 while (fgets (buffer
, sizeof(buffer
) - 1, in
))
1436 if (buffer
[0] == '-' && buffer
[1] == '-') {
1439 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1440 if (!isspace ((unsigned char) *cp
))
1443 if (strcmp (buffer
+ 2, prefix
) == 0) {
1454 #define plural(x) (x == 1 ? "" : "s")
1459 if (badadr
&& unkadr
)
1460 die (NULL
, "%d address%s unparsable, %d addressee%s undeliverable",
1461 badadr
, plural (badadr
), unkadr
, plural (badadr
));
1463 die (NULL
, "%d address%s unparsable", badadr
, plural (badadr
));
1465 die (NULL
, "%d addressee%s undeliverable", unkadr
, plural (unkadr
));
1470 do_addresses (int bccque
, int talk
)
1474 struct mailname
*lp
;
1477 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1478 if (lp
->m_bcc
? bccque
: !bccque
) {
1480 printf (" -- Local Recipients --\n");
1481 do_an_address (lp
, talk
);
1486 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1487 if (lp
->m_bcc
? bccque
: !bccque
) {
1489 printf (" -- UUCP Recipients --\n");
1490 do_an_address (lp
, talk
);
1495 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1496 if (lp
->m_bcc
? bccque
: !bccque
) {
1498 printf (" -- Network Recipients --\n");
1499 do_an_address (lp
, talk
);
1505 if (rp_isbad (retval
= sm_waend ()))
1506 die (NULL
, "problem ending addresses; %s", rp_string (retval
));
1511 * MTS-SPECIFIC INTERACTION
1516 * SENDMAIL/SMTP routines
1520 post (char *file
, int bccque
, int talk
, char *envelope
,
1521 const char *xoauth_client_res
)
1528 if (msgflags
& MINV
)
1529 printf (" -- Posting for %s Recipients --\n",
1530 bccque
? "Blind" : "Sighted");
1532 printf (" -- Posting for All Recipients --\n");
1537 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
1538 char **argp
, *program
;
1541 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1545 adios ("fork", "unable to");
1548 if (freopen( file
, "r", stdin
) == NULL
) {
1549 adios (file
, "can't reopen for sendmail");
1552 argp
= argsplit(sendmail
, &program
, &argc
);
1553 argp
[argc
++] = "-t"; /* read msg for recipients */
1554 argp
[argc
++] = "-i"; /* don't stop on "." */
1556 argp
[argc
++] = "-bv";
1558 argp
[argc
++] = "-v";
1561 execv (program
, argp
);
1562 adios (sendmail
, "can't exec");
1565 pidXwait (child_id
, NULL
);
1569 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1570 verbose
, snoop
, sasl
, saslssf
,
1571 saslmech
, user
, xoauth_client_res
, tls
))
1572 || rp_isbad (retval
= sm_winit (envelope
)))
1573 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1575 do_addresses (bccque
, talk
&& verbose
);
1576 if ((fd
= open (file
, O_RDONLY
)) == NOTOK
)
1577 die (file
, "unable to re-open");
1582 sm_end (!(msgflags
& MINV
) || bccque
? OK
: DONE
);
1586 if (msgflags
& MINV
)
1587 printf (" -- %s Recipient Copies Posted --\n",
1588 bccque
? "Blind" : "Sighted");
1590 printf (" -- Recipient Copies Posted --\n");
1598 /* Address Verification */
1601 verify_all_addresses (int talk
, char *envelope
, const char *xoauth_client_res
)
1604 struct mailname
*lp
;
1608 if (!whomsw
|| checksw
)
1609 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1610 verbose
, snoop
, sasl
, saslssf
,
1611 saslmech
, user
, xoauth_client_res
, tls
))
1612 || rp_isbad (retval
= sm_winit (envelope
)))
1613 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1615 if (talk
&& !whomsw
)
1616 printf (" -- Address Verification --\n");
1617 if (talk
&& localaddrs
.m_next
)
1618 printf (" -- Local Recipients --\n");
1619 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1620 do_an_address (lp
, talk
);
1622 if (talk
&& uuaddrs
.m_next
)
1623 printf (" -- UUCP Recipients --\n");
1624 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1625 do_an_address (lp
, talk
);
1627 if (talk
&& netaddrs
.m_next
)
1628 printf (" -- Network Recipients --\n");
1629 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1630 do_an_address (lp
, talk
);
1633 if (talk
&& !whomsw
)
1634 printf (" -- Address Verification Successful --\n");
1636 if (!whomsw
|| checksw
)
1645 do_an_address (struct mailname
*lp
, int talk
)
1651 switch (lp
->m_type
) {
1655 strncpy (addr
, mbox
, sizeof(addr
));
1659 mbox
= auxformat (lp
, 0);
1661 snprintf (addr
, sizeof(addr
), "%s!%s", lp
->m_host
, lp
->m_mbox
);
1664 default: /* let SendMail decide if the host is bad */
1667 snprintf (addr
, sizeof(addr
), "%s at %s", mbox
, host
);
1672 printf (" %s%s", addr
, whomsw
&& lp
->m_bcc
? "[BCC]" : "");
1674 if (whomsw
&& !checksw
) {
1682 switch (retval
= sm_wadr (mbox
, host
,
1683 lp
->m_type
!= UUCPHOST
? lp
->m_path
: NULL
)) {
1686 printf ("address ok\n");
1692 fprintf (stderr
, " %s: ", addr
);
1693 fprintf (talk
? stdout
: stderr
, "loses; %s\n",
1694 rp_string (retval
));
1700 fprintf (stderr
, " %s: ", addr
);
1701 die (NULL
, "unexpected response; %s", rp_string (retval
));
1709 do_text (char *file
, int fd
)
1714 lseek (fd
, (off_t
) 0, SEEK_SET
);
1716 while ((state
= read (fd
, buf
, sizeof(buf
))) > 0) {
1717 if (rp_isbad (retval
= sm_wtxt (buf
, state
)))
1718 die (NULL
, "problem writing text; %s\n", rp_string (retval
));
1722 die (file
, "problem reading from");
1724 switch (retval
= sm_wtend ()) {
1730 die (NULL
, "posting failed; %s", rp_string (retval
));
1733 die (NULL
, "unexpected response; %s", rp_string (retval
));
1747 (void) m_unlink (tmpfil
);
1748 if (msgflags
& MINV
)
1749 (void) m_unlink (bccfil
);
1751 if (!whomsw
|| checksw
)
1764 hstat
= SIGNAL2 (SIGHUP
, sigser
);
1765 istat
= SIGNAL2 (SIGINT
, sigser
);
1766 qstat
= SIGNAL2 (SIGQUIT
, sigser
);
1767 tstat
= SIGNAL2 (SIGTERM
, sigser
);
1777 SIGNAL (SIGHUP
, hstat
);
1778 SIGNAL (SIGINT
, istat
);
1779 SIGNAL (SIGQUIT
, qstat
);
1780 SIGNAL (SIGTERM
, tstat
);
1788 p_refile (char *file
)
1796 printf (" -- Filing Folder Copies --\n");
1797 for (i
= 0; i
< fccind
; i
++)
1798 fcc (file
, fccfold
[i
]);
1800 printf (" -- Folder Copies Filed --\n");
1805 * Call the `fileproc' to add the file to the folder.
1809 fcc (char *file
, char *folder
)
1812 int i
, status
, argp
;
1814 char **arglist
, *program
;
1817 printf (" %sFcc %s: ", msgstate
== RESENT
? "Resent-" : "", folder
);
1820 for (i
= 0; (child_id
= fork ()) == NOTOK
&& i
< 5; i
++)
1826 fprintf (stderr
, " %sFcc %s: ",
1827 msgstate
== RESENT
? "Resent-" : "", folder
);
1828 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
1832 /* see if we need to add `+' */
1833 snprintf (fold
, sizeof(fold
), "%s%s",
1834 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
1836 /* now exec the fileproc */
1838 arglist
= argsplit(fileproc
, &program
, &argp
);
1839 arglist
[argp
++] = "-link";
1840 arglist
[argp
++] = "-file";
1841 arglist
[argp
++] = file
;
1842 arglist
[argp
++] = fold
;
1843 arglist
[argp
] = NULL
;
1844 execvp (program
, arglist
);
1848 if ((status
= pidwait (child_id
, OK
))) {
1850 fprintf (stderr
, " %sFcc %s: ",
1851 msgstate
== RESENT
? "Resent-" : "", folder
);
1852 pidstatus (status
, verbose
? stdout
: stderr
, NULL
);
1855 printf ("folder ok\n");
1867 die (char *what
, char *fmt
, ...)
1871 (void) m_unlink (tmpfil
);
1872 if (msgflags
& MINV
)
1873 (void) m_unlink (bccfil
);
1875 if (!whomsw
|| checksw
)
1879 advertise (what
, NULL
, fmt
, ap
);