engine.c revision 168993
1139776Simp/* 2130678Sphk * Copyright (c) 1999-2004, 2006, 2007 Sendmail, Inc. and its suppliers. 364880Sphk * All rights reserved. 464880Sphk * 564880Sphk * By using this file, you agree to the terms and conditions set 664880Sphk * forth in the LICENSE file which can be found at the top level of 764880Sphk * the sendmail distribution. 864880Sphk * 964880Sphk */ 1064880Sphk 1164880Sphk#include <sm/gen.h> 1264880SphkSM_RCSID("@(#)$Id: engine.c,v 8.157 2007/03/26 18:10:04 ca Exp $") 1364880Sphk 1464880Sphk#include "libmilter.h" 1564880Sphk 1664880Sphk#if NETINET || NETINET6 1764880Sphk# include <arpa/inet.h> 1864880Sphk#endif /* NETINET || NETINET6 */ 1964880Sphk 2064880Sphk/* generic argument for functions in the command table */ 2164880Sphkstruct arg_struct 2264880Sphk{ 2364880Sphk size_t a_len; /* length of buffer */ 2464880Sphk char *a_buf; /* argument string */ 2564880Sphk int a_idx; /* index for macro array */ 2664880Sphk SMFICTX_PTR a_ctx; /* context */ 2764880Sphk}; 2864880Sphk 2964880Sphktypedef struct arg_struct genarg; 3064880Sphk 3164880Sphk/* structure for commands received from MTA */ 3264880Sphkstruct cmdfct_t 3376166Smarkm{ 3464880Sphk char cm_cmd; /* command */ 3576166Smarkm int cm_argt; /* type of arguments expected */ 36150342Sphk int cm_next; /* next state */ 3776166Smarkm int cm_todo; /* what to do next */ 3864880Sphk int cm_macros; /* index for macros */ 3976166Smarkm int (*cm_fct) __P((genarg *)); /* function to execute */ 40150342Sphk}; 4165515Sphk 4276166Smarkmtypedef struct cmdfct_t cmdfct; 4364880Sphk 44150342Sphk/* possible values for cm_argt */ 4565515Sphk#define CM_ARG0 0 /* no args */ 4664880Sphk#define CM_ARG1 1 /* one arg (string) */ 47149144Sphk#define CM_ARG2 2 /* two args (strings) */ 4864880Sphk#define CM_ARGA 4 /* one string and _SOCK_ADDR */ 49163606Srwatson#define CM_ARGO 5 /* two integers */ 50163606Srwatson#define CM_ARGV 8 /* \0 separated list of args, NULL-terminated */ 51150342Sphk#define CM_ARGN 9 /* \0 separated list of args (strings) */ 52150342Sphk 53150342Sphk/* possible values for cm_todo */ 54150342Sphk#define CT_CONT 0x0000 /* continue reading commands */ 55163481Skib#define CT_IGNO 0x0001 /* continue even when error */ 5665515Sphk 57150342Sphk/* not needed right now, done via return code instead */ 5869767Sphk#define CT_KEEP 0x0004 /* keep buffer (contains symbols) */ 59150342Sphk#define CT_END 0x0008 /* last command of session, stop replying */ 60150342Sphk 61150342Sphk/* index in macro array: macros only for these commands */ 62150342Sphk#define CI_NONE (-1) 63150342Sphk#define CI_CONN 0 64141633Sphk#define CI_HELO 1 65150342Sphk#define CI_MAIL 2 66150342Sphk#define CI_RCPT 3 6765515Sphk#define CI_DATA 4 6865515Sphk#define CI_EOM 5 6965515Sphk#define CI_EOH 6 70150147Sphk#define CI_LAST CI_EOH 71150147Sphk#if CI_LAST < CI_DATA 72150342SphkERROR: do not compile with CI_LAST < CI_DATA 73150147Sphk#endif 74149146Sphk#if CI_LAST < CI_EOM 75203292SedERROR: do not compile with CI_LAST < CI_EOM 76203292Sed#endif 77149146Sphk#if CI_LAST < CI_EOH 78149146SphkERROR: do not compile with CI_LAST < CI_EOH 79149146Sphk#endif 80149146Sphk#if CI_LAST < CI_ENVRCPT 81149146SphkERROR: do not compile with CI_LAST < CI_ENVRCPT 82149146Sphk#endif 83150342Sphk#if CI_LAST < CI_ENVFROM 84203292SedERROR: do not compile with CI_LAST < CI_ENVFROM 85149146Sphk#endif 86149146Sphk#if CI_LAST < CI_HELO 87149146SphkERROR: do not compile with CI_LAST < CI_HELO 88149146Sphk#endif 89149146Sphk#if CI_LAST < CI_CONNECT 90203292SedERROR: do not compile with CI_LAST < CI_CONNECT 91203292Sed#endif 92150342Sphk#if CI_LAST >= MAX_MACROS_ENTRIES 93150342SphkERROR: do not compile with CI_LAST >= MAX_MACROS_ENTRIES 94203292Sed#endif 95203292Sed 96203292Sed/* function prototypes */ 97150342Sphkstatic int st_abortfct __P((genarg *)); 98203292Sedstatic int st_macros __P((genarg *)); 99150342Sphkstatic int st_optionneg __P((genarg *)); 100203292Sedstatic int st_bodychunk __P((genarg *)); 101203292Sedstatic int st_connectinfo __P((genarg *)); 102203292Sedstatic int st_bodyend __P((genarg *)); 103203292Sedstatic int st_helo __P((genarg *)); 104149146Sphkstatic int st_header __P((genarg *)); 105149146Sphkstatic int st_sender __P((genarg *)); 106149146Sphkstatic int st_rcpt __P((genarg *)); 107187864Sedstatic int st_unknown __P((genarg *)); 108187864Sedstatic int st_data __P((genarg *)); 109187864Sedstatic int st_eoh __P((genarg *)); 110149146Sphkstatic int st_quit __P((genarg *)); 111149146Sphkstatic int sendreply __P((sfsistat, socket_t, struct timeval *, SMFICTX_PTR)); 112149146Sphkstatic void fix_stm __P((SMFICTX_PTR)); 113149146Sphkstatic bool trans_ok __P((int, int)); 114150342Sphkstatic char **dec_argv __P((char *, size_t)); 115150342Sphkstatic int dec_arg2 __P((char *, size_t, char **, char **)); 11665515Sphk 117150342Sphk#if _FFR_WORKERS_POOL 118207729Skibstatic bool mi_rd_socket_ready __P((int)); 11965515Sphk#endif /* _FFR_WORKERS_POOL */ 120150342Sphk 121150342Sphk/* states */ 122183230Sed#define ST_NONE (-1) 12365515Sphk#define ST_INIT 0 /* initial state */ 124243039Skib#define ST_OPTS 1 /* option negotiation */ 125207729Skib#define ST_CONN 2 /* connection info */ 126207729Skib#define ST_HELO 3 /* helo */ 127207729Skib#define ST_MAIL 4 /* mail from */ 12865515Sphk#define ST_RCPT 5 /* rcpt to */ 129150342Sphk#define ST_DATA 6 /* data */ 130150342Sphk#define ST_HDRS 7 /* headers */ 131150342Sphk#define ST_EOHS 8 /* end of headers */ 132207729Skib#define ST_BODY 9 /* body */ 13365515Sphk#define ST_ENDM 10 /* end of message */ 134150342Sphk#define ST_QUIT 11 /* quit */ 13565515Sphk#define ST_ABRT 12 /* abort */ 136150342Sphk#define ST_UNKN 13 /* unknown SMTP command */ 137183230Sed#define ST_Q_NC 14 /* quit, new connection follows */ 138183230Sed#define ST_LAST ST_Q_NC /* last valid state */ 139207729Skib#define ST_SKIP 16 /* not a state but required for the state table */ 140183230Sed 141150342Sphk/* in a mail transaction? must be before eom according to spec. */ 14265515Sphk#define ST_IN_MAIL(st) ((st) >= ST_MAIL && (st) < ST_ENDM) 14365515Sphk 144213221Sjh/* 145213221Sjh** set of next states 146213221Sjh** each state (ST_*) corresponds to bit in an int value (1 << state) 147213221Sjh** each state has a set of allowed transitions ('or' of bits of states) 148213221Sjh** so a state transition is valid if the mask of the next state 149213221Sjh** is set in the NX_* value 150213221Sjh** this function is coded in trans_ok(), see below. 151213221Sjh*/ 152213221Sjh 153213221Sjh#define MI_MASK(x) (0x0001 << (x)) /* generate a bit "mask" for a state */ 154213221Sjh#define NX_INIT (MI_MASK(ST_OPTS)) 155213221Sjh#define NX_OPTS (MI_MASK(ST_CONN) | MI_MASK(ST_UNKN)) 156213221Sjh#define NX_CONN (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN)) 157213221Sjh#define NX_HELO (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN)) 158213221Sjh#define NX_MAIL (MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | MI_MASK(ST_UNKN)) 159213221Sjh#define NX_RCPT (MI_MASK(ST_HDRS) | MI_MASK(ST_EOHS) | MI_MASK(ST_DATA) | \ 160213221Sjh MI_MASK(ST_BODY) | MI_MASK(ST_ENDM) | \ 161213221Sjh MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | MI_MASK(ST_UNKN)) 162213221Sjh#define NX_DATA (MI_MASK(ST_EOHS) | MI_MASK(ST_HDRS) | MI_MASK(ST_ABRT)) 163213221Sjh#define NX_HDRS (MI_MASK(ST_EOHS) | MI_MASK(ST_HDRS) | MI_MASK(ST_ABRT)) 164213221Sjh#define NX_EOHS (MI_MASK(ST_BODY) | MI_MASK(ST_ENDM) | MI_MASK(ST_ABRT)) 165150342Sphk#define NX_BODY (MI_MASK(ST_ENDM) | MI_MASK(ST_BODY) | MI_MASK(ST_ABRT)) 166150342Sphk#define NX_ENDM (MI_MASK(ST_QUIT) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN) | \ 16765515Sphk MI_MASK(ST_Q_NC)) 168150342Sphk#define NX_QUIT 0 16965515Sphk#define NX_ABRT 0 170179828Skib#define NX_UNKN (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | \ 171150342Sphk MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | \ 172150342Sphk MI_MASK(ST_DATA) | \ 173226041Skib MI_MASK(ST_BODY) | MI_MASK(ST_UNKN) | \ 174150342Sphk MI_MASK(ST_ABRT) | MI_MASK(ST_QUIT) | MI_MASK(ST_Q_NC)) 175150342Sphk#define NX_Q_NC (MI_MASK(ST_CONN) | MI_MASK(ST_UNKN)) 176150342Sphk#define NX_SKIP MI_MASK(ST_SKIP) 17765515Sphk 17865515Sphkstatic int next_states[] = 179150342Sphk{ 180211226Sjh NX_INIT 18165132Sphk , NX_OPTS 18265132Sphk , NX_CONN 18365132Sphk , NX_HELO 18465132Sphk , NX_MAIL 18565132Sphk , NX_RCPT 18665132Sphk , NX_DATA 187211226Sjh , NX_HDRS 188211226Sjh , NX_EOHS 18965132Sphk , NX_BODY 19065132Sphk , NX_ENDM 19165132Sphk , NX_QUIT 19265132Sphk , NX_ABRT 193212826Sjh , NX_UNKN 194212826Sjh , NX_Q_NC 19565132Sphk}; 19665132Sphk 19765132Sphk#define SIZE_NEXT_STATES (sizeof(next_states) / sizeof(next_states[0])) 19865132Sphk 19964880Sphk/* commands received by milter */ 20064880Sphkstatic cmdfct cmds[] = 20164880Sphk{ 20264880Sphk {SMFIC_ABORT, CM_ARG0, ST_ABRT, CT_CONT, CI_NONE, st_abortfct } 20364880Sphk, {SMFIC_MACRO, CM_ARGV, ST_NONE, CT_KEEP, CI_NONE, st_macros } 20464880Sphk, {SMFIC_BODY, CM_ARG1, ST_BODY, CT_CONT, CI_NONE, st_bodychunk } 20564880Sphk, {SMFIC_CONNECT, CM_ARG2, ST_CONN, CT_CONT, CI_CONN, st_connectinfo } 206150342Sphk, {SMFIC_BODYEOB, CM_ARG1, ST_ENDM, CT_CONT, CI_EOM, st_bodyend } 207150342Sphk, {SMFIC_HELO, CM_ARG1, ST_HELO, CT_CONT, CI_HELO, st_helo } 20864880Sphk, {SMFIC_HEADER, CM_ARG2, ST_HDRS, CT_CONT, CI_NONE, st_header } 20964880Sphk, {SMFIC_MAIL, CM_ARGV, ST_MAIL, CT_CONT, CI_MAIL, st_sender } 21064880Sphk, {SMFIC_OPTNEG, CM_ARGO, ST_OPTS, CT_CONT, CI_NONE, st_optionneg } 21171822Sphk, {SMFIC_EOH, CM_ARG0, ST_EOHS, CT_CONT, CI_EOH, st_eoh } 21271822Sphk, {SMFIC_QUIT, CM_ARG0, ST_QUIT, CT_END, CI_NONE, st_quit } 21385979Sphk, {SMFIC_DATA, CM_ARG0, ST_DATA, CT_CONT, CI_DATA, st_data } 21465051Sphk, {SMFIC_RCPT, CM_ARGV, ST_RCPT, CT_IGNO, CI_RCPT, st_rcpt } 21565051Sphk, {SMFIC_UNKNOWN, CM_ARG1, ST_UNKN, CT_IGNO, CI_NONE, st_unknown } 216162398Skib, {SMFIC_QUIT_NC, CM_ARG0, ST_Q_NC, CT_CONT, CI_NONE, st_quit } 217101069Srwatson}; 218172930Srwatson 219101069Srwatson/* 22064880Sphk** Additional (internal) reply codes; 22164880Sphk** must be coordinated wit libmilter/mfapi.h 22264880Sphk*/ 22365051Sphk 224208951Sjh#define _SMFIS_KEEP 20 225208951Sjh#define _SMFIS_ABORT 21 226208951Sjh#define _SMFIS_OPTIONS 22 227208951Sjh#define _SMFIS_NOREPLY SMFIS_NOREPLY 228208951Sjh#define _SMFIS_FAIL (-1) 229208951Sjh#define _SMFIS_NONE (-2) 230208951Sjh 231208951Sjh/* 232208951Sjh** MI_ENGINE -- receive commands and process them 233208951Sjh** 234208951Sjh** Parameters: 235208951Sjh** ctx -- context structure 236208951Sjh** 237208951Sjh** Returns: 238208951Sjh** MI_FAILURE/MI_SUCCESS 239208951Sjh*/ 240208951Sjh 241208951Sjhint 242208951Sjhmi_engine(ctx) 243208951Sjh SMFICTX_PTR ctx; 244150342Sphk{ 24564880Sphk size_t len; 24665051Sphk int i; 24764880Sphk socket_t sd; 24864880Sphk int ret = MI_SUCCESS; 249150342Sphk int ncmds = sizeof(cmds) / sizeof(cmdfct); 25065051Sphk int curstate = ST_INIT; 25165051Sphk int newstate; 25265051Sphk bool call_abort; 25385979Sphk sfsistat r; 25465051Sphk char cmd; 25565051Sphk char *buf = NULL; 256150342Sphk genarg arg; 257150342Sphk struct timeval timeout; 258150342Sphk int (*f) __P((genarg *)); 259150342Sphk sfsistat (*fi_abort) __P((SMFICTX *)); 26065051Sphk sfsistat (*fi_close) __P((SMFICTX *)); 261211226Sjh 262211226Sjh arg.a_ctx = ctx; 263211226Sjh sd = ctx->ctx_sd; 264211226Sjh fi_abort = ctx->ctx_smfi->xxfi_abort; 265211226Sjh#if _FFR_WORKERS_POOL 266211226Sjh curstate = ctx->ctx_state; 26764880Sphk if (curstate == ST_INIT) 26864880Sphk { 26965051Sphk mi_clr_macros(ctx, 0); 27065051Sphk fix_stm(ctx); 271150342Sphk } 27265051Sphk#else /* _FFR_WORKERS_POOL */ 273211226Sjh mi_clr_macros(ctx, 0); 27465051Sphk fix_stm(ctx); 27565051Sphk#endif /* _FFR_WORKERS_POOL */ 276150342Sphk r = _SMFIS_NONE; 277150342Sphk do 278150342Sphk { 27965051Sphk /* call abort only if in a mail transaction */ 280150342Sphk call_abort = ST_IN_MAIL(curstate); 28165051Sphk timeout.tv_sec = ctx->ctx_timeout; 282216461Sjh timeout.tv_usec = 0; 283150342Sphk if (mi_stop() == MILTER_ABRT) 284150342Sphk { 285216461Sjh if (ctx->ctx_dbg > 3) 286150342Sphk sm_dprintf("[%ld] milter_abort\n", 28765051Sphk (long) ctx->ctx_id); 288150342Sphk ret = MI_FAILURE; 289172930Srwatson break; 290150342Sphk } 29164880Sphk 29264880Sphk /* 29364880Sphk ** Notice: buf is allocated by mi_rd_cmd() and it will 294150342Sphk ** usually be free()d after it has been used in f(). 295162398Skib ** However, if the function returns _SMFIS_KEEP then buf 296162398Skib ** contains macros and will not be free()d. 297162398Skib ** Hence r must be set to _SMFIS_NONE if a new buf is 298162398Skib ** allocated to avoid problem with housekeeping, esp. 299162398Skib ** if the code "break"s out of the loop. 300163481Skib */ 301212660Sjh 302212660Sjh#if _FFR_WORKERS_POOL 303212660Sjh /* Is the socket ready to be read ??? */ 304212660Sjh if (!mi_rd_socket_ready(sd)) 305212660Sjh { 306212660Sjh ret = MI_CONTINUE; 307212660Sjh break; 308212660Sjh } 309212660Sjh#endif /* _FFR_WORKERS_POOL */ 310212660Sjh 311212660Sjh r = _SMFIS_NONE; 312212660Sjh if ((buf = mi_rd_cmd(sd, &timeout, &cmd, &len, 313212660Sjh ctx->ctx_smfi->xxfi_name)) == NULL && 314212660Sjh cmd < SMFIC_VALIDCMD) 315212660Sjh { 316212660Sjh if (ctx->ctx_dbg > 5) 317212660Sjh sm_dprintf("[%ld] mi_engine: mi_rd_cmd error (%x)\n", 318212660Sjh (long) ctx->ctx_id, (int) cmd); 319212660Sjh 320212660Sjh /* 321212660Sjh ** eof is currently treated as failure -> 322212660Sjh ** abort() instead of close(), otherwise use: 323212660Sjh ** if (cmd != SMFIC_EOF) 324212660Sjh */ 325212660Sjh 326212660Sjh ret = MI_FAILURE; 327212660Sjh break; 328212826Sjh } 329212826Sjh if (ctx->ctx_dbg > 4) 330212660Sjh sm_dprintf("[%ld] got cmd '%c' len %d\n", 331212660Sjh (long) ctx->ctx_id, cmd, (int) len); 332212660Sjh for (i = 0; i < ncmds; i++) 333212660Sjh { 334212660Sjh if (cmd == cmds[i].cm_cmd) 335212660Sjh break; 336212660Sjh } 337212660Sjh if (i >= ncmds) 338212660Sjh { 339212660Sjh /* unknown command */ 340212660Sjh if (ctx->ctx_dbg > 1) 341212660Sjh sm_dprintf("[%ld] cmd '%c' unknown\n", 342212660Sjh (long) ctx->ctx_id, cmd); 343212660Sjh ret = MI_FAILURE; 344212660Sjh break; 345163481Skib } 346163481Skib if ((f = cmds[i].cm_fct) == NULL) 347163481Skib { 348162398Skib /* stop for now */ 349212660Sjh if (ctx->ctx_dbg > 1) 35064880Sphk sm_dprintf("[%ld] cmd '%c' not impl\n", 351212660Sjh (long) ctx->ctx_id, cmd); 352163481Skib ret = MI_FAILURE; 35364880Sphk break; 354162398Skib } 355162398Skib 356162398Skib /* is new state ok? */ 357212660Sjh newstate = cmds[i].cm_next; 358212660Sjh if (ctx->ctx_dbg > 5) 359212660Sjh sm_dprintf("[%ld] cur %x new %x nextmask %x\n", 360212660Sjh (long) ctx->ctx_id, 361212660Sjh curstate, newstate, next_states[curstate]); 362213215Sjh 363213215Sjh if (newstate != ST_NONE && !trans_ok(curstate, newstate)) 364213215Sjh { 365213215Sjh if (ctx->ctx_dbg > 1) 366212660Sjh sm_dprintf("[%ld] abort: cur %d (%x) new %d (%x) next %x\n", 367212660Sjh (long) ctx->ctx_id, 368212660Sjh curstate, MI_MASK(curstate), 369163481Skib newstate, MI_MASK(newstate), 370163481Skib next_states[curstate]); 371163481Skib 372163481Skib /* call abort only if in a mail transaction */ 373163481Skib if (fi_abort != NULL && call_abort) 374163481Skib (void) (*fi_abort)(ctx); 375163481Skib 376212660Sjh /* 377175202Sattilio ** try to reach the new state from HELO 378163481Skib ** if it can't be reached, ignore the command. 379163481Skib */ 380163481Skib 381212660Sjh curstate = ST_HELO; 382175294Sattilio if (!trans_ok(curstate, newstate)) 383163481Skib { 384163481Skib if (buf != NULL) 385163481Skib { 386163481Skib free(buf); 38764880Sphk buf = NULL; 388150151Sphk } 38964880Sphk continue; 39064880Sphk } 391101069Srwatson } 392172930Srwatson arg.a_len = len; 393101069Srwatson arg.a_buf = buf; 394150342Sphk if (newstate != ST_NONE) 395226041Skib { 396150342Sphk curstate = newstate; 397150342Sphk ctx->ctx_state = curstate; 398162398Skib } 399162398Skib arg.a_idx = cmds[i].cm_macros; 400212660Sjh call_abort = ST_IN_MAIL(curstate); 401212660Sjh 402212660Sjh /* call function to deal with command */ 403212660Sjh MI_MONITOR_BEGIN(ctx, cmd); 404212660Sjh r = (*f)(&arg); 405212660Sjh MI_MONITOR_END(ctx, cmd); 406212660Sjh if (r != _SMFIS_KEEP && buf != NULL) 40764880Sphk { 40864880Sphk free(buf); 409150342Sphk buf = NULL; 410150342Sphk } 411163481Skib if (sendreply(r, sd, &timeout, ctx) != MI_SUCCESS) 412163481Skib { 413150342Sphk ret = MI_FAILURE; 414150342Sphk break; 415150342Sphk } 416150342Sphk 41764880Sphk if (r == SMFIS_ACCEPT) 41864880Sphk { 41964880Sphk /* accept mail, no further actions taken */ 420150342Sphk curstate = ST_HELO; 421212660Sjh } 422212660Sjh else if (r == SMFIS_REJECT || r == SMFIS_DISCARD || 42364880Sphk r == SMFIS_TEMPFAIL) 424213215Sjh { 425213215Sjh /* 426213215Sjh ** further actions depend on current state 427213215Sjh ** if the IGNO bit is set: "ignore" the error, 428213215Sjh ** i.e., stay in the current state 429213215Sjh */ 43064880Sphk if (!bitset(CT_IGNO, cmds[i].cm_todo)) 43164880Sphk curstate = ST_HELO; 432150342Sphk } 433213215Sjh else if (r == _SMFIS_ABORT) 434213215Sjh { 435212660Sjh if (ctx->ctx_dbg > 5) 436212660Sjh sm_dprintf("[%ld] function returned abort\n", 437150342Sphk (long) ctx->ctx_id); 438150342Sphk ret = MI_FAILURE; 439212660Sjh break; 440212660Sjh } 44164880Sphk } while (!bitset(CT_END, cmds[i].cm_todo)); 442212660Sjh 443212660Sjh ctx->ctx_state = curstate; 444212660Sjh 445212660Sjh if (ret == MI_FAILURE) 44664880Sphk { 44764880Sphk /* call abort only if in a mail transaction */ 448150342Sphk if (fi_abort != NULL && call_abort) 449150342Sphk (void) (*fi_abort)(ctx); 450150342Sphk } 451150342Sphk 452150342Sphk /* has close been called? */ 453150342Sphk if (ctx->ctx_state != ST_QUIT 454150342Sphk#if _FFR_WORKERS_POOL 455150342Sphk && ret != MI_CONTINUE 456150342Sphk#endif /* _FFR_WORKERS_POOL */ 457150342Sphk ) 458150342Sphk { 45964880Sphk if ((fi_close = ctx->ctx_smfi->xxfi_close) != NULL) 460150342Sphk (void) (*fi_close)(ctx); 461150342Sphk } 462150342Sphk if (r != _SMFIS_KEEP && buf != NULL) 463150342Sphk free(buf); 464150342Sphk#if !_FFR_WORKERS_POOL 465150342Sphk mi_clr_macros(ctx, 0); 466150342Sphk#endif /* _FFR_WORKERS_POOL */ 467150342Sphk return ret; 468150342Sphk} 469150342Sphk 470150342Sphkstatic size_t milter_addsymlist __P((SMFICTX_PTR, char *, char **)); 471150342Sphk 472150342Sphkstatic size_t 473150342Sphkmilter_addsymlist(ctx, buf, newbuf) 474150342Sphk SMFICTX_PTR ctx; 475150342Sphk char *buf; 476150342Sphk char **newbuf; 477150342Sphk{ 478150342Sphk size_t len; 479150342Sphk int i; 480150342Sphk mi_int32 v; 481163481Skib char *buffer; 482163481Skib 483163481Skib SM_ASSERT(ctx != NULL); 484150342Sphk SM_ASSERT(buf != NULL); 485150342Sphk SM_ASSERT(newbuf != NULL); 48664880Sphk len = 0; 487150342Sphk for (i = 0; i < MAX_MACROS_ENTRIES; i++) 488150342Sphk { 48965051Sphk if (ctx->ctx_mac_list[i] != NULL) 490150342Sphk { 491211226Sjh len += strlen(ctx->ctx_mac_list[i]) + 1 + 49264880Sphk MILTER_LEN_BYTES; 49364880Sphk } 494150342Sphk } 495150342Sphk if (len > 0) 496150342Sphk { 497150342Sphk size_t offset; 498150342Sphk 499150342Sphk SM_ASSERT(len + MILTER_OPTLEN > len); 500150342Sphk len += MILTER_OPTLEN; 501150342Sphk buffer = malloc(len); 502150342Sphk if (buffer != NULL) 503150342Sphk { 504150342Sphk (void) memcpy(buffer, buf, MILTER_OPTLEN); 505150342Sphk offset = MILTER_OPTLEN; 506150342Sphk for (i = 0; i < MAX_MACROS_ENTRIES; i++) 507150342Sphk { 508150342Sphk size_t l; 509150342Sphk 510150342Sphk if (ctx->ctx_mac_list[i] == NULL) 511150342Sphk continue; 512150342Sphk 513150342Sphk SM_ASSERT(offset + MILTER_LEN_BYTES < len); 514150342Sphk v = htonl(i); 515150342Sphk (void) memcpy(buffer + offset, (void *) &v, 516150342Sphk MILTER_LEN_BYTES); 517150342Sphk offset += MILTER_LEN_BYTES; 518163481Skib l = strlen(ctx->ctx_mac_list[i]) + 1; 519163481Skib SM_ASSERT(offset + l <= len); 520163481Skib (void) memcpy(buffer + offset, 521163481Skib ctx->ctx_mac_list[i], l); 522150342Sphk offset += l; 523150342Sphk } 524150342Sphk } 525150342Sphk else 526150342Sphk { 527150342Sphk /* oops ... */ 528150342Sphk } 52965051Sphk } 530150342Sphk else 531150342Sphk { 532150342Sphk len = MILTER_OPTLEN; 533150342Sphk buffer = buf; 534150342Sphk } 535150342Sphk *newbuf = buffer; 536150342Sphk return len; 537150342Sphk} 538150342Sphk 539150342Sphk/* 540150342Sphk** GET_NR_BIT -- get "no reply" bit matching state 541150342Sphk** 542150342Sphk** Parameters: 543150342Sphk** state -- current protocol stage 544150342Sphk** 545150342Sphk** Returns: 546150342Sphk** 0: no matching bit 547150342Sphk** >0: the matching "no reply" bit 548150342Sphk*/ 549150342Sphk 550150342Sphkstatic unsigned long get_nr_bit __P((int)); 551150342Sphk 552150342Sphkstatic unsigned long 553150342Sphkget_nr_bit(state) 554150342Sphk int state; 555150342Sphk{ 556150342Sphk unsigned long bit; 557150342Sphk 558150342Sphk switch (state) 559150342Sphk { 56064880Sphk case ST_CONN: 561150342Sphk bit = SMFIP_NR_CONN; 562150342Sphk break; 563211226Sjh case ST_HELO: 564150342Sphk bit = SMFIP_NR_HELO; 565150342Sphk break; 566211226Sjh case ST_MAIL: 567211226Sjh bit = SMFIP_NR_MAIL; 568211226Sjh break; 569211226Sjh case ST_RCPT: 570211226Sjh bit = SMFIP_NR_RCPT; 571211226Sjh break; 572150342Sphk case ST_DATA: 573150342Sphk bit = SMFIP_NR_DATA; 574211226Sjh break; 575211226Sjh case ST_UNKN: 576211226Sjh bit = SMFIP_NR_UNKN; 577211226Sjh break; 578211226Sjh case ST_HDRS: 579150342Sphk bit = SMFIP_NR_HDR; 580211226Sjh break; 581211226Sjh case ST_EOHS: 582211226Sjh bit = SMFIP_NR_EOH; 583211226Sjh break; 584150342Sphk case ST_BODY: 585150342Sphk bit = SMFIP_NR_BODY; 586150342Sphk break; 587150342Sphk default: 588150342Sphk bit = 0; 589150342Sphk break; 590150342Sphk } 591150342Sphk return bit; 592150342Sphk} 593150342Sphk 594150342Sphk/* 595150342Sphk** SENDREPLY -- send a reply to the MTA 596150342Sphk** 597150342Sphk** Parameters: 598150342Sphk** r -- reply code 599150342Sphk** sd -- socket descriptor 600150342Sphk** timeout_ptr -- (ptr to) timeout to use for sending 601211226Sjh** ctx -- context structure 602150342Sphk** 603150342Sphk** Returns: 604101069Srwatson** MI_SUCCESS/MI_FAILURE 605172930Srwatson*/ 606150342Sphk 607101069Srwatsonstatic int 608150342Sphksendreply(r, sd, timeout_ptr, ctx) 609150342Sphk sfsistat r; 610150342Sphk socket_t sd; 611150342Sphk struct timeval *timeout_ptr; 612150342Sphk SMFICTX_PTR ctx; 613150342Sphk{ 614150342Sphk int ret; 615150342Sphk unsigned long bit; 616150342Sphk 617150342Sphk ret = MI_SUCCESS; 618150342Sphk 619150342Sphk bit = get_nr_bit(ctx->ctx_state); 62064880Sphk if (bit != 0 && (ctx->ctx_pflags & bit) != 0 && r != SMFIS_NOREPLY) 621150342Sphk { 622150342Sphk if (r >= SMFIS_CONTINUE && r < _SMFIS_KEEP) 62364880Sphk { 62464880Sphk /* milter said it wouldn't reply, but it lied... */ 625163481Skib smi_log(SMI_LOG_ERR, 626163481Skib "%s: milter claimed not to reply in state %d but did anyway %d\n", 627163481Skib ctx->ctx_smfi->xxfi_name, 628150342Sphk ctx->ctx_state, r); 629150342Sphk 630150342Sphk } 631224743Skib 632150342Sphk /* 633150342Sphk ** Force specified behavior, otherwise libmilter 634224743Skib ** and MTA will fail to communicate properly. 635224743Skib */ 636150342Sphk 637150342Sphk switch (r) 638150342Sphk { 639224743Skib case SMFIS_CONTINUE: 640150342Sphk case SMFIS_TEMPFAIL: 641150342Sphk case SMFIS_REJECT: 642163481Skib case SMFIS_DISCARD: 643163481Skib case SMFIS_ACCEPT: 644163481Skib case SMFIS_SKIP: 645150342Sphk case _SMFIS_OPTIONS: 646150342Sphk r = SMFIS_NOREPLY; 647150342Sphk break; 648150342Sphk } 649150342Sphk } 650150342Sphk 651150342Sphk switch (r) 652150342Sphk { 653150342Sphk case SMFIS_CONTINUE: 654150342Sphk ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_CONTINUE, NULL, 0); 655130678Sphk break; 656130678Sphk case SMFIS_TEMPFAIL: 657130678Sphk case SMFIS_REJECT: 658249583Sgabor if (ctx->ctx_reply != NULL && 659130678Sphk ((r == SMFIS_TEMPFAIL && *ctx->ctx_reply == '4') || 660130678Sphk (r == SMFIS_REJECT && *ctx->ctx_reply == '5'))) 661111730Sphk { 662130585Sphk ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_REPLYCODE, 66364880Sphk ctx->ctx_reply, 664150342Sphk strlen(ctx->ctx_reply) + 1); 66565515Sphk free(ctx->ctx_reply); 666150342Sphk ctx->ctx_reply = NULL; 667179828Skib } 668150342Sphk else 669150342Sphk { 670150342Sphk ret = mi_wr_cmd(sd, timeout_ptr, r == SMFIS_REJECT ? 671150342Sphk SMFIR_REJECT : SMFIR_TEMPFAIL, NULL, 0); 672130678Sphk } 67364880Sphk break; 67464880Sphk case SMFIS_DISCARD: 675111730Sphk ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_DISCARD, NULL, 0); 676130585Sphk break; 67764880Sphk case SMFIS_ACCEPT: 678150342Sphk ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_ACCEPT, NULL, 0); 67965515Sphk break; 680150342Sphk case SMFIS_SKIP: 681179828Skib ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_SKIP, NULL, 0); 682150342Sphk break; 683130678Sphk case _SMFIS_OPTIONS: 68464880Sphk { 685150342Sphk mi_int32 v; 686226041Skib size_t len; 687226041Skib char *buffer; 688226041Skib char buf[MILTER_OPTLEN]; 689226041Skib 690226041Skib v = htonl(ctx->ctx_prot_vers2mta); 691226041Skib (void) memcpy(&(buf[0]), (void *) &v, 692226041Skib MILTER_LEN_BYTES); 693226041Skib v = htonl(ctx->ctx_aflags); 694226041Skib (void) memcpy(&(buf[MILTER_LEN_BYTES]), (void *) &v, 695226041Skib MILTER_LEN_BYTES); 696226041Skib v = htonl(ctx->ctx_pflags2mta); 697226041Skib (void) memcpy(&(buf[MILTER_LEN_BYTES * 2]), 698226041Skib (void *) &v, MILTER_LEN_BYTES); 699226041Skib len = milter_addsymlist(ctx, buf, &buffer); 700226041Skib if (buffer != NULL) 701150342Sphk ret = mi_wr_cmd(sd, timeout_ptr, SMFIC_OPTNEG, 702150342Sphk buffer, len); 703150342Sphk else 704150342Sphk ret = MI_FAILURE; 705150342Sphk } 706150342Sphk break; 707150342Sphk case SMFIS_NOREPLY: 708150342Sphk if (bit != 0 && 709 (ctx->ctx_pflags & bit) != 0 && 710 (ctx->ctx_mta_pflags & bit) == 0) 711 { 712 /* 713 ** milter doesn't want to send a reply, 714 ** but the MTA doesn't have that feature: fake it. 715 */ 716 717 ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_CONTINUE, NULL, 718 0); 719 } 720 break; 721 default: /* don't send a reply */ 722 break; 723 } 724 return ret; 725} 726 727/* 728** CLR_MACROS -- clear set of macros starting from a given index 729** 730** Parameters: 731** ctx -- context structure 732** m -- index from which to clear all macros 733** 734** Returns: 735** None. 736*/ 737 738void 739mi_clr_macros(ctx, m) 740 SMFICTX_PTR ctx; 741 int m; 742{ 743 int i; 744 745 for (i = m; i < MAX_MACROS_ENTRIES; i++) 746 { 747 if (ctx->ctx_mac_ptr[i] != NULL) 748 { 749 free(ctx->ctx_mac_ptr[i]); 750 ctx->ctx_mac_ptr[i] = NULL; 751 } 752 if (ctx->ctx_mac_buf[i] != NULL) 753 { 754 free(ctx->ctx_mac_buf[i]); 755 ctx->ctx_mac_buf[i] = NULL; 756 } 757 } 758} 759 760/* 761** ST_OPTIONNEG -- negotiate options 762** 763** Parameters: 764** g -- generic argument structure 765** 766** Returns: 767** abort/send options/continue 768*/ 769 770static int 771st_optionneg(g) 772 genarg *g; 773{ 774 mi_int32 i, v, fake_pflags; 775 SMFICTX_PTR ctx; 776 int (*fi_negotiate) __P((SMFICTX *, 777 unsigned long, unsigned long, 778 unsigned long, unsigned long, 779 unsigned long *, unsigned long *, 780 unsigned long *, unsigned long *)); 781 782 if (g == NULL || g->a_ctx->ctx_smfi == NULL) 783 return SMFIS_CONTINUE; 784 ctx = g->a_ctx; 785 mi_clr_macros(ctx, g->a_idx + 1); 786 ctx->ctx_prot_vers = SMFI_PROT_VERSION; 787 788 /* check for minimum length */ 789 if (g->a_len < MILTER_OPTLEN) 790 { 791 smi_log(SMI_LOG_ERR, 792 "%s: st_optionneg[%ld]: len too short %d < %d", 793 ctx->ctx_smfi->xxfi_name, 794 (long) ctx->ctx_id, (int) g->a_len, 795 MILTER_OPTLEN); 796 return _SMFIS_ABORT; 797 } 798 799 /* protocol version */ 800 (void) memcpy((void *) &i, (void *) &(g->a_buf[0]), MILTER_LEN_BYTES); 801 v = ntohl(i); 802 803#define SMFI_PROT_VERSION_MIN 2 804 805 /* check for minimum version */ 806 if (v < SMFI_PROT_VERSION_MIN) 807 { 808 smi_log(SMI_LOG_ERR, 809 "%s: st_optionneg[%ld]: protocol version too old %d < %d", 810 ctx->ctx_smfi->xxfi_name, 811 (long) ctx->ctx_id, v, SMFI_PROT_VERSION_MIN); 812 return _SMFIS_ABORT; 813 } 814 ctx->ctx_mta_prot_vers = v; 815 if (ctx->ctx_prot_vers < ctx->ctx_mta_prot_vers) 816 ctx->ctx_prot_vers2mta = ctx->ctx_prot_vers; 817 else 818 ctx->ctx_prot_vers2mta = ctx->ctx_mta_prot_vers; 819 820 (void) memcpy((void *) &i, (void *) &(g->a_buf[MILTER_LEN_BYTES]), 821 MILTER_LEN_BYTES); 822 v = ntohl(i); 823 824 /* no flags? set to default value for V1 actions */ 825 if (v == 0) 826 v = SMFI_V1_ACTS; 827 ctx->ctx_mta_aflags = v; /* MTA action flags */ 828 829 (void) memcpy((void *) &i, (void *) &(g->a_buf[MILTER_LEN_BYTES * 2]), 830 MILTER_LEN_BYTES); 831 v = ntohl(i); 832 833 /* no flags? set to default value for V1 protocol */ 834 if (v == 0) 835 v = SMFI_V1_PROT; 836 ctx->ctx_mta_pflags = v; /* MTA protocol flags */ 837 838 /* 839 ** Copy flags from milter struct into libmilter context; 840 ** this variable will be used later on to check whether 841 ** the MTA "actions" can fulfill the milter requirements, 842 ** but it may be overwritten by the negotiate callback. 843 */ 844 845 ctx->ctx_aflags = ctx->ctx_smfi->xxfi_flags; 846 fake_pflags = SMFIP_NR_CONN 847 |SMFIP_NR_HELO 848 |SMFIP_NR_MAIL 849 |SMFIP_NR_RCPT 850 |SMFIP_NR_DATA 851 |SMFIP_NR_UNKN 852 |SMFIP_NR_HDR 853 |SMFIP_NR_EOH 854 |SMFIP_NR_BODY 855 ; 856 857 if (g->a_ctx->ctx_smfi != NULL && 858 g->a_ctx->ctx_smfi->xxfi_version > 4 && 859 (fi_negotiate = g->a_ctx->ctx_smfi->xxfi_negotiate) != NULL) 860 { 861 int r; 862 unsigned long m_aflags, m_pflags, m_f2, m_f3; 863 864 /* 865 ** let milter decide whether the features offered by the 866 ** MTA are "good enough". 867 ** Notes: 868 ** - libmilter can "fake" some features (e.g., SMFIP_NR_HDR) 869 ** - m_f2, m_f3 are for future extensions 870 */ 871 872 m_f2 = m_f3 = 0; 873 m_aflags = ctx->ctx_mta_aflags; 874 m_pflags = ctx->ctx_pflags; 875 if ((SMFIP_SKIP & ctx->ctx_mta_pflags) != 0) 876 m_pflags |= SMFIP_SKIP; 877 r = fi_negotiate(g->a_ctx, 878 ctx->ctx_mta_aflags, 879 ctx->ctx_mta_pflags|fake_pflags, 880 0, 0, 881 &m_aflags, &m_pflags, &m_f2, &m_f3); 882 883 /* 884 ** Types of protocol flags (pflags): 885 ** 1. do NOT send protocol step X 886 ** 2. MTA can do/understand something extra (SKIP, 887 ** send unknown RCPTs) 888 ** 3. MTA can deal with "no reply" for various protocol steps 889 ** Note: this mean that it isn't possible to simply set all 890 ** flags to get "everything": 891 ** setting a flag of type 1 turns off a step 892 ** (it should be the other way around: 893 ** a flag means a protocol step can be sent) 894 ** setting a flag of type 3 requires that milter 895 ** never sends a reply for the corresponding step. 896 ** Summary: the "negation" of protocol flags is causing 897 ** problems, but at least for type 3 there is no simple 898 ** solution. 899 ** 900 ** What should "all options" mean? 901 ** send all protocol steps _except_ those for which there is 902 ** no callback (currently registered in ctx_pflags) 903 ** expect SKIP as return code? Yes 904 ** send unknown RCPTs? No, 905 ** must be explicitly requested? 906 ** "no reply" for some protocol steps? No, 907 ** must be explicitly requested. 908 */ 909 910 if (SMFIS_ALL_OPTS == r) 911 { 912 ctx->ctx_aflags = ctx->ctx_mta_aflags; 913 ctx->ctx_pflags2mta = ctx->ctx_pflags; 914 if ((SMFIP_SKIP & ctx->ctx_mta_pflags) != 0) 915 ctx->ctx_pflags2mta |= SMFIP_SKIP; 916 } 917 else if (r != SMFIS_CONTINUE) 918 { 919 smi_log(SMI_LOG_ERR, 920 "%s: st_optionneg[%ld]: xxfi_negotiate returned %d (protocol options=0x%lx, actions=0x%lx)", 921 ctx->ctx_smfi->xxfi_name, 922 (long) ctx->ctx_id, r, ctx->ctx_mta_pflags, 923 ctx->ctx_mta_aflags); 924 return _SMFIS_ABORT; 925 } 926 else 927 { 928 ctx->ctx_aflags = m_aflags; 929 ctx->ctx_pflags = m_pflags; 930 ctx->ctx_pflags2mta = m_pflags; 931 } 932 933 /* check whether some flags need to be "faked" */ 934 i = ctx->ctx_pflags2mta; 935 if ((ctx->ctx_mta_pflags & i) != i) 936 { 937 unsigned int idx; 938 unsigned long b; 939 940 /* 941 ** If some behavior can be faked (set in fake_pflags), 942 ** but the MTA doesn't support it, then unset 943 ** that flag in the value that is sent to the MTA. 944 */ 945 946 for (idx = 0; idx < 32; idx++) 947 { 948 b = 1 << idx; 949 if ((ctx->ctx_mta_pflags & b) != b && 950 (fake_pflags & b) == b) 951 ctx->ctx_pflags2mta &= ~b; 952 } 953 } 954 } 955 else 956 { 957 /* 958 ** Set the protocol flags based on the values determined 959 ** in mi_listener() which checked the defined callbacks. 960 */ 961 962 ctx->ctx_pflags2mta = ctx->ctx_pflags; 963 } 964 965 /* check whether actions and protocol requirements can be satisfied */ 966 i = ctx->ctx_aflags; 967 if ((i & ctx->ctx_mta_aflags) != i) 968 { 969 smi_log(SMI_LOG_ERR, 970 "%s: st_optionneg[%ld]: 0x%lx does not fulfill action requirements 0x%x", 971 ctx->ctx_smfi->xxfi_name, 972 (long) ctx->ctx_id, ctx->ctx_mta_aflags, i); 973 return _SMFIS_ABORT; 974 } 975 976 i = ctx->ctx_pflags2mta; 977 if ((ctx->ctx_mta_pflags & i) != i) 978 { 979 /* 980 ** Older MTAs do not support some protocol steps. 981 ** As this protocol is a bit "wierd" (it asks for steps 982 ** NOT to be taken/sent) we have to check whether we 983 ** should turn off those "negative" requests. 984 ** Currently these are only SMFIP_NODATA and SMFIP_NOUNKNOWN. 985 */ 986 987 if (bitset(SMFIP_NODATA, ctx->ctx_pflags2mta) && 988 !bitset(SMFIP_NODATA, ctx->ctx_mta_pflags)) 989 ctx->ctx_pflags2mta &= ~SMFIP_NODATA; 990 if (bitset(SMFIP_NOUNKNOWN, ctx->ctx_pflags2mta) && 991 !bitset(SMFIP_NOUNKNOWN, ctx->ctx_mta_pflags)) 992 ctx->ctx_pflags2mta &= ~SMFIP_NOUNKNOWN; 993 i = ctx->ctx_pflags2mta; 994 } 995 996 if ((ctx->ctx_mta_pflags & i) != i) 997 { 998 smi_log(SMI_LOG_ERR, 999 "%s: st_optionneg[%ld]: 0x%lx does not fulfill protocol requirements 0x%x", 1000 ctx->ctx_smfi->xxfi_name, 1001 (long) ctx->ctx_id, ctx->ctx_mta_pflags, i); 1002 return _SMFIS_ABORT; 1003 } 1004 1005 if (ctx->ctx_dbg > 3) 1006 sm_dprintf("[%ld] milter_negotiate:" 1007 " mta_actions=0x%lx, mta_flags=0x%lx" 1008 " actions=0x%lx, flags=0x%lx\n" 1009 , (long) ctx->ctx_id 1010 , ctx->ctx_mta_aflags, ctx->ctx_mta_pflags 1011 , ctx->ctx_aflags, ctx->ctx_pflags); 1012 1013 return _SMFIS_OPTIONS; 1014} 1015 1016/* 1017** ST_CONNECTINFO -- receive connection information 1018** 1019** Parameters: 1020** g -- generic argument structure 1021** 1022** Returns: 1023** continue or filter-specified value 1024*/ 1025 1026static int 1027st_connectinfo(g) 1028 genarg *g; 1029{ 1030 size_t l; 1031 size_t i; 1032 char *s, family; 1033 unsigned short port = 0; 1034 _SOCK_ADDR sockaddr; 1035 sfsistat (*fi_connect) __P((SMFICTX *, char *, _SOCK_ADDR *)); 1036 1037 if (g == NULL) 1038 return _SMFIS_ABORT; 1039 mi_clr_macros(g->a_ctx, g->a_idx + 1); 1040 if (g->a_ctx->ctx_smfi == NULL || 1041 (fi_connect = g->a_ctx->ctx_smfi->xxfi_connect) == NULL) 1042 return SMFIS_CONTINUE; 1043 1044 s = g->a_buf; 1045 i = 0; 1046 l = g->a_len; 1047 while (s[i] != '\0' && i <= l) 1048 ++i; 1049 if (i + 1 >= l) 1050 return _SMFIS_ABORT; 1051 1052 /* Move past trailing \0 in host string */ 1053 i++; 1054 family = s[i++]; 1055 (void) memset(&sockaddr, '\0', sizeof sockaddr); 1056 if (family != SMFIA_UNKNOWN) 1057 { 1058 if (i + sizeof port >= l) 1059 { 1060 smi_log(SMI_LOG_ERR, 1061 "%s: connect[%ld]: wrong len %d >= %d", 1062 g->a_ctx->ctx_smfi->xxfi_name, 1063 (long) g->a_ctx->ctx_id, (int) i, (int) l); 1064 return _SMFIS_ABORT; 1065 } 1066 (void) memcpy((void *) &port, (void *) (s + i), 1067 sizeof port); 1068 i += sizeof port; 1069 1070 /* make sure string is terminated */ 1071 if (s[l - 1] != '\0') 1072 return _SMFIS_ABORT; 1073# if NETINET 1074 if (family == SMFIA_INET) 1075 { 1076 if (inet_aton(s + i, (struct in_addr *) &sockaddr.sin.sin_addr) 1077 != 1) 1078 { 1079 smi_log(SMI_LOG_ERR, 1080 "%s: connect[%ld]: inet_aton failed", 1081 g->a_ctx->ctx_smfi->xxfi_name, 1082 (long) g->a_ctx->ctx_id); 1083 return _SMFIS_ABORT; 1084 } 1085 sockaddr.sa.sa_family = AF_INET; 1086 if (port > 0) 1087 sockaddr.sin.sin_port = port; 1088 } 1089 else 1090# endif /* NETINET */ 1091# if NETINET6 1092 if (family == SMFIA_INET6) 1093 { 1094 if (mi_inet_pton(AF_INET6, s + i, 1095 &sockaddr.sin6.sin6_addr) != 1) 1096 { 1097 smi_log(SMI_LOG_ERR, 1098 "%s: connect[%ld]: mi_inet_pton failed", 1099 g->a_ctx->ctx_smfi->xxfi_name, 1100 (long) g->a_ctx->ctx_id); 1101 return _SMFIS_ABORT; 1102 } 1103 sockaddr.sa.sa_family = AF_INET6; 1104 if (port > 0) 1105 sockaddr.sin6.sin6_port = port; 1106 } 1107 else 1108# endif /* NETINET6 */ 1109# if NETUNIX 1110 if (family == SMFIA_UNIX) 1111 { 1112 if (sm_strlcpy(sockaddr.sunix.sun_path, s + i, 1113 sizeof sockaddr.sunix.sun_path) >= 1114 sizeof sockaddr.sunix.sun_path) 1115 { 1116 smi_log(SMI_LOG_ERR, 1117 "%s: connect[%ld]: path too long", 1118 g->a_ctx->ctx_smfi->xxfi_name, 1119 (long) g->a_ctx->ctx_id); 1120 return _SMFIS_ABORT; 1121 } 1122 sockaddr.sunix.sun_family = AF_UNIX; 1123 } 1124 else 1125# endif /* NETUNIX */ 1126 { 1127 smi_log(SMI_LOG_ERR, 1128 "%s: connect[%ld]: unknown family %d", 1129 g->a_ctx->ctx_smfi->xxfi_name, 1130 (long) g->a_ctx->ctx_id, family); 1131 return _SMFIS_ABORT; 1132 } 1133 } 1134 return (*fi_connect)(g->a_ctx, g->a_buf, 1135 family != SMFIA_UNKNOWN ? &sockaddr : NULL); 1136} 1137 1138/* 1139** ST_EOH -- end of headers 1140** 1141** Parameters: 1142** g -- generic argument structure 1143** 1144** Returns: 1145** continue or filter-specified value 1146*/ 1147 1148static int 1149st_eoh(g) 1150 genarg *g; 1151{ 1152 sfsistat (*fi_eoh) __P((SMFICTX *)); 1153 1154 if (g == NULL) 1155 return _SMFIS_ABORT; 1156 if (g->a_ctx->ctx_smfi != NULL && 1157 (fi_eoh = g->a_ctx->ctx_smfi->xxfi_eoh) != NULL) 1158 return (*fi_eoh)(g->a_ctx); 1159 return SMFIS_CONTINUE; 1160} 1161 1162/* 1163** ST_DATA -- DATA command 1164** 1165** Parameters: 1166** g -- generic argument structure 1167** 1168** Returns: 1169** continue or filter-specified value 1170*/ 1171 1172static int 1173st_data(g) 1174 genarg *g; 1175{ 1176 sfsistat (*fi_data) __P((SMFICTX *)); 1177 1178 if (g == NULL) 1179 return _SMFIS_ABORT; 1180 if (g->a_ctx->ctx_smfi != NULL && 1181 g->a_ctx->ctx_smfi->xxfi_version > 3 && 1182 (fi_data = g->a_ctx->ctx_smfi->xxfi_data) != NULL) 1183 return (*fi_data)(g->a_ctx); 1184 return SMFIS_CONTINUE; 1185} 1186 1187/* 1188** ST_HELO -- helo/ehlo command 1189** 1190** Parameters: 1191** g -- generic argument structure 1192** 1193** Returns: 1194** continue or filter-specified value 1195*/ 1196 1197static int 1198st_helo(g) 1199 genarg *g; 1200{ 1201 sfsistat (*fi_helo) __P((SMFICTX *, char *)); 1202 1203 if (g == NULL) 1204 return _SMFIS_ABORT; 1205 mi_clr_macros(g->a_ctx, g->a_idx + 1); 1206 if (g->a_ctx->ctx_smfi != NULL && 1207 (fi_helo = g->a_ctx->ctx_smfi->xxfi_helo) != NULL) 1208 { 1209 /* paranoia: check for terminating '\0' */ 1210 if (g->a_len == 0 || g->a_buf[g->a_len - 1] != '\0') 1211 return MI_FAILURE; 1212 return (*fi_helo)(g->a_ctx, g->a_buf); 1213 } 1214 return SMFIS_CONTINUE; 1215} 1216 1217/* 1218** ST_HEADER -- header line 1219** 1220** Parameters: 1221** g -- generic argument structure 1222** 1223** Returns: 1224** continue or filter-specified value 1225*/ 1226 1227static int 1228st_header(g) 1229 genarg *g; 1230{ 1231 char *hf, *hv; 1232 sfsistat (*fi_header) __P((SMFICTX *, char *, char *)); 1233 1234 if (g == NULL) 1235 return _SMFIS_ABORT; 1236 if (g->a_ctx->ctx_smfi == NULL || 1237 (fi_header = g->a_ctx->ctx_smfi->xxfi_header) == NULL) 1238 return SMFIS_CONTINUE; 1239 if (dec_arg2(g->a_buf, g->a_len, &hf, &hv) == MI_SUCCESS) 1240 return (*fi_header)(g->a_ctx, hf, hv); 1241 else 1242 return _SMFIS_ABORT; 1243} 1244 1245#define ARGV_FCT(lf, rf, idx) \ 1246 char **argv; \ 1247 sfsistat (*lf) __P((SMFICTX *, char **)); \ 1248 int r; \ 1249 \ 1250 if (g == NULL) \ 1251 return _SMFIS_ABORT; \ 1252 mi_clr_macros(g->a_ctx, g->a_idx + 1); \ 1253 if (g->a_ctx->ctx_smfi == NULL || \ 1254 (lf = g->a_ctx->ctx_smfi->rf) == NULL) \ 1255 return SMFIS_CONTINUE; \ 1256 if ((argv = dec_argv(g->a_buf, g->a_len)) == NULL) \ 1257 return _SMFIS_ABORT; \ 1258 r = (*lf)(g->a_ctx, argv); \ 1259 free(argv); \ 1260 return r; 1261 1262/* 1263** ST_SENDER -- MAIL FROM command 1264** 1265** Parameters: 1266** g -- generic argument structure 1267** 1268** Returns: 1269** continue or filter-specified value 1270*/ 1271 1272static int 1273st_sender(g) 1274 genarg *g; 1275{ 1276 ARGV_FCT(fi_envfrom, xxfi_envfrom, CI_MAIL) 1277} 1278 1279/* 1280** ST_RCPT -- RCPT TO command 1281** 1282** Parameters: 1283** g -- generic argument structure 1284** 1285** Returns: 1286** continue or filter-specified value 1287*/ 1288 1289static int 1290st_rcpt(g) 1291 genarg *g; 1292{ 1293 ARGV_FCT(fi_envrcpt, xxfi_envrcpt, CI_RCPT) 1294} 1295 1296/* 1297** ST_UNKNOWN -- unrecognized or unimplemented command 1298** 1299** Parameters: 1300** g -- generic argument structure 1301** 1302** Returns: 1303** continue or filter-specified value 1304*/ 1305 1306static int 1307st_unknown(g) 1308 genarg *g; 1309{ 1310 sfsistat (*fi_unknown) __P((SMFICTX *, const char *)); 1311 1312 if (g == NULL) 1313 return _SMFIS_ABORT; 1314 if (g->a_ctx->ctx_smfi != NULL && 1315 g->a_ctx->ctx_smfi->xxfi_version > 2 && 1316 (fi_unknown = g->a_ctx->ctx_smfi->xxfi_unknown) != NULL) 1317 return (*fi_unknown)(g->a_ctx, (const char *) g->a_buf); 1318 return SMFIS_CONTINUE; 1319} 1320 1321/* 1322** ST_MACROS -- deal with macros received from the MTA 1323** 1324** Parameters: 1325** g -- generic argument structure 1326** 1327** Returns: 1328** continue/keep 1329** 1330** Side effects: 1331** set pointer in macro array to current values. 1332*/ 1333 1334static int 1335st_macros(g) 1336 genarg *g; 1337{ 1338 int i; 1339 char **argv; 1340 1341 if (g == NULL || g->a_len < 1) 1342 return _SMFIS_FAIL; 1343 if ((argv = dec_argv(g->a_buf + 1, g->a_len - 1)) == NULL) 1344 return _SMFIS_FAIL; 1345 switch (g->a_buf[0]) 1346 { 1347 case SMFIC_CONNECT: 1348 i = CI_CONN; 1349 break; 1350 case SMFIC_HELO: 1351 i = CI_HELO; 1352 break; 1353 case SMFIC_MAIL: 1354 i = CI_MAIL; 1355 break; 1356 case SMFIC_RCPT: 1357 i = CI_RCPT; 1358 break; 1359 case SMFIC_DATA: 1360 i = CI_DATA; 1361 break; 1362 case SMFIC_BODYEOB: 1363 i = CI_EOM; 1364 break; 1365 case SMFIC_EOH: 1366 i = CI_EOH; 1367 break; 1368 default: 1369 free(argv); 1370 return _SMFIS_FAIL; 1371 } 1372 if (g->a_ctx->ctx_mac_ptr[i] != NULL) 1373 free(g->a_ctx->ctx_mac_ptr[i]); 1374 if (g->a_ctx->ctx_mac_buf[i] != NULL) 1375 free(g->a_ctx->ctx_mac_buf[i]); 1376 g->a_ctx->ctx_mac_ptr[i] = argv; 1377 g->a_ctx->ctx_mac_buf[i] = g->a_buf; 1378 return _SMFIS_KEEP; 1379} 1380 1381/* 1382** ST_QUIT -- quit command 1383** 1384** Parameters: 1385** g -- generic argument structure 1386** 1387** Returns: 1388** noreply 1389*/ 1390 1391/* ARGSUSED */ 1392static int 1393st_quit(g) 1394 genarg *g; 1395{ 1396 sfsistat (*fi_close) __P((SMFICTX *)); 1397 1398 if (g == NULL) 1399 return _SMFIS_ABORT; 1400 if (g->a_ctx->ctx_smfi != NULL && 1401 (fi_close = g->a_ctx->ctx_smfi->xxfi_close) != NULL) 1402 (void) (*fi_close)(g->a_ctx); 1403 mi_clr_macros(g->a_ctx, 0); 1404 return _SMFIS_NOREPLY; 1405} 1406 1407/* 1408** ST_BODYCHUNK -- deal with a piece of the mail body 1409** 1410** Parameters: 1411** g -- generic argument structure 1412** 1413** Returns: 1414** continue or filter-specified value 1415*/ 1416 1417static int 1418st_bodychunk(g) 1419 genarg *g; 1420{ 1421 sfsistat (*fi_body) __P((SMFICTX *, unsigned char *, size_t)); 1422 1423 if (g == NULL) 1424 return _SMFIS_ABORT; 1425 if (g->a_ctx->ctx_smfi != NULL && 1426 (fi_body = g->a_ctx->ctx_smfi->xxfi_body) != NULL) 1427 return (*fi_body)(g->a_ctx, (unsigned char *)g->a_buf, 1428 g->a_len); 1429 return SMFIS_CONTINUE; 1430} 1431 1432/* 1433** ST_BODYEND -- deal with the last piece of the mail body 1434** 1435** Parameters: 1436** g -- generic argument structure 1437** 1438** Returns: 1439** continue or filter-specified value 1440** 1441** Side effects: 1442** sends a reply for the body part (if non-empty). 1443*/ 1444 1445static int 1446st_bodyend(g) 1447 genarg *g; 1448{ 1449 sfsistat r; 1450 sfsistat (*fi_body) __P((SMFICTX *, unsigned char *, size_t)); 1451 sfsistat (*fi_eom) __P((SMFICTX *)); 1452 1453 if (g == NULL) 1454 return _SMFIS_ABORT; 1455 r = SMFIS_CONTINUE; 1456 if (g->a_ctx->ctx_smfi != NULL) 1457 { 1458 if ((fi_body = g->a_ctx->ctx_smfi->xxfi_body) != NULL && 1459 g->a_len > 0) 1460 { 1461 socket_t sd; 1462 struct timeval timeout; 1463 1464 timeout.tv_sec = g->a_ctx->ctx_timeout; 1465 timeout.tv_usec = 0; 1466 sd = g->a_ctx->ctx_sd; 1467 r = (*fi_body)(g->a_ctx, (unsigned char *)g->a_buf, 1468 g->a_len); 1469 if (r != SMFIS_CONTINUE && 1470 sendreply(r, sd, &timeout, g->a_ctx) != MI_SUCCESS) 1471 return _SMFIS_ABORT; 1472 } 1473 } 1474 if (r == SMFIS_CONTINUE && 1475 (fi_eom = g->a_ctx->ctx_smfi->xxfi_eom) != NULL) 1476 return (*fi_eom)(g->a_ctx); 1477 return r; 1478} 1479 1480/* 1481** ST_ABORTFCT -- deal with aborts 1482** 1483** Parameters: 1484** g -- generic argument structure 1485** 1486** Returns: 1487** abort or filter-specified value 1488*/ 1489 1490static int 1491st_abortfct(g) 1492 genarg *g; 1493{ 1494 sfsistat (*fi_abort) __P((SMFICTX *)); 1495 1496 if (g == NULL) 1497 return _SMFIS_ABORT; 1498 if (g != NULL && g->a_ctx->ctx_smfi != NULL && 1499 (fi_abort = g->a_ctx->ctx_smfi->xxfi_abort) != NULL) 1500 (void) (*fi_abort)(g->a_ctx); 1501 return _SMFIS_NOREPLY; 1502} 1503 1504/* 1505** TRANS_OK -- is the state transition ok? 1506** 1507** Parameters: 1508** old -- old state 1509** new -- new state 1510** 1511** Returns: 1512** state transition ok 1513*/ 1514 1515static bool 1516trans_ok(old, new) 1517 int old, new; 1518{ 1519 int s, n; 1520 1521 s = old; 1522 if (s >= SIZE_NEXT_STATES) 1523 return false; 1524 do 1525 { 1526 /* is this state transition allowed? */ 1527 if ((MI_MASK(new) & next_states[s]) != 0) 1528 return true; 1529 1530 /* 1531 ** no: try next state; 1532 ** this works since the relevant states are ordered 1533 ** strict sequentially 1534 */ 1535 1536 n = s + 1; 1537 if (n >= SIZE_NEXT_STATES) 1538 return false; 1539 1540 /* 1541 ** can we actually "skip" this state? 1542 ** see fix_stm() which sets this bit for those 1543 ** states which the filter program is not interested in 1544 */ 1545 1546 if (bitset(NX_SKIP, next_states[n])) 1547 s = n; 1548 else 1549 return false; 1550 } while (s < SIZE_NEXT_STATES); 1551 return false; 1552} 1553 1554/* 1555** FIX_STM -- add "skip" bits to the state transition table 1556** 1557** Parameters: 1558** ctx -- context structure 1559** 1560** Returns: 1561** None. 1562** 1563** Side effects: 1564** may change state transition table. 1565*/ 1566 1567static void 1568fix_stm(ctx) 1569 SMFICTX_PTR ctx; 1570{ 1571 unsigned long fl; 1572 1573 if (ctx == NULL || ctx->ctx_smfi == NULL) 1574 return; 1575 fl = ctx->ctx_pflags; 1576 if (bitset(SMFIP_NOCONNECT, fl)) 1577 next_states[ST_CONN] |= NX_SKIP; 1578 if (bitset(SMFIP_NOHELO, fl)) 1579 next_states[ST_HELO] |= NX_SKIP; 1580 if (bitset(SMFIP_NOMAIL, fl)) 1581 next_states[ST_MAIL] |= NX_SKIP; 1582 if (bitset(SMFIP_NORCPT, fl)) 1583 next_states[ST_RCPT] |= NX_SKIP; 1584 if (bitset(SMFIP_NOHDRS, fl)) 1585 next_states[ST_HDRS] |= NX_SKIP; 1586 if (bitset(SMFIP_NOEOH, fl)) 1587 next_states[ST_EOHS] |= NX_SKIP; 1588 if (bitset(SMFIP_NOBODY, fl)) 1589 next_states[ST_BODY] |= NX_SKIP; 1590 if (bitset(SMFIP_NODATA, fl)) 1591 next_states[ST_DATA] |= NX_SKIP; 1592 if (bitset(SMFIP_NOUNKNOWN, fl)) 1593 next_states[ST_UNKN] |= NX_SKIP; 1594} 1595 1596/* 1597** DEC_ARGV -- split a buffer into a list of strings, NULL terminated 1598** 1599** Parameters: 1600** buf -- buffer with several strings 1601** len -- length of buffer 1602** 1603** Returns: 1604** array of pointers to the individual strings 1605*/ 1606 1607static char ** 1608dec_argv(buf, len) 1609 char *buf; 1610 size_t len; 1611{ 1612 char **s; 1613 size_t i; 1614 int elem, nelem; 1615 1616 nelem = 0; 1617 for (i = 0; i < len; i++) 1618 { 1619 if (buf[i] == '\0') 1620 ++nelem; 1621 } 1622 if (nelem == 0) 1623 return NULL; 1624 1625 /* last entry is only for the name */ 1626 s = (char **)malloc((nelem + 1) * (sizeof *s)); 1627 if (s == NULL) 1628 return NULL; 1629 s[0] = buf; 1630 for (i = 0, elem = 0; i < len && elem < nelem; i++) 1631 { 1632 if (buf[i] == '\0') 1633 { 1634 ++elem; 1635 if (i + 1 >= len) 1636 s[elem] = NULL; 1637 else 1638 s[elem] = &(buf[i + 1]); 1639 } 1640 } 1641 1642 /* overwrite last entry (already done above, just paranoia) */ 1643 s[elem] = NULL; 1644 return s; 1645} 1646 1647/* 1648** DEC_ARG2 -- split a buffer into two strings 1649** 1650** Parameters: 1651** buf -- buffer with two strings 1652** len -- length of buffer 1653** s1,s2 -- pointer to result strings 1654** 1655** Returns: 1656** MI_FAILURE/MI_SUCCESS 1657*/ 1658 1659static int 1660dec_arg2(buf, len, s1, s2) 1661 char *buf; 1662 size_t len; 1663 char **s1; 1664 char **s2; 1665{ 1666 size_t i; 1667 1668 /* paranoia: check for terminating '\0' */ 1669 if (len == 0 || buf[len - 1] != '\0') 1670 return MI_FAILURE; 1671 *s1 = buf; 1672 for (i = 1; i < len && buf[i] != '\0'; i++) 1673 continue; 1674 if (i >= len - 1) 1675 return MI_FAILURE; 1676 *s2 = buf + i + 1; 1677 return MI_SUCCESS; 1678} 1679 1680/* 1681** SENDOK -- is it ok for the filter to send stuff to the MTA? 1682** 1683** Parameters: 1684** ctx -- context structure 1685** flag -- flag to check 1686** 1687** Returns: 1688** sending allowed (in current state) 1689*/ 1690 1691bool 1692mi_sendok(ctx, flag) 1693 SMFICTX_PTR ctx; 1694 int flag; 1695{ 1696 if (ctx == NULL || ctx->ctx_smfi == NULL) 1697 return false; 1698 1699 /* did the milter request this operation? */ 1700 if (flag != 0 && !bitset(flag, ctx->ctx_aflags)) 1701 return false; 1702 1703 /* are we in the correct state? It must be "End of Message". */ 1704 return ctx->ctx_state == ST_ENDM; 1705} 1706 1707#if _FFR_WORKERS_POOL 1708/* 1709** MI_RD_SOCKET_READY - checks if the socket is ready for read(2) 1710** 1711** Parameters: 1712** sd -- socket_t 1713** 1714** Returns: 1715** true iff socket is ready for read(2) 1716*/ 1717 1718#define MI_RD_CMD_TO 1 1719#define MI_RD_MAX_ERR 16 1720 1721static bool 1722mi_rd_socket_ready (sd) 1723 socket_t sd; 1724{ 1725 int n; 1726 int nerr = 0; 1727#if SM_CONF_POLL 1728 struct pollfd pfd; 1729#else /* SM_CONF_POLL */ 1730 fd_set rd_set, exc_set; 1731#endif /* SM_CONF_POLL */ 1732 1733 do 1734 { 1735#if SM_CONF_POLL 1736 pfd.fd = sd; 1737 pfd.events = POLLIN; 1738 pfd.revents = 0; 1739 1740 n = poll(&pfd, 1, MI_RD_CMD_TO); 1741#else /* SM_CONF_POLL */ 1742 struct timeval timeout; 1743 1744 FD_ZERO(&rd_set); 1745 FD_ZERO(&exc_set); 1746 FD_SET(sd, &rd_set); 1747 FD_SET(sd, &exc_set); 1748 1749 timeout.tv_sec = MI_RD_CMD_TO / 1000; 1750 timeout.tv_usec = 0; 1751 n = select(sd + 1, &rd_set, NULL, &exc_set, &timeout); 1752#endif /* SM_CONF_POLL */ 1753 1754 if (n < 0) 1755 { 1756 if (errno == EINTR) 1757 { 1758 nerr++; 1759 continue; 1760 } 1761 return true; 1762 } 1763 1764 if (n == 0) 1765 return false; 1766 break; 1767 } while (nerr < MI_RD_MAX_ERR); 1768 if (nerr >= MI_RD_MAX_ERR) 1769 return false; 1770 1771#if SM_CONF_POLL 1772 return (pfd.revents != 0); 1773#else /* SM_CONF_POLL */ 1774 return FD_ISSET(sd, &rd_set) || FD_ISSET(sd, &exc_set); 1775#endif /* SM_CONF_POLL */ 1776} 1777#endif /* _FFR_WORKERS_POOL */ 1778