]>
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("messageid localname|random", 2, MESSAGEIDSW) \
94 #define X(sw, minchars, id) id,
95 DEFINE_SWITCH_ENUM(POST
);
98 #define X(sw, minchars, id) { sw, minchars, id },
99 DEFINE_SWITCH_ARRAY(POST
, switches
);
110 * flags for headers->flags
112 #define HNOP 0x0000 /* just used to keep .set around */
113 #define HBAD 0x0001 /* bad header - don't let it through */
114 #define HADR 0x0002 /* header has an address field */
115 #define HSUB 0x0004 /* Subject: header */
116 #define HTRY 0x0008 /* try to send to addrs on header */
117 #define HBCC 0x0010 /* don't output this header, unless MTS_SENDMAIL_PIPE */
118 #define HMNG 0x0020 /* munge this header */
119 #define HNGR 0x0040 /* no groups allowed in this header */
120 #define HFCC 0x0080 /* FCC: type header */
121 #define HNIL 0x0100 /* okay for this header not to have addrs */
122 #define HIGN 0x0200 /* ignore this header */
123 #define HDCC 0x0400 /* another undocumented feature */
124 #define HONE 0x0800 /* Only (zero or) one address allowed */
125 #define HEFM 0x1000 /* Envelope-From: header */
128 * flags for headers->set
130 #define MFRM 0x0001 /* we've seen a From: */
131 #define MDAT 0x0002 /* we've seen a Date: */
132 #define MRFM 0x0004 /* we've seen a Resent-From: */
133 #define MVIS 0x0008 /* we've seen sighted addrs */
134 #define MINV 0x0010 /* we've seen blind addrs */
135 #define MSND 0x0020 /* we've seen a Sender: */
136 #define MRSN 0x0040 /* We've seen a Resent-Sendr:*/
137 #define MEFM 0x0080 /* We've seen Envelope-From: */
140 static struct headers NHeaders
[] = {
141 { "Return-Path", HBAD
, 0 },
142 { "Received", HBAD
, 0 },
143 { "Reply-To", HADR
|HNGR
, 0 },
144 { "From", HADR
|HNGR
, MFRM
},
145 { "Sender", HADR
|HNGR
|HONE
, MSND
},
147 { "Subject", HSUB
, 0 },
148 { "To", HADR
|HTRY
, MVIS
},
149 { "cc", HADR
|HTRY
, MVIS
},
150 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, MINV
},
151 { "Dcc", HADR
|HTRY
|HDCC
|HNIL
, MVIS
}, /* sorta cc & bcc combined */
152 { "Message-ID", HBAD
, 0 },
154 { "Envelope-From", HADR
|HONE
|HEFM
, MEFM
},
158 static struct headers RHeaders
[] = {
159 { "Resent-Reply-To", HADR
|HNGR
, 0 },
160 { "Resent-From", HADR
|HNGR
, MRFM
},
161 { "Resent-Sender", HADR
|HNGR
, MRSN
},
162 { "Resent-Date", HBAD
, 0 },
163 { "Resent-Subject", HSUB
, 0 },
164 { "Resent-To", HADR
|HTRY
, MVIS
},
165 { "Resent-cc", HADR
|HTRY
, MVIS
},
166 { "Resent-Bcc", HADR
|HTRY
|HBCC
, MINV
},
167 { "Resent-Message-ID", HBAD
, 0 },
168 { "Resent-Fcc", HFCC
, 0 },
169 { "Reply-To", HADR
, 0 },
170 { "From", HADR
|HNGR
, MFRM
},
171 { "Sender", HADR
|HNGR
, MSND
},
172 { "Date", HNOP
, MDAT
},
173 { "To", HADR
|HNIL
, 0 },
174 { "cc", HADR
|HNIL
, 0 },
175 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, 0 },
177 { "Envelope-From", HADR
|HONE
|HEFM
, MEFM
},
181 static short fccind
= 0; /* index into fccfold[] */
182 static short outputlinelen
= OUTPUTLINELEN
;
184 static int pfd
= NOTOK
; /* fd to write annotation list to */
185 static int recipients
= 0; /* how many people will get a copy */
186 static int unkadr
= 0; /* how many of those were unknown */
187 static int badadr
= 0; /* number of bad addrs */
188 static int badmsg
= 0; /* message has bad semantics */
189 static int verbose
= 0; /* spell it out */
190 static int format
= 1; /* format addresses */
191 static int mime
= 0; /* use MIME-style encapsulations for Bcc */
192 static int msgid
= 0; /* add msgid */
193 static int debug
= 0; /* debugging post */
194 static int watch
= 0; /* watch the delivery process */
195 static int whomsw
= 0; /* we are whom not post */
196 static int checksw
= 0; /* whom -check */
197 static int linepos
=0; /* putadr()'s position on the line */
198 static int nameoutput
=0; /* putadr() has output header name */
199 static int sasl
=0; /* Use SASL auth for SMTP */
200 static int saslssf
=-1; /* Our maximum SSF for SASL */
201 static char *saslmech
=NULL
; /* Force use of particular SASL mech */
202 static char *user
=NULL
; /* Authenticate as this user */
203 static char *port
="smtp"; /* Name of server port for SMTP */
204 static int tls
=0; /* Use TLS for encryption */
205 static int fromcount
=0; /* Count of addresses on From: header */
206 static int seensender
=0; /* Have we seen a Sender: header? */
208 static unsigned msgflags
= 0; /* what we've seen */
212 static int msgstate
= NORMAL
;
214 static time_t tclock
= 0; /* the time we started (more or less) */
216 static SIGNAL_HANDLER hstat
, istat
, qstat
, tstat
;
218 static char tmpfil
[BUFSIZ
];
219 static char bccfil
[BUFSIZ
];
221 static char from
[BUFSIZ
]; /* my network address */
222 static char sender
[BUFSIZ
]; /* my Sender: header */
223 static char efrom
[BUFSIZ
]; /* my Envelope-From: header */
224 static char fullfrom
[BUFSIZ
]; /* full contents of From header */
225 static char signature
[BUFSIZ
]; /* my signature */
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 extern struct smtp sm_reply
;
243 static char prefix
[] = "----- =_aaaaaaaaaa";
245 static char *partno
= NULL
;
246 static int queued
= 0;
251 static void putfmt (char *, char *, FILE *);
252 static void start_headers (void);
253 static void finish_headers (FILE *);
254 static int get_header (char *, struct headers
*);
255 static int putadr (char *, char *, struct mailname
*, FILE *, unsigned int);
256 static void putgrp (char *, char *, FILE *, unsigned int);
257 static int insert (struct mailname
*);
258 static void pl (void);
259 static void anno (void);
260 static int annoaux (struct mailname
*);
261 static void insert_fcc (struct headers
*, char *);
262 static void make_bcc_file (int);
263 static void verify_all_addresses (int, char *);
264 static void chkadr (void);
265 static void sigon (void);
266 static void sigoff (void);
267 static void p_refile (char *);
268 static void fcc (char *, char *);
269 static void die (char *, char *, ...);
270 static void post (char *, int, int, char *);
271 static void do_text (char *file
, int fd
);
272 static void do_an_address (struct mailname
*, int);
273 static void do_addresses (int, int);
274 static int find_prefix (void);
278 main (int argc
, char **argv
)
280 int state
, compnum
, dashstuff
= 0;
281 char *cp
, *msg
= NULL
, **argp
, **arguments
, *envelope
;
282 char buf
[BUFSIZ
], name
[NAMESZ
];
284 m_getfld_state_t gstate
= 0;
287 setlocale(LC_ALL
, "");
289 invo_name
= r1bindex (argv
[0], '/');
291 /* foil search of user profile/context */
292 if (context_foil (NULL
) == -1)
295 mts_init (invo_name
);
296 arguments
= getarguments (invo_name
, argc
, argv
, 0);
299 while ((cp
= *argp
++)) {
301 switch (smatch (++cp
, switches
)) {
303 ambigsw (cp
, switches
);
306 adios (NULL
, "-%s unknown", cp
);
309 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
310 print_help (buf
, switches
, 0);
313 print_version(invo_name
);
317 if (!(cp
= *argp
++) || *cp
== '-')
318 adios (NULL
, "missing argument to %s", argp
[-2]);
319 /* create a minimal context */
320 if (context_foil (cp
) == -1)
325 if (!(cp
= *argp
++) || *cp
== '-')
326 adios (NULL
, "missing argument to %s", argp
[-2]);
327 if ((state
= alias (cp
)) != AK_OK
)
328 adios (NULL
, "aliasing error in %s - %s",
329 cp
, akerror (state
));
348 if (!(filter
= *argp
++) || *filter
== '-')
349 adios (NULL
, "missing argument to %s", argp
[-2]);
404 if (!(cp
= *argp
++) || *cp
== '-')
405 adios (NULL
, "missing argument to %s", argp
[-2]);
406 if ((outputlinelen
= atoi (cp
)) < 10)
407 adios (NULL
, "impossible width %d", outputlinelen
);
411 if (!(cp
= *argp
++) || *cp
== '-')
412 adios (NULL
, "missing argument to %s", argp
[-2]);
413 if ((pfd
= atoi (cp
)) <= 2)
414 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
418 if (!(clientsw
= *argp
++) || *clientsw
== '-')
419 adios (NULL
, "missing argument to %s", argp
[-2]);
422 if (!(serversw
= *argp
++) || *serversw
== '-')
423 adios (NULL
, "missing argument to %s", argp
[-2]);
430 if (!(partno
= *argp
++) || *partno
== '-')
431 adios (NULL
, "missing argument to %s", argp
[-2]);
447 if (!(cp
= *argp
++) || *cp
== '-')
448 adios (NULL
, "missing argument to %s", argp
[-2]);
453 if (!(saslmech
= *argp
++) || *saslmech
== '-')
454 adios (NULL
, "missing argument to %s", argp
[-2]);
458 if (!(user
= *argp
++) || *user
== '-')
459 adios (NULL
, "missing argument to %s", argp
[-2]);
463 if (!(port
= *argp
++) || *port
== '-')
464 adios (NULL
, "missing argument to %s", argp
[-2]);
480 if (!(cp
= *argp
++) || *cp
== '-')
481 adios (NULL
, "missing argument to %s", argp
[-2]);
486 if (!(cp
= *argp
++) || *cp
== '-')
487 adios (NULL
, "missing argument to %s", argp
[-2]);
492 if (!(cp
= *argp
++) || *cp
== '-')
493 adios (NULL
, "missing argument to %s", argp
[-2]);
494 save_mts_method (cp
);
498 if (!(cp
= *argp
++) || *cp
== '-')
499 adios (NULL
, "missing argument to %s", argp
[-2]);
500 if (save_message_id_style (cp
) != 0)
501 adios (NULL
, "unsupported messageid \"%s\"", cp
);
506 adios (NULL
, "only one message at a time!");
514 adios (NULL
, "usage: %s [switches] file", invo_name
);
516 if (outputlinelen
< 10)
517 adios (NULL
, "impossible width %d", outputlinelen
);
519 if ((in
= fopen (msg
, "r")) == NULL
)
520 adios (msg
, "unable to open");
528 if ((out
= fopen ("/dev/null", "w")) == NULL
)
529 adios ("/dev/null", "unable to open");
531 char *cp
= m_mktemp(m_maildir(invo_name
), NULL
, &out
);
533 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
535 adios ("post", "unable to create temporary file");
538 strncpy(tmpfil
, cp
, sizeof(tmpfil
));
539 chmod (tmpfil
, 0600);
543 hdrtab
= msgstate
== NORMAL
? NHeaders
: RHeaders
;
545 for (compnum
= 1;;) {
546 int bufsz
= sizeof buf
;
547 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
551 cp
= add (buf
, NULL
);
552 while (state
== FLDPLUS
) {
554 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
557 putfmt (name
, cp
, out
);
562 finish_headers (out
);
565 fprintf (out
, "\n%s", buf
);
566 while (state
== BODY
) {
568 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
574 finish_headers (out
);
579 adios (NULL
, "message format error in component #%d", compnum
);
582 adios (NULL
, "getfld() returned %d", state
);
586 m_getfld_state_destroy (&gstate
);
600 * Here's how we decide which address to use as the envelope-from
603 * - If we were given an Envelope-From header, use that.
604 * - If we were given a Sender: address, use that.
605 * - Otherwise, use the address on the From: line
608 if (msgflags
& MEFM
) {
610 } else if (seensender
) {
616 /* If we are doing a "whom" check */
618 /* This won't work with MTS_SENDMAIL_PIPE. */
619 verify_all_addresses (1, envelope
);
623 if (msgflags
& MINV
) {
624 make_bcc_file (dashstuff
);
625 if (msgflags
& MVIS
) {
626 if (sm_mts
!= MTS_SENDMAIL_PIPE
) {
627 /* It would be nice to have support to call
628 verify_all_addresses with MTS_SENDMAIL_PIPE, but
629 that might require running sendmail as root. Note
630 that spost didn't verify addresses. */
631 verify_all_addresses (verbose
, envelope
);
633 post (tmpfil
, 0, verbose
, envelope
);
635 post (bccfil
, 1, verbose
, envelope
);
638 post (tmpfil
, 0, isatty (1), envelope
);
645 printf (partno
? "Partial Message #%s Processed\n" : "Message Processed\n",
657 putfmt (char *name
, char *str
, FILE *out
)
659 int count
, grp
, i
, keep
;
662 struct mailname
*mp
= NULL
, *np
= NULL
;
665 while (*str
== ' ' || *str
== '\t')
668 if (msgstate
== NORMAL
&& uprf (name
, "resent")) {
669 advise (NULL
, "illegal header line -- %s:", name
);
674 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
675 if (strncasecmp (name
, "nmh-", 4)) {
676 fprintf (out
, "%s: %s", name
, str
);
678 /* Filter out all Nmh-* headers, because Norm asked. They
679 should never have reached this point. Warn about any
680 that are non-empty. */
681 if (strcmp (str
, "\n")) {
682 char *newline
= strchr (str
, '\n');
683 if (newline
) *newline
= '\0';
685 advise (NULL
, "ignoring header line -- %s: %s", name
, str
);
694 if (hdr
->flags
& HIGN
) {
697 if (hdr
->flags
& HBAD
) {
698 advise (NULL
, "illegal header line -- %s:", name
);
702 msgflags
|= (hdr
->set
& ~(MVIS
| MINV
));
704 if (hdr
->flags
& HSUB
)
705 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
706 if (hdr
->flags
& HFCC
) {
707 if ((cp
= strrchr(str
, '\n')))
709 for (cp
= pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
711 insert_fcc (hdr
, pp
);
713 insert_fcc (hdr
, pp
);
717 if (!(hdr
->flags
& HADR
)) {
718 fprintf (out
, "%s: %s", name
, str
);
722 tmpaddrs
.m_next
= NULL
;
723 for (count
= 0; (cp
= getname (str
)); count
++)
724 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
728 tmpaddrs
.m_next
= mp
;
732 if (hdr
->flags
& HTRY
)
738 if (hdr
->flags
& HNIL
)
739 fprintf (out
, "%s: %s", name
, str
);
742 * Sender (or Resent-Sender) can have only one address
744 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
745 : (hdr
->set
& MSND
)) {
746 advise (NULL
, "%s: field requires one address", name
);
750 advise (NULL
, "%s: field requires at least one address", name
);
757 if (count
> 1 && (hdr
->flags
& HONE
)) {
758 advise (NULL
, "%s: field only permits one address", name
);
763 nameoutput
= linepos
= 0;
764 snprintf (namep
, sizeof(namep
), "%s%s",
765 (hdr
->flags
& HMNG
) ? "Original-" : "", name
);
767 for (grp
= 0, mp
= tmpaddrs
.m_next
; mp
; mp
= np
)
768 if (mp
->m_nohost
) { /* also used to test (hdr->flags & HTRY) */
769 /* The address doesn't include a host, so it might be an alias. */
770 pp
= akvalue (mp
->m_mbox
); /* do mh alias substitution */
771 qp
= akvisible () ? mp
->m_mbox
: "";
774 putgrp (namep
, np
->m_gname
, out
, hdr
->flags
);
775 while ((cp
= getname (pp
))) {
776 if (!(mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
782 * If it's a From: or Resent-From: header, save the address
783 * for later possible use (as the envelope address for SMTP)
786 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
787 : (hdr
->set
& MFRM
)) {
788 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
789 from
[sizeof(from
) - 1] = '\0';
794 * Also save the Sender: or Resent-Sender: header as well
797 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
798 : (hdr
->set
& MSND
)) {
799 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
800 sender
[sizeof(sender
) - 1] = '\0';
805 * ALSO ... save Envelope-From
808 if (hdr
->set
& MEFM
) {
809 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
810 efrom
[sizeof(efrom
) - 1] = '\0';
813 if (hdr
->flags
& HBCC
)
816 mp
->m_ingrp
= np
->m_ingrp
;
819 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
821 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
822 /* Catch this before sendmail chokes with:
823 "553 List:; syntax illegal for recipient
825 If we wanted to, we could expand out blind
826 aliases and put them in Bcc:, but then
827 they'd have the Blind-Carbon-Copy
830 "blind lists not compatible with"
836 if (putadr (namep
, qp
, mp
, out
, hdr
->flags
))
837 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
846 /* Address includes a host, so no alias substitution is needed. */
849 * If it's a From: or Resent-From header, save the address
850 * for later possible use (as the envelope address for SMTP)
853 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
854 : (hdr
->set
& MFRM
)) {
855 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
860 * Also save the Sender: header as well
863 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
864 : (hdr
->set
& MSND
)) {
865 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
866 sender
[sizeof(sender
) - 1] = '\0';
871 * ALSO ... save Envelope-From
874 if (hdr
->set
& MEFM
) {
875 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
876 efrom
[sizeof(efrom
) - 1] = '\0';
879 if (hdr
->flags
& HBCC
)
882 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
885 keep
= putadr (namep
, "", mp
, out
, hdr
->flags
);
889 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
896 * If this is a From:/Resent-From: header, save the full thing for
897 * later in case we need it for use when constructing a Bcc draft message
900 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
) : (hdr
->set
& MFRM
)) {
901 strncpy(fullfrom
, str
, sizeof(fullfrom
));
902 fullfrom
[sizeof(fullfrom
) - 1] = 0;
904 * Strip off any trailing newlines
907 while (strlen(fullfrom
) > 0 && fullfrom
[strlen(fullfrom
) - 1] == '\n') {
908 fullfrom
[strlen(fullfrom
) - 1] = '\0';
912 if (grp
> 0 && (hdr
->flags
& HNGR
)) {
913 advise (NULL
, "%s: field does not allow groups", name
);
925 char *cp
, sigbuf
[BUFSIZ
];
931 * Probably not necessary, but just in case ...
939 if ((cp
= getfullname ()) && *cp
) {
940 strncpy (sigbuf
, cp
, sizeof(sigbuf
));
941 snprintf (signature
, sizeof(signature
), "%s <%s>",
942 sigbuf
, getlocaladdr());
943 if ((cp
= getname (signature
)) == NULL
)
944 adios (NULL
, "getname () failed -- you lose extraordinarily big");
945 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
)) == NULL
)
946 adios (NULL
, "bad signature '%s'", sigbuf
);
951 strncpy (signature
, getlocaladdr(), sizeof(signature
));
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 tfile
= m_mktemp2(NULL
, "bccs", NULL
, &out
);
1290 if (tfile
== NULL
) adios("bcc", "unable to create temporary file");
1291 strncpy (bccfil
, tfile
, sizeof(bccfil
));
1293 fprintf (out
, "From: %s\n", fullfrom
);
1294 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1296 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
1298 fprintf (out
, "Subject: %s", subject
);
1299 fprintf (out
, "BCC:\n");
1302 * Use MIME encapsulation for Bcc messages
1308 * Check if any lines in the message clash with the
1309 * prefix for the MIME multipart separator. If there
1310 * is a clash, increment one of the letters in the
1311 * prefix and check again.
1313 if ((cp
= strchr(prefix
, 'a')) == NULL
)
1314 adios (NULL
, "lost prefix start");
1315 while (find_prefix () == NOTOK
) {
1320 adios (NULL
, "can't find a unique delimiter string");
1325 fprintf (out
, "%s: %s\n%s: multipart/digest; boundary=\"",
1326 VRSN_FIELD
, VRSN_VALUE
, TYPE_FIELD
);
1327 fprintf (out
, "%s\"\n\n--%s\n\n", prefix
, prefix
);
1329 fprintf (out
, "\n------- Blind-Carbon-Copy\n\n");
1335 * Do mhl filtering of Bcc messages instead
1336 * of MIME encapsulation.
1338 if (filter
!= NULL
) {
1339 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1343 adios ("fork", "unable to");
1346 dup2 (fileno (out
), 1);
1348 vec
= argsplit(mhlproc
, &program
, &i
);
1349 vec
[i
++] = "-forward";
1354 /* was the flag -[no]dashstuffing specified? */
1356 vec
[i
++] = "-dashstuffing";
1357 else if (dashstuff
< 0)
1358 vec
[i
++] = "-nodashstuffing";
1361 execvp (program
, vec
);
1362 fprintf (stderr
, "unable to exec ");
1367 pidXwait (child_id
, mhlproc
);
1371 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
1372 adios (tmpfil
, "unable to re-open");
1375 * If using MIME encapsulation, or if the -nodashstuffing
1376 * flag was given, then just copy message. Else do
1377 * RFC934 quoting (dashstuffing).
1379 if (mime
|| dashstuff
< 0)
1380 cpydata (fd
, fileno (out
), tmpfil
, bccfil
);
1382 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
1386 fseek (out
, 0L, SEEK_END
);
1388 fprintf (out
, "\n--%s--\n", prefix
);
1390 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
1396 * Scan message to check if any lines clash with
1397 * the prefix of the MIME multipart separator.
1404 char buffer
[BUFSIZ
];
1407 if ((in
= fopen (tmpfil
, "r")) == NULL
)
1408 adios (tmpfil
, "unable to re-open");
1410 while (fgets (buffer
, sizeof(buffer
) - 1, in
))
1411 if (buffer
[0] == '-' && buffer
[1] == '-') {
1414 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1415 if (!isspace ((unsigned char) *cp
))
1418 if (strcmp (buffer
+ 2, prefix
) == 0) {
1429 #define plural(x) (x == 1 ? "" : "s")
1434 if (badadr
&& unkadr
)
1435 die (NULL
, "%d address%s unparsable, %d addressee%s undeliverable",
1436 badadr
, plural (badadr
), unkadr
, plural (badadr
));
1438 die (NULL
, "%d address%s unparsable", badadr
, plural (badadr
));
1440 die (NULL
, "%d addressee%s undeliverable", unkadr
, plural (unkadr
));
1445 do_addresses (int bccque
, int talk
)
1449 struct mailname
*lp
;
1452 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1453 if (lp
->m_bcc
? bccque
: !bccque
) {
1455 printf (" -- Local Recipients --\n");
1456 do_an_address (lp
, talk
);
1461 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1462 if (lp
->m_bcc
? bccque
: !bccque
) {
1464 printf (" -- UUCP Recipients --\n");
1465 do_an_address (lp
, talk
);
1470 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1471 if (lp
->m_bcc
? bccque
: !bccque
) {
1473 printf (" -- Network Recipients --\n");
1474 do_an_address (lp
, talk
);
1480 if (rp_isbad (retval
= sm_waend ()))
1481 die (NULL
, "problem ending addresses; %s", rp_string (retval
));
1486 * MTS-SPECIFIC INTERACTION
1491 * SENDMAIL/SMTP routines
1495 post (char *file
, int bccque
, int talk
, char *envelope
)
1502 if (msgflags
& MINV
)
1503 printf (" -- Posting for %s Recipients --\n",
1504 bccque
? "Blind" : "Sighted");
1506 printf (" -- Posting for All Recipients --\n");
1511 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
1512 char **argp
, *program
;
1515 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1519 adios ("fork", "unable to");
1522 if (freopen( file
, "r", stdin
) == NULL
) {
1523 adios (file
, "can't reopen for sendmail");
1526 argp
= argsplit(sendmail
, &program
, &argc
);
1527 argp
[argc
++] = "-t"; /* read msg for recipients */
1528 argp
[argc
++] = "-i"; /* don't stop on "." */
1530 argp
[argc
++] = "-bv";
1532 argp
[argc
++] = "-v";
1535 execv (program
, argp
);
1536 adios (sendmail
, "can't exec");
1539 pidXwait (child_id
, NULL
);
1543 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1544 verbose
, snoop
, queued
, sasl
,
1545 saslssf
, saslmech
, user
, tls
)) ||
1546 rp_isbad (retval
= sm_winit (envelope
)))
1547 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1549 do_addresses (bccque
, talk
&& verbose
);
1550 if ((fd
= open (file
, O_RDONLY
)) == NOTOK
)
1551 die (file
, "unable to re-open");
1556 sm_end (!(msgflags
& MINV
) || bccque
? OK
: DONE
);
1560 if (msgflags
& MINV
)
1561 printf (" -- %s Recipient Copies Posted --\n",
1562 bccque
? "Blind" : "Sighted");
1564 printf (" -- Recipient Copies Posted --\n");
1572 /* Address Verification */
1575 verify_all_addresses (int talk
, char *envelope
)
1578 struct mailname
*lp
;
1582 if (!whomsw
|| checksw
)
1583 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1584 verbose
, snoop
, queued
, sasl
,
1585 saslssf
, saslmech
, user
, tls
))
1586 || rp_isbad (retval
= sm_winit (envelope
)))
1587 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1589 if (talk
&& !whomsw
)
1590 printf (" -- Address Verification --\n");
1591 if (talk
&& localaddrs
.m_next
)
1592 printf (" -- Local Recipients --\n");
1593 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1594 do_an_address (lp
, talk
);
1596 if (talk
&& uuaddrs
.m_next
)
1597 printf (" -- UUCP Recipients --\n");
1598 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1599 do_an_address (lp
, talk
);
1601 if (talk
&& netaddrs
.m_next
)
1602 printf (" -- Network Recipients --\n");
1603 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1604 do_an_address (lp
, talk
);
1607 if (talk
&& !whomsw
)
1608 printf (" -- Address Verification Successful --\n");
1610 if (!whomsw
|| checksw
)
1619 do_an_address (struct mailname
*lp
, int talk
)
1625 switch (lp
->m_type
) {
1629 strncpy (addr
, mbox
, sizeof(addr
));
1633 mbox
= auxformat (lp
, 0);
1635 snprintf (addr
, sizeof(addr
), "%s!%s", lp
->m_host
, lp
->m_mbox
);
1638 default: /* let SendMail decide if the host is bad */
1641 snprintf (addr
, sizeof(addr
), "%s at %s", mbox
, host
);
1646 printf (" %s%s", addr
, whomsw
&& lp
->m_bcc
? "[BCC]" : "");
1648 if (whomsw
&& !checksw
) {
1656 switch (retval
= sm_wadr (mbox
, host
,
1657 lp
->m_type
!= UUCPHOST
? lp
->m_path
: NULL
)) {
1660 printf ("address ok\n");
1666 fprintf (stderr
, " %s: ", addr
);
1667 fprintf (talk
? stdout
: stderr
, "loses; %s\n",
1668 rp_string (retval
));
1674 fprintf (stderr
, " %s: ", addr
);
1675 die (NULL
, "unexpected response; %s", rp_string (retval
));
1683 do_text (char *file
, int fd
)
1688 lseek (fd
, (off_t
) 0, SEEK_SET
);
1690 while ((state
= read (fd
, buf
, sizeof(buf
))) > 0) {
1691 if (rp_isbad (retval
= sm_wtxt (buf
, state
)))
1692 die (NULL
, "problem writing text; %s\n", rp_string (retval
));
1696 die (file
, "problem reading from");
1698 switch (retval
= sm_wtend ()) {
1704 die (NULL
, "posting failed; %s", rp_string (retval
));
1707 die (NULL
, "unexpected response; %s", rp_string (retval
));
1722 if (msgflags
& MINV
)
1725 if (!whomsw
|| checksw
)
1738 hstat
= SIGNAL2 (SIGHUP
, sigser
);
1739 istat
= SIGNAL2 (SIGINT
, sigser
);
1740 qstat
= SIGNAL2 (SIGQUIT
, sigser
);
1741 tstat
= SIGNAL2 (SIGTERM
, sigser
);
1751 SIGNAL (SIGHUP
, hstat
);
1752 SIGNAL (SIGINT
, istat
);
1753 SIGNAL (SIGQUIT
, qstat
);
1754 SIGNAL (SIGTERM
, tstat
);
1762 p_refile (char *file
)
1770 printf (" -- Filing Folder Copies --\n");
1771 for (i
= 0; i
< fccind
; i
++)
1772 fcc (file
, fccfold
[i
]);
1774 printf (" -- Folder Copies Filed --\n");
1779 * Call the `fileproc' to add the file to the folder.
1783 fcc (char *file
, char *folder
)
1786 int i
, status
, argp
;
1788 char **arglist
, *program
;
1791 printf (" %sFcc %s: ", msgstate
== RESENT
? "Resent-" : "", folder
);
1794 for (i
= 0; (child_id
= fork ()) == NOTOK
&& i
< 5; i
++)
1800 fprintf (stderr
, " %sFcc %s: ",
1801 msgstate
== RESENT
? "Resent-" : "", folder
);
1802 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
1806 /* see if we need to add `+' */
1807 snprintf (fold
, sizeof(fold
), "%s%s",
1808 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
1810 /* now exec the fileproc */
1812 arglist
= argsplit(fileproc
, &program
, &argp
);
1813 arglist
[argp
++] = "-link";
1814 arglist
[argp
++] = "-file";
1815 arglist
[argp
++] = file
;
1816 arglist
[argp
++] = fold
;
1817 arglist
[argp
] = NULL
;
1818 execvp (program
, arglist
);
1822 if ((status
= pidwait (child_id
, OK
))) {
1824 fprintf (stderr
, " %sFcc %s: ",
1825 msgstate
== RESENT
? "Resent-" : "", folder
);
1826 pidstatus (status
, verbose
? stdout
: stderr
, NULL
);
1829 printf ("folder ok\n");
1841 die (char *what
, char *fmt
, ...)
1846 if (msgflags
& MINV
)
1849 if (!whomsw
|| checksw
)
1853 advertise (what
, NULL
, fmt
, ap
);