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 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 17 | #include <haproxy/arg.h> |
Willy Tarreau | c13ed53 | 2020-06-02 10:22:45 +0200 | [diff] [blame] | 18 | #include <haproxy/chunk.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 19 | #include <haproxy/global.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 20 | #include <haproxy/tools.h> |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 21 | |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 22 | const char *arg_type_names[ARGT_NBTYPES] = { |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 23 | [ARGT_STOP] = "end of arguments", |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 24 | [ARGT_SINT] = "integer", |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 25 | [ARGT_STR] = "string", |
| 26 | [ARGT_IPV4] = "IPv4 address", |
| 27 | [ARGT_MSK4] = "IPv4 mask", |
| 28 | [ARGT_IPV6] = "IPv6 address", |
| 29 | [ARGT_MSK6] = "IPv6 mask", |
| 30 | [ARGT_TIME] = "delay", |
| 31 | [ARGT_SIZE] = "size", |
| 32 | [ARGT_FE] = "frontend", |
| 33 | [ARGT_BE] = "backend", |
| 34 | [ARGT_TAB] = "table", |
| 35 | [ARGT_SRV] = "server", |
| 36 | [ARGT_USR] = "user list", |
Willy Tarreau | 53c250e | 2015-01-19 18:58:20 +0100 | [diff] [blame] | 37 | [ARGT_MAP] = "map", |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 38 | [ARGT_REG] = "regex", |
Willy Tarreau | f7ead61 | 2015-09-21 20:57:12 +0200 | [diff] [blame] | 39 | [ARGT_VAR] = "variable", |
Frédéric Lécaille | 3a463c9 | 2019-02-25 15:20:35 +0100 | [diff] [blame] | 40 | [ARGT_PBUF_FNUM] = "Protocol buffers field number", |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 41 | /* Unassigned types must never happen. Better crash during parsing if they do. */ |
| 42 | }; |
| 43 | |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 44 | /* This dummy arg list may be used by default when no arg is found, it helps |
| 45 | * parsers by removing pointer checks. |
| 46 | */ |
Willy Tarreau | 3d241e7 | 2015-01-19 18:44:07 +0100 | [diff] [blame] | 47 | struct arg empty_arg_list[ARGM_NBARGS] = { }; |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 48 | |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 49 | /* This function clones a struct arg_list template into a new one which is |
| 50 | * returned. |
| 51 | */ |
| 52 | struct arg_list *arg_list_clone(const struct arg_list *orig) |
| 53 | { |
| 54 | struct arg_list *new; |
| 55 | |
| 56 | if ((new = calloc(1, sizeof(*new))) != NULL) { |
| 57 | /* ->list will be set by the caller when inserting the element. |
| 58 | * ->arg and ->arg_pos will be set by the caller. |
| 59 | */ |
| 60 | new->ctx = orig->ctx; |
| 61 | new->kw = orig->kw; |
| 62 | new->conv = orig->conv; |
| 63 | new->file = orig->file; |
| 64 | new->line = orig->line; |
| 65 | } |
| 66 | return new; |
| 67 | } |
| 68 | |
| 69 | /* This function clones a struct <arg_list> template into a new one which is |
| 70 | * set to point to arg <arg> at pos <pos>, and which is returned if the caller |
| 71 | * wants to apply further changes. |
| 72 | */ |
| 73 | struct arg_list *arg_list_add(struct arg_list *orig, struct arg *arg, int pos) |
| 74 | { |
| 75 | struct arg_list *new; |
| 76 | |
| 77 | new = arg_list_clone(orig); |
Willy Tarreau | a9e2e4b | 2017-04-12 22:28:52 +0200 | [diff] [blame] | 78 | if (new) { |
| 79 | new->arg = arg; |
| 80 | new->arg_pos = pos; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 81 | LIST_APPEND(&orig->list, &new->list); |
Willy Tarreau | a9e2e4b | 2017-04-12 22:28:52 +0200 | [diff] [blame] | 82 | } |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 83 | return new; |
| 84 | } |
| 85 | |
Willy Tarreau | 80b53ff | 2020-02-14 08:40:37 +0100 | [diff] [blame] | 86 | /* This function builds an argument list from a config line, and stops at the |
| 87 | * first non-matching character, which is pointed to in <end_ptr>. A valid arg |
| 88 | * list starts with an opening parenthesis '(', contains a number of comma- |
| 89 | * delimited words, and ends with the closing parenthesis ')'. An empty list |
| 90 | * (with or without the parenthesis) will lead to a valid empty argument if the |
| 91 | * keyword has a mandatory one. The function returns the number of arguments |
| 92 | * emitted, or <0 in case of any error. Everything needed it automatically |
| 93 | * allocated. A pointer to an error message might be returned in err_msg if not |
| 94 | * NULL, in which case it would be allocated and the caller will have to check |
| 95 | * it and free it. The output arg list is returned in argp which must be valid. |
| 96 | * The returned array is always terminated by an arg of type ARGT_STOP (0), |
| 97 | * unless the mask indicates that no argument is supported. Unresolved arguments |
| 98 | * are appended to arg list <al>, which also serves as a template to create new |
| 99 | * entries. The mask is composed of a number of mandatory arguments in its lower |
| 100 | * ARGM_BITS bits, and a concatenation of each argument type in each subsequent |
| 101 | * ARGT_BITS-bit sblock. If <err_msg> is not NULL, it must point to a freeable |
| 102 | * or NULL pointer. The caller is expected to restart the parsing from the new |
| 103 | * pointer set in <end_ptr>, which is the first character considered as not |
| 104 | * being part of the arg list. The input string ends on the first between <len> |
| 105 | * characters (when len is positive) or the first NUL character. Placing -1 in |
| 106 | * <len> will make it virtually unbounded (~2GB long strings). |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 107 | */ |
David Carlier | 15073a3 | 2016-03-15 19:00:35 +0000 | [diff] [blame] | 108 | int make_arg_list(const char *in, int len, uint64_t mask, struct arg **argp, |
Willy Tarreau | 80b53ff | 2020-02-14 08:40:37 +0100 | [diff] [blame] | 109 | char **err_msg, const char **end_ptr, int *err_arg, |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 110 | struct arg_list *al) |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 111 | { |
| 112 | int nbarg; |
| 113 | int pos; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 114 | struct arg *arg; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 115 | const char *beg; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 116 | const char *ptr_err = NULL; |
| 117 | int min_arg; |
Willy Tarreau | 80b53ff | 2020-02-14 08:40:37 +0100 | [diff] [blame] | 118 | int empty; |
Willy Tarreau | 0622f02 | 2017-04-12 22:32:04 +0200 | [diff] [blame] | 119 | struct arg_list *new_al = al; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 120 | |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 121 | *argp = NULL; |
| 122 | |
Willy Tarreau | 80b53ff | 2020-02-14 08:40:37 +0100 | [diff] [blame] | 123 | empty = 0; |
| 124 | if (!len || *in != '(') { |
| 125 | /* it's already not for us, stop here */ |
| 126 | empty = 1; |
| 127 | len = 0; |
| 128 | } else { |
| 129 | /* skip opening parenthesis */ |
| 130 | len--; |
| 131 | in++; |
| 132 | } |
| 133 | |
Willy Tarreau | 3d241e7 | 2015-01-19 18:44:07 +0100 | [diff] [blame] | 134 | min_arg = mask & ARGM_MASK; |
| 135 | mask >>= ARGM_BITS; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 136 | |
| 137 | pos = 0; |
Willy Tarreau | 3d241e7 | 2015-01-19 18:44:07 +0100 | [diff] [blame] | 138 | /* find between 0 and NBARGS the max number of args supported by the mask */ |
| 139 | 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] | 140 | |
| 141 | if (!nbarg) |
| 142 | goto end_parse; |
| 143 | |
| 144 | /* Note: an empty input string contains an empty argument if this argument |
| 145 | * is marked mandatory. Otherwise we can ignore it. |
| 146 | */ |
Willy Tarreau | 80b53ff | 2020-02-14 08:40:37 +0100 | [diff] [blame] | 147 | if (empty && !min_arg) |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 148 | goto end_parse; |
| 149 | |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 150 | arg = *argp = calloc(nbarg + 1, sizeof(**argp)); |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 151 | |
Remi Tricot-Le Breton | 6d303d6 | 2021-05-19 12:00:54 +0200 | [diff] [blame] | 152 | if (!arg) |
| 153 | goto alloc_err; |
| 154 | |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 155 | /* Note: empty arguments after a comma always exist. */ |
| 156 | while (pos < nbarg) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 157 | unsigned int uint; |
Willy Tarreau | ef21fac | 2020-02-14 13:37:20 +0100 | [diff] [blame] | 158 | int squote = 0, dquote = 0; |
| 159 | char *out; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 160 | |
Willy Tarreau | ef21fac | 2020-02-14 13:37:20 +0100 | [diff] [blame] | 161 | chunk_reset(&trash); |
| 162 | out = trash.area; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 163 | |
Willy Tarreau | ef21fac | 2020-02-14 13:37:20 +0100 | [diff] [blame] | 164 | while (len && *in && trash.data < trash.size - 1) { |
| 165 | if (*in == '"' && !squote) { /* double quote outside single quotes */ |
| 166 | if (dquote) |
| 167 | dquote = 0; |
| 168 | else |
| 169 | dquote = 1; |
| 170 | in++; len--; |
| 171 | continue; |
| 172 | } |
| 173 | else if (*in == '\'' && !dquote) { /* single quote outside double quotes */ |
| 174 | if (squote) |
| 175 | squote = 0; |
| 176 | else |
| 177 | squote = 1; |
| 178 | in++; len--; |
| 179 | continue; |
| 180 | } |
| 181 | else if (*in == '\\' && !squote && len != 1) { |
| 182 | /* '\', ', ' ', '"' support being escaped by '\' */ |
| 183 | if (len == 1 || in[1] == 0) |
| 184 | goto unquote_err; |
| 185 | |
| 186 | if (in[1] == '\\' || in[1] == ' ' || in[1] == '"' || in[1] == '\'') { |
| 187 | in++; len--; |
| 188 | *out++ = *in; |
| 189 | } |
| 190 | else if (in[1] == 'r') { |
| 191 | in++; len--; |
| 192 | *out++ = '\r'; |
| 193 | } |
| 194 | else if (in[1] == 'n') { |
| 195 | in++; len--; |
| 196 | *out++ = '\n'; |
| 197 | } |
| 198 | else if (in[1] == 't') { |
| 199 | in++; len--; |
| 200 | *out++ = '\t'; |
| 201 | } |
| 202 | else { |
| 203 | /* just a lone '\' */ |
| 204 | *out++ = *in; |
| 205 | } |
| 206 | in++; len--; |
| 207 | } |
| 208 | else { |
| 209 | if (!squote && !dquote && (*in == ',' || *in == ')')) { |
| 210 | /* end of argument */ |
| 211 | break; |
| 212 | } |
| 213 | /* verbatim copy */ |
| 214 | *out++ = *in++; |
| 215 | len--; |
| 216 | } |
| 217 | trash.data = out - trash.area; |
| 218 | } |
Willy Tarreau | 807aef8 | 2020-02-15 14:54:28 +0100 | [diff] [blame] | 219 | |
Willy Tarreau | 9af749b | 2020-02-16 10:46:37 +0100 | [diff] [blame] | 220 | if (len && *in && *in != ',' && *in != ')') |
Willy Tarreau | 807aef8 | 2020-02-15 14:54:28 +0100 | [diff] [blame] | 221 | goto buffer_err; |
| 222 | |
Willy Tarreau | ef21fac | 2020-02-14 13:37:20 +0100 | [diff] [blame] | 223 | trash.area[trash.data] = 0; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 224 | |
Willy Tarreau | 3d241e7 | 2015-01-19 18:44:07 +0100 | [diff] [blame] | 225 | arg->type = (mask >> (pos * ARGT_BITS)) & ARGT_MASK; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 226 | |
| 227 | switch (arg->type) { |
| 228 | case ARGT_SINT: |
Willy Tarreau | 338c670 | 2020-02-14 11:34:35 +0100 | [diff] [blame] | 229 | if (!trash.data) // empty number |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 230 | goto empty_err; |
Willy Tarreau | 338c670 | 2020-02-14 11:34:35 +0100 | [diff] [blame] | 231 | beg = trash.area; |
| 232 | arg->data.sint = read_int64(&beg, trash.area + trash.data); |
| 233 | if (beg < trash.area + trash.data) |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 234 | goto parse_err; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 235 | arg->type = ARGT_SINT; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 236 | break; |
| 237 | |
| 238 | case ARGT_FE: |
| 239 | case ARGT_BE: |
| 240 | case ARGT_TAB: |
| 241 | case ARGT_SRV: |
| 242 | case ARGT_USR: |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 243 | case ARGT_REG: |
Willy Tarreau | 496aa01 | 2012-06-01 10:38:29 +0200 | [diff] [blame] | 244 | /* These argument types need to be stored as strings during |
| 245 | * parsing then resolved later. |
| 246 | */ |
| 247 | arg->unresolved = 1; |
Willy Tarreau | 0622f02 | 2017-04-12 22:32:04 +0200 | [diff] [blame] | 248 | new_al = arg_list_add(al, arg, pos); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 249 | |
Willy Tarreau | 496aa01 | 2012-06-01 10:38:29 +0200 | [diff] [blame] | 250 | /* fall through */ |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 251 | case ARGT_STR: |
| 252 | /* all types that must be resolved are stored as strings |
| 253 | * during the parsing. The caller must at one point resolve |
| 254 | * them and free the string. |
| 255 | */ |
Willy Tarreau | 338c670 | 2020-02-14 11:34:35 +0100 | [diff] [blame] | 256 | arg->data.str.area = my_strndup(trash.area, trash.data); |
| 257 | arg->data.str.data = trash.data; |
| 258 | arg->data.str.size = trash.data + 1; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 259 | break; |
| 260 | |
| 261 | case ARGT_IPV4: |
Willy Tarreau | 338c670 | 2020-02-14 11:34:35 +0100 | [diff] [blame] | 262 | if (!trash.data) // empty address |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 263 | goto empty_err; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 264 | |
Willy Tarreau | 338c670 | 2020-02-14 11:34:35 +0100 | [diff] [blame] | 265 | if (inet_pton(AF_INET, trash.area, &arg->data.ipv4) <= 0) |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 266 | goto parse_err; |
| 267 | break; |
| 268 | |
| 269 | case ARGT_MSK4: |
Willy Tarreau | 338c670 | 2020-02-14 11:34:35 +0100 | [diff] [blame] | 270 | if (!trash.data) // empty mask |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 271 | goto empty_err; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 272 | |
Willy Tarreau | 338c670 | 2020-02-14 11:34:35 +0100 | [diff] [blame] | 273 | if (!str2mask(trash.area, &arg->data.ipv4)) |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 274 | goto parse_err; |
| 275 | |
| 276 | arg->type = ARGT_IPV4; |
| 277 | break; |
| 278 | |
| 279 | case ARGT_IPV6: |
Willy Tarreau | 338c670 | 2020-02-14 11:34:35 +0100 | [diff] [blame] | 280 | if (!trash.data) // empty address |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 281 | goto empty_err; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 282 | |
Willy Tarreau | 338c670 | 2020-02-14 11:34:35 +0100 | [diff] [blame] | 283 | if (inet_pton(AF_INET6, trash.area, &arg->data.ipv6) <= 0) |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 284 | goto parse_err; |
| 285 | break; |
| 286 | |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 287 | case ARGT_MSK6: |
Willy Tarreau | 338c670 | 2020-02-14 11:34:35 +0100 | [diff] [blame] | 288 | if (!trash.data) // empty mask |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 289 | goto empty_err; |
| 290 | |
Willy Tarreau | 338c670 | 2020-02-14 11:34:35 +0100 | [diff] [blame] | 291 | if (!str2mask6(trash.area, &arg->data.ipv6)) |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 292 | goto parse_err; |
| 293 | |
| 294 | arg->type = ARGT_IPV6; |
| 295 | break; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 296 | |
| 297 | case ARGT_TIME: |
Willy Tarreau | 338c670 | 2020-02-14 11:34:35 +0100 | [diff] [blame] | 298 | if (!trash.data) // empty time |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 299 | goto empty_err; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 300 | |
Willy Tarreau | 338c670 | 2020-02-14 11:34:35 +0100 | [diff] [blame] | 301 | ptr_err = parse_time_err(trash.area, &uint, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 302 | if (ptr_err) { |
| 303 | if (ptr_err == PARSE_TIME_OVER || ptr_err == PARSE_TIME_UNDER) |
Willy Tarreau | 338c670 | 2020-02-14 11:34:35 +0100 | [diff] [blame] | 304 | ptr_err = trash.area; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 305 | goto parse_err; |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 306 | } |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 307 | arg->data.sint = uint; |
| 308 | arg->type = ARGT_SINT; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 309 | break; |
| 310 | |
| 311 | case ARGT_SIZE: |
Willy Tarreau | 338c670 | 2020-02-14 11:34:35 +0100 | [diff] [blame] | 312 | if (!trash.data) // empty size |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 313 | goto empty_err; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 314 | |
Willy Tarreau | 338c670 | 2020-02-14 11:34:35 +0100 | [diff] [blame] | 315 | ptr_err = parse_size_err(trash.area, &uint); |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 316 | if (ptr_err) |
| 317 | goto parse_err; |
| 318 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 319 | arg->data.sint = uint; |
| 320 | arg->type = ARGT_SINT; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 321 | break; |
| 322 | |
Frédéric Lécaille | 3a463c9 | 2019-02-25 15:20:35 +0100 | [diff] [blame] | 323 | case ARGT_PBUF_FNUM: |
Willy Tarreau | 338c670 | 2020-02-14 11:34:35 +0100 | [diff] [blame] | 324 | if (!trash.data) |
Frédéric Lécaille | 756d97f | 2019-03-04 19:03:48 +0100 | [diff] [blame] | 325 | goto empty_err; |
| 326 | |
Willy Tarreau | 338c670 | 2020-02-14 11:34:35 +0100 | [diff] [blame] | 327 | if (!parse_dotted_uints(trash.area, &arg->data.fid.ids, &arg->data.fid.sz)) |
Frédéric Lécaille | 3a463c9 | 2019-02-25 15:20:35 +0100 | [diff] [blame] | 328 | goto parse_err; |
| 329 | |
| 330 | break; |
| 331 | |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 332 | /* FIXME: other types need to be implemented here */ |
| 333 | default: |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 334 | goto not_impl; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | pos++; |
| 338 | arg++; |
| 339 | |
| 340 | /* don't go back to parsing if we reached end */ |
Willy Tarreau | 80b53ff | 2020-02-14 08:40:37 +0100 | [diff] [blame] | 341 | if (!len || !*in || *in == ')' || pos >= nbarg) |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 342 | break; |
| 343 | |
| 344 | /* skip comma */ |
| 345 | in++; len--; |
| 346 | } |
| 347 | |
| 348 | end_parse: |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 349 | if (pos < min_arg) { |
| 350 | /* not enough arguments */ |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 351 | memprintf(err_msg, |
Willy Tarreau | b111d42 | 2013-12-13 00:38:47 +0100 | [diff] [blame] | 352 | "missing arguments (got %d/%d), type '%s' expected", |
Willy Tarreau | 3d241e7 | 2015-01-19 18:44:07 +0100 | [diff] [blame] | 353 | pos, min_arg, arg_type_names[(mask >> (pos * ARGT_BITS)) & ARGT_MASK]); |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 354 | goto err; |
| 355 | } |
| 356 | |
Willy Tarreau | 80b53ff | 2020-02-14 08:40:37 +0100 | [diff] [blame] | 357 | if (empty) { |
| 358 | /* nothing to do */ |
| 359 | } else if (*in == ')') { |
| 360 | /* skip the expected closing parenthesis */ |
| 361 | in++; |
| 362 | } else { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 363 | /* the caller is responsible for freeing this message */ |
Willy Tarreau | 338c670 | 2020-02-14 11:34:35 +0100 | [diff] [blame] | 364 | char *word = (len > 0) ? my_strndup(in, len) : (char *)in; |
Willy Tarreau | 3e293a9 | 2021-05-06 14:50:30 +0200 | [diff] [blame] | 365 | |
| 366 | if (*word) |
| 367 | memprintf(err_msg, "expected ')' before '%s'", word); |
| 368 | else |
| 369 | memprintf(err_msg, "expected ')'"); |
| 370 | |
Willy Tarreau | 80b53ff | 2020-02-14 08:40:37 +0100 | [diff] [blame] | 371 | if (len > 0) |
| 372 | free(word); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 373 | /* when we're missing a right paren, the empty part preceding |
Willy Tarreau | 80b53ff | 2020-02-14 08:40:37 +0100 | [diff] [blame] | 374 | * already created an empty arg, adding one to the position, so |
| 375 | * let's fix the reporting to avoid being confusing. |
| 376 | */ |
| 377 | if (pos > 1) |
| 378 | pos--; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 379 | goto err; |
| 380 | } |
| 381 | |
| 382 | /* note that pos might be < nbarg and this is not an error, it's up to the |
| 383 | * caller to decide what to do with optional args. |
| 384 | */ |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 385 | if (err_arg) |
| 386 | *err_arg = pos; |
Willy Tarreau | 80b53ff | 2020-02-14 08:40:37 +0100 | [diff] [blame] | 387 | if (end_ptr) |
| 388 | *end_ptr = in; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 389 | return pos; |
| 390 | |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 391 | err: |
Willy Tarreau | 0622f02 | 2017-04-12 22:32:04 +0200 | [diff] [blame] | 392 | if (new_al == al) { |
| 393 | /* only free the arg area if we have not queued unresolved args |
| 394 | * still pointing to it. |
| 395 | */ |
| 396 | free(*argp); |
| 397 | } |
Willy Tarreau | 681e49d | 2013-12-06 15:30:05 +0100 | [diff] [blame] | 398 | *argp = NULL; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 399 | if (err_arg) |
| 400 | *err_arg = pos; |
Willy Tarreau | 80b53ff | 2020-02-14 08:40:37 +0100 | [diff] [blame] | 401 | if (end_ptr) |
| 402 | *end_ptr = in; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 403 | return -1; |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 404 | |
| 405 | empty_err: |
Willy Tarreau | 75fd2ff | 2020-07-21 15:44:38 +0200 | [diff] [blame] | 406 | /* If we've only got an empty set of parenthesis with nothing |
| 407 | * in between, there is no arg at all. |
| 408 | */ |
| 409 | if (!pos) { |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 410 | ha_free(argp); |
Willy Tarreau | 75fd2ff | 2020-07-21 15:44:38 +0200 | [diff] [blame] | 411 | } |
| 412 | |
Willy Tarreau | 77e463f | 2020-02-28 16:41:29 +0100 | [diff] [blame] | 413 | if (pos >= min_arg) |
| 414 | goto end_parse; |
| 415 | |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 416 | memprintf(err_msg, "expected type '%s' at position %d, but got nothing", |
Willy Tarreau | 3d241e7 | 2015-01-19 18:44:07 +0100 | [diff] [blame] | 417 | arg_type_names[(mask >> (pos * ARGT_BITS)) & ARGT_MASK], pos + 1); |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 418 | goto err; |
| 419 | |
| 420 | parse_err: |
Willy Tarreau | 338c670 | 2020-02-14 11:34:35 +0100 | [diff] [blame] | 421 | /* come here with the word attempted to parse in trash */ |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 422 | memprintf(err_msg, "failed to parse '%s' as type '%s' at position %d", |
Willy Tarreau | 338c670 | 2020-02-14 11:34:35 +0100 | [diff] [blame] | 423 | trash.area, arg_type_names[(mask >> (pos * ARGT_BITS)) & ARGT_MASK], pos + 1); |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 424 | goto err; |
| 425 | |
| 426 | not_impl: |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 427 | 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] | 428 | arg_type_names[(mask >> (pos * ARGT_BITS)) & ARGT_MASK]); |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 429 | goto err; |
Willy Tarreau | 338c670 | 2020-02-14 11:34:35 +0100 | [diff] [blame] | 430 | |
| 431 | buffer_err: |
| 432 | memprintf(err_msg, "too small buffer size to store decoded argument %d, increase bufsize ?", |
| 433 | pos + 1); |
| 434 | goto err; |
Willy Tarreau | ef21fac | 2020-02-14 13:37:20 +0100 | [diff] [blame] | 435 | |
| 436 | unquote_err: |
| 437 | /* come here with the parsed part in <trash.area>:<trash.data> and the |
| 438 | * unparsable part in <in>. |
| 439 | */ |
| 440 | trash.area[trash.data] = 0; |
| 441 | memprintf(err_msg, "failed to parse '%s' after '%s' as type '%s' at position %d", |
| 442 | in, trash.area, arg_type_names[(mask >> (pos * ARGT_BITS)) & ARGT_MASK], pos + 1); |
| 443 | goto err; |
| 444 | |
Remi Tricot-Le Breton | 6d303d6 | 2021-05-19 12:00:54 +0200 | [diff] [blame] | 445 | alloc_err: |
| 446 | memprintf(err_msg, "out of memory"); |
| 447 | goto err; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 448 | } |