-
Notifications
You must be signed in to change notification settings - Fork 18
/
ssmtp.c
2146 lines (1827 loc) · 43.8 KB
/
ssmtp.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
/*
sSMTP -- send messages via SMTP to a mailhub for local delivery or forwarding.
This program is used in place of /usr/sbin/sendmail, called by "mail" (et all).
sSMTP does a selected subset of sendmail's standard tasks (including exactly
one rewriting task), and explains if you ask it to do something it can't. It
then sends the mail to the mailhub via an SMTP connection. Believe it or not,
this is nothing but a filter
See COPYRIGHT for the license
*/
#define VERSION "2.65"
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/param.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
#include <syslog.h>
#include <signal.h>
#include <setjmp.h>
#include <string.h>
#include <ctype.h>
#include <netdb.h>
#ifdef HAVE_SSL
#ifdef HAVE_GNUTLS
#include <gnutls/openssl.h>
#else
#include <openssl/crypto.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#endif
#endif
#ifdef MD5AUTH
#include "md5auth/hmac_md5.h"
#endif
#include "ssmtp.h"
#include <fcntl.h>
#include "xgethostname.h"
/* Warning removal by defining defaults */
#ifndef REVALIASES_FILE
#define REVALIASES_FILE "/etc/ssmtp/revaliases"
#endif
#ifndef CONFIGURATION_FILE
#define CONFIGURATION_FILE "/etc/ssmtp/ssmtp.conf"
#endif
bool_t have_date = False;
bool_t have_from = False;
#ifdef HASTO_OPTION
bool_t have_to = False;
#endif
bool_t minus_t = False;
bool_t minus_v = False;
bool_t override_from = False;
bool_t rewrite_domain = False;
bool_t use_tls = False; /* Use SSL to transfer mail to HUB */
bool_t use_starttls = False; /* SSL only after STARTTLS (RFC2487) */
bool_t use_cert = False; /* Use a certificate to transfer SSL mail */
bool_t use_oldauth = False; /* use old AUTH LOGIN username style */
#define ARPADATE_LENGTH 32 /* Current date in RFC format */
char arpadate[ARPADATE_LENGTH];
char *auth_user = '\0';
char *auth_pass = '\0';
char *auth_method = '\0'; /* Mechanism for SMTP authentication */
char *mail_domain = '\0';
char *from = '\0'; /* Use this as the From: address */
char *hostname;
char *mailhost = "mailhub";
char *minus_f = '\0';
char *minus_F = '\0';
char *gecos;
char *prog = '\0';
char *root = NULL;
char *tls_cert = "/etc/ssl/certs/ssmtp.pem"; /* Default Certificate */
char *uad = '\0';
char *config_file = '\0'; /* alternate configuration file */
headers_t headers, *ht;
#ifdef DEBUG
int log_level = 1;
#else
int log_level = 0;
#endif
int minuserid = MAXSYSUID+1;
int port = 25;
#ifdef INET6
int p_family = PF_UNSPEC; /* Protocol family used in SMTP connection */
#endif
jmp_buf TimeoutJmpBuf; /* Timeout waiting for input from network */
rcpt_t rcpt_list, *rt;
#ifdef HAVE_SSL
SSL *ssl;
#endif
#ifdef MD5AUTH
static char hextab[]="0123456789abcdef";
#endif
ssize_t outbytes;
#if defined (__SVR4) && defined (__sun)
/*
strndup() - Unimplemented by the Solaris libc.
*/
char *strndup(char const *s, size_t n)
{
size_t len = strlen(s, n);
char *new = malloc(len + 1);
if(new == NULL) return NULL;
new[len] = '\0';
return memcpy(new, s, len);
}
#endif
/*
log_event() -- Write event to syslog (or log file if defined)
*/
void log_event(int priority, char *format, ...)
{
char buf[(BUF_SZ + 1)];
va_list ap;
va_start(ap, format);
(void)vsnprintf(buf, BUF_SZ, format, ap);
va_end(ap);
#ifdef LOGFILE
FILE *fp;
if((fp = fopen("/tmp/ssmtp.log", "a")) != (FILE *)NULL) {
(void)fprintf(fp, "%s\n", buf);
(void)fclose(fp);
}
else {
(void)fprintf(stderr, "Can't write to /tmp/ssmtp.log\n");
}
#endif
#if HAVE_SYSLOG_H
#if OLDSYSLOG
openlog("sSMTP", LOG_PID);
#else
openlog("sSMTP", LOG_PID, LOG_MAIL);
#endif
syslog(priority, "%s", buf);
closelog();
#endif
}
ssize_t smtp_write(int fd, char *format, ...);
int smtp_read(int fd, char *response);
int smtp_read_all(int fd, char *response);
int smtp_okay(int fd, char *response);
/*
dead_letter() -- Save stdin to ~/dead.letter if possible
*/
void dead_letter(void)
{
char *path;
char buf[(BUF_SZ + 1)];
struct passwd *pw;
uid_t uid;
FILE *fp;
uid = getuid();
pw = getpwuid(uid);
if(isatty(fileno(stdin))) {
if(log_level > 0) {
log_event(LOG_ERR,
"stdin is a TTY - not saving to %s/dead.letter", pw->pw_dir);
}
return;
}
if(pw == (struct passwd *)NULL) {
/* Far to early to save things */
if(log_level > 0) {
log_event(LOG_ERR, "No sender failing horribly!");
}
return;
}
#define DEAD_LETTER "/dead.letter"
path = malloc (strlen (pw->pw_dir) + sizeof (DEAD_LETTER));
if (!path) {
/* Can't use die() here since dead_letter() is called from die() */
exit(1);
}
memcpy (path, pw->pw_dir, strlen (pw->pw_dir));
memcpy (path + strlen (pw->pw_dir), DEAD_LETTER, sizeof (DEAD_LETTER));
if((fp = fopen(path, "a")) == (FILE *)NULL) {
/* Perhaps the person doesn't have a homedir... */
if(log_level > 0) {
log_event(LOG_ERR, "Can't open %s failing horribly!", path);
}
free(path);
return;
}
/* We start on a new line with a blank line separating messages */
(void)fprintf(fp, "\n\n");
while(fgets(buf, sizeof(buf), stdin)) {
(void)fputs(buf, fp);
}
if(fclose(fp) == -1) {
if(log_level > 0) {
log_event(LOG_ERR,
"Can't close %s/dead.letter, possibly truncated", pw->pw_dir);
}
}
free(path);
}
/*
die() -- Write error message, dead.letter and exit
*/
void die(char *format, ...)
{
char buf[(BUF_SZ + 1)];
va_list ap;
va_start(ap, format);
(void)vsnprintf(buf, BUF_SZ, format, ap);
va_end(ap);
(void)fprintf(stderr, "%s: %s\n", prog, buf);
log_event(LOG_ERR, "%s", buf);
/* Send message to dead.letter */
(void)dead_letter();
exit(1);
}
/*
xbasename() -- Return last element of path
*/
char *xbasename(char *str)
{
char *p;
p = strrchr(str, '/');
if (!p) {
p = str;
}
return(strdup(p));
}
/*
strip_pre_ws() -- Return pointer to first non-whitespace character
*/
char *strip_pre_ws(char *str)
{
char *p;
p = str;
while(*p && isspace(*p)) p++;
return(p);
}
/*
strip_post_ws() -- Return pointer to last non-whitespace character
*/
char *strip_post_ws(char *str)
{
char *p;
p = (str + strlen(str));
while(isspace(*--p)) {
*p = '\0';
}
return(p);
}
/*
addr_parse() -- Parse <user@domain.com> from full email address
*/
char *addr_parse(char *str)
{
char *p, *q;
#if 0
(void)fprintf(stderr, "*** addr_parse(): str = [%s]\n", str);
#endif
/* Simple case with email address enclosed in <> */
if((p = strdup(str)) == (char *)NULL) {
die("addr_parse(): strdup()");
}
if((q = strchr(p, '<'))) {
q++;
if((p = strchr(q, '>'))) {
*p = '\0';
}
#if 0
(void)fprintf(stderr, "*** addr_parse(): q = [%s]\n", q);
#endif
return(q);
}
q = strip_pre_ws(p);
if(*q == '(') {
while((*q++ != ')'));
}
p = strip_pre_ws(q);
#if 0
(void)fprintf(stderr, "*** addr_parse(): p = [%s]\n", p);
#endif
q = strip_post_ws(p);
if(*q == ')') {
while((*--q != '('));
*q = '\0';
}
(void)strip_post_ws(p);
#if 0
(void)fprintf(stderr, "*** addr_parse(): p = [%s]\n", p);
#endif
return(p);
}
/*
append_domain() -- Fix up address with @domain.com
*/
char *append_domain(char *str)
{
char buf[(BUF_SZ + 1)];
if(strchr(str, '@') == (char *)NULL) {
if(snprintf(buf, BUF_SZ, "%s@%s", str,
#ifdef REWRITE_DOMAIN
rewrite_domain == True ? mail_domain : hostname
#else
hostname
#endif
) == -1) {
die("append_domain() -- snprintf() failed");
}
return(strdup(buf));
}
return(strdup(str));
}
/*
standardise() -- Trim off '\n's and double leading dots
*/
bool_t standardise(char *str, bool_t *linestart)
{
size_t sl;
char *p;
bool_t leadingdot = False;
/* Any line beginning with a dot has an additional dot inserted;
not just a line consisting solely of a dot. Thus we have to move
the buffer start up one */
if(*linestart && *str == '.') {
leadingdot = True;
}
*linestart = False;
if((p = strchr(str, '\n'))) {
*p = '\0';
*linestart = True;
}
return(leadingdot);
}
/*
revaliases() -- Parse the reverse alias file
Fix globals to use any entry for sender
*/
void revaliases(struct passwd *pw)
{
char buf[(BUF_SZ + 1)], *p;
FILE *fp;
/* Try to open the reverse aliases file */
if((fp = fopen(REVALIASES_FILE, "r"))) {
/* Search if a reverse alias is defined for the sender */
while(fgets(buf, sizeof(buf), fp)) {
/* Make comments invisible */
if((p = strchr(buf, '#'))) {
*p = '\0';
}
/* Ignore malformed lines and comments */
if(strchr(buf, ':') == (char *)NULL) {
continue;
}
/* Parse the alias */
if(((p = strtok(buf, ":"))) && !strcmp(p, pw->pw_name)) {
if((p = strtok(NULL, ": \t\r\n"))) {
if((uad = strdup(p)) == (char *)NULL) {
die("revaliases() -- strdup() failed");
}
}
if((p = strtok(NULL, " \t\r\n:"))) {
if((mailhost = strdup(p)) == (char *)NULL) {
die("revaliases() -- strdup() failed");
}
if((p = strtok(NULL, " \t\r\n:"))) {
port = atoi(p);
}
if(log_level > 0) {
log_event(LOG_INFO, "Set MailHub=\"%s\"\n", mailhost);
log_event(LOG_INFO,
"via SMTP Port Number=\"%d\"\n", port);
}
}
}
}
fclose(fp);
}
}
/*
from_strip() -- Transforms "Name <login@host>" into "login@host" or "login@host (Real name)"
*/
char *from_strip(char *str)
{
char *p;
#if 0
(void)fprintf(stderr, "*** from_strip(): str = [%s]\n", str);
#endif
if(strncmp("From:", str, 5) == 0) {
str += 5;
}
/* Remove the real name if necessary - just send the address */
if((p = addr_parse(str)) == (char *)NULL) {
die("from_strip() -- addr_parse() failed");
}
#if 0
(void)fprintf(stderr, "*** from_strip(): p = [%s]\n", p);
#endif
return(strdup(p));
}
/*
from_format() -- Generate standard From: line
*/
char *from_format(char *str, bool_t override_from)
{
char buf[(BUF_SZ + 1)] = "";
if(override_from) {
if(minus_f) {
str = append_domain(minus_f);
}
if(minus_F) {
if(snprintf(buf,
BUF_SZ, "\"%s\" <%s>", minus_F, str) == -1) {
die("from_format() -- snprintf() failed");
}
}
else if(gecos) {
if(snprintf(buf, BUF_SZ, "\"%s\" <%s>", gecos, str) == -1) {
die("from_format() -- snprintf() failed");
}
}
else {
if(snprintf(buf, BUF_SZ, "%s", str) == -1) {
die("from_format() -- snprintf() failed");
}
}
}
else {
if(gecos) {
if(snprintf(buf, BUF_SZ, "\"%s\" <%s>", gecos, str) == -1) {
die("from_format() -- snprintf() failed");
}
}
else {
if(snprintf(buf, BUF_SZ, "%s", str) == -1) {
die("from_format() -- snprintf() failed");
}
}
}
#if 0
(void)fprintf(stderr, "*** from_format(): buf = [%s]\n", buf);
#endif
return(strdup(buf));
}
/*
rcpt_save() -- Store entry into RCPT list
*/
void rcpt_save(char *str)
{
char *p;
# if 1
/* Horrible botch for group stuff */
p = str;
while(*p) p++;
if(*--p == ';') {
return;
}
#endif
#if 0
(void)fprintf(stderr, "*** rcpt_save(): str = [%s]\n", str);
#endif
/* Ignore missing usernames */
if(*str == '\0') {
return;
}
if((rt->string = strdup(str)) == (char *)NULL) {
die("rcpt_save() -- strdup() failed");
}
rt->next = (rcpt_t *)malloc(sizeof(rcpt_t));
if(rt->next == (rcpt_t *)NULL) {
die("rcpt_save() -- malloc() failed");
}
rt = rt->next;
rt->next = (rcpt_t *)NULL;
}
/*
rcpt_parse() -- Break To|Cc|Bcc into individual addresses
*/
void rcpt_parse(char *str)
{
bool_t in_quotes = False, got_addr = False;
char *p, *q, *r;
#if 0
(void)fprintf(stderr, "*** rcpt_parse(): str = [%s]\n", str);
#endif
if((p = strdup(str)) == (char *)NULL) {
die("rcpt_parse(): strdup() failed");
}
q = p;
/* Replace <CR>, <LF> and <TAB> */
while(*q) {
switch(*q) {
case '\t':
case '\n':
case '\r':
*q = ' ';
}
q++;
}
q = p;
#if 0
(void)fprintf(stderr, "*** rcpt_parse(): q = [%s]\n", q);
#endif
r = q;
while(*q) {
if(*q == '"') {
in_quotes = (in_quotes ? False : True);
}
/* End of string? */
if(*(q + 1) == '\0') {
got_addr = True;
}
/* End of address? */
if((*q == ',') && (in_quotes == False)) {
got_addr = True;
*q = '\0';
}
if(got_addr) {
while(*r && isspace(*r)) r++;
rcpt_save(addr_parse(r));
r = (q + 1);
#if 0
(void)fprintf(stderr, "*** rcpt_parse(): r = [%s]\n", r);
#endif
got_addr = False;
}
q++;
}
free(p);
}
#ifdef MD5AUTH
int crammd5(char *challengeb64, char *username, char *password, char *responseb64)
{
int i;
unsigned char digest[MD5_DIGEST_LEN];
unsigned char digascii[MD5_DIGEST_LEN * 2 + 1];
unsigned char challenge[(BUF_SZ + 1)];
unsigned char response[(BUF_SZ + 1)];
unsigned char secret[(MD5_BLOCK_LEN + 1)];
memset (secret,0,sizeof(secret));
memset (challenge,0,sizeof(challenge));
strncpy (secret, password, sizeof(secret));
if (!challengeb64 || strlen(challengeb64) > sizeof(challenge) * 3 / 4)
return 0;
from64tobits(challenge, challengeb64);
hmac_md5(challenge, strlen(challenge), secret, strlen(secret), digest);
for (i = 0; i < MD5_DIGEST_LEN; i++) {
digascii[2 * i] = hextab[digest[i] >> 4];
digascii[2 * i + 1] = hextab[(digest[i] & 0x0F)];
}
digascii[MD5_DIGEST_LEN * 2] = '\0';
if (sizeof(response) <= strlen(username) + sizeof(digascii))
return 0;
strncpy (response, username, sizeof(response) - sizeof(digascii) - 2);
strcat (response, " ");
strcat (response, digascii);
to64frombits(responseb64, response, strlen(response));
return 1;
}
#endif
/*
rcpt_remap() -- Alias systems-level users to the person who
reads their mail. This is variously the owner of a workstation,
the sysadmin of a group of stations and the postmaster otherwise.
We don't just mail stuff off to root on the mailhub :-)
*/
char *rcpt_remap(char *str)
{
struct passwd *pw;
if((root==NULL) || strlen(root)==0 || strchr(str, '@') ||
((pw = getpwnam(str)) == NULL) || (pw->pw_uid >= minuserid)) {
return(append_domain(str)); /* It's not a local systems-level user */
}
else {
return(append_domain(root));
}
}
/*
header_save() -- Store entry into header list
*/
void header_save(char *str)
{
char *p;
#if 0
(void)fprintf(stderr, "header_save(): str = [%s]\n", str);
#endif
if((p = strdup(str)) == (char *)NULL) {
die("header_save() -- strdup() failed");
}
ht->string = p;
if(strncasecmp(ht->string, "From:", 5) == 0) {
#if 1
/* Hack check for NULL From: line */
if(*(p + 6) == '\0') {
return;
}
#endif
#ifdef REWRITE_DOMAIN
if(override_from == True) {
uad = from_strip(ht->string);
}
else {
return;
}
#endif
have_from = True;
}
#ifdef HASTO_OPTION
else if(strncasecmp(ht->string, "To:" ,3) == 0) {
have_to = True;
}
#endif
else if(strncasecmp(ht->string, "Date:", 5) == 0) {
have_date = True;
}
if(minus_t) {
/* Need to figure out recipients from the e-mail */
if(strncasecmp(ht->string, "To:", 3) == 0) {
p = (ht->string + 3);
rcpt_parse(p);
}
else if(strncasecmp(ht->string, "Bcc:", 4) == 0) {
p = (ht->string + 4);
rcpt_parse(p);
/* Undo adding the header to the list: */
free(ht->string);
ht->string = NULL;
return;
}
else if(strncasecmp(ht->string, "CC:", 3) == 0) {
p = (ht->string + 3);
rcpt_parse(p);
}
}
#if 0
(void)fprintf(stderr, "header_save(): ht->string = [%s]\n", ht->string);
#endif
ht->next = (headers_t *)malloc(sizeof(headers_t));
if(ht->next == (headers_t *)NULL) {
die("header_save() -- malloc() failed");
}
ht = ht->next;
ht->next = (headers_t *)NULL;
}
/*
header_parse() -- Break headers into seperate entries
*/
void header_parse(FILE *stream)
{
size_t size = BUF_SZ, len = 0;
char *p = (char *)NULL, *q;
bool_t in_header = True;
char l = '\0';
int c;
while(in_header && ((c = fgetc(stream)) != EOF)) {
/* Must have space for up to two more characters, since we
may need to insert a '\r' */
if((p == (char *)NULL) || (len >= (size - 1))) {
size += BUF_SZ;
p = (char *)realloc(p, (size * sizeof(char)));
if(p == (char *)NULL) {
die("header_parse() -- realloc() failed");
}
q = (p + len);
}
len++;
if(l == '\n') {
switch(c) {
case ' ':
case '\t':
/* Must insert '\r' before '\n's embedded in header
fields otherwise qmail won't accept our mail
because a bare '\n' violates some RFC */
*(q - 1) = '\r'; /* Replace previous \n with \r */
*q++ = '\n'; /* Insert \n */
len++;
break;
case '\n':
in_header = False;
default:
*q = '\0';
if((q = strrchr(p, '\n'))) {
*q = '\0';
}
header_save(p);
q = p;
len = 0;
}
}
*q++ = c;
l = c;
}
if(in_header) {
if(l == '\n') {
switch(c) {
case ' ':
case '\t':
/* Must insert '\r' before '\n's embedded in header
fields otherwise qmail won't accept our mail
because a bare '\n' violates some RFC */
*(q - 1) = '\r'; /* Replace previous \n with \r */
*q++ = '\n'; /* Insert \n */
len++;
break;
case '\n':
in_header = False;
default:
*q = '\0';
if((q = strrchr(p, '\n'))) {
*q = '\0';
}
header_save(p);
q = p;
len = 0;
}
}
}
(void)free(p);
}
/*
* This is much like strtok, but does not modify the string
* argument.
* Args:
* char **s:
* Address of the pointer to the string we are looking at.
* const char *delim:
* The set of delimiters.
* Return value:
* The first token, copied by strndup (caller have to free it),
* if a token is found, or NULL if isn't (os strndup fails)
* *s points to the rest of the string
*/
char *firsttok(char **s, const char *delim)
{
char *tok;
char *rest;
rest=strpbrk(*s,delim);
if (!rest) {
return NULL;
}
#ifdef HAVE_STRNDUP
tok=strndup(*s,rest-(*s));
#else
{
size_t len = rest - (*s);
tok = malloc(sizeof(char) * (len + 1));
memcpy(tok, *s, len);
tok[len] = '\0';
}
#endif
if (!tok) {
die("firsttok() -- strndup() failed");
}
*s=rest+1;
return tok;
}
/*
read_config() -- Open and parse config file and extract values of variables
*/
bool_t read_config()
{
char buf[(BUF_SZ + 1)], *p, *q, *r;
FILE *fp;
if(config_file == (char *)NULL) {
config_file = strdup(CONFIGURATION_FILE);
if(config_file == (char *)NULL) {
die("parse_config() -- strdup() failed");
}
}
if((fp = fopen(config_file, "r")) == NULL) {
return(False);
}
while(fgets(buf, sizeof(buf), fp)) {
char *begin=buf;
char *rightside;
/* Make comments invisible */
if ((p = strip_pre_ws(buf))[0] == '#') {
*p = '\0';
}
/* Ignore malformed lines and comments */
if(strchr(buf, '=') == (char *)NULL) continue;
/* Parse out keywords */
p=firsttok(&begin, "= \t\n");
if(p){
rightside=begin;
q = firsttok(&begin, "= \t\n");
}
if(p && q) {
if(strcasecmp(p, "Root") == 0) {
if((root = strdup(q)) == (char *)NULL) {
die("parse_config() -- strdup() failed");
}
if(log_level > 0) {
log_event(LOG_INFO, "Set Root=\"%s\"\n", root);
}
}
else if(strcasecmp(p, "MinUserId") == 0) {
if((r = strdup(q)) == (char *)NULL) {
die("parse_config() -- strdup() failed");
}
minuserid = atoi(r);
if(log_level > 0) {
log_event(LOG_INFO, "Set MinUserId=\"%d\"\n", minuserid);
}
}
else if(strcasecmp(p, "MailHub") == 0) {
if((r = strchr(q, ':')) != NULL) {
*r++ = '\0';
port = atoi(r);
}
if((mailhost = strdup(q)) == (char *)NULL) {
die("parse_config() -- strdup() failed");
}
if(log_level > 0) {
log_event(LOG_INFO, "Set MailHub=\"%s\"\n", mailhost);
log_event(LOG_INFO, "Set RemotePort=\"%d\"\n", port);
}
}
else if(strcasecmp(p, "HostName") == 0) {
free(hostname);
hostname = strdup(q);
if (!hostname) {
die("parse_config() -- strdup() failed");
}
if(log_level > 0) {
log_event(LOG_INFO, "Set HostName=\"%s\"\n", hostname);
}
}
else if(strcasecmp(p,"AddHeader") == 0) {
if((r = firsttok(&rightside, "\n#")) != NULL) {
header_save(r);
free(r);
} else {
die("cannot AddHeader");
}
if(log_level > 0 ) {
log_event(LOG_INFO, "Set AddHeader=\"%s\"\n", q);
}
}
#ifdef REWRITE_DOMAIN
else if(strcasecmp(p, "RewriteDomain") == 0) {
if((p = strrchr(q, '@'))) {
mail_domain = strdup(++p);
log_event(LOG_ERR,
"Set RewriteDomain=\"%s\" is invalid\n", q);
log_event(LOG_ERR,
"Set RewriteDomain=\"%s\" used\n", mail_domain);
}
else {
mail_domain = strdup(q);
}
if(mail_domain == (char *)NULL) {
die("parse_config() -- strdup() failed");
}