Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 16 | #include <common/initcall.h> |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 17 | #include <common/standard.h> |
| 18 | |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 19 | #include <types/applet.h> |
| 20 | #include <types/cli.h> |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 21 | #include <types/global.h> |
| 22 | #include <types/map.h> |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 23 | #include <types/pattern.h> |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 24 | #include <types/stats.h> |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 25 | |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 26 | #include <proto/applet.h> |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 27 | #include <proto/arg.h> |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 28 | #include <proto/cli.h> |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 29 | #include <proto/log.h> |
Thierry FOURNIER | b0c0a0f | 2013-12-10 15:05:34 +0100 | [diff] [blame] | 30 | #include <proto/map.h> |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 31 | #include <proto/pattern.h> |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 32 | #include <proto/stream_interface.h> |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 33 | #include <proto/sample.h> |
| 34 | |
Thierry FOURNIER | b2f8f08 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 35 | /* Parse an IPv4 or IPv6 address and store it into the sample. |
| 36 | * The output type is IPv4 or IPv6. |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 37 | */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 38 | int map_parse_ip(const char *text, struct sample_data *data) |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 39 | { |
Thierry FOURNIER | b2f8f08 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 40 | int len = strlen(text); |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 41 | |
Thierry FOURNIER | b2f8f08 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 42 | if (buf2ip(text, len, &data->u.ipv4)) { |
| 43 | data->type = SMP_T_IPV4; |
| 44 | return 1; |
| 45 | } |
| 46 | if (buf2ip6(text, len, &data->u.ipv6)) { |
| 47 | data->type = SMP_T_IPV6; |
| 48 | return 1; |
| 49 | } |
| 50 | return 0; |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /* Parse a string and store a pointer to it into the sample. The original |
| 54 | * string must be left in memory because we return a direct memory reference. |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 55 | * The output type is SMP_T_STR. There is no risk that the data will be |
| 56 | * overwritten because sample_conv_map() makes a const sample with this |
| 57 | * output. |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 58 | */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 59 | int map_parse_str(const char *text, struct sample_data *data) |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 60 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 61 | data->u.str.area = (char *)text; |
| 62 | data->u.str.data = strlen(text); |
| 63 | data->u.str.size = data->u.str.data + 1; |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 64 | data->type = SMP_T_STR; |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 65 | return 1; |
| 66 | } |
| 67 | |
| 68 | /* Parse an integer and convert it to a sample. The output type is SINT if the |
| 69 | * number is negative, or UINT if it is positive or null. The function returns |
| 70 | * zero (error) if the number is too large. |
| 71 | */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 72 | int map_parse_int(const char *text, struct sample_data *data) |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 73 | { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 74 | data->type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 75 | data->u.sint = read_int64(&text, text + strlen(text)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 76 | if (*text != '\0') |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 77 | return 0; |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 78 | return 1; |
| 79 | } |
| 80 | |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 81 | /* This crete and initialize map descriptor. |
| 82 | * Return NULL if out of memory error |
| 83 | */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 84 | static struct map_descriptor *map_create_descriptor(struct sample_conv *conv) |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 85 | { |
| 86 | struct map_descriptor *desc; |
| 87 | |
| 88 | desc = calloc(1, sizeof(*desc)); |
| 89 | if (!desc) |
| 90 | return NULL; |
| 91 | |
| 92 | desc->conv = conv; |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 93 | |
| 94 | return desc; |
| 95 | } |
| 96 | |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 97 | /* This function load the map file according with data type declared into |
| 98 | * the "struct sample_conv". |
| 99 | * |
| 100 | * This function choose the indexation type (ebtree or list) according with |
| 101 | * the type of match needed. |
| 102 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 103 | int sample_load_map(struct arg *arg, struct sample_conv *conv, |
| 104 | const char *file, int line, char **err) |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 105 | { |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 106 | struct map_descriptor *desc; |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 107 | |
| 108 | /* create new map descriptor */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 109 | desc = map_create_descriptor(conv); |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 110 | if (!desc) { |
| 111 | memprintf(err, "out of memory"); |
| 112 | return 0; |
| 113 | } |
| 114 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 115 | /* 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 FOURNIER | 1edc971 | 2014-12-15 16:18:39 +0100 | [diff] [blame] | 122 | 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]; |
| 125 | desc->pat.delete = pat_delete_fcts[(long)conv->private]; |
| 126 | desc->pat.prune = pat_prune_fcts[(long)conv->private]; |
| 127 | desc->pat.expect_type = pat_match_types[(long)conv->private]; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 128 | |
| 129 | /* Set the output parse method. */ |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 130 | switch (desc->conv->out_type) { |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 131 | case SMP_T_STR: desc->pat.parse_smp = map_parse_str; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 132 | case SMP_T_SINT: desc->pat.parse_smp = map_parse_int; break; |
Thierry FOURNIER | b2f8f08 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 133 | case SMP_T_ADDR: desc->pat.parse_smp = map_parse_ip; break; |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 134 | default: |
| 135 | memprintf(err, "map: internal haproxy error: no default parse case for the input type <%d>.", |
| 136 | conv->out_type); |
Andreas Seltenreich | 78f3595 | 2016-03-03 20:32:23 +0100 | [diff] [blame] | 137 | free(desc); |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 141 | /* Load map. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 142 | if (!pattern_read_from_file(&desc->pat, PAT_REF_MAP, arg[0].data.str.area, PAT_MF_NO_DNS, |
Thierry FOURNIER | 94580c9 | 2014-02-11 14:36:45 +0100 | [diff] [blame] | 143 | 1, err, file, line)) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 144 | return 0; |
Thierry FOURNIER | 0ffe78c | 2013-12-05 14:40:25 +0100 | [diff] [blame] | 145 | |
Willy Tarreau | aa5801b | 2019-04-19 11:35:22 +0200 | [diff] [blame] | 146 | /* the maps of type IP support a string as default value. This |
| 147 | * string can be an ipv4 or an ipv6, we must convert it. |
Thierry FOURNIER | b2f8f08 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 148 | */ |
Willy Tarreau | aa5801b | 2019-04-19 11:35:22 +0200 | [diff] [blame] | 149 | if (arg[1].type != ARGT_STOP && desc->conv->out_type == SMP_T_ADDR) { |
Thierry FOURNIER | b2f8f08 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 150 | struct sample_data data; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 151 | if (!map_parse_ip(arg[1].data.str.area, &data)) { |
| 152 | memprintf(err, "map: cannot parse default ip <%s>.", |
| 153 | arg[1].data.str.area); |
Thierry FOURNIER | b2f8f08 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | 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 FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 165 | /* replace the first argument by this definition */ |
| 166 | arg[0].type = ARGT_MAP; |
| 167 | arg[0].data.map = desc; |
| 168 | |
| 169 | return 1; |
| 170 | } |
| 171 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 172 | static int sample_conv_map(const struct arg *arg_p, struct sample *smp, void *private) |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 173 | { |
| 174 | struct map_descriptor *desc; |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 175 | struct pattern *pat; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 176 | struct buffer *str; |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 177 | |
| 178 | /* get config */ |
| 179 | desc = arg_p[0].data.map; |
| 180 | |
| 181 | /* Execute the match function. */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 182 | pat = pattern_exec_match(&desc->pat, smp, 1); |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 183 | |
| 184 | /* Match case. */ |
| 185 | if (pat) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 186 | if (pat->data) { |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 187 | /* In the regm case, merge the sample with the input. */ |
| 188 | if ((long)private == PAT_MATCH_REGM) { |
Emeric Brun | 2710221 | 2018-07-17 09:47:07 -0400 | [diff] [blame] | 189 | struct buffer *tmptrash; |
Willy Tarreau | 2842e05 | 2018-08-22 04:55:43 +0200 | [diff] [blame] | 190 | int len; |
Emeric Brun | 2710221 | 2018-07-17 09:47:07 -0400 | [diff] [blame] | 191 | |
| 192 | /* Copy the content of the sample because it could |
| 193 | be scratched by incoming get_trash_chunk */ |
| 194 | tmptrash = alloc_trash_chunk(); |
| 195 | if (!tmptrash) |
| 196 | return 0; |
| 197 | |
| 198 | tmptrash->data = smp->data.u.str.data; |
| 199 | if (tmptrash->data > (tmptrash->size-1)) |
| 200 | tmptrash->data = tmptrash->size-1; |
| 201 | |
| 202 | memcpy(tmptrash->area, smp->data.u.str.area, tmptrash->data); |
| 203 | tmptrash->area[tmptrash->data] = 0; |
| 204 | |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 205 | str = get_trash_chunk(); |
Willy Tarreau | 2842e05 | 2018-08-22 04:55:43 +0200 | [diff] [blame] | 206 | len = exp_replace(str->area, str->size, |
| 207 | tmptrash->area, |
| 208 | pat->data->u.str.area, |
| 209 | (regmatch_t *)smp->ctx.a[0]); |
Nenad Merdanovic | 646b774 | 2019-04-12 22:54:28 +0200 | [diff] [blame] | 210 | free_trash_chunk(tmptrash); |
| 211 | |
Willy Tarreau | 2842e05 | 2018-08-22 04:55:43 +0200 | [diff] [blame] | 212 | if (len == -1) |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 213 | return 0; |
Willy Tarreau | 2842e05 | 2018-08-22 04:55:43 +0200 | [diff] [blame] | 214 | |
| 215 | str->data = len; |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 216 | smp->data.u.str = *str; |
| 217 | return 1; |
| 218 | } |
| 219 | /* Copy sample. */ |
Thierry FOURNIER | 5cc18d4 | 2015-08-19 09:02:36 +0200 | [diff] [blame] | 220 | smp->data = *pat->data; |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 221 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 222 | return 1; |
| 223 | } |
| 224 | |
| 225 | /* Return just int sample containing 1. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 226 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 227 | smp->data.u.sint = 1; |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 228 | return 1; |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 229 | } |
| 230 | |
Joseph Herlant | f43b88b | 2018-11-25 11:48:18 -0800 | [diff] [blame] | 231 | /* If no default value available, the converter fails. */ |
Thierry FOURNIER | 933e5de | 2015-03-13 00:10:16 +0100 | [diff] [blame] | 232 | if (arg_p[1].type == ARGT_STOP) |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 233 | return 0; |
| 234 | |
| 235 | /* Return the default value. */ |
Thierry FOURNIER | 933e5de | 2015-03-13 00:10:16 +0100 | [diff] [blame] | 236 | switch (desc->conv->out_type) { |
| 237 | |
| 238 | case SMP_T_STR: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 239 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 933e5de | 2015-03-13 00:10:16 +0100 | [diff] [blame] | 240 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 241 | smp->data.u.str = arg_p[1].data.str; |
Thierry FOURNIER | 933e5de | 2015-03-13 00:10:16 +0100 | [diff] [blame] | 242 | break; |
| 243 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 244 | case SMP_T_SINT: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 245 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 246 | smp->data.u.sint = arg_p[1].data.sint; |
Thierry FOURNIER | 933e5de | 2015-03-13 00:10:16 +0100 | [diff] [blame] | 247 | break; |
| 248 | |
Thierry FOURNIER | b2f8f08 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 249 | case SMP_T_ADDR: |
| 250 | if (arg_p[1].type == ARGT_IPV4) { |
| 251 | smp->data.type = SMP_T_IPV4; |
| 252 | smp->data.u.ipv4 = arg_p[1].data.ipv4; |
| 253 | } else { |
| 254 | smp->data.type = SMP_T_IPV6; |
| 255 | smp->data.u.ipv6 = arg_p[1].data.ipv6; |
| 256 | } |
Thierry FOURNIER | 933e5de | 2015-03-13 00:10:16 +0100 | [diff] [blame] | 257 | break; |
| 258 | } |
| 259 | |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 260 | return 1; |
| 261 | } |
| 262 | |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 263 | /* This function is used with map and acl management. It permits to browse |
| 264 | * each reference. The variable <getnext> must contain the current node, |
| 265 | * <end> point to the root node and the <flags> permit to filter required |
| 266 | * nodes. |
| 267 | */ |
| 268 | static inline |
| 269 | struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end, |
| 270 | unsigned int flags) |
| 271 | { |
| 272 | struct pat_ref *ref = getnext; |
| 273 | |
| 274 | while (1) { |
| 275 | |
| 276 | /* Get next list entry. */ |
| 277 | ref = LIST_NEXT(&ref->list, struct pat_ref *, list); |
| 278 | |
| 279 | /* If the entry is the last of the list, return NULL. */ |
| 280 | if (&ref->list == end) |
| 281 | return NULL; |
| 282 | |
| 283 | /* If the entry match the flag, return it. */ |
| 284 | if (ref->flags & flags) |
| 285 | return ref; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | static inline |
| 290 | struct pat_ref *pat_ref_lookup_ref(const char *reference) |
| 291 | { |
| 292 | int id; |
| 293 | char *error; |
| 294 | |
| 295 | /* If the reference starts by a '#', this is numeric id. */ |
| 296 | if (reference[0] == '#') { |
| 297 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 298 | id = strtol(reference + 1, &error, 10); |
| 299 | if (*error != '\0') |
| 300 | return NULL; |
| 301 | |
| 302 | /* Perform the unique id lookup. */ |
| 303 | return pat_ref_lookupid(id); |
| 304 | } |
| 305 | |
| 306 | /* Perform the string lookup. */ |
| 307 | return pat_ref_lookup(reference); |
| 308 | } |
| 309 | |
| 310 | /* This function is used with map and acl management. It permits to browse |
| 311 | * each reference. |
| 312 | */ |
| 313 | static inline |
| 314 | struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end) |
| 315 | { |
| 316 | struct pattern_expr *expr; |
| 317 | expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list); |
| 318 | if (&expr->list == end) |
| 319 | return NULL; |
| 320 | return expr; |
| 321 | } |
| 322 | |
| 323 | static int cli_io_handler_pat_list(struct appctx *appctx) |
| 324 | { |
| 325 | struct stream_interface *si = appctx->owner; |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 326 | struct pat_ref_elt *elt; |
| 327 | |
| 328 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) { |
| 329 | /* If we're forced to shut down, we might have to remove our |
| 330 | * reference to the last ref_elt being dumped. |
| 331 | */ |
| 332 | if (appctx->st2 == STAT_ST_LIST) { |
Dragan Dosen | 336a11f | 2018-05-04 16:27:15 +0200 | [diff] [blame] | 333 | if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users)) { |
| 334 | LIST_DEL(&appctx->ctx.map.bref.users); |
| 335 | LIST_INIT(&appctx->ctx.map.bref.users); |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | return 1; |
| 339 | } |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 340 | |
| 341 | switch (appctx->st2) { |
| 342 | |
| 343 | case STAT_ST_INIT: |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 344 | /* the function had not been called yet, let's prepare the |
| 345 | * buffer for a response. We initialize the current stream |
| 346 | * pointer to the first in the global list. When a target |
| 347 | * stream is being destroyed, it is responsible for updating |
| 348 | * this pointer. We know we have reached the end when this |
| 349 | * pointer points back to the head of the streams list. |
| 350 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 351 | HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 352 | LIST_INIT(&appctx->ctx.map.bref.users); |
| 353 | appctx->ctx.map.bref.ref = appctx->ctx.map.ref->head.n; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 354 | HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 355 | appctx->st2 = STAT_ST_LIST; |
| 356 | /* fall through */ |
| 357 | |
| 358 | case STAT_ST_LIST: |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 359 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 360 | HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 361 | |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 362 | if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users)) { |
| 363 | LIST_DEL(&appctx->ctx.map.bref.users); |
| 364 | LIST_INIT(&appctx->ctx.map.bref.users); |
| 365 | } |
| 366 | |
| 367 | while (appctx->ctx.map.bref.ref != &appctx->ctx.map.ref->head) { |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 368 | chunk_reset(&trash); |
| 369 | |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 370 | elt = LIST_ELEM(appctx->ctx.map.bref.ref, struct pat_ref_elt *, list); |
| 371 | |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 372 | /* build messages */ |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 373 | if (elt->sample) |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 374 | chunk_appendf(&trash, "%p %s %s\n", |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 375 | elt, elt->pattern, |
| 376 | elt->sample); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 377 | else |
| 378 | chunk_appendf(&trash, "%p %s\n", |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 379 | elt, elt->pattern); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 380 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 381 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 382 | /* let's try again later from this stream. We add ourselves into |
| 383 | * this stream's users so that it can remove us upon termination. |
| 384 | */ |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 385 | LIST_ADDQ(&elt->back_refs, &appctx->ctx.map.bref.users); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 386 | HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 387 | si_rx_room_blk(si); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | /* get next list entry and check the end of the list */ |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 392 | appctx->ctx.map.bref.ref = elt->list.n; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 393 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 394 | HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 395 | /* fall through */ |
| 396 | |
| 397 | default: |
| 398 | appctx->st2 = STAT_ST_FIN; |
| 399 | return 1; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | static int cli_io_handler_pats_list(struct appctx *appctx) |
| 404 | { |
| 405 | struct stream_interface *si = appctx->owner; |
| 406 | |
| 407 | switch (appctx->st2) { |
| 408 | case STAT_ST_INIT: |
| 409 | /* Display the column headers. If the message cannot be sent, |
Joseph Herlant | f43b88b | 2018-11-25 11:48:18 -0800 | [diff] [blame] | 410 | * quit the function with returning 0. The function is called |
| 411 | * later and restarted at the state "STAT_ST_INIT". |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 412 | */ |
| 413 | chunk_reset(&trash); |
| 414 | chunk_appendf(&trash, "# id (file) description\n"); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 415 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 416 | si_rx_room_blk(si); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | /* Now, we start the browsing of the references lists. |
Joseph Herlant | f43b88b | 2018-11-25 11:48:18 -0800 | [diff] [blame] | 421 | * Note that the following call to LIST_ELEM returns a bad pointer. The only |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 422 | * available field of this pointer is <list>. It is used with the function |
Joseph Herlant | f43b88b | 2018-11-25 11:48:18 -0800 | [diff] [blame] | 423 | * pat_list_get_next() for returning the first available entry |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 424 | */ |
| 425 | appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list); |
| 426 | appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference, |
| 427 | appctx->ctx.map.display_flags); |
| 428 | appctx->st2 = STAT_ST_LIST; |
| 429 | /* fall through */ |
| 430 | |
| 431 | case STAT_ST_LIST: |
| 432 | while (appctx->ctx.map.ref) { |
| 433 | chunk_reset(&trash); |
| 434 | |
| 435 | /* Build messages. If the reference is used by another category than |
Joseph Herlant | f43b88b | 2018-11-25 11:48:18 -0800 | [diff] [blame] | 436 | * the listed categories, display the information in the message. |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 437 | */ |
| 438 | chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id, |
| 439 | appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "", |
| 440 | appctx->ctx.map.ref->display); |
| 441 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 442 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 443 | /* let's try again later from this stream. We add ourselves into |
| 444 | * this stream's users so that it can remove us upon termination. |
| 445 | */ |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 446 | si_rx_room_blk(si); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | /* get next list entry and check the end of the list */ |
| 451 | appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference, |
| 452 | appctx->ctx.map.display_flags); |
| 453 | } |
| 454 | |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 455 | /* fall through */ |
| 456 | |
| 457 | default: |
| 458 | appctx->st2 = STAT_ST_FIN; |
| 459 | return 1; |
| 460 | } |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | static int cli_io_handler_map_lookup(struct appctx *appctx) |
| 465 | { |
| 466 | struct stream_interface *si = appctx->owner; |
| 467 | struct sample sample; |
| 468 | struct pattern *pat; |
| 469 | int match_method; |
| 470 | |
| 471 | switch (appctx->st2) { |
| 472 | case STAT_ST_INIT: |
| 473 | /* Init to the first entry. The list cannot be change */ |
| 474 | appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list); |
| 475 | appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat); |
| 476 | appctx->st2 = STAT_ST_LIST; |
| 477 | /* fall through */ |
| 478 | |
| 479 | case STAT_ST_LIST: |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 480 | HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 481 | /* for each lookup type */ |
| 482 | while (appctx->ctx.map.expr) { |
| 483 | /* initialise chunk to build new message */ |
| 484 | chunk_reset(&trash); |
| 485 | |
| 486 | /* execute pattern matching */ |
| 487 | sample.data.type = SMP_T_STR; |
| 488 | sample.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 489 | sample.data.u.str.data = appctx->ctx.map.chunk.data; |
| 490 | sample.data.u.str.area = appctx->ctx.map.chunk.area; |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 491 | |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 492 | if (appctx->ctx.map.expr->pat_head->match && |
| 493 | sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type)) |
| 494 | pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1); |
| 495 | else |
| 496 | pat = NULL; |
| 497 | |
| 498 | /* build return message: set type of match */ |
| 499 | for (match_method=0; match_method<PAT_MATCH_NUM; match_method++) |
| 500 | if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method]) |
| 501 | break; |
| 502 | if (match_method >= PAT_MATCH_NUM) |
| 503 | chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match); |
| 504 | else |
| 505 | chunk_appendf(&trash, "type=%s", pat_match_names[match_method]); |
| 506 | |
| 507 | /* case sensitive */ |
| 508 | if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE) |
| 509 | chunk_appendf(&trash, ", case=insensitive"); |
| 510 | else |
| 511 | chunk_appendf(&trash, ", case=sensitive"); |
| 512 | |
| 513 | /* Display no match, and set default value */ |
| 514 | if (!pat) { |
| 515 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 516 | chunk_appendf(&trash, ", found=no"); |
| 517 | else |
| 518 | chunk_appendf(&trash, ", match=no"); |
| 519 | } |
| 520 | |
| 521 | /* Display match and match info */ |
| 522 | else { |
| 523 | /* display match */ |
| 524 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 525 | chunk_appendf(&trash, ", found=yes"); |
| 526 | else |
| 527 | chunk_appendf(&trash, ", match=yes"); |
| 528 | |
| 529 | /* display index mode */ |
| 530 | if (pat->sflags & PAT_SF_TREE) |
| 531 | chunk_appendf(&trash, ", idx=tree"); |
| 532 | else |
| 533 | chunk_appendf(&trash, ", idx=list"); |
| 534 | |
| 535 | /* display pattern */ |
| 536 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) { |
| 537 | if (pat->ref && pat->ref->pattern) |
| 538 | chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern); |
| 539 | else |
| 540 | chunk_appendf(&trash, ", key=unknown"); |
| 541 | } |
| 542 | else { |
| 543 | if (pat->ref && pat->ref->pattern) |
| 544 | chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern); |
| 545 | else |
| 546 | chunk_appendf(&trash, ", pattern=unknown"); |
| 547 | } |
| 548 | |
| 549 | /* display return value */ |
| 550 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) { |
| 551 | if (pat->data && pat->ref && pat->ref->sample) |
| 552 | chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample, |
| 553 | smp_to_type[pat->data->type]); |
| 554 | else |
| 555 | chunk_appendf(&trash, ", value=none"); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | chunk_appendf(&trash, "\n"); |
| 560 | |
| 561 | /* display response */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 562 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 563 | /* let's try again later from this stream. We add ourselves into |
| 564 | * this stream's users so that it can remove us upon termination. |
| 565 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 566 | HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 567 | si_rx_room_blk(si); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | /* get next entry */ |
| 572 | appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, |
| 573 | &appctx->ctx.map.ref->pat); |
| 574 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 575 | HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 576 | /* fall through */ |
| 577 | |
| 578 | default: |
| 579 | appctx->st2 = STAT_ST_FIN; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 580 | return 1; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | static void cli_release_mlook(struct appctx *appctx) |
| 585 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 586 | free(appctx->ctx.map.chunk.area); |
| 587 | appctx->ctx.map.chunk.area = NULL; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 591 | static int cli_parse_get_map(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 592 | { |
| 593 | if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) { |
| 594 | /* Set flags. */ |
| 595 | if (args[1][0] == 'm') |
| 596 | appctx->ctx.map.display_flags = PAT_REF_MAP; |
| 597 | else |
| 598 | appctx->ctx.map.display_flags = PAT_REF_ACL; |
| 599 | |
| 600 | /* No parameter. */ |
| 601 | if (!*args[2] || !*args[3]) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 602 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 603 | return cli_err(appctx, "Missing map identifier and/or key.\n"); |
| 604 | else |
| 605 | return cli_err(appctx, "Missing ACL identifier and/or key.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | /* lookup into the maps */ |
| 609 | appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); |
| 610 | if (!appctx->ctx.map.ref) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 611 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 612 | return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n"); |
| 613 | else |
| 614 | return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /* copy input string. The string must be allocated because |
| 618 | * it may be used over multiple iterations. It's released |
| 619 | * at the end and upon abort anyway. |
| 620 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 621 | appctx->ctx.map.chunk.data = strlen(args[3]); |
| 622 | appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.data + 1; |
| 623 | appctx->ctx.map.chunk.area = strdup(args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 624 | if (!appctx->ctx.map.chunk.area) |
| 625 | return cli_err(appctx, "Out of memory error.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 626 | |
| 627 | return 0; |
| 628 | } |
| 629 | return 1; |
| 630 | } |
| 631 | |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 632 | static void cli_release_show_map(struct appctx *appctx) |
| 633 | { |
| 634 | if (appctx->st2 == STAT_ST_LIST) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 635 | HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 636 | if (!LIST_ISEMPTY(&appctx->ctx.map.bref.users)) |
| 637 | LIST_DEL(&appctx->ctx.map.bref.users); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 638 | HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 642 | static int cli_parse_show_map(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 643 | { |
| 644 | if (strcmp(args[1], "map") == 0 || |
| 645 | strcmp(args[1], "acl") == 0) { |
| 646 | |
| 647 | /* Set ACL or MAP flags. */ |
| 648 | if (args[1][0] == 'm') |
| 649 | appctx->ctx.map.display_flags = PAT_REF_MAP; |
| 650 | else |
| 651 | appctx->ctx.map.display_flags = PAT_REF_ACL; |
| 652 | |
| 653 | /* no parameter: display all map available */ |
| 654 | if (!*args[2]) { |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 655 | appctx->io_handler = cli_io_handler_pats_list; |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | /* lookup into the refs and check the map flag */ |
| 660 | appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); |
| 661 | if (!appctx->ctx.map.ref || |
| 662 | !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 663 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 664 | return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n"); |
| 665 | else |
| 666 | return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 667 | } |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 668 | appctx->io_handler = cli_io_handler_pat_list; |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 669 | appctx->io_release = cli_release_show_map; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | return 0; |
| 674 | } |
| 675 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 676 | static int cli_parse_set_map(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 677 | { |
| 678 | if (strcmp(args[1], "map") == 0) { |
| 679 | char *err; |
| 680 | |
| 681 | /* Set flags. */ |
| 682 | appctx->ctx.map.display_flags = PAT_REF_MAP; |
| 683 | |
| 684 | /* Expect three parameters: map name, key and new value. */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 685 | if (!*args[2] || !*args[3] || !*args[4]) |
| 686 | return cli_err(appctx, "'set map' expects three parameters: map identifier, key and value.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 687 | |
| 688 | /* Lookup the reference in the maps. */ |
| 689 | appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 690 | if (!appctx->ctx.map.ref) |
| 691 | return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 692 | |
| 693 | /* If the entry identifier start with a '#', it is considered as |
| 694 | * pointer id |
| 695 | */ |
| 696 | if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') { |
| 697 | struct pat_ref_elt *ref; |
| 698 | long long int conv; |
| 699 | char *error; |
| 700 | |
| 701 | /* Convert argument to integer value. */ |
| 702 | conv = strtoll(&args[3][1], &error, 16); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 703 | if (*error != '\0') |
| 704 | return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 705 | |
| 706 | /* Convert and check integer to pointer. */ |
| 707 | ref = (struct pat_ref_elt *)(long)conv; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 708 | if ((long long int)(long)ref != conv) |
| 709 | return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 710 | |
Aurélien Nephtali | 9a4da68 | 2018-04-16 19:02:42 +0200 | [diff] [blame] | 711 | /* Try to modify the entry. */ |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 712 | err = NULL; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 713 | HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 714 | if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 715 | HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 716 | if (err) |
| 717 | return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); |
| 718 | else |
| 719 | return cli_err(appctx, "Failed to update an entry.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 720 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 721 | HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 722 | } |
| 723 | else { |
| 724 | /* Else, use the entry identifier as pattern |
| 725 | * string, and update the value. |
| 726 | */ |
| 727 | err = NULL; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 728 | HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 729 | if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 730 | HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 731 | if (err) |
| 732 | return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); |
| 733 | else |
| 734 | return cli_err(appctx, "Failed to update an entry.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 735 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 736 | HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | /* The set is done, send message. */ |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 740 | appctx->st0 = CLI_ST_PROMPT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 741 | return 0; |
| 742 | } |
| 743 | return 1; |
| 744 | } |
| 745 | |
Aurélien Nephtali | 25650ce | 2018-04-18 14:04:47 +0200 | [diff] [blame] | 746 | static int map_add_key_value(struct appctx *appctx, const char *key, const char *value, char **err) |
| 747 | { |
| 748 | int ret; |
| 749 | |
| 750 | HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
| 751 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 752 | ret = pat_ref_add(appctx->ctx.map.ref, key, value, err); |
| 753 | else |
| 754 | ret = pat_ref_add(appctx->ctx.map.ref, key, NULL, err); |
| 755 | HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
| 756 | |
| 757 | return ret; |
| 758 | } |
| 759 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 760 | static int cli_parse_add_map(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 761 | { |
| 762 | if (strcmp(args[1], "map") == 0 || |
| 763 | strcmp(args[1], "acl") == 0) { |
| 764 | int ret; |
| 765 | char *err; |
| 766 | |
| 767 | /* Set flags. */ |
| 768 | if (args[1][0] == 'm') |
| 769 | appctx->ctx.map.display_flags = PAT_REF_MAP; |
| 770 | else |
| 771 | appctx->ctx.map.display_flags = PAT_REF_ACL; |
| 772 | |
Aurélien Nephtali | 25650ce | 2018-04-18 14:04:47 +0200 | [diff] [blame] | 773 | /* If the keyword is "map", we expect: |
| 774 | * - three parameters if there is no payload |
| 775 | * - one parameter if there is a payload |
| 776 | * If it is "acl", we expect only two parameters |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 777 | */ |
| 778 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) { |
Aurélien Nephtali | 25650ce | 2018-04-18 14:04:47 +0200 | [diff] [blame] | 779 | if ((!payload && (!*args[2] || !*args[3] || !*args[4])) || |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 780 | (payload && !*args[2])) |
| 781 | return cli_err(appctx, |
| 782 | "'add map' expects three parameters (map identifier, key and value)" |
| 783 | " or one parameter (map identifier) and a payload\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 784 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 785 | else if (!*args[2] || !*args[3]) |
| 786 | return cli_err(appctx, "'add acl' expects two parameters: ACL identifier and pattern.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 787 | |
| 788 | /* Lookup for the reference. */ |
| 789 | appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); |
| 790 | if (!appctx->ctx.map.ref) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 791 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 792 | return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n"); |
| 793 | else |
| 794 | return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | /* The command "add acl" is prohibited if the reference |
| 798 | * use samples. |
| 799 | */ |
| 800 | if ((appctx->ctx.map.display_flags & PAT_REF_ACL) && |
| 801 | (appctx->ctx.map.ref->flags & PAT_REF_SMP)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 802 | return cli_err(appctx, |
| 803 | "This ACL is shared with a map containing samples. " |
| 804 | "You must use the command 'add map' to add values.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 805 | } |
Aurélien Nephtali | 25650ce | 2018-04-18 14:04:47 +0200 | [diff] [blame] | 806 | /* Add value(s). */ |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 807 | err = NULL; |
Aurélien Nephtali | 25650ce | 2018-04-18 14:04:47 +0200 | [diff] [blame] | 808 | if (!payload) { |
| 809 | ret = map_add_key_value(appctx, args[3], args[4], &err); |
| 810 | if (!ret) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 811 | if (err) |
| 812 | return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); |
| 813 | else |
| 814 | return cli_err(appctx, "Failed to add an entry.\n"); |
Aurélien Nephtali | 9a4da68 | 2018-04-16 19:02:42 +0200 | [diff] [blame] | 815 | } |
Aurélien Nephtali | 25650ce | 2018-04-18 14:04:47 +0200 | [diff] [blame] | 816 | } |
| 817 | else { |
| 818 | const char *end = payload + strlen(payload); |
| 819 | |
| 820 | while (payload < end) { |
| 821 | char *key, *value; |
| 822 | size_t l; |
| 823 | |
| 824 | /* key */ |
| 825 | key = payload; |
| 826 | l = strcspn(key, " \t"); |
| 827 | payload += l; |
| 828 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 829 | if (!*payload && appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 830 | return cli_dynerr(appctx, memprintf(&err, "Missing value for key '%s'.\n", key)); |
| 831 | |
Aurélien Nephtali | 25650ce | 2018-04-18 14:04:47 +0200 | [diff] [blame] | 832 | key[l] = 0; |
| 833 | payload++; |
| 834 | |
| 835 | /* value */ |
| 836 | payload += strspn(payload, " \t"); |
| 837 | value = payload; |
| 838 | l = strcspn(value, "\n"); |
| 839 | payload += l; |
| 840 | if (*payload) |
| 841 | payload++; |
| 842 | value[l] = 0; |
| 843 | |
| 844 | ret = map_add_key_value(appctx, key, value, &err); |
| 845 | if (!ret) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 846 | if (err) |
| 847 | return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); |
| 848 | else |
| 849 | return cli_err(appctx, "Failed to add a key.\n"); |
Aurélien Nephtali | 25650ce | 2018-04-18 14:04:47 +0200 | [diff] [blame] | 850 | } |
Aurélien Nephtali | 9a4da68 | 2018-04-16 19:02:42 +0200 | [diff] [blame] | 851 | } |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | /* The add is done, send message. */ |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 855 | appctx->st0 = CLI_ST_PROMPT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 856 | return 1; |
| 857 | } |
| 858 | |
| 859 | return 0; |
| 860 | } |
| 861 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 862 | static int cli_parse_del_map(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 863 | { |
| 864 | if (args[1][0] == 'm') |
| 865 | appctx->ctx.map.display_flags = PAT_REF_MAP; |
| 866 | else |
| 867 | appctx->ctx.map.display_flags = PAT_REF_ACL; |
| 868 | |
| 869 | /* Expect two parameters: map name and key. */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 870 | if (!*args[2] || !*args[3]) { |
| 871 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 872 | return cli_err(appctx, "This command expects two parameters: map identifier and key.\n"); |
| 873 | else |
| 874 | return cli_err(appctx, "This command expects two parameters: ACL identifier and key.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | /* Lookup the reference in the maps. */ |
| 878 | appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); |
| 879 | if (!appctx->ctx.map.ref || |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 880 | !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) |
| 881 | return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 882 | |
| 883 | /* If the entry identifier start with a '#', it is considered as |
| 884 | * pointer id |
| 885 | */ |
| 886 | if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') { |
| 887 | struct pat_ref_elt *ref; |
| 888 | long long int conv; |
| 889 | char *error; |
| 890 | |
| 891 | /* Convert argument to integer value. */ |
| 892 | conv = strtoll(&args[3][1], &error, 16); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 893 | if (*error != '\0') |
| 894 | return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 895 | |
| 896 | /* Convert and check integer to pointer. */ |
| 897 | ref = (struct pat_ref_elt *)(long)conv; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 898 | if ((long long int)(long)ref != conv) |
| 899 | return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 900 | |
| 901 | /* Try to delete the entry. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 902 | HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 903 | if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 904 | HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 905 | /* The entry is not found, send message. */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 906 | return cli_err(appctx, "Key not found.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 907 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 908 | HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 909 | } |
| 910 | else { |
| 911 | /* Else, use the entry identifier as pattern |
| 912 | * string and try to delete the entry. |
| 913 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 914 | HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 915 | if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 916 | HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 917 | /* The entry is not found, send message. */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 918 | return cli_err(appctx, "Key not found.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 919 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 920 | HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | /* The deletion is done, send message. */ |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 924 | appctx->st0 = CLI_ST_PROMPT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 925 | return 1; |
| 926 | } |
| 927 | |
| 928 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 929 | static int cli_parse_clear_map(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 930 | { |
| 931 | if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) { |
| 932 | /* Set ACL or MAP flags. */ |
| 933 | if (args[1][0] == 'm') |
| 934 | appctx->ctx.map.display_flags = PAT_REF_MAP; |
| 935 | else |
| 936 | appctx->ctx.map.display_flags = PAT_REF_ACL; |
| 937 | |
| 938 | /* no parameter */ |
| 939 | if (!*args[2]) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 940 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 941 | return cli_err(appctx, "Missing map identifier.\n"); |
| 942 | else |
| 943 | return cli_err(appctx, "Missing ACL identifier.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | /* lookup into the refs and check the map flag */ |
| 947 | appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); |
| 948 | if (!appctx->ctx.map.ref || |
| 949 | !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 950 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 951 | return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n"); |
| 952 | else |
| 953 | return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | /* Clear all. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 957 | HA_SPIN_LOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 958 | pat_ref_prune(appctx->ctx.map.ref); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 959 | HA_SPIN_UNLOCK(PATREF_LOCK, &appctx->ctx.map.ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 960 | |
| 961 | /* return response */ |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 962 | appctx->st0 = CLI_ST_PROMPT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 963 | return 1; |
| 964 | } |
| 965 | return 0; |
| 966 | } |
| 967 | |
| 968 | /* register cli keywords */ |
| 969 | |
| 970 | static struct cli_kw_list cli_kws = {{ },{ |
| 971 | { { "add", "acl", NULL }, "add acl : add acl entry", cli_parse_add_map, NULL }, |
| 972 | { { "clear", "acl", NULL }, "clear acl <id> : clear the content of this acl", cli_parse_clear_map, NULL }, |
| 973 | { { "del", "acl", NULL }, "del acl : delete acl entry", cli_parse_del_map, NULL }, |
| 974 | { { "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 }, |
| 975 | { { "show", "acl", NULL }, "show acl [id] : report available acls or dump an acl's contents", cli_parse_show_map, NULL }, |
| 976 | { { "add", "map", NULL }, "add map : add map entry", cli_parse_add_map, NULL }, |
| 977 | { { "clear", "map", NULL }, "clear map <id> : clear the content of this map", cli_parse_clear_map, NULL }, |
| 978 | { { "del", "map", NULL }, "del map : delete map entry", cli_parse_del_map, NULL }, |
Nenad Merdanovic | 96c1571 | 2017-03-12 22:01:36 +0100 | [diff] [blame] | 979 | { { "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 Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 980 | { { "set", "map", NULL }, "set map : modify map entry", cli_parse_set_map, NULL }, |
| 981 | { { "show", "map", NULL }, "show map [id] : report available maps or dump a map's contents", cli_parse_show_map, NULL }, |
| 982 | { { NULL }, NULL, NULL, NULL } |
| 983 | }}; |
| 984 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 985 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 986 | |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 987 | /* Note: must not be declared <const> as its list will be overwritten |
| 988 | * |
| 989 | * For the map_*_int keywords, the output is declared as SMP_T_UINT, but the converter function |
| 990 | * can provide SMP_T_UINT, SMP_T_SINT or SMP_T_BOOL depending on how the patterns found in the |
| 991 | * file can be parsed. |
| 992 | * |
| 993 | * For the map_*_ip keyword, the output is declared as SMP_T_IPV4, but the converter function |
| 994 | * can provide SMP_T_IPV4 or SMP_T_IPV6 depending on the patterns found in the file. |
| 995 | * |
| 996 | * The map_* keywords only emit strings. |
| 997 | * |
| 998 | * The output type is only used during the configuration parsing. It is used for detecting |
| 999 | * compatibility problems. |
| 1000 | * |
| 1001 | * The arguments are: <file>[,<default value>] |
| 1002 | */ |
| 1003 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 1edc971 | 2014-12-15 16:18:39 +0100 | [diff] [blame] | 1004 | { "map", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR }, |
| 1005 | { "map_str", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR }, |
| 1006 | { "map_beg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_BEG }, |
| 1007 | { "map_sub", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_SUB }, |
| 1008 | { "map_dir", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DIR }, |
| 1009 | { "map_dom", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DOM }, |
| 1010 | { "map_end", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_END }, |
| 1011 | { "map_reg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_REG }, |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 1012 | { "map_regm", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_REGM}, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1013 | { "map_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_SINT, SMP_T_STR, (void *)PAT_MATCH_INT }, |
Thierry FOURNIER | 1edc971 | 2014-12-15 16:18:39 +0100 | [diff] [blame] | 1014 | { "map_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_ADDR, SMP_T_STR, (void *)PAT_MATCH_IP }, |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 1015 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1016 | { "map_str_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_STR }, |
| 1017 | { "map_beg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_BEG }, |
| 1018 | { "map_sub_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_SUB }, |
| 1019 | { "map_dir_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DIR }, |
| 1020 | { "map_dom_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DOM }, |
| 1021 | { "map_end_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_END }, |
| 1022 | { "map_reg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_REG }, |
| 1023 | { "map_int_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_SINT, SMP_T_SINT, (void *)PAT_MATCH_INT }, |
| 1024 | { "map_ip_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_ADDR, SMP_T_SINT, (void *)PAT_MATCH_IP }, |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 1025 | |
Thierry FOURNIER | b2f8f08 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 1026 | { "map_str_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_STR }, |
| 1027 | { "map_beg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_BEG }, |
| 1028 | { "map_sub_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_SUB }, |
| 1029 | { "map_dir_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DIR }, |
| 1030 | { "map_dom_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DOM }, |
| 1031 | { "map_end_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_END }, |
| 1032 | { "map_reg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_REG }, |
| 1033 | { "map_int_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_SINT, SMP_T_ADDR, (void *)PAT_MATCH_INT }, |
| 1034 | { "map_ip_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_ADDR, SMP_T_ADDR, (void *)PAT_MATCH_IP }, |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 1035 | |
| 1036 | { /* END */ }, |
| 1037 | }}; |
| 1038 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1039 | INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws); |