-
Notifications
You must be signed in to change notification settings - Fork 1
/
sendout.c
1534 lines (1474 loc) · 35.7 KB
/
sendout.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Heirloom mailx - a mail user agent derived from Berkeley Mail.
*
* Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
*/
/*
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
#ifdef DOSCCS
static char sccsid[] = "@(#)sendout.c 2.100 (gritter) 3/1/09";
#endif
#endif /* not lint */
#include "rcv.h"
#include "extern.h"
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include "md5.h"
/*
* Mail -- a mail program
*
* Mail to others.
*/
static char *send_boundary;
static char *getencoding(enum conversion convert);
static struct name *fixhead(struct header *hp, struct name *tolist);
static int put_signature(FILE *fo, int convert);
static int attach_file1(struct attachment *ap, FILE *fo, int dosign);
static int attach_file(struct attachment *ap, FILE *fo, int dosign);
static int attach_message(struct attachment *ap, FILE *fo, int dosign);
static int make_multipart(struct header *hp, int convert, FILE *fi, FILE *fo,
const char *contenttype, const char *charset, int dosign);
static FILE *infix(struct header *hp, FILE *fi, int dosign);
static int savemail(char *name, FILE *fi);
static int sendmail_internal(void *v, int recipient_record);
static enum okay transfer(struct name *to, struct name *mailargs, FILE *input,
struct header *hp);
static enum okay start_mta(struct name *to, struct name *mailargs, FILE *input,
struct header *hp);
static void message_id(FILE *fo, struct header *hp);
static int fmt(char *str, struct name *np, FILE *fo, int comma,
int dropinvalid, int domime);
static int infix_resend(FILE *fi, FILE *fo, struct message *mp,
struct name *to, int add_resent);
/*
* Generate a boundary for MIME multipart messages.
*/
char *
makeboundary(void)
{
static char bound[70];
time_t now;
time(&now);
snprintf(bound, sizeof bound, "=_%lx.%s", (long)now, getrandstring(48));
send_boundary = bound;
return send_boundary;
}
/*
* Get an encoding flag based on the given string.
*/
static char *
getencoding(enum conversion convert)
{
switch (convert) {
case CONV_7BIT:
return "7bit";
case CONV_8BIT:
return "8bit";
case CONV_TOQP:
return "quoted-printable";
case CONV_TOB64:
return "base64";
default:
break;
}
/*NOTREACHED*/
return NULL;
}
/*
* Fix the header by glopping all of the expanded names from
* the distribution list into the appropriate fields.
*/
static struct name *
fixhead(struct header *hp, struct name *tolist)
{
struct name *np;
hp->h_to = NULL;
hp->h_cc = NULL;
hp->h_bcc = NULL;
for (np = tolist; np != NULL; np = np->n_flink)
if ((np->n_type & GMASK) == GTO)
hp->h_to =
cat(hp->h_to, nalloc(np->n_fullname,
np->n_type|GFULL));
else if ((np->n_type & GMASK) == GCC)
hp->h_cc =
cat(hp->h_cc, nalloc(np->n_fullname,
np->n_type|GFULL));
else if ((np->n_type & GMASK) == GBCC)
hp->h_bcc =
cat(hp->h_bcc, nalloc(np->n_fullname,
np->n_type|GFULL));
return tolist;
}
/*
* Do not change, you get incorrect base64 encodings else!
*/
#define INFIX_BUF 972
/*
* Put the signature file at fo.
*/
static int
put_signature(FILE *fo, int convert)
{
char *sig, buf[INFIX_BUF], c = '\n';
FILE *fsig;
size_t sz;
sig = value("signature");
if (sig == NULL || *sig == '\0')
return 0;
else
sig = expand(sig);
if ((fsig = Fopen(sig, "r")) == NULL) {
perror(sig);
return -1;
}
while ((sz = fread(buf, sizeof *buf, INFIX_BUF, fsig)) != 0) {
c = buf[sz - 1];
if (mime_write(buf, sz, fo, convert, TD_NONE,
NULL, (size_t)0, NULL, NULL)
== 0) {
perror(sig);
Fclose(fsig);
return -1;
}
}
if (ferror(fsig)) {
perror(sig);
Fclose(fsig);
return -1;
}
Fclose(fsig);
if (c != '\n')
putc('\n', fo);
return 0;
}
/*
* Write an attachment to the file buffer, converting to MIME.
*/
static int
attach_file1(struct attachment *ap, FILE *fo, int dosign)
{
FILE *fi;
char *charset = NULL, *contenttype = NULL, *basename;
enum conversion convert = CONV_TOB64;
int err = 0;
enum mimeclean isclean;
size_t sz;
char *buf;
size_t bufsize, count;
int lastc = EOF;
#ifdef HAVE_ICONV
char *tcs;
#endif
if ((fi = Fopen(ap->a_name, "r")) == NULL) {
perror(ap->a_name);
return -1;
}
if ((basename = strrchr(ap->a_name, '/')) == NULL)
basename = ap->a_name;
else
basename++;
if (ap->a_content_type)
contenttype = ap->a_content_type;
else
contenttype = mime_filecontent(basename);
if (ap->a_charset)
charset = ap->a_charset;
convert = get_mime_convert(fi, &contenttype, &charset, &isclean,
dosign);
fprintf(fo,
"\n--%s\n"
"Content-Type: %s",
send_boundary, contenttype);
if (charset == NULL)
putc('\n', fo);
else
fprintf(fo, ";\n charset=%s\n", charset);
if (ap->a_content_disposition == NULL)
ap->a_content_disposition = "attachment";
fprintf(fo, "Content-Transfer-Encoding: %s\n"
"Content-Disposition: %s;\n"
" filename=\"",
getencoding(convert),
ap->a_content_disposition);
mime_write(basename, strlen(basename), fo,
CONV_TOHDR, TD_NONE, NULL, (size_t)0, NULL, NULL);
fwrite("\"\n", sizeof (char), 2, fo);
if (ap->a_content_id)
fprintf(fo, "Content-ID: %s\n", ap->a_content_id);
if (ap->a_content_description)
fprintf(fo, "Content-Description: %s\n",
ap->a_content_description);
putc('\n', fo);
#ifdef HAVE_ICONV
if (iconvd != (iconv_t)-1) {
iconv_close(iconvd);
iconvd = (iconv_t)-1;
}
tcs = gettcharset();
if ((isclean & (MIME_HASNUL|MIME_CTRLCHAR)) == 0 &&
ascncasecmp(contenttype, "text/", 5) == 0 &&
isclean & MIME_HIGHBIT &&
charset != NULL) {
if ((iconvd = iconv_open_ft(charset, tcs)) == (iconv_t)-1 &&
errno != 0) {
if (errno == EINVAL)
fprintf(stderr, catgets(catd, CATSET, 179,
"Cannot convert from %s to %s\n"), tcs, charset);
else
perror("iconv_open");
Fclose(fi);
return -1;
}
}
#endif /* HAVE_ICONV */
buf = smalloc(bufsize = INFIX_BUF);
if (convert == CONV_TOQP
#ifdef HAVE_ICONV
|| iconvd != (iconv_t)-1
#endif
) {
fflush(fi);
count = fsize(fi);
}
for (;;) {
if (convert == CONV_TOQP
#ifdef HAVE_ICONV
|| iconvd != (iconv_t)-1
#endif
) {
if (fgetline(&buf, &bufsize, &count, &sz, fi, 0)
== NULL)
break;
} else {
if ((sz = fread(buf, sizeof *buf, bufsize, fi)) == 0)
break;
}
lastc = buf[sz-1];
if (mime_write(buf, sz, fo, convert, TD_ICONV,
NULL, (size_t)0, NULL, NULL) == 0)
err = -1;
}
if (convert == CONV_TOQP && lastc != '\n')
fwrite("=\n", 1, 2, fo);
if (ferror(fi))
err = -1;
Fclose(fi);
free(buf);
return err;
}
/*
* Try out different character set conversions to attach a file.
*/
static int
attach_file(struct attachment *ap, FILE *fo, int dosign)
{
char *_wantcharset, *charsets, *ncs;
size_t offs = ftell(fo);
if (ap->a_charset || (charsets = value("sendcharsets")) == NULL)
return attach_file1(ap, fo, dosign);
_wantcharset = wantcharset;
wantcharset = savestr(charsets);
loop: if ((ncs = strchr(wantcharset, ',')) != NULL)
*ncs++ = '\0';
try: if (attach_file1(ap, fo, dosign) != 0) {
if (errno == EILSEQ || errno == EINVAL) {
if (ncs && *ncs) {
wantcharset = ncs;
clearerr(fo);
fseek(fo, offs, SEEK_SET);
goto loop;
}
if (wantcharset) {
if (wantcharset == (char *)-1)
wantcharset = NULL;
else {
wantcharset = (char *)-1;
clearerr(fo);
fseek(fo, offs, SEEK_SET);
goto try;
}
}
}
}
wantcharset = _wantcharset;
return 0;
}
/*
* Attach a message to the file buffer.
*/
static int
attach_message(struct attachment *ap, FILE *fo, int dosign)
{
struct message *mp;
fprintf(fo, "\n--%s\n"
"Content-Type: message/rfc822\n"
"Content-Disposition: inline\n\n", send_boundary);
mp = &message[ap->a_msgno - 1];
touch(mp);
if (send(mp, fo, 0, NULL, SEND_RFC822, NULL) < 0)
return -1;
return 0;
}
/*
* Generate the body of a MIME multipart message.
*/
static int
make_multipart(struct header *hp, int convert, FILE *fi, FILE *fo,
const char *contenttype, const char *charset, int dosign)
{
struct attachment *att;
fputs("This is a multi-part message in MIME format.\n", fo);
if (fsize(fi) != 0) {
char *buf, c = '\n';
size_t sz, bufsize, count;
fprintf(fo, "\n--%s\n", send_boundary);
fprintf(fo, "Content-Type: %s", contenttype);
if (charset)
fprintf(fo, "; charset=%s", charset);
fprintf(fo, "\nContent-Transfer-Encoding: %s\n"
"Content-Disposition: inline\n\n",
getencoding(convert));
buf = smalloc(bufsize = INFIX_BUF);
if (convert == CONV_TOQP
#ifdef HAVE_ICONV
|| iconvd != (iconv_t)-1
#endif /* HAVE_ICONV */
) {
fflush(fi);
count = fsize(fi);
}
for (;;) {
if (convert == CONV_TOQP
#ifdef HAVE_ICONV
|| iconvd != (iconv_t)-1
#endif /* HAVE_ICONV */
) {
if (fgetline(&buf, &bufsize, &count, &sz, fi, 0)
== NULL)
break;
} else {
sz = fread(buf, sizeof *buf, bufsize, fi);
if (sz == 0)
break;
}
c = buf[sz - 1];
if (mime_write(buf, sz, fo, convert,
TD_ICONV, NULL, (size_t)0,
NULL, NULL) == 0) {
free(buf);
return -1;
}
}
free(buf);
if (ferror(fi))
return -1;
if (c != '\n')
putc('\n', fo);
if (charset != NULL)
put_signature(fo, convert);
}
for (att = hp->h_attach; att != NULL; att = att->a_flink) {
if (att->a_msgno) {
if (attach_message(att, fo, dosign) != 0)
return -1;
} else {
if (attach_file(att, fo, dosign) != 0)
return -1;
}
}
/* the final boundary with two attached dashes */
fprintf(fo, "\n--%s--\n", send_boundary);
return 0;
}
/*
* Prepend a header in front of the collected stuff
* and return the new file.
*/
static FILE *
infix(struct header *hp, FILE *fi, int dosign)
{
FILE *nfo, *nfi;
char *tempMail;
#ifdef HAVE_ICONV
char *tcs, *convhdr = NULL;
#endif
enum mimeclean isclean;
enum conversion convert;
char *charset = NULL, *contenttype = NULL;
int lastc = EOF;
if ((nfo = Ftemp(&tempMail, "Rs", "w", 0600, 1)) == NULL) {
perror(catgets(catd, CATSET, 178, "temporary mail file"));
return(NULL);
}
if ((nfi = Fopen(tempMail, "r")) == NULL) {
perror(tempMail);
Fclose(nfo);
return(NULL);
}
rm(tempMail);
Ftfree(&tempMail);
convert = get_mime_convert(fi, &contenttype, &charset,
&isclean, dosign);
#ifdef HAVE_ICONV
tcs = gettcharset();
if ((convhdr = need_hdrconv(hp, GTO|GSUBJECT|GCC|GBCC|GIDENT)) != 0) {
if (iconvd != (iconv_t)-1)
iconv_close(iconvd);
if ((iconvd = iconv_open_ft(convhdr, tcs)) == (iconv_t)-1
&& errno != 0) {
if (errno == EINVAL)
fprintf(stderr, catgets(catd, CATSET, 179,
"Cannot convert from %s to %s\n"), tcs, convhdr);
else
perror("iconv_open");
Fclose(nfo);
return NULL;
}
}
#endif /* HAVE_ICONV */
if (puthead(hp, nfo,
GTO|GSUBJECT|GCC|GBCC|GNL|GCOMMA|GUA|GMIME
|GMSGID|GIDENT|GREF|GDATE,
SEND_MBOX, convert, contenttype, charset)) {
Fclose(nfo);
Fclose(nfi);
#ifdef HAVE_ICONV
if (iconvd != (iconv_t)-1) {
iconv_close(iconvd);
iconvd = (iconv_t)-1;
}
#endif
return NULL;
}
#ifdef HAVE_ICONV
if (convhdr && iconvd != (iconv_t)-1) {
iconv_close(iconvd);
iconvd = (iconv_t)-1;
}
if ((isclean & (MIME_HASNUL|MIME_CTRLCHAR)) == 0 &&
ascncasecmp(contenttype, "text/", 5) == 0 &&
isclean & MIME_HIGHBIT &&
charset != NULL) {
if (iconvd != (iconv_t)-1)
iconv_close(iconvd);
if ((iconvd = iconv_open_ft(charset, tcs)) == (iconv_t)-1
&& errno != 0) {
if (errno == EINVAL)
fprintf(stderr, catgets(catd, CATSET, 179,
"Cannot convert from %s to %s\n"), tcs, charset);
else
perror("iconv_open");
Fclose(nfo);
return NULL;
}
}
#endif
if (hp->h_attach != NULL) {
if (make_multipart(hp, convert, fi, nfo,
contenttype, charset, dosign) != 0) {
Fclose(nfo);
Fclose(nfi);
#ifdef HAVE_ICONV
if (iconvd != (iconv_t)-1) {
iconv_close(iconvd);
iconvd = (iconv_t)-1;
}
#endif
return NULL;
}
} else {
size_t sz, bufsize, count;
char *buf;
if (convert == CONV_TOQP
#ifdef HAVE_ICONV
|| iconvd != (iconv_t)-1
#endif /* HAVE_ICONV */
) {
fflush(fi);
count = fsize(fi);
}
buf = smalloc(bufsize = INFIX_BUF);
for (;;) {
if (convert == CONV_TOQP
#ifdef HAVE_ICONV
|| iconvd != (iconv_t)-1
#endif /* HAVE_ICONV */
) {
if (fgetline(&buf, &bufsize, &count, &sz, fi, 0)
== NULL)
break;
} else {
sz = fread(buf, sizeof *buf, bufsize, fi);
if (sz == 0)
break;
}
lastc = buf[sz - 1];
if (mime_write(buf, sz, nfo, convert,
TD_ICONV, NULL, (size_t)0,
NULL, NULL) == 0) {
Fclose(nfo);
Fclose(nfi);
#ifdef HAVE_ICONV
if (iconvd != (iconv_t)-1) {
iconv_close(iconvd);
iconvd = (iconv_t)-1;
}
#endif
free(buf);
return NULL;
}
}
if (convert == CONV_TOQP && lastc != '\n')
fwrite("=\n", 1, 2, nfo);
free(buf);
if (ferror(fi)) {
Fclose(nfo);
Fclose(nfi);
#ifdef HAVE_ICONV
if (iconvd != (iconv_t)-1) {
iconv_close(iconvd);
iconvd = (iconv_t)-1;
}
#endif
return NULL;
}
if (charset != NULL)
put_signature(nfo, convert);
}
#ifdef HAVE_ICONV
if (iconvd != (iconv_t)-1) {
iconv_close(iconvd);
iconvd = (iconv_t)-1;
}
#endif
fflush(nfo);
if (ferror(nfo)) {
perror(catgets(catd, CATSET, 180, "temporary mail file"));
Fclose(nfo);
Fclose(nfi);
return NULL;
}
Fclose(nfo);
Fclose(fi);
fflush(nfi);
rewind(nfi);
return(nfi);
}
/*
* Save the outgoing mail on the passed file.
*/
/*ARGSUSED*/
static int
savemail(char *name, FILE *fi)
{
FILE *fo;
char *buf;
size_t bufsize, buflen, count;
char *p;
time_t now;
int prependnl = 0;
int error = 0;
buf = smalloc(bufsize = LINESIZE);
time(&now);
if ((fo = Zopen(name, "a+", NULL)) == NULL) {
if ((fo = Zopen(name, "wx", NULL)) == NULL) {
perror(name);
free(buf);
return (-1);
}
} else {
if (fseek(fo, -2L, SEEK_END) == 0) {
switch (fread(buf, sizeof *buf, 2, fo)) {
case 2:
if (buf[1] != '\n') {
prependnl = 1;
break;
}
/*FALLTHRU*/
case 1:
if (buf[0] != '\n')
prependnl = 1;
break;
default:
if (ferror(fo)) {
perror(name);
free(buf);
return -1;
}
}
fflush(fo);
if (prependnl) {
putc('\n', fo);
fflush(fo);
}
}
}
fprintf(fo, "From %s %s", myname, ctime(&now));
buflen = 0;
fflush(fi);
rewind(fi);
count = fsize(fi);
while (fgetline(&buf, &bufsize, &count, &buflen, fi, 0) != NULL) {
if (*buf == '>') {
p = buf + 1;
while (*p == '>')
p++;
if (strncmp(p, "From ", 5) == 0)
/* we got a masked From line */
putc('>', fo);
} else if (strncmp(buf, "From ", 5) == 0)
putc('>', fo);
fwrite(buf, sizeof *buf, buflen, fo);
}
if (buflen && *(buf + buflen - 1) != '\n')
putc('\n', fo);
putc('\n', fo);
fflush(fo);
if (ferror(fo)) {
perror(name);
error = -1;
}
if (Fclose(fo) != 0)
error = -1;
fflush(fi);
rewind(fi);
/*
* OpenBSD 3.2 and NetBSD 1.5.2 were reported not to
* reset the kernel file offset after the calls above,
* a clear violation of IEEE Std 1003.1, 1996, 8.2.3.7.
* So do it 'manually'.
*/
lseek(fileno(fi), 0, SEEK_SET);
free(buf);
return error;
}
/*
* Interface between the argument list and the mail1 routine
* which does all the dirty work.
*/
int
mail(struct name *to, struct name *cc, struct name *bcc,
struct name *smopts, char *subject, struct attachment *attach,
char *quotefile, int recipient_record, int tflag, int Eflag)
{
struct header head;
struct str in, out;
memset(&head, 0, sizeof head);
/* The given subject may be in RFC1522 format. */
if (subject != NULL) {
in.s = subject;
in.l = strlen(subject);
mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
head.h_subject = out.s;
}
if (tflag == 0) {
head.h_to = to;
head.h_cc = cc;
head.h_bcc = bcc;
}
head.h_attach = attach;
head.h_smopts = smopts;
mail1(&head, 0, NULL, quotefile, recipient_record, 0, tflag, Eflag);
if (subject != NULL)
free(out.s);
return(0);
}
/*
* Send mail to a bunch of user names. The interface is through
* the mail routine below.
*/
static int
sendmail_internal(void *v, int recipient_record)
{
int Eflag;
char *str = v;
struct header head;
memset(&head, 0, sizeof head);
head.h_to = extract(str, GTO|GFULL);
Eflag = value("skipemptybody") != NULL;
mail1(&head, 0, NULL, NULL, recipient_record, 0, 0, Eflag);
return(0);
}
int
sendmail(void *v)
{
return sendmail_internal(v, 0);
}
int
Sendmail(void *v)
{
return sendmail_internal(v, 1);
}
static enum okay
transfer(struct name *to, struct name *mailargs, FILE *input, struct header *hp)
{
char o[LINESIZE], *cp;
struct name *np, *nt;
int cnt = 0;
FILE *ef;
enum okay ok = OKAY;
np = to;
while (np) {
snprintf(o, sizeof o, "smime-encrypt-%s", np->n_name);
if ((cp = value(o)) != NULL) {
if ((ef = smime_encrypt(input, cp, np->n_name)) != 0) {
nt = nalloc(np->n_name,
np->n_type & ~(GFULL|GSKIN));
if (start_mta(nt, mailargs, ef, hp) != OKAY)
ok = STOP;
Fclose(ef);
} else {
fprintf(stderr, "Message not sent to <%s>\n",
np->n_name);
senderr++;
}
rewind(input);
if (np->n_flink)
np->n_flink->n_blink = np->n_blink;
if (np->n_blink)
np->n_blink->n_flink = np->n_flink;
if (np == to)
to = np->n_flink;
np = np->n_flink;
} else {
cnt++;
np = np->n_flink;
}
}
if (cnt) {
if (value("smime-force-encryption") ||
start_mta(to, mailargs, input, hp) != OKAY)
ok = STOP;
}
return ok;
}
/*
* Start the Mail Transfer Agent
* mailing to namelist and stdin redirected to input.
*/
static enum okay
start_mta(struct name *to, struct name *mailargs, FILE *input,
struct header *hp)
{
char **args = NULL, **t;
pid_t pid;
sigset_t nset;
char *cp, *smtp;
char *user = NULL, *password = NULL, *skinned = NULL;
enum okay ok = STOP;
#ifdef HAVE_SOCKETS
struct termios otio;
int reset_tio;
#endif /* HAVE_SOCKETS */
if ((smtp = value("smtp")) == NULL) {
args = unpack(mailargs, to);
if (debug || value("debug")) {
printf(catgets(catd, CATSET, 181,
"Sendmail arguments:"));
for (t = args; *t != NULL; t++)
printf(" \"%s\"", *t);
printf("\n");
return OKAY;
}
}
#ifdef HAVE_SOCKETS
if (smtp != NULL) {
skinned = skin(myorigin(hp));
if ((user = smtp_auth_var("-user", skinned)) != NULL &&
(password = smtp_auth_var("-password",
skinned)) == NULL)
password = getpassword(&otio, &reset_tio, NULL);
}
#endif /* HAVE_SOCKETS */
/*
* Fork, set up the temporary mail file as standard
* input for "mail", and exec with the user list we generated
* far above.
*/
if ((pid = fork()) == -1) {
perror("fork");
savedeadletter(input);
senderr++;
return STOP;
}
if (pid == 0) {
sigemptyset(&nset);
sigaddset(&nset, SIGHUP);
sigaddset(&nset, SIGINT);
sigaddset(&nset, SIGQUIT);
sigaddset(&nset, SIGTSTP);
sigaddset(&nset, SIGTTIN);
sigaddset(&nset, SIGTTOU);
freopen("/dev/null", "r", stdin);
if (smtp != NULL) {
prepare_child(&nset, 0, 1);
if (smtp_mta(smtp, to, input, hp,
user, password, skinned) == 0)
_exit(0);
} else {
prepare_child(&nset, fileno(input), -1);
if ((cp = value("sendmail")) != NULL)
cp = expand(cp);
else
cp = SENDMAIL;
execv(cp, args);
perror(cp);
}
savedeadletter(input);
fputs(catgets(catd, CATSET, 182,
". . . message not sent.\n"), stderr);
_exit(1);
}
if (value("verbose") != NULL || value("sendwait") || debug
|| value("debug")) {
if (wait_child(pid) == 0)
ok = OKAY;
else
senderr++;
} else {
ok = OKAY;
free_child(pid);
}
return ok;
}
/*
* Record outgoing mail if instructed to do so.
*/
static enum okay
mightrecord(FILE *fp, struct name *to, int recipient_record)
{
char *cp, *cq, *ep;
if (recipient_record) {
cq = skin(to->n_name);
cp = salloc(strlen(cq) + 1);
strcpy(cp, cq);
for (cq = cp; *cq && *cq != '@'; cq++);
*cq = '\0';
} else
cp = value("record");
if (cp != NULL) {
ep = expand(cp);
if (value("outfolder") && *ep != '/' && *ep != '+' &&
which_protocol(ep) == PROTO_FILE) {
cq = salloc(strlen(cp) + 2);
cq[0] = '+';
strcpy(&cq[1], cp);
cp = cq;
ep = expand(cp);
}
if (savemail(ep, fp) != 0) {
fprintf(stderr,
"Error while saving message to %s - "
"message not sent\n", ep);
rewind(fp);
exit_status |= 1;
savedeadletter(fp);
return STOP;
}
}
return OKAY;
}
/*
* Mail a message on standard input to the people indicated
* in the passed header. (Internal interface).
*/
enum okay
mail1(struct header *hp, int printheaders, struct message *quote,
char *quotefile, int recipient_record, int doprefix, int tflag,
int Eflag)
{
struct name *to;
FILE *mtf, *nmtf;
enum okay ok = STOP;
int dosign = -1;
char *charsets, *ncs = NULL, *cp;
#ifdef notdef
if ((hp->h_to = checkaddrs(hp->h_to)) == NULL) {
senderr++;
return STOP;
}
#endif
if ((cp = value("autocc")) != NULL && *cp)
hp->h_cc = cat(hp->h_cc, checkaddrs(sextract(cp, GCC|GFULL)));
if ((cp = value("autobcc")) != NULL && *cp)
hp->h_bcc = cat(hp->h_bcc,
checkaddrs(sextract(cp, GBCC|GFULL)));
/*
* Collect user's mail from standard input.
* Get the result as mtf.
*/
if ((mtf = collect(hp, printheaders, quote, quotefile, doprefix,
tflag)) == NULL)
return STOP;
if (value("interactive") != NULL) {
if (((value("bsdcompat") || value("askatend"))
&& (value("askcc") != NULL ||
value("askbcc") != NULL)) ||
value("askattach") != NULL ||
value("asksign") != NULL) {
if (value("askcc") != NULL)
grabh(hp, GCC, 1);
if (value("askbcc") != NULL)
grabh(hp, GBCC, 1);
if (value("askattach") != NULL)
hp->h_attach = edit_attachments(hp->h_attach);
if (value("asksign") != NULL)
dosign = yorn("Sign this message (y/n)? ");
} else {
printf(catgets(catd, CATSET, 183, "EOT\n"));
fflush(stdout);
}
}
if (fsize(mtf) == 0) {