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