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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
|
/*
* Copyright (c) 1999-2001 Sendmail, Inc. and its suppliers.
* All rights reserved.
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the sendmail distribution.
*
*/
#include <sm/gen.h>
SM_RCSID("@(#)$Sendmail: engine.c,v 8.97 2001/09/11 04:04:44 gshapiro Exp $")
#include "libmilter.h"
#if NETINET || NETINET6
# include <arpa/inet.h>
#endif /* NETINET || NETINET6 */
/* generic argument for functions in the command table */
struct arg_struct
{
size_t a_len; /* length of buffer */
char *a_buf; /* argument string */
int a_idx; /* index for macro array */
SMFICTX_PTR a_ctx; /* context */
};
typedef struct arg_struct genarg;
/* structure for commands received from MTA */
struct cmdfct_t
{
char cm_cmd; /* command */
int cm_argt; /* type of arguments expected */
int cm_next; /* next state */
int cm_todo; /* what to do next */
int cm_macros; /* index for macros */
int (*cm_fct) __P((genarg *)); /* function to execute */
};
typedef struct cmdfct_t cmdfct;
/* possible values for cm_argt */
#define CM_ARG0 0 /* no args */
#define CM_ARG1 1 /* one arg (string) */
#define CM_ARG2 2 /* two args (strings) */
#define CM_ARGA 4 /* one string and _SOCK_ADDR */
#define CM_ARGO 5 /* two integers */
#define CM_ARGV 8 /* \0 separated list of args, NULL-terminated */
#define CM_ARGN 9 /* \0 separated list of args (strings) */
/* possible values for cm_todo */
#define CT_CONT 0x0000 /* continue reading commands */
#define CT_IGNO 0x0001 /* continue even when error */
/* not needed right now, done via return code instead */
#define CT_KEEP 0x0004 /* keep buffer (contains symbols) */
#define CT_END 0x0008 /* start replying */
/* index in macro array: macros only for these commands */
#define CI_NONE (-1)
#define CI_CONN 0
#define CI_HELO 1
#define CI_MAIL 2
#define CI_RCPT 3
#if CI_RCPT >= MAX_MACROS_ENTRIES
ERROR: do not compile with CI_RCPT >= MAX_MACROS_ENTRIES
#endif
/* function prototypes */
static int st_abortfct __P((genarg *));
static int st_macros __P((genarg *));
static int st_optionneg __P((genarg *));
static int st_bodychunk __P((genarg *));
static int st_connectinfo __P((genarg *));
static int st_bodyend __P((genarg *));
static int st_helo __P((genarg *));
static int st_header __P((genarg *));
static int st_sender __P((genarg *));
static int st_rcpt __P((genarg *));
static int st_eoh __P((genarg *));
static int st_quit __P((genarg *));
static int sendreply __P((sfsistat, socket_t, struct timeval *, SMFICTX_PTR));
static void fix_stm __P((SMFICTX_PTR));
static bool trans_ok __P((int, int));
static char **dec_argv __P((char *, size_t));
static int dec_arg2 __P((char *, size_t, char **, char **));
/* states */
#define ST_NONE (-1)
#define ST_INIT 0 /* initial state */
#define ST_OPTS 1 /* option negotiation */
#define ST_CONN 2 /* connection info */
#define ST_HELO 3 /* helo */
#define ST_MAIL 4 /* mail from */
#define ST_RCPT 5 /* rcpt to */
#define ST_HDRS 6 /* headers */
#define ST_EOHS 7 /* end of headers */
#define ST_BODY 8 /* body */
#define ST_ENDM 9 /* end of message */
#define ST_QUIT 10 /* quit */
#define ST_ABRT 11 /* abort */
#define ST_LAST ST_ABRT
#define ST_SKIP 15 /* not a state but required for the state table */
/* in a mail transaction? must be before eom according to spec. */
#define ST_IN_MAIL(st) ((st) >= ST_MAIL && (st) < ST_ENDM)
/*
** set of next states
** each state (ST_*) corresponds to bit in an int value (1 << state)
** each state has a set of allowed transitions ('or' of bits of states)
** so a state transition is valid if the mask of the next state
** is set in the NX_* value
** this function is coded in trans_ok(), see below.
*/
#define MASK(x) (0x0001 << (x)) /* generate a bit "mask" for a state */
#define NX_INIT (MASK(ST_OPTS))
#define NX_OPTS (MASK(ST_CONN))
#define NX_CONN (MASK(ST_HELO) | MASK(ST_MAIL))
#define NX_HELO (MASK(ST_HELO) | MASK(ST_MAIL))
#define NX_MAIL (MASK(ST_RCPT) | MASK(ST_ABRT))
#define NX_RCPT (MASK(ST_HDRS) | MASK(ST_EOHS) | \
MASK(ST_BODY) | MASK(ST_ENDM) | \
MASK(ST_RCPT) | MASK(ST_ABRT))
#define NX_HDRS (MASK(ST_EOHS) | MASK(ST_HDRS) | MASK(ST_ABRT))
#define NX_EOHS (MASK(ST_BODY) | MASK(ST_ENDM) | MASK(ST_ABRT))
#define NX_BODY (MASK(ST_ENDM) | MASK(ST_BODY) | MASK(ST_ABRT))
#define NX_ENDM (MASK(ST_QUIT) | MASK(ST_MAIL))
#define NX_QUIT 0
#define NX_ABRT 0
#define NX_SKIP MASK(ST_SKIP)
static int next_states[] =
{
NX_INIT,
NX_OPTS,
NX_CONN,
NX_HELO,
NX_MAIL,
NX_RCPT,
NX_HDRS,
NX_EOHS,
NX_BODY,
NX_ENDM,
NX_QUIT,
NX_ABRT
};
/* commands received by milter */
static cmdfct cmds[] =
{
{SMFIC_ABORT, CM_ARG0, ST_ABRT, CT_CONT, CI_NONE, st_abortfct },
{SMFIC_MACRO, CM_ARGV, ST_NONE, CT_KEEP, CI_NONE, st_macros },
{SMFIC_BODY, CM_ARG1, ST_BODY, CT_CONT, CI_NONE, st_bodychunk },
{SMFIC_CONNECT, CM_ARG2, ST_CONN, CT_CONT, CI_CONN, st_connectinfo },
{SMFIC_BODYEOB, CM_ARG1, ST_ENDM, CT_CONT, CI_NONE, st_bodyend },
{SMFIC_HELO, CM_ARG1, ST_HELO, CT_CONT, CI_HELO, st_helo },
{SMFIC_HEADER, CM_ARG2, ST_HDRS, CT_CONT, CI_NONE, st_header },
{SMFIC_MAIL, CM_ARGV, ST_MAIL, CT_CONT, CI_MAIL, st_sender },
{SMFIC_OPTNEG, CM_ARGO, ST_OPTS, CT_CONT, CI_NONE, st_optionneg },
{SMFIC_EOH, CM_ARG0, ST_EOHS, CT_CONT, CI_NONE, st_eoh },
{SMFIC_QUIT, CM_ARG0, ST_QUIT, CT_END, CI_NONE, st_quit },
{SMFIC_RCPT, CM_ARGV, ST_RCPT, CT_IGNO, CI_RCPT, st_rcpt }
};
/* additional (internal) reply codes */
#define _SMFIS_KEEP 20
#define _SMFIS_ABORT 21
#define _SMFIS_OPTIONS 22
#define _SMFIS_NOREPLY 23
#define _SMFIS_FAIL (-1)
/*
** MI_ENGINE -- receive commands and process them
**
** Parameters:
** ctx -- context structure
**
** Returns:
** MI_FAILURE/MI_SUCCESS
*/
int
mi_engine(ctx)
SMFICTX_PTR ctx;
{
size_t len;
int i;
socket_t sd;
int ret = MI_SUCCESS;
int ncmds = sizeof(cmds) / sizeof(cmdfct);
int curstate = ST_INIT;
int newstate;
bool call_abort;
sfsistat r;
char cmd;
char *buf = NULL;
genarg arg;
struct timeval timeout;
int (*f) __P((genarg *));
sfsistat (*fi_abort) __P((SMFICTX *));
sfsistat (*fi_close) __P((SMFICTX *));
arg.a_ctx = ctx;
sd = ctx->ctx_sd;
fi_abort = ctx->ctx_smfi->xxfi_abort;
mi_clr_macros(ctx, 0);
fix_stm(ctx);
do
{
/* call abort only if in a mail transaction */
call_abort = ST_IN_MAIL(curstate);
timeout.tv_sec = ctx->ctx_timeout;
timeout.tv_usec = 0;
if (mi_stop() == MILTER_ABRT)
{
if (ctx->ctx_dbg > 3)
sm_dprintf("[%d] milter_abort\n",
(int) ctx->ctx_id);
ret = MI_FAILURE;
break;
}
if ((buf = mi_rd_cmd(sd, &timeout, &cmd, &len,
ctx->ctx_smfi->xxfi_name)) == NULL &&
cmd < SMFIC_VALIDCMD)
{
if (ctx->ctx_dbg > 5)
sm_dprintf("[%d] mi_engine: mi_rd_cmd error (%x)\n",
(int) ctx->ctx_id, (int) cmd);
/*
** eof is currently treated as failure ->
** abort() instead of close(), otherwise use:
** if (cmd != SMFIC_EOF)
*/
ret = MI_FAILURE;
break;
}
if (ctx->ctx_dbg > 4)
sm_dprintf("[%d] got cmd '%c' len %d\n",
(int) ctx->ctx_id, cmd, len);
for (i = 0; i < ncmds; i++)
{
if (cmd == cmds[i].cm_cmd)
break;
}
if (i >= ncmds)
{
/* unknown command */
if (ctx->ctx_dbg > 1)
sm_dprintf("[%d] cmd '%c' unknown\n",
(int) ctx->ctx_id, cmd);
ret = MI_FAILURE;
break;
}
if ((f = cmds[i].cm_fct) == NULL)
{
/* stop for now */
if (ctx->ctx_dbg > 1)
sm_dprintf("[%d] cmd '%c' not impl\n",
(int) ctx->ctx_id, cmd);
ret = MI_FAILURE;
break;
}
/* is new state ok? */
newstate = cmds[i].cm_next;
if (ctx->ctx_dbg > 5)
sm_dprintf("[%d] cur %x new %x nextmask %x\n",
(int) ctx->ctx_id,
curstate, newstate, next_states[curstate]);
if (newstate != ST_NONE && !trans_ok(curstate, newstate))
{
if (ctx->ctx_dbg > 1)
sm_dprintf("[%d] abort: cur %d (%x) new %d (%x) next %x\n",
(int) ctx->ctx_id,
curstate, MASK(curstate),
newstate, MASK(newstate),
next_states[curstate]);
/* call abort only if in a mail transaction */
if (fi_abort != NULL && call_abort)
(void) (*fi_abort)(ctx);
/*
** try to reach the new state from HELO
** if it can't be reached, ignore the command.
*/
curstate = ST_HELO;
if (!trans_ok(curstate, newstate))
continue;
}
arg.a_len = len;
arg.a_buf = buf;
if (newstate != ST_NONE)
{
curstate = newstate;
ctx->ctx_state = curstate;
}
arg.a_idx = cmds[i].cm_macros;
/* call function to deal with command */
r = (*f)(&arg);
if (r != _SMFIS_KEEP && buf != NULL)
{
free(buf);
buf = NULL;
}
if (sendreply(r, sd, &timeout, ctx) != MI_SUCCESS)
{
ret = MI_FAILURE;
break;
}
call_abort = ST_IN_MAIL(curstate);
if (r == SMFIS_ACCEPT)
{
/* accept mail, no further actions taken */
curstate = ST_HELO;
}
else if (r == SMFIS_REJECT || r == SMFIS_DISCARD ||
r == SMFIS_TEMPFAIL)
{
/*
** further actions depend on current state
** if the IGNO bit is set: "ignore" the error,
** i.e., stay in the current state
*/
if (!bitset(CT_IGNO, cmds[i].cm_todo))
curstate = ST_HELO;
}
else if (r == _SMFIS_ABORT)
{
if (ctx->ctx_dbg > 5)
sm_dprintf("[%d] function returned abort\n",
(int) ctx->ctx_id);
ret = MI_FAILURE;
break;
}
} while (!bitset(CT_END, cmds[i].cm_todo));
if (ret != MI_SUCCESS)
{
/* call abort only if in a mail transaction */
if (fi_abort != NULL && call_abort)
(void) (*fi_abort)(ctx);
}
/* close must always be called */
if ((fi_close = ctx->ctx_smfi->xxfi_close) != NULL)
(void) (*fi_close)(ctx);
if (buf != NULL)
free(buf);
mi_clr_macros(ctx, 0);
return ret;
}
/*
** SENDREPLY -- send a reply to the MTA
**
** Parameters:
** r -- reply code
** sd -- socket descriptor
** timeout_ptr -- (ptr to) timeout to use for sending
** ctx -- context structure
**
** Returns:
** MI_SUCCESS/MI_FAILURE
*/
static int
sendreply(r, sd, timeout_ptr, ctx)
sfsistat r;
socket_t sd;
struct timeval *timeout_ptr;
SMFICTX_PTR ctx;
{
int ret = MI_SUCCESS;
switch (r)
{
case SMFIS_CONTINUE:
ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_CONTINUE, NULL, 0);
break;
case SMFIS_TEMPFAIL:
case SMFIS_REJECT:
if (ctx->ctx_reply != NULL)
{
ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_REPLYCODE,
ctx->ctx_reply,
strlen(ctx->ctx_reply) + 1);
free(ctx->ctx_reply);
ctx->ctx_reply = NULL;
}
else
{
ret = mi_wr_cmd(sd, timeout_ptr, r == SMFIS_REJECT ?
SMFIR_REJECT : SMFIR_TEMPFAIL, NULL, 0);
}
break;
case SMFIS_DISCARD:
ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_DISCARD, NULL, 0);
break;
case SMFIS_ACCEPT:
ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_ACCEPT, NULL, 0);
break;
case _SMFIS_OPTIONS:
{
char buf[MILTER_OPTLEN];
mi_int32 v;
v = htonl(ctx->ctx_smfi->xxfi_version);
(void) memcpy(&(buf[0]), (void *) &v, MILTER_LEN_BYTES);
v = htonl(ctx->ctx_smfi->xxfi_flags);
(void) memcpy(&(buf[MILTER_LEN_BYTES]), (void *) &v,
MILTER_LEN_BYTES);
v = htonl(ctx->ctx_pflags);
(void) memcpy(&(buf[MILTER_LEN_BYTES * 2]), (void *) &v,
MILTER_LEN_BYTES);
ret = mi_wr_cmd(sd, timeout_ptr, SMFIC_OPTNEG, buf,
MILTER_OPTLEN);
}
break;
default: /* don't send a reply */
break;
}
return ret;
}
/*
** CLR_MACROS -- clear set of macros starting from a given index
**
** Parameters:
** ctx -- context structure
** m -- index from which to clear all macros
**
** Returns:
** None.
*/
void
mi_clr_macros(ctx, m)
SMFICTX_PTR ctx;
int m;
{
int i;
for (i = m; i < MAX_MACROS_ENTRIES; i++)
{
if (ctx->ctx_mac_ptr[i] != NULL)
{
free(ctx->ctx_mac_ptr[i]);
ctx->ctx_mac_ptr[i] = NULL;
}
if (ctx->ctx_mac_buf[i] != NULL)
{
free(ctx->ctx_mac_buf[i]);
ctx->ctx_mac_buf[i] = NULL;
}
}
}
/*
** ST_OPTIONNEG -- negotiate options
**
** Parameters:
** g -- generic argument structure
**
** Returns:
** abort/send options/continue
*/
static int
st_optionneg(g)
genarg *g;
{
mi_int32 i, v;
if (g == NULL || g->a_ctx->ctx_smfi == NULL)
return SMFIS_CONTINUE;
mi_clr_macros(g->a_ctx, g->a_idx + 1);
/* check for minimum length */
if (g->a_len < MILTER_OPTLEN)
{
smi_log(SMI_LOG_ERR,
"%s: st_optionneg[%d]: len too short %d < %d",
g->a_ctx->ctx_smfi->xxfi_name,
(int) g->a_ctx->ctx_id, g->a_len,
MILTER_OPTLEN);
return _SMFIS_ABORT;
}
(void) memcpy((void *) &i, (void *) &(g->a_buf[0]),
MILTER_LEN_BYTES);
v = ntohl(i);
if (v < g->a_ctx->ctx_smfi->xxfi_version)
{
/* hard failure for now! */
smi_log(SMI_LOG_ERR,
"%s: st_optionneg[%d]: version mismatch MTA: %d < milter: %d",
g->a_ctx->ctx_smfi->xxfi_name,
(int) g->a_ctx->ctx_id, (int) v,
g->a_ctx->ctx_smfi->xxfi_version);
return _SMFIS_ABORT;
}
(void) memcpy((void *) &i, (void *) &(g->a_buf[MILTER_LEN_BYTES]),
MILTER_LEN_BYTES);
v = ntohl(i);
/* no flags? set to default value for V1 actions */
if (v == 0)
v = SMFI_V1_ACTS;
i = g->a_ctx->ctx_smfi->xxfi_flags;
if ((v & i) != i)
{
smi_log(SMI_LOG_ERR,
"%s: st_optionneg[%d]: 0x%x does not fulfill action requirements 0x%x",
g->a_ctx->ctx_smfi->xxfi_name,
(int) g->a_ctx->ctx_id, v, i);
return _SMFIS_ABORT;
}
(void) memcpy((void *) &i, (void *) &(g->a_buf[MILTER_LEN_BYTES * 2]),
MILTER_LEN_BYTES);
v = ntohl(i);
/* no flags? set to default value for V1 protocol */
if (v == 0)
v = SMFI_V1_PROT;
i = g->a_ctx->ctx_pflags;
if ((v & i) != i)
{
smi_log(SMI_LOG_ERR,
"%s: st_optionneg[%d]: 0x%x does not fulfill protocol requirements 0x%x",
g->a_ctx->ctx_smfi->xxfi_name,
(int) g->a_ctx->ctx_id, v, i);
return _SMFIS_ABORT;
}
return _SMFIS_OPTIONS;
}
/*
** ST_CONNECTINFO -- receive connection information
**
** Parameters:
** g -- generic argument structure
**
** Returns:
** continue or filter-specified value
*/
static int
st_connectinfo(g)
genarg *g;
{
size_t l;
size_t i;
char *s, family;
unsigned short port = 0;
_SOCK_ADDR sockaddr;
sfsistat (*fi_connect) __P((SMFICTX *, char *, _SOCK_ADDR *));
if (g == NULL)
return _SMFIS_ABORT;
mi_clr_macros(g->a_ctx, g->a_idx + 1);
if (g->a_ctx->ctx_smfi == NULL ||
(fi_connect = g->a_ctx->ctx_smfi->xxfi_connect) == NULL)
return SMFIS_CONTINUE;
s = g->a_buf;
i = 0;
l = g->a_len;
while (s[i] != '\0' && i <= l)
++i;
if (i >= l)
return _SMFIS_ABORT;
/* Move past trailing \0 in host string */
i++;
family = s[i++];
memset(&sockaddr, '\0', sizeof sockaddr);
if (family != SMFIA_UNKNOWN)
{
(void) memcpy((void *) &port, (void *) (s + i),
sizeof port);
port = ntohs(port);
if ((i += sizeof port) >= l)
{
smi_log(SMI_LOG_ERR,
"%s: connect[%d]: wrong len %d >= %d",
g->a_ctx->ctx_smfi->xxfi_name,
(int) g->a_ctx->ctx_id, i, l);
return _SMFIS_ABORT;
}
# if NETINET
if (family == SMFIA_INET)
{
if (inet_aton(s + i, (struct in_addr *) &sockaddr.sin.sin_addr)
== INADDR_NONE)
{
smi_log(SMI_LOG_ERR,
"%s: connect[%d]: inet_aton failed",
g->a_ctx->ctx_smfi->xxfi_name,
(int) g->a_ctx->ctx_id);
return _SMFIS_ABORT;
}
sockaddr.sa.sa_family = AF_INET;
if (port > 0)
sockaddr.sin.sin_port = port;
}
else
# endif /* NETINET */
# if NETINET6
if (family == SMFIA_INET6)
{
if (mi_inet_pton(AF_INET6, s + i,
&sockaddr.sin6.sin6_addr) != 1)
{
smi_log(SMI_LOG_ERR,
"%s: connect[%d]: mi_inet_pton failed",
g->a_ctx->ctx_smfi->xxfi_name,
(int) g->a_ctx->ctx_id);
return _SMFIS_ABORT;
}
sockaddr.sa.sa_family = AF_INET6;
if (port > 0)
sockaddr.sin6.sin6_port = port;
}
else
# endif /* NETINET6 */
# if NETUNIX
if (family == SMFIA_UNIX)
{
if (sm_strlcpy(sockaddr.sunix.sun_path, s + i,
sizeof sockaddr.sunix.sun_path) >=
sizeof sockaddr.sunix.sun_path)
{
smi_log(SMI_LOG_ERR,
"%s: connect[%d]: path too long",
g->a_ctx->ctx_smfi->xxfi_name,
(int) g->a_ctx->ctx_id);
return _SMFIS_ABORT;
}
sockaddr.sunix.sun_family = AF_UNIX;
}
else
# endif /* NETUNIX */
{
smi_log(SMI_LOG_ERR,
"%s: connect[%d]: unknown family %d",
g->a_ctx->ctx_smfi->xxfi_name,
(int) g->a_ctx->ctx_id, family);
return _SMFIS_ABORT;
}
}
return (*fi_connect)(g->a_ctx, g->a_buf,
family != SMFIA_UNKNOWN ? &sockaddr : NULL);
}
/*
** ST_EOH -- end of headers
**
** Parameters:
** g -- generic argument structure
**
** Returns:
** continue or filter-specified value
*/
static int
st_eoh(g)
genarg *g;
{
sfsistat (*fi_eoh) __P((SMFICTX *));
if (g == NULL)
return _SMFIS_ABORT;
if (g->a_ctx->ctx_smfi != NULL &&
(fi_eoh = g->a_ctx->ctx_smfi->xxfi_eoh) != NULL)
return (*fi_eoh)(g->a_ctx);
return SMFIS_CONTINUE;
}
/*
** ST_HELO -- helo/ehlo command
**
** Parameters:
** g -- generic argument structure
**
** Returns:
** continue or filter-specified value
*/
static int
st_helo(g)
genarg *g;
{
sfsistat (*fi_helo) __P((SMFICTX *, char *));
if (g == NULL)
return _SMFIS_ABORT;
mi_clr_macros(g->a_ctx, g->a_idx + 1);
if (g->a_ctx->ctx_smfi != NULL &&
(fi_helo = g->a_ctx->ctx_smfi->xxfi_helo) != NULL)
return (*fi_helo)(g->a_ctx, g->a_buf);
return SMFIS_CONTINUE;
}
/*
** ST_HEADER -- header line
**
** Parameters:
** g -- generic argument structure
**
** Returns:
** continue or filter-specified value
*/
static int
st_header(g)
genarg *g;
{
char *hf, *hv;
sfsistat (*fi_header) __P((SMFICTX *, char *, char *));
if (g == NULL)
return _SMFIS_ABORT;
if (g->a_ctx->ctx_smfi == NULL ||
(fi_header = g->a_ctx->ctx_smfi->xxfi_header) == NULL)
return SMFIS_CONTINUE;
if (dec_arg2(g->a_buf, g->a_len, &hf, &hv) == MI_SUCCESS)
return (*fi_header)(g->a_ctx, hf, hv);
else
return _SMFIS_ABORT;
}
#define ARGV_FCT(lf, rf, idx) \
char **argv; \
sfsistat (*lf) __P((SMFICTX *, char **)); \
int r; \
\
if (g == NULL) \
return _SMFIS_ABORT; \
mi_clr_macros(g->a_ctx, g->a_idx + 1); \
if (g->a_ctx->ctx_smfi == NULL || \
(lf = g->a_ctx->ctx_smfi->rf) == NULL) \
return SMFIS_CONTINUE; \
if ((argv = dec_argv(g->a_buf, g->a_len)) == NULL) \
return _SMFIS_ABORT; \
r = (*lf)(g->a_ctx, argv); \
free(argv); \
return r;
/*
** ST_SENDER -- MAIL FROM command
**
** Parameters:
** g -- generic argument structure
**
** Returns:
** continue or filter-specified value
*/
static int
st_sender(g)
genarg *g;
{
ARGV_FCT(fi_envfrom, xxfi_envfrom, CI_MAIL)
}
/*
** ST_RCPT -- RCPT TO command
**
** Parameters:
** g -- generic argument structure
**
** Returns:
** continue or filter-specified value
*/
static int
st_rcpt(g)
genarg *g;
{
ARGV_FCT(fi_envrcpt, xxfi_envrcpt, CI_RCPT)
}
/*
** ST_MACROS -- deal with macros received from the MTA
**
** Parameters:
** g -- generic argument structure
**
** Returns:
** continue/keep
**
** Side effects:
** set pointer in macro array to current values.
*/
static int
st_macros(g)
genarg *g;
{
int i;
char **argv;
if (g == NULL || g->a_len < 1)
return _SMFIS_FAIL;
if ((argv = dec_argv(g->a_buf + 1, g->a_len - 1)) == NULL)
return _SMFIS_FAIL;
switch (g->a_buf[0])
{
case SMFIC_CONNECT:
i = CI_CONN;
break;
case SMFIC_HELO:
i = CI_HELO;
break;
case SMFIC_MAIL:
i = CI_MAIL;
break;
case SMFIC_RCPT:
i = CI_RCPT;
break;
default:
free(argv);
return _SMFIS_FAIL;
}
if (g->a_ctx->ctx_mac_ptr[i] != NULL)
free(g->a_ctx->ctx_mac_ptr[i]);
if (g->a_ctx->ctx_mac_buf[i] != NULL)
free(g->a_ctx->ctx_mac_buf[i]);
g->a_ctx->ctx_mac_ptr[i] = argv;
g->a_ctx->ctx_mac_buf[i] = g->a_buf;
return _SMFIS_KEEP;
}
/*
** ST_QUIT -- quit command
**
** Parameters:
** g -- generic argument structure
**
** Returns:
** noreply
*/
static int
st_quit(g)
genarg *g;
{
return _SMFIS_NOREPLY;
}
/*
** ST_BODYCHUNK -- deal with a piece of the mail body
**
** Parameters:
** g -- generic argument structure
**
** Returns:
** continue or filter-specified value
*/
static int
st_bodychunk(g)
genarg *g;
{
sfsistat (*fi_body) __P((SMFICTX *, unsigned char *, size_t));
if (g == NULL)
return _SMFIS_ABORT;
if (g->a_ctx->ctx_smfi != NULL &&
(fi_body = g->a_ctx->ctx_smfi->xxfi_body) != NULL)
return (*fi_body)(g->a_ctx, (unsigned char *)g->a_buf,
g->a_len);
return SMFIS_CONTINUE;
}
/*
** ST_BODYEND -- deal with the last piece of the mail body
**
** Parameters:
** g -- generic argument structure
**
** Returns:
** continue or filter-specified value
**
** Side effects:
** sends a reply for the body part (if non-empty).
*/
static int
st_bodyend(g)
genarg *g;
{
sfsistat r;
sfsistat (*fi_body) __P((SMFICTX *, unsigned char *, size_t));
sfsistat (*fi_eom) __P((SMFICTX *));
if (g == NULL)
return _SMFIS_ABORT;
r = SMFIS_CONTINUE;
if (g->a_ctx->ctx_smfi != NULL)
{
if ((fi_body = g->a_ctx->ctx_smfi->xxfi_body) != NULL &&
g->a_len > 0)
{
socket_t sd;
struct timeval timeout;
timeout.tv_sec = g->a_ctx->ctx_timeout;
timeout.tv_usec = 0;
sd = g->a_ctx->ctx_sd;
r = (*fi_body)(g->a_ctx, (unsigned char *)g->a_buf,
g->a_len);
if (r != SMFIS_CONTINUE &&
sendreply(r, sd, &timeout, g->a_ctx) != MI_SUCCESS)
return _SMFIS_ABORT;
}
}
if (r == SMFIS_CONTINUE &&
(fi_eom = g->a_ctx->ctx_smfi->xxfi_eom) != NULL)
return (*fi_eom)(g->a_ctx);
return r;
}
/*
** ST_ABORTFCT -- deal with aborts
**
** Parameters:
** g -- generic argument structure
**
** Returns:
** abort or filter-specified value
*/
static int
st_abortfct(g)
genarg *g;
{
sfsistat (*fi_abort) __P((SMFICTX *));
if (g == NULL)
return _SMFIS_ABORT;
if (g != NULL && g->a_ctx->ctx_smfi != NULL &&
(fi_abort = g->a_ctx->ctx_smfi->xxfi_abort) != NULL)
(void) (*fi_abort)(g->a_ctx);
return _SMFIS_NOREPLY;
}
/*
** TRANS_OK -- is the state transition ok?
**
** Parameters:
** old -- old state
** new -- new state
**
** Returns:
** state transition ok
*/
static bool
trans_ok(old, new)
int old, new;
{
int s, n;
s = old;
do
{
/* is this state transition allowed? */
if ((MASK(new) & next_states[s]) != 0)
return true;
/*
** no: try next state;
** this works since the relevant states are ordered
** strict sequentially
*/
n = s + 1;
/*
** can we actually "skip" this state?
** see fix_stm() which sets this bit for those
** states which the filter program is not interested in
*/
if (bitset(NX_SKIP, next_states[n]))
s = n;
else
return false;
} while (s <= ST_LAST);
return false;
}
/*
** FIX_STM -- add "skip" bits to the state transition table
**
** Parameters:
** ctx -- context structure
**
** Returns:
** None.
**
** Side effects:
** may change state transition table.
*/
static void
fix_stm(ctx)
SMFICTX_PTR ctx;
{
unsigned long fl;
if (ctx == NULL || ctx->ctx_smfi == NULL)
return;
fl = ctx->ctx_pflags;
if (bitset(SMFIP_NOCONNECT, fl))
next_states[ST_CONN] |= NX_SKIP;
if (bitset(SMFIP_NOHELO, fl))
next_states[ST_HELO] |= NX_SKIP;
if (bitset(SMFIP_NOMAIL, fl))
next_states[ST_MAIL] |= NX_SKIP;
if (bitset(SMFIP_NORCPT, fl))
next_states[ST_RCPT] |= NX_SKIP;
if (bitset(SMFIP_NOHDRS, fl))
next_states[ST_HDRS] |= NX_SKIP;
if (bitset(SMFIP_NOEOH, fl))
next_states[ST_EOHS] |= NX_SKIP;
if (bitset(SMFIP_NOBODY, fl))
next_states[ST_BODY] |= NX_SKIP;
}
/*
** DEC_ARGV -- split a buffer into a list of strings, NULL terminated
**
** Parameters:
** buf -- buffer with several strings
** len -- length of buffer
**
** Returns:
** array of pointers to the individual strings
*/
static char **
dec_argv(buf, len)
char *buf;
size_t len;
{
char **s;
size_t i;
int elem, nelem;
nelem = 0;
for (i = 0; i < len; i++)
{
if (buf[i] == '\0')
++nelem;
}
if (nelem == 0)
return NULL;
/* last entry is only for the name */
s = (char **)malloc((nelem + 1) * (sizeof *s));
if (s == NULL)
return NULL;
s[0] = buf;
for (i = 0, elem = 0; i < len && elem < nelem; i++)
{
if (buf[i] == '\0')
s[++elem] = &(buf[i + 1]);
}
/* overwrite last entry */
s[elem] = NULL;
return s;
}
/*
** DEC_ARG2 -- split a buffer into two strings
**
** Parameters:
** buf -- buffer with two strings
** len -- length of buffer
** s1,s2 -- pointer to result strings
**
** Returns:
** MI_FAILURE/MI_SUCCESS
*/
static int
dec_arg2(buf, len, s1, s2)
char *buf;
size_t len;
char **s1;
char **s2;
{
size_t i;
*s1 = buf;
for (i = 1; i < len && buf[i] != '\0'; i++)
continue;
if (i >= len - 1)
return MI_FAILURE;
*s2 = buf + i + 1;
return MI_SUCCESS;
}
/*
** SENDOK -- is it ok for the filter to send stuff to the MTA?
**
** Parameters:
** ctx -- context structure
** flag -- flag to check
**
** Returns:
** sending allowed (in current state)
*/
bool
mi_sendok(ctx, flag)
SMFICTX_PTR ctx;
int flag;
{
if (ctx == NULL || ctx->ctx_smfi == NULL)
return false;
if (flag != 0 && !bitset(flag, ctx->ctx_smfi->xxfi_flags))
return false;
return ctx->ctx_state == ST_ENDM;
}
|