blob: 2a313f481f1a42f1534145cb27cff006766e01df [file] [log] [blame]
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +01001/*
2 * MAP management functions.
3 *
4 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010013#include <stdio.h>
Willy Tarreau97218ce2021-04-30 14:57:03 +020014#include <syslog.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010015
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020016#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <haproxy/applet-t.h>
18#include <haproxy/arg.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020019#include <haproxy/cli.h>
Christopher Faulet908628c2022-03-25 16:43:49 +010020#include <haproxy/conn_stream.h>
21#include <haproxy/cs_utils.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020022#include <haproxy/map.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020023#include <haproxy/pattern.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020024#include <haproxy/regex.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020025#include <haproxy/sample.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020026#include <haproxy/stats-t.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020027#include <haproxy/tools.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010028
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010029
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020030/* Parse an IPv4 or IPv6 address and store it into the sample.
31 * The output type is IPv4 or IPv6.
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010032 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020033int map_parse_ip(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010034{
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020035 int len = strlen(text);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010036
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020037 if (buf2ip(text, len, &data->u.ipv4)) {
38 data->type = SMP_T_IPV4;
39 return 1;
40 }
41 if (buf2ip6(text, len, &data->u.ipv6)) {
42 data->type = SMP_T_IPV6;
43 return 1;
44 }
45 return 0;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010046}
47
48/* Parse a string and store a pointer to it into the sample. The original
49 * string must be left in memory because we return a direct memory reference.
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010050 * The output type is SMP_T_STR. There is no risk that the data will be
51 * overwritten because sample_conv_map() makes a const sample with this
52 * output.
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010053 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020054int map_parse_str(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010055{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020056 data->u.str.area = (char *)text;
57 data->u.str.data = strlen(text);
58 data->u.str.size = data->u.str.data + 1;
Thierry FOURNIER503bb092015-08-19 08:35:43 +020059 data->type = SMP_T_STR;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010060 return 1;
61}
62
63/* Parse an integer and convert it to a sample. The output type is SINT if the
64 * number is negative, or UINT if it is positive or null. The function returns
65 * zero (error) if the number is too large.
66 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020067int map_parse_int(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010068{
Thierry FOURNIER503bb092015-08-19 08:35:43 +020069 data->type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020070 data->u.sint = read_int64(&text, text + strlen(text));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020071 if (*text != '\0')
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010072 return 0;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010073 return 1;
74}
75
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010076/* This crete and initialize map descriptor.
77 * Return NULL if out of memory error
78 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010079static struct map_descriptor *map_create_descriptor(struct sample_conv *conv)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010080{
81 struct map_descriptor *desc;
82
83 desc = calloc(1, sizeof(*desc));
84 if (!desc)
85 return NULL;
86
87 desc->conv = conv;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010088
89 return desc;
90}
91
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010092/* This function load the map file according with data type declared into
93 * the "struct sample_conv".
94 *
95 * This function choose the indexation type (ebtree or list) according with
96 * the type of match needed.
97 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +020098int sample_load_map(struct arg *arg, struct sample_conv *conv,
99 const char *file, int line, char **err)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100100{
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100101 struct map_descriptor *desc;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100102
Christopher Faulet0eb967d2020-08-05 23:23:37 +0200103 if (!(global.mode & MODE_STARTING)) {
104 memprintf(err, "map: cannot load map at runtime");
105 return 0;
106 }
107
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100108 /* create new map descriptor */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100109 desc = map_create_descriptor(conv);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100110 if (!desc) {
111 memprintf(err, "out of memory");
112 return 0;
113 }
114
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100115 /* Initialize pattern */
116 pattern_init_head(&desc->pat);
117
118 /* This is original pattern, must free */
119 desc->do_free = 1;
120
121 /* Set the match method. */
Thierry FOURNIER1edc9712014-12-15 16:18:39 +0100122 desc->pat.match = pat_match_fcts[(long)conv->private];
123 desc->pat.parse = pat_parse_fcts[(long)conv->private];
124 desc->pat.index = pat_index_fcts[(long)conv->private];
Thierry FOURNIER1edc9712014-12-15 16:18:39 +0100125 desc->pat.prune = pat_prune_fcts[(long)conv->private];
126 desc->pat.expect_type = pat_match_types[(long)conv->private];
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100127
128 /* Set the output parse method. */
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100129 switch (desc->conv->out_type) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100130 case SMP_T_STR: desc->pat.parse_smp = map_parse_str; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200131 case SMP_T_SINT: desc->pat.parse_smp = map_parse_int; break;
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200132 case SMP_T_ADDR: desc->pat.parse_smp = map_parse_ip; break;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100133 default:
134 memprintf(err, "map: internal haproxy error: no default parse case for the input type <%d>.",
135 conv->out_type);
Andreas Seltenreich78f35952016-03-03 20:32:23 +0100136 free(desc);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100137 return 0;
138 }
139
Thierry FOURNIER39bef452014-01-29 13:29:45 +0100140 /* Load map. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200141 if (!pattern_read_from_file(&desc->pat, PAT_REF_MAP, arg[0].data.str.area, PAT_MF_NO_DNS,
Thierry FOURNIER94580c92014-02-11 14:36:45 +0100142 1, err, file, line))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100143 return 0;
Thierry FOURNIER0ffe78c2013-12-05 14:40:25 +0100144
Willy Tarreauaa5801b2019-04-19 11:35:22 +0200145 /* the maps of type IP support a string as default value. This
146 * string can be an ipv4 or an ipv6, we must convert it.
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200147 */
Willy Tarreauaa5801b2019-04-19 11:35:22 +0200148 if (arg[1].type != ARGT_STOP && desc->conv->out_type == SMP_T_ADDR) {
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200149 struct sample_data data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200150 if (!map_parse_ip(arg[1].data.str.area, &data)) {
151 memprintf(err, "map: cannot parse default ip <%s>.",
152 arg[1].data.str.area);
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200153 return 0;
154 }
Christopher Faulet6ad7df42020-08-07 11:45:18 +0200155 chunk_destroy(&arg[1].data.str);
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200156 if (data.type == SMP_T_IPV4) {
157 arg[1].type = ARGT_IPV4;
158 arg[1].data.ipv4 = data.u.ipv4;
159 } else {
160 arg[1].type = ARGT_IPV6;
161 arg[1].data.ipv6 = data.u.ipv6;
162 }
163 }
164
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100165 /* replace the first argument by this definition */
Christopher Faulet6ad7df42020-08-07 11:45:18 +0200166 chunk_destroy(&arg[0].data.str);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100167 arg[0].type = ARGT_MAP;
168 arg[0].data.map = desc;
169
170 return 1;
171}
172
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200173static int sample_conv_map(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100174{
175 struct map_descriptor *desc;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100176 struct pattern *pat;
Willy Tarreau83061a82018-07-13 11:56:34 +0200177 struct buffer *str;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100178
179 /* get config */
180 desc = arg_p[0].data.map;
181
182 /* Execute the match function. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100183 pat = pattern_exec_match(&desc->pat, smp, 1);
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100184
185 /* Match case. */
186 if (pat) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200187 if (pat->data) {
Thierry Fournier8feaa662016-02-10 22:55:20 +0100188 /* In the regm case, merge the sample with the input. */
189 if ((long)private == PAT_MATCH_REGM) {
Emeric Brun27102212018-07-17 09:47:07 -0400190 struct buffer *tmptrash;
Willy Tarreau2842e052018-08-22 04:55:43 +0200191 int len;
Emeric Brun27102212018-07-17 09:47:07 -0400192
193 /* Copy the content of the sample because it could
194 be scratched by incoming get_trash_chunk */
195 tmptrash = alloc_trash_chunk();
196 if (!tmptrash)
197 return 0;
198
199 tmptrash->data = smp->data.u.str.data;
200 if (tmptrash->data > (tmptrash->size-1))
201 tmptrash->data = tmptrash->size-1;
202
203 memcpy(tmptrash->area, smp->data.u.str.area, tmptrash->data);
204 tmptrash->area[tmptrash->data] = 0;
205
Thierry Fournier8feaa662016-02-10 22:55:20 +0100206 str = get_trash_chunk();
Willy Tarreau2842e052018-08-22 04:55:43 +0200207 len = exp_replace(str->area, str->size,
208 tmptrash->area,
209 pat->data->u.str.area,
210 (regmatch_t *)smp->ctx.a[0]);
Nenad Merdanovic646b7742019-04-12 22:54:28 +0200211 free_trash_chunk(tmptrash);
212
Willy Tarreau2842e052018-08-22 04:55:43 +0200213 if (len == -1)
Thierry Fournier8feaa662016-02-10 22:55:20 +0100214 return 0;
Willy Tarreau2842e052018-08-22 04:55:43 +0200215
216 str->data = len;
Thierry Fournier8feaa662016-02-10 22:55:20 +0100217 smp->data.u.str = *str;
218 return 1;
219 }
220 /* Copy sample. */
Thierry FOURNIER5cc18d42015-08-19 09:02:36 +0200221 smp->data = *pat->data;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100222 smp->flags |= SMP_F_CONST;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100223 return 1;
224 }
225
226 /* Return just int sample containing 1. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200227 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200228 smp->data.u.sint = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100229 return 1;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100230 }
231
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800232 /* If no default value available, the converter fails. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100233 if (arg_p[1].type == ARGT_STOP)
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100234 return 0;
235
236 /* Return the default value. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100237 switch (desc->conv->out_type) {
238
239 case SMP_T_STR:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200240 smp->data.type = SMP_T_STR;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100241 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200242 smp->data.u.str = arg_p[1].data.str;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100243 break;
244
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200245 case SMP_T_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200246 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200247 smp->data.u.sint = arg_p[1].data.sint;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100248 break;
249
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200250 case SMP_T_ADDR:
251 if (arg_p[1].type == ARGT_IPV4) {
252 smp->data.type = SMP_T_IPV4;
253 smp->data.u.ipv4 = arg_p[1].data.ipv4;
254 } else {
255 smp->data.type = SMP_T_IPV6;
256 smp->data.u.ipv6 = arg_p[1].data.ipv6;
257 }
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100258 break;
259 }
260
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100261 return 1;
262}
263
William Lallemandad8be612016-11-18 19:26:17 +0100264/* This function is used with map and acl management. It permits to browse
265 * each reference. The variable <getnext> must contain the current node,
266 * <end> point to the root node and the <flags> permit to filter required
267 * nodes.
268 */
269static inline
270struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
271 unsigned int flags)
272{
273 struct pat_ref *ref = getnext;
274
275 while (1) {
276
277 /* Get next list entry. */
278 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
279
280 /* If the entry is the last of the list, return NULL. */
281 if (&ref->list == end)
282 return NULL;
283
284 /* If the entry match the flag, return it. */
285 if (ref->flags & flags)
286 return ref;
287 }
288}
289
290static inline
291struct pat_ref *pat_ref_lookup_ref(const char *reference)
292{
293 int id;
294 char *error;
295
296 /* If the reference starts by a '#', this is numeric id. */
297 if (reference[0] == '#') {
298 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
299 id = strtol(reference + 1, &error, 10);
300 if (*error != '\0')
301 return NULL;
302
303 /* Perform the unique id lookup. */
304 return pat_ref_lookupid(id);
305 }
306
307 /* Perform the string lookup. */
308 return pat_ref_lookup(reference);
309}
310
311/* This function is used with map and acl management. It permits to browse
312 * each reference.
313 */
314static inline
315struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
316{
317 struct pattern_expr *expr;
318 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
319 if (&expr->list == end)
320 return NULL;
321 return expr;
322}
323
Willy Tarreau95f753e2021-04-30 12:09:54 +0200324/* expects the current generation ID in appctx->cli.cli.i0 */
William Lallemandad8be612016-11-18 19:26:17 +0100325static int cli_io_handler_pat_list(struct appctx *appctx)
326{
Christopher Faulet908628c2022-03-25 16:43:49 +0100327 struct conn_stream *cs = appctx->owner;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200328 struct pat_ref_elt *elt;
329
Christopher Faulet908628c2022-03-25 16:43:49 +0100330 if (unlikely(cs_ic(cs)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Emeric Brun8d85aa42017-06-29 15:40:33 +0200331 /* If we're forced to shut down, we might have to remove our
332 * reference to the last ref_elt being dumped.
333 */
334 if (appctx->st2 == STAT_ST_LIST) {
Dragan Dosen336a11f2018-05-04 16:27:15 +0200335 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users)) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200336 LIST_DELETE(&appctx->ctx.map.bref.users);
Dragan Dosen336a11f2018-05-04 16:27:15 +0200337 LIST_INIT(&appctx->ctx.map.bref.users);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200338 }
339 }
340 return 1;
341 }
William Lallemandad8be612016-11-18 19:26:17 +0100342
343 switch (appctx->st2) {
344
345 case STAT_ST_INIT:
Emeric Brun8d85aa42017-06-29 15:40:33 +0200346 /* the function had not been called yet, let's prepare the
347 * buffer for a response. We initialize the current stream
348 * pointer to the first in the global list. When a target
349 * stream is being destroyed, it is responsible for updating
350 * this pointer. We know we have reached the end when this
351 * pointer points back to the head of the streams list.
352 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100353 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200354 LIST_INIT(&appctx->ctx.map.bref.users);
355 appctx->ctx.map.bref.ref = appctx->ctx.map.ref->head.n;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100356 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100357 appctx->st2 = STAT_ST_LIST;
358 /* fall through */
359
360 case STAT_ST_LIST:
Emeric Brunb5997f72017-07-03 11:34:05 +0200361
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100362 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200363
Emeric Brun8d85aa42017-06-29 15:40:33 +0200364 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users)) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200365 LIST_DELETE(&appctx->ctx.map.bref.users);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200366 LIST_INIT(&appctx->ctx.map.bref.users);
367 }
368
369 while (appctx->ctx.map.bref.ref != &appctx->ctx.map.ref->head) {
William Lallemandad8be612016-11-18 19:26:17 +0100370 chunk_reset(&trash);
371
Emeric Brun8d85aa42017-06-29 15:40:33 +0200372 elt = LIST_ELEM(appctx->ctx.map.bref.ref, struct pat_ref_elt *, list);
373
Willy Tarreau95f753e2021-04-30 12:09:54 +0200374 if (elt->gen_id != appctx->ctx.cli.i0)
Willy Tarreauc93da692020-10-29 09:41:34 +0100375 goto skip;
376
William Lallemandad8be612016-11-18 19:26:17 +0100377 /* build messages */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200378 if (elt->sample)
William Lallemandad8be612016-11-18 19:26:17 +0100379 chunk_appendf(&trash, "%p %s %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200380 elt, elt->pattern,
381 elt->sample);
William Lallemandad8be612016-11-18 19:26:17 +0100382 else
383 chunk_appendf(&trash, "%p %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200384 elt, elt->pattern);
William Lallemandad8be612016-11-18 19:26:17 +0100385
Christopher Faulet908628c2022-03-25 16:43:49 +0100386 if (ci_putchk(cs_ic(cs), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100387 /* let's try again later from this stream. We add ourselves into
388 * this stream's users so that it can remove us upon termination.
389 */
Willy Tarreau2b718102021-04-21 07:32:39 +0200390 LIST_APPEND(&elt->back_refs, &appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100391 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200392 cs_rx_room_blk(cs);
William Lallemandad8be612016-11-18 19:26:17 +0100393 return 0;
394 }
Willy Tarreauc93da692020-10-29 09:41:34 +0100395 skip:
William Lallemandad8be612016-11-18 19:26:17 +0100396 /* get next list entry and check the end of the list */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200397 appctx->ctx.map.bref.ref = elt->list.n;
William Lallemandad8be612016-11-18 19:26:17 +0100398 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100399 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100400 /* fall through */
401
402 default:
403 appctx->st2 = STAT_ST_FIN;
404 return 1;
405 }
406}
407
408static int cli_io_handler_pats_list(struct appctx *appctx)
409{
Christopher Faulet908628c2022-03-25 16:43:49 +0100410 struct conn_stream *cs = appctx->owner;
William Lallemandad8be612016-11-18 19:26:17 +0100411
412 switch (appctx->st2) {
413 case STAT_ST_INIT:
414 /* Display the column headers. If the message cannot be sent,
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800415 * quit the function with returning 0. The function is called
416 * later and restarted at the state "STAT_ST_INIT".
William Lallemandad8be612016-11-18 19:26:17 +0100417 */
418 chunk_reset(&trash);
419 chunk_appendf(&trash, "# id (file) description\n");
Christopher Faulet908628c2022-03-25 16:43:49 +0100420 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200421 cs_rx_room_blk(cs);
William Lallemandad8be612016-11-18 19:26:17 +0100422 return 0;
423 }
424
425 /* Now, we start the browsing of the references lists.
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800426 * Note that the following call to LIST_ELEM returns a bad pointer. The only
William Lallemandad8be612016-11-18 19:26:17 +0100427 * available field of this pointer is <list>. It is used with the function
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800428 * pat_list_get_next() for returning the first available entry
William Lallemandad8be612016-11-18 19:26:17 +0100429 */
430 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
431 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
432 appctx->ctx.map.display_flags);
433 appctx->st2 = STAT_ST_LIST;
434 /* fall through */
435
436 case STAT_ST_LIST:
437 while (appctx->ctx.map.ref) {
438 chunk_reset(&trash);
439
440 /* Build messages. If the reference is used by another category than
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800441 * the listed categories, display the information in the message.
William Lallemandad8be612016-11-18 19:26:17 +0100442 */
Dragan Dosena75eea72021-05-21 16:59:15 +0200443 chunk_appendf(&trash, "%d (%s) %s. curr_ver=%u next_ver=%u entry_cnt=%llu\n", appctx->ctx.map.ref->unique_id,
William Lallemandad8be612016-11-18 19:26:17 +0100444 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
Dragan Dosena75eea72021-05-21 16:59:15 +0200445 appctx->ctx.map.ref->display, appctx->ctx.map.ref->curr_gen, appctx->ctx.map.ref->next_gen,
446 appctx->ctx.map.ref->entry_cnt);
William Lallemandad8be612016-11-18 19:26:17 +0100447
Christopher Faulet908628c2022-03-25 16:43:49 +0100448 if (ci_putchk(cs_ic(cs), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100449 /* let's try again later from this stream. We add ourselves into
450 * this stream's users so that it can remove us upon termination.
451 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200452 cs_rx_room_blk(cs);
William Lallemandad8be612016-11-18 19:26:17 +0100453 return 0;
454 }
455
456 /* get next list entry and check the end of the list */
457 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
458 appctx->ctx.map.display_flags);
459 }
460
William Lallemandad8be612016-11-18 19:26:17 +0100461 /* fall through */
462
463 default:
464 appctx->st2 = STAT_ST_FIN;
465 return 1;
466 }
467 return 0;
468}
469
470static int cli_io_handler_map_lookup(struct appctx *appctx)
471{
Christopher Faulet908628c2022-03-25 16:43:49 +0100472 struct conn_stream *cs = appctx->owner;
William Lallemandad8be612016-11-18 19:26:17 +0100473 struct sample sample;
474 struct pattern *pat;
475 int match_method;
476
477 switch (appctx->st2) {
478 case STAT_ST_INIT:
479 /* Init to the first entry. The list cannot be change */
480 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
481 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
482 appctx->st2 = STAT_ST_LIST;
483 /* fall through */
484
485 case STAT_ST_LIST:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100486 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100487 /* for each lookup type */
488 while (appctx->ctx.map.expr) {
489 /* initialise chunk to build new message */
490 chunk_reset(&trash);
491
492 /* execute pattern matching */
493 sample.data.type = SMP_T_STR;
494 sample.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200495 sample.data.u.str.data = appctx->ctx.map.chunk.data;
496 sample.data.u.str.area = appctx->ctx.map.chunk.area;
Emeric Brunb5997f72017-07-03 11:34:05 +0200497
William Lallemandad8be612016-11-18 19:26:17 +0100498 if (appctx->ctx.map.expr->pat_head->match &&
499 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
500 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
501 else
502 pat = NULL;
503
504 /* build return message: set type of match */
505 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
506 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
507 break;
508 if (match_method >= PAT_MATCH_NUM)
509 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
510 else
511 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
512
513 /* case sensitive */
514 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
515 chunk_appendf(&trash, ", case=insensitive");
516 else
517 chunk_appendf(&trash, ", case=sensitive");
518
519 /* Display no match, and set default value */
520 if (!pat) {
521 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
522 chunk_appendf(&trash, ", found=no");
523 else
524 chunk_appendf(&trash, ", match=no");
525 }
526
527 /* Display match and match info */
528 else {
529 /* display match */
530 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
531 chunk_appendf(&trash, ", found=yes");
532 else
533 chunk_appendf(&trash, ", match=yes");
534
535 /* display index mode */
536 if (pat->sflags & PAT_SF_TREE)
537 chunk_appendf(&trash, ", idx=tree");
538 else
539 chunk_appendf(&trash, ", idx=list");
540
541 /* display pattern */
542 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
543 if (pat->ref && pat->ref->pattern)
544 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
545 else
546 chunk_appendf(&trash, ", key=unknown");
547 }
548 else {
549 if (pat->ref && pat->ref->pattern)
550 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
551 else
552 chunk_appendf(&trash, ", pattern=unknown");
553 }
554
555 /* display return value */
556 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
557 if (pat->data && pat->ref && pat->ref->sample)
558 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
559 smp_to_type[pat->data->type]);
560 else
561 chunk_appendf(&trash, ", value=none");
562 }
563 }
564
565 chunk_appendf(&trash, "\n");
566
567 /* display response */
Christopher Faulet908628c2022-03-25 16:43:49 +0100568 if (ci_putchk(cs_ic(cs), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100569 /* let's try again later from this stream. We add ourselves into
570 * this stream's users so that it can remove us upon termination.
571 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100572 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200573 cs_rx_room_blk(cs);
William Lallemandad8be612016-11-18 19:26:17 +0100574 return 0;
575 }
576
577 /* get next entry */
578 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
579 &appctx->ctx.map.ref->pat);
580 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100581 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100582 /* fall through */
583
584 default:
585 appctx->st2 = STAT_ST_FIN;
William Lallemandad8be612016-11-18 19:26:17 +0100586 return 1;
587 }
588}
589
590static void cli_release_mlook(struct appctx *appctx)
591{
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100592 ha_free(&appctx->ctx.map.chunk.area);
William Lallemandad8be612016-11-18 19:26:17 +0100593}
594
595
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200596static int cli_parse_get_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100597{
598 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
599 /* Set flags. */
600 if (args[1][0] == 'm')
601 appctx->ctx.map.display_flags = PAT_REF_MAP;
602 else
603 appctx->ctx.map.display_flags = PAT_REF_ACL;
604
605 /* No parameter. */
606 if (!*args[2] || !*args[3]) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200607 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
608 return cli_err(appctx, "Missing map identifier and/or key.\n");
609 else
610 return cli_err(appctx, "Missing ACL identifier and/or key.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100611 }
612
613 /* lookup into the maps */
614 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
615 if (!appctx->ctx.map.ref) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200616 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
617 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
618 else
619 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100620 }
621
622 /* copy input string. The string must be allocated because
623 * it may be used over multiple iterations. It's released
624 * at the end and upon abort anyway.
625 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200626 appctx->ctx.map.chunk.data = strlen(args[3]);
627 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.data + 1;
628 appctx->ctx.map.chunk.area = strdup(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200629 if (!appctx->ctx.map.chunk.area)
630 return cli_err(appctx, "Out of memory error.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100631
632 return 0;
633 }
634 return 1;
635}
636
Willy Tarreau97218ce2021-04-30 14:57:03 +0200637static int cli_parse_prepare_map(char **args, char *payload, struct appctx *appctx, void *private)
638{
639 if (strcmp(args[1], "map") == 0 ||
640 strcmp(args[1], "acl") == 0) {
641 uint next_gen;
642 char *msg = NULL;
643
644 /* Set ACL or MAP flags. */
645 if (args[1][0] == 'm')
646 appctx->ctx.map.display_flags = PAT_REF_MAP;
647 else
648 appctx->ctx.map.display_flags = PAT_REF_ACL;
649
650 /* lookup into the refs and check the map flag */
651 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
652 if (!appctx->ctx.map.ref ||
653 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
654 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
655 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
656 else
657 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
658 }
659 next_gen = pat_ref_newgen(appctx->ctx.map.ref);
660 return cli_dynmsg(appctx, LOG_INFO, memprintf(&msg, "New version created: %u\n", next_gen));
661 }
662
663 return 0;
664}
665
Emeric Brun8d85aa42017-06-29 15:40:33 +0200666static void cli_release_show_map(struct appctx *appctx)
667{
668 if (appctx->st2 == STAT_ST_LIST) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100669 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200670 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users))
Willy Tarreau2b718102021-04-21 07:32:39 +0200671 LIST_DELETE(&appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100672 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200673 }
674}
675
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200676static int cli_parse_show_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100677{
678 if (strcmp(args[1], "map") == 0 ||
679 strcmp(args[1], "acl") == 0) {
Willy Tarreau95f753e2021-04-30 12:09:54 +0200680 const char *gen = NULL;
William Lallemandad8be612016-11-18 19:26:17 +0100681
682 /* Set ACL or MAP flags. */
683 if (args[1][0] == 'm')
684 appctx->ctx.map.display_flags = PAT_REF_MAP;
685 else
686 appctx->ctx.map.display_flags = PAT_REF_ACL;
687
688 /* no parameter: display all map available */
689 if (!*args[2]) {
William Lallemandad8be612016-11-18 19:26:17 +0100690 appctx->io_handler = cli_io_handler_pats_list;
691 return 0;
692 }
693
Willy Tarreau95f753e2021-04-30 12:09:54 +0200694 /* For both "map" and "acl" we may have an optional generation
695 * number specified using a "@" character before the pattern
696 * file name.
697 */
698 if (*args[2] == '@') {
699 gen = args[2] + 1;
700 args++;
701 }
702
William Lallemandad8be612016-11-18 19:26:17 +0100703 /* lookup into the refs and check the map flag */
704 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
705 if (!appctx->ctx.map.ref ||
706 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200707 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
708 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
709 else
710 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100711 }
Willy Tarreau95f753e2021-04-30 12:09:54 +0200712
713 /* set the desired generation id in cli.i0 */
714 if (gen)
715 appctx->ctx.cli.i0 = str2uic(gen);
716 else
717 appctx->ctx.cli.i0 = appctx->ctx.map.ref->curr_gen;
718
William Lallemandad8be612016-11-18 19:26:17 +0100719 appctx->io_handler = cli_io_handler_pat_list;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200720 appctx->io_release = cli_release_show_map;
William Lallemandad8be612016-11-18 19:26:17 +0100721 return 0;
722 }
723
724 return 0;
725}
726
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200727static int cli_parse_set_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100728{
729 if (strcmp(args[1], "map") == 0) {
730 char *err;
731
732 /* Set flags. */
733 appctx->ctx.map.display_flags = PAT_REF_MAP;
734
735 /* Expect three parameters: map name, key and new value. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200736 if (!*args[2] || !*args[3] || !*args[4])
737 return cli_err(appctx, "'set map' expects three parameters: map identifier, key and value.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100738
739 /* Lookup the reference in the maps. */
740 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200741 if (!appctx->ctx.map.ref)
742 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100743
744 /* If the entry identifier start with a '#', it is considered as
745 * pointer id
746 */
747 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
748 struct pat_ref_elt *ref;
749 long long int conv;
750 char *error;
751
752 /* Convert argument to integer value. */
753 conv = strtoll(&args[3][1], &error, 16);
Willy Tarreau9d008692019-08-09 11:21:01 +0200754 if (*error != '\0')
755 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100756
757 /* Convert and check integer to pointer. */
758 ref = (struct pat_ref_elt *)(long)conv;
Willy Tarreau9d008692019-08-09 11:21:01 +0200759 if ((long long int)(long)ref != conv)
760 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100761
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200762 /* Try to modify the entry. */
William Lallemandad8be612016-11-18 19:26:17 +0100763 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100764 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100765 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100766 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +0200767 if (err)
768 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
769 else
770 return cli_err(appctx, "Failed to update an entry.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100771 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100772 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100773 }
774 else {
775 /* Else, use the entry identifier as pattern
776 * string, and update the value.
777 */
778 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100779 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100780 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100781 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +0200782 if (err)
783 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
784 else
785 return cli_err(appctx, "Failed to update an entry.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100786 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100787 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100788 }
789
790 /* The set is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100791 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100792 return 0;
793 }
794 return 1;
795}
796
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200797static int cli_parse_add_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100798{
799 if (strcmp(args[1], "map") == 0 ||
800 strcmp(args[1], "acl") == 0) {
Willy Tarreaubb51c442021-04-30 15:23:36 +0200801 const char *gen = NULL;
802 uint genid = 0;
William Lallemandad8be612016-11-18 19:26:17 +0100803 int ret;
804 char *err;
805
806 /* Set flags. */
807 if (args[1][0] == 'm')
808 appctx->ctx.map.display_flags = PAT_REF_MAP;
809 else
810 appctx->ctx.map.display_flags = PAT_REF_ACL;
811
Willy Tarreaubb51c442021-04-30 15:23:36 +0200812 /* For both "map" and "acl" we may have an optional generation
813 * number specified using a "@" character before the pattern
814 * file name.
815 */
816 if (*args[2] == '@') {
817 gen = args[2] + 1;
818 args++;
819 }
820
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200821 /* If the keyword is "map", we expect:
822 * - three parameters if there is no payload
823 * - one parameter if there is a payload
824 * If it is "acl", we expect only two parameters
William Lallemandad8be612016-11-18 19:26:17 +0100825 */
826 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200827 if ((!payload && (!*args[2] || !*args[3] || !*args[4])) ||
Willy Tarreau9d008692019-08-09 11:21:01 +0200828 (payload && !*args[2]))
829 return cli_err(appctx,
830 "'add map' expects three parameters (map identifier, key and value)"
831 " or one parameter (map identifier) and a payload\n");
William Lallemandad8be612016-11-18 19:26:17 +0100832 }
Willy Tarreau9d008692019-08-09 11:21:01 +0200833 else if (!*args[2] || !*args[3])
834 return cli_err(appctx, "'add acl' expects two parameters: ACL identifier and pattern.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100835
836 /* Lookup for the reference. */
837 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
838 if (!appctx->ctx.map.ref) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200839 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
840 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
841 else
842 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100843 }
844
Willy Tarreaubb51c442021-04-30 15:23:36 +0200845 if (gen) {
846 genid = str2uic(gen);
847 if ((int)(genid - appctx->ctx.map.ref->next_gen) > 0) {
848 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
849 return cli_err(appctx, "Version number in the future, please use 'prepare map' before.\n");
850 else
851 return cli_err(appctx, "Version number in the future, please use 'prepare acl' before.\n");
852 }
853 }
854
William Lallemandad8be612016-11-18 19:26:17 +0100855 /* The command "add acl" is prohibited if the reference
856 * use samples.
857 */
858 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
859 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200860 return cli_err(appctx,
861 "This ACL is shared with a map containing samples. "
862 "You must use the command 'add map' to add values.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100863 }
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200864
865 /* Add value(s). If no payload is used, key and value are read
866 * from the command line and only one key is set. If a payload
867 * is passed, one key/value pair is read per line till the end
868 * of the payload is reached.
869 */
William Lallemandad8be612016-11-18 19:26:17 +0100870 err = NULL;
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200871
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200872 do {
873 char *key = args[3];
874 char *value = args[4];
875 size_t l;
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200876
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200877 if (payload) {
878 /* key and value passed as payload, one pair per line */
879 if (!*payload)
880 break;
881
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200882 key = payload;
883 l = strcspn(key, " \t");
884 payload += l;
885
Willy Tarreau9d008692019-08-09 11:21:01 +0200886 if (!*payload && appctx->ctx.map.display_flags == PAT_REF_MAP)
887 return cli_dynerr(appctx, memprintf(&err, "Missing value for key '%s'.\n", key));
888
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200889 key[l] = 0;
890 payload++;
891
892 /* value */
893 payload += strspn(payload, " \t");
894 value = payload;
895 l = strcspn(value, "\n");
896 payload += l;
897 if (*payload)
898 payload++;
899 value[l] = 0;
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200900 }
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200901
Willy Tarreau4053b032021-04-29 16:55:17 +0200902 if (appctx->ctx.map.display_flags != PAT_REF_MAP)
903 value = NULL;
904
905 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreaubb51c442021-04-30 15:23:36 +0200906 ret = !!pat_ref_load(appctx->ctx.map.ref, gen ? genid : appctx->ctx.map.ref->curr_gen, key, value, -1, &err);
Willy Tarreau4053b032021-04-29 16:55:17 +0200907 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
908
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200909 if (!ret) {
910 if (err)
911 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
912 else
913 return cli_err(appctx, "Failed to add a key.\n");
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200914 }
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200915 } while (payload && *payload);
William Lallemandad8be612016-11-18 19:26:17 +0100916
917 /* The add is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100918 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100919 return 1;
920 }
921
922 return 0;
923}
924
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200925static int cli_parse_del_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100926{
927 if (args[1][0] == 'm')
928 appctx->ctx.map.display_flags = PAT_REF_MAP;
929 else
930 appctx->ctx.map.display_flags = PAT_REF_ACL;
931
932 /* Expect two parameters: map name and key. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200933 if (!*args[2] || !*args[3]) {
934 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
935 return cli_err(appctx, "This command expects two parameters: map identifier and key.\n");
936 else
937 return cli_err(appctx, "This command expects two parameters: ACL identifier and key.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100938 }
939
940 /* Lookup the reference in the maps. */
941 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
942 if (!appctx->ctx.map.ref ||
Willy Tarreau9d008692019-08-09 11:21:01 +0200943 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags))
944 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100945
946 /* If the entry identifier start with a '#', it is considered as
947 * pointer id
948 */
949 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
950 struct pat_ref_elt *ref;
951 long long int conv;
952 char *error;
953
954 /* Convert argument to integer value. */
955 conv = strtoll(&args[3][1], &error, 16);
Willy Tarreau9d008692019-08-09 11:21:01 +0200956 if (*error != '\0')
957 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100958
959 /* Convert and check integer to pointer. */
960 ref = (struct pat_ref_elt *)(long)conv;
Willy Tarreau9d008692019-08-09 11:21:01 +0200961 if ((long long int)(long)ref != conv)
962 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100963
964 /* Try to delete the entry. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100965 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100966 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100967 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100968 /* The entry is not found, send message. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200969 return cli_err(appctx, "Key not found.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100970 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100971 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100972 }
973 else {
974 /* Else, use the entry identifier as pattern
975 * string and try to delete the entry.
976 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100977 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100978 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100979 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100980 /* The entry is not found, send message. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200981 return cli_err(appctx, "Key not found.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100982 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100983 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100984 }
985
986 /* The deletion is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100987 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100988 return 1;
989}
990
Willy Tarreauff3feeb2021-04-30 13:31:43 +0200991/* continue to clear a map which was started in the parser. The range of
992 * generations this applies to is taken from appctx->ctx.cli.i0 for the oldest
993 * and appctx->ctx.cli.i1 for the latest.
994 */
Willy Tarreaud1d005d2019-12-20 18:22:02 +0100995static int cli_io_handler_clear_map(struct appctx *appctx)
996{
Willy Tarreaud1d005d2019-12-20 18:22:02 +0100997 int finished;
998
999 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreauff3feeb2021-04-30 13:31:43 +02001000 finished = pat_ref_purge_range(appctx->ctx.map.ref, appctx->ctx.cli.i0, appctx->ctx.cli.i1, 100);
Willy Tarreaud1d005d2019-12-20 18:22:02 +01001001 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
1002
1003 if (!finished) {
1004 /* let's come back later */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001005 cs_rx_endp_more(appctx->owner);
Willy Tarreaud1d005d2019-12-20 18:22:02 +01001006 return 0;
1007 }
1008 return 1;
1009}
1010
Willy Tarreauff3feeb2021-04-30 13:31:43 +02001011/* note: sets appctx->ctx.cli.i0 and appctx->ctx.cli.i1 to the oldest and
1012 * latest generations to clear, respectively, and will call the clear_map
1013 * handler.
1014 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001015static int cli_parse_clear_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +01001016{
1017 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
Willy Tarreauff3feeb2021-04-30 13:31:43 +02001018 const char *gen = NULL;
1019
William Lallemandad8be612016-11-18 19:26:17 +01001020 /* Set ACL or MAP flags. */
1021 if (args[1][0] == 'm')
1022 appctx->ctx.map.display_flags = PAT_REF_MAP;
1023 else
1024 appctx->ctx.map.display_flags = PAT_REF_ACL;
1025
Willy Tarreauff3feeb2021-04-30 13:31:43 +02001026 /* For both "map" and "acl" we may have an optional generation
1027 * number specified using a "@" character before the pattern
1028 * file name.
1029 */
1030 if (*args[2] == '@') {
1031 gen = args[2] + 1;
1032 args++;
1033 }
1034
William Lallemandad8be612016-11-18 19:26:17 +01001035 /* no parameter */
1036 if (!*args[2]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02001037 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1038 return cli_err(appctx, "Missing map identifier.\n");
1039 else
1040 return cli_err(appctx, "Missing ACL identifier.\n");
William Lallemandad8be612016-11-18 19:26:17 +01001041 }
1042
1043 /* lookup into the refs and check the map flag */
1044 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
1045 if (!appctx->ctx.map.ref ||
1046 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02001047 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1048 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
1049 else
1050 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +01001051 }
1052
Willy Tarreauff3feeb2021-04-30 13:31:43 +02001053 /* set the desired generation id in cli.i0/i1 */
1054 if (gen)
1055 appctx->ctx.cli.i1 = appctx->ctx.cli.i0 = str2uic(gen);
1056 else
1057 appctx->ctx.cli.i1 = appctx->ctx.cli.i0 = appctx->ctx.map.ref->curr_gen;
1058
Willy Tarreaud1d005d2019-12-20 18:22:02 +01001059 /* delegate the clearing to the I/O handler which can yield */
1060 return 0;
William Lallemandad8be612016-11-18 19:26:17 +01001061 }
Willy Tarreaud1d005d2019-12-20 18:22:02 +01001062 return 1;
William Lallemandad8be612016-11-18 19:26:17 +01001063}
1064
Willy Tarreau7a562ca2021-04-30 15:10:01 +02001065/* note: sets appctx->ctx.cli.i0 and appctx->ctx.cli.i1 to the oldest and
1066 * latest generations to clear, respectively, and will call the clear_map
1067 * handler.
1068 */
1069static int cli_parse_commit_map(char **args, char *payload, struct appctx *appctx, void *private)
1070{
1071 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1072 const char *gen = NULL;
1073 uint genid;
1074 uint ret;
1075
1076 /* Set ACL or MAP flags. */
1077 if (args[1][0] == 'm')
1078 appctx->ctx.map.display_flags = PAT_REF_MAP;
1079 else
1080 appctx->ctx.map.display_flags = PAT_REF_ACL;
1081
1082 if (*args[2] != '@')
1083 return cli_err(appctx, "Missing version number.\n");
1084
1085 /* The generation number is mandatory for a commit. The range
1086 * of generations that get trashed by a commit starts from the
1087 * opposite of the current one and ends at the previous one.
1088 */
1089 gen = args[2] + 1;
1090 genid = str2uic(gen);
1091 appctx->ctx.cli.i1 = genid - 1;
1092 appctx->ctx.cli.i0 = appctx->ctx.cli.i1 - ((~0U) >> 1);
1093
1094 /* no parameter */
1095 if (!*args[3]) {
1096 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1097 return cli_err(appctx, "Missing map identifier.\n");
1098 else
1099 return cli_err(appctx, "Missing ACL identifier.\n");
1100 }
1101
1102 /* lookup into the refs and check the map flag */
1103 appctx->ctx.map.ref = pat_ref_lookup_ref(args[3]);
1104 if (!appctx->ctx.map.ref ||
1105 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1106 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1107 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
1108 else
1109 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
1110 }
1111
1112 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
1113 if (genid - (appctx->ctx.map.ref->curr_gen + 1) <
1114 appctx->ctx.map.ref->next_gen - appctx->ctx.map.ref->curr_gen)
1115 ret = pat_ref_commit(appctx->ctx.map.ref, genid);
1116 else
1117 ret = 1;
1118 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
1119
1120 if (ret != 0)
1121 return cli_err(appctx, "Version number out of range.\n");
1122
1123 /* delegate the clearing to the I/O handler which can yield */
1124 return 0;
1125 }
1126 return 1;
1127}
1128
William Lallemandad8be612016-11-18 19:26:17 +01001129/* register cli keywords */
1130
1131static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001132 { { "add", "acl", NULL }, "add acl [@<ver>] <acl> <pattern> : add an acl entry", cli_parse_add_map, NULL },
1133 { { "clear", "acl", NULL }, "clear acl [@<ver>] <acl> : clear the contents of this acl", cli_parse_clear_map, cli_io_handler_clear_map, NULL },
1134 { { "commit","acl", NULL }, "commit acl @<ver> <acl> : commit the ACL at this version", cli_parse_commit_map, cli_io_handler_clear_map, NULL },
1135 { { "del", "acl", NULL }, "del acl <acl> [<key>|#<ref>] : delete acl entries matching <key>", cli_parse_del_map, NULL },
1136 { { "get", "acl", NULL }, "get acl <acl> <value> : report the patterns matching a sample for an ACL", cli_parse_get_map, cli_io_handler_map_lookup, cli_release_mlook },
1137 { { "prepare","acl",NULL }, "prepare acl <acl> : prepare a new version for atomic ACL replacement", cli_parse_prepare_map, NULL },
1138 { { "show", "acl", NULL }, "show acl [@<ver>] <acl>] : report available acls or dump an acl's contents", cli_parse_show_map, NULL },
1139 { { "add", "map", NULL }, "add map [@<ver>] <map> <key> <val> : add a map entry (payload supported instead of key/val)", cli_parse_add_map, NULL },
1140 { { "clear", "map", NULL }, "clear map [@<ver>] <map> : clear the contents of this map", cli_parse_clear_map, cli_io_handler_clear_map, NULL },
1141 { { "commit","map", NULL }, "commit map @<ver> <map> : commit the map at this version", cli_parse_commit_map, cli_io_handler_clear_map, NULL },
1142 { { "del", "map", NULL }, "del map <map> [<key>|#<ref>] : delete map entries matching <key>", cli_parse_del_map, NULL },
1143 { { "get", "map", NULL }, "get map <acl> <value> : report the keys and values matching a sample for a map", cli_parse_get_map, cli_io_handler_map_lookup, cli_release_mlook },
1144 { { "prepare","map",NULL }, "prepare map <acl> : prepare a new version for atomic map replacement", cli_parse_prepare_map, NULL },
1145 { { "set", "map", NULL }, "set map <map> [<key>|#<ref>] <value> : modify a map entry", cli_parse_set_map, NULL },
1146 { { "show", "map", NULL }, "show map [@ver] [map] : report available maps or dump a map's contents", cli_parse_show_map, NULL },
William Lallemandad8be612016-11-18 19:26:17 +01001147 { { NULL }, NULL, NULL, NULL }
1148}};
1149
Willy Tarreau0108d902018-11-25 19:14:37 +01001150INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemandad8be612016-11-18 19:26:17 +01001151
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +01001152/* Note: must not be declared <const> as its list will be overwritten
1153 *
1154 * For the map_*_int keywords, the output is declared as SMP_T_UINT, but the converter function
1155 * can provide SMP_T_UINT, SMP_T_SINT or SMP_T_BOOL depending on how the patterns found in the
1156 * file can be parsed.
1157 *
1158 * For the map_*_ip keyword, the output is declared as SMP_T_IPV4, but the converter function
1159 * can provide SMP_T_IPV4 or SMP_T_IPV6 depending on the patterns found in the file.
1160 *
1161 * The map_* keywords only emit strings.
1162 *
1163 * The output type is only used during the configuration parsing. It is used for detecting
1164 * compatibility problems.
1165 *
1166 * The arguments are: <file>[,<default value>]
1167 */
1168static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER1edc9712014-12-15 16:18:39 +01001169 { "map", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1170 { "map_str", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1171 { "map_beg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_BEG },
1172 { "map_sub", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_SUB },
1173 { "map_dir", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DIR },
1174 { "map_dom", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DOM },
1175 { "map_end", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_END },
1176 { "map_reg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_REG },
Thierry Fournier8feaa662016-02-10 22:55:20 +01001177 { "map_regm", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_REGM},
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001178 { "map_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_SINT, SMP_T_STR, (void *)PAT_MATCH_INT },
Thierry FOURNIER1edc9712014-12-15 16:18:39 +01001179 { "map_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_ADDR, SMP_T_STR, (void *)PAT_MATCH_IP },
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +01001180
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001181 { "map_str_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_STR },
1182 { "map_beg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_BEG },
1183 { "map_sub_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_SUB },
1184 { "map_dir_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DIR },
1185 { "map_dom_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DOM },
1186 { "map_end_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_END },
1187 { "map_reg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_REG },
1188 { "map_int_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_SINT, SMP_T_SINT, (void *)PAT_MATCH_INT },
1189 { "map_ip_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_ADDR, SMP_T_SINT, (void *)PAT_MATCH_IP },
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +01001190
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +02001191 { "map_str_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_STR },
1192 { "map_beg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_BEG },
1193 { "map_sub_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_SUB },
1194 { "map_dir_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DIR },
1195 { "map_dom_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DOM },
1196 { "map_end_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_END },
1197 { "map_reg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_REG },
1198 { "map_int_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_SINT, SMP_T_ADDR, (void *)PAT_MATCH_INT },
1199 { "map_ip_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_ADDR, SMP_T_ADDR, (void *)PAT_MATCH_IP },
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +01001200
1201 { /* END */ },
1202}};
1203
Willy Tarreau0108d902018-11-25 19:14:37 +01001204INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);