]>
diplodocus.org Git - nmh/blob - uip/post.c
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 */
40 #define FCCS 10 /* max number of fccs allowed */
42 /* In the following array of structures, the numeric second field of the
43 structures (minchars) is apparently used like this:
45 -# : Switch can be abbreviated to # characters; switch hidden in -help.
46 0 : Switch can't be abbreviated; switch shown in -help.
47 # : Switch can be abbreviated to # characters; switch shown in -help. */
49 #define POST_SWITCHES \
50 X("alias aliasfile", 0, ALIASW) \
51 X("check", -5, CHKSW) /* interface from whom */ \
52 X("nocheck", -7, NCHKSW) /* interface from whom */ \
53 X("debug", -5, DEBUGSW) \
54 X("dist", -4, DISTSW) /* interface from dist */ \
55 X("filter filterfile", 0, FILTSW) \
56 X("nofilter", 0, NFILTSW) \
57 X("format", 0, FRMTSW) \
58 X("noformat", 0, NFRMTSW) \
59 X("library directory", -7, LIBSW) /* interface from send, whom */ \
60 X("mime", 0, MIMESW) \
61 X("nomime", 0, NMIMESW) \
62 X("msgid", 0, MSGDSW) \
63 X("nomsgid", 0, NMSGDSW) \
64 X("verbose", 0, VERBSW) \
65 X("noverbose", 0, NVERBSW) \
66 X("watch", 0, WATCSW) \
67 X("nowatch", 0, NWATCSW) \
68 X("whom", -4, WHOMSW) /* interface from whom */ \
69 X("width columns", 0, WIDTHSW) \
70 X("version", 0, VERSIONSW) \
71 X("help", 0, HELPSW) \
72 X("dashstuffing", -12, BITSTUFFSW) /* should we dashstuff BCC messages? */ \
73 X("nodashstuffing", -14, NBITSTUFFSW) \
74 X("idanno number", -6, ANNOSW) /* interface from send */ \
75 X("client host", -6, CLIESW) \
76 X("server host", 6, SERVSW) /* specify alternate SMTP server */ \
77 X("snoop", -5, SNOOPSW) /* snoop the SMTP transaction */ \
78 X("partno", -6, PARTSW) \
79 X("sasl", SASLminc(4), SASLSW) \
80 X("nosasl", SASLminc(6), NOSASLSW) \
81 X("saslmaxssf", SASLminc(10), SASLMXSSFSW) \
82 X("saslmech", SASLminc(5), SASLMECHSW) \
83 X("authservice", SASLminc(0), AUTHSERVICESW) \
84 X("user", SASLminc(-4), USERSW) \
85 X("port server submission port name/number", 4, PORTSW) \
86 X("tls", TLSminc(-3), TLSSW) \
87 X("initialtls", TLSminc(-10), INITTLSSW) \
88 X("notls", TLSminc(-5), NTLSSW) \
89 X("fileproc", -4, FILEPROCSW) \
90 X("mhlproc", -3, MHLPROCSW) \
91 X("sendmail program", 0, MTSSM) \
92 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
93 X("credentials legacy|file:filename", 0, CREDENTIALSSW) \
94 X("messageid localname|random", 2, MESSAGEIDSW) \
96 #define X(sw, minchars, id) id,
97 DEFINE_SWITCH_ENUM(POST
);
100 #define X(sw, minchars, id) { sw, minchars, id },
101 DEFINE_SWITCH_ARRAY(POST
, switches
);
112 * flags for headers->flags
114 #define HNOP 0x0000 /* just used to keep .set around */
115 #define HBAD 0x0001 /* bad header - don't let it through */
116 #define HADR 0x0002 /* header has an address field */
117 #define HSUB 0x0004 /* Subject: header */
118 #define HTRY 0x0008 /* try to send to addrs on header */
119 #define HBCC 0x0010 /* don't output this header, unless MTS_SENDMAIL_PIPE */
120 #define HMNG 0x0020 /* munge this header */
121 #define HNGR 0x0040 /* no groups allowed in this header */
122 #define HFCC 0x0080 /* FCC: type header */
123 #define HNIL 0x0100 /* okay for this header not to have addrs */
124 #define HIGN 0x0200 /* ignore this header */
125 #define HDCC 0x0400 /* another undocumented feature */
126 #define HONE 0x0800 /* Only (zero or) one address allowed */
127 #define HEFM 0x1000 /* Envelope-From: header */
130 * flags for headers->set
132 #define MFRM 0x0001 /* we've seen a From: */
133 #define MDAT 0x0002 /* we've seen a Date: */
134 #define MRFM 0x0004 /* we've seen a Resent-From: */
135 #define MVIS 0x0008 /* we've seen sighted addrs */
136 #define MINV 0x0010 /* we've seen blind addrs */
137 #define MSND 0x0020 /* we've seen a Sender: */
138 #define MRSN 0x0040 /* We've seen a Resent-Sendr:*/
139 #define MEFM 0x0080 /* We've seen Envelope-From: */
142 static struct headers NHeaders
[] = {
143 { "Return-Path", HBAD
, 0 },
144 { "Received", HBAD
, 0 },
145 { "Reply-To", HADR
|HNGR
, 0 },
146 { "From", HADR
|HNGR
, MFRM
},
147 { "Sender", HADR
|HNGR
|HONE
, MSND
},
149 { "Subject", HSUB
, 0 },
150 { "To", HADR
|HTRY
, MVIS
},
151 { "cc", HADR
|HTRY
, MVIS
},
152 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, MINV
},
153 { "Dcc", HADR
|HTRY
|HDCC
|HNIL
, MVIS
}, /* sorta cc & bcc combined */
154 { "Message-ID", HBAD
, 0 },
156 { "Envelope-From", HADR
|HONE
|HEFM
, MEFM
},
160 static struct headers RHeaders
[] = {
161 { "Resent-Reply-To", HADR
|HNGR
, 0 },
162 { "Resent-From", HADR
|HNGR
, MRFM
},
163 { "Resent-Sender", HADR
|HNGR
, MRSN
},
164 { "Resent-Date", HBAD
, 0 },
165 { "Resent-Subject", HSUB
, 0 },
166 { "Resent-To", HADR
|HTRY
, MVIS
},
167 { "Resent-cc", HADR
|HTRY
, MVIS
},
168 { "Resent-Bcc", HADR
|HTRY
|HBCC
, MINV
},
169 { "Resent-Message-ID", HBAD
, 0 },
170 { "Resent-Fcc", HFCC
, 0 },
171 { "Reply-To", HADR
, 0 },
172 { "From", HADR
|HNGR
, MFRM
},
173 { "Sender", HADR
|HNGR
, MSND
},
174 { "Date", HNOP
, MDAT
},
175 { "To", HADR
|HNIL
, 0 },
176 { "cc", HADR
|HNIL
, 0 },
177 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, 0 },
179 { "Envelope-From", HADR
|HONE
|HEFM
, MEFM
},
183 static short fccind
= 0; /* index into fccfold[] */
184 static short outputlinelen
= OUTPUTLINELEN
;
186 static int pfd
= NOTOK
; /* fd to write annotation list to */
187 static int recipients
= 0; /* how many people will get a copy */
188 static int unkadr
= 0; /* how many of those were unknown */
189 static int badadr
= 0; /* number of bad addrs */
190 static int badmsg
= 0; /* message has bad semantics */
191 static int verbose
= 0; /* spell it out */
192 static int format
= 1; /* format addresses */
193 static int mime
= 0; /* use MIME-style encapsulations for Bcc */
194 static int msgid
= 0; /* add msgid */
195 static int debug
= 0; /* debugging post */
196 static int watch
= 0; /* watch the delivery process */
197 static int whomsw
= 0; /* we are whom not post */
198 static int checksw
= 0; /* whom -check */
199 static int linepos
=0; /* putadr()'s position on the line */
200 static int nameoutput
=0; /* putadr() has output header name */
201 static int sasl
=0; /* Use SASL auth for SMTP */
202 static int saslssf
=-1; /* Our maximum SSF for SASL */
203 static char *saslmech
=NULL
; /* Force use of particular SASL mech */
204 static char *user
=NULL
; /* Authenticate as this user */
205 static char *port
="submission"; /* Name of server port for SMTP submission */
206 static int tls
=-1; /* Use TLS for encryption */
207 static int fromcount
=0; /* Count of addresses on From: header */
208 static int seensender
=0; /* Have we seen a Sender: header? */
210 static unsigned msgflags
= 0; /* what we've seen */
214 static int msgstate
= NORMAL
;
216 static time_t tclock
= 0; /* the time we started (more or less) */
218 static SIGNAL_HANDLER hstat
, istat
, qstat
, tstat
;
220 static char tmpfil
[BUFSIZ
];
221 static char bccfil
[BUFSIZ
];
223 static char from
[BUFSIZ
]; /* my network address */
224 static char sender
[BUFSIZ
]; /* my Sender: header */
225 static char efrom
[BUFSIZ
]; /* my Envelope-From: header */
226 static char fullfrom
[BUFSIZ
]; /* full contents of From header */
227 static char *filter
= NULL
; /* the filter for BCC'ing */
228 static char *subject
= NULL
; /* the subject field for BCC'ing */
229 static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
231 static struct headers
*hdrtab
; /* table for the message we're doing */
233 static struct mailname localaddrs
; /* local addrs */
234 static struct mailname netaddrs
; /* network addrs */
235 static struct mailname uuaddrs
; /* uucp addrs */
236 static struct mailname tmpaddrs
; /* temporary queue */
238 static int snoop
= 0;
239 static char *clientsw
= NULL
;
240 static char *serversw
= NULL
;
242 static char prefix
[] = "----- =_aaaaaaaaaa";
244 static char *partno
= NULL
;
249 static void putfmt (char *, char *, FILE *);
250 static void start_headers (void);
251 static void finish_headers (FILE *);
252 static int get_header (char *, struct headers
*);
253 static int putadr (char *, char *, struct mailname
*, FILE *, unsigned int);
254 static void putgrp (char *, char *, FILE *, unsigned int);
255 static int insert (struct mailname
*);
256 static void pl (void);
257 static void anno (void);
258 static int annoaux (struct mailname
*);
259 static void insert_fcc (struct headers
*, char *);
260 static void make_bcc_file (int);
261 static void verify_all_addresses (int, char *, const char *);
262 static void chkadr (void);
263 static void sigon (void);
264 static void sigoff (void);
265 static void p_refile (char *);
266 static void fcc (char *, char *);
267 static void die (char *, char *, ...);
268 static void post (char *, int, int, char *, const char *);
269 static void do_text (char *file
, int fd
);
270 static void do_an_address (struct mailname
*, int);
271 static void do_addresses (int, int);
272 static int find_prefix (void);
276 main (int argc
, char **argv
)
278 int state
, compnum
, dashstuff
= 0;
279 char *cp
, *msg
= NULL
, **argp
, **arguments
, *envelope
;
280 char buf
[BUFSIZ
], name
[NAMESZ
];
282 m_getfld_state_t gstate
= 0;
283 char *xoauth_client_res
= NULL
;
285 if (nmh_init(argv
[0], 0 /* use context_foil() */)) { return 1; }
287 mts_init (invo_name
);
288 arguments
= getarguments (invo_name
, argc
, argv
, 0);
291 while ((cp
= *argp
++)) {
293 switch (smatch (++cp
, switches
)) {
295 ambigsw (cp
, switches
);
298 adios (NULL
, "-%s unknown", cp
);
301 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
302 print_help (buf
, switches
, 0);
305 print_version(invo_name
);
309 if (!(cp
= *argp
++) || *cp
== '-')
310 adios (NULL
, "missing argument to %s", argp
[-2]);
311 /* create a minimal context */
312 if (context_foil (cp
) == -1)
317 if (!(cp
= *argp
++) || *cp
== '-')
318 adios (NULL
, "missing argument to %s", argp
[-2]);
319 if ((state
= alias (cp
)) != AK_OK
)
320 adios (NULL
, "aliasing error in %s - %s",
321 cp
, akerror (state
));
340 if (!(filter
= *argp
++) || *filter
== '-')
341 adios (NULL
, "missing argument to %s", argp
[-2]);
396 if (!(cp
= *argp
++) || *cp
== '-')
397 adios (NULL
, "missing argument to %s", argp
[-2]);
398 if ((outputlinelen
= atoi (cp
)) < 10)
399 adios (NULL
, "impossible width %d", outputlinelen
);
403 if (!(cp
= *argp
++) || *cp
== '-')
404 adios (NULL
, "missing argument to %s", argp
[-2]);
405 if ((pfd
= atoi (cp
)) <= 2)
406 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
410 if (!(clientsw
= *argp
++) || *clientsw
== '-')
411 adios (NULL
, "missing argument to %s", argp
[-2]);
414 if (!(serversw
= *argp
++) || *serversw
== '-')
415 adios (NULL
, "missing argument to %s", argp
[-2]);
422 if (!(partno
= *argp
++) || *partno
== '-')
423 adios (NULL
, "missing argument to %s", argp
[-2]);
435 if (!(cp
= *argp
++) || *cp
== '-')
436 adios (NULL
, "missing argument to %s", argp
[-2]);
441 if (!(saslmech
= *argp
++) || *saslmech
== '-')
442 adios (NULL
, "missing argument to %s", argp
[-2]);
447 if (!(xoauth_client_res
= *argp
++) || *xoauth_client_res
== '-')
448 adios (NULL
, "missing argument to %s", argp
[-2]);
450 adios (NULL
, "not built with OAuth support");
455 if (!(user
= *argp
++) || *user
== '-')
456 adios (NULL
, "missing argument to %s", argp
[-2]);
460 if (!(port
= *argp
++) || *port
== '-')
461 adios (NULL
, "missing argument to %s", argp
[-2]);
477 if (!(cp
= *argp
++) || *cp
== '-')
478 adios (NULL
, "missing argument to %s", argp
[-2]);
483 if (!(cp
= *argp
++) || *cp
== '-')
484 adios (NULL
, "missing argument to %s", argp
[-2]);
489 if (!(cp
= *argp
++) || *cp
== '-')
490 adios (NULL
, "missing argument to %s", argp
[-2]);
495 if (!(cp
= *argp
++) || *cp
== '-')
496 adios (NULL
, "missing argument to %s", argp
[-2]);
497 save_mts_method (cp
);
500 case CREDENTIALSSW
: {
501 if (!(cp
= *argp
++) || *cp
== '-')
502 adios (NULL
, "missing argument to %s", argp
[-2]);
503 add_profile_entry ("credentials", cp
);
508 if (!(cp
= *argp
++) || *cp
== '-')
509 adios (NULL
, "missing argument to %s", argp
[-2]);
510 if (save_message_id_style (cp
) != 0)
511 adios (NULL
, "unsupported messageid \"%s\"", cp
);
516 adios (NULL
, "only one message at a time!");
524 adios (NULL
, "usage: %s [switches] file", invo_name
);
526 if (outputlinelen
< 10)
527 adios (NULL
, "impossible width %d", outputlinelen
);
529 if ((in
= fopen (msg
, "r")) == NULL
)
530 adios (msg
, "unable to open");
538 if ((out
= fopen ("/dev/null", "w")) == NULL
)
539 adios ("/dev/null", "unable to open");
541 char *cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
543 adios(NULL
, "unable to create temporary file in %s",
546 strncpy(tmpfil
, cp
, sizeof(tmpfil
));
550 hdrtab
= msgstate
== NORMAL
? NHeaders
: RHeaders
;
552 for (compnum
= 1;;) {
553 int bufsz
= sizeof buf
;
554 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
558 cp
= add (buf
, NULL
);
559 while (state
== FLDPLUS
) {
561 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
564 putfmt (name
, cp
, out
);
569 finish_headers (out
);
572 fprintf (out
, "\n%s", buf
);
573 while (state
== BODY
) {
575 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
581 finish_headers (out
);
586 adios (NULL
, "message format error in component #%d", compnum
);
589 adios (NULL
, "getfld() returned %d", state
);
593 m_getfld_state_destroy (&gstate
);
607 * Here's how we decide which address to use as the envelope-from
610 * - If we were given an Envelope-From header, use that.
611 * - If we were given a Sender: address, use that.
612 * - Otherwise, use the address on the From: line
615 if (msgflags
& MEFM
) {
617 } else if (seensender
) {
626 * The user didn't specify any of the tls switches. Try to
627 * help them by implying -initialtls if they're using port 465
628 * (smtps, until IANA revoked that registration in 1998).
630 tls
= ! strcmp (port
, "465") || ! strcasecmp (port
, "smtps")
633 #else /* ! TLS_SUPPORT */
635 #endif /* ! TLS_SUPPORT */
638 /* If we are doing a "whom" check */
640 /* This won't work with MTS_SENDMAIL_PIPE. */
641 verify_all_addresses (1, envelope
, xoauth_client_res
);
645 if (msgflags
& MINV
) {
646 make_bcc_file (dashstuff
);
647 if (msgflags
& MVIS
) {
648 if (sm_mts
!= MTS_SENDMAIL_PIPE
) {
649 /* It would be nice to have support to call
650 verify_all_addresses with MTS_SENDMAIL_PIPE, but
651 that might require running sendmail as root. Note
652 that spost didn't verify addresses. */
653 verify_all_addresses (verbose
, envelope
, xoauth_client_res
);
655 post (tmpfil
, 0, verbose
, envelope
, xoauth_client_res
);
657 post (bccfil
, 1, verbose
, envelope
, xoauth_client_res
);
658 (void) m_unlink (bccfil
);
660 post (tmpfil
, 0, isatty (1), envelope
, xoauth_client_res
);
664 (void) m_unlink (tmpfil
);
668 printf ("Partial Message #%s Processed\n", partno
);
670 printf ("Message Processed\n");
683 putfmt (char *name
, char *str
, FILE *out
)
685 int count
, grp
, i
, keep
;
687 char namep
[BUFSIZ
], error
[BUFSIZ
];
688 struct mailname
*mp
= NULL
, *np
= NULL
;
691 while (*str
== ' ' || *str
== '\t')
694 if (msgstate
== NORMAL
&& uprf (name
, "resent")) {
695 advise (NULL
, "illegal header line -- %s:", name
);
700 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
701 if (strncasecmp (name
, "nmh-", 4)) {
702 fprintf (out
, "%s: %s", name
, str
);
704 /* Filter out all Nmh-* headers, because Norm asked. They
705 should never have reached this point. Warn about any
706 that are non-empty. */
707 if (strcmp (str
, "\n")) {
708 char *newline
= strchr (str
, '\n');
709 if (newline
) *newline
= '\0';
711 advise (NULL
, "ignoring header line -- %s: %s", name
, str
);
720 if (hdr
->flags
& HIGN
) {
723 if (hdr
->flags
& HBAD
) {
724 advise (NULL
, "illegal header line -- %s:", name
);
728 msgflags
|= (hdr
->set
& ~(MVIS
| MINV
));
730 if (hdr
->flags
& HSUB
)
731 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
732 if (hdr
->flags
& HFCC
) {
733 if ((cp
= strrchr(str
, '\n')))
735 for (pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
737 insert_fcc (hdr
, pp
);
739 insert_fcc (hdr
, pp
);
743 if (!(hdr
->flags
& HADR
)) {
744 fprintf (out
, "%s: %s", name
, str
);
748 tmpaddrs
.m_next
= NULL
;
749 for (count
= 0; (cp
= getname (str
)); count
++)
750 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
)))) {
754 tmpaddrs
.m_next
= mp
;
758 admonish(cp
, "%s", error
);
759 if (hdr
->flags
& HTRY
)
766 if (hdr
->flags
& HNIL
)
767 fprintf (out
, "%s: %s", name
, str
);
770 * Sender (or Resent-Sender) can have only one address
772 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
773 : (hdr
->set
& MSND
)) {
774 advise (NULL
, "%s: field requires one address", name
);
778 advise (NULL
, "%s: field requires at least one address", name
);
785 if (count
> 1 && (hdr
->flags
& HONE
)) {
786 advise (NULL
, "%s: field only permits one address", name
);
791 nameoutput
= linepos
= 0;
792 snprintf (namep
, sizeof(namep
), "%s%s",
793 (hdr
->flags
& HMNG
) ? "Original-" : "", name
);
795 for (grp
= 0, mp
= tmpaddrs
.m_next
; mp
; mp
= np
)
796 if (mp
->m_nohost
) { /* also used to test (hdr->flags & HTRY) */
797 /* The address doesn't include a host, so it might be an alias. */
798 pp
= akvalue (mp
->m_mbox
); /* do mh alias substitution */
799 qp
= akvisible () ? mp
->m_mbox
: "";
802 putgrp (namep
, np
->m_gname
, out
, hdr
->flags
);
803 while ((cp
= getname (pp
))) {
804 if (!(mp
= getm (cp
, NULL
, 0, error
, sizeof(error
)))) {
805 admonish(cp
, "%s", error
);
811 * If it's a From: or Resent-From: header, save the address
812 * for later possible use (as the envelope address for SMTP)
815 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
816 : (hdr
->set
& MFRM
)) {
817 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
818 from
[sizeof(from
) - 1] = '\0';
823 * Also save the Sender: or Resent-Sender: header as well
826 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
827 : (hdr
->set
& MSND
)) {
828 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
829 sender
[sizeof(sender
) - 1] = '\0';
834 * ALSO ... save Envelope-From
837 if (hdr
->set
& MEFM
) {
838 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
839 efrom
[sizeof(efrom
) - 1] = '\0';
842 if (hdr
->flags
& HBCC
)
845 mp
->m_ingrp
= np
->m_ingrp
;
848 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
850 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
851 /* Catch this before sendmail chokes with:
852 "553 List:; syntax illegal for recipient
854 If we wanted to, we could expand out blind
855 aliases and put them in Bcc:, but then
856 they'd have the Blind-Carbon-Copy
859 "blind lists not compatible with"
865 if (putadr (namep
, qp
, mp
, out
, hdr
->flags
))
866 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
875 /* Address includes a host, so no alias substitution is needed. */
878 * If it's a From: or Resent-From header, save the address
879 * for later possible use (as the envelope address for SMTP)
882 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
883 : (hdr
->set
& MFRM
)) {
884 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
889 * Also save the Sender: header as well
892 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
893 : (hdr
->set
& MSND
)) {
894 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
895 sender
[sizeof(sender
) - 1] = '\0';
900 * ALSO ... save Envelope-From
903 if (hdr
->set
& MEFM
) {
904 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
905 efrom
[sizeof(efrom
) - 1] = '\0';
908 if (hdr
->flags
& HBCC
)
911 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
914 keep
= putadr (namep
, "", mp
, out
, hdr
->flags
);
918 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
925 * If this is a From:/Resent-From: header, save the full thing for
926 * later in case we need it for use when constructing a Bcc draft message
929 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
) : (hdr
->set
& MFRM
)) {
930 strncpy(fullfrom
, str
, sizeof(fullfrom
));
931 fullfrom
[sizeof(fullfrom
) - 1] = 0;
933 * Strip off any trailing newlines
936 while (strlen(fullfrom
) > 0 && fullfrom
[strlen(fullfrom
) - 1] == '\n') {
937 fullfrom
[strlen(fullfrom
) - 1] = '\0';
941 if (grp
> 0 && (hdr
->flags
& HNGR
)) {
942 advise (NULL
, "%s: field does not allow groups", name
);
957 * Probably not necessary, but just in case ...
968 * Now that we've outputted the header fields in the draft
969 * message, we will now output any remaining header fields
970 * that we need to add/create.
974 finish_headers (FILE *out
)
978 if (!(msgflags
& MFRM
)) {
980 * A From: header is now required in the draft.
982 advise (NULL
, "message has no From: header");
983 advise (NULL
, "See default components files for examples");
988 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
989 advise (NULL
, "A Sender: or Envelope-From: header is required "
990 "with multiple\nFrom: addresses");
998 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1000 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
1002 * If we have multiple From: addresses, make sure we have an
1003 * Sender: header. If we don't have one, then generate one
1004 * from Envelope-From: (which in this case, cannot be blank)
1007 if (fromcount
> 1 && seensender
== 0) {
1008 if (efrom
[0] == '\0') {
1009 advise (NULL
, "Envelope-From cannot be blank when there "
1010 "is multiple From: addresses\nand no Sender: "
1014 fprintf (out
, "Sender: %s\n", efrom
);
1018 if (!(msgflags
& MVIS
))
1019 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
1023 if (!(msgflags
& MDAT
)) {
1024 advise (NULL
, "message has no Date: header");
1027 if (!(msgflags
& MFRM
)) {
1028 advise (NULL
, "message has no From: header");
1031 if (!(msgflags
& MRFM
)) {
1032 advise (NULL
, "message has no Resent-From: header");
1033 advise (NULL
, "See default components files for examples");
1037 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
1038 advise (NULL
, "A Resent-Sender: or Envelope-From: header is "
1039 "required with multiple\nResent-From: addresses");
1047 fprintf (out
, "Resent-Date: %s\n", dtime (&tclock
, 0));
1049 fprintf (out
, "Resent-Message-ID: %s\n",
1050 message_id (tclock
, 0));
1052 * If we have multiple Resent-From: addresses, make sure we have an
1053 * Resent-Sender: header. If we don't have one, then generate one
1054 * from Envelope-From (which in this case, cannot be blank)
1057 if (fromcount
> 1 && seensender
== 0) {
1058 if (efrom
[0] == '\0') {
1059 advise (NULL
, "Envelope-From cannot be blank when there "
1060 "is multiple Resent-From: addresses and no "
1061 "Resent-Sender: header");
1064 fprintf (out
, "Resent-Sender: %s\n", efrom
);
1068 if (!(msgflags
& MVIS
))
1069 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
1074 adios (NULL
, "re-format message and try again");
1076 adios (NULL
, "no addressees");
1081 get_header (char *header
, struct headers
*table
)
1085 for (h
= table
; h
->value
; h
++)
1086 if (!strcasecmp (header
? header
: "", h
->value
? h
->value
: ""))
1094 putadr (char *name
, char *aka
, struct mailname
*mp
, FILE *out
, unsigned int flags
)
1098 char buffer
[BUFSIZ
];
1100 if (mp
->m_mbox
== NULL
|| ((flags
& HTRY
) && !insert (mp
)))
1102 if (sm_mts
!= MTS_SENDMAIL_PIPE
&&
1103 ((flags
& (HBCC
| HDCC
| HEFM
)) || mp
->m_ingrp
))
1107 fprintf (out
, "%s: ", name
);
1108 linepos
+= (nameoutput
= strlen (name
) + 2);
1111 if (*aka
&& mp
->m_type
!= UUCPHOST
&& !mp
->m_pers
)
1112 mp
->m_pers
= getcpy (aka
);
1115 snprintf (buffer
, sizeof(buffer
), "%s;", mp
->m_gname
);
1118 cp
= adrformat (mp
);
1125 if (linepos
!= nameoutput
) {
1126 if (len
+ linepos
+ 2 > outputlinelen
)
1127 fprintf (out
, ",\n%*s", linepos
= nameoutput
, "");
1137 return (flags
& HTRY
);
1142 putgrp (char *name
, char *group
, FILE *out
, unsigned int flags
)
1147 if (sm_mts
!= MTS_SENDMAIL_PIPE
&& (flags
& HBCC
))
1151 fprintf (out
, "%s: ", name
);
1152 linepos
+= (nameoutput
= strlen (name
) + 2);
1155 cp
= concat (group
, ";", NULL
);
1158 if (linepos
> nameoutput
) {
1159 if (len
+ linepos
+ 2 > outputlinelen
) {
1160 fprintf (out
, ",\n%*s", nameoutput
, "");
1161 linepos
= nameoutput
;
1175 insert (struct mailname
*np
)
1177 struct mailname
*mp
;
1179 if (np
->m_mbox
== NULL
)
1182 for (mp
= np
->m_type
== LOCALHOST
? &localaddrs
1183 : np
->m_type
== UUCPHOST
? &uuaddrs
1187 if (!strcasecmp (np
->m_host
? np
->m_host
: "",
1188 mp
->m_next
->m_host
? mp
->m_next
->m_host
: "") &&
1189 !strcasecmp (np
->m_mbox
? np
->m_mbox
: "",
1190 mp
->m_next
->m_mbox
? mp
->m_next
->m_mbox
: "") &&
1191 np
->m_bcc
== mp
->m_next
->m_bcc
)
1204 struct mailname
*mp
;
1206 printf ("-------\n\t-- Addresses --\nlocal:\t");
1207 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1208 printf ("%s%s%s", mp
->m_mbox
,
1209 mp
->m_bcc
? "[BCC]" : "",
1210 mp
->m_next
? ",\n\t" : "");
1212 printf ("\nnet:\t");
1213 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1214 printf ("%s%s@%s%s%s", mp
->m_path
? mp
->m_path
: "",
1215 mp
->m_mbox
, mp
->m_host
,
1216 mp
->m_bcc
? "[BCC]" : "",
1217 mp
->m_next
? ",\n\t" : "");
1219 printf ("\nuucp:\t");
1220 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1221 printf ("%s!%s%s%s", mp
->m_host
, mp
->m_mbox
,
1222 mp
->m_bcc
? "[BCC]" : "",
1223 mp
->m_next
? ",\n\t" : "");
1225 printf ("\n\t-- Folder Copies --\nfcc:\t");
1226 for (i
= 0; i
< fccind
; i
++)
1227 printf ("%s%s", fccfold
[i
], i
+ 1 < fccind
? ",\n\t" : "");
1235 struct mailname
*mp
;
1237 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1238 if (annoaux (mp
) == NOTOK
)
1241 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1242 if (annoaux (mp
) == NOTOK
)
1245 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1246 if (annoaux (mp
) == NOTOK
)
1256 annoaux (struct mailname
*mp
)
1259 char buffer
[BUFSIZ
];
1261 snprintf (buffer
, sizeof(buffer
), "%s\n", adrformat (mp
));
1262 i
= strlen (buffer
);
1264 return (write (pfd
, buffer
, i
) == i
? OK
: NOTOK
);
1269 insert_fcc (struct headers
*hdr
, char *pp
)
1273 for (cp
= pp
; isspace ((unsigned char) *cp
); cp
++)
1275 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace ((unsigned char) *pp
); pp
--)
1283 adios (NULL
, "too many %ss", hdr
->value
);
1284 fccfold
[fccind
++] = getcpy (cp
);
1292 make_bcc_file (int dashstuff
)
1298 char *tfile
= NULL
, *program
;
1300 if ((tfile
= m_mktemp2(NULL
, "bccs", NULL
, &out
)) == NULL
) {
1301 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
1303 strncpy (bccfil
, tfile
, sizeof(bccfil
));
1305 fprintf (out
, "From: %s\n", fullfrom
);
1306 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1308 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
1310 fprintf (out
, "Subject: %s", subject
);
1311 fprintf (out
, "BCC:\n");
1314 * Use MIME encapsulation for Bcc messages
1320 * Check if any lines in the message clash with the
1321 * prefix for the MIME multipart separator. If there
1322 * is a clash, increment one of the letters in the
1323 * prefix and check again.
1325 if ((cp
= strchr(prefix
, 'a')) == NULL
)
1326 adios (NULL
, "lost prefix start");
1327 while (find_prefix () == NOTOK
) {
1332 adios (NULL
, "can't find a unique delimiter string");
1337 fprintf (out
, "%s: %s\n%s: multipart/digest; boundary=\"",
1338 VRSN_FIELD
, VRSN_VALUE
, TYPE_FIELD
);
1339 fprintf (out
, "%s\"\n\n--%s\n\n", prefix
, prefix
);
1341 fprintf (out
, "\n------- Blind-Carbon-Copy\n\n");
1347 * Do mhl filtering of Bcc messages instead
1348 * of MIME encapsulation.
1350 if (filter
!= NULL
) {
1351 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1355 adios ("fork", "unable to");
1358 dup2 (fileno (out
), 1);
1360 vec
= argsplit(mhlproc
, &program
, &i
);
1361 vec
[i
++] = "-forward";
1366 /* was the flag -[no]dashstuffing specified? */
1368 vec
[i
++] = "-dashstuffing";
1369 else if (dashstuff
< 0)
1370 vec
[i
++] = "-nodashstuffing";
1373 execvp (program
, vec
);
1374 fprintf (stderr
, "unable to exec ");
1379 pidXwait (child_id
, mhlproc
);
1383 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
1384 adios (tmpfil
, "unable to re-open");
1387 * If using MIME encapsulation, or if the -nodashstuffing
1388 * flag was given, then just copy message. Else do
1389 * RFC934 quoting (dashstuffing).
1391 if (mime
|| dashstuff
< 0)
1392 cpydata (fd
, fileno (out
), tmpfil
, bccfil
);
1394 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
1398 fseek (out
, 0L, SEEK_END
);
1400 fprintf (out
, "\n--%s--\n", prefix
);
1402 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
1408 * Scan message to check if any lines clash with
1409 * the prefix of the MIME multipart separator.
1416 char buffer
[BUFSIZ
];
1419 if ((in
= fopen (tmpfil
, "r")) == NULL
)
1420 adios (tmpfil
, "unable to re-open");
1422 while (fgets (buffer
, sizeof(buffer
) - 1, in
))
1423 if (buffer
[0] == '-' && buffer
[1] == '-') {
1426 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1427 if (!isspace ((unsigned char) *cp
))
1430 if (strcmp (buffer
+ 2, prefix
) == 0) {
1441 #define plural(x) (x == 1 ? "" : "s")
1446 if (badadr
&& unkadr
)
1447 die (NULL
, "%d address%s unparsable, %d addressee%s undeliverable",
1448 badadr
, plural (badadr
), unkadr
, plural (badadr
));
1450 die (NULL
, "%d address%s unparsable", badadr
, plural (badadr
));
1452 die (NULL
, "%d addressee%s undeliverable", unkadr
, plural (unkadr
));
1457 do_addresses (int bccque
, int talk
)
1461 struct mailname
*lp
;
1464 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1465 if (lp
->m_bcc
? bccque
: !bccque
) {
1467 printf (" -- Local Recipients --\n");
1468 do_an_address (lp
, talk
);
1473 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1474 if (lp
->m_bcc
? bccque
: !bccque
) {
1476 printf (" -- UUCP Recipients --\n");
1477 do_an_address (lp
, talk
);
1482 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1483 if (lp
->m_bcc
? bccque
: !bccque
) {
1485 printf (" -- Network Recipients --\n");
1486 do_an_address (lp
, talk
);
1492 if (rp_isbad (retval
= sm_waend ()))
1493 die (NULL
, "problem ending addresses; %s", rp_string (retval
));
1498 * MTS-SPECIFIC INTERACTION
1503 * SENDMAIL/SMTP routines
1507 post (char *file
, int bccque
, int talk
, char *envelope
,
1508 const char *xoauth_client_res
)
1515 if (msgflags
& MINV
)
1516 printf (" -- Posting for %s Recipients --\n",
1517 bccque
? "Blind" : "Sighted");
1519 printf (" -- Posting for All Recipients --\n");
1524 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
1525 char **argp
, *program
;
1528 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1532 adios ("fork", "unable to");
1535 if (freopen( file
, "r", stdin
) == NULL
) {
1536 adios (file
, "can't reopen for sendmail");
1539 argp
= argsplit(sendmail
, &program
, &argc
);
1540 argp
[argc
++] = "-t"; /* read msg for recipients */
1541 argp
[argc
++] = "-i"; /* don't stop on "." */
1543 argp
[argc
++] = "-bv";
1545 argp
[argc
++] = "-v";
1548 execv (program
, argp
);
1549 adios (sendmail
, "can't exec");
1552 pidXwait (child_id
, NULL
);
1556 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1557 verbose
, snoop
, sasl
, saslssf
,
1558 saslmech
, user
, xoauth_client_res
, tls
))
1559 || rp_isbad (retval
= sm_winit (envelope
)))
1560 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1562 do_addresses (bccque
, talk
&& verbose
);
1563 if ((fd
= open (file
, O_RDONLY
)) == NOTOK
)
1564 die (file
, "unable to re-open");
1569 sm_end (!(msgflags
& MINV
) || bccque
? OK
: DONE
);
1573 if (msgflags
& MINV
)
1574 printf (" -- %s Recipient Copies Posted --\n",
1575 bccque
? "Blind" : "Sighted");
1577 printf (" -- Recipient Copies Posted --\n");
1585 /* Address Verification */
1588 verify_all_addresses (int talk
, char *envelope
, const char *xoauth_client_res
)
1591 struct mailname
*lp
;
1595 if (!whomsw
|| checksw
)
1596 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1597 verbose
, snoop
, sasl
, saslssf
,
1598 saslmech
, user
, xoauth_client_res
, tls
))
1599 || rp_isbad (retval
= sm_winit (envelope
)))
1600 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1602 if (talk
&& !whomsw
)
1603 printf (" -- Address Verification --\n");
1604 if (talk
&& localaddrs
.m_next
)
1605 printf (" -- Local Recipients --\n");
1606 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1607 do_an_address (lp
, talk
);
1609 if (talk
&& uuaddrs
.m_next
)
1610 printf (" -- UUCP Recipients --\n");
1611 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1612 do_an_address (lp
, talk
);
1614 if (talk
&& netaddrs
.m_next
)
1615 printf (" -- Network Recipients --\n");
1616 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1617 do_an_address (lp
, talk
);
1620 if (talk
&& !whomsw
)
1621 printf (" -- Address Verification Successful --\n");
1623 if (!whomsw
|| checksw
)
1632 do_an_address (struct mailname
*lp
, int talk
)
1638 switch (lp
->m_type
) {
1642 strncpy (addr
, mbox
, sizeof(addr
));
1646 mbox
= auxformat (lp
, 0);
1648 snprintf (addr
, sizeof(addr
), "%s!%s", lp
->m_host
, lp
->m_mbox
);
1651 default: /* let SendMail decide if the host is bad */
1654 snprintf (addr
, sizeof(addr
), "%s at %s", mbox
, host
);
1659 printf (" %s%s", addr
, whomsw
&& lp
->m_bcc
? "[BCC]" : "");
1661 if (whomsw
&& !checksw
) {
1669 switch (retval
= sm_wadr (mbox
, host
,
1670 lp
->m_type
!= UUCPHOST
? lp
->m_path
: NULL
)) {
1673 printf ("address ok\n");
1679 fprintf (stderr
, " %s: ", addr
);
1680 fprintf (talk
? stdout
: stderr
, "loses; %s\n",
1681 rp_string (retval
));
1687 fprintf (stderr
, " %s: ", addr
);
1688 die (NULL
, "unexpected response; %s", rp_string (retval
));
1696 do_text (char *file
, int fd
)
1701 lseek (fd
, (off_t
) 0, SEEK_SET
);
1703 while ((state
= read (fd
, buf
, sizeof(buf
))) > 0) {
1704 if (rp_isbad (retval
= sm_wtxt (buf
, state
)))
1705 die (NULL
, "problem writing text; %s\n", rp_string (retval
));
1709 die (file
, "problem reading from");
1711 switch (retval
= sm_wtend ()) {
1717 die (NULL
, "posting failed; %s", rp_string (retval
));
1720 die (NULL
, "unexpected response; %s", rp_string (retval
));
1734 (void) m_unlink (tmpfil
);
1735 if (msgflags
& MINV
)
1736 (void) m_unlink (bccfil
);
1738 if (!whomsw
|| checksw
)
1751 hstat
= SIGNAL2 (SIGHUP
, sigser
);
1752 istat
= SIGNAL2 (SIGINT
, sigser
);
1753 qstat
= SIGNAL2 (SIGQUIT
, sigser
);
1754 tstat
= SIGNAL2 (SIGTERM
, sigser
);
1764 SIGNAL (SIGHUP
, hstat
);
1765 SIGNAL (SIGINT
, istat
);
1766 SIGNAL (SIGQUIT
, qstat
);
1767 SIGNAL (SIGTERM
, tstat
);
1775 p_refile (char *file
)
1783 printf (" -- Filing Folder Copies --\n");
1784 for (i
= 0; i
< fccind
; i
++)
1785 fcc (file
, fccfold
[i
]);
1787 printf (" -- Folder Copies Filed --\n");
1792 * Call the `fileproc' to add the file to the folder.
1796 fcc (char *file
, char *folder
)
1799 int i
, status
, argp
;
1801 char **arglist
, *program
;
1804 printf (" %sFcc %s: ", msgstate
== RESENT
? "Resent-" : "", folder
);
1807 for (i
= 0; (child_id
= fork ()) == NOTOK
&& i
< 5; i
++)
1813 fprintf (stderr
, " %sFcc %s: ",
1814 msgstate
== RESENT
? "Resent-" : "", folder
);
1815 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
1819 /* see if we need to add `+' */
1820 snprintf (fold
, sizeof(fold
), "%s%s",
1821 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
1823 /* now exec the fileproc */
1825 arglist
= argsplit(fileproc
, &program
, &argp
);
1826 arglist
[argp
++] = "-link";
1827 arglist
[argp
++] = "-file";
1828 arglist
[argp
++] = file
;
1829 arglist
[argp
++] = fold
;
1830 arglist
[argp
] = NULL
;
1831 execvp (program
, arglist
);
1835 if ((status
= pidwait (child_id
, OK
))) {
1837 fprintf (stderr
, " %sFcc %s: ",
1838 msgstate
== RESENT
? "Resent-" : "", folder
);
1839 pidstatus (status
, verbose
? stdout
: stderr
, NULL
);
1842 printf ("folder ok\n");
1854 die (char *what
, char *fmt
, ...)
1858 (void) m_unlink (tmpfil
);
1859 if (msgflags
& MINV
)
1860 (void) m_unlink (bccfil
);
1862 if (!whomsw
|| checksw
)
1866 advertise (what
, NULL
, fmt
, ap
);