]>
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("queued", -6, QUEUESW) \
80 X("sasl", SASLminc(-4), SASLSW) \
81 X("nosasl", SASLminc(-6), NOSASLSW) \
82 X("saslmaxssf", SASLminc(-10), SASLMXSSFSW) \
83 X("saslmech", SASLminc(-5), SASLMECHSW) \
84 X("user", SASLminc(-4), USERSW) \
85 X("port server 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("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
="smtp"; /* Name of server port for SMTP */
205 static int tls
=0; /* 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 signature
[BUFSIZ
]; /* my signature */
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 extern struct smtp sm_reply
;
244 static char prefix
[] = "----- =_aaaaaaaaaa";
246 static char *partno
= NULL
;
247 static int queued
= 0;
252 static void putfmt (char *, char *, FILE *);
253 static void start_headers (void);
254 static void finish_headers (FILE *);
255 static int get_header (char *, struct headers
*);
256 static int putadr (char *, char *, struct mailname
*, FILE *, unsigned int);
257 static void putgrp (char *, char *, FILE *, unsigned int);
258 static int insert (struct mailname
*);
259 static void pl (void);
260 static void anno (void);
261 static int annoaux (struct mailname
*);
262 static void insert_fcc (struct headers
*, char *);
263 static void make_bcc_file (int);
264 static void verify_all_addresses (int, char *);
265 static void chkadr (void);
266 static void sigon (void);
267 static void sigoff (void);
268 static void p_refile (char *);
269 static void fcc (char *, char *);
270 static void die (char *, char *, ...);
271 static void post (char *, int, int, char *);
272 static void do_text (char *file
, int fd
);
273 static void do_an_address (struct mailname
*, int);
274 static void do_addresses (int, int);
275 static int find_prefix (void);
279 main (int argc
, char **argv
)
281 int state
, compnum
, dashstuff
= 0;
282 char *cp
, *msg
= NULL
, **argp
, **arguments
, *envelope
;
283 char buf
[BUFSIZ
], name
[NAMESZ
];
285 m_getfld_state_t gstate
= 0;
288 setlocale(LC_ALL
, "");
290 invo_name
= r1bindex (argv
[0], '/');
292 /* foil search of user profile/context */
293 if (context_foil (NULL
) == -1)
296 mts_init (invo_name
);
297 arguments
= getarguments (invo_name
, argc
, argv
, 0);
300 while ((cp
= *argp
++)) {
302 switch (smatch (++cp
, switches
)) {
304 ambigsw (cp
, switches
);
307 adios (NULL
, "-%s unknown", cp
);
310 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
311 print_help (buf
, switches
, 0);
314 print_version(invo_name
);
318 if (!(cp
= *argp
++) || *cp
== '-')
319 adios (NULL
, "missing argument to %s", argp
[-2]);
320 /* create a minimal context */
321 if (context_foil (cp
) == -1)
326 if (!(cp
= *argp
++) || *cp
== '-')
327 adios (NULL
, "missing argument to %s", argp
[-2]);
328 if ((state
= alias (cp
)) != AK_OK
)
329 adios (NULL
, "aliasing error in %s - %s",
330 cp
, akerror (state
));
349 if (!(filter
= *argp
++) || *filter
== '-')
350 adios (NULL
, "missing argument to %s", argp
[-2]);
405 if (!(cp
= *argp
++) || *cp
== '-')
406 adios (NULL
, "missing argument to %s", argp
[-2]);
407 if ((outputlinelen
= atoi (cp
)) < 10)
408 adios (NULL
, "impossible width %d", outputlinelen
);
412 if (!(cp
= *argp
++) || *cp
== '-')
413 adios (NULL
, "missing argument to %s", argp
[-2]);
414 if ((pfd
= atoi (cp
)) <= 2)
415 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
419 if (!(clientsw
= *argp
++) || *clientsw
== '-')
420 adios (NULL
, "missing argument to %s", argp
[-2]);
423 if (!(serversw
= *argp
++) || *serversw
== '-')
424 adios (NULL
, "missing argument to %s", argp
[-2]);
431 if (!(partno
= *argp
++) || *partno
== '-')
432 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]);
459 if (!(user
= *argp
++) || *user
== '-')
460 adios (NULL
, "missing argument to %s", argp
[-2]);
464 if (!(port
= *argp
++) || *port
== '-')
465 adios (NULL
, "missing argument to %s", argp
[-2]);
481 if (!(cp
= *argp
++) || *cp
== '-')
482 adios (NULL
, "missing argument to %s", argp
[-2]);
487 if (!(cp
= *argp
++) || *cp
== '-')
488 adios (NULL
, "missing argument to %s", argp
[-2]);
493 if (!(cp
= *argp
++) || *cp
== '-')
494 adios (NULL
, "missing argument to %s", argp
[-2]);
495 save_mts_method (cp
);
498 case CREDENTIALSSW
: {
499 if (!(cp
= *argp
++) || *cp
== '-')
500 adios (NULL
, "missing argument to %s", argp
[-2]);
501 add_profile_entry ("credentials", cp
);
506 if (!(cp
= *argp
++) || *cp
== '-')
507 adios (NULL
, "missing argument to %s", argp
[-2]);
508 if (save_message_id_style (cp
) != 0)
509 adios (NULL
, "unsupported messageid \"%s\"", cp
);
514 adios (NULL
, "only one message at a time!");
522 adios (NULL
, "usage: %s [switches] file", invo_name
);
524 if (outputlinelen
< 10)
525 adios (NULL
, "impossible width %d", outputlinelen
);
527 if ((in
= fopen (msg
, "r")) == NULL
)
528 adios (msg
, "unable to open");
536 if ((out
= fopen ("/dev/null", "w")) == NULL
)
537 adios ("/dev/null", "unable to open");
539 char *cp
= m_mktemp(m_maildir(invo_name
), NULL
, &out
);
541 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
543 adios ("post", "unable to create temporary file");
546 strncpy(tmpfil
, cp
, sizeof(tmpfil
));
547 chmod (tmpfil
, 0600);
551 hdrtab
= msgstate
== NORMAL
? NHeaders
: RHeaders
;
553 for (compnum
= 1;;) {
554 int bufsz
= sizeof buf
;
555 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
559 cp
= add (buf
, NULL
);
560 while (state
== FLDPLUS
) {
562 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
565 putfmt (name
, cp
, out
);
570 finish_headers (out
);
573 fprintf (out
, "\n%s", buf
);
574 while (state
== BODY
) {
576 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
582 finish_headers (out
);
587 adios (NULL
, "message format error in component #%d", compnum
);
590 adios (NULL
, "getfld() returned %d", state
);
594 m_getfld_state_destroy (&gstate
);
608 * Here's how we decide which address to use as the envelope-from
611 * - If we were given an Envelope-From header, use that.
612 * - If we were given a Sender: address, use that.
613 * - Otherwise, use the address on the From: line
616 if (msgflags
& MEFM
) {
618 } else if (seensender
) {
624 /* If we are doing a "whom" check */
626 /* This won't work with MTS_SENDMAIL_PIPE. */
627 verify_all_addresses (1, envelope
);
631 if (msgflags
& MINV
) {
632 make_bcc_file (dashstuff
);
633 if (msgflags
& MVIS
) {
634 if (sm_mts
!= MTS_SENDMAIL_PIPE
) {
635 /* It would be nice to have support to call
636 verify_all_addresses with MTS_SENDMAIL_PIPE, but
637 that might require running sendmail as root. Note
638 that spost didn't verify addresses. */
639 verify_all_addresses (verbose
, envelope
);
641 post (tmpfil
, 0, verbose
, envelope
);
643 post (bccfil
, 1, verbose
, envelope
);
646 post (tmpfil
, 0, isatty (1), envelope
);
653 printf (partno
? "Partial Message #%s Processed\n" : "Message Processed\n",
665 putfmt (char *name
, char *str
, FILE *out
)
667 int count
, grp
, i
, keep
;
670 struct mailname
*mp
= NULL
, *np
= NULL
;
673 while (*str
== ' ' || *str
== '\t')
676 if (msgstate
== NORMAL
&& uprf (name
, "resent")) {
677 advise (NULL
, "illegal header line -- %s:", name
);
682 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
683 if (strncasecmp (name
, "nmh-", 4)) {
684 fprintf (out
, "%s: %s", name
, str
);
686 /* Filter out all Nmh-* headers, because Norm asked. They
687 should never have reached this point. Warn about any
688 that are non-empty. */
689 if (strcmp (str
, "\n")) {
690 char *newline
= strchr (str
, '\n');
691 if (newline
) *newline
= '\0';
693 advise (NULL
, "ignoring header line -- %s: %s", name
, str
);
702 if (hdr
->flags
& HIGN
) {
705 if (hdr
->flags
& HBAD
) {
706 advise (NULL
, "illegal header line -- %s:", name
);
710 msgflags
|= (hdr
->set
& ~(MVIS
| MINV
));
712 if (hdr
->flags
& HSUB
)
713 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
714 if (hdr
->flags
& HFCC
) {
715 if ((cp
= strrchr(str
, '\n')))
717 for (cp
= pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
719 insert_fcc (hdr
, pp
);
721 insert_fcc (hdr
, pp
);
725 if (!(hdr
->flags
& HADR
)) {
726 fprintf (out
, "%s: %s", name
, str
);
730 tmpaddrs
.m_next
= NULL
;
731 for (count
= 0; (cp
= getname (str
)); count
++)
732 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
736 tmpaddrs
.m_next
= mp
;
740 if (hdr
->flags
& HTRY
)
746 if (hdr
->flags
& HNIL
)
747 fprintf (out
, "%s: %s", name
, str
);
750 * Sender (or Resent-Sender) can have only one address
752 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
753 : (hdr
->set
& MSND
)) {
754 advise (NULL
, "%s: field requires one address", name
);
758 advise (NULL
, "%s: field requires at least one address", name
);
765 if (count
> 1 && (hdr
->flags
& HONE
)) {
766 advise (NULL
, "%s: field only permits one address", name
);
771 nameoutput
= linepos
= 0;
772 snprintf (namep
, sizeof(namep
), "%s%s",
773 (hdr
->flags
& HMNG
) ? "Original-" : "", name
);
775 for (grp
= 0, mp
= tmpaddrs
.m_next
; mp
; mp
= np
)
776 if (mp
->m_nohost
) { /* also used to test (hdr->flags & HTRY) */
777 /* The address doesn't include a host, so it might be an alias. */
778 pp
= akvalue (mp
->m_mbox
); /* do mh alias substitution */
779 qp
= akvisible () ? mp
->m_mbox
: "";
782 putgrp (namep
, np
->m_gname
, out
, hdr
->flags
);
783 while ((cp
= getname (pp
))) {
784 if (!(mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
790 * If it's a From: or Resent-From: header, save the address
791 * for later possible use (as the envelope address for SMTP)
794 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
795 : (hdr
->set
& MFRM
)) {
796 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
797 from
[sizeof(from
) - 1] = '\0';
802 * Also save the Sender: or Resent-Sender: header as well
805 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
806 : (hdr
->set
& MSND
)) {
807 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
808 sender
[sizeof(sender
) - 1] = '\0';
813 * ALSO ... save Envelope-From
816 if (hdr
->set
& MEFM
) {
817 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
818 efrom
[sizeof(efrom
) - 1] = '\0';
821 if (hdr
->flags
& HBCC
)
824 mp
->m_ingrp
= np
->m_ingrp
;
827 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
829 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
830 /* Catch this before sendmail chokes with:
831 "553 List:; syntax illegal for recipient
833 If we wanted to, we could expand out blind
834 aliases and put them in Bcc:, but then
835 they'd have the Blind-Carbon-Copy
838 "blind lists not compatible with"
844 if (putadr (namep
, qp
, mp
, out
, hdr
->flags
))
845 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
854 /* Address includes a host, so no alias substitution is needed. */
857 * If it's a From: or Resent-From header, save the address
858 * for later possible use (as the envelope address for SMTP)
861 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
862 : (hdr
->set
& MFRM
)) {
863 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
868 * Also save the Sender: header as well
871 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
872 : (hdr
->set
& MSND
)) {
873 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
874 sender
[sizeof(sender
) - 1] = '\0';
879 * ALSO ... save Envelope-From
882 if (hdr
->set
& MEFM
) {
883 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
884 efrom
[sizeof(efrom
) - 1] = '\0';
887 if (hdr
->flags
& HBCC
)
890 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
893 keep
= putadr (namep
, "", mp
, out
, hdr
->flags
);
897 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
904 * If this is a From:/Resent-From: header, save the full thing for
905 * later in case we need it for use when constructing a Bcc draft message
908 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
) : (hdr
->set
& MFRM
)) {
909 strncpy(fullfrom
, str
, sizeof(fullfrom
));
910 fullfrom
[sizeof(fullfrom
) - 1] = 0;
912 * Strip off any trailing newlines
915 while (strlen(fullfrom
) > 0 && fullfrom
[strlen(fullfrom
) - 1] == '\n') {
916 fullfrom
[strlen(fullfrom
) - 1] = '\0';
920 if (grp
> 0 && (hdr
->flags
& HNGR
)) {
921 advise (NULL
, "%s: field does not allow groups", name
);
933 char *cp
, sigbuf
[BUFSIZ
];
939 * Probably not necessary, but just in case ...
947 if ((cp
= getfullname ()) && *cp
) {
948 strncpy (sigbuf
, cp
, sizeof(sigbuf
));
949 snprintf (signature
, sizeof(signature
), "%s <%s>",
950 sigbuf
, getlocaladdr());
951 if ((cp
= getname (signature
)) == NULL
)
952 adios (NULL
, "getname () failed -- you lose extraordinarily big");
953 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
)) == NULL
)
954 adios (NULL
, "bad signature '%s'", sigbuf
);
959 strncpy (signature
, getlocaladdr(), sizeof(signature
));
965 * Now that we've outputted the header fields in the draft
966 * message, we will now output any remaining header fields
967 * that we need to add/create.
971 finish_headers (FILE *out
)
975 if (!(msgflags
& MFRM
)) {
977 * A From: header is now required in the draft.
979 advise (NULL
, "message has no From: header");
980 advise (NULL
, "See default components files for examples");
985 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
986 advise (NULL
, "A Sender: or Envelope-From: header is required "
987 "with multiple\nFrom: addresses");
995 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
997 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
999 * If we have multiple From: addresses, make sure we have an
1000 * Sender: header. If we don't have one, then generate one
1001 * from Envelope-From: (which in this case, cannot be blank)
1004 if (fromcount
> 1 && seensender
== 0) {
1005 if (efrom
[0] == '\0') {
1006 advise (NULL
, "Envelope-From cannot be blank when there "
1007 "is multiple From: addresses\nand no Sender: "
1011 fprintf (out
, "Sender: %s\n", efrom
);
1015 if (!(msgflags
& MVIS
))
1016 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
1020 if (!(msgflags
& MDAT
)) {
1021 advise (NULL
, "message has no Date: header");
1024 if (!(msgflags
& MFRM
)) {
1025 advise (NULL
, "message has no From: header");
1028 if (!(msgflags
& MRFM
)) {
1029 advise (NULL
, "message has no Resent-From: header");
1030 advise (NULL
, "See default components files for examples");
1034 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
1035 advise (NULL
, "A Resent-Sender: or Envelope-From: header is "
1036 "required with multiple\nResent-From: addresses");
1044 fprintf (out
, "Resent-Date: %s\n", dtime (&tclock
, 0));
1046 fprintf (out
, "Resent-Message-ID: %s\n",
1047 message_id (tclock
, 0));
1049 * If we have multiple Resent-From: addresses, make sure we have an
1050 * Resent-Sender: header. If we don't have one, then generate one
1051 * from Envelope-From (which in this case, cannot be blank)
1054 if (fromcount
> 1 && seensender
== 0) {
1055 if (efrom
[0] == '\0') {
1056 advise (NULL
, "Envelope-From cannot be blank when there "
1057 "is multiple Resent-From: addresses and no "
1058 "Resent-Sender: header");
1061 fprintf (out
, "Resent-Sender: %s\n", efrom
);
1065 if (!(msgflags
& MVIS
))
1066 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
1071 adios (NULL
, "re-format message and try again");
1073 adios (NULL
, "no addressees");
1078 get_header (char *header
, struct headers
*table
)
1082 for (h
= table
; h
->value
; h
++)
1083 if (!strcasecmp (header
? header
: "", h
->value
? h
->value
: ""))
1091 putadr (char *name
, char *aka
, struct mailname
*mp
, FILE *out
, unsigned int flags
)
1095 char buffer
[BUFSIZ
];
1097 if (mp
->m_mbox
== NULL
|| ((flags
& HTRY
) && !insert (mp
)))
1099 if (sm_mts
!= MTS_SENDMAIL_PIPE
&&
1100 ((flags
& (HBCC
| HDCC
| HEFM
)) || mp
->m_ingrp
))
1104 fprintf (out
, "%s: ", name
);
1105 linepos
+= (nameoutput
= strlen (name
) + 2);
1108 if (*aka
&& mp
->m_type
!= UUCPHOST
&& !mp
->m_pers
)
1109 mp
->m_pers
= getcpy (aka
);
1112 snprintf (buffer
, sizeof(buffer
), "%s;", mp
->m_gname
);
1115 cp
= adrformat (mp
);
1122 if (linepos
!= nameoutput
) {
1123 if (len
+ linepos
+ 2 > outputlinelen
)
1124 fprintf (out
, ",\n%*s", linepos
= nameoutput
, "");
1134 return (flags
& HTRY
);
1139 putgrp (char *name
, char *group
, FILE *out
, unsigned int flags
)
1144 if (sm_mts
!= MTS_SENDMAIL_PIPE
&& (flags
& HBCC
))
1148 fprintf (out
, "%s: ", name
);
1149 linepos
+= (nameoutput
= strlen (name
) + 2);
1152 cp
= concat (group
, ";", NULL
);
1155 if (linepos
> nameoutput
) {
1156 if (len
+ linepos
+ 2 > outputlinelen
) {
1157 fprintf (out
, ",\n%*s", nameoutput
, "");
1158 linepos
= nameoutput
;
1172 insert (struct mailname
*np
)
1174 struct mailname
*mp
;
1176 if (np
->m_mbox
== NULL
)
1179 for (mp
= np
->m_type
== LOCALHOST
? &localaddrs
1180 : np
->m_type
== UUCPHOST
? &uuaddrs
1184 if (!strcasecmp (np
->m_host
? np
->m_host
: "",
1185 mp
->m_next
->m_host
? mp
->m_next
->m_host
: "") &&
1186 !strcasecmp (np
->m_mbox
? np
->m_mbox
: "",
1187 mp
->m_next
->m_mbox
? mp
->m_next
->m_mbox
: "") &&
1188 np
->m_bcc
== mp
->m_next
->m_bcc
)
1201 struct mailname
*mp
;
1203 printf ("-------\n\t-- Addresses --\nlocal:\t");
1204 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1205 printf ("%s%s%s", mp
->m_mbox
,
1206 mp
->m_bcc
? "[BCC]" : "",
1207 mp
->m_next
? ",\n\t" : "");
1209 printf ("\nnet:\t");
1210 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1211 printf ("%s%s@%s%s%s", mp
->m_path
? mp
->m_path
: "",
1212 mp
->m_mbox
, mp
->m_host
,
1213 mp
->m_bcc
? "[BCC]" : "",
1214 mp
->m_next
? ",\n\t" : "");
1216 printf ("\nuucp:\t");
1217 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1218 printf ("%s!%s%s%s", mp
->m_host
, mp
->m_mbox
,
1219 mp
->m_bcc
? "[BCC]" : "",
1220 mp
->m_next
? ",\n\t" : "");
1222 printf ("\n\t-- Folder Copies --\nfcc:\t");
1223 for (i
= 0; i
< fccind
; i
++)
1224 printf ("%s%s", fccfold
[i
], i
+ 1 < fccind
? ",\n\t" : "");
1232 struct mailname
*mp
;
1234 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1235 if (annoaux (mp
) == NOTOK
)
1238 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1239 if (annoaux (mp
) == NOTOK
)
1242 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1243 if (annoaux (mp
) == NOTOK
)
1253 annoaux (struct mailname
*mp
)
1256 char buffer
[BUFSIZ
];
1258 snprintf (buffer
, sizeof(buffer
), "%s\n", adrformat (mp
));
1259 i
= strlen (buffer
);
1261 return (write (pfd
, buffer
, i
) == i
? OK
: NOTOK
);
1266 insert_fcc (struct headers
*hdr
, char *pp
)
1270 for (cp
= pp
; isspace ((unsigned char) *cp
); cp
++)
1272 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace ((unsigned char) *pp
); pp
--)
1280 adios (NULL
, "too many %ss", hdr
->value
);
1281 fccfold
[fccind
++] = getcpy (cp
);
1289 make_bcc_file (int dashstuff
)
1295 char *tfile
= NULL
, *program
;
1297 tfile
= m_mktemp2(NULL
, "bccs", NULL
, &out
);
1298 if (tfile
== NULL
) adios("bcc", "unable to create temporary file");
1299 strncpy (bccfil
, tfile
, sizeof(bccfil
));
1301 fprintf (out
, "From: %s\n", fullfrom
);
1302 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1304 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
1306 fprintf (out
, "Subject: %s", subject
);
1307 fprintf (out
, "BCC:\n");
1310 * Use MIME encapsulation for Bcc messages
1316 * Check if any lines in the message clash with the
1317 * prefix for the MIME multipart separator. If there
1318 * is a clash, increment one of the letters in the
1319 * prefix and check again.
1321 if ((cp
= strchr(prefix
, 'a')) == NULL
)
1322 adios (NULL
, "lost prefix start");
1323 while (find_prefix () == NOTOK
) {
1328 adios (NULL
, "can't find a unique delimiter string");
1333 fprintf (out
, "%s: %s\n%s: multipart/digest; boundary=\"",
1334 VRSN_FIELD
, VRSN_VALUE
, TYPE_FIELD
);
1335 fprintf (out
, "%s\"\n\n--%s\n\n", prefix
, prefix
);
1337 fprintf (out
, "\n------- Blind-Carbon-Copy\n\n");
1343 * Do mhl filtering of Bcc messages instead
1344 * of MIME encapsulation.
1346 if (filter
!= NULL
) {
1347 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1351 adios ("fork", "unable to");
1354 dup2 (fileno (out
), 1);
1356 vec
= argsplit(mhlproc
, &program
, &i
);
1357 vec
[i
++] = "-forward";
1362 /* was the flag -[no]dashstuffing specified? */
1364 vec
[i
++] = "-dashstuffing";
1365 else if (dashstuff
< 0)
1366 vec
[i
++] = "-nodashstuffing";
1369 execvp (program
, vec
);
1370 fprintf (stderr
, "unable to exec ");
1375 pidXwait (child_id
, mhlproc
);
1379 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
1380 adios (tmpfil
, "unable to re-open");
1383 * If using MIME encapsulation, or if the -nodashstuffing
1384 * flag was given, then just copy message. Else do
1385 * RFC934 quoting (dashstuffing).
1387 if (mime
|| dashstuff
< 0)
1388 cpydata (fd
, fileno (out
), tmpfil
, bccfil
);
1390 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
1394 fseek (out
, 0L, SEEK_END
);
1396 fprintf (out
, "\n--%s--\n", prefix
);
1398 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
1404 * Scan message to check if any lines clash with
1405 * the prefix of the MIME multipart separator.
1412 char buffer
[BUFSIZ
];
1415 if ((in
= fopen (tmpfil
, "r")) == NULL
)
1416 adios (tmpfil
, "unable to re-open");
1418 while (fgets (buffer
, sizeof(buffer
) - 1, in
))
1419 if (buffer
[0] == '-' && buffer
[1] == '-') {
1422 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1423 if (!isspace ((unsigned char) *cp
))
1426 if (strcmp (buffer
+ 2, prefix
) == 0) {
1437 #define plural(x) (x == 1 ? "" : "s")
1442 if (badadr
&& unkadr
)
1443 die (NULL
, "%d address%s unparsable, %d addressee%s undeliverable",
1444 badadr
, plural (badadr
), unkadr
, plural (badadr
));
1446 die (NULL
, "%d address%s unparsable", badadr
, plural (badadr
));
1448 die (NULL
, "%d addressee%s undeliverable", unkadr
, plural (unkadr
));
1453 do_addresses (int bccque
, int talk
)
1457 struct mailname
*lp
;
1460 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1461 if (lp
->m_bcc
? bccque
: !bccque
) {
1463 printf (" -- Local Recipients --\n");
1464 do_an_address (lp
, talk
);
1469 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1470 if (lp
->m_bcc
? bccque
: !bccque
) {
1472 printf (" -- UUCP Recipients --\n");
1473 do_an_address (lp
, talk
);
1478 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1479 if (lp
->m_bcc
? bccque
: !bccque
) {
1481 printf (" -- Network Recipients --\n");
1482 do_an_address (lp
, talk
);
1488 if (rp_isbad (retval
= sm_waend ()))
1489 die (NULL
, "problem ending addresses; %s", rp_string (retval
));
1494 * MTS-SPECIFIC INTERACTION
1499 * SENDMAIL/SMTP routines
1503 post (char *file
, int bccque
, int talk
, char *envelope
)
1510 if (msgflags
& MINV
)
1511 printf (" -- Posting for %s Recipients --\n",
1512 bccque
? "Blind" : "Sighted");
1514 printf (" -- Posting for All Recipients --\n");
1519 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
1520 char **argp
, *program
;
1523 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1527 adios ("fork", "unable to");
1530 if (freopen( file
, "r", stdin
) == NULL
) {
1531 adios (file
, "can't reopen for sendmail");
1534 argp
= argsplit(sendmail
, &program
, &argc
);
1535 argp
[argc
++] = "-t"; /* read msg for recipients */
1536 argp
[argc
++] = "-i"; /* don't stop on "." */
1538 argp
[argc
++] = "-bv";
1540 argp
[argc
++] = "-v";
1543 execv (program
, argp
);
1544 adios (sendmail
, "can't exec");
1547 pidXwait (child_id
, NULL
);
1551 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1552 verbose
, snoop
, queued
, sasl
,
1553 saslssf
, saslmech
, user
, tls
)) ||
1554 rp_isbad (retval
= sm_winit (envelope
)))
1555 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1557 do_addresses (bccque
, talk
&& verbose
);
1558 if ((fd
= open (file
, O_RDONLY
)) == NOTOK
)
1559 die (file
, "unable to re-open");
1564 sm_end (!(msgflags
& MINV
) || bccque
? OK
: DONE
);
1568 if (msgflags
& MINV
)
1569 printf (" -- %s Recipient Copies Posted --\n",
1570 bccque
? "Blind" : "Sighted");
1572 printf (" -- Recipient Copies Posted --\n");
1580 /* Address Verification */
1583 verify_all_addresses (int talk
, char *envelope
)
1586 struct mailname
*lp
;
1590 if (!whomsw
|| checksw
)
1591 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1592 verbose
, snoop
, queued
, sasl
,
1593 saslssf
, saslmech
, user
, tls
))
1594 || rp_isbad (retval
= sm_winit (envelope
)))
1595 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1597 if (talk
&& !whomsw
)
1598 printf (" -- Address Verification --\n");
1599 if (talk
&& localaddrs
.m_next
)
1600 printf (" -- Local Recipients --\n");
1601 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1602 do_an_address (lp
, talk
);
1604 if (talk
&& uuaddrs
.m_next
)
1605 printf (" -- UUCP Recipients --\n");
1606 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1607 do_an_address (lp
, talk
);
1609 if (talk
&& netaddrs
.m_next
)
1610 printf (" -- Network Recipients --\n");
1611 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1612 do_an_address (lp
, talk
);
1615 if (talk
&& !whomsw
)
1616 printf (" -- Address Verification Successful --\n");
1618 if (!whomsw
|| checksw
)
1627 do_an_address (struct mailname
*lp
, int talk
)
1633 switch (lp
->m_type
) {
1637 strncpy (addr
, mbox
, sizeof(addr
));
1641 mbox
= auxformat (lp
, 0);
1643 snprintf (addr
, sizeof(addr
), "%s!%s", lp
->m_host
, lp
->m_mbox
);
1646 default: /* let SendMail decide if the host is bad */
1649 snprintf (addr
, sizeof(addr
), "%s at %s", mbox
, host
);
1654 printf (" %s%s", addr
, whomsw
&& lp
->m_bcc
? "[BCC]" : "");
1656 if (whomsw
&& !checksw
) {
1664 switch (retval
= sm_wadr (mbox
, host
,
1665 lp
->m_type
!= UUCPHOST
? lp
->m_path
: NULL
)) {
1668 printf ("address ok\n");
1674 fprintf (stderr
, " %s: ", addr
);
1675 fprintf (talk
? stdout
: stderr
, "loses; %s\n",
1676 rp_string (retval
));
1682 fprintf (stderr
, " %s: ", addr
);
1683 die (NULL
, "unexpected response; %s", rp_string (retval
));
1691 do_text (char *file
, int fd
)
1696 lseek (fd
, (off_t
) 0, SEEK_SET
);
1698 while ((state
= read (fd
, buf
, sizeof(buf
))) > 0) {
1699 if (rp_isbad (retval
= sm_wtxt (buf
, state
)))
1700 die (NULL
, "problem writing text; %s\n", rp_string (retval
));
1704 die (file
, "problem reading from");
1706 switch (retval
= sm_wtend ()) {
1712 die (NULL
, "posting failed; %s", rp_string (retval
));
1715 die (NULL
, "unexpected response; %s", rp_string (retval
));
1730 if (msgflags
& MINV
)
1733 if (!whomsw
|| checksw
)
1746 hstat
= SIGNAL2 (SIGHUP
, sigser
);
1747 istat
= SIGNAL2 (SIGINT
, sigser
);
1748 qstat
= SIGNAL2 (SIGQUIT
, sigser
);
1749 tstat
= SIGNAL2 (SIGTERM
, sigser
);
1759 SIGNAL (SIGHUP
, hstat
);
1760 SIGNAL (SIGINT
, istat
);
1761 SIGNAL (SIGQUIT
, qstat
);
1762 SIGNAL (SIGTERM
, tstat
);
1770 p_refile (char *file
)
1778 printf (" -- Filing Folder Copies --\n");
1779 for (i
= 0; i
< fccind
; i
++)
1780 fcc (file
, fccfold
[i
]);
1782 printf (" -- Folder Copies Filed --\n");
1787 * Call the `fileproc' to add the file to the folder.
1791 fcc (char *file
, char *folder
)
1794 int i
, status
, argp
;
1796 char **arglist
, *program
;
1799 printf (" %sFcc %s: ", msgstate
== RESENT
? "Resent-" : "", folder
);
1802 for (i
= 0; (child_id
= fork ()) == NOTOK
&& i
< 5; i
++)
1808 fprintf (stderr
, " %sFcc %s: ",
1809 msgstate
== RESENT
? "Resent-" : "", folder
);
1810 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
1814 /* see if we need to add `+' */
1815 snprintf (fold
, sizeof(fold
), "%s%s",
1816 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
1818 /* now exec the fileproc */
1820 arglist
= argsplit(fileproc
, &program
, &argp
);
1821 arglist
[argp
++] = "-link";
1822 arglist
[argp
++] = "-file";
1823 arglist
[argp
++] = file
;
1824 arglist
[argp
++] = fold
;
1825 arglist
[argp
] = NULL
;
1826 execvp (program
, arglist
);
1830 if ((status
= pidwait (child_id
, OK
))) {
1832 fprintf (stderr
, " %sFcc %s: ",
1833 msgstate
== RESENT
? "Resent-" : "", folder
);
1834 pidstatus (status
, verbose
? stdout
: stderr
, NULL
);
1837 printf ("folder ok\n");
1849 die (char *what
, char *fmt
, ...)
1854 if (msgflags
& MINV
)
1857 if (!whomsw
|| checksw
)
1861 advertise (what
, NULL
, fmt
, ap
);