blob: 8e71dcca25da673cdcb5d8a13918c4175ec9c4aa [file] [log] [blame]
Willy Tarreau2ac57182012-04-19 15:24:50 +02001/*
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 FOURNIER49f45af2014-12-08 19:50:43 +010020const char *arg_type_names[ARGT_NBTYPES] = {
Willy Tarreau2ac57182012-04-19 15:24:50 +020021 [ARGT_STOP] = "end of arguments",
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020022 [ARGT_SINT] = "integer",
Willy Tarreau2ac57182012-04-19 15:24:50 +020023 [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 Tarreau53c250e2015-01-19 18:58:20 +010035 [ARGT_MAP] = "map",
Willy Tarreau46947782015-01-19 19:00:58 +010036 [ARGT_REG] = "regex",
Willy Tarreauf7ead612015-09-21 20:57:12 +020037 [ARGT_VAR] = "variable",
Willy Tarreau2ac57182012-04-19 15:24:50 +020038 /* Unassigned types must never happen. Better crash during parsing if they do. */
39};
40
Willy Tarreau2e845be2012-10-19 19:49:09 +020041/* This dummy arg list may be used by default when no arg is found, it helps
42 * parsers by removing pointer checks.
43 */
Willy Tarreau3d241e72015-01-19 18:44:07 +010044struct arg empty_arg_list[ARGM_NBARGS] = { };
Willy Tarreau2e845be2012-10-19 19:49:09 +020045
Willy Tarreaua4312fa2013-04-02 16:34:32 +020046/* This function clones a struct arg_list template into a new one which is
47 * returned.
48 */
49struct 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 */
70struct 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);
Willy Tarreaua9e2e4b2017-04-12 22:28:52 +020075 if (new) {
76 new->arg = arg;
77 new->arg_pos = pos;
78 LIST_ADDQ(&orig->list, &new->list);
79 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +020080 return new;
81}
82
Willy Tarreau2ac57182012-04-19 15:24:50 +020083/* This function builds an argument list from a config line. It returns the
84 * number of arguments found, or <0 in case of any error. Everything needed
85 * it automatically allocated. A pointer to an error message might be returned
86 * in err_msg if not NULL, in which case it would be allocated and the caller
87 * will have to check it and free it. The output arg list is returned in argp
88 * which must be valid. The returned array is always terminated by an arg of
89 * type ARGT_STOP (0), unless the mask indicates that no argument is supported.
Willy Tarreaua4312fa2013-04-02 16:34:32 +020090 * Unresolved arguments are appended to arg list <al>, which also serves as a
91 * template to create new entries. The mask is composed of a number of
Willy Tarreau3d241e72015-01-19 18:44:07 +010092 * mandatory arguments in its lower ARGM_BITS bits, and a concatenation of each
93 * argument type in each subsequent ARGT_BITS-bit sblock. If <err_msg> is not
94 * NULL, it must point to a freeable or NULL pointer.
Willy Tarreau2ac57182012-04-19 15:24:50 +020095 */
David Carlier15073a32016-03-15 19:00:35 +000096int make_arg_list(const char *in, int len, uint64_t mask, struct arg **argp,
Willy Tarreaua4312fa2013-04-02 16:34:32 +020097 char **err_msg, const char **err_ptr, int *err_arg,
98 struct arg_list *al)
Willy Tarreau2ac57182012-04-19 15:24:50 +020099{
100 int nbarg;
101 int pos;
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200102 struct arg *arg;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200103 const char *beg;
104 char *word = NULL;
105 const char *ptr_err = NULL;
106 int min_arg;
Willy Tarreau0622f022017-04-12 22:32:04 +0200107 struct arg_list *new_al = al;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200108
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200109 *argp = NULL;
110
Willy Tarreau3d241e72015-01-19 18:44:07 +0100111 min_arg = mask & ARGM_MASK;
112 mask >>= ARGM_BITS;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200113
114 pos = 0;
Willy Tarreau3d241e72015-01-19 18:44:07 +0100115 /* find between 0 and NBARGS the max number of args supported by the mask */
116 for (nbarg = 0; nbarg < ARGM_NBARGS && ((mask >> (nbarg * ARGT_BITS)) & ARGT_MASK); nbarg++);
Willy Tarreau2ac57182012-04-19 15:24:50 +0200117
118 if (!nbarg)
119 goto end_parse;
120
121 /* Note: an empty input string contains an empty argument if this argument
122 * is marked mandatory. Otherwise we can ignore it.
123 */
124 if (!len && !min_arg)
125 goto end_parse;
126
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200127 arg = *argp = calloc(nbarg + 1, sizeof(*arg));
Willy Tarreau2ac57182012-04-19 15:24:50 +0200128
129 /* Note: empty arguments after a comma always exist. */
130 while (pos < nbarg) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200131 unsigned int uint;
132
Willy Tarreau2ac57182012-04-19 15:24:50 +0200133 beg = in;
134 while (len && *in != ',') {
135 in++;
136 len--;
137 }
138
139 /* we have a new argument between <beg> and <in> (not included).
140 * For ease of handling, we copy it into a zero-terminated word.
141 * By default, the output argument will be the same type of the
142 * expected one.
143 */
144 free(word);
145 word = my_strndup(beg, in - beg);
146
Willy Tarreau3d241e72015-01-19 18:44:07 +0100147 arg->type = (mask >> (pos * ARGT_BITS)) & ARGT_MASK;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200148
149 switch (arg->type) {
150 case ARGT_SINT:
151 if (in == beg) // empty number
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200152 goto empty_err;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200153 arg->data.sint = read_int64(&beg, in);
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200154 if (beg < in)
155 goto parse_err;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200156 arg->type = ARGT_SINT;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200157 break;
158
159 case ARGT_FE:
160 case ARGT_BE:
161 case ARGT_TAB:
162 case ARGT_SRV:
163 case ARGT_USR:
Willy Tarreau46947782015-01-19 19:00:58 +0100164 case ARGT_REG:
Willy Tarreau496aa012012-06-01 10:38:29 +0200165 /* These argument types need to be stored as strings during
166 * parsing then resolved later.
167 */
168 arg->unresolved = 1;
Willy Tarreau0622f022017-04-12 22:32:04 +0200169 new_al = arg_list_add(al, arg, pos);
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200170
Willy Tarreau496aa012012-06-01 10:38:29 +0200171 /* fall through */
Willy Tarreau2ac57182012-04-19 15:24:50 +0200172 case ARGT_STR:
173 /* all types that must be resolved are stored as strings
174 * during the parsing. The caller must at one point resolve
175 * them and free the string.
176 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200177 arg->data.str.area = word;
178 arg->data.str.data = in - beg;
179 arg->data.str.size = arg->data.str.data + 1;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200180 word = NULL;
181 break;
182
183 case ARGT_IPV4:
184 if (in == beg) // empty address
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200185 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200186
187 if (inet_pton(AF_INET, word, &arg->data.ipv4) <= 0)
188 goto parse_err;
189 break;
190
191 case ARGT_MSK4:
192 if (in == beg) // empty mask
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200193 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200194
195 if (!str2mask(word, &arg->data.ipv4))
196 goto parse_err;
197
198 arg->type = ARGT_IPV4;
199 break;
200
201 case ARGT_IPV6:
202 if (in == beg) // empty address
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200203 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200204
205 if (inet_pton(AF_INET6, word, &arg->data.ipv6) <= 0)
206 goto parse_err;
207 break;
208
Tim Duesterhusb814da62018-01-25 16:24:50 +0100209 case ARGT_MSK6:
210 if (in == beg) // empty mask
211 goto empty_err;
212
213 if (!str2mask6(word, &arg->data.ipv6))
214 goto parse_err;
215
216 arg->type = ARGT_IPV6;
217 break;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200218
219 case ARGT_TIME:
220 if (in == beg) // empty time
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200221 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200222
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200223 ptr_err = parse_time_err(word, &uint, TIME_UNIT_MS);
Willy Tarreau2ac57182012-04-19 15:24:50 +0200224 if (ptr_err)
225 goto parse_err;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200226 arg->data.sint = uint;
227 arg->type = ARGT_SINT;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200228 break;
229
230 case ARGT_SIZE:
231 if (in == beg) // empty size
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200232 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200233
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200234 ptr_err = parse_size_err(word, &uint);
Willy Tarreau2ac57182012-04-19 15:24:50 +0200235 if (ptr_err)
236 goto parse_err;
237
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200238 arg->data.sint = uint;
239 arg->type = ARGT_SINT;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200240 break;
241
242 /* FIXME: other types need to be implemented here */
243 default:
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200244 goto not_impl;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200245 }
246
247 pos++;
248 arg++;
249
250 /* don't go back to parsing if we reached end */
251 if (!len || pos >= nbarg)
252 break;
253
254 /* skip comma */
255 in++; len--;
256 }
257
258 end_parse:
259 free(word); word = NULL;
260
261 if (pos < min_arg) {
262 /* not enough arguments */
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200263 memprintf(err_msg,
Willy Tarreaub111d422013-12-13 00:38:47 +0100264 "missing arguments (got %d/%d), type '%s' expected",
Willy Tarreau3d241e72015-01-19 18:44:07 +0100265 pos, min_arg, arg_type_names[(mask >> (pos * ARGT_BITS)) & ARGT_MASK]);
Willy Tarreau2ac57182012-04-19 15:24:50 +0200266 goto err;
267 }
268
269 if (len) {
270 /* too many arguments, starting at <in> */
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200271 /* the caller is responsible for freeing this message */
272 word = my_strndup(in, len);
Willy Tarreaub111d422013-12-13 00:38:47 +0100273 if (nbarg)
274 memprintf(err_msg, "end of arguments expected at position %d, but got '%s'",
275 pos + 1, word);
276 else
277 memprintf(err_msg, "no argument supported, but got '%s'", word);
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200278 free(word); word = NULL;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200279 goto err;
280 }
281
282 /* note that pos might be < nbarg and this is not an error, it's up to the
283 * caller to decide what to do with optional args.
284 */
Willy Tarreau2ac57182012-04-19 15:24:50 +0200285 if (err_arg)
286 *err_arg = pos;
287 if (err_ptr)
288 *err_ptr = in;
289 return pos;
290
Willy Tarreau2ac57182012-04-19 15:24:50 +0200291 err:
292 free(word);
Willy Tarreau0622f022017-04-12 22:32:04 +0200293 if (new_al == al) {
294 /* only free the arg area if we have not queued unresolved args
295 * still pointing to it.
296 */
297 free(*argp);
298 }
Willy Tarreau681e49d2013-12-06 15:30:05 +0100299 *argp = NULL;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200300 if (err_arg)
301 *err_arg = pos;
302 if (err_ptr)
303 *err_ptr = in;
304 return -1;
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200305
306 empty_err:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200307 memprintf(err_msg, "expected type '%s' at position %d, but got nothing",
Willy Tarreau3d241e72015-01-19 18:44:07 +0100308 arg_type_names[(mask >> (pos * ARGT_BITS)) & ARGT_MASK], pos + 1);
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200309 goto err;
310
311 parse_err:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200312 memprintf(err_msg, "failed to parse '%s' as type '%s' at position %d",
Willy Tarreau3d241e72015-01-19 18:44:07 +0100313 word, arg_type_names[(mask >> (pos * ARGT_BITS)) & ARGT_MASK], pos + 1);
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200314 goto err;
315
316 not_impl:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200317 memprintf(err_msg, "parsing for type '%s' was not implemented, please report this bug",
Willy Tarreau3d241e72015-01-19 18:44:07 +0100318 arg_type_names[(mask >> (pos * ARGT_BITS)) & ARGT_MASK]);
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200319 goto err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200320}