blob: aef151a47e3428f8464bb6830074cb19574bceff [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) {
Willy Tarreau9e92d322010-01-26 17:58:06 +0100435 int i = end - endw - 2;
436 char *p = my_strndup(endw + 1, i);
437
438 if (conv->parse_args) {
439 i = conv->parse_args(p, &conv_expr->arg_p, &conv_expr->arg_i);
440 free(p);
441 if (!i)
442 goto out_error;
443 } else {
444 conv_expr->arg_i = i;
445 conv_expr->arg_p = p;
446 }
Emeric Brun107ca302010-01-04 16:16:05 +0100447 }
448 }
449 return expr;
450
451out_error:
452 /* TODO: prune_pattern_expr(expr); */
453 return NULL;
454}
455
456/*
457 * Process a fetch + format conversion of defined by the pattern expression <expr>
458 * on request or response considering the <dir> parameter.
459 * Returns a pointer on a typed pattern structure containing the result or NULL if
460 * pattern is not found or when format conversion failed.
461 * If <p> is not null, function returns results in structure pointed by <p>.
462 * If <p> is null, functions returns a pointer on a static pattern structure.
463 */
464struct pattern *pattern_process(struct proxy *px, struct session *l4, void *l7, int dir,
465 struct pattern_expr *expr, struct pattern *p)
466{
467 struct pattern_conv_expr *conv_expr;
468
469 if (p == NULL)
470 p = &spattern;
471
472 if (!expr->fetch->process(px, l4, l7, dir, expr->arg, expr->arg_len, &p->data))
473 return NULL;
474
475 p->type = expr->fetch->out_type;
476
477 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
478 if (!pattern_casts[p->type][conv_expr->conv->in_type](&p->data))
479 return NULL;
480
481 p->type = conv_expr->conv->in_type;
Willy Tarreau1a51b632010-01-26 17:17:56 +0100482 if (!conv_expr->conv->process(conv_expr->arg_p, conv_expr->arg_i, &p->data))
Emeric Brun107ca302010-01-04 16:16:05 +0100483 return NULL;
484
485 p->type = conv_expr->conv->out_type;
486 }
487 return p;
488}
489
490/*
491 * Process a fetch + format conversion of defined by the pattern expression <expr>
492 * on request or response considering the <dir> parameter.
493 * Returns a pointer on a static tablekey structure of type <table_type> of
494 * the converted result.
495 */
496struct stktable_key *pattern_process_key(struct proxy *px, struct session *l4, void *l7, int dir,
497 struct pattern_expr *expr, unsigned long table_type)
498{
499 struct pattern *ptrn;
500
501 ptrn = pattern_process(px, l4, l7, dir, expr, NULL);
502 if (!ptrn)
503 return NULL;
504
505 stable_key.key_len = (size_t)-1;
506 stable_key.key = pattern_keys[ptrn->type][table_type](&ptrn->data, &stable_key.data, &stable_key.key_len);
507
508 if (!stable_key.key)
509 return NULL;
510
511 return &stable_key;
512}
513
514/*
515 * Returns 1 if pattern expression <expr> result cannot be converted to table key of
516 * type <table_type> .
517 *
518 * Used in configuration check
519 */
520int pattern_notusable_key(struct pattern_expr *expr, unsigned long table_type)
521{
522
523 if (table_type >= STKTABLE_TYPES)
524 return 1;
525
526 if (LIST_ISEMPTY(&expr->conv_exprs)) {
527 if (!pattern_keys[expr->fetch->out_type][table_type])
528 return 1;
529 } else {
530 struct pattern_conv_expr *conv_expr;
531 conv_expr = LIST_PREV(&expr->conv_exprs, typeof(conv_expr), list);
532
533 if (!pattern_keys[conv_expr->conv->out_type][table_type])
534 return 1;
535 }
536 return 0;
537}
538
539/*****************************************************************/
540/* Pattern format convert functions */
541/*****************************************************************/
542
Willy Tarreau1a51b632010-01-26 17:17:56 +0100543static int pattern_conv_str2lower(const void *arg_p, int arg_i, union pattern_data *data)
Emeric Brun107ca302010-01-04 16:16:05 +0100544{
545 int i;
546
547 for (i = 0; i < data->str.len; i++) {
548 if ((data->str.str[i] >= 'A') && (data->str.str[i] <= 'Z'))
549 data->str.str[i] += 'a' - 'A';
550 }
551 return 1;
552}
553
Willy Tarreau1a51b632010-01-26 17:17:56 +0100554static int pattern_conv_str2upper(const void *arg_p, int arg_i, union pattern_data *data)
Emeric Brun107ca302010-01-04 16:16:05 +0100555{
556 int i;
557
558 for (i = 0; i < data->str.len; i++) {
559 if ((data->str.str[i] >= 'a') && (data->str.str[i] <= 'z'))
560 data->str.str[i] += 'A' - 'a';
561 }
562 return 1;
563}
564
565/* Note: must not be declared <const> as its list will be overwritten */
566static struct pattern_conv_kw_list pattern_conv_kws = {{ },{
567 { "upper", pattern_conv_str2upper, PATTERN_TYPE_STRING, PATTERN_TYPE_STRING },
568 { "lower", pattern_conv_str2lower, PATTERN_TYPE_STRING, PATTERN_TYPE_STRING },
569 { NULL, NULL, 0, 0 },
570}};
571
572__attribute__((constructor))
573static void __pattern_init(void)
574{
575 /* register pattern format convert keywords */
576 pattern_register_convs(&pattern_conv_kws);
577}