blob: 6c35680a7a0b48dd31b7d04921742dac13da73ea [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
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010013#include <stdio.h>
Willy Tarreau97218ce2021-04-30 14:57:03 +020014#include <syslog.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010015
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020016#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <haproxy/applet-t.h>
18#include <haproxy/arg.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020019#include <haproxy/cli.h>
Christopher Faulet908628c2022-03-25 16:43:49 +010020#include <haproxy/conn_stream.h>
21#include <haproxy/cs_utils.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020022#include <haproxy/map.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020023#include <haproxy/pattern.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020024#include <haproxy/regex.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020025#include <haproxy/sample.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020026#include <haproxy/stats-t.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020027#include <haproxy/tools.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010028
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010029
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020030/* Parse an IPv4 or IPv6 address and store it into the sample.
31 * The output type is IPv4 or IPv6.
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010032 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020033int map_parse_ip(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010034{
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020035 int len = strlen(text);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010036
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +020037 if (buf2ip(text, len, &data->u.ipv4)) {
38 data->type = SMP_T_IPV4;
39 return 1;
40 }
41 if (buf2ip6(text, len, &data->u.ipv6)) {
42 data->type = SMP_T_IPV6;
43 return 1;
44 }
45 return 0;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010046}
47
48/* Parse a string and store a pointer to it into the sample. The original
49 * string must be left in memory because we return a direct memory reference.
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010050 * The output type is SMP_T_STR. There is no risk that the data will be
51 * overwritten because sample_conv_map() makes a const sample with this
52 * output.
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010053 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020054int map_parse_str(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010055{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020056 data->u.str.area = (char *)text;
57 data->u.str.data = strlen(text);
58 data->u.str.size = data->u.str.data + 1;
Thierry FOURNIER503bb092015-08-19 08:35:43 +020059 data->type = SMP_T_STR;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010060 return 1;
61}
62
63/* Parse an integer and convert it to a sample. The output type is SINT if the
64 * number is negative, or UINT if it is positive or null. The function returns
65 * zero (error) if the number is too large.
66 */
Thierry FOURNIER503bb092015-08-19 08:35:43 +020067int map_parse_int(const char *text, struct sample_data *data)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010068{
Thierry FOURNIER503bb092015-08-19 08:35:43 +020069 data->type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020070 data->u.sint = read_int64(&text, text + strlen(text));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020071 if (*text != '\0')
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010072 return 0;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010073 return 1;
74}
75
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010076/* This crete and initialize map descriptor.
77 * Return NULL if out of memory error
78 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010079static struct map_descriptor *map_create_descriptor(struct sample_conv *conv)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010080{
81 struct map_descriptor *desc;
82
83 desc = calloc(1, sizeof(*desc));
84 if (!desc)
85 return NULL;
86
87 desc->conv = conv;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010088
89 return desc;
90}
91
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010092/* This function load the map file according with data type declared into
93 * the "struct sample_conv".
94 *
95 * This function choose the indexation type (ebtree or list) according with
96 * the type of match needed.
97 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +020098int sample_load_map(struct arg *arg, struct sample_conv *conv,
99 const char *file, int line, char **err)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100100{
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100101 struct map_descriptor *desc;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100102
Christopher Faulet0eb967d2020-08-05 23:23:37 +0200103 if (!(global.mode & MODE_STARTING)) {
104 memprintf(err, "map: cannot load map at runtime");
105 return 0;
106 }
107
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100108 /* create new map descriptor */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100109 desc = map_create_descriptor(conv);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100110 if (!desc) {
111 memprintf(err, "out of memory");
112 return 0;
113 }
114
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100115 /* Initialize pattern */
116 pattern_init_head(&desc->pat);
117
118 /* This is original pattern, must free */
119 desc->do_free = 1;
120
121 /* Set the match method. */
Thierry FOURNIER1edc9712014-12-15 16:18:39 +0100122 desc->pat.match = pat_match_fcts[(long)conv->private];
123 desc->pat.parse = pat_parse_fcts[(long)conv->private];
124 desc->pat.index = pat_index_fcts[(long)conv->private];
Thierry FOURNIER1edc9712014-12-15 16:18:39 +0100125 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
Willy Tarreauaa5801b2019-04-19 11:35:22 +0200145 /* the maps of type IP support a string as default value. This
146 * string can be an ipv4 or an ipv6, we must convert it.
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200147 */
Willy Tarreauaa5801b2019-04-19 11:35:22 +0200148 if (arg[1].type != ARGT_STOP && desc->conv->out_type == SMP_T_ADDR) {
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200149 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 }
Christopher Faulet6ad7df42020-08-07 11:45:18 +0200155 chunk_destroy(&arg[1].data.str);
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200156 if (data.type == SMP_T_IPV4) {
157 arg[1].type = ARGT_IPV4;
158 arg[1].data.ipv4 = data.u.ipv4;
159 } else {
160 arg[1].type = ARGT_IPV6;
161 arg[1].data.ipv6 = data.u.ipv6;
162 }
163 }
164
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100165 /* replace the first argument by this definition */
Christopher Faulet6ad7df42020-08-07 11:45:18 +0200166 chunk_destroy(&arg[0].data.str);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100167 arg[0].type = ARGT_MAP;
168 arg[0].data.map = desc;
169
170 return 1;
171}
172
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200173static int sample_conv_map(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100174{
175 struct map_descriptor *desc;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100176 struct pattern *pat;
Willy Tarreau83061a82018-07-13 11:56:34 +0200177 struct buffer *str;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100178
179 /* get config */
180 desc = arg_p[0].data.map;
181
182 /* Execute the match function. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100183 pat = pattern_exec_match(&desc->pat, smp, 1);
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100184
185 /* Match case. */
186 if (pat) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200187 if (pat->data) {
Thierry Fournier8feaa662016-02-10 22:55:20 +0100188 /* In the regm case, merge the sample with the input. */
189 if ((long)private == PAT_MATCH_REGM) {
Emeric Brun27102212018-07-17 09:47:07 -0400190 struct buffer *tmptrash;
Willy Tarreau2842e052018-08-22 04:55:43 +0200191 int len;
Emeric Brun27102212018-07-17 09:47:07 -0400192
193 /* Copy the content of the sample because it could
194 be scratched by incoming get_trash_chunk */
195 tmptrash = alloc_trash_chunk();
196 if (!tmptrash)
197 return 0;
198
199 tmptrash->data = smp->data.u.str.data;
200 if (tmptrash->data > (tmptrash->size-1))
201 tmptrash->data = tmptrash->size-1;
202
203 memcpy(tmptrash->area, smp->data.u.str.area, tmptrash->data);
204 tmptrash->area[tmptrash->data] = 0;
205
Thierry Fournier8feaa662016-02-10 22:55:20 +0100206 str = get_trash_chunk();
Willy Tarreau2842e052018-08-22 04:55:43 +0200207 len = exp_replace(str->area, str->size,
208 tmptrash->area,
209 pat->data->u.str.area,
210 (regmatch_t *)smp->ctx.a[0]);
Nenad Merdanovic646b7742019-04-12 22:54:28 +0200211 free_trash_chunk(tmptrash);
212
Willy Tarreau2842e052018-08-22 04:55:43 +0200213 if (len == -1)
Thierry Fournier8feaa662016-02-10 22:55:20 +0100214 return 0;
Willy Tarreau2842e052018-08-22 04:55:43 +0200215
216 str->data = len;
Thierry Fournier8feaa662016-02-10 22:55:20 +0100217 smp->data.u.str = *str;
218 return 1;
219 }
220 /* Copy sample. */
Thierry FOURNIER5cc18d42015-08-19 09:02:36 +0200221 smp->data = *pat->data;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100222 smp->flags |= SMP_F_CONST;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100223 return 1;
224 }
225
226 /* Return just int sample containing 1. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200227 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200228 smp->data.u.sint = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100229 return 1;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100230 }
231
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800232 /* If no default value available, the converter fails. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100233 if (arg_p[1].type == ARGT_STOP)
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100234 return 0;
235
236 /* Return the default value. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100237 switch (desc->conv->out_type) {
238
239 case SMP_T_STR:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200240 smp->data.type = SMP_T_STR;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100241 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200242 smp->data.u.str = arg_p[1].data.str;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100243 break;
244
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200245 case SMP_T_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200246 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200247 smp->data.u.sint = arg_p[1].data.sint;
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100248 break;
249
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +0200250 case SMP_T_ADDR:
251 if (arg_p[1].type == ARGT_IPV4) {
252 smp->data.type = SMP_T_IPV4;
253 smp->data.u.ipv4 = arg_p[1].data.ipv4;
254 } else {
255 smp->data.type = SMP_T_IPV6;
256 smp->data.u.ipv6 = arg_p[1].data.ipv6;
257 }
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100258 break;
259 }
260
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100261 return 1;
262}
263
William Lallemandad8be612016-11-18 19:26:17 +0100264/* This function is used with map and acl management. It permits to browse
265 * each reference. The variable <getnext> must contain the current node,
266 * <end> point to the root node and the <flags> permit to filter required
267 * nodes.
268 */
269static inline
270struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
271 unsigned int flags)
272{
273 struct pat_ref *ref = getnext;
274
275 while (1) {
276
277 /* Get next list entry. */
278 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
279
280 /* If the entry is the last of the list, return NULL. */
281 if (&ref->list == end)
282 return NULL;
283
284 /* If the entry match the flag, return it. */
285 if (ref->flags & flags)
286 return ref;
287 }
288}
289
290static inline
291struct pat_ref *pat_ref_lookup_ref(const char *reference)
292{
293 int id;
294 char *error;
295
296 /* If the reference starts by a '#', this is numeric id. */
297 if (reference[0] == '#') {
298 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
299 id = strtol(reference + 1, &error, 10);
300 if (*error != '\0')
301 return NULL;
302
303 /* Perform the unique id lookup. */
304 return pat_ref_lookupid(id);
305 }
306
307 /* Perform the string lookup. */
308 return pat_ref_lookup(reference);
309}
310
311/* This function is used with map and acl management. It permits to browse
312 * each reference.
313 */
314static inline
315struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
316{
317 struct pattern_expr *expr;
318 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
319 if (&expr->list == end)
320 return NULL;
321 return expr;
322}
323
Willy Tarreau95f753e2021-04-30 12:09:54 +0200324/* expects the current generation ID in appctx->cli.cli.i0 */
William Lallemandad8be612016-11-18 19:26:17 +0100325static int cli_io_handler_pat_list(struct appctx *appctx)
326{
Christopher Faulet908628c2022-03-25 16:43:49 +0100327 struct conn_stream *cs = appctx->owner;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200328 struct pat_ref_elt *elt;
329
Christopher Faulet908628c2022-03-25 16:43:49 +0100330 if (unlikely(cs_ic(cs)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Emeric Brun8d85aa42017-06-29 15:40:33 +0200331 /* If we're forced to shut down, we might have to remove our
332 * reference to the last ref_elt being dumped.
333 */
334 if (appctx->st2 == STAT_ST_LIST) {
Willy Tarreau2edaace2022-05-03 15:19:49 +0200335 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Dragan Dosen336a11f2018-05-04 16:27:15 +0200336 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users)) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200337 LIST_DELETE(&appctx->ctx.map.bref.users);
Dragan Dosen336a11f2018-05-04 16:27:15 +0200338 LIST_INIT(&appctx->ctx.map.bref.users);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200339 }
Willy Tarreau2edaace2022-05-03 15:19:49 +0200340 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200341 }
342 return 1;
343 }
William Lallemandad8be612016-11-18 19:26:17 +0100344
345 switch (appctx->st2) {
346
347 case STAT_ST_INIT:
William Lallemandad8be612016-11-18 19:26:17 +0100348 appctx->st2 = STAT_ST_LIST;
349 /* fall through */
350
351 case STAT_ST_LIST:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100352 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200353
Emeric Brun8d85aa42017-06-29 15:40:33 +0200354 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users)) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200355 LIST_DELETE(&appctx->ctx.map.bref.users);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200356 LIST_INIT(&appctx->ctx.map.bref.users);
Willy Tarreau1ae0c432022-05-03 15:26:27 +0200357 } else {
358 appctx->ctx.map.bref.ref = appctx->ctx.map.ref->head.n;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200359 }
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
Willy Tarreau95f753e2021-04-30 12:09:54 +0200366 if (elt->gen_id != appctx->ctx.cli.i0)
Willy Tarreauc93da692020-10-29 09:41:34 +0100367 goto skip;
368
William Lallemandad8be612016-11-18 19:26:17 +0100369 /* build messages */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200370 if (elt->sample)
William Lallemandad8be612016-11-18 19:26:17 +0100371 chunk_appendf(&trash, "%p %s %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200372 elt, elt->pattern,
373 elt->sample);
William Lallemandad8be612016-11-18 19:26:17 +0100374 else
375 chunk_appendf(&trash, "%p %s\n",
Emeric Brun8d85aa42017-06-29 15:40:33 +0200376 elt, elt->pattern);
William Lallemandad8be612016-11-18 19:26:17 +0100377
Christopher Faulet908628c2022-03-25 16:43:49 +0100378 if (ci_putchk(cs_ic(cs), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100379 /* let's try again later from this stream. We add ourselves into
380 * this stream's users so that it can remove us upon termination.
381 */
Willy Tarreau2b718102021-04-21 07:32:39 +0200382 LIST_APPEND(&elt->back_refs, &appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100383 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200384 cs_rx_room_blk(cs);
William Lallemandad8be612016-11-18 19:26:17 +0100385 return 0;
386 }
Willy Tarreauc93da692020-10-29 09:41:34 +0100387 skip:
William Lallemandad8be612016-11-18 19:26:17 +0100388 /* get next list entry and check the end of the list */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200389 appctx->ctx.map.bref.ref = elt->list.n;
William Lallemandad8be612016-11-18 19:26:17 +0100390 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100391 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100392 /* fall through */
393
394 default:
395 appctx->st2 = STAT_ST_FIN;
396 return 1;
397 }
398}
399
400static int cli_io_handler_pats_list(struct appctx *appctx)
401{
Christopher Faulet908628c2022-03-25 16:43:49 +0100402 struct conn_stream *cs = appctx->owner;
William Lallemandad8be612016-11-18 19:26:17 +0100403
404 switch (appctx->st2) {
405 case STAT_ST_INIT:
406 /* Display the column headers. If the message cannot be sent,
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800407 * quit the function with returning 0. The function is called
408 * later and restarted at the state "STAT_ST_INIT".
William Lallemandad8be612016-11-18 19:26:17 +0100409 */
410 chunk_reset(&trash);
411 chunk_appendf(&trash, "# id (file) description\n");
Christopher Faulet908628c2022-03-25 16:43:49 +0100412 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200413 cs_rx_room_blk(cs);
William Lallemandad8be612016-11-18 19:26:17 +0100414 return 0;
415 }
416
417 /* Now, we start the browsing of the references lists.
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800418 * Note that the following call to LIST_ELEM returns a bad pointer. The only
William Lallemandad8be612016-11-18 19:26:17 +0100419 * available field of this pointer is <list>. It is used with the function
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800420 * pat_list_get_next() for returning the first available entry
William Lallemandad8be612016-11-18 19:26:17 +0100421 */
422 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
423 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
424 appctx->ctx.map.display_flags);
425 appctx->st2 = STAT_ST_LIST;
426 /* fall through */
427
428 case STAT_ST_LIST:
429 while (appctx->ctx.map.ref) {
430 chunk_reset(&trash);
431
432 /* Build messages. If the reference is used by another category than
Joseph Herlantf43b88b2018-11-25 11:48:18 -0800433 * the listed categories, display the information in the message.
William Lallemandad8be612016-11-18 19:26:17 +0100434 */
Dragan Dosena75eea72021-05-21 16:59:15 +0200435 chunk_appendf(&trash, "%d (%s) %s. curr_ver=%u next_ver=%u entry_cnt=%llu\n", appctx->ctx.map.ref->unique_id,
William Lallemandad8be612016-11-18 19:26:17 +0100436 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
Dragan Dosena75eea72021-05-21 16:59:15 +0200437 appctx->ctx.map.ref->display, appctx->ctx.map.ref->curr_gen, appctx->ctx.map.ref->next_gen,
438 appctx->ctx.map.ref->entry_cnt);
William Lallemandad8be612016-11-18 19:26:17 +0100439
Christopher Faulet908628c2022-03-25 16:43:49 +0100440 if (ci_putchk(cs_ic(cs), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100441 /* let's try again later from this stream. We add ourselves into
442 * this stream's users so that it can remove us upon termination.
443 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200444 cs_rx_room_blk(cs);
William Lallemandad8be612016-11-18 19:26:17 +0100445 return 0;
446 }
447
448 /* get next list entry and check the end of the list */
449 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
450 appctx->ctx.map.display_flags);
451 }
452
William Lallemandad8be612016-11-18 19:26:17 +0100453 /* fall through */
454
455 default:
456 appctx->st2 = STAT_ST_FIN;
457 return 1;
458 }
459 return 0;
460}
461
462static int cli_io_handler_map_lookup(struct appctx *appctx)
463{
Christopher Faulet908628c2022-03-25 16:43:49 +0100464 struct conn_stream *cs = appctx->owner;
William Lallemandad8be612016-11-18 19:26:17 +0100465 struct sample sample;
466 struct pattern *pat;
467 int match_method;
468
469 switch (appctx->st2) {
470 case STAT_ST_INIT:
471 /* Init to the first entry. The list cannot be change */
472 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
473 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
474 appctx->st2 = STAT_ST_LIST;
475 /* fall through */
476
477 case STAT_ST_LIST:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100478 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100479 /* for each lookup type */
480 while (appctx->ctx.map.expr) {
481 /* initialise chunk to build new message */
482 chunk_reset(&trash);
483
484 /* execute pattern matching */
485 sample.data.type = SMP_T_STR;
486 sample.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200487 sample.data.u.str.data = appctx->ctx.map.chunk.data;
488 sample.data.u.str.area = appctx->ctx.map.chunk.area;
Emeric Brunb5997f72017-07-03 11:34:05 +0200489
William Lallemandad8be612016-11-18 19:26:17 +0100490 if (appctx->ctx.map.expr->pat_head->match &&
491 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
492 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
493 else
494 pat = NULL;
495
496 /* build return message: set type of match */
497 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
498 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
499 break;
500 if (match_method >= PAT_MATCH_NUM)
501 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
502 else
503 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
504
505 /* case sensitive */
506 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
507 chunk_appendf(&trash, ", case=insensitive");
508 else
509 chunk_appendf(&trash, ", case=sensitive");
510
511 /* Display no match, and set default value */
512 if (!pat) {
513 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
514 chunk_appendf(&trash, ", found=no");
515 else
516 chunk_appendf(&trash, ", match=no");
517 }
518
519 /* Display match and match info */
520 else {
521 /* display match */
522 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
523 chunk_appendf(&trash, ", found=yes");
524 else
525 chunk_appendf(&trash, ", match=yes");
526
527 /* display index mode */
528 if (pat->sflags & PAT_SF_TREE)
529 chunk_appendf(&trash, ", idx=tree");
530 else
531 chunk_appendf(&trash, ", idx=list");
532
533 /* display pattern */
534 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
535 if (pat->ref && pat->ref->pattern)
536 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
537 else
538 chunk_appendf(&trash, ", key=unknown");
539 }
540 else {
541 if (pat->ref && pat->ref->pattern)
542 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
543 else
544 chunk_appendf(&trash, ", pattern=unknown");
545 }
546
547 /* display return value */
548 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
549 if (pat->data && pat->ref && pat->ref->sample)
550 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
551 smp_to_type[pat->data->type]);
552 else
553 chunk_appendf(&trash, ", value=none");
554 }
555 }
556
557 chunk_appendf(&trash, "\n");
558
559 /* display response */
Christopher Faulet908628c2022-03-25 16:43:49 +0100560 if (ci_putchk(cs_ic(cs), &trash) == -1) {
William Lallemandad8be612016-11-18 19:26:17 +0100561 /* let's try again later from this stream. We add ourselves into
562 * this stream's users so that it can remove us upon termination.
563 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100564 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200565 cs_rx_room_blk(cs);
William Lallemandad8be612016-11-18 19:26:17 +0100566 return 0;
567 }
568
569 /* get next entry */
570 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
571 &appctx->ctx.map.ref->pat);
572 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100573 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100574 /* fall through */
575
576 default:
577 appctx->st2 = STAT_ST_FIN;
William Lallemandad8be612016-11-18 19:26:17 +0100578 return 1;
579 }
580}
581
582static void cli_release_mlook(struct appctx *appctx)
583{
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100584 ha_free(&appctx->ctx.map.chunk.area);
William Lallemandad8be612016-11-18 19:26:17 +0100585}
586
587
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200588static int cli_parse_get_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100589{
590 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
591 /* Set flags. */
592 if (args[1][0] == 'm')
593 appctx->ctx.map.display_flags = PAT_REF_MAP;
594 else
595 appctx->ctx.map.display_flags = PAT_REF_ACL;
596
597 /* No parameter. */
598 if (!*args[2] || !*args[3]) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200599 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
600 return cli_err(appctx, "Missing map identifier and/or key.\n");
601 else
602 return cli_err(appctx, "Missing ACL identifier and/or key.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100603 }
604
605 /* lookup into the maps */
606 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
607 if (!appctx->ctx.map.ref) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200608 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
609 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
610 else
611 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100612 }
613
614 /* copy input string. The string must be allocated because
615 * it may be used over multiple iterations. It's released
616 * at the end and upon abort anyway.
617 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200618 appctx->ctx.map.chunk.data = strlen(args[3]);
619 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.data + 1;
620 appctx->ctx.map.chunk.area = strdup(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200621 if (!appctx->ctx.map.chunk.area)
622 return cli_err(appctx, "Out of memory error.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100623
624 return 0;
625 }
626 return 1;
627}
628
Willy Tarreau97218ce2021-04-30 14:57:03 +0200629static int cli_parse_prepare_map(char **args, char *payload, struct appctx *appctx, void *private)
630{
631 if (strcmp(args[1], "map") == 0 ||
632 strcmp(args[1], "acl") == 0) {
633 uint next_gen;
634 char *msg = NULL;
635
636 /* Set ACL or MAP flags. */
637 if (args[1][0] == 'm')
638 appctx->ctx.map.display_flags = PAT_REF_MAP;
639 else
640 appctx->ctx.map.display_flags = PAT_REF_ACL;
641
642 /* lookup into the refs and check the map flag */
643 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
644 if (!appctx->ctx.map.ref ||
645 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
646 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
647 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
648 else
649 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
650 }
651 next_gen = pat_ref_newgen(appctx->ctx.map.ref);
652 return cli_dynmsg(appctx, LOG_INFO, memprintf(&msg, "New version created: %u\n", next_gen));
653 }
654
655 return 0;
656}
657
Emeric Brun8d85aa42017-06-29 15:40:33 +0200658static void cli_release_show_map(struct appctx *appctx)
659{
660 if (appctx->st2 == STAT_ST_LIST) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100661 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200662 if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users))
Willy Tarreau2b718102021-04-21 07:32:39 +0200663 LIST_DELETE(&appctx->ctx.map.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100664 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Emeric Brun8d85aa42017-06-29 15:40:33 +0200665 }
666}
667
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200668static int cli_parse_show_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100669{
670 if (strcmp(args[1], "map") == 0 ||
671 strcmp(args[1], "acl") == 0) {
Willy Tarreau95f753e2021-04-30 12:09:54 +0200672 const char *gen = NULL;
William Lallemandad8be612016-11-18 19:26:17 +0100673
674 /* Set ACL or MAP flags. */
675 if (args[1][0] == 'm')
676 appctx->ctx.map.display_flags = PAT_REF_MAP;
677 else
678 appctx->ctx.map.display_flags = PAT_REF_ACL;
679
680 /* no parameter: display all map available */
681 if (!*args[2]) {
William Lallemandad8be612016-11-18 19:26:17 +0100682 appctx->io_handler = cli_io_handler_pats_list;
683 return 0;
684 }
685
Willy Tarreau95f753e2021-04-30 12:09:54 +0200686 /* For both "map" and "acl" we may have an optional generation
687 * number specified using a "@" character before the pattern
688 * file name.
689 */
690 if (*args[2] == '@') {
691 gen = args[2] + 1;
692 args++;
693 }
694
William Lallemandad8be612016-11-18 19:26:17 +0100695 /* lookup into the refs and check the map flag */
696 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
697 if (!appctx->ctx.map.ref ||
698 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200699 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
700 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
701 else
702 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100703 }
Willy Tarreau95f753e2021-04-30 12:09:54 +0200704
705 /* set the desired generation id in cli.i0 */
706 if (gen)
707 appctx->ctx.cli.i0 = str2uic(gen);
708 else
709 appctx->ctx.cli.i0 = appctx->ctx.map.ref->curr_gen;
710
Willy Tarreau1ae0c432022-05-03 15:26:27 +0200711 LIST_INIT(&appctx->ctx.map.bref.users);
William Lallemandad8be612016-11-18 19:26:17 +0100712 appctx->io_handler = cli_io_handler_pat_list;
Emeric Brun8d85aa42017-06-29 15:40:33 +0200713 appctx->io_release = cli_release_show_map;
William Lallemandad8be612016-11-18 19:26:17 +0100714 return 0;
715 }
716
717 return 0;
718}
719
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200720static int cli_parse_set_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100721{
722 if (strcmp(args[1], "map") == 0) {
723 char *err;
724
725 /* Set flags. */
726 appctx->ctx.map.display_flags = PAT_REF_MAP;
727
728 /* Expect three parameters: map name, key and new value. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200729 if (!*args[2] || !*args[3] || !*args[4])
730 return cli_err(appctx, "'set map' expects three parameters: map identifier, key and value.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100731
732 /* Lookup the reference in the maps. */
733 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +0200734 if (!appctx->ctx.map.ref)
735 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100736
737 /* If the entry identifier start with a '#', it is considered as
738 * pointer id
739 */
740 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
741 struct pat_ref_elt *ref;
742 long long int conv;
743 char *error;
744
745 /* Convert argument to integer value. */
746 conv = strtoll(&args[3][1], &error, 16);
Willy Tarreau9d008692019-08-09 11:21:01 +0200747 if (*error != '\0')
748 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100749
750 /* Convert and check integer to pointer. */
751 ref = (struct pat_ref_elt *)(long)conv;
Willy Tarreau9d008692019-08-09 11:21:01 +0200752 if ((long long int)(long)ref != conv)
753 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100754
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200755 /* Try to modify the entry. */
William Lallemandad8be612016-11-18 19:26:17 +0100756 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100757 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100758 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100759 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +0200760 if (err)
761 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
762 else
763 return cli_err(appctx, "Failed to update an entry.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100764 }
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 else {
768 /* Else, use the entry identifier as pattern
769 * string, and update the value.
770 */
771 err = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100772 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100773 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100774 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +0200775 if (err)
776 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
777 else
778 return cli_err(appctx, "Failed to update an entry.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100779 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100780 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100781 }
782
783 /* The set is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100784 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100785 return 0;
786 }
787 return 1;
788}
789
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200790static int cli_parse_add_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100791{
792 if (strcmp(args[1], "map") == 0 ||
793 strcmp(args[1], "acl") == 0) {
Willy Tarreaubb51c442021-04-30 15:23:36 +0200794 const char *gen = NULL;
795 uint genid = 0;
William Lallemandad8be612016-11-18 19:26:17 +0100796 int ret;
797 char *err;
798
799 /* Set flags. */
800 if (args[1][0] == 'm')
801 appctx->ctx.map.display_flags = PAT_REF_MAP;
802 else
803 appctx->ctx.map.display_flags = PAT_REF_ACL;
804
Willy Tarreaubb51c442021-04-30 15:23:36 +0200805 /* For both "map" and "acl" we may have an optional generation
806 * number specified using a "@" character before the pattern
807 * file name.
808 */
809 if (*args[2] == '@') {
810 gen = args[2] + 1;
811 args++;
812 }
813
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200814 /* If the keyword is "map", we expect:
815 * - three parameters if there is no payload
816 * - one parameter if there is a payload
817 * If it is "acl", we expect only two parameters
William Lallemandad8be612016-11-18 19:26:17 +0100818 */
819 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200820 if ((!payload && (!*args[2] || !*args[3] || !*args[4])) ||
Willy Tarreau9d008692019-08-09 11:21:01 +0200821 (payload && !*args[2]))
822 return cli_err(appctx,
823 "'add map' expects three parameters (map identifier, key and value)"
824 " or one parameter (map identifier) and a payload\n");
William Lallemandad8be612016-11-18 19:26:17 +0100825 }
Willy Tarreau9d008692019-08-09 11:21:01 +0200826 else if (!*args[2] || !*args[3])
827 return cli_err(appctx, "'add acl' expects two parameters: ACL identifier and pattern.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100828
829 /* Lookup for the reference. */
830 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
831 if (!appctx->ctx.map.ref) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200832 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
833 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
834 else
835 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100836 }
837
Willy Tarreaubb51c442021-04-30 15:23:36 +0200838 if (gen) {
839 genid = str2uic(gen);
840 if ((int)(genid - appctx->ctx.map.ref->next_gen) > 0) {
841 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
842 return cli_err(appctx, "Version number in the future, please use 'prepare map' before.\n");
843 else
844 return cli_err(appctx, "Version number in the future, please use 'prepare acl' before.\n");
845 }
846 }
847
William Lallemandad8be612016-11-18 19:26:17 +0100848 /* The command "add acl" is prohibited if the reference
849 * use samples.
850 */
851 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
852 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
Willy Tarreau9d008692019-08-09 11:21:01 +0200853 return cli_err(appctx,
854 "This ACL is shared with a map containing samples. "
855 "You must use the command 'add map' to add values.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100856 }
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200857
858 /* Add value(s). If no payload is used, key and value are read
859 * from the command line and only one key is set. If a payload
860 * is passed, one key/value pair is read per line till the end
861 * of the payload is reached.
862 */
William Lallemandad8be612016-11-18 19:26:17 +0100863 err = NULL;
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200864
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200865 do {
866 char *key = args[3];
867 char *value = args[4];
868 size_t l;
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200869
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200870 if (payload) {
871 /* key and value passed as payload, one pair per line */
872 if (!*payload)
873 break;
874
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200875 key = payload;
876 l = strcspn(key, " \t");
877 payload += l;
878
Willy Tarreau9d008692019-08-09 11:21:01 +0200879 if (!*payload && appctx->ctx.map.display_flags == PAT_REF_MAP)
880 return cli_dynerr(appctx, memprintf(&err, "Missing value for key '%s'.\n", key));
881
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200882 key[l] = 0;
883 payload++;
884
885 /* value */
886 payload += strspn(payload, " \t");
887 value = payload;
888 l = strcspn(value, "\n");
889 payload += l;
890 if (*payload)
891 payload++;
892 value[l] = 0;
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200893 }
Aurélien Nephtali25650ce2018-04-18 14:04:47 +0200894
Willy Tarreau4053b032021-04-29 16:55:17 +0200895 if (appctx->ctx.map.display_flags != PAT_REF_MAP)
896 value = NULL;
897
898 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreaubb51c442021-04-30 15:23:36 +0200899 ret = !!pat_ref_load(appctx->ctx.map.ref, gen ? genid : appctx->ctx.map.ref->curr_gen, key, value, -1, &err);
Willy Tarreau4053b032021-04-29 16:55:17 +0200900 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
901
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200902 if (!ret) {
903 if (err)
904 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
905 else
906 return cli_err(appctx, "Failed to add a key.\n");
Aurélien Nephtali9a4da682018-04-16 19:02:42 +0200907 }
Willy Tarreauf7dd0e82021-04-29 16:02:48 +0200908 } while (payload && *payload);
William Lallemandad8be612016-11-18 19:26:17 +0100909
910 /* The add is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100911 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100912 return 1;
913 }
914
915 return 0;
916}
917
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200918static int cli_parse_del_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +0100919{
920 if (args[1][0] == 'm')
921 appctx->ctx.map.display_flags = PAT_REF_MAP;
922 else
923 appctx->ctx.map.display_flags = PAT_REF_ACL;
924
925 /* Expect two parameters: map name and key. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200926 if (!*args[2] || !*args[3]) {
927 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
928 return cli_err(appctx, "This command expects two parameters: map identifier and key.\n");
929 else
930 return cli_err(appctx, "This command expects two parameters: ACL identifier and key.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100931 }
932
933 /* Lookup the reference in the maps. */
934 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
935 if (!appctx->ctx.map.ref ||
Willy Tarreau9d008692019-08-09 11:21:01 +0200936 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags))
937 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100938
939 /* If the entry identifier start with a '#', it is considered as
940 * pointer id
941 */
942 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
943 struct pat_ref_elt *ref;
944 long long int conv;
945 char *error;
946
947 /* Convert argument to integer value. */
948 conv = strtoll(&args[3][1], &error, 16);
Willy Tarreau9d008692019-08-09 11:21:01 +0200949 if (*error != '\0')
950 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100951
952 /* Convert and check integer to pointer. */
953 ref = (struct pat_ref_elt *)(long)conv;
Willy Tarreau9d008692019-08-09 11:21:01 +0200954 if ((long long int)(long)ref != conv)
955 return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100956
957 /* Try to delete the entry. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100958 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100959 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100960 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100961 /* The entry is not found, send message. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200962 return cli_err(appctx, "Key not found.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100963 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100964 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100965 }
966 else {
967 /* Else, use the entry identifier as pattern
968 * string and try to delete the entry.
969 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100970 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100971 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100972 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100973 /* The entry is not found, send message. */
Willy Tarreau9d008692019-08-09 11:21:01 +0200974 return cli_err(appctx, "Key not found.\n");
William Lallemandad8be612016-11-18 19:26:17 +0100975 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100976 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
William Lallemandad8be612016-11-18 19:26:17 +0100977 }
978
979 /* The deletion is done, send message. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100980 appctx->st0 = CLI_ST_PROMPT;
William Lallemandad8be612016-11-18 19:26:17 +0100981 return 1;
982}
983
Willy Tarreauff3feeb2021-04-30 13:31:43 +0200984/* continue to clear a map which was started in the parser. The range of
985 * generations this applies to is taken from appctx->ctx.cli.i0 for the oldest
986 * and appctx->ctx.cli.i1 for the latest.
987 */
Willy Tarreaud1d005d2019-12-20 18:22:02 +0100988static int cli_io_handler_clear_map(struct appctx *appctx)
989{
Willy Tarreaud1d005d2019-12-20 18:22:02 +0100990 int finished;
991
992 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
Willy Tarreauff3feeb2021-04-30 13:31:43 +0200993 finished = pat_ref_purge_range(appctx->ctx.map.ref, appctx->ctx.cli.i0, appctx->ctx.cli.i1, 100);
Willy Tarreaud1d005d2019-12-20 18:22:02 +0100994 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
995
996 if (!finished) {
997 /* let's come back later */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200998 cs_rx_endp_more(appctx->owner);
Willy Tarreaud1d005d2019-12-20 18:22:02 +0100999 return 0;
1000 }
1001 return 1;
1002}
1003
Willy Tarreauff3feeb2021-04-30 13:31:43 +02001004/* note: sets appctx->ctx.cli.i0 and appctx->ctx.cli.i1 to the oldest and
1005 * latest generations to clear, respectively, and will call the clear_map
1006 * handler.
1007 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001008static int cli_parse_clear_map(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandad8be612016-11-18 19:26:17 +01001009{
1010 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
Willy Tarreauff3feeb2021-04-30 13:31:43 +02001011 const char *gen = NULL;
1012
William Lallemandad8be612016-11-18 19:26:17 +01001013 /* Set ACL or MAP flags. */
1014 if (args[1][0] == 'm')
1015 appctx->ctx.map.display_flags = PAT_REF_MAP;
1016 else
1017 appctx->ctx.map.display_flags = PAT_REF_ACL;
1018
Willy Tarreauff3feeb2021-04-30 13:31:43 +02001019 /* For both "map" and "acl" we may have an optional generation
1020 * number specified using a "@" character before the pattern
1021 * file name.
1022 */
1023 if (*args[2] == '@') {
1024 gen = args[2] + 1;
1025 args++;
1026 }
1027
William Lallemandad8be612016-11-18 19:26:17 +01001028 /* no parameter */
1029 if (!*args[2]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02001030 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1031 return cli_err(appctx, "Missing map identifier.\n");
1032 else
1033 return cli_err(appctx, "Missing ACL identifier.\n");
William Lallemandad8be612016-11-18 19:26:17 +01001034 }
1035
1036 /* lookup into the refs and check the map flag */
1037 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
1038 if (!appctx->ctx.map.ref ||
1039 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02001040 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1041 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
1042 else
1043 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
William Lallemandad8be612016-11-18 19:26:17 +01001044 }
1045
Willy Tarreauff3feeb2021-04-30 13:31:43 +02001046 /* set the desired generation id in cli.i0/i1 */
1047 if (gen)
1048 appctx->ctx.cli.i1 = appctx->ctx.cli.i0 = str2uic(gen);
1049 else
1050 appctx->ctx.cli.i1 = appctx->ctx.cli.i0 = appctx->ctx.map.ref->curr_gen;
1051
Willy Tarreaud1d005d2019-12-20 18:22:02 +01001052 /* delegate the clearing to the I/O handler which can yield */
1053 return 0;
William Lallemandad8be612016-11-18 19:26:17 +01001054 }
Willy Tarreaud1d005d2019-12-20 18:22:02 +01001055 return 1;
William Lallemandad8be612016-11-18 19:26:17 +01001056}
1057
Willy Tarreau7a562ca2021-04-30 15:10:01 +02001058/* note: sets appctx->ctx.cli.i0 and appctx->ctx.cli.i1 to the oldest and
1059 * latest generations to clear, respectively, and will call the clear_map
1060 * handler.
1061 */
1062static int cli_parse_commit_map(char **args, char *payload, struct appctx *appctx, void *private)
1063{
1064 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1065 const char *gen = NULL;
1066 uint genid;
1067 uint ret;
1068
1069 /* Set ACL or MAP flags. */
1070 if (args[1][0] == 'm')
1071 appctx->ctx.map.display_flags = PAT_REF_MAP;
1072 else
1073 appctx->ctx.map.display_flags = PAT_REF_ACL;
1074
1075 if (*args[2] != '@')
1076 return cli_err(appctx, "Missing version number.\n");
1077
1078 /* The generation number is mandatory for a commit. The range
1079 * of generations that get trashed by a commit starts from the
1080 * opposite of the current one and ends at the previous one.
1081 */
1082 gen = args[2] + 1;
1083 genid = str2uic(gen);
1084 appctx->ctx.cli.i1 = genid - 1;
1085 appctx->ctx.cli.i0 = appctx->ctx.cli.i1 - ((~0U) >> 1);
1086
1087 /* no parameter */
1088 if (!*args[3]) {
1089 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1090 return cli_err(appctx, "Missing map identifier.\n");
1091 else
1092 return cli_err(appctx, "Missing ACL identifier.\n");
1093 }
1094
1095 /* lookup into the refs and check the map flag */
1096 appctx->ctx.map.ref = pat_ref_lookup_ref(args[3]);
1097 if (!appctx->ctx.map.ref ||
1098 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1099 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1100 return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n");
1101 else
1102 return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n");
1103 }
1104
1105 HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
1106 if (genid - (appctx->ctx.map.ref->curr_gen + 1) <
1107 appctx->ctx.map.ref->next_gen - appctx->ctx.map.ref->curr_gen)
1108 ret = pat_ref_commit(appctx->ctx.map.ref, genid);
1109 else
1110 ret = 1;
1111 HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock);
1112
1113 if (ret != 0)
1114 return cli_err(appctx, "Version number out of range.\n");
1115
1116 /* delegate the clearing to the I/O handler which can yield */
1117 return 0;
1118 }
1119 return 1;
1120}
1121
William Lallemandad8be612016-11-18 19:26:17 +01001122/* register cli keywords */
1123
1124static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001125 { { "add", "acl", NULL }, "add acl [@<ver>] <acl> <pattern> : add an acl entry", cli_parse_add_map, NULL },
1126 { { "clear", "acl", NULL }, "clear acl [@<ver>] <acl> : clear the contents of this acl", cli_parse_clear_map, cli_io_handler_clear_map, NULL },
1127 { { "commit","acl", NULL }, "commit acl @<ver> <acl> : commit the ACL at this version", cli_parse_commit_map, cli_io_handler_clear_map, NULL },
1128 { { "del", "acl", NULL }, "del acl <acl> [<key>|#<ref>] : delete acl entries matching <key>", cli_parse_del_map, NULL },
1129 { { "get", "acl", NULL }, "get acl <acl> <value> : report the patterns matching a sample for an ACL", cli_parse_get_map, cli_io_handler_map_lookup, cli_release_mlook },
1130 { { "prepare","acl",NULL }, "prepare acl <acl> : prepare a new version for atomic ACL replacement", cli_parse_prepare_map, NULL },
1131 { { "show", "acl", NULL }, "show acl [@<ver>] <acl>] : report available acls or dump an acl's contents", cli_parse_show_map, NULL },
1132 { { "add", "map", NULL }, "add map [@<ver>] <map> <key> <val> : add a map entry (payload supported instead of key/val)", cli_parse_add_map, NULL },
1133 { { "clear", "map", NULL }, "clear map [@<ver>] <map> : clear the contents of this map", cli_parse_clear_map, cli_io_handler_clear_map, NULL },
1134 { { "commit","map", NULL }, "commit map @<ver> <map> : commit the map at this version", cli_parse_commit_map, cli_io_handler_clear_map, NULL },
1135 { { "del", "map", NULL }, "del map <map> [<key>|#<ref>] : delete map entries matching <key>", cli_parse_del_map, NULL },
1136 { { "get", "map", NULL }, "get map <acl> <value> : report the keys and values matching a sample for a map", cli_parse_get_map, cli_io_handler_map_lookup, cli_release_mlook },
1137 { { "prepare","map",NULL }, "prepare map <acl> : prepare a new version for atomic map replacement", cli_parse_prepare_map, NULL },
1138 { { "set", "map", NULL }, "set map <map> [<key>|#<ref>] <value> : modify a map entry", cli_parse_set_map, NULL },
1139 { { "show", "map", NULL }, "show map [@ver] [map] : report available maps or dump a map's contents", cli_parse_show_map, NULL },
William Lallemandad8be612016-11-18 19:26:17 +01001140 { { NULL }, NULL, NULL, NULL }
1141}};
1142
Willy Tarreau0108d902018-11-25 19:14:37 +01001143INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemandad8be612016-11-18 19:26:17 +01001144
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +01001145/* Note: must not be declared <const> as its list will be overwritten
1146 *
1147 * For the map_*_int keywords, the output is declared as SMP_T_UINT, but the converter function
1148 * can provide SMP_T_UINT, SMP_T_SINT or SMP_T_BOOL depending on how the patterns found in the
1149 * file can be parsed.
1150 *
1151 * For the map_*_ip keyword, the output is declared as SMP_T_IPV4, but the converter function
1152 * can provide SMP_T_IPV4 or SMP_T_IPV6 depending on the patterns found in the file.
1153 *
1154 * The map_* keywords only emit strings.
1155 *
1156 * The output type is only used during the configuration parsing. It is used for detecting
1157 * compatibility problems.
1158 *
1159 * The arguments are: <file>[,<default value>]
1160 */
1161static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER1edc9712014-12-15 16:18:39 +01001162 { "map", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1163 { "map_str", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
1164 { "map_beg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_BEG },
1165 { "map_sub", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_SUB },
1166 { "map_dir", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DIR },
1167 { "map_dom", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DOM },
1168 { "map_end", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_END },
1169 { "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 +01001170 { "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 +02001171 { "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 +01001172 { "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 +01001173
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001174 { "map_str_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_STR },
1175 { "map_beg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_BEG },
1176 { "map_sub_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_SUB },
1177 { "map_dir_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DIR },
1178 { "map_dom_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DOM },
1179 { "map_end_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_END },
1180 { "map_reg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_REG },
1181 { "map_int_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_SINT, SMP_T_SINT, (void *)PAT_MATCH_INT },
1182 { "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 +01001183
Thierry FOURNIERb2f8f082015-08-04 19:35:46 +02001184 { "map_str_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_STR },
1185 { "map_beg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_BEG },
1186 { "map_sub_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_SUB },
1187 { "map_dir_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DIR },
1188 { "map_dom_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DOM },
1189 { "map_end_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_END },
1190 { "map_reg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_REG },
1191 { "map_int_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_SINT, SMP_T_ADDR, (void *)PAT_MATCH_INT },
1192 { "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 +01001193
1194 { /* END */ },
1195}};
1196
Willy Tarreau0108d902018-11-25 19:14:37 +01001197INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);