forked from simta/simta
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheader.c
More file actions
2482 lines (1992 loc) · 50.4 KB
/
header.c
File metadata and controls
2482 lines (1992 loc) · 50.4 KB
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
/********** header.c **********/
#include "config.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifdef HAVE_LIBSSL
#include <openssl/ssl.h>
#include <openssl/rand.h>
#include <openssl/err.h>
#endif /* HAVE_LIBSSL */
#ifdef HAVE_LIBSASL
#include <sasl/sasl.h>
#endif /* HAVE_LIBSASL */
#include <snet.h>
#include <time.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <netdb.h>
#include <pwd.h>
#include <unistd.h>
#include <stdlib.h>
#include <syslog.h>
#include <dirent.h>
#include "denser.h"
#include "line_file.h"
#include "envelope.h"
#include "header.h"
#include "simta.h"
#include "queue.h"
#define TOKEN_UNDEFINED 0
#define TOKEN_QUOTED_STRING 1
#define TOKEN_DOT_ATOM 2
#define TOKEN_DOMAIN_LITERAL 3
#define MAILBOX_FROM_VERIFY 1
#define MAILBOX_FROM_CORRECT 2
#define MAILBOX_SENDER 3
#define MAILBOX_TO_CORRECT 4
#define MAILBOX_RECIPIENTS_CORRECT 5
#define MAILBOX_GROUP_CORRECT 6
#define HEADER_STDERR 1
#define HEADER_NO_ERR 2
#define TEXT_PLAIN "text/plain;"
struct line_token {
int t_type;
char *t_start;
struct line *t_start_line;
char *t_end;
struct line *t_end_line;
char *t_unfolded;
};
char *skip_ws( char * );
int line_token_dot_atom( struct line_token *, struct line *, char * );
int line_token_quoted_string( struct line_token *, struct line *, char * );
int line_token_domain_literal( struct line_token *, struct line *, char * );
void header_stdout( struct header[] );
void header_exceptions( struct line_file * );
char *skip_cfws( struct line **, char ** );
int is_dot_atom_text( int );
int parse_addr( struct envelope *, struct line **, char **, int );
int parse_mailbox_list( struct envelope *, struct line *, char *, int );
int parse_recipients( struct envelope *, struct line *, char * );
int match_sender( struct line_token *, struct line_token *, char * );
int line_token_unfold( struct line_token * );
int header_lines( struct line_file *, struct header *, int );
int mid_text( struct receive_headers *, char *, char ** );
int seen_text( struct receive_headers *, char *, char ** );
int is_unquoted_atom_text( int );
char *token_unquoted_atom( char * );
void make_more_seen( struct receive_headers * );
char *append_seen( struct receive_headers *, char *, int );
struct header headers_punt[] = {
{ "Content-Type", NULL, NULL },
#define PUNT_CONTENT 0
{ NULL, NULL, NULL }
};
struct header headers_simsendmail[] = {
{ "Date", NULL, NULL },
#define HEAD_DATE 0
{ "From", NULL, NULL },
#define HEAD_FROM 1
{ "Sender", NULL, NULL },
#define HEAD_SENDER 2
{ "To", NULL, NULL },
#define HEAD_TO 3
{ "Message-ID", NULL, NULL },
#define HEAD_MESSAGE_ID 4
{ "Cc", NULL, NULL },
#define HEAD_CC 5
{ "Bcc", NULL, NULL },
#define HEAD_BCC 6
{ "Reply-To", NULL, NULL },
#define HEAD_REPLY_TO 7
{ "References", NULL, NULL },
#define HEAD_REFRENCES 8
{ "Subject", NULL, NULL },
#define HEAD_SUBJECT 9
{ NULL, NULL, NULL }
};
int simta_generate_sender;
int
match_sender( struct line_token *local, struct line_token *domain, char *addr )
{
char *a;
char *b;
/* only try to match dot atext for sender */
if (( local->t_type != TOKEN_DOT_ATOM ) ||
( domain->t_type != TOKEN_DOT_ATOM )) {
return( 0 );
}
a = addr;
b = local->t_start;
while ( b != local->t_end + 1 ) {
if ( *a != *b ) {
return( 0 );
}
a++;
b++;
}
if ( *a != '@' ) {
return( 0 );
}
a++;
b = domain->t_start;
while ( b != domain->t_end + 1 ) {
if ( *a != *b ) {
return( 0 );
}
a++;
b++;
}
if ( *a != '\0' ) {
return( 0 );
}
return( 1 );
}
/*
* return non-zero if the headers can't be uncommented
* return 0 on success
* -c will be on next word, or NULL
* -l will be on c's line, or NULL
*/
char *
skip_cfws( struct line **l, char **c )
{
int comment = 0;
struct line *comment_line = NULL;
for ( ; ; ) {
switch ( **c ) {
case ' ':
case '\t':
break;
case '(':
if ( comment_line == NULL ) {
comment_line = *l;
}
comment++;
break;
case ')':
comment --;
if ( comment == 0 ) {
comment_line = NULL;
} else if ( comment < 0 ) {
return( "unbalanced )" );
}
break;
case '\\':
(*c)++;
if ( **c == '\0' ) {
/* trailing '\' is illegal */
return( "trailing '\\' is illegal" );
}
break;
case '\0':
/* end of line. if next line starts with WSP, continue */
if (((*l)->line_next != NULL ) &&
(( *((*l)->line_next->line_data) == ' ' ) ||
( *((*l)->line_next->line_data) == '\t' ))) {
*l = (*l)->line_next;
*c = (*l)->line_data;
break;
} else {
/* End of header */
*c = NULL;
if ( comment_line != NULL ) {
*l = comment_line;
return( "unbalanced \(" );
} else {
return( NULL );
}
}
default:
if ( comment == 0 ) {
return( NULL );
}
}
(*c)++;
}
}
void
header_stdout( struct header h[])
{
while ( h->h_key != NULL ) {
if ( h->h_line != NULL ) {
printf( "%s\n", h->h_line->line_data );
if ( h->h_data != NULL ) {
printf( "\tdata: %s\n", h->h_data );
}
} else {
printf( "%s NULL\n", h->h_key );
}
h++;
}
}
/* Some mail clents exhibit bad behavior when generating headers.
*
* return 0 if all went well.
* return 1 if we reject the message.
* die -1 if there was a serious error.
*/
void
header_exceptions( struct line_file *lf )
{
char *c;
char *end;
if ( lf->l_first == NULL ) {
/* empty message */
return;
}
/* mail(1) on Solaris gives non-rfc compliant first header line */
c = lf->l_first->line_data;
if ( strncasecmp( c, "From ", 5 ) == 0 ) {
c += 5;
for ( end = c; ( *end > 33 ) && ( *end < 126 ); end++ )
;
/* if "From "word ..., rewrite header "From:"word'\0' */
if (( end - c ) > 0 ) {
*(lf->l_first->line_data + 4) = ':';
*end = '\0';
}
}
}
int
header_file_out( struct line_file *lf, FILE *file )
{
struct line *l;
for ( l = lf->l_first; l != NULL; l = l->line_next ) {
if ( fprintf( file, "%s\n", l->line_data ) < 0 ) {
return( 1 );
}
}
return( 0 );
}
int
header_timestamp( struct envelope *env, FILE *file )
{
time_t clock;
struct tm *tm;
char daytime[ 30 ];
if ( time( &clock ) < 0 ) {
return( -1 );
}
if (( tm = localtime( &clock )) == NULL ) {
return( -1 );
}
if ( strftime( daytime, sizeof( daytime ), "%e %b %Y %T", tm ) == 0 ) {
return( -1 );
}
/* Received header */
if ( fprintf( file, "Received: FROM %s\n\tBY %s ID %s ;\n\t%s %s\n",
env->e_mail, simta_hostname, env->e_id, daytime, tz( tm )) < 0 ) {
return( -1 );
}
return( 0 );
}
int
mid_text( struct receive_headers *r, char *line, char **msg )
{
char *start;
char *end;
if (( start = skip_cws( line )) != NULL ) {
if ( r->r_mid != NULL ) {
free( r->r_mid );
r->r_mid = NULL;
r->r_state = R_HEADER_READ;
if ( msg != NULL ) {
*msg = "Illegal Message-ID Header: illegal extra content";
}
return( 0 );
}
if ( *start != '<' ) {
r->r_state = R_HEADER_READ;
if ( msg != NULL ) {
*msg = "Illegal Message-ID Header: expected '<' character";
}
return( 0 );
}
start++;
if ( *start == '"' ) {
if (( end = token_quoted_string( start )) == NULL ) {
r->r_state = R_HEADER_READ;
if ( msg != NULL ) {
*msg = "Illegal Message-ID Header: bad LHS quoted string";
}
return( 0 );
}
} else {
if (( end = token_dot_atom( start )) == NULL ) {
r->r_state = R_HEADER_READ;
if ( msg != NULL ) {
*msg = "Illegal Message-ID Header: bad LHS dot atom text";
}
return( 0 );
}
}
end++;
if ( *end != '@' ) {
r->r_state = R_HEADER_READ;
if ( msg != NULL ) {
*msg = "Illegal Message-ID Header: expected '@'";
}
return( 0 );
}
end++;
if ( *end == '[' ) {
if (( end = token_domain_literal( end )) == NULL ) {
r->r_state = R_HEADER_READ;
if ( msg != NULL ) {
*msg = "Illegal Message-ID Header: bad RHS domain literal";
}
return( 0 );
}
} else {
if (( end = token_dot_atom( end )) == NULL ) {
r->r_state = R_HEADER_READ;
if ( msg != NULL ) {
*msg = "Illegal Message-ID Header: bad RHS dot atom text";
}
return( 0 );
}
}
end++;
if ( *end != '>' ) {
r->r_state = R_HEADER_READ;
if ( msg != NULL ) {
*msg = "Illegal Message-ID Header: expected '>'";
}
return( 0 );
}
*end = '\0';
if (( r->r_mid = strdup( start )) == NULL ) {
*end = '>';
syslog( LOG_ERR, "mid_text strdup: %m" );
return( -1 );
}
*end = '>';
if ( skip_cws( end + 1 ) != NULL ) {
free( r->r_mid );
r->r_mid = NULL;
r->r_state = R_HEADER_READ;
if ( msg != NULL ) {
*msg = "Illegal Message-ID Header: illegal extra content";
}
return( 0 );
}
}
return( 0 );
}
void
make_more_seen( struct receive_headers *r )
{
int n = 0;
int i;
char **cpp;
if ( !r ) {
return;
}
if ( r->r_all_seen_before ) {
for ( i = 0 ; r->r_all_seen_before[ i ] ; ++i ) {
}
n = i;
}
cpp = malloc( (n+2) * sizeof *cpp );
if ( cpp ) {
if ( r->r_all_seen_before ) {
memcpy( cpp, r->r_all_seen_before, n * sizeof *cpp );
free( r->r_all_seen_before );
}
cpp[ n ] = strdup( "" );
cpp[ n+1 ] = 0;
r->r_all_seen_before = cpp;
syslog( LOG_ERR, "make_more_seen: n=%d\n", n);
}
}
char *
append_seen( struct receive_headers *r, char *msg, int l2 )
{
int i;
int l1;
char *cp;
char *new;
if ( !r || ! r->r_all_seen_before) {
errno = EDOM;
return( 0 );
}
for ( i = 0 ; r->r_all_seen_before[ i ] ; ++i ) {
}
if ( !i ) {
errno = EDOM;
return( 0 );
}
--i;
cp = r->r_all_seen_before[ i ];
l1 = strlen( cp );
if ( (new = malloc( l1 + l2 + 1 + !!*cp )) ) {
if ( *cp ) {
memcpy( new, cp, l1 );
new[ l1++ ] = ' ';
if ( r->r_seen_before == cp ) {
r->r_seen_before = new;
}
} else {
int l3 = strlen( simta_seen_before_domain );
if ( l3 == l2 && !memcmp( msg, simta_seen_before_domain, l3 )) {
r->r_seen_before = new;
}
}
memcpy( new + l1, msg, l2 );
new[ l1 + l2 ] = 0;
free(cp);
r->r_all_seen_before[ i ] = new;
}
return( new );
}
int
seen_text( struct receive_headers *r, char *line, char **msg )
{
char *start;
char *end;
char *t;
while (( start = skip_cws( line )) != NULL ) {
if ( *start == '"' ) {
t = "Illegal " STRING_SEEN_BEFORE " Header: bad quoted string";
end = token_quoted_string( start );
} else {
t = "Illegal " STRING_SEEN_BEFORE " Header: bad unquoted atom text";
end = token_unquoted_atom( start );
}
if ( end == NULL ) {
r->r_state = R_HEADER_READ;
if ( msg != NULL ) {
*msg = t;
}
return( 0 );
}
end++;
if (!(append_seen( r, start, end - start ))) {
syslog( LOG_ERR, "seen_text strdup: %m" );
return( 1 );
}
line = end;
}
return( 0 );
}
/* return 0 if line is the next line in header block lf */
/* rfc2822, 2.1 General Description:
* A message consists of header fields (collectively called "the header
* of the message") followed, optionally, by a body. The header is a
* sequence of lines of characters with special syntax as defined in
* this standard. The body is simply a sequence of characters that
* follows the header and is separated from the header by an empty line
* (i.e., a line with nothing preceding the CRLF).
*/
/* rfc 2822, 2.2.2. Structured Header Field Bodies:
* SP, ASCII value 32) and horizontal tab (HTAB, ASCII value 9)
* characters (together known as the white space characters, WSP
*/
/* this only takes two header types in to account, so it is currently
* a state machine. If additional headers need to be added, a better
* strategy might be to cache header lines in core and do analysis on
* complete header fields one at a time.
*/
int
header_text( int line_no, char *line, struct receive_headers *r, char **msg )
{
char *c;
int header_len;
/* null line means that message data begins */
if ((( *line ) == '\0' ) ||
(( r != NULL ) && ( r->r_state == R_HEADER_END ))) {
/* blank line, or headers already over */
if ( r != NULL ) {
r->r_state = R_HEADER_END;
}
return( 1 );
} else if (( *line == ' ' ) || ( *line == '\t' )) {
/* if line is not the first line it could be header FWS */
if ( line_no == 1 ) {
if ( r != NULL ) {
r->r_state = R_HEADER_END;
}
return( 1 );
} else if (( r != NULL ) && ( r->r_state == R_HEADER_MID )) {
return( mid_text( r, line, msg ));
} else if (( r != NULL ) && ( r->r_state == R_HEADER_SEEN )) {
return( seen_text( r, line, msg ));
}
} else {
/* line could be started with a new header */
if ( r != NULL ) {
r->r_state = R_HEADER_READ;
}
for ( c = line; *c != ':'; c++ ) {
/* colon ascii value is 58 */
if (( *c < 33 ) || ( *c > 126 )) {
break;
}
}
/* check to e if it's a proper field name followed by a colon */
if (( *c == ':' ) && (( header_len = ( c - line )) > 0 )) {
if ( r == NULL ) {
return( 0 );
}
if (( header_len == STRING_MID_LEN ) &&
( strncasecmp( line, STRING_MID, STRING_MID_LEN ) == 0 )) {
if ( r->r_mid_set != 0 ) {
if ( msg != NULL ) {
*msg = "Illegal Duplicate Message-ID Headers";
}
if ( r->r_mid != NULL ) {
free( r->r_mid );
r->r_mid = NULL;
}
return( 0 );
}
r->r_mid_set = 1;
r->r_state = R_HEADER_MID;
return( mid_text( r, c + 1, msg ));
} else if (( header_len == STRING_RECEIVED_LEN ) &&
( strncasecmp( line, STRING_RECEIVED,
STRING_RECEIVED_LEN ) == 0 )) {
r->r_received_count++;
} else if (( header_len == STRING_SEEN_BEFORE_LEN ) &&
( strncasecmp( line, STRING_SEEN_BEFORE,
STRING_SEEN_BEFORE_LEN ) == 0 )) {
r->r_state = R_HEADER_SEEN;
make_more_seen( r );
return( seen_text( r, c + 1, msg ));
}
} else {
/* not a proper header */
if ( r != NULL ) {
r->r_state = R_HEADER_END;
}
return( 1 );
}
}
return( 0 );
}
int
header_lines( struct line_file *lf, struct header headers[], int err_out )
{
struct line *l;
struct header *h;
char *colon;
size_t header_len;
/* put header information in to data structures for later processing */
for ( l = lf->l_first; l != NULL ; l = l->line_next ) {
/* rfc 2822:
* Header fields are lines composed of a field name, followed
* by a colon (":"), followed by a field body, and terminated
* by CRLF. A field name MUST be composed of printable
* US-ASCII characters (i.e., characters that have values
* between 33 and 126, inclusive), except colon.
*/
/* line is FWS if first character of the line is whitespace */
if (( *l->line_data == ' ' ) || ( *l->line_data == '\t' )) {
continue;
}
for ( colon = l->line_data; *colon != ':'; colon++ )
;
header_len = ( colon - ( l->line_data ));
/* field name followed by a colon */
for ( h = headers; h->h_key != NULL; h++ ) {
if ( strncasecmp( h->h_key, l->line_data, header_len ) == 0 ) {
/* correct field name */
if ( h->h_line == NULL ) {
h->h_line = l;
} else {
/* header h->h_key appears at least twice */
if ( err_out == HEADER_STDERR ) {
fprintf( stderr,
"line %d: illegal duplicate header %s\n",
l->line_no, h->h_key );
} else {
/* XXX syslog? */
}
return( 1 );
}
}
}
}
return( 0 );
}
/* return 0 if we don't punt
* return 1 if we punt
*/
int
header_punt( struct line_file *lf )
{
char *c;
struct line *l;
if ( header_lines( lf, headers_punt, HEADER_NO_ERR ) != 0 ) {
return( 1 );
}
if (( l = headers_punt[ PUNT_CONTENT ].h_line ) == NULL ) {
return( 0 );
}
c = l->line_data + 13;
if ( skip_cfws( &l, &c ) != NULL ) {
return( 1 );
}
if ( c != NULL ) {
if ( strncasecmp( c, TEXT_PLAIN, strlen( TEXT_PLAIN )) == 0 ) {
return( 0 );
}
}
return( 1 );
}
/* return 0 if all went well.
* return 1 if we reject the message.
* return -1 if there was a serious error.
*/
/* all errors out to stderr, as you should only be correcting headers
* from simsendmail, for now.
*/
int
header_correct( int read_headers, struct line_file *lf, struct envelope *env )
{
struct line *l;
struct line **lp;
int result;
char *prepend_line = NULL;
size_t prepend_len = 0;
size_t len;
time_t clock;
struct tm *tm;
char daytime[ 35 ];
struct envelope *to_env = NULL;
if ( read_headers != 0 ) {
to_env = env;
}
/* check headers for known mail clients behaving badly */
header_exceptions( lf );
if ( header_lines( lf, headers_simsendmail, HEADER_NO_ERR ) != 0 ) {
return( 1 );
}
simta_generate_sender = 0;
/* examine & correct header data */
/* From: */
if (( l = headers_simsendmail[ HEAD_FROM ].h_line ) != NULL ) {
if (( result = parse_mailbox_list( NULL, l, l->line_data + 5,
MAILBOX_FROM_CORRECT )) != 0 ) {
return( result );
}
} else {
/* generate From: header */
if (( len = ( strlen( headers_simsendmail[ HEAD_FROM ].h_key ) +
strlen( env->e_mail ) + 3 )) > prepend_len ) {
if (( prepend_line = (char*)realloc( prepend_line, len ))
== NULL ) {
perror( "realloc" );
return( -1 );
}
prepend_len = len;
}
sprintf( prepend_line, "%s: %s",
headers_simsendmail[ HEAD_FROM ].h_key, env->e_mail );
if (( headers_simsendmail[ HEAD_FROM ].h_line =
line_prepend( lf, prepend_line, COPY )) == NULL ) {
perror( "malloc" );
return( -1 );
}
}
/* Sender: */
if (( l = headers_simsendmail[ HEAD_SENDER ].h_line ) != NULL ) {
if (( result = parse_mailbox_list( env, l, l->line_data + 7,
MAILBOX_SENDER )) != 0 ) {
return( result );
}
} else {
if (( simta_simsend_strict_from != 0 ) &&
( simta_generate_sender != 0 )) {
if (( len = ( strlen( headers_simsendmail[ HEAD_SENDER ].h_key ) +
strlen( env->e_mail ) + 3 )) > prepend_len ) {
if (( prepend_line = (char*)realloc( prepend_line, len ))
== NULL ) {
perror( "realloc" );
return( -1 );
}
prepend_len = len;
sprintf( prepend_line, "%s: %s",
headers_simsendmail[ HEAD_SENDER ].h_key, env->e_mail );
if (( headers_simsendmail[ HEAD_SENDER ].h_line =
line_prepend( lf, prepend_line, COPY )) == NULL ) {
perror( "malloc" );
return( -1 );
}
}
}
}
if ( headers_simsendmail[ HEAD_DATE ].h_line == NULL ) {
if ( time( &clock ) < 0 ) {
perror( "time" );
return( -1 );
}
if (( tm = localtime( &clock )) == NULL ) {
perror( "localtime" );
return( -1 );
}
if ( strftime( daytime, sizeof( daytime ), "%a, %e %b %Y %T", tm )
== 0 ) {
perror( "strftime" );
return( -1 );
}
if (( len = ( strlen( headers_simsendmail[ HEAD_DATE ].h_key ) +
strlen( daytime ) + 3 )) > prepend_len ) {
if (( prepend_line = (char*)realloc( prepend_line, len ))
== NULL ) {
perror( "realloc" );
return( -1 );
}
prepend_len = len;
}
sprintf( prepend_line, "%s: %s",
headers_simsendmail[ HEAD_DATE ].h_key, daytime );
if (( headers_simsendmail[ HEAD_DATE ].h_line =
line_prepend( lf, prepend_line, COPY )) == NULL ) {
perror( "malloc" );
return( -1 );
}
}
if ( headers_simsendmail[ HEAD_MESSAGE_ID ].h_line == NULL ) {
if (( len = ( strlen( headers_simsendmail[ HEAD_MESSAGE_ID ].h_key ) +
strlen( env->e_id ) + 6 + strlen( simta_hostname ))) >
prepend_len ) {
if (( prepend_line = (char*)realloc( prepend_line, len ))
== NULL ) {
perror( "realloc" );
return( -1 );
}
prepend_len = len;
}
sprintf( prepend_line, "%s: <%s@%s>",
headers_simsendmail[ HEAD_MESSAGE_ID ].h_key, env->e_id,
simta_hostname );
if (( headers_simsendmail[ HEAD_MESSAGE_ID ].h_line =
line_prepend( lf, prepend_line, COPY )) == NULL ) {
perror( "malloc" );
return( -1 );
}
}
if (( l = headers_simsendmail[ HEAD_TO ].h_line ) != NULL ) {
if (( result = parse_recipients( to_env, l, l->line_data + 3 )) != 0 ) {
return( result );
}
}
if ( headers_simsendmail[ HEAD_CC ].h_line != NULL ) {
if (( result = parse_recipients( to_env, l, l->line_data + 3 )) != 0 ) {
return( result );
}
}
if (( l = headers_simsendmail[ HEAD_BCC ].h_line ) != NULL ) {
if (( result = parse_recipients( to_env, l, l->line_data + 4 )) != 0 ) {
return( result );
}
/* remove bcc lines */
if ( l->line_prev != NULL ) {
lp = &(l->line_prev->line_next);
} else {
lp = &(lf->l_first);
}
for ( l = l->line_next; l != NULL; l = l->line_next ) {
if (( *(l->line_data) != ' ' ) && ( *(l->line_data) != '\t' )) {
break;
}
}
*lp = l;
/* XXX free bcc lines if we're anal */
}
#ifdef DEBUG
header_stdout( headers_simsendmail );
#endif /* DEBUG */
if ( prepend_line != NULL ) {
free( prepend_line );
}
return( 0 );
}
/* return 0 if all went well.
* return 1 if we reject the message.
* return -1 if there was a serious error.
*/
/* all errors out to stderr, as you should only be correcting headers
* from simsendmail, for now.
*/
/* RFC 2822:
*
* address = mailbox / group
* group = display-name ":" [mailbox-list / CFWS] ";" [CFWS]
* mailbox-list = (mailbox *("," mailbox))
* mailbox = name-addr / addr-spec
* name-addr = [display-name] angle-addr