blob: 94c605f9279b02754a5c534cf7c219cef66c731a [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 Tarreau35b51c62018-09-10 15:38:55 +020023#include <common/http.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010024#include <common/initcall.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020025#include <common/standard.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020026#include <common/uri_auth.h>
Emeric Brun53d1a982014-04-30 18:21:37 +020027#include <common/base64.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020028
Willy Tarreau9fcb9842012-04-20 14:45:49 +020029#include <proto/arg.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020030#include <proto/auth.h>
31#include <proto/log.h>
32#include <proto/proxy.h>
Frédéric Lécaillefd95c622019-02-26 14:09:08 +010033#include <proto/protocol_buffers.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020034#include <proto/sample.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020035#include <proto/stick_table.h>
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +020036#include <proto/vars.h>
Emeric Brun107ca302010-01-04 16:16:05 +010037
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +020038#include <import/sha1.h>
Thierry FOURNIER01e09742016-12-26 11:46:11 +010039#include <import/xxhash.h>
40
Willy Tarreau1cf8f082014-02-07 12:14:54 +010041/* sample type names */
42const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020043 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010044 [SMP_T_BOOL] = "bool",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010045 [SMP_T_SINT] = "sint",
46 [SMP_T_ADDR] = "addr",
47 [SMP_T_IPV4] = "ipv4",
48 [SMP_T_IPV6] = "ipv6",
49 [SMP_T_STR] = "str",
50 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020051 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010052};
53
Willy Tarreau12785782012-04-27 21:37:17 +020054/* static sample used in sample_process() when <p> is NULL */
Emeric Brune5c918b2017-06-14 14:15:36 +020055static THREAD_LOCAL struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010056
Willy Tarreau12785782012-04-27 21:37:17 +020057/* list head of all known sample fetch keywords */
58static struct sample_fetch_kw_list sample_fetches = {
59 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010060};
61
Willy Tarreau12785782012-04-27 21:37:17 +020062/* list head of all known sample format conversion keywords */
63static struct sample_conv_kw_list sample_convs = {
64 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010065};
66
Willy Tarreau80aca902013-01-07 15:42:20 +010067const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
68 [SMP_SRC_INTRN] = (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_LISTN] = (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_FTEND] = (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_L4CLI] = (SMP_VAL_FE_CON_ACC | 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_L5CLI] = (SMP_VAL___________ | 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_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | 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_BE_RES_CNT |
112 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
113 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
114 SMP_VAL_FE_LOG_END),
115
116 [SMP_SRC_L6REQ] = (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_HRQHV] = (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___________ |
128 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
129 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
130 SMP_VAL___________),
131
132 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
133 SMP_VAL_FE_HRQ_HDR | 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_BE_RES_CNT |
136 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
137 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
138 SMP_VAL_FE_LOG_END),
139
140 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
141 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
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___________ |
144 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
145 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
146 SMP_VAL___________),
147
148 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
149 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
150 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
151 SMP_VAL_BE_SET_SRV | 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_SERVR] = (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_BE_SRV_CON | 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_L4SRV] = (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_L5SRV] = (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_FE_LOG_END),
179
180 [SMP_SRC_L6RES] = (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_HRSHV] = (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___________),
195
196 [SMP_SRC_HRSHP] = (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_BE_RES_CNT |
200 SMP_VAL_BE_HRS_HDR | 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_FE_LOG_END),
203
204 [SMP_SRC_HRSBO] = (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_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
209 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
210 SMP_VAL___________),
211
212 [SMP_SRC_RQFIN] = (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_RSFIN] = (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_TXFIN] = (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 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
237 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
238 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
239 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
240 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
241 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
242 SMP_VAL_FE_LOG_END),
243};
244
245static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
246 [SMP_SRC_INTRN] = "internal state",
247 [SMP_SRC_LISTN] = "listener",
248 [SMP_SRC_FTEND] = "frontend",
249 [SMP_SRC_L4CLI] = "client address",
250 [SMP_SRC_L5CLI] = "client-side connection",
251 [SMP_SRC_TRACK] = "track counters",
252 [SMP_SRC_L6REQ] = "request buffer",
253 [SMP_SRC_HRQHV] = "HTTP request headers",
254 [SMP_SRC_HRQHP] = "HTTP request",
255 [SMP_SRC_HRQBO] = "HTTP request body",
256 [SMP_SRC_BKEND] = "backend",
257 [SMP_SRC_SERVR] = "server",
258 [SMP_SRC_L4SRV] = "server address",
259 [SMP_SRC_L5SRV] = "server-side connection",
260 [SMP_SRC_L6RES] = "response buffer",
261 [SMP_SRC_HRSHV] = "HTTP response headers",
262 [SMP_SRC_HRSHP] = "HTTP response",
263 [SMP_SRC_HRSBO] = "HTTP response body",
264 [SMP_SRC_RQFIN] = "request buffer statistics",
265 [SMP_SRC_RSFIN] = "response buffer statistics",
266 [SMP_SRC_TXFIN] = "transaction statistics",
267 [SMP_SRC_SSFIN] = "session statistics",
268};
269
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100270static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
271 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
272 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
273 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
274 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
275 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
276 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
277 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
278 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
279 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
280 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
281 [SMP_CKP_BE_SRV_CON] = "server source selection",
282 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
283 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
284 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
285 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
286 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
287 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
288 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
289 [SMP_CKP_FE_LOG_END] = "logs",
290};
291
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100292/* This function returns the type of the data returned by the sample_expr.
293 * It assumes that the <expr> and all of its converters are properly
294 * initialized.
295 */
296inline
297int smp_expr_output_type(struct sample_expr *expr)
298{
299 struct sample_conv_expr *smp_expr;
300
301 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
302 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
303 return smp_expr->conv->out_type;
304 }
305 return expr->fetch->out_type;
306}
307
308
Willy Tarreau80aca902013-01-07 15:42:20 +0100309/* fill the trash with a comma-delimited list of source names for the <use> bit
310 * field which must be composed of a non-null set of SMP_USE_* flags. The return
311 * value is the pointer to the string in the trash buffer.
312 */
313const char *sample_src_names(unsigned int use)
314{
315 int bit;
316
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200317 trash.data = 0;
318 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100319 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
320 if (!(use & ~((1 << bit) - 1)))
321 break; /* no more bits */
322
323 if (!(use & (1 << bit)))
324 continue; /* bit not set */
325
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200326 trash.data += snprintf(trash.area + trash.data,
327 trash.size - trash.data, "%s%s",
328 (use & ((1 << bit) - 1)) ? "," : "",
329 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100330 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200331 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100332}
333
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100334/* return a pointer to the correct sample checkpoint name, or "unknown" when
335 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
336 * if set.
337 */
338const char *sample_ckp_names(unsigned int use)
339{
340 int bit;
341
342 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
343 if (use & (1 << bit))
344 return fetch_ckp_names[bit];
345 return "unknown sample check place, please report this bug";
346}
347
Emeric Brun107ca302010-01-04 16:16:05 +0100348/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100349 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
350 * for next parsing sessions. The fetch keywords capabilities are also computed
351 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100352 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100353void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100354{
Willy Tarreau80aca902013-01-07 15:42:20 +0100355 struct sample_fetch *sf;
356 int bit;
357
358 for (sf = kwl->kw; sf->kw != NULL; sf++) {
359 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
360 if (sf->use & (1 << bit))
361 sf->val |= fetch_cap[bit];
362 }
363 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100364}
365
366/*
Willy Tarreau12785782012-04-27 21:37:17 +0200367 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100368 * parsing sessions.
369 */
Willy Tarreau12785782012-04-27 21:37:17 +0200370void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100371{
Willy Tarreau12785782012-04-27 21:37:17 +0200372 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100373}
374
375/*
Willy Tarreau12785782012-04-27 21:37:17 +0200376 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100377 * string of <len> in buffer <kw>.
378 *
379 */
Willy Tarreau12785782012-04-27 21:37:17 +0200380struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100381{
382 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200383 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100384
Willy Tarreau12785782012-04-27 21:37:17 +0200385 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100386 for (index = 0; kwl->kw[index].kw != NULL; index++) {
387 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
388 kwl->kw[index].kw[len] == '\0')
389 return &kwl->kw[index];
390 }
391 }
392 return NULL;
393}
394
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100395/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100396 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100397 * <idx> is the index of the next sample fetch entry. It is used as private
398 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100399 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100400 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100401 * the list is reached.
402 */
403struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
404{
405 struct sample_fetch_kw_list *kwl;
406 struct sample_fetch *base;
407
408 if (!current) {
409 /* Get first kwl entry. */
410 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
411 (*idx) = 0;
412 } else {
413 /* Get kwl corresponding to the curret entry. */
414 base = current + 1 - (*idx);
415 kwl = container_of(base, struct sample_fetch_kw_list, kw);
416 }
417
418 while (1) {
419
420 /* Check if kwl is the last entry. */
421 if (&kwl->list == &sample_fetches.list)
422 return NULL;
423
424 /* idx contain the next keyword. If it is available, return it. */
425 if (kwl->kw[*idx].kw) {
426 (*idx)++;
427 return &kwl->kw[(*idx)-1];
428 }
429
430 /* get next entry in the main list, and return NULL if the end is reached. */
431 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
432
433 /* Set index to 0, ans do one other loop. */
434 (*idx) = 0;
435 }
436}
437
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100438/* This function browses the list of available converters. <current> is
439 * the last used converter. If it is the first call, it must set to NULL.
440 * <idx> is the index of the next converter entry. It is used as private
441 * value. It is useless to initiate it.
442 *
443 * It returns always the next sample_conv entry, and NULL when the end of
444 * the list is reached.
445 */
446struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
447{
448 struct sample_conv_kw_list *kwl;
449 struct sample_conv *base;
450
451 if (!current) {
452 /* Get first kwl entry. */
453 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
454 (*idx) = 0;
455 } else {
456 /* Get kwl corresponding to the curret entry. */
457 base = current + 1 - (*idx);
458 kwl = container_of(base, struct sample_conv_kw_list, kw);
459 }
460
461 while (1) {
462 /* Check if kwl is the last entry. */
463 if (&kwl->list == &sample_convs.list)
464 return NULL;
465
466 /* idx contain the next keyword. If it is available, return it. */
467 if (kwl->kw[*idx].kw) {
468 (*idx)++;
469 return &kwl->kw[(*idx)-1];
470 }
471
472 /* get next entry in the main list, and return NULL if the end is reached. */
473 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
474
475 /* Set index to 0, ans do one other loop. */
476 (*idx) = 0;
477 }
478}
479
Emeric Brun107ca302010-01-04 16:16:05 +0100480/*
Willy Tarreau12785782012-04-27 21:37:17 +0200481 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100482 * string of <len> in buffer <kw>.
483 *
484 */
Willy Tarreau12785782012-04-27 21:37:17 +0200485struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100486{
487 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200488 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100489
Willy Tarreau12785782012-04-27 21:37:17 +0200490 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100491 for (index = 0; kwl->kw[index].kw != NULL; index++) {
492 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
493 kwl->kw[index].kw[len] == '\0')
494 return &kwl->kw[index];
495 }
496 }
497 return NULL;
498}
499
Emeric Brun107ca302010-01-04 16:16:05 +0100500/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200501/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100502/******************************************************************/
503
Willy Tarreau342acb42012-04-23 22:03:39 +0200504static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100505{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200506 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200507 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100508 return 1;
509}
510
Willy Tarreau342acb42012-04-23 22:03:39 +0200511static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100512{
Willy Tarreau83061a82018-07-13 11:56:34 +0200513 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100514
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200515 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100516 return 0;
517
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200518 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200519 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200520 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100521 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100522
523 return 1;
524}
525
Willy Tarreau342acb42012-04-23 22:03:39 +0200526static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100527{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200528 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200529 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100530 return 1;
531}
532
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200533static int c_ipv62ip(struct sample *smp)
534{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200535 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200536 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100537 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200538 return 1;
539}
540
Willy Tarreau342acb42012-04-23 22:03:39 +0200541static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100542{
Willy Tarreau83061a82018-07-13 11:56:34 +0200543 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100544
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200545 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100546 return 0;
547
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200548 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200549 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200550 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100551 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100552 return 1;
553}
554
555/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200556static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100557{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200558 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100559}
560*/
561
Willy Tarreau342acb42012-04-23 22:03:39 +0200562static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100563{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200564 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200565 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100566 return 1;
567}
568
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200569static int c_int2ipv6(struct sample *smp)
570{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200571 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
572 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200573 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200574 return 1;
575}
576
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100577static int c_str2addr(struct sample *smp)
578{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200579 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
580 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100581 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200582 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100583 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100584 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100585 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200586 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100587 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100588 return 1;
589}
590
Willy Tarreau342acb42012-04-23 22:03:39 +0200591static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100592{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200593 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100594 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200595 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100596 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100597 return 1;
598}
599
Willy Tarreau342acb42012-04-23 22:03:39 +0200600static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100601{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200602 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100603 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200604 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100605 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100606 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100607}
608
Emeric Brun4b9e8022014-11-03 18:17:10 +0100609/*
610 * The NULL char always enforces the end of string if it is met.
611 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100612 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200613static int c_bin2str(struct sample *smp)
614{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100615 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200616
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200617 for (i = 0; i < smp->data.u.str.data; i++) {
618 if (!smp->data.u.str.area[i]) {
619 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100620 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100621 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200622 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200623 return 1;
624}
625
Willy Tarreau342acb42012-04-23 22:03:39 +0200626static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100627{
Willy Tarreau83061a82018-07-13 11:56:34 +0200628 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100629 char *pos;
630
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200631 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100632 if (!pos)
633 return 0;
634
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200635 trash->size = trash->size - (pos - trash->area);
636 trash->area = pos;
637 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200638 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200639 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100640 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100641 return 1;
642}
643
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800644/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200645 * For strings and binary blocks, it also provides a known allocated size with
646 * a length that is capped to the size, and ensures a trailing zero is always
647 * appended for strings. This is necessary for some operations which may
648 * require to extend the length. It returns 0 if it fails, 1 on success.
649 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100650int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200651{
Willy Tarreau83061a82018-07-13 11:56:34 +0200652 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200653
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200654 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100655 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100656 case SMP_T_SINT:
657 case SMP_T_ADDR:
658 case SMP_T_IPV4:
659 case SMP_T_IPV6:
660 /* These type are not const. */
661 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200662
Christopher Fauletec100512017-07-24 15:38:41 +0200663 case SMP_T_METH:
664 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
665 break;
666 /* Fall through */
667
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100668 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200669 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100670 trash->data = smp->data.type == SMP_T_STR ?
671 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200672 if (trash->data > trash->size - 1)
673 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200674
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100675 memcpy(trash->area, smp->data.type == SMP_T_STR ?
676 smp->data.u.str.area : smp->data.u.meth.str.area,
677 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200678 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200679 smp->data.u.str = *trash;
680 break;
681
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100682 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100683 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200684 trash->data = smp->data.u.str.data;
685 if (trash->data > trash->size)
686 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200687
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200688 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200689 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100690 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200691
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100692 default:
693 /* Other cases are unexpected. */
694 return 0;
695 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100696
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100697 /* remove const flag */
698 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200699 return 1;
700}
701
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100702int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100703{
704 return 1;
705}
706
Willy Tarreau342acb42012-04-23 22:03:39 +0200707static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100708{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200709 const char *str;
710 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100711
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200712 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100713 return 0;
714
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200715 str = smp->data.u.str.area;
716 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100717
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200718 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200719 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100720 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100721 return 1;
722}
723
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100724static int c_str2meth(struct sample *smp)
725{
726 enum http_meth_t meth;
727 int len;
728
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200729 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100730 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200731 len = smp->data.u.str.data;
732 smp->data.u.meth.str.area = smp->data.u.str.area;
733 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100734 }
735 else
736 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200737 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200738 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100739 return 1;
740}
741
742static int c_meth2str(struct sample *smp)
743{
744 int len;
745 enum http_meth_t meth;
746
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200747 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100748 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200749 len = smp->data.u.meth.str.data;
750 smp->data.u.str.area = smp->data.u.meth.str.area;
751 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200752 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100753 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200754 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100755 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200756 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200757 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200758 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100759 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200760 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100761 }
762 else {
763 /* Unknown method */
764 return 0;
765 }
766 return 1;
767}
768
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200769static int c_addr2bin(struct sample *smp)
770{
Willy Tarreau83061a82018-07-13 11:56:34 +0200771 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200772
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200773 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200774 chk->data = 4;
775 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200776 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200777 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200778 chk->data = 16;
779 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200780 }
781 else
782 return 0;
783
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200784 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200785 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200786 return 1;
787}
788
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200789static int c_int2bin(struct sample *smp)
790{
Willy Tarreau83061a82018-07-13 11:56:34 +0200791 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200792
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200793 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
794 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200795
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200796 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200797 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200798 return 1;
799}
800
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200801
Emeric Brun107ca302010-01-04 16:16:05 +0100802/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200803/* Sample casts matrix: */
804/* sample_casts[from type][to type] */
805/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100806/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100807
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100808sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200809/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
810/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
811/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200812/* SINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, c_int2ipv6, c_int2str, c_int2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200813/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
814/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200815/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200816/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
817/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
818/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200819};
Emeric Brun107ca302010-01-04 16:16:05 +0100820
Emeric Brun107ca302010-01-04 16:16:05 +0100821/*
Willy Tarreau12785782012-04-27 21:37:17 +0200822 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100823 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200824 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200825 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100826 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100827struct 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 +0100828{
Willy Tarreau833cc792013-07-24 15:34:19 +0200829 const char *begw; /* beginning of word */
830 const char *endw; /* end of word */
831 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200832 struct sample_expr *expr;
833 struct sample_fetch *fetch;
834 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100835 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200836 char *fkw = NULL;
837 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100838 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100839
Willy Tarreau833cc792013-07-24 15:34:19 +0200840 begw = str[*idx];
841 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
842
843 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100844 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100845 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200846 }
Emeric Brun107ca302010-01-04 16:16:05 +0100847
Willy Tarreau833cc792013-07-24 15:34:19 +0200848 /* keep a copy of the current fetch keyword for error reporting */
849 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100850
Willy Tarreau833cc792013-07-24 15:34:19 +0200851 fetch = find_sample_fetch(begw, endw - begw);
852 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100853 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100854 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200855 }
Emeric Brun107ca302010-01-04 16:16:05 +0100856
Willy Tarreau833cc792013-07-24 15:34:19 +0200857 endt = endw;
858 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100859 /* look for the end of this term and skip the opening parenthesis */
860 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200861 while (*endt && *endt != ')')
862 endt++;
863 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100864 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200865 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200866 }
Emeric Brun485479d2010-09-23 18:02:19 +0200867 }
Emeric Brun107ca302010-01-04 16:16:05 +0100868
Willy Tarreau833cc792013-07-24 15:34:19 +0200869 /* At this point, we have :
870 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100871 * - endw : end of the keyword, first character not part of keyword
872 * nor the opening parenthesis (so first character of args
873 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200874 * - endt : end of the term (=endw or last parenthesis if args are present)
875 */
876
877 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100878 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100879 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200880 }
Emeric Brun107ca302010-01-04 16:16:05 +0100881 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200882
Vincent Bernat02779b62016-04-03 13:48:43 +0200883 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200884 if (!expr)
885 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100886
887 LIST_INIT(&(expr->conv_exprs));
888 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200889 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100890
Willy Tarreau689a1df2013-12-13 00:40:11 +0100891 /* Note that we call the argument parser even with an empty string,
892 * this allows it to automatically create entries for mandatory
893 * implicit arguments (eg: local proxy name).
894 */
895 al->kw = expr->fetch->kw;
896 al->conv = NULL;
897 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
898 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
899 goto out_error;
900 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200901
Willy Tarreau689a1df2013-12-13 00:40:11 +0100902 if (!expr->arg_p) {
903 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200904 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100905 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
906 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200907 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100908 }
909
Willy Tarreau833cc792013-07-24 15:34:19 +0200910 /* Now process the converters if any. We have two supported syntaxes
911 * for the converters, which can be combined :
912 * - comma-delimited list of converters just after the keyword and args ;
913 * - one converter per keyword
914 * The combination allows to have each keyword being a comma-delimited
915 * series of converters.
916 *
917 * We want to process the former first, then the latter. For this we start
918 * from the beginning of the supposed place in the exiting conv chain, which
919 * starts at the last comma (endt).
920 */
921
922 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200923 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100924
Willy Tarreau833cc792013-07-24 15:34:19 +0200925 if (*endt == ')') /* skip last closing parenthesis */
926 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100927
Willy Tarreau833cc792013-07-24 15:34:19 +0200928 if (*endt && *endt != ',') {
929 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100930 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200931 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100932 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100933 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200934 }
Emeric Brun107ca302010-01-04 16:16:05 +0100935
Willy Tarreau833cc792013-07-24 15:34:19 +0200936 while (*endt == ',') /* then trailing commas */
937 endt++;
938
Willy Tarreau97108e02016-11-25 07:33:24 +0100939 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200940
941 if (!*begw) {
942 /* none ? skip to next string */
943 (*idx)++;
944 begw = str[*idx];
945 if (!begw || !*begw)
946 break;
947 }
948
949 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
950
951 free(ckw);
952 ckw = my_strndup(begw, endw - begw);
953
954 conv = find_sample_conv(begw, endw - begw);
955 if (!conv) {
956 /* we found an isolated keyword that we don't know, it's not ours */
957 if (begw == str[*idx])
958 break;
Willy Tarreau97108e02016-11-25 07:33:24 +0100959 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200960 goto out_error;
961 }
Emeric Brun107ca302010-01-04 16:16:05 +0100962
Willy Tarreau833cc792013-07-24 15:34:19 +0200963 endt = endw;
964 if (*endt == '(') {
965 /* look for the end of this term */
966 while (*endt && *endt != ')')
967 endt++;
968 if (*endt != ')') {
Willy Tarreau97108e02016-11-25 07:33:24 +0100969 memprintf(err_msg, "syntax error: missing ')' after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200970 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200971 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200972 }
973
974 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100975 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100976 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200977 }
Emeric Brun107ca302010-01-04 16:16:05 +0100978
979 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200980 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100981 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100982 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200983 }
Emeric Brun107ca302010-01-04 16:16:05 +0100984
985 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +0200986 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200987 if (!conv_expr)
988 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100989
990 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
991 conv_expr->conv = conv;
992
Willy Tarreau833cc792013-07-24 15:34:19 +0200993 if (endt != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200994 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200995
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200996 if (!conv->arg_mask) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100997 memprintf(err_msg, "converter '%s' does not support any args", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200998 goto out_error;
999 }
Willy Tarreau9e92d322010-01-26 17:58:06 +01001000
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001001 al->kw = expr->fetch->kw;
1002 al->conv = conv_expr->conv->kw;
Willy Tarreau975c1782013-12-12 23:16:54 +01001003 if (make_arg_list(endw + 1, endt - endw - 1, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001004 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +02001005 goto out_error;
1006 }
1007
Willy Tarreau2e845be2012-10-19 19:49:09 +02001008 if (!conv_expr->arg_p)
1009 conv_expr->arg_p = empty_arg_list;
1010
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01001011 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001012 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001013 goto out_error;
1014 }
1015 }
Willy Tarreau833cc792013-07-24 15:34:19 +02001016 else if (ARGM(conv->arg_mask)) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001017 memprintf(err_msg, "missing args for converter '%s'", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +02001018 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001019 }
1020 }
Emeric Brun485479d2010-09-23 18:02:19 +02001021
Willy Tarreau833cc792013-07-24 15:34:19 +02001022 out:
1023 free(fkw);
1024 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001025 return expr;
1026
1027out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001028 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001029 expr = NULL;
1030 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001031}
1032
1033/*
Willy Tarreau12785782012-04-27 21:37:17 +02001034 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001035 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001036 * Returns a pointer on a typed sample structure containing the result or NULL if
1037 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001038 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001039 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001040 *
1041 * Note: the fetch functions are required to properly set the return type. The
1042 * conversion functions must do so too. However the cast functions do not need
1043 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001044 *
1045 * The caller may indicate in <opt> if it considers the result final or not.
1046 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1047 * if the result is stable or not, according to the following table :
1048 *
1049 * return MAY_CHANGE FINAL Meaning for the sample
1050 * NULL 0 * Not present and will never be (eg: header)
1051 * NULL 1 0 Not present yet, could change (eg: POST param)
1052 * NULL 1 1 Not present yet, will not change anymore
1053 * smp 0 * Present and will not change (eg: header)
1054 * smp 1 0 Present, may change (eg: request length)
1055 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001056 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001057struct sample *sample_process(struct proxy *px, struct session *sess,
1058 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001059 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001060{
Willy Tarreau12785782012-04-27 21:37:17 +02001061 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001062
Willy Tarreau18387e22013-07-25 12:02:38 +02001063 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001064 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001065 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001066 }
Emeric Brun107ca302010-01-04 16:16:05 +01001067
Willy Tarreau1777ea62016-03-10 16:15:46 +01001068 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001069 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001070 return NULL;
1071
Emeric Brun107ca302010-01-04 16:16:05 +01001072 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001073 /* we want to ensure that p->type can be casted into
1074 * conv_expr->conv->in_type. We have 3 possibilities :
1075 * - NULL => not castable.
1076 * - c_none => nothing to do (let's optimize it)
1077 * - other => apply cast and prepare to fail
1078 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001079 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001080 return NULL;
1081
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001082 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1083 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001084 return NULL;
1085
Willy Tarreau12e50112012-04-25 17:21:49 +02001086 /* OK cast succeeded */
1087
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001088 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001089 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001090 }
1091 return p;
1092}
1093
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001094/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001095 * Resolve all remaining arguments in proxy <p>. Returns the number of
1096 * errors or 0 if everything is fine.
1097 */
1098int smp_resolve_args(struct proxy *p)
1099{
1100 struct arg_list *cur, *bak;
1101 const char *ctx, *where;
1102 const char *conv_ctx, *conv_pre, *conv_pos;
1103 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001104 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001105 struct arg *arg;
1106 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001107 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001108
1109 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1110 struct proxy *px;
1111 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001112 struct stktable *t;
1113 char *pname, *sname, *stktname;
Willy Tarreau46947782015-01-19 19:00:58 +01001114 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001115
1116 arg = cur->arg;
1117
1118 /* prepare output messages */
1119 conv_pre = conv_pos = conv_ctx = "";
1120 if (cur->conv) {
1121 conv_ctx = cur->conv;
1122 conv_pre = "conversion keyword '";
1123 conv_pos = "' for ";
1124 }
1125
1126 where = "in";
1127 ctx = "sample fetch keyword";
1128 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001129 case ARGC_STK: where = "in stick rule in"; break;
1130 case ARGC_TRK: where = "in tracking rule in"; break;
1131 case ARGC_LOG: where = "in log-format string in"; break;
1132 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1133 case ARGC_HRQ: where = "in http-request header format string in"; break;
1134 case ARGC_HRS: where = "in http-response header format string in"; break;
1135 case ARGC_UIF: where = "in unique-id-format string in"; break;
1136 case ARGC_RDR: where = "in redirect format string in"; break;
1137 case ARGC_CAP: where = "in capture rule in"; break;
1138 case ARGC_ACL: ctx = "ACL keyword"; break;
1139 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001140 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001141 }
1142
1143 /* set a few default settings */
1144 px = p;
1145 pname = p->id;
1146
1147 switch (arg->type) {
1148 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001149 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001150 ha_alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1151 cur->file, cur->line,
1152 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001153 cfgerr++;
1154 continue;
1155 }
1156
1157 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001158 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001159
1160 if (sname) {
1161 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001162 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001163
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001164 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001165 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001166 ha_alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1167 cur->file, cur->line, pname,
1168 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001169 cfgerr++;
1170 break;
1171 }
1172 }
1173 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001174 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001175
1176 srv = findserver(px, sname);
1177 if (!srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001178 ha_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",
1179 cur->file, cur->line, sname, pname,
1180 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001181 cfgerr++;
1182 break;
1183 }
1184
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001185 free(arg->data.str.area);
1186 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001187 arg->unresolved = 0;
1188 arg->data.srv = srv;
1189 break;
1190
1191 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001192 if (arg->data.str.data) {
1193 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001194 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001195 }
1196
1197 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001198 ha_alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1199 cur->file, cur->line, pname,
1200 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001201 cfgerr++;
1202 break;
1203 }
1204
1205 if (!(px->cap & PR_CAP_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001206 ha_alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1207 cur->file, cur->line, pname,
1208 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001209 cfgerr++;
1210 break;
1211 }
1212
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001213 free(arg->data.str.area);
1214 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001215 arg->unresolved = 0;
1216 arg->data.prx = px;
1217 break;
1218
1219 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001220 if (arg->data.str.data) {
1221 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001222 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001223 }
1224
1225 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001226 ha_alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1227 cur->file, cur->line, pname,
1228 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001229 cfgerr++;
1230 break;
1231 }
1232
1233 if (!(px->cap & PR_CAP_BE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001234 ha_alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1235 cur->file, cur->line, pname,
1236 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001237 cfgerr++;
1238 break;
1239 }
1240
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001241 free(arg->data.str.area);
1242 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001243 arg->unresolved = 0;
1244 arg->data.prx = px;
1245 break;
1246
1247 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001248 if (arg->data.str.data)
1249 stktname = arg->data.str.area;
1250 else
1251 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001252
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001253 t = stktable_find_by_name(stktname);
1254 if (!t) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001255 ha_alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001256 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001257 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001258 cfgerr++;
1259 break;
1260 }
1261
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001262 if (!t->size) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001263 ha_alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001264 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001265 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001266 cfgerr++;
1267 break;
1268 }
1269
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001270 if (t->proxy && (p->bind_proc & ~t->proxy->bind_proc)) {
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001271 ha_alert("parsing [%s:%d] : stick-table '%s' not present on all processes covered by proxy '%s'.\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001272 cur->file, cur->line, t->proxy->id, p->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01001273 cfgerr++;
1274 break;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001275 }
1276
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001277 if (t->proxies_list != p) {
1278 p->next_stkt_ref = t->proxies_list;
1279 t->proxies_list = p;
1280 }
1281
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001282 free(arg->data.str.area);
1283 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001284 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001285 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001286 break;
1287
1288 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001289 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001290 ha_alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1291 cur->file, cur->line,
1292 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001293 cfgerr++;
1294 break;
1295 }
1296
1297 if (p->uri_auth && p->uri_auth->userlist &&
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001298 !strcmp(p->uri_auth->userlist->name, arg->data.str.area))
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001299 ul = p->uri_auth->userlist;
1300 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001301 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001302
1303 if (!ul) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001304 ha_alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001305 cur->file, cur->line,
1306 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001307 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001308 cfgerr++;
1309 break;
1310 }
1311
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001312 free(arg->data.str.area);
1313 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001314 arg->unresolved = 0;
1315 arg->data.usr = ul;
1316 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001317
1318 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001319 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001320 ha_alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1321 cur->file, cur->line,
1322 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001323 cfgerr++;
1324 continue;
1325 }
1326
Willy Tarreau46947782015-01-19 19:00:58 +01001327 rflags = 0;
1328 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1329 err = NULL;
1330
Dragan Dosen26743032019-04-30 15:54:36 +02001331 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001332 ha_alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1333 cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001334 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001335 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
Willy Tarreau46947782015-01-19 19:00:58 +01001336 cfgerr++;
1337 continue;
1338 }
1339
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001340 free(arg->data.str.area);
1341 arg->data.str.area = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001342 arg->unresolved = 0;
1343 arg->data.reg = reg;
1344 break;
1345
1346
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001347 }
1348
1349 LIST_DEL(&cur->list);
1350 free(cur);
1351 } /* end of args processing */
1352
1353 return cfgerr;
1354}
1355
1356/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001357 * Process a fetch + format conversion as defined by the sample expression
1358 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001359 * not explicitly set to <smp_type>, but shall be compatible with it as
1360 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1361 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001362 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1363 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1364 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1365 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001366 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1367 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1368 * still hope to get it after waiting longer, and is not converted to string.
1369 * The possible output combinations are the following :
1370 *
1371 * return MAY_CHANGE FINAL Meaning for the sample
1372 * NULL * * Not present and will never be (eg: header)
1373 * smp 0 * Final value converted (eg: header)
1374 * smp 1 0 Not present yet, may appear later (eg: header)
1375 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001376 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001377struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001378 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001379 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001380{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001381 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001382
Willy Tarreau6c616e02014-06-25 16:56:41 +02001383 memset(smp, 0, sizeof(*smp));
1384
Willy Tarreau192252e2015-04-04 01:47:55 +02001385 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001386 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1387 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001388 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001389 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001390
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001391 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001392 return NULL;
1393
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001394 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001395 return NULL;
1396
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001397 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001398 return smp;
1399}
1400
Christopher Faulet476e5d02016-10-26 11:34:47 +02001401static void release_sample_arg(struct arg *p)
1402{
1403 struct arg *p_back = p;
1404
1405 if (!p)
1406 return;
1407
1408 while (p->type != ARGT_STOP) {
1409 if (p->type == ARGT_STR || p->unresolved) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001410 free(p->data.str.area);
1411 p->data.str.area = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001412 p->unresolved = 0;
1413 }
1414 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001415 regex_free(p->data.reg);
1416 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001417 }
1418 p++;
1419 }
1420
1421 if (p_back != empty_arg_list)
1422 free(p_back);
1423}
1424
1425void release_sample_expr(struct sample_expr *expr)
1426{
1427 struct sample_conv_expr *conv_expr, *conv_exprb;
1428
1429 if (!expr)
1430 return;
1431
1432 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list)
1433 release_sample_arg(conv_expr->arg_p);
1434 release_sample_arg(expr->arg_p);
1435 free(expr);
1436}
1437
Emeric Brun107ca302010-01-04 16:16:05 +01001438/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001439/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001440/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001441/*****************************************************************/
1442
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001443#ifdef DEBUG_EXPR
1444static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1445{
1446 int i;
1447 struct sample tmp;
1448
1449 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001450 fprintf(stderr, "[debug converter] type: %s ", smp_to_type[smp->data.type]);
1451 if (!sample_casts[smp->data.type][SMP_T_STR]) {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001452 fprintf(stderr, "(undisplayable)");
1453 } else {
1454
1455 /* Copy sample fetch. This put the sample as const, the
1456 * cast will copy data if a transformation is required.
1457 */
1458 memcpy(&tmp, smp, sizeof(struct sample));
1459 tmp.flags = SMP_F_CONST;
1460
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001461 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001462 fprintf(stderr, "(undisplayable)");
1463
1464 else {
1465 /* Display the displayable chars*. */
1466 fprintf(stderr, "<");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001467 for (i = 0; i < tmp.data.u.str.data; i++) {
1468 if (isprint(tmp.data.u.str.area[i]))
1469 fputc(tmp.data.u.str.area[i],
1470 stderr);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001471 else
1472 fputc('.', stderr);
1473 }
1474 }
1475 fprintf(stderr, ">\n");
1476 }
1477 }
1478 return 1;
1479}
1480#endif
1481
Holger Just1bfc24b2017-05-06 00:56:53 +02001482static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1483{
Willy Tarreau83061a82018-07-13 11:56:34 +02001484 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001485 int bin_len;
1486
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001487 trash->data = 0;
1488 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1489 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001490 if (bin_len < 0)
1491 return 0;
1492
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001493 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001494 smp->data.u.str = *trash;
1495 smp->data.type = SMP_T_BIN;
1496 smp->flags &= ~SMP_F_CONST;
1497 return 1;
1498}
1499
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001500static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001501{
Willy Tarreau83061a82018-07-13 11:56:34 +02001502 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001503 int b64_len;
1504
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001505 trash->data = 0;
1506 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1507 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001508 if (b64_len < 0)
1509 return 0;
1510
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001511 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001512 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001513 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001514 smp->flags &= ~SMP_F_CONST;
1515 return 1;
1516}
1517
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001518static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1519{
1520 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001521 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001522
1523 memset(&ctx, 0, sizeof(ctx));
1524
1525 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001526 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1527 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001528
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001529 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001530 smp->data.u.str = *trash;
1531 smp->data.type = SMP_T_BIN;
1532 smp->flags &= ~SMP_F_CONST;
1533 return 1;
1534}
1535
Tim Duesterhusd4376302019-06-17 12:41:44 +02001536#ifdef USE_OPENSSL
1537static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
1538{
1539 struct buffer *trash = get_trash_chunk();
1540 int bits = 256;
1541 if (arg_p && arg_p->data.sint)
1542 bits = arg_p->data.sint;
1543
1544 switch (bits) {
1545 case 224: {
1546 SHA256_CTX ctx;
1547
1548 memset(&ctx, 0, sizeof(ctx));
1549
1550 SHA224_Init(&ctx);
1551 SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1552 SHA224_Final((unsigned char *) trash->area, &ctx);
1553 trash->data = SHA224_DIGEST_LENGTH;
1554 break;
1555 }
1556 case 256: {
1557 SHA256_CTX ctx;
1558
1559 memset(&ctx, 0, sizeof(ctx));
1560
1561 SHA256_Init(&ctx);
1562 SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1563 SHA256_Final((unsigned char *) trash->area, &ctx);
1564 trash->data = SHA256_DIGEST_LENGTH;
1565 break;
1566 }
1567 case 384: {
1568 SHA512_CTX ctx;
1569
1570 memset(&ctx, 0, sizeof(ctx));
1571
1572 SHA384_Init(&ctx);
1573 SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1574 SHA384_Final((unsigned char *) trash->area, &ctx);
1575 trash->data = SHA384_DIGEST_LENGTH;
1576 break;
1577 }
1578 case 512: {
1579 SHA512_CTX ctx;
1580
1581 memset(&ctx, 0, sizeof(ctx));
1582
1583 SHA512_Init(&ctx);
1584 SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1585 SHA512_Final((unsigned char *) trash->area, &ctx);
1586 trash->data = SHA512_DIGEST_LENGTH;
1587 break;
1588 }
1589 default:
1590 return 0;
1591 }
1592
1593 smp->data.u.str = *trash;
1594 smp->data.type = SMP_T_BIN;
1595 smp->flags &= ~SMP_F_CONST;
1596 return 1;
1597}
1598#endif
1599
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001600static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001601{
Willy Tarreau83061a82018-07-13 11:56:34 +02001602 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001603 unsigned char c;
1604 int ptr = 0;
1605
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001606 trash->data = 0;
1607 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
1608 c = smp->data.u.str.area[ptr++];
1609 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1610 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001611 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001612 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001613 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001614 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001615 return 1;
1616}
1617
Dragan Dosen3f957b22017-10-24 09:27:34 +02001618static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1619{
1620 long long int n = 0;
1621 int i, c;
1622
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001623 for (i = 0; i < smp->data.u.str.data; i++) {
1624 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02001625 return 0;
1626 n = (n << 4) + c;
1627 }
1628
1629 smp->data.u.sint = n;
1630 smp->data.type = SMP_T_SINT;
1631 smp->flags &= ~SMP_F_CONST;
1632 return 1;
1633}
1634
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001635/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001636static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001637{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001638 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
1639 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001640 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001641 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001642 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001643 return 1;
1644}
1645
Willy Tarreau60a2ee72017-12-15 07:13:48 +01001646static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001647{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001648 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001649 smp->data.u.sint = i;
1650 smp->data.type = SMP_T_SINT;
1651 return 1;
1652}
1653
1654
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001655static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001656{
1657 int i;
1658
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001659 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001660 return 0;
1661
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001662 for (i = 0; i < smp->data.u.str.data; i++) {
1663 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
1664 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001665 }
1666 return 1;
1667}
1668
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001669static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001670{
1671 int i;
1672
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001673 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001674 return 0;
1675
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001676 for (i = 0; i < smp->data.u.str.data; i++) {
1677 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
1678 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001679 }
1680 return 1;
1681}
1682
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001683/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
1684static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001685{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001686 /* Attempt to convert to IPv4 to apply the correct mask. */
1687 c_ipv62ip(smp);
1688
1689 if (smp->data.type == SMP_T_IPV4) {
1690 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
1691 smp->data.type = SMP_T_IPV4;
1692 }
1693 else if (smp->data.type == SMP_T_IPV6) {
1694 /* IPv6 cannot be converted without an IPv6 mask. */
1695 if (args[1].type != ARGT_IPV6)
1696 return 0;
1697
1698 *(uint32_t*)&smp->data.u.ipv6.s6_addr[0] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[0];
1699 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[4];
1700 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[8];
1701 *(uint32_t*)&smp->data.u.ipv6.s6_addr[12] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[12];
1702 smp->data.type = SMP_T_IPV6;
1703 }
1704
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001705 return 1;
1706}
1707
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001708/* takes an UINT value on input supposed to represent the time since EPOCH,
1709 * adds an optional offset found in args[1] and emits a string representing
1710 * the local time in the format specified in args[1] using strftime().
1711 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001712static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001713{
Willy Tarreau83061a82018-07-13 11:56:34 +02001714 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001715 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001716 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001717 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001718
1719 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001720 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001721 curr_date += args[1].data.sint;
1722
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001723 tm = localtime(&curr_date);
1724 if (!tm)
1725 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001726 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001727 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1728 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001729 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001730 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001731 return 1;
1732}
1733
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001734/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001735static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001736{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001737 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
1738 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001739 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001740 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001741 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001742 return 1;
1743}
1744
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001745/* takes an UINT value on input supposed to represent the time since EPOCH,
1746 * adds an optional offset found in args[1] and emits a string representing
1747 * the UTC date in the format specified in args[1] using strftime().
1748 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001749static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001750{
Willy Tarreau83061a82018-07-13 11:56:34 +02001751 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001752 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001753 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001754 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001755
1756 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001757 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001758 curr_date += args[1].data.sint;
1759
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001760 tm = gmtime(&curr_date);
1761 if (!tm)
1762 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001763 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001764 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1765 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001766 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001767 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001768 return 1;
1769}
1770
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001771/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001772static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001773{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001774 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
1775 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001776 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001777 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001778 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001779 return 1;
1780}
1781
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001782/* hashes the binary input into a 32-bit unsigned int using xxh.
1783 * The seed of the hash defaults to 0 but can be changd in argument 1.
1784 */
1785static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
1786{
1787 unsigned int seed;
1788
1789 if (arg_p && arg_p->data.sint)
1790 seed = arg_p->data.sint;
1791 else
1792 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001793 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
1794 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001795 smp->data.type = SMP_T_SINT;
1796 return 1;
1797}
1798
1799/* hashes the binary input into a 64-bit unsigned int using xxh.
1800 * In fact, the function returns a 64 bit unsigned, but the sample
1801 * storage of haproxy only proposes 64-bits signed, so the value is
1802 * cast as signed. This cast doesn't impact the hash repartition.
1803 * The seed of the hash defaults to 0 but can be changd in argument 1.
1804 */
1805static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
1806{
1807 unsigned long long int seed;
1808
1809 if (arg_p && arg_p->data.sint)
1810 seed = (unsigned long long int)arg_p->data.sint;
1811 else
1812 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001813 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
1814 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001815 smp->data.type = SMP_T_SINT;
1816 return 1;
1817}
1818
Willy Tarreau80599772015-01-20 19:35:24 +01001819/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001820static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001821{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001822 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
1823 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001824 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001825 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001826 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01001827 return 1;
1828}
1829
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001830/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
1831static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
1832{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001833 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
1834 smp->data.u.str.data);
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001835 if (arg_p && arg_p->data.sint)
1836 smp->data.u.sint = full_hash(smp->data.u.sint);
1837 smp->data.type = SMP_T_SINT;
1838 return 1;
1839}
1840
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001841/* This function escape special json characters. The returned string can be
1842 * safely set between two '"' and used as json string. The json string is
1843 * defined like this:
1844 *
1845 * any Unicode character except '"' or '\' or control character
1846 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1847 *
1848 * The enum input_type contain all the allowed mode for decoding the input
1849 * string.
1850 */
1851enum input_type {
1852 IT_ASCII = 0,
1853 IT_UTF8,
1854 IT_UTF8S,
1855 IT_UTF8P,
1856 IT_UTF8PS,
1857};
1858static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1859 const char *file, int line, char **err)
1860{
1861 if (!arg) {
1862 memprintf(err, "Unexpected empty arg list");
1863 return 0;
1864 }
1865
1866 if (arg->type != ARGT_STR) {
1867 memprintf(err, "Unexpected arg type");
1868 return 0;
1869 }
1870
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001871 if (strcmp(arg->data.str.area, "") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001872 arg->type = ARGT_SINT;
1873 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001874 return 1;
1875 }
1876
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001877 else if (strcmp(arg->data.str.area, "ascii") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001878 arg->type = ARGT_SINT;
1879 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001880 return 1;
1881 }
1882
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001883 else if (strcmp(arg->data.str.area, "utf8") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001884 arg->type = ARGT_SINT;
1885 arg->data.sint = IT_UTF8;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001886 return 1;
1887 }
1888
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001889 else if (strcmp(arg->data.str.area, "utf8s") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001890 arg->type = ARGT_SINT;
1891 arg->data.sint = IT_UTF8S;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001892 return 1;
1893 }
1894
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001895 else if (strcmp(arg->data.str.area, "utf8p") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001896 arg->type = ARGT_SINT;
1897 arg->data.sint = IT_UTF8P;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001898 return 1;
1899 }
1900
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001901 else if (strcmp(arg->data.str.area, "utf8ps") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001902 arg->type = ARGT_SINT;
1903 arg->data.sint = IT_UTF8PS;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001904 return 1;
1905 }
1906
Thierry FOURNIER / OZON.IOa69c9122016-11-23 01:13:57 +01001907 memprintf(err, "Unexpected input code type. "
1908 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001909 return 0;
1910}
1911
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001912static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001913{
Willy Tarreau83061a82018-07-13 11:56:34 +02001914 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001915 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1916 const char *str;
1917 int len;
1918 enum input_type input_type = IT_ASCII;
1919 unsigned int c;
1920 unsigned int ret;
1921 char *p;
1922
1923 if (arg_p)
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001924 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001925
1926 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001927 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001928
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001929 p = smp->data.u.str.area;
1930 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001931
1932 if (input_type == IT_ASCII) {
1933 /* Read input as ASCII. */
1934 c = *(unsigned char *)p;
1935 p++;
1936 }
1937 else {
1938 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001939 ret = utf8_next(p,
1940 smp->data.u.str.data - ( p - smp->data.u.str.area),
1941 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001942 p += utf8_return_length(ret);
1943
1944 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
1945 return 0;
1946 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
1947 continue;
1948 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1949 return 0;
1950 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1951 continue;
1952
1953 /* Check too big values. */
1954 if ((unsigned int)c > 0xffff) {
1955 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
1956 return 0;
1957 continue;
1958 }
1959 }
1960
1961 /* Convert character. */
1962 if (c == '"') {
1963 len = 2;
1964 str = "\\\"";
1965 }
1966 else if (c == '\\') {
1967 len = 2;
1968 str = "\\\\";
1969 }
1970 else if (c == '/') {
1971 len = 2;
1972 str = "\\/";
1973 }
1974 else if (c == '\b') {
1975 len = 2;
1976 str = "\\b";
1977 }
1978 else if (c == '\f') {
1979 len = 2;
1980 str = "\\f";
1981 }
1982 else if (c == '\r') {
1983 len = 2;
1984 str = "\\r";
1985 }
1986 else if (c == '\n') {
1987 len = 2;
1988 str = "\\n";
1989 }
1990 else if (c == '\t') {
1991 len = 2;
1992 str = "\\t";
1993 }
1994 else if (c > 0xff || !isprint(c)) {
1995 /* isprint generate a segfault if c is too big. The man says that
1996 * c must have the value of an unsigned char or EOF.
1997 */
1998 len = 6;
1999 _str[0] = '\\';
2000 _str[1] = 'u';
2001 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2002 str = _str;
2003 }
2004 else {
2005 len = 1;
2006 str = (char *)&c;
2007 }
2008
2009 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002010 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002011 return 0;
2012
2013 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002014 memcpy(temp->area + temp->data, str, len);
2015 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002016 }
2017
2018 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002019 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002020 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002021
2022 return 1;
2023}
2024
Emeric Brun54c4ac82014-11-03 15:32:43 +01002025/* This sample function is designed to extract some bytes from an input buffer.
2026 * First arg is the offset.
2027 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002028static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002029{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002030 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2031 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002032 return 1;
2033 }
2034
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002035 if (smp->data.u.str.size)
2036 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002037 smp->data.u.str.data -= arg_p[0].data.sint;
2038 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002039
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002040 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2041 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002042
2043 return 1;
2044}
2045
Emeric Brunf399b0d2014-11-03 17:07:03 +01002046static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2047 const char *file, int line, char **err)
2048{
2049 struct arg *arg = args;
2050
2051 if (!arg) {
2052 memprintf(err, "Unexpected empty arg list");
2053 return 0;
2054 }
2055
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002056 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002057 memprintf(err, "Unexpected arg type");
2058 return 0;
2059 }
2060
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002061 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002062 memprintf(err, "Unexpected value 0 for index");
2063 return 0;
2064 }
2065
2066 arg++;
2067
2068 if (arg->type != ARGT_STR) {
2069 memprintf(err, "Unexpected arg type");
2070 return 0;
2071 }
2072
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002073 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002074 memprintf(err, "Empty separators list");
2075 return 0;
2076 }
2077
2078 return 1;
2079}
2080
2081/* This sample function is designed to a return selected part of a string (field).
2082 * First arg is the index of the field (start at 1)
2083 * Second arg is a char list of separators (type string)
2084 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002085static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002086{
Marcin Deranek9631a282018-04-16 14:30:46 +02002087 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002088 char *start, *end;
2089 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002090 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002091
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002092 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002093 return 0;
2094
Marcin Deranek9631a282018-04-16 14:30:46 +02002095 if (arg_p[0].data.sint < 0) {
2096 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002097 end = start = smp->data.u.str.area + smp->data.u.str.data;
2098 while (start > smp->data.u.str.area) {
2099 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2100 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002101 if (field == arg_p[0].data.sint) {
2102 if (count == 1)
2103 goto found;
2104 else if (count > 1)
2105 count--;
2106 } else {
2107 end = start-1;
2108 field--;
2109 }
2110 break;
2111 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002112 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002113 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002114 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002115 } else {
2116 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002117 end = start = smp->data.u.str.area;
2118 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2119 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2120 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002121 if (field == arg_p[0].data.sint) {
2122 if (count == 1)
2123 goto found;
2124 else if (count > 1)
2125 count--;
2126 } else {
2127 start = end+1;
2128 field++;
2129 }
2130 break;
2131 }
2132 }
2133 end++;
2134 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002135 }
2136
2137 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002138 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002139 smp->data.u.str.data = 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002140 return 1;
2141 }
2142found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002143 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002144 /* If ret string is len 0, no need to
2145 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002146 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002147 return 1;
2148
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002149 smp->data.u.str.area = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002150
2151 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002152 Note: smp->data.u.str.size cannot be set to 0 */
2153 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002154 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002155
2156 return 1;
2157}
2158
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002159/* This sample function is designed to return a word from a string.
2160 * First arg is the index of the word (start at 1)
2161 * Second arg is a char list of words separators (type string)
2162 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002163static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002164{
Marcin Deranek9631a282018-04-16 14:30:46 +02002165 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002166 char *start, *end;
2167 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002168 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002169
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002170 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002171 return 0;
2172
2173 word = 0;
2174 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002175 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002176 end = start = smp->data.u.str.area + smp->data.u.str.data;
2177 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002178 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002179 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2180 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002181 issep = 1;
2182 break;
2183 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002184 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002185 if (!inword) {
2186 if (!issep) {
2187 if (word != arg_p[0].data.sint) {
2188 word--;
2189 end = start;
2190 }
2191 inword = 1;
2192 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002193 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002194 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002195 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002196 if (count == 1)
2197 goto found;
2198 else if (count > 1)
2199 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002200 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002201 inword = 0;
2202 }
2203 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002204 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002205 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002206 end = start = smp->data.u.str.area;
2207 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002208 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002209 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2210 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002211 issep = 1;
2212 break;
2213 }
2214 }
2215 if (!inword) {
2216 if (!issep) {
2217 if (word != arg_p[0].data.sint) {
2218 word++;
2219 start = end;
2220 }
2221 inword = 1;
2222 }
2223 }
2224 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002225 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002226 if (count == 1)
2227 goto found;
2228 else if (count > 1)
2229 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002230 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002231 inword = 0;
2232 }
2233 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002234 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002235 }
2236
2237 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002238 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002239 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002240 return 1;
2241 }
2242found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002243 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002244 /* If ret string is len 0, no need to
2245 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002246 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002247 return 1;
2248
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002249 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002250
2251 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002252 Note: smp->data.u.str.size cannot be set to 0 */
2253 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002254 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002255
2256 return 1;
2257}
2258
Willy Tarreau7eda8492015-01-20 19:47:06 +01002259static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2260 const char *file, int line, char **err)
2261{
2262 struct arg *arg = args;
2263 char *p;
2264 int len;
2265
2266 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2267 arg[0].type_flags = 0;
2268
2269 if (arg[2].type != ARGT_STR)
2270 return 1;
2271
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002272 p = arg[2].data.str.area;
2273 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002274 while (len) {
2275 if (*p == 'i') {
2276 arg[0].type_flags |= ARGF_REG_ICASE;
2277 }
2278 else if (*p == 'g') {
2279 arg[0].type_flags |= ARGF_REG_GLOB;
2280 }
2281 else {
2282 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2283 return 0;
2284 }
2285 p++;
2286 len--;
2287 }
2288 return 1;
2289}
2290
2291/* This sample function is designed to do the equivalent of s/match/replace/ on
2292 * the input string. It applies a regex and restarts from the last matched
2293 * location until nothing matches anymore. First arg is the regex to apply to
2294 * the input string, second arg is the replacement expression.
2295 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002296static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002297{
2298 char *start, *end;
2299 struct my_regex *reg = arg_p[0].data.reg;
2300 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002301 struct buffer *trash = get_trash_chunk();
Willy Tarreau7eda8492015-01-20 19:47:06 +01002302 int flag, max;
2303 int found;
2304
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002305 start = smp->data.u.str.area;
2306 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002307
2308 flag = 0;
2309 while (1) {
2310 /* check for last round which is used to copy remaining parts
2311 * when not running in global replacement mode.
2312 */
2313 found = 0;
2314 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2315 /* Note: we can have start == end on empty strings or at the end */
2316 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2317 }
2318
2319 if (!found)
2320 pmatch[0].rm_so = end - start;
2321
2322 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002323 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002324 if (max && pmatch[0].rm_so > 0) {
2325 if (max > pmatch[0].rm_so)
2326 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002327 memcpy(trash->area + trash->data, start, max);
2328 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002329 }
2330
2331 if (!found)
2332 break;
2333
2334 /* replace the matching part */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002335 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002336 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002337 if (max > arg_p[1].data.str.data)
2338 max = arg_p[1].data.str.data;
2339 memcpy(trash->area + trash->data,
2340 arg_p[1].data.str.area, max);
2341 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002342 }
2343
2344 /* stop here if we're done with this string */
2345 if (start >= end)
2346 break;
2347
2348 /* We have a special case for matches of length 0 (eg: "x*y*").
2349 * These ones are considered to match in front of a character,
2350 * so we have to copy that character and skip to the next one.
2351 */
2352 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002353 if (trash->data < trash->size)
2354 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002355 pmatch[0].rm_eo++;
2356 }
2357
2358 start += pmatch[0].rm_eo;
2359 flag |= REG_NOTBOL;
2360 }
2361
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002362 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002363 return 1;
2364}
2365
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002366/* This function check an operator entry. It expects a string.
2367 * The string can be an integer or a variable name.
2368 */
2369static int check_operator(struct arg *args, struct sample_conv *conv,
2370 const char *file, int line, char **err)
2371{
2372 const char *str;
2373 const char *end;
2374
2375 /* Try to decode a variable. */
2376 if (vars_check_arg(&args[0], NULL))
2377 return 1;
2378
2379 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002380 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002381 end = str + strlen(str);
2382 args[0].data.sint = read_int64(&str, end);
2383 if (*str != '\0') {
2384 memprintf(err, "expects an integer or a variable name");
2385 return 0;
2386 }
2387 args[0].type = ARGT_SINT;
2388 return 1;
2389}
2390
Willy Tarreau6204cd92016-03-10 16:33:04 +01002391/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002392 * If the arg contain an integer, the integer is returned in the
2393 * sample. If the arg contains a variable descriptor, it returns the
2394 * variable value.
2395 *
2396 * This function returns 0 if an error occurs, otherwise it returns 1.
2397 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002398static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002399{
2400 switch (arg->type) {
2401 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002402 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002403 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002404 return 1;
2405 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002406 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002407 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002408 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002409 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002410 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002411 return 0;
2412 return 1;
2413 default:
2414 return 0;
2415 }
2416}
2417
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002418/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002419 * result.
2420 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002421static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002422{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002423 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002424 return 1;
2425}
2426
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002427/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002428 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002429 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002430static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002431{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002432 struct sample tmp;
2433
Willy Tarreau7560dd42016-03-10 16:28:58 +01002434 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002435 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002436 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002437 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002438 return 1;
2439}
2440
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002441/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002442 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002443 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002444static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002445{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002446 struct sample tmp;
2447
Willy Tarreau7560dd42016-03-10 16:28:58 +01002448 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002449 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002450 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002451 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002452 return 1;
2453}
2454
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002455/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002456 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002457 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002458static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002459{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002460 struct sample tmp;
2461
Willy Tarreau7560dd42016-03-10 16:28:58 +01002462 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002463 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002464 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002465 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002466 return 1;
2467}
2468
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002469static inline long long int arith_add(long long int a, long long int b)
2470{
2471 /* Prevent overflow and makes capped calculus.
2472 * We must ensure that the check calculus doesn't
2473 * exceed the signed 64 bits limits.
2474 *
2475 * +----------+----------+
2476 * | a<0 | a>=0 |
2477 * +------+----------+----------+
2478 * | b<0 | MIN-a>b | no check |
2479 * +------+----------+----------+
2480 * | b>=0 | no check | MAX-a<b |
2481 * +------+----------+----------+
2482 */
2483 if ((a ^ b) >= 0) {
2484 /* signs are differents. */
2485 if (a < 0) {
2486 if (LLONG_MIN - a > b)
2487 return LLONG_MIN;
2488 }
2489 if (LLONG_MAX - a < b)
2490 return LLONG_MAX;
2491 }
2492 return a + b;
2493}
2494
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002495/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002496 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002497 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002498static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002499{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002500 struct sample tmp;
2501
Willy Tarreau7560dd42016-03-10 16:28:58 +01002502 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002503 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002504 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002505 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002506 return 1;
2507}
2508
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002509/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002510 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002511 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002512static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002513 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002514{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002515 struct sample tmp;
2516
Willy Tarreau7560dd42016-03-10 16:28:58 +01002517 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002518 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002519 return 0;
2520
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002521 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2522 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2523 * of -LLONG_MIN and correct the result.
2524 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002525 if (tmp.data.u.sint == LLONG_MIN) {
2526 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2527 if (smp->data.u.sint < LLONG_MAX)
2528 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002529 return 1;
2530 }
2531
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002532 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002533 * the second operand.
2534 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002535 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002536 return 1;
2537}
2538
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002539/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002540 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002541 * If the result makes an overflow, then the largest possible quantity is
2542 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002543 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002544static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002545 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002546{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002547 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002548 long long int c;
2549
Willy Tarreau7560dd42016-03-10 16:28:58 +01002550 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002551 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002552 return 0;
2553
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002554 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002555 if (!smp->data.u.sint || !tmp.data.u.sint) {
2556 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002557 return 1;
2558 }
2559
2560 /* The multiply between LLONG_MIN and -1 returns a
2561 * "floting point exception".
2562 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002563 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2564 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002565 return 1;
2566 }
2567
2568 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002569 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002570
2571 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002572 if (smp->data.u.sint != c / tmp.data.u.sint) {
2573 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2574 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002575 return 1;
2576 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002577 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002578 return 1;
2579 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002580 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002581 return 1;
2582}
2583
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002584/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002585 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002586 * If arg_p makes the result overflow, then the largest possible quantity is
2587 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002588 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002589static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002590 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002591{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002592 struct sample tmp;
2593
Willy Tarreau7560dd42016-03-10 16:28:58 +01002594 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002595 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002596 return 0;
2597
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002598 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002599 /* The divide between LLONG_MIN and -1 returns a
2600 * "floting point exception".
2601 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002602 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2603 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002604 return 1;
2605 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002606 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002607 return 1;
2608 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002609 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002610 return 1;
2611}
2612
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002613/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002614 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002615 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002616 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002617static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002618 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002619{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002620 struct sample tmp;
2621
Willy Tarreau7560dd42016-03-10 16:28:58 +01002622 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002623 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002624 return 0;
2625
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002626 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002627 /* The divide between LLONG_MIN and -1 returns a
2628 * "floting point exception".
2629 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002630 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2631 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002632 return 1;
2633 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002634 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002635 return 1;
2636 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002637 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002638 return 1;
2639}
2640
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002641/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002642 * result.
2643 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002644static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002645 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002646{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002647 if (smp->data.u.sint == LLONG_MIN)
2648 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002649 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002650 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002651 return 1;
2652}
2653
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002654/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002655 * false. The output is a BOOL.
2656 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002657static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002658 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002659{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002660 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002661 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002662 return 1;
2663}
2664
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002665/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002666 * truee. The output is a BOOL.
2667 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002668static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002669 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002670{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002671 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002672 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002673 return 1;
2674}
2675
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002676/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002677 * The output is a BOOL.
2678 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002679static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002680 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002681{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002682 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002683 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002684 return 1;
2685}
2686
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002687/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002688 * The output is a BOOL.
2689 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002690static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002691 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002692{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002693 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002694 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002695 return 1;
2696}
2697
Willy Tarreau280f42b2018-02-19 15:34:12 +01002698/* appends an optional const string, an optional variable contents and another
2699 * optional const string to an existing string.
2700 */
2701static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
2702{
Willy Tarreau83061a82018-07-13 11:56:34 +02002703 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002704 struct sample tmp;
2705 int max;
2706
2707 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002708 trash->data = smp->data.u.str.data;
2709 if (trash->data > trash->size - 1)
2710 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002711
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002712 memcpy(trash->area, smp->data.u.str.area, trash->data);
2713 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002714
2715 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002716 max = arg_p[0].data.str.data;
2717 if (max > trash->size - 1 - trash->data)
2718 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002719
2720 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002721 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
2722 trash->data += max;
2723 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002724 }
2725
2726 /* append second string (variable) if it's found and we can turn it
2727 * into a string.
2728 */
2729 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2730 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
2731 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
2732 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
2733
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002734 max = tmp.data.u.str.data;
2735 if (max > trash->size - 1 - trash->data)
2736 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002737
2738 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002739 memcpy(trash->area + trash->data, tmp.data.u.str.area,
2740 max);
2741 trash->data += max;
2742 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002743 }
2744 }
2745
2746 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002747 max = arg_p[2].data.str.data;
2748 if (max > trash->size - 1 - trash->data)
2749 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002750
2751 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002752 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
2753 trash->data += max;
2754 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002755 }
2756
2757 smp->data.u.str = *trash;
2758 smp->data.type = SMP_T_STR;
2759 return 1;
2760}
2761
2762/* This function checks the "concat" converter's arguments and extracts the
2763 * variable name and its scope.
2764 */
2765static int smp_check_concat(struct arg *args, struct sample_conv *conv,
2766 const char *file, int line, char **err)
2767{
2768 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002769 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
2770 memprintf(err, "failed to register variable name '%s'",
2771 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01002772 return 0;
2773 }
2774 return 1;
2775}
2776
Tim Duesterhusca097c12018-04-27 21:18:45 +02002777/* compares string with a variable containing a string. Return value
2778 * is compatible with strcmp(3)'s return value.
2779 */
2780static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
2781{
2782 struct sample tmp;
2783 int max, result;
2784
2785 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2786 if (arg_p[0].type != ARGT_VAR)
2787 return 0;
2788 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
2789 return 0;
2790 if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp))
2791 return 0;
2792
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002793 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
2794 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002795 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002796 if (smp->data.u.str.data != tmp.data.u.str.data) {
2797 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02002798 result = -1;
2799 }
2800 else {
2801 result = 1;
2802 }
2803 }
2804 }
2805
2806 smp->data.u.sint = result;
2807 smp->data.type = SMP_T_SINT;
2808 return 1;
2809}
2810
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002811#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
2812#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
2813#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
2814
2815/*
2816 * Extract the field value of an input binary sample. Takes a mandatory argument:
2817 * the protocol buffers field identifier (dotted notation) internally represented
2818 * as an array of unsigned integers and its size.
2819 * Return 1 if the field was found, 0 if not.
2820 */
2821static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
2822{
2823 unsigned char *pos;
2824 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002825
2826 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002827 grpc_left = smp->data.u.str.data;
2828
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01002829 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002830 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002831
2832 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
2833
2834 pos += GRPC_MSG_HEADER_SZ;
2835 grpc_left -= GRPC_MSG_HEADER_SZ;
2836
2837 if (grpc_left < left)
2838 return 0;
2839
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01002840 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
2841 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002842
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002843 grpc_left -= grpc_msg_len;
2844 }
2845
2846 return 0;
2847}
2848
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01002849static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
2850{
2851 unsigned char *pos;
2852 size_t left;
2853
2854 pos = (unsigned char *)smp->data.u.str.area;
2855 left = smp->data.u.str.data;
2856
2857 return protobuf_field_lookup(arg_p, smp, &pos, &left);
2858}
2859
2860static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
2861 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01002862{
2863 if (!args[1].type) {
2864 args[1].type = ARGT_SINT;
2865 args[1].data.sint = PBUF_T_BINARY;
2866 }
2867 else {
2868 int pbuf_type;
2869
2870 pbuf_type = protobuf_type(args[1].data.str.area);
2871 if (pbuf_type == -1) {
2872 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
2873 return 0;
2874 }
2875
2876 args[1].type = ARGT_SINT;
2877 args[1].data.sint = pbuf_type;
2878 }
2879
2880 return 1;
2881}
2882
Tim Duesterhusca097c12018-04-27 21:18:45 +02002883/* This function checks the "strcmp" converter's arguments and extracts the
2884 * variable name and its scope.
2885 */
2886static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
2887 const char *file, int line, char **err)
2888{
2889 /* Try to decode a variable. */
2890 if (vars_check_arg(&args[0], NULL))
2891 return 1;
2892
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002893 memprintf(err, "failed to register variable name '%s'",
2894 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002895 return 0;
2896}
2897
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002898/************************************************************************/
2899/* All supported sample fetch functions must be declared here */
2900/************************************************************************/
2901
2902/* force TRUE to be returned at the fetch level */
2903static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002904smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002905{
Willy Tarreau280f42b2018-02-19 15:34:12 +01002906 if (!smp_make_rw(smp))
2907 return 0;
2908
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002909 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002910 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002911 return 1;
2912}
2913
2914/* force FALSE to be returned at the fetch level */
2915static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002916smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002917{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002918 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002919 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002920 return 1;
2921}
2922
2923/* retrieve environment variable $1 as a string */
2924static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002925smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002926{
2927 char *env;
2928
2929 if (!args || args[0].type != ARGT_STR)
2930 return 0;
2931
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002932 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002933 if (!env)
2934 return 0;
2935
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002936 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002937 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002938 smp->data.u.str.area = env;
2939 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002940 return 1;
2941}
2942
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002943/* retrieve the current local date in epoch time, and applies an optional offset
2944 * of args[0] seconds.
2945 */
2946static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002947smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002948{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002949 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002950
2951 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002952 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002953 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002954
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002955 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002956 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2957 return 1;
2958}
2959
Etienne Carrierea792a0a2018-01-17 13:43:24 +01002960/* retrieve the current microsecond part of the date */
2961static int
2962smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
2963{
2964 smp->data.u.sint = date.tv_usec;
2965 smp->data.type = SMP_T_SINT;
2966 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2967 return 1;
2968}
2969
2970
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01002971/* returns the hostname */
2972static int
2973smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
2974{
2975 smp->data.type = SMP_T_STR;
2976 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002977 smp->data.u.str.area = hostname;
2978 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01002979 return 1;
2980}
2981
Willy Tarreau0f30d262014-11-24 16:02:05 +01002982/* returns the number of processes */
2983static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002984smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002985{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002986 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002987 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002988 return 1;
2989}
2990
2991/* returns the number of the current process (between 1 and nbproc */
2992static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002993smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002994{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002995 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002996 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002997 return 1;
2998}
2999
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003000/* returns the number of the current thread (between 1 and nbthread */
3001static int
3002smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3003{
3004 smp->data.type = SMP_T_SINT;
3005 smp->data.u.sint = tid;
3006 return 1;
3007}
3008
Willy Tarreau84310e22014-02-14 11:59:04 +01003009/* generate a random 32-bit integer for whatever purpose, with an optional
3010 * range specified in argument.
3011 */
3012static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003013smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01003014{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003015 smp->data.u.sint = random();
Willy Tarreau84310e22014-02-14 11:59:04 +01003016
3017 /* reduce if needed. Don't do a modulo, use all bits! */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003018 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003019 smp->data.u.sint = (smp->data.u.sint * args[0].data.sint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01003020
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003021 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01003022 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3023 return 1;
3024}
3025
Willy Tarreau0f30d262014-11-24 16:02:05 +01003026/* returns true if the current process is stopping */
3027static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003028smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003029{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003030 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003031 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003032 return 1;
3033}
3034
Willy Tarreau70fe9442018-11-22 16:07:39 +01003035/* returns the number of calls of the current stream's process_stream() */
3036static int
3037smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
3038{
3039 smp->data.type = SMP_T_SINT;
3040 smp->data.u.sint = smp->strm->task->calls;
3041 return 1;
3042}
3043
3044/* returns the average number of nanoseconds spent processing the stream per call */
3045static int
3046smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3047{
3048 smp->data.type = SMP_T_SINT;
3049 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
3050 return 1;
3051}
3052
3053/* returns the total number of nanoseconds spent processing the stream */
3054static int
3055smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3056{
3057 smp->data.type = SMP_T_SINT;
3058 smp->data.u.sint = smp->strm->task->cpu_time;
3059 return 1;
3060}
3061
3062/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
3063static int
3064smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3065{
3066 smp->data.type = SMP_T_SINT;
3067 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
3068 return 1;
3069}
3070
3071/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
3072static int
3073smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3074{
3075 smp->data.type = SMP_T_SINT;
3076 smp->data.u.sint = smp->strm->task->lat_time;
3077 return 1;
3078}
3079
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003080static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
3081{
3082 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003083 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003084 smp->data.u.str.area = args[0].data.str.area;
3085 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003086 return 1;
3087}
3088
3089static int smp_check_const_bool(struct arg *args, char **err)
3090{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003091 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
3092 strcasecmp(args[0].data.str.area, "1") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003093 args[0].type = ARGT_SINT;
3094 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003095 return 1;
3096 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003097 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
3098 strcasecmp(args[0].data.str.area, "0") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003099 args[0].type = ARGT_SINT;
3100 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003101 return 1;
3102 }
3103 memprintf(err, "Expects 'true', 'false', '0' or '1'");
3104 return 0;
3105}
3106
3107static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
3108{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003109 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003110 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003111 return 1;
3112}
3113
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003114static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003115{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003116 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003117 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003118 return 1;
3119}
3120
3121static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
3122{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003123 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003124 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003125 return 1;
3126}
3127
3128static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
3129{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003130 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003131 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003132 return 1;
3133}
3134
3135static int smp_check_const_bin(struct arg *args, char **err)
3136{
David Carlier64a16ab2016-04-08 10:37:02 +01003137 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003138 int binstrlen;
3139
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003140 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003141 return 0;
3142 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003143 args[0].data.str.area = binstr;
3144 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003145 return 1;
3146}
3147
3148static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
3149{
3150 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003151 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003152 smp->data.u.str.area = args[0].data.str.area;
3153 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003154 return 1;
3155}
3156
3157static int smp_check_const_meth(struct arg *args, char **err)
3158{
3159 enum http_meth_t meth;
3160 int i;
3161
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003162 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003163 if (meth != HTTP_METH_OTHER) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003164 args[0].type = ARGT_SINT;
3165 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003166 } else {
3167 /* Check method avalaibility. A methos is a token defined as :
3168 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
3169 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
3170 * token = 1*tchar
3171 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003172 for (i = 0; i < args[0].data.str.data; i++) {
3173 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003174 memprintf(err, "expects valid method.");
3175 return 0;
3176 }
3177 }
3178 }
3179 return 1;
3180}
3181
3182static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
3183{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003184 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003185 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003186 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003187 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003188 smp->data.u.meth.str.area = "";
3189 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003190 } else {
3191 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003192 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003193 smp->data.u.meth.str.area = args[0].data.str.area;
3194 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003195 }
3196 return 1;
3197}
3198
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003199/* Note: must not be declared <const> as its list will be overwritten.
3200 * Note: fetches that may return multiple types must be declared as the lowest
3201 * common denominator, the type that can be casted into all other ones. For
3202 * instance IPv4/IPv6 must be declared IPv4.
3203 */
3204static struct sample_fetch_kw_list smp_kws = {ILH, {
3205 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
3206 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003207 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003208 { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003209 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003210 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003211 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3212 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003213 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003214 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01003215 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003216 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
3217
3218 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3219 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3220 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3221 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3222 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003223
3224 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
3225 { "bool", smp_fetch_const_bool, ARG1(1,STR), smp_check_const_bool, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003226 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003227 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
3228 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
3229 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
3230 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
3231
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003232 { /* END */ },
3233}};
3234
Willy Tarreau0108d902018-11-25 19:14:37 +01003235INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
3236
Emeric Brun107ca302010-01-04 16:16:05 +01003237/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003238static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02003239#ifdef DEBUG_EXPR
3240 { "debug", sample_conv_debug, 0, NULL, SMP_T_ANY, SMP_T_ANY },
3241#endif
3242
Holger Just1bfc24b2017-05-06 00:56:53 +02003243 { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN },
Emeric Brun53d1a982014-04-30 18:21:37 +02003244 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02003245 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
3246 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau60a2ee72017-12-15 07:13:48 +01003247 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01003248 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Dragan Dosen3f957b22017-10-24 09:27:34 +02003249 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
Tim Duesterhus1478aa72018-01-25 16:24:51 +01003250 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003251 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
3252 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003253 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01003254 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003255 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3256 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3257 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER01e09742016-12-26 11:46:11 +01003258 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3259 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02003260 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003261 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
Marcin Deranek9631a282018-04-16 14:30:46 +02003262 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
3263 { "word", sample_conv_word, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
Willy Tarreau7eda8492015-01-20 19:47:06 +01003264 { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02003265 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02003266#ifdef USE_OPENSSL
3267 { "sha2", sample_conv_sha2, ARG1(0, SINT), NULL, SMP_T_BIN, SMP_T_BIN },
3268#endif
Willy Tarreau280f42b2018-02-19 15:34:12 +01003269 { "concat", sample_conv_concat, ARG3(1,STR,STR,STR), smp_check_concat, SMP_T_STR, SMP_T_STR },
Tim Duesterhusca097c12018-04-27 21:18:45 +02003270 { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT },
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003271
3272 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003273 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
3274 { "protobuf", sample_conv_protobuf, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
Willy Tarreau97707872015-01-27 15:12:13 +01003275
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003276 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3277 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3278 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3279 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
3280 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3281 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3282 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3283 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3284 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3285 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3286 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3287 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3288 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3289 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01003290
Willy Tarreau9fcb9842012-04-20 14:45:49 +02003291 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01003292}};
3293
Willy Tarreau0108d902018-11-25 19:14:37 +01003294INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);