blob: 4270cde30dfcab601563fb729f93ef73e892ea24 [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;
400 expr->arg = malloc(expr->arg_len + 1);
401 expr->arg = memcpy(expr->arg, endw + 1, expr->arg_len);
402 expr->arg[expr->arg_len] = '\0';
403 }
404
405 for (*idx += 1; *(str[*idx]); (*idx)++) {
406 struct pattern_conv_expr *conv_expr;
407
408 end = str[*idx] + strlen(str[*idx]);
409 endw = strchr(str[*idx], '(');
410
411 if (!endw)
412 endw = end;
413 else if ((end-1)[0] != ')')
414 goto out_error;
415
416 conv = find_pattern_conv(str[*idx], endw - str[*idx]);
417 if (!conv)
418 break;
419
420 if (conv->in_type >= PATTERN_TYPES ||
421 conv->out_type >= PATTERN_TYPES)
422 goto out_error;
423
424 /* If impossible type conversion */
425 if (!pattern_casts[prev_type][conv->in_type])
426 goto out_error;
427
428 prev_type = conv->out_type;
429 conv_expr = calloc(1, sizeof(struct pattern_conv_expr));
430
431 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
432 conv_expr->conv = conv;
433
434 if (end != endw) {
435 conv_expr->arg_len = end - endw - 2;
436 conv_expr->arg = malloc(conv_expr->arg_len + 1);
437 conv_expr->arg = memcpy(conv_expr->arg, endw + 1, conv_expr->arg_len);
438 conv_expr->arg[expr->arg_len] = '\0';
439 }
440 }
441 return expr;
442
443out_error:
444 /* TODO: prune_pattern_expr(expr); */
445 return NULL;
446}
447
448/*
449 * Process a fetch + format conversion of defined by the pattern expression <expr>
450 * on request or response considering the <dir> parameter.
451 * Returns a pointer on a typed pattern structure containing the result or NULL if
452 * pattern is not found or when format conversion failed.
453 * If <p> is not null, function returns results in structure pointed by <p>.
454 * If <p> is null, functions returns a pointer on a static pattern structure.
455 */
456struct pattern *pattern_process(struct proxy *px, struct session *l4, void *l7, int dir,
457 struct pattern_expr *expr, struct pattern *p)
458{
459 struct pattern_conv_expr *conv_expr;
460
461 if (p == NULL)
462 p = &spattern;
463
464 if (!expr->fetch->process(px, l4, l7, dir, expr->arg, expr->arg_len, &p->data))
465 return NULL;
466
467 p->type = expr->fetch->out_type;
468
469 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
470 if (!pattern_casts[p->type][conv_expr->conv->in_type](&p->data))
471 return NULL;
472
473 p->type = conv_expr->conv->in_type;
474 if (!conv_expr->conv->process(conv_expr->arg, expr->arg_len, &p->data))
475 return NULL;
476
477 p->type = conv_expr->conv->out_type;
478 }
479 return p;
480}
481
482/*
483 * Process a fetch + format conversion of defined by the pattern expression <expr>
484 * on request or response considering the <dir> parameter.
485 * Returns a pointer on a static tablekey structure of type <table_type> of
486 * the converted result.
487 */
488struct stktable_key *pattern_process_key(struct proxy *px, struct session *l4, void *l7, int dir,
489 struct pattern_expr *expr, unsigned long table_type)
490{
491 struct pattern *ptrn;
492
493 ptrn = pattern_process(px, l4, l7, dir, expr, NULL);
494 if (!ptrn)
495 return NULL;
496
497 stable_key.key_len = (size_t)-1;
498 stable_key.key = pattern_keys[ptrn->type][table_type](&ptrn->data, &stable_key.data, &stable_key.key_len);
499
500 if (!stable_key.key)
501 return NULL;
502
503 return &stable_key;
504}
505
506/*
507 * Returns 1 if pattern expression <expr> result cannot be converted to table key of
508 * type <table_type> .
509 *
510 * Used in configuration check
511 */
512int pattern_notusable_key(struct pattern_expr *expr, unsigned long table_type)
513{
514
515 if (table_type >= STKTABLE_TYPES)
516 return 1;
517
518 if (LIST_ISEMPTY(&expr->conv_exprs)) {
519 if (!pattern_keys[expr->fetch->out_type][table_type])
520 return 1;
521 } else {
522 struct pattern_conv_expr *conv_expr;
523 conv_expr = LIST_PREV(&expr->conv_exprs, typeof(conv_expr), list);
524
525 if (!pattern_keys[conv_expr->conv->out_type][table_type])
526 return 1;
527 }
528 return 0;
529}
530
531/*****************************************************************/
532/* Pattern format convert functions */
533/*****************************************************************/
534
535static int pattern_conv_str2lower(const char *arg, int arg_len, union pattern_data *data)
536{
537 int i;
538
539 for (i = 0; i < data->str.len; i++) {
540 if ((data->str.str[i] >= 'A') && (data->str.str[i] <= 'Z'))
541 data->str.str[i] += 'a' - 'A';
542 }
543 return 1;
544}
545
546static int pattern_conv_str2upper(const char *arg, int arg_len, union pattern_data *data)
547{
548 int i;
549
550 for (i = 0; i < data->str.len; i++) {
551 if ((data->str.str[i] >= 'a') && (data->str.str[i] <= 'z'))
552 data->str.str[i] += 'A' - 'a';
553 }
554 return 1;
555}
556
557/* Note: must not be declared <const> as its list will be overwritten */
558static struct pattern_conv_kw_list pattern_conv_kws = {{ },{
559 { "upper", pattern_conv_str2upper, PATTERN_TYPE_STRING, PATTERN_TYPE_STRING },
560 { "lower", pattern_conv_str2lower, PATTERN_TYPE_STRING, PATTERN_TYPE_STRING },
561 { NULL, NULL, 0, 0 },
562}};
563
564__attribute__((constructor))
565static void __pattern_init(void)
566{
567 /* register pattern format convert keywords */
568 pattern_register_convs(&pattern_conv_kws);
569}