blob: 903c5b267ea5d3a3ae42bbdc42f2c8e2a58dfeb4 [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
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200146 /* the maps of type IP have a string as defaultvalue. This
147 * string canbe anipv4 or an ipv6, we must convert it.
148 */
149 if (desc->conv->out_type == SMP_T_ADDR) {
150 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]);
210 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]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200600 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
601 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100602 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200603 }
604 else {
605 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100606 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200607 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100608 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100609 return 1;
610 }
611
612 /* lookup into the maps */
613 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
614 if (!appctx->ctx.map.ref) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200615 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
616 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100617 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200618 }
619 else {
620 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100621 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200622 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100623 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100624 return 1;
625 }
626
627 /* copy input string. The string must be allocated because
628 * it may be used over multiple iterations. It's released
629 * at the end and upon abort anyway.
630 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200631 appctx->ctx.map.chunk.data = strlen(args[3]);
632 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.data + 1;
633 appctx->ctx.map.chunk.area = strdup(args[3]);
634 if (!appctx->ctx.map.chunk.area) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200635 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100636 appctx->ctx.cli.msg = "Out of memory error.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100637 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100638 return 1;
639 }
640
641 return 0;
642 }
643 return 1;
644}
645
Emeric Brun8d85aa42017-06-29 15:40:33 +0200646static void cli_release_show_map(struct appctx *appctx)
647{
648 if (appctx->st2 == STAT_ST_LIST) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100649 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200650 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users))
651 LIST_DEL(&appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100652 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200653 }
654}
655
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200656static int cli_parse_show_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100657{
658 if (strcmp(args[1], "map") == 0 ||
659 strcmp(args[1], "acl") == 0) {
660
661 /* Set ACL or MAP flags. */
662 if (args[1][0] == 'm')
663 appctx->ctx.map.display_flags = PAT_REF_MAP;
664 else
665 appctx->ctx.map.display_flags = PAT_REF_ACL;
666
667 /* no parameter: display all map available */
668 if (!*args[2]) {
William Lallemandad8be612016-11-18 19:26:17 +0100669 appctx->io_handler = cli_io_handler_pats_list;
670 return 0;
671 }
672
673 /* lookup into the refs and check the map flag */
674 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
675 if (!appctx->ctx.map.ref ||
676 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200677 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
678 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100679 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200680 }
681 else {
682 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100683 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200684 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100685 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100686 return 1;
687 }
William Lallemandad8be612016-11-18 19:26:17 +0100688 appctx->io_handler = cli_io_handler_pat_list;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200689 appctx->io_release = cli_release_show_map;
William Lallemandad8be612016-11-18 19:26:17 +0100690 return 0;
691 }
692
693 return 0;
694}
695
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200696static int cli_parse_set_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100697{
698 if (strcmp(args[1], "map") == 0) {
699 char *err;
700
701 /* Set flags. */
702 appctx->ctx.map.display_flags = PAT_REF_MAP;
703
704 /* Expect three parameters: map name, key and new value. */
705 if (!*args[2] || !*args[3] || !*args[4]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200706 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100707 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100708 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100709 return 1;
710 }
711
712 /* Lookup the reference in the maps. */
713 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
714 if (!appctx->ctx.map.ref) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200715 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100716 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100717 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100718 return 1;
719 }
720
721 /* If the entry identifier start with a '#', it is considered as
722 * pointer id
723 */
724 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
725 struct pat_ref_elt *ref;
726 long long int conv;
727 char *error;
728
729 /* Convert argument to integer value. */
730 conv = strtoll(&args[3][1], &error, 16);
731 if (*error != '\0') {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200732 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100733 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100734 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100735 return 1;
736 }
737
738 /* Convert and check integer to pointer. */
739 ref = (struct pat_ref_elt *)(long)conv;
740 if ((long long int)(long)ref != conv) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200741 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100742 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100743 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100744 return 1;
745 }
746
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200747 /* Try to modify the entry. */
William Lallemandad8be612016-11-18 19:26:17 +0100748 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100749 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100750 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100751 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200752 if (err) {
William Lallemandad8be612016-11-18 19:26:17 +0100753 memprintf(&err, "%s.\n", err);
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200754 appctx->ctx.cli.err = err;
755 appctx->st0 = CLI_ST_PRINT_FREE;
756 }
757 else {
758 appctx->ctx.cli.severity = LOG_ERR;
759 appctx->ctx.cli.msg = "Failed to update an entry.\n";
760 appctx->st0 = CLI_ST_PRINT;
761 }
William Lallemandad8be612016-11-18 19:26:17 +0100762 return 1;
763 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100764 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100765 }
766 else {
767 /* Else, use the entry identifier as pattern
768 * string, and update the value.
769 */
770 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100771 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100772 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100773 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200774 if (err) {
William Lallemandad8be612016-11-18 19:26:17 +0100775 memprintf(&err, "%s.\n", err);
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200776 appctx->ctx.cli.err = err;
777 appctx->st0 = CLI_ST_PRINT_FREE;
778 }
779 else {
780 appctx->ctx.cli.severity = LOG_ERR;
781 appctx->ctx.cli.msg = "Failed to update an entry.\n";
782 appctx->st0 = CLI_ST_PRINT;
783 }
William Lallemandad8be612016-11-18 19:26:17 +0100784 return 1;
785 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100786 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100787 }
788
789 /* The set is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100790 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100791 return 0;
792 }
793 return 1;
794}
795
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200796static int map_add_key_value(struct appctx *appctx, const char *key, const char *value, char **err)
797{
798 int ret;
799
800 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
801 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
802 ret = pat_ref_add(appctx->ctx.map.ref, key, value, err);
803 else
804 ret = pat_ref_add(appctx->ctx.map.ref, key, NULL, err);
805 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
806
807 return ret;
808}
809
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200810static int cli_parse_add_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100811{
812 if (strcmp(args[1], "map") == 0 ||
813 strcmp(args[1], "acl") == 0) {
814 int ret;
815 char *err;
816
817 /* Set flags. */
818 if (args[1][0] == 'm')
819 appctx->ctx.map.display_flags = PAT_REF_MAP;
820 else
821 appctx->ctx.map.display_flags = PAT_REF_ACL;
822
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200823 /* If the keyword is "map", we expect:
824 * - three parameters if there is no payload
825 * - one parameter if there is a payload
826 * If it is "acl", we expect only two parameters
William Lallemandad8be612016-11-18 19:26:17 +0100827 */
828 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200829 if ((!payload && (!*args[2] || !*args[3] || !*args[4])) ||
830 (payload && !*args[2])) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200831 appctx->ctx.cli.severity = LOG_ERR;
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200832 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 +0100833 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100834 return 1;
835 }
836 }
837 else {
838 if (!*args[2] || !*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200839 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100840 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100841 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100842 return 1;
843 }
844 }
845
846 /* Lookup for the reference. */
847 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
848 if (!appctx->ctx.map.ref) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200849 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
850 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100851 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200852 }
853 else {
854 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100855 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200856 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100857 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100858 return 1;
859 }
860
861 /* The command "add acl" is prohibited if the reference
862 * use samples.
863 */
864 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
865 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200866 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100867 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
868 "You must use the command 'add map' to add values.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100869 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100870 return 1;
871 }
872
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200873 /* Add value(s). */
William Lallemandad8be612016-11-18 19:26:17 +0100874 err = NULL;
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200875 if (!payload) {
876 ret = map_add_key_value(appctx, args[3], args[4], &err);
877 if (!ret) {
878 if (err) {
879 memprintf(&err, "%s.\n", err);
880 appctx->ctx.cli.err = err;
881 appctx->st0 = CLI_ST_PRINT_FREE;
882 }
883 else {
884 appctx->ctx.cli.severity = LOG_ERR;
885 appctx->ctx.cli.msg = "Failed to add an entry.\n";
886 appctx->st0 = CLI_ST_PRINT;
887 }
888 return 1;
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200889 }
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200890 }
891 else {
892 const char *end = payload + strlen(payload);
893
894 while (payload < end) {
895 char *key, *value;
896 size_t l;
897
898 /* key */
899 key = payload;
900 l = strcspn(key, " \t");
901 payload += l;
902
903 if (!*payload && appctx->ctx.map.display_flags == PAT_REF_MAP) {
904 memprintf(&err, "Missing value for key '%s'.\n", key);
905 appctx->ctx.cli.err = err;
906 appctx->st0 = CLI_ST_PRINT_FREE;
907 return 1;
908 }
909 key[l] = 0;
910 payload++;
911
912 /* value */
913 payload += strspn(payload, " \t");
914 value = payload;
915 l = strcspn(value, "\n");
916 payload += l;
917 if (*payload)
918 payload++;
919 value[l] = 0;
920
921 ret = map_add_key_value(appctx, key, value, &err);
922 if (!ret) {
923 if (err) {
924 memprintf(&err, "%s.\n", err);
925 appctx->ctx.cli.err = err;
926 appctx->st0 = CLI_ST_PRINT_FREE;
927 }
928 else {
929 appctx->ctx.cli.severity = LOG_ERR;
930 appctx->ctx.cli.msg = "Failed to add a key.\n";
931 appctx->st0 = CLI_ST_PRINT;
932 }
933 return 1;
934 }
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200935 }
William Lallemandad8be612016-11-18 19:26:17 +0100936 }
937
938 /* The add is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100939 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100940 return 1;
941 }
942
943 return 0;
944}
945
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200946static int cli_parse_del_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100947{
948 if (args[1][0] == 'm')
949 appctx->ctx.map.display_flags = PAT_REF_MAP;
950 else
951 appctx->ctx.map.display_flags = PAT_REF_ACL;
952
953 /* Expect two parameters: map name and key. */
954 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
955 if (!*args[2] || !*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200956 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100957 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100958 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100959 return 1;
960 }
961 }
962
963 else {
964 if (!*args[2] || !*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200965 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100966 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100967 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100968 return 1;
969 }
970 }
971
972 /* Lookup the reference in the maps. */
973 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
974 if (!appctx->ctx.map.ref ||
975 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200976 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100977 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100978 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100979 return 1;
980 }
981
982 /* If the entry identifier start with a '#', it is considered as
983 * pointer id
984 */
985 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
986 struct pat_ref_elt *ref;
987 long long int conv;
988 char *error;
989
990 /* Convert argument to integer value. */
991 conv = strtoll(&args[3][1], &error, 16);
992 if (*error != '\0') {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200993 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100994 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100995 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100996 return 1;
997 }
998
999 /* Convert and check integer to pointer. */
1000 ref = (struct pat_ref_elt *)(long)conv;
1001 if ((long long int)(long)ref != conv) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001002 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001003 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001004 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001005 return 1;
1006 }
1007
1008 /* Try to delete the entry. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001009 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001010 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001011 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001012 /* The entry is not found, send message. */
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001013 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001014 appctx->ctx.cli.msg = "Key not found.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001015 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001016 return 1;
1017 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001018 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001019 }
1020 else {
1021 /* Else, use the entry identifier as pattern
1022 * string and try to delete the entry.
1023 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001024 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001025 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001026 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001027 /* The entry is not found, send message. */
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001028 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001029 appctx->ctx.cli.msg = "Key not found.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001030 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001031 return 1;
1032 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001033 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001034 }
1035
1036 /* The deletion is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001037 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +01001038 return 1;
1039}
1040
1041
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001042static int cli_parse_clear_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +01001043{
1044 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1045 /* Set ACL or MAP flags. */
1046 if (args[1][0] == 'm')
1047 appctx->ctx.map.display_flags = PAT_REF_MAP;
1048 else
1049 appctx->ctx.map.display_flags = PAT_REF_ACL;
1050
1051 /* no parameter */
1052 if (!*args[2]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001053 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
1054 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001055 appctx->ctx.cli.msg = "Missing map identifier.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001056 }
1057 else {
1058 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001059 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001060 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001061 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001062 return 1;
1063 }
1064
1065 /* lookup into the refs and check the map flag */
1066 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
1067 if (!appctx->ctx.map.ref ||
1068 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001069 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
1070 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001071 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001072 }
1073 else {
1074 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001075 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001076 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001077 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001078 return 1;
1079 }
1080
1081 /* Clear all. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001082 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001083 pat_ref_prune(appctx->ctx.map.ref);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001084 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001085
1086 /* return response */
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001087 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +01001088 return 1;
1089 }
1090 return 0;
1091}
1092
1093/* register cli keywords */
1094
1095static struct cli_kw_list cli_kws = {{ },{
1096 { { "add", "acl", NULL }, "add acl : add acl entry", cli_parse_add_map, NULL },
1097 { { "clear", "acl", NULL }, "clear acl <id> : clear the content of this acl", cli_parse_clear_map, NULL },
1098 { { "del", "acl", NULL }, "del acl : delete acl entry", cli_parse_del_map, NULL },
1099 { { "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 },
1100 { { "show", "acl", NULL }, "show acl [id] : report available acls or dump an acl's contents", cli_parse_show_map, NULL },
1101 { { "add", "map", NULL }, "add map : add map entry", cli_parse_add_map, NULL },
1102 { { "clear", "map", NULL }, "clear map <id> : clear the content of this map", cli_parse_clear_map, NULL },
1103 { { "del", "map", NULL }, "del map : delete map entry", cli_parse_del_map, NULL },
Nenad Merdanovic96c15712017-03-12 22:01:36 +01001104 { { "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 +01001105 { { "set", "map", NULL }, "set map : modify map entry", cli_parse_set_map, NULL },
1106 { { "show", "map", NULL }, "show map [id] : report available maps or dump a map's contents", cli_parse_show_map, NULL },
1107 { { NULL }, NULL, NULL, NULL }
1108}};
1109
Willy Tarreau0108d902018-11-25 19:14:37 +01001110INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemandad8be612016-11-18 19:26:17 +01001111
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +01001112/* Note: must not be declared <const> as its list will be overwritten
1113 *
1114 * For the map_*_int keywords, the output is declared as SMP_T_UINT, but the converter function
1115 * can provide SMP_T_UINT, SMP_T_SINT or SMP_T_BOOL depending on how the patterns found in the
1116 * file can be parsed.
1117 *
1118 * For the map_*_ip keyword, the output is declared as SMP_T_IPV4, but the converter function
1119 * can provide SMP_T_IPV4 or SMP_T_IPV6 depending on the patterns found in the file.
1120 *
1121 * The map_* keywords only emit strings.
1122 *
1123 * The output type is only used during the configuration parsing. It is used for detecting
1124 * compatibility problems.
1125 *
1126 * The arguments are: <file>[,<default value>]
1127 */
1128static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER1edc9712014-12-15 16:18:39 +01001129 { "map", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1130 { "map_str", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1131 { "map_beg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_BEG },
1132 { "map_sub", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_SUB },
1133 { "map_dir", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DIR },
1134 { "map_dom", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DOM },
1135 { "map_end", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_END },
1136 { "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 +01001137 { "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 +02001138 { "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 +01001139 { "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 +01001140
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001141 { "map_str_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_STR },
1142 { "map_beg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_BEG },
1143 { "map_sub_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_SUB },
1144 { "map_dir_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DIR },
1145 { "map_dom_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DOM },
1146 { "map_end_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_END },
1147 { "map_reg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_REG },
1148 { "map_int_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_SINT, SMP_T_SINT, (void *)PAT_MATCH_INT },
1149 { "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 +01001150
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +02001151 { "map_str_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_STR },
1152 { "map_beg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_BEG },
1153 { "map_sub_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_SUB },
1154 { "map_dir_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DIR },
1155 { "map_dom_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DOM },
1156 { "map_end_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_END },
1157 { "map_reg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_REG },
1158 { "map_int_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_SINT, SMP_T_ADDR, (void *)PAT_MATCH_INT },
1159 { "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 +01001160
1161 { /* END */ },
1162}};
1163
Willy Tarreau0108d902018-11-25 19:14:37 +01001164INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);