1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2014 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #ifndef HAVE_AMALGAMATION
47 ((1024 / B64_ENCODE_INPUT_PER_LINE) * B64_ENCODE_INPUT_PER_LINE)
49 static char * _sendout_boundary
;
50 static bool_t _sendout_error
;
52 static enum okay
_putname(char const *line
, enum gfield w
,
53 enum sendaction action
, size_t *gotcha
,
54 char const *prefix
, FILE *fo
, struct name
**xp
);
56 /* Get an encoding flag based on the given string */
57 static char const * _get_encoding(const enum conversion convert
);
59 /* Write an attachment to the file buffer, converting to MIME */
60 static int _attach_file(struct attachment
*ap
, FILE *fo
);
61 static int __attach_file(struct attachment
*ap
, FILE *fo
);
63 static char const ** _prepare_mta_args(struct name
*to
, struct header
*hp
);
65 /* Fix the header by glopping all of the expanded names from the distribution
66 * list into the appropriate fields */
67 static struct name
* fixhead(struct header
*hp
, struct name
*tolist
);
69 /* Put the signature file at fo. TODO layer rewrite: *integrate in body*!! */
70 static int put_signature(FILE *fo
, int convert
);
72 /* Attach a message to the file buffer */
73 static int attach_message(struct attachment
*ap
, FILE *fo
);
75 /* Generate the body of a MIME multipart message */
76 static int make_multipart(struct header
*hp
, int convert
, FILE *fi
,
77 FILE *fo
, char const *contenttype
, char const *charset
);
79 /* Prepend a header in front of the collected stuff and return the new file */
80 static FILE * infix(struct header
*hp
, FILE *fi
);
82 /* Save the outgoing mail on the passed file */
83 static int savemail(char const *name
, FILE *fi
);
85 /* Send mail to a bunch of user names. The interface is through mail() */
86 static int sendmail_internal(void *v
, int recipient_record
);
88 static enum okay
transfer(struct name
*to
, FILE *input
, struct header
*hp
);
90 /* Start the MTA mailing to namelist and stdin redirected to input */
91 static enum okay
start_mta(struct name
*to
, FILE *input
, struct header
*hp
);
93 /* Record outgoing mail if instructed to do so; in *record* unless to is set */
94 static enum okay
mightrecord(FILE *fp
, struct name
*to
);
96 /* Create a Message-Id: header field. Use either host name or from address */
97 static void message_id(FILE *fo
, struct header
*hp
);
99 /* Format the given header line to not exceed 72 characters */
100 static int fmt(char const *str
, struct name
*np
, FILE *fo
, int comma
,
101 int dropinvalid
, int domime
);
103 /* Rewrite a message for resending, adding the Resent-Headers */
104 static int infix_resend(FILE *fi
, FILE *fo
, struct message
*mp
,
105 struct name
*to
, int add_resent
);
108 _putname(char const *line
, enum gfield w
, enum sendaction action
,
109 size_t *gotcha
, char const *prefix
, FILE *fo
, struct name
**xp
)
115 np
= lextract(line
, GEXTRA
| GFULL
);
120 else if (fmt(prefix
, np
, fo
, w
& GCOMMA
, 0, (action
!= SEND_TODISP
)))
122 else if (gotcha
!= NULL
)
129 _get_encoding(enum conversion
const convert
)
135 case CONV_7BIT
: rv
= "7bit"; break;
136 case CONV_8BIT
: rv
= "8bit"; break;
137 case CONV_TOQP
: rv
= "quoted-printable"; break;
138 case CONV_TOB64
: rv
= "base64"; break;
139 default: rv
= NULL
; break;
146 _attach_file(struct attachment
*ap
, FILE *fo
)
148 /* TODO of course, the MIME classification needs to performed once
149 * TODO only, not for each and every charset anew ... ;-// */
150 char *charset_iter_orig
[2];
155 /* Is this already in target charset? Simply copy over */
156 if (ap
->a_conv
== AC_TMPFILE
) {
157 err
= __attach_file(ap
, fo
);
159 DBG( ap
->a_tmpf
= NULL
; )
163 /* If we don't apply charset conversion at all (fixed input=ouput charset)
164 * we also simply copy over, since it's the users desire */
165 if (ap
->a_conv
== AC_FIX_INCS
) {
166 ap
->a_charset
= ap
->a_input_charset
;
167 err
= __attach_file(ap
, fo
);
171 /* Otherwise we need to iterate over all possible output charsets */
172 if ((offs
= ftell(fo
)) == -1) {
176 charset_iter_recurse(charset_iter_orig
);
177 for (charset_iter_reset(NULL
);; charset_iter_next()) {
178 if (!charset_iter_is_valid()) {
182 err
= __attach_file(ap
, fo
);
183 if (err
== 0 || (err
!= EILSEQ
&& err
!= EINVAL
))
186 if (fseek(fo
, offs
, SEEK_SET
) == -1) {
190 if (ap
->a_conv
!= AC_DEFAULT
) {
194 ap
->a_charset
= NULL
;
196 charset_iter_restore(charset_iter_orig
);
203 __attach_file(struct attachment
*ap
, FILE *fo
) /* XXX linelength */
205 int err
= 0, do_iconv
;
208 enum conversion convert
;
210 size_t bufsize
, lncnt
, inlen
;
213 /* Either charset-converted temporary file, or plain path */
214 if (ap
->a_conv
== AC_TMPFILE
) {
216 assert(ftell(fi
) == 0);
217 } else if ((fi
= Fopen(ap
->a_name
, "r")) == NULL
) {
223 /* MIME part header for attachment */
224 { char const *bn
= ap
->a_name
, *ct
;
226 if ((ct
= strrchr(bn
, '/')) != NULL
)
228 ct
= ap
->a_content_type
;
229 charset
= ap
->a_charset
;
230 convert
= mime_classify_file(fi
, (char const**)&ct
, &charset
, &do_iconv
);
231 if (charset
== NULL
|| ap
->a_conv
== AC_FIX_INCS
||
232 ap
->a_conv
== AC_TMPFILE
)
235 if (fprintf(fo
, "\n--%s\nContent-Type: %s", _sendout_boundary
, ct
) == -1)
238 if (charset
== NULL
) {
239 if (putc('\n', fo
) == EOF
)
241 } else if (fprintf(fo
, "; charset=%s\n", charset
) == -1)
244 if (fprintf(fo
, "Content-Transfer-Encoding: %s\n"
245 "Content-Disposition: %s;\n filename=\"",
246 _get_encoding(convert
), ap
->a_content_disposition
) == -1)
248 if (xmime_write(bn
, strlen(bn
), fo
, CONV_TOHDR
, TD_NONE
, NULL
) < 0)
250 if (fwrite("\"\n", sizeof(char), 2, fo
) != 2 * sizeof(char))
253 if ((bn
= ap
->a_content_id
) != NULL
&&
254 fprintf(fo
, "Content-ID: %s\n", bn
) == -1)
257 if ((bn
= ap
->a_content_description
) != NULL
&&
258 fprintf(fo
, "Content-Description: %s\n", bn
) == -1)
261 if (putc('\n', fo
) == EOF
) {
269 if (iconvd
!= (iconv_t
)-1)
270 n_iconv_close(iconvd
);
272 char const *tcs
= charset_get_lc();
273 if (asccasecmp(charset
, tcs
) &&
274 (iconvd
= n_iconv_open(charset
, tcs
)) == (iconv_t
)-1 &&
275 (err
= errno
) != 0) {
277 fprintf(stderr
, tr(179, "Cannot convert from %s to %s\n"),
280 perror("iconv_open");
287 buf
= smalloc(bufsize
);
288 if (convert
== CONV_TOQP
290 || iconvd
!= (iconv_t
)-1
295 if (convert
== CONV_TOQP
297 || iconvd
!= (iconv_t
)-1
300 if (fgetline(&buf
, &bufsize
, &lncnt
, &inlen
, fi
, 0) == NULL
)
302 } else if ((inlen
= fread(buf
, sizeof *buf
, bufsize
, fi
)) == 0)
304 if (xmime_write(buf
, inlen
, fo
, convert
, TD_ICONV
, NULL
) < 0) {
314 if (ap
->a_conv
!= AC_TMPFILE
)
322 _prepare_mta_args(struct name
*to
, struct header
*hp
)
328 i
= 4 + smopts_count
+ 2 + count(to
) + 1;
329 args
= salloc(i
* sizeof(char*));
331 args
[0] = ok_vlook(sendmail_progname
);
332 if (args
[0] == NULL
|| *args
[0] == '\0')
333 args
[0] = SENDMAIL_PROGNAME
;
339 if (options
& OPT_VERBOSE
)
342 for (j
= 0; j
< smopts_count
; ++j
, ++i
)
345 /* -r option? We may only pass skinned addresses */
346 if (options
& OPT_r_FLAG
) {
349 if (option_r_arg
[0] != '\0')
350 froma
= option_r_arg
;
351 else if (hp
!= NULL
) {
352 /* puthead() did it, then */
353 assert(hp
->h_from
!= NULL
);
354 froma
= hp
->h_from
->n_name
;
356 froma
= skin(myorigin(NULL
)); /* XXX ugh! ugh!! */
357 if (froma
!= NULL
) { /* XXX ugh! */
363 /* Receivers follow */
364 for (; to
!= NULL
; to
= to
->n_flink
)
365 if (!(to
->n_type
& GDEL
))
366 args
[i
++] = to
->n_name
;
373 fixhead(struct header
*hp
, struct name
*tolist
)
375 struct name
**npp
, *np
;
378 tolist
= elide(tolist
);
380 hp
->h_to
= hp
->h_cc
= hp
->h_bcc
= NULL
;
381 for (np
= tolist
; np
!= NULL
; np
= np
->n_flink
) {
382 switch (np
->n_type
& (GDEL
| GMASK
)) {
383 case GTO
: npp
= &hp
->h_to
; break;
384 case GCC
: npp
= &hp
->h_cc
; break;
385 case GBCC
: npp
= &hp
->h_bcc
; break;
388 *npp
= cat(*npp
, ndup(np
, np
->n_type
| GFULL
));
395 put_signature(FILE *fo
, int convert
)
397 char buf
[INFIX_BUF
], *sig
, c
= '\n';
403 if ((sig
= ok_vlook(signature
)) == NULL
|| *sig
== '\0') {
409 if ((sig
= file_expand(sig
)) == NULL
)
412 if ((fsig
= Fopen(sig
, "r")) == NULL
) {
416 while ((sz
= fread(buf
, sizeof *buf
, INFIX_BUF
, fsig
)) != 0) {
418 if (xmime_write(buf
, sz
, fo
, convert
, TD_NONE
, NULL
) < 0)
438 attach_message(struct attachment
*ap
, FILE *fo
)
445 fprintf(fo
, "\n--%s\nContent-Type: message/rfc822\n"
446 "Content-Disposition: inline\n", _sendout_boundary
);
447 if ((ccp
= ap
->a_content_description
) != NULL
)
448 fprintf(fo
, "Content-Description: %s\n", ccp
);
451 mp
= message
+ ap
->a_msgno
- 1;
453 rv
= (sendmp(mp
, fo
, 0, NULL
, SEND_RFC822
, NULL
) < 0) ? -1 : 0;
459 make_multipart(struct header
*hp
, int convert
, FILE *fi
, FILE *fo
,
460 char const *contenttype
, char const *charset
)
462 struct attachment
*att
;
466 fputs("This is a multi-part message in MIME format.\n", fo
);
467 if (fsize(fi
) != 0) {
469 size_t sz
, bufsize
, cnt
;
471 fprintf(fo
, "\n--%s\n", _sendout_boundary
);
472 fprintf(fo
, "Content-Type: %s", contenttype
);
474 fprintf(fo
, "; charset=%s", charset
);
475 fprintf(fo
, "\nContent-Transfer-Encoding: %s\n"
476 "Content-Disposition: inline\n\n", _get_encoding(convert
));
478 buf
= smalloc(bufsize
= INFIX_BUF
);
479 if (convert
== CONV_TOQP
481 || iconvd
!= (iconv_t
)-1
488 if (convert
== CONV_TOQP
490 || iconvd
!= (iconv_t
)-1
493 if (fgetline(&buf
, &bufsize
, &cnt
, &sz
, fi
, 0) == NULL
)
495 } else if ((sz
= fread(buf
, sizeof *buf
, bufsize
, fi
)) == 0)
498 if (xmime_write(buf
, sz
, fo
, convert
, TD_ICONV
, NULL
) < 0) {
508 put_signature(fo
, convert
);
511 for (att
= hp
->h_attach
; att
!= NULL
; att
= att
->a_flink
) {
513 if (attach_message(att
, fo
) != 0)
515 } else if (_attach_file(att
, fo
) != 0)
519 /* the final boundary with two attached dashes */
520 fprintf(fo
, "\n--%s--\n", _sendout_boundary
);
528 infix(struct header
*hp
, FILE *fi
) /* TODO check */
530 FILE *nfo
, *nfi
= NULL
;
532 char const *contenttype
, *charset
= NULL
;
533 enum conversion convert
;
534 int do_iconv
= 0, err
;
536 char const *tcs
, *convhdr
= NULL
;
540 if ((nfo
= Ftmp(&tempMail
, "infix", OF_WRONLY
| OF_HOLDSIGS
| OF_REGISTER
,
542 perror(tr(178, "temporary mail file"));
545 if ((nfi
= Fopen(tempMail
, "r")) == NULL
) {
549 Ftmp_release(&tempMail
);
553 contenttype
= "text/plain"; /* XXX mail body - always text/plain, want XX? */
554 convert
= mime_classify_file(fi
, &contenttype
, &charset
, &do_iconv
);
557 tcs
= charset_get_lc();
558 if ((convhdr
= need_hdrconv(hp
, GTO
| GSUBJECT
| GCC
| GBCC
| GIDENT
))) {
559 if (iconvd
!= (iconv_t
)-1) /* XXX */
560 n_iconv_close(iconvd
);
561 if (asccasecmp(convhdr
, tcs
) != 0 &&
562 (iconvd
= n_iconv_open(convhdr
, tcs
)) == (iconv_t
)-1 &&
568 (GTO
| GSUBJECT
| GCC
| GBCC
| GNL
| GCOMMA
| GUA
| GMIME
| GMSGID
|
569 GIDENT
| GREF
| GDATE
), SEND_MBOX
, convert
, contenttype
, charset
))
572 if (iconvd
!= (iconv_t
)-1)
573 n_iconv_close(iconvd
);
577 if (do_iconv
&& charset
!= NULL
) { /*TODO charset->mime_classify_file*/
578 if (asccasecmp(charset
, tcs
) != 0 &&
579 (iconvd
= n_iconv_open(charset
, tcs
)) == (iconv_t
)-1 &&
580 (err
= errno
) != 0) {
583 fprintf(stderr
, tr(179, "Cannot convert from %s to %s\n"),
586 perror("iconv_open");
592 if (hp
->h_attach
!= NULL
) {
593 if (make_multipart(hp
, convert
, fi
, nfo
, contenttype
, charset
) != 0)
596 size_t sz
, bufsize
, cnt
;
599 if (convert
== CONV_TOQP
601 || iconvd
!= (iconv_t
)-1
607 buf
= smalloc(bufsize
= INFIX_BUF
);
609 if (convert
== CONV_TOQP
611 || iconvd
!= (iconv_t
)-1
614 if (fgetline(&buf
, &bufsize
, &cnt
, &sz
, fi
, 0) == NULL
)
616 } else if ((sz
= fread(buf
, sizeof *buf
, bufsize
, fi
)) == 0)
618 if (xmime_write(buf
, sz
, nfo
, convert
, TD_ICONV
, NULL
) < 0) {
625 if (err
|| ferror(fi
)) {
630 if (iconvd
!= (iconv_t
)-1)
631 n_iconv_close(iconvd
);
637 put_signature(nfo
, convert
); /* XXX if (text/) !! */
641 if (iconvd
!= (iconv_t
)-1)
642 n_iconv_close(iconvd
);
646 if ((err
= ferror(nfo
)))
647 perror(tr(180, "temporary mail file"));
662 savemail(char const *name
, FILE *fi
)
666 size_t bufsize
, buflen
, cnt
;
667 int prependnl
= 0, rv
= -1;
670 buf
= smalloc(bufsize
= LINESIZE
);
672 if ((fo
= Zopen(name
, "a+", NULL
)) == NULL
) {
673 if ((fo
= Zopen(name
, "wx", NULL
)) == NULL
) {
678 if (fseek(fo
, -2L, SEEK_END
) == 0) {
679 switch (fread(buf
, sizeof *buf
, 2, fo
)) {
681 if (buf
[1] != '\n') {
703 fprintf(fo
, "From %s %s", myname
, time_current
.tc_ctime
);
707 while (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fi
, 0) != NULL
) {
708 #ifdef HAVE_DEBUG /* TODO assert legacy */
709 assert(!is_head(buf
, buflen
));
711 if (is_head(buf
, buflen
))
714 fwrite(buf
, sizeof *buf
, buflen
, fo
);
716 if (buflen
&& *(buf
+ buflen
- 1) != '\n')
736 sendmail_internal(void *v
, int recipient_record
)
743 memset(&head
, 0, sizeof head
);
744 head
.h_to
= lextract(str
, GTO
| GFULL
);
745 rv
= mail1(&head
, 0, NULL
, NULL
, recipient_record
, 0);
751 transfer(struct name
*to
, FILE *input
, struct header
*hp
)
753 char o
[LINESIZE
], *cp
;
761 snprintf(o
, sizeof o
, "smime-encrypt-%s", np
->n_name
);/* XXX */
762 if ((cp
= vok_vlook(o
)) != NULL
) {
767 if ((ef
= smime_encrypt(input
, cp
, np
->n_name
)) != NULL
) {
768 nt
= ndup(np
, np
->n_type
& ~(GFULL
| GSKIN
));
769 rv
= start_mta(nt
, ef
, hp
);
773 fprintf(stderr
, tr(225, "No SSL support compiled in.\n"));
776 fprintf(stderr
, tr(38, "Message not sent to <%s>\n"), np
->n_name
);
777 _sendout_error
= TRU1
;
783 if (np
->n_flink
!= NULL
)
784 np
->n_flink
->n_blink
= np
->n_blink
;
785 if (np
->n_blink
!= NULL
)
786 np
->n_blink
->n_flink
= np
->n_flink
;
797 if (ok_blook(smime_force_encryption
) || start_mta(to
, input
, hp
) != OKAY
)
804 start_mta(struct name
*to
, FILE *input
, struct header
*hp
)
810 char const **args
= NULL
, **t
, *mta
;
818 if ((smtp
= ok_vlook(smtp
)) == NULL
) {
819 if ((mta
= ok_vlook(sendmail
)) != NULL
) {
820 if ((mta
= file_expand(mta
)) == NULL
)
825 args
= _prepare_mta_args(to
, hp
);
826 if (options
& OPT_DEBUG
) {
827 printf(tr(181, "Sendmail arguments:"));
828 for (t
= args
; *t
!= NULL
; ++t
)
829 printf(" \"%s\"", *t
);
835 mta
= NULL
; /* Silence cc */
837 fputs(tr(194, "No SMTP support compiled in.\n"), stderr
);
840 if (!url_parse(&url
, CPROTO_SMTP
, smtp
)) /* v15-compat: check EARLY! */
843 smtp
= skin(myorigin(hp
));
844 if (ok_blook(v15_compat
)) {
847 /* Can we use the URL spec, or need we be relative to *from*? */
848 if (url
.url_had_user
)
851 if (!url_parse(&u2
, CPROTO_SMTP
, smtp
))/*v15-compat: check EARLY!*/
856 if (!ccred_lookup(&ccred
, up
))
858 } else if (!ccred_lookup_old(&ccred
, CPROTO_SMTP
, smtp
))
860 url
.url_uh
.l
= strlen(url
.url_uh
.s
= smtp
);
864 /* Fork, set up the temporary mail file as standard input for "mail", and
865 * exec with the user list we generated far above */
866 if ((pid
= fork()) == -1) {
869 savedeadletter(input
, 0);
870 _sendout_error
= TRU1
;
875 sigaddset(&nset
, SIGHUP
);
876 sigaddset(&nset
, SIGINT
);
877 sigaddset(&nset
, SIGQUIT
);
878 sigaddset(&nset
, SIGTSTP
);
879 sigaddset(&nset
, SIGTTIN
);
880 sigaddset(&nset
, SIGTTOU
);
881 freopen("/dev/null", "r", stdin
);
884 prepare_child(&nset
, 0, 1);
885 if (smtp_mta(&url
, to
, input
, hp
, &ccred
) == 0)
889 prepare_child(&nset
, fileno(input
), -1);
890 /* If *record* is set then savemail() will move the file position;
891 * it'll call rewind(), but that may optimize away the systemcall if
892 * possible, and since dup2() shares the position with the original FD
893 * the MTA may end up reading nothing */
894 lseek(0, 0, SEEK_SET
);
895 execv(mta
, UNCONST(args
));
900 savedeadletter(input
, 1);
901 fputs(tr(182, "... message not sent.\n"), stderr
);
904 if ((options
& (OPT_DEBUG
| OPT_VERBOSE
| OPT_BATCH_FLAG
)) ||
905 ok_blook(sendwait
)) {
906 if (wait_child(pid
, NULL
))
909 _sendout_error
= TRU1
;
920 mightrecord(FILE *fp
, struct name
*to
)
928 cp
= savestr(skinned_name(to
));
929 for (cq
= cp
; *cq
!= '\0' && *cq
!= '@'; ++cq
)
933 cp
= ok_vlook(record
);
936 if ((ep
= expand(cp
)) == NULL
) {
941 if (*ep
!= '/' && *ep
!= '+' && ok_blook(outfolder
) &&
942 which_protocol(ep
) == PROTO_FILE
) {
943 size_t i
= strlen(cp
);
944 cq
= salloc(i
+ 1 +1);
946 memcpy(cq
+ 1, cp
, i
+1);
948 if ((ep
= file_expand(cp
)) == NULL
) {
954 if (savemail(ep
, fp
) != 0) {
956 fprintf(stderr
, tr(285, "Failed to save message in %s - "
957 "message not sent\n"), ep
);
958 exit_status
|= EXIT_ERR
;
959 savedeadletter(fp
, 1);
968 message_id(FILE *fo
, struct header
*hp
)
975 if (ok_blook(message_id_disable
))
978 if ((h
= ok_vlook(hostname
)) != NULL
)
980 else if ((h
= skin(myorigin(hp
))) != NULL
&& strchr(h
, '@') != NULL
)
986 tmp
= &time_current
.tc_gm
;
987 fprintf(fo
, "Message-ID: <%04d%02d%02d%02d%02d%02d.%s%c%s>\n",
988 tmp
->tm_year
+ 1900, tmp
->tm_mon
+ 1, tmp
->tm_mday
,
989 tmp
->tm_hour
, tmp
->tm_min
, tmp
->tm_sec
,
990 getrandstring(rl
), (rl
== 16 ? '%' : '@'), h
);
996 fmt(char const *str
, struct name
*np
, FILE *fo
, int flags
, int dropinvalid
,
1004 } m
= (flags
& GCOMMA
) ? m_COMMA
: 0;
1011 fwrite(str
, sizeof *str
, col
, fo
);
1014 if (col
== 9 && !asccasecmp(str
, "reply-to:")) {
1018 if (ok_blook(add_file_recipients
))
1020 if ((col
== 3 && (!asccasecmp(str
, "to:") || !asccasecmp(str
, "cc:"))) ||
1021 (col
== 4 && !asccasecmp(str
, "bcc:")) ||
1022 (col
== 10 && !asccasecmp(str
, "Resent-To:")))
1026 for (; np
!= NULL
; np
= np
->n_flink
) {
1027 if ((m
& m_NOPF
) && is_fileorpipe_addr(np
))
1029 if (is_addr_invalid(np
, !dropinvalid
)) {
1035 if ((m
& (m_INIT
| m_COMMA
)) == (m_INIT
| m_COMMA
)) {
1040 len
= strlen(np
->n_fullname
);
1041 ++col
; /* The separating space */
1042 if ((m
& m_INIT
) && col
> 1 && col
+ len
> 72) {
1048 m
= (m
& ~m_CSEEN
) | m_INIT
;
1049 len
= xmime_write(np
->n_fullname
, len
, fo
,
1050 (domime
? CONV_TOHDR_A
: CONV_NONE
), TD_ICONV
, NULL
);
1063 infix_resend(FILE *fi
, FILE *fo
, struct message
*mp
, struct name
*to
,
1066 size_t cnt
, c
, bufsize
= 0;
1069 struct name
*fromfield
= NULL
, *senderfield
= NULL
;
1075 /* Write the Resent-Fields */
1077 fputs("Resent-", fo
);
1079 if ((cp
= myaddrs(NULL
)) != NULL
) {
1080 if (_putname(cp
, GCOMMA
, SEND_MBOX
, NULL
, "Resent-From:", fo
,
1084 if ((cp
= ok_vlook(sender
)) != NULL
) {
1085 if (_putname(cp
, GCOMMA
, SEND_MBOX
, NULL
, "Resent-Sender:", fo
,
1089 if (fmt("Resent-To:", to
, fo
, 1, 1, 0))
1091 if ((cp
= ok_vlook(stealthmua
)) == NULL
|| !strcmp(cp
, "noagent")) {
1092 fputs("Resent-", fo
);
1093 message_id(fo
, NULL
);
1096 if (check_from_and_sender(fromfield
, senderfield
))
1099 /* Write the original headers */
1101 if (fgetline(&buf
, &bufsize
, &cnt
, &c
, fi
, 0) == NULL
)
1103 /* XXX more checks: The From_ line may be seen when resending */
1104 /* During headers is_head() is actually overkill, so ^From_ is sufficient
1105 * && !is_head(buf, c) */
1106 if (ascncasecmp("status: ", buf
, 8) && strncmp("From ", buf
, 5))
1107 fwrite(buf
, sizeof *buf
, c
, fo
);
1108 if (cnt
> 0 && *buf
== '\n')
1112 /* Write the message body */
1114 if (fgetline(&buf
, &bufsize
, &cnt
, &c
, fi
, 0) == NULL
)
1116 if (cnt
== 0 && *buf
== '\n')
1118 fwrite(buf
, sizeof *buf
, c
, fo
);
1123 perror(tr(188, "temporary mail file"));
1133 mail(struct name
*to
, struct name
*cc
, struct name
*bcc
, char *subject
,
1134 struct attachment
*attach
, char *quotefile
, int recipient_record
)
1140 memset(&head
, 0, sizeof head
);
1142 /* The given subject may be in RFC1522 format. */
1143 if (subject
!= NULL
) {
1145 in
.l
= strlen(subject
);
1146 mime_fromhdr(&in
, &out
, /* TODO ??? TD_ISPR |*/ TD_ICONV
);
1147 head
.h_subject
= out
.s
;
1149 if (!(options
& OPT_t_FLAG
)) {
1154 head
.h_attach
= attach
;
1156 mail1(&head
, 0, NULL
, quotefile
, recipient_record
, 0);
1158 if (subject
!= NULL
)
1170 rv
= sendmail_internal(v
, 0);
1181 rv
= sendmail_internal(v
, 1);
1187 mail1(struct header
*hp
, int printheaders
, struct message
*quote
,
1188 char *quotefile
, int recipient_record
, int doprefix
)
1192 int dosign
= -1, err
;
1194 enum okay rv
= STOP
;
1197 _sendout_error
= FAL0
;
1199 /* Update some globals we likely need first */
1200 time_current_update(&time_current
, TRU1
);
1203 if ((cp
= ok_vlook(autocc
)) != NULL
&& *cp
!= '\0')
1204 hp
->h_cc
= cat(hp
->h_cc
, checkaddrs(lextract(cp
, GCC
| GFULL
)));
1205 if ((cp
= ok_vlook(autobcc
)) != NULL
&& *cp
!= '\0')
1206 hp
->h_bcc
= cat(hp
->h_bcc
, checkaddrs(lextract(cp
, GBCC
| GFULL
)));
1208 /* Collect user's mail from standard input. Get the result as mtf */
1209 mtf
= collect(hp
, printheaders
, quote
, quotefile
, doprefix
);
1213 if (options
& OPT_INTERACTIVE
) {
1214 err
= (ok_blook(bsdcompat
) || ok_blook(askatend
));
1217 if (ok_blook(askcc
))
1218 ++err
, grab_headers(hp
, GCC
, 1);
1219 if (ok_blook(askbcc
))
1220 ++err
, grab_headers(hp
, GBCC
, 1);
1221 if (ok_blook(askattach
))
1222 ++err
, edit_attachments(&hp
->h_attach
);
1223 if (ok_blook(asksign
))
1224 ++err
, dosign
= getapproval(tr(35, "Sign this message (y/n)? "), TRU1
);
1227 printf(tr(183, "EOT\n"));
1232 if (fsize(mtf
) == 0) {
1233 if (options
& OPT_E_FLAG
)
1235 if (hp
->h_subject
== NULL
)
1236 printf(tr(184, "No message, no subject; hope that's ok\n"));
1237 else if (ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
1238 printf(tr(185, "Null message body; hope that's ok\n"));
1242 dosign
= ok_blook(smime_sign
);
1245 fprintf(stderr
, tr(225, "No SSL support compiled in.\n"));
1250 /* XXX Update time_current again; once collect() offers editing of more
1251 * XXX headers, including Date:, this must only happen if Date: is the
1252 * XXX same that it was before collect() (e.g., postponing etc.).
1253 * XXX But *do* update otherwise because the mail seems to be backdated
1254 * XXX if the user edited some time, which looks odd and it happened
1255 * XXX to me that i got mis-dated response mails due to that... */
1256 time_current_update(&time_current
, TRU1
);
1258 /* TODO hrmpf; the MIME/send layer rewrite MUST address the init crap:
1259 * TODO setup the header ONCE; note this affects edit.c, collect.c ...,
1260 * TODO but: offer a hook that rebuilds/expands/checks/fixates all
1261 * TODO header fields ONCE, call that ONCE after user editing etc. has
1262 * TODO completed (one edit cycle) */
1264 /* Take the user names from the combined to and cc lists and do all the
1265 * alias processing. The POSIX standard says:
1266 * The names shall be substituted when alias is used as a recipient
1267 * address specified by the user in an outgoing message (that is,
1268 * other recipients addressed indirectly through the reply command
1269 * shall not be substituted in this manner).
1270 * S-nail thus violates POSIX, as has been pointed out correctly by
1271 * Martin Neitzel, but logic and usability of POSIX standards is not seldom
1272 * disputable anyway. Go for user friendliness */
1274 /* Do alias expansion on Reply-To: members, too */
1275 /* TODO puthead() YET (!!! see ONCE note above) expands the value, but
1276 * TODO doesn't perform alias expansion; encapsulate in the ONCE-o */
1277 if (hp
->h_replyto
== NULL
&& (cp
= ok_vlook(replyto
)) != NULL
)
1278 hp
->h_replyto
= checkaddrs(lextract(cp
, GEXTRA
| GFULL
));
1279 if (hp
->h_replyto
!= NULL
)
1280 hp
->h_replyto
= elide(usermap(hp
->h_replyto
, TRU1
));
1282 /* TODO what happens now is that all recipients are merged into
1283 * TODO a duplicated list with expanded aliases, then this list is
1284 * TODO splitted again into the three individual recipient lists (with
1285 * TODO duplicates removed).
1286 * TODO later on we use the merged list for outof() pipe/file saving,
1287 * TODO then we eliminate duplicates (again) and then we use that one
1288 * TODO for mightrecord() and transfer(), and again. ... Please ... */
1290 /* NOTE: Due to elide() in fixhead(), ENSURE to,cc,bcc order of to!,
1291 * because otherwise the recipients will be "degraded" if they occur
1293 to
= usermap(cat(hp
->h_to
, cat(hp
->h_cc
, hp
->h_bcc
)), FAL0
);
1295 fprintf(stderr
, tr(186, "No recipients specified\n"));
1296 _sendout_error
= TRU1
;
1298 to
= fixhead(hp
, to
);
1300 /* 'Bit ugly kind of control flow until we find a charset that does it */
1301 for (charset_iter_reset(hp
->h_charset
);; charset_iter_next()) {
1302 if (!charset_iter_is_valid())
1304 else if ((nmtf
= infix(hp
, mtf
)) != NULL
)
1306 else if ((err
= errno
) == EILSEQ
|| err
== EINVAL
) {
1315 _sendout_error
= TRU1
;
1316 savedeadletter(mtf
, 1);
1317 fputs(tr(182, "... message not sent.\n"), stderr
);
1324 if ((nmtf
= smime_sign(mtf
, skin(myorigin(hp
)))) == NULL
)
1331 /* TODO truly - i still don't get what follows: (1) we deliver file
1332 * TODO and pipe addressees, (2) we mightrecord() and (3) we transfer
1333 * TODO even if (1) savedeadletter() etc. To me this doesn't make sense? */
1335 /* Deliver pipe and file addressees */
1336 to
= outof(to
, mtf
, &_sendout_error
);
1338 savedeadletter(mtf
, 0);
1340 to
= elide(to
); /* XXX needed only to drop GDELs due to outof()! */
1341 { ui32_t cnt
= count(to
);
1342 if ((!recipient_record
|| cnt
> 0) &&
1343 mightrecord(mtf
, (recipient_record
? to
: NULL
)) != OKAY
)
1346 rv
= transfer(to
, mtf
, hp
);
1347 else if (!_sendout_error
)
1354 exit_status
|= EXIT_SEND_ERROR
;
1360 mkdate(FILE *fo
, char const *field
)
1363 int tzdiff
, tzdiff_hour
, tzdiff_min
, rv
;
1366 tzdiff
= time_current
.tc_time
- mktime(&time_current
.tc_gm
);
1367 tzdiff_hour
= (int)(tzdiff
/ 60);
1368 tzdiff_min
= tzdiff_hour
% 60;
1370 tmptr
= &time_current
.tc_local
;
1371 if (tmptr
->tm_isdst
> 0)
1373 rv
= fprintf(fo
, "%s: %s, %02d %s %04d %02d:%02d:%02d %+05d\n",
1375 weekday_names
[tmptr
->tm_wday
],
1376 tmptr
->tm_mday
, month_names
[tmptr
->tm_mon
],
1377 tmptr
->tm_year
+ 1900, tmptr
->tm_hour
,
1378 tmptr
->tm_min
, tmptr
->tm_sec
,
1379 tzdiff_hour
* 100 + tzdiff_min
);
1385 puthead(struct header
*hp
, FILE *fo
, enum gfield w
, enum sendaction action
,
1386 enum conversion convert
, char const *contenttype
, char const *charset
)
1388 #define FMT_CC_AND_BCC() \
1390 if (hp->h_cc != NULL && (w & GCC)) {\
1391 if (fmt("Cc:", hp->h_cc, fo, (w & (GCOMMA | GFILES)), 0,\
1392 (action != SEND_TODISP)))\
1396 if (hp->h_bcc != NULL && (w & GBCC)) {\
1397 if (fmt("Bcc:", hp->h_bcc, fo, (w & (GCOMMA | GFILES)), 0,\
1398 (action != SEND_TODISP)))\
1406 struct name
*np
, *fromfield
= NULL
, *senderfield
= NULL
;
1407 int stealthmua
, rv
= 1;
1411 if ((addr
= ok_vlook(stealthmua
)) != NULL
)
1412 stealthmua
= !strcmp(addr
, "noagent") ? -1 : 1;
1416 nodisp
= (action
!= SEND_TODISP
);
1419 mkdate(fo
, "Date"), ++gotcha
;
1421 if (hp
->h_from
!= NULL
) {
1422 if (fmt("From:", hp
->h_from
, fo
, (w
& (GCOMMA
| GFILES
)), 0, nodisp
))
1425 fromfield
= hp
->h_from
;
1426 } else if ((addr
= myaddrs(hp
)) != NULL
) {
1427 if (_putname(addr
, w
, action
, &gotcha
, "From:", fo
, &fromfield
))
1429 hp
->h_from
= fromfield
;
1432 if (((addr
= hp
->h_organization
) != NULL
||
1433 (addr
= ok_vlook(ORGANIZATION
)) != NULL
) &&
1434 (l
= strlen(addr
)) > 0) {
1435 fwrite("Organization: ", sizeof(char), 14, fo
);
1436 if (xmime_write(addr
, l
, fo
, (!nodisp
? CONV_NONE
: CONV_TOHDR
),
1437 (!nodisp
? TD_ISPR
| TD_ICONV
: TD_ICONV
), NULL
) < 0)
1443 /* TODO see the ONCE TODO note somewhere around this file;
1444 * TODO but anyway, do NOT perform alias expansion UNLESS
1445 * TODO we are actually sending out! */
1446 if (hp
->h_replyto
!= NULL
) {
1447 if (fmt("Reply-To:", hp
->h_replyto
, fo
, w
& GCOMMA
, 0, nodisp
))
1450 } else if ((addr
= ok_vlook(replyto
)) != NULL
)
1451 if (_putname(addr
, w
, action
, &gotcha
, "Reply-To:", fo
, NULL
))
1454 if (hp
->h_sender
!= NULL
) {
1455 if (fmt("Sender:", hp
->h_sender
, fo
, w
& GCOMMA
, 0, nodisp
))
1458 senderfield
= hp
->h_sender
;
1459 } else if ((addr
= ok_vlook(sender
)) != NULL
)
1460 if (_putname(addr
, w
, action
, &gotcha
, "Sender:", fo
, &senderfield
))
1463 if (check_from_and_sender(fromfield
, senderfield
))
1467 if (hp
->h_to
!= NULL
&& w
& GTO
) {
1468 if (fmt("To:", hp
->h_to
, fo
, (w
& (GCOMMA
| GFILES
)), 0, nodisp
))
1473 if (!ok_blook(bsdcompat
) && !ok_blook(bsdorder
))
1476 if (hp
->h_subject
!= NULL
&& (w
& GSUBJECT
)) {
1477 fwrite("Subject: ", sizeof (char), 9, fo
);
1478 if (!ascncasecmp(hp
->h_subject
, "re: ", 4)) {/* TODO localizable */
1479 fwrite("Re: ", sizeof(char), 4, fo
);
1480 if (strlen(hp
->h_subject
+ 4) > 0 &&
1481 xmime_write(hp
->h_subject
+ 4, strlen(hp
->h_subject
+ 4), fo
,
1482 (!nodisp
? CONV_NONE
: CONV_TOHDR
),
1483 (!nodisp
? TD_ISPR
| TD_ICONV
: TD_ICONV
), NULL
) < 0)
1485 } else if (*hp
->h_subject
!= '\0') {
1486 if (xmime_write(hp
->h_subject
, strlen(hp
->h_subject
), fo
,
1487 (!nodisp
? CONV_NONE
: CONV_TOHDR
),
1488 (!nodisp
? TD_ISPR
| TD_ICONV
: TD_ICONV
), NULL
) < 0)
1492 fwrite("\n", sizeof (char), 1, fo
);
1495 if (ok_blook(bsdcompat
) || ok_blook(bsdorder
))
1498 if ((w
& GMSGID
) && stealthmua
<= 0)
1499 message_id(fo
, hp
), ++gotcha
;
1501 if ((np
= hp
->h_ref
) != NULL
&& (w
& GREF
)) {
1502 fmt("References:", np
, fo
, 0, 1, 0);
1503 if (np
->n_name
!= NULL
) {
1504 while (np
->n_flink
!= NULL
)
1506 if (!is_addr_invalid(np
, 0)) {
1507 fprintf(fo
, "In-Reply-To: %s\n", np
->n_name
);
1513 if ((w
& GUA
) && stealthmua
== 0)
1514 fprintf(fo
, "User-Agent: %s %s\n", uagent
, version
), ++gotcha
;
1517 fputs("MIME-Version: 1.0\n", fo
), ++gotcha
;
1518 if (hp
->h_attach
!= NULL
) {
1519 _sendout_boundary
= mime_create_boundary();/*TODO carrier*/
1520 fprintf(fo
, "Content-Type: multipart/mixed;\n boundary=\"%s\"\n",
1523 fprintf(fo
, "Content-Type: %s", contenttype
);
1524 if (charset
!= NULL
)
1525 fprintf(fo
, "; charset=%s", charset
);
1526 fprintf(fo
, "\nContent-Transfer-Encoding: %s\n",
1527 _get_encoding(convert
));
1531 if (gotcha
&& (w
& GNL
))
1537 #undef FMT_CC_AND_BCC
1541 resend_msg(struct message
*mp
, struct name
*to
, int add_resent
) /* TODO check */
1543 FILE *ibuf
, *nfo
, *nfi
;
1545 enum okay rv
= STOP
;
1548 _sendout_error
= FAL0
;
1550 /* Update some globals we likely need first */
1551 time_current_update(&time_current
, TRU1
);
1553 if ((to
= checkaddrs(to
)) == NULL
) {
1554 _sendout_error
= TRU1
;
1558 if ((nfo
= Ftmp(&tempMail
, "resend", OF_WRONLY
| OF_HOLDSIGS
| OF_REGISTER
,
1560 _sendout_error
= TRU1
;
1561 perror(tr(189, "temporary mail file"));
1564 if ((nfi
= Fopen(tempMail
, "r")) == NULL
) {
1565 _sendout_error
= TRU1
;
1568 Ftmp_release(&tempMail
);
1572 if ((ibuf
= setinput(&mb
, mp
, NEED_BODY
)) == NULL
)
1575 if (infix_resend(ibuf
, nfo
, mp
, to
, add_resent
) != 0) {
1576 savedeadletter(nfi
, 1);
1577 fputs(tr(182, "... message not sent.\n"), stderr
);
1582 _sendout_error
= TRU1
;
1588 to
= outof(to
, nfi
, &_sendout_error
);
1590 savedeadletter(nfi
, 0);
1592 to
= elide(to
); /* TODO should have been done in fixhead()? */
1593 if (count(to
) != 0) {
1594 if (!ok_blook(record_resent
) || mightrecord(nfi
, to
) == OKAY
)
1595 rv
= transfer(to
, nfi
, NULL
);
1596 } else if (!_sendout_error
)
1602 exit_status
|= EXIT_SEND_ERROR
;
1607 /* vim:set fenc=utf-8:s-it-mode */