blob: 8cfbbe51a1381705ced603829eb376b329184429 [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];
123 desc->pat.delete = pat_delete_fcts[(long)conv->private];
124 desc->pat.prune = pat_prune_fcts[(long)conv->private];
125 desc->pat.expect_type = pat_match_types[(long)conv->private];
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100126
127 /* Set the output parse method. */
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100128 switch (desc->conv->out_type) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100129 case SMP_T_STR: desc->pat.parse_smp = map_parse_str; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200130 case SMP_T_SINT: desc->pat.parse_smp = map_parse_int; break;
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200131 case SMP_T_ADDR: desc->pat.parse_smp = map_parse_ip; break;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100132 default:
133 memprintf(err, "map: internal haproxy error: no default parse case for the input type <%d>.",
134 conv->out_type);
Andreas Seltenreich78f35952016-03-03 20:32:23 +0100135 free(desc);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100136 return 0;
137 }
138
Thierry FOURNIER39bef452014-01-29 13:29:45 +0100139 /* Load map. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200140 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 +0100141 1, err, file, line))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100142 return 0;
Thierry FOURNIER0ffe78c2013-12-05 14:40:25 +0100143
Willy Tarreauaa5801b2019-04-19 11:35:22 +0200144 /* the maps of type IP support a string as default value. This
145 * string can be an ipv4 or an ipv6, we must convert it.
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200146 */
Willy Tarreauaa5801b2019-04-19 11:35:22 +0200147 if (arg[1].type != ARGT_STOP && desc->conv->out_type == SMP_T_ADDR) {
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200148 struct sample_data data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200149 if (!map_parse_ip(arg[1].data.str.area, &data)) {
150 memprintf(err, "map: cannot parse default ip <%s>.",
151 arg[1].data.str.area);
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200152 return 0;
153 }
Christopher Faulet6ad7df42020-08-07 11:45:18 +0200154 chunk_destroy(&arg[1].data.str);
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200155 if (data.type == SMP_T_IPV4) {
156 arg[1].type = ARGT_IPV4;
157 arg[1].data.ipv4 = data.u.ipv4;
158 } else {
159 arg[1].type = ARGT_IPV6;
160 arg[1].data.ipv6 = data.u.ipv6;
161 }
162 }
163
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100164 /* replace the first argument by this definition */
Christopher Faulet6ad7df42020-08-07 11:45:18 +0200165 chunk_destroy(&arg[0].data.str);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100166 arg[0].type = ARGT_MAP;
167 arg[0].data.map = desc;
168
169 return 1;
170}
171
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200172static int sample_conv_map(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100173{
174 struct map_descriptor *desc;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100175 struct pattern *pat;
Willy Tarreau83061a82018-07-13 11:56:34 +0200176 struct buffer *str;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100177
178 /* get config */
179 desc = arg_p[0].data.map;
180
181 /* Execute the match function. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100182 pat = pattern_exec_match(&desc->pat, smp, 1);
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100183
184 /* Match case. */
185 if (pat) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200186 if (pat->data) {
Thierry Fournier8feaa662016-02-10 22:55:20 +0100187 /* In the regm case, merge the sample with the input. */
188 if ((long)private == PAT_MATCH_REGM) {
Emeric Brun27102212018-07-17 09:47:07 -0400189 struct buffer *tmptrash;
Willy Tarreau2842e052018-08-22 04:55:43 +0200190 int len;
Emeric Brun27102212018-07-17 09:47:07 -0400191
192 /* Copy the content of the sample because it could
193 be scratched by incoming get_trash_chunk */
194 tmptrash = alloc_trash_chunk();
195 if (!tmptrash)
196 return 0;
197
198 tmptrash->data = smp->data.u.str.data;
199 if (tmptrash->data > (tmptrash->size-1))
200 tmptrash->data = tmptrash->size-1;
201
202 memcpy(tmptrash->area, smp->data.u.str.area, tmptrash->data);
203 tmptrash->area[tmptrash->data] = 0;
204
Thierry Fournier8feaa662016-02-10 22:55:20 +0100205 str = get_trash_chunk();
Willy Tarreau2842e052018-08-22 04:55:43 +0200206 len = exp_replace(str->area, str->size,
207 tmptrash->area,
208 pat->data->u.str.area,
209 (regmatch_t *)smp->ctx.a[0]);
Nenad Merdanovic646b7742019-04-12 22:54:28 +0200210 free_trash_chunk(tmptrash);
211
Willy Tarreau2842e052018-08-22 04:55:43 +0200212 if (len == -1)
Thierry Fournier8feaa662016-02-10 22:55:20 +0100213 return 0;
Willy Tarreau2842e052018-08-22 04:55:43 +0200214
215 str->data = len;
Thierry Fournier8feaa662016-02-10 22:55:20 +0100216 smp->data.u.str = *str;
217 return 1;
218 }
219 /* Copy sample. */
Thierry FOURNIER5cc18d42015-08-19 09:02:36 +0200220 smp->data = *pat->data;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100221 smp->flags |= SMP_F_CONST;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100222 return 1;
223 }
224
225 /* Return just int sample containing 1. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200226 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200227 smp->data.u.sint = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100228 return 1;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100229 }
230
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800231 /* If no default value available, the converter fails. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100232 if (arg_p[1].type == ARGT_STOP)
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100233 return 0;
234
235 /* Return the default value. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100236 switch (desc->conv->out_type) {
237
238 case SMP_T_STR:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200239 smp->data.type = SMP_T_STR;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100240 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200241 smp->data.u.str = arg_p[1].data.str;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100242 break;
243
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200244 case SMP_T_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200245 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200246 smp->data.u.sint = arg_p[1].data.sint;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100247 break;
248
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200249 case SMP_T_ADDR:
250 if (arg_p[1].type == ARGT_IPV4) {
251 smp->data.type = SMP_T_IPV4;
252 smp->data.u.ipv4 = arg_p[1].data.ipv4;
253 } else {
254 smp->data.type = SMP_T_IPV6;
255 smp->data.u.ipv6 = arg_p[1].data.ipv6;
256 }
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100257 break;
258 }
259
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100260 return 1;
261}
262
William Lallemandad8be612016-11-18 19:26:17 +0100263/* This function is used with map and acl management. It permits to browse
264 * each reference. The variable <getnext> must contain the current node,
265 * <end> point to the root node and the <flags> permit to filter required
266 * nodes.
267 */
268static inline
269struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
270 unsigned int flags)
271{
272 struct pat_ref *ref = getnext;
273
274 while (1) {
275
276 /* Get next list entry. */
277 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
278
279 /* If the entry is the last of the list, return NULL. */
280 if (&ref->list == end)
281 return NULL;
282
283 /* If the entry match the flag, return it. */
284 if (ref->flags & flags)
285 return ref;
286 }
287}
288
289static inline
290struct pat_ref *pat_ref_lookup_ref(const char *reference)
291{
292 int id;
293 char *error;
294
295 /* If the reference starts by a '#', this is numeric id. */
296 if (reference[0] == '#') {
297 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
298 id = strtol(reference + 1, &error, 10);
299 if (*error != '\0')
300 return NULL;
301
302 /* Perform the unique id lookup. */
303 return pat_ref_lookupid(id);
304 }
305
306 /* Perform the string lookup. */
307 return pat_ref_lookup(reference);
308}
309
310/* This function is used with map and acl management. It permits to browse
311 * each reference.
312 */
313static inline
314struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
315{
316 struct pattern_expr *expr;
317 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
318 if (&expr->list == end)
319 return NULL;
320 return expr;
321}
322
323static 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)) {
334 LIST_DEL(&appctx->ctx.map.bref.users);
335 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)) {
363 LIST_DEL(&appctx->ctx.map.bref.users);
364 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
William Lallemandad8be612016-11-18 19:26:17 +0100372 /* build messages */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200373 if (elt->sample)
William Lallemandad8be612016-11-18 19:26:17 +0100374 chunk_appendf(&trash, "%p %s %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200375 elt, elt->pattern,
376 elt->sample);
William Lallemandad8be612016-11-18 19:26:17 +0100377 else
378 chunk_appendf(&trash, "%p %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200379 elt, elt->pattern);
William Lallemandad8be612016-11-18 19:26:17 +0100380
Willy Tarreau06d80a92017-10-19 14:32:15 +0200381 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100382 /* let's try again later from this stream. We add ourselves into
383 * this stream's users so that it can remove us upon termination.
384 */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200385 LIST_ADDQ(&elt->back_refs, &appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100386 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +0100387 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100388 return 0;
389 }
390
391 /* get next list entry and check the end of the list */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200392 appctx->ctx.map.bref.ref = elt->list.n;
William Lallemandad8be612016-11-18 19:26:17 +0100393 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100394 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100395 /* fall through */
396
397 default:
398 appctx->st2 = STAT_ST_FIN;
399 return 1;
400 }
401}
402
403static int cli_io_handler_pats_list(struct appctx *appctx)
404{
405 struct stream_interface *si = appctx->owner;
406
407 switch (appctx->st2) {
408 case STAT_ST_INIT:
409 /* Display the column headers. If the message cannot be sent,
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800410 * quit the function with returning 0. The function is called
411 * later and restarted at the state "STAT_ST_INIT".
William Lallemandad8be612016-11-18 19:26:17 +0100412 */
413 chunk_reset(&trash);
414 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +0200415 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +0100416 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100417 return 0;
418 }
419
420 /* Now, we start the browsing of the references lists.
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800421 * Note that the following call to LIST_ELEM returns a bad pointer. The only
William Lallemandad8be612016-11-18 19:26:17 +0100422 * available field of this pointer is <list>. It is used with the function
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800423 * pat_list_get_next() for returning the first available entry
William Lallemandad8be612016-11-18 19:26:17 +0100424 */
425 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
426 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
427 appctx->ctx.map.display_flags);
428 appctx->st2 = STAT_ST_LIST;
429 /* fall through */
430
431 case STAT_ST_LIST:
432 while (appctx->ctx.map.ref) {
433 chunk_reset(&trash);
434
435 /* Build messages. If the reference is used by another category than
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800436 * the listed categories, display the information in the message.
William Lallemandad8be612016-11-18 19:26:17 +0100437 */
438 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
439 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
440 appctx->ctx.map.ref->display);
441
Willy Tarreau06d80a92017-10-19 14:32:15 +0200442 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100443 /* let's try again later from this stream. We add ourselves into
444 * this stream's users so that it can remove us upon termination.
445 */
Willy Tarreaudb398432018-11-15 11:08:52 +0100446 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100447 return 0;
448 }
449
450 /* get next list entry and check the end of the list */
451 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
452 appctx->ctx.map.display_flags);
453 }
454
William Lallemandad8be612016-11-18 19:26:17 +0100455 /* fall through */
456
457 default:
458 appctx->st2 = STAT_ST_FIN;
459 return 1;
460 }
461 return 0;
462}
463
464static int cli_io_handler_map_lookup(struct appctx *appctx)
465{
466 struct stream_interface *si = appctx->owner;
467 struct sample sample;
468 struct pattern *pat;
469 int match_method;
470
471 switch (appctx->st2) {
472 case STAT_ST_INIT:
473 /* Init to the first entry. The list cannot be change */
474 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
475 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
476 appctx->st2 = STAT_ST_LIST;
477 /* fall through */
478
479 case STAT_ST_LIST:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100480 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100481 /* for each lookup type */
482 while (appctx->ctx.map.expr) {
483 /* initialise chunk to build new message */
484 chunk_reset(&trash);
485
486 /* execute pattern matching */
487 sample.data.type = SMP_T_STR;
488 sample.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200489 sample.data.u.str.data = appctx->ctx.map.chunk.data;
490 sample.data.u.str.area = appctx->ctx.map.chunk.area;
Emeric Brunb5997f72017-07-03 11:34:05 +0200491
William Lallemandad8be612016-11-18 19:26:17 +0100492 if (appctx->ctx.map.expr->pat_head->match &&
493 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
494 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
495 else
496 pat = NULL;
497
498 /* build return message: set type of match */
499 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
500 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
501 break;
502 if (match_method >= PAT_MATCH_NUM)
503 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
504 else
505 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
506
507 /* case sensitive */
508 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
509 chunk_appendf(&trash, ", case=insensitive");
510 else
511 chunk_appendf(&trash, ", case=sensitive");
512
513 /* Display no match, and set default value */
514 if (!pat) {
515 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
516 chunk_appendf(&trash, ", found=no");
517 else
518 chunk_appendf(&trash, ", match=no");
519 }
520
521 /* Display match and match info */
522 else {
523 /* display match */
524 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
525 chunk_appendf(&trash, ", found=yes");
526 else
527 chunk_appendf(&trash, ", match=yes");
528
529 /* display index mode */
530 if (pat->sflags & PAT_SF_TREE)
531 chunk_appendf(&trash, ", idx=tree");
532 else
533 chunk_appendf(&trash, ", idx=list");
534
535 /* display pattern */
536 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
537 if (pat->ref && pat->ref->pattern)
538 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
539 else
540 chunk_appendf(&trash, ", key=unknown");
541 }
542 else {
543 if (pat->ref && pat->ref->pattern)
544 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
545 else
546 chunk_appendf(&trash, ", pattern=unknown");
547 }
548
549 /* display return value */
550 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
551 if (pat->data && pat->ref && pat->ref->sample)
552 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
553 smp_to_type[pat->data->type]);
554 else
555 chunk_appendf(&trash, ", value=none");
556 }
557 }
558
559 chunk_appendf(&trash, "\n");
560
561 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200562 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100563 /* let's try again later from this stream. We add ourselves into
564 * this stream's users so that it can remove us upon termination.
565 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100566 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +0100567 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100568 return 0;
569 }
570
571 /* get next entry */
572 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
573 &appctx->ctx.map.ref->pat);
574 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100575 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100576 /* fall through */
577
578 default:
579 appctx->st2 = STAT_ST_FIN;
William Lallemandad8be612016-11-18 19:26:17 +0100580 return 1;
581 }
582}
583
584static void cli_release_mlook(struct appctx *appctx)
585{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200586 free(appctx->ctx.map.chunk.area);
587 appctx->ctx.map.chunk.area = NULL;
William Lallemandad8be612016-11-18 19:26:17 +0100588}
589
590
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200591static int cli_parse_get_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100592{
593 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
594 /* Set flags. */
595 if (args[1][0] == 'm')
596 appctx->ctx.map.display_flags = PAT_REF_MAP;
597 else
598 appctx->ctx.map.display_flags = PAT_REF_ACL;
599
600 /* No parameter. */
601 if (!*args[2] || !*args[3]) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200602 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
603 return cli_err(appctx, "Missing map identifier and/or key.\n");
604 else
605 return cli_err(appctx, "Missing ACL identifier and/or key.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100606 }
607
608 /* lookup into the maps */
609 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
610 if (!appctx->ctx.map.ref) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200611 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
612 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
613 else
614 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100615 }
616
617 /* copy input string. The string must be allocated because
618 * it may be used over multiple iterations. It's released
619 * at the end and upon abort anyway.
620 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200621 appctx->ctx.map.chunk.data = strlen(args[3]);
622 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.data + 1;
623 appctx->ctx.map.chunk.area = strdup(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200624 if (!appctx->ctx.map.chunk.area)
625 return cli_err(appctx, "Out of memory error.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100626
627 return 0;
628 }
629 return 1;
630}
631
Emeric Brun8d85aa42017-06-29 15:40:33 +0200632static void cli_release_show_map(struct appctx *appctx)
633{
634 if (appctx->st2 == STAT_ST_LIST) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100635 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200636 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users))
637 LIST_DEL(&appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100638 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200639 }
640}
641
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200642static int cli_parse_show_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100643{
644 if (strcmp(args[1], "map") == 0 ||
645 strcmp(args[1], "acl") == 0) {
646
647 /* Set ACL or MAP flags. */
648 if (args[1][0] == 'm')
649 appctx->ctx.map.display_flags = PAT_REF_MAP;
650 else
651 appctx->ctx.map.display_flags = PAT_REF_ACL;
652
653 /* no parameter: display all map available */
654 if (!*args[2]) {
William Lallemandad8be612016-11-18 19:26:17 +0100655 appctx->io_handler = cli_io_handler_pats_list;
656 return 0;
657 }
658
659 /* lookup into the refs and check the map flag */
660 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
661 if (!appctx->ctx.map.ref ||
662 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200663 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
664 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
665 else
666 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100667 }
William Lallemandad8be612016-11-18 19:26:17 +0100668 appctx->io_handler = cli_io_handler_pat_list;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200669 appctx->io_release = cli_release_show_map;
William Lallemandad8be612016-11-18 19:26:17 +0100670 return 0;
671 }
672
673 return 0;
674}
675
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200676static int cli_parse_set_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100677{
678 if (strcmp(args[1], "map") == 0) {
679 char *err;
680
681 /* Set flags. */
682 appctx->ctx.map.display_flags = PAT_REF_MAP;
683
684 /* Expect three parameters: map name, key and new value. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200685 if (!*args[2] || !*args[3] || !*args[4])
686 return cli_err(appctx, "'set map' expects three parameters: map identifier, key and value.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100687
688 /* Lookup the reference in the maps. */
689 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200690 if (!appctx->ctx.map.ref)
691 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100692
693 /* If the entry identifier start with a '#', it is considered as
694 * pointer id
695 */
696 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
697 struct pat_ref_elt *ref;
698 long long int conv;
699 char *error;
700
701 /* Convert argument to integer value. */
702 conv = strtoll(&args[3][1], &error, 16);
Willy Tarreau9d008692019-08-09 11:21:01 +0200703 if (*error != '\0')
704 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100705
706 /* Convert and check integer to pointer. */
707 ref = (struct pat_ref_elt *)(long)conv;
Willy Tarreau9d008692019-08-09 11:21:01 +0200708 if ((long long int)(long)ref != conv)
709 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100710
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200711 /* Try to modify the entry. */
William Lallemandad8be612016-11-18 19:26:17 +0100712 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100713 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100714 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100715 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +0200716 if (err)
717 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
718 else
719 return cli_err(appctx, "Failed to update an entry.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100720 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100721 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100722 }
723 else {
724 /* Else, use the entry identifier as pattern
725 * string, and update the value.
726 */
727 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100728 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100729 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100730 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +0200731 if (err)
732 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
733 else
734 return cli_err(appctx, "Failed to update an entry.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100735 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100736 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100737 }
738
739 /* The set is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100740 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100741 return 0;
742 }
743 return 1;
744}
745
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200746static int map_add_key_value(struct appctx *appctx, const char *key, const char *value, char **err)
747{
748 int ret;
749
750 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
751 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
752 ret = pat_ref_add(appctx->ctx.map.ref, key, value, err);
753 else
754 ret = pat_ref_add(appctx->ctx.map.ref, key, NULL, err);
755 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
756
757 return ret;
758}
759
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200760static int cli_parse_add_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100761{
762 if (strcmp(args[1], "map") == 0 ||
763 strcmp(args[1], "acl") == 0) {
764 int ret;
765 char *err;
766
767 /* Set flags. */
768 if (args[1][0] == 'm')
769 appctx->ctx.map.display_flags = PAT_REF_MAP;
770 else
771 appctx->ctx.map.display_flags = PAT_REF_ACL;
772
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200773 /* If the keyword is "map", we expect:
774 * - three parameters if there is no payload
775 * - one parameter if there is a payload
776 * If it is "acl", we expect only two parameters
William Lallemandad8be612016-11-18 19:26:17 +0100777 */
778 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200779 if ((!payload && (!*args[2] || !*args[3] || !*args[4])) ||
Willy Tarreau9d008692019-08-09 11:21:01 +0200780 (payload && !*args[2]))
781 return cli_err(appctx,
782 "'add map' expects three parameters (map identifier, key and value)"
783 " or one parameter (map identifier) and a payload\n");
William Lallemandad8be612016-11-18 19:26:17 +0100784 }
Willy Tarreau9d008692019-08-09 11:21:01 +0200785 else if (!*args[2] || !*args[3])
786 return cli_err(appctx, "'add acl' expects two parameters: ACL identifier and pattern.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100787
788 /* Lookup for the reference. */
789 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
790 if (!appctx->ctx.map.ref) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200791 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
792 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
793 else
794 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100795 }
796
797 /* The command "add acl" is prohibited if the reference
798 * use samples.
799 */
800 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
801 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200802 return cli_err(appctx,
803 "This ACL is shared with a map containing samples. "
804 "You must use the command 'add map' to add values.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100805 }
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200806 /* Add value(s). */
William Lallemandad8be612016-11-18 19:26:17 +0100807 err = NULL;
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200808 if (!payload) {
809 ret = map_add_key_value(appctx, args[3], args[4], &err);
810 if (!ret) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200811 if (err)
812 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
813 else
814 return cli_err(appctx, "Failed to add an entry.\n");
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200815 }
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200816 }
817 else {
818 const char *end = payload + strlen(payload);
819
820 while (payload < end) {
821 char *key, *value;
822 size_t l;
823
824 /* key */
825 key = payload;
826 l = strcspn(key, " \t");
827 payload += l;
828
Willy Tarreau9d008692019-08-09 11:21:01 +0200829 if (!*payload && appctx->ctx.map.display_flags == PAT_REF_MAP)
830 return cli_dynerr(appctx, memprintf(&err, "Missing value for key '%s'.\n", key));
831
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200832 key[l] = 0;
833 payload++;
834
835 /* value */
836 payload += strspn(payload, " \t");
837 value = payload;
838 l = strcspn(value, "\n");
839 payload += l;
840 if (*payload)
841 payload++;
842 value[l] = 0;
843
844 ret = map_add_key_value(appctx, key, value, &err);
845 if (!ret) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200846 if (err)
847 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
848 else
849 return cli_err(appctx, "Failed to add a key.\n");
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200850 }
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200851 }
William Lallemandad8be612016-11-18 19:26:17 +0100852 }
853
854 /* The add is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100855 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100856 return 1;
857 }
858
859 return 0;
860}
861
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200862static int cli_parse_del_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100863{
864 if (args[1][0] == 'm')
865 appctx->ctx.map.display_flags = PAT_REF_MAP;
866 else
867 appctx->ctx.map.display_flags = PAT_REF_ACL;
868
869 /* Expect two parameters: map name and key. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200870 if (!*args[2] || !*args[3]) {
871 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
872 return cli_err(appctx, "This command expects two parameters: map identifier and key.\n");
873 else
874 return cli_err(appctx, "This command expects two parameters: ACL identifier and key.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100875 }
876
877 /* Lookup the reference in the maps. */
878 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
879 if (!appctx->ctx.map.ref ||
Willy Tarreau9d008692019-08-09 11:21:01 +0200880 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags))
881 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100882
883 /* If the entry identifier start with a '#', it is considered as
884 * pointer id
885 */
886 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
887 struct pat_ref_elt *ref;
888 long long int conv;
889 char *error;
890
891 /* Convert argument to integer value. */
892 conv = strtoll(&args[3][1], &error, 16);
Willy Tarreau9d008692019-08-09 11:21:01 +0200893 if (*error != '\0')
894 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100895
896 /* Convert and check integer to pointer. */
897 ref = (struct pat_ref_elt *)(long)conv;
Willy Tarreau9d008692019-08-09 11:21:01 +0200898 if ((long long int)(long)ref != conv)
899 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100900
901 /* Try to delete the entry. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100902 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100903 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100904 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100905 /* The entry is not found, send message. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200906 return cli_err(appctx, "Key not found.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100907 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100908 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100909 }
910 else {
911 /* Else, use the entry identifier as pattern
912 * string and try to delete the entry.
913 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100914 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100915 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100916 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100917 /* The entry is not found, send message. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200918 return cli_err(appctx, "Key not found.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100919 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100920 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100921 }
922
923 /* The deletion is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100924 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100925 return 1;
926}
927
928
Willy Tarreaud1d005d2019-12-20 18:22:02 +0100929/* continue to clear a map which was started in the parser */
930static int cli_io_handler_clear_map(struct appctx *appctx)
931{
932 struct stream_interface *si = appctx->owner;
933 int finished;
934
935 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
936 finished = pat_ref_prune(appctx->ctx.map.ref);
937 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
938
939 if (!finished) {
940 /* let's come back later */
941 si_rx_endp_more(si);
942 return 0;
943 }
944 return 1;
945}
946
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200947static int cli_parse_clear_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100948{
949 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
950 /* Set ACL or MAP flags. */
951 if (args[1][0] == 'm')
952 appctx->ctx.map.display_flags = PAT_REF_MAP;
953 else
954 appctx->ctx.map.display_flags = PAT_REF_ACL;
955
956 /* no parameter */
957 if (!*args[2]) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200958 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
959 return cli_err(appctx, "Missing map identifier.\n");
960 else
961 return cli_err(appctx, "Missing ACL identifier.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100962 }
963
964 /* lookup into the refs and check the map flag */
965 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
966 if (!appctx->ctx.map.ref ||
967 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200968 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
969 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
970 else
971 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100972 }
973
Willy Tarreaud1d005d2019-12-20 18:22:02 +0100974 /* delegate the clearing to the I/O handler which can yield */
975 return 0;
William Lallemandad8be612016-11-18 19:26:17 +0100976 }
Willy Tarreaud1d005d2019-12-20 18:22:02 +0100977 return 1;
William Lallemandad8be612016-11-18 19:26:17 +0100978}
979
980/* register cli keywords */
981
982static struct cli_kw_list cli_kws = {{ },{
983 { { "add", "acl", NULL }, "add acl : add acl entry", cli_parse_add_map, NULL },
Willy Tarreaud1d005d2019-12-20 18:22:02 +0100984 { { "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 +0100985 { { "del", "acl", NULL }, "del acl : delete acl entry", cli_parse_del_map, NULL },
986 { { "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 },
987 { { "show", "acl", NULL }, "show acl [id] : report available acls or dump an acl's contents", cli_parse_show_map, NULL },
988 { { "add", "map", NULL }, "add map : add map entry", cli_parse_add_map, NULL },
Willy Tarreaud1d005d2019-12-20 18:22:02 +0100989 { { "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 +0100990 { { "del", "map", NULL }, "del map : delete map entry", cli_parse_del_map, NULL },
Nenad Merdanovic96c15712017-03-12 22:01:36 +0100991 { { "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 +0100992 { { "set", "map", NULL }, "set map : modify map entry", cli_parse_set_map, NULL },
993 { { "show", "map", NULL }, "show map [id] : report available maps or dump a map's contents", cli_parse_show_map, NULL },
994 { { NULL }, NULL, NULL, NULL }
995}};
996
Willy Tarreau0108d902018-11-25 19:14:37 +0100997INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemandad8be612016-11-18 19:26:17 +0100998
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100999/* Note: must not be declared <const> as its list will be overwritten
1000 *
1001 * For the map_*_int keywords, the output is declared as SMP_T_UINT, but the converter function
1002 * can provide SMP_T_UINT, SMP_T_SINT or SMP_T_BOOL depending on how the patterns found in the
1003 * file can be parsed.
1004 *
1005 * For the map_*_ip keyword, the output is declared as SMP_T_IPV4, but the converter function
1006 * can provide SMP_T_IPV4 or SMP_T_IPV6 depending on the patterns found in the file.
1007 *
1008 * The map_* keywords only emit strings.
1009 *
1010 * The output type is only used during the configuration parsing. It is used for detecting
1011 * compatibility problems.
1012 *
1013 * The arguments are: <file>[,<default value>]
1014 */
1015static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER1edc9712014-12-15 16:18:39 +01001016 { "map", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1017 { "map_str", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1018 { "map_beg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_BEG },
1019 { "map_sub", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_SUB },
1020 { "map_dir", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DIR },
1021 { "map_dom", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DOM },
1022 { "map_end", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_END },
1023 { "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 +01001024 { "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 +02001025 { "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 +01001026 { "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 +01001027
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001028 { "map_str_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_STR },
1029 { "map_beg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_BEG },
1030 { "map_sub_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_SUB },
1031 { "map_dir_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DIR },
1032 { "map_dom_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DOM },
1033 { "map_end_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_END },
1034 { "map_reg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_REG },
1035 { "map_int_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_SINT, SMP_T_SINT, (void *)PAT_MATCH_INT },
1036 { "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 +01001037
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +02001038 { "map_str_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_STR },
1039 { "map_beg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_BEG },
1040 { "map_sub_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_SUB },
1041 { "map_dir_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DIR },
1042 { "map_dom_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DOM },
1043 { "map_end_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_END },
1044 { "map_reg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_REG },
1045 { "map_int_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_SINT, SMP_T_ADDR, (void *)PAT_MATCH_INT },
1046 { "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 +01001047
1048 { /* END */ },
1049}};
1050
Willy Tarreau0108d902018-11-25 19:14:37 +01001051INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);