blob: 2ed9f3a9f6873e39bd2f9fda2daa777639de5589 [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 */
323 if (!pattern_register(&desc->pat, ent->key, smp, pattern, patflags, err))
324 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;
341
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
363 /* create new map descriptor */
364 desc = map_create_descriptor(ref, conv);
365 if (!desc) {
366 memprintf(err, "out of memory");
367 return 0;
368 }
369
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100370 pattern_init_expr(&desc->pat);
371
372 /* set the match method */
373 desc->pat.match = pat_match_fcts[conv->private];
374
375 /* set the input parse method */
376 switch (conv->in_type) {
377 case SMP_T_STR: desc->pat.parse = pat_parse_fcts[PAT_MATCH_STR]; break;
378 case SMP_T_UINT: desc->pat.parse = pat_parse_fcts[PAT_MATCH_INT]; break;
379 case SMP_T_ADDR: desc->pat.parse = pat_parse_fcts[PAT_MATCH_IP]; break;
380 default:
381 memprintf(err, "map: internal haproxy error: no default parse case for the input type <%d>.",
382 conv->in_type);
383 return 0;
384 }
385
386 /* check the output parse method */
387 switch (desc->conv->out_type) {
388 case SMP_T_STR: desc->parse = map_parse_str; break;
389 case SMP_T_UINT: desc->parse = map_parse_int; break;
390 case SMP_T_IPV4: desc->parse = map_parse_ip; break;
391 case SMP_T_IPV6: desc->parse = map_parse_ip6; break;
392 default:
393 memprintf(err, "map: internal haproxy error: no default parse case for the input type <%d>.",
394 conv->out_type);
395 return 0;
396 }
397
398 /* The second argument is the default value */
399 if (arg[1].type == ARGT_STR) {
400 desc->default_value = strdup(arg[1].data.str.str);
401 if (!desc->default_value) {
402 memprintf(err, "out of memory");
403 return 0;
404 }
405 desc->def = calloc(1, sizeof(*desc->def));
406 if (!desc->def) {
407 memprintf(err, "out of memory");
408 return 0;
409 }
410 if (!desc->parse(desc->default_value, desc->def)) {
411 memprintf(err, "Cannot parse default value");
412 return 0;
413 }
414 }
415 else
416 desc->def = NULL;
417
418 /* parse each line of the file */
419 pattern = NULL;
420 list_for_each_entry(ent, &ref->entries, list)
421 if (!map_parse_and_index(desc, &pattern, ent, 0, err))
422 return 0;
423
424 /* replace the first argument by this definition */
425 arg[0].type = ARGT_MAP;
426 arg[0].data.map = desc;
427
428 return 1;
429}
430
431static int sample_conv_map(const struct arg *arg_p, struct sample *smp)
432{
433 struct map_descriptor *desc;
434 struct sample_storage *sample;
435 enum pat_match_res ret;
436
437 /* get config */
438 desc = arg_p[0].data.map;
439
440 /* Execute the match function. */
441 ret = pattern_exec_match(&desc->pat, smp, &sample);
442 if (ret != PAT_MATCH) {
443 if (!desc->def)
444 return 0;
445 sample = desc->def;
446 }
447
448 /* copy new data */
449 smp->type = sample->type;
450 memcpy(&smp->data, &sample->data, sizeof(smp->data));
451 return 1;
452}
453
454/* Note: must not be declared <const> as its list will be overwritten
455 *
456 * For the map_*_int keywords, the output is declared as SMP_T_UINT, but the converter function
457 * can provide SMP_T_UINT, SMP_T_SINT or SMP_T_BOOL depending on how the patterns found in the
458 * file can be parsed.
459 *
460 * For the map_*_ip keyword, the output is declared as SMP_T_IPV4, but the converter function
461 * can provide SMP_T_IPV4 or SMP_T_IPV6 depending on the patterns found in the file.
462 *
463 * The map_* keywords only emit strings.
464 *
465 * The output type is only used during the configuration parsing. It is used for detecting
466 * compatibility problems.
467 *
468 * The arguments are: <file>[,<default value>]
469 */
470static struct sample_conv_kw_list sample_conv_kws = {ILH, {
471 { "map", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_STR },
472 { "map_str", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_STR },
473 { "map_beg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_BEG },
474 { "map_sub", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_SUB },
475 { "map_dir", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_DIR },
476 { "map_dom", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_DOM },
477 { "map_end", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_END },
478 { "map_reg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_REG },
479 { "map_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_UINT, SMP_T_STR, PAT_MATCH_INT },
480 { "map_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_ADDR, SMP_T_STR, PAT_MATCH_IP },
481
482 { "map_str_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, PAT_MATCH_STR },
483 { "map_beg_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, PAT_MATCH_BEG },
484 { "map_sub_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, PAT_MATCH_SUB },
485 { "map_dir_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, PAT_MATCH_DIR },
486 { "map_dom_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, PAT_MATCH_DOM },
487 { "map_end_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, PAT_MATCH_END },
488 { "map_reg_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, PAT_MATCH_REG },
489 { "map_int_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_UINT, SMP_T_UINT, PAT_MATCH_INT },
490 { "map_ip_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_ADDR, SMP_T_UINT, PAT_MATCH_IP },
491
492 { "map_str_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, PAT_MATCH_STR },
493 { "map_beg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, PAT_MATCH_BEG },
494 { "map_sub_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, PAT_MATCH_SUB },
495 { "map_dir_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, PAT_MATCH_DIR },
496 { "map_dom_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, PAT_MATCH_DOM },
497 { "map_end_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, PAT_MATCH_END },
498 { "map_reg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, PAT_MATCH_REG },
499 { "map_int_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_UINT, SMP_T_IPV4, PAT_MATCH_INT },
500 { "map_ip_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_ADDR, SMP_T_IPV4, PAT_MATCH_IP },
501
502 { /* END */ },
503}};
504
505__attribute__((constructor))
506static void __map_init(void)
507{
508 /* register format conversion keywords */
509 sample_register_convs(&sample_conv_kws);
510}