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