2 * Copyright (c) 1999-2003 Sendmail, 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.120 2004/10/20 21:09:00 ca Exp $")
14 #include "libmilter.h"
16 #if NETINET || NETINET6
17 # include <arpa/inet.h>
18 #endif /* NETINET || NETINET6 */
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 */
45 #define CM_ARG0 0 /* no args */
46 #define CM_ARG1 1 /* one arg (string) */
47 #define CM_ARG2 2 /* two args (strings) */
48 #define CM_ARGA 4 /* one string and _SOCK_ADDR */
49 #define CM_ARGO 5 /* two integers */
50 #define CM_ARGV 8 /* \0 separated list of args, NULL-terminated */
51 #define CM_ARGN 9 /* \0 separated list of args (strings) */
53 /* possible values for cm_todo */
54 #define CT_CONT 0x0000 /* continue reading commands */
55 #define CT_IGNO 0x0001 /* continue even when error */
57 /* not needed right now, done via return code instead */
58 #define CT_KEEP 0x0004 /* keep buffer (contains symbols) */
59 #define CT_END 0x0008 /* start replying */
61 /* index in macro array: macros only for these commands */
68 #if CI_EOM >= MAX_MACROS_ENTRIES
69 ERROR: do not compile with CI_EOM >= MAX_MACROS_ENTRIES
72 /* function prototypes */
73 static int st_abortfct __P((genarg *));
74 static int st_macros __P((genarg *));
75 static int st_optionneg __P((genarg *));
76 static int st_bodychunk __P((genarg *));
77 static int st_connectinfo __P((genarg *));
78 static int st_bodyend __P((genarg *));
79 static int st_helo __P((genarg *));
80 static int st_header __P((genarg *));
81 static int st_sender __P((genarg *));
82 static int st_rcpt __P((genarg *));
84 static int st_unknown __P((genarg *));
85 #endif /* SMFI_VERSION > 2 */
87 static int st_data __P((genarg *));
88 #endif /* SMFI_VERSION > 3 */
89 static int st_eoh __P((genarg *));
90 static int st_quit __P((genarg *));
91 static int sendreply __P((sfsistat, socket_t, struct timeval *, SMFICTX_PTR));
92 static void fix_stm __P((SMFICTX_PTR));
93 static bool trans_ok __P((int, int));
94 static char **dec_argv __P((char *, size_t));
95 static int dec_arg2 __P((char *, size_t, char **, char **));
99 #define ST_INIT 0 /* initial state */
100 #define ST_OPTS 1 /* option negotiation */
101 #define ST_CONN 2 /* connection info */
102 #define ST_HELO 3 /* helo */
103 #define ST_MAIL 4 /* mail from */
104 #define ST_RCPT 5 /* rcpt to */
105 #define ST_DATA 6 /* data */
106 #define ST_HDRS 7 /* headers */
107 #define ST_EOHS 8 /* end of headers */
108 #define ST_BODY 9 /* body */
109 #define ST_ENDM 10 /* end of message */
110 #define ST_QUIT 11 /* quit */
111 #define ST_ABRT 12 /* abort */
112 #define ST_UNKN 13 /* unknown SMTP command */
113 #define ST_LAST ST_UNKN /* last valid state */
114 #define ST_SKIP 15 /* not a state but required for the state table */
116 /* in a mail transaction? must be before eom according to spec. */
117 #define ST_IN_MAIL(st) ((st) >= ST_MAIL && (st) < ST_ENDM)
120 ** set of next states
121 ** each state (ST_*) corresponds to bit in an int value (1 << state)
122 ** each state has a set of allowed transitions ('or' of bits of states)
123 ** so a state transition is valid if the mask of the next state
124 ** is set in the NX_* value
125 ** this function is coded in trans_ok(), see below.
128 #define MI_MASK(x) (0x0001 << (x)) /* generate a bit "mask" for a state */
129 #define NX_INIT (MI_MASK(ST_OPTS))
130 #define NX_OPTS (MI_MASK(ST_CONN) | MI_MASK(ST_UNKN))
131 #define NX_CONN (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN))
132 #define NX_HELO (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN))
133 #define NX_MAIL (MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | MI_MASK(ST_UNKN))
134 #define NX_RCPT (MI_MASK(ST_HDRS) | MI_MASK(ST_EOHS) | MI_MASK(ST_DATA) | \
135 MI_MASK(ST_BODY) | MI_MASK(ST_ENDM) | \
136 MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | MI_MASK(ST_UNKN))
137 #define NX_DATA (MI_MASK(ST_EOHS) | MI_MASK(ST_HDRS) | MI_MASK(ST_ABRT))
138 #define NX_HDRS (MI_MASK(ST_EOHS) | MI_MASK(ST_HDRS) | MI_MASK(ST_ABRT))
139 #define NX_EOHS (MI_MASK(ST_BODY) | MI_MASK(ST_ENDM) | MI_MASK(ST_ABRT))
140 #define NX_BODY (MI_MASK(ST_ENDM) | MI_MASK(ST_BODY) | MI_MASK(ST_ABRT))
141 #define NX_ENDM (MI_MASK(ST_QUIT) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN))
144 #define NX_UNKN (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | \
145 MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | \
147 MI_MASK(ST_BODY) | MI_MASK(ST_UNKN) | \
148 MI_MASK(ST_ABRT) | MI_MASK(ST_QUIT))
149 #define NX_SKIP MI_MASK(ST_SKIP)
151 static int next_states[] =
169 /* commands received by milter */
170 static cmdfct cmds[] =
172 {SMFIC_ABORT, CM_ARG0, ST_ABRT, CT_CONT, CI_NONE, st_abortfct },
173 {SMFIC_MACRO, CM_ARGV, ST_NONE, CT_KEEP, CI_NONE, st_macros },
174 {SMFIC_BODY, CM_ARG1, ST_BODY, CT_CONT, CI_NONE, st_bodychunk },
175 {SMFIC_CONNECT, CM_ARG2, ST_CONN, CT_CONT, CI_CONN, st_connectinfo },
176 {SMFIC_BODYEOB, CM_ARG1, ST_ENDM, CT_CONT, CI_EOM, st_bodyend },
177 {SMFIC_HELO, CM_ARG1, ST_HELO, CT_CONT, CI_HELO, st_helo },
178 {SMFIC_HEADER, CM_ARG2, ST_HDRS, CT_CONT, CI_NONE, st_header },
179 {SMFIC_MAIL, CM_ARGV, ST_MAIL, CT_CONT, CI_MAIL, st_sender },
180 {SMFIC_OPTNEG, CM_ARGO, ST_OPTS, CT_CONT, CI_NONE, st_optionneg },
181 {SMFIC_EOH, CM_ARG0, ST_EOHS, CT_CONT, CI_NONE, st_eoh },
182 {SMFIC_QUIT, CM_ARG0, ST_QUIT, CT_END, CI_NONE, st_quit },
184 {SMFIC_DATA, CM_ARG0, ST_DATA, CT_CONT, CI_NONE, st_data },
185 #endif /* SMFI_VERSION > 3 */
186 {SMFIC_RCPT, CM_ARGV, ST_RCPT, CT_IGNO, CI_RCPT, st_rcpt }
188 ,{SMFIC_UNKNOWN,CM_ARG1, ST_UNKN, CT_IGNO, CI_NONE, st_unknown }
189 #endif /* SMFI_VERSION > 2 */
192 /* additional (internal) reply codes */
193 #define _SMFIS_KEEP 20
194 #define _SMFIS_ABORT 21
195 #define _SMFIS_OPTIONS 22
196 #define _SMFIS_NOREPLY 23
197 #define _SMFIS_FAIL (-1)
198 #define _SMFIS_NONE (-2)
201 ** MI_ENGINE -- receive commands and process them
204 ** ctx -- context structure
207 ** MI_FAILURE/MI_SUCCESS
216 int ret = MI_SUCCESS;
217 int ncmds = sizeof(cmds) / sizeof(cmdfct);
218 int curstate = ST_INIT;
225 struct timeval timeout;
226 int (*f) __P((genarg *));
227 sfsistat (*fi_abort) __P((SMFICTX *));
228 sfsistat (*fi_close) __P((SMFICTX *));
232 fi_abort = ctx->ctx_smfi->xxfi_abort;
233 mi_clr_macros(ctx, 0);
238 /* call abort only if in a mail transaction */
239 call_abort = ST_IN_MAIL(curstate);
240 timeout.tv_sec = ctx->ctx_timeout;
242 if (mi_stop() == MILTER_ABRT)
244 if (ctx->ctx_dbg > 3)
245 sm_dprintf("[%d] milter_abort\n",
252 ** Notice: buf is allocated by mi_rd_cmd() and it will
253 ** usually be free()d after it has been used in f().
254 ** However, if the function returns _SMFIS_KEEP then buf
255 ** contains macros and will not be free()d.
256 ** Hence r must be set to _SMFIS_NONE if a new buf is
257 ** allocated to avoid problem with housekeeping, esp.
258 ** if the code "break"s out of the loop.
262 if ((buf = mi_rd_cmd(sd, &timeout, &cmd, &len,
263 ctx->ctx_smfi->xxfi_name)) == NULL &&
264 cmd < SMFIC_VALIDCMD)
266 if (ctx->ctx_dbg > 5)
267 sm_dprintf("[%d] mi_engine: mi_rd_cmd error (%x)\n",
268 (int) ctx->ctx_id, (int) cmd);
271 ** eof is currently treated as failure ->
272 ** abort() instead of close(), otherwise use:
273 ** if (cmd != SMFIC_EOF)
279 if (ctx->ctx_dbg > 4)
280 sm_dprintf("[%d] got cmd '%c' len %d\n",
281 (int) ctx->ctx_id, cmd, (int) len);
282 for (i = 0; i < ncmds; i++)
284 if (cmd == cmds[i].cm_cmd)
289 /* unknown command */
290 if (ctx->ctx_dbg > 1)
291 sm_dprintf("[%d] cmd '%c' unknown\n",
292 (int) ctx->ctx_id, cmd);
296 if ((f = cmds[i].cm_fct) == NULL)
299 if (ctx->ctx_dbg > 1)
300 sm_dprintf("[%d] cmd '%c' not impl\n",
301 (int) ctx->ctx_id, cmd);
306 /* is new state ok? */
307 newstate = cmds[i].cm_next;
308 if (ctx->ctx_dbg > 5)
309 sm_dprintf("[%d] cur %x new %x nextmask %x\n",
311 curstate, newstate, next_states[curstate]);
313 if (newstate != ST_NONE && !trans_ok(curstate, newstate))
315 if (ctx->ctx_dbg > 1)
316 sm_dprintf("[%d] abort: cur %d (%x) new %d (%x) next %x\n",
318 curstate, MI_MASK(curstate),
319 newstate, MI_MASK(newstate),
320 next_states[curstate]);
322 /* call abort only if in a mail transaction */
323 if (fi_abort != NULL && call_abort)
324 (void) (*fi_abort)(ctx);
327 ** try to reach the new state from HELO
328 ** if it can't be reached, ignore the command.
332 if (!trans_ok(curstate, newstate))
344 if (newstate != ST_NONE)
347 ctx->ctx_state = curstate;
349 arg.a_idx = cmds[i].cm_macros;
350 call_abort = ST_IN_MAIL(curstate);
352 /* call function to deal with command */
354 if (r != _SMFIS_KEEP && buf != NULL)
359 if (sendreply(r, sd, &timeout, ctx) != MI_SUCCESS)
365 if (r == SMFIS_ACCEPT)
367 /* accept mail, no further actions taken */
370 else if (r == SMFIS_REJECT || r == SMFIS_DISCARD ||
374 ** further actions depend on current state
375 ** if the IGNO bit is set: "ignore" the error,
376 ** i.e., stay in the current state
378 if (!bitset(CT_IGNO, cmds[i].cm_todo))
381 else if (r == _SMFIS_ABORT)
383 if (ctx->ctx_dbg > 5)
384 sm_dprintf("[%d] function returned abort\n",
389 } while (!bitset(CT_END, cmds[i].cm_todo));
391 if (ret != MI_SUCCESS)
393 /* call abort only if in a mail transaction */
394 if (fi_abort != NULL && call_abort)
395 (void) (*fi_abort)(ctx);
398 /* close must always be called */
399 if ((fi_close = ctx->ctx_smfi->xxfi_close) != NULL)
400 (void) (*fi_close)(ctx);
401 if (r != _SMFIS_KEEP && buf != NULL)
403 mi_clr_macros(ctx, 0);
407 ** SENDREPLY -- send a reply to the MTA
411 ** sd -- socket descriptor
412 ** timeout_ptr -- (ptr to) timeout to use for sending
413 ** ctx -- context structure
416 ** MI_SUCCESS/MI_FAILURE
420 sendreply(r, sd, timeout_ptr, ctx)
423 struct timeval *timeout_ptr;
426 int ret = MI_SUCCESS;
431 ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_CONTINUE, NULL, 0);
435 if (ctx->ctx_reply != NULL &&
436 ((r == SMFIS_TEMPFAIL && *ctx->ctx_reply == '4') ||
437 (r == SMFIS_REJECT && *ctx->ctx_reply == '5')))
439 ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_REPLYCODE,
441 strlen(ctx->ctx_reply) + 1);
442 free(ctx->ctx_reply);
443 ctx->ctx_reply = NULL;
447 ret = mi_wr_cmd(sd, timeout_ptr, r == SMFIS_REJECT ?
448 SMFIR_REJECT : SMFIR_TEMPFAIL, NULL, 0);
452 ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_DISCARD, NULL, 0);
455 ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_ACCEPT, NULL, 0);
459 char buf[MILTER_OPTLEN];
462 v = htonl(ctx->ctx_smfi->xxfi_version);
463 (void) memcpy(&(buf[0]), (void *) &v, MILTER_LEN_BYTES);
464 v = htonl(ctx->ctx_smfi->xxfi_flags);
465 (void) memcpy(&(buf[MILTER_LEN_BYTES]), (void *) &v,
467 v = htonl(ctx->ctx_pflags);
468 (void) memcpy(&(buf[MILTER_LEN_BYTES * 2]), (void *) &v,
470 ret = mi_wr_cmd(sd, timeout_ptr, SMFIC_OPTNEG, buf,
474 default: /* don't send a reply */
481 ** CLR_MACROS -- clear set of macros starting from a given index
484 ** ctx -- context structure
485 ** m -- index from which to clear all macros
491 mi_clr_macros(ctx, m)
497 for (i = m; i < MAX_MACROS_ENTRIES; i++)
499 if (ctx->ctx_mac_ptr[i] != NULL)
501 free(ctx->ctx_mac_ptr[i]);
502 ctx->ctx_mac_ptr[i] = NULL;
504 if (ctx->ctx_mac_buf[i] != NULL)
506 free(ctx->ctx_mac_buf[i]);
507 ctx->ctx_mac_buf[i] = NULL;
512 ** ST_OPTIONNEG -- negotiate options
515 ** g -- generic argument structure
518 ** abort/send options/continue
527 if (g == NULL || g->a_ctx->ctx_smfi == NULL)
528 return SMFIS_CONTINUE;
529 mi_clr_macros(g->a_ctx, g->a_idx + 1);
531 /* check for minimum length */
532 if (g->a_len < MILTER_OPTLEN)
535 "%s: st_optionneg[%d]: len too short %d < %d",
536 g->a_ctx->ctx_smfi->xxfi_name,
537 (int) g->a_ctx->ctx_id, (int) g->a_len,
542 (void) memcpy((void *) &i, (void *) &(g->a_buf[0]),
545 if (v < g->a_ctx->ctx_smfi->xxfi_version)
547 /* hard failure for now! */
549 "%s: st_optionneg[%d]: version mismatch MTA: %d < milter: %d",
550 g->a_ctx->ctx_smfi->xxfi_name,
551 (int) g->a_ctx->ctx_id, (int) v,
552 g->a_ctx->ctx_smfi->xxfi_version);
556 (void) memcpy((void *) &i, (void *) &(g->a_buf[MILTER_LEN_BYTES]),
560 /* no flags? set to default value for V1 actions */
563 i = g->a_ctx->ctx_smfi->xxfi_flags;
567 "%s: st_optionneg[%d]: 0x%x does not fulfill action requirements 0x%x",
568 g->a_ctx->ctx_smfi->xxfi_name,
569 (int) g->a_ctx->ctx_id, v, i);
573 (void) memcpy((void *) &i, (void *) &(g->a_buf[MILTER_LEN_BYTES * 2]),
577 /* no flags? set to default value for V1 protocol */
580 i = g->a_ctx->ctx_pflags;
584 "%s: st_optionneg[%d]: 0x%x does not fulfill protocol requirements 0x%x",
585 g->a_ctx->ctx_smfi->xxfi_name,
586 (int) g->a_ctx->ctx_id, v, i);
590 return _SMFIS_OPTIONS;
593 ** ST_CONNECTINFO -- receive connection information
596 ** g -- generic argument structure
599 ** continue or filter-specified value
609 unsigned short port = 0;
611 sfsistat (*fi_connect) __P((SMFICTX *, char *, _SOCK_ADDR *));
615 mi_clr_macros(g->a_ctx, g->a_idx + 1);
616 if (g->a_ctx->ctx_smfi == NULL ||
617 (fi_connect = g->a_ctx->ctx_smfi->xxfi_connect) == NULL)
618 return SMFIS_CONTINUE;
623 while (s[i] != '\0' && i <= l)
628 /* Move past trailing \0 in host string */
631 (void) memset(&sockaddr, '\0', sizeof sockaddr);
632 if (family != SMFIA_UNKNOWN)
634 if (i + sizeof port >= l)
637 "%s: connect[%d]: wrong len %d >= %d",
638 g->a_ctx->ctx_smfi->xxfi_name,
639 (int) g->a_ctx->ctx_id, (int) i, (int) l);
642 (void) memcpy((void *) &port, (void *) (s + i),
646 /* make sure string is terminated */
647 if (s[l - 1] != '\0')
650 if (family == SMFIA_INET)
652 if (inet_aton(s + i, (struct in_addr *) &sockaddr.sin.sin_addr)
656 "%s: connect[%d]: inet_aton failed",
657 g->a_ctx->ctx_smfi->xxfi_name,
658 (int) g->a_ctx->ctx_id);
661 sockaddr.sa.sa_family = AF_INET;
663 sockaddr.sin.sin_port = port;
666 # endif /* NETINET */
668 if (family == SMFIA_INET6)
670 if (mi_inet_pton(AF_INET6, s + i,
671 &sockaddr.sin6.sin6_addr) != 1)
674 "%s: connect[%d]: mi_inet_pton failed",
675 g->a_ctx->ctx_smfi->xxfi_name,
676 (int) g->a_ctx->ctx_id);
679 sockaddr.sa.sa_family = AF_INET6;
681 sockaddr.sin6.sin6_port = port;
684 # endif /* NETINET6 */
686 if (family == SMFIA_UNIX)
688 if (sm_strlcpy(sockaddr.sunix.sun_path, s + i,
689 sizeof sockaddr.sunix.sun_path) >=
690 sizeof sockaddr.sunix.sun_path)
693 "%s: connect[%d]: path too long",
694 g->a_ctx->ctx_smfi->xxfi_name,
695 (int) g->a_ctx->ctx_id);
698 sockaddr.sunix.sun_family = AF_UNIX;
701 # endif /* NETUNIX */
704 "%s: connect[%d]: unknown family %d",
705 g->a_ctx->ctx_smfi->xxfi_name,
706 (int) g->a_ctx->ctx_id, family);
710 return (*fi_connect)(g->a_ctx, g->a_buf,
711 family != SMFIA_UNKNOWN ? &sockaddr : NULL);
715 ** ST_EOH -- end of headers
718 ** g -- generic argument structure
721 ** continue or filter-specified value
728 sfsistat (*fi_eoh) __P((SMFICTX *));
732 if (g->a_ctx->ctx_smfi != NULL &&
733 (fi_eoh = g->a_ctx->ctx_smfi->xxfi_eoh) != NULL)
734 return (*fi_eoh)(g->a_ctx);
735 return SMFIS_CONTINUE;
740 ** ST_DATA -- DATA command
743 ** g -- generic argument structure
746 ** continue or filter-specified value
753 sfsistat (*fi_data) __P((SMFICTX *));
757 if (g->a_ctx->ctx_smfi != NULL &&
758 (fi_data = g->a_ctx->ctx_smfi->xxfi_data) != NULL)
759 return (*fi_data)(g->a_ctx);
760 return SMFIS_CONTINUE;
762 #endif /* SMFI_VERSION > 3 */
765 ** ST_HELO -- helo/ehlo command
768 ** g -- generic argument structure
771 ** continue or filter-specified value
777 sfsistat (*fi_helo) __P((SMFICTX *, char *));
781 mi_clr_macros(g->a_ctx, g->a_idx + 1);
782 if (g->a_ctx->ctx_smfi != NULL &&
783 (fi_helo = g->a_ctx->ctx_smfi->xxfi_helo) != NULL)
785 /* paranoia: check for terminating '\0' */
786 if (g->a_len == 0 || g->a_buf[g->a_len - 1] != '\0')
788 return (*fi_helo)(g->a_ctx, g->a_buf);
790 return SMFIS_CONTINUE;
793 ** ST_HEADER -- header line
796 ** g -- generic argument structure
799 ** continue or filter-specified value
807 sfsistat (*fi_header) __P((SMFICTX *, char *, char *));
811 if (g->a_ctx->ctx_smfi == NULL ||
812 (fi_header = g->a_ctx->ctx_smfi->xxfi_header) == NULL)
813 return SMFIS_CONTINUE;
814 if (dec_arg2(g->a_buf, g->a_len, &hf, &hv) == MI_SUCCESS)
815 return (*fi_header)(g->a_ctx, hf, hv);
820 #define ARGV_FCT(lf, rf, idx) \
822 sfsistat (*lf) __P((SMFICTX *, char **)); \
826 return _SMFIS_ABORT; \
827 mi_clr_macros(g->a_ctx, g->a_idx + 1); \
828 if (g->a_ctx->ctx_smfi == NULL || \
829 (lf = g->a_ctx->ctx_smfi->rf) == NULL) \
830 return SMFIS_CONTINUE; \
831 if ((argv = dec_argv(g->a_buf, g->a_len)) == NULL) \
832 return _SMFIS_ABORT; \
833 r = (*lf)(g->a_ctx, argv); \
838 ** ST_SENDER -- MAIL FROM command
841 ** g -- generic argument structure
844 ** continue or filter-specified value
851 ARGV_FCT(fi_envfrom, xxfi_envfrom, CI_MAIL)
854 ** ST_RCPT -- RCPT TO command
857 ** g -- generic argument structure
860 ** continue or filter-specified value
867 ARGV_FCT(fi_envrcpt, xxfi_envrcpt, CI_RCPT)
872 ** ST_UNKNOWN -- unrecognized or unimplemented command
875 ** g -- generic argument structure
878 ** continue or filter-specified value
885 sfsistat (*fi_unknown) __P((SMFICTX *, char *));
889 mi_clr_macros(g->a_ctx, g->a_idx + 1);
890 if (g->a_ctx->ctx_smfi != NULL &&
891 (fi_unknown = g->a_ctx->ctx_smfi->xxfi_unknown) != NULL)
892 return (*fi_unknown)(g->a_ctx, g->a_buf);
893 return SMFIS_CONTINUE;
895 #endif /* SMFI_VERSION > 2 */
898 ** ST_MACROS -- deal with macros received from the MTA
901 ** g -- generic argument structure
907 ** set pointer in macro array to current values.
917 if (g == NULL || g->a_len < 1)
919 if ((argv = dec_argv(g->a_buf + 1, g->a_len - 1)) == NULL)
942 if (g->a_ctx->ctx_mac_ptr[i] != NULL)
943 free(g->a_ctx->ctx_mac_ptr[i]);
944 if (g->a_ctx->ctx_mac_buf[i] != NULL)
945 free(g->a_ctx->ctx_mac_buf[i]);
946 g->a_ctx->ctx_mac_ptr[i] = argv;
947 g->a_ctx->ctx_mac_buf[i] = g->a_buf;
951 ** ST_QUIT -- quit command
954 ** g -- generic argument structure
965 return _SMFIS_NOREPLY;
968 ** ST_BODYCHUNK -- deal with a piece of the mail body
971 ** g -- generic argument structure
974 ** continue or filter-specified value
981 sfsistat (*fi_body) __P((SMFICTX *, unsigned char *, size_t));
985 if (g->a_ctx->ctx_smfi != NULL &&
986 (fi_body = g->a_ctx->ctx_smfi->xxfi_body) != NULL)
987 return (*fi_body)(g->a_ctx, (unsigned char *)g->a_buf,
989 return SMFIS_CONTINUE;
992 ** ST_BODYEND -- deal with the last piece of the mail body
995 ** g -- generic argument structure
998 ** continue or filter-specified value
1001 ** sends a reply for the body part (if non-empty).
1009 sfsistat (*fi_body) __P((SMFICTX *, unsigned char *, size_t));
1010 sfsistat (*fi_eom) __P((SMFICTX *));
1013 return _SMFIS_ABORT;
1015 if (g->a_ctx->ctx_smfi != NULL)
1017 if ((fi_body = g->a_ctx->ctx_smfi->xxfi_body) != NULL &&
1021 struct timeval timeout;
1023 timeout.tv_sec = g->a_ctx->ctx_timeout;
1024 timeout.tv_usec = 0;
1025 sd = g->a_ctx->ctx_sd;
1026 r = (*fi_body)(g->a_ctx, (unsigned char *)g->a_buf,
1028 if (r != SMFIS_CONTINUE &&
1029 sendreply(r, sd, &timeout, g->a_ctx) != MI_SUCCESS)
1030 return _SMFIS_ABORT;
1033 if (r == SMFIS_CONTINUE &&
1034 (fi_eom = g->a_ctx->ctx_smfi->xxfi_eom) != NULL)
1035 return (*fi_eom)(g->a_ctx);
1039 ** ST_ABORTFCT -- deal with aborts
1042 ** g -- generic argument structure
1045 ** abort or filter-specified value
1052 sfsistat (*fi_abort) __P((SMFICTX *));
1055 return _SMFIS_ABORT;
1056 if (g != NULL && g->a_ctx->ctx_smfi != NULL &&
1057 (fi_abort = g->a_ctx->ctx_smfi->xxfi_abort) != NULL)
1058 (void) (*fi_abort)(g->a_ctx);
1059 return _SMFIS_NOREPLY;
1062 ** TRANS_OK -- is the state transition ok?
1069 ** state transition ok
1081 /* is this state transition allowed? */
1082 if ((MI_MASK(new) & next_states[s]) != 0)
1086 ** no: try next state;
1087 ** this works since the relevant states are ordered
1088 ** strict sequentially
1094 ** can we actually "skip" this state?
1095 ** see fix_stm() which sets this bit for those
1096 ** states which the filter program is not interested in
1099 if (bitset(NX_SKIP, next_states[n]))
1103 } while (s <= ST_LAST);
1107 ** FIX_STM -- add "skip" bits to the state transition table
1110 ** ctx -- context structure
1116 ** may change state transition table.
1125 if (ctx == NULL || ctx->ctx_smfi == NULL)
1127 fl = ctx->ctx_pflags;
1128 if (bitset(SMFIP_NOCONNECT, fl))
1129 next_states[ST_CONN] |= NX_SKIP;
1130 if (bitset(SMFIP_NOHELO, fl))
1131 next_states[ST_HELO] |= NX_SKIP;
1132 if (bitset(SMFIP_NOMAIL, fl))
1133 next_states[ST_MAIL] |= NX_SKIP;
1134 if (bitset(SMFIP_NORCPT, fl))
1135 next_states[ST_RCPT] |= NX_SKIP;
1136 if (bitset(SMFIP_NOHDRS, fl))
1137 next_states[ST_HDRS] |= NX_SKIP;
1138 if (bitset(SMFIP_NOEOH, fl))
1139 next_states[ST_EOHS] |= NX_SKIP;
1140 if (bitset(SMFIP_NOBODY, fl))
1141 next_states[ST_BODY] |= NX_SKIP;
1144 ** DEC_ARGV -- split a buffer into a list of strings, NULL terminated
1147 ** buf -- buffer with several strings
1148 ** len -- length of buffer
1151 ** array of pointers to the individual strings
1164 for (i = 0; i < len; i++)
1172 /* last entry is only for the name */
1173 s = (char **)malloc((nelem + 1) * (sizeof *s));
1177 for (i = 0, elem = 0; i < len && elem < nelem; i++)
1185 s[elem] = &(buf[i + 1]);
1189 /* overwrite last entry (already done above, just paranoia) */
1194 ** DEC_ARG2 -- split a buffer into two strings
1197 ** buf -- buffer with two strings
1198 ** len -- length of buffer
1199 ** s1,s2 -- pointer to result strings
1202 ** MI_FAILURE/MI_SUCCESS
1206 dec_arg2(buf, len, s1, s2)
1214 /* paranoia: check for terminating '\0' */
1215 if (len == 0 || buf[len - 1] != '\0')
1218 for (i = 1; i < len && buf[i] != '\0'; i++)
1226 ** SENDOK -- is it ok for the filter to send stuff to the MTA?
1229 ** ctx -- context structure
1230 ** flag -- flag to check
1233 ** sending allowed (in current state)
1237 mi_sendok(ctx, flag)
1241 if (ctx == NULL || ctx->ctx_smfi == NULL)
1244 /* did the milter request this operation? */
1245 if (flag != 0 && !bitset(flag, ctx->ctx_smfi->xxfi_flags))
1248 /* are we in the correct state? It must be "End of Message". */
1249 return ctx->ctx_state == ST_ENDM;