3 * post.c -- enter messages into the mail transport system
10 #include <h/signals.h>
11 #include <h/addrsbr.h>
12 #include <h/aliasbr.h>
13 #include <h/dropsbr.h>
23 #ifdef TIME_WITH_SYS_TIME
24 # include <sys/time.h>
27 # ifdef TM_IN_SYS_TIME
28 # include <sys/time.h>
35 # include <mts/mmdf/util.h>
36 # include <mts/mmdf/mmdf.h>
40 # include <mts/smtp/smtp.h>
44 # define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : (c))
48 # define SASLminc(a) (a)
49 #else /* CYRUS_SASL */
50 # define SASLminc(a) 0
51 #endif /* CYRUS_SASL */
53 #define FCCS 10 /* max number of fccs allowed */
55 /* In the following array of structures, the numeric second field of the
56 structures (minchars) is apparently used like this:
58 -# : Switch can be abbreviated to # characters; switch hidden in -help.
59 0 : Switch can't be abbreviated; switch shown in -help.
60 # : Switch can be abbreviated to # characters; switch shown in -help. */
62 static struct swit switches
[] = {
64 { "alias aliasfile", 0 },
66 { "check", -5 }, /* interface from whom */
68 { "nocheck", -7 }, /* interface from whom */
72 { "dist", -4 }, /* interface from dist */
74 { "filter filterfile", 0 },
82 { "library directory", -7 }, /* interface from send, whom */
100 { "whom", -4 }, /* interface from whom */
102 { "width columns", 0 },
107 #define BITSTUFFSW 22
108 { "dashstuffing", -12 }, /* should we dashstuff BCC messages? */
109 #define NBITSTUFFSW 23
110 { "nodashstuffing", -14 },
112 { "mail", -4 }, /* specify MAIL smtp mode */
114 { "saml", -4 }, /* specify SAML smtp mode */
116 { "send", -4 }, /* specify SEND smtp mode */
118 { "soml", -4 }, /* specify SOML smtp mode */
120 { "idanno number", -6 }, /* interface from send */
122 { "deliver address-list", -7 },
124 { "client host", -6 },
126 { "server host", -6 }, /* specify alternate SMTP server */
128 { "snoop", -5 }, /* snoop the SMTP transaction */
130 { "fill-in file", -7 },
138 { "sasl", SASLminc(-4) },
139 #define SASLMECHSW 38
140 { "saslmech", SASLminc(-5) },
142 { "user", SASLminc(-4) },
154 * flags for headers->flags
156 #define HNOP 0x0000 /* just used to keep .set around */
157 #define HBAD 0x0001 /* bad header - don't let it through */
158 #define HADR 0x0002 /* header has an address field */
159 #define HSUB 0x0004 /* Subject: header */
160 #define HTRY 0x0008 /* try to send to addrs on header */
161 #define HBCC 0x0010 /* don't output this header */
162 #define HMNG 0x0020 /* munge this header */
163 #define HNGR 0x0040 /* no groups allowed in this header */
164 #define HFCC 0x0080 /* FCC: type header */
165 #define HNIL 0x0100 /* okay for this header not to have addrs */
166 #define HIGN 0x0200 /* ignore this header */
167 #define HDCC 0x0400 /* another undocumented feature */
170 * flags for headers->set
172 #define MFRM 0x0001 /* we've seen a From: */
173 #define MDAT 0x0002 /* we've seen a Date: */
174 #define MRFM 0x0004 /* we've seen a Resent-From: */
175 #define MVIS 0x0008 /* we've seen sighted addrs */
176 #define MINV 0x0010 /* we've seen blind addrs */
179 static struct headers NHeaders
[] = {
180 { "Return-Path", HBAD
, 0 },
181 { "Received", HBAD
, 0 },
182 { "Reply-To", HADR
|HNGR
, 0 },
183 { "From", HADR
|HNGR
, MFRM
},
184 { "Sender", HADR
|HBAD
, 0 },
186 { "Subject", HSUB
, 0 },
187 { "To", HADR
|HTRY
, MVIS
},
188 { "cc", HADR
|HTRY
, MVIS
},
189 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, MINV
},
190 { "Dcc", HADR
|HTRY
|HDCC
|HNIL
, MVIS
}, /* sorta cc & bcc combined */
191 { "Message-ID", HBAD
, 0 },
196 static struct headers RHeaders
[] = {
197 { "Resent-Reply-To", HADR
|HNGR
, 0 },
198 { "Resent-From", HADR
|HNGR
, MRFM
},
199 { "Resent-Sender", HADR
|HBAD
, 0 },
200 { "Resent-Date", HBAD
, 0 },
201 { "Resent-Subject", HSUB
, 0 },
202 { "Resent-To", HADR
|HTRY
, MVIS
},
203 { "Resent-cc", HADR
|HTRY
, MVIS
},
204 { "Resent-Bcc", HADR
|HTRY
|HBCC
, MINV
},
205 { "Resent-Message-ID", HBAD
, 0 },
206 { "Resent-Fcc", HFCC
, 0 },
207 { "Reply-To", HADR
, 0 },
208 { "From", HADR
|HNGR
, MFRM
},
210 { "Sender", HADR
|HNGR
|HMNG
, 0 },
212 { "Sender", HADR
|HNGR
, 0 },
214 { "Date", HNOP
, MDAT
},
215 { "To", HADR
|HNIL
, 0 },
216 { "cc", HADR
|HNIL
, 0 },
217 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, 0 },
222 static short fccind
= 0; /* index into fccfold[] */
223 static short outputlinelen
= OUTPUTLINELEN
;
225 static int pfd
= NOTOK
; /* fd to write annotation list to */
226 static uid_t myuid
= -1; /* my user id */
227 static gid_t mygid
= -1; /* my group id */
228 static int recipients
= 0; /* how many people will get a copy */
229 static int unkadr
= 0; /* how many of those were unknown */
230 static int badadr
= 0; /* number of bad addrs */
231 static int badmsg
= 0; /* message has bad semantics */
232 static int verbose
= 0; /* spell it out */
233 static int format
= 1; /* format addresses */
234 static int mime
= 0; /* use MIME-style encapsulations for Bcc */
235 static int msgid
= 0; /* add msgid */
236 static int debug
= 0; /* debugging post */
237 static int watch
= 0; /* watch the delivery process */
238 static int whomsw
= 0; /* we are whom not post */
239 static int checksw
= 0; /* whom -check */
240 static int linepos
=0; /* putadr()'s position on the line */
241 static int nameoutput
=0; /* putadr() has output header name */
242 static int sasl
=0; /* Use SASL auth for SMTP */
243 static char *saslmech
=NULL
; /* Force use of particular SASL mech */
244 static char *user
=NULL
; /* Authenticate as this user */
246 static unsigned msgflags
= 0; /* what we've seen */
250 static int msgstate
= NORMAL
;
252 static time_t tclock
= 0; /* the time we started (more or less) */
254 static SIGNAL_HANDLER hstat
, istat
, qstat
, tstat
;
256 static char tmpfil
[BUFSIZ
];
257 static char bccfil
[BUFSIZ
];
259 static char from
[BUFSIZ
]; /* my network address */
260 static char signature
[BUFSIZ
]; /* my signature */
261 static char *filter
= NULL
; /* the filter for BCC'ing */
262 static char *subject
= NULL
; /* the subject field for BCC'ing */
263 static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
265 static struct headers
*hdrtab
; /* table for the message we're doing */
267 static struct mailname localaddrs
={NULL
}; /* local addrs */
268 static struct mailname netaddrs
={NULL
}; /* network addrs */
269 static struct mailname uuaddrs
={NULL
}; /* uucp addrs */
270 static struct mailname tmpaddrs
={NULL
}; /* temporary queue */
273 static char *submitmode
= "m"; /* deliver to mailbox only */
274 static char submitopts
[6] = "vl"; /* initial options for submit */
278 static int snoop
= 0;
279 static int smtpmode
= S_MAIL
;
280 static char *clientsw
= NULL
;
281 static char *serversw
= NULL
;
283 extern struct smtp sm_reply
;
286 static char prefix
[] = "----- =_aaaaaaaaaa";
288 static int fill_up
= 0;
289 static char *fill_in
= NULL
;
290 static char *partno
= NULL
;
291 static int queued
= 0;
293 extern boolean draft_from_masquerading
; /* defined in mts.c */
298 static void putfmt (char *, char *, FILE *);
299 static void start_headers (void);
300 static void finish_headers (FILE *);
301 static int get_header (char *, struct headers
*);
302 static int putadr (char *, char *, struct mailname
*, FILE *, unsigned int);
303 static void putgrp (char *, char *, FILE *, unsigned int);
304 static int insert (struct mailname
*);
305 static void pl (void);
306 static void anno (void);
307 static int annoaux (struct mailname
*);
308 static void insert_fcc (struct headers
*, char *);
309 static void make_bcc_file (int);
310 static void verify_all_addresses (int);
311 static void chkadr (void);
312 static void sigon (void);
313 static void sigoff (void);
314 static void p_refile (char *);
315 static void fcc (char *, char *);
316 static void die (char *, char *, ...);
317 static void post (char *, int, int);
318 static void do_text (char *file
, int fd
);
319 static void do_an_address (struct mailname
*, int);
320 static void do_addresses (int, int);
321 static int find_prefix (void);
325 main (int argc
, char **argv
)
327 int state
, compnum
, dashstuff
= 0;
328 char *cp
, *msg
= NULL
, **argp
, **arguments
;
329 char buf
[BUFSIZ
], name
[NAMESZ
];
333 setlocale(LC_ALL
, "");
335 invo_name
= r1bindex (argv
[0], '/');
337 /* foil search of user profile/context */
338 if (context_foil (NULL
) == -1)
341 mts_init (invo_name
);
342 arguments
= getarguments (invo_name
, argc
, argv
, 0);
345 #if defined(MMDFMTS) && defined(MMDFII)
346 mmdf_init (invo_name
);
347 #endif /* MMDFMTS and MMDFII */
349 while ((cp
= *argp
++)) {
351 switch (smatch (++cp
, switches
)) {
353 ambigsw (cp
, switches
);
356 adios (NULL
, "-%s unknown", cp
);
359 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
360 print_help (buf
, switches
, 0);
363 print_version(invo_name
);
367 if (!(cp
= *argp
++) || *cp
== '-')
368 adios (NULL
, "missing argument to %s", argp
[-2]);
369 /* create a minimal context */
370 if (context_foil (cp
) == -1)
375 if (!(cp
= *argp
++) || *cp
== '-')
376 adios (NULL
, "missing argument to %s", argp
[-2]);
377 if ((state
= alias (cp
)) != AK_OK
)
378 adios (NULL
, "aliasing error in %s - %s",
379 cp
, akerror (state
));
398 if (!(filter
= *argp
++) || *filter
== '-')
399 adios (NULL
, "missing argument to %s", argp
[-2]);
454 if (!(cp
= *argp
++) || *cp
== '-')
455 adios (NULL
, "missing argument to %s", argp
[-2]);
456 if ((outputlinelen
= atoi (cp
)) < 10)
457 adios (NULL
, "impossible width %d", outputlinelen
);
461 if (!(cp
= *argp
++) || *cp
== '-')
462 adios (NULL
, "missing argument to %s", argp
[-2]);
463 if ((pfd
= atoi (cp
)) <= 2)
464 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
471 case SOMLSW
: /* for right now, sigh... */
481 if (!(cp
= *argp
++) || *cp
== '-')
482 adios (NULL
, "missing argument to %s", argp
[-2]);
488 if (!(cp
= *argp
++) || *cp
== '-')
489 adios (NULL
, "missing argument to %s", argp
[-2]);
508 if (!(clientsw
= *argp
++) || *clientsw
== '-')
509 adios (NULL
, "missing argument to %s", argp
[-2]);
512 if (!(serversw
= *argp
++) || *serversw
== '-')
513 adios (NULL
, "missing argument to %s", argp
[-2]);
521 if (!(fill_in
= *argp
++) || *fill_in
== '-')
522 adios (NULL
, "missing argument to %s", argp
[-2]);
528 if (!(partno
= *argp
++) || *partno
== '-')
529 adios (NULL
, "missing argument to %s", argp
[-2]);
541 if (!(saslmech
= *argp
++) || *saslmech
== '-')
542 adios (NULL
, "missing argument to %s", argp
[-2]);
546 if (!(user
= *argp
++) || *user
== '-')
547 adios (NULL
, "missing argument to %s", argp
[-2]);
552 adios (NULL
, "only one message at a time!");
560 adios (NULL
, "usage: %s [switches] file", invo_name
);
562 if (outputlinelen
< 10)
563 adios (NULL
, "impossible width %d", outputlinelen
);
565 if ((in
= fopen (msg
, "r")) == NULL
)
566 adios (msg
, "unable to open");
571 discard (out
= stdout
); /* XXX: reference discard() to help loader */
574 if ((out
= fopen (fill_in
? fill_in
: "/dev/null", "w")) == NULL
)
575 adios ("/dev/null", "unable to open");
577 strncpy (tmpfil
, m_scratch ("", m_maildir (invo_name
)),
579 if ((out
= fopen (tmpfil
, "w")) == NULL
) {
580 strncpy (tmpfil
, m_tmpfil (invo_name
), sizeof(tmpfil
));
581 if ((out
= fopen (tmpfil
, "w")) == NULL
)
582 adios (tmpfil
, "unable to create");
584 chmod (tmpfil
, 0600);
588 hdrtab
= msgstate
== NORMAL
? NHeaders
: RHeaders
;
590 for (compnum
= 1, state
= FLD
;;) {
591 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
)) {
596 cp
= add (buf
, NULL
);
597 while (state
== FLDPLUS
) {
598 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
601 putfmt (name
, cp
, out
);
605 finish_headers (out
);
610 finish_headers (out
);
611 if (whomsw
&& !fill_in
)
613 fprintf (out
, "\n%s", buf
);
614 while (state
== BODY
) {
615 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
621 finish_headers (out
);
626 adios (NULL
, "message format error in component #%d", compnum
);
629 adios (NULL
, "getfld() returned %d", state
);
645 /* If we are doing a "whom" check */
648 verify_all_addresses (1);
653 strcat (submitopts
, submitmode
);
655 strcat (submitopts
, "nw");
658 if (msgflags
& MINV
) {
659 make_bcc_file (dashstuff
);
660 if (msgflags
& MVIS
) {
661 verify_all_addresses (verbose
);
662 post (tmpfil
, 0, verbose
);
664 post (bccfil
, 1, verbose
);
667 post (tmpfil
, 0, isatty (1));
674 printf (partno
? "Partial Message #%s Processed\n" : "Message Processed\n",
685 putfmt (char *name
, char *str
, FILE *out
)
687 int count
, grp
, i
, keep
;
690 struct mailname
*mp
, *np
;
693 while (*str
== ' ' || *str
== '\t')
696 if (msgstate
== NORMAL
&& uprf (name
, "resent")) {
697 advise (NULL
, "illegal header line -- %s:", name
);
702 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
703 fprintf (out
, "%s: %s", name
, str
);
708 if (hdr
->flags
& HIGN
) {
710 fprintf (out
, "%s: %s", name
, str
);
713 if (hdr
->flags
& HBAD
) {
715 fprintf (out
, "%s: %s", name
, str
);
717 advise (NULL
, "illegal header line -- %s:", name
);
722 msgflags
|= (hdr
->set
& ~(MVIS
| MINV
));
724 if (hdr
->flags
& HSUB
)
725 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
726 if (hdr
->flags
& HFCC
) {
728 fprintf (out
, "%s: %s", name
, str
);
732 if ((cp
= strrchr(str
, '\n')))
734 for (cp
= pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
736 insert_fcc (hdr
, pp
);
738 insert_fcc (hdr
, pp
);
742 if (!(hdr
->flags
& HADR
)) {
743 fprintf (out
, "%s: %s", name
, str
);
747 tmpaddrs
.m_next
= NULL
;
748 for (count
= 0; (cp
= getname (str
)); count
++)
749 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
753 tmpaddrs
.m_next
= mp
;
757 if (hdr
->flags
& HTRY
)
763 if (hdr
->flags
& HNIL
)
764 fprintf (out
, "%s: %s", name
, str
);
767 advise (NULL
, "%s: field requires at least one address", name
);
774 nameoutput
= linepos
= 0;
775 snprintf (namep
, sizeof(namep
), "%s%s",
776 !fill_in
&& (hdr
->flags
& HMNG
) ? "Original-" : "", name
);
778 for (grp
= 0, mp
= tmpaddrs
.m_next
; mp
; mp
= np
)
779 if (mp
->m_nohost
) { /* also used to test (hdr->flags & HTRY) */
780 /* The address doesn't include a host, so it might be an alias. */
781 pp
= akvalue (mp
->m_mbox
); /* do mh alias substitution */
782 qp
= akvisible () ? mp
->m_mbox
: "";
785 putgrp (namep
, np
->m_gname
, out
, hdr
->flags
);
786 while ((cp
= getname (pp
))) {
787 if (!(mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
792 if (draft_from_masquerading
&& ((msgstate
== RESENT
)
794 : (hdr
->set
& MFRM
)))
795 /* The user manually specified a [Resent-]From: address in
796 their draft and the "masquerade:" line in mts.conf
797 doesn't contain "draft_from", so we'll set things up to
798 use the actual email address embedded in the draft
799 [Resent-]From: (after alias substitution, and without the
800 GECOS full name or angle brackets) as the envelope
802 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
804 if (hdr
->flags
& HBCC
)
807 mp
->m_ingrp
= np
->m_ingrp
;
810 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
813 if (putadr (namep
, qp
, mp
, out
, hdr
->flags
))
814 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
823 /* Address includes a host, so no alias substitution is needed. */
824 if (draft_from_masquerading
&& ((msgstate
== RESENT
)
826 : (hdr
->set
& MFRM
)))
827 /* The user manually specified a [Resent-]From: address in
828 their draft and the "masquerade:" line in mts.conf
829 doesn't contain "draft_from", so we'll set things up to
830 use the actual email address embedded in the draft
831 [Resent-]From: (after alias substitution, and without the
832 GECOS full name or angle brackets) as the envelope
834 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
836 if (hdr
->flags
& HBCC
)
839 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
842 keep
= putadr (namep
, "", mp
, out
, hdr
->flags
);
846 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
852 if (grp
> 0 && (hdr
->flags
& HNGR
)) {
853 advise (NULL
, "%s: field does not allow groups", name
);
857 if (fill_in
&& grp
> 0)
868 char myhost
[BUFSIZ
], sigbuf
[BUFSIZ
];
875 strncpy (from
, adrsprintf (NULL
, NULL
), sizeof(from
));
876 strncpy (myhost
, LocalName (), sizeof(myhost
));
878 for (cp
= myhost
; *cp
; cp
++)
881 if ((cp
= getfullname ()) && *cp
) {
882 strncpy (sigbuf
, cp
, sizeof(sigbuf
));
883 snprintf (signature
, sizeof(signature
), "%s <%s>",
884 sigbuf
, adrsprintf (NULL
, NULL
));
885 if ((cp
= getname (signature
)) == NULL
)
886 adios (NULL
, "getname () failed -- you lose extraordinarily big");
887 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
)) == NULL
)
888 adios (NULL
, "bad signature '%s'", sigbuf
);
893 strncpy (signature
, adrsprintf (NULL
, NULL
), sizeof(signature
));
899 * Now that we've outputted the header fields in the draft
900 * message, we will now output any remaining header fields
901 * that we need to add/create.
905 finish_headers (FILE *out
)
909 if (whomsw
&& !fill_up
)
912 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
914 fprintf (out
, "Message-ID: <%d.%ld@%s>\n",
915 (int) getpid (), (long) tclock
, LocalName ());
916 if (msgflags
& MFRM
) {
917 /* There was already a From: in the draft. Don't add one. */
918 if (!draft_from_masquerading
)
919 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
920 so we'll reveal the user's actual account@thismachine
921 address in a Sender: header (and use it as the envelope
923 fprintf (out
, "Sender: %s\n", from
);
926 /* Construct a From: header. */
927 fprintf (out
, "From: %s\n", signature
);
931 if (!(msgflags
& MVIS
))
932 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
936 if (!(msgflags
& MDAT
)) {
937 advise (NULL
, "message has no Date: header");
940 if (!(msgflags
& MFRM
)) {
941 advise (NULL
, "message has no From: header");
944 if (whomsw
&& !fill_up
)
947 #ifdef MMDFI /* sigh */
948 fprintf (out
, "Sender: %s\n", from
);
951 fprintf (out
, "Resent-Date: %s\n", dtime (&tclock
, 0));
953 fprintf (out
, "Resent-Message-ID: <%d.%ld@%s>\n",
954 (int) getpid (), (long) tclock
, LocalName ());
955 if (msgflags
& MRFM
) {
956 /* There was already a Resent-From: in draft. Don't add one. */
957 if (!draft_from_masquerading
)
958 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
959 so we'll reveal the user's actual account@thismachine
960 address in a Sender: header (and use it as the envelope
962 fprintf (out
, "Resent-Sender: %s\n", from
);
965 /* Construct a Resent-From: header. */
966 fprintf (out
, "Resent-From: %s\n", signature
);
969 if (!(msgflags
& MVIS
))
970 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
975 adios (NULL
, "re-format message and try again");
977 adios (NULL
, "no addressees");
982 get_header (char *header
, struct headers
*table
)
986 for (h
= table
; h
->value
; h
++)
987 if (!strcasecmp (header
, h
->value
))
995 putadr (char *name
, char *aka
, struct mailname
*mp
, FILE *out
, unsigned int flags
)
1001 if (mp
->m_mbox
== NULL
|| ((flags
& HTRY
) && !insert (mp
)))
1003 if ((!fill_in
&& (flags
& (HBCC
| HDCC
))) || mp
->m_ingrp
)
1007 fprintf (out
, "%s: ", name
);
1008 linepos
+= (nameoutput
= strlen (name
) + 2);
1011 if (*aka
&& mp
->m_type
!= UUCPHOST
&& !mp
->m_pers
)
1012 mp
->m_pers
= getcpy (aka
);
1014 if (mp
->m_gname
&& !fill_in
) {
1015 snprintf (buffer
, sizeof(buffer
), "%s;", mp
->m_gname
);
1018 cp
= adrformat (mp
);
1025 if (linepos
!= nameoutput
) {
1026 if (len
+ linepos
+ 2 > outputlinelen
)
1027 fprintf (out
, ",\n%*s", linepos
= nameoutput
, "");
1037 return (flags
& HTRY
);
1042 putgrp (char *name
, char *group
, FILE *out
, unsigned int flags
)
1047 if (!fill_in
&& (flags
& HBCC
))
1051 fprintf (out
, "%s: ", name
);
1052 linepos
+= (nameoutput
= strlen (name
) + 2);
1054 linepos
-= strlen (group
);
1057 cp
= fill_in
? group
: concat (group
, ";", NULL
);
1060 if (linepos
> nameoutput
) {
1061 if (len
+ linepos
+ 2 > outputlinelen
) {
1062 fprintf (out
, ",\n%*s", nameoutput
, "");
1063 linepos
= nameoutput
;
1077 insert (struct mailname
*np
)
1079 struct mailname
*mp
;
1081 if (np
->m_mbox
== NULL
)
1084 for (mp
= np
->m_type
== LOCALHOST
? &localaddrs
1085 : np
->m_type
== UUCPHOST
? &uuaddrs
1089 if (!strcasecmp (np
->m_host
, mp
->m_next
->m_host
)
1090 && !strcasecmp (np
->m_mbox
, mp
->m_next
->m_mbox
)
1091 && np
->m_bcc
== mp
->m_next
->m_bcc
)
1104 struct mailname
*mp
;
1106 printf ("-------\n\t-- Addresses --\nlocal:\t");
1107 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1108 printf ("%s%s%s", mp
->m_mbox
,
1109 mp
->m_bcc
? "[BCC]" : "",
1110 mp
->m_next
? ",\n\t" : "");
1112 printf ("\nnet:\t");
1113 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1114 printf ("%s%s@%s%s%s", mp
->m_path
? mp
->m_path
: "",
1115 mp
->m_mbox
, mp
->m_host
,
1116 mp
->m_bcc
? "[BCC]" : "",
1117 mp
->m_next
? ",\n\t" : "");
1119 printf ("\nuucp:\t");
1120 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1121 printf ("%s!%s%s%s", mp
->m_host
, mp
->m_mbox
,
1122 mp
->m_bcc
? "[BCC]" : "",
1123 mp
->m_next
? ",\n\t" : "");
1125 printf ("\n\t-- Folder Copies --\nfcc:\t");
1126 for (i
= 0; i
< fccind
; i
++)
1127 printf ("%s%s", fccfold
[i
], i
+ 1 < fccind
? ",\n\t" : "");
1135 struct mailname
*mp
;
1137 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1138 if (annoaux (mp
) == NOTOK
)
1141 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1142 if (annoaux (mp
) == NOTOK
)
1145 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1146 if (annoaux (mp
) == NOTOK
)
1156 annoaux (struct mailname
*mp
)
1159 char buffer
[BUFSIZ
];
1161 snprintf (buffer
, sizeof(buffer
), "%s\n", adrformat (mp
));
1162 i
= strlen (buffer
);
1164 return (write (pfd
, buffer
, i
) == i
? OK
: NOTOK
);
1169 insert_fcc (struct headers
*hdr
, char *pp
)
1173 for (cp
= pp
; isspace (*cp
); cp
++)
1175 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace (*pp
); pp
--)
1183 adios (NULL
, "too many %ss", hdr
->value
);
1184 fccfold
[fccind
++] = getcpy (cp
);
1192 make_bcc_file (int dashstuff
)
1199 strncpy (bccfil
, m_tmpfil ("bccs"), sizeof(bccfil
));
1200 if ((out
= fopen (bccfil
, "w")) == NULL
)
1201 adios (bccfil
, "unable to create");
1202 chmod (bccfil
, 0600);
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 fprintf (out
, "From: %s\n", signature
);
1210 fprintf (out
, "Subject: %s", subject
);
1211 fprintf (out
, "BCC:\n");
1214 * Use MIME encapsulation for Bcc messages
1220 * Check if any lines in the message clash with the
1221 * prefix for the MIME multipart separator. If there
1222 * is a clash, increment one of the letters in the
1223 * prefix and check again.
1225 if ((cp
= strchr(prefix
, 'a')) == NULL
)
1226 adios (NULL
, "lost prefix start");
1227 while (find_prefix () == NOTOK
) {
1232 adios (NULL
, "can't find a unique delimiter string");
1237 fprintf (out
, "%s: %s\n%s: multipart/digest; boundary=\"",
1238 VRSN_FIELD
, VRSN_VALUE
, TYPE_FIELD
);
1239 fprintf (out
, "%s\"\n\n--%s\n\n", prefix
, prefix
);
1241 fprintf (out
, "\n------- Blind-Carbon-Copy\n\n");
1247 * Do mhl filtering of Bcc messages instead
1248 * of MIME encapsulation.
1250 if (filter
!= NULL
) {
1251 vec
[0] = r1bindex (mhlproc
, '/');
1253 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1257 adios ("fork", "unable to");
1260 dup2 (fileno (out
), 1);
1263 vec
[i
++] = "-forward";
1268 /* was the flag -[no]dashstuffing specified? */
1270 vec
[i
++] = "-dashstuffing";
1271 else if (dashstuff
< 0)
1272 vec
[i
++] = "-nodashstuffing";
1275 execvp (mhlproc
, vec
);
1276 fprintf (stderr
, "unable to exec ");
1281 pidXwait (child_id
, mhlproc
);
1285 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
1286 adios (tmpfil
, "unable to re-open");
1289 * If using MIME encapsulation, or if the -nodashstuffing
1290 * flag was given, then just copy message. Else do
1291 * RFC934 quoting (dashstuffing).
1293 if (mime
|| dashstuff
< 0)
1294 cpydata (fd
, fileno (out
), tmpfil
, bccfil
);
1296 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
1300 fseek (out
, 0L, SEEK_END
);
1302 fprintf (out
, "\n--%s--\n", prefix
);
1304 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
1310 * Scan message to check if any lines clash with
1311 * the prefix of the MIME multipart separator.
1318 char buffer
[BUFSIZ
];
1321 if ((in
= fopen (tmpfil
, "r")) == NULL
)
1322 adios (tmpfil
, "unable to re-open");
1324 len
= strlen (prefix
);
1327 while (fgets (buffer
, sizeof(buffer
) - 1, in
))
1328 if (buffer
[0] == '-' && buffer
[1] == '-') {
1331 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1335 if (strcmp (buffer
+ 2, prefix
) == 0) {
1346 #define plural(x) (x == 1 ? "" : "s")
1351 if (badadr
&& unkadr
)
1352 die (NULL
, "%d address%s unparsable, %d addressee%s undeliverable",
1353 badadr
, plural (badadr
), unkadr
, plural (badadr
));
1355 die (NULL
, "%d address%s unparsable", badadr
, plural (badadr
));
1357 die (NULL
, "%d addressee%s undeliverable", unkadr
, plural (unkadr
));
1362 do_addresses (int bccque
, int talk
)
1366 struct mailname
*lp
;
1369 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1370 if (lp
->m_bcc
? bccque
: !bccque
) {
1372 printf (" -- Local Recipients --\n");
1373 do_an_address (lp
, talk
);
1378 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1379 if (lp
->m_bcc
? bccque
: !bccque
) {
1381 printf (" -- UUCP Recipients --\n");
1382 do_an_address (lp
, talk
);
1387 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1388 if (lp
->m_bcc
? bccque
: !bccque
) {
1390 printf (" -- Network Recipients --\n");
1391 do_an_address (lp
, talk
);
1398 if (rp_isbad (retval
= mm_waend ()))
1399 die (NULL
, "problem ending addresses [%s]\n", rp_valstr (retval
));
1400 #endif /* MMDFMTS */
1403 if (rp_isbad (retval
= sm_waend ()))
1404 die (NULL
, "problem ending addresses; %s", rp_string (retval
));
1405 #endif /* SMTPMTS */
1410 * MTS-SPECIFIC INTERACTION
1415 * SENDMAIL/SMTP routines
1421 post (char *file
, int bccque
, int talk
)
1426 onex
= !(msgflags
& MINV
) || bccque
;
1428 if (msgflags
& MINV
)
1429 printf (" -- Posting for %s Recipients --\n",
1430 bccque
? "Blind" : "Sighted");
1432 printf (" -- Posting for All Recipients --\n");
1437 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, watch
, verbose
,
1438 snoop
, onex
, queued
, sasl
, saslmech
,
1440 || rp_isbad (retval
= sm_winit (smtpmode
, from
)))
1441 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1443 do_addresses (bccque
, talk
&& verbose
);
1444 if ((fd
= open (file
, O_RDONLY
)) == NOTOK
)
1445 die (file
, "unable to re-open");
1450 sm_end (onex
? OK
: DONE
);
1454 if (msgflags
& MINV
)
1455 printf (" -- %s Recipient Copies Posted --\n",
1456 bccque
? "Blind" : "Sighted");
1458 printf (" -- Recipient Copies Posted --\n");
1465 /* Address Verification */
1468 verify_all_addresses (int talk
)
1471 struct mailname
*lp
;
1475 if (!whomsw
|| checksw
)
1476 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, 0, 0, snoop
, 0,
1478 || rp_isbad (retval
= sm_winit (smtpmode
, from
)))
1479 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1481 if (talk
&& !whomsw
)
1482 printf (" -- Address Verification --\n");
1483 if (talk
&& localaddrs
.m_next
)
1484 printf (" -- Local Recipients --\n");
1485 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1486 do_an_address (lp
, talk
);
1488 if (talk
&& uuaddrs
.m_next
)
1489 printf (" -- UUCP Recipients --\n");
1490 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1491 do_an_address (lp
, talk
);
1493 if (talk
&& netaddrs
.m_next
)
1494 printf (" -- Network Recipients --\n");
1495 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1496 do_an_address (lp
, talk
);
1499 if (talk
&& !whomsw
)
1500 printf (" -- Address Verification Successful --\n");
1502 if (!whomsw
|| checksw
)
1511 do_an_address (struct mailname
*lp
, int talk
)
1517 switch (lp
->m_type
) {
1521 strncpy (addr
, mbox
, sizeof(addr
));
1525 mbox
= auxformat (lp
, 0);
1527 snprintf (addr
, sizeof(addr
), "%s!%s", lp
->m_host
, lp
->m_mbox
);
1530 default: /* let SendMail decide if the host is bad */
1533 snprintf (addr
, sizeof(addr
), "%s at %s", mbox
, host
);
1538 printf (" %s%s", addr
, whomsw
&& lp
->m_bcc
? "[BCC]" : "");
1540 if (whomsw
&& !checksw
) {
1548 switch (retval
= sm_wadr (mbox
, host
,
1549 lp
->m_type
!= UUCPHOST
? lp
->m_path
: NULL
)) {
1552 printf ("address ok\n");
1558 fprintf (stderr
, " %s: ", addr
);
1559 fprintf (talk
? stdout
: stderr
, "loses; %s\n",
1560 rp_string (retval
));
1566 fprintf (stderr
, " %s: ", addr
);
1567 die (NULL
, "unexpected response; %s", rp_string (retval
));
1575 do_text (char *file
, int fd
)
1580 lseek (fd
, (off_t
) 0, SEEK_SET
);
1582 while ((state
= read (fd
, buf
, sizeof(buf
))) > 0) {
1583 if (rp_isbad (retval
= sm_wtxt (buf
, state
)))
1584 die (NULL
, "problem writing text; %s\n", rp_string (retval
));
1588 die (file
, "problem reading from");
1590 switch (retval
= sm_wtend ()) {
1596 die (NULL
, "posting failed; %s", rp_string (retval
));
1599 die (NULL
, "unexpected response; %s", rp_string (retval
));
1603 #endif /* SMTPMTS */
1612 post (char *file
, int bccque
, int talk
)
1618 struct rp_bufstruct reply
;
1621 onex
= !(msgflags
& MINV
) || bccque
;
1623 if (msgflags
& MINV
)
1624 printf (" -- Posting for %s Recipients --\n",
1625 bccque
? "Blind" : "Sighted");
1627 printf (" -- Posting for All Recipients --\n");
1632 if (rp_isbad (retval
= mm_init ())
1633 || rp_isbad (retval
= mm_sbinit ())
1634 || rp_isbad (retval
= mm_winit (NULL
, submitopts
, from
)))
1635 die (NULL
, "problem initializing MMDF system [%s]",
1636 rp_valstr (retval
));
1638 if (rp_isbad (retval
= mm_rrply (&reply
, &len
)))
1639 die (NULL
, "problem with sender address [%s]",
1640 rp_valstr (retval
));
1643 do_addresses (bccque
, talk
&& verbose
);
1644 if ((fd
= open (file
, O_RDONLY
)) == NOTOK
)
1645 die (file
, "unable to re-open");
1655 if (msgflags
& MINV
)
1656 printf (" -- %s Recipient Copies Posted --\n",
1657 bccque
? "Blind" : "Sighted");
1659 printf (" -- Recipient Copies Posted --\n");
1664 /* Address Verification */
1667 verify_all_addresses (int talk
)
1670 struct mailname
*lp
;
1674 struct rp_bufstruct reply
;
1679 if (!whomsw
|| checksw
) {
1680 if (rp_isbad (retval
= mm_init ())
1681 || rp_isbad (retval
= mm_sbinit ())
1682 || rp_isbad (retval
= mm_winit (NULL
, submitopts
, from
)))
1683 die (NULL
, "problem initializing MMDF system [%s]",
1684 rp_valstr (retval
));
1686 if (rp_isbad (retval
= mm_rrply (&reply
, &len
)))
1687 die (NULL
, "problem with sender address [%s]", rp_valstr (retval
));
1691 if (talk
&& !whomsw
)
1692 printf (" -- Address Verification --\n");
1693 if (talk
&& localaddrs
.m_next
)
1694 printf (" -- Local Recipients --\n");
1695 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1696 do_an_address (lp
, talk
);
1698 if (talk
&& uuaddrs
.m_next
)
1699 printf (" -- UUCP Recipients --\n");
1700 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1701 do_an_address (lp
, talk
);
1703 if (talk
&& netaddrs
.m_next
)
1704 printf (" -- Network Recipients --\n");
1705 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1706 do_an_address (lp
, talk
);
1709 if (talk
&& !whomsw
)
1710 printf (" -- Address Verification Successful --\n");
1712 if (!whomsw
|| checksw
)
1721 do_an_address (struct mailname
*lp
, int talk
)
1724 char *mbox
, *host
, *text
, *path
;
1726 struct rp_bufstruct reply
;
1728 switch (lp
->m_type
) {
1731 host
= LocalName ();
1732 strncpy (addr
, mbox
, sizeof(addr
));
1736 fprintf (talk
? stdout
: stderr
, " %s!%s: %s\n",
1737 lp
->m_host
, lp
->m_mbox
, "not supported; UUCP address");
1742 default: /* let MMDF decide if the host is bad */
1745 snprintf (addr
, sizeof(addr
), "%s at %s", mbox
, host
);
1750 printf (" %s%s", addr
, whomsw
&& lp
->m_bcc
? "[BCC]" : "");
1752 if (whomsw
&& !checksw
) {
1762 path
= concat (lp
->m_path
, mbox
, "@", host
, NULL
);
1766 if (rp_isbad (retval
= mm_wadr (path
? NULL
: host
, path
? path
: mbox
))
1767 || rp_isbad (retval
= mm_rrply (&reply
, &len
)))
1768 die (NULL
, "problem submitting address [%s]", rp_valstr (retval
));
1770 switch (rp_gval (reply
.rp_val
)) {
1773 printf ("address ok\n");
1780 printf ("nameserver timeout - queued for checking\n");
1791 text
= "temporary nameserver failure";
1798 text
= "not deliverable";
1802 text
= "try again later";
1806 text
= "nothing done";
1811 fprintf (stderr
, " %s: ", addr
);
1812 text
= "unexpected response";
1813 die (NULL
, "%s;\n [%s] -- %s", text
,
1814 rp_valstr (reply
.rp_val
), reply
.rp_line
);
1818 fprintf (stderr
, " %s: ", addr
);
1819 fprintf (talk
? stdout
: stderr
, "%s;\n %s\n", text
, reply
.rp_line
);
1827 do_text (char *file
, int fd
)
1831 struct rp_bufstruct reply
;
1833 lseek (fd
, (off_t
) 0, SEEK_SET
);
1835 while ((state
= read (fd
, buf
, sizeof(buf
))) > 0) {
1836 if (rp_isbad (mm_wtxt (buf
, state
)))
1837 die (NULL
, "problem writing text [%s]\n", rp_valstr (retval
));
1841 die (file
, "problem reading from");
1843 if (rp_isbad (retval
= mm_wtend ()))
1844 die (NULL
, "problem ending text [%s]\n", rp_valstr (retval
));
1846 if (rp_isbad (retval
= mm_rrply (&reply
, &state
)))
1847 die (NULL
, "problem getting submission status [%s]\n",
1848 rp_valstr (retval
));
1850 switch (rp_gval (reply
.rp_val
)) {
1856 die (NULL
, "you lose; %s", reply
.rp_line
);
1859 die (NULL
, "no delivery occurred; %s", reply
.rp_line
);
1862 die (NULL
, "try again later; %s", reply
.rp_line
);
1865 die (NULL
, "nothing done; %s", reply
.rp_line
);
1868 die (NULL
, "unexpected response;\n\t[%s] -- %s",
1869 rp_valstr (reply
.rp_val
), reply
.rp_line
);
1873 #endif /* MMDFMTS */
1883 #ifndef RELIABLE_SIGNALS
1884 SIGNAL (i
, SIG_IGN
);
1888 if (msgflags
& MINV
)
1892 if (!whomsw
|| checksw
)
1894 #endif /* MMDFMTS */
1897 if (!whomsw
|| checksw
)
1899 #endif /* SMTPMTS */
1911 hstat
= SIGNAL2 (SIGHUP
, sigser
);
1912 istat
= SIGNAL2 (SIGINT
, sigser
);
1913 qstat
= SIGNAL2 (SIGQUIT
, sigser
);
1914 tstat
= SIGNAL2 (SIGTERM
, sigser
);
1924 SIGNAL (SIGHUP
, hstat
);
1925 SIGNAL (SIGINT
, istat
);
1926 SIGNAL (SIGQUIT
, qstat
);
1927 SIGNAL (SIGTERM
, tstat
);
1935 p_refile (char *file
)
1943 printf (" -- Filing Folder Copies --\n");
1944 for (i
= 0; i
< fccind
; i
++)
1945 fcc (file
, fccfold
[i
]);
1947 printf (" -- Folder Copies Filed --\n");
1952 * Call the `fileproc' to add the file to the folder.
1956 fcc (char *file
, char *folder
)
1963 printf (" %sFcc %s: ", msgstate
== RESENT
? "Resent-" : "", folder
);
1966 for (i
= 0; (child_id
= fork ()) == NOTOK
&& i
< 5; i
++)
1972 fprintf (stderr
, " %sFcc %s: ",
1973 msgstate
== RESENT
? "Resent-" : "", folder
);
1974 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
1978 /* see if we need to add `+' */
1979 snprintf (fold
, sizeof(fold
), "%s%s",
1980 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
1982 /* now exec the fileproc */
1983 execlp (fileproc
, r1bindex (fileproc
, '/'),
1984 "-link", "-file", file
, fold
, NULL
);
1988 if ((status
= pidwait (child_id
, OK
))) {
1990 fprintf (stderr
, " %sFcc %s: ",
1991 msgstate
== RESENT
? "Resent-" : "", folder
);
1992 pidstatus (status
, verbose
? stdout
: stderr
, NULL
);
1995 printf ("folder ok\n");
2007 die (char *what
, char *fmt
, ...)
2012 if (msgflags
& MINV
)
2016 if (!whomsw
|| checksw
)
2018 #endif /* MMDFMTS */
2021 if (!whomsw
|| checksw
)
2023 #endif /* SMTPMTS */
2026 advertise (what
, NULL
, fmt
, ap
);
2034 * err_abrt() is used by the mm_ routines
2035 * do not, under *ANY* circumstances, remove it from post,
2036 * or you will lose *BIG*
2040 err_abrt (int code
, char *fmt
, ...)
2042 char buffer
[BUFSIZ
];
2045 snprintf (buffer
, sizeof(buffer
), "[%s]", rp_valstr (code
));
2048 advertise (buffer
, NULL
, fmt
, ap
);
2053 #endif /* MMDFMTS */