]>
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("user", SASLminc(-4), USERSW) \
84 X("port server submission port name/number", 4, PORTSW) \
85 X("tls", TLSminc(-3), TLSSW) \
86 X("initialtls", TLSminc(-10), INITTLSSW) \
87 X("notls", TLSminc(-5), NTLSSW) \
88 X("fileproc", -4, FILEPROCSW) \
89 X("mhlproc", -3, MHLPROCSW) \
90 X("sendmail program", 0, MTSSM) \
91 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
92 X("credentials legacy|file:filename", 0, CREDENTIALSSW) \
93 X("messageid localname|random", 2, MESSAGEIDSW) \
95 #define X(sw, minchars, id) id,
96 DEFINE_SWITCH_ENUM(POST
);
99 #define X(sw, minchars, id) { sw, minchars, id },
100 DEFINE_SWITCH_ARRAY(POST
, switches
);
111 * flags for headers->flags
113 #define HNOP 0x0000 /* just used to keep .set around */
114 #define HBAD 0x0001 /* bad header - don't let it through */
115 #define HADR 0x0002 /* header has an address field */
116 #define HSUB 0x0004 /* Subject: header */
117 #define HTRY 0x0008 /* try to send to addrs on header */
118 #define HBCC 0x0010 /* don't output this header, unless MTS_SENDMAIL_PIPE */
119 #define HMNG 0x0020 /* munge this header */
120 #define HNGR 0x0040 /* no groups allowed in this header */
121 #define HFCC 0x0080 /* FCC: type header */
122 #define HNIL 0x0100 /* okay for this header not to have addrs */
123 #define HIGN 0x0200 /* ignore this header */
124 #define HDCC 0x0400 /* another undocumented feature */
125 #define HONE 0x0800 /* Only (zero or) one address allowed */
126 #define HEFM 0x1000 /* Envelope-From: header */
129 * flags for headers->set
131 #define MFRM 0x0001 /* we've seen a From: */
132 #define MDAT 0x0002 /* we've seen a Date: */
133 #define MRFM 0x0004 /* we've seen a Resent-From: */
134 #define MVIS 0x0008 /* we've seen sighted addrs */
135 #define MINV 0x0010 /* we've seen blind addrs */
136 #define MSND 0x0020 /* we've seen a Sender: */
137 #define MRSN 0x0040 /* We've seen a Resent-Sendr:*/
138 #define MEFM 0x0080 /* We've seen Envelope-From: */
141 static struct headers NHeaders
[] = {
142 { "Return-Path", HBAD
, 0 },
143 { "Received", HBAD
, 0 },
144 { "Reply-To", HADR
|HNGR
, 0 },
145 { "From", HADR
|HNGR
, MFRM
},
146 { "Sender", HADR
|HNGR
|HONE
, MSND
},
148 { "Subject", HSUB
, 0 },
149 { "To", HADR
|HTRY
, MVIS
},
150 { "cc", HADR
|HTRY
, MVIS
},
151 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, MINV
},
152 { "Dcc", HADR
|HTRY
|HDCC
|HNIL
, MVIS
}, /* sorta cc & bcc combined */
153 { "Message-ID", HBAD
, 0 },
155 { "Envelope-From", HADR
|HONE
|HEFM
, MEFM
},
159 static struct headers RHeaders
[] = {
160 { "Resent-Reply-To", HADR
|HNGR
, 0 },
161 { "Resent-From", HADR
|HNGR
, MRFM
},
162 { "Resent-Sender", HADR
|HNGR
, MRSN
},
163 { "Resent-Date", HBAD
, 0 },
164 { "Resent-Subject", HSUB
, 0 },
165 { "Resent-To", HADR
|HTRY
, MVIS
},
166 { "Resent-cc", HADR
|HTRY
, MVIS
},
167 { "Resent-Bcc", HADR
|HTRY
|HBCC
, MINV
},
168 { "Resent-Message-ID", HBAD
, 0 },
169 { "Resent-Fcc", HFCC
, 0 },
170 { "Reply-To", HADR
, 0 },
171 { "From", HADR
|HNGR
, MFRM
},
172 { "Sender", HADR
|HNGR
, MSND
},
173 { "Date", HNOP
, MDAT
},
174 { "To", HADR
|HNIL
, 0 },
175 { "cc", HADR
|HNIL
, 0 },
176 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, 0 },
178 { "Envelope-From", HADR
|HONE
|HEFM
, MEFM
},
182 static short fccind
= 0; /* index into fccfold[] */
183 static short outputlinelen
= OUTPUTLINELEN
;
185 static int pfd
= NOTOK
; /* fd to write annotation list to */
186 static int recipients
= 0; /* how many people will get a copy */
187 static int unkadr
= 0; /* how many of those were unknown */
188 static int badadr
= 0; /* number of bad addrs */
189 static int badmsg
= 0; /* message has bad semantics */
190 static int verbose
= 0; /* spell it out */
191 static int format
= 1; /* format addresses */
192 static int mime
= 0; /* use MIME-style encapsulations for Bcc */
193 static int msgid
= 0; /* add msgid */
194 static int debug
= 0; /* debugging post */
195 static int watch
= 0; /* watch the delivery process */
196 static int whomsw
= 0; /* we are whom not post */
197 static int checksw
= 0; /* whom -check */
198 static int linepos
=0; /* putadr()'s position on the line */
199 static int nameoutput
=0; /* putadr() has output header name */
200 static int sasl
=0; /* Use SASL auth for SMTP */
201 static int saslssf
=-1; /* Our maximum SSF for SASL */
202 static char *saslmech
=NULL
; /* Force use of particular SASL mech */
203 static char *user
=NULL
; /* Authenticate as this user */
204 static char *port
="submission"; /* Name of server port for SMTP submission */
205 static int tls
=-1; /* Use TLS for encryption */
206 static int fromcount
=0; /* Count of addresses on From: header */
207 static int seensender
=0; /* Have we seen a Sender: header? */
209 static unsigned msgflags
= 0; /* what we've seen */
213 static int msgstate
= NORMAL
;
215 static time_t tclock
= 0; /* the time we started (more or less) */
217 static SIGNAL_HANDLER hstat
, istat
, qstat
, tstat
;
219 static char tmpfil
[BUFSIZ
];
220 static char bccfil
[BUFSIZ
];
222 static char from
[BUFSIZ
]; /* my network address */
223 static char sender
[BUFSIZ
]; /* my Sender: header */
224 static char efrom
[BUFSIZ
]; /* my Envelope-From: header */
225 static char fullfrom
[BUFSIZ
]; /* full contents of From header */
226 static char *filter
= NULL
; /* the filter for BCC'ing */
227 static char *subject
= NULL
; /* the subject field for BCC'ing */
228 static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
230 static struct headers
*hdrtab
; /* table for the message we're doing */
232 static struct mailname localaddrs
; /* local addrs */
233 static struct mailname netaddrs
; /* network addrs */
234 static struct mailname uuaddrs
; /* uucp addrs */
235 static struct mailname tmpaddrs
; /* temporary queue */
237 static int snoop
= 0;
238 static char *clientsw
= NULL
;
239 static char *serversw
= NULL
;
241 static char prefix
[] = "----- =_aaaaaaaaaa";
243 static char *partno
= NULL
;
248 static void putfmt (char *, char *, FILE *);
249 static void start_headers (void);
250 static void finish_headers (FILE *);
251 static int get_header (char *, struct headers
*);
252 static int putadr (char *, char *, struct mailname
*, FILE *, unsigned int);
253 static void putgrp (char *, char *, FILE *, unsigned int);
254 static int insert (struct mailname
*);
255 static void pl (void);
256 static void anno (void);
257 static int annoaux (struct mailname
*);
258 static void insert_fcc (struct headers
*, char *);
259 static void make_bcc_file (int);
260 static void verify_all_addresses (int, char *);
261 static void chkadr (void);
262 static void sigon (void);
263 static void sigoff (void);
264 static void p_refile (char *);
265 static void fcc (char *, char *);
266 static void die (char *, char *, ...);
267 static void post (char *, int, int, char *);
268 static void do_text (char *file
, int fd
);
269 static void do_an_address (struct mailname
*, int);
270 static void do_addresses (int, int);
271 static int find_prefix (void);
275 main (int argc
, char **argv
)
277 int state
, compnum
, dashstuff
= 0;
278 char *cp
, *msg
= NULL
, **argp
, **arguments
, *envelope
;
279 char buf
[BUFSIZ
], name
[NAMESZ
];
281 m_getfld_state_t gstate
= 0;
283 if (nmh_init(argv
[0], 0 /* use context_foil() */)) { return 1; }
285 mts_init (invo_name
);
286 arguments
= getarguments (invo_name
, argc
, argv
, 0);
289 while ((cp
= *argp
++)) {
291 switch (smatch (++cp
, switches
)) {
293 ambigsw (cp
, switches
);
296 adios (NULL
, "-%s unknown", cp
);
299 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
300 print_help (buf
, switches
, 0);
303 print_version(invo_name
);
307 if (!(cp
= *argp
++) || *cp
== '-')
308 adios (NULL
, "missing argument to %s", argp
[-2]);
309 /* create a minimal context */
310 if (context_foil (cp
) == -1)
315 if (!(cp
= *argp
++) || *cp
== '-')
316 adios (NULL
, "missing argument to %s", argp
[-2]);
317 if ((state
= alias (cp
)) != AK_OK
)
318 adios (NULL
, "aliasing error in %s - %s",
319 cp
, akerror (state
));
338 if (!(filter
= *argp
++) || *filter
== '-')
339 adios (NULL
, "missing argument to %s", argp
[-2]);
394 if (!(cp
= *argp
++) || *cp
== '-')
395 adios (NULL
, "missing argument to %s", argp
[-2]);
396 if ((outputlinelen
= atoi (cp
)) < 10)
397 adios (NULL
, "impossible width %d", outputlinelen
);
401 if (!(cp
= *argp
++) || *cp
== '-')
402 adios (NULL
, "missing argument to %s", argp
[-2]);
403 if ((pfd
= atoi (cp
)) <= 2)
404 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
408 if (!(clientsw
= *argp
++) || *clientsw
== '-')
409 adios (NULL
, "missing argument to %s", argp
[-2]);
412 if (!(serversw
= *argp
++) || *serversw
== '-')
413 adios (NULL
, "missing argument to %s", argp
[-2]);
420 if (!(partno
= *argp
++) || *partno
== '-')
421 adios (NULL
, "missing argument to %s", argp
[-2]);
433 if (!(cp
= *argp
++) || *cp
== '-')
434 adios (NULL
, "missing argument to %s", argp
[-2]);
439 if (!(saslmech
= *argp
++) || *saslmech
== '-')
440 adios (NULL
, "missing argument to %s", argp
[-2]);
444 if (!(user
= *argp
++) || *user
== '-')
445 adios (NULL
, "missing argument to %s", argp
[-2]);
449 if (!(port
= *argp
++) || *port
== '-')
450 adios (NULL
, "missing argument to %s", argp
[-2]);
466 if (!(cp
= *argp
++) || *cp
== '-')
467 adios (NULL
, "missing argument to %s", argp
[-2]);
472 if (!(cp
= *argp
++) || *cp
== '-')
473 adios (NULL
, "missing argument to %s", argp
[-2]);
478 if (!(cp
= *argp
++) || *cp
== '-')
479 adios (NULL
, "missing argument to %s", argp
[-2]);
484 if (!(cp
= *argp
++) || *cp
== '-')
485 adios (NULL
, "missing argument to %s", argp
[-2]);
486 save_mts_method (cp
);
489 case CREDENTIALSSW
: {
490 if (!(cp
= *argp
++) || *cp
== '-')
491 adios (NULL
, "missing argument to %s", argp
[-2]);
492 add_profile_entry ("credentials", cp
);
497 if (!(cp
= *argp
++) || *cp
== '-')
498 adios (NULL
, "missing argument to %s", argp
[-2]);
499 if (save_message_id_style (cp
) != 0)
500 adios (NULL
, "unsupported messageid \"%s\"", cp
);
505 adios (NULL
, "only one message at a time!");
513 adios (NULL
, "usage: %s [switches] file", invo_name
);
515 if (outputlinelen
< 10)
516 adios (NULL
, "impossible width %d", outputlinelen
);
518 if ((in
= fopen (msg
, "r")) == NULL
)
519 adios (msg
, "unable to open");
527 if ((out
= fopen ("/dev/null", "w")) == NULL
)
528 adios ("/dev/null", "unable to open");
530 char *cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
532 adios(NULL
, "unable to create temporary file in %s",
535 strncpy(tmpfil
, cp
, sizeof(tmpfil
));
539 hdrtab
= msgstate
== NORMAL
? NHeaders
: RHeaders
;
541 for (compnum
= 1;;) {
542 int bufsz
= sizeof buf
;
543 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
547 cp
= add (buf
, NULL
);
548 while (state
== FLDPLUS
) {
550 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
553 putfmt (name
, cp
, out
);
558 finish_headers (out
);
561 fprintf (out
, "\n%s", buf
);
562 while (state
== BODY
) {
564 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
570 finish_headers (out
);
575 adios (NULL
, "message format error in component #%d", compnum
);
578 adios (NULL
, "getfld() returned %d", state
);
582 m_getfld_state_destroy (&gstate
);
596 * Here's how we decide which address to use as the envelope-from
599 * - If we were given an Envelope-From header, use that.
600 * - If we were given a Sender: address, use that.
601 * - Otherwise, use the address on the From: line
604 if (msgflags
& MEFM
) {
606 } else if (seensender
) {
615 * The user didn't specify any of the tls switches. Try to
616 * help them by implying -initialtls if they're using port 465
617 * (smtps, until IANA revoked that registration in 1998).
619 tls
= ! strcmp (port
, "465") || ! strcasecmp (port
, "smtps")
622 #else /* ! TLS_SUPPORT */
624 #endif /* ! TLS_SUPPORT */
627 /* If we are doing a "whom" check */
629 /* This won't work with MTS_SENDMAIL_PIPE. */
630 verify_all_addresses (1, envelope
);
634 if (msgflags
& MINV
) {
635 make_bcc_file (dashstuff
);
636 if (msgflags
& MVIS
) {
637 if (sm_mts
!= MTS_SENDMAIL_PIPE
) {
638 /* It would be nice to have support to call
639 verify_all_addresses with MTS_SENDMAIL_PIPE, but
640 that might require running sendmail as root. Note
641 that spost didn't verify addresses. */
642 verify_all_addresses (verbose
, envelope
);
644 post (tmpfil
, 0, verbose
, envelope
);
646 post (bccfil
, 1, verbose
, envelope
);
647 (void) m_unlink (bccfil
);
649 post (tmpfil
, 0, isatty (1), envelope
);
653 (void) m_unlink (tmpfil
);
657 printf ("Partial Message #%s Processed\n", partno
);
659 printf ("Message Processed\n");
672 putfmt (char *name
, char *str
, FILE *out
)
674 int count
, grp
, i
, keep
;
676 char namep
[BUFSIZ
], error
[BUFSIZ
];
677 struct mailname
*mp
= NULL
, *np
= NULL
;
680 while (*str
== ' ' || *str
== '\t')
683 if (msgstate
== NORMAL
&& uprf (name
, "resent")) {
684 advise (NULL
, "illegal header line -- %s:", name
);
689 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
690 if (strncasecmp (name
, "nmh-", 4)) {
691 fprintf (out
, "%s: %s", name
, str
);
693 /* Filter out all Nmh-* headers, because Norm asked. They
694 should never have reached this point. Warn about any
695 that are non-empty. */
696 if (strcmp (str
, "\n")) {
697 char *newline
= strchr (str
, '\n');
698 if (newline
) *newline
= '\0';
700 advise (NULL
, "ignoring header line -- %s: %s", name
, str
);
709 if (hdr
->flags
& HIGN
) {
712 if (hdr
->flags
& HBAD
) {
713 advise (NULL
, "illegal header line -- %s:", name
);
717 msgflags
|= (hdr
->set
& ~(MVIS
| MINV
));
719 if (hdr
->flags
& HSUB
)
720 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
721 if (hdr
->flags
& HFCC
) {
722 if ((cp
= strrchr(str
, '\n')))
724 for (pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
726 insert_fcc (hdr
, pp
);
728 insert_fcc (hdr
, pp
);
732 if (!(hdr
->flags
& HADR
)) {
733 fprintf (out
, "%s: %s", name
, str
);
737 tmpaddrs
.m_next
= NULL
;
738 for (count
= 0; (cp
= getname (str
)); count
++)
739 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
)))) {
743 tmpaddrs
.m_next
= mp
;
747 admonish(cp
, "%s", error
);
748 if (hdr
->flags
& HTRY
)
755 if (hdr
->flags
& HNIL
)
756 fprintf (out
, "%s: %s", name
, str
);
759 * Sender (or Resent-Sender) can have only one address
761 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
762 : (hdr
->set
& MSND
)) {
763 advise (NULL
, "%s: field requires one address", name
);
767 advise (NULL
, "%s: field requires at least one address", name
);
774 if (count
> 1 && (hdr
->flags
& HONE
)) {
775 advise (NULL
, "%s: field only permits one address", name
);
780 nameoutput
= linepos
= 0;
781 snprintf (namep
, sizeof(namep
), "%s%s",
782 (hdr
->flags
& HMNG
) ? "Original-" : "", name
);
784 for (grp
= 0, mp
= tmpaddrs
.m_next
; mp
; mp
= np
)
785 if (mp
->m_nohost
) { /* also used to test (hdr->flags & HTRY) */
786 /* The address doesn't include a host, so it might be an alias. */
787 pp
= akvalue (mp
->m_mbox
); /* do mh alias substitution */
788 qp
= akvisible () ? mp
->m_mbox
: "";
791 putgrp (namep
, np
->m_gname
, out
, hdr
->flags
);
792 while ((cp
= getname (pp
))) {
793 if (!(mp
= getm (cp
, NULL
, 0, error
, sizeof(error
)))) {
794 admonish(cp
, "%s", error
);
800 * If it's a From: or Resent-From: header, save the address
801 * for later possible use (as the envelope address for SMTP)
804 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
805 : (hdr
->set
& MFRM
)) {
806 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
807 from
[sizeof(from
) - 1] = '\0';
812 * Also save the Sender: or Resent-Sender: header as well
815 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
816 : (hdr
->set
& MSND
)) {
817 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
818 sender
[sizeof(sender
) - 1] = '\0';
823 * ALSO ... save Envelope-From
826 if (hdr
->set
& MEFM
) {
827 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
828 efrom
[sizeof(efrom
) - 1] = '\0';
831 if (hdr
->flags
& HBCC
)
834 mp
->m_ingrp
= np
->m_ingrp
;
837 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
839 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
840 /* Catch this before sendmail chokes with:
841 "553 List:; syntax illegal for recipient
843 If we wanted to, we could expand out blind
844 aliases and put them in Bcc:, but then
845 they'd have the Blind-Carbon-Copy
848 "blind lists not compatible with"
854 if (putadr (namep
, qp
, mp
, out
, hdr
->flags
))
855 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
864 /* Address includes a host, so no alias substitution is needed. */
867 * If it's a From: or Resent-From header, save the address
868 * for later possible use (as the envelope address for SMTP)
871 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
872 : (hdr
->set
& MFRM
)) {
873 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
878 * Also save the Sender: header as well
881 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
882 : (hdr
->set
& MSND
)) {
883 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
884 sender
[sizeof(sender
) - 1] = '\0';
889 * ALSO ... save Envelope-From
892 if (hdr
->set
& MEFM
) {
893 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
894 efrom
[sizeof(efrom
) - 1] = '\0';
897 if (hdr
->flags
& HBCC
)
900 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
903 keep
= putadr (namep
, "", mp
, out
, hdr
->flags
);
907 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
914 * If this is a From:/Resent-From: header, save the full thing for
915 * later in case we need it for use when constructing a Bcc draft message
918 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
) : (hdr
->set
& MFRM
)) {
919 strncpy(fullfrom
, str
, sizeof(fullfrom
));
920 fullfrom
[sizeof(fullfrom
) - 1] = 0;
922 * Strip off any trailing newlines
925 while (strlen(fullfrom
) > 0 && fullfrom
[strlen(fullfrom
) - 1] == '\n') {
926 fullfrom
[strlen(fullfrom
) - 1] = '\0';
930 if (grp
> 0 && (hdr
->flags
& HNGR
)) {
931 advise (NULL
, "%s: field does not allow groups", name
);
946 * Probably not necessary, but just in case ...
957 * Now that we've outputted the header fields in the draft
958 * message, we will now output any remaining header fields
959 * that we need to add/create.
963 finish_headers (FILE *out
)
967 if (!(msgflags
& MFRM
)) {
969 * A From: header is now required in the draft.
971 advise (NULL
, "message has no From: header");
972 advise (NULL
, "See default components files for examples");
977 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
978 advise (NULL
, "A Sender: or Envelope-From: header is required "
979 "with multiple\nFrom: addresses");
987 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
989 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
991 * If we have multiple From: addresses, make sure we have an
992 * Sender: header. If we don't have one, then generate one
993 * from Envelope-From: (which in this case, cannot be blank)
996 if (fromcount
> 1 && seensender
== 0) {
997 if (efrom
[0] == '\0') {
998 advise (NULL
, "Envelope-From cannot be blank when there "
999 "is multiple From: addresses\nand no Sender: "
1003 fprintf (out
, "Sender: %s\n", efrom
);
1007 if (!(msgflags
& MVIS
))
1008 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
1012 if (!(msgflags
& MDAT
)) {
1013 advise (NULL
, "message has no Date: header");
1016 if (!(msgflags
& MFRM
)) {
1017 advise (NULL
, "message has no From: header");
1020 if (!(msgflags
& MRFM
)) {
1021 advise (NULL
, "message has no Resent-From: header");
1022 advise (NULL
, "See default components files for examples");
1026 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
1027 advise (NULL
, "A Resent-Sender: or Envelope-From: header is "
1028 "required with multiple\nResent-From: addresses");
1036 fprintf (out
, "Resent-Date: %s\n", dtime (&tclock
, 0));
1038 fprintf (out
, "Resent-Message-ID: %s\n",
1039 message_id (tclock
, 0));
1041 * If we have multiple Resent-From: addresses, make sure we have an
1042 * Resent-Sender: header. If we don't have one, then generate one
1043 * from Envelope-From (which in this case, cannot be blank)
1046 if (fromcount
> 1 && seensender
== 0) {
1047 if (efrom
[0] == '\0') {
1048 advise (NULL
, "Envelope-From cannot be blank when there "
1049 "is multiple Resent-From: addresses and no "
1050 "Resent-Sender: header");
1053 fprintf (out
, "Resent-Sender: %s\n", efrom
);
1057 if (!(msgflags
& MVIS
))
1058 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
1063 adios (NULL
, "re-format message and try again");
1065 adios (NULL
, "no addressees");
1070 get_header (char *header
, struct headers
*table
)
1074 for (h
= table
; h
->value
; h
++)
1075 if (!strcasecmp (header
? header
: "", h
->value
? h
->value
: ""))
1083 putadr (char *name
, char *aka
, struct mailname
*mp
, FILE *out
, unsigned int flags
)
1087 char buffer
[BUFSIZ
];
1089 if (mp
->m_mbox
== NULL
|| ((flags
& HTRY
) && !insert (mp
)))
1091 if (sm_mts
!= MTS_SENDMAIL_PIPE
&&
1092 ((flags
& (HBCC
| HDCC
| HEFM
)) || mp
->m_ingrp
))
1096 fprintf (out
, "%s: ", name
);
1097 linepos
+= (nameoutput
= strlen (name
) + 2);
1100 if (*aka
&& mp
->m_type
!= UUCPHOST
&& !mp
->m_pers
)
1101 mp
->m_pers
= getcpy (aka
);
1104 snprintf (buffer
, sizeof(buffer
), "%s;", mp
->m_gname
);
1107 cp
= adrformat (mp
);
1114 if (linepos
!= nameoutput
) {
1115 if (len
+ linepos
+ 2 > outputlinelen
)
1116 fprintf (out
, ",\n%*s", linepos
= nameoutput
, "");
1126 return (flags
& HTRY
);
1131 putgrp (char *name
, char *group
, FILE *out
, unsigned int flags
)
1136 if (sm_mts
!= MTS_SENDMAIL_PIPE
&& (flags
& HBCC
))
1140 fprintf (out
, "%s: ", name
);
1141 linepos
+= (nameoutput
= strlen (name
) + 2);
1144 cp
= concat (group
, ";", NULL
);
1147 if (linepos
> nameoutput
) {
1148 if (len
+ linepos
+ 2 > outputlinelen
) {
1149 fprintf (out
, ",\n%*s", nameoutput
, "");
1150 linepos
= nameoutput
;
1164 insert (struct mailname
*np
)
1166 struct mailname
*mp
;
1168 if (np
->m_mbox
== NULL
)
1171 for (mp
= np
->m_type
== LOCALHOST
? &localaddrs
1172 : np
->m_type
== UUCPHOST
? &uuaddrs
1176 if (!strcasecmp (np
->m_host
? np
->m_host
: "",
1177 mp
->m_next
->m_host
? mp
->m_next
->m_host
: "") &&
1178 !strcasecmp (np
->m_mbox
? np
->m_mbox
: "",
1179 mp
->m_next
->m_mbox
? mp
->m_next
->m_mbox
: "") &&
1180 np
->m_bcc
== mp
->m_next
->m_bcc
)
1193 struct mailname
*mp
;
1195 printf ("-------\n\t-- Addresses --\nlocal:\t");
1196 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1197 printf ("%s%s%s", mp
->m_mbox
,
1198 mp
->m_bcc
? "[BCC]" : "",
1199 mp
->m_next
? ",\n\t" : "");
1201 printf ("\nnet:\t");
1202 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1203 printf ("%s%s@%s%s%s", mp
->m_path
? mp
->m_path
: "",
1204 mp
->m_mbox
, mp
->m_host
,
1205 mp
->m_bcc
? "[BCC]" : "",
1206 mp
->m_next
? ",\n\t" : "");
1208 printf ("\nuucp:\t");
1209 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1210 printf ("%s!%s%s%s", mp
->m_host
, mp
->m_mbox
,
1211 mp
->m_bcc
? "[BCC]" : "",
1212 mp
->m_next
? ",\n\t" : "");
1214 printf ("\n\t-- Folder Copies --\nfcc:\t");
1215 for (i
= 0; i
< fccind
; i
++)
1216 printf ("%s%s", fccfold
[i
], i
+ 1 < fccind
? ",\n\t" : "");
1224 struct mailname
*mp
;
1226 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1227 if (annoaux (mp
) == NOTOK
)
1230 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1231 if (annoaux (mp
) == NOTOK
)
1234 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1235 if (annoaux (mp
) == NOTOK
)
1245 annoaux (struct mailname
*mp
)
1248 char buffer
[BUFSIZ
];
1250 snprintf (buffer
, sizeof(buffer
), "%s\n", adrformat (mp
));
1251 i
= strlen (buffer
);
1253 return (write (pfd
, buffer
, i
) == i
? OK
: NOTOK
);
1258 insert_fcc (struct headers
*hdr
, char *pp
)
1262 for (cp
= pp
; isspace ((unsigned char) *cp
); cp
++)
1264 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace ((unsigned char) *pp
); pp
--)
1272 adios (NULL
, "too many %ss", hdr
->value
);
1273 fccfold
[fccind
++] = getcpy (cp
);
1281 make_bcc_file (int dashstuff
)
1287 char *tfile
= NULL
, *program
;
1289 if ((tfile
= m_mktemp2(NULL
, "bccs", NULL
, &out
)) == NULL
) {
1290 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
1292 strncpy (bccfil
, tfile
, sizeof(bccfil
));
1294 fprintf (out
, "From: %s\n", fullfrom
);
1295 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1297 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
1299 fprintf (out
, "Subject: %s", subject
);
1300 fprintf (out
, "BCC:\n");
1303 * Use MIME encapsulation for Bcc messages
1309 * Check if any lines in the message clash with the
1310 * prefix for the MIME multipart separator. If there
1311 * is a clash, increment one of the letters in the
1312 * prefix and check again.
1314 if ((cp
= strchr(prefix
, 'a')) == NULL
)
1315 adios (NULL
, "lost prefix start");
1316 while (find_prefix () == NOTOK
) {
1321 adios (NULL
, "can't find a unique delimiter string");
1326 fprintf (out
, "%s: %s\n%s: multipart/digest; boundary=\"",
1327 VRSN_FIELD
, VRSN_VALUE
, TYPE_FIELD
);
1328 fprintf (out
, "%s\"\n\n--%s\n\n", prefix
, prefix
);
1330 fprintf (out
, "\n------- Blind-Carbon-Copy\n\n");
1336 * Do mhl filtering of Bcc messages instead
1337 * of MIME encapsulation.
1339 if (filter
!= NULL
) {
1340 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1344 adios ("fork", "unable to");
1347 dup2 (fileno (out
), 1);
1349 vec
= argsplit(mhlproc
, &program
, &i
);
1350 vec
[i
++] = "-forward";
1355 /* was the flag -[no]dashstuffing specified? */
1357 vec
[i
++] = "-dashstuffing";
1358 else if (dashstuff
< 0)
1359 vec
[i
++] = "-nodashstuffing";
1362 execvp (program
, vec
);
1363 fprintf (stderr
, "unable to exec ");
1368 pidXwait (child_id
, mhlproc
);
1372 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
1373 adios (tmpfil
, "unable to re-open");
1376 * If using MIME encapsulation, or if the -nodashstuffing
1377 * flag was given, then just copy message. Else do
1378 * RFC934 quoting (dashstuffing).
1380 if (mime
|| dashstuff
< 0)
1381 cpydata (fd
, fileno (out
), tmpfil
, bccfil
);
1383 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
1387 fseek (out
, 0L, SEEK_END
);
1389 fprintf (out
, "\n--%s--\n", prefix
);
1391 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
1397 * Scan message to check if any lines clash with
1398 * the prefix of the MIME multipart separator.
1405 char buffer
[BUFSIZ
];
1408 if ((in
= fopen (tmpfil
, "r")) == NULL
)
1409 adios (tmpfil
, "unable to re-open");
1411 while (fgets (buffer
, sizeof(buffer
) - 1, in
))
1412 if (buffer
[0] == '-' && buffer
[1] == '-') {
1415 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1416 if (!isspace ((unsigned char) *cp
))
1419 if (strcmp (buffer
+ 2, prefix
) == 0) {
1430 #define plural(x) (x == 1 ? "" : "s")
1435 if (badadr
&& unkadr
)
1436 die (NULL
, "%d address%s unparsable, %d addressee%s undeliverable",
1437 badadr
, plural (badadr
), unkadr
, plural (badadr
));
1439 die (NULL
, "%d address%s unparsable", badadr
, plural (badadr
));
1441 die (NULL
, "%d addressee%s undeliverable", unkadr
, plural (unkadr
));
1446 do_addresses (int bccque
, int talk
)
1450 struct mailname
*lp
;
1453 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1454 if (lp
->m_bcc
? bccque
: !bccque
) {
1456 printf (" -- Local Recipients --\n");
1457 do_an_address (lp
, talk
);
1462 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1463 if (lp
->m_bcc
? bccque
: !bccque
) {
1465 printf (" -- UUCP Recipients --\n");
1466 do_an_address (lp
, talk
);
1471 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1472 if (lp
->m_bcc
? bccque
: !bccque
) {
1474 printf (" -- Network Recipients --\n");
1475 do_an_address (lp
, talk
);
1481 if (rp_isbad (retval
= sm_waend ()))
1482 die (NULL
, "problem ending addresses; %s", rp_string (retval
));
1487 * MTS-SPECIFIC INTERACTION
1492 * SENDMAIL/SMTP routines
1496 post (char *file
, int bccque
, int talk
, char *envelope
)
1503 if (msgflags
& MINV
)
1504 printf (" -- Posting for %s Recipients --\n",
1505 bccque
? "Blind" : "Sighted");
1507 printf (" -- Posting for All Recipients --\n");
1512 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
1513 char **argp
, *program
;
1516 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1520 adios ("fork", "unable to");
1523 if (freopen( file
, "r", stdin
) == NULL
) {
1524 adios (file
, "can't reopen for sendmail");
1527 argp
= argsplit(sendmail
, &program
, &argc
);
1528 argp
[argc
++] = "-t"; /* read msg for recipients */
1529 argp
[argc
++] = "-i"; /* don't stop on "." */
1531 argp
[argc
++] = "-bv";
1533 argp
[argc
++] = "-v";
1536 execv (program
, argp
);
1537 adios (sendmail
, "can't exec");
1540 pidXwait (child_id
, NULL
);
1544 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1545 verbose
, snoop
, sasl
, saslssf
,
1546 saslmech
, user
, tls
)) ||
1547 rp_isbad (retval
= sm_winit (envelope
)))
1548 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1550 do_addresses (bccque
, talk
&& verbose
);
1551 if ((fd
= open (file
, O_RDONLY
)) == NOTOK
)
1552 die (file
, "unable to re-open");
1557 sm_end (!(msgflags
& MINV
) || bccque
? OK
: DONE
);
1561 if (msgflags
& MINV
)
1562 printf (" -- %s Recipient Copies Posted --\n",
1563 bccque
? "Blind" : "Sighted");
1565 printf (" -- Recipient Copies Posted --\n");
1573 /* Address Verification */
1576 verify_all_addresses (int talk
, char *envelope
)
1579 struct mailname
*lp
;
1583 if (!whomsw
|| checksw
)
1584 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1585 verbose
, snoop
, sasl
, saslssf
,
1586 saslmech
, user
, tls
))
1587 || rp_isbad (retval
= sm_winit (envelope
)))
1588 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1590 if (talk
&& !whomsw
)
1591 printf (" -- Address Verification --\n");
1592 if (talk
&& localaddrs
.m_next
)
1593 printf (" -- Local Recipients --\n");
1594 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1595 do_an_address (lp
, talk
);
1597 if (talk
&& uuaddrs
.m_next
)
1598 printf (" -- UUCP Recipients --\n");
1599 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1600 do_an_address (lp
, talk
);
1602 if (talk
&& netaddrs
.m_next
)
1603 printf (" -- Network Recipients --\n");
1604 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1605 do_an_address (lp
, talk
);
1608 if (talk
&& !whomsw
)
1609 printf (" -- Address Verification Successful --\n");
1611 if (!whomsw
|| checksw
)
1620 do_an_address (struct mailname
*lp
, int talk
)
1626 switch (lp
->m_type
) {
1630 strncpy (addr
, mbox
, sizeof(addr
));
1634 mbox
= auxformat (lp
, 0);
1636 snprintf (addr
, sizeof(addr
), "%s!%s", lp
->m_host
, lp
->m_mbox
);
1639 default: /* let SendMail decide if the host is bad */
1642 snprintf (addr
, sizeof(addr
), "%s at %s", mbox
, host
);
1647 printf (" %s%s", addr
, whomsw
&& lp
->m_bcc
? "[BCC]" : "");
1649 if (whomsw
&& !checksw
) {
1657 switch (retval
= sm_wadr (mbox
, host
,
1658 lp
->m_type
!= UUCPHOST
? lp
->m_path
: NULL
)) {
1661 printf ("address ok\n");
1667 fprintf (stderr
, " %s: ", addr
);
1668 fprintf (talk
? stdout
: stderr
, "loses; %s\n",
1669 rp_string (retval
));
1675 fprintf (stderr
, " %s: ", addr
);
1676 die (NULL
, "unexpected response; %s", rp_string (retval
));
1684 do_text (char *file
, int fd
)
1689 lseek (fd
, (off_t
) 0, SEEK_SET
);
1691 while ((state
= read (fd
, buf
, sizeof(buf
))) > 0) {
1692 if (rp_isbad (retval
= sm_wtxt (buf
, state
)))
1693 die (NULL
, "problem writing text; %s\n", rp_string (retval
));
1697 die (file
, "problem reading from");
1699 switch (retval
= sm_wtend ()) {
1705 die (NULL
, "posting failed; %s", rp_string (retval
));
1708 die (NULL
, "unexpected response; %s", rp_string (retval
));
1722 (void) m_unlink (tmpfil
);
1723 if (msgflags
& MINV
)
1724 (void) m_unlink (bccfil
);
1726 if (!whomsw
|| checksw
)
1739 hstat
= SIGNAL2 (SIGHUP
, sigser
);
1740 istat
= SIGNAL2 (SIGINT
, sigser
);
1741 qstat
= SIGNAL2 (SIGQUIT
, sigser
);
1742 tstat
= SIGNAL2 (SIGTERM
, sigser
);
1752 SIGNAL (SIGHUP
, hstat
);
1753 SIGNAL (SIGINT
, istat
);
1754 SIGNAL (SIGQUIT
, qstat
);
1755 SIGNAL (SIGTERM
, tstat
);
1763 p_refile (char *file
)
1771 printf (" -- Filing Folder Copies --\n");
1772 for (i
= 0; i
< fccind
; i
++)
1773 fcc (file
, fccfold
[i
]);
1775 printf (" -- Folder Copies Filed --\n");
1780 * Call the `fileproc' to add the file to the folder.
1784 fcc (char *file
, char *folder
)
1787 int i
, status
, argp
;
1789 char **arglist
, *program
;
1792 printf (" %sFcc %s: ", msgstate
== RESENT
? "Resent-" : "", folder
);
1795 for (i
= 0; (child_id
= fork ()) == NOTOK
&& i
< 5; i
++)
1801 fprintf (stderr
, " %sFcc %s: ",
1802 msgstate
== RESENT
? "Resent-" : "", folder
);
1803 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
1807 /* see if we need to add `+' */
1808 snprintf (fold
, sizeof(fold
), "%s%s",
1809 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
1811 /* now exec the fileproc */
1813 arglist
= argsplit(fileproc
, &program
, &argp
);
1814 arglist
[argp
++] = "-link";
1815 arglist
[argp
++] = "-file";
1816 arglist
[argp
++] = file
;
1817 arglist
[argp
++] = fold
;
1818 arglist
[argp
] = NULL
;
1819 execvp (program
, arglist
);
1823 if ((status
= pidwait (child_id
, OK
))) {
1825 fprintf (stderr
, " %sFcc %s: ",
1826 msgstate
== RESENT
? "Resent-" : "", folder
);
1827 pidstatus (status
, verbose
? stdout
: stderr
, NULL
);
1830 printf ("folder ok\n");
1842 die (char *what
, char *fmt
, ...)
1846 (void) m_unlink (tmpfil
);
1847 if (msgflags
& MINV
)
1848 (void) m_unlink (bccfil
);
1850 if (!whomsw
|| checksw
)
1854 advertise (what
, NULL
, fmt
, ap
);