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