Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Functions used to parse typed argument lists |
| 3 | * |
| 4 | * Copyright 2012 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 <sys/types.h> |
| 14 | #include <sys/socket.h> |
| 15 | #include <arpa/inet.h> |
| 16 | |
| 17 | #include <common/standard.h> |
| 18 | #include <proto/arg.h> |
| 19 | |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 20 | const char *arg_type_names[ARGT_NBTYPES] = { |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 21 | [ARGT_STOP] = "end of arguments", |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 22 | [ARGT_SINT] = "integer", |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 23 | [ARGT_STR] = "string", |
| 24 | [ARGT_IPV4] = "IPv4 address", |
| 25 | [ARGT_MSK4] = "IPv4 mask", |
| 26 | [ARGT_IPV6] = "IPv6 address", |
| 27 | [ARGT_MSK6] = "IPv6 mask", |
| 28 | [ARGT_TIME] = "delay", |
| 29 | [ARGT_SIZE] = "size", |
| 30 | [ARGT_FE] = "frontend", |
| 31 | [ARGT_BE] = "backend", |
| 32 | [ARGT_TAB] = "table", |
| 33 | [ARGT_SRV] = "server", |
| 34 | [ARGT_USR] = "user list", |
Willy Tarreau | 53c250e | 2015-01-19 18:58:20 +0100 | [diff] [blame] | 35 | [ARGT_MAP] = "map", |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 36 | [ARGT_REG] = "regex", |
Willy Tarreau | f7ead61 | 2015-09-21 20:57:12 +0200 | [diff] [blame] | 37 | [ARGT_VAR] = "variable", |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 38 | /* Unassigned types must never happen. Better crash during parsing if they do. */ |
| 39 | }; |
| 40 | |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 41 | /* This dummy arg list may be used by default when no arg is found, it helps |
| 42 | * parsers by removing pointer checks. |
| 43 | */ |
Willy Tarreau | 3d241e7 | 2015-01-19 18:44:07 +0100 | [diff] [blame] | 44 | struct arg empty_arg_list[ARGM_NBARGS] = { }; |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 45 | |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 46 | /* This function clones a struct arg_list template into a new one which is |
| 47 | * returned. |
| 48 | */ |
| 49 | struct arg_list *arg_list_clone(const struct arg_list *orig) |
| 50 | { |
| 51 | struct arg_list *new; |
| 52 | |
| 53 | if ((new = calloc(1, sizeof(*new))) != NULL) { |
| 54 | /* ->list will be set by the caller when inserting the element. |
| 55 | * ->arg and ->arg_pos will be set by the caller. |
| 56 | */ |
| 57 | new->ctx = orig->ctx; |
| 58 | new->kw = orig->kw; |
| 59 | new->conv = orig->conv; |
| 60 | new->file = orig->file; |
| 61 | new->line = orig->line; |
| 62 | } |
| 63 | return new; |
| 64 | } |
| 65 | |
| 66 | /* This function clones a struct <arg_list> template into a new one which is |
| 67 | * set to point to arg <arg> at pos <pos>, and which is returned if the caller |
| 68 | * wants to apply further changes. |
| 69 | */ |
| 70 | struct arg_list *arg_list_add(struct arg_list *orig, struct arg *arg, int pos) |
| 71 | { |
| 72 | struct arg_list *new; |
| 73 | |
| 74 | new = arg_list_clone(orig); |
| 75 | new->arg = arg; |
| 76 | new->arg_pos = pos; |
| 77 | LIST_ADDQ(&orig->list, &new->list); |
| 78 | return new; |
| 79 | } |
| 80 | |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 81 | /* This function builds an argument list from a config line. It returns the |
| 82 | * number of arguments found, or <0 in case of any error. Everything needed |
| 83 | * it automatically allocated. A pointer to an error message might be returned |
| 84 | * in err_msg if not NULL, in which case it would be allocated and the caller |
| 85 | * will have to check it and free it. The output arg list is returned in argp |
| 86 | * which must be valid. The returned array is always terminated by an arg of |
| 87 | * type ARGT_STOP (0), unless the mask indicates that no argument is supported. |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 88 | * Unresolved arguments are appended to arg list <al>, which also serves as a |
| 89 | * template to create new entries. The mask is composed of a number of |
Willy Tarreau | 3d241e7 | 2015-01-19 18:44:07 +0100 | [diff] [blame] | 90 | * mandatory arguments in its lower ARGM_BITS bits, and a concatenation of each |
| 91 | * argument type in each subsequent ARGT_BITS-bit sblock. If <err_msg> is not |
| 92 | * NULL, it must point to a freeable or NULL pointer. |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 93 | */ |
David Carlier | 15073a3 | 2016-03-15 19:00:35 +0000 | [diff] [blame] | 94 | int make_arg_list(const char *in, int len, uint64_t mask, struct arg **argp, |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 95 | char **err_msg, const char **err_ptr, int *err_arg, |
| 96 | struct arg_list *al) |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 97 | { |
| 98 | int nbarg; |
| 99 | int pos; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 100 | struct arg *arg; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 101 | const char *beg; |
| 102 | char *word = NULL; |
| 103 | const char *ptr_err = NULL; |
| 104 | int min_arg; |
| 105 | |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 106 | *argp = NULL; |
| 107 | |
Willy Tarreau | 3d241e7 | 2015-01-19 18:44:07 +0100 | [diff] [blame] | 108 | min_arg = mask & ARGM_MASK; |
| 109 | mask >>= ARGM_BITS; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 110 | |
| 111 | pos = 0; |
Willy Tarreau | 3d241e7 | 2015-01-19 18:44:07 +0100 | [diff] [blame] | 112 | /* find between 0 and NBARGS the max number of args supported by the mask */ |
| 113 | for (nbarg = 0; nbarg < ARGM_NBARGS && ((mask >> (nbarg * ARGT_BITS)) & ARGT_MASK); nbarg++); |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 114 | |
| 115 | if (!nbarg) |
| 116 | goto end_parse; |
| 117 | |
| 118 | /* Note: an empty input string contains an empty argument if this argument |
| 119 | * is marked mandatory. Otherwise we can ignore it. |
| 120 | */ |
| 121 | if (!len && !min_arg) |
| 122 | goto end_parse; |
| 123 | |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 124 | arg = *argp = calloc(nbarg + 1, sizeof(*arg)); |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 125 | |
| 126 | /* Note: empty arguments after a comma always exist. */ |
| 127 | while (pos < nbarg) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 128 | unsigned int uint; |
| 129 | |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 130 | beg = in; |
| 131 | while (len && *in != ',') { |
| 132 | in++; |
| 133 | len--; |
| 134 | } |
| 135 | |
| 136 | /* we have a new argument between <beg> and <in> (not included). |
| 137 | * For ease of handling, we copy it into a zero-terminated word. |
| 138 | * By default, the output argument will be the same type of the |
| 139 | * expected one. |
| 140 | */ |
| 141 | free(word); |
| 142 | word = my_strndup(beg, in - beg); |
| 143 | |
Willy Tarreau | 3d241e7 | 2015-01-19 18:44:07 +0100 | [diff] [blame] | 144 | arg->type = (mask >> (pos * ARGT_BITS)) & ARGT_MASK; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 145 | |
| 146 | switch (arg->type) { |
| 147 | case ARGT_SINT: |
| 148 | if (in == beg) // empty number |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 149 | goto empty_err; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 150 | arg->data.sint = read_int64(&beg, in); |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 151 | if (beg < in) |
| 152 | goto parse_err; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 153 | arg->type = ARGT_SINT; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 154 | break; |
| 155 | |
| 156 | case ARGT_FE: |
| 157 | case ARGT_BE: |
| 158 | case ARGT_TAB: |
| 159 | case ARGT_SRV: |
| 160 | case ARGT_USR: |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 161 | case ARGT_REG: |
Willy Tarreau | 496aa01 | 2012-06-01 10:38:29 +0200 | [diff] [blame] | 162 | /* These argument types need to be stored as strings during |
| 163 | * parsing then resolved later. |
| 164 | */ |
| 165 | arg->unresolved = 1; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 166 | arg_list_add(al, arg, pos); |
| 167 | |
Willy Tarreau | 496aa01 | 2012-06-01 10:38:29 +0200 | [diff] [blame] | 168 | /* fall through */ |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 169 | case ARGT_STR: |
| 170 | /* all types that must be resolved are stored as strings |
| 171 | * during the parsing. The caller must at one point resolve |
| 172 | * them and free the string. |
| 173 | */ |
| 174 | arg->data.str.str = word; |
| 175 | arg->data.str.len = in - beg; |
| 176 | arg->data.str.size = arg->data.str.len + 1; |
| 177 | word = NULL; |
| 178 | break; |
| 179 | |
| 180 | case ARGT_IPV4: |
| 181 | if (in == beg) // empty address |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 182 | goto empty_err; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 183 | |
| 184 | if (inet_pton(AF_INET, word, &arg->data.ipv4) <= 0) |
| 185 | goto parse_err; |
| 186 | break; |
| 187 | |
| 188 | case ARGT_MSK4: |
| 189 | if (in == beg) // empty mask |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 190 | goto empty_err; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 191 | |
| 192 | if (!str2mask(word, &arg->data.ipv4)) |
| 193 | goto parse_err; |
| 194 | |
| 195 | arg->type = ARGT_IPV4; |
| 196 | break; |
| 197 | |
| 198 | case ARGT_IPV6: |
| 199 | if (in == beg) // empty address |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 200 | goto empty_err; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 201 | |
| 202 | if (inet_pton(AF_INET6, word, &arg->data.ipv6) <= 0) |
| 203 | goto parse_err; |
| 204 | break; |
| 205 | |
| 206 | case ARGT_MSK6: /* not yet implemented */ |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 207 | goto not_impl; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 208 | |
| 209 | case ARGT_TIME: |
| 210 | if (in == beg) // empty time |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 211 | goto empty_err; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 212 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 213 | ptr_err = parse_time_err(word, &uint, TIME_UNIT_MS); |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 214 | if (ptr_err) |
| 215 | goto parse_err; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 216 | arg->data.sint = uint; |
| 217 | arg->type = ARGT_SINT; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 218 | break; |
| 219 | |
| 220 | case ARGT_SIZE: |
| 221 | if (in == beg) // empty size |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 222 | goto empty_err; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 223 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 224 | ptr_err = parse_size_err(word, &uint); |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 225 | if (ptr_err) |
| 226 | goto parse_err; |
| 227 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 228 | arg->data.sint = uint; |
| 229 | arg->type = ARGT_SINT; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 230 | break; |
| 231 | |
| 232 | /* FIXME: other types need to be implemented here */ |
| 233 | default: |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 234 | goto not_impl; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | pos++; |
| 238 | arg++; |
| 239 | |
| 240 | /* don't go back to parsing if we reached end */ |
| 241 | if (!len || pos >= nbarg) |
| 242 | break; |
| 243 | |
| 244 | /* skip comma */ |
| 245 | in++; len--; |
| 246 | } |
| 247 | |
| 248 | end_parse: |
| 249 | free(word); word = NULL; |
| 250 | |
| 251 | if (pos < min_arg) { |
| 252 | /* not enough arguments */ |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 253 | memprintf(err_msg, |
Willy Tarreau | b111d42 | 2013-12-13 00:38:47 +0100 | [diff] [blame] | 254 | "missing arguments (got %d/%d), type '%s' expected", |
Willy Tarreau | 3d241e7 | 2015-01-19 18:44:07 +0100 | [diff] [blame] | 255 | pos, min_arg, arg_type_names[(mask >> (pos * ARGT_BITS)) & ARGT_MASK]); |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 256 | goto err; |
| 257 | } |
| 258 | |
| 259 | if (len) { |
| 260 | /* too many arguments, starting at <in> */ |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 261 | /* the caller is responsible for freeing this message */ |
| 262 | word = my_strndup(in, len); |
Willy Tarreau | b111d42 | 2013-12-13 00:38:47 +0100 | [diff] [blame] | 263 | if (nbarg) |
| 264 | memprintf(err_msg, "end of arguments expected at position %d, but got '%s'", |
| 265 | pos + 1, word); |
| 266 | else |
| 267 | memprintf(err_msg, "no argument supported, but got '%s'", word); |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 268 | free(word); word = NULL; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 269 | goto err; |
| 270 | } |
| 271 | |
| 272 | /* note that pos might be < nbarg and this is not an error, it's up to the |
| 273 | * caller to decide what to do with optional args. |
| 274 | */ |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 275 | if (err_arg) |
| 276 | *err_arg = pos; |
| 277 | if (err_ptr) |
| 278 | *err_ptr = in; |
| 279 | return pos; |
| 280 | |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 281 | err: |
| 282 | free(word); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 283 | free(*argp); |
Willy Tarreau | 681e49d | 2013-12-06 15:30:05 +0100 | [diff] [blame] | 284 | *argp = NULL; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 285 | if (err_arg) |
| 286 | *err_arg = pos; |
| 287 | if (err_ptr) |
| 288 | *err_ptr = in; |
| 289 | return -1; |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 290 | |
| 291 | empty_err: |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 292 | memprintf(err_msg, "expected type '%s' at position %d, but got nothing", |
Willy Tarreau | 3d241e7 | 2015-01-19 18:44:07 +0100 | [diff] [blame] | 293 | arg_type_names[(mask >> (pos * ARGT_BITS)) & ARGT_MASK], pos + 1); |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 294 | goto err; |
| 295 | |
| 296 | parse_err: |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 297 | memprintf(err_msg, "failed to parse '%s' as type '%s' at position %d", |
Willy Tarreau | 3d241e7 | 2015-01-19 18:44:07 +0100 | [diff] [blame] | 298 | word, arg_type_names[(mask >> (pos * ARGT_BITS)) & ARGT_MASK], pos + 1); |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 299 | goto err; |
| 300 | |
| 301 | not_impl: |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 302 | memprintf(err_msg, "parsing for type '%s' was not implemented, please report this bug", |
Willy Tarreau | 3d241e7 | 2015-01-19 18:44:07 +0100 | [diff] [blame] | 303 | arg_type_names[(mask >> (pos * ARGT_BITS)) & ARGT_MASK]); |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 304 | goto err; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 305 | } |