]>
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>
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("notls", TLSminc(-5), NTLSSW) \
92 X("fileproc", -4, FILEPROCSW) \
93 X("mhlproc", -3, MHLPROCSW) \
94 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
95 X("messageid localname|random", 2, MESSAGEIDSW) \
97 #define X(sw, minchars, id) id,
98 DEFINE_SWITCH_ENUM(POST
);
101 #define X(sw, minchars, id) { sw, minchars, id },
102 DEFINE_SWITCH_ARRAY(POST
, switches
);
113 * flags for headers->flags
115 #define HNOP 0x0000 /* just used to keep .set around */
116 #define HBAD 0x0001 /* bad header - don't let it through */
117 #define HADR 0x0002 /* header has an address field */
118 #define HSUB 0x0004 /* Subject: header */
119 #define HTRY 0x0008 /* try to send to addrs on header */
120 #define HBCC 0x0010 /* don't output this header, unless MTS_SENDMAIL_PIPE */
121 #define HMNG 0x0020 /* munge this header */
122 #define HNGR 0x0040 /* no groups allowed in this header */
123 #define HFCC 0x0080 /* FCC: type header */
124 #define HNIL 0x0100 /* okay for this header not to have addrs */
125 #define HIGN 0x0200 /* ignore this header */
126 #define HDCC 0x0400 /* another undocumented feature */
127 #define HONE 0x0800 /* Only (zero or) one address allowed */
128 #define HEFM 0x1000 /* Envelope-From: header */
131 * flags for headers->set
133 #define MFRM 0x0001 /* we've seen a From: */
134 #define MDAT 0x0002 /* we've seen a Date: */
135 #define MRFM 0x0004 /* we've seen a Resent-From: */
136 #define MVIS 0x0008 /* we've seen sighted addrs */
137 #define MINV 0x0010 /* we've seen blind addrs */
138 #define MSND 0x0020 /* we've seen a Sender: */
139 #define MRSN 0x0040 /* We've seen a Resent-Sendr:*/
140 #define MEFM 0x0080 /* We've seen Envelope-From: */
143 static struct headers NHeaders
[] = {
144 { "Return-Path", HBAD
, 0 },
145 { "Received", HBAD
, 0 },
146 { "Reply-To", HADR
|HNGR
, 0 },
147 { "From", HADR
|HNGR
, MFRM
},
148 { "Sender", HADR
|HNGR
|HONE
, MSND
},
150 { "Subject", HSUB
, 0 },
151 { "To", HADR
|HTRY
, MVIS
},
152 { "cc", HADR
|HTRY
, MVIS
},
153 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, MINV
},
154 { "Dcc", HADR
|HTRY
|HDCC
|HNIL
, MVIS
}, /* sorta cc & bcc combined */
155 { "Message-ID", HBAD
, 0 },
157 { "Envelope-From", HADR
|HONE
|HEFM
, MEFM
},
161 static struct headers RHeaders
[] = {
162 { "Resent-Reply-To", HADR
|HNGR
, 0 },
163 { "Resent-From", HADR
|HNGR
, MRFM
},
164 { "Resent-Sender", HADR
|HNGR
, MRSN
},
165 { "Resent-Date", HBAD
, 0 },
166 { "Resent-Subject", HSUB
, 0 },
167 { "Resent-To", HADR
|HTRY
, MVIS
},
168 { "Resent-cc", HADR
|HTRY
, MVIS
},
169 { "Resent-Bcc", HADR
|HTRY
|HBCC
, MINV
},
170 { "Resent-Message-ID", HBAD
, 0 },
171 { "Resent-Fcc", HFCC
, 0 },
172 { "Reply-To", HADR
, 0 },
173 { "From", HADR
|HNGR
, MFRM
},
174 { "Sender", HADR
|HNGR
, MSND
},
175 { "Date", HNOP
, MDAT
},
176 { "To", HADR
|HNIL
, 0 },
177 { "cc", HADR
|HNIL
, 0 },
178 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, 0 },
180 { "Envelope-From", HADR
|HONE
|HEFM
, MEFM
},
184 static short fccind
= 0; /* index into fccfold[] */
185 static short outputlinelen
= OUTPUTLINELEN
;
187 static int pfd
= NOTOK
; /* fd to write annotation list to */
188 static int recipients
= 0; /* how many people will get a copy */
189 static int unkadr
= 0; /* how many of those were unknown */
190 static int badadr
= 0; /* number of bad addrs */
191 static int badmsg
= 0; /* message has bad semantics */
192 static int verbose
= 0; /* spell it out */
193 static int format
= 1; /* format addresses */
194 static int mime
= 0; /* use MIME-style encapsulations for Bcc */
195 static int msgid
= 0; /* add msgid */
196 static int debug
= 0; /* debugging post */
197 static int watch
= 0; /* watch the delivery process */
198 static int whomsw
= 0; /* we are whom not post */
199 static int checksw
= 0; /* whom -check */
200 static int linepos
=0; /* putadr()'s position on the line */
201 static int nameoutput
=0; /* putadr() has output header name */
202 static int sasl
=0; /* Use SASL auth for SMTP */
203 static int saslssf
=-1; /* Our maximum SSF for SASL */
204 static char *saslmech
=NULL
; /* Force use of particular SASL mech */
205 static char *user
=NULL
; /* Authenticate as this user */
206 static char *port
="smtp"; /* Name of server port for SMTP */
207 static int tls
=0; /* Use TLS for encryption */
208 static int fromcount
=0; /* Count of addresses on From: header */
209 static int seensender
=0; /* Have we seen a Sender: header? */
211 static unsigned msgflags
= 0; /* what we've seen */
215 static int msgstate
= NORMAL
;
217 static time_t tclock
= 0; /* the time we started (more or less) */
219 static SIGNAL_HANDLER hstat
, istat
, qstat
, tstat
;
221 static char tmpfil
[BUFSIZ
];
222 static char bccfil
[BUFSIZ
];
224 static char from
[BUFSIZ
]; /* my network address */
225 static char sender
[BUFSIZ
]; /* my Sender: header */
226 static char efrom
[BUFSIZ
]; /* my Envelope-From: header */
227 static char fullfrom
[BUFSIZ
]; /* full contents of From header */
228 static char signature
[BUFSIZ
]; /* my signature */
229 static char *filter
= NULL
; /* the filter for BCC'ing */
230 static char *subject
= NULL
; /* the subject field for BCC'ing */
231 static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
233 static struct headers
*hdrtab
; /* table for the message we're doing */
235 static struct mailname localaddrs
; /* local addrs */
236 static struct mailname netaddrs
; /* network addrs */
237 static struct mailname uuaddrs
; /* uucp addrs */
238 static struct mailname tmpaddrs
; /* temporary queue */
240 static int snoop
= 0;
241 static char *clientsw
= NULL
;
242 static char *serversw
= NULL
;
244 extern struct smtp sm_reply
;
246 static char prefix
[] = "----- =_aaaaaaaaaa";
248 static char *partno
= NULL
;
249 static int queued
= 0;
254 static void putfmt (char *, char *, FILE *);
255 static void start_headers (void);
256 static void finish_headers (FILE *);
257 static int get_header (char *, struct headers
*);
258 static int putadr (char *, char *, struct mailname
*, FILE *, unsigned int);
259 static void putgrp (char *, char *, FILE *, unsigned int);
260 static int insert (struct mailname
*);
261 static void pl (void);
262 static void anno (void);
263 static int annoaux (struct mailname
*);
264 static void insert_fcc (struct headers
*, char *);
265 static void make_bcc_file (int);
266 static void verify_all_addresses (int, char *);
267 static void chkadr (void);
268 static void sigon (void);
269 static void sigoff (void);
270 static void p_refile (char *);
271 static void fcc (char *, char *);
272 static void die (char *, char *, ...);
273 static void post (char *, int, int, char *);
274 static void do_text (char *file
, int fd
);
275 static void do_an_address (struct mailname
*, int);
276 static void do_addresses (int, int);
277 static int find_prefix (void);
281 main (int argc
, char **argv
)
283 int state
, compnum
, dashstuff
= 0;
284 char *cp
, *msg
= NULL
, **argp
, **arguments
, *envelope
;
285 char buf
[BUFSIZ
], name
[NAMESZ
];
287 m_getfld_state_t gstate
= 0;
290 setlocale(LC_ALL
, "");
292 invo_name
= r1bindex (argv
[0], '/');
294 /* foil search of user profile/context */
295 if (context_foil (NULL
) == -1)
298 mts_init (invo_name
);
299 arguments
= getarguments (invo_name
, argc
, argv
, 0);
302 while ((cp
= *argp
++)) {
304 switch (smatch (++cp
, switches
)) {
306 ambigsw (cp
, switches
);
309 adios (NULL
, "-%s unknown", cp
);
312 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
313 print_help (buf
, switches
, 0);
316 print_version(invo_name
);
320 if (!(cp
= *argp
++) || *cp
== '-')
321 adios (NULL
, "missing argument to %s", argp
[-2]);
322 /* create a minimal context */
323 if (context_foil (cp
) == -1)
328 if (!(cp
= *argp
++) || *cp
== '-')
329 adios (NULL
, "missing argument to %s", argp
[-2]);
330 if ((state
= alias (cp
)) != AK_OK
)
331 adios (NULL
, "aliasing error in %s - %s",
332 cp
, akerror (state
));
351 if (!(filter
= *argp
++) || *filter
== '-')
352 adios (NULL
, "missing argument to %s", argp
[-2]);
407 if (!(cp
= *argp
++) || *cp
== '-')
408 adios (NULL
, "missing argument to %s", argp
[-2]);
409 if ((outputlinelen
= atoi (cp
)) < 10)
410 adios (NULL
, "impossible width %d", outputlinelen
);
414 if (!(cp
= *argp
++) || *cp
== '-')
415 adios (NULL
, "missing argument to %s", argp
[-2]);
416 if ((pfd
= atoi (cp
)) <= 2)
417 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
421 if (!(clientsw
= *argp
++) || *clientsw
== '-')
422 adios (NULL
, "missing argument to %s", argp
[-2]);
425 if (!(serversw
= *argp
++) || *serversw
== '-')
426 adios (NULL
, "missing argument to %s", argp
[-2]);
433 if (!(partno
= *argp
++) || *partno
== '-')
434 adios (NULL
, "missing argument to %s", argp
[-2]);
450 if (!(cp
= *argp
++) || *cp
== '-')
451 adios (NULL
, "missing argument to %s", argp
[-2]);
456 if (!(saslmech
= *argp
++) || *saslmech
== '-')
457 adios (NULL
, "missing argument to %s", argp
[-2]);
461 if (!(user
= *argp
++) || *user
== '-')
462 adios (NULL
, "missing argument to %s", argp
[-2]);
466 if (!(port
= *argp
++) || *port
== '-')
467 adios (NULL
, "missing argument to %s", argp
[-2]);
479 if (!(cp
= *argp
++) || *cp
== '-')
480 adios (NULL
, "missing argument to %s", argp
[-2]);
485 if (!(cp
= *argp
++) || *cp
== '-')
486 adios (NULL
, "missing argument to %s", argp
[-2]);
491 if (!(cp
= *argp
++) || *cp
== '-')
492 adios (NULL
, "missing argument to %s", argp
[-2]);
493 save_mts_method (cp
);
497 if (!(cp
= *argp
++) || *cp
== '-')
498 adios (NULL
, "missing argument to %s", argp
[-2]);
499 if (save_message_id_style (cp
) != 0)
500 adios (NULL
, "unsupported messageid \"%s\"", cp
);
505 adios (NULL
, "only one message at a time!");
513 adios (NULL
, "usage: %s [switches] file", invo_name
);
515 if (outputlinelen
< 10)
516 adios (NULL
, "impossible width %d", outputlinelen
);
518 if ((in
= fopen (msg
, "r")) == NULL
)
519 adios (msg
, "unable to open");
527 if ((out
= fopen ("/dev/null", "w")) == NULL
)
528 adios ("/dev/null", "unable to open");
530 char *cp
= m_mktemp(m_maildir(invo_name
), NULL
, &out
);
532 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
534 adios ("post", "unable to create temporary file");
537 strncpy(tmpfil
, cp
, sizeof(tmpfil
));
538 chmod (tmpfil
, 0600);
542 hdrtab
= msgstate
== NORMAL
? NHeaders
: RHeaders
;
544 for (compnum
= 1;;) {
545 int bufsz
= sizeof buf
;
546 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
550 cp
= add (buf
, NULL
);
551 while (state
== FLDPLUS
) {
553 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
556 putfmt (name
, cp
, out
);
561 finish_headers (out
);
564 fprintf (out
, "\n%s", buf
);
565 while (state
== BODY
) {
567 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
573 finish_headers (out
);
578 adios (NULL
, "message format error in component #%d", compnum
);
581 adios (NULL
, "getfld() returned %d", state
);
585 m_getfld_state_destroy (&gstate
);
599 * Here's how we decide which address to use as the envelope-from
602 * - If we were given an Envelope-From header, use that.
603 * - If we were given a Sender: address, use that.
604 * - Otherwise, use the address on the From: line
607 if (msgflags
& MEFM
) {
609 } else if (seensender
) {
615 /* If we are doing a "whom" check */
617 /* This won't work with MTS_SENDMAIL_PIPE. */
618 verify_all_addresses (1, envelope
);
622 if (msgflags
& MINV
) {
623 make_bcc_file (dashstuff
);
624 if (msgflags
& MVIS
) {
625 if (sm_mts
!= MTS_SENDMAIL_PIPE
) {
626 /* It would be nice to have support to call
627 verify_all_addresses with MTS_SENDMAIL_PIPE, but
628 that might require running sendmail as root. Note
629 that spost didn't verify addresses. */
630 verify_all_addresses (verbose
, envelope
);
632 post (tmpfil
, 0, verbose
, envelope
);
634 post (bccfil
, 1, verbose
, envelope
);
637 post (tmpfil
, 0, isatty (1), envelope
);
644 printf (partno
? "Partial Message #%s Processed\n" : "Message Processed\n",
656 putfmt (char *name
, char *str
, FILE *out
)
658 int count
, grp
, i
, keep
;
661 struct mailname
*mp
= NULL
, *np
= NULL
;
664 while (*str
== ' ' || *str
== '\t')
667 if (msgstate
== NORMAL
&& uprf (name
, "resent")) {
668 advise (NULL
, "illegal header line -- %s:", name
);
673 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
674 if (strncasecmp (name
, "nmh-", 4)) {
675 fprintf (out
, "%s: %s", name
, str
);
677 /* Filter out all Nmh-* headers, because Norm asked. They
678 should never have reached this point. Warn about any
679 that are non-empty. */
680 if (strcmp (str
, "\n")) {
681 char *newline
= strchr (str
, '\n');
682 if (newline
) *newline
= '\0';
684 advise (NULL
, "ignoring header line -- %s: %s", name
, str
);
693 if (hdr
->flags
& HIGN
) {
696 if (hdr
->flags
& HBAD
) {
697 advise (NULL
, "illegal header line -- %s:", name
);
701 msgflags
|= (hdr
->set
& ~(MVIS
| MINV
));
703 if (hdr
->flags
& HSUB
)
704 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
705 if (hdr
->flags
& HFCC
) {
706 if ((cp
= strrchr(str
, '\n')))
708 for (cp
= pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
710 insert_fcc (hdr
, pp
);
712 insert_fcc (hdr
, pp
);
716 if (!(hdr
->flags
& HADR
)) {
717 fprintf (out
, "%s: %s", name
, str
);
721 tmpaddrs
.m_next
= NULL
;
722 for (count
= 0; (cp
= getname (str
)); count
++)
723 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
727 tmpaddrs
.m_next
= mp
;
731 if (hdr
->flags
& HTRY
)
737 if (hdr
->flags
& HNIL
)
738 fprintf (out
, "%s: %s", name
, str
);
741 * Sender (or Resent-Sender) can have only one address
743 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
744 : (hdr
->set
& MSND
)) {
745 advise (NULL
, "%s: field requires one address", name
);
749 advise (NULL
, "%s: field requires at least one address", name
);
756 if (count
> 1 && (hdr
->flags
& HONE
)) {
757 advise (NULL
, "%s: field only permits one address", name
);
762 nameoutput
= linepos
= 0;
763 snprintf (namep
, sizeof(namep
), "%s%s",
764 (hdr
->flags
& HMNG
) ? "Original-" : "", name
);
766 for (grp
= 0, mp
= tmpaddrs
.m_next
; mp
; mp
= np
)
767 if (mp
->m_nohost
) { /* also used to test (hdr->flags & HTRY) */
768 /* The address doesn't include a host, so it might be an alias. */
769 pp
= akvalue (mp
->m_mbox
); /* do mh alias substitution */
770 qp
= akvisible () ? mp
->m_mbox
: "";
773 putgrp (namep
, np
->m_gname
, out
, hdr
->flags
);
774 while ((cp
= getname (pp
))) {
775 if (!(mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
781 * If it's a From: or Resent-From: header, save the address
782 * for later possible use (as the envelope address for SMTP)
785 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
786 : (hdr
->set
& MFRM
)) {
787 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
788 from
[sizeof(from
) - 1] = '\0';
793 * Also save the Sender: or Resent-Sender: header as well
796 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
797 : (hdr
->set
& MSND
)) {
798 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
799 sender
[sizeof(sender
) - 1] = '\0';
804 * ALSO ... save Envelope-From
807 if (hdr
->set
& MEFM
) {
808 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
809 efrom
[sizeof(efrom
) - 1] = '\0';
812 if (hdr
->flags
& HBCC
)
815 mp
->m_ingrp
= np
->m_ingrp
;
818 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
820 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
821 /* Catch this before sendmail chokes with:
822 "553 List:; syntax illegal for recipient
824 If we wanted to, we could expand out blind
825 aliases and put them in Bcc:, but then
826 they'd have the Blind-Carbon-Copy
829 "blind lists not compatible with"
835 if (putadr (namep
, qp
, mp
, out
, hdr
->flags
))
836 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
845 /* Address includes a host, so no alias substitution is needed. */
848 * If it's a From: or Resent-From header, save the address
849 * for later possible use (as the envelope address for SMTP)
852 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
853 : (hdr
->set
& MFRM
)) {
854 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
859 * Also save the Sender: header as well
862 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
863 : (hdr
->set
& MSND
)) {
864 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
865 sender
[sizeof(sender
) - 1] = '\0';
870 * ALSO ... save Envelope-From
873 if (hdr
->set
& MEFM
) {
874 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
875 efrom
[sizeof(efrom
) - 1] = '\0';
878 if (hdr
->flags
& HBCC
)
881 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
884 keep
= putadr (namep
, "", mp
, out
, hdr
->flags
);
888 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
895 * If this is a From:/Resent-From: header, save the full thing for
896 * later in case we need it for use when constructing a Bcc draft message
899 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
) : (hdr
->set
& MFRM
)) {
900 strncpy(fullfrom
, str
, sizeof(fullfrom
));
901 fullfrom
[sizeof(fullfrom
) - 1] = 0;
903 * Strip off any trailing newlines
906 while (strlen(fullfrom
) > 0 && fullfrom
[strlen(fullfrom
) - 1] == '\n') {
907 fullfrom
[strlen(fullfrom
) - 1] = '\0';
911 if (grp
> 0 && (hdr
->flags
& HNGR
)) {
912 advise (NULL
, "%s: field does not allow groups", name
);
924 char *cp
, sigbuf
[BUFSIZ
];
930 * Probably not necessary, but just in case ...
938 if ((cp
= getfullname ()) && *cp
) {
939 strncpy (sigbuf
, cp
, sizeof(sigbuf
));
940 snprintf (signature
, sizeof(signature
), "%s <%s>",
941 sigbuf
, getlocaladdr());
942 if ((cp
= getname (signature
)) == NULL
)
943 adios (NULL
, "getname () failed -- you lose extraordinarily big");
944 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
)) == NULL
)
945 adios (NULL
, "bad signature '%s'", sigbuf
);
950 strncpy (signature
, getlocaladdr(), sizeof(signature
));
956 * Now that we've outputted the header fields in the draft
957 * message, we will now output any remaining header fields
958 * that we need to add/create.
962 finish_headers (FILE *out
)
966 if (!(msgflags
& MFRM
)) {
968 * A From: header is now required in the draft.
970 advise (NULL
, "message has no From: header");
971 advise (NULL
, "See default components files for examples");
976 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
977 advise (NULL
, "A Sender: or Envelope-From: header is required "
978 "with multiple\nFrom: addresses");
986 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
988 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
990 * If we have multiple From: addresses, make sure we have an
991 * Sender: header. If we don't have one, then generate one
992 * from Envelope-From: (which in this case, cannot be blank)
995 if (fromcount
> 1 && seensender
== 0) {
996 if (efrom
[0] == '\0') {
997 advise (NULL
, "Envelope-From cannot be blank when there "
998 "is multiple From: addresses\nand no Sender: "
1002 fprintf (out
, "Sender: %s\n", efrom
);
1006 if (!(msgflags
& MVIS
))
1007 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
1011 if (!(msgflags
& MDAT
)) {
1012 advise (NULL
, "message has no Date: header");
1015 if (!(msgflags
& MFRM
)) {
1016 advise (NULL
, "message has no From: header");
1019 if (!(msgflags
& MRFM
)) {
1020 advise (NULL
, "message has no Resent-From: header");
1021 advise (NULL
, "See default components files for examples");
1025 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
1026 advise (NULL
, "A Resent-Sender: or Envelope-From: header is "
1027 "required with multiple\nResent-From: addresses");
1035 fprintf (out
, "Resent-Date: %s\n", dtime (&tclock
, 0));
1037 fprintf (out
, "Resent-Message-ID: %s\n",
1038 message_id (tclock
, 0));
1040 * If we have multiple Resent-From: addresses, make sure we have an
1041 * Resent-Sender: header. If we don't have one, then generate one
1042 * from Envelope-From (which in this case, cannot be blank)
1045 if (fromcount
> 1 && seensender
== 0) {
1046 if (efrom
[0] == '\0') {
1047 advise (NULL
, "Envelope-From cannot be blank when there "
1048 "is multiple Resent-From: addresses and no "
1049 "Resent-Sender: header");
1052 fprintf (out
, "Resent-Sender: %s\n", efrom
);
1056 if (!(msgflags
& MVIS
))
1057 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
1062 adios (NULL
, "re-format message and try again");
1064 adios (NULL
, "no addressees");
1069 get_header (char *header
, struct headers
*table
)
1073 for (h
= table
; h
->value
; h
++)
1074 if (!mh_strcasecmp (header
, h
->value
))
1082 putadr (char *name
, char *aka
, struct mailname
*mp
, FILE *out
, unsigned int flags
)
1086 char buffer
[BUFSIZ
];
1088 if (mp
->m_mbox
== NULL
|| ((flags
& HTRY
) && !insert (mp
)))
1090 if (sm_mts
!= MTS_SENDMAIL_PIPE
&&
1091 ((flags
& (HBCC
| HDCC
| HEFM
)) || mp
->m_ingrp
))
1095 fprintf (out
, "%s: ", name
);
1096 linepos
+= (nameoutput
= strlen (name
) + 2);
1099 if (*aka
&& mp
->m_type
!= UUCPHOST
&& !mp
->m_pers
)
1100 mp
->m_pers
= getcpy (aka
);
1103 snprintf (buffer
, sizeof(buffer
), "%s;", mp
->m_gname
);
1106 cp
= adrformat (mp
);
1113 if (linepos
!= nameoutput
) {
1114 if (len
+ linepos
+ 2 > outputlinelen
)
1115 fprintf (out
, ",\n%*s", linepos
= nameoutput
, "");
1125 return (flags
& HTRY
);
1130 putgrp (char *name
, char *group
, FILE *out
, unsigned int flags
)
1135 if (sm_mts
!= MTS_SENDMAIL_PIPE
&& (flags
& HBCC
))
1139 fprintf (out
, "%s: ", name
);
1140 linepos
+= (nameoutput
= strlen (name
) + 2);
1143 cp
= concat (group
, ";", NULL
);
1146 if (linepos
> nameoutput
) {
1147 if (len
+ linepos
+ 2 > outputlinelen
) {
1148 fprintf (out
, ",\n%*s", nameoutput
, "");
1149 linepos
= nameoutput
;
1163 insert (struct mailname
*np
)
1165 struct mailname
*mp
;
1167 if (np
->m_mbox
== NULL
)
1170 for (mp
= np
->m_type
== LOCALHOST
? &localaddrs
1171 : np
->m_type
== UUCPHOST
? &uuaddrs
1175 if (!mh_strcasecmp (np
->m_host
, mp
->m_next
->m_host
)
1176 && !mh_strcasecmp (np
->m_mbox
, mp
->m_next
->m_mbox
)
1177 && np
->m_bcc
== mp
->m_next
->m_bcc
)
1190 struct mailname
*mp
;
1192 printf ("-------\n\t-- Addresses --\nlocal:\t");
1193 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1194 printf ("%s%s%s", mp
->m_mbox
,
1195 mp
->m_bcc
? "[BCC]" : "",
1196 mp
->m_next
? ",\n\t" : "");
1198 printf ("\nnet:\t");
1199 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1200 printf ("%s%s@%s%s%s", mp
->m_path
? mp
->m_path
: "",
1201 mp
->m_mbox
, mp
->m_host
,
1202 mp
->m_bcc
? "[BCC]" : "",
1203 mp
->m_next
? ",\n\t" : "");
1205 printf ("\nuucp:\t");
1206 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1207 printf ("%s!%s%s%s", mp
->m_host
, mp
->m_mbox
,
1208 mp
->m_bcc
? "[BCC]" : "",
1209 mp
->m_next
? ",\n\t" : "");
1211 printf ("\n\t-- Folder Copies --\nfcc:\t");
1212 for (i
= 0; i
< fccind
; i
++)
1213 printf ("%s%s", fccfold
[i
], i
+ 1 < fccind
? ",\n\t" : "");
1221 struct mailname
*mp
;
1223 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1224 if (annoaux (mp
) == NOTOK
)
1227 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1228 if (annoaux (mp
) == NOTOK
)
1231 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1232 if (annoaux (mp
) == NOTOK
)
1242 annoaux (struct mailname
*mp
)
1245 char buffer
[BUFSIZ
];
1247 snprintf (buffer
, sizeof(buffer
), "%s\n", adrformat (mp
));
1248 i
= strlen (buffer
);
1250 return (write (pfd
, buffer
, i
) == i
? OK
: NOTOK
);
1255 insert_fcc (struct headers
*hdr
, char *pp
)
1259 for (cp
= pp
; isspace ((unsigned char) *cp
); cp
++)
1261 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace ((unsigned char) *pp
); pp
--)
1269 adios (NULL
, "too many %ss", hdr
->value
);
1270 fccfold
[fccind
++] = getcpy (cp
);
1278 make_bcc_file (int dashstuff
)
1284 char *tfile
= NULL
, *program
;
1286 tfile
= m_mktemp2(NULL
, "bccs", NULL
, &out
);
1287 if (tfile
== NULL
) adios("bcc", "unable to create temporary file");
1288 strncpy (bccfil
, tfile
, sizeof(bccfil
));
1290 fprintf (out
, "From: %s\n", fullfrom
);
1291 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1293 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
1295 fprintf (out
, "Subject: %s", subject
);
1296 fprintf (out
, "BCC:\n");
1299 * Use MIME encapsulation for Bcc messages
1305 * Check if any lines in the message clash with the
1306 * prefix for the MIME multipart separator. If there
1307 * is a clash, increment one of the letters in the
1308 * prefix and check again.
1310 if ((cp
= strchr(prefix
, 'a')) == NULL
)
1311 adios (NULL
, "lost prefix start");
1312 while (find_prefix () == NOTOK
) {
1317 adios (NULL
, "can't find a unique delimiter string");
1322 fprintf (out
, "%s: %s\n%s: multipart/digest; boundary=\"",
1323 VRSN_FIELD
, VRSN_VALUE
, TYPE_FIELD
);
1324 fprintf (out
, "%s\"\n\n--%s\n\n", prefix
, prefix
);
1326 fprintf (out
, "\n------- Blind-Carbon-Copy\n\n");
1332 * Do mhl filtering of Bcc messages instead
1333 * of MIME encapsulation.
1335 if (filter
!= NULL
) {
1336 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1340 adios ("fork", "unable to");
1343 dup2 (fileno (out
), 1);
1345 vec
= argsplit(mhlproc
, &program
, &i
);
1346 vec
[i
++] = "-forward";
1351 /* was the flag -[no]dashstuffing specified? */
1353 vec
[i
++] = "-dashstuffing";
1354 else if (dashstuff
< 0)
1355 vec
[i
++] = "-nodashstuffing";
1358 execvp (program
, vec
);
1359 fprintf (stderr
, "unable to exec ");
1364 pidXwait (child_id
, mhlproc
);
1368 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
1369 adios (tmpfil
, "unable to re-open");
1372 * If using MIME encapsulation, or if the -nodashstuffing
1373 * flag was given, then just copy message. Else do
1374 * RFC934 quoting (dashstuffing).
1376 if (mime
|| dashstuff
< 0)
1377 cpydata (fd
, fileno (out
), tmpfil
, bccfil
);
1379 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
1383 fseek (out
, 0L, SEEK_END
);
1385 fprintf (out
, "\n--%s--\n", prefix
);
1387 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
1393 * Scan message to check if any lines clash with
1394 * the prefix of the MIME multipart separator.
1401 char buffer
[BUFSIZ
];
1404 if ((in
= fopen (tmpfil
, "r")) == NULL
)
1405 adios (tmpfil
, "unable to re-open");
1407 while (fgets (buffer
, sizeof(buffer
) - 1, in
))
1408 if (buffer
[0] == '-' && buffer
[1] == '-') {
1411 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1412 if (!isspace ((unsigned char) *cp
))
1415 if (strcmp (buffer
+ 2, prefix
) == 0) {
1426 #define plural(x) (x == 1 ? "" : "s")
1431 if (badadr
&& unkadr
)
1432 die (NULL
, "%d address%s unparsable, %d addressee%s undeliverable",
1433 badadr
, plural (badadr
), unkadr
, plural (badadr
));
1435 die (NULL
, "%d address%s unparsable", badadr
, plural (badadr
));
1437 die (NULL
, "%d addressee%s undeliverable", unkadr
, plural (unkadr
));
1442 do_addresses (int bccque
, int talk
)
1446 struct mailname
*lp
;
1449 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1450 if (lp
->m_bcc
? bccque
: !bccque
) {
1452 printf (" -- Local Recipients --\n");
1453 do_an_address (lp
, talk
);
1458 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1459 if (lp
->m_bcc
? bccque
: !bccque
) {
1461 printf (" -- UUCP Recipients --\n");
1462 do_an_address (lp
, talk
);
1467 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1468 if (lp
->m_bcc
? bccque
: !bccque
) {
1470 printf (" -- Network Recipients --\n");
1471 do_an_address (lp
, talk
);
1477 if (rp_isbad (retval
= sm_waend ()))
1478 die (NULL
, "problem ending addresses; %s", rp_string (retval
));
1483 * MTS-SPECIFIC INTERACTION
1488 * SENDMAIL/SMTP routines
1492 post (char *file
, int bccque
, int talk
, char *envelope
)
1499 if (msgflags
& MINV
)
1500 printf (" -- Posting for %s Recipients --\n",
1501 bccque
? "Blind" : "Sighted");
1503 printf (" -- Posting for All Recipients --\n");
1508 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
1509 char **argp
, *program
;
1512 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1516 adios ("fork", "unable to");
1519 if (freopen( file
, "r", stdin
) == NULL
) {
1520 adios (file
, "can't reopen for sendmail");
1523 argp
= argsplit(sendmail
, &program
, &argc
);
1524 argp
[argc
++] = "-t"; /* read msg for recipients */
1525 argp
[argc
++] = "-i"; /* don't stop on "." */
1527 argp
[argc
++] = "-bv";
1529 argp
[argc
++] = "-v";
1532 execv (program
, argp
);
1533 adios (sendmail
, "can't exec");
1536 pidXwait (child_id
, NULL
);
1540 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1541 verbose
, snoop
, queued
, sasl
,
1542 saslssf
, saslmech
, user
, tls
)) ||
1543 rp_isbad (retval
= sm_winit (envelope
)))
1544 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1546 do_addresses (bccque
, talk
&& verbose
);
1547 if ((fd
= open (file
, O_RDONLY
)) == NOTOK
)
1548 die (file
, "unable to re-open");
1553 sm_end (!(msgflags
& MINV
) || bccque
? OK
: DONE
);
1557 if (msgflags
& MINV
)
1558 printf (" -- %s Recipient Copies Posted --\n",
1559 bccque
? "Blind" : "Sighted");
1561 printf (" -- Recipient Copies Posted --\n");
1569 /* Address Verification */
1572 verify_all_addresses (int talk
, char *envelope
)
1575 struct mailname
*lp
;
1579 if (!whomsw
|| checksw
)
1580 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1581 verbose
, snoop
, queued
, sasl
,
1582 saslssf
, saslmech
, user
, tls
))
1583 || rp_isbad (retval
= sm_winit (envelope
)))
1584 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1586 if (talk
&& !whomsw
)
1587 printf (" -- Address Verification --\n");
1588 if (talk
&& localaddrs
.m_next
)
1589 printf (" -- Local Recipients --\n");
1590 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1591 do_an_address (lp
, talk
);
1593 if (talk
&& uuaddrs
.m_next
)
1594 printf (" -- UUCP Recipients --\n");
1595 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1596 do_an_address (lp
, talk
);
1598 if (talk
&& netaddrs
.m_next
)
1599 printf (" -- Network Recipients --\n");
1600 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1601 do_an_address (lp
, talk
);
1604 if (talk
&& !whomsw
)
1605 printf (" -- Address Verification Successful --\n");
1607 if (!whomsw
|| checksw
)
1616 do_an_address (struct mailname
*lp
, int talk
)
1622 switch (lp
->m_type
) {
1626 strncpy (addr
, mbox
, sizeof(addr
));
1630 mbox
= auxformat (lp
, 0);
1632 snprintf (addr
, sizeof(addr
), "%s!%s", lp
->m_host
, lp
->m_mbox
);
1635 default: /* let SendMail decide if the host is bad */
1638 snprintf (addr
, sizeof(addr
), "%s at %s", mbox
, host
);
1643 printf (" %s%s", addr
, whomsw
&& lp
->m_bcc
? "[BCC]" : "");
1645 if (whomsw
&& !checksw
) {
1653 switch (retval
= sm_wadr (mbox
, host
,
1654 lp
->m_type
!= UUCPHOST
? lp
->m_path
: NULL
)) {
1657 printf ("address ok\n");
1663 fprintf (stderr
, " %s: ", addr
);
1664 fprintf (talk
? stdout
: stderr
, "loses; %s\n",
1665 rp_string (retval
));
1671 fprintf (stderr
, " %s: ", addr
);
1672 die (NULL
, "unexpected response; %s", rp_string (retval
));
1680 do_text (char *file
, int fd
)
1685 lseek (fd
, (off_t
) 0, SEEK_SET
);
1687 while ((state
= read (fd
, buf
, sizeof(buf
))) > 0) {
1688 if (rp_isbad (retval
= sm_wtxt (buf
, state
)))
1689 die (NULL
, "problem writing text; %s\n", rp_string (retval
));
1693 die (file
, "problem reading from");
1695 switch (retval
= sm_wtend ()) {
1701 die (NULL
, "posting failed; %s", rp_string (retval
));
1704 die (NULL
, "unexpected response; %s", rp_string (retval
));
1719 if (msgflags
& MINV
)
1722 if (!whomsw
|| checksw
)
1735 hstat
= SIGNAL2 (SIGHUP
, sigser
);
1736 istat
= SIGNAL2 (SIGINT
, sigser
);
1737 qstat
= SIGNAL2 (SIGQUIT
, sigser
);
1738 tstat
= SIGNAL2 (SIGTERM
, sigser
);
1748 SIGNAL (SIGHUP
, hstat
);
1749 SIGNAL (SIGINT
, istat
);
1750 SIGNAL (SIGQUIT
, qstat
);
1751 SIGNAL (SIGTERM
, tstat
);
1759 p_refile (char *file
)
1767 printf (" -- Filing Folder Copies --\n");
1768 for (i
= 0; i
< fccind
; i
++)
1769 fcc (file
, fccfold
[i
]);
1771 printf (" -- Folder Copies Filed --\n");
1776 * Call the `fileproc' to add the file to the folder.
1780 fcc (char *file
, char *folder
)
1783 int i
, status
, argp
;
1785 char **arglist
, *program
;
1788 printf (" %sFcc %s: ", msgstate
== RESENT
? "Resent-" : "", folder
);
1791 for (i
= 0; (child_id
= fork ()) == NOTOK
&& i
< 5; i
++)
1797 fprintf (stderr
, " %sFcc %s: ",
1798 msgstate
== RESENT
? "Resent-" : "", folder
);
1799 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
1803 /* see if we need to add `+' */
1804 snprintf (fold
, sizeof(fold
), "%s%s",
1805 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
1807 /* now exec the fileproc */
1809 arglist
= argsplit(fileproc
, &program
, &argp
);
1810 arglist
[argp
++] = "-link";
1811 arglist
[argp
++] = "-file";
1812 arglist
[argp
++] = file
;
1813 arglist
[argp
++] = fold
;
1814 arglist
[argp
] = NULL
;
1815 execvp (program
, arglist
);
1819 if ((status
= pidwait (child_id
, OK
))) {
1821 fprintf (stderr
, " %sFcc %s: ",
1822 msgstate
== RESENT
? "Resent-" : "", folder
);
1823 pidstatus (status
, verbose
? stdout
: stderr
, NULL
);
1826 printf ("folder ok\n");
1838 die (char *what
, char *fmt
, ...)
1843 if (msgflags
& MINV
)
1846 if (!whomsw
|| checksw
)
1850 advertise (what
, NULL
, fmt
, ap
);