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