forked from The-McGrail-Foundation/MIMEDefang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
1346 lines (1217 loc) · 33.5 KB
/
utils.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
/***********************************************************************
*
* utils.c
*
* Utility functions for MIMEDefang
*
* Copyright (C) 2002-2005 Roaring Penguin Software Inc.
* http://www.roaringpenguin.com
*
* This program may be distributed under the terms of the GNU General
* Public License, Version 2, or (at your option) any later version.
*
***********************************************************************/
#define _DEFAULT_SOURCE 1
#include "config.h"
#include "mimedefang.h"
#include <stdio.h>
#include <ctype.h>
#include <syslog.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <errno.h>
#include <stdarg.h>
#include <netinet/in.h>
#include <fcntl.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#ifdef ENABLE_DEBUGGING
extern void *malloc_debug(void *, size_t, char const *fname, int);
extern char *strdup_debug(void *, char const *, char const *, int);
extern void free_debug(void *, void *, char const *, int);
#undef malloc
#undef strdup
#undef free
#define malloc(x) malloc_debug(ctx, x, __FILE__, __LINE__)
#define strdup(x) strdup_debug(ctx, x, __FILE__, __LINE__)
#define free(x) free_debug(ctx, x, __FILE__, __LINE__)
#define malloc_with_log(x) malloc_debug(ctx, x, __FILE__, __LINE__)
#define strdup_with_log(x) strdup_debug(x, __FILE__, __LINE__)
#endif
#ifndef HAVE_UINT32_T
/* On these machines, punt to unsigned int */
typedef unsigned int uint32_t;
#endif
#ifndef AF_LOCAL
#define AF_LOCAL AF_UNIX
#endif
#ifndef SUN_LEN
#define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
+ strlen ((ptr)->sun_path))
#endif
#ifndef INADDR_LOOPBACK
#define INADDR_LOOPBACK 0x7f000001
#endif
static int percent_encode_command(int term_with_newline,
char *out, int outlen, ...);
/**********************************************************************
* %FUNCTION: split_on_space
* %ARGUMENTS:
* buf -- input buffer
* first -- set to first word
* rest -- set to everything following a space, or NULL if no space
* %RETURNS:
* Nothing
* %DESCRIPTION:
* Splits a line on whitespace.
***********************************************************************/
void
split_on_space(char *buf,
char **first,
char **rest)
{
*first = buf;
*rest = NULL;
while(*buf && !isspace(*buf)) buf++;
if (*buf && isspace(*buf)) {
*buf = 0;
*rest = buf+1;
}
}
/**********************************************************************
* %FUNCTION: split_on_space3
* %ARGUMENTS:
* buf -- input buffer
* first -- set to first word
* second -- set to second word or NULL
* rest -- set to everything following a space, or NULL if no space
* %RETURNS:
* Nothing
* %DESCRIPTION:
* Splits a line on whitespace.
***********************************************************************/
void
split_on_space3(char *buf,
char **first,
char **second,
char **rest)
{
*first = buf;
*second = NULL;
*rest = NULL;
while(*buf && !isspace(*buf)) buf++;
if (*buf && isspace(*buf)) {
*buf = 0;
*second = buf+1;
buf++;
while(*buf && !isspace(*buf)) buf++;
if (*buf && isspace(*buf)) {
*buf = 0;
*rest = buf+1;
}
}
}
/**********************************************************************
* %FUNCTION: split_on_space4
* %ARGUMENTS:
* buf -- input buffer
* first -- set to first word
* second -- set to second word or NULL
* third -- set to third word or NULL
* rest -- set to everything following a space, or NULL if no space
* %RETURNS:
* Nothing
* %DESCRIPTION:
* Splits a line on whitespace.
***********************************************************************/
void
split_on_space4(char *buf,
char **first,
char **second,
char **third,
char **rest)
{
*first = buf;
*second = NULL;
*third = NULL;
*rest = NULL;
while(*buf && !isspace(*buf)) buf++;
if (*buf && isspace(*buf)) {
*buf = 0;
*second = buf+1;
buf++;
while(*buf && !isspace(*buf)) buf++;
if (*buf && isspace(*buf)) {
*buf = 0;
*third = buf+1;
buf++;
while(*buf && !isspace(*buf)) buf++;
if (*buf && isspace(*buf)) {
*buf = 0;
*rest = buf+1;
}
}
}
}
#ifndef ENABLE_DEBUGGING
/**********************************************************************
* %FUNCTION: malloc_with_log
* %ARGUMENTS:
* size -- amount of memory to allocate
* %RETURNS:
* Allocated memory
* %DESCRIPTION:
* Calls malloc, but syslogs an error on failure to allocate memory.
***********************************************************************/
void *
malloc_with_log(size_t s)
{
void *p = malloc(s);
if (!p) {
syslog(LOG_WARNING, "Failed to allocate %lu bytes of memory",
(unsigned long) s);
}
return p;
}
/**********************************************************************
* %FUNCTION: strdup_with_log
* %ARGUMENTS:
* s -- string to strdup
* %RETURNS:
* A copy of s in malloc'd memory.
* %DESCRIPTION:
* Calls strdup, but syslogs an error on failure to allocate memory.
***********************************************************************/
char *
strdup_with_log(char const *s)
{
char *p = strdup(s);
if (!p) {
syslog(LOG_WARNING, "Failed to allocate %d bytes of memory in strdup",
(int) strlen(s)+1);
}
return p;
}
#endif
/**********************************************************************
*%FUNCTION: chomp
*%ARGUMENTS:
* str -- a string
*%RETURNS:
* Nothing
*%DESCRIPTION:
* Removes newlines and carriage-returns (if any) from str
***********************************************************************/
void
chomp(char *str)
{
char *s, *t;
s = str;
for (t=str; *t; t++) {
if (*t == '\n' || *t == '\r') continue;
*s++ = *t;
}
*s = 0;
}
/**********************************************************************
* %FUNCTION: MXCommand
* %ARGUMENTS:
* sockname -- multiplexor socket name
* cmd -- command to send
* buf -- buffer for reply
* len -- length of buffer
* qid -- Sendmail queue identifier
* %RETURNS:
* 0 if all went well, -1 on error.
* %DESCRIPTION:
* Sends a command to the multiplexor and reads the answer back.
***********************************************************************/
int
MXCommand(char const *sockname,
char const *cmd,
char *buf,
int len,
char const *qid)
{
int fd;
struct sockaddr_un addr;
int nread;
int n;
if (!qid || !*qid) {
qid = "NOQUEUE";
}
fd = socket(AF_LOCAL, SOCK_STREAM, 0);
if (fd < 0) {
syslog(LOG_ERR, "%s: MXCommand: socket: %m", qid);
return MD_TEMPFAIL;
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_LOCAL;
strncpy(addr.sun_path, sockname, sizeof(addr.sun_path) - 1);
if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
syslog(LOG_ERR, "%s: MXCommand: connect: %m: Is multiplexor running?", qid);
close(fd);
return MD_TEMPFAIL;
}
n = writestr(fd, cmd);
if (n < 0) {
syslog(LOG_ERR, "%s: MXCommand: write: %m: Is multiplexor running?", qid);
close(fd);
return MD_TEMPFAIL;
}
/* Now read the answer */
nread = readn(fd, buf, len-1);
if (nread < 0) {
syslog(LOG_ERR, "%s: MXCommand: read: %m: Is multiplexor running?", qid);
close(fd);
return MD_TEMPFAIL;
}
buf[nread] = 0;
/* If we read a full buffer, read to EOF to maintain synchronizaion */
if (nread == len-1) {
char slop[SMALLBUF];
if (readn(fd, slop, SMALLBUF) > 0) {
syslog(LOG_WARNING, "%s: MXCommand: Overlong reply from multiplexor was truncated!", qid);
/* Read all the way to EOF */
while (readn(fd, slop, SMALLBUF) > 0);
}
}
close(fd);
return 0;
}
/**********************************************************************
* %FUNCTION: MXCheckFreeWorkers
* %ARGUMENTS:
* sockname -- MX socket name
* %RETURNS:
* >0 if there are free workers, 0 if all workers are busy, -1 if there
* was an error.
* %DESCRIPTION:
* Queries multiplexor for number of free workers.
***********************************************************************/
int
MXCheckFreeWorkers(char const *sockname, char const *qid)
{
char ans[SMALLBUF];
int workers;
if (MXCommand(sockname, "free\n", ans, SMALLBUF-1, qid) < 0) return MD_TEMPFAIL;
if (sscanf(ans, "%d", &workers) != 1) return MD_TEMPFAIL;
return workers;
}
/**********************************************************************
* %FUNCTION: MXScanDir
* %ARGUMENTS:
* sockname -- MX socket name
* qid -- Sendmail queue ID
* dir -- directory to scan
* %RETURNS:
* 0 if scanning succeeded; -1 if there was an error.
* %DESCRIPTION:
* Asks multiplexor to initiate a scan.
***********************************************************************/
int
MXScanDir(char const *sockname,
char const *qid,
char const *dir)
{
char cmd[SMALLBUF];
char ans[SMALLBUF];
int len;
if (!qid || !*qid) {
qid = "NOQUEUE";
}
if (percent_encode_command(1, cmd, sizeof(cmd), "scan", qid, dir, NULL) < 0) {
return MD_TEMPFAIL;
}
if (MXCommand(sockname, cmd, ans, SMALLBUF-1, qid) < 0) return MD_TEMPFAIL;
if (!strcmp(ans, "ok\n")) return 0;
len = strlen(ans);
if (len > 0 && ans[len-1] == '\n') ans[len-1] = 0;
syslog(LOG_ERR, "%s: Error from multiplexor: %s", qid, ans);
return MD_TEMPFAIL;
}
/**********************************************************************
* %FUNCTION: percent_decode
* %ARGUMENTS:
* buf -- a buffer with percent-encoded data
* %RETURNS:
* Nothing
* %DESCRIPTION:
* Decodes buf IN PLACE.
***********************************************************************/
void
percent_decode(char *buf)
{
unsigned char *in = (unsigned char *) buf;
unsigned char *out = (unsigned char *) buf;
unsigned int val;
if (!buf) {
return;
}
while(*in) {
if (*in == '%' && isxdigit(*(in+1)) && isxdigit(*(in+2))) {
sscanf((char *) in+1, "%2x", &val);
*out++ = (unsigned char) val;
in += 3;
continue;
}
*out++ = *in++;
}
/* Copy terminator */
*out = 0;
}
/**********************************************************************
* %FUNCTION: percent_encode
* %ARGUMENTS:
* in -- input buffer to encode
* out -- output buffer to place encoded data
* outlen -- number of chars in output buffer.
* %RETURNS:
* Number of chars written, not including trailing NULL. Ranges from
* 0 to outlen-1
* %DESCRIPTION:
* Encodes "in" into "out", writing at most (outlen-1) chars. Then writes
* trailing 0.
***********************************************************************/
int
percent_encode(char const *in,
char *out,
int outlen)
{
unsigned char tmp[8];
int nwritten = 0;
unsigned char c;
unsigned char const *uin = (unsigned char const *) in;
unsigned char *uout = (unsigned char *) out;
if (outlen <= 0) {
return 0;
}
if (outlen == 1) {
*uout = 0;
return 0;
}
/* Do real work */
while((c = *uin++) != 0) {
if (c <= 32 || c > 126 || c == '%' || c == '\\' || c == '\'' || c == '"') {
if (nwritten >= outlen-3) {
break;
}
sprintf((char *) tmp, "%%%02X", (unsigned int) c);
*uout++ = tmp[0];
*uout++ = tmp[1];
*uout++ = tmp[2];
nwritten += 3;
} else {
*uout++ = c;
nwritten++;
}
if (nwritten >= outlen-1) {
break;
}
}
*uout = 0;
return nwritten;
}
/**********************************************************************
* %FUNCTION: percent_encode_command
* %ARGUMENTS:
* term_with_newline -- if true, terminate with "\n\0". Otherwise, just "\0"
* out -- output buffer
* outlen -- length of output buffer
* args -- arguments. Each one is percent-encoded and space-separated from
* previous.
* %RETURNS:
* 0 if everything fits; -1 otherwise.
* %DESCRIPTION:
* Writes a series of space-separated, percent-encoded words to a buffer.
***********************************************************************/
static int
percent_encode_command(int term_with_newline, char *out, int outlen, ...)
{
va_list ap;
int spaceleft = outlen-2;
int first = 1;
int len;
char *arg;
if (outlen < 2) return MD_TEMPFAIL;
va_start(ap, outlen);
while ((arg = va_arg(ap, char *)) != NULL) {
if (first) {
first = 0;
} else {
if (spaceleft <= 0) {
va_end(ap);
return MD_TEMPFAIL;
}
*out++ = ' ';
spaceleft--;
}
len = percent_encode(arg, out, spaceleft);
spaceleft -= len;
out += len;
}
va_end(ap);
if (term_with_newline) *out++ = '\n';
*out = 0;
return 0;
}
/**********************************************************************
* %FUNCTION: munch_mx_return
* %ARGUMENTS:
* ans -- answer from multiplexor
* msg -- buffer for holding error message, at least SMALLBUF chars
* qid -- Sendmail queue ID
* %RETURNS:
* 1 if it's OK to accept connections from this host; 0 if not, -1 if error.
* If connection is rejected, error message *may* be set.
***********************************************************************/
static int
munch_mx_return(char *ans, char *msg, char const *qid)
{
size_t len;
if (!qid || !*qid) {
qid = "NOQUEUE";
}
if (!strcmp(ans, "ok -1\n")) return MD_TEMPFAIL;
if (!strcmp(ans, "ok 1\n")) return MD_CONTINUE;
if (!strcmp(ans, "ok 2\n")) return MD_ACCEPT_AND_NO_MORE_FILTERING;
if (!strcmp(ans, "ok 3\n")) return MD_DISCARD;
if (!strcmp(ans, "ok 0\n")) return MD_REJECT;
chomp(ans);
/* If rejection message is supplied, set failure code and return 0 */
len = strlen(ans);
if (len >= 6 && !strncmp(ans, "ok 0 ", 5)) {
strcpy(msg, ans+5);
return MD_REJECT;
}
if (len >= 7 && !strncmp(ans, "ok -1 ", 6)) {
strcpy(msg, ans+6);
return MD_TEMPFAIL;
}
if (len >= 6 && !strncmp(ans, "ok 1 ", 5)) {
strcpy(msg, ans+5);
return MD_CONTINUE;
}
if (len >= 6 && !strncmp(ans, "ok 2 ", 5)) {
strcpy(msg, ans+5);
return MD_ACCEPT_AND_NO_MORE_FILTERING;
}
if (len >= 6 && !strncmp(ans, "ok 3 ", 5)) {
strcpy(msg, ans+5);
return MD_DISCARD;
}
if (len > 0 && ans[len-1] == '\n') ans[len-1] = 0;
syslog(LOG_ERR, "%s: Error from multiplexor: %s", qid, ans);
return MD_TEMPFAIL;
}
/**********************************************************************
* %FUNCTION: MXRelayOK
* %ARGUMENTS:
* sockname -- multiplexor socket name
* msg -- buffer for holding error message, at least SMALLBUF chars
* ip -- relay IP address
* name -- relay name
* port -- client port
* myip -- My IP address, if known.
* daemon_port -- Listening port
* %RETURNS:
* 1 if it's OK to accept connections from this host; 0 if not, -1 if error.
* If connection is rejected, error message *may* be set.
***********************************************************************/
int
MXRelayOK(char const *sockname,
char *msg,
char const *ip,
char const *name,
unsigned int port,
char const *myip,
unsigned int daemon_port)
{
char cmd[SMALLBUF];
char ans[SMALLBUF];
char port_string[65];
char daemon_port_string[65];
snprintf(port_string, sizeof(port_string), "%u", port);
snprintf(daemon_port_string, sizeof(daemon_port_string), "%u", daemon_port);
*msg = 0;
if (!ip || !*ip) {
ip = "UNKNOWN";
}
if (!name || !*name) {
name = ip;
}
if (!myip || !*myip) {
myip = "UNKNOWN";
}
if (percent_encode_command(1, cmd, sizeof(cmd), "relayok", ip, name, port_string, myip, daemon_port_string, NULL) < 0) {
return MD_TEMPFAIL;
}
if (MXCommand(sockname, cmd, ans, SMALLBUF-1, NULL) < 0) return MD_TEMPFAIL;
return munch_mx_return(ans, msg, NULL);
}
/**********************************************************************
* %FUNCTION: MXHeloOK
* %ARGUMENTS:
* sockname -- multiplexor socket name
* msg -- buffer for holding error message, at least SMALLBUF chars
* ip -- IP address of client
* name -- resolved name of client
* helo -- the helo string
* port -- client port
* myip -- My IP address, if known.
* daemon_port -- Listening port
* %RETURNS:
* 1 if it's OK to accept messages from this sender; 0 if not, -1 if error or
* we should tempfail.
***********************************************************************/
int
MXHeloOK(char const *sockname,
char *msg,
char const *ip,
char const *name,
char const *helo,
unsigned int port,
char const *myip,
unsigned int daemon_port)
{
char cmd[SMALLBUF];
char ans[SMALLBUF];
char port_string[65];
char daemon_port_string[65];
snprintf(port_string, sizeof(port_string), "%u", port);
snprintf(daemon_port_string, sizeof(daemon_port_string), "%u", daemon_port);
*msg = 0;
if (!ip || !*ip) {
ip = "UNKNOWN";
}
if (!name || !*name) {
name = ip;
}
if (!helo) {
helo = "UNKNOWN";
}
if (!myip || !*myip) {
myip = "UNKNOWN";
}
if (percent_encode_command(1, cmd, sizeof(cmd), "helook", ip, name, helo, port_string, myip, daemon_port_string, NULL) < 0) {
return MD_TEMPFAIL;
}
if (MXCommand(sockname, cmd, ans, SMALLBUF-1, NULL) < 0) return MD_TEMPFAIL;
return munch_mx_return(ans, msg, NULL);
}
/**********************************************************************
* %FUNCTION: MXSenderOK
* %ARGUMENTS:
* sockname -- socket name
* msg -- buffer of at least SMALLBUF size for error message
* sender_argv -- args from sendmail. sender_argv[0] is sender; rest are
* ESMTP args.
* ip -- sending relay's IP address
* name -- sending relay's host name
* helo -- argument to "HELO/EHLO" (may be NULL)
* dir -- MIMEDefang working directory
* qid -- Sendmail queue identifier
* %RETURNS:
* 1 if it's OK to accept messages from this sender; 0 if not, -1 if error or
* we should tempfail.
* If message is rejected, error message *may* be set.
***********************************************************************/
int
MXSenderOK(char const *sockname,
char *msg,
char const **sender_argv,
char const *ip,
char const *name,
char const *helo,
char const *dir,
char const *qid)
{
char cmd[SMALLBUF];
char ans[SMALLBUF];
int l, l2, i;
char const *sender = sender_argv[0];
*msg = 0;
if (!sender || !*sender) {
sender = "UNKNOWN";
}
if (!ip || !*ip) {
ip = "UNKNOWN";
}
if (!name || !*name) {
name = ip;
}
if (!helo) {
helo = "UNKNOWN";
}
if (percent_encode_command(0, cmd, sizeof(cmd)-1, "senderok", sender, ip,
name,
helo, dir, qid, NULL) < 0) {
return MD_TEMPFAIL;
}
/* Append ESMTP args */
l = strlen(cmd);
for (i=1; sender_argv[i]; i++) {
percent_encode(sender_argv[i],
ans,
sizeof(ans));
l2 = strlen(ans) + 1;
if (l + l2 < sizeof(cmd)-1) {
strcat(cmd, " ");
strcat(cmd, ans);
l += l2;
} else {
break;
}
}
/* Add newline */
strcat(cmd, "\n");
if (MXCommand(sockname, cmd, ans, SMALLBUF-1, qid) < 0) return MD_TEMPFAIL;
return munch_mx_return(ans, msg, qid);
}
/**********************************************************************
* %FUNCTION: MXRecipientOK
* %ARGUMENTS:
* sockname -- multiplexor socket name
* msg -- buffer of at least SMALLBUF size for error messages
* recip_argv -- recipient e-mail address and ESMTP args
* sender -- sender's e-mail address
* ip -- sending relay's IP address
* name -- sending relay's host name
* firstRecip -- first recipient of the message
* helo -- argument to "HELO/EHLO" (may be NULL)
* dir -- MIMEDefang working directory
* qid -- Sendmail queue identifier
* rcpt_mailer -- the "mailer" part of the triple for RCPT TO address
* rcpt_host -- the "host" part of the triple for RCPT TO address
* rcpt_addr -- the "addr" part of the triple for RCPT TO address
* %RETURNS:
* 1 if it's OK to accept messages to this recipient; 0 if not, -1 if error.
* If recipient is rejected, error message *may* be set.
***********************************************************************/
int
MXRecipientOK(char const *sockname,
char *msg,
char const **recip_argv,
char const *sender,
char const *ip,
char const *name,
char const *firstRecip,
char const *helo,
char const *dir,
char const *qid,
char const *rcpt_mailer,
char const *rcpt_host,
char const *rcpt_addr)
{
char cmd[SMALLBUF];
char ans[SMALLBUF];
int i, l, l2;
char const *recipient = recip_argv[0];
*msg = 0;
if (!recipient || !*recipient) {
recipient = "UNKNOWN";
}
if (!sender || !*sender) {
sender = "UNKNOWN";
}
if (!ip || !*ip) {
ip = "UNKNOWN";
}
if (!name || !*name) {
name = ip;
}
if (!firstRecip || !*firstRecip) {
firstRecip = "UNKNOWN";
}
if (!helo) {
helo = "UNKNOWN";
}
if (percent_encode_command(0, cmd, sizeof(cmd),
"recipok", recipient, sender, ip, name, firstRecip,
helo, dir, qid, rcpt_mailer, rcpt_host, rcpt_addr,
NULL) < 0) {
return MD_TEMPFAIL;
}
/* Append ESMTP args */
l = strlen(cmd);
for (i=1; recip_argv[i]; i++) {
percent_encode(recip_argv[i],
ans,
sizeof(ans));
l2 = strlen(ans) + 1;
if (l + l2 < sizeof(cmd)-1) {
strcat(cmd, " ");
strcat(cmd, ans);
l += l2;
} else {
break;
}
}
/* Add newline */
strcat(cmd, "\n");
if (MXCommand(sockname, cmd, ans, SMALLBUF-1, qid) < 0) return MD_TEMPFAIL;
return munch_mx_return(ans, msg, qid);
}
/**********************************************************************
* %FUNCTION: writen
* %ARGUMENTS:
* fd -- file to write to
* buf -- buffer to write
* len -- length to write
* %RETURNS:
* Number of bytes written, or -1 on error
* %DESCRIPTION:
* Writes exactly "len" bytes from "buf" to file descriptor fd
***********************************************************************/
int
writen(int fd,
char const *buf,
size_t len)
{
int r;
int nleft = len;
while(nleft) {
r = write(fd, buf, nleft);
if (r > 0) {
nleft -= r;
buf += r;
continue;
}
if (r == 0) {
/* Shouldn't happen! */
errno = EIO;
return MD_TEMPFAIL;
}
if (r < 0) {
if (errno == EINTR || errno == EAGAIN) {
continue;
}
}
return r;
}
return len;
}
/**********************************************************************
* %FUNCTION: writestr
* %ARGUMENTS:
* fd -- file to write to
* buf -- null-terminated string to write
* %RETURNS:
* Number of bytes written, or -1 on error
* %DESCRIPTION:
* Writes the string in "buf" to fd.
***********************************************************************/
int
writestr(int fd,
char const *buf)
{
return writen(fd, buf, strlen(buf));
}
/**********************************************************************
* %FUNCTION: readn
* %ARGUMENTS:
* fd -- file descriptor to read from
* buf -- buffer to read into
* count -- number of bytes to read
* %RETURNS:
* The number of bytes actually read, or -1 on error
* %DESCRIPTION:
* Attempts to read exactly "count" bytes from a descriptor.
***********************************************************************/
int
readn(int fd, void *buf, size_t count)
{
size_t num_read = 0;
char *c = (char *) buf;
while (count) {
int n = read(fd, c, count);
if (n == 0) { /* EOF */
return num_read;
}
if (n < 0) { /* Error */
if (errno == EINTR || errno == EAGAIN) {
continue;
}
return n;
}
num_read += n;
count -= n;
c += n;
}
return num_read;
}
/**********************************************************************
* %FUNCTION: closefd
* %ARGUMENTS:
* fd -- file to close
* %RETURNS:
* Whatever close(2) returns
* %DESCRIPTION:
* Closes fd, handling EINTR
***********************************************************************/
int
closefd(int fd)
{
int r;
while(1) {
r = close(fd);
if (r >= 0) return r;
if (errno != EINTR) return r;
}
}
/**********************************************************************
* %FUNCTION: validate_smtp_code
* %ARGUMENTS:
* code -- an SMTP code (eg 451)
* first -- what the first char must be
* %RETURNS:
* 1 if it's a valid code; 0 otherwise. A valid code consists
* of three decimal digits only. The first digit must match "first".
***********************************************************************/
int
validate_smtp_code(char const *code,
char const *first)
{
if (!code) return 0;
if (*code != *first) return 0;
if (!isdigit(*(code+1))) return 0;
if (!isdigit(*(code+2))) return 0;
if (*(code+3)) return 0;
return 1;
}
/**********************************************************************
* %FUNCTION: validate_smtp_dsn
* %ARGUMENTS:
* dsn -- an SMTP dsn reply (eg 4.7.1)
* first -- what the first char must be
* %RETURNS:
* 1 if it's a valid dsn; 0 otherwise. A valid DSN consits of three
* numerical fields separated by periods. The first field must be a
* single digit that matches "first". The second and
* third fields can be 1-3 digits long each.
***********************************************************************/
int
validate_smtp_dsn(char const *dsn,
char const *first)
{
char const *s;
int count;
if (!dsn) return 0;