]>
diplodocus.org Git - nmh/blob - uip/post.c
9c7af29e7623e01f616b77bf6083f0642ac5228d
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>
25 #ifdef HAVE_SYS_TIME_H
26 # include <sys/time.h>
30 #include <mts/smtp/smtp.h>
33 # define SASLminc(a) (a)
34 #else /* CYRUS_SASL */
35 # define SASLminc(a) 0
36 #endif /* CYRUS_SASL */
39 # define TLSminc(a) (a)
40 #else /* TLS_SUPPORT */
42 #endif /* TLS_SUPPORT */
44 #define FCCS 10 /* max number of fccs allowed */
46 /* In the following array of structures, the numeric second field of the
47 structures (minchars) is apparently used like this:
49 -# : Switch can be abbreviated to # characters; switch hidden in -help.
50 0 : Switch can't be abbreviated; switch shown in -help.
51 # : Switch can be abbreviated to # characters; switch shown in -help. */
53 #define POST_SWITCHES \
54 X("alias aliasfile", 0, ALIASW) \
55 X("check", -5, CHKSW) /* interface from whom */ \
56 X("nocheck", -7, NCHKSW) /* interface from whom */ \
57 X("debug", -5, DEBUGSW) \
58 X("dist", -4, DISTSW) /* interface from dist */ \
59 X("filter filterfile", 0, FILTSW) \
60 X("nofilter", 0, NFILTSW) \
61 X("format", 0, FRMTSW) \
62 X("noformat", 0, NFRMTSW) \
63 X("library directory", -7, LIBSW) /* interface from send, whom */ \
64 X("mime", 0, MIMESW) \
65 X("nomime", 0, NMIMESW) \
66 X("msgid", 0, MSGDSW) \
67 X("nomsgid", 0, NMSGDSW) \
68 X("verbose", 0, VERBSW) \
69 X("noverbose", 0, NVERBSW) \
70 X("watch", 0, WATCSW) \
71 X("nowatch", 0, NWATCSW) \
72 X("whom", -4, WHOMSW) /* interface from whom */ \
73 X("width columns", 0, WIDTHSW) \
74 X("version", 0, VERSIONSW) \
75 X("help", 0, HELPSW) \
76 X("dashstuffing", -12, BITSTUFFSW) /* should we dashstuff BCC messages? */ \
77 X("nodashstuffing", -14, NBITSTUFFSW) \
78 X("idanno number", -6, ANNOSW) /* interface from send */ \
79 X("client host", -6, CLIESW) \
80 X("server host", 6, SERVSW) /* specify alternate SMTP server */ \
81 X("snoop", -5, SNOOPSW) /* snoop the SMTP transaction */ \
82 X("partno", -6, PARTSW) \
83 X("queued", -6, QUEUESW) \
84 X("sasl", SASLminc(-4), SASLSW) \
85 X("nosasl", SASLminc(-6), NOSASLSW) \
86 X("saslmaxssf", SASLminc(-10), SASLMXSSFSW) \
87 X("saslmech", SASLminc(-5), SASLMECHSW) \
88 X("user", SASLminc(-4), USERSW) \
89 X("port server port name/number", 4, PORTSW) \
90 X("tls", TLSminc(-3), TLSSW) \
91 X("initialtls", TLSminc(-10), INITTLSSW) \
92 X("notls", TLSminc(-5), NTLSSW) \
93 X("fileproc", -4, FILEPROCSW) \
94 X("mhlproc", -3, MHLPROCSW) \
95 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
96 X("messageid localname|random", 2, MESSAGEIDSW) \
98 #define X(sw, minchars, id) id,
99 DEFINE_SWITCH_ENUM(POST
);
102 #define X(sw, minchars, id) { sw, minchars, id },
103 DEFINE_SWITCH_ARRAY(POST
, switches
);
114 * flags for headers->flags
116 #define HNOP 0x0000 /* just used to keep .set around */
117 #define HBAD 0x0001 /* bad header - don't let it through */
118 #define HADR 0x0002 /* header has an address field */
119 #define HSUB 0x0004 /* Subject: header */
120 #define HTRY 0x0008 /* try to send to addrs on header */
121 #define HBCC 0x0010 /* don't output this header, unless MTS_SENDMAIL_PIPE */
122 #define HMNG 0x0020 /* munge this header */
123 #define HNGR 0x0040 /* no groups allowed in this header */
124 #define HFCC 0x0080 /* FCC: type header */
125 #define HNIL 0x0100 /* okay for this header not to have addrs */
126 #define HIGN 0x0200 /* ignore this header */
127 #define HDCC 0x0400 /* another undocumented feature */
128 #define HONE 0x0800 /* Only (zero or) one address allowed */
129 #define HEFM 0x1000 /* Envelope-From: header */
132 * flags for headers->set
134 #define MFRM 0x0001 /* we've seen a From: */
135 #define MDAT 0x0002 /* we've seen a Date: */
136 #define MRFM 0x0004 /* we've seen a Resent-From: */
137 #define MVIS 0x0008 /* we've seen sighted addrs */
138 #define MINV 0x0010 /* we've seen blind addrs */
139 #define MSND 0x0020 /* we've seen a Sender: */
140 #define MRSN 0x0040 /* We've seen a Resent-Sendr:*/
141 #define MEFM 0x0080 /* We've seen Envelope-From: */
144 static struct headers NHeaders
[] = {
145 { "Return-Path", HBAD
, 0 },
146 { "Received", HBAD
, 0 },
147 { "Reply-To", HADR
|HNGR
, 0 },
148 { "From", HADR
|HNGR
, MFRM
},
149 { "Sender", HADR
|HNGR
|HONE
, MSND
},
151 { "Subject", HSUB
, 0 },
152 { "To", HADR
|HTRY
, MVIS
},
153 { "cc", HADR
|HTRY
, MVIS
},
154 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, MINV
},
155 { "Dcc", HADR
|HTRY
|HDCC
|HNIL
, MVIS
}, /* sorta cc & bcc combined */
156 { "Message-ID", HBAD
, 0 },
158 { "Envelope-From", HADR
|HONE
|HEFM
, MEFM
},
162 static struct headers RHeaders
[] = {
163 { "Resent-Reply-To", HADR
|HNGR
, 0 },
164 { "Resent-From", HADR
|HNGR
, MRFM
},
165 { "Resent-Sender", HADR
|HNGR
, MRSN
},
166 { "Resent-Date", HBAD
, 0 },
167 { "Resent-Subject", HSUB
, 0 },
168 { "Resent-To", HADR
|HTRY
, MVIS
},
169 { "Resent-cc", HADR
|HTRY
, MVIS
},
170 { "Resent-Bcc", HADR
|HTRY
|HBCC
, MINV
},
171 { "Resent-Message-ID", HBAD
, 0 },
172 { "Resent-Fcc", HFCC
, 0 },
173 { "Reply-To", HADR
, 0 },
174 { "From", HADR
|HNGR
, MFRM
},
175 { "Sender", HADR
|HNGR
, MSND
},
176 { "Date", HNOP
, MDAT
},
177 { "To", HADR
|HNIL
, 0 },
178 { "cc", HADR
|HNIL
, 0 },
179 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, 0 },
181 { "Envelope-From", HADR
|HONE
|HEFM
, MEFM
},
185 static short fccind
= 0; /* index into fccfold[] */
186 static short outputlinelen
= OUTPUTLINELEN
;
188 static int pfd
= NOTOK
; /* fd to write annotation list to */
189 static int recipients
= 0; /* how many people will get a copy */
190 static int unkadr
= 0; /* how many of those were unknown */
191 static int badadr
= 0; /* number of bad addrs */
192 static int badmsg
= 0; /* message has bad semantics */
193 static int verbose
= 0; /* spell it out */
194 static int format
= 1; /* format addresses */
195 static int mime
= 0; /* use MIME-style encapsulations for Bcc */
196 static int msgid
= 0; /* add msgid */
197 static int debug
= 0; /* debugging post */
198 static int watch
= 0; /* watch the delivery process */
199 static int whomsw
= 0; /* we are whom not post */
200 static int checksw
= 0; /* whom -check */
201 static int linepos
=0; /* putadr()'s position on the line */
202 static int nameoutput
=0; /* putadr() has output header name */
203 static int sasl
=0; /* Use SASL auth for SMTP */
204 static int saslssf
=-1; /* Our maximum SSF for SASL */
205 static char *saslmech
=NULL
; /* Force use of particular SASL mech */
206 static char *user
=NULL
; /* Authenticate as this user */
207 static char *port
="smtp"; /* Name of server port for SMTP */
208 static int tls
=0; /* Use TLS for encryption */
209 static int fromcount
=0; /* Count of addresses on From: header */
210 static int seensender
=0; /* Have we seen a Sender: header? */
212 static unsigned msgflags
= 0; /* what we've seen */
216 static int msgstate
= NORMAL
;
218 static time_t tclock
= 0; /* the time we started (more or less) */
220 static SIGNAL_HANDLER hstat
, istat
, qstat
, tstat
;
222 static char tmpfil
[BUFSIZ
];
223 static char bccfil
[BUFSIZ
];
225 static char from
[BUFSIZ
]; /* my network address */
226 static char sender
[BUFSIZ
]; /* my Sender: header */
227 static char efrom
[BUFSIZ
]; /* my Envelope-From: header */
228 static char fullfrom
[BUFSIZ
]; /* full contents of From header */
229 static char signature
[BUFSIZ
]; /* my signature */
230 static char *filter
= NULL
; /* the filter for BCC'ing */
231 static char *subject
= NULL
; /* the subject field for BCC'ing */
232 static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
234 static struct headers
*hdrtab
; /* table for the message we're doing */
236 static struct mailname localaddrs
; /* local addrs */
237 static struct mailname netaddrs
; /* network addrs */
238 static struct mailname uuaddrs
; /* uucp addrs */
239 static struct mailname tmpaddrs
; /* temporary queue */
241 static int snoop
= 0;
242 static char *clientsw
= NULL
;
243 static char *serversw
= NULL
;
245 extern struct smtp sm_reply
;
247 static char prefix
[] = "----- =_aaaaaaaaaa";
249 static char *partno
= NULL
;
250 static int queued
= 0;
255 static void putfmt (char *, char *, FILE *);
256 static void start_headers (void);
257 static void finish_headers (FILE *);
258 static int get_header (char *, struct headers
*);
259 static int putadr (char *, char *, struct mailname
*, FILE *, unsigned int);
260 static void putgrp (char *, char *, FILE *, unsigned int);
261 static int insert (struct mailname
*);
262 static void pl (void);
263 static void anno (void);
264 static int annoaux (struct mailname
*);
265 static void insert_fcc (struct headers
*, char *);
266 static void make_bcc_file (int);
267 static void verify_all_addresses (int, char *);
268 static void chkadr (void);
269 static void sigon (void);
270 static void sigoff (void);
271 static void p_refile (char *);
272 static void fcc (char *, char *);
273 static void die (char *, char *, ...);
274 static void post (char *, int, int, char *);
275 static void do_text (char *file
, int fd
);
276 static void do_an_address (struct mailname
*, int);
277 static void do_addresses (int, int);
278 static int find_prefix (void);
282 main (int argc
, char **argv
)
284 int state
, compnum
, dashstuff
= 0;
285 char *cp
, *msg
= NULL
, **argp
, **arguments
, *envelope
;
286 char buf
[BUFSIZ
], name
[NAMESZ
];
288 m_getfld_state_t gstate
= 0;
291 setlocale(LC_ALL
, "");
293 invo_name
= r1bindex (argv
[0], '/');
295 /* foil search of user profile/context */
296 if (context_foil (NULL
) == -1)
299 mts_init (invo_name
);
300 arguments
= getarguments (invo_name
, argc
, argv
, 0);
303 while ((cp
= *argp
++)) {
305 switch (smatch (++cp
, switches
)) {
307 ambigsw (cp
, switches
);
310 adios (NULL
, "-%s unknown", cp
);
313 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
314 print_help (buf
, switches
, 0);
317 print_version(invo_name
);
321 if (!(cp
= *argp
++) || *cp
== '-')
322 adios (NULL
, "missing argument to %s", argp
[-2]);
323 /* create a minimal context */
324 if (context_foil (cp
) == -1)
329 if (!(cp
= *argp
++) || *cp
== '-')
330 adios (NULL
, "missing argument to %s", argp
[-2]);
331 if ((state
= alias (cp
)) != AK_OK
)
332 adios (NULL
, "aliasing error in %s - %s",
333 cp
, akerror (state
));
352 if (!(filter
= *argp
++) || *filter
== '-')
353 adios (NULL
, "missing argument to %s", argp
[-2]);
408 if (!(cp
= *argp
++) || *cp
== '-')
409 adios (NULL
, "missing argument to %s", argp
[-2]);
410 if ((outputlinelen
= atoi (cp
)) < 10)
411 adios (NULL
, "impossible width %d", outputlinelen
);
415 if (!(cp
= *argp
++) || *cp
== '-')
416 adios (NULL
, "missing argument to %s", argp
[-2]);
417 if ((pfd
= atoi (cp
)) <= 2)
418 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
422 if (!(clientsw
= *argp
++) || *clientsw
== '-')
423 adios (NULL
, "missing argument to %s", argp
[-2]);
426 if (!(serversw
= *argp
++) || *serversw
== '-')
427 adios (NULL
, "missing argument to %s", argp
[-2]);
434 if (!(partno
= *argp
++) || *partno
== '-')
435 adios (NULL
, "missing argument to %s", argp
[-2]);
451 if (!(cp
= *argp
++) || *cp
== '-')
452 adios (NULL
, "missing argument to %s", argp
[-2]);
457 if (!(saslmech
= *argp
++) || *saslmech
== '-')
458 adios (NULL
, "missing argument to %s", argp
[-2]);
462 if (!(user
= *argp
++) || *user
== '-')
463 adios (NULL
, "missing argument to %s", argp
[-2]);
467 if (!(port
= *argp
++) || *port
== '-')
468 adios (NULL
, "missing argument to %s", argp
[-2]);
484 if (!(cp
= *argp
++) || *cp
== '-')
485 adios (NULL
, "missing argument to %s", argp
[-2]);
490 if (!(cp
= *argp
++) || *cp
== '-')
491 adios (NULL
, "missing argument to %s", argp
[-2]);
496 if (!(cp
= *argp
++) || *cp
== '-')
497 adios (NULL
, "missing argument to %s", argp
[-2]);
498 save_mts_method (cp
);
502 if (!(cp
= *argp
++) || *cp
== '-')
503 adios (NULL
, "missing argument to %s", argp
[-2]);
504 if (save_message_id_style (cp
) != 0)
505 adios (NULL
, "unsupported messageid \"%s\"", cp
);
510 adios (NULL
, "only one message at a time!");
518 adios (NULL
, "usage: %s [switches] file", invo_name
);
520 if (outputlinelen
< 10)
521 adios (NULL
, "impossible width %d", outputlinelen
);
523 if ((in
= fopen (msg
, "r")) == NULL
)
524 adios (msg
, "unable to open");
532 if ((out
= fopen ("/dev/null", "w")) == NULL
)
533 adios ("/dev/null", "unable to open");
535 char *cp
= m_mktemp(m_maildir(invo_name
), NULL
, &out
);
537 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
539 adios ("post", "unable to create temporary file");
542 strncpy(tmpfil
, cp
, sizeof(tmpfil
));
543 chmod (tmpfil
, 0600);
547 hdrtab
= msgstate
== NORMAL
? NHeaders
: RHeaders
;
549 for (compnum
= 1;;) {
550 int bufsz
= sizeof buf
;
551 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
555 cp
= add (buf
, NULL
);
556 while (state
== FLDPLUS
) {
558 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
561 putfmt (name
, cp
, out
);
566 finish_headers (out
);
569 fprintf (out
, "\n%s", buf
);
570 while (state
== BODY
) {
572 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
578 finish_headers (out
);
583 adios (NULL
, "message format error in component #%d", compnum
);
586 adios (NULL
, "getfld() returned %d", state
);
590 m_getfld_state_destroy (&gstate
);
604 * Here's how we decide which address to use as the envelope-from
607 * - If we were given an Envelope-From header, use that.
608 * - If we were given a Sender: address, use that.
609 * - Otherwise, use the address on the From: line
612 if (msgflags
& MEFM
) {
614 } else if (seensender
) {
620 /* If we are doing a "whom" check */
622 /* This won't work with MTS_SENDMAIL_PIPE. */
623 verify_all_addresses (1, envelope
);
627 if (msgflags
& MINV
) {
628 make_bcc_file (dashstuff
);
629 if (msgflags
& MVIS
) {
630 if (sm_mts
!= MTS_SENDMAIL_PIPE
) {
631 /* It would be nice to have support to call
632 verify_all_addresses with MTS_SENDMAIL_PIPE, but
633 that might require running sendmail as root. Note
634 that spost didn't verify addresses. */
635 verify_all_addresses (verbose
, envelope
);
637 post (tmpfil
, 0, verbose
, envelope
);
639 post (bccfil
, 1, verbose
, envelope
);
642 post (tmpfil
, 0, isatty (1), envelope
);
649 printf (partno
? "Partial Message #%s Processed\n" : "Message Processed\n",
661 putfmt (char *name
, char *str
, FILE *out
)
663 int count
, grp
, i
, keep
;
666 struct mailname
*mp
= NULL
, *np
= NULL
;
669 while (*str
== ' ' || *str
== '\t')
672 if (msgstate
== NORMAL
&& uprf (name
, "resent")) {
673 advise (NULL
, "illegal header line -- %s:", name
);
678 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
679 if (strncasecmp (name
, "nmh-", 4)) {
680 fprintf (out
, "%s: %s", name
, str
);
682 /* Filter out all Nmh-* headers, because Norm asked. They
683 should never have reached this point. Warn about any
684 that are non-empty. */
685 if (strcmp (str
, "\n")) {
686 char *newline
= strchr (str
, '\n');
687 if (newline
) *newline
= '\0';
689 advise (NULL
, "ignoring header line -- %s: %s", name
, str
);
698 if (hdr
->flags
& HIGN
) {
701 if (hdr
->flags
& HBAD
) {
702 advise (NULL
, "illegal header line -- %s:", name
);
706 msgflags
|= (hdr
->set
& ~(MVIS
| MINV
));
708 if (hdr
->flags
& HSUB
)
709 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
710 if (hdr
->flags
& HFCC
) {
711 if ((cp
= strrchr(str
, '\n')))
713 for (cp
= pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
715 insert_fcc (hdr
, pp
);
717 insert_fcc (hdr
, pp
);
721 if (!(hdr
->flags
& HADR
)) {
722 fprintf (out
, "%s: %s", name
, str
);
726 tmpaddrs
.m_next
= NULL
;
727 for (count
= 0; (cp
= getname (str
)); count
++)
728 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
732 tmpaddrs
.m_next
= mp
;
736 if (hdr
->flags
& HTRY
)
742 if (hdr
->flags
& HNIL
)
743 fprintf (out
, "%s: %s", name
, str
);
746 * Sender (or Resent-Sender) can have only one address
748 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
749 : (hdr
->set
& MSND
)) {
750 advise (NULL
, "%s: field requires one address", name
);
754 advise (NULL
, "%s: field requires at least one address", name
);
761 if (count
> 1 && (hdr
->flags
& HONE
)) {
762 advise (NULL
, "%s: field only permits one address", name
);
767 nameoutput
= linepos
= 0;
768 snprintf (namep
, sizeof(namep
), "%s%s",
769 (hdr
->flags
& HMNG
) ? "Original-" : "", name
);
771 for (grp
= 0, mp
= tmpaddrs
.m_next
; mp
; mp
= np
)
772 if (mp
->m_nohost
) { /* also used to test (hdr->flags & HTRY) */
773 /* The address doesn't include a host, so it might be an alias. */
774 pp
= akvalue (mp
->m_mbox
); /* do mh alias substitution */
775 qp
= akvisible () ? mp
->m_mbox
: "";
778 putgrp (namep
, np
->m_gname
, out
, hdr
->flags
);
779 while ((cp
= getname (pp
))) {
780 if (!(mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
786 * If it's a From: or Resent-From: header, save the address
787 * for later possible use (as the envelope address for SMTP)
790 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
791 : (hdr
->set
& MFRM
)) {
792 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
793 from
[sizeof(from
) - 1] = '\0';
798 * Also save the Sender: or Resent-Sender: header as well
801 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
802 : (hdr
->set
& MSND
)) {
803 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
804 sender
[sizeof(sender
) - 1] = '\0';
809 * ALSO ... save Envelope-From
812 if (hdr
->set
& MEFM
) {
813 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
814 efrom
[sizeof(efrom
) - 1] = '\0';
817 if (hdr
->flags
& HBCC
)
820 mp
->m_ingrp
= np
->m_ingrp
;
823 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
825 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
826 /* Catch this before sendmail chokes with:
827 "553 List:; syntax illegal for recipient
829 If we wanted to, we could expand out blind
830 aliases and put them in Bcc:, but then
831 they'd have the Blind-Carbon-Copy
834 "blind lists not compatible with"
840 if (putadr (namep
, qp
, mp
, out
, hdr
->flags
))
841 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
850 /* Address includes a host, so no alias substitution is needed. */
853 * If it's a From: or Resent-From header, save the address
854 * for later possible use (as the envelope address for SMTP)
857 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
858 : (hdr
->set
& MFRM
)) {
859 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
864 * Also save the Sender: header as well
867 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
868 : (hdr
->set
& MSND
)) {
869 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
870 sender
[sizeof(sender
) - 1] = '\0';
875 * ALSO ... save Envelope-From
878 if (hdr
->set
& MEFM
) {
879 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
880 efrom
[sizeof(efrom
) - 1] = '\0';
883 if (hdr
->flags
& HBCC
)
886 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
889 keep
= putadr (namep
, "", mp
, out
, hdr
->flags
);
893 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
900 * If this is a From:/Resent-From: header, save the full thing for
901 * later in case we need it for use when constructing a Bcc draft message
904 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
) : (hdr
->set
& MFRM
)) {
905 strncpy(fullfrom
, str
, sizeof(fullfrom
));
906 fullfrom
[sizeof(fullfrom
) - 1] = 0;
908 * Strip off any trailing newlines
911 while (strlen(fullfrom
) > 0 && fullfrom
[strlen(fullfrom
) - 1] == '\n') {
912 fullfrom
[strlen(fullfrom
) - 1] = '\0';
916 if (grp
> 0 && (hdr
->flags
& HNGR
)) {
917 advise (NULL
, "%s: field does not allow groups", name
);
929 char *cp
, sigbuf
[BUFSIZ
];
935 * Probably not necessary, but just in case ...
943 if ((cp
= getfullname ()) && *cp
) {
944 strncpy (sigbuf
, cp
, sizeof(sigbuf
));
945 snprintf (signature
, sizeof(signature
), "%s <%s>",
946 sigbuf
, getlocaladdr());
947 if ((cp
= getname (signature
)) == NULL
)
948 adios (NULL
, "getname () failed -- you lose extraordinarily big");
949 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
)) == NULL
)
950 adios (NULL
, "bad signature '%s'", sigbuf
);
955 strncpy (signature
, getlocaladdr(), sizeof(signature
));
961 * Now that we've outputted the header fields in the draft
962 * message, we will now output any remaining header fields
963 * that we need to add/create.
967 finish_headers (FILE *out
)
971 if (!(msgflags
& MFRM
)) {
973 * A From: header is now required in the draft.
975 advise (NULL
, "message has no From: header");
976 advise (NULL
, "See default components files for examples");
981 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
982 advise (NULL
, "A Sender: or Envelope-From: header is required "
983 "with multiple\nFrom: addresses");
991 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
993 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
995 * If we have multiple From: addresses, make sure we have an
996 * Sender: header. If we don't have one, then generate one
997 * from Envelope-From: (which in this case, cannot be blank)
1000 if (fromcount
> 1 && seensender
== 0) {
1001 if (efrom
[0] == '\0') {
1002 advise (NULL
, "Envelope-From cannot be blank when there "
1003 "is multiple From: addresses\nand no Sender: "
1007 fprintf (out
, "Sender: %s\n", efrom
);
1011 if (!(msgflags
& MVIS
))
1012 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
1016 if (!(msgflags
& MDAT
)) {
1017 advise (NULL
, "message has no Date: header");
1020 if (!(msgflags
& MFRM
)) {
1021 advise (NULL
, "message has no From: header");
1024 if (!(msgflags
& MRFM
)) {
1025 advise (NULL
, "message has no Resent-From: header");
1026 advise (NULL
, "See default components files for examples");
1030 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
1031 advise (NULL
, "A Resent-Sender: or Envelope-From: header is "
1032 "required with multiple\nResent-From: addresses");
1040 fprintf (out
, "Resent-Date: %s\n", dtime (&tclock
, 0));
1042 fprintf (out
, "Resent-Message-ID: %s\n",
1043 message_id (tclock
, 0));
1045 * If we have multiple Resent-From: addresses, make sure we have an
1046 * Resent-Sender: header. If we don't have one, then generate one
1047 * from Envelope-From (which in this case, cannot be blank)
1050 if (fromcount
> 1 && seensender
== 0) {
1051 if (efrom
[0] == '\0') {
1052 advise (NULL
, "Envelope-From cannot be blank when there "
1053 "is multiple Resent-From: addresses and no "
1054 "Resent-Sender: header");
1057 fprintf (out
, "Resent-Sender: %s\n", efrom
);
1061 if (!(msgflags
& MVIS
))
1062 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
1067 adios (NULL
, "re-format message and try again");
1069 adios (NULL
, "no addressees");
1074 get_header (char *header
, struct headers
*table
)
1078 for (h
= table
; h
->value
; h
++)
1079 if (!strcasecmp (header
? header
: "", h
->value
? h
->value
: ""))
1087 putadr (char *name
, char *aka
, struct mailname
*mp
, FILE *out
, unsigned int flags
)
1091 char buffer
[BUFSIZ
];
1093 if (mp
->m_mbox
== NULL
|| ((flags
& HTRY
) && !insert (mp
)))
1095 if (sm_mts
!= MTS_SENDMAIL_PIPE
&&
1096 ((flags
& (HBCC
| HDCC
| HEFM
)) || mp
->m_ingrp
))
1100 fprintf (out
, "%s: ", name
);
1101 linepos
+= (nameoutput
= strlen (name
) + 2);
1104 if (*aka
&& mp
->m_type
!= UUCPHOST
&& !mp
->m_pers
)
1105 mp
->m_pers
= getcpy (aka
);
1108 snprintf (buffer
, sizeof(buffer
), "%s;", mp
->m_gname
);
1111 cp
= adrformat (mp
);
1118 if (linepos
!= nameoutput
) {
1119 if (len
+ linepos
+ 2 > outputlinelen
)
1120 fprintf (out
, ",\n%*s", linepos
= nameoutput
, "");
1130 return (flags
& HTRY
);
1135 putgrp (char *name
, char *group
, FILE *out
, unsigned int flags
)
1140 if (sm_mts
!= MTS_SENDMAIL_PIPE
&& (flags
& HBCC
))
1144 fprintf (out
, "%s: ", name
);
1145 linepos
+= (nameoutput
= strlen (name
) + 2);
1148 cp
= concat (group
, ";", NULL
);
1151 if (linepos
> nameoutput
) {
1152 if (len
+ linepos
+ 2 > outputlinelen
) {
1153 fprintf (out
, ",\n%*s", nameoutput
, "");
1154 linepos
= nameoutput
;
1168 insert (struct mailname
*np
)
1170 struct mailname
*mp
;
1172 if (np
->m_mbox
== NULL
)
1175 for (mp
= np
->m_type
== LOCALHOST
? &localaddrs
1176 : np
->m_type
== UUCPHOST
? &uuaddrs
1180 if (!strcasecmp (np
->m_host
? np
->m_host
: "",
1181 mp
->m_next
->m_host
? mp
->m_next
->m_host
: "") &&
1182 !strcasecmp (np
->m_mbox
? np
->m_mbox
: "",
1183 mp
->m_next
->m_mbox
? mp
->m_next
->m_mbox
: "") &&
1184 np
->m_bcc
== mp
->m_next
->m_bcc
)
1197 struct mailname
*mp
;
1199 printf ("-------\n\t-- Addresses --\nlocal:\t");
1200 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1201 printf ("%s%s%s", mp
->m_mbox
,
1202 mp
->m_bcc
? "[BCC]" : "",
1203 mp
->m_next
? ",\n\t" : "");
1205 printf ("\nnet:\t");
1206 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1207 printf ("%s%s@%s%s%s", mp
->m_path
? mp
->m_path
: "",
1208 mp
->m_mbox
, mp
->m_host
,
1209 mp
->m_bcc
? "[BCC]" : "",
1210 mp
->m_next
? ",\n\t" : "");
1212 printf ("\nuucp:\t");
1213 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1214 printf ("%s!%s%s%s", mp
->m_host
, mp
->m_mbox
,
1215 mp
->m_bcc
? "[BCC]" : "",
1216 mp
->m_next
? ",\n\t" : "");
1218 printf ("\n\t-- Folder Copies --\nfcc:\t");
1219 for (i
= 0; i
< fccind
; i
++)
1220 printf ("%s%s", fccfold
[i
], i
+ 1 < fccind
? ",\n\t" : "");
1228 struct mailname
*mp
;
1230 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1231 if (annoaux (mp
) == NOTOK
)
1234 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1235 if (annoaux (mp
) == NOTOK
)
1238 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1239 if (annoaux (mp
) == NOTOK
)
1249 annoaux (struct mailname
*mp
)
1252 char buffer
[BUFSIZ
];
1254 snprintf (buffer
, sizeof(buffer
), "%s\n", adrformat (mp
));
1255 i
= strlen (buffer
);
1257 return (write (pfd
, buffer
, i
) == i
? OK
: NOTOK
);
1262 insert_fcc (struct headers
*hdr
, char *pp
)
1266 for (cp
= pp
; isspace ((unsigned char) *cp
); cp
++)
1268 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace ((unsigned char) *pp
); pp
--)
1276 adios (NULL
, "too many %ss", hdr
->value
);
1277 fccfold
[fccind
++] = getcpy (cp
);
1285 make_bcc_file (int dashstuff
)
1291 char *tfile
= NULL
, *program
;
1293 tfile
= m_mktemp2(NULL
, "bccs", NULL
, &out
);
1294 if (tfile
== NULL
) adios("bcc", "unable to create temporary file");
1295 strncpy (bccfil
, tfile
, sizeof(bccfil
));
1297 fprintf (out
, "From: %s\n", fullfrom
);
1298 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1300 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
1302 fprintf (out
, "Subject: %s", subject
);
1303 fprintf (out
, "BCC:\n");
1306 * Use MIME encapsulation for Bcc messages
1312 * Check if any lines in the message clash with the
1313 * prefix for the MIME multipart separator. If there
1314 * is a clash, increment one of the letters in the
1315 * prefix and check again.
1317 if ((cp
= strchr(prefix
, 'a')) == NULL
)
1318 adios (NULL
, "lost prefix start");
1319 while (find_prefix () == NOTOK
) {
1324 adios (NULL
, "can't find a unique delimiter string");
1329 fprintf (out
, "%s: %s\n%s: multipart/digest; boundary=\"",
1330 VRSN_FIELD
, VRSN_VALUE
, TYPE_FIELD
);
1331 fprintf (out
, "%s\"\n\n--%s\n\n", prefix
, prefix
);
1333 fprintf (out
, "\n------- Blind-Carbon-Copy\n\n");
1339 * Do mhl filtering of Bcc messages instead
1340 * of MIME encapsulation.
1342 if (filter
!= NULL
) {
1343 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1347 adios ("fork", "unable to");
1350 dup2 (fileno (out
), 1);
1352 vec
= argsplit(mhlproc
, &program
, &i
);
1353 vec
[i
++] = "-forward";
1358 /* was the flag -[no]dashstuffing specified? */
1360 vec
[i
++] = "-dashstuffing";
1361 else if (dashstuff
< 0)
1362 vec
[i
++] = "-nodashstuffing";
1365 execvp (program
, vec
);
1366 fprintf (stderr
, "unable to exec ");
1371 pidXwait (child_id
, mhlproc
);
1375 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
1376 adios (tmpfil
, "unable to re-open");
1379 * If using MIME encapsulation, or if the -nodashstuffing
1380 * flag was given, then just copy message. Else do
1381 * RFC934 quoting (dashstuffing).
1383 if (mime
|| dashstuff
< 0)
1384 cpydata (fd
, fileno (out
), tmpfil
, bccfil
);
1386 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
1390 fseek (out
, 0L, SEEK_END
);
1392 fprintf (out
, "\n--%s--\n", prefix
);
1394 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
1400 * Scan message to check if any lines clash with
1401 * the prefix of the MIME multipart separator.
1408 char buffer
[BUFSIZ
];
1411 if ((in
= fopen (tmpfil
, "r")) == NULL
)
1412 adios (tmpfil
, "unable to re-open");
1414 while (fgets (buffer
, sizeof(buffer
) - 1, in
))
1415 if (buffer
[0] == '-' && buffer
[1] == '-') {
1418 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1419 if (!isspace ((unsigned char) *cp
))
1422 if (strcmp (buffer
+ 2, prefix
) == 0) {
1433 #define plural(x) (x == 1 ? "" : "s")
1438 if (badadr
&& unkadr
)
1439 die (NULL
, "%d address%s unparsable, %d addressee%s undeliverable",
1440 badadr
, plural (badadr
), unkadr
, plural (badadr
));
1442 die (NULL
, "%d address%s unparsable", badadr
, plural (badadr
));
1444 die (NULL
, "%d addressee%s undeliverable", unkadr
, plural (unkadr
));
1449 do_addresses (int bccque
, int talk
)
1453 struct mailname
*lp
;
1456 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1457 if (lp
->m_bcc
? bccque
: !bccque
) {
1459 printf (" -- Local Recipients --\n");
1460 do_an_address (lp
, talk
);
1465 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1466 if (lp
->m_bcc
? bccque
: !bccque
) {
1468 printf (" -- UUCP Recipients --\n");
1469 do_an_address (lp
, talk
);
1474 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1475 if (lp
->m_bcc
? bccque
: !bccque
) {
1477 printf (" -- Network Recipients --\n");
1478 do_an_address (lp
, talk
);
1484 if (rp_isbad (retval
= sm_waend ()))
1485 die (NULL
, "problem ending addresses; %s", rp_string (retval
));
1490 * MTS-SPECIFIC INTERACTION
1495 * SENDMAIL/SMTP routines
1499 post (char *file
, int bccque
, int talk
, char *envelope
)
1506 if (msgflags
& MINV
)
1507 printf (" -- Posting for %s Recipients --\n",
1508 bccque
? "Blind" : "Sighted");
1510 printf (" -- Posting for All Recipients --\n");
1515 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
1516 char **argp
, *program
;
1519 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1523 adios ("fork", "unable to");
1526 if (freopen( file
, "r", stdin
) == NULL
) {
1527 adios (file
, "can't reopen for sendmail");
1530 argp
= argsplit(sendmail
, &program
, &argc
);
1531 argp
[argc
++] = "-t"; /* read msg for recipients */
1532 argp
[argc
++] = "-i"; /* don't stop on "." */
1534 argp
[argc
++] = "-bv";
1536 argp
[argc
++] = "-v";
1539 execv (program
, argp
);
1540 adios (sendmail
, "can't exec");
1543 pidXwait (child_id
, NULL
);
1547 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1548 verbose
, snoop
, queued
, sasl
,
1549 saslssf
, saslmech
, user
, tls
)) ||
1550 rp_isbad (retval
= sm_winit (envelope
)))
1551 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1553 do_addresses (bccque
, talk
&& verbose
);
1554 if ((fd
= open (file
, O_RDONLY
)) == NOTOK
)
1555 die (file
, "unable to re-open");
1560 sm_end (!(msgflags
& MINV
) || bccque
? OK
: DONE
);
1564 if (msgflags
& MINV
)
1565 printf (" -- %s Recipient Copies Posted --\n",
1566 bccque
? "Blind" : "Sighted");
1568 printf (" -- Recipient Copies Posted --\n");
1576 /* Address Verification */
1579 verify_all_addresses (int talk
, char *envelope
)
1582 struct mailname
*lp
;
1586 if (!whomsw
|| checksw
)
1587 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1588 verbose
, snoop
, queued
, sasl
,
1589 saslssf
, saslmech
, user
, tls
))
1590 || rp_isbad (retval
= sm_winit (envelope
)))
1591 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1593 if (talk
&& !whomsw
)
1594 printf (" -- Address Verification --\n");
1595 if (talk
&& localaddrs
.m_next
)
1596 printf (" -- Local Recipients --\n");
1597 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1598 do_an_address (lp
, talk
);
1600 if (talk
&& uuaddrs
.m_next
)
1601 printf (" -- UUCP Recipients --\n");
1602 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1603 do_an_address (lp
, talk
);
1605 if (talk
&& netaddrs
.m_next
)
1606 printf (" -- Network Recipients --\n");
1607 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1608 do_an_address (lp
, talk
);
1611 if (talk
&& !whomsw
)
1612 printf (" -- Address Verification Successful --\n");
1614 if (!whomsw
|| checksw
)
1623 do_an_address (struct mailname
*lp
, int talk
)
1629 switch (lp
->m_type
) {
1633 strncpy (addr
, mbox
, sizeof(addr
));
1637 mbox
= auxformat (lp
, 0);
1639 snprintf (addr
, sizeof(addr
), "%s!%s", lp
->m_host
, lp
->m_mbox
);
1642 default: /* let SendMail decide if the host is bad */
1645 snprintf (addr
, sizeof(addr
), "%s at %s", mbox
, host
);
1650 printf (" %s%s", addr
, whomsw
&& lp
->m_bcc
? "[BCC]" : "");
1652 if (whomsw
&& !checksw
) {
1660 switch (retval
= sm_wadr (mbox
, host
,
1661 lp
->m_type
!= UUCPHOST
? lp
->m_path
: NULL
)) {
1664 printf ("address ok\n");
1670 fprintf (stderr
, " %s: ", addr
);
1671 fprintf (talk
? stdout
: stderr
, "loses; %s\n",
1672 rp_string (retval
));
1678 fprintf (stderr
, " %s: ", addr
);
1679 die (NULL
, "unexpected response; %s", rp_string (retval
));
1687 do_text (char *file
, int fd
)
1692 lseek (fd
, (off_t
) 0, SEEK_SET
);
1694 while ((state
= read (fd
, buf
, sizeof(buf
))) > 0) {
1695 if (rp_isbad (retval
= sm_wtxt (buf
, state
)))
1696 die (NULL
, "problem writing text; %s\n", rp_string (retval
));
1700 die (file
, "problem reading from");
1702 switch (retval
= sm_wtend ()) {
1708 die (NULL
, "posting failed; %s", rp_string (retval
));
1711 die (NULL
, "unexpected response; %s", rp_string (retval
));
1726 if (msgflags
& MINV
)
1729 if (!whomsw
|| checksw
)
1742 hstat
= SIGNAL2 (SIGHUP
, sigser
);
1743 istat
= SIGNAL2 (SIGINT
, sigser
);
1744 qstat
= SIGNAL2 (SIGQUIT
, sigser
);
1745 tstat
= SIGNAL2 (SIGTERM
, sigser
);
1755 SIGNAL (SIGHUP
, hstat
);
1756 SIGNAL (SIGINT
, istat
);
1757 SIGNAL (SIGQUIT
, qstat
);
1758 SIGNAL (SIGTERM
, tstat
);
1766 p_refile (char *file
)
1774 printf (" -- Filing Folder Copies --\n");
1775 for (i
= 0; i
< fccind
; i
++)
1776 fcc (file
, fccfold
[i
]);
1778 printf (" -- Folder Copies Filed --\n");
1783 * Call the `fileproc' to add the file to the folder.
1787 fcc (char *file
, char *folder
)
1790 int i
, status
, argp
;
1792 char **arglist
, *program
;
1795 printf (" %sFcc %s: ", msgstate
== RESENT
? "Resent-" : "", folder
);
1798 for (i
= 0; (child_id
= fork ()) == NOTOK
&& i
< 5; i
++)
1804 fprintf (stderr
, " %sFcc %s: ",
1805 msgstate
== RESENT
? "Resent-" : "", folder
);
1806 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
1810 /* see if we need to add `+' */
1811 snprintf (fold
, sizeof(fold
), "%s%s",
1812 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
1814 /* now exec the fileproc */
1816 arglist
= argsplit(fileproc
, &program
, &argp
);
1817 arglist
[argp
++] = "-link";
1818 arglist
[argp
++] = "-file";
1819 arglist
[argp
++] = file
;
1820 arglist
[argp
++] = fold
;
1821 arglist
[argp
] = NULL
;
1822 execvp (program
, arglist
);
1826 if ((status
= pidwait (child_id
, OK
))) {
1828 fprintf (stderr
, " %sFcc %s: ",
1829 msgstate
== RESENT
? "Resent-" : "", folder
);
1830 pidstatus (status
, verbose
? stdout
: stderr
, NULL
);
1833 printf ("folder ok\n");
1845 die (char *what
, char *fmt
, ...)
1850 if (msgflags
& MINV
)
1853 if (!whomsw
|| checksw
)
1857 advertise (what
, NULL
, fmt
, ap
);