106f25ae9SGregory Neil Shapiro /*
25dd76dd0SGregory Neil Shapiro * Copyright (c) 1999-2004, 2006-2008 Proofpoint, Inc. and its suppliers.
306f25ae9SGregory Neil Shapiro * All rights reserved.
406f25ae9SGregory Neil Shapiro *
506f25ae9SGregory Neil Shapiro * By using this file, you agree to the terms and conditions set
606f25ae9SGregory Neil Shapiro * forth in the LICENSE file which can be found at the top level of
706f25ae9SGregory Neil Shapiro * the sendmail distribution.
806f25ae9SGregory Neil Shapiro *
906f25ae9SGregory Neil Shapiro */
1006f25ae9SGregory Neil Shapiro
1140266059SGregory Neil Shapiro #include <sm/gen.h>
124313cc83SGregory Neil Shapiro SM_RCSID("@(#)$Id: engine.c,v 8.168 2013-11-22 20:51:36 ca Exp $")
1306f25ae9SGregory Neil Shapiro
1406f25ae9SGregory Neil Shapiro #include "libmilter.h"
1506f25ae9SGregory Neil Shapiro
1606f25ae9SGregory Neil Shapiro #if NETINET || NETINET6
1706f25ae9SGregory Neil Shapiro # include <arpa/inet.h>
185b0945b5SGregory Neil Shapiro #endif
1906f25ae9SGregory Neil Shapiro
2006f25ae9SGregory Neil Shapiro /* generic argument for functions in the command table */
2106f25ae9SGregory Neil Shapiro struct arg_struct
2206f25ae9SGregory Neil Shapiro {
2306f25ae9SGregory Neil Shapiro size_t a_len; /* length of buffer */
2406f25ae9SGregory Neil Shapiro char *a_buf; /* argument string */
2506f25ae9SGregory Neil Shapiro int a_idx; /* index for macro array */
2606f25ae9SGregory Neil Shapiro SMFICTX_PTR a_ctx; /* context */
2706f25ae9SGregory Neil Shapiro };
2806f25ae9SGregory Neil Shapiro
2906f25ae9SGregory Neil Shapiro typedef struct arg_struct genarg;
3006f25ae9SGregory Neil Shapiro
3106f25ae9SGregory Neil Shapiro /* structure for commands received from MTA */
3206f25ae9SGregory Neil Shapiro struct cmdfct_t
3306f25ae9SGregory Neil Shapiro {
3406f25ae9SGregory Neil Shapiro char cm_cmd; /* command */
3506f25ae9SGregory Neil Shapiro int cm_argt; /* type of arguments expected */
3606f25ae9SGregory Neil Shapiro int cm_next; /* next state */
3706f25ae9SGregory Neil Shapiro int cm_todo; /* what to do next */
3806f25ae9SGregory Neil Shapiro int cm_macros; /* index for macros */
3906f25ae9SGregory Neil Shapiro int (*cm_fct) __P((genarg *)); /* function to execute */
4006f25ae9SGregory Neil Shapiro };
4106f25ae9SGregory Neil Shapiro
4206f25ae9SGregory Neil Shapiro typedef struct cmdfct_t cmdfct;
4306f25ae9SGregory Neil Shapiro
4406f25ae9SGregory Neil Shapiro /* possible values for cm_argt */
45da7d7b9cSGregory Neil Shapiro #define CM_BUF 0
46da7d7b9cSGregory Neil Shapiro #define CM_NULLOK 1
4706f25ae9SGregory Neil Shapiro
4806f25ae9SGregory Neil Shapiro /* possible values for cm_todo */
4906f25ae9SGregory Neil Shapiro #define CT_CONT 0x0000 /* continue reading commands */
5006f25ae9SGregory Neil Shapiro #define CT_IGNO 0x0001 /* continue even when error */
5106f25ae9SGregory Neil Shapiro
5206f25ae9SGregory Neil Shapiro /* not needed right now, done via return code instead */
5306f25ae9SGregory Neil Shapiro #define CT_KEEP 0x0004 /* keep buffer (contains symbols) */
54d0cef73dSGregory Neil Shapiro #define CT_END 0x0008 /* last command of session, stop replying */
5506f25ae9SGregory Neil Shapiro
5606f25ae9SGregory Neil Shapiro /* index in macro array: macros only for these commands */
5706f25ae9SGregory Neil Shapiro #define CI_NONE (-1)
5806f25ae9SGregory Neil Shapiro #define CI_CONN 0
5906f25ae9SGregory Neil Shapiro #define CI_HELO 1
6006f25ae9SGregory Neil Shapiro #define CI_MAIL 2
6106f25ae9SGregory Neil Shapiro #define CI_RCPT 3
62d0cef73dSGregory Neil Shapiro #define CI_DATA 4
63*d39bd2c1SGregory Neil Shapiro #define CI_EOH 5
64*d39bd2c1SGregory Neil Shapiro #define CI_EOM 6
65*d39bd2c1SGregory Neil Shapiro #define CI_LAST CI_EOM
66d0cef73dSGregory Neil Shapiro #if CI_LAST < CI_DATA
67*d39bd2c1SGregory Neil Shapiro # error "do not compile with CI_LAST < CI_DATA"
68d0cef73dSGregory Neil Shapiro #endif
69d0cef73dSGregory Neil Shapiro #if CI_LAST < CI_EOM
70*d39bd2c1SGregory Neil Shapiro # error "do not compile with CI_LAST < CI_EOM"
71d0cef73dSGregory Neil Shapiro #endif
72d0cef73dSGregory Neil Shapiro #if CI_LAST < CI_EOH
73*d39bd2c1SGregory Neil Shapiro # error "do not compile with CI_LAST < CI_EOH"
74d0cef73dSGregory Neil Shapiro #endif
755b0945b5SGregory Neil Shapiro #if CI_LAST < CI_RCPT
76*d39bd2c1SGregory Neil Shapiro # error "do not compile with CI_LAST < CI_RCPT"
77d0cef73dSGregory Neil Shapiro #endif
785b0945b5SGregory Neil Shapiro #if CI_LAST < CI_MAIL
79*d39bd2c1SGregory Neil Shapiro # error "do not compile with CI_LAST < CI_MAIL"
80d0cef73dSGregory Neil Shapiro #endif
81d0cef73dSGregory Neil Shapiro #if CI_LAST < CI_HELO
82*d39bd2c1SGregory Neil Shapiro # error "do not compile with CI_LAST < CI_HELO"
83d0cef73dSGregory Neil Shapiro #endif
845b0945b5SGregory Neil Shapiro #if CI_LAST < CI_CONN
85*d39bd2c1SGregory Neil Shapiro # error "do not compile with CI_LAST < CI_CONN"
86d0cef73dSGregory Neil Shapiro #endif
87d0cef73dSGregory Neil Shapiro #if CI_LAST >= MAX_MACROS_ENTRIES
88*d39bd2c1SGregory Neil Shapiro # error "do not compile with CI_LAST >= MAX_MACROS_ENTRIES"
89323f6dcbSGregory Neil Shapiro #endif
9006f25ae9SGregory Neil Shapiro
9106f25ae9SGregory Neil Shapiro /* function prototypes */
9206f25ae9SGregory Neil Shapiro static int st_abortfct __P((genarg *));
9306f25ae9SGregory Neil Shapiro static int st_macros __P((genarg *));
9406f25ae9SGregory Neil Shapiro static int st_optionneg __P((genarg *));
9506f25ae9SGregory Neil Shapiro static int st_bodychunk __P((genarg *));
9606f25ae9SGregory Neil Shapiro static int st_connectinfo __P((genarg *));
9706f25ae9SGregory Neil Shapiro static int st_bodyend __P((genarg *));
9806f25ae9SGregory Neil Shapiro static int st_helo __P((genarg *));
9906f25ae9SGregory Neil Shapiro static int st_header __P((genarg *));
10006f25ae9SGregory Neil Shapiro static int st_sender __P((genarg *));
10106f25ae9SGregory Neil Shapiro static int st_rcpt __P((genarg *));
102e92d3f3fSGregory Neil Shapiro static int st_unknown __P((genarg *));
103e92d3f3fSGregory Neil Shapiro static int st_data __P((genarg *));
10406f25ae9SGregory Neil Shapiro static int st_eoh __P((genarg *));
10506f25ae9SGregory Neil Shapiro static int st_quit __P((genarg *));
10606f25ae9SGregory Neil Shapiro static int sendreply __P((sfsistat, socket_t, struct timeval *, SMFICTX_PTR));
10706f25ae9SGregory Neil Shapiro static void fix_stm __P((SMFICTX_PTR));
10806f25ae9SGregory Neil Shapiro static bool trans_ok __P((int, int));
10906f25ae9SGregory Neil Shapiro static char **dec_argv __P((char *, size_t));
11006f25ae9SGregory Neil Shapiro static int dec_arg2 __P((char *, size_t, char **, char **));
1119bd497b8SGregory Neil Shapiro static void mi_clr_symlist __P((SMFICTX_PTR));
11206f25ae9SGregory Neil Shapiro
113d0cef73dSGregory Neil Shapiro #if _FFR_WORKERS_POOL
114d0cef73dSGregory Neil Shapiro static bool mi_rd_socket_ready __P((int));
1155b0945b5SGregory Neil Shapiro #endif
116d0cef73dSGregory Neil Shapiro
11706f25ae9SGregory Neil Shapiro /* states */
11806f25ae9SGregory Neil Shapiro #define ST_NONE (-1)
11906f25ae9SGregory Neil Shapiro #define ST_INIT 0 /* initial state */
12006f25ae9SGregory Neil Shapiro #define ST_OPTS 1 /* option negotiation */
12106f25ae9SGregory Neil Shapiro #define ST_CONN 2 /* connection info */
12206f25ae9SGregory Neil Shapiro #define ST_HELO 3 /* helo */
12306f25ae9SGregory Neil Shapiro #define ST_MAIL 4 /* mail from */
12406f25ae9SGregory Neil Shapiro #define ST_RCPT 5 /* rcpt to */
125e92d3f3fSGregory Neil Shapiro #define ST_DATA 6 /* data */
126e92d3f3fSGregory Neil Shapiro #define ST_HDRS 7 /* headers */
127e92d3f3fSGregory Neil Shapiro #define ST_EOHS 8 /* end of headers */
128e92d3f3fSGregory Neil Shapiro #define ST_BODY 9 /* body */
129e92d3f3fSGregory Neil Shapiro #define ST_ENDM 10 /* end of message */
130e92d3f3fSGregory Neil Shapiro #define ST_QUIT 11 /* quit */
131e92d3f3fSGregory Neil Shapiro #define ST_ABRT 12 /* abort */
132e92d3f3fSGregory Neil Shapiro #define ST_UNKN 13 /* unknown SMTP command */
133d0cef73dSGregory Neil Shapiro #define ST_Q_NC 14 /* quit, new connection follows */
134d0cef73dSGregory Neil Shapiro #define ST_LAST ST_Q_NC /* last valid state */
135d0cef73dSGregory Neil Shapiro #define ST_SKIP 16 /* not a state but required for the state table */
13606f25ae9SGregory Neil Shapiro
13706f25ae9SGregory Neil Shapiro /* in a mail transaction? must be before eom according to spec. */
13806f25ae9SGregory Neil Shapiro #define ST_IN_MAIL(st) ((st) >= ST_MAIL && (st) < ST_ENDM)
13906f25ae9SGregory Neil Shapiro
14006f25ae9SGregory Neil Shapiro /*
14106f25ae9SGregory Neil Shapiro ** set of next states
14206f25ae9SGregory Neil Shapiro ** each state (ST_*) corresponds to bit in an int value (1 << state)
14306f25ae9SGregory Neil Shapiro ** each state has a set of allowed transitions ('or' of bits of states)
14406f25ae9SGregory Neil Shapiro ** so a state transition is valid if the mask of the next state
14506f25ae9SGregory Neil Shapiro ** is set in the NX_* value
14606f25ae9SGregory Neil Shapiro ** this function is coded in trans_ok(), see below.
14706f25ae9SGregory Neil Shapiro */
14840266059SGregory Neil Shapiro
14913bd1963SGregory Neil Shapiro #define MI_MASK(x) (0x0001 << (x)) /* generate a bit "mask" for a state */
15013bd1963SGregory Neil Shapiro #define NX_INIT (MI_MASK(ST_OPTS))
151e92d3f3fSGregory Neil Shapiro #define NX_OPTS (MI_MASK(ST_CONN) | MI_MASK(ST_UNKN))
152e92d3f3fSGregory Neil Shapiro #define NX_CONN (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN))
153e92d3f3fSGregory Neil Shapiro #define NX_HELO (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN))
154e92d3f3fSGregory Neil Shapiro #define NX_MAIL (MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | MI_MASK(ST_UNKN))
155e92d3f3fSGregory Neil Shapiro #define NX_RCPT (MI_MASK(ST_HDRS) | MI_MASK(ST_EOHS) | MI_MASK(ST_DATA) | \
15613bd1963SGregory Neil Shapiro MI_MASK(ST_BODY) | MI_MASK(ST_ENDM) | \
157e92d3f3fSGregory Neil Shapiro MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | MI_MASK(ST_UNKN))
158e92d3f3fSGregory Neil Shapiro #define NX_DATA (MI_MASK(ST_EOHS) | MI_MASK(ST_HDRS) | MI_MASK(ST_ABRT))
15913bd1963SGregory Neil Shapiro #define NX_HDRS (MI_MASK(ST_EOHS) | MI_MASK(ST_HDRS) | MI_MASK(ST_ABRT))
16013bd1963SGregory Neil Shapiro #define NX_EOHS (MI_MASK(ST_BODY) | MI_MASK(ST_ENDM) | MI_MASK(ST_ABRT))
16113bd1963SGregory Neil Shapiro #define NX_BODY (MI_MASK(ST_ENDM) | MI_MASK(ST_BODY) | MI_MASK(ST_ABRT))
162d0cef73dSGregory Neil Shapiro #define NX_ENDM (MI_MASK(ST_QUIT) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN) | \
163d0cef73dSGregory Neil Shapiro MI_MASK(ST_Q_NC))
16406f25ae9SGregory Neil Shapiro #define NX_QUIT 0
16506f25ae9SGregory Neil Shapiro #define NX_ABRT 0
166e92d3f3fSGregory Neil Shapiro #define NX_UNKN (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | \
167e92d3f3fSGregory Neil Shapiro MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | \
168e92d3f3fSGregory Neil Shapiro MI_MASK(ST_DATA) | \
169e92d3f3fSGregory Neil Shapiro MI_MASK(ST_BODY) | MI_MASK(ST_UNKN) | \
170d0cef73dSGregory Neil Shapiro MI_MASK(ST_ABRT) | MI_MASK(ST_QUIT) | MI_MASK(ST_Q_NC))
171d0cef73dSGregory Neil Shapiro #define NX_Q_NC (MI_MASK(ST_CONN) | MI_MASK(ST_UNKN))
17213bd1963SGregory Neil Shapiro #define NX_SKIP MI_MASK(ST_SKIP)
17306f25ae9SGregory Neil Shapiro
17406f25ae9SGregory Neil Shapiro static int next_states[] =
17506f25ae9SGregory Neil Shapiro {
176d0cef73dSGregory Neil Shapiro NX_INIT
177d0cef73dSGregory Neil Shapiro , NX_OPTS
178d0cef73dSGregory Neil Shapiro , NX_CONN
179d0cef73dSGregory Neil Shapiro , NX_HELO
180d0cef73dSGregory Neil Shapiro , NX_MAIL
181d0cef73dSGregory Neil Shapiro , NX_RCPT
182d0cef73dSGregory Neil Shapiro , NX_DATA
183d0cef73dSGregory Neil Shapiro , NX_HDRS
184d0cef73dSGregory Neil Shapiro , NX_EOHS
185d0cef73dSGregory Neil Shapiro , NX_BODY
186d0cef73dSGregory Neil Shapiro , NX_ENDM
187d0cef73dSGregory Neil Shapiro , NX_QUIT
188d0cef73dSGregory Neil Shapiro , NX_ABRT
189d0cef73dSGregory Neil Shapiro , NX_UNKN
190d0cef73dSGregory Neil Shapiro , NX_Q_NC
19106f25ae9SGregory Neil Shapiro };
19206f25ae9SGregory Neil Shapiro
193af9557fdSGregory Neil Shapiro #define SIZE_NEXT_STATES (sizeof(next_states) / sizeof(next_states[0]))
194af9557fdSGregory Neil Shapiro
19506f25ae9SGregory Neil Shapiro /* commands received by milter */
19606f25ae9SGregory Neil Shapiro static cmdfct cmds[] =
19706f25ae9SGregory Neil Shapiro {
198da7d7b9cSGregory Neil Shapiro {SMFIC_ABORT, CM_NULLOK, ST_ABRT, CT_CONT, CI_NONE, st_abortfct}
199da7d7b9cSGregory Neil Shapiro , {SMFIC_MACRO, CM_BUF, ST_NONE, CT_KEEP, CI_NONE, st_macros }
200da7d7b9cSGregory Neil Shapiro , {SMFIC_BODY, CM_BUF, ST_BODY, CT_CONT, CI_NONE, st_bodychunk}
201da7d7b9cSGregory Neil Shapiro , {SMFIC_CONNECT, CM_BUF, ST_CONN, CT_CONT, CI_CONN, st_connectinfo}
202da7d7b9cSGregory Neil Shapiro , {SMFIC_BODYEOB, CM_NULLOK, ST_ENDM, CT_CONT, CI_EOM, st_bodyend }
203da7d7b9cSGregory Neil Shapiro , {SMFIC_HELO, CM_BUF, ST_HELO, CT_CONT, CI_HELO, st_helo }
204da7d7b9cSGregory Neil Shapiro , {SMFIC_HEADER, CM_BUF, ST_HDRS, CT_CONT, CI_NONE, st_header }
205da7d7b9cSGregory Neil Shapiro , {SMFIC_MAIL, CM_BUF, ST_MAIL, CT_CONT, CI_MAIL, st_sender }
206da7d7b9cSGregory Neil Shapiro , {SMFIC_OPTNEG, CM_BUF, ST_OPTS, CT_CONT, CI_NONE, st_optionneg}
207da7d7b9cSGregory Neil Shapiro , {SMFIC_EOH, CM_NULLOK, ST_EOHS, CT_CONT, CI_EOH, st_eoh }
208da7d7b9cSGregory Neil Shapiro , {SMFIC_QUIT, CM_NULLOK, ST_QUIT, CT_END, CI_NONE, st_quit }
209da7d7b9cSGregory Neil Shapiro , {SMFIC_DATA, CM_NULLOK, ST_DATA, CT_CONT, CI_DATA, st_data }
210da7d7b9cSGregory Neil Shapiro , {SMFIC_RCPT, CM_BUF, ST_RCPT, CT_IGNO, CI_RCPT, st_rcpt }
211da7d7b9cSGregory Neil Shapiro , {SMFIC_UNKNOWN, CM_BUF, ST_UNKN, CT_IGNO, CI_NONE, st_unknown }
212da7d7b9cSGregory Neil Shapiro , {SMFIC_QUIT_NC, CM_NULLOK, ST_Q_NC, CT_CONT, CI_NONE, st_quit }
21306f25ae9SGregory Neil Shapiro };
21406f25ae9SGregory Neil Shapiro
215d0cef73dSGregory Neil Shapiro /*
216d0cef73dSGregory Neil Shapiro ** Additional (internal) reply codes;
217d0cef73dSGregory Neil Shapiro ** must be coordinated wit libmilter/mfapi.h
218d0cef73dSGregory Neil Shapiro */
219d0cef73dSGregory Neil Shapiro
22006f25ae9SGregory Neil Shapiro #define _SMFIS_KEEP 20
22106f25ae9SGregory Neil Shapiro #define _SMFIS_ABORT 21
22206f25ae9SGregory Neil Shapiro #define _SMFIS_OPTIONS 22
223d0cef73dSGregory Neil Shapiro #define _SMFIS_NOREPLY SMFIS_NOREPLY
22406f25ae9SGregory Neil Shapiro #define _SMFIS_FAIL (-1)
22540266059SGregory Neil Shapiro #define _SMFIS_NONE (-2)
22606f25ae9SGregory Neil Shapiro
22740266059SGregory Neil Shapiro /*
22806f25ae9SGregory Neil Shapiro ** MI_ENGINE -- receive commands and process them
22906f25ae9SGregory Neil Shapiro **
23006f25ae9SGregory Neil Shapiro ** Parameters:
23106f25ae9SGregory Neil Shapiro ** ctx -- context structure
23206f25ae9SGregory Neil Shapiro **
23306f25ae9SGregory Neil Shapiro ** Returns:
23406f25ae9SGregory Neil Shapiro ** MI_FAILURE/MI_SUCCESS
23506f25ae9SGregory Neil Shapiro */
236d0cef73dSGregory Neil Shapiro
23706f25ae9SGregory Neil Shapiro int
mi_engine(ctx)23806f25ae9SGregory Neil Shapiro mi_engine(ctx)
23906f25ae9SGregory Neil Shapiro SMFICTX_PTR ctx;
24006f25ae9SGregory Neil Shapiro {
24106f25ae9SGregory Neil Shapiro size_t len;
24206f25ae9SGregory Neil Shapiro int i;
24306f25ae9SGregory Neil Shapiro socket_t sd;
24406f25ae9SGregory Neil Shapiro int ret = MI_SUCCESS;
24506f25ae9SGregory Neil Shapiro int ncmds = sizeof(cmds) / sizeof(cmdfct);
24606f25ae9SGregory Neil Shapiro int curstate = ST_INIT;
24706f25ae9SGregory Neil Shapiro int newstate;
24806f25ae9SGregory Neil Shapiro bool call_abort;
24906f25ae9SGregory Neil Shapiro sfsistat r;
25006f25ae9SGregory Neil Shapiro char cmd;
25106f25ae9SGregory Neil Shapiro char *buf = NULL;
25206f25ae9SGregory Neil Shapiro genarg arg;
25306f25ae9SGregory Neil Shapiro struct timeval timeout;
25406f25ae9SGregory Neil Shapiro int (*f) __P((genarg *));
25506f25ae9SGregory Neil Shapiro sfsistat (*fi_abort) __P((SMFICTX *));
25606f25ae9SGregory Neil Shapiro sfsistat (*fi_close) __P((SMFICTX *));
25706f25ae9SGregory Neil Shapiro
25806f25ae9SGregory Neil Shapiro arg.a_ctx = ctx;
25906f25ae9SGregory Neil Shapiro sd = ctx->ctx_sd;
26006f25ae9SGregory Neil Shapiro fi_abort = ctx->ctx_smfi->xxfi_abort;
261d0cef73dSGregory Neil Shapiro #if _FFR_WORKERS_POOL
262d0cef73dSGregory Neil Shapiro curstate = ctx->ctx_state;
263d0cef73dSGregory Neil Shapiro if (curstate == ST_INIT)
264d0cef73dSGregory Neil Shapiro {
26506f25ae9SGregory Neil Shapiro mi_clr_macros(ctx, 0);
26606f25ae9SGregory Neil Shapiro fix_stm(ctx);
267d0cef73dSGregory Neil Shapiro }
268d0cef73dSGregory Neil Shapiro #else /* _FFR_WORKERS_POOL */
269d0cef73dSGregory Neil Shapiro mi_clr_macros(ctx, 0);
270d0cef73dSGregory Neil Shapiro fix_stm(ctx);
271d0cef73dSGregory Neil Shapiro #endif /* _FFR_WORKERS_POOL */
27240266059SGregory Neil Shapiro r = _SMFIS_NONE;
27342e5d165SGregory Neil Shapiro do
27442e5d165SGregory Neil Shapiro {
27506f25ae9SGregory Neil Shapiro /* call abort only if in a mail transaction */
27606f25ae9SGregory Neil Shapiro call_abort = ST_IN_MAIL(curstate);
27706f25ae9SGregory Neil Shapiro timeout.tv_sec = ctx->ctx_timeout;
27806f25ae9SGregory Neil Shapiro timeout.tv_usec = 0;
27906f25ae9SGregory Neil Shapiro if (mi_stop() == MILTER_ABRT)
28006f25ae9SGregory Neil Shapiro {
28106f25ae9SGregory Neil Shapiro if (ctx->ctx_dbg > 3)
2826f9c8e5bSGregory Neil Shapiro sm_dprintf("[%lu] milter_abort\n",
283d0cef73dSGregory Neil Shapiro (long) ctx->ctx_id);
28406f25ae9SGregory Neil Shapiro ret = MI_FAILURE;
28506f25ae9SGregory Neil Shapiro break;
28606f25ae9SGregory Neil Shapiro }
28740266059SGregory Neil Shapiro
28840266059SGregory Neil Shapiro /*
28940266059SGregory Neil Shapiro ** Notice: buf is allocated by mi_rd_cmd() and it will
29040266059SGregory Neil Shapiro ** usually be free()d after it has been used in f().
29140266059SGregory Neil Shapiro ** However, if the function returns _SMFIS_KEEP then buf
29240266059SGregory Neil Shapiro ** contains macros and will not be free()d.
29340266059SGregory Neil Shapiro ** Hence r must be set to _SMFIS_NONE if a new buf is
29440266059SGregory Neil Shapiro ** allocated to avoid problem with housekeeping, esp.
29540266059SGregory Neil Shapiro ** if the code "break"s out of the loop.
29640266059SGregory Neil Shapiro */
29740266059SGregory Neil Shapiro
298d0cef73dSGregory Neil Shapiro #if _FFR_WORKERS_POOL
299d0cef73dSGregory Neil Shapiro /* Is the socket ready to be read ??? */
300d0cef73dSGregory Neil Shapiro if (!mi_rd_socket_ready(sd))
301d0cef73dSGregory Neil Shapiro {
302d0cef73dSGregory Neil Shapiro ret = MI_CONTINUE;
303d0cef73dSGregory Neil Shapiro break;
304d0cef73dSGregory Neil Shapiro }
305d0cef73dSGregory Neil Shapiro #endif /* _FFR_WORKERS_POOL */
306d0cef73dSGregory Neil Shapiro
30740266059SGregory Neil Shapiro r = _SMFIS_NONE;
30806f25ae9SGregory Neil Shapiro if ((buf = mi_rd_cmd(sd, &timeout, &cmd, &len,
30906f25ae9SGregory Neil Shapiro ctx->ctx_smfi->xxfi_name)) == NULL &&
31006f25ae9SGregory Neil Shapiro cmd < SMFIC_VALIDCMD)
31106f25ae9SGregory Neil Shapiro {
31206f25ae9SGregory Neil Shapiro if (ctx->ctx_dbg > 5)
3136f9c8e5bSGregory Neil Shapiro sm_dprintf("[%lu] mi_engine: mi_rd_cmd error (%x)\n",
314d0cef73dSGregory Neil Shapiro (long) ctx->ctx_id, (int) cmd);
31506f25ae9SGregory Neil Shapiro
31606f25ae9SGregory Neil Shapiro /*
31706f25ae9SGregory Neil Shapiro ** eof is currently treated as failure ->
31806f25ae9SGregory Neil Shapiro ** abort() instead of close(), otherwise use:
31906f25ae9SGregory Neil Shapiro ** if (cmd != SMFIC_EOF)
32006f25ae9SGregory Neil Shapiro */
32106f25ae9SGregory Neil Shapiro
32206f25ae9SGregory Neil Shapiro ret = MI_FAILURE;
32306f25ae9SGregory Neil Shapiro break;
32406f25ae9SGregory Neil Shapiro }
32506f25ae9SGregory Neil Shapiro if (ctx->ctx_dbg > 4)
3266f9c8e5bSGregory Neil Shapiro sm_dprintf("[%lu] got cmd '%c' len %d\n",
327d0cef73dSGregory Neil Shapiro (long) ctx->ctx_id, cmd, (int) len);
32806f25ae9SGregory Neil Shapiro for (i = 0; i < ncmds; i++)
32906f25ae9SGregory Neil Shapiro {
33006f25ae9SGregory Neil Shapiro if (cmd == cmds[i].cm_cmd)
33106f25ae9SGregory Neil Shapiro break;
33206f25ae9SGregory Neil Shapiro }
33306f25ae9SGregory Neil Shapiro if (i >= ncmds)
33406f25ae9SGregory Neil Shapiro {
33506f25ae9SGregory Neil Shapiro /* unknown command */
33606f25ae9SGregory Neil Shapiro if (ctx->ctx_dbg > 1)
3376f9c8e5bSGregory Neil Shapiro sm_dprintf("[%lu] cmd '%c' unknown\n",
338d0cef73dSGregory Neil Shapiro (long) ctx->ctx_id, cmd);
33906f25ae9SGregory Neil Shapiro ret = MI_FAILURE;
34006f25ae9SGregory Neil Shapiro break;
34106f25ae9SGregory Neil Shapiro }
34206f25ae9SGregory Neil Shapiro if ((f = cmds[i].cm_fct) == NULL)
34306f25ae9SGregory Neil Shapiro {
34406f25ae9SGregory Neil Shapiro /* stop for now */
34506f25ae9SGregory Neil Shapiro if (ctx->ctx_dbg > 1)
3466f9c8e5bSGregory Neil Shapiro sm_dprintf("[%lu] cmd '%c' not impl\n",
347d0cef73dSGregory Neil Shapiro (long) ctx->ctx_id, cmd);
34806f25ae9SGregory Neil Shapiro ret = MI_FAILURE;
34906f25ae9SGregory Neil Shapiro break;
35006f25ae9SGregory Neil Shapiro }
35106f25ae9SGregory Neil Shapiro
35206f25ae9SGregory Neil Shapiro /* is new state ok? */
35306f25ae9SGregory Neil Shapiro newstate = cmds[i].cm_next;
35406f25ae9SGregory Neil Shapiro if (ctx->ctx_dbg > 5)
3556f9c8e5bSGregory Neil Shapiro sm_dprintf("[%lu] cur %x new %x nextmask %x\n",
356d0cef73dSGregory Neil Shapiro (long) ctx->ctx_id,
35706f25ae9SGregory Neil Shapiro curstate, newstate, next_states[curstate]);
35806f25ae9SGregory Neil Shapiro
35906f25ae9SGregory Neil Shapiro if (newstate != ST_NONE && !trans_ok(curstate, newstate))
36006f25ae9SGregory Neil Shapiro {
36106f25ae9SGregory Neil Shapiro if (ctx->ctx_dbg > 1)
3626f9c8e5bSGregory Neil Shapiro sm_dprintf("[%lu] abort: cur %d (%x) new %d (%x) next %x\n",
363d0cef73dSGregory Neil Shapiro (long) ctx->ctx_id,
36413bd1963SGregory Neil Shapiro curstate, MI_MASK(curstate),
36513bd1963SGregory Neil Shapiro newstate, MI_MASK(newstate),
36606f25ae9SGregory Neil Shapiro next_states[curstate]);
36706f25ae9SGregory Neil Shapiro
36806f25ae9SGregory Neil Shapiro /* call abort only if in a mail transaction */
36906f25ae9SGregory Neil Shapiro if (fi_abort != NULL && call_abort)
37006f25ae9SGregory Neil Shapiro (void) (*fi_abort)(ctx);
37106f25ae9SGregory Neil Shapiro
37206f25ae9SGregory Neil Shapiro /*
37306f25ae9SGregory Neil Shapiro ** try to reach the new state from HELO
37406f25ae9SGregory Neil Shapiro ** if it can't be reached, ignore the command.
37506f25ae9SGregory Neil Shapiro */
37606f25ae9SGregory Neil Shapiro
37706f25ae9SGregory Neil Shapiro curstate = ST_HELO;
37806f25ae9SGregory Neil Shapiro if (!trans_ok(curstate, newstate))
37940266059SGregory Neil Shapiro {
380959366dcSGregory Neil Shapiro if (buf != NULL)
381959366dcSGregory Neil Shapiro {
38240266059SGregory Neil Shapiro free(buf);
38340266059SGregory Neil Shapiro buf = NULL;
384959366dcSGregory Neil Shapiro }
38506f25ae9SGregory Neil Shapiro continue;
38606f25ae9SGregory Neil Shapiro }
38740266059SGregory Neil Shapiro }
388da7d7b9cSGregory Neil Shapiro if (cmds[i].cm_argt != CM_NULLOK && buf == NULL)
389da7d7b9cSGregory Neil Shapiro {
390da7d7b9cSGregory Neil Shapiro /* stop for now */
391da7d7b9cSGregory Neil Shapiro if (ctx->ctx_dbg > 1)
392da7d7b9cSGregory Neil Shapiro sm_dprintf("[%lu] cmd='%c', buf=NULL\n",
393da7d7b9cSGregory Neil Shapiro (long) ctx->ctx_id, cmd);
394da7d7b9cSGregory Neil Shapiro ret = MI_FAILURE;
395da7d7b9cSGregory Neil Shapiro break;
396da7d7b9cSGregory Neil Shapiro }
39706f25ae9SGregory Neil Shapiro arg.a_len = len;
39806f25ae9SGregory Neil Shapiro arg.a_buf = buf;
39906f25ae9SGregory Neil Shapiro if (newstate != ST_NONE)
40006f25ae9SGregory Neil Shapiro {
40106f25ae9SGregory Neil Shapiro curstate = newstate;
40206f25ae9SGregory Neil Shapiro ctx->ctx_state = curstate;
40306f25ae9SGregory Neil Shapiro }
40406f25ae9SGregory Neil Shapiro arg.a_idx = cmds[i].cm_macros;
405b6bacd31SGregory Neil Shapiro call_abort = ST_IN_MAIL(curstate);
40606f25ae9SGregory Neil Shapiro
40706f25ae9SGregory Neil Shapiro /* call function to deal with command */
408d0cef73dSGregory Neil Shapiro MI_MONITOR_BEGIN(ctx, cmd);
40906f25ae9SGregory Neil Shapiro r = (*f)(&arg);
410d0cef73dSGregory Neil Shapiro MI_MONITOR_END(ctx, cmd);
41106f25ae9SGregory Neil Shapiro if (r != _SMFIS_KEEP && buf != NULL)
41206f25ae9SGregory Neil Shapiro {
41306f25ae9SGregory Neil Shapiro free(buf);
41406f25ae9SGregory Neil Shapiro buf = NULL;
41506f25ae9SGregory Neil Shapiro }
41606f25ae9SGregory Neil Shapiro if (sendreply(r, sd, &timeout, ctx) != MI_SUCCESS)
41706f25ae9SGregory Neil Shapiro {
41806f25ae9SGregory Neil Shapiro ret = MI_FAILURE;
41906f25ae9SGregory Neil Shapiro break;
42006f25ae9SGregory Neil Shapiro }
42106f25ae9SGregory Neil Shapiro
42206f25ae9SGregory Neil Shapiro if (r == SMFIS_ACCEPT)
42306f25ae9SGregory Neil Shapiro {
42406f25ae9SGregory Neil Shapiro /* accept mail, no further actions taken */
42506f25ae9SGregory Neil Shapiro curstate = ST_HELO;
42606f25ae9SGregory Neil Shapiro }
42706f25ae9SGregory Neil Shapiro else if (r == SMFIS_REJECT || r == SMFIS_DISCARD ||
42806f25ae9SGregory Neil Shapiro r == SMFIS_TEMPFAIL)
42906f25ae9SGregory Neil Shapiro {
43006f25ae9SGregory Neil Shapiro /*
43106f25ae9SGregory Neil Shapiro ** further actions depend on current state
43206f25ae9SGregory Neil Shapiro ** if the IGNO bit is set: "ignore" the error,
43306f25ae9SGregory Neil Shapiro ** i.e., stay in the current state
43406f25ae9SGregory Neil Shapiro */
43506f25ae9SGregory Neil Shapiro if (!bitset(CT_IGNO, cmds[i].cm_todo))
43606f25ae9SGregory Neil Shapiro curstate = ST_HELO;
43706f25ae9SGregory Neil Shapiro }
43806f25ae9SGregory Neil Shapiro else if (r == _SMFIS_ABORT)
43906f25ae9SGregory Neil Shapiro {
44006f25ae9SGregory Neil Shapiro if (ctx->ctx_dbg > 5)
4416f9c8e5bSGregory Neil Shapiro sm_dprintf("[%lu] function returned abort\n",
442d0cef73dSGregory Neil Shapiro (long) ctx->ctx_id);
44306f25ae9SGregory Neil Shapiro ret = MI_FAILURE;
44406f25ae9SGregory Neil Shapiro break;
44506f25ae9SGregory Neil Shapiro }
44606f25ae9SGregory Neil Shapiro } while (!bitset(CT_END, cmds[i].cm_todo));
44706f25ae9SGregory Neil Shapiro
448d0cef73dSGregory Neil Shapiro ctx->ctx_state = curstate;
449d0cef73dSGregory Neil Shapiro
450d0cef73dSGregory Neil Shapiro if (ret == MI_FAILURE)
45106f25ae9SGregory Neil Shapiro {
45206f25ae9SGregory Neil Shapiro /* call abort only if in a mail transaction */
45306f25ae9SGregory Neil Shapiro if (fi_abort != NULL && call_abort)
45406f25ae9SGregory Neil Shapiro (void) (*fi_abort)(ctx);
45506f25ae9SGregory Neil Shapiro }
45606f25ae9SGregory Neil Shapiro
457d0cef73dSGregory Neil Shapiro /* has close been called? */
458d0cef73dSGregory Neil Shapiro if (ctx->ctx_state != ST_QUIT
459d0cef73dSGregory Neil Shapiro #if _FFR_WORKERS_POOL
460d0cef73dSGregory Neil Shapiro && ret != MI_CONTINUE
4615b0945b5SGregory Neil Shapiro #endif
462d0cef73dSGregory Neil Shapiro )
463d0cef73dSGregory Neil Shapiro {
46406f25ae9SGregory Neil Shapiro if ((fi_close = ctx->ctx_smfi->xxfi_close) != NULL)
46506f25ae9SGregory Neil Shapiro (void) (*fi_close)(ctx);
466d0cef73dSGregory Neil Shapiro }
46740266059SGregory Neil Shapiro if (r != _SMFIS_KEEP && buf != NULL)
46806f25ae9SGregory Neil Shapiro free(buf);
469d0cef73dSGregory Neil Shapiro #if !_FFR_WORKERS_POOL
47006f25ae9SGregory Neil Shapiro mi_clr_macros(ctx, 0);
4715b0945b5SGregory Neil Shapiro #endif
47206f25ae9SGregory Neil Shapiro return ret;
47306f25ae9SGregory Neil Shapiro }
474d0cef73dSGregory Neil Shapiro
475d0cef73dSGregory Neil Shapiro static size_t milter_addsymlist __P((SMFICTX_PTR, char *, char **));
476d0cef73dSGregory Neil Shapiro
477d0cef73dSGregory Neil Shapiro static size_t
milter_addsymlist(ctx,buf,newbuf)478d0cef73dSGregory Neil Shapiro milter_addsymlist(ctx, buf, newbuf)
479d0cef73dSGregory Neil Shapiro SMFICTX_PTR ctx;
480d0cef73dSGregory Neil Shapiro char *buf;
481d0cef73dSGregory Neil Shapiro char **newbuf;
482d0cef73dSGregory Neil Shapiro {
483d0cef73dSGregory Neil Shapiro size_t len;
484d0cef73dSGregory Neil Shapiro int i;
485d0cef73dSGregory Neil Shapiro mi_int32 v;
486d0cef73dSGregory Neil Shapiro char *buffer;
487d0cef73dSGregory Neil Shapiro
488d0cef73dSGregory Neil Shapiro SM_ASSERT(ctx != NULL);
489d0cef73dSGregory Neil Shapiro SM_ASSERT(buf != NULL);
490d0cef73dSGregory Neil Shapiro SM_ASSERT(newbuf != NULL);
491d0cef73dSGregory Neil Shapiro len = 0;
492d0cef73dSGregory Neil Shapiro for (i = 0; i < MAX_MACROS_ENTRIES; i++)
493d0cef73dSGregory Neil Shapiro {
494d0cef73dSGregory Neil Shapiro if (ctx->ctx_mac_list[i] != NULL)
495d0cef73dSGregory Neil Shapiro {
496d0cef73dSGregory Neil Shapiro len += strlen(ctx->ctx_mac_list[i]) + 1 +
497d0cef73dSGregory Neil Shapiro MILTER_LEN_BYTES;
498d0cef73dSGregory Neil Shapiro }
499d0cef73dSGregory Neil Shapiro }
500d0cef73dSGregory Neil Shapiro if (len > 0)
501d0cef73dSGregory Neil Shapiro {
502d0cef73dSGregory Neil Shapiro size_t offset;
503d0cef73dSGregory Neil Shapiro
504d0cef73dSGregory Neil Shapiro SM_ASSERT(len + MILTER_OPTLEN > len);
505d0cef73dSGregory Neil Shapiro len += MILTER_OPTLEN;
506d0cef73dSGregory Neil Shapiro buffer = malloc(len);
507d0cef73dSGregory Neil Shapiro if (buffer != NULL)
508d0cef73dSGregory Neil Shapiro {
509d0cef73dSGregory Neil Shapiro (void) memcpy(buffer, buf, MILTER_OPTLEN);
510d0cef73dSGregory Neil Shapiro offset = MILTER_OPTLEN;
511d0cef73dSGregory Neil Shapiro for (i = 0; i < MAX_MACROS_ENTRIES; i++)
512d0cef73dSGregory Neil Shapiro {
513d0cef73dSGregory Neil Shapiro size_t l;
514d0cef73dSGregory Neil Shapiro
515d0cef73dSGregory Neil Shapiro if (ctx->ctx_mac_list[i] == NULL)
516d0cef73dSGregory Neil Shapiro continue;
517d0cef73dSGregory Neil Shapiro
518d0cef73dSGregory Neil Shapiro SM_ASSERT(offset + MILTER_LEN_BYTES < len);
519d0cef73dSGregory Neil Shapiro v = htonl(i);
520d0cef73dSGregory Neil Shapiro (void) memcpy(buffer + offset, (void *) &v,
521d0cef73dSGregory Neil Shapiro MILTER_LEN_BYTES);
522d0cef73dSGregory Neil Shapiro offset += MILTER_LEN_BYTES;
523d0cef73dSGregory Neil Shapiro l = strlen(ctx->ctx_mac_list[i]) + 1;
524d0cef73dSGregory Neil Shapiro SM_ASSERT(offset + l <= len);
525d0cef73dSGregory Neil Shapiro (void) memcpy(buffer + offset,
526d0cef73dSGregory Neil Shapiro ctx->ctx_mac_list[i], l);
527d0cef73dSGregory Neil Shapiro offset += l;
528d0cef73dSGregory Neil Shapiro }
529d0cef73dSGregory Neil Shapiro }
530d0cef73dSGregory Neil Shapiro else
531d0cef73dSGregory Neil Shapiro {
532d0cef73dSGregory Neil Shapiro /* oops ... */
533d0cef73dSGregory Neil Shapiro }
534d0cef73dSGregory Neil Shapiro }
535d0cef73dSGregory Neil Shapiro else
536d0cef73dSGregory Neil Shapiro {
537d0cef73dSGregory Neil Shapiro len = MILTER_OPTLEN;
538d0cef73dSGregory Neil Shapiro buffer = buf;
539d0cef73dSGregory Neil Shapiro }
540d0cef73dSGregory Neil Shapiro *newbuf = buffer;
541d0cef73dSGregory Neil Shapiro return len;
542d0cef73dSGregory Neil Shapiro }
543d0cef73dSGregory Neil Shapiro
544d0cef73dSGregory Neil Shapiro /*
545d0cef73dSGregory Neil Shapiro ** GET_NR_BIT -- get "no reply" bit matching state
546d0cef73dSGregory Neil Shapiro **
547d0cef73dSGregory Neil Shapiro ** Parameters:
548d0cef73dSGregory Neil Shapiro ** state -- current protocol stage
549d0cef73dSGregory Neil Shapiro **
550d0cef73dSGregory Neil Shapiro ** Returns:
551d0cef73dSGregory Neil Shapiro ** 0: no matching bit
552d0cef73dSGregory Neil Shapiro ** >0: the matching "no reply" bit
553d0cef73dSGregory Neil Shapiro */
554d0cef73dSGregory Neil Shapiro
555d0cef73dSGregory Neil Shapiro static unsigned long get_nr_bit __P((int));
556d0cef73dSGregory Neil Shapiro
557d0cef73dSGregory Neil Shapiro static unsigned long
get_nr_bit(state)558d0cef73dSGregory Neil Shapiro get_nr_bit(state)
559d0cef73dSGregory Neil Shapiro int state;
560d0cef73dSGregory Neil Shapiro {
561d0cef73dSGregory Neil Shapiro unsigned long bit;
562d0cef73dSGregory Neil Shapiro
563d0cef73dSGregory Neil Shapiro switch (state)
564d0cef73dSGregory Neil Shapiro {
565d0cef73dSGregory Neil Shapiro case ST_CONN:
566d0cef73dSGregory Neil Shapiro bit = SMFIP_NR_CONN;
567d0cef73dSGregory Neil Shapiro break;
568d0cef73dSGregory Neil Shapiro case ST_HELO:
569d0cef73dSGregory Neil Shapiro bit = SMFIP_NR_HELO;
570d0cef73dSGregory Neil Shapiro break;
571d0cef73dSGregory Neil Shapiro case ST_MAIL:
572d0cef73dSGregory Neil Shapiro bit = SMFIP_NR_MAIL;
573d0cef73dSGregory Neil Shapiro break;
574d0cef73dSGregory Neil Shapiro case ST_RCPT:
575d0cef73dSGregory Neil Shapiro bit = SMFIP_NR_RCPT;
576d0cef73dSGregory Neil Shapiro break;
577d0cef73dSGregory Neil Shapiro case ST_DATA:
578d0cef73dSGregory Neil Shapiro bit = SMFIP_NR_DATA;
579d0cef73dSGregory Neil Shapiro break;
580d0cef73dSGregory Neil Shapiro case ST_UNKN:
581d0cef73dSGregory Neil Shapiro bit = SMFIP_NR_UNKN;
582d0cef73dSGregory Neil Shapiro break;
583d0cef73dSGregory Neil Shapiro case ST_HDRS:
584d0cef73dSGregory Neil Shapiro bit = SMFIP_NR_HDR;
585d0cef73dSGregory Neil Shapiro break;
586d0cef73dSGregory Neil Shapiro case ST_EOHS:
587d0cef73dSGregory Neil Shapiro bit = SMFIP_NR_EOH;
588d0cef73dSGregory Neil Shapiro break;
589d0cef73dSGregory Neil Shapiro case ST_BODY:
590d0cef73dSGregory Neil Shapiro bit = SMFIP_NR_BODY;
591d0cef73dSGregory Neil Shapiro break;
592d0cef73dSGregory Neil Shapiro default:
593d0cef73dSGregory Neil Shapiro bit = 0;
594d0cef73dSGregory Neil Shapiro break;
595d0cef73dSGregory Neil Shapiro }
596d0cef73dSGregory Neil Shapiro return bit;
597d0cef73dSGregory Neil Shapiro }
598d0cef73dSGregory Neil Shapiro
59940266059SGregory Neil Shapiro /*
60006f25ae9SGregory Neil Shapiro ** SENDREPLY -- send a reply to the MTA
60106f25ae9SGregory Neil Shapiro **
60206f25ae9SGregory Neil Shapiro ** Parameters:
60306f25ae9SGregory Neil Shapiro ** r -- reply code
60406f25ae9SGregory Neil Shapiro ** sd -- socket descriptor
60506f25ae9SGregory Neil Shapiro ** timeout_ptr -- (ptr to) timeout to use for sending
60606f25ae9SGregory Neil Shapiro ** ctx -- context structure
60706f25ae9SGregory Neil Shapiro **
60806f25ae9SGregory Neil Shapiro ** Returns:
60906f25ae9SGregory Neil Shapiro ** MI_SUCCESS/MI_FAILURE
61006f25ae9SGregory Neil Shapiro */
61106f25ae9SGregory Neil Shapiro
61206f25ae9SGregory Neil Shapiro static int
sendreply(r,sd,timeout_ptr,ctx)61306f25ae9SGregory Neil Shapiro sendreply(r, sd, timeout_ptr, ctx)
61406f25ae9SGregory Neil Shapiro sfsistat r;
61506f25ae9SGregory Neil Shapiro socket_t sd;
61606f25ae9SGregory Neil Shapiro struct timeval *timeout_ptr;
61706f25ae9SGregory Neil Shapiro SMFICTX_PTR ctx;
61806f25ae9SGregory Neil Shapiro {
619d0cef73dSGregory Neil Shapiro int ret;
620d0cef73dSGregory Neil Shapiro unsigned long bit;
621d0cef73dSGregory Neil Shapiro
622d0cef73dSGregory Neil Shapiro ret = MI_SUCCESS;
623d0cef73dSGregory Neil Shapiro
624d0cef73dSGregory Neil Shapiro bit = get_nr_bit(ctx->ctx_state);
625d0cef73dSGregory Neil Shapiro if (bit != 0 && (ctx->ctx_pflags & bit) != 0 && r != SMFIS_NOREPLY)
626d0cef73dSGregory Neil Shapiro {
627d0cef73dSGregory Neil Shapiro if (r >= SMFIS_CONTINUE && r < _SMFIS_KEEP)
628d0cef73dSGregory Neil Shapiro {
629d0cef73dSGregory Neil Shapiro /* milter said it wouldn't reply, but it lied... */
630d0cef73dSGregory Neil Shapiro smi_log(SMI_LOG_ERR,
6315b0945b5SGregory Neil Shapiro "%s: milter claimed not to reply in state %d but did anyway %d",
632d0cef73dSGregory Neil Shapiro ctx->ctx_smfi->xxfi_name,
633d0cef73dSGregory Neil Shapiro ctx->ctx_state, r);
634d0cef73dSGregory Neil Shapiro
635d0cef73dSGregory Neil Shapiro }
636d0cef73dSGregory Neil Shapiro
637d0cef73dSGregory Neil Shapiro /*
638d0cef73dSGregory Neil Shapiro ** Force specified behavior, otherwise libmilter
639d0cef73dSGregory Neil Shapiro ** and MTA will fail to communicate properly.
640d0cef73dSGregory Neil Shapiro */
641d0cef73dSGregory Neil Shapiro
642d0cef73dSGregory Neil Shapiro switch (r)
643d0cef73dSGregory Neil Shapiro {
644d0cef73dSGregory Neil Shapiro case SMFIS_CONTINUE:
645d0cef73dSGregory Neil Shapiro case SMFIS_TEMPFAIL:
646d0cef73dSGregory Neil Shapiro case SMFIS_REJECT:
647d0cef73dSGregory Neil Shapiro case SMFIS_DISCARD:
648d0cef73dSGregory Neil Shapiro case SMFIS_ACCEPT:
649d0cef73dSGregory Neil Shapiro case SMFIS_SKIP:
650d0cef73dSGregory Neil Shapiro case _SMFIS_OPTIONS:
651d0cef73dSGregory Neil Shapiro r = SMFIS_NOREPLY;
652d0cef73dSGregory Neil Shapiro break;
653d0cef73dSGregory Neil Shapiro }
654d0cef73dSGregory Neil Shapiro }
65506f25ae9SGregory Neil Shapiro
65606f25ae9SGregory Neil Shapiro switch (r)
65706f25ae9SGregory Neil Shapiro {
65806f25ae9SGregory Neil Shapiro case SMFIS_CONTINUE:
65906f25ae9SGregory Neil Shapiro ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_CONTINUE, NULL, 0);
66006f25ae9SGregory Neil Shapiro break;
66106f25ae9SGregory Neil Shapiro case SMFIS_TEMPFAIL:
66206f25ae9SGregory Neil Shapiro case SMFIS_REJECT:
663605302a5SGregory Neil Shapiro if (ctx->ctx_reply != NULL &&
664605302a5SGregory Neil Shapiro ((r == SMFIS_TEMPFAIL && *ctx->ctx_reply == '4') ||
665605302a5SGregory Neil Shapiro (r == SMFIS_REJECT && *ctx->ctx_reply == '5')))
66606f25ae9SGregory Neil Shapiro {
66706f25ae9SGregory Neil Shapiro ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_REPLYCODE,
66806f25ae9SGregory Neil Shapiro ctx->ctx_reply,
66906f25ae9SGregory Neil Shapiro strlen(ctx->ctx_reply) + 1);
67006f25ae9SGregory Neil Shapiro free(ctx->ctx_reply);
67106f25ae9SGregory Neil Shapiro ctx->ctx_reply = NULL;
67206f25ae9SGregory Neil Shapiro }
67306f25ae9SGregory Neil Shapiro else
67406f25ae9SGregory Neil Shapiro {
67506f25ae9SGregory Neil Shapiro ret = mi_wr_cmd(sd, timeout_ptr, r == SMFIS_REJECT ?
67606f25ae9SGregory Neil Shapiro SMFIR_REJECT : SMFIR_TEMPFAIL, NULL, 0);
67706f25ae9SGregory Neil Shapiro }
67806f25ae9SGregory Neil Shapiro break;
67906f25ae9SGregory Neil Shapiro case SMFIS_DISCARD:
68006f25ae9SGregory Neil Shapiro ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_DISCARD, NULL, 0);
68106f25ae9SGregory Neil Shapiro break;
68206f25ae9SGregory Neil Shapiro case SMFIS_ACCEPT:
68306f25ae9SGregory Neil Shapiro ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_ACCEPT, NULL, 0);
68406f25ae9SGregory Neil Shapiro break;
685d0cef73dSGregory Neil Shapiro case SMFIS_SKIP:
686d0cef73dSGregory Neil Shapiro ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_SKIP, NULL, 0);
687d0cef73dSGregory Neil Shapiro break;
68806f25ae9SGregory Neil Shapiro case _SMFIS_OPTIONS:
68906f25ae9SGregory Neil Shapiro {
69006f25ae9SGregory Neil Shapiro mi_int32 v;
691d0cef73dSGregory Neil Shapiro size_t len;
692d0cef73dSGregory Neil Shapiro char *buffer;
693d0cef73dSGregory Neil Shapiro char buf[MILTER_OPTLEN];
69406f25ae9SGregory Neil Shapiro
695d0cef73dSGregory Neil Shapiro v = htonl(ctx->ctx_prot_vers2mta);
696d0cef73dSGregory Neil Shapiro (void) memcpy(&(buf[0]), (void *) &v,
697d0cef73dSGregory Neil Shapiro MILTER_LEN_BYTES);
698d0cef73dSGregory Neil Shapiro v = htonl(ctx->ctx_aflags);
69906f25ae9SGregory Neil Shapiro (void) memcpy(&(buf[MILTER_LEN_BYTES]), (void *) &v,
70006f25ae9SGregory Neil Shapiro MILTER_LEN_BYTES);
701d0cef73dSGregory Neil Shapiro v = htonl(ctx->ctx_pflags2mta);
702d0cef73dSGregory Neil Shapiro (void) memcpy(&(buf[MILTER_LEN_BYTES * 2]),
703d0cef73dSGregory Neil Shapiro (void *) &v, MILTER_LEN_BYTES);
704d0cef73dSGregory Neil Shapiro len = milter_addsymlist(ctx, buf, &buffer);
705d0cef73dSGregory Neil Shapiro if (buffer != NULL)
7065b0945b5SGregory Neil Shapiro {
707d0cef73dSGregory Neil Shapiro ret = mi_wr_cmd(sd, timeout_ptr, SMFIC_OPTNEG,
708d0cef73dSGregory Neil Shapiro buffer, len);
7095b0945b5SGregory Neil Shapiro if (buffer != buf)
7105b0945b5SGregory Neil Shapiro free(buffer);
7115b0945b5SGregory Neil Shapiro }
712d0cef73dSGregory Neil Shapiro else
713d0cef73dSGregory Neil Shapiro ret = MI_FAILURE;
714d0cef73dSGregory Neil Shapiro }
715d0cef73dSGregory Neil Shapiro break;
716d0cef73dSGregory Neil Shapiro case SMFIS_NOREPLY:
717d0cef73dSGregory Neil Shapiro if (bit != 0 &&
718d0cef73dSGregory Neil Shapiro (ctx->ctx_pflags & bit) != 0 &&
719d0cef73dSGregory Neil Shapiro (ctx->ctx_mta_pflags & bit) == 0)
720d0cef73dSGregory Neil Shapiro {
721d0cef73dSGregory Neil Shapiro /*
722d0cef73dSGregory Neil Shapiro ** milter doesn't want to send a reply,
723d0cef73dSGregory Neil Shapiro ** but the MTA doesn't have that feature: fake it.
724d0cef73dSGregory Neil Shapiro */
725d0cef73dSGregory Neil Shapiro
726d0cef73dSGregory Neil Shapiro ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_CONTINUE, NULL,
727d0cef73dSGregory Neil Shapiro 0);
72806f25ae9SGregory Neil Shapiro }
72906f25ae9SGregory Neil Shapiro break;
73006f25ae9SGregory Neil Shapiro default: /* don't send a reply */
73106f25ae9SGregory Neil Shapiro break;
73206f25ae9SGregory Neil Shapiro }
73306f25ae9SGregory Neil Shapiro return ret;
73406f25ae9SGregory Neil Shapiro }
73506f25ae9SGregory Neil Shapiro
73606f25ae9SGregory Neil Shapiro /*
737da7d7b9cSGregory Neil Shapiro ** MI_CLR_MACROS -- clear set of macros starting from a given index
73806f25ae9SGregory Neil Shapiro **
73906f25ae9SGregory Neil Shapiro ** Parameters:
74006f25ae9SGregory Neil Shapiro ** ctx -- context structure
74106f25ae9SGregory Neil Shapiro ** m -- index from which to clear all macros
74206f25ae9SGregory Neil Shapiro **
74306f25ae9SGregory Neil Shapiro ** Returns:
74406f25ae9SGregory Neil Shapiro ** None.
74506f25ae9SGregory Neil Shapiro */
746d0cef73dSGregory Neil Shapiro
74706f25ae9SGregory Neil Shapiro void
mi_clr_macros(ctx,m)74806f25ae9SGregory Neil Shapiro mi_clr_macros(ctx, m)
74906f25ae9SGregory Neil Shapiro SMFICTX_PTR ctx;
75006f25ae9SGregory Neil Shapiro int m;
75106f25ae9SGregory Neil Shapiro {
75206f25ae9SGregory Neil Shapiro int i;
75306f25ae9SGregory Neil Shapiro
75406f25ae9SGregory Neil Shapiro for (i = m; i < MAX_MACROS_ENTRIES; i++)
75506f25ae9SGregory Neil Shapiro {
75606f25ae9SGregory Neil Shapiro if (ctx->ctx_mac_ptr[i] != NULL)
75706f25ae9SGregory Neil Shapiro {
75806f25ae9SGregory Neil Shapiro free(ctx->ctx_mac_ptr[i]);
75906f25ae9SGregory Neil Shapiro ctx->ctx_mac_ptr[i] = NULL;
76006f25ae9SGregory Neil Shapiro }
76106f25ae9SGregory Neil Shapiro if (ctx->ctx_mac_buf[i] != NULL)
76206f25ae9SGregory Neil Shapiro {
76306f25ae9SGregory Neil Shapiro free(ctx->ctx_mac_buf[i]);
76406f25ae9SGregory Neil Shapiro ctx->ctx_mac_buf[i] = NULL;
76506f25ae9SGregory Neil Shapiro }
76606f25ae9SGregory Neil Shapiro }
76706f25ae9SGregory Neil Shapiro }
768d0cef73dSGregory Neil Shapiro
76940266059SGregory Neil Shapiro /*
7709bd497b8SGregory Neil Shapiro ** MI_CLR_SYMLIST -- clear list of macros
7719bd497b8SGregory Neil Shapiro **
7729bd497b8SGregory Neil Shapiro ** Parameters:
7739bd497b8SGregory Neil Shapiro ** ctx -- context structure
7749bd497b8SGregory Neil Shapiro **
7759bd497b8SGregory Neil Shapiro ** Returns:
7769bd497b8SGregory Neil Shapiro ** None.
7779bd497b8SGregory Neil Shapiro */
7789bd497b8SGregory Neil Shapiro
7799bd497b8SGregory Neil Shapiro static void
mi_clr_symlist(ctx)7809bd497b8SGregory Neil Shapiro mi_clr_symlist(ctx)
7819bd497b8SGregory Neil Shapiro SMFICTX *ctx;
7829bd497b8SGregory Neil Shapiro {
7839bd497b8SGregory Neil Shapiro int i;
7849bd497b8SGregory Neil Shapiro
7859bd497b8SGregory Neil Shapiro SM_ASSERT(ctx != NULL);
7869bd497b8SGregory Neil Shapiro for (i = SMFIM_FIRST; i <= SMFIM_LAST; i++)
7879bd497b8SGregory Neil Shapiro {
7889bd497b8SGregory Neil Shapiro if (ctx->ctx_mac_list[i] != NULL)
7899bd497b8SGregory Neil Shapiro {
7909bd497b8SGregory Neil Shapiro free(ctx->ctx_mac_list[i]);
7919bd497b8SGregory Neil Shapiro ctx->ctx_mac_list[i] = NULL;
7929bd497b8SGregory Neil Shapiro }
7939bd497b8SGregory Neil Shapiro }
7949bd497b8SGregory Neil Shapiro }
7959bd497b8SGregory Neil Shapiro
7969bd497b8SGregory Neil Shapiro /*
7979bd497b8SGregory Neil Shapiro ** MI_CLR_CTX -- clear context
7989bd497b8SGregory Neil Shapiro **
7999bd497b8SGregory Neil Shapiro ** Parameters:
8009bd497b8SGregory Neil Shapiro ** ctx -- context structure
8019bd497b8SGregory Neil Shapiro **
8029bd497b8SGregory Neil Shapiro ** Returns:
8039bd497b8SGregory Neil Shapiro ** None.
8049bd497b8SGregory Neil Shapiro */
8059bd497b8SGregory Neil Shapiro
8069bd497b8SGregory Neil Shapiro void
mi_clr_ctx(ctx)8079bd497b8SGregory Neil Shapiro mi_clr_ctx(ctx)
8089bd497b8SGregory Neil Shapiro SMFICTX *ctx;
8099bd497b8SGregory Neil Shapiro {
8109bd497b8SGregory Neil Shapiro SM_ASSERT(ctx != NULL);
8119bd497b8SGregory Neil Shapiro if (ValidSocket(ctx->ctx_sd))
8129bd497b8SGregory Neil Shapiro {
8139bd497b8SGregory Neil Shapiro (void) closesocket(ctx->ctx_sd);
8149bd497b8SGregory Neil Shapiro ctx->ctx_sd = INVALID_SOCKET;
8159bd497b8SGregory Neil Shapiro }
8169bd497b8SGregory Neil Shapiro if (ctx->ctx_reply != NULL)
8179bd497b8SGregory Neil Shapiro {
8189bd497b8SGregory Neil Shapiro free(ctx->ctx_reply);
8199bd497b8SGregory Neil Shapiro ctx->ctx_reply = NULL;
8209bd497b8SGregory Neil Shapiro }
8219bd497b8SGregory Neil Shapiro if (ctx->ctx_privdata != NULL)
8229bd497b8SGregory Neil Shapiro {
8239bd497b8SGregory Neil Shapiro smi_log(SMI_LOG_WARN,
8249bd497b8SGregory Neil Shapiro "%s: private data not NULL",
8259bd497b8SGregory Neil Shapiro ctx->ctx_smfi->xxfi_name);
8269bd497b8SGregory Neil Shapiro }
8279bd497b8SGregory Neil Shapiro mi_clr_macros(ctx, 0);
8289bd497b8SGregory Neil Shapiro mi_clr_symlist(ctx);
8299bd497b8SGregory Neil Shapiro free(ctx);
8309bd497b8SGregory Neil Shapiro }
8319bd497b8SGregory Neil Shapiro
8329bd497b8SGregory Neil Shapiro /*
83306f25ae9SGregory Neil Shapiro ** ST_OPTIONNEG -- negotiate options
83406f25ae9SGregory Neil Shapiro **
83506f25ae9SGregory Neil Shapiro ** Parameters:
83606f25ae9SGregory Neil Shapiro ** g -- generic argument structure
83706f25ae9SGregory Neil Shapiro **
83806f25ae9SGregory Neil Shapiro ** Returns:
83906f25ae9SGregory Neil Shapiro ** abort/send options/continue
84006f25ae9SGregory Neil Shapiro */
84106f25ae9SGregory Neil Shapiro
84206f25ae9SGregory Neil Shapiro static int
st_optionneg(g)84306f25ae9SGregory Neil Shapiro st_optionneg(g)
84406f25ae9SGregory Neil Shapiro genarg *g;
84506f25ae9SGregory Neil Shapiro {
8469bd497b8SGregory Neil Shapiro mi_int32 i, v, fake_pflags, internal_pflags;
847d0cef73dSGregory Neil Shapiro SMFICTX_PTR ctx;
8489bd497b8SGregory Neil Shapiro #if _FFR_MILTER_CHECK
8499bd497b8SGregory Neil Shapiro bool testmode = false;
8505b0945b5SGregory Neil Shapiro #endif
851d0cef73dSGregory Neil Shapiro int (*fi_negotiate) __P((SMFICTX *,
852d0cef73dSGregory Neil Shapiro unsigned long, unsigned long,
853d0cef73dSGregory Neil Shapiro unsigned long, unsigned long,
854d0cef73dSGregory Neil Shapiro unsigned long *, unsigned long *,
855d0cef73dSGregory Neil Shapiro unsigned long *, unsigned long *));
85606f25ae9SGregory Neil Shapiro
85706f25ae9SGregory Neil Shapiro if (g == NULL || g->a_ctx->ctx_smfi == NULL)
85806f25ae9SGregory Neil Shapiro return SMFIS_CONTINUE;
859d0cef73dSGregory Neil Shapiro ctx = g->a_ctx;
860d0cef73dSGregory Neil Shapiro mi_clr_macros(ctx, g->a_idx + 1);
861d0cef73dSGregory Neil Shapiro ctx->ctx_prot_vers = SMFI_PROT_VERSION;
86206f25ae9SGregory Neil Shapiro
86306f25ae9SGregory Neil Shapiro /* check for minimum length */
86406f25ae9SGregory Neil Shapiro if (g->a_len < MILTER_OPTLEN)
86506f25ae9SGregory Neil Shapiro {
86606f25ae9SGregory Neil Shapiro smi_log(SMI_LOG_ERR,
867d0cef73dSGregory Neil Shapiro "%s: st_optionneg[%ld]: len too short %d < %d",
868d0cef73dSGregory Neil Shapiro ctx->ctx_smfi->xxfi_name,
869d0cef73dSGregory Neil Shapiro (long) ctx->ctx_id, (int) g->a_len,
87006f25ae9SGregory Neil Shapiro MILTER_OPTLEN);
87106f25ae9SGregory Neil Shapiro return _SMFIS_ABORT;
87206f25ae9SGregory Neil Shapiro }
87306f25ae9SGregory Neil Shapiro
874d0cef73dSGregory Neil Shapiro /* protocol version */
875d0cef73dSGregory Neil Shapiro (void) memcpy((void *) &i, (void *) &(g->a_buf[0]), MILTER_LEN_BYTES);
87606f25ae9SGregory Neil Shapiro v = ntohl(i);
877d0cef73dSGregory Neil Shapiro
878d0cef73dSGregory Neil Shapiro #define SMFI_PROT_VERSION_MIN 2
879d0cef73dSGregory Neil Shapiro
880d0cef73dSGregory Neil Shapiro /* check for minimum version */
881d0cef73dSGregory Neil Shapiro if (v < SMFI_PROT_VERSION_MIN)
88206f25ae9SGregory Neil Shapiro {
88306f25ae9SGregory Neil Shapiro smi_log(SMI_LOG_ERR,
884d0cef73dSGregory Neil Shapiro "%s: st_optionneg[%ld]: protocol version too old %d < %d",
885d0cef73dSGregory Neil Shapiro ctx->ctx_smfi->xxfi_name,
886d0cef73dSGregory Neil Shapiro (long) ctx->ctx_id, v, SMFI_PROT_VERSION_MIN);
88706f25ae9SGregory Neil Shapiro return _SMFIS_ABORT;
88806f25ae9SGregory Neil Shapiro }
889d0cef73dSGregory Neil Shapiro ctx->ctx_mta_prot_vers = v;
890d0cef73dSGregory Neil Shapiro if (ctx->ctx_prot_vers < ctx->ctx_mta_prot_vers)
891d0cef73dSGregory Neil Shapiro ctx->ctx_prot_vers2mta = ctx->ctx_prot_vers;
892d0cef73dSGregory Neil Shapiro else
893d0cef73dSGregory Neil Shapiro ctx->ctx_prot_vers2mta = ctx->ctx_mta_prot_vers;
89406f25ae9SGregory Neil Shapiro
89506f25ae9SGregory Neil Shapiro (void) memcpy((void *) &i, (void *) &(g->a_buf[MILTER_LEN_BYTES]),
89606f25ae9SGregory Neil Shapiro MILTER_LEN_BYTES);
89706f25ae9SGregory Neil Shapiro v = ntohl(i);
89806f25ae9SGregory Neil Shapiro
89906f25ae9SGregory Neil Shapiro /* no flags? set to default value for V1 actions */
90006f25ae9SGregory Neil Shapiro if (v == 0)
90106f25ae9SGregory Neil Shapiro v = SMFI_V1_ACTS;
902d0cef73dSGregory Neil Shapiro ctx->ctx_mta_aflags = v; /* MTA action flags */
90306f25ae9SGregory Neil Shapiro
9049bd497b8SGregory Neil Shapiro internal_pflags = 0;
90506f25ae9SGregory Neil Shapiro (void) memcpy((void *) &i, (void *) &(g->a_buf[MILTER_LEN_BYTES * 2]),
90606f25ae9SGregory Neil Shapiro MILTER_LEN_BYTES);
90706f25ae9SGregory Neil Shapiro v = ntohl(i);
90806f25ae9SGregory Neil Shapiro
90906f25ae9SGregory Neil Shapiro /* no flags? set to default value for V1 protocol */
91006f25ae9SGregory Neil Shapiro if (v == 0)
91106f25ae9SGregory Neil Shapiro v = SMFI_V1_PROT;
9129bd497b8SGregory Neil Shapiro #if _FFR_MDS_NEGOTIATE
9139bd497b8SGregory Neil Shapiro else if (ctx->ctx_smfi->xxfi_version >= SMFI_VERSION_MDS)
9149bd497b8SGregory Neil Shapiro {
9159bd497b8SGregory Neil Shapiro /*
9169bd497b8SGregory Neil Shapiro ** Allow changing the size only if milter is compiled
9179bd497b8SGregory Neil Shapiro ** against a version that supports this.
9189bd497b8SGregory Neil Shapiro ** If a milter is dynamically linked against a newer
9199bd497b8SGregory Neil Shapiro ** libmilter version, we don't want to "surprise"
9209bd497b8SGregory Neil Shapiro ** it with a larger buffer as it may rely on it
9219bd497b8SGregory Neil Shapiro ** even though it is not documented as a limit.
9229bd497b8SGregory Neil Shapiro */
9239bd497b8SGregory Neil Shapiro
9249bd497b8SGregory Neil Shapiro if (bitset(SMFIP_MDS_1M, v))
9259bd497b8SGregory Neil Shapiro {
9269bd497b8SGregory Neil Shapiro internal_pflags |= SMFIP_MDS_1M;
9279bd497b8SGregory Neil Shapiro (void) smfi_setmaxdatasize(MILTER_MDS_1M);
9289bd497b8SGregory Neil Shapiro }
9299bd497b8SGregory Neil Shapiro else if (bitset(SMFIP_MDS_256K, v))
9309bd497b8SGregory Neil Shapiro {
9319bd497b8SGregory Neil Shapiro internal_pflags |= SMFIP_MDS_256K;
9329bd497b8SGregory Neil Shapiro (void) smfi_setmaxdatasize(MILTER_MDS_256K);
9339bd497b8SGregory Neil Shapiro }
9349bd497b8SGregory Neil Shapiro }
9359bd497b8SGregory Neil Shapiro # if 0
9369bd497b8SGregory Neil Shapiro /* don't log this for now... */
9379bd497b8SGregory Neil Shapiro else if (ctx->ctx_smfi->xxfi_version < SMFI_VERSION_MDS &&
9389bd497b8SGregory Neil Shapiro bitset(SMFIP_MDS_1M|SMFIP_MDS_256K, v))
9399bd497b8SGregory Neil Shapiro {
9409bd497b8SGregory Neil Shapiro smi_log(SMI_LOG_WARN,
9419bd497b8SGregory Neil Shapiro "%s: st_optionneg[%ld]: milter version=%X, trying flags=%X",
9429bd497b8SGregory Neil Shapiro ctx->ctx_smfi->xxfi_name,
9439bd497b8SGregory Neil Shapiro (long) ctx->ctx_id, ctx->ctx_smfi->xxfi_version, v);
9449bd497b8SGregory Neil Shapiro }
9459bd497b8SGregory Neil Shapiro # endif /* 0 */
9469bd497b8SGregory Neil Shapiro #endif /* _FFR_MDS_NEGOTIATE */
9479bd497b8SGregory Neil Shapiro
9489bd497b8SGregory Neil Shapiro /*
9499bd497b8SGregory Neil Shapiro ** MTA protocol flags.
9509bd497b8SGregory Neil Shapiro ** We pass the internal flags to the milter as "read only",
9519bd497b8SGregory Neil Shapiro ** i.e., a milter can read them so it knows which size
9529bd497b8SGregory Neil Shapiro ** will be used, but any changes by a milter will be ignored
9539bd497b8SGregory Neil Shapiro ** (see below, search for SMFI_INTERNAL).
9549bd497b8SGregory Neil Shapiro */
9559bd497b8SGregory Neil Shapiro
9569bd497b8SGregory Neil Shapiro ctx->ctx_mta_pflags = (v & ~SMFI_INTERNAL) | internal_pflags;
957d0cef73dSGregory Neil Shapiro
958d0cef73dSGregory Neil Shapiro /*
959d0cef73dSGregory Neil Shapiro ** Copy flags from milter struct into libmilter context;
960d0cef73dSGregory Neil Shapiro ** this variable will be used later on to check whether
961d0cef73dSGregory Neil Shapiro ** the MTA "actions" can fulfill the milter requirements,
962d0cef73dSGregory Neil Shapiro ** but it may be overwritten by the negotiate callback.
963d0cef73dSGregory Neil Shapiro */
964d0cef73dSGregory Neil Shapiro
965d0cef73dSGregory Neil Shapiro ctx->ctx_aflags = ctx->ctx_smfi->xxfi_flags;
966d0cef73dSGregory Neil Shapiro fake_pflags = SMFIP_NR_CONN
967d0cef73dSGregory Neil Shapiro |SMFIP_NR_HELO
968d0cef73dSGregory Neil Shapiro |SMFIP_NR_MAIL
969d0cef73dSGregory Neil Shapiro |SMFIP_NR_RCPT
970d0cef73dSGregory Neil Shapiro |SMFIP_NR_DATA
971d0cef73dSGregory Neil Shapiro |SMFIP_NR_UNKN
972d0cef73dSGregory Neil Shapiro |SMFIP_NR_HDR
973d0cef73dSGregory Neil Shapiro |SMFIP_NR_EOH
974d0cef73dSGregory Neil Shapiro |SMFIP_NR_BODY
975d0cef73dSGregory Neil Shapiro ;
976d0cef73dSGregory Neil Shapiro
977d0cef73dSGregory Neil Shapiro if (g->a_ctx->ctx_smfi != NULL &&
9786d13d6cbSGregory Neil Shapiro g->a_ctx->ctx_smfi->xxfi_version > 4 &&
979d0cef73dSGregory Neil Shapiro (fi_negotiate = g->a_ctx->ctx_smfi->xxfi_negotiate) != NULL)
980d0cef73dSGregory Neil Shapiro {
981d0cef73dSGregory Neil Shapiro int r;
982d0cef73dSGregory Neil Shapiro unsigned long m_aflags, m_pflags, m_f2, m_f3;
983d0cef73dSGregory Neil Shapiro
984d0cef73dSGregory Neil Shapiro /*
985d0cef73dSGregory Neil Shapiro ** let milter decide whether the features offered by the
986d0cef73dSGregory Neil Shapiro ** MTA are "good enough".
987d0cef73dSGregory Neil Shapiro ** Notes:
988d0cef73dSGregory Neil Shapiro ** - libmilter can "fake" some features (e.g., SMFIP_NR_HDR)
989d0cef73dSGregory Neil Shapiro ** - m_f2, m_f3 are for future extensions
990d0cef73dSGregory Neil Shapiro */
991d0cef73dSGregory Neil Shapiro
992d0cef73dSGregory Neil Shapiro m_f2 = m_f3 = 0;
993d0cef73dSGregory Neil Shapiro m_aflags = ctx->ctx_mta_aflags;
994d0cef73dSGregory Neil Shapiro m_pflags = ctx->ctx_pflags;
995d0cef73dSGregory Neil Shapiro if ((SMFIP_SKIP & ctx->ctx_mta_pflags) != 0)
996d0cef73dSGregory Neil Shapiro m_pflags |= SMFIP_SKIP;
997d0cef73dSGregory Neil Shapiro r = fi_negotiate(g->a_ctx,
998d0cef73dSGregory Neil Shapiro ctx->ctx_mta_aflags,
999d0cef73dSGregory Neil Shapiro ctx->ctx_mta_pflags|fake_pflags,
1000d0cef73dSGregory Neil Shapiro 0, 0,
1001d0cef73dSGregory Neil Shapiro &m_aflags, &m_pflags, &m_f2, &m_f3);
1002d0cef73dSGregory Neil Shapiro
10039bd497b8SGregory Neil Shapiro #if _FFR_MILTER_CHECK
10049bd497b8SGregory Neil Shapiro testmode = bitset(SMFIP_TEST, m_pflags);
10059bd497b8SGregory Neil Shapiro if (testmode)
10069bd497b8SGregory Neil Shapiro m_pflags &= ~SMFIP_TEST;
10075b0945b5SGregory Neil Shapiro #endif
10089bd497b8SGregory Neil Shapiro
1009d0cef73dSGregory Neil Shapiro /*
1010d0cef73dSGregory Neil Shapiro ** Types of protocol flags (pflags):
1011d0cef73dSGregory Neil Shapiro ** 1. do NOT send protocol step X
1012d0cef73dSGregory Neil Shapiro ** 2. MTA can do/understand something extra (SKIP,
1013d0cef73dSGregory Neil Shapiro ** send unknown RCPTs)
1014d0cef73dSGregory Neil Shapiro ** 3. MTA can deal with "no reply" for various protocol steps
1015d0cef73dSGregory Neil Shapiro ** Note: this mean that it isn't possible to simply set all
1016d0cef73dSGregory Neil Shapiro ** flags to get "everything":
1017d0cef73dSGregory Neil Shapiro ** setting a flag of type 1 turns off a step
1018d0cef73dSGregory Neil Shapiro ** (it should be the other way around:
1019d0cef73dSGregory Neil Shapiro ** a flag means a protocol step can be sent)
1020d0cef73dSGregory Neil Shapiro ** setting a flag of type 3 requires that milter
1021d0cef73dSGregory Neil Shapiro ** never sends a reply for the corresponding step.
1022d0cef73dSGregory Neil Shapiro ** Summary: the "negation" of protocol flags is causing
1023d0cef73dSGregory Neil Shapiro ** problems, but at least for type 3 there is no simple
1024d0cef73dSGregory Neil Shapiro ** solution.
1025d0cef73dSGregory Neil Shapiro **
1026d0cef73dSGregory Neil Shapiro ** What should "all options" mean?
1027d0cef73dSGregory Neil Shapiro ** send all protocol steps _except_ those for which there is
1028d0cef73dSGregory Neil Shapiro ** no callback (currently registered in ctx_pflags)
1029d0cef73dSGregory Neil Shapiro ** expect SKIP as return code? Yes
1030d0cef73dSGregory Neil Shapiro ** send unknown RCPTs? No,
1031d0cef73dSGregory Neil Shapiro ** must be explicitly requested?
1032d0cef73dSGregory Neil Shapiro ** "no reply" for some protocol steps? No,
1033d0cef73dSGregory Neil Shapiro ** must be explicitly requested.
1034d0cef73dSGregory Neil Shapiro */
1035d0cef73dSGregory Neil Shapiro
1036d0cef73dSGregory Neil Shapiro if (SMFIS_ALL_OPTS == r)
1037d0cef73dSGregory Neil Shapiro {
1038d0cef73dSGregory Neil Shapiro ctx->ctx_aflags = ctx->ctx_mta_aflags;
1039d0cef73dSGregory Neil Shapiro ctx->ctx_pflags2mta = ctx->ctx_pflags;
1040d0cef73dSGregory Neil Shapiro if ((SMFIP_SKIP & ctx->ctx_mta_pflags) != 0)
1041d0cef73dSGregory Neil Shapiro ctx->ctx_pflags2mta |= SMFIP_SKIP;
1042d0cef73dSGregory Neil Shapiro }
1043d0cef73dSGregory Neil Shapiro else if (r != SMFIS_CONTINUE)
104406f25ae9SGregory Neil Shapiro {
104506f25ae9SGregory Neil Shapiro smi_log(SMI_LOG_ERR,
1046d0cef73dSGregory Neil Shapiro "%s: st_optionneg[%ld]: xxfi_negotiate returned %d (protocol options=0x%lx, actions=0x%lx)",
1047d0cef73dSGregory Neil Shapiro ctx->ctx_smfi->xxfi_name,
1048d0cef73dSGregory Neil Shapiro (long) ctx->ctx_id, r, ctx->ctx_mta_pflags,
1049d0cef73dSGregory Neil Shapiro ctx->ctx_mta_aflags);
1050d0cef73dSGregory Neil Shapiro return _SMFIS_ABORT;
1051d0cef73dSGregory Neil Shapiro }
1052d0cef73dSGregory Neil Shapiro else
1053d0cef73dSGregory Neil Shapiro {
1054d0cef73dSGregory Neil Shapiro ctx->ctx_aflags = m_aflags;
1055d0cef73dSGregory Neil Shapiro ctx->ctx_pflags = m_pflags;
1056d0cef73dSGregory Neil Shapiro ctx->ctx_pflags2mta = m_pflags;
1057d0cef73dSGregory Neil Shapiro }
1058d0cef73dSGregory Neil Shapiro
1059d0cef73dSGregory Neil Shapiro /* check whether some flags need to be "faked" */
1060d0cef73dSGregory Neil Shapiro i = ctx->ctx_pflags2mta;
1061d0cef73dSGregory Neil Shapiro if ((ctx->ctx_mta_pflags & i) != i)
1062d0cef73dSGregory Neil Shapiro {
1063d0cef73dSGregory Neil Shapiro unsigned int idx;
1064d0cef73dSGregory Neil Shapiro unsigned long b;
1065d0cef73dSGregory Neil Shapiro
1066d0cef73dSGregory Neil Shapiro /*
1067d0cef73dSGregory Neil Shapiro ** If some behavior can be faked (set in fake_pflags),
1068d0cef73dSGregory Neil Shapiro ** but the MTA doesn't support it, then unset
1069d0cef73dSGregory Neil Shapiro ** that flag in the value that is sent to the MTA.
1070d0cef73dSGregory Neil Shapiro */
1071d0cef73dSGregory Neil Shapiro
1072d0cef73dSGregory Neil Shapiro for (idx = 0; idx < 32; idx++)
1073d0cef73dSGregory Neil Shapiro {
1074d0cef73dSGregory Neil Shapiro b = 1 << idx;
1075d0cef73dSGregory Neil Shapiro if ((ctx->ctx_mta_pflags & b) != b &&
1076d0cef73dSGregory Neil Shapiro (fake_pflags & b) == b)
1077d0cef73dSGregory Neil Shapiro ctx->ctx_pflags2mta &= ~b;
1078d0cef73dSGregory Neil Shapiro }
1079d0cef73dSGregory Neil Shapiro }
1080d0cef73dSGregory Neil Shapiro }
1081d0cef73dSGregory Neil Shapiro else
1082d0cef73dSGregory Neil Shapiro {
1083d0cef73dSGregory Neil Shapiro /*
1084d0cef73dSGregory Neil Shapiro ** Set the protocol flags based on the values determined
1085d0cef73dSGregory Neil Shapiro ** in mi_listener() which checked the defined callbacks.
1086d0cef73dSGregory Neil Shapiro */
1087d0cef73dSGregory Neil Shapiro
1088d0cef73dSGregory Neil Shapiro ctx->ctx_pflags2mta = ctx->ctx_pflags;
1089d0cef73dSGregory Neil Shapiro }
1090d0cef73dSGregory Neil Shapiro
1091d0cef73dSGregory Neil Shapiro /* check whether actions and protocol requirements can be satisfied */
1092d0cef73dSGregory Neil Shapiro i = ctx->ctx_aflags;
1093d0cef73dSGregory Neil Shapiro if ((i & ctx->ctx_mta_aflags) != i)
1094d0cef73dSGregory Neil Shapiro {
1095d0cef73dSGregory Neil Shapiro smi_log(SMI_LOG_ERR,
1096d0cef73dSGregory Neil Shapiro "%s: st_optionneg[%ld]: 0x%lx does not fulfill action requirements 0x%x",
1097d0cef73dSGregory Neil Shapiro ctx->ctx_smfi->xxfi_name,
1098d0cef73dSGregory Neil Shapiro (long) ctx->ctx_id, ctx->ctx_mta_aflags, i);
109906f25ae9SGregory Neil Shapiro return _SMFIS_ABORT;
110006f25ae9SGregory Neil Shapiro }
110106f25ae9SGregory Neil Shapiro
1102d0cef73dSGregory Neil Shapiro i = ctx->ctx_pflags2mta;
1103d0cef73dSGregory Neil Shapiro if ((ctx->ctx_mta_pflags & i) != i)
1104d0cef73dSGregory Neil Shapiro {
1105d0cef73dSGregory Neil Shapiro /*
1106d0cef73dSGregory Neil Shapiro ** Older MTAs do not support some protocol steps.
11075b0945b5SGregory Neil Shapiro ** As this protocol is a bit "weird" (it asks for steps
1108d0cef73dSGregory Neil Shapiro ** NOT to be taken/sent) we have to check whether we
1109d0cef73dSGregory Neil Shapiro ** should turn off those "negative" requests.
1110d0cef73dSGregory Neil Shapiro ** Currently these are only SMFIP_NODATA and SMFIP_NOUNKNOWN.
1111d0cef73dSGregory Neil Shapiro */
1112d0cef73dSGregory Neil Shapiro
1113d0cef73dSGregory Neil Shapiro if (bitset(SMFIP_NODATA, ctx->ctx_pflags2mta) &&
1114d0cef73dSGregory Neil Shapiro !bitset(SMFIP_NODATA, ctx->ctx_mta_pflags))
1115d0cef73dSGregory Neil Shapiro ctx->ctx_pflags2mta &= ~SMFIP_NODATA;
1116d0cef73dSGregory Neil Shapiro if (bitset(SMFIP_NOUNKNOWN, ctx->ctx_pflags2mta) &&
1117d0cef73dSGregory Neil Shapiro !bitset(SMFIP_NOUNKNOWN, ctx->ctx_mta_pflags))
1118d0cef73dSGregory Neil Shapiro ctx->ctx_pflags2mta &= ~SMFIP_NOUNKNOWN;
1119d0cef73dSGregory Neil Shapiro i = ctx->ctx_pflags2mta;
1120d0cef73dSGregory Neil Shapiro }
1121d0cef73dSGregory Neil Shapiro
1122d0cef73dSGregory Neil Shapiro if ((ctx->ctx_mta_pflags & i) != i)
1123d0cef73dSGregory Neil Shapiro {
1124d0cef73dSGregory Neil Shapiro smi_log(SMI_LOG_ERR,
1125d0cef73dSGregory Neil Shapiro "%s: st_optionneg[%ld]: 0x%lx does not fulfill protocol requirements 0x%x",
1126d0cef73dSGregory Neil Shapiro ctx->ctx_smfi->xxfi_name,
1127d0cef73dSGregory Neil Shapiro (long) ctx->ctx_id, ctx->ctx_mta_pflags, i);
1128d0cef73dSGregory Neil Shapiro return _SMFIS_ABORT;
1129d0cef73dSGregory Neil Shapiro }
1130e3793f76SGregory Neil Shapiro fix_stm(ctx);
1131d0cef73dSGregory Neil Shapiro
1132d0cef73dSGregory Neil Shapiro if (ctx->ctx_dbg > 3)
11336f9c8e5bSGregory Neil Shapiro sm_dprintf("[%lu] milter_negotiate:"
1134d0cef73dSGregory Neil Shapiro " mta_actions=0x%lx, mta_flags=0x%lx"
1135d0cef73dSGregory Neil Shapiro " actions=0x%lx, flags=0x%lx\n"
1136d0cef73dSGregory Neil Shapiro , (long) ctx->ctx_id
1137d0cef73dSGregory Neil Shapiro , ctx->ctx_mta_aflags, ctx->ctx_mta_pflags
1138d0cef73dSGregory Neil Shapiro , ctx->ctx_aflags, ctx->ctx_pflags);
1139d0cef73dSGregory Neil Shapiro
11409bd497b8SGregory Neil Shapiro #if _FFR_MILTER_CHECK
11419bd497b8SGregory Neil Shapiro if (ctx->ctx_dbg > 3)
11426f9c8e5bSGregory Neil Shapiro sm_dprintf("[%lu] milter_negotiate:"
11439bd497b8SGregory Neil Shapiro " testmode=%d, pflags2mta=%X, internal_pflags=%X\n"
11449bd497b8SGregory Neil Shapiro , (long) ctx->ctx_id, testmode
11459bd497b8SGregory Neil Shapiro , ctx->ctx_pflags2mta, internal_pflags);
11469bd497b8SGregory Neil Shapiro
11479bd497b8SGregory Neil Shapiro /* in test mode: take flags without further modifications */
11489bd497b8SGregory Neil Shapiro if (!testmode)
11499bd497b8SGregory Neil Shapiro /* Warning: check statement below! */
11509bd497b8SGregory Neil Shapiro #endif /* _FFR_MILTER_CHECK */
11519bd497b8SGregory Neil Shapiro
11529bd497b8SGregory Neil Shapiro /*
11539bd497b8SGregory Neil Shapiro ** Remove the internal flags that might have been set by a milter
11549bd497b8SGregory Neil Shapiro ** and set only those determined above.
11559bd497b8SGregory Neil Shapiro */
11569bd497b8SGregory Neil Shapiro
11579bd497b8SGregory Neil Shapiro ctx->ctx_pflags2mta = (ctx->ctx_pflags2mta & ~SMFI_INTERNAL)
11589bd497b8SGregory Neil Shapiro | internal_pflags;
115906f25ae9SGregory Neil Shapiro return _SMFIS_OPTIONS;
116006f25ae9SGregory Neil Shapiro }
1161d0cef73dSGregory Neil Shapiro
116240266059SGregory Neil Shapiro /*
116306f25ae9SGregory Neil Shapiro ** ST_CONNECTINFO -- receive connection information
116406f25ae9SGregory Neil Shapiro **
116506f25ae9SGregory Neil Shapiro ** Parameters:
116606f25ae9SGregory Neil Shapiro ** g -- generic argument structure
116706f25ae9SGregory Neil Shapiro **
116806f25ae9SGregory Neil Shapiro ** Returns:
116906f25ae9SGregory Neil Shapiro ** continue or filter-specified value
117006f25ae9SGregory Neil Shapiro */
117106f25ae9SGregory Neil Shapiro
117206f25ae9SGregory Neil Shapiro static int
st_connectinfo(g)117306f25ae9SGregory Neil Shapiro st_connectinfo(g)
117406f25ae9SGregory Neil Shapiro genarg *g;
117506f25ae9SGregory Neil Shapiro {
117606f25ae9SGregory Neil Shapiro size_t l;
117706f25ae9SGregory Neil Shapiro size_t i;
117806f25ae9SGregory Neil Shapiro char *s, family;
117940266059SGregory Neil Shapiro unsigned short port = 0;
118006f25ae9SGregory Neil Shapiro _SOCK_ADDR sockaddr;
118106f25ae9SGregory Neil Shapiro sfsistat (*fi_connect) __P((SMFICTX *, char *, _SOCK_ADDR *));
118206f25ae9SGregory Neil Shapiro
118306f25ae9SGregory Neil Shapiro if (g == NULL)
118406f25ae9SGregory Neil Shapiro return _SMFIS_ABORT;
118506f25ae9SGregory Neil Shapiro mi_clr_macros(g->a_ctx, g->a_idx + 1);
118606f25ae9SGregory Neil Shapiro if (g->a_ctx->ctx_smfi == NULL ||
118706f25ae9SGregory Neil Shapiro (fi_connect = g->a_ctx->ctx_smfi->xxfi_connect) == NULL)
118806f25ae9SGregory Neil Shapiro return SMFIS_CONTINUE;
118906f25ae9SGregory Neil Shapiro
119006f25ae9SGregory Neil Shapiro s = g->a_buf;
119106f25ae9SGregory Neil Shapiro i = 0;
119206f25ae9SGregory Neil Shapiro l = g->a_len;
11935b0945b5SGregory Neil Shapiro while (i <= l && s[i] != '\0')
119406f25ae9SGregory Neil Shapiro ++i;
1195323f6dcbSGregory Neil Shapiro if (i + 1 >= l)
119606f25ae9SGregory Neil Shapiro return _SMFIS_ABORT;
119706f25ae9SGregory Neil Shapiro
119806f25ae9SGregory Neil Shapiro /* Move past trailing \0 in host string */
119906f25ae9SGregory Neil Shapiro i++;
120006f25ae9SGregory Neil Shapiro family = s[i++];
1201a7ec597cSGregory Neil Shapiro (void) memset(&sockaddr, '\0', sizeof sockaddr);
120206f25ae9SGregory Neil Shapiro if (family != SMFIA_UNKNOWN)
120306f25ae9SGregory Neil Shapiro {
1204323f6dcbSGregory Neil Shapiro if (i + sizeof port >= l)
120506f25ae9SGregory Neil Shapiro {
120606f25ae9SGregory Neil Shapiro smi_log(SMI_LOG_ERR,
1207d0cef73dSGregory Neil Shapiro "%s: connect[%ld]: wrong len %d >= %d",
120806f25ae9SGregory Neil Shapiro g->a_ctx->ctx_smfi->xxfi_name,
1209d0cef73dSGregory Neil Shapiro (long) g->a_ctx->ctx_id, (int) i, (int) l);
121006f25ae9SGregory Neil Shapiro return _SMFIS_ABORT;
121106f25ae9SGregory Neil Shapiro }
1212323f6dcbSGregory Neil Shapiro (void) memcpy((void *) &port, (void *) (s + i),
1213323f6dcbSGregory Neil Shapiro sizeof port);
1214323f6dcbSGregory Neil Shapiro i += sizeof port;
1215605302a5SGregory Neil Shapiro
1216605302a5SGregory Neil Shapiro /* make sure string is terminated */
1217605302a5SGregory Neil Shapiro if (s[l - 1] != '\0')
1218605302a5SGregory Neil Shapiro return _SMFIS_ABORT;
121906f25ae9SGregory Neil Shapiro #if NETINET
122006f25ae9SGregory Neil Shapiro if (family == SMFIA_INET)
122106f25ae9SGregory Neil Shapiro {
122206f25ae9SGregory Neil Shapiro if (inet_aton(s + i, (struct in_addr *) &sockaddr.sin.sin_addr)
1223605302a5SGregory Neil Shapiro != 1)
122406f25ae9SGregory Neil Shapiro {
122506f25ae9SGregory Neil Shapiro smi_log(SMI_LOG_ERR,
1226d0cef73dSGregory Neil Shapiro "%s: connect[%ld]: inet_aton failed",
122706f25ae9SGregory Neil Shapiro g->a_ctx->ctx_smfi->xxfi_name,
1228d0cef73dSGregory Neil Shapiro (long) g->a_ctx->ctx_id);
122906f25ae9SGregory Neil Shapiro return _SMFIS_ABORT;
123006f25ae9SGregory Neil Shapiro }
123106f25ae9SGregory Neil Shapiro sockaddr.sa.sa_family = AF_INET;
123206f25ae9SGregory Neil Shapiro if (port > 0)
123306f25ae9SGregory Neil Shapiro sockaddr.sin.sin_port = port;
123406f25ae9SGregory Neil Shapiro }
123506f25ae9SGregory Neil Shapiro else
123606f25ae9SGregory Neil Shapiro #endif /* NETINET */
123706f25ae9SGregory Neil Shapiro #if NETINET6
123806f25ae9SGregory Neil Shapiro if (family == SMFIA_INET6)
123906f25ae9SGregory Neil Shapiro {
124040266059SGregory Neil Shapiro if (mi_inet_pton(AF_INET6, s + i,
124106f25ae9SGregory Neil Shapiro &sockaddr.sin6.sin6_addr) != 1)
124206f25ae9SGregory Neil Shapiro {
124306f25ae9SGregory Neil Shapiro smi_log(SMI_LOG_ERR,
1244d0cef73dSGregory Neil Shapiro "%s: connect[%ld]: mi_inet_pton failed",
124506f25ae9SGregory Neil Shapiro g->a_ctx->ctx_smfi->xxfi_name,
1246d0cef73dSGregory Neil Shapiro (long) g->a_ctx->ctx_id);
124706f25ae9SGregory Neil Shapiro return _SMFIS_ABORT;
124806f25ae9SGregory Neil Shapiro }
124906f25ae9SGregory Neil Shapiro sockaddr.sa.sa_family = AF_INET6;
125006f25ae9SGregory Neil Shapiro if (port > 0)
125106f25ae9SGregory Neil Shapiro sockaddr.sin6.sin6_port = port;
125206f25ae9SGregory Neil Shapiro }
125306f25ae9SGregory Neil Shapiro else
125406f25ae9SGregory Neil Shapiro #endif /* NETINET6 */
125506f25ae9SGregory Neil Shapiro #if NETUNIX
125606f25ae9SGregory Neil Shapiro if (family == SMFIA_UNIX)
125706f25ae9SGregory Neil Shapiro {
125840266059SGregory Neil Shapiro if (sm_strlcpy(sockaddr.sunix.sun_path, s + i,
125906f25ae9SGregory Neil Shapiro sizeof sockaddr.sunix.sun_path) >=
126006f25ae9SGregory Neil Shapiro sizeof sockaddr.sunix.sun_path)
126106f25ae9SGregory Neil Shapiro {
126206f25ae9SGregory Neil Shapiro smi_log(SMI_LOG_ERR,
1263d0cef73dSGregory Neil Shapiro "%s: connect[%ld]: path too long",
126406f25ae9SGregory Neil Shapiro g->a_ctx->ctx_smfi->xxfi_name,
1265d0cef73dSGregory Neil Shapiro (long) g->a_ctx->ctx_id);
126606f25ae9SGregory Neil Shapiro return _SMFIS_ABORT;
126706f25ae9SGregory Neil Shapiro }
126806f25ae9SGregory Neil Shapiro sockaddr.sunix.sun_family = AF_UNIX;
126906f25ae9SGregory Neil Shapiro }
127006f25ae9SGregory Neil Shapiro else
127106f25ae9SGregory Neil Shapiro #endif /* NETUNIX */
127206f25ae9SGregory Neil Shapiro {
127306f25ae9SGregory Neil Shapiro smi_log(SMI_LOG_ERR,
1274d0cef73dSGregory Neil Shapiro "%s: connect[%ld]: unknown family %d",
127506f25ae9SGregory Neil Shapiro g->a_ctx->ctx_smfi->xxfi_name,
1276d0cef73dSGregory Neil Shapiro (long) g->a_ctx->ctx_id, family);
127706f25ae9SGregory Neil Shapiro return _SMFIS_ABORT;
127806f25ae9SGregory Neil Shapiro }
127906f25ae9SGregory Neil Shapiro }
128006f25ae9SGregory Neil Shapiro return (*fi_connect)(g->a_ctx, g->a_buf,
128106f25ae9SGregory Neil Shapiro family != SMFIA_UNKNOWN ? &sockaddr : NULL);
128206f25ae9SGregory Neil Shapiro }
1283e92d3f3fSGregory Neil Shapiro
128440266059SGregory Neil Shapiro /*
128506f25ae9SGregory Neil Shapiro ** ST_EOH -- end of headers
128606f25ae9SGregory Neil Shapiro **
128706f25ae9SGregory Neil Shapiro ** Parameters:
128806f25ae9SGregory Neil Shapiro ** g -- generic argument structure
128906f25ae9SGregory Neil Shapiro **
129006f25ae9SGregory Neil Shapiro ** Returns:
129106f25ae9SGregory Neil Shapiro ** continue or filter-specified value
129206f25ae9SGregory Neil Shapiro */
129306f25ae9SGregory Neil Shapiro
129406f25ae9SGregory Neil Shapiro static int
st_eoh(g)129506f25ae9SGregory Neil Shapiro st_eoh(g)
129606f25ae9SGregory Neil Shapiro genarg *g;
129706f25ae9SGregory Neil Shapiro {
129806f25ae9SGregory Neil Shapiro sfsistat (*fi_eoh) __P((SMFICTX *));
129906f25ae9SGregory Neil Shapiro
130006f25ae9SGregory Neil Shapiro if (g == NULL)
130106f25ae9SGregory Neil Shapiro return _SMFIS_ABORT;
130206f25ae9SGregory Neil Shapiro if (g->a_ctx->ctx_smfi != NULL &&
130306f25ae9SGregory Neil Shapiro (fi_eoh = g->a_ctx->ctx_smfi->xxfi_eoh) != NULL)
130406f25ae9SGregory Neil Shapiro return (*fi_eoh)(g->a_ctx);
130506f25ae9SGregory Neil Shapiro return SMFIS_CONTINUE;
130606f25ae9SGregory Neil Shapiro }
1307e92d3f3fSGregory Neil Shapiro
1308e92d3f3fSGregory Neil Shapiro /*
1309e92d3f3fSGregory Neil Shapiro ** ST_DATA -- DATA command
1310e92d3f3fSGregory Neil Shapiro **
1311e92d3f3fSGregory Neil Shapiro ** Parameters:
1312e92d3f3fSGregory Neil Shapiro ** g -- generic argument structure
1313e92d3f3fSGregory Neil Shapiro **
1314e92d3f3fSGregory Neil Shapiro ** Returns:
1315e92d3f3fSGregory Neil Shapiro ** continue or filter-specified value
1316e92d3f3fSGregory Neil Shapiro */
1317e92d3f3fSGregory Neil Shapiro
1318e92d3f3fSGregory Neil Shapiro static int
st_data(g)1319e92d3f3fSGregory Neil Shapiro st_data(g)
1320e92d3f3fSGregory Neil Shapiro genarg *g;
1321e92d3f3fSGregory Neil Shapiro {
1322e92d3f3fSGregory Neil Shapiro sfsistat (*fi_data) __P((SMFICTX *));
1323e92d3f3fSGregory Neil Shapiro
1324e92d3f3fSGregory Neil Shapiro if (g == NULL)
1325e92d3f3fSGregory Neil Shapiro return _SMFIS_ABORT;
1326e92d3f3fSGregory Neil Shapiro if (g->a_ctx->ctx_smfi != NULL &&
13276d13d6cbSGregory Neil Shapiro g->a_ctx->ctx_smfi->xxfi_version > 3 &&
1328e92d3f3fSGregory Neil Shapiro (fi_data = g->a_ctx->ctx_smfi->xxfi_data) != NULL)
1329e92d3f3fSGregory Neil Shapiro return (*fi_data)(g->a_ctx);
1330e92d3f3fSGregory Neil Shapiro return SMFIS_CONTINUE;
1331e92d3f3fSGregory Neil Shapiro }
1332e92d3f3fSGregory Neil Shapiro
133340266059SGregory Neil Shapiro /*
133406f25ae9SGregory Neil Shapiro ** ST_HELO -- helo/ehlo command
133506f25ae9SGregory Neil Shapiro **
133606f25ae9SGregory Neil Shapiro ** Parameters:
133706f25ae9SGregory Neil Shapiro ** g -- generic argument structure
133806f25ae9SGregory Neil Shapiro **
133906f25ae9SGregory Neil Shapiro ** Returns:
134006f25ae9SGregory Neil Shapiro ** continue or filter-specified value
134106f25ae9SGregory Neil Shapiro */
1342d0cef73dSGregory Neil Shapiro
134306f25ae9SGregory Neil Shapiro static int
st_helo(g)134406f25ae9SGregory Neil Shapiro st_helo(g)
134506f25ae9SGregory Neil Shapiro genarg *g;
134606f25ae9SGregory Neil Shapiro {
134706f25ae9SGregory Neil Shapiro sfsistat (*fi_helo) __P((SMFICTX *, char *));
134806f25ae9SGregory Neil Shapiro
134906f25ae9SGregory Neil Shapiro if (g == NULL)
135006f25ae9SGregory Neil Shapiro return _SMFIS_ABORT;
135106f25ae9SGregory Neil Shapiro mi_clr_macros(g->a_ctx, g->a_idx + 1);
135206f25ae9SGregory Neil Shapiro if (g->a_ctx->ctx_smfi != NULL &&
135306f25ae9SGregory Neil Shapiro (fi_helo = g->a_ctx->ctx_smfi->xxfi_helo) != NULL)
1354323f6dcbSGregory Neil Shapiro {
1355323f6dcbSGregory Neil Shapiro /* paranoia: check for terminating '\0' */
1356323f6dcbSGregory Neil Shapiro if (g->a_len == 0 || g->a_buf[g->a_len - 1] != '\0')
1357323f6dcbSGregory Neil Shapiro return MI_FAILURE;
135806f25ae9SGregory Neil Shapiro return (*fi_helo)(g->a_ctx, g->a_buf);
1359323f6dcbSGregory Neil Shapiro }
136006f25ae9SGregory Neil Shapiro return SMFIS_CONTINUE;
136106f25ae9SGregory Neil Shapiro }
1362d0cef73dSGregory Neil Shapiro
136340266059SGregory Neil Shapiro /*
136406f25ae9SGregory Neil Shapiro ** ST_HEADER -- header line
136506f25ae9SGregory Neil Shapiro **
136606f25ae9SGregory Neil Shapiro ** Parameters:
136706f25ae9SGregory Neil Shapiro ** g -- generic argument structure
136806f25ae9SGregory Neil Shapiro **
136906f25ae9SGregory Neil Shapiro ** Returns:
137006f25ae9SGregory Neil Shapiro ** continue or filter-specified value
137106f25ae9SGregory Neil Shapiro */
137206f25ae9SGregory Neil Shapiro
137306f25ae9SGregory Neil Shapiro static int
st_header(g)137406f25ae9SGregory Neil Shapiro st_header(g)
137506f25ae9SGregory Neil Shapiro genarg *g;
137606f25ae9SGregory Neil Shapiro {
137706f25ae9SGregory Neil Shapiro char *hf, *hv;
137806f25ae9SGregory Neil Shapiro sfsistat (*fi_header) __P((SMFICTX *, char *, char *));
137906f25ae9SGregory Neil Shapiro
138006f25ae9SGregory Neil Shapiro if (g == NULL)
138106f25ae9SGregory Neil Shapiro return _SMFIS_ABORT;
138206f25ae9SGregory Neil Shapiro if (g->a_ctx->ctx_smfi == NULL ||
138306f25ae9SGregory Neil Shapiro (fi_header = g->a_ctx->ctx_smfi->xxfi_header) == NULL)
138406f25ae9SGregory Neil Shapiro return SMFIS_CONTINUE;
138506f25ae9SGregory Neil Shapiro if (dec_arg2(g->a_buf, g->a_len, &hf, &hv) == MI_SUCCESS)
138606f25ae9SGregory Neil Shapiro return (*fi_header)(g->a_ctx, hf, hv);
138706f25ae9SGregory Neil Shapiro else
138806f25ae9SGregory Neil Shapiro return _SMFIS_ABORT;
138906f25ae9SGregory Neil Shapiro }
139006f25ae9SGregory Neil Shapiro
139106f25ae9SGregory Neil Shapiro #define ARGV_FCT(lf, rf, idx) \
139206f25ae9SGregory Neil Shapiro char **argv; \
139306f25ae9SGregory Neil Shapiro sfsistat (*lf) __P((SMFICTX *, char **)); \
139406f25ae9SGregory Neil Shapiro int r; \
139506f25ae9SGregory Neil Shapiro \
139606f25ae9SGregory Neil Shapiro if (g == NULL) \
139706f25ae9SGregory Neil Shapiro return _SMFIS_ABORT; \
139806f25ae9SGregory Neil Shapiro mi_clr_macros(g->a_ctx, g->a_idx + 1); \
139906f25ae9SGregory Neil Shapiro if (g->a_ctx->ctx_smfi == NULL || \
140006f25ae9SGregory Neil Shapiro (lf = g->a_ctx->ctx_smfi->rf) == NULL) \
140106f25ae9SGregory Neil Shapiro return SMFIS_CONTINUE; \
140206f25ae9SGregory Neil Shapiro if ((argv = dec_argv(g->a_buf, g->a_len)) == NULL) \
140306f25ae9SGregory Neil Shapiro return _SMFIS_ABORT; \
140406f25ae9SGregory Neil Shapiro r = (*lf)(g->a_ctx, argv); \
140506f25ae9SGregory Neil Shapiro free(argv); \
140606f25ae9SGregory Neil Shapiro return r;
140706f25ae9SGregory Neil Shapiro
140840266059SGregory Neil Shapiro /*
140906f25ae9SGregory Neil Shapiro ** ST_SENDER -- MAIL FROM command
141006f25ae9SGregory Neil Shapiro **
141106f25ae9SGregory Neil Shapiro ** Parameters:
141206f25ae9SGregory Neil Shapiro ** g -- generic argument structure
141306f25ae9SGregory Neil Shapiro **
141406f25ae9SGregory Neil Shapiro ** Returns:
141506f25ae9SGregory Neil Shapiro ** continue or filter-specified value
141606f25ae9SGregory Neil Shapiro */
141706f25ae9SGregory Neil Shapiro
141806f25ae9SGregory Neil Shapiro static int
st_sender(g)141906f25ae9SGregory Neil Shapiro st_sender(g)
142006f25ae9SGregory Neil Shapiro genarg *g;
142106f25ae9SGregory Neil Shapiro {
142206f25ae9SGregory Neil Shapiro ARGV_FCT(fi_envfrom, xxfi_envfrom, CI_MAIL)
142306f25ae9SGregory Neil Shapiro }
1424d0cef73dSGregory Neil Shapiro
142540266059SGregory Neil Shapiro /*
142606f25ae9SGregory Neil Shapiro ** ST_RCPT -- RCPT TO command
142706f25ae9SGregory Neil Shapiro **
142806f25ae9SGregory Neil Shapiro ** Parameters:
142906f25ae9SGregory Neil Shapiro ** g -- generic argument structure
143006f25ae9SGregory Neil Shapiro **
143106f25ae9SGregory Neil Shapiro ** Returns:
143206f25ae9SGregory Neil Shapiro ** continue or filter-specified value
143306f25ae9SGregory Neil Shapiro */
143406f25ae9SGregory Neil Shapiro
143506f25ae9SGregory Neil Shapiro static int
st_rcpt(g)143606f25ae9SGregory Neil Shapiro st_rcpt(g)
143706f25ae9SGregory Neil Shapiro genarg *g;
143806f25ae9SGregory Neil Shapiro {
143906f25ae9SGregory Neil Shapiro ARGV_FCT(fi_envrcpt, xxfi_envrcpt, CI_RCPT)
144006f25ae9SGregory Neil Shapiro }
1441e92d3f3fSGregory Neil Shapiro
1442e92d3f3fSGregory Neil Shapiro /*
1443e92d3f3fSGregory Neil Shapiro ** ST_UNKNOWN -- unrecognized or unimplemented command
1444e92d3f3fSGregory Neil Shapiro **
1445e92d3f3fSGregory Neil Shapiro ** Parameters:
1446e92d3f3fSGregory Neil Shapiro ** g -- generic argument structure
1447e92d3f3fSGregory Neil Shapiro **
1448e92d3f3fSGregory Neil Shapiro ** Returns:
1449e92d3f3fSGregory Neil Shapiro ** continue or filter-specified value
1450e92d3f3fSGregory Neil Shapiro */
1451e92d3f3fSGregory Neil Shapiro
1452e92d3f3fSGregory Neil Shapiro static int
st_unknown(g)1453e92d3f3fSGregory Neil Shapiro st_unknown(g)
1454e92d3f3fSGregory Neil Shapiro genarg *g;
1455e92d3f3fSGregory Neil Shapiro {
1456d0cef73dSGregory Neil Shapiro sfsistat (*fi_unknown) __P((SMFICTX *, const char *));
1457e92d3f3fSGregory Neil Shapiro
1458e92d3f3fSGregory Neil Shapiro if (g == NULL)
1459e92d3f3fSGregory Neil Shapiro return _SMFIS_ABORT;
1460e92d3f3fSGregory Neil Shapiro if (g->a_ctx->ctx_smfi != NULL &&
14616d13d6cbSGregory Neil Shapiro g->a_ctx->ctx_smfi->xxfi_version > 2 &&
1462e92d3f3fSGregory Neil Shapiro (fi_unknown = g->a_ctx->ctx_smfi->xxfi_unknown) != NULL)
1463d0cef73dSGregory Neil Shapiro return (*fi_unknown)(g->a_ctx, (const char *) g->a_buf);
1464e92d3f3fSGregory Neil Shapiro return SMFIS_CONTINUE;
1465e92d3f3fSGregory Neil Shapiro }
1466e92d3f3fSGregory Neil Shapiro
146740266059SGregory Neil Shapiro /*
146806f25ae9SGregory Neil Shapiro ** ST_MACROS -- deal with macros received from the MTA
146906f25ae9SGregory Neil Shapiro **
147006f25ae9SGregory Neil Shapiro ** Parameters:
147106f25ae9SGregory Neil Shapiro ** g -- generic argument structure
147206f25ae9SGregory Neil Shapiro **
147306f25ae9SGregory Neil Shapiro ** Returns:
147406f25ae9SGregory Neil Shapiro ** continue/keep
147506f25ae9SGregory Neil Shapiro **
147606f25ae9SGregory Neil Shapiro ** Side effects:
147706f25ae9SGregory Neil Shapiro ** set pointer in macro array to current values.
147806f25ae9SGregory Neil Shapiro */
147906f25ae9SGregory Neil Shapiro
148006f25ae9SGregory Neil Shapiro static int
st_macros(g)148106f25ae9SGregory Neil Shapiro st_macros(g)
148206f25ae9SGregory Neil Shapiro genarg *g;
148306f25ae9SGregory Neil Shapiro {
148406f25ae9SGregory Neil Shapiro int i;
148506f25ae9SGregory Neil Shapiro char **argv;
148606f25ae9SGregory Neil Shapiro
148706f25ae9SGregory Neil Shapiro if (g == NULL || g->a_len < 1)
148806f25ae9SGregory Neil Shapiro return _SMFIS_FAIL;
148906f25ae9SGregory Neil Shapiro if ((argv = dec_argv(g->a_buf + 1, g->a_len - 1)) == NULL)
149006f25ae9SGregory Neil Shapiro return _SMFIS_FAIL;
149106f25ae9SGregory Neil Shapiro switch (g->a_buf[0])
149206f25ae9SGregory Neil Shapiro {
149306f25ae9SGregory Neil Shapiro case SMFIC_CONNECT:
149406f25ae9SGregory Neil Shapiro i = CI_CONN;
149506f25ae9SGregory Neil Shapiro break;
149606f25ae9SGregory Neil Shapiro case SMFIC_HELO:
149706f25ae9SGregory Neil Shapiro i = CI_HELO;
149806f25ae9SGregory Neil Shapiro break;
149906f25ae9SGregory Neil Shapiro case SMFIC_MAIL:
150006f25ae9SGregory Neil Shapiro i = CI_MAIL;
150106f25ae9SGregory Neil Shapiro break;
150206f25ae9SGregory Neil Shapiro case SMFIC_RCPT:
150306f25ae9SGregory Neil Shapiro i = CI_RCPT;
150406f25ae9SGregory Neil Shapiro break;
1505d0cef73dSGregory Neil Shapiro case SMFIC_DATA:
1506d0cef73dSGregory Neil Shapiro i = CI_DATA;
1507d0cef73dSGregory Neil Shapiro break;
1508323f6dcbSGregory Neil Shapiro case SMFIC_BODYEOB:
1509323f6dcbSGregory Neil Shapiro i = CI_EOM;
1510323f6dcbSGregory Neil Shapiro break;
1511d0cef73dSGregory Neil Shapiro case SMFIC_EOH:
1512d0cef73dSGregory Neil Shapiro i = CI_EOH;
1513d0cef73dSGregory Neil Shapiro break;
151406f25ae9SGregory Neil Shapiro default:
151506f25ae9SGregory Neil Shapiro free(argv);
151606f25ae9SGregory Neil Shapiro return _SMFIS_FAIL;
151706f25ae9SGregory Neil Shapiro }
151806f25ae9SGregory Neil Shapiro if (g->a_ctx->ctx_mac_ptr[i] != NULL)
151906f25ae9SGregory Neil Shapiro free(g->a_ctx->ctx_mac_ptr[i]);
152006f25ae9SGregory Neil Shapiro if (g->a_ctx->ctx_mac_buf[i] != NULL)
152106f25ae9SGregory Neil Shapiro free(g->a_ctx->ctx_mac_buf[i]);
152206f25ae9SGregory Neil Shapiro g->a_ctx->ctx_mac_ptr[i] = argv;
152306f25ae9SGregory Neil Shapiro g->a_ctx->ctx_mac_buf[i] = g->a_buf;
152406f25ae9SGregory Neil Shapiro return _SMFIS_KEEP;
152506f25ae9SGregory Neil Shapiro }
1526d0cef73dSGregory Neil Shapiro
152740266059SGregory Neil Shapiro /*
152806f25ae9SGregory Neil Shapiro ** ST_QUIT -- quit command
152906f25ae9SGregory Neil Shapiro **
153006f25ae9SGregory Neil Shapiro ** Parameters:
153106f25ae9SGregory Neil Shapiro ** g -- generic argument structure
153206f25ae9SGregory Neil Shapiro **
153306f25ae9SGregory Neil Shapiro ** Returns:
153406f25ae9SGregory Neil Shapiro ** noreply
153506f25ae9SGregory Neil Shapiro */
153606f25ae9SGregory Neil Shapiro
1537a7ec597cSGregory Neil Shapiro /* ARGSUSED */
153806f25ae9SGregory Neil Shapiro static int
st_quit(g)153906f25ae9SGregory Neil Shapiro st_quit(g)
154006f25ae9SGregory Neil Shapiro genarg *g;
154106f25ae9SGregory Neil Shapiro {
1542d0cef73dSGregory Neil Shapiro sfsistat (*fi_close) __P((SMFICTX *));
1543d0cef73dSGregory Neil Shapiro
1544d0cef73dSGregory Neil Shapiro if (g == NULL)
1545d0cef73dSGregory Neil Shapiro return _SMFIS_ABORT;
1546d0cef73dSGregory Neil Shapiro if (g->a_ctx->ctx_smfi != NULL &&
1547d0cef73dSGregory Neil Shapiro (fi_close = g->a_ctx->ctx_smfi->xxfi_close) != NULL)
1548d0cef73dSGregory Neil Shapiro (void) (*fi_close)(g->a_ctx);
1549d0cef73dSGregory Neil Shapiro mi_clr_macros(g->a_ctx, 0);
155006f25ae9SGregory Neil Shapiro return _SMFIS_NOREPLY;
155106f25ae9SGregory Neil Shapiro }
1552d0cef73dSGregory Neil Shapiro
155340266059SGregory Neil Shapiro /*
155406f25ae9SGregory Neil Shapiro ** ST_BODYCHUNK -- deal with a piece of the mail body
155506f25ae9SGregory Neil Shapiro **
155606f25ae9SGregory Neil Shapiro ** Parameters:
155706f25ae9SGregory Neil Shapiro ** g -- generic argument structure
155806f25ae9SGregory Neil Shapiro **
155906f25ae9SGregory Neil Shapiro ** Returns:
156006f25ae9SGregory Neil Shapiro ** continue or filter-specified value
156106f25ae9SGregory Neil Shapiro */
156206f25ae9SGregory Neil Shapiro
156306f25ae9SGregory Neil Shapiro static int
st_bodychunk(g)156406f25ae9SGregory Neil Shapiro st_bodychunk(g)
156506f25ae9SGregory Neil Shapiro genarg *g;
156606f25ae9SGregory Neil Shapiro {
156740266059SGregory Neil Shapiro sfsistat (*fi_body) __P((SMFICTX *, unsigned char *, size_t));
156806f25ae9SGregory Neil Shapiro
156906f25ae9SGregory Neil Shapiro if (g == NULL)
157006f25ae9SGregory Neil Shapiro return _SMFIS_ABORT;
157106f25ae9SGregory Neil Shapiro if (g->a_ctx->ctx_smfi != NULL &&
157206f25ae9SGregory Neil Shapiro (fi_body = g->a_ctx->ctx_smfi->xxfi_body) != NULL)
157340266059SGregory Neil Shapiro return (*fi_body)(g->a_ctx, (unsigned char *)g->a_buf,
157440266059SGregory Neil Shapiro g->a_len);
157506f25ae9SGregory Neil Shapiro return SMFIS_CONTINUE;
157606f25ae9SGregory Neil Shapiro }
1577d0cef73dSGregory Neil Shapiro
157840266059SGregory Neil Shapiro /*
157906f25ae9SGregory Neil Shapiro ** ST_BODYEND -- deal with the last piece of the mail body
158006f25ae9SGregory Neil Shapiro **
158106f25ae9SGregory Neil Shapiro ** Parameters:
158206f25ae9SGregory Neil Shapiro ** g -- generic argument structure
158306f25ae9SGregory Neil Shapiro **
158406f25ae9SGregory Neil Shapiro ** Returns:
158506f25ae9SGregory Neil Shapiro ** continue or filter-specified value
158606f25ae9SGregory Neil Shapiro **
158706f25ae9SGregory Neil Shapiro ** Side effects:
158806f25ae9SGregory Neil Shapiro ** sends a reply for the body part (if non-empty).
158906f25ae9SGregory Neil Shapiro */
159006f25ae9SGregory Neil Shapiro
159106f25ae9SGregory Neil Shapiro static int
st_bodyend(g)159206f25ae9SGregory Neil Shapiro st_bodyend(g)
159306f25ae9SGregory Neil Shapiro genarg *g;
159406f25ae9SGregory Neil Shapiro {
159506f25ae9SGregory Neil Shapiro sfsistat r;
159640266059SGregory Neil Shapiro sfsistat (*fi_body) __P((SMFICTX *, unsigned char *, size_t));
159706f25ae9SGregory Neil Shapiro sfsistat (*fi_eom) __P((SMFICTX *));
159806f25ae9SGregory Neil Shapiro
159906f25ae9SGregory Neil Shapiro if (g == NULL)
160006f25ae9SGregory Neil Shapiro return _SMFIS_ABORT;
160106f25ae9SGregory Neil Shapiro r = SMFIS_CONTINUE;
160206f25ae9SGregory Neil Shapiro if (g->a_ctx->ctx_smfi != NULL)
160306f25ae9SGregory Neil Shapiro {
160406f25ae9SGregory Neil Shapiro if ((fi_body = g->a_ctx->ctx_smfi->xxfi_body) != NULL &&
160506f25ae9SGregory Neil Shapiro g->a_len > 0)
160606f25ae9SGregory Neil Shapiro {
160706f25ae9SGregory Neil Shapiro socket_t sd;
160806f25ae9SGregory Neil Shapiro struct timeval timeout;
160906f25ae9SGregory Neil Shapiro
161006f25ae9SGregory Neil Shapiro timeout.tv_sec = g->a_ctx->ctx_timeout;
161106f25ae9SGregory Neil Shapiro timeout.tv_usec = 0;
161206f25ae9SGregory Neil Shapiro sd = g->a_ctx->ctx_sd;
161340266059SGregory Neil Shapiro r = (*fi_body)(g->a_ctx, (unsigned char *)g->a_buf,
161440266059SGregory Neil Shapiro g->a_len);
161506f25ae9SGregory Neil Shapiro if (r != SMFIS_CONTINUE &&
161606f25ae9SGregory Neil Shapiro sendreply(r, sd, &timeout, g->a_ctx) != MI_SUCCESS)
161706f25ae9SGregory Neil Shapiro return _SMFIS_ABORT;
161806f25ae9SGregory Neil Shapiro }
161906f25ae9SGregory Neil Shapiro }
162006f25ae9SGregory Neil Shapiro if (r == SMFIS_CONTINUE &&
162106f25ae9SGregory Neil Shapiro (fi_eom = g->a_ctx->ctx_smfi->xxfi_eom) != NULL)
162206f25ae9SGregory Neil Shapiro return (*fi_eom)(g->a_ctx);
162306f25ae9SGregory Neil Shapiro return r;
162406f25ae9SGregory Neil Shapiro }
1625d0cef73dSGregory Neil Shapiro
162640266059SGregory Neil Shapiro /*
162706f25ae9SGregory Neil Shapiro ** ST_ABORTFCT -- deal with aborts
162806f25ae9SGregory Neil Shapiro **
162906f25ae9SGregory Neil Shapiro ** Parameters:
163006f25ae9SGregory Neil Shapiro ** g -- generic argument structure
163106f25ae9SGregory Neil Shapiro **
163206f25ae9SGregory Neil Shapiro ** Returns:
163306f25ae9SGregory Neil Shapiro ** abort or filter-specified value
163406f25ae9SGregory Neil Shapiro */
163506f25ae9SGregory Neil Shapiro
163606f25ae9SGregory Neil Shapiro static int
st_abortfct(g)163706f25ae9SGregory Neil Shapiro st_abortfct(g)
163806f25ae9SGregory Neil Shapiro genarg *g;
163906f25ae9SGregory Neil Shapiro {
164006f25ae9SGregory Neil Shapiro sfsistat (*fi_abort) __P((SMFICTX *));
164106f25ae9SGregory Neil Shapiro
164206f25ae9SGregory Neil Shapiro if (g == NULL)
164306f25ae9SGregory Neil Shapiro return _SMFIS_ABORT;
164406f25ae9SGregory Neil Shapiro if (g != NULL && g->a_ctx->ctx_smfi != NULL &&
164506f25ae9SGregory Neil Shapiro (fi_abort = g->a_ctx->ctx_smfi->xxfi_abort) != NULL)
164606f25ae9SGregory Neil Shapiro (void) (*fi_abort)(g->a_ctx);
164706f25ae9SGregory Neil Shapiro return _SMFIS_NOREPLY;
164806f25ae9SGregory Neil Shapiro }
1649d0cef73dSGregory Neil Shapiro
165040266059SGregory Neil Shapiro /*
165106f25ae9SGregory Neil Shapiro ** TRANS_OK -- is the state transition ok?
165206f25ae9SGregory Neil Shapiro **
165306f25ae9SGregory Neil Shapiro ** Parameters:
165406f25ae9SGregory Neil Shapiro ** old -- old state
165506f25ae9SGregory Neil Shapiro ** new -- new state
165606f25ae9SGregory Neil Shapiro **
165706f25ae9SGregory Neil Shapiro ** Returns:
165806f25ae9SGregory Neil Shapiro ** state transition ok
165906f25ae9SGregory Neil Shapiro */
166006f25ae9SGregory Neil Shapiro
166106f25ae9SGregory Neil Shapiro static bool
trans_ok(old,new)166206f25ae9SGregory Neil Shapiro trans_ok(old, new)
166306f25ae9SGregory Neil Shapiro int old, new;
166406f25ae9SGregory Neil Shapiro {
166506f25ae9SGregory Neil Shapiro int s, n;
166606f25ae9SGregory Neil Shapiro
166706f25ae9SGregory Neil Shapiro s = old;
1668af9557fdSGregory Neil Shapiro if (s >= SIZE_NEXT_STATES)
1669af9557fdSGregory Neil Shapiro return false;
167042e5d165SGregory Neil Shapiro do
167142e5d165SGregory Neil Shapiro {
167206f25ae9SGregory Neil Shapiro /* is this state transition allowed? */
167313bd1963SGregory Neil Shapiro if ((MI_MASK(new) & next_states[s]) != 0)
167440266059SGregory Neil Shapiro return true;
167506f25ae9SGregory Neil Shapiro
167606f25ae9SGregory Neil Shapiro /*
167706f25ae9SGregory Neil Shapiro ** no: try next state;
167806f25ae9SGregory Neil Shapiro ** this works since the relevant states are ordered
167906f25ae9SGregory Neil Shapiro ** strict sequentially
168006f25ae9SGregory Neil Shapiro */
168140266059SGregory Neil Shapiro
168206f25ae9SGregory Neil Shapiro n = s + 1;
1683af9557fdSGregory Neil Shapiro if (n >= SIZE_NEXT_STATES)
1684af9557fdSGregory Neil Shapiro return false;
168506f25ae9SGregory Neil Shapiro
168606f25ae9SGregory Neil Shapiro /*
168706f25ae9SGregory Neil Shapiro ** can we actually "skip" this state?
168806f25ae9SGregory Neil Shapiro ** see fix_stm() which sets this bit for those
168906f25ae9SGregory Neil Shapiro ** states which the filter program is not interested in
169006f25ae9SGregory Neil Shapiro */
169140266059SGregory Neil Shapiro
169206f25ae9SGregory Neil Shapiro if (bitset(NX_SKIP, next_states[n]))
169306f25ae9SGregory Neil Shapiro s = n;
169406f25ae9SGregory Neil Shapiro else
169540266059SGregory Neil Shapiro return false;
1696af9557fdSGregory Neil Shapiro } while (s < SIZE_NEXT_STATES);
169740266059SGregory Neil Shapiro return false;
169806f25ae9SGregory Neil Shapiro }
1699d0cef73dSGregory Neil Shapiro
170040266059SGregory Neil Shapiro /*
170106f25ae9SGregory Neil Shapiro ** FIX_STM -- add "skip" bits to the state transition table
170206f25ae9SGregory Neil Shapiro **
170306f25ae9SGregory Neil Shapiro ** Parameters:
170406f25ae9SGregory Neil Shapiro ** ctx -- context structure
170506f25ae9SGregory Neil Shapiro **
170606f25ae9SGregory Neil Shapiro ** Returns:
170706f25ae9SGregory Neil Shapiro ** None.
170806f25ae9SGregory Neil Shapiro **
170906f25ae9SGregory Neil Shapiro ** Side effects:
171006f25ae9SGregory Neil Shapiro ** may change state transition table.
171106f25ae9SGregory Neil Shapiro */
171206f25ae9SGregory Neil Shapiro
171306f25ae9SGregory Neil Shapiro static void
fix_stm(ctx)171406f25ae9SGregory Neil Shapiro fix_stm(ctx)
171506f25ae9SGregory Neil Shapiro SMFICTX_PTR ctx;
171606f25ae9SGregory Neil Shapiro {
171740266059SGregory Neil Shapiro unsigned long fl;
171806f25ae9SGregory Neil Shapiro
171906f25ae9SGregory Neil Shapiro if (ctx == NULL || ctx->ctx_smfi == NULL)
172006f25ae9SGregory Neil Shapiro return;
172106f25ae9SGregory Neil Shapiro fl = ctx->ctx_pflags;
172206f25ae9SGregory Neil Shapiro if (bitset(SMFIP_NOCONNECT, fl))
172306f25ae9SGregory Neil Shapiro next_states[ST_CONN] |= NX_SKIP;
172406f25ae9SGregory Neil Shapiro if (bitset(SMFIP_NOHELO, fl))
172506f25ae9SGregory Neil Shapiro next_states[ST_HELO] |= NX_SKIP;
172606f25ae9SGregory Neil Shapiro if (bitset(SMFIP_NOMAIL, fl))
172706f25ae9SGregory Neil Shapiro next_states[ST_MAIL] |= NX_SKIP;
172806f25ae9SGregory Neil Shapiro if (bitset(SMFIP_NORCPT, fl))
172906f25ae9SGregory Neil Shapiro next_states[ST_RCPT] |= NX_SKIP;
173006f25ae9SGregory Neil Shapiro if (bitset(SMFIP_NOHDRS, fl))
173106f25ae9SGregory Neil Shapiro next_states[ST_HDRS] |= NX_SKIP;
173206f25ae9SGregory Neil Shapiro if (bitset(SMFIP_NOEOH, fl))
173306f25ae9SGregory Neil Shapiro next_states[ST_EOHS] |= NX_SKIP;
173406f25ae9SGregory Neil Shapiro if (bitset(SMFIP_NOBODY, fl))
173506f25ae9SGregory Neil Shapiro next_states[ST_BODY] |= NX_SKIP;
1736d0cef73dSGregory Neil Shapiro if (bitset(SMFIP_NODATA, fl))
1737d0cef73dSGregory Neil Shapiro next_states[ST_DATA] |= NX_SKIP;
1738d0cef73dSGregory Neil Shapiro if (bitset(SMFIP_NOUNKNOWN, fl))
1739d0cef73dSGregory Neil Shapiro next_states[ST_UNKN] |= NX_SKIP;
174006f25ae9SGregory Neil Shapiro }
1741d0cef73dSGregory Neil Shapiro
174240266059SGregory Neil Shapiro /*
174306f25ae9SGregory Neil Shapiro ** DEC_ARGV -- split a buffer into a list of strings, NULL terminated
174406f25ae9SGregory Neil Shapiro **
174506f25ae9SGregory Neil Shapiro ** Parameters:
174606f25ae9SGregory Neil Shapiro ** buf -- buffer with several strings
174706f25ae9SGregory Neil Shapiro ** len -- length of buffer
174806f25ae9SGregory Neil Shapiro **
174906f25ae9SGregory Neil Shapiro ** Returns:
175006f25ae9SGregory Neil Shapiro ** array of pointers to the individual strings
175106f25ae9SGregory Neil Shapiro */
175206f25ae9SGregory Neil Shapiro
175306f25ae9SGregory Neil Shapiro static char **
dec_argv(buf,len)175406f25ae9SGregory Neil Shapiro dec_argv(buf, len)
175506f25ae9SGregory Neil Shapiro char *buf;
175606f25ae9SGregory Neil Shapiro size_t len;
175706f25ae9SGregory Neil Shapiro {
175806f25ae9SGregory Neil Shapiro char **s;
175906f25ae9SGregory Neil Shapiro size_t i;
176006f25ae9SGregory Neil Shapiro int elem, nelem;
176106f25ae9SGregory Neil Shapiro
176206f25ae9SGregory Neil Shapiro nelem = 0;
176306f25ae9SGregory Neil Shapiro for (i = 0; i < len; i++)
176406f25ae9SGregory Neil Shapiro {
176506f25ae9SGregory Neil Shapiro if (buf[i] == '\0')
176606f25ae9SGregory Neil Shapiro ++nelem;
176706f25ae9SGregory Neil Shapiro }
176806f25ae9SGregory Neil Shapiro if (nelem == 0)
176906f25ae9SGregory Neil Shapiro return NULL;
177006f25ae9SGregory Neil Shapiro
177106f25ae9SGregory Neil Shapiro /* last entry is only for the name */
177206f25ae9SGregory Neil Shapiro s = (char **)malloc((nelem + 1) * (sizeof *s));
177306f25ae9SGregory Neil Shapiro if (s == NULL)
177406f25ae9SGregory Neil Shapiro return NULL;
177506f25ae9SGregory Neil Shapiro s[0] = buf;
177606f25ae9SGregory Neil Shapiro for (i = 0, elem = 0; i < len && elem < nelem; i++)
177706f25ae9SGregory Neil Shapiro {
177806f25ae9SGregory Neil Shapiro if (buf[i] == '\0')
1779323f6dcbSGregory Neil Shapiro {
1780323f6dcbSGregory Neil Shapiro ++elem;
1781323f6dcbSGregory Neil Shapiro if (i + 1 >= len)
1782323f6dcbSGregory Neil Shapiro s[elem] = NULL;
1783323f6dcbSGregory Neil Shapiro else
1784323f6dcbSGregory Neil Shapiro s[elem] = &(buf[i + 1]);
1785323f6dcbSGregory Neil Shapiro }
178606f25ae9SGregory Neil Shapiro }
178706f25ae9SGregory Neil Shapiro
1788323f6dcbSGregory Neil Shapiro /* overwrite last entry (already done above, just paranoia) */
178906f25ae9SGregory Neil Shapiro s[elem] = NULL;
179006f25ae9SGregory Neil Shapiro return s;
179106f25ae9SGregory Neil Shapiro }
1792d0cef73dSGregory Neil Shapiro
179340266059SGregory Neil Shapiro /*
179406f25ae9SGregory Neil Shapiro ** DEC_ARG2 -- split a buffer into two strings
179506f25ae9SGregory Neil Shapiro **
179606f25ae9SGregory Neil Shapiro ** Parameters:
179706f25ae9SGregory Neil Shapiro ** buf -- buffer with two strings
179806f25ae9SGregory Neil Shapiro ** len -- length of buffer
179906f25ae9SGregory Neil Shapiro ** s1,s2 -- pointer to result strings
180006f25ae9SGregory Neil Shapiro **
180106f25ae9SGregory Neil Shapiro ** Returns:
180206f25ae9SGregory Neil Shapiro ** MI_FAILURE/MI_SUCCESS
180306f25ae9SGregory Neil Shapiro */
180406f25ae9SGregory Neil Shapiro
180506f25ae9SGregory Neil Shapiro static int
dec_arg2(buf,len,s1,s2)180606f25ae9SGregory Neil Shapiro dec_arg2(buf, len, s1, s2)
180706f25ae9SGregory Neil Shapiro char *buf;
180806f25ae9SGregory Neil Shapiro size_t len;
180906f25ae9SGregory Neil Shapiro char **s1;
181006f25ae9SGregory Neil Shapiro char **s2;
181106f25ae9SGregory Neil Shapiro {
181206f25ae9SGregory Neil Shapiro size_t i;
181306f25ae9SGregory Neil Shapiro
1814323f6dcbSGregory Neil Shapiro /* paranoia: check for terminating '\0' */
1815323f6dcbSGregory Neil Shapiro if (len == 0 || buf[len - 1] != '\0')
1816323f6dcbSGregory Neil Shapiro return MI_FAILURE;
181706f25ae9SGregory Neil Shapiro *s1 = buf;
181806f25ae9SGregory Neil Shapiro for (i = 1; i < len && buf[i] != '\0'; i++)
181906f25ae9SGregory Neil Shapiro continue;
182006f25ae9SGregory Neil Shapiro if (i >= len - 1)
182106f25ae9SGregory Neil Shapiro return MI_FAILURE;
182206f25ae9SGregory Neil Shapiro *s2 = buf + i + 1;
182306f25ae9SGregory Neil Shapiro return MI_SUCCESS;
182406f25ae9SGregory Neil Shapiro }
1825d0cef73dSGregory Neil Shapiro
182640266059SGregory Neil Shapiro /*
1827da7d7b9cSGregory Neil Shapiro ** MI_SENDOK -- is it ok for the filter to send stuff to the MTA?
182806f25ae9SGregory Neil Shapiro **
182906f25ae9SGregory Neil Shapiro ** Parameters:
183006f25ae9SGregory Neil Shapiro ** ctx -- context structure
183106f25ae9SGregory Neil Shapiro ** flag -- flag to check
183206f25ae9SGregory Neil Shapiro **
183306f25ae9SGregory Neil Shapiro ** Returns:
183406f25ae9SGregory Neil Shapiro ** sending allowed (in current state)
183506f25ae9SGregory Neil Shapiro */
183606f25ae9SGregory Neil Shapiro
183706f25ae9SGregory Neil Shapiro bool
mi_sendok(ctx,flag)183806f25ae9SGregory Neil Shapiro mi_sendok(ctx, flag)
183906f25ae9SGregory Neil Shapiro SMFICTX_PTR ctx;
184006f25ae9SGregory Neil Shapiro int flag;
184106f25ae9SGregory Neil Shapiro {
184206f25ae9SGregory Neil Shapiro if (ctx == NULL || ctx->ctx_smfi == NULL)
184340266059SGregory Neil Shapiro return false;
184440266059SGregory Neil Shapiro
184540266059SGregory Neil Shapiro /* did the milter request this operation? */
1846d0cef73dSGregory Neil Shapiro if (flag != 0 && !bitset(flag, ctx->ctx_aflags))
184740266059SGregory Neil Shapiro return false;
184840266059SGregory Neil Shapiro
184940266059SGregory Neil Shapiro /* are we in the correct state? It must be "End of Message". */
185006f25ae9SGregory Neil Shapiro return ctx->ctx_state == ST_ENDM;
185106f25ae9SGregory Neil Shapiro }
1852d0cef73dSGregory Neil Shapiro
1853d0cef73dSGregory Neil Shapiro #if _FFR_WORKERS_POOL
1854d0cef73dSGregory Neil Shapiro /*
1855d0cef73dSGregory Neil Shapiro ** MI_RD_SOCKET_READY - checks if the socket is ready for read(2)
1856d0cef73dSGregory Neil Shapiro **
1857d0cef73dSGregory Neil Shapiro ** Parameters:
1858d0cef73dSGregory Neil Shapiro ** sd -- socket_t
1859d0cef73dSGregory Neil Shapiro **
1860d0cef73dSGregory Neil Shapiro ** Returns:
1861d0cef73dSGregory Neil Shapiro ** true iff socket is ready for read(2)
1862d0cef73dSGregory Neil Shapiro */
1863d0cef73dSGregory Neil Shapiro
1864d0cef73dSGregory Neil Shapiro #define MI_RD_CMD_TO 1
1865d0cef73dSGregory Neil Shapiro #define MI_RD_MAX_ERR 16
1866d0cef73dSGregory Neil Shapiro
1867d0cef73dSGregory Neil Shapiro static bool
mi_rd_socket_ready(sd)1868d0cef73dSGregory Neil Shapiro mi_rd_socket_ready (sd)
1869d0cef73dSGregory Neil Shapiro socket_t sd;
1870d0cef73dSGregory Neil Shapiro {
1871d0cef73dSGregory Neil Shapiro int n;
1872d0cef73dSGregory Neil Shapiro int nerr = 0;
1873d0cef73dSGregory Neil Shapiro # if SM_CONF_POLL
1874d0cef73dSGregory Neil Shapiro struct pollfd pfd;
18755b0945b5SGregory Neil Shapiro # else
1876d0cef73dSGregory Neil Shapiro fd_set rd_set, exc_set;
18775b0945b5SGregory Neil Shapiro # endif
1878d0cef73dSGregory Neil Shapiro
1879d0cef73dSGregory Neil Shapiro do
1880d0cef73dSGregory Neil Shapiro {
1881d0cef73dSGregory Neil Shapiro # if SM_CONF_POLL
1882d0cef73dSGregory Neil Shapiro pfd.fd = sd;
1883d0cef73dSGregory Neil Shapiro pfd.events = POLLIN;
1884d0cef73dSGregory Neil Shapiro pfd.revents = 0;
1885d0cef73dSGregory Neil Shapiro
1886d0cef73dSGregory Neil Shapiro n = poll(&pfd, 1, MI_RD_CMD_TO);
1887d0cef73dSGregory Neil Shapiro # else /* SM_CONF_POLL */
1888d0cef73dSGregory Neil Shapiro struct timeval timeout;
1889d0cef73dSGregory Neil Shapiro
1890d0cef73dSGregory Neil Shapiro FD_ZERO(&rd_set);
1891d0cef73dSGregory Neil Shapiro FD_ZERO(&exc_set);
1892d0cef73dSGregory Neil Shapiro FD_SET(sd, &rd_set);
1893d0cef73dSGregory Neil Shapiro FD_SET(sd, &exc_set);
1894d0cef73dSGregory Neil Shapiro
1895d0cef73dSGregory Neil Shapiro timeout.tv_sec = MI_RD_CMD_TO / 1000;
1896d0cef73dSGregory Neil Shapiro timeout.tv_usec = 0;
1897d0cef73dSGregory Neil Shapiro n = select(sd + 1, &rd_set, NULL, &exc_set, &timeout);
1898d0cef73dSGregory Neil Shapiro # endif /* SM_CONF_POLL */
1899d0cef73dSGregory Neil Shapiro
1900d0cef73dSGregory Neil Shapiro if (n < 0)
1901d0cef73dSGregory Neil Shapiro {
1902d0cef73dSGregory Neil Shapiro if (errno == EINTR)
1903d0cef73dSGregory Neil Shapiro {
1904d0cef73dSGregory Neil Shapiro nerr++;
1905d0cef73dSGregory Neil Shapiro continue;
1906d0cef73dSGregory Neil Shapiro }
1907d0cef73dSGregory Neil Shapiro return true;
1908d0cef73dSGregory Neil Shapiro }
1909d0cef73dSGregory Neil Shapiro
1910d0cef73dSGregory Neil Shapiro if (n == 0)
1911d0cef73dSGregory Neil Shapiro return false;
1912d0cef73dSGregory Neil Shapiro break;
1913d0cef73dSGregory Neil Shapiro } while (nerr < MI_RD_MAX_ERR);
1914d0cef73dSGregory Neil Shapiro if (nerr >= MI_RD_MAX_ERR)
1915d0cef73dSGregory Neil Shapiro return false;
1916d0cef73dSGregory Neil Shapiro
1917d0cef73dSGregory Neil Shapiro # if SM_CONF_POLL
1918d0cef73dSGregory Neil Shapiro return (pfd.revents != 0);
19195b0945b5SGregory Neil Shapiro # else
1920d0cef73dSGregory Neil Shapiro return FD_ISSET(sd, &rd_set) || FD_ISSET(sd, &exc_set);
19215b0945b5SGregory Neil Shapiro # endif
1922d0cef73dSGregory Neil Shapiro }
1923d0cef73dSGregory Neil Shapiro #endif /* _FFR_WORKERS_POOL */
1924