blob: aabaa60df22a56a1ece1ba6f61e224bbbe1cbe11 [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
16#include <common/standard.h>
17
William Lallemandad8be612016-11-18 19:26:17 +010018#include <types/applet.h>
19#include <types/cli.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010020#include <types/global.h>
21#include <types/map.h>
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010022#include <types/pattern.h>
William Lallemandad8be612016-11-18 19:26:17 +010023#include <types/stats.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010024
William Lallemandad8be612016-11-18 19:26:17 +010025#include <proto/applet.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010026#include <proto/arg.h>
William Lallemandad8be612016-11-18 19:26:17 +010027#include <proto/cli.h>
Thierry FOURNIERb0c0a0f2013-12-10 15:05:34 +010028#include <proto/map.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010029#include <proto/pattern.h>
William Lallemandad8be612016-11-18 19:26:17 +010030#include <proto/stream_interface.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010031#include <proto/sample.h>
32
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020033/* Parse an IPv4 or IPv6 address and store it into the sample.
34 * The output type is IPv4 or IPv6.
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010035 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020036int map_parse_ip(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010037{
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020038 int len = strlen(text);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010039
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020040 if (buf2ip(text, len, &data->u.ipv4)) {
41 data->type = SMP_T_IPV4;
42 return 1;
43 }
44 if (buf2ip6(text, len, &data->u.ipv6)) {
45 data->type = SMP_T_IPV6;
46 return 1;
47 }
48 return 0;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010049}
50
51/* Parse a string and store a pointer to it into the sample. The original
52 * string must be left in memory because we return a direct memory reference.
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010053 * The output type is SMP_T_STR. There is no risk that the data will be
54 * overwritten because sample_conv_map() makes a const sample with this
55 * output.
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010056 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020057int map_parse_str(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010058{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020059 data->u.str.str = (char *)text;
60 data->u.str.len = strlen(text);
61 data->u.str.size = data->u.str.len + 1;
Thierry FOURNIER503bb092015-08-19 08:35:43 +020062 data->type = SMP_T_STR;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010063 return 1;
64}
65
66/* Parse an integer and convert it to a sample. The output type is SINT if the
67 * number is negative, or UINT if it is positive or null. The function returns
68 * zero (error) if the number is too large.
69 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020070int map_parse_int(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010071{
Thierry FOURNIER503bb092015-08-19 08:35:43 +020072 data->type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020073 data->u.sint = read_int64(&text, text + strlen(text));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020074 if (*text != '\0')
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010075 return 0;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010076 return 1;
77}
78
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010079/* This crete and initialize map descriptor.
80 * Return NULL if out of memory error
81 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010082static struct map_descriptor *map_create_descriptor(struct sample_conv *conv)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010083{
84 struct map_descriptor *desc;
85
86 desc = calloc(1, sizeof(*desc));
87 if (!desc)
88 return NULL;
89
90 desc->conv = conv;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010091
92 return desc;
93}
94
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010095/* This function load the map file according with data type declared into
96 * the "struct sample_conv".
97 *
98 * This function choose the indexation type (ebtree or list) according with
99 * the type of match needed.
100 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200101int sample_load_map(struct arg *arg, struct sample_conv *conv,
102 const char *file, int line, char **err)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100103{
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100104 struct map_descriptor *desc;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100105
106 /* create new map descriptor */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100107 desc = map_create_descriptor(conv);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100108 if (!desc) {
109 memprintf(err, "out of memory");
110 return 0;
111 }
112
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100113 /* Initialize pattern */
114 pattern_init_head(&desc->pat);
115
116 /* This is original pattern, must free */
117 desc->do_free = 1;
118
119 /* Set the match method. */
Thierry FOURNIER1edc9712014-12-15 16:18:39 +0100120 desc->pat.match = pat_match_fcts[(long)conv->private];
121 desc->pat.parse = pat_parse_fcts[(long)conv->private];
122 desc->pat.index = pat_index_fcts[(long)conv->private];
123 desc->pat.delete = pat_delete_fcts[(long)conv->private];
124 desc->pat.prune = pat_prune_fcts[(long)conv->private];
125 desc->pat.expect_type = pat_match_types[(long)conv->private];
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100126
127 /* Set the output parse method. */
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100128 switch (desc->conv->out_type) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100129 case SMP_T_STR: desc->pat.parse_smp = map_parse_str; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200130 case SMP_T_SINT: desc->pat.parse_smp = map_parse_int; break;
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200131 case SMP_T_ADDR: desc->pat.parse_smp = map_parse_ip; break;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100132 default:
133 memprintf(err, "map: internal haproxy error: no default parse case for the input type <%d>.",
134 conv->out_type);
Andreas Seltenreich78f35952016-03-03 20:32:23 +0100135 free(desc);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100136 return 0;
137 }
138
Thierry FOURNIER39bef452014-01-29 13:29:45 +0100139 /* Load map. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200140 if (!pattern_read_from_file(&desc->pat, PAT_REF_MAP, arg[0].data.str.str, PAT_MF_NO_DNS,
Thierry FOURNIER94580c92014-02-11 14:36:45 +0100141 1, err, file, line))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100142 return 0;
Thierry FOURNIER0ffe78c2013-12-05 14:40:25 +0100143
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200144 /* the maps of type IP have a string as defaultvalue. This
145 * string canbe anipv4 or an ipv6, we must convert it.
146 */
147 if (desc->conv->out_type == SMP_T_ADDR) {
148 struct sample_data data;
149 if (!map_parse_ip(arg[1].data.str.str, &data)) {
150 memprintf(err, "map: cannot parse default ip <%s>.", arg[1].data.str.str);
151 return 0;
152 }
153 if (data.type == SMP_T_IPV4) {
154 arg[1].type = ARGT_IPV4;
155 arg[1].data.ipv4 = data.u.ipv4;
156 } else {
157 arg[1].type = ARGT_IPV6;
158 arg[1].data.ipv6 = data.u.ipv6;
159 }
160 }
161
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100162 /* replace the first argument by this definition */
163 arg[0].type = ARGT_MAP;
164 arg[0].data.map = desc;
165
166 return 1;
167}
168
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200169static int sample_conv_map(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100170{
171 struct map_descriptor *desc;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100172 struct pattern *pat;
Thierry Fournier8feaa662016-02-10 22:55:20 +0100173 struct chunk *str;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100174
175 /* get config */
176 desc = arg_p[0].data.map;
177
178 /* Execute the match function. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100179 pat = pattern_exec_match(&desc->pat, smp, 1);
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100180
181 /* Match case. */
182 if (pat) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200183 if (pat->data) {
Thierry Fournier8feaa662016-02-10 22:55:20 +0100184 /* In the regm case, merge the sample with the input. */
185 if ((long)private == PAT_MATCH_REGM) {
186 str = get_trash_chunk();
187 str->len = exp_replace(str->str, str->size, smp->data.u.str.str,
188 pat->data->u.str.str,
189 (regmatch_t *)smp->ctx.a[0]);
190 if (str->len == -1)
191 return 0;
192 smp->data.u.str = *str;
193 return 1;
194 }
195 /* Copy sample. */
Thierry FOURNIER5cc18d42015-08-19 09:02:36 +0200196 smp->data = *pat->data;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100197 smp->flags |= SMP_F_CONST;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100198 return 1;
199 }
200
201 /* Return just int sample containing 1. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200202 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200203 smp->data.u.sint = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100204 return 1;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100205 }
206
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100207 /* If no default value avalaible, the converter fails. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100208 if (arg_p[1].type == ARGT_STOP)
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100209 return 0;
210
211 /* Return the default value. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100212 switch (desc->conv->out_type) {
213
214 case SMP_T_STR:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200215 smp->data.type = SMP_T_STR;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100216 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200217 smp->data.u.str = arg_p[1].data.str;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100218 break;
219
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200220 case SMP_T_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200221 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200222 smp->data.u.sint = arg_p[1].data.sint;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100223 break;
224
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200225 case SMP_T_ADDR:
226 if (arg_p[1].type == ARGT_IPV4) {
227 smp->data.type = SMP_T_IPV4;
228 smp->data.u.ipv4 = arg_p[1].data.ipv4;
229 } else {
230 smp->data.type = SMP_T_IPV6;
231 smp->data.u.ipv6 = arg_p[1].data.ipv6;
232 }
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100233 break;
234 }
235
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100236 return 1;
237}
238
William Lallemandad8be612016-11-18 19:26:17 +0100239/* This function is used with map and acl management. It permits to browse
240 * each reference. The variable <getnext> must contain the current node,
241 * <end> point to the root node and the <flags> permit to filter required
242 * nodes.
243 */
244static inline
245struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
246 unsigned int flags)
247{
248 struct pat_ref *ref = getnext;
249
250 while (1) {
251
252 /* Get next list entry. */
253 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
254
255 /* If the entry is the last of the list, return NULL. */
256 if (&ref->list == end)
257 return NULL;
258
259 /* If the entry match the flag, return it. */
260 if (ref->flags & flags)
261 return ref;
262 }
263}
264
265static inline
266struct pat_ref *pat_ref_lookup_ref(const char *reference)
267{
268 int id;
269 char *error;
270
271 /* If the reference starts by a '#', this is numeric id. */
272 if (reference[0] == '#') {
273 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
274 id = strtol(reference + 1, &error, 10);
275 if (*error != '\0')
276 return NULL;
277
278 /* Perform the unique id lookup. */
279 return pat_ref_lookupid(id);
280 }
281
282 /* Perform the string lookup. */
283 return pat_ref_lookup(reference);
284}
285
286/* This function is used with map and acl management. It permits to browse
287 * each reference.
288 */
289static inline
290struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
291{
292 struct pattern_expr *expr;
293 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
294 if (&expr->list == end)
295 return NULL;
296 return expr;
297}
298
299static int cli_io_handler_pat_list(struct appctx *appctx)
300{
301 struct stream_interface *si = appctx->owner;
302
303 switch (appctx->st2) {
304
305 case STAT_ST_INIT:
306 /* Init to the first entry. The list cannot be change */
307 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
308 struct pat_ref_elt *, list);
309 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
310 appctx->ctx.map.elt = NULL;
311 appctx->st2 = STAT_ST_LIST;
312 /* fall through */
313
314 case STAT_ST_LIST:
315 while (appctx->ctx.map.elt) {
316 chunk_reset(&trash);
317
318 /* build messages */
319 if (appctx->ctx.map.elt->sample)
320 chunk_appendf(&trash, "%p %s %s\n",
321 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
322 appctx->ctx.map.elt->sample);
323 else
324 chunk_appendf(&trash, "%p %s\n",
325 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
326
327 if (bi_putchk(si_ic(si), &trash) == -1) {
328 /* let's try again later from this stream. We add ourselves into
329 * this stream's users so that it can remove us upon termination.
330 */
331 si_applet_cant_put(si);
332 return 0;
333 }
334
335 /* get next list entry and check the end of the list */
336 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
337 struct pat_ref_elt *, list);
338 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
339 break;
340 }
341
342 appctx->st2 = STAT_ST_FIN;
343 /* fall through */
344
345 default:
346 appctx->st2 = STAT_ST_FIN;
347 return 1;
348 }
349}
350
351static int cli_io_handler_pats_list(struct appctx *appctx)
352{
353 struct stream_interface *si = appctx->owner;
354
355 switch (appctx->st2) {
356 case STAT_ST_INIT:
357 /* Display the column headers. If the message cannot be sent,
358 * quit the fucntion with returning 0. The function is called
359 * later and restart at the state "STAT_ST_INIT".
360 */
361 chunk_reset(&trash);
362 chunk_appendf(&trash, "# id (file) description\n");
363 if (bi_putchk(si_ic(si), &trash) == -1) {
364 si_applet_cant_put(si);
365 return 0;
366 }
367
368 /* Now, we start the browsing of the references lists.
369 * Note that the following call to LIST_ELEM return bad pointer. The only
370 * available field of this pointer is <list>. It is used with the function
371 * pat_list_get_next() for retruning the first available entry
372 */
373 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
374 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
375 appctx->ctx.map.display_flags);
376 appctx->st2 = STAT_ST_LIST;
377 /* fall through */
378
379 case STAT_ST_LIST:
380 while (appctx->ctx.map.ref) {
381 chunk_reset(&trash);
382
383 /* Build messages. If the reference is used by another category than
384 * the listed categorie, display the information in the massage.
385 */
386 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
387 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
388 appctx->ctx.map.ref->display);
389
390 if (bi_putchk(si_ic(si), &trash) == -1) {
391 /* let's try again later from this stream. We add ourselves into
392 * this stream's users so that it can remove us upon termination.
393 */
394 si_applet_cant_put(si);
395 return 0;
396 }
397
398 /* get next list entry and check the end of the list */
399 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
400 appctx->ctx.map.display_flags);
401 }
402
403 appctx->st2 = STAT_ST_FIN;
404 /* fall through */
405
406 default:
407 appctx->st2 = STAT_ST_FIN;
408 return 1;
409 }
410 return 0;
411}
412
413static int cli_io_handler_map_lookup(struct appctx *appctx)
414{
415 struct stream_interface *si = appctx->owner;
416 struct sample sample;
417 struct pattern *pat;
418 int match_method;
419
420 switch (appctx->st2) {
421 case STAT_ST_INIT:
422 /* Init to the first entry. The list cannot be change */
423 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
424 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
425 appctx->st2 = STAT_ST_LIST;
426 /* fall through */
427
428 case STAT_ST_LIST:
429 /* for each lookup type */
430 while (appctx->ctx.map.expr) {
431 /* initialise chunk to build new message */
432 chunk_reset(&trash);
433
434 /* execute pattern matching */
435 sample.data.type = SMP_T_STR;
436 sample.flags = SMP_F_CONST;
437 sample.data.u.str.len = appctx->ctx.map.chunk.len;
438 sample.data.u.str.str = appctx->ctx.map.chunk.str;
439 if (appctx->ctx.map.expr->pat_head->match &&
440 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
441 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
442 else
443 pat = NULL;
444
445 /* build return message: set type of match */
446 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
447 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
448 break;
449 if (match_method >= PAT_MATCH_NUM)
450 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
451 else
452 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
453
454 /* case sensitive */
455 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
456 chunk_appendf(&trash, ", case=insensitive");
457 else
458 chunk_appendf(&trash, ", case=sensitive");
459
460 /* Display no match, and set default value */
461 if (!pat) {
462 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
463 chunk_appendf(&trash, ", found=no");
464 else
465 chunk_appendf(&trash, ", match=no");
466 }
467
468 /* Display match and match info */
469 else {
470 /* display match */
471 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
472 chunk_appendf(&trash, ", found=yes");
473 else
474 chunk_appendf(&trash, ", match=yes");
475
476 /* display index mode */
477 if (pat->sflags & PAT_SF_TREE)
478 chunk_appendf(&trash, ", idx=tree");
479 else
480 chunk_appendf(&trash, ", idx=list");
481
482 /* display pattern */
483 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
484 if (pat->ref && pat->ref->pattern)
485 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
486 else
487 chunk_appendf(&trash, ", key=unknown");
488 }
489 else {
490 if (pat->ref && pat->ref->pattern)
491 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
492 else
493 chunk_appendf(&trash, ", pattern=unknown");
494 }
495
496 /* display return value */
497 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
498 if (pat->data && pat->ref && pat->ref->sample)
499 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
500 smp_to_type[pat->data->type]);
501 else
502 chunk_appendf(&trash, ", value=none");
503 }
504 }
505
506 chunk_appendf(&trash, "\n");
507
508 /* display response */
509 if (bi_putchk(si_ic(si), &trash) == -1) {
510 /* let's try again later from this stream. We add ourselves into
511 * this stream's users so that it can remove us upon termination.
512 */
513 si_applet_cant_put(si);
514 return 0;
515 }
516
517 /* get next entry */
518 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
519 &appctx->ctx.map.ref->pat);
520 }
521
522 appctx->st2 = STAT_ST_FIN;
523 /* fall through */
524
525 default:
526 appctx->st2 = STAT_ST_FIN;
527 free(appctx->ctx.map.chunk.str);
528 return 1;
529 }
530}
531
532static void cli_release_mlook(struct appctx *appctx)
533{
534 free(appctx->ctx.map.chunk.str);
535 appctx->ctx.map.chunk.str = NULL;
536}
537
538
539static int cli_parse_get_map(char **args, struct appctx *appctx, void *private)
540{
541 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
542 /* Set flags. */
543 if (args[1][0] == 'm')
544 appctx->ctx.map.display_flags = PAT_REF_MAP;
545 else
546 appctx->ctx.map.display_flags = PAT_REF_ACL;
547
548 /* No parameter. */
549 if (!*args[2] || !*args[3]) {
550 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
551 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
552 else
553 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
554 appctx->st0 = STAT_CLI_PRINT;
555 return 1;
556 }
557
558 /* lookup into the maps */
559 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
560 if (!appctx->ctx.map.ref) {
561 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
562 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
563 else
564 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
565 appctx->st0 = STAT_CLI_PRINT;
566 return 1;
567 }
568
569 /* copy input string. The string must be allocated because
570 * it may be used over multiple iterations. It's released
571 * at the end and upon abort anyway.
572 */
573 appctx->ctx.map.chunk.len = strlen(args[3]);
574 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
575 appctx->ctx.map.chunk.str = strdup(args[3]);
576 if (!appctx->ctx.map.chunk.str) {
577 appctx->ctx.cli.msg = "Out of memory error.\n";
578 appctx->st0 = STAT_CLI_PRINT;
579 return 1;
580 }
581
582 return 0;
583 }
584 return 1;
585}
586
587static int cli_parse_show_map(char **args, struct appctx *appctx, void *private)
588{
589 if (strcmp(args[1], "map") == 0 ||
590 strcmp(args[1], "acl") == 0) {
591
592 /* Set ACL or MAP 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: display all map available */
599 if (!*args[2]) {
600 appctx->st2 = STAT_ST_INIT;
601 appctx->st0 = STAT_CLI_O_CUSTOM;
602 appctx->io_handler = cli_io_handler_pats_list;
603 return 0;
604 }
605
606 /* lookup into the refs and check the map flag */
607 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
608 if (!appctx->ctx.map.ref ||
609 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
610 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
611 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
612 else
613 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
614 appctx->st0 = STAT_CLI_PRINT;
615 return 1;
616 }
617 appctx->st2 = STAT_ST_INIT;
618 appctx->st0 = STAT_CLI_O_CUSTOM;
619 appctx->io_handler = cli_io_handler_pat_list;
620 return 0;
621 }
622
623 return 0;
624}
625
626static int cli_parse_set_map(char **args, struct appctx *appctx, void *private)
627{
628 if (strcmp(args[1], "map") == 0) {
629 char *err;
630
631 /* Set flags. */
632 appctx->ctx.map.display_flags = PAT_REF_MAP;
633
634 /* Expect three parameters: map name, key and new value. */
635 if (!*args[2] || !*args[3] || !*args[4]) {
636 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
637 appctx->st0 = STAT_CLI_PRINT;
638 return 1;
639 }
640
641 /* Lookup the reference in the maps. */
642 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
643 if (!appctx->ctx.map.ref) {
644 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
645 appctx->st0 = STAT_CLI_PRINT;
646 return 1;
647 }
648
649 /* If the entry identifier start with a '#', it is considered as
650 * pointer id
651 */
652 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
653 struct pat_ref_elt *ref;
654 long long int conv;
655 char *error;
656
657 /* Convert argument to integer value. */
658 conv = strtoll(&args[3][1], &error, 16);
659 if (*error != '\0') {
660 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
661 appctx->st0 = STAT_CLI_PRINT;
662 return 1;
663 }
664
665 /* Convert and check integer to pointer. */
666 ref = (struct pat_ref_elt *)(long)conv;
667 if ((long long int)(long)ref != conv) {
668 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
669 appctx->st0 = STAT_CLI_PRINT;
670 return 1;
671 }
672
673 /* Try to delete the entry. */
674 err = NULL;
675 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
676 if (err)
677 memprintf(&err, "%s.\n", err);
678 appctx->ctx.cli.err = err;
679 appctx->st0 = STAT_CLI_PRINT_FREE;
680 return 1;
681 }
682 }
683 else {
684 /* Else, use the entry identifier as pattern
685 * string, and update the value.
686 */
687 err = NULL;
688 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
689 if (err)
690 memprintf(&err, "%s.\n", err);
691 appctx->ctx.cli.err = err;
692 appctx->st0 = STAT_CLI_PRINT_FREE;
693 return 1;
694 }
695 }
696
697 /* The set is done, send message. */
698 appctx->st0 = STAT_CLI_PROMPT;
699 return 0;
700 }
701 return 1;
702}
703
704static int cli_parse_add_map(char **args, struct appctx *appctx, void *private)
705{
706 if (strcmp(args[1], "map") == 0 ||
707 strcmp(args[1], "acl") == 0) {
708 int ret;
709 char *err;
710
711 /* Set flags. */
712 if (args[1][0] == 'm')
713 appctx->ctx.map.display_flags = PAT_REF_MAP;
714 else
715 appctx->ctx.map.display_flags = PAT_REF_ACL;
716
717 /* If the keywork is "map", we expect three parameters, if it
718 * is "acl", we expect only two parameters
719 */
720 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
721 if (!*args[2] || !*args[3] || !*args[4]) {
722 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
723 appctx->st0 = STAT_CLI_PRINT;
724 return 1;
725 }
726 }
727 else {
728 if (!*args[2] || !*args[3]) {
729 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
730 appctx->st0 = STAT_CLI_PRINT;
731 return 1;
732 }
733 }
734
735 /* Lookup for the reference. */
736 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
737 if (!appctx->ctx.map.ref) {
738 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
739 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
740 else
741 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
742 appctx->st0 = STAT_CLI_PRINT;
743 return 1;
744 }
745
746 /* The command "add acl" is prohibited if the reference
747 * use samples.
748 */
749 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
750 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
751 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
752 "You must use the command 'add map' to add values.\n";
753 appctx->st0 = STAT_CLI_PRINT;
754 return 1;
755 }
756
757 /* Add value. */
758 err = NULL;
759 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
760 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
761 else
762 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
763 if (!ret) {
764 if (err)
765 memprintf(&err, "%s.\n", err);
766 appctx->ctx.cli.err = err;
767 appctx->st0 = STAT_CLI_PRINT_FREE;
768 return 1;
769 }
770
771 /* The add is done, send message. */
772 appctx->st0 = STAT_CLI_PROMPT;
773 return 1;
774 }
775
776 return 0;
777}
778
779static int cli_parse_del_map(char **args, struct appctx *appctx, void *private)
780{
781 if (args[1][0] == 'm')
782 appctx->ctx.map.display_flags = PAT_REF_MAP;
783 else
784 appctx->ctx.map.display_flags = PAT_REF_ACL;
785
786 /* Expect two parameters: map name and key. */
787 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
788 if (!*args[2] || !*args[3]) {
789 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
790 appctx->st0 = STAT_CLI_PRINT;
791 return 1;
792 }
793 }
794
795 else {
796 if (!*args[2] || !*args[3]) {
797 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
798 appctx->st0 = STAT_CLI_PRINT;
799 return 1;
800 }
801 }
802
803 /* Lookup the reference in the maps. */
804 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
805 if (!appctx->ctx.map.ref ||
806 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
807 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
808 appctx->st0 = STAT_CLI_PRINT;
809 return 1;
810 }
811
812 /* If the entry identifier start with a '#', it is considered as
813 * pointer id
814 */
815 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
816 struct pat_ref_elt *ref;
817 long long int conv;
818 char *error;
819
820 /* Convert argument to integer value. */
821 conv = strtoll(&args[3][1], &error, 16);
822 if (*error != '\0') {
823 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
824 appctx->st0 = STAT_CLI_PRINT;
825 return 1;
826 }
827
828 /* Convert and check integer to pointer. */
829 ref = (struct pat_ref_elt *)(long)conv;
830 if ((long long int)(long)ref != conv) {
831 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
832 appctx->st0 = STAT_CLI_PRINT;
833 return 1;
834 }
835
836 /* Try to delete the entry. */
837 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
838 /* The entry is not found, send message. */
839 appctx->ctx.cli.msg = "Key not found.\n";
840 appctx->st0 = STAT_CLI_PRINT;
841 return 1;
842 }
843 }
844 else {
845 /* Else, use the entry identifier as pattern
846 * string and try to delete the entry.
847 */
848 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
849 /* The entry is not found, send message. */
850 appctx->ctx.cli.msg = "Key not found.\n";
851 appctx->st0 = STAT_CLI_PRINT;
852 return 1;
853 }
854 }
855
856 /* The deletion is done, send message. */
857 appctx->st0 = STAT_CLI_PROMPT;
858 return 1;
859}
860
861
862static int cli_parse_clear_map(char **args, struct appctx *appctx, void *private)
863{
864 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
865 /* Set ACL or MAP flags. */
866 if (args[1][0] == 'm')
867 appctx->ctx.map.display_flags = PAT_REF_MAP;
868 else
869 appctx->ctx.map.display_flags = PAT_REF_ACL;
870
871 /* no parameter */
872 if (!*args[2]) {
873 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
874 appctx->ctx.cli.msg = "Missing map identifier.\n";
875 else
876 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
877 appctx->st0 = STAT_CLI_PRINT;
878 return 1;
879 }
880
881 /* lookup into the refs and check the map flag */
882 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
883 if (!appctx->ctx.map.ref ||
884 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
885 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
886 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
887 else
888 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
889 appctx->st0 = STAT_CLI_PRINT;
890 return 1;
891 }
892
893 /* Clear all. */
894 pat_ref_prune(appctx->ctx.map.ref);
895
896 /* return response */
897 appctx->st0 = STAT_CLI_PROMPT;
898 return 1;
899 }
900 return 0;
901}
902
903/* register cli keywords */
904
905static struct cli_kw_list cli_kws = {{ },{
906 { { "add", "acl", NULL }, "add acl : add acl entry", cli_parse_add_map, NULL },
907 { { "clear", "acl", NULL }, "clear acl <id> : clear the content of this acl", cli_parse_clear_map, NULL },
908 { { "del", "acl", NULL }, "del acl : delete acl entry", cli_parse_del_map, NULL },
909 { { "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 },
910 { { "show", "acl", NULL }, "show acl [id] : report available acls or dump an acl's contents", cli_parse_show_map, NULL },
911 { { "add", "map", NULL }, "add map : add map entry", cli_parse_add_map, NULL },
912 { { "clear", "map", NULL }, "clear map <id> : clear the content of this map", cli_parse_clear_map, NULL },
913 { { "del", "map", NULL }, "del map : delete map entry", cli_parse_del_map, NULL },
914 { { "get", "map", NULL }, "get map : report the keys and values matching a sample for a map", cli_parse_get_map, NULL },
915 { { "set", "map", NULL }, "set map : modify map entry", cli_parse_set_map, NULL },
916 { { "show", "map", NULL }, "show map [id] : report available maps or dump a map's contents", cli_parse_show_map, NULL },
917 { { NULL }, NULL, NULL, NULL }
918}};
919
920
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100921/* Note: must not be declared <const> as its list will be overwritten
922 *
923 * For the map_*_int keywords, the output is declared as SMP_T_UINT, but the converter function
924 * can provide SMP_T_UINT, SMP_T_SINT or SMP_T_BOOL depending on how the patterns found in the
925 * file can be parsed.
926 *
927 * For the map_*_ip keyword, the output is declared as SMP_T_IPV4, but the converter function
928 * can provide SMP_T_IPV4 or SMP_T_IPV6 depending on the patterns found in the file.
929 *
930 * The map_* keywords only emit strings.
931 *
932 * The output type is only used during the configuration parsing. It is used for detecting
933 * compatibility problems.
934 *
935 * The arguments are: <file>[,<default value>]
936 */
937static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER1edc9712014-12-15 16:18:39 +0100938 { "map", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
939 { "map_str", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
940 { "map_beg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_BEG },
941 { "map_sub", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_SUB },
942 { "map_dir", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DIR },
943 { "map_dom", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DOM },
944 { "map_end", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_END },
945 { "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 +0100946 { "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 +0200947 { "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 +0100948 { "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 +0100949
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200950 { "map_str_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_STR },
951 { "map_beg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_BEG },
952 { "map_sub_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_SUB },
953 { "map_dir_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DIR },
954 { "map_dom_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DOM },
955 { "map_end_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_END },
956 { "map_reg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_REG },
957 { "map_int_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_SINT, SMP_T_SINT, (void *)PAT_MATCH_INT },
958 { "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 +0100959
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200960 { "map_str_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_STR },
961 { "map_beg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_BEG },
962 { "map_sub_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_SUB },
963 { "map_dir_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DIR },
964 { "map_dom_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DOM },
965 { "map_end_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_END },
966 { "map_reg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_REG },
967 { "map_int_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_SINT, SMP_T_ADDR, (void *)PAT_MATCH_INT },
968 { "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 +0100969
970 { /* END */ },
971}};
972
973__attribute__((constructor))
974static void __map_init(void)
975{
976 /* register format conversion keywords */
977 sample_register_convs(&sample_conv_kws);
William Lallemandad8be612016-11-18 19:26:17 +0100978 cli_register_kw(&cli_kws);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100979}