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