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