-
Notifications
You must be signed in to change notification settings - Fork 68
/
smtp.c
executable file
·1908 lines (1671 loc) · 44.5 KB
/
smtp.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
/*
** SMTP routines for mailsend - a simple mail sender via SMTP
**
** Limitations and Comments:
** SMTP RF[C] RFC-821
** Also look at: TCP/IP Illustraged Vol 1 by Richard Stevens
**
** Written mainly for NT, Unix has this kind of tools in hundreds.
**
** Development History:
** who when why
** [email protected] Mar-26-2001 first cut
*/
#include "mailsend.h"
static char buf[BUFSIZ];
static int bufsz = sizeof(buf) - 1;
static int break_out=0;
static char smtp_line[BUFSIZ];
static char smtp_errbuf[BUFSIZ];
static int smtp_code;
static int smtp_sep;
static int s_esmtp=0;
#ifdef WINNT
static BOOL
WINAPI CntrlHandler(DWORD CtrlEvent);
#else
#define closesocket close
#endif /* WINNT */
void smtpDisconnect(void)
{
msock_close();
}
/* connect to SMTP server and returns the socket fd */
static SOCKET smtpConnect(char *smtp_server,int port)
{
SOCKET
sfd;
if (g_use_protocol == MSOCK_USE_IPV4)
{
showVerbose("Forcing to use IPv4 address of SMTP server\n");
}
else if (g_use_protocol == MSOCK_USE_IPV6)
{
showVerbose("Forcing to use IPv6 address of SMTP server\n");
}
else
{
showVerbose("Will detect IPv4 or IPv6 automatically\n");
}
sfd=clientSocket(g_use_protocol, smtp_server,port, g_connect_timeout);
if (sfd == INVALID_SOCKET)
{
errorMsg("Could not connect to SMTP server \"%s\" at port %d",
smtp_server,port);
return (INVALID_SOCKET);
}
/* set the socket to msock lib's static place, not thread safe*/
msock_set_socket(sfd);
return (sfd);
}
int read_smtp_multi_lines(void)
{
int
rc,
lcnt = 0;
if (smtp_sep == A_DASH)
{
for (;;)
{
rc=read_smtp_line();
if (rc < 0)
break;
lcnt++;
if (lcnt >= 100)
{
errorMsg("Too many lines from server\n");
rc=(-1);
goto ExitProcessing;
}
if (smtp_sep != A_DASH)
break;
}
}
smtp_sep = A_SPACE;
return(0);
ExitProcessing:
return(1);
}
/*
* sfd socket
*
* return 0 on success, -1 on failure
*
* populates globals: smtp_code,smtp_line and smtp_errbuf on error
*
*/
int read_smtp_line(void)
{
int
rc=(-1),
n;
char
tbuf[BUFSIZ],
lbuf[BUFSIZ];
memset(smtp_line,0,sizeof(smtp_line));
memset(smtp_errbuf,0,sizeof(smtp_errbuf));
smtp_code=(-1);
memset(lbuf,0,sizeof(lbuf));
/* read a line */
n=msock_gets(lbuf,sizeof(lbuf)-1, g_read_timeout);
if (n < 3 )
{
/*errorMsg("Error reading SMTP line, read %d bytes",n);*/
return(-1);
}
showVerbose("[S] %s\n",lbuf);
if (n >= 5)
{
memset(tbuf,0,sizeof(tbuf));
memcpy(tbuf,lbuf,3);
smtp_code=atoi(tbuf);
smtp_sep=lbuf[3];
(void) snprintf(smtp_line,sizeof(smtp_line)-1,"%s",lbuf + 4);
rc=0;
/*
(void) fprintf(stderr," Line: \"%s\"\n",lbuf);
(void) fprintf(stderr," Code: \"%d\"\n",smtp_code);
(void) fprintf(stderr," Sep: '%c'\n",smtp_sep);
(void) fprintf(stderr," line: \"%s\"\n",smtp_line);
*/
}
else
{
(void) snprintf(smtp_errbuf,sizeof(smtp_errbuf)-1,"%s",lbuf);
}
return(rc);
}
int smtp_start_tls(int sfd)
{
int
rc=(-1);
#ifdef HAVE_OPENSSL
SSL
*ssl=NULL;
#endif /* HAVE_OPENSSL */
memset(buf,0,sizeof(buf));
(void) snprintf(buf,sizeof(buf)-1,"%s\r\n","STARTTLS");
showVerbose("[C] %s",buf);
msock_puts(buf);
rc=read_smtp_line();
if (smtp_code != 220)
{
errorMsg("Unknown STARTTLS response code %d\n",smtp_code);
return(-1);
}
#ifdef HAVE_OPENSSL
ssl=msock_get_ssl();
if (ssl)
{
if (!SSL_set_fd(ssl,sfd))
{
errorMsg("failed to set socket to SSL\n");
return(-1);
}
/* must set back to msock's static */
msock_set_ssl(ssl);
rc=SSL_connect(ssl);
if (rc < 1)
{
errorMsg("SSL connection failed\n");
ERR_print_errors_fp(stderr);
return(-1);
}
print_cert_info(ssl);
/* tell msock everything is ssl after that */
msock_turn_ssl_on();
rc=0;
}
else
{
errorMsg("Could not start STARTTLS, SSL not initialized properly");
rc=(-1);
}
#else
errorMsg("Not Compiled with OpenSSL, will not try STARTTLS");
rc=(-1);
#endif /*HAVE_OPENSSL */
return(rc);
}
/* SMTP: HELO */
static int say_helo(char *helo_domain)
{
int
cnt=0,
rc;
(void) snprintf(buf,sizeof(buf)-1,"%s %s\r\n",
s_esmtp ? "EHLO" : "HELO",helo_domain);
showVerbose("[C] %s",buf);
/* send */
msock_puts(buf);
rc=read_smtp_line();
if (smtp_code != 250)
{
errorMsg("%s failed", s_esmtp ? "EHLO" : "HELO");
return(-1);
}
/* read all the capabilities if separator is - */
if (smtp_sep == A_DASH)
{
for (;;)
{
rc=read_smtp_line();
if (rc == 0)
add_server_cap_to_list(smtp_line);
cnt++;
if (cnt >= 1000)
break;
if (rc < 0 || smtp_sep != A_DASH)
break;
}
}
smtp_sep = A_SPACE;
return(rc);
}
/* SMTP: MAIL FROM */
static int smtp_MAIL_FROM(char *from)
{
memset(buf,0,sizeof(buf));
(void) snprintf(buf,sizeof(buf)-1,"MAIL FROM: <%s>\r\n",from);
showVerbose("[C] %s",buf);
msock_puts(buf);
read_smtp_line();
if (smtp_code != 250)
{
errorMsg("MAIL FROM failed: '%d %s'",smtp_code,smtp_line);
return(-1);
}
return(0);
}
/* SMTP: quit */
static int smtp_QUIT(void)
{
showVerbose("[C] QUIT\r\n");
msock_puts("QUIT\r\n");
read_smtp_line();
/*
** google does not seem to write anything back in response to QUIT
** command. I'll ignore it anyway
*/
return(0);
}
/* SMTP: RSET */
/* aborts current mail transaction and cause both ends to reset */
static int smtp_RSET()
{
msock_puts("RSET\r\n");
return(read_smtp_line());
}
/* SMTP: RCPT TO */
static int smtp_RCPT_TO(void)
{
Sll
*l,
*al;
Address
*a;
char
*x;
int
rc;
al=getAddressList();
for (l=al; l; l=l->next)
{
a=(Address *) l->data;
if (! a)
return(-1);
if (! a->address)
return(-1);
memset(buf,0,sizeof(buf));
x=getenv("NOTIFY_RCPT");
if (x != NULL)
{
/* MS Exchange has it */
showVerbose("NOTIFY_RCPT=%s\n",x);
(void) snprintf(buf,sizeof(buf)-1,"RCPT TO: %s %s\r\n",
a->address,x);
}
else
{
(void) snprintf(buf,sizeof(buf)-1,"RCPT TO: <%s>\r\n",a->address);
}
showVerbose("[C] %s",buf);
msock_puts(buf);
rc=read_smtp_line();
if (rc == 0)
{
if (smtp_code != 250)
{
errorMsg("RCPT TO: <%s> failed '%d:%s'\n",
a->address,smtp_code,smtp_line);
smtp_RSET();
return(-1);
}
}
}
return (0);
}
/* SMTP: DATA */
static int smtp_DATA(void)
{
int
rc;
msock_puts("DATA\r\n");
showVerbose("[C] DATA\r\n");
rc=read_smtp_line();
if (rc == 0)
{
if (smtp_code != 354)
{
errorMsg("DATA failed: '%d %s'\n",smtp_code,smtp_line);
return(-1);
}
}
return(0);
}
/* SMTP: EOM */
/* return 0 on success, -1 on failure */
int smtpEom(int sfd)
{
int
rc;
msock_puts("\r\n.\r\n");
showVerbose("\r\n[C] .\r\n");
/*
** Bug# 1
** we want to see smtp code 250 now
** if mail is too big, it can mail with 552 message too large
*/
rc = read_smtp_line();
if (smtp_code != 250)
{
read_smtp_multi_lines();
errorMsg("Expected smtp code 250, got %d\n",smtp_code);
rc = (-1);
}
return(rc);
}
void doCleanup(void)
{
smtpDisconnect();
}
#ifdef WINNT
/*
** Handle Ctrl+C
*/
static BOOL WINAPI CntrlHandler(DWORD CtrlEvent)
{
break_out=0;
switch (CtrlEvent)
{
case CTRL_C_EVENT:
{
break_out=1;
(void) fprintf(stderr,"\nNot sending mail. Exiting.......\n");
exit_error();
break;
}
}
return (TRUE);
}
#endif /* WINNT */
int write_to_socket(char *str)
{
int
n;
if (str == NULL)
{
return (-1);
}
n = msock_puts(str);
/*showVerbose("%d bytes: %s\n",n, str);*/
return(n);
}
/*
** return 0 on success
*/
int encode2base64andwrite2socket(const char *str)
{
FILE
*tfp1 = NULL,
*tfp2 = NULL;
char
mbuf[1000],
oneline_tempfile1[MUTILS_PATH_MAX],
oneline_tempfile2[MUTILS_PATH_MAX];
memset(oneline_tempfile1, 0, sizeof(oneline_tempfile1));
/* write the text to a tmp file */
tfp1 = mutils_get_tempfileFP(oneline_tempfile1,
sizeof(oneline_tempfile1)-1);
if (tfp1 == NULL)
{
errorMsg("%s (%d) - Could not open temp file1 for writing (%s)",
MFL,
ERR_STR);
return (-1);
}
(void) fprintf(tfp1,"%s",str);
(void) fclose(tfp1);
/* open another tmp file to write the base64 of the first tmp file to */
memset(oneline_tempfile2, 0, sizeof(oneline_tempfile2));
tfp2 = mutils_get_tempfileFP(oneline_tempfile2,
sizeof(oneline_tempfile2)-1);
showVerbose("Oneline temp file2: * %s\n",oneline_tempfile2);
if (tfp2 == NULL)
{
errorMsg("%s (%d) - Could not open temp file2 for writing (%s)",
MFL,
ERR_STR);
return (-1);
}
tfp1 = fopen(oneline_tempfile1,"rb");
if (tfp1 == NULL)
{
errorMsg("%s (%d) - Could not open temp file for reading (%s)",
MFL,
ERR_STR);
return(-1);
}
mutilsBase64Encode(tfp1,tfp2);
(void) fclose(tfp1);
(void) fclose(tfp2);
/* open the file with base64 and write the content to socket */
tfp2 = fopen(oneline_tempfile2,"r");
if (tfp2 == NULL)
{
errorMsg("%s (%d) - Could not open temp file for reading (%s)",
MFL,
ERR_STR);
return(-1);
}
while(fgets(mbuf, sizeof(mbuf)-1, tfp2))
{
write_to_socket(mbuf);
if (g_show_attachment_in_log)
{
showVerbose("[C] %s",mbuf);
}
}
(void) fclose(tfp2);
unlink(oneline_tempfile1);
unlink(oneline_tempfile2);
return(0);
}
/*
** send one line messages, each one is an inline attachment
** return 0 if mail is sent, -1 otherwise
*/
int process_oneline_messages(const char *boundary)
{
Attachment
*a = NULL;
Sll
*l,
*oneline_attachment_list;
oneline_attachment_list = get_oneline_attachment_list();
if (oneline_attachment_list == NULL)
{
return(0);
}
print_oneline_attachment_list();
for (l = oneline_attachment_list; l; l = l->next)
{
a = (Attachment *) l->data;
(void) snprintf(buf, bufsz, "\r\n--%s\r\n",boundary);
write_to_socket(buf);
if (strcmp(a->charset,"none") != 0)
{
(void) snprintf(buf, bufsz, "Content-Type: %s; charset=%s\r\n",
a->mime_type,
a->charset);
}
else
{
(void) snprintf(buf, bufsz, "Content-Type: %s\r\n",a->mime_type);
}
write_to_socket(buf);
(void) strcpy(buf,"Content-Disposition: inline\r\n");
write_to_socket(buf);
/* add encoding type if needed */
if (strncmp(a->content_transfer_encoding,"none",4) != 0)
{
(void) snprintf(buf, bufsz, "Content-Transfer-Encoding: %s\r\n\r\n",
a->content_transfer_encoding);
write_to_socket(buf);
}
if (strncmp(a->content_transfer_encoding,"base64",6) == 0)
{
/* encode the mssage to base 64 and write to socket */
encode2base64andwrite2socket(a->oneline_msg);
}
else
{
/*
** no encoding type, so last \r\n was no added, we must
** add it or some mailer will thing the mail is invalid
*/
if (strncmp(a->content_transfer_encoding,"none",4) == 0)
{
write_to_socket("\r\n");
}
write_to_socket(a->oneline_msg);
if (g_show_attachment_in_log)
{
showVerbose("[C] %s\n",a->oneline_msg);
}
}
write_to_socket("\r\n");
}
return(0);
}
/*
** include the content of the file as body of the message
** No encoding will be done.
*/
int include_msg_body(void)
{
Sll
*l,
*msg_body_attachment_head;
FILE
*fp = NULL;
Attachment
*a;
msg_body_attachment_head = get_msg_body_attachment_list();
if (msg_body_attachment_head == NULL)
{
return(-1);
}
l = msg_body_attachment_head;
a = (Attachment *) l->data;
(void) snprintf(buf,bufsz,"MIME-Version: 1.0\r\n");
write_to_socket(buf);
if (strcmp(a->charset,"none") != 0)
{
(void) snprintf(buf,bufsz,"Content-Type: %s; charset=%s\r\n\r\n",
a->mime_type,
a->charset);
}
else
{
(void) snprintf(buf,bufsz,"Content-Type: %s\r\n\r\n",a->mime_type);
}
write_to_socket(buf);
fp=fopen(a->file_path,"r");
if (fp == (FILE *) NULL)
{
errorMsg("Could not open message body file: %s",a->file_path);
return (-1);
}
while (fgets(buf,bufsz,fp))
{
write_to_socket(buf);
if (g_show_attachment_in_log)
{
showVerbose("[C] %s",buf);
}
}
(void) fclose(fp);
(void) snprintf(buf,bufsz,"\r\n\r\n");
msock_puts(buf);
showVerbose(buf);
return(0);
}
int include_image(void)
{
return(0);
}
static void cleanup_attachment(Attachment *a)
{
if (a == NULL)
return;
if (a->fp_read)
{
(void) fclose(a->fp_read);
}
if (*a->mime_tmpfile != '\0')
{
unlink(a->mime_tmpfile);
}
}
/*
** return the attachment with fp_read member open.
** If encoding type requires some kind of encoding,
** mime_tmpfile will contain the file which holds the
** encoded content. The caller must close the file pointer
** and unlik the tmp_filename
** return NULL on error
*/
static Attachment *get_encoded_attachment(Attachment *a)
{
FILE
*fp_read = NULL,
*tfp_write = NULL;
int
tempfile_opened = 0;
if (strncmp(a->content_transfer_encoding, "base64", 6) == 0)
{
tfp_write = mutils_get_tempfileFP(a->mime_tmpfile,sizeof(a->mime_tmpfile)-1);
if (tfp_write == NULL)
{
errorMsg("%s (%d) - Could not create temp file for MIME (%s)",
MFL,
ERR_STR);
return(NULL);
}
tempfile_opened = 1;
showVerbose("%s (%d) - MIME temp file: %s created successfully, FILE pointer=%x\n",
MFL,
a->mime_tmpfile,
tfp_write);
/* open the file to attach */
fp_read = fopen(a->file_path,"rb");
if (fp_read == (FILE *) NULL)
{
errorMsg("%s (%d) - Could not open file for %s reading (%s)",
MFL,
a->file_path,
ERR_STR);
goto ExitProcessing;
}
showVerbose("%s (%d) - Writing Content to FILE pointer: %x\n",
MFL,
tfp_write);
/* write base64 content to tmp file */
mutilsBase64Encode(fp_read,tfp_write);
(void) fclose(fp_read);
fp_read = NULL;
(void) fclose(tfp_write);
tfp_write = NULL;
a->fp_read = fopen(a->mime_tmpfile, "r");
if (a->fp_read == (FILE *) NULL)
{
errorMsg("%s (%d) - Could not open file for %s reading (%s)",
MFL,
a->file_path,
ERR_STR);
goto ExitProcessing;
}
if (tempfile_opened)
{
unlink(a->mime_tmpfile);
}
return(a);
}
/* no tmp file */
memset(a->mime_tmpfile, 0, sizeof(a->mime_tmpfile));
/* open the attachment. caller must close it */
a->fp_read = fopen(a->file_path, "r");
if (a->fp_read == (FILE *) NULL)
{
errorMsg("%s (%d) - Could not open file for %s reading (%s)",
MFL,
a->file_path,
ERR_STR);
return (NULL);
}
return(a);
ExitProcessing:
if (tempfile_opened)
{
unlink(a->mime_tmpfile);
}
return(NULL);
}
/*
** write MIME headers, encode attachment if needed
** and write to socket
** returns 0 on success -1 on failure
*/
int send_attachment(Attachment *a, const char *boundary)
{
Attachment
*encoded_attachment;
encoded_attachment = get_encoded_attachment(a);
if (encoded_attachment == NULL)
{
errorMsg("%s (%d) - Could not encode attachment %s",
MFL,
a->file_path);
return(-1);
}
(void) snprintf(buf, bufsz,"\r\n--%s\r\n",boundary);
write_to_socket(buf);
if (a->charset && strncmp(a->charset,"none", 4) != 0)
{
(void) snprintf(buf, bufsz, "Content-Type: %s; charset=%s\r\n",
a->mime_type,
a->charset);
}
else
{
if (a->attachment_name)
{
(void) snprintf(buf, bufsz, "Content-Type: %s; name=\"%s\"\r\n",
a->mime_type,
a->attachment_name);
}
else
{
(void) snprintf(buf, bufsz, "Content-Type: %s\r\n",a->mime_type);
}
}
write_to_socket(buf);
if (a->content_id == NULL)
{
if (strncmp(a->content_disposition, "inline", 6) == 0)
{
(void) snprintf(buf, bufsz, "Content-Disposition: %s\r\n",a->content_disposition);
}
else
{
if (a->attachment_name)
{
(void) snprintf(buf, bufsz, "Content-Disposition: %s; filename=\"%s\"\r\n",
a->content_disposition,
a->attachment_name);
}
else if (a->file_name)
{
(void) snprintf(buf, bufsz, "Content-Disposition: %s; filename=\"%s\"\r\n",
a->content_disposition,
a->file_name);
}
else
{
(void) snprintf(buf, bufsz, "Content-Disposition: %s\r\n",a->content_disposition);
}
}
write_to_socket(buf);
}
if (a->content_id)
{
(void) snprintf(buf, bufsz, "Content-ID: <%s>\r\n",a->content_id);
write_to_socket(buf);
(void) snprintf(buf, bufsz, "X-Attachment-Id: %s\r\n",a->content_id);
write_to_socket(buf);
}
if (strncmp(a->content_transfer_encoding,"none",4) != 0)
{
(void) snprintf(buf, bufsz, "Content-Transfer-Encoding: %s\r\n\r\n",
a->content_transfer_encoding);
write_to_socket(buf);
}
else
{
write_to_socket("\r\n");
}
/* our FILE pointer is already open and read to read from */
while (fgets(buf, bufsz, a->fp_read))
{
write_to_socket(buf);
}
/* close file, remove tmp file if needed */
cleanup_attachment(a);
return(0);
}
int process_attachments(const char *boundary)
{
Attachment
*a;
Sll
*attachment_list,
*al;
int
rc;
attachment_list = get_attachment_list();
if (attachment_list == NULL)
{
return(0);
}
for (al=attachment_list; al; al=al->next)
{
a=(Attachment *) al->data;
if (a == NULL)
continue;
rc = send_attachment(a, boundary);
if (rc == -1)
{
errorMsg("%s (%d) - failed to send attachment %s\n",
MFL,
a->file_path);
return(-1);
}
}
(void) snprintf(buf,sizeof(buf)-1,"\r\n--%s--\r\n",boundary);
msock_puts(buf);
showVerbose(buf);
return(0);
}
/*
** Print multipart/mixed or multipart/related header
** if mixed and embedded images are specified, make
** to use multipart/alternative for the embedded images.
*/
int print_content_type_header(const char *boundary)
{
Sll
*oneline_attachment_list,
*attachment_list,
*embed_image_list;
(void) snprintf(buf, bufsz,"MIME-Version: 1.0\r\n");
write_to_socket(buf);
if (*g_content_type!='\0')
{
(void) snprintf(buf,sizeof(buf)-1,"Content-Type: %s; boundary=\"%s\"\r\n",
g_content_type,
boundary);
write_to_socket(buf);
return(0);
}
oneline_attachment_list = get_oneline_attachment_list();
attachment_list = get_attachment_list();
embed_image_list = get_embed_image_attachment_list();
if (oneline_attachment_list || attachment_list)
{
(void) snprintf(buf,sizeof(buf)-1,
"Content-Type: multipart/mixed; boundary=\"%s\"\r\n", boundary);
write_to_socket(buf);
return(0);
}
if (embed_image_list)
{
(void) snprintf(buf,sizeof(buf)-1,
"Content-Type: multipart/related; boundary=\"%s\"\r\n", boundary);
write_to_socket(buf);
return(0);
}
write_to_socket("\r\n");
return(0);
}
int process_embeded_images(const char *boundary)
{
char
*ib,
*b,
related[17],
cid[17],
tbuf[24],
alternative[17];
Attachment
*a;
Sll
*il,
*oneline_attachment_list,
*attachment_list,
*embed_image_list;
int
rc,
ic = 1;
oneline_attachment_list = get_oneline_attachment_list();
attachment_list = get_attachment_list();
embed_image_list = get_embed_image_attachment_list();
if (embed_image_list == NULL)
{
return(0);
}
memset(related, 0, sizeof(related));
memset(alternative, 0, sizeof(alternative));
mutilsGenerateMIMEBoundary(related,sizeof(related));
mutilsGenerateMIMEBoundary(alternative,sizeof(alternative));
b = boundary;
ib = boundary;
(void) snprintf(buf, bufsz, "\r\n--%s\r\n",boundary);
write_to_socket(buf);
if (attachment_list || oneline_attachment_list)
{
b = related;
ib = b;
(void) snprintf(buf, bufsz, "Content-Type: multipart/related; boundary=%s\r\n",b);
write_to_socket(buf);