blob: eae597d47deb508cb680521aad2de1472eb6b5ff [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 Tarreau0fcecc62022-05-03 11:54:47 +020017#include <haproxy/applet.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020018#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 Tarreau0fcecc62022-05-03 11:54:47 +0200324/* appctx context for the "{show|get|add|del|*} {map|acl}" commands. This is
325 * used even by commands that only have a parser and no I/O handler because
326 * it provides a unified way to manipulate some fields and will allow to
327 * expand some of them more easily later if needed.
328 */
329struct show_map_ctx {
330 struct pat_ref *ref;
331 struct bref bref; /* back-reference from the pat_ref_elt being dumped */
332 struct pattern_expr *expr;
333 struct buffer chunk;
334 unsigned int display_flags;
Willy Tarreau76f771e2022-05-03 14:12:56 +0200335 unsigned int curr_gen; /* current/latest generation, for show/clear */
336 unsigned int prev_gen; /* prev generation, for clear */
Willy Tarreaua0d62802022-05-03 15:12:21 +0200337 enum {
338 STATE_INIT = 0, /* initialize list and backrefs */
339 STATE_LIST, /* list entries */
340 STATE_DONE, /* finished */
341 } state; /* state of the dump */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200342};
343
Willy Tarreau76f771e2022-05-03 14:12:56 +0200344/* expects the current generation ID in ctx->curr_gen */
William Lallemandad8be612016-11-18 19:26:17 +0100345static int cli_io_handler_pat_list(struct appctx *appctx)
346{
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200347 struct show_map_ctx *ctx = appctx->svcctx;
Willy Tarreau4596fe22022-05-17 19:07:51 +0200348 struct stconn *cs = appctx_cs(appctx);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200349 struct pat_ref_elt *elt;
350
Christopher Faulet908628c2022-03-25 16:43:49 +0100351 if (unlikely(cs_ic(cs)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Emeric Brun8d85aa42017-06-29 15:40:33 +0200352 /* If we're forced to shut down, we might have to remove our
353 * reference to the last ref_elt being dumped.
354 */
Willy Tarreauc7e97062022-05-03 15:42:07 +0200355 if (!LIST_ISEMPTY(&ctx->bref.users)) {
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200356 HA_SPIN_LOCK(PATREF_LOCK, &ctx->ref->lock);
Willy Tarreauc7e97062022-05-03 15:42:07 +0200357 LIST_DEL_INIT(&ctx->bref.users);
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200358 HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200359 }
360 return 1;
361 }
William Lallemandad8be612016-11-18 19:26:17 +0100362
Willy Tarreaua0d62802022-05-03 15:12:21 +0200363 switch (ctx->state) {
364 case STATE_INIT:
365 ctx->state = STATE_LIST;
William Lallemandad8be612016-11-18 19:26:17 +0100366 /* fall through */
367
Willy Tarreaua0d62802022-05-03 15:12:21 +0200368 case STATE_LIST:
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200369 HA_SPIN_LOCK(PATREF_LOCK, &ctx->ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200370
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200371 if (!LIST_ISEMPTY(&ctx->bref.users)) {
372 LIST_DELETE(&ctx->bref.users);
373 LIST_INIT(&ctx->bref.users);
Willy Tarreau1ae0c432022-05-03 15:26:27 +0200374 } else {
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200375 ctx->bref.ref = ctx->ref->head.n;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200376 }
377
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200378 while (ctx->bref.ref != &ctx->ref->head) {
William Lallemandad8be612016-11-18 19:26:17 +0100379 chunk_reset(&trash);
380
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200381 elt = LIST_ELEM(ctx->bref.ref, struct pat_ref_elt *, list);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200382
Willy Tarreau76f771e2022-05-03 14:12:56 +0200383 if (elt->gen_id != ctx->curr_gen)
Willy Tarreauc93da692020-10-29 09:41:34 +0100384 goto skip;
385
William Lallemandad8be612016-11-18 19:26:17 +0100386 /* build messages */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200387 if (elt->sample)
William Lallemandad8be612016-11-18 19:26:17 +0100388 chunk_appendf(&trash, "%p %s %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200389 elt, elt->pattern,
390 elt->sample);
William Lallemandad8be612016-11-18 19:26:17 +0100391 else
392 chunk_appendf(&trash, "%p %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200393 elt, elt->pattern);
William Lallemandad8be612016-11-18 19:26:17 +0100394
Christopher Faulet908628c2022-03-25 16:43:49 +0100395 if (ci_putchk(cs_ic(cs), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100396 /* let's try again later from this stream. We add ourselves into
397 * this stream's users so that it can remove us upon termination.
398 */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200399 LIST_APPEND(&elt->back_refs, &ctx->bref.users);
400 HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock);
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200401 cs_rx_room_blk(cs);
William Lallemandad8be612016-11-18 19:26:17 +0100402 return 0;
403 }
Willy Tarreauc93da692020-10-29 09:41:34 +0100404 skip:
William Lallemandad8be612016-11-18 19:26:17 +0100405 /* get next list entry and check the end of the list */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200406 ctx->bref.ref = elt->list.n;
William Lallemandad8be612016-11-18 19:26:17 +0100407 }
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200408 HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100409 /* fall through */
410
411 default:
Willy Tarreaua0d62802022-05-03 15:12:21 +0200412 ctx->state = STATE_DONE;
William Lallemandad8be612016-11-18 19:26:17 +0100413 return 1;
414 }
415}
416
417static int cli_io_handler_pats_list(struct appctx *appctx)
418{
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200419 struct show_map_ctx *ctx = appctx->svcctx;
Willy Tarreau4596fe22022-05-17 19:07:51 +0200420 struct stconn *cs = appctx_cs(appctx);
William Lallemandad8be612016-11-18 19:26:17 +0100421
Willy Tarreaua0d62802022-05-03 15:12:21 +0200422 switch (ctx->state) {
423 case STATE_INIT:
William Lallemandad8be612016-11-18 19:26:17 +0100424 /* Display the column headers. If the message cannot be sent,
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800425 * quit the function with returning 0. The function is called
Willy Tarreaua0d62802022-05-03 15:12:21 +0200426 * later and restarted at the state "STATE_INIT".
William Lallemandad8be612016-11-18 19:26:17 +0100427 */
428 chunk_reset(&trash);
429 chunk_appendf(&trash, "# id (file) description\n");
Christopher Faulet908628c2022-03-25 16:43:49 +0100430 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200431 cs_rx_room_blk(cs);
William Lallemandad8be612016-11-18 19:26:17 +0100432 return 0;
433 }
434
435 /* Now, we start the browsing of the references lists.
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800436 * Note that the following call to LIST_ELEM returns a bad pointer. The only
William Lallemandad8be612016-11-18 19:26:17 +0100437 * available field of this pointer is <list>. It is used with the function
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800438 * pat_list_get_next() for returning the first available entry
William Lallemandad8be612016-11-18 19:26:17 +0100439 */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200440 ctx->ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
441 ctx->ref = pat_list_get_next(ctx->ref, &pattern_reference,
442 ctx->display_flags);
Willy Tarreaua0d62802022-05-03 15:12:21 +0200443 ctx->state = STATE_LIST;
William Lallemandad8be612016-11-18 19:26:17 +0100444 /* fall through */
445
Willy Tarreaua0d62802022-05-03 15:12:21 +0200446 case STATE_LIST:
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200447 while (ctx->ref) {
William Lallemandad8be612016-11-18 19:26:17 +0100448 chunk_reset(&trash);
449
450 /* Build messages. If the reference is used by another category than
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800451 * the listed categories, display the information in the message.
William Lallemandad8be612016-11-18 19:26:17 +0100452 */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200453 chunk_appendf(&trash, "%d (%s) %s. curr_ver=%u next_ver=%u entry_cnt=%llu\n", ctx->ref->unique_id,
454 ctx->ref->reference ? ctx->ref->reference : "",
455 ctx->ref->display, ctx->ref->curr_gen, ctx->ref->next_gen,
456 ctx->ref->entry_cnt);
William Lallemandad8be612016-11-18 19:26:17 +0100457
Christopher Faulet908628c2022-03-25 16:43:49 +0100458 if (ci_putchk(cs_ic(cs), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100459 /* let's try again later from this stream. We add ourselves into
460 * this stream's users so that it can remove us upon termination.
461 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200462 cs_rx_room_blk(cs);
William Lallemandad8be612016-11-18 19:26:17 +0100463 return 0;
464 }
465
466 /* get next list entry and check the end of the list */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200467 ctx->ref = pat_list_get_next(ctx->ref, &pattern_reference,
468 ctx->display_flags);
William Lallemandad8be612016-11-18 19:26:17 +0100469 }
470
William Lallemandad8be612016-11-18 19:26:17 +0100471 /* fall through */
472
473 default:
Willy Tarreaua0d62802022-05-03 15:12:21 +0200474 ctx->state = STATE_DONE;
William Lallemandad8be612016-11-18 19:26:17 +0100475 return 1;
476 }
477 return 0;
478}
479
480static int cli_io_handler_map_lookup(struct appctx *appctx)
481{
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200482 struct show_map_ctx *ctx = appctx->svcctx;
Willy Tarreau4596fe22022-05-17 19:07:51 +0200483 struct stconn *cs = appctx_cs(appctx);
William Lallemandad8be612016-11-18 19:26:17 +0100484 struct sample sample;
485 struct pattern *pat;
486 int match_method;
487
Willy Tarreaua0d62802022-05-03 15:12:21 +0200488 switch (ctx->state) {
489 case STATE_INIT:
William Lallemandad8be612016-11-18 19:26:17 +0100490 /* Init to the first entry. The list cannot be change */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200491 ctx->expr = LIST_ELEM(&ctx->ref->pat, struct pattern_expr *, list);
492 ctx->expr = pat_expr_get_next(ctx->expr, &ctx->ref->pat);
Willy Tarreaua0d62802022-05-03 15:12:21 +0200493 ctx->state = STATE_LIST;
William Lallemandad8be612016-11-18 19:26:17 +0100494 /* fall through */
495
Willy Tarreaua0d62802022-05-03 15:12:21 +0200496 case STATE_LIST:
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200497 HA_SPIN_LOCK(PATREF_LOCK, &ctx->ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100498 /* for each lookup type */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200499 while (ctx->expr) {
William Lallemandad8be612016-11-18 19:26:17 +0100500 /* initialise chunk to build new message */
501 chunk_reset(&trash);
502
503 /* execute pattern matching */
504 sample.data.type = SMP_T_STR;
505 sample.flags = SMP_F_CONST;
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200506 sample.data.u.str.data = ctx->chunk.data;
507 sample.data.u.str.area = ctx->chunk.area;
Emeric Brunb5997f72017-07-03 11:34:05 +0200508
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200509 if (ctx->expr->pat_head->match &&
510 sample_convert(&sample, ctx->expr->pat_head->expect_type))
511 pat = ctx->expr->pat_head->match(&sample, ctx->expr, 1);
William Lallemandad8be612016-11-18 19:26:17 +0100512 else
513 pat = NULL;
514
515 /* build return message: set type of match */
516 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200517 if (ctx->expr->pat_head->match == pat_match_fcts[match_method])
William Lallemandad8be612016-11-18 19:26:17 +0100518 break;
519 if (match_method >= PAT_MATCH_NUM)
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200520 chunk_appendf(&trash, "type=unknown(%p)", ctx->expr->pat_head->match);
William Lallemandad8be612016-11-18 19:26:17 +0100521 else
522 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
523
524 /* case sensitive */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200525 if (ctx->expr->mflags & PAT_MF_IGNORE_CASE)
William Lallemandad8be612016-11-18 19:26:17 +0100526 chunk_appendf(&trash, ", case=insensitive");
527 else
528 chunk_appendf(&trash, ", case=sensitive");
529
530 /* Display no match, and set default value */
531 if (!pat) {
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200532 if (ctx->display_flags == PAT_REF_MAP)
William Lallemandad8be612016-11-18 19:26:17 +0100533 chunk_appendf(&trash, ", found=no");
534 else
535 chunk_appendf(&trash, ", match=no");
536 }
537
538 /* Display match and match info */
539 else {
540 /* display match */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200541 if (ctx->display_flags == PAT_REF_MAP)
William Lallemandad8be612016-11-18 19:26:17 +0100542 chunk_appendf(&trash, ", found=yes");
543 else
544 chunk_appendf(&trash, ", match=yes");
545
546 /* display index mode */
547 if (pat->sflags & PAT_SF_TREE)
548 chunk_appendf(&trash, ", idx=tree");
549 else
550 chunk_appendf(&trash, ", idx=list");
551
552 /* display pattern */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200553 if (ctx->display_flags == PAT_REF_MAP) {
William Lallemandad8be612016-11-18 19:26:17 +0100554 if (pat->ref && pat->ref->pattern)
555 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
556 else
557 chunk_appendf(&trash, ", key=unknown");
558 }
559 else {
560 if (pat->ref && pat->ref->pattern)
561 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
562 else
563 chunk_appendf(&trash, ", pattern=unknown");
564 }
565
566 /* display return value */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200567 if (ctx->display_flags == PAT_REF_MAP) {
William Lallemandad8be612016-11-18 19:26:17 +0100568 if (pat->data && pat->ref && pat->ref->sample)
569 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
570 smp_to_type[pat->data->type]);
571 else
572 chunk_appendf(&trash, ", value=none");
573 }
574 }
575
576 chunk_appendf(&trash, "\n");
577
578 /* display response */
Christopher Faulet908628c2022-03-25 16:43:49 +0100579 if (ci_putchk(cs_ic(cs), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100580 /* let's try again later from this stream. We add ourselves into
581 * this stream's users so that it can remove us upon termination.
582 */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200583 HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock);
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200584 cs_rx_room_blk(cs);
William Lallemandad8be612016-11-18 19:26:17 +0100585 return 0;
586 }
587
588 /* get next entry */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200589 ctx->expr = pat_expr_get_next(ctx->expr,
590 &ctx->ref->pat);
William Lallemandad8be612016-11-18 19:26:17 +0100591 }
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200592 HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100593 /* fall through */
594
595 default:
Willy Tarreaua0d62802022-05-03 15:12:21 +0200596 ctx->state = STATE_DONE;
William Lallemandad8be612016-11-18 19:26:17 +0100597 return 1;
598 }
599}
600
601static void cli_release_mlook(struct appctx *appctx)
602{
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200603 struct show_map_ctx *ctx = appctx->svcctx;
604
605 ha_free(&ctx->chunk.area);
William Lallemandad8be612016-11-18 19:26:17 +0100606}
607
608
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200609static int cli_parse_get_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100610{
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200611 struct show_map_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
612
William Lallemandad8be612016-11-18 19:26:17 +0100613 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
614 /* Set flags. */
615 if (args[1][0] == 'm')
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200616 ctx->display_flags = PAT_REF_MAP;
William Lallemandad8be612016-11-18 19:26:17 +0100617 else
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200618 ctx->display_flags = PAT_REF_ACL;
William Lallemandad8be612016-11-18 19:26:17 +0100619
620 /* No parameter. */
621 if (!*args[2] || !*args[3]) {
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200622 if (ctx->display_flags == PAT_REF_MAP)
Willy Tarreau9d008692019-08-09 11:21:01 +0200623 return cli_err(appctx, "Missing map identifier and/or key.\n");
624 else
625 return cli_err(appctx, "Missing ACL identifier and/or key.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100626 }
627
628 /* lookup into the maps */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200629 ctx->ref = pat_ref_lookup_ref(args[2]);
630 if (!ctx->ref) {
631 if (ctx->display_flags == PAT_REF_MAP)
Willy Tarreau9d008692019-08-09 11:21:01 +0200632 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
633 else
634 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100635 }
636
637 /* copy input string. The string must be allocated because
638 * it may be used over multiple iterations. It's released
639 * at the end and upon abort anyway.
640 */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200641 ctx->chunk.data = strlen(args[3]);
642 ctx->chunk.size = ctx->chunk.data + 1;
643 ctx->chunk.area = strdup(args[3]);
644 if (!ctx->chunk.area)
Willy Tarreau9d008692019-08-09 11:21:01 +0200645 return cli_err(appctx, "Out of memory error.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100646
647 return 0;
648 }
649 return 1;
650}
651
Willy Tarreau97218ce2021-04-30 14:57:03 +0200652static int cli_parse_prepare_map(char **args, char *payload, struct appctx *appctx, void *private)
653{
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200654 struct show_map_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
655
Willy Tarreau97218ce2021-04-30 14:57:03 +0200656 if (strcmp(args[1], "map") == 0 ||
657 strcmp(args[1], "acl") == 0) {
658 uint next_gen;
659 char *msg = NULL;
660
661 /* Set ACL or MAP flags. */
662 if (args[1][0] == 'm')
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200663 ctx->display_flags = PAT_REF_MAP;
Willy Tarreau97218ce2021-04-30 14:57:03 +0200664 else
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200665 ctx->display_flags = PAT_REF_ACL;
Willy Tarreau97218ce2021-04-30 14:57:03 +0200666
667 /* lookup into the refs and check the map flag */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200668 ctx->ref = pat_ref_lookup_ref(args[2]);
669 if (!ctx->ref ||
670 !(ctx->ref->flags & ctx->display_flags)) {
671 if (ctx->display_flags == PAT_REF_MAP)
Willy Tarreau97218ce2021-04-30 14:57:03 +0200672 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
673 else
674 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
675 }
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200676 next_gen = pat_ref_newgen(ctx->ref);
Willy Tarreau97218ce2021-04-30 14:57:03 +0200677 return cli_dynmsg(appctx, LOG_INFO, memprintf(&msg, "New version created: %u\n", next_gen));
678 }
679
680 return 0;
681}
682
Emeric Brun8d85aa42017-06-29 15:40:33 +0200683static void cli_release_show_map(struct appctx *appctx)
684{
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200685 struct show_map_ctx *ctx = appctx->svcctx;
686
Willy Tarreauc7e97062022-05-03 15:42:07 +0200687 if (!LIST_ISEMPTY(&ctx->bref.users)) {
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200688 HA_SPIN_LOCK(PATREF_LOCK, &ctx->ref->lock);
Willy Tarreauc7e97062022-05-03 15:42:07 +0200689 LIST_DEL_INIT(&ctx->bref.users);
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200690 HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200691 }
692}
693
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200694static int cli_parse_show_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100695{
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200696 struct show_map_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
697
William Lallemandad8be612016-11-18 19:26:17 +0100698 if (strcmp(args[1], "map") == 0 ||
699 strcmp(args[1], "acl") == 0) {
Willy Tarreau95f753e2021-04-30 12:09:54 +0200700 const char *gen = NULL;
William Lallemandad8be612016-11-18 19:26:17 +0100701
702 /* Set ACL or MAP flags. */
703 if (args[1][0] == 'm')
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200704 ctx->display_flags = PAT_REF_MAP;
William Lallemandad8be612016-11-18 19:26:17 +0100705 else
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200706 ctx->display_flags = PAT_REF_ACL;
William Lallemandad8be612016-11-18 19:26:17 +0100707
708 /* no parameter: display all map available */
709 if (!*args[2]) {
William Lallemandad8be612016-11-18 19:26:17 +0100710 appctx->io_handler = cli_io_handler_pats_list;
711 return 0;
712 }
713
Willy Tarreau95f753e2021-04-30 12:09:54 +0200714 /* For both "map" and "acl" we may have an optional generation
715 * number specified using a "@" character before the pattern
716 * file name.
717 */
718 if (*args[2] == '@') {
719 gen = args[2] + 1;
720 args++;
721 }
722
William Lallemandad8be612016-11-18 19:26:17 +0100723 /* lookup into the refs and check the map flag */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200724 ctx->ref = pat_ref_lookup_ref(args[2]);
725 if (!ctx->ref ||
726 !(ctx->ref->flags & ctx->display_flags)) {
727 if (ctx->display_flags == PAT_REF_MAP)
Willy Tarreau9d008692019-08-09 11:21:01 +0200728 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
729 else
730 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100731 }
Willy Tarreau95f753e2021-04-30 12:09:54 +0200732
Willy Tarreau76f771e2022-05-03 14:12:56 +0200733 /* set the desired generation id in curr_gen */
Willy Tarreau95f753e2021-04-30 12:09:54 +0200734 if (gen)
Willy Tarreau76f771e2022-05-03 14:12:56 +0200735 ctx->curr_gen = str2uic(gen);
Willy Tarreau95f753e2021-04-30 12:09:54 +0200736 else
Willy Tarreau76f771e2022-05-03 14:12:56 +0200737 ctx->curr_gen = ctx->ref->curr_gen;
Willy Tarreau95f753e2021-04-30 12:09:54 +0200738
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200739 LIST_INIT(&ctx->bref.users);
William Lallemandad8be612016-11-18 19:26:17 +0100740 appctx->io_handler = cli_io_handler_pat_list;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200741 appctx->io_release = cli_release_show_map;
William Lallemandad8be612016-11-18 19:26:17 +0100742 return 0;
743 }
744
745 return 0;
746}
747
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200748static int cli_parse_set_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100749{
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200750 struct show_map_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
751
William Lallemandad8be612016-11-18 19:26:17 +0100752 if (strcmp(args[1], "map") == 0) {
753 char *err;
754
755 /* Set flags. */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200756 ctx->display_flags = PAT_REF_MAP;
William Lallemandad8be612016-11-18 19:26:17 +0100757
758 /* Expect three parameters: map name, key and new value. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200759 if (!*args[2] || !*args[3] || !*args[4])
760 return cli_err(appctx, "'set map' expects three parameters: map identifier, key and value.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100761
762 /* Lookup the reference in the maps. */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200763 ctx->ref = pat_ref_lookup_ref(args[2]);
764 if (!ctx->ref)
Willy Tarreau9d008692019-08-09 11:21:01 +0200765 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100766
767 /* If the entry identifier start with a '#', it is considered as
768 * pointer id
769 */
770 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
771 struct pat_ref_elt *ref;
772 long long int conv;
773 char *error;
774
775 /* Convert argument to integer value. */
776 conv = strtoll(&args[3][1], &error, 16);
Willy Tarreau9d008692019-08-09 11:21:01 +0200777 if (*error != '\0')
778 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100779
780 /* Convert and check integer to pointer. */
781 ref = (struct pat_ref_elt *)(long)conv;
Willy Tarreau9d008692019-08-09 11:21:01 +0200782 if ((long long int)(long)ref != conv)
783 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100784
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200785 /* Try to modify the entry. */
William Lallemandad8be612016-11-18 19:26:17 +0100786 err = NULL;
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200787 HA_SPIN_LOCK(PATREF_LOCK, &ctx->ref->lock);
788 if (!pat_ref_set_by_id(ctx->ref, ref, args[4], &err)) {
789 HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +0200790 if (err)
791 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
792 else
793 return cli_err(appctx, "Failed to update an entry.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100794 }
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200795 HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100796 }
797 else {
798 /* Else, use the entry identifier as pattern
799 * string, and update the value.
800 */
801 err = NULL;
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200802 HA_SPIN_LOCK(PATREF_LOCK, &ctx->ref->lock);
803 if (!pat_ref_set(ctx->ref, args[3], args[4], &err)) {
804 HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +0200805 if (err)
806 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
807 else
808 return cli_err(appctx, "Failed to update an entry.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100809 }
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200810 HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100811 }
812
813 /* The set is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100814 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100815 return 0;
816 }
817 return 1;
818}
819
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200820static int cli_parse_add_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100821{
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200822 struct show_map_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
823
William Lallemandad8be612016-11-18 19:26:17 +0100824 if (strcmp(args[1], "map") == 0 ||
825 strcmp(args[1], "acl") == 0) {
Willy Tarreaubb51c442021-04-30 15:23:36 +0200826 const char *gen = NULL;
827 uint genid = 0;
William Lallemandad8be612016-11-18 19:26:17 +0100828 int ret;
829 char *err;
830
831 /* Set flags. */
832 if (args[1][0] == 'm')
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200833 ctx->display_flags = PAT_REF_MAP;
William Lallemandad8be612016-11-18 19:26:17 +0100834 else
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200835 ctx->display_flags = PAT_REF_ACL;
William Lallemandad8be612016-11-18 19:26:17 +0100836
Willy Tarreaubb51c442021-04-30 15:23:36 +0200837 /* For both "map" and "acl" we may have an optional generation
838 * number specified using a "@" character before the pattern
839 * file name.
840 */
841 if (*args[2] == '@') {
842 gen = args[2] + 1;
843 args++;
844 }
845
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200846 /* If the keyword is "map", we expect:
847 * - three parameters if there is no payload
848 * - one parameter if there is a payload
849 * If it is "acl", we expect only two parameters
William Lallemandad8be612016-11-18 19:26:17 +0100850 */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200851 if (ctx->display_flags == PAT_REF_MAP) {
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200852 if ((!payload && (!*args[2] || !*args[3] || !*args[4])) ||
Willy Tarreau9d008692019-08-09 11:21:01 +0200853 (payload && !*args[2]))
854 return cli_err(appctx,
855 "'add map' expects three parameters (map identifier, key and value)"
856 " or one parameter (map identifier) and a payload\n");
William Lallemandad8be612016-11-18 19:26:17 +0100857 }
Willy Tarreau9d008692019-08-09 11:21:01 +0200858 else if (!*args[2] || !*args[3])
859 return cli_err(appctx, "'add acl' expects two parameters: ACL identifier and pattern.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100860
861 /* Lookup for the reference. */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200862 ctx->ref = pat_ref_lookup_ref(args[2]);
863 if (!ctx->ref) {
864 if (ctx->display_flags == PAT_REF_MAP)
Willy Tarreau9d008692019-08-09 11:21:01 +0200865 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
866 else
867 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100868 }
869
Willy Tarreaubb51c442021-04-30 15:23:36 +0200870 if (gen) {
871 genid = str2uic(gen);
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200872 if ((int)(genid - ctx->ref->next_gen) > 0) {
873 if (ctx->display_flags == PAT_REF_MAP)
Willy Tarreaubb51c442021-04-30 15:23:36 +0200874 return cli_err(appctx, "Version number in the future, please use 'prepare map' before.\n");
875 else
876 return cli_err(appctx, "Version number in the future, please use 'prepare acl' before.\n");
877 }
878 }
879
William Lallemandad8be612016-11-18 19:26:17 +0100880 /* The command "add acl" is prohibited if the reference
881 * use samples.
882 */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200883 if ((ctx->display_flags & PAT_REF_ACL) &&
884 (ctx->ref->flags & PAT_REF_SMP)) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200885 return cli_err(appctx,
886 "This ACL is shared with a map containing samples. "
887 "You must use the command 'add map' to add values.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100888 }
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200889
890 /* Add value(s). If no payload is used, key and value are read
891 * from the command line and only one key is set. If a payload
892 * is passed, one key/value pair is read per line till the end
893 * of the payload is reached.
894 */
William Lallemandad8be612016-11-18 19:26:17 +0100895 err = NULL;
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200896
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200897 do {
898 char *key = args[3];
899 char *value = args[4];
900 size_t l;
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200901
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200902 if (payload) {
903 /* key and value passed as payload, one pair per line */
904 if (!*payload)
905 break;
906
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200907 key = payload;
908 l = strcspn(key, " \t");
909 payload += l;
910
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200911 if (!*payload && ctx->display_flags == PAT_REF_MAP)
Willy Tarreau9d008692019-08-09 11:21:01 +0200912 return cli_dynerr(appctx, memprintf(&err, "Missing value for key '%s'.\n", key));
913
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200914 key[l] = 0;
915 payload++;
916
917 /* value */
918 payload += strspn(payload, " \t");
919 value = payload;
920 l = strcspn(value, "\n");
921 payload += l;
922 if (*payload)
923 payload++;
924 value[l] = 0;
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200925 }
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200926
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200927 if (ctx->display_flags != PAT_REF_MAP)
Willy Tarreau4053b032021-04-29 16:55:17 +0200928 value = NULL;
929
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200930 HA_SPIN_LOCK(PATREF_LOCK, &ctx->ref->lock);
931 ret = !!pat_ref_load(ctx->ref, gen ? genid : ctx->ref->curr_gen, key, value, -1, &err);
932 HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock);
Willy Tarreau4053b032021-04-29 16:55:17 +0200933
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200934 if (!ret) {
935 if (err)
936 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
937 else
938 return cli_err(appctx, "Failed to add a key.\n");
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200939 }
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200940 } while (payload && *payload);
William Lallemandad8be612016-11-18 19:26:17 +0100941
942 /* The add is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100943 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100944 return 1;
945 }
946
947 return 0;
948}
949
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200950static int cli_parse_del_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100951{
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200952 struct show_map_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
953
William Lallemandad8be612016-11-18 19:26:17 +0100954 if (args[1][0] == 'm')
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200955 ctx->display_flags = PAT_REF_MAP;
William Lallemandad8be612016-11-18 19:26:17 +0100956 else
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200957 ctx->display_flags = PAT_REF_ACL;
William Lallemandad8be612016-11-18 19:26:17 +0100958
959 /* Expect two parameters: map name and key. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200960 if (!*args[2] || !*args[3]) {
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200961 if (ctx->display_flags == PAT_REF_MAP)
Willy Tarreau9d008692019-08-09 11:21:01 +0200962 return cli_err(appctx, "This command expects two parameters: map identifier and key.\n");
963 else
964 return cli_err(appctx, "This command expects two parameters: ACL identifier and key.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100965 }
966
967 /* Lookup the reference in the maps. */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200968 ctx->ref = pat_ref_lookup_ref(args[2]);
969 if (!ctx->ref ||
970 !(ctx->ref->flags & ctx->display_flags))
Willy Tarreau9d008692019-08-09 11:21:01 +0200971 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100972
973 /* If the entry identifier start with a '#', it is considered as
974 * pointer id
975 */
976 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
977 struct pat_ref_elt *ref;
978 long long int conv;
979 char *error;
980
981 /* Convert argument to integer value. */
982 conv = strtoll(&args[3][1], &error, 16);
Willy Tarreau9d008692019-08-09 11:21:01 +0200983 if (*error != '\0')
984 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100985
986 /* Convert and check integer to pointer. */
987 ref = (struct pat_ref_elt *)(long)conv;
Willy Tarreau9d008692019-08-09 11:21:01 +0200988 if ((long long int)(long)ref != conv)
989 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100990
991 /* Try to delete the entry. */
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200992 HA_SPIN_LOCK(PATREF_LOCK, &ctx->ref->lock);
993 if (!pat_ref_delete_by_id(ctx->ref, ref)) {
994 HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100995 /* The entry is not found, send message. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200996 return cli_err(appctx, "Key not found.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100997 }
Willy Tarreau0fcecc62022-05-03 11:54:47 +0200998 HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100999 }
1000 else {
1001 /* Else, use the entry identifier as pattern
1002 * string and try to delete the entry.
1003 */
Willy Tarreau0fcecc62022-05-03 11:54:47 +02001004 HA_SPIN_LOCK(PATREF_LOCK, &ctx->ref->lock);
1005 if (!pat_ref_delete(ctx->ref, args[3])) {
1006 HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001007 /* The entry is not found, send message. */
Willy Tarreau9d008692019-08-09 11:21:01 +02001008 return cli_err(appctx, "Key not found.\n");
William Lallemandad8be612016-11-18 19:26:17 +01001009 }
Willy Tarreau0fcecc62022-05-03 11:54:47 +02001010 HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001011 }
1012
1013 /* The deletion is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001014 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +01001015 return 1;
1016}
1017
Willy Tarreauff3feeb2021-04-30 13:31:43 +02001018/* continue to clear a map which was started in the parser. The range of
Willy Tarreau76f771e2022-05-03 14:12:56 +02001019 * generations this applies to is taken from ctx->curr_gen for the oldest
1020 * and ctx->prev_gen for the latest.
Willy Tarreauff3feeb2021-04-30 13:31:43 +02001021 */
Willy Tarreaud1d005d2019-12-20 18:22:02 +01001022static int cli_io_handler_clear_map(struct appctx *appctx)
1023{
Willy Tarreau0fcecc62022-05-03 11:54:47 +02001024 struct show_map_ctx *ctx = appctx->svcctx;
Willy Tarreaud1d005d2019-12-20 18:22:02 +01001025 int finished;
1026
Willy Tarreau0fcecc62022-05-03 11:54:47 +02001027 HA_SPIN_LOCK(PATREF_LOCK, &ctx->ref->lock);
Willy Tarreau76f771e2022-05-03 14:12:56 +02001028 finished = pat_ref_purge_range(ctx->ref, ctx->curr_gen, ctx->prev_gen, 100);
Willy Tarreau0fcecc62022-05-03 11:54:47 +02001029 HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock);
Willy Tarreaud1d005d2019-12-20 18:22:02 +01001030
1031 if (!finished) {
1032 /* let's come back later */
Willy Tarreau0698c802022-05-11 14:09:57 +02001033 cs_rx_endp_more(appctx_cs(appctx));
Willy Tarreaud1d005d2019-12-20 18:22:02 +01001034 return 0;
1035 }
1036 return 1;
1037}
1038
Willy Tarreau76f771e2022-05-03 14:12:56 +02001039/* note: sets ctx->curr_gen and ctx->prev_gen to the oldest and
Willy Tarreauff3feeb2021-04-30 13:31:43 +02001040 * latest generations to clear, respectively, and will call the clear_map
1041 * handler.
1042 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001043static int cli_parse_clear_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +01001044{
Willy Tarreau0fcecc62022-05-03 11:54:47 +02001045 struct show_map_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
1046
William Lallemandad8be612016-11-18 19:26:17 +01001047 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
Willy Tarreauff3feeb2021-04-30 13:31:43 +02001048 const char *gen = NULL;
1049
William Lallemandad8be612016-11-18 19:26:17 +01001050 /* Set ACL or MAP flags. */
1051 if (args[1][0] == 'm')
Willy Tarreau0fcecc62022-05-03 11:54:47 +02001052 ctx->display_flags = PAT_REF_MAP;
William Lallemandad8be612016-11-18 19:26:17 +01001053 else
Willy Tarreau0fcecc62022-05-03 11:54:47 +02001054 ctx->display_flags = PAT_REF_ACL;
William Lallemandad8be612016-11-18 19:26:17 +01001055
Willy Tarreauff3feeb2021-04-30 13:31:43 +02001056 /* For both "map" and "acl" we may have an optional generation
1057 * number specified using a "@" character before the pattern
1058 * file name.
1059 */
1060 if (*args[2] == '@') {
1061 gen = args[2] + 1;
1062 args++;
1063 }
1064
William Lallemandad8be612016-11-18 19:26:17 +01001065 /* no parameter */
1066 if (!*args[2]) {
Willy Tarreau0fcecc62022-05-03 11:54:47 +02001067 if (ctx->display_flags == PAT_REF_MAP)
Willy Tarreau9d008692019-08-09 11:21:01 +02001068 return cli_err(appctx, "Missing map identifier.\n");
1069 else
1070 return cli_err(appctx, "Missing ACL identifier.\n");
William Lallemandad8be612016-11-18 19:26:17 +01001071 }
1072
1073 /* lookup into the refs and check the map flag */
Willy Tarreau0fcecc62022-05-03 11:54:47 +02001074 ctx->ref = pat_ref_lookup_ref(args[2]);
1075 if (!ctx->ref ||
1076 !(ctx->ref->flags & ctx->display_flags)) {
1077 if (ctx->display_flags == PAT_REF_MAP)
Willy Tarreau9d008692019-08-09 11:21:01 +02001078 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
1079 else
1080 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +01001081 }
1082
Willy Tarreau76f771e2022-05-03 14:12:56 +02001083 /* set the desired generation id in curr_gen/prev_gen */
Willy Tarreauff3feeb2021-04-30 13:31:43 +02001084 if (gen)
Willy Tarreau76f771e2022-05-03 14:12:56 +02001085 ctx->prev_gen = ctx->curr_gen = str2uic(gen);
Willy Tarreauff3feeb2021-04-30 13:31:43 +02001086 else
Willy Tarreau76f771e2022-05-03 14:12:56 +02001087 ctx->prev_gen = ctx->curr_gen = ctx->ref->curr_gen;
Willy Tarreauff3feeb2021-04-30 13:31:43 +02001088
Willy Tarreaud1d005d2019-12-20 18:22:02 +01001089 /* delegate the clearing to the I/O handler which can yield */
1090 return 0;
William Lallemandad8be612016-11-18 19:26:17 +01001091 }
Willy Tarreaud1d005d2019-12-20 18:22:02 +01001092 return 1;
William Lallemandad8be612016-11-18 19:26:17 +01001093}
1094
Willy Tarreau76f771e2022-05-03 14:12:56 +02001095/* note: sets ctx->curr_gen and ctx->prev_gen to the oldest and
Willy Tarreau7a562ca2021-04-30 15:10:01 +02001096 * latest generations to clear, respectively, and will call the clear_map
1097 * handler.
1098 */
1099static int cli_parse_commit_map(char **args, char *payload, struct appctx *appctx, void *private)
1100{
Willy Tarreau0fcecc62022-05-03 11:54:47 +02001101 struct show_map_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
1102
Willy Tarreau7a562ca2021-04-30 15:10:01 +02001103 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1104 const char *gen = NULL;
1105 uint genid;
1106 uint ret;
1107
1108 /* Set ACL or MAP flags. */
1109 if (args[1][0] == 'm')
Willy Tarreau0fcecc62022-05-03 11:54:47 +02001110 ctx->display_flags = PAT_REF_MAP;
Willy Tarreau7a562ca2021-04-30 15:10:01 +02001111 else
Willy Tarreau0fcecc62022-05-03 11:54:47 +02001112 ctx->display_flags = PAT_REF_ACL;
Willy Tarreau7a562ca2021-04-30 15:10:01 +02001113
1114 if (*args[2] != '@')
1115 return cli_err(appctx, "Missing version number.\n");
1116
1117 /* The generation number is mandatory for a commit. The range
1118 * of generations that get trashed by a commit starts from the
1119 * opposite of the current one and ends at the previous one.
1120 */
1121 gen = args[2] + 1;
1122 genid = str2uic(gen);
Willy Tarreau76f771e2022-05-03 14:12:56 +02001123 ctx->prev_gen = genid - 1;
1124 ctx->curr_gen = ctx->prev_gen - ((~0U) >> 1);
Willy Tarreau7a562ca2021-04-30 15:10:01 +02001125
1126 /* no parameter */
1127 if (!*args[3]) {
Willy Tarreau0fcecc62022-05-03 11:54:47 +02001128 if (ctx->display_flags == PAT_REF_MAP)
Willy Tarreau7a562ca2021-04-30 15:10:01 +02001129 return cli_err(appctx, "Missing map identifier.\n");
1130 else
1131 return cli_err(appctx, "Missing ACL identifier.\n");
1132 }
1133
1134 /* lookup into the refs and check the map flag */
Willy Tarreau0fcecc62022-05-03 11:54:47 +02001135 ctx->ref = pat_ref_lookup_ref(args[3]);
1136 if (!ctx->ref ||
1137 !(ctx->ref->flags & ctx->display_flags)) {
1138 if (ctx->display_flags == PAT_REF_MAP)
Willy Tarreau7a562ca2021-04-30 15:10:01 +02001139 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
1140 else
1141 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
1142 }
1143
Willy Tarreau0fcecc62022-05-03 11:54:47 +02001144 HA_SPIN_LOCK(PATREF_LOCK, &ctx->ref->lock);
1145 if (genid - (ctx->ref->curr_gen + 1) <
1146 ctx->ref->next_gen - ctx->ref->curr_gen)
1147 ret = pat_ref_commit(ctx->ref, genid);
Willy Tarreau7a562ca2021-04-30 15:10:01 +02001148 else
1149 ret = 1;
Willy Tarreau0fcecc62022-05-03 11:54:47 +02001150 HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock);
Willy Tarreau7a562ca2021-04-30 15:10:01 +02001151
1152 if (ret != 0)
1153 return cli_err(appctx, "Version number out of range.\n");
1154
1155 /* delegate the clearing to the I/O handler which can yield */
1156 return 0;
1157 }
1158 return 1;
1159}
1160
William Lallemandad8be612016-11-18 19:26:17 +01001161/* register cli keywords */
1162
1163static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001164 { { "add", "acl", NULL }, "add acl [@<ver>] <acl> <pattern> : add an acl entry", cli_parse_add_map, NULL },
1165 { { "clear", "acl", NULL }, "clear acl [@<ver>] <acl> : clear the contents of this acl", cli_parse_clear_map, cli_io_handler_clear_map, NULL },
1166 { { "commit","acl", NULL }, "commit acl @<ver> <acl> : commit the ACL at this version", cli_parse_commit_map, cli_io_handler_clear_map, NULL },
1167 { { "del", "acl", NULL }, "del acl <acl> [<key>|#<ref>] : delete acl entries matching <key>", cli_parse_del_map, NULL },
1168 { { "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 },
1169 { { "prepare","acl",NULL }, "prepare acl <acl> : prepare a new version for atomic ACL replacement", cli_parse_prepare_map, NULL },
1170 { { "show", "acl", NULL }, "show acl [@<ver>] <acl>] : report available acls or dump an acl's contents", cli_parse_show_map, NULL },
1171 { { "add", "map", NULL }, "add map [@<ver>] <map> <key> <val> : add a map entry (payload supported instead of key/val)", cli_parse_add_map, NULL },
1172 { { "clear", "map", NULL }, "clear map [@<ver>] <map> : clear the contents of this map", cli_parse_clear_map, cli_io_handler_clear_map, NULL },
1173 { { "commit","map", NULL }, "commit map @<ver> <map> : commit the map at this version", cli_parse_commit_map, cli_io_handler_clear_map, NULL },
1174 { { "del", "map", NULL }, "del map <map> [<key>|#<ref>] : delete map entries matching <key>", cli_parse_del_map, NULL },
1175 { { "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 },
1176 { { "prepare","map",NULL }, "prepare map <acl> : prepare a new version for atomic map replacement", cli_parse_prepare_map, NULL },
1177 { { "set", "map", NULL }, "set map <map> [<key>|#<ref>] <value> : modify a map entry", cli_parse_set_map, NULL },
1178 { { "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 +01001179 { { NULL }, NULL, NULL, NULL }
1180}};
1181
Willy Tarreau0108d902018-11-25 19:14:37 +01001182INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemandad8be612016-11-18 19:26:17 +01001183
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +01001184/* Note: must not be declared <const> as its list will be overwritten
1185 *
1186 * For the map_*_int keywords, the output is declared as SMP_T_UINT, but the converter function
1187 * can provide SMP_T_UINT, SMP_T_SINT or SMP_T_BOOL depending on how the patterns found in the
1188 * file can be parsed.
1189 *
1190 * For the map_*_ip keyword, the output is declared as SMP_T_IPV4, but the converter function
1191 * can provide SMP_T_IPV4 or SMP_T_IPV6 depending on the patterns found in the file.
1192 *
1193 * The map_* keywords only emit strings.
1194 *
1195 * The output type is only used during the configuration parsing. It is used for detecting
1196 * compatibility problems.
1197 *
1198 * The arguments are: <file>[,<default value>]
1199 */
1200static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER1edc9712014-12-15 16:18:39 +01001201 { "map", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1202 { "map_str", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1203 { "map_beg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_BEG },
1204 { "map_sub", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_SUB },
1205 { "map_dir", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DIR },
1206 { "map_dom", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DOM },
1207 { "map_end", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_END },
1208 { "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 +01001209 { "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 +02001210 { "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 +01001211 { "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 +01001212
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001213 { "map_str_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_STR },
1214 { "map_beg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_BEG },
1215 { "map_sub_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_SUB },
1216 { "map_dir_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DIR },
1217 { "map_dom_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DOM },
1218 { "map_end_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_END },
1219 { "map_reg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_REG },
1220 { "map_int_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_SINT, SMP_T_SINT, (void *)PAT_MATCH_INT },
1221 { "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 +01001222
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +02001223 { "map_str_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_STR },
1224 { "map_beg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_BEG },
1225 { "map_sub_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_SUB },
1226 { "map_dir_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DIR },
1227 { "map_dom_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DOM },
1228 { "map_end_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_END },
1229 { "map_reg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_REG },
1230 { "map_int_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_SINT, SMP_T_ADDR, (void *)PAT_MATCH_INT },
1231 { "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 +01001232
1233 { /* END */ },
1234}};
1235
Willy Tarreau0108d902018-11-25 19:14:37 +01001236INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);