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