xref: /freebsd/contrib/sendmail/libmilter/engine.c (revision 195ebc7e9e4b129de810833791a19dfb4349d6a9)
1 /*
2  *  Copyright (c) 1999-2004, 2006-2008 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 #include <sm/gen.h>
12 SM_RCSID("@(#)$Id: engine.c,v 8.162 2008/02/27 01:34:14 ca Exp $")
13 
14 #include "libmilter.h"
15 
16 #if NETINET || NETINET6
17 # include <arpa/inet.h>
18 #endif /* NETINET || NETINET6 */
19 
20 /* generic argument for functions in the command table */
21 struct arg_struct
22 {
23 	size_t		a_len;		/* length of buffer */
24 	char		*a_buf;		/* argument string */
25 	int		a_idx;		/* index for macro array */
26 	SMFICTX_PTR	a_ctx;		/* context */
27 };
28 
29 typedef struct arg_struct genarg;
30 
31 /* structure for commands received from MTA */
32 struct cmdfct_t
33 {
34 	char	cm_cmd;				/* command */
35 	int	cm_argt;			/* type of arguments expected */
36 	int	cm_next;			/* next state */
37 	int	cm_todo;			/* what to do next */
38 	int	cm_macros;			/* index for macros */
39 	int	(*cm_fct) __P((genarg *));	/* function to execute */
40 };
41 
42 typedef struct cmdfct_t cmdfct;
43 
44 /* possible values for cm_argt */
45 #define	CM_ARG0	0	/* no args */
46 #define	CM_ARG1	1	/* one arg (string) */
47 #define	CM_ARG2	2	/* two args (strings) */
48 #define	CM_ARGA	4	/* one string and _SOCK_ADDR */
49 #define	CM_ARGO	5	/* two integers */
50 #define	CM_ARGV	8	/* \0 separated list of args, NULL-terminated */
51 #define	CM_ARGN	9	/* \0 separated list of args (strings) */
52 
53 /* possible values for cm_todo */
54 #define	CT_CONT		0x0000	/* continue reading commands */
55 #define	CT_IGNO		0x0001	/* continue even when error  */
56 
57 /* not needed right now, done via return code instead */
58 #define	CT_KEEP		0x0004	/* keep buffer (contains symbols) */
59 #define	CT_END		0x0008	/* last command of session, stop replying */
60 
61 /* index in macro array: macros only for these commands */
62 #define	CI_NONE		(-1)
63 #define	CI_CONN		0
64 #define	CI_HELO		1
65 #define	CI_MAIL		2
66 #define CI_RCPT		3
67 #define CI_DATA		4
68 #define CI_EOM		5
69 #define CI_EOH		6
70 #define CI_LAST		CI_EOH
71 #if CI_LAST < CI_DATA
72 ERROR: do not compile with CI_LAST < CI_DATA
73 #endif
74 #if CI_LAST < CI_EOM
75 ERROR: do not compile with CI_LAST < CI_EOM
76 #endif
77 #if CI_LAST < CI_EOH
78 ERROR: do not compile with CI_LAST < CI_EOH
79 #endif
80 #if CI_LAST < CI_ENVRCPT
81 ERROR: do not compile with CI_LAST < CI_ENVRCPT
82 #endif
83 #if CI_LAST < CI_ENVFROM
84 ERROR: do not compile with CI_LAST < CI_ENVFROM
85 #endif
86 #if CI_LAST < CI_HELO
87 ERROR: do not compile with CI_LAST < CI_HELO
88 #endif
89 #if CI_LAST < CI_CONNECT
90 ERROR: do not compile with CI_LAST < CI_CONNECT
91 #endif
92 #if CI_LAST >= MAX_MACROS_ENTRIES
93 ERROR: do not compile with CI_LAST >= MAX_MACROS_ENTRIES
94 #endif
95 
96 /* function prototypes */
97 static int	st_abortfct __P((genarg *));
98 static int	st_macros __P((genarg *));
99 static int	st_optionneg __P((genarg *));
100 static int	st_bodychunk __P((genarg *));
101 static int	st_connectinfo __P((genarg *));
102 static int	st_bodyend __P((genarg *));
103 static int	st_helo __P((genarg *));
104 static int	st_header __P((genarg *));
105 static int	st_sender __P((genarg *));
106 static int	st_rcpt __P((genarg *));
107 static int	st_unknown __P((genarg *));
108 static int	st_data __P((genarg *));
109 static int	st_eoh __P((genarg *));
110 static int	st_quit __P((genarg *));
111 static int	sendreply __P((sfsistat, socket_t, struct timeval *, SMFICTX_PTR));
112 static void	fix_stm __P((SMFICTX_PTR));
113 static bool	trans_ok __P((int, int));
114 static char	**dec_argv __P((char *, size_t));
115 static int	dec_arg2 __P((char *, size_t, char **, char **));
116 
117 #if _FFR_WORKERS_POOL
118 static bool     mi_rd_socket_ready __P((int));
119 #endif /* _FFR_WORKERS_POOL */
120 
121 /* states */
122 #define ST_NONE	(-1)
123 #define ST_INIT	0	/* initial state */
124 #define ST_OPTS	1	/* option negotiation */
125 #define ST_CONN	2	/* connection info */
126 #define ST_HELO	3	/* helo */
127 #define ST_MAIL	4	/* mail from */
128 #define ST_RCPT	5	/* rcpt to */
129 #define ST_DATA	6	/* data */
130 #define ST_HDRS	7	/* headers */
131 #define ST_EOHS	8	/* end of headers */
132 #define ST_BODY	9	/* body */
133 #define ST_ENDM	10	/* end of message */
134 #define ST_QUIT	11	/* quit */
135 #define ST_ABRT	12	/* abort */
136 #define ST_UNKN 13	/* unknown SMTP command */
137 #define ST_Q_NC	14	/* quit, new connection follows */
138 #define ST_LAST	ST_Q_NC	/* last valid state */
139 #define ST_SKIP	16	/* not a state but required for the state table */
140 
141 /* in a mail transaction? must be before eom according to spec. */
142 #define ST_IN_MAIL(st)	((st) >= ST_MAIL && (st) < ST_ENDM)
143 
144 /*
145 **  set of next states
146 **  each state (ST_*) corresponds to bit in an int value (1 << state)
147 **  each state has a set of allowed transitions ('or' of bits of states)
148 **  so a state transition is valid if the mask of the next state
149 **  is set in the NX_* value
150 **  this function is coded in trans_ok(), see below.
151 */
152 
153 #define MI_MASK(x)	(0x0001 << (x))	/* generate a bit "mask" for a state */
154 #define NX_INIT	(MI_MASK(ST_OPTS))
155 #define NX_OPTS	(MI_MASK(ST_CONN) | MI_MASK(ST_UNKN))
156 #define NX_CONN	(MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN))
157 #define NX_HELO	(MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN))
158 #define NX_MAIL	(MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | MI_MASK(ST_UNKN))
159 #define NX_RCPT	(MI_MASK(ST_HDRS) | MI_MASK(ST_EOHS) | MI_MASK(ST_DATA) | \
160 		 MI_MASK(ST_BODY) | MI_MASK(ST_ENDM) | \
161 		 MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | MI_MASK(ST_UNKN))
162 #define NX_DATA	(MI_MASK(ST_EOHS) | MI_MASK(ST_HDRS) | MI_MASK(ST_ABRT))
163 #define NX_HDRS	(MI_MASK(ST_EOHS) | MI_MASK(ST_HDRS) | MI_MASK(ST_ABRT))
164 #define NX_EOHS	(MI_MASK(ST_BODY) | MI_MASK(ST_ENDM) | MI_MASK(ST_ABRT))
165 #define NX_BODY	(MI_MASK(ST_ENDM) | MI_MASK(ST_BODY) | MI_MASK(ST_ABRT))
166 #define NX_ENDM	(MI_MASK(ST_QUIT) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN) | \
167 		MI_MASK(ST_Q_NC))
168 #define NX_QUIT	0
169 #define NX_ABRT	0
170 #define NX_UNKN (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | \
171 		 MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | \
172 		 MI_MASK(ST_DATA) | \
173 		 MI_MASK(ST_BODY) | MI_MASK(ST_UNKN) | \
174 		 MI_MASK(ST_ABRT) | MI_MASK(ST_QUIT) | MI_MASK(ST_Q_NC))
175 #define NX_Q_NC	(MI_MASK(ST_CONN) | MI_MASK(ST_UNKN))
176 #define NX_SKIP MI_MASK(ST_SKIP)
177 
178 static int next_states[] =
179 {
180 	  NX_INIT
181 	, NX_OPTS
182 	, NX_CONN
183 	, NX_HELO
184 	, NX_MAIL
185 	, NX_RCPT
186 	, NX_DATA
187 	, NX_HDRS
188 	, NX_EOHS
189 	, NX_BODY
190 	, NX_ENDM
191 	, NX_QUIT
192 	, NX_ABRT
193 	, NX_UNKN
194 	, NX_Q_NC
195 };
196 
197 #define SIZE_NEXT_STATES	(sizeof(next_states) / sizeof(next_states[0]))
198 
199 /* commands received by milter */
200 static cmdfct cmds[] =
201 {
202   {SMFIC_ABORT,	CM_ARG0, ST_ABRT,  CT_CONT,	CI_NONE, st_abortfct	}
203 , {SMFIC_MACRO,	CM_ARGV, ST_NONE,  CT_KEEP,	CI_NONE, st_macros	}
204 , {SMFIC_BODY,	CM_ARG1, ST_BODY,  CT_CONT,	CI_NONE, st_bodychunk	}
205 , {SMFIC_CONNECT, CM_ARG2, ST_CONN,  CT_CONT,	CI_CONN, st_connectinfo	}
206 , {SMFIC_BODYEOB, CM_ARG1, ST_ENDM,  CT_CONT,	CI_EOM,  st_bodyend	}
207 , {SMFIC_HELO,	CM_ARG1, ST_HELO,  CT_CONT,	CI_HELO, st_helo	}
208 , {SMFIC_HEADER, CM_ARG2, ST_HDRS,  CT_CONT,	CI_NONE, st_header	}
209 , {SMFIC_MAIL,	CM_ARGV, ST_MAIL,  CT_CONT,	CI_MAIL, st_sender	}
210 , {SMFIC_OPTNEG, CM_ARGO, ST_OPTS,  CT_CONT,	CI_NONE, st_optionneg	}
211 , {SMFIC_EOH,	CM_ARG0, ST_EOHS,  CT_CONT,	CI_EOH,  st_eoh		}
212 , {SMFIC_QUIT,	CM_ARG0, ST_QUIT,  CT_END,	CI_NONE, st_quit	}
213 , {SMFIC_DATA,	CM_ARG0, ST_DATA,  CT_CONT,	CI_DATA, st_data	}
214 , {SMFIC_RCPT,	CM_ARGV, ST_RCPT,  CT_IGNO,	CI_RCPT, st_rcpt	}
215 , {SMFIC_UNKNOWN, CM_ARG1, ST_UNKN,  CT_IGNO,	CI_NONE, st_unknown	}
216 , {SMFIC_QUIT_NC, CM_ARG0, ST_Q_NC,  CT_CONT,	CI_NONE, st_quit	}
217 };
218 
219 /*
220 **  Additional (internal) reply codes;
221 **  must be coordinated wit libmilter/mfapi.h
222 */
223 
224 #define _SMFIS_KEEP	20
225 #define _SMFIS_ABORT	21
226 #define _SMFIS_OPTIONS	22
227 #define _SMFIS_NOREPLY	SMFIS_NOREPLY
228 #define _SMFIS_FAIL	(-1)
229 #define _SMFIS_NONE	(-2)
230 
231 /*
232 **  MI_ENGINE -- receive commands and process them
233 **
234 **	Parameters:
235 **		ctx -- context structure
236 **
237 **	Returns:
238 **		MI_FAILURE/MI_SUCCESS
239 */
240 
241 int
242 mi_engine(ctx)
243 	SMFICTX_PTR ctx;
244 {
245 	size_t len;
246 	int i;
247 	socket_t sd;
248 	int ret = MI_SUCCESS;
249 	int ncmds = sizeof(cmds) / sizeof(cmdfct);
250 	int curstate = ST_INIT;
251 	int newstate;
252 	bool call_abort;
253 	sfsistat r;
254 	char cmd;
255 	char *buf = NULL;
256 	genarg arg;
257 	struct timeval timeout;
258 	int (*f) __P((genarg *));
259 	sfsistat (*fi_abort) __P((SMFICTX *));
260 	sfsistat (*fi_close) __P((SMFICTX *));
261 
262 	arg.a_ctx = ctx;
263 	sd = ctx->ctx_sd;
264 	fi_abort = ctx->ctx_smfi->xxfi_abort;
265 #if _FFR_WORKERS_POOL
266 	curstate = ctx->ctx_state;
267 	if (curstate == ST_INIT)
268 	{
269 		mi_clr_macros(ctx, 0);
270 		fix_stm(ctx);
271 	}
272 #else   /* _FFR_WORKERS_POOL */
273 	mi_clr_macros(ctx, 0);
274 	fix_stm(ctx);
275 #endif  /* _FFR_WORKERS_POOL */
276 	r = _SMFIS_NONE;
277 	do
278 	{
279 		/* call abort only if in a mail transaction */
280 		call_abort = ST_IN_MAIL(curstate);
281 		timeout.tv_sec = ctx->ctx_timeout;
282 		timeout.tv_usec = 0;
283 		if (mi_stop() == MILTER_ABRT)
284 		{
285 			if (ctx->ctx_dbg > 3)
286 				sm_dprintf("[%ld] milter_abort\n",
287 					(long) ctx->ctx_id);
288 			ret = MI_FAILURE;
289 			break;
290 		}
291 
292 		/*
293 		**  Notice: buf is allocated by mi_rd_cmd() and it will
294 		**  usually be free()d after it has been used in f().
295 		**  However, if the function returns _SMFIS_KEEP then buf
296 		**  contains macros and will not be free()d.
297 		**  Hence r must be set to _SMFIS_NONE if a new buf is
298 		**  allocated to avoid problem with housekeeping, esp.
299 		**  if the code "break"s out of the loop.
300 		*/
301 
302 #if _FFR_WORKERS_POOL
303 		/* Is the socket ready to be read ??? */
304 		if (!mi_rd_socket_ready(sd))
305 		{
306 			ret = MI_CONTINUE;
307 			break;
308 		}
309 #endif  /* _FFR_WORKERS_POOL */
310 
311 		r = _SMFIS_NONE;
312 		if ((buf = mi_rd_cmd(sd, &timeout, &cmd, &len,
313 				     ctx->ctx_smfi->xxfi_name)) == NULL &&
314 		    cmd < SMFIC_VALIDCMD)
315 		{
316 			if (ctx->ctx_dbg > 5)
317 				sm_dprintf("[%ld] mi_engine: mi_rd_cmd error (%x)\n",
318 					(long) ctx->ctx_id, (int) cmd);
319 
320 			/*
321 			**  eof is currently treated as failure ->
322 			**  abort() instead of close(), otherwise use:
323 			**  if (cmd != SMFIC_EOF)
324 			*/
325 
326 			ret = MI_FAILURE;
327 			break;
328 		}
329 		if (ctx->ctx_dbg > 4)
330 			sm_dprintf("[%ld] got cmd '%c' len %d\n",
331 				(long) ctx->ctx_id, cmd, (int) len);
332 		for (i = 0; i < ncmds; i++)
333 		{
334 			if (cmd == cmds[i].cm_cmd)
335 				break;
336 		}
337 		if (i >= ncmds)
338 		{
339 			/* unknown command */
340 			if (ctx->ctx_dbg > 1)
341 				sm_dprintf("[%ld] cmd '%c' unknown\n",
342 					(long) ctx->ctx_id, cmd);
343 			ret = MI_FAILURE;
344 			break;
345 		}
346 		if ((f = cmds[i].cm_fct) == NULL)
347 		{
348 			/* stop for now */
349 			if (ctx->ctx_dbg > 1)
350 				sm_dprintf("[%ld] cmd '%c' not impl\n",
351 					(long) ctx->ctx_id, cmd);
352 			ret = MI_FAILURE;
353 			break;
354 		}
355 
356 		/* is new state ok? */
357 		newstate = cmds[i].cm_next;
358 		if (ctx->ctx_dbg > 5)
359 			sm_dprintf("[%ld] cur %x new %x nextmask %x\n",
360 				(long) ctx->ctx_id,
361 				curstate, newstate, next_states[curstate]);
362 
363 		if (newstate != ST_NONE && !trans_ok(curstate, newstate))
364 		{
365 			if (ctx->ctx_dbg > 1)
366 				sm_dprintf("[%ld] abort: cur %d (%x) new %d (%x) next %x\n",
367 					(long) ctx->ctx_id,
368 					curstate, MI_MASK(curstate),
369 					newstate, MI_MASK(newstate),
370 					next_states[curstate]);
371 
372 			/* call abort only if in a mail transaction */
373 			if (fi_abort != NULL && call_abort)
374 				(void) (*fi_abort)(ctx);
375 
376 			/*
377 			**  try to reach the new state from HELO
378 			**  if it can't be reached, ignore the command.
379 			*/
380 
381 			curstate = ST_HELO;
382 			if (!trans_ok(curstate, newstate))
383 			{
384 				if (buf != NULL)
385 				{
386 					free(buf);
387 					buf = NULL;
388 				}
389 				continue;
390 			}
391 		}
392 		arg.a_len = len;
393 		arg.a_buf = buf;
394 		if (newstate != ST_NONE)
395 		{
396 			curstate = newstate;
397 			ctx->ctx_state = curstate;
398 		}
399 		arg.a_idx = cmds[i].cm_macros;
400 		call_abort = ST_IN_MAIL(curstate);
401 
402 		/* call function to deal with command */
403 		MI_MONITOR_BEGIN(ctx, cmd);
404 		r = (*f)(&arg);
405 		MI_MONITOR_END(ctx, cmd);
406 		if (r != _SMFIS_KEEP && buf != NULL)
407 		{
408 			free(buf);
409 			buf = NULL;
410 		}
411 		if (sendreply(r, sd, &timeout, ctx) != MI_SUCCESS)
412 		{
413 			ret = MI_FAILURE;
414 			break;
415 		}
416 
417 		if (r == SMFIS_ACCEPT)
418 		{
419 			/* accept mail, no further actions taken */
420 			curstate = ST_HELO;
421 		}
422 		else if (r == SMFIS_REJECT || r == SMFIS_DISCARD ||
423 			 r ==  SMFIS_TEMPFAIL)
424 		{
425 			/*
426 			**  further actions depend on current state
427 			**  if the IGNO bit is set: "ignore" the error,
428 			**  i.e., stay in the current state
429 			*/
430 			if (!bitset(CT_IGNO, cmds[i].cm_todo))
431 				curstate = ST_HELO;
432 		}
433 		else if (r == _SMFIS_ABORT)
434 		{
435 			if (ctx->ctx_dbg > 5)
436 				sm_dprintf("[%ld] function returned abort\n",
437 					(long) ctx->ctx_id);
438 			ret = MI_FAILURE;
439 			break;
440 		}
441 	} while (!bitset(CT_END, cmds[i].cm_todo));
442 
443 	ctx->ctx_state = curstate;
444 
445 	if (ret == MI_FAILURE)
446 	{
447 		/* call abort only if in a mail transaction */
448 		if (fi_abort != NULL && call_abort)
449 			(void) (*fi_abort)(ctx);
450 	}
451 
452 	/* has close been called? */
453 	if (ctx->ctx_state != ST_QUIT
454 #if _FFR_WORKERS_POOL
455 	   && ret != MI_CONTINUE
456 #endif /* _FFR_WORKERS_POOL */
457 	   )
458 	{
459 		if ((fi_close = ctx->ctx_smfi->xxfi_close) != NULL)
460 			(void) (*fi_close)(ctx);
461 	}
462 	if (r != _SMFIS_KEEP && buf != NULL)
463 		free(buf);
464 #if !_FFR_WORKERS_POOL
465 	mi_clr_macros(ctx, 0);
466 #endif /* _FFR_WORKERS_POOL */
467 	return ret;
468 }
469 
470 static size_t milter_addsymlist __P((SMFICTX_PTR, char *, char **));
471 
472 static size_t
473 milter_addsymlist(ctx, buf, newbuf)
474 	SMFICTX_PTR ctx;
475 	char *buf;
476 	char **newbuf;
477 {
478 	size_t len;
479 	int i;
480 	mi_int32 v;
481 	char *buffer;
482 
483 	SM_ASSERT(ctx != NULL);
484 	SM_ASSERT(buf != NULL);
485 	SM_ASSERT(newbuf != NULL);
486 	len = 0;
487 	for (i = 0; i < MAX_MACROS_ENTRIES; i++)
488 	{
489 		if (ctx->ctx_mac_list[i] != NULL)
490 		{
491 			len += strlen(ctx->ctx_mac_list[i]) + 1 +
492 				MILTER_LEN_BYTES;
493 		}
494 	}
495 	if (len > 0)
496 	{
497 		size_t offset;
498 
499 		SM_ASSERT(len + MILTER_OPTLEN > len);
500 		len += MILTER_OPTLEN;
501 		buffer = malloc(len);
502 		if (buffer != NULL)
503 		{
504 			(void) memcpy(buffer, buf, MILTER_OPTLEN);
505 			offset = MILTER_OPTLEN;
506 			for (i = 0; i < MAX_MACROS_ENTRIES; i++)
507 			{
508 				size_t l;
509 
510 				if (ctx->ctx_mac_list[i] == NULL)
511 					continue;
512 
513 				SM_ASSERT(offset + MILTER_LEN_BYTES < len);
514 				v = htonl(i);
515 				(void) memcpy(buffer + offset, (void *) &v,
516 						MILTER_LEN_BYTES);
517 				offset += MILTER_LEN_BYTES;
518 				l = strlen(ctx->ctx_mac_list[i]) + 1;
519 				SM_ASSERT(offset + l <= len);
520 				(void) memcpy(buffer + offset,
521 						ctx->ctx_mac_list[i], l);
522 				offset += l;
523 			}
524 		}
525 		else
526 		{
527 			/* oops ... */
528 		}
529 	}
530 	else
531 	{
532 		len = MILTER_OPTLEN;
533 		buffer = buf;
534 	}
535 	*newbuf = buffer;
536 	return len;
537 }
538 
539 /*
540 **  GET_NR_BIT -- get "no reply" bit matching state
541 **
542 **	Parameters:
543 **		state -- current protocol stage
544 **
545 **	Returns:
546 **		0: no matching bit
547 **		>0: the matching "no reply" bit
548 */
549 
550 static unsigned long get_nr_bit __P((int));
551 
552 static unsigned long
553 get_nr_bit(state)
554 	int state;
555 {
556 	unsigned long bit;
557 
558 	switch (state)
559 	{
560 	  case ST_CONN:
561 		bit = SMFIP_NR_CONN;
562 		break;
563 	  case ST_HELO:
564 		bit = SMFIP_NR_HELO;
565 		break;
566 	  case ST_MAIL:
567 		bit = SMFIP_NR_MAIL;
568 		break;
569 	  case ST_RCPT:
570 		bit = SMFIP_NR_RCPT;
571 		break;
572 	  case ST_DATA:
573 		bit = SMFIP_NR_DATA;
574 		break;
575 	  case ST_UNKN:
576 		bit = SMFIP_NR_UNKN;
577 		break;
578 	  case ST_HDRS:
579 		bit = SMFIP_NR_HDR;
580 		break;
581 	  case ST_EOHS:
582 		bit = SMFIP_NR_EOH;
583 		break;
584 	  case ST_BODY:
585 		bit = SMFIP_NR_BODY;
586 		break;
587 	  default:
588 		bit = 0;
589 		break;
590 	}
591 	return bit;
592 }
593 
594 /*
595 **  SENDREPLY -- send a reply to the MTA
596 **
597 **	Parameters:
598 **		r -- reply code
599 **		sd -- socket descriptor
600 **		timeout_ptr -- (ptr to) timeout to use for sending
601 **		ctx -- context structure
602 **
603 **	Returns:
604 **		MI_SUCCESS/MI_FAILURE
605 */
606 
607 static int
608 sendreply(r, sd, timeout_ptr, ctx)
609 	sfsistat r;
610 	socket_t sd;
611 	struct timeval *timeout_ptr;
612 	SMFICTX_PTR ctx;
613 {
614 	int ret;
615 	unsigned long bit;
616 
617 	ret = MI_SUCCESS;
618 
619 	bit = get_nr_bit(ctx->ctx_state);
620 	if (bit != 0 && (ctx->ctx_pflags & bit) != 0 && r != SMFIS_NOREPLY)
621 	{
622 		if (r >= SMFIS_CONTINUE && r < _SMFIS_KEEP)
623 		{
624 			/* milter said it wouldn't reply, but it lied... */
625 			smi_log(SMI_LOG_ERR,
626 				"%s: milter claimed not to reply in state %d but did anyway %d\n",
627 				ctx->ctx_smfi->xxfi_name,
628 				ctx->ctx_state, r);
629 
630 		}
631 
632 		/*
633 		**  Force specified behavior, otherwise libmilter
634 		**  and MTA will fail to communicate properly.
635 		*/
636 
637 		switch (r)
638 		{
639 		  case SMFIS_CONTINUE:
640 		  case SMFIS_TEMPFAIL:
641 		  case SMFIS_REJECT:
642 		  case SMFIS_DISCARD:
643 		  case SMFIS_ACCEPT:
644 		  case SMFIS_SKIP:
645 		  case _SMFIS_OPTIONS:
646 			r = SMFIS_NOREPLY;
647 			break;
648 		}
649 	}
650 
651 	switch (r)
652 	{
653 	  case SMFIS_CONTINUE:
654 		ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_CONTINUE, NULL, 0);
655 		break;
656 	  case SMFIS_TEMPFAIL:
657 	  case SMFIS_REJECT:
658 		if (ctx->ctx_reply != NULL &&
659 		    ((r == SMFIS_TEMPFAIL && *ctx->ctx_reply == '4') ||
660 		     (r == SMFIS_REJECT && *ctx->ctx_reply == '5')))
661 		{
662 			ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_REPLYCODE,
663 					ctx->ctx_reply,
664 					strlen(ctx->ctx_reply) + 1);
665 			free(ctx->ctx_reply);
666 			ctx->ctx_reply = NULL;
667 		}
668 		else
669 		{
670 			ret = mi_wr_cmd(sd, timeout_ptr, r == SMFIS_REJECT ?
671 					SMFIR_REJECT : SMFIR_TEMPFAIL, NULL, 0);
672 		}
673 		break;
674 	  case SMFIS_DISCARD:
675 		ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_DISCARD, NULL, 0);
676 		break;
677 	  case SMFIS_ACCEPT:
678 		ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_ACCEPT, NULL, 0);
679 		break;
680 	  case SMFIS_SKIP:
681 		ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_SKIP, NULL, 0);
682 		break;
683 	  case _SMFIS_OPTIONS:
684 		{
685 			mi_int32 v;
686 			size_t len;
687 			char *buffer;
688 			char buf[MILTER_OPTLEN];
689 
690 			v = htonl(ctx->ctx_prot_vers2mta);
691 			(void) memcpy(&(buf[0]), (void *) &v,
692 				      MILTER_LEN_BYTES);
693 			v = htonl(ctx->ctx_aflags);
694 			(void) memcpy(&(buf[MILTER_LEN_BYTES]), (void *) &v,
695 				      MILTER_LEN_BYTES);
696 			v = htonl(ctx->ctx_pflags2mta);
697 			(void) memcpy(&(buf[MILTER_LEN_BYTES * 2]),
698 				      (void *) &v, MILTER_LEN_BYTES);
699 			len = milter_addsymlist(ctx, buf, &buffer);
700 			if (buffer != NULL)
701 				ret = mi_wr_cmd(sd, timeout_ptr, SMFIC_OPTNEG,
702 						buffer, len);
703 			else
704 				ret = MI_FAILURE;
705 		}
706 		break;
707 	  case SMFIS_NOREPLY:
708 		if (bit != 0 &&
709 		    (ctx->ctx_pflags & bit) != 0 &&
710 		    (ctx->ctx_mta_pflags & bit) == 0)
711 		{
712 			/*
713 			**  milter doesn't want to send a reply,
714 			**  but the MTA doesn't have that feature: fake it.
715 			*/
716 
717 			ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_CONTINUE, NULL,
718 					0);
719 		}
720 		break;
721 	  default:	/* don't send a reply */
722 		break;
723 	}
724 	return ret;
725 }
726 
727 /*
728 **  CLR_MACROS -- clear set of macros starting from a given index
729 **
730 **	Parameters:
731 **		ctx -- context structure
732 **		m -- index from which to clear all macros
733 **
734 **	Returns:
735 **		None.
736 */
737 
738 void
739 mi_clr_macros(ctx, m)
740 	SMFICTX_PTR ctx;
741 	int m;
742 {
743 	int i;
744 
745 	for (i = m; i < MAX_MACROS_ENTRIES; i++)
746 	{
747 		if (ctx->ctx_mac_ptr[i] != NULL)
748 		{
749 			free(ctx->ctx_mac_ptr[i]);
750 			ctx->ctx_mac_ptr[i] = NULL;
751 		}
752 		if (ctx->ctx_mac_buf[i] != NULL)
753 		{
754 			free(ctx->ctx_mac_buf[i]);
755 			ctx->ctx_mac_buf[i] = NULL;
756 		}
757 	}
758 }
759 
760 /*
761 **  ST_OPTIONNEG -- negotiate options
762 **
763 **	Parameters:
764 **		g -- generic argument structure
765 **
766 **	Returns:
767 **		abort/send options/continue
768 */
769 
770 static int
771 st_optionneg(g)
772 	genarg *g;
773 {
774 	mi_int32 i, v, fake_pflags;
775 	SMFICTX_PTR ctx;
776 	int (*fi_negotiate) __P((SMFICTX *,
777 					unsigned long, unsigned long,
778 					unsigned long, unsigned long,
779 					unsigned long *, unsigned long *,
780 					unsigned long *, unsigned long *));
781 
782 	if (g == NULL || g->a_ctx->ctx_smfi == NULL)
783 		return SMFIS_CONTINUE;
784 	ctx = g->a_ctx;
785 	mi_clr_macros(ctx, g->a_idx + 1);
786 	ctx->ctx_prot_vers = SMFI_PROT_VERSION;
787 
788 	/* check for minimum length */
789 	if (g->a_len < MILTER_OPTLEN)
790 	{
791 		smi_log(SMI_LOG_ERR,
792 			"%s: st_optionneg[%ld]: len too short %d < %d",
793 			ctx->ctx_smfi->xxfi_name,
794 			(long) ctx->ctx_id, (int) g->a_len,
795 			MILTER_OPTLEN);
796 		return _SMFIS_ABORT;
797 	}
798 
799 	/* protocol version */
800 	(void) memcpy((void *) &i, (void *) &(g->a_buf[0]), MILTER_LEN_BYTES);
801 	v = ntohl(i);
802 
803 #define SMFI_PROT_VERSION_MIN	2
804 
805 	/* check for minimum version */
806 	if (v < SMFI_PROT_VERSION_MIN)
807 	{
808 		smi_log(SMI_LOG_ERR,
809 			"%s: st_optionneg[%ld]: protocol version too old %d < %d",
810 			ctx->ctx_smfi->xxfi_name,
811 			(long) ctx->ctx_id, v, SMFI_PROT_VERSION_MIN);
812 		return _SMFIS_ABORT;
813 	}
814 	ctx->ctx_mta_prot_vers = v;
815 	if (ctx->ctx_prot_vers < ctx->ctx_mta_prot_vers)
816 		ctx->ctx_prot_vers2mta = ctx->ctx_prot_vers;
817 	else
818 		ctx->ctx_prot_vers2mta = ctx->ctx_mta_prot_vers;
819 
820 	(void) memcpy((void *) &i, (void *) &(g->a_buf[MILTER_LEN_BYTES]),
821 		      MILTER_LEN_BYTES);
822 	v = ntohl(i);
823 
824 	/* no flags? set to default value for V1 actions */
825 	if (v == 0)
826 		v = SMFI_V1_ACTS;
827 	ctx->ctx_mta_aflags = v;	/* MTA action flags */
828 
829 	(void) memcpy((void *) &i, (void *) &(g->a_buf[MILTER_LEN_BYTES * 2]),
830 		      MILTER_LEN_BYTES);
831 	v = ntohl(i);
832 
833 	/* no flags? set to default value for V1 protocol */
834 	if (v == 0)
835 		v = SMFI_V1_PROT;
836 	ctx->ctx_mta_pflags = v;	/* MTA protocol flags */
837 
838 	/*
839 	**  Copy flags from milter struct into libmilter context;
840 	**  this variable will be used later on to check whether
841 	**  the MTA "actions" can fulfill the milter requirements,
842 	**  but it may be overwritten by the negotiate callback.
843 	*/
844 
845 	ctx->ctx_aflags = ctx->ctx_smfi->xxfi_flags;
846 	fake_pflags = SMFIP_NR_CONN
847 			|SMFIP_NR_HELO
848 			|SMFIP_NR_MAIL
849 			|SMFIP_NR_RCPT
850 			|SMFIP_NR_DATA
851 			|SMFIP_NR_UNKN
852 			|SMFIP_NR_HDR
853 			|SMFIP_NR_EOH
854 			|SMFIP_NR_BODY
855 			;
856 
857 	if (g->a_ctx->ctx_smfi != NULL &&
858 	    g->a_ctx->ctx_smfi->xxfi_version > 4 &&
859 	    (fi_negotiate = g->a_ctx->ctx_smfi->xxfi_negotiate) != NULL)
860 	{
861 		int r;
862 		unsigned long m_aflags, m_pflags, m_f2, m_f3;
863 
864 		/*
865 		**  let milter decide whether the features offered by the
866 		**  MTA are "good enough".
867 		**  Notes:
868 		**  - libmilter can "fake" some features (e.g., SMFIP_NR_HDR)
869 		**  - m_f2, m_f3 are for future extensions
870 		*/
871 
872 		m_f2 = m_f3 = 0;
873 		m_aflags = ctx->ctx_mta_aflags;
874 		m_pflags = ctx->ctx_pflags;
875 		if ((SMFIP_SKIP & ctx->ctx_mta_pflags) != 0)
876 			m_pflags |= SMFIP_SKIP;
877 		r = fi_negotiate(g->a_ctx,
878 				ctx->ctx_mta_aflags,
879 				ctx->ctx_mta_pflags|fake_pflags,
880 				0, 0,
881 				&m_aflags, &m_pflags, &m_f2, &m_f3);
882 
883 		/*
884 		**  Types of protocol flags (pflags):
885 		**  1. do NOT send protocol step X
886 		**  2. MTA can do/understand something extra (SKIP,
887 		**	send unknown RCPTs)
888 		**  3. MTA can deal with "no reply" for various protocol steps
889 		**  Note: this mean that it isn't possible to simply set all
890 		**	flags to get "everything":
891 		**	setting a flag of type 1 turns off a step
892 		**		(it should be the other way around:
893 		**		a flag means a protocol step can be sent)
894 		**	setting a flag of type 3 requires that milter
895 		**	never sends a reply for the corresponding step.
896 		**  Summary: the "negation" of protocol flags is causing
897 		**	problems, but at least for type 3 there is no simple
898 		**	solution.
899 		**
900 		**  What should "all options" mean?
901 		**  send all protocol steps _except_ those for which there is
902 		**	no callback (currently registered in ctx_pflags)
903 		**  expect SKIP as return code?		Yes
904 		**  send unknown RCPTs?			No,
905 		**				must be explicitly requested?
906 		**  "no reply" for some protocol steps?	No,
907 		**				must be explicitly requested.
908 		*/
909 
910 		if (SMFIS_ALL_OPTS == r)
911 		{
912 			ctx->ctx_aflags = ctx->ctx_mta_aflags;
913 			ctx->ctx_pflags2mta = ctx->ctx_pflags;
914 			if ((SMFIP_SKIP & ctx->ctx_mta_pflags) != 0)
915 				ctx->ctx_pflags2mta |= SMFIP_SKIP;
916 		}
917 		else if (r != SMFIS_CONTINUE)
918 		{
919 			smi_log(SMI_LOG_ERR,
920 				"%s: st_optionneg[%ld]: xxfi_negotiate returned %d (protocol options=0x%lx, actions=0x%lx)",
921 				ctx->ctx_smfi->xxfi_name,
922 				(long) ctx->ctx_id, r, ctx->ctx_mta_pflags,
923 				ctx->ctx_mta_aflags);
924 			return _SMFIS_ABORT;
925 		}
926 		else
927 		{
928 			ctx->ctx_aflags = m_aflags;
929 			ctx->ctx_pflags = m_pflags;
930 			ctx->ctx_pflags2mta = m_pflags;
931 		}
932 
933 		/* check whether some flags need to be "faked" */
934 		i = ctx->ctx_pflags2mta;
935 		if ((ctx->ctx_mta_pflags & i) != i)
936 		{
937 			unsigned int idx;
938 			unsigned long b;
939 
940 			/*
941 			**  If some behavior can be faked (set in fake_pflags),
942 			**  but the MTA doesn't support it, then unset
943 			**  that flag in the value that is sent to the MTA.
944 			*/
945 
946 			for (idx = 0; idx < 32; idx++)
947 			{
948 				b = 1 << idx;
949 				if ((ctx->ctx_mta_pflags & b) != b &&
950 				    (fake_pflags & b) == b)
951 					ctx->ctx_pflags2mta &= ~b;
952 			}
953 		}
954 	}
955 	else
956 	{
957 		/*
958 		**  Set the protocol flags based on the values determined
959 		**  in mi_listener() which checked the defined callbacks.
960 		*/
961 
962 		ctx->ctx_pflags2mta = ctx->ctx_pflags;
963 	}
964 
965 	/* check whether actions and protocol requirements can be satisfied */
966 	i = ctx->ctx_aflags;
967 	if ((i & ctx->ctx_mta_aflags) != i)
968 	{
969 		smi_log(SMI_LOG_ERR,
970 			"%s: st_optionneg[%ld]: 0x%lx does not fulfill action requirements 0x%x",
971 			ctx->ctx_smfi->xxfi_name,
972 			(long) ctx->ctx_id, ctx->ctx_mta_aflags, i);
973 		return _SMFIS_ABORT;
974 	}
975 
976 	i = ctx->ctx_pflags2mta;
977 	if ((ctx->ctx_mta_pflags & i) != i)
978 	{
979 		/*
980 		**  Older MTAs do not support some protocol steps.
981 		**  As this protocol is a bit "wierd" (it asks for steps
982 		**  NOT to be taken/sent) we have to check whether we
983 		**  should turn off those "negative" requests.
984 		**  Currently these are only SMFIP_NODATA and SMFIP_NOUNKNOWN.
985 		*/
986 
987 		if (bitset(SMFIP_NODATA, ctx->ctx_pflags2mta) &&
988 		    !bitset(SMFIP_NODATA, ctx->ctx_mta_pflags))
989 			ctx->ctx_pflags2mta &= ~SMFIP_NODATA;
990 		if (bitset(SMFIP_NOUNKNOWN, ctx->ctx_pflags2mta) &&
991 		    !bitset(SMFIP_NOUNKNOWN, ctx->ctx_mta_pflags))
992 			ctx->ctx_pflags2mta &= ~SMFIP_NOUNKNOWN;
993 		i = ctx->ctx_pflags2mta;
994 	}
995 
996 	if ((ctx->ctx_mta_pflags & i) != i)
997 	{
998 		smi_log(SMI_LOG_ERR,
999 			"%s: st_optionneg[%ld]: 0x%lx does not fulfill protocol requirements 0x%x",
1000 			ctx->ctx_smfi->xxfi_name,
1001 			(long) ctx->ctx_id, ctx->ctx_mta_pflags, i);
1002 		return _SMFIS_ABORT;
1003 	}
1004 	fix_stm(ctx);
1005 
1006 	if (ctx->ctx_dbg > 3)
1007 		sm_dprintf("[%ld] milter_negotiate:"
1008 			" mta_actions=0x%lx, mta_flags=0x%lx"
1009 			" actions=0x%lx, flags=0x%lx\n"
1010 			, (long) ctx->ctx_id
1011 			, ctx->ctx_mta_aflags, ctx->ctx_mta_pflags
1012 			, ctx->ctx_aflags, ctx->ctx_pflags);
1013 
1014 	return _SMFIS_OPTIONS;
1015 }
1016 
1017 /*
1018 **  ST_CONNECTINFO -- receive connection information
1019 **
1020 **	Parameters:
1021 **		g -- generic argument structure
1022 **
1023 **	Returns:
1024 **		continue or filter-specified value
1025 */
1026 
1027 static int
1028 st_connectinfo(g)
1029 	genarg *g;
1030 {
1031 	size_t l;
1032 	size_t i;
1033 	char *s, family;
1034 	unsigned short port = 0;
1035 	_SOCK_ADDR sockaddr;
1036 	sfsistat (*fi_connect) __P((SMFICTX *, char *, _SOCK_ADDR *));
1037 
1038 	if (g == NULL)
1039 		return _SMFIS_ABORT;
1040 	mi_clr_macros(g->a_ctx, g->a_idx + 1);
1041 	if (g->a_ctx->ctx_smfi == NULL ||
1042 	    (fi_connect = g->a_ctx->ctx_smfi->xxfi_connect) == NULL)
1043 		return SMFIS_CONTINUE;
1044 
1045 	s = g->a_buf;
1046 	i = 0;
1047 	l = g->a_len;
1048 	while (s[i] != '\0' && i <= l)
1049 		++i;
1050 	if (i + 1 >= l)
1051 		return _SMFIS_ABORT;
1052 
1053 	/* Move past trailing \0 in host string */
1054 	i++;
1055 	family = s[i++];
1056 	(void) memset(&sockaddr, '\0', sizeof sockaddr);
1057 	if (family != SMFIA_UNKNOWN)
1058 	{
1059 		if (i + sizeof port >= l)
1060 		{
1061 			smi_log(SMI_LOG_ERR,
1062 				"%s: connect[%ld]: wrong len %d >= %d",
1063 				g->a_ctx->ctx_smfi->xxfi_name,
1064 				(long) g->a_ctx->ctx_id, (int) i, (int) l);
1065 			return _SMFIS_ABORT;
1066 		}
1067 		(void) memcpy((void *) &port, (void *) (s + i),
1068 			      sizeof port);
1069 		i += sizeof port;
1070 
1071 		/* make sure string is terminated */
1072 		if (s[l - 1] != '\0')
1073 			return _SMFIS_ABORT;
1074 # if NETINET
1075 		if (family == SMFIA_INET)
1076 		{
1077 			if (inet_aton(s + i, (struct in_addr *) &sockaddr.sin.sin_addr)
1078 			    != 1)
1079 			{
1080 				smi_log(SMI_LOG_ERR,
1081 					"%s: connect[%ld]: inet_aton failed",
1082 					g->a_ctx->ctx_smfi->xxfi_name,
1083 					(long) g->a_ctx->ctx_id);
1084 				return _SMFIS_ABORT;
1085 			}
1086 			sockaddr.sa.sa_family = AF_INET;
1087 			if (port > 0)
1088 				sockaddr.sin.sin_port = port;
1089 		}
1090 		else
1091 # endif /* NETINET */
1092 # if NETINET6
1093 		if (family == SMFIA_INET6)
1094 		{
1095 			if (mi_inet_pton(AF_INET6, s + i,
1096 					 &sockaddr.sin6.sin6_addr) != 1)
1097 			{
1098 				smi_log(SMI_LOG_ERR,
1099 					"%s: connect[%ld]: mi_inet_pton failed",
1100 					g->a_ctx->ctx_smfi->xxfi_name,
1101 					(long) g->a_ctx->ctx_id);
1102 				return _SMFIS_ABORT;
1103 			}
1104 			sockaddr.sa.sa_family = AF_INET6;
1105 			if (port > 0)
1106 				sockaddr.sin6.sin6_port = port;
1107 		}
1108 		else
1109 # endif /* NETINET6 */
1110 # if NETUNIX
1111 		if (family == SMFIA_UNIX)
1112 		{
1113 			if (sm_strlcpy(sockaddr.sunix.sun_path, s + i,
1114 			    sizeof sockaddr.sunix.sun_path) >=
1115 			    sizeof sockaddr.sunix.sun_path)
1116 			{
1117 				smi_log(SMI_LOG_ERR,
1118 					"%s: connect[%ld]: path too long",
1119 					g->a_ctx->ctx_smfi->xxfi_name,
1120 					(long) g->a_ctx->ctx_id);
1121 				return _SMFIS_ABORT;
1122 			}
1123 			sockaddr.sunix.sun_family = AF_UNIX;
1124 		}
1125 		else
1126 # endif /* NETUNIX */
1127 		{
1128 			smi_log(SMI_LOG_ERR,
1129 				"%s: connect[%ld]: unknown family %d",
1130 				g->a_ctx->ctx_smfi->xxfi_name,
1131 				(long) g->a_ctx->ctx_id, family);
1132 			return _SMFIS_ABORT;
1133 		}
1134 	}
1135 	return (*fi_connect)(g->a_ctx, g->a_buf,
1136 			     family != SMFIA_UNKNOWN ? &sockaddr : NULL);
1137 }
1138 
1139 /*
1140 **  ST_EOH -- end of headers
1141 **
1142 **	Parameters:
1143 **		g -- generic argument structure
1144 **
1145 **	Returns:
1146 **		continue or filter-specified value
1147 */
1148 
1149 static int
1150 st_eoh(g)
1151 	genarg *g;
1152 {
1153 	sfsistat (*fi_eoh) __P((SMFICTX *));
1154 
1155 	if (g == NULL)
1156 		return _SMFIS_ABORT;
1157 	if (g->a_ctx->ctx_smfi != NULL &&
1158 	    (fi_eoh = g->a_ctx->ctx_smfi->xxfi_eoh) != NULL)
1159 		return (*fi_eoh)(g->a_ctx);
1160 	return SMFIS_CONTINUE;
1161 }
1162 
1163 /*
1164 **  ST_DATA -- DATA command
1165 **
1166 **	Parameters:
1167 **		g -- generic argument structure
1168 **
1169 **	Returns:
1170 **		continue or filter-specified value
1171 */
1172 
1173 static int
1174 st_data(g)
1175 	genarg *g;
1176 {
1177 	sfsistat (*fi_data) __P((SMFICTX *));
1178 
1179 	if (g == NULL)
1180 		return _SMFIS_ABORT;
1181 	if (g->a_ctx->ctx_smfi != NULL &&
1182 	    g->a_ctx->ctx_smfi->xxfi_version > 3 &&
1183 	    (fi_data = g->a_ctx->ctx_smfi->xxfi_data) != NULL)
1184 		return (*fi_data)(g->a_ctx);
1185 	return SMFIS_CONTINUE;
1186 }
1187 
1188 /*
1189 **  ST_HELO -- helo/ehlo command
1190 **
1191 **	Parameters:
1192 **		g -- generic argument structure
1193 **
1194 **	Returns:
1195 **		continue or filter-specified value
1196 */
1197 
1198 static int
1199 st_helo(g)
1200 	genarg *g;
1201 {
1202 	sfsistat (*fi_helo) __P((SMFICTX *, char *));
1203 
1204 	if (g == NULL)
1205 		return _SMFIS_ABORT;
1206 	mi_clr_macros(g->a_ctx, g->a_idx + 1);
1207 	if (g->a_ctx->ctx_smfi != NULL &&
1208 	    (fi_helo = g->a_ctx->ctx_smfi->xxfi_helo) != NULL)
1209 	{
1210 		/* paranoia: check for terminating '\0' */
1211 		if (g->a_len == 0 || g->a_buf[g->a_len - 1] != '\0')
1212 			return MI_FAILURE;
1213 		return (*fi_helo)(g->a_ctx, g->a_buf);
1214 	}
1215 	return SMFIS_CONTINUE;
1216 }
1217 
1218 /*
1219 **  ST_HEADER -- header line
1220 **
1221 **	Parameters:
1222 **		g -- generic argument structure
1223 **
1224 **	Returns:
1225 **		continue or filter-specified value
1226 */
1227 
1228 static int
1229 st_header(g)
1230 	genarg *g;
1231 {
1232 	char *hf, *hv;
1233 	sfsistat (*fi_header) __P((SMFICTX *, char *, char *));
1234 
1235 	if (g == NULL)
1236 		return _SMFIS_ABORT;
1237 	if (g->a_ctx->ctx_smfi == NULL ||
1238 	    (fi_header = g->a_ctx->ctx_smfi->xxfi_header) == NULL)
1239 		return SMFIS_CONTINUE;
1240 	if (dec_arg2(g->a_buf, g->a_len, &hf, &hv) == MI_SUCCESS)
1241 		return (*fi_header)(g->a_ctx, hf, hv);
1242 	else
1243 		return _SMFIS_ABORT;
1244 }
1245 
1246 #define ARGV_FCT(lf, rf, idx)					\
1247 	char **argv;						\
1248 	sfsistat (*lf) __P((SMFICTX *, char **));		\
1249 	int r;							\
1250 								\
1251 	if (g == NULL)						\
1252 		return _SMFIS_ABORT;				\
1253 	mi_clr_macros(g->a_ctx, g->a_idx + 1);			\
1254 	if (g->a_ctx->ctx_smfi == NULL ||			\
1255 	    (lf = g->a_ctx->ctx_smfi->rf) == NULL)		\
1256 		return SMFIS_CONTINUE;				\
1257 	if ((argv = dec_argv(g->a_buf, g->a_len)) == NULL)	\
1258 		return _SMFIS_ABORT;				\
1259 	r = (*lf)(g->a_ctx, argv);				\
1260 	free(argv);						\
1261 	return r;
1262 
1263 /*
1264 **  ST_SENDER -- MAIL FROM command
1265 **
1266 **	Parameters:
1267 **		g -- generic argument structure
1268 **
1269 **	Returns:
1270 **		continue or filter-specified value
1271 */
1272 
1273 static int
1274 st_sender(g)
1275 	genarg *g;
1276 {
1277 	ARGV_FCT(fi_envfrom, xxfi_envfrom, CI_MAIL)
1278 }
1279 
1280 /*
1281 **  ST_RCPT -- RCPT TO command
1282 **
1283 **	Parameters:
1284 **		g -- generic argument structure
1285 **
1286 **	Returns:
1287 **		continue or filter-specified value
1288 */
1289 
1290 static int
1291 st_rcpt(g)
1292 	genarg *g;
1293 {
1294 	ARGV_FCT(fi_envrcpt, xxfi_envrcpt, CI_RCPT)
1295 }
1296 
1297 /*
1298 **  ST_UNKNOWN -- unrecognized or unimplemented command
1299 **
1300 **	Parameters:
1301 **		g -- generic argument structure
1302 **
1303 **	Returns:
1304 **		continue or filter-specified value
1305 */
1306 
1307 static int
1308 st_unknown(g)
1309 	genarg *g;
1310 {
1311 	sfsistat (*fi_unknown) __P((SMFICTX *, const char *));
1312 
1313 	if (g == NULL)
1314 		return _SMFIS_ABORT;
1315 	if (g->a_ctx->ctx_smfi != NULL &&
1316 	    g->a_ctx->ctx_smfi->xxfi_version > 2 &&
1317 	    (fi_unknown = g->a_ctx->ctx_smfi->xxfi_unknown) != NULL)
1318 		return (*fi_unknown)(g->a_ctx, (const char *) g->a_buf);
1319 	return SMFIS_CONTINUE;
1320 }
1321 
1322 /*
1323 **  ST_MACROS -- deal with macros received from the MTA
1324 **
1325 **	Parameters:
1326 **		g -- generic argument structure
1327 **
1328 **	Returns:
1329 **		continue/keep
1330 **
1331 **	Side effects:
1332 **		set pointer in macro array to current values.
1333 */
1334 
1335 static int
1336 st_macros(g)
1337 	genarg *g;
1338 {
1339 	int i;
1340 	char **argv;
1341 
1342 	if (g == NULL || g->a_len < 1)
1343 		return _SMFIS_FAIL;
1344 	if ((argv = dec_argv(g->a_buf + 1, g->a_len - 1)) == NULL)
1345 		return _SMFIS_FAIL;
1346 	switch (g->a_buf[0])
1347 	{
1348 	  case SMFIC_CONNECT:
1349 		i = CI_CONN;
1350 		break;
1351 	  case SMFIC_HELO:
1352 		i = CI_HELO;
1353 		break;
1354 	  case SMFIC_MAIL:
1355 		i = CI_MAIL;
1356 		break;
1357 	  case SMFIC_RCPT:
1358 		i = CI_RCPT;
1359 		break;
1360 	  case SMFIC_DATA:
1361 		i = CI_DATA;
1362 		break;
1363 	  case SMFIC_BODYEOB:
1364 		i = CI_EOM;
1365 		break;
1366 	  case SMFIC_EOH:
1367 		i = CI_EOH;
1368 		break;
1369 	  default:
1370 		free(argv);
1371 		return _SMFIS_FAIL;
1372 	}
1373 	if (g->a_ctx->ctx_mac_ptr[i] != NULL)
1374 		free(g->a_ctx->ctx_mac_ptr[i]);
1375 	if (g->a_ctx->ctx_mac_buf[i] != NULL)
1376 		free(g->a_ctx->ctx_mac_buf[i]);
1377 	g->a_ctx->ctx_mac_ptr[i] = argv;
1378 	g->a_ctx->ctx_mac_buf[i] = g->a_buf;
1379 	return _SMFIS_KEEP;
1380 }
1381 
1382 /*
1383 **  ST_QUIT -- quit command
1384 **
1385 **	Parameters:
1386 **		g -- generic argument structure
1387 **
1388 **	Returns:
1389 **		noreply
1390 */
1391 
1392 /* ARGSUSED */
1393 static int
1394 st_quit(g)
1395 	genarg *g;
1396 {
1397 	sfsistat (*fi_close) __P((SMFICTX *));
1398 
1399 	if (g == NULL)
1400 		return _SMFIS_ABORT;
1401 	if (g->a_ctx->ctx_smfi != NULL &&
1402 	    (fi_close = g->a_ctx->ctx_smfi->xxfi_close) != NULL)
1403 		(void) (*fi_close)(g->a_ctx);
1404 	mi_clr_macros(g->a_ctx, 0);
1405 	return _SMFIS_NOREPLY;
1406 }
1407 
1408 /*
1409 **  ST_BODYCHUNK -- deal with a piece of the mail body
1410 **
1411 **	Parameters:
1412 **		g -- generic argument structure
1413 **
1414 **	Returns:
1415 **		continue or filter-specified value
1416 */
1417 
1418 static int
1419 st_bodychunk(g)
1420 	genarg *g;
1421 {
1422 	sfsistat (*fi_body) __P((SMFICTX *, unsigned char *, size_t));
1423 
1424 	if (g == NULL)
1425 		return _SMFIS_ABORT;
1426 	if (g->a_ctx->ctx_smfi != NULL &&
1427 	    (fi_body = g->a_ctx->ctx_smfi->xxfi_body) != NULL)
1428 		return (*fi_body)(g->a_ctx, (unsigned char *)g->a_buf,
1429 				  g->a_len);
1430 	return SMFIS_CONTINUE;
1431 }
1432 
1433 /*
1434 **  ST_BODYEND -- deal with the last piece of the mail body
1435 **
1436 **	Parameters:
1437 **		g -- generic argument structure
1438 **
1439 **	Returns:
1440 **		continue or filter-specified value
1441 **
1442 **	Side effects:
1443 **		sends a reply for the body part (if non-empty).
1444 */
1445 
1446 static int
1447 st_bodyend(g)
1448 	genarg *g;
1449 {
1450 	sfsistat r;
1451 	sfsistat (*fi_body) __P((SMFICTX *, unsigned char *, size_t));
1452 	sfsistat (*fi_eom) __P((SMFICTX *));
1453 
1454 	if (g == NULL)
1455 		return _SMFIS_ABORT;
1456 	r = SMFIS_CONTINUE;
1457 	if (g->a_ctx->ctx_smfi != NULL)
1458 	{
1459 		if ((fi_body = g->a_ctx->ctx_smfi->xxfi_body) != NULL &&
1460 		    g->a_len > 0)
1461 		{
1462 			socket_t sd;
1463 			struct timeval timeout;
1464 
1465 			timeout.tv_sec = g->a_ctx->ctx_timeout;
1466 			timeout.tv_usec = 0;
1467 			sd = g->a_ctx->ctx_sd;
1468 			r = (*fi_body)(g->a_ctx, (unsigned char *)g->a_buf,
1469 				       g->a_len);
1470 			if (r != SMFIS_CONTINUE &&
1471 			    sendreply(r, sd, &timeout, g->a_ctx) != MI_SUCCESS)
1472 				return _SMFIS_ABORT;
1473 		}
1474 	}
1475 	if (r == SMFIS_CONTINUE &&
1476 	    (fi_eom = g->a_ctx->ctx_smfi->xxfi_eom) != NULL)
1477 		return (*fi_eom)(g->a_ctx);
1478 	return r;
1479 }
1480 
1481 /*
1482 **  ST_ABORTFCT -- deal with aborts
1483 **
1484 **	Parameters:
1485 **		g -- generic argument structure
1486 **
1487 **	Returns:
1488 **		abort or filter-specified value
1489 */
1490 
1491 static int
1492 st_abortfct(g)
1493 	genarg *g;
1494 {
1495 	sfsistat (*fi_abort) __P((SMFICTX *));
1496 
1497 	if (g == NULL)
1498 		return _SMFIS_ABORT;
1499 	if (g != NULL && g->a_ctx->ctx_smfi != NULL &&
1500 	    (fi_abort = g->a_ctx->ctx_smfi->xxfi_abort) != NULL)
1501 		(void) (*fi_abort)(g->a_ctx);
1502 	return _SMFIS_NOREPLY;
1503 }
1504 
1505 /*
1506 **  TRANS_OK -- is the state transition ok?
1507 **
1508 **	Parameters:
1509 **		old -- old state
1510 **		new -- new state
1511 **
1512 **	Returns:
1513 **		state transition ok
1514 */
1515 
1516 static bool
1517 trans_ok(old, new)
1518 	int old, new;
1519 {
1520 	int s, n;
1521 
1522 	s = old;
1523 	if (s >= SIZE_NEXT_STATES)
1524 		return false;
1525 	do
1526 	{
1527 		/* is this state transition allowed? */
1528 		if ((MI_MASK(new) & next_states[s]) != 0)
1529 			return true;
1530 
1531 		/*
1532 		**  no: try next state;
1533 		**  this works since the relevant states are ordered
1534 		**  strict sequentially
1535 		*/
1536 
1537 		n = s + 1;
1538 		if (n >= SIZE_NEXT_STATES)
1539 			return false;
1540 
1541 		/*
1542 		**  can we actually "skip" this state?
1543 		**  see fix_stm() which sets this bit for those
1544 		**  states which the filter program is not interested in
1545 		*/
1546 
1547 		if (bitset(NX_SKIP, next_states[n]))
1548 			s = n;
1549 		else
1550 			return false;
1551 	} while (s < SIZE_NEXT_STATES);
1552 	return false;
1553 }
1554 
1555 /*
1556 **  FIX_STM -- add "skip" bits to the state transition table
1557 **
1558 **	Parameters:
1559 **		ctx -- context structure
1560 **
1561 **	Returns:
1562 **		None.
1563 **
1564 **	Side effects:
1565 **		may change state transition table.
1566 */
1567 
1568 static void
1569 fix_stm(ctx)
1570 	SMFICTX_PTR ctx;
1571 {
1572 	unsigned long fl;
1573 
1574 	if (ctx == NULL || ctx->ctx_smfi == NULL)
1575 		return;
1576 	fl = ctx->ctx_pflags;
1577 	if (bitset(SMFIP_NOCONNECT, fl))
1578 		next_states[ST_CONN] |= NX_SKIP;
1579 	if (bitset(SMFIP_NOHELO, fl))
1580 		next_states[ST_HELO] |= NX_SKIP;
1581 	if (bitset(SMFIP_NOMAIL, fl))
1582 		next_states[ST_MAIL] |= NX_SKIP;
1583 	if (bitset(SMFIP_NORCPT, fl))
1584 		next_states[ST_RCPT] |= NX_SKIP;
1585 	if (bitset(SMFIP_NOHDRS, fl))
1586 		next_states[ST_HDRS] |= NX_SKIP;
1587 	if (bitset(SMFIP_NOEOH, fl))
1588 		next_states[ST_EOHS] |= NX_SKIP;
1589 	if (bitset(SMFIP_NOBODY, fl))
1590 		next_states[ST_BODY] |= NX_SKIP;
1591 	if (bitset(SMFIP_NODATA, fl))
1592 		next_states[ST_DATA] |= NX_SKIP;
1593 	if (bitset(SMFIP_NOUNKNOWN, fl))
1594 		next_states[ST_UNKN] |= NX_SKIP;
1595 }
1596 
1597 /*
1598 **  DEC_ARGV -- split a buffer into a list of strings, NULL terminated
1599 **
1600 **	Parameters:
1601 **		buf -- buffer with several strings
1602 **		len -- length of buffer
1603 **
1604 **	Returns:
1605 **		array of pointers to the individual strings
1606 */
1607 
1608 static char **
1609 dec_argv(buf, len)
1610 	char *buf;
1611 	size_t len;
1612 {
1613 	char **s;
1614 	size_t i;
1615 	int elem, nelem;
1616 
1617 	nelem = 0;
1618 	for (i = 0; i < len; i++)
1619 	{
1620 		if (buf[i] == '\0')
1621 			++nelem;
1622 	}
1623 	if (nelem == 0)
1624 		return NULL;
1625 
1626 	/* last entry is only for the name */
1627 	s = (char **)malloc((nelem + 1) * (sizeof *s));
1628 	if (s == NULL)
1629 		return NULL;
1630 	s[0] = buf;
1631 	for (i = 0, elem = 0; i < len && elem < nelem; i++)
1632 	{
1633 		if (buf[i] == '\0')
1634 		{
1635 			++elem;
1636 			if (i + 1 >= len)
1637 				s[elem] = NULL;
1638 			else
1639 				s[elem] = &(buf[i + 1]);
1640 		}
1641 	}
1642 
1643 	/* overwrite last entry (already done above, just paranoia) */
1644 	s[elem] = NULL;
1645 	return s;
1646 }
1647 
1648 /*
1649 **  DEC_ARG2 -- split a buffer into two strings
1650 **
1651 **	Parameters:
1652 **		buf -- buffer with two strings
1653 **		len -- length of buffer
1654 **		s1,s2 -- pointer to result strings
1655 **
1656 **	Returns:
1657 **		MI_FAILURE/MI_SUCCESS
1658 */
1659 
1660 static int
1661 dec_arg2(buf, len, s1, s2)
1662 	char *buf;
1663 	size_t len;
1664 	char **s1;
1665 	char **s2;
1666 {
1667 	size_t i;
1668 
1669 	/* paranoia: check for terminating '\0' */
1670 	if (len == 0 || buf[len - 1] != '\0')
1671 		return MI_FAILURE;
1672 	*s1 = buf;
1673 	for (i = 1; i < len && buf[i] != '\0'; i++)
1674 		continue;
1675 	if (i >= len - 1)
1676 		return MI_FAILURE;
1677 	*s2 = buf + i + 1;
1678 	return MI_SUCCESS;
1679 }
1680 
1681 /*
1682 **  SENDOK -- is it ok for the filter to send stuff to the MTA?
1683 **
1684 **	Parameters:
1685 **		ctx -- context structure
1686 **		flag -- flag to check
1687 **
1688 **	Returns:
1689 **		sending allowed (in current state)
1690 */
1691 
1692 bool
1693 mi_sendok(ctx, flag)
1694 	SMFICTX_PTR ctx;
1695 	int flag;
1696 {
1697 	if (ctx == NULL || ctx->ctx_smfi == NULL)
1698 		return false;
1699 
1700 	/* did the milter request this operation? */
1701 	if (flag != 0 && !bitset(flag, ctx->ctx_aflags))
1702 		return false;
1703 
1704 	/* are we in the correct state? It must be "End of Message". */
1705 	return ctx->ctx_state == ST_ENDM;
1706 }
1707 
1708 #if _FFR_WORKERS_POOL
1709 /*
1710 **  MI_RD_SOCKET_READY - checks if the socket is ready for read(2)
1711 **
1712 **	Parameters:
1713 **		sd -- socket_t
1714 **
1715 **	Returns:
1716 **		true iff socket is ready for read(2)
1717 */
1718 
1719 #define MI_RD_CMD_TO  1
1720 #define MI_RD_MAX_ERR 16
1721 
1722 static bool
1723 mi_rd_socket_ready (sd)
1724 	socket_t sd;
1725 {
1726 	int n;
1727 	int nerr = 0;
1728 #if SM_CONF_POLL
1729 	struct pollfd pfd;
1730 #else /* SM_CONF_POLL */
1731 	fd_set	rd_set, exc_set;
1732 #endif /* SM_CONF_POLL */
1733 
1734 	do
1735 	{
1736 #if SM_CONF_POLL
1737 		pfd.fd = sd;
1738 		pfd.events = POLLIN;
1739 		pfd.revents = 0;
1740 
1741 		n = poll(&pfd, 1, MI_RD_CMD_TO);
1742 #else /* SM_CONF_POLL */
1743 		struct timeval timeout;
1744 
1745 		FD_ZERO(&rd_set);
1746 		FD_ZERO(&exc_set);
1747 		FD_SET(sd, &rd_set);
1748 		FD_SET(sd, &exc_set);
1749 
1750 		timeout.tv_sec = MI_RD_CMD_TO / 1000;
1751 		timeout.tv_usec = 0;
1752 		n = select(sd + 1, &rd_set, NULL, &exc_set, &timeout);
1753 #endif /* SM_CONF_POLL */
1754 
1755 		if (n < 0)
1756 		{
1757 			if (errno == EINTR)
1758 			{
1759 				nerr++;
1760 				continue;
1761 			}
1762 			return true;
1763 		}
1764 
1765 		if (n == 0)
1766 			return false;
1767 		break;
1768 	} while (nerr < MI_RD_MAX_ERR);
1769 	if (nerr >= MI_RD_MAX_ERR)
1770 		return false;
1771 
1772 #if SM_CONF_POLL
1773 	return (pfd.revents != 0);
1774 #else /* SM_CONF_POLL */
1775 	return FD_ISSET(sd, &rd_set) || FD_ISSET(sd, &exc_set);
1776 #endif /* SM_CONF_POLL */
1777 }
1778 #endif /* _FFR_WORKERS_POOL */
1779