blob: d94b0f2605c43134e8e1d2d71a196ab0b4057343 [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>
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020028#include <proto/log.h>
Thierry FOURNIERb0c0a0f2013-12-10 15:05:34 +010029#include <proto/map.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010030#include <proto/pattern.h>
William Lallemandad8be612016-11-18 19:26:17 +010031#include <proto/stream_interface.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010032#include <proto/sample.h>
33
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020034/* Parse an IPv4 or IPv6 address and store it into the sample.
35 * The output type is IPv4 or IPv6.
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010036 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020037int map_parse_ip(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010038{
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020039 int len = strlen(text);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010040
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020041 if (buf2ip(text, len, &data->u.ipv4)) {
42 data->type = SMP_T_IPV4;
43 return 1;
44 }
45 if (buf2ip6(text, len, &data->u.ipv6)) {
46 data->type = SMP_T_IPV6;
47 return 1;
48 }
49 return 0;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010050}
51
52/* Parse a string and store a pointer to it into the sample. The original
53 * string must be left in memory because we return a direct memory reference.
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010054 * The output type is SMP_T_STR. There is no risk that the data will be
55 * overwritten because sample_conv_map() makes a const sample with this
56 * output.
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010057 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020058int map_parse_str(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010059{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020060 data->u.str.area = (char *)text;
61 data->u.str.data = strlen(text);
62 data->u.str.size = data->u.str.data + 1;
Thierry FOURNIER503bb092015-08-19 08:35:43 +020063 data->type = SMP_T_STR;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010064 return 1;
65}
66
67/* Parse an integer and convert it to a sample. The output type is SINT if the
68 * number is negative, or UINT if it is positive or null. The function returns
69 * zero (error) if the number is too large.
70 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020071int map_parse_int(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010072{
Thierry FOURNIER503bb092015-08-19 08:35:43 +020073 data->type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020074 data->u.sint = read_int64(&text, text + strlen(text));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020075 if (*text != '\0')
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010076 return 0;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010077 return 1;
78}
79
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010080/* This crete and initialize map descriptor.
81 * Return NULL if out of memory error
82 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010083static struct map_descriptor *map_create_descriptor(struct sample_conv *conv)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010084{
85 struct map_descriptor *desc;
86
87 desc = calloc(1, sizeof(*desc));
88 if (!desc)
89 return NULL;
90
91 desc->conv = conv;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010092
93 return desc;
94}
95
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010096/* This function load the map file according with data type declared into
97 * the "struct sample_conv".
98 *
99 * This function choose the indexation type (ebtree or list) according with
100 * the type of match needed.
101 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200102int sample_load_map(struct arg *arg, struct sample_conv *conv,
103 const char *file, int line, char **err)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100104{
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100105 struct map_descriptor *desc;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100106
107 /* create new map descriptor */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100108 desc = map_create_descriptor(conv);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100109 if (!desc) {
110 memprintf(err, "out of memory");
111 return 0;
112 }
113
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100114 /* Initialize pattern */
115 pattern_init_head(&desc->pat);
116
117 /* This is original pattern, must free */
118 desc->do_free = 1;
119
120 /* Set the match method. */
Thierry FOURNIER1edc9712014-12-15 16:18:39 +0100121 desc->pat.match = pat_match_fcts[(long)conv->private];
122 desc->pat.parse = pat_parse_fcts[(long)conv->private];
123 desc->pat.index = pat_index_fcts[(long)conv->private];
124 desc->pat.delete = pat_delete_fcts[(long)conv->private];
125 desc->pat.prune = pat_prune_fcts[(long)conv->private];
126 desc->pat.expect_type = pat_match_types[(long)conv->private];
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100127
128 /* Set the output parse method. */
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100129 switch (desc->conv->out_type) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100130 case SMP_T_STR: desc->pat.parse_smp = map_parse_str; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200131 case SMP_T_SINT: desc->pat.parse_smp = map_parse_int; break;
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200132 case SMP_T_ADDR: desc->pat.parse_smp = map_parse_ip; break;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100133 default:
134 memprintf(err, "map: internal haproxy error: no default parse case for the input type <%d>.",
135 conv->out_type);
Andreas Seltenreich78f35952016-03-03 20:32:23 +0100136 free(desc);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100137 return 0;
138 }
139
Thierry FOURNIER39bef452014-01-29 13:29:45 +0100140 /* Load map. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200141 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 +0100142 1, err, file, line))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100143 return 0;
Thierry FOURNIER0ffe78c2013-12-05 14:40:25 +0100144
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200145 /* the maps of type IP have a string as defaultvalue. This
146 * string canbe anipv4 or an ipv6, we must convert it.
147 */
148 if (desc->conv->out_type == SMP_T_ADDR) {
149 struct sample_data data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200150 if (!map_parse_ip(arg[1].data.str.area, &data)) {
151 memprintf(err, "map: cannot parse default ip <%s>.",
152 arg[1].data.str.area);
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200153 return 0;
154 }
155 if (data.type == SMP_T_IPV4) {
156 arg[1].type = ARGT_IPV4;
157 arg[1].data.ipv4 = data.u.ipv4;
158 } else {
159 arg[1].type = ARGT_IPV6;
160 arg[1].data.ipv6 = data.u.ipv6;
161 }
162 }
163
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100164 /* replace the first argument by this definition */
165 arg[0].type = ARGT_MAP;
166 arg[0].data.map = desc;
167
168 return 1;
169}
170
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200171static int sample_conv_map(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100172{
173 struct map_descriptor *desc;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100174 struct pattern *pat;
Willy Tarreau83061a82018-07-13 11:56:34 +0200175 struct buffer *str;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100176
177 /* get config */
178 desc = arg_p[0].data.map;
179
180 /* Execute the match function. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100181 pat = pattern_exec_match(&desc->pat, smp, 1);
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100182
183 /* Match case. */
184 if (pat) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200185 if (pat->data) {
Thierry Fournier8feaa662016-02-10 22:55:20 +0100186 /* In the regm case, merge the sample with the input. */
187 if ((long)private == PAT_MATCH_REGM) {
Emeric Brun27102212018-07-17 09:47:07 -0400188 struct buffer *tmptrash;
Willy Tarreau2842e052018-08-22 04:55:43 +0200189 int len;
Emeric Brun27102212018-07-17 09:47:07 -0400190
191 /* Copy the content of the sample because it could
192 be scratched by incoming get_trash_chunk */
193 tmptrash = alloc_trash_chunk();
194 if (!tmptrash)
195 return 0;
196
197 tmptrash->data = smp->data.u.str.data;
198 if (tmptrash->data > (tmptrash->size-1))
199 tmptrash->data = tmptrash->size-1;
200
201 memcpy(tmptrash->area, smp->data.u.str.area, tmptrash->data);
202 tmptrash->area[tmptrash->data] = 0;
203
Thierry Fournier8feaa662016-02-10 22:55:20 +0100204 str = get_trash_chunk();
Willy Tarreau2842e052018-08-22 04:55:43 +0200205 len = exp_replace(str->area, str->size,
206 tmptrash->area,
207 pat->data->u.str.area,
208 (regmatch_t *)smp->ctx.a[0]);
209 if (len == -1)
Thierry Fournier8feaa662016-02-10 22:55:20 +0100210 return 0;
Willy Tarreau2842e052018-08-22 04:55:43 +0200211
212 str->data = len;
Thierry Fournier8feaa662016-02-10 22:55:20 +0100213 smp->data.u.str = *str;
214 return 1;
215 }
216 /* Copy sample. */
Thierry FOURNIER5cc18d42015-08-19 09:02:36 +0200217 smp->data = *pat->data;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100218 smp->flags |= SMP_F_CONST;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100219 return 1;
220 }
221
222 /* Return just int sample containing 1. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200223 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200224 smp->data.u.sint = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100225 return 1;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100226 }
227
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100228 /* If no default value avalaible, the converter fails. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100229 if (arg_p[1].type == ARGT_STOP)
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100230 return 0;
231
232 /* Return the default value. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100233 switch (desc->conv->out_type) {
234
235 case SMP_T_STR:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200236 smp->data.type = SMP_T_STR;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100237 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200238 smp->data.u.str = arg_p[1].data.str;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100239 break;
240
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200241 case SMP_T_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200242 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200243 smp->data.u.sint = arg_p[1].data.sint;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100244 break;
245
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200246 case SMP_T_ADDR:
247 if (arg_p[1].type == ARGT_IPV4) {
248 smp->data.type = SMP_T_IPV4;
249 smp->data.u.ipv4 = arg_p[1].data.ipv4;
250 } else {
251 smp->data.type = SMP_T_IPV6;
252 smp->data.u.ipv6 = arg_p[1].data.ipv6;
253 }
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100254 break;
255 }
256
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100257 return 1;
258}
259
William Lallemandad8be612016-11-18 19:26:17 +0100260/* This function is used with map and acl management. It permits to browse
261 * each reference. The variable <getnext> must contain the current node,
262 * <end> point to the root node and the <flags> permit to filter required
263 * nodes.
264 */
265static inline
266struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
267 unsigned int flags)
268{
269 struct pat_ref *ref = getnext;
270
271 while (1) {
272
273 /* Get next list entry. */
274 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
275
276 /* If the entry is the last of the list, return NULL. */
277 if (&ref->list == end)
278 return NULL;
279
280 /* If the entry match the flag, return it. */
281 if (ref->flags & flags)
282 return ref;
283 }
284}
285
286static inline
287struct pat_ref *pat_ref_lookup_ref(const char *reference)
288{
289 int id;
290 char *error;
291
292 /* If the reference starts by a '#', this is numeric id. */
293 if (reference[0] == '#') {
294 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
295 id = strtol(reference + 1, &error, 10);
296 if (*error != '\0')
297 return NULL;
298
299 /* Perform the unique id lookup. */
300 return pat_ref_lookupid(id);
301 }
302
303 /* Perform the string lookup. */
304 return pat_ref_lookup(reference);
305}
306
307/* This function is used with map and acl management. It permits to browse
308 * each reference.
309 */
310static inline
311struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
312{
313 struct pattern_expr *expr;
314 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
315 if (&expr->list == end)
316 return NULL;
317 return expr;
318}
319
320static int cli_io_handler_pat_list(struct appctx *appctx)
321{
322 struct stream_interface *si = appctx->owner;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200323 struct pat_ref_elt *elt;
324
325 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
326 /* If we're forced to shut down, we might have to remove our
327 * reference to the last ref_elt being dumped.
328 */
329 if (appctx->st2 == STAT_ST_LIST) {
Dragan Dosen336a11f2018-05-04 16:27:15 +0200330 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users)) {
331 LIST_DEL(&appctx->ctx.map.bref.users);
332 LIST_INIT(&appctx->ctx.map.bref.users);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200333 }
334 }
335 return 1;
336 }
William Lallemandad8be612016-11-18 19:26:17 +0100337
338 switch (appctx->st2) {
339
340 case STAT_ST_INIT:
Emeric Brun8d85aa42017-06-29 15:40:33 +0200341 /* the function had not been called yet, let's prepare the
342 * buffer for a response. We initialize the current stream
343 * pointer to the first in the global list. When a target
344 * stream is being destroyed, it is responsible for updating
345 * this pointer. We know we have reached the end when this
346 * pointer points back to the head of the streams list.
347 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100348 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200349 LIST_INIT(&appctx->ctx.map.bref.users);
350 appctx->ctx.map.bref.ref = appctx->ctx.map.ref->head.n;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100351 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100352 appctx->st2 = STAT_ST_LIST;
353 /* fall through */
354
355 case STAT_ST_LIST:
Emeric Brunb5997f72017-07-03 11:34:05 +0200356
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100357 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200358
Emeric Brun8d85aa42017-06-29 15:40:33 +0200359 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users)) {
360 LIST_DEL(&appctx->ctx.map.bref.users);
361 LIST_INIT(&appctx->ctx.map.bref.users);
362 }
363
364 while (appctx->ctx.map.bref.ref != &appctx->ctx.map.ref->head) {
William Lallemandad8be612016-11-18 19:26:17 +0100365 chunk_reset(&trash);
366
Emeric Brun8d85aa42017-06-29 15:40:33 +0200367 elt = LIST_ELEM(appctx->ctx.map.bref.ref, struct pat_ref_elt *, list);
368
William Lallemandad8be612016-11-18 19:26:17 +0100369 /* build messages */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200370 if (elt->sample)
William Lallemandad8be612016-11-18 19:26:17 +0100371 chunk_appendf(&trash, "%p %s %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200372 elt, elt->pattern,
373 elt->sample);
William Lallemandad8be612016-11-18 19:26:17 +0100374 else
375 chunk_appendf(&trash, "%p %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200376 elt, elt->pattern);
William Lallemandad8be612016-11-18 19:26:17 +0100377
Willy Tarreau06d80a92017-10-19 14:32:15 +0200378 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100379 /* let's try again later from this stream. We add ourselves into
380 * this stream's users so that it can remove us upon termination.
381 */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200382 LIST_ADDQ(&elt->back_refs, &appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100383 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200384 si_applet_cant_put(si);
William Lallemandad8be612016-11-18 19:26:17 +0100385 return 0;
386 }
387
388 /* get next list entry and check the end of the list */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200389 appctx->ctx.map.bref.ref = elt->list.n;
William Lallemandad8be612016-11-18 19:26:17 +0100390 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100391 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100392 /* fall through */
393
394 default:
395 appctx->st2 = STAT_ST_FIN;
396 return 1;
397 }
398}
399
400static int cli_io_handler_pats_list(struct appctx *appctx)
401{
402 struct stream_interface *si = appctx->owner;
403
404 switch (appctx->st2) {
405 case STAT_ST_INIT:
406 /* Display the column headers. If the message cannot be sent,
407 * quit the fucntion with returning 0. The function is called
408 * later and restart at the state "STAT_ST_INIT".
409 */
410 chunk_reset(&trash);
411 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +0200412 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100413 si_applet_cant_put(si);
414 return 0;
415 }
416
417 /* Now, we start the browsing of the references lists.
418 * Note that the following call to LIST_ELEM return bad pointer. The only
419 * available field of this pointer is <list>. It is used with the function
420 * pat_list_get_next() for retruning the first available entry
421 */
422 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
423 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
424 appctx->ctx.map.display_flags);
425 appctx->st2 = STAT_ST_LIST;
426 /* fall through */
427
428 case STAT_ST_LIST:
429 while (appctx->ctx.map.ref) {
430 chunk_reset(&trash);
431
432 /* Build messages. If the reference is used by another category than
433 * the listed categorie, display the information in the massage.
434 */
435 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
436 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
437 appctx->ctx.map.ref->display);
438
Willy Tarreau06d80a92017-10-19 14:32:15 +0200439 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100440 /* let's try again later from this stream. We add ourselves into
441 * this stream's users so that it can remove us upon termination.
442 */
443 si_applet_cant_put(si);
444 return 0;
445 }
446
447 /* get next list entry and check the end of the list */
448 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
449 appctx->ctx.map.display_flags);
450 }
451
William Lallemandad8be612016-11-18 19:26:17 +0100452 /* fall through */
453
454 default:
455 appctx->st2 = STAT_ST_FIN;
456 return 1;
457 }
458 return 0;
459}
460
461static int cli_io_handler_map_lookup(struct appctx *appctx)
462{
463 struct stream_interface *si = appctx->owner;
464 struct sample sample;
465 struct pattern *pat;
466 int match_method;
467
468 switch (appctx->st2) {
469 case STAT_ST_INIT:
470 /* Init to the first entry. The list cannot be change */
471 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
472 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
473 appctx->st2 = STAT_ST_LIST;
474 /* fall through */
475
476 case STAT_ST_LIST:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100477 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100478 /* for each lookup type */
479 while (appctx->ctx.map.expr) {
480 /* initialise chunk to build new message */
481 chunk_reset(&trash);
482
483 /* execute pattern matching */
484 sample.data.type = SMP_T_STR;
485 sample.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200486 sample.data.u.str.data = appctx->ctx.map.chunk.data;
487 sample.data.u.str.area = appctx->ctx.map.chunk.area;
Emeric Brunb5997f72017-07-03 11:34:05 +0200488
William Lallemandad8be612016-11-18 19:26:17 +0100489 if (appctx->ctx.map.expr->pat_head->match &&
490 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
491 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
492 else
493 pat = NULL;
494
495 /* build return message: set type of match */
496 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
497 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
498 break;
499 if (match_method >= PAT_MATCH_NUM)
500 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
501 else
502 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
503
504 /* case sensitive */
505 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
506 chunk_appendf(&trash, ", case=insensitive");
507 else
508 chunk_appendf(&trash, ", case=sensitive");
509
510 /* Display no match, and set default value */
511 if (!pat) {
512 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
513 chunk_appendf(&trash, ", found=no");
514 else
515 chunk_appendf(&trash, ", match=no");
516 }
517
518 /* Display match and match info */
519 else {
520 /* display match */
521 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
522 chunk_appendf(&trash, ", found=yes");
523 else
524 chunk_appendf(&trash, ", match=yes");
525
526 /* display index mode */
527 if (pat->sflags & PAT_SF_TREE)
528 chunk_appendf(&trash, ", idx=tree");
529 else
530 chunk_appendf(&trash, ", idx=list");
531
532 /* display pattern */
533 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
534 if (pat->ref && pat->ref->pattern)
535 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
536 else
537 chunk_appendf(&trash, ", key=unknown");
538 }
539 else {
540 if (pat->ref && pat->ref->pattern)
541 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
542 else
543 chunk_appendf(&trash, ", pattern=unknown");
544 }
545
546 /* display return value */
547 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
548 if (pat->data && pat->ref && pat->ref->sample)
549 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
550 smp_to_type[pat->data->type]);
551 else
552 chunk_appendf(&trash, ", value=none");
553 }
554 }
555
556 chunk_appendf(&trash, "\n");
557
558 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200559 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100560 /* let's try again later from this stream. We add ourselves into
561 * this stream's users so that it can remove us upon termination.
562 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100563 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100564 si_applet_cant_put(si);
565 return 0;
566 }
567
568 /* get next entry */
569 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
570 &appctx->ctx.map.ref->pat);
571 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100572 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100573 /* fall through */
574
575 default:
576 appctx->st2 = STAT_ST_FIN;
William Lallemandad8be612016-11-18 19:26:17 +0100577 return 1;
578 }
579}
580
581static void cli_release_mlook(struct appctx *appctx)
582{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200583 free(appctx->ctx.map.chunk.area);
584 appctx->ctx.map.chunk.area = NULL;
William Lallemandad8be612016-11-18 19:26:17 +0100585}
586
587
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200588static int cli_parse_get_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100589{
590 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
591 /* Set flags. */
592 if (args[1][0] == 'm')
593 appctx->ctx.map.display_flags = PAT_REF_MAP;
594 else
595 appctx->ctx.map.display_flags = PAT_REF_ACL;
596
597 /* No parameter. */
598 if (!*args[2] || !*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200599 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
600 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100601 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200602 }
603 else {
604 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100605 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200606 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100607 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100608 return 1;
609 }
610
611 /* lookup into the maps */
612 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
613 if (!appctx->ctx.map.ref) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200614 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
615 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100616 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200617 }
618 else {
619 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100620 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200621 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100622 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100623 return 1;
624 }
625
626 /* copy input string. The string must be allocated because
627 * it may be used over multiple iterations. It's released
628 * at the end and upon abort anyway.
629 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200630 appctx->ctx.map.chunk.data = strlen(args[3]);
631 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.data + 1;
632 appctx->ctx.map.chunk.area = strdup(args[3]);
633 if (!appctx->ctx.map.chunk.area) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200634 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100635 appctx->ctx.cli.msg = "Out of memory error.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100636 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100637 return 1;
638 }
639
640 return 0;
641 }
642 return 1;
643}
644
Emeric Brun8d85aa42017-06-29 15:40:33 +0200645static void cli_release_show_map(struct appctx *appctx)
646{
647 if (appctx->st2 == STAT_ST_LIST) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100648 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200649 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users))
650 LIST_DEL(&appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100651 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200652 }
653}
654
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200655static int cli_parse_show_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100656{
657 if (strcmp(args[1], "map") == 0 ||
658 strcmp(args[1], "acl") == 0) {
659
660 /* Set ACL or MAP flags. */
661 if (args[1][0] == 'm')
662 appctx->ctx.map.display_flags = PAT_REF_MAP;
663 else
664 appctx->ctx.map.display_flags = PAT_REF_ACL;
665
666 /* no parameter: display all map available */
667 if (!*args[2]) {
William Lallemandad8be612016-11-18 19:26:17 +0100668 appctx->io_handler = cli_io_handler_pats_list;
669 return 0;
670 }
671
672 /* lookup into the refs and check the map flag */
673 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
674 if (!appctx->ctx.map.ref ||
675 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200676 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
677 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100678 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200679 }
680 else {
681 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100682 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200683 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100684 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100685 return 1;
686 }
William Lallemandad8be612016-11-18 19:26:17 +0100687 appctx->io_handler = cli_io_handler_pat_list;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200688 appctx->io_release = cli_release_show_map;
William Lallemandad8be612016-11-18 19:26:17 +0100689 return 0;
690 }
691
692 return 0;
693}
694
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200695static int cli_parse_set_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100696{
697 if (strcmp(args[1], "map") == 0) {
698 char *err;
699
700 /* Set flags. */
701 appctx->ctx.map.display_flags = PAT_REF_MAP;
702
703 /* Expect three parameters: map name, key and new value. */
704 if (!*args[2] || !*args[3] || !*args[4]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200705 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100706 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100707 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100708 return 1;
709 }
710
711 /* Lookup the reference in the maps. */
712 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
713 if (!appctx->ctx.map.ref) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200714 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100715 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100716 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100717 return 1;
718 }
719
720 /* If the entry identifier start with a '#', it is considered as
721 * pointer id
722 */
723 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
724 struct pat_ref_elt *ref;
725 long long int conv;
726 char *error;
727
728 /* Convert argument to integer value. */
729 conv = strtoll(&args[3][1], &error, 16);
730 if (*error != '\0') {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200731 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100732 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100733 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100734 return 1;
735 }
736
737 /* Convert and check integer to pointer. */
738 ref = (struct pat_ref_elt *)(long)conv;
739 if ((long long int)(long)ref != conv) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200740 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100741 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100742 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100743 return 1;
744 }
745
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200746 /* Try to modify the entry. */
William Lallemandad8be612016-11-18 19:26:17 +0100747 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100748 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100749 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100750 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200751 if (err) {
William Lallemandad8be612016-11-18 19:26:17 +0100752 memprintf(&err, "%s.\n", err);
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200753 appctx->ctx.cli.err = err;
754 appctx->st0 = CLI_ST_PRINT_FREE;
755 }
756 else {
757 appctx->ctx.cli.severity = LOG_ERR;
758 appctx->ctx.cli.msg = "Failed to update an entry.\n";
759 appctx->st0 = CLI_ST_PRINT;
760 }
William Lallemandad8be612016-11-18 19:26:17 +0100761 return 1;
762 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100763 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100764 }
765 else {
766 /* Else, use the entry identifier as pattern
767 * string, and update the value.
768 */
769 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100770 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100771 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100772 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200773 if (err) {
William Lallemandad8be612016-11-18 19:26:17 +0100774 memprintf(&err, "%s.\n", err);
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200775 appctx->ctx.cli.err = err;
776 appctx->st0 = CLI_ST_PRINT_FREE;
777 }
778 else {
779 appctx->ctx.cli.severity = LOG_ERR;
780 appctx->ctx.cli.msg = "Failed to update an entry.\n";
781 appctx->st0 = CLI_ST_PRINT;
782 }
William Lallemandad8be612016-11-18 19:26:17 +0100783 return 1;
784 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100785 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100786 }
787
788 /* The set is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100789 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100790 return 0;
791 }
792 return 1;
793}
794
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200795static int map_add_key_value(struct appctx *appctx, const char *key, const char *value, char **err)
796{
797 int ret;
798
799 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
800 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
801 ret = pat_ref_add(appctx->ctx.map.ref, key, value, err);
802 else
803 ret = pat_ref_add(appctx->ctx.map.ref, key, NULL, err);
804 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
805
806 return ret;
807}
808
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200809static int cli_parse_add_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100810{
811 if (strcmp(args[1], "map") == 0 ||
812 strcmp(args[1], "acl") == 0) {
813 int ret;
814 char *err;
815
816 /* Set flags. */
817 if (args[1][0] == 'm')
818 appctx->ctx.map.display_flags = PAT_REF_MAP;
819 else
820 appctx->ctx.map.display_flags = PAT_REF_ACL;
821
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200822 /* If the keyword is "map", we expect:
823 * - three parameters if there is no payload
824 * - one parameter if there is a payload
825 * If it is "acl", we expect only two parameters
William Lallemandad8be612016-11-18 19:26:17 +0100826 */
827 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200828 if ((!payload && (!*args[2] || !*args[3] || !*args[4])) ||
829 (payload && !*args[2])) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200830 appctx->ctx.cli.severity = LOG_ERR;
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200831 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 +0100832 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100833 return 1;
834 }
835 }
836 else {
837 if (!*args[2] || !*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200838 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100839 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100840 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100841 return 1;
842 }
843 }
844
845 /* Lookup for the reference. */
846 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
847 if (!appctx->ctx.map.ref) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200848 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
849 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100850 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200851 }
852 else {
853 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100854 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200855 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100856 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100857 return 1;
858 }
859
860 /* The command "add acl" is prohibited if the reference
861 * use samples.
862 */
863 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
864 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200865 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100866 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
867 "You must use the command 'add map' to add values.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100868 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100869 return 1;
870 }
871
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200872 /* Add value(s). */
William Lallemandad8be612016-11-18 19:26:17 +0100873 err = NULL;
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200874 if (!payload) {
875 ret = map_add_key_value(appctx, args[3], args[4], &err);
876 if (!ret) {
877 if (err) {
878 memprintf(&err, "%s.\n", err);
879 appctx->ctx.cli.err = err;
880 appctx->st0 = CLI_ST_PRINT_FREE;
881 }
882 else {
883 appctx->ctx.cli.severity = LOG_ERR;
884 appctx->ctx.cli.msg = "Failed to add an entry.\n";
885 appctx->st0 = CLI_ST_PRINT;
886 }
887 return 1;
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200888 }
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200889 }
890 else {
891 const char *end = payload + strlen(payload);
892
893 while (payload < end) {
894 char *key, *value;
895 size_t l;
896
897 /* key */
898 key = payload;
899 l = strcspn(key, " \t");
900 payload += l;
901
902 if (!*payload && appctx->ctx.map.display_flags == PAT_REF_MAP) {
903 memprintf(&err, "Missing value for key '%s'.\n", key);
904 appctx->ctx.cli.err = err;
905 appctx->st0 = CLI_ST_PRINT_FREE;
906 return 1;
907 }
908 key[l] = 0;
909 payload++;
910
911 /* value */
912 payload += strspn(payload, " \t");
913 value = payload;
914 l = strcspn(value, "\n");
915 payload += l;
916 if (*payload)
917 payload++;
918 value[l] = 0;
919
920 ret = map_add_key_value(appctx, key, value, &err);
921 if (!ret) {
922 if (err) {
923 memprintf(&err, "%s.\n", err);
924 appctx->ctx.cli.err = err;
925 appctx->st0 = CLI_ST_PRINT_FREE;
926 }
927 else {
928 appctx->ctx.cli.severity = LOG_ERR;
929 appctx->ctx.cli.msg = "Failed to add a key.\n";
930 appctx->st0 = CLI_ST_PRINT;
931 }
932 return 1;
933 }
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200934 }
William Lallemandad8be612016-11-18 19:26:17 +0100935 }
936
937 /* The add is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100938 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100939 return 1;
940 }
941
942 return 0;
943}
944
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200945static int cli_parse_del_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100946{
947 if (args[1][0] == 'm')
948 appctx->ctx.map.display_flags = PAT_REF_MAP;
949 else
950 appctx->ctx.map.display_flags = PAT_REF_ACL;
951
952 /* Expect two parameters: map name and key. */
953 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
954 if (!*args[2] || !*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200955 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100956 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100957 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100958 return 1;
959 }
960 }
961
962 else {
963 if (!*args[2] || !*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200964 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100965 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100966 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100967 return 1;
968 }
969 }
970
971 /* Lookup the reference in the maps. */
972 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
973 if (!appctx->ctx.map.ref ||
974 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200975 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100976 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100977 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100978 return 1;
979 }
980
981 /* If the entry identifier start with a '#', it is considered as
982 * pointer id
983 */
984 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
985 struct pat_ref_elt *ref;
986 long long int conv;
987 char *error;
988
989 /* Convert argument to integer value. */
990 conv = strtoll(&args[3][1], &error, 16);
991 if (*error != '\0') {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200992 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100993 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100994 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100995 return 1;
996 }
997
998 /* Convert and check integer to pointer. */
999 ref = (struct pat_ref_elt *)(long)conv;
1000 if ((long long int)(long)ref != conv) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001001 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001002 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001003 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001004 return 1;
1005 }
1006
1007 /* Try to delete the entry. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001008 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001009 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001010 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001011 /* The entry is not found, send message. */
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001012 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001013 appctx->ctx.cli.msg = "Key not found.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001014 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001015 return 1;
1016 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001017 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001018 }
1019 else {
1020 /* Else, use the entry identifier as pattern
1021 * string and try to delete the entry.
1022 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001023 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001024 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001025 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001026 /* The entry is not found, send message. */
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001027 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001028 appctx->ctx.cli.msg = "Key not found.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001029 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001030 return 1;
1031 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001032 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001033 }
1034
1035 /* The deletion is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001036 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +01001037 return 1;
1038}
1039
1040
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001041static int cli_parse_clear_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +01001042{
1043 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1044 /* Set ACL or MAP flags. */
1045 if (args[1][0] == 'm')
1046 appctx->ctx.map.display_flags = PAT_REF_MAP;
1047 else
1048 appctx->ctx.map.display_flags = PAT_REF_ACL;
1049
1050 /* no parameter */
1051 if (!*args[2]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001052 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
1053 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001054 appctx->ctx.cli.msg = "Missing map identifier.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001055 }
1056 else {
1057 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001058 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001059 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001060 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001061 return 1;
1062 }
1063
1064 /* lookup into the refs and check the map flag */
1065 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
1066 if (!appctx->ctx.map.ref ||
1067 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001068 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
1069 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001070 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001071 }
1072 else {
1073 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001074 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001075 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001076 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001077 return 1;
1078 }
1079
1080 /* Clear all. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001081 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001082 pat_ref_prune(appctx->ctx.map.ref);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001083 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001084
1085 /* return response */
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001086 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +01001087 return 1;
1088 }
1089 return 0;
1090}
1091
1092/* register cli keywords */
1093
1094static struct cli_kw_list cli_kws = {{ },{
1095 { { "add", "acl", NULL }, "add acl : add acl entry", cli_parse_add_map, NULL },
1096 { { "clear", "acl", NULL }, "clear acl <id> : clear the content of this acl", cli_parse_clear_map, NULL },
1097 { { "del", "acl", NULL }, "del acl : delete acl entry", cli_parse_del_map, NULL },
1098 { { "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 },
1099 { { "show", "acl", NULL }, "show acl [id] : report available acls or dump an acl's contents", cli_parse_show_map, NULL },
1100 { { "add", "map", NULL }, "add map : add map entry", cli_parse_add_map, NULL },
1101 { { "clear", "map", NULL }, "clear map <id> : clear the content of this map", cli_parse_clear_map, NULL },
1102 { { "del", "map", NULL }, "del map : delete map entry", cli_parse_del_map, NULL },
Nenad Merdanovic96c15712017-03-12 22:01:36 +01001103 { { "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 +01001104 { { "set", "map", NULL }, "set map : modify map entry", cli_parse_set_map, NULL },
1105 { { "show", "map", NULL }, "show map [id] : report available maps or dump a map's contents", cli_parse_show_map, NULL },
1106 { { NULL }, NULL, NULL, NULL }
1107}};
1108
1109
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +01001110/* Note: must not be declared <const> as its list will be overwritten
1111 *
1112 * For the map_*_int keywords, the output is declared as SMP_T_UINT, but the converter function
1113 * can provide SMP_T_UINT, SMP_T_SINT or SMP_T_BOOL depending on how the patterns found in the
1114 * file can be parsed.
1115 *
1116 * For the map_*_ip keyword, the output is declared as SMP_T_IPV4, but the converter function
1117 * can provide SMP_T_IPV4 or SMP_T_IPV6 depending on the patterns found in the file.
1118 *
1119 * The map_* keywords only emit strings.
1120 *
1121 * The output type is only used during the configuration parsing. It is used for detecting
1122 * compatibility problems.
1123 *
1124 * The arguments are: <file>[,<default value>]
1125 */
1126static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER1edc9712014-12-15 16:18:39 +01001127 { "map", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1128 { "map_str", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1129 { "map_beg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_BEG },
1130 { "map_sub", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_SUB },
1131 { "map_dir", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DIR },
1132 { "map_dom", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DOM },
1133 { "map_end", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_END },
1134 { "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 +01001135 { "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 +02001136 { "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 +01001137 { "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 +01001138
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001139 { "map_str_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_STR },
1140 { "map_beg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_BEG },
1141 { "map_sub_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_SUB },
1142 { "map_dir_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DIR },
1143 { "map_dom_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DOM },
1144 { "map_end_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_END },
1145 { "map_reg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_REG },
1146 { "map_int_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_SINT, SMP_T_SINT, (void *)PAT_MATCH_INT },
1147 { "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 +01001148
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +02001149 { "map_str_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_STR },
1150 { "map_beg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_BEG },
1151 { "map_sub_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_SUB },
1152 { "map_dir_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DIR },
1153 { "map_dom_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DOM },
1154 { "map_end_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_END },
1155 { "map_reg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_REG },
1156 { "map_int_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_SINT, SMP_T_ADDR, (void *)PAT_MATCH_INT },
1157 { "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 +01001158
1159 { /* END */ },
1160}};
1161
1162__attribute__((constructor))
1163static void __map_init(void)
1164{
1165 /* register format conversion keywords */
1166 sample_register_convs(&sample_conv_kws);
William Lallemandad8be612016-11-18 19:26:17 +01001167 cli_register_kw(&cli_kws);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +01001168}