blob: 77d687bb83902a65b6d655d4166ae225703321f7 [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 Tarreau3f0f82e2020-06-04 19:42:41 +020015#include <haproxy/applet-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020016#include <haproxy/api.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020017#include <haproxy/map.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020018#include <haproxy/pattern.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020019#include <haproxy/regex.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020020#include <haproxy/tools.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010021
William Lallemandad8be612016-11-18 19:26:17 +010022#include <types/cli.h>
William Lallemandad8be612016-11-18 19:26:17 +010023#include <types/stats.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010024
Willy Tarreauaa74c4e2020-06-04 10:19:23 +020025#include <haproxy/arg.h>
William Lallemandad8be612016-11-18 19:26:17 +010026#include <proto/cli.h>
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020027#include <proto/log.h>
William Lallemandad8be612016-11-18 19:26:17 +010028#include <proto/stream_interface.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020029#include <haproxy/sample.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010030
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020031/* Parse an IPv4 or IPv6 address and store it into the sample.
32 * The output type is IPv4 or IPv6.
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010033 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020034int map_parse_ip(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010035{
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020036 int len = strlen(text);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010037
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020038 if (buf2ip(text, len, &data->u.ipv4)) {
39 data->type = SMP_T_IPV4;
40 return 1;
41 }
42 if (buf2ip6(text, len, &data->u.ipv6)) {
43 data->type = SMP_T_IPV6;
44 return 1;
45 }
46 return 0;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010047}
48
49/* Parse a string and store a pointer to it into the sample. The original
50 * string must be left in memory because we return a direct memory reference.
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010051 * The output type is SMP_T_STR. There is no risk that the data will be
52 * overwritten because sample_conv_map() makes a const sample with this
53 * output.
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010054 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020055int map_parse_str(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010056{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020057 data->u.str.area = (char *)text;
58 data->u.str.data = strlen(text);
59 data->u.str.size = data->u.str.data + 1;
Thierry FOURNIER503bb092015-08-19 08:35:43 +020060 data->type = SMP_T_STR;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010061 return 1;
62}
63
64/* Parse an integer and convert it to a sample. The output type is SINT if the
65 * number is negative, or UINT if it is positive or null. The function returns
66 * zero (error) if the number is too large.
67 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020068int map_parse_int(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010069{
Thierry FOURNIER503bb092015-08-19 08:35:43 +020070 data->type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020071 data->u.sint = read_int64(&text, text + strlen(text));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020072 if (*text != '\0')
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010073 return 0;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010074 return 1;
75}
76
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010077/* This crete and initialize map descriptor.
78 * Return NULL if out of memory error
79 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010080static struct map_descriptor *map_create_descriptor(struct sample_conv *conv)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010081{
82 struct map_descriptor *desc;
83
84 desc = calloc(1, sizeof(*desc));
85 if (!desc)
86 return NULL;
87
88 desc->conv = conv;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010089
90 return desc;
91}
92
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010093/* This function load the map file according with data type declared into
94 * the "struct sample_conv".
95 *
96 * This function choose the indexation type (ebtree or list) according with
97 * the type of match needed.
98 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +020099int sample_load_map(struct arg *arg, struct sample_conv *conv,
100 const char *file, int line, char **err)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100101{
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100102 struct map_descriptor *desc;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100103
104 /* create new map descriptor */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100105 desc = map_create_descriptor(conv);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100106 if (!desc) {
107 memprintf(err, "out of memory");
108 return 0;
109 }
110
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100111 /* Initialize pattern */
112 pattern_init_head(&desc->pat);
113
114 /* This is original pattern, must free */
115 desc->do_free = 1;
116
117 /* Set the match method. */
Thierry FOURNIER1edc9712014-12-15 16:18:39 +0100118 desc->pat.match = pat_match_fcts[(long)conv->private];
119 desc->pat.parse = pat_parse_fcts[(long)conv->private];
120 desc->pat.index = pat_index_fcts[(long)conv->private];
121 desc->pat.delete = pat_delete_fcts[(long)conv->private];
122 desc->pat.prune = pat_prune_fcts[(long)conv->private];
123 desc->pat.expect_type = pat_match_types[(long)conv->private];
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100124
125 /* Set the output parse method. */
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100126 switch (desc->conv->out_type) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100127 case SMP_T_STR: desc->pat.parse_smp = map_parse_str; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200128 case SMP_T_SINT: desc->pat.parse_smp = map_parse_int; break;
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200129 case SMP_T_ADDR: desc->pat.parse_smp = map_parse_ip; break;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100130 default:
131 memprintf(err, "map: internal haproxy error: no default parse case for the input type <%d>.",
132 conv->out_type);
Andreas Seltenreich78f35952016-03-03 20:32:23 +0100133 free(desc);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100134 return 0;
135 }
136
Thierry FOURNIER39bef452014-01-29 13:29:45 +0100137 /* Load map. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200138 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 +0100139 1, err, file, line))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100140 return 0;
Thierry FOURNIER0ffe78c2013-12-05 14:40:25 +0100141
Willy Tarreauaa5801b2019-04-19 11:35:22 +0200142 /* the maps of type IP support a string as default value. This
143 * string can be an ipv4 or an ipv6, we must convert it.
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200144 */
Willy Tarreauaa5801b2019-04-19 11:35:22 +0200145 if (arg[1].type != ARGT_STOP && desc->conv->out_type == SMP_T_ADDR) {
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200146 struct sample_data data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200147 if (!map_parse_ip(arg[1].data.str.area, &data)) {
148 memprintf(err, "map: cannot parse default ip <%s>.",
149 arg[1].data.str.area);
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200150 return 0;
151 }
152 if (data.type == SMP_T_IPV4) {
153 arg[1].type = ARGT_IPV4;
154 arg[1].data.ipv4 = data.u.ipv4;
155 } else {
156 arg[1].type = ARGT_IPV6;
157 arg[1].data.ipv6 = data.u.ipv6;
158 }
159 }
160
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100161 /* replace the first argument by this definition */
162 arg[0].type = ARGT_MAP;
163 arg[0].data.map = desc;
164
165 return 1;
166}
167
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200168static int sample_conv_map(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100169{
170 struct map_descriptor *desc;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100171 struct pattern *pat;
Willy Tarreau83061a82018-07-13 11:56:34 +0200172 struct buffer *str;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100173
174 /* get config */
175 desc = arg_p[0].data.map;
176
177 /* Execute the match function. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100178 pat = pattern_exec_match(&desc->pat, smp, 1);
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100179
180 /* Match case. */
181 if (pat) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200182 if (pat->data) {
Thierry Fournier8feaa662016-02-10 22:55:20 +0100183 /* In the regm case, merge the sample with the input. */
184 if ((long)private == PAT_MATCH_REGM) {
Emeric Brun27102212018-07-17 09:47:07 -0400185 struct buffer *tmptrash;
Willy Tarreau2842e052018-08-22 04:55:43 +0200186 int len;
Emeric Brun27102212018-07-17 09:47:07 -0400187
188 /* Copy the content of the sample because it could
189 be scratched by incoming get_trash_chunk */
190 tmptrash = alloc_trash_chunk();
191 if (!tmptrash)
192 return 0;
193
194 tmptrash->data = smp->data.u.str.data;
195 if (tmptrash->data > (tmptrash->size-1))
196 tmptrash->data = tmptrash->size-1;
197
198 memcpy(tmptrash->area, smp->data.u.str.area, tmptrash->data);
199 tmptrash->area[tmptrash->data] = 0;
200
Thierry Fournier8feaa662016-02-10 22:55:20 +0100201 str = get_trash_chunk();
Willy Tarreau2842e052018-08-22 04:55:43 +0200202 len = exp_replace(str->area, str->size,
203 tmptrash->area,
204 pat->data->u.str.area,
205 (regmatch_t *)smp->ctx.a[0]);
Nenad Merdanovic646b7742019-04-12 22:54:28 +0200206 free_trash_chunk(tmptrash);
207
Willy Tarreau2842e052018-08-22 04:55:43 +0200208 if (len == -1)
Thierry Fournier8feaa662016-02-10 22:55:20 +0100209 return 0;
Willy Tarreau2842e052018-08-22 04:55:43 +0200210
211 str->data = len;
Thierry Fournier8feaa662016-02-10 22:55:20 +0100212 smp->data.u.str = *str;
213 return 1;
214 }
215 /* Copy sample. */
Thierry FOURNIER5cc18d42015-08-19 09:02:36 +0200216 smp->data = *pat->data;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100217 smp->flags |= SMP_F_CONST;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100218 return 1;
219 }
220
221 /* Return just int sample containing 1. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200222 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200223 smp->data.u.sint = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100224 return 1;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100225 }
226
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800227 /* If no default value available, the converter fails. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100228 if (arg_p[1].type == ARGT_STOP)
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100229 return 0;
230
231 /* Return the default value. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100232 switch (desc->conv->out_type) {
233
234 case SMP_T_STR:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200235 smp->data.type = SMP_T_STR;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100236 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200237 smp->data.u.str = arg_p[1].data.str;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100238 break;
239
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200240 case SMP_T_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200241 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200242 smp->data.u.sint = arg_p[1].data.sint;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100243 break;
244
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200245 case SMP_T_ADDR:
246 if (arg_p[1].type == ARGT_IPV4) {
247 smp->data.type = SMP_T_IPV4;
248 smp->data.u.ipv4 = arg_p[1].data.ipv4;
249 } else {
250 smp->data.type = SMP_T_IPV6;
251 smp->data.u.ipv6 = arg_p[1].data.ipv6;
252 }
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100253 break;
254 }
255
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100256 return 1;
257}
258
William Lallemandad8be612016-11-18 19:26:17 +0100259/* This function is used with map and acl management. It permits to browse
260 * each reference. The variable <getnext> must contain the current node,
261 * <end> point to the root node and the <flags> permit to filter required
262 * nodes.
263 */
264static inline
265struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
266 unsigned int flags)
267{
268 struct pat_ref *ref = getnext;
269
270 while (1) {
271
272 /* Get next list entry. */
273 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
274
275 /* If the entry is the last of the list, return NULL. */
276 if (&ref->list == end)
277 return NULL;
278
279 /* If the entry match the flag, return it. */
280 if (ref->flags & flags)
281 return ref;
282 }
283}
284
285static inline
286struct pat_ref *pat_ref_lookup_ref(const char *reference)
287{
288 int id;
289 char *error;
290
291 /* If the reference starts by a '#', this is numeric id. */
292 if (reference[0] == '#') {
293 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
294 id = strtol(reference + 1, &error, 10);
295 if (*error != '\0')
296 return NULL;
297
298 /* Perform the unique id lookup. */
299 return pat_ref_lookupid(id);
300 }
301
302 /* Perform the string lookup. */
303 return pat_ref_lookup(reference);
304}
305
306/* This function is used with map and acl management. It permits to browse
307 * each reference.
308 */
309static inline
310struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
311{
312 struct pattern_expr *expr;
313 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
314 if (&expr->list == end)
315 return NULL;
316 return expr;
317}
318
319static int cli_io_handler_pat_list(struct appctx *appctx)
320{
321 struct stream_interface *si = appctx->owner;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200322 struct pat_ref_elt *elt;
323
324 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
325 /* If we're forced to shut down, we might have to remove our
326 * reference to the last ref_elt being dumped.
327 */
328 if (appctx->st2 == STAT_ST_LIST) {
Dragan Dosen336a11f2018-05-04 16:27:15 +0200329 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users)) {
330 LIST_DEL(&appctx->ctx.map.bref.users);
331 LIST_INIT(&appctx->ctx.map.bref.users);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200332 }
333 }
334 return 1;
335 }
William Lallemandad8be612016-11-18 19:26:17 +0100336
337 switch (appctx->st2) {
338
339 case STAT_ST_INIT:
Emeric Brun8d85aa42017-06-29 15:40:33 +0200340 /* the function had not been called yet, let's prepare the
341 * buffer for a response. We initialize the current stream
342 * pointer to the first in the global list. When a target
343 * stream is being destroyed, it is responsible for updating
344 * this pointer. We know we have reached the end when this
345 * pointer points back to the head of the streams list.
346 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100347 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200348 LIST_INIT(&appctx->ctx.map.bref.users);
349 appctx->ctx.map.bref.ref = appctx->ctx.map.ref->head.n;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100350 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100351 appctx->st2 = STAT_ST_LIST;
352 /* fall through */
353
354 case STAT_ST_LIST:
Emeric Brunb5997f72017-07-03 11:34:05 +0200355
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100356 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200357
Emeric Brun8d85aa42017-06-29 15:40:33 +0200358 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users)) {
359 LIST_DEL(&appctx->ctx.map.bref.users);
360 LIST_INIT(&appctx->ctx.map.bref.users);
361 }
362
363 while (appctx->ctx.map.bref.ref != &appctx->ctx.map.ref->head) {
William Lallemandad8be612016-11-18 19:26:17 +0100364 chunk_reset(&trash);
365
Emeric Brun8d85aa42017-06-29 15:40:33 +0200366 elt = LIST_ELEM(appctx->ctx.map.bref.ref, struct pat_ref_elt *, list);
367
William Lallemandad8be612016-11-18 19:26:17 +0100368 /* build messages */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200369 if (elt->sample)
William Lallemandad8be612016-11-18 19:26:17 +0100370 chunk_appendf(&trash, "%p %s %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200371 elt, elt->pattern,
372 elt->sample);
William Lallemandad8be612016-11-18 19:26:17 +0100373 else
374 chunk_appendf(&trash, "%p %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200375 elt, elt->pattern);
William Lallemandad8be612016-11-18 19:26:17 +0100376
Willy Tarreau06d80a92017-10-19 14:32:15 +0200377 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100378 /* let's try again later from this stream. We add ourselves into
379 * this stream's users so that it can remove us upon termination.
380 */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200381 LIST_ADDQ(&elt->back_refs, &appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100382 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +0100383 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100384 return 0;
385 }
386
387 /* get next list entry and check the end of the list */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200388 appctx->ctx.map.bref.ref = elt->list.n;
William Lallemandad8be612016-11-18 19:26:17 +0100389 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100390 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100391 /* fall through */
392
393 default:
394 appctx->st2 = STAT_ST_FIN;
395 return 1;
396 }
397}
398
399static int cli_io_handler_pats_list(struct appctx *appctx)
400{
401 struct stream_interface *si = appctx->owner;
402
403 switch (appctx->st2) {
404 case STAT_ST_INIT:
405 /* Display the column headers. If the message cannot be sent,
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800406 * quit the function with returning 0. The function is called
407 * later and restarted at the state "STAT_ST_INIT".
William Lallemandad8be612016-11-18 19:26:17 +0100408 */
409 chunk_reset(&trash);
410 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +0200411 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +0100412 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100413 return 0;
414 }
415
416 /* Now, we start the browsing of the references lists.
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800417 * Note that the following call to LIST_ELEM returns a bad pointer. The only
William Lallemandad8be612016-11-18 19:26:17 +0100418 * available field of this pointer is <list>. It is used with the function
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800419 * pat_list_get_next() for returning the first available entry
William Lallemandad8be612016-11-18 19:26:17 +0100420 */
421 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
422 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
423 appctx->ctx.map.display_flags);
424 appctx->st2 = STAT_ST_LIST;
425 /* fall through */
426
427 case STAT_ST_LIST:
428 while (appctx->ctx.map.ref) {
429 chunk_reset(&trash);
430
431 /* Build messages. If the reference is used by another category than
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800432 * the listed categories, display the information in the message.
William Lallemandad8be612016-11-18 19:26:17 +0100433 */
434 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
435 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
436 appctx->ctx.map.ref->display);
437
Willy Tarreau06d80a92017-10-19 14:32:15 +0200438 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100439 /* let's try again later from this stream. We add ourselves into
440 * this stream's users so that it can remove us upon termination.
441 */
Willy Tarreaudb398432018-11-15 11:08:52 +0100442 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100443 return 0;
444 }
445
446 /* get next list entry and check the end of the list */
447 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
448 appctx->ctx.map.display_flags);
449 }
450
William Lallemandad8be612016-11-18 19:26:17 +0100451 /* fall through */
452
453 default:
454 appctx->st2 = STAT_ST_FIN;
455 return 1;
456 }
457 return 0;
458}
459
460static int cli_io_handler_map_lookup(struct appctx *appctx)
461{
462 struct stream_interface *si = appctx->owner;
463 struct sample sample;
464 struct pattern *pat;
465 int match_method;
466
467 switch (appctx->st2) {
468 case STAT_ST_INIT:
469 /* Init to the first entry. The list cannot be change */
470 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
471 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
472 appctx->st2 = STAT_ST_LIST;
473 /* fall through */
474
475 case STAT_ST_LIST:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100476 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100477 /* for each lookup type */
478 while (appctx->ctx.map.expr) {
479 /* initialise chunk to build new message */
480 chunk_reset(&trash);
481
482 /* execute pattern matching */
483 sample.data.type = SMP_T_STR;
484 sample.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200485 sample.data.u.str.data = appctx->ctx.map.chunk.data;
486 sample.data.u.str.area = appctx->ctx.map.chunk.area;
Emeric Brunb5997f72017-07-03 11:34:05 +0200487
William Lallemandad8be612016-11-18 19:26:17 +0100488 if (appctx->ctx.map.expr->pat_head->match &&
489 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
490 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
491 else
492 pat = NULL;
493
494 /* build return message: set type of match */
495 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
496 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
497 break;
498 if (match_method >= PAT_MATCH_NUM)
499 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
500 else
501 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
502
503 /* case sensitive */
504 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
505 chunk_appendf(&trash, ", case=insensitive");
506 else
507 chunk_appendf(&trash, ", case=sensitive");
508
509 /* Display no match, and set default value */
510 if (!pat) {
511 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
512 chunk_appendf(&trash, ", found=no");
513 else
514 chunk_appendf(&trash, ", match=no");
515 }
516
517 /* Display match and match info */
518 else {
519 /* display match */
520 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
521 chunk_appendf(&trash, ", found=yes");
522 else
523 chunk_appendf(&trash, ", match=yes");
524
525 /* display index mode */
526 if (pat->sflags & PAT_SF_TREE)
527 chunk_appendf(&trash, ", idx=tree");
528 else
529 chunk_appendf(&trash, ", idx=list");
530
531 /* display pattern */
532 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
533 if (pat->ref && pat->ref->pattern)
534 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
535 else
536 chunk_appendf(&trash, ", key=unknown");
537 }
538 else {
539 if (pat->ref && pat->ref->pattern)
540 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
541 else
542 chunk_appendf(&trash, ", pattern=unknown");
543 }
544
545 /* display return value */
546 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
547 if (pat->data && pat->ref && pat->ref->sample)
548 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
549 smp_to_type[pat->data->type]);
550 else
551 chunk_appendf(&trash, ", value=none");
552 }
553 }
554
555 chunk_appendf(&trash, "\n");
556
557 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200558 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100559 /* let's try again later from this stream. We add ourselves into
560 * this stream's users so that it can remove us upon termination.
561 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100562 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +0100563 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100564 return 0;
565 }
566
567 /* get next entry */
568 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
569 &appctx->ctx.map.ref->pat);
570 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100571 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100572 /* fall through */
573
574 default:
575 appctx->st2 = STAT_ST_FIN;
William Lallemandad8be612016-11-18 19:26:17 +0100576 return 1;
577 }
578}
579
580static void cli_release_mlook(struct appctx *appctx)
581{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200582 free(appctx->ctx.map.chunk.area);
583 appctx->ctx.map.chunk.area = NULL;
William Lallemandad8be612016-11-18 19:26:17 +0100584}
585
586
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200587static int cli_parse_get_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100588{
589 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
590 /* Set flags. */
591 if (args[1][0] == 'm')
592 appctx->ctx.map.display_flags = PAT_REF_MAP;
593 else
594 appctx->ctx.map.display_flags = PAT_REF_ACL;
595
596 /* No parameter. */
597 if (!*args[2] || !*args[3]) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200598 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
599 return cli_err(appctx, "Missing map identifier and/or key.\n");
600 else
601 return cli_err(appctx, "Missing ACL identifier and/or key.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100602 }
603
604 /* lookup into the maps */
605 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
606 if (!appctx->ctx.map.ref) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200607 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
608 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
609 else
610 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100611 }
612
613 /* copy input string. The string must be allocated because
614 * it may be used over multiple iterations. It's released
615 * at the end and upon abort anyway.
616 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200617 appctx->ctx.map.chunk.data = strlen(args[3]);
618 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.data + 1;
619 appctx->ctx.map.chunk.area = strdup(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200620 if (!appctx->ctx.map.chunk.area)
621 return cli_err(appctx, "Out of memory error.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100622
623 return 0;
624 }
625 return 1;
626}
627
Emeric Brun8d85aa42017-06-29 15:40:33 +0200628static void cli_release_show_map(struct appctx *appctx)
629{
630 if (appctx->st2 == STAT_ST_LIST) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100631 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200632 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users))
633 LIST_DEL(&appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100634 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200635 }
636}
637
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200638static int cli_parse_show_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100639{
640 if (strcmp(args[1], "map") == 0 ||
641 strcmp(args[1], "acl") == 0) {
642
643 /* Set ACL or MAP flags. */
644 if (args[1][0] == 'm')
645 appctx->ctx.map.display_flags = PAT_REF_MAP;
646 else
647 appctx->ctx.map.display_flags = PAT_REF_ACL;
648
649 /* no parameter: display all map available */
650 if (!*args[2]) {
William Lallemandad8be612016-11-18 19:26:17 +0100651 appctx->io_handler = cli_io_handler_pats_list;
652 return 0;
653 }
654
655 /* lookup into the refs and check the map flag */
656 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
657 if (!appctx->ctx.map.ref ||
658 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200659 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
660 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
661 else
662 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100663 }
William Lallemandad8be612016-11-18 19:26:17 +0100664 appctx->io_handler = cli_io_handler_pat_list;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200665 appctx->io_release = cli_release_show_map;
William Lallemandad8be612016-11-18 19:26:17 +0100666 return 0;
667 }
668
669 return 0;
670}
671
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200672static int cli_parse_set_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100673{
674 if (strcmp(args[1], "map") == 0) {
675 char *err;
676
677 /* Set flags. */
678 appctx->ctx.map.display_flags = PAT_REF_MAP;
679
680 /* Expect three parameters: map name, key and new value. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200681 if (!*args[2] || !*args[3] || !*args[4])
682 return cli_err(appctx, "'set map' expects three parameters: map identifier, key and value.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100683
684 /* Lookup the reference in the maps. */
685 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200686 if (!appctx->ctx.map.ref)
687 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100688
689 /* If the entry identifier start with a '#', it is considered as
690 * pointer id
691 */
692 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
693 struct pat_ref_elt *ref;
694 long long int conv;
695 char *error;
696
697 /* Convert argument to integer value. */
698 conv = strtoll(&args[3][1], &error, 16);
Willy Tarreau9d008692019-08-09 11:21:01 +0200699 if (*error != '\0')
700 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100701
702 /* Convert and check integer to pointer. */
703 ref = (struct pat_ref_elt *)(long)conv;
Willy Tarreau9d008692019-08-09 11:21:01 +0200704 if ((long long int)(long)ref != conv)
705 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100706
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200707 /* Try to modify the entry. */
William Lallemandad8be612016-11-18 19:26:17 +0100708 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100709 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100710 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100711 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +0200712 if (err)
713 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
714 else
715 return cli_err(appctx, "Failed to update an entry.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100716 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100717 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100718 }
719 else {
720 /* Else, use the entry identifier as pattern
721 * string, and update the value.
722 */
723 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100724 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100725 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100726 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +0200727 if (err)
728 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
729 else
730 return cli_err(appctx, "Failed to update an entry.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100731 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100732 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100733 }
734
735 /* The set is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100736 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100737 return 0;
738 }
739 return 1;
740}
741
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200742static int map_add_key_value(struct appctx *appctx, const char *key, const char *value, char **err)
743{
744 int ret;
745
746 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
747 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
748 ret = pat_ref_add(appctx->ctx.map.ref, key, value, err);
749 else
750 ret = pat_ref_add(appctx->ctx.map.ref, key, NULL, err);
751 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
752
753 return ret;
754}
755
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200756static int cli_parse_add_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100757{
758 if (strcmp(args[1], "map") == 0 ||
759 strcmp(args[1], "acl") == 0) {
760 int ret;
761 char *err;
762
763 /* Set flags. */
764 if (args[1][0] == 'm')
765 appctx->ctx.map.display_flags = PAT_REF_MAP;
766 else
767 appctx->ctx.map.display_flags = PAT_REF_ACL;
768
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200769 /* If the keyword is "map", we expect:
770 * - three parameters if there is no payload
771 * - one parameter if there is a payload
772 * If it is "acl", we expect only two parameters
William Lallemandad8be612016-11-18 19:26:17 +0100773 */
774 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200775 if ((!payload && (!*args[2] || !*args[3] || !*args[4])) ||
Willy Tarreau9d008692019-08-09 11:21:01 +0200776 (payload && !*args[2]))
777 return cli_err(appctx,
778 "'add map' expects three parameters (map identifier, key and value)"
779 " or one parameter (map identifier) and a payload\n");
William Lallemandad8be612016-11-18 19:26:17 +0100780 }
Willy Tarreau9d008692019-08-09 11:21:01 +0200781 else if (!*args[2] || !*args[3])
782 return cli_err(appctx, "'add acl' expects two parameters: ACL identifier and pattern.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100783
784 /* Lookup for the reference. */
785 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
786 if (!appctx->ctx.map.ref) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200787 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
788 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
789 else
790 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100791 }
792
793 /* The command "add acl" is prohibited if the reference
794 * use samples.
795 */
796 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
797 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200798 return cli_err(appctx,
799 "This ACL is shared with a map containing samples. "
800 "You must use the command 'add map' to add values.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100801 }
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200802 /* Add value(s). */
William Lallemandad8be612016-11-18 19:26:17 +0100803 err = NULL;
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200804 if (!payload) {
805 ret = map_add_key_value(appctx, args[3], args[4], &err);
806 if (!ret) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200807 if (err)
808 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
809 else
810 return cli_err(appctx, "Failed to add an entry.\n");
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200811 }
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200812 }
813 else {
814 const char *end = payload + strlen(payload);
815
816 while (payload < end) {
817 char *key, *value;
818 size_t l;
819
820 /* key */
821 key = payload;
822 l = strcspn(key, " \t");
823 payload += l;
824
Willy Tarreau9d008692019-08-09 11:21:01 +0200825 if (!*payload && appctx->ctx.map.display_flags == PAT_REF_MAP)
826 return cli_dynerr(appctx, memprintf(&err, "Missing value for key '%s'.\n", key));
827
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200828 key[l] = 0;
829 payload++;
830
831 /* value */
832 payload += strspn(payload, " \t");
833 value = payload;
834 l = strcspn(value, "\n");
835 payload += l;
836 if (*payload)
837 payload++;
838 value[l] = 0;
839
840 ret = map_add_key_value(appctx, key, value, &err);
841 if (!ret) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200842 if (err)
843 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
844 else
845 return cli_err(appctx, "Failed to add a key.\n");
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200846 }
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200847 }
William Lallemandad8be612016-11-18 19:26:17 +0100848 }
849
850 /* The add is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100851 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100852 return 1;
853 }
854
855 return 0;
856}
857
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200858static int cli_parse_del_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100859{
860 if (args[1][0] == 'm')
861 appctx->ctx.map.display_flags = PAT_REF_MAP;
862 else
863 appctx->ctx.map.display_flags = PAT_REF_ACL;
864
865 /* Expect two parameters: map name and key. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200866 if (!*args[2] || !*args[3]) {
867 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
868 return cli_err(appctx, "This command expects two parameters: map identifier and key.\n");
869 else
870 return cli_err(appctx, "This command expects two parameters: ACL identifier and key.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100871 }
872
873 /* Lookup the reference in the maps. */
874 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
875 if (!appctx->ctx.map.ref ||
Willy Tarreau9d008692019-08-09 11:21:01 +0200876 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags))
877 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100878
879 /* If the entry identifier start with a '#', it is considered as
880 * pointer id
881 */
882 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
883 struct pat_ref_elt *ref;
884 long long int conv;
885 char *error;
886
887 /* Convert argument to integer value. */
888 conv = strtoll(&args[3][1], &error, 16);
Willy Tarreau9d008692019-08-09 11:21:01 +0200889 if (*error != '\0')
890 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100891
892 /* Convert and check integer to pointer. */
893 ref = (struct pat_ref_elt *)(long)conv;
Willy Tarreau9d008692019-08-09 11:21:01 +0200894 if ((long long int)(long)ref != conv)
895 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100896
897 /* Try to delete the entry. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100898 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100899 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100900 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100901 /* The entry is not found, send message. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200902 return cli_err(appctx, "Key not found.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100903 }
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 }
906 else {
907 /* Else, use the entry identifier as pattern
908 * string and try to delete the entry.
909 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100910 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100911 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100912 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100913 /* The entry is not found, send message. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200914 return cli_err(appctx, "Key not found.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100915 }
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 }
918
919 /* The deletion is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100920 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100921 return 1;
922}
923
924
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200925static int cli_parse_clear_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100926{
927 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
928 /* Set ACL or MAP flags. */
929 if (args[1][0] == 'm')
930 appctx->ctx.map.display_flags = PAT_REF_MAP;
931 else
932 appctx->ctx.map.display_flags = PAT_REF_ACL;
933
934 /* no parameter */
935 if (!*args[2]) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200936 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
937 return cli_err(appctx, "Missing map identifier.\n");
938 else
939 return cli_err(appctx, "Missing ACL identifier.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100940 }
941
942 /* lookup into the refs and check the map flag */
943 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
944 if (!appctx->ctx.map.ref ||
945 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200946 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
947 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
948 else
949 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100950 }
951
952 /* Clear all. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100953 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100954 pat_ref_prune(appctx->ctx.map.ref);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100955 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100956
957 /* return response */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100958 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100959 return 1;
960 }
961 return 0;
962}
963
964/* register cli keywords */
965
966static struct cli_kw_list cli_kws = {{ },{
967 { { "add", "acl", NULL }, "add acl : add acl entry", cli_parse_add_map, NULL },
968 { { "clear", "acl", NULL }, "clear acl <id> : clear the content of this acl", cli_parse_clear_map, NULL },
969 { { "del", "acl", NULL }, "del acl : delete acl entry", cli_parse_del_map, NULL },
970 { { "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 },
971 { { "show", "acl", NULL }, "show acl [id] : report available acls or dump an acl's contents", cli_parse_show_map, NULL },
972 { { "add", "map", NULL }, "add map : add map entry", cli_parse_add_map, NULL },
973 { { "clear", "map", NULL }, "clear map <id> : clear the content of this map", cli_parse_clear_map, NULL },
974 { { "del", "map", NULL }, "del map : delete map entry", cli_parse_del_map, NULL },
Nenad Merdanovic96c15712017-03-12 22:01:36 +0100975 { { "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 +0100976 { { "set", "map", NULL }, "set map : modify map entry", cli_parse_set_map, NULL },
977 { { "show", "map", NULL }, "show map [id] : report available maps or dump a map's contents", cli_parse_show_map, NULL },
978 { { NULL }, NULL, NULL, NULL }
979}};
980
Willy Tarreau0108d902018-11-25 19:14:37 +0100981INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemandad8be612016-11-18 19:26:17 +0100982
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100983/* Note: must not be declared <const> as its list will be overwritten
984 *
985 * For the map_*_int keywords, the output is declared as SMP_T_UINT, but the converter function
986 * can provide SMP_T_UINT, SMP_T_SINT or SMP_T_BOOL depending on how the patterns found in the
987 * file can be parsed.
988 *
989 * For the map_*_ip keyword, the output is declared as SMP_T_IPV4, but the converter function
990 * can provide SMP_T_IPV4 or SMP_T_IPV6 depending on the patterns found in the file.
991 *
992 * The map_* keywords only emit strings.
993 *
994 * The output type is only used during the configuration parsing. It is used for detecting
995 * compatibility problems.
996 *
997 * The arguments are: <file>[,<default value>]
998 */
999static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER1edc9712014-12-15 16:18:39 +01001000 { "map", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1001 { "map_str", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1002 { "map_beg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_BEG },
1003 { "map_sub", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_SUB },
1004 { "map_dir", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DIR },
1005 { "map_dom", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DOM },
1006 { "map_end", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_END },
1007 { "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 +01001008 { "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 +02001009 { "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 +01001010 { "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 +01001011
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001012 { "map_str_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_STR },
1013 { "map_beg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_BEG },
1014 { "map_sub_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_SUB },
1015 { "map_dir_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DIR },
1016 { "map_dom_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DOM },
1017 { "map_end_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_END },
1018 { "map_reg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_REG },
1019 { "map_int_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_SINT, SMP_T_SINT, (void *)PAT_MATCH_INT },
1020 { "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 +01001021
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +02001022 { "map_str_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_STR },
1023 { "map_beg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_BEG },
1024 { "map_sub_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_SUB },
1025 { "map_dir_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DIR },
1026 { "map_dom_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DOM },
1027 { "map_end_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_END },
1028 { "map_reg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_REG },
1029 { "map_int_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_SINT, SMP_T_ADDR, (void *)PAT_MATCH_INT },
1030 { "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 +01001031
1032 { /* END */ },
1033}};
1034
Willy Tarreau0108d902018-11-25 19:14:37 +01001035INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);