1 /*
2  * include/types/action.h
3  * This file contains actions definitions.
4  *
5  * Copyright (C) 2000-2010 Willy Tarreau - w@1wt.eu
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation, version 2.1
10  * exclusively.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #ifndef _TYPES_ACTION_H
23 #define _TYPES_ACTION_H
24 
25 #include <common/regex.h>
26 
27 #include <types/applet.h>
28 #include <types/stick_table.h>
29 
30 enum act_from {
31 	ACT_F_TCP_REQ_CON, /* tcp-request connection */
32 	ACT_F_TCP_REQ_SES, /* tcp-request session */
33 	ACT_F_TCP_REQ_CNT, /* tcp-request content */
34 	ACT_F_TCP_RES_CNT, /* tcp-response content */
35 	ACT_F_HTTP_REQ,    /* http-request */
36 	ACT_F_HTTP_RES,    /* http-response */
37 };
38 
39 enum act_return {
40 	ACT_RET_CONT,  /* continue processing. */
41 	ACT_RET_STOP,  /* stop processing. */
42 	ACT_RET_YIELD, /* call me again. */
43 	ACT_RET_ERR,   /* processing error. */
44 	ACT_RET_DONE,  /* processing done, stop processing */
45 };
46 
47 enum act_parse_ret {
48 	ACT_RET_PRS_OK,    /* continue processing. */
49 	ACT_RET_PRS_ERR,   /* abort processing. */
50 };
51 
52 /* flags passed to custom actions */
53 enum act_flag {
54 	ACT_FLAG_NONE  = 0x00000000,  /* no flag */
55 	ACT_FLAG_FINAL = 0x00000001,  /* last call, cannot yield */
56 	ACT_FLAG_FIRST = 0x00000002,  /* first call for this action */
57 };
58 
59 enum act_name {
60 	ACT_CUSTOM = 0,
61 
62 	/* common action */
63 	ACT_ACTION_ALLOW,
64 	ACT_ACTION_DENY,
65 
66 	/* common http actions .*/
67 	ACT_HTTP_ADD_HDR,
68 	ACT_HTTP_REPLACE_HDR,
69 	ACT_HTTP_REPLACE_VAL,
70 	ACT_HTTP_SET_HDR,
71 	ACT_HTTP_DEL_HDR,
72 	ACT_HTTP_REDIR,
73 	ACT_HTTP_SET_NICE,
74 	ACT_HTTP_SET_LOGL,
75 	ACT_HTTP_SET_TOS,
76 	ACT_HTTP_SET_MARK,
77 	ACT_HTTP_ADD_ACL,
78 	ACT_HTTP_DEL_ACL,
79 	ACT_HTTP_DEL_MAP,
80 	ACT_HTTP_SET_MAP,
81 	ACT_HTTP_EARLY_HINT,
82 
83 	/* http request actions. */
84 	ACT_HTTP_REQ_TARPIT,
85 	ACT_HTTP_REQ_AUTH,
86 
87 	/* tcp actions */
88 	ACT_TCP_EXPECT_PX,
89 	ACT_TCP_EXPECT_CIP,
90 	ACT_TCP_CLOSE, /* close at the sender's */
91 	ACT_TCP_CAPTURE, /* capture a fetched sample */
92 
93 	/* track stick counters */
94 	ACT_ACTION_TRK_SC0,
95 	/* SC1, SC2, ... SCn */
96 	ACT_ACTION_TRK_SCMAX = ACT_ACTION_TRK_SC0 + MAX_SESS_STKCTR - 1,
97 };
98 
99 struct act_rule {
100 	struct list list;
101 	struct acl_cond *cond;                 /* acl condition to meet */
102 	enum act_name action;                  /* ACT_ACTION_* */
103 	enum act_from from;                    /* ACT_F_* */
104 	short deny_status;                     /* HTTP status to return to user when denying */
105 	enum act_return (*action_ptr)(struct act_rule *rule, struct proxy *px,  /* ptr to custom action */
106 	                              struct session *sess, struct stream *s, int flags);
107 	int (*check_ptr)(struct act_rule *rule, struct proxy *px, char **err); /* ptr to check function */
108 	struct action_kw *kw;
109 	struct applet applet;                  /* used for the applet registration. */
110 	union {
111 		struct {
112 			struct sample_expr *expr;
113 			char *varname;
114 			char *resolvers_id;
115 			struct dns_resolvers *resolvers;
116 			struct dns_options dns_opts;
117 		} dns;                        /* dns resolution */
118 		struct {
119 			char *realm;
120 		} auth;                        /* arg used by "auth" */
121 		struct {
122 			char *name;            /* header name */
123 			int name_len;          /* header name's length */
124 			struct list fmt;       /* log-format compatible expression */
125 			struct my_regex *re;   /* used by replace-header and replace-value */
126 		} hdr_add;                     /* args used by "add-header" and "set-header" */
127 		struct {
128 			char *name;            /* header name */
129 			int name_len;          /* header name's length */
130 			struct list fmt;       /* log-format compatible expression */
131 		} early_hint;
132 		struct redirect_rule *redir;   /* redirect rule or "http-request redirect" */
133 		int nice;                      /* nice value for ACT_HTTP_SET_NICE */
134 		int loglevel;                  /* log-level value for ACT_HTTP_SET_LOGL */
135 		int tos;                       /* tos value for ACT_HTTP_SET_TOS */
136 		int mark;                      /* nfmark value for ACT_HTTP_SET_MARK */
137 		struct {
138 			char *ref;             /* MAP or ACL file name to update */
139 			struct list key;       /* pattern to retrieve MAP or ACL key */
140 			struct list value;     /* pattern to retrieve MAP value */
141 		} map;
142 		struct sample_expr *expr;
143 		struct {
144 			struct list logfmt;
145 			int action;
146 		} http;
147 		struct {
148 			struct sample_expr *expr; /* expression used as the key */
149 			struct cap_hdr *hdr;      /* the capture storage */
150 		} cap;
151 		struct {
152 			unsigned int code;     /* HTTP status code */
153 			const char *reason;    /* HTTP status reason */
154 		} status;
155 		struct {
156 			struct sample_expr *expr;
157 			int idx;
158 		} capid;
159 		struct hlua_rule *hlua_rule;
160 		struct {
161 			struct sample_expr *expr;
162 			const char *name;
163 			enum vars_scope scope;
164 		} vars;
165 		struct {
166 			int sc;
167 		} gpc;
168 		struct {
169 			int sc;
170 			long long int value;
171 		} gpt;
172 		struct track_ctr_prm trk_ctr;
173 		struct {
174 			void *p[4];
175 		} act;                         /* generic pointers to be used by custom actions */
176 	} arg;                                 /* arguments used by some actions */
177 };
178 
179 struct action_kw {
180 	const char *kw;
181 	enum act_parse_ret (*parse)(const char **args, int *cur_arg, struct proxy *px,
182 	                            struct act_rule *rule, char **err);
183 	int match_pfx;
184 	void *private;
185 };
186 
187 struct action_kw_list {
188 	struct list list;
189 	struct action_kw kw[VAR_ARRAY];
190 };
191 
192 #endif /* _TYPES_ACTION_H */
193