blob: 52f077d8e91d8c87317dee0e8412afcaf1d77fd7 [file] [log] [blame]
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +01001/*
2 * MAP management functions.
3 *
4 * Copyright 2000-2013 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 <limits.h>
14#include <stdio.h>
15
16#include <common/standard.h>
17
18#include <types/global.h>
19#include <types/map.h>
20
21#include <proto/arg.h>
Thierry FOURNIERb0c0a0f2013-12-10 15:05:34 +010022#include <proto/map.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010023#include <proto/pattern.h>
24#include <proto/sample.h>
25
Thierry FOURNIER275db692013-12-06 10:39:36 +010026struct list maps = LIST_HEAD_INIT(maps); /* list of struct map_reference */
27
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010028/* This function return existing map reference or return NULL. */
Thierry FOURNIER2d4771b2013-12-06 10:39:48 +010029struct map_reference *map_get_reference(const char *reference)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010030{
31 struct map_reference *ref;
32
33 /* process the lookup */
34 list_for_each_entry(ref, &maps, list)
35 if (strcmp(ref->reference, reference) == 0)
36 return ref;
37 return NULL;
38}
39
40/* Parse an IPv4 address and store it into the sample.
41 * The output type is IPV4.
42 */
Thierry FOURNIERb0c0a0f2013-12-10 15:05:34 +010043int map_parse_ip(const char *text, struct sample_storage *smp)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010044{
45 if (!buf2ip(text, strlen(text), &smp->data.ipv4))
46 return 0;
47 smp->type = SMP_T_IPV4;
48 return 1;
49}
50
51/* Parse an IPv6 address and store it into the sample.
52 * The output type is IPV6.
53 */
Thierry FOURNIERb0c0a0f2013-12-10 15:05:34 +010054int map_parse_ip6(const char *text, struct sample_storage *smp)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010055{
56 if (!buf2ip6(text, strlen(text), &smp->data.ipv6))
57 return 0;
58 smp->type = SMP_T_IPV6;
59 return 1;
60}
61
62/* Parse a string and store a pointer to it into the sample. The original
63 * string must be left in memory because we return a direct memory reference.
64 * The output type is CSTR.
65 */
Thierry FOURNIERb0c0a0f2013-12-10 15:05:34 +010066int map_parse_str(const char *text, struct sample_storage *smp)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010067{
68 /* The loose of the "const" is balanced by the SMP_T_CSTR type */
69 smp->data.str.str = (char *)text;
70 smp->data.str.len = strlen(text);
71 smp->data.str.size = smp->data.str.len + 1;
72 smp->type = SMP_T_CSTR;
73 return 1;
74}
75
76/* Parse an integer and convert it to a sample. The output type is SINT if the
77 * number is negative, or UINT if it is positive or null. The function returns
78 * zero (error) if the number is too large.
79 */
Thierry FOURNIERb0c0a0f2013-12-10 15:05:34 +010080int map_parse_int(const char *text, struct sample_storage *smp)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010081{
82 long long int value;
83 char *error;
84
85 /* parse interger and convert it. Return the value in 64 format. */
86 value = strtoll(text, &error, 10);
87 if (*error != '\0')
88 return 0;
89
90 /* check sign iand limits */
91 if (value < 0) {
92 if (value < INT_MIN)
93 return 0;
94 smp->type = SMP_T_SINT;
95 smp->data.sint = value;
96 }
97 else {
98 if (value > UINT_MAX)
99 return 0;
100 smp->type = SMP_T_UINT;
101 smp->data.uint = value;
102 }
103
104 return 1;
105}
106
107/* This function creates and initializes a new map_reference entry. This
108 * function only fails in case of a memory allocation issue, in which case
109 * it returns NULL. <reference> here is a unique identifier for the map's
110 * contents, typically the name of the file used to build the map.
111 */
112static struct map_reference *map_create_reference(const char *reference)
113{
114 struct map_reference *ref;
115
116 /* create new entry */
117 ref = calloc(1, sizeof(*ref));
118 if (!ref)
119 return NULL;
120
121 ref->reference = strdup(reference);
122 if (!ref->reference)
123 return NULL;
124
125 LIST_INIT(&ref->entries);
126 LIST_INIT(&ref->maps);
127 LIST_ADDQ(&maps, &ref->list);
128
129 return ref;
130}
131
132/* This function just create new entry */
133static struct map_entry *map_create_entry(int line, char *key, char *value)
134{
135 struct map_entry *ent;
136
137 ent = calloc(1, sizeof(*ent));
138 if (!ent)
139 return NULL;
140
141 ent->line = line;
142
143 ent->key = strdup(key);
144 if (!ent->key) {
145 free(ent);
146 return NULL;
147 }
148
149 ent->value = strdup(value);
150 if (!ent->value) {
151 free(ent->key);
152 free(ent);
153 return NULL;
154 }
155
156 return ent;
157}
158
159/* This crete and initialize map descriptor.
160 * Return NULL if out of memory error
161 */
162static struct map_descriptor *map_create_descriptor(struct map_reference *ref,
163 struct sample_conv *conv)
164{
165 struct map_descriptor *desc;
166
167 desc = calloc(1, sizeof(*desc));
168 if (!desc)
169 return NULL;
170
171 desc->conv = conv;
Thierry FOURNIERa82d2022013-12-05 11:59:55 +0100172 desc->ref = ref;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100173
174 LIST_ADDQ(&ref->maps, &desc->list);
175
176 return desc;
177}
178
179/* This function just add entry into the list of pattern.
180 * It can return false only in memory problem case
181 */
182static int map_add_entry(struct map_reference *map, int line, char *key, char *value)
183{
184 struct map_entry *ent;
185
186 ent = map_create_entry(line, key, value);
187 if (!ent)
188 return 0;
189 LIST_ADDQ(&map->entries, &ent->list);
190 return 1;
191}
192
193/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
194 * be returned there on errors and the caller will have to free it.
195 *
196 * The file contains one key + value per line. Lines which start with '#' are
197 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
198 * then the first "word" (series of non-space/tabs characters), and the value is
199 * what follows this series of space/tab till the end of the line excluding
200 * trailing spaces/tabs.
201 *
202 * Example :
203 *
204 * # this is a comment and is ignored
205 * 62.212.114.60 1wt.eu \n
206 * <-><-----------><---><----><---->
207 * | | | | `--- trailing spaces ignored
208 * | | | `-------- value
209 * | | `--------------- middle spaces ignored
210 * | `------------------------ key
211 * `-------------------------------- leading spaces ignored
212 *
213 * Return non-zero in case of succes, otherwise 0.
214 */
215static int map_read_entries_from_file(const char *filename,
216 struct map_reference *ref,
217 char **err)
218{
219 FILE *file;
220 char *c;
221 int ret = 0;
222 int line = 0;
223 char *key_beg;
224 char *key_end;
225 char *value_beg;
226 char *value_end;
227
228 file = fopen(filename, "r");
229 if (!file) {
230 memprintf(err, "failed to open pattern file <%s>", filename);
231 return 0;
232 }
233
234 /* now parse all patterns. The file may contain only one pattern
235 * followed by one value per line. The start spaces, separator spaces
236 * and and spaces are stripped. Each can contain comment started by '#'
237 */
238 while (fgets(trash.str, trash.size, file) != NULL) {
239 line++;
240 c = trash.str;
241
242 /* ignore lines beginning with a dash */
243 if (*c == '#')
244 continue;
245
246 /* strip leading spaces and tabs */
247 while (*c == ' ' || *c == '\t')
248 c++;
249
250 /* empty lines are ignored too */
Thierry FOURNIER338a0312014-02-26 18:30:13 +0100251 if (*c == '\0' || *c == '\r' || *c == '\n')
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100252 continue;
253
254 /* look for the end of the key */
255 key_beg = c;
256 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
257 c++;
258
259 key_end = c;
260
261 /* strip middle spaces and tabs */
262 while (*c == ' ' || *c == '\t')
263 c++;
264
265 /* look for the end of the value, it is the end of the line */
266 value_beg = c;
267 while (*c && *c != '\n' && *c != '\r')
268 c++;
269 value_end = c;
270
271 /* trim possibly trailing spaces and tabs */
272 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
273 value_end--;
274
275 /* set final \0 and check entries */
276 *key_end = '\0';
277 *value_end = '\0';
278
279 /* insert values */
280 if (!map_add_entry(ref, line, key_beg, value_beg)) {
281 memprintf(err, "out of memory");
282 goto out_close;
283 }
284 }
285
286 /* succes */
287 ret = 1;
288
289 out_close:
290 fclose(file);
291 return ret;
292}
293
294/* This function read the string entries of <ent>, parse it with
295 * the <desc> methods, and strore the result into <desc> dummy ACL.
296 * return 1 in succes case, else return 0 and <err> is filled.
297 *
298 * The acm parser use <pattern> for creating new pattern (list
299 * of values case) or using the same pattern (tree index case).
300 *
301 * <patflags> must be PAT_F_*.
302 */
303static int map_parse_and_index(struct map_descriptor *desc,
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100304 struct map_entry *ent,
305 int patflags,
306 char **err)
307{
308 struct sample_storage *smp;
309
310 /* use new smp for storing value */
311 smp = calloc(1, sizeof(*smp));
312 if (!smp)
313 return 0;
314
315 /* first read and convert value */
316 if (!desc->parse(ent->value, smp)) {
317 memprintf(err, "parse value failed at line %d of file <%s>",
318 ent->line, desc->ref->reference);
319 return 0;
320 }
321
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100322 /* register key */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100323 if (!pattern_register(desc->pat, ent->key, smp, patflags, err))
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100324 return 0;
325
326 return 1;
327}
328
329/* This function load the map file according with data type declared into
330 * the "struct sample_conv".
331 *
332 * This function choose the indexation type (ebtree or list) according with
333 * the type of match needed.
334 */
335static int sample_load_map(struct arg *arg, struct sample_conv *conv, char **err)
336{
337 struct map_reference *ref;
338 struct map_descriptor *desc;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100339 struct map_entry *ent;
Thierry FOURNIER0ffe78c2013-12-05 14:40:25 +0100340 struct pattern_expr *pat = NULL;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100341
342 /* look for existing map reference. The reference is the
343 * file encountered in the first argument. arg[0] with string
344 * type is guaranteed by the parser.
345 */
346 ref = map_get_reference(arg[0].data.str.str);
347
348 /* The reference doesn't exist */
349 if (!ref) {
350
351 /* create new reference entry */
352 ref = map_create_reference(arg[0].data.str.str);
353 if (!ref) {
354 memprintf(err, "out of memory");
355 return 0;
356 }
357
358 /* load the file */
359 if (!map_read_entries_from_file(arg[0].data.str.str, ref, err))
360 return 0;
361 }
362
Thierry FOURNIER0ffe78c2013-12-05 14:40:25 +0100363 /* look for identical existing map. Two maps are identical if
364 * their in_type and out_type are the same. If is not found, pat
365 * is NULL.
366 */
367 else {
368 list_for_each_entry(desc, &ref->maps, list)
369 if (desc->conv->in_type == conv->in_type &&
Thierry FOURNIER736459e2013-12-11 11:20:24 +0100370 desc->conv->out_type == conv->out_type &&
371 desc->conv->private == conv->private)
Thierry FOURNIER0ffe78c2013-12-05 14:40:25 +0100372 break;
373 if (&desc->list != &ref->maps)
374 pat = desc->pat;
375 }
376
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100377 /* create new map descriptor */
378 desc = map_create_descriptor(ref, conv);
379 if (!desc) {
380 memprintf(err, "out of memory");
381 return 0;
382 }
383
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100384 /* check the output parse method */
385 switch (desc->conv->out_type) {
386 case SMP_T_STR: desc->parse = map_parse_str; break;
387 case SMP_T_UINT: desc->parse = map_parse_int; break;
388 case SMP_T_IPV4: desc->parse = map_parse_ip; break;
389 case SMP_T_IPV6: desc->parse = map_parse_ip6; break;
390 default:
391 memprintf(err, "map: internal haproxy error: no default parse case for the input type <%d>.",
392 conv->out_type);
393 return 0;
394 }
395
Thierry FOURNIER0ffe78c2013-12-05 14:40:25 +0100396 /* If identical pattern is not found, initialize his own pattern */
397 if (!pat) {
398
399 desc->pat = calloc(1, sizeof(*desc->pat));
400 if (!desc->pat) {
401 memprintf(err, "out of memory");
402 return 0;
403 }
404
405 pattern_init_expr(desc->pat);
406
407 /* This is original pattern, must free */
408 desc->do_free = 1;
409
410 /* set the match method */
411 desc->pat->match = pat_match_fcts[conv->private];
Thierry FOURNIER736459e2013-12-11 11:20:24 +0100412 desc->pat->parse = pat_parse_fcts[conv->private];
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100413 desc->pat->index = pat_index_fcts[conv->private];
Thierry FOURNIER0ffe78c2013-12-05 14:40:25 +0100414
415 /* parse each line of the file */
Thierry FOURNIER0ffe78c2013-12-05 14:40:25 +0100416 list_for_each_entry(ent, &ref->entries, list)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100417 if (!map_parse_and_index(desc, ent, 0, err))
Thierry FOURNIER0ffe78c2013-12-05 14:40:25 +0100418 return 0;
419 }
420
421 /* identical pattern found. Use reference to this pattern, and mark
422 * the map_descriptor pattern as non freeable
423 */
424 else {
425 desc->pat = pat;
426 desc->do_free = 0;
427 }
428
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100429 /* The second argument is the default value */
430 if (arg[1].type == ARGT_STR) {
431 desc->default_value = strdup(arg[1].data.str.str);
432 if (!desc->default_value) {
433 memprintf(err, "out of memory");
434 return 0;
435 }
436 desc->def = calloc(1, sizeof(*desc->def));
437 if (!desc->def) {
438 memprintf(err, "out of memory");
439 return 0;
440 }
441 if (!desc->parse(desc->default_value, desc->def)) {
442 memprintf(err, "Cannot parse default value");
443 return 0;
444 }
445 }
446 else
447 desc->def = NULL;
448
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100449 /* replace the first argument by this definition */
450 arg[0].type = ARGT_MAP;
451 arg[0].data.map = desc;
452
453 return 1;
454}
455
456static int sample_conv_map(const struct arg *arg_p, struct sample *smp)
457{
458 struct map_descriptor *desc;
459 struct sample_storage *sample;
460 enum pat_match_res ret;
461
462 /* get config */
463 desc = arg_p[0].data.map;
464
465 /* Execute the match function. */
Thierry FOURNIER76090642013-12-10 15:03:38 +0100466 ret = pattern_exec_match(desc->pat, smp, &sample, NULL, NULL);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100467 if (ret != PAT_MATCH) {
468 if (!desc->def)
469 return 0;
470 sample = desc->def;
471 }
472
473 /* copy new data */
474 smp->type = sample->type;
475 memcpy(&smp->data, &sample->data, sizeof(smp->data));
476 return 1;
477}
478
479/* Note: must not be declared <const> as its list will be overwritten
480 *
481 * For the map_*_int keywords, the output is declared as SMP_T_UINT, but the converter function
482 * can provide SMP_T_UINT, SMP_T_SINT or SMP_T_BOOL depending on how the patterns found in the
483 * file can be parsed.
484 *
485 * For the map_*_ip keyword, the output is declared as SMP_T_IPV4, but the converter function
486 * can provide SMP_T_IPV4 or SMP_T_IPV6 depending on the patterns found in the file.
487 *
488 * The map_* keywords only emit strings.
489 *
490 * The output type is only used during the configuration parsing. It is used for detecting
491 * compatibility problems.
492 *
493 * The arguments are: <file>[,<default value>]
494 */
495static struct sample_conv_kw_list sample_conv_kws = {ILH, {
496 { "map", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_STR },
497 { "map_str", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_STR },
498 { "map_beg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_BEG },
499 { "map_sub", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_SUB },
500 { "map_dir", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_DIR },
501 { "map_dom", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_DOM },
502 { "map_end", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_END },
503 { "map_reg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_REG },
504 { "map_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_UINT, SMP_T_STR, PAT_MATCH_INT },
505 { "map_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_ADDR, SMP_T_STR, PAT_MATCH_IP },
506
507 { "map_str_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, PAT_MATCH_STR },
508 { "map_beg_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, PAT_MATCH_BEG },
509 { "map_sub_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, PAT_MATCH_SUB },
510 { "map_dir_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, PAT_MATCH_DIR },
511 { "map_dom_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, PAT_MATCH_DOM },
512 { "map_end_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, PAT_MATCH_END },
513 { "map_reg_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, PAT_MATCH_REG },
514 { "map_int_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_UINT, SMP_T_UINT, PAT_MATCH_INT },
515 { "map_ip_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_ADDR, SMP_T_UINT, PAT_MATCH_IP },
516
517 { "map_str_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, PAT_MATCH_STR },
518 { "map_beg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, PAT_MATCH_BEG },
519 { "map_sub_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, PAT_MATCH_SUB },
520 { "map_dir_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, PAT_MATCH_DIR },
521 { "map_dom_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, PAT_MATCH_DOM },
522 { "map_end_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, PAT_MATCH_END },
523 { "map_reg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, PAT_MATCH_REG },
524 { "map_int_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_UINT, SMP_T_IPV4, PAT_MATCH_INT },
525 { "map_ip_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_ADDR, SMP_T_IPV4, PAT_MATCH_IP },
526
527 { /* END */ },
528}};
529
530__attribute__((constructor))
531static void __map_init(void)
532{
533 /* register format conversion keywords */
534 sample_register_convs(&sample_conv_kws);
535}