blob: 34f4c1d00c8ce0d30b01439eec75d1cb037296f5 [file] [log] [blame]
Emeric Brun107ca302010-01-04 16:16:05 +01001/*
Willy Tarreaucd3b0942012-04-27 21:52:18 +02002 * Sample management functions.
Emeric Brun107ca302010-01-04 16:16:05 +01003 *
4 * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreaucd3b0942012-04-27 21:52:18 +02005 * Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
Emeric Brun107ca302010-01-04 16:16:05 +01006 *
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 <string.h>
15#include <arpa/inet.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020016#include <stdio.h>
Emeric Brun107ca302010-01-04 16:16:05 +010017
Willy Tarreau7e2c6472012-10-29 20:44:36 +010018#include <types/global.h>
19
Willy Tarreauc7e42382012-08-24 19:22:53 +020020#include <common/chunk.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020021#include <common/standard.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020022#include <common/uri_auth.h>
Emeric Brun53d1a982014-04-30 18:21:37 +020023#include <common/base64.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020024
Willy Tarreau9fcb9842012-04-20 14:45:49 +020025#include <proto/arg.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020026#include <proto/auth.h>
27#include <proto/log.h>
Thierry FOURNIERd4373142013-12-17 01:10:10 +010028#include <proto/proto_http.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020029#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020030#include <proto/sample.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020031#include <proto/stick_table.h>
Emeric Brun107ca302010-01-04 16:16:05 +010032
Willy Tarreau1cf8f082014-02-07 12:14:54 +010033/* sample type names */
34const char *smp_to_type[SMP_TYPES] = {
35 [SMP_T_BOOL] = "bool",
36 [SMP_T_UINT] = "uint",
37 [SMP_T_SINT] = "sint",
38 [SMP_T_ADDR] = "addr",
39 [SMP_T_IPV4] = "ipv4",
40 [SMP_T_IPV6] = "ipv6",
41 [SMP_T_STR] = "str",
42 [SMP_T_BIN] = "bin",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010043};
44
Willy Tarreau12785782012-04-27 21:37:17 +020045/* static sample used in sample_process() when <p> is NULL */
Willy Tarreaub4a88f02012-04-23 21:35:11 +020046static struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010047
Willy Tarreau12785782012-04-27 21:37:17 +020048/* list head of all known sample fetch keywords */
49static struct sample_fetch_kw_list sample_fetches = {
50 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010051};
52
Willy Tarreau12785782012-04-27 21:37:17 +020053/* list head of all known sample format conversion keywords */
54static struct sample_conv_kw_list sample_convs = {
55 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010056};
57
Willy Tarreau80aca902013-01-07 15:42:20 +010058const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
59 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
60 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
61 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
62 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
63 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
64 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
65 SMP_VAL_FE_LOG_END),
66
67 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
68 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
69 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
70 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
71 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
72 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
73 SMP_VAL_FE_LOG_END),
74
75 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
76 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
77 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
78 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
79 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
80 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
81 SMP_VAL_FE_LOG_END),
82
83 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
84 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
85 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
86 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
87 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
88 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
89 SMP_VAL_FE_LOG_END),
90
91 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
92 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
93 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
94 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
95 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
96 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
97 SMP_VAL_FE_LOG_END),
98
99 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
100 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
101 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
102 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
103 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
104 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
105 SMP_VAL_FE_LOG_END),
106
107 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
108 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
109 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
110 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
111 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
112 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
113 SMP_VAL___________),
114
115 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
116 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
117 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
118 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
119 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
120 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
121 SMP_VAL___________),
122
123 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
124 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
125 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
126 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
127 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
128 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
129 SMP_VAL_FE_LOG_END),
130
131 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
132 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
133 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
134 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
135 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
136 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
137 SMP_VAL___________),
138
139 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
140 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
141 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
142 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
143 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
144 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
145 SMP_VAL_FE_LOG_END),
146
147 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
148 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
149 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
150 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
151 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
152 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
153 SMP_VAL_FE_LOG_END),
154
155 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
156 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
157 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
158 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
159 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
160 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
161 SMP_VAL_FE_LOG_END),
162
163 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
164 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
165 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
166 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
167 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
168 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
169 SMP_VAL_FE_LOG_END),
170
171 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
172 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
173 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
174 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
175 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
176 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
177 SMP_VAL___________),
178
179 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
180 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
181 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
182 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
183 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
184 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
185 SMP_VAL___________),
186
187 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
188 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
189 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
190 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
191 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
192 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
193 SMP_VAL_FE_LOG_END),
194
195 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
196 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
197 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
198 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
199 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
200 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
201 SMP_VAL___________),
202
203 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
204 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
205 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
206 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
207 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
208 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
209 SMP_VAL_FE_LOG_END),
210
211 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
212 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
213 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
214 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
215 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
216 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
217 SMP_VAL_FE_LOG_END),
218
219 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
220 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
221 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
222 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
223 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
224 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
225 SMP_VAL_FE_LOG_END),
226
227 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
228 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
229 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
230 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
231 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
232 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
233 SMP_VAL_FE_LOG_END),
234};
235
236static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
237 [SMP_SRC_INTRN] = "internal state",
238 [SMP_SRC_LISTN] = "listener",
239 [SMP_SRC_FTEND] = "frontend",
240 [SMP_SRC_L4CLI] = "client address",
241 [SMP_SRC_L5CLI] = "client-side connection",
242 [SMP_SRC_TRACK] = "track counters",
243 [SMP_SRC_L6REQ] = "request buffer",
244 [SMP_SRC_HRQHV] = "HTTP request headers",
245 [SMP_SRC_HRQHP] = "HTTP request",
246 [SMP_SRC_HRQBO] = "HTTP request body",
247 [SMP_SRC_BKEND] = "backend",
248 [SMP_SRC_SERVR] = "server",
249 [SMP_SRC_L4SRV] = "server address",
250 [SMP_SRC_L5SRV] = "server-side connection",
251 [SMP_SRC_L6RES] = "response buffer",
252 [SMP_SRC_HRSHV] = "HTTP response headers",
253 [SMP_SRC_HRSHP] = "HTTP response",
254 [SMP_SRC_HRSBO] = "HTTP response body",
255 [SMP_SRC_RQFIN] = "request buffer statistics",
256 [SMP_SRC_RSFIN] = "response buffer statistics",
257 [SMP_SRC_TXFIN] = "transaction statistics",
258 [SMP_SRC_SSFIN] = "session statistics",
259};
260
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100261static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
262 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
263 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
264 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
265 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
266 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
267 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
268 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
269 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
270 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
271 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
272 [SMP_CKP_BE_SRV_CON] = "server source selection",
273 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
274 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
275 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
276 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
277 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
278 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
279 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
280 [SMP_CKP_FE_LOG_END] = "logs",
281};
282
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100283/* This function returns the type of the data returned by the sample_expr.
284 * It assumes that the <expr> and all of its converters are properly
285 * initialized.
286 */
287inline
288int smp_expr_output_type(struct sample_expr *expr)
289{
290 struct sample_conv_expr *smp_expr;
291
292 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
293 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
294 return smp_expr->conv->out_type;
295 }
296 return expr->fetch->out_type;
297}
298
299
Willy Tarreau80aca902013-01-07 15:42:20 +0100300/* fill the trash with a comma-delimited list of source names for the <use> bit
301 * field which must be composed of a non-null set of SMP_USE_* flags. The return
302 * value is the pointer to the string in the trash buffer.
303 */
304const char *sample_src_names(unsigned int use)
305{
306 int bit;
307
308 trash.len = 0;
309 trash.str[0] = '\0';
310 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
311 if (!(use & ~((1 << bit) - 1)))
312 break; /* no more bits */
313
314 if (!(use & (1 << bit)))
315 continue; /* bit not set */
316
317 trash.len += snprintf(trash.str + trash.len, trash.size - trash.len, "%s%s",
318 (use & ((1 << bit) - 1)) ? "," : "",
319 fetch_src_names[bit]);
320 }
321 return trash.str;
322}
323
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100324/* return a pointer to the correct sample checkpoint name, or "unknown" when
325 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
326 * if set.
327 */
328const char *sample_ckp_names(unsigned int use)
329{
330 int bit;
331
332 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
333 if (use & (1 << bit))
334 return fetch_ckp_names[bit];
335 return "unknown sample check place, please report this bug";
336}
337
Emeric Brun107ca302010-01-04 16:16:05 +0100338/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100339 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
340 * for next parsing sessions. The fetch keywords capabilities are also computed
341 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100342 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100343void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100344{
Willy Tarreau80aca902013-01-07 15:42:20 +0100345 struct sample_fetch *sf;
346 int bit;
347
348 for (sf = kwl->kw; sf->kw != NULL; sf++) {
349 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
350 if (sf->use & (1 << bit))
351 sf->val |= fetch_cap[bit];
352 }
353 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100354}
355
356/*
Willy Tarreau12785782012-04-27 21:37:17 +0200357 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100358 * parsing sessions.
359 */
Willy Tarreau12785782012-04-27 21:37:17 +0200360void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100361{
Willy Tarreau12785782012-04-27 21:37:17 +0200362 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100363}
364
365/*
Willy Tarreau12785782012-04-27 21:37:17 +0200366 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100367 * string of <len> in buffer <kw>.
368 *
369 */
Willy Tarreau12785782012-04-27 21:37:17 +0200370struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100371{
372 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200373 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100374
Willy Tarreau12785782012-04-27 21:37:17 +0200375 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100376 for (index = 0; kwl->kw[index].kw != NULL; index++) {
377 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
378 kwl->kw[index].kw[len] == '\0')
379 return &kwl->kw[index];
380 }
381 }
382 return NULL;
383}
384
385/*
Willy Tarreau12785782012-04-27 21:37:17 +0200386 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100387 * string of <len> in buffer <kw>.
388 *
389 */
Willy Tarreau12785782012-04-27 21:37:17 +0200390struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100391{
392 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200393 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100394
Willy Tarreau12785782012-04-27 21:37:17 +0200395 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100396 for (index = 0; kwl->kw[index].kw != NULL; index++) {
397 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
398 kwl->kw[index].kw[len] == '\0')
399 return &kwl->kw[index];
400 }
401 }
402 return NULL;
403}
404
Emeric Brun107ca302010-01-04 16:16:05 +0100405/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200406/* Sample casts functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200407/* Note: these functions do *NOT* set the output type on the */
408/* sample, the caller is responsible for doing this on return. */
Emeric Brun107ca302010-01-04 16:16:05 +0100409/******************************************************************/
410
Willy Tarreau342acb42012-04-23 22:03:39 +0200411static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100412{
Willy Tarreau342acb42012-04-23 22:03:39 +0200413 smp->data.uint = ntohl(smp->data.ipv4.s_addr);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100414 smp->type = SMP_T_UINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100415 return 1;
416}
417
Willy Tarreau342acb42012-04-23 22:03:39 +0200418static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100419{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100420 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100421
Willy Tarreau342acb42012-04-23 22:03:39 +0200422 if (!inet_ntop(AF_INET, (void *)&smp->data.ipv4, trash->str, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100423 return 0;
424
425 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200426 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100427 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100428 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100429
430 return 1;
431}
432
Willy Tarreau342acb42012-04-23 22:03:39 +0200433static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100434{
Willy Tarreau342acb42012-04-23 22:03:39 +0200435 v4tov6(&smp->data.ipv6, &smp->data.ipv4);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100436 smp->type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100437 return 1;
438}
439
Willy Tarreau342acb42012-04-23 22:03:39 +0200440static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100441{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100442 struct chunk *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100443
Willy Tarreau342acb42012-04-23 22:03:39 +0200444 if (!inet_ntop(AF_INET6, (void *)&smp->data.ipv6, trash->str, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100445 return 0;
446
447 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200448 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100449 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100450 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100451 return 1;
452}
453
454/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200455static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100456{
Willy Tarreau342acb42012-04-23 22:03:39 +0200457 return v6tov4(&smp->data.ipv4, &smp->data.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100458}
459*/
460
Willy Tarreau342acb42012-04-23 22:03:39 +0200461static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100462{
Willy Tarreau342acb42012-04-23 22:03:39 +0200463 smp->data.ipv4.s_addr = htonl(smp->data.uint);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100464 smp->type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100465 return 1;
466}
467
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100468static int c_str2addr(struct sample *smp)
469{
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100470 if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4)) {
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100471 if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6))
472 return 0;
473 smp->type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100474 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100475 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100476 }
477 smp->type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100478 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100479 return 1;
480}
481
Willy Tarreau342acb42012-04-23 22:03:39 +0200482static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100483{
Willy Tarreau342acb42012-04-23 22:03:39 +0200484 if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100485 return 0;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100486 smp->type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100487 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100488 return 1;
489}
490
Willy Tarreau342acb42012-04-23 22:03:39 +0200491static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100492{
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100493 if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6))
494 return 0;
495 smp->type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100496 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100497 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100498}
499
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100500/* The sample is always copied into a new one so that smp->size is always
501 * valid. The NULL char always enforces the end of string if it is met.
502 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200503static int c_bin2str(struct sample *smp)
504{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100505 struct chunk *trash = get_trash_chunk();
Emeric Brun8ac33d92012-10-17 13:36:06 +0200506 unsigned char c;
507 int ptr = 0;
508
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100509 while (ptr < smp->data.str.len) {
510 c = smp->data.str.str[ptr];
511 if (!c)
512 break;
513 trash->str[ptr] = c;
514 ptr++;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200515 }
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100516 trash->len = ptr;
517 trash->str[ptr] = 0;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200518 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100519 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100520 smp->flags &= ~SMP_F_CONST;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200521 return 1;
522}
523
Willy Tarreau342acb42012-04-23 22:03:39 +0200524static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100525{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100526 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100527 char *pos;
528
Willy Tarreau342acb42012-04-23 22:03:39 +0200529 pos = ultoa_r(smp->data.uint, trash->str, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100530
531 if (!pos)
532 return 0;
533
Emeric Brun485479d2010-09-23 18:02:19 +0200534 trash->size = trash->size - (pos - trash->str);
Emeric Brun107ca302010-01-04 16:16:05 +0100535 trash->str = pos;
536 trash->len = strlen(pos);
Willy Tarreau342acb42012-04-23 22:03:39 +0200537 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100538 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100539 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100540 return 1;
541}
542
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100543/* This function duplicates data and removes the flag "const". */
544int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200545{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100546 struct chunk *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200547
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100548 /* If the const flag is not set, we don't need to duplicate the
549 * pattern as it can be modified in place.
550 */
551 if (!(smp->flags & SMP_F_CONST))
552 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100553
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100554 switch (smp->type) {
555 case SMP_T_BOOL:
556 case SMP_T_UINT:
557 case SMP_T_SINT:
558 case SMP_T_ADDR:
559 case SMP_T_IPV4:
560 case SMP_T_IPV6:
561 /* These type are not const. */
562 break;
563 case SMP_T_STR:
564 case SMP_T_BIN:
565 /* Duplicate data. */
566 trash = get_trash_chunk();
567 trash->len = smp->data.str.len < trash->size ? smp->data.str.len : trash->size;
568 memcpy(trash->str, smp->data.str.str, trash->len);
569 smp->data.str = *trash;
570 break;
571 default:
572 /* Other cases are unexpected. */
573 return 0;
574 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100575
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100576 /* remove const flag */
577 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200578 return 1;
579}
580
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100581int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100582{
583 return 1;
584}
585
Willy Tarreau342acb42012-04-23 22:03:39 +0200586static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100587{
588 int i;
589 uint32_t ret = 0;
590
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100591 if (smp->data.str.len == 0)
592 return 0;
593
Willy Tarreau342acb42012-04-23 22:03:39 +0200594 for (i = 0; i < smp->data.str.len; i++) {
595 uint32_t val = smp->data.str.str[i] - '0';
Emeric Brun107ca302010-01-04 16:16:05 +0100596
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100597 if (val > 9) {
598 if (i == 0)
599 return 0;
Emeric Brun107ca302010-01-04 16:16:05 +0100600 break;
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100601 }
Emeric Brun107ca302010-01-04 16:16:05 +0100602
603 ret = ret * 10 + val;
604 }
605
Willy Tarreau342acb42012-04-23 22:03:39 +0200606 smp->data.uint = ret;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100607 smp->type = SMP_T_UINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100608 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100609 return 1;
610}
611
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100612static int c_str2meth(struct sample *smp)
613{
614 enum http_meth_t meth;
615 int len;
616
617 meth = find_http_meth(smp->data.str.str, smp->data.str.len);
618 if (meth == HTTP_METH_OTHER) {
619 len = smp->data.str.len;
620 smp->data.meth.str.str = smp->data.str.str;
621 smp->data.meth.str.len = len;
622 }
623 else
624 smp->flags &= ~SMP_F_CONST;
625 smp->data.meth.meth = meth;
626 smp->type = SMP_T_METH;
627 return 1;
628}
629
630static int c_meth2str(struct sample *smp)
631{
632 int len;
633 enum http_meth_t meth;
634
635 if (smp->data.meth.meth == HTTP_METH_OTHER) {
636 /* The method is unknown. Copy the original pointer. */
637 len = smp->data.meth.str.len;
638 smp->data.str.str = smp->data.meth.str.str;
639 smp->data.str.len = len;
640 smp->type = SMP_T_STR;
641 }
642 else if (smp->data.meth.meth < HTTP_METH_OTHER) {
643 /* The method is known, copy the pointer containing the string. */
644 meth = smp->data.meth.meth;
645 smp->data.str.str = http_known_methods[meth].name;
646 smp->data.str.len = http_known_methods[meth].len;
647 smp->flags |= SMP_F_CONST;
648 smp->type = SMP_T_STR;
649 }
650 else {
651 /* Unknown method */
652 return 0;
653 }
654 return 1;
655}
656
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200657static int c_addr2bin(struct sample *smp)
658{
659 struct chunk *chk = get_trash_chunk();
660
661 if (smp->type == SMP_T_IPV4) {
662 chk->len = 4;
663 memcpy(chk->str, &smp->data.ipv4, chk->len);
664 }
665 else if (smp->type == SMP_T_IPV6) {
666 chk->len = 16;
667 memcpy(chk->str, &smp->data.ipv6, chk->len);
668 }
669 else
670 return 0;
671
672 smp->data.str = *chk;
673 smp->type = SMP_T_BIN;
674 return 1;
675}
676
677
Emeric Brun107ca302010-01-04 16:16:05 +0100678/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200679/* Sample casts matrix: */
680/* sample_casts[from type][to type] */
681/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100682/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100683
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100684sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100685/* to: BOOL UINT SINT ADDR IPV4 IPV6 STR BIN METH */
686/* from: BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
687/* UINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_int2str, NULL, NULL, },
688/* SINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_int2str, NULL, NULL, },
689/* ADDR */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200690/* IPV4 */ { NULL, c_ip2int, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
691/* IPV6 */ { NULL, NULL, NULL, c_none, NULL, c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100692/* STR */ { c_str2int, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
693/* BIN */ { NULL, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
694/* METH */ { NULL, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, },
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200695};
Emeric Brun107ca302010-01-04 16:16:05 +0100696
Emeric Brun107ca302010-01-04 16:16:05 +0100697/*
Willy Tarreau12785782012-04-27 21:37:17 +0200698 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100699 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200700 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200701 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100702 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100703struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err_msg, struct arg_list *al)
Emeric Brun107ca302010-01-04 16:16:05 +0100704{
Willy Tarreau833cc792013-07-24 15:34:19 +0200705 const char *begw; /* beginning of word */
706 const char *endw; /* end of word */
707 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200708 struct sample_expr *expr;
709 struct sample_fetch *fetch;
710 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100711 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200712 char *fkw = NULL;
713 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100714 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100715
Willy Tarreau833cc792013-07-24 15:34:19 +0200716 begw = str[*idx];
717 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
718
719 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100720 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100721 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200722 }
Emeric Brun107ca302010-01-04 16:16:05 +0100723
Willy Tarreau833cc792013-07-24 15:34:19 +0200724 /* keep a copy of the current fetch keyword for error reporting */
725 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100726
Willy Tarreau833cc792013-07-24 15:34:19 +0200727 fetch = find_sample_fetch(begw, endw - begw);
728 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100729 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100730 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200731 }
Emeric Brun107ca302010-01-04 16:16:05 +0100732
Willy Tarreau833cc792013-07-24 15:34:19 +0200733 endt = endw;
734 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100735 /* look for the end of this term and skip the opening parenthesis */
736 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200737 while (*endt && *endt != ')')
738 endt++;
739 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100740 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200741 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200742 }
Emeric Brun485479d2010-09-23 18:02:19 +0200743 }
Emeric Brun107ca302010-01-04 16:16:05 +0100744
Willy Tarreau833cc792013-07-24 15:34:19 +0200745 /* At this point, we have :
746 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100747 * - endw : end of the keyword, first character not part of keyword
748 * nor the opening parenthesis (so first character of args
749 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200750 * - endt : end of the term (=endw or last parenthesis if args are present)
751 */
752
753 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100754 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100755 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200756 }
Emeric Brun107ca302010-01-04 16:16:05 +0100757 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200758
Willy Tarreau12785782012-04-27 21:37:17 +0200759 expr = calloc(1, sizeof(struct sample_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200760 if (!expr)
761 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100762
763 LIST_INIT(&(expr->conv_exprs));
764 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200765 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100766
Willy Tarreau689a1df2013-12-13 00:40:11 +0100767 /* Note that we call the argument parser even with an empty string,
768 * this allows it to automatically create entries for mandatory
769 * implicit arguments (eg: local proxy name).
770 */
771 al->kw = expr->fetch->kw;
772 al->conv = NULL;
773 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
774 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
775 goto out_error;
776 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200777
Willy Tarreau689a1df2013-12-13 00:40:11 +0100778 if (!expr->arg_p) {
779 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200780 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100781 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
782 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200783 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100784 }
785
Willy Tarreau833cc792013-07-24 15:34:19 +0200786 /* Now process the converters if any. We have two supported syntaxes
787 * for the converters, which can be combined :
788 * - comma-delimited list of converters just after the keyword and args ;
789 * - one converter per keyword
790 * The combination allows to have each keyword being a comma-delimited
791 * series of converters.
792 *
793 * We want to process the former first, then the latter. For this we start
794 * from the beginning of the supposed place in the exiting conv chain, which
795 * starts at the last comma (endt).
796 */
797
798 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200799 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100800
Willy Tarreau833cc792013-07-24 15:34:19 +0200801 if (*endt == ')') /* skip last closing parenthesis */
802 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100803
Willy Tarreau833cc792013-07-24 15:34:19 +0200804 if (*endt && *endt != ',') {
805 if (ckw)
Willy Tarreau975c1782013-12-12 23:16:54 +0100806 memprintf(err_msg, "missing comma after conv keyword '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200807 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100808 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100809 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200810 }
Emeric Brun107ca302010-01-04 16:16:05 +0100811
Willy Tarreau833cc792013-07-24 15:34:19 +0200812 while (*endt == ',') /* then trailing commas */
813 endt++;
814
815 begw = endt; /* start of conv keyword */
816
817 if (!*begw) {
818 /* none ? skip to next string */
819 (*idx)++;
820 begw = str[*idx];
821 if (!begw || !*begw)
822 break;
823 }
824
825 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
826
827 free(ckw);
828 ckw = my_strndup(begw, endw - begw);
829
830 conv = find_sample_conv(begw, endw - begw);
831 if (!conv) {
832 /* we found an isolated keyword that we don't know, it's not ours */
833 if (begw == str[*idx])
834 break;
Willy Tarreau975c1782013-12-12 23:16:54 +0100835 memprintf(err_msg, "unknown conv method '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200836 goto out_error;
837 }
Emeric Brun107ca302010-01-04 16:16:05 +0100838
Willy Tarreau833cc792013-07-24 15:34:19 +0200839 endt = endw;
840 if (*endt == '(') {
841 /* look for the end of this term */
842 while (*endt && *endt != ')')
843 endt++;
844 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100845 memprintf(err_msg, "syntax error: missing ')' after conv keyword '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200846 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200847 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200848 }
849
850 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100851 memprintf(err_msg, "returns type of conv method '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100852 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200853 }
Emeric Brun107ca302010-01-04 16:16:05 +0100854
855 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200856 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100857 memprintf(err_msg, "conv method '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100858 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200859 }
Emeric Brun107ca302010-01-04 16:16:05 +0100860
861 prev_type = conv->out_type;
Willy Tarreau12785782012-04-27 21:37:17 +0200862 conv_expr = calloc(1, sizeof(struct sample_conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200863 if (!conv_expr)
864 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100865
866 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
867 conv_expr->conv = conv;
868
Willy Tarreau833cc792013-07-24 15:34:19 +0200869 if (endt != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200870 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200871
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200872 if (!conv->arg_mask) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100873 memprintf(err_msg, "conv method '%s' does not support any args", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200874 goto out_error;
875 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100876
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200877 al->kw = expr->fetch->kw;
878 al->conv = conv_expr->conv->kw;
Willy Tarreau975c1782013-12-12 23:16:54 +0100879 if (make_arg_list(endw + 1, endt - endw - 1, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
880 memprintf(err_msg, "invalid arg %d in conv method '%s' : %s", err_arg+1, ckw, *err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200881 goto out_error;
882 }
883
Willy Tarreau2e845be2012-10-19 19:49:09 +0200884 if (!conv_expr->arg_p)
885 conv_expr->arg_p = empty_arg_list;
886
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100887 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100888 memprintf(err_msg, "invalid args in conv method '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200889 goto out_error;
890 }
891 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200892 else if (ARGM(conv->arg_mask)) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100893 memprintf(err_msg, "missing args for conv method '%s'", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200894 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100895 }
896 }
Emeric Brun485479d2010-09-23 18:02:19 +0200897
Willy Tarreau833cc792013-07-24 15:34:19 +0200898 out:
899 free(fkw);
900 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100901 return expr;
902
903out_error:
Willy Tarreau12785782012-04-27 21:37:17 +0200904 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +0200905 expr = NULL;
906 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +0100907}
908
909/*
Willy Tarreau12785782012-04-27 21:37:17 +0200910 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200911 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +0200912 * Returns a pointer on a typed sample structure containing the result or NULL if
913 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +0100914 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +0200915 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200916 *
917 * Note: the fetch functions are required to properly set the return type. The
918 * conversion functions must do so too. However the cast functions do not need
919 * to since they're made to cast mutiple types according to what is required.
Emeric Brun107ca302010-01-04 16:16:05 +0100920 */
Willy Tarreau12785782012-04-27 21:37:17 +0200921struct sample *sample_process(struct proxy *px, struct session *l4, void *l7,
922 unsigned int opt,
923 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +0100924{
Willy Tarreau12785782012-04-27 21:37:17 +0200925 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100926
Willy Tarreau18387e22013-07-25 12:02:38 +0200927 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +0200928 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +0200929 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +0200930 }
Emeric Brun107ca302010-01-04 16:16:05 +0100931
Willy Tarreauef38c392013-07-22 16:29:32 +0200932 if (!expr->fetch->process(px, l4, l7, opt, expr->arg_p, p, expr->fetch->kw))
Emeric Brun107ca302010-01-04 16:16:05 +0100933 return NULL;
934
Emeric Brun107ca302010-01-04 16:16:05 +0100935 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +0200936 /* we want to ensure that p->type can be casted into
937 * conv_expr->conv->in_type. We have 3 possibilities :
938 * - NULL => not castable.
939 * - c_none => nothing to do (let's optimize it)
940 * - other => apply cast and prepare to fail
941 */
Willy Tarreau12785782012-04-27 21:37:17 +0200942 if (!sample_casts[p->type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +0200943 return NULL;
944
Willy Tarreau12785782012-04-27 21:37:17 +0200945 if (sample_casts[p->type][conv_expr->conv->in_type] != c_none &&
946 !sample_casts[p->type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +0100947 return NULL;
948
Willy Tarreau12e50112012-04-25 17:21:49 +0200949 /* OK cast succeeded */
950
Willy Tarreau342acb42012-04-23 22:03:39 +0200951 if (!conv_expr->conv->process(conv_expr->arg_p, p))
Emeric Brun107ca302010-01-04 16:16:05 +0100952 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +0100953 }
954 return p;
955}
956
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +0100957/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200958 * Resolve all remaining arguments in proxy <p>. Returns the number of
959 * errors or 0 if everything is fine.
960 */
961int smp_resolve_args(struct proxy *p)
962{
963 struct arg_list *cur, *bak;
964 const char *ctx, *where;
965 const char *conv_ctx, *conv_pre, *conv_pos;
966 struct userlist *ul;
967 struct arg *arg;
968 int cfgerr = 0;
969
970 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
971 struct proxy *px;
972 struct server *srv;
973 char *pname, *sname;
974
975 arg = cur->arg;
976
977 /* prepare output messages */
978 conv_pre = conv_pos = conv_ctx = "";
979 if (cur->conv) {
980 conv_ctx = cur->conv;
981 conv_pre = "conversion keyword '";
982 conv_pos = "' for ";
983 }
984
985 where = "in";
986 ctx = "sample fetch keyword";
987 switch (cur->ctx) {
988 case ARGC_STK:where = "in stick rule in"; break;
989 case ARGC_TRK: where = "in tracking rule in"; break;
990 case ARGC_LOG: where = "in log-format string in"; break;
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100991 case ARGC_HRQ: where = "in http-request header format string in"; break;
992 case ARGC_HRS: where = "in http-response header format string in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200993 case ARGC_UIF: where = "in unique-id-format string in"; break;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100994 case ARGC_RDR: where = "in redirect format string in"; break;
Willy Tarreau3a4ac422014-06-13 16:17:14 +0200995 case ARGC_CAP: where = "in capture rule in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200996 case ARGC_ACL: ctx = "ACL keyword"; break;
997 }
998
999 /* set a few default settings */
1000 px = p;
1001 pname = p->id;
1002
1003 switch (arg->type) {
1004 case ARGT_SRV:
1005 if (!arg->data.str.len) {
1006 Alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1007 cur->file, cur->line,
1008 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1009 cfgerr++;
1010 continue;
1011 }
1012
1013 /* we support two formats : "bck/srv" and "srv" */
1014 sname = strrchr(arg->data.str.str, '/');
1015
1016 if (sname) {
1017 *sname++ = '\0';
1018 pname = arg->data.str.str;
1019
1020 px = findproxy(pname, PR_CAP_BE);
1021 if (!px) {
1022 Alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1023 cur->file, cur->line, pname,
1024 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1025 cfgerr++;
1026 break;
1027 }
1028 }
1029 else
1030 sname = arg->data.str.str;
1031
1032 srv = findserver(px, sname);
1033 if (!srv) {
1034 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",
1035 cur->file, cur->line, sname, pname,
1036 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1037 cfgerr++;
1038 break;
1039 }
1040
1041 free(arg->data.str.str);
1042 arg->data.str.str = NULL;
1043 arg->unresolved = 0;
1044 arg->data.srv = srv;
1045 break;
1046
1047 case ARGT_FE:
1048 if (arg->data.str.len) {
1049 pname = arg->data.str.str;
1050 px = findproxy(pname, PR_CAP_FE);
1051 }
1052
1053 if (!px) {
1054 Alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1055 cur->file, cur->line, pname,
1056 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1057 cfgerr++;
1058 break;
1059 }
1060
1061 if (!(px->cap & PR_CAP_FE)) {
1062 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1063 cur->file, cur->line, pname,
1064 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1065 cfgerr++;
1066 break;
1067 }
1068
1069 free(arg->data.str.str);
1070 arg->data.str.str = NULL;
1071 arg->unresolved = 0;
1072 arg->data.prx = px;
1073 break;
1074
1075 case ARGT_BE:
1076 if (arg->data.str.len) {
1077 pname = arg->data.str.str;
1078 px = findproxy(pname, PR_CAP_BE);
1079 }
1080
1081 if (!px) {
1082 Alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1083 cur->file, cur->line, pname,
1084 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1085 cfgerr++;
1086 break;
1087 }
1088
1089 if (!(px->cap & PR_CAP_BE)) {
1090 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1091 cur->file, cur->line, pname,
1092 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1093 cfgerr++;
1094 break;
1095 }
1096
1097 free(arg->data.str.str);
1098 arg->data.str.str = NULL;
1099 arg->unresolved = 0;
1100 arg->data.prx = px;
1101 break;
1102
1103 case ARGT_TAB:
1104 if (arg->data.str.len) {
1105 pname = arg->data.str.str;
1106 px = find_stktable(pname);
1107 }
1108
1109 if (!px) {
1110 Alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1111 cur->file, cur->line, pname,
1112 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1113 cfgerr++;
1114 break;
1115 }
1116
1117 if (!px->table.size) {
1118 Alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1119 cur->file, cur->line, pname,
1120 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1121 cfgerr++;
1122 break;
1123 }
1124
1125 free(arg->data.str.str);
1126 arg->data.str.str = NULL;
1127 arg->unresolved = 0;
1128 arg->data.prx = px;
1129 break;
1130
1131 case ARGT_USR:
1132 if (!arg->data.str.len) {
1133 Alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1134 cur->file, cur->line,
1135 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1136 cfgerr++;
1137 break;
1138 }
1139
1140 if (p->uri_auth && p->uri_auth->userlist &&
1141 !strcmp(p->uri_auth->userlist->name, arg->data.str.str))
1142 ul = p->uri_auth->userlist;
1143 else
1144 ul = auth_find_userlist(arg->data.str.str);
1145
1146 if (!ul) {
1147 Alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1148 cur->file, cur->line, arg->data.str.str,
1149 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1150 cfgerr++;
1151 break;
1152 }
1153
1154 free(arg->data.str.str);
1155 arg->data.str.str = NULL;
1156 arg->unresolved = 0;
1157 arg->data.usr = ul;
1158 break;
1159 }
1160
1161 LIST_DEL(&cur->list);
1162 free(cur);
1163 } /* end of args processing */
1164
1165 return cfgerr;
1166}
1167
1168/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001169 * Process a fetch + format conversion as defined by the sample expression
1170 * <expr> on request or response considering the <opt> parameter. The output is
1171 * always of type string. If a stable sample can be fetched, or an unstable one
1172 * when <opt> contains SMP_OPT_FINAL, the sample is converted to a string and
1173 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1174 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1175 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1176 * take actions (eg: wait longer). If a sample could not be found or could not
1177 * be converted, NULL is returned.
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001178 */
1179struct sample *sample_fetch_string(struct proxy *px, struct session *l4, void *l7,
1180 unsigned int opt, struct sample_expr *expr)
1181{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001182 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001183
Willy Tarreau6c616e02014-06-25 16:56:41 +02001184 memset(smp, 0, sizeof(*smp));
1185
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001186 if (!sample_process(px, l4, l7, opt, expr, smp)) {
1187 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1188 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001189 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001190 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001191
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001192 if (!sample_casts[smp->type][SMP_T_STR])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001193 return NULL;
1194
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001195 if (!sample_casts[smp->type][SMP_T_STR](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001196 return NULL;
1197
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001198 smp->type = SMP_T_STR;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001199 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001200 return smp;
1201}
1202
Emeric Brun107ca302010-01-04 16:16:05 +01001203/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001204/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001205/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001206/*****************************************************************/
1207
Emeric Brun53d1a982014-04-30 18:21:37 +02001208static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp)
1209{
1210 struct chunk *trash = get_trash_chunk();
1211 int b64_len;
1212
1213 trash->len = 0;
1214 b64_len = a2base64(smp->data.str.str, smp->data.str.len, trash->str, trash->size);
1215 if (b64_len < 0)
1216 return 0;
1217
1218 trash->len = b64_len;
1219 smp->data.str = *trash;
1220 smp->type = SMP_T_STR;
1221 smp->flags &= ~SMP_F_CONST;
1222 return 1;
1223}
1224
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001225static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp)
1226{
1227 struct chunk *trash = get_trash_chunk();
1228 unsigned char c;
1229 int ptr = 0;
1230
1231 trash->len = 0;
1232 while (ptr < smp->data.str.len && trash->len <= trash->size - 2) {
1233 c = smp->data.str.str[ptr++];
1234 trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
1235 trash->str[trash->len++] = hextab[c & 0xF];
1236 }
1237 smp->data.str = *trash;
1238 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001239 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001240 return 1;
1241}
1242
Willy Tarreau12785782012-04-27 21:37:17 +02001243static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +01001244{
1245 int i;
1246
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001247 if (!smp_dup(smp))
1248 return 0;
1249
Willy Tarreau342acb42012-04-23 22:03:39 +02001250 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +02001251 return 0;
1252
Willy Tarreau342acb42012-04-23 22:03:39 +02001253 for (i = 0; i < smp->data.str.len; i++) {
1254 if ((smp->data.str.str[i] >= 'A') && (smp->data.str.str[i] <= 'Z'))
1255 smp->data.str.str[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001256 }
1257 return 1;
1258}
1259
Willy Tarreau12785782012-04-27 21:37:17 +02001260static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +01001261{
1262 int i;
1263
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001264 if (!smp_dup(smp))
1265 return 0;
1266
Willy Tarreau342acb42012-04-23 22:03:39 +02001267 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +02001268 return 0;
1269
Willy Tarreau342acb42012-04-23 22:03:39 +02001270 for (i = 0; i < smp->data.str.len; i++) {
1271 if ((smp->data.str.str[i] >= 'a') && (smp->data.str.str[i] <= 'z'))
1272 smp->data.str.str[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001273 }
1274 return 1;
1275}
1276
Willy Tarreauf9954102012-04-20 14:03:29 +02001277/* takes the netmask in arg_p */
Willy Tarreau12785782012-04-27 21:37:17 +02001278static int sample_conv_ipmask(const struct arg *arg_p, struct sample *smp)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001279{
Willy Tarreau342acb42012-04-23 22:03:39 +02001280 smp->data.ipv4.s_addr &= arg_p->data.ipv4.s_addr;
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001281 smp->type = SMP_T_IPV4;
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001282 return 1;
1283}
1284
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001285/* takes an UINT value on input supposed to represent the time since EPOCH,
1286 * adds an optional offset found in args[1] and emits a string representing
1287 * the local time in the format specified in args[1] using strftime().
1288 */
1289static int sample_conv_ltime(const struct arg *args, struct sample *smp)
1290{
1291 struct chunk *temp;
1292 time_t curr_date = smp->data.uint;
1293
1294 /* add offset */
1295 if (args[1].type == ARGT_SINT || args[1].type == ARGT_UINT)
1296 curr_date += args[1].data.sint;
1297
1298 temp = get_trash_chunk();
1299 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, localtime(&curr_date));
1300 smp->data.str = *temp;
1301 smp->type = SMP_T_STR;
1302 return 1;
1303}
1304
1305/* takes an UINT value on input supposed to represent the time since EPOCH,
1306 * adds an optional offset found in args[1] and emits a string representing
1307 * the UTC date in the format specified in args[1] using strftime().
1308 */
1309static int sample_conv_utime(const struct arg *args, struct sample *smp)
1310{
1311 struct chunk *temp;
1312 time_t curr_date = smp->data.uint;
1313
1314 /* add offset */
1315 if (args[1].type == ARGT_SINT || args[1].type == ARGT_UINT)
1316 curr_date += args[1].data.sint;
1317
1318 temp = get_trash_chunk();
1319 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, gmtime(&curr_date));
1320 smp->data.str = *temp;
1321 smp->type = SMP_T_STR;
1322 return 1;
1323}
1324
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001325/************************************************************************/
1326/* All supported sample fetch functions must be declared here */
1327/************************************************************************/
1328
1329/* force TRUE to be returned at the fetch level */
1330static int
1331smp_fetch_true(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001332 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001333{
1334 smp->type = SMP_T_BOOL;
1335 smp->data.uint = 1;
1336 return 1;
1337}
1338
1339/* force FALSE to be returned at the fetch level */
1340static int
1341smp_fetch_false(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001342 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001343{
1344 smp->type = SMP_T_BOOL;
1345 smp->data.uint = 0;
1346 return 1;
1347}
1348
1349/* retrieve environment variable $1 as a string */
1350static int
1351smp_fetch_env(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001352 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001353{
1354 char *env;
1355
1356 if (!args || args[0].type != ARGT_STR)
1357 return 0;
1358
1359 env = getenv(args[0].data.str.str);
1360 if (!env)
1361 return 0;
1362
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001363 smp->type = SMP_T_STR;
1364 smp->flags = SMP_F_CONST;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001365 smp->data.str.str = env;
1366 smp->data.str.len = strlen(env);
1367 return 1;
1368}
1369
Willy Tarreau6236d3a2013-07-25 14:28:25 +02001370/* retrieve the current local date in epoch time, and applies an optional offset
1371 * of args[0] seconds.
1372 */
1373static int
1374smp_fetch_date(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001375 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02001376{
1377 smp->data.uint = date.tv_sec;
1378
1379 /* add offset */
1380 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
1381 smp->data.uint += args[0].data.sint;
1382
1383 smp->type = SMP_T_UINT;
1384 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
1385 return 1;
1386}
1387
Willy Tarreau84310e22014-02-14 11:59:04 +01001388/* generate a random 32-bit integer for whatever purpose, with an optional
1389 * range specified in argument.
1390 */
1391static int
1392smp_fetch_rand(struct proxy *px, struct session *s, void *l7, unsigned int opt,
1393 const struct arg *args, struct sample *smp, const char *kw)
1394{
1395 smp->data.uint = random();
1396
1397 /* reduce if needed. Don't do a modulo, use all bits! */
1398 if (args && args[0].type == ARGT_UINT)
1399 smp->data.uint = ((uint64_t)smp->data.uint * args[0].data.uint) >> 32;
1400
1401 smp->type = SMP_T_UINT;
1402 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
1403 return 1;
1404}
1405
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001406/* Note: must not be declared <const> as its list will be overwritten.
1407 * Note: fetches that may return multiple types must be declared as the lowest
1408 * common denominator, the type that can be casted into all other ones. For
1409 * instance IPv4/IPv6 must be declared IPv4.
1410 */
1411static struct sample_fetch_kw_list smp_kws = {ILH, {
1412 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
1413 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001414 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Willy Tarreau6236d3a2013-07-25 14:28:25 +02001415 { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreau84310e22014-02-14 11:59:04 +01001416 { "rand", smp_fetch_rand, ARG1(0,UINT), NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001417 { /* END */ },
1418}};
1419
Emeric Brun107ca302010-01-04 16:16:05 +01001420/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001421static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Emeric Brun53d1a982014-04-30 18:21:37 +02001422 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02001423 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
1424 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001425 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02001426 { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 },
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001427 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR },
1428 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001429 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01001430}};
1431
1432__attribute__((constructor))
Willy Tarreau12785782012-04-27 21:37:17 +02001433static void __sample_init(void)
Emeric Brun107ca302010-01-04 16:16:05 +01001434{
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001435 /* register sample fetch and format conversion keywords */
1436 sample_register_fetches(&smp_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02001437 sample_register_convs(&sample_conv_kws);
Emeric Brun107ca302010-01-04 16:16:05 +01001438}