blob: 33f24dd9fb196f13a553d006bb4fa583741a845e [file] [log] [blame]
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +01001/*
2 * MAP management functions.
3 *
4 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010013#include <stdio.h>
14
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020016#include <haproxy/map.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020017#include <haproxy/pattern.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020018#include <haproxy/regex.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020019#include <haproxy/tools.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010020
William Lallemandad8be612016-11-18 19:26:17 +010021#include <types/applet.h>
22#include <types/cli.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010023#include <types/global.h>
William Lallemandad8be612016-11-18 19:26:17 +010024#include <types/stats.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010025
William Lallemandad8be612016-11-18 19:26:17 +010026#include <proto/applet.h>
Willy Tarreauaa74c4e2020-06-04 10:19:23 +020027#include <haproxy/arg.h>
William Lallemandad8be612016-11-18 19:26:17 +010028#include <proto/cli.h>
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020029#include <proto/log.h>
William Lallemandad8be612016-11-18 19:26:17 +010030#include <proto/stream_interface.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020031#include <haproxy/sample.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010032
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020033/* Parse an IPv4 or IPv6 address and store it into the sample.
34 * The output type is IPv4 or IPv6.
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010035 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020036int map_parse_ip(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010037{
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020038 int len = strlen(text);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010039
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020040 if (buf2ip(text, len, &data->u.ipv4)) {
41 data->type = SMP_T_IPV4;
42 return 1;
43 }
44 if (buf2ip6(text, len, &data->u.ipv6)) {
45 data->type = SMP_T_IPV6;
46 return 1;
47 }
48 return 0;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010049}
50
51/* Parse a string and store a pointer to it into the sample. The original
52 * string must be left in memory because we return a direct memory reference.
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010053 * The output type is SMP_T_STR. There is no risk that the data will be
54 * overwritten because sample_conv_map() makes a const sample with this
55 * output.
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010056 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020057int map_parse_str(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010058{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020059 data->u.str.area = (char *)text;
60 data->u.str.data = strlen(text);
61 data->u.str.size = data->u.str.data + 1;
Thierry FOURNIER503bb092015-08-19 08:35:43 +020062 data->type = SMP_T_STR;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010063 return 1;
64}
65
66/* Parse an integer and convert it to a sample. The output type is SINT if the
67 * number is negative, or UINT if it is positive or null. The function returns
68 * zero (error) if the number is too large.
69 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020070int map_parse_int(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010071{
Thierry FOURNIER503bb092015-08-19 08:35:43 +020072 data->type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020073 data->u.sint = read_int64(&text, text + strlen(text));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020074 if (*text != '\0')
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010075 return 0;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010076 return 1;
77}
78
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010079/* This crete and initialize map descriptor.
80 * Return NULL if out of memory error
81 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010082static struct map_descriptor *map_create_descriptor(struct sample_conv *conv)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010083{
84 struct map_descriptor *desc;
85
86 desc = calloc(1, sizeof(*desc));
87 if (!desc)
88 return NULL;
89
90 desc->conv = conv;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010091
92 return desc;
93}
94
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010095/* This function load the map file according with data type declared into
96 * the "struct sample_conv".
97 *
98 * This function choose the indexation type (ebtree or list) according with
99 * the type of match needed.
100 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200101int sample_load_map(struct arg *arg, struct sample_conv *conv,
102 const char *file, int line, char **err)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100103{
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100104 struct map_descriptor *desc;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100105
106 /* create new map descriptor */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100107 desc = map_create_descriptor(conv);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100108 if (!desc) {
109 memprintf(err, "out of memory");
110 return 0;
111 }
112
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100113 /* Initialize pattern */
114 pattern_init_head(&desc->pat);
115
116 /* This is original pattern, must free */
117 desc->do_free = 1;
118
119 /* Set the match method. */
Thierry FOURNIER1edc9712014-12-15 16:18:39 +0100120 desc->pat.match = pat_match_fcts[(long)conv->private];
121 desc->pat.parse = pat_parse_fcts[(long)conv->private];
122 desc->pat.index = pat_index_fcts[(long)conv->private];
123 desc->pat.delete = pat_delete_fcts[(long)conv->private];
124 desc->pat.prune = pat_prune_fcts[(long)conv->private];
125 desc->pat.expect_type = pat_match_types[(long)conv->private];
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100126
127 /* Set the output parse method. */
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100128 switch (desc->conv->out_type) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100129 case SMP_T_STR: desc->pat.parse_smp = map_parse_str; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200130 case SMP_T_SINT: desc->pat.parse_smp = map_parse_int; break;
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200131 case SMP_T_ADDR: desc->pat.parse_smp = map_parse_ip; break;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100132 default:
133 memprintf(err, "map: internal haproxy error: no default parse case for the input type <%d>.",
134 conv->out_type);
Andreas Seltenreich78f35952016-03-03 20:32:23 +0100135 free(desc);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100136 return 0;
137 }
138
Thierry FOURNIER39bef452014-01-29 13:29:45 +0100139 /* Load map. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200140 if (!pattern_read_from_file(&desc->pat, PAT_REF_MAP, arg[0].data.str.area, PAT_MF_NO_DNS,
Thierry FOURNIER94580c92014-02-11 14:36:45 +0100141 1, err, file, line))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100142 return 0;
Thierry FOURNIER0ffe78c2013-12-05 14:40:25 +0100143
Willy Tarreauaa5801b2019-04-19 11:35:22 +0200144 /* the maps of type IP support a string as default value. This
145 * string can be an ipv4 or an ipv6, we must convert it.
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200146 */
Willy Tarreauaa5801b2019-04-19 11:35:22 +0200147 if (arg[1].type != ARGT_STOP && desc->conv->out_type == SMP_T_ADDR) {
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200148 struct sample_data data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200149 if (!map_parse_ip(arg[1].data.str.area, &data)) {
150 memprintf(err, "map: cannot parse default ip <%s>.",
151 arg[1].data.str.area);
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200152 return 0;
153 }
154 if (data.type == SMP_T_IPV4) {
155 arg[1].type = ARGT_IPV4;
156 arg[1].data.ipv4 = data.u.ipv4;
157 } else {
158 arg[1].type = ARGT_IPV6;
159 arg[1].data.ipv6 = data.u.ipv6;
160 }
161 }
162
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100163 /* replace the first argument by this definition */
164 arg[0].type = ARGT_MAP;
165 arg[0].data.map = desc;
166
167 return 1;
168}
169
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200170static int sample_conv_map(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100171{
172 struct map_descriptor *desc;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100173 struct pattern *pat;
Willy Tarreau83061a82018-07-13 11:56:34 +0200174 struct buffer *str;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100175
176 /* get config */
177 desc = arg_p[0].data.map;
178
179 /* Execute the match function. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100180 pat = pattern_exec_match(&desc->pat, smp, 1);
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100181
182 /* Match case. */
183 if (pat) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200184 if (pat->data) {
Thierry Fournier8feaa662016-02-10 22:55:20 +0100185 /* In the regm case, merge the sample with the input. */
186 if ((long)private == PAT_MATCH_REGM) {
Emeric Brun27102212018-07-17 09:47:07 -0400187 struct buffer *tmptrash;
Willy Tarreau2842e052018-08-22 04:55:43 +0200188 int len;
Emeric Brun27102212018-07-17 09:47:07 -0400189
190 /* Copy the content of the sample because it could
191 be scratched by incoming get_trash_chunk */
192 tmptrash = alloc_trash_chunk();
193 if (!tmptrash)
194 return 0;
195
196 tmptrash->data = smp->data.u.str.data;
197 if (tmptrash->data > (tmptrash->size-1))
198 tmptrash->data = tmptrash->size-1;
199
200 memcpy(tmptrash->area, smp->data.u.str.area, tmptrash->data);
201 tmptrash->area[tmptrash->data] = 0;
202
Thierry Fournier8feaa662016-02-10 22:55:20 +0100203 str = get_trash_chunk();
Willy Tarreau2842e052018-08-22 04:55:43 +0200204 len = exp_replace(str->area, str->size,
205 tmptrash->area,
206 pat->data->u.str.area,
207 (regmatch_t *)smp->ctx.a[0]);
Nenad Merdanovic646b7742019-04-12 22:54:28 +0200208 free_trash_chunk(tmptrash);
209
Willy Tarreau2842e052018-08-22 04:55:43 +0200210 if (len == -1)
Thierry Fournier8feaa662016-02-10 22:55:20 +0100211 return 0;
Willy Tarreau2842e052018-08-22 04:55:43 +0200212
213 str->data = len;
Thierry Fournier8feaa662016-02-10 22:55:20 +0100214 smp->data.u.str = *str;
215 return 1;
216 }
217 /* Copy sample. */
Thierry FOURNIER5cc18d42015-08-19 09:02:36 +0200218 smp->data = *pat->data;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100219 smp->flags |= SMP_F_CONST;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100220 return 1;
221 }
222
223 /* Return just int sample containing 1. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200224 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200225 smp->data.u.sint = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100226 return 1;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100227 }
228
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800229 /* If no default value available, the converter fails. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100230 if (arg_p[1].type == ARGT_STOP)
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100231 return 0;
232
233 /* Return the default value. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100234 switch (desc->conv->out_type) {
235
236 case SMP_T_STR:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200237 smp->data.type = SMP_T_STR;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100238 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200239 smp->data.u.str = arg_p[1].data.str;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100240 break;
241
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200242 case SMP_T_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200243 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200244 smp->data.u.sint = arg_p[1].data.sint;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100245 break;
246
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200247 case SMP_T_ADDR:
248 if (arg_p[1].type == ARGT_IPV4) {
249 smp->data.type = SMP_T_IPV4;
250 smp->data.u.ipv4 = arg_p[1].data.ipv4;
251 } else {
252 smp->data.type = SMP_T_IPV6;
253 smp->data.u.ipv6 = arg_p[1].data.ipv6;
254 }
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100255 break;
256 }
257
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100258 return 1;
259}
260
William Lallemandad8be612016-11-18 19:26:17 +0100261/* This function is used with map and acl management. It permits to browse
262 * each reference. The variable <getnext> must contain the current node,
263 * <end> point to the root node and the <flags> permit to filter required
264 * nodes.
265 */
266static inline
267struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
268 unsigned int flags)
269{
270 struct pat_ref *ref = getnext;
271
272 while (1) {
273
274 /* Get next list entry. */
275 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
276
277 /* If the entry is the last of the list, return NULL. */
278 if (&ref->list == end)
279 return NULL;
280
281 /* If the entry match the flag, return it. */
282 if (ref->flags & flags)
283 return ref;
284 }
285}
286
287static inline
288struct pat_ref *pat_ref_lookup_ref(const char *reference)
289{
290 int id;
291 char *error;
292
293 /* If the reference starts by a '#', this is numeric id. */
294 if (reference[0] == '#') {
295 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
296 id = strtol(reference + 1, &error, 10);
297 if (*error != '\0')
298 return NULL;
299
300 /* Perform the unique id lookup. */
301 return pat_ref_lookupid(id);
302 }
303
304 /* Perform the string lookup. */
305 return pat_ref_lookup(reference);
306}
307
308/* This function is used with map and acl management. It permits to browse
309 * each reference.
310 */
311static inline
312struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
313{
314 struct pattern_expr *expr;
315 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
316 if (&expr->list == end)
317 return NULL;
318 return expr;
319}
320
321static int cli_io_handler_pat_list(struct appctx *appctx)
322{
323 struct stream_interface *si = appctx->owner;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200324 struct pat_ref_elt *elt;
325
326 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
327 /* If we're forced to shut down, we might have to remove our
328 * reference to the last ref_elt being dumped.
329 */
330 if (appctx->st2 == STAT_ST_LIST) {
Dragan Dosen336a11f2018-05-04 16:27:15 +0200331 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users)) {
332 LIST_DEL(&appctx->ctx.map.bref.users);
333 LIST_INIT(&appctx->ctx.map.bref.users);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200334 }
335 }
336 return 1;
337 }
William Lallemandad8be612016-11-18 19:26:17 +0100338
339 switch (appctx->st2) {
340
341 case STAT_ST_INIT:
Emeric Brun8d85aa42017-06-29 15:40:33 +0200342 /* the function had not been called yet, let's prepare the
343 * buffer for a response. We initialize the current stream
344 * pointer to the first in the global list. When a target
345 * stream is being destroyed, it is responsible for updating
346 * this pointer. We know we have reached the end when this
347 * pointer points back to the head of the streams list.
348 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100349 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200350 LIST_INIT(&appctx->ctx.map.bref.users);
351 appctx->ctx.map.bref.ref = appctx->ctx.map.ref->head.n;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100352 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100353 appctx->st2 = STAT_ST_LIST;
354 /* fall through */
355
356 case STAT_ST_LIST:
Emeric Brunb5997f72017-07-03 11:34:05 +0200357
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100358 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200359
Emeric Brun8d85aa42017-06-29 15:40:33 +0200360 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users)) {
361 LIST_DEL(&appctx->ctx.map.bref.users);
362 LIST_INIT(&appctx->ctx.map.bref.users);
363 }
364
365 while (appctx->ctx.map.bref.ref != &appctx->ctx.map.ref->head) {
William Lallemandad8be612016-11-18 19:26:17 +0100366 chunk_reset(&trash);
367
Emeric Brun8d85aa42017-06-29 15:40:33 +0200368 elt = LIST_ELEM(appctx->ctx.map.bref.ref, struct pat_ref_elt *, list);
369
William Lallemandad8be612016-11-18 19:26:17 +0100370 /* build messages */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200371 if (elt->sample)
William Lallemandad8be612016-11-18 19:26:17 +0100372 chunk_appendf(&trash, "%p %s %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200373 elt, elt->pattern,
374 elt->sample);
William Lallemandad8be612016-11-18 19:26:17 +0100375 else
376 chunk_appendf(&trash, "%p %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200377 elt, elt->pattern);
William Lallemandad8be612016-11-18 19:26:17 +0100378
Willy Tarreau06d80a92017-10-19 14:32:15 +0200379 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100380 /* let's try again later from this stream. We add ourselves into
381 * this stream's users so that it can remove us upon termination.
382 */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200383 LIST_ADDQ(&elt->back_refs, &appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100384 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +0100385 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100386 return 0;
387 }
388
389 /* get next list entry and check the end of the list */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200390 appctx->ctx.map.bref.ref = elt->list.n;
William Lallemandad8be612016-11-18 19:26:17 +0100391 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100392 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100393 /* fall through */
394
395 default:
396 appctx->st2 = STAT_ST_FIN;
397 return 1;
398 }
399}
400
401static int cli_io_handler_pats_list(struct appctx *appctx)
402{
403 struct stream_interface *si = appctx->owner;
404
405 switch (appctx->st2) {
406 case STAT_ST_INIT:
407 /* Display the column headers. If the message cannot be sent,
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800408 * quit the function with returning 0. The function is called
409 * later and restarted at the state "STAT_ST_INIT".
William Lallemandad8be612016-11-18 19:26:17 +0100410 */
411 chunk_reset(&trash);
412 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +0200413 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +0100414 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100415 return 0;
416 }
417
418 /* Now, we start the browsing of the references lists.
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800419 * Note that the following call to LIST_ELEM returns a bad pointer. The only
William Lallemandad8be612016-11-18 19:26:17 +0100420 * available field of this pointer is <list>. It is used with the function
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800421 * pat_list_get_next() for returning the first available entry
William Lallemandad8be612016-11-18 19:26:17 +0100422 */
423 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
424 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
425 appctx->ctx.map.display_flags);
426 appctx->st2 = STAT_ST_LIST;
427 /* fall through */
428
429 case STAT_ST_LIST:
430 while (appctx->ctx.map.ref) {
431 chunk_reset(&trash);
432
433 /* Build messages. If the reference is used by another category than
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800434 * the listed categories, display the information in the message.
William Lallemandad8be612016-11-18 19:26:17 +0100435 */
436 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
437 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
438 appctx->ctx.map.ref->display);
439
Willy Tarreau06d80a92017-10-19 14:32:15 +0200440 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100441 /* let's try again later from this stream. We add ourselves into
442 * this stream's users so that it can remove us upon termination.
443 */
Willy Tarreaudb398432018-11-15 11:08:52 +0100444 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100445 return 0;
446 }
447
448 /* get next list entry and check the end of the list */
449 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
450 appctx->ctx.map.display_flags);
451 }
452
William Lallemandad8be612016-11-18 19:26:17 +0100453 /* fall through */
454
455 default:
456 appctx->st2 = STAT_ST_FIN;
457 return 1;
458 }
459 return 0;
460}
461
462static int cli_io_handler_map_lookup(struct appctx *appctx)
463{
464 struct stream_interface *si = appctx->owner;
465 struct sample sample;
466 struct pattern *pat;
467 int match_method;
468
469 switch (appctx->st2) {
470 case STAT_ST_INIT:
471 /* Init to the first entry. The list cannot be change */
472 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
473 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
474 appctx->st2 = STAT_ST_LIST;
475 /* fall through */
476
477 case STAT_ST_LIST:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100478 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100479 /* for each lookup type */
480 while (appctx->ctx.map.expr) {
481 /* initialise chunk to build new message */
482 chunk_reset(&trash);
483
484 /* execute pattern matching */
485 sample.data.type = SMP_T_STR;
486 sample.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200487 sample.data.u.str.data = appctx->ctx.map.chunk.data;
488 sample.data.u.str.area = appctx->ctx.map.chunk.area;
Emeric Brunb5997f72017-07-03 11:34:05 +0200489
William Lallemandad8be612016-11-18 19:26:17 +0100490 if (appctx->ctx.map.expr->pat_head->match &&
491 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
492 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
493 else
494 pat = NULL;
495
496 /* build return message: set type of match */
497 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
498 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
499 break;
500 if (match_method >= PAT_MATCH_NUM)
501 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
502 else
503 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
504
505 /* case sensitive */
506 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
507 chunk_appendf(&trash, ", case=insensitive");
508 else
509 chunk_appendf(&trash, ", case=sensitive");
510
511 /* Display no match, and set default value */
512 if (!pat) {
513 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
514 chunk_appendf(&trash, ", found=no");
515 else
516 chunk_appendf(&trash, ", match=no");
517 }
518
519 /* Display match and match info */
520 else {
521 /* display match */
522 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
523 chunk_appendf(&trash, ", found=yes");
524 else
525 chunk_appendf(&trash, ", match=yes");
526
527 /* display index mode */
528 if (pat->sflags & PAT_SF_TREE)
529 chunk_appendf(&trash, ", idx=tree");
530 else
531 chunk_appendf(&trash, ", idx=list");
532
533 /* display pattern */
534 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
535 if (pat->ref && pat->ref->pattern)
536 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
537 else
538 chunk_appendf(&trash, ", key=unknown");
539 }
540 else {
541 if (pat->ref && pat->ref->pattern)
542 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
543 else
544 chunk_appendf(&trash, ", pattern=unknown");
545 }
546
547 /* display return value */
548 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
549 if (pat->data && pat->ref && pat->ref->sample)
550 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
551 smp_to_type[pat->data->type]);
552 else
553 chunk_appendf(&trash, ", value=none");
554 }
555 }
556
557 chunk_appendf(&trash, "\n");
558
559 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200560 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100561 /* let's try again later from this stream. We add ourselves into
562 * this stream's users so that it can remove us upon termination.
563 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100564 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +0100565 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100566 return 0;
567 }
568
569 /* get next entry */
570 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
571 &appctx->ctx.map.ref->pat);
572 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100573 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100574 /* fall through */
575
576 default:
577 appctx->st2 = STAT_ST_FIN;
William Lallemandad8be612016-11-18 19:26:17 +0100578 return 1;
579 }
580}
581
582static void cli_release_mlook(struct appctx *appctx)
583{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200584 free(appctx->ctx.map.chunk.area);
585 appctx->ctx.map.chunk.area = NULL;
William Lallemandad8be612016-11-18 19:26:17 +0100586}
587
588
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200589static int cli_parse_get_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100590{
591 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
592 /* Set flags. */
593 if (args[1][0] == 'm')
594 appctx->ctx.map.display_flags = PAT_REF_MAP;
595 else
596 appctx->ctx.map.display_flags = PAT_REF_ACL;
597
598 /* No parameter. */
599 if (!*args[2] || !*args[3]) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200600 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
601 return cli_err(appctx, "Missing map identifier and/or key.\n");
602 else
603 return cli_err(appctx, "Missing ACL identifier and/or key.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100604 }
605
606 /* lookup into the maps */
607 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
608 if (!appctx->ctx.map.ref) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200609 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
610 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
611 else
612 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100613 }
614
615 /* copy input string. The string must be allocated because
616 * it may be used over multiple iterations. It's released
617 * at the end and upon abort anyway.
618 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200619 appctx->ctx.map.chunk.data = strlen(args[3]);
620 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.data + 1;
621 appctx->ctx.map.chunk.area = strdup(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200622 if (!appctx->ctx.map.chunk.area)
623 return cli_err(appctx, "Out of memory error.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100624
625 return 0;
626 }
627 return 1;
628}
629
Emeric Brun8d85aa42017-06-29 15:40:33 +0200630static void cli_release_show_map(struct appctx *appctx)
631{
632 if (appctx->st2 == STAT_ST_LIST) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100633 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200634 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users))
635 LIST_DEL(&appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100636 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200637 }
638}
639
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200640static int cli_parse_show_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100641{
642 if (strcmp(args[1], "map") == 0 ||
643 strcmp(args[1], "acl") == 0) {
644
645 /* Set ACL or MAP flags. */
646 if (args[1][0] == 'm')
647 appctx->ctx.map.display_flags = PAT_REF_MAP;
648 else
649 appctx->ctx.map.display_flags = PAT_REF_ACL;
650
651 /* no parameter: display all map available */
652 if (!*args[2]) {
William Lallemandad8be612016-11-18 19:26:17 +0100653 appctx->io_handler = cli_io_handler_pats_list;
654 return 0;
655 }
656
657 /* lookup into the refs and check the map flag */
658 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
659 if (!appctx->ctx.map.ref ||
660 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200661 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
662 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
663 else
664 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100665 }
William Lallemandad8be612016-11-18 19:26:17 +0100666 appctx->io_handler = cli_io_handler_pat_list;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200667 appctx->io_release = cli_release_show_map;
William Lallemandad8be612016-11-18 19:26:17 +0100668 return 0;
669 }
670
671 return 0;
672}
673
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200674static int cli_parse_set_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100675{
676 if (strcmp(args[1], "map") == 0) {
677 char *err;
678
679 /* Set flags. */
680 appctx->ctx.map.display_flags = PAT_REF_MAP;
681
682 /* Expect three parameters: map name, key and new value. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200683 if (!*args[2] || !*args[3] || !*args[4])
684 return cli_err(appctx, "'set map' expects three parameters: map identifier, key and value.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100685
686 /* Lookup the reference in the maps. */
687 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200688 if (!appctx->ctx.map.ref)
689 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100690
691 /* If the entry identifier start with a '#', it is considered as
692 * pointer id
693 */
694 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
695 struct pat_ref_elt *ref;
696 long long int conv;
697 char *error;
698
699 /* Convert argument to integer value. */
700 conv = strtoll(&args[3][1], &error, 16);
Willy Tarreau9d008692019-08-09 11:21:01 +0200701 if (*error != '\0')
702 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100703
704 /* Convert and check integer to pointer. */
705 ref = (struct pat_ref_elt *)(long)conv;
Willy Tarreau9d008692019-08-09 11:21:01 +0200706 if ((long long int)(long)ref != conv)
707 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100708
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200709 /* Try to modify the entry. */
William Lallemandad8be612016-11-18 19:26:17 +0100710 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100711 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100712 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100713 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +0200714 if (err)
715 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
716 else
717 return cli_err(appctx, "Failed to update an entry.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100718 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100719 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100720 }
721 else {
722 /* Else, use the entry identifier as pattern
723 * string, and update the value.
724 */
725 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100726 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100727 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100728 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +0200729 if (err)
730 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
731 else
732 return cli_err(appctx, "Failed to update an entry.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100733 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100734 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100735 }
736
737 /* The set is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100738 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100739 return 0;
740 }
741 return 1;
742}
743
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200744static int map_add_key_value(struct appctx *appctx, const char *key, const char *value, char **err)
745{
746 int ret;
747
748 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
749 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
750 ret = pat_ref_add(appctx->ctx.map.ref, key, value, err);
751 else
752 ret = pat_ref_add(appctx->ctx.map.ref, key, NULL, err);
753 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
754
755 return ret;
756}
757
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200758static int cli_parse_add_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100759{
760 if (strcmp(args[1], "map") == 0 ||
761 strcmp(args[1], "acl") == 0) {
762 int ret;
763 char *err;
764
765 /* Set flags. */
766 if (args[1][0] == 'm')
767 appctx->ctx.map.display_flags = PAT_REF_MAP;
768 else
769 appctx->ctx.map.display_flags = PAT_REF_ACL;
770
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200771 /* If the keyword is "map", we expect:
772 * - three parameters if there is no payload
773 * - one parameter if there is a payload
774 * If it is "acl", we expect only two parameters
William Lallemandad8be612016-11-18 19:26:17 +0100775 */
776 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200777 if ((!payload && (!*args[2] || !*args[3] || !*args[4])) ||
Willy Tarreau9d008692019-08-09 11:21:01 +0200778 (payload && !*args[2]))
779 return cli_err(appctx,
780 "'add map' expects three parameters (map identifier, key and value)"
781 " or one parameter (map identifier) and a payload\n");
William Lallemandad8be612016-11-18 19:26:17 +0100782 }
Willy Tarreau9d008692019-08-09 11:21:01 +0200783 else if (!*args[2] || !*args[3])
784 return cli_err(appctx, "'add acl' expects two parameters: ACL identifier and pattern.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100785
786 /* Lookup for the reference. */
787 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
788 if (!appctx->ctx.map.ref) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200789 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
790 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
791 else
792 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100793 }
794
795 /* The command "add acl" is prohibited if the reference
796 * use samples.
797 */
798 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
799 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200800 return cli_err(appctx,
801 "This ACL is shared with a map containing samples. "
802 "You must use the command 'add map' to add values.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100803 }
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200804 /* Add value(s). */
William Lallemandad8be612016-11-18 19:26:17 +0100805 err = NULL;
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200806 if (!payload) {
807 ret = map_add_key_value(appctx, args[3], args[4], &err);
808 if (!ret) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200809 if (err)
810 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
811 else
812 return cli_err(appctx, "Failed to add an entry.\n");
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200813 }
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200814 }
815 else {
816 const char *end = payload + strlen(payload);
817
818 while (payload < end) {
819 char *key, *value;
820 size_t l;
821
822 /* key */
823 key = payload;
824 l = strcspn(key, " \t");
825 payload += l;
826
Willy Tarreau9d008692019-08-09 11:21:01 +0200827 if (!*payload && appctx->ctx.map.display_flags == PAT_REF_MAP)
828 return cli_dynerr(appctx, memprintf(&err, "Missing value for key '%s'.\n", key));
829
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200830 key[l] = 0;
831 payload++;
832
833 /* value */
834 payload += strspn(payload, " \t");
835 value = payload;
836 l = strcspn(value, "\n");
837 payload += l;
838 if (*payload)
839 payload++;
840 value[l] = 0;
841
842 ret = map_add_key_value(appctx, key, value, &err);
843 if (!ret) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200844 if (err)
845 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
846 else
847 return cli_err(appctx, "Failed to add a key.\n");
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200848 }
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200849 }
William Lallemandad8be612016-11-18 19:26:17 +0100850 }
851
852 /* The add is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100853 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100854 return 1;
855 }
856
857 return 0;
858}
859
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200860static int cli_parse_del_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100861{
862 if (args[1][0] == 'm')
863 appctx->ctx.map.display_flags = PAT_REF_MAP;
864 else
865 appctx->ctx.map.display_flags = PAT_REF_ACL;
866
867 /* Expect two parameters: map name and key. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200868 if (!*args[2] || !*args[3]) {
869 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
870 return cli_err(appctx, "This command expects two parameters: map identifier and key.\n");
871 else
872 return cli_err(appctx, "This command expects two parameters: ACL identifier and key.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100873 }
874
875 /* Lookup the reference in the maps. */
876 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
877 if (!appctx->ctx.map.ref ||
Willy Tarreau9d008692019-08-09 11:21:01 +0200878 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags))
879 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100880
881 /* If the entry identifier start with a '#', it is considered as
882 * pointer id
883 */
884 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
885 struct pat_ref_elt *ref;
886 long long int conv;
887 char *error;
888
889 /* Convert argument to integer value. */
890 conv = strtoll(&args[3][1], &error, 16);
Willy Tarreau9d008692019-08-09 11:21:01 +0200891 if (*error != '\0')
892 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100893
894 /* Convert and check integer to pointer. */
895 ref = (struct pat_ref_elt *)(long)conv;
Willy Tarreau9d008692019-08-09 11:21:01 +0200896 if ((long long int)(long)ref != conv)
897 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100898
899 /* Try to delete the entry. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100900 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100901 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100902 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100903 /* The entry is not found, send message. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200904 return cli_err(appctx, "Key not found.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100905 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100906 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100907 }
908 else {
909 /* Else, use the entry identifier as pattern
910 * string and try to delete the entry.
911 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100912 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100913 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100914 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100915 /* The entry is not found, send message. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200916 return cli_err(appctx, "Key not found.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100917 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100918 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100919 }
920
921 /* The deletion is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100922 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100923 return 1;
924}
925
926
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200927static int cli_parse_clear_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100928{
929 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
930 /* Set ACL or MAP flags. */
931 if (args[1][0] == 'm')
932 appctx->ctx.map.display_flags = PAT_REF_MAP;
933 else
934 appctx->ctx.map.display_flags = PAT_REF_ACL;
935
936 /* no parameter */
937 if (!*args[2]) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200938 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
939 return cli_err(appctx, "Missing map identifier.\n");
940 else
941 return cli_err(appctx, "Missing ACL identifier.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100942 }
943
944 /* lookup into the refs and check the map flag */
945 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
946 if (!appctx->ctx.map.ref ||
947 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200948 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
949 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
950 else
951 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100952 }
953
954 /* Clear all. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100955 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100956 pat_ref_prune(appctx->ctx.map.ref);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100957 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100958
959 /* return response */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100960 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100961 return 1;
962 }
963 return 0;
964}
965
966/* register cli keywords */
967
968static struct cli_kw_list cli_kws = {{ },{
969 { { "add", "acl", NULL }, "add acl : add acl entry", cli_parse_add_map, NULL },
970 { { "clear", "acl", NULL }, "clear acl <id> : clear the content of this acl", cli_parse_clear_map, NULL },
971 { { "del", "acl", NULL }, "del acl : delete acl entry", cli_parse_del_map, NULL },
972 { { "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 },
973 { { "show", "acl", NULL }, "show acl [id] : report available acls or dump an acl's contents", cli_parse_show_map, NULL },
974 { { "add", "map", NULL }, "add map : add map entry", cli_parse_add_map, NULL },
975 { { "clear", "map", NULL }, "clear map <id> : clear the content of this map", cli_parse_clear_map, NULL },
976 { { "del", "map", NULL }, "del map : delete map entry", cli_parse_del_map, NULL },
Nenad Merdanovic96c15712017-03-12 22:01:36 +0100977 { { "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 +0100978 { { "set", "map", NULL }, "set map : modify map entry", cli_parse_set_map, NULL },
979 { { "show", "map", NULL }, "show map [id] : report available maps or dump a map's contents", cli_parse_show_map, NULL },
980 { { NULL }, NULL, NULL, NULL }
981}};
982
Willy Tarreau0108d902018-11-25 19:14:37 +0100983INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemandad8be612016-11-18 19:26:17 +0100984
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100985/* Note: must not be declared <const> as its list will be overwritten
986 *
987 * For the map_*_int keywords, the output is declared as SMP_T_UINT, but the converter function
988 * can provide SMP_T_UINT, SMP_T_SINT or SMP_T_BOOL depending on how the patterns found in the
989 * file can be parsed.
990 *
991 * For the map_*_ip keyword, the output is declared as SMP_T_IPV4, but the converter function
992 * can provide SMP_T_IPV4 or SMP_T_IPV6 depending on the patterns found in the file.
993 *
994 * The map_* keywords only emit strings.
995 *
996 * The output type is only used during the configuration parsing. It is used for detecting
997 * compatibility problems.
998 *
999 * The arguments are: <file>[,<default value>]
1000 */
1001static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER1edc9712014-12-15 16:18:39 +01001002 { "map", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1003 { "map_str", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1004 { "map_beg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_BEG },
1005 { "map_sub", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_SUB },
1006 { "map_dir", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DIR },
1007 { "map_dom", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DOM },
1008 { "map_end", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_END },
1009 { "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 +01001010 { "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 +02001011 { "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 +01001012 { "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 +01001013
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001014 { "map_str_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_STR },
1015 { "map_beg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_BEG },
1016 { "map_sub_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_SUB },
1017 { "map_dir_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DIR },
1018 { "map_dom_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DOM },
1019 { "map_end_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_END },
1020 { "map_reg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_REG },
1021 { "map_int_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_SINT, SMP_T_SINT, (void *)PAT_MATCH_INT },
1022 { "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 +01001023
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +02001024 { "map_str_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_STR },
1025 { "map_beg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_BEG },
1026 { "map_sub_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_SUB },
1027 { "map_dir_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DIR },
1028 { "map_dom_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DOM },
1029 { "map_end_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_END },
1030 { "map_reg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_REG },
1031 { "map_int_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_SINT, SMP_T_ADDR, (void *)PAT_MATCH_INT },
1032 { "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 +01001033
1034 { /* END */ },
1035}};
1036
Willy Tarreau0108d902018-11-25 19:14:37 +01001037INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);