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 #define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : c)
48 /* In the following array of structures, the numeric second field of the
49 structures (minchars) is apparently used like this:
51 -# : Switch can be abbreviated to # characters; switch hidden in -help.
52 0 : Switch can't be abbreviated; switch shown in -help.
53 # : Switch can be abbreviated to # characters; switch shown in -help. */
55 static struct swit switches
[] = {
57 { "alias aliasfile", 0 },
59 { "check", -5 }, /* interface from whom */
61 { "nocheck", -7 }, /* interface from whom */
65 { "dist", -4 }, /* interface from dist */
67 { "filter filterfile", 0 },
75 { "library directory", -7 }, /* interface from send, whom */
93 { "whom", -4 }, /* interface from whom */
95 { "width columns", 0 },
100 #define BITSTUFFSW 22
101 { "dashstuffing", -12 }, /* should we dashstuff BCC messages? */
102 #define NBITSTUFFSW 23
103 { "nodashstuffing", -14 },
105 { "idanno number", -6 }, /* interface from send */
107 { "client host", -6 },
109 { "server host", 6 }, /* specify alternate SMTP server */
111 { "snoop", -5 }, /* snoop the SMTP transaction */
117 { "sasl", SASLminc(-4) },
119 { "nosasl", SASLminc(-6) },
120 #define SASLMXSSFSW 32
121 { "saslmaxssf", SASLminc(-10) },
122 #define SASLMECHSW 33
123 { "saslmech", SASLminc(-5) },
125 { "user", SASLminc(-4) },
127 { "port server port name/number", 4 },
129 { "tls", TLSminc(-3) },
131 { "notls", TLSminc(-5) },
132 #define FILEPROCSW 38
147 * flags for headers->flags
149 #define HNOP 0x0000 /* just used to keep .set around */
150 #define HBAD 0x0001 /* bad header - don't let it through */
151 #define HADR 0x0002 /* header has an address field */
152 #define HSUB 0x0004 /* Subject: header */
153 #define HTRY 0x0008 /* try to send to addrs on header */
154 #define HBCC 0x0010 /* don't output this header */
155 #define HMNG 0x0020 /* munge this header */
156 #define HNGR 0x0040 /* no groups allowed in this header */
157 #define HFCC 0x0080 /* FCC: type header */
158 #define HNIL 0x0100 /* okay for this header not to have addrs */
159 #define HIGN 0x0200 /* ignore this header */
160 #define HDCC 0x0400 /* another undocumented feature */
163 * flags for headers->set
165 #define MFRM 0x0001 /* we've seen a From: */
166 #define MDAT 0x0002 /* we've seen a Date: */
167 #define MRFM 0x0004 /* we've seen a Resent-From: */
168 #define MVIS 0x0008 /* we've seen sighted addrs */
169 #define MINV 0x0010 /* we've seen blind addrs */
172 static struct headers NHeaders
[] = {
173 { "Return-Path", HBAD
, 0 },
174 { "Received", HBAD
, 0 },
175 { "Reply-To", HADR
|HNGR
, 0 },
176 { "From", HADR
|HNGR
, MFRM
},
177 { "Sender", HADR
|HBAD
, 0 },
179 { "Subject", HSUB
, 0 },
180 { "To", HADR
|HTRY
, MVIS
},
181 { "cc", HADR
|HTRY
, MVIS
},
182 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, MINV
},
183 { "Dcc", HADR
|HTRY
|HDCC
|HNIL
, MVIS
}, /* sorta cc & bcc combined */
184 { "Message-ID", HBAD
, 0 },
189 static struct headers RHeaders
[] = {
190 { "Resent-Reply-To", HADR
|HNGR
, 0 },
191 { "Resent-From", HADR
|HNGR
, MRFM
},
192 { "Resent-Sender", HADR
|HBAD
, 0 },
193 { "Resent-Date", HBAD
, 0 },
194 { "Resent-Subject", HSUB
, 0 },
195 { "Resent-To", HADR
|HTRY
, MVIS
},
196 { "Resent-cc", HADR
|HTRY
, MVIS
},
197 { "Resent-Bcc", HADR
|HTRY
|HBCC
, MINV
},
198 { "Resent-Message-ID", HBAD
, 0 },
199 { "Resent-Fcc", HFCC
, 0 },
200 { "Reply-To", HADR
, 0 },
201 { "From", HADR
|HNGR
, MFRM
},
202 { "Sender", HADR
|HNGR
, 0 },
203 { "Date", HNOP
, MDAT
},
204 { "To", HADR
|HNIL
, 0 },
205 { "cc", HADR
|HNIL
, 0 },
206 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, 0 },
211 static short fccind
= 0; /* index into fccfold[] */
212 static short outputlinelen
= OUTPUTLINELEN
;
214 static int pfd
= NOTOK
; /* fd to write annotation list to */
215 static uid_t myuid
= -1; /* my user id */
216 static gid_t mygid
= -1; /* my group id */
217 static int recipients
= 0; /* how many people will get a copy */
218 static int unkadr
= 0; /* how many of those were unknown */
219 static int badadr
= 0; /* number of bad addrs */
220 static int badmsg
= 0; /* message has bad semantics */
221 static int verbose
= 0; /* spell it out */
222 static int format
= 1; /* format addresses */
223 static int mime
= 0; /* use MIME-style encapsulations for Bcc */
224 static int msgid
= 0; /* add msgid */
225 static int debug
= 0; /* debugging post */
226 static int watch
= 0; /* watch the delivery process */
227 static int whomsw
= 0; /* we are whom not post */
228 static int checksw
= 0; /* whom -check */
229 static int linepos
=0; /* putadr()'s position on the line */
230 static int nameoutput
=0; /* putadr() has output header name */
231 static int sasl
=0; /* Use SASL auth for SMTP */
232 static int saslssf
=-1; /* Our maximum SSF for SASL */
233 static char *saslmech
=NULL
; /* Force use of particular SASL mech */
234 static char *user
=NULL
; /* Authenticate as this user */
235 static char *port
="smtp"; /* Name of server port for SMTP */
236 static int tls
=0; /* Use TLS for encryption */
238 static unsigned msgflags
= 0; /* what we've seen */
242 static int msgstate
= NORMAL
;
244 static time_t tclock
= 0; /* the time we started (more or less) */
246 static SIGNAL_HANDLER hstat
, istat
, qstat
, tstat
;
248 static char tmpfil
[BUFSIZ
];
249 static char bccfil
[BUFSIZ
];
251 static char from
[BUFSIZ
]; /* my network address */
252 static char signature
[BUFSIZ
]; /* my signature */
253 static char *filter
= NULL
; /* the filter for BCC'ing */
254 static char *subject
= NULL
; /* the subject field for BCC'ing */
255 static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
257 static struct headers
*hdrtab
; /* table for the message we're doing */
259 static struct mailname localaddrs
; /* local addrs */
260 static struct mailname netaddrs
; /* network addrs */
261 static struct mailname uuaddrs
; /* uucp addrs */
262 static struct mailname tmpaddrs
; /* temporary queue */
264 static int snoop
= 0;
265 static char *clientsw
= NULL
;
266 static char *serversw
= NULL
;
268 extern struct smtp sm_reply
;
270 static char prefix
[] = "----- =_aaaaaaaaaa";
272 static char *partno
= NULL
;
273 static int queued
= 0;
275 extern boolean draft_from_masquerading
; /* defined in mts.c */
280 static void putfmt (char *, char *, FILE *);
281 static void start_headers (void);
282 static void finish_headers (FILE *);
283 static int get_header (char *, struct headers
*);
284 static int putadr (char *, char *, struct mailname
*, FILE *, unsigned int);
285 static void putgrp (char *, char *, FILE *, unsigned int);
286 static int insert (struct mailname
*);
287 static void pl (void);
288 static void anno (void);
289 static int annoaux (struct mailname
*);
290 static void insert_fcc (struct headers
*, unsigned char *);
291 static void make_bcc_file (int);
292 static void verify_all_addresses (int);
293 static void chkadr (void);
294 static void sigon (void);
295 static void sigoff (void);
296 static void p_refile (char *);
297 static void fcc (char *, char *);
298 static void die (char *, char *, ...);
299 static void post (char *, int, int);
300 static void do_text (char *file
, int fd
);
301 static void do_an_address (struct mailname
*, int);
302 static void do_addresses (int, int);
303 static int find_prefix (void);
307 main (int argc
, char **argv
)
309 int state
, compnum
, dashstuff
= 0;
310 char *cp
, *msg
= NULL
, **argp
, **arguments
;
311 char buf
[BUFSIZ
], name
[NAMESZ
];
315 setlocale(LC_ALL
, "");
317 invo_name
= r1bindex (argv
[0], '/');
319 /* foil search of user profile/context */
320 if (context_foil (NULL
) == -1)
323 mts_init (invo_name
);
324 arguments
= getarguments (invo_name
, argc
, argv
, 0);
327 while ((cp
= *argp
++)) {
329 switch (smatch (++cp
, switches
)) {
331 ambigsw (cp
, switches
);
334 adios (NULL
, "-%s unknown", cp
);
337 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
338 print_help (buf
, switches
, 0);
341 print_version(invo_name
);
345 if (!(cp
= *argp
++) || *cp
== '-')
346 adios (NULL
, "missing argument to %s", argp
[-2]);
347 /* create a minimal context */
348 if (context_foil (cp
) == -1)
353 if (!(cp
= *argp
++) || *cp
== '-')
354 adios (NULL
, "missing argument to %s", argp
[-2]);
355 if ((state
= alias (cp
)) != AK_OK
)
356 adios (NULL
, "aliasing error in %s - %s",
357 cp
, akerror (state
));
376 if (!(filter
= *argp
++) || *filter
== '-')
377 adios (NULL
, "missing argument to %s", argp
[-2]);
432 if (!(cp
= *argp
++) || *cp
== '-')
433 adios (NULL
, "missing argument to %s", argp
[-2]);
434 if ((outputlinelen
= atoi (cp
)) < 10)
435 adios (NULL
, "impossible width %d", outputlinelen
);
439 if (!(cp
= *argp
++) || *cp
== '-')
440 adios (NULL
, "missing argument to %s", argp
[-2]);
441 if ((pfd
= atoi (cp
)) <= 2)
442 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
446 if (!(clientsw
= *argp
++) || *clientsw
== '-')
447 adios (NULL
, "missing argument to %s", argp
[-2]);
450 if (!(serversw
= *argp
++) || *serversw
== '-')
451 adios (NULL
, "missing argument to %s", argp
[-2]);
458 if (!(partno
= *argp
++) || *partno
== '-')
459 adios (NULL
, "missing argument to %s", argp
[-2]);
475 if (!(cp
= *argp
++) || *cp
== '-')
476 adios (NULL
, "missing argument to %s", argp
[-2]);
481 if (!(saslmech
= *argp
++) || *saslmech
== '-')
482 adios (NULL
, "missing argument to %s", argp
[-2]);
486 if (!(user
= *argp
++) || *user
== '-')
487 adios (NULL
, "missing argument to %s", argp
[-2]);
491 if (!(port
= *argp
++) || *port
== '-')
492 adios (NULL
, "missing argument to %s", argp
[-2]);
504 if (!(cp
= *argp
++) || *cp
== '-')
505 adios (NULL
, "missing argument to %s", argp
[-2]);
510 if (!(cp
= *argp
++) || *cp
== '-')
511 adios (NULL
, "missing argument to %s", argp
[-2]);
517 adios (NULL
, "only one message at a time!");
525 adios (NULL
, "usage: %s [switches] file", invo_name
);
527 if (outputlinelen
< 10)
528 adios (NULL
, "impossible width %d", outputlinelen
);
530 if ((in
= fopen (msg
, "r")) == NULL
)
531 adios (msg
, "unable to open");
536 discard (out
= stdout
); /* XXX: reference discard() to help loader */
539 if ((out
= fopen ("/dev/null", "w")) == NULL
)
540 adios ("/dev/null", "unable to open");
542 char *cp
= m_mktemp(m_maildir(invo_name
), NULL
, &out
);
544 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
546 adios ("post", "unable to create temporary file");
549 strncpy(tmpfil
, cp
, sizeof(tmpfil
));
550 chmod (tmpfil
, 0600);
554 hdrtab
= msgstate
== NORMAL
? NHeaders
: RHeaders
;
556 for (compnum
= 1, state
= FLD
;;) {
557 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
)) {
562 cp
= add (buf
, NULL
);
563 while (state
== FLDPLUS
) {
564 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
567 putfmt (name
, cp
, out
);
571 finish_headers (out
);
576 finish_headers (out
);
579 fprintf (out
, "\n%s", buf
);
580 while (state
== BODY
) {
581 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
587 finish_headers (out
);
592 adios (NULL
, "message format error in component #%d", compnum
);
595 adios (NULL
, "getfld() returned %d", state
);
611 /* If we are doing a "whom" check */
613 verify_all_addresses (1);
617 if (msgflags
& MINV
) {
618 make_bcc_file (dashstuff
);
619 if (msgflags
& MVIS
) {
620 verify_all_addresses (verbose
);
621 post (tmpfil
, 0, verbose
);
623 post (bccfil
, 1, verbose
);
626 post (tmpfil
, 0, isatty (1));
633 printf (partno
? "Partial Message #%s Processed\n" : "Message Processed\n",
645 putfmt (char *name
, char *str
, FILE *out
)
647 int count
, grp
, i
, keep
;
650 struct mailname
*mp
= NULL
, *np
= NULL
;
653 while (*str
== ' ' || *str
== '\t')
656 if (msgstate
== NORMAL
&& uprf (name
, "resent")) {
657 advise (NULL
, "illegal header line -- %s:", name
);
662 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
663 fprintf (out
, "%s: %s", name
, str
);
668 if (hdr
->flags
& HIGN
) {
671 if (hdr
->flags
& HBAD
) {
672 advise (NULL
, "illegal header line -- %s:", name
);
676 msgflags
|= (hdr
->set
& ~(MVIS
| MINV
));
678 if (hdr
->flags
& HSUB
)
679 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
680 if (hdr
->flags
& HFCC
) {
681 if ((cp
= strrchr(str
, '\n')))
683 for (cp
= pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
685 insert_fcc (hdr
, pp
);
687 insert_fcc (hdr
, pp
);
691 if (!(hdr
->flags
& HADR
)) {
692 fprintf (out
, "%s: %s", name
, str
);
696 tmpaddrs
.m_next
= NULL
;
697 for (count
= 0; (cp
= getname (str
)); count
++)
698 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
702 tmpaddrs
.m_next
= mp
;
706 if (hdr
->flags
& HTRY
)
712 if (hdr
->flags
& HNIL
)
713 fprintf (out
, "%s: %s", name
, str
);
716 advise (NULL
, "%s: field requires at least one address", name
);
723 nameoutput
= linepos
= 0;
724 snprintf (namep
, sizeof(namep
), "%s%s",
725 (hdr
->flags
& HMNG
) ? "Original-" : "", name
);
727 for (grp
= 0, mp
= tmpaddrs
.m_next
; mp
; mp
= np
)
728 if (mp
->m_nohost
) { /* also used to test (hdr->flags & HTRY) */
729 /* The address doesn't include a host, so it might be an alias. */
730 pp
= akvalue (mp
->m_mbox
); /* do mh alias substitution */
731 qp
= akvisible () ? mp
->m_mbox
: "";
734 putgrp (namep
, np
->m_gname
, out
, hdr
->flags
);
735 while ((cp
= getname (pp
))) {
736 if (!(mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
741 if (draft_from_masquerading
&& ((msgstate
== RESENT
)
743 : (hdr
->set
& MFRM
)))
744 /* The user manually specified a [Resent-]From: address in
745 their draft and the "masquerade:" line in mts.conf
746 doesn't contain "draft_from", so we'll set things up to
747 use the actual email address embedded in the draft
748 [Resent-]From: (after alias substitution, and without the
749 GECOS full name or angle brackets) as the envelope
751 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
753 if (hdr
->flags
& HBCC
)
756 mp
->m_ingrp
= np
->m_ingrp
;
759 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
762 if (putadr (namep
, qp
, mp
, out
, hdr
->flags
))
763 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
772 /* Address includes a host, so no alias substitution is needed. */
773 if (draft_from_masquerading
&& ((msgstate
== RESENT
)
775 : (hdr
->set
& MFRM
)))
776 /* The user manually specified a [Resent-]From: address in
777 their draft and the "masquerade:" line in mts.conf
778 doesn't contain "draft_from", so we'll set things up to
779 use the actual email address embedded in the draft
780 [Resent-]From: (after alias substitution, and without the
781 GECOS full name or angle brackets) as the envelope
783 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
785 if (hdr
->flags
& HBCC
)
788 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
791 keep
= putadr (namep
, "", mp
, out
, hdr
->flags
);
795 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
801 if (grp
> 0 && (hdr
->flags
& HNGR
)) {
802 advise (NULL
, "%s: field does not allow groups", name
);
815 char myhost
[BUFSIZ
], sigbuf
[BUFSIZ
];
822 strncpy (from
, getlocaladdr(), sizeof(from
));
823 strncpy (myhost
, LocalName (0), sizeof(myhost
));
825 for (cp
= myhost
; *cp
; cp
++)
828 if ((cp
= getfullname ()) && *cp
) {
829 strncpy (sigbuf
, cp
, sizeof(sigbuf
));
830 snprintf (signature
, sizeof(signature
), "%s <%s>",
831 sigbuf
, getlocaladdr());
832 if ((cp
= getname (signature
)) == NULL
)
833 adios (NULL
, "getname () failed -- you lose extraordinarily big");
834 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
)) == NULL
)
835 adios (NULL
, "bad signature '%s'", sigbuf
);
840 strncpy (signature
, getlocaladdr(), sizeof(signature
));
846 * Now that we've outputted the header fields in the draft
847 * message, we will now output any remaining header fields
848 * that we need to add/create.
852 finish_headers (FILE *out
)
859 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
861 fprintf (out
, "Message-ID: <%d.%ld@%s>\n",
862 (int) getpid (), (long) tclock
, LocalName (1));
863 if (msgflags
& MFRM
) {
864 /* There was already a From: in the draft. Don't add one. */
865 if (!draft_from_masquerading
)
866 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
867 so we'll reveal the user's actual account@thismachine
868 address in a Sender: header (and use it as the envelope
870 fprintf (out
, "Sender: %s\n", from
);
873 /* Construct a From: header. */
874 fprintf (out
, "From: %s\n", signature
);
878 if (!(msgflags
& MVIS
))
879 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
883 if (!(msgflags
& MDAT
)) {
884 advise (NULL
, "message has no Date: header");
887 if (!(msgflags
& MFRM
)) {
888 advise (NULL
, "message has no From: header");
894 fprintf (out
, "Resent-Date: %s\n", dtime (&tclock
, 0));
896 fprintf (out
, "Resent-Message-ID: <%d.%ld@%s>\n",
897 (int) getpid (), (long) tclock
, LocalName (1));
898 if (msgflags
& MRFM
) {
899 /* There was already a Resent-From: in draft. Don't add one. */
900 if (!draft_from_masquerading
)
901 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
902 so we'll reveal the user's actual account@thismachine
903 address in a Sender: header (and use it as the envelope
905 fprintf (out
, "Resent-Sender: %s\n", from
);
908 /* Construct a Resent-From: header. */
909 fprintf (out
, "Resent-From: %s\n", signature
);
912 if (!(msgflags
& MVIS
))
913 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
918 adios (NULL
, "re-format message and try again");
920 adios (NULL
, "no addressees");
925 get_header (char *header
, struct headers
*table
)
929 for (h
= table
; h
->value
; h
++)
930 if (!mh_strcasecmp (header
, h
->value
))
938 putadr (char *name
, char *aka
, struct mailname
*mp
, FILE *out
, unsigned int flags
)
944 if (mp
->m_mbox
== NULL
|| ((flags
& HTRY
) && !insert (mp
)))
946 if ((flags
& (HBCC
| HDCC
)) || mp
->m_ingrp
)
950 fprintf (out
, "%s: ", name
);
951 linepos
+= (nameoutput
= strlen (name
) + 2);
954 if (*aka
&& mp
->m_type
!= UUCPHOST
&& !mp
->m_pers
)
955 mp
->m_pers
= getcpy (aka
);
958 snprintf (buffer
, sizeof(buffer
), "%s;", mp
->m_gname
);
968 if (linepos
!= nameoutput
) {
969 if (len
+ linepos
+ 2 > outputlinelen
)
970 fprintf (out
, ",\n%*s", linepos
= nameoutput
, "");
980 return (flags
& HTRY
);
985 putgrp (char *name
, char *group
, FILE *out
, unsigned int flags
)
994 fprintf (out
, "%s: ", name
);
995 linepos
+= (nameoutput
= strlen (name
) + 2);
998 cp
= concat (group
, ";", NULL
);
1001 if (linepos
> nameoutput
) {
1002 if (len
+ linepos
+ 2 > outputlinelen
) {
1003 fprintf (out
, ",\n%*s", nameoutput
, "");
1004 linepos
= nameoutput
;
1018 insert (struct mailname
*np
)
1020 struct mailname
*mp
;
1022 if (np
->m_mbox
== NULL
)
1025 for (mp
= np
->m_type
== LOCALHOST
? &localaddrs
1026 : np
->m_type
== UUCPHOST
? &uuaddrs
1030 if (!mh_strcasecmp (np
->m_host
, mp
->m_next
->m_host
)
1031 && !mh_strcasecmp (np
->m_mbox
, mp
->m_next
->m_mbox
)
1032 && np
->m_bcc
== mp
->m_next
->m_bcc
)
1045 struct mailname
*mp
;
1047 printf ("-------\n\t-- Addresses --\nlocal:\t");
1048 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1049 printf ("%s%s%s", mp
->m_mbox
,
1050 mp
->m_bcc
? "[BCC]" : "",
1051 mp
->m_next
? ",\n\t" : "");
1053 printf ("\nnet:\t");
1054 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1055 printf ("%s%s@%s%s%s", mp
->m_path
? mp
->m_path
: "",
1056 mp
->m_mbox
, mp
->m_host
,
1057 mp
->m_bcc
? "[BCC]" : "",
1058 mp
->m_next
? ",\n\t" : "");
1060 printf ("\nuucp:\t");
1061 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1062 printf ("%s!%s%s%s", mp
->m_host
, mp
->m_mbox
,
1063 mp
->m_bcc
? "[BCC]" : "",
1064 mp
->m_next
? ",\n\t" : "");
1066 printf ("\n\t-- Folder Copies --\nfcc:\t");
1067 for (i
= 0; i
< fccind
; i
++)
1068 printf ("%s%s", fccfold
[i
], i
+ 1 < fccind
? ",\n\t" : "");
1076 struct mailname
*mp
;
1078 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1079 if (annoaux (mp
) == NOTOK
)
1082 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1083 if (annoaux (mp
) == NOTOK
)
1086 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1087 if (annoaux (mp
) == NOTOK
)
1097 annoaux (struct mailname
*mp
)
1100 char buffer
[BUFSIZ
];
1102 snprintf (buffer
, sizeof(buffer
), "%s\n", adrformat (mp
));
1103 i
= strlen (buffer
);
1105 return (write (pfd
, buffer
, i
) == i
? OK
: NOTOK
);
1110 insert_fcc (struct headers
*hdr
, unsigned char *pp
)
1114 for (cp
= pp
; isspace (*cp
); cp
++)
1116 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace (*pp
); pp
--)
1124 adios (NULL
, "too many %ss", hdr
->value
);
1125 fccfold
[fccind
++] = getcpy (cp
);
1133 make_bcc_file (int dashstuff
)
1141 tfile
= m_mktemp2(NULL
, "bccs", NULL
, &out
);
1142 if (tfile
== NULL
) adios("bcc", "unable to create temporary file");
1143 chmod (bccfil
, 0600);
1144 strncpy (bccfil
, tfile
, sizeof(bccfil
));
1146 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1148 fprintf (out
, "Message-ID: <%d.%ld@%s>\n",
1149 (int) getpid (), (long) tclock
, LocalName (1));
1150 if (msgflags
& MFRM
) {
1151 /* There was already a From: in the draft. Don't add one. */
1152 if (!draft_from_masquerading
)
1153 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
1154 so we'll reveal the user's actual account@thismachine
1155 address in a Sender: header (and use it as the envelope
1157 fprintf (out
, "Sender: %s\n", from
);
1160 /* Construct a From: header. */
1161 fprintf (out
, "From: %s\n", signature
);
1163 fprintf (out
, "Subject: %s", subject
);
1164 fprintf (out
, "BCC:\n");
1167 * Use MIME encapsulation for Bcc messages
1173 * Check if any lines in the message clash with the
1174 * prefix for the MIME multipart separator. If there
1175 * is a clash, increment one of the letters in the
1176 * prefix and check again.
1178 if ((cp
= strchr(prefix
, 'a')) == NULL
)
1179 adios (NULL
, "lost prefix start");
1180 while (find_prefix () == NOTOK
) {
1185 adios (NULL
, "can't find a unique delimiter string");
1190 fprintf (out
, "%s: %s\n%s: multipart/digest; boundary=\"",
1191 VRSN_FIELD
, VRSN_VALUE
, TYPE_FIELD
);
1192 fprintf (out
, "%s\"\n\n--%s\n\n", prefix
, prefix
);
1194 fprintf (out
, "\n------- Blind-Carbon-Copy\n\n");
1200 * Do mhl filtering of Bcc messages instead
1201 * of MIME encapsulation.
1203 if (filter
!= NULL
) {
1204 vec
[0] = r1bindex (mhlproc
, '/');
1206 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1210 adios ("fork", "unable to");
1213 dup2 (fileno (out
), 1);
1216 vec
[i
++] = "-forward";
1221 /* was the flag -[no]dashstuffing specified? */
1223 vec
[i
++] = "-dashstuffing";
1224 else if (dashstuff
< 0)
1225 vec
[i
++] = "-nodashstuffing";
1228 execvp (mhlproc
, vec
);
1229 fprintf (stderr
, "unable to exec ");
1234 pidXwait (child_id
, mhlproc
);
1238 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
1239 adios (tmpfil
, "unable to re-open");
1242 * If using MIME encapsulation, or if the -nodashstuffing
1243 * flag was given, then just copy message. Else do
1244 * RFC934 quoting (dashstuffing).
1246 if (mime
|| dashstuff
< 0)
1247 cpydata (fd
, fileno (out
), tmpfil
, bccfil
);
1249 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
1253 fseek (out
, 0L, SEEK_END
);
1255 fprintf (out
, "\n--%s--\n", prefix
);
1257 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
1263 * Scan message to check if any lines clash with
1264 * the prefix of the MIME multipart separator.
1271 unsigned char buffer
[BUFSIZ
];
1274 if ((in
= fopen (tmpfil
, "r")) == NULL
)
1275 adios (tmpfil
, "unable to re-open");
1277 while (fgets (buffer
, sizeof(buffer
) - 1, in
))
1278 if (buffer
[0] == '-' && buffer
[1] == '-') {
1281 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1285 if (strcmp (buffer
+ 2, prefix
) == 0) {
1296 #define plural(x) (x == 1 ? "" : "s")
1301 if (badadr
&& unkadr
)
1302 die (NULL
, "%d address%s unparsable, %d addressee%s undeliverable",
1303 badadr
, plural (badadr
), unkadr
, plural (badadr
));
1305 die (NULL
, "%d address%s unparsable", badadr
, plural (badadr
));
1307 die (NULL
, "%d addressee%s undeliverable", unkadr
, plural (unkadr
));
1312 do_addresses (int bccque
, int talk
)
1316 struct mailname
*lp
;
1319 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1320 if (lp
->m_bcc
? bccque
: !bccque
) {
1322 printf (" -- Local Recipients --\n");
1323 do_an_address (lp
, talk
);
1328 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1329 if (lp
->m_bcc
? bccque
: !bccque
) {
1331 printf (" -- UUCP Recipients --\n");
1332 do_an_address (lp
, talk
);
1337 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1338 if (lp
->m_bcc
? bccque
: !bccque
) {
1340 printf (" -- Network Recipients --\n");
1341 do_an_address (lp
, talk
);
1347 if (rp_isbad (retval
= sm_waend ()))
1348 die (NULL
, "problem ending addresses; %s", rp_string (retval
));
1353 * MTS-SPECIFIC INTERACTION
1358 * SENDMAIL/SMTP routines
1362 post (char *file
, int bccque
, int talk
)
1367 onex
= !(msgflags
& MINV
) || bccque
;
1369 if (msgflags
& MINV
)
1370 printf (" -- Posting for %s Recipients --\n",
1371 bccque
? "Blind" : "Sighted");
1373 printf (" -- Posting for All Recipients --\n");
1378 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
, verbose
,
1379 snoop
, onex
, queued
, sasl
, saslssf
,
1380 saslmech
, user
, tls
))
1381 || rp_isbad (retval
= sm_winit (from
)))
1382 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1384 do_addresses (bccque
, talk
&& verbose
);
1385 if ((fd
= open (file
, O_RDONLY
)) == NOTOK
)
1386 die (file
, "unable to re-open");
1391 sm_end (onex
? OK
: DONE
);
1395 if (msgflags
& MINV
)
1396 printf (" -- %s Recipient Copies Posted --\n",
1397 bccque
? "Blind" : "Sighted");
1399 printf (" -- Recipient Copies Posted --\n");
1406 /* Address Verification */
1409 verify_all_addresses (int talk
)
1412 struct mailname
*lp
;
1416 if (!whomsw
|| checksw
)
1417 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1418 verbose
, snoop
, 0, queued
, sasl
,
1419 saslssf
, saslmech
, user
, tls
))
1420 || rp_isbad (retval
= sm_winit (from
)))
1421 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1423 if (talk
&& !whomsw
)
1424 printf (" -- Address Verification --\n");
1425 if (talk
&& localaddrs
.m_next
)
1426 printf (" -- Local Recipients --\n");
1427 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1428 do_an_address (lp
, talk
);
1430 if (talk
&& uuaddrs
.m_next
)
1431 printf (" -- UUCP Recipients --\n");
1432 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1433 do_an_address (lp
, talk
);
1435 if (talk
&& netaddrs
.m_next
)
1436 printf (" -- Network Recipients --\n");
1437 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1438 do_an_address (lp
, talk
);
1441 if (talk
&& !whomsw
)
1442 printf (" -- Address Verification Successful --\n");
1444 if (!whomsw
|| checksw
)
1453 do_an_address (struct mailname
*lp
, int talk
)
1459 switch (lp
->m_type
) {
1463 strncpy (addr
, mbox
, sizeof(addr
));
1467 mbox
= auxformat (lp
, 0);
1469 snprintf (addr
, sizeof(addr
), "%s!%s", lp
->m_host
, lp
->m_mbox
);
1472 default: /* let SendMail decide if the host is bad */
1475 snprintf (addr
, sizeof(addr
), "%s at %s", mbox
, host
);
1480 printf (" %s%s", addr
, whomsw
&& lp
->m_bcc
? "[BCC]" : "");
1482 if (whomsw
&& !checksw
) {
1490 switch (retval
= sm_wadr (mbox
, host
,
1491 lp
->m_type
!= UUCPHOST
? lp
->m_path
: NULL
)) {
1494 printf ("address ok\n");
1500 fprintf (stderr
, " %s: ", addr
);
1501 fprintf (talk
? stdout
: stderr
, "loses; %s\n",
1502 rp_string (retval
));
1508 fprintf (stderr
, " %s: ", addr
);
1509 die (NULL
, "unexpected response; %s", rp_string (retval
));
1517 do_text (char *file
, int fd
)
1522 lseek (fd
, (off_t
) 0, SEEK_SET
);
1524 while ((state
= read (fd
, buf
, sizeof(buf
))) > 0) {
1525 if (rp_isbad (retval
= sm_wtxt (buf
, state
)))
1526 die (NULL
, "problem writing text; %s\n", rp_string (retval
));
1530 die (file
, "problem reading from");
1532 switch (retval
= sm_wtend ()) {
1538 die (NULL
, "posting failed; %s", rp_string (retval
));
1541 die (NULL
, "unexpected response; %s", rp_string (retval
));
1556 if (msgflags
& MINV
)
1559 if (!whomsw
|| checksw
)
1572 hstat
= SIGNAL2 (SIGHUP
, sigser
);
1573 istat
= SIGNAL2 (SIGINT
, sigser
);
1574 qstat
= SIGNAL2 (SIGQUIT
, sigser
);
1575 tstat
= SIGNAL2 (SIGTERM
, sigser
);
1585 SIGNAL (SIGHUP
, hstat
);
1586 SIGNAL (SIGINT
, istat
);
1587 SIGNAL (SIGQUIT
, qstat
);
1588 SIGNAL (SIGTERM
, tstat
);
1596 p_refile (char *file
)
1604 printf (" -- Filing Folder Copies --\n");
1605 for (i
= 0; i
< fccind
; i
++)
1606 fcc (file
, fccfold
[i
]);
1608 printf (" -- Folder Copies Filed --\n");
1613 * Call the `fileproc' to add the file to the folder.
1617 fcc (char *file
, char *folder
)
1624 printf (" %sFcc %s: ", msgstate
== RESENT
? "Resent-" : "", folder
);
1627 for (i
= 0; (child_id
= fork ()) == NOTOK
&& i
< 5; i
++)
1633 fprintf (stderr
, " %sFcc %s: ",
1634 msgstate
== RESENT
? "Resent-" : "", folder
);
1635 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
1639 /* see if we need to add `+' */
1640 snprintf (fold
, sizeof(fold
), "%s%s",
1641 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
1643 /* now exec the fileproc */
1644 execlp (fileproc
, r1bindex (fileproc
, '/'),
1645 "-link", "-file", file
, fold
, NULL
);
1649 if ((status
= pidwait (child_id
, OK
))) {
1651 fprintf (stderr
, " %sFcc %s: ",
1652 msgstate
== RESENT
? "Resent-" : "", folder
);
1653 pidstatus (status
, verbose
? stdout
: stderr
, NULL
);
1656 printf ("folder ok\n");
1668 die (char *what
, char *fmt
, ...)
1673 if (msgflags
& MINV
)
1676 if (!whomsw
|| checksw
)
1680 advertise (what
, NULL
, fmt
, ap
);