blob: 59c80b26cf3df6b8eb75a19d5c7706b2fa52c915 [file] [log] [blame]
Emeric Brun107ca302010-01-04 16:16:05 +01001/*
Willy Tarreaucd3b0942012-04-27 21:52:18 +02002 * Sample management functions.
Emeric Brun107ca302010-01-04 16:16:05 +01003 *
4 * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreaucd3b0942012-04-27 21:52:18 +02005 * Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
Emeric Brun107ca302010-01-04 16:16:05 +01006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <string.h>
15#include <arpa/inet.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020016#include <stdio.h>
Emeric Brun107ca302010-01-04 16:16:05 +010017
Willy Tarreau7e2c6472012-10-29 20:44:36 +010018#include <types/global.h>
19
Willy Tarreauc7e42382012-08-24 19:22:53 +020020#include <common/chunk.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020021#include <common/standard.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020022#include <common/uri_auth.h>
Emeric Brun53d1a982014-04-30 18:21:37 +020023#include <common/base64.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020024
Willy Tarreau9fcb9842012-04-20 14:45:49 +020025#include <proto/arg.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020026#include <proto/auth.h>
27#include <proto/log.h>
Thierry FOURNIERd4373142013-12-17 01:10:10 +010028#include <proto/proto_http.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020029#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020030#include <proto/sample.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020031#include <proto/stick_table.h>
Emeric Brun107ca302010-01-04 16:16:05 +010032
Willy Tarreau1cf8f082014-02-07 12:14:54 +010033/* sample type names */
34const char *smp_to_type[SMP_TYPES] = {
35 [SMP_T_BOOL] = "bool",
36 [SMP_T_UINT] = "uint",
37 [SMP_T_SINT] = "sint",
38 [SMP_T_ADDR] = "addr",
39 [SMP_T_IPV4] = "ipv4",
40 [SMP_T_IPV6] = "ipv6",
41 [SMP_T_STR] = "str",
42 [SMP_T_BIN] = "bin",
Thierry FOURNIERc51fe0f2015-06-03 20:12:04 +020043 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010044};
45
Willy Tarreau12785782012-04-27 21:37:17 +020046/* static sample used in sample_process() when <p> is NULL */
Willy Tarreaub4a88f02012-04-23 21:35:11 +020047static struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010048
Willy Tarreau12785782012-04-27 21:37:17 +020049/* list head of all known sample fetch keywords */
50static struct sample_fetch_kw_list sample_fetches = {
51 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010052};
53
Willy Tarreau12785782012-04-27 21:37:17 +020054/* list head of all known sample format conversion keywords */
55static struct sample_conv_kw_list sample_convs = {
56 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010057};
58
Willy Tarreau80aca902013-01-07 15:42:20 +010059const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
60 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
61 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
62 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
63 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
64 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
65 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
66 SMP_VAL_FE_LOG_END),
67
68 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
69 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
70 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
71 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
72 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
73 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
74 SMP_VAL_FE_LOG_END),
75
76 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
77 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
78 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
79 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
80 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
81 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
82 SMP_VAL_FE_LOG_END),
83
84 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
85 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
86 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
87 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
88 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
89 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
90 SMP_VAL_FE_LOG_END),
91
92 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
93 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
94 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
95 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
96 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
97 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
98 SMP_VAL_FE_LOG_END),
99
100 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
101 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
102 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
103 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
104 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
105 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
106 SMP_VAL_FE_LOG_END),
107
108 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
109 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
110 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
111 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
112 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
113 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
114 SMP_VAL___________),
115
116 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
117 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
118 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
119 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
120 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
121 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
122 SMP_VAL___________),
123
124 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
125 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
126 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
127 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
128 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
129 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
130 SMP_VAL_FE_LOG_END),
131
132 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
133 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
134 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
135 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
136 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
137 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
138 SMP_VAL___________),
139
140 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
141 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
142 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
143 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
144 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
145 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
146 SMP_VAL_FE_LOG_END),
147
148 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
149 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
150 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
151 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
152 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
153 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
154 SMP_VAL_FE_LOG_END),
155
156 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
157 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
158 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
159 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
160 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
161 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
162 SMP_VAL_FE_LOG_END),
163
164 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
165 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
166 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
167 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
168 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
169 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
170 SMP_VAL_FE_LOG_END),
171
172 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
173 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
174 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
175 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
176 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
177 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
178 SMP_VAL___________),
179
180 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
181 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
182 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
183 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
184 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
185 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
186 SMP_VAL___________),
187
188 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
189 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
190 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
191 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
192 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
193 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
194 SMP_VAL_FE_LOG_END),
195
196 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
197 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
198 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
199 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
200 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
201 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
202 SMP_VAL___________),
203
204 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
205 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
206 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
207 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
208 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
209 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
210 SMP_VAL_FE_LOG_END),
211
212 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
213 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
214 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
215 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
216 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
217 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
218 SMP_VAL_FE_LOG_END),
219
220 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
221 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
222 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
223 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
224 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
225 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
226 SMP_VAL_FE_LOG_END),
227
228 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
229 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
230 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
231 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
232 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
233 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
234 SMP_VAL_FE_LOG_END),
235};
236
237static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
238 [SMP_SRC_INTRN] = "internal state",
239 [SMP_SRC_LISTN] = "listener",
240 [SMP_SRC_FTEND] = "frontend",
241 [SMP_SRC_L4CLI] = "client address",
242 [SMP_SRC_L5CLI] = "client-side connection",
243 [SMP_SRC_TRACK] = "track counters",
244 [SMP_SRC_L6REQ] = "request buffer",
245 [SMP_SRC_HRQHV] = "HTTP request headers",
246 [SMP_SRC_HRQHP] = "HTTP request",
247 [SMP_SRC_HRQBO] = "HTTP request body",
248 [SMP_SRC_BKEND] = "backend",
249 [SMP_SRC_SERVR] = "server",
250 [SMP_SRC_L4SRV] = "server address",
251 [SMP_SRC_L5SRV] = "server-side connection",
252 [SMP_SRC_L6RES] = "response buffer",
253 [SMP_SRC_HRSHV] = "HTTP response headers",
254 [SMP_SRC_HRSHP] = "HTTP response",
255 [SMP_SRC_HRSBO] = "HTTP response body",
256 [SMP_SRC_RQFIN] = "request buffer statistics",
257 [SMP_SRC_RSFIN] = "response buffer statistics",
258 [SMP_SRC_TXFIN] = "transaction statistics",
259 [SMP_SRC_SSFIN] = "session statistics",
260};
261
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100262static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
263 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
264 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
265 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
266 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
267 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
268 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
269 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
270 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
271 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
272 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
273 [SMP_CKP_BE_SRV_CON] = "server source selection",
274 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
275 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
276 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
277 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
278 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
279 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
280 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
281 [SMP_CKP_FE_LOG_END] = "logs",
282};
283
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100284/* This function returns the type of the data returned by the sample_expr.
285 * It assumes that the <expr> and all of its converters are properly
286 * initialized.
287 */
288inline
289int smp_expr_output_type(struct sample_expr *expr)
290{
291 struct sample_conv_expr *smp_expr;
292
293 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
294 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
295 return smp_expr->conv->out_type;
296 }
297 return expr->fetch->out_type;
298}
299
300
Willy Tarreau80aca902013-01-07 15:42:20 +0100301/* fill the trash with a comma-delimited list of source names for the <use> bit
302 * field which must be composed of a non-null set of SMP_USE_* flags. The return
303 * value is the pointer to the string in the trash buffer.
304 */
305const char *sample_src_names(unsigned int use)
306{
307 int bit;
308
309 trash.len = 0;
310 trash.str[0] = '\0';
311 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
312 if (!(use & ~((1 << bit) - 1)))
313 break; /* no more bits */
314
315 if (!(use & (1 << bit)))
316 continue; /* bit not set */
317
318 trash.len += snprintf(trash.str + trash.len, trash.size - trash.len, "%s%s",
319 (use & ((1 << bit) - 1)) ? "," : "",
320 fetch_src_names[bit]);
321 }
322 return trash.str;
323}
324
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100325/* return a pointer to the correct sample checkpoint name, or "unknown" when
326 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
327 * if set.
328 */
329const char *sample_ckp_names(unsigned int use)
330{
331 int bit;
332
333 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
334 if (use & (1 << bit))
335 return fetch_ckp_names[bit];
336 return "unknown sample check place, please report this bug";
337}
338
Emeric Brun107ca302010-01-04 16:16:05 +0100339/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100340 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
341 * for next parsing sessions. The fetch keywords capabilities are also computed
342 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100343 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100344void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100345{
Willy Tarreau80aca902013-01-07 15:42:20 +0100346 struct sample_fetch *sf;
347 int bit;
348
349 for (sf = kwl->kw; sf->kw != NULL; sf++) {
350 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
351 if (sf->use & (1 << bit))
352 sf->val |= fetch_cap[bit];
353 }
354 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100355}
356
357/*
Willy Tarreau12785782012-04-27 21:37:17 +0200358 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100359 * parsing sessions.
360 */
Willy Tarreau12785782012-04-27 21:37:17 +0200361void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100362{
Willy Tarreau12785782012-04-27 21:37:17 +0200363 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100364}
365
366/*
Willy Tarreau12785782012-04-27 21:37:17 +0200367 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100368 * string of <len> in buffer <kw>.
369 *
370 */
Willy Tarreau12785782012-04-27 21:37:17 +0200371struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100372{
373 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200374 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100375
Willy Tarreau12785782012-04-27 21:37:17 +0200376 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100377 for (index = 0; kwl->kw[index].kw != NULL; index++) {
378 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
379 kwl->kw[index].kw[len] == '\0')
380 return &kwl->kw[index];
381 }
382 }
383 return NULL;
384}
385
386/*
Willy Tarreau12785782012-04-27 21:37:17 +0200387 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100388 * string of <len> in buffer <kw>.
389 *
390 */
Willy Tarreau12785782012-04-27 21:37:17 +0200391struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100392{
393 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200394 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100395
Willy Tarreau12785782012-04-27 21:37:17 +0200396 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100397 for (index = 0; kwl->kw[index].kw != NULL; index++) {
398 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
399 kwl->kw[index].kw[len] == '\0')
400 return &kwl->kw[index];
401 }
402 }
403 return NULL;
404}
405
Emeric Brun107ca302010-01-04 16:16:05 +0100406/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200407/* Sample casts functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200408/* Note: these functions do *NOT* set the output type on the */
409/* sample, the caller is responsible for doing this on return. */
Emeric Brun107ca302010-01-04 16:16:05 +0100410/******************************************************************/
411
Willy Tarreau342acb42012-04-23 22:03:39 +0200412static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100413{
Willy Tarreau342acb42012-04-23 22:03:39 +0200414 smp->data.uint = ntohl(smp->data.ipv4.s_addr);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100415 smp->type = SMP_T_UINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100416 return 1;
417}
418
Willy Tarreau342acb42012-04-23 22:03:39 +0200419static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100420{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100421 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100422
Willy Tarreau342acb42012-04-23 22:03:39 +0200423 if (!inet_ntop(AF_INET, (void *)&smp->data.ipv4, trash->str, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100424 return 0;
425
426 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200427 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100428 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100429 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100430
431 return 1;
432}
433
Willy Tarreau342acb42012-04-23 22:03:39 +0200434static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100435{
Willy Tarreau342acb42012-04-23 22:03:39 +0200436 v4tov6(&smp->data.ipv6, &smp->data.ipv4);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100437 smp->type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100438 return 1;
439}
440
Willy Tarreau342acb42012-04-23 22:03:39 +0200441static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100442{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100443 struct chunk *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100444
Willy Tarreau342acb42012-04-23 22:03:39 +0200445 if (!inet_ntop(AF_INET6, (void *)&smp->data.ipv6, trash->str, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100446 return 0;
447
448 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200449 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100450 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100451 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100452 return 1;
453}
454
455/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200456static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100457{
Willy Tarreau342acb42012-04-23 22:03:39 +0200458 return v6tov4(&smp->data.ipv4, &smp->data.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100459}
460*/
461
Willy Tarreau342acb42012-04-23 22:03:39 +0200462static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100463{
Willy Tarreau342acb42012-04-23 22:03:39 +0200464 smp->data.ipv4.s_addr = htonl(smp->data.uint);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100465 smp->type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100466 return 1;
467}
468
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100469static int c_str2addr(struct sample *smp)
470{
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100471 if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4)) {
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100472 if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6))
473 return 0;
474 smp->type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100475 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100476 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100477 }
478 smp->type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100479 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100480 return 1;
481}
482
Willy Tarreau342acb42012-04-23 22:03:39 +0200483static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100484{
Willy Tarreau342acb42012-04-23 22:03:39 +0200485 if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100486 return 0;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100487 smp->type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100488 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100489 return 1;
490}
491
Willy Tarreau342acb42012-04-23 22:03:39 +0200492static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100493{
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100494 if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6))
495 return 0;
496 smp->type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100497 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100498 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100499}
500
Emeric Brun3e256e52014-11-03 18:17:10 +0100501/*
502 * The NULL char always enforces the end of string if it is met.
503 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100504 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200505static int c_bin2str(struct sample *smp)
506{
Emeric Brun3e256e52014-11-03 18:17:10 +0100507 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200508
Emeric Brun3e256e52014-11-03 18:17:10 +0100509 for (i = 0; i < smp->data.str.len; i++) {
510 if (!smp->data.str.str[i]) {
511 smp->data.str.len = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100512 break;
Emeric Brun3e256e52014-11-03 18:17:10 +0100513 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200514 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200515 return 1;
516}
517
Willy Tarreau342acb42012-04-23 22:03:39 +0200518static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100519{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100520 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100521 char *pos;
522
Willy Tarreau342acb42012-04-23 22:03:39 +0200523 pos = ultoa_r(smp->data.uint, trash->str, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100524
525 if (!pos)
526 return 0;
527
Emeric Brun485479d2010-09-23 18:02:19 +0200528 trash->size = trash->size - (pos - trash->str);
Emeric Brun107ca302010-01-04 16:16:05 +0100529 trash->str = pos;
530 trash->len = strlen(pos);
Willy Tarreau342acb42012-04-23 22:03:39 +0200531 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100532 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100533 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100534 return 1;
535}
536
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100537/* This function duplicates data and removes the flag "const". */
538int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200539{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100540 struct chunk *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200541
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100542 /* If the const flag is not set, we don't need to duplicate the
543 * pattern as it can be modified in place.
544 */
545 if (!(smp->flags & SMP_F_CONST))
546 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100547
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100548 switch (smp->type) {
549 case SMP_T_BOOL:
550 case SMP_T_UINT:
551 case SMP_T_SINT:
552 case SMP_T_ADDR:
553 case SMP_T_IPV4:
554 case SMP_T_IPV6:
555 /* These type are not const. */
556 break;
557 case SMP_T_STR:
558 case SMP_T_BIN:
559 /* Duplicate data. */
560 trash = get_trash_chunk();
561 trash->len = smp->data.str.len < trash->size ? smp->data.str.len : trash->size;
562 memcpy(trash->str, smp->data.str.str, trash->len);
563 smp->data.str = *trash;
564 break;
565 default:
566 /* Other cases are unexpected. */
567 return 0;
568 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100569
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100570 /* remove const flag */
571 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200572 return 1;
573}
574
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100575int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100576{
577 return 1;
578}
579
Willy Tarreau342acb42012-04-23 22:03:39 +0200580static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100581{
582 int i;
583 uint32_t ret = 0;
584
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100585 if (smp->data.str.len == 0)
586 return 0;
587
Willy Tarreau342acb42012-04-23 22:03:39 +0200588 for (i = 0; i < smp->data.str.len; i++) {
589 uint32_t val = smp->data.str.str[i] - '0';
Emeric Brun107ca302010-01-04 16:16:05 +0100590
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100591 if (val > 9) {
592 if (i == 0)
593 return 0;
Emeric Brun107ca302010-01-04 16:16:05 +0100594 break;
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100595 }
Emeric Brun107ca302010-01-04 16:16:05 +0100596
597 ret = ret * 10 + val;
598 }
599
Willy Tarreau342acb42012-04-23 22:03:39 +0200600 smp->data.uint = ret;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100601 smp->type = SMP_T_UINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100602 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100603 return 1;
604}
605
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100606static int c_str2meth(struct sample *smp)
607{
608 enum http_meth_t meth;
609 int len;
610
611 meth = find_http_meth(smp->data.str.str, smp->data.str.len);
612 if (meth == HTTP_METH_OTHER) {
613 len = smp->data.str.len;
614 smp->data.meth.str.str = smp->data.str.str;
615 smp->data.meth.str.len = len;
616 }
617 else
618 smp->flags &= ~SMP_F_CONST;
619 smp->data.meth.meth = meth;
620 smp->type = SMP_T_METH;
621 return 1;
622}
623
624static int c_meth2str(struct sample *smp)
625{
626 int len;
627 enum http_meth_t meth;
628
629 if (smp->data.meth.meth == HTTP_METH_OTHER) {
630 /* The method is unknown. Copy the original pointer. */
631 len = smp->data.meth.str.len;
632 smp->data.str.str = smp->data.meth.str.str;
633 smp->data.str.len = len;
634 smp->type = SMP_T_STR;
635 }
636 else if (smp->data.meth.meth < HTTP_METH_OTHER) {
637 /* The method is known, copy the pointer containing the string. */
638 meth = smp->data.meth.meth;
639 smp->data.str.str = http_known_methods[meth].name;
640 smp->data.str.len = http_known_methods[meth].len;
641 smp->flags |= SMP_F_CONST;
642 smp->type = SMP_T_STR;
643 }
644 else {
645 /* Unknown method */
646 return 0;
647 }
648 return 1;
649}
650
Emeric Brun107ca302010-01-04 16:16:05 +0100651/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200652/* Sample casts matrix: */
653/* sample_casts[from type][to type] */
654/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100655/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100656
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100657sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100658/* to: BOOL UINT SINT ADDR IPV4 IPV6 STR BIN METH */
659/* from: BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
660/* UINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_int2str, NULL, NULL, },
661/* SINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_int2str, NULL, NULL, },
662/* ADDR */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
663/* IPV4 */ { NULL, c_ip2int, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, NULL, NULL, },
664/* IPV6 */ { NULL, NULL, NULL, c_none, NULL, c_none, c_ipv62str, NULL, NULL, },
665/* STR */ { c_str2int, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
666/* BIN */ { NULL, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
667/* METH */ { NULL, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, },
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200668};
Emeric Brun107ca302010-01-04 16:16:05 +0100669
Emeric Brun107ca302010-01-04 16:16:05 +0100670/*
Willy Tarreau12785782012-04-27 21:37:17 +0200671 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100672 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200673 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200674 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100675 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100676struct 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 +0100677{
Willy Tarreau833cc792013-07-24 15:34:19 +0200678 const char *begw; /* beginning of word */
679 const char *endw; /* end of word */
680 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200681 struct sample_expr *expr;
682 struct sample_fetch *fetch;
683 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100684 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200685 char *fkw = NULL;
686 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100687 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100688
Willy Tarreau833cc792013-07-24 15:34:19 +0200689 begw = str[*idx];
690 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
691
692 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100693 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100694 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200695 }
Emeric Brun107ca302010-01-04 16:16:05 +0100696
Willy Tarreau833cc792013-07-24 15:34:19 +0200697 /* keep a copy of the current fetch keyword for error reporting */
698 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100699
Willy Tarreau833cc792013-07-24 15:34:19 +0200700 fetch = find_sample_fetch(begw, endw - begw);
701 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100702 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100703 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200704 }
Emeric Brun107ca302010-01-04 16:16:05 +0100705
Willy Tarreau833cc792013-07-24 15:34:19 +0200706 endt = endw;
707 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100708 /* look for the end of this term and skip the opening parenthesis */
709 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200710 while (*endt && *endt != ')')
711 endt++;
712 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100713 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200714 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200715 }
Emeric Brun485479d2010-09-23 18:02:19 +0200716 }
Emeric Brun107ca302010-01-04 16:16:05 +0100717
Willy Tarreau833cc792013-07-24 15:34:19 +0200718 /* At this point, we have :
719 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100720 * - endw : end of the keyword, first character not part of keyword
721 * nor the opening parenthesis (so first character of args
722 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200723 * - endt : end of the term (=endw or last parenthesis if args are present)
724 */
725
726 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100727 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
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 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200731
Willy Tarreau12785782012-04-27 21:37:17 +0200732 expr = calloc(1, sizeof(struct sample_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200733 if (!expr)
734 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100735
736 LIST_INIT(&(expr->conv_exprs));
737 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200738 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100739
Willy Tarreau689a1df2013-12-13 00:40:11 +0100740 /* Note that we call the argument parser even with an empty string,
741 * this allows it to automatically create entries for mandatory
742 * implicit arguments (eg: local proxy name).
743 */
744 al->kw = expr->fetch->kw;
745 al->conv = NULL;
746 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
747 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
748 goto out_error;
749 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200750
Willy Tarreau689a1df2013-12-13 00:40:11 +0100751 if (!expr->arg_p) {
752 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200753 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100754 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
755 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200756 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100757 }
758
Willy Tarreau833cc792013-07-24 15:34:19 +0200759 /* Now process the converters if any. We have two supported syntaxes
760 * for the converters, which can be combined :
761 * - comma-delimited list of converters just after the keyword and args ;
762 * - one converter per keyword
763 * The combination allows to have each keyword being a comma-delimited
764 * series of converters.
765 *
766 * We want to process the former first, then the latter. For this we start
767 * from the beginning of the supposed place in the exiting conv chain, which
768 * starts at the last comma (endt).
769 */
770
771 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200772 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100773
Willy Tarreau833cc792013-07-24 15:34:19 +0200774 if (*endt == ')') /* skip last closing parenthesis */
775 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100776
Willy Tarreau833cc792013-07-24 15:34:19 +0200777 if (*endt && *endt != ',') {
778 if (ckw)
Willy Tarreau975c1782013-12-12 23:16:54 +0100779 memprintf(err_msg, "missing comma after conv keyword '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200780 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100781 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100782 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200783 }
Emeric Brun107ca302010-01-04 16:16:05 +0100784
Willy Tarreau833cc792013-07-24 15:34:19 +0200785 while (*endt == ',') /* then trailing commas */
786 endt++;
787
788 begw = endt; /* start of conv keyword */
789
790 if (!*begw) {
791 /* none ? skip to next string */
792 (*idx)++;
793 begw = str[*idx];
794 if (!begw || !*begw)
795 break;
796 }
797
798 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
799
800 free(ckw);
801 ckw = my_strndup(begw, endw - begw);
802
803 conv = find_sample_conv(begw, endw - begw);
804 if (!conv) {
805 /* we found an isolated keyword that we don't know, it's not ours */
806 if (begw == str[*idx])
807 break;
Willy Tarreau975c1782013-12-12 23:16:54 +0100808 memprintf(err_msg, "unknown conv method '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200809 goto out_error;
810 }
Emeric Brun107ca302010-01-04 16:16:05 +0100811
Willy Tarreau833cc792013-07-24 15:34:19 +0200812 endt = endw;
813 if (*endt == '(') {
814 /* look for the end of this term */
815 while (*endt && *endt != ')')
816 endt++;
817 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100818 memprintf(err_msg, "syntax error: missing ')' after conv keyword '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200819 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200820 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200821 }
822
823 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100824 memprintf(err_msg, "returns type of conv method '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100825 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200826 }
Emeric Brun107ca302010-01-04 16:16:05 +0100827
828 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200829 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100830 memprintf(err_msg, "conv method '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100831 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200832 }
Emeric Brun107ca302010-01-04 16:16:05 +0100833
834 prev_type = conv->out_type;
Willy Tarreau12785782012-04-27 21:37:17 +0200835 conv_expr = calloc(1, sizeof(struct sample_conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200836 if (!conv_expr)
837 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100838
839 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
840 conv_expr->conv = conv;
841
Willy Tarreau833cc792013-07-24 15:34:19 +0200842 if (endt != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200843 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200844
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200845 if (!conv->arg_mask) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100846 memprintf(err_msg, "conv method '%s' does not support any args", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200847 goto out_error;
848 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100849
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200850 al->kw = expr->fetch->kw;
851 al->conv = conv_expr->conv->kw;
Willy Tarreau975c1782013-12-12 23:16:54 +0100852 if (make_arg_list(endw + 1, endt - endw - 1, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
853 memprintf(err_msg, "invalid arg %d in conv method '%s' : %s", err_arg+1, ckw, *err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200854 goto out_error;
855 }
856
Willy Tarreau2e845be2012-10-19 19:49:09 +0200857 if (!conv_expr->arg_p)
858 conv_expr->arg_p = empty_arg_list;
859
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100860 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100861 memprintf(err_msg, "invalid args in conv method '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200862 goto out_error;
863 }
864 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200865 else if (ARGM(conv->arg_mask)) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100866 memprintf(err_msg, "missing args for conv method '%s'", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200867 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100868 }
869 }
Emeric Brun485479d2010-09-23 18:02:19 +0200870
Willy Tarreau833cc792013-07-24 15:34:19 +0200871 out:
872 free(fkw);
873 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100874 return expr;
875
876out_error:
Willy Tarreau12785782012-04-27 21:37:17 +0200877 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +0200878 expr = NULL;
879 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +0100880}
881
882/*
Willy Tarreau12785782012-04-27 21:37:17 +0200883 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200884 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +0200885 * Returns a pointer on a typed sample structure containing the result or NULL if
886 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +0100887 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +0200888 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200889 *
890 * Note: the fetch functions are required to properly set the return type. The
891 * conversion functions must do so too. However the cast functions do not need
892 * to since they're made to cast mutiple types according to what is required.
Willy Tarreauf94735e2014-07-30 08:56:35 +0200893 *
894 * The caller may indicate in <opt> if it considers the result final or not.
895 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
896 * if the result is stable or not, according to the following table :
897 *
898 * return MAY_CHANGE FINAL Meaning for the sample
899 * NULL 0 * Not present and will never be (eg: header)
900 * NULL 1 0 Not present yet, could change (eg: POST param)
901 * NULL 1 1 Not present yet, will not change anymore
902 * smp 0 * Present and will not change (eg: header)
903 * smp 1 0 Present, may change (eg: request length)
904 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +0100905 */
Willy Tarreau12785782012-04-27 21:37:17 +0200906struct sample *sample_process(struct proxy *px, struct session *l4, void *l7,
907 unsigned int opt,
908 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +0100909{
Willy Tarreau12785782012-04-27 21:37:17 +0200910 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100911
Willy Tarreau18387e22013-07-25 12:02:38 +0200912 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +0200913 p = &temp_smp;
Willy Tarreaua4ba9db2014-06-25 16:56:41 +0200914 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +0200915 }
Emeric Brun107ca302010-01-04 16:16:05 +0100916
Willy Tarreauef38c392013-07-22 16:29:32 +0200917 if (!expr->fetch->process(px, l4, l7, opt, expr->arg_p, p, expr->fetch->kw))
Emeric Brun107ca302010-01-04 16:16:05 +0100918 return NULL;
919
Emeric Brun107ca302010-01-04 16:16:05 +0100920 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +0200921 /* we want to ensure that p->type can be casted into
922 * conv_expr->conv->in_type. We have 3 possibilities :
923 * - NULL => not castable.
924 * - c_none => nothing to do (let's optimize it)
925 * - other => apply cast and prepare to fail
926 */
Willy Tarreau12785782012-04-27 21:37:17 +0200927 if (!sample_casts[p->type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +0200928 return NULL;
929
Willy Tarreau12785782012-04-27 21:37:17 +0200930 if (sample_casts[p->type][conv_expr->conv->in_type] != c_none &&
931 !sample_casts[p->type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +0100932 return NULL;
933
Willy Tarreau12e50112012-04-25 17:21:49 +0200934 /* OK cast succeeded */
935
Willy Tarreau342acb42012-04-23 22:03:39 +0200936 if (!conv_expr->conv->process(conv_expr->arg_p, p))
Emeric Brun107ca302010-01-04 16:16:05 +0100937 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +0100938 }
939 return p;
940}
941
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +0100942/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200943 * Resolve all remaining arguments in proxy <p>. Returns the number of
944 * errors or 0 if everything is fine.
945 */
946int smp_resolve_args(struct proxy *p)
947{
948 struct arg_list *cur, *bak;
949 const char *ctx, *where;
950 const char *conv_ctx, *conv_pre, *conv_pos;
951 struct userlist *ul;
952 struct arg *arg;
953 int cfgerr = 0;
954
955 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
956 struct proxy *px;
957 struct server *srv;
958 char *pname, *sname;
959
960 arg = cur->arg;
961
962 /* prepare output messages */
963 conv_pre = conv_pos = conv_ctx = "";
964 if (cur->conv) {
965 conv_ctx = cur->conv;
966 conv_pre = "conversion keyword '";
967 conv_pos = "' for ";
968 }
969
970 where = "in";
971 ctx = "sample fetch keyword";
972 switch (cur->ctx) {
973 case ARGC_STK:where = "in stick rule in"; break;
974 case ARGC_TRK: where = "in tracking rule in"; break;
975 case ARGC_LOG: where = "in log-format string in"; break;
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100976 case ARGC_HRQ: where = "in http-request header format string in"; break;
977 case ARGC_HRS: where = "in http-response header format string in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200978 case ARGC_UIF: where = "in unique-id-format string in"; break;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100979 case ARGC_RDR: where = "in redirect format string in"; break;
Willy Tarreau3a4ac422014-06-13 16:17:14 +0200980 case ARGC_CAP: where = "in capture rule in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200981 case ARGC_ACL: ctx = "ACL keyword"; break;
982 }
983
984 /* set a few default settings */
985 px = p;
986 pname = p->id;
987
988 switch (arg->type) {
989 case ARGT_SRV:
990 if (!arg->data.str.len) {
991 Alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
992 cur->file, cur->line,
993 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
994 cfgerr++;
995 continue;
996 }
997
998 /* we support two formats : "bck/srv" and "srv" */
999 sname = strrchr(arg->data.str.str, '/');
1000
1001 if (sname) {
1002 *sname++ = '\0';
1003 pname = arg->data.str.str;
1004
1005 px = findproxy(pname, PR_CAP_BE);
1006 if (!px) {
1007 Alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1008 cur->file, cur->line, pname,
1009 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1010 cfgerr++;
1011 break;
1012 }
1013 }
1014 else
1015 sname = arg->data.str.str;
1016
1017 srv = findserver(px, sname);
1018 if (!srv) {
1019 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",
1020 cur->file, cur->line, sname, pname,
1021 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1022 cfgerr++;
1023 break;
1024 }
1025
1026 free(arg->data.str.str);
1027 arg->data.str.str = NULL;
1028 arg->unresolved = 0;
1029 arg->data.srv = srv;
1030 break;
1031
1032 case ARGT_FE:
1033 if (arg->data.str.len) {
1034 pname = arg->data.str.str;
1035 px = findproxy(pname, PR_CAP_FE);
1036 }
1037
1038 if (!px) {
1039 Alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1040 cur->file, cur->line, pname,
1041 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1042 cfgerr++;
1043 break;
1044 }
1045
1046 if (!(px->cap & PR_CAP_FE)) {
1047 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1048 cur->file, cur->line, pname,
1049 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1050 cfgerr++;
1051 break;
1052 }
1053
1054 free(arg->data.str.str);
1055 arg->data.str.str = NULL;
1056 arg->unresolved = 0;
1057 arg->data.prx = px;
1058 break;
1059
1060 case ARGT_BE:
1061 if (arg->data.str.len) {
1062 pname = arg->data.str.str;
1063 px = findproxy(pname, PR_CAP_BE);
1064 }
1065
1066 if (!px) {
1067 Alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1068 cur->file, cur->line, pname,
1069 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1070 cfgerr++;
1071 break;
1072 }
1073
1074 if (!(px->cap & PR_CAP_BE)) {
1075 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1076 cur->file, cur->line, pname,
1077 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1078 cfgerr++;
1079 break;
1080 }
1081
1082 free(arg->data.str.str);
1083 arg->data.str.str = NULL;
1084 arg->unresolved = 0;
1085 arg->data.prx = px;
1086 break;
1087
1088 case ARGT_TAB:
1089 if (arg->data.str.len) {
1090 pname = arg->data.str.str;
1091 px = find_stktable(pname);
1092 }
1093
1094 if (!px) {
1095 Alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1096 cur->file, cur->line, pname,
1097 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1098 cfgerr++;
1099 break;
1100 }
1101
1102 if (!px->table.size) {
1103 Alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1104 cur->file, cur->line, pname,
1105 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1106 cfgerr++;
1107 break;
1108 }
1109
1110 free(arg->data.str.str);
1111 arg->data.str.str = NULL;
1112 arg->unresolved = 0;
1113 arg->data.prx = px;
1114 break;
1115
1116 case ARGT_USR:
1117 if (!arg->data.str.len) {
1118 Alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1119 cur->file, cur->line,
1120 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1121 cfgerr++;
1122 break;
1123 }
1124
1125 if (p->uri_auth && p->uri_auth->userlist &&
1126 !strcmp(p->uri_auth->userlist->name, arg->data.str.str))
1127 ul = p->uri_auth->userlist;
1128 else
1129 ul = auth_find_userlist(arg->data.str.str);
1130
1131 if (!ul) {
1132 Alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1133 cur->file, cur->line, arg->data.str.str,
1134 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1135 cfgerr++;
1136 break;
1137 }
1138
1139 free(arg->data.str.str);
1140 arg->data.str.str = NULL;
1141 arg->unresolved = 0;
1142 arg->data.usr = ul;
1143 break;
1144 }
1145
1146 LIST_DEL(&cur->list);
1147 free(cur);
1148 } /* end of args processing */
1149
1150 return cfgerr;
1151}
1152
1153/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001154 * Process a fetch + format conversion as defined by the sample expression
1155 * <expr> on request or response considering the <opt> parameter. The output is
1156 * always of type string. If a stable sample can be fetched, or an unstable one
1157 * when <opt> contains SMP_OPT_FINAL, the sample is converted to a string and
1158 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1159 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1160 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1161 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreauf94735e2014-07-30 08:56:35 +02001162 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1163 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1164 * still hope to get it after waiting longer, and is not converted to string.
1165 * The possible output combinations are the following :
1166 *
1167 * return MAY_CHANGE FINAL Meaning for the sample
1168 * NULL * * Not present and will never be (eg: header)
1169 * smp 0 * Final value converted (eg: header)
1170 * smp 1 0 Not present yet, may appear later (eg: header)
1171 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001172 */
1173struct sample *sample_fetch_string(struct proxy *px, struct session *l4, void *l7,
1174 unsigned int opt, struct sample_expr *expr)
1175{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001176 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001177
Willy Tarreaua4ba9db2014-06-25 16:56:41 +02001178 memset(smp, 0, sizeof(*smp));
1179
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001180 if (!sample_process(px, l4, l7, opt, expr, smp)) {
1181 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1182 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001183 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001184 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001185
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001186 if (!sample_casts[smp->type][SMP_T_STR])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001187 return NULL;
1188
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001189 if (!sample_casts[smp->type][SMP_T_STR](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001190 return NULL;
1191
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001192 smp->type = SMP_T_STR;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001193 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001194 return smp;
1195}
1196
Emeric Brun107ca302010-01-04 16:16:05 +01001197/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001198/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001199/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001200/*****************************************************************/
1201
Emeric Brun53d1a982014-04-30 18:21:37 +02001202static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp)
1203{
1204 struct chunk *trash = get_trash_chunk();
1205 int b64_len;
1206
1207 trash->len = 0;
1208 b64_len = a2base64(smp->data.str.str, smp->data.str.len, trash->str, trash->size);
1209 if (b64_len < 0)
1210 return 0;
1211
1212 trash->len = b64_len;
1213 smp->data.str = *trash;
1214 smp->type = SMP_T_STR;
1215 smp->flags &= ~SMP_F_CONST;
1216 return 1;
1217}
1218
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001219static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp)
1220{
1221 struct chunk *trash = get_trash_chunk();
1222 unsigned char c;
1223 int ptr = 0;
1224
1225 trash->len = 0;
1226 while (ptr < smp->data.str.len && trash->len <= trash->size - 2) {
1227 c = smp->data.str.str[ptr++];
1228 trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
1229 trash->str[trash->len++] = hextab[c & 0xF];
1230 }
1231 smp->data.str = *trash;
1232 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001233 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001234 return 1;
1235}
1236
Willy Tarreau12785782012-04-27 21:37:17 +02001237static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +01001238{
1239 int i;
1240
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001241 if (!smp_dup(smp))
1242 return 0;
1243
Willy Tarreau342acb42012-04-23 22:03:39 +02001244 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +02001245 return 0;
1246
Willy Tarreau342acb42012-04-23 22:03:39 +02001247 for (i = 0; i < smp->data.str.len; i++) {
1248 if ((smp->data.str.str[i] >= 'A') && (smp->data.str.str[i] <= 'Z'))
1249 smp->data.str.str[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001250 }
1251 return 1;
1252}
1253
Willy Tarreau12785782012-04-27 21:37:17 +02001254static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +01001255{
1256 int i;
1257
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001258 if (!smp_dup(smp))
1259 return 0;
1260
Willy Tarreau342acb42012-04-23 22:03:39 +02001261 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +02001262 return 0;
1263
Willy Tarreau342acb42012-04-23 22:03:39 +02001264 for (i = 0; i < smp->data.str.len; i++) {
1265 if ((smp->data.str.str[i] >= 'a') && (smp->data.str.str[i] <= 'z'))
1266 smp->data.str.str[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001267 }
1268 return 1;
1269}
1270
Willy Tarreauf9954102012-04-20 14:03:29 +02001271/* takes the netmask in arg_p */
Willy Tarreau12785782012-04-27 21:37:17 +02001272static int sample_conv_ipmask(const struct arg *arg_p, struct sample *smp)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001273{
Willy Tarreau342acb42012-04-23 22:03:39 +02001274 smp->data.ipv4.s_addr &= arg_p->data.ipv4.s_addr;
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001275 smp->type = SMP_T_IPV4;
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001276 return 1;
1277}
1278
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001279/************************************************************************/
1280/* All supported sample fetch functions must be declared here */
1281/************************************************************************/
1282
1283/* force TRUE to be returned at the fetch level */
1284static int
1285smp_fetch_true(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001286 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001287{
1288 smp->type = SMP_T_BOOL;
1289 smp->data.uint = 1;
1290 return 1;
1291}
1292
1293/* force FALSE to be returned at the fetch level */
1294static int
1295smp_fetch_false(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001296 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001297{
1298 smp->type = SMP_T_BOOL;
1299 smp->data.uint = 0;
1300 return 1;
1301}
1302
1303/* retrieve environment variable $1 as a string */
1304static int
1305smp_fetch_env(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001306 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001307{
1308 char *env;
1309
1310 if (!args || args[0].type != ARGT_STR)
1311 return 0;
1312
1313 env = getenv(args[0].data.str.str);
1314 if (!env)
1315 return 0;
1316
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001317 smp->type = SMP_T_STR;
1318 smp->flags = SMP_F_CONST;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001319 smp->data.str.str = env;
1320 smp->data.str.len = strlen(env);
1321 return 1;
1322}
1323
Willy Tarreau6236d3a2013-07-25 14:28:25 +02001324/* retrieve the current local date in epoch time, and applies an optional offset
1325 * of args[0] seconds.
1326 */
1327static int
1328smp_fetch_date(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001329 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02001330{
1331 smp->data.uint = date.tv_sec;
1332
1333 /* add offset */
1334 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
1335 smp->data.uint += args[0].data.sint;
1336
1337 smp->type = SMP_T_UINT;
1338 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
1339 return 1;
1340}
1341
Willy Tarreaufad7f072014-11-24 16:02:05 +01001342/* returns the number of processes */
1343static int
1344smp_fetch_nbproc(struct proxy *px, struct session *s, void *l7, unsigned int opt,
1345 const struct arg *args, struct sample *smp, const char *kw)
1346{
1347 smp->type = SMP_T_UINT;
1348 smp->data.uint = global.nbproc;
1349 return 1;
1350}
1351
1352/* returns the number of the current process (between 1 and nbproc */
1353static int
1354smp_fetch_proc(struct proxy *px, struct session *s, void *l7, unsigned int opt,
1355 const struct arg *args, struct sample *smp, const char *kw)
1356{
1357 smp->type = SMP_T_UINT;
1358 smp->data.uint = relative_pid;
1359 return 1;
1360}
1361
Willy Tarreau84310e22014-02-14 11:59:04 +01001362/* generate a random 32-bit integer for whatever purpose, with an optional
1363 * range specified in argument.
1364 */
1365static int
1366smp_fetch_rand(struct proxy *px, struct session *s, void *l7, unsigned int opt,
1367 const struct arg *args, struct sample *smp, const char *kw)
1368{
1369 smp->data.uint = random();
1370
1371 /* reduce if needed. Don't do a modulo, use all bits! */
1372 if (args && args[0].type == ARGT_UINT)
Vincent Bernat7d2dda82014-12-10 10:31:37 +01001373 smp->data.uint = ((uint64_t)smp->data.uint * args[0].data.uint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01001374
1375 smp->type = SMP_T_UINT;
1376 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
1377 return 1;
1378}
1379
Willy Tarreaufad7f072014-11-24 16:02:05 +01001380/* returns true if the current process is stopping */
1381static int
1382smp_fetch_stopping(struct proxy *px, struct session *s, void *l7, unsigned int opt,
1383 const struct arg *args, struct sample *smp, const char *kw)
1384{
1385 smp->type = SMP_T_BOOL;
1386 smp->data.uint = stopping;
1387 return 1;
1388}
1389
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001390/* Note: must not be declared <const> as its list will be overwritten.
1391 * Note: fetches that may return multiple types must be declared as the lowest
1392 * common denominator, the type that can be casted into all other ones. For
1393 * instance IPv4/IPv6 must be declared IPv4.
1394 */
1395static struct sample_fetch_kw_list smp_kws = {ILH, {
1396 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
1397 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001398 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Willy Tarreau6236d3a2013-07-25 14:28:25 +02001399 { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreaufad7f072014-11-24 16:02:05 +01001400 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_UINT, SMP_USE_INTRN },
1401 { "proc", smp_fetch_proc, 0, NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreau84310e22014-02-14 11:59:04 +01001402 { "rand", smp_fetch_rand, ARG1(0,UINT), NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreaufad7f072014-11-24 16:02:05 +01001403 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001404 { /* END */ },
1405}};
1406
Emeric Brun107ca302010-01-04 16:16:05 +01001407/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001408static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Emeric Brun53d1a982014-04-30 18:21:37 +02001409 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02001410 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
1411 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001412 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02001413 { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001414 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01001415}};
1416
1417__attribute__((constructor))
Willy Tarreau12785782012-04-27 21:37:17 +02001418static void __sample_init(void)
Emeric Brun107ca302010-01-04 16:16:05 +01001419{
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001420 /* register sample fetch and format conversion keywords */
1421 sample_register_fetches(&smp_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02001422 sample_register_convs(&sample_conv_kws);
Emeric Brun107ca302010-01-04 16:16:05 +01001423}