xref: /titanic_50/usr/src/cmd/sendmail/libmilter/engine.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  *  Copyright (c) 1999-2003 Sendmail, Inc. and its suppliers.
3*7c478bd9Sstevel@tonic-gate  *	All rights reserved.
4*7c478bd9Sstevel@tonic-gate  *
5*7c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
6*7c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
7*7c478bd9Sstevel@tonic-gate  * the sendmail distribution.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  */
10*7c478bd9Sstevel@tonic-gate 
11*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
12*7c478bd9Sstevel@tonic-gate 
13*7c478bd9Sstevel@tonic-gate #include <sm/gen.h>
14*7c478bd9Sstevel@tonic-gate SM_RCSID("@(#)$Id: engine.c,v 8.120 2004/10/20 21:09:00 ca Exp $")
15*7c478bd9Sstevel@tonic-gate 
16*7c478bd9Sstevel@tonic-gate #include "libmilter.h"
17*7c478bd9Sstevel@tonic-gate 
18*7c478bd9Sstevel@tonic-gate #if NETINET || NETINET6
19*7c478bd9Sstevel@tonic-gate # include <arpa/inet.h>
20*7c478bd9Sstevel@tonic-gate #endif /* NETINET || NETINET6 */
21*7c478bd9Sstevel@tonic-gate 
22*7c478bd9Sstevel@tonic-gate /* generic argument for functions in the command table */
23*7c478bd9Sstevel@tonic-gate struct arg_struct
24*7c478bd9Sstevel@tonic-gate {
25*7c478bd9Sstevel@tonic-gate 	size_t		a_len;		/* length of buffer */
26*7c478bd9Sstevel@tonic-gate 	char		*a_buf;		/* argument string */
27*7c478bd9Sstevel@tonic-gate 	int		a_idx;		/* index for macro array */
28*7c478bd9Sstevel@tonic-gate 	SMFICTX_PTR	a_ctx;		/* context */
29*7c478bd9Sstevel@tonic-gate };
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate typedef struct arg_struct genarg;
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /* structure for commands received from MTA */
34*7c478bd9Sstevel@tonic-gate struct cmdfct_t
35*7c478bd9Sstevel@tonic-gate {
36*7c478bd9Sstevel@tonic-gate 	char	cm_cmd;				/* command */
37*7c478bd9Sstevel@tonic-gate 	int	cm_argt;			/* type of arguments expected */
38*7c478bd9Sstevel@tonic-gate 	int	cm_next;			/* next state */
39*7c478bd9Sstevel@tonic-gate 	int	cm_todo;			/* what to do next */
40*7c478bd9Sstevel@tonic-gate 	int	cm_macros;			/* index for macros */
41*7c478bd9Sstevel@tonic-gate 	int	(*cm_fct) __P((genarg *));	/* function to execute */
42*7c478bd9Sstevel@tonic-gate };
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate typedef struct cmdfct_t cmdfct;
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate /* possible values for cm_argt */
47*7c478bd9Sstevel@tonic-gate #define	CM_ARG0	0	/* no args */
48*7c478bd9Sstevel@tonic-gate #define	CM_ARG1	1	/* one arg (string) */
49*7c478bd9Sstevel@tonic-gate #define	CM_ARG2	2	/* two args (strings) */
50*7c478bd9Sstevel@tonic-gate #define	CM_ARGA	4	/* one string and _SOCK_ADDR */
51*7c478bd9Sstevel@tonic-gate #define	CM_ARGO	5	/* two integers */
52*7c478bd9Sstevel@tonic-gate #define	CM_ARGV	8	/* \0 separated list of args, NULL-terminated */
53*7c478bd9Sstevel@tonic-gate #define	CM_ARGN	9	/* \0 separated list of args (strings) */
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate /* possible values for cm_todo */
56*7c478bd9Sstevel@tonic-gate #define	CT_CONT		0x0000	/* continue reading commands */
57*7c478bd9Sstevel@tonic-gate #define	CT_IGNO		0x0001	/* continue even when error  */
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate /* not needed right now, done via return code instead */
60*7c478bd9Sstevel@tonic-gate #define	CT_KEEP		0x0004	/* keep buffer (contains symbols) */
61*7c478bd9Sstevel@tonic-gate #define	CT_END		0x0008	/* start replying */
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate /* index in macro array: macros only for these commands */
64*7c478bd9Sstevel@tonic-gate #define	CI_NONE		(-1)
65*7c478bd9Sstevel@tonic-gate #define	CI_CONN		0
66*7c478bd9Sstevel@tonic-gate #define	CI_HELO		1
67*7c478bd9Sstevel@tonic-gate #define	CI_MAIL		2
68*7c478bd9Sstevel@tonic-gate #define CI_RCPT		3
69*7c478bd9Sstevel@tonic-gate #define CI_EOM		4
70*7c478bd9Sstevel@tonic-gate #if CI_EOM >= MAX_MACROS_ENTRIES
71*7c478bd9Sstevel@tonic-gate ERROR: do not compile with CI_EOM >= MAX_MACROS_ENTRIES
72*7c478bd9Sstevel@tonic-gate #endif
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate /* function prototypes */
75*7c478bd9Sstevel@tonic-gate static int	st_abortfct __P((genarg *));
76*7c478bd9Sstevel@tonic-gate static int	st_macros __P((genarg *));
77*7c478bd9Sstevel@tonic-gate static int	st_optionneg __P((genarg *));
78*7c478bd9Sstevel@tonic-gate static int	st_bodychunk __P((genarg *));
79*7c478bd9Sstevel@tonic-gate static int	st_connectinfo __P((genarg *));
80*7c478bd9Sstevel@tonic-gate static int	st_bodyend __P((genarg *));
81*7c478bd9Sstevel@tonic-gate static int	st_helo __P((genarg *));
82*7c478bd9Sstevel@tonic-gate static int	st_header __P((genarg *));
83*7c478bd9Sstevel@tonic-gate static int	st_sender __P((genarg *));
84*7c478bd9Sstevel@tonic-gate static int	st_rcpt __P((genarg *));
85*7c478bd9Sstevel@tonic-gate #if SMFI_VERSION > 2
86*7c478bd9Sstevel@tonic-gate static int	st_unknown __P((genarg *));
87*7c478bd9Sstevel@tonic-gate #endif /* SMFI_VERSION > 2 */
88*7c478bd9Sstevel@tonic-gate #if SMFI_VERSION > 3
89*7c478bd9Sstevel@tonic-gate static int	st_data __P((genarg *));
90*7c478bd9Sstevel@tonic-gate #endif /* SMFI_VERSION > 3 */
91*7c478bd9Sstevel@tonic-gate static int	st_eoh __P((genarg *));
92*7c478bd9Sstevel@tonic-gate static int	st_quit __P((genarg *));
93*7c478bd9Sstevel@tonic-gate static int	sendreply __P((sfsistat, socket_t, struct timeval *, SMFICTX_PTR));
94*7c478bd9Sstevel@tonic-gate static void	fix_stm __P((SMFICTX_PTR));
95*7c478bd9Sstevel@tonic-gate static bool	trans_ok __P((int, int));
96*7c478bd9Sstevel@tonic-gate static char	**dec_argv __P((char *, size_t));
97*7c478bd9Sstevel@tonic-gate static int	dec_arg2 __P((char *, size_t, char **, char **));
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate /* states */
100*7c478bd9Sstevel@tonic-gate #define ST_NONE	(-1)
101*7c478bd9Sstevel@tonic-gate #define ST_INIT	0	/* initial state */
102*7c478bd9Sstevel@tonic-gate #define ST_OPTS	1	/* option negotiation */
103*7c478bd9Sstevel@tonic-gate #define ST_CONN	2	/* connection info */
104*7c478bd9Sstevel@tonic-gate #define ST_HELO	3	/* helo */
105*7c478bd9Sstevel@tonic-gate #define ST_MAIL	4	/* mail from */
106*7c478bd9Sstevel@tonic-gate #define ST_RCPT	5	/* rcpt to */
107*7c478bd9Sstevel@tonic-gate #define ST_DATA	6	/* data */
108*7c478bd9Sstevel@tonic-gate #define ST_HDRS	7	/* headers */
109*7c478bd9Sstevel@tonic-gate #define ST_EOHS	8	/* end of headers */
110*7c478bd9Sstevel@tonic-gate #define ST_BODY	9	/* body */
111*7c478bd9Sstevel@tonic-gate #define ST_ENDM	10	/* end of message */
112*7c478bd9Sstevel@tonic-gate #define ST_QUIT	11	/* quit */
113*7c478bd9Sstevel@tonic-gate #define ST_ABRT	12	/* abort */
114*7c478bd9Sstevel@tonic-gate #define ST_UNKN 13	/* unknown SMTP command */
115*7c478bd9Sstevel@tonic-gate #define ST_LAST	ST_UNKN	/* last valid state */
116*7c478bd9Sstevel@tonic-gate #define ST_SKIP	15	/* not a state but required for the state table */
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate /* in a mail transaction? must be before eom according to spec. */
119*7c478bd9Sstevel@tonic-gate #define ST_IN_MAIL(st)	((st) >= ST_MAIL && (st) < ST_ENDM)
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate /*
122*7c478bd9Sstevel@tonic-gate **  set of next states
123*7c478bd9Sstevel@tonic-gate **  each state (ST_*) corresponds to bit in an int value (1 << state)
124*7c478bd9Sstevel@tonic-gate **  each state has a set of allowed transitions ('or' of bits of states)
125*7c478bd9Sstevel@tonic-gate **  so a state transition is valid if the mask of the next state
126*7c478bd9Sstevel@tonic-gate **  is set in the NX_* value
127*7c478bd9Sstevel@tonic-gate **  this function is coded in trans_ok(), see below.
128*7c478bd9Sstevel@tonic-gate */
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate #define MI_MASK(x)	(0x0001 << (x))	/* generate a bit "mask" for a state */
131*7c478bd9Sstevel@tonic-gate #define NX_INIT	(MI_MASK(ST_OPTS))
132*7c478bd9Sstevel@tonic-gate #define NX_OPTS	(MI_MASK(ST_CONN) | MI_MASK(ST_UNKN))
133*7c478bd9Sstevel@tonic-gate #define NX_CONN	(MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN))
134*7c478bd9Sstevel@tonic-gate #define NX_HELO	(MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN))
135*7c478bd9Sstevel@tonic-gate #define NX_MAIL	(MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | MI_MASK(ST_UNKN))
136*7c478bd9Sstevel@tonic-gate #define NX_RCPT	(MI_MASK(ST_HDRS) | MI_MASK(ST_EOHS) | MI_MASK(ST_DATA) | \
137*7c478bd9Sstevel@tonic-gate 		 MI_MASK(ST_BODY) | MI_MASK(ST_ENDM) | \
138*7c478bd9Sstevel@tonic-gate 		 MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | MI_MASK(ST_UNKN))
139*7c478bd9Sstevel@tonic-gate #define NX_DATA	(MI_MASK(ST_EOHS) | MI_MASK(ST_HDRS) | MI_MASK(ST_ABRT))
140*7c478bd9Sstevel@tonic-gate #define NX_HDRS	(MI_MASK(ST_EOHS) | MI_MASK(ST_HDRS) | MI_MASK(ST_ABRT))
141*7c478bd9Sstevel@tonic-gate #define NX_EOHS	(MI_MASK(ST_BODY) | MI_MASK(ST_ENDM) | MI_MASK(ST_ABRT))
142*7c478bd9Sstevel@tonic-gate #define NX_BODY	(MI_MASK(ST_ENDM) | MI_MASK(ST_BODY) | MI_MASK(ST_ABRT))
143*7c478bd9Sstevel@tonic-gate #define NX_ENDM	(MI_MASK(ST_QUIT) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN))
144*7c478bd9Sstevel@tonic-gate #define NX_QUIT	0
145*7c478bd9Sstevel@tonic-gate #define NX_ABRT	0
146*7c478bd9Sstevel@tonic-gate #define NX_UNKN (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | \
147*7c478bd9Sstevel@tonic-gate 		 MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | \
148*7c478bd9Sstevel@tonic-gate 		 MI_MASK(ST_DATA) | \
149*7c478bd9Sstevel@tonic-gate 		 MI_MASK(ST_BODY) | MI_MASK(ST_UNKN) | \
150*7c478bd9Sstevel@tonic-gate 		 MI_MASK(ST_ABRT) | MI_MASK(ST_QUIT))
151*7c478bd9Sstevel@tonic-gate #define NX_SKIP MI_MASK(ST_SKIP)
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate static int next_states[] =
154*7c478bd9Sstevel@tonic-gate {
155*7c478bd9Sstevel@tonic-gate 	NX_INIT,
156*7c478bd9Sstevel@tonic-gate 	NX_OPTS,
157*7c478bd9Sstevel@tonic-gate 	NX_CONN,
158*7c478bd9Sstevel@tonic-gate 	NX_HELO,
159*7c478bd9Sstevel@tonic-gate 	NX_MAIL,
160*7c478bd9Sstevel@tonic-gate 	NX_RCPT,
161*7c478bd9Sstevel@tonic-gate 	NX_DATA,
162*7c478bd9Sstevel@tonic-gate 	NX_HDRS,
163*7c478bd9Sstevel@tonic-gate 	NX_EOHS,
164*7c478bd9Sstevel@tonic-gate 	NX_BODY,
165*7c478bd9Sstevel@tonic-gate 	NX_ENDM,
166*7c478bd9Sstevel@tonic-gate 	NX_QUIT,
167*7c478bd9Sstevel@tonic-gate 	NX_ABRT,
168*7c478bd9Sstevel@tonic-gate 	NX_UNKN
169*7c478bd9Sstevel@tonic-gate };
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate /* commands received by milter */
172*7c478bd9Sstevel@tonic-gate static cmdfct cmds[] =
173*7c478bd9Sstevel@tonic-gate {
174*7c478bd9Sstevel@tonic-gate {SMFIC_ABORT,	CM_ARG0, ST_ABRT,  CT_CONT,	CI_NONE, st_abortfct	},
175*7c478bd9Sstevel@tonic-gate {SMFIC_MACRO,	CM_ARGV, ST_NONE,  CT_KEEP,	CI_NONE, st_macros	},
176*7c478bd9Sstevel@tonic-gate {SMFIC_BODY,	CM_ARG1, ST_BODY,  CT_CONT,	CI_NONE, st_bodychunk	},
177*7c478bd9Sstevel@tonic-gate {SMFIC_CONNECT,	CM_ARG2, ST_CONN,  CT_CONT,	CI_CONN, st_connectinfo	},
178*7c478bd9Sstevel@tonic-gate {SMFIC_BODYEOB,	CM_ARG1, ST_ENDM,  CT_CONT,	CI_EOM,  st_bodyend	},
179*7c478bd9Sstevel@tonic-gate {SMFIC_HELO,	CM_ARG1, ST_HELO,  CT_CONT,	CI_HELO, st_helo	},
180*7c478bd9Sstevel@tonic-gate {SMFIC_HEADER,	CM_ARG2, ST_HDRS,  CT_CONT,	CI_NONE, st_header	},
181*7c478bd9Sstevel@tonic-gate {SMFIC_MAIL,	CM_ARGV, ST_MAIL,  CT_CONT,	CI_MAIL, st_sender	},
182*7c478bd9Sstevel@tonic-gate {SMFIC_OPTNEG,	CM_ARGO, ST_OPTS,  CT_CONT,	CI_NONE, st_optionneg	},
183*7c478bd9Sstevel@tonic-gate {SMFIC_EOH,	CM_ARG0, ST_EOHS,  CT_CONT,	CI_NONE, st_eoh		},
184*7c478bd9Sstevel@tonic-gate {SMFIC_QUIT,	CM_ARG0, ST_QUIT,  CT_END,	CI_NONE, st_quit	},
185*7c478bd9Sstevel@tonic-gate #if SMFI_VERSION > 3
186*7c478bd9Sstevel@tonic-gate {SMFIC_DATA,	CM_ARG0, ST_DATA,  CT_CONT,	CI_NONE, st_data	},
187*7c478bd9Sstevel@tonic-gate #endif /* SMFI_VERSION > 3 */
188*7c478bd9Sstevel@tonic-gate {SMFIC_RCPT,	CM_ARGV, ST_RCPT,  CT_IGNO,	CI_RCPT, st_rcpt	}
189*7c478bd9Sstevel@tonic-gate #if SMFI_VERSION > 2
190*7c478bd9Sstevel@tonic-gate ,{SMFIC_UNKNOWN,CM_ARG1, ST_UNKN,  CT_IGNO,	CI_NONE, st_unknown	}
191*7c478bd9Sstevel@tonic-gate #endif /* SMFI_VERSION > 2 */
192*7c478bd9Sstevel@tonic-gate };
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate /* additional (internal) reply codes */
195*7c478bd9Sstevel@tonic-gate #define _SMFIS_KEEP	20
196*7c478bd9Sstevel@tonic-gate #define _SMFIS_ABORT	21
197*7c478bd9Sstevel@tonic-gate #define _SMFIS_OPTIONS	22
198*7c478bd9Sstevel@tonic-gate #define _SMFIS_NOREPLY	23
199*7c478bd9Sstevel@tonic-gate #define _SMFIS_FAIL	(-1)
200*7c478bd9Sstevel@tonic-gate #define _SMFIS_NONE	(-2)
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate /*
203*7c478bd9Sstevel@tonic-gate **  MI_ENGINE -- receive commands and process them
204*7c478bd9Sstevel@tonic-gate **
205*7c478bd9Sstevel@tonic-gate **	Parameters:
206*7c478bd9Sstevel@tonic-gate **		ctx -- context structure
207*7c478bd9Sstevel@tonic-gate **
208*7c478bd9Sstevel@tonic-gate **	Returns:
209*7c478bd9Sstevel@tonic-gate **		MI_FAILURE/MI_SUCCESS
210*7c478bd9Sstevel@tonic-gate */
211*7c478bd9Sstevel@tonic-gate int
212*7c478bd9Sstevel@tonic-gate mi_engine(ctx)
213*7c478bd9Sstevel@tonic-gate 	SMFICTX_PTR ctx;
214*7c478bd9Sstevel@tonic-gate {
215*7c478bd9Sstevel@tonic-gate 	size_t len;
216*7c478bd9Sstevel@tonic-gate 	int i;
217*7c478bd9Sstevel@tonic-gate 	socket_t sd;
218*7c478bd9Sstevel@tonic-gate 	int ret = MI_SUCCESS;
219*7c478bd9Sstevel@tonic-gate 	int ncmds = sizeof(cmds) / sizeof(cmdfct);
220*7c478bd9Sstevel@tonic-gate 	int curstate = ST_INIT;
221*7c478bd9Sstevel@tonic-gate 	int newstate;
222*7c478bd9Sstevel@tonic-gate 	bool call_abort;
223*7c478bd9Sstevel@tonic-gate 	sfsistat r;
224*7c478bd9Sstevel@tonic-gate 	char cmd;
225*7c478bd9Sstevel@tonic-gate 	char *buf = NULL;
226*7c478bd9Sstevel@tonic-gate 	genarg arg;
227*7c478bd9Sstevel@tonic-gate 	struct timeval timeout;
228*7c478bd9Sstevel@tonic-gate 	int (*f) __P((genarg *));
229*7c478bd9Sstevel@tonic-gate 	sfsistat (*fi_abort) __P((SMFICTX *));
230*7c478bd9Sstevel@tonic-gate 	sfsistat (*fi_close) __P((SMFICTX *));
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	arg.a_ctx = ctx;
233*7c478bd9Sstevel@tonic-gate 	sd = ctx->ctx_sd;
234*7c478bd9Sstevel@tonic-gate 	fi_abort = ctx->ctx_smfi->xxfi_abort;
235*7c478bd9Sstevel@tonic-gate 	mi_clr_macros(ctx, 0);
236*7c478bd9Sstevel@tonic-gate 	fix_stm(ctx);
237*7c478bd9Sstevel@tonic-gate 	r = _SMFIS_NONE;
238*7c478bd9Sstevel@tonic-gate 	do
239*7c478bd9Sstevel@tonic-gate 	{
240*7c478bd9Sstevel@tonic-gate 		/* call abort only if in a mail transaction */
241*7c478bd9Sstevel@tonic-gate 		call_abort = ST_IN_MAIL(curstate);
242*7c478bd9Sstevel@tonic-gate 		timeout.tv_sec = ctx->ctx_timeout;
243*7c478bd9Sstevel@tonic-gate 		timeout.tv_usec = 0;
244*7c478bd9Sstevel@tonic-gate 		if (mi_stop() == MILTER_ABRT)
245*7c478bd9Sstevel@tonic-gate 		{
246*7c478bd9Sstevel@tonic-gate 			if (ctx->ctx_dbg > 3)
247*7c478bd9Sstevel@tonic-gate 				sm_dprintf("[%d] milter_abort\n",
248*7c478bd9Sstevel@tonic-gate 					(int) ctx->ctx_id);
249*7c478bd9Sstevel@tonic-gate 			ret = MI_FAILURE;
250*7c478bd9Sstevel@tonic-gate 			break;
251*7c478bd9Sstevel@tonic-gate 		}
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate 		/*
254*7c478bd9Sstevel@tonic-gate 		**  Notice: buf is allocated by mi_rd_cmd() and it will
255*7c478bd9Sstevel@tonic-gate 		**  usually be free()d after it has been used in f().
256*7c478bd9Sstevel@tonic-gate 		**  However, if the function returns _SMFIS_KEEP then buf
257*7c478bd9Sstevel@tonic-gate 		**  contains macros and will not be free()d.
258*7c478bd9Sstevel@tonic-gate 		**  Hence r must be set to _SMFIS_NONE if a new buf is
259*7c478bd9Sstevel@tonic-gate 		**  allocated to avoid problem with housekeeping, esp.
260*7c478bd9Sstevel@tonic-gate 		**  if the code "break"s out of the loop.
261*7c478bd9Sstevel@tonic-gate 		*/
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 		r = _SMFIS_NONE;
264*7c478bd9Sstevel@tonic-gate 		if ((buf = mi_rd_cmd(sd, &timeout, &cmd, &len,
265*7c478bd9Sstevel@tonic-gate 				     ctx->ctx_smfi->xxfi_name)) == NULL &&
266*7c478bd9Sstevel@tonic-gate 		    cmd < SMFIC_VALIDCMD)
267*7c478bd9Sstevel@tonic-gate 		{
268*7c478bd9Sstevel@tonic-gate 			if (ctx->ctx_dbg > 5)
269*7c478bd9Sstevel@tonic-gate 				sm_dprintf("[%d] mi_engine: mi_rd_cmd error (%x)\n",
270*7c478bd9Sstevel@tonic-gate 					(int) ctx->ctx_id, (int) cmd);
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate 			/*
273*7c478bd9Sstevel@tonic-gate 			**  eof is currently treated as failure ->
274*7c478bd9Sstevel@tonic-gate 			**  abort() instead of close(), otherwise use:
275*7c478bd9Sstevel@tonic-gate 			**  if (cmd != SMFIC_EOF)
276*7c478bd9Sstevel@tonic-gate 			*/
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 			ret = MI_FAILURE;
279*7c478bd9Sstevel@tonic-gate 			break;
280*7c478bd9Sstevel@tonic-gate 		}
281*7c478bd9Sstevel@tonic-gate 		if (ctx->ctx_dbg > 4)
282*7c478bd9Sstevel@tonic-gate 			sm_dprintf("[%d] got cmd '%c' len %d\n",
283*7c478bd9Sstevel@tonic-gate 				(int) ctx->ctx_id, cmd, (int) len);
284*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < ncmds; i++)
285*7c478bd9Sstevel@tonic-gate 		{
286*7c478bd9Sstevel@tonic-gate 			if (cmd == cmds[i].cm_cmd)
287*7c478bd9Sstevel@tonic-gate 				break;
288*7c478bd9Sstevel@tonic-gate 		}
289*7c478bd9Sstevel@tonic-gate 		if (i >= ncmds)
290*7c478bd9Sstevel@tonic-gate 		{
291*7c478bd9Sstevel@tonic-gate 			/* unknown command */
292*7c478bd9Sstevel@tonic-gate 			if (ctx->ctx_dbg > 1)
293*7c478bd9Sstevel@tonic-gate 				sm_dprintf("[%d] cmd '%c' unknown\n",
294*7c478bd9Sstevel@tonic-gate 					(int) ctx->ctx_id, cmd);
295*7c478bd9Sstevel@tonic-gate 			ret = MI_FAILURE;
296*7c478bd9Sstevel@tonic-gate 			break;
297*7c478bd9Sstevel@tonic-gate 		}
298*7c478bd9Sstevel@tonic-gate 		if ((f = cmds[i].cm_fct) == NULL)
299*7c478bd9Sstevel@tonic-gate 		{
300*7c478bd9Sstevel@tonic-gate 			/* stop for now */
301*7c478bd9Sstevel@tonic-gate 			if (ctx->ctx_dbg > 1)
302*7c478bd9Sstevel@tonic-gate 				sm_dprintf("[%d] cmd '%c' not impl\n",
303*7c478bd9Sstevel@tonic-gate 					(int) ctx->ctx_id, cmd);
304*7c478bd9Sstevel@tonic-gate 			ret = MI_FAILURE;
305*7c478bd9Sstevel@tonic-gate 			break;
306*7c478bd9Sstevel@tonic-gate 		}
307*7c478bd9Sstevel@tonic-gate 
308*7c478bd9Sstevel@tonic-gate 		/* is new state ok? */
309*7c478bd9Sstevel@tonic-gate 		newstate = cmds[i].cm_next;
310*7c478bd9Sstevel@tonic-gate 		if (ctx->ctx_dbg > 5)
311*7c478bd9Sstevel@tonic-gate 			sm_dprintf("[%d] cur %x new %x nextmask %x\n",
312*7c478bd9Sstevel@tonic-gate 				(int) ctx->ctx_id,
313*7c478bd9Sstevel@tonic-gate 				curstate, newstate, next_states[curstate]);
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate 		if (newstate != ST_NONE && !trans_ok(curstate, newstate))
316*7c478bd9Sstevel@tonic-gate 		{
317*7c478bd9Sstevel@tonic-gate 			if (ctx->ctx_dbg > 1)
318*7c478bd9Sstevel@tonic-gate 				sm_dprintf("[%d] abort: cur %d (%x) new %d (%x) next %x\n",
319*7c478bd9Sstevel@tonic-gate 					(int) ctx->ctx_id,
320*7c478bd9Sstevel@tonic-gate 					curstate, MI_MASK(curstate),
321*7c478bd9Sstevel@tonic-gate 					newstate, MI_MASK(newstate),
322*7c478bd9Sstevel@tonic-gate 					next_states[curstate]);
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 			/* call abort only if in a mail transaction */
325*7c478bd9Sstevel@tonic-gate 			if (fi_abort != NULL && call_abort)
326*7c478bd9Sstevel@tonic-gate 				(void) (*fi_abort)(ctx);
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate 			/*
329*7c478bd9Sstevel@tonic-gate 			**  try to reach the new state from HELO
330*7c478bd9Sstevel@tonic-gate 			**  if it can't be reached, ignore the command.
331*7c478bd9Sstevel@tonic-gate 			*/
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate 			curstate = ST_HELO;
334*7c478bd9Sstevel@tonic-gate 			if (!trans_ok(curstate, newstate))
335*7c478bd9Sstevel@tonic-gate 			{
336*7c478bd9Sstevel@tonic-gate 				if (buf != NULL)
337*7c478bd9Sstevel@tonic-gate 				{
338*7c478bd9Sstevel@tonic-gate 					free(buf);
339*7c478bd9Sstevel@tonic-gate 					buf = NULL;
340*7c478bd9Sstevel@tonic-gate 				}
341*7c478bd9Sstevel@tonic-gate 				continue;
342*7c478bd9Sstevel@tonic-gate 			}
343*7c478bd9Sstevel@tonic-gate 		}
344*7c478bd9Sstevel@tonic-gate 		arg.a_len = len;
345*7c478bd9Sstevel@tonic-gate 		arg.a_buf = buf;
346*7c478bd9Sstevel@tonic-gate 		if (newstate != ST_NONE)
347*7c478bd9Sstevel@tonic-gate 		{
348*7c478bd9Sstevel@tonic-gate 			curstate = newstate;
349*7c478bd9Sstevel@tonic-gate 			ctx->ctx_state = curstate;
350*7c478bd9Sstevel@tonic-gate 		}
351*7c478bd9Sstevel@tonic-gate 		arg.a_idx = cmds[i].cm_macros;
352*7c478bd9Sstevel@tonic-gate 		call_abort = ST_IN_MAIL(curstate);
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate 		/* call function to deal with command */
355*7c478bd9Sstevel@tonic-gate 		r = (*f)(&arg);
356*7c478bd9Sstevel@tonic-gate 		if (r != _SMFIS_KEEP && buf != NULL)
357*7c478bd9Sstevel@tonic-gate 		{
358*7c478bd9Sstevel@tonic-gate 			free(buf);
359*7c478bd9Sstevel@tonic-gate 			buf = NULL;
360*7c478bd9Sstevel@tonic-gate 		}
361*7c478bd9Sstevel@tonic-gate 		if (sendreply(r, sd, &timeout, ctx) != MI_SUCCESS)
362*7c478bd9Sstevel@tonic-gate 		{
363*7c478bd9Sstevel@tonic-gate 			ret = MI_FAILURE;
364*7c478bd9Sstevel@tonic-gate 			break;
365*7c478bd9Sstevel@tonic-gate 		}
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate 		if (r == SMFIS_ACCEPT)
368*7c478bd9Sstevel@tonic-gate 		{
369*7c478bd9Sstevel@tonic-gate 			/* accept mail, no further actions taken */
370*7c478bd9Sstevel@tonic-gate 			curstate = ST_HELO;
371*7c478bd9Sstevel@tonic-gate 		}
372*7c478bd9Sstevel@tonic-gate 		else if (r == SMFIS_REJECT || r == SMFIS_DISCARD ||
373*7c478bd9Sstevel@tonic-gate 			 r ==  SMFIS_TEMPFAIL)
374*7c478bd9Sstevel@tonic-gate 		{
375*7c478bd9Sstevel@tonic-gate 			/*
376*7c478bd9Sstevel@tonic-gate 			**  further actions depend on current state
377*7c478bd9Sstevel@tonic-gate 			**  if the IGNO bit is set: "ignore" the error,
378*7c478bd9Sstevel@tonic-gate 			**  i.e., stay in the current state
379*7c478bd9Sstevel@tonic-gate 			*/
380*7c478bd9Sstevel@tonic-gate 			if (!bitset(CT_IGNO, cmds[i].cm_todo))
381*7c478bd9Sstevel@tonic-gate 				curstate = ST_HELO;
382*7c478bd9Sstevel@tonic-gate 		}
383*7c478bd9Sstevel@tonic-gate 		else if (r == _SMFIS_ABORT)
384*7c478bd9Sstevel@tonic-gate 		{
385*7c478bd9Sstevel@tonic-gate 			if (ctx->ctx_dbg > 5)
386*7c478bd9Sstevel@tonic-gate 				sm_dprintf("[%d] function returned abort\n",
387*7c478bd9Sstevel@tonic-gate 					(int) ctx->ctx_id);
388*7c478bd9Sstevel@tonic-gate 			ret = MI_FAILURE;
389*7c478bd9Sstevel@tonic-gate 			break;
390*7c478bd9Sstevel@tonic-gate 		}
391*7c478bd9Sstevel@tonic-gate 	} while (!bitset(CT_END, cmds[i].cm_todo));
392*7c478bd9Sstevel@tonic-gate 
393*7c478bd9Sstevel@tonic-gate 	if (ret != MI_SUCCESS)
394*7c478bd9Sstevel@tonic-gate 	{
395*7c478bd9Sstevel@tonic-gate 		/* call abort only if in a mail transaction */
396*7c478bd9Sstevel@tonic-gate 		if (fi_abort != NULL && call_abort)
397*7c478bd9Sstevel@tonic-gate 			(void) (*fi_abort)(ctx);
398*7c478bd9Sstevel@tonic-gate 	}
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate 	/* close must always be called */
401*7c478bd9Sstevel@tonic-gate 	if ((fi_close = ctx->ctx_smfi->xxfi_close) != NULL)
402*7c478bd9Sstevel@tonic-gate 		(void) (*fi_close)(ctx);
403*7c478bd9Sstevel@tonic-gate 	if (r != _SMFIS_KEEP && buf != NULL)
404*7c478bd9Sstevel@tonic-gate 		free(buf);
405*7c478bd9Sstevel@tonic-gate 	mi_clr_macros(ctx, 0);
406*7c478bd9Sstevel@tonic-gate 	return ret;
407*7c478bd9Sstevel@tonic-gate }
408*7c478bd9Sstevel@tonic-gate /*
409*7c478bd9Sstevel@tonic-gate **  SENDREPLY -- send a reply to the MTA
410*7c478bd9Sstevel@tonic-gate **
411*7c478bd9Sstevel@tonic-gate **	Parameters:
412*7c478bd9Sstevel@tonic-gate **		r -- reply code
413*7c478bd9Sstevel@tonic-gate **		sd -- socket descriptor
414*7c478bd9Sstevel@tonic-gate **		timeout_ptr -- (ptr to) timeout to use for sending
415*7c478bd9Sstevel@tonic-gate **		ctx -- context structure
416*7c478bd9Sstevel@tonic-gate **
417*7c478bd9Sstevel@tonic-gate **	Returns:
418*7c478bd9Sstevel@tonic-gate **		MI_SUCCESS/MI_FAILURE
419*7c478bd9Sstevel@tonic-gate */
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate static int
422*7c478bd9Sstevel@tonic-gate sendreply(r, sd, timeout_ptr, ctx)
423*7c478bd9Sstevel@tonic-gate 	sfsistat r;
424*7c478bd9Sstevel@tonic-gate 	socket_t sd;
425*7c478bd9Sstevel@tonic-gate 	struct timeval *timeout_ptr;
426*7c478bd9Sstevel@tonic-gate 	SMFICTX_PTR ctx;
427*7c478bd9Sstevel@tonic-gate {
428*7c478bd9Sstevel@tonic-gate 	int ret = MI_SUCCESS;
429*7c478bd9Sstevel@tonic-gate 
430*7c478bd9Sstevel@tonic-gate 	switch (r)
431*7c478bd9Sstevel@tonic-gate 	{
432*7c478bd9Sstevel@tonic-gate 	  case SMFIS_CONTINUE:
433*7c478bd9Sstevel@tonic-gate 		ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_CONTINUE, NULL, 0);
434*7c478bd9Sstevel@tonic-gate 		break;
435*7c478bd9Sstevel@tonic-gate 	  case SMFIS_TEMPFAIL:
436*7c478bd9Sstevel@tonic-gate 	  case SMFIS_REJECT:
437*7c478bd9Sstevel@tonic-gate 		if (ctx->ctx_reply != NULL &&
438*7c478bd9Sstevel@tonic-gate 		    ((r == SMFIS_TEMPFAIL && *ctx->ctx_reply == '4') ||
439*7c478bd9Sstevel@tonic-gate 		     (r == SMFIS_REJECT && *ctx->ctx_reply == '5')))
440*7c478bd9Sstevel@tonic-gate 		{
441*7c478bd9Sstevel@tonic-gate 			ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_REPLYCODE,
442*7c478bd9Sstevel@tonic-gate 					ctx->ctx_reply,
443*7c478bd9Sstevel@tonic-gate 					strlen(ctx->ctx_reply) + 1);
444*7c478bd9Sstevel@tonic-gate 			free(ctx->ctx_reply);
445*7c478bd9Sstevel@tonic-gate 			ctx->ctx_reply = NULL;
446*7c478bd9Sstevel@tonic-gate 		}
447*7c478bd9Sstevel@tonic-gate 		else
448*7c478bd9Sstevel@tonic-gate 		{
449*7c478bd9Sstevel@tonic-gate 			ret = mi_wr_cmd(sd, timeout_ptr, r == SMFIS_REJECT ?
450*7c478bd9Sstevel@tonic-gate 					SMFIR_REJECT : SMFIR_TEMPFAIL, NULL, 0);
451*7c478bd9Sstevel@tonic-gate 		}
452*7c478bd9Sstevel@tonic-gate 		break;
453*7c478bd9Sstevel@tonic-gate 	  case SMFIS_DISCARD:
454*7c478bd9Sstevel@tonic-gate 		ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_DISCARD, NULL, 0);
455*7c478bd9Sstevel@tonic-gate 		break;
456*7c478bd9Sstevel@tonic-gate 	  case SMFIS_ACCEPT:
457*7c478bd9Sstevel@tonic-gate 		ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_ACCEPT, NULL, 0);
458*7c478bd9Sstevel@tonic-gate 		break;
459*7c478bd9Sstevel@tonic-gate 	  case _SMFIS_OPTIONS:
460*7c478bd9Sstevel@tonic-gate 		{
461*7c478bd9Sstevel@tonic-gate 			char buf[MILTER_OPTLEN];
462*7c478bd9Sstevel@tonic-gate 			mi_int32 v;
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 			v = htonl(ctx->ctx_smfi->xxfi_version);
465*7c478bd9Sstevel@tonic-gate 			(void) memcpy(&(buf[0]), (void *) &v, MILTER_LEN_BYTES);
466*7c478bd9Sstevel@tonic-gate 			v = htonl(ctx->ctx_smfi->xxfi_flags);
467*7c478bd9Sstevel@tonic-gate 			(void) memcpy(&(buf[MILTER_LEN_BYTES]), (void *) &v,
468*7c478bd9Sstevel@tonic-gate 				      MILTER_LEN_BYTES);
469*7c478bd9Sstevel@tonic-gate 			v = htonl(ctx->ctx_pflags);
470*7c478bd9Sstevel@tonic-gate 			(void) memcpy(&(buf[MILTER_LEN_BYTES * 2]), (void *) &v,
471*7c478bd9Sstevel@tonic-gate 				      MILTER_LEN_BYTES);
472*7c478bd9Sstevel@tonic-gate 			ret = mi_wr_cmd(sd, timeout_ptr, SMFIC_OPTNEG, buf,
473*7c478bd9Sstevel@tonic-gate 				       MILTER_OPTLEN);
474*7c478bd9Sstevel@tonic-gate 		}
475*7c478bd9Sstevel@tonic-gate 		break;
476*7c478bd9Sstevel@tonic-gate 	  default:	/* don't send a reply */
477*7c478bd9Sstevel@tonic-gate 		break;
478*7c478bd9Sstevel@tonic-gate 	}
479*7c478bd9Sstevel@tonic-gate 	return ret;
480*7c478bd9Sstevel@tonic-gate }
481*7c478bd9Sstevel@tonic-gate 
482*7c478bd9Sstevel@tonic-gate /*
483*7c478bd9Sstevel@tonic-gate **  CLR_MACROS -- clear set of macros starting from a given index
484*7c478bd9Sstevel@tonic-gate **
485*7c478bd9Sstevel@tonic-gate **	Parameters:
486*7c478bd9Sstevel@tonic-gate **		ctx -- context structure
487*7c478bd9Sstevel@tonic-gate **		m -- index from which to clear all macros
488*7c478bd9Sstevel@tonic-gate **
489*7c478bd9Sstevel@tonic-gate **	Returns:
490*7c478bd9Sstevel@tonic-gate **		None.
491*7c478bd9Sstevel@tonic-gate */
492*7c478bd9Sstevel@tonic-gate void
493*7c478bd9Sstevel@tonic-gate mi_clr_macros(ctx, m)
494*7c478bd9Sstevel@tonic-gate 	SMFICTX_PTR ctx;
495*7c478bd9Sstevel@tonic-gate 	int m;
496*7c478bd9Sstevel@tonic-gate {
497*7c478bd9Sstevel@tonic-gate 	int i;
498*7c478bd9Sstevel@tonic-gate 
499*7c478bd9Sstevel@tonic-gate 	for (i = m; i < MAX_MACROS_ENTRIES; i++)
500*7c478bd9Sstevel@tonic-gate 	{
501*7c478bd9Sstevel@tonic-gate 		if (ctx->ctx_mac_ptr[i] != NULL)
502*7c478bd9Sstevel@tonic-gate 		{
503*7c478bd9Sstevel@tonic-gate 			free(ctx->ctx_mac_ptr[i]);
504*7c478bd9Sstevel@tonic-gate 			ctx->ctx_mac_ptr[i] = NULL;
505*7c478bd9Sstevel@tonic-gate 		}
506*7c478bd9Sstevel@tonic-gate 		if (ctx->ctx_mac_buf[i] != NULL)
507*7c478bd9Sstevel@tonic-gate 		{
508*7c478bd9Sstevel@tonic-gate 			free(ctx->ctx_mac_buf[i]);
509*7c478bd9Sstevel@tonic-gate 			ctx->ctx_mac_buf[i] = NULL;
510*7c478bd9Sstevel@tonic-gate 		}
511*7c478bd9Sstevel@tonic-gate 	}
512*7c478bd9Sstevel@tonic-gate }
513*7c478bd9Sstevel@tonic-gate /*
514*7c478bd9Sstevel@tonic-gate **  ST_OPTIONNEG -- negotiate options
515*7c478bd9Sstevel@tonic-gate **
516*7c478bd9Sstevel@tonic-gate **	Parameters:
517*7c478bd9Sstevel@tonic-gate **		g -- generic argument structure
518*7c478bd9Sstevel@tonic-gate **
519*7c478bd9Sstevel@tonic-gate **	Returns:
520*7c478bd9Sstevel@tonic-gate **		abort/send options/continue
521*7c478bd9Sstevel@tonic-gate */
522*7c478bd9Sstevel@tonic-gate 
523*7c478bd9Sstevel@tonic-gate static int
524*7c478bd9Sstevel@tonic-gate st_optionneg(g)
525*7c478bd9Sstevel@tonic-gate 	genarg *g;
526*7c478bd9Sstevel@tonic-gate {
527*7c478bd9Sstevel@tonic-gate 	mi_int32 i, v;
528*7c478bd9Sstevel@tonic-gate 
529*7c478bd9Sstevel@tonic-gate 	if (g == NULL || g->a_ctx->ctx_smfi == NULL)
530*7c478bd9Sstevel@tonic-gate 		return SMFIS_CONTINUE;
531*7c478bd9Sstevel@tonic-gate 	mi_clr_macros(g->a_ctx, g->a_idx + 1);
532*7c478bd9Sstevel@tonic-gate 
533*7c478bd9Sstevel@tonic-gate 	/* check for minimum length */
534*7c478bd9Sstevel@tonic-gate 	if (g->a_len < MILTER_OPTLEN)
535*7c478bd9Sstevel@tonic-gate 	{
536*7c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_ERR,
537*7c478bd9Sstevel@tonic-gate 			"%s: st_optionneg[%d]: len too short %d < %d",
538*7c478bd9Sstevel@tonic-gate 			g->a_ctx->ctx_smfi->xxfi_name,
539*7c478bd9Sstevel@tonic-gate 			(int) g->a_ctx->ctx_id, (int) g->a_len,
540*7c478bd9Sstevel@tonic-gate 			MILTER_OPTLEN);
541*7c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
542*7c478bd9Sstevel@tonic-gate 	}
543*7c478bd9Sstevel@tonic-gate 
544*7c478bd9Sstevel@tonic-gate 	(void) memcpy((void *) &i, (void *) &(g->a_buf[0]),
545*7c478bd9Sstevel@tonic-gate 		      MILTER_LEN_BYTES);
546*7c478bd9Sstevel@tonic-gate 	v = ntohl(i);
547*7c478bd9Sstevel@tonic-gate 	if (v < g->a_ctx->ctx_smfi->xxfi_version)
548*7c478bd9Sstevel@tonic-gate 	{
549*7c478bd9Sstevel@tonic-gate 		/* hard failure for now! */
550*7c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_ERR,
551*7c478bd9Sstevel@tonic-gate 			"%s: st_optionneg[%d]: version mismatch MTA: %d < milter: %d",
552*7c478bd9Sstevel@tonic-gate 			g->a_ctx->ctx_smfi->xxfi_name,
553*7c478bd9Sstevel@tonic-gate 			(int) g->a_ctx->ctx_id, (int) v,
554*7c478bd9Sstevel@tonic-gate 			g->a_ctx->ctx_smfi->xxfi_version);
555*7c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
556*7c478bd9Sstevel@tonic-gate 	}
557*7c478bd9Sstevel@tonic-gate 
558*7c478bd9Sstevel@tonic-gate 	(void) memcpy((void *) &i, (void *) &(g->a_buf[MILTER_LEN_BYTES]),
559*7c478bd9Sstevel@tonic-gate 		      MILTER_LEN_BYTES);
560*7c478bd9Sstevel@tonic-gate 	v = ntohl(i);
561*7c478bd9Sstevel@tonic-gate 
562*7c478bd9Sstevel@tonic-gate 	/* no flags? set to default value for V1 actions */
563*7c478bd9Sstevel@tonic-gate 	if (v == 0)
564*7c478bd9Sstevel@tonic-gate 		v = SMFI_V1_ACTS;
565*7c478bd9Sstevel@tonic-gate 	i = g->a_ctx->ctx_smfi->xxfi_flags;
566*7c478bd9Sstevel@tonic-gate 	if ((v & i) != i)
567*7c478bd9Sstevel@tonic-gate 	{
568*7c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_ERR,
569*7c478bd9Sstevel@tonic-gate 			"%s: st_optionneg[%d]: 0x%x does not fulfill action requirements 0x%x",
570*7c478bd9Sstevel@tonic-gate 			g->a_ctx->ctx_smfi->xxfi_name,
571*7c478bd9Sstevel@tonic-gate 			(int) g->a_ctx->ctx_id, v, i);
572*7c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
573*7c478bd9Sstevel@tonic-gate 	}
574*7c478bd9Sstevel@tonic-gate 
575*7c478bd9Sstevel@tonic-gate 	(void) memcpy((void *) &i, (void *) &(g->a_buf[MILTER_LEN_BYTES * 2]),
576*7c478bd9Sstevel@tonic-gate 		      MILTER_LEN_BYTES);
577*7c478bd9Sstevel@tonic-gate 	v = ntohl(i);
578*7c478bd9Sstevel@tonic-gate 
579*7c478bd9Sstevel@tonic-gate 	/* no flags? set to default value for V1 protocol */
580*7c478bd9Sstevel@tonic-gate 	if (v == 0)
581*7c478bd9Sstevel@tonic-gate 		v = SMFI_V1_PROT;
582*7c478bd9Sstevel@tonic-gate 	i = g->a_ctx->ctx_pflags;
583*7c478bd9Sstevel@tonic-gate 	if ((v & i) != i)
584*7c478bd9Sstevel@tonic-gate 	{
585*7c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_ERR,
586*7c478bd9Sstevel@tonic-gate 			"%s: st_optionneg[%d]: 0x%x does not fulfill protocol requirements 0x%x",
587*7c478bd9Sstevel@tonic-gate 			g->a_ctx->ctx_smfi->xxfi_name,
588*7c478bd9Sstevel@tonic-gate 			(int) g->a_ctx->ctx_id, v, i);
589*7c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
590*7c478bd9Sstevel@tonic-gate 	}
591*7c478bd9Sstevel@tonic-gate 
592*7c478bd9Sstevel@tonic-gate 	return _SMFIS_OPTIONS;
593*7c478bd9Sstevel@tonic-gate }
594*7c478bd9Sstevel@tonic-gate /*
595*7c478bd9Sstevel@tonic-gate **  ST_CONNECTINFO -- receive connection information
596*7c478bd9Sstevel@tonic-gate **
597*7c478bd9Sstevel@tonic-gate **	Parameters:
598*7c478bd9Sstevel@tonic-gate **		g -- generic argument structure
599*7c478bd9Sstevel@tonic-gate **
600*7c478bd9Sstevel@tonic-gate **	Returns:
601*7c478bd9Sstevel@tonic-gate **		continue or filter-specified value
602*7c478bd9Sstevel@tonic-gate */
603*7c478bd9Sstevel@tonic-gate 
604*7c478bd9Sstevel@tonic-gate static int
605*7c478bd9Sstevel@tonic-gate st_connectinfo(g)
606*7c478bd9Sstevel@tonic-gate 	genarg *g;
607*7c478bd9Sstevel@tonic-gate {
608*7c478bd9Sstevel@tonic-gate 	size_t l;
609*7c478bd9Sstevel@tonic-gate 	size_t i;
610*7c478bd9Sstevel@tonic-gate 	char *s, family;
611*7c478bd9Sstevel@tonic-gate 	unsigned short port = 0;
612*7c478bd9Sstevel@tonic-gate 	_SOCK_ADDR sockaddr;
613*7c478bd9Sstevel@tonic-gate 	sfsistat (*fi_connect) __P((SMFICTX *, char *, _SOCK_ADDR *));
614*7c478bd9Sstevel@tonic-gate 
615*7c478bd9Sstevel@tonic-gate 	if (g == NULL)
616*7c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
617*7c478bd9Sstevel@tonic-gate 	mi_clr_macros(g->a_ctx, g->a_idx + 1);
618*7c478bd9Sstevel@tonic-gate 	if (g->a_ctx->ctx_smfi == NULL ||
619*7c478bd9Sstevel@tonic-gate 	    (fi_connect = g->a_ctx->ctx_smfi->xxfi_connect) == NULL)
620*7c478bd9Sstevel@tonic-gate 		return SMFIS_CONTINUE;
621*7c478bd9Sstevel@tonic-gate 
622*7c478bd9Sstevel@tonic-gate 	s = g->a_buf;
623*7c478bd9Sstevel@tonic-gate 	i = 0;
624*7c478bd9Sstevel@tonic-gate 	l = g->a_len;
625*7c478bd9Sstevel@tonic-gate 	while (s[i] != '\0' && i <= l)
626*7c478bd9Sstevel@tonic-gate 		++i;
627*7c478bd9Sstevel@tonic-gate 	if (i + 1 >= l)
628*7c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
629*7c478bd9Sstevel@tonic-gate 
630*7c478bd9Sstevel@tonic-gate 	/* Move past trailing \0 in host string */
631*7c478bd9Sstevel@tonic-gate 	i++;
632*7c478bd9Sstevel@tonic-gate 	family = s[i++];
633*7c478bd9Sstevel@tonic-gate 	(void) memset(&sockaddr, '\0', sizeof sockaddr);
634*7c478bd9Sstevel@tonic-gate 	if (family != SMFIA_UNKNOWN)
635*7c478bd9Sstevel@tonic-gate 	{
636*7c478bd9Sstevel@tonic-gate 		if (i + sizeof port >= l)
637*7c478bd9Sstevel@tonic-gate 		{
638*7c478bd9Sstevel@tonic-gate 			smi_log(SMI_LOG_ERR,
639*7c478bd9Sstevel@tonic-gate 				"%s: connect[%d]: wrong len %d >= %d",
640*7c478bd9Sstevel@tonic-gate 				g->a_ctx->ctx_smfi->xxfi_name,
641*7c478bd9Sstevel@tonic-gate 				(int) g->a_ctx->ctx_id, (int) i, (int) l);
642*7c478bd9Sstevel@tonic-gate 			return _SMFIS_ABORT;
643*7c478bd9Sstevel@tonic-gate 		}
644*7c478bd9Sstevel@tonic-gate 		(void) memcpy((void *) &port, (void *) (s + i),
645*7c478bd9Sstevel@tonic-gate 			      sizeof port);
646*7c478bd9Sstevel@tonic-gate 		i += sizeof port;
647*7c478bd9Sstevel@tonic-gate 
648*7c478bd9Sstevel@tonic-gate 		/* make sure string is terminated */
649*7c478bd9Sstevel@tonic-gate 		if (s[l - 1] != '\0')
650*7c478bd9Sstevel@tonic-gate 			return _SMFIS_ABORT;
651*7c478bd9Sstevel@tonic-gate # if NETINET
652*7c478bd9Sstevel@tonic-gate 		if (family == SMFIA_INET)
653*7c478bd9Sstevel@tonic-gate 		{
654*7c478bd9Sstevel@tonic-gate 			if (inet_aton(s + i, (struct in_addr *) &sockaddr.sin.sin_addr)
655*7c478bd9Sstevel@tonic-gate 			    != 1)
656*7c478bd9Sstevel@tonic-gate 			{
657*7c478bd9Sstevel@tonic-gate 				smi_log(SMI_LOG_ERR,
658*7c478bd9Sstevel@tonic-gate 					"%s: connect[%d]: inet_aton failed",
659*7c478bd9Sstevel@tonic-gate 					g->a_ctx->ctx_smfi->xxfi_name,
660*7c478bd9Sstevel@tonic-gate 					(int) g->a_ctx->ctx_id);
661*7c478bd9Sstevel@tonic-gate 				return _SMFIS_ABORT;
662*7c478bd9Sstevel@tonic-gate 			}
663*7c478bd9Sstevel@tonic-gate 			sockaddr.sa.sa_family = AF_INET;
664*7c478bd9Sstevel@tonic-gate 			if (port > 0)
665*7c478bd9Sstevel@tonic-gate 				sockaddr.sin.sin_port = port;
666*7c478bd9Sstevel@tonic-gate 		}
667*7c478bd9Sstevel@tonic-gate 		else
668*7c478bd9Sstevel@tonic-gate # endif /* NETINET */
669*7c478bd9Sstevel@tonic-gate # if NETINET6
670*7c478bd9Sstevel@tonic-gate 		if (family == SMFIA_INET6)
671*7c478bd9Sstevel@tonic-gate 		{
672*7c478bd9Sstevel@tonic-gate 			if (mi_inet_pton(AF_INET6, s + i,
673*7c478bd9Sstevel@tonic-gate 					 &sockaddr.sin6.sin6_addr) != 1)
674*7c478bd9Sstevel@tonic-gate 			{
675*7c478bd9Sstevel@tonic-gate 				smi_log(SMI_LOG_ERR,
676*7c478bd9Sstevel@tonic-gate 					"%s: connect[%d]: mi_inet_pton failed",
677*7c478bd9Sstevel@tonic-gate 					g->a_ctx->ctx_smfi->xxfi_name,
678*7c478bd9Sstevel@tonic-gate 					(int) g->a_ctx->ctx_id);
679*7c478bd9Sstevel@tonic-gate 				return _SMFIS_ABORT;
680*7c478bd9Sstevel@tonic-gate 			}
681*7c478bd9Sstevel@tonic-gate 			sockaddr.sa.sa_family = AF_INET6;
682*7c478bd9Sstevel@tonic-gate 			if (port > 0)
683*7c478bd9Sstevel@tonic-gate 				sockaddr.sin6.sin6_port = port;
684*7c478bd9Sstevel@tonic-gate 		}
685*7c478bd9Sstevel@tonic-gate 		else
686*7c478bd9Sstevel@tonic-gate # endif /* NETINET6 */
687*7c478bd9Sstevel@tonic-gate # if NETUNIX
688*7c478bd9Sstevel@tonic-gate 		if (family == SMFIA_UNIX)
689*7c478bd9Sstevel@tonic-gate 		{
690*7c478bd9Sstevel@tonic-gate 			if (sm_strlcpy(sockaddr.sunix.sun_path, s + i,
691*7c478bd9Sstevel@tonic-gate 			    sizeof sockaddr.sunix.sun_path) >=
692*7c478bd9Sstevel@tonic-gate 			    sizeof sockaddr.sunix.sun_path)
693*7c478bd9Sstevel@tonic-gate 			{
694*7c478bd9Sstevel@tonic-gate 				smi_log(SMI_LOG_ERR,
695*7c478bd9Sstevel@tonic-gate 					"%s: connect[%d]: path too long",
696*7c478bd9Sstevel@tonic-gate 					g->a_ctx->ctx_smfi->xxfi_name,
697*7c478bd9Sstevel@tonic-gate 					(int) g->a_ctx->ctx_id);
698*7c478bd9Sstevel@tonic-gate 				return _SMFIS_ABORT;
699*7c478bd9Sstevel@tonic-gate 			}
700*7c478bd9Sstevel@tonic-gate 			sockaddr.sunix.sun_family = AF_UNIX;
701*7c478bd9Sstevel@tonic-gate 		}
702*7c478bd9Sstevel@tonic-gate 		else
703*7c478bd9Sstevel@tonic-gate # endif /* NETUNIX */
704*7c478bd9Sstevel@tonic-gate 		{
705*7c478bd9Sstevel@tonic-gate 			smi_log(SMI_LOG_ERR,
706*7c478bd9Sstevel@tonic-gate 				"%s: connect[%d]: unknown family %d",
707*7c478bd9Sstevel@tonic-gate 				g->a_ctx->ctx_smfi->xxfi_name,
708*7c478bd9Sstevel@tonic-gate 				(int) g->a_ctx->ctx_id, family);
709*7c478bd9Sstevel@tonic-gate 			return _SMFIS_ABORT;
710*7c478bd9Sstevel@tonic-gate 		}
711*7c478bd9Sstevel@tonic-gate 	}
712*7c478bd9Sstevel@tonic-gate 	return (*fi_connect)(g->a_ctx, g->a_buf,
713*7c478bd9Sstevel@tonic-gate 			     family != SMFIA_UNKNOWN ? &sockaddr : NULL);
714*7c478bd9Sstevel@tonic-gate }
715*7c478bd9Sstevel@tonic-gate 
716*7c478bd9Sstevel@tonic-gate /*
717*7c478bd9Sstevel@tonic-gate **  ST_EOH -- end of headers
718*7c478bd9Sstevel@tonic-gate **
719*7c478bd9Sstevel@tonic-gate **	Parameters:
720*7c478bd9Sstevel@tonic-gate **		g -- generic argument structure
721*7c478bd9Sstevel@tonic-gate **
722*7c478bd9Sstevel@tonic-gate **	Returns:
723*7c478bd9Sstevel@tonic-gate **		continue or filter-specified value
724*7c478bd9Sstevel@tonic-gate */
725*7c478bd9Sstevel@tonic-gate 
726*7c478bd9Sstevel@tonic-gate static int
727*7c478bd9Sstevel@tonic-gate st_eoh(g)
728*7c478bd9Sstevel@tonic-gate 	genarg *g;
729*7c478bd9Sstevel@tonic-gate {
730*7c478bd9Sstevel@tonic-gate 	sfsistat (*fi_eoh) __P((SMFICTX *));
731*7c478bd9Sstevel@tonic-gate 
732*7c478bd9Sstevel@tonic-gate 	if (g == NULL)
733*7c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
734*7c478bd9Sstevel@tonic-gate 	if (g->a_ctx->ctx_smfi != NULL &&
735*7c478bd9Sstevel@tonic-gate 	    (fi_eoh = g->a_ctx->ctx_smfi->xxfi_eoh) != NULL)
736*7c478bd9Sstevel@tonic-gate 		return (*fi_eoh)(g->a_ctx);
737*7c478bd9Sstevel@tonic-gate 	return SMFIS_CONTINUE;
738*7c478bd9Sstevel@tonic-gate }
739*7c478bd9Sstevel@tonic-gate 
740*7c478bd9Sstevel@tonic-gate #if SMFI_VERSION > 3
741*7c478bd9Sstevel@tonic-gate /*
742*7c478bd9Sstevel@tonic-gate **  ST_DATA -- DATA command
743*7c478bd9Sstevel@tonic-gate **
744*7c478bd9Sstevel@tonic-gate **	Parameters:
745*7c478bd9Sstevel@tonic-gate **		g -- generic argument structure
746*7c478bd9Sstevel@tonic-gate **
747*7c478bd9Sstevel@tonic-gate **	Returns:
748*7c478bd9Sstevel@tonic-gate **		continue or filter-specified value
749*7c478bd9Sstevel@tonic-gate */
750*7c478bd9Sstevel@tonic-gate 
751*7c478bd9Sstevel@tonic-gate static int
752*7c478bd9Sstevel@tonic-gate st_data(g)
753*7c478bd9Sstevel@tonic-gate 	genarg *g;
754*7c478bd9Sstevel@tonic-gate {
755*7c478bd9Sstevel@tonic-gate 	sfsistat (*fi_data) __P((SMFICTX *));
756*7c478bd9Sstevel@tonic-gate 
757*7c478bd9Sstevel@tonic-gate 	if (g == NULL)
758*7c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
759*7c478bd9Sstevel@tonic-gate 	if (g->a_ctx->ctx_smfi != NULL &&
760*7c478bd9Sstevel@tonic-gate 	    (fi_data = g->a_ctx->ctx_smfi->xxfi_data) != NULL)
761*7c478bd9Sstevel@tonic-gate 		return (*fi_data)(g->a_ctx);
762*7c478bd9Sstevel@tonic-gate 	return SMFIS_CONTINUE;
763*7c478bd9Sstevel@tonic-gate }
764*7c478bd9Sstevel@tonic-gate #endif /* SMFI_VERSION > 3 */
765*7c478bd9Sstevel@tonic-gate 
766*7c478bd9Sstevel@tonic-gate /*
767*7c478bd9Sstevel@tonic-gate **  ST_HELO -- helo/ehlo command
768*7c478bd9Sstevel@tonic-gate **
769*7c478bd9Sstevel@tonic-gate **	Parameters:
770*7c478bd9Sstevel@tonic-gate **		g -- generic argument structure
771*7c478bd9Sstevel@tonic-gate **
772*7c478bd9Sstevel@tonic-gate **	Returns:
773*7c478bd9Sstevel@tonic-gate **		continue or filter-specified value
774*7c478bd9Sstevel@tonic-gate */
775*7c478bd9Sstevel@tonic-gate static int
776*7c478bd9Sstevel@tonic-gate st_helo(g)
777*7c478bd9Sstevel@tonic-gate 	genarg *g;
778*7c478bd9Sstevel@tonic-gate {
779*7c478bd9Sstevel@tonic-gate 	sfsistat (*fi_helo) __P((SMFICTX *, char *));
780*7c478bd9Sstevel@tonic-gate 
781*7c478bd9Sstevel@tonic-gate 	if (g == NULL)
782*7c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
783*7c478bd9Sstevel@tonic-gate 	mi_clr_macros(g->a_ctx, g->a_idx + 1);
784*7c478bd9Sstevel@tonic-gate 	if (g->a_ctx->ctx_smfi != NULL &&
785*7c478bd9Sstevel@tonic-gate 	    (fi_helo = g->a_ctx->ctx_smfi->xxfi_helo) != NULL)
786*7c478bd9Sstevel@tonic-gate 	{
787*7c478bd9Sstevel@tonic-gate 		/* paranoia: check for terminating '\0' */
788*7c478bd9Sstevel@tonic-gate 		if (g->a_len == 0 || g->a_buf[g->a_len - 1] != '\0')
789*7c478bd9Sstevel@tonic-gate 			return MI_FAILURE;
790*7c478bd9Sstevel@tonic-gate 		return (*fi_helo)(g->a_ctx, g->a_buf);
791*7c478bd9Sstevel@tonic-gate 	}
792*7c478bd9Sstevel@tonic-gate 	return SMFIS_CONTINUE;
793*7c478bd9Sstevel@tonic-gate }
794*7c478bd9Sstevel@tonic-gate /*
795*7c478bd9Sstevel@tonic-gate **  ST_HEADER -- header line
796*7c478bd9Sstevel@tonic-gate **
797*7c478bd9Sstevel@tonic-gate **	Parameters:
798*7c478bd9Sstevel@tonic-gate **		g -- generic argument structure
799*7c478bd9Sstevel@tonic-gate **
800*7c478bd9Sstevel@tonic-gate **	Returns:
801*7c478bd9Sstevel@tonic-gate **		continue or filter-specified value
802*7c478bd9Sstevel@tonic-gate */
803*7c478bd9Sstevel@tonic-gate 
804*7c478bd9Sstevel@tonic-gate static int
805*7c478bd9Sstevel@tonic-gate st_header(g)
806*7c478bd9Sstevel@tonic-gate 	genarg *g;
807*7c478bd9Sstevel@tonic-gate {
808*7c478bd9Sstevel@tonic-gate 	char *hf, *hv;
809*7c478bd9Sstevel@tonic-gate 	sfsistat (*fi_header) __P((SMFICTX *, char *, char *));
810*7c478bd9Sstevel@tonic-gate 
811*7c478bd9Sstevel@tonic-gate 	if (g == NULL)
812*7c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
813*7c478bd9Sstevel@tonic-gate 	if (g->a_ctx->ctx_smfi == NULL ||
814*7c478bd9Sstevel@tonic-gate 	    (fi_header = g->a_ctx->ctx_smfi->xxfi_header) == NULL)
815*7c478bd9Sstevel@tonic-gate 		return SMFIS_CONTINUE;
816*7c478bd9Sstevel@tonic-gate 	if (dec_arg2(g->a_buf, g->a_len, &hf, &hv) == MI_SUCCESS)
817*7c478bd9Sstevel@tonic-gate 		return (*fi_header)(g->a_ctx, hf, hv);
818*7c478bd9Sstevel@tonic-gate 	else
819*7c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
820*7c478bd9Sstevel@tonic-gate }
821*7c478bd9Sstevel@tonic-gate 
822*7c478bd9Sstevel@tonic-gate #define ARGV_FCT(lf, rf, idx)					\
823*7c478bd9Sstevel@tonic-gate 	char **argv;						\
824*7c478bd9Sstevel@tonic-gate 	sfsistat (*lf) __P((SMFICTX *, char **));		\
825*7c478bd9Sstevel@tonic-gate 	int r;							\
826*7c478bd9Sstevel@tonic-gate 								\
827*7c478bd9Sstevel@tonic-gate 	if (g == NULL)						\
828*7c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;				\
829*7c478bd9Sstevel@tonic-gate 	mi_clr_macros(g->a_ctx, g->a_idx + 1);			\
830*7c478bd9Sstevel@tonic-gate 	if (g->a_ctx->ctx_smfi == NULL ||			\
831*7c478bd9Sstevel@tonic-gate 	    (lf = g->a_ctx->ctx_smfi->rf) == NULL)		\
832*7c478bd9Sstevel@tonic-gate 		return SMFIS_CONTINUE;				\
833*7c478bd9Sstevel@tonic-gate 	if ((argv = dec_argv(g->a_buf, g->a_len)) == NULL)	\
834*7c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;				\
835*7c478bd9Sstevel@tonic-gate 	r = (*lf)(g->a_ctx, argv);				\
836*7c478bd9Sstevel@tonic-gate 	free(argv);						\
837*7c478bd9Sstevel@tonic-gate 	return r;
838*7c478bd9Sstevel@tonic-gate 
839*7c478bd9Sstevel@tonic-gate /*
840*7c478bd9Sstevel@tonic-gate **  ST_SENDER -- MAIL FROM command
841*7c478bd9Sstevel@tonic-gate **
842*7c478bd9Sstevel@tonic-gate **	Parameters:
843*7c478bd9Sstevel@tonic-gate **		g -- generic argument structure
844*7c478bd9Sstevel@tonic-gate **
845*7c478bd9Sstevel@tonic-gate **	Returns:
846*7c478bd9Sstevel@tonic-gate **		continue or filter-specified value
847*7c478bd9Sstevel@tonic-gate */
848*7c478bd9Sstevel@tonic-gate 
849*7c478bd9Sstevel@tonic-gate static int
850*7c478bd9Sstevel@tonic-gate st_sender(g)
851*7c478bd9Sstevel@tonic-gate 	genarg *g;
852*7c478bd9Sstevel@tonic-gate {
853*7c478bd9Sstevel@tonic-gate 	ARGV_FCT(fi_envfrom, xxfi_envfrom, CI_MAIL)
854*7c478bd9Sstevel@tonic-gate }
855*7c478bd9Sstevel@tonic-gate /*
856*7c478bd9Sstevel@tonic-gate **  ST_RCPT -- RCPT TO command
857*7c478bd9Sstevel@tonic-gate **
858*7c478bd9Sstevel@tonic-gate **	Parameters:
859*7c478bd9Sstevel@tonic-gate **		g -- generic argument structure
860*7c478bd9Sstevel@tonic-gate **
861*7c478bd9Sstevel@tonic-gate **	Returns:
862*7c478bd9Sstevel@tonic-gate **		continue or filter-specified value
863*7c478bd9Sstevel@tonic-gate */
864*7c478bd9Sstevel@tonic-gate 
865*7c478bd9Sstevel@tonic-gate static int
866*7c478bd9Sstevel@tonic-gate st_rcpt(g)
867*7c478bd9Sstevel@tonic-gate 	genarg *g;
868*7c478bd9Sstevel@tonic-gate {
869*7c478bd9Sstevel@tonic-gate 	ARGV_FCT(fi_envrcpt, xxfi_envrcpt, CI_RCPT)
870*7c478bd9Sstevel@tonic-gate }
871*7c478bd9Sstevel@tonic-gate 
872*7c478bd9Sstevel@tonic-gate #if SMFI_VERSION > 2
873*7c478bd9Sstevel@tonic-gate /*
874*7c478bd9Sstevel@tonic-gate **  ST_UNKNOWN -- unrecognized or unimplemented command
875*7c478bd9Sstevel@tonic-gate **
876*7c478bd9Sstevel@tonic-gate **	Parameters:
877*7c478bd9Sstevel@tonic-gate **		g -- generic argument structure
878*7c478bd9Sstevel@tonic-gate **
879*7c478bd9Sstevel@tonic-gate **	Returns:
880*7c478bd9Sstevel@tonic-gate **		continue or filter-specified value
881*7c478bd9Sstevel@tonic-gate */
882*7c478bd9Sstevel@tonic-gate 
883*7c478bd9Sstevel@tonic-gate static int
884*7c478bd9Sstevel@tonic-gate st_unknown(g)
885*7c478bd9Sstevel@tonic-gate 	genarg *g;
886*7c478bd9Sstevel@tonic-gate {
887*7c478bd9Sstevel@tonic-gate 	sfsistat (*fi_unknown) __P((SMFICTX *, char *));
888*7c478bd9Sstevel@tonic-gate 
889*7c478bd9Sstevel@tonic-gate 	if (g == NULL)
890*7c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
891*7c478bd9Sstevel@tonic-gate 	mi_clr_macros(g->a_ctx, g->a_idx + 1);
892*7c478bd9Sstevel@tonic-gate 	if (g->a_ctx->ctx_smfi != NULL &&
893*7c478bd9Sstevel@tonic-gate 	    (fi_unknown = g->a_ctx->ctx_smfi->xxfi_unknown) != NULL)
894*7c478bd9Sstevel@tonic-gate 		return (*fi_unknown)(g->a_ctx, g->a_buf);
895*7c478bd9Sstevel@tonic-gate 	return SMFIS_CONTINUE;
896*7c478bd9Sstevel@tonic-gate }
897*7c478bd9Sstevel@tonic-gate #endif /* SMFI_VERSION > 2 */
898*7c478bd9Sstevel@tonic-gate 
899*7c478bd9Sstevel@tonic-gate /*
900*7c478bd9Sstevel@tonic-gate **  ST_MACROS -- deal with macros received from the MTA
901*7c478bd9Sstevel@tonic-gate **
902*7c478bd9Sstevel@tonic-gate **	Parameters:
903*7c478bd9Sstevel@tonic-gate **		g -- generic argument structure
904*7c478bd9Sstevel@tonic-gate **
905*7c478bd9Sstevel@tonic-gate **	Returns:
906*7c478bd9Sstevel@tonic-gate **		continue/keep
907*7c478bd9Sstevel@tonic-gate **
908*7c478bd9Sstevel@tonic-gate **	Side effects:
909*7c478bd9Sstevel@tonic-gate **		set pointer in macro array to current values.
910*7c478bd9Sstevel@tonic-gate */
911*7c478bd9Sstevel@tonic-gate 
912*7c478bd9Sstevel@tonic-gate static int
913*7c478bd9Sstevel@tonic-gate st_macros(g)
914*7c478bd9Sstevel@tonic-gate 	genarg *g;
915*7c478bd9Sstevel@tonic-gate {
916*7c478bd9Sstevel@tonic-gate 	int i;
917*7c478bd9Sstevel@tonic-gate 	char **argv;
918*7c478bd9Sstevel@tonic-gate 
919*7c478bd9Sstevel@tonic-gate 	if (g == NULL || g->a_len < 1)
920*7c478bd9Sstevel@tonic-gate 		return _SMFIS_FAIL;
921*7c478bd9Sstevel@tonic-gate 	if ((argv = dec_argv(g->a_buf + 1, g->a_len - 1)) == NULL)
922*7c478bd9Sstevel@tonic-gate 		return _SMFIS_FAIL;
923*7c478bd9Sstevel@tonic-gate 	switch (g->a_buf[0])
924*7c478bd9Sstevel@tonic-gate 	{
925*7c478bd9Sstevel@tonic-gate 	  case SMFIC_CONNECT:
926*7c478bd9Sstevel@tonic-gate 		i = CI_CONN;
927*7c478bd9Sstevel@tonic-gate 		break;
928*7c478bd9Sstevel@tonic-gate 	  case SMFIC_HELO:
929*7c478bd9Sstevel@tonic-gate 		i = CI_HELO;
930*7c478bd9Sstevel@tonic-gate 		break;
931*7c478bd9Sstevel@tonic-gate 	  case SMFIC_MAIL:
932*7c478bd9Sstevel@tonic-gate 		i = CI_MAIL;
933*7c478bd9Sstevel@tonic-gate 		break;
934*7c478bd9Sstevel@tonic-gate 	  case SMFIC_RCPT:
935*7c478bd9Sstevel@tonic-gate 		i = CI_RCPT;
936*7c478bd9Sstevel@tonic-gate 		break;
937*7c478bd9Sstevel@tonic-gate 	  case SMFIC_BODYEOB:
938*7c478bd9Sstevel@tonic-gate 		i = CI_EOM;
939*7c478bd9Sstevel@tonic-gate 		break;
940*7c478bd9Sstevel@tonic-gate 	  default:
941*7c478bd9Sstevel@tonic-gate 		free(argv);
942*7c478bd9Sstevel@tonic-gate 		return _SMFIS_FAIL;
943*7c478bd9Sstevel@tonic-gate 	}
944*7c478bd9Sstevel@tonic-gate 	if (g->a_ctx->ctx_mac_ptr[i] != NULL)
945*7c478bd9Sstevel@tonic-gate 		free(g->a_ctx->ctx_mac_ptr[i]);
946*7c478bd9Sstevel@tonic-gate 	if (g->a_ctx->ctx_mac_buf[i] != NULL)
947*7c478bd9Sstevel@tonic-gate 		free(g->a_ctx->ctx_mac_buf[i]);
948*7c478bd9Sstevel@tonic-gate 	g->a_ctx->ctx_mac_ptr[i] = argv;
949*7c478bd9Sstevel@tonic-gate 	g->a_ctx->ctx_mac_buf[i] = g->a_buf;
950*7c478bd9Sstevel@tonic-gate 	return _SMFIS_KEEP;
951*7c478bd9Sstevel@tonic-gate }
952*7c478bd9Sstevel@tonic-gate /*
953*7c478bd9Sstevel@tonic-gate **  ST_QUIT -- quit command
954*7c478bd9Sstevel@tonic-gate **
955*7c478bd9Sstevel@tonic-gate **	Parameters:
956*7c478bd9Sstevel@tonic-gate **		g -- generic argument structure
957*7c478bd9Sstevel@tonic-gate **
958*7c478bd9Sstevel@tonic-gate **	Returns:
959*7c478bd9Sstevel@tonic-gate **		noreply
960*7c478bd9Sstevel@tonic-gate */
961*7c478bd9Sstevel@tonic-gate 
962*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
963*7c478bd9Sstevel@tonic-gate static int
964*7c478bd9Sstevel@tonic-gate st_quit(g)
965*7c478bd9Sstevel@tonic-gate 	genarg *g;
966*7c478bd9Sstevel@tonic-gate {
967*7c478bd9Sstevel@tonic-gate 	return _SMFIS_NOREPLY;
968*7c478bd9Sstevel@tonic-gate }
969*7c478bd9Sstevel@tonic-gate /*
970*7c478bd9Sstevel@tonic-gate **  ST_BODYCHUNK -- deal with a piece of the mail body
971*7c478bd9Sstevel@tonic-gate **
972*7c478bd9Sstevel@tonic-gate **	Parameters:
973*7c478bd9Sstevel@tonic-gate **		g -- generic argument structure
974*7c478bd9Sstevel@tonic-gate **
975*7c478bd9Sstevel@tonic-gate **	Returns:
976*7c478bd9Sstevel@tonic-gate **		continue or filter-specified value
977*7c478bd9Sstevel@tonic-gate */
978*7c478bd9Sstevel@tonic-gate 
979*7c478bd9Sstevel@tonic-gate static int
980*7c478bd9Sstevel@tonic-gate st_bodychunk(g)
981*7c478bd9Sstevel@tonic-gate 	genarg *g;
982*7c478bd9Sstevel@tonic-gate {
983*7c478bd9Sstevel@tonic-gate 	sfsistat (*fi_body) __P((SMFICTX *, unsigned char *, size_t));
984*7c478bd9Sstevel@tonic-gate 
985*7c478bd9Sstevel@tonic-gate 	if (g == NULL)
986*7c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
987*7c478bd9Sstevel@tonic-gate 	if (g->a_ctx->ctx_smfi != NULL &&
988*7c478bd9Sstevel@tonic-gate 	    (fi_body = g->a_ctx->ctx_smfi->xxfi_body) != NULL)
989*7c478bd9Sstevel@tonic-gate 		return (*fi_body)(g->a_ctx, (unsigned char *)g->a_buf,
990*7c478bd9Sstevel@tonic-gate 				  g->a_len);
991*7c478bd9Sstevel@tonic-gate 	return SMFIS_CONTINUE;
992*7c478bd9Sstevel@tonic-gate }
993*7c478bd9Sstevel@tonic-gate /*
994*7c478bd9Sstevel@tonic-gate **  ST_BODYEND -- deal with the last piece of the mail body
995*7c478bd9Sstevel@tonic-gate **
996*7c478bd9Sstevel@tonic-gate **	Parameters:
997*7c478bd9Sstevel@tonic-gate **		g -- generic argument structure
998*7c478bd9Sstevel@tonic-gate **
999*7c478bd9Sstevel@tonic-gate **	Returns:
1000*7c478bd9Sstevel@tonic-gate **		continue or filter-specified value
1001*7c478bd9Sstevel@tonic-gate **
1002*7c478bd9Sstevel@tonic-gate **	Side effects:
1003*7c478bd9Sstevel@tonic-gate **		sends a reply for the body part (if non-empty).
1004*7c478bd9Sstevel@tonic-gate */
1005*7c478bd9Sstevel@tonic-gate 
1006*7c478bd9Sstevel@tonic-gate static int
1007*7c478bd9Sstevel@tonic-gate st_bodyend(g)
1008*7c478bd9Sstevel@tonic-gate 	genarg *g;
1009*7c478bd9Sstevel@tonic-gate {
1010*7c478bd9Sstevel@tonic-gate 	sfsistat r;
1011*7c478bd9Sstevel@tonic-gate 	sfsistat (*fi_body) __P((SMFICTX *, unsigned char *, size_t));
1012*7c478bd9Sstevel@tonic-gate 	sfsistat (*fi_eom) __P((SMFICTX *));
1013*7c478bd9Sstevel@tonic-gate 
1014*7c478bd9Sstevel@tonic-gate 	if (g == NULL)
1015*7c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
1016*7c478bd9Sstevel@tonic-gate 	r = SMFIS_CONTINUE;
1017*7c478bd9Sstevel@tonic-gate 	if (g->a_ctx->ctx_smfi != NULL)
1018*7c478bd9Sstevel@tonic-gate 	{
1019*7c478bd9Sstevel@tonic-gate 		if ((fi_body = g->a_ctx->ctx_smfi->xxfi_body) != NULL &&
1020*7c478bd9Sstevel@tonic-gate 		    g->a_len > 0)
1021*7c478bd9Sstevel@tonic-gate 		{
1022*7c478bd9Sstevel@tonic-gate 			socket_t sd;
1023*7c478bd9Sstevel@tonic-gate 			struct timeval timeout;
1024*7c478bd9Sstevel@tonic-gate 
1025*7c478bd9Sstevel@tonic-gate 			timeout.tv_sec = g->a_ctx->ctx_timeout;
1026*7c478bd9Sstevel@tonic-gate 			timeout.tv_usec = 0;
1027*7c478bd9Sstevel@tonic-gate 			sd = g->a_ctx->ctx_sd;
1028*7c478bd9Sstevel@tonic-gate 			r = (*fi_body)(g->a_ctx, (unsigned char *)g->a_buf,
1029*7c478bd9Sstevel@tonic-gate 				       g->a_len);
1030*7c478bd9Sstevel@tonic-gate 			if (r != SMFIS_CONTINUE &&
1031*7c478bd9Sstevel@tonic-gate 			    sendreply(r, sd, &timeout, g->a_ctx) != MI_SUCCESS)
1032*7c478bd9Sstevel@tonic-gate 				return _SMFIS_ABORT;
1033*7c478bd9Sstevel@tonic-gate 		}
1034*7c478bd9Sstevel@tonic-gate 	}
1035*7c478bd9Sstevel@tonic-gate 	if (r == SMFIS_CONTINUE &&
1036*7c478bd9Sstevel@tonic-gate 	    (fi_eom = g->a_ctx->ctx_smfi->xxfi_eom) != NULL)
1037*7c478bd9Sstevel@tonic-gate 		return (*fi_eom)(g->a_ctx);
1038*7c478bd9Sstevel@tonic-gate 	return r;
1039*7c478bd9Sstevel@tonic-gate }
1040*7c478bd9Sstevel@tonic-gate /*
1041*7c478bd9Sstevel@tonic-gate **  ST_ABORTFCT -- deal with aborts
1042*7c478bd9Sstevel@tonic-gate **
1043*7c478bd9Sstevel@tonic-gate **	Parameters:
1044*7c478bd9Sstevel@tonic-gate **		g -- generic argument structure
1045*7c478bd9Sstevel@tonic-gate **
1046*7c478bd9Sstevel@tonic-gate **	Returns:
1047*7c478bd9Sstevel@tonic-gate **		abort or filter-specified value
1048*7c478bd9Sstevel@tonic-gate */
1049*7c478bd9Sstevel@tonic-gate 
1050*7c478bd9Sstevel@tonic-gate static int
1051*7c478bd9Sstevel@tonic-gate st_abortfct(g)
1052*7c478bd9Sstevel@tonic-gate 	genarg *g;
1053*7c478bd9Sstevel@tonic-gate {
1054*7c478bd9Sstevel@tonic-gate 	sfsistat (*fi_abort) __P((SMFICTX *));
1055*7c478bd9Sstevel@tonic-gate 
1056*7c478bd9Sstevel@tonic-gate 	if (g == NULL)
1057*7c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
1058*7c478bd9Sstevel@tonic-gate 	if (g != NULL && g->a_ctx->ctx_smfi != NULL &&
1059*7c478bd9Sstevel@tonic-gate 	    (fi_abort = g->a_ctx->ctx_smfi->xxfi_abort) != NULL)
1060*7c478bd9Sstevel@tonic-gate 		(void) (*fi_abort)(g->a_ctx);
1061*7c478bd9Sstevel@tonic-gate 	return _SMFIS_NOREPLY;
1062*7c478bd9Sstevel@tonic-gate }
1063*7c478bd9Sstevel@tonic-gate /*
1064*7c478bd9Sstevel@tonic-gate **  TRANS_OK -- is the state transition ok?
1065*7c478bd9Sstevel@tonic-gate **
1066*7c478bd9Sstevel@tonic-gate **	Parameters:
1067*7c478bd9Sstevel@tonic-gate **		old -- old state
1068*7c478bd9Sstevel@tonic-gate **		new -- new state
1069*7c478bd9Sstevel@tonic-gate **
1070*7c478bd9Sstevel@tonic-gate **	Returns:
1071*7c478bd9Sstevel@tonic-gate **		state transition ok
1072*7c478bd9Sstevel@tonic-gate */
1073*7c478bd9Sstevel@tonic-gate 
1074*7c478bd9Sstevel@tonic-gate static bool
1075*7c478bd9Sstevel@tonic-gate trans_ok(old, new)
1076*7c478bd9Sstevel@tonic-gate 	int old, new;
1077*7c478bd9Sstevel@tonic-gate {
1078*7c478bd9Sstevel@tonic-gate 	int s, n;
1079*7c478bd9Sstevel@tonic-gate 
1080*7c478bd9Sstevel@tonic-gate 	s = old;
1081*7c478bd9Sstevel@tonic-gate 	do
1082*7c478bd9Sstevel@tonic-gate 	{
1083*7c478bd9Sstevel@tonic-gate 		/* is this state transition allowed? */
1084*7c478bd9Sstevel@tonic-gate 		if ((MI_MASK(new) & next_states[s]) != 0)
1085*7c478bd9Sstevel@tonic-gate 			return true;
1086*7c478bd9Sstevel@tonic-gate 
1087*7c478bd9Sstevel@tonic-gate 		/*
1088*7c478bd9Sstevel@tonic-gate 		**  no: try next state;
1089*7c478bd9Sstevel@tonic-gate 		**  this works since the relevant states are ordered
1090*7c478bd9Sstevel@tonic-gate 		**  strict sequentially
1091*7c478bd9Sstevel@tonic-gate 		*/
1092*7c478bd9Sstevel@tonic-gate 
1093*7c478bd9Sstevel@tonic-gate 		n = s + 1;
1094*7c478bd9Sstevel@tonic-gate 
1095*7c478bd9Sstevel@tonic-gate 		/*
1096*7c478bd9Sstevel@tonic-gate 		**  can we actually "skip" this state?
1097*7c478bd9Sstevel@tonic-gate 		**  see fix_stm() which sets this bit for those
1098*7c478bd9Sstevel@tonic-gate 		**  states which the filter program is not interested in
1099*7c478bd9Sstevel@tonic-gate 		*/
1100*7c478bd9Sstevel@tonic-gate 
1101*7c478bd9Sstevel@tonic-gate 		if (bitset(NX_SKIP, next_states[n]))
1102*7c478bd9Sstevel@tonic-gate 			s = n;
1103*7c478bd9Sstevel@tonic-gate 		else
1104*7c478bd9Sstevel@tonic-gate 			return false;
1105*7c478bd9Sstevel@tonic-gate 	} while (s <= ST_LAST);
1106*7c478bd9Sstevel@tonic-gate 	return false;
1107*7c478bd9Sstevel@tonic-gate }
1108*7c478bd9Sstevel@tonic-gate /*
1109*7c478bd9Sstevel@tonic-gate **  FIX_STM -- add "skip" bits to the state transition table
1110*7c478bd9Sstevel@tonic-gate **
1111*7c478bd9Sstevel@tonic-gate **	Parameters:
1112*7c478bd9Sstevel@tonic-gate **		ctx -- context structure
1113*7c478bd9Sstevel@tonic-gate **
1114*7c478bd9Sstevel@tonic-gate **	Returns:
1115*7c478bd9Sstevel@tonic-gate **		None.
1116*7c478bd9Sstevel@tonic-gate **
1117*7c478bd9Sstevel@tonic-gate **	Side effects:
1118*7c478bd9Sstevel@tonic-gate **		may change state transition table.
1119*7c478bd9Sstevel@tonic-gate */
1120*7c478bd9Sstevel@tonic-gate 
1121*7c478bd9Sstevel@tonic-gate static void
1122*7c478bd9Sstevel@tonic-gate fix_stm(ctx)
1123*7c478bd9Sstevel@tonic-gate 	SMFICTX_PTR ctx;
1124*7c478bd9Sstevel@tonic-gate {
1125*7c478bd9Sstevel@tonic-gate 	unsigned long fl;
1126*7c478bd9Sstevel@tonic-gate 
1127*7c478bd9Sstevel@tonic-gate 	if (ctx == NULL || ctx->ctx_smfi == NULL)
1128*7c478bd9Sstevel@tonic-gate 		return;
1129*7c478bd9Sstevel@tonic-gate 	fl = ctx->ctx_pflags;
1130*7c478bd9Sstevel@tonic-gate 	if (bitset(SMFIP_NOCONNECT, fl))
1131*7c478bd9Sstevel@tonic-gate 		next_states[ST_CONN] |= NX_SKIP;
1132*7c478bd9Sstevel@tonic-gate 	if (bitset(SMFIP_NOHELO, fl))
1133*7c478bd9Sstevel@tonic-gate 		next_states[ST_HELO] |= NX_SKIP;
1134*7c478bd9Sstevel@tonic-gate 	if (bitset(SMFIP_NOMAIL, fl))
1135*7c478bd9Sstevel@tonic-gate 		next_states[ST_MAIL] |= NX_SKIP;
1136*7c478bd9Sstevel@tonic-gate 	if (bitset(SMFIP_NORCPT, fl))
1137*7c478bd9Sstevel@tonic-gate 		next_states[ST_RCPT] |= NX_SKIP;
1138*7c478bd9Sstevel@tonic-gate 	if (bitset(SMFIP_NOHDRS, fl))
1139*7c478bd9Sstevel@tonic-gate 		next_states[ST_HDRS] |= NX_SKIP;
1140*7c478bd9Sstevel@tonic-gate 	if (bitset(SMFIP_NOEOH, fl))
1141*7c478bd9Sstevel@tonic-gate 		next_states[ST_EOHS] |= NX_SKIP;
1142*7c478bd9Sstevel@tonic-gate 	if (bitset(SMFIP_NOBODY, fl))
1143*7c478bd9Sstevel@tonic-gate 		next_states[ST_BODY] |= NX_SKIP;
1144*7c478bd9Sstevel@tonic-gate }
1145*7c478bd9Sstevel@tonic-gate /*
1146*7c478bd9Sstevel@tonic-gate **  DEC_ARGV -- split a buffer into a list of strings, NULL terminated
1147*7c478bd9Sstevel@tonic-gate **
1148*7c478bd9Sstevel@tonic-gate **	Parameters:
1149*7c478bd9Sstevel@tonic-gate **		buf -- buffer with several strings
1150*7c478bd9Sstevel@tonic-gate **		len -- length of buffer
1151*7c478bd9Sstevel@tonic-gate **
1152*7c478bd9Sstevel@tonic-gate **	Returns:
1153*7c478bd9Sstevel@tonic-gate **		array of pointers to the individual strings
1154*7c478bd9Sstevel@tonic-gate */
1155*7c478bd9Sstevel@tonic-gate 
1156*7c478bd9Sstevel@tonic-gate static char **
1157*7c478bd9Sstevel@tonic-gate dec_argv(buf, len)
1158*7c478bd9Sstevel@tonic-gate 	char *buf;
1159*7c478bd9Sstevel@tonic-gate 	size_t len;
1160*7c478bd9Sstevel@tonic-gate {
1161*7c478bd9Sstevel@tonic-gate 	char **s;
1162*7c478bd9Sstevel@tonic-gate 	size_t i;
1163*7c478bd9Sstevel@tonic-gate 	int elem, nelem;
1164*7c478bd9Sstevel@tonic-gate 
1165*7c478bd9Sstevel@tonic-gate 	nelem = 0;
1166*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++)
1167*7c478bd9Sstevel@tonic-gate 	{
1168*7c478bd9Sstevel@tonic-gate 		if (buf[i] == '\0')
1169*7c478bd9Sstevel@tonic-gate 			++nelem;
1170*7c478bd9Sstevel@tonic-gate 	}
1171*7c478bd9Sstevel@tonic-gate 	if (nelem == 0)
1172*7c478bd9Sstevel@tonic-gate 		return NULL;
1173*7c478bd9Sstevel@tonic-gate 
1174*7c478bd9Sstevel@tonic-gate 	/* last entry is only for the name */
1175*7c478bd9Sstevel@tonic-gate 	s = (char **)malloc((nelem + 1) * (sizeof *s));
1176*7c478bd9Sstevel@tonic-gate 	if (s == NULL)
1177*7c478bd9Sstevel@tonic-gate 		return NULL;
1178*7c478bd9Sstevel@tonic-gate 	s[0] = buf;
1179*7c478bd9Sstevel@tonic-gate 	for (i = 0, elem = 0; i < len && elem < nelem; i++)
1180*7c478bd9Sstevel@tonic-gate 	{
1181*7c478bd9Sstevel@tonic-gate 		if (buf[i] == '\0')
1182*7c478bd9Sstevel@tonic-gate 		{
1183*7c478bd9Sstevel@tonic-gate 			++elem;
1184*7c478bd9Sstevel@tonic-gate 			if (i + 1 >= len)
1185*7c478bd9Sstevel@tonic-gate 				s[elem] = NULL;
1186*7c478bd9Sstevel@tonic-gate 			else
1187*7c478bd9Sstevel@tonic-gate 				s[elem] = &(buf[i + 1]);
1188*7c478bd9Sstevel@tonic-gate 		}
1189*7c478bd9Sstevel@tonic-gate 	}
1190*7c478bd9Sstevel@tonic-gate 
1191*7c478bd9Sstevel@tonic-gate 	/* overwrite last entry (already done above, just paranoia) */
1192*7c478bd9Sstevel@tonic-gate 	s[elem] = NULL;
1193*7c478bd9Sstevel@tonic-gate 	return s;
1194*7c478bd9Sstevel@tonic-gate }
1195*7c478bd9Sstevel@tonic-gate /*
1196*7c478bd9Sstevel@tonic-gate **  DEC_ARG2 -- split a buffer into two strings
1197*7c478bd9Sstevel@tonic-gate **
1198*7c478bd9Sstevel@tonic-gate **	Parameters:
1199*7c478bd9Sstevel@tonic-gate **		buf -- buffer with two strings
1200*7c478bd9Sstevel@tonic-gate **		len -- length of buffer
1201*7c478bd9Sstevel@tonic-gate **		s1,s2 -- pointer to result strings
1202*7c478bd9Sstevel@tonic-gate **
1203*7c478bd9Sstevel@tonic-gate **	Returns:
1204*7c478bd9Sstevel@tonic-gate **		MI_FAILURE/MI_SUCCESS
1205*7c478bd9Sstevel@tonic-gate */
1206*7c478bd9Sstevel@tonic-gate 
1207*7c478bd9Sstevel@tonic-gate static int
1208*7c478bd9Sstevel@tonic-gate dec_arg2(buf, len, s1, s2)
1209*7c478bd9Sstevel@tonic-gate 	char *buf;
1210*7c478bd9Sstevel@tonic-gate 	size_t len;
1211*7c478bd9Sstevel@tonic-gate 	char **s1;
1212*7c478bd9Sstevel@tonic-gate 	char **s2;
1213*7c478bd9Sstevel@tonic-gate {
1214*7c478bd9Sstevel@tonic-gate 	size_t i;
1215*7c478bd9Sstevel@tonic-gate 
1216*7c478bd9Sstevel@tonic-gate 	/* paranoia: check for terminating '\0' */
1217*7c478bd9Sstevel@tonic-gate 	if (len == 0 || buf[len - 1] != '\0')
1218*7c478bd9Sstevel@tonic-gate 		return MI_FAILURE;
1219*7c478bd9Sstevel@tonic-gate 	*s1 = buf;
1220*7c478bd9Sstevel@tonic-gate 	for (i = 1; i < len && buf[i] != '\0'; i++)
1221*7c478bd9Sstevel@tonic-gate 		continue;
1222*7c478bd9Sstevel@tonic-gate 	if (i >= len - 1)
1223*7c478bd9Sstevel@tonic-gate 		return MI_FAILURE;
1224*7c478bd9Sstevel@tonic-gate 	*s2 = buf + i + 1;
1225*7c478bd9Sstevel@tonic-gate 	return MI_SUCCESS;
1226*7c478bd9Sstevel@tonic-gate }
1227*7c478bd9Sstevel@tonic-gate /*
1228*7c478bd9Sstevel@tonic-gate **  SENDOK -- is it ok for the filter to send stuff to the MTA?
1229*7c478bd9Sstevel@tonic-gate **
1230*7c478bd9Sstevel@tonic-gate **	Parameters:
1231*7c478bd9Sstevel@tonic-gate **		ctx -- context structure
1232*7c478bd9Sstevel@tonic-gate **		flag -- flag to check
1233*7c478bd9Sstevel@tonic-gate **
1234*7c478bd9Sstevel@tonic-gate **	Returns:
1235*7c478bd9Sstevel@tonic-gate **		sending allowed (in current state)
1236*7c478bd9Sstevel@tonic-gate */
1237*7c478bd9Sstevel@tonic-gate 
1238*7c478bd9Sstevel@tonic-gate bool
1239*7c478bd9Sstevel@tonic-gate mi_sendok(ctx, flag)
1240*7c478bd9Sstevel@tonic-gate 	SMFICTX_PTR ctx;
1241*7c478bd9Sstevel@tonic-gate 	int flag;
1242*7c478bd9Sstevel@tonic-gate {
1243*7c478bd9Sstevel@tonic-gate 	if (ctx == NULL || ctx->ctx_smfi == NULL)
1244*7c478bd9Sstevel@tonic-gate 		return false;
1245*7c478bd9Sstevel@tonic-gate 
1246*7c478bd9Sstevel@tonic-gate 	/* did the milter request this operation? */
1247*7c478bd9Sstevel@tonic-gate 	if (flag != 0 && !bitset(flag, ctx->ctx_smfi->xxfi_flags))
1248*7c478bd9Sstevel@tonic-gate 		return false;
1249*7c478bd9Sstevel@tonic-gate 
1250*7c478bd9Sstevel@tonic-gate 	/* are we in the correct state? It must be "End of Message". */
1251*7c478bd9Sstevel@tonic-gate 	return ctx->ctx_state == ST_ENDM;
1252*7c478bd9Sstevel@tonic-gate }
1253