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