blob: ad47c7f233fa570d9ca24fba8a2efb93b92ed720 [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{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020060 data->u.str.str = (char *)text;
61 data->u.str.len = strlen(text);
62 data->u.str.size = data->u.str.len + 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. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200141 if (!pattern_read_from_file(&desc->pat, PAT_REF_MAP, arg[0].data.str.str, PAT_MF_NO_DNS,
Thierry FOURNIER94580c92014-02-11 14:36:45 +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;
150 if (!map_parse_ip(arg[1].data.str.str, &data)) {
151 memprintf(err, "map: cannot parse default ip <%s>.", arg[1].data.str.str);
152 return 0;
153 }
154 if (data.type == SMP_T_IPV4) {
155 arg[1].type = ARGT_IPV4;
156 arg[1].data.ipv4 = data.u.ipv4;
157 } else {
158 arg[1].type = ARGT_IPV6;
159 arg[1].data.ipv6 = data.u.ipv6;
160 }
161 }
162
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100163 /* replace the first argument by this definition */
164 arg[0].type = ARGT_MAP;
165 arg[0].data.map = desc;
166
167 return 1;
168}
169
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200170static int sample_conv_map(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100171{
172 struct map_descriptor *desc;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100173 struct pattern *pat;
Thierry Fournier8feaa662016-02-10 22:55:20 +0100174 struct chunk *str;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100175
176 /* get config */
177 desc = arg_p[0].data.map;
178
179 /* Execute the match function. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100180 pat = pattern_exec_match(&desc->pat, smp, 1);
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100181
182 /* Match case. */
183 if (pat) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200184 if (pat->data) {
Thierry Fournier8feaa662016-02-10 22:55:20 +0100185 /* In the regm case, merge the sample with the input. */
186 if ((long)private == PAT_MATCH_REGM) {
187 str = get_trash_chunk();
188 str->len = exp_replace(str->str, str->size, smp->data.u.str.str,
189 pat->data->u.str.str,
190 (regmatch_t *)smp->ctx.a[0]);
191 if (str->len == -1)
192 return 0;
193 smp->data.u.str = *str;
194 return 1;
195 }
196 /* Copy sample. */
Thierry FOURNIER5cc18d42015-08-19 09:02:36 +0200197 smp->data = *pat->data;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100198 smp->flags |= SMP_F_CONST;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100199 return 1;
200 }
201
202 /* Return just int sample containing 1. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200203 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200204 smp->data.u.sint = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100205 return 1;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100206 }
207
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100208 /* If no default value avalaible, the converter fails. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100209 if (arg_p[1].type == ARGT_STOP)
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100210 return 0;
211
212 /* Return the default value. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100213 switch (desc->conv->out_type) {
214
215 case SMP_T_STR:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200216 smp->data.type = SMP_T_STR;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100217 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200218 smp->data.u.str = arg_p[1].data.str;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100219 break;
220
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200221 case SMP_T_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200222 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200223 smp->data.u.sint = arg_p[1].data.sint;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100224 break;
225
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200226 case SMP_T_ADDR:
227 if (arg_p[1].type == ARGT_IPV4) {
228 smp->data.type = SMP_T_IPV4;
229 smp->data.u.ipv4 = arg_p[1].data.ipv4;
230 } else {
231 smp->data.type = SMP_T_IPV6;
232 smp->data.u.ipv6 = arg_p[1].data.ipv6;
233 }
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100234 break;
235 }
236
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100237 return 1;
238}
239
William Lallemandad8be612016-11-18 19:26:17 +0100240/* This function is used with map and acl management. It permits to browse
241 * each reference. The variable <getnext> must contain the current node,
242 * <end> point to the root node and the <flags> permit to filter required
243 * nodes.
244 */
245static inline
246struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
247 unsigned int flags)
248{
249 struct pat_ref *ref = getnext;
250
251 while (1) {
252
253 /* Get next list entry. */
254 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
255
256 /* If the entry is the last of the list, return NULL. */
257 if (&ref->list == end)
258 return NULL;
259
260 /* If the entry match the flag, return it. */
261 if (ref->flags & flags)
262 return ref;
263 }
264}
265
266static inline
267struct pat_ref *pat_ref_lookup_ref(const char *reference)
268{
269 int id;
270 char *error;
271
272 /* If the reference starts by a '#', this is numeric id. */
273 if (reference[0] == '#') {
274 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
275 id = strtol(reference + 1, &error, 10);
276 if (*error != '\0')
277 return NULL;
278
279 /* Perform the unique id lookup. */
280 return pat_ref_lookupid(id);
281 }
282
283 /* Perform the string lookup. */
284 return pat_ref_lookup(reference);
285}
286
287/* This function is used with map and acl management. It permits to browse
288 * each reference.
289 */
290static inline
291struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
292{
293 struct pattern_expr *expr;
294 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
295 if (&expr->list == end)
296 return NULL;
297 return expr;
298}
299
300static int cli_io_handler_pat_list(struct appctx *appctx)
301{
302 struct stream_interface *si = appctx->owner;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200303 struct pat_ref_elt *elt;
304
305 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
306 /* If we're forced to shut down, we might have to remove our
307 * reference to the last ref_elt being dumped.
308 */
309 if (appctx->st2 == STAT_ST_LIST) {
310 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
311 LIST_DEL(&appctx->ctx.sess.bref.users);
312 LIST_INIT(&appctx->ctx.sess.bref.users);
313 }
314 }
315 return 1;
316 }
William Lallemandad8be612016-11-18 19:26:17 +0100317
318 switch (appctx->st2) {
319
320 case STAT_ST_INIT:
Emeric Brun8d85aa42017-06-29 15:40:33 +0200321 /* the function had not been called yet, let's prepare the
322 * buffer for a response. We initialize the current stream
323 * pointer to the first in the global list. When a target
324 * stream is being destroyed, it is responsible for updating
325 * this pointer. We know we have reached the end when this
326 * pointer points back to the head of the streams list.
327 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100328 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200329 LIST_INIT(&appctx->ctx.map.bref.users);
330 appctx->ctx.map.bref.ref = appctx->ctx.map.ref->head.n;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100331 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100332 appctx->st2 = STAT_ST_LIST;
333 /* fall through */
334
335 case STAT_ST_LIST:
Emeric Brunb5997f72017-07-03 11:34:05 +0200336
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100337 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200338
Emeric Brun8d85aa42017-06-29 15:40:33 +0200339 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users)) {
340 LIST_DEL(&appctx->ctx.map.bref.users);
341 LIST_INIT(&appctx->ctx.map.bref.users);
342 }
343
344 while (appctx->ctx.map.bref.ref != &appctx->ctx.map.ref->head) {
William Lallemandad8be612016-11-18 19:26:17 +0100345 chunk_reset(&trash);
346
Emeric Brun8d85aa42017-06-29 15:40:33 +0200347 elt = LIST_ELEM(appctx->ctx.map.bref.ref, struct pat_ref_elt *, list);
348
William Lallemandad8be612016-11-18 19:26:17 +0100349 /* build messages */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200350 if (elt->sample)
William Lallemandad8be612016-11-18 19:26:17 +0100351 chunk_appendf(&trash, "%p %s %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200352 elt, elt->pattern,
353 elt->sample);
William Lallemandad8be612016-11-18 19:26:17 +0100354 else
355 chunk_appendf(&trash, "%p %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200356 elt, elt->pattern);
William Lallemandad8be612016-11-18 19:26:17 +0100357
Willy Tarreau06d80a92017-10-19 14:32:15 +0200358 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100359 /* let's try again later from this stream. We add ourselves into
360 * this stream's users so that it can remove us upon termination.
361 */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200362 LIST_ADDQ(&elt->back_refs, &appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100363 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200364 si_applet_cant_put(si);
William Lallemandad8be612016-11-18 19:26:17 +0100365 return 0;
366 }
367
368 /* get next list entry and check the end of the list */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200369 appctx->ctx.map.bref.ref = elt->list.n;
William Lallemandad8be612016-11-18 19:26:17 +0100370 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100371 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100372 /* fall through */
373
374 default:
375 appctx->st2 = STAT_ST_FIN;
376 return 1;
377 }
378}
379
380static int cli_io_handler_pats_list(struct appctx *appctx)
381{
382 struct stream_interface *si = appctx->owner;
383
384 switch (appctx->st2) {
385 case STAT_ST_INIT:
386 /* Display the column headers. If the message cannot be sent,
387 * quit the fucntion with returning 0. The function is called
388 * later and restart at the state "STAT_ST_INIT".
389 */
390 chunk_reset(&trash);
391 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +0200392 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100393 si_applet_cant_put(si);
394 return 0;
395 }
396
397 /* Now, we start the browsing of the references lists.
398 * Note that the following call to LIST_ELEM return bad pointer. The only
399 * available field of this pointer is <list>. It is used with the function
400 * pat_list_get_next() for retruning the first available entry
401 */
402 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
403 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
404 appctx->ctx.map.display_flags);
405 appctx->st2 = STAT_ST_LIST;
406 /* fall through */
407
408 case STAT_ST_LIST:
409 while (appctx->ctx.map.ref) {
410 chunk_reset(&trash);
411
412 /* Build messages. If the reference is used by another category than
413 * the listed categorie, display the information in the massage.
414 */
415 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
416 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
417 appctx->ctx.map.ref->display);
418
Willy Tarreau06d80a92017-10-19 14:32:15 +0200419 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100420 /* let's try again later from this stream. We add ourselves into
421 * this stream's users so that it can remove us upon termination.
422 */
423 si_applet_cant_put(si);
424 return 0;
425 }
426
427 /* get next list entry and check the end of the list */
428 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
429 appctx->ctx.map.display_flags);
430 }
431
William Lallemandad8be612016-11-18 19:26:17 +0100432 /* fall through */
433
434 default:
435 appctx->st2 = STAT_ST_FIN;
436 return 1;
437 }
438 return 0;
439}
440
441static int cli_io_handler_map_lookup(struct appctx *appctx)
442{
443 struct stream_interface *si = appctx->owner;
444 struct sample sample;
445 struct pattern *pat;
446 int match_method;
447
448 switch (appctx->st2) {
449 case STAT_ST_INIT:
450 /* Init to the first entry. The list cannot be change */
451 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
452 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
453 appctx->st2 = STAT_ST_LIST;
454 /* fall through */
455
456 case STAT_ST_LIST:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100457 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100458 /* for each lookup type */
459 while (appctx->ctx.map.expr) {
460 /* initialise chunk to build new message */
461 chunk_reset(&trash);
462
463 /* execute pattern matching */
464 sample.data.type = SMP_T_STR;
465 sample.flags = SMP_F_CONST;
466 sample.data.u.str.len = appctx->ctx.map.chunk.len;
467 sample.data.u.str.str = appctx->ctx.map.chunk.str;
Emeric Brunb5997f72017-07-03 11:34:05 +0200468
William Lallemandad8be612016-11-18 19:26:17 +0100469 if (appctx->ctx.map.expr->pat_head->match &&
470 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
471 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
472 else
473 pat = NULL;
474
475 /* build return message: set type of match */
476 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
477 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
478 break;
479 if (match_method >= PAT_MATCH_NUM)
480 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
481 else
482 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
483
484 /* case sensitive */
485 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
486 chunk_appendf(&trash, ", case=insensitive");
487 else
488 chunk_appendf(&trash, ", case=sensitive");
489
490 /* Display no match, and set default value */
491 if (!pat) {
492 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
493 chunk_appendf(&trash, ", found=no");
494 else
495 chunk_appendf(&trash, ", match=no");
496 }
497
498 /* Display match and match info */
499 else {
500 /* display match */
501 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
502 chunk_appendf(&trash, ", found=yes");
503 else
504 chunk_appendf(&trash, ", match=yes");
505
506 /* display index mode */
507 if (pat->sflags & PAT_SF_TREE)
508 chunk_appendf(&trash, ", idx=tree");
509 else
510 chunk_appendf(&trash, ", idx=list");
511
512 /* display pattern */
513 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
514 if (pat->ref && pat->ref->pattern)
515 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
516 else
517 chunk_appendf(&trash, ", key=unknown");
518 }
519 else {
520 if (pat->ref && pat->ref->pattern)
521 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
522 else
523 chunk_appendf(&trash, ", pattern=unknown");
524 }
525
526 /* display return value */
527 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
528 if (pat->data && pat->ref && pat->ref->sample)
529 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
530 smp_to_type[pat->data->type]);
531 else
532 chunk_appendf(&trash, ", value=none");
533 }
534 }
535
536 chunk_appendf(&trash, "\n");
537
538 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200539 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100540 /* let's try again later from this stream. We add ourselves into
541 * this stream's users so that it can remove us upon termination.
542 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100543 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100544 si_applet_cant_put(si);
545 return 0;
546 }
547
548 /* get next entry */
549 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
550 &appctx->ctx.map.ref->pat);
551 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100552 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100553 /* fall through */
554
555 default:
556 appctx->st2 = STAT_ST_FIN;
William Lallemandad8be612016-11-18 19:26:17 +0100557 return 1;
558 }
559}
560
561static void cli_release_mlook(struct appctx *appctx)
562{
563 free(appctx->ctx.map.chunk.str);
564 appctx->ctx.map.chunk.str = NULL;
565}
566
567
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200568static int cli_parse_get_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100569{
570 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
571 /* Set flags. */
572 if (args[1][0] == 'm')
573 appctx->ctx.map.display_flags = PAT_REF_MAP;
574 else
575 appctx->ctx.map.display_flags = PAT_REF_ACL;
576
577 /* No parameter. */
578 if (!*args[2] || !*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200579 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
580 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100581 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200582 }
583 else {
584 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100585 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200586 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100587 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100588 return 1;
589 }
590
591 /* lookup into the maps */
592 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
593 if (!appctx->ctx.map.ref) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200594 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
595 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100596 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200597 }
598 else {
599 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100600 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200601 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100602 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100603 return 1;
604 }
605
606 /* copy input string. The string must be allocated because
607 * it may be used over multiple iterations. It's released
608 * at the end and upon abort anyway.
609 */
610 appctx->ctx.map.chunk.len = strlen(args[3]);
611 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
612 appctx->ctx.map.chunk.str = strdup(args[3]);
613 if (!appctx->ctx.map.chunk.str) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200614 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100615 appctx->ctx.cli.msg = "Out of memory error.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100616 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100617 return 1;
618 }
619
620 return 0;
621 }
622 return 1;
623}
624
Emeric Brun8d85aa42017-06-29 15:40:33 +0200625static void cli_release_show_map(struct appctx *appctx)
626{
627 if (appctx->st2 == STAT_ST_LIST) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100628 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200629 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users))
630 LIST_DEL(&appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100631 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200632 }
633}
634
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200635static int cli_parse_show_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100636{
637 if (strcmp(args[1], "map") == 0 ||
638 strcmp(args[1], "acl") == 0) {
639
640 /* Set ACL or MAP flags. */
641 if (args[1][0] == 'm')
642 appctx->ctx.map.display_flags = PAT_REF_MAP;
643 else
644 appctx->ctx.map.display_flags = PAT_REF_ACL;
645
646 /* no parameter: display all map available */
647 if (!*args[2]) {
William Lallemandad8be612016-11-18 19:26:17 +0100648 appctx->io_handler = cli_io_handler_pats_list;
649 return 0;
650 }
651
652 /* lookup into the refs and check the map flag */
653 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
654 if (!appctx->ctx.map.ref ||
655 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200656 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
657 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100658 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200659 }
660 else {
661 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100662 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200663 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100664 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100665 return 1;
666 }
William Lallemandad8be612016-11-18 19:26:17 +0100667 appctx->io_handler = cli_io_handler_pat_list;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200668 appctx->io_release = cli_release_show_map;
William Lallemandad8be612016-11-18 19:26:17 +0100669 return 0;
670 }
671
672 return 0;
673}
674
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200675static int cli_parse_set_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100676{
677 if (strcmp(args[1], "map") == 0) {
678 char *err;
679
680 /* Set flags. */
681 appctx->ctx.map.display_flags = PAT_REF_MAP;
682
683 /* Expect three parameters: map name, key and new value. */
684 if (!*args[2] || !*args[3] || !*args[4]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200685 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100686 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100687 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100688 return 1;
689 }
690
691 /* Lookup the reference in the maps. */
692 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
693 if (!appctx->ctx.map.ref) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200694 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100695 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100696 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100697 return 1;
698 }
699
700 /* If the entry identifier start with a '#', it is considered as
701 * pointer id
702 */
703 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
704 struct pat_ref_elt *ref;
705 long long int conv;
706 char *error;
707
708 /* Convert argument to integer value. */
709 conv = strtoll(&args[3][1], &error, 16);
710 if (*error != '\0') {
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 = "Malformed 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 /* Convert and check integer to pointer. */
718 ref = (struct pat_ref_elt *)(long)conv;
719 if ((long long int)(long)ref != conv) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200720 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100721 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100722 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100723 return 1;
724 }
725
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200726 /* Try to modify the entry. */
William Lallemandad8be612016-11-18 19:26:17 +0100727 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100728 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100729 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100730 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200731 if (err) {
William Lallemandad8be612016-11-18 19:26:17 +0100732 memprintf(&err, "%s.\n", err);
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200733 appctx->ctx.cli.err = err;
734 appctx->st0 = CLI_ST_PRINT_FREE;
735 }
736 else {
737 appctx->ctx.cli.severity = LOG_ERR;
738 appctx->ctx.cli.msg = "Failed to update an entry.\n";
739 appctx->st0 = CLI_ST_PRINT;
740 }
William Lallemandad8be612016-11-18 19:26:17 +0100741 return 1;
742 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100743 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100744 }
745 else {
746 /* Else, use the entry identifier as pattern
747 * string, and update the value.
748 */
749 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100750 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100751 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100752 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200753 if (err) {
William Lallemandad8be612016-11-18 19:26:17 +0100754 memprintf(&err, "%s.\n", err);
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200755 appctx->ctx.cli.err = err;
756 appctx->st0 = CLI_ST_PRINT_FREE;
757 }
758 else {
759 appctx->ctx.cli.severity = LOG_ERR;
760 appctx->ctx.cli.msg = "Failed to update an entry.\n";
761 appctx->st0 = CLI_ST_PRINT;
762 }
William Lallemandad8be612016-11-18 19:26:17 +0100763 return 1;
764 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100765 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100766 }
767
768 /* The set is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100769 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100770 return 0;
771 }
772 return 1;
773}
774
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200775static int map_add_key_value(struct appctx *appctx, const char *key, const char *value, char **err)
776{
777 int ret;
778
779 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
780 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
781 ret = pat_ref_add(appctx->ctx.map.ref, key, value, err);
782 else
783 ret = pat_ref_add(appctx->ctx.map.ref, key, NULL, err);
784 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
785
786 return ret;
787}
788
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200789static int cli_parse_add_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100790{
791 if (strcmp(args[1], "map") == 0 ||
792 strcmp(args[1], "acl") == 0) {
793 int ret;
794 char *err;
795
796 /* Set flags. */
797 if (args[1][0] == 'm')
798 appctx->ctx.map.display_flags = PAT_REF_MAP;
799 else
800 appctx->ctx.map.display_flags = PAT_REF_ACL;
801
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200802 /* If the keyword is "map", we expect:
803 * - three parameters if there is no payload
804 * - one parameter if there is a payload
805 * If it is "acl", we expect only two parameters
William Lallemandad8be612016-11-18 19:26:17 +0100806 */
807 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200808 if ((!payload && (!*args[2] || !*args[3] || !*args[4])) ||
809 (payload && !*args[2])) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200810 appctx->ctx.cli.severity = LOG_ERR;
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200811 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 +0100812 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100813 return 1;
814 }
815 }
816 else {
817 if (!*args[2] || !*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200818 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100819 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100820 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100821 return 1;
822 }
823 }
824
825 /* Lookup for the reference. */
826 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
827 if (!appctx->ctx.map.ref) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200828 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
829 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100830 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200831 }
832 else {
833 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100834 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200835 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100836 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100837 return 1;
838 }
839
840 /* The command "add acl" is prohibited if the reference
841 * use samples.
842 */
843 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
844 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200845 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100846 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
847 "You must use the command 'add map' to add values.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100848 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100849 return 1;
850 }
851
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200852 /* Add value(s). */
William Lallemandad8be612016-11-18 19:26:17 +0100853 err = NULL;
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200854 if (!payload) {
855 ret = map_add_key_value(appctx, args[3], args[4], &err);
856 if (!ret) {
857 if (err) {
858 memprintf(&err, "%s.\n", err);
859 appctx->ctx.cli.err = err;
860 appctx->st0 = CLI_ST_PRINT_FREE;
861 }
862 else {
863 appctx->ctx.cli.severity = LOG_ERR;
864 appctx->ctx.cli.msg = "Failed to add an entry.\n";
865 appctx->st0 = CLI_ST_PRINT;
866 }
867 return 1;
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200868 }
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200869 }
870 else {
871 const char *end = payload + strlen(payload);
872
873 while (payload < end) {
874 char *key, *value;
875 size_t l;
876
877 /* key */
878 key = payload;
879 l = strcspn(key, " \t");
880 payload += l;
881
882 if (!*payload && appctx->ctx.map.display_flags == PAT_REF_MAP) {
883 memprintf(&err, "Missing value for key '%s'.\n", key);
884 appctx->ctx.cli.err = err;
885 appctx->st0 = CLI_ST_PRINT_FREE;
886 return 1;
887 }
888 key[l] = 0;
889 payload++;
890
891 /* value */
892 payload += strspn(payload, " \t");
893 value = payload;
894 l = strcspn(value, "\n");
895 payload += l;
896 if (*payload)
897 payload++;
898 value[l] = 0;
899
900 ret = map_add_key_value(appctx, key, value, &err);
901 if (!ret) {
902 if (err) {
903 memprintf(&err, "%s.\n", err);
904 appctx->ctx.cli.err = err;
905 appctx->st0 = CLI_ST_PRINT_FREE;
906 }
907 else {
908 appctx->ctx.cli.severity = LOG_ERR;
909 appctx->ctx.cli.msg = "Failed to add a key.\n";
910 appctx->st0 = CLI_ST_PRINT;
911 }
912 return 1;
913 }
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200914 }
William Lallemandad8be612016-11-18 19:26:17 +0100915 }
916
917 /* The add is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100918 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100919 return 1;
920 }
921
922 return 0;
923}
924
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200925static int cli_parse_del_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100926{
927 if (args[1][0] == 'm')
928 appctx->ctx.map.display_flags = PAT_REF_MAP;
929 else
930 appctx->ctx.map.display_flags = PAT_REF_ACL;
931
932 /* Expect two parameters: map name and key. */
933 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
934 if (!*args[2] || !*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200935 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100936 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100937 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100938 return 1;
939 }
940 }
941
942 else {
943 if (!*args[2] || !*args[3]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200944 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100945 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100946 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100947 return 1;
948 }
949 }
950
951 /* Lookup the reference in the maps. */
952 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
953 if (!appctx->ctx.map.ref ||
954 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
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 = "Unknown map identifier. Please use #<id> or <file>.\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 /* If the entry identifier start with a '#', it is considered as
962 * pointer id
963 */
964 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
965 struct pat_ref_elt *ref;
966 long long int conv;
967 char *error;
968
969 /* Convert argument to integer value. */
970 conv = strtoll(&args[3][1], &error, 16);
971 if (*error != '\0') {
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 = "Malformed 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 /* Convert and check integer to pointer. */
979 ref = (struct pat_ref_elt *)(long)conv;
980 if ((long long int)(long)ref != conv) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200981 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +0100982 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100983 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +0100984 return 1;
985 }
986
987 /* Try to delete the entry. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100988 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100989 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100990 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100991 /* The entry is not found, send message. */
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 = "Key not found.\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 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100997 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100998 }
999 else {
1000 /* Else, use the entry identifier as pattern
1001 * string and try to delete the entry.
1002 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001003 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001004 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001005 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001006 /* The entry is not found, send message. */
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001007 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001008 appctx->ctx.cli.msg = "Key not found.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001009 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001010 return 1;
1011 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001012 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001013 }
1014
1015 /* The deletion is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001016 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +01001017 return 1;
1018}
1019
1020
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001021static int cli_parse_clear_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +01001022{
1023 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1024 /* Set ACL or MAP flags. */
1025 if (args[1][0] == 'm')
1026 appctx->ctx.map.display_flags = PAT_REF_MAP;
1027 else
1028 appctx->ctx.map.display_flags = PAT_REF_ACL;
1029
1030 /* no parameter */
1031 if (!*args[2]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001032 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
1033 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001034 appctx->ctx.cli.msg = "Missing map identifier.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001035 }
1036 else {
1037 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001038 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001039 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001040 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001041 return 1;
1042 }
1043
1044 /* lookup into the refs and check the map flag */
1045 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
1046 if (!appctx->ctx.map.ref ||
1047 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001048 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
1049 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001050 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001051 }
1052 else {
1053 appctx->ctx.cli.severity = LOG_ERR;
William Lallemandad8be612016-11-18 19:26:17 +01001054 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001055 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001056 appctx->st0 = CLI_ST_PRINT;
William Lallemandad8be612016-11-18 19:26:17 +01001057 return 1;
1058 }
1059
1060 /* Clear all. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001061 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001062 pat_ref_prune(appctx->ctx.map.ref);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001063 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +01001064
1065 /* return response */
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001066 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +01001067 return 1;
1068 }
1069 return 0;
1070}
1071
1072/* register cli keywords */
1073
1074static struct cli_kw_list cli_kws = {{ },{
1075 { { "add", "acl", NULL }, "add acl : add acl entry", cli_parse_add_map, NULL },
1076 { { "clear", "acl", NULL }, "clear acl <id> : clear the content of this acl", cli_parse_clear_map, NULL },
1077 { { "del", "acl", NULL }, "del acl : delete acl entry", cli_parse_del_map, NULL },
1078 { { "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 },
1079 { { "show", "acl", NULL }, "show acl [id] : report available acls or dump an acl's contents", cli_parse_show_map, NULL },
1080 { { "add", "map", NULL }, "add map : add map entry", cli_parse_add_map, NULL },
1081 { { "clear", "map", NULL }, "clear map <id> : clear the content of this map", cli_parse_clear_map, NULL },
1082 { { "del", "map", NULL }, "del map : delete map entry", cli_parse_del_map, NULL },
Nenad Merdanovic96c15712017-03-12 22:01:36 +01001083 { { "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 +01001084 { { "set", "map", NULL }, "set map : modify map entry", cli_parse_set_map, NULL },
1085 { { "show", "map", NULL }, "show map [id] : report available maps or dump a map's contents", cli_parse_show_map, NULL },
1086 { { NULL }, NULL, NULL, NULL }
1087}};
1088
1089
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +01001090/* Note: must not be declared <const> as its list will be overwritten
1091 *
1092 * For the map_*_int keywords, the output is declared as SMP_T_UINT, but the converter function
1093 * can provide SMP_T_UINT, SMP_T_SINT or SMP_T_BOOL depending on how the patterns found in the
1094 * file can be parsed.
1095 *
1096 * For the map_*_ip keyword, the output is declared as SMP_T_IPV4, but the converter function
1097 * can provide SMP_T_IPV4 or SMP_T_IPV6 depending on the patterns found in the file.
1098 *
1099 * The map_* keywords only emit strings.
1100 *
1101 * The output type is only used during the configuration parsing. It is used for detecting
1102 * compatibility problems.
1103 *
1104 * The arguments are: <file>[,<default value>]
1105 */
1106static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER1edc9712014-12-15 16:18:39 +01001107 { "map", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1108 { "map_str", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1109 { "map_beg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_BEG },
1110 { "map_sub", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_SUB },
1111 { "map_dir", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DIR },
1112 { "map_dom", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DOM },
1113 { "map_end", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_END },
1114 { "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 +01001115 { "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 +02001116 { "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 +01001117 { "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 +01001118
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001119 { "map_str_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_STR },
1120 { "map_beg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_BEG },
1121 { "map_sub_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_SUB },
1122 { "map_dir_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DIR },
1123 { "map_dom_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DOM },
1124 { "map_end_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_END },
1125 { "map_reg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_REG },
1126 { "map_int_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_SINT, SMP_T_SINT, (void *)PAT_MATCH_INT },
1127 { "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 +01001128
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +02001129 { "map_str_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_STR },
1130 { "map_beg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_BEG },
1131 { "map_sub_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_SUB },
1132 { "map_dir_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DIR },
1133 { "map_dom_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DOM },
1134 { "map_end_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_END },
1135 { "map_reg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_REG },
1136 { "map_int_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_SINT, SMP_T_ADDR, (void *)PAT_MATCH_INT },
1137 { "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 +01001138
1139 { /* END */ },
1140}};
1141
1142__attribute__((constructor))
1143static void __map_init(void)
1144{
1145 /* register format conversion keywords */
1146 sample_register_convs(&sample_conv_kws);
William Lallemandad8be612016-11-18 19:26:17 +01001147 cli_register_kw(&cli_kws);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +01001148}