blob: 1a2190d9ac5880050798eebf7a0cb955ab341ab8 [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
13#include <limits.h>
14#include <stdio.h>
15
Willy Tarreau0108d902018-11-25 19:14:37 +010016#include <common/initcall.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010017#include <common/standard.h>
18
William Lallemandad8be612016-11-18 19:26:17 +010019#include <types/applet.h>
20#include <types/cli.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010021#include <types/global.h>
22#include <types/map.h>
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010023#include <types/pattern.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>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010027#include <proto/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>
Thierry FOURNIERb0c0a0f2013-12-10 15:05:34 +010030#include <proto/map.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010031#include <proto/pattern.h>
William Lallemandad8be612016-11-18 19:26:17 +010032#include <proto/stream_interface.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010033#include <proto/sample.h>
34
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020035/* Parse an IPv4 or IPv6 address and store it into the sample.
36 * The output type is IPv4 or IPv6.
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010037 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020038int map_parse_ip(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010039{
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020040 int len = strlen(text);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010041
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020042 if (buf2ip(text, len, &data->u.ipv4)) {
43 data->type = SMP_T_IPV4;
44 return 1;
45 }
46 if (buf2ip6(text, len, &data->u.ipv6)) {
47 data->type = SMP_T_IPV6;
48 return 1;
49 }
50 return 0;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010051}
52
53/* Parse a string and store a pointer to it into the sample. The original
54 * string must be left in memory because we return a direct memory reference.
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010055 * The output type is SMP_T_STR. There is no risk that the data will be
56 * overwritten because sample_conv_map() makes a const sample with this
57 * output.
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010058 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020059int map_parse_str(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010060{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020061 data->u.str.area = (char *)text;
62 data->u.str.data = strlen(text);
63 data->u.str.size = data->u.str.data + 1;
Thierry FOURNIER503bb092015-08-19 08:35:43 +020064 data->type = SMP_T_STR;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010065 return 1;
66}
67
68/* Parse an integer and convert it to a sample. The output type is SINT if the
69 * number is negative, or UINT if it is positive or null. The function returns
70 * zero (error) if the number is too large.
71 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020072int map_parse_int(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010073{
Thierry FOURNIER503bb092015-08-19 08:35:43 +020074 data->type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020075 data->u.sint = read_int64(&text, text + strlen(text));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020076 if (*text != '\0')
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010077 return 0;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010078 return 1;
79}
80
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010081/* This crete and initialize map descriptor.
82 * Return NULL if out of memory error
83 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010084static struct map_descriptor *map_create_descriptor(struct sample_conv *conv)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010085{
86 struct map_descriptor *desc;
87
88 desc = calloc(1, sizeof(*desc));
89 if (!desc)
90 return NULL;
91
92 desc->conv = conv;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010093
94 return desc;
95}
96
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010097/* This function load the map file according with data type declared into
98 * the "struct sample_conv".
99 *
100 * This function choose the indexation type (ebtree or list) according with
101 * the type of match needed.
102 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200103int sample_load_map(struct arg *arg, struct sample_conv *conv,
104 const char *file, int line, char **err)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100105{
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100106 struct map_descriptor *desc;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100107
108 /* create new map descriptor */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100109 desc = map_create_descriptor(conv);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100110 if (!desc) {
111 memprintf(err, "out of memory");
112 return 0;
113 }
114
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100115 /* Initialize pattern */
116 pattern_init_head(&desc->pat);
117
118 /* This is original pattern, must free */
119 desc->do_free = 1;
120
121 /* Set the match method. */
Thierry FOURNIER1edc9712014-12-15 16:18:39 +0100122 desc->pat.match = pat_match_fcts[(long)conv->private];
123 desc->pat.parse = pat_parse_fcts[(long)conv->private];
124 desc->pat.index = pat_index_fcts[(long)conv->private];
125 desc->pat.delete = pat_delete_fcts[(long)conv->private];
126 desc->pat.prune = pat_prune_fcts[(long)conv->private];
127 desc->pat.expect_type = pat_match_types[(long)conv->private];
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100128
129 /* Set the output parse method. */
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100130 switch (desc->conv->out_type) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100131 case SMP_T_STR: desc->pat.parse_smp = map_parse_str; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200132 case SMP_T_SINT: desc->pat.parse_smp = map_parse_int; break;
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200133 case SMP_T_ADDR: desc->pat.parse_smp = map_parse_ip; break;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100134 default:
135 memprintf(err, "map: internal haproxy error: no default parse case for the input type <%d>.",
136 conv->out_type);
Andreas Seltenreich78f35952016-03-03 20:32:23 +0100137 free(desc);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100138 return 0;
139 }
140
Thierry FOURNIER39bef452014-01-29 13:29:45 +0100141 /* Load map. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200142 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 +0100143 1, err, file, line))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100144 return 0;
Thierry FOURNIER0ffe78c2013-12-05 14:40:25 +0100145
Willy Tarreauaa5801b2019-04-19 11:35:22 +0200146 /* the maps of type IP support a string as default value. This
147 * string can be an ipv4 or an ipv6, we must convert it.
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200148 */
Willy Tarreauaa5801b2019-04-19 11:35:22 +0200149 if (arg[1].type != ARGT_STOP && desc->conv->out_type == SMP_T_ADDR) {
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200150 struct sample_data data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200151 if (!map_parse_ip(arg[1].data.str.area, &data)) {
152 memprintf(err, "map: cannot parse default ip <%s>.",
153 arg[1].data.str.area);
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200154 return 0;
155 }
156 if (data.type == SMP_T_IPV4) {
157 arg[1].type = ARGT_IPV4;
158 arg[1].data.ipv4 = data.u.ipv4;
159 } else {
160 arg[1].type = ARGT_IPV6;
161 arg[1].data.ipv6 = data.u.ipv6;
162 }
163 }
164
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100165 /* replace the first argument by this definition */
166 arg[0].type = ARGT_MAP;
167 arg[0].data.map = desc;
168
169 return 1;
170}
171
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200172static int sample_conv_map(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100173{
174 struct map_descriptor *desc;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100175 struct pattern *pat;
Willy Tarreau83061a82018-07-13 11:56:34 +0200176 struct buffer *str;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100177
178 /* get config */
179 desc = arg_p[0].data.map;
180
181 /* Execute the match function. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100182 pat = pattern_exec_match(&desc->pat, smp, 1);
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100183
184 /* Match case. */
185 if (pat) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200186 if (pat->data) {
Thierry Fournier8feaa662016-02-10 22:55:20 +0100187 /* In the regm case, merge the sample with the input. */
188 if ((long)private == PAT_MATCH_REGM) {
Emeric Brun27102212018-07-17 09:47:07 -0400189 struct buffer *tmptrash;
Willy Tarreau2842e052018-08-22 04:55:43 +0200190 int len;
Emeric Brun27102212018-07-17 09:47:07 -0400191
192 /* Copy the content of the sample because it could
193 be scratched by incoming get_trash_chunk */
194 tmptrash = alloc_trash_chunk();
195 if (!tmptrash)
196 return 0;
197
198 tmptrash->data = smp->data.u.str.data;
199 if (tmptrash->data > (tmptrash->size-1))
200 tmptrash->data = tmptrash->size-1;
201
202 memcpy(tmptrash->area, smp->data.u.str.area, tmptrash->data);
203 tmptrash->area[tmptrash->data] = 0;
204
Thierry Fournier8feaa662016-02-10 22:55:20 +0100205 str = get_trash_chunk();
Willy Tarreau2842e052018-08-22 04:55:43 +0200206 len = exp_replace(str->area, str->size,
207 tmptrash->area,
208 pat->data->u.str.area,
209 (regmatch_t *)smp->ctx.a[0]);
Nenad Merdanovic646b7742019-04-12 22:54:28 +0200210 free_trash_chunk(tmptrash);
211
Willy Tarreau2842e052018-08-22 04:55:43 +0200212 if (len == -1)
Thierry Fournier8feaa662016-02-10 22:55:20 +0100213 return 0;
Willy Tarreau2842e052018-08-22 04:55:43 +0200214
215 str->data = len;
Thierry Fournier8feaa662016-02-10 22:55:20 +0100216 smp->data.u.str = *str;
217 return 1;
218 }
219 /* Copy sample. */
Thierry FOURNIER5cc18d42015-08-19 09:02:36 +0200220 smp->data = *pat->data;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100221 smp->flags |= SMP_F_CONST;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100222 return 1;
223 }
224
225 /* Return just int sample containing 1. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200226 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200227 smp->data.u.sint = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100228 return 1;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100229 }
230
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800231 /* If no default value available, the converter fails. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100232 if (arg_p[1].type == ARGT_STOP)
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100233 return 0;
234
235 /* Return the default value. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100236 switch (desc->conv->out_type) {
237
238 case SMP_T_STR:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200239 smp->data.type = SMP_T_STR;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100240 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200241 smp->data.u.str = arg_p[1].data.str;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100242 break;
243
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200244 case SMP_T_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200245 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200246 smp->data.u.sint = arg_p[1].data.sint;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100247 break;
248
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200249 case SMP_T_ADDR:
250 if (arg_p[1].type == ARGT_IPV4) {
251 smp->data.type = SMP_T_IPV4;
252 smp->data.u.ipv4 = arg_p[1].data.ipv4;
253 } else {
254 smp->data.type = SMP_T_IPV6;
255 smp->data.u.ipv6 = arg_p[1].data.ipv6;
256 }
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100257 break;
258 }
259
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100260 return 1;
261}
262
William Lallemandad8be612016-11-18 19:26:17 +0100263/* This function is used with map and acl management. It permits to browse
264 * each reference. The variable <getnext> must contain the current node,
265 * <end> point to the root node and the <flags> permit to filter required
266 * nodes.
267 */
268static inline
269struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
270 unsigned int flags)
271{
272 struct pat_ref *ref = getnext;
273
274 while (1) {
275
276 /* Get next list entry. */
277 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
278
279 /* If the entry is the last of the list, return NULL. */
280 if (&ref->list == end)
281 return NULL;
282
283 /* If the entry match the flag, return it. */
284 if (ref->flags & flags)
285 return ref;
286 }
287}
288
289static inline
290struct pat_ref *pat_ref_lookup_ref(const char *reference)
291{
292 int id;
293 char *error;
294
295 /* If the reference starts by a '#', this is numeric id. */
296 if (reference[0] == '#') {
297 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
298 id = strtol(reference + 1, &error, 10);
299 if (*error != '\0')
300 return NULL;
301
302 /* Perform the unique id lookup. */
303 return pat_ref_lookupid(id);
304 }
305
306 /* Perform the string lookup. */
307 return pat_ref_lookup(reference);
308}
309
310/* This function is used with map and acl management. It permits to browse
311 * each reference.
312 */
313static inline
314struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
315{
316 struct pattern_expr *expr;
317 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
318 if (&expr->list == end)
319 return NULL;
320 return expr;
321}
322
323static int cli_io_handler_pat_list(struct appctx *appctx)
324{
325 struct stream_interface *si = appctx->owner;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200326 struct pat_ref_elt *elt;
327
328 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
329 /* If we're forced to shut down, we might have to remove our
330 * reference to the last ref_elt being dumped.
331 */
332 if (appctx->st2 == STAT_ST_LIST) {
Dragan Dosen336a11f2018-05-04 16:27:15 +0200333 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users)) {
334 LIST_DEL(&appctx->ctx.map.bref.users);
335 LIST_INIT(&appctx->ctx.map.bref.users);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200336 }
337 }
338 return 1;
339 }
William Lallemandad8be612016-11-18 19:26:17 +0100340
341 switch (appctx->st2) {
342
343 case STAT_ST_INIT:
Emeric Brun8d85aa42017-06-29 15:40:33 +0200344 /* the function had not been called yet, let's prepare the
345 * buffer for a response. We initialize the current stream
346 * pointer to the first in the global list. When a target
347 * stream is being destroyed, it is responsible for updating
348 * this pointer. We know we have reached the end when this
349 * pointer points back to the head of the streams list.
350 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100351 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200352 LIST_INIT(&appctx->ctx.map.bref.users);
353 appctx->ctx.map.bref.ref = appctx->ctx.map.ref->head.n;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100354 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100355 appctx->st2 = STAT_ST_LIST;
356 /* fall through */
357
358 case STAT_ST_LIST:
Emeric Brunb5997f72017-07-03 11:34:05 +0200359
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100360 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200361
Emeric Brun8d85aa42017-06-29 15:40:33 +0200362 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users)) {
363 LIST_DEL(&appctx->ctx.map.bref.users);
364 LIST_INIT(&appctx->ctx.map.bref.users);
365 }
366
367 while (appctx->ctx.map.bref.ref != &appctx->ctx.map.ref->head) {
William Lallemandad8be612016-11-18 19:26:17 +0100368 chunk_reset(&trash);
369
Emeric Brun8d85aa42017-06-29 15:40:33 +0200370 elt = LIST_ELEM(appctx->ctx.map.bref.ref, struct pat_ref_elt *, list);
371
William Lallemandad8be612016-11-18 19:26:17 +0100372 /* build messages */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200373 if (elt->sample)
William Lallemandad8be612016-11-18 19:26:17 +0100374 chunk_appendf(&trash, "%p %s %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200375 elt, elt->pattern,
376 elt->sample);
William Lallemandad8be612016-11-18 19:26:17 +0100377 else
378 chunk_appendf(&trash, "%p %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200379 elt, elt->pattern);
William Lallemandad8be612016-11-18 19:26:17 +0100380
Willy Tarreau06d80a92017-10-19 14:32:15 +0200381 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100382 /* let's try again later from this stream. We add ourselves into
383 * this stream's users so that it can remove us upon termination.
384 */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200385 LIST_ADDQ(&elt->back_refs, &appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100386 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +0100387 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100388 return 0;
389 }
390
391 /* get next list entry and check the end of the list */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200392 appctx->ctx.map.bref.ref = elt->list.n;
William Lallemandad8be612016-11-18 19:26:17 +0100393 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100394 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100395 /* fall through */
396
397 default:
398 appctx->st2 = STAT_ST_FIN;
399 return 1;
400 }
401}
402
403static int cli_io_handler_pats_list(struct appctx *appctx)
404{
405 struct stream_interface *si = appctx->owner;
406
407 switch (appctx->st2) {
408 case STAT_ST_INIT:
409 /* Display the column headers. If the message cannot be sent,
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800410 * quit the function with returning 0. The function is called
411 * later and restarted at the state "STAT_ST_INIT".
William Lallemandad8be612016-11-18 19:26:17 +0100412 */
413 chunk_reset(&trash);
414 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +0200415 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +0100416 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100417 return 0;
418 }
419
420 /* Now, we start the browsing of the references lists.
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800421 * Note that the following call to LIST_ELEM returns a bad pointer. The only
William Lallemandad8be612016-11-18 19:26:17 +0100422 * available field of this pointer is <list>. It is used with the function
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800423 * pat_list_get_next() for returning the first available entry
William Lallemandad8be612016-11-18 19:26:17 +0100424 */
425 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
426 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
427 appctx->ctx.map.display_flags);
428 appctx->st2 = STAT_ST_LIST;
429 /* fall through */
430
431 case STAT_ST_LIST:
432 while (appctx->ctx.map.ref) {
433 chunk_reset(&trash);
434
435 /* Build messages. If the reference is used by another category than
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800436 * the listed categories, display the information in the message.
William Lallemandad8be612016-11-18 19:26:17 +0100437 */
438 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
439 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
440 appctx->ctx.map.ref->display);
441
Willy Tarreau06d80a92017-10-19 14:32:15 +0200442 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100443 /* let's try again later from this stream. We add ourselves into
444 * this stream's users so that it can remove us upon termination.
445 */
Willy Tarreaudb398432018-11-15 11:08:52 +0100446 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100447 return 0;
448 }
449
450 /* get next list entry and check the end of the list */
451 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
452 appctx->ctx.map.display_flags);
453 }
454
William Lallemandad8be612016-11-18 19:26:17 +0100455 /* fall through */
456
457 default:
458 appctx->st2 = STAT_ST_FIN;
459 return 1;
460 }
461 return 0;
462}
463
464static int cli_io_handler_map_lookup(struct appctx *appctx)
465{
466 struct stream_interface *si = appctx->owner;
467 struct sample sample;
468 struct pattern *pat;
469 int match_method;
470
471 switch (appctx->st2) {
472 case STAT_ST_INIT:
473 /* Init to the first entry. The list cannot be change */
474 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
475 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
476 appctx->st2 = STAT_ST_LIST;
477 /* fall through */
478
479 case STAT_ST_LIST:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100480 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100481 /* for each lookup type */
482 while (appctx->ctx.map.expr) {
483 /* initialise chunk to build new message */
484 chunk_reset(&trash);
485
486 /* execute pattern matching */
487 sample.data.type = SMP_T_STR;
488 sample.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200489 sample.data.u.str.data = appctx->ctx.map.chunk.data;
490 sample.data.u.str.area = appctx->ctx.map.chunk.area;
Emeric Brunb5997f72017-07-03 11:34:05 +0200491
William Lallemandad8be612016-11-18 19:26:17 +0100492 if (appctx->ctx.map.expr->pat_head->match &&
493 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
494 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
495 else
496 pat = NULL;
497
498 /* build return message: set type of match */
499 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
500 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
501 break;
502 if (match_method >= PAT_MATCH_NUM)
503 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
504 else
505 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
506
507 /* case sensitive */
508 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
509 chunk_appendf(&trash, ", case=insensitive");
510 else
511 chunk_appendf(&trash, ", case=sensitive");
512
513 /* Display no match, and set default value */
514 if (!pat) {
515 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
516 chunk_appendf(&trash, ", found=no");
517 else
518 chunk_appendf(&trash, ", match=no");
519 }
520
521 /* Display match and match info */
522 else {
523 /* display match */
524 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
525 chunk_appendf(&trash, ", found=yes");
526 else
527 chunk_appendf(&trash, ", match=yes");
528
529 /* display index mode */
530 if (pat->sflags & PAT_SF_TREE)
531 chunk_appendf(&trash, ", idx=tree");
532 else
533 chunk_appendf(&trash, ", idx=list");
534
535 /* display pattern */
536 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
537 if (pat->ref && pat->ref->pattern)
538 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
539 else
540 chunk_appendf(&trash, ", key=unknown");
541 }
542 else {
543 if (pat->ref && pat->ref->pattern)
544 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
545 else
546 chunk_appendf(&trash, ", pattern=unknown");
547 }
548
549 /* display return value */
550 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
551 if (pat->data && pat->ref && pat->ref->sample)
552 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
553 smp_to_type[pat->data->type]);
554 else
555 chunk_appendf(&trash, ", value=none");
556 }
557 }
558
559 chunk_appendf(&trash, "\n");
560
561 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200562 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100563 /* let's try again later from this stream. We add ourselves into
564 * this stream's users so that it can remove us upon termination.
565 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100566 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +0100567 si_rx_room_blk(si);
William Lallemandad8be612016-11-18 19:26:17 +0100568 return 0;
569 }
570
571 /* get next entry */
572 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
573 &appctx->ctx.map.ref->pat);
574 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100575 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100576 /* fall through */
577
578 default:
579 appctx->st2 = STAT_ST_FIN;
William Lallemandad8be612016-11-18 19:26:17 +0100580 return 1;
581 }
582}
583
584static void cli_release_mlook(struct appctx *appctx)
585{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200586 free(appctx->ctx.map.chunk.area);
587 appctx->ctx.map.chunk.area = NULL;
William Lallemandad8be612016-11-18 19:26:17 +0100588}
589
590
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200591static int cli_parse_get_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100592{
593 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
594 /* Set flags. */
595 if (args[1][0] == 'm')
596 appctx->ctx.map.display_flags = PAT_REF_MAP;
597 else
598 appctx->ctx.map.display_flags = PAT_REF_ACL;
599
600 /* No parameter. */
601 if (!*args[2] || !*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200602 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
603 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100604 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200605 }
606 else {
607 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100608 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200609 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100610 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100611 return 1;
612 }
613
614 /* lookup into the maps */
615 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
616 if (!appctx->ctx.map.ref) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200617 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
618 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100619 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200620 }
621 else {
622 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100623 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200624 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100625 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100626 return 1;
627 }
628
629 /* copy input string. The string must be allocated because
630 * it may be used over multiple iterations. It's released
631 * at the end and upon abort anyway.
632 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200633 appctx->ctx.map.chunk.data = strlen(args[3]);
634 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.data + 1;
635 appctx->ctx.map.chunk.area = strdup(args[3]);
636 if (!appctx->ctx.map.chunk.area) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200637 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100638 appctx->ctx.cli.msg = "Out of memory error.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100639 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100640 return 1;
641 }
642
643 return 0;
644 }
645 return 1;
646}
647
Emeric Brun8d85aa42017-06-29 15:40:33 +0200648static void cli_release_show_map(struct appctx *appctx)
649{
650 if (appctx->st2 == STAT_ST_LIST) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100651 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200652 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users))
653 LIST_DEL(&appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100654 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200655 }
656}
657
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200658static int cli_parse_show_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100659{
660 if (strcmp(args[1], "map") == 0 ||
661 strcmp(args[1], "acl") == 0) {
662
663 /* Set ACL or MAP flags. */
664 if (args[1][0] == 'm')
665 appctx->ctx.map.display_flags = PAT_REF_MAP;
666 else
667 appctx->ctx.map.display_flags = PAT_REF_ACL;
668
669 /* no parameter: display all map available */
670 if (!*args[2]) {
William Lallemandad8be612016-11-18 19:26:17 +0100671 appctx->io_handler = cli_io_handler_pats_list;
672 return 0;
673 }
674
675 /* lookup into the refs and check the map flag */
676 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
677 if (!appctx->ctx.map.ref ||
678 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200679 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
680 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100681 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200682 }
683 else {
684 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100685 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200686 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100687 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100688 return 1;
689 }
William Lallemandad8be612016-11-18 19:26:17 +0100690 appctx->io_handler = cli_io_handler_pat_list;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200691 appctx->io_release = cli_release_show_map;
William Lallemandad8be612016-11-18 19:26:17 +0100692 return 0;
693 }
694
695 return 0;
696}
697
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200698static int cli_parse_set_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100699{
700 if (strcmp(args[1], "map") == 0) {
701 char *err;
702
703 /* Set flags. */
704 appctx->ctx.map.display_flags = PAT_REF_MAP;
705
706 /* Expect three parameters: map name, key and new value. */
707 if (!*args[2] || !*args[3] || !*args[4]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200708 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100709 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100710 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100711 return 1;
712 }
713
714 /* Lookup the reference in the maps. */
715 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
716 if (!appctx->ctx.map.ref) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200717 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100718 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100719 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100720 return 1;
721 }
722
723 /* If the entry identifier start with a '#', it is considered as
724 * pointer id
725 */
726 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
727 struct pat_ref_elt *ref;
728 long long int conv;
729 char *error;
730
731 /* Convert argument to integer value. */
732 conv = strtoll(&args[3][1], &error, 16);
733 if (*error != '\0') {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200734 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100735 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100736 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100737 return 1;
738 }
739
740 /* Convert and check integer to pointer. */
741 ref = (struct pat_ref_elt *)(long)conv;
742 if ((long long int)(long)ref != conv) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200743 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100744 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100745 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100746 return 1;
747 }
748
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200749 /* Try to modify the entry. */
William Lallemandad8be612016-11-18 19:26:17 +0100750 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100751 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100752 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100753 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200754 if (err) {
William Lallemandad8be612016-11-18 19:26:17 +0100755 memprintf(&err, "%s.\n", err);
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200756 appctx->ctx.cli.err = err;
757 appctx->st0 = CLI_ST_PRINT_FREE;
758 }
759 else {
760 appctx->ctx.cli.severity = LOG_ERR;
761 appctx->ctx.cli.msg = "Failed to update an entry.\n";
762 appctx->st0 = CLI_ST_PRINT;
763 }
William Lallemandad8be612016-11-18 19:26:17 +0100764 return 1;
765 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100766 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100767 }
768 else {
769 /* Else, use the entry identifier as pattern
770 * string, and update the value.
771 */
772 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100773 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100774 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100775 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200776 if (err) {
William Lallemandad8be612016-11-18 19:26:17 +0100777 memprintf(&err, "%s.\n", err);
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200778 appctx->ctx.cli.err = err;
779 appctx->st0 = CLI_ST_PRINT_FREE;
780 }
781 else {
782 appctx->ctx.cli.severity = LOG_ERR;
783 appctx->ctx.cli.msg = "Failed to update an entry.\n";
784 appctx->st0 = CLI_ST_PRINT;
785 }
William Lallemandad8be612016-11-18 19:26:17 +0100786 return 1;
787 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100788 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100789 }
790
791 /* The set is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100792 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100793 return 0;
794 }
795 return 1;
796}
797
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200798static int map_add_key_value(struct appctx *appctx, const char *key, const char *value, char **err)
799{
800 int ret;
801
802 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
803 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
804 ret = pat_ref_add(appctx->ctx.map.ref, key, value, err);
805 else
806 ret = pat_ref_add(appctx->ctx.map.ref, key, NULL, err);
807 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
808
809 return ret;
810}
811
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200812static int cli_parse_add_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100813{
814 if (strcmp(args[1], "map") == 0 ||
815 strcmp(args[1], "acl") == 0) {
816 int ret;
817 char *err;
818
819 /* Set flags. */
820 if (args[1][0] == 'm')
821 appctx->ctx.map.display_flags = PAT_REF_MAP;
822 else
823 appctx->ctx.map.display_flags = PAT_REF_ACL;
824
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200825 /* If the keyword is "map", we expect:
826 * - three parameters if there is no payload
827 * - one parameter if there is a payload
828 * If it is "acl", we expect only two parameters
William Lallemandad8be612016-11-18 19:26:17 +0100829 */
830 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200831 if ((!payload && (!*args[2] || !*args[3] || !*args[4])) ||
832 (payload && !*args[2])) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200833 appctx->ctx.cli.severity = LOG_ERR;
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200834 appctx->ctx.cli.msg = "'add map' expects three parameters (map identifier, key and value) or one parameter (map identifier) and a payload\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100835 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100836 return 1;
837 }
838 }
839 else {
840 if (!*args[2] || !*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200841 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100842 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100843 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100844 return 1;
845 }
846 }
847
848 /* Lookup for the reference. */
849 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
850 if (!appctx->ctx.map.ref) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200851 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
852 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100853 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200854 }
855 else {
856 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100857 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200858 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100859 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100860 return 1;
861 }
862
863 /* The command "add acl" is prohibited if the reference
864 * use samples.
865 */
866 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
867 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200868 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100869 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
870 "You must use the command 'add map' to add values.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100871 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100872 return 1;
873 }
874
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200875 /* Add value(s). */
William Lallemandad8be612016-11-18 19:26:17 +0100876 err = NULL;
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200877 if (!payload) {
878 ret = map_add_key_value(appctx, args[3], args[4], &err);
879 if (!ret) {
880 if (err) {
881 memprintf(&err, "%s.\n", err);
882 appctx->ctx.cli.err = err;
883 appctx->st0 = CLI_ST_PRINT_FREE;
884 }
885 else {
886 appctx->ctx.cli.severity = LOG_ERR;
887 appctx->ctx.cli.msg = "Failed to add an entry.\n";
888 appctx->st0 = CLI_ST_PRINT;
889 }
890 return 1;
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200891 }
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200892 }
893 else {
894 const char *end = payload + strlen(payload);
895
896 while (payload < end) {
897 char *key, *value;
898 size_t l;
899
900 /* key */
901 key = payload;
902 l = strcspn(key, " \t");
903 payload += l;
904
905 if (!*payload && appctx->ctx.map.display_flags == PAT_REF_MAP) {
906 memprintf(&err, "Missing value for key '%s'.\n", key);
907 appctx->ctx.cli.err = err;
908 appctx->st0 = CLI_ST_PRINT_FREE;
909 return 1;
910 }
911 key[l] = 0;
912 payload++;
913
914 /* value */
915 payload += strspn(payload, " \t");
916 value = payload;
917 l = strcspn(value, "\n");
918 payload += l;
919 if (*payload)
920 payload++;
921 value[l] = 0;
922
923 ret = map_add_key_value(appctx, key, value, &err);
924 if (!ret) {
925 if (err) {
926 memprintf(&err, "%s.\n", err);
927 appctx->ctx.cli.err = err;
928 appctx->st0 = CLI_ST_PRINT_FREE;
929 }
930 else {
931 appctx->ctx.cli.severity = LOG_ERR;
932 appctx->ctx.cli.msg = "Failed to add a key.\n";
933 appctx->st0 = CLI_ST_PRINT;
934 }
935 return 1;
936 }
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200937 }
William Lallemandad8be612016-11-18 19:26:17 +0100938 }
939
940 /* The add is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100941 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100942 return 1;
943 }
944
945 return 0;
946}
947
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200948static int cli_parse_del_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100949{
950 if (args[1][0] == 'm')
951 appctx->ctx.map.display_flags = PAT_REF_MAP;
952 else
953 appctx->ctx.map.display_flags = PAT_REF_ACL;
954
955 /* Expect two parameters: map name and key. */
956 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
957 if (!*args[2] || !*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200958 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100959 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100960 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100961 return 1;
962 }
963 }
964
965 else {
966 if (!*args[2] || !*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200967 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100968 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100969 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100970 return 1;
971 }
972 }
973
974 /* Lookup the reference in the maps. */
975 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
976 if (!appctx->ctx.map.ref ||
977 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200978 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100979 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100980 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100981 return 1;
982 }
983
984 /* If the entry identifier start with a '#', it is considered as
985 * pointer id
986 */
987 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
988 struct pat_ref_elt *ref;
989 long long int conv;
990 char *error;
991
992 /* Convert argument to integer value. */
993 conv = strtoll(&args[3][1], &error, 16);
994 if (*error != '\0') {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200995 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100996 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100997 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100998 return 1;
999 }
1000
1001 /* Convert and check integer to pointer. */
1002 ref = (struct pat_ref_elt *)(long)conv;
1003 if ((long long int)(long)ref != conv) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001004 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001005 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001006 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001007 return 1;
1008 }
1009
1010 /* Try to delete the entry. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001011 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001012 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001013 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001014 /* The entry is not found, send message. */
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001015 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001016 appctx->ctx.cli.msg = "Key not found.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001017 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001018 return 1;
1019 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001020 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001021 }
1022 else {
1023 /* Else, use the entry identifier as pattern
1024 * string and try to delete the entry.
1025 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001026 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001027 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001028 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001029 /* The entry is not found, send message. */
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001030 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001031 appctx->ctx.cli.msg = "Key not found.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001032 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001033 return 1;
1034 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001035 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001036 }
1037
1038 /* The deletion is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001039 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +01001040 return 1;
1041}
1042
1043
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001044static int cli_parse_clear_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +01001045{
1046 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1047 /* Set ACL or MAP flags. */
1048 if (args[1][0] == 'm')
1049 appctx->ctx.map.display_flags = PAT_REF_MAP;
1050 else
1051 appctx->ctx.map.display_flags = PAT_REF_ACL;
1052
1053 /* no parameter */
1054 if (!*args[2]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001055 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
1056 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001057 appctx->ctx.cli.msg = "Missing map identifier.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001058 }
1059 else {
1060 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001061 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001062 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001063 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001064 return 1;
1065 }
1066
1067 /* lookup into the refs and check the map flag */
1068 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
1069 if (!appctx->ctx.map.ref ||
1070 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001071 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
1072 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001073 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001074 }
1075 else {
1076 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001077 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001078 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001079 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001080 return 1;
1081 }
1082
1083 /* Clear all. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001084 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001085 pat_ref_prune(appctx->ctx.map.ref);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001086 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001087
1088 /* return response */
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001089 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +01001090 return 1;
1091 }
1092 return 0;
1093}
1094
1095/* register cli keywords */
1096
1097static struct cli_kw_list cli_kws = {{ },{
1098 { { "add", "acl", NULL }, "add acl : add acl entry", cli_parse_add_map, NULL },
1099 { { "clear", "acl", NULL }, "clear acl <id> : clear the content of this acl", cli_parse_clear_map, NULL },
1100 { { "del", "acl", NULL }, "del acl : delete acl entry", cli_parse_del_map, NULL },
1101 { { "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 },
1102 { { "show", "acl", NULL }, "show acl [id] : report available acls or dump an acl's contents", cli_parse_show_map, NULL },
1103 { { "add", "map", NULL }, "add map : add map entry", cli_parse_add_map, NULL },
1104 { { "clear", "map", NULL }, "clear map <id> : clear the content of this map", cli_parse_clear_map, NULL },
1105 { { "del", "map", NULL }, "del map : delete map entry", cli_parse_del_map, NULL },
Nenad Merdanovic96c15712017-03-12 22:01:36 +01001106 { { "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 +01001107 { { "set", "map", NULL }, "set map : modify map entry", cli_parse_set_map, NULL },
1108 { { "show", "map", NULL }, "show map [id] : report available maps or dump a map's contents", cli_parse_show_map, NULL },
1109 { { NULL }, NULL, NULL, NULL }
1110}};
1111
Willy Tarreau0108d902018-11-25 19:14:37 +01001112INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemandad8be612016-11-18 19:26:17 +01001113
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +01001114/* Note: must not be declared <const> as its list will be overwritten
1115 *
1116 * For the map_*_int keywords, the output is declared as SMP_T_UINT, but the converter function
1117 * can provide SMP_T_UINT, SMP_T_SINT or SMP_T_BOOL depending on how the patterns found in the
1118 * file can be parsed.
1119 *
1120 * For the map_*_ip keyword, the output is declared as SMP_T_IPV4, but the converter function
1121 * can provide SMP_T_IPV4 or SMP_T_IPV6 depending on the patterns found in the file.
1122 *
1123 * The map_* keywords only emit strings.
1124 *
1125 * The output type is only used during the configuration parsing. It is used for detecting
1126 * compatibility problems.
1127 *
1128 * The arguments are: <file>[,<default value>]
1129 */
1130static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER1edc9712014-12-15 16:18:39 +01001131 { "map", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1132 { "map_str", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1133 { "map_beg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_BEG },
1134 { "map_sub", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_SUB },
1135 { "map_dir", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DIR },
1136 { "map_dom", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DOM },
1137 { "map_end", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_END },
1138 { "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 +01001139 { "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 +02001140 { "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 +01001141 { "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 +01001142
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001143 { "map_str_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_STR },
1144 { "map_beg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_BEG },
1145 { "map_sub_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_SUB },
1146 { "map_dir_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DIR },
1147 { "map_dom_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DOM },
1148 { "map_end_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_END },
1149 { "map_reg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_REG },
1150 { "map_int_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_SINT, SMP_T_SINT, (void *)PAT_MATCH_INT },
1151 { "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 +01001152
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +02001153 { "map_str_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_STR },
1154 { "map_beg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_BEG },
1155 { "map_sub_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_SUB },
1156 { "map_dir_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DIR },
1157 { "map_dom_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DOM },
1158 { "map_end_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_END },
1159 { "map_reg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_REG },
1160 { "map_int_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_SINT, SMP_T_ADDR, (void *)PAT_MATCH_INT },
1161 { "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 +01001162
1163 { /* END */ },
1164}};
1165
Willy Tarreau0108d902018-11-25 19:14:37 +01001166INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);