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>
26 #ifdef TIME_WITH_SYS_TIME
27 # include <sys/time.h>
30 # ifdef TM_IN_SYS_TIME
31 # include <sys/time.h>
38 # include <mts/smtp/smtp.h>
42 # define SASLminc(a) (a)
43 #else /* CYRUS_SASL */
44 # define SASLminc(a) 0
45 #endif /* CYRUS_SASL */
48 # define TLSminc(a) (a)
49 #else /* TLS_SUPPORT */
51 #endif /* TLS_SUPPORT */
53 #define FCCS 10 /* max number of fccs allowed */
55 #define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : c)
57 /* In the following array of structures, the numeric second field of the
58 structures (minchars) is apparently used like this:
60 -# : Switch can be abbreviated to # characters; switch hidden in -help.
61 0 : Switch can't be abbreviated; switch shown in -help.
62 # : Switch can be abbreviated to # characters; switch shown in -help. */
64 static struct swit switches
[] = {
66 { "alias aliasfile", 0 },
68 { "check", -5 }, /* interface from whom */
70 { "nocheck", -7 }, /* interface from whom */
74 { "dist", -4 }, /* interface from dist */
76 { "filter filterfile", 0 },
84 { "library directory", -7 }, /* interface from send, whom */
102 { "whom", -4 }, /* interface from whom */
104 { "width columns", 0 },
109 #define BITSTUFFSW 22
110 { "dashstuffing", -12 }, /* should we dashstuff BCC messages? */
111 #define NBITSTUFFSW 23
112 { "nodashstuffing", -14 },
114 { "mail", -4 }, /* specify MAIL smtp mode */
116 { "saml", -4 }, /* specify SAML smtp mode */
118 { "send", -4 }, /* specify SEND smtp mode */
120 { "soml", -4 }, /* specify SOML smtp mode */
122 { "idanno number", -6 }, /* interface from send */
124 { "deliver address-list", -7 },
126 { "client host", -6 },
128 { "server host", -6 }, /* specify alternate SMTP server */
130 { "snoop", -5 }, /* snoop the SMTP transaction */
132 { "fill-in file", -7 },
140 { "sasl", SASLminc(-4) },
141 #define SASLMECHSW 38
142 { "saslmech", SASLminc(-5) },
144 { "user", SASLminc(-4) },
146 { "port server port name/number", 4 },
148 { "tls", TLSminc(-3) },
149 #define FILEPROCSW 42
164 * flags for headers->flags
166 #define HNOP 0x0000 /* just used to keep .set around */
167 #define HBAD 0x0001 /* bad header - don't let it through */
168 #define HADR 0x0002 /* header has an address field */
169 #define HSUB 0x0004 /* Subject: header */
170 #define HTRY 0x0008 /* try to send to addrs on header */
171 #define HBCC 0x0010 /* don't output this header */
172 #define HMNG 0x0020 /* munge this header */
173 #define HNGR 0x0040 /* no groups allowed in this header */
174 #define HFCC 0x0080 /* FCC: type header */
175 #define HNIL 0x0100 /* okay for this header not to have addrs */
176 #define HIGN 0x0200 /* ignore this header */
177 #define HDCC 0x0400 /* another undocumented feature */
180 * flags for headers->set
182 #define MFRM 0x0001 /* we've seen a From: */
183 #define MDAT 0x0002 /* we've seen a Date: */
184 #define MRFM 0x0004 /* we've seen a Resent-From: */
185 #define MVIS 0x0008 /* we've seen sighted addrs */
186 #define MINV 0x0010 /* we've seen blind addrs */
189 static struct headers NHeaders
[] = {
190 { "Return-Path", HBAD
, 0 },
191 { "Received", HBAD
, 0 },
192 { "Reply-To", HADR
|HNGR
, 0 },
193 { "From", HADR
|HNGR
, MFRM
},
194 { "Sender", HADR
|HBAD
, 0 },
196 { "Subject", HSUB
, 0 },
197 { "To", HADR
|HTRY
, MVIS
},
198 { "cc", HADR
|HTRY
, MVIS
},
199 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, MINV
},
200 { "Dcc", HADR
|HTRY
|HDCC
|HNIL
, MVIS
}, /* sorta cc & bcc combined */
201 { "Message-ID", HBAD
, 0 },
206 static struct headers RHeaders
[] = {
207 { "Resent-Reply-To", HADR
|HNGR
, 0 },
208 { "Resent-From", HADR
|HNGR
, MRFM
},
209 { "Resent-Sender", HADR
|HBAD
, 0 },
210 { "Resent-Date", HBAD
, 0 },
211 { "Resent-Subject", HSUB
, 0 },
212 { "Resent-To", HADR
|HTRY
, MVIS
},
213 { "Resent-cc", HADR
|HTRY
, MVIS
},
214 { "Resent-Bcc", HADR
|HTRY
|HBCC
, MINV
},
215 { "Resent-Message-ID", HBAD
, 0 },
216 { "Resent-Fcc", HFCC
, 0 },
217 { "Reply-To", HADR
, 0 },
218 { "From", HADR
|HNGR
, MFRM
},
219 { "Sender", HADR
|HNGR
, 0 },
220 { "Date", HNOP
, MDAT
},
221 { "To", HADR
|HNIL
, 0 },
222 { "cc", HADR
|HNIL
, 0 },
223 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, 0 },
228 static short fccind
= 0; /* index into fccfold[] */
229 static short outputlinelen
= OUTPUTLINELEN
;
231 static int pfd
= NOTOK
; /* fd to write annotation list to */
232 static uid_t myuid
= -1; /* my user id */
233 static gid_t mygid
= -1; /* my group id */
234 static int recipients
= 0; /* how many people will get a copy */
235 static int unkadr
= 0; /* how many of those were unknown */
236 static int badadr
= 0; /* number of bad addrs */
237 static int badmsg
= 0; /* message has bad semantics */
238 static int verbose
= 0; /* spell it out */
239 static int format
= 1; /* format addresses */
240 static int mime
= 0; /* use MIME-style encapsulations for Bcc */
241 static int msgid
= 0; /* add msgid */
242 static int debug
= 0; /* debugging post */
243 static int watch
= 0; /* watch the delivery process */
244 static int whomsw
= 0; /* we are whom not post */
245 static int checksw
= 0; /* whom -check */
246 static int linepos
=0; /* putadr()'s position on the line */
247 static int nameoutput
=0; /* putadr() has output header name */
248 static int sasl
=0; /* Use SASL auth for SMTP */
249 static char *saslmech
=NULL
; /* Force use of particular SASL mech */
250 static char *user
=NULL
; /* Authenticate as this user */
251 static char *port
="smtp"; /* Name of server port for SMTP */
252 static int tls
=0; /* Use TLS for encryption */
254 static unsigned msgflags
= 0; /* what we've seen */
258 static int msgstate
= NORMAL
;
260 static time_t tclock
= 0; /* the time we started (more or less) */
262 static SIGNAL_HANDLER hstat
, istat
, qstat
, tstat
;
264 static char tmpfil
[BUFSIZ
];
265 static char bccfil
[BUFSIZ
];
267 static char from
[BUFSIZ
]; /* my network address */
268 static char signature
[BUFSIZ
]; /* my signature */
269 static char *filter
= NULL
; /* the filter for BCC'ing */
270 static char *subject
= NULL
; /* the subject field for BCC'ing */
271 static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
273 static struct headers
*hdrtab
; /* table for the message we're doing */
275 static struct mailname localaddrs
={NULL
}; /* local addrs */
276 static struct mailname netaddrs
={NULL
}; /* network addrs */
277 static struct mailname uuaddrs
={NULL
}; /* uucp addrs */
278 static struct mailname tmpaddrs
={NULL
}; /* temporary queue */
281 static int snoop
= 0;
282 static int smtpmode
= S_MAIL
;
283 static char *clientsw
= NULL
;
284 static char *serversw
= NULL
;
286 extern struct smtp sm_reply
;
289 static char prefix
[] = "----- =_aaaaaaaaaa";
291 static int fill_up
= 0;
292 static char *fill_in
= NULL
;
293 static char *partno
= NULL
;
294 static int queued
= 0;
296 extern boolean draft_from_masquerading
; /* defined in mts.c */
301 static void putfmt (char *, char *, FILE *);
302 static void start_headers (void);
303 static void finish_headers (FILE *);
304 static int get_header (char *, struct headers
*);
305 static int putadr (char *, char *, struct mailname
*, FILE *, unsigned int);
306 static void putgrp (char *, char *, FILE *, unsigned int);
307 static int insert (struct mailname
*);
308 static void pl (void);
309 static void anno (void);
310 static int annoaux (struct mailname
*);
311 static void insert_fcc (struct headers
*, unsigned char *);
312 static void make_bcc_file (int);
313 static void verify_all_addresses (int);
314 static void chkadr (void);
315 static void sigon (void);
316 static void sigoff (void);
317 static void p_refile (char *);
318 static void fcc (char *, char *);
319 static void die (char *, char *, ...);
320 static void post (char *, int, int);
321 static void do_text (char *file
, int fd
);
322 static void do_an_address (struct mailname
*, int);
323 static void do_addresses (int, int);
324 static int find_prefix (void);
328 main (int argc
, char **argv
)
330 int state
, compnum
, dashstuff
= 0;
331 char *cp
, *msg
= NULL
, **argp
, **arguments
;
332 char buf
[BUFSIZ
], name
[NAMESZ
];
336 setlocale(LC_ALL
, "");
338 invo_name
= r1bindex (argv
[0], '/');
340 /* foil search of user profile/context */
341 if (context_foil (NULL
) == -1)
344 mts_init (invo_name
);
345 arguments
= getarguments (invo_name
, argc
, argv
, 0);
348 while ((cp
= *argp
++)) {
350 switch (smatch (++cp
, switches
)) {
352 ambigsw (cp
, switches
);
355 adios (NULL
, "-%s unknown", cp
);
358 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
359 print_help (buf
, switches
, 0);
362 print_version(invo_name
);
366 if (!(cp
= *argp
++) || *cp
== '-')
367 adios (NULL
, "missing argument to %s", argp
[-2]);
368 /* create a minimal context */
369 if (context_foil (cp
) == -1)
374 if (!(cp
= *argp
++) || *cp
== '-')
375 adios (NULL
, "missing argument to %s", argp
[-2]);
376 if ((state
= alias (cp
)) != AK_OK
)
377 adios (NULL
, "aliasing error in %s - %s",
378 cp
, akerror (state
));
397 if (!(filter
= *argp
++) || *filter
== '-')
398 adios (NULL
, "missing argument to %s", argp
[-2]);
453 if (!(cp
= *argp
++) || *cp
== '-')
454 adios (NULL
, "missing argument to %s", argp
[-2]);
455 if ((outputlinelen
= atoi (cp
)) < 10)
456 adios (NULL
, "impossible width %d", outputlinelen
);
460 if (!(cp
= *argp
++) || *cp
== '-')
461 adios (NULL
, "missing argument to %s", argp
[-2]);
462 if ((pfd
= atoi (cp
)) <= 2)
463 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
467 if (!(cp
= *argp
++) || *cp
== '-')
468 adios (NULL
, "missing argument to %s", argp
[-2]);
474 if (!(cp
= *argp
++) || *cp
== '-')
475 adios (NULL
, "missing argument to %s", argp
[-2]);
494 if (!(clientsw
= *argp
++) || *clientsw
== '-')
495 adios (NULL
, "missing argument to %s", argp
[-2]);
498 if (!(serversw
= *argp
++) || *serversw
== '-')
499 adios (NULL
, "missing argument to %s", argp
[-2]);
507 if (!(fill_in
= *argp
++) || *fill_in
== '-')
508 adios (NULL
, "missing argument to %s", argp
[-2]);
514 if (!(partno
= *argp
++) || *partno
== '-')
515 adios (NULL
, "missing argument to %s", argp
[-2]);
527 if (!(saslmech
= *argp
++) || *saslmech
== '-')
528 adios (NULL
, "missing argument to %s", argp
[-2]);
532 if (!(user
= *argp
++) || *user
== '-')
533 adios (NULL
, "missing argument to %s", argp
[-2]);
537 if (!(port
= *argp
++) || *port
== '-')
538 adios (NULL
, "missing argument to %s", argp
[-2]);
546 if (!(cp
= *argp
++) || *cp
== '-')
547 adios (NULL
, "missing argument to %s", argp
[-2]);
552 if (!(cp
= *argp
++) || *cp
== '-')
553 adios (NULL
, "missing argument to %s", argp
[-2]);
559 adios (NULL
, "only one message at a time!");
567 adios (NULL
, "usage: %s [switches] file", invo_name
);
569 if (outputlinelen
< 10)
570 adios (NULL
, "impossible width %d", outputlinelen
);
572 if ((in
= fopen (msg
, "r")) == NULL
)
573 adios (msg
, "unable to open");
578 discard (out
= stdout
); /* XXX: reference discard() to help loader */
581 if ((out
= fopen (fill_in
? fill_in
: "/dev/null", "w")) == NULL
)
582 adios ("/dev/null", "unable to open");
584 char *cp
= m_mktemp(m_maildir(invo_name
), NULL
, &out
);
586 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
588 adios ("post", "unable to create temporary file");
591 strncpy(tmpfil
, cp
, sizeof(tmpfil
));
592 chmod (tmpfil
, 0600);
596 hdrtab
= msgstate
== NORMAL
? NHeaders
: RHeaders
;
598 for (compnum
= 1, state
= FLD
;;) {
599 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
)) {
604 cp
= add (buf
, NULL
);
605 while (state
== FLDPLUS
) {
606 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
609 putfmt (name
, cp
, out
);
613 finish_headers (out
);
618 finish_headers (out
);
619 if (whomsw
&& !fill_in
)
621 fprintf (out
, "\n%s", buf
);
622 while (state
== BODY
) {
623 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
629 finish_headers (out
);
634 adios (NULL
, "message format error in component #%d", compnum
);
637 adios (NULL
, "getfld() returned %d", state
);
653 /* If we are doing a "whom" check */
656 verify_all_addresses (1);
660 if (msgflags
& MINV
) {
661 make_bcc_file (dashstuff
);
662 if (msgflags
& MVIS
) {
663 verify_all_addresses (verbose
);
664 post (tmpfil
, 0, verbose
);
666 post (bccfil
, 1, verbose
);
669 post (tmpfil
, 0, isatty (1));
676 printf (partno
? "Partial Message #%s Processed\n" : "Message Processed\n",
688 putfmt (char *name
, char *str
, FILE *out
)
690 int count
, grp
, i
, keep
;
693 struct mailname
*mp
= NULL
, *np
= NULL
;
696 while (*str
== ' ' || *str
== '\t')
699 if (msgstate
== NORMAL
&& uprf (name
, "resent")) {
700 advise (NULL
, "illegal header line -- %s:", name
);
705 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
706 fprintf (out
, "%s: %s", name
, str
);
711 if (hdr
->flags
& HIGN
) {
713 fprintf (out
, "%s: %s", name
, str
);
716 if (hdr
->flags
& HBAD
) {
718 fprintf (out
, "%s: %s", name
, str
);
720 advise (NULL
, "illegal header line -- %s:", name
);
725 msgflags
|= (hdr
->set
& ~(MVIS
| MINV
));
727 if (hdr
->flags
& HSUB
)
728 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
729 if (hdr
->flags
& HFCC
) {
731 fprintf (out
, "%s: %s", name
, str
);
735 if ((cp
= strrchr(str
, '\n')))
737 for (cp
= pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
739 insert_fcc (hdr
, pp
);
741 insert_fcc (hdr
, pp
);
745 if (!(hdr
->flags
& HADR
)) {
746 fprintf (out
, "%s: %s", name
, str
);
750 tmpaddrs
.m_next
= NULL
;
751 for (count
= 0; (cp
= getname (str
)); count
++)
752 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
756 tmpaddrs
.m_next
= mp
;
760 if (hdr
->flags
& HTRY
)
766 if (hdr
->flags
& HNIL
)
767 fprintf (out
, "%s: %s", name
, str
);
770 advise (NULL
, "%s: field requires at least one address", name
);
777 nameoutput
= linepos
= 0;
778 snprintf (namep
, sizeof(namep
), "%s%s",
779 !fill_in
&& (hdr
->flags
& HMNG
) ? "Original-" : "", name
);
781 for (grp
= 0, mp
= tmpaddrs
.m_next
; mp
; mp
= np
)
782 if (mp
->m_nohost
) { /* also used to test (hdr->flags & HTRY) */
783 /* The address doesn't include a host, so it might be an alias. */
784 pp
= akvalue (mp
->m_mbox
); /* do mh alias substitution */
785 qp
= akvisible () ? mp
->m_mbox
: "";
788 putgrp (namep
, np
->m_gname
, out
, hdr
->flags
);
789 while ((cp
= getname (pp
))) {
790 if (!(mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
795 if (draft_from_masquerading
&& ((msgstate
== RESENT
)
797 : (hdr
->set
& MFRM
)))
798 /* The user manually specified a [Resent-]From: address in
799 their draft and the "masquerade:" line in mts.conf
800 doesn't contain "draft_from", so we'll set things up to
801 use the actual email address embedded in the draft
802 [Resent-]From: (after alias substitution, and without the
803 GECOS full name or angle brackets) as the envelope
805 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
807 if (hdr
->flags
& HBCC
)
810 mp
->m_ingrp
= np
->m_ingrp
;
813 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
816 if (putadr (namep
, qp
, mp
, out
, hdr
->flags
))
817 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
826 /* Address includes a host, so no alias substitution is needed. */
827 if (draft_from_masquerading
&& ((msgstate
== RESENT
)
829 : (hdr
->set
& MFRM
)))
830 /* The user manually specified a [Resent-]From: address in
831 their draft and the "masquerade:" line in mts.conf
832 doesn't contain "draft_from", so we'll set things up to
833 use the actual email address embedded in the draft
834 [Resent-]From: (after alias substitution, and without the
835 GECOS full name or angle brackets) as the envelope
837 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
839 if (hdr
->flags
& HBCC
)
842 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
845 keep
= putadr (namep
, "", mp
, out
, hdr
->flags
);
849 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
855 if (grp
> 0 && (hdr
->flags
& HNGR
)) {
856 advise (NULL
, "%s: field does not allow groups", name
);
860 if (fill_in
&& grp
> 0)
871 char myhost
[BUFSIZ
], sigbuf
[BUFSIZ
];
878 strncpy (from
, adrsprintf (NULL
, NULL
), sizeof(from
));
879 strncpy (myhost
, LocalName (), sizeof(myhost
));
881 for (cp
= myhost
; *cp
; cp
++)
884 if ((cp
= getfullname ()) && *cp
) {
885 strncpy (sigbuf
, cp
, sizeof(sigbuf
));
886 snprintf (signature
, sizeof(signature
), "%s <%s>",
887 sigbuf
, adrsprintf (NULL
, NULL
));
888 if ((cp
= getname (signature
)) == NULL
)
889 adios (NULL
, "getname () failed -- you lose extraordinarily big");
890 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
)) == NULL
)
891 adios (NULL
, "bad signature '%s'", sigbuf
);
896 strncpy (signature
, adrsprintf (NULL
, NULL
), sizeof(signature
));
902 * Now that we've outputted the header fields in the draft
903 * message, we will now output any remaining header fields
904 * that we need to add/create.
908 finish_headers (FILE *out
)
912 if (whomsw
&& !fill_up
)
915 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
917 fprintf (out
, "Message-ID: <%d.%ld@%s>\n",
918 (int) getpid (), (long) tclock
, LocalName ());
919 if (msgflags
& MFRM
) {
920 /* There was already a From: in the draft. Don't add one. */
921 if (!draft_from_masquerading
)
922 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
923 so we'll reveal the user's actual account@thismachine
924 address in a Sender: header (and use it as the envelope
926 fprintf (out
, "Sender: %s\n", from
);
929 /* Construct a From: header. */
930 fprintf (out
, "From: %s\n", signature
);
934 if (!(msgflags
& MVIS
))
935 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
939 if (!(msgflags
& MDAT
)) {
940 advise (NULL
, "message has no Date: header");
943 if (!(msgflags
& MFRM
)) {
944 advise (NULL
, "message has no From: header");
947 if (whomsw
&& !fill_up
)
950 fprintf (out
, "Resent-Date: %s\n", dtime (&tclock
, 0));
952 fprintf (out
, "Resent-Message-ID: <%d.%ld@%s>\n",
953 (int) getpid (), (long) tclock
, LocalName ());
954 if (msgflags
& MRFM
) {
955 /* There was already a Resent-From: in draft. Don't add one. */
956 if (!draft_from_masquerading
)
957 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
958 so we'll reveal the user's actual account@thismachine
959 address in a Sender: header (and use it as the envelope
961 fprintf (out
, "Resent-Sender: %s\n", from
);
964 /* Construct a Resent-From: header. */
965 fprintf (out
, "Resent-From: %s\n", signature
);
968 if (!(msgflags
& MVIS
))
969 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
974 adios (NULL
, "re-format message and try again");
976 adios (NULL
, "no addressees");
981 get_header (char *header
, struct headers
*table
)
985 for (h
= table
; h
->value
; h
++)
986 if (!mh_strcasecmp (header
, h
->value
))
994 putadr (char *name
, char *aka
, struct mailname
*mp
, FILE *out
, unsigned int flags
)
1000 if (mp
->m_mbox
== NULL
|| ((flags
& HTRY
) && !insert (mp
)))
1002 if ((!fill_in
&& (flags
& (HBCC
| HDCC
))) || mp
->m_ingrp
)
1006 fprintf (out
, "%s: ", name
);
1007 linepos
+= (nameoutput
= strlen (name
) + 2);
1010 if (*aka
&& mp
->m_type
!= UUCPHOST
&& !mp
->m_pers
)
1011 mp
->m_pers
= getcpy (aka
);
1013 if (mp
->m_gname
&& !fill_in
) {
1014 snprintf (buffer
, sizeof(buffer
), "%s;", mp
->m_gname
);
1017 cp
= adrformat (mp
);
1024 if (linepos
!= nameoutput
) {
1025 if (len
+ linepos
+ 2 > outputlinelen
)
1026 fprintf (out
, ",\n%*s", linepos
= nameoutput
, "");
1036 return (flags
& HTRY
);
1041 putgrp (char *name
, char *group
, FILE *out
, unsigned int flags
)
1046 if (!fill_in
&& (flags
& HBCC
))
1050 fprintf (out
, "%s: ", name
);
1051 linepos
+= (nameoutput
= strlen (name
) + 2);
1053 linepos
-= strlen (group
);
1056 cp
= fill_in
? group
: concat (group
, ";", NULL
);
1059 if (linepos
> nameoutput
) {
1060 if (len
+ linepos
+ 2 > outputlinelen
) {
1061 fprintf (out
, ",\n%*s", nameoutput
, "");
1062 linepos
= nameoutput
;
1076 insert (struct mailname
*np
)
1078 struct mailname
*mp
;
1080 if (np
->m_mbox
== NULL
)
1083 for (mp
= np
->m_type
== LOCALHOST
? &localaddrs
1084 : np
->m_type
== UUCPHOST
? &uuaddrs
1088 if (!mh_strcasecmp (np
->m_host
, mp
->m_next
->m_host
)
1089 && !mh_strcasecmp (np
->m_mbox
, mp
->m_next
->m_mbox
)
1090 && np
->m_bcc
== mp
->m_next
->m_bcc
)
1103 struct mailname
*mp
;
1105 printf ("-------\n\t-- Addresses --\nlocal:\t");
1106 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1107 printf ("%s%s%s", mp
->m_mbox
,
1108 mp
->m_bcc
? "[BCC]" : "",
1109 mp
->m_next
? ",\n\t" : "");
1111 printf ("\nnet:\t");
1112 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1113 printf ("%s%s@%s%s%s", mp
->m_path
? mp
->m_path
: "",
1114 mp
->m_mbox
, mp
->m_host
,
1115 mp
->m_bcc
? "[BCC]" : "",
1116 mp
->m_next
? ",\n\t" : "");
1118 printf ("\nuucp:\t");
1119 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1120 printf ("%s!%s%s%s", mp
->m_host
, mp
->m_mbox
,
1121 mp
->m_bcc
? "[BCC]" : "",
1122 mp
->m_next
? ",\n\t" : "");
1124 printf ("\n\t-- Folder Copies --\nfcc:\t");
1125 for (i
= 0; i
< fccind
; i
++)
1126 printf ("%s%s", fccfold
[i
], i
+ 1 < fccind
? ",\n\t" : "");
1134 struct mailname
*mp
;
1136 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1137 if (annoaux (mp
) == NOTOK
)
1140 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1141 if (annoaux (mp
) == NOTOK
)
1144 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1145 if (annoaux (mp
) == NOTOK
)
1155 annoaux (struct mailname
*mp
)
1158 char buffer
[BUFSIZ
];
1160 snprintf (buffer
, sizeof(buffer
), "%s\n", adrformat (mp
));
1161 i
= strlen (buffer
);
1163 return (write (pfd
, buffer
, i
) == i
? OK
: NOTOK
);
1168 insert_fcc (struct headers
*hdr
, unsigned char *pp
)
1172 for (cp
= pp
; isspace (*cp
); cp
++)
1174 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace (*pp
); pp
--)
1182 adios (NULL
, "too many %ss", hdr
->value
);
1183 fccfold
[fccind
++] = getcpy (cp
);
1191 make_bcc_file (int dashstuff
)
1199 tfile
= m_mktemp2(NULL
, "bccs", NULL
, &out
);
1200 if (tfile
== NULL
) adios("bcc", "unable to create temporary file");
1201 chmod (bccfil
, 0600);
1202 strncpy (bccfil
, tfile
, sizeof(bccfil
));
1204 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1206 fprintf (out
, "Message-ID: <%d.%ld@%s>\n",
1207 (int) getpid (), (long) tclock
, LocalName ());
1208 if (msgflags
& MFRM
) {
1209 /* There was already a From: in the draft. Don't add one. */
1210 if (!draft_from_masquerading
)
1211 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
1212 so we'll reveal the user's actual account@thismachine
1213 address in a Sender: header (and use it as the envelope
1215 fprintf (out
, "Sender: %s\n", from
);
1218 /* Construct a From: header. */
1219 fprintf (out
, "From: %s\n", signature
);
1221 fprintf (out
, "Subject: %s", subject
);
1222 fprintf (out
, "BCC:\n");
1225 * Use MIME encapsulation for Bcc messages
1231 * Check if any lines in the message clash with the
1232 * prefix for the MIME multipart separator. If there
1233 * is a clash, increment one of the letters in the
1234 * prefix and check again.
1236 if ((cp
= strchr(prefix
, 'a')) == NULL
)
1237 adios (NULL
, "lost prefix start");
1238 while (find_prefix () == NOTOK
) {
1243 adios (NULL
, "can't find a unique delimiter string");
1248 fprintf (out
, "%s: %s\n%s: multipart/digest; boundary=\"",
1249 VRSN_FIELD
, VRSN_VALUE
, TYPE_FIELD
);
1250 fprintf (out
, "%s\"\n\n--%s\n\n", prefix
, prefix
);
1252 fprintf (out
, "\n------- Blind-Carbon-Copy\n\n");
1258 * Do mhl filtering of Bcc messages instead
1259 * of MIME encapsulation.
1261 if (filter
!= NULL
) {
1262 vec
[0] = r1bindex (mhlproc
, '/');
1264 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1268 adios ("fork", "unable to");
1271 dup2 (fileno (out
), 1);
1274 vec
[i
++] = "-forward";
1279 /* was the flag -[no]dashstuffing specified? */
1281 vec
[i
++] = "-dashstuffing";
1282 else if (dashstuff
< 0)
1283 vec
[i
++] = "-nodashstuffing";
1286 execvp (mhlproc
, vec
);
1287 fprintf (stderr
, "unable to exec ");
1292 pidXwait (child_id
, mhlproc
);
1296 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
1297 adios (tmpfil
, "unable to re-open");
1300 * If using MIME encapsulation, or if the -nodashstuffing
1301 * flag was given, then just copy message. Else do
1302 * RFC934 quoting (dashstuffing).
1304 if (mime
|| dashstuff
< 0)
1305 cpydata (fd
, fileno (out
), tmpfil
, bccfil
);
1307 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
1311 fseek (out
, 0L, SEEK_END
);
1313 fprintf (out
, "\n--%s--\n", prefix
);
1315 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
1321 * Scan message to check if any lines clash with
1322 * the prefix of the MIME multipart separator.
1329 unsigned char buffer
[BUFSIZ
];
1332 if ((in
= fopen (tmpfil
, "r")) == NULL
)
1333 adios (tmpfil
, "unable to re-open");
1335 while (fgets (buffer
, sizeof(buffer
) - 1, in
))
1336 if (buffer
[0] == '-' && buffer
[1] == '-') {
1339 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1343 if (strcmp (buffer
+ 2, prefix
) == 0) {
1354 #define plural(x) (x == 1 ? "" : "s")
1359 if (badadr
&& unkadr
)
1360 die (NULL
, "%d address%s unparsable, %d addressee%s undeliverable",
1361 badadr
, plural (badadr
), unkadr
, plural (badadr
));
1363 die (NULL
, "%d address%s unparsable", badadr
, plural (badadr
));
1365 die (NULL
, "%d addressee%s undeliverable", unkadr
, plural (unkadr
));
1370 do_addresses (int bccque
, int talk
)
1374 struct mailname
*lp
;
1377 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1378 if (lp
->m_bcc
? bccque
: !bccque
) {
1380 printf (" -- Local Recipients --\n");
1381 do_an_address (lp
, talk
);
1386 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1387 if (lp
->m_bcc
? bccque
: !bccque
) {
1389 printf (" -- UUCP Recipients --\n");
1390 do_an_address (lp
, talk
);
1395 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1396 if (lp
->m_bcc
? bccque
: !bccque
) {
1398 printf (" -- Network Recipients --\n");
1399 do_an_address (lp
, talk
);
1406 if (rp_isbad (retval
= sm_waend ()))
1407 die (NULL
, "problem ending addresses; %s", rp_string (retval
));
1408 #endif /* SMTPMTS */
1413 * MTS-SPECIFIC INTERACTION
1418 * SENDMAIL/SMTP routines
1424 post (char *file
, int bccque
, int talk
)
1429 onex
= !(msgflags
& MINV
) || bccque
;
1431 if (msgflags
& MINV
)
1432 printf (" -- Posting for %s Recipients --\n",
1433 bccque
? "Blind" : "Sighted");
1435 printf (" -- Posting for All Recipients --\n");
1440 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
, verbose
,
1441 snoop
, onex
, queued
, sasl
, saslmech
,
1443 || rp_isbad (retval
= sm_winit (smtpmode
, from
)))
1444 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1446 do_addresses (bccque
, talk
&& verbose
);
1447 if ((fd
= open (file
, O_RDONLY
)) == NOTOK
)
1448 die (file
, "unable to re-open");
1453 sm_end (onex
? OK
: DONE
);
1457 if (msgflags
& MINV
)
1458 printf (" -- %s Recipient Copies Posted --\n",
1459 bccque
? "Blind" : "Sighted");
1461 printf (" -- Recipient Copies Posted --\n");
1468 /* Address Verification */
1471 verify_all_addresses (int talk
)
1474 struct mailname
*lp
;
1478 if (!whomsw
|| checksw
)
1479 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1480 verbose
, snoop
, 0, queued
, sasl
,
1481 saslmech
, user
, tls
))
1482 || rp_isbad (retval
= sm_winit (smtpmode
, from
)))
1483 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1485 if (talk
&& !whomsw
)
1486 printf (" -- Address Verification --\n");
1487 if (talk
&& localaddrs
.m_next
)
1488 printf (" -- Local Recipients --\n");
1489 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1490 do_an_address (lp
, talk
);
1492 if (talk
&& uuaddrs
.m_next
)
1493 printf (" -- UUCP Recipients --\n");
1494 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1495 do_an_address (lp
, talk
);
1497 if (talk
&& netaddrs
.m_next
)
1498 printf (" -- Network Recipients --\n");
1499 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1500 do_an_address (lp
, talk
);
1503 if (talk
&& !whomsw
)
1504 printf (" -- Address Verification Successful --\n");
1506 if (!whomsw
|| checksw
)
1515 do_an_address (struct mailname
*lp
, int talk
)
1521 switch (lp
->m_type
) {
1525 strncpy (addr
, mbox
, sizeof(addr
));
1529 mbox
= auxformat (lp
, 0);
1531 snprintf (addr
, sizeof(addr
), "%s!%s", lp
->m_host
, lp
->m_mbox
);
1534 default: /* let SendMail decide if the host is bad */
1537 snprintf (addr
, sizeof(addr
), "%s at %s", mbox
, host
);
1542 printf (" %s%s", addr
, whomsw
&& lp
->m_bcc
? "[BCC]" : "");
1544 if (whomsw
&& !checksw
) {
1552 switch (retval
= sm_wadr (mbox
, host
,
1553 lp
->m_type
!= UUCPHOST
? lp
->m_path
: NULL
)) {
1556 printf ("address ok\n");
1562 fprintf (stderr
, " %s: ", addr
);
1563 fprintf (talk
? stdout
: stderr
, "loses; %s\n",
1564 rp_string (retval
));
1570 fprintf (stderr
, " %s: ", addr
);
1571 die (NULL
, "unexpected response; %s", rp_string (retval
));
1579 do_text (char *file
, int fd
)
1584 lseek (fd
, (off_t
) 0, SEEK_SET
);
1586 while ((state
= read (fd
, buf
, sizeof(buf
))) > 0) {
1587 if (rp_isbad (retval
= sm_wtxt (buf
, state
)))
1588 die (NULL
, "problem writing text; %s\n", rp_string (retval
));
1592 die (file
, "problem reading from");
1594 switch (retval
= sm_wtend ()) {
1600 die (NULL
, "posting failed; %s", rp_string (retval
));
1603 die (NULL
, "unexpected response; %s", rp_string (retval
));
1607 #endif /* SMTPMTS */
1617 #ifndef RELIABLE_SIGNALS
1618 SIGNAL (i
, SIG_IGN
);
1622 if (msgflags
& MINV
)
1626 if (!whomsw
|| checksw
)
1628 #endif /* SMTPMTS */
1640 hstat
= SIGNAL2 (SIGHUP
, sigser
);
1641 istat
= SIGNAL2 (SIGINT
, sigser
);
1642 qstat
= SIGNAL2 (SIGQUIT
, sigser
);
1643 tstat
= SIGNAL2 (SIGTERM
, sigser
);
1653 SIGNAL (SIGHUP
, hstat
);
1654 SIGNAL (SIGINT
, istat
);
1655 SIGNAL (SIGQUIT
, qstat
);
1656 SIGNAL (SIGTERM
, tstat
);
1664 p_refile (char *file
)
1672 printf (" -- Filing Folder Copies --\n");
1673 for (i
= 0; i
< fccind
; i
++)
1674 fcc (file
, fccfold
[i
]);
1676 printf (" -- Folder Copies Filed --\n");
1681 * Call the `fileproc' to add the file to the folder.
1685 fcc (char *file
, char *folder
)
1692 printf (" %sFcc %s: ", msgstate
== RESENT
? "Resent-" : "", folder
);
1695 for (i
= 0; (child_id
= fork ()) == NOTOK
&& i
< 5; i
++)
1701 fprintf (stderr
, " %sFcc %s: ",
1702 msgstate
== RESENT
? "Resent-" : "", folder
);
1703 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
1707 /* see if we need to add `+' */
1708 snprintf (fold
, sizeof(fold
), "%s%s",
1709 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
1711 /* now exec the fileproc */
1712 execlp (fileproc
, r1bindex (fileproc
, '/'),
1713 "-link", "-file", file
, fold
, NULL
);
1717 if ((status
= pidwait (child_id
, OK
))) {
1719 fprintf (stderr
, " %sFcc %s: ",
1720 msgstate
== RESENT
? "Resent-" : "", folder
);
1721 pidstatus (status
, verbose
? stdout
: stderr
, NULL
);
1724 printf ("folder ok\n");
1736 die (char *what
, char *fmt
, ...)
1741 if (msgflags
& MINV
)
1745 if (!whomsw
|| checksw
)
1747 #endif /* SMTPMTS */
1750 advertise (what
, NULL
, fmt
, ap
);