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