blob: 63bae2b8cada375a65e0d2534a3fa63d02663c07 [file] [log] [blame]
Emeric Brun107ca302010-01-04 16:16:05 +01001/*
2 * Patterns management functions.
3 *
4 * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
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 <string.h>
14#include <arpa/inet.h>
15#include <types/stick_table.h>
16
17#include <proto/pattern.h>
18#include <common/standard.h>
19
20/* static structure used on pattern_process if <p> is NULL*/
21static struct pattern spattern;
22
23/* trash chunk used for pattern conversions */
24static struct chunk trash_chunk;
25
26/* trash buffers used or pattern conversions */
27static char pattern_trash_buf1[BUFSIZE];
28static char pattern_trash_buf2[BUFSIZE];
29
30/* pattern_trash_buf point on used buffer*/
31static char *pattern_trash_buf = pattern_trash_buf1;
32
33/* static structure used to returns builded table key from a pattern*/
34static struct stktable_key stable_key;
35
36/* list head of all known pattern fetch keywords */
37static struct pattern_fetch_kw_list pattern_fetches = {
38 .list = LIST_HEAD_INIT(pattern_fetches.list)
39};
40
41/* list head of all known pattern format conversion keywords */
42static struct pattern_conv_kw_list pattern_convs = {
43 .list = LIST_HEAD_INIT(pattern_convs.list)
44};
45
46/*
47 * Registers the pattern fetch keyword list <kwl> as a list of valid keywords for next
48 * parsing sessions.
49 */
50void pattern_register_fetches(struct pattern_fetch_kw_list *pfkl)
51{
52 LIST_ADDQ(&pattern_fetches.list, &pfkl->list);
53}
54
55/*
56 * Registers the pattern format coverstion keyword list <pckl> as a list of valid keywords for next
57 * parsing sessions.
58 */
59void pattern_register_convs(struct pattern_conv_kw_list *pckl)
60{
61 LIST_ADDQ(&pattern_convs.list, &pckl->list);
62}
63
64/*
65 * Returns the pointer on pattern fetch keyword structure identified by
66 * string of <len> in buffer <kw>.
67 *
68 */
69struct pattern_fetch *find_pattern_fetch(const char *kw, int len)
70{
71 int index;
72 struct pattern_fetch_kw_list *kwl;
73
74 list_for_each_entry(kwl, &pattern_fetches.list, list) {
75 for (index = 0; kwl->kw[index].kw != NULL; index++) {
76 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
77 kwl->kw[index].kw[len] == '\0')
78 return &kwl->kw[index];
79 }
80 }
81 return NULL;
82}
83
84/*
85 * Returns the pointer on pattern format conversion keyword structure identified by
86 * string of <len> in buffer <kw>.
87 *
88 */
89struct pattern_conv *find_pattern_conv(const char *kw, int len)
90{
91 int index;
92 struct pattern_conv_kw_list *kwl;
93
94 list_for_each_entry(kwl, &pattern_convs.list, list) {
95 for (index = 0; kwl->kw[index].kw != NULL; index++) {
96 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
97 kwl->kw[index].kw[len] == '\0')
98 return &kwl->kw[index];
99 }
100 }
101 return NULL;
102}
103
104
105/*
106* Returns a static trash struct chunk to use in pattern casts or format conversions
107* Swiths the 2 available trash buffers to protect data during convert
108*/
109static struct chunk *get_trash_chunk(void)
110{
111 if (pattern_trash_buf == pattern_trash_buf1)
112 pattern_trash_buf = pattern_trash_buf2;
113 else
114 pattern_trash_buf = pattern_trash_buf1;
115
116 trash_chunk.str = pattern_trash_buf;
117 trash_chunk.len = 0;
118 trash_chunk.size = BUFSIZE;
119
120 return &trash_chunk;
121}
122
123/*
124* Used to set pattern data from a struct chunk, could be the trash struct chunk
125*/
126static void pattern_data_setstring(union pattern_data *data, struct chunk *c)
127{
128 data->str.str = c->str;
129 data->str.len = c->len;
130 data->str.size = c->size;
131}
132
133/******************************************************************/
134/* Pattern casts functions */
135/******************************************************************/
136
137static int c_ip2int(union pattern_data *data)
138{
139 data->integer = ntohl(data->ip.s_addr);
140 return 1;
141}
142
143static int c_ip2str(union pattern_data *data)
144{
145 struct chunk *trash = get_trash_chunk();
146
147 if (!inet_ntop(AF_INET, (void *)&data->ip, trash->str, trash->size))
148 return 0;
149
150 trash->len = strlen(trash->str);
151 pattern_data_setstring(data, trash);
152
153 return 1;
154}
155
156static int c_int2ip(union pattern_data *data)
157{
158 data->ip.s_addr = htonl(data->integer);
159 return 1;
160}
161
162/* Convert a fixed-length string to an IP address. Returns 0 in case of error,
163 * or the number of chars read in case of success.
164 */
165static int buf2ip(const char *buf, size_t len, struct in_addr *dst)
166{
167 const char *addr;
168 int saw_digit, octets, ch;
169 u_char tmp[4], *tp;
170 const char *cp = buf;
171
172 saw_digit = 0;
173 octets = 0;
174 *(tp = tmp) = 0;
175
176 for (addr = buf; addr - buf < len; addr++) {
177 unsigned char digit = (ch = *addr) - '0';
178
179 if (digit > 9 && ch != '.')
180 break;
181
182 if (digit <= 9) {
183 u_int new = *tp * 10 + digit;
184
185 if (new > 255)
186 return 0;
187
188 *tp = new;
189
190 if (!saw_digit) {
191 if (++octets > 4)
192 return 0;
193 saw_digit = 1;
194 }
195 } else if (ch == '.' && saw_digit) {
196 if (octets == 4)
197 return 0;
198
199 *++tp = 0;
200 saw_digit = 0;
201 } else
202 return 0;
203 }
204
205 if (octets < 4)
206 return 0;
207
208 memcpy(&dst->s_addr, tmp, 4);
209 return addr - cp;
210}
211
212static int c_str2ip(union pattern_data *data)
213{
214 if (!buf2ip(data->str.str, data->str.len, &data->ip))
215 return 0;
216 return 1;
217}
218
219static int c_int2str(union pattern_data *data)
220{
221 struct chunk *trash = get_trash_chunk();
222 char *pos;
223
224 pos = ultoa_r(data->integer, trash->str, trash->size);
225
226 if (!pos)
227 return 0;
228
229 trash->str = pos;
230 trash->len = strlen(pos);
231
232 pattern_data_setstring(data, trash);
233
234 return 1;
235}
236
237static int c_donothing(union pattern_data *data)
238{
239 return 1;
240}
241
242static int c_str2int(union pattern_data *data)
243{
244 int i;
245 uint32_t ret = 0;
246
247 for (i = 0; i < data->str.len; i++) {
248 uint32_t val = data->str.str[i] - '0';
249
250 if (val > 9)
251 break;
252
253 ret = ret * 10 + val;
254 }
255
256 data->integer = ret;
257 return 1;
258}
259
260/*****************************************************************/
261/* Pattern casts matrix: */
262/* pattern_casts[from type][to type] */
263/* NULL pointer used for impossible pattern casts */
264/*****************************************************************/
265
266typedef int (*pattern_cast)(union pattern_data *data);
267static pattern_cast pattern_casts[PATTERN_TYPES][PATTERN_TYPES] = { { c_donothing, c_ip2int, c_ip2str },
268 { c_int2ip, c_donothing, c_int2str },
269 { c_str2ip, c_str2int, c_donothing } };
270
271
272/*****************************************************************/
273/* typed pattern to typed table key functions */
274/*****************************************************************/
275
276static void *k_int2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
277{
278 return (void *)&pdata->integer;
279}
280
281static void *k_ip2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
282{
283 return (void *)&pdata->ip.s_addr;
284}
285
286static void *k_ip2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
287{
288 kdata->integer = ntohl(pdata->ip.s_addr);
289 return (void *)&kdata->integer;
290}
291
292static void *k_int2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
293{
294 kdata->ip.s_addr = htonl(pdata->integer);
295 return (void *)&kdata->ip.s_addr;
296}
297
298static void *k_str2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
299{
300 *len = pdata->str.len;
301 return (void *)pdata->str.str;
302}
303
304static void *k_ip2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
305{
306 if (!inet_ntop(AF_INET, &pdata->ip, kdata->buf, sizeof(kdata->buf)))
307 return NULL;
308
309 *len = strlen((const char *)kdata->buf);
310 return (void *)kdata->buf;
311}
312
313static void *k_int2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
314{
315 void *key;
316
317 key = (void *)ultoa_r(pdata->integer, kdata->buf, sizeof(kdata->buf));
318 if (!key)
319 return NULL;
320
321 *len = strlen((const char *)key);
322 return key;
323}
324
325static void *k_str2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
326{
327 if (!buf2ip(pdata->str.str, pdata->str.len, &kdata->ip))
328 return NULL;
329
330 return (void *)&kdata->ip.s_addr;
331}
332
333
334static void *k_str2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
335{
336 int i;
337
338 kdata->integer = 0;
339 for (i = 0; i < pdata->str.len; i++) {
340 uint32_t val = pdata->str.str[i] - '0';
341
342 if (val > 9)
343 break;
344
345 kdata->integer = kdata->integer * 10 + val;
346 }
347 return (void *)&kdata->integer;
348}
349
350/*****************************************************************/
351/* typed pattern to typed table key matrix: */
352/* pattern_keys[from pattern type][to table key type] */
353/* NULL pointer used for impossible pattern casts */
354/*****************************************************************/
355
356typedef void *(*pattern_key)(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len);
357static pattern_key pattern_keys[PATTERN_TYPES][STKTABLE_TYPES] = { { k_ip2ip, k_ip2int, k_ip2str },
358 { k_int2ip, k_int2int, k_int2str },
359 { k_str2ip, k_str2int, k_str2str } };
360/*
361 * Parse a pattern expression configuration:
362 * fetch keyword followed by format conversion keywords.
363 * Returns a pointer on allocated pattern expression structure.
364 */
365struct pattern_expr *pattern_parse_expr(char **str, int *idx)
366{
367 const char *endw;
368 const char *end;
369 struct pattern_expr *expr;
370 struct pattern_fetch *fetch;
371 struct pattern_conv *conv;
372 unsigned long prev_type;
373
374 if (!str[*idx])
375 goto out_error;
376
377 end = str[*idx] + strlen(str[*idx]);
378 endw = strchr(str[*idx], '(');
379
380 if (!endw)
381 endw = end;
382 else if ((end-1)[0] != ')')
383 goto out_error;
384
385 fetch = find_pattern_fetch(str[*idx], endw - str[*idx]);
386 if (!fetch)
387 goto out_error;
388
389 if (fetch->out_type >= PATTERN_TYPES)
390 goto out_error;
391
392 prev_type = fetch->out_type;
393 expr = calloc(1, sizeof(struct pattern_expr));
394
395 LIST_INIT(&(expr->conv_exprs));
396 expr->fetch = fetch;
397
398 if (end != endw) {
399 expr->arg_len = end - endw - 2;
Willy Tarreauac778f52010-01-26 19:02:46 +0100400 expr->arg = my_strndup(endw + 1, expr->arg_len);
Emeric Brun107ca302010-01-04 16:16:05 +0100401 }
402
403 for (*idx += 1; *(str[*idx]); (*idx)++) {
404 struct pattern_conv_expr *conv_expr;
405
406 end = str[*idx] + strlen(str[*idx]);
407 endw = strchr(str[*idx], '(');
408
409 if (!endw)
410 endw = end;
411 else if ((end-1)[0] != ')')
412 goto out_error;
413
414 conv = find_pattern_conv(str[*idx], endw - str[*idx]);
415 if (!conv)
416 break;
417
418 if (conv->in_type >= PATTERN_TYPES ||
419 conv->out_type >= PATTERN_TYPES)
420 goto out_error;
421
422 /* If impossible type conversion */
423 if (!pattern_casts[prev_type][conv->in_type])
424 goto out_error;
425
426 prev_type = conv->out_type;
427 conv_expr = calloc(1, sizeof(struct pattern_conv_expr));
428
429 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
430 conv_expr->conv = conv;
431
432 if (end != endw) {
Willy Tarreau9e92d322010-01-26 17:58:06 +0100433 int i = end - endw - 2;
434 char *p = my_strndup(endw + 1, i);
435
436 if (conv->parse_args) {
437 i = conv->parse_args(p, &conv_expr->arg_p, &conv_expr->arg_i);
438 free(p);
439 if (!i)
440 goto out_error;
441 } else {
442 conv_expr->arg_i = i;
443 conv_expr->arg_p = p;
444 }
Emeric Brun107ca302010-01-04 16:16:05 +0100445 }
446 }
447 return expr;
448
449out_error:
450 /* TODO: prune_pattern_expr(expr); */
451 return NULL;
452}
453
454/*
455 * Process a fetch + format conversion of defined by the pattern expression <expr>
456 * on request or response considering the <dir> parameter.
457 * Returns a pointer on a typed pattern structure containing the result or NULL if
458 * pattern is not found or when format conversion failed.
459 * If <p> is not null, function returns results in structure pointed by <p>.
460 * If <p> is null, functions returns a pointer on a static pattern structure.
461 */
462struct pattern *pattern_process(struct proxy *px, struct session *l4, void *l7, int dir,
463 struct pattern_expr *expr, struct pattern *p)
464{
465 struct pattern_conv_expr *conv_expr;
466
467 if (p == NULL)
468 p = &spattern;
469
470 if (!expr->fetch->process(px, l4, l7, dir, expr->arg, expr->arg_len, &p->data))
471 return NULL;
472
473 p->type = expr->fetch->out_type;
474
475 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
476 if (!pattern_casts[p->type][conv_expr->conv->in_type](&p->data))
477 return NULL;
478
479 p->type = conv_expr->conv->in_type;
Willy Tarreau1a51b632010-01-26 17:17:56 +0100480 if (!conv_expr->conv->process(conv_expr->arg_p, conv_expr->arg_i, &p->data))
Emeric Brun107ca302010-01-04 16:16:05 +0100481 return NULL;
482
483 p->type = conv_expr->conv->out_type;
484 }
485 return p;
486}
487
488/*
489 * Process a fetch + format conversion of defined by the pattern expression <expr>
490 * on request or response considering the <dir> parameter.
491 * Returns a pointer on a static tablekey structure of type <table_type> of
492 * the converted result.
493 */
494struct stktable_key *pattern_process_key(struct proxy *px, struct session *l4, void *l7, int dir,
495 struct pattern_expr *expr, unsigned long table_type)
496{
497 struct pattern *ptrn;
498
499 ptrn = pattern_process(px, l4, l7, dir, expr, NULL);
500 if (!ptrn)
501 return NULL;
502
503 stable_key.key_len = (size_t)-1;
504 stable_key.key = pattern_keys[ptrn->type][table_type](&ptrn->data, &stable_key.data, &stable_key.key_len);
505
506 if (!stable_key.key)
507 return NULL;
508
509 return &stable_key;
510}
511
512/*
513 * Returns 1 if pattern expression <expr> result cannot be converted to table key of
514 * type <table_type> .
515 *
516 * Used in configuration check
517 */
518int pattern_notusable_key(struct pattern_expr *expr, unsigned long table_type)
519{
520
521 if (table_type >= STKTABLE_TYPES)
522 return 1;
523
524 if (LIST_ISEMPTY(&expr->conv_exprs)) {
525 if (!pattern_keys[expr->fetch->out_type][table_type])
526 return 1;
527 } else {
528 struct pattern_conv_expr *conv_expr;
529 conv_expr = LIST_PREV(&expr->conv_exprs, typeof(conv_expr), list);
530
531 if (!pattern_keys[conv_expr->conv->out_type][table_type])
532 return 1;
533 }
534 return 0;
535}
536
Willy Tarreaud31d6eb2010-01-26 18:01:41 +0100537/* Converts an argument string to an IPv4 mask stored in network byte order in
538 * arg_i. Returns non-zero in case of success, 0 on error.
539 */
540static int pattern_conv_arg_to_ipmask(const char *arg_str, void **arg_p, int *arg_i)
541{
542 struct in_addr mask;
543
544 if (!str2mask(arg_str, &mask))
545 return 0;
546
547 *arg_i = mask.s_addr;
548 return 1;
549}
550
Emeric Brun107ca302010-01-04 16:16:05 +0100551/*****************************************************************/
552/* Pattern format convert functions */
553/*****************************************************************/
554
Willy Tarreau1a51b632010-01-26 17:17:56 +0100555static int pattern_conv_str2lower(const void *arg_p, int arg_i, union pattern_data *data)
Emeric Brun107ca302010-01-04 16:16:05 +0100556{
557 int i;
558
559 for (i = 0; i < data->str.len; i++) {
560 if ((data->str.str[i] >= 'A') && (data->str.str[i] <= 'Z'))
561 data->str.str[i] += 'a' - 'A';
562 }
563 return 1;
564}
565
Willy Tarreau1a51b632010-01-26 17:17:56 +0100566static int pattern_conv_str2upper(const void *arg_p, int arg_i, union pattern_data *data)
Emeric Brun107ca302010-01-04 16:16:05 +0100567{
568 int i;
569
570 for (i = 0; i < data->str.len; i++) {
571 if ((data->str.str[i] >= 'a') && (data->str.str[i] <= 'z'))
572 data->str.str[i] += 'A' - 'a';
573 }
574 return 1;
575}
576
Willy Tarreaud31d6eb2010-01-26 18:01:41 +0100577/* takes the netmask in arg_i */
578static int pattern_conv_ipmask(const void *arg_p, int arg_i, union pattern_data *data)
579{
580 data->ip.s_addr &= arg_i;
581 return 1;
582}
583
Emeric Brun107ca302010-01-04 16:16:05 +0100584/* Note: must not be declared <const> as its list will be overwritten */
585static struct pattern_conv_kw_list pattern_conv_kws = {{ },{
586 { "upper", pattern_conv_str2upper, PATTERN_TYPE_STRING, PATTERN_TYPE_STRING },
587 { "lower", pattern_conv_str2lower, PATTERN_TYPE_STRING, PATTERN_TYPE_STRING },
Willy Tarreaud31d6eb2010-01-26 18:01:41 +0100588 { "ipmask", pattern_conv_ipmask, PATTERN_TYPE_IP, PATTERN_TYPE_IP, pattern_conv_arg_to_ipmask },
Emeric Brun107ca302010-01-04 16:16:05 +0100589 { NULL, NULL, 0, 0 },
590}};
591
592__attribute__((constructor))
593static void __pattern_init(void)
594{
595 /* register pattern format convert keywords */
596 pattern_register_convs(&pattern_conv_kws);
597}