2 * Copyright (c) 1999-2004, 2006-2008 Proofpoint, Inc. and its suppliers.
5 * By using this file, you agree to the terms and conditions set
6 * forth in the LICENSE file which can be found at the top level of
7 * the sendmail distribution.
12 SM_RCSID("@(#)$Id: engine.c,v 8.168 2013-11-22 20:51:36 ca Exp $")
14 #include "libmilter.h"
16 #if NETINET || NETINET6
17 # include <arpa/inet.h>
20 /* generic argument for functions in the command table */
23 size_t a_len; /* length of buffer */
24 char *a_buf; /* argument string */
25 int a_idx; /* index for macro array */
26 SMFICTX_PTR a_ctx; /* context */
29 typedef struct arg_struct genarg;
31 /* structure for commands received from MTA */
34 char cm_cmd; /* command */
35 int cm_argt; /* type of arguments expected */
36 int cm_next; /* next state */
37 int cm_todo; /* what to do next */
38 int cm_macros; /* index for macros */
39 int (*cm_fct) __P((genarg *)); /* function to execute */
42 typedef struct cmdfct_t cmdfct;
44 /* possible values for cm_argt */
48 /* possible values for cm_todo */
49 #define CT_CONT 0x0000 /* continue reading commands */
50 #define CT_IGNO 0x0001 /* continue even when error */
52 /* not needed right now, done via return code instead */
53 #define CT_KEEP 0x0004 /* keep buffer (contains symbols) */
54 #define CT_END 0x0008 /* last command of session, stop replying */
56 /* index in macro array: macros only for these commands */
65 #define CI_LAST CI_EOH
67 ERROR: do not compile with CI_LAST < CI_DATA
70 ERROR: do not compile with CI_LAST < CI_EOM
73 ERROR: do not compile with CI_LAST < CI_EOH
76 ERROR: do not compile with CI_LAST < CI_RCPT
79 ERROR: do not compile with CI_LAST < CI_MAIL
82 ERROR: do not compile with CI_LAST < CI_HELO
85 ERROR: do not compile with CI_LAST < CI_CONN
87 #if CI_LAST >= MAX_MACROS_ENTRIES
88 ERROR: do not compile with CI_LAST >= MAX_MACROS_ENTRIES
91 /* function prototypes */
92 static int st_abortfct __P((genarg *));
93 static int st_macros __P((genarg *));
94 static int st_optionneg __P((genarg *));
95 static int st_bodychunk __P((genarg *));
96 static int st_connectinfo __P((genarg *));
97 static int st_bodyend __P((genarg *));
98 static int st_helo __P((genarg *));
99 static int st_header __P((genarg *));
100 static int st_sender __P((genarg *));
101 static int st_rcpt __P((genarg *));
102 static int st_unknown __P((genarg *));
103 static int st_data __P((genarg *));
104 static int st_eoh __P((genarg *));
105 static int st_quit __P((genarg *));
106 static int sendreply __P((sfsistat, socket_t, struct timeval *, SMFICTX_PTR));
107 static void fix_stm __P((SMFICTX_PTR));
108 static bool trans_ok __P((int, int));
109 static char **dec_argv __P((char *, size_t));
110 static int dec_arg2 __P((char *, size_t, char **, char **));
111 static void mi_clr_symlist __P((SMFICTX_PTR));
113 #if _FFR_WORKERS_POOL
114 static bool mi_rd_socket_ready __P((int));
119 #define ST_INIT 0 /* initial state */
120 #define ST_OPTS 1 /* option negotiation */
121 #define ST_CONN 2 /* connection info */
122 #define ST_HELO 3 /* helo */
123 #define ST_MAIL 4 /* mail from */
124 #define ST_RCPT 5 /* rcpt to */
125 #define ST_DATA 6 /* data */
126 #define ST_HDRS 7 /* headers */
127 #define ST_EOHS 8 /* end of headers */
128 #define ST_BODY 9 /* body */
129 #define ST_ENDM 10 /* end of message */
130 #define ST_QUIT 11 /* quit */
131 #define ST_ABRT 12 /* abort */
132 #define ST_UNKN 13 /* unknown SMTP command */
133 #define ST_Q_NC 14 /* quit, new connection follows */
134 #define ST_LAST ST_Q_NC /* last valid state */
135 #define ST_SKIP 16 /* not a state but required for the state table */
137 /* in a mail transaction? must be before eom according to spec. */
138 #define ST_IN_MAIL(st) ((st) >= ST_MAIL && (st) < ST_ENDM)
141 ** set of next states
142 ** each state (ST_*) corresponds to bit in an int value (1 << state)
143 ** each state has a set of allowed transitions ('or' of bits of states)
144 ** so a state transition is valid if the mask of the next state
145 ** is set in the NX_* value
146 ** this function is coded in trans_ok(), see below.
149 #define MI_MASK(x) (0x0001 << (x)) /* generate a bit "mask" for a state */
150 #define NX_INIT (MI_MASK(ST_OPTS))
151 #define NX_OPTS (MI_MASK(ST_CONN) | MI_MASK(ST_UNKN))
152 #define NX_CONN (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN))
153 #define NX_HELO (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN))
154 #define NX_MAIL (MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | MI_MASK(ST_UNKN))
155 #define NX_RCPT (MI_MASK(ST_HDRS) | MI_MASK(ST_EOHS) | MI_MASK(ST_DATA) | \
156 MI_MASK(ST_BODY) | MI_MASK(ST_ENDM) | \
157 MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | MI_MASK(ST_UNKN))
158 #define NX_DATA (MI_MASK(ST_EOHS) | MI_MASK(ST_HDRS) | MI_MASK(ST_ABRT))
159 #define NX_HDRS (MI_MASK(ST_EOHS) | MI_MASK(ST_HDRS) | MI_MASK(ST_ABRT))
160 #define NX_EOHS (MI_MASK(ST_BODY) | MI_MASK(ST_ENDM) | MI_MASK(ST_ABRT))
161 #define NX_BODY (MI_MASK(ST_ENDM) | MI_MASK(ST_BODY) | MI_MASK(ST_ABRT))
162 #define NX_ENDM (MI_MASK(ST_QUIT) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN) | \
166 #define NX_UNKN (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | \
167 MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | \
169 MI_MASK(ST_BODY) | MI_MASK(ST_UNKN) | \
170 MI_MASK(ST_ABRT) | MI_MASK(ST_QUIT) | MI_MASK(ST_Q_NC))
171 #define NX_Q_NC (MI_MASK(ST_CONN) | MI_MASK(ST_UNKN))
172 #define NX_SKIP MI_MASK(ST_SKIP)
174 static int next_states[] =
193 #define SIZE_NEXT_STATES (sizeof(next_states) / sizeof(next_states[0]))
195 /* commands received by milter */
196 static cmdfct cmds[] =
198 {SMFIC_ABORT, CM_NULLOK, ST_ABRT, CT_CONT, CI_NONE, st_abortfct}
199 , {SMFIC_MACRO, CM_BUF, ST_NONE, CT_KEEP, CI_NONE, st_macros }
200 , {SMFIC_BODY, CM_BUF, ST_BODY, CT_CONT, CI_NONE, st_bodychunk}
201 , {SMFIC_CONNECT, CM_BUF, ST_CONN, CT_CONT, CI_CONN, st_connectinfo}
202 , {SMFIC_BODYEOB, CM_NULLOK, ST_ENDM, CT_CONT, CI_EOM, st_bodyend }
203 , {SMFIC_HELO, CM_BUF, ST_HELO, CT_CONT, CI_HELO, st_helo }
204 , {SMFIC_HEADER, CM_BUF, ST_HDRS, CT_CONT, CI_NONE, st_header }
205 , {SMFIC_MAIL, CM_BUF, ST_MAIL, CT_CONT, CI_MAIL, st_sender }
206 , {SMFIC_OPTNEG, CM_BUF, ST_OPTS, CT_CONT, CI_NONE, st_optionneg}
207 , {SMFIC_EOH, CM_NULLOK, ST_EOHS, CT_CONT, CI_EOH, st_eoh }
208 , {SMFIC_QUIT, CM_NULLOK, ST_QUIT, CT_END, CI_NONE, st_quit }
209 , {SMFIC_DATA, CM_NULLOK, ST_DATA, CT_CONT, CI_DATA, st_data }
210 , {SMFIC_RCPT, CM_BUF, ST_RCPT, CT_IGNO, CI_RCPT, st_rcpt }
211 , {SMFIC_UNKNOWN, CM_BUF, ST_UNKN, CT_IGNO, CI_NONE, st_unknown }
212 , {SMFIC_QUIT_NC, CM_NULLOK, ST_Q_NC, CT_CONT, CI_NONE, st_quit }
216 ** Additional (internal) reply codes;
217 ** must be coordinated wit libmilter/mfapi.h
220 #define _SMFIS_KEEP 20
221 #define _SMFIS_ABORT 21
222 #define _SMFIS_OPTIONS 22
223 #define _SMFIS_NOREPLY SMFIS_NOREPLY
224 #define _SMFIS_FAIL (-1)
225 #define _SMFIS_NONE (-2)
228 ** MI_ENGINE -- receive commands and process them
231 ** ctx -- context structure
234 ** MI_FAILURE/MI_SUCCESS
244 int ret = MI_SUCCESS;
245 int ncmds = sizeof(cmds) / sizeof(cmdfct);
246 int curstate = ST_INIT;
253 struct timeval timeout;
254 int (*f) __P((genarg *));
255 sfsistat (*fi_abort) __P((SMFICTX *));
256 sfsistat (*fi_close) __P((SMFICTX *));
260 fi_abort = ctx->ctx_smfi->xxfi_abort;
261 #if _FFR_WORKERS_POOL
262 curstate = ctx->ctx_state;
263 if (curstate == ST_INIT)
265 mi_clr_macros(ctx, 0);
268 #else /* _FFR_WORKERS_POOL */
269 mi_clr_macros(ctx, 0);
271 #endif /* _FFR_WORKERS_POOL */
275 /* call abort only if in a mail transaction */
276 call_abort = ST_IN_MAIL(curstate);
277 timeout.tv_sec = ctx->ctx_timeout;
279 if (mi_stop() == MILTER_ABRT)
281 if (ctx->ctx_dbg > 3)
282 sm_dprintf("[%lu] milter_abort\n",
289 ** Notice: buf is allocated by mi_rd_cmd() and it will
290 ** usually be free()d after it has been used in f().
291 ** However, if the function returns _SMFIS_KEEP then buf
292 ** contains macros and will not be free()d.
293 ** Hence r must be set to _SMFIS_NONE if a new buf is
294 ** allocated to avoid problem with housekeeping, esp.
295 ** if the code "break"s out of the loop.
298 #if _FFR_WORKERS_POOL
299 /* Is the socket ready to be read ??? */
300 if (!mi_rd_socket_ready(sd))
305 #endif /* _FFR_WORKERS_POOL */
308 if ((buf = mi_rd_cmd(sd, &timeout, &cmd, &len,
309 ctx->ctx_smfi->xxfi_name)) == NULL &&
310 cmd < SMFIC_VALIDCMD)
312 if (ctx->ctx_dbg > 5)
313 sm_dprintf("[%lu] mi_engine: mi_rd_cmd error (%x)\n",
314 (long) ctx->ctx_id, (int) cmd);
317 ** eof is currently treated as failure ->
318 ** abort() instead of close(), otherwise use:
319 ** if (cmd != SMFIC_EOF)
325 if (ctx->ctx_dbg > 4)
326 sm_dprintf("[%lu] got cmd '%c' len %d\n",
327 (long) ctx->ctx_id, cmd, (int) len);
328 for (i = 0; i < ncmds; i++)
330 if (cmd == cmds[i].cm_cmd)
335 /* unknown command */
336 if (ctx->ctx_dbg > 1)
337 sm_dprintf("[%lu] cmd '%c' unknown\n",
338 (long) ctx->ctx_id, cmd);
342 if ((f = cmds[i].cm_fct) == NULL)
345 if (ctx->ctx_dbg > 1)
346 sm_dprintf("[%lu] cmd '%c' not impl\n",
347 (long) ctx->ctx_id, cmd);
352 /* is new state ok? */
353 newstate = cmds[i].cm_next;
354 if (ctx->ctx_dbg > 5)
355 sm_dprintf("[%lu] cur %x new %x nextmask %x\n",
357 curstate, newstate, next_states[curstate]);
359 if (newstate != ST_NONE && !trans_ok(curstate, newstate))
361 if (ctx->ctx_dbg > 1)
362 sm_dprintf("[%lu] abort: cur %d (%x) new %d (%x) next %x\n",
364 curstate, MI_MASK(curstate),
365 newstate, MI_MASK(newstate),
366 next_states[curstate]);
368 /* call abort only if in a mail transaction */
369 if (fi_abort != NULL && call_abort)
370 (void) (*fi_abort)(ctx);
373 ** try to reach the new state from HELO
374 ** if it can't be reached, ignore the command.
378 if (!trans_ok(curstate, newstate))
388 if (cmds[i].cm_argt != CM_NULLOK && buf == NULL)
391 if (ctx->ctx_dbg > 1)
392 sm_dprintf("[%lu] cmd='%c', buf=NULL\n",
393 (long) ctx->ctx_id, cmd);
399 if (newstate != ST_NONE)
402 ctx->ctx_state = curstate;
404 arg.a_idx = cmds[i].cm_macros;
405 call_abort = ST_IN_MAIL(curstate);
407 /* call function to deal with command */
408 MI_MONITOR_BEGIN(ctx, cmd);
410 MI_MONITOR_END(ctx, cmd);
411 if (r != _SMFIS_KEEP && buf != NULL)
416 if (sendreply(r, sd, &timeout, ctx) != MI_SUCCESS)
422 if (r == SMFIS_ACCEPT)
424 /* accept mail, no further actions taken */
427 else if (r == SMFIS_REJECT || r == SMFIS_DISCARD ||
431 ** further actions depend on current state
432 ** if the IGNO bit is set: "ignore" the error,
433 ** i.e., stay in the current state
435 if (!bitset(CT_IGNO, cmds[i].cm_todo))
438 else if (r == _SMFIS_ABORT)
440 if (ctx->ctx_dbg > 5)
441 sm_dprintf("[%lu] function returned abort\n",
446 } while (!bitset(CT_END, cmds[i].cm_todo));
448 ctx->ctx_state = curstate;
450 if (ret == MI_FAILURE)
452 /* call abort only if in a mail transaction */
453 if (fi_abort != NULL && call_abort)
454 (void) (*fi_abort)(ctx);
457 /* has close been called? */
458 if (ctx->ctx_state != ST_QUIT
459 #if _FFR_WORKERS_POOL
460 && ret != MI_CONTINUE
464 if ((fi_close = ctx->ctx_smfi->xxfi_close) != NULL)
465 (void) (*fi_close)(ctx);
467 if (r != _SMFIS_KEEP && buf != NULL)
469 #if !_FFR_WORKERS_POOL
470 mi_clr_macros(ctx, 0);
475 static size_t milter_addsymlist __P((SMFICTX_PTR, char *, char **));
478 milter_addsymlist(ctx, buf, newbuf)
488 SM_ASSERT(ctx != NULL);
489 SM_ASSERT(buf != NULL);
490 SM_ASSERT(newbuf != NULL);
492 for (i = 0; i < MAX_MACROS_ENTRIES; i++)
494 if (ctx->ctx_mac_list[i] != NULL)
496 len += strlen(ctx->ctx_mac_list[i]) + 1 +
504 SM_ASSERT(len + MILTER_OPTLEN > len);
505 len += MILTER_OPTLEN;
506 buffer = malloc(len);
509 (void) memcpy(buffer, buf, MILTER_OPTLEN);
510 offset = MILTER_OPTLEN;
511 for (i = 0; i < MAX_MACROS_ENTRIES; i++)
515 if (ctx->ctx_mac_list[i] == NULL)
518 SM_ASSERT(offset + MILTER_LEN_BYTES < len);
520 (void) memcpy(buffer + offset, (void *) &v,
522 offset += MILTER_LEN_BYTES;
523 l = strlen(ctx->ctx_mac_list[i]) + 1;
524 SM_ASSERT(offset + l <= len);
525 (void) memcpy(buffer + offset,
526 ctx->ctx_mac_list[i], l);
545 ** GET_NR_BIT -- get "no reply" bit matching state
548 ** state -- current protocol stage
551 ** 0: no matching bit
552 ** >0: the matching "no reply" bit
555 static unsigned long get_nr_bit __P((int));
600 ** SENDREPLY -- send a reply to the MTA
604 ** sd -- socket descriptor
605 ** timeout_ptr -- (ptr to) timeout to use for sending
606 ** ctx -- context structure
609 ** MI_SUCCESS/MI_FAILURE
613 sendreply(r, sd, timeout_ptr, ctx)
616 struct timeval *timeout_ptr;
624 bit = get_nr_bit(ctx->ctx_state);
625 if (bit != 0 && (ctx->ctx_pflags & bit) != 0 && r != SMFIS_NOREPLY)
627 if (r >= SMFIS_CONTINUE && r < _SMFIS_KEEP)
629 /* milter said it wouldn't reply, but it lied... */
631 "%s: milter claimed not to reply in state %d but did anyway %d",
632 ctx->ctx_smfi->xxfi_name,
638 ** Force specified behavior, otherwise libmilter
639 ** and MTA will fail to communicate properly.
659 ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_CONTINUE, NULL, 0);
663 if (ctx->ctx_reply != NULL &&
664 ((r == SMFIS_TEMPFAIL && *ctx->ctx_reply == '4') ||
665 (r == SMFIS_REJECT && *ctx->ctx_reply == '5')))
667 ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_REPLYCODE,
669 strlen(ctx->ctx_reply) + 1);
670 free(ctx->ctx_reply);
671 ctx->ctx_reply = NULL;
675 ret = mi_wr_cmd(sd, timeout_ptr, r == SMFIS_REJECT ?
676 SMFIR_REJECT : SMFIR_TEMPFAIL, NULL, 0);
680 ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_DISCARD, NULL, 0);
683 ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_ACCEPT, NULL, 0);
686 ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_SKIP, NULL, 0);
693 char buf[MILTER_OPTLEN];
695 v = htonl(ctx->ctx_prot_vers2mta);
696 (void) memcpy(&(buf[0]), (void *) &v,
698 v = htonl(ctx->ctx_aflags);
699 (void) memcpy(&(buf[MILTER_LEN_BYTES]), (void *) &v,
701 v = htonl(ctx->ctx_pflags2mta);
702 (void) memcpy(&(buf[MILTER_LEN_BYTES * 2]),
703 (void *) &v, MILTER_LEN_BYTES);
704 len = milter_addsymlist(ctx, buf, &buffer);
707 ret = mi_wr_cmd(sd, timeout_ptr, SMFIC_OPTNEG,
718 (ctx->ctx_pflags & bit) != 0 &&
719 (ctx->ctx_mta_pflags & bit) == 0)
722 ** milter doesn't want to send a reply,
723 ** but the MTA doesn't have that feature: fake it.
726 ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_CONTINUE, NULL,
730 default: /* don't send a reply */
737 ** MI_CLR_MACROS -- clear set of macros starting from a given index
740 ** ctx -- context structure
741 ** m -- index from which to clear all macros
748 mi_clr_macros(ctx, m)
754 for (i = m; i < MAX_MACROS_ENTRIES; i++)
756 if (ctx->ctx_mac_ptr[i] != NULL)
758 free(ctx->ctx_mac_ptr[i]);
759 ctx->ctx_mac_ptr[i] = NULL;
761 if (ctx->ctx_mac_buf[i] != NULL)
763 free(ctx->ctx_mac_buf[i]);
764 ctx->ctx_mac_buf[i] = NULL;
770 ** MI_CLR_SYMLIST -- clear list of macros
773 ** ctx -- context structure
785 SM_ASSERT(ctx != NULL);
786 for (i = SMFIM_FIRST; i <= SMFIM_LAST; i++)
788 if (ctx->ctx_mac_list[i] != NULL)
790 free(ctx->ctx_mac_list[i]);
791 ctx->ctx_mac_list[i] = NULL;
797 ** MI_CLR_CTX -- clear context
800 ** ctx -- context structure
810 SM_ASSERT(ctx != NULL);
811 if (ValidSocket(ctx->ctx_sd))
813 (void) closesocket(ctx->ctx_sd);
814 ctx->ctx_sd = INVALID_SOCKET;
816 if (ctx->ctx_reply != NULL)
818 free(ctx->ctx_reply);
819 ctx->ctx_reply = NULL;
821 if (ctx->ctx_privdata != NULL)
823 smi_log(SMI_LOG_WARN,
824 "%s: private data not NULL",
825 ctx->ctx_smfi->xxfi_name);
827 mi_clr_macros(ctx, 0);
833 ** ST_OPTIONNEG -- negotiate options
836 ** g -- generic argument structure
839 ** abort/send options/continue
846 mi_int32 i, v, fake_pflags, internal_pflags;
848 #if _FFR_MILTER_CHECK
849 bool testmode = false;
851 int (*fi_negotiate) __P((SMFICTX *,
852 unsigned long, unsigned long,
853 unsigned long, unsigned long,
854 unsigned long *, unsigned long *,
855 unsigned long *, unsigned long *));
857 if (g == NULL || g->a_ctx->ctx_smfi == NULL)
858 return SMFIS_CONTINUE;
860 mi_clr_macros(ctx, g->a_idx + 1);
861 ctx->ctx_prot_vers = SMFI_PROT_VERSION;
863 /* check for minimum length */
864 if (g->a_len < MILTER_OPTLEN)
867 "%s: st_optionneg[%ld]: len too short %d < %d",
868 ctx->ctx_smfi->xxfi_name,
869 (long) ctx->ctx_id, (int) g->a_len,
874 /* protocol version */
875 (void) memcpy((void *) &i, (void *) &(g->a_buf[0]), MILTER_LEN_BYTES);
878 #define SMFI_PROT_VERSION_MIN 2
880 /* check for minimum version */
881 if (v < SMFI_PROT_VERSION_MIN)
884 "%s: st_optionneg[%ld]: protocol version too old %d < %d",
885 ctx->ctx_smfi->xxfi_name,
886 (long) ctx->ctx_id, v, SMFI_PROT_VERSION_MIN);
889 ctx->ctx_mta_prot_vers = v;
890 if (ctx->ctx_prot_vers < ctx->ctx_mta_prot_vers)
891 ctx->ctx_prot_vers2mta = ctx->ctx_prot_vers;
893 ctx->ctx_prot_vers2mta = ctx->ctx_mta_prot_vers;
895 (void) memcpy((void *) &i, (void *) &(g->a_buf[MILTER_LEN_BYTES]),
899 /* no flags? set to default value for V1 actions */
902 ctx->ctx_mta_aflags = v; /* MTA action flags */
905 (void) memcpy((void *) &i, (void *) &(g->a_buf[MILTER_LEN_BYTES * 2]),
909 /* no flags? set to default value for V1 protocol */
912 #if _FFR_MDS_NEGOTIATE
913 else if (ctx->ctx_smfi->xxfi_version >= SMFI_VERSION_MDS)
916 ** Allow changing the size only if milter is compiled
917 ** against a version that supports this.
918 ** If a milter is dynamically linked against a newer
919 ** libmilter version, we don't want to "surprise"
920 ** it with a larger buffer as it may rely on it
921 ** even though it is not documented as a limit.
924 if (bitset(SMFIP_MDS_1M, v))
926 internal_pflags |= SMFIP_MDS_1M;
927 (void) smfi_setmaxdatasize(MILTER_MDS_1M);
929 else if (bitset(SMFIP_MDS_256K, v))
931 internal_pflags |= SMFIP_MDS_256K;
932 (void) smfi_setmaxdatasize(MILTER_MDS_256K);
936 /* don't log this for now... */
937 else if (ctx->ctx_smfi->xxfi_version < SMFI_VERSION_MDS &&
938 bitset(SMFIP_MDS_1M|SMFIP_MDS_256K, v))
940 smi_log(SMI_LOG_WARN,
941 "%s: st_optionneg[%ld]: milter version=%X, trying flags=%X",
942 ctx->ctx_smfi->xxfi_name,
943 (long) ctx->ctx_id, ctx->ctx_smfi->xxfi_version, v);
946 #endif /* _FFR_MDS_NEGOTIATE */
949 ** MTA protocol flags.
950 ** We pass the internal flags to the milter as "read only",
951 ** i.e., a milter can read them so it knows which size
952 ** will be used, but any changes by a milter will be ignored
953 ** (see below, search for SMFI_INTERNAL).
956 ctx->ctx_mta_pflags = (v & ~SMFI_INTERNAL) | internal_pflags;
959 ** Copy flags from milter struct into libmilter context;
960 ** this variable will be used later on to check whether
961 ** the MTA "actions" can fulfill the milter requirements,
962 ** but it may be overwritten by the negotiate callback.
965 ctx->ctx_aflags = ctx->ctx_smfi->xxfi_flags;
966 fake_pflags = SMFIP_NR_CONN
977 if (g->a_ctx->ctx_smfi != NULL &&
978 g->a_ctx->ctx_smfi->xxfi_version > 4 &&
979 (fi_negotiate = g->a_ctx->ctx_smfi->xxfi_negotiate) != NULL)
982 unsigned long m_aflags, m_pflags, m_f2, m_f3;
985 ** let milter decide whether the features offered by the
986 ** MTA are "good enough".
988 ** - libmilter can "fake" some features (e.g., SMFIP_NR_HDR)
989 ** - m_f2, m_f3 are for future extensions
993 m_aflags = ctx->ctx_mta_aflags;
994 m_pflags = ctx->ctx_pflags;
995 if ((SMFIP_SKIP & ctx->ctx_mta_pflags) != 0)
996 m_pflags |= SMFIP_SKIP;
997 r = fi_negotiate(g->a_ctx,
999 ctx->ctx_mta_pflags|fake_pflags,
1001 &m_aflags, &m_pflags, &m_f2, &m_f3);
1003 #if _FFR_MILTER_CHECK
1004 testmode = bitset(SMFIP_TEST, m_pflags);
1006 m_pflags &= ~SMFIP_TEST;
1010 ** Types of protocol flags (pflags):
1011 ** 1. do NOT send protocol step X
1012 ** 2. MTA can do/understand something extra (SKIP,
1013 ** send unknown RCPTs)
1014 ** 3. MTA can deal with "no reply" for various protocol steps
1015 ** Note: this mean that it isn't possible to simply set all
1016 ** flags to get "everything":
1017 ** setting a flag of type 1 turns off a step
1018 ** (it should be the other way around:
1019 ** a flag means a protocol step can be sent)
1020 ** setting a flag of type 3 requires that milter
1021 ** never sends a reply for the corresponding step.
1022 ** Summary: the "negation" of protocol flags is causing
1023 ** problems, but at least for type 3 there is no simple
1026 ** What should "all options" mean?
1027 ** send all protocol steps _except_ those for which there is
1028 ** no callback (currently registered in ctx_pflags)
1029 ** expect SKIP as return code? Yes
1030 ** send unknown RCPTs? No,
1031 ** must be explicitly requested?
1032 ** "no reply" for some protocol steps? No,
1033 ** must be explicitly requested.
1036 if (SMFIS_ALL_OPTS == r)
1038 ctx->ctx_aflags = ctx->ctx_mta_aflags;
1039 ctx->ctx_pflags2mta = ctx->ctx_pflags;
1040 if ((SMFIP_SKIP & ctx->ctx_mta_pflags) != 0)
1041 ctx->ctx_pflags2mta |= SMFIP_SKIP;
1043 else if (r != SMFIS_CONTINUE)
1045 smi_log(SMI_LOG_ERR,
1046 "%s: st_optionneg[%ld]: xxfi_negotiate returned %d (protocol options=0x%lx, actions=0x%lx)",
1047 ctx->ctx_smfi->xxfi_name,
1048 (long) ctx->ctx_id, r, ctx->ctx_mta_pflags,
1049 ctx->ctx_mta_aflags);
1050 return _SMFIS_ABORT;
1054 ctx->ctx_aflags = m_aflags;
1055 ctx->ctx_pflags = m_pflags;
1056 ctx->ctx_pflags2mta = m_pflags;
1059 /* check whether some flags need to be "faked" */
1060 i = ctx->ctx_pflags2mta;
1061 if ((ctx->ctx_mta_pflags & i) != i)
1067 ** If some behavior can be faked (set in fake_pflags),
1068 ** but the MTA doesn't support it, then unset
1069 ** that flag in the value that is sent to the MTA.
1072 for (idx = 0; idx < 32; idx++)
1075 if ((ctx->ctx_mta_pflags & b) != b &&
1076 (fake_pflags & b) == b)
1077 ctx->ctx_pflags2mta &= ~b;
1084 ** Set the protocol flags based on the values determined
1085 ** in mi_listener() which checked the defined callbacks.
1088 ctx->ctx_pflags2mta = ctx->ctx_pflags;
1091 /* check whether actions and protocol requirements can be satisfied */
1092 i = ctx->ctx_aflags;
1093 if ((i & ctx->ctx_mta_aflags) != i)
1095 smi_log(SMI_LOG_ERR,
1096 "%s: st_optionneg[%ld]: 0x%lx does not fulfill action requirements 0x%x",
1097 ctx->ctx_smfi->xxfi_name,
1098 (long) ctx->ctx_id, ctx->ctx_mta_aflags, i);
1099 return _SMFIS_ABORT;
1102 i = ctx->ctx_pflags2mta;
1103 if ((ctx->ctx_mta_pflags & i) != i)
1106 ** Older MTAs do not support some protocol steps.
1107 ** As this protocol is a bit "weird" (it asks for steps
1108 ** NOT to be taken/sent) we have to check whether we
1109 ** should turn off those "negative" requests.
1110 ** Currently these are only SMFIP_NODATA and SMFIP_NOUNKNOWN.
1113 if (bitset(SMFIP_NODATA, ctx->ctx_pflags2mta) &&
1114 !bitset(SMFIP_NODATA, ctx->ctx_mta_pflags))
1115 ctx->ctx_pflags2mta &= ~SMFIP_NODATA;
1116 if (bitset(SMFIP_NOUNKNOWN, ctx->ctx_pflags2mta) &&
1117 !bitset(SMFIP_NOUNKNOWN, ctx->ctx_mta_pflags))
1118 ctx->ctx_pflags2mta &= ~SMFIP_NOUNKNOWN;
1119 i = ctx->ctx_pflags2mta;
1122 if ((ctx->ctx_mta_pflags & i) != i)
1124 smi_log(SMI_LOG_ERR,
1125 "%s: st_optionneg[%ld]: 0x%lx does not fulfill protocol requirements 0x%x",
1126 ctx->ctx_smfi->xxfi_name,
1127 (long) ctx->ctx_id, ctx->ctx_mta_pflags, i);
1128 return _SMFIS_ABORT;
1132 if (ctx->ctx_dbg > 3)
1133 sm_dprintf("[%lu] milter_negotiate:"
1134 " mta_actions=0x%lx, mta_flags=0x%lx"
1135 " actions=0x%lx, flags=0x%lx\n"
1136 , (long) ctx->ctx_id
1137 , ctx->ctx_mta_aflags, ctx->ctx_mta_pflags
1138 , ctx->ctx_aflags, ctx->ctx_pflags);
1140 #if _FFR_MILTER_CHECK
1141 if (ctx->ctx_dbg > 3)
1142 sm_dprintf("[%lu] milter_negotiate:"
1143 " testmode=%d, pflags2mta=%X, internal_pflags=%X\n"
1144 , (long) ctx->ctx_id, testmode
1145 , ctx->ctx_pflags2mta, internal_pflags);
1147 /* in test mode: take flags without further modifications */
1149 /* Warning: check statement below! */
1150 #endif /* _FFR_MILTER_CHECK */
1153 ** Remove the internal flags that might have been set by a milter
1154 ** and set only those determined above.
1157 ctx->ctx_pflags2mta = (ctx->ctx_pflags2mta & ~SMFI_INTERNAL)
1159 return _SMFIS_OPTIONS;
1163 ** ST_CONNECTINFO -- receive connection information
1166 ** g -- generic argument structure
1169 ** continue or filter-specified value
1179 unsigned short port = 0;
1180 _SOCK_ADDR sockaddr;
1181 sfsistat (*fi_connect) __P((SMFICTX *, char *, _SOCK_ADDR *));
1184 return _SMFIS_ABORT;
1185 mi_clr_macros(g->a_ctx, g->a_idx + 1);
1186 if (g->a_ctx->ctx_smfi == NULL ||
1187 (fi_connect = g->a_ctx->ctx_smfi->xxfi_connect) == NULL)
1188 return SMFIS_CONTINUE;
1193 while (i <= l && s[i] != '\0')
1196 return _SMFIS_ABORT;
1198 /* Move past trailing \0 in host string */
1201 (void) memset(&sockaddr, '\0', sizeof sockaddr);
1202 if (family != SMFIA_UNKNOWN)
1204 if (i + sizeof port >= l)
1206 smi_log(SMI_LOG_ERR,
1207 "%s: connect[%ld]: wrong len %d >= %d",
1208 g->a_ctx->ctx_smfi->xxfi_name,
1209 (long) g->a_ctx->ctx_id, (int) i, (int) l);
1210 return _SMFIS_ABORT;
1212 (void) memcpy((void *) &port, (void *) (s + i),
1216 /* make sure string is terminated */
1217 if (s[l - 1] != '\0')
1218 return _SMFIS_ABORT;
1220 if (family == SMFIA_INET)
1222 if (inet_aton(s + i, (struct in_addr *) &sockaddr.sin.sin_addr)
1225 smi_log(SMI_LOG_ERR,
1226 "%s: connect[%ld]: inet_aton failed",
1227 g->a_ctx->ctx_smfi->xxfi_name,
1228 (long) g->a_ctx->ctx_id);
1229 return _SMFIS_ABORT;
1231 sockaddr.sa.sa_family = AF_INET;
1233 sockaddr.sin.sin_port = port;
1236 # endif /* NETINET */
1238 if (family == SMFIA_INET6)
1240 if (mi_inet_pton(AF_INET6, s + i,
1241 &sockaddr.sin6.sin6_addr) != 1)
1243 smi_log(SMI_LOG_ERR,
1244 "%s: connect[%ld]: mi_inet_pton failed",
1245 g->a_ctx->ctx_smfi->xxfi_name,
1246 (long) g->a_ctx->ctx_id);
1247 return _SMFIS_ABORT;
1249 sockaddr.sa.sa_family = AF_INET6;
1251 sockaddr.sin6.sin6_port = port;
1254 # endif /* NETINET6 */
1256 if (family == SMFIA_UNIX)
1258 if (sm_strlcpy(sockaddr.sunix.sun_path, s + i,
1259 sizeof sockaddr.sunix.sun_path) >=
1260 sizeof sockaddr.sunix.sun_path)
1262 smi_log(SMI_LOG_ERR,
1263 "%s: connect[%ld]: path too long",
1264 g->a_ctx->ctx_smfi->xxfi_name,
1265 (long) g->a_ctx->ctx_id);
1266 return _SMFIS_ABORT;
1268 sockaddr.sunix.sun_family = AF_UNIX;
1271 # endif /* NETUNIX */
1273 smi_log(SMI_LOG_ERR,
1274 "%s: connect[%ld]: unknown family %d",
1275 g->a_ctx->ctx_smfi->xxfi_name,
1276 (long) g->a_ctx->ctx_id, family);
1277 return _SMFIS_ABORT;
1280 return (*fi_connect)(g->a_ctx, g->a_buf,
1281 family != SMFIA_UNKNOWN ? &sockaddr : NULL);
1285 ** ST_EOH -- end of headers
1288 ** g -- generic argument structure
1291 ** continue or filter-specified value
1298 sfsistat (*fi_eoh) __P((SMFICTX *));
1301 return _SMFIS_ABORT;
1302 if (g->a_ctx->ctx_smfi != NULL &&
1303 (fi_eoh = g->a_ctx->ctx_smfi->xxfi_eoh) != NULL)
1304 return (*fi_eoh)(g->a_ctx);
1305 return SMFIS_CONTINUE;
1309 ** ST_DATA -- DATA command
1312 ** g -- generic argument structure
1315 ** continue or filter-specified value
1322 sfsistat (*fi_data) __P((SMFICTX *));
1325 return _SMFIS_ABORT;
1326 if (g->a_ctx->ctx_smfi != NULL &&
1327 g->a_ctx->ctx_smfi->xxfi_version > 3 &&
1328 (fi_data = g->a_ctx->ctx_smfi->xxfi_data) != NULL)
1329 return (*fi_data)(g->a_ctx);
1330 return SMFIS_CONTINUE;
1334 ** ST_HELO -- helo/ehlo command
1337 ** g -- generic argument structure
1340 ** continue or filter-specified value
1347 sfsistat (*fi_helo) __P((SMFICTX *, char *));
1350 return _SMFIS_ABORT;
1351 mi_clr_macros(g->a_ctx, g->a_idx + 1);
1352 if (g->a_ctx->ctx_smfi != NULL &&
1353 (fi_helo = g->a_ctx->ctx_smfi->xxfi_helo) != NULL)
1355 /* paranoia: check for terminating '\0' */
1356 if (g->a_len == 0 || g->a_buf[g->a_len - 1] != '\0')
1358 return (*fi_helo)(g->a_ctx, g->a_buf);
1360 return SMFIS_CONTINUE;
1364 ** ST_HEADER -- header line
1367 ** g -- generic argument structure
1370 ** continue or filter-specified value
1378 sfsistat (*fi_header) __P((SMFICTX *, char *, char *));
1381 return _SMFIS_ABORT;
1382 if (g->a_ctx->ctx_smfi == NULL ||
1383 (fi_header = g->a_ctx->ctx_smfi->xxfi_header) == NULL)
1384 return SMFIS_CONTINUE;
1385 if (dec_arg2(g->a_buf, g->a_len, &hf, &hv) == MI_SUCCESS)
1386 return (*fi_header)(g->a_ctx, hf, hv);
1388 return _SMFIS_ABORT;
1391 #define ARGV_FCT(lf, rf, idx) \
1393 sfsistat (*lf) __P((SMFICTX *, char **)); \
1397 return _SMFIS_ABORT; \
1398 mi_clr_macros(g->a_ctx, g->a_idx + 1); \
1399 if (g->a_ctx->ctx_smfi == NULL || \
1400 (lf = g->a_ctx->ctx_smfi->rf) == NULL) \
1401 return SMFIS_CONTINUE; \
1402 if ((argv = dec_argv(g->a_buf, g->a_len)) == NULL) \
1403 return _SMFIS_ABORT; \
1404 r = (*lf)(g->a_ctx, argv); \
1409 ** ST_SENDER -- MAIL FROM command
1412 ** g -- generic argument structure
1415 ** continue or filter-specified value
1422 ARGV_FCT(fi_envfrom, xxfi_envfrom, CI_MAIL)
1426 ** ST_RCPT -- RCPT TO command
1429 ** g -- generic argument structure
1432 ** continue or filter-specified value
1439 ARGV_FCT(fi_envrcpt, xxfi_envrcpt, CI_RCPT)
1443 ** ST_UNKNOWN -- unrecognized or unimplemented command
1446 ** g -- generic argument structure
1449 ** continue or filter-specified value
1456 sfsistat (*fi_unknown) __P((SMFICTX *, const char *));
1459 return _SMFIS_ABORT;
1460 if (g->a_ctx->ctx_smfi != NULL &&
1461 g->a_ctx->ctx_smfi->xxfi_version > 2 &&
1462 (fi_unknown = g->a_ctx->ctx_smfi->xxfi_unknown) != NULL)
1463 return (*fi_unknown)(g->a_ctx, (const char *) g->a_buf);
1464 return SMFIS_CONTINUE;
1468 ** ST_MACROS -- deal with macros received from the MTA
1471 ** g -- generic argument structure
1477 ** set pointer in macro array to current values.
1487 if (g == NULL || g->a_len < 1)
1489 if ((argv = dec_argv(g->a_buf + 1, g->a_len - 1)) == NULL)
1491 switch (g->a_buf[0])
1518 if (g->a_ctx->ctx_mac_ptr[i] != NULL)
1519 free(g->a_ctx->ctx_mac_ptr[i]);
1520 if (g->a_ctx->ctx_mac_buf[i] != NULL)
1521 free(g->a_ctx->ctx_mac_buf[i]);
1522 g->a_ctx->ctx_mac_ptr[i] = argv;
1523 g->a_ctx->ctx_mac_buf[i] = g->a_buf;
1528 ** ST_QUIT -- quit command
1531 ** g -- generic argument structure
1542 sfsistat (*fi_close) __P((SMFICTX *));
1545 return _SMFIS_ABORT;
1546 if (g->a_ctx->ctx_smfi != NULL &&
1547 (fi_close = g->a_ctx->ctx_smfi->xxfi_close) != NULL)
1548 (void) (*fi_close)(g->a_ctx);
1549 mi_clr_macros(g->a_ctx, 0);
1550 return _SMFIS_NOREPLY;
1554 ** ST_BODYCHUNK -- deal with a piece of the mail body
1557 ** g -- generic argument structure
1560 ** continue or filter-specified value
1567 sfsistat (*fi_body) __P((SMFICTX *, unsigned char *, size_t));
1570 return _SMFIS_ABORT;
1571 if (g->a_ctx->ctx_smfi != NULL &&
1572 (fi_body = g->a_ctx->ctx_smfi->xxfi_body) != NULL)
1573 return (*fi_body)(g->a_ctx, (unsigned char *)g->a_buf,
1575 return SMFIS_CONTINUE;
1579 ** ST_BODYEND -- deal with the last piece of the mail body
1582 ** g -- generic argument structure
1585 ** continue or filter-specified value
1588 ** sends a reply for the body part (if non-empty).
1596 sfsistat (*fi_body) __P((SMFICTX *, unsigned char *, size_t));
1597 sfsistat (*fi_eom) __P((SMFICTX *));
1600 return _SMFIS_ABORT;
1602 if (g->a_ctx->ctx_smfi != NULL)
1604 if ((fi_body = g->a_ctx->ctx_smfi->xxfi_body) != NULL &&
1608 struct timeval timeout;
1610 timeout.tv_sec = g->a_ctx->ctx_timeout;
1611 timeout.tv_usec = 0;
1612 sd = g->a_ctx->ctx_sd;
1613 r = (*fi_body)(g->a_ctx, (unsigned char *)g->a_buf,
1615 if (r != SMFIS_CONTINUE &&
1616 sendreply(r, sd, &timeout, g->a_ctx) != MI_SUCCESS)
1617 return _SMFIS_ABORT;
1620 if (r == SMFIS_CONTINUE &&
1621 (fi_eom = g->a_ctx->ctx_smfi->xxfi_eom) != NULL)
1622 return (*fi_eom)(g->a_ctx);
1627 ** ST_ABORTFCT -- deal with aborts
1630 ** g -- generic argument structure
1633 ** abort or filter-specified value
1640 sfsistat (*fi_abort) __P((SMFICTX *));
1643 return _SMFIS_ABORT;
1644 if (g != NULL && g->a_ctx->ctx_smfi != NULL &&
1645 (fi_abort = g->a_ctx->ctx_smfi->xxfi_abort) != NULL)
1646 (void) (*fi_abort)(g->a_ctx);
1647 return _SMFIS_NOREPLY;
1651 ** TRANS_OK -- is the state transition ok?
1658 ** state transition ok
1668 if (s >= SIZE_NEXT_STATES)
1672 /* is this state transition allowed? */
1673 if ((MI_MASK(new) & next_states[s]) != 0)
1677 ** no: try next state;
1678 ** this works since the relevant states are ordered
1679 ** strict sequentially
1683 if (n >= SIZE_NEXT_STATES)
1687 ** can we actually "skip" this state?
1688 ** see fix_stm() which sets this bit for those
1689 ** states which the filter program is not interested in
1692 if (bitset(NX_SKIP, next_states[n]))
1696 } while (s < SIZE_NEXT_STATES);
1701 ** FIX_STM -- add "skip" bits to the state transition table
1704 ** ctx -- context structure
1710 ** may change state transition table.
1719 if (ctx == NULL || ctx->ctx_smfi == NULL)
1721 fl = ctx->ctx_pflags;
1722 if (bitset(SMFIP_NOCONNECT, fl))
1723 next_states[ST_CONN] |= NX_SKIP;
1724 if (bitset(SMFIP_NOHELO, fl))
1725 next_states[ST_HELO] |= NX_SKIP;
1726 if (bitset(SMFIP_NOMAIL, fl))
1727 next_states[ST_MAIL] |= NX_SKIP;
1728 if (bitset(SMFIP_NORCPT, fl))
1729 next_states[ST_RCPT] |= NX_SKIP;
1730 if (bitset(SMFIP_NOHDRS, fl))
1731 next_states[ST_HDRS] |= NX_SKIP;
1732 if (bitset(SMFIP_NOEOH, fl))
1733 next_states[ST_EOHS] |= NX_SKIP;
1734 if (bitset(SMFIP_NOBODY, fl))
1735 next_states[ST_BODY] |= NX_SKIP;
1736 if (bitset(SMFIP_NODATA, fl))
1737 next_states[ST_DATA] |= NX_SKIP;
1738 if (bitset(SMFIP_NOUNKNOWN, fl))
1739 next_states[ST_UNKN] |= NX_SKIP;
1743 ** DEC_ARGV -- split a buffer into a list of strings, NULL terminated
1746 ** buf -- buffer with several strings
1747 ** len -- length of buffer
1750 ** array of pointers to the individual strings
1763 for (i = 0; i < len; i++)
1771 /* last entry is only for the name */
1772 s = (char **)malloc((nelem + 1) * (sizeof *s));
1776 for (i = 0, elem = 0; i < len && elem < nelem; i++)
1784 s[elem] = &(buf[i + 1]);
1788 /* overwrite last entry (already done above, just paranoia) */
1794 ** DEC_ARG2 -- split a buffer into two strings
1797 ** buf -- buffer with two strings
1798 ** len -- length of buffer
1799 ** s1,s2 -- pointer to result strings
1802 ** MI_FAILURE/MI_SUCCESS
1806 dec_arg2(buf, len, s1, s2)
1814 /* paranoia: check for terminating '\0' */
1815 if (len == 0 || buf[len - 1] != '\0')
1818 for (i = 1; i < len && buf[i] != '\0'; i++)
1827 ** MI_SENDOK -- is it ok for the filter to send stuff to the MTA?
1830 ** ctx -- context structure
1831 ** flag -- flag to check
1834 ** sending allowed (in current state)
1838 mi_sendok(ctx, flag)
1842 if (ctx == NULL || ctx->ctx_smfi == NULL)
1845 /* did the milter request this operation? */
1846 if (flag != 0 && !bitset(flag, ctx->ctx_aflags))
1849 /* are we in the correct state? It must be "End of Message". */
1850 return ctx->ctx_state == ST_ENDM;
1853 #if _FFR_WORKERS_POOL
1855 ** MI_RD_SOCKET_READY - checks if the socket is ready for read(2)
1861 ** true iff socket is ready for read(2)
1864 #define MI_RD_CMD_TO 1
1865 #define MI_RD_MAX_ERR 16
1868 mi_rd_socket_ready (sd)
1876 fd_set rd_set, exc_set;
1883 pfd.events = POLLIN;
1886 n = poll(&pfd, 1, MI_RD_CMD_TO);
1887 #else /* SM_CONF_POLL */
1888 struct timeval timeout;
1892 FD_SET(sd, &rd_set);
1893 FD_SET(sd, &exc_set);
1895 timeout.tv_sec = MI_RD_CMD_TO / 1000;
1896 timeout.tv_usec = 0;
1897 n = select(sd + 1, &rd_set, NULL, &exc_set, &timeout);
1898 #endif /* SM_CONF_POLL */
1913 } while (nerr < MI_RD_MAX_ERR);
1914 if (nerr >= MI_RD_MAX_ERR)
1918 return (pfd.revents != 0);
1920 return FD_ISSET(sd, &rd_set) || FD_ISSET(sd, &exc_set);
1923 #endif /* _FFR_WORKERS_POOL */