blob: 81c61eb95f7fc06beb0c1be1f3646d41508f0629 [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;
171
172 LIST_ADDQ(&ref->maps, &desc->list);
173
174 return desc;
175}
176
177/* This function just add entry into the list of pattern.
178 * It can return false only in memory problem case
179 */
180static int map_add_entry(struct map_reference *map, int line, char *key, char *value)
181{
182 struct map_entry *ent;
183
184 ent = map_create_entry(line, key, value);
185 if (!ent)
186 return 0;
187 LIST_ADDQ(&map->entries, &ent->list);
188 return 1;
189}
190
191/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
192 * be returned there on errors and the caller will have to free it.
193 *
194 * The file contains one key + value per line. Lines which start with '#' are
195 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
196 * then the first "word" (series of non-space/tabs characters), and the value is
197 * what follows this series of space/tab till the end of the line excluding
198 * trailing spaces/tabs.
199 *
200 * Example :
201 *
202 * # this is a comment and is ignored
203 * 62.212.114.60 1wt.eu \n
204 * <-><-----------><---><----><---->
205 * | | | | `--- trailing spaces ignored
206 * | | | `-------- value
207 * | | `--------------- middle spaces ignored
208 * | `------------------------ key
209 * `-------------------------------- leading spaces ignored
210 *
211 * Return non-zero in case of succes, otherwise 0.
212 */
213static int map_read_entries_from_file(const char *filename,
214 struct map_reference *ref,
215 char **err)
216{
217 FILE *file;
218 char *c;
219 int ret = 0;
220 int line = 0;
221 char *key_beg;
222 char *key_end;
223 char *value_beg;
224 char *value_end;
225
226 file = fopen(filename, "r");
227 if (!file) {
228 memprintf(err, "failed to open pattern file <%s>", filename);
229 return 0;
230 }
231
232 /* now parse all patterns. The file may contain only one pattern
233 * followed by one value per line. The start spaces, separator spaces
234 * and and spaces are stripped. Each can contain comment started by '#'
235 */
236 while (fgets(trash.str, trash.size, file) != NULL) {
237 line++;
238 c = trash.str;
239
240 /* ignore lines beginning with a dash */
241 if (*c == '#')
242 continue;
243
244 /* strip leading spaces and tabs */
245 while (*c == ' ' || *c == '\t')
246 c++;
247
248 /* empty lines are ignored too */
249 if (*c == '\0')
250 continue;
251
252 /* look for the end of the key */
253 key_beg = c;
254 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
255 c++;
256
257 key_end = c;
258
259 /* strip middle spaces and tabs */
260 while (*c == ' ' || *c == '\t')
261 c++;
262
263 /* look for the end of the value, it is the end of the line */
264 value_beg = c;
265 while (*c && *c != '\n' && *c != '\r')
266 c++;
267 value_end = c;
268
269 /* trim possibly trailing spaces and tabs */
270 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
271 value_end--;
272
273 /* set final \0 and check entries */
274 *key_end = '\0';
275 *value_end = '\0';
276
277 /* insert values */
278 if (!map_add_entry(ref, line, key_beg, value_beg)) {
279 memprintf(err, "out of memory");
280 goto out_close;
281 }
282 }
283
284 /* succes */
285 ret = 1;
286
287 out_close:
288 fclose(file);
289 return ret;
290}
291
292/* This function read the string entries of <ent>, parse it with
293 * the <desc> methods, and strore the result into <desc> dummy ACL.
294 * return 1 in succes case, else return 0 and <err> is filled.
295 *
296 * The acm parser use <pattern> for creating new pattern (list
297 * of values case) or using the same pattern (tree index case).
298 *
299 * <patflags> must be PAT_F_*.
300 */
301static int map_parse_and_index(struct map_descriptor *desc,
302 struct pattern **pattern,
303 struct map_entry *ent,
304 int patflags,
305 char **err)
306{
307 struct sample_storage *smp;
308
309 /* use new smp for storing value */
310 smp = calloc(1, sizeof(*smp));
311 if (!smp)
312 return 0;
313
314 /* first read and convert value */
315 if (!desc->parse(ent->value, smp)) {
316 memprintf(err, "parse value failed at line %d of file <%s>",
317 ent->line, desc->ref->reference);
318 return 0;
319 }
320
321 /* read and convert key */
322 if (!pattern_register(&desc->pat, ent->key, smp, pattern, patflags, err))
323 return 0;
324
325 return 1;
326}
327
328/* This function load the map file according with data type declared into
329 * the "struct sample_conv".
330 *
331 * This function choose the indexation type (ebtree or list) according with
332 * the type of match needed.
333 */
334static int sample_load_map(struct arg *arg, struct sample_conv *conv, char **err)
335{
336 struct map_reference *ref;
337 struct map_descriptor *desc;
338 struct pattern *pattern;
339 struct map_entry *ent;
340
341 /* look for existing map reference. The reference is the
342 * file encountered in the first argument. arg[0] with string
343 * type is guaranteed by the parser.
344 */
345 ref = map_get_reference(arg[0].data.str.str);
346
347 /* The reference doesn't exist */
348 if (!ref) {
349
350 /* create new reference entry */
351 ref = map_create_reference(arg[0].data.str.str);
352 if (!ref) {
353 memprintf(err, "out of memory");
354 return 0;
355 }
356
357 /* load the file */
358 if (!map_read_entries_from_file(arg[0].data.str.str, ref, err))
359 return 0;
360 }
361
362 /* create new map descriptor */
363 desc = map_create_descriptor(ref, conv);
364 if (!desc) {
365 memprintf(err, "out of memory");
366 return 0;
367 }
368
369 desc->ref = ref;
370 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}