blob: d2df22e21212e0ce02577217e06cc86587fd2386 [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;
189
190 /* Copy the content of the sample because it could
191 be scratched by incoming get_trash_chunk */
192 tmptrash = alloc_trash_chunk();
193 if (!tmptrash)
194 return 0;
195
196 tmptrash->data = smp->data.u.str.data;
197 if (tmptrash->data > (tmptrash->size-1))
198 tmptrash->data = tmptrash->size-1;
199
200 memcpy(tmptrash->area, smp->data.u.str.area, tmptrash->data);
201 tmptrash->area[tmptrash->data] = 0;
202
Thierry Fournier8feaa662016-02-10 22:55:20 +0100203 str = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200204 str->data = exp_replace(str->area, str->size,
Emeric Brun27102212018-07-17 09:47:07 -0400205 tmptrash->area,
206 pat->data->u.str.area,
207 (regmatch_t *)smp->ctx.a[0]);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200208 if (str->data == -1)
Thierry Fournier8feaa662016-02-10 22:55:20 +0100209 return 0;
210 smp->data.u.str = *str;
211 return 1;
212 }
213 /* Copy sample. */
Thierry FOURNIER5cc18d42015-08-19 09:02:36 +0200214 smp->data = *pat->data;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100215 smp->flags |= SMP_F_CONST;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100216 return 1;
217 }
218
219 /* Return just int sample containing 1. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200220 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200221 smp->data.u.sint = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100222 return 1;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100223 }
224
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100225 /* If no default value avalaible, the converter fails. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100226 if (arg_p[1].type == ARGT_STOP)
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100227 return 0;
228
229 /* Return the default value. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100230 switch (desc->conv->out_type) {
231
232 case SMP_T_STR:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200233 smp->data.type = SMP_T_STR;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100234 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200235 smp->data.u.str = arg_p[1].data.str;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100236 break;
237
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200238 case SMP_T_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200239 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200240 smp->data.u.sint = arg_p[1].data.sint;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100241 break;
242
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200243 case SMP_T_ADDR:
244 if (arg_p[1].type == ARGT_IPV4) {
245 smp->data.type = SMP_T_IPV4;
246 smp->data.u.ipv4 = arg_p[1].data.ipv4;
247 } else {
248 smp->data.type = SMP_T_IPV6;
249 smp->data.u.ipv6 = arg_p[1].data.ipv6;
250 }
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100251 break;
252 }
253
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100254 return 1;
255}
256
William Lallemandad8be612016-11-18 19:26:17 +0100257/* This function is used with map and acl management. It permits to browse
258 * each reference. The variable <getnext> must contain the current node,
259 * <end> point to the root node and the <flags> permit to filter required
260 * nodes.
261 */
262static inline
263struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
264 unsigned int flags)
265{
266 struct pat_ref *ref = getnext;
267
268 while (1) {
269
270 /* Get next list entry. */
271 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
272
273 /* If the entry is the last of the list, return NULL. */
274 if (&ref->list == end)
275 return NULL;
276
277 /* If the entry match the flag, return it. */
278 if (ref->flags & flags)
279 return ref;
280 }
281}
282
283static inline
284struct pat_ref *pat_ref_lookup_ref(const char *reference)
285{
286 int id;
287 char *error;
288
289 /* If the reference starts by a '#', this is numeric id. */
290 if (reference[0] == '#') {
291 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
292 id = strtol(reference + 1, &error, 10);
293 if (*error != '\0')
294 return NULL;
295
296 /* Perform the unique id lookup. */
297 return pat_ref_lookupid(id);
298 }
299
300 /* Perform the string lookup. */
301 return pat_ref_lookup(reference);
302}
303
304/* This function is used with map and acl management. It permits to browse
305 * each reference.
306 */
307static inline
308struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
309{
310 struct pattern_expr *expr;
311 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
312 if (&expr->list == end)
313 return NULL;
314 return expr;
315}
316
317static int cli_io_handler_pat_list(struct appctx *appctx)
318{
319 struct stream_interface *si = appctx->owner;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200320 struct pat_ref_elt *elt;
321
322 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
323 /* If we're forced to shut down, we might have to remove our
324 * reference to the last ref_elt being dumped.
325 */
326 if (appctx->st2 == STAT_ST_LIST) {
Dragan Dosen336a11f2018-05-04 16:27:15 +0200327 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users)) {
328 LIST_DEL(&appctx->ctx.map.bref.users);
329 LIST_INIT(&appctx->ctx.map.bref.users);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200330 }
331 }
332 return 1;
333 }
William Lallemandad8be612016-11-18 19:26:17 +0100334
335 switch (appctx->st2) {
336
337 case STAT_ST_INIT:
Emeric Brun8d85aa42017-06-29 15:40:33 +0200338 /* the function had not been called yet, let's prepare the
339 * buffer for a response. We initialize the current stream
340 * pointer to the first in the global list. When a target
341 * stream is being destroyed, it is responsible for updating
342 * this pointer. We know we have reached the end when this
343 * pointer points back to the head of the streams list.
344 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100345 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200346 LIST_INIT(&appctx->ctx.map.bref.users);
347 appctx->ctx.map.bref.ref = appctx->ctx.map.ref->head.n;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100348 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100349 appctx->st2 = STAT_ST_LIST;
350 /* fall through */
351
352 case STAT_ST_LIST:
Emeric Brunb5997f72017-07-03 11:34:05 +0200353
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100354 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200355
Emeric Brun8d85aa42017-06-29 15:40:33 +0200356 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users)) {
357 LIST_DEL(&appctx->ctx.map.bref.users);
358 LIST_INIT(&appctx->ctx.map.bref.users);
359 }
360
361 while (appctx->ctx.map.bref.ref != &appctx->ctx.map.ref->head) {
William Lallemandad8be612016-11-18 19:26:17 +0100362 chunk_reset(&trash);
363
Emeric Brun8d85aa42017-06-29 15:40:33 +0200364 elt = LIST_ELEM(appctx->ctx.map.bref.ref, struct pat_ref_elt *, list);
365
William Lallemandad8be612016-11-18 19:26:17 +0100366 /* build messages */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200367 if (elt->sample)
William Lallemandad8be612016-11-18 19:26:17 +0100368 chunk_appendf(&trash, "%p %s %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200369 elt, elt->pattern,
370 elt->sample);
William Lallemandad8be612016-11-18 19:26:17 +0100371 else
372 chunk_appendf(&trash, "%p %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200373 elt, elt->pattern);
William Lallemandad8be612016-11-18 19:26:17 +0100374
Willy Tarreau06d80a92017-10-19 14:32:15 +0200375 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100376 /* let's try again later from this stream. We add ourselves into
377 * this stream's users so that it can remove us upon termination.
378 */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200379 LIST_ADDQ(&elt->back_refs, &appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100380 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200381 si_applet_cant_put(si);
William Lallemandad8be612016-11-18 19:26:17 +0100382 return 0;
383 }
384
385 /* get next list entry and check the end of the list */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200386 appctx->ctx.map.bref.ref = elt->list.n;
William Lallemandad8be612016-11-18 19:26:17 +0100387 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100388 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100389 /* fall through */
390
391 default:
392 appctx->st2 = STAT_ST_FIN;
393 return 1;
394 }
395}
396
397static int cli_io_handler_pats_list(struct appctx *appctx)
398{
399 struct stream_interface *si = appctx->owner;
400
401 switch (appctx->st2) {
402 case STAT_ST_INIT:
403 /* Display the column headers. If the message cannot be sent,
404 * quit the fucntion with returning 0. The function is called
405 * later and restart at the state "STAT_ST_INIT".
406 */
407 chunk_reset(&trash);
408 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +0200409 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100410 si_applet_cant_put(si);
411 return 0;
412 }
413
414 /* Now, we start the browsing of the references lists.
415 * Note that the following call to LIST_ELEM return bad pointer. The only
416 * available field of this pointer is <list>. It is used with the function
417 * pat_list_get_next() for retruning the first available entry
418 */
419 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
420 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
421 appctx->ctx.map.display_flags);
422 appctx->st2 = STAT_ST_LIST;
423 /* fall through */
424
425 case STAT_ST_LIST:
426 while (appctx->ctx.map.ref) {
427 chunk_reset(&trash);
428
429 /* Build messages. If the reference is used by another category than
430 * the listed categorie, display the information in the massage.
431 */
432 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
433 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
434 appctx->ctx.map.ref->display);
435
Willy Tarreau06d80a92017-10-19 14:32:15 +0200436 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100437 /* let's try again later from this stream. We add ourselves into
438 * this stream's users so that it can remove us upon termination.
439 */
440 si_applet_cant_put(si);
441 return 0;
442 }
443
444 /* get next list entry and check the end of the list */
445 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
446 appctx->ctx.map.display_flags);
447 }
448
William Lallemandad8be612016-11-18 19:26:17 +0100449 /* fall through */
450
451 default:
452 appctx->st2 = STAT_ST_FIN;
453 return 1;
454 }
455 return 0;
456}
457
458static int cli_io_handler_map_lookup(struct appctx *appctx)
459{
460 struct stream_interface *si = appctx->owner;
461 struct sample sample;
462 struct pattern *pat;
463 int match_method;
464
465 switch (appctx->st2) {
466 case STAT_ST_INIT:
467 /* Init to the first entry. The list cannot be change */
468 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
469 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
470 appctx->st2 = STAT_ST_LIST;
471 /* fall through */
472
473 case STAT_ST_LIST:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100474 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100475 /* for each lookup type */
476 while (appctx->ctx.map.expr) {
477 /* initialise chunk to build new message */
478 chunk_reset(&trash);
479
480 /* execute pattern matching */
481 sample.data.type = SMP_T_STR;
482 sample.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200483 sample.data.u.str.data = appctx->ctx.map.chunk.data;
484 sample.data.u.str.area = appctx->ctx.map.chunk.area;
Emeric Brunb5997f72017-07-03 11:34:05 +0200485
William Lallemandad8be612016-11-18 19:26:17 +0100486 if (appctx->ctx.map.expr->pat_head->match &&
487 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
488 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
489 else
490 pat = NULL;
491
492 /* build return message: set type of match */
493 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
494 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
495 break;
496 if (match_method >= PAT_MATCH_NUM)
497 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
498 else
499 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
500
501 /* case sensitive */
502 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
503 chunk_appendf(&trash, ", case=insensitive");
504 else
505 chunk_appendf(&trash, ", case=sensitive");
506
507 /* Display no match, and set default value */
508 if (!pat) {
509 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
510 chunk_appendf(&trash, ", found=no");
511 else
512 chunk_appendf(&trash, ", match=no");
513 }
514
515 /* Display match and match info */
516 else {
517 /* display match */
518 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
519 chunk_appendf(&trash, ", found=yes");
520 else
521 chunk_appendf(&trash, ", match=yes");
522
523 /* display index mode */
524 if (pat->sflags & PAT_SF_TREE)
525 chunk_appendf(&trash, ", idx=tree");
526 else
527 chunk_appendf(&trash, ", idx=list");
528
529 /* display pattern */
530 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
531 if (pat->ref && pat->ref->pattern)
532 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
533 else
534 chunk_appendf(&trash, ", key=unknown");
535 }
536 else {
537 if (pat->ref && pat->ref->pattern)
538 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
539 else
540 chunk_appendf(&trash, ", pattern=unknown");
541 }
542
543 /* display return value */
544 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
545 if (pat->data && pat->ref && pat->ref->sample)
546 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
547 smp_to_type[pat->data->type]);
548 else
549 chunk_appendf(&trash, ", value=none");
550 }
551 }
552
553 chunk_appendf(&trash, "\n");
554
555 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200556 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100557 /* let's try again later from this stream. We add ourselves into
558 * this stream's users so that it can remove us upon termination.
559 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100560 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100561 si_applet_cant_put(si);
562 return 0;
563 }
564
565 /* get next entry */
566 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
567 &appctx->ctx.map.ref->pat);
568 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100569 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100570 /* fall through */
571
572 default:
573 appctx->st2 = STAT_ST_FIN;
William Lallemandad8be612016-11-18 19:26:17 +0100574 return 1;
575 }
576}
577
578static void cli_release_mlook(struct appctx *appctx)
579{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200580 free(appctx->ctx.map.chunk.area);
581 appctx->ctx.map.chunk.area = NULL;
William Lallemandad8be612016-11-18 19:26:17 +0100582}
583
584
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200585static int cli_parse_get_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100586{
587 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
588 /* Set flags. */
589 if (args[1][0] == 'm')
590 appctx->ctx.map.display_flags = PAT_REF_MAP;
591 else
592 appctx->ctx.map.display_flags = PAT_REF_ACL;
593
594 /* No parameter. */
595 if (!*args[2] || !*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200596 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
597 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100598 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200599 }
600 else {
601 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100602 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200603 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100604 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100605 return 1;
606 }
607
608 /* lookup into the maps */
609 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
610 if (!appctx->ctx.map.ref) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200611 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
612 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100613 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200614 }
615 else {
616 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100617 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200618 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100619 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100620 return 1;
621 }
622
623 /* copy input string. The string must be allocated because
624 * it may be used over multiple iterations. It's released
625 * at the end and upon abort anyway.
626 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200627 appctx->ctx.map.chunk.data = strlen(args[3]);
628 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.data + 1;
629 appctx->ctx.map.chunk.area = strdup(args[3]);
630 if (!appctx->ctx.map.chunk.area) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200631 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100632 appctx->ctx.cli.msg = "Out of memory error.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100633 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100634 return 1;
635 }
636
637 return 0;
638 }
639 return 1;
640}
641
Emeric Brun8d85aa42017-06-29 15:40:33 +0200642static void cli_release_show_map(struct appctx *appctx)
643{
644 if (appctx->st2 == STAT_ST_LIST) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100645 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200646 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users))
647 LIST_DEL(&appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100648 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200649 }
650}
651
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200652static int cli_parse_show_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100653{
654 if (strcmp(args[1], "map") == 0 ||
655 strcmp(args[1], "acl") == 0) {
656
657 /* Set ACL or MAP flags. */
658 if (args[1][0] == 'm')
659 appctx->ctx.map.display_flags = PAT_REF_MAP;
660 else
661 appctx->ctx.map.display_flags = PAT_REF_ACL;
662
663 /* no parameter: display all map available */
664 if (!*args[2]) {
William Lallemandad8be612016-11-18 19:26:17 +0100665 appctx->io_handler = cli_io_handler_pats_list;
666 return 0;
667 }
668
669 /* lookup into the refs and check the map flag */
670 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
671 if (!appctx->ctx.map.ref ||
672 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200673 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
674 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100675 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200676 }
677 else {
678 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100679 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200680 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100681 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100682 return 1;
683 }
William Lallemandad8be612016-11-18 19:26:17 +0100684 appctx->io_handler = cli_io_handler_pat_list;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200685 appctx->io_release = cli_release_show_map;
William Lallemandad8be612016-11-18 19:26:17 +0100686 return 0;
687 }
688
689 return 0;
690}
691
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200692static int cli_parse_set_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100693{
694 if (strcmp(args[1], "map") == 0) {
695 char *err;
696
697 /* Set flags. */
698 appctx->ctx.map.display_flags = PAT_REF_MAP;
699
700 /* Expect three parameters: map name, key and new value. */
701 if (!*args[2] || !*args[3] || !*args[4]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200702 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100703 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100704 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100705 return 1;
706 }
707
708 /* Lookup the reference in the maps. */
709 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
710 if (!appctx->ctx.map.ref) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200711 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100712 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100713 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100714 return 1;
715 }
716
717 /* If the entry identifier start with a '#', it is considered as
718 * pointer id
719 */
720 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
721 struct pat_ref_elt *ref;
722 long long int conv;
723 char *error;
724
725 /* Convert argument to integer value. */
726 conv = strtoll(&args[3][1], &error, 16);
727 if (*error != '\0') {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200728 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100729 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100730 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100731 return 1;
732 }
733
734 /* Convert and check integer to pointer. */
735 ref = (struct pat_ref_elt *)(long)conv;
736 if ((long long int)(long)ref != conv) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200737 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100738 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100739 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100740 return 1;
741 }
742
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200743 /* Try to modify the entry. */
William Lallemandad8be612016-11-18 19:26:17 +0100744 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100745 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100746 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100747 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200748 if (err) {
William Lallemandad8be612016-11-18 19:26:17 +0100749 memprintf(&err, "%s.\n", err);
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200750 appctx->ctx.cli.err = err;
751 appctx->st0 = CLI_ST_PRINT_FREE;
752 }
753 else {
754 appctx->ctx.cli.severity = LOG_ERR;
755 appctx->ctx.cli.msg = "Failed to update an entry.\n";
756 appctx->st0 = CLI_ST_PRINT;
757 }
William Lallemandad8be612016-11-18 19:26:17 +0100758 return 1;
759 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100760 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100761 }
762 else {
763 /* Else, use the entry identifier as pattern
764 * string, and update the value.
765 */
766 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100767 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100768 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100769 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200770 if (err) {
William Lallemandad8be612016-11-18 19:26:17 +0100771 memprintf(&err, "%s.\n", err);
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200772 appctx->ctx.cli.err = err;
773 appctx->st0 = CLI_ST_PRINT_FREE;
774 }
775 else {
776 appctx->ctx.cli.severity = LOG_ERR;
777 appctx->ctx.cli.msg = "Failed to update an entry.\n";
778 appctx->st0 = CLI_ST_PRINT;
779 }
William Lallemandad8be612016-11-18 19:26:17 +0100780 return 1;
781 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100782 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100783 }
784
785 /* The set is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100786 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100787 return 0;
788 }
789 return 1;
790}
791
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200792static int map_add_key_value(struct appctx *appctx, const char *key, const char *value, char **err)
793{
794 int ret;
795
796 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
797 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
798 ret = pat_ref_add(appctx->ctx.map.ref, key, value, err);
799 else
800 ret = pat_ref_add(appctx->ctx.map.ref, key, NULL, err);
801 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
802
803 return ret;
804}
805
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200806static int cli_parse_add_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100807{
808 if (strcmp(args[1], "map") == 0 ||
809 strcmp(args[1], "acl") == 0) {
810 int ret;
811 char *err;
812
813 /* Set flags. */
814 if (args[1][0] == 'm')
815 appctx->ctx.map.display_flags = PAT_REF_MAP;
816 else
817 appctx->ctx.map.display_flags = PAT_REF_ACL;
818
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200819 /* If the keyword is "map", we expect:
820 * - three parameters if there is no payload
821 * - one parameter if there is a payload
822 * If it is "acl", we expect only two parameters
William Lallemandad8be612016-11-18 19:26:17 +0100823 */
824 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200825 if ((!payload && (!*args[2] || !*args[3] || !*args[4])) ||
826 (payload && !*args[2])) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200827 appctx->ctx.cli.severity = LOG_ERR;
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200828 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 +0100829 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100830 return 1;
831 }
832 }
833 else {
834 if (!*args[2] || !*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200835 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100836 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100837 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100838 return 1;
839 }
840 }
841
842 /* Lookup for the reference. */
843 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
844 if (!appctx->ctx.map.ref) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200845 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
846 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100847 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200848 }
849 else {
850 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100851 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200852 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100853 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100854 return 1;
855 }
856
857 /* The command "add acl" is prohibited if the reference
858 * use samples.
859 */
860 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
861 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200862 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100863 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
864 "You must use the command 'add map' to add values.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100865 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100866 return 1;
867 }
868
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200869 /* Add value(s). */
William Lallemandad8be612016-11-18 19:26:17 +0100870 err = NULL;
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200871 if (!payload) {
872 ret = map_add_key_value(appctx, args[3], args[4], &err);
873 if (!ret) {
874 if (err) {
875 memprintf(&err, "%s.\n", err);
876 appctx->ctx.cli.err = err;
877 appctx->st0 = CLI_ST_PRINT_FREE;
878 }
879 else {
880 appctx->ctx.cli.severity = LOG_ERR;
881 appctx->ctx.cli.msg = "Failed to add an entry.\n";
882 appctx->st0 = CLI_ST_PRINT;
883 }
884 return 1;
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200885 }
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200886 }
887 else {
888 const char *end = payload + strlen(payload);
889
890 while (payload < end) {
891 char *key, *value;
892 size_t l;
893
894 /* key */
895 key = payload;
896 l = strcspn(key, " \t");
897 payload += l;
898
899 if (!*payload && appctx->ctx.map.display_flags == PAT_REF_MAP) {
900 memprintf(&err, "Missing value for key '%s'.\n", key);
901 appctx->ctx.cli.err = err;
902 appctx->st0 = CLI_ST_PRINT_FREE;
903 return 1;
904 }
905 key[l] = 0;
906 payload++;
907
908 /* value */
909 payload += strspn(payload, " \t");
910 value = payload;
911 l = strcspn(value, "\n");
912 payload += l;
913 if (*payload)
914 payload++;
915 value[l] = 0;
916
917 ret = map_add_key_value(appctx, key, value, &err);
918 if (!ret) {
919 if (err) {
920 memprintf(&err, "%s.\n", err);
921 appctx->ctx.cli.err = err;
922 appctx->st0 = CLI_ST_PRINT_FREE;
923 }
924 else {
925 appctx->ctx.cli.severity = LOG_ERR;
926 appctx->ctx.cli.msg = "Failed to add a key.\n";
927 appctx->st0 = CLI_ST_PRINT;
928 }
929 return 1;
930 }
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200931 }
William Lallemandad8be612016-11-18 19:26:17 +0100932 }
933
934 /* The add is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100935 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100936 return 1;
937 }
938
939 return 0;
940}
941
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200942static int cli_parse_del_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100943{
944 if (args[1][0] == 'm')
945 appctx->ctx.map.display_flags = PAT_REF_MAP;
946 else
947 appctx->ctx.map.display_flags = PAT_REF_ACL;
948
949 /* Expect two parameters: map name and key. */
950 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
951 if (!*args[2] || !*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200952 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100953 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100954 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100955 return 1;
956 }
957 }
958
959 else {
960 if (!*args[2] || !*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200961 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100962 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100963 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100964 return 1;
965 }
966 }
967
968 /* Lookup the reference in the maps. */
969 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
970 if (!appctx->ctx.map.ref ||
971 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200972 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100973 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100974 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100975 return 1;
976 }
977
978 /* If the entry identifier start with a '#', it is considered as
979 * pointer id
980 */
981 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
982 struct pat_ref_elt *ref;
983 long long int conv;
984 char *error;
985
986 /* Convert argument to integer value. */
987 conv = strtoll(&args[3][1], &error, 16);
988 if (*error != '\0') {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200989 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100990 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100991 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100992 return 1;
993 }
994
995 /* Convert and check integer to pointer. */
996 ref = (struct pat_ref_elt *)(long)conv;
997 if ((long long int)(long)ref != conv) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200998 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100999 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001000 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001001 return 1;
1002 }
1003
1004 /* Try to delete the entry. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001005 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001006 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001007 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001008 /* The entry is not found, send message. */
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001009 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001010 appctx->ctx.cli.msg = "Key not found.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001011 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001012 return 1;
1013 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001014 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001015 }
1016 else {
1017 /* Else, use the entry identifier as pattern
1018 * string and try to delete the entry.
1019 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001020 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001021 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001022 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001023 /* The entry is not found, send message. */
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001024 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001025 appctx->ctx.cli.msg = "Key not found.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001026 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001027 return 1;
1028 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001029 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001030 }
1031
1032 /* The deletion is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001033 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +01001034 return 1;
1035}
1036
1037
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001038static int cli_parse_clear_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +01001039{
1040 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1041 /* Set ACL or MAP flags. */
1042 if (args[1][0] == 'm')
1043 appctx->ctx.map.display_flags = PAT_REF_MAP;
1044 else
1045 appctx->ctx.map.display_flags = PAT_REF_ACL;
1046
1047 /* no parameter */
1048 if (!*args[2]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001049 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
1050 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001051 appctx->ctx.cli.msg = "Missing map identifier.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001052 }
1053 else {
1054 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001055 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001056 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001057 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001058 return 1;
1059 }
1060
1061 /* lookup into the refs and check the map flag */
1062 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
1063 if (!appctx->ctx.map.ref ||
1064 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001065 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
1066 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001067 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001068 }
1069 else {
1070 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001071 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001072 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001073 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001074 return 1;
1075 }
1076
1077 /* Clear all. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001078 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001079 pat_ref_prune(appctx->ctx.map.ref);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001080 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001081
1082 /* return response */
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001083 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +01001084 return 1;
1085 }
1086 return 0;
1087}
1088
1089/* register cli keywords */
1090
1091static struct cli_kw_list cli_kws = {{ },{
1092 { { "add", "acl", NULL }, "add acl : add acl entry", cli_parse_add_map, NULL },
1093 { { "clear", "acl", NULL }, "clear acl <id> : clear the content of this acl", cli_parse_clear_map, NULL },
1094 { { "del", "acl", NULL }, "del acl : delete acl entry", cli_parse_del_map, NULL },
1095 { { "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 },
1096 { { "show", "acl", NULL }, "show acl [id] : report available acls or dump an acl's contents", cli_parse_show_map, NULL },
1097 { { "add", "map", NULL }, "add map : add map entry", cli_parse_add_map, NULL },
1098 { { "clear", "map", NULL }, "clear map <id> : clear the content of this map", cli_parse_clear_map, NULL },
1099 { { "del", "map", NULL }, "del map : delete map entry", cli_parse_del_map, NULL },
Nenad Merdanovic96c15712017-03-12 22:01:36 +01001100 { { "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 +01001101 { { "set", "map", NULL }, "set map : modify map entry", cli_parse_set_map, NULL },
1102 { { "show", "map", NULL }, "show map [id] : report available maps or dump a map's contents", cli_parse_show_map, NULL },
1103 { { NULL }, NULL, NULL, NULL }
1104}};
1105
1106
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +01001107/* Note: must not be declared <const> as its list will be overwritten
1108 *
1109 * For the map_*_int keywords, the output is declared as SMP_T_UINT, but the converter function
1110 * can provide SMP_T_UINT, SMP_T_SINT or SMP_T_BOOL depending on how the patterns found in the
1111 * file can be parsed.
1112 *
1113 * For the map_*_ip keyword, the output is declared as SMP_T_IPV4, but the converter function
1114 * can provide SMP_T_IPV4 or SMP_T_IPV6 depending on the patterns found in the file.
1115 *
1116 * The map_* keywords only emit strings.
1117 *
1118 * The output type is only used during the configuration parsing. It is used for detecting
1119 * compatibility problems.
1120 *
1121 * The arguments are: <file>[,<default value>]
1122 */
1123static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER1edc9712014-12-15 16:18:39 +01001124 { "map", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1125 { "map_str", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1126 { "map_beg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_BEG },
1127 { "map_sub", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_SUB },
1128 { "map_dir", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DIR },
1129 { "map_dom", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DOM },
1130 { "map_end", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_END },
1131 { "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 +01001132 { "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 +02001133 { "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 +01001134 { "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 +01001135
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001136 { "map_str_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_STR },
1137 { "map_beg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_BEG },
1138 { "map_sub_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_SUB },
1139 { "map_dir_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DIR },
1140 { "map_dom_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DOM },
1141 { "map_end_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_END },
1142 { "map_reg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_REG },
1143 { "map_int_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_SINT, SMP_T_SINT, (void *)PAT_MATCH_INT },
1144 { "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 +01001145
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +02001146 { "map_str_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_STR },
1147 { "map_beg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_BEG },
1148 { "map_sub_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_SUB },
1149 { "map_dir_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DIR },
1150 { "map_dom_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DOM },
1151 { "map_end_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_END },
1152 { "map_reg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_REG },
1153 { "map_int_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_SINT, SMP_T_ADDR, (void *)PAT_MATCH_INT },
1154 { "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 +01001155
1156 { /* END */ },
1157}};
1158
1159__attribute__((constructor))
1160static void __map_init(void)
1161{
1162 /* register format conversion keywords */
1163 sample_register_convs(&sample_conv_kws);
William Lallemandad8be612016-11-18 19:26:17 +01001164 cli_register_kw(&cli_kws);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +01001165}