blob: 080fee85d146754e20cb63a861b0a3c9d0094b75 [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>
14
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020016#include <haproxy/applet-t.h>
17#include <haproxy/arg.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020018#include <haproxy/cli.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020019#include <haproxy/map.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020020#include <haproxy/pattern.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020021#include <haproxy/regex.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/sample.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020023#include <haproxy/stats-t.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020024#include <haproxy/stream_interface.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020025#include <haproxy/tools.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010026
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010027
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020028/* Parse an IPv4 or IPv6 address and store it into the sample.
29 * The output type is IPv4 or IPv6.
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010030 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020031int map_parse_ip(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010032{
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020033 int len = strlen(text);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010034
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020035 if (buf2ip(text, len, &data->u.ipv4)) {
36 data->type = SMP_T_IPV4;
37 return 1;
38 }
39 if (buf2ip6(text, len, &data->u.ipv6)) {
40 data->type = SMP_T_IPV6;
41 return 1;
42 }
43 return 0;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010044}
45
46/* Parse a string and store a pointer to it into the sample. The original
47 * string must be left in memory because we return a direct memory reference.
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010048 * The output type is SMP_T_STR. There is no risk that the data will be
49 * overwritten because sample_conv_map() makes a const sample with this
50 * output.
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010051 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020052int map_parse_str(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010053{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020054 data->u.str.area = (char *)text;
55 data->u.str.data = strlen(text);
56 data->u.str.size = data->u.str.data + 1;
Thierry FOURNIER503bb092015-08-19 08:35:43 +020057 data->type = SMP_T_STR;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010058 return 1;
59}
60
61/* Parse an integer and convert it to a sample. The output type is SINT if the
62 * number is negative, or UINT if it is positive or null. The function returns
63 * zero (error) if the number is too large.
64 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020065int map_parse_int(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010066{
Thierry FOURNIER503bb092015-08-19 08:35:43 +020067 data->type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020068 data->u.sint = read_int64(&text, text + strlen(text));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020069 if (*text != '\0')
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010070 return 0;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010071 return 1;
72}
73
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010074/* This crete and initialize map descriptor.
75 * Return NULL if out of memory error
76 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010077static struct map_descriptor *map_create_descriptor(struct sample_conv *conv)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010078{
79 struct map_descriptor *desc;
80
81 desc = calloc(1, sizeof(*desc));
82 if (!desc)
83 return NULL;
84
85 desc->conv = conv;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010086
87 return desc;
88}
89
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010090/* This function load the map file according with data type declared into
91 * the "struct sample_conv".
92 *
93 * This function choose the indexation type (ebtree or list) according with
94 * the type of match needed.
95 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +020096int sample_load_map(struct arg *arg, struct sample_conv *conv,
97 const char *file, int line, char **err)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010098{
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010099 struct map_descriptor *desc;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100100
Christopher Faulet0eb967d2020-08-05 23:23:37 +0200101 if (!(global.mode & MODE_STARTING)) {
102 memprintf(err, "map: cannot load map at runtime");
103 return 0;
104 }
105
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100106 /* create new map descriptor */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100107 desc = map_create_descriptor(conv);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100108 if (!desc) {
109 memprintf(err, "out of memory");
110 return 0;
111 }
112
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100113 /* Initialize pattern */
114 pattern_init_head(&desc->pat);
115
116 /* This is original pattern, must free */
117 desc->do_free = 1;
118
119 /* Set the match method. */
Thierry FOURNIER1edc9712014-12-15 16:18:39 +0100120 desc->pat.match = pat_match_fcts[(long)conv->private];
121 desc->pat.parse = pat_parse_fcts[(long)conv->private];
122 desc->pat.index = pat_index_fcts[(long)conv->private];
Thierry FOURNIER1edc9712014-12-15 16:18:39 +0100123 desc->pat.prune = pat_prune_fcts[(long)conv->private];
124 desc->pat.expect_type = pat_match_types[(long)conv->private];
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100125
126 /* Set the output parse method. */
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100127 switch (desc->conv->out_type) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100128 case SMP_T_STR: desc->pat.parse_smp = map_parse_str; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200129 case SMP_T_SINT: desc->pat.parse_smp = map_parse_int; break;
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200130 case SMP_T_ADDR: desc->pat.parse_smp = map_parse_ip; break;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100131 default:
132 memprintf(err, "map: internal haproxy error: no default parse case for the input type <%d>.",
133 conv->out_type);
Andreas Seltenreich78f35952016-03-03 20:32:23 +0100134 free(desc);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100135 return 0;
136 }
137
Thierry FOURNIER39bef452014-01-29 13:29:45 +0100138 /* Load map. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200139 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 +0100140 1, err, file, line))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100141 return 0;
Thierry FOURNIER0ffe78c2013-12-05 14:40:25 +0100142
Willy Tarreauaa5801b2019-04-19 11:35:22 +0200143 /* the maps of type IP support a string as default value. This
144 * string can be an ipv4 or an ipv6, we must convert it.
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200145 */
Willy Tarreauaa5801b2019-04-19 11:35:22 +0200146 if (arg[1].type != ARGT_STOP && desc->conv->out_type == SMP_T_ADDR) {
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200147 struct sample_data data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200148 if (!map_parse_ip(arg[1].data.str.area, &data)) {
149 memprintf(err, "map: cannot parse default ip <%s>.",
150 arg[1].data.str.area);
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200151 return 0;
152 }
Christopher Faulet6ad7df42020-08-07 11:45:18 +0200153 chunk_destroy(&arg[1].data.str);
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200154 if (data.type == SMP_T_IPV4) {
155 arg[1].type = ARGT_IPV4;
156 arg[1].data.ipv4 = data.u.ipv4;
157 } else {
158 arg[1].type = ARGT_IPV6;
159 arg[1].data.ipv6 = data.u.ipv6;
160 }
161 }
162
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100163 /* replace the first argument by this definition */
Christopher Faulet6ad7df42020-08-07 11:45:18 +0200164 chunk_destroy(&arg[0].data.str);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100165 arg[0].type = ARGT_MAP;
166 arg[0].data.map = desc;
167
168 return 1;
169}
170
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200171static int sample_conv_map(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100172{
173 struct map_descriptor *desc;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100174 struct pattern *pat;
Willy Tarreau83061a82018-07-13 11:56:34 +0200175 struct buffer *str;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100176
177 /* get config */
178 desc = arg_p[0].data.map;
179
180 /* Execute the match function. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100181 pat = pattern_exec_match(&desc->pat, smp, 1);
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100182
183 /* Match case. */
184 if (pat) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200185 if (pat->data) {
Thierry Fournier8feaa662016-02-10 22:55:20 +0100186 /* In the regm case, merge the sample with the input. */
187 if ((long)private == PAT_MATCH_REGM) {
Emeric Brun27102212018-07-17 09:47:07 -0400188 struct buffer *tmptrash;
Willy Tarreau2842e052018-08-22 04:55:43 +0200189 int len;
Emeric Brun27102212018-07-17 09:47:07 -0400190
191 /* Copy the content of the sample because it could
192 be scratched by incoming get_trash_chunk */
193 tmptrash = alloc_trash_chunk();
194 if (!tmptrash)
195 return 0;
196
197 tmptrash->data = smp->data.u.str.data;
198 if (tmptrash->data > (tmptrash->size-1))
199 tmptrash->data = tmptrash->size-1;
200
201 memcpy(tmptrash->area, smp->data.u.str.area, tmptrash->data);
202 tmptrash->area[tmptrash->data] = 0;
203
Thierry Fournier8feaa662016-02-10 22:55:20 +0100204 str = get_trash_chunk();
Willy Tarreau2842e052018-08-22 04:55:43 +0200205 len = exp_replace(str->area, str->size,
206 tmptrash->area,
207 pat->data->u.str.area,
208 (regmatch_t *)smp->ctx.a[0]);
Nenad Merdanovic646b7742019-04-12 22:54:28 +0200209 free_trash_chunk(tmptrash);
210
Willy Tarreau2842e052018-08-22 04:55:43 +0200211 if (len == -1)
Thierry Fournier8feaa662016-02-10 22:55:20 +0100212 return 0;
Willy Tarreau2842e052018-08-22 04:55:43 +0200213
214 str->data = len;
Thierry Fournier8feaa662016-02-10 22:55:20 +0100215 smp->data.u.str = *str;
216 return 1;
217 }
218 /* Copy sample. */
Thierry FOURNIER5cc18d42015-08-19 09:02:36 +0200219 smp->data = *pat->data;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100220 smp->flags |= SMP_F_CONST;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100221 return 1;
222 }
223
224 /* Return just int sample containing 1. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200225 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200226 smp->data.u.sint = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100227 return 1;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100228 }
229
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800230 /* If no default value available, the converter fails. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100231 if (arg_p[1].type == ARGT_STOP)
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100232 return 0;
233
234 /* Return the default value. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100235 switch (desc->conv->out_type) {
236
237 case SMP_T_STR:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200238 smp->data.type = SMP_T_STR;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100239 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200240 smp->data.u.str = arg_p[1].data.str;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100241 break;
242
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200243 case SMP_T_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200244 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200245 smp->data.u.sint = arg_p[1].data.sint;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100246 break;
247
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200248 case SMP_T_ADDR:
249 if (arg_p[1].type == ARGT_IPV4) {
250 smp->data.type = SMP_T_IPV4;
251 smp->data.u.ipv4 = arg_p[1].data.ipv4;
252 } else {
253 smp->data.type = SMP_T_IPV6;
254 smp->data.u.ipv6 = arg_p[1].data.ipv6;
255 }
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100256 break;
257 }
258
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100259 return 1;
260}
261
William Lallemandad8be612016-11-18 19:26:17 +0100262/* This function is used with map and acl management. It permits to browse
263 * each reference. The variable <getnext> must contain the current node,
264 * <end> point to the root node and the <flags> permit to filter required
265 * nodes.
266 */
267static inline
268struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
269 unsigned int flags)
270{
271 struct pat_ref *ref = getnext;
272
273 while (1) {
274
275 /* Get next list entry. */
276 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
277
278 /* If the entry is the last of the list, return NULL. */
279 if (&ref->list == end)
280 return NULL;
281
282 /* If the entry match the flag, return it. */
283 if (ref->flags & flags)
284 return ref;
285 }
286}
287
288static inline
289struct pat_ref *pat_ref_lookup_ref(const char *reference)
290{
291 int id;
292 char *error;
293
294 /* If the reference starts by a '#', this is numeric id. */
295 if (reference[0] == '#') {
296 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
297 id = strtol(reference + 1, &error, 10);
298 if (*error != '\0')
299 return NULL;
300
301 /* Perform the unique id lookup. */
302 return pat_ref_lookupid(id);
303 }
304
305 /* Perform the string lookup. */
306 return pat_ref_lookup(reference);
307}
308
309/* This function is used with map and acl management. It permits to browse
310 * each reference.
311 */
312static inline
313struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
314{
315 struct pattern_expr *expr;
316 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
317 if (&expr->list == end)
318 return NULL;
319 return expr;
320}
321
Willy Tarreau95f753e2021-04-30 12:09:54 +0200322/* expects the current generation ID in appctx->cli.cli.i0 */
William Lallemandad8be612016-11-18 19:26:17 +0100323static int cli_io_handler_pat_list(struct appctx *appctx)
324{
325 struct stream_interface *si = appctx->owner;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200326 struct pat_ref_elt *elt;
327
328 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
329 /* If we're forced to shut down, we might have to remove our
330 * reference to the last ref_elt being dumped.
331 */
332 if (appctx->st2 == STAT_ST_LIST) {
Dragan Dosen336a11f2018-05-04 16:27:15 +0200333 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users)) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200334 LIST_DELETE(&appctx->ctx.map.bref.users);
Dragan Dosen336a11f2018-05-04 16:27:15 +0200335 LIST_INIT(&appctx->ctx.map.bref.users);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200336 }
337 }
338 return 1;
339 }
William Lallemandad8be612016-11-18 19:26:17 +0100340
341 switch (appctx->st2) {
342
343 case STAT_ST_INIT:
Emeric Brun8d85aa42017-06-29 15:40:33 +0200344 /* the function had not been called yet, let's prepare the
345 * buffer for a response. We initialize the current stream
346 * pointer to the first in the global list. When a target
347 * stream is being destroyed, it is responsible for updating
348 * this pointer. We know we have reached the end when this
349 * pointer points back to the head of the streams list.
350 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100351 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200352 LIST_INIT(&appctx->ctx.map.bref.users);
353 appctx->ctx.map.bref.ref = appctx->ctx.map.ref->head.n;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100354 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100355 appctx->st2 = STAT_ST_LIST;
356 /* fall through */
357
358 case STAT_ST_LIST:
Emeric Brunb5997f72017-07-03 11:34:05 +0200359
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100360 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200361
Emeric Brun8d85aa42017-06-29 15:40:33 +0200362 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users)) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200363 LIST_DELETE(&appctx->ctx.map.bref.users);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200364 LIST_INIT(&appctx->ctx.map.bref.users);
365 }
366
367 while (appctx->ctx.map.bref.ref != &appctx->ctx.map.ref->head) {
William Lallemandad8be612016-11-18 19:26:17 +0100368 chunk_reset(&trash);
369
Emeric Brun8d85aa42017-06-29 15:40:33 +0200370 elt = LIST_ELEM(appctx->ctx.map.bref.ref, struct pat_ref_elt *, list);
371
Willy Tarreau95f753e2021-04-30 12:09:54 +0200372 if (elt->gen_id != appctx->ctx.cli.i0)
Willy Tarreauc93da692020-10-29 09:41:34 +0100373 goto skip;
374
William Lallemandad8be612016-11-18 19:26:17 +0100375 /* build messages */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200376 if (elt->sample)
William Lallemandad8be612016-11-18 19:26:17 +0100377 chunk_appendf(&trash, "%p %s %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200378 elt, elt->pattern,
379 elt->sample);
William Lallemandad8be612016-11-18 19:26:17 +0100380 else
381 chunk_appendf(&trash, "%p %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200382 elt, elt->pattern);
William Lallemandad8be612016-11-18 19:26:17 +0100383
Willy Tarreau06d80a92017-10-19 14:32:15 +0200384 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100385 /* let's try again later from this stream. We add ourselves into
386 * this stream's users so that it can remove us upon termination.
387 */
Willy Tarreau2b718102021-04-21 07:32:39 +0200388 LIST_APPEND(&elt->back_refs, &appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100389 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +0100390 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100391 return 0;
392 }
Willy Tarreauc93da692020-10-29 09:41:34 +0100393 skip:
William Lallemandad8be612016-11-18 19:26:17 +0100394 /* get next list entry and check the end of the list */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200395 appctx->ctx.map.bref.ref = elt->list.n;
William Lallemandad8be612016-11-18 19:26:17 +0100396 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100397 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100398 /* fall through */
399
400 default:
401 appctx->st2 = STAT_ST_FIN;
402 return 1;
403 }
404}
405
406static int cli_io_handler_pats_list(struct appctx *appctx)
407{
408 struct stream_interface *si = appctx->owner;
409
410 switch (appctx->st2) {
411 case STAT_ST_INIT:
412 /* Display the column headers. If the message cannot be sent,
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800413 * quit the function with returning 0. The function is called
414 * later and restarted at the state "STAT_ST_INIT".
William Lallemandad8be612016-11-18 19:26:17 +0100415 */
416 chunk_reset(&trash);
417 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +0200418 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +0100419 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100420 return 0;
421 }
422
423 /* Now, we start the browsing of the references lists.
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800424 * Note that the following call to LIST_ELEM returns a bad pointer. The only
William Lallemandad8be612016-11-18 19:26:17 +0100425 * available field of this pointer is <list>. It is used with the function
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800426 * pat_list_get_next() for returning the first available entry
William Lallemandad8be612016-11-18 19:26:17 +0100427 */
428 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
429 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
430 appctx->ctx.map.display_flags);
431 appctx->st2 = STAT_ST_LIST;
432 /* fall through */
433
434 case STAT_ST_LIST:
435 while (appctx->ctx.map.ref) {
436 chunk_reset(&trash);
437
438 /* Build messages. If the reference is used by another category than
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800439 * the listed categories, display the information in the message.
William Lallemandad8be612016-11-18 19:26:17 +0100440 */
Willy Tarreaue3a42a62021-04-30 10:55:53 +0200441 chunk_appendf(&trash, "%d (%s) %s. curr_ver=%u next_ver=%u\n", appctx->ctx.map.ref->unique_id,
William Lallemandad8be612016-11-18 19:26:17 +0100442 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
Willy Tarreaue3a42a62021-04-30 10:55:53 +0200443 appctx->ctx.map.ref->display, appctx->ctx.map.ref->curr_gen, appctx->ctx.map.ref->next_gen);
William Lallemandad8be612016-11-18 19:26:17 +0100444
Willy Tarreau06d80a92017-10-19 14:32:15 +0200445 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100446 /* let's try again later from this stream. We add ourselves into
447 * this stream's users so that it can remove us upon termination.
448 */
Willy Tarreaudb398432018-11-15 11:08:52 +0100449 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100450 return 0;
451 }
452
453 /* get next list entry and check the end of the list */
454 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
455 appctx->ctx.map.display_flags);
456 }
457
William Lallemandad8be612016-11-18 19:26:17 +0100458 /* fall through */
459
460 default:
461 appctx->st2 = STAT_ST_FIN;
462 return 1;
463 }
464 return 0;
465}
466
467static int cli_io_handler_map_lookup(struct appctx *appctx)
468{
469 struct stream_interface *si = appctx->owner;
470 struct sample sample;
471 struct pattern *pat;
472 int match_method;
473
474 switch (appctx->st2) {
475 case STAT_ST_INIT:
476 /* Init to the first entry. The list cannot be change */
477 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
478 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
479 appctx->st2 = STAT_ST_LIST;
480 /* fall through */
481
482 case STAT_ST_LIST:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100483 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100484 /* for each lookup type */
485 while (appctx->ctx.map.expr) {
486 /* initialise chunk to build new message */
487 chunk_reset(&trash);
488
489 /* execute pattern matching */
490 sample.data.type = SMP_T_STR;
491 sample.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200492 sample.data.u.str.data = appctx->ctx.map.chunk.data;
493 sample.data.u.str.area = appctx->ctx.map.chunk.area;
Emeric Brunb5997f72017-07-03 11:34:05 +0200494
William Lallemandad8be612016-11-18 19:26:17 +0100495 if (appctx->ctx.map.expr->pat_head->match &&
496 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
497 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
498 else
499 pat = NULL;
500
501 /* build return message: set type of match */
502 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
503 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
504 break;
505 if (match_method >= PAT_MATCH_NUM)
506 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
507 else
508 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
509
510 /* case sensitive */
511 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
512 chunk_appendf(&trash, ", case=insensitive");
513 else
514 chunk_appendf(&trash, ", case=sensitive");
515
516 /* Display no match, and set default value */
517 if (!pat) {
518 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
519 chunk_appendf(&trash, ", found=no");
520 else
521 chunk_appendf(&trash, ", match=no");
522 }
523
524 /* Display match and match info */
525 else {
526 /* display match */
527 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
528 chunk_appendf(&trash, ", found=yes");
529 else
530 chunk_appendf(&trash, ", match=yes");
531
532 /* display index mode */
533 if (pat->sflags & PAT_SF_TREE)
534 chunk_appendf(&trash, ", idx=tree");
535 else
536 chunk_appendf(&trash, ", idx=list");
537
538 /* display pattern */
539 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
540 if (pat->ref && pat->ref->pattern)
541 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
542 else
543 chunk_appendf(&trash, ", key=unknown");
544 }
545 else {
546 if (pat->ref && pat->ref->pattern)
547 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
548 else
549 chunk_appendf(&trash, ", pattern=unknown");
550 }
551
552 /* display return value */
553 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
554 if (pat->data && pat->ref && pat->ref->sample)
555 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
556 smp_to_type[pat->data->type]);
557 else
558 chunk_appendf(&trash, ", value=none");
559 }
560 }
561
562 chunk_appendf(&trash, "\n");
563
564 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200565 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100566 /* let's try again later from this stream. We add ourselves into
567 * this stream's users so that it can remove us upon termination.
568 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100569 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +0100570 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100571 return 0;
572 }
573
574 /* get next entry */
575 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
576 &appctx->ctx.map.ref->pat);
577 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100578 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100579 /* fall through */
580
581 default:
582 appctx->st2 = STAT_ST_FIN;
William Lallemandad8be612016-11-18 19:26:17 +0100583 return 1;
584 }
585}
586
587static void cli_release_mlook(struct appctx *appctx)
588{
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100589 ha_free(&appctx->ctx.map.chunk.area);
William Lallemandad8be612016-11-18 19:26:17 +0100590}
591
592
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200593static int cli_parse_get_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100594{
595 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
596 /* Set flags. */
597 if (args[1][0] == 'm')
598 appctx->ctx.map.display_flags = PAT_REF_MAP;
599 else
600 appctx->ctx.map.display_flags = PAT_REF_ACL;
601
602 /* No parameter. */
603 if (!*args[2] || !*args[3]) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200604 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
605 return cli_err(appctx, "Missing map identifier and/or key.\n");
606 else
607 return cli_err(appctx, "Missing ACL identifier and/or key.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100608 }
609
610 /* lookup into the maps */
611 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
612 if (!appctx->ctx.map.ref) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200613 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
614 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
615 else
616 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100617 }
618
619 /* copy input string. The string must be allocated because
620 * it may be used over multiple iterations. It's released
621 * at the end and upon abort anyway.
622 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200623 appctx->ctx.map.chunk.data = strlen(args[3]);
624 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.data + 1;
625 appctx->ctx.map.chunk.area = strdup(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200626 if (!appctx->ctx.map.chunk.area)
627 return cli_err(appctx, "Out of memory error.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100628
629 return 0;
630 }
631 return 1;
632}
633
Emeric Brun8d85aa42017-06-29 15:40:33 +0200634static void cli_release_show_map(struct appctx *appctx)
635{
636 if (appctx->st2 == STAT_ST_LIST) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100637 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200638 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users))
Willy Tarreau2b718102021-04-21 07:32:39 +0200639 LIST_DELETE(&appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100640 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200641 }
642}
643
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200644static int cli_parse_show_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100645{
646 if (strcmp(args[1], "map") == 0 ||
647 strcmp(args[1], "acl") == 0) {
Willy Tarreau95f753e2021-04-30 12:09:54 +0200648 const char *gen = NULL;
William Lallemandad8be612016-11-18 19:26:17 +0100649
650 /* Set ACL or MAP flags. */
651 if (args[1][0] == 'm')
652 appctx->ctx.map.display_flags = PAT_REF_MAP;
653 else
654 appctx->ctx.map.display_flags = PAT_REF_ACL;
655
656 /* no parameter: display all map available */
657 if (!*args[2]) {
William Lallemandad8be612016-11-18 19:26:17 +0100658 appctx->io_handler = cli_io_handler_pats_list;
659 return 0;
660 }
661
Willy Tarreau95f753e2021-04-30 12:09:54 +0200662 /* For both "map" and "acl" we may have an optional generation
663 * number specified using a "@" character before the pattern
664 * file name.
665 */
666 if (*args[2] == '@') {
667 gen = args[2] + 1;
668 args++;
669 }
670
William Lallemandad8be612016-11-18 19:26:17 +0100671 /* lookup into the refs and check the map flag */
672 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
673 if (!appctx->ctx.map.ref ||
674 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200675 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
676 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
677 else
678 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100679 }
Willy Tarreau95f753e2021-04-30 12:09:54 +0200680
681 /* set the desired generation id in cli.i0 */
682 if (gen)
683 appctx->ctx.cli.i0 = str2uic(gen);
684 else
685 appctx->ctx.cli.i0 = appctx->ctx.map.ref->curr_gen;
686
William Lallemandad8be612016-11-18 19:26:17 +0100687 appctx->io_handler = cli_io_handler_pat_list;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200688 appctx->io_release = cli_release_show_map;
William Lallemandad8be612016-11-18 19:26:17 +0100689 return 0;
690 }
691
692 return 0;
693}
694
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200695static int cli_parse_set_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100696{
697 if (strcmp(args[1], "map") == 0) {
698 char *err;
699
700 /* Set flags. */
701 appctx->ctx.map.display_flags = PAT_REF_MAP;
702
703 /* Expect three parameters: map name, key and new value. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200704 if (!*args[2] || !*args[3] || !*args[4])
705 return cli_err(appctx, "'set map' expects three parameters: map identifier, key and value.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100706
707 /* Lookup the reference in the maps. */
708 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200709 if (!appctx->ctx.map.ref)
710 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100711
712 /* If the entry identifier start with a '#', it is considered as
713 * pointer id
714 */
715 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
716 struct pat_ref_elt *ref;
717 long long int conv;
718 char *error;
719
720 /* Convert argument to integer value. */
721 conv = strtoll(&args[3][1], &error, 16);
Willy Tarreau9d008692019-08-09 11:21:01 +0200722 if (*error != '\0')
723 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100724
725 /* Convert and check integer to pointer. */
726 ref = (struct pat_ref_elt *)(long)conv;
Willy Tarreau9d008692019-08-09 11:21:01 +0200727 if ((long long int)(long)ref != conv)
728 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100729
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200730 /* Try to modify the entry. */
William Lallemandad8be612016-11-18 19:26:17 +0100731 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100732 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100733 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100734 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +0200735 if (err)
736 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
737 else
738 return cli_err(appctx, "Failed to update an entry.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100739 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100740 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100741 }
742 else {
743 /* Else, use the entry identifier as pattern
744 * string, and update the value.
745 */
746 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100747 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100748 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100749 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +0200750 if (err)
751 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
752 else
753 return cli_err(appctx, "Failed to update an entry.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100754 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100755 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100756 }
757
758 /* The set is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100759 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100760 return 0;
761 }
762 return 1;
763}
764
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200765static int cli_parse_add_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100766{
767 if (strcmp(args[1], "map") == 0 ||
768 strcmp(args[1], "acl") == 0) {
769 int ret;
770 char *err;
771
772 /* Set flags. */
773 if (args[1][0] == 'm')
774 appctx->ctx.map.display_flags = PAT_REF_MAP;
775 else
776 appctx->ctx.map.display_flags = PAT_REF_ACL;
777
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200778 /* If the keyword is "map", we expect:
779 * - three parameters if there is no payload
780 * - one parameter if there is a payload
781 * If it is "acl", we expect only two parameters
William Lallemandad8be612016-11-18 19:26:17 +0100782 */
783 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200784 if ((!payload && (!*args[2] || !*args[3] || !*args[4])) ||
Willy Tarreau9d008692019-08-09 11:21:01 +0200785 (payload && !*args[2]))
786 return cli_err(appctx,
787 "'add map' expects three parameters (map identifier, key and value)"
788 " or one parameter (map identifier) and a payload\n");
William Lallemandad8be612016-11-18 19:26:17 +0100789 }
Willy Tarreau9d008692019-08-09 11:21:01 +0200790 else if (!*args[2] || !*args[3])
791 return cli_err(appctx, "'add acl' expects two parameters: ACL identifier and pattern.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100792
793 /* Lookup for the reference. */
794 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
795 if (!appctx->ctx.map.ref) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200796 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
797 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
798 else
799 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100800 }
801
802 /* The command "add acl" is prohibited if the reference
803 * use samples.
804 */
805 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
806 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200807 return cli_err(appctx,
808 "This ACL is shared with a map containing samples. "
809 "You must use the command 'add map' to add values.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100810 }
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200811
812 /* Add value(s). If no payload is used, key and value are read
813 * from the command line and only one key is set. If a payload
814 * is passed, one key/value pair is read per line till the end
815 * of the payload is reached.
816 */
William Lallemandad8be612016-11-18 19:26:17 +0100817 err = NULL;
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200818
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200819 do {
820 char *key = args[3];
821 char *value = args[4];
822 size_t l;
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200823
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200824 if (payload) {
825 /* key and value passed as payload, one pair per line */
826 if (!*payload)
827 break;
828
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200829 key = payload;
830 l = strcspn(key, " \t");
831 payload += l;
832
Willy Tarreau9d008692019-08-09 11:21:01 +0200833 if (!*payload && appctx->ctx.map.display_flags == PAT_REF_MAP)
834 return cli_dynerr(appctx, memprintf(&err, "Missing value for key '%s'.\n", key));
835
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200836 key[l] = 0;
837 payload++;
838
839 /* value */
840 payload += strspn(payload, " \t");
841 value = payload;
842 l = strcspn(value, "\n");
843 payload += l;
844 if (*payload)
845 payload++;
846 value[l] = 0;
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200847 }
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200848
Willy Tarreau4053b032021-04-29 16:55:17 +0200849 if (appctx->ctx.map.display_flags != PAT_REF_MAP)
850 value = NULL;
851
852 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
853 ret = !!pat_ref_load(appctx->ctx.map.ref, appctx->ctx.map.ref->curr_gen, key, value, -1, &err);
854 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
855
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200856 if (!ret) {
857 if (err)
858 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
859 else
860 return cli_err(appctx, "Failed to add a key.\n");
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200861 }
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200862 } while (payload && *payload);
William Lallemandad8be612016-11-18 19:26:17 +0100863
864 /* The add is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100865 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100866 return 1;
867 }
868
869 return 0;
870}
871
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200872static int cli_parse_del_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100873{
874 if (args[1][0] == 'm')
875 appctx->ctx.map.display_flags = PAT_REF_MAP;
876 else
877 appctx->ctx.map.display_flags = PAT_REF_ACL;
878
879 /* Expect two parameters: map name and key. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200880 if (!*args[2] || !*args[3]) {
881 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
882 return cli_err(appctx, "This command expects two parameters: map identifier and key.\n");
883 else
884 return cli_err(appctx, "This command expects two parameters: ACL identifier and key.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100885 }
886
887 /* Lookup the reference in the maps. */
888 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
889 if (!appctx->ctx.map.ref ||
Willy Tarreau9d008692019-08-09 11:21:01 +0200890 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags))
891 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100892
893 /* If the entry identifier start with a '#', it is considered as
894 * pointer id
895 */
896 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
897 struct pat_ref_elt *ref;
898 long long int conv;
899 char *error;
900
901 /* Convert argument to integer value. */
902 conv = strtoll(&args[3][1], &error, 16);
Willy Tarreau9d008692019-08-09 11:21:01 +0200903 if (*error != '\0')
904 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100905
906 /* Convert and check integer to pointer. */
907 ref = (struct pat_ref_elt *)(long)conv;
Willy Tarreau9d008692019-08-09 11:21:01 +0200908 if ((long long int)(long)ref != conv)
909 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100910
911 /* Try to delete the entry. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100912 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100913 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100914 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100915 /* The entry is not found, send message. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200916 return cli_err(appctx, "Key not found.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100917 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100918 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100919 }
920 else {
921 /* Else, use the entry identifier as pattern
922 * string and try to delete the entry.
923 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100924 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100925 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100926 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100927 /* The entry is not found, send message. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200928 return cli_err(appctx, "Key not found.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100929 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100930 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100931 }
932
933 /* The deletion is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100934 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100935 return 1;
936}
937
938
Willy Tarreaud1d005d2019-12-20 18:22:02 +0100939/* continue to clear a map which was started in the parser */
940static int cli_io_handler_clear_map(struct appctx *appctx)
941{
942 struct stream_interface *si = appctx->owner;
943 int finished;
944
945 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
946 finished = pat_ref_prune(appctx->ctx.map.ref);
947 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
948
949 if (!finished) {
950 /* let's come back later */
951 si_rx_endp_more(si);
952 return 0;
953 }
954 return 1;
955}
956
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200957static int cli_parse_clear_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100958{
959 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
960 /* Set ACL or MAP flags. */
961 if (args[1][0] == 'm')
962 appctx->ctx.map.display_flags = PAT_REF_MAP;
963 else
964 appctx->ctx.map.display_flags = PAT_REF_ACL;
965
966 /* no parameter */
967 if (!*args[2]) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200968 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
969 return cli_err(appctx, "Missing map identifier.\n");
970 else
971 return cli_err(appctx, "Missing ACL identifier.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100972 }
973
974 /* lookup into the refs and check the map flag */
975 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
976 if (!appctx->ctx.map.ref ||
977 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200978 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
979 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
980 else
981 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100982 }
983
Willy Tarreaud1d005d2019-12-20 18:22:02 +0100984 /* delegate the clearing to the I/O handler which can yield */
985 return 0;
William Lallemandad8be612016-11-18 19:26:17 +0100986 }
Willy Tarreaud1d005d2019-12-20 18:22:02 +0100987 return 1;
William Lallemandad8be612016-11-18 19:26:17 +0100988}
989
990/* register cli keywords */
991
992static struct cli_kw_list cli_kws = {{ },{
993 { { "add", "acl", NULL }, "add acl : add acl entry", cli_parse_add_map, NULL },
Willy Tarreaud1d005d2019-12-20 18:22:02 +0100994 { { "clear", "acl", NULL }, "clear acl <id> : clear the content of this acl", cli_parse_clear_map, cli_io_handler_clear_map, NULL },
William Lallemandad8be612016-11-18 19:26:17 +0100995 { { "del", "acl", NULL }, "del acl : delete acl entry", cli_parse_del_map, NULL },
996 { { "get", "acl", NULL }, "get acl : report the patterns matching a sample for an ACL", cli_parse_get_map, cli_io_handler_map_lookup, cli_release_mlook },
Willy Tarreau95f753e2021-04-30 12:09:54 +0200997 { { "show", "acl", NULL }, "show acl [@ver] [id] : report available acls or dump an acl's contents", cli_parse_show_map, NULL },
William Lallemandad8be612016-11-18 19:26:17 +0100998 { { "add", "map", NULL }, "add map : add map entry", cli_parse_add_map, NULL },
Willy Tarreaud1d005d2019-12-20 18:22:02 +0100999 { { "clear", "map", NULL }, "clear map <id> : clear the content of this map", cli_parse_clear_map, cli_io_handler_clear_map, NULL },
William Lallemandad8be612016-11-18 19:26:17 +01001000 { { "del", "map", NULL }, "del map : delete map entry", cli_parse_del_map, NULL },
Nenad Merdanovic96c15712017-03-12 22:01:36 +01001001 { { "get", "map", NULL }, "get map : report the keys and values matching a sample for a map", cli_parse_get_map, cli_io_handler_map_lookup, cli_release_mlook },
William Lallemandad8be612016-11-18 19:26:17 +01001002 { { "set", "map", NULL }, "set map : modify map entry", cli_parse_set_map, NULL },
Willy Tarreau95f753e2021-04-30 12:09:54 +02001003 { { "show", "map", NULL }, "show map [@ver] [id] : report available maps or dump a map's contents", cli_parse_show_map, NULL },
William Lallemandad8be612016-11-18 19:26:17 +01001004 { { NULL }, NULL, NULL, NULL }
1005}};
1006
Willy Tarreau0108d902018-11-25 19:14:37 +01001007INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemandad8be612016-11-18 19:26:17 +01001008
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +01001009/* Note: must not be declared <const> as its list will be overwritten
1010 *
1011 * For the map_*_int keywords, the output is declared as SMP_T_UINT, but the converter function
1012 * can provide SMP_T_UINT, SMP_T_SINT or SMP_T_BOOL depending on how the patterns found in the
1013 * file can be parsed.
1014 *
1015 * For the map_*_ip keyword, the output is declared as SMP_T_IPV4, but the converter function
1016 * can provide SMP_T_IPV4 or SMP_T_IPV6 depending on the patterns found in the file.
1017 *
1018 * The map_* keywords only emit strings.
1019 *
1020 * The output type is only used during the configuration parsing. It is used for detecting
1021 * compatibility problems.
1022 *
1023 * The arguments are: <file>[,<default value>]
1024 */
1025static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER1edc9712014-12-15 16:18:39 +01001026 { "map", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1027 { "map_str", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1028 { "map_beg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_BEG },
1029 { "map_sub", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_SUB },
1030 { "map_dir", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DIR },
1031 { "map_dom", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DOM },
1032 { "map_end", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_END },
1033 { "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 +01001034 { "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 +02001035 { "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 +01001036 { "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 +01001037
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001038 { "map_str_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_STR },
1039 { "map_beg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_BEG },
1040 { "map_sub_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_SUB },
1041 { "map_dir_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DIR },
1042 { "map_dom_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DOM },
1043 { "map_end_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_END },
1044 { "map_reg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_REG },
1045 { "map_int_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_SINT, SMP_T_SINT, (void *)PAT_MATCH_INT },
1046 { "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 +01001047
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +02001048 { "map_str_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_STR },
1049 { "map_beg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_BEG },
1050 { "map_sub_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_SUB },
1051 { "map_dir_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DIR },
1052 { "map_dom_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DOM },
1053 { "map_end_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_END },
1054 { "map_reg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_REG },
1055 { "map_int_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_SINT, SMP_T_ADDR, (void *)PAT_MATCH_INT },
1056 { "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 +01001057
1058 { /* END */ },
1059}};
1060
Willy Tarreau0108d902018-11-25 19:14:37 +01001061INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);