blob: ba8d5a01e9ce52d7471223aa058ef6e567239e31 [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>
Emeric Brun107ca302010-01-04 16:16:05 +010015
16#include <proto/pattern.h>
Emeric Brun485479d2010-09-23 18:02:19 +020017#include <proto/buffers.h>
Emeric Brun107ca302010-01-04 16:16:05 +010018#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
Emeric Brun107ca302010-01-04 16:16:05 +010033/* list head of all known pattern fetch keywords */
34static struct pattern_fetch_kw_list pattern_fetches = {
35 .list = LIST_HEAD_INIT(pattern_fetches.list)
36};
37
38/* list head of all known pattern format conversion keywords */
39static struct pattern_conv_kw_list pattern_convs = {
40 .list = LIST_HEAD_INIT(pattern_convs.list)
41};
42
43/*
44 * Registers the pattern fetch keyword list <kwl> as a list of valid keywords for next
45 * parsing sessions.
46 */
47void pattern_register_fetches(struct pattern_fetch_kw_list *pfkl)
48{
49 LIST_ADDQ(&pattern_fetches.list, &pfkl->list);
50}
51
52/*
53 * Registers the pattern format coverstion keyword list <pckl> as a list of valid keywords for next
54 * parsing sessions.
55 */
56void pattern_register_convs(struct pattern_conv_kw_list *pckl)
57{
58 LIST_ADDQ(&pattern_convs.list, &pckl->list);
59}
60
61/*
62 * Returns the pointer on pattern fetch keyword structure identified by
63 * string of <len> in buffer <kw>.
64 *
65 */
66struct pattern_fetch *find_pattern_fetch(const char *kw, int len)
67{
68 int index;
69 struct pattern_fetch_kw_list *kwl;
70
71 list_for_each_entry(kwl, &pattern_fetches.list, list) {
72 for (index = 0; kwl->kw[index].kw != NULL; index++) {
73 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
74 kwl->kw[index].kw[len] == '\0')
75 return &kwl->kw[index];
76 }
77 }
78 return NULL;
79}
80
81/*
82 * Returns the pointer on pattern format conversion keyword structure identified by
83 * string of <len> in buffer <kw>.
84 *
85 */
86struct pattern_conv *find_pattern_conv(const char *kw, int len)
87{
88 int index;
89 struct pattern_conv_kw_list *kwl;
90
91 list_for_each_entry(kwl, &pattern_convs.list, list) {
92 for (index = 0; kwl->kw[index].kw != NULL; index++) {
93 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
94 kwl->kw[index].kw[len] == '\0')
95 return &kwl->kw[index];
96 }
97 }
98 return NULL;
99}
100
101
102/*
103* Returns a static trash struct chunk to use in pattern casts or format conversions
104* Swiths the 2 available trash buffers to protect data during convert
105*/
106static struct chunk *get_trash_chunk(void)
107{
108 if (pattern_trash_buf == pattern_trash_buf1)
109 pattern_trash_buf = pattern_trash_buf2;
110 else
111 pattern_trash_buf = pattern_trash_buf1;
112
Emeric Brun485479d2010-09-23 18:02:19 +0200113 chunk_init(&trash_chunk, pattern_trash_buf, BUFSIZE);
Emeric Brun107ca302010-01-04 16:16:05 +0100114
115 return &trash_chunk;
116}
117
118/*
119* Used to set pattern data from a struct chunk, could be the trash struct chunk
120*/
121static void pattern_data_setstring(union pattern_data *data, struct chunk *c)
122{
Emeric Brun485479d2010-09-23 18:02:19 +0200123 chunk_initlen(&data->str, c->str, c->size, c->len);
Emeric Brun107ca302010-01-04 16:16:05 +0100124}
125
126/******************************************************************/
127/* Pattern casts functions */
128/******************************************************************/
129
130static int c_ip2int(union pattern_data *data)
131{
132 data->integer = ntohl(data->ip.s_addr);
133 return 1;
134}
135
136static int c_ip2str(union pattern_data *data)
137{
138 struct chunk *trash = get_trash_chunk();
139
140 if (!inet_ntop(AF_INET, (void *)&data->ip, trash->str, trash->size))
141 return 0;
142
143 trash->len = strlen(trash->str);
144 pattern_data_setstring(data, trash);
145
146 return 1;
147}
148
149static int c_int2ip(union pattern_data *data)
150{
151 data->ip.s_addr = htonl(data->integer);
152 return 1;
153}
154
Emeric Brun107ca302010-01-04 16:16:05 +0100155static int c_str2ip(union pattern_data *data)
156{
157 if (!buf2ip(data->str.str, data->str.len, &data->ip))
158 return 0;
159 return 1;
160}
161
162static int c_int2str(union pattern_data *data)
163{
164 struct chunk *trash = get_trash_chunk();
165 char *pos;
166
167 pos = ultoa_r(data->integer, trash->str, trash->size);
168
169 if (!pos)
170 return 0;
171
Emeric Brun485479d2010-09-23 18:02:19 +0200172 trash->size = trash->size - (pos - trash->str);
Emeric Brun107ca302010-01-04 16:16:05 +0100173 trash->str = pos;
174 trash->len = strlen(pos);
175
176 pattern_data_setstring(data, trash);
177
178 return 1;
179}
180
Emeric Brun485479d2010-09-23 18:02:19 +0200181static int c_datadup(union pattern_data *data)
182{
183 struct chunk *trash = get_trash_chunk();
184
185 trash->len = data->str.len < trash->size ? data->str.len : trash->size;
186 memcpy(trash->str, data->str.str, trash->len);
187
188 pattern_data_setstring(data, trash);
189
190 return 1;
191}
192
193
Emeric Brun107ca302010-01-04 16:16:05 +0100194static int c_donothing(union pattern_data *data)
195{
196 return 1;
197}
198
199static int c_str2int(union pattern_data *data)
200{
201 int i;
202 uint32_t ret = 0;
203
204 for (i = 0; i < data->str.len; i++) {
205 uint32_t val = data->str.str[i] - '0';
206
207 if (val > 9)
208 break;
209
210 ret = ret * 10 + val;
211 }
212
213 data->integer = ret;
214 return 1;
215}
216
217/*****************************************************************/
218/* Pattern casts matrix: */
219/* pattern_casts[from type][to type] */
220/* NULL pointer used for impossible pattern casts */
221/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100222
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200223typedef int (*pattern_cast_fct)(union pattern_data *data);
224static pattern_cast_fct pattern_casts[PATTERN_TYPES][PATTERN_TYPES] = {
Emeric Brun485479d2010-09-23 18:02:19 +0200225/* to: IP INTEGER STRING DATA CONSTSTRING CONSTDATA */
226/* from: IP */ { c_donothing, c_ip2int, c_ip2str, NULL, c_ip2str, NULL },
227/* INTEGER */ { c_int2ip, c_donothing, c_int2str, NULL, c_int2str, NULL },
228/* STRING */ { c_str2ip, c_str2int, c_donothing, c_donothing, c_donothing, c_donothing },
229/* DATA */ { NULL, NULL, NULL, c_donothing, NULL, c_donothing },
230/* CONSTSTRING */ { c_str2ip, c_str2int, c_datadup, c_datadup, c_donothing, c_donothing },
231/* CONSTDATA */ { NULL, NULL, NULL, c_datadup, NULL, NULL },
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200232};
Emeric Brun107ca302010-01-04 16:16:05 +0100233
Emeric Brun107ca302010-01-04 16:16:05 +0100234
Emeric Brun107ca302010-01-04 16:16:05 +0100235/*
236 * Parse a pattern expression configuration:
237 * fetch keyword followed by format conversion keywords.
238 * Returns a pointer on allocated pattern expression structure.
239 */
Emeric Brun485479d2010-09-23 18:02:19 +0200240struct pattern_expr *pattern_parse_expr(char **str, int *idx, char *err, int err_size)
Emeric Brun107ca302010-01-04 16:16:05 +0100241{
242 const char *endw;
243 const char *end;
244 struct pattern_expr *expr;
245 struct pattern_fetch *fetch;
246 struct pattern_conv *conv;
247 unsigned long prev_type;
Emeric Brun485479d2010-09-23 18:02:19 +0200248 char *p;
Emeric Brun107ca302010-01-04 16:16:05 +0100249
Emeric Brun485479d2010-09-23 18:02:19 +0200250 snprintf(err, err_size, "memory error.");
251 if (!str[*idx]) {
252
253 snprintf(err, err_size, "missing fetch method.");
Emeric Brun107ca302010-01-04 16:16:05 +0100254 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200255 }
Emeric Brun107ca302010-01-04 16:16:05 +0100256
257 end = str[*idx] + strlen(str[*idx]);
258 endw = strchr(str[*idx], '(');
259
260 if (!endw)
261 endw = end;
Emeric Brun485479d2010-09-23 18:02:19 +0200262 else if ((end-1)[0] != ')') {
263 p = my_strndup(str[*idx], endw - str[*idx]);
264 if (p) {
265 snprintf(err, err_size, "syntax error: missing ')' after keyword '%s'.", p);
266 free(p);
267 }
Emeric Brun107ca302010-01-04 16:16:05 +0100268 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200269 }
Emeric Brun107ca302010-01-04 16:16:05 +0100270
271 fetch = find_pattern_fetch(str[*idx], endw - str[*idx]);
Emeric Brun485479d2010-09-23 18:02:19 +0200272 if (!fetch) {
273 p = my_strndup(str[*idx], endw - str[*idx]);
274 if (p) {
275 snprintf(err, err_size, "unknown fetch method '%s'.", p);
276 free(p);
277 }
Emeric Brun107ca302010-01-04 16:16:05 +0100278 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200279 }
280 if (fetch->out_type >= PATTERN_TYPES) {
Emeric Brun107ca302010-01-04 16:16:05 +0100281
Emeric Brun485479d2010-09-23 18:02:19 +0200282 p = my_strndup(str[*idx], endw - str[*idx]);
283 if (p) {
284 snprintf(err, err_size, "returns type of fetch method '%s' is unknown.", p);
285 free(p);
286 }
Emeric Brun107ca302010-01-04 16:16:05 +0100287 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200288 }
Emeric Brun107ca302010-01-04 16:16:05 +0100289
290 prev_type = fetch->out_type;
291 expr = calloc(1, sizeof(struct pattern_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200292 if (!expr)
293 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100294
295 LIST_INIT(&(expr->conv_exprs));
296 expr->fetch = fetch;
297
298 if (end != endw) {
Emeric Brun485479d2010-09-23 18:02:19 +0200299 int i = end - endw - 2;
300
301 if (!fetch->parse_args) {
302 p = my_strndup(str[*idx], endw - str[*idx]);
303 if (p) {
304 snprintf(err, err_size, "fetch method '%s' does not support any args.", p);
305 free(p);
306 }
307 goto out_error;
308 }
309 p = my_strndup(endw + 1, i);
310 if (!p)
311 goto out_error;
312 i = fetch->parse_args(p, &expr->arg_p, &expr->arg_i);
313 free(p);
314 if (!i) {
315 p = my_strndup(str[*idx], endw - str[*idx]);
316 if (p) {
317 snprintf(err, err_size, "invalid args in fetch method '%s'.", p);
318 free(p);
319 }
320 goto out_error;
321 }
322 }
323 else if (fetch->parse_args) {
324 p = my_strndup(str[*idx], endw - str[*idx]);
325 if (p) {
326 snprintf(err, err_size, "missing args for fetch method '%s'.", p);
327 free(p);
328 }
329 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100330 }
331
332 for (*idx += 1; *(str[*idx]); (*idx)++) {
333 struct pattern_conv_expr *conv_expr;
334
335 end = str[*idx] + strlen(str[*idx]);
336 endw = strchr(str[*idx], '(');
337
338 if (!endw)
339 endw = end;
Emeric Brun485479d2010-09-23 18:02:19 +0200340 else if ((end-1)[0] != ')') {
341 p = my_strndup(str[*idx], endw - str[*idx]);
342 if (p) {
343 snprintf(err, err_size, "syntax error, missing ')' after keyword '%s'.", p);
344 free(p);
345 }
Emeric Brun107ca302010-01-04 16:16:05 +0100346 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200347 }
Emeric Brun107ca302010-01-04 16:16:05 +0100348
349 conv = find_pattern_conv(str[*idx], endw - str[*idx]);
350 if (!conv)
351 break;
352
353 if (conv->in_type >= PATTERN_TYPES ||
Emeric Brun485479d2010-09-23 18:02:19 +0200354 conv->out_type >= PATTERN_TYPES) {
355 p = my_strndup(str[*idx], endw - str[*idx]);
356 if (p) {
357 snprintf(err, err_size, "returns type of conv method '%s' is unknown.", p);
358 free(p);
359 }
Emeric Brun107ca302010-01-04 16:16:05 +0100360 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200361 }
Emeric Brun107ca302010-01-04 16:16:05 +0100362
363 /* If impossible type conversion */
Emeric Brun485479d2010-09-23 18:02:19 +0200364 if (!pattern_casts[prev_type][conv->in_type]) {
365 p = my_strndup(str[*idx], endw - str[*idx]);
366 if (p) {
367 snprintf(err, err_size, "conv method '%s' cannot be applied.", p);
368 free(p);
369 }
Emeric Brun107ca302010-01-04 16:16:05 +0100370 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200371 }
Emeric Brun107ca302010-01-04 16:16:05 +0100372
373 prev_type = conv->out_type;
374 conv_expr = calloc(1, sizeof(struct pattern_conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200375 if (!conv_expr)
376 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100377
378 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
379 conv_expr->conv = conv;
380
381 if (end != endw) {
Willy Tarreau9e92d322010-01-26 17:58:06 +0100382 int i = end - endw - 2;
Emeric Brun485479d2010-09-23 18:02:19 +0200383
384 if (!conv->parse_args) {
385 p = my_strndup(str[*idx], endw - str[*idx]);
386
387 if (p) {
388 snprintf(err, err_size, "conv method '%s' does not support any args.", p);
389 free(p);
390 }
391 goto out_error;
392 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100393
Emeric Brun485479d2010-09-23 18:02:19 +0200394 p = my_strndup(endw + 1, i);
395 if (!p)
396 goto out_error;
397 i = conv->parse_args(p, &conv_expr->arg_p, &conv_expr->arg_i);
398 free(p);
399 if (!i) {
400 p = my_strndup(str[*idx], endw - str[*idx]);
401 if (p) {
402 snprintf(err, err_size, "invalid args in conv method '%s'.", p);
403 free(p);
404 }
405 goto out_error;
406 }
407 }
408 else if (conv->parse_args) {
409 p = my_strndup(str[*idx], endw - str[*idx]);
410 if (p) {
411 snprintf(err, err_size, "missing args for conv method '%s'.", p);
Willy Tarreau9e92d322010-01-26 17:58:06 +0100412 free(p);
Willy Tarreau9e92d322010-01-26 17:58:06 +0100413 }
Emeric Brun485479d2010-09-23 18:02:19 +0200414 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100415 }
Emeric Brun485479d2010-09-23 18:02:19 +0200416
Emeric Brun107ca302010-01-04 16:16:05 +0100417 }
Emeric Brun485479d2010-09-23 18:02:19 +0200418
Emeric Brun107ca302010-01-04 16:16:05 +0100419 return expr;
420
421out_error:
422 /* TODO: prune_pattern_expr(expr); */
423 return NULL;
424}
425
426/*
427 * Process a fetch + format conversion of defined by the pattern expression <expr>
428 * on request or response considering the <dir> parameter.
429 * Returns a pointer on a typed pattern structure containing the result or NULL if
430 * pattern is not found or when format conversion failed.
431 * If <p> is not null, function returns results in structure pointed by <p>.
432 * If <p> is null, functions returns a pointer on a static pattern structure.
433 */
434struct pattern *pattern_process(struct proxy *px, struct session *l4, void *l7, int dir,
435 struct pattern_expr *expr, struct pattern *p)
436{
437 struct pattern_conv_expr *conv_expr;
438
439 if (p == NULL)
440 p = &spattern;
441
Emeric Brun485479d2010-09-23 18:02:19 +0200442 if (!expr->fetch->process(px, l4, l7, dir, expr->arg_p, expr->arg_i, &p->data))
Emeric Brun107ca302010-01-04 16:16:05 +0100443 return NULL;
444
445 p->type = expr->fetch->out_type;
446
447 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
448 if (!pattern_casts[p->type][conv_expr->conv->in_type](&p->data))
449 return NULL;
450
451 p->type = conv_expr->conv->in_type;
Willy Tarreau1a51b632010-01-26 17:17:56 +0100452 if (!conv_expr->conv->process(conv_expr->arg_p, conv_expr->arg_i, &p->data))
Emeric Brun107ca302010-01-04 16:16:05 +0100453 return NULL;
454
455 p->type = conv_expr->conv->out_type;
456 }
457 return p;
458}
459
Emeric Brun485479d2010-09-23 18:02:19 +0200460/* Converts an argument string mask to a pattern_arg type IP.
461 * Returns non-zero in case of success, 0 on error.
Willy Tarreaud31d6eb2010-01-26 18:01:41 +0100462 */
Emeric Brun485479d2010-09-23 18:02:19 +0200463int pattern_arg_ipmask(const char *arg_str, struct pattern_arg **arg_p, int *arg_i)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +0100464{
Emeric Brun485479d2010-09-23 18:02:19 +0200465 *arg_i = 1;
466 *arg_p = calloc(1, *arg_i*sizeof(struct pattern_arg));
467 (*arg_p)->type = PATTERN_ARG_TYPE_IP;
Willy Tarreaud31d6eb2010-01-26 18:01:41 +0100468
Emeric Brun485479d2010-09-23 18:02:19 +0200469 if (!str2mask(arg_str, &(*arg_p)->data.ip))
Willy Tarreaud31d6eb2010-01-26 18:01:41 +0100470 return 0;
471
Willy Tarreaud31d6eb2010-01-26 18:01:41 +0100472 return 1;
473}
474
Emeric Brun485479d2010-09-23 18:02:19 +0200475
476/* Converts an argument string to a pattern_arg type STRING.
477 * Returns non-zero in case of success, 0 on error.
478 */
479int pattern_arg_str(const char *arg_str, struct pattern_arg **arg_p, int *arg_i)
480{
481 *arg_i = 1;
482 *arg_p = calloc(1, *arg_i*sizeof(struct pattern_arg));
483 (*arg_p)->type = PATTERN_ARG_TYPE_STRING;
484 (*arg_p)->data.str.str = strdup(arg_str);
485 (*arg_p)->data.str.len = strlen(arg_str);
486
487
488 return 1;
489}
490
491
Emeric Brun107ca302010-01-04 16:16:05 +0100492/*****************************************************************/
493/* Pattern format convert functions */
494/*****************************************************************/
495
Emeric Brun485479d2010-09-23 18:02:19 +0200496static int pattern_conv_str2lower(const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Emeric Brun107ca302010-01-04 16:16:05 +0100497{
498 int i;
499
Emeric Brun485479d2010-09-23 18:02:19 +0200500 if (!data->str.size)
501 return 0;
502
Emeric Brun107ca302010-01-04 16:16:05 +0100503 for (i = 0; i < data->str.len; i++) {
504 if ((data->str.str[i] >= 'A') && (data->str.str[i] <= 'Z'))
505 data->str.str[i] += 'a' - 'A';
506 }
507 return 1;
508}
509
Emeric Brun485479d2010-09-23 18:02:19 +0200510static int pattern_conv_str2upper(const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Emeric Brun107ca302010-01-04 16:16:05 +0100511{
512 int i;
513
Emeric Brun485479d2010-09-23 18:02:19 +0200514 if (!data->str.size)
515 return 0;
516
Emeric Brun107ca302010-01-04 16:16:05 +0100517 for (i = 0; i < data->str.len; i++) {
518 if ((data->str.str[i] >= 'a') && (data->str.str[i] <= 'z'))
519 data->str.str[i] += 'A' - 'a';
520 }
521 return 1;
522}
523
Willy Tarreaud31d6eb2010-01-26 18:01:41 +0100524/* takes the netmask in arg_i */
Emeric Brun485479d2010-09-23 18:02:19 +0200525static int pattern_conv_ipmask(const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +0100526{
527 data->ip.s_addr &= arg_i;
528 return 1;
529}
530
Emeric Brun107ca302010-01-04 16:16:05 +0100531/* Note: must not be declared <const> as its list will be overwritten */
532static struct pattern_conv_kw_list pattern_conv_kws = {{ },{
Emeric Brun485479d2010-09-23 18:02:19 +0200533 { "upper", pattern_conv_str2upper, NULL, PATTERN_TYPE_STRING, PATTERN_TYPE_STRING },
534 { "lower", pattern_conv_str2lower, NULL, PATTERN_TYPE_STRING, PATTERN_TYPE_STRING },
535 { "ipmask", pattern_conv_ipmask, pattern_arg_ipmask, PATTERN_TYPE_IP, PATTERN_TYPE_IP },
536 { NULL, NULL, NULL, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +0100537}};
538
539__attribute__((constructor))
540static void __pattern_init(void)
541{
542 /* register pattern format convert keywords */
543 pattern_register_convs(&pattern_conv_kws);
544}