-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiltertest.c
More file actions
4468 lines (3698 loc) · 83.2 KB
/
Copy pathmiltertest.c
File metadata and controls
4468 lines (3698 loc) · 83.2 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
/*
** Copyright (c) 2009-2014, The Trusted Domain Project. All rights reserved.
*/
#include "build-config.h"
/* system includes */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <sys/uio.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sysexits.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <unistd.h>
#include <netdb.h>
#include <time.h>
/* libmilter includes */
#include <libmilter/mfapi.h>
#ifndef SMFI_PROT_VERSION
# define SMFI_PROT_VERSION SMFI_VERSION
#endif /* ! SMFI_PROT_VERSION */
/* libbsd if found */
#ifdef USE_BSD_H
# include <bsd/string.h>
#endif /* USE_BSD_H */
/* libstrl if needed */
#ifdef USE_STRL_H
# include <strl.h>
#endif /* USE_STRL_H */
/* Lua includes */
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
/* types */
#ifndef HAVE_USECONDS_T
typedef unsigned int useconds_t;
#endif /* ! HAVE_USECONDS_T */
/* macros */
#ifndef FALSE
# define FALSE 0
#endif /* ! FALSE */
#ifndef TRUE
# define TRUE 1
#endif /* ! TRUE */
#ifdef SMFIP_NODATA
# define CHECK_MPOPTS(c,o) (((c)->ctx_mpopts & (o)) != 0)
#else /* SMFIP_NODATA */
# define CHECK_MPOPTS(c,o) 0
#endif /* SMFIP_NODATA */
#ifndef SMFIP_NR_CONN
# define SMFIP_NR_CONN 0
# define SMFIP_NR_HELO 0
# define SMFIP_NR_MAIL 0
# define SMFIP_NR_RCPT 0
# define SMFIP_NR_DATA 0
# define SMFIP_NR_HDR 0
# define SMFIP_NR_EOH 0
# define SMFIP_NR_BODY 0
# define SMFIP_NR_UNKN 0
#endif /* SMFIP_NR_CONN */
#define MT_PRODUCT "OpenDKIM milter test facility"
#define MT_VERSION "1.6.0"
#define BUFRSZ 1024
#define CHUNKSZ 65536
#define CMDLINEOPTS "D:s:uvVw"
#define DEFBODY "Dummy message body.\r\n"
#define DEFCLIENTPORT 12345
#define DEFCLIENTHOST "test.example.com"
#define DEFCLIENTIP "12.34.56.78"
#define DEFHEADERNAME "From"
#define DEFSENDER "<sender@example.com>"
#define DEFTIMEOUT 10
#define DEFRECIPIENT "<recipient@example.com>"
#define STATE_UNKNOWN (-1)
#define STATE_INIT 0
#define STATE_NEGOTIATED 1
#define STATE_CONNINFO 2
#define STATE_HELO 3
#define STATE_ENVFROM 4
#define STATE_ENVRCPT 5
#define STATE_DATA 6
#define STATE_HEADER 7
#define STATE_EOH 8
#define STATE_BODY 9
#define STATE_EOM 10
#define STATE_DEAD 99
#define MT_HDRADD 1
#define MT_HDRINSERT 2
#define MT_HDRCHANGE 3
#define MT_HDRDELETE 4
#define MT_RCPTADD 5
#define MT_RCPTDELETE 6
#define MT_BODYCHANGE 7
#define MT_QUARANTINE 8
#define MT_SMTPREPLY 9
/* prototypes */
int mt_abort(lua_State *);
int mt_bodyfile(lua_State *);
int mt_bodyrandom(lua_State *);
int mt_bodystring(lua_State *);
int mt_chdir(lua_State *);
int mt_connect(lua_State *);
int mt_conninfo(lua_State *);
int mt_data(lua_State *);
int mt_disconnect(lua_State *);
int mt_echo(lua_State *);
int mt_eoh(lua_State *);
int mt_eom(lua_State *);
int mt_eom_check(lua_State *);
int mt_getcwd(lua_State *);
int mt_getheader(lua_State *);
int mt_getreply(lua_State *);
int mt_header(lua_State *);
int mt_helo(lua_State *);
int mt_macro(lua_State *);
int mt_mailfrom(lua_State *);
int mt_negotiate(lua_State *);
int mt_rcptto(lua_State *);
int mt_set_timeout(lua_State *);
int mt_signal(lua_State *);
int mt_sleep(lua_State *);
int mt_startfilter(lua_State *);
int mt_test_action(lua_State *);
int mt_test_option(lua_State *);
int mt_unknown(lua_State *);
/* data types */
struct mt_eom_request
{
char eom_request; /* request code */
size_t eom_rlen; /* request length */
char * eom_rdata; /* request data */
struct mt_eom_request * eom_next; /* next request */
};
struct mt_context
{
char ctx_response; /* milter response code */
char ctx_smtp_reply[BUFRSZ]; /* last SMFIR_REPLYCODE data */
int ctx_fd; /* descriptor */
int ctx_state; /* current state */
unsigned long ctx_mactions; /* requested actions */
unsigned long ctx_mpopts; /* requested protocol opts */
struct mt_eom_request * ctx_eomreqs; /* EOM requests */
};
struct mt_lua_io
{
_Bool lua_io_done;
size_t lua_io_scriptlen;
const char * lua_io_script;
};
static const luaL_Reg mt_library[] =
{
{ "abort", mt_abort },
{ "bodyfile", mt_bodyfile },
{ "bodyrandom", mt_bodyrandom },
{ "bodystring", mt_bodystring },
{ "chdir", mt_chdir },
{ "connect", mt_connect },
{ "conninfo", mt_conninfo },
{ "data", mt_data },
{ "disconnect", mt_disconnect },
{ "echo", mt_echo },
{ "eoh", mt_eoh },
{ "eom", mt_eom },
{ "eom_check", mt_eom_check },
{ "getcwd", mt_getcwd },
{ "getheader", mt_getheader },
{ "getreply", mt_getreply },
{ "header", mt_header },
{ "helo", mt_helo },
{ "macro", mt_macro },
{ "mailfrom", mt_mailfrom },
{ "negotiate", mt_negotiate },
{ "rcptto", mt_rcptto },
{ "set_timeout", mt_set_timeout },
{ "signal", mt_signal },
{ "sleep", mt_sleep },
{ "startfilter", mt_startfilter },
{ "test_action", mt_test_action },
{ "test_option", mt_test_option },
{ "unknown", mt_unknown },
{ NULL, NULL }
};
/* globals */
_Bool rusage;
_Bool nowait;
int verbose;
unsigned int tmo;
pid_t filterpid;
char scriptbuf[BUFRSZ];
char *progname;
/*
** MT_INET_NTOA -- thread-safe inet_ntoa()
**
** Parameters:
** a -- (struct in_addr) to be converted
** buf -- destination buffer
** buflen -- number of bytes at buf
**
** Return value:
** Size of the resultant string. If the result is greater than buflen,
** then buf does not contain the complete result.
*/
size_t
mt_inet_ntoa(struct in_addr a, char *buf, size_t buflen)
{
in_addr_t addr;
assert(buf != NULL);
addr = ntohl(a.s_addr);
return snprintf(buf, buflen, "%d.%d.%d.%d",
(addr >> 24), (addr >> 16) & 0xff,
(addr >> 8) & 0xff, addr & 0xff);
}
/*
** MT_LUA_READER -- "read" a script and make it available to Lua
**
** Parameters:
** l -- Lua state
** data -- pointer to a Lua I/O structure
** size -- size (returned)
**
** Return value:
** Pointer to the data.
*/
const char *
mt_lua_reader(lua_State *l, void *data, size_t *size)
{
struct mt_lua_io *io;
assert(l != NULL);
assert(data != NULL);
assert(size != NULL);
io = (struct mt_lua_io *) data;
if (io->lua_io_done)
{
*size = 0;
return NULL;
}
else if (io->lua_io_script != NULL)
{
io->lua_io_done = TRUE;
*size = io->lua_io_scriptlen;
return io->lua_io_script;
}
else
{
size_t rlen;
memset(scriptbuf, '\0', sizeof scriptbuf);
if (feof(stdin))
{
*size = 0;
io->lua_io_done = TRUE;
return NULL;
}
rlen = fread(scriptbuf, 1, sizeof scriptbuf, stdin);
*size = rlen;
return (const char *) scriptbuf;
}
}
/*
** MT_LUA_ALLOC -- allocate memory
**
** Parameters:
** ud -- context (not used)
** ptr -- pointer (for realloc())
** osize -- old size
** nsize -- new size
**
** Return value:
** Allocated memory, or NULL on failure.
*/
void *
mt_lua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
{
if (nsize == 0 && osize != 0)
{
free(ptr);
return NULL;
}
else if (nsize != 0 && osize == 0)
{
return malloc(nsize);
}
else
{
return realloc(ptr, nsize);
}
}
/*
** MT_FLUSH_EOMREQS -- free EOM requests
**
** Parameters:
** ctx -- mt_context handle
**
** Return value:
** None.
*/
void
mt_flush_eomreqs(struct mt_context *ctx)
{
struct mt_eom_request *r;
assert(ctx != NULL);
while (ctx->ctx_eomreqs != NULL)
{
r = ctx->ctx_eomreqs;
if (r->eom_rdata != NULL)
free(r->eom_rdata);
ctx->ctx_eomreqs = r->eom_next;
free(r);
}
}
/*
** MT_EOM_REQUEST -- record a request received during EOM
**
** Parameters:
** ctx -- mt_context handle
** cmd -- command received
** len -- length of data
** data -- data received (i.e. request parameters)
**
** Return value:
** TRUE iff addition was successful.
*/
_Bool
mt_eom_request(struct mt_context *ctx, char cmd, size_t len, char *data)
{
struct mt_eom_request *r;
assert(ctx != NULL);
r = (struct mt_eom_request *) malloc(sizeof *r);
if (r == NULL)
return FALSE;
r->eom_request = cmd;
r->eom_rlen = len;
r->eom_rdata = malloc(len);
if (r->eom_rdata == NULL)
{
free(r);
return FALSE;
}
memcpy(r->eom_rdata, data, len);
r->eom_next = ctx->ctx_eomreqs;
ctx->ctx_eomreqs = r;
return TRUE;
}
/*
** MT_MILTER_READ_HDR -- read the milter protocol header (cmd byte + payload length)
**
** Parameters:
** fd -- descriptor to read from
** cmd -- milter command received (returned)
** paylen -- payload byte count, not including cmd byte (returned)
**
** Return value:
** TRUE iff successful.
*/
static _Bool
mt_milter_read_hdr(int fd, char *cmd, size_t *paylen)
{
int i;
size_t rlen;
fd_set fds;
struct timeval timeout;
char data[MILTER_LEN_BYTES + 1];
assert(fd >= 0);
FD_ZERO(&fds);
FD_SET(fd, &fds);
timeout.tv_sec = tmo;
timeout.tv_usec = 0;
i = select(fd + 1, &fds, NULL, NULL, &timeout);
if (i == 0)
{
fprintf(stderr, "%s: select(): timeout on fd %d\n", progname,
fd);
return FALSE;
}
else if (i == -1)
{
fprintf(stderr, "%s: select(): fd %d: %s\n", progname, fd,
strerror(errno));
return FALSE;
}
rlen = read(fd, data, sizeof data);
if (rlen != sizeof data)
{
fprintf(stderr, "%s: read(%d): returned %ld, expected %ld\n",
progname, fd, (long) rlen, (long) sizeof data);
return FALSE;
}
*cmd = data[MILTER_LEN_BYTES];
data[MILTER_LEN_BYTES] = '\0';
(void) memcpy(&i, data, MILTER_LEN_BYTES);
*paylen = (size_t) (ntohl(i) - 1);
return TRUE;
}
/*
** MT_MILTER_READ -- read from a connected filter
**
** Parameters:
** fd -- descriptor to which to write
** cmd -- milter command received (returned)
** buf -- where to write data
** len -- bytes available at "buf" (updated with bytes written)
**
** Return value:
** TRUE iff successful.
*/
_Bool
mt_milter_read(int fd, char *cmd, const char *buf, size_t *len)
{
size_t paylen;
size_t rlen;
assert(fd >= 0);
if (!mt_milter_read_hdr(fd, cmd, &paylen))
return FALSE;
rlen = 0;
if (paylen > 0)
{
if (paylen > *len)
{
fprintf(stderr,
"%s: mt_milter_read(%d): response size %zu exceeds buffer %zu\n",
progname, fd, paylen, *len);
return FALSE;
}
rlen = read(fd, (void *) buf, paylen);
if (rlen != paylen)
{
fprintf(stderr,
"%s: read(%d): returned %ld, expected %ld\n",
progname, fd, (long) rlen, (long) paylen);
return FALSE;
}
}
if (verbose > 1)
{
fprintf(stdout, "%s: mt_milter_read(%d): cmd %c, len %ld\n",
progname, fd, *cmd, (long) rlen);
}
*len = rlen;
return TRUE;
}
/*
** MT_MILTER_WRITE -- write to a connected filter
**
** Parameters:
** fd -- descriptor to which to write
** cmd -- command to send (an SMFIC_* constant)
** buf -- command data (or NULL)
** len -- length of data at "buf"
**
** Return value:
** TRUE iff successful.
*/
_Bool
mt_milter_write(int fd, int cmd, const char *buf, size_t len)
{
char command = (char) cmd;
ssize_t sl, i;
int num_vectors;
uint32_t nl;
char data[MILTER_LEN_BYTES + 1];
struct iovec vector[2];
assert(fd >= 0);
if (verbose > 1)
{
fprintf(stdout, "%s: mt_milter_write(%d): cmd %c, len %ld\n",
progname, fd, command, (long) len);
}
nl = htonl(len + 1);
(void) memcpy(data, (char *) &nl, MILTER_LEN_BYTES);
data[MILTER_LEN_BYTES] = command;
sl = MILTER_LEN_BYTES + 1;
/* set up the vector for the size / command */
vector[0].iov_base = (void *) data;
vector[0].iov_len = sl;
/*
** Determine if there is command data. If so, there will be two
** vectors. If not, there will be only one. The vectors are set
** up here and 'num_vectors' and 'sl' are set appropriately.
*/
if (len <= 0 || buf == NULL)
{
num_vectors = 1;
}
else
{
num_vectors = 2;
sl += len;
vector[1].iov_base = (void *) buf;
vector[1].iov_len = len;
}
/* write the vector(s) */
i = writev(fd, vector, num_vectors);
if (i != sl)
{
fprintf(stderr, "%s: writev(%d): returned %ld, expected %ld\n",
progname, fd, (long) i, (long) sl);
}
return (i == sl);
}
/*
** MT_ASSERT_STATE -- bring a connection up to a given state
**
** Parameters:
** ctx -- miltertest context
** state -- desired state
**
** Return value:
** TRUE if successful, FALSE otherwise.
*/
_Bool
mt_assert_state(struct mt_context *ctx, int state)
{
size_t len;
size_t s;
uint16_t port;
char buf[BUFRSZ];
assert(ctx != NULL);
if (state >= STATE_NEGOTIATED && ctx->ctx_state < STATE_NEGOTIATED)
{
char rcmd;
size_t buflen;
uint32_t mta_version;
uint32_t mta_protoopts;
uint32_t mta_actions;
uint32_t nvers;
uint32_t npopts;
uint32_t nacts;
buflen = sizeof buf;
mta_version = SMFI_PROT_VERSION;
mta_protoopts = SMFI_CURR_PROT;
mta_actions = SMFI_CURR_ACTS;
nvers = htonl(mta_version);
nacts = htonl(mta_actions);
npopts = htonl(mta_protoopts);
(void) memcpy(buf, (char *) &nvers, MILTER_LEN_BYTES);
(void) memcpy(buf + MILTER_LEN_BYTES,
(char *) &nacts, MILTER_LEN_BYTES);
(void) memcpy(buf + (MILTER_LEN_BYTES * 2),
(char *) &npopts, MILTER_LEN_BYTES);
if (!mt_milter_write(ctx->ctx_fd, SMFIC_OPTNEG, buf,
MILTER_OPTLEN))
return FALSE;
if (!mt_milter_read(ctx->ctx_fd, &rcmd, buf, &buflen))
return FALSE;
if (rcmd != SMFIC_OPTNEG)
{
if (verbose > 0)
{
fprintf(stdout,
"%s: filter returned status %d to option negotiation on fd %d\n",
progname, rcmd, ctx->ctx_fd);
}
ctx->ctx_state = STATE_DEAD;
return FALSE;
}
/* decode and store requested protocol steps and actions */
(void) memcpy((char *) &nvers, buf, MILTER_LEN_BYTES);
(void) memcpy((char *) &nacts, buf + MILTER_LEN_BYTES,
MILTER_LEN_BYTES);
(void) memcpy((char *) &npopts, buf + (MILTER_LEN_BYTES * 2),
MILTER_LEN_BYTES);
ctx->ctx_mactions = ntohl(nacts);
ctx->ctx_mpopts = ntohl(npopts);
ctx->ctx_state = STATE_NEGOTIATED;
}
if (state >= STATE_CONNINFO && ctx->ctx_state < STATE_CONNINFO)
{
if (!CHECK_MPOPTS(ctx, SMFIP_NOCONNECT))
{
char rcmd;
size_t buflen;
buflen = sizeof buf;
port = htons(DEFCLIENTPORT);
len = strlcpy(buf, DEFCLIENTHOST, sizeof buf);
buf[len++] = '\0';
buf[len++] = '4'; /* IPv4 only for now */
memcpy(&buf[len], &port, sizeof port);
len += sizeof port;
memcpy(&buf[len], DEFCLIENTIP,
strlen(DEFCLIENTIP) + 1);
s = len + strlen(DEFCLIENTIP) + 1;
if (!mt_milter_write(ctx->ctx_fd, SMFIC_CONNECT,
buf, s))
return FALSE;
rcmd = SMFIR_CONTINUE;
if (!CHECK_MPOPTS(ctx, SMFIP_NR_CONN))
{
if (!mt_milter_read(ctx->ctx_fd, &rcmd,
buf, &buflen))
return FALSE;
ctx->ctx_response = rcmd;
if (rcmd == SMFIR_REPLYCODE)
strlcpy(ctx->ctx_smtp_reply, buf,
sizeof ctx->ctx_smtp_reply);
else
ctx->ctx_smtp_reply[0] = '\0';
}
if (rcmd != SMFIR_CONTINUE)
{
if (verbose > 0)
{
fprintf(stdout,
"%s: filter returned status %d to connection information on fd %d\n",
progname, rcmd, ctx->ctx_fd);
}
ctx->ctx_state = STATE_DEAD;
}
}
ctx->ctx_state = STATE_CONNINFO;
}
if (state >= STATE_HELO && ctx->ctx_state < STATE_HELO)
{
if (!CHECK_MPOPTS(ctx, SMFIP_NOHELO))
{
char rcmd;
size_t buflen;
buflen = sizeof buf;
len = strlcpy(buf, DEFCLIENTHOST, sizeof buf);
buf[len++] = '\0';
if (!mt_milter_write(ctx->ctx_fd, SMFIC_HELO,
buf, len))
return FALSE;
rcmd = SMFIR_CONTINUE;
if (!CHECK_MPOPTS(ctx, SMFIP_NR_HELO))
{
if (!mt_milter_read(ctx->ctx_fd, &rcmd,
buf, &buflen))
return FALSE;
ctx->ctx_response = rcmd;
if (rcmd == SMFIR_REPLYCODE)
strlcpy(ctx->ctx_smtp_reply, buf,
sizeof ctx->ctx_smtp_reply);
else
ctx->ctx_smtp_reply[0] = '\0';
}
if (rcmd != SMFIR_CONTINUE)
{
if (verbose > 0)
{
fprintf(stdout,
"%s: filter returned status %d to HELO on fd %d\n",
progname, rcmd, ctx->ctx_fd);
}
ctx->ctx_state = STATE_DEAD;
}
}
ctx->ctx_state = STATE_HELO;
}
if (state >= STATE_ENVFROM && ctx->ctx_state < STATE_ENVFROM)
{
if (!CHECK_MPOPTS(ctx, SMFIP_NOMAIL))
{
char rcmd;
size_t buflen;
buflen = sizeof buf;
len = strlcpy(buf, DEFSENDER, sizeof buf);
buf[len++] = '\0';
if (!mt_milter_write(ctx->ctx_fd, SMFIC_MAIL,
buf, len))
return FALSE;
rcmd = SMFIR_CONTINUE;
if (!CHECK_MPOPTS(ctx, SMFIP_NR_MAIL))
{
if (!mt_milter_read(ctx->ctx_fd, &rcmd,
buf, &buflen))
return FALSE;
ctx->ctx_response = rcmd;
if (rcmd == SMFIR_REPLYCODE)
strlcpy(ctx->ctx_smtp_reply, buf,
sizeof ctx->ctx_smtp_reply);
else
ctx->ctx_smtp_reply[0] = '\0';
}
if (rcmd != SMFIR_CONTINUE)
{
if (verbose > 0)
{
fprintf(stdout,
"%s: filter returned status %d to MAIL on fd %d\n",
progname, rcmd, ctx->ctx_fd);
}
ctx->ctx_state = STATE_DEAD;
}
}
ctx->ctx_state = STATE_ENVFROM;
}
if (state >= STATE_ENVRCPT && ctx->ctx_state < STATE_ENVRCPT)
{
if (!CHECK_MPOPTS(ctx, SMFIP_NORCPT))
{
char rcmd;
size_t buflen;
buflen = sizeof buf;
len = strlcpy(buf, DEFRECIPIENT, sizeof buf);
buf[len++] = '\0';
if (!mt_milter_write(ctx->ctx_fd, SMFIC_RCPT,
buf, len))
return FALSE;
rcmd = SMFIR_CONTINUE;
if ((ctx->ctx_mpopts & SMFIP_NR_RCPT) == 0)
{
if (!mt_milter_read(ctx->ctx_fd, &rcmd,
buf, &buflen))
return FALSE;
ctx->ctx_response = rcmd;
if (rcmd == SMFIR_REPLYCODE)
strlcpy(ctx->ctx_smtp_reply, buf,
sizeof ctx->ctx_smtp_reply);
else
ctx->ctx_smtp_reply[0] = '\0';
}
if (rcmd != SMFIR_CONTINUE)
{
if (verbose > 0)
{
fprintf(stdout,
"%s: filter returned status %d to RCPT on fd %d\n",
progname, rcmd, ctx->ctx_fd);
}
ctx->ctx_state = STATE_DEAD;
}
}
ctx->ctx_state = STATE_ENVRCPT;
}
if (state >= STATE_DATA && ctx->ctx_state < STATE_DATA)
{
#ifdef SMFIC_DATA
if (!CHECK_MPOPTS(ctx, SMFIP_NODATA))
{
char rcmd;
size_t buflen;
buflen = sizeof buf;
if (!mt_milter_write(ctx->ctx_fd, SMFIC_DATA, NULL, 0))
return FALSE;
rcmd = SMFIR_CONTINUE;
if (!CHECK_MPOPTS(ctx, SMFIP_NR_DATA))
{
if (!mt_milter_read(ctx->ctx_fd, &rcmd,
buf, &buflen))
return FALSE;
ctx->ctx_response = rcmd;
if (rcmd == SMFIR_REPLYCODE)
strlcpy(ctx->ctx_smtp_reply, buf,
sizeof ctx->ctx_smtp_reply);
else
ctx->ctx_smtp_reply[0] = '\0';
}
if (rcmd != SMFIR_CONTINUE)
{
if (verbose > 0)
{
fprintf(stdout,
"%s: filter returned status %d to DATA on fd %d\n",
progname, rcmd, ctx->ctx_fd);
}
ctx->ctx_state = STATE_DEAD;
}
}
#endif /* SMFIC_DATA */
ctx->ctx_state = STATE_DATA;
}
if (state >= STATE_HEADER && ctx->ctx_state < STATE_HEADER)
{
if (!CHECK_MPOPTS(ctx, SMFIP_NOHDRS))
{
char rcmd;
size_t buflen;
buflen = sizeof buf;
len = strlcpy(buf, DEFHEADERNAME, sizeof buf);
buf[len++] = '\0';
len += strlcpy(buf + len, DEFSENDER, sizeof buf - len);
buf[len++] = '\0';
if (!mt_milter_write(ctx->ctx_fd, SMFIC_HEADER,
buf, len))
return FALSE;
rcmd = SMFIR_CONTINUE;
if (!CHECK_MPOPTS(ctx, SMFIP_NR_HDR))
{
if (!mt_milter_read(ctx->ctx_fd, &rcmd,
buf, &buflen))
return FALSE;
ctx->ctx_response = rcmd;
if (rcmd == SMFIR_REPLYCODE)
strlcpy(ctx->ctx_smtp_reply, buf,
sizeof ctx->ctx_smtp_reply);
else
ctx->ctx_smtp_reply[0] = '\0';
}
if (rcmd != SMFIR_CONTINUE)
{
if (verbose > 0)
{
fprintf(stdout,
"%s: filter returned status %d to header on fd %d\n",
progname, rcmd, ctx->ctx_fd);
}
ctx->ctx_state = STATE_DEAD;
}
}
ctx->ctx_state = STATE_HEADER;
}
if (state >= STATE_EOH && ctx->ctx_state < STATE_EOH)
{
if (!CHECK_MPOPTS(ctx, SMFIP_NOEOH))
{
char rcmd;
size_t buflen;
buflen = sizeof buf;
if (!mt_milter_write(ctx->ctx_fd, SMFIC_EOH, NULL, 0))
return FALSE;
rcmd = SMFIR_CONTINUE;
if (!CHECK_MPOPTS(ctx, SMFIP_NR_EOH))
{
if (!mt_milter_read(ctx->ctx_fd, &rcmd,
buf, &buflen))
return FALSE;
ctx->ctx_response = rcmd;
if (rcmd == SMFIR_REPLYCODE)
strlcpy(ctx->ctx_smtp_reply, buf,
sizeof ctx->ctx_smtp_reply);
else
ctx->ctx_smtp_reply[0] = '\0';
}
if (rcmd != SMFIR_CONTINUE)
{
if (verbose > 0)