1 /*
2 * Sample management functions.
3 *
4 * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
5 * Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14 #include <ctype.h>
15 #include <string.h>
16 #include <arpa/inet.h>
17 #include <stdio.h>
18
19 #include <types/global.h>
20
21 #include <common/chunk.h>
22 #include <common/hash.h>
23 #include <common/standard.h>
24 #include <common/uri_auth.h>
25 #include <common/base64.h>
26
27 #include <proto/arg.h>
28 #include <proto/auth.h>
29 #include <proto/log.h>
30 #include <proto/proto_http.h>
31 #include <proto/proxy.h>
32 #include <proto/sample.h>
33 #include <proto/stick_table.h>
34 #include <proto/vars.h>
35
36 /* sample type names */
37 const char *smp_to_type[SMP_TYPES] = {
38 [SMP_T_ANY] = "any",
39 [SMP_T_BOOL] = "bool",
40 [SMP_T_SINT] = "sint",
41 [SMP_T_ADDR] = "addr",
42 [SMP_T_IPV4] = "ipv4",
43 [SMP_T_IPV6] = "ipv6",
44 [SMP_T_STR] = "str",
45 [SMP_T_BIN] = "bin",
46 [SMP_T_METH] = "meth",
47 };
48
49 /* static sample used in sample_process() when <p> is NULL */
50 static struct sample temp_smp;
51
52 /* list head of all known sample fetch keywords */
53 static struct sample_fetch_kw_list sample_fetches = {
54 .list = LIST_HEAD_INIT(sample_fetches.list)
55 };
56
57 /* list head of all known sample format conversion keywords */
58 static struct sample_conv_kw_list sample_convs = {
59 .list = LIST_HEAD_INIT(sample_convs.list)
60 };
61
62 const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
63 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
64 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
65 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
66 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
67 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
68 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
69 SMP_VAL_FE_LOG_END),
70
71 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
72 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
73 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
74 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
75 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
76 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
77 SMP_VAL_FE_LOG_END),
78
79 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
80 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
81 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
82 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
83 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
84 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
85 SMP_VAL_FE_LOG_END),
86
87 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
88 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
89 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
90 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
91 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
92 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
93 SMP_VAL_FE_LOG_END),
94
95 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
96 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
97 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
98 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
99 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
100 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
101 SMP_VAL_FE_LOG_END),
102
103 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
104 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
105 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
106 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
107 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
108 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
109 SMP_VAL_FE_LOG_END),
110
111 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
112 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
113 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
114 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
115 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
116 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
117 SMP_VAL___________),
118
119 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
120 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
121 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
122 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
123 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
124 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
125 SMP_VAL___________),
126
127 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
128 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
129 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
130 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
131 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
132 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
133 SMP_VAL_FE_LOG_END),
134
135 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
136 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
137 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
138 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
139 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
140 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
141 SMP_VAL___________),
142
143 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
144 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
145 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
146 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
147 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
148 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
149 SMP_VAL_FE_LOG_END),
150
151 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
152 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
153 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
154 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
155 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
156 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
157 SMP_VAL_FE_LOG_END),
158
159 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
160 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
161 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
162 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
163 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
164 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
165 SMP_VAL_FE_LOG_END),
166
167 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
168 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
169 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
170 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
171 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
172 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
173 SMP_VAL_FE_LOG_END),
174
175 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
176 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
177 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
178 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
179 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
180 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
181 SMP_VAL___________),
182
183 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
184 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
185 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
186 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
187 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
188 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
189 SMP_VAL___________),
190
191 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
192 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
193 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
194 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
195 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
196 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
197 SMP_VAL_FE_LOG_END),
198
199 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
200 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
201 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
202 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
203 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
204 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
205 SMP_VAL___________),
206
207 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
208 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
209 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
210 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
211 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
212 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
213 SMP_VAL_FE_LOG_END),
214
215 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
216 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
217 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
218 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
219 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
220 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
221 SMP_VAL_FE_LOG_END),
222
223 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
224 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
225 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
226 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
227 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
228 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
229 SMP_VAL_FE_LOG_END),
230
231 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
232 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
233 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
234 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
235 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
236 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
237 SMP_VAL_FE_LOG_END),
238 };
239
240 static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
241 [SMP_SRC_INTRN] = "internal state",
242 [SMP_SRC_LISTN] = "listener",
243 [SMP_SRC_FTEND] = "frontend",
244 [SMP_SRC_L4CLI] = "client address",
245 [SMP_SRC_L5CLI] = "client-side connection",
246 [SMP_SRC_TRACK] = "track counters",
247 [SMP_SRC_L6REQ] = "request buffer",
248 [SMP_SRC_HRQHV] = "HTTP request headers",
249 [SMP_SRC_HRQHP] = "HTTP request",
250 [SMP_SRC_HRQBO] = "HTTP request body",
251 [SMP_SRC_BKEND] = "backend",
252 [SMP_SRC_SERVR] = "server",
253 [SMP_SRC_L4SRV] = "server address",
254 [SMP_SRC_L5SRV] = "server-side connection",
255 [SMP_SRC_L6RES] = "response buffer",
256 [SMP_SRC_HRSHV] = "HTTP response headers",
257 [SMP_SRC_HRSHP] = "HTTP response",
258 [SMP_SRC_HRSBO] = "HTTP response body",
259 [SMP_SRC_RQFIN] = "request buffer statistics",
260 [SMP_SRC_RSFIN] = "response buffer statistics",
261 [SMP_SRC_TXFIN] = "transaction statistics",
262 [SMP_SRC_SSFIN] = "session statistics",
263 };
264
265 static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
266 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
267 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
268 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
269 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
270 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
271 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
272 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
273 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
274 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
275 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
276 [SMP_CKP_BE_SRV_CON] = "server source selection",
277 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
278 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
279 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
280 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
281 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
282 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
283 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
284 [SMP_CKP_FE_LOG_END] = "logs",
285 };
286
287 /* This function returns the type of the data returned by the sample_expr.
288 * It assumes that the <expr> and all of its converters are properly
289 * initialized.
290 */
291 inline
smp_expr_output_type(struct sample_expr * expr)292 int smp_expr_output_type(struct sample_expr *expr)
293 {
294 struct sample_conv_expr *smp_expr;
295
296 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
297 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
298 return smp_expr->conv->out_type;
299 }
300 return expr->fetch->out_type;
301 }
302
303
304 /* fill the trash with a comma-delimited list of source names for the <use> bit
305 * field which must be composed of a non-null set of SMP_USE_* flags. The return
306 * value is the pointer to the string in the trash buffer.
307 */
sample_src_names(unsigned int use)308 const char *sample_src_names(unsigned int use)
309 {
310 int bit;
311
312 trash.len = 0;
313 trash.str[0] = '\0';
314 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
315 if (!(use & ~((1 << bit) - 1)))
316 break; /* no more bits */
317
318 if (!(use & (1 << bit)))
319 continue; /* bit not set */
320
321 trash.len += snprintf(trash.str + trash.len, trash.size - trash.len, "%s%s",
322 (use & ((1 << bit) - 1)) ? "," : "",
323 fetch_src_names[bit]);
324 }
325 return trash.str;
326 }
327
328 /* return a pointer to the correct sample checkpoint name, or "unknown" when
329 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
330 * if set.
331 */
sample_ckp_names(unsigned int use)332 const char *sample_ckp_names(unsigned int use)
333 {
334 int bit;
335
336 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
337 if (use & (1 << bit))
338 return fetch_ckp_names[bit];
339 return "unknown sample check place, please report this bug";
340 }
341
342 /*
343 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
344 * for next parsing sessions. The fetch keywords capabilities are also computed
345 * from their ->use field.
346 */
sample_register_fetches(struct sample_fetch_kw_list * kwl)347 void sample_register_fetches(struct sample_fetch_kw_list *kwl)
348 {
349 struct sample_fetch *sf;
350 int bit;
351
352 for (sf = kwl->kw; sf->kw != NULL; sf++) {
353 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
354 if (sf->use & (1 << bit))
355 sf->val |= fetch_cap[bit];
356 }
357 LIST_ADDQ(&sample_fetches.list, &kwl->list);
358 }
359
360 /*
361 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
362 * parsing sessions.
363 */
sample_register_convs(struct sample_conv_kw_list * pckl)364 void sample_register_convs(struct sample_conv_kw_list *pckl)
365 {
366 LIST_ADDQ(&sample_convs.list, &pckl->list);
367 }
368
369 /*
370 * Returns the pointer on sample fetch keyword structure identified by
371 * string of <len> in buffer <kw>.
372 *
373 */
find_sample_fetch(const char * kw,int len)374 struct sample_fetch *find_sample_fetch(const char *kw, int len)
375 {
376 int index;
377 struct sample_fetch_kw_list *kwl;
378
379 list_for_each_entry(kwl, &sample_fetches.list, list) {
380 for (index = 0; kwl->kw[index].kw != NULL; index++) {
381 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
382 kwl->kw[index].kw[len] == '\0')
383 return &kwl->kw[index];
384 }
385 }
386 return NULL;
387 }
388
389 /* This function browses the list of available sample fetches. <current> is
390 * the last used sample fetch. If it is the first call, it must set to NULL.
391 * <idx> is the index of the next sample fetch entry. It is used as private
392 * value. It is useless to initiate it.
393 *
394 * It returns always the new fetch_sample entry, and NULL when the end of
395 * the list is reached.
396 */
sample_fetch_getnext(struct sample_fetch * current,int * idx)397 struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
398 {
399 struct sample_fetch_kw_list *kwl;
400 struct sample_fetch *base;
401
402 if (!current) {
403 /* Get first kwl entry. */
404 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
405 (*idx) = 0;
406 } else {
407 /* Get kwl corresponding to the curret entry. */
408 base = current + 1 - (*idx);
409 kwl = container_of(base, struct sample_fetch_kw_list, kw);
410 }
411
412 while (1) {
413
414 /* Check if kwl is the last entry. */
415 if (&kwl->list == &sample_fetches.list)
416 return NULL;
417
418 /* idx contain the next keyword. If it is available, return it. */
419 if (kwl->kw[*idx].kw) {
420 (*idx)++;
421 return &kwl->kw[(*idx)-1];
422 }
423
424 /* get next entry in the main list, and return NULL if the end is reached. */
425 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
426
427 /* Set index to 0, ans do one other loop. */
428 (*idx) = 0;
429 }
430 }
431
432 /* This function browses the list of available converters. <current> is
433 * the last used converter. If it is the first call, it must set to NULL.
434 * <idx> is the index of the next converter entry. It is used as private
435 * value. It is useless to initiate it.
436 *
437 * It returns always the next sample_conv entry, and NULL when the end of
438 * the list is reached.
439 */
sample_conv_getnext(struct sample_conv * current,int * idx)440 struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
441 {
442 struct sample_conv_kw_list *kwl;
443 struct sample_conv *base;
444
445 if (!current) {
446 /* Get first kwl entry. */
447 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
448 (*idx) = 0;
449 } else {
450 /* Get kwl corresponding to the curret entry. */
451 base = current + 1 - (*idx);
452 kwl = container_of(base, struct sample_conv_kw_list, kw);
453 }
454
455 while (1) {
456 /* Check if kwl is the last entry. */
457 if (&kwl->list == &sample_convs.list)
458 return NULL;
459
460 /* idx contain the next keyword. If it is available, return it. */
461 if (kwl->kw[*idx].kw) {
462 (*idx)++;
463 return &kwl->kw[(*idx)-1];
464 }
465
466 /* get next entry in the main list, and return NULL if the end is reached. */
467 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
468
469 /* Set index to 0, ans do one other loop. */
470 (*idx) = 0;
471 }
472 }
473
474 /*
475 * Returns the pointer on sample format conversion keyword structure identified by
476 * string of <len> in buffer <kw>.
477 *
478 */
find_sample_conv(const char * kw,int len)479 struct sample_conv *find_sample_conv(const char *kw, int len)
480 {
481 int index;
482 struct sample_conv_kw_list *kwl;
483
484 list_for_each_entry(kwl, &sample_convs.list, list) {
485 for (index = 0; kwl->kw[index].kw != NULL; index++) {
486 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
487 kwl->kw[index].kw[len] == '\0')
488 return &kwl->kw[index];
489 }
490 }
491 return NULL;
492 }
493
494 /******************************************************************/
495 /* Sample casts functions */
496 /******************************************************************/
497
c_ip2int(struct sample * smp)498 static int c_ip2int(struct sample *smp)
499 {
500 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
501 smp->data.type = SMP_T_SINT;
502 return 1;
503 }
504
c_ip2str(struct sample * smp)505 static int c_ip2str(struct sample *smp)
506 {
507 struct chunk *trash = get_trash_chunk();
508
509 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->str, trash->size))
510 return 0;
511
512 trash->len = strlen(trash->str);
513 smp->data.u.str = *trash;
514 smp->data.type = SMP_T_STR;
515 smp->flags &= ~SMP_F_CONST;
516
517 return 1;
518 }
519
c_ip2ipv6(struct sample * smp)520 static int c_ip2ipv6(struct sample *smp)
521 {
522 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
523 smp->data.type = SMP_T_IPV6;
524 return 1;
525 }
526
c_ipv62ip(struct sample * smp)527 static int c_ipv62ip(struct sample *smp)
528 {
529 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
530 return 0;
531 smp->data.type = SMP_T_IPV4;
532 return 1;
533 }
534
c_ipv62str(struct sample * smp)535 static int c_ipv62str(struct sample *smp)
536 {
537 struct chunk *trash = get_trash_chunk();
538
539 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->str, trash->size))
540 return 0;
541
542 trash->len = strlen(trash->str);
543 smp->data.u.str = *trash;
544 smp->data.type = SMP_T_STR;
545 smp->flags &= ~SMP_F_CONST;
546 return 1;
547 }
548
549 /*
550 static int c_ipv62ip(struct sample *smp)
551 {
552 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
553 }
554 */
555
c_int2ip(struct sample * smp)556 static int c_int2ip(struct sample *smp)
557 {
558 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
559 smp->data.type = SMP_T_IPV4;
560 return 1;
561 }
562
c_int2ipv6(struct sample * smp)563 static int c_int2ipv6(struct sample *smp)
564 {
565 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
566 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
567 smp->data.type = SMP_T_IPV6;
568 return 1;
569 }
570
c_str2addr(struct sample * smp)571 static int c_str2addr(struct sample *smp)
572 {
573 if (!buf2ip(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv4)) {
574 if (!buf2ip6(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv6))
575 return 0;
576 smp->data.type = SMP_T_IPV6;
577 smp->flags &= ~SMP_F_CONST;
578 return 1;
579 }
580 smp->data.type = SMP_T_IPV4;
581 smp->flags &= ~SMP_F_CONST;
582 return 1;
583 }
584
c_str2ip(struct sample * smp)585 static int c_str2ip(struct sample *smp)
586 {
587 if (!buf2ip(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv4))
588 return 0;
589 smp->data.type = SMP_T_IPV4;
590 smp->flags &= ~SMP_F_CONST;
591 return 1;
592 }
593
c_str2ipv6(struct sample * smp)594 static int c_str2ipv6(struct sample *smp)
595 {
596 if (!buf2ip6(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv6))
597 return 0;
598 smp->data.type = SMP_T_IPV6;
599 smp->flags &= ~SMP_F_CONST;
600 return 1;
601 }
602
603 /*
604 * The NULL char always enforces the end of string if it is met.
605 * Data is never changed, so we can ignore the CONST case
606 */
c_bin2str(struct sample * smp)607 static int c_bin2str(struct sample *smp)
608 {
609 int i;
610
611 for (i = 0; i < smp->data.u.str.len; i++) {
612 if (!smp->data.u.str.str[i]) {
613 smp->data.u.str.len = i;
614 break;
615 }
616 }
617 return 1;
618 }
619
c_int2str(struct sample * smp)620 static int c_int2str(struct sample *smp)
621 {
622 struct chunk *trash = get_trash_chunk();
623 char *pos;
624
625 pos = lltoa_r(smp->data.u.sint, trash->str, trash->size);
626 if (!pos)
627 return 0;
628
629 trash->size = trash->size - (pos - trash->str);
630 trash->str = pos;
631 trash->len = strlen(pos);
632 smp->data.u.str = *trash;
633 smp->data.type = SMP_T_STR;
634 smp->flags &= ~SMP_F_CONST;
635 return 1;
636 }
637
638 /* This function inconditionally duplicates data and removes the "const" flag.
639 * For strings and binary blocks, it also provides a known allocated size with
640 * a length that is capped to the size, and ensures a trailing zero is always
641 * appended for strings. This is necessary for some operations which may
642 * require to extend the length. It returns 0 if it fails, 1 on success.
643 */
smp_dup(struct sample * smp)644 int smp_dup(struct sample *smp)
645 {
646 struct chunk *trash;
647
648 /* If the const flag is not set, we don't need to duplicate the
649 * pattern as it can be modified in place.
650 */
651
652 switch (smp->data.type) {
653 case SMP_T_BOOL:
654 case SMP_T_SINT:
655 case SMP_T_ADDR:
656 case SMP_T_IPV4:
657 case SMP_T_IPV6:
658 /* These type are not const. */
659 break;
660
661 case SMP_T_STR:
662 trash = get_trash_chunk();
663 trash->len = (smp->data.type == SMP_T_STR) ?
664 smp->data.u.str.len : smp->data.u.meth.str.len;
665 if (trash->len > trash->size - 1)
666 trash->len = trash->size - 1;
667
668 memcpy(trash->str, (smp->data.type == SMP_T_STR) ?
669 smp->data.u.str.str : smp->data.u.meth.str.str, trash->len);
670 trash->str[trash->len] = 0;
671 smp->data.u.str = *trash;
672 break;
673
674 case SMP_T_BIN:
675 trash = get_trash_chunk();
676 trash->len = smp->data.u.str.len;
677 if (trash->len > trash->size)
678 trash->len = trash->size;
679
680 memcpy(trash->str, smp->data.u.str.str, trash->len);
681 smp->data.u.str = *trash;
682 break;
683 default:
684 /* Other cases are unexpected. */
685 return 0;
686 }
687
688 /* remove const flag */
689 smp->flags &= ~SMP_F_CONST;
690 return 1;
691 }
692
c_none(struct sample * smp)693 int c_none(struct sample *smp)
694 {
695 return 1;
696 }
697
c_str2int(struct sample * smp)698 static int c_str2int(struct sample *smp)
699 {
700 const char *str;
701 const char *end;
702
703 if (smp->data.u.str.len == 0)
704 return 0;
705
706 str = smp->data.u.str.str;
707 end = smp->data.u.str.str + smp->data.u.str.len;
708
709 smp->data.u.sint = read_int64(&str, end);
710 smp->data.type = SMP_T_SINT;
711 smp->flags &= ~SMP_F_CONST;
712 return 1;
713 }
714
c_str2meth(struct sample * smp)715 static int c_str2meth(struct sample *smp)
716 {
717 enum http_meth_t meth;
718 int len;
719
720 meth = find_http_meth(smp->data.u.str.str, smp->data.u.str.len);
721 if (meth == HTTP_METH_OTHER) {
722 len = smp->data.u.str.len;
723 smp->data.u.meth.str.str = smp->data.u.str.str;
724 smp->data.u.meth.str.len = len;
725 }
726 else
727 smp->flags &= ~SMP_F_CONST;
728 smp->data.u.meth.meth = meth;
729 smp->data.type = SMP_T_METH;
730 return 1;
731 }
732
c_meth2str(struct sample * smp)733 static int c_meth2str(struct sample *smp)
734 {
735 int len;
736 enum http_meth_t meth;
737
738 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
739 /* The method is unknown. Copy the original pointer. */
740 len = smp->data.u.meth.str.len;
741 smp->data.u.str.str = smp->data.u.meth.str.str;
742 smp->data.u.str.len = len;
743 smp->data.type = SMP_T_STR;
744 }
745 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
746 /* The method is known, copy the pointer containing the string. */
747 meth = smp->data.u.meth.meth;
748 smp->data.u.str.str = http_known_methods[meth].name;
749 smp->data.u.str.len = http_known_methods[meth].len;
750 smp->flags |= SMP_F_CONST;
751 smp->data.type = SMP_T_STR;
752 }
753 else {
754 /* Unknown method */
755 return 0;
756 }
757 return 1;
758 }
759
c_addr2bin(struct sample * smp)760 static int c_addr2bin(struct sample *smp)
761 {
762 struct chunk *chk = get_trash_chunk();
763
764 if (smp->data.type == SMP_T_IPV4) {
765 chk->len = 4;
766 memcpy(chk->str, &smp->data.u.ipv4, chk->len);
767 }
768 else if (smp->data.type == SMP_T_IPV6) {
769 chk->len = 16;
770 memcpy(chk->str, &smp->data.u.ipv6, chk->len);
771 }
772 else
773 return 0;
774
775 smp->data.u.str = *chk;
776 smp->data.type = SMP_T_BIN;
777 return 1;
778 }
779
c_int2bin(struct sample * smp)780 static int c_int2bin(struct sample *smp)
781 {
782 struct chunk *chk = get_trash_chunk();
783
784 *(unsigned long long int *)chk->str = my_htonll(smp->data.u.sint);
785 chk->len = 8;
786
787 smp->data.u.str = *chk;
788 smp->data.type = SMP_T_BIN;
789 return 1;
790 }
791
792
793 /*****************************************************************/
794 /* Sample casts matrix: */
795 /* sample_casts[from type][to type] */
796 /* NULL pointer used for impossible sample casts */
797 /*****************************************************************/
798
799 sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
800 /* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
801 /* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
802 /* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
803 /* SINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, c_int2ipv6, c_int2str, c_int2bin, NULL, },
804 /* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
805 /* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
806 /* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
807 /* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
808 /* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
809 /* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
810 };
811
812 /*
813 * Parse a sample expression configuration:
814 * fetch keyword followed by format conversion keywords.
815 * Returns a pointer on allocated sample expression structure.
816 * The caller must have set al->ctx.
817 */
sample_parse_expr(char ** str,int * idx,const char * file,int line,char ** err_msg,struct arg_list * al)818 struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err_msg, struct arg_list *al)
819 {
820 const char *begw; /* beginning of word */
821 const char *endw; /* end of word */
822 const char *endt; /* end of term */
823 struct sample_expr *expr;
824 struct sample_fetch *fetch;
825 struct sample_conv *conv;
826 unsigned long prev_type;
827 char *fkw = NULL;
828 char *ckw = NULL;
829 int err_arg;
830
831 begw = str[*idx];
832 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
833
834 if (endw == begw) {
835 memprintf(err_msg, "missing fetch method");
836 goto out_error;
837 }
838
839 /* keep a copy of the current fetch keyword for error reporting */
840 fkw = my_strndup(begw, endw - begw);
841
842 fetch = find_sample_fetch(begw, endw - begw);
843 if (!fetch) {
844 memprintf(err_msg, "unknown fetch method '%s'", fkw);
845 goto out_error;
846 }
847
848 endt = endw;
849 if (*endt == '(') {
850 /* look for the end of this term and skip the opening parenthesis */
851 endt = ++endw;
852 while (*endt && *endt != ')')
853 endt++;
854 if (*endt != ')') {
855 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
856 goto out_error;
857 }
858 }
859
860 /* At this point, we have :
861 * - begw : beginning of the keyword
862 * - endw : end of the keyword, first character not part of keyword
863 * nor the opening parenthesis (so first character of args
864 * if present).
865 * - endt : end of the term (=endw or last parenthesis if args are present)
866 */
867
868 if (fetch->out_type >= SMP_TYPES) {
869 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
870 goto out_error;
871 }
872 prev_type = fetch->out_type;
873
874 expr = calloc(1, sizeof(*expr));
875 if (!expr)
876 goto out_error;
877
878 LIST_INIT(&(expr->conv_exprs));
879 expr->fetch = fetch;
880 expr->arg_p = empty_arg_list;
881
882 /* Note that we call the argument parser even with an empty string,
883 * this allows it to automatically create entries for mandatory
884 * implicit arguments (eg: local proxy name).
885 */
886 al->kw = expr->fetch->kw;
887 al->conv = NULL;
888 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
889 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
890 goto out_error;
891 }
892
893 if (!expr->arg_p) {
894 expr->arg_p = empty_arg_list;
895 }
896 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
897 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
898 goto out_error;
899 }
900
901 /* Now process the converters if any. We have two supported syntaxes
902 * for the converters, which can be combined :
903 * - comma-delimited list of converters just after the keyword and args ;
904 * - one converter per keyword
905 * The combination allows to have each keyword being a comma-delimited
906 * series of converters.
907 *
908 * We want to process the former first, then the latter. For this we start
909 * from the beginning of the supposed place in the exiting conv chain, which
910 * starts at the last comma (endt).
911 */
912
913 while (1) {
914 struct sample_conv_expr *conv_expr;
915 int err_arg;
916 int argcnt;
917
918 if (*endt == ')') /* skip last closing parenthesis */
919 endt++;
920
921 if (*endt && *endt != ',') {
922 if (ckw)
923 memprintf(err_msg, "missing comma after converter '%s'", ckw);
924 else
925 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
926 goto out_error;
927 }
928
929 while (*endt == ',') /* then trailing commas */
930 endt++;
931
932 begw = endt; /* start of converter */
933
934 if (!*begw) {
935 /* none ? skip to next string */
936 (*idx)++;
937 begw = str[*idx];
938 if (!begw || !*begw)
939 break;
940 }
941
942 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
943
944 free(ckw);
945 ckw = my_strndup(begw, endw - begw);
946
947 conv = find_sample_conv(begw, endw - begw);
948 if (!conv) {
949 /* we found an isolated keyword that we don't know, it's not ours */
950 if (begw == str[*idx])
951 break;
952 memprintf(err_msg, "unknown converter '%s'", ckw);
953 goto out_error;
954 }
955
956 endt = endw;
957 if (*endt == '(') {
958 /* look for the end of this term */
959 endt = ++endw;
960 while (*endt && *endt != ')')
961 endt++;
962 if (*endt != ')') {
963 memprintf(err_msg, "syntax error: missing ')' after converter '%s'", ckw);
964 goto out_error;
965 }
966 }
967
968 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
969 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
970 goto out_error;
971 }
972
973 /* If impossible type conversion */
974 if (!sample_casts[prev_type][conv->in_type]) {
975 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
976 goto out_error;
977 }
978
979 prev_type = conv->out_type;
980 conv_expr = calloc(1, sizeof(*conv_expr));
981 if (!conv_expr)
982 goto out_error;
983
984 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
985 conv_expr->conv = conv;
986
987 al->kw = expr->fetch->kw;
988 al->conv = conv_expr->conv->kw;
989 argcnt = make_arg_list(endw, endt - endw, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al);
990 if (argcnt < 0) {
991 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
992 goto out_error;
993 }
994
995 if (argcnt && !conv->arg_mask) {
996 memprintf(err_msg, "converter '%s' does not support any args", ckw);
997 goto out_error;
998 }
999
1000 if (!conv_expr->arg_p)
1001 conv_expr->arg_p = empty_arg_list;
1002
1003 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1004 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
1005 goto out_error;
1006 }
1007 }
1008
1009 out:
1010 free(fkw);
1011 free(ckw);
1012 return expr;
1013
1014 out_error:
1015 /* TODO: prune_sample_expr(expr); */
1016 expr = NULL;
1017 goto out;
1018 }
1019
1020 /*
1021 * Process a fetch + format conversion of defined by the sample expression <expr>
1022 * on request or response considering the <opt> parameter.
1023 * Returns a pointer on a typed sample structure containing the result or NULL if
1024 * sample is not found or when format conversion failed.
1025 * If <p> is not null, function returns results in structure pointed by <p>.
1026 * If <p> is null, functions returns a pointer on a static sample structure.
1027 *
1028 * Note: the fetch functions are required to properly set the return type. The
1029 * conversion functions must do so too. However the cast functions do not need
1030 * to since they're made to cast mutiple types according to what is required.
1031 *
1032 * The caller may indicate in <opt> if it considers the result final or not.
1033 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1034 * if the result is stable or not, according to the following table :
1035 *
1036 * return MAY_CHANGE FINAL Meaning for the sample
1037 * NULL 0 * Not present and will never be (eg: header)
1038 * NULL 1 0 Not present yet, could change (eg: POST param)
1039 * NULL 1 1 Not present yet, will not change anymore
1040 * smp 0 * Present and will not change (eg: header)
1041 * smp 1 0 Present, may change (eg: request length)
1042 * smp 1 1 Present, last known value (eg: request length)
1043 */
sample_process(struct proxy * px,struct session * sess,struct stream * strm,unsigned int opt,struct sample_expr * expr,struct sample * p)1044 struct sample *sample_process(struct proxy *px, struct session *sess,
1045 struct stream *strm, unsigned int opt,
1046 struct sample_expr *expr, struct sample *p)
1047 {
1048 struct sample_conv_expr *conv_expr;
1049
1050 if (p == NULL) {
1051 p = &temp_smp;
1052 memset(p, 0, sizeof(*p));
1053 }
1054
1055 smp_set_owner(p, px, sess, strm, opt);
1056 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
1057 return NULL;
1058
1059 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
1060 /* we want to ensure that p->type can be casted into
1061 * conv_expr->conv->in_type. We have 3 possibilities :
1062 * - NULL => not castable.
1063 * - c_none => nothing to do (let's optimize it)
1064 * - other => apply cast and prepare to fail
1065 */
1066 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
1067 return NULL;
1068
1069 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1070 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
1071 return NULL;
1072
1073 /* OK cast succeeded */
1074
1075 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
1076 return NULL;
1077 }
1078 return p;
1079 }
1080
1081 /*
1082 * Resolve all remaining arguments in proxy <p>. Returns the number of
1083 * errors or 0 if everything is fine.
1084 */
smp_resolve_args(struct proxy * p)1085 int smp_resolve_args(struct proxy *p)
1086 {
1087 struct arg_list *cur, *bak;
1088 const char *ctx, *where;
1089 const char *conv_ctx, *conv_pre, *conv_pos;
1090 struct userlist *ul;
1091 struct my_regex *reg;
1092 struct arg *arg;
1093 int cfgerr = 0;
1094 int rflags;
1095
1096 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1097 struct proxy *px;
1098 struct server *srv;
1099 char *pname, *sname;
1100 char *err;
1101
1102 arg = cur->arg;
1103
1104 /* prepare output messages */
1105 conv_pre = conv_pos = conv_ctx = "";
1106 if (cur->conv) {
1107 conv_ctx = cur->conv;
1108 conv_pre = "conversion keyword '";
1109 conv_pos = "' for ";
1110 }
1111
1112 where = "in";
1113 ctx = "sample fetch keyword";
1114 switch (cur->ctx) {
1115 case ARGC_STK: where = "in stick rule in"; break;
1116 case ARGC_TRK: where = "in tracking rule in"; break;
1117 case ARGC_LOG: where = "in log-format string in"; break;
1118 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1119 case ARGC_HRQ: where = "in http-request header format string in"; break;
1120 case ARGC_HRS: where = "in http-response header format string in"; break;
1121 case ARGC_UIF: where = "in unique-id-format string in"; break;
1122 case ARGC_RDR: where = "in redirect format string in"; break;
1123 case ARGC_CAP: where = "in capture rule in"; break;
1124 case ARGC_ACL: ctx = "ACL keyword"; break;
1125 case ARGC_SRV: where = "in server directive in"; break;
1126 case ARGC_SPOE: where = "in spoe-message directive in"; break;
1127 }
1128
1129 /* set a few default settings */
1130 px = p;
1131 pname = p->id;
1132
1133 switch (arg->type) {
1134 case ARGT_SRV:
1135 if (!arg->data.str.len) {
1136 Alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1137 cur->file, cur->line,
1138 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1139 cfgerr++;
1140 continue;
1141 }
1142
1143 /* we support two formats : "bck/srv" and "srv" */
1144 sname = strrchr(arg->data.str.str, '/');
1145
1146 if (sname) {
1147 *sname++ = '\0';
1148 pname = arg->data.str.str;
1149
1150 px = proxy_be_by_name(pname);
1151 if (!px) {
1152 Alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1153 cur->file, cur->line, pname,
1154 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1155 cfgerr++;
1156 break;
1157 }
1158 }
1159 else
1160 sname = arg->data.str.str;
1161
1162 srv = findserver(px, sname);
1163 if (!srv) {
1164 Alert("parsing [%s:%d] : unable to find server '%s' in proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1165 cur->file, cur->line, sname, pname,
1166 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1167 cfgerr++;
1168 break;
1169 }
1170
1171 free(arg->data.str.str);
1172 arg->data.str.str = NULL;
1173 arg->unresolved = 0;
1174 arg->data.srv = srv;
1175 break;
1176
1177 case ARGT_FE:
1178 if (arg->data.str.len) {
1179 pname = arg->data.str.str;
1180 px = proxy_fe_by_name(pname);
1181 }
1182
1183 if (!px) {
1184 Alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1185 cur->file, cur->line, pname,
1186 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1187 cfgerr++;
1188 break;
1189 }
1190
1191 if (!(px->cap & PR_CAP_FE)) {
1192 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1193 cur->file, cur->line, pname,
1194 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1195 cfgerr++;
1196 break;
1197 }
1198
1199 free(arg->data.str.str);
1200 arg->data.str.str = NULL;
1201 arg->unresolved = 0;
1202 arg->data.prx = px;
1203 break;
1204
1205 case ARGT_BE:
1206 if (arg->data.str.len) {
1207 pname = arg->data.str.str;
1208 px = proxy_be_by_name(pname);
1209 }
1210
1211 if (!px) {
1212 Alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1213 cur->file, cur->line, pname,
1214 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1215 cfgerr++;
1216 break;
1217 }
1218
1219 if (!(px->cap & PR_CAP_BE)) {
1220 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1221 cur->file, cur->line, pname,
1222 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1223 cfgerr++;
1224 break;
1225 }
1226
1227 free(arg->data.str.str);
1228 arg->data.str.str = NULL;
1229 arg->unresolved = 0;
1230 arg->data.prx = px;
1231 break;
1232
1233 case ARGT_TAB:
1234 if (arg->data.str.len) {
1235 pname = arg->data.str.str;
1236 px = proxy_tbl_by_name(pname);
1237 }
1238
1239 if (!px) {
1240 Alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1241 cur->file, cur->line, pname,
1242 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1243 cfgerr++;
1244 break;
1245 }
1246
1247 if (!px->table.size) {
1248 Alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1249 cur->file, cur->line, pname,
1250 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1251 cfgerr++;
1252 break;
1253 }
1254
1255 if (p->bind_proc & ~px->bind_proc) {
1256 Alert("parsing [%s:%d] : stick-table '%s' not present on all processes covered by proxy '%s'.\n",
1257 cur->file, cur->line, px->id, p->id);
1258 cfgerr++;
1259 break;
1260 }
1261
1262 free(arg->data.str.str);
1263 arg->data.str.str = NULL;
1264 arg->unresolved = 0;
1265 arg->data.prx = px;
1266 break;
1267
1268 case ARGT_USR:
1269 if (!arg->data.str.len) {
1270 Alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1271 cur->file, cur->line,
1272 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1273 cfgerr++;
1274 break;
1275 }
1276
1277 if (p->uri_auth && p->uri_auth->userlist &&
1278 !strcmp(p->uri_auth->userlist->name, arg->data.str.str))
1279 ul = p->uri_auth->userlist;
1280 else
1281 ul = auth_find_userlist(arg->data.str.str);
1282
1283 if (!ul) {
1284 Alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1285 cur->file, cur->line, arg->data.str.str,
1286 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1287 cfgerr++;
1288 break;
1289 }
1290
1291 free(arg->data.str.str);
1292 arg->data.str.str = NULL;
1293 arg->unresolved = 0;
1294 arg->data.usr = ul;
1295 break;
1296
1297 case ARGT_REG:
1298 if (!arg->data.str.len) {
1299 Alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1300 cur->file, cur->line,
1301 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1302 cfgerr++;
1303 continue;
1304 }
1305
1306 reg = calloc(1, sizeof(*reg));
1307 if (!reg) {
1308 Alert("parsing [%s:%d] : not enough memory to build regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1309 cur->file, cur->line,
1310 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1311 cfgerr++;
1312 continue;
1313 }
1314
1315 rflags = 0;
1316 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1317 err = NULL;
1318
1319 if (!regex_comp(arg->data.str.str, reg, !(rflags & REG_ICASE), 1 /* capture substr */, &err)) {
1320 Alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1321 cur->file, cur->line,
1322 arg->data.str.str,
1323 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
1324 cfgerr++;
1325 continue;
1326 }
1327
1328 free(arg->data.str.str);
1329 arg->data.str.str = NULL;
1330 arg->unresolved = 0;
1331 arg->data.reg = reg;
1332 break;
1333
1334
1335 }
1336
1337 LIST_DEL(&cur->list);
1338 free(cur);
1339 } /* end of args processing */
1340
1341 return cfgerr;
1342 }
1343
1344 /*
1345 * Process a fetch + format conversion as defined by the sample expression
1346 * <expr> on request or response considering the <opt> parameter. The output is
1347 * not explicitly set to <smp_type>, but shall be compatible with it as
1348 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1349 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
1350 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1351 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1352 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1353 * take actions (eg: wait longer). If a sample could not be found or could not
1354 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1355 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1356 * still hope to get it after waiting longer, and is not converted to string.
1357 * The possible output combinations are the following :
1358 *
1359 * return MAY_CHANGE FINAL Meaning for the sample
1360 * NULL * * Not present and will never be (eg: header)
1361 * smp 0 * Final value converted (eg: header)
1362 * smp 1 0 Not present yet, may appear later (eg: header)
1363 * smp 1 1 never happens (either flag is cleared on output)
1364 */
sample_fetch_as_type(struct proxy * px,struct session * sess,struct stream * strm,unsigned int opt,struct sample_expr * expr,int smp_type)1365 struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
1366 struct stream *strm, unsigned int opt,
1367 struct sample_expr *expr, int smp_type)
1368 {
1369 struct sample *smp = &temp_smp;
1370
1371 memset(smp, 0, sizeof(*smp));
1372
1373 if (!sample_process(px, sess, strm, opt, expr, smp)) {
1374 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1375 return smp;
1376 return NULL;
1377 }
1378
1379 if (!sample_casts[smp->data.type][smp_type])
1380 return NULL;
1381
1382 if (!sample_casts[smp->data.type][smp_type](smp))
1383 return NULL;
1384
1385 smp->flags &= ~SMP_F_MAY_CHANGE;
1386 return smp;
1387 }
1388
release_sample_arg(struct arg * p)1389 static void release_sample_arg(struct arg *p)
1390 {
1391 struct arg *p_back = p;
1392
1393 if (!p)
1394 return;
1395
1396 while (p->type != ARGT_STOP) {
1397 if (p->type == ARGT_STR || p->unresolved) {
1398 free(p->data.str.str);
1399 p->data.str.str = NULL;
1400 p->unresolved = 0;
1401 }
1402 else if (p->type == ARGT_REG) {
1403 if (p->data.reg) {
1404 regex_free(p->data.reg);
1405 free(p->data.reg);
1406 p->data.reg = NULL;
1407 }
1408 }
1409 p++;
1410 }
1411
1412 if (p_back != empty_arg_list)
1413 free(p_back);
1414 }
1415
release_sample_expr(struct sample_expr * expr)1416 void release_sample_expr(struct sample_expr *expr)
1417 {
1418 struct sample_conv_expr *conv_expr, *conv_exprb;
1419
1420 if (!expr)
1421 return;
1422
1423 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list)
1424 release_sample_arg(conv_expr->arg_p);
1425 release_sample_arg(expr->arg_p);
1426 free(expr);
1427 }
1428
1429 /*****************************************************************/
1430 /* Sample format convert functions */
1431 /* These functions set the data type on return. */
1432 /*****************************************************************/
1433
1434 #ifdef DEBUG_EXPR
sample_conv_debug(const struct arg * arg_p,struct sample * smp,void * private)1435 static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1436 {
1437 int i;
1438 struct sample tmp;
1439
1440 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
1441 fprintf(stderr, "[debug converter] type: %s ", smp_to_type[smp->data.type]);
1442 if (!sample_casts[smp->data.type][SMP_T_STR]) {
1443 fprintf(stderr, "(undisplayable)");
1444 } else {
1445
1446 /* Copy sample fetch. This put the sample as const, the
1447 * cast will copy data if a transformation is required.
1448 */
1449 memcpy(&tmp, smp, sizeof(struct sample));
1450 tmp.flags = SMP_F_CONST;
1451
1452 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1453 fprintf(stderr, "(undisplayable)");
1454
1455 else {
1456 /* Display the displayable chars*. */
1457 fputc('<', stderr);
1458 for (i = 0; i < tmp.data.u.str.len; i++) {
1459 if (isprint(tmp.data.u.str.str[i]))
1460 fputc(tmp.data.u.str.str[i], stderr);
1461 else
1462 fputc('.', stderr);
1463 }
1464 fputc('>', stderr);
1465 }
1466 }
1467 fputc('\n', stderr);
1468 }
1469 return 1;
1470 }
1471 #endif
1472
sample_conv_bin2base64(const struct arg * arg_p,struct sample * smp,void * private)1473 static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
1474 {
1475 struct chunk *trash = get_trash_chunk();
1476 int b64_len;
1477
1478 trash->len = 0;
1479 b64_len = a2base64(smp->data.u.str.str, smp->data.u.str.len, trash->str, trash->size);
1480 if (b64_len < 0)
1481 return 0;
1482
1483 trash->len = b64_len;
1484 smp->data.u.str = *trash;
1485 smp->data.type = SMP_T_STR;
1486 smp->flags &= ~SMP_F_CONST;
1487 return 1;
1488 }
1489
sample_conv_bin2hex(const struct arg * arg_p,struct sample * smp,void * private)1490 static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
1491 {
1492 struct chunk *trash = get_trash_chunk();
1493 unsigned char c;
1494 int ptr = 0;
1495
1496 trash->len = 0;
1497 while (ptr < smp->data.u.str.len && trash->len <= trash->size - 2) {
1498 c = smp->data.u.str.str[ptr++];
1499 trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
1500 trash->str[trash->len++] = hextab[c & 0xF];
1501 }
1502 smp->data.u.str = *trash;
1503 smp->data.type = SMP_T_STR;
1504 smp->flags &= ~SMP_F_CONST;
1505 return 1;
1506 }
1507
1508 /* hashes the binary input into a 32-bit unsigned int */
sample_conv_djb2(const struct arg * arg_p,struct sample * smp,void * private)1509 static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
1510 {
1511 smp->data.u.sint = hash_djb2(smp->data.u.str.str, smp->data.u.str.len);
1512 if (arg_p && arg_p->data.sint)
1513 smp->data.u.sint = full_hash(smp->data.u.sint);
1514 smp->data.type = SMP_T_SINT;
1515 return 1;
1516 }
1517
sample_conv_str2lower(const struct arg * arg_p,struct sample * smp,void * private)1518 static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
1519 {
1520 int i;
1521
1522 if (!smp_make_rw(smp))
1523 return 0;
1524
1525 for (i = 0; i < smp->data.u.str.len; i++) {
1526 if ((smp->data.u.str.str[i] >= 'A') && (smp->data.u.str.str[i] <= 'Z'))
1527 smp->data.u.str.str[i] += 'a' - 'A';
1528 }
1529 return 1;
1530 }
1531
sample_conv_str2upper(const struct arg * arg_p,struct sample * smp,void * private)1532 static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
1533 {
1534 int i;
1535
1536 if (!smp_make_rw(smp))
1537 return 0;
1538
1539 for (i = 0; i < smp->data.u.str.len; i++) {
1540 if ((smp->data.u.str.str[i] >= 'a') && (smp->data.u.str.str[i] <= 'z'))
1541 smp->data.u.str.str[i] += 'A' - 'a';
1542 }
1543 return 1;
1544 }
1545
1546 /* takes the netmask in arg_p */
sample_conv_ipmask(const struct arg * arg_p,struct sample * smp,void * private)1547 static int sample_conv_ipmask(const struct arg *arg_p, struct sample *smp, void *private)
1548 {
1549 smp->data.u.ipv4.s_addr &= arg_p->data.ipv4.s_addr;
1550 smp->data.type = SMP_T_IPV4;
1551 return 1;
1552 }
1553
1554 /* takes an UINT value on input supposed to represent the time since EPOCH,
1555 * adds an optional offset found in args[1] and emits a string representing
1556 * the local time in the format specified in args[1] using strftime().
1557 */
sample_conv_ltime(const struct arg * args,struct sample * smp,void * private)1558 static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
1559 {
1560 struct chunk *temp;
1561 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
1562 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
1563 struct tm *tm;
1564
1565 /* add offset */
1566 if (args[1].type == ARGT_SINT)
1567 curr_date += args[1].data.sint;
1568
1569 tm = localtime(&curr_date);
1570 if (!tm)
1571 return 0;
1572 temp = get_trash_chunk();
1573 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, tm);
1574 smp->data.u.str = *temp;
1575 smp->data.type = SMP_T_STR;
1576 return 1;
1577 }
1578
1579 /* hashes the binary input into a 32-bit unsigned int */
sample_conv_sdbm(const struct arg * arg_p,struct sample * smp,void * private)1580 static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
1581 {
1582 smp->data.u.sint = hash_sdbm(smp->data.u.str.str, smp->data.u.str.len);
1583 if (arg_p && arg_p->data.sint)
1584 smp->data.u.sint = full_hash(smp->data.u.sint);
1585 smp->data.type = SMP_T_SINT;
1586 return 1;
1587 }
1588
1589 /* takes an UINT value on input supposed to represent the time since EPOCH,
1590 * adds an optional offset found in args[1] and emits a string representing
1591 * the UTC date in the format specified in args[1] using strftime().
1592 */
sample_conv_utime(const struct arg * args,struct sample * smp,void * private)1593 static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
1594 {
1595 struct chunk *temp;
1596 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
1597 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
1598 struct tm *tm;
1599
1600 /* add offset */
1601 if (args[1].type == ARGT_SINT)
1602 curr_date += args[1].data.sint;
1603
1604 tm = gmtime(&curr_date);
1605 if (!tm)
1606 return 0;
1607 temp = get_trash_chunk();
1608 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, tm);
1609 smp->data.u.str = *temp;
1610 smp->data.type = SMP_T_STR;
1611 return 1;
1612 }
1613
1614 /* hashes the binary input into a 32-bit unsigned int */
sample_conv_wt6(const struct arg * arg_p,struct sample * smp,void * private)1615 static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
1616 {
1617 smp->data.u.sint = hash_wt6(smp->data.u.str.str, smp->data.u.str.len);
1618 if (arg_p && arg_p->data.sint)
1619 smp->data.u.sint = full_hash(smp->data.u.sint);
1620 smp->data.type = SMP_T_SINT;
1621 return 1;
1622 }
1623
1624 /* hashes the binary input into a 32-bit unsigned int */
sample_conv_crc32(const struct arg * arg_p,struct sample * smp,void * private)1625 static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
1626 {
1627 smp->data.u.sint = hash_crc32(smp->data.u.str.str, smp->data.u.str.len);
1628 if (arg_p && arg_p->data.sint)
1629 smp->data.u.sint = full_hash(smp->data.u.sint);
1630 smp->data.type = SMP_T_SINT;
1631 return 1;
1632 }
1633
1634 /* This function escape special json characters. The returned string can be
1635 * safely set between two '"' and used as json string. The json string is
1636 * defined like this:
1637 *
1638 * any Unicode character except '"' or '\' or control character
1639 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1640 *
1641 * The enum input_type contain all the allowed mode for decoding the input
1642 * string.
1643 */
1644 enum input_type {
1645 IT_ASCII = 0,
1646 IT_UTF8,
1647 IT_UTF8S,
1648 IT_UTF8P,
1649 IT_UTF8PS,
1650 };
sample_conv_json_check(struct arg * arg,struct sample_conv * conv,const char * file,int line,char ** err)1651 static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1652 const char *file, int line, char **err)
1653 {
1654 if (!arg) {
1655 memprintf(err, "Unexpected empty arg list");
1656 return 0;
1657 }
1658
1659 if (arg->type != ARGT_STR) {
1660 memprintf(err, "Unexpected arg type");
1661 return 0;
1662 }
1663
1664 if (strcmp(arg->data.str.str, "") == 0) {
1665 arg->type = ARGT_SINT;
1666 arg->data.sint = IT_ASCII;
1667 return 1;
1668 }
1669
1670 else if (strcmp(arg->data.str.str, "ascii") == 0) {
1671 arg->type = ARGT_SINT;
1672 arg->data.sint = IT_ASCII;
1673 return 1;
1674 }
1675
1676 else if (strcmp(arg->data.str.str, "utf8") == 0) {
1677 arg->type = ARGT_SINT;
1678 arg->data.sint = IT_UTF8;
1679 return 1;
1680 }
1681
1682 else if (strcmp(arg->data.str.str, "utf8s") == 0) {
1683 arg->type = ARGT_SINT;
1684 arg->data.sint = IT_UTF8S;
1685 return 1;
1686 }
1687
1688 else if (strcmp(arg->data.str.str, "utf8p") == 0) {
1689 arg->type = ARGT_SINT;
1690 arg->data.sint = IT_UTF8P;
1691 return 1;
1692 }
1693
1694 else if (strcmp(arg->data.str.str, "utf8ps") == 0) {
1695 arg->type = ARGT_SINT;
1696 arg->data.sint = IT_UTF8PS;
1697 return 1;
1698 }
1699
1700 memprintf(err, "Unexpected input code type. "
1701 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
1702 return 0;
1703 }
1704
sample_conv_json(const struct arg * arg_p,struct sample * smp,void * private)1705 static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
1706 {
1707 struct chunk *temp;
1708 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1709 const char *str;
1710 int len;
1711 enum input_type input_type = IT_ASCII;
1712 unsigned int c;
1713 unsigned int ret;
1714 char *p;
1715
1716 if (arg_p)
1717 input_type = arg_p->data.sint;
1718
1719 temp = get_trash_chunk();
1720 temp->len = 0;
1721
1722 p = smp->data.u.str.str;
1723 while (p < smp->data.u.str.str + smp->data.u.str.len) {
1724
1725 if (input_type == IT_ASCII) {
1726 /* Read input as ASCII. */
1727 c = *(unsigned char *)p;
1728 p++;
1729 }
1730 else {
1731 /* Read input as UTF8. */
1732 ret = utf8_next(p, smp->data.u.str.len - ( p - smp->data.u.str.str ), &c);
1733 p += utf8_return_length(ret);
1734
1735 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
1736 return 0;
1737 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
1738 continue;
1739 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1740 return 0;
1741 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1742 continue;
1743
1744 /* Check too big values. */
1745 if ((unsigned int)c > 0xffff) {
1746 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
1747 return 0;
1748 continue;
1749 }
1750 }
1751
1752 /* Convert character. */
1753 if (c == '"') {
1754 len = 2;
1755 str = "\\\"";
1756 }
1757 else if (c == '\\') {
1758 len = 2;
1759 str = "\\\\";
1760 }
1761 else if (c == '/') {
1762 len = 2;
1763 str = "\\/";
1764 }
1765 else if (c == '\b') {
1766 len = 2;
1767 str = "\\b";
1768 }
1769 else if (c == '\f') {
1770 len = 2;
1771 str = "\\f";
1772 }
1773 else if (c == '\r') {
1774 len = 2;
1775 str = "\\r";
1776 }
1777 else if (c == '\n') {
1778 len = 2;
1779 str = "\\n";
1780 }
1781 else if (c == '\t') {
1782 len = 2;
1783 str = "\\t";
1784 }
1785 else if (c > 0xff || !isprint(c)) {
1786 /* isprint generate a segfault if c is too big. The man says that
1787 * c must have the value of an unsigned char or EOF.
1788 */
1789 len = 6;
1790 _str[0] = '\\';
1791 _str[1] = 'u';
1792 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
1793 str = _str;
1794 }
1795 else {
1796 len = 1;
1797 _str[0] = c;
1798 str = _str;
1799 }
1800
1801 /* Check length */
1802 if (temp->len + len > temp->size)
1803 return 0;
1804
1805 /* Copy string. */
1806 memcpy(temp->str + temp->len, str, len);
1807 temp->len += len;
1808 }
1809
1810 smp->flags &= ~SMP_F_CONST;
1811 smp->data.u.str = *temp;
1812 smp->data.type = SMP_T_STR;
1813
1814 return 1;
1815 }
1816
1817 /* This sample function is designed to extract some bytes from an input buffer.
1818 * First arg is the offset.
1819 * Optional second arg is the length to truncate */
sample_conv_bytes(const struct arg * arg_p,struct sample * smp,void * private)1820 static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
1821 {
1822 if (smp->data.u.str.len <= arg_p[0].data.sint) {
1823 smp->data.u.str.len = 0;
1824 return 1;
1825 }
1826
1827 if (smp->data.u.str.size)
1828 smp->data.u.str.size -= arg_p[0].data.sint;
1829 smp->data.u.str.len -= arg_p[0].data.sint;
1830 smp->data.u.str.str += arg_p[0].data.sint;
1831
1832 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.len))
1833 smp->data.u.str.len = arg_p[1].data.sint;
1834
1835 return 1;
1836 }
1837
sample_conv_field_check(struct arg * args,struct sample_conv * conv,const char * file,int line,char ** err)1838 static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
1839 const char *file, int line, char **err)
1840 {
1841 struct arg *arg = args;
1842
1843 if (!arg) {
1844 memprintf(err, "Unexpected empty arg list");
1845 return 0;
1846 }
1847
1848 if (arg->type != ARGT_SINT) {
1849 memprintf(err, "Unexpected arg type");
1850 return 0;
1851 }
1852
1853 if (!arg->data.sint) {
1854 memprintf(err, "Unexpected value 0 for index");
1855 return 0;
1856 }
1857
1858 arg++;
1859
1860 if (arg->type != ARGT_STR) {
1861 memprintf(err, "Unexpected arg type");
1862 return 0;
1863 }
1864
1865 if (!arg->data.str.len) {
1866 memprintf(err, "Empty separators list");
1867 return 0;
1868 }
1869
1870 return 1;
1871 }
1872
1873 /* This sample function is designed to a return selected part of a string (field).
1874 * First arg is the index of the field (start at 1)
1875 * Second arg is a char list of separators (type string)
1876 */
sample_conv_field(const struct arg * arg_p,struct sample * smp,void * private)1877 static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
1878 {
1879 unsigned int field;
1880 char *start, *end;
1881 int i;
1882
1883 if (!arg_p[0].data.sint)
1884 return 0;
1885
1886 field = 1;
1887 end = start = smp->data.u.str.str;
1888 while (end - smp->data.u.str.str < smp->data.u.str.len) {
1889
1890 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1891 if (*end == arg_p[1].data.str.str[i]) {
1892 if (field == arg_p[0].data.sint)
1893 goto found;
1894 start = end+1;
1895 field++;
1896 break;
1897 }
1898 }
1899 end++;
1900 }
1901
1902 /* Field not found */
1903 if (field != arg_p[0].data.sint) {
1904 smp->data.u.str.len = 0;
1905 return 0;
1906 }
1907 found:
1908 smp->data.u.str.len = end - start;
1909 /* If ret string is len 0, no need to
1910 change pointers or to update size */
1911 if (!smp->data.u.str.len)
1912 return 1;
1913
1914 smp->data.u.str.str = start;
1915
1916 /* Compute remaining size if needed
1917 Note: smp->data.u.str.size cannot be set to 0 */
1918 if (smp->data.u.str.size)
1919 smp->data.u.str.size -= start - smp->data.u.str.str;
1920
1921 return 1;
1922 }
1923
1924 /* This sample function is designed to return a word from a string.
1925 * First arg is the index of the word (start at 1)
1926 * Second arg is a char list of words separators (type string)
1927 */
sample_conv_word(const struct arg * arg_p,struct sample * smp,void * private)1928 static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
1929 {
1930 unsigned int word;
1931 char *start, *end;
1932 int i, issep, inword;
1933
1934 if (!arg_p[0].data.sint)
1935 return 0;
1936
1937 word = 0;
1938 inword = 0;
1939 end = start = smp->data.u.str.str;
1940 while (end - smp->data.u.str.str < smp->data.u.str.len) {
1941 issep = 0;
1942 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1943 if (*end == arg_p[1].data.str.str[i]) {
1944 issep = 1;
1945 break;
1946 }
1947 }
1948 if (!inword) {
1949 if (!issep) {
1950 word++;
1951 start = end;
1952 inword = 1;
1953 }
1954 }
1955 else if (issep) {
1956 if (word == arg_p[0].data.sint)
1957 goto found;
1958 inword = 0;
1959 }
1960 end++;
1961 }
1962
1963 /* Field not found */
1964 if (word != arg_p[0].data.sint) {
1965 smp->data.u.str.len = 0;
1966 return 1;
1967 }
1968 found:
1969 smp->data.u.str.len = end - start;
1970 /* If ret string is len 0, no need to
1971 change pointers or to update size */
1972 if (!smp->data.u.str.len)
1973 return 1;
1974
1975 smp->data.u.str.str = start;
1976
1977 /* Compute remaining size if needed
1978 Note: smp->data.u.str.size cannot be set to 0 */
1979 if (smp->data.u.str.size)
1980 smp->data.u.str.size -= start - smp->data.u.str.str;
1981
1982 return 1;
1983 }
1984
sample_conv_regsub_check(struct arg * args,struct sample_conv * conv,const char * file,int line,char ** err)1985 static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
1986 const char *file, int line, char **err)
1987 {
1988 struct arg *arg = args;
1989 char *p;
1990 int len;
1991
1992 /* arg0 is a regex, it uses type_flag for ICASE and global match */
1993 arg[0].type_flags = 0;
1994
1995 if (arg[2].type != ARGT_STR)
1996 return 1;
1997
1998 p = arg[2].data.str.str;
1999 len = arg[2].data.str.len;
2000 while (len) {
2001 if (*p == 'i') {
2002 arg[0].type_flags |= ARGF_REG_ICASE;
2003 }
2004 else if (*p == 'g') {
2005 arg[0].type_flags |= ARGF_REG_GLOB;
2006 }
2007 else {
2008 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2009 return 0;
2010 }
2011 p++;
2012 len--;
2013 }
2014 return 1;
2015 }
2016
2017 /* This sample function is designed to do the equivalent of s/match/replace/ on
2018 * the input string. It applies a regex and restarts from the last matched
2019 * location until nothing matches anymore. First arg is the regex to apply to
2020 * the input string, second arg is the replacement expression.
2021 */
sample_conv_regsub(const struct arg * arg_p,struct sample * smp,void * private)2022 static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
2023 {
2024 char *start, *end;
2025 struct my_regex *reg = arg_p[0].data.reg;
2026 regmatch_t pmatch[MAX_MATCH];
2027 struct chunk *trash = get_trash_chunk();
2028 int flag, max;
2029 int found;
2030
2031 start = smp->data.u.str.str;
2032 end = start + smp->data.u.str.len;
2033
2034 flag = 0;
2035 while (1) {
2036 /* check for last round which is used to copy remaining parts
2037 * when not running in global replacement mode.
2038 */
2039 found = 0;
2040 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2041 /* Note: we can have start == end on empty strings or at the end */
2042 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2043 }
2044
2045 if (!found)
2046 pmatch[0].rm_so = end - start;
2047
2048 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
2049 max = trash->size - trash->len;
2050 if (max && pmatch[0].rm_so > 0) {
2051 if (max > pmatch[0].rm_so)
2052 max = pmatch[0].rm_so;
2053 memcpy(trash->str + trash->len, start, max);
2054 trash->len += max;
2055 }
2056
2057 if (!found)
2058 break;
2059
2060 /* replace the matching part */
2061 max = trash->size - trash->len;
2062 if (max) {
2063 if (max > arg_p[1].data.str.len)
2064 max = arg_p[1].data.str.len;
2065 memcpy(trash->str + trash->len, arg_p[1].data.str.str, max);
2066 trash->len += max;
2067 }
2068
2069 /* stop here if we're done with this string */
2070 if (start >= end)
2071 break;
2072
2073 /* We have a special case for matches of length 0 (eg: "x*y*").
2074 * These ones are considered to match in front of a character,
2075 * so we have to copy that character and skip to the next one.
2076 */
2077 if (!pmatch[0].rm_eo) {
2078 if (trash->len < trash->size)
2079 trash->str[trash->len++] = start[pmatch[0].rm_eo];
2080 pmatch[0].rm_eo++;
2081 }
2082
2083 start += pmatch[0].rm_eo;
2084 flag |= REG_NOTBOL;
2085 }
2086
2087 smp->data.u.str = *trash;
2088 return 1;
2089 }
2090
2091 /* This function check an operator entry. It expects a string.
2092 * The string can be an integer or a variable name.
2093 */
check_operator(struct arg * args,struct sample_conv * conv,const char * file,int line,char ** err)2094 static int check_operator(struct arg *args, struct sample_conv *conv,
2095 const char *file, int line, char **err)
2096 {
2097 const char *str;
2098 const char *end;
2099
2100 /* Try to decode a variable. */
2101 if (vars_check_arg(&args[0], NULL))
2102 return 1;
2103
2104 /* Try to convert an integer */
2105 str = args[0].data.str.str;
2106 end = str + strlen(str);
2107 args[0].data.sint = read_int64(&str, end);
2108 if (*str != '\0') {
2109 memprintf(err, "expects an integer or a variable name");
2110 return 0;
2111 }
2112 args[0].type = ARGT_SINT;
2113 return 1;
2114 }
2115
2116 /* This function returns a sample struct filled with an arg content.
2117 * If the arg contain an integer, the integer is returned in the
2118 * sample. If the arg contains a variable descriptor, it returns the
2119 * variable value.
2120 *
2121 * This function returns 0 if an error occurs, otherwise it returns 1.
2122 */
sample_conv_var2smp(const struct arg * arg,struct sample * smp)2123 static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
2124 {
2125 switch (arg->type) {
2126 case ARGT_SINT:
2127 smp->data.type = SMP_T_SINT;
2128 smp->data.u.sint = arg->data.sint;
2129 return 1;
2130 case ARGT_VAR:
2131 if (!vars_get_by_desc(&arg->data.var, smp))
2132 return 0;
2133 if (!sample_casts[smp->data.type][SMP_T_SINT])
2134 return 0;
2135 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
2136 return 0;
2137 return 1;
2138 default:
2139 return 0;
2140 }
2141 }
2142
2143 /* Takes a SINT on input, applies a binary twos complement and returns the SINT
2144 * result.
2145 */
sample_conv_binary_cpl(const struct arg * arg_p,struct sample * smp,void * private)2146 static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
2147 {
2148 smp->data.u.sint = ~smp->data.u.sint;
2149 return 1;
2150 }
2151
2152 /* Takes a SINT on input, applies a binary "and" with the SINT directly in
2153 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2154 */
sample_conv_binary_and(const struct arg * arg_p,struct sample * smp,void * private)2155 static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
2156 {
2157 struct sample tmp;
2158
2159 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2160 if (!sample_conv_var2smp(arg_p, &tmp))
2161 return 0;
2162 smp->data.u.sint &= tmp.data.u.sint;
2163 return 1;
2164 }
2165
2166 /* Takes a SINT on input, applies a binary "or" with the SINT directly in
2167 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2168 */
sample_conv_binary_or(const struct arg * arg_p,struct sample * smp,void * private)2169 static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
2170 {
2171 struct sample tmp;
2172
2173 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2174 if (!sample_conv_var2smp(arg_p, &tmp))
2175 return 0;
2176 smp->data.u.sint |= tmp.data.u.sint;
2177 return 1;
2178 }
2179
2180 /* Takes a SINT on input, applies a binary "xor" with the SINT directly in
2181 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2182 */
sample_conv_binary_xor(const struct arg * arg_p,struct sample * smp,void * private)2183 static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
2184 {
2185 struct sample tmp;
2186
2187 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2188 if (!sample_conv_var2smp(arg_p, &tmp))
2189 return 0;
2190 smp->data.u.sint ^= tmp.data.u.sint;
2191 return 1;
2192 }
2193
arith_add(long long int a,long long int b)2194 static inline long long int arith_add(long long int a, long long int b)
2195 {
2196 /* Prevent overflow and makes capped calculus.
2197 * We must ensure that the check calculus doesn't
2198 * exceed the signed 64 bits limits.
2199 *
2200 * +----------+----------+
2201 * | a<0 | a>=0 |
2202 * +------+----------+----------+
2203 * | b<0 | MIN-a>b | no check |
2204 * +------+----------+----------+
2205 * | b>=0 | no check | MAX-a<b |
2206 * +------+----------+----------+
2207 */
2208 if ((a ^ b) >= 0) {
2209 /* signs are differents. */
2210 if (a < 0) {
2211 if (LLONG_MIN - a > b)
2212 return LLONG_MIN;
2213 }
2214 if (LLONG_MAX - a < b)
2215 return LLONG_MAX;
2216 }
2217 return a + b;
2218 }
2219
2220 /* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
2221 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2222 */
sample_conv_arith_add(const struct arg * arg_p,struct sample * smp,void * private)2223 static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
2224 {
2225 struct sample tmp;
2226
2227 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2228 if (!sample_conv_var2smp(arg_p, &tmp))
2229 return 0;
2230 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
2231 return 1;
2232 }
2233
2234 /* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
2235 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2236 */
sample_conv_arith_sub(const struct arg * arg_p,struct sample * smp,void * private)2237 static int sample_conv_arith_sub(const struct arg *arg_p,
2238 struct sample *smp, void *private)
2239 {
2240 struct sample tmp;
2241
2242 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2243 if (!sample_conv_var2smp(arg_p, &tmp))
2244 return 0;
2245
2246 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2247 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2248 * of -LLONG_MIN and correct the result.
2249 */
2250 if (tmp.data.u.sint == LLONG_MIN) {
2251 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2252 if (smp->data.u.sint < LLONG_MAX)
2253 smp->data.u.sint++;
2254 return 1;
2255 }
2256
2257 /* standard substraction: we use the "add" function and negate
2258 * the second operand.
2259 */
2260 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
2261 return 1;
2262 }
2263
2264 /* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
2265 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2266 * If the result makes an overflow, then the largest possible quantity is
2267 * returned.
2268 */
sample_conv_arith_mul(const struct arg * arg_p,struct sample * smp,void * private)2269 static int sample_conv_arith_mul(const struct arg *arg_p,
2270 struct sample *smp, void *private)
2271 {
2272 struct sample tmp;
2273 long long int c;
2274
2275 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2276 if (!sample_conv_var2smp(arg_p, &tmp))
2277 return 0;
2278
2279 /* prevent divide by 0 during the check */
2280 if (!smp->data.u.sint || !tmp.data.u.sint) {
2281 smp->data.u.sint = 0;
2282 return 1;
2283 }
2284
2285 /* The multiply between LLONG_MIN and -1 returns a
2286 * "floting point exception".
2287 */
2288 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2289 smp->data.u.sint = LLONG_MAX;
2290 return 1;
2291 }
2292
2293 /* execute standard multiplication. */
2294 c = smp->data.u.sint * tmp.data.u.sint;
2295
2296 /* check for overflow and makes capped multiply. */
2297 if (smp->data.u.sint != c / tmp.data.u.sint) {
2298 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2299 smp->data.u.sint = LLONG_MAX;
2300 return 1;
2301 }
2302 smp->data.u.sint = LLONG_MIN;
2303 return 1;
2304 }
2305 smp->data.u.sint = c;
2306 return 1;
2307 }
2308
2309 /* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
2310 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2311 * If arg_p makes the result overflow, then the largest possible quantity is
2312 * returned.
2313 */
sample_conv_arith_div(const struct arg * arg_p,struct sample * smp,void * private)2314 static int sample_conv_arith_div(const struct arg *arg_p,
2315 struct sample *smp, void *private)
2316 {
2317 struct sample tmp;
2318
2319 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2320 if (!sample_conv_var2smp(arg_p, &tmp))
2321 return 0;
2322
2323 if (tmp.data.u.sint) {
2324 /* The divide between LLONG_MIN and -1 returns a
2325 * "floting point exception".
2326 */
2327 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2328 smp->data.u.sint = LLONG_MAX;
2329 return 1;
2330 }
2331 smp->data.u.sint /= tmp.data.u.sint;
2332 return 1;
2333 }
2334 smp->data.u.sint = LLONG_MAX;
2335 return 1;
2336 }
2337
2338 /* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
2339 * arg_p or in the varaible described in arg_p, and returns the SINT result.
2340 * If arg_p makes the result overflow, then 0 is returned.
2341 */
sample_conv_arith_mod(const struct arg * arg_p,struct sample * smp,void * private)2342 static int sample_conv_arith_mod(const struct arg *arg_p,
2343 struct sample *smp, void *private)
2344 {
2345 struct sample tmp;
2346
2347 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2348 if (!sample_conv_var2smp(arg_p, &tmp))
2349 return 0;
2350
2351 if (tmp.data.u.sint) {
2352 /* The divide between LLONG_MIN and -1 returns a
2353 * "floting point exception".
2354 */
2355 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2356 smp->data.u.sint = 0;
2357 return 1;
2358 }
2359 smp->data.u.sint %= tmp.data.u.sint;
2360 return 1;
2361 }
2362 smp->data.u.sint = 0;
2363 return 1;
2364 }
2365
2366 /* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
2367 * result.
2368 */
sample_conv_arith_neg(const struct arg * arg_p,struct sample * smp,void * private)2369 static int sample_conv_arith_neg(const struct arg *arg_p,
2370 struct sample *smp, void *private)
2371 {
2372 if (smp->data.u.sint == LLONG_MIN)
2373 smp->data.u.sint = LLONG_MAX;
2374 else
2375 smp->data.u.sint = -smp->data.u.sint;
2376 return 1;
2377 }
2378
2379 /* Takes a SINT on input, returns true is the value is non-null, otherwise
2380 * false. The output is a BOOL.
2381 */
sample_conv_arith_bool(const struct arg * arg_p,struct sample * smp,void * private)2382 static int sample_conv_arith_bool(const struct arg *arg_p,
2383 struct sample *smp, void *private)
2384 {
2385 smp->data.u.sint = !!smp->data.u.sint;
2386 smp->data.type = SMP_T_BOOL;
2387 return 1;
2388 }
2389
2390 /* Takes a SINT on input, returns false is the value is non-null, otherwise
2391 * truee. The output is a BOOL.
2392 */
sample_conv_arith_not(const struct arg * arg_p,struct sample * smp,void * private)2393 static int sample_conv_arith_not(const struct arg *arg_p,
2394 struct sample *smp, void *private)
2395 {
2396 smp->data.u.sint = !smp->data.u.sint;
2397 smp->data.type = SMP_T_BOOL;
2398 return 1;
2399 }
2400
2401 /* Takes a SINT on input, returns true is the value is odd, otherwise false.
2402 * The output is a BOOL.
2403 */
sample_conv_arith_odd(const struct arg * arg_p,struct sample * smp,void * private)2404 static int sample_conv_arith_odd(const struct arg *arg_p,
2405 struct sample *smp, void *private)
2406 {
2407 smp->data.u.sint = smp->data.u.sint & 1;
2408 smp->data.type = SMP_T_BOOL;
2409 return 1;
2410 }
2411
2412 /* Takes a SINT on input, returns true is the value is even, otherwise false.
2413 * The output is a BOOL.
2414 */
sample_conv_arith_even(const struct arg * arg_p,struct sample * smp,void * private)2415 static int sample_conv_arith_even(const struct arg *arg_p,
2416 struct sample *smp, void *private)
2417 {
2418 smp->data.u.sint = !(smp->data.u.sint & 1);
2419 smp->data.type = SMP_T_BOOL;
2420 return 1;
2421 }
2422
2423 /************************************************************************/
2424 /* All supported sample fetch functions must be declared here */
2425 /************************************************************************/
2426
2427 /* force TRUE to be returned at the fetch level */
2428 static int
smp_fetch_true(const struct arg * args,struct sample * smp,const char * kw,void * private)2429 smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
2430 {
2431 smp->data.type = SMP_T_BOOL;
2432 smp->data.u.sint = 1;
2433 return 1;
2434 }
2435
2436 /* force FALSE to be returned at the fetch level */
2437 static int
smp_fetch_false(const struct arg * args,struct sample * smp,const char * kw,void * private)2438 smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
2439 {
2440 smp->data.type = SMP_T_BOOL;
2441 smp->data.u.sint = 0;
2442 return 1;
2443 }
2444
2445 /* retrieve environment variable $1 as a string */
2446 static int
smp_fetch_env(const struct arg * args,struct sample * smp,const char * kw,void * private)2447 smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
2448 {
2449 char *env;
2450
2451 if (!args || args[0].type != ARGT_STR)
2452 return 0;
2453
2454 env = getenv(args[0].data.str.str);
2455 if (!env)
2456 return 0;
2457
2458 smp->data.type = SMP_T_STR;
2459 smp->flags = SMP_F_CONST;
2460 smp->data.u.str.str = env;
2461 smp->data.u.str.len = strlen(env);
2462 return 1;
2463 }
2464
2465 /* retrieve the current local date in epoch time, and applies an optional offset
2466 * of args[0] seconds.
2467 */
2468 static int
smp_fetch_date(const struct arg * args,struct sample * smp,const char * kw,void * private)2469 smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
2470 {
2471 smp->data.u.sint = date.tv_sec;
2472
2473 /* add offset */
2474 if (args && args[0].type == ARGT_SINT)
2475 smp->data.u.sint += args[0].data.sint;
2476
2477 smp->data.type = SMP_T_SINT;
2478 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2479 return 1;
2480 }
2481
2482 /* returns the number of processes */
2483 static int
smp_fetch_nbproc(const struct arg * args,struct sample * smp,const char * kw,void * private)2484 smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
2485 {
2486 smp->data.type = SMP_T_SINT;
2487 smp->data.u.sint = global.nbproc;
2488 return 1;
2489 }
2490
2491 /* returns the number of the current process (between 1 and nbproc */
2492 static int
smp_fetch_proc(const struct arg * args,struct sample * smp,const char * kw,void * private)2493 smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
2494 {
2495 smp->data.type = SMP_T_SINT;
2496 smp->data.u.sint = relative_pid;
2497 return 1;
2498 }
2499
2500 /* generate a random 32-bit integer for whatever purpose, with an optional
2501 * range specified in argument.
2502 */
2503 static int
smp_fetch_rand(const struct arg * args,struct sample * smp,const char * kw,void * private)2504 smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
2505 {
2506 smp->data.u.sint = random();
2507
2508 /* reduce if needed. Don't do a modulo, use all bits! */
2509 if (args && args[0].type == ARGT_SINT)
2510 smp->data.u.sint = (smp->data.u.sint * args[0].data.sint) / ((u64)RAND_MAX+1);
2511
2512 smp->data.type = SMP_T_SINT;
2513 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2514 return 1;
2515 }
2516
2517 /* returns true if the current process is stopping */
2518 static int
smp_fetch_stopping(const struct arg * args,struct sample * smp,const char * kw,void * private)2519 smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
2520 {
2521 smp->data.type = SMP_T_BOOL;
2522 smp->data.u.sint = stopping;
2523 return 1;
2524 }
2525
smp_fetch_const_str(const struct arg * args,struct sample * smp,const char * kw,void * private)2526 static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
2527 {
2528 smp->flags |= SMP_F_CONST;
2529 smp->data.type = SMP_T_STR;
2530 smp->data.u.str.str = args[0].data.str.str;
2531 smp->data.u.str.len = args[0].data.str.len;
2532 return 1;
2533 }
2534
smp_check_const_bool(struct arg * args,char ** err)2535 static int smp_check_const_bool(struct arg *args, char **err)
2536 {
2537 if (strcasecmp(args[0].data.str.str, "true") == 0 ||
2538 strcasecmp(args[0].data.str.str, "1") == 0) {
2539 args[0].type = ARGT_SINT;
2540 args[0].data.sint = 1;
2541 return 1;
2542 }
2543 if (strcasecmp(args[0].data.str.str, "false") == 0 ||
2544 strcasecmp(args[0].data.str.str, "0") == 0) {
2545 args[0].type = ARGT_SINT;
2546 args[0].data.sint = 0;
2547 return 1;
2548 }
2549 memprintf(err, "Expects 'true', 'false', '0' or '1'");
2550 return 0;
2551 }
2552
smp_fetch_const_bool(const struct arg * args,struct sample * smp,const char * kw,void * private)2553 static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
2554 {
2555 smp->data.type = SMP_T_BOOL;
2556 smp->data.u.sint = args[0].data.sint;
2557 return 1;
2558 }
2559
smp_fetch_const_int(const struct arg * args,struct sample * smp,const char * kw,void * private)2560 static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
2561 {
2562 smp->data.type = SMP_T_SINT;
2563 smp->data.u.sint = args[0].data.sint;
2564 return 1;
2565 }
2566
smp_fetch_const_ipv4(const struct arg * args,struct sample * smp,const char * kw,void * private)2567 static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
2568 {
2569 smp->data.type = SMP_T_IPV4;
2570 smp->data.u.ipv4 = args[0].data.ipv4;
2571 return 1;
2572 }
2573
smp_fetch_const_ipv6(const struct arg * args,struct sample * smp,const char * kw,void * private)2574 static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
2575 {
2576 smp->data.type = SMP_T_IPV6;
2577 smp->data.u.ipv6 = args[0].data.ipv6;
2578 return 1;
2579 }
2580
smp_check_const_bin(struct arg * args,char ** err)2581 static int smp_check_const_bin(struct arg *args, char **err)
2582 {
2583 char *binstr = NULL;
2584 int binstrlen;
2585
2586 if (!parse_binary(args[0].data.str.str, &binstr, &binstrlen, err))
2587 return 0;
2588 args[0].type = ARGT_STR;
2589 args[0].data.str.str = binstr;
2590 args[0].data.str.len = binstrlen;
2591 return 1;
2592 }
2593
smp_fetch_const_bin(const struct arg * args,struct sample * smp,const char * kw,void * private)2594 static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
2595 {
2596 smp->flags |= SMP_F_CONST;
2597 smp->data.type = SMP_T_BIN;
2598 smp->data.u.str.str = args[0].data.str.str;
2599 smp->data.u.str.len = args[0].data.str.len;
2600 return 1;
2601 }
2602
smp_check_const_meth(struct arg * args,char ** err)2603 static int smp_check_const_meth(struct arg *args, char **err)
2604 {
2605 enum http_meth_t meth;
2606 int i;
2607
2608 meth = find_http_meth(args[0].data.str.str, args[0].data.str.len);
2609 if (meth != HTTP_METH_OTHER) {
2610 args[0].type = ARGT_SINT;
2611 args[0].data.sint = meth;
2612 } else {
2613 /* Check method avalaibility. A methos is a token defined as :
2614 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
2615 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
2616 * token = 1*tchar
2617 */
2618 for (i = 0; i < args[0].data.str.len; i++) {
2619 if (!HTTP_IS_TOKEN(args[0].data.str.str[i])) {
2620 memprintf(err, "expects valid method.");
2621 return 0;
2622 }
2623 }
2624 }
2625 return 1;
2626 }
2627
smp_fetch_const_meth(const struct arg * args,struct sample * smp,const char * kw,void * private)2628 static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
2629 {
2630 smp->data.type = SMP_T_METH;
2631 if (args[0].type == ARGT_SINT) {
2632 smp->flags &= ~SMP_F_CONST;
2633 smp->data.u.meth.meth = args[0].data.sint;
2634 smp->data.u.meth.str.str = "";
2635 smp->data.u.meth.str.len = 0;
2636 } else {
2637 smp->flags |= SMP_F_CONST;
2638 smp->data.u.meth.meth = HTTP_METH_OTHER;
2639 smp->data.u.meth.str.str = args[0].data.str.str;
2640 smp->data.u.meth.str.len = args[0].data.str.len;
2641 }
2642 return 1;
2643 }
2644
2645 /* Note: must not be declared <const> as its list will be overwritten.
2646 * Note: fetches that may return multiple types must be declared as the lowest
2647 * common denominator, the type that can be casted into all other ones. For
2648 * instance IPv4/IPv6 must be declared IPv4.
2649 */
2650 static struct sample_fetch_kw_list smp_kws = {ILH, {
2651 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
2652 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
2653 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
2654 { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
2655 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
2656 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
2657 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
2658 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
2659
2660 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
2661 { "bool", smp_fetch_const_bool, ARG1(1,STR), smp_check_const_bool, SMP_T_BOOL, SMP_USE_INTRN },
2662 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
2663 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
2664 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
2665 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
2666 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
2667
2668 { /* END */ },
2669 }};
2670
2671 /* Note: must not be declared <const> as its list will be overwritten */
2672 static struct sample_conv_kw_list sample_conv_kws = {ILH, {
2673 #ifdef DEBUG_EXPR
2674 { "debug", sample_conv_debug, 0, NULL, SMP_T_ANY, SMP_T_ANY },
2675 #endif
2676
2677 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
2678 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
2679 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
2680 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
2681 { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 },
2682 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
2683 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
2684 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2685 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2686 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2687 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
2688 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
2689 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
2690 { "field", sample_conv_field, ARG2(2,SINT,STR), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
2691 { "word", sample_conv_word, ARG2(2,SINT,STR), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
2692 { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
2693
2694 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2695 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2696 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2697 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
2698 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2699 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2700 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2701 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
2702 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2703 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2704 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2705 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2706 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
2707 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
2708
2709 { NULL, NULL, 0, 0, 0 },
2710 }};
2711
2712 __attribute__((constructor))
__sample_init(void)2713 static void __sample_init(void)
2714 {
2715 /* register sample fetch and format conversion keywords */
2716 sample_register_fetches(&smp_kws);
2717 sample_register_convs(&sample_conv_kws);
2718 }
2719