1 /*
2  * Sample management functions.
3  *
4  * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
5  * Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version
10  * 2 of the License, or (at your option) any later version.
11  *
12  */
13 
14 #include <ctype.h>
15 #include <string.h>
16 #include <arpa/inet.h>
17 #include <stdio.h>
18 
19 #include <import/mjson.h>
20 #include <import/sha1.h>
21 #include <import/xxhash.h>
22 
23 #include <haproxy/api.h>
24 #include <haproxy/arg.h>
25 #include <haproxy/auth.h>
26 #include <haproxy/base64.h>
27 #include <haproxy/buf.h>
28 #include <haproxy/chunk.h>
29 #include <haproxy/errors.h>
30 #include <haproxy/fix.h>
31 #include <haproxy/global.h>
32 #include <haproxy/hash.h>
33 #include <haproxy/http.h>
34 #include <haproxy/istbuf.h>
35 #include <haproxy/mqtt.h>
36 #include <haproxy/net_helper.h>
37 #include <haproxy/protobuf.h>
38 #include <haproxy/proxy.h>
39 #include <haproxy/regex.h>
40 #include <haproxy/sample.h>
41 #include <haproxy/sink.h>
42 #include <haproxy/stick_table.h>
43 #include <haproxy/tools.h>
44 #include <haproxy/uri_auth-t.h>
45 #include <haproxy/vars.h>
46 
47 /* sample type names */
48 const char *smp_to_type[SMP_TYPES] = {
49 	[SMP_T_ANY]  = "any",
50 	[SMP_T_BOOL] = "bool",
51 	[SMP_T_SINT] = "sint",
52 	[SMP_T_ADDR] = "addr",
53 	[SMP_T_IPV4] = "ipv4",
54 	[SMP_T_IPV6] = "ipv6",
55 	[SMP_T_STR]  = "str",
56 	[SMP_T_BIN]  = "bin",
57 	[SMP_T_METH] = "meth",
58 };
59 
60 /* static sample used in sample_process() when <p> is NULL */
61 static THREAD_LOCAL struct sample temp_smp;
62 
63 /* list head of all known sample fetch keywords */
64 static struct sample_fetch_kw_list sample_fetches = {
65 	.list = LIST_HEAD_INIT(sample_fetches.list)
66 };
67 
68 /* list head of all known sample format conversion keywords */
69 static struct sample_conv_kw_list sample_convs = {
70 	.list = LIST_HEAD_INIT(sample_convs.list)
71 };
72 
73 const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
74 	[SMP_SRC_CONST] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
75 	                   SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
76 	                   SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
77 	                   SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
78 	                   SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
79 	                   SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
80 	                   SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL_CFG_PARSER |
81 	                   SMP_VAL_CLI_PARSER ),
82 
83 	[SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
84 	                   SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
85 	                   SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
86 	                   SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
87 	                   SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
88 	                   SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
89 	                   SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
90 	                   SMP_VAL_CLI_PARSER ),
91 
92 	[SMP_SRC_LISTN] = (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 | SMP_VAL___________ | SMP_VAL___________ |
99 	                   SMP_VAL___________ ),
100 
101 	[SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
102 	                   SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
103 	                   SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
104 	                   SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
105 	                   SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
106 	                   SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
107 	                   SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
108 	                   SMP_VAL___________ ),
109 
110 	[SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
111 	                   SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
112 	                   SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
113 	                   SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
114 	                   SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
115 	                   SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
116 	                   SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
117 	                   SMP_VAL___________ ),
118 
119 	[SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
120 	                   SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
121 	                   SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
122 	                   SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
123 	                   SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
124 	                   SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
125 	                   SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
126 	                   SMP_VAL___________ ),
127 
128 	[SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
129 	                   SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
130 	                   SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
131 	                   SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
132 	                   SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
133 	                   SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
134 	                   SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
135 	                   SMP_VAL___________ ),
136 
137 	[SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
138 	                   SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
139 	                   SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
140 	                   SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
141 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
142 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
143 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
144 	                   SMP_VAL___________ ),
145 
146 	[SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
147 	                   SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
148 	                   SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
149 	                   SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
150 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
151 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
152 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
153 	                   SMP_VAL___________ ),
154 
155 	[SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
156 	                   SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
157 	                   SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
158 	                   SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
159 	                   SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
160 	                   SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
161 	                   SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
162 	                   SMP_VAL___________ ),
163 
164 	[SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
165 	                   SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
166 	                   SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
167 	                   SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
168 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
169 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
170 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
171 	                   SMP_VAL___________ ),
172 
173 	[SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
174 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
175 	                   SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
176 	                   SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
177 	                   SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
178 	                   SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
179 	                   SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
180 	                   SMP_VAL___________ ),
181 
182 	[SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
183 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
184 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
185 	                   SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
186 	                   SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
187 	                   SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
188 	                   SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
189 	                   SMP_VAL___________ ),
190 
191 	[SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
192 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
193 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
194 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
195 	                   SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
196 	                   SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
197 	                   SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
198 	                   SMP_VAL___________ ),
199 
200 	[SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
201 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
202 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
203 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
204 	                   SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
205 	                   SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
206 	                   SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
207 	                   SMP_VAL___________ ),
208 
209 	[SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
210 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
211 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
212 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
213 	                   SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
214 	                   SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
215 	                   SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
216 	                   SMP_VAL___________ ),
217 
218 	[SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
219 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
220 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
221 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
222 	                   SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
223 	                   SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
224 	                   SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
225 	                   SMP_VAL___________ ),
226 
227 	[SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
228 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
229 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
230 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
231 	                   SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
232 	                   SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
233 	                   SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
234 	                   SMP_VAL___________ ),
235 
236 	[SMP_SRC_HRSBO] = (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_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
241 	                   SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
242 	                   SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
243 	                   SMP_VAL___________ ),
244 
245 	[SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
246 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
247 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
248 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
249 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
250 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
251 	                   SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
252 	                   SMP_VAL___________ ),
253 
254 	[SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
255 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
256 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
257 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
258 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
259 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
260 	                   SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
261 	                   SMP_VAL___________ ),
262 
263 	[SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
264 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
265 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
266 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
267 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
268 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
269 	                   SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
270 	                   SMP_VAL___________ ),
271 
272 	[SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
273 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
274 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
275 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
276 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
277 	                   SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
278 	                   SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
279 	                   SMP_VAL___________ ),
280 };
281 
282 static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
283 	[SMP_SRC_INTRN] = "internal state",
284 	[SMP_SRC_LISTN] = "listener",
285 	[SMP_SRC_FTEND] = "frontend",
286 	[SMP_SRC_L4CLI] = "client address",
287 	[SMP_SRC_L5CLI] = "client-side connection",
288 	[SMP_SRC_TRACK] = "track counters",
289 	[SMP_SRC_L6REQ] = "request buffer",
290 	[SMP_SRC_HRQHV] = "HTTP request headers",
291 	[SMP_SRC_HRQHP] = "HTTP request",
292 	[SMP_SRC_HRQBO] = "HTTP request body",
293 	[SMP_SRC_BKEND] = "backend",
294 	[SMP_SRC_SERVR] = "server",
295 	[SMP_SRC_L4SRV] = "server address",
296 	[SMP_SRC_L5SRV] = "server-side connection",
297 	[SMP_SRC_L6RES] = "response buffer",
298 	[SMP_SRC_HRSHV] = "HTTP response headers",
299 	[SMP_SRC_HRSHP] = "HTTP response",
300 	[SMP_SRC_HRSBO] = "HTTP response body",
301 	[SMP_SRC_RQFIN] = "request buffer statistics",
302 	[SMP_SRC_RSFIN] = "response buffer statistics",
303 	[SMP_SRC_TXFIN] = "transaction statistics",
304 	[SMP_SRC_SSFIN] = "session statistics",
305 };
306 
307 static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
308 	[SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
309 	[SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
310 	[SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
311 	[SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
312 	[SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
313 	[SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
314 	[SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
315 	[SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
316 	[SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
317 	[SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
318 	[SMP_CKP_BE_SRV_CON] = "server source selection",
319 	[SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
320 	[SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
321 	[SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
322 	[SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
323 	[SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
324 	[SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
325 	[SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
326 	[SMP_CKP_FE_LOG_END] = "logs",
327 	[SMP_CKP_BE_CHK_RUL] = "backend tcp-check rule",
328 };
329 
330 /* This function returns the type of the data returned by the sample_expr.
331  * It assumes that the <expr> and all of its converters are properly
332  * initialized.
333  */
334 inline
smp_expr_output_type(struct sample_expr * expr)335 int smp_expr_output_type(struct sample_expr *expr)
336 {
337 	struct sample_conv_expr *smp_expr;
338 
339 	if (!LIST_ISEMPTY(&expr->conv_exprs)) {
340 		smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
341 		return smp_expr->conv->out_type;
342 	}
343 	return expr->fetch->out_type;
344 }
345 
346 
347 /* fill the trash with a comma-delimited list of source names for the <use> bit
348  * field which must be composed of a non-null set of SMP_USE_* flags. The return
349  * value is the pointer to the string in the trash buffer.
350  */
sample_src_names(unsigned int use)351 const char *sample_src_names(unsigned int use)
352 {
353 	int bit;
354 
355 	trash.data = 0;
356 	trash.area[0] = '\0';
357 	for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
358 		if (!(use & ~((1 << bit) - 1)))
359 			break; /* no more bits */
360 
361 		if (!(use & (1 << bit)))
362 			continue; /* bit not set */
363 
364 		trash.data += snprintf(trash.area + trash.data,
365 				       trash.size - trash.data, "%s%s",
366 				       (use & ((1 << bit) - 1)) ? "," : "",
367 				       fetch_src_names[bit]);
368 	}
369 	return trash.area;
370 }
371 
372 /* return a pointer to the correct sample checkpoint name, or "unknown" when
373  * the flags are invalid. Only the lowest bit is used, higher bits are ignored
374  * if set.
375  */
sample_ckp_names(unsigned int use)376 const char *sample_ckp_names(unsigned int use)
377 {
378 	int bit;
379 
380 	for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
381 		if (use & (1 << bit))
382 			return fetch_ckp_names[bit];
383 	return "unknown sample check place, please report this bug";
384 }
385 
386 /*
387  * Registers the sample fetch keyword list <kwl> as a list of valid keywords
388  * for next parsing sessions. The fetch keywords capabilities are also computed
389  * from their ->use field.
390  */
sample_register_fetches(struct sample_fetch_kw_list * kwl)391 void sample_register_fetches(struct sample_fetch_kw_list *kwl)
392 {
393 	struct sample_fetch *sf;
394 	int bit;
395 
396 	for (sf = kwl->kw; sf->kw != NULL; sf++) {
397 		for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
398 			if (sf->use & (1 << bit))
399 				sf->val |= fetch_cap[bit];
400 	}
401 	LIST_APPEND(&sample_fetches.list, &kwl->list);
402 }
403 
404 /*
405  * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
406  * parsing sessions.
407  */
sample_register_convs(struct sample_conv_kw_list * pckl)408 void sample_register_convs(struct sample_conv_kw_list *pckl)
409 {
410 	LIST_APPEND(&sample_convs.list, &pckl->list);
411 }
412 
413 /*
414  * Returns the pointer on sample fetch keyword structure identified by
415  * string of <len> in buffer <kw>.
416  *
417  */
find_sample_fetch(const char * kw,int len)418 struct sample_fetch *find_sample_fetch(const char *kw, int len)
419 {
420 	int index;
421 	struct sample_fetch_kw_list *kwl;
422 
423 	list_for_each_entry(kwl, &sample_fetches.list, list) {
424 		for (index = 0; kwl->kw[index].kw != NULL; index++) {
425 			if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
426 			    kwl->kw[index].kw[len] == '\0')
427 				return &kwl->kw[index];
428 		}
429 	}
430 	return NULL;
431 }
432 
433 /* This function browses the list of available sample fetches. <current> is
434  * the last used sample fetch. If it is the first call, it must set to NULL.
435  * <idx> is the index of the next sample fetch entry. It is used as private
436  * value. It is useless to initiate it.
437  *
438  * It returns always the new fetch_sample entry, and NULL when the end of
439  * the list is reached.
440  */
sample_fetch_getnext(struct sample_fetch * current,int * idx)441 struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
442 {
443 	struct sample_fetch_kw_list *kwl;
444 	struct sample_fetch *base;
445 
446 	if (!current) {
447 		/* Get first kwl entry. */
448 		kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
449 		(*idx) = 0;
450 	} else {
451 		/* Get kwl corresponding to the curret entry. */
452 		base = current + 1 - (*idx);
453 		kwl = container_of(base, struct sample_fetch_kw_list, kw);
454 	}
455 
456 	while (1) {
457 
458 		/* Check if kwl is the last entry. */
459 		if (&kwl->list == &sample_fetches.list)
460 			return NULL;
461 
462 		/* idx contain the next keyword. If it is available, return it. */
463 		if (kwl->kw[*idx].kw) {
464 			(*idx)++;
465 			return &kwl->kw[(*idx)-1];
466 		}
467 
468 		/* get next entry in the main list, and return NULL if the end is reached. */
469 		kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
470 
471 		/* Set index to 0, ans do one other loop. */
472 		(*idx) = 0;
473 	}
474 }
475 
476 /* This function browses the list of available converters. <current> is
477  * the last used converter. If it is the first call, it must set to NULL.
478  * <idx> is the index of the next converter entry. It is used as private
479  * value. It is useless to initiate it.
480  *
481  * It returns always the next sample_conv entry, and NULL when the end of
482  * the list is reached.
483  */
sample_conv_getnext(struct sample_conv * current,int * idx)484 struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
485 {
486 	struct sample_conv_kw_list *kwl;
487 	struct sample_conv *base;
488 
489 	if (!current) {
490 		/* Get first kwl entry. */
491 		kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
492 		(*idx) = 0;
493 	} else {
494 		/* Get kwl corresponding to the curret entry. */
495 		base = current + 1 - (*idx);
496 		kwl = container_of(base, struct sample_conv_kw_list, kw);
497 	}
498 
499 	while (1) {
500 		/* Check if kwl is the last entry. */
501 		if (&kwl->list == &sample_convs.list)
502 			return NULL;
503 
504 		/* idx contain the next keyword. If it is available, return it. */
505 		if (kwl->kw[*idx].kw) {
506 			(*idx)++;
507 			return &kwl->kw[(*idx)-1];
508 		}
509 
510 		/* get next entry in the main list, and return NULL if the end is reached. */
511 		kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
512 
513 		/* Set index to 0, ans do one other loop. */
514 		(*idx) = 0;
515 	}
516 }
517 
518 /*
519  * Returns the pointer on sample format conversion keyword structure identified by
520  * string of <len> in buffer <kw>.
521  *
522  */
find_sample_conv(const char * kw,int len)523 struct sample_conv *find_sample_conv(const char *kw, int len)
524 {
525 	int index;
526 	struct sample_conv_kw_list *kwl;
527 
528 	list_for_each_entry(kwl, &sample_convs.list, list) {
529 		for (index = 0; kwl->kw[index].kw != NULL; index++) {
530 			if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
531 			    kwl->kw[index].kw[len] == '\0')
532 				return &kwl->kw[index];
533 		}
534 	}
535 	return NULL;
536 }
537 
538 /******************************************************************/
539 /*          Sample casts functions                                */
540 /******************************************************************/
541 
c_ip2int(struct sample * smp)542 static int c_ip2int(struct sample *smp)
543 {
544 	smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
545 	smp->data.type = SMP_T_SINT;
546 	return 1;
547 }
548 
c_ip2str(struct sample * smp)549 static int c_ip2str(struct sample *smp)
550 {
551 	struct buffer *trash = get_trash_chunk();
552 
553 	if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
554 		return 0;
555 
556 	trash->data = strlen(trash->area);
557 	smp->data.u.str = *trash;
558 	smp->data.type = SMP_T_STR;
559 	smp->flags &= ~SMP_F_CONST;
560 
561 	return 1;
562 }
563 
c_ip2ipv6(struct sample * smp)564 static int c_ip2ipv6(struct sample *smp)
565 {
566 	v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
567 	smp->data.type = SMP_T_IPV6;
568 	return 1;
569 }
570 
c_ipv62ip(struct sample * smp)571 static int c_ipv62ip(struct sample *smp)
572 {
573 	if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
574 		return 0;
575 	smp->data.type = SMP_T_IPV4;
576 	return 1;
577 }
578 
c_ipv62str(struct sample * smp)579 static int c_ipv62str(struct sample *smp)
580 {
581 	struct buffer *trash = get_trash_chunk();
582 
583 	if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
584 		return 0;
585 
586 	trash->data = strlen(trash->area);
587 	smp->data.u.str = *trash;
588 	smp->data.type = SMP_T_STR;
589 	smp->flags &= ~SMP_F_CONST;
590 	return 1;
591 }
592 
593 /*
594 static int c_ipv62ip(struct sample *smp)
595 {
596 	return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
597 }
598 */
599 
c_int2ip(struct sample * smp)600 static int c_int2ip(struct sample *smp)
601 {
602 	smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
603 	smp->data.type = SMP_T_IPV4;
604 	return 1;
605 }
606 
c_int2ipv6(struct sample * smp)607 static int c_int2ipv6(struct sample *smp)
608 {
609 	smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
610 	v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
611 	smp->data.type = SMP_T_IPV6;
612 	return 1;
613 }
614 
c_str2addr(struct sample * smp)615 static int c_str2addr(struct sample *smp)
616 {
617 	if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
618 		if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
619 			return 0;
620 		smp->data.type = SMP_T_IPV6;
621 		smp->flags &= ~SMP_F_CONST;
622 		return 1;
623 	}
624 	smp->data.type = SMP_T_IPV4;
625 	smp->flags &= ~SMP_F_CONST;
626 	return 1;
627 }
628 
c_str2ip(struct sample * smp)629 static int c_str2ip(struct sample *smp)
630 {
631 	if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
632 		return 0;
633 	smp->data.type = SMP_T_IPV4;
634 	smp->flags &= ~SMP_F_CONST;
635 	return 1;
636 }
637 
c_str2ipv6(struct sample * smp)638 static int c_str2ipv6(struct sample *smp)
639 {
640 	if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
641 		return 0;
642 	smp->data.type = SMP_T_IPV6;
643 	smp->flags &= ~SMP_F_CONST;
644 	return 1;
645 }
646 
647 /*
648  * The NULL char always enforces the end of string if it is met.
649  * Data is never changed, so we can ignore the CONST case
650  */
c_bin2str(struct sample * smp)651 static int c_bin2str(struct sample *smp)
652 {
653 	int i;
654 
655 	for (i = 0; i < smp->data.u.str.data; i++) {
656 		if (!smp->data.u.str.area[i]) {
657 			smp->data.u.str.data = i;
658 			break;
659 		}
660 	}
661 	smp->data.type = SMP_T_STR;
662 	return 1;
663 }
664 
c_int2str(struct sample * smp)665 static int c_int2str(struct sample *smp)
666 {
667 	struct buffer *trash = get_trash_chunk();
668 	char *pos;
669 
670 	pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
671 	if (!pos)
672 		return 0;
673 
674 	trash->size = trash->size - (pos - trash->area);
675 	trash->area = pos;
676 	trash->data = strlen(pos);
677 	smp->data.u.str = *trash;
678 	smp->data.type = SMP_T_STR;
679 	smp->flags &= ~SMP_F_CONST;
680 	return 1;
681 }
682 
683 /* This function unconditionally duplicates data and removes the "const" flag.
684  * For strings and binary blocks, it also provides a known allocated size with
685  * a length that is capped to the size, and ensures a trailing zero is always
686  * appended for strings. This is necessary for some operations which may
687  * require to extend the length. It returns 0 if it fails, 1 on success.
688  */
smp_dup(struct sample * smp)689 int smp_dup(struct sample *smp)
690 {
691 	struct buffer *trash;
692 
693 	switch (smp->data.type) {
694 	case SMP_T_BOOL:
695 	case SMP_T_SINT:
696 	case SMP_T_ADDR:
697 	case SMP_T_IPV4:
698 	case SMP_T_IPV6:
699 		/* These type are not const. */
700 		break;
701 
702 	case SMP_T_METH:
703 		if (smp->data.u.meth.meth != HTTP_METH_OTHER)
704 			break;
705 		/* Fall through */
706 
707 	case SMP_T_STR:
708 		trash = get_trash_chunk();
709 		trash->data = smp->data.type == SMP_T_STR ?
710 		    smp->data.u.str.data : smp->data.u.meth.str.data;
711 		if (trash->data > trash->size - 1)
712 			trash->data = trash->size - 1;
713 
714 		memcpy(trash->area, smp->data.type == SMP_T_STR ?
715 		    smp->data.u.str.area : smp->data.u.meth.str.area,
716 		    trash->data);
717 		trash->area[trash->data] = 0;
718 		smp->data.u.str = *trash;
719 		break;
720 
721 	case SMP_T_BIN:
722 		trash = get_trash_chunk();
723 		trash->data = smp->data.u.str.data;
724 		if (trash->data > trash->size)
725 			trash->data = trash->size;
726 
727 		memcpy(trash->area, smp->data.u.str.area, trash->data);
728 		smp->data.u.str = *trash;
729 		break;
730 
731 	default:
732 		/* Other cases are unexpected. */
733 		return 0;
734 	}
735 
736 	/* remove const flag */
737 	smp->flags &= ~SMP_F_CONST;
738 	return 1;
739 }
740 
c_none(struct sample * smp)741 int c_none(struct sample *smp)
742 {
743 	return 1;
744 }
745 
c_str2int(struct sample * smp)746 static int c_str2int(struct sample *smp)
747 {
748 	const char *str;
749 	const char *end;
750 
751 	if (smp->data.u.str.data == 0)
752 		return 0;
753 
754 	str = smp->data.u.str.area;
755 	end = smp->data.u.str.area + smp->data.u.str.data;
756 
757 	smp->data.u.sint = read_int64(&str, end);
758 	smp->data.type = SMP_T_SINT;
759 	smp->flags &= ~SMP_F_CONST;
760 	return 1;
761 }
762 
c_str2meth(struct sample * smp)763 static int c_str2meth(struct sample *smp)
764 {
765 	enum http_meth_t meth;
766 	int len;
767 
768 	meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
769 	if (meth == HTTP_METH_OTHER) {
770 		len = smp->data.u.str.data;
771 		smp->data.u.meth.str.area = smp->data.u.str.area;
772 		smp->data.u.meth.str.data = len;
773 	}
774 	else
775 		smp->flags &= ~SMP_F_CONST;
776 	smp->data.u.meth.meth = meth;
777 	smp->data.type = SMP_T_METH;
778 	return 1;
779 }
780 
c_meth2str(struct sample * smp)781 static int c_meth2str(struct sample *smp)
782 {
783 	int len;
784 	enum http_meth_t meth;
785 
786 	if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
787 		/* The method is unknown. Copy the original pointer. */
788 		len = smp->data.u.meth.str.data;
789 		smp->data.u.str.area = smp->data.u.meth.str.area;
790 		smp->data.u.str.data = len;
791 		smp->data.type = SMP_T_STR;
792 	}
793 	else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
794 		/* The method is known, copy the pointer containing the string. */
795 		meth = smp->data.u.meth.meth;
796 		smp->data.u.str.area = http_known_methods[meth].ptr;
797 		smp->data.u.str.data = http_known_methods[meth].len;
798 		smp->flags |= SMP_F_CONST;
799 		smp->data.type = SMP_T_STR;
800 	}
801 	else {
802 		/* Unknown method */
803 		return 0;
804 	}
805 	return 1;
806 }
807 
c_addr2bin(struct sample * smp)808 static int c_addr2bin(struct sample *smp)
809 {
810 	struct buffer *chk = get_trash_chunk();
811 
812 	if (smp->data.type == SMP_T_IPV4) {
813 		chk->data = 4;
814 		memcpy(chk->area, &smp->data.u.ipv4, chk->data);
815 	}
816 	else if (smp->data.type == SMP_T_IPV6) {
817 		chk->data = 16;
818 		memcpy(chk->area, &smp->data.u.ipv6, chk->data);
819 	}
820 	else
821 		return 0;
822 
823 	smp->data.u.str = *chk;
824 	smp->data.type = SMP_T_BIN;
825 	return 1;
826 }
827 
c_int2bin(struct sample * smp)828 static int c_int2bin(struct sample *smp)
829 {
830 	struct buffer *chk = get_trash_chunk();
831 
832 	*(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
833 	chk->data = 8;
834 
835 	smp->data.u.str = *chk;
836 	smp->data.type = SMP_T_BIN;
837 	return 1;
838 }
839 
840 
841 /*****************************************************************/
842 /*      Sample casts matrix:                                     */
843 /*           sample_casts[from type][to type]                    */
844 /*           NULL pointer used for impossible sample casts       */
845 /*****************************************************************/
846 
847 sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
848 /*            to:  ANY     BOOL       SINT       ADDR        IPV4      IPV6        STR         BIN         METH */
849 /* from:  ANY */ { c_none, c_none,    c_none,    c_none,     c_none,   c_none,     c_none,     c_none,     c_none,     },
850 /*       BOOL */ { c_none, c_none,    c_none,    NULL,       NULL,     NULL,       c_int2str,  NULL,       NULL,       },
851 /*       SINT */ { c_none, c_none,    c_none,    c_int2ip,   c_int2ip, c_int2ipv6, c_int2str,  c_int2bin,  NULL,       },
852 /*       ADDR */ { c_none, NULL,      NULL,      NULL,       NULL,     NULL,       NULL,       NULL,       NULL,       },
853 /*       IPV4 */ { c_none, NULL,      c_ip2int,  c_none,     c_none,   c_ip2ipv6,  c_ip2str,   c_addr2bin, NULL,       },
854 /*       IPV6 */ { c_none, NULL,      NULL,      c_none,     c_ipv62ip,c_none,     c_ipv62str, c_addr2bin, NULL,       },
855 /*        STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none,     c_none,     c_str2meth, },
856 /*        BIN */ { c_none, NULL,      NULL,      NULL,       NULL,     NULL,       c_bin2str,  c_none,     c_str2meth, },
857 /*       METH */ { c_none, NULL,      NULL,      NULL,       NULL,     NULL,       c_meth2str, c_meth2str, c_none,     }
858 };
859 
860 /*
861  * Parse a sample expression configuration:
862  *        fetch keyword followed by format conversion keywords.
863  * Returns a pointer on allocated sample expression structure.
864  * <al> is an arg_list serving as a list head to report missing dependencies.
865  * It may be NULL if such dependencies are not allowed. Otherwise, the caller
866  * must have set al->ctx if al is set.
867  * If <endptr> is non-nul, it will be set to the first unparsed character
868  * (which may be the final '\0') on success. If it is nul, the expression
869  * must be properly terminated by a '\0' otherwise an error is reported.
870  */
sample_parse_expr(char ** str,int * idx,const char * file,int line,char ** err_msg,struct arg_list * al,char ** endptr)871 struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err_msg, struct arg_list *al, char **endptr)
872 {
873 	const char *begw; /* beginning of word */
874 	const char *endw; /* end of word */
875 	const char *endt; /* end of term */
876 	struct sample_expr *expr = NULL;
877 	struct sample_fetch *fetch;
878 	struct sample_conv *conv;
879 	unsigned long prev_type;
880 	char *fkw = NULL;
881 	char *ckw = NULL;
882 	int err_arg;
883 
884 	begw = str[*idx];
885 	for (endw = begw; is_idchar(*endw); endw++)
886 		;
887 
888 	if (endw == begw) {
889 		memprintf(err_msg, "missing fetch method");
890 		goto out_error;
891 	}
892 
893 	/* keep a copy of the current fetch keyword for error reporting */
894 	fkw = my_strndup(begw, endw - begw);
895 
896 	fetch = find_sample_fetch(begw, endw - begw);
897 	if (!fetch) {
898 		memprintf(err_msg, "unknown fetch method '%s'", fkw);
899 		goto out_error;
900 	}
901 
902 	/* At this point, we have :
903 	 *   - begw : beginning of the keyword
904 	 *   - endw : end of the keyword, first character not part of keyword
905 	 */
906 
907 	if (fetch->out_type >= SMP_TYPES) {
908 		memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
909 		goto out_error;
910 	}
911 	prev_type = fetch->out_type;
912 
913 	expr = calloc(1, sizeof(*expr));
914 	if (!expr)
915 		goto out_error;
916 
917 	LIST_INIT(&(expr->conv_exprs));
918 	expr->fetch = fetch;
919 	expr->arg_p = empty_arg_list;
920 
921 	/* Note that we call the argument parser even with an empty string,
922 	 * this allows it to automatically create entries for mandatory
923 	 * implicit arguments (eg: local proxy name).
924 	 */
925 	if (al) {
926 		al->kw = expr->fetch->kw;
927 		al->conv = NULL;
928 	}
929 	if (make_arg_list(endw, -1, fetch->arg_mask, &expr->arg_p, err_msg, &endt, &err_arg, al) < 0) {
930 		memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
931 		goto out_error;
932 	}
933 
934 	/* now endt is our first char not part of the arg list, typically the
935 	 * comma after the sample fetch name or after the closing parenthesis,
936 	 * or the NUL char.
937 	 */
938 
939 	if (!expr->arg_p) {
940 		expr->arg_p = empty_arg_list;
941 	}
942 	else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
943 		memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
944 		goto out_error;
945 	}
946 
947 	/* Now process the converters if any. We have two supported syntaxes
948 	 * for the converters, which can be combined :
949 	 *  - comma-delimited list of converters just after the keyword and args ;
950 	 *  - one converter per keyword
951 	 * The combination allows to have each keyword being a comma-delimited
952 	 * series of converters.
953 	 *
954 	 * We want to process the former first, then the latter. For this we start
955 	 * from the beginning of the supposed place in the exiting conv chain, which
956 	 * starts at the last comma (endt).
957 	 */
958 
959 	while (1) {
960 		struct sample_conv_expr *conv_expr;
961 		int err_arg;
962 		int argcnt;
963 
964 		if (*endt && *endt != ',') {
965 			if (endptr) {
966 				/* end found, let's stop here */
967 				break;
968 			}
969 			if (ckw)
970 				memprintf(err_msg, "missing comma after converter '%s'", ckw);
971 			else
972 				memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
973 			goto out_error;
974 		}
975 
976 		/* FIXME: how long should we support such idiocies ? Maybe we
977 		 * should already warn ?
978 		 */
979 		while (*endt == ',') /* then trailing commas */
980 			endt++;
981 
982 		begw = endt; /* start of converter */
983 
984 		if (!*begw) {
985 			/* none ? skip to next string */
986 			(*idx)++;
987 			begw = str[*idx];
988 			if (!begw || !*begw)
989 				break;
990 		}
991 
992 		for (endw = begw; is_idchar(*endw); endw++)
993 			;
994 
995 		free(ckw);
996 		ckw = my_strndup(begw, endw - begw);
997 
998 		conv = find_sample_conv(begw, endw - begw);
999 		if (!conv) {
1000 			/* we found an isolated keyword that we don't know, it's not ours */
1001 			if (begw == str[*idx]) {
1002 				endt = begw;
1003 				break;
1004 			}
1005 			memprintf(err_msg, "unknown converter '%s'", ckw);
1006 			goto out_error;
1007 		}
1008 
1009 		if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
1010 			memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
1011 			goto out_error;
1012 		}
1013 
1014 		/* If impossible type conversion */
1015 		if (!sample_casts[prev_type][conv->in_type]) {
1016 			memprintf(err_msg, "converter '%s' cannot be applied", ckw);
1017 			goto out_error;
1018 		}
1019 
1020 		prev_type = conv->out_type;
1021 		conv_expr = calloc(1, sizeof(*conv_expr));
1022 		if (!conv_expr)
1023 			goto out_error;
1024 
1025 		LIST_APPEND(&(expr->conv_exprs), &(conv_expr->list));
1026 		conv_expr->conv = conv;
1027 
1028 		if (al) {
1029 			al->kw = expr->fetch->kw;
1030 			al->conv = conv_expr->conv->kw;
1031 		}
1032 		argcnt = make_arg_list(endw, -1, conv->arg_mask, &conv_expr->arg_p, err_msg, &endt, &err_arg, al);
1033 		if (argcnt < 0) {
1034 			memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
1035 			goto out_error;
1036 		}
1037 
1038 		if (argcnt && !conv->arg_mask) {
1039 			memprintf(err_msg, "converter '%s' does not support any args", ckw);
1040 			goto out_error;
1041 		}
1042 
1043 		if (!conv_expr->arg_p)
1044 			conv_expr->arg_p = empty_arg_list;
1045 
1046 		if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1047 			memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
1048 			goto out_error;
1049 		}
1050 	}
1051 
1052 	if (endptr) {
1053 		/* end found, let's stop here */
1054 		*endptr = (char *)endt;
1055 	}
1056 
1057  out:
1058 	free(fkw);
1059 	free(ckw);
1060 	return expr;
1061 
1062 out_error:
1063 	release_sample_expr(expr);
1064 	expr = NULL;
1065 	goto out;
1066 }
1067 
1068 /*
1069  * Process a fetch + format conversion of defined by the sample expression <expr>
1070  * on request or response considering the <opt> parameter.
1071  * Returns a pointer on a typed sample structure containing the result or NULL if
1072  * sample is not found or when format conversion failed.
1073  *  If <p> is not null, function returns results in structure pointed by <p>.
1074  *  If <p> is null, functions returns a pointer on a static sample structure.
1075  *
1076  * Note: the fetch functions are required to properly set the return type. The
1077  * conversion functions must do so too. However the cast functions do not need
1078  * to since they're made to cast multiple types according to what is required.
1079  *
1080  * The caller may indicate in <opt> if it considers the result final or not.
1081  * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1082  * if the result is stable or not, according to the following table :
1083  *
1084  * return MAY_CHANGE FINAL   Meaning for the sample
1085  *  NULL      0        *     Not present and will never be (eg: header)
1086  *  NULL      1        0     Not present yet, could change (eg: POST param)
1087  *  NULL      1        1     Not present yet, will not change anymore
1088  *   smp      0        *     Present and will not change (eg: header)
1089  *   smp      1        0     Present, may change (eg: request length)
1090  *   smp      1        1     Present, last known value (eg: request length)
1091  */
sample_process(struct proxy * px,struct session * sess,struct stream * strm,unsigned int opt,struct sample_expr * expr,struct sample * p)1092 struct sample *sample_process(struct proxy *px, struct session *sess,
1093                               struct stream *strm, unsigned int opt,
1094                               struct sample_expr *expr, struct sample *p)
1095 {
1096 	struct sample_conv_expr *conv_expr;
1097 
1098 	if (p == NULL) {
1099 		p = &temp_smp;
1100 		memset(p, 0, sizeof(*p));
1101 	}
1102 
1103 	smp_set_owner(p, px, sess, strm, opt);
1104 	if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
1105 		return NULL;
1106 
1107 	list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
1108 		/* we want to ensure that p->type can be casted into
1109 		 * conv_expr->conv->in_type. We have 3 possibilities :
1110 		 *  - NULL   => not castable.
1111 		 *  - c_none => nothing to do (let's optimize it)
1112 		 *  - other  => apply cast and prepare to fail
1113 		 */
1114 		if (!sample_casts[p->data.type][conv_expr->conv->in_type])
1115 			return NULL;
1116 
1117 		if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1118 		    !sample_casts[p->data.type][conv_expr->conv->in_type](p))
1119 			return NULL;
1120 
1121 		/* OK cast succeeded */
1122 
1123 		if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
1124 			return NULL;
1125 	}
1126 	return p;
1127 }
1128 
1129 /*
1130  * Resolve all remaining arguments in proxy <p>. Returns the number of
1131  * errors or 0 if everything is fine. If at least one error is met, it will
1132  * be appended to *err. If *err==NULL it will be allocated first.
1133  */
smp_resolve_args(struct proxy * p,char ** err)1134 int smp_resolve_args(struct proxy *p, char **err)
1135 {
1136 	struct arg_list *cur, *bak;
1137 	const char *ctx, *where;
1138 	const char *conv_ctx, *conv_pre, *conv_pos;
1139 	struct userlist *ul;
1140 	struct my_regex *reg;
1141 	struct arg *arg;
1142 	int cfgerr = 0;
1143 	int rflags;
1144 
1145 	list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1146 		struct proxy *px;
1147 		struct server *srv;
1148 		struct stktable *t;
1149 		char *pname, *sname, *stktname;
1150 		char *err2;
1151 
1152 		arg = cur->arg;
1153 
1154 		/* prepare output messages */
1155 		conv_pre = conv_pos = conv_ctx = "";
1156 		if (cur->conv) {
1157 			conv_ctx = cur->conv;
1158 			conv_pre = "conversion keyword '";
1159 			conv_pos = "' for ";
1160 		}
1161 
1162 		where = "in";
1163 		ctx = "sample fetch keyword";
1164 		switch (cur->ctx) {
1165 		case ARGC_STK:   where = "in stick rule in"; break;
1166 		case ARGC_TRK:   where = "in tracking rule in"; break;
1167 		case ARGC_LOG:   where = "in log-format string in"; break;
1168 		case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1169 		case ARGC_HRQ:   where = "in http-request expression in"; break;
1170 		case ARGC_HRS:   where = "in http-response response in"; break;
1171 		case ARGC_UIF:   where = "in unique-id-format string in"; break;
1172 		case ARGC_RDR:   where = "in redirect format string in"; break;
1173 		case ARGC_CAP:   where = "in capture rule in"; break;
1174 		case ARGC_ACL:   ctx = "ACL keyword"; break;
1175 		case ARGC_SRV:   where = "in server directive in"; break;
1176 		case ARGC_SPOE:  where = "in spoe-message directive in"; break;
1177 		case ARGC_HERR:  where = "in http-error directive in"; break;
1178 		case ARGC_OT:    where = "in ot-scope directive in"; break;
1179 		case ARGC_TCO:   where = "in tcp-request connection expression in"; break;
1180 		case ARGC_TSE:   where = "in tcp-request session expression in"; break;
1181 		case ARGC_TRQ:   where = "in tcp-request content expression in"; break;
1182 		case ARGC_TRS:   where = "in tcp-response content expression in"; break;
1183 		case ARGC_TCK:   where = "in tcp-check expression in"; break;
1184 		case ARGC_CFG:   where = "in configuration expression in"; break;
1185 		case ARGC_CLI:   where = "in CLI expression in"; break;
1186 		}
1187 
1188 		/* set a few default settings */
1189 		px = p;
1190 		pname = p->id;
1191 
1192 		switch (arg->type) {
1193 		case ARGT_SRV:
1194 			if (!arg->data.str.data) {
1195 				memprintf(err, "%sparsing [%s:%d]: missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1196 					 *err ? *err : "", cur->file, cur->line,
1197 					 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1198 				cfgerr++;
1199 				continue;
1200 			}
1201 
1202 			/* we support two formats : "bck/srv" and "srv" */
1203 			sname = strrchr(arg->data.str.area, '/');
1204 
1205 			if (sname) {
1206 				*sname++ = '\0';
1207 				pname = arg->data.str.area;
1208 
1209 				px = proxy_be_by_name(pname);
1210 				if (!px) {
1211 					memprintf(err, "%sparsing [%s:%d]: unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1212 						 *err ? *err : "", cur->file, cur->line, pname,
1213 						 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1214 					cfgerr++;
1215 					break;
1216 				}
1217 			}
1218 			else
1219 				sname = arg->data.str.area;
1220 
1221 			srv = findserver(px, sname);
1222 			if (!srv) {
1223 				memprintf(err, "%sparsing [%s:%d]: unable to find server '%s' in proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1224 					 *err ? *err : "", cur->file, cur->line, sname, pname,
1225 					 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1226 				cfgerr++;
1227 				break;
1228 			}
1229 
1230 			chunk_destroy(&arg->data.str);
1231 			arg->unresolved = 0;
1232 			arg->data.srv = srv;
1233 			break;
1234 
1235 		case ARGT_FE:
1236 			if (arg->data.str.data) {
1237 				pname = arg->data.str.area;
1238 				px = proxy_fe_by_name(pname);
1239 			}
1240 
1241 			if (!px) {
1242 				memprintf(err, "%sparsing [%s:%d]: unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1243 					 *err ? *err : "", cur->file, cur->line, pname,
1244 					 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1245 				cfgerr++;
1246 				break;
1247 			}
1248 
1249 			if (!(px->cap & PR_CAP_FE)) {
1250 				memprintf(err, "%sparsing [%s:%d]: proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1251 					 *err ? *err : "", cur->file, cur->line, pname,
1252 					 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1253 				cfgerr++;
1254 				break;
1255 			}
1256 
1257 			chunk_destroy(&arg->data.str);
1258 			arg->unresolved = 0;
1259 			arg->data.prx = px;
1260 			break;
1261 
1262 		case ARGT_BE:
1263 			if (arg->data.str.data) {
1264 				pname = arg->data.str.area;
1265 				px = proxy_be_by_name(pname);
1266 			}
1267 
1268 			if (!px) {
1269 				memprintf(err, "%sparsing [%s:%d]: unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1270 					 *err ? *err : "", cur->file, cur->line, pname,
1271 					 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1272 				cfgerr++;
1273 				break;
1274 			}
1275 
1276 			if (!(px->cap & PR_CAP_BE)) {
1277 				memprintf(err, "%sparsing [%s:%d]: proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1278 					 *err ? *err : "", cur->file, cur->line, pname,
1279 					 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1280 				cfgerr++;
1281 				break;
1282 			}
1283 
1284 			chunk_destroy(&arg->data.str);
1285 			arg->unresolved = 0;
1286 			arg->data.prx = px;
1287 			break;
1288 
1289 		case ARGT_TAB:
1290 			if (arg->data.str.data)
1291 				stktname = arg->data.str.area;
1292 			else
1293 				stktname = px->id;
1294 
1295 			t = stktable_find_by_name(stktname);
1296 			if (!t) {
1297 				memprintf(err, "%sparsing [%s:%d]: unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1298 					 *err ? *err : "", cur->file, cur->line, stktname,
1299 					 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1300 				cfgerr++;
1301 				break;
1302 			}
1303 
1304 			if (!t->size) {
1305 				memprintf(err, "%sparsing [%s:%d]: no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1306 					 *err ? *err : "", cur->file, cur->line, stktname,
1307 					 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1308 				cfgerr++;
1309 				break;
1310 			}
1311 
1312 			if (t->proxy && (p->bind_proc & ~t->proxy->bind_proc)) {
1313 				memprintf(err, "%sparsing [%s:%d]: stick-table '%s' not present on all processes covered by proxy '%s'.\n",
1314 					 *err ? *err : "", cur->file, cur->line, t->proxy->id, p->id);
1315 				cfgerr++;
1316 				break;
1317 			}
1318 
1319 			if (!in_proxies_list(t->proxies_list, p)) {
1320 				p->next_stkt_ref = t->proxies_list;
1321 				t->proxies_list = p;
1322 			}
1323 
1324 			chunk_destroy(&arg->data.str);
1325 			arg->unresolved = 0;
1326 			arg->data.t = t;
1327 			break;
1328 
1329 		case ARGT_USR:
1330 			if (!arg->data.str.data) {
1331 				memprintf(err, "%sparsing [%s:%d]: missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1332 					 *err ? *err : "", cur->file, cur->line,
1333 					 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1334 				cfgerr++;
1335 				break;
1336 			}
1337 
1338 			if (p->uri_auth && p->uri_auth->userlist &&
1339 			    strcmp(p->uri_auth->userlist->name, arg->data.str.area) == 0)
1340 				ul = p->uri_auth->userlist;
1341 			else
1342 				ul = auth_find_userlist(arg->data.str.area);
1343 
1344 			if (!ul) {
1345 				memprintf(err, "%sparsing [%s:%d]: unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1346 					 *err ? *err : "", cur->file, cur->line,
1347 					 arg->data.str.area,
1348 					 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1349 				cfgerr++;
1350 				break;
1351 			}
1352 
1353 			chunk_destroy(&arg->data.str);
1354 			arg->unresolved = 0;
1355 			arg->data.usr = ul;
1356 			break;
1357 
1358 		case ARGT_REG:
1359 			if (!arg->data.str.data) {
1360 				memprintf(err, "%sparsing [%s:%d]: missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1361 					 *err ? *err : "", cur->file, cur->line,
1362 					 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1363 				cfgerr++;
1364 				continue;
1365 			}
1366 
1367 			rflags = 0;
1368 			rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1369 			err2 = NULL;
1370 
1371 			if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err2))) {
1372 				memprintf(err, "%sparsing [%s:%d]: error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1373 					  *err ? *err : "", cur->file, cur->line,
1374 					 arg->data.str.area,
1375 					 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err2);
1376 				cfgerr++;
1377 				continue;
1378 			}
1379 
1380 			chunk_destroy(&arg->data.str);
1381 			arg->unresolved = 0;
1382 			arg->data.reg = reg;
1383 			break;
1384 
1385 
1386 		}
1387 
1388 		LIST_DELETE(&cur->list);
1389 		free(cur);
1390 	} /* end of args processing */
1391 
1392 	return cfgerr;
1393 }
1394 
1395 /*
1396  * Process a fetch + format conversion as defined by the sample expression
1397  * <expr> on request or response considering the <opt> parameter. The output is
1398  * not explicitly set to <smp_type>, but shall be compatible with it as
1399  * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1400  * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
1401  * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1402  * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1403  * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1404  * take actions (eg: wait longer). If a sample could not be found or could not
1405  * be converted, NULL is returned. The caller MUST NOT use the sample if the
1406  * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1407  * still hope to get it after waiting longer, and is not converted to string.
1408  * The possible output combinations are the following :
1409  *
1410  * return MAY_CHANGE FINAL   Meaning for the sample
1411  *  NULL      *        *     Not present and will never be (eg: header)
1412  *   smp      0        *     Final value converted (eg: header)
1413  *   smp      1        0     Not present yet, may appear later (eg: header)
1414  *   smp      1        1     never happens (either flag is cleared on output)
1415  */
sample_fetch_as_type(struct proxy * px,struct session * sess,struct stream * strm,unsigned int opt,struct sample_expr * expr,int smp_type)1416 struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
1417                                    struct stream *strm, unsigned int opt,
1418                                    struct sample_expr *expr, int smp_type)
1419 {
1420 	struct sample *smp = &temp_smp;
1421 
1422 	memset(smp, 0, sizeof(*smp));
1423 
1424 	if (!sample_process(px, sess, strm, opt, expr, smp)) {
1425 		if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1426 			return smp;
1427 		return NULL;
1428 	}
1429 
1430 	if (!sample_casts[smp->data.type][smp_type])
1431 		return NULL;
1432 
1433 	if (!sample_casts[smp->data.type][smp_type](smp))
1434 		return NULL;
1435 
1436 	smp->flags &= ~SMP_F_MAY_CHANGE;
1437 	return smp;
1438 }
1439 
release_sample_arg(struct arg * p)1440 static void release_sample_arg(struct arg *p)
1441 {
1442 	struct arg *p_back = p;
1443 
1444 	if (!p)
1445 		return;
1446 
1447 	while (p->type != ARGT_STOP) {
1448 		if (p->type == ARGT_STR || p->unresolved) {
1449 			chunk_destroy(&p->data.str);
1450 			p->unresolved = 0;
1451 		}
1452 		else if (p->type == ARGT_REG) {
1453 			regex_free(p->data.reg);
1454 			p->data.reg = NULL;
1455 		}
1456 		p++;
1457 	}
1458 
1459 	if (p_back != empty_arg_list)
1460 		free(p_back);
1461 }
1462 
release_sample_expr(struct sample_expr * expr)1463 void release_sample_expr(struct sample_expr *expr)
1464 {
1465 	struct sample_conv_expr *conv_expr, *conv_exprb;
1466 
1467 	if (!expr)
1468 		return;
1469 
1470 	list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list) {
1471 		LIST_DELETE(&conv_expr->list);
1472 		release_sample_arg(conv_expr->arg_p);
1473 		free(conv_expr);
1474 	}
1475 
1476 	release_sample_arg(expr->arg_p);
1477 	free(expr);
1478 }
1479 
1480 /*****************************************************************/
1481 /*    Sample format convert functions                            */
1482 /*    These functions set the data type on return.               */
1483 /*****************************************************************/
1484 
sample_conv_debug(const struct arg * arg_p,struct sample * smp,void * private)1485 static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1486 {
1487 	int i;
1488 	struct sample tmp;
1489 	struct buffer *buf;
1490 	struct sink *sink;
1491 	struct ist line;
1492 	char *pfx;
1493 
1494 	buf = alloc_trash_chunk();
1495 	if (!buf)
1496 		goto end;
1497 
1498 	sink = (struct sink *)arg_p[1].data.ptr;
1499 	BUG_ON(!sink);
1500 
1501 	pfx = arg_p[0].data.str.area;
1502 	BUG_ON(!pfx);
1503 
1504 	chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1505 	if (!sample_casts[smp->data.type][SMP_T_STR])
1506 		goto nocast;
1507 
1508 	/* Copy sample fetch. This puts the sample as const, the
1509 	 * cast will copy data if a transformation is required.
1510 	 */
1511 	memcpy(&tmp, smp, sizeof(struct sample));
1512 	tmp.flags = SMP_F_CONST;
1513 
1514 	if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1515 		goto nocast;
1516 
1517 	/* Display the displayable chars*. */
1518 	b_putchr(buf, '<');
1519 	for (i = 0; i < tmp.data.u.str.data; i++) {
1520 		if (isprint((unsigned char)tmp.data.u.str.area[i]))
1521 			b_putchr(buf, tmp.data.u.str.area[i]);
1522 		else
1523 			b_putchr(buf, '.');
1524 	}
1525 	b_putchr(buf, '>');
1526 
1527  done:
1528 	line = ist2(buf->area, buf->data);
1529 	sink_write(sink, &line, 1, 0, 0, NULL);
1530  end:
1531 	free_trash_chunk(buf);
1532 	return 1;
1533  nocast:
1534 	chunk_appendf(buf, "(undisplayable)");
1535 	goto done;
1536 }
1537 
1538 // This function checks the "debug" converter's arguments.
smp_check_debug(struct arg * args,struct sample_conv * conv,const char * file,int line,char ** err)1539 static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1540                            const char *file, int line, char **err)
1541 {
1542 	const char *name = "buf0";
1543 	struct sink *sink = NULL;
1544 
1545 	if (args[0].type != ARGT_STR) {
1546 		/* optional prefix */
1547 		args[0].data.str.area = "";
1548 		args[0].data.str.data = 0;
1549 	}
1550 
1551 	if (args[1].type == ARGT_STR)
1552 		name = args[1].data.str.area;
1553 
1554 	sink = sink_find(name);
1555 	if (!sink) {
1556 		memprintf(err, "No such sink '%s'", name);
1557 		return 0;
1558 	}
1559 
1560 	chunk_destroy(&args[1].data.str);
1561 	args[1].type = ARGT_PTR;
1562 	args[1].data.ptr = sink;
1563 	return 1;
1564 }
1565 
sample_conv_base642bin(const struct arg * arg_p,struct sample * smp,void * private)1566 static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1567 {
1568 	struct buffer *trash = get_trash_chunk();
1569 	int bin_len;
1570 
1571 	trash->data = 0;
1572 	bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1573 			    trash->area, trash->size);
1574 	if (bin_len < 0)
1575 		return 0;
1576 
1577 	trash->data = bin_len;
1578 	smp->data.u.str = *trash;
1579 	smp->data.type = SMP_T_BIN;
1580 	smp->flags &= ~SMP_F_CONST;
1581 	return 1;
1582 }
1583 
sample_conv_base64url2bin(const struct arg * arg_p,struct sample * smp,void * private)1584 static int sample_conv_base64url2bin(const struct arg *arg_p, struct sample *smp, void *private)
1585 {
1586 	struct buffer *trash = get_trash_chunk();
1587 	int bin_len;
1588 
1589 	trash->data = 0;
1590 	bin_len = base64urldec(smp->data.u.str.area, smp->data.u.str.data,
1591 			    trash->area, trash->size);
1592 	if (bin_len < 0)
1593 		return 0;
1594 
1595 	trash->data = bin_len;
1596 	smp->data.u.str = *trash;
1597 	smp->data.type = SMP_T_BIN;
1598 	smp->flags &= ~SMP_F_CONST;
1599 	return 1;
1600 }
1601 
sample_conv_bin2base64(const struct arg * arg_p,struct sample * smp,void * private)1602 static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
1603 {
1604 	struct buffer *trash = get_trash_chunk();
1605 	int b64_len;
1606 
1607 	trash->data = 0;
1608 	b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1609 			   trash->area, trash->size);
1610 	if (b64_len < 0)
1611 		return 0;
1612 
1613 	trash->data = b64_len;
1614 	smp->data.u.str = *trash;
1615 	smp->data.type = SMP_T_STR;
1616 	smp->flags &= ~SMP_F_CONST;
1617 	return 1;
1618 }
1619 
sample_conv_bin2base64url(const struct arg * arg_p,struct sample * smp,void * private)1620 static int sample_conv_bin2base64url(const struct arg *arg_p, struct sample *smp, void *private)
1621 {
1622 	struct buffer *trash = get_trash_chunk();
1623 	int b64_len;
1624 
1625 	trash->data = 0;
1626 	b64_len = a2base64url(smp->data.u.str.area, smp->data.u.str.data,
1627 			   trash->area, trash->size);
1628 	if (b64_len < 0)
1629 		return 0;
1630 
1631 	trash->data = b64_len;
1632 	smp->data.u.str = *trash;
1633 	smp->data.type = SMP_T_STR;
1634 	smp->flags &= ~SMP_F_CONST;
1635 	return 1;
1636 }
1637 
1638 /* This function returns a sample struct filled with the conversion of variable
1639  * <var> to sample type <type> (SMP_T_*), via a cast to the target type. If the
1640  * variable cannot be retrieved or casted, 0 is returned, otherwise 1.
1641  *
1642  * Keep in mind that the sample content may be written to a pre-allocated
1643  * trash chunk as returned by get_trash_chunk().
1644  */
sample_conv_var2smp(const struct var_desc * var,struct sample * smp,int type)1645 int sample_conv_var2smp(const struct var_desc *var, struct sample *smp, int type)
1646 {
1647 	if (!vars_get_by_desc(var, smp))
1648 		return 0;
1649 	if (!sample_casts[smp->data.type][type])
1650 		return 0;
1651 	if (!sample_casts[smp->data.type][type](smp))
1652 		return 0;
1653 	return 1;
1654 }
1655 
sample_conv_sha1(const struct arg * arg_p,struct sample * smp,void * private)1656 static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1657 {
1658 	blk_SHA_CTX ctx;
1659 	struct buffer *trash = get_trash_chunk();
1660 
1661 	memset(&ctx, 0, sizeof(ctx));
1662 
1663 	blk_SHA1_Init(&ctx);
1664 	blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1665 	blk_SHA1_Final((unsigned char *) trash->area, &ctx);
1666 
1667 	trash->data = 20;
1668 	smp->data.u.str = *trash;
1669 	smp->data.type = SMP_T_BIN;
1670 	smp->flags &= ~SMP_F_CONST;
1671 	return 1;
1672 }
1673 
1674 #ifdef USE_OPENSSL
smp_check_sha2(struct arg * args,struct sample_conv * conv,const char * file,int line,char ** err)1675 static int smp_check_sha2(struct arg *args, struct sample_conv *conv,
1676                           const char *file, int line, char **err)
1677 {
1678 	if (args[0].type == ARGT_STOP)
1679 		return 1;
1680 	if (args[0].type != ARGT_SINT) {
1681 		memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]);
1682 		return 0;
1683 	}
1684 
1685 	switch (args[0].data.sint) {
1686 		case 224:
1687 		case 256:
1688 		case 384:
1689 		case 512:
1690 			/* this is okay */
1691 			return 1;
1692 		default:
1693 			memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint);
1694 			return 0;
1695 	}
1696 }
1697 
sample_conv_sha2(const struct arg * arg_p,struct sample * smp,void * private)1698 static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
1699 {
1700 	struct buffer *trash = get_trash_chunk();
1701 	int bits = 256;
1702 	if (arg_p->data.sint)
1703 		bits = arg_p->data.sint;
1704 
1705 	switch (bits) {
1706 	case 224: {
1707 		SHA256_CTX ctx;
1708 
1709 		memset(&ctx, 0, sizeof(ctx));
1710 
1711 		SHA224_Init(&ctx);
1712 		SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1713 		SHA224_Final((unsigned char *) trash->area, &ctx);
1714 		trash->data = SHA224_DIGEST_LENGTH;
1715 		break;
1716 	}
1717 	case 256: {
1718 		SHA256_CTX ctx;
1719 
1720 		memset(&ctx, 0, sizeof(ctx));
1721 
1722 		SHA256_Init(&ctx);
1723 		SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1724 		SHA256_Final((unsigned char *) trash->area, &ctx);
1725 		trash->data = SHA256_DIGEST_LENGTH;
1726 		break;
1727 	}
1728 	case 384: {
1729 		SHA512_CTX ctx;
1730 
1731 		memset(&ctx, 0, sizeof(ctx));
1732 
1733 		SHA384_Init(&ctx);
1734 		SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1735 		SHA384_Final((unsigned char *) trash->area, &ctx);
1736 		trash->data = SHA384_DIGEST_LENGTH;
1737 		break;
1738 	}
1739 	case 512: {
1740 		SHA512_CTX ctx;
1741 
1742 		memset(&ctx, 0, sizeof(ctx));
1743 
1744 		SHA512_Init(&ctx);
1745 		SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1746 		SHA512_Final((unsigned char *) trash->area, &ctx);
1747 		trash->data = SHA512_DIGEST_LENGTH;
1748 		break;
1749 	}
1750 	default:
1751 		return 0;
1752 	}
1753 
1754 	smp->data.u.str = *trash;
1755 	smp->data.type = SMP_T_BIN;
1756 	smp->flags &= ~SMP_F_CONST;
1757 	return 1;
1758 }
1759 
1760 /* This function returns a sample struct filled with an <arg> content.
1761  * If the <arg> contains a string, it is returned in the sample flagged as
1762  * SMP_F_CONST. If the <arg> contains a variable descriptor, the sample is
1763  * filled with the content of the variable by using vars_get_by_desc().
1764  *
1765  * Keep in mind that the sample content may be written to a pre-allocated
1766  * trash chunk as returned by get_trash_chunk().
1767  *
1768  * This function returns 0 if an error occurs, otherwise it returns 1.
1769  */
sample_conv_var2smp_str(const struct arg * arg,struct sample * smp)1770 int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
1771 {
1772 	switch (arg->type) {
1773 	case ARGT_STR:
1774 		smp->data.type = SMP_T_STR;
1775 		smp->data.u.str = arg->data.str;
1776 		smp->flags = SMP_F_CONST;
1777 		return 1;
1778 	case ARGT_VAR:
1779 		return sample_conv_var2smp(&arg->data.var, smp, SMP_T_STR);
1780 	default:
1781 		return 0;
1782 	}
1783 }
1784 
1785 /* This function checks an <arg> and fills it with a variable type if the
1786  * <arg> string contains a valid variable name. If failed, the function
1787  * tries to perform a base64 decode operation on the same string, and
1788  * fills the <arg> with the decoded content.
1789  *
1790  * Validation is skipped if the <arg> string is empty.
1791  *
1792  * This function returns 0 if the variable lookup fails and the specified
1793  * <arg> string is not a valid base64 encoded string, as well if
1794  * unexpected argument type is specified or memory allocation error
1795  * occurs. Otherwise it returns 1.
1796  */
sample_check_arg_base64(struct arg * arg,char ** err)1797 static inline int sample_check_arg_base64(struct arg *arg, char **err)
1798 {
1799 	char *dec = NULL;
1800 	int dec_size;
1801 
1802 	if (arg->type != ARGT_STR) {
1803 		memprintf(err, "unexpected argument type");
1804 		return 0;
1805 	}
1806 
1807 	if (arg->data.str.data == 0) /* empty */
1808 		return 1;
1809 
1810 	if (vars_check_arg(arg, NULL))
1811 		return 1;
1812 
1813 	if (arg->data.str.data % 4) {
1814 		memprintf(err, "argument needs to be base64 encoded, and "
1815 		               "can either be a string or a variable");
1816 		return 0;
1817 	}
1818 
1819 	dec_size = (arg->data.str.data / 4 * 3)
1820 	           - (arg->data.str.area[arg->data.str.data-1] == '=' ? 1 : 0)
1821 	           - (arg->data.str.area[arg->data.str.data-2] == '=' ? 1 : 0);
1822 
1823 	if ((dec = malloc(dec_size)) == NULL) {
1824 		memprintf(err, "memory allocation error");
1825 		return 0;
1826 	}
1827 
1828 	dec_size = base64dec(arg->data.str.area, arg->data.str.data, dec, dec_size);
1829 	if (dec_size < 0) {
1830 		memprintf(err, "argument needs to be base64 encoded, and "
1831 		               "can either be a string or a variable");
1832 		free(dec);
1833 		return 0;
1834 	}
1835 
1836 	/* base64 decoded */
1837 	chunk_destroy(&arg->data.str);
1838 	arg->data.str.area = dec;
1839 	arg->data.str.data = dec_size;
1840 	return 1;
1841 }
1842 
1843 #ifdef EVP_CIPH_GCM_MODE
check_aes_gcm(struct arg * args,struct sample_conv * conv,const char * file,int line,char ** err)1844 static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
1845 						  const char *file, int line, char **err)
1846 {
1847 	switch(args[0].data.sint) {
1848 	case 128:
1849 	case 192:
1850 	case 256:
1851 		break;
1852 	default:
1853 		memprintf(err, "key size must be 128, 192 or 256 (bits).");
1854 		return 0;
1855 	}
1856 
1857 	/* Try to decode variables. */
1858 	if (!sample_check_arg_base64(&args[1], err)) {
1859 		memprintf(err, "failed to parse nonce : %s", *err);
1860 		return 0;
1861 	}
1862 	if (!sample_check_arg_base64(&args[2], err)) {
1863 		memprintf(err, "failed to parse key : %s", *err);
1864 		return 0;
1865 	}
1866 	if (!sample_check_arg_base64(&args[3], err)) {
1867 		memprintf(err, "failed to parse aead_tag : %s", *err);
1868 		return 0;
1869 	}
1870 
1871 	return 1;
1872 }
1873 
1874 /* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
sample_conv_aes_gcm_dec(const struct arg * arg_p,struct sample * smp,void * private)1875 static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
1876 {
1877 	struct sample nonce, key, aead_tag;
1878 	struct buffer *smp_trash = NULL, *smp_trash_alloc = NULL;
1879 	EVP_CIPHER_CTX *ctx;
1880 	int dec_size, ret;
1881 
1882 	smp_trash_alloc = alloc_trash_chunk();
1883 	if (!smp_trash_alloc)
1884 		return 0;
1885 
1886 	/* smp copy */
1887 	smp_trash_alloc->data = smp->data.u.str.data;
1888 	if (unlikely(smp_trash_alloc->data > smp_trash_alloc->size))
1889 		smp_trash_alloc->data = smp_trash_alloc->size;
1890 	memcpy(smp_trash_alloc->area, smp->data.u.str.area, smp_trash_alloc->data);
1891 
1892 	ctx = EVP_CIPHER_CTX_new();
1893 
1894 	if (!ctx)
1895 		goto err;
1896 
1897 	smp_trash = alloc_trash_chunk();
1898 	if (!smp_trash)
1899 		goto err;
1900 
1901 	smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
1902 	if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
1903 		goto err;
1904 
1905 	if (arg_p[1].type == ARGT_VAR) {
1906 		dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
1907 		if (dec_size < 0)
1908 			goto err;
1909 		smp_trash->data = dec_size;
1910 		nonce.data.u.str = *smp_trash;
1911 	}
1912 
1913 	/* Set cipher type and mode */
1914 	switch(arg_p[0].data.sint) {
1915 	case 128:
1916 		EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
1917 		break;
1918 	case 192:
1919 		EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
1920 		break;
1921 	case 256:
1922 		EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
1923 		break;
1924 	}
1925 
1926 	EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, nonce.data.u.str.data, NULL);
1927 
1928 	/* Initialise IV */
1929 	if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) nonce.data.u.str.area))
1930 		goto err;
1931 
1932 	smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
1933 	if (!sample_conv_var2smp_str(&arg_p[2], &key))
1934 		goto err;
1935 
1936 	if (arg_p[2].type == ARGT_VAR) {
1937 		dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
1938 		if (dec_size < 0)
1939 			goto err;
1940 		smp_trash->data = dec_size;
1941 		key.data.u.str = *smp_trash;
1942 	}
1943 
1944 	/* Initialise key */
1945 	if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) key.data.u.str.area, NULL))
1946 		goto err;
1947 
1948 	if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
1949 	                       (unsigned char *) smp_trash_alloc->area, (int) smp_trash_alloc->data))
1950 		goto err;
1951 
1952 	smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
1953 	if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
1954 		goto err;
1955 
1956 	if (arg_p[3].type == ARGT_VAR) {
1957 		dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
1958 		if (dec_size < 0)
1959 			goto err;
1960 		smp_trash_alloc->data = dec_size;
1961 		aead_tag.data.u.str = *smp_trash_alloc;
1962 	}
1963 
1964 	dec_size = smp_trash->data;
1965 
1966 	EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, aead_tag.data.u.str.data, (void *) aead_tag.data.u.str.area);
1967 	ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
1968 
1969 	if (ret <= 0)
1970 		goto err;
1971 
1972 	smp->data.u.str.data = dec_size + smp_trash->data;
1973 	smp->data.u.str.area = smp_trash->area;
1974 	smp->data.type = SMP_T_BIN;
1975 	smp_dup(smp);
1976 	free_trash_chunk(smp_trash_alloc);
1977 	free_trash_chunk(smp_trash);
1978 	return 1;
1979 
1980 err:
1981 	free_trash_chunk(smp_trash_alloc);
1982 	free_trash_chunk(smp_trash);
1983 	return 0;
1984 }
1985 #endif
1986 
check_crypto_digest(struct arg * args,struct sample_conv * conv,const char * file,int line,char ** err)1987 static int check_crypto_digest(struct arg *args, struct sample_conv *conv,
1988 						  const char *file, int line, char **err)
1989 {
1990 	const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1991 
1992 	if (evp)
1993 		return 1;
1994 
1995 	memprintf(err, "algorithm must be a valid OpenSSL message digest name.");
1996 	return 0;
1997 }
1998 
sample_conv_crypto_digest(const struct arg * args,struct sample * smp,void * private)1999 static int sample_conv_crypto_digest(const struct arg *args, struct sample *smp, void *private)
2000 {
2001 	struct buffer *trash = get_trash_chunk();
2002 	unsigned char *md = (unsigned char*) trash->area;
2003 	unsigned int md_len = trash->size;
2004 	EVP_MD_CTX *ctx = EVP_MD_CTX_new();
2005 	const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
2006 
2007 	if (!ctx)
2008 		return 0;
2009 
2010 	if (!EVP_DigestInit_ex(ctx, evp, NULL) ||
2011 	    !EVP_DigestUpdate(ctx, smp->data.u.str.area, smp->data.u.str.data) ||
2012 	    !EVP_DigestFinal_ex(ctx, md, &md_len)) {
2013 		EVP_MD_CTX_free(ctx);
2014 		return 0;
2015 	}
2016 
2017 	EVP_MD_CTX_free(ctx);
2018 
2019 	trash->data = md_len;
2020 	smp->data.u.str = *trash;
2021 	smp->data.type = SMP_T_BIN;
2022 	smp->flags &= ~SMP_F_CONST;
2023 	return 1;
2024 }
2025 
check_crypto_hmac(struct arg * args,struct sample_conv * conv,const char * file,int line,char ** err)2026 static int check_crypto_hmac(struct arg *args, struct sample_conv *conv,
2027 						  const char *file, int line, char **err)
2028 {
2029 	if (!check_crypto_digest(args, conv, file, line, err))
2030 		return 0;
2031 
2032 	if (!sample_check_arg_base64(&args[1], err)) {
2033 		memprintf(err, "failed to parse key : %s", *err);
2034 		return 0;
2035 	}
2036 
2037 	return 1;
2038 }
2039 
sample_conv_crypto_hmac(const struct arg * args,struct sample * smp,void * private)2040 static int sample_conv_crypto_hmac(const struct arg *args, struct sample *smp, void *private)
2041 {
2042 	struct sample key;
2043 	struct buffer *trash = NULL, *key_trash = NULL;
2044 	unsigned char *md;
2045 	unsigned int md_len;
2046 	const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
2047 	int dec_size;
2048 
2049 	smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
2050 	if (!sample_conv_var2smp_str(&args[1], &key))
2051 		return 0;
2052 
2053 	if (args[1].type == ARGT_VAR) {
2054 		key_trash = alloc_trash_chunk();
2055 		if (!key_trash)
2056 			goto err;
2057 
2058 		dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, key_trash->area, key_trash->size);
2059 		if (dec_size < 0)
2060 			goto err;
2061 		key_trash->data = dec_size;
2062 		key.data.u.str = *key_trash;
2063 	}
2064 
2065 	trash = alloc_trash_chunk();
2066 	if (!trash)
2067 		goto err;
2068 
2069 	md = (unsigned char*) trash->area;
2070 	md_len = trash->size;
2071 	if (!HMAC(evp, key.data.u.str.area, key.data.u.str.data, (const unsigned char*) smp->data.u.str.area,
2072 	          smp->data.u.str.data, md, &md_len))
2073 		goto err;
2074 
2075 	free_trash_chunk(key_trash);
2076 
2077 	trash->data = md_len;
2078 	smp->data.u.str = *trash;
2079 	smp->data.type = SMP_T_BIN;
2080 	smp_dup(smp);
2081 	free_trash_chunk(trash);
2082 	return 1;
2083 
2084 err:
2085 	free_trash_chunk(key_trash);
2086 	free_trash_chunk(trash);
2087 	return 0;
2088 }
2089 
2090 #endif /* USE_OPENSSL */
2091 
sample_conv_bin2hex(const struct arg * arg_p,struct sample * smp,void * private)2092 static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
2093 {
2094 	struct buffer *trash = get_trash_chunk();
2095 	unsigned char c;
2096 	int ptr = 0;
2097 
2098 	trash->data = 0;
2099 	while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
2100 		c = smp->data.u.str.area[ptr++];
2101 		trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
2102 		trash->area[trash->data++] = hextab[c & 0xF];
2103 	}
2104 	smp->data.u.str = *trash;
2105 	smp->data.type = SMP_T_STR;
2106 	smp->flags &= ~SMP_F_CONST;
2107 	return 1;
2108 }
2109 
sample_conv_hex2int(const struct arg * arg_p,struct sample * smp,void * private)2110 static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
2111 {
2112 	long long int n = 0;
2113 	int i, c;
2114 
2115 	for (i = 0; i < smp->data.u.str.data; i++) {
2116 		if ((c = hex2i(smp->data.u.str.area[i])) < 0)
2117 			return 0;
2118 		n = (n << 4) + c;
2119 	}
2120 
2121 	smp->data.u.sint = n;
2122 	smp->data.type = SMP_T_SINT;
2123 	smp->flags &= ~SMP_F_CONST;
2124 	return 1;
2125 }
2126 
2127 /* hashes the binary input into a 32-bit unsigned int */
sample_conv_djb2(const struct arg * arg_p,struct sample * smp,void * private)2128 static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
2129 {
2130 	smp->data.u.sint = hash_djb2(smp->data.u.str.area,
2131 				     smp->data.u.str.data);
2132 	if (arg_p->data.sint)
2133 		smp->data.u.sint = full_hash(smp->data.u.sint);
2134 	smp->data.type = SMP_T_SINT;
2135 	return 1;
2136 }
2137 
sample_conv_length(const struct arg * arg_p,struct sample * smp,void * private)2138 static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
2139 {
2140 	int i = smp->data.u.str.data;
2141 	smp->data.u.sint = i;
2142 	smp->data.type = SMP_T_SINT;
2143 	return 1;
2144 }
2145 
2146 
sample_conv_str2lower(const struct arg * arg_p,struct sample * smp,void * private)2147 static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
2148 {
2149 	int i;
2150 
2151 	if (!smp_make_rw(smp))
2152 		return 0;
2153 
2154 	for (i = 0; i < smp->data.u.str.data; i++) {
2155 		if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
2156 			smp->data.u.str.area[i] += 'a' - 'A';
2157 	}
2158 	return 1;
2159 }
2160 
sample_conv_str2upper(const struct arg * arg_p,struct sample * smp,void * private)2161 static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
2162 {
2163 	int i;
2164 
2165 	if (!smp_make_rw(smp))
2166 		return 0;
2167 
2168 	for (i = 0; i < smp->data.u.str.data; i++) {
2169 		if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
2170 			smp->data.u.str.area[i] += 'A' - 'a';
2171 	}
2172 	return 1;
2173 }
2174 
2175 /* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
sample_conv_ipmask(const struct arg * args,struct sample * smp,void * private)2176 static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
2177 {
2178 	/* Attempt to convert to IPv4 to apply the correct mask. */
2179 	c_ipv62ip(smp);
2180 
2181 	if (smp->data.type == SMP_T_IPV4) {
2182 		smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
2183 		smp->data.type = SMP_T_IPV4;
2184 	}
2185 	else if (smp->data.type == SMP_T_IPV6) {
2186 		/* IPv6 cannot be converted without an IPv6 mask. */
2187 		if (args[1].type != ARGT_IPV6)
2188 			return 0;
2189 
2190 		write_u64(&smp->data.u.ipv6.s6_addr[0],
2191 			  read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0]));
2192 		write_u64(&smp->data.u.ipv6.s6_addr[8],
2193 			  read_u64(&smp->data.u.ipv6.s6_addr[8]) & read_u64(&args[1].data.ipv6.s6_addr[8]));
2194 		smp->data.type = SMP_T_IPV6;
2195 	}
2196 
2197 	return 1;
2198 }
2199 
2200 /* takes an UINT value on input supposed to represent the time since EPOCH,
2201  * adds an optional offset found in args[1] and emits a string representing
2202  * the local time in the format specified in args[1] using strftime().
2203  */
sample_conv_ltime(const struct arg * args,struct sample * smp,void * private)2204 static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
2205 {
2206 	struct buffer *temp;
2207 	/* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
2208 	time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
2209 	struct tm tm;
2210 
2211 	/* add offset */
2212 	if (args[1].type == ARGT_SINT)
2213 		curr_date += args[1].data.sint;
2214 
2215 	get_localtime(curr_date, &tm);
2216 
2217 	temp = get_trash_chunk();
2218 	temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
2219 	smp->data.u.str = *temp;
2220 	smp->data.type = SMP_T_STR;
2221 	return 1;
2222 }
2223 
2224 /* hashes the binary input into a 32-bit unsigned int */
sample_conv_sdbm(const struct arg * arg_p,struct sample * smp,void * private)2225 static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
2226 {
2227 	smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
2228 				     smp->data.u.str.data);
2229 	if (arg_p->data.sint)
2230 		smp->data.u.sint = full_hash(smp->data.u.sint);
2231 	smp->data.type = SMP_T_SINT;
2232 	return 1;
2233 }
2234 
2235 /* takes an UINT value on input supposed to represent the time since EPOCH,
2236  * adds an optional offset found in args[1] and emits a string representing
2237  * the UTC date in the format specified in args[1] using strftime().
2238  */
sample_conv_utime(const struct arg * args,struct sample * smp,void * private)2239 static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
2240 {
2241 	struct buffer *temp;
2242 	/* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
2243 	time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
2244 	struct tm tm;
2245 
2246 	/* add offset */
2247 	if (args[1].type == ARGT_SINT)
2248 		curr_date += args[1].data.sint;
2249 
2250 	get_gmtime(curr_date, &tm);
2251 
2252 	temp = get_trash_chunk();
2253 	temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
2254 	smp->data.u.str = *temp;
2255 	smp->data.type = SMP_T_STR;
2256 	return 1;
2257 }
2258 
2259 /* hashes the binary input into a 32-bit unsigned int */
sample_conv_wt6(const struct arg * arg_p,struct sample * smp,void * private)2260 static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
2261 {
2262 	smp->data.u.sint = hash_wt6(smp->data.u.str.area,
2263 				    smp->data.u.str.data);
2264 	if (arg_p->data.sint)
2265 		smp->data.u.sint = full_hash(smp->data.u.sint);
2266 	smp->data.type = SMP_T_SINT;
2267 	return 1;
2268 }
2269 
2270 /* hashes the binary input into a 32-bit unsigned int using xxh.
2271  * The seed of the hash defaults to 0 but can be changd in argument 1.
2272  */
sample_conv_xxh32(const struct arg * arg_p,struct sample * smp,void * private)2273 static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
2274 {
2275 	unsigned int seed;
2276 
2277 	if (arg_p->data.sint)
2278 		seed = arg_p->data.sint;
2279 	else
2280 		seed = 0;
2281 	smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
2282 				 seed);
2283 	smp->data.type = SMP_T_SINT;
2284 	return 1;
2285 }
2286 
2287 /* hashes the binary input into a 64-bit unsigned int using xxh.
2288  * In fact, the function returns a 64 bit unsigned, but the sample
2289  * storage of haproxy only proposes 64-bits signed, so the value is
2290  * cast as signed. This cast doesn't impact the hash repartition.
2291  * The seed of the hash defaults to 0 but can be changd in argument 1.
2292  */
sample_conv_xxh64(const struct arg * arg_p,struct sample * smp,void * private)2293 static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
2294 {
2295 	unsigned long long int seed;
2296 
2297 	if (arg_p->data.sint)
2298 		seed = (unsigned long long int)arg_p->data.sint;
2299 	else
2300 		seed = 0;
2301 	smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
2302 						smp->data.u.str.data, seed);
2303 	smp->data.type = SMP_T_SINT;
2304 	return 1;
2305 }
2306 
sample_conv_xxh3(const struct arg * arg_p,struct sample * smp,void * private)2307 static int sample_conv_xxh3(const struct arg *arg_p, struct sample *smp, void *private)
2308 {
2309 	unsigned long long int seed;
2310 
2311 	if (arg_p->data.sint)
2312 		seed = (unsigned long long int)arg_p->data.sint;
2313 	else
2314 		seed = 0;
2315 	smp->data.u.sint = (long long int)XXH3(smp->data.u.str.area,
2316 	                                       smp->data.u.str.data, seed);
2317 	smp->data.type = SMP_T_SINT;
2318 	return 1;
2319 }
2320 
2321 /* hashes the binary input into a 32-bit unsigned int */
sample_conv_crc32(const struct arg * arg_p,struct sample * smp,void * private)2322 static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
2323 {
2324 	smp->data.u.sint = hash_crc32(smp->data.u.str.area,
2325 				      smp->data.u.str.data);
2326 	if (arg_p->data.sint)
2327 		smp->data.u.sint = full_hash(smp->data.u.sint);
2328 	smp->data.type = SMP_T_SINT;
2329 	return 1;
2330 }
2331 
2332 /* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
sample_conv_crc32c(const struct arg * arg_p,struct sample * smp,void * private)2333 static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
2334 {
2335 	smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
2336 				       smp->data.u.str.data);
2337 	if (arg_p->data.sint)
2338 		smp->data.u.sint = full_hash(smp->data.u.sint);
2339 	smp->data.type = SMP_T_SINT;
2340 	return 1;
2341 }
2342 
2343 /* This function escape special json characters. The returned string can be
2344  * safely set between two '"' and used as json string. The json string is
2345  * defined like this:
2346  *
2347  *    any Unicode character except '"' or '\' or control character
2348  *    \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
2349  *
2350  * The enum input_type contain all the allowed mode for decoding the input
2351  * string.
2352  */
2353 enum input_type {
2354 	IT_ASCII = 0,
2355 	IT_UTF8,
2356 	IT_UTF8S,
2357 	IT_UTF8P,
2358 	IT_UTF8PS,
2359 };
2360 
sample_conv_json_check(struct arg * arg,struct sample_conv * conv,const char * file,int line,char ** err)2361 static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
2362                                   const char *file, int line, char **err)
2363 {
2364 	enum input_type type;
2365 
2366 	if (strcmp(arg->data.str.area, "") == 0)
2367 		type = IT_ASCII;
2368 	else if (strcmp(arg->data.str.area, "ascii") == 0)
2369 		type = IT_ASCII;
2370 	else if (strcmp(arg->data.str.area, "utf8") == 0)
2371 		type = IT_UTF8;
2372 	else if (strcmp(arg->data.str.area, "utf8s") == 0)
2373 		type = IT_UTF8S;
2374 	else if (strcmp(arg->data.str.area, "utf8p") == 0)
2375 		type = IT_UTF8P;
2376 	else if (strcmp(arg->data.str.area, "utf8ps") == 0)
2377 		type = IT_UTF8PS;
2378 	else {
2379 		memprintf(err, "Unexpected input code type. "
2380 			  "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
2381 		return 0;
2382 	}
2383 
2384 	chunk_destroy(&arg->data.str);
2385 	arg->type = ARGT_SINT;
2386 	arg->data.sint = type;
2387 	return 1;
2388 }
2389 
sample_conv_json(const struct arg * arg_p,struct sample * smp,void * private)2390 static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
2391 {
2392 	struct buffer *temp;
2393 	char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
2394 	const char *str;
2395 	int len;
2396 	enum input_type input_type = IT_ASCII;
2397 	unsigned int c;
2398 	unsigned int ret;
2399 	char *p;
2400 
2401 	input_type = arg_p->data.sint;
2402 
2403 	temp = get_trash_chunk();
2404 	temp->data = 0;
2405 
2406 	p = smp->data.u.str.area;
2407 	while (p < smp->data.u.str.area + smp->data.u.str.data) {
2408 
2409 		if (input_type == IT_ASCII) {
2410 			/* Read input as ASCII. */
2411 			c = *(unsigned char *)p;
2412 			p++;
2413 		}
2414 		else {
2415 			/* Read input as UTF8. */
2416 			ret = utf8_next(p,
2417 					smp->data.u.str.data - ( p - smp->data.u.str.area),
2418 					&c);
2419 			p += utf8_return_length(ret);
2420 
2421 			if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2422 					return 0;
2423 			if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2424 					continue;
2425 			if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2426 					return 0;
2427 			if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2428 					continue;
2429 
2430 			/* Check too big values. */
2431 			if ((unsigned int)c > 0xffff) {
2432 				if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2433 					return 0;
2434 				continue;
2435 			}
2436 		}
2437 
2438 		/* Convert character. */
2439 		if (c == '"') {
2440 			len = 2;
2441 			str = "\\\"";
2442 		}
2443 		else if (c == '\\') {
2444 			len = 2;
2445 			str = "\\\\";
2446 		}
2447 		else if (c == '/') {
2448 			len = 2;
2449 			str = "\\/";
2450 		}
2451 		else if (c == '\b') {
2452 			len = 2;
2453 			str = "\\b";
2454 		}
2455 		else if (c == '\f') {
2456 			len = 2;
2457 			str = "\\f";
2458 		}
2459 		else if (c == '\r') {
2460 			len = 2;
2461 			str = "\\r";
2462 		}
2463 		else if (c == '\n') {
2464 			len = 2;
2465 			str = "\\n";
2466 		}
2467 		else if (c == '\t') {
2468 			len = 2;
2469 			str = "\\t";
2470 		}
2471 		else if (c > 0xff || !isprint((unsigned char)c)) {
2472 			/* isprint generate a segfault if c is too big. The man says that
2473 			 * c must have the value of an unsigned char or EOF.
2474 			 */
2475 			len = 6;
2476 			_str[0] = '\\';
2477 			_str[1] = 'u';
2478 			snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2479 			str = _str;
2480 		}
2481 		else {
2482 			len = 1;
2483 			_str[0] = c;
2484 			str = _str;
2485 		}
2486 
2487 		/* Check length */
2488 		if (temp->data + len > temp->size)
2489 			return 0;
2490 
2491 		/* Copy string. */
2492 		memcpy(temp->area + temp->data, str, len);
2493 		temp->data += len;
2494 	}
2495 
2496 	smp->flags &= ~SMP_F_CONST;
2497 	smp->data.u.str = *temp;
2498 	smp->data.type = SMP_T_STR;
2499 
2500 	return 1;
2501 }
2502 
2503 /* This sample function is designed to extract some bytes from an input buffer.
2504  * First arg is the offset.
2505  * Optional second arg is the length to truncate */
sample_conv_bytes(const struct arg * arg_p,struct sample * smp,void * private)2506 static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
2507 {
2508 	if (smp->data.u.str.data <= arg_p[0].data.sint) {
2509 		smp->data.u.str.data = 0;
2510 		return 1;
2511 	}
2512 
2513 	if (smp->data.u.str.size)
2514 			smp->data.u.str.size -= arg_p[0].data.sint;
2515 	smp->data.u.str.data -= arg_p[0].data.sint;
2516 	smp->data.u.str.area += arg_p[0].data.sint;
2517 
2518 	if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2519 		smp->data.u.str.data = arg_p[1].data.sint;
2520 
2521 	return 1;
2522 }
2523 
sample_conv_field_check(struct arg * args,struct sample_conv * conv,const char * file,int line,char ** err)2524 static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2525                                   const char *file, int line, char **err)
2526 {
2527 	struct arg *arg = args;
2528 
2529 	if (arg->type != ARGT_SINT) {
2530 		memprintf(err, "Unexpected arg type");
2531 		return 0;
2532 	}
2533 
2534 	if (!arg->data.sint) {
2535 		memprintf(err, "Unexpected value 0 for index");
2536 		return 0;
2537 	}
2538 
2539 	arg++;
2540 
2541 	if (arg->type != ARGT_STR) {
2542 		memprintf(err, "Unexpected arg type");
2543 		return 0;
2544 	}
2545 
2546 	if (!arg->data.str.data) {
2547 		memprintf(err, "Empty separators list");
2548 		return 0;
2549 	}
2550 
2551 	return 1;
2552 }
2553 
2554 /* This sample function is designed to a return selected part of a string (field).
2555  * First arg is the index of the field (start at 1)
2556  * Second arg is a char list of separators (type string)
2557  */
sample_conv_field(const struct arg * arg_p,struct sample * smp,void * private)2558 static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
2559 {
2560 	int field;
2561 	char *start, *end;
2562 	int i;
2563 	int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
2564 
2565 	if (!arg_p[0].data.sint)
2566 		return 0;
2567 
2568 	if (arg_p[0].data.sint < 0) {
2569 		field = -1;
2570 		end = start = smp->data.u.str.area + smp->data.u.str.data;
2571 		while (start > smp->data.u.str.area) {
2572 			for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2573 				if (*(start-1) == arg_p[1].data.str.area[i]) {
2574 					if (field == arg_p[0].data.sint) {
2575 						if (count == 1)
2576 							goto found;
2577 						else if (count > 1)
2578 							count--;
2579 					} else {
2580 						end = start-1;
2581 						field--;
2582 					}
2583 					break;
2584 				}
2585 			}
2586 			start--;
2587 		}
2588 	} else {
2589 		field = 1;
2590 		end = start = smp->data.u.str.area;
2591 		while (end - smp->data.u.str.area < smp->data.u.str.data) {
2592 			for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2593 				if (*end == arg_p[1].data.str.area[i]) {
2594 					if (field == arg_p[0].data.sint) {
2595 						if (count == 1)
2596 							goto found;
2597 						else if (count > 1)
2598 							count--;
2599 					} else {
2600 						start = end+1;
2601 						field++;
2602 					}
2603 					break;
2604 				}
2605 			}
2606 			end++;
2607 		}
2608 	}
2609 
2610 	/* Field not found */
2611 	if (field != arg_p[0].data.sint) {
2612 		smp->data.u.str.data = 0;
2613 		return 0;
2614 	}
2615 found:
2616 	smp->data.u.str.data = end - start;
2617 	/* If ret string is len 0, no need to
2618            change pointers or to update size */
2619 	if (!smp->data.u.str.data)
2620 		return 1;
2621 
2622 	/* Compute remaining size if needed
2623            Note: smp->data.u.str.size cannot be set to 0 */
2624 	if (smp->data.u.str.size)
2625 		smp->data.u.str.size -= start - smp->data.u.str.area;
2626 
2627 	smp->data.u.str.area = start;
2628 
2629 	return 1;
2630 }
2631 
2632 /* This sample function is designed to return a word from a string.
2633  * First arg is the index of the word (start at 1)
2634  * Second arg is a char list of words separators (type string)
2635  */
sample_conv_word(const struct arg * arg_p,struct sample * smp,void * private)2636 static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
2637 {
2638 	int word;
2639 	char *start, *end;
2640 	int i, issep, inword;
2641 	int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
2642 
2643 	if (!arg_p[0].data.sint)
2644 		return 0;
2645 
2646 	word = 0;
2647 	inword = 0;
2648 	if (arg_p[0].data.sint < 0) {
2649 		end = start = smp->data.u.str.area + smp->data.u.str.data;
2650 		while (start > smp->data.u.str.area) {
2651 			issep = 0;
2652 			for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2653 				if (*(start-1) == arg_p[1].data.str.area[i]) {
2654 					issep = 1;
2655 					break;
2656 				}
2657 			}
2658 			if (!inword) {
2659 				if (!issep) {
2660 					if (word != arg_p[0].data.sint) {
2661 						word--;
2662 						end = start;
2663 					}
2664 					inword = 1;
2665 				}
2666 			}
2667 			else if (issep) {
2668 				if (word == arg_p[0].data.sint) {
2669 					if (count == 1)
2670 						goto found;
2671 					else if (count > 1)
2672 						count--;
2673 				}
2674 				inword = 0;
2675 			}
2676 			start--;
2677 		}
2678 	} else {
2679 		end = start = smp->data.u.str.area;
2680 		while (end - smp->data.u.str.area < smp->data.u.str.data) {
2681 			issep = 0;
2682 			for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2683 				if (*end == arg_p[1].data.str.area[i]) {
2684 					issep = 1;
2685 					break;
2686 				}
2687 			}
2688 			if (!inword) {
2689 				if (!issep) {
2690 					if (word != arg_p[0].data.sint) {
2691 						word++;
2692 						start = end;
2693 					}
2694 					inword = 1;
2695 				}
2696 			}
2697 			else if (issep) {
2698 				if (word == arg_p[0].data.sint) {
2699 					if (count == 1)
2700 						goto found;
2701 					else if (count > 1)
2702 						count--;
2703 				}
2704 				inword = 0;
2705 			}
2706 			end++;
2707 		}
2708 	}
2709 
2710 	/* Field not found */
2711 	if (word != arg_p[0].data.sint) {
2712 		smp->data.u.str.data = 0;
2713 		return 1;
2714 	}
2715 found:
2716 	smp->data.u.str.data = end - start;
2717 	/* If ret string is len 0, no need to
2718            change pointers or to update size */
2719 	if (!smp->data.u.str.data)
2720 		return 1;
2721 
2722 	smp->data.u.str.area = start;
2723 
2724 	/* Compute remaining size if needed
2725            Note: smp->data.u.str.size cannot be set to 0 */
2726 	if (smp->data.u.str.size)
2727 		smp->data.u.str.size -= start - smp->data.u.str.area;
2728 
2729 	return 1;
2730 }
2731 
sample_conv_regsub_check(struct arg * args,struct sample_conv * conv,const char * file,int line,char ** err)2732 static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2733                                     const char *file, int line, char **err)
2734 {
2735 	struct arg *arg = args;
2736 	char *p;
2737 	int len;
2738 
2739 	/* arg0 is a regex, it uses type_flag for ICASE and global match */
2740 	arg[0].type_flags = 0;
2741 
2742 	if (arg[2].type != ARGT_STR)
2743 		return 1;
2744 
2745 	p = arg[2].data.str.area;
2746 	len = arg[2].data.str.data;
2747 	while (len) {
2748 		if (*p == 'i') {
2749 			arg[0].type_flags |= ARGF_REG_ICASE;
2750 		}
2751 		else if (*p == 'g') {
2752 			arg[0].type_flags |= ARGF_REG_GLOB;
2753 		}
2754 		else {
2755 			memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2756 			return 0;
2757 		}
2758 		p++;
2759 		len--;
2760 	}
2761 	return 1;
2762 }
2763 
2764 /* This sample function is designed to do the equivalent of s/match/replace/ on
2765  * the input string. It applies a regex and restarts from the last matched
2766  * location until nothing matches anymore. First arg is the regex to apply to
2767  * the input string, second arg is the replacement expression.
2768  */
sample_conv_regsub(const struct arg * arg_p,struct sample * smp,void * private)2769 static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
2770 {
2771 	char *start, *end;
2772 	struct my_regex *reg = arg_p[0].data.reg;
2773 	regmatch_t pmatch[MAX_MATCH];
2774 	struct buffer *trash = get_trash_chunk();
2775 	struct buffer *output;
2776 	int flag, max;
2777 	int found;
2778 
2779 	start = smp->data.u.str.area;
2780 	end = start + smp->data.u.str.data;
2781 
2782 	flag = 0;
2783 	while (1) {
2784 		/* check for last round which is used to copy remaining parts
2785 		 * when not running in global replacement mode.
2786 		 */
2787 		found = 0;
2788 		if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2789 			/* Note: we can have start == end on empty strings or at the end */
2790 			found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2791 		}
2792 
2793 		if (!found)
2794 			pmatch[0].rm_so = end - start;
2795 
2796 		/* copy the heading non-matching part (which may also be the tail if nothing matches) */
2797 		max = trash->size - trash->data;
2798 		if (max && pmatch[0].rm_so > 0) {
2799 			if (max > pmatch[0].rm_so)
2800 				max = pmatch[0].rm_so;
2801 			memcpy(trash->area + trash->data, start, max);
2802 			trash->data += max;
2803 		}
2804 
2805 		if (!found)
2806 			break;
2807 
2808 		output = alloc_trash_chunk();
2809 		if (!output)
2810 			break;
2811 
2812 		output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2813 
2814 		/* replace the matching part */
2815 		max = output->size - output->data;
2816 		if (max) {
2817 			if (max > output->data)
2818 				max = output->data;
2819 			memcpy(trash->area + trash->data,
2820 			       output->area, max);
2821 			trash->data += max;
2822 		}
2823 
2824 		free_trash_chunk(output);
2825 
2826 		/* stop here if we're done with this string */
2827 		if (start >= end)
2828 			break;
2829 
2830 		/* We have a special case for matches of length 0 (eg: "x*y*").
2831 		 * These ones are considered to match in front of a character,
2832 		 * so we have to copy that character and skip to the next one.
2833 		 */
2834 		if (!pmatch[0].rm_eo) {
2835 			if (trash->data < trash->size)
2836 				trash->area[trash->data++] = start[pmatch[0].rm_eo];
2837 			pmatch[0].rm_eo++;
2838 		}
2839 
2840 		start += pmatch[0].rm_eo;
2841 		flag |= REG_NOTBOL;
2842 	}
2843 
2844 	smp->data.u.str = *trash;
2845 	return 1;
2846 }
2847 
2848 /* This function check an operator entry. It expects a string.
2849  * The string can be an integer or a variable name.
2850  */
check_operator(struct arg * args,struct sample_conv * conv,const char * file,int line,char ** err)2851 static int check_operator(struct arg *args, struct sample_conv *conv,
2852                           const char *file, int line, char **err)
2853 {
2854 	const char *str;
2855 	const char *end;
2856 	long long int i;
2857 
2858 	/* Try to decode a variable. */
2859 	if (vars_check_arg(&args[0], NULL))
2860 		return 1;
2861 
2862 	/* Try to convert an integer */
2863 	str = args[0].data.str.area;
2864 	end = str + strlen(str);
2865 	i = read_int64(&str, end);
2866 	if (*str != '\0') {
2867 		memprintf(err, "expects an integer or a variable name");
2868 		return 0;
2869 	}
2870 
2871 	chunk_destroy(&args[0].data.str);
2872 	args[0].type = ARGT_SINT;
2873 	args[0].data.sint = i;
2874 	return 1;
2875 }
2876 
2877 /* This function returns a sample struct filled with an arg content.
2878  * If the arg contain an integer, the integer is returned in the
2879  * sample. If the arg contains a variable descriptor, it returns the
2880  * variable value.
2881  *
2882  * This function returns 0 if an error occurs, otherwise it returns 1.
2883  */
sample_conv_var2smp_sint(const struct arg * arg,struct sample * smp)2884 int sample_conv_var2smp_sint(const struct arg *arg, struct sample *smp)
2885 {
2886 	switch (arg->type) {
2887 	case ARGT_SINT:
2888 		smp->data.type = SMP_T_SINT;
2889 		smp->data.u.sint = arg->data.sint;
2890 		return 1;
2891 	case ARGT_VAR:
2892 		return sample_conv_var2smp(&arg->data.var, smp, SMP_T_SINT);
2893 	default:
2894 		return 0;
2895 	}
2896 }
2897 
2898 /* Takes a SINT on input, applies a binary twos complement and returns the SINT
2899  * result.
2900  */
sample_conv_binary_cpl(const struct arg * arg_p,struct sample * smp,void * private)2901 static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
2902 {
2903 	smp->data.u.sint = ~smp->data.u.sint;
2904 	return 1;
2905 }
2906 
2907 /* Takes a SINT on input, applies a binary "and" with the SINT directly in
2908  * arg_p or in the variable described in arg_p, and returns the SINT result.
2909  */
sample_conv_binary_and(const struct arg * arg_p,struct sample * smp,void * private)2910 static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
2911 {
2912 	struct sample tmp;
2913 
2914 	smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2915 	if (!sample_conv_var2smp_sint(arg_p, &tmp))
2916 		return 0;
2917 	smp->data.u.sint &= tmp.data.u.sint;
2918 	return 1;
2919 }
2920 
2921 /* Takes a SINT on input, applies a binary "or" with the SINT directly in
2922  * arg_p or in the variable described in arg_p, and returns the SINT result.
2923  */
sample_conv_binary_or(const struct arg * arg_p,struct sample * smp,void * private)2924 static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
2925 {
2926 	struct sample tmp;
2927 
2928 	smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2929 	if (!sample_conv_var2smp_sint(arg_p, &tmp))
2930 		return 0;
2931 	smp->data.u.sint |= tmp.data.u.sint;
2932 	return 1;
2933 }
2934 
2935 /* Takes a SINT on input, applies a binary "xor" with the SINT directly in
2936  * arg_p or in the variable described in arg_p, and returns the SINT result.
2937  */
sample_conv_binary_xor(const struct arg * arg_p,struct sample * smp,void * private)2938 static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
2939 {
2940 	struct sample tmp;
2941 
2942 	smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2943 	if (!sample_conv_var2smp_sint(arg_p, &tmp))
2944 		return 0;
2945 	smp->data.u.sint ^= tmp.data.u.sint;
2946 	return 1;
2947 }
2948 
arith_add(long long int a,long long int b)2949 static inline long long int arith_add(long long int a, long long int b)
2950 {
2951 	/* Prevent overflow and makes capped calculus.
2952 	 * We must ensure that the check calculus doesn't
2953 	 * exceed the signed 64 bits limits.
2954 	 *
2955 	 *        +----------+----------+
2956 	 *        |   a<0    |   a>=0   |
2957 	 * +------+----------+----------+
2958 	 * | b<0  | MIN-a>b  | no check |
2959 	 * +------+----------+----------+
2960 	 * | b>=0 | no check | MAX-a<b  |
2961 	 * +------+----------+----------+
2962 	 */
2963 	if ((a ^ b) >= 0) {
2964 		/* signs are different. */
2965 		if (a < 0) {
2966 			if (LLONG_MIN - a > b)
2967 				return LLONG_MIN;
2968 		}
2969 		if (LLONG_MAX - a < b)
2970 			return LLONG_MAX;
2971 	}
2972 	return a + b;
2973 }
2974 
2975 /* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
2976  * arg_p or in the variable described in arg_p, and returns the SINT result.
2977  */
sample_conv_arith_add(const struct arg * arg_p,struct sample * smp,void * private)2978 static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
2979 {
2980 	struct sample tmp;
2981 
2982 	smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2983 	if (!sample_conv_var2smp_sint(arg_p, &tmp))
2984 		return 0;
2985 	smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
2986 	return 1;
2987 }
2988 
2989 /* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
2990  * arg_p or in the variable described in arg_p, and returns the SINT result.
2991  */
sample_conv_arith_sub(const struct arg * arg_p,struct sample * smp,void * private)2992 static int sample_conv_arith_sub(const struct arg *arg_p,
2993                                  struct sample *smp, void *private)
2994 {
2995 	struct sample tmp;
2996 
2997 	smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2998 	if (!sample_conv_var2smp_sint(arg_p, &tmp))
2999 		return 0;
3000 
3001 	/* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
3002 	 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
3003 	 * of -LLONG_MIN and correct the result.
3004 	 */
3005 	if (tmp.data.u.sint == LLONG_MIN) {
3006 		smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
3007 		if (smp->data.u.sint < LLONG_MAX)
3008 			smp->data.u.sint++;
3009 		return 1;
3010 	}
3011 
3012 	/* standard subtraction: we use the "add" function and negate
3013 	 * the second operand.
3014 	 */
3015 	smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
3016 	return 1;
3017 }
3018 
3019 /* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
3020  * arg_p or in the variable described in arg_p, and returns the SINT result.
3021  * If the result makes an overflow, then the largest possible quantity is
3022  * returned.
3023  */
sample_conv_arith_mul(const struct arg * arg_p,struct sample * smp,void * private)3024 static int sample_conv_arith_mul(const struct arg *arg_p,
3025                                  struct sample *smp, void *private)
3026 {
3027 	struct sample tmp;
3028 	long long int c;
3029 
3030 	smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3031 	if (!sample_conv_var2smp_sint(arg_p, &tmp))
3032 		return 0;
3033 
3034 	/* prevent divide by 0 during the check */
3035 	if (!smp->data.u.sint || !tmp.data.u.sint) {
3036 		smp->data.u.sint = 0;
3037 		return 1;
3038 	}
3039 
3040 	/* The multiply between LLONG_MIN and -1 returns a
3041 	 * "floating point exception".
3042 	 */
3043 	if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3044 		smp->data.u.sint = LLONG_MAX;
3045 		return 1;
3046 	}
3047 
3048 	/* execute standard multiplication. */
3049 	c = smp->data.u.sint * tmp.data.u.sint;
3050 
3051 	/* check for overflow and makes capped multiply. */
3052 	if (smp->data.u.sint != c / tmp.data.u.sint) {
3053 		if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
3054 			smp->data.u.sint = LLONG_MAX;
3055 			return 1;
3056 		}
3057 		smp->data.u.sint = LLONG_MIN;
3058 		return 1;
3059 	}
3060 	smp->data.u.sint = c;
3061 	return 1;
3062 }
3063 
3064 /* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
3065  * arg_p or in the variable described in arg_p, and returns the SINT result.
3066  * If arg_p makes the result overflow, then the largest possible quantity is
3067  * returned.
3068  */
sample_conv_arith_div(const struct arg * arg_p,struct sample * smp,void * private)3069 static int sample_conv_arith_div(const struct arg *arg_p,
3070                                  struct sample *smp, void *private)
3071 {
3072 	struct sample tmp;
3073 
3074 	smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3075 	if (!sample_conv_var2smp_sint(arg_p, &tmp))
3076 		return 0;
3077 
3078 	if (tmp.data.u.sint) {
3079 		/* The divide between LLONG_MIN and -1 returns a
3080 		 * "floating point exception".
3081 		 */
3082 		if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3083 			smp->data.u.sint = LLONG_MAX;
3084 			return 1;
3085 		}
3086 		smp->data.u.sint /= tmp.data.u.sint;
3087 		return 1;
3088 	}
3089 	smp->data.u.sint = LLONG_MAX;
3090 	return 1;
3091 }
3092 
3093 /* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
3094  * arg_p or in the variable described in arg_p, and returns the SINT result.
3095  * If arg_p makes the result overflow, then 0 is returned.
3096  */
sample_conv_arith_mod(const struct arg * arg_p,struct sample * smp,void * private)3097 static int sample_conv_arith_mod(const struct arg *arg_p,
3098                                  struct sample *smp, void *private)
3099 {
3100 	struct sample tmp;
3101 
3102 	smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3103 	if (!sample_conv_var2smp_sint(arg_p, &tmp))
3104 		return 0;
3105 
3106 	if (tmp.data.u.sint) {
3107 		/* The divide between LLONG_MIN and -1 returns a
3108 		 * "floating point exception".
3109 		 */
3110 		if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3111 			smp->data.u.sint = 0;
3112 			return 1;
3113 		}
3114 		smp->data.u.sint %= tmp.data.u.sint;
3115 		return 1;
3116 	}
3117 	smp->data.u.sint = 0;
3118 	return 1;
3119 }
3120 
3121 /* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
3122  * result.
3123  */
sample_conv_arith_neg(const struct arg * arg_p,struct sample * smp,void * private)3124 static int sample_conv_arith_neg(const struct arg *arg_p,
3125                                  struct sample *smp, void *private)
3126 {
3127 	if (smp->data.u.sint == LLONG_MIN)
3128 		smp->data.u.sint = LLONG_MAX;
3129 	else
3130 		smp->data.u.sint = -smp->data.u.sint;
3131 	return 1;
3132 }
3133 
3134 /* Takes a SINT on input, returns true is the value is non-null, otherwise
3135  * false. The output is a BOOL.
3136  */
sample_conv_arith_bool(const struct arg * arg_p,struct sample * smp,void * private)3137 static int sample_conv_arith_bool(const struct arg *arg_p,
3138                                   struct sample *smp, void *private)
3139 {
3140 	smp->data.u.sint = !!smp->data.u.sint;
3141 	smp->data.type = SMP_T_BOOL;
3142 	return 1;
3143 }
3144 
3145 /* Takes a SINT on input, returns false is the value is non-null, otherwise
3146  * truee. The output is a BOOL.
3147  */
sample_conv_arith_not(const struct arg * arg_p,struct sample * smp,void * private)3148 static int sample_conv_arith_not(const struct arg *arg_p,
3149                                  struct sample *smp, void *private)
3150 {
3151 	smp->data.u.sint = !smp->data.u.sint;
3152 	smp->data.type = SMP_T_BOOL;
3153 	return 1;
3154 }
3155 
3156 /* Takes a SINT on input, returns true is the value is odd, otherwise false.
3157  * The output is a BOOL.
3158  */
sample_conv_arith_odd(const struct arg * arg_p,struct sample * smp,void * private)3159 static int sample_conv_arith_odd(const struct arg *arg_p,
3160                                  struct sample *smp, void *private)
3161 {
3162 	smp->data.u.sint = smp->data.u.sint & 1;
3163 	smp->data.type = SMP_T_BOOL;
3164 	return 1;
3165 }
3166 
3167 /* Takes a SINT on input, returns true is the value is even, otherwise false.
3168  * The output is a BOOL.
3169  */
sample_conv_arith_even(const struct arg * arg_p,struct sample * smp,void * private)3170 static int sample_conv_arith_even(const struct arg *arg_p,
3171                                   struct sample *smp, void *private)
3172 {
3173 	smp->data.u.sint = !(smp->data.u.sint & 1);
3174 	smp->data.type = SMP_T_BOOL;
3175 	return 1;
3176 }
3177 
3178 /* appends an optional const string, an optional variable contents and another
3179  * optional const string to an existing string.
3180  */
sample_conv_concat(const struct arg * arg_p,struct sample * smp,void * private)3181 static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
3182 {
3183 	struct buffer *trash;
3184 	struct sample tmp;
3185 	int max;
3186 
3187 	trash = alloc_trash_chunk();
3188 	if (!trash)
3189 		return 0;
3190 
3191 	trash->data = smp->data.u.str.data;
3192 	if (trash->data > trash->size - 1)
3193 		trash->data = trash->size - 1;
3194 
3195 	memcpy(trash->area, smp->data.u.str.area, trash->data);
3196 	trash->area[trash->data] = 0;
3197 
3198 	/* append first string */
3199 	max = arg_p[0].data.str.data;
3200 	if (max > trash->size - 1 - trash->data)
3201 		max = trash->size - 1 - trash->data;
3202 
3203 	if (max) {
3204 		memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
3205 		trash->data += max;
3206 		trash->area[trash->data] = 0;
3207 	}
3208 
3209 	/* append second string (variable) if it's found and we can turn it
3210 	 * into a string.
3211 	 */
3212 	smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3213 	if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
3214 	    (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
3215 	     sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
3216 
3217 		max = tmp.data.u.str.data;
3218 		if (max > trash->size - 1 - trash->data)
3219 			max = trash->size - 1 - trash->data;
3220 
3221 		if (max) {
3222 			memcpy(trash->area + trash->data, tmp.data.u.str.area,
3223 			       max);
3224 			trash->data += max;
3225 			trash->area[trash->data] = 0;
3226 		}
3227 	}
3228 
3229 	/* append third string */
3230 	max = arg_p[2].data.str.data;
3231 	if (max > trash->size - 1 - trash->data)
3232 		max = trash->size - 1 - trash->data;
3233 
3234 	if (max) {
3235 		memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
3236 		trash->data += max;
3237 		trash->area[trash->data] = 0;
3238 	}
3239 
3240 	smp->data.u.str = *trash;
3241 	smp->data.type = SMP_T_STR;
3242 	smp_dup(smp);
3243 	free_trash_chunk(trash);
3244 	return 1;
3245 }
3246 
3247 /* This function checks the "concat" converter's arguments and extracts the
3248  * variable name and its scope.
3249  */
smp_check_concat(struct arg * args,struct sample_conv * conv,const char * file,int line,char ** err)3250 static int smp_check_concat(struct arg *args, struct sample_conv *conv,
3251                            const char *file, int line, char **err)
3252 {
3253 	/* Try to decode a variable. */
3254 	if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
3255 		memprintf(err, "failed to register variable name '%s'",
3256 			  args[1].data.str.area);
3257 		return 0;
3258 	}
3259 	return 1;
3260 }
3261 
3262 /* Compares string with a variable containing a string. Return value
3263  * is compatible with strcmp(3)'s return value.
3264  */
sample_conv_strcmp(const struct arg * arg_p,struct sample * smp,void * private)3265 static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
3266 {
3267 	struct sample tmp;
3268 	int max, result;
3269 
3270 	smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3271 	if (arg_p[0].type != ARGT_VAR)
3272 		return 0;
3273 
3274 	if (!sample_conv_var2smp(&arg_p[0].data.var, &tmp, SMP_T_STR))
3275 		return 0;
3276 
3277 	max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
3278 	result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
3279 	if (result == 0) {
3280 		if (smp->data.u.str.data != tmp.data.u.str.data) {
3281 			if (smp->data.u.str.data < tmp.data.u.str.data) {
3282 				result = -1;
3283 			}
3284 			else {
3285 				result = 1;
3286 			}
3287 		}
3288 	}
3289 
3290 	smp->data.u.sint = result;
3291 	smp->data.type = SMP_T_SINT;
3292 	return 1;
3293 }
3294 
3295 #if defined(HAVE_CRYPTO_memcmp)
3296 /* Compares bytestring with a variable containing a bytestring. Return value
3297  * is `true` if both bytestrings are bytewise identical and `false` otherwise.
3298  *
3299  * Comparison will be performed in constant time if both bytestrings are of
3300  * the same length. If the lengths differ execution time will not be constant.
3301  */
sample_conv_secure_memcmp(const struct arg * arg_p,struct sample * smp,void * private)3302 static int sample_conv_secure_memcmp(const struct arg *arg_p, struct sample *smp, void *private)
3303 {
3304 	struct sample tmp;
3305 	int result;
3306 
3307 	smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3308 	if (arg_p[0].type != ARGT_VAR)
3309 		return 0;
3310 
3311 	if (!sample_conv_var2smp(&arg_p[0].data.var, &tmp, SMP_T_BIN))
3312 		return 0;
3313 
3314 	if (smp->data.u.str.data != tmp.data.u.str.data) {
3315 		smp->data.u.sint = 0;
3316 		smp->data.type = SMP_T_BOOL;
3317 		return 1;
3318 	}
3319 
3320 	/* The following comparison is performed in constant time. */
3321 	result = CRYPTO_memcmp(smp->data.u.str.area, tmp.data.u.str.area, smp->data.u.str.data);
3322 
3323 	smp->data.u.sint = result == 0;
3324 	smp->data.type = SMP_T_BOOL;
3325 	return 1;
3326 }
3327 #endif
3328 
3329 /* Takes a boolean as input. Returns the first argument if that boolean is true and
3330  * the second argument otherwise.
3331  */
sample_conv_iif(const struct arg * arg_p,struct sample * smp,void * private)3332 static int sample_conv_iif(const struct arg *arg_p, struct sample *smp, void *private)
3333 {
3334 	smp->data.type = SMP_T_STR;
3335 	smp->flags |= SMP_F_CONST;
3336 
3337 	if (smp->data.u.sint) {
3338 		smp->data.u.str.data = arg_p[0].data.str.data;
3339 		smp->data.u.str.area = arg_p[0].data.str.area;
3340 	}
3341 	else {
3342 		smp->data.u.str.data = arg_p[1].data.str.data;
3343 		smp->data.u.str.area = arg_p[1].data.str.area;
3344 	}
3345 
3346 	return 1;
3347 }
3348 
3349 #define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
3350 #define GRPC_MSG_LENGTH_SZ        4 /* 4 bytes */
3351 #define GRPC_MSG_HEADER_SZ        (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
3352 
3353 /*
3354  * Extract the field value of an input binary sample. Takes a mandatory argument:
3355  * the protocol buffers field identifier (dotted notation) internally represented
3356  * as an array of unsigned integers and its size.
3357  * Return 1 if the field was found, 0 if not.
3358  */
sample_conv_ungrpc(const struct arg * arg_p,struct sample * smp,void * private)3359 static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
3360 {
3361 	unsigned char *pos;
3362 	size_t grpc_left;
3363 
3364 	pos = (unsigned char *)smp->data.u.str.area;
3365 	grpc_left = smp->data.u.str.data;
3366 
3367 	while (grpc_left > GRPC_MSG_HEADER_SZ) {
3368 		size_t grpc_msg_len, left;
3369 
3370 		grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
3371 
3372 		pos += GRPC_MSG_HEADER_SZ;
3373 		grpc_left -= GRPC_MSG_HEADER_SZ;
3374 
3375 		if (grpc_left < left)
3376 			return 0;
3377 
3378 		if (protobuf_field_lookup(arg_p, smp, &pos, &left))
3379 			return 1;
3380 
3381 		grpc_left -= grpc_msg_len;
3382 	}
3383 
3384 	return 0;
3385 }
3386 
sample_conv_protobuf(const struct arg * arg_p,struct sample * smp,void * private)3387 static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
3388 {
3389 	unsigned char *pos;
3390 	size_t left;
3391 
3392 	pos = (unsigned char *)smp->data.u.str.area;
3393 	left = smp->data.u.str.data;
3394 
3395 	return protobuf_field_lookup(arg_p, smp, &pos, &left);
3396 }
3397 
sample_conv_protobuf_check(struct arg * args,struct sample_conv * conv,const char * file,int line,char ** err)3398 static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
3399                                       const char *file, int line, char **err)
3400 {
3401 	if (!args[1].type) {
3402 		args[1].type = ARGT_SINT;
3403 		args[1].data.sint = PBUF_T_BINARY;
3404 	}
3405 	else {
3406 		int pbuf_type;
3407 
3408 		pbuf_type = protobuf_type(args[1].data.str.area);
3409 		if (pbuf_type == -1) {
3410 			memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
3411 			return 0;
3412 		}
3413 
3414 		chunk_destroy(&args[1].data.str);
3415 		args[1].type = ARGT_SINT;
3416 		args[1].data.sint = pbuf_type;
3417 	}
3418 
3419 	return 1;
3420 }
3421 
3422 /*
3423  * Extract the tag value of an input binary sample. Takes a mandatory argument:
3424  * the FIX protocol tag identifier.
3425  * Return 1 if the tag was found, 0 if not.
3426  */
sample_conv_fix_tag_value(const struct arg * arg_p,struct sample * smp,void * private)3427 static int sample_conv_fix_tag_value(const struct arg *arg_p, struct sample *smp, void *private)
3428 {
3429 	struct ist value;
3430 
3431 	smp->flags &= ~SMP_F_MAY_CHANGE;
3432 	value = fix_tag_value(ist2(smp->data.u.str.area, smp->data.u.str.data),
3433 			      arg_p[0].data.sint);
3434 	if (!istlen(value)) {
3435 		if (isttest(value)) {
3436 			/* value != IST_NULL, need more data */
3437 			smp->flags |= SMP_F_MAY_CHANGE;
3438 		}
3439 		return 0;
3440 	}
3441 
3442 	smp->data.u.str = ist2buf(value);
3443 	smp->flags |= SMP_F_CONST;
3444 
3445 	return 1;
3446 }
3447 
3448 /* This function checks the "fix_tag_value" converter configuration.
3449  * It expects a "known" (by HAProxy) tag name or ID.
3450  * Tag string names are converted to their ID counterpart because this is the
3451  * format they are sent over the wire.
3452  */
sample_conv_fix_value_check(struct arg * args,struct sample_conv * conv,const char * file,int line,char ** err)3453 static int sample_conv_fix_value_check(struct arg *args, struct sample_conv *conv,
3454 				       const char *file, int line, char **err)
3455 {
3456 	struct ist str;
3457 	unsigned int tag;
3458 
3459 	str = ist2(args[0].data.str.area, args[0].data.str.data);
3460 	tag = fix_tagid(str);
3461 	if (!tag) {
3462 		memprintf(err, "Unknown FIX tag name '%s'", args[0].data.str.area);
3463 		return 0;
3464 	}
3465 
3466 	chunk_destroy(&args[0].data.str);
3467 	args[0].type = ARGT_SINT;
3468 	args[0].data.sint = tag;
3469 
3470 	return 1;
3471 }
3472 
3473 /*
3474  * Checks that a buffer contains a valid FIX message
3475  *
3476  * Return 1 if the check could be run, 0 if not.
3477  * The result of the analyse itself is stored in <smp> as a boolean
3478  */
sample_conv_fix_is_valid(const struct arg * arg_p,struct sample * smp,void * private)3479 static int sample_conv_fix_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3480 {
3481 	struct ist msg;
3482 
3483 	msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3484 
3485 	smp->flags &= ~SMP_F_MAY_CHANGE;
3486 	switch (fix_validate_message(msg)) {
3487 	case FIX_VALID_MESSAGE:
3488 		smp->data.type = SMP_T_BOOL;
3489 		smp->data.u.sint = 1;
3490 		return 1;
3491 	case FIX_NEED_MORE_DATA:
3492 		smp->flags |= SMP_F_MAY_CHANGE;
3493 		return 0;
3494 	case FIX_INVALID_MESSAGE:
3495 		smp->data.type = SMP_T_BOOL;
3496 		smp->data.u.sint = 0;
3497 		return 1;
3498 	}
3499 	return 0;
3500 }
3501 
3502 /*
3503  * Extract the field value of an input binary sample containing an MQTT packet.
3504  * Takes 2 mandatory arguments:
3505  * - packet type
3506  * - field name
3507  *
3508  * return 1 if the field was found, 0 if not.
3509  */
sample_conv_mqtt_field_value(const struct arg * arg_p,struct sample * smp,void * private)3510 static int sample_conv_mqtt_field_value(const struct arg *arg_p, struct sample *smp, void *private)
3511 {
3512 	struct ist pkt, value;
3513 	int type, fieldname_id;
3514 
3515 	pkt = ist2(smp->data.u.str.area, smp->data.u.str.data);
3516 	type = arg_p[0].data.sint;
3517 	fieldname_id = arg_p[1].data.sint;
3518 
3519 	smp->flags &= ~SMP_F_MAY_CHANGE;
3520 	value = mqtt_field_value(pkt, type, fieldname_id);
3521 	if (!istlen(value)) {
3522 		if (isttest(value)) {
3523 			/* value != IST_NULL, need more data */
3524 			smp->flags |= SMP_F_MAY_CHANGE;
3525 		}
3526 		return 0;
3527 	}
3528 
3529 	smp->data.u.str = ist2buf(value);
3530 	smp->flags |= SMP_F_CONST;
3531 	return 1;
3532 }
3533 
3534 /*
3535  * this function checks the "mqtt_field_value" converter configuration.
3536  * It expects a known packet type name or ID and a field name, in this order
3537  *
3538  * Args[0] will be turned into a MQTT_CPT_* value for direct matching when parsing
3539  * a packet.
3540  */
sample_conv_mqtt_field_value_check(struct arg * args,struct sample_conv * conv,const char * file,int line,char ** err)3541 static int sample_conv_mqtt_field_value_check(struct arg *args, struct sample_conv *conv,
3542 					      const char *file, int line, char **err)
3543 {
3544 	int type, fieldname_id;
3545 
3546 	/* check the MQTT packet type is valid */
3547 	type = mqtt_typeid(ist2(args[0].data.str.area, args[0].data.str.data));
3548 	if (type == MQTT_CPT_INVALID) {
3549 		memprintf(err, "Unknown MQTT type '%s'", args[0].data.str.area);
3550 		return 0;
3551 	}
3552 
3553 	/* check the field name belongs to the MQTT packet type */
3554 	fieldname_id = mqtt_check_type_fieldname(type, ist2(args[1].data.str.area, args[1].data.str.data));
3555 	if (fieldname_id == MQTT_FN_INVALID) {
3556 		memprintf(err, "Unknown MQTT field name '%s' for packet type '%s'", args[1].data.str.area,
3557 			  args[0].data.str.area);
3558 		return 0;
3559 	}
3560 
3561 	/* save numeric counterparts of type and field name */
3562 	chunk_destroy(&args[0].data.str);
3563 	chunk_destroy(&args[1].data.str);
3564 	args[0].type = ARGT_SINT;
3565 	args[0].data.sint = type;
3566 	args[1].type = ARGT_SINT;
3567 	args[1].data.sint = fieldname_id;
3568 
3569 	return 1;
3570 }
3571 
3572 /*
3573  * Checks that <smp> contains a valid MQTT message
3574  *
3575  * The function returns 1 if the check was run to its end, 0 otherwise.
3576  * The result of the analyse itself is stored in <smp> as a boolean.
3577  */
sample_conv_mqtt_is_valid(const struct arg * arg_p,struct sample * smp,void * private)3578 static int sample_conv_mqtt_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3579 {
3580 	struct ist msg;
3581 
3582 	msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3583 
3584 	smp->flags &= ~SMP_F_MAY_CHANGE;
3585 	switch (mqtt_validate_message(msg, NULL)) {
3586 	case FIX_VALID_MESSAGE:
3587 		smp->data.type = SMP_T_BOOL;
3588 		smp->data.u.sint = 1;
3589 		return 1;
3590 	case FIX_NEED_MORE_DATA:
3591 		smp->flags |= SMP_F_MAY_CHANGE;
3592 		return 0;
3593 	case FIX_INVALID_MESSAGE:
3594 		smp->data.type = SMP_T_BOOL;
3595 		smp->data.u.sint = 0;
3596 		return 1;
3597 	}
3598 	return 0;
3599 }
3600 
3601 /* This function checks the "strcmp" converter's arguments and extracts the
3602  * variable name and its scope.
3603  */
smp_check_strcmp(struct arg * args,struct sample_conv * conv,const char * file,int line,char ** err)3604 static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
3605                            const char *file, int line, char **err)
3606 {
3607 	if (!args[0].data.str.data) {
3608 		memprintf(err, "missing variable name");
3609 		return 0;
3610 	}
3611 
3612 	/* Try to decode a variable. */
3613 	if (vars_check_arg(&args[0], NULL))
3614 		return 1;
3615 
3616 	memprintf(err, "failed to register variable name '%s'",
3617 		  args[0].data.str.area);
3618 	return 0;
3619 }
3620 
3621 #if defined(HAVE_CRYPTO_memcmp)
3622 /* This function checks the "secure_memcmp" converter's arguments and extracts the
3623  * variable name and its scope.
3624  */
smp_check_secure_memcmp(struct arg * args,struct sample_conv * conv,const char * file,int line,char ** err)3625 static int smp_check_secure_memcmp(struct arg *args, struct sample_conv *conv,
3626                            const char *file, int line, char **err)
3627 {
3628 	if (!args[0].data.str.data) {
3629 		memprintf(err, "missing variable name");
3630 		return 0;
3631 	}
3632 
3633 	/* Try to decode a variable. */
3634 	if (vars_check_arg(&args[0], NULL))
3635 		return 1;
3636 
3637 	memprintf(err, "failed to register variable name '%s'",
3638 		  args[0].data.str.area);
3639 	return 0;
3640 }
3641 #endif
3642 
3643 /**/
sample_conv_htonl(const struct arg * arg_p,struct sample * smp,void * private)3644 static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private)
3645 {
3646 	struct buffer *tmp;
3647 	uint32_t n;
3648 
3649 	n = htonl((uint32_t)smp->data.u.sint);
3650 	tmp = get_trash_chunk();
3651 
3652 	memcpy(b_head(tmp), &n, 4);
3653 	b_add(tmp, 4);
3654 
3655 	smp->data.u.str = *tmp;
3656 	smp->data.type = SMP_T_BIN;
3657 	return 1;
3658 }
3659 
3660 /**/
sample_conv_cut_crlf(const struct arg * arg_p,struct sample * smp,void * private)3661 static int sample_conv_cut_crlf(const struct arg *arg_p, struct sample *smp, void *private)
3662 {
3663 	char *p;
3664 	size_t l;
3665 
3666 	p = smp->data.u.str.area;
3667 	for (l = 0; l < smp->data.u.str.data; l++) {
3668 		if (*(p+l) == '\r' || *(p+l) == '\n')
3669 			break;
3670 	}
3671 	smp->data.u.str.data = l;
3672 	return 1;
3673 }
3674 
3675 /**/
sample_conv_ltrim(const struct arg * arg_p,struct sample * smp,void * private)3676 static int sample_conv_ltrim(const struct arg *arg_p, struct sample *smp, void *private)
3677 {
3678 	char *delimiters, *p;
3679 	size_t dlen, l;
3680 
3681 	delimiters =  arg_p[0].data.str.area;
3682 	dlen = arg_p[0].data.str.data;
3683 
3684 	l = smp->data.u.str.data;
3685 	p = smp->data.u.str.area;
3686 	while (l && memchr(delimiters, *p, dlen) != NULL) {
3687 		p++;
3688 		l--;
3689 	}
3690 
3691 	smp->data.u.str.area = p;
3692 	smp->data.u.str.data = l;
3693 	return 1;
3694 }
3695 
3696 /**/
sample_conv_rtrim(const struct arg * arg_p,struct sample * smp,void * private)3697 static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *private)
3698 {
3699 	char *delimiters, *p;
3700 	size_t dlen, l;
3701 
3702 	delimiters =  arg_p[0].data.str.area;
3703 	dlen = arg_p[0].data.str.data;
3704 
3705 	l = smp->data.u.str.data;
3706 	p = smp->data.u.str.area + l - 1;
3707 	while (l && memchr(delimiters, *p, dlen) != NULL) {
3708 		p--;
3709 		l--;
3710 	}
3711 
3712 	smp->data.u.str.data = l;
3713 	return 1;
3714 }
3715 
3716 /* This function checks the "json_query" converter's arguments. */
sample_check_json_query(struct arg * arg,struct sample_conv * conv,const char * file,int line,char ** err)3717 static int sample_check_json_query(struct arg *arg, struct sample_conv *conv,
3718                            const char *file, int line, char **err)
3719 {
3720 	if (arg[0].data.str.data == 0) {
3721 		memprintf(err, "json_path must not be empty");
3722 		return 0;
3723 	}
3724 
3725 	if (arg[1].data.str.data != 0) {
3726 		if (strcmp(arg[1].data.str.area, "int") != 0) {
3727 			memprintf(err, "output_type only supports \"int\" as argument");
3728 			return 0;
3729 		} else {
3730 			arg[1].type = ARGT_SINT;
3731 			arg[1].data.sint = 0;
3732 		}
3733 	}
3734 	return 1;
3735 }
3736 
3737 /* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
3738  * the recommendation for interoperable integers in section 6 of RFC 7159.
3739  */
3740 #define JSON_INT_MAX ((1LL << 53) - 1)
3741 #define JSON_INT_MIN (-JSON_INT_MAX)
3742 
3743 /* This sample function get the value from a given json string.
3744  * The mjson library is used to parse the JSON struct
3745  */
sample_conv_json_query(const struct arg * args,struct sample * smp,void * private)3746 static int sample_conv_json_query(const struct arg *args, struct sample *smp, void *private)
3747 {
3748 	struct buffer *trash = get_trash_chunk();
3749 	const char *token; /* holds the temporary string from mjson_find */
3750 	int token_size;    /* holds the length of <token> */
3751 
3752 	enum mjson_tok token_type;
3753 
3754 	token_type = mjson_find(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &token, &token_size);
3755 
3756 	switch (token_type) {
3757 		case MJSON_TOK_NUMBER:
3758 			if (args[1].type == ARGT_SINT) {
3759 				smp->data.u.sint = strtoll(token, NULL, 0);
3760 
3761 				if (smp->data.u.sint < JSON_INT_MIN || smp->data.u.sint > JSON_INT_MAX)
3762 					return 0;
3763 
3764 				smp->data.type = SMP_T_SINT;
3765 
3766 				return 1;
3767 			} else {
3768 				double double_val;
3769 
3770 				if (mjson_get_number(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &double_val) == 0)
3771 					return 0;
3772 
3773 				trash->data = snprintf(trash->area,trash->size,"%g",double_val);
3774 				smp->data.u.str = *trash;
3775 				smp->data.type = SMP_T_STR;
3776 
3777 				return 1;
3778 			}
3779 		case MJSON_TOK_TRUE:
3780 			smp->data.type = SMP_T_BOOL;
3781 			smp->data.u.sint = 1;
3782 
3783 			return 1;
3784 		case MJSON_TOK_FALSE:
3785 			smp->data.type = SMP_T_BOOL;
3786 			smp->data.u.sint = 0;
3787 
3788 			return 1;
3789 		case MJSON_TOK_STRING: {
3790 			int len;
3791 
3792 			len = mjson_get_string(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, trash->area, trash->size);
3793 
3794 			if (len == -1) {
3795 				/* invalid string */
3796 				return 0;
3797 			}
3798 
3799 			trash->data = len;
3800 			smp->data.u.str = *trash;
3801 			smp->data.type = SMP_T_STR;
3802 
3803 			return 1;
3804 		}
3805 		case MJSON_TOK_NULL:
3806 		case MJSON_TOK_ARRAY:
3807 		case MJSON_TOK_OBJECT:
3808 			/* We cannot handle these. */
3809 			return 0;
3810 		case MJSON_TOK_INVALID:
3811 			/* Nothing matches the query. */
3812 			return 0;
3813 		case MJSON_TOK_KEY:
3814 			/* This is not a valid return value according to the
3815 			 * mjson documentation, but we handle it to benefit
3816 			 * from '-Wswitch'.
3817 			 */
3818 			return 0;
3819 	}
3820 
3821 	my_unreachable();
3822 	return 0;
3823 }
3824 
3825 
3826 /************************************************************************/
3827 /*       All supported sample fetch functions must be declared here     */
3828 /************************************************************************/
3829 
3830 /* force TRUE to be returned at the fetch level */
3831 static int
smp_fetch_true(const struct arg * args,struct sample * smp,const char * kw,void * private)3832 smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
3833 {
3834 	if (!smp_make_rw(smp))
3835 		return 0;
3836 
3837 	smp->data.type = SMP_T_BOOL;
3838 	smp->data.u.sint = 1;
3839 	return 1;
3840 }
3841 
3842 /* force FALSE to be returned at the fetch level */
3843 static int
smp_fetch_false(const struct arg * args,struct sample * smp,const char * kw,void * private)3844 smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
3845 {
3846 	smp->data.type = SMP_T_BOOL;
3847 	smp->data.u.sint = 0;
3848 	return 1;
3849 }
3850 
3851 /* retrieve environment variable $1 as a string */
3852 static int
smp_fetch_env(const struct arg * args,struct sample * smp,const char * kw,void * private)3853 smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
3854 {
3855 	char *env;
3856 
3857 	if (args[0].type != ARGT_STR)
3858 		return 0;
3859 
3860 	env = getenv(args[0].data.str.area);
3861 	if (!env)
3862 		return 0;
3863 
3864 	smp->data.type = SMP_T_STR;
3865 	smp->flags = SMP_F_CONST;
3866 	smp->data.u.str.area = env;
3867 	smp->data.u.str.data = strlen(env);
3868 	return 1;
3869 }
3870 
3871 /* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3872  * optional string representing the unit of the result: "s" for seconds, "ms" for
3873  * milliseconds and "us" for microseconds.
3874  * Returns 0 on error and non-zero if OK.
3875  */
smp_check_date_unit(struct arg * args,char ** err)3876 int smp_check_date_unit(struct arg *args, char **err)
3877 {
3878         if (args[1].type == ARGT_STR) {
3879 		long long int unit;
3880 
3881                 if (strcmp(args[1].data.str.area, "s") == 0) {
3882                         unit = TIME_UNIT_S;
3883                 }
3884                 else if (strcmp(args[1].data.str.area, "ms") == 0) {
3885                         unit = TIME_UNIT_MS;
3886                 }
3887                 else if (strcmp(args[1].data.str.area, "us") == 0) {
3888                         unit = TIME_UNIT_US;
3889                 }
3890                 else {
3891                         memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
3892                                   args[1].data.str.area);
3893                         return 0;
3894                 }
3895 
3896 		chunk_destroy(&args[1].data.str);
3897                 args[1].type = ARGT_SINT;
3898 		args[1].data.sint = unit;
3899         }
3900         else if (args[1].type != ARGT_STOP) {
3901                 memprintf(err, "Unexpected arg type");
3902                 return 0;
3903         }
3904 
3905         return 1;
3906 }
3907 
3908 /* retrieve the current local date in epoch time, converts it to milliseconds
3909  * or microseconds if asked to in optional args[1] unit param, and applies an
3910  * optional args[0] offset.
3911  */
3912 static int
smp_fetch_date(const struct arg * args,struct sample * smp,const char * kw,void * private)3913 smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
3914 {
3915 	smp->data.u.sint = date.tv_sec;
3916 
3917 	/* report in milliseconds */
3918 	if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
3919 		smp->data.u.sint *= 1000;
3920 		smp->data.u.sint += date.tv_usec / 1000;
3921 	}
3922 	/* report in microseconds */
3923 	else if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
3924 		smp->data.u.sint *= 1000000;
3925 		smp->data.u.sint += date.tv_usec;
3926 	}
3927 
3928 	/* add offset */
3929 	if (args[0].type == ARGT_SINT)
3930 		smp->data.u.sint += args[0].data.sint;
3931 
3932 	smp->data.type = SMP_T_SINT;
3933 	smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3934 	return 1;
3935 }
3936 
3937 /* retrieve the current microsecond part of the date  */
3938 static int
smp_fetch_date_us(const struct arg * args,struct sample * smp,const char * kw,void * private)3939 smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3940 {
3941 	smp->data.u.sint = date.tv_usec;
3942 	smp->data.type = SMP_T_SINT;
3943 	smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3944 	return 1;
3945 }
3946 
3947 
3948 /* returns the hostname */
3949 static int
smp_fetch_hostname(const struct arg * args,struct sample * smp,const char * kw,void * private)3950 smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3951 {
3952 	smp->data.type = SMP_T_STR;
3953 	smp->flags = SMP_F_CONST;
3954 	smp->data.u.str.area = hostname;
3955 	smp->data.u.str.data = strlen(hostname);
3956 	return 1;
3957 }
3958 
3959 /* returns the number of processes */
3960 static int
smp_fetch_nbproc(const struct arg * args,struct sample * smp,const char * kw,void * private)3961 smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
3962 {
3963 	smp->data.type = SMP_T_SINT;
3964 	smp->data.u.sint = global.nbproc;
3965 	return 1;
3966 }
3967 
3968 /* returns the number of the current process (between 1 and nbproc */
3969 static int
smp_fetch_proc(const struct arg * args,struct sample * smp,const char * kw,void * private)3970 smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
3971 {
3972 	smp->data.type = SMP_T_SINT;
3973 	smp->data.u.sint = relative_pid;
3974 	return 1;
3975 }
3976 
3977 /* returns the number of the current thread (between 1 and nbthread */
3978 static int
smp_fetch_thread(const struct arg * args,struct sample * smp,const char * kw,void * private)3979 smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3980 {
3981 	smp->data.type = SMP_T_SINT;
3982 	smp->data.u.sint = tid;
3983 	return 1;
3984 }
3985 
3986 /* generate a random 32-bit integer for whatever purpose, with an optional
3987  * range specified in argument.
3988  */
3989 static int
smp_fetch_rand(const struct arg * args,struct sample * smp,const char * kw,void * private)3990 smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
3991 {
3992 	smp->data.u.sint = ha_random32();
3993 
3994 	/* reduce if needed. Don't do a modulo, use all bits! */
3995 	if (args[0].type == ARGT_SINT)
3996 		smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32;
3997 
3998 	smp->data.type = SMP_T_SINT;
3999 	smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4000 	return 1;
4001 }
4002 
4003 /* returns true if the current process is stopping */
4004 static int
smp_fetch_stopping(const struct arg * args,struct sample * smp,const char * kw,void * private)4005 smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
4006 {
4007 	smp->data.type = SMP_T_BOOL;
4008 	smp->data.u.sint = stopping;
4009 	return 1;
4010 }
4011 
4012 /* returns the number of calls of the current stream's process_stream() */
4013 static int
smp_fetch_cpu_calls(const struct arg * args,struct sample * smp,const char * kw,void * private)4014 smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
4015 {
4016 	if (!smp->strm)
4017 		return 0;
4018 
4019 	smp->data.type = SMP_T_SINT;
4020 	smp->data.u.sint = smp->strm->task->calls;
4021 	return 1;
4022 }
4023 
4024 /* returns the average number of nanoseconds spent processing the stream per call */
4025 static int
smp_fetch_cpu_ns_avg(const struct arg * args,struct sample * smp,const char * kw,void * private)4026 smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
4027 {
4028 	if (!smp->strm)
4029 		return 0;
4030 
4031 	smp->data.type = SMP_T_SINT;
4032 	smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
4033 	return 1;
4034 }
4035 
4036 /* returns the total number of nanoseconds spent processing the stream */
4037 static int
smp_fetch_cpu_ns_tot(const struct arg * args,struct sample * smp,const char * kw,void * private)4038 smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
4039 {
4040 	if (!smp->strm)
4041 		return 0;
4042 
4043 	smp->data.type = SMP_T_SINT;
4044 	smp->data.u.sint = smp->strm->task->cpu_time;
4045 	return 1;
4046 }
4047 
4048 /* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
4049 static int
smp_fetch_lat_ns_avg(const struct arg * args,struct sample * smp,const char * kw,void * private)4050 smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
4051 {
4052 	if (!smp->strm)
4053 		return 0;
4054 
4055 	smp->data.type = SMP_T_SINT;
4056 	smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
4057 	return 1;
4058 }
4059 
4060 /* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
4061 static int
smp_fetch_lat_ns_tot(const struct arg * args,struct sample * smp,const char * kw,void * private)4062 smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
4063 {
4064 	if (!smp->strm)
4065 		return 0;
4066 
4067 	smp->data.type = SMP_T_SINT;
4068 	smp->data.u.sint = smp->strm->task->lat_time;
4069 	return 1;
4070 }
4071 
smp_fetch_const_str(const struct arg * args,struct sample * smp,const char * kw,void * private)4072 static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
4073 {
4074 	smp->flags |= SMP_F_CONST;
4075 	smp->data.type = SMP_T_STR;
4076 	smp->data.u.str.area = args[0].data.str.area;
4077 	smp->data.u.str.data = args[0].data.str.data;
4078 	return 1;
4079 }
4080 
smp_check_const_bool(struct arg * args,char ** err)4081 static int smp_check_const_bool(struct arg *args, char **err)
4082 {
4083 	if (strcasecmp(args[0].data.str.area, "true") == 0 ||
4084 	    strcasecmp(args[0].data.str.area, "1") == 0) {
4085 		chunk_destroy(&args[0].data.str);
4086 		args[0].type = ARGT_SINT;
4087 		args[0].data.sint = 1;
4088 		return 1;
4089 	}
4090 	if (strcasecmp(args[0].data.str.area, "false") == 0 ||
4091 	    strcasecmp(args[0].data.str.area, "0") == 0) {
4092 		chunk_destroy(&args[0].data.str);
4093 		args[0].type = ARGT_SINT;
4094 		args[0].data.sint = 0;
4095 		return 1;
4096 	}
4097 	memprintf(err, "Expects 'true', 'false', '0' or '1'");
4098 	return 0;
4099 }
4100 
smp_fetch_const_bool(const struct arg * args,struct sample * smp,const char * kw,void * private)4101 static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
4102 {
4103 	smp->data.type = SMP_T_BOOL;
4104 	smp->data.u.sint = args[0].data.sint;
4105 	return 1;
4106 }
4107 
smp_fetch_const_int(const struct arg * args,struct sample * smp,const char * kw,void * private)4108 static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
4109 {
4110 	smp->data.type = SMP_T_SINT;
4111 	smp->data.u.sint = args[0].data.sint;
4112 	return 1;
4113 }
4114 
smp_fetch_const_ipv4(const struct arg * args,struct sample * smp,const char * kw,void * private)4115 static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
4116 {
4117 	smp->data.type = SMP_T_IPV4;
4118 	smp->data.u.ipv4 = args[0].data.ipv4;
4119 	return 1;
4120 }
4121 
smp_fetch_const_ipv6(const struct arg * args,struct sample * smp,const char * kw,void * private)4122 static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
4123 {
4124 	smp->data.type = SMP_T_IPV6;
4125 	smp->data.u.ipv6 = args[0].data.ipv6;
4126 	return 1;
4127 }
4128 
smp_check_const_bin(struct arg * args,char ** err)4129 static int smp_check_const_bin(struct arg *args, char **err)
4130 {
4131 	char *binstr = NULL;
4132 	int binstrlen;
4133 
4134 	if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
4135 		return 0;
4136 	chunk_destroy(&args[0].data.str);
4137 	args[0].type = ARGT_STR;
4138 	args[0].data.str.area = binstr;
4139 	args[0].data.str.data = binstrlen;
4140 	return 1;
4141 }
4142 
smp_fetch_const_bin(const struct arg * args,struct sample * smp,const char * kw,void * private)4143 static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
4144 {
4145 	smp->flags |= SMP_F_CONST;
4146 	smp->data.type = SMP_T_BIN;
4147 	smp->data.u.str.area = args[0].data.str.area;
4148 	smp->data.u.str.data = args[0].data.str.data;
4149 	return 1;
4150 }
4151 
smp_check_const_meth(struct arg * args,char ** err)4152 static int smp_check_const_meth(struct arg *args, char **err)
4153 {
4154 	enum http_meth_t meth;
4155 	int i;
4156 
4157 	meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
4158 	if (meth != HTTP_METH_OTHER) {
4159 		chunk_destroy(&args[0].data.str);
4160 		args[0].type = ARGT_SINT;
4161 		args[0].data.sint = meth;
4162 	} else {
4163 		/* Check method avalaibility. A method is a token defined as :
4164 		 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
4165 		 *         "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
4166 		 * token = 1*tchar
4167 		 */
4168 		for (i = 0; i < args[0].data.str.data; i++) {
4169 			if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
4170 				memprintf(err, "expects valid method.");
4171 				return 0;
4172 			}
4173 		}
4174 	}
4175 	return 1;
4176 }
4177 
smp_fetch_const_meth(const struct arg * args,struct sample * smp,const char * kw,void * private)4178 static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
4179 {
4180 	smp->data.type = SMP_T_METH;
4181 	if (args[0].type == ARGT_SINT) {
4182 		smp->flags &= ~SMP_F_CONST;
4183 		smp->data.u.meth.meth = args[0].data.sint;
4184 		smp->data.u.meth.str.area = "";
4185 		smp->data.u.meth.str.data = 0;
4186 	} else {
4187 		smp->flags |= SMP_F_CONST;
4188 		smp->data.u.meth.meth = HTTP_METH_OTHER;
4189 		smp->data.u.meth.str.area = args[0].data.str.area;
4190 		smp->data.u.meth.str.data = args[0].data.str.data;
4191 	}
4192 	return 1;
4193 }
4194 
4195 // This function checks the "uuid" sample's arguments.
4196 // Function won't get called when no parameter is specified (maybe a bug?)
smp_check_uuid(struct arg * args,char ** err)4197 static int smp_check_uuid(struct arg *args, char **err)
4198 {
4199 	if (!args[0].type) {
4200 		args[0].type = ARGT_SINT;
4201 		args[0].data.sint = 4;
4202 	}
4203 	else if (args[0].data.sint != 4) {
4204 		memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
4205 		return 0;
4206 	}
4207 
4208 	return 1;
4209 }
4210 
4211 // Generate a RFC4122 UUID (default is v4 = fully random)
smp_fetch_uuid(const struct arg * args,struct sample * smp,const char * kw,void * private)4212 static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
4213 {
4214 	if (args[0].data.sint == 4 || !args[0].type) {
4215 		ha_generate_uuid(&trash);
4216 		smp->data.type = SMP_T_STR;
4217 		smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4218 		smp->data.u.str = trash;
4219 		return 1;
4220 	}
4221 
4222 	// more implementations of other uuid formats possible here
4223 	return 0;
4224 }
4225 
4226 /* Note: must not be declared <const> as its list will be overwritten.
4227  * Note: fetches that may return multiple types must be declared as the lowest
4228  * common denominator, the type that can be casted into all other ones. For
4229  * instance IPv4/IPv6 must be declared IPv4.
4230  */
4231 static struct sample_fetch_kw_list smp_kws = {ILH, {
4232 	{ "always_false", smp_fetch_false, 0,            NULL, SMP_T_BOOL, SMP_USE_CONST },
4233 	{ "always_true",  smp_fetch_true,  0,            NULL, SMP_T_BOOL, SMP_USE_CONST },
4234 	{ "env",          smp_fetch_env,   ARG1(1,STR),  NULL, SMP_T_STR,  SMP_USE_CONST },
4235 	{ "date",         smp_fetch_date,  ARG2(0,SINT,STR), smp_check_date_unit, SMP_T_SINT, SMP_USE_CONST },
4236 	{ "date_us",      smp_fetch_date_us,  0,         NULL, SMP_T_SINT, SMP_USE_CONST },
4237 	{ "hostname",     smp_fetch_hostname, 0,         NULL, SMP_T_STR,  SMP_USE_CONST },
4238 	{ "nbproc",       smp_fetch_nbproc,0,            NULL, SMP_T_SINT, SMP_USE_CONST },
4239 	{ "proc",         smp_fetch_proc,  0,            NULL, SMP_T_SINT, SMP_USE_CONST },
4240 	{ "thread",       smp_fetch_thread,  0,          NULL, SMP_T_SINT, SMP_USE_CONST },
4241 	{ "rand",         smp_fetch_rand,  ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_CONST },
4242 	{ "stopping",     smp_fetch_stopping, 0,         NULL, SMP_T_BOOL, SMP_USE_INTRN },
4243 	{ "uuid",         smp_fetch_uuid,  ARG1(0, SINT),      smp_check_uuid, SMP_T_STR, SMP_USE_CONST },
4244 
4245 	{ "cpu_calls",    smp_fetch_cpu_calls,  0,       NULL, SMP_T_SINT, SMP_USE_INTRN },
4246 	{ "cpu_ns_avg",   smp_fetch_cpu_ns_avg, 0,       NULL, SMP_T_SINT, SMP_USE_INTRN },
4247 	{ "cpu_ns_tot",   smp_fetch_cpu_ns_tot, 0,       NULL, SMP_T_SINT, SMP_USE_INTRN },
4248 	{ "lat_ns_avg",   smp_fetch_lat_ns_avg, 0,       NULL, SMP_T_SINT, SMP_USE_INTRN },
4249 	{ "lat_ns_tot",   smp_fetch_lat_ns_tot, 0,       NULL, SMP_T_SINT, SMP_USE_INTRN },
4250 
4251 	{ "str",  smp_fetch_const_str,  ARG1(1,STR),  NULL                , SMP_T_STR,  SMP_USE_CONST },
4252 	{ "bool", smp_fetch_const_bool, ARG1(1,STR),  smp_check_const_bool, SMP_T_BOOL, SMP_USE_CONST },
4253 	{ "int",  smp_fetch_const_int,  ARG1(1,SINT), NULL                , SMP_T_SINT, SMP_USE_CONST },
4254 	{ "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL                , SMP_T_IPV4, SMP_USE_CONST },
4255 	{ "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL                , SMP_T_IPV6, SMP_USE_CONST },
4256 	{ "bin",  smp_fetch_const_bin,  ARG1(1,STR),  smp_check_const_bin , SMP_T_BIN,  SMP_USE_CONST },
4257 	{ "meth", smp_fetch_const_meth, ARG1(1,STR),  smp_check_const_meth, SMP_T_METH, SMP_USE_CONST },
4258 
4259 	{ /* END */ },
4260 }};
4261 
4262 INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
4263 
4264 /* Note: must not be declared <const> as its list will be overwritten */
4265 static struct sample_conv_kw_list sample_conv_kws = {ILH, {
4266 	{ "debug",   sample_conv_debug,        ARG2(0,STR,STR),       smp_check_debug,          SMP_T_ANY,  SMP_T_ANY  },
4267 	{ "b64dec",  sample_conv_base642bin,   0,                     NULL,                     SMP_T_STR,  SMP_T_BIN  },
4268 	{ "base64",  sample_conv_bin2base64,   0,                     NULL,                     SMP_T_BIN,  SMP_T_STR  },
4269 	{ "ub64enc", sample_conv_bin2base64url,0,                     NULL,                     SMP_T_BIN,  SMP_T_STR  },
4270 	{ "ub64dec", sample_conv_base64url2bin,0,                     NULL,                     SMP_T_STR,  SMP_T_BIN  },
4271 	{ "upper",   sample_conv_str2upper,    0,                     NULL,                     SMP_T_STR,  SMP_T_STR  },
4272 	{ "lower",   sample_conv_str2lower,    0,                     NULL,                     SMP_T_STR,  SMP_T_STR  },
4273 	{ "length",  sample_conv_length,       0,                     NULL,                     SMP_T_STR,  SMP_T_SINT },
4274 	{ "hex",     sample_conv_bin2hex,      0,                     NULL,                     SMP_T_BIN,  SMP_T_STR  },
4275 	{ "hex2i",   sample_conv_hex2int,      0,                     NULL,                     SMP_T_STR,  SMP_T_SINT },
4276 	{ "ipmask",  sample_conv_ipmask,       ARG2(1,MSK4,MSK6),     NULL,                     SMP_T_ADDR, SMP_T_IPV4 },
4277 	{ "ltime",   sample_conv_ltime,        ARG2(1,STR,SINT),      NULL,                     SMP_T_SINT, SMP_T_STR  },
4278 	{ "utime",   sample_conv_utime,        ARG2(1,STR,SINT),      NULL,                     SMP_T_SINT, SMP_T_STR  },
4279 	{ "crc32",   sample_conv_crc32,        ARG1(0,SINT),          NULL,                     SMP_T_BIN,  SMP_T_SINT },
4280 	{ "crc32c",  sample_conv_crc32c,       ARG1(0,SINT),          NULL,                     SMP_T_BIN,  SMP_T_SINT },
4281 	{ "djb2",    sample_conv_djb2,         ARG1(0,SINT),          NULL,                     SMP_T_BIN,  SMP_T_SINT },
4282 	{ "sdbm",    sample_conv_sdbm,         ARG1(0,SINT),          NULL,                     SMP_T_BIN,  SMP_T_SINT },
4283 	{ "wt6",     sample_conv_wt6,          ARG1(0,SINT),          NULL,                     SMP_T_BIN,  SMP_T_SINT },
4284 	{ "xxh3",    sample_conv_xxh3,         ARG1(0,SINT),          NULL,                     SMP_T_BIN,  SMP_T_SINT },
4285 	{ "xxh32",   sample_conv_xxh32,        ARG1(0,SINT),          NULL,                     SMP_T_BIN,  SMP_T_SINT },
4286 	{ "xxh64",   sample_conv_xxh64,        ARG1(0,SINT),          NULL,                     SMP_T_BIN,  SMP_T_SINT },
4287 	{ "json",    sample_conv_json,         ARG1(1,STR),           sample_conv_json_check,   SMP_T_STR,  SMP_T_STR  },
4288 	{ "bytes",   sample_conv_bytes,        ARG2(1,SINT,SINT),     NULL,                     SMP_T_BIN,  SMP_T_BIN  },
4289 	{ "field",   sample_conv_field,        ARG3(2,SINT,STR,SINT), sample_conv_field_check,  SMP_T_STR,  SMP_T_STR  },
4290 	{ "word",    sample_conv_word,         ARG3(2,SINT,STR,SINT), sample_conv_field_check,  SMP_T_STR,  SMP_T_STR  },
4291 	{ "regsub",  sample_conv_regsub,       ARG3(2,REG,STR,STR),   sample_conv_regsub_check, SMP_T_STR,  SMP_T_STR  },
4292 	{ "sha1",    sample_conv_sha1,         0,                     NULL,                     SMP_T_BIN,  SMP_T_BIN  },
4293 #ifdef USE_OPENSSL
4294 	{ "sha2",   sample_conv_sha2,      ARG1(0, SINT), smp_check_sha2, SMP_T_BIN,  SMP_T_BIN  },
4295 #ifdef EVP_CIPH_GCM_MODE
4296 	{ "aes_gcm_dec", sample_conv_aes_gcm_dec,   ARG4(4,SINT,STR,STR,STR), check_aes_gcm,       SMP_T_BIN, SMP_T_BIN },
4297 #endif
4298 	{ "digest",      sample_conv_crypto_digest, ARG1(1,STR),              check_crypto_digest, SMP_T_BIN, SMP_T_BIN },
4299 	{ "hmac",        sample_conv_crypto_hmac,   ARG2(2,STR,STR),          check_crypto_hmac,   SMP_T_BIN, SMP_T_BIN },
4300 #endif
4301 	{ "concat", sample_conv_concat,    ARG3(1,STR,STR,STR), smp_check_concat, SMP_T_STR,  SMP_T_STR },
4302 	{ "strcmp", sample_conv_strcmp,    ARG1(1,STR), smp_check_strcmp, SMP_T_STR,  SMP_T_SINT },
4303 #if defined(HAVE_CRYPTO_memcmp)
4304 	{ "secure_memcmp", sample_conv_secure_memcmp,    ARG1(1,STR), smp_check_secure_memcmp, SMP_T_BIN,  SMP_T_BOOL },
4305 #endif
4306 
4307 	/* gRPC converters. */
4308 	{ "ungrpc", sample_conv_ungrpc,    ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN  },
4309 	{ "protobuf", sample_conv_protobuf, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN  },
4310 
4311 	/* FIX converters */
4312 	{ "fix_is_valid",  sample_conv_fix_is_valid,  0,           NULL,                        SMP_T_BIN, SMP_T_BOOL  },
4313 	{ "fix_tag_value", sample_conv_fix_tag_value, ARG1(1,STR), sample_conv_fix_value_check, SMP_T_BIN, SMP_T_BIN  },
4314 
4315 	/* MQTT converters */
4316 	{ "mqtt_is_valid",    sample_conv_mqtt_is_valid,     0,               NULL,                               SMP_T_BIN, SMP_T_BOOL },
4317 	{ "mqtt_field_value", sample_conv_mqtt_field_value,  ARG2(2,STR,STR), sample_conv_mqtt_field_value_check, SMP_T_BIN, SMP_T_STR },
4318 
4319 	{ "iif", sample_conv_iif, ARG2(2, STR, STR), NULL, SMP_T_BOOL, SMP_T_STR },
4320 
4321 	{ "and",    sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT  },
4322 	{ "or",     sample_conv_binary_or,  ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT  },
4323 	{ "xor",    sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT  },
4324 	{ "cpl",    sample_conv_binary_cpl,           0, NULL, SMP_T_SINT, SMP_T_SINT  },
4325 	{ "bool",   sample_conv_arith_bool,           0, NULL, SMP_T_SINT, SMP_T_BOOL },
4326 	{ "not",    sample_conv_arith_not,            0, NULL, SMP_T_SINT, SMP_T_BOOL },
4327 	{ "odd",    sample_conv_arith_odd,            0, NULL, SMP_T_SINT, SMP_T_BOOL },
4328 	{ "even",   sample_conv_arith_even,           0, NULL, SMP_T_SINT, SMP_T_BOOL },
4329 	{ "add",    sample_conv_arith_add,  ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT  },
4330 	{ "sub",    sample_conv_arith_sub,  ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT  },
4331 	{ "mul",    sample_conv_arith_mul,  ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT  },
4332 	{ "div",    sample_conv_arith_div,  ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT  },
4333 	{ "mod",    sample_conv_arith_mod,  ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT  },
4334 	{ "neg",    sample_conv_arith_neg,            0, NULL, SMP_T_SINT, SMP_T_SINT  },
4335 
4336 	{ "htonl",    sample_conv_htonl,              0, NULL, SMP_T_SINT, SMP_T_BIN  },
4337 	{ "cut_crlf", sample_conv_cut_crlf,           0, NULL, SMP_T_STR,  SMP_T_STR  },
4338 	{ "ltrim",    sample_conv_ltrim,    ARG1(1,STR), NULL, SMP_T_STR,  SMP_T_STR  },
4339 	{ "rtrim",    sample_conv_rtrim,    ARG1(1,STR), NULL, SMP_T_STR,  SMP_T_STR  },
4340 	{ "json_query", sample_conv_json_query, ARG2(1,STR,STR),  sample_check_json_query , SMP_T_STR, SMP_T_ANY },
4341 	{ NULL, NULL, 0, 0, 0 },
4342 }};
4343 
4344 INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);
4345