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