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