xref: /titanic_53/usr/src/cmd/sendmail/libmilter/engine.c (revision 3ee0e49223f178da635734759b9167f924321ff0)
17c478bd9Sstevel@tonic-gate /*
2*3ee0e492Sjbeck  *  Copyright (c) 1999-2004, 2006 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate  *	All rights reserved.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
67c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
77c478bd9Sstevel@tonic-gate  * the sendmail distribution.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  */
107c478bd9Sstevel@tonic-gate 
117c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
127c478bd9Sstevel@tonic-gate 
137c478bd9Sstevel@tonic-gate #include <sm/gen.h>
14*3ee0e492Sjbeck SM_RCSID("@(#)$Id: engine.c,v 8.121 2006/04/18 21:01:46 ca Exp $")
157c478bd9Sstevel@tonic-gate 
167c478bd9Sstevel@tonic-gate #include "libmilter.h"
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #if NETINET || NETINET6
197c478bd9Sstevel@tonic-gate # include <arpa/inet.h>
207c478bd9Sstevel@tonic-gate #endif /* NETINET || NETINET6 */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /* generic argument for functions in the command table */
237c478bd9Sstevel@tonic-gate struct arg_struct
247c478bd9Sstevel@tonic-gate {
257c478bd9Sstevel@tonic-gate 	size_t		a_len;		/* length of buffer */
267c478bd9Sstevel@tonic-gate 	char		*a_buf;		/* argument string */
277c478bd9Sstevel@tonic-gate 	int		a_idx;		/* index for macro array */
287c478bd9Sstevel@tonic-gate 	SMFICTX_PTR	a_ctx;		/* context */
297c478bd9Sstevel@tonic-gate };
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate typedef struct arg_struct genarg;
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /* structure for commands received from MTA */
347c478bd9Sstevel@tonic-gate struct cmdfct_t
357c478bd9Sstevel@tonic-gate {
367c478bd9Sstevel@tonic-gate 	char	cm_cmd;				/* command */
377c478bd9Sstevel@tonic-gate 	int	cm_argt;			/* type of arguments expected */
387c478bd9Sstevel@tonic-gate 	int	cm_next;			/* next state */
397c478bd9Sstevel@tonic-gate 	int	cm_todo;			/* what to do next */
407c478bd9Sstevel@tonic-gate 	int	cm_macros;			/* index for macros */
417c478bd9Sstevel@tonic-gate 	int	(*cm_fct) __P((genarg *));	/* function to execute */
427c478bd9Sstevel@tonic-gate };
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate typedef struct cmdfct_t cmdfct;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /* possible values for cm_argt */
477c478bd9Sstevel@tonic-gate #define	CM_ARG0	0	/* no args */
487c478bd9Sstevel@tonic-gate #define	CM_ARG1	1	/* one arg (string) */
497c478bd9Sstevel@tonic-gate #define	CM_ARG2	2	/* two args (strings) */
507c478bd9Sstevel@tonic-gate #define	CM_ARGA	4	/* one string and _SOCK_ADDR */
517c478bd9Sstevel@tonic-gate #define	CM_ARGO	5	/* two integers */
527c478bd9Sstevel@tonic-gate #define	CM_ARGV	8	/* \0 separated list of args, NULL-terminated */
537c478bd9Sstevel@tonic-gate #define	CM_ARGN	9	/* \0 separated list of args (strings) */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /* possible values for cm_todo */
567c478bd9Sstevel@tonic-gate #define	CT_CONT		0x0000	/* continue reading commands */
577c478bd9Sstevel@tonic-gate #define	CT_IGNO		0x0001	/* continue even when error  */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /* not needed right now, done via return code instead */
607c478bd9Sstevel@tonic-gate #define	CT_KEEP		0x0004	/* keep buffer (contains symbols) */
617c478bd9Sstevel@tonic-gate #define	CT_END		0x0008	/* start replying */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /* index in macro array: macros only for these commands */
647c478bd9Sstevel@tonic-gate #define	CI_NONE		(-1)
657c478bd9Sstevel@tonic-gate #define	CI_CONN		0
667c478bd9Sstevel@tonic-gate #define	CI_HELO		1
677c478bd9Sstevel@tonic-gate #define	CI_MAIL		2
687c478bd9Sstevel@tonic-gate #define CI_RCPT		3
697c478bd9Sstevel@tonic-gate #define CI_EOM		4
707c478bd9Sstevel@tonic-gate #if CI_EOM >= MAX_MACROS_ENTRIES
717c478bd9Sstevel@tonic-gate ERROR: do not compile with CI_EOM >= MAX_MACROS_ENTRIES
727c478bd9Sstevel@tonic-gate #endif
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /* function prototypes */
757c478bd9Sstevel@tonic-gate static int	st_abortfct __P((genarg *));
767c478bd9Sstevel@tonic-gate static int	st_macros __P((genarg *));
777c478bd9Sstevel@tonic-gate static int	st_optionneg __P((genarg *));
787c478bd9Sstevel@tonic-gate static int	st_bodychunk __P((genarg *));
797c478bd9Sstevel@tonic-gate static int	st_connectinfo __P((genarg *));
807c478bd9Sstevel@tonic-gate static int	st_bodyend __P((genarg *));
817c478bd9Sstevel@tonic-gate static int	st_helo __P((genarg *));
827c478bd9Sstevel@tonic-gate static int	st_header __P((genarg *));
837c478bd9Sstevel@tonic-gate static int	st_sender __P((genarg *));
847c478bd9Sstevel@tonic-gate static int	st_rcpt __P((genarg *));
857c478bd9Sstevel@tonic-gate #if SMFI_VERSION > 2
867c478bd9Sstevel@tonic-gate static int	st_unknown __P((genarg *));
877c478bd9Sstevel@tonic-gate #endif /* SMFI_VERSION > 2 */
887c478bd9Sstevel@tonic-gate #if SMFI_VERSION > 3
897c478bd9Sstevel@tonic-gate static int	st_data __P((genarg *));
907c478bd9Sstevel@tonic-gate #endif /* SMFI_VERSION > 3 */
917c478bd9Sstevel@tonic-gate static int	st_eoh __P((genarg *));
927c478bd9Sstevel@tonic-gate static int	st_quit __P((genarg *));
937c478bd9Sstevel@tonic-gate static int	sendreply __P((sfsistat, socket_t, struct timeval *, SMFICTX_PTR));
947c478bd9Sstevel@tonic-gate static void	fix_stm __P((SMFICTX_PTR));
957c478bd9Sstevel@tonic-gate static bool	trans_ok __P((int, int));
967c478bd9Sstevel@tonic-gate static char	**dec_argv __P((char *, size_t));
977c478bd9Sstevel@tonic-gate static int	dec_arg2 __P((char *, size_t, char **, char **));
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /* states */
1007c478bd9Sstevel@tonic-gate #define ST_NONE	(-1)
1017c478bd9Sstevel@tonic-gate #define ST_INIT	0	/* initial state */
1027c478bd9Sstevel@tonic-gate #define ST_OPTS	1	/* option negotiation */
1037c478bd9Sstevel@tonic-gate #define ST_CONN	2	/* connection info */
1047c478bd9Sstevel@tonic-gate #define ST_HELO	3	/* helo */
1057c478bd9Sstevel@tonic-gate #define ST_MAIL	4	/* mail from */
1067c478bd9Sstevel@tonic-gate #define ST_RCPT	5	/* rcpt to */
1077c478bd9Sstevel@tonic-gate #define ST_DATA	6	/* data */
1087c478bd9Sstevel@tonic-gate #define ST_HDRS	7	/* headers */
1097c478bd9Sstevel@tonic-gate #define ST_EOHS	8	/* end of headers */
1107c478bd9Sstevel@tonic-gate #define ST_BODY	9	/* body */
1117c478bd9Sstevel@tonic-gate #define ST_ENDM	10	/* end of message */
1127c478bd9Sstevel@tonic-gate #define ST_QUIT	11	/* quit */
1137c478bd9Sstevel@tonic-gate #define ST_ABRT	12	/* abort */
1147c478bd9Sstevel@tonic-gate #define ST_UNKN 13	/* unknown SMTP command */
1157c478bd9Sstevel@tonic-gate #define ST_LAST	ST_UNKN	/* last valid state */
1167c478bd9Sstevel@tonic-gate #define ST_SKIP	15	/* not a state but required for the state table */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /* in a mail transaction? must be before eom according to spec. */
1197c478bd9Sstevel@tonic-gate #define ST_IN_MAIL(st)	((st) >= ST_MAIL && (st) < ST_ENDM)
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate **  set of next states
1237c478bd9Sstevel@tonic-gate **  each state (ST_*) corresponds to bit in an int value (1 << state)
1247c478bd9Sstevel@tonic-gate **  each state has a set of allowed transitions ('or' of bits of states)
1257c478bd9Sstevel@tonic-gate **  so a state transition is valid if the mask of the next state
1267c478bd9Sstevel@tonic-gate **  is set in the NX_* value
1277c478bd9Sstevel@tonic-gate **  this function is coded in trans_ok(), see below.
1287c478bd9Sstevel@tonic-gate */
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate #define MI_MASK(x)	(0x0001 << (x))	/* generate a bit "mask" for a state */
1317c478bd9Sstevel@tonic-gate #define NX_INIT	(MI_MASK(ST_OPTS))
1327c478bd9Sstevel@tonic-gate #define NX_OPTS	(MI_MASK(ST_CONN) | MI_MASK(ST_UNKN))
1337c478bd9Sstevel@tonic-gate #define NX_CONN	(MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN))
1347c478bd9Sstevel@tonic-gate #define NX_HELO	(MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN))
1357c478bd9Sstevel@tonic-gate #define NX_MAIL	(MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | MI_MASK(ST_UNKN))
1367c478bd9Sstevel@tonic-gate #define NX_RCPT	(MI_MASK(ST_HDRS) | MI_MASK(ST_EOHS) | MI_MASK(ST_DATA) | \
1377c478bd9Sstevel@tonic-gate 		 MI_MASK(ST_BODY) | MI_MASK(ST_ENDM) | \
1387c478bd9Sstevel@tonic-gate 		 MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | MI_MASK(ST_UNKN))
1397c478bd9Sstevel@tonic-gate #define NX_DATA	(MI_MASK(ST_EOHS) | MI_MASK(ST_HDRS) | MI_MASK(ST_ABRT))
1407c478bd9Sstevel@tonic-gate #define NX_HDRS	(MI_MASK(ST_EOHS) | MI_MASK(ST_HDRS) | MI_MASK(ST_ABRT))
1417c478bd9Sstevel@tonic-gate #define NX_EOHS	(MI_MASK(ST_BODY) | MI_MASK(ST_ENDM) | MI_MASK(ST_ABRT))
1427c478bd9Sstevel@tonic-gate #define NX_BODY	(MI_MASK(ST_ENDM) | MI_MASK(ST_BODY) | MI_MASK(ST_ABRT))
1437c478bd9Sstevel@tonic-gate #define NX_ENDM	(MI_MASK(ST_QUIT) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN))
1447c478bd9Sstevel@tonic-gate #define NX_QUIT	0
1457c478bd9Sstevel@tonic-gate #define NX_ABRT	0
1467c478bd9Sstevel@tonic-gate #define NX_UNKN (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | \
1477c478bd9Sstevel@tonic-gate 		 MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | \
1487c478bd9Sstevel@tonic-gate 		 MI_MASK(ST_DATA) | \
1497c478bd9Sstevel@tonic-gate 		 MI_MASK(ST_BODY) | MI_MASK(ST_UNKN) | \
1507c478bd9Sstevel@tonic-gate 		 MI_MASK(ST_ABRT) | MI_MASK(ST_QUIT))
1517c478bd9Sstevel@tonic-gate #define NX_SKIP MI_MASK(ST_SKIP)
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate static int next_states[] =
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	NX_INIT,
1567c478bd9Sstevel@tonic-gate 	NX_OPTS,
1577c478bd9Sstevel@tonic-gate 	NX_CONN,
1587c478bd9Sstevel@tonic-gate 	NX_HELO,
1597c478bd9Sstevel@tonic-gate 	NX_MAIL,
1607c478bd9Sstevel@tonic-gate 	NX_RCPT,
1617c478bd9Sstevel@tonic-gate 	NX_DATA,
1627c478bd9Sstevel@tonic-gate 	NX_HDRS,
1637c478bd9Sstevel@tonic-gate 	NX_EOHS,
1647c478bd9Sstevel@tonic-gate 	NX_BODY,
1657c478bd9Sstevel@tonic-gate 	NX_ENDM,
1667c478bd9Sstevel@tonic-gate 	NX_QUIT,
1677c478bd9Sstevel@tonic-gate 	NX_ABRT,
1687c478bd9Sstevel@tonic-gate 	NX_UNKN
1697c478bd9Sstevel@tonic-gate };
1707c478bd9Sstevel@tonic-gate 
171*3ee0e492Sjbeck #define SIZE_NEXT_STATES	(sizeof(next_states) / sizeof(next_states[0]))
172*3ee0e492Sjbeck 
1737c478bd9Sstevel@tonic-gate /* commands received by milter */
1747c478bd9Sstevel@tonic-gate static cmdfct cmds[] =
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate {SMFIC_ABORT,	CM_ARG0, ST_ABRT,  CT_CONT,	CI_NONE, st_abortfct	},
1777c478bd9Sstevel@tonic-gate {SMFIC_MACRO,	CM_ARGV, ST_NONE,  CT_KEEP,	CI_NONE, st_macros	},
1787c478bd9Sstevel@tonic-gate {SMFIC_BODY,	CM_ARG1, ST_BODY,  CT_CONT,	CI_NONE, st_bodychunk	},
1797c478bd9Sstevel@tonic-gate {SMFIC_CONNECT,	CM_ARG2, ST_CONN,  CT_CONT,	CI_CONN, st_connectinfo	},
1807c478bd9Sstevel@tonic-gate {SMFIC_BODYEOB,	CM_ARG1, ST_ENDM,  CT_CONT,	CI_EOM,  st_bodyend	},
1817c478bd9Sstevel@tonic-gate {SMFIC_HELO,	CM_ARG1, ST_HELO,  CT_CONT,	CI_HELO, st_helo	},
1827c478bd9Sstevel@tonic-gate {SMFIC_HEADER,	CM_ARG2, ST_HDRS,  CT_CONT,	CI_NONE, st_header	},
1837c478bd9Sstevel@tonic-gate {SMFIC_MAIL,	CM_ARGV, ST_MAIL,  CT_CONT,	CI_MAIL, st_sender	},
1847c478bd9Sstevel@tonic-gate {SMFIC_OPTNEG,	CM_ARGO, ST_OPTS,  CT_CONT,	CI_NONE, st_optionneg	},
1857c478bd9Sstevel@tonic-gate {SMFIC_EOH,	CM_ARG0, ST_EOHS,  CT_CONT,	CI_NONE, st_eoh		},
1867c478bd9Sstevel@tonic-gate {SMFIC_QUIT,	CM_ARG0, ST_QUIT,  CT_END,	CI_NONE, st_quit	},
1877c478bd9Sstevel@tonic-gate #if SMFI_VERSION > 3
1887c478bd9Sstevel@tonic-gate {SMFIC_DATA,	CM_ARG0, ST_DATA,  CT_CONT,	CI_NONE, st_data	},
1897c478bd9Sstevel@tonic-gate #endif /* SMFI_VERSION > 3 */
1907c478bd9Sstevel@tonic-gate {SMFIC_RCPT,	CM_ARGV, ST_RCPT,  CT_IGNO,	CI_RCPT, st_rcpt	}
1917c478bd9Sstevel@tonic-gate #if SMFI_VERSION > 2
1927c478bd9Sstevel@tonic-gate ,{SMFIC_UNKNOWN,CM_ARG1, ST_UNKN,  CT_IGNO,	CI_NONE, st_unknown	}
1937c478bd9Sstevel@tonic-gate #endif /* SMFI_VERSION > 2 */
1947c478bd9Sstevel@tonic-gate };
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /* additional (internal) reply codes */
1977c478bd9Sstevel@tonic-gate #define _SMFIS_KEEP	20
1987c478bd9Sstevel@tonic-gate #define _SMFIS_ABORT	21
1997c478bd9Sstevel@tonic-gate #define _SMFIS_OPTIONS	22
2007c478bd9Sstevel@tonic-gate #define _SMFIS_NOREPLY	23
2017c478bd9Sstevel@tonic-gate #define _SMFIS_FAIL	(-1)
2027c478bd9Sstevel@tonic-gate #define _SMFIS_NONE	(-2)
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate /*
2057c478bd9Sstevel@tonic-gate **  MI_ENGINE -- receive commands and process them
2067c478bd9Sstevel@tonic-gate **
2077c478bd9Sstevel@tonic-gate **	Parameters:
2087c478bd9Sstevel@tonic-gate **		ctx -- context structure
2097c478bd9Sstevel@tonic-gate **
2107c478bd9Sstevel@tonic-gate **	Returns:
2117c478bd9Sstevel@tonic-gate **		MI_FAILURE/MI_SUCCESS
2127c478bd9Sstevel@tonic-gate */
2137c478bd9Sstevel@tonic-gate int
2147c478bd9Sstevel@tonic-gate mi_engine(ctx)
2157c478bd9Sstevel@tonic-gate 	SMFICTX_PTR ctx;
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	size_t len;
2187c478bd9Sstevel@tonic-gate 	int i;
2197c478bd9Sstevel@tonic-gate 	socket_t sd;
2207c478bd9Sstevel@tonic-gate 	int ret = MI_SUCCESS;
2217c478bd9Sstevel@tonic-gate 	int ncmds = sizeof(cmds) / sizeof(cmdfct);
2227c478bd9Sstevel@tonic-gate 	int curstate = ST_INIT;
2237c478bd9Sstevel@tonic-gate 	int newstate;
2247c478bd9Sstevel@tonic-gate 	bool call_abort;
2257c478bd9Sstevel@tonic-gate 	sfsistat r;
2267c478bd9Sstevel@tonic-gate 	char cmd;
2277c478bd9Sstevel@tonic-gate 	char *buf = NULL;
2287c478bd9Sstevel@tonic-gate 	genarg arg;
2297c478bd9Sstevel@tonic-gate 	struct timeval timeout;
2307c478bd9Sstevel@tonic-gate 	int (*f) __P((genarg *));
2317c478bd9Sstevel@tonic-gate 	sfsistat (*fi_abort) __P((SMFICTX *));
2327c478bd9Sstevel@tonic-gate 	sfsistat (*fi_close) __P((SMFICTX *));
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	arg.a_ctx = ctx;
2357c478bd9Sstevel@tonic-gate 	sd = ctx->ctx_sd;
2367c478bd9Sstevel@tonic-gate 	fi_abort = ctx->ctx_smfi->xxfi_abort;
2377c478bd9Sstevel@tonic-gate 	mi_clr_macros(ctx, 0);
2387c478bd9Sstevel@tonic-gate 	fix_stm(ctx);
2397c478bd9Sstevel@tonic-gate 	r = _SMFIS_NONE;
2407c478bd9Sstevel@tonic-gate 	do
2417c478bd9Sstevel@tonic-gate 	{
2427c478bd9Sstevel@tonic-gate 		/* call abort only if in a mail transaction */
2437c478bd9Sstevel@tonic-gate 		call_abort = ST_IN_MAIL(curstate);
2447c478bd9Sstevel@tonic-gate 		timeout.tv_sec = ctx->ctx_timeout;
2457c478bd9Sstevel@tonic-gate 		timeout.tv_usec = 0;
2467c478bd9Sstevel@tonic-gate 		if (mi_stop() == MILTER_ABRT)
2477c478bd9Sstevel@tonic-gate 		{
2487c478bd9Sstevel@tonic-gate 			if (ctx->ctx_dbg > 3)
2497c478bd9Sstevel@tonic-gate 				sm_dprintf("[%d] milter_abort\n",
2507c478bd9Sstevel@tonic-gate 					(int) ctx->ctx_id);
2517c478bd9Sstevel@tonic-gate 			ret = MI_FAILURE;
2527c478bd9Sstevel@tonic-gate 			break;
2537c478bd9Sstevel@tonic-gate 		}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 		/*
2567c478bd9Sstevel@tonic-gate 		**  Notice: buf is allocated by mi_rd_cmd() and it will
2577c478bd9Sstevel@tonic-gate 		**  usually be free()d after it has been used in f().
2587c478bd9Sstevel@tonic-gate 		**  However, if the function returns _SMFIS_KEEP then buf
2597c478bd9Sstevel@tonic-gate 		**  contains macros and will not be free()d.
2607c478bd9Sstevel@tonic-gate 		**  Hence r must be set to _SMFIS_NONE if a new buf is
2617c478bd9Sstevel@tonic-gate 		**  allocated to avoid problem with housekeeping, esp.
2627c478bd9Sstevel@tonic-gate 		**  if the code "break"s out of the loop.
2637c478bd9Sstevel@tonic-gate 		*/
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 		r = _SMFIS_NONE;
2667c478bd9Sstevel@tonic-gate 		if ((buf = mi_rd_cmd(sd, &timeout, &cmd, &len,
2677c478bd9Sstevel@tonic-gate 				     ctx->ctx_smfi->xxfi_name)) == NULL &&
2687c478bd9Sstevel@tonic-gate 		    cmd < SMFIC_VALIDCMD)
2697c478bd9Sstevel@tonic-gate 		{
2707c478bd9Sstevel@tonic-gate 			if (ctx->ctx_dbg > 5)
2717c478bd9Sstevel@tonic-gate 				sm_dprintf("[%d] mi_engine: mi_rd_cmd error (%x)\n",
2727c478bd9Sstevel@tonic-gate 					(int) ctx->ctx_id, (int) cmd);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 			/*
2757c478bd9Sstevel@tonic-gate 			**  eof is currently treated as failure ->
2767c478bd9Sstevel@tonic-gate 			**  abort() instead of close(), otherwise use:
2777c478bd9Sstevel@tonic-gate 			**  if (cmd != SMFIC_EOF)
2787c478bd9Sstevel@tonic-gate 			*/
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 			ret = MI_FAILURE;
2817c478bd9Sstevel@tonic-gate 			break;
2827c478bd9Sstevel@tonic-gate 		}
2837c478bd9Sstevel@tonic-gate 		if (ctx->ctx_dbg > 4)
2847c478bd9Sstevel@tonic-gate 			sm_dprintf("[%d] got cmd '%c' len %d\n",
2857c478bd9Sstevel@tonic-gate 				(int) ctx->ctx_id, cmd, (int) len);
2867c478bd9Sstevel@tonic-gate 		for (i = 0; i < ncmds; i++)
2877c478bd9Sstevel@tonic-gate 		{
2887c478bd9Sstevel@tonic-gate 			if (cmd == cmds[i].cm_cmd)
2897c478bd9Sstevel@tonic-gate 				break;
2907c478bd9Sstevel@tonic-gate 		}
2917c478bd9Sstevel@tonic-gate 		if (i >= ncmds)
2927c478bd9Sstevel@tonic-gate 		{
2937c478bd9Sstevel@tonic-gate 			/* unknown command */
2947c478bd9Sstevel@tonic-gate 			if (ctx->ctx_dbg > 1)
2957c478bd9Sstevel@tonic-gate 				sm_dprintf("[%d] cmd '%c' unknown\n",
2967c478bd9Sstevel@tonic-gate 					(int) ctx->ctx_id, cmd);
2977c478bd9Sstevel@tonic-gate 			ret = MI_FAILURE;
2987c478bd9Sstevel@tonic-gate 			break;
2997c478bd9Sstevel@tonic-gate 		}
3007c478bd9Sstevel@tonic-gate 		if ((f = cmds[i].cm_fct) == NULL)
3017c478bd9Sstevel@tonic-gate 		{
3027c478bd9Sstevel@tonic-gate 			/* stop for now */
3037c478bd9Sstevel@tonic-gate 			if (ctx->ctx_dbg > 1)
3047c478bd9Sstevel@tonic-gate 				sm_dprintf("[%d] cmd '%c' not impl\n",
3057c478bd9Sstevel@tonic-gate 					(int) ctx->ctx_id, cmd);
3067c478bd9Sstevel@tonic-gate 			ret = MI_FAILURE;
3077c478bd9Sstevel@tonic-gate 			break;
3087c478bd9Sstevel@tonic-gate 		}
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 		/* is new state ok? */
3117c478bd9Sstevel@tonic-gate 		newstate = cmds[i].cm_next;
3127c478bd9Sstevel@tonic-gate 		if (ctx->ctx_dbg > 5)
3137c478bd9Sstevel@tonic-gate 			sm_dprintf("[%d] cur %x new %x nextmask %x\n",
3147c478bd9Sstevel@tonic-gate 				(int) ctx->ctx_id,
3157c478bd9Sstevel@tonic-gate 				curstate, newstate, next_states[curstate]);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 		if (newstate != ST_NONE && !trans_ok(curstate, newstate))
3187c478bd9Sstevel@tonic-gate 		{
3197c478bd9Sstevel@tonic-gate 			if (ctx->ctx_dbg > 1)
3207c478bd9Sstevel@tonic-gate 				sm_dprintf("[%d] abort: cur %d (%x) new %d (%x) next %x\n",
3217c478bd9Sstevel@tonic-gate 					(int) ctx->ctx_id,
3227c478bd9Sstevel@tonic-gate 					curstate, MI_MASK(curstate),
3237c478bd9Sstevel@tonic-gate 					newstate, MI_MASK(newstate),
3247c478bd9Sstevel@tonic-gate 					next_states[curstate]);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 			/* call abort only if in a mail transaction */
3277c478bd9Sstevel@tonic-gate 			if (fi_abort != NULL && call_abort)
3287c478bd9Sstevel@tonic-gate 				(void) (*fi_abort)(ctx);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 			/*
3317c478bd9Sstevel@tonic-gate 			**  try to reach the new state from HELO
3327c478bd9Sstevel@tonic-gate 			**  if it can't be reached, ignore the command.
3337c478bd9Sstevel@tonic-gate 			*/
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 			curstate = ST_HELO;
3367c478bd9Sstevel@tonic-gate 			if (!trans_ok(curstate, newstate))
3377c478bd9Sstevel@tonic-gate 			{
3387c478bd9Sstevel@tonic-gate 				if (buf != NULL)
3397c478bd9Sstevel@tonic-gate 				{
3407c478bd9Sstevel@tonic-gate 					free(buf);
3417c478bd9Sstevel@tonic-gate 					buf = NULL;
3427c478bd9Sstevel@tonic-gate 				}
3437c478bd9Sstevel@tonic-gate 				continue;
3447c478bd9Sstevel@tonic-gate 			}
3457c478bd9Sstevel@tonic-gate 		}
3467c478bd9Sstevel@tonic-gate 		arg.a_len = len;
3477c478bd9Sstevel@tonic-gate 		arg.a_buf = buf;
3487c478bd9Sstevel@tonic-gate 		if (newstate != ST_NONE)
3497c478bd9Sstevel@tonic-gate 		{
3507c478bd9Sstevel@tonic-gate 			curstate = newstate;
3517c478bd9Sstevel@tonic-gate 			ctx->ctx_state = curstate;
3527c478bd9Sstevel@tonic-gate 		}
3537c478bd9Sstevel@tonic-gate 		arg.a_idx = cmds[i].cm_macros;
3547c478bd9Sstevel@tonic-gate 		call_abort = ST_IN_MAIL(curstate);
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 		/* call function to deal with command */
3577c478bd9Sstevel@tonic-gate 		r = (*f)(&arg);
3587c478bd9Sstevel@tonic-gate 		if (r != _SMFIS_KEEP && buf != NULL)
3597c478bd9Sstevel@tonic-gate 		{
3607c478bd9Sstevel@tonic-gate 			free(buf);
3617c478bd9Sstevel@tonic-gate 			buf = NULL;
3627c478bd9Sstevel@tonic-gate 		}
3637c478bd9Sstevel@tonic-gate 		if (sendreply(r, sd, &timeout, ctx) != MI_SUCCESS)
3647c478bd9Sstevel@tonic-gate 		{
3657c478bd9Sstevel@tonic-gate 			ret = MI_FAILURE;
3667c478bd9Sstevel@tonic-gate 			break;
3677c478bd9Sstevel@tonic-gate 		}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 		if (r == SMFIS_ACCEPT)
3707c478bd9Sstevel@tonic-gate 		{
3717c478bd9Sstevel@tonic-gate 			/* accept mail, no further actions taken */
3727c478bd9Sstevel@tonic-gate 			curstate = ST_HELO;
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 		else if (r == SMFIS_REJECT || r == SMFIS_DISCARD ||
3757c478bd9Sstevel@tonic-gate 			 r ==  SMFIS_TEMPFAIL)
3767c478bd9Sstevel@tonic-gate 		{
3777c478bd9Sstevel@tonic-gate 			/*
3787c478bd9Sstevel@tonic-gate 			**  further actions depend on current state
3797c478bd9Sstevel@tonic-gate 			**  if the IGNO bit is set: "ignore" the error,
3807c478bd9Sstevel@tonic-gate 			**  i.e., stay in the current state
3817c478bd9Sstevel@tonic-gate 			*/
3827c478bd9Sstevel@tonic-gate 			if (!bitset(CT_IGNO, cmds[i].cm_todo))
3837c478bd9Sstevel@tonic-gate 				curstate = ST_HELO;
3847c478bd9Sstevel@tonic-gate 		}
3857c478bd9Sstevel@tonic-gate 		else if (r == _SMFIS_ABORT)
3867c478bd9Sstevel@tonic-gate 		{
3877c478bd9Sstevel@tonic-gate 			if (ctx->ctx_dbg > 5)
3887c478bd9Sstevel@tonic-gate 				sm_dprintf("[%d] function returned abort\n",
3897c478bd9Sstevel@tonic-gate 					(int) ctx->ctx_id);
3907c478bd9Sstevel@tonic-gate 			ret = MI_FAILURE;
3917c478bd9Sstevel@tonic-gate 			break;
3927c478bd9Sstevel@tonic-gate 		}
3937c478bd9Sstevel@tonic-gate 	} while (!bitset(CT_END, cmds[i].cm_todo));
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	if (ret != MI_SUCCESS)
3967c478bd9Sstevel@tonic-gate 	{
3977c478bd9Sstevel@tonic-gate 		/* call abort only if in a mail transaction */
3987c478bd9Sstevel@tonic-gate 		if (fi_abort != NULL && call_abort)
3997c478bd9Sstevel@tonic-gate 			(void) (*fi_abort)(ctx);
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	/* close must always be called */
4037c478bd9Sstevel@tonic-gate 	if ((fi_close = ctx->ctx_smfi->xxfi_close) != NULL)
4047c478bd9Sstevel@tonic-gate 		(void) (*fi_close)(ctx);
4057c478bd9Sstevel@tonic-gate 	if (r != _SMFIS_KEEP && buf != NULL)
4067c478bd9Sstevel@tonic-gate 		free(buf);
4077c478bd9Sstevel@tonic-gate 	mi_clr_macros(ctx, 0);
4087c478bd9Sstevel@tonic-gate 	return ret;
4097c478bd9Sstevel@tonic-gate }
4107c478bd9Sstevel@tonic-gate /*
4117c478bd9Sstevel@tonic-gate **  SENDREPLY -- send a reply to the MTA
4127c478bd9Sstevel@tonic-gate **
4137c478bd9Sstevel@tonic-gate **	Parameters:
4147c478bd9Sstevel@tonic-gate **		r -- reply code
4157c478bd9Sstevel@tonic-gate **		sd -- socket descriptor
4167c478bd9Sstevel@tonic-gate **		timeout_ptr -- (ptr to) timeout to use for sending
4177c478bd9Sstevel@tonic-gate **		ctx -- context structure
4187c478bd9Sstevel@tonic-gate **
4197c478bd9Sstevel@tonic-gate **	Returns:
4207c478bd9Sstevel@tonic-gate **		MI_SUCCESS/MI_FAILURE
4217c478bd9Sstevel@tonic-gate */
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate static int
4247c478bd9Sstevel@tonic-gate sendreply(r, sd, timeout_ptr, ctx)
4257c478bd9Sstevel@tonic-gate 	sfsistat r;
4267c478bd9Sstevel@tonic-gate 	socket_t sd;
4277c478bd9Sstevel@tonic-gate 	struct timeval *timeout_ptr;
4287c478bd9Sstevel@tonic-gate 	SMFICTX_PTR ctx;
4297c478bd9Sstevel@tonic-gate {
4307c478bd9Sstevel@tonic-gate 	int ret = MI_SUCCESS;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	switch (r)
4337c478bd9Sstevel@tonic-gate 	{
4347c478bd9Sstevel@tonic-gate 	  case SMFIS_CONTINUE:
4357c478bd9Sstevel@tonic-gate 		ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_CONTINUE, NULL, 0);
4367c478bd9Sstevel@tonic-gate 		break;
4377c478bd9Sstevel@tonic-gate 	  case SMFIS_TEMPFAIL:
4387c478bd9Sstevel@tonic-gate 	  case SMFIS_REJECT:
4397c478bd9Sstevel@tonic-gate 		if (ctx->ctx_reply != NULL &&
4407c478bd9Sstevel@tonic-gate 		    ((r == SMFIS_TEMPFAIL && *ctx->ctx_reply == '4') ||
4417c478bd9Sstevel@tonic-gate 		     (r == SMFIS_REJECT && *ctx->ctx_reply == '5')))
4427c478bd9Sstevel@tonic-gate 		{
4437c478bd9Sstevel@tonic-gate 			ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_REPLYCODE,
4447c478bd9Sstevel@tonic-gate 					ctx->ctx_reply,
4457c478bd9Sstevel@tonic-gate 					strlen(ctx->ctx_reply) + 1);
4467c478bd9Sstevel@tonic-gate 			free(ctx->ctx_reply);
4477c478bd9Sstevel@tonic-gate 			ctx->ctx_reply = NULL;
4487c478bd9Sstevel@tonic-gate 		}
4497c478bd9Sstevel@tonic-gate 		else
4507c478bd9Sstevel@tonic-gate 		{
4517c478bd9Sstevel@tonic-gate 			ret = mi_wr_cmd(sd, timeout_ptr, r == SMFIS_REJECT ?
4527c478bd9Sstevel@tonic-gate 					SMFIR_REJECT : SMFIR_TEMPFAIL, NULL, 0);
4537c478bd9Sstevel@tonic-gate 		}
4547c478bd9Sstevel@tonic-gate 		break;
4557c478bd9Sstevel@tonic-gate 	  case SMFIS_DISCARD:
4567c478bd9Sstevel@tonic-gate 		ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_DISCARD, NULL, 0);
4577c478bd9Sstevel@tonic-gate 		break;
4587c478bd9Sstevel@tonic-gate 	  case SMFIS_ACCEPT:
4597c478bd9Sstevel@tonic-gate 		ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_ACCEPT, NULL, 0);
4607c478bd9Sstevel@tonic-gate 		break;
4617c478bd9Sstevel@tonic-gate 	  case _SMFIS_OPTIONS:
4627c478bd9Sstevel@tonic-gate 		{
4637c478bd9Sstevel@tonic-gate 			char buf[MILTER_OPTLEN];
4647c478bd9Sstevel@tonic-gate 			mi_int32 v;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 			v = htonl(ctx->ctx_smfi->xxfi_version);
4677c478bd9Sstevel@tonic-gate 			(void) memcpy(&(buf[0]), (void *) &v, MILTER_LEN_BYTES);
4687c478bd9Sstevel@tonic-gate 			v = htonl(ctx->ctx_smfi->xxfi_flags);
4697c478bd9Sstevel@tonic-gate 			(void) memcpy(&(buf[MILTER_LEN_BYTES]), (void *) &v,
4707c478bd9Sstevel@tonic-gate 				      MILTER_LEN_BYTES);
4717c478bd9Sstevel@tonic-gate 			v = htonl(ctx->ctx_pflags);
4727c478bd9Sstevel@tonic-gate 			(void) memcpy(&(buf[MILTER_LEN_BYTES * 2]), (void *) &v,
4737c478bd9Sstevel@tonic-gate 				      MILTER_LEN_BYTES);
4747c478bd9Sstevel@tonic-gate 			ret = mi_wr_cmd(sd, timeout_ptr, SMFIC_OPTNEG, buf,
4757c478bd9Sstevel@tonic-gate 				       MILTER_OPTLEN);
4767c478bd9Sstevel@tonic-gate 		}
4777c478bd9Sstevel@tonic-gate 		break;
4787c478bd9Sstevel@tonic-gate 	  default:	/* don't send a reply */
4797c478bd9Sstevel@tonic-gate 		break;
4807c478bd9Sstevel@tonic-gate 	}
4817c478bd9Sstevel@tonic-gate 	return ret;
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate /*
4857c478bd9Sstevel@tonic-gate **  CLR_MACROS -- clear set of macros starting from a given index
4867c478bd9Sstevel@tonic-gate **
4877c478bd9Sstevel@tonic-gate **	Parameters:
4887c478bd9Sstevel@tonic-gate **		ctx -- context structure
4897c478bd9Sstevel@tonic-gate **		m -- index from which to clear all macros
4907c478bd9Sstevel@tonic-gate **
4917c478bd9Sstevel@tonic-gate **	Returns:
4927c478bd9Sstevel@tonic-gate **		None.
4937c478bd9Sstevel@tonic-gate */
4947c478bd9Sstevel@tonic-gate void
4957c478bd9Sstevel@tonic-gate mi_clr_macros(ctx, m)
4967c478bd9Sstevel@tonic-gate 	SMFICTX_PTR ctx;
4977c478bd9Sstevel@tonic-gate 	int m;
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate 	int i;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	for (i = m; i < MAX_MACROS_ENTRIES; i++)
5027c478bd9Sstevel@tonic-gate 	{
5037c478bd9Sstevel@tonic-gate 		if (ctx->ctx_mac_ptr[i] != NULL)
5047c478bd9Sstevel@tonic-gate 		{
5057c478bd9Sstevel@tonic-gate 			free(ctx->ctx_mac_ptr[i]);
5067c478bd9Sstevel@tonic-gate 			ctx->ctx_mac_ptr[i] = NULL;
5077c478bd9Sstevel@tonic-gate 		}
5087c478bd9Sstevel@tonic-gate 		if (ctx->ctx_mac_buf[i] != NULL)
5097c478bd9Sstevel@tonic-gate 		{
5107c478bd9Sstevel@tonic-gate 			free(ctx->ctx_mac_buf[i]);
5117c478bd9Sstevel@tonic-gate 			ctx->ctx_mac_buf[i] = NULL;
5127c478bd9Sstevel@tonic-gate 		}
5137c478bd9Sstevel@tonic-gate 	}
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate /*
5167c478bd9Sstevel@tonic-gate **  ST_OPTIONNEG -- negotiate options
5177c478bd9Sstevel@tonic-gate **
5187c478bd9Sstevel@tonic-gate **	Parameters:
5197c478bd9Sstevel@tonic-gate **		g -- generic argument structure
5207c478bd9Sstevel@tonic-gate **
5217c478bd9Sstevel@tonic-gate **	Returns:
5227c478bd9Sstevel@tonic-gate **		abort/send options/continue
5237c478bd9Sstevel@tonic-gate */
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate static int
5267c478bd9Sstevel@tonic-gate st_optionneg(g)
5277c478bd9Sstevel@tonic-gate 	genarg *g;
5287c478bd9Sstevel@tonic-gate {
5297c478bd9Sstevel@tonic-gate 	mi_int32 i, v;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	if (g == NULL || g->a_ctx->ctx_smfi == NULL)
5327c478bd9Sstevel@tonic-gate 		return SMFIS_CONTINUE;
5337c478bd9Sstevel@tonic-gate 	mi_clr_macros(g->a_ctx, g->a_idx + 1);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	/* check for minimum length */
5367c478bd9Sstevel@tonic-gate 	if (g->a_len < MILTER_OPTLEN)
5377c478bd9Sstevel@tonic-gate 	{
5387c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_ERR,
5397c478bd9Sstevel@tonic-gate 			"%s: st_optionneg[%d]: len too short %d < %d",
5407c478bd9Sstevel@tonic-gate 			g->a_ctx->ctx_smfi->xxfi_name,
5417c478bd9Sstevel@tonic-gate 			(int) g->a_ctx->ctx_id, (int) g->a_len,
5427c478bd9Sstevel@tonic-gate 			MILTER_OPTLEN);
5437c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	(void) memcpy((void *) &i, (void *) &(g->a_buf[0]),
5477c478bd9Sstevel@tonic-gate 		      MILTER_LEN_BYTES);
5487c478bd9Sstevel@tonic-gate 	v = ntohl(i);
5497c478bd9Sstevel@tonic-gate 	if (v < g->a_ctx->ctx_smfi->xxfi_version)
5507c478bd9Sstevel@tonic-gate 	{
5517c478bd9Sstevel@tonic-gate 		/* hard failure for now! */
5527c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_ERR,
5537c478bd9Sstevel@tonic-gate 			"%s: st_optionneg[%d]: version mismatch MTA: %d < milter: %d",
5547c478bd9Sstevel@tonic-gate 			g->a_ctx->ctx_smfi->xxfi_name,
5557c478bd9Sstevel@tonic-gate 			(int) g->a_ctx->ctx_id, (int) v,
5567c478bd9Sstevel@tonic-gate 			g->a_ctx->ctx_smfi->xxfi_version);
5577c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	(void) memcpy((void *) &i, (void *) &(g->a_buf[MILTER_LEN_BYTES]),
5617c478bd9Sstevel@tonic-gate 		      MILTER_LEN_BYTES);
5627c478bd9Sstevel@tonic-gate 	v = ntohl(i);
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	/* no flags? set to default value for V1 actions */
5657c478bd9Sstevel@tonic-gate 	if (v == 0)
5667c478bd9Sstevel@tonic-gate 		v = SMFI_V1_ACTS;
5677c478bd9Sstevel@tonic-gate 	i = g->a_ctx->ctx_smfi->xxfi_flags;
5687c478bd9Sstevel@tonic-gate 	if ((v & i) != i)
5697c478bd9Sstevel@tonic-gate 	{
5707c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_ERR,
5717c478bd9Sstevel@tonic-gate 			"%s: st_optionneg[%d]: 0x%x does not fulfill action requirements 0x%x",
5727c478bd9Sstevel@tonic-gate 			g->a_ctx->ctx_smfi->xxfi_name,
5737c478bd9Sstevel@tonic-gate 			(int) g->a_ctx->ctx_id, v, i);
5747c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	(void) memcpy((void *) &i, (void *) &(g->a_buf[MILTER_LEN_BYTES * 2]),
5787c478bd9Sstevel@tonic-gate 		      MILTER_LEN_BYTES);
5797c478bd9Sstevel@tonic-gate 	v = ntohl(i);
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	/* no flags? set to default value for V1 protocol */
5827c478bd9Sstevel@tonic-gate 	if (v == 0)
5837c478bd9Sstevel@tonic-gate 		v = SMFI_V1_PROT;
5847c478bd9Sstevel@tonic-gate 	i = g->a_ctx->ctx_pflags;
5857c478bd9Sstevel@tonic-gate 	if ((v & i) != i)
5867c478bd9Sstevel@tonic-gate 	{
5877c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_ERR,
5887c478bd9Sstevel@tonic-gate 			"%s: st_optionneg[%d]: 0x%x does not fulfill protocol requirements 0x%x",
5897c478bd9Sstevel@tonic-gate 			g->a_ctx->ctx_smfi->xxfi_name,
5907c478bd9Sstevel@tonic-gate 			(int) g->a_ctx->ctx_id, v, i);
5917c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	return _SMFIS_OPTIONS;
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate /*
5977c478bd9Sstevel@tonic-gate **  ST_CONNECTINFO -- receive connection information
5987c478bd9Sstevel@tonic-gate **
5997c478bd9Sstevel@tonic-gate **	Parameters:
6007c478bd9Sstevel@tonic-gate **		g -- generic argument structure
6017c478bd9Sstevel@tonic-gate **
6027c478bd9Sstevel@tonic-gate **	Returns:
6037c478bd9Sstevel@tonic-gate **		continue or filter-specified value
6047c478bd9Sstevel@tonic-gate */
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate static int
6077c478bd9Sstevel@tonic-gate st_connectinfo(g)
6087c478bd9Sstevel@tonic-gate 	genarg *g;
6097c478bd9Sstevel@tonic-gate {
6107c478bd9Sstevel@tonic-gate 	size_t l;
6117c478bd9Sstevel@tonic-gate 	size_t i;
6127c478bd9Sstevel@tonic-gate 	char *s, family;
6137c478bd9Sstevel@tonic-gate 	unsigned short port = 0;
6147c478bd9Sstevel@tonic-gate 	_SOCK_ADDR sockaddr;
6157c478bd9Sstevel@tonic-gate 	sfsistat (*fi_connect) __P((SMFICTX *, char *, _SOCK_ADDR *));
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	if (g == NULL)
6187c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
6197c478bd9Sstevel@tonic-gate 	mi_clr_macros(g->a_ctx, g->a_idx + 1);
6207c478bd9Sstevel@tonic-gate 	if (g->a_ctx->ctx_smfi == NULL ||
6217c478bd9Sstevel@tonic-gate 	    (fi_connect = g->a_ctx->ctx_smfi->xxfi_connect) == NULL)
6227c478bd9Sstevel@tonic-gate 		return SMFIS_CONTINUE;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	s = g->a_buf;
6257c478bd9Sstevel@tonic-gate 	i = 0;
6267c478bd9Sstevel@tonic-gate 	l = g->a_len;
6277c478bd9Sstevel@tonic-gate 	while (s[i] != '\0' && i <= l)
6287c478bd9Sstevel@tonic-gate 		++i;
6297c478bd9Sstevel@tonic-gate 	if (i + 1 >= l)
6307c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	/* Move past trailing \0 in host string */
6337c478bd9Sstevel@tonic-gate 	i++;
6347c478bd9Sstevel@tonic-gate 	family = s[i++];
6357c478bd9Sstevel@tonic-gate 	(void) memset(&sockaddr, '\0', sizeof sockaddr);
6367c478bd9Sstevel@tonic-gate 	if (family != SMFIA_UNKNOWN)
6377c478bd9Sstevel@tonic-gate 	{
6387c478bd9Sstevel@tonic-gate 		if (i + sizeof port >= l)
6397c478bd9Sstevel@tonic-gate 		{
6407c478bd9Sstevel@tonic-gate 			smi_log(SMI_LOG_ERR,
6417c478bd9Sstevel@tonic-gate 				"%s: connect[%d]: wrong len %d >= %d",
6427c478bd9Sstevel@tonic-gate 				g->a_ctx->ctx_smfi->xxfi_name,
6437c478bd9Sstevel@tonic-gate 				(int) g->a_ctx->ctx_id, (int) i, (int) l);
6447c478bd9Sstevel@tonic-gate 			return _SMFIS_ABORT;
6457c478bd9Sstevel@tonic-gate 		}
6467c478bd9Sstevel@tonic-gate 		(void) memcpy((void *) &port, (void *) (s + i),
6477c478bd9Sstevel@tonic-gate 			      sizeof port);
6487c478bd9Sstevel@tonic-gate 		i += sizeof port;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 		/* make sure string is terminated */
6517c478bd9Sstevel@tonic-gate 		if (s[l - 1] != '\0')
6527c478bd9Sstevel@tonic-gate 			return _SMFIS_ABORT;
6537c478bd9Sstevel@tonic-gate # if NETINET
6547c478bd9Sstevel@tonic-gate 		if (family == SMFIA_INET)
6557c478bd9Sstevel@tonic-gate 		{
6567c478bd9Sstevel@tonic-gate 			if (inet_aton(s + i, (struct in_addr *) &sockaddr.sin.sin_addr)
6577c478bd9Sstevel@tonic-gate 			    != 1)
6587c478bd9Sstevel@tonic-gate 			{
6597c478bd9Sstevel@tonic-gate 				smi_log(SMI_LOG_ERR,
6607c478bd9Sstevel@tonic-gate 					"%s: connect[%d]: inet_aton failed",
6617c478bd9Sstevel@tonic-gate 					g->a_ctx->ctx_smfi->xxfi_name,
6627c478bd9Sstevel@tonic-gate 					(int) g->a_ctx->ctx_id);
6637c478bd9Sstevel@tonic-gate 				return _SMFIS_ABORT;
6647c478bd9Sstevel@tonic-gate 			}
6657c478bd9Sstevel@tonic-gate 			sockaddr.sa.sa_family = AF_INET;
6667c478bd9Sstevel@tonic-gate 			if (port > 0)
6677c478bd9Sstevel@tonic-gate 				sockaddr.sin.sin_port = port;
6687c478bd9Sstevel@tonic-gate 		}
6697c478bd9Sstevel@tonic-gate 		else
6707c478bd9Sstevel@tonic-gate # endif /* NETINET */
6717c478bd9Sstevel@tonic-gate # if NETINET6
6727c478bd9Sstevel@tonic-gate 		if (family == SMFIA_INET6)
6737c478bd9Sstevel@tonic-gate 		{
6747c478bd9Sstevel@tonic-gate 			if (mi_inet_pton(AF_INET6, s + i,
6757c478bd9Sstevel@tonic-gate 					 &sockaddr.sin6.sin6_addr) != 1)
6767c478bd9Sstevel@tonic-gate 			{
6777c478bd9Sstevel@tonic-gate 				smi_log(SMI_LOG_ERR,
6787c478bd9Sstevel@tonic-gate 					"%s: connect[%d]: mi_inet_pton failed",
6797c478bd9Sstevel@tonic-gate 					g->a_ctx->ctx_smfi->xxfi_name,
6807c478bd9Sstevel@tonic-gate 					(int) g->a_ctx->ctx_id);
6817c478bd9Sstevel@tonic-gate 				return _SMFIS_ABORT;
6827c478bd9Sstevel@tonic-gate 			}
6837c478bd9Sstevel@tonic-gate 			sockaddr.sa.sa_family = AF_INET6;
6847c478bd9Sstevel@tonic-gate 			if (port > 0)
6857c478bd9Sstevel@tonic-gate 				sockaddr.sin6.sin6_port = port;
6867c478bd9Sstevel@tonic-gate 		}
6877c478bd9Sstevel@tonic-gate 		else
6887c478bd9Sstevel@tonic-gate # endif /* NETINET6 */
6897c478bd9Sstevel@tonic-gate # if NETUNIX
6907c478bd9Sstevel@tonic-gate 		if (family == SMFIA_UNIX)
6917c478bd9Sstevel@tonic-gate 		{
6927c478bd9Sstevel@tonic-gate 			if (sm_strlcpy(sockaddr.sunix.sun_path, s + i,
6937c478bd9Sstevel@tonic-gate 			    sizeof sockaddr.sunix.sun_path) >=
6947c478bd9Sstevel@tonic-gate 			    sizeof sockaddr.sunix.sun_path)
6957c478bd9Sstevel@tonic-gate 			{
6967c478bd9Sstevel@tonic-gate 				smi_log(SMI_LOG_ERR,
6977c478bd9Sstevel@tonic-gate 					"%s: connect[%d]: path too long",
6987c478bd9Sstevel@tonic-gate 					g->a_ctx->ctx_smfi->xxfi_name,
6997c478bd9Sstevel@tonic-gate 					(int) g->a_ctx->ctx_id);
7007c478bd9Sstevel@tonic-gate 				return _SMFIS_ABORT;
7017c478bd9Sstevel@tonic-gate 			}
7027c478bd9Sstevel@tonic-gate 			sockaddr.sunix.sun_family = AF_UNIX;
7037c478bd9Sstevel@tonic-gate 		}
7047c478bd9Sstevel@tonic-gate 		else
7057c478bd9Sstevel@tonic-gate # endif /* NETUNIX */
7067c478bd9Sstevel@tonic-gate 		{
7077c478bd9Sstevel@tonic-gate 			smi_log(SMI_LOG_ERR,
7087c478bd9Sstevel@tonic-gate 				"%s: connect[%d]: unknown family %d",
7097c478bd9Sstevel@tonic-gate 				g->a_ctx->ctx_smfi->xxfi_name,
7107c478bd9Sstevel@tonic-gate 				(int) g->a_ctx->ctx_id, family);
7117c478bd9Sstevel@tonic-gate 			return _SMFIS_ABORT;
7127c478bd9Sstevel@tonic-gate 		}
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 	return (*fi_connect)(g->a_ctx, g->a_buf,
7157c478bd9Sstevel@tonic-gate 			     family != SMFIA_UNKNOWN ? &sockaddr : NULL);
7167c478bd9Sstevel@tonic-gate }
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate /*
7197c478bd9Sstevel@tonic-gate **  ST_EOH -- end of headers
7207c478bd9Sstevel@tonic-gate **
7217c478bd9Sstevel@tonic-gate **	Parameters:
7227c478bd9Sstevel@tonic-gate **		g -- generic argument structure
7237c478bd9Sstevel@tonic-gate **
7247c478bd9Sstevel@tonic-gate **	Returns:
7257c478bd9Sstevel@tonic-gate **		continue or filter-specified value
7267c478bd9Sstevel@tonic-gate */
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate static int
7297c478bd9Sstevel@tonic-gate st_eoh(g)
7307c478bd9Sstevel@tonic-gate 	genarg *g;
7317c478bd9Sstevel@tonic-gate {
7327c478bd9Sstevel@tonic-gate 	sfsistat (*fi_eoh) __P((SMFICTX *));
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	if (g == NULL)
7357c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
7367c478bd9Sstevel@tonic-gate 	if (g->a_ctx->ctx_smfi != NULL &&
7377c478bd9Sstevel@tonic-gate 	    (fi_eoh = g->a_ctx->ctx_smfi->xxfi_eoh) != NULL)
7387c478bd9Sstevel@tonic-gate 		return (*fi_eoh)(g->a_ctx);
7397c478bd9Sstevel@tonic-gate 	return SMFIS_CONTINUE;
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate #if SMFI_VERSION > 3
7437c478bd9Sstevel@tonic-gate /*
7447c478bd9Sstevel@tonic-gate **  ST_DATA -- DATA command
7457c478bd9Sstevel@tonic-gate **
7467c478bd9Sstevel@tonic-gate **	Parameters:
7477c478bd9Sstevel@tonic-gate **		g -- generic argument structure
7487c478bd9Sstevel@tonic-gate **
7497c478bd9Sstevel@tonic-gate **	Returns:
7507c478bd9Sstevel@tonic-gate **		continue or filter-specified value
7517c478bd9Sstevel@tonic-gate */
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate static int
7547c478bd9Sstevel@tonic-gate st_data(g)
7557c478bd9Sstevel@tonic-gate 	genarg *g;
7567c478bd9Sstevel@tonic-gate {
7577c478bd9Sstevel@tonic-gate 	sfsistat (*fi_data) __P((SMFICTX *));
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	if (g == NULL)
7607c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
7617c478bd9Sstevel@tonic-gate 	if (g->a_ctx->ctx_smfi != NULL &&
7627c478bd9Sstevel@tonic-gate 	    (fi_data = g->a_ctx->ctx_smfi->xxfi_data) != NULL)
7637c478bd9Sstevel@tonic-gate 		return (*fi_data)(g->a_ctx);
7647c478bd9Sstevel@tonic-gate 	return SMFIS_CONTINUE;
7657c478bd9Sstevel@tonic-gate }
7667c478bd9Sstevel@tonic-gate #endif /* SMFI_VERSION > 3 */
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate /*
7697c478bd9Sstevel@tonic-gate **  ST_HELO -- helo/ehlo command
7707c478bd9Sstevel@tonic-gate **
7717c478bd9Sstevel@tonic-gate **	Parameters:
7727c478bd9Sstevel@tonic-gate **		g -- generic argument structure
7737c478bd9Sstevel@tonic-gate **
7747c478bd9Sstevel@tonic-gate **	Returns:
7757c478bd9Sstevel@tonic-gate **		continue or filter-specified value
7767c478bd9Sstevel@tonic-gate */
7777c478bd9Sstevel@tonic-gate static int
7787c478bd9Sstevel@tonic-gate st_helo(g)
7797c478bd9Sstevel@tonic-gate 	genarg *g;
7807c478bd9Sstevel@tonic-gate {
7817c478bd9Sstevel@tonic-gate 	sfsistat (*fi_helo) __P((SMFICTX *, char *));
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	if (g == NULL)
7847c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
7857c478bd9Sstevel@tonic-gate 	mi_clr_macros(g->a_ctx, g->a_idx + 1);
7867c478bd9Sstevel@tonic-gate 	if (g->a_ctx->ctx_smfi != NULL &&
7877c478bd9Sstevel@tonic-gate 	    (fi_helo = g->a_ctx->ctx_smfi->xxfi_helo) != NULL)
7887c478bd9Sstevel@tonic-gate 	{
7897c478bd9Sstevel@tonic-gate 		/* paranoia: check for terminating '\0' */
7907c478bd9Sstevel@tonic-gate 		if (g->a_len == 0 || g->a_buf[g->a_len - 1] != '\0')
7917c478bd9Sstevel@tonic-gate 			return MI_FAILURE;
7927c478bd9Sstevel@tonic-gate 		return (*fi_helo)(g->a_ctx, g->a_buf);
7937c478bd9Sstevel@tonic-gate 	}
7947c478bd9Sstevel@tonic-gate 	return SMFIS_CONTINUE;
7957c478bd9Sstevel@tonic-gate }
7967c478bd9Sstevel@tonic-gate /*
7977c478bd9Sstevel@tonic-gate **  ST_HEADER -- header line
7987c478bd9Sstevel@tonic-gate **
7997c478bd9Sstevel@tonic-gate **	Parameters:
8007c478bd9Sstevel@tonic-gate **		g -- generic argument structure
8017c478bd9Sstevel@tonic-gate **
8027c478bd9Sstevel@tonic-gate **	Returns:
8037c478bd9Sstevel@tonic-gate **		continue or filter-specified value
8047c478bd9Sstevel@tonic-gate */
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate static int
8077c478bd9Sstevel@tonic-gate st_header(g)
8087c478bd9Sstevel@tonic-gate 	genarg *g;
8097c478bd9Sstevel@tonic-gate {
8107c478bd9Sstevel@tonic-gate 	char *hf, *hv;
8117c478bd9Sstevel@tonic-gate 	sfsistat (*fi_header) __P((SMFICTX *, char *, char *));
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	if (g == NULL)
8147c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
8157c478bd9Sstevel@tonic-gate 	if (g->a_ctx->ctx_smfi == NULL ||
8167c478bd9Sstevel@tonic-gate 	    (fi_header = g->a_ctx->ctx_smfi->xxfi_header) == NULL)
8177c478bd9Sstevel@tonic-gate 		return SMFIS_CONTINUE;
8187c478bd9Sstevel@tonic-gate 	if (dec_arg2(g->a_buf, g->a_len, &hf, &hv) == MI_SUCCESS)
8197c478bd9Sstevel@tonic-gate 		return (*fi_header)(g->a_ctx, hf, hv);
8207c478bd9Sstevel@tonic-gate 	else
8217c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
8227c478bd9Sstevel@tonic-gate }
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate #define ARGV_FCT(lf, rf, idx)					\
8257c478bd9Sstevel@tonic-gate 	char **argv;						\
8267c478bd9Sstevel@tonic-gate 	sfsistat (*lf) __P((SMFICTX *, char **));		\
8277c478bd9Sstevel@tonic-gate 	int r;							\
8287c478bd9Sstevel@tonic-gate 								\
8297c478bd9Sstevel@tonic-gate 	if (g == NULL)						\
8307c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;				\
8317c478bd9Sstevel@tonic-gate 	mi_clr_macros(g->a_ctx, g->a_idx + 1);			\
8327c478bd9Sstevel@tonic-gate 	if (g->a_ctx->ctx_smfi == NULL ||			\
8337c478bd9Sstevel@tonic-gate 	    (lf = g->a_ctx->ctx_smfi->rf) == NULL)		\
8347c478bd9Sstevel@tonic-gate 		return SMFIS_CONTINUE;				\
8357c478bd9Sstevel@tonic-gate 	if ((argv = dec_argv(g->a_buf, g->a_len)) == NULL)	\
8367c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;				\
8377c478bd9Sstevel@tonic-gate 	r = (*lf)(g->a_ctx, argv);				\
8387c478bd9Sstevel@tonic-gate 	free(argv);						\
8397c478bd9Sstevel@tonic-gate 	return r;
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate /*
8427c478bd9Sstevel@tonic-gate **  ST_SENDER -- MAIL FROM command
8437c478bd9Sstevel@tonic-gate **
8447c478bd9Sstevel@tonic-gate **	Parameters:
8457c478bd9Sstevel@tonic-gate **		g -- generic argument structure
8467c478bd9Sstevel@tonic-gate **
8477c478bd9Sstevel@tonic-gate **	Returns:
8487c478bd9Sstevel@tonic-gate **		continue or filter-specified value
8497c478bd9Sstevel@tonic-gate */
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate static int
8527c478bd9Sstevel@tonic-gate st_sender(g)
8537c478bd9Sstevel@tonic-gate 	genarg *g;
8547c478bd9Sstevel@tonic-gate {
8557c478bd9Sstevel@tonic-gate 	ARGV_FCT(fi_envfrom, xxfi_envfrom, CI_MAIL)
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate /*
8587c478bd9Sstevel@tonic-gate **  ST_RCPT -- RCPT TO command
8597c478bd9Sstevel@tonic-gate **
8607c478bd9Sstevel@tonic-gate **	Parameters:
8617c478bd9Sstevel@tonic-gate **		g -- generic argument structure
8627c478bd9Sstevel@tonic-gate **
8637c478bd9Sstevel@tonic-gate **	Returns:
8647c478bd9Sstevel@tonic-gate **		continue or filter-specified value
8657c478bd9Sstevel@tonic-gate */
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate static int
8687c478bd9Sstevel@tonic-gate st_rcpt(g)
8697c478bd9Sstevel@tonic-gate 	genarg *g;
8707c478bd9Sstevel@tonic-gate {
8717c478bd9Sstevel@tonic-gate 	ARGV_FCT(fi_envrcpt, xxfi_envrcpt, CI_RCPT)
8727c478bd9Sstevel@tonic-gate }
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate #if SMFI_VERSION > 2
8757c478bd9Sstevel@tonic-gate /*
8767c478bd9Sstevel@tonic-gate **  ST_UNKNOWN -- unrecognized or unimplemented command
8777c478bd9Sstevel@tonic-gate **
8787c478bd9Sstevel@tonic-gate **	Parameters:
8797c478bd9Sstevel@tonic-gate **		g -- generic argument structure
8807c478bd9Sstevel@tonic-gate **
8817c478bd9Sstevel@tonic-gate **	Returns:
8827c478bd9Sstevel@tonic-gate **		continue or filter-specified value
8837c478bd9Sstevel@tonic-gate */
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate static int
8867c478bd9Sstevel@tonic-gate st_unknown(g)
8877c478bd9Sstevel@tonic-gate 	genarg *g;
8887c478bd9Sstevel@tonic-gate {
8897c478bd9Sstevel@tonic-gate 	sfsistat (*fi_unknown) __P((SMFICTX *, char *));
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	if (g == NULL)
8927c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
8937c478bd9Sstevel@tonic-gate 	mi_clr_macros(g->a_ctx, g->a_idx + 1);
8947c478bd9Sstevel@tonic-gate 	if (g->a_ctx->ctx_smfi != NULL &&
8957c478bd9Sstevel@tonic-gate 	    (fi_unknown = g->a_ctx->ctx_smfi->xxfi_unknown) != NULL)
8967c478bd9Sstevel@tonic-gate 		return (*fi_unknown)(g->a_ctx, g->a_buf);
8977c478bd9Sstevel@tonic-gate 	return SMFIS_CONTINUE;
8987c478bd9Sstevel@tonic-gate }
8997c478bd9Sstevel@tonic-gate #endif /* SMFI_VERSION > 2 */
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate /*
9027c478bd9Sstevel@tonic-gate **  ST_MACROS -- deal with macros received from the MTA
9037c478bd9Sstevel@tonic-gate **
9047c478bd9Sstevel@tonic-gate **	Parameters:
9057c478bd9Sstevel@tonic-gate **		g -- generic argument structure
9067c478bd9Sstevel@tonic-gate **
9077c478bd9Sstevel@tonic-gate **	Returns:
9087c478bd9Sstevel@tonic-gate **		continue/keep
9097c478bd9Sstevel@tonic-gate **
9107c478bd9Sstevel@tonic-gate **	Side effects:
9117c478bd9Sstevel@tonic-gate **		set pointer in macro array to current values.
9127c478bd9Sstevel@tonic-gate */
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate static int
9157c478bd9Sstevel@tonic-gate st_macros(g)
9167c478bd9Sstevel@tonic-gate 	genarg *g;
9177c478bd9Sstevel@tonic-gate {
9187c478bd9Sstevel@tonic-gate 	int i;
9197c478bd9Sstevel@tonic-gate 	char **argv;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	if (g == NULL || g->a_len < 1)
9227c478bd9Sstevel@tonic-gate 		return _SMFIS_FAIL;
9237c478bd9Sstevel@tonic-gate 	if ((argv = dec_argv(g->a_buf + 1, g->a_len - 1)) == NULL)
9247c478bd9Sstevel@tonic-gate 		return _SMFIS_FAIL;
9257c478bd9Sstevel@tonic-gate 	switch (g->a_buf[0])
9267c478bd9Sstevel@tonic-gate 	{
9277c478bd9Sstevel@tonic-gate 	  case SMFIC_CONNECT:
9287c478bd9Sstevel@tonic-gate 		i = CI_CONN;
9297c478bd9Sstevel@tonic-gate 		break;
9307c478bd9Sstevel@tonic-gate 	  case SMFIC_HELO:
9317c478bd9Sstevel@tonic-gate 		i = CI_HELO;
9327c478bd9Sstevel@tonic-gate 		break;
9337c478bd9Sstevel@tonic-gate 	  case SMFIC_MAIL:
9347c478bd9Sstevel@tonic-gate 		i = CI_MAIL;
9357c478bd9Sstevel@tonic-gate 		break;
9367c478bd9Sstevel@tonic-gate 	  case SMFIC_RCPT:
9377c478bd9Sstevel@tonic-gate 		i = CI_RCPT;
9387c478bd9Sstevel@tonic-gate 		break;
9397c478bd9Sstevel@tonic-gate 	  case SMFIC_BODYEOB:
9407c478bd9Sstevel@tonic-gate 		i = CI_EOM;
9417c478bd9Sstevel@tonic-gate 		break;
9427c478bd9Sstevel@tonic-gate 	  default:
9437c478bd9Sstevel@tonic-gate 		free(argv);
9447c478bd9Sstevel@tonic-gate 		return _SMFIS_FAIL;
9457c478bd9Sstevel@tonic-gate 	}
9467c478bd9Sstevel@tonic-gate 	if (g->a_ctx->ctx_mac_ptr[i] != NULL)
9477c478bd9Sstevel@tonic-gate 		free(g->a_ctx->ctx_mac_ptr[i]);
9487c478bd9Sstevel@tonic-gate 	if (g->a_ctx->ctx_mac_buf[i] != NULL)
9497c478bd9Sstevel@tonic-gate 		free(g->a_ctx->ctx_mac_buf[i]);
9507c478bd9Sstevel@tonic-gate 	g->a_ctx->ctx_mac_ptr[i] = argv;
9517c478bd9Sstevel@tonic-gate 	g->a_ctx->ctx_mac_buf[i] = g->a_buf;
9527c478bd9Sstevel@tonic-gate 	return _SMFIS_KEEP;
9537c478bd9Sstevel@tonic-gate }
9547c478bd9Sstevel@tonic-gate /*
9557c478bd9Sstevel@tonic-gate **  ST_QUIT -- quit command
9567c478bd9Sstevel@tonic-gate **
9577c478bd9Sstevel@tonic-gate **	Parameters:
9587c478bd9Sstevel@tonic-gate **		g -- generic argument structure
9597c478bd9Sstevel@tonic-gate **
9607c478bd9Sstevel@tonic-gate **	Returns:
9617c478bd9Sstevel@tonic-gate **		noreply
9627c478bd9Sstevel@tonic-gate */
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate /* ARGSUSED */
9657c478bd9Sstevel@tonic-gate static int
9667c478bd9Sstevel@tonic-gate st_quit(g)
9677c478bd9Sstevel@tonic-gate 	genarg *g;
9687c478bd9Sstevel@tonic-gate {
9697c478bd9Sstevel@tonic-gate 	return _SMFIS_NOREPLY;
9707c478bd9Sstevel@tonic-gate }
9717c478bd9Sstevel@tonic-gate /*
9727c478bd9Sstevel@tonic-gate **  ST_BODYCHUNK -- deal with a piece of the mail body
9737c478bd9Sstevel@tonic-gate **
9747c478bd9Sstevel@tonic-gate **	Parameters:
9757c478bd9Sstevel@tonic-gate **		g -- generic argument structure
9767c478bd9Sstevel@tonic-gate **
9777c478bd9Sstevel@tonic-gate **	Returns:
9787c478bd9Sstevel@tonic-gate **		continue or filter-specified value
9797c478bd9Sstevel@tonic-gate */
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate static int
9827c478bd9Sstevel@tonic-gate st_bodychunk(g)
9837c478bd9Sstevel@tonic-gate 	genarg *g;
9847c478bd9Sstevel@tonic-gate {
9857c478bd9Sstevel@tonic-gate 	sfsistat (*fi_body) __P((SMFICTX *, unsigned char *, size_t));
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	if (g == NULL)
9887c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
9897c478bd9Sstevel@tonic-gate 	if (g->a_ctx->ctx_smfi != NULL &&
9907c478bd9Sstevel@tonic-gate 	    (fi_body = g->a_ctx->ctx_smfi->xxfi_body) != NULL)
9917c478bd9Sstevel@tonic-gate 		return (*fi_body)(g->a_ctx, (unsigned char *)g->a_buf,
9927c478bd9Sstevel@tonic-gate 				  g->a_len);
9937c478bd9Sstevel@tonic-gate 	return SMFIS_CONTINUE;
9947c478bd9Sstevel@tonic-gate }
9957c478bd9Sstevel@tonic-gate /*
9967c478bd9Sstevel@tonic-gate **  ST_BODYEND -- deal with the last piece of the mail body
9977c478bd9Sstevel@tonic-gate **
9987c478bd9Sstevel@tonic-gate **	Parameters:
9997c478bd9Sstevel@tonic-gate **		g -- generic argument structure
10007c478bd9Sstevel@tonic-gate **
10017c478bd9Sstevel@tonic-gate **	Returns:
10027c478bd9Sstevel@tonic-gate **		continue or filter-specified value
10037c478bd9Sstevel@tonic-gate **
10047c478bd9Sstevel@tonic-gate **	Side effects:
10057c478bd9Sstevel@tonic-gate **		sends a reply for the body part (if non-empty).
10067c478bd9Sstevel@tonic-gate */
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate static int
10097c478bd9Sstevel@tonic-gate st_bodyend(g)
10107c478bd9Sstevel@tonic-gate 	genarg *g;
10117c478bd9Sstevel@tonic-gate {
10127c478bd9Sstevel@tonic-gate 	sfsistat r;
10137c478bd9Sstevel@tonic-gate 	sfsistat (*fi_body) __P((SMFICTX *, unsigned char *, size_t));
10147c478bd9Sstevel@tonic-gate 	sfsistat (*fi_eom) __P((SMFICTX *));
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	if (g == NULL)
10177c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
10187c478bd9Sstevel@tonic-gate 	r = SMFIS_CONTINUE;
10197c478bd9Sstevel@tonic-gate 	if (g->a_ctx->ctx_smfi != NULL)
10207c478bd9Sstevel@tonic-gate 	{
10217c478bd9Sstevel@tonic-gate 		if ((fi_body = g->a_ctx->ctx_smfi->xxfi_body) != NULL &&
10227c478bd9Sstevel@tonic-gate 		    g->a_len > 0)
10237c478bd9Sstevel@tonic-gate 		{
10247c478bd9Sstevel@tonic-gate 			socket_t sd;
10257c478bd9Sstevel@tonic-gate 			struct timeval timeout;
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 			timeout.tv_sec = g->a_ctx->ctx_timeout;
10287c478bd9Sstevel@tonic-gate 			timeout.tv_usec = 0;
10297c478bd9Sstevel@tonic-gate 			sd = g->a_ctx->ctx_sd;
10307c478bd9Sstevel@tonic-gate 			r = (*fi_body)(g->a_ctx, (unsigned char *)g->a_buf,
10317c478bd9Sstevel@tonic-gate 				       g->a_len);
10327c478bd9Sstevel@tonic-gate 			if (r != SMFIS_CONTINUE &&
10337c478bd9Sstevel@tonic-gate 			    sendreply(r, sd, &timeout, g->a_ctx) != MI_SUCCESS)
10347c478bd9Sstevel@tonic-gate 				return _SMFIS_ABORT;
10357c478bd9Sstevel@tonic-gate 		}
10367c478bd9Sstevel@tonic-gate 	}
10377c478bd9Sstevel@tonic-gate 	if (r == SMFIS_CONTINUE &&
10387c478bd9Sstevel@tonic-gate 	    (fi_eom = g->a_ctx->ctx_smfi->xxfi_eom) != NULL)
10397c478bd9Sstevel@tonic-gate 		return (*fi_eom)(g->a_ctx);
10407c478bd9Sstevel@tonic-gate 	return r;
10417c478bd9Sstevel@tonic-gate }
10427c478bd9Sstevel@tonic-gate /*
10437c478bd9Sstevel@tonic-gate **  ST_ABORTFCT -- deal with aborts
10447c478bd9Sstevel@tonic-gate **
10457c478bd9Sstevel@tonic-gate **	Parameters:
10467c478bd9Sstevel@tonic-gate **		g -- generic argument structure
10477c478bd9Sstevel@tonic-gate **
10487c478bd9Sstevel@tonic-gate **	Returns:
10497c478bd9Sstevel@tonic-gate **		abort or filter-specified value
10507c478bd9Sstevel@tonic-gate */
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate static int
10537c478bd9Sstevel@tonic-gate st_abortfct(g)
10547c478bd9Sstevel@tonic-gate 	genarg *g;
10557c478bd9Sstevel@tonic-gate {
10567c478bd9Sstevel@tonic-gate 	sfsistat (*fi_abort) __P((SMFICTX *));
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	if (g == NULL)
10597c478bd9Sstevel@tonic-gate 		return _SMFIS_ABORT;
10607c478bd9Sstevel@tonic-gate 	if (g != NULL && g->a_ctx->ctx_smfi != NULL &&
10617c478bd9Sstevel@tonic-gate 	    (fi_abort = g->a_ctx->ctx_smfi->xxfi_abort) != NULL)
10627c478bd9Sstevel@tonic-gate 		(void) (*fi_abort)(g->a_ctx);
10637c478bd9Sstevel@tonic-gate 	return _SMFIS_NOREPLY;
10647c478bd9Sstevel@tonic-gate }
10657c478bd9Sstevel@tonic-gate /*
10667c478bd9Sstevel@tonic-gate **  TRANS_OK -- is the state transition ok?
10677c478bd9Sstevel@tonic-gate **
10687c478bd9Sstevel@tonic-gate **	Parameters:
10697c478bd9Sstevel@tonic-gate **		old -- old state
10707c478bd9Sstevel@tonic-gate **		new -- new state
10717c478bd9Sstevel@tonic-gate **
10727c478bd9Sstevel@tonic-gate **	Returns:
10737c478bd9Sstevel@tonic-gate **		state transition ok
10747c478bd9Sstevel@tonic-gate */
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate static bool
10777c478bd9Sstevel@tonic-gate trans_ok(old, new)
10787c478bd9Sstevel@tonic-gate 	int old, new;
10797c478bd9Sstevel@tonic-gate {
10807c478bd9Sstevel@tonic-gate 	int s, n;
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	s = old;
1083*3ee0e492Sjbeck 	if (s >= SIZE_NEXT_STATES)
1084*3ee0e492Sjbeck 		return false;
10857c478bd9Sstevel@tonic-gate 	do
10867c478bd9Sstevel@tonic-gate 	{
10877c478bd9Sstevel@tonic-gate 		/* is this state transition allowed? */
10887c478bd9Sstevel@tonic-gate 		if ((MI_MASK(new) & next_states[s]) != 0)
10897c478bd9Sstevel@tonic-gate 			return true;
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 		/*
10927c478bd9Sstevel@tonic-gate 		**  no: try next state;
10937c478bd9Sstevel@tonic-gate 		**  this works since the relevant states are ordered
10947c478bd9Sstevel@tonic-gate 		**  strict sequentially
10957c478bd9Sstevel@tonic-gate 		*/
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 		n = s + 1;
1098*3ee0e492Sjbeck 		if (n >= SIZE_NEXT_STATES)
1099*3ee0e492Sjbeck 			return false;
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 		/*
11027c478bd9Sstevel@tonic-gate 		**  can we actually "skip" this state?
11037c478bd9Sstevel@tonic-gate 		**  see fix_stm() which sets this bit for those
11047c478bd9Sstevel@tonic-gate 		**  states which the filter program is not interested in
11057c478bd9Sstevel@tonic-gate 		*/
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 		if (bitset(NX_SKIP, next_states[n]))
11087c478bd9Sstevel@tonic-gate 			s = n;
11097c478bd9Sstevel@tonic-gate 		else
11107c478bd9Sstevel@tonic-gate 			return false;
1111*3ee0e492Sjbeck 	} while (s < SIZE_NEXT_STATES);
11127c478bd9Sstevel@tonic-gate 	return false;
11137c478bd9Sstevel@tonic-gate }
11147c478bd9Sstevel@tonic-gate /*
11157c478bd9Sstevel@tonic-gate **  FIX_STM -- add "skip" bits to the state transition table
11167c478bd9Sstevel@tonic-gate **
11177c478bd9Sstevel@tonic-gate **	Parameters:
11187c478bd9Sstevel@tonic-gate **		ctx -- context structure
11197c478bd9Sstevel@tonic-gate **
11207c478bd9Sstevel@tonic-gate **	Returns:
11217c478bd9Sstevel@tonic-gate **		None.
11227c478bd9Sstevel@tonic-gate **
11237c478bd9Sstevel@tonic-gate **	Side effects:
11247c478bd9Sstevel@tonic-gate **		may change state transition table.
11257c478bd9Sstevel@tonic-gate */
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate static void
11287c478bd9Sstevel@tonic-gate fix_stm(ctx)
11297c478bd9Sstevel@tonic-gate 	SMFICTX_PTR ctx;
11307c478bd9Sstevel@tonic-gate {
11317c478bd9Sstevel@tonic-gate 	unsigned long fl;
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	if (ctx == NULL || ctx->ctx_smfi == NULL)
11347c478bd9Sstevel@tonic-gate 		return;
11357c478bd9Sstevel@tonic-gate 	fl = ctx->ctx_pflags;
11367c478bd9Sstevel@tonic-gate 	if (bitset(SMFIP_NOCONNECT, fl))
11377c478bd9Sstevel@tonic-gate 		next_states[ST_CONN] |= NX_SKIP;
11387c478bd9Sstevel@tonic-gate 	if (bitset(SMFIP_NOHELO, fl))
11397c478bd9Sstevel@tonic-gate 		next_states[ST_HELO] |= NX_SKIP;
11407c478bd9Sstevel@tonic-gate 	if (bitset(SMFIP_NOMAIL, fl))
11417c478bd9Sstevel@tonic-gate 		next_states[ST_MAIL] |= NX_SKIP;
11427c478bd9Sstevel@tonic-gate 	if (bitset(SMFIP_NORCPT, fl))
11437c478bd9Sstevel@tonic-gate 		next_states[ST_RCPT] |= NX_SKIP;
11447c478bd9Sstevel@tonic-gate 	if (bitset(SMFIP_NOHDRS, fl))
11457c478bd9Sstevel@tonic-gate 		next_states[ST_HDRS] |= NX_SKIP;
11467c478bd9Sstevel@tonic-gate 	if (bitset(SMFIP_NOEOH, fl))
11477c478bd9Sstevel@tonic-gate 		next_states[ST_EOHS] |= NX_SKIP;
11487c478bd9Sstevel@tonic-gate 	if (bitset(SMFIP_NOBODY, fl))
11497c478bd9Sstevel@tonic-gate 		next_states[ST_BODY] |= NX_SKIP;
11507c478bd9Sstevel@tonic-gate }
11517c478bd9Sstevel@tonic-gate /*
11527c478bd9Sstevel@tonic-gate **  DEC_ARGV -- split a buffer into a list of strings, NULL terminated
11537c478bd9Sstevel@tonic-gate **
11547c478bd9Sstevel@tonic-gate **	Parameters:
11557c478bd9Sstevel@tonic-gate **		buf -- buffer with several strings
11567c478bd9Sstevel@tonic-gate **		len -- length of buffer
11577c478bd9Sstevel@tonic-gate **
11587c478bd9Sstevel@tonic-gate **	Returns:
11597c478bd9Sstevel@tonic-gate **		array of pointers to the individual strings
11607c478bd9Sstevel@tonic-gate */
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate static char **
11637c478bd9Sstevel@tonic-gate dec_argv(buf, len)
11647c478bd9Sstevel@tonic-gate 	char *buf;
11657c478bd9Sstevel@tonic-gate 	size_t len;
11667c478bd9Sstevel@tonic-gate {
11677c478bd9Sstevel@tonic-gate 	char **s;
11687c478bd9Sstevel@tonic-gate 	size_t i;
11697c478bd9Sstevel@tonic-gate 	int elem, nelem;
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	nelem = 0;
11727c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++)
11737c478bd9Sstevel@tonic-gate 	{
11747c478bd9Sstevel@tonic-gate 		if (buf[i] == '\0')
11757c478bd9Sstevel@tonic-gate 			++nelem;
11767c478bd9Sstevel@tonic-gate 	}
11777c478bd9Sstevel@tonic-gate 	if (nelem == 0)
11787c478bd9Sstevel@tonic-gate 		return NULL;
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 	/* last entry is only for the name */
11817c478bd9Sstevel@tonic-gate 	s = (char **)malloc((nelem + 1) * (sizeof *s));
11827c478bd9Sstevel@tonic-gate 	if (s == NULL)
11837c478bd9Sstevel@tonic-gate 		return NULL;
11847c478bd9Sstevel@tonic-gate 	s[0] = buf;
11857c478bd9Sstevel@tonic-gate 	for (i = 0, elem = 0; i < len && elem < nelem; i++)
11867c478bd9Sstevel@tonic-gate 	{
11877c478bd9Sstevel@tonic-gate 		if (buf[i] == '\0')
11887c478bd9Sstevel@tonic-gate 		{
11897c478bd9Sstevel@tonic-gate 			++elem;
11907c478bd9Sstevel@tonic-gate 			if (i + 1 >= len)
11917c478bd9Sstevel@tonic-gate 				s[elem] = NULL;
11927c478bd9Sstevel@tonic-gate 			else
11937c478bd9Sstevel@tonic-gate 				s[elem] = &(buf[i + 1]);
11947c478bd9Sstevel@tonic-gate 		}
11957c478bd9Sstevel@tonic-gate 	}
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 	/* overwrite last entry (already done above, just paranoia) */
11987c478bd9Sstevel@tonic-gate 	s[elem] = NULL;
11997c478bd9Sstevel@tonic-gate 	return s;
12007c478bd9Sstevel@tonic-gate }
12017c478bd9Sstevel@tonic-gate /*
12027c478bd9Sstevel@tonic-gate **  DEC_ARG2 -- split a buffer into two strings
12037c478bd9Sstevel@tonic-gate **
12047c478bd9Sstevel@tonic-gate **	Parameters:
12057c478bd9Sstevel@tonic-gate **		buf -- buffer with two strings
12067c478bd9Sstevel@tonic-gate **		len -- length of buffer
12077c478bd9Sstevel@tonic-gate **		s1,s2 -- pointer to result strings
12087c478bd9Sstevel@tonic-gate **
12097c478bd9Sstevel@tonic-gate **	Returns:
12107c478bd9Sstevel@tonic-gate **		MI_FAILURE/MI_SUCCESS
12117c478bd9Sstevel@tonic-gate */
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate static int
12147c478bd9Sstevel@tonic-gate dec_arg2(buf, len, s1, s2)
12157c478bd9Sstevel@tonic-gate 	char *buf;
12167c478bd9Sstevel@tonic-gate 	size_t len;
12177c478bd9Sstevel@tonic-gate 	char **s1;
12187c478bd9Sstevel@tonic-gate 	char **s2;
12197c478bd9Sstevel@tonic-gate {
12207c478bd9Sstevel@tonic-gate 	size_t i;
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 	/* paranoia: check for terminating '\0' */
12237c478bd9Sstevel@tonic-gate 	if (len == 0 || buf[len - 1] != '\0')
12247c478bd9Sstevel@tonic-gate 		return MI_FAILURE;
12257c478bd9Sstevel@tonic-gate 	*s1 = buf;
12267c478bd9Sstevel@tonic-gate 	for (i = 1; i < len && buf[i] != '\0'; i++)
12277c478bd9Sstevel@tonic-gate 		continue;
12287c478bd9Sstevel@tonic-gate 	if (i >= len - 1)
12297c478bd9Sstevel@tonic-gate 		return MI_FAILURE;
12307c478bd9Sstevel@tonic-gate 	*s2 = buf + i + 1;
12317c478bd9Sstevel@tonic-gate 	return MI_SUCCESS;
12327c478bd9Sstevel@tonic-gate }
12337c478bd9Sstevel@tonic-gate /*
12347c478bd9Sstevel@tonic-gate **  SENDOK -- is it ok for the filter to send stuff to the MTA?
12357c478bd9Sstevel@tonic-gate **
12367c478bd9Sstevel@tonic-gate **	Parameters:
12377c478bd9Sstevel@tonic-gate **		ctx -- context structure
12387c478bd9Sstevel@tonic-gate **		flag -- flag to check
12397c478bd9Sstevel@tonic-gate **
12407c478bd9Sstevel@tonic-gate **	Returns:
12417c478bd9Sstevel@tonic-gate **		sending allowed (in current state)
12427c478bd9Sstevel@tonic-gate */
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate bool
12457c478bd9Sstevel@tonic-gate mi_sendok(ctx, flag)
12467c478bd9Sstevel@tonic-gate 	SMFICTX_PTR ctx;
12477c478bd9Sstevel@tonic-gate 	int flag;
12487c478bd9Sstevel@tonic-gate {
12497c478bd9Sstevel@tonic-gate 	if (ctx == NULL || ctx->ctx_smfi == NULL)
12507c478bd9Sstevel@tonic-gate 		return false;
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	/* did the milter request this operation? */
12537c478bd9Sstevel@tonic-gate 	if (flag != 0 && !bitset(flag, ctx->ctx_smfi->xxfi_flags))
12547c478bd9Sstevel@tonic-gate 		return false;
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 	/* are we in the correct state? It must be "End of Message". */
12577c478bd9Sstevel@tonic-gate 	return ctx->ctx_state == ST_ENDM;
12587c478bd9Sstevel@tonic-gate }
1259