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> |
Willy Tarreau | 97218ce | 2021-04-30 14:57:03 +0200 | [diff] [blame] | 14 | #include <syslog.h> |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 15 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 16 | #include <haproxy/api.h> |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 17 | #include <haproxy/applet.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 18 | #include <haproxy/arg.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 19 | #include <haproxy/cli.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 20 | #include <haproxy/map.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 21 | #include <haproxy/pattern.h> |
Willy Tarreau | 7cd8b6e | 2020-06-02 17:32:26 +0200 | [diff] [blame] | 22 | #include <haproxy/regex.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 23 | #include <haproxy/sample.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 24 | #include <haproxy/sc_strm.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 25 | #include <haproxy/stats-t.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 26 | #include <haproxy/stconn.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 27 | #include <haproxy/tools.h> |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 28 | |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 29 | |
Thierry FOURNIER | b2f8f08 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 30 | /* Parse an IPv4 or IPv6 address and store it into the sample. |
| 31 | * The output type is IPv4 or IPv6. |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 32 | */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 33 | int map_parse_ip(const char *text, struct sample_data *data) |
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 | int len = strlen(text); |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 36 | |
Thierry FOURNIER | b2f8f08 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 37 | if (buf2ip(text, len, &data->u.ipv4)) { |
| 38 | data->type = SMP_T_IPV4; |
| 39 | return 1; |
| 40 | } |
| 41 | if (buf2ip6(text, len, &data->u.ipv6)) { |
| 42 | data->type = SMP_T_IPV6; |
| 43 | return 1; |
| 44 | } |
| 45 | return 0; |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /* Parse a string and store a pointer to it into the sample. The original |
| 49 | * string must be left in memory because we return a direct memory reference. |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 50 | * The output type is SMP_T_STR. There is no risk that the data will be |
| 51 | * overwritten because sample_conv_map() makes a const sample with this |
| 52 | * output. |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 53 | */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 54 | int map_parse_str(const char *text, struct sample_data *data) |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 55 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 56 | data->u.str.area = (char *)text; |
| 57 | data->u.str.data = strlen(text); |
| 58 | data->u.str.size = data->u.str.data + 1; |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 59 | data->type = SMP_T_STR; |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | /* Parse an integer and convert it to a sample. The output type is SINT if the |
| 64 | * number is negative, or UINT if it is positive or null. The function returns |
| 65 | * zero (error) if the number is too large. |
| 66 | */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 67 | int map_parse_int(const char *text, struct sample_data *data) |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 68 | { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 69 | data->type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 70 | data->u.sint = read_int64(&text, text + strlen(text)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 71 | if (*text != '\0') |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 72 | return 0; |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 73 | return 1; |
| 74 | } |
| 75 | |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 76 | /* This crete and initialize map descriptor. |
| 77 | * Return NULL if out of memory error |
| 78 | */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 79 | static struct map_descriptor *map_create_descriptor(struct sample_conv *conv) |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 80 | { |
| 81 | struct map_descriptor *desc; |
| 82 | |
| 83 | desc = calloc(1, sizeof(*desc)); |
| 84 | if (!desc) |
| 85 | return NULL; |
| 86 | |
| 87 | desc->conv = conv; |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 88 | |
| 89 | return desc; |
| 90 | } |
| 91 | |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 92 | /* This function load the map file according with data type declared into |
| 93 | * the "struct sample_conv". |
| 94 | * |
| 95 | * This function choose the indexation type (ebtree or list) according with |
| 96 | * the type of match needed. |
| 97 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 98 | int sample_load_map(struct arg *arg, struct sample_conv *conv, |
| 99 | const char *file, int line, char **err) |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 100 | { |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 101 | struct map_descriptor *desc; |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 102 | |
Christopher Faulet | 0eb967d | 2020-08-05 23:23:37 +0200 | [diff] [blame] | 103 | if (!(global.mode & MODE_STARTING)) { |
| 104 | memprintf(err, "map: cannot load map at runtime"); |
| 105 | return 0; |
| 106 | } |
| 107 | |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 108 | /* create new map descriptor */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 109 | desc = map_create_descriptor(conv); |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 110 | if (!desc) { |
| 111 | memprintf(err, "out of memory"); |
| 112 | return 0; |
| 113 | } |
| 114 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 115 | /* Initialize pattern */ |
| 116 | pattern_init_head(&desc->pat); |
| 117 | |
| 118 | /* This is original pattern, must free */ |
| 119 | desc->do_free = 1; |
| 120 | |
| 121 | /* Set the match method. */ |
Thierry FOURNIER | 1edc971 | 2014-12-15 16:18:39 +0100 | [diff] [blame] | 122 | desc->pat.match = pat_match_fcts[(long)conv->private]; |
| 123 | desc->pat.parse = pat_parse_fcts[(long)conv->private]; |
| 124 | desc->pat.index = pat_index_fcts[(long)conv->private]; |
Thierry FOURNIER | 1edc971 | 2014-12-15 16:18:39 +0100 | [diff] [blame] | 125 | desc->pat.prune = pat_prune_fcts[(long)conv->private]; |
| 126 | desc->pat.expect_type = pat_match_types[(long)conv->private]; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 127 | |
| 128 | /* Set the output parse method. */ |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 129 | switch (desc->conv->out_type) { |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 130 | case SMP_T_STR: desc->pat.parse_smp = map_parse_str; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 131 | case SMP_T_SINT: desc->pat.parse_smp = map_parse_int; break; |
Thierry FOURNIER | b2f8f08 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 132 | case SMP_T_ADDR: desc->pat.parse_smp = map_parse_ip; break; |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 133 | default: |
| 134 | memprintf(err, "map: internal haproxy error: no default parse case for the input type <%d>.", |
| 135 | conv->out_type); |
Andreas Seltenreich | 78f3595 | 2016-03-03 20:32:23 +0100 | [diff] [blame] | 136 | free(desc); |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 140 | /* Load map. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 141 | 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] | 142 | 1, err, file, line)) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 143 | return 0; |
Thierry FOURNIER | 0ffe78c | 2013-12-05 14:40:25 +0100 | [diff] [blame] | 144 | |
Willy Tarreau | aa5801b | 2019-04-19 11:35:22 +0200 | [diff] [blame] | 145 | /* the maps of type IP support a string as default value. This |
| 146 | * string can be an ipv4 or an ipv6, we must convert it. |
Thierry FOURNIER | b2f8f08 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 147 | */ |
Willy Tarreau | aa5801b | 2019-04-19 11:35:22 +0200 | [diff] [blame] | 148 | 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] | 149 | struct sample_data data; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 150 | if (!map_parse_ip(arg[1].data.str.area, &data)) { |
| 151 | memprintf(err, "map: cannot parse default ip <%s>.", |
| 152 | arg[1].data.str.area); |
Thierry FOURNIER | b2f8f08 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 153 | return 0; |
| 154 | } |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 155 | chunk_destroy(&arg[1].data.str); |
Thierry FOURNIER | b2f8f08 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 156 | if (data.type == SMP_T_IPV4) { |
| 157 | arg[1].type = ARGT_IPV4; |
| 158 | arg[1].data.ipv4 = data.u.ipv4; |
| 159 | } else { |
| 160 | arg[1].type = ARGT_IPV6; |
| 161 | arg[1].data.ipv6 = data.u.ipv6; |
| 162 | } |
| 163 | } |
| 164 | |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 165 | /* replace the first argument by this definition */ |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 166 | chunk_destroy(&arg[0].data.str); |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 167 | arg[0].type = ARGT_MAP; |
| 168 | arg[0].data.map = desc; |
| 169 | |
| 170 | return 1; |
| 171 | } |
| 172 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 173 | 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] | 174 | { |
| 175 | struct map_descriptor *desc; |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 176 | struct pattern *pat; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 177 | struct buffer *str; |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 178 | |
| 179 | /* get config */ |
| 180 | desc = arg_p[0].data.map; |
| 181 | |
| 182 | /* Execute the match function. */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 183 | pat = pattern_exec_match(&desc->pat, smp, 1); |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 184 | |
| 185 | /* Match case. */ |
| 186 | if (pat) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 187 | if (pat->data) { |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 188 | /* In the regm case, merge the sample with the input. */ |
| 189 | if ((long)private == PAT_MATCH_REGM) { |
Emeric Brun | 2710221 | 2018-07-17 09:47:07 -0400 | [diff] [blame] | 190 | struct buffer *tmptrash; |
Willy Tarreau | 2842e05 | 2018-08-22 04:55:43 +0200 | [diff] [blame] | 191 | int len; |
Emeric Brun | 2710221 | 2018-07-17 09:47:07 -0400 | [diff] [blame] | 192 | |
| 193 | /* Copy the content of the sample because it could |
| 194 | be scratched by incoming get_trash_chunk */ |
| 195 | tmptrash = alloc_trash_chunk(); |
| 196 | if (!tmptrash) |
| 197 | return 0; |
| 198 | |
| 199 | tmptrash->data = smp->data.u.str.data; |
| 200 | if (tmptrash->data > (tmptrash->size-1)) |
| 201 | tmptrash->data = tmptrash->size-1; |
| 202 | |
| 203 | memcpy(tmptrash->area, smp->data.u.str.area, tmptrash->data); |
| 204 | tmptrash->area[tmptrash->data] = 0; |
| 205 | |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 206 | str = get_trash_chunk(); |
Willy Tarreau | 2842e05 | 2018-08-22 04:55:43 +0200 | [diff] [blame] | 207 | len = exp_replace(str->area, str->size, |
| 208 | tmptrash->area, |
| 209 | pat->data->u.str.area, |
| 210 | (regmatch_t *)smp->ctx.a[0]); |
Nenad Merdanovic | 646b774 | 2019-04-12 22:54:28 +0200 | [diff] [blame] | 211 | free_trash_chunk(tmptrash); |
| 212 | |
Willy Tarreau | 2842e05 | 2018-08-22 04:55:43 +0200 | [diff] [blame] | 213 | if (len == -1) |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 214 | return 0; |
Willy Tarreau | 2842e05 | 2018-08-22 04:55:43 +0200 | [diff] [blame] | 215 | |
| 216 | str->data = len; |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 217 | smp->data.u.str = *str; |
| 218 | return 1; |
| 219 | } |
| 220 | /* Copy sample. */ |
Thierry FOURNIER | 5cc18d4 | 2015-08-19 09:02:36 +0200 | [diff] [blame] | 221 | smp->data = *pat->data; |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 222 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 223 | return 1; |
| 224 | } |
| 225 | |
| 226 | /* Return just int sample containing 1. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 227 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 228 | smp->data.u.sint = 1; |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 229 | return 1; |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 230 | } |
| 231 | |
Joseph Herlant | f43b88b | 2018-11-25 11:48:18 -0800 | [diff] [blame] | 232 | /* If no default value available, the converter fails. */ |
Thierry FOURNIER | 933e5de | 2015-03-13 00:10:16 +0100 | [diff] [blame] | 233 | if (arg_p[1].type == ARGT_STOP) |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 234 | return 0; |
| 235 | |
| 236 | /* Return the default value. */ |
Thierry FOURNIER | 933e5de | 2015-03-13 00:10:16 +0100 | [diff] [blame] | 237 | switch (desc->conv->out_type) { |
| 238 | |
| 239 | case SMP_T_STR: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 240 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 933e5de | 2015-03-13 00:10:16 +0100 | [diff] [blame] | 241 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 242 | smp->data.u.str = arg_p[1].data.str; |
Thierry FOURNIER | 933e5de | 2015-03-13 00:10:16 +0100 | [diff] [blame] | 243 | break; |
| 244 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 245 | case SMP_T_SINT: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 246 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 247 | smp->data.u.sint = arg_p[1].data.sint; |
Thierry FOURNIER | 933e5de | 2015-03-13 00:10:16 +0100 | [diff] [blame] | 248 | break; |
| 249 | |
Thierry FOURNIER | b2f8f08 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 250 | case SMP_T_ADDR: |
| 251 | if (arg_p[1].type == ARGT_IPV4) { |
| 252 | smp->data.type = SMP_T_IPV4; |
| 253 | smp->data.u.ipv4 = arg_p[1].data.ipv4; |
| 254 | } else { |
| 255 | smp->data.type = SMP_T_IPV6; |
| 256 | smp->data.u.ipv6 = arg_p[1].data.ipv6; |
| 257 | } |
Thierry FOURNIER | 933e5de | 2015-03-13 00:10:16 +0100 | [diff] [blame] | 258 | break; |
| 259 | } |
| 260 | |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 261 | return 1; |
| 262 | } |
| 263 | |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 264 | /* This function is used with map and acl management. It permits to browse |
| 265 | * each reference. The variable <getnext> must contain the current node, |
| 266 | * <end> point to the root node and the <flags> permit to filter required |
| 267 | * nodes. |
| 268 | */ |
| 269 | static inline |
| 270 | struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end, |
| 271 | unsigned int flags) |
| 272 | { |
| 273 | struct pat_ref *ref = getnext; |
| 274 | |
| 275 | while (1) { |
| 276 | |
| 277 | /* Get next list entry. */ |
| 278 | ref = LIST_NEXT(&ref->list, struct pat_ref *, list); |
| 279 | |
| 280 | /* If the entry is the last of the list, return NULL. */ |
| 281 | if (&ref->list == end) |
| 282 | return NULL; |
| 283 | |
| 284 | /* If the entry match the flag, return it. */ |
| 285 | if (ref->flags & flags) |
| 286 | return ref; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | static inline |
| 291 | struct pat_ref *pat_ref_lookup_ref(const char *reference) |
| 292 | { |
| 293 | int id; |
| 294 | char *error; |
| 295 | |
| 296 | /* If the reference starts by a '#', this is numeric id. */ |
| 297 | if (reference[0] == '#') { |
| 298 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 299 | id = strtol(reference + 1, &error, 10); |
| 300 | if (*error != '\0') |
| 301 | return NULL; |
| 302 | |
| 303 | /* Perform the unique id lookup. */ |
| 304 | return pat_ref_lookupid(id); |
| 305 | } |
| 306 | |
| 307 | /* Perform the string lookup. */ |
| 308 | return pat_ref_lookup(reference); |
| 309 | } |
| 310 | |
| 311 | /* This function is used with map and acl management. It permits to browse |
| 312 | * each reference. |
| 313 | */ |
| 314 | static inline |
| 315 | struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end) |
| 316 | { |
| 317 | struct pattern_expr *expr; |
| 318 | expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list); |
| 319 | if (&expr->list == end) |
| 320 | return NULL; |
| 321 | return expr; |
| 322 | } |
| 323 | |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 324 | /* appctx context for the "{show|get|add|del|*} {map|acl}" commands. This is |
| 325 | * used even by commands that only have a parser and no I/O handler because |
| 326 | * it provides a unified way to manipulate some fields and will allow to |
| 327 | * expand some of them more easily later if needed. |
| 328 | */ |
| 329 | struct show_map_ctx { |
| 330 | struct pat_ref *ref; |
| 331 | struct bref bref; /* back-reference from the pat_ref_elt being dumped */ |
| 332 | struct pattern_expr *expr; |
| 333 | struct buffer chunk; |
| 334 | unsigned int display_flags; |
Willy Tarreau | 76f771e | 2022-05-03 14:12:56 +0200 | [diff] [blame] | 335 | unsigned int curr_gen; /* current/latest generation, for show/clear */ |
| 336 | unsigned int prev_gen; /* prev generation, for clear */ |
Willy Tarreau | a0d6280 | 2022-05-03 15:12:21 +0200 | [diff] [blame] | 337 | enum { |
| 338 | STATE_INIT = 0, /* initialize list and backrefs */ |
| 339 | STATE_LIST, /* list entries */ |
| 340 | STATE_DONE, /* finished */ |
| 341 | } state; /* state of the dump */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 342 | }; |
| 343 | |
Willy Tarreau | 76f771e | 2022-05-03 14:12:56 +0200 | [diff] [blame] | 344 | /* expects the current generation ID in ctx->curr_gen */ |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 345 | static int cli_io_handler_pat_list(struct appctx *appctx) |
| 346 | { |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 347 | struct show_map_ctx *ctx = appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 348 | struct stconn *sc = appctx_sc(appctx); |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 349 | struct pat_ref_elt *elt; |
| 350 | |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 351 | /* FIXME: Don't watch the other side !*/ |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 352 | if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE)) { |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 353 | /* If we're forced to shut down, we might have to remove our |
| 354 | * reference to the last ref_elt being dumped. |
| 355 | */ |
Willy Tarreau | c7e9706 | 2022-05-03 15:42:07 +0200 | [diff] [blame] | 356 | if (!LIST_ISEMPTY(&ctx->bref.users)) { |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 357 | HA_SPIN_LOCK(PATREF_LOCK, &ctx->ref->lock); |
Willy Tarreau | c7e9706 | 2022-05-03 15:42:07 +0200 | [diff] [blame] | 358 | LIST_DEL_INIT(&ctx->bref.users); |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 359 | HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock); |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 360 | } |
| 361 | return 1; |
| 362 | } |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 363 | |
Willy Tarreau | a0d6280 | 2022-05-03 15:12:21 +0200 | [diff] [blame] | 364 | switch (ctx->state) { |
| 365 | case STATE_INIT: |
| 366 | ctx->state = STATE_LIST; |
Willy Tarreau | ab42dc3 | 2022-11-14 07:35:24 +0100 | [diff] [blame] | 367 | __fallthrough; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 368 | |
Willy Tarreau | a0d6280 | 2022-05-03 15:12:21 +0200 | [diff] [blame] | 369 | case STATE_LIST: |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 370 | HA_SPIN_LOCK(PATREF_LOCK, &ctx->ref->lock); |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 371 | |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 372 | if (!LIST_ISEMPTY(&ctx->bref.users)) { |
| 373 | LIST_DELETE(&ctx->bref.users); |
| 374 | LIST_INIT(&ctx->bref.users); |
Willy Tarreau | 1ae0c43 | 2022-05-03 15:26:27 +0200 | [diff] [blame] | 375 | } else { |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 376 | ctx->bref.ref = ctx->ref->head.n; |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 377 | } |
| 378 | |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 379 | while (ctx->bref.ref != &ctx->ref->head) { |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 380 | chunk_reset(&trash); |
| 381 | |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 382 | elt = LIST_ELEM(ctx->bref.ref, struct pat_ref_elt *, list); |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 383 | |
Willy Tarreau | 76f771e | 2022-05-03 14:12:56 +0200 | [diff] [blame] | 384 | if (elt->gen_id != ctx->curr_gen) |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 385 | goto skip; |
| 386 | |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 387 | /* build messages */ |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 388 | if (elt->sample) |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 389 | chunk_appendf(&trash, "%p %s %s\n", |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 390 | elt, elt->pattern, |
| 391 | elt->sample); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 392 | else |
| 393 | chunk_appendf(&trash, "%p %s\n", |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 394 | elt, elt->pattern); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 395 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 396 | if (applet_putchk(appctx, &trash) == -1) { |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 397 | /* let's try again later from this stream. We add ourselves into |
| 398 | * this stream's users so that it can remove us upon termination. |
| 399 | */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 400 | LIST_APPEND(&elt->back_refs, &ctx->bref.users); |
| 401 | HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 402 | return 0; |
| 403 | } |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 404 | skip: |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 405 | /* get next list entry and check the end of the list */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 406 | ctx->bref.ref = elt->list.n; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 407 | } |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 408 | HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock); |
Willy Tarreau | ab42dc3 | 2022-11-14 07:35:24 +0100 | [diff] [blame] | 409 | __fallthrough; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 410 | |
| 411 | default: |
Willy Tarreau | a0d6280 | 2022-05-03 15:12:21 +0200 | [diff] [blame] | 412 | ctx->state = STATE_DONE; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 413 | return 1; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | static int cli_io_handler_pats_list(struct appctx *appctx) |
| 418 | { |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 419 | struct show_map_ctx *ctx = appctx->svcctx; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 420 | |
Willy Tarreau | a0d6280 | 2022-05-03 15:12:21 +0200 | [diff] [blame] | 421 | switch (ctx->state) { |
| 422 | case STATE_INIT: |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 423 | /* Display the column headers. If the message cannot be sent, |
Joseph Herlant | f43b88b | 2018-11-25 11:48:18 -0800 | [diff] [blame] | 424 | * quit the function with returning 0. The function is called |
Willy Tarreau | a0d6280 | 2022-05-03 15:12:21 +0200 | [diff] [blame] | 425 | * later and restarted at the state "STATE_INIT". |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 426 | */ |
| 427 | chunk_reset(&trash); |
| 428 | chunk_appendf(&trash, "# id (file) description\n"); |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 429 | if (applet_putchk(appctx, &trash) == -1) |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 430 | return 0; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 431 | |
| 432 | /* Now, we start the browsing of the references lists. |
Joseph Herlant | f43b88b | 2018-11-25 11:48:18 -0800 | [diff] [blame] | 433 | * 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] | 434 | * 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] | 435 | * pat_list_get_next() for returning the first available entry |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 436 | */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 437 | ctx->ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list); |
| 438 | ctx->ref = pat_list_get_next(ctx->ref, &pattern_reference, |
| 439 | ctx->display_flags); |
Willy Tarreau | a0d6280 | 2022-05-03 15:12:21 +0200 | [diff] [blame] | 440 | ctx->state = STATE_LIST; |
Willy Tarreau | ab42dc3 | 2022-11-14 07:35:24 +0100 | [diff] [blame] | 441 | __fallthrough; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 442 | |
Willy Tarreau | a0d6280 | 2022-05-03 15:12:21 +0200 | [diff] [blame] | 443 | case STATE_LIST: |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 444 | while (ctx->ref) { |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 445 | chunk_reset(&trash); |
| 446 | |
| 447 | /* Build messages. If the reference is used by another category than |
Joseph Herlant | f43b88b | 2018-11-25 11:48:18 -0800 | [diff] [blame] | 448 | * the listed categories, display the information in the message. |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 449 | */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 450 | chunk_appendf(&trash, "%d (%s) %s. curr_ver=%u next_ver=%u entry_cnt=%llu\n", ctx->ref->unique_id, |
| 451 | ctx->ref->reference ? ctx->ref->reference : "", |
| 452 | ctx->ref->display, ctx->ref->curr_gen, ctx->ref->next_gen, |
| 453 | ctx->ref->entry_cnt); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 454 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 455 | if (applet_putchk(appctx, &trash) == -1) { |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 456 | /* let's try again later from this stream. We add ourselves into |
| 457 | * this stream's users so that it can remove us upon termination. |
| 458 | */ |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | /* get next list entry and check the end of the list */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 463 | ctx->ref = pat_list_get_next(ctx->ref, &pattern_reference, |
| 464 | ctx->display_flags); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 465 | } |
| 466 | |
Willy Tarreau | ab42dc3 | 2022-11-14 07:35:24 +0100 | [diff] [blame] | 467 | __fallthrough; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 468 | |
| 469 | default: |
Willy Tarreau | a0d6280 | 2022-05-03 15:12:21 +0200 | [diff] [blame] | 470 | ctx->state = STATE_DONE; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 471 | return 1; |
| 472 | } |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | static int cli_io_handler_map_lookup(struct appctx *appctx) |
| 477 | { |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 478 | struct show_map_ctx *ctx = appctx->svcctx; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 479 | struct sample sample; |
| 480 | struct pattern *pat; |
| 481 | int match_method; |
| 482 | |
Willy Tarreau | a0d6280 | 2022-05-03 15:12:21 +0200 | [diff] [blame] | 483 | switch (ctx->state) { |
| 484 | case STATE_INIT: |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 485 | /* Init to the first entry. The list cannot be change */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 486 | ctx->expr = LIST_ELEM(&ctx->ref->pat, struct pattern_expr *, list); |
| 487 | ctx->expr = pat_expr_get_next(ctx->expr, &ctx->ref->pat); |
Willy Tarreau | a0d6280 | 2022-05-03 15:12:21 +0200 | [diff] [blame] | 488 | ctx->state = STATE_LIST; |
Willy Tarreau | ab42dc3 | 2022-11-14 07:35:24 +0100 | [diff] [blame] | 489 | __fallthrough; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 490 | |
Willy Tarreau | a0d6280 | 2022-05-03 15:12:21 +0200 | [diff] [blame] | 491 | case STATE_LIST: |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 492 | HA_SPIN_LOCK(PATREF_LOCK, &ctx->ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 493 | /* for each lookup type */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 494 | while (ctx->expr) { |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 495 | /* initialise chunk to build new message */ |
| 496 | chunk_reset(&trash); |
| 497 | |
| 498 | /* execute pattern matching */ |
| 499 | sample.data.type = SMP_T_STR; |
| 500 | sample.flags = SMP_F_CONST; |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 501 | sample.data.u.str.data = ctx->chunk.data; |
| 502 | sample.data.u.str.area = ctx->chunk.area; |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 503 | |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 504 | if (ctx->expr->pat_head->match && |
| 505 | sample_convert(&sample, ctx->expr->pat_head->expect_type)) |
| 506 | pat = ctx->expr->pat_head->match(&sample, ctx->expr, 1); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 507 | else |
| 508 | pat = NULL; |
| 509 | |
| 510 | /* build return message: set type of match */ |
| 511 | for (match_method=0; match_method<PAT_MATCH_NUM; match_method++) |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 512 | if (ctx->expr->pat_head->match == pat_match_fcts[match_method]) |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 513 | break; |
| 514 | if (match_method >= PAT_MATCH_NUM) |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 515 | chunk_appendf(&trash, "type=unknown(%p)", ctx->expr->pat_head->match); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 516 | else |
| 517 | chunk_appendf(&trash, "type=%s", pat_match_names[match_method]); |
| 518 | |
| 519 | /* case sensitive */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 520 | if (ctx->expr->mflags & PAT_MF_IGNORE_CASE) |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 521 | chunk_appendf(&trash, ", case=insensitive"); |
| 522 | else |
| 523 | chunk_appendf(&trash, ", case=sensitive"); |
| 524 | |
| 525 | /* Display no match, and set default value */ |
| 526 | if (!pat) { |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 527 | if (ctx->display_flags == PAT_REF_MAP) |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 528 | chunk_appendf(&trash, ", found=no"); |
| 529 | else |
| 530 | chunk_appendf(&trash, ", match=no"); |
| 531 | } |
| 532 | |
| 533 | /* Display match and match info */ |
| 534 | else { |
| 535 | /* display match */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 536 | if (ctx->display_flags == PAT_REF_MAP) |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 537 | chunk_appendf(&trash, ", found=yes"); |
| 538 | else |
| 539 | chunk_appendf(&trash, ", match=yes"); |
| 540 | |
| 541 | /* display index mode */ |
| 542 | if (pat->sflags & PAT_SF_TREE) |
| 543 | chunk_appendf(&trash, ", idx=tree"); |
| 544 | else |
| 545 | chunk_appendf(&trash, ", idx=list"); |
| 546 | |
| 547 | /* display pattern */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 548 | if (ctx->display_flags == PAT_REF_MAP) { |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 549 | if (pat->ref && pat->ref->pattern) |
| 550 | chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern); |
| 551 | else |
| 552 | chunk_appendf(&trash, ", key=unknown"); |
| 553 | } |
| 554 | else { |
| 555 | if (pat->ref && pat->ref->pattern) |
| 556 | chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern); |
| 557 | else |
| 558 | chunk_appendf(&trash, ", pattern=unknown"); |
| 559 | } |
| 560 | |
| 561 | /* display return value */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 562 | if (ctx->display_flags == PAT_REF_MAP) { |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 563 | if (pat->data && pat->ref && pat->ref->sample) |
| 564 | chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample, |
| 565 | smp_to_type[pat->data->type]); |
| 566 | else |
| 567 | chunk_appendf(&trash, ", value=none"); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | chunk_appendf(&trash, "\n"); |
| 572 | |
| 573 | /* display response */ |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 574 | if (applet_putchk(appctx, &trash) == -1) { |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 575 | /* let's try again later from this stream. We add ourselves into |
| 576 | * this stream's users so that it can remove us upon termination. |
| 577 | */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 578 | HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | /* get next entry */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 583 | ctx->expr = pat_expr_get_next(ctx->expr, |
| 584 | &ctx->ref->pat); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 585 | } |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 586 | HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock); |
Willy Tarreau | ab42dc3 | 2022-11-14 07:35:24 +0100 | [diff] [blame] | 587 | __fallthrough; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 588 | |
| 589 | default: |
Willy Tarreau | a0d6280 | 2022-05-03 15:12:21 +0200 | [diff] [blame] | 590 | ctx->state = STATE_DONE; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 591 | return 1; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | static void cli_release_mlook(struct appctx *appctx) |
| 596 | { |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 597 | struct show_map_ctx *ctx = appctx->svcctx; |
| 598 | |
| 599 | ha_free(&ctx->chunk.area); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 603 | 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] | 604 | { |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 605 | struct show_map_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 606 | |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 607 | if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) { |
| 608 | /* Set flags. */ |
| 609 | if (args[1][0] == 'm') |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 610 | ctx->display_flags = PAT_REF_MAP; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 611 | else |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 612 | ctx->display_flags = PAT_REF_ACL; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 613 | |
| 614 | /* No parameter. */ |
| 615 | if (!*args[2] || !*args[3]) { |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 616 | if (ctx->display_flags == PAT_REF_MAP) |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 617 | return cli_err(appctx, "Missing map identifier and/or key.\n"); |
| 618 | else |
| 619 | return cli_err(appctx, "Missing ACL identifier and/or key.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /* lookup into the maps */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 623 | ctx->ref = pat_ref_lookup_ref(args[2]); |
| 624 | if (!ctx->ref) { |
| 625 | if (ctx->display_flags == PAT_REF_MAP) |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 626 | return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n"); |
| 627 | else |
| 628 | 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] | 629 | } |
| 630 | |
| 631 | /* copy input string. The string must be allocated because |
| 632 | * it may be used over multiple iterations. It's released |
| 633 | * at the end and upon abort anyway. |
| 634 | */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 635 | ctx->chunk.data = strlen(args[3]); |
| 636 | ctx->chunk.size = ctx->chunk.data + 1; |
| 637 | ctx->chunk.area = strdup(args[3]); |
| 638 | if (!ctx->chunk.area) |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 639 | return cli_err(appctx, "Out of memory error.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | return 1; |
| 644 | } |
| 645 | |
Willy Tarreau | 97218ce | 2021-04-30 14:57:03 +0200 | [diff] [blame] | 646 | static int cli_parse_prepare_map(char **args, char *payload, struct appctx *appctx, void *private) |
| 647 | { |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 648 | struct show_map_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 649 | |
Willy Tarreau | 97218ce | 2021-04-30 14:57:03 +0200 | [diff] [blame] | 650 | if (strcmp(args[1], "map") == 0 || |
| 651 | strcmp(args[1], "acl") == 0) { |
| 652 | uint next_gen; |
| 653 | char *msg = NULL; |
| 654 | |
| 655 | /* Set ACL or MAP flags. */ |
| 656 | if (args[1][0] == 'm') |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 657 | ctx->display_flags = PAT_REF_MAP; |
Willy Tarreau | 97218ce | 2021-04-30 14:57:03 +0200 | [diff] [blame] | 658 | else |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 659 | ctx->display_flags = PAT_REF_ACL; |
Willy Tarreau | 97218ce | 2021-04-30 14:57:03 +0200 | [diff] [blame] | 660 | |
| 661 | /* lookup into the refs and check the map flag */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 662 | ctx->ref = pat_ref_lookup_ref(args[2]); |
| 663 | if (!ctx->ref || |
| 664 | !(ctx->ref->flags & ctx->display_flags)) { |
| 665 | if (ctx->display_flags == PAT_REF_MAP) |
Willy Tarreau | 97218ce | 2021-04-30 14:57:03 +0200 | [diff] [blame] | 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"); |
| 669 | } |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 670 | next_gen = pat_ref_newgen(ctx->ref); |
Willy Tarreau | 97218ce | 2021-04-30 14:57:03 +0200 | [diff] [blame] | 671 | return cli_dynmsg(appctx, LOG_INFO, memprintf(&msg, "New version created: %u\n", next_gen)); |
| 672 | } |
| 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 677 | static void cli_release_show_map(struct appctx *appctx) |
| 678 | { |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 679 | struct show_map_ctx *ctx = appctx->svcctx; |
| 680 | |
Willy Tarreau | c7e9706 | 2022-05-03 15:42:07 +0200 | [diff] [blame] | 681 | if (!LIST_ISEMPTY(&ctx->bref.users)) { |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 682 | HA_SPIN_LOCK(PATREF_LOCK, &ctx->ref->lock); |
Willy Tarreau | c7e9706 | 2022-05-03 15:42:07 +0200 | [diff] [blame] | 683 | LIST_DEL_INIT(&ctx->bref.users); |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 684 | HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock); |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 688 | 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] | 689 | { |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 690 | struct show_map_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 691 | |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 692 | if (strcmp(args[1], "map") == 0 || |
| 693 | strcmp(args[1], "acl") == 0) { |
Willy Tarreau | 95f753e | 2021-04-30 12:09:54 +0200 | [diff] [blame] | 694 | const char *gen = NULL; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 695 | |
| 696 | /* Set ACL or MAP flags. */ |
| 697 | if (args[1][0] == 'm') |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 698 | ctx->display_flags = PAT_REF_MAP; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 699 | else |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 700 | ctx->display_flags = PAT_REF_ACL; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 701 | |
| 702 | /* no parameter: display all map available */ |
| 703 | if (!*args[2]) { |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 704 | appctx->io_handler = cli_io_handler_pats_list; |
| 705 | return 0; |
| 706 | } |
| 707 | |
Willy Tarreau | 95f753e | 2021-04-30 12:09:54 +0200 | [diff] [blame] | 708 | /* For both "map" and "acl" we may have an optional generation |
| 709 | * number specified using a "@" character before the pattern |
| 710 | * file name. |
| 711 | */ |
| 712 | if (*args[2] == '@') { |
| 713 | gen = args[2] + 1; |
| 714 | args++; |
| 715 | } |
| 716 | |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 717 | /* lookup into the refs and check the map flag */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 718 | ctx->ref = pat_ref_lookup_ref(args[2]); |
| 719 | if (!ctx->ref || |
| 720 | !(ctx->ref->flags & ctx->display_flags)) { |
| 721 | if (ctx->display_flags == PAT_REF_MAP) |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 722 | return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n"); |
| 723 | else |
| 724 | 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] | 725 | } |
Willy Tarreau | 95f753e | 2021-04-30 12:09:54 +0200 | [diff] [blame] | 726 | |
Willy Tarreau | 76f771e | 2022-05-03 14:12:56 +0200 | [diff] [blame] | 727 | /* set the desired generation id in curr_gen */ |
Willy Tarreau | 95f753e | 2021-04-30 12:09:54 +0200 | [diff] [blame] | 728 | if (gen) |
Willy Tarreau | 76f771e | 2022-05-03 14:12:56 +0200 | [diff] [blame] | 729 | ctx->curr_gen = str2uic(gen); |
Willy Tarreau | 95f753e | 2021-04-30 12:09:54 +0200 | [diff] [blame] | 730 | else |
Willy Tarreau | 76f771e | 2022-05-03 14:12:56 +0200 | [diff] [blame] | 731 | ctx->curr_gen = ctx->ref->curr_gen; |
Willy Tarreau | 95f753e | 2021-04-30 12:09:54 +0200 | [diff] [blame] | 732 | |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 733 | LIST_INIT(&ctx->bref.users); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 734 | appctx->io_handler = cli_io_handler_pat_list; |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 735 | appctx->io_release = cli_release_show_map; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | return 0; |
| 740 | } |
| 741 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 742 | 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] | 743 | { |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 744 | struct show_map_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 745 | |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 746 | if (strcmp(args[1], "map") == 0) { |
| 747 | char *err; |
| 748 | |
| 749 | /* Set flags. */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 750 | ctx->display_flags = PAT_REF_MAP; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 751 | |
| 752 | /* Expect three parameters: map name, key and new value. */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 753 | if (!*args[2] || !*args[3] || !*args[4]) |
| 754 | 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] | 755 | |
| 756 | /* Lookup the reference in the maps. */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 757 | ctx->ref = pat_ref_lookup_ref(args[2]); |
| 758 | if (!ctx->ref) |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 759 | 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] | 760 | |
| 761 | /* If the entry identifier start with a '#', it is considered as |
| 762 | * pointer id |
| 763 | */ |
| 764 | if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') { |
| 765 | struct pat_ref_elt *ref; |
| 766 | long long int conv; |
| 767 | char *error; |
| 768 | |
| 769 | /* Convert argument to integer value. */ |
| 770 | conv = strtoll(&args[3][1], &error, 16); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 771 | if (*error != '\0') |
| 772 | return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 773 | |
| 774 | /* Convert and check integer to pointer. */ |
| 775 | ref = (struct pat_ref_elt *)(long)conv; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 776 | if ((long long int)(long)ref != conv) |
| 777 | return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 778 | |
Aurélien Nephtali | 9a4da68 | 2018-04-16 19:02:42 +0200 | [diff] [blame] | 779 | /* Try to modify the entry. */ |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 780 | err = NULL; |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 781 | HA_SPIN_LOCK(PATREF_LOCK, &ctx->ref->lock); |
| 782 | if (!pat_ref_set_by_id(ctx->ref, ref, args[4], &err)) { |
| 783 | HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 784 | if (err) |
| 785 | return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); |
| 786 | else |
| 787 | return cli_err(appctx, "Failed to update an entry.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 788 | } |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 789 | HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 790 | } |
| 791 | else { |
| 792 | /* Else, use the entry identifier as pattern |
| 793 | * string, and update the value. |
| 794 | */ |
| 795 | err = NULL; |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 796 | HA_SPIN_LOCK(PATREF_LOCK, &ctx->ref->lock); |
| 797 | if (!pat_ref_set(ctx->ref, args[3], args[4], &err)) { |
| 798 | HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 799 | if (err) |
| 800 | return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); |
| 801 | else |
| 802 | return cli_err(appctx, "Failed to update an entry.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 803 | } |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 804 | HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | /* The set is done, send message. */ |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 808 | appctx->st0 = CLI_ST_PROMPT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 809 | return 0; |
| 810 | } |
| 811 | return 1; |
| 812 | } |
| 813 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 814 | 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] | 815 | { |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 816 | struct show_map_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 817 | |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 818 | if (strcmp(args[1], "map") == 0 || |
| 819 | strcmp(args[1], "acl") == 0) { |
Willy Tarreau | bb51c44 | 2021-04-30 15:23:36 +0200 | [diff] [blame] | 820 | const char *gen = NULL; |
| 821 | uint genid = 0; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 822 | int ret; |
| 823 | char *err; |
| 824 | |
| 825 | /* Set flags. */ |
| 826 | if (args[1][0] == 'm') |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 827 | ctx->display_flags = PAT_REF_MAP; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 828 | else |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 829 | ctx->display_flags = PAT_REF_ACL; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 830 | |
Willy Tarreau | bb51c44 | 2021-04-30 15:23:36 +0200 | [diff] [blame] | 831 | /* For both "map" and "acl" we may have an optional generation |
| 832 | * number specified using a "@" character before the pattern |
| 833 | * file name. |
| 834 | */ |
| 835 | if (*args[2] == '@') { |
| 836 | gen = args[2] + 1; |
| 837 | args++; |
| 838 | } |
| 839 | |
Aurélien Nephtali | 25650ce | 2018-04-18 14:04:47 +0200 | [diff] [blame] | 840 | /* If the keyword is "map", we expect: |
| 841 | * - three parameters if there is no payload |
| 842 | * - one parameter if there is a payload |
| 843 | * If it is "acl", we expect only two parameters |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 844 | */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 845 | if (ctx->display_flags == PAT_REF_MAP) { |
Aurélien Nephtali | 25650ce | 2018-04-18 14:04:47 +0200 | [diff] [blame] | 846 | if ((!payload && (!*args[2] || !*args[3] || !*args[4])) || |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 847 | (payload && !*args[2])) |
| 848 | return cli_err(appctx, |
| 849 | "'add map' expects three parameters (map identifier, key and value)" |
| 850 | " or one parameter (map identifier) and a payload\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 851 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 852 | else if (!*args[2] || !*args[3]) |
| 853 | 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] | 854 | |
| 855 | /* Lookup for the reference. */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 856 | ctx->ref = pat_ref_lookup_ref(args[2]); |
| 857 | if (!ctx->ref) { |
| 858 | if (ctx->display_flags == PAT_REF_MAP) |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 859 | return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n"); |
| 860 | else |
| 861 | 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] | 862 | } |
| 863 | |
Willy Tarreau | bb51c44 | 2021-04-30 15:23:36 +0200 | [diff] [blame] | 864 | if (gen) { |
| 865 | genid = str2uic(gen); |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 866 | if ((int)(genid - ctx->ref->next_gen) > 0) { |
| 867 | if (ctx->display_flags == PAT_REF_MAP) |
Willy Tarreau | bb51c44 | 2021-04-30 15:23:36 +0200 | [diff] [blame] | 868 | return cli_err(appctx, "Version number in the future, please use 'prepare map' before.\n"); |
| 869 | else |
| 870 | return cli_err(appctx, "Version number in the future, please use 'prepare acl' before.\n"); |
| 871 | } |
| 872 | } |
| 873 | |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 874 | /* The command "add acl" is prohibited if the reference |
| 875 | * use samples. |
| 876 | */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 877 | if ((ctx->display_flags & PAT_REF_ACL) && |
| 878 | (ctx->ref->flags & PAT_REF_SMP)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 879 | return cli_err(appctx, |
| 880 | "This ACL is shared with a map containing samples. " |
| 881 | "You must use the command 'add map' to add values.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 882 | } |
Willy Tarreau | f7dd0e8 | 2021-04-29 16:02:48 +0200 | [diff] [blame] | 883 | |
| 884 | /* Add value(s). If no payload is used, key and value are read |
| 885 | * from the command line and only one key is set. If a payload |
| 886 | * is passed, one key/value pair is read per line till the end |
| 887 | * of the payload is reached. |
| 888 | */ |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 889 | err = NULL; |
Aurélien Nephtali | 25650ce | 2018-04-18 14:04:47 +0200 | [diff] [blame] | 890 | |
Willy Tarreau | f7dd0e8 | 2021-04-29 16:02:48 +0200 | [diff] [blame] | 891 | do { |
| 892 | char *key = args[3]; |
| 893 | char *value = args[4]; |
| 894 | size_t l; |
Aurélien Nephtali | 25650ce | 2018-04-18 14:04:47 +0200 | [diff] [blame] | 895 | |
Willy Tarreau | f7dd0e8 | 2021-04-29 16:02:48 +0200 | [diff] [blame] | 896 | if (payload) { |
| 897 | /* key and value passed as payload, one pair per line */ |
| 898 | if (!*payload) |
| 899 | break; |
| 900 | |
Aurélien Nephtali | 25650ce | 2018-04-18 14:04:47 +0200 | [diff] [blame] | 901 | key = payload; |
| 902 | l = strcspn(key, " \t"); |
| 903 | payload += l; |
| 904 | |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 905 | if (!*payload && ctx->display_flags == PAT_REF_MAP) |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 906 | return cli_dynerr(appctx, memprintf(&err, "Missing value for key '%s'.\n", key)); |
| 907 | |
Aurélien Nephtali | 25650ce | 2018-04-18 14:04:47 +0200 | [diff] [blame] | 908 | key[l] = 0; |
| 909 | payload++; |
| 910 | |
| 911 | /* value */ |
| 912 | payload += strspn(payload, " \t"); |
| 913 | value = payload; |
| 914 | l = strcspn(value, "\n"); |
| 915 | payload += l; |
| 916 | if (*payload) |
| 917 | payload++; |
| 918 | value[l] = 0; |
Willy Tarreau | f7dd0e8 | 2021-04-29 16:02:48 +0200 | [diff] [blame] | 919 | } |
Aurélien Nephtali | 25650ce | 2018-04-18 14:04:47 +0200 | [diff] [blame] | 920 | |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 921 | if (ctx->display_flags != PAT_REF_MAP) |
Willy Tarreau | 4053b03 | 2021-04-29 16:55:17 +0200 | [diff] [blame] | 922 | value = NULL; |
| 923 | |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 924 | HA_SPIN_LOCK(PATREF_LOCK, &ctx->ref->lock); |
| 925 | ret = !!pat_ref_load(ctx->ref, gen ? genid : ctx->ref->curr_gen, key, value, -1, &err); |
| 926 | HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock); |
Willy Tarreau | 4053b03 | 2021-04-29 16:55:17 +0200 | [diff] [blame] | 927 | |
Willy Tarreau | f7dd0e8 | 2021-04-29 16:02:48 +0200 | [diff] [blame] | 928 | if (!ret) { |
| 929 | if (err) |
| 930 | return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); |
| 931 | else |
| 932 | return cli_err(appctx, "Failed to add a key.\n"); |
Aurélien Nephtali | 9a4da68 | 2018-04-16 19:02:42 +0200 | [diff] [blame] | 933 | } |
Willy Tarreau | f7dd0e8 | 2021-04-29 16:02:48 +0200 | [diff] [blame] | 934 | } while (payload && *payload); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 935 | |
| 936 | /* The add is done, send message. */ |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 937 | appctx->st0 = CLI_ST_PROMPT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 938 | return 1; |
| 939 | } |
| 940 | |
| 941 | return 0; |
| 942 | } |
| 943 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 944 | 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] | 945 | { |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 946 | struct show_map_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 947 | |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 948 | if (args[1][0] == 'm') |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 949 | ctx->display_flags = PAT_REF_MAP; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 950 | else |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 951 | ctx->display_flags = PAT_REF_ACL; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 952 | |
| 953 | /* Expect two parameters: map name and key. */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 954 | if (!*args[2] || !*args[3]) { |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 955 | if (ctx->display_flags == PAT_REF_MAP) |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 956 | return cli_err(appctx, "This command expects two parameters: map identifier and key.\n"); |
| 957 | else |
| 958 | 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] | 959 | } |
| 960 | |
| 961 | /* Lookup the reference in the maps. */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 962 | ctx->ref = pat_ref_lookup_ref(args[2]); |
| 963 | if (!ctx->ref || |
| 964 | !(ctx->ref->flags & ctx->display_flags)) |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 965 | 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] | 966 | |
| 967 | /* If the entry identifier start with a '#', it is considered as |
| 968 | * pointer id |
| 969 | */ |
| 970 | if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') { |
| 971 | struct pat_ref_elt *ref; |
| 972 | long long int conv; |
| 973 | char *error; |
| 974 | |
| 975 | /* Convert argument to integer value. */ |
| 976 | conv = strtoll(&args[3][1], &error, 16); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 977 | if (*error != '\0') |
| 978 | return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 979 | |
| 980 | /* Convert and check integer to pointer. */ |
| 981 | ref = (struct pat_ref_elt *)(long)conv; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 982 | if ((long long int)(long)ref != conv) |
| 983 | return cli_err(appctx, "Malformed identifier. Please use #<id> or <file>.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 984 | |
| 985 | /* Try to delete the entry. */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 986 | HA_SPIN_LOCK(PATREF_LOCK, &ctx->ref->lock); |
| 987 | if (!pat_ref_delete_by_id(ctx->ref, ref)) { |
| 988 | HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 989 | /* The entry is not found, send message. */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 990 | return cli_err(appctx, "Key not found.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 991 | } |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 992 | HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 993 | } |
| 994 | else { |
| 995 | /* Else, use the entry identifier as pattern |
| 996 | * string and try to delete the entry. |
| 997 | */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 998 | HA_SPIN_LOCK(PATREF_LOCK, &ctx->ref->lock); |
| 999 | if (!pat_ref_delete(ctx->ref, args[3])) { |
| 1000 | HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 1001 | /* The entry is not found, send message. */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 1002 | return cli_err(appctx, "Key not found.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 1003 | } |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 1004 | HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | /* The deletion is done, send message. */ |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 1008 | appctx->st0 = CLI_ST_PROMPT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 1009 | return 1; |
| 1010 | } |
| 1011 | |
Willy Tarreau | ff3feeb | 2021-04-30 13:31:43 +0200 | [diff] [blame] | 1012 | /* continue to clear a map which was started in the parser. The range of |
Willy Tarreau | 76f771e | 2022-05-03 14:12:56 +0200 | [diff] [blame] | 1013 | * generations this applies to is taken from ctx->curr_gen for the oldest |
| 1014 | * and ctx->prev_gen for the latest. |
Willy Tarreau | ff3feeb | 2021-04-30 13:31:43 +0200 | [diff] [blame] | 1015 | */ |
Willy Tarreau | d1d005d | 2019-12-20 18:22:02 +0100 | [diff] [blame] | 1016 | static int cli_io_handler_clear_map(struct appctx *appctx) |
| 1017 | { |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 1018 | struct show_map_ctx *ctx = appctx->svcctx; |
Willy Tarreau | d1d005d | 2019-12-20 18:22:02 +0100 | [diff] [blame] | 1019 | int finished; |
| 1020 | |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 1021 | HA_SPIN_LOCK(PATREF_LOCK, &ctx->ref->lock); |
Willy Tarreau | 76f771e | 2022-05-03 14:12:56 +0200 | [diff] [blame] | 1022 | finished = pat_ref_purge_range(ctx->ref, ctx->curr_gen, ctx->prev_gen, 100); |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 1023 | HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock); |
Willy Tarreau | d1d005d | 2019-12-20 18:22:02 +0100 | [diff] [blame] | 1024 | |
| 1025 | if (!finished) { |
| 1026 | /* let's come back later */ |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 1027 | applet_have_more_data(appctx); |
Willy Tarreau | d1d005d | 2019-12-20 18:22:02 +0100 | [diff] [blame] | 1028 | return 0; |
| 1029 | } |
Willy Tarreau | 88b1ec9 | 2023-11-04 07:55:37 +0100 | [diff] [blame] | 1030 | |
| 1031 | trim_all_pools(); |
Willy Tarreau | d1d005d | 2019-12-20 18:22:02 +0100 | [diff] [blame] | 1032 | return 1; |
| 1033 | } |
| 1034 | |
Willy Tarreau | 76f771e | 2022-05-03 14:12:56 +0200 | [diff] [blame] | 1035 | /* note: sets ctx->curr_gen and ctx->prev_gen to the oldest and |
Willy Tarreau | ff3feeb | 2021-04-30 13:31:43 +0200 | [diff] [blame] | 1036 | * latest generations to clear, respectively, and will call the clear_map |
| 1037 | * handler. |
| 1038 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 1039 | 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] | 1040 | { |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 1041 | struct show_map_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 1042 | |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 1043 | if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) { |
Willy Tarreau | ff3feeb | 2021-04-30 13:31:43 +0200 | [diff] [blame] | 1044 | const char *gen = NULL; |
| 1045 | |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 1046 | /* Set ACL or MAP flags. */ |
| 1047 | if (args[1][0] == 'm') |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 1048 | ctx->display_flags = PAT_REF_MAP; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 1049 | else |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 1050 | ctx->display_flags = PAT_REF_ACL; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 1051 | |
Willy Tarreau | ff3feeb | 2021-04-30 13:31:43 +0200 | [diff] [blame] | 1052 | /* For both "map" and "acl" we may have an optional generation |
| 1053 | * number specified using a "@" character before the pattern |
| 1054 | * file name. |
| 1055 | */ |
| 1056 | if (*args[2] == '@') { |
| 1057 | gen = args[2] + 1; |
| 1058 | args++; |
| 1059 | } |
| 1060 | |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 1061 | /* no parameter */ |
| 1062 | if (!*args[2]) { |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 1063 | if (ctx->display_flags == PAT_REF_MAP) |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 1064 | return cli_err(appctx, "Missing map identifier.\n"); |
| 1065 | else |
| 1066 | return cli_err(appctx, "Missing ACL identifier.\n"); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | /* lookup into the refs and check the map flag */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 1070 | ctx->ref = pat_ref_lookup_ref(args[2]); |
| 1071 | if (!ctx->ref || |
| 1072 | !(ctx->ref->flags & ctx->display_flags)) { |
| 1073 | if (ctx->display_flags == PAT_REF_MAP) |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 1074 | return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n"); |
| 1075 | else |
| 1076 | 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] | 1077 | } |
| 1078 | |
Willy Tarreau | 76f771e | 2022-05-03 14:12:56 +0200 | [diff] [blame] | 1079 | /* set the desired generation id in curr_gen/prev_gen */ |
Willy Tarreau | ff3feeb | 2021-04-30 13:31:43 +0200 | [diff] [blame] | 1080 | if (gen) |
Willy Tarreau | 76f771e | 2022-05-03 14:12:56 +0200 | [diff] [blame] | 1081 | ctx->prev_gen = ctx->curr_gen = str2uic(gen); |
Willy Tarreau | ff3feeb | 2021-04-30 13:31:43 +0200 | [diff] [blame] | 1082 | else |
Willy Tarreau | 76f771e | 2022-05-03 14:12:56 +0200 | [diff] [blame] | 1083 | ctx->prev_gen = ctx->curr_gen = ctx->ref->curr_gen; |
Willy Tarreau | ff3feeb | 2021-04-30 13:31:43 +0200 | [diff] [blame] | 1084 | |
Willy Tarreau | d1d005d | 2019-12-20 18:22:02 +0100 | [diff] [blame] | 1085 | /* delegate the clearing to the I/O handler which can yield */ |
| 1086 | return 0; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 1087 | } |
Willy Tarreau | d1d005d | 2019-12-20 18:22:02 +0100 | [diff] [blame] | 1088 | return 1; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 1089 | } |
| 1090 | |
Willy Tarreau | 76f771e | 2022-05-03 14:12:56 +0200 | [diff] [blame] | 1091 | /* note: sets ctx->curr_gen and ctx->prev_gen to the oldest and |
Willy Tarreau | 7a562ca | 2021-04-30 15:10:01 +0200 | [diff] [blame] | 1092 | * latest generations to clear, respectively, and will call the clear_map |
| 1093 | * handler. |
| 1094 | */ |
| 1095 | static int cli_parse_commit_map(char **args, char *payload, struct appctx *appctx, void *private) |
| 1096 | { |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 1097 | struct show_map_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 1098 | |
Willy Tarreau | 7a562ca | 2021-04-30 15:10:01 +0200 | [diff] [blame] | 1099 | if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) { |
| 1100 | const char *gen = NULL; |
| 1101 | uint genid; |
| 1102 | uint ret; |
| 1103 | |
| 1104 | /* Set ACL or MAP flags. */ |
| 1105 | if (args[1][0] == 'm') |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 1106 | ctx->display_flags = PAT_REF_MAP; |
Willy Tarreau | 7a562ca | 2021-04-30 15:10:01 +0200 | [diff] [blame] | 1107 | else |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 1108 | ctx->display_flags = PAT_REF_ACL; |
Willy Tarreau | 7a562ca | 2021-04-30 15:10:01 +0200 | [diff] [blame] | 1109 | |
| 1110 | if (*args[2] != '@') |
| 1111 | return cli_err(appctx, "Missing version number.\n"); |
| 1112 | |
| 1113 | /* The generation number is mandatory for a commit. The range |
| 1114 | * of generations that get trashed by a commit starts from the |
| 1115 | * opposite of the current one and ends at the previous one. |
| 1116 | */ |
| 1117 | gen = args[2] + 1; |
| 1118 | genid = str2uic(gen); |
Willy Tarreau | 76f771e | 2022-05-03 14:12:56 +0200 | [diff] [blame] | 1119 | ctx->prev_gen = genid - 1; |
| 1120 | ctx->curr_gen = ctx->prev_gen - ((~0U) >> 1); |
Willy Tarreau | 7a562ca | 2021-04-30 15:10:01 +0200 | [diff] [blame] | 1121 | |
| 1122 | /* no parameter */ |
| 1123 | if (!*args[3]) { |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 1124 | if (ctx->display_flags == PAT_REF_MAP) |
Willy Tarreau | 7a562ca | 2021-04-30 15:10:01 +0200 | [diff] [blame] | 1125 | return cli_err(appctx, "Missing map identifier.\n"); |
| 1126 | else |
| 1127 | return cli_err(appctx, "Missing ACL identifier.\n"); |
| 1128 | } |
| 1129 | |
| 1130 | /* lookup into the refs and check the map flag */ |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 1131 | ctx->ref = pat_ref_lookup_ref(args[3]); |
| 1132 | if (!ctx->ref || |
| 1133 | !(ctx->ref->flags & ctx->display_flags)) { |
| 1134 | if (ctx->display_flags == PAT_REF_MAP) |
Willy Tarreau | 7a562ca | 2021-04-30 15:10:01 +0200 | [diff] [blame] | 1135 | return cli_err(appctx, "Unknown map identifier. Please use #<id> or <file>.\n"); |
| 1136 | else |
| 1137 | return cli_err(appctx, "Unknown ACL identifier. Please use #<id> or <file>.\n"); |
| 1138 | } |
| 1139 | |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 1140 | HA_SPIN_LOCK(PATREF_LOCK, &ctx->ref->lock); |
| 1141 | if (genid - (ctx->ref->curr_gen + 1) < |
| 1142 | ctx->ref->next_gen - ctx->ref->curr_gen) |
| 1143 | ret = pat_ref_commit(ctx->ref, genid); |
Willy Tarreau | 7a562ca | 2021-04-30 15:10:01 +0200 | [diff] [blame] | 1144 | else |
| 1145 | ret = 1; |
Willy Tarreau | 0fcecc6 | 2022-05-03 11:54:47 +0200 | [diff] [blame] | 1146 | HA_SPIN_UNLOCK(PATREF_LOCK, &ctx->ref->lock); |
Willy Tarreau | 7a562ca | 2021-04-30 15:10:01 +0200 | [diff] [blame] | 1147 | |
| 1148 | if (ret != 0) |
| 1149 | return cli_err(appctx, "Version number out of range.\n"); |
| 1150 | |
| 1151 | /* delegate the clearing to the I/O handler which can yield */ |
| 1152 | return 0; |
| 1153 | } |
| 1154 | return 1; |
| 1155 | } |
| 1156 | |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 1157 | /* register cli keywords */ |
| 1158 | |
| 1159 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | b205bfd | 2021-05-07 11:38:37 +0200 | [diff] [blame] | 1160 | { { "add", "acl", NULL }, "add acl [@<ver>] <acl> <pattern> : add an acl entry", cli_parse_add_map, NULL }, |
| 1161 | { { "clear", "acl", NULL }, "clear acl [@<ver>] <acl> : clear the contents of this acl", cli_parse_clear_map, cli_io_handler_clear_map, NULL }, |
| 1162 | { { "commit","acl", NULL }, "commit acl @<ver> <acl> : commit the ACL at this version", cli_parse_commit_map, cli_io_handler_clear_map, NULL }, |
| 1163 | { { "del", "acl", NULL }, "del acl <acl> [<key>|#<ref>] : delete acl entries matching <key>", cli_parse_del_map, NULL }, |
| 1164 | { { "get", "acl", NULL }, "get acl <acl> <value> : report the patterns matching a sample for an ACL", cli_parse_get_map, cli_io_handler_map_lookup, cli_release_mlook }, |
| 1165 | { { "prepare","acl",NULL }, "prepare acl <acl> : prepare a new version for atomic ACL replacement", cli_parse_prepare_map, NULL }, |
| 1166 | { { "show", "acl", NULL }, "show acl [@<ver>] <acl>] : report available acls or dump an acl's contents", cli_parse_show_map, NULL }, |
| 1167 | { { "add", "map", NULL }, "add map [@<ver>] <map> <key> <val> : add a map entry (payload supported instead of key/val)", cli_parse_add_map, NULL }, |
| 1168 | { { "clear", "map", NULL }, "clear map [@<ver>] <map> : clear the contents of this map", cli_parse_clear_map, cli_io_handler_clear_map, NULL }, |
| 1169 | { { "commit","map", NULL }, "commit map @<ver> <map> : commit the map at this version", cli_parse_commit_map, cli_io_handler_clear_map, NULL }, |
| 1170 | { { "del", "map", NULL }, "del map <map> [<key>|#<ref>] : delete map entries matching <key>", cli_parse_del_map, NULL }, |
| 1171 | { { "get", "map", NULL }, "get map <acl> <value> : report the keys and values matching a sample for a map", cli_parse_get_map, cli_io_handler_map_lookup, cli_release_mlook }, |
| 1172 | { { "prepare","map",NULL }, "prepare map <acl> : prepare a new version for atomic map replacement", cli_parse_prepare_map, NULL }, |
| 1173 | { { "set", "map", NULL }, "set map <map> [<key>|#<ref>] <value> : modify a map entry", cli_parse_set_map, NULL }, |
| 1174 | { { "show", "map", NULL }, "show map [@ver] [map] : report available maps or dump a map's contents", cli_parse_show_map, NULL }, |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 1175 | { { NULL }, NULL, NULL, NULL } |
| 1176 | }}; |
| 1177 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1178 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 1179 | |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 1180 | /* Note: must not be declared <const> as its list will be overwritten |
| 1181 | * |
| 1182 | * For the map_*_int keywords, the output is declared as SMP_T_UINT, but the converter function |
| 1183 | * can provide SMP_T_UINT, SMP_T_SINT or SMP_T_BOOL depending on how the patterns found in the |
| 1184 | * file can be parsed. |
| 1185 | * |
| 1186 | * For the map_*_ip keyword, the output is declared as SMP_T_IPV4, but the converter function |
| 1187 | * can provide SMP_T_IPV4 or SMP_T_IPV6 depending on the patterns found in the file. |
| 1188 | * |
| 1189 | * The map_* keywords only emit strings. |
| 1190 | * |
| 1191 | * The output type is only used during the configuration parsing. It is used for detecting |
| 1192 | * compatibility problems. |
| 1193 | * |
| 1194 | * The arguments are: <file>[,<default value>] |
| 1195 | */ |
| 1196 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 1edc971 | 2014-12-15 16:18:39 +0100 | [diff] [blame] | 1197 | { "map", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR }, |
| 1198 | { "map_str", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR }, |
| 1199 | { "map_beg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_BEG }, |
| 1200 | { "map_sub", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_SUB }, |
| 1201 | { "map_dir", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DIR }, |
| 1202 | { "map_dom", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DOM }, |
| 1203 | { "map_end", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_END }, |
| 1204 | { "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] | 1205 | { "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] | 1206 | { "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] | 1207 | { "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] | 1208 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1209 | { "map_str_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_STR }, |
| 1210 | { "map_beg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_BEG }, |
| 1211 | { "map_sub_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_SUB }, |
| 1212 | { "map_dir_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DIR }, |
| 1213 | { "map_dom_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DOM }, |
| 1214 | { "map_end_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_END }, |
| 1215 | { "map_reg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_REG }, |
| 1216 | { "map_int_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_SINT, SMP_T_SINT, (void *)PAT_MATCH_INT }, |
| 1217 | { "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] | 1218 | |
Thierry FOURNIER | b2f8f08 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 1219 | { "map_str_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_STR }, |
| 1220 | { "map_beg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_BEG }, |
| 1221 | { "map_sub_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_SUB }, |
| 1222 | { "map_dir_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DIR }, |
| 1223 | { "map_dom_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DOM }, |
| 1224 | { "map_end_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_END }, |
| 1225 | { "map_reg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_REG }, |
| 1226 | { "map_int_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_SINT, SMP_T_ADDR, (void *)PAT_MATCH_INT }, |
| 1227 | { "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] | 1228 | |
| 1229 | { /* END */ }, |
| 1230 | }}; |
| 1231 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1232 | INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws); |