blob: 5f98dd0a00541cfbc0981f84a513ecb7bf559a0c [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>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010016#include <common/standard.h>
17
William Lallemandad8be612016-11-18 19:26:17 +010018#include <types/applet.h>
19#include <types/cli.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010020#include <types/global.h>
21#include <types/map.h>
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010022#include <types/pattern.h>
William Lallemandad8be612016-11-18 19:26:17 +010023#include <types/stats.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010024
William Lallemandad8be612016-11-18 19:26:17 +010025#include <proto/applet.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010026#include <proto/arg.h>
William Lallemandad8be612016-11-18 19:26:17 +010027#include <proto/cli.h>
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020028#include <proto/log.h>
Thierry FOURNIERb0c0a0f2013-12-10 15:05:34 +010029#include <proto/map.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010030#include <proto/pattern.h>
William Lallemandad8be612016-11-18 19:26:17 +010031#include <proto/stream_interface.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010032#include <proto/sample.h>
33
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020034/* Parse an IPv4 or IPv6 address and store it into the sample.
35 * The output type is IPv4 or IPv6.
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010036 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020037int map_parse_ip(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010038{
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020039 int len = strlen(text);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010040
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020041 if (buf2ip(text, len, &data->u.ipv4)) {
42 data->type = SMP_T_IPV4;
43 return 1;
44 }
45 if (buf2ip6(text, len, &data->u.ipv6)) {
46 data->type = SMP_T_IPV6;
47 return 1;
48 }
49 return 0;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010050}
51
52/* Parse a string and store a pointer to it into the sample. The original
53 * string must be left in memory because we return a direct memory reference.
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010054 * The output type is SMP_T_STR. There is no risk that the data will be
55 * overwritten because sample_conv_map() makes a const sample with this
56 * output.
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010057 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020058int map_parse_str(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010059{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020060 data->u.str.area = (char *)text;
61 data->u.str.data = strlen(text);
62 data->u.str.size = data->u.str.data + 1;
Thierry FOURNIER503bb092015-08-19 08:35:43 +020063 data->type = SMP_T_STR;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010064 return 1;
65}
66
67/* Parse an integer and convert it to a sample. The output type is SINT if the
68 * number is negative, or UINT if it is positive or null. The function returns
69 * zero (error) if the number is too large.
70 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020071int map_parse_int(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010072{
Thierry FOURNIER503bb092015-08-19 08:35:43 +020073 data->type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020074 data->u.sint = read_int64(&text, text + strlen(text));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020075 if (*text != '\0')
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010076 return 0;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010077 return 1;
78}
79
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010080/* This crete and initialize map descriptor.
81 * Return NULL if out of memory error
82 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010083static struct map_descriptor *map_create_descriptor(struct sample_conv *conv)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010084{
85 struct map_descriptor *desc;
86
87 desc = calloc(1, sizeof(*desc));
88 if (!desc)
89 return NULL;
90
91 desc->conv = conv;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010092
93 return desc;
94}
95
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010096/* This function load the map file according with data type declared into
97 * the "struct sample_conv".
98 *
99 * This function choose the indexation type (ebtree or list) according with
100 * the type of match needed.
101 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200102int sample_load_map(struct arg *arg, struct sample_conv *conv,
103 const char *file, int line, char **err)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100104{
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100105 struct map_descriptor *desc;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100106
107 /* create new map descriptor */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100108 desc = map_create_descriptor(conv);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100109 if (!desc) {
110 memprintf(err, "out of memory");
111 return 0;
112 }
113
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100114 /* Initialize pattern */
115 pattern_init_head(&desc->pat);
116
117 /* This is original pattern, must free */
118 desc->do_free = 1;
119
120 /* Set the match method. */
Thierry FOURNIER1edc9712014-12-15 16:18:39 +0100121 desc->pat.match = pat_match_fcts[(long)conv->private];
122 desc->pat.parse = pat_parse_fcts[(long)conv->private];
123 desc->pat.index = pat_index_fcts[(long)conv->private];
124 desc->pat.delete = pat_delete_fcts[(long)conv->private];
125 desc->pat.prune = pat_prune_fcts[(long)conv->private];
126 desc->pat.expect_type = pat_match_types[(long)conv->private];
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100127
128 /* Set the output parse method. */
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100129 switch (desc->conv->out_type) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100130 case SMP_T_STR: desc->pat.parse_smp = map_parse_str; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200131 case SMP_T_SINT: desc->pat.parse_smp = map_parse_int; break;
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200132 case SMP_T_ADDR: desc->pat.parse_smp = map_parse_ip; break;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100133 default:
134 memprintf(err, "map: internal haproxy error: no default parse case for the input type <%d>.",
135 conv->out_type);
Andreas Seltenreich78f35952016-03-03 20:32:23 +0100136 free(desc);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100137 return 0;
138 }
139
Thierry FOURNIER39bef452014-01-29 13:29:45 +0100140 /* Load map. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200141 if (!pattern_read_from_file(&desc->pat, PAT_REF_MAP, arg[0].data.str.area, PAT_MF_NO_DNS,
Thierry FOURNIER94580c92014-02-11 14:36:45 +0100142 1, err, file, line))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100143 return 0;
Thierry FOURNIER0ffe78c2013-12-05 14:40:25 +0100144
Willy Tarreauaa5801b2019-04-19 11:35:22 +0200145 /* the maps of type IP support a string as default value. This
146 * string can be an ipv4 or an ipv6, we must convert it.
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200147 */
Willy Tarreauaa5801b2019-04-19 11:35:22 +0200148 if (arg[1].type != ARGT_STOP && desc->conv->out_type == SMP_T_ADDR) {
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200149 struct sample_data data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200150 if (!map_parse_ip(arg[1].data.str.area, &data)) {
151 memprintf(err, "map: cannot parse default ip <%s>.",
152 arg[1].data.str.area);
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200153 return 0;
154 }
155 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 */
165 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
322static int cli_io_handler_pat_list(struct appctx *appctx)
323{
324 struct stream_interface *si = appctx->owner;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200325 struct pat_ref_elt *elt;
326
327 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
328 /* If we're forced to shut down, we might have to remove our
329 * reference to the last ref_elt being dumped.
330 */
331 if (appctx->st2 == STAT_ST_LIST) {
Dragan Dosen336a11f2018-05-04 16:27:15 +0200332 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users)) {
333 LIST_DEL(&appctx->ctx.map.bref.users);
334 LIST_INIT(&appctx->ctx.map.bref.users);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200335 }
336 }
337 return 1;
338 }
William Lallemandad8be612016-11-18 19:26:17 +0100339
340 switch (appctx->st2) {
341
342 case STAT_ST_INIT:
Emeric Brun8d85aa42017-06-29 15:40:33 +0200343 /* the function had not been called yet, let's prepare the
344 * buffer for a response. We initialize the current stream
345 * pointer to the first in the global list. When a target
346 * stream is being destroyed, it is responsible for updating
347 * this pointer. We know we have reached the end when this
348 * pointer points back to the head of the streams list.
349 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100350 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200351 LIST_INIT(&appctx->ctx.map.bref.users);
352 appctx->ctx.map.bref.ref = appctx->ctx.map.ref->head.n;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100353 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100354 appctx->st2 = STAT_ST_LIST;
355 /* fall through */
356
357 case STAT_ST_LIST:
Emeric Brunb5997f72017-07-03 11:34:05 +0200358
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100359 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200360
Emeric Brun8d85aa42017-06-29 15:40:33 +0200361 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users)) {
362 LIST_DEL(&appctx->ctx.map.bref.users);
363 LIST_INIT(&appctx->ctx.map.bref.users);
364 }
365
366 while (appctx->ctx.map.bref.ref != &appctx->ctx.map.ref->head) {
William Lallemandad8be612016-11-18 19:26:17 +0100367 chunk_reset(&trash);
368
Emeric Brun8d85aa42017-06-29 15:40:33 +0200369 elt = LIST_ELEM(appctx->ctx.map.bref.ref, struct pat_ref_elt *, list);
370
William Lallemandad8be612016-11-18 19:26:17 +0100371 /* build messages */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200372 if (elt->sample)
William Lallemandad8be612016-11-18 19:26:17 +0100373 chunk_appendf(&trash, "%p %s %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200374 elt, elt->pattern,
375 elt->sample);
William Lallemandad8be612016-11-18 19:26:17 +0100376 else
377 chunk_appendf(&trash, "%p %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200378 elt, elt->pattern);
William Lallemandad8be612016-11-18 19:26:17 +0100379
Willy Tarreau06d80a92017-10-19 14:32:15 +0200380 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100381 /* let's try again later from this stream. We add ourselves into
382 * this stream's users so that it can remove us upon termination.
383 */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200384 LIST_ADDQ(&elt->back_refs, &appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100385 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +0100386 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100387 return 0;
388 }
389
390 /* get next list entry and check the end of the list */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200391 appctx->ctx.map.bref.ref = elt->list.n;
William Lallemandad8be612016-11-18 19:26:17 +0100392 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100393 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100394 /* fall through */
395
396 default:
397 appctx->st2 = STAT_ST_FIN;
398 return 1;
399 }
400}
401
402static int cli_io_handler_pats_list(struct appctx *appctx)
403{
404 struct stream_interface *si = appctx->owner;
405
406 switch (appctx->st2) {
407 case STAT_ST_INIT:
408 /* Display the column headers. If the message cannot be sent,
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800409 * quit the function with returning 0. The function is called
410 * later and restarted at the state "STAT_ST_INIT".
William Lallemandad8be612016-11-18 19:26:17 +0100411 */
412 chunk_reset(&trash);
413 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +0200414 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +0100415 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100416 return 0;
417 }
418
419 /* Now, we start the browsing of the references lists.
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800420 * Note that the following call to LIST_ELEM returns a bad pointer. The only
William Lallemandad8be612016-11-18 19:26:17 +0100421 * available field of this pointer is <list>. It is used with the function
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800422 * pat_list_get_next() for returning the first available entry
William Lallemandad8be612016-11-18 19:26:17 +0100423 */
424 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
425 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
426 appctx->ctx.map.display_flags);
427 appctx->st2 = STAT_ST_LIST;
428 /* fall through */
429
430 case STAT_ST_LIST:
431 while (appctx->ctx.map.ref) {
432 chunk_reset(&trash);
433
434 /* Build messages. If the reference is used by another category than
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800435 * the listed categories, display the information in the message.
William Lallemandad8be612016-11-18 19:26:17 +0100436 */
437 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
438 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
439 appctx->ctx.map.ref->display);
440
Willy Tarreau06d80a92017-10-19 14:32:15 +0200441 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100442 /* let's try again later from this stream. We add ourselves into
443 * this stream's users so that it can remove us upon termination.
444 */
Willy Tarreaudb398432018-11-15 11:08:52 +0100445 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100446 return 0;
447 }
448
449 /* get next list entry and check the end of the list */
450 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
451 appctx->ctx.map.display_flags);
452 }
453
William Lallemandad8be612016-11-18 19:26:17 +0100454 /* fall through */
455
456 default:
457 appctx->st2 = STAT_ST_FIN;
458 return 1;
459 }
460 return 0;
461}
462
463static int cli_io_handler_map_lookup(struct appctx *appctx)
464{
465 struct stream_interface *si = appctx->owner;
466 struct sample sample;
467 struct pattern *pat;
468 int match_method;
469
470 switch (appctx->st2) {
471 case STAT_ST_INIT:
472 /* Init to the first entry. The list cannot be change */
473 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
474 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
475 appctx->st2 = STAT_ST_LIST;
476 /* fall through */
477
478 case STAT_ST_LIST:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100479 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100480 /* for each lookup type */
481 while (appctx->ctx.map.expr) {
482 /* initialise chunk to build new message */
483 chunk_reset(&trash);
484
485 /* execute pattern matching */
486 sample.data.type = SMP_T_STR;
487 sample.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200488 sample.data.u.str.data = appctx->ctx.map.chunk.data;
489 sample.data.u.str.area = appctx->ctx.map.chunk.area;
Emeric Brunb5997f72017-07-03 11:34:05 +0200490
William Lallemandad8be612016-11-18 19:26:17 +0100491 if (appctx->ctx.map.expr->pat_head->match &&
492 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
493 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
494 else
495 pat = NULL;
496
497 /* build return message: set type of match */
498 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
499 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
500 break;
501 if (match_method >= PAT_MATCH_NUM)
502 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
503 else
504 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
505
506 /* case sensitive */
507 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
508 chunk_appendf(&trash, ", case=insensitive");
509 else
510 chunk_appendf(&trash, ", case=sensitive");
511
512 /* Display no match, and set default value */
513 if (!pat) {
514 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
515 chunk_appendf(&trash, ", found=no");
516 else
517 chunk_appendf(&trash, ", match=no");
518 }
519
520 /* Display match and match info */
521 else {
522 /* display match */
523 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
524 chunk_appendf(&trash, ", found=yes");
525 else
526 chunk_appendf(&trash, ", match=yes");
527
528 /* display index mode */
529 if (pat->sflags & PAT_SF_TREE)
530 chunk_appendf(&trash, ", idx=tree");
531 else
532 chunk_appendf(&trash, ", idx=list");
533
534 /* display pattern */
535 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
536 if (pat->ref && pat->ref->pattern)
537 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
538 else
539 chunk_appendf(&trash, ", key=unknown");
540 }
541 else {
542 if (pat->ref && pat->ref->pattern)
543 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
544 else
545 chunk_appendf(&trash, ", pattern=unknown");
546 }
547
548 /* display return value */
549 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
550 if (pat->data && pat->ref && pat->ref->sample)
551 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
552 smp_to_type[pat->data->type]);
553 else
554 chunk_appendf(&trash, ", value=none");
555 }
556 }
557
558 chunk_appendf(&trash, "\n");
559
560 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200561 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100562 /* let's try again later from this stream. We add ourselves into
563 * this stream's users so that it can remove us upon termination.
564 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100565 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +0100566 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100567 return 0;
568 }
569
570 /* get next entry */
571 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
572 &appctx->ctx.map.ref->pat);
573 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100574 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100575 /* fall through */
576
577 default:
578 appctx->st2 = STAT_ST_FIN;
William Lallemandad8be612016-11-18 19:26:17 +0100579 return 1;
580 }
581}
582
583static void cli_release_mlook(struct appctx *appctx)
584{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200585 free(appctx->ctx.map.chunk.area);
586 appctx->ctx.map.chunk.area = NULL;
William Lallemandad8be612016-11-18 19:26:17 +0100587}
588
589
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200590static int cli_parse_get_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100591{
592 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
593 /* Set flags. */
594 if (args[1][0] == 'm')
595 appctx->ctx.map.display_flags = PAT_REF_MAP;
596 else
597 appctx->ctx.map.display_flags = PAT_REF_ACL;
598
599 /* No parameter. */
600 if (!*args[2] || !*args[3]) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200601 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
602 return cli_err(appctx, "Missing map identifier and/or key.\n");
603 else
604 return cli_err(appctx, "Missing ACL identifier and/or key.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100605 }
606
607 /* lookup into the maps */
608 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
609 if (!appctx->ctx.map.ref) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200610 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
611 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
612 else
613 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100614 }
615
616 /* copy input string. The string must be allocated because
617 * it may be used over multiple iterations. It's released
618 * at the end and upon abort anyway.
619 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200620 appctx->ctx.map.chunk.data = strlen(args[3]);
621 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.data + 1;
622 appctx->ctx.map.chunk.area = strdup(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200623 if (!appctx->ctx.map.chunk.area)
624 return cli_err(appctx, "Out of memory error.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100625
626 return 0;
627 }
628 return 1;
629}
630
Emeric Brun8d85aa42017-06-29 15:40:33 +0200631static void cli_release_show_map(struct appctx *appctx)
632{
633 if (appctx->st2 == STAT_ST_LIST) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100634 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200635 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users))
636 LIST_DEL(&appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100637 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200638 }
639}
640
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200641static int cli_parse_show_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100642{
643 if (strcmp(args[1], "map") == 0 ||
644 strcmp(args[1], "acl") == 0) {
645
646 /* Set ACL or MAP flags. */
647 if (args[1][0] == 'm')
648 appctx->ctx.map.display_flags = PAT_REF_MAP;
649 else
650 appctx->ctx.map.display_flags = PAT_REF_ACL;
651
652 /* no parameter: display all map available */
653 if (!*args[2]) {
William Lallemandad8be612016-11-18 19:26:17 +0100654 appctx->io_handler = cli_io_handler_pats_list;
655 return 0;
656 }
657
658 /* lookup into the refs and check the map flag */
659 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
660 if (!appctx->ctx.map.ref ||
661 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200662 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
663 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
664 else
665 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100666 }
William Lallemandad8be612016-11-18 19:26:17 +0100667 appctx->io_handler = cli_io_handler_pat_list;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200668 appctx->io_release = cli_release_show_map;
William Lallemandad8be612016-11-18 19:26:17 +0100669 return 0;
670 }
671
672 return 0;
673}
674
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200675static int cli_parse_set_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100676{
677 if (strcmp(args[1], "map") == 0) {
678 char *err;
679
680 /* Set flags. */
681 appctx->ctx.map.display_flags = PAT_REF_MAP;
682
683 /* Expect three parameters: map name, key and new value. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200684 if (!*args[2] || !*args[3] || !*args[4])
685 return cli_err(appctx, "'set map' expects three parameters: map identifier, key and value.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100686
687 /* Lookup the reference in the maps. */
688 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200689 if (!appctx->ctx.map.ref)
690 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100691
692 /* If the entry identifier start with a '#', it is considered as
693 * pointer id
694 */
695 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
696 struct pat_ref_elt *ref;
697 long long int conv;
698 char *error;
699
700 /* Convert argument to integer value. */
701 conv = strtoll(&args[3][1], &error, 16);
Willy Tarreau9d008692019-08-09 11:21:01 +0200702 if (*error != '\0')
703 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100704
705 /* Convert and check integer to pointer. */
706 ref = (struct pat_ref_elt *)(long)conv;
Willy Tarreau9d008692019-08-09 11:21:01 +0200707 if ((long long int)(long)ref != conv)
708 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100709
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200710 /* Try to modify the entry. */
William Lallemandad8be612016-11-18 19:26:17 +0100711 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100712 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100713 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100714 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +0200715 if (err)
716 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
717 else
718 return cli_err(appctx, "Failed to update an entry.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100719 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100720 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100721 }
722 else {
723 /* Else, use the entry identifier as pattern
724 * string, and update the value.
725 */
726 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100727 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100728 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100729 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +0200730 if (err)
731 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
732 else
733 return cli_err(appctx, "Failed to update an entry.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100734 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100735 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100736 }
737
738 /* The set is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100739 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100740 return 0;
741 }
742 return 1;
743}
744
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200745static int map_add_key_value(struct appctx *appctx, const char *key, const char *value, char **err)
746{
747 int ret;
748
749 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
750 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
751 ret = pat_ref_add(appctx->ctx.map.ref, key, value, err);
752 else
753 ret = pat_ref_add(appctx->ctx.map.ref, key, NULL, err);
754 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
755
756 return ret;
757}
758
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200759static int cli_parse_add_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100760{
761 if (strcmp(args[1], "map") == 0 ||
762 strcmp(args[1], "acl") == 0) {
763 int ret;
764 char *err;
765
766 /* Set flags. */
767 if (args[1][0] == 'm')
768 appctx->ctx.map.display_flags = PAT_REF_MAP;
769 else
770 appctx->ctx.map.display_flags = PAT_REF_ACL;
771
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200772 /* If the keyword is "map", we expect:
773 * - three parameters if there is no payload
774 * - one parameter if there is a payload
775 * If it is "acl", we expect only two parameters
William Lallemandad8be612016-11-18 19:26:17 +0100776 */
777 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200778 if ((!payload && (!*args[2] || !*args[3] || !*args[4])) ||
Willy Tarreau9d008692019-08-09 11:21:01 +0200779 (payload && !*args[2]))
780 return cli_err(appctx,
781 "'add map' expects three parameters (map identifier, key and value)"
782 " or one parameter (map identifier) and a payload\n");
William Lallemandad8be612016-11-18 19:26:17 +0100783 }
Willy Tarreau9d008692019-08-09 11:21:01 +0200784 else if (!*args[2] || !*args[3])
785 return cli_err(appctx, "'add acl' expects two parameters: ACL identifier and pattern.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100786
787 /* Lookup for the reference. */
788 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
789 if (!appctx->ctx.map.ref) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200790 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
791 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
792 else
793 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100794 }
795
796 /* The command "add acl" is prohibited if the reference
797 * use samples.
798 */
799 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
800 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200801 return cli_err(appctx,
802 "This ACL is shared with a map containing samples. "
803 "You must use the command 'add map' to add values.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100804 }
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200805 /* Add value(s). */
William Lallemandad8be612016-11-18 19:26:17 +0100806 err = NULL;
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200807 if (!payload) {
808 ret = map_add_key_value(appctx, args[3], args[4], &err);
809 if (!ret) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200810 if (err)
811 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
812 else
813 return cli_err(appctx, "Failed to add an entry.\n");
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200814 }
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200815 }
816 else {
817 const char *end = payload + strlen(payload);
818
819 while (payload < end) {
820 char *key, *value;
821 size_t l;
822
823 /* key */
824 key = payload;
825 l = strcspn(key, " \t");
826 payload += l;
827
Willy Tarreau9d008692019-08-09 11:21:01 +0200828 if (!*payload && appctx->ctx.map.display_flags == PAT_REF_MAP)
829 return cli_dynerr(appctx, memprintf(&err, "Missing value for key '%s'.\n", key));
830
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200831 key[l] = 0;
832 payload++;
833
834 /* value */
835 payload += strspn(payload, " \t");
836 value = payload;
837 l = strcspn(value, "\n");
838 payload += l;
839 if (*payload)
840 payload++;
841 value[l] = 0;
842
843 ret = map_add_key_value(appctx, key, value, &err);
844 if (!ret) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200845 if (err)
846 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
847 else
848 return cli_err(appctx, "Failed to add a key.\n");
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200849 }
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200850 }
William Lallemandad8be612016-11-18 19:26:17 +0100851 }
852
853 /* The add is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100854 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100855 return 1;
856 }
857
858 return 0;
859}
860
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200861static int cli_parse_del_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100862{
863 if (args[1][0] == 'm')
864 appctx->ctx.map.display_flags = PAT_REF_MAP;
865 else
866 appctx->ctx.map.display_flags = PAT_REF_ACL;
867
868 /* Expect two parameters: map name and key. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200869 if (!*args[2] || !*args[3]) {
870 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
871 return cli_err(appctx, "This command expects two parameters: map identifier and key.\n");
872 else
873 return cli_err(appctx, "This command expects two parameters: ACL identifier and key.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100874 }
875
876 /* Lookup the reference in the maps. */
877 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
878 if (!appctx->ctx.map.ref ||
Willy Tarreau9d008692019-08-09 11:21:01 +0200879 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags))
880 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100881
882 /* If the entry identifier start with a '#', it is considered as
883 * pointer id
884 */
885 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
886 struct pat_ref_elt *ref;
887 long long int conv;
888 char *error;
889
890 /* Convert argument to integer value. */
891 conv = strtoll(&args[3][1], &error, 16);
Willy Tarreau9d008692019-08-09 11:21:01 +0200892 if (*error != '\0')
893 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100894
895 /* Convert and check integer to pointer. */
896 ref = (struct pat_ref_elt *)(long)conv;
Willy Tarreau9d008692019-08-09 11:21:01 +0200897 if ((long long int)(long)ref != conv)
898 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100899
900 /* Try to delete the entry. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100901 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100902 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100903 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100904 /* The entry is not found, send message. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200905 return cli_err(appctx, "Key not found.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100906 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100907 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100908 }
909 else {
910 /* Else, use the entry identifier as pattern
911 * string and try to delete the entry.
912 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100913 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100914 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100915 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100916 /* The entry is not found, send message. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200917 return cli_err(appctx, "Key not found.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100918 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100919 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100920 }
921
922 /* The deletion is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100923 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100924 return 1;
925}
926
927
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200928static int cli_parse_clear_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100929{
930 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
931 /* Set ACL or MAP flags. */
932 if (args[1][0] == 'm')
933 appctx->ctx.map.display_flags = PAT_REF_MAP;
934 else
935 appctx->ctx.map.display_flags = PAT_REF_ACL;
936
937 /* no parameter */
938 if (!*args[2]) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200939 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
940 return cli_err(appctx, "Missing map identifier.\n");
941 else
942 return cli_err(appctx, "Missing ACL identifier.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100943 }
944
945 /* lookup into the refs and check the map flag */
946 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
947 if (!appctx->ctx.map.ref ||
948 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200949 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
950 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
951 else
952 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100953 }
954
955 /* Clear all. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100956 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100957 pat_ref_prune(appctx->ctx.map.ref);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100958 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100959
960 /* return response */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100961 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100962 return 1;
963 }
964 return 0;
965}
966
967/* register cli keywords */
968
969static struct cli_kw_list cli_kws = {{ },{
970 { { "add", "acl", NULL }, "add acl : add acl entry", cli_parse_add_map, NULL },
971 { { "clear", "acl", NULL }, "clear acl <id> : clear the content of this acl", cli_parse_clear_map, NULL },
972 { { "del", "acl", NULL }, "del acl : delete acl entry", cli_parse_del_map, NULL },
973 { { "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 },
974 { { "show", "acl", NULL }, "show acl [id] : report available acls or dump an acl's contents", cli_parse_show_map, NULL },
975 { { "add", "map", NULL }, "add map : add map entry", cli_parse_add_map, NULL },
976 { { "clear", "map", NULL }, "clear map <id> : clear the content of this map", cli_parse_clear_map, NULL },
977 { { "del", "map", NULL }, "del map : delete map entry", cli_parse_del_map, NULL },
Nenad Merdanovic96c15712017-03-12 22:01:36 +0100978 { { "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 +0100979 { { "set", "map", NULL }, "set map : modify map entry", cli_parse_set_map, NULL },
980 { { "show", "map", NULL }, "show map [id] : report available maps or dump a map's contents", cli_parse_show_map, NULL },
981 { { NULL }, NULL, NULL, NULL }
982}};
983
Willy Tarreau0108d902018-11-25 19:14:37 +0100984INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemandad8be612016-11-18 19:26:17 +0100985
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100986/* Note: must not be declared <const> as its list will be overwritten
987 *
988 * For the map_*_int keywords, the output is declared as SMP_T_UINT, but the converter function
989 * can provide SMP_T_UINT, SMP_T_SINT or SMP_T_BOOL depending on how the patterns found in the
990 * file can be parsed.
991 *
992 * For the map_*_ip keyword, the output is declared as SMP_T_IPV4, but the converter function
993 * can provide SMP_T_IPV4 or SMP_T_IPV6 depending on the patterns found in the file.
994 *
995 * The map_* keywords only emit strings.
996 *
997 * The output type is only used during the configuration parsing. It is used for detecting
998 * compatibility problems.
999 *
1000 * The arguments are: <file>[,<default value>]
1001 */
1002static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER1edc9712014-12-15 16:18:39 +01001003 { "map", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1004 { "map_str", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1005 { "map_beg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_BEG },
1006 { "map_sub", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_SUB },
1007 { "map_dir", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DIR },
1008 { "map_dom", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DOM },
1009 { "map_end", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_END },
1010 { "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 +01001011 { "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 +02001012 { "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 +01001013 { "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 +01001014
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001015 { "map_str_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_STR },
1016 { "map_beg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_BEG },
1017 { "map_sub_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_SUB },
1018 { "map_dir_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DIR },
1019 { "map_dom_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DOM },
1020 { "map_end_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_END },
1021 { "map_reg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_REG },
1022 { "map_int_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_SINT, SMP_T_SINT, (void *)PAT_MATCH_INT },
1023 { "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 +01001024
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +02001025 { "map_str_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_STR },
1026 { "map_beg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_BEG },
1027 { "map_sub_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_SUB },
1028 { "map_dir_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DIR },
1029 { "map_dom_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DOM },
1030 { "map_end_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_END },
1031 { "map_reg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_REG },
1032 { "map_int_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_SINT, SMP_T_ADDR, (void *)PAT_MATCH_INT },
1033 { "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 +01001034
1035 { /* END */ },
1036}};
1037
Willy Tarreau0108d902018-11-25 19:14:37 +01001038INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);