10Sstevel@tonic-gate /*
26562Sjbeck * Copyright (c) 1999-2004, 2006-2008 Sendmail, Inc. and its suppliers.
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * By using this file, you agree to the terms and conditions set
60Sstevel@tonic-gate * forth in the LICENSE file which can be found at the top level of
70Sstevel@tonic-gate * the sendmail distribution.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate */
100Sstevel@tonic-gate
110Sstevel@tonic-gate #include <sm/gen.h>
12*11440SJohn.Beck@Sun.COM SM_RCSID("@(#)$Id: engine.c,v 8.166 2009/11/06 00:57:07 ca Exp $")
130Sstevel@tonic-gate
140Sstevel@tonic-gate #include "libmilter.h"
150Sstevel@tonic-gate
160Sstevel@tonic-gate #if NETINET || NETINET6
170Sstevel@tonic-gate # include <arpa/inet.h>
180Sstevel@tonic-gate #endif /* NETINET || NETINET6 */
190Sstevel@tonic-gate
200Sstevel@tonic-gate /* generic argument for functions in the command table */
210Sstevel@tonic-gate struct arg_struct
220Sstevel@tonic-gate {
230Sstevel@tonic-gate size_t a_len; /* length of buffer */
240Sstevel@tonic-gate char *a_buf; /* argument string */
250Sstevel@tonic-gate int a_idx; /* index for macro array */
260Sstevel@tonic-gate SMFICTX_PTR a_ctx; /* context */
270Sstevel@tonic-gate };
280Sstevel@tonic-gate
290Sstevel@tonic-gate typedef struct arg_struct genarg;
300Sstevel@tonic-gate
310Sstevel@tonic-gate /* structure for commands received from MTA */
320Sstevel@tonic-gate struct cmdfct_t
330Sstevel@tonic-gate {
340Sstevel@tonic-gate char cm_cmd; /* command */
350Sstevel@tonic-gate int cm_argt; /* type of arguments expected */
360Sstevel@tonic-gate int cm_next; /* next state */
370Sstevel@tonic-gate int cm_todo; /* what to do next */
380Sstevel@tonic-gate int cm_macros; /* index for macros */
390Sstevel@tonic-gate int (*cm_fct) __P((genarg *)); /* function to execute */
400Sstevel@tonic-gate };
410Sstevel@tonic-gate
420Sstevel@tonic-gate typedef struct cmdfct_t cmdfct;
430Sstevel@tonic-gate
440Sstevel@tonic-gate /* possible values for cm_argt */
450Sstevel@tonic-gate #define CM_ARG0 0 /* no args */
460Sstevel@tonic-gate #define CM_ARG1 1 /* one arg (string) */
470Sstevel@tonic-gate #define CM_ARG2 2 /* two args (strings) */
480Sstevel@tonic-gate #define CM_ARGA 4 /* one string and _SOCK_ADDR */
490Sstevel@tonic-gate #define CM_ARGO 5 /* two integers */
500Sstevel@tonic-gate #define CM_ARGV 8 /* \0 separated list of args, NULL-terminated */
510Sstevel@tonic-gate #define CM_ARGN 9 /* \0 separated list of args (strings) */
520Sstevel@tonic-gate
530Sstevel@tonic-gate /* possible values for cm_todo */
540Sstevel@tonic-gate #define CT_CONT 0x0000 /* continue reading commands */
550Sstevel@tonic-gate #define CT_IGNO 0x0001 /* continue even when error */
560Sstevel@tonic-gate
570Sstevel@tonic-gate /* not needed right now, done via return code instead */
580Sstevel@tonic-gate #define CT_KEEP 0x0004 /* keep buffer (contains symbols) */
593544Sjbeck #define CT_END 0x0008 /* last command of session, stop replying */
600Sstevel@tonic-gate
610Sstevel@tonic-gate /* index in macro array: macros only for these commands */
620Sstevel@tonic-gate #define CI_NONE (-1)
630Sstevel@tonic-gate #define CI_CONN 0
640Sstevel@tonic-gate #define CI_HELO 1
650Sstevel@tonic-gate #define CI_MAIL 2
660Sstevel@tonic-gate #define CI_RCPT 3
673544Sjbeck #define CI_DATA 4
683544Sjbeck #define CI_EOM 5
693544Sjbeck #define CI_EOH 6
703544Sjbeck #define CI_LAST CI_EOH
713544Sjbeck #if CI_LAST < CI_DATA
723544Sjbeck ERROR: do not compile with CI_LAST < CI_DATA
733544Sjbeck #endif
743544Sjbeck #if CI_LAST < CI_EOM
753544Sjbeck ERROR: do not compile with CI_LAST < CI_EOM
763544Sjbeck #endif
773544Sjbeck #if CI_LAST < CI_EOH
783544Sjbeck ERROR: do not compile with CI_LAST < CI_EOH
793544Sjbeck #endif
803544Sjbeck #if CI_LAST < CI_ENVRCPT
813544Sjbeck ERROR: do not compile with CI_LAST < CI_ENVRCPT
823544Sjbeck #endif
833544Sjbeck #if CI_LAST < CI_ENVFROM
843544Sjbeck ERROR: do not compile with CI_LAST < CI_ENVFROM
853544Sjbeck #endif
863544Sjbeck #if CI_LAST < CI_HELO
873544Sjbeck ERROR: do not compile with CI_LAST < CI_HELO
883544Sjbeck #endif
893544Sjbeck #if CI_LAST < CI_CONNECT
903544Sjbeck ERROR: do not compile with CI_LAST < CI_CONNECT
913544Sjbeck #endif
923544Sjbeck #if CI_LAST >= MAX_MACROS_ENTRIES
933544Sjbeck ERROR: do not compile with CI_LAST >= MAX_MACROS_ENTRIES
940Sstevel@tonic-gate #endif
950Sstevel@tonic-gate
960Sstevel@tonic-gate /* function prototypes */
970Sstevel@tonic-gate static int st_abortfct __P((genarg *));
980Sstevel@tonic-gate static int st_macros __P((genarg *));
990Sstevel@tonic-gate static int st_optionneg __P((genarg *));
1000Sstevel@tonic-gate static int st_bodychunk __P((genarg *));
1010Sstevel@tonic-gate static int st_connectinfo __P((genarg *));
1020Sstevel@tonic-gate static int st_bodyend __P((genarg *));
1030Sstevel@tonic-gate static int st_helo __P((genarg *));
1040Sstevel@tonic-gate static int st_header __P((genarg *));
1050Sstevel@tonic-gate static int st_sender __P((genarg *));
1060Sstevel@tonic-gate static int st_rcpt __P((genarg *));
1070Sstevel@tonic-gate static int st_unknown __P((genarg *));
1080Sstevel@tonic-gate static int st_data __P((genarg *));
1090Sstevel@tonic-gate static int st_eoh __P((genarg *));
1100Sstevel@tonic-gate static int st_quit __P((genarg *));
1110Sstevel@tonic-gate static int sendreply __P((sfsistat, socket_t, struct timeval *, SMFICTX_PTR));
1120Sstevel@tonic-gate static void fix_stm __P((SMFICTX_PTR));
1130Sstevel@tonic-gate static bool trans_ok __P((int, int));
1140Sstevel@tonic-gate static char **dec_argv __P((char *, size_t));
1150Sstevel@tonic-gate static int dec_arg2 __P((char *, size_t, char **, char **));
116*11440SJohn.Beck@Sun.COM static void mi_clr_symlist __P((SMFICTX_PTR));
1170Sstevel@tonic-gate
1183544Sjbeck #if _FFR_WORKERS_POOL
1193544Sjbeck static bool mi_rd_socket_ready __P((int));
1203544Sjbeck #endif /* _FFR_WORKERS_POOL */
1213544Sjbeck
1220Sstevel@tonic-gate /* states */
1230Sstevel@tonic-gate #define ST_NONE (-1)
1240Sstevel@tonic-gate #define ST_INIT 0 /* initial state */
1250Sstevel@tonic-gate #define ST_OPTS 1 /* option negotiation */
1260Sstevel@tonic-gate #define ST_CONN 2 /* connection info */
1270Sstevel@tonic-gate #define ST_HELO 3 /* helo */
1280Sstevel@tonic-gate #define ST_MAIL 4 /* mail from */
1290Sstevel@tonic-gate #define ST_RCPT 5 /* rcpt to */
1300Sstevel@tonic-gate #define ST_DATA 6 /* data */
1310Sstevel@tonic-gate #define ST_HDRS 7 /* headers */
1320Sstevel@tonic-gate #define ST_EOHS 8 /* end of headers */
1330Sstevel@tonic-gate #define ST_BODY 9 /* body */
1340Sstevel@tonic-gate #define ST_ENDM 10 /* end of message */
1350Sstevel@tonic-gate #define ST_QUIT 11 /* quit */
1360Sstevel@tonic-gate #define ST_ABRT 12 /* abort */
1370Sstevel@tonic-gate #define ST_UNKN 13 /* unknown SMTP command */
1383544Sjbeck #define ST_Q_NC 14 /* quit, new connection follows */
1393544Sjbeck #define ST_LAST ST_Q_NC /* last valid state */
1403544Sjbeck #define ST_SKIP 16 /* not a state but required for the state table */
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate /* in a mail transaction? must be before eom according to spec. */
1430Sstevel@tonic-gate #define ST_IN_MAIL(st) ((st) >= ST_MAIL && (st) < ST_ENDM)
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate /*
1460Sstevel@tonic-gate ** set of next states
1470Sstevel@tonic-gate ** each state (ST_*) corresponds to bit in an int value (1 << state)
1480Sstevel@tonic-gate ** each state has a set of allowed transitions ('or' of bits of states)
1490Sstevel@tonic-gate ** so a state transition is valid if the mask of the next state
1500Sstevel@tonic-gate ** is set in the NX_* value
1510Sstevel@tonic-gate ** this function is coded in trans_ok(), see below.
1520Sstevel@tonic-gate */
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate #define MI_MASK(x) (0x0001 << (x)) /* generate a bit "mask" for a state */
1550Sstevel@tonic-gate #define NX_INIT (MI_MASK(ST_OPTS))
1560Sstevel@tonic-gate #define NX_OPTS (MI_MASK(ST_CONN) | MI_MASK(ST_UNKN))
1570Sstevel@tonic-gate #define NX_CONN (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN))
1580Sstevel@tonic-gate #define NX_HELO (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN))
1590Sstevel@tonic-gate #define NX_MAIL (MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | MI_MASK(ST_UNKN))
1600Sstevel@tonic-gate #define NX_RCPT (MI_MASK(ST_HDRS) | MI_MASK(ST_EOHS) | MI_MASK(ST_DATA) | \
1610Sstevel@tonic-gate MI_MASK(ST_BODY) | MI_MASK(ST_ENDM) | \
1620Sstevel@tonic-gate MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | MI_MASK(ST_UNKN))
1630Sstevel@tonic-gate #define NX_DATA (MI_MASK(ST_EOHS) | MI_MASK(ST_HDRS) | MI_MASK(ST_ABRT))
1640Sstevel@tonic-gate #define NX_HDRS (MI_MASK(ST_EOHS) | MI_MASK(ST_HDRS) | MI_MASK(ST_ABRT))
1650Sstevel@tonic-gate #define NX_EOHS (MI_MASK(ST_BODY) | MI_MASK(ST_ENDM) | MI_MASK(ST_ABRT))
1660Sstevel@tonic-gate #define NX_BODY (MI_MASK(ST_ENDM) | MI_MASK(ST_BODY) | MI_MASK(ST_ABRT))
1673544Sjbeck #define NX_ENDM (MI_MASK(ST_QUIT) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN) | \
1683544Sjbeck MI_MASK(ST_Q_NC))
1690Sstevel@tonic-gate #define NX_QUIT 0
1700Sstevel@tonic-gate #define NX_ABRT 0
1710Sstevel@tonic-gate #define NX_UNKN (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | \
1720Sstevel@tonic-gate MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | \
1730Sstevel@tonic-gate MI_MASK(ST_DATA) | \
1740Sstevel@tonic-gate MI_MASK(ST_BODY) | MI_MASK(ST_UNKN) | \
1753544Sjbeck MI_MASK(ST_ABRT) | MI_MASK(ST_QUIT) | MI_MASK(ST_Q_NC))
1763544Sjbeck #define NX_Q_NC (MI_MASK(ST_CONN) | MI_MASK(ST_UNKN))
1770Sstevel@tonic-gate #define NX_SKIP MI_MASK(ST_SKIP)
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate static int next_states[] =
1800Sstevel@tonic-gate {
1813544Sjbeck NX_INIT
1823544Sjbeck , NX_OPTS
1833544Sjbeck , NX_CONN
1843544Sjbeck , NX_HELO
1853544Sjbeck , NX_MAIL
1863544Sjbeck , NX_RCPT
1873544Sjbeck , NX_DATA
1883544Sjbeck , NX_HDRS
1893544Sjbeck , NX_EOHS
1903544Sjbeck , NX_BODY
1913544Sjbeck , NX_ENDM
1923544Sjbeck , NX_QUIT
1933544Sjbeck , NX_ABRT
1943544Sjbeck , NX_UNKN
1953544Sjbeck , NX_Q_NC
1960Sstevel@tonic-gate };
1970Sstevel@tonic-gate
1982197Sjbeck #define SIZE_NEXT_STATES (sizeof(next_states) / sizeof(next_states[0]))
1992197Sjbeck
2000Sstevel@tonic-gate /* commands received by milter */
2010Sstevel@tonic-gate static cmdfct cmds[] =
2020Sstevel@tonic-gate {
2033544Sjbeck {SMFIC_ABORT, CM_ARG0, ST_ABRT, CT_CONT, CI_NONE, st_abortfct }
2043544Sjbeck , {SMFIC_MACRO, CM_ARGV, ST_NONE, CT_KEEP, CI_NONE, st_macros }
2053544Sjbeck , {SMFIC_BODY, CM_ARG1, ST_BODY, CT_CONT, CI_NONE, st_bodychunk }
2063544Sjbeck , {SMFIC_CONNECT, CM_ARG2, ST_CONN, CT_CONT, CI_CONN, st_connectinfo }
2073544Sjbeck , {SMFIC_BODYEOB, CM_ARG1, ST_ENDM, CT_CONT, CI_EOM, st_bodyend }
2083544Sjbeck , {SMFIC_HELO, CM_ARG1, ST_HELO, CT_CONT, CI_HELO, st_helo }
2093544Sjbeck , {SMFIC_HEADER, CM_ARG2, ST_HDRS, CT_CONT, CI_NONE, st_header }
2103544Sjbeck , {SMFIC_MAIL, CM_ARGV, ST_MAIL, CT_CONT, CI_MAIL, st_sender }
2113544Sjbeck , {SMFIC_OPTNEG, CM_ARGO, ST_OPTS, CT_CONT, CI_NONE, st_optionneg }
2123544Sjbeck , {SMFIC_EOH, CM_ARG0, ST_EOHS, CT_CONT, CI_EOH, st_eoh }
2133544Sjbeck , {SMFIC_QUIT, CM_ARG0, ST_QUIT, CT_END, CI_NONE, st_quit }
2143544Sjbeck , {SMFIC_DATA, CM_ARG0, ST_DATA, CT_CONT, CI_DATA, st_data }
2153544Sjbeck , {SMFIC_RCPT, CM_ARGV, ST_RCPT, CT_IGNO, CI_RCPT, st_rcpt }
2163544Sjbeck , {SMFIC_UNKNOWN, CM_ARG1, ST_UNKN, CT_IGNO, CI_NONE, st_unknown }
2173544Sjbeck , {SMFIC_QUIT_NC, CM_ARG0, ST_Q_NC, CT_CONT, CI_NONE, st_quit }
2180Sstevel@tonic-gate };
2190Sstevel@tonic-gate
2203544Sjbeck /*
2213544Sjbeck ** Additional (internal) reply codes;
2223544Sjbeck ** must be coordinated wit libmilter/mfapi.h
2233544Sjbeck */
2243544Sjbeck
2250Sstevel@tonic-gate #define _SMFIS_KEEP 20
2260Sstevel@tonic-gate #define _SMFIS_ABORT 21
2270Sstevel@tonic-gate #define _SMFIS_OPTIONS 22
2283544Sjbeck #define _SMFIS_NOREPLY SMFIS_NOREPLY
2290Sstevel@tonic-gate #define _SMFIS_FAIL (-1)
2300Sstevel@tonic-gate #define _SMFIS_NONE (-2)
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate /*
2330Sstevel@tonic-gate ** MI_ENGINE -- receive commands and process them
2340Sstevel@tonic-gate **
2350Sstevel@tonic-gate ** Parameters:
2360Sstevel@tonic-gate ** ctx -- context structure
2370Sstevel@tonic-gate **
2380Sstevel@tonic-gate ** Returns:
2390Sstevel@tonic-gate ** MI_FAILURE/MI_SUCCESS
2400Sstevel@tonic-gate */
2413544Sjbeck
2420Sstevel@tonic-gate int
mi_engine(ctx)2430Sstevel@tonic-gate mi_engine(ctx)
2440Sstevel@tonic-gate SMFICTX_PTR ctx;
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate size_t len;
2470Sstevel@tonic-gate int i;
2480Sstevel@tonic-gate socket_t sd;
2490Sstevel@tonic-gate int ret = MI_SUCCESS;
2500Sstevel@tonic-gate int ncmds = sizeof(cmds) / sizeof(cmdfct);
2510Sstevel@tonic-gate int curstate = ST_INIT;
2520Sstevel@tonic-gate int newstate;
2530Sstevel@tonic-gate bool call_abort;
2540Sstevel@tonic-gate sfsistat r;
2550Sstevel@tonic-gate char cmd;
2560Sstevel@tonic-gate char *buf = NULL;
2570Sstevel@tonic-gate genarg arg;
2580Sstevel@tonic-gate struct timeval timeout;
2590Sstevel@tonic-gate int (*f) __P((genarg *));
2600Sstevel@tonic-gate sfsistat (*fi_abort) __P((SMFICTX *));
2610Sstevel@tonic-gate sfsistat (*fi_close) __P((SMFICTX *));
2620Sstevel@tonic-gate
2630Sstevel@tonic-gate arg.a_ctx = ctx;
2640Sstevel@tonic-gate sd = ctx->ctx_sd;
2650Sstevel@tonic-gate fi_abort = ctx->ctx_smfi->xxfi_abort;
2663544Sjbeck #if _FFR_WORKERS_POOL
2673544Sjbeck curstate = ctx->ctx_state;
2683544Sjbeck if (curstate == ST_INIT)
2693544Sjbeck {
2703544Sjbeck mi_clr_macros(ctx, 0);
2713544Sjbeck fix_stm(ctx);
2723544Sjbeck }
2733544Sjbeck #else /* _FFR_WORKERS_POOL */
2740Sstevel@tonic-gate mi_clr_macros(ctx, 0);
2750Sstevel@tonic-gate fix_stm(ctx);
2763544Sjbeck #endif /* _FFR_WORKERS_POOL */
2770Sstevel@tonic-gate r = _SMFIS_NONE;
2780Sstevel@tonic-gate do
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate /* call abort only if in a mail transaction */
2810Sstevel@tonic-gate call_abort = ST_IN_MAIL(curstate);
2820Sstevel@tonic-gate timeout.tv_sec = ctx->ctx_timeout;
2830Sstevel@tonic-gate timeout.tv_usec = 0;
2840Sstevel@tonic-gate if (mi_stop() == MILTER_ABRT)
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate if (ctx->ctx_dbg > 3)
2873544Sjbeck sm_dprintf("[%ld] milter_abort\n",
2883544Sjbeck (long) ctx->ctx_id);
2890Sstevel@tonic-gate ret = MI_FAILURE;
2900Sstevel@tonic-gate break;
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate /*
2940Sstevel@tonic-gate ** Notice: buf is allocated by mi_rd_cmd() and it will
2950Sstevel@tonic-gate ** usually be free()d after it has been used in f().
2960Sstevel@tonic-gate ** However, if the function returns _SMFIS_KEEP then buf
2970Sstevel@tonic-gate ** contains macros and will not be free()d.
2980Sstevel@tonic-gate ** Hence r must be set to _SMFIS_NONE if a new buf is
2990Sstevel@tonic-gate ** allocated to avoid problem with housekeeping, esp.
3000Sstevel@tonic-gate ** if the code "break"s out of the loop.
3010Sstevel@tonic-gate */
3020Sstevel@tonic-gate
3033544Sjbeck #if _FFR_WORKERS_POOL
3043544Sjbeck /* Is the socket ready to be read ??? */
3053544Sjbeck if (!mi_rd_socket_ready(sd))
3063544Sjbeck {
3073544Sjbeck ret = MI_CONTINUE;
3083544Sjbeck break;
3093544Sjbeck }
3103544Sjbeck #endif /* _FFR_WORKERS_POOL */
3113544Sjbeck
3120Sstevel@tonic-gate r = _SMFIS_NONE;
3130Sstevel@tonic-gate if ((buf = mi_rd_cmd(sd, &timeout, &cmd, &len,
3140Sstevel@tonic-gate ctx->ctx_smfi->xxfi_name)) == NULL &&
3150Sstevel@tonic-gate cmd < SMFIC_VALIDCMD)
3160Sstevel@tonic-gate {
3170Sstevel@tonic-gate if (ctx->ctx_dbg > 5)
3183544Sjbeck sm_dprintf("[%ld] mi_engine: mi_rd_cmd error (%x)\n",
3193544Sjbeck (long) ctx->ctx_id, (int) cmd);
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate /*
3220Sstevel@tonic-gate ** eof is currently treated as failure ->
3230Sstevel@tonic-gate ** abort() instead of close(), otherwise use:
3240Sstevel@tonic-gate ** if (cmd != SMFIC_EOF)
3250Sstevel@tonic-gate */
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate ret = MI_FAILURE;
3280Sstevel@tonic-gate break;
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate if (ctx->ctx_dbg > 4)
3313544Sjbeck sm_dprintf("[%ld] got cmd '%c' len %d\n",
3323544Sjbeck (long) ctx->ctx_id, cmd, (int) len);
3330Sstevel@tonic-gate for (i = 0; i < ncmds; i++)
3340Sstevel@tonic-gate {
3350Sstevel@tonic-gate if (cmd == cmds[i].cm_cmd)
3360Sstevel@tonic-gate break;
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate if (i >= ncmds)
3390Sstevel@tonic-gate {
3400Sstevel@tonic-gate /* unknown command */
3410Sstevel@tonic-gate if (ctx->ctx_dbg > 1)
3423544Sjbeck sm_dprintf("[%ld] cmd '%c' unknown\n",
3433544Sjbeck (long) ctx->ctx_id, cmd);
3440Sstevel@tonic-gate ret = MI_FAILURE;
3450Sstevel@tonic-gate break;
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate if ((f = cmds[i].cm_fct) == NULL)
3480Sstevel@tonic-gate {
3490Sstevel@tonic-gate /* stop for now */
3500Sstevel@tonic-gate if (ctx->ctx_dbg > 1)
3513544Sjbeck sm_dprintf("[%ld] cmd '%c' not impl\n",
3523544Sjbeck (long) ctx->ctx_id, cmd);
3530Sstevel@tonic-gate ret = MI_FAILURE;
3540Sstevel@tonic-gate break;
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate
3570Sstevel@tonic-gate /* is new state ok? */
3580Sstevel@tonic-gate newstate = cmds[i].cm_next;
3590Sstevel@tonic-gate if (ctx->ctx_dbg > 5)
3603544Sjbeck sm_dprintf("[%ld] cur %x new %x nextmask %x\n",
3613544Sjbeck (long) ctx->ctx_id,
3620Sstevel@tonic-gate curstate, newstate, next_states[curstate]);
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate if (newstate != ST_NONE && !trans_ok(curstate, newstate))
3650Sstevel@tonic-gate {
3660Sstevel@tonic-gate if (ctx->ctx_dbg > 1)
3673544Sjbeck sm_dprintf("[%ld] abort: cur %d (%x) new %d (%x) next %x\n",
3683544Sjbeck (long) ctx->ctx_id,
3690Sstevel@tonic-gate curstate, MI_MASK(curstate),
3700Sstevel@tonic-gate newstate, MI_MASK(newstate),
3710Sstevel@tonic-gate next_states[curstate]);
3720Sstevel@tonic-gate
3730Sstevel@tonic-gate /* call abort only if in a mail transaction */
3740Sstevel@tonic-gate if (fi_abort != NULL && call_abort)
3750Sstevel@tonic-gate (void) (*fi_abort)(ctx);
3760Sstevel@tonic-gate
3770Sstevel@tonic-gate /*
3780Sstevel@tonic-gate ** try to reach the new state from HELO
3790Sstevel@tonic-gate ** if it can't be reached, ignore the command.
3800Sstevel@tonic-gate */
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate curstate = ST_HELO;
3830Sstevel@tonic-gate if (!trans_ok(curstate, newstate))
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate if (buf != NULL)
3860Sstevel@tonic-gate {
3870Sstevel@tonic-gate free(buf);
3880Sstevel@tonic-gate buf = NULL;
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate continue;
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate arg.a_len = len;
3940Sstevel@tonic-gate arg.a_buf = buf;
3950Sstevel@tonic-gate if (newstate != ST_NONE)
3960Sstevel@tonic-gate {
3970Sstevel@tonic-gate curstate = newstate;
3980Sstevel@tonic-gate ctx->ctx_state = curstate;
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate arg.a_idx = cmds[i].cm_macros;
4010Sstevel@tonic-gate call_abort = ST_IN_MAIL(curstate);
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate /* call function to deal with command */
4043544Sjbeck MI_MONITOR_BEGIN(ctx, cmd);
4050Sstevel@tonic-gate r = (*f)(&arg);
4063544Sjbeck MI_MONITOR_END(ctx, cmd);
4070Sstevel@tonic-gate if (r != _SMFIS_KEEP && buf != NULL)
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate free(buf);
4100Sstevel@tonic-gate buf = NULL;
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate if (sendreply(r, sd, &timeout, ctx) != MI_SUCCESS)
4130Sstevel@tonic-gate {
4140Sstevel@tonic-gate ret = MI_FAILURE;
4150Sstevel@tonic-gate break;
4160Sstevel@tonic-gate }
4170Sstevel@tonic-gate
4180Sstevel@tonic-gate if (r == SMFIS_ACCEPT)
4190Sstevel@tonic-gate {
4200Sstevel@tonic-gate /* accept mail, no further actions taken */
4210Sstevel@tonic-gate curstate = ST_HELO;
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate else if (r == SMFIS_REJECT || r == SMFIS_DISCARD ||
4240Sstevel@tonic-gate r == SMFIS_TEMPFAIL)
4250Sstevel@tonic-gate {
4260Sstevel@tonic-gate /*
4270Sstevel@tonic-gate ** further actions depend on current state
4280Sstevel@tonic-gate ** if the IGNO bit is set: "ignore" the error,
4290Sstevel@tonic-gate ** i.e., stay in the current state
4300Sstevel@tonic-gate */
4310Sstevel@tonic-gate if (!bitset(CT_IGNO, cmds[i].cm_todo))
4320Sstevel@tonic-gate curstate = ST_HELO;
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate else if (r == _SMFIS_ABORT)
4350Sstevel@tonic-gate {
4360Sstevel@tonic-gate if (ctx->ctx_dbg > 5)
4373544Sjbeck sm_dprintf("[%ld] function returned abort\n",
4383544Sjbeck (long) ctx->ctx_id);
4390Sstevel@tonic-gate ret = MI_FAILURE;
4400Sstevel@tonic-gate break;
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate } while (!bitset(CT_END, cmds[i].cm_todo));
4430Sstevel@tonic-gate
4443544Sjbeck ctx->ctx_state = curstate;
4453544Sjbeck
4463544Sjbeck if (ret == MI_FAILURE)
4470Sstevel@tonic-gate {
4480Sstevel@tonic-gate /* call abort only if in a mail transaction */
4490Sstevel@tonic-gate if (fi_abort != NULL && call_abort)
4500Sstevel@tonic-gate (void) (*fi_abort)(ctx);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate
4533544Sjbeck /* has close been called? */
4543544Sjbeck if (ctx->ctx_state != ST_QUIT
4553544Sjbeck #if _FFR_WORKERS_POOL
4563544Sjbeck && ret != MI_CONTINUE
4573544Sjbeck #endif /* _FFR_WORKERS_POOL */
4583544Sjbeck )
4593544Sjbeck {
4603544Sjbeck if ((fi_close = ctx->ctx_smfi->xxfi_close) != NULL)
4613544Sjbeck (void) (*fi_close)(ctx);
4623544Sjbeck }
4630Sstevel@tonic-gate if (r != _SMFIS_KEEP && buf != NULL)
4640Sstevel@tonic-gate free(buf);
4653544Sjbeck #if !_FFR_WORKERS_POOL
4660Sstevel@tonic-gate mi_clr_macros(ctx, 0);
4673544Sjbeck #endif /* _FFR_WORKERS_POOL */
4680Sstevel@tonic-gate return ret;
4690Sstevel@tonic-gate }
4703544Sjbeck
4713544Sjbeck static size_t milter_addsymlist __P((SMFICTX_PTR, char *, char **));
4723544Sjbeck
4733544Sjbeck static size_t
milter_addsymlist(ctx,buf,newbuf)4743544Sjbeck milter_addsymlist(ctx, buf, newbuf)
4753544Sjbeck SMFICTX_PTR ctx;
4763544Sjbeck char *buf;
4773544Sjbeck char **newbuf;
4783544Sjbeck {
4793544Sjbeck size_t len;
4803544Sjbeck int i;
4813544Sjbeck mi_int32 v;
4823544Sjbeck char *buffer;
4833544Sjbeck
4843544Sjbeck SM_ASSERT(ctx != NULL);
4853544Sjbeck SM_ASSERT(buf != NULL);
4863544Sjbeck SM_ASSERT(newbuf != NULL);
4873544Sjbeck len = 0;
4883544Sjbeck for (i = 0; i < MAX_MACROS_ENTRIES; i++)
4893544Sjbeck {
4903544Sjbeck if (ctx->ctx_mac_list[i] != NULL)
4913544Sjbeck {
4923544Sjbeck len += strlen(ctx->ctx_mac_list[i]) + 1 +
4933544Sjbeck MILTER_LEN_BYTES;
4943544Sjbeck }
4953544Sjbeck }
4963544Sjbeck if (len > 0)
4973544Sjbeck {
4983544Sjbeck size_t offset;
4993544Sjbeck
5003544Sjbeck SM_ASSERT(len + MILTER_OPTLEN > len);
5013544Sjbeck len += MILTER_OPTLEN;
5023544Sjbeck buffer = malloc(len);
5033544Sjbeck if (buffer != NULL)
5043544Sjbeck {
5053544Sjbeck (void) memcpy(buffer, buf, MILTER_OPTLEN);
5063544Sjbeck offset = MILTER_OPTLEN;
5073544Sjbeck for (i = 0; i < MAX_MACROS_ENTRIES; i++)
5083544Sjbeck {
5093544Sjbeck size_t l;
5103544Sjbeck
5113544Sjbeck if (ctx->ctx_mac_list[i] == NULL)
5123544Sjbeck continue;
5133544Sjbeck
5143544Sjbeck SM_ASSERT(offset + MILTER_LEN_BYTES < len);
5153544Sjbeck v = htonl(i);
5163544Sjbeck (void) memcpy(buffer + offset, (void *) &v,
5173544Sjbeck MILTER_LEN_BYTES);
5183544Sjbeck offset += MILTER_LEN_BYTES;
5193544Sjbeck l = strlen(ctx->ctx_mac_list[i]) + 1;
5203544Sjbeck SM_ASSERT(offset + l <= len);
5213544Sjbeck (void) memcpy(buffer + offset,
5223544Sjbeck ctx->ctx_mac_list[i], l);
5233544Sjbeck offset += l;
5243544Sjbeck }
5253544Sjbeck }
5263544Sjbeck else
5273544Sjbeck {
5283544Sjbeck /* oops ... */
5293544Sjbeck }
5303544Sjbeck }
5313544Sjbeck else
5323544Sjbeck {
5333544Sjbeck len = MILTER_OPTLEN;
5343544Sjbeck buffer = buf;
5353544Sjbeck }
5363544Sjbeck *newbuf = buffer;
5373544Sjbeck return len;
5383544Sjbeck }
5393544Sjbeck
5403544Sjbeck /*
5413544Sjbeck ** GET_NR_BIT -- get "no reply" bit matching state
5423544Sjbeck **
5433544Sjbeck ** Parameters:
5443544Sjbeck ** state -- current protocol stage
5453544Sjbeck **
5463544Sjbeck ** Returns:
5473544Sjbeck ** 0: no matching bit
5483544Sjbeck ** >0: the matching "no reply" bit
5493544Sjbeck */
5503544Sjbeck
5513544Sjbeck static unsigned long get_nr_bit __P((int));
5523544Sjbeck
5533544Sjbeck static unsigned long
get_nr_bit(state)5543544Sjbeck get_nr_bit(state)
5553544Sjbeck int state;
5563544Sjbeck {
5573544Sjbeck unsigned long bit;
5583544Sjbeck
5593544Sjbeck switch (state)
5603544Sjbeck {
5613544Sjbeck case ST_CONN:
5623544Sjbeck bit = SMFIP_NR_CONN;
5633544Sjbeck break;
5643544Sjbeck case ST_HELO:
5653544Sjbeck bit = SMFIP_NR_HELO;
5663544Sjbeck break;
5673544Sjbeck case ST_MAIL:
5683544Sjbeck bit = SMFIP_NR_MAIL;
5693544Sjbeck break;
5703544Sjbeck case ST_RCPT:
5713544Sjbeck bit = SMFIP_NR_RCPT;
5723544Sjbeck break;
5733544Sjbeck case ST_DATA:
5743544Sjbeck bit = SMFIP_NR_DATA;
5753544Sjbeck break;
5763544Sjbeck case ST_UNKN:
5773544Sjbeck bit = SMFIP_NR_UNKN;
5783544Sjbeck break;
5793544Sjbeck case ST_HDRS:
5803544Sjbeck bit = SMFIP_NR_HDR;
5813544Sjbeck break;
5823544Sjbeck case ST_EOHS:
5833544Sjbeck bit = SMFIP_NR_EOH;
5843544Sjbeck break;
5853544Sjbeck case ST_BODY:
5863544Sjbeck bit = SMFIP_NR_BODY;
5873544Sjbeck break;
5883544Sjbeck default:
5893544Sjbeck bit = 0;
5903544Sjbeck break;
5913544Sjbeck }
5923544Sjbeck return bit;
5933544Sjbeck }
5943544Sjbeck
5950Sstevel@tonic-gate /*
5960Sstevel@tonic-gate ** SENDREPLY -- send a reply to the MTA
5970Sstevel@tonic-gate **
5980Sstevel@tonic-gate ** Parameters:
5990Sstevel@tonic-gate ** r -- reply code
6000Sstevel@tonic-gate ** sd -- socket descriptor
6010Sstevel@tonic-gate ** timeout_ptr -- (ptr to) timeout to use for sending
6020Sstevel@tonic-gate ** ctx -- context structure
6030Sstevel@tonic-gate **
6040Sstevel@tonic-gate ** Returns:
6050Sstevel@tonic-gate ** MI_SUCCESS/MI_FAILURE
6060Sstevel@tonic-gate */
6070Sstevel@tonic-gate
6080Sstevel@tonic-gate static int
sendreply(r,sd,timeout_ptr,ctx)6090Sstevel@tonic-gate sendreply(r, sd, timeout_ptr, ctx)
6100Sstevel@tonic-gate sfsistat r;
6110Sstevel@tonic-gate socket_t sd;
6120Sstevel@tonic-gate struct timeval *timeout_ptr;
6130Sstevel@tonic-gate SMFICTX_PTR ctx;
6140Sstevel@tonic-gate {
6153544Sjbeck int ret;
6163544Sjbeck unsigned long bit;
6173544Sjbeck
6183544Sjbeck ret = MI_SUCCESS;
6193544Sjbeck
6203544Sjbeck bit = get_nr_bit(ctx->ctx_state);
6213544Sjbeck if (bit != 0 && (ctx->ctx_pflags & bit) != 0 && r != SMFIS_NOREPLY)
6223544Sjbeck {
6233544Sjbeck if (r >= SMFIS_CONTINUE && r < _SMFIS_KEEP)
6243544Sjbeck {
6253544Sjbeck /* milter said it wouldn't reply, but it lied... */
6263544Sjbeck smi_log(SMI_LOG_ERR,
6273544Sjbeck "%s: milter claimed not to reply in state %d but did anyway %d\n",
6283544Sjbeck ctx->ctx_smfi->xxfi_name,
6293544Sjbeck ctx->ctx_state, r);
6303544Sjbeck
6313544Sjbeck }
6323544Sjbeck
6333544Sjbeck /*
6343544Sjbeck ** Force specified behavior, otherwise libmilter
6353544Sjbeck ** and MTA will fail to communicate properly.
6363544Sjbeck */
6373544Sjbeck
6383544Sjbeck switch (r)
6393544Sjbeck {
6403544Sjbeck case SMFIS_CONTINUE:
6413544Sjbeck case SMFIS_TEMPFAIL:
6423544Sjbeck case SMFIS_REJECT:
6433544Sjbeck case SMFIS_DISCARD:
6443544Sjbeck case SMFIS_ACCEPT:
6453544Sjbeck case SMFIS_SKIP:
6463544Sjbeck case _SMFIS_OPTIONS:
6473544Sjbeck r = SMFIS_NOREPLY;
6483544Sjbeck break;
6493544Sjbeck }
6503544Sjbeck }
6510Sstevel@tonic-gate
6520Sstevel@tonic-gate switch (r)
6530Sstevel@tonic-gate {
6540Sstevel@tonic-gate case SMFIS_CONTINUE:
6550Sstevel@tonic-gate ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_CONTINUE, NULL, 0);
6560Sstevel@tonic-gate break;
6570Sstevel@tonic-gate case SMFIS_TEMPFAIL:
6580Sstevel@tonic-gate case SMFIS_REJECT:
6590Sstevel@tonic-gate if (ctx->ctx_reply != NULL &&
6600Sstevel@tonic-gate ((r == SMFIS_TEMPFAIL && *ctx->ctx_reply == '4') ||
6610Sstevel@tonic-gate (r == SMFIS_REJECT && *ctx->ctx_reply == '5')))
6620Sstevel@tonic-gate {
6630Sstevel@tonic-gate ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_REPLYCODE,
6640Sstevel@tonic-gate ctx->ctx_reply,
6650Sstevel@tonic-gate strlen(ctx->ctx_reply) + 1);
6660Sstevel@tonic-gate free(ctx->ctx_reply);
6670Sstevel@tonic-gate ctx->ctx_reply = NULL;
6680Sstevel@tonic-gate }
6690Sstevel@tonic-gate else
6700Sstevel@tonic-gate {
6710Sstevel@tonic-gate ret = mi_wr_cmd(sd, timeout_ptr, r == SMFIS_REJECT ?
6720Sstevel@tonic-gate SMFIR_REJECT : SMFIR_TEMPFAIL, NULL, 0);
6730Sstevel@tonic-gate }
6740Sstevel@tonic-gate break;
6750Sstevel@tonic-gate case SMFIS_DISCARD:
6760Sstevel@tonic-gate ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_DISCARD, NULL, 0);
6770Sstevel@tonic-gate break;
6780Sstevel@tonic-gate case SMFIS_ACCEPT:
6790Sstevel@tonic-gate ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_ACCEPT, NULL, 0);
6800Sstevel@tonic-gate break;
6813544Sjbeck case SMFIS_SKIP:
6823544Sjbeck ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_SKIP, NULL, 0);
6833544Sjbeck break;
6840Sstevel@tonic-gate case _SMFIS_OPTIONS:
6850Sstevel@tonic-gate {
6863544Sjbeck mi_int32 v;
6873544Sjbeck size_t len;
6883544Sjbeck char *buffer;
6890Sstevel@tonic-gate char buf[MILTER_OPTLEN];
6900Sstevel@tonic-gate
6913544Sjbeck v = htonl(ctx->ctx_prot_vers2mta);
6923544Sjbeck (void) memcpy(&(buf[0]), (void *) &v,
6933544Sjbeck MILTER_LEN_BYTES);
6943544Sjbeck v = htonl(ctx->ctx_aflags);
6950Sstevel@tonic-gate (void) memcpy(&(buf[MILTER_LEN_BYTES]), (void *) &v,
6960Sstevel@tonic-gate MILTER_LEN_BYTES);
6973544Sjbeck v = htonl(ctx->ctx_pflags2mta);
6983544Sjbeck (void) memcpy(&(buf[MILTER_LEN_BYTES * 2]),
6993544Sjbeck (void *) &v, MILTER_LEN_BYTES);
7003544Sjbeck len = milter_addsymlist(ctx, buf, &buffer);
7013544Sjbeck if (buffer != NULL)
7023544Sjbeck ret = mi_wr_cmd(sd, timeout_ptr, SMFIC_OPTNEG,
7033544Sjbeck buffer, len);
7043544Sjbeck else
7053544Sjbeck ret = MI_FAILURE;
7063544Sjbeck }
7073544Sjbeck break;
7083544Sjbeck case SMFIS_NOREPLY:
7093544Sjbeck if (bit != 0 &&
7103544Sjbeck (ctx->ctx_pflags & bit) != 0 &&
7113544Sjbeck (ctx->ctx_mta_pflags & bit) == 0)
7123544Sjbeck {
7133544Sjbeck /*
7143544Sjbeck ** milter doesn't want to send a reply,
7153544Sjbeck ** but the MTA doesn't have that feature: fake it.
7163544Sjbeck */
7173544Sjbeck
7183544Sjbeck ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_CONTINUE, NULL,
7193544Sjbeck 0);
7200Sstevel@tonic-gate }
7210Sstevel@tonic-gate break;
7220Sstevel@tonic-gate default: /* don't send a reply */
7230Sstevel@tonic-gate break;
7240Sstevel@tonic-gate }
7250Sstevel@tonic-gate return ret;
7260Sstevel@tonic-gate }
7270Sstevel@tonic-gate
7280Sstevel@tonic-gate /*
7290Sstevel@tonic-gate ** CLR_MACROS -- clear set of macros starting from a given index
7300Sstevel@tonic-gate **
7310Sstevel@tonic-gate ** Parameters:
7320Sstevel@tonic-gate ** ctx -- context structure
7330Sstevel@tonic-gate ** m -- index from which to clear all macros
7340Sstevel@tonic-gate **
7350Sstevel@tonic-gate ** Returns:
7360Sstevel@tonic-gate ** None.
7370Sstevel@tonic-gate */
7383966Sjbeck
7390Sstevel@tonic-gate void
mi_clr_macros(ctx,m)7400Sstevel@tonic-gate mi_clr_macros(ctx, m)
7410Sstevel@tonic-gate SMFICTX_PTR ctx;
7420Sstevel@tonic-gate int m;
7430Sstevel@tonic-gate {
7440Sstevel@tonic-gate int i;
7450Sstevel@tonic-gate
7460Sstevel@tonic-gate for (i = m; i < MAX_MACROS_ENTRIES; i++)
7470Sstevel@tonic-gate {
7480Sstevel@tonic-gate if (ctx->ctx_mac_ptr[i] != NULL)
7490Sstevel@tonic-gate {
7500Sstevel@tonic-gate free(ctx->ctx_mac_ptr[i]);
7510Sstevel@tonic-gate ctx->ctx_mac_ptr[i] = NULL;
7520Sstevel@tonic-gate }
7530Sstevel@tonic-gate if (ctx->ctx_mac_buf[i] != NULL)
7540Sstevel@tonic-gate {
7550Sstevel@tonic-gate free(ctx->ctx_mac_buf[i]);
7560Sstevel@tonic-gate ctx->ctx_mac_buf[i] = NULL;
7570Sstevel@tonic-gate }
7580Sstevel@tonic-gate }
7590Sstevel@tonic-gate }
7603544Sjbeck
7610Sstevel@tonic-gate /*
762*11440SJohn.Beck@Sun.COM ** MI_CLR_SYMLIST -- clear list of macros
763*11440SJohn.Beck@Sun.COM **
764*11440SJohn.Beck@Sun.COM ** Parameters:
765*11440SJohn.Beck@Sun.COM ** ctx -- context structure
766*11440SJohn.Beck@Sun.COM **
767*11440SJohn.Beck@Sun.COM ** Returns:
768*11440SJohn.Beck@Sun.COM ** None.
769*11440SJohn.Beck@Sun.COM */
770*11440SJohn.Beck@Sun.COM
771*11440SJohn.Beck@Sun.COM static void
mi_clr_symlist(ctx)772*11440SJohn.Beck@Sun.COM mi_clr_symlist(ctx)
773*11440SJohn.Beck@Sun.COM SMFICTX *ctx;
774*11440SJohn.Beck@Sun.COM {
775*11440SJohn.Beck@Sun.COM int i;
776*11440SJohn.Beck@Sun.COM
777*11440SJohn.Beck@Sun.COM SM_ASSERT(ctx != NULL);
778*11440SJohn.Beck@Sun.COM for (i = SMFIM_FIRST; i <= SMFIM_LAST; i++)
779*11440SJohn.Beck@Sun.COM {
780*11440SJohn.Beck@Sun.COM if (ctx->ctx_mac_list[i] != NULL)
781*11440SJohn.Beck@Sun.COM {
782*11440SJohn.Beck@Sun.COM free(ctx->ctx_mac_list[i]);
783*11440SJohn.Beck@Sun.COM ctx->ctx_mac_list[i] = NULL;
784*11440SJohn.Beck@Sun.COM }
785*11440SJohn.Beck@Sun.COM }
786*11440SJohn.Beck@Sun.COM }
787*11440SJohn.Beck@Sun.COM
788*11440SJohn.Beck@Sun.COM /*
789*11440SJohn.Beck@Sun.COM ** MI_CLR_CTX -- clear context
790*11440SJohn.Beck@Sun.COM **
791*11440SJohn.Beck@Sun.COM ** Parameters:
792*11440SJohn.Beck@Sun.COM ** ctx -- context structure
793*11440SJohn.Beck@Sun.COM **
794*11440SJohn.Beck@Sun.COM ** Returns:
795*11440SJohn.Beck@Sun.COM ** None.
796*11440SJohn.Beck@Sun.COM */
797*11440SJohn.Beck@Sun.COM
798*11440SJohn.Beck@Sun.COM void
mi_clr_ctx(ctx)799*11440SJohn.Beck@Sun.COM mi_clr_ctx(ctx)
800*11440SJohn.Beck@Sun.COM SMFICTX *ctx;
801*11440SJohn.Beck@Sun.COM {
802*11440SJohn.Beck@Sun.COM SM_ASSERT(ctx != NULL);
803*11440SJohn.Beck@Sun.COM if (ValidSocket(ctx->ctx_sd))
804*11440SJohn.Beck@Sun.COM {
805*11440SJohn.Beck@Sun.COM (void) closesocket(ctx->ctx_sd);
806*11440SJohn.Beck@Sun.COM ctx->ctx_sd = INVALID_SOCKET;
807*11440SJohn.Beck@Sun.COM }
808*11440SJohn.Beck@Sun.COM if (ctx->ctx_reply != NULL)
809*11440SJohn.Beck@Sun.COM {
810*11440SJohn.Beck@Sun.COM free(ctx->ctx_reply);
811*11440SJohn.Beck@Sun.COM ctx->ctx_reply = NULL;
812*11440SJohn.Beck@Sun.COM }
813*11440SJohn.Beck@Sun.COM if (ctx->ctx_privdata != NULL)
814*11440SJohn.Beck@Sun.COM {
815*11440SJohn.Beck@Sun.COM smi_log(SMI_LOG_WARN,
816*11440SJohn.Beck@Sun.COM "%s: private data not NULL",
817*11440SJohn.Beck@Sun.COM ctx->ctx_smfi->xxfi_name);
818*11440SJohn.Beck@Sun.COM }
819*11440SJohn.Beck@Sun.COM mi_clr_macros(ctx, 0);
820*11440SJohn.Beck@Sun.COM mi_clr_symlist(ctx);
821*11440SJohn.Beck@Sun.COM free(ctx);
822*11440SJohn.Beck@Sun.COM }
823*11440SJohn.Beck@Sun.COM
824*11440SJohn.Beck@Sun.COM /*
8250Sstevel@tonic-gate ** ST_OPTIONNEG -- negotiate options
8260Sstevel@tonic-gate **
8270Sstevel@tonic-gate ** Parameters:
8280Sstevel@tonic-gate ** g -- generic argument structure
8290Sstevel@tonic-gate **
8300Sstevel@tonic-gate ** Returns:
8310Sstevel@tonic-gate ** abort/send options/continue
8320Sstevel@tonic-gate */
8330Sstevel@tonic-gate
8340Sstevel@tonic-gate static int
st_optionneg(g)8350Sstevel@tonic-gate st_optionneg(g)
8360Sstevel@tonic-gate genarg *g;
8370Sstevel@tonic-gate {
838*11440SJohn.Beck@Sun.COM mi_int32 i, v, fake_pflags, internal_pflags;
8393544Sjbeck SMFICTX_PTR ctx;
840*11440SJohn.Beck@Sun.COM #if _FFR_MILTER_CHECK
841*11440SJohn.Beck@Sun.COM bool testmode = false;
842*11440SJohn.Beck@Sun.COM #endif /* _FFR_MILTER_CHECK */
8433544Sjbeck int (*fi_negotiate) __P((SMFICTX *,
8443544Sjbeck unsigned long, unsigned long,
8453544Sjbeck unsigned long, unsigned long,
8463544Sjbeck unsigned long *, unsigned long *,
8473544Sjbeck unsigned long *, unsigned long *));
8480Sstevel@tonic-gate
8490Sstevel@tonic-gate if (g == NULL || g->a_ctx->ctx_smfi == NULL)
8500Sstevel@tonic-gate return SMFIS_CONTINUE;
8513544Sjbeck ctx = g->a_ctx;
8523544Sjbeck mi_clr_macros(ctx, g->a_idx + 1);
8533544Sjbeck ctx->ctx_prot_vers = SMFI_PROT_VERSION;
8540Sstevel@tonic-gate
8550Sstevel@tonic-gate /* check for minimum length */
8560Sstevel@tonic-gate if (g->a_len < MILTER_OPTLEN)
8570Sstevel@tonic-gate {
8580Sstevel@tonic-gate smi_log(SMI_LOG_ERR,
8593544Sjbeck "%s: st_optionneg[%ld]: len too short %d < %d",
8603544Sjbeck ctx->ctx_smfi->xxfi_name,
8613544Sjbeck (long) ctx->ctx_id, (int) g->a_len,
8620Sstevel@tonic-gate MILTER_OPTLEN);
8630Sstevel@tonic-gate return _SMFIS_ABORT;
8640Sstevel@tonic-gate }
8650Sstevel@tonic-gate
8663544Sjbeck /* protocol version */
8673544Sjbeck (void) memcpy((void *) &i, (void *) &(g->a_buf[0]), MILTER_LEN_BYTES);
8680Sstevel@tonic-gate v = ntohl(i);
8693544Sjbeck
8703544Sjbeck #define SMFI_PROT_VERSION_MIN 2
8713544Sjbeck
8723544Sjbeck /* check for minimum version */
8733544Sjbeck if (v < SMFI_PROT_VERSION_MIN)
8740Sstevel@tonic-gate {
8750Sstevel@tonic-gate smi_log(SMI_LOG_ERR,
8763544Sjbeck "%s: st_optionneg[%ld]: protocol version too old %d < %d",
8773544Sjbeck ctx->ctx_smfi->xxfi_name,
8783544Sjbeck (long) ctx->ctx_id, v, SMFI_PROT_VERSION_MIN);
8790Sstevel@tonic-gate return _SMFIS_ABORT;
8800Sstevel@tonic-gate }
8813544Sjbeck ctx->ctx_mta_prot_vers = v;
8823544Sjbeck if (ctx->ctx_prot_vers < ctx->ctx_mta_prot_vers)
8833544Sjbeck ctx->ctx_prot_vers2mta = ctx->ctx_prot_vers;
8843544Sjbeck else
8853544Sjbeck ctx->ctx_prot_vers2mta = ctx->ctx_mta_prot_vers;
8860Sstevel@tonic-gate
8870Sstevel@tonic-gate (void) memcpy((void *) &i, (void *) &(g->a_buf[MILTER_LEN_BYTES]),
8880Sstevel@tonic-gate MILTER_LEN_BYTES);
8890Sstevel@tonic-gate v = ntohl(i);
8900Sstevel@tonic-gate
8910Sstevel@tonic-gate /* no flags? set to default value for V1 actions */
8920Sstevel@tonic-gate if (v == 0)
8930Sstevel@tonic-gate v = SMFI_V1_ACTS;
8943544Sjbeck ctx->ctx_mta_aflags = v; /* MTA action flags */
8950Sstevel@tonic-gate
896*11440SJohn.Beck@Sun.COM internal_pflags = 0;
8970Sstevel@tonic-gate (void) memcpy((void *) &i, (void *) &(g->a_buf[MILTER_LEN_BYTES * 2]),
8980Sstevel@tonic-gate MILTER_LEN_BYTES);
8990Sstevel@tonic-gate v = ntohl(i);
9000Sstevel@tonic-gate
9010Sstevel@tonic-gate /* no flags? set to default value for V1 protocol */
9020Sstevel@tonic-gate if (v == 0)
9030Sstevel@tonic-gate v = SMFI_V1_PROT;
904*11440SJohn.Beck@Sun.COM #if _FFR_MDS_NEGOTIATE
905*11440SJohn.Beck@Sun.COM else if (ctx->ctx_smfi->xxfi_version >= SMFI_VERSION_MDS)
906*11440SJohn.Beck@Sun.COM {
907*11440SJohn.Beck@Sun.COM /*
908*11440SJohn.Beck@Sun.COM ** Allow changing the size only if milter is compiled
909*11440SJohn.Beck@Sun.COM ** against a version that supports this.
910*11440SJohn.Beck@Sun.COM ** If a milter is dynamically linked against a newer
911*11440SJohn.Beck@Sun.COM ** libmilter version, we don't want to "surprise"
912*11440SJohn.Beck@Sun.COM ** it with a larger buffer as it may rely on it
913*11440SJohn.Beck@Sun.COM ** even though it is not documented as a limit.
914*11440SJohn.Beck@Sun.COM */
915*11440SJohn.Beck@Sun.COM
916*11440SJohn.Beck@Sun.COM if (bitset(SMFIP_MDS_1M, v))
917*11440SJohn.Beck@Sun.COM {
918*11440SJohn.Beck@Sun.COM internal_pflags |= SMFIP_MDS_1M;
919*11440SJohn.Beck@Sun.COM (void) smfi_setmaxdatasize(MILTER_MDS_1M);
920*11440SJohn.Beck@Sun.COM }
921*11440SJohn.Beck@Sun.COM else if (bitset(SMFIP_MDS_256K, v))
922*11440SJohn.Beck@Sun.COM {
923*11440SJohn.Beck@Sun.COM internal_pflags |= SMFIP_MDS_256K;
924*11440SJohn.Beck@Sun.COM (void) smfi_setmaxdatasize(MILTER_MDS_256K);
925*11440SJohn.Beck@Sun.COM }
926*11440SJohn.Beck@Sun.COM }
927*11440SJohn.Beck@Sun.COM # if 0
928*11440SJohn.Beck@Sun.COM /* don't log this for now... */
929*11440SJohn.Beck@Sun.COM else if (ctx->ctx_smfi->xxfi_version < SMFI_VERSION_MDS &&
930*11440SJohn.Beck@Sun.COM bitset(SMFIP_MDS_1M|SMFIP_MDS_256K, v))
931*11440SJohn.Beck@Sun.COM {
932*11440SJohn.Beck@Sun.COM smi_log(SMI_LOG_WARN,
933*11440SJohn.Beck@Sun.COM "%s: st_optionneg[%ld]: milter version=%X, trying flags=%X",
934*11440SJohn.Beck@Sun.COM ctx->ctx_smfi->xxfi_name,
935*11440SJohn.Beck@Sun.COM (long) ctx->ctx_id, ctx->ctx_smfi->xxfi_version, v);
936*11440SJohn.Beck@Sun.COM }
937*11440SJohn.Beck@Sun.COM # endif /* 0 */
938*11440SJohn.Beck@Sun.COM #endif /* _FFR_MDS_NEGOTIATE */
939*11440SJohn.Beck@Sun.COM
940*11440SJohn.Beck@Sun.COM /*
941*11440SJohn.Beck@Sun.COM ** MTA protocol flags.
942*11440SJohn.Beck@Sun.COM ** We pass the internal flags to the milter as "read only",
943*11440SJohn.Beck@Sun.COM ** i.e., a milter can read them so it knows which size
944*11440SJohn.Beck@Sun.COM ** will be used, but any changes by a milter will be ignored
945*11440SJohn.Beck@Sun.COM ** (see below, search for SMFI_INTERNAL).
946*11440SJohn.Beck@Sun.COM */
947*11440SJohn.Beck@Sun.COM
948*11440SJohn.Beck@Sun.COM ctx->ctx_mta_pflags = (v & ~SMFI_INTERNAL) | internal_pflags;
9493544Sjbeck
9503544Sjbeck /*
9513544Sjbeck ** Copy flags from milter struct into libmilter context;
9523544Sjbeck ** this variable will be used later on to check whether
9533544Sjbeck ** the MTA "actions" can fulfill the milter requirements,
9543544Sjbeck ** but it may be overwritten by the negotiate callback.
9553544Sjbeck */
9563544Sjbeck
9573544Sjbeck ctx->ctx_aflags = ctx->ctx_smfi->xxfi_flags;
9583544Sjbeck fake_pflags = SMFIP_NR_CONN
9593544Sjbeck |SMFIP_NR_HELO
9603544Sjbeck |SMFIP_NR_MAIL
9613544Sjbeck |SMFIP_NR_RCPT
9623544Sjbeck |SMFIP_NR_DATA
9633544Sjbeck |SMFIP_NR_UNKN
9643544Sjbeck |SMFIP_NR_HDR
9653544Sjbeck |SMFIP_NR_EOH
9663544Sjbeck |SMFIP_NR_BODY
9673544Sjbeck ;
9683544Sjbeck
9693544Sjbeck if (g->a_ctx->ctx_smfi != NULL &&
9704518Sjbeck g->a_ctx->ctx_smfi->xxfi_version > 4 &&
9713544Sjbeck (fi_negotiate = g->a_ctx->ctx_smfi->xxfi_negotiate) != NULL)
9723544Sjbeck {
9733544Sjbeck int r;
9743544Sjbeck unsigned long m_aflags, m_pflags, m_f2, m_f3;
9753544Sjbeck
9763544Sjbeck /*
9773544Sjbeck ** let milter decide whether the features offered by the
9783544Sjbeck ** MTA are "good enough".
9793544Sjbeck ** Notes:
9803544Sjbeck ** - libmilter can "fake" some features (e.g., SMFIP_NR_HDR)
9813544Sjbeck ** - m_f2, m_f3 are for future extensions
9823544Sjbeck */
9833544Sjbeck
9843544Sjbeck m_f2 = m_f3 = 0;
9853544Sjbeck m_aflags = ctx->ctx_mta_aflags;
9863544Sjbeck m_pflags = ctx->ctx_pflags;
9873544Sjbeck if ((SMFIP_SKIP & ctx->ctx_mta_pflags) != 0)
9883544Sjbeck m_pflags |= SMFIP_SKIP;
9893544Sjbeck r = fi_negotiate(g->a_ctx,
9903544Sjbeck ctx->ctx_mta_aflags,
9913544Sjbeck ctx->ctx_mta_pflags|fake_pflags,
9923544Sjbeck 0, 0,
9933544Sjbeck &m_aflags, &m_pflags, &m_f2, &m_f3);
9943544Sjbeck
995*11440SJohn.Beck@Sun.COM #if _FFR_MILTER_CHECK
996*11440SJohn.Beck@Sun.COM testmode = bitset(SMFIP_TEST, m_pflags);
997*11440SJohn.Beck@Sun.COM if (testmode)
998*11440SJohn.Beck@Sun.COM m_pflags &= ~SMFIP_TEST;
999*11440SJohn.Beck@Sun.COM #endif /* _FFR_MILTER_CHECK */
1000*11440SJohn.Beck@Sun.COM
10013544Sjbeck /*
10023544Sjbeck ** Types of protocol flags (pflags):
10033544Sjbeck ** 1. do NOT send protocol step X
10043544Sjbeck ** 2. MTA can do/understand something extra (SKIP,
10053544Sjbeck ** send unknown RCPTs)
10063544Sjbeck ** 3. MTA can deal with "no reply" for various protocol steps
10073544Sjbeck ** Note: this mean that it isn't possible to simply set all
10083544Sjbeck ** flags to get "everything":
10093544Sjbeck ** setting a flag of type 1 turns off a step
10103544Sjbeck ** (it should be the other way around:
10113544Sjbeck ** a flag means a protocol step can be sent)
10123544Sjbeck ** setting a flag of type 3 requires that milter
10133544Sjbeck ** never sends a reply for the corresponding step.
10143544Sjbeck ** Summary: the "negation" of protocol flags is causing
10153544Sjbeck ** problems, but at least for type 3 there is no simple
10163544Sjbeck ** solution.
10173544Sjbeck **
10183544Sjbeck ** What should "all options" mean?
10193544Sjbeck ** send all protocol steps _except_ those for which there is
10203544Sjbeck ** no callback (currently registered in ctx_pflags)
10213544Sjbeck ** expect SKIP as return code? Yes
10223544Sjbeck ** send unknown RCPTs? No,
10233544Sjbeck ** must be explicitly requested?
10243544Sjbeck ** "no reply" for some protocol steps? No,
10253544Sjbeck ** must be explicitly requested.
10263544Sjbeck */
10273544Sjbeck
10283544Sjbeck if (SMFIS_ALL_OPTS == r)
10293544Sjbeck {
10303544Sjbeck ctx->ctx_aflags = ctx->ctx_mta_aflags;
10313544Sjbeck ctx->ctx_pflags2mta = ctx->ctx_pflags;
10323544Sjbeck if ((SMFIP_SKIP & ctx->ctx_mta_pflags) != 0)
10333544Sjbeck ctx->ctx_pflags2mta |= SMFIP_SKIP;
10343544Sjbeck }
10353544Sjbeck else if (r != SMFIS_CONTINUE)
10363544Sjbeck {
10373544Sjbeck smi_log(SMI_LOG_ERR,
10383544Sjbeck "%s: st_optionneg[%ld]: xxfi_negotiate returned %d (protocol options=0x%lx, actions=0x%lx)",
10393544Sjbeck ctx->ctx_smfi->xxfi_name,
10403544Sjbeck (long) ctx->ctx_id, r, ctx->ctx_mta_pflags,
10413544Sjbeck ctx->ctx_mta_aflags);
10423544Sjbeck return _SMFIS_ABORT;
10433544Sjbeck }
10443544Sjbeck else
10453544Sjbeck {
10463544Sjbeck ctx->ctx_aflags = m_aflags;
10473544Sjbeck ctx->ctx_pflags = m_pflags;
10483544Sjbeck ctx->ctx_pflags2mta = m_pflags;
10493544Sjbeck }
10503544Sjbeck
10513544Sjbeck /* check whether some flags need to be "faked" */
10523544Sjbeck i = ctx->ctx_pflags2mta;
10533544Sjbeck if ((ctx->ctx_mta_pflags & i) != i)
10543544Sjbeck {
10553544Sjbeck unsigned int idx;
10563544Sjbeck unsigned long b;
10573544Sjbeck
10583544Sjbeck /*
10593544Sjbeck ** If some behavior can be faked (set in fake_pflags),
10603544Sjbeck ** but the MTA doesn't support it, then unset
10613544Sjbeck ** that flag in the value that is sent to the MTA.
10623544Sjbeck */
10633544Sjbeck
10643544Sjbeck for (idx = 0; idx < 32; idx++)
10653544Sjbeck {
10663544Sjbeck b = 1 << idx;
10673544Sjbeck if ((ctx->ctx_mta_pflags & b) != b &&
10683544Sjbeck (fake_pflags & b) == b)
10693544Sjbeck ctx->ctx_pflags2mta &= ~b;
10703544Sjbeck }
10713544Sjbeck }
10723544Sjbeck }
10733544Sjbeck else
10743544Sjbeck {
10753544Sjbeck /*
10763544Sjbeck ** Set the protocol flags based on the values determined
10773544Sjbeck ** in mi_listener() which checked the defined callbacks.
10783544Sjbeck */
10793544Sjbeck
10803544Sjbeck ctx->ctx_pflags2mta = ctx->ctx_pflags;
10813544Sjbeck }
10823544Sjbeck
10833544Sjbeck /* check whether actions and protocol requirements can be satisfied */
10843544Sjbeck i = ctx->ctx_aflags;
10853544Sjbeck if ((i & ctx->ctx_mta_aflags) != i)
10860Sstevel@tonic-gate {
10870Sstevel@tonic-gate smi_log(SMI_LOG_ERR,
10883544Sjbeck "%s: st_optionneg[%ld]: 0x%lx does not fulfill action requirements 0x%x",
10893544Sjbeck ctx->ctx_smfi->xxfi_name,
10903544Sjbeck (long) ctx->ctx_id, ctx->ctx_mta_aflags, i);
10910Sstevel@tonic-gate return _SMFIS_ABORT;
10920Sstevel@tonic-gate }
10930Sstevel@tonic-gate
10943544Sjbeck i = ctx->ctx_pflags2mta;
10953544Sjbeck if ((ctx->ctx_mta_pflags & i) != i)
10963544Sjbeck {
10973544Sjbeck /*
10983544Sjbeck ** Older MTAs do not support some protocol steps.
10993544Sjbeck ** As this protocol is a bit "wierd" (it asks for steps
11003544Sjbeck ** NOT to be taken/sent) we have to check whether we
11013544Sjbeck ** should turn off those "negative" requests.
11023544Sjbeck ** Currently these are only SMFIP_NODATA and SMFIP_NOUNKNOWN.
11033544Sjbeck */
11043544Sjbeck
11053544Sjbeck if (bitset(SMFIP_NODATA, ctx->ctx_pflags2mta) &&
11063544Sjbeck !bitset(SMFIP_NODATA, ctx->ctx_mta_pflags))
11073544Sjbeck ctx->ctx_pflags2mta &= ~SMFIP_NODATA;
11083544Sjbeck if (bitset(SMFIP_NOUNKNOWN, ctx->ctx_pflags2mta) &&
11093544Sjbeck !bitset(SMFIP_NOUNKNOWN, ctx->ctx_mta_pflags))
11103544Sjbeck ctx->ctx_pflags2mta &= ~SMFIP_NOUNKNOWN;
11113544Sjbeck i = ctx->ctx_pflags2mta;
11123544Sjbeck }
11133544Sjbeck
11143544Sjbeck if ((ctx->ctx_mta_pflags & i) != i)
11153544Sjbeck {
11163544Sjbeck smi_log(SMI_LOG_ERR,
11173544Sjbeck "%s: st_optionneg[%ld]: 0x%lx does not fulfill protocol requirements 0x%x",
11183544Sjbeck ctx->ctx_smfi->xxfi_name,
11193544Sjbeck (long) ctx->ctx_id, ctx->ctx_mta_pflags, i);
11203544Sjbeck return _SMFIS_ABORT;
11213544Sjbeck }
11226562Sjbeck fix_stm(ctx);
11233544Sjbeck
11243544Sjbeck if (ctx->ctx_dbg > 3)
11253544Sjbeck sm_dprintf("[%ld] milter_negotiate:"
11263544Sjbeck " mta_actions=0x%lx, mta_flags=0x%lx"
11273544Sjbeck " actions=0x%lx, flags=0x%lx\n"
11283544Sjbeck , (long) ctx->ctx_id
11293544Sjbeck , ctx->ctx_mta_aflags, ctx->ctx_mta_pflags
11303544Sjbeck , ctx->ctx_aflags, ctx->ctx_pflags);
11313544Sjbeck
1132*11440SJohn.Beck@Sun.COM #if _FFR_MILTER_CHECK
1133*11440SJohn.Beck@Sun.COM if (ctx->ctx_dbg > 3)
1134*11440SJohn.Beck@Sun.COM sm_dprintf("[%ld] milter_negotiate:"
1135*11440SJohn.Beck@Sun.COM " testmode=%d, pflags2mta=%X, internal_pflags=%X\n"
1136*11440SJohn.Beck@Sun.COM , (long) ctx->ctx_id, testmode
1137*11440SJohn.Beck@Sun.COM , ctx->ctx_pflags2mta, internal_pflags);
1138*11440SJohn.Beck@Sun.COM
1139*11440SJohn.Beck@Sun.COM /* in test mode: take flags without further modifications */
1140*11440SJohn.Beck@Sun.COM if (!testmode)
1141*11440SJohn.Beck@Sun.COM /* Warning: check statement below! */
1142*11440SJohn.Beck@Sun.COM #endif /* _FFR_MILTER_CHECK */
1143*11440SJohn.Beck@Sun.COM
1144*11440SJohn.Beck@Sun.COM /*
1145*11440SJohn.Beck@Sun.COM ** Remove the internal flags that might have been set by a milter
1146*11440SJohn.Beck@Sun.COM ** and set only those determined above.
1147*11440SJohn.Beck@Sun.COM */
1148*11440SJohn.Beck@Sun.COM
1149*11440SJohn.Beck@Sun.COM ctx->ctx_pflags2mta = (ctx->ctx_pflags2mta & ~SMFI_INTERNAL)
1150*11440SJohn.Beck@Sun.COM | internal_pflags;
11510Sstevel@tonic-gate return _SMFIS_OPTIONS;
11520Sstevel@tonic-gate }
11533544Sjbeck
11540Sstevel@tonic-gate /*
11550Sstevel@tonic-gate ** ST_CONNECTINFO -- receive connection information
11560Sstevel@tonic-gate **
11570Sstevel@tonic-gate ** Parameters:
11580Sstevel@tonic-gate ** g -- generic argument structure
11590Sstevel@tonic-gate **
11600Sstevel@tonic-gate ** Returns:
11610Sstevel@tonic-gate ** continue or filter-specified value
11620Sstevel@tonic-gate */
11630Sstevel@tonic-gate
11640Sstevel@tonic-gate static int
st_connectinfo(g)11650Sstevel@tonic-gate st_connectinfo(g)
11660Sstevel@tonic-gate genarg *g;
11670Sstevel@tonic-gate {
11680Sstevel@tonic-gate size_t l;
11690Sstevel@tonic-gate size_t i;
11700Sstevel@tonic-gate char *s, family;
11710Sstevel@tonic-gate unsigned short port = 0;
11720Sstevel@tonic-gate _SOCK_ADDR sockaddr;
11730Sstevel@tonic-gate sfsistat (*fi_connect) __P((SMFICTX *, char *, _SOCK_ADDR *));
11740Sstevel@tonic-gate
11750Sstevel@tonic-gate if (g == NULL)
11760Sstevel@tonic-gate return _SMFIS_ABORT;
11770Sstevel@tonic-gate mi_clr_macros(g->a_ctx, g->a_idx + 1);
11780Sstevel@tonic-gate if (g->a_ctx->ctx_smfi == NULL ||
11790Sstevel@tonic-gate (fi_connect = g->a_ctx->ctx_smfi->xxfi_connect) == NULL)
11800Sstevel@tonic-gate return SMFIS_CONTINUE;
11810Sstevel@tonic-gate
11820Sstevel@tonic-gate s = g->a_buf;
11830Sstevel@tonic-gate i = 0;
11840Sstevel@tonic-gate l = g->a_len;
11850Sstevel@tonic-gate while (s[i] != '\0' && i <= l)
11860Sstevel@tonic-gate ++i;
11870Sstevel@tonic-gate if (i + 1 >= l)
11880Sstevel@tonic-gate return _SMFIS_ABORT;
11890Sstevel@tonic-gate
11900Sstevel@tonic-gate /* Move past trailing \0 in host string */
11910Sstevel@tonic-gate i++;
11920Sstevel@tonic-gate family = s[i++];
11930Sstevel@tonic-gate (void) memset(&sockaddr, '\0', sizeof sockaddr);
11940Sstevel@tonic-gate if (family != SMFIA_UNKNOWN)
11950Sstevel@tonic-gate {
11960Sstevel@tonic-gate if (i + sizeof port >= l)
11970Sstevel@tonic-gate {
11980Sstevel@tonic-gate smi_log(SMI_LOG_ERR,
11993544Sjbeck "%s: connect[%ld]: wrong len %d >= %d",
12000Sstevel@tonic-gate g->a_ctx->ctx_smfi->xxfi_name,
12013544Sjbeck (long) g->a_ctx->ctx_id, (int) i, (int) l);
12020Sstevel@tonic-gate return _SMFIS_ABORT;
12030Sstevel@tonic-gate }
12040Sstevel@tonic-gate (void) memcpy((void *) &port, (void *) (s + i),
12050Sstevel@tonic-gate sizeof port);
12060Sstevel@tonic-gate i += sizeof port;
12070Sstevel@tonic-gate
12080Sstevel@tonic-gate /* make sure string is terminated */
12090Sstevel@tonic-gate if (s[l - 1] != '\0')
12100Sstevel@tonic-gate return _SMFIS_ABORT;
12110Sstevel@tonic-gate # if NETINET
12120Sstevel@tonic-gate if (family == SMFIA_INET)
12130Sstevel@tonic-gate {
12140Sstevel@tonic-gate if (inet_aton(s + i, (struct in_addr *) &sockaddr.sin.sin_addr)
12150Sstevel@tonic-gate != 1)
12160Sstevel@tonic-gate {
12170Sstevel@tonic-gate smi_log(SMI_LOG_ERR,
12183544Sjbeck "%s: connect[%ld]: inet_aton failed",
12190Sstevel@tonic-gate g->a_ctx->ctx_smfi->xxfi_name,
12203544Sjbeck (long) g->a_ctx->ctx_id);
12210Sstevel@tonic-gate return _SMFIS_ABORT;
12220Sstevel@tonic-gate }
12230Sstevel@tonic-gate sockaddr.sa.sa_family = AF_INET;
12240Sstevel@tonic-gate if (port > 0)
12250Sstevel@tonic-gate sockaddr.sin.sin_port = port;
12260Sstevel@tonic-gate }
12270Sstevel@tonic-gate else
12280Sstevel@tonic-gate # endif /* NETINET */
12290Sstevel@tonic-gate # if NETINET6
12300Sstevel@tonic-gate if (family == SMFIA_INET6)
12310Sstevel@tonic-gate {
12320Sstevel@tonic-gate if (mi_inet_pton(AF_INET6, s + i,
12330Sstevel@tonic-gate &sockaddr.sin6.sin6_addr) != 1)
12340Sstevel@tonic-gate {
12350Sstevel@tonic-gate smi_log(SMI_LOG_ERR,
12363544Sjbeck "%s: connect[%ld]: mi_inet_pton failed",
12370Sstevel@tonic-gate g->a_ctx->ctx_smfi->xxfi_name,
12383544Sjbeck (long) g->a_ctx->ctx_id);
12390Sstevel@tonic-gate return _SMFIS_ABORT;
12400Sstevel@tonic-gate }
12410Sstevel@tonic-gate sockaddr.sa.sa_family = AF_INET6;
12420Sstevel@tonic-gate if (port > 0)
12430Sstevel@tonic-gate sockaddr.sin6.sin6_port = port;
12440Sstevel@tonic-gate }
12450Sstevel@tonic-gate else
12460Sstevel@tonic-gate # endif /* NETINET6 */
12470Sstevel@tonic-gate # if NETUNIX
12480Sstevel@tonic-gate if (family == SMFIA_UNIX)
12490Sstevel@tonic-gate {
12500Sstevel@tonic-gate if (sm_strlcpy(sockaddr.sunix.sun_path, s + i,
12510Sstevel@tonic-gate sizeof sockaddr.sunix.sun_path) >=
12520Sstevel@tonic-gate sizeof sockaddr.sunix.sun_path)
12530Sstevel@tonic-gate {
12540Sstevel@tonic-gate smi_log(SMI_LOG_ERR,
12553544Sjbeck "%s: connect[%ld]: path too long",
12560Sstevel@tonic-gate g->a_ctx->ctx_smfi->xxfi_name,
12573544Sjbeck (long) g->a_ctx->ctx_id);
12580Sstevel@tonic-gate return _SMFIS_ABORT;
12590Sstevel@tonic-gate }
12600Sstevel@tonic-gate sockaddr.sunix.sun_family = AF_UNIX;
12610Sstevel@tonic-gate }
12620Sstevel@tonic-gate else
12630Sstevel@tonic-gate # endif /* NETUNIX */
12640Sstevel@tonic-gate {
12650Sstevel@tonic-gate smi_log(SMI_LOG_ERR,
12663544Sjbeck "%s: connect[%ld]: unknown family %d",
12670Sstevel@tonic-gate g->a_ctx->ctx_smfi->xxfi_name,
12683544Sjbeck (long) g->a_ctx->ctx_id, family);
12690Sstevel@tonic-gate return _SMFIS_ABORT;
12700Sstevel@tonic-gate }
12710Sstevel@tonic-gate }
12720Sstevel@tonic-gate return (*fi_connect)(g->a_ctx, g->a_buf,
12730Sstevel@tonic-gate family != SMFIA_UNKNOWN ? &sockaddr : NULL);
12740Sstevel@tonic-gate }
12750Sstevel@tonic-gate
12760Sstevel@tonic-gate /*
12770Sstevel@tonic-gate ** ST_EOH -- end of headers
12780Sstevel@tonic-gate **
12790Sstevel@tonic-gate ** Parameters:
12800Sstevel@tonic-gate ** g -- generic argument structure
12810Sstevel@tonic-gate **
12820Sstevel@tonic-gate ** Returns:
12830Sstevel@tonic-gate ** continue or filter-specified value
12840Sstevel@tonic-gate */
12850Sstevel@tonic-gate
12860Sstevel@tonic-gate static int
st_eoh(g)12870Sstevel@tonic-gate st_eoh(g)
12880Sstevel@tonic-gate genarg *g;
12890Sstevel@tonic-gate {
12900Sstevel@tonic-gate sfsistat (*fi_eoh) __P((SMFICTX *));
12910Sstevel@tonic-gate
12920Sstevel@tonic-gate if (g == NULL)
12930Sstevel@tonic-gate return _SMFIS_ABORT;
12940Sstevel@tonic-gate if (g->a_ctx->ctx_smfi != NULL &&
12950Sstevel@tonic-gate (fi_eoh = g->a_ctx->ctx_smfi->xxfi_eoh) != NULL)
12960Sstevel@tonic-gate return (*fi_eoh)(g->a_ctx);
12970Sstevel@tonic-gate return SMFIS_CONTINUE;
12980Sstevel@tonic-gate }
12990Sstevel@tonic-gate
13000Sstevel@tonic-gate /*
13010Sstevel@tonic-gate ** ST_DATA -- DATA command
13020Sstevel@tonic-gate **
13030Sstevel@tonic-gate ** Parameters:
13040Sstevel@tonic-gate ** g -- generic argument structure
13050Sstevel@tonic-gate **
13060Sstevel@tonic-gate ** Returns:
13070Sstevel@tonic-gate ** continue or filter-specified value
13080Sstevel@tonic-gate */
13090Sstevel@tonic-gate
13100Sstevel@tonic-gate static int
st_data(g)13110Sstevel@tonic-gate st_data(g)
13120Sstevel@tonic-gate genarg *g;
13130Sstevel@tonic-gate {
13140Sstevel@tonic-gate sfsistat (*fi_data) __P((SMFICTX *));
13150Sstevel@tonic-gate
13160Sstevel@tonic-gate if (g == NULL)
13170Sstevel@tonic-gate return _SMFIS_ABORT;
13180Sstevel@tonic-gate if (g->a_ctx->ctx_smfi != NULL &&
13194518Sjbeck g->a_ctx->ctx_smfi->xxfi_version > 3 &&
13200Sstevel@tonic-gate (fi_data = g->a_ctx->ctx_smfi->xxfi_data) != NULL)
13210Sstevel@tonic-gate return (*fi_data)(g->a_ctx);
13220Sstevel@tonic-gate return SMFIS_CONTINUE;
13230Sstevel@tonic-gate }
13240Sstevel@tonic-gate
13250Sstevel@tonic-gate /*
13260Sstevel@tonic-gate ** ST_HELO -- helo/ehlo command
13270Sstevel@tonic-gate **
13280Sstevel@tonic-gate ** Parameters:
13290Sstevel@tonic-gate ** g -- generic argument structure
13300Sstevel@tonic-gate **
13310Sstevel@tonic-gate ** Returns:
13320Sstevel@tonic-gate ** continue or filter-specified value
13330Sstevel@tonic-gate */
13343966Sjbeck
13350Sstevel@tonic-gate static int
st_helo(g)13360Sstevel@tonic-gate st_helo(g)
13370Sstevel@tonic-gate genarg *g;
13380Sstevel@tonic-gate {
13390Sstevel@tonic-gate sfsistat (*fi_helo) __P((SMFICTX *, char *));
13400Sstevel@tonic-gate
13410Sstevel@tonic-gate if (g == NULL)
13420Sstevel@tonic-gate return _SMFIS_ABORT;
13430Sstevel@tonic-gate mi_clr_macros(g->a_ctx, g->a_idx + 1);
13440Sstevel@tonic-gate if (g->a_ctx->ctx_smfi != NULL &&
13450Sstevel@tonic-gate (fi_helo = g->a_ctx->ctx_smfi->xxfi_helo) != NULL)
13460Sstevel@tonic-gate {
13470Sstevel@tonic-gate /* paranoia: check for terminating '\0' */
13480Sstevel@tonic-gate if (g->a_len == 0 || g->a_buf[g->a_len - 1] != '\0')
13490Sstevel@tonic-gate return MI_FAILURE;
13500Sstevel@tonic-gate return (*fi_helo)(g->a_ctx, g->a_buf);
13510Sstevel@tonic-gate }
13520Sstevel@tonic-gate return SMFIS_CONTINUE;
13530Sstevel@tonic-gate }
13543966Sjbeck
13550Sstevel@tonic-gate /*
13560Sstevel@tonic-gate ** ST_HEADER -- header line
13570Sstevel@tonic-gate **
13580Sstevel@tonic-gate ** Parameters:
13590Sstevel@tonic-gate ** g -- generic argument structure
13600Sstevel@tonic-gate **
13610Sstevel@tonic-gate ** Returns:
13620Sstevel@tonic-gate ** continue or filter-specified value
13630Sstevel@tonic-gate */
13640Sstevel@tonic-gate
13650Sstevel@tonic-gate static int
st_header(g)13660Sstevel@tonic-gate st_header(g)
13670Sstevel@tonic-gate genarg *g;
13680Sstevel@tonic-gate {
13690Sstevel@tonic-gate char *hf, *hv;
13700Sstevel@tonic-gate sfsistat (*fi_header) __P((SMFICTX *, char *, char *));
13710Sstevel@tonic-gate
13720Sstevel@tonic-gate if (g == NULL)
13730Sstevel@tonic-gate return _SMFIS_ABORT;
13740Sstevel@tonic-gate if (g->a_ctx->ctx_smfi == NULL ||
13750Sstevel@tonic-gate (fi_header = g->a_ctx->ctx_smfi->xxfi_header) == NULL)
13760Sstevel@tonic-gate return SMFIS_CONTINUE;
13770Sstevel@tonic-gate if (dec_arg2(g->a_buf, g->a_len, &hf, &hv) == MI_SUCCESS)
13780Sstevel@tonic-gate return (*fi_header)(g->a_ctx, hf, hv);
13790Sstevel@tonic-gate else
13800Sstevel@tonic-gate return _SMFIS_ABORT;
13810Sstevel@tonic-gate }
13820Sstevel@tonic-gate
13830Sstevel@tonic-gate #define ARGV_FCT(lf, rf, idx) \
13840Sstevel@tonic-gate char **argv; \
13850Sstevel@tonic-gate sfsistat (*lf) __P((SMFICTX *, char **)); \
13860Sstevel@tonic-gate int r; \
13870Sstevel@tonic-gate \
13880Sstevel@tonic-gate if (g == NULL) \
13890Sstevel@tonic-gate return _SMFIS_ABORT; \
13900Sstevel@tonic-gate mi_clr_macros(g->a_ctx, g->a_idx + 1); \
13910Sstevel@tonic-gate if (g->a_ctx->ctx_smfi == NULL || \
13920Sstevel@tonic-gate (lf = g->a_ctx->ctx_smfi->rf) == NULL) \
13930Sstevel@tonic-gate return SMFIS_CONTINUE; \
13940Sstevel@tonic-gate if ((argv = dec_argv(g->a_buf, g->a_len)) == NULL) \
13950Sstevel@tonic-gate return _SMFIS_ABORT; \
13960Sstevel@tonic-gate r = (*lf)(g->a_ctx, argv); \
13970Sstevel@tonic-gate free(argv); \
13980Sstevel@tonic-gate return r;
13990Sstevel@tonic-gate
14000Sstevel@tonic-gate /*
14010Sstevel@tonic-gate ** ST_SENDER -- MAIL FROM command
14020Sstevel@tonic-gate **
14030Sstevel@tonic-gate ** Parameters:
14040Sstevel@tonic-gate ** g -- generic argument structure
14050Sstevel@tonic-gate **
14060Sstevel@tonic-gate ** Returns:
14070Sstevel@tonic-gate ** continue or filter-specified value
14080Sstevel@tonic-gate */
14090Sstevel@tonic-gate
14100Sstevel@tonic-gate static int
st_sender(g)14110Sstevel@tonic-gate st_sender(g)
14120Sstevel@tonic-gate genarg *g;
14130Sstevel@tonic-gate {
14140Sstevel@tonic-gate ARGV_FCT(fi_envfrom, xxfi_envfrom, CI_MAIL)
14150Sstevel@tonic-gate }
14163544Sjbeck
14170Sstevel@tonic-gate /*
14180Sstevel@tonic-gate ** ST_RCPT -- RCPT TO command
14190Sstevel@tonic-gate **
14200Sstevel@tonic-gate ** Parameters:
14210Sstevel@tonic-gate ** g -- generic argument structure
14220Sstevel@tonic-gate **
14230Sstevel@tonic-gate ** Returns:
14240Sstevel@tonic-gate ** continue or filter-specified value
14250Sstevel@tonic-gate */
14260Sstevel@tonic-gate
14270Sstevel@tonic-gate static int
st_rcpt(g)14280Sstevel@tonic-gate st_rcpt(g)
14290Sstevel@tonic-gate genarg *g;
14300Sstevel@tonic-gate {
14310Sstevel@tonic-gate ARGV_FCT(fi_envrcpt, xxfi_envrcpt, CI_RCPT)
14320Sstevel@tonic-gate }
14330Sstevel@tonic-gate
14340Sstevel@tonic-gate /*
14350Sstevel@tonic-gate ** ST_UNKNOWN -- unrecognized or unimplemented command
14360Sstevel@tonic-gate **
14370Sstevel@tonic-gate ** Parameters:
14380Sstevel@tonic-gate ** g -- generic argument structure
14390Sstevel@tonic-gate **
14400Sstevel@tonic-gate ** Returns:
14410Sstevel@tonic-gate ** continue or filter-specified value
14420Sstevel@tonic-gate */
14430Sstevel@tonic-gate
14440Sstevel@tonic-gate static int
st_unknown(g)14450Sstevel@tonic-gate st_unknown(g)
14460Sstevel@tonic-gate genarg *g;
14470Sstevel@tonic-gate {
14483544Sjbeck sfsistat (*fi_unknown) __P((SMFICTX *, const char *));
14490Sstevel@tonic-gate
14500Sstevel@tonic-gate if (g == NULL)
14510Sstevel@tonic-gate return _SMFIS_ABORT;
14520Sstevel@tonic-gate if (g->a_ctx->ctx_smfi != NULL &&
14534518Sjbeck g->a_ctx->ctx_smfi->xxfi_version > 2 &&
14540Sstevel@tonic-gate (fi_unknown = g->a_ctx->ctx_smfi->xxfi_unknown) != NULL)
14553544Sjbeck return (*fi_unknown)(g->a_ctx, (const char *) g->a_buf);
14560Sstevel@tonic-gate return SMFIS_CONTINUE;
14570Sstevel@tonic-gate }
14580Sstevel@tonic-gate
14590Sstevel@tonic-gate /*
14600Sstevel@tonic-gate ** ST_MACROS -- deal with macros received from the MTA
14610Sstevel@tonic-gate **
14620Sstevel@tonic-gate ** Parameters:
14630Sstevel@tonic-gate ** g -- generic argument structure
14640Sstevel@tonic-gate **
14650Sstevel@tonic-gate ** Returns:
14660Sstevel@tonic-gate ** continue/keep
14670Sstevel@tonic-gate **
14680Sstevel@tonic-gate ** Side effects:
14690Sstevel@tonic-gate ** set pointer in macro array to current values.
14700Sstevel@tonic-gate */
14710Sstevel@tonic-gate
14720Sstevel@tonic-gate static int
st_macros(g)14730Sstevel@tonic-gate st_macros(g)
14740Sstevel@tonic-gate genarg *g;
14750Sstevel@tonic-gate {
14760Sstevel@tonic-gate int i;
14770Sstevel@tonic-gate char **argv;
14780Sstevel@tonic-gate
14790Sstevel@tonic-gate if (g == NULL || g->a_len < 1)
14800Sstevel@tonic-gate return _SMFIS_FAIL;
14810Sstevel@tonic-gate if ((argv = dec_argv(g->a_buf + 1, g->a_len - 1)) == NULL)
14820Sstevel@tonic-gate return _SMFIS_FAIL;
14830Sstevel@tonic-gate switch (g->a_buf[0])
14840Sstevel@tonic-gate {
14850Sstevel@tonic-gate case SMFIC_CONNECT:
14860Sstevel@tonic-gate i = CI_CONN;
14870Sstevel@tonic-gate break;
14880Sstevel@tonic-gate case SMFIC_HELO:
14890Sstevel@tonic-gate i = CI_HELO;
14900Sstevel@tonic-gate break;
14910Sstevel@tonic-gate case SMFIC_MAIL:
14920Sstevel@tonic-gate i = CI_MAIL;
14930Sstevel@tonic-gate break;
14940Sstevel@tonic-gate case SMFIC_RCPT:
14950Sstevel@tonic-gate i = CI_RCPT;
14960Sstevel@tonic-gate break;
14973544Sjbeck case SMFIC_DATA:
14983544Sjbeck i = CI_DATA;
14993544Sjbeck break;
15000Sstevel@tonic-gate case SMFIC_BODYEOB:
15010Sstevel@tonic-gate i = CI_EOM;
15020Sstevel@tonic-gate break;
15033544Sjbeck case SMFIC_EOH:
15043544Sjbeck i = CI_EOH;
15053544Sjbeck break;
15060Sstevel@tonic-gate default:
15070Sstevel@tonic-gate free(argv);
15080Sstevel@tonic-gate return _SMFIS_FAIL;
15090Sstevel@tonic-gate }
15100Sstevel@tonic-gate if (g->a_ctx->ctx_mac_ptr[i] != NULL)
15110Sstevel@tonic-gate free(g->a_ctx->ctx_mac_ptr[i]);
15120Sstevel@tonic-gate if (g->a_ctx->ctx_mac_buf[i] != NULL)
15130Sstevel@tonic-gate free(g->a_ctx->ctx_mac_buf[i]);
15140Sstevel@tonic-gate g->a_ctx->ctx_mac_ptr[i] = argv;
15150Sstevel@tonic-gate g->a_ctx->ctx_mac_buf[i] = g->a_buf;
15160Sstevel@tonic-gate return _SMFIS_KEEP;
15170Sstevel@tonic-gate }
15183966Sjbeck
15190Sstevel@tonic-gate /*
15200Sstevel@tonic-gate ** ST_QUIT -- quit command
15210Sstevel@tonic-gate **
15220Sstevel@tonic-gate ** Parameters:
15230Sstevel@tonic-gate ** g -- generic argument structure
15240Sstevel@tonic-gate **
15250Sstevel@tonic-gate ** Returns:
15260Sstevel@tonic-gate ** noreply
15270Sstevel@tonic-gate */
15280Sstevel@tonic-gate
15290Sstevel@tonic-gate /* ARGSUSED */
15300Sstevel@tonic-gate static int
st_quit(g)15310Sstevel@tonic-gate st_quit(g)
15320Sstevel@tonic-gate genarg *g;
15330Sstevel@tonic-gate {
15343544Sjbeck sfsistat (*fi_close) __P((SMFICTX *));
15353544Sjbeck
15363544Sjbeck if (g == NULL)
15373544Sjbeck return _SMFIS_ABORT;
15383544Sjbeck if (g->a_ctx->ctx_smfi != NULL &&
15393544Sjbeck (fi_close = g->a_ctx->ctx_smfi->xxfi_close) != NULL)
15403544Sjbeck (void) (*fi_close)(g->a_ctx);
15413544Sjbeck mi_clr_macros(g->a_ctx, 0);
15420Sstevel@tonic-gate return _SMFIS_NOREPLY;
15430Sstevel@tonic-gate }
15443966Sjbeck
15450Sstevel@tonic-gate /*
15460Sstevel@tonic-gate ** ST_BODYCHUNK -- deal with a piece of the mail body
15470Sstevel@tonic-gate **
15480Sstevel@tonic-gate ** Parameters:
15490Sstevel@tonic-gate ** g -- generic argument structure
15500Sstevel@tonic-gate **
15510Sstevel@tonic-gate ** Returns:
15520Sstevel@tonic-gate ** continue or filter-specified value
15530Sstevel@tonic-gate */
15540Sstevel@tonic-gate
15550Sstevel@tonic-gate static int
st_bodychunk(g)15560Sstevel@tonic-gate st_bodychunk(g)
15570Sstevel@tonic-gate genarg *g;
15580Sstevel@tonic-gate {
15590Sstevel@tonic-gate sfsistat (*fi_body) __P((SMFICTX *, unsigned char *, size_t));
15600Sstevel@tonic-gate
15610Sstevel@tonic-gate if (g == NULL)
15620Sstevel@tonic-gate return _SMFIS_ABORT;
15630Sstevel@tonic-gate if (g->a_ctx->ctx_smfi != NULL &&
15640Sstevel@tonic-gate (fi_body = g->a_ctx->ctx_smfi->xxfi_body) != NULL)
15650Sstevel@tonic-gate return (*fi_body)(g->a_ctx, (unsigned char *)g->a_buf,
15660Sstevel@tonic-gate g->a_len);
15670Sstevel@tonic-gate return SMFIS_CONTINUE;
15680Sstevel@tonic-gate }
15693966Sjbeck
15700Sstevel@tonic-gate /*
15710Sstevel@tonic-gate ** ST_BODYEND -- deal with the last piece of the mail body
15720Sstevel@tonic-gate **
15730Sstevel@tonic-gate ** Parameters:
15740Sstevel@tonic-gate ** g -- generic argument structure
15750Sstevel@tonic-gate **
15760Sstevel@tonic-gate ** Returns:
15770Sstevel@tonic-gate ** continue or filter-specified value
15780Sstevel@tonic-gate **
15790Sstevel@tonic-gate ** Side effects:
15800Sstevel@tonic-gate ** sends a reply for the body part (if non-empty).
15810Sstevel@tonic-gate */
15820Sstevel@tonic-gate
15830Sstevel@tonic-gate static int
st_bodyend(g)15840Sstevel@tonic-gate st_bodyend(g)
15850Sstevel@tonic-gate genarg *g;
15860Sstevel@tonic-gate {
15870Sstevel@tonic-gate sfsistat r;
15880Sstevel@tonic-gate sfsistat (*fi_body) __P((SMFICTX *, unsigned char *, size_t));
15890Sstevel@tonic-gate sfsistat (*fi_eom) __P((SMFICTX *));
15900Sstevel@tonic-gate
15910Sstevel@tonic-gate if (g == NULL)
15920Sstevel@tonic-gate return _SMFIS_ABORT;
15930Sstevel@tonic-gate r = SMFIS_CONTINUE;
15940Sstevel@tonic-gate if (g->a_ctx->ctx_smfi != NULL)
15950Sstevel@tonic-gate {
15960Sstevel@tonic-gate if ((fi_body = g->a_ctx->ctx_smfi->xxfi_body) != NULL &&
15970Sstevel@tonic-gate g->a_len > 0)
15980Sstevel@tonic-gate {
15990Sstevel@tonic-gate socket_t sd;
16000Sstevel@tonic-gate struct timeval timeout;
16010Sstevel@tonic-gate
16020Sstevel@tonic-gate timeout.tv_sec = g->a_ctx->ctx_timeout;
16030Sstevel@tonic-gate timeout.tv_usec = 0;
16040Sstevel@tonic-gate sd = g->a_ctx->ctx_sd;
16050Sstevel@tonic-gate r = (*fi_body)(g->a_ctx, (unsigned char *)g->a_buf,
16060Sstevel@tonic-gate g->a_len);
16070Sstevel@tonic-gate if (r != SMFIS_CONTINUE &&
16080Sstevel@tonic-gate sendreply(r, sd, &timeout, g->a_ctx) != MI_SUCCESS)
16090Sstevel@tonic-gate return _SMFIS_ABORT;
16100Sstevel@tonic-gate }
16110Sstevel@tonic-gate }
16120Sstevel@tonic-gate if (r == SMFIS_CONTINUE &&
16130Sstevel@tonic-gate (fi_eom = g->a_ctx->ctx_smfi->xxfi_eom) != NULL)
16140Sstevel@tonic-gate return (*fi_eom)(g->a_ctx);
16150Sstevel@tonic-gate return r;
16160Sstevel@tonic-gate }
16173966Sjbeck
16180Sstevel@tonic-gate /*
16190Sstevel@tonic-gate ** ST_ABORTFCT -- deal with aborts
16200Sstevel@tonic-gate **
16210Sstevel@tonic-gate ** Parameters:
16220Sstevel@tonic-gate ** g -- generic argument structure
16230Sstevel@tonic-gate **
16240Sstevel@tonic-gate ** Returns:
16250Sstevel@tonic-gate ** abort or filter-specified value
16260Sstevel@tonic-gate */
16270Sstevel@tonic-gate
16280Sstevel@tonic-gate static int
st_abortfct(g)16290Sstevel@tonic-gate st_abortfct(g)
16300Sstevel@tonic-gate genarg *g;
16310Sstevel@tonic-gate {
16320Sstevel@tonic-gate sfsistat (*fi_abort) __P((SMFICTX *));
16330Sstevel@tonic-gate
16340Sstevel@tonic-gate if (g == NULL)
16350Sstevel@tonic-gate return _SMFIS_ABORT;
16360Sstevel@tonic-gate if (g != NULL && g->a_ctx->ctx_smfi != NULL &&
16370Sstevel@tonic-gate (fi_abort = g->a_ctx->ctx_smfi->xxfi_abort) != NULL)
16380Sstevel@tonic-gate (void) (*fi_abort)(g->a_ctx);
16390Sstevel@tonic-gate return _SMFIS_NOREPLY;
16400Sstevel@tonic-gate }
16413966Sjbeck
16420Sstevel@tonic-gate /*
16430Sstevel@tonic-gate ** TRANS_OK -- is the state transition ok?
16440Sstevel@tonic-gate **
16450Sstevel@tonic-gate ** Parameters:
16460Sstevel@tonic-gate ** old -- old state
16470Sstevel@tonic-gate ** new -- new state
16480Sstevel@tonic-gate **
16490Sstevel@tonic-gate ** Returns:
16500Sstevel@tonic-gate ** state transition ok
16510Sstevel@tonic-gate */
16520Sstevel@tonic-gate
16530Sstevel@tonic-gate static bool
trans_ok(old,new)16540Sstevel@tonic-gate trans_ok(old, new)
16550Sstevel@tonic-gate int old, new;
16560Sstevel@tonic-gate {
16570Sstevel@tonic-gate int s, n;
16580Sstevel@tonic-gate
16590Sstevel@tonic-gate s = old;
16602197Sjbeck if (s >= SIZE_NEXT_STATES)
16612197Sjbeck return false;
16620Sstevel@tonic-gate do
16630Sstevel@tonic-gate {
16640Sstevel@tonic-gate /* is this state transition allowed? */
16650Sstevel@tonic-gate if ((MI_MASK(new) & next_states[s]) != 0)
16660Sstevel@tonic-gate return true;
16670Sstevel@tonic-gate
16680Sstevel@tonic-gate /*
16690Sstevel@tonic-gate ** no: try next state;
16700Sstevel@tonic-gate ** this works since the relevant states are ordered
16710Sstevel@tonic-gate ** strict sequentially
16720Sstevel@tonic-gate */
16730Sstevel@tonic-gate
16740Sstevel@tonic-gate n = s + 1;
16752197Sjbeck if (n >= SIZE_NEXT_STATES)
16762197Sjbeck return false;
16770Sstevel@tonic-gate
16780Sstevel@tonic-gate /*
16790Sstevel@tonic-gate ** can we actually "skip" this state?
16800Sstevel@tonic-gate ** see fix_stm() which sets this bit for those
16810Sstevel@tonic-gate ** states which the filter program is not interested in
16820Sstevel@tonic-gate */
16830Sstevel@tonic-gate
16840Sstevel@tonic-gate if (bitset(NX_SKIP, next_states[n]))
16850Sstevel@tonic-gate s = n;
16860Sstevel@tonic-gate else
16870Sstevel@tonic-gate return false;
16882197Sjbeck } while (s < SIZE_NEXT_STATES);
16890Sstevel@tonic-gate return false;
16900Sstevel@tonic-gate }
16913966Sjbeck
16920Sstevel@tonic-gate /*
16930Sstevel@tonic-gate ** FIX_STM -- add "skip" bits to the state transition table
16940Sstevel@tonic-gate **
16950Sstevel@tonic-gate ** Parameters:
16960Sstevel@tonic-gate ** ctx -- context structure
16970Sstevel@tonic-gate **
16980Sstevel@tonic-gate ** Returns:
16990Sstevel@tonic-gate ** None.
17000Sstevel@tonic-gate **
17010Sstevel@tonic-gate ** Side effects:
17020Sstevel@tonic-gate ** may change state transition table.
17030Sstevel@tonic-gate */
17040Sstevel@tonic-gate
17050Sstevel@tonic-gate static void
fix_stm(ctx)17060Sstevel@tonic-gate fix_stm(ctx)
17070Sstevel@tonic-gate SMFICTX_PTR ctx;
17080Sstevel@tonic-gate {
17090Sstevel@tonic-gate unsigned long fl;
17100Sstevel@tonic-gate
17110Sstevel@tonic-gate if (ctx == NULL || ctx->ctx_smfi == NULL)
17120Sstevel@tonic-gate return;
17130Sstevel@tonic-gate fl = ctx->ctx_pflags;
17140Sstevel@tonic-gate if (bitset(SMFIP_NOCONNECT, fl))
17150Sstevel@tonic-gate next_states[ST_CONN] |= NX_SKIP;
17160Sstevel@tonic-gate if (bitset(SMFIP_NOHELO, fl))
17170Sstevel@tonic-gate next_states[ST_HELO] |= NX_SKIP;
17180Sstevel@tonic-gate if (bitset(SMFIP_NOMAIL, fl))
17190Sstevel@tonic-gate next_states[ST_MAIL] |= NX_SKIP;
17200Sstevel@tonic-gate if (bitset(SMFIP_NORCPT, fl))
17210Sstevel@tonic-gate next_states[ST_RCPT] |= NX_SKIP;
17220Sstevel@tonic-gate if (bitset(SMFIP_NOHDRS, fl))
17230Sstevel@tonic-gate next_states[ST_HDRS] |= NX_SKIP;
17240Sstevel@tonic-gate if (bitset(SMFIP_NOEOH, fl))
17250Sstevel@tonic-gate next_states[ST_EOHS] |= NX_SKIP;
17260Sstevel@tonic-gate if (bitset(SMFIP_NOBODY, fl))
17270Sstevel@tonic-gate next_states[ST_BODY] |= NX_SKIP;
17283544Sjbeck if (bitset(SMFIP_NODATA, fl))
17293544Sjbeck next_states[ST_DATA] |= NX_SKIP;
17303544Sjbeck if (bitset(SMFIP_NOUNKNOWN, fl))
17313544Sjbeck next_states[ST_UNKN] |= NX_SKIP;
17320Sstevel@tonic-gate }
17333544Sjbeck
17340Sstevel@tonic-gate /*
17350Sstevel@tonic-gate ** DEC_ARGV -- split a buffer into a list of strings, NULL terminated
17360Sstevel@tonic-gate **
17370Sstevel@tonic-gate ** Parameters:
17380Sstevel@tonic-gate ** buf -- buffer with several strings
17390Sstevel@tonic-gate ** len -- length of buffer
17400Sstevel@tonic-gate **
17410Sstevel@tonic-gate ** Returns:
17420Sstevel@tonic-gate ** array of pointers to the individual strings
17430Sstevel@tonic-gate */
17440Sstevel@tonic-gate
17450Sstevel@tonic-gate static char **
dec_argv(buf,len)17460Sstevel@tonic-gate dec_argv(buf, len)
17470Sstevel@tonic-gate char *buf;
17480Sstevel@tonic-gate size_t len;
17490Sstevel@tonic-gate {
17500Sstevel@tonic-gate char **s;
17510Sstevel@tonic-gate size_t i;
17520Sstevel@tonic-gate int elem, nelem;
17530Sstevel@tonic-gate
17540Sstevel@tonic-gate nelem = 0;
17550Sstevel@tonic-gate for (i = 0; i < len; i++)
17560Sstevel@tonic-gate {
17570Sstevel@tonic-gate if (buf[i] == '\0')
17580Sstevel@tonic-gate ++nelem;
17590Sstevel@tonic-gate }
17600Sstevel@tonic-gate if (nelem == 0)
17610Sstevel@tonic-gate return NULL;
17620Sstevel@tonic-gate
17630Sstevel@tonic-gate /* last entry is only for the name */
17640Sstevel@tonic-gate s = (char **)malloc((nelem + 1) * (sizeof *s));
17650Sstevel@tonic-gate if (s == NULL)
17660Sstevel@tonic-gate return NULL;
17670Sstevel@tonic-gate s[0] = buf;
17680Sstevel@tonic-gate for (i = 0, elem = 0; i < len && elem < nelem; i++)
17690Sstevel@tonic-gate {
17700Sstevel@tonic-gate if (buf[i] == '\0')
17710Sstevel@tonic-gate {
17720Sstevel@tonic-gate ++elem;
17730Sstevel@tonic-gate if (i + 1 >= len)
17740Sstevel@tonic-gate s[elem] = NULL;
17750Sstevel@tonic-gate else
17760Sstevel@tonic-gate s[elem] = &(buf[i + 1]);
17770Sstevel@tonic-gate }
17780Sstevel@tonic-gate }
17790Sstevel@tonic-gate
17800Sstevel@tonic-gate /* overwrite last entry (already done above, just paranoia) */
17810Sstevel@tonic-gate s[elem] = NULL;
17820Sstevel@tonic-gate return s;
17830Sstevel@tonic-gate }
17843966Sjbeck
17850Sstevel@tonic-gate /*
17860Sstevel@tonic-gate ** DEC_ARG2 -- split a buffer into two strings
17870Sstevel@tonic-gate **
17880Sstevel@tonic-gate ** Parameters:
17890Sstevel@tonic-gate ** buf -- buffer with two strings
17900Sstevel@tonic-gate ** len -- length of buffer
17910Sstevel@tonic-gate ** s1,s2 -- pointer to result strings
17920Sstevel@tonic-gate **
17930Sstevel@tonic-gate ** Returns:
17940Sstevel@tonic-gate ** MI_FAILURE/MI_SUCCESS
17950Sstevel@tonic-gate */
17960Sstevel@tonic-gate
17970Sstevel@tonic-gate static int
dec_arg2(buf,len,s1,s2)17980Sstevel@tonic-gate dec_arg2(buf, len, s1, s2)
17990Sstevel@tonic-gate char *buf;
18000Sstevel@tonic-gate size_t len;
18010Sstevel@tonic-gate char **s1;
18020Sstevel@tonic-gate char **s2;
18030Sstevel@tonic-gate {
18040Sstevel@tonic-gate size_t i;
18050Sstevel@tonic-gate
18060Sstevel@tonic-gate /* paranoia: check for terminating '\0' */
18070Sstevel@tonic-gate if (len == 0 || buf[len - 1] != '\0')
18080Sstevel@tonic-gate return MI_FAILURE;
18090Sstevel@tonic-gate *s1 = buf;
18100Sstevel@tonic-gate for (i = 1; i < len && buf[i] != '\0'; i++)
18110Sstevel@tonic-gate continue;
18120Sstevel@tonic-gate if (i >= len - 1)
18130Sstevel@tonic-gate return MI_FAILURE;
18140Sstevel@tonic-gate *s2 = buf + i + 1;
18150Sstevel@tonic-gate return MI_SUCCESS;
18160Sstevel@tonic-gate }
18173966Sjbeck
18180Sstevel@tonic-gate /*
18190Sstevel@tonic-gate ** SENDOK -- is it ok for the filter to send stuff to the MTA?
18200Sstevel@tonic-gate **
18210Sstevel@tonic-gate ** Parameters:
18220Sstevel@tonic-gate ** ctx -- context structure
18230Sstevel@tonic-gate ** flag -- flag to check
18240Sstevel@tonic-gate **
18250Sstevel@tonic-gate ** Returns:
18260Sstevel@tonic-gate ** sending allowed (in current state)
18270Sstevel@tonic-gate */
18280Sstevel@tonic-gate
18290Sstevel@tonic-gate bool
mi_sendok(ctx,flag)18300Sstevel@tonic-gate mi_sendok(ctx, flag)
18310Sstevel@tonic-gate SMFICTX_PTR ctx;
18320Sstevel@tonic-gate int flag;
18330Sstevel@tonic-gate {
18340Sstevel@tonic-gate if (ctx == NULL || ctx->ctx_smfi == NULL)
18350Sstevel@tonic-gate return false;
18360Sstevel@tonic-gate
18370Sstevel@tonic-gate /* did the milter request this operation? */
18383544Sjbeck if (flag != 0 && !bitset(flag, ctx->ctx_aflags))
18390Sstevel@tonic-gate return false;
18400Sstevel@tonic-gate
18410Sstevel@tonic-gate /* are we in the correct state? It must be "End of Message". */
18420Sstevel@tonic-gate return ctx->ctx_state == ST_ENDM;
18430Sstevel@tonic-gate }
18443544Sjbeck
18453544Sjbeck #if _FFR_WORKERS_POOL
18463544Sjbeck /*
18473544Sjbeck ** MI_RD_SOCKET_READY - checks if the socket is ready for read(2)
18483544Sjbeck **
18493544Sjbeck ** Parameters:
18503544Sjbeck ** sd -- socket_t
18513544Sjbeck **
18523544Sjbeck ** Returns:
18533544Sjbeck ** true iff socket is ready for read(2)
18543544Sjbeck */
18553544Sjbeck
18563544Sjbeck #define MI_RD_CMD_TO 1
18573544Sjbeck #define MI_RD_MAX_ERR 16
18583544Sjbeck
18593544Sjbeck static bool
mi_rd_socket_ready(sd)18603544Sjbeck mi_rd_socket_ready (sd)
18613544Sjbeck socket_t sd;
18623544Sjbeck {
18633544Sjbeck int n;
18643544Sjbeck int nerr = 0;
18653544Sjbeck #if SM_CONF_POLL
18666562Sjbeck struct pollfd pfd;
18673544Sjbeck #else /* SM_CONF_POLL */
18686562Sjbeck fd_set rd_set, exc_set;
18693544Sjbeck #endif /* SM_CONF_POLL */
18703544Sjbeck
18713544Sjbeck do
18723544Sjbeck {
18733544Sjbeck #if SM_CONF_POLL
18743544Sjbeck pfd.fd = sd;
18753544Sjbeck pfd.events = POLLIN;
18763544Sjbeck pfd.revents = 0;
18773544Sjbeck
18783544Sjbeck n = poll(&pfd, 1, MI_RD_CMD_TO);
18793544Sjbeck #else /* SM_CONF_POLL */
18803544Sjbeck struct timeval timeout;
18813544Sjbeck
18823544Sjbeck FD_ZERO(&rd_set);
18833544Sjbeck FD_ZERO(&exc_set);
18843544Sjbeck FD_SET(sd, &rd_set);
18853544Sjbeck FD_SET(sd, &exc_set);
18863544Sjbeck
18873544Sjbeck timeout.tv_sec = MI_RD_CMD_TO / 1000;
18883544Sjbeck timeout.tv_usec = 0;
18893544Sjbeck n = select(sd + 1, &rd_set, NULL, &exc_set, &timeout);
18903544Sjbeck #endif /* SM_CONF_POLL */
18913544Sjbeck
18923544Sjbeck if (n < 0)
18933544Sjbeck {
18943544Sjbeck if (errno == EINTR)
18953544Sjbeck {
18963544Sjbeck nerr++;
18973544Sjbeck continue;
18983544Sjbeck }
18993544Sjbeck return true;
19003544Sjbeck }
19013544Sjbeck
19023544Sjbeck if (n == 0)
19033544Sjbeck return false;
19043544Sjbeck break;
19053544Sjbeck } while (nerr < MI_RD_MAX_ERR);
19063544Sjbeck if (nerr >= MI_RD_MAX_ERR)
19073544Sjbeck return false;
19083544Sjbeck
19093544Sjbeck #if SM_CONF_POLL
19103544Sjbeck return (pfd.revents != 0);
19113544Sjbeck #else /* SM_CONF_POLL */
19123544Sjbeck return FD_ISSET(sd, &rd_set) || FD_ISSET(sd, &exc_set);
19133544Sjbeck #endif /* SM_CONF_POLL */
19143544Sjbeck }
19153544Sjbeck #endif /* _FFR_WORKERS_POOL */
1916