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; |
| 527 | free(appctx->ctx.map.chunk.str); |
| 528 | return 1; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | static void cli_release_mlook(struct appctx *appctx) |
| 533 | { |
| 534 | free(appctx->ctx.map.chunk.str); |
| 535 | appctx->ctx.map.chunk.str = NULL; |
| 536 | } |
| 537 | |
| 538 | |
| 539 | static int cli_parse_get_map(char **args, struct appctx *appctx, void *private) |
| 540 | { |
| 541 | if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) { |
| 542 | /* Set flags. */ |
| 543 | if (args[1][0] == 'm') |
| 544 | appctx->ctx.map.display_flags = PAT_REF_MAP; |
| 545 | else |
| 546 | appctx->ctx.map.display_flags = PAT_REF_ACL; |
| 547 | |
| 548 | /* No parameter. */ |
| 549 | if (!*args[2] || !*args[3]) { |
| 550 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 551 | appctx->ctx.cli.msg = "Missing map identifier and/or key.\n"; |
| 552 | else |
| 553 | appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 554 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 555 | return 1; |
| 556 | } |
| 557 | |
| 558 | /* lookup into the maps */ |
| 559 | appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); |
| 560 | if (!appctx->ctx.map.ref) { |
| 561 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 562 | appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n"; |
| 563 | else |
| 564 | 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] | 565 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 566 | return 1; |
| 567 | } |
| 568 | |
| 569 | /* copy input string. The string must be allocated because |
| 570 | * it may be used over multiple iterations. It's released |
| 571 | * at the end and upon abort anyway. |
| 572 | */ |
| 573 | appctx->ctx.map.chunk.len = strlen(args[3]); |
| 574 | appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1; |
| 575 | appctx->ctx.map.chunk.str = strdup(args[3]); |
| 576 | if (!appctx->ctx.map.chunk.str) { |
| 577 | appctx->ctx.cli.msg = "Out of memory error.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 578 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 579 | return 1; |
| 580 | } |
| 581 | |
| 582 | return 0; |
| 583 | } |
| 584 | return 1; |
| 585 | } |
| 586 | |
| 587 | static int cli_parse_show_map(char **args, struct appctx *appctx, void *private) |
| 588 | { |
| 589 | if (strcmp(args[1], "map") == 0 || |
| 590 | strcmp(args[1], "acl") == 0) { |
| 591 | |
| 592 | /* Set ACL or MAP flags. */ |
| 593 | if (args[1][0] == 'm') |
| 594 | appctx->ctx.map.display_flags = PAT_REF_MAP; |
| 595 | else |
| 596 | appctx->ctx.map.display_flags = PAT_REF_ACL; |
| 597 | |
| 598 | /* no parameter: display all map available */ |
| 599 | if (!*args[2]) { |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 600 | appctx->io_handler = cli_io_handler_pats_list; |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | /* lookup into the refs and check the map flag */ |
| 605 | appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); |
| 606 | if (!appctx->ctx.map.ref || |
| 607 | !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) { |
| 608 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 609 | appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n"; |
| 610 | else |
| 611 | 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] | 612 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 613 | return 1; |
| 614 | } |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 615 | appctx->io_handler = cli_io_handler_pat_list; |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | static int cli_parse_set_map(char **args, struct appctx *appctx, void *private) |
| 623 | { |
| 624 | if (strcmp(args[1], "map") == 0) { |
| 625 | char *err; |
| 626 | |
| 627 | /* Set flags. */ |
| 628 | appctx->ctx.map.display_flags = PAT_REF_MAP; |
| 629 | |
| 630 | /* Expect three parameters: map name, key and new value. */ |
| 631 | if (!*args[2] || !*args[3] || !*args[4]) { |
| 632 | 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] | 633 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 634 | return 1; |
| 635 | } |
| 636 | |
| 637 | /* Lookup the reference in the maps. */ |
| 638 | appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); |
| 639 | if (!appctx->ctx.map.ref) { |
| 640 | 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] | 641 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 642 | return 1; |
| 643 | } |
| 644 | |
| 645 | /* If the entry identifier start with a '#', it is considered as |
| 646 | * pointer id |
| 647 | */ |
| 648 | if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') { |
| 649 | struct pat_ref_elt *ref; |
| 650 | long long int conv; |
| 651 | char *error; |
| 652 | |
| 653 | /* Convert argument to integer value. */ |
| 654 | conv = strtoll(&args[3][1], &error, 16); |
| 655 | if (*error != '\0') { |
| 656 | appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 657 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 658 | return 1; |
| 659 | } |
| 660 | |
| 661 | /* Convert and check integer to pointer. */ |
| 662 | ref = (struct pat_ref_elt *)(long)conv; |
| 663 | if ((long long int)(long)ref != conv) { |
| 664 | appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 665 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 666 | return 1; |
| 667 | } |
| 668 | |
| 669 | /* Try to delete the entry. */ |
| 670 | err = NULL; |
| 671 | if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) { |
| 672 | if (err) |
| 673 | memprintf(&err, "%s.\n", err); |
| 674 | appctx->ctx.cli.err = err; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 675 | appctx->st0 = CLI_ST_PRINT_FREE; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 676 | return 1; |
| 677 | } |
| 678 | } |
| 679 | else { |
| 680 | /* Else, use the entry identifier as pattern |
| 681 | * string, and update the value. |
| 682 | */ |
| 683 | err = NULL; |
| 684 | if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) { |
| 685 | if (err) |
| 686 | memprintf(&err, "%s.\n", err); |
| 687 | appctx->ctx.cli.err = err; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 688 | appctx->st0 = CLI_ST_PRINT_FREE; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 689 | return 1; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | /* The set is done, send message. */ |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 694 | appctx->st0 = CLI_ST_PROMPT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 695 | return 0; |
| 696 | } |
| 697 | return 1; |
| 698 | } |
| 699 | |
| 700 | static int cli_parse_add_map(char **args, struct appctx *appctx, void *private) |
| 701 | { |
| 702 | if (strcmp(args[1], "map") == 0 || |
| 703 | strcmp(args[1], "acl") == 0) { |
| 704 | int ret; |
| 705 | char *err; |
| 706 | |
| 707 | /* Set flags. */ |
| 708 | if (args[1][0] == 'm') |
| 709 | appctx->ctx.map.display_flags = PAT_REF_MAP; |
| 710 | else |
| 711 | appctx->ctx.map.display_flags = PAT_REF_ACL; |
| 712 | |
| 713 | /* If the keywork is "map", we expect three parameters, if it |
| 714 | * is "acl", we expect only two parameters |
| 715 | */ |
| 716 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) { |
| 717 | if (!*args[2] || !*args[3] || !*args[4]) { |
| 718 | 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] | 719 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 720 | return 1; |
| 721 | } |
| 722 | } |
| 723 | else { |
| 724 | if (!*args[2] || !*args[3]) { |
| 725 | 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] | 726 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 727 | return 1; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | /* Lookup for the reference. */ |
| 732 | appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); |
| 733 | if (!appctx->ctx.map.ref) { |
| 734 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 735 | appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n"; |
| 736 | else |
| 737 | 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] | 738 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 739 | return 1; |
| 740 | } |
| 741 | |
| 742 | /* The command "add acl" is prohibited if the reference |
| 743 | * use samples. |
| 744 | */ |
| 745 | if ((appctx->ctx.map.display_flags & PAT_REF_ACL) && |
| 746 | (appctx->ctx.map.ref->flags & PAT_REF_SMP)) { |
| 747 | appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. " |
| 748 | "You must use the command 'add map' to add values.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 749 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 750 | return 1; |
| 751 | } |
| 752 | |
| 753 | /* Add value. */ |
| 754 | err = NULL; |
| 755 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 756 | ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err); |
| 757 | else |
| 758 | ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err); |
| 759 | if (!ret) { |
| 760 | if (err) |
| 761 | memprintf(&err, "%s.\n", err); |
| 762 | appctx->ctx.cli.err = err; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 763 | appctx->st0 = CLI_ST_PRINT_FREE; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 764 | return 1; |
| 765 | } |
| 766 | |
| 767 | /* The add is done, send message. */ |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 768 | appctx->st0 = CLI_ST_PROMPT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 769 | return 1; |
| 770 | } |
| 771 | |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | static int cli_parse_del_map(char **args, struct appctx *appctx, void *private) |
| 776 | { |
| 777 | if (args[1][0] == 'm') |
| 778 | appctx->ctx.map.display_flags = PAT_REF_MAP; |
| 779 | else |
| 780 | appctx->ctx.map.display_flags = PAT_REF_ACL; |
| 781 | |
| 782 | /* Expect two parameters: map name and key. */ |
| 783 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) { |
| 784 | if (!*args[2] || !*args[3]) { |
| 785 | 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] | 786 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 787 | return 1; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | else { |
| 792 | if (!*args[2] || !*args[3]) { |
| 793 | 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] | 794 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 795 | return 1; |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | /* Lookup the reference in the maps. */ |
| 800 | appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); |
| 801 | if (!appctx->ctx.map.ref || |
| 802 | !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) { |
| 803 | 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] | 804 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 805 | return 1; |
| 806 | } |
| 807 | |
| 808 | /* If the entry identifier start with a '#', it is considered as |
| 809 | * pointer id |
| 810 | */ |
| 811 | if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') { |
| 812 | struct pat_ref_elt *ref; |
| 813 | long long int conv; |
| 814 | char *error; |
| 815 | |
| 816 | /* Convert argument to integer value. */ |
| 817 | conv = strtoll(&args[3][1], &error, 16); |
| 818 | if (*error != '\0') { |
| 819 | appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 820 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 821 | return 1; |
| 822 | } |
| 823 | |
| 824 | /* Convert and check integer to pointer. */ |
| 825 | ref = (struct pat_ref_elt *)(long)conv; |
| 826 | if ((long long int)(long)ref != conv) { |
| 827 | appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 828 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 829 | return 1; |
| 830 | } |
| 831 | |
| 832 | /* Try to delete the entry. */ |
| 833 | if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) { |
| 834 | /* The entry is not found, send message. */ |
| 835 | appctx->ctx.cli.msg = "Key not found.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 836 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 837 | return 1; |
| 838 | } |
| 839 | } |
| 840 | else { |
| 841 | /* Else, use the entry identifier as pattern |
| 842 | * string and try to delete the entry. |
| 843 | */ |
| 844 | if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) { |
| 845 | /* The entry is not found, send message. */ |
| 846 | appctx->ctx.cli.msg = "Key not found.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 847 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 848 | return 1; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | /* The deletion is done, send message. */ |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 853 | appctx->st0 = CLI_ST_PROMPT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 854 | return 1; |
| 855 | } |
| 856 | |
| 857 | |
| 858 | static int cli_parse_clear_map(char **args, struct appctx *appctx, void *private) |
| 859 | { |
| 860 | if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) { |
| 861 | /* Set ACL or MAP flags. */ |
| 862 | if (args[1][0] == 'm') |
| 863 | appctx->ctx.map.display_flags = PAT_REF_MAP; |
| 864 | else |
| 865 | appctx->ctx.map.display_flags = PAT_REF_ACL; |
| 866 | |
| 867 | /* no parameter */ |
| 868 | if (!*args[2]) { |
| 869 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 870 | appctx->ctx.cli.msg = "Missing map identifier.\n"; |
| 871 | else |
| 872 | appctx->ctx.cli.msg = "Missing ACL identifier.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 873 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 874 | return 1; |
| 875 | } |
| 876 | |
| 877 | /* lookup into the refs and check the map flag */ |
| 878 | appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]); |
| 879 | if (!appctx->ctx.map.ref || |
| 880 | !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) { |
| 881 | if (appctx->ctx.map.display_flags == PAT_REF_MAP) |
| 882 | appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n"; |
| 883 | else |
| 884 | 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] | 885 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 886 | return 1; |
| 887 | } |
| 888 | |
| 889 | /* Clear all. */ |
| 890 | pat_ref_prune(appctx->ctx.map.ref); |
| 891 | |
| 892 | /* return response */ |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 893 | appctx->st0 = CLI_ST_PROMPT; |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 894 | return 1; |
| 895 | } |
| 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | /* register cli keywords */ |
| 900 | |
| 901 | static struct cli_kw_list cli_kws = {{ },{ |
| 902 | { { "add", "acl", NULL }, "add acl : add acl entry", cli_parse_add_map, NULL }, |
| 903 | { { "clear", "acl", NULL }, "clear acl <id> : clear the content of this acl", cli_parse_clear_map, NULL }, |
| 904 | { { "del", "acl", NULL }, "del acl : delete acl entry", cli_parse_del_map, NULL }, |
| 905 | { { "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 }, |
| 906 | { { "show", "acl", NULL }, "show acl [id] : report available acls or dump an acl's contents", cli_parse_show_map, NULL }, |
| 907 | { { "add", "map", NULL }, "add map : add map entry", cli_parse_add_map, NULL }, |
| 908 | { { "clear", "map", NULL }, "clear map <id> : clear the content of this map", cli_parse_clear_map, NULL }, |
| 909 | { { "del", "map", NULL }, "del map : delete map entry", cli_parse_del_map, NULL }, |
| 910 | { { "get", "map", NULL }, "get map : report the keys and values matching a sample for a map", cli_parse_get_map, NULL }, |
| 911 | { { "set", "map", NULL }, "set map : modify map entry", cli_parse_set_map, NULL }, |
| 912 | { { "show", "map", NULL }, "show map [id] : report available maps or dump a map's contents", cli_parse_show_map, NULL }, |
| 913 | { { NULL }, NULL, NULL, NULL } |
| 914 | }}; |
| 915 | |
| 916 | |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 917 | /* Note: must not be declared <const> as its list will be overwritten |
| 918 | * |
| 919 | * For the map_*_int keywords, the output is declared as SMP_T_UINT, but the converter function |
| 920 | * can provide SMP_T_UINT, SMP_T_SINT or SMP_T_BOOL depending on how the patterns found in the |
| 921 | * file can be parsed. |
| 922 | * |
| 923 | * For the map_*_ip keyword, the output is declared as SMP_T_IPV4, but the converter function |
| 924 | * can provide SMP_T_IPV4 or SMP_T_IPV6 depending on the patterns found in the file. |
| 925 | * |
| 926 | * The map_* keywords only emit strings. |
| 927 | * |
| 928 | * The output type is only used during the configuration parsing. It is used for detecting |
| 929 | * compatibility problems. |
| 930 | * |
| 931 | * The arguments are: <file>[,<default value>] |
| 932 | */ |
| 933 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 1edc971 | 2014-12-15 16:18:39 +0100 | [diff] [blame] | 934 | { "map", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR }, |
| 935 | { "map_str", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR }, |
| 936 | { "map_beg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_BEG }, |
| 937 | { "map_sub", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_SUB }, |
| 938 | { "map_dir", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DIR }, |
| 939 | { "map_dom", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DOM }, |
| 940 | { "map_end", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_END }, |
| 941 | { "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] | 942 | { "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] | 943 | { "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] | 944 | { "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] | 945 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 946 | { "map_str_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_STR }, |
| 947 | { "map_beg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_BEG }, |
| 948 | { "map_sub_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_SUB }, |
| 949 | { "map_dir_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DIR }, |
| 950 | { "map_dom_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_DOM }, |
| 951 | { "map_end_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_END }, |
| 952 | { "map_reg_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_STR, SMP_T_SINT, (void *)PAT_MATCH_REG }, |
| 953 | { "map_int_int", sample_conv_map, ARG2(1,STR,SINT), sample_load_map, SMP_T_SINT, SMP_T_SINT, (void *)PAT_MATCH_INT }, |
| 954 | { "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] | 955 | |
Thierry FOURNIER | b2f8f08 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 956 | { "map_str_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_STR }, |
| 957 | { "map_beg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_BEG }, |
| 958 | { "map_sub_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_SUB }, |
| 959 | { "map_dir_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DIR }, |
| 960 | { "map_dom_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_DOM }, |
| 961 | { "map_end_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_END }, |
| 962 | { "map_reg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_ADDR, (void *)PAT_MATCH_REG }, |
| 963 | { "map_int_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_SINT, SMP_T_ADDR, (void *)PAT_MATCH_INT }, |
| 964 | { "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] | 965 | |
| 966 | { /* END */ }, |
| 967 | }}; |
| 968 | |
| 969 | __attribute__((constructor)) |
| 970 | static void __map_init(void) |
| 971 | { |
| 972 | /* register format conversion keywords */ |
| 973 | sample_register_convs(&sample_conv_kws); |
William Lallemand | ad8be61 | 2016-11-18 19:26:17 +0100 | [diff] [blame] | 974 | cli_register_kw(&cli_kws); |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 975 | } |