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; |
| 302 | |
| 303 | switch (appctx->st2) { |
| 304 | |
| 305 | case STAT_ST_INIT: |
| 306 | /* Init to the first entry. The list cannot be change */ |
| 307 | appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head, |
| 308 | struct pat_ref_elt *, list); |
| 309 | if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head) |
| 310 | appctx->ctx.map.elt = NULL; |
| 311 | appctx->st2 = STAT_ST_LIST; |
| 312 | /* fall through */ |
| 313 | |
| 314 | case STAT_ST_LIST: |
| 315 | while (appctx->ctx.map.elt) { |
| 316 | chunk_reset(&trash); |
| 317 | |
| 318 | /* build messages */ |
| 319 | if (appctx->ctx.map.elt->sample) |
| 320 | chunk_appendf(&trash, "%p %s %s\n", |
| 321 | appctx->ctx.map.elt, appctx->ctx.map.elt->pattern, |
| 322 | appctx->ctx.map.elt->sample); |
| 323 | else |
| 324 | chunk_appendf(&trash, "%p %s\n", |
| 325 | appctx->ctx.map.elt, appctx->ctx.map.elt->pattern); |
| 326 | |
| 327 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 328 | /* let's try again later from this stream. We add ourselves into |
| 329 | * this stream's users so that it can remove us upon termination. |
| 330 | */ |
| 331 | si_applet_cant_put(si); |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | /* get next list entry and check the end of the list */ |
| 336 | appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list, |
| 337 | struct pat_ref_elt *, list); |
| 338 | if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head) |
| 339 | break; |
| 340 | } |
| 341 | |
| 342 | appctx->st2 = STAT_ST_FIN; |
| 343 | /* fall through */ |
| 344 | |
| 345 | default: |
| 346 | appctx->st2 = STAT_ST_FIN; |
| 347 | return 1; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | static int cli_io_handler_pats_list(struct appctx *appctx) |
| 352 | { |
| 353 | struct stream_interface *si = appctx->owner; |
| 354 | |
| 355 | switch (appctx->st2) { |
| 356 | case STAT_ST_INIT: |
| 357 | /* Display the column headers. If the message cannot be sent, |
| 358 | * quit the fucntion with returning 0. The function is called |
| 359 | * later and restart at the state "STAT_ST_INIT". |
| 360 | */ |
| 361 | chunk_reset(&trash); |
| 362 | chunk_appendf(&trash, "# id (file) description\n"); |
| 363 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 364 | si_applet_cant_put(si); |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | /* Now, we start the browsing of the references lists. |
| 369 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 370 | * available field of this pointer is <list>. It is used with the function |
| 371 | * pat_list_get_next() for retruning the first available entry |
| 372 | */ |
| 373 | appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list); |
| 374 | appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference, |
| 375 | appctx->ctx.map.display_flags); |
| 376 | appctx->st2 = STAT_ST_LIST; |
| 377 | /* fall through */ |
| 378 | |
| 379 | case STAT_ST_LIST: |
| 380 | while (appctx->ctx.map.ref) { |
| 381 | chunk_reset(&trash); |
| 382 | |
| 383 | /* Build messages. If the reference is used by another category than |
| 384 | * the listed categorie, display the information in the massage. |
| 385 | */ |
| 386 | chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id, |
| 387 | appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "", |
| 388 | appctx->ctx.map.ref->display); |
| 389 | |
| 390 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 391 | /* let's try again later from this stream. We add ourselves into |
| 392 | * this stream's users so that it can remove us upon termination. |
| 393 | */ |
| 394 | si_applet_cant_put(si); |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | /* get next list entry and check the end of the list */ |
| 399 | appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference, |
| 400 | appctx->ctx.map.display_flags); |
| 401 | } |
| 402 | |
| 403 | appctx->st2 = STAT_ST_FIN; |
| 404 | /* fall through */ |
| 405 | |
| 406 | default: |
| 407 | appctx->st2 = STAT_ST_FIN; |
| 408 | return 1; |
| 409 | } |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | static int cli_io_handler_map_lookup(struct appctx *appctx) |
| 414 | { |
| 415 | struct stream_interface *si = appctx->owner; |
| 416 | struct sample sample; |
| 417 | struct pattern *pat; |
| 418 | int match_method; |
| 419 | |
| 420 | switch (appctx->st2) { |
| 421 | case STAT_ST_INIT: |
| 422 | /* Init to the first entry. The list cannot be change */ |
| 423 | appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list); |
| 424 | appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat); |
| 425 | appctx->st2 = STAT_ST_LIST; |
| 426 | /* fall through */ |
| 427 | |
| 428 | case STAT_ST_LIST: |
| 429 | /* for each lookup type */ |
| 430 | while (appctx->ctx.map.expr) { |
| 431 | /* initialise chunk to build new message */ |
| 432 | chunk_reset(&trash); |
| 433 | |
| 434 | /* execute pattern matching */ |
| 435 | sample.data.type = SMP_T_STR; |
| 436 | sample.flags = SMP_F_CONST; |
| 437 | sample.data.u.str.len = appctx->ctx.map.chunk.len; |
| 438 | sample.data.u.str.str = appctx->ctx.map.chunk.str; |
| 439 | if (appctx->ctx.map.expr->pat_head->match && |
| 440 | sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type)) |
| 441 | pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1); |
| 442 | else |
| 443 | pat = NULL; |
| 444 | |
| 445 | /* build return message: set type of match */ |
| 446 | for (match_method=0; match_method<PAT_MATCH_NUM; match_method++) |
| 447 | if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method]) |
| 448 | break; |
| 449 | if (match_method >= PAT_MATCH_NUM) |
| 450 | chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match); |
| 451 | else |
| 452 | chunk_appendf(&trash, "type=%s", pat_match_names[match_method]); |
| 453 | |
| 454 | /* case sensitive */ |
| 455 | if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE) |
| 456 | chunk_appendf(&trash, ", case=insensitive"); |
| 457 | else |
| 458 | chunk_appendf(&trash, ", case=sensitive"); |
| 459 | |
| 460 | /* Display no match, and set default value */ |
| 461 | if (!pat) { |
| 462 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 463 | chunk_appendf(&trash, ", found=no"); |
| 464 | else |
| 465 | chunk_appendf(&trash, ", match=no"); |
| 466 | } |
| 467 | |
| 468 | /* Display match and match info */ |
| 469 | else { |
| 470 | /* display match */ |
| 471 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 472 | chunk_appendf(&trash, ", found=yes"); |
| 473 | else |
| 474 | chunk_appendf(&trash, ", match=yes"); |
| 475 | |
| 476 | /* display index mode */ |
| 477 | if (pat->sflags & PAT_SF_TREE) |
| 478 | chunk_appendf(&trash, ", idx=tree"); |
| 479 | else |
| 480 | chunk_appendf(&trash, ", idx=list"); |
| 481 | |
| 482 | /* display pattern */ |
| 483 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) { |
| 484 | if (pat->ref && pat->ref->pattern) |
| 485 | chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern); |
| 486 | else |
| 487 | chunk_appendf(&trash, ", key=unknown"); |
| 488 | } |
| 489 | else { |
| 490 | if (pat->ref && pat->ref->pattern) |
| 491 | chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern); |
| 492 | else |
| 493 | chunk_appendf(&trash, ", pattern=unknown"); |
| 494 | } |
| 495 | |
| 496 | /* display return value */ |
| 497 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) { |
| 498 | if (pat->data && pat->ref && pat->ref->sample) |
| 499 | chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample, |
| 500 | smp_to_type[pat->data->type]); |
| 501 | else |
| 502 | chunk_appendf(&trash, ", value=none"); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | chunk_appendf(&trash, "\n"); |
| 507 | |
| 508 | /* display response */ |
| 509 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 510 | /* let's try again later from this stream. We add ourselves into |
| 511 | * this stream's users so that it can remove us upon termination. |
| 512 | */ |
| 513 | si_applet_cant_put(si); |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | /* get next entry */ |
| 518 | appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, |
| 519 | &appctx->ctx.map.ref->pat); |
| 520 | } |
| 521 | |
| 522 | appctx->st2 = STAT_ST_FIN; |
| 523 | /* fall through */ |
| 524 | |
| 525 | default: |
| 526 | appctx->st2 = STAT_ST_FIN; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 527 | return 1; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | static void cli_release_mlook(struct appctx *appctx) |
| 532 | { |
| 533 | free(appctx->ctx.map.chunk.str); |
| 534 | appctx->ctx.map.chunk.str = NULL; |
| 535 | } |
| 536 | |
| 537 | |
| 538 | static int cli_parse_get_map(char **args, struct appctx *appctx, void *private) |
| 539 | { |
| 540 | if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) { |
| 541 | /* Set flags. */ |
| 542 | if (args[1][0] == 'm') |
| 543 | appctx->ctx.map.display_flags = PAT_REF_MAP; |
| 544 | else |
| 545 | appctx->ctx.map.display_flags = PAT_REF_ACL; |
| 546 | |
| 547 | /* No parameter. */ |
| 548 | if (!*args[2] || !*args[3]) { |
| 549 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 550 | appctx->ctx.cli.msg = "Missing map identifier and/or key.\n"; |
| 551 | else |
| 552 | appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 553 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 554 | return 1; |
| 555 | } |
| 556 | |
| 557 | /* lookup into the maps */ |
| 558 | appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); |
| 559 | if (!appctx->ctx.map.ref) { |
| 560 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 561 | appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n"; |
| 562 | else |
| 563 | 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] | 564 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 565 | return 1; |
| 566 | } |
| 567 | |
| 568 | /* copy input string. The string must be allocated because |
| 569 | * it may be used over multiple iterations. It's released |
| 570 | * at the end and upon abort anyway. |
| 571 | */ |
| 572 | appctx->ctx.map.chunk.len = strlen(args[3]); |
| 573 | appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1; |
| 574 | appctx->ctx.map.chunk.str = strdup(args[3]); |
| 575 | if (!appctx->ctx.map.chunk.str) { |
| 576 | appctx->ctx.cli.msg = "Out of memory error.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 577 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 578 | return 1; |
| 579 | } |
| 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | return 1; |
| 584 | } |
| 585 | |
| 586 | static int cli_parse_show_map(char **args, struct appctx *appctx, void *private) |
| 587 | { |
| 588 | if (strcmp(args[1], "map") == 0 || |
| 589 | strcmp(args[1], "acl") == 0) { |
| 590 | |
| 591 | /* Set ACL or MAP flags. */ |
| 592 | if (args[1][0] == 'm') |
| 593 | appctx->ctx.map.display_flags = PAT_REF_MAP; |
| 594 | else |
| 595 | appctx->ctx.map.display_flags = PAT_REF_ACL; |
| 596 | |
| 597 | /* no parameter: display all map available */ |
| 598 | if (!*args[2]) { |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 599 | appctx->io_handler = cli_io_handler_pats_list; |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | /* lookup into the refs and check the map flag */ |
| 604 | appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); |
| 605 | if (!appctx->ctx.map.ref || |
| 606 | !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) { |
| 607 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 608 | appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n"; |
| 609 | else |
| 610 | 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] | 611 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 612 | return 1; |
| 613 | } |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 614 | appctx->io_handler = cli_io_handler_pat_list; |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | static int cli_parse_set_map(char **args, struct appctx *appctx, void *private) |
| 622 | { |
| 623 | if (strcmp(args[1], "map") == 0) { |
| 624 | char *err; |
| 625 | |
| 626 | /* Set flags. */ |
| 627 | appctx->ctx.map.display_flags = PAT_REF_MAP; |
| 628 | |
| 629 | /* Expect three parameters: map name, key and new value. */ |
| 630 | if (!*args[2] || !*args[3] || !*args[4]) { |
| 631 | 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] | 632 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 633 | return 1; |
| 634 | } |
| 635 | |
| 636 | /* Lookup the reference in the maps. */ |
| 637 | appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); |
| 638 | if (!appctx->ctx.map.ref) { |
| 639 | 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] | 640 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 641 | return 1; |
| 642 | } |
| 643 | |
| 644 | /* If the entry identifier start with a '#', it is considered as |
| 645 | * pointer id |
| 646 | */ |
| 647 | if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') { |
| 648 | struct pat_ref_elt *ref; |
| 649 | long long int conv; |
| 650 | char *error; |
| 651 | |
| 652 | /* Convert argument to integer value. */ |
| 653 | conv = strtoll(&args[3][1], &error, 16); |
| 654 | if (*error != '\0') { |
| 655 | appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 656 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 657 | return 1; |
| 658 | } |
| 659 | |
| 660 | /* Convert and check integer to pointer. */ |
| 661 | ref = (struct pat_ref_elt *)(long)conv; |
| 662 | if ((long long int)(long)ref != conv) { |
| 663 | appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\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 | /* Try to delete the entry. */ |
| 669 | err = NULL; |
| 670 | if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) { |
| 671 | if (err) |
| 672 | memprintf(&err, "%s.\n", err); |
| 673 | appctx->ctx.cli.err = err; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 674 | appctx->st0 = CLI_ST_PRINT_FREE; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 675 | return 1; |
| 676 | } |
| 677 | } |
| 678 | else { |
| 679 | /* Else, use the entry identifier as pattern |
| 680 | * string, and update the value. |
| 681 | */ |
| 682 | err = NULL; |
| 683 | if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) { |
| 684 | if (err) |
| 685 | memprintf(&err, "%s.\n", err); |
| 686 | appctx->ctx.cli.err = err; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 687 | appctx->st0 = CLI_ST_PRINT_FREE; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 688 | return 1; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | /* The set is done, send message. */ |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 693 | appctx->st0 = CLI_ST_PROMPT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 694 | return 0; |
| 695 | } |
| 696 | return 1; |
| 697 | } |
| 698 | |
| 699 | static int cli_parse_add_map(char **args, struct appctx *appctx, void *private) |
| 700 | { |
| 701 | if (strcmp(args[1], "map") == 0 || |
| 702 | strcmp(args[1], "acl") == 0) { |
| 703 | int ret; |
| 704 | char *err; |
| 705 | |
| 706 | /* Set flags. */ |
| 707 | if (args[1][0] == 'm') |
| 708 | appctx->ctx.map.display_flags = PAT_REF_MAP; |
| 709 | else |
| 710 | appctx->ctx.map.display_flags = PAT_REF_ACL; |
| 711 | |
| 712 | /* If the keywork is "map", we expect three parameters, if it |
| 713 | * is "acl", we expect only two parameters |
| 714 | */ |
| 715 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) { |
| 716 | if (!*args[2] || !*args[3] || !*args[4]) { |
| 717 | 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] | 718 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 719 | return 1; |
| 720 | } |
| 721 | } |
| 722 | else { |
| 723 | if (!*args[2] || !*args[3]) { |
| 724 | 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] | 725 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 726 | return 1; |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | /* Lookup for the reference. */ |
| 731 | appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); |
| 732 | if (!appctx->ctx.map.ref) { |
| 733 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 734 | appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n"; |
| 735 | else |
| 736 | 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] | 737 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 738 | return 1; |
| 739 | } |
| 740 | |
| 741 | /* The command "add acl" is prohibited if the reference |
| 742 | * use samples. |
| 743 | */ |
| 744 | if ((appctx->ctx.map.display_flags & PAT_REF_ACL) && |
| 745 | (appctx->ctx.map.ref->flags & PAT_REF_SMP)) { |
| 746 | appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. " |
| 747 | "You must use the command 'add map' to add values.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 748 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 749 | return 1; |
| 750 | } |
| 751 | |
| 752 | /* Add value. */ |
| 753 | err = NULL; |
| 754 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 755 | ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err); |
| 756 | else |
| 757 | ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err); |
| 758 | if (!ret) { |
| 759 | if (err) |
| 760 | memprintf(&err, "%s.\n", err); |
| 761 | appctx->ctx.cli.err = err; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 762 | appctx->st0 = CLI_ST_PRINT_FREE; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 763 | return 1; |
| 764 | } |
| 765 | |
| 766 | /* The add is done, send message. */ |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 767 | appctx->st0 = CLI_ST_PROMPT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 768 | return 1; |
| 769 | } |
| 770 | |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | static int cli_parse_del_map(char **args, struct appctx *appctx, void *private) |
| 775 | { |
| 776 | if (args[1][0] == 'm') |
| 777 | appctx->ctx.map.display_flags = PAT_REF_MAP; |
| 778 | else |
| 779 | appctx->ctx.map.display_flags = PAT_REF_ACL; |
| 780 | |
| 781 | /* Expect two parameters: map name and key. */ |
| 782 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) { |
| 783 | if (!*args[2] || !*args[3]) { |
| 784 | 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] | 785 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 786 | return 1; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | else { |
| 791 | if (!*args[2] || !*args[3]) { |
| 792 | 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] | 793 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 794 | return 1; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | /* Lookup the reference in the maps. */ |
| 799 | appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); |
| 800 | if (!appctx->ctx.map.ref || |
| 801 | !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) { |
| 802 | 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] | 803 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 804 | return 1; |
| 805 | } |
| 806 | |
| 807 | /* If the entry identifier start with a '#', it is considered as |
| 808 | * pointer id |
| 809 | */ |
| 810 | if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') { |
| 811 | struct pat_ref_elt *ref; |
| 812 | long long int conv; |
| 813 | char *error; |
| 814 | |
| 815 | /* Convert argument to integer value. */ |
| 816 | conv = strtoll(&args[3][1], &error, 16); |
| 817 | if (*error != '\0') { |
| 818 | appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 819 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 820 | return 1; |
| 821 | } |
| 822 | |
| 823 | /* Convert and check integer to pointer. */ |
| 824 | ref = (struct pat_ref_elt *)(long)conv; |
| 825 | if ((long long int)(long)ref != conv) { |
| 826 | appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 827 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 828 | return 1; |
| 829 | } |
| 830 | |
| 831 | /* Try to delete the entry. */ |
| 832 | if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) { |
| 833 | /* The entry is not found, send message. */ |
| 834 | appctx->ctx.cli.msg = "Key not found.\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 | else { |
| 840 | /* Else, use the entry identifier as pattern |
| 841 | * string and try to delete the entry. |
| 842 | */ |
| 843 | if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) { |
| 844 | /* The entry is not found, send message. */ |
| 845 | appctx->ctx.cli.msg = "Key not found.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 846 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 847 | return 1; |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | /* The deletion is done, send message. */ |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 852 | appctx->st0 = CLI_ST_PROMPT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 853 | return 1; |
| 854 | } |
| 855 | |
| 856 | |
| 857 | static int cli_parse_clear_map(char **args, struct appctx *appctx, void *private) |
| 858 | { |
| 859 | if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) { |
| 860 | /* Set ACL or MAP flags. */ |
| 861 | if (args[1][0] == 'm') |
| 862 | appctx->ctx.map.display_flags = PAT_REF_MAP; |
| 863 | else |
| 864 | appctx->ctx.map.display_flags = PAT_REF_ACL; |
| 865 | |
| 866 | /* no parameter */ |
| 867 | if (!*args[2]) { |
| 868 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 869 | appctx->ctx.cli.msg = "Missing map identifier.\n"; |
| 870 | else |
| 871 | appctx->ctx.cli.msg = "Missing ACL identifier.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 872 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 873 | return 1; |
| 874 | } |
| 875 | |
| 876 | /* lookup into the refs and check the map flag */ |
| 877 | appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); |
| 878 | if (!appctx->ctx.map.ref || |
| 879 | !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) { |
| 880 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 881 | appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n"; |
| 882 | else |
| 883 | 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] | 884 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 885 | return 1; |
| 886 | } |
| 887 | |
| 888 | /* Clear all. */ |
| 889 | pat_ref_prune(appctx->ctx.map.ref); |
| 890 | |
| 891 | /* return response */ |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 892 | appctx->st0 = CLI_ST_PROMPT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 893 | return 1; |
| 894 | } |
| 895 | return 0; |
| 896 | } |
| 897 | |
| 898 | /* register cli keywords */ |
| 899 | |
| 900 | static struct cli_kw_list cli_kws = {{ },{ |
| 901 | { { "add", "acl", NULL }, "add acl : add acl entry", cli_parse_add_map, NULL }, |
| 902 | { { "clear", "acl", NULL }, "clear acl <id> : clear the content of this acl", cli_parse_clear_map, NULL }, |
| 903 | { { "del", "acl", NULL }, "del acl : delete acl entry", cli_parse_del_map, NULL }, |
| 904 | { { "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 }, |
| 905 | { { "show", "acl", NULL }, "show acl [id] : report available acls or dump an acl's contents", cli_parse_show_map, NULL }, |
| 906 | { { "add", "map", NULL }, "add map : add map entry", cli_parse_add_map, NULL }, |
| 907 | { { "clear", "map", NULL }, "clear map <id> : clear the content of this map", cli_parse_clear_map, NULL }, |
| 908 | { { "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] | 909 | { { "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] | 910 | { { "set", "map", NULL }, "set map : modify map entry", cli_parse_set_map, NULL }, |
| 911 | { { "show", "map", NULL }, "show map [id] : report available maps or dump a map's contents", cli_parse_show_map, NULL }, |
| 912 | { { NULL }, NULL, NULL, NULL } |
| 913 | }}; |
| 914 | |
| 915 | |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 916 | /* Note: must not be declared <const> as its list will be overwritten |
| 917 | * |
| 918 | * For the map_*_int keywords, the output is declared as SMP_T_UINT, but the converter function |
| 919 | * can provide SMP_T_UINT, SMP_T_SINT or SMP_T_BOOL depending on how the patterns found in the |
| 920 | * file can be parsed. |
| 921 | * |
| 922 | * For the map_*_ip keyword, the output is declared as SMP_T_IPV4, but the converter function |
| 923 | * can provide SMP_T_IPV4 or SMP_T_IPV6 depending on the patterns found in the file. |
| 924 | * |
| 925 | * The map_* keywords only emit strings. |
| 926 | * |
| 927 | * The output type is only used during the configuration parsing. It is used for detecting |
| 928 | * compatibility problems. |
| 929 | * |
| 930 | * The arguments are: <file>[,<default value>] |
| 931 | */ |
| 932 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 1edc971 | 2014-12-15 16:18:39 +0100 | [diff] [blame] | 933 | { "map", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR }, |
| 934 | { "map_str", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR }, |
| 935 | { "map_beg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_BEG }, |
| 936 | { "map_sub", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_SUB }, |
| 937 | { "map_dir", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DIR }, |
| 938 | { "map_dom", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DOM }, |
| 939 | { "map_end", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_END }, |
| 940 | { "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] | 941 | { "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] | 942 | { "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] | 943 | { "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] | 944 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 945 | { "map_str_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_STR }, |
| 946 | { "map_beg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_BEG }, |
| 947 | { "map_sub_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_SUB }, |
| 948 | { "map_dir_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DIR }, |
| 949 | { "map_dom_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DOM }, |
| 950 | { "map_end_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_END }, |
| 951 | { "map_reg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_REG }, |
| 952 | { "map_int_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_SINT, SMP_T_SINT, (void *)PAT_MATCH_INT }, |
| 953 | { "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] | 954 | |
Thierry FOURNIER | b2f8f08 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 955 | { "map_str_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_STR }, |
| 956 | { "map_beg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_BEG }, |
| 957 | { "map_sub_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_SUB }, |
| 958 | { "map_dir_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DIR }, |
| 959 | { "map_dom_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DOM }, |
| 960 | { "map_end_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_END }, |
| 961 | { "map_reg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_REG }, |
| 962 | { "map_int_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_SINT, SMP_T_ADDR, (void *)PAT_MATCH_INT }, |
| 963 | { "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] | 964 | |
| 965 | { /* END */ }, |
| 966 | }}; |
| 967 | |
| 968 | __attribute__((constructor)) |
| 969 | static void __map_init(void) |
| 970 | { |
| 971 | /* register format conversion keywords */ |
| 972 | sample_register_convs(&sample_conv_kws); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 973 | cli_register_kw(&cli_kws); |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 974 | } |