1N/A/*
1N/A * Copyright (c) 1999-2004, 2006-2008 Sendmail, Inc. and its suppliers.
1N/A * All rights reserved.
1N/A *
1N/A * By using this file, you agree to the terms and conditions set
1N/A * forth in the LICENSE file which can be found at the top level of
1N/A * the sendmail distribution.
1N/A *
1N/A */
1N/A
1N/A#include <sm/gen.h>
1N/ASM_RCSID("@(#)$Id: engine.c,v 8.167 2011/03/03 06:09:15 ca Exp $")
1N/A
1N/A#include "libmilter.h"
1N/A
1N/A#if NETINET || NETINET6
1N/A# include <arpa/inet.h>
1N/A#endif /* NETINET || NETINET6 */
1N/A
1N/A/* generic argument for functions in the command table */
1N/Astruct arg_struct
1N/A{
1N/A size_t a_len; /* length of buffer */
1N/A char *a_buf; /* argument string */
1N/A int a_idx; /* index for macro array */
1N/A SMFICTX_PTR a_ctx; /* context */
1N/A};
1N/A
1N/Atypedef struct arg_struct genarg;
1N/A
1N/A/* structure for commands received from MTA */
1N/Astruct cmdfct_t
1N/A{
1N/A char cm_cmd; /* command */
1N/A int cm_argt; /* type of arguments expected */
1N/A int cm_next; /* next state */
1N/A int cm_todo; /* what to do next */
1N/A int cm_macros; /* index for macros */
1N/A int (*cm_fct) __P((genarg *)); /* function to execute */
1N/A};
1N/A
1N/Atypedef struct cmdfct_t cmdfct;
1N/A
1N/A/* possible values for cm_argt */
1N/A#define CM_ARG0 0 /* no args */
1N/A#define CM_ARG1 1 /* one arg (string) */
1N/A#define CM_ARG2 2 /* two args (strings) */
1N/A#define CM_ARGA 4 /* one string and _SOCK_ADDR */
1N/A#define CM_ARGO 5 /* two integers */
1N/A#define CM_ARGV 8 /* \0 separated list of args, NULL-terminated */
1N/A#define CM_ARGN 9 /* \0 separated list of args (strings) */
1N/A
1N/A/* possible values for cm_todo */
1N/A#define CT_CONT 0x0000 /* continue reading commands */
1N/A#define CT_IGNO 0x0001 /* continue even when error */
1N/A
1N/A/* not needed right now, done via return code instead */
1N/A#define CT_KEEP 0x0004 /* keep buffer (contains symbols) */
1N/A#define CT_END 0x0008 /* last command of session, stop replying */
1N/A
1N/A/* index in macro array: macros only for these commands */
1N/A#define CI_NONE (-1)
1N/A#define CI_CONN 0
1N/A#define CI_HELO 1
1N/A#define CI_MAIL 2
1N/A#define CI_RCPT 3
1N/A#define CI_DATA 4
1N/A#define CI_EOM 5
1N/A#define CI_EOH 6
1N/A#define CI_LAST CI_EOH
1N/A#if CI_LAST < CI_DATA
1N/AERROR: do not compile with CI_LAST < CI_DATA
1N/A#endif
1N/A#if CI_LAST < CI_EOM
1N/AERROR: do not compile with CI_LAST < CI_EOM
1N/A#endif
1N/A#if CI_LAST < CI_EOH
1N/AERROR: do not compile with CI_LAST < CI_EOH
1N/A#endif
1N/A#if CI_LAST < CI_ENVRCPT
1N/AERROR: do not compile with CI_LAST < CI_ENVRCPT
1N/A#endif
1N/A#if CI_LAST < CI_ENVFROM
1N/AERROR: do not compile with CI_LAST < CI_ENVFROM
1N/A#endif
1N/A#if CI_LAST < CI_HELO
1N/AERROR: do not compile with CI_LAST < CI_HELO
1N/A#endif
1N/A#if CI_LAST < CI_CONNECT
1N/AERROR: do not compile with CI_LAST < CI_CONNECT
1N/A#endif
1N/A#if CI_LAST >= MAX_MACROS_ENTRIES
1N/AERROR: do not compile with CI_LAST >= MAX_MACROS_ENTRIES
1N/A#endif
1N/A
1N/A/* function prototypes */
1N/Astatic int st_abortfct __P((genarg *));
1N/Astatic int st_macros __P((genarg *));
1N/Astatic int st_optionneg __P((genarg *));
1N/Astatic int st_bodychunk __P((genarg *));
1N/Astatic int st_connectinfo __P((genarg *));
1N/Astatic int st_bodyend __P((genarg *));
1N/Astatic int st_helo __P((genarg *));
1N/Astatic int st_header __P((genarg *));
1N/Astatic int st_sender __P((genarg *));
1N/Astatic int st_rcpt __P((genarg *));
1N/Astatic int st_unknown __P((genarg *));
1N/Astatic int st_data __P((genarg *));
1N/Astatic int st_eoh __P((genarg *));
1N/Astatic int st_quit __P((genarg *));
1N/Astatic int sendreply __P((sfsistat, socket_t, struct timeval *, SMFICTX_PTR));
1N/Astatic void fix_stm __P((SMFICTX_PTR));
1N/Astatic bool trans_ok __P((int, int));
1N/Astatic char **dec_argv __P((char *, size_t));
1N/Astatic int dec_arg2 __P((char *, size_t, char **, char **));
1N/Astatic void mi_clr_symlist __P((SMFICTX_PTR));
1N/A
1N/A#if _FFR_WORKERS_POOL
1N/Astatic bool mi_rd_socket_ready __P((int));
1N/A#endif /* _FFR_WORKERS_POOL */
1N/A
1N/A/* states */
1N/A#define ST_NONE (-1)
1N/A#define ST_INIT 0 /* initial state */
1N/A#define ST_OPTS 1 /* option negotiation */
1N/A#define ST_CONN 2 /* connection info */
1N/A#define ST_HELO 3 /* helo */
1N/A#define ST_MAIL 4 /* mail from */
1N/A#define ST_RCPT 5 /* rcpt to */
1N/A#define ST_DATA 6 /* data */
1N/A#define ST_HDRS 7 /* headers */
1N/A#define ST_EOHS 8 /* end of headers */
1N/A#define ST_BODY 9 /* body */
1N/A#define ST_ENDM 10 /* end of message */
1N/A#define ST_QUIT 11 /* quit */
1N/A#define ST_ABRT 12 /* abort */
1N/A#define ST_UNKN 13 /* unknown SMTP command */
1N/A#define ST_Q_NC 14 /* quit, new connection follows */
1N/A#define ST_LAST ST_Q_NC /* last valid state */
1N/A#define ST_SKIP 16 /* not a state but required for the state table */
1N/A
1N/A/* in a mail transaction? must be before eom according to spec. */
1N/A#define ST_IN_MAIL(st) ((st) >= ST_MAIL && (st) < ST_ENDM)
1N/A
1N/A/*
1N/A** set of next states
1N/A** each state (ST_*) corresponds to bit in an int value (1 << state)
1N/A** each state has a set of allowed transitions ('or' of bits of states)
1N/A** so a state transition is valid if the mask of the next state
1N/A** is set in the NX_* value
1N/A** this function is coded in trans_ok(), see below.
1N/A*/
1N/A
1N/A#define MI_MASK(x) (0x0001 << (x)) /* generate a bit "mask" for a state */
1N/A#define NX_INIT (MI_MASK(ST_OPTS))
1N/A#define NX_OPTS (MI_MASK(ST_CONN) | MI_MASK(ST_UNKN))
1N/A#define NX_CONN (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN))
1N/A#define NX_HELO (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN))
1N/A#define NX_MAIL (MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | MI_MASK(ST_UNKN))
1N/A#define NX_RCPT (MI_MASK(ST_HDRS) | MI_MASK(ST_EOHS) | MI_MASK(ST_DATA) | \
1N/A MI_MASK(ST_BODY) | MI_MASK(ST_ENDM) | \
1N/A MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | MI_MASK(ST_UNKN))
1N/A#define NX_DATA (MI_MASK(ST_EOHS) | MI_MASK(ST_HDRS) | MI_MASK(ST_ABRT))
1N/A#define NX_HDRS (MI_MASK(ST_EOHS) | MI_MASK(ST_HDRS) | MI_MASK(ST_ABRT))
1N/A#define NX_EOHS (MI_MASK(ST_BODY) | MI_MASK(ST_ENDM) | MI_MASK(ST_ABRT))
1N/A#define NX_BODY (MI_MASK(ST_ENDM) | MI_MASK(ST_BODY) | MI_MASK(ST_ABRT))
1N/A#define NX_ENDM (MI_MASK(ST_QUIT) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN) | \
1N/A MI_MASK(ST_Q_NC))
1N/A#define NX_QUIT 0
1N/A#define NX_ABRT 0
1N/A#define NX_UNKN (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | \
1N/A MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | \
1N/A MI_MASK(ST_DATA) | \
1N/A MI_MASK(ST_BODY) | MI_MASK(ST_UNKN) | \
1N/A MI_MASK(ST_ABRT) | MI_MASK(ST_QUIT) | MI_MASK(ST_Q_NC))
1N/A#define NX_Q_NC (MI_MASK(ST_CONN) | MI_MASK(ST_UNKN))
1N/A#define NX_SKIP MI_MASK(ST_SKIP)
1N/A
1N/Astatic int next_states[] =
1N/A{
1N/A NX_INIT
1N/A , NX_OPTS
1N/A , NX_CONN
1N/A , NX_HELO
1N/A , NX_MAIL
1N/A , NX_RCPT
1N/A , NX_DATA
1N/A , NX_HDRS
1N/A , NX_EOHS
1N/A , NX_BODY
1N/A , NX_ENDM
1N/A , NX_QUIT
1N/A , NX_ABRT
1N/A , NX_UNKN
1N/A , NX_Q_NC
1N/A};
1N/A
1N/A#define SIZE_NEXT_STATES (sizeof(next_states) / sizeof(next_states[0]))
1N/A
1N/A/* commands received by milter */
1N/Astatic cmdfct cmds[] =
1N/A{
1N/A {SMFIC_ABORT, CM_ARG0, ST_ABRT, CT_CONT, CI_NONE, st_abortfct }
1N/A, {SMFIC_MACRO, CM_ARGV, ST_NONE, CT_KEEP, CI_NONE, st_macros }
1N/A, {SMFIC_BODY, CM_ARG1, ST_BODY, CT_CONT, CI_NONE, st_bodychunk }
1N/A, {SMFIC_CONNECT, CM_ARG2, ST_CONN, CT_CONT, CI_CONN, st_connectinfo }
1N/A, {SMFIC_BODYEOB, CM_ARG1, ST_ENDM, CT_CONT, CI_EOM, st_bodyend }
1N/A, {SMFIC_HELO, CM_ARG1, ST_HELO, CT_CONT, CI_HELO, st_helo }
1N/A, {SMFIC_HEADER, CM_ARG2, ST_HDRS, CT_CONT, CI_NONE, st_header }
1N/A, {SMFIC_MAIL, CM_ARGV, ST_MAIL, CT_CONT, CI_MAIL, st_sender }
1N/A, {SMFIC_OPTNEG, CM_ARGO, ST_OPTS, CT_CONT, CI_NONE, st_optionneg }
1N/A, {SMFIC_EOH, CM_ARG0, ST_EOHS, CT_CONT, CI_EOH, st_eoh }
1N/A, {SMFIC_QUIT, CM_ARG0, ST_QUIT, CT_END, CI_NONE, st_quit }
1N/A, {SMFIC_DATA, CM_ARG0, ST_DATA, CT_CONT, CI_DATA, st_data }
1N/A, {SMFIC_RCPT, CM_ARGV, ST_RCPT, CT_IGNO, CI_RCPT, st_rcpt }
1N/A, {SMFIC_UNKNOWN, CM_ARG1, ST_UNKN, CT_IGNO, CI_NONE, st_unknown }
1N/A, {SMFIC_QUIT_NC, CM_ARG0, ST_Q_NC, CT_CONT, CI_NONE, st_quit }
1N/A};
1N/A
1N/A/*
1N/A** Additional (internal) reply codes;
1N/A** must be coordinated wit libmilter/mfapi.h
1N/A*/
1N/A
1N/A#define _SMFIS_KEEP 20
1N/A#define _SMFIS_ABORT 21
1N/A#define _SMFIS_OPTIONS 22
1N/A#define _SMFIS_NOREPLY SMFIS_NOREPLY
1N/A#define _SMFIS_FAIL (-1)
1N/A#define _SMFIS_NONE (-2)
1N/A
1N/A/*
1N/A** MI_ENGINE -- receive commands and process them
1N/A**
1N/A** Parameters:
1N/A** ctx -- context structure
1N/A**
1N/A** Returns:
1N/A** MI_FAILURE/MI_SUCCESS
1N/A*/
1N/A
1N/Aint
1N/Ami_engine(ctx)
1N/A SMFICTX_PTR ctx;
1N/A{
1N/A size_t len;
1N/A int i;
1N/A socket_t sd;
1N/A int ret = MI_SUCCESS;
1N/A int ncmds = sizeof(cmds) / sizeof(cmdfct);
1N/A int curstate = ST_INIT;
1N/A int newstate;
1N/A bool call_abort;
1N/A sfsistat r;
1N/A char cmd;
1N/A char *buf = NULL;
1N/A genarg arg;
1N/A struct timeval timeout;
1N/A int (*f) __P((genarg *));
1N/A sfsistat (*fi_abort) __P((SMFICTX *));
1N/A sfsistat (*fi_close) __P((SMFICTX *));
1N/A
1N/A arg.a_ctx = ctx;
1N/A sd = ctx->ctx_sd;
1N/A fi_abort = ctx->ctx_smfi->xxfi_abort;
1N/A#if _FFR_WORKERS_POOL
1N/A curstate = ctx->ctx_state;
1N/A if (curstate == ST_INIT)
1N/A {
1N/A mi_clr_macros(ctx, 0);
1N/A fix_stm(ctx);
1N/A }
1N/A#else /* _FFR_WORKERS_POOL */
1N/A mi_clr_macros(ctx, 0);
1N/A fix_stm(ctx);
1N/A#endif /* _FFR_WORKERS_POOL */
1N/A r = _SMFIS_NONE;
1N/A do
1N/A {
1N/A /* call abort only if in a mail transaction */
1N/A call_abort = ST_IN_MAIL(curstate);
1N/A timeout.tv_sec = ctx->ctx_timeout;
1N/A timeout.tv_usec = 0;
1N/A if (mi_stop() == MILTER_ABRT)
1N/A {
1N/A if (ctx->ctx_dbg > 3)
1N/A sm_dprintf("[%lu] milter_abort\n",
1N/A (long) ctx->ctx_id);
1N/A ret = MI_FAILURE;
1N/A break;
1N/A }
1N/A
1N/A /*
1N/A ** Notice: buf is allocated by mi_rd_cmd() and it will
1N/A ** usually be free()d after it has been used in f().
1N/A ** However, if the function returns _SMFIS_KEEP then buf
1N/A ** contains macros and will not be free()d.
1N/A ** Hence r must be set to _SMFIS_NONE if a new buf is
1N/A ** allocated to avoid problem with housekeeping, esp.
1N/A ** if the code "break"s out of the loop.
1N/A */
1N/A
1N/A#if _FFR_WORKERS_POOL
1N/A /* Is the socket ready to be read ??? */
1N/A if (!mi_rd_socket_ready(sd))
1N/A {
1N/A ret = MI_CONTINUE;
1N/A break;
1N/A }
1N/A#endif /* _FFR_WORKERS_POOL */
1N/A
1N/A r = _SMFIS_NONE;
1N/A if ((buf = mi_rd_cmd(sd, &timeout, &cmd, &len,
1N/A ctx->ctx_smfi->xxfi_name)) == NULL &&
1N/A cmd < SMFIC_VALIDCMD)
1N/A {
1N/A if (ctx->ctx_dbg > 5)
1N/A sm_dprintf("[%lu] mi_engine: mi_rd_cmd error (%x)\n",
1N/A (long) ctx->ctx_id, (int) cmd);
1N/A
1N/A /*
1N/A ** eof is currently treated as failure ->
1N/A ** abort() instead of close(), otherwise use:
1N/A ** if (cmd != SMFIC_EOF)
1N/A */
1N/A
1N/A ret = MI_FAILURE;
1N/A break;
1N/A }
1N/A if (ctx->ctx_dbg > 4)
1N/A sm_dprintf("[%lu] got cmd '%c' len %d\n",
1N/A (long) ctx->ctx_id, cmd, (int) len);
1N/A for (i = 0; i < ncmds; i++)
1N/A {
1N/A if (cmd == cmds[i].cm_cmd)
1N/A break;
1N/A }
1N/A if (i >= ncmds)
1N/A {
1N/A /* unknown command */
1N/A if (ctx->ctx_dbg > 1)
1N/A sm_dprintf("[%lu] cmd '%c' unknown\n",
1N/A (long) ctx->ctx_id, cmd);
1N/A ret = MI_FAILURE;
1N/A break;
1N/A }
1N/A if ((f = cmds[i].cm_fct) == NULL)
1N/A {
1N/A /* stop for now */
1N/A if (ctx->ctx_dbg > 1)
1N/A sm_dprintf("[%lu] cmd '%c' not impl\n",
1N/A (long) ctx->ctx_id, cmd);
1N/A ret = MI_FAILURE;
1N/A break;
1N/A }
1N/A
1N/A /* is new state ok? */
1N/A newstate = cmds[i].cm_next;
1N/A if (ctx->ctx_dbg > 5)
1N/A sm_dprintf("[%lu] cur %x new %x nextmask %x\n",
1N/A (long) ctx->ctx_id,
1N/A curstate, newstate, next_states[curstate]);
1N/A
1N/A if (newstate != ST_NONE && !trans_ok(curstate, newstate))
1N/A {
1N/A if (ctx->ctx_dbg > 1)
1N/A sm_dprintf("[%lu] abort: cur %d (%x) new %d (%x) next %x\n",
1N/A (long) ctx->ctx_id,
1N/A curstate, MI_MASK(curstate),
1N/A newstate, MI_MASK(newstate),
1N/A next_states[curstate]);
1N/A
1N/A /* call abort only if in a mail transaction */
1N/A if (fi_abort != NULL && call_abort)
1N/A (void) (*fi_abort)(ctx);
1N/A
1N/A /*
1N/A ** try to reach the new state from HELO
1N/A ** if it can't be reached, ignore the command.
1N/A */
1N/A
1N/A curstate = ST_HELO;
1N/A if (!trans_ok(curstate, newstate))
1N/A {
1N/A if (buf != NULL)
1N/A {
1N/A free(buf);
1N/A buf = NULL;
1N/A }
1N/A continue;
1N/A }
1N/A }
1N/A arg.a_len = len;
1N/A arg.a_buf = buf;
1N/A if (newstate != ST_NONE)
1N/A {
1N/A curstate = newstate;
1N/A ctx->ctx_state = curstate;
1N/A }
1N/A arg.a_idx = cmds[i].cm_macros;
1N/A call_abort = ST_IN_MAIL(curstate);
1N/A
1N/A /* call function to deal with command */
1N/A MI_MONITOR_BEGIN(ctx, cmd);
1N/A r = (*f)(&arg);
1N/A MI_MONITOR_END(ctx, cmd);
1N/A if (r != _SMFIS_KEEP && buf != NULL)
1N/A {
1N/A free(buf);
1N/A buf = NULL;
1N/A }
1N/A if (sendreply(r, sd, &timeout, ctx) != MI_SUCCESS)
1N/A {
1N/A ret = MI_FAILURE;
1N/A break;
1N/A }
1N/A
1N/A if (r == SMFIS_ACCEPT)
1N/A {
1N/A /* accept mail, no further actions taken */
1N/A curstate = ST_HELO;
1N/A }
1N/A else if (r == SMFIS_REJECT || r == SMFIS_DISCARD ||
1N/A r == SMFIS_TEMPFAIL)
1N/A {
1N/A /*
1N/A ** further actions depend on current state
1N/A ** if the IGNO bit is set: "ignore" the error,
1N/A ** i.e., stay in the current state
1N/A */
1N/A if (!bitset(CT_IGNO, cmds[i].cm_todo))
1N/A curstate = ST_HELO;
1N/A }
1N/A else if (r == _SMFIS_ABORT)
1N/A {
1N/A if (ctx->ctx_dbg > 5)
1N/A sm_dprintf("[%lu] function returned abort\n",
1N/A (long) ctx->ctx_id);
1N/A ret = MI_FAILURE;
1N/A break;
1N/A }
1N/A } while (!bitset(CT_END, cmds[i].cm_todo));
1N/A
1N/A ctx->ctx_state = curstate;
1N/A
1N/A if (ret == MI_FAILURE)
1N/A {
1N/A /* call abort only if in a mail transaction */
1N/A if (fi_abort != NULL && call_abort)
1N/A (void) (*fi_abort)(ctx);
1N/A }
1N/A
1N/A /* has close been called? */
1N/A if (ctx->ctx_state != ST_QUIT
1N/A#if _FFR_WORKERS_POOL
1N/A && ret != MI_CONTINUE
1N/A#endif /* _FFR_WORKERS_POOL */
1N/A )
1N/A {
1N/A if ((fi_close = ctx->ctx_smfi->xxfi_close) != NULL)
1N/A (void) (*fi_close)(ctx);
1N/A }
1N/A if (r != _SMFIS_KEEP && buf != NULL)
1N/A free(buf);
1N/A#if !_FFR_WORKERS_POOL
1N/A mi_clr_macros(ctx, 0);
1N/A#endif /* _FFR_WORKERS_POOL */
1N/A return ret;
1N/A}
1N/A
1N/Astatic size_t milter_addsymlist __P((SMFICTX_PTR, char *, char **));
1N/A
1N/Astatic size_t
1N/Amilter_addsymlist(ctx, buf, newbuf)
1N/A SMFICTX_PTR ctx;
1N/A char *buf;
1N/A char **newbuf;
1N/A{
1N/A size_t len;
1N/A int i;
1N/A mi_int32 v;
1N/A char *buffer;
1N/A
1N/A SM_ASSERT(ctx != NULL);
1N/A SM_ASSERT(buf != NULL);
1N/A SM_ASSERT(newbuf != NULL);
1N/A len = 0;
1N/A for (i = 0; i < MAX_MACROS_ENTRIES; i++)
1N/A {
1N/A if (ctx->ctx_mac_list[i] != NULL)
1N/A {
1N/A len += strlen(ctx->ctx_mac_list[i]) + 1 +
1N/A MILTER_LEN_BYTES;
1N/A }
1N/A }
1N/A if (len > 0)
1N/A {
1N/A size_t offset;
1N/A
1N/A SM_ASSERT(len + MILTER_OPTLEN > len);
1N/A len += MILTER_OPTLEN;
1N/A buffer = malloc(len);
1N/A if (buffer != NULL)
1N/A {
1N/A (void) memcpy(buffer, buf, MILTER_OPTLEN);
1N/A offset = MILTER_OPTLEN;
1N/A for (i = 0; i < MAX_MACROS_ENTRIES; i++)
1N/A {
1N/A size_t l;
1N/A
1N/A if (ctx->ctx_mac_list[i] == NULL)
1N/A continue;
1N/A
1N/A SM_ASSERT(offset + MILTER_LEN_BYTES < len);
1N/A v = htonl(i);
1N/A (void) memcpy(buffer + offset, (void *) &v,
1N/A MILTER_LEN_BYTES);
1N/A offset += MILTER_LEN_BYTES;
1N/A l = strlen(ctx->ctx_mac_list[i]) + 1;
1N/A SM_ASSERT(offset + l <= len);
1N/A (void) memcpy(buffer + offset,
1N/A ctx->ctx_mac_list[i], l);
1N/A offset += l;
1N/A }
1N/A }
1N/A else
1N/A {
1N/A /* oops ... */
1N/A }
1N/A }
1N/A else
1N/A {
1N/A len = MILTER_OPTLEN;
1N/A buffer = buf;
1N/A }
1N/A *newbuf = buffer;
1N/A return len;
1N/A}
1N/A
1N/A/*
1N/A** GET_NR_BIT -- get "no reply" bit matching state
1N/A**
1N/A** Parameters:
1N/A** state -- current protocol stage
1N/A**
1N/A** Returns:
1N/A** 0: no matching bit
1N/A** >0: the matching "no reply" bit
1N/A*/
1N/A
1N/Astatic unsigned long get_nr_bit __P((int));
1N/A
1N/Astatic unsigned long
1N/Aget_nr_bit(state)
1N/A int state;
1N/A{
1N/A unsigned long bit;
1N/A
1N/A switch (state)
1N/A {
1N/A case ST_CONN:
1N/A bit = SMFIP_NR_CONN;
1N/A break;
1N/A case ST_HELO:
1N/A bit = SMFIP_NR_HELO;
1N/A break;
1N/A case ST_MAIL:
1N/A bit = SMFIP_NR_MAIL;
1N/A break;
1N/A case ST_RCPT:
1N/A bit = SMFIP_NR_RCPT;
1N/A break;
1N/A case ST_DATA:
1N/A bit = SMFIP_NR_DATA;
1N/A break;
1N/A case ST_UNKN:
1N/A bit = SMFIP_NR_UNKN;
1N/A break;
1N/A case ST_HDRS:
1N/A bit = SMFIP_NR_HDR;
1N/A break;
1N/A case ST_EOHS:
1N/A bit = SMFIP_NR_EOH;
1N/A break;
1N/A case ST_BODY:
1N/A bit = SMFIP_NR_BODY;
1N/A break;
1N/A default:
1N/A bit = 0;
1N/A break;
1N/A }
1N/A return bit;
1N/A}
1N/A
1N/A/*
1N/A** SENDREPLY -- send a reply to the MTA
1N/A**
1N/A** Parameters:
1N/A** r -- reply code
1N/A** sd -- socket descriptor
1N/A** timeout_ptr -- (ptr to) timeout to use for sending
1N/A** ctx -- context structure
1N/A**
1N/A** Returns:
1N/A** MI_SUCCESS/MI_FAILURE
1N/A*/
1N/A
1N/Astatic int
1N/Asendreply(r, sd, timeout_ptr, ctx)
1N/A sfsistat r;
1N/A socket_t sd;
1N/A struct timeval *timeout_ptr;
1N/A SMFICTX_PTR ctx;
1N/A{
1N/A int ret;
1N/A unsigned long bit;
1N/A
1N/A ret = MI_SUCCESS;
1N/A
1N/A bit = get_nr_bit(ctx->ctx_state);
1N/A if (bit != 0 && (ctx->ctx_pflags & bit) != 0 && r != SMFIS_NOREPLY)
1N/A {
1N/A if (r >= SMFIS_CONTINUE && r < _SMFIS_KEEP)
1N/A {
1N/A /* milter said it wouldn't reply, but it lied... */
1N/A smi_log(SMI_LOG_ERR,
1N/A "%s: milter claimed not to reply in state %d but did anyway %d\n",
1N/A ctx->ctx_smfi->xxfi_name,
1N/A ctx->ctx_state, r);
1N/A
1N/A }
1N/A
1N/A /*
1N/A ** Force specified behavior, otherwise libmilter
1N/A ** and MTA will fail to communicate properly.
1N/A */
1N/A
1N/A switch (r)
1N/A {
1N/A case SMFIS_CONTINUE:
1N/A case SMFIS_TEMPFAIL:
1N/A case SMFIS_REJECT:
1N/A case SMFIS_DISCARD:
1N/A case SMFIS_ACCEPT:
1N/A case SMFIS_SKIP:
1N/A case _SMFIS_OPTIONS:
1N/A r = SMFIS_NOREPLY;
1N/A break;
1N/A }
1N/A }
1N/A
1N/A switch (r)
1N/A {
1N/A case SMFIS_CONTINUE:
1N/A ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_CONTINUE, NULL, 0);
1N/A break;
1N/A case SMFIS_TEMPFAIL:
1N/A case SMFIS_REJECT:
1N/A if (ctx->ctx_reply != NULL &&
1N/A ((r == SMFIS_TEMPFAIL && *ctx->ctx_reply == '4') ||
1N/A (r == SMFIS_REJECT && *ctx->ctx_reply == '5')))
1N/A {
1N/A ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_REPLYCODE,
1N/A ctx->ctx_reply,
1N/A strlen(ctx->ctx_reply) + 1);
1N/A free(ctx->ctx_reply);
1N/A ctx->ctx_reply = NULL;
1N/A }
1N/A else
1N/A {
1N/A ret = mi_wr_cmd(sd, timeout_ptr, r == SMFIS_REJECT ?
1N/A SMFIR_REJECT : SMFIR_TEMPFAIL, NULL, 0);
1N/A }
1N/A break;
1N/A case SMFIS_DISCARD:
1N/A ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_DISCARD, NULL, 0);
1N/A break;
1N/A case SMFIS_ACCEPT:
1N/A ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_ACCEPT, NULL, 0);
1N/A break;
1N/A case SMFIS_SKIP:
1N/A ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_SKIP, NULL, 0);
1N/A break;
1N/A case _SMFIS_OPTIONS:
1N/A {
1N/A mi_int32 v;
1N/A size_t len;
1N/A char *buffer;
1N/A char buf[MILTER_OPTLEN];
1N/A
1N/A v = htonl(ctx->ctx_prot_vers2mta);
1N/A (void) memcpy(&(buf[0]), (void *) &v,
1N/A MILTER_LEN_BYTES);
1N/A v = htonl(ctx->ctx_aflags);
1N/A (void) memcpy(&(buf[MILTER_LEN_BYTES]), (void *) &v,
1N/A MILTER_LEN_BYTES);
1N/A v = htonl(ctx->ctx_pflags2mta);
1N/A (void) memcpy(&(buf[MILTER_LEN_BYTES * 2]),
1N/A (void *) &v, MILTER_LEN_BYTES);
1N/A len = milter_addsymlist(ctx, buf, &buffer);
1N/A if (buffer != NULL)
1N/A ret = mi_wr_cmd(sd, timeout_ptr, SMFIC_OPTNEG,
1N/A buffer, len);
1N/A else
1N/A ret = MI_FAILURE;
1N/A }
1N/A break;
1N/A case SMFIS_NOREPLY:
1N/A if (bit != 0 &&
1N/A (ctx->ctx_pflags & bit) != 0 &&
1N/A (ctx->ctx_mta_pflags & bit) == 0)
1N/A {
1N/A /*
1N/A ** milter doesn't want to send a reply,
1N/A ** but the MTA doesn't have that feature: fake it.
1N/A */
1N/A
1N/A ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_CONTINUE, NULL,
1N/A 0);
1N/A }
1N/A break;
1N/A default: /* don't send a reply */
1N/A break;
1N/A }
1N/A return ret;
1N/A}
1N/A
1N/A/*
1N/A** CLR_MACROS -- clear set of macros starting from a given index
1N/A**
1N/A** Parameters:
1N/A** ctx -- context structure
1N/A** m -- index from which to clear all macros
1N/A**
1N/A** Returns:
1N/A** None.
1N/A*/
1N/A
1N/Avoid
1N/Ami_clr_macros(ctx, m)
1N/A SMFICTX_PTR ctx;
1N/A int m;
1N/A{
1N/A int i;
1N/A
1N/A for (i = m; i < MAX_MACROS_ENTRIES; i++)
1N/A {
1N/A if (ctx->ctx_mac_ptr[i] != NULL)
1N/A {
1N/A free(ctx->ctx_mac_ptr[i]);
1N/A ctx->ctx_mac_ptr[i] = NULL;
1N/A }
1N/A if (ctx->ctx_mac_buf[i] != NULL)
1N/A {
1N/A free(ctx->ctx_mac_buf[i]);
1N/A ctx->ctx_mac_buf[i] = NULL;
1N/A }
1N/A }
1N/A}
1N/A
1N/A/*
1N/A** MI_CLR_SYMLIST -- clear list of macros
1N/A**
1N/A** Parameters:
1N/A** ctx -- context structure
1N/A**
1N/A** Returns:
1N/A** None.
1N/A*/
1N/A
1N/Astatic void
1N/Ami_clr_symlist(ctx)
1N/A SMFICTX *ctx;
1N/A{
1N/A int i;
1N/A
1N/A SM_ASSERT(ctx != NULL);
1N/A for (i = SMFIM_FIRST; i <= SMFIM_LAST; i++)
1N/A {
1N/A if (ctx->ctx_mac_list[i] != NULL)
1N/A {
1N/A free(ctx->ctx_mac_list[i]);
1N/A ctx->ctx_mac_list[i] = NULL;
1N/A }
1N/A }
1N/A}
1N/A
1N/A/*
1N/A** MI_CLR_CTX -- clear context
1N/A**
1N/A** Parameters:
1N/A** ctx -- context structure
1N/A**
1N/A** Returns:
1N/A** None.
1N/A*/
1N/A
1N/Avoid
1N/Ami_clr_ctx(ctx)
1N/A SMFICTX *ctx;
1N/A{
1N/A SM_ASSERT(ctx != NULL);
1N/A if (ValidSocket(ctx->ctx_sd))
1N/A {
1N/A (void) closesocket(ctx->ctx_sd);
1N/A ctx->ctx_sd = INVALID_SOCKET;
1N/A }
1N/A if (ctx->ctx_reply != NULL)
1N/A {
1N/A free(ctx->ctx_reply);
1N/A ctx->ctx_reply = NULL;
1N/A }
1N/A if (ctx->ctx_privdata != NULL)
1N/A {
1N/A smi_log(SMI_LOG_WARN,
1N/A "%s: private data not NULL",
1N/A ctx->ctx_smfi->xxfi_name);
1N/A }
1N/A mi_clr_macros(ctx, 0);
1N/A mi_clr_symlist(ctx);
1N/A free(ctx);
1N/A}
1N/A
1N/A/*
1N/A** ST_OPTIONNEG -- negotiate options
1N/A**
1N/A** Parameters:
1N/A** g -- generic argument structure
1N/A**
1N/A** Returns:
1N/A** abort/send options/continue
1N/A*/
1N/A
1N/Astatic int
1N/Ast_optionneg(g)
1N/A genarg *g;
1N/A{
1N/A mi_int32 i, v, fake_pflags, internal_pflags;
1N/A SMFICTX_PTR ctx;
1N/A#if _FFR_MILTER_CHECK
1N/A bool testmode = false;
1N/A#endif /* _FFR_MILTER_CHECK */
1N/A int (*fi_negotiate) __P((SMFICTX *,
1N/A unsigned long, unsigned long,
1N/A unsigned long, unsigned long,
1N/A unsigned long *, unsigned long *,
1N/A unsigned long *, unsigned long *));
1N/A
1N/A if (g == NULL || g->a_ctx->ctx_smfi == NULL)
1N/A return SMFIS_CONTINUE;
1N/A ctx = g->a_ctx;
1N/A mi_clr_macros(ctx, g->a_idx + 1);
1N/A ctx->ctx_prot_vers = SMFI_PROT_VERSION;
1N/A
1N/A /* check for minimum length */
1N/A if (g->a_len < MILTER_OPTLEN)
1N/A {
1N/A smi_log(SMI_LOG_ERR,
1N/A "%s: st_optionneg[%ld]: len too short %d < %d",
1N/A ctx->ctx_smfi->xxfi_name,
1N/A (long) ctx->ctx_id, (int) g->a_len,
1N/A MILTER_OPTLEN);
1N/A return _SMFIS_ABORT;
1N/A }
1N/A
1N/A /* protocol version */
1N/A (void) memcpy((void *) &i, (void *) &(g->a_buf[0]), MILTER_LEN_BYTES);
1N/A v = ntohl(i);
1N/A
1N/A#define SMFI_PROT_VERSION_MIN 2
1N/A
1N/A /* check for minimum version */
1N/A if (v < SMFI_PROT_VERSION_MIN)
1N/A {
1N/A smi_log(SMI_LOG_ERR,
1N/A "%s: st_optionneg[%ld]: protocol version too old %d < %d",
1N/A ctx->ctx_smfi->xxfi_name,
1N/A (long) ctx->ctx_id, v, SMFI_PROT_VERSION_MIN);
1N/A return _SMFIS_ABORT;
1N/A }
1N/A ctx->ctx_mta_prot_vers = v;
1N/A if (ctx->ctx_prot_vers < ctx->ctx_mta_prot_vers)
1N/A ctx->ctx_prot_vers2mta = ctx->ctx_prot_vers;
1N/A else
1N/A ctx->ctx_prot_vers2mta = ctx->ctx_mta_prot_vers;
1N/A
1N/A (void) memcpy((void *) &i, (void *) &(g->a_buf[MILTER_LEN_BYTES]),
1N/A MILTER_LEN_BYTES);
1N/A v = ntohl(i);
1N/A
1N/A /* no flags? set to default value for V1 actions */
1N/A if (v == 0)
1N/A v = SMFI_V1_ACTS;
1N/A ctx->ctx_mta_aflags = v; /* MTA action flags */
1N/A
1N/A internal_pflags = 0;
1N/A (void) memcpy((void *) &i, (void *) &(g->a_buf[MILTER_LEN_BYTES * 2]),
1N/A MILTER_LEN_BYTES);
1N/A v = ntohl(i);
1N/A
1N/A /* no flags? set to default value for V1 protocol */
1N/A if (v == 0)
1N/A v = SMFI_V1_PROT;
1N/A#if _FFR_MDS_NEGOTIATE
1N/A else if (ctx->ctx_smfi->xxfi_version >= SMFI_VERSION_MDS)
1N/A {
1N/A /*
1N/A ** Allow changing the size only if milter is compiled
1N/A ** against a version that supports this.
1N/A ** If a milter is dynamically linked against a newer
1N/A ** libmilter version, we don't want to "surprise"
1N/A ** it with a larger buffer as it may rely on it
1N/A ** even though it is not documented as a limit.
1N/A */
1N/A
1N/A if (bitset(SMFIP_MDS_1M, v))
1N/A {
1N/A internal_pflags |= SMFIP_MDS_1M;
1N/A (void) smfi_setmaxdatasize(MILTER_MDS_1M);
1N/A }
1N/A else if (bitset(SMFIP_MDS_256K, v))
1N/A {
1N/A internal_pflags |= SMFIP_MDS_256K;
1N/A (void) smfi_setmaxdatasize(MILTER_MDS_256K);
1N/A }
1N/A }
1N/A# if 0
1N/A /* don't log this for now... */
1N/A else if (ctx->ctx_smfi->xxfi_version < SMFI_VERSION_MDS &&
1N/A bitset(SMFIP_MDS_1M|SMFIP_MDS_256K, v))
1N/A {
1N/A smi_log(SMI_LOG_WARN,
1N/A "%s: st_optionneg[%ld]: milter version=%X, trying flags=%X",
1N/A ctx->ctx_smfi->xxfi_name,
1N/A (long) ctx->ctx_id, ctx->ctx_smfi->xxfi_version, v);
1N/A }
1N/A# endif /* 0 */
1N/A#endif /* _FFR_MDS_NEGOTIATE */
1N/A
1N/A /*
1N/A ** MTA protocol flags.
1N/A ** We pass the internal flags to the milter as "read only",
1N/A ** i.e., a milter can read them so it knows which size
1N/A ** will be used, but any changes by a milter will be ignored
1N/A ** (see below, search for SMFI_INTERNAL).
1N/A */
1N/A
1N/A ctx->ctx_mta_pflags = (v & ~SMFI_INTERNAL) | internal_pflags;
1N/A
1N/A /*
1N/A ** Copy flags from milter struct into libmilter context;
1N/A ** this variable will be used later on to check whether
1N/A ** the MTA "actions" can fulfill the milter requirements,
1N/A ** but it may be overwritten by the negotiate callback.
1N/A */
1N/A
1N/A ctx->ctx_aflags = ctx->ctx_smfi->xxfi_flags;
1N/A fake_pflags = SMFIP_NR_CONN
1N/A |SMFIP_NR_HELO
1N/A |SMFIP_NR_MAIL
1N/A |SMFIP_NR_RCPT
1N/A |SMFIP_NR_DATA
1N/A |SMFIP_NR_UNKN
1N/A |SMFIP_NR_HDR
1N/A |SMFIP_NR_EOH
1N/A |SMFIP_NR_BODY
1N/A ;
1N/A
1N/A if (g->a_ctx->ctx_smfi != NULL &&
1N/A g->a_ctx->ctx_smfi->xxfi_version > 4 &&
1N/A (fi_negotiate = g->a_ctx->ctx_smfi->xxfi_negotiate) != NULL)
1N/A {
1N/A int r;
1N/A unsigned long m_aflags, m_pflags, m_f2, m_f3;
1N/A
1N/A /*
1N/A ** let milter decide whether the features offered by the
1N/A ** MTA are "good enough".
1N/A ** Notes:
1N/A ** - libmilter can "fake" some features (e.g., SMFIP_NR_HDR)
1N/A ** - m_f2, m_f3 are for future extensions
1N/A */
1N/A
1N/A m_f2 = m_f3 = 0;
1N/A m_aflags = ctx->ctx_mta_aflags;
1N/A m_pflags = ctx->ctx_pflags;
1N/A if ((SMFIP_SKIP & ctx->ctx_mta_pflags) != 0)
1N/A m_pflags |= SMFIP_SKIP;
1N/A r = fi_negotiate(g->a_ctx,
1N/A ctx->ctx_mta_aflags,
1N/A ctx->ctx_mta_pflags|fake_pflags,
1N/A 0, 0,
1N/A &m_aflags, &m_pflags, &m_f2, &m_f3);
1N/A
1N/A#if _FFR_MILTER_CHECK
1N/A testmode = bitset(SMFIP_TEST, m_pflags);
1N/A if (testmode)
1N/A m_pflags &= ~SMFIP_TEST;
1N/A#endif /* _FFR_MILTER_CHECK */
1N/A
1N/A /*
1N/A ** Types of protocol flags (pflags):
1N/A ** 1. do NOT send protocol step X
1N/A ** 2. MTA can do/understand something extra (SKIP,
1N/A ** send unknown RCPTs)
1N/A ** 3. MTA can deal with "no reply" for various protocol steps
1N/A ** Note: this mean that it isn't possible to simply set all
1N/A ** flags to get "everything":
1N/A ** setting a flag of type 1 turns off a step
1N/A ** (it should be the other way around:
1N/A ** a flag means a protocol step can be sent)
1N/A ** setting a flag of type 3 requires that milter
1N/A ** never sends a reply for the corresponding step.
1N/A ** Summary: the "negation" of protocol flags is causing
1N/A ** problems, but at least for type 3 there is no simple
1N/A ** solution.
1N/A **
1N/A ** What should "all options" mean?
1N/A ** send all protocol steps _except_ those for which there is
1N/A ** no callback (currently registered in ctx_pflags)
1N/A ** expect SKIP as return code? Yes
1N/A ** send unknown RCPTs? No,
1N/A ** must be explicitly requested?
1N/A ** "no reply" for some protocol steps? No,
1N/A ** must be explicitly requested.
1N/A */
1N/A
1N/A if (SMFIS_ALL_OPTS == r)
1N/A {
1N/A ctx->ctx_aflags = ctx->ctx_mta_aflags;
1N/A ctx->ctx_pflags2mta = ctx->ctx_pflags;
1N/A if ((SMFIP_SKIP & ctx->ctx_mta_pflags) != 0)
1N/A ctx->ctx_pflags2mta |= SMFIP_SKIP;
1N/A }
1N/A else if (r != SMFIS_CONTINUE)
1N/A {
1N/A smi_log(SMI_LOG_ERR,
1N/A "%s: st_optionneg[%ld]: xxfi_negotiate returned %d (protocol options=0x%lx, actions=0x%lx)",
1N/A ctx->ctx_smfi->xxfi_name,
1N/A (long) ctx->ctx_id, r, ctx->ctx_mta_pflags,
1N/A ctx->ctx_mta_aflags);
1N/A return _SMFIS_ABORT;
1N/A }
1N/A else
1N/A {
1N/A ctx->ctx_aflags = m_aflags;
1N/A ctx->ctx_pflags = m_pflags;
1N/A ctx->ctx_pflags2mta = m_pflags;
1N/A }
1N/A
1N/A /* check whether some flags need to be "faked" */
1N/A i = ctx->ctx_pflags2mta;
1N/A if ((ctx->ctx_mta_pflags & i) != i)
1N/A {
1N/A unsigned int idx;
1N/A unsigned long b;
1N/A
1N/A /*
1N/A ** If some behavior can be faked (set in fake_pflags),
1N/A ** but the MTA doesn't support it, then unset
1N/A ** that flag in the value that is sent to the MTA.
1N/A */
1N/A
1N/A for (idx = 0; idx < 32; idx++)
1N/A {
1N/A b = 1 << idx;
1N/A if ((ctx->ctx_mta_pflags & b) != b &&
1N/A (fake_pflags & b) == b)
1N/A ctx->ctx_pflags2mta &= ~b;
1N/A }
1N/A }
1N/A }
1N/A else
1N/A {
1N/A /*
1N/A ** Set the protocol flags based on the values determined
1N/A ** in mi_listener() which checked the defined callbacks.
1N/A */
1N/A
1N/A ctx->ctx_pflags2mta = ctx->ctx_pflags;
1N/A }
1N/A
1N/A /* check whether actions and protocol requirements can be satisfied */
1N/A i = ctx->ctx_aflags;
1N/A if ((i & ctx->ctx_mta_aflags) != i)
1N/A {
1N/A smi_log(SMI_LOG_ERR,
1N/A "%s: st_optionneg[%ld]: 0x%lx does not fulfill action requirements 0x%x",
1N/A ctx->ctx_smfi->xxfi_name,
1N/A (long) ctx->ctx_id, ctx->ctx_mta_aflags, i);
1N/A return _SMFIS_ABORT;
1N/A }
1N/A
1N/A i = ctx->ctx_pflags2mta;
1N/A if ((ctx->ctx_mta_pflags & i) != i)
1N/A {
1N/A /*
1N/A ** Older MTAs do not support some protocol steps.
1N/A ** As this protocol is a bit "wierd" (it asks for steps
1N/A ** NOT to be taken/sent) we have to check whether we
1N/A ** should turn off those "negative" requests.
1N/A ** Currently these are only SMFIP_NODATA and SMFIP_NOUNKNOWN.
1N/A */
1N/A
1N/A if (bitset(SMFIP_NODATA, ctx->ctx_pflags2mta) &&
1N/A !bitset(SMFIP_NODATA, ctx->ctx_mta_pflags))
1N/A ctx->ctx_pflags2mta &= ~SMFIP_NODATA;
1N/A if (bitset(SMFIP_NOUNKNOWN, ctx->ctx_pflags2mta) &&
1N/A !bitset(SMFIP_NOUNKNOWN, ctx->ctx_mta_pflags))
1N/A ctx->ctx_pflags2mta &= ~SMFIP_NOUNKNOWN;
1N/A i = ctx->ctx_pflags2mta;
1N/A }
1N/A
1N/A if ((ctx->ctx_mta_pflags & i) != i)
1N/A {
1N/A smi_log(SMI_LOG_ERR,
1N/A "%s: st_optionneg[%ld]: 0x%lx does not fulfill protocol requirements 0x%x",
1N/A ctx->ctx_smfi->xxfi_name,
1N/A (long) ctx->ctx_id, ctx->ctx_mta_pflags, i);
1N/A return _SMFIS_ABORT;
1N/A }
1N/A fix_stm(ctx);
1N/A
1N/A if (ctx->ctx_dbg > 3)
1N/A sm_dprintf("[%lu] milter_negotiate:"
1N/A " mta_actions=0x%lx, mta_flags=0x%lx"
1N/A " actions=0x%lx, flags=0x%lx\n"
1N/A , (long) ctx->ctx_id
1N/A , ctx->ctx_mta_aflags, ctx->ctx_mta_pflags
1N/A , ctx->ctx_aflags, ctx->ctx_pflags);
1N/A
1N/A#if _FFR_MILTER_CHECK
1N/A if (ctx->ctx_dbg > 3)
1N/A sm_dprintf("[%lu] milter_negotiate:"
1N/A " testmode=%d, pflags2mta=%X, internal_pflags=%X\n"
1N/A , (long) ctx->ctx_id, testmode
1N/A , ctx->ctx_pflags2mta, internal_pflags);
1N/A
1N/A /* in test mode: take flags without further modifications */
1N/A if (!testmode)
1N/A /* Warning: check statement below! */
1N/A#endif /* _FFR_MILTER_CHECK */
1N/A
1N/A /*
1N/A ** Remove the internal flags that might have been set by a milter
1N/A ** and set only those determined above.
1N/A */
1N/A
1N/A ctx->ctx_pflags2mta = (ctx->ctx_pflags2mta & ~SMFI_INTERNAL)
1N/A | internal_pflags;
1N/A return _SMFIS_OPTIONS;
1N/A}
1N/A
1N/A/*
1N/A** ST_CONNECTINFO -- receive connection information
1N/A**
1N/A** Parameters:
1N/A** g -- generic argument structure
1N/A**
1N/A** Returns:
1N/A** continue or filter-specified value
1N/A*/
1N/A
1N/Astatic int
1N/Ast_connectinfo(g)
1N/A genarg *g;
1N/A{
1N/A size_t l;
1N/A size_t i;
1N/A char *s, family;
1N/A unsigned short port = 0;
1N/A _SOCK_ADDR sockaddr;
1N/A sfsistat (*fi_connect) __P((SMFICTX *, char *, _SOCK_ADDR *));
1N/A
1N/A if (g == NULL)
1N/A return _SMFIS_ABORT;
1N/A mi_clr_macros(g->a_ctx, g->a_idx + 1);
1N/A if (g->a_ctx->ctx_smfi == NULL ||
1N/A (fi_connect = g->a_ctx->ctx_smfi->xxfi_connect) == NULL)
1N/A return SMFIS_CONTINUE;
1N/A
1N/A s = g->a_buf;
1N/A i = 0;
1N/A l = g->a_len;
1N/A while (s[i] != '\0' && i <= l)
1N/A ++i;
1N/A if (i + 1 >= l)
1N/A return _SMFIS_ABORT;
1N/A
1N/A /* Move past trailing \0 in host string */
1N/A i++;
1N/A family = s[i++];
1N/A (void) memset(&sockaddr, '\0', sizeof sockaddr);
1N/A if (family != SMFIA_UNKNOWN)
1N/A {
1N/A if (i + sizeof port >= l)
1N/A {
1N/A smi_log(SMI_LOG_ERR,
1N/A "%s: connect[%ld]: wrong len %d >= %d",
1N/A g->a_ctx->ctx_smfi->xxfi_name,
1N/A (long) g->a_ctx->ctx_id, (int) i, (int) l);
1N/A return _SMFIS_ABORT;
1N/A }
1N/A (void) memcpy((void *) &port, (void *) (s + i),
1N/A sizeof port);
1N/A i += sizeof port;
1N/A
1N/A /* make sure string is terminated */
1N/A if (s[l - 1] != '\0')
1N/A return _SMFIS_ABORT;
1N/A# if NETINET
1N/A if (family == SMFIA_INET)
1N/A {
1N/A if (inet_aton(s + i, (struct in_addr *) &sockaddr.sin.sin_addr)
1N/A != 1)
1N/A {
1N/A smi_log(SMI_LOG_ERR,
1N/A "%s: connect[%ld]: inet_aton failed",
1N/A g->a_ctx->ctx_smfi->xxfi_name,
1N/A (long) g->a_ctx->ctx_id);
1N/A return _SMFIS_ABORT;
1N/A }
1N/A sockaddr.sa.sa_family = AF_INET;
1N/A if (port > 0)
1N/A sockaddr.sin.sin_port = port;
1N/A }
1N/A else
1N/A# endif /* NETINET */
1N/A# if NETINET6
1N/A if (family == SMFIA_INET6)
1N/A {
1N/A if (mi_inet_pton(AF_INET6, s + i,
1N/A &sockaddr.sin6.sin6_addr) != 1)
1N/A {
1N/A smi_log(SMI_LOG_ERR,
1N/A "%s: connect[%ld]: mi_inet_pton failed",
1N/A g->a_ctx->ctx_smfi->xxfi_name,
1N/A (long) g->a_ctx->ctx_id);
1N/A return _SMFIS_ABORT;
1N/A }
1N/A sockaddr.sa.sa_family = AF_INET6;
1N/A if (port > 0)
1N/A sockaddr.sin6.sin6_port = port;
1N/A }
1N/A else
1N/A# endif /* NETINET6 */
1N/A# if NETUNIX
1N/A if (family == SMFIA_UNIX)
1N/A {
1N/A if (sm_strlcpy(sockaddr.sunix.sun_path, s + i,
1N/A sizeof sockaddr.sunix.sun_path) >=
1N/A sizeof sockaddr.sunix.sun_path)
1N/A {
1N/A smi_log(SMI_LOG_ERR,
1N/A "%s: connect[%ld]: path too long",
1N/A g->a_ctx->ctx_smfi->xxfi_name,
1N/A (long) g->a_ctx->ctx_id);
1N/A return _SMFIS_ABORT;
1N/A }
1N/A sockaddr.sunix.sun_family = AF_UNIX;
1N/A }
1N/A else
1N/A# endif /* NETUNIX */
1N/A {
1N/A smi_log(SMI_LOG_ERR,
1N/A "%s: connect[%ld]: unknown family %d",
1N/A g->a_ctx->ctx_smfi->xxfi_name,
1N/A (long) g->a_ctx->ctx_id, family);
1N/A return _SMFIS_ABORT;
1N/A }
1N/A }
1N/A return (*fi_connect)(g->a_ctx, g->a_buf,
1N/A family != SMFIA_UNKNOWN ? &sockaddr : NULL);
1N/A}
1N/A
1N/A/*
1N/A** ST_EOH -- end of headers
1N/A**
1N/A** Parameters:
1N/A** g -- generic argument structure
1N/A**
1N/A** Returns:
1N/A** continue or filter-specified value
1N/A*/
1N/A
1N/Astatic int
1N/Ast_eoh(g)
1N/A genarg *g;
1N/A{
1N/A sfsistat (*fi_eoh) __P((SMFICTX *));
1N/A
1N/A if (g == NULL)
1N/A return _SMFIS_ABORT;
1N/A if (g->a_ctx->ctx_smfi != NULL &&
1N/A (fi_eoh = g->a_ctx->ctx_smfi->xxfi_eoh) != NULL)
1N/A return (*fi_eoh)(g->a_ctx);
1N/A return SMFIS_CONTINUE;
1N/A}
1N/A
1N/A/*
1N/A** ST_DATA -- DATA command
1N/A**
1N/A** Parameters:
1N/A** g -- generic argument structure
1N/A**
1N/A** Returns:
1N/A** continue or filter-specified value
1N/A*/
1N/A
1N/Astatic int
1N/Ast_data(g)
1N/A genarg *g;
1N/A{
1N/A sfsistat (*fi_data) __P((SMFICTX *));
1N/A
1N/A if (g == NULL)
1N/A return _SMFIS_ABORT;
1N/A if (g->a_ctx->ctx_smfi != NULL &&
1N/A g->a_ctx->ctx_smfi->xxfi_version > 3 &&
1N/A (fi_data = g->a_ctx->ctx_smfi->xxfi_data) != NULL)
1N/A return (*fi_data)(g->a_ctx);
1N/A return SMFIS_CONTINUE;
1N/A}
1N/A
1N/A/*
1N/A** ST_HELO -- helo/ehlo command
1N/A**
1N/A** Parameters:
1N/A** g -- generic argument structure
1N/A**
1N/A** Returns:
1N/A** continue or filter-specified value
1N/A*/
1N/A
1N/Astatic int
1N/Ast_helo(g)
1N/A genarg *g;
1N/A{
1N/A sfsistat (*fi_helo) __P((SMFICTX *, char *));
1N/A
1N/A if (g == NULL)
1N/A return _SMFIS_ABORT;
1N/A mi_clr_macros(g->a_ctx, g->a_idx + 1);
1N/A if (g->a_ctx->ctx_smfi != NULL &&
1N/A (fi_helo = g->a_ctx->ctx_smfi->xxfi_helo) != NULL)
1N/A {
1N/A /* paranoia: check for terminating '\0' */
1N/A if (g->a_len == 0 || g->a_buf[g->a_len - 1] != '\0')
1N/A return MI_FAILURE;
1N/A return (*fi_helo)(g->a_ctx, g->a_buf);
1N/A }
1N/A return SMFIS_CONTINUE;
1N/A}
1N/A
1N/A/*
1N/A** ST_HEADER -- header line
1N/A**
1N/A** Parameters:
1N/A** g -- generic argument structure
1N/A**
1N/A** Returns:
1N/A** continue or filter-specified value
1N/A*/
1N/A
1N/Astatic int
1N/Ast_header(g)
1N/A genarg *g;
1N/A{
1N/A char *hf, *hv;
1N/A sfsistat (*fi_header) __P((SMFICTX *, char *, char *));
1N/A
1N/A if (g == NULL)
1N/A return _SMFIS_ABORT;
1N/A if (g->a_ctx->ctx_smfi == NULL ||
1N/A (fi_header = g->a_ctx->ctx_smfi->xxfi_header) == NULL)
1N/A return SMFIS_CONTINUE;
1N/A if (dec_arg2(g->a_buf, g->a_len, &hf, &hv) == MI_SUCCESS)
1N/A return (*fi_header)(g->a_ctx, hf, hv);
1N/A else
1N/A return _SMFIS_ABORT;
1N/A}
1N/A
1N/A#define ARGV_FCT(lf, rf, idx) \
1N/A char **argv; \
1N/A sfsistat (*lf) __P((SMFICTX *, char **)); \
1N/A int r; \
1N/A \
1N/A if (g == NULL) \
1N/A return _SMFIS_ABORT; \
1N/A mi_clr_macros(g->a_ctx, g->a_idx + 1); \
1N/A if (g->a_ctx->ctx_smfi == NULL || \
1N/A (lf = g->a_ctx->ctx_smfi->rf) == NULL) \
1N/A return SMFIS_CONTINUE; \
1N/A if ((argv = dec_argv(g->a_buf, g->a_len)) == NULL) \
1N/A return _SMFIS_ABORT; \
1N/A r = (*lf)(g->a_ctx, argv); \
1N/A free(argv); \
1N/A return r;
1N/A
1N/A/*
1N/A** ST_SENDER -- MAIL FROM command
1N/A**
1N/A** Parameters:
1N/A** g -- generic argument structure
1N/A**
1N/A** Returns:
1N/A** continue or filter-specified value
1N/A*/
1N/A
1N/Astatic int
1N/Ast_sender(g)
1N/A genarg *g;
1N/A{
1N/A ARGV_FCT(fi_envfrom, xxfi_envfrom, CI_MAIL)
1N/A}
1N/A
1N/A/*
1N/A** ST_RCPT -- RCPT TO command
1N/A**
1N/A** Parameters:
1N/A** g -- generic argument structure
1N/A**
1N/A** Returns:
1N/A** continue or filter-specified value
1N/A*/
1N/A
1N/Astatic int
1N/Ast_rcpt(g)
1N/A genarg *g;
1N/A{
1N/A ARGV_FCT(fi_envrcpt, xxfi_envrcpt, CI_RCPT)
1N/A}
1N/A
1N/A/*
1N/A** ST_UNKNOWN -- unrecognized or unimplemented command
1N/A**
1N/A** Parameters:
1N/A** g -- generic argument structure
1N/A**
1N/A** Returns:
1N/A** continue or filter-specified value
1N/A*/
1N/A
1N/Astatic int
1N/Ast_unknown(g)
1N/A genarg *g;
1N/A{
1N/A sfsistat (*fi_unknown) __P((SMFICTX *, const char *));
1N/A
1N/A if (g == NULL)
1N/A return _SMFIS_ABORT;
1N/A if (g->a_ctx->ctx_smfi != NULL &&
1N/A g->a_ctx->ctx_smfi->xxfi_version > 2 &&
1N/A (fi_unknown = g->a_ctx->ctx_smfi->xxfi_unknown) != NULL)
1N/A return (*fi_unknown)(g->a_ctx, (const char *) g->a_buf);
1N/A return SMFIS_CONTINUE;
1N/A}
1N/A
1N/A/*
1N/A** ST_MACROS -- deal with macros received from the MTA
1N/A**
1N/A** Parameters:
1N/A** g -- generic argument structure
1N/A**
1N/A** Returns:
1N/A** continue/keep
1N/A**
1N/A** Side effects:
1N/A** set pointer in macro array to current values.
1N/A*/
1N/A
1N/Astatic int
1N/Ast_macros(g)
1N/A genarg *g;
1N/A{
1N/A int i;
1N/A char **argv;
1N/A
1N/A if (g == NULL || g->a_len < 1)
1N/A return _SMFIS_FAIL;
1N/A if ((argv = dec_argv(g->a_buf + 1, g->a_len - 1)) == NULL)
1N/A return _SMFIS_FAIL;
1N/A switch (g->a_buf[0])
1N/A {
1N/A case SMFIC_CONNECT:
1N/A i = CI_CONN;
1N/A break;
1N/A case SMFIC_HELO:
1N/A i = CI_HELO;
1N/A break;
1N/A case SMFIC_MAIL:
1N/A i = CI_MAIL;
1N/A break;
1N/A case SMFIC_RCPT:
1N/A i = CI_RCPT;
1N/A break;
1N/A case SMFIC_DATA:
1N/A i = CI_DATA;
1N/A break;
1N/A case SMFIC_BODYEOB:
1N/A i = CI_EOM;
1N/A break;
1N/A case SMFIC_EOH:
1N/A i = CI_EOH;
1N/A break;
1N/A default:
1N/A free(argv);
1N/A return _SMFIS_FAIL;
1N/A }
1N/A if (g->a_ctx->ctx_mac_ptr[i] != NULL)
1N/A free(g->a_ctx->ctx_mac_ptr[i]);
1N/A if (g->a_ctx->ctx_mac_buf[i] != NULL)
1N/A free(g->a_ctx->ctx_mac_buf[i]);
1N/A g->a_ctx->ctx_mac_ptr[i] = argv;
1N/A g->a_ctx->ctx_mac_buf[i] = g->a_buf;
1N/A return _SMFIS_KEEP;
1N/A}
1N/A
1N/A/*
1N/A** ST_QUIT -- quit command
1N/A**
1N/A** Parameters:
1N/A** g -- generic argument structure
1N/A**
1N/A** Returns:
1N/A** noreply
1N/A*/
1N/A
1N/A/* ARGSUSED */
1N/Astatic int
1N/Ast_quit(g)
1N/A genarg *g;
1N/A{
1N/A sfsistat (*fi_close) __P((SMFICTX *));
1N/A
1N/A if (g == NULL)
1N/A return _SMFIS_ABORT;
1N/A if (g->a_ctx->ctx_smfi != NULL &&
1N/A (fi_close = g->a_ctx->ctx_smfi->xxfi_close) != NULL)
1N/A (void) (*fi_close)(g->a_ctx);
1N/A mi_clr_macros(g->a_ctx, 0);
1N/A return _SMFIS_NOREPLY;
1N/A}
1N/A
1N/A/*
1N/A** ST_BODYCHUNK -- deal with a piece of the mail body
1N/A**
1N/A** Parameters:
1N/A** g -- generic argument structure
1N/A**
1N/A** Returns:
1N/A** continue or filter-specified value
1N/A*/
1N/A
1N/Astatic int
1N/Ast_bodychunk(g)
1N/A genarg *g;
1N/A{
1N/A sfsistat (*fi_body) __P((SMFICTX *, unsigned char *, size_t));
1N/A
1N/A if (g == NULL)
1N/A return _SMFIS_ABORT;
1N/A if (g->a_ctx->ctx_smfi != NULL &&
1N/A (fi_body = g->a_ctx->ctx_smfi->xxfi_body) != NULL)
1N/A return (*fi_body)(g->a_ctx, (unsigned char *)g->a_buf,
1N/A g->a_len);
1N/A return SMFIS_CONTINUE;
1N/A}
1N/A
1N/A/*
1N/A** ST_BODYEND -- deal with the last piece of the mail body
1N/A**
1N/A** Parameters:
1N/A** g -- generic argument structure
1N/A**
1N/A** Returns:
1N/A** continue or filter-specified value
1N/A**
1N/A** Side effects:
1N/A** sends a reply for the body part (if non-empty).
1N/A*/
1N/A
1N/Astatic int
1N/Ast_bodyend(g)
1N/A genarg *g;
1N/A{
1N/A sfsistat r;
1N/A sfsistat (*fi_body) __P((SMFICTX *, unsigned char *, size_t));
1N/A sfsistat (*fi_eom) __P((SMFICTX *));
1N/A
1N/A if (g == NULL)
1N/A return _SMFIS_ABORT;
1N/A r = SMFIS_CONTINUE;
1N/A if (g->a_ctx->ctx_smfi != NULL)
1N/A {
1N/A if ((fi_body = g->a_ctx->ctx_smfi->xxfi_body) != NULL &&
1N/A g->a_len > 0)
1N/A {
1N/A socket_t sd;
1N/A struct timeval timeout;
1N/A
1N/A timeout.tv_sec = g->a_ctx->ctx_timeout;
1N/A timeout.tv_usec = 0;
1N/A sd = g->a_ctx->ctx_sd;
1N/A r = (*fi_body)(g->a_ctx, (unsigned char *)g->a_buf,
1N/A g->a_len);
1N/A if (r != SMFIS_CONTINUE &&
1N/A sendreply(r, sd, &timeout, g->a_ctx) != MI_SUCCESS)
1N/A return _SMFIS_ABORT;
1N/A }
1N/A }
1N/A if (r == SMFIS_CONTINUE &&
1N/A (fi_eom = g->a_ctx->ctx_smfi->xxfi_eom) != NULL)
1N/A return (*fi_eom)(g->a_ctx);
1N/A return r;
1N/A}
1N/A
1N/A/*
1N/A** ST_ABORTFCT -- deal with aborts
1N/A**
1N/A** Parameters:
1N/A** g -- generic argument structure
1N/A**
1N/A** Returns:
1N/A** abort or filter-specified value
1N/A*/
1N/A
1N/Astatic int
1N/Ast_abortfct(g)
1N/A genarg *g;
1N/A{
1N/A sfsistat (*fi_abort) __P((SMFICTX *));
1N/A
1N/A if (g == NULL)
1N/A return _SMFIS_ABORT;
1N/A if (g != NULL && g->a_ctx->ctx_smfi != NULL &&
1N/A (fi_abort = g->a_ctx->ctx_smfi->xxfi_abort) != NULL)
1N/A (void) (*fi_abort)(g->a_ctx);
1N/A return _SMFIS_NOREPLY;
1N/A}
1N/A
1N/A/*
1N/A** TRANS_OK -- is the state transition ok?
1N/A**
1N/A** Parameters:
1N/A** old -- old state
1N/A** new -- new state
1N/A**
1N/A** Returns:
1N/A** state transition ok
1N/A*/
1N/A
1N/Astatic bool
1N/Atrans_ok(old, new)
1N/A int old, new;
1N/A{
1N/A int s, n;
1N/A
1N/A s = old;
1N/A if (s >= SIZE_NEXT_STATES)
1N/A return false;
1N/A do
1N/A {
1N/A /* is this state transition allowed? */
1N/A if ((MI_MASK(new) & next_states[s]) != 0)
1N/A return true;
1N/A
1N/A /*
1N/A ** no: try next state;
1N/A ** this works since the relevant states are ordered
1N/A ** strict sequentially
1N/A */
1N/A
1N/A n = s + 1;
1N/A if (n >= SIZE_NEXT_STATES)
1N/A return false;
1N/A
1N/A /*
1N/A ** can we actually "skip" this state?
1N/A ** see fix_stm() which sets this bit for those
1N/A ** states which the filter program is not interested in
1N/A */
1N/A
1N/A if (bitset(NX_SKIP, next_states[n]))
1N/A s = n;
1N/A else
1N/A return false;
1N/A } while (s < SIZE_NEXT_STATES);
1N/A return false;
1N/A}
1N/A
1N/A/*
1N/A** FIX_STM -- add "skip" bits to the state transition table
1N/A**
1N/A** Parameters:
1N/A** ctx -- context structure
1N/A**
1N/A** Returns:
1N/A** None.
1N/A**
1N/A** Side effects:
1N/A** may change state transition table.
1N/A*/
1N/A
1N/Astatic void
1N/Afix_stm(ctx)
1N/A SMFICTX_PTR ctx;
1N/A{
1N/A unsigned long fl;
1N/A
1N/A if (ctx == NULL || ctx->ctx_smfi == NULL)
1N/A return;
1N/A fl = ctx->ctx_pflags;
1N/A if (bitset(SMFIP_NOCONNECT, fl))
1N/A next_states[ST_CONN] |= NX_SKIP;
1N/A if (bitset(SMFIP_NOHELO, fl))
1N/A next_states[ST_HELO] |= NX_SKIP;
1N/A if (bitset(SMFIP_NOMAIL, fl))
1N/A next_states[ST_MAIL] |= NX_SKIP;
1N/A if (bitset(SMFIP_NORCPT, fl))
1N/A next_states[ST_RCPT] |= NX_SKIP;
1N/A if (bitset(SMFIP_NOHDRS, fl))
1N/A next_states[ST_HDRS] |= NX_SKIP;
1N/A if (bitset(SMFIP_NOEOH, fl))
1N/A next_states[ST_EOHS] |= NX_SKIP;
1N/A if (bitset(SMFIP_NOBODY, fl))
1N/A next_states[ST_BODY] |= NX_SKIP;
1N/A if (bitset(SMFIP_NODATA, fl))
1N/A next_states[ST_DATA] |= NX_SKIP;
1N/A if (bitset(SMFIP_NOUNKNOWN, fl))
1N/A next_states[ST_UNKN] |= NX_SKIP;
1N/A}
1N/A
1N/A/*
1N/A** DEC_ARGV -- split a buffer into a list of strings, NULL terminated
1N/A**
1N/A** Parameters:
1N/A** buf -- buffer with several strings
1N/A** len -- length of buffer
1N/A**
1N/A** Returns:
1N/A** array of pointers to the individual strings
1N/A*/
1N/A
1N/Astatic char **
1N/Adec_argv(buf, len)
1N/A char *buf;
1N/A size_t len;
1N/A{
1N/A char **s;
1N/A size_t i;
1N/A int elem, nelem;
1N/A
1N/A nelem = 0;
1N/A for (i = 0; i < len; i++)
1N/A {
1N/A if (buf[i] == '\0')
1N/A ++nelem;
1N/A }
1N/A if (nelem == 0)
1N/A return NULL;
1N/A
1N/A /* last entry is only for the name */
1N/A s = (char **)malloc((nelem + 1) * (sizeof *s));
1N/A if (s == NULL)
1N/A return NULL;
1N/A s[0] = buf;
1N/A for (i = 0, elem = 0; i < len && elem < nelem; i++)
1N/A {
1N/A if (buf[i] == '\0')
1N/A {
1N/A ++elem;
1N/A if (i + 1 >= len)
1N/A s[elem] = NULL;
1N/A else
1N/A s[elem] = &(buf[i + 1]);
1N/A }
1N/A }
1N/A
1N/A /* overwrite last entry (already done above, just paranoia) */
1N/A s[elem] = NULL;
1N/A return s;
1N/A}
1N/A
1N/A/*
1N/A** DEC_ARG2 -- split a buffer into two strings
1N/A**
1N/A** Parameters:
1N/A** buf -- buffer with two strings
1N/A** len -- length of buffer
1N/A** s1,s2 -- pointer to result strings
1N/A**
1N/A** Returns:
1N/A** MI_FAILURE/MI_SUCCESS
1N/A*/
1N/A
1N/Astatic int
1N/Adec_arg2(buf, len, s1, s2)
1N/A char *buf;
1N/A size_t len;
1N/A char **s1;
1N/A char **s2;
1N/A{
1N/A size_t i;
1N/A
1N/A /* paranoia: check for terminating '\0' */
1N/A if (len == 0 || buf[len - 1] != '\0')
1N/A return MI_FAILURE;
1N/A *s1 = buf;
1N/A for (i = 1; i < len && buf[i] != '\0'; i++)
1N/A continue;
1N/A if (i >= len - 1)
1N/A return MI_FAILURE;
1N/A *s2 = buf + i + 1;
1N/A return MI_SUCCESS;
1N/A}
1N/A
1N/A/*
1N/A** SENDOK -- is it ok for the filter to send stuff to the MTA?
1N/A**
1N/A** Parameters:
1N/A** ctx -- context structure
1N/A** flag -- flag to check
1N/A**
1N/A** Returns:
1N/A** sending allowed (in current state)
1N/A*/
1N/A
1N/Abool
1N/Ami_sendok(ctx, flag)
1N/A SMFICTX_PTR ctx;
1N/A int flag;
1N/A{
1N/A if (ctx == NULL || ctx->ctx_smfi == NULL)
1N/A return false;
1N/A
1N/A /* did the milter request this operation? */
1N/A if (flag != 0 && !bitset(flag, ctx->ctx_aflags))
1N/A return false;
1N/A
1N/A /* are we in the correct state? It must be "End of Message". */
1N/A return ctx->ctx_state == ST_ENDM;
1N/A}
1N/A
1N/A#if _FFR_WORKERS_POOL
1N/A/*
1N/A** MI_RD_SOCKET_READY - checks if the socket is ready for read(2)
1N/A**
1N/A** Parameters:
1N/A** sd -- socket_t
1N/A**
1N/A** Returns:
1N/A** true iff socket is ready for read(2)
1N/A*/
1N/A
1N/A#define MI_RD_CMD_TO 1
1N/A#define MI_RD_MAX_ERR 16
1N/A
1N/Astatic bool
1N/Ami_rd_socket_ready (sd)
1N/A socket_t sd;
1N/A{
1N/A int n;
1N/A int nerr = 0;
1N/A#if SM_CONF_POLL
1N/A struct pollfd pfd;
1N/A#else /* SM_CONF_POLL */
1N/A fd_set rd_set, exc_set;
1N/A#endif /* SM_CONF_POLL */
1N/A
1N/A do
1N/A {
1N/A#if SM_CONF_POLL
1N/A pfd.fd = sd;
1N/A pfd.events = POLLIN;
1N/A pfd.revents = 0;
1N/A
1N/A n = poll(&pfd, 1, MI_RD_CMD_TO);
1N/A#else /* SM_CONF_POLL */
1N/A struct timeval timeout;
1N/A
1N/A FD_ZERO(&rd_set);
1N/A FD_ZERO(&exc_set);
1N/A FD_SET(sd, &rd_set);
1N/A FD_SET(sd, &exc_set);
1N/A
1N/A timeout.tv_sec = MI_RD_CMD_TO / 1000;
1N/A timeout.tv_usec = 0;
1N/A n = select(sd + 1, &rd_set, NULL, &exc_set, &timeout);
1N/A#endif /* SM_CONF_POLL */
1N/A
1N/A if (n < 0)
1N/A {
1N/A if (errno == EINTR)
1N/A {
1N/A nerr++;
1N/A continue;
1N/A }
1N/A return true;
1N/A }
1N/A
1N/A if (n == 0)
1N/A return false;
1N/A break;
1N/A } while (nerr < MI_RD_MAX_ERR);
1N/A if (nerr >= MI_RD_MAX_ERR)
1N/A return false;
1N/A
1N/A#if SM_CONF_POLL
1N/A return (pfd.revents != 0);
1N/A#else /* SM_CONF_POLL */
1N/A return FD_ISSET(sd, &rd_set) || FD_ISSET(sd, &exc_set);
1N/A#endif /* SM_CONF_POLL */
1N/A}
1N/A#endif /* _FFR_WORKERS_POOL */