blob: 57b2c003a80917513dfaf8ecc435484ae877274e [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
Thierry FOURNIER317e1c42014-08-12 10:20:47 +020014#include <ctype.h>
Emeric Brun107ca302010-01-04 16:16:05 +010015#include <string.h>
16#include <arpa/inet.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020017#include <stdio.h>
Emeric Brun107ca302010-01-04 16:16:05 +010018
Willy Tarreau7e2c6472012-10-29 20:44:36 +010019#include <types/global.h>
20
Willy Tarreauc7e42382012-08-24 19:22:53 +020021#include <common/chunk.h>
Willy Tarreau23ec4ca2014-07-15 20:15:37 +020022#include <common/hash.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020023#include <common/standard.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020024#include <common/uri_auth.h>
Emeric Brun53d1a982014-04-30 18:21:37 +020025#include <common/base64.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020026
Willy Tarreau9fcb9842012-04-20 14:45:49 +020027#include <proto/arg.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020028#include <proto/auth.h>
29#include <proto/log.h>
Thierry FOURNIERd4373142013-12-17 01:10:10 +010030#include <proto/proto_http.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020031#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020032#include <proto/sample.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020033#include <proto/stick_table.h>
Emeric Brun107ca302010-01-04 16:16:05 +010034
Willy Tarreau1cf8f082014-02-07 12:14:54 +010035/* sample type names */
36const char *smp_to_type[SMP_TYPES] = {
37 [SMP_T_BOOL] = "bool",
38 [SMP_T_UINT] = "uint",
39 [SMP_T_SINT] = "sint",
40 [SMP_T_ADDR] = "addr",
41 [SMP_T_IPV4] = "ipv4",
42 [SMP_T_IPV6] = "ipv6",
43 [SMP_T_STR] = "str",
44 [SMP_T_BIN] = "bin",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010045};
46
Willy Tarreau12785782012-04-27 21:37:17 +020047/* static sample used in sample_process() when <p> is NULL */
Willy Tarreaub4a88f02012-04-23 21:35:11 +020048static struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010049
Willy Tarreau12785782012-04-27 21:37:17 +020050/* list head of all known sample fetch keywords */
51static struct sample_fetch_kw_list sample_fetches = {
52 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010053};
54
Willy Tarreau12785782012-04-27 21:37:17 +020055/* list head of all known sample format conversion keywords */
56static struct sample_conv_kw_list sample_convs = {
57 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010058};
59
Willy Tarreau80aca902013-01-07 15:42:20 +010060const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
61 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
62 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
63 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
64 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
65 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
66 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
67 SMP_VAL_FE_LOG_END),
68
69 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
70 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
71 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
72 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
73 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
74 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
75 SMP_VAL_FE_LOG_END),
76
77 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
78 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
79 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
80 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
81 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
82 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
83 SMP_VAL_FE_LOG_END),
84
85 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
86 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
87 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
88 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
89 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
90 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
91 SMP_VAL_FE_LOG_END),
92
93 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
94 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
95 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
96 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
97 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
98 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
99 SMP_VAL_FE_LOG_END),
100
101 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
102 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
103 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
104 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
105 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
106 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
107 SMP_VAL_FE_LOG_END),
108
109 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
110 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
111 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
112 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
113 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
114 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
115 SMP_VAL___________),
116
117 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
118 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
119 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
120 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
121 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
122 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
123 SMP_VAL___________),
124
125 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
126 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
127 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
128 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
129 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
130 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
131 SMP_VAL_FE_LOG_END),
132
133 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
134 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
135 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
136 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
137 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
138 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
139 SMP_VAL___________),
140
141 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
142 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
143 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
144 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
145 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
146 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
147 SMP_VAL_FE_LOG_END),
148
149 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
150 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
151 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
152 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
153 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
154 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
155 SMP_VAL_FE_LOG_END),
156
157 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
158 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
159 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
160 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
161 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
162 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
163 SMP_VAL_FE_LOG_END),
164
165 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
166 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
167 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
168 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
169 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
170 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
171 SMP_VAL_FE_LOG_END),
172
173 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
174 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
175 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
176 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
177 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
178 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
179 SMP_VAL___________),
180
181 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
182 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
183 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
184 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
185 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
186 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
187 SMP_VAL___________),
188
189 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
190 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
191 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
192 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
193 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
194 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
195 SMP_VAL_FE_LOG_END),
196
197 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
198 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
199 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
200 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
201 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
202 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
203 SMP_VAL___________),
204
205 [SMP_SRC_RQFIN] = (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___________ | SMP_VAL___________ | SMP_VAL___________ |
210 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
211 SMP_VAL_FE_LOG_END),
212
213 [SMP_SRC_RSFIN] = (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___________ | SMP_VAL___________ | SMP_VAL___________ |
218 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
219 SMP_VAL_FE_LOG_END),
220
221 [SMP_SRC_TXFIN] = (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___________ | SMP_VAL___________ | SMP_VAL___________ |
226 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
227 SMP_VAL_FE_LOG_END),
228
229 [SMP_SRC_SSFIN] = (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___________ | SMP_VAL___________ | SMP_VAL___________ |
234 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
235 SMP_VAL_FE_LOG_END),
236};
237
238static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
239 [SMP_SRC_INTRN] = "internal state",
240 [SMP_SRC_LISTN] = "listener",
241 [SMP_SRC_FTEND] = "frontend",
242 [SMP_SRC_L4CLI] = "client address",
243 [SMP_SRC_L5CLI] = "client-side connection",
244 [SMP_SRC_TRACK] = "track counters",
245 [SMP_SRC_L6REQ] = "request buffer",
246 [SMP_SRC_HRQHV] = "HTTP request headers",
247 [SMP_SRC_HRQHP] = "HTTP request",
248 [SMP_SRC_HRQBO] = "HTTP request body",
249 [SMP_SRC_BKEND] = "backend",
250 [SMP_SRC_SERVR] = "server",
251 [SMP_SRC_L4SRV] = "server address",
252 [SMP_SRC_L5SRV] = "server-side connection",
253 [SMP_SRC_L6RES] = "response buffer",
254 [SMP_SRC_HRSHV] = "HTTP response headers",
255 [SMP_SRC_HRSHP] = "HTTP response",
256 [SMP_SRC_HRSBO] = "HTTP response body",
257 [SMP_SRC_RQFIN] = "request buffer statistics",
258 [SMP_SRC_RSFIN] = "response buffer statistics",
259 [SMP_SRC_TXFIN] = "transaction statistics",
260 [SMP_SRC_SSFIN] = "session statistics",
261};
262
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100263static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
264 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
265 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
266 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
267 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
268 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
269 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
270 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
271 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
272 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
273 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
274 [SMP_CKP_BE_SRV_CON] = "server source selection",
275 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
276 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
277 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
278 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
279 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
280 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
281 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
282 [SMP_CKP_FE_LOG_END] = "logs",
283};
284
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100285/* This function returns the type of the data returned by the sample_expr.
286 * It assumes that the <expr> and all of its converters are properly
287 * initialized.
288 */
289inline
290int smp_expr_output_type(struct sample_expr *expr)
291{
292 struct sample_conv_expr *smp_expr;
293
294 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
295 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
296 return smp_expr->conv->out_type;
297 }
298 return expr->fetch->out_type;
299}
300
301
Willy Tarreau80aca902013-01-07 15:42:20 +0100302/* fill the trash with a comma-delimited list of source names for the <use> bit
303 * field which must be composed of a non-null set of SMP_USE_* flags. The return
304 * value is the pointer to the string in the trash buffer.
305 */
306const char *sample_src_names(unsigned int use)
307{
308 int bit;
309
310 trash.len = 0;
311 trash.str[0] = '\0';
312 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
313 if (!(use & ~((1 << bit) - 1)))
314 break; /* no more bits */
315
316 if (!(use & (1 << bit)))
317 continue; /* bit not set */
318
319 trash.len += snprintf(trash.str + trash.len, trash.size - trash.len, "%s%s",
320 (use & ((1 << bit) - 1)) ? "," : "",
321 fetch_src_names[bit]);
322 }
323 return trash.str;
324}
325
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100326/* return a pointer to the correct sample checkpoint name, or "unknown" when
327 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
328 * if set.
329 */
330const char *sample_ckp_names(unsigned int use)
331{
332 int bit;
333
334 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
335 if (use & (1 << bit))
336 return fetch_ckp_names[bit];
337 return "unknown sample check place, please report this bug";
338}
339
Emeric Brun107ca302010-01-04 16:16:05 +0100340/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100341 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
342 * for next parsing sessions. The fetch keywords capabilities are also computed
343 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100344 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100345void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100346{
Willy Tarreau80aca902013-01-07 15:42:20 +0100347 struct sample_fetch *sf;
348 int bit;
349
350 for (sf = kwl->kw; sf->kw != NULL; sf++) {
351 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
352 if (sf->use & (1 << bit))
353 sf->val |= fetch_cap[bit];
354 }
355 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100356}
357
358/*
Willy Tarreau12785782012-04-27 21:37:17 +0200359 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100360 * parsing sessions.
361 */
Willy Tarreau12785782012-04-27 21:37:17 +0200362void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100363{
Willy Tarreau12785782012-04-27 21:37:17 +0200364 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100365}
366
367/*
Willy Tarreau12785782012-04-27 21:37:17 +0200368 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100369 * string of <len> in buffer <kw>.
370 *
371 */
Willy Tarreau12785782012-04-27 21:37:17 +0200372struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100373{
374 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200375 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100376
Willy Tarreau12785782012-04-27 21:37:17 +0200377 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100378 for (index = 0; kwl->kw[index].kw != NULL; index++) {
379 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
380 kwl->kw[index].kw[len] == '\0')
381 return &kwl->kw[index];
382 }
383 }
384 return NULL;
385}
386
387/*
Willy Tarreau12785782012-04-27 21:37:17 +0200388 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100389 * string of <len> in buffer <kw>.
390 *
391 */
Willy Tarreau12785782012-04-27 21:37:17 +0200392struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100393{
394 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200395 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100396
Willy Tarreau12785782012-04-27 21:37:17 +0200397 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100398 for (index = 0; kwl->kw[index].kw != NULL; index++) {
399 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
400 kwl->kw[index].kw[len] == '\0')
401 return &kwl->kw[index];
402 }
403 }
404 return NULL;
405}
406
Emeric Brun107ca302010-01-04 16:16:05 +0100407/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200408/* Sample casts functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200409/* Note: these functions do *NOT* set the output type on the */
410/* sample, the caller is responsible for doing this on return. */
Emeric Brun107ca302010-01-04 16:16:05 +0100411/******************************************************************/
412
Willy Tarreau342acb42012-04-23 22:03:39 +0200413static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100414{
Willy Tarreau342acb42012-04-23 22:03:39 +0200415 smp->data.uint = ntohl(smp->data.ipv4.s_addr);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100416 smp->type = SMP_T_UINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100417 return 1;
418}
419
Willy Tarreau342acb42012-04-23 22:03:39 +0200420static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100421{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100422 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100423
Willy Tarreau342acb42012-04-23 22:03:39 +0200424 if (!inet_ntop(AF_INET, (void *)&smp->data.ipv4, trash->str, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100425 return 0;
426
427 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200428 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100429 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100430 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100431
432 return 1;
433}
434
Willy Tarreau342acb42012-04-23 22:03:39 +0200435static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100436{
Willy Tarreau342acb42012-04-23 22:03:39 +0200437 v4tov6(&smp->data.ipv6, &smp->data.ipv4);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100438 smp->type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100439 return 1;
440}
441
Willy Tarreau342acb42012-04-23 22:03:39 +0200442static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100443{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100444 struct chunk *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100445
Willy Tarreau342acb42012-04-23 22:03:39 +0200446 if (!inet_ntop(AF_INET6, (void *)&smp->data.ipv6, trash->str, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100447 return 0;
448
449 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200450 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100451 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100452 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100453 return 1;
454}
455
456/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200457static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100458{
Willy Tarreau342acb42012-04-23 22:03:39 +0200459 return v6tov4(&smp->data.ipv4, &smp->data.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100460}
461*/
462
Willy Tarreau342acb42012-04-23 22:03:39 +0200463static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100464{
Willy Tarreau342acb42012-04-23 22:03:39 +0200465 smp->data.ipv4.s_addr = htonl(smp->data.uint);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100466 smp->type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100467 return 1;
468}
469
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100470static int c_str2addr(struct sample *smp)
471{
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100472 if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4)) {
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100473 if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6))
474 return 0;
475 smp->type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100476 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100477 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100478 }
479 smp->type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100480 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100481 return 1;
482}
483
Willy Tarreau342acb42012-04-23 22:03:39 +0200484static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100485{
Willy Tarreau342acb42012-04-23 22:03:39 +0200486 if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100487 return 0;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100488 smp->type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100489 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100490 return 1;
491}
492
Willy Tarreau342acb42012-04-23 22:03:39 +0200493static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100494{
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100495 if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6))
496 return 0;
497 smp->type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100498 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100499 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100500}
501
Emeric Brun4b9e8022014-11-03 18:17:10 +0100502/*
503 * The NULL char always enforces the end of string if it is met.
504 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100505 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200506static int c_bin2str(struct sample *smp)
507{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100508 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200509
Emeric Brun4b9e8022014-11-03 18:17:10 +0100510 for (i = 0; i < smp->data.str.len; i++) {
511 if (!smp->data.str.str[i]) {
512 smp->data.str.len = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100513 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100514 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200515 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200516 return 1;
517}
518
Willy Tarreau342acb42012-04-23 22:03:39 +0200519static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100520{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100521 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100522 char *pos;
523
Willy Tarreau342acb42012-04-23 22:03:39 +0200524 pos = ultoa_r(smp->data.uint, trash->str, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100525
526 if (!pos)
527 return 0;
528
Emeric Brun485479d2010-09-23 18:02:19 +0200529 trash->size = trash->size - (pos - trash->str);
Emeric Brun107ca302010-01-04 16:16:05 +0100530 trash->str = pos;
531 trash->len = strlen(pos);
Willy Tarreau342acb42012-04-23 22:03:39 +0200532 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100533 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100534 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100535 return 1;
536}
537
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100538/* This function duplicates data and removes the flag "const". */
539int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200540{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100541 struct chunk *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200542
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100543 /* If the const flag is not set, we don't need to duplicate the
544 * pattern as it can be modified in place.
545 */
546 if (!(smp->flags & SMP_F_CONST))
547 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100548
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100549 switch (smp->type) {
550 case SMP_T_BOOL:
551 case SMP_T_UINT:
552 case SMP_T_SINT:
553 case SMP_T_ADDR:
554 case SMP_T_IPV4:
555 case SMP_T_IPV6:
556 /* These type are not const. */
557 break;
558 case SMP_T_STR:
559 case SMP_T_BIN:
560 /* Duplicate data. */
561 trash = get_trash_chunk();
562 trash->len = smp->data.str.len < trash->size ? smp->data.str.len : trash->size;
563 memcpy(trash->str, smp->data.str.str, trash->len);
564 smp->data.str = *trash;
565 break;
566 default:
567 /* Other cases are unexpected. */
568 return 0;
569 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100570
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100571 /* remove const flag */
572 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200573 return 1;
574}
575
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100576int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100577{
578 return 1;
579}
580
Willy Tarreau342acb42012-04-23 22:03:39 +0200581static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100582{
583 int i;
584 uint32_t ret = 0;
585
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100586 if (smp->data.str.len == 0)
587 return 0;
588
Willy Tarreau342acb42012-04-23 22:03:39 +0200589 for (i = 0; i < smp->data.str.len; i++) {
590 uint32_t val = smp->data.str.str[i] - '0';
Emeric Brun107ca302010-01-04 16:16:05 +0100591
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100592 if (val > 9) {
593 if (i == 0)
594 return 0;
Emeric Brun107ca302010-01-04 16:16:05 +0100595 break;
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100596 }
Emeric Brun107ca302010-01-04 16:16:05 +0100597
598 ret = ret * 10 + val;
599 }
600
Willy Tarreau342acb42012-04-23 22:03:39 +0200601 smp->data.uint = ret;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100602 smp->type = SMP_T_UINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100603 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100604 return 1;
605}
606
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100607static int c_str2meth(struct sample *smp)
608{
609 enum http_meth_t meth;
610 int len;
611
612 meth = find_http_meth(smp->data.str.str, smp->data.str.len);
613 if (meth == HTTP_METH_OTHER) {
614 len = smp->data.str.len;
615 smp->data.meth.str.str = smp->data.str.str;
616 smp->data.meth.str.len = len;
617 }
618 else
619 smp->flags &= ~SMP_F_CONST;
620 smp->data.meth.meth = meth;
621 smp->type = SMP_T_METH;
622 return 1;
623}
624
625static int c_meth2str(struct sample *smp)
626{
627 int len;
628 enum http_meth_t meth;
629
630 if (smp->data.meth.meth == HTTP_METH_OTHER) {
631 /* The method is unknown. Copy the original pointer. */
632 len = smp->data.meth.str.len;
633 smp->data.str.str = smp->data.meth.str.str;
634 smp->data.str.len = len;
635 smp->type = SMP_T_STR;
636 }
637 else if (smp->data.meth.meth < HTTP_METH_OTHER) {
638 /* The method is known, copy the pointer containing the string. */
639 meth = smp->data.meth.meth;
640 smp->data.str.str = http_known_methods[meth].name;
641 smp->data.str.len = http_known_methods[meth].len;
642 smp->flags |= SMP_F_CONST;
643 smp->type = SMP_T_STR;
644 }
645 else {
646 /* Unknown method */
647 return 0;
648 }
649 return 1;
650}
651
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200652static int c_addr2bin(struct sample *smp)
653{
654 struct chunk *chk = get_trash_chunk();
655
656 if (smp->type == SMP_T_IPV4) {
657 chk->len = 4;
658 memcpy(chk->str, &smp->data.ipv4, chk->len);
659 }
660 else if (smp->type == SMP_T_IPV6) {
661 chk->len = 16;
662 memcpy(chk->str, &smp->data.ipv6, chk->len);
663 }
664 else
665 return 0;
666
667 smp->data.str = *chk;
668 smp->type = SMP_T_BIN;
669 return 1;
670}
671
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200672static int c_int2bin(struct sample *smp)
673{
674 struct chunk *chk = get_trash_chunk();
675
676 *(unsigned int *)chk->str = htonl(smp->data.uint);
677 chk->len = 4;
678
679 smp->data.str = *chk;
680 smp->type = SMP_T_BIN;
681 return 1;
682}
683
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200684
Emeric Brun107ca302010-01-04 16:16:05 +0100685/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200686/* Sample casts matrix: */
687/* sample_casts[from type][to type] */
688/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100689/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100690
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100691sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100692/* to: BOOL UINT SINT ADDR IPV4 IPV6 STR BIN METH */
693/* from: BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200694/* UINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_int2str, c_int2bin, NULL, },
695/* SINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_int2str, c_int2bin, NULL, },
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100696/* ADDR */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200697/* IPV4 */ { NULL, c_ip2int, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
698/* IPV6 */ { NULL, NULL, NULL, c_none, NULL, c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100699/* STR */ { c_str2int, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
700/* BIN */ { NULL, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
701/* METH */ { NULL, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, },
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200702};
Emeric Brun107ca302010-01-04 16:16:05 +0100703
Emeric Brun107ca302010-01-04 16:16:05 +0100704/*
Willy Tarreau12785782012-04-27 21:37:17 +0200705 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100706 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200707 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200708 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100709 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100710struct 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 +0100711{
Willy Tarreau833cc792013-07-24 15:34:19 +0200712 const char *begw; /* beginning of word */
713 const char *endw; /* end of word */
714 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200715 struct sample_expr *expr;
716 struct sample_fetch *fetch;
717 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100718 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200719 char *fkw = NULL;
720 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100721 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100722
Willy Tarreau833cc792013-07-24 15:34:19 +0200723 begw = str[*idx];
724 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
725
726 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100727 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100728 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200729 }
Emeric Brun107ca302010-01-04 16:16:05 +0100730
Willy Tarreau833cc792013-07-24 15:34:19 +0200731 /* keep a copy of the current fetch keyword for error reporting */
732 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100733
Willy Tarreau833cc792013-07-24 15:34:19 +0200734 fetch = find_sample_fetch(begw, endw - begw);
735 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100736 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100737 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200738 }
Emeric Brun107ca302010-01-04 16:16:05 +0100739
Willy Tarreau833cc792013-07-24 15:34:19 +0200740 endt = endw;
741 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100742 /* look for the end of this term and skip the opening parenthesis */
743 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200744 while (*endt && *endt != ')')
745 endt++;
746 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100747 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200748 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200749 }
Emeric Brun485479d2010-09-23 18:02:19 +0200750 }
Emeric Brun107ca302010-01-04 16:16:05 +0100751
Willy Tarreau833cc792013-07-24 15:34:19 +0200752 /* At this point, we have :
753 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100754 * - endw : end of the keyword, first character not part of keyword
755 * nor the opening parenthesis (so first character of args
756 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200757 * - endt : end of the term (=endw or last parenthesis if args are present)
758 */
759
760 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100761 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100762 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200763 }
Emeric Brun107ca302010-01-04 16:16:05 +0100764 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200765
Willy Tarreau12785782012-04-27 21:37:17 +0200766 expr = calloc(1, sizeof(struct sample_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200767 if (!expr)
768 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100769
770 LIST_INIT(&(expr->conv_exprs));
771 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200772 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100773
Willy Tarreau689a1df2013-12-13 00:40:11 +0100774 /* Note that we call the argument parser even with an empty string,
775 * this allows it to automatically create entries for mandatory
776 * implicit arguments (eg: local proxy name).
777 */
778 al->kw = expr->fetch->kw;
779 al->conv = NULL;
780 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
781 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
782 goto out_error;
783 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200784
Willy Tarreau689a1df2013-12-13 00:40:11 +0100785 if (!expr->arg_p) {
786 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200787 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100788 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
789 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200790 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100791 }
792
Willy Tarreau833cc792013-07-24 15:34:19 +0200793 /* Now process the converters if any. We have two supported syntaxes
794 * for the converters, which can be combined :
795 * - comma-delimited list of converters just after the keyword and args ;
796 * - one converter per keyword
797 * The combination allows to have each keyword being a comma-delimited
798 * series of converters.
799 *
800 * We want to process the former first, then the latter. For this we start
801 * from the beginning of the supposed place in the exiting conv chain, which
802 * starts at the last comma (endt).
803 */
804
805 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200806 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100807
Willy Tarreau833cc792013-07-24 15:34:19 +0200808 if (*endt == ')') /* skip last closing parenthesis */
809 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100810
Willy Tarreau833cc792013-07-24 15:34:19 +0200811 if (*endt && *endt != ',') {
812 if (ckw)
Willy Tarreau975c1782013-12-12 23:16:54 +0100813 memprintf(err_msg, "missing comma after conv keyword '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200814 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100815 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100816 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200817 }
Emeric Brun107ca302010-01-04 16:16:05 +0100818
Willy Tarreau833cc792013-07-24 15:34:19 +0200819 while (*endt == ',') /* then trailing commas */
820 endt++;
821
822 begw = endt; /* start of conv keyword */
823
824 if (!*begw) {
825 /* none ? skip to next string */
826 (*idx)++;
827 begw = str[*idx];
828 if (!begw || !*begw)
829 break;
830 }
831
832 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
833
834 free(ckw);
835 ckw = my_strndup(begw, endw - begw);
836
837 conv = find_sample_conv(begw, endw - begw);
838 if (!conv) {
839 /* we found an isolated keyword that we don't know, it's not ours */
840 if (begw == str[*idx])
841 break;
Willy Tarreau975c1782013-12-12 23:16:54 +0100842 memprintf(err_msg, "unknown conv method '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200843 goto out_error;
844 }
Emeric Brun107ca302010-01-04 16:16:05 +0100845
Willy Tarreau833cc792013-07-24 15:34:19 +0200846 endt = endw;
847 if (*endt == '(') {
848 /* look for the end of this term */
849 while (*endt && *endt != ')')
850 endt++;
851 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100852 memprintf(err_msg, "syntax error: missing ')' after conv keyword '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200853 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200854 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200855 }
856
857 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100858 memprintf(err_msg, "returns type of conv method '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100859 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200860 }
Emeric Brun107ca302010-01-04 16:16:05 +0100861
862 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200863 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100864 memprintf(err_msg, "conv method '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100865 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200866 }
Emeric Brun107ca302010-01-04 16:16:05 +0100867
868 prev_type = conv->out_type;
Willy Tarreau12785782012-04-27 21:37:17 +0200869 conv_expr = calloc(1, sizeof(struct sample_conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200870 if (!conv_expr)
871 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100872
873 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
874 conv_expr->conv = conv;
875
Willy Tarreau833cc792013-07-24 15:34:19 +0200876 if (endt != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200877 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200878
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200879 if (!conv->arg_mask) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100880 memprintf(err_msg, "conv method '%s' does not support any args", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200881 goto out_error;
882 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100883
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200884 al->kw = expr->fetch->kw;
885 al->conv = conv_expr->conv->kw;
Willy Tarreau975c1782013-12-12 23:16:54 +0100886 if (make_arg_list(endw + 1, endt - endw - 1, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
887 memprintf(err_msg, "invalid arg %d in conv method '%s' : %s", err_arg+1, ckw, *err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200888 goto out_error;
889 }
890
Willy Tarreau2e845be2012-10-19 19:49:09 +0200891 if (!conv_expr->arg_p)
892 conv_expr->arg_p = empty_arg_list;
893
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100894 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100895 memprintf(err_msg, "invalid args in conv method '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200896 goto out_error;
897 }
898 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200899 else if (ARGM(conv->arg_mask)) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100900 memprintf(err_msg, "missing args for conv method '%s'", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200901 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100902 }
903 }
Emeric Brun485479d2010-09-23 18:02:19 +0200904
Willy Tarreau833cc792013-07-24 15:34:19 +0200905 out:
906 free(fkw);
907 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100908 return expr;
909
910out_error:
Willy Tarreau12785782012-04-27 21:37:17 +0200911 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +0200912 expr = NULL;
913 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +0100914}
915
916/*
Willy Tarreau12785782012-04-27 21:37:17 +0200917 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200918 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +0200919 * Returns a pointer on a typed sample structure containing the result or NULL if
920 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +0100921 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +0200922 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200923 *
924 * Note: the fetch functions are required to properly set the return type. The
925 * conversion functions must do so too. However the cast functions do not need
926 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +0200927 *
928 * The caller may indicate in <opt> if it considers the result final or not.
929 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
930 * if the result is stable or not, according to the following table :
931 *
932 * return MAY_CHANGE FINAL Meaning for the sample
933 * NULL 0 * Not present and will never be (eg: header)
934 * NULL 1 0 Not present yet, could change (eg: POST param)
935 * NULL 1 1 Not present yet, will not change anymore
936 * smp 0 * Present and will not change (eg: header)
937 * smp 1 0 Present, may change (eg: request length)
938 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +0100939 */
Willy Tarreau12785782012-04-27 21:37:17 +0200940struct sample *sample_process(struct proxy *px, struct session *l4, void *l7,
941 unsigned int opt,
942 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +0100943{
Willy Tarreau12785782012-04-27 21:37:17 +0200944 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100945
Willy Tarreau18387e22013-07-25 12:02:38 +0200946 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +0200947 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +0200948 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +0200949 }
Emeric Brun107ca302010-01-04 16:16:05 +0100950
Willy Tarreauef38c392013-07-22 16:29:32 +0200951 if (!expr->fetch->process(px, l4, l7, opt, expr->arg_p, p, expr->fetch->kw))
Emeric Brun107ca302010-01-04 16:16:05 +0100952 return NULL;
953
Emeric Brun107ca302010-01-04 16:16:05 +0100954 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +0200955 /* we want to ensure that p->type can be casted into
956 * conv_expr->conv->in_type. We have 3 possibilities :
957 * - NULL => not castable.
958 * - c_none => nothing to do (let's optimize it)
959 * - other => apply cast and prepare to fail
960 */
Willy Tarreau12785782012-04-27 21:37:17 +0200961 if (!sample_casts[p->type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +0200962 return NULL;
963
Willy Tarreau12785782012-04-27 21:37:17 +0200964 if (sample_casts[p->type][conv_expr->conv->in_type] != c_none &&
965 !sample_casts[p->type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +0100966 return NULL;
967
Willy Tarreau12e50112012-04-25 17:21:49 +0200968 /* OK cast succeeded */
969
Willy Tarreau342acb42012-04-23 22:03:39 +0200970 if (!conv_expr->conv->process(conv_expr->arg_p, p))
Emeric Brun107ca302010-01-04 16:16:05 +0100971 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +0100972 }
973 return p;
974}
975
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +0100976/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200977 * Resolve all remaining arguments in proxy <p>. Returns the number of
978 * errors or 0 if everything is fine.
979 */
980int smp_resolve_args(struct proxy *p)
981{
982 struct arg_list *cur, *bak;
983 const char *ctx, *where;
984 const char *conv_ctx, *conv_pre, *conv_pos;
985 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +0100986 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200987 struct arg *arg;
988 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +0100989 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200990
991 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
992 struct proxy *px;
993 struct server *srv;
994 char *pname, *sname;
Willy Tarreau46947782015-01-19 19:00:58 +0100995 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200996
997 arg = cur->arg;
998
999 /* prepare output messages */
1000 conv_pre = conv_pos = conv_ctx = "";
1001 if (cur->conv) {
1002 conv_ctx = cur->conv;
1003 conv_pre = "conversion keyword '";
1004 conv_pos = "' for ";
1005 }
1006
1007 where = "in";
1008 ctx = "sample fetch keyword";
1009 switch (cur->ctx) {
Willy Tarreau46947782015-01-19 19:00:58 +01001010 case ARGC_STK: where = "in stick rule in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001011 case ARGC_TRK: where = "in tracking rule in"; break;
1012 case ARGC_LOG: where = "in log-format string in"; break;
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01001013 case ARGC_HRQ: where = "in http-request header format string in"; break;
1014 case ARGC_HRS: where = "in http-response header format string in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001015 case ARGC_UIF: where = "in unique-id-format string in"; break;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01001016 case ARGC_RDR: where = "in redirect format string in"; break;
Willy Tarreau3a4ac422014-06-13 16:17:14 +02001017 case ARGC_CAP: where = "in capture rule in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001018 case ARGC_ACL: ctx = "ACL keyword"; break;
1019 }
1020
1021 /* set a few default settings */
1022 px = p;
1023 pname = p->id;
1024
1025 switch (arg->type) {
1026 case ARGT_SRV:
1027 if (!arg->data.str.len) {
1028 Alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1029 cur->file, cur->line,
1030 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1031 cfgerr++;
1032 continue;
1033 }
1034
1035 /* we support two formats : "bck/srv" and "srv" */
1036 sname = strrchr(arg->data.str.str, '/');
1037
1038 if (sname) {
1039 *sname++ = '\0';
1040 pname = arg->data.str.str;
1041
1042 px = findproxy(pname, PR_CAP_BE);
1043 if (!px) {
1044 Alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1045 cur->file, cur->line, pname,
1046 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1047 cfgerr++;
1048 break;
1049 }
1050 }
1051 else
1052 sname = arg->data.str.str;
1053
1054 srv = findserver(px, sname);
1055 if (!srv) {
1056 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",
1057 cur->file, cur->line, sname, pname,
1058 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1059 cfgerr++;
1060 break;
1061 }
1062
1063 free(arg->data.str.str);
1064 arg->data.str.str = NULL;
1065 arg->unresolved = 0;
1066 arg->data.srv = srv;
1067 break;
1068
1069 case ARGT_FE:
1070 if (arg->data.str.len) {
1071 pname = arg->data.str.str;
1072 px = findproxy(pname, PR_CAP_FE);
1073 }
1074
1075 if (!px) {
1076 Alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1077 cur->file, cur->line, pname,
1078 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1079 cfgerr++;
1080 break;
1081 }
1082
1083 if (!(px->cap & PR_CAP_FE)) {
1084 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1085 cur->file, cur->line, pname,
1086 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1087 cfgerr++;
1088 break;
1089 }
1090
1091 free(arg->data.str.str);
1092 arg->data.str.str = NULL;
1093 arg->unresolved = 0;
1094 arg->data.prx = px;
1095 break;
1096
1097 case ARGT_BE:
1098 if (arg->data.str.len) {
1099 pname = arg->data.str.str;
1100 px = findproxy(pname, PR_CAP_BE);
1101 }
1102
1103 if (!px) {
1104 Alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1105 cur->file, cur->line, pname,
1106 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1107 cfgerr++;
1108 break;
1109 }
1110
1111 if (!(px->cap & PR_CAP_BE)) {
1112 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1113 cur->file, cur->line, pname,
1114 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1115 cfgerr++;
1116 break;
1117 }
1118
1119 free(arg->data.str.str);
1120 arg->data.str.str = NULL;
1121 arg->unresolved = 0;
1122 arg->data.prx = px;
1123 break;
1124
1125 case ARGT_TAB:
1126 if (arg->data.str.len) {
1127 pname = arg->data.str.str;
1128 px = find_stktable(pname);
1129 }
1130
1131 if (!px) {
1132 Alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1133 cur->file, cur->line, pname,
1134 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1135 cfgerr++;
1136 break;
1137 }
1138
1139 if (!px->table.size) {
1140 Alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1141 cur->file, cur->line, pname,
1142 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1143 cfgerr++;
1144 break;
1145 }
1146
1147 free(arg->data.str.str);
1148 arg->data.str.str = NULL;
1149 arg->unresolved = 0;
1150 arg->data.prx = px;
1151 break;
1152
1153 case ARGT_USR:
1154 if (!arg->data.str.len) {
1155 Alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1156 cur->file, cur->line,
1157 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1158 cfgerr++;
1159 break;
1160 }
1161
1162 if (p->uri_auth && p->uri_auth->userlist &&
1163 !strcmp(p->uri_auth->userlist->name, arg->data.str.str))
1164 ul = p->uri_auth->userlist;
1165 else
1166 ul = auth_find_userlist(arg->data.str.str);
1167
1168 if (!ul) {
1169 Alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1170 cur->file, cur->line, arg->data.str.str,
1171 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1172 cfgerr++;
1173 break;
1174 }
1175
1176 free(arg->data.str.str);
1177 arg->data.str.str = NULL;
1178 arg->unresolved = 0;
1179 arg->data.usr = ul;
1180 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001181
1182 case ARGT_REG:
1183 if (!arg->data.str.len) {
1184 Alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1185 cur->file, cur->line,
1186 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1187 cfgerr++;
1188 continue;
1189 }
1190
1191 reg = calloc(1, sizeof(*reg));
1192 if (!reg) {
1193 Alert("parsing [%s:%d] : not enough memory to build regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1194 cur->file, cur->line,
1195 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1196 cfgerr++;
1197 continue;
1198 }
1199
1200 rflags = 0;
1201 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1202 err = NULL;
1203
Willy Tarreaud817e462015-01-23 20:23:17 +01001204 if (!regex_comp(arg->data.str.str, reg, !(rflags & REG_ICASE), 1 /* capture substr */, &err)) {
Willy Tarreau46947782015-01-19 19:00:58 +01001205 Alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1206 cur->file, cur->line,
1207 arg->data.str.str,
1208 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
1209 cfgerr++;
1210 continue;
1211 }
1212
1213 free(arg->data.str.str);
1214 arg->data.str.str = NULL;
1215 arg->unresolved = 0;
1216 arg->data.reg = reg;
1217 break;
1218
1219
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001220 }
1221
1222 LIST_DEL(&cur->list);
1223 free(cur);
1224 } /* end of args processing */
1225
1226 return cfgerr;
1227}
1228
1229/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001230 * Process a fetch + format conversion as defined by the sample expression
1231 * <expr> on request or response considering the <opt> parameter. The output is
1232 * always of type string. If a stable sample can be fetched, or an unstable one
1233 * when <opt> contains SMP_OPT_FINAL, the sample is converted to a string and
1234 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1235 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1236 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1237 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001238 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1239 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1240 * still hope to get it after waiting longer, and is not converted to string.
1241 * The possible output combinations are the following :
1242 *
1243 * return MAY_CHANGE FINAL Meaning for the sample
1244 * NULL * * Not present and will never be (eg: header)
1245 * smp 0 * Final value converted (eg: header)
1246 * smp 1 0 Not present yet, may appear later (eg: header)
1247 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001248 */
1249struct sample *sample_fetch_string(struct proxy *px, struct session *l4, void *l7,
1250 unsigned int opt, struct sample_expr *expr)
1251{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001252 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001253
Willy Tarreau6c616e02014-06-25 16:56:41 +02001254 memset(smp, 0, sizeof(*smp));
1255
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001256 if (!sample_process(px, l4, l7, opt, expr, smp)) {
1257 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1258 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001259 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001260 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001261
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001262 if (!sample_casts[smp->type][SMP_T_STR])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001263 return NULL;
1264
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001265 if (!sample_casts[smp->type][SMP_T_STR](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001266 return NULL;
1267
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001268 smp->type = SMP_T_STR;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001269 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001270 return smp;
1271}
1272
Emeric Brun107ca302010-01-04 16:16:05 +01001273/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001274/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001275/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001276/*****************************************************************/
1277
Emeric Brun53d1a982014-04-30 18:21:37 +02001278static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp)
1279{
1280 struct chunk *trash = get_trash_chunk();
1281 int b64_len;
1282
1283 trash->len = 0;
1284 b64_len = a2base64(smp->data.str.str, smp->data.str.len, trash->str, trash->size);
1285 if (b64_len < 0)
1286 return 0;
1287
1288 trash->len = b64_len;
1289 smp->data.str = *trash;
1290 smp->type = SMP_T_STR;
1291 smp->flags &= ~SMP_F_CONST;
1292 return 1;
1293}
1294
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001295static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp)
1296{
1297 struct chunk *trash = get_trash_chunk();
1298 unsigned char c;
1299 int ptr = 0;
1300
1301 trash->len = 0;
1302 while (ptr < smp->data.str.len && trash->len <= trash->size - 2) {
1303 c = smp->data.str.str[ptr++];
1304 trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
1305 trash->str[trash->len++] = hextab[c & 0xF];
1306 }
1307 smp->data.str = *trash;
1308 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001309 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001310 return 1;
1311}
1312
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001313/* hashes the binary input into a 32-bit unsigned int */
1314static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp)
1315{
1316 smp->data.uint = hash_djb2(smp->data.str.str, smp->data.str.len);
1317 if (arg_p && arg_p->data.uint)
1318 smp->data.uint = full_hash(smp->data.uint);
1319 smp->type = SMP_T_UINT;
1320 return 1;
1321}
1322
Willy Tarreau12785782012-04-27 21:37:17 +02001323static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +01001324{
1325 int i;
1326
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001327 if (!smp_dup(smp))
1328 return 0;
1329
Willy Tarreau342acb42012-04-23 22:03:39 +02001330 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +02001331 return 0;
1332
Willy Tarreau342acb42012-04-23 22:03:39 +02001333 for (i = 0; i < smp->data.str.len; i++) {
1334 if ((smp->data.str.str[i] >= 'A') && (smp->data.str.str[i] <= 'Z'))
1335 smp->data.str.str[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001336 }
1337 return 1;
1338}
1339
Willy Tarreau12785782012-04-27 21:37:17 +02001340static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +01001341{
1342 int i;
1343
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001344 if (!smp_dup(smp))
1345 return 0;
1346
Willy Tarreau342acb42012-04-23 22:03:39 +02001347 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +02001348 return 0;
1349
Willy Tarreau342acb42012-04-23 22:03:39 +02001350 for (i = 0; i < smp->data.str.len; i++) {
1351 if ((smp->data.str.str[i] >= 'a') && (smp->data.str.str[i] <= 'z'))
1352 smp->data.str.str[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001353 }
1354 return 1;
1355}
1356
Willy Tarreauf9954102012-04-20 14:03:29 +02001357/* takes the netmask in arg_p */
Willy Tarreau12785782012-04-27 21:37:17 +02001358static int sample_conv_ipmask(const struct arg *arg_p, struct sample *smp)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001359{
Willy Tarreau342acb42012-04-23 22:03:39 +02001360 smp->data.ipv4.s_addr &= arg_p->data.ipv4.s_addr;
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001361 smp->type = SMP_T_IPV4;
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001362 return 1;
1363}
1364
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001365/* takes an UINT value on input supposed to represent the time since EPOCH,
1366 * adds an optional offset found in args[1] and emits a string representing
1367 * the local time in the format specified in args[1] using strftime().
1368 */
1369static int sample_conv_ltime(const struct arg *args, struct sample *smp)
1370{
1371 struct chunk *temp;
1372 time_t curr_date = smp->data.uint;
1373
1374 /* add offset */
1375 if (args[1].type == ARGT_SINT || args[1].type == ARGT_UINT)
1376 curr_date += args[1].data.sint;
1377
1378 temp = get_trash_chunk();
1379 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, localtime(&curr_date));
1380 smp->data.str = *temp;
1381 smp->type = SMP_T_STR;
1382 return 1;
1383}
1384
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001385/* hashes the binary input into a 32-bit unsigned int */
1386static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp)
1387{
1388 smp->data.uint = hash_sdbm(smp->data.str.str, smp->data.str.len);
1389 if (arg_p && arg_p->data.uint)
1390 smp->data.uint = full_hash(smp->data.uint);
1391 smp->type = SMP_T_UINT;
1392 return 1;
1393}
1394
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001395/* takes an UINT value on input supposed to represent the time since EPOCH,
1396 * adds an optional offset found in args[1] and emits a string representing
1397 * the UTC date in the format specified in args[1] using strftime().
1398 */
1399static int sample_conv_utime(const struct arg *args, struct sample *smp)
1400{
1401 struct chunk *temp;
1402 time_t curr_date = smp->data.uint;
1403
1404 /* add offset */
1405 if (args[1].type == ARGT_SINT || args[1].type == ARGT_UINT)
1406 curr_date += args[1].data.sint;
1407
1408 temp = get_trash_chunk();
1409 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, gmtime(&curr_date));
1410 smp->data.str = *temp;
1411 smp->type = SMP_T_STR;
1412 return 1;
1413}
1414
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001415/* hashes the binary input into a 32-bit unsigned int */
1416static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp)
1417{
1418 smp->data.uint = hash_wt6(smp->data.str.str, smp->data.str.len);
1419 if (arg_p && arg_p->data.uint)
1420 smp->data.uint = full_hash(smp->data.uint);
1421 smp->type = SMP_T_UINT;
1422 return 1;
1423}
1424
Willy Tarreau80599772015-01-20 19:35:24 +01001425/* hashes the binary input into a 32-bit unsigned int */
1426static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp)
1427{
1428 smp->data.uint = hash_crc32(smp->data.str.str, smp->data.str.len);
1429 if (arg_p && arg_p->data.uint)
1430 smp->data.uint = full_hash(smp->data.uint);
1431 smp->type = SMP_T_UINT;
1432 return 1;
1433}
1434
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001435/* This function escape special json characters. The returned string can be
1436 * safely set between two '"' and used as json string. The json string is
1437 * defined like this:
1438 *
1439 * any Unicode character except '"' or '\' or control character
1440 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1441 *
1442 * The enum input_type contain all the allowed mode for decoding the input
1443 * string.
1444 */
1445enum input_type {
1446 IT_ASCII = 0,
1447 IT_UTF8,
1448 IT_UTF8S,
1449 IT_UTF8P,
1450 IT_UTF8PS,
1451};
1452static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1453 const char *file, int line, char **err)
1454{
1455 if (!arg) {
1456 memprintf(err, "Unexpected empty arg list");
1457 return 0;
1458 }
1459
1460 if (arg->type != ARGT_STR) {
1461 memprintf(err, "Unexpected arg type");
1462 return 0;
1463 }
1464
1465 if (strcmp(arg->data.str.str, "") == 0) {
1466 arg->type = ARGT_UINT;
1467 arg->data.uint = IT_ASCII;
1468 return 1;
1469 }
1470
1471 else if (strcmp(arg->data.str.str, "ascii") == 0) {
1472 arg->type = ARGT_UINT;
1473 arg->data.uint = IT_ASCII;
1474 return 1;
1475 }
1476
1477 else if (strcmp(arg->data.str.str, "utf8") == 0) {
1478 arg->type = ARGT_UINT;
1479 arg->data.uint = IT_UTF8;
1480 return 1;
1481 }
1482
1483 else if (strcmp(arg->data.str.str, "utf8s") == 0) {
1484 arg->type = ARGT_UINT;
1485 arg->data.uint = IT_UTF8S;
1486 return 1;
1487 }
1488
1489 else if (strcmp(arg->data.str.str, "utf8p") == 0) {
1490 arg->type = ARGT_UINT;
1491 arg->data.uint = IT_UTF8P;
1492 return 1;
1493 }
1494
1495 else if (strcmp(arg->data.str.str, "utf8ps") == 0) {
1496 arg->type = ARGT_UINT;
1497 arg->data.uint = IT_UTF8PS;
1498 return 1;
1499 }
1500
1501 memprintf(err, "Unexpected input code type at file '%s', line %d. "
1502 "Allowed value are 'ascii', 'utf8', 'utf8p' and 'utf8pp'", file, line);
1503 return 0;
1504}
1505
1506static int sample_conv_json(const struct arg *arg_p, struct sample *smp)
1507{
1508 struct chunk *temp;
1509 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1510 const char *str;
1511 int len;
1512 enum input_type input_type = IT_ASCII;
1513 unsigned int c;
1514 unsigned int ret;
1515 char *p;
1516
1517 if (arg_p)
1518 input_type = arg_p->data.uint;
1519
1520 temp = get_trash_chunk();
1521 temp->len = 0;
1522
1523 p = smp->data.str.str;
1524 while (p < smp->data.str.str + smp->data.str.len) {
1525
1526 if (input_type == IT_ASCII) {
1527 /* Read input as ASCII. */
1528 c = *(unsigned char *)p;
1529 p++;
1530 }
1531 else {
1532 /* Read input as UTF8. */
1533 ret = utf8_next(p, smp->data.str.len - ( p - smp->data.str.str ), &c);
1534 p += utf8_return_length(ret);
1535
1536 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
1537 return 0;
1538 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
1539 continue;
1540 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1541 return 0;
1542 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1543 continue;
1544
1545 /* Check too big values. */
1546 if ((unsigned int)c > 0xffff) {
1547 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
1548 return 0;
1549 continue;
1550 }
1551 }
1552
1553 /* Convert character. */
1554 if (c == '"') {
1555 len = 2;
1556 str = "\\\"";
1557 }
1558 else if (c == '\\') {
1559 len = 2;
1560 str = "\\\\";
1561 }
1562 else if (c == '/') {
1563 len = 2;
1564 str = "\\/";
1565 }
1566 else if (c == '\b') {
1567 len = 2;
1568 str = "\\b";
1569 }
1570 else if (c == '\f') {
1571 len = 2;
1572 str = "\\f";
1573 }
1574 else if (c == '\r') {
1575 len = 2;
1576 str = "\\r";
1577 }
1578 else if (c == '\n') {
1579 len = 2;
1580 str = "\\n";
1581 }
1582 else if (c == '\t') {
1583 len = 2;
1584 str = "\\t";
1585 }
1586 else if (c > 0xff || !isprint(c)) {
1587 /* isprint generate a segfault if c is too big. The man says that
1588 * c must have the value of an unsigned char or EOF.
1589 */
1590 len = 6;
1591 _str[0] = '\\';
1592 _str[1] = 'u';
1593 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
1594 str = _str;
1595 }
1596 else {
1597 len = 1;
1598 str = (char *)&c;
1599 }
1600
1601 /* Check length */
1602 if (temp->len + len > temp->size)
1603 return 0;
1604
1605 /* Copy string. */
1606 memcpy(temp->str + temp->len, str, len);
1607 temp->len += len;
1608 }
1609
1610 smp->flags &= ~SMP_F_CONST;
1611 smp->data.str = *temp;
1612 smp->type = SMP_T_STR;
1613
1614 return 1;
1615}
1616
Emeric Brun54c4ac82014-11-03 15:32:43 +01001617/* This sample function is designed to extract some bytes from an input buffer.
1618 * First arg is the offset.
1619 * Optional second arg is the length to truncate */
1620static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp)
1621{
1622 if (smp->data.str.len <= arg_p[0].data.uint) {
1623 smp->data.str.len = 0;
1624 return 1;
1625 }
1626
1627 if (smp->data.str.size)
1628 smp->data.str.size -= arg_p[0].data.uint;
1629 smp->data.str.len -= arg_p[0].data.uint;
1630 smp->data.str.str += arg_p[0].data.uint;
1631
1632 if ((arg_p[1].type == ARGT_UINT) && (arg_p[1].data.uint < smp->data.str.len))
1633 smp->data.str.len = arg_p[1].data.uint;
1634
1635 return 1;
1636}
1637
Emeric Brunf399b0d2014-11-03 17:07:03 +01001638static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
1639 const char *file, int line, char **err)
1640{
1641 struct arg *arg = args;
1642
1643 if (!arg) {
1644 memprintf(err, "Unexpected empty arg list");
1645 return 0;
1646 }
1647
1648 if (arg->type != ARGT_UINT) {
1649 memprintf(err, "Unexpected arg type");
1650 return 0;
1651 }
1652
1653 if (!arg->data.uint) {
1654 memprintf(err, "Unexpected value 0 for index");
1655 return 0;
1656 }
1657
1658 arg++;
1659
1660 if (arg->type != ARGT_STR) {
1661 memprintf(err, "Unexpected arg type");
1662 return 0;
1663 }
1664
1665 if (!arg->data.str.len) {
1666 memprintf(err, "Empty separators list");
1667 return 0;
1668 }
1669
1670 return 1;
1671}
1672
1673/* This sample function is designed to a return selected part of a string (field).
1674 * First arg is the index of the field (start at 1)
1675 * Second arg is a char list of separators (type string)
1676 */
1677static int sample_conv_field(const struct arg *arg_p, struct sample *smp)
1678{
1679 unsigned int field;
1680 char *start, *end;
1681 int i;
1682
1683 if (!arg_p[0].data.uint)
1684 return 0;
1685
1686 field = 1;
1687 end = start = smp->data.str.str;
1688 while (end - smp->data.str.str < smp->data.str.len) {
1689
1690 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1691 if (*end == arg_p[1].data.str.str[i]) {
1692 if (field == arg_p[0].data.uint)
1693 goto found;
1694 start = end+1;
1695 field++;
1696 break;
1697 }
1698 }
1699 end++;
1700 }
1701
1702 /* Field not found */
1703 if (field != arg_p[0].data.uint) {
1704 smp->data.str.len = 0;
1705 return 1;
1706 }
1707found:
1708 smp->data.str.len = end - start;
1709 /* If ret string is len 0, no need to
1710 change pointers or to update size */
1711 if (!smp->data.str.len)
1712 return 1;
1713
1714 smp->data.str.str = start;
1715
1716 /* Compute remaining size if needed
1717 Note: smp->data.str.size cannot be set to 0 */
1718 if (smp->data.str.size)
1719 smp->data.str.size -= start - smp->data.str.str;
1720
1721 return 1;
1722}
1723
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001724/* This sample function is designed to return a word from a string.
1725 * First arg is the index of the word (start at 1)
1726 * Second arg is a char list of words separators (type string)
1727 */
1728static int sample_conv_word(const struct arg *arg_p, struct sample *smp)
1729{
1730 unsigned int word;
1731 char *start, *end;
1732 int i, issep, inword;
1733
1734 if (!arg_p[0].data.uint)
1735 return 0;
1736
1737 word = 0;
1738 inword = 0;
1739 end = start = smp->data.str.str;
1740 while (end - smp->data.str.str < smp->data.str.len) {
1741 issep = 0;
1742 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1743 if (*end == arg_p[1].data.str.str[i]) {
1744 issep = 1;
1745 break;
1746 }
1747 }
1748 if (!inword) {
1749 if (!issep) {
1750 word++;
1751 start = end;
1752 inword = 1;
1753 }
1754 }
1755 else if (issep) {
1756 if (word == arg_p[0].data.uint)
1757 goto found;
1758 inword = 0;
1759 }
1760 end++;
1761 }
1762
1763 /* Field not found */
1764 if (word != arg_p[0].data.uint) {
1765 smp->data.str.len = 0;
1766 return 1;
1767 }
1768found:
1769 smp->data.str.len = end - start;
1770 /* If ret string is len 0, no need to
1771 change pointers or to update size */
1772 if (!smp->data.str.len)
1773 return 1;
1774
1775 smp->data.str.str = start;
1776
1777 /* Compute remaining size if needed
1778 Note: smp->data.str.size cannot be set to 0 */
1779 if (smp->data.str.size)
1780 smp->data.str.size -= start - smp->data.str.str;
1781
1782 return 1;
1783}
1784
Willy Tarreau7eda8492015-01-20 19:47:06 +01001785static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
1786 const char *file, int line, char **err)
1787{
1788 struct arg *arg = args;
1789 char *p;
1790 int len;
1791
1792 /* arg0 is a regex, it uses type_flag for ICASE and global match */
1793 arg[0].type_flags = 0;
1794
1795 if (arg[2].type != ARGT_STR)
1796 return 1;
1797
1798 p = arg[2].data.str.str;
1799 len = arg[2].data.str.len;
1800 while (len) {
1801 if (*p == 'i') {
1802 arg[0].type_flags |= ARGF_REG_ICASE;
1803 }
1804 else if (*p == 'g') {
1805 arg[0].type_flags |= ARGF_REG_GLOB;
1806 }
1807 else {
1808 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
1809 return 0;
1810 }
1811 p++;
1812 len--;
1813 }
1814 return 1;
1815}
1816
1817/* This sample function is designed to do the equivalent of s/match/replace/ on
1818 * the input string. It applies a regex and restarts from the last matched
1819 * location until nothing matches anymore. First arg is the regex to apply to
1820 * the input string, second arg is the replacement expression.
1821 */
1822static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp)
1823{
1824 char *start, *end;
1825 struct my_regex *reg = arg_p[0].data.reg;
1826 regmatch_t pmatch[MAX_MATCH];
1827 struct chunk *trash = get_trash_chunk();
1828 int flag, max;
1829 int found;
1830
1831 start = smp->data.str.str;
1832 end = start + smp->data.str.len;
1833
1834 flag = 0;
1835 while (1) {
1836 /* check for last round which is used to copy remaining parts
1837 * when not running in global replacement mode.
1838 */
1839 found = 0;
1840 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
1841 /* Note: we can have start == end on empty strings or at the end */
1842 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
1843 }
1844
1845 if (!found)
1846 pmatch[0].rm_so = end - start;
1847
1848 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
1849 max = trash->size - trash->len;
1850 if (max && pmatch[0].rm_so > 0) {
1851 if (max > pmatch[0].rm_so)
1852 max = pmatch[0].rm_so;
1853 memcpy(trash->str + trash->len, start, max);
1854 trash->len += max;
1855 }
1856
1857 if (!found)
1858 break;
1859
1860 /* replace the matching part */
1861 max = trash->size - trash->len;
1862 if (max) {
1863 if (max > arg_p[1].data.str.len)
1864 max = arg_p[1].data.str.len;
1865 memcpy(trash->str + trash->len, arg_p[1].data.str.str, max);
1866 trash->len += max;
1867 }
1868
1869 /* stop here if we're done with this string */
1870 if (start >= end)
1871 break;
1872
1873 /* We have a special case for matches of length 0 (eg: "x*y*").
1874 * These ones are considered to match in front of a character,
1875 * so we have to copy that character and skip to the next one.
1876 */
1877 if (!pmatch[0].rm_eo) {
1878 if (trash->len < trash->size)
1879 trash->str[trash->len++] = start[pmatch[0].rm_eo];
1880 pmatch[0].rm_eo++;
1881 }
1882
1883 start += pmatch[0].rm_eo;
1884 flag |= REG_NOTBOL;
1885 }
1886
1887 smp->data.str = *trash;
1888 return 1;
1889}
1890
Willy Tarreau97707872015-01-27 15:12:13 +01001891/* Takes a UINT on input, applies a binary twos complement and returns the UINT
1892 * result.
1893 */
1894static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp)
1895{
1896 smp->data.uint = ~smp->data.uint;
1897 return 1;
1898}
1899
1900/* Takes a UINT on input, applies a binary "and" with the UINT in arg_p, and
1901 * returns the UINT result.
1902 */
1903static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp)
1904{
1905 smp->data.uint &= arg_p->data.uint;
1906 return 1;
1907}
1908
1909/* Takes a UINT on input, applies a binary "or" with the UINT in arg_p, and
1910 * returns the UINT result.
1911 */
1912static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp)
1913{
1914 smp->data.uint |= arg_p->data.uint;
1915 return 1;
1916}
1917
1918/* Takes a UINT on input, applies a binary "xor" with the UINT in arg_p, and
1919 * returns the UINT result.
1920 */
1921static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp)
1922{
1923 smp->data.uint ^= arg_p->data.uint;
1924 return 1;
1925}
1926
1927/* Takes a UINT on input, applies an arithmetic "add" with the UINT in arg_p,
1928 * and returns the UINT result.
1929 */
1930static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp)
1931{
1932 smp->data.uint += arg_p->data.uint;
1933 return 1;
1934}
1935
1936/* Takes a UINT on input, applies an arithmetic "sub" with the UINT in arg_p,
1937 * and returns the UINT result.
1938 */
1939static int sample_conv_arith_sub(const struct arg *arg_p, struct sample *smp)
1940{
1941 smp->data.uint -= arg_p->data.uint;
1942 return 1;
1943}
1944
1945/* Takes a UINT on input, applies an arithmetic "mul" with the UINT in arg_p,
1946 * and returns the UINT result.
1947 */
1948static int sample_conv_arith_mul(const struct arg *arg_p, struct sample *smp)
1949{
1950 smp->data.uint *= arg_p->data.uint;
1951 return 1;
1952}
1953
1954/* Takes a UINT on input, applies an arithmetic "div" with the UINT in arg_p,
1955 * and returns the UINT result. If arg_p makes the result overflow, then the
1956 * largest possible quantity is returned.
1957 */
1958static int sample_conv_arith_div(const struct arg *arg_p, struct sample *smp)
1959{
1960 if (arg_p->data.uint)
1961 smp->data.uint /= arg_p->data.uint;
1962 else
1963 smp->data.uint = ~0;
1964 return 1;
1965}
1966
1967/* Takes a UINT on input, applies an arithmetic "mod" with the UINT in arg_p,
1968 * and returns the UINT result. If arg_p makes the result overflow, then zero
1969 * is returned.
1970 */
1971static int sample_conv_arith_mod(const struct arg *arg_p, struct sample *smp)
1972{
1973 if (arg_p->data.uint)
1974 smp->data.uint %= arg_p->data.uint;
1975 else
1976 smp->data.uint = 0;
1977 return 1;
1978}
1979
1980/* Takes an UINT on input, applies an arithmetic "neg" and returns the UINT
1981 * result.
1982 */
1983static int sample_conv_arith_neg(const struct arg *arg_p, struct sample *smp)
1984{
1985 smp->data.uint = -smp->data.uint;
1986 return 1;
1987}
1988
1989/* Takes a UINT on input, returns true is the value is non-null, otherwise
1990 * false. The output is a BOOL.
1991 */
1992static int sample_conv_arith_bool(const struct arg *arg_p, struct sample *smp)
1993{
1994 smp->data.uint = !!smp->data.uint;
1995 smp->type = SMP_T_BOOL;
1996 return 1;
1997}
1998
1999/* Takes a UINT on input, returns false is the value is non-null, otherwise
2000 * truee. The output is a BOOL.
2001 */
2002static int sample_conv_arith_not(const struct arg *arg_p, struct sample *smp)
2003{
2004 smp->data.uint = !smp->data.uint;
2005 smp->type = SMP_T_BOOL;
2006 return 1;
2007}
2008
2009/* Takes a UINT on input, returns true is the value is odd, otherwise false.
2010 * The output is a BOOL.
2011 */
2012static int sample_conv_arith_odd(const struct arg *arg_p, struct sample *smp)
2013{
2014 smp->data.uint = smp->data.uint & 1;
2015 smp->type = SMP_T_BOOL;
2016 return 1;
2017}
2018
2019/* Takes a UINT on input, returns true is the value is even, otherwise false.
2020 * The output is a BOOL.
2021 */
2022static int sample_conv_arith_even(const struct arg *arg_p, struct sample *smp)
2023{
2024 smp->data.uint = !(smp->data.uint & 1);
2025 smp->type = SMP_T_BOOL;
2026 return 1;
2027}
2028
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002029/************************************************************************/
2030/* All supported sample fetch functions must be declared here */
2031/************************************************************************/
2032
2033/* force TRUE to be returned at the fetch level */
2034static int
2035smp_fetch_true(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02002036 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002037{
2038 smp->type = SMP_T_BOOL;
2039 smp->data.uint = 1;
2040 return 1;
2041}
2042
2043/* force FALSE to be returned at the fetch level */
2044static int
2045smp_fetch_false(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02002046 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002047{
2048 smp->type = SMP_T_BOOL;
2049 smp->data.uint = 0;
2050 return 1;
2051}
2052
2053/* retrieve environment variable $1 as a string */
2054static int
2055smp_fetch_env(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02002056 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002057{
2058 char *env;
2059
2060 if (!args || args[0].type != ARGT_STR)
2061 return 0;
2062
2063 env = getenv(args[0].data.str.str);
2064 if (!env)
2065 return 0;
2066
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002067 smp->type = SMP_T_STR;
2068 smp->flags = SMP_F_CONST;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002069 smp->data.str.str = env;
2070 smp->data.str.len = strlen(env);
2071 return 1;
2072}
2073
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002074/* retrieve the current local date in epoch time, and applies an optional offset
2075 * of args[0] seconds.
2076 */
2077static int
2078smp_fetch_date(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02002079 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002080{
2081 smp->data.uint = date.tv_sec;
2082
2083 /* add offset */
2084 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
2085 smp->data.uint += args[0].data.sint;
2086
2087 smp->type = SMP_T_UINT;
2088 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2089 return 1;
2090}
2091
Willy Tarreau0f30d262014-11-24 16:02:05 +01002092/* returns the number of processes */
2093static int
2094smp_fetch_nbproc(struct proxy *px, struct session *s, void *l7, unsigned int opt,
2095 const struct arg *args, struct sample *smp, const char *kw)
2096{
2097 smp->type = SMP_T_UINT;
2098 smp->data.uint = global.nbproc;
2099 return 1;
2100}
2101
2102/* returns the number of the current process (between 1 and nbproc */
2103static int
2104smp_fetch_proc(struct proxy *px, struct session *s, void *l7, unsigned int opt,
2105 const struct arg *args, struct sample *smp, const char *kw)
2106{
2107 smp->type = SMP_T_UINT;
2108 smp->data.uint = relative_pid;
2109 return 1;
2110}
2111
Willy Tarreau84310e22014-02-14 11:59:04 +01002112/* generate a random 32-bit integer for whatever purpose, with an optional
2113 * range specified in argument.
2114 */
2115static int
2116smp_fetch_rand(struct proxy *px, struct session *s, void *l7, unsigned int opt,
2117 const struct arg *args, struct sample *smp, const char *kw)
2118{
2119 smp->data.uint = random();
2120
2121 /* reduce if needed. Don't do a modulo, use all bits! */
2122 if (args && args[0].type == ARGT_UINT)
Vincent Bernat1228dc02014-12-10 10:31:37 +01002123 smp->data.uint = ((uint64_t)smp->data.uint * args[0].data.uint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01002124
2125 smp->type = SMP_T_UINT;
2126 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2127 return 1;
2128}
2129
Willy Tarreau0f30d262014-11-24 16:02:05 +01002130/* returns true if the current process is stopping */
2131static int
2132smp_fetch_stopping(struct proxy *px, struct session *s, void *l7, unsigned int opt,
2133 const struct arg *args, struct sample *smp, const char *kw)
2134{
2135 smp->type = SMP_T_BOOL;
2136 smp->data.uint = stopping;
2137 return 1;
2138}
2139
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002140/* Note: must not be declared <const> as its list will be overwritten.
2141 * Note: fetches that may return multiple types must be declared as the lowest
2142 * common denominator, the type that can be casted into all other ones. For
2143 * instance IPv4/IPv6 must be declared IPv4.
2144 */
2145static struct sample_fetch_kw_list smp_kws = {ILH, {
2146 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
2147 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002148 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002149 { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01002150 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_UINT, SMP_USE_INTRN },
2151 { "proc", smp_fetch_proc, 0, NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreau84310e22014-02-14 11:59:04 +01002152 { "rand", smp_fetch_rand, ARG1(0,UINT), NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01002153 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002154 { /* END */ },
2155}};
2156
Emeric Brun107ca302010-01-04 16:16:05 +01002157/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002158static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Emeric Brun53d1a982014-04-30 18:21:37 +02002159 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02002160 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
2161 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002162 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02002163 { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 },
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002164 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR },
2165 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR },
Willy Tarreau80599772015-01-20 19:35:24 +01002166 { "crc32", sample_conv_crc32, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002167 { "djb2", sample_conv_djb2, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
2168 { "sdbm", sample_conv_sdbm, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
2169 { "wt6", sample_conv_wt6, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002170 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Emeric Brun54c4ac82014-11-03 15:32:43 +01002171 { "bytes", sample_conv_bytes, ARG2(1,UINT,UINT), NULL, SMP_T_BIN, SMP_T_BIN },
Emeric Brunf399b0d2014-11-03 17:07:03 +01002172 { "field", sample_conv_field, ARG2(2,UINT,STR), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002173 { "word", sample_conv_word, ARG2(2,UINT,STR), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
Willy Tarreau7eda8492015-01-20 19:47:06 +01002174 { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
Willy Tarreau97707872015-01-27 15:12:13 +01002175
2176 { "and", sample_conv_binary_and, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2177 { "or", sample_conv_binary_or, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2178 { "xor", sample_conv_binary_xor, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2179 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_UINT, SMP_T_UINT },
2180 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2181 { "not", sample_conv_arith_not, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2182 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2183 { "even", sample_conv_arith_even, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2184 { "add", sample_conv_arith_add, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2185 { "sub", sample_conv_arith_sub, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2186 { "mul", sample_conv_arith_mul, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2187 { "div", sample_conv_arith_div, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2188 { "mod", sample_conv_arith_mod, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2189 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_UINT, SMP_T_UINT },
2190
Willy Tarreau9fcb9842012-04-20 14:45:49 +02002191 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01002192}};
2193
2194__attribute__((constructor))
Willy Tarreau12785782012-04-27 21:37:17 +02002195static void __sample_init(void)
Emeric Brun107ca302010-01-04 16:16:05 +01002196{
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002197 /* register sample fetch and format conversion keywords */
2198 sample_register_fetches(&smp_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02002199 sample_register_convs(&sample_conv_kws);
Emeric Brun107ca302010-01-04 16:16:05 +01002200}