blob: f62163c4143caa293f902ceb877e1596993ebb12 [file] [log] [blame]
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001/*
2 * General protocol-agnostic payload-based sample fetches and ACLs
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 <stdlib.h>
14#include <string.h>
15
16#include <proto/acl.h>
17#include <proto/arg.h>
18#include <proto/channel.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010019#include <proto/pattern.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010020#include <proto/payload.h>
21#include <proto/sample.h>
22
23
24/************************************************************************/
25/* All supported sample fetch functions must be declared here */
26/************************************************************************/
27
28/* wait for more data as long as possible, then return TRUE. This should be
29 * used with content inspection.
30 */
31static int
32smp_fetch_wait_end(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020033 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaud4c33c82013-01-07 21:59:07 +010034{
35 if (!(opt & SMP_OPT_FINAL)) {
36 smp->flags |= SMP_F_MAY_CHANGE;
37 return 0;
38 }
39 smp->type = SMP_T_BOOL;
40 smp->data.uint = 1;
41 return 1;
42}
43
44/* return the number of bytes in the request buffer */
45static int
Willy Tarreau47e8eba2013-09-11 23:28:46 +020046smp_fetch_len(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020047 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaud4c33c82013-01-07 21:59:07 +010048{
Willy Tarreau47e8eba2013-09-11 23:28:46 +020049 struct channel *chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? s->rep : s->req;
50
51 if (!s || !chn)
Willy Tarreaud4c33c82013-01-07 21:59:07 +010052 return 0;
53
54 smp->type = SMP_T_UINT;
Willy Tarreau47e8eba2013-09-11 23:28:46 +020055 smp->data.uint = chn->buf->i;
Willy Tarreaud4c33c82013-01-07 21:59:07 +010056 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
57 return 1;
58}
59
60/* returns the type of SSL hello message (mainly used to detect an SSL hello) */
61static int
62smp_fetch_ssl_hello_type(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020063 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaud4c33c82013-01-07 21:59:07 +010064{
65 int hs_len;
66 int hs_type, bleft;
67 struct channel *chn;
68 const unsigned char *data;
69
70 if (!s)
71 goto not_ssl_hello;
72
73 chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? s->rep : s->req;
74
Willy Tarreau1e89acb2014-11-26 13:24:24 +010075 if (!chn)
76 goto not_ssl_hello;
77
Willy Tarreaud4c33c82013-01-07 21:59:07 +010078 bleft = chn->buf->i;
79 data = (const unsigned char *)chn->buf->p;
80
81 if (!bleft)
82 goto too_short;
83
84 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
85 /* SSLv3 header format */
86 if (bleft < 9)
87 goto too_short;
88
89 /* ssl version 3 */
90 if ((data[1] << 16) + data[2] < 0x00030000)
91 goto not_ssl_hello;
92
93 /* ssl message len must present handshake type and len */
94 if ((data[3] << 8) + data[4] < 4)
95 goto not_ssl_hello;
96
97 /* format introduced with SSLv3 */
98
99 hs_type = (int)data[5];
100 hs_len = ( data[6] << 16 ) + ( data[7] << 8 ) + data[8];
101
102 /* not a full handshake */
103 if (bleft < (9 + hs_len))
104 goto too_short;
105
106 }
107 else {
108 goto not_ssl_hello;
109 }
110
111 smp->type = SMP_T_UINT;
112 smp->data.uint = hs_type;
113 smp->flags = SMP_F_VOLATILE;
114
115 return 1;
116
117 too_short:
118 smp->flags = SMP_F_MAY_CHANGE;
119
120 not_ssl_hello:
121
122 return 0;
123}
124
125/* Return the version of the SSL protocol in the request. It supports both
126 * SSLv3 (TLSv1) header format for any message, and SSLv2 header format for
127 * the hello message. The SSLv3 format is described in RFC 2246 p49, and the
128 * SSLv2 format is described here, and completed p67 of RFC 2246 :
129 * http://wp.netscape.com/eng/security/SSL_2.html
130 *
131 * Note: this decoder only works with non-wrapping data.
132 */
133static int
134smp_fetch_req_ssl_ver(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200135 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100136{
137 int version, bleft, msg_len;
138 const unsigned char *data;
139
140 if (!s || !s->req)
141 return 0;
142
143 msg_len = 0;
144 bleft = s->req->buf->i;
145 if (!bleft)
146 goto too_short;
147
148 data = (const unsigned char *)s->req->buf->p;
149 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
150 /* SSLv3 header format */
151 if (bleft < 5)
152 goto too_short;
153
154 version = (data[1] << 16) + data[2]; /* version: major, minor */
155 msg_len = (data[3] << 8) + data[4]; /* record length */
156
157 /* format introduced with SSLv3 */
158 if (version < 0x00030000)
159 goto not_ssl;
160
161 /* message length between 1 and 2^14 + 2048 */
162 if (msg_len < 1 || msg_len > ((1<<14) + 2048))
163 goto not_ssl;
164
165 bleft -= 5; data += 5;
166 } else {
167 /* SSLv2 header format, only supported for hello (msg type 1) */
168 int rlen, plen, cilen, silen, chlen;
169
170 if (*data & 0x80) {
171 if (bleft < 3)
172 goto too_short;
173 /* short header format : 15 bits for length */
174 rlen = ((data[0] & 0x7F) << 8) | data[1];
175 plen = 0;
176 bleft -= 2; data += 2;
177 } else {
178 if (bleft < 4)
179 goto too_short;
180 /* long header format : 14 bits for length + pad length */
181 rlen = ((data[0] & 0x3F) << 8) | data[1];
182 plen = data[2];
183 bleft -= 3; data += 2;
184 }
185
186 if (*data != 0x01)
187 goto not_ssl;
188 bleft--; data++;
189
190 if (bleft < 8)
191 goto too_short;
192 version = (data[0] << 16) + data[1]; /* version: major, minor */
193 cilen = (data[2] << 8) + data[3]; /* cipher len, multiple of 3 */
194 silen = (data[4] << 8) + data[5]; /* session_id_len: 0 or 16 */
195 chlen = (data[6] << 8) + data[7]; /* 16<=challenge length<=32 */
196
197 bleft -= 8; data += 8;
198 if (cilen % 3 != 0)
199 goto not_ssl;
200 if (silen && silen != 16)
201 goto not_ssl;
202 if (chlen < 16 || chlen > 32)
203 goto not_ssl;
204 if (rlen != 9 + cilen + silen + chlen)
205 goto not_ssl;
206
207 /* focus on the remaining data length */
208 msg_len = cilen + silen + chlen + plen;
209 }
210 /* We could recursively check that the buffer ends exactly on an SSL
211 * fragment boundary and that a possible next segment is still SSL,
212 * but that's a bit pointless. However, we could still check that
213 * all the part of the request which fits in a buffer is already
214 * there.
215 */
216 if (msg_len > buffer_max_len(s->req) + s->req->buf->data - s->req->buf->p)
217 msg_len = buffer_max_len(s->req) + s->req->buf->data - s->req->buf->p;
218
219 if (bleft < msg_len)
220 goto too_short;
221
222 /* OK that's enough. We have at least the whole message, and we have
223 * the protocol version.
224 */
225 smp->type = SMP_T_UINT;
226 smp->data.uint = version;
227 smp->flags = SMP_F_VOLATILE;
228 return 1;
229
230 too_short:
231 smp->flags = SMP_F_MAY_CHANGE;
232 not_ssl:
233 return 0;
234}
235
236/* Try to extract the Server Name Indication that may be presented in a TLS
237 * client hello handshake message. The format of the message is the following
238 * (cf RFC5246 + RFC6066) :
239 * TLS frame :
240 * - uint8 type = 0x16 (Handshake)
241 * - uint16 version >= 0x0301 (TLSv1)
242 * - uint16 length (frame length)
243 * - TLS handshake :
244 * - uint8 msg_type = 0x01 (ClientHello)
245 * - uint24 length (handshake message length)
246 * - ClientHello :
247 * - uint16 client_version >= 0x0301 (TLSv1)
248 * - uint8 Random[32] (4 first ones are timestamp)
249 * - SessionID :
250 * - uint8 session_id_len (0..32) (SessionID len in bytes)
251 * - uint8 session_id[session_id_len]
252 * - CipherSuite :
253 * - uint16 cipher_len >= 2 (Cipher length in bytes)
254 * - uint16 ciphers[cipher_len/2]
255 * - CompressionMethod :
256 * - uint8 compression_len >= 1 (# of supported methods)
257 * - uint8 compression_methods[compression_len]
258 * - optional client_extension_len (in bytes)
259 * - optional sequence of ClientHelloExtensions (as many bytes as above):
260 * - uint16 extension_type = 0 for server_name
261 * - uint16 extension_len
262 * - opaque extension_data[extension_len]
263 * - uint16 server_name_list_len (# of bytes here)
264 * - opaque server_names[server_name_list_len bytes]
265 * - uint8 name_type = 0 for host_name
266 * - uint16 name_len
267 * - opaque hostname[name_len bytes]
268 */
269static int
270smp_fetch_ssl_hello_sni(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200271 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100272{
273 int hs_len, ext_len, bleft;
274 struct channel *chn;
275 unsigned char *data;
276
277 if (!s)
278 goto not_ssl_hello;
279
280 chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? s->rep : s->req;
281
Willy Tarreau1e89acb2014-11-26 13:24:24 +0100282 if (!chn)
283 goto not_ssl_hello;
284
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100285 bleft = chn->buf->i;
286 data = (unsigned char *)chn->buf->p;
287
288 /* Check for SSL/TLS Handshake */
289 if (!bleft)
290 goto too_short;
291 if (*data != 0x16)
292 goto not_ssl_hello;
293
Lukas Tribus57d22972014-04-10 21:36:22 +0200294 /* Check for SSLv3 or later (SSL version >= 3.0) in the record layer*/
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100295 if (bleft < 3)
296 goto too_short;
Lukas Tribus57d22972014-04-10 21:36:22 +0200297 if (data[1] < 0x03)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100298 goto not_ssl_hello;
299
300 if (bleft < 5)
301 goto too_short;
302 hs_len = (data[3] << 8) + data[4];
303 if (hs_len < 1 + 3 + 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
304 goto not_ssl_hello; /* too short to have an extension */
305
306 data += 5; /* enter TLS handshake */
307 bleft -= 5;
308
309 /* Check for a complete client hello starting at <data> */
310 if (bleft < 1)
311 goto too_short;
312 if (data[0] != 0x01) /* msg_type = Client Hello */
313 goto not_ssl_hello;
314
315 /* Check the Hello's length */
316 if (bleft < 4)
317 goto too_short;
318 hs_len = (data[1] << 16) + (data[2] << 8) + data[3];
319 if (hs_len < 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
320 goto not_ssl_hello; /* too short to have an extension */
321
322 /* We want the full handshake here */
323 if (bleft < hs_len)
324 goto too_short;
325
326 data += 4;
327 /* Start of the ClientHello message */
328 if (data[0] < 0x03 || data[1] < 0x01) /* TLSv1 minimum */
329 goto not_ssl_hello;
330
331 ext_len = data[34]; /* session_id_len */
332 if (ext_len > 32 || ext_len > (hs_len - 35)) /* check for correct session_id len */
333 goto not_ssl_hello;
334
335 /* Jump to cipher suite */
336 hs_len -= 35 + ext_len;
337 data += 35 + ext_len;
338
339 if (hs_len < 4 || /* minimum one cipher */
340 (ext_len = (data[0] << 8) + data[1]) < 2 || /* minimum 2 bytes for a cipher */
341 ext_len > hs_len)
342 goto not_ssl_hello;
343
344 /* Jump to the compression methods */
345 hs_len -= 2 + ext_len;
346 data += 2 + ext_len;
347
348 if (hs_len < 2 || /* minimum one compression method */
349 data[0] < 1 || data[0] > hs_len) /* minimum 1 bytes for a method */
350 goto not_ssl_hello;
351
352 /* Jump to the extensions */
353 hs_len -= 1 + data[0];
354 data += 1 + data[0];
355
356 if (hs_len < 2 || /* minimum one extension list length */
357 (ext_len = (data[0] << 8) + data[1]) > hs_len - 2) /* list too long */
358 goto not_ssl_hello;
359
360 hs_len = ext_len; /* limit ourselves to the extension length */
361 data += 2;
362
363 while (hs_len >= 4) {
364 int ext_type, name_type, srv_len, name_len;
365
366 ext_type = (data[0] << 8) + data[1];
367 ext_len = (data[2] << 8) + data[3];
368
369 if (ext_len > hs_len - 4) /* Extension too long */
370 goto not_ssl_hello;
371
372 if (ext_type == 0) { /* Server name */
373 if (ext_len < 2) /* need one list length */
374 goto not_ssl_hello;
375
376 srv_len = (data[4] << 8) + data[5];
377 if (srv_len < 4 || srv_len > hs_len - 6)
378 goto not_ssl_hello; /* at least 4 bytes per server name */
379
380 name_type = data[6];
381 name_len = (data[7] << 8) + data[8];
382
383 if (name_type == 0) { /* hostname */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100384 smp->type = SMP_T_STR;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100385 smp->data.str.str = (char *)data + 9;
386 smp->data.str.len = name_len;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100387 smp->flags = SMP_F_VOLATILE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100388 return 1;
389 }
390 }
391
392 hs_len -= 4 + ext_len;
393 data += 4 + ext_len;
394 }
395 /* server name not found */
396 goto not_ssl_hello;
397
398 too_short:
399 smp->flags = SMP_F_MAY_CHANGE;
400
401 not_ssl_hello:
402
403 return 0;
404}
405
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200406/* Fetch the request RDP cookie identified in <cname>:<clen>, or any cookie if
Willy Tarreaub169eba2013-12-16 15:14:43 +0100407 * <clen> is empty (cname is then ignored). It returns the data into sample <smp>
408 * of type SMP_T_CSTR. Note: this decoder only works with non-wrapping data.
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100409 */
410int
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200411fetch_rdp_cookie_name(struct session *s, struct sample *smp, const char *cname, int clen)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100412{
413 int bleft;
414 const unsigned char *data;
415
416 if (!s || !s->req)
417 return 0;
418
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100419 smp->flags = SMP_F_CONST;
420 smp->type = SMP_T_STR;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100421
422 bleft = s->req->buf->i;
423 if (bleft <= 11)
424 goto too_short;
425
426 data = (const unsigned char *)s->req->buf->p + 11;
427 bleft -= 11;
428
429 if (bleft <= 7)
430 goto too_short;
431
432 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
433 goto not_cookie;
434
435 data += 7;
436 bleft -= 7;
437
438 while (bleft > 0 && *data == ' ') {
439 data++;
440 bleft--;
441 }
442
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200443 if (clen) {
444 if (bleft <= clen)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100445 goto too_short;
446
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200447 if ((data[clen] != '=') ||
448 strncasecmp(cname, (const char *)data, clen) != 0)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100449 goto not_cookie;
450
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200451 data += clen + 1;
452 bleft -= clen + 1;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100453 } else {
454 while (bleft > 0 && *data != '=') {
455 if (*data == '\r' || *data == '\n')
456 goto not_cookie;
457 data++;
458 bleft--;
459 }
460
461 if (bleft < 1)
462 goto too_short;
463
464 if (*data != '=')
465 goto not_cookie;
466
467 data++;
468 bleft--;
469 }
470
471 /* data points to cookie value */
472 smp->data.str.str = (char *)data;
473 smp->data.str.len = 0;
474
475 while (bleft > 0 && *data != '\r') {
476 data++;
477 bleft--;
478 }
479
480 if (bleft < 2)
481 goto too_short;
482
483 if (data[0] != '\r' || data[1] != '\n')
484 goto not_cookie;
485
486 smp->data.str.len = (char *)data - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100487 smp->flags = SMP_F_VOLATILE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100488 return 1;
489
490 too_short:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100491 smp->flags = SMP_F_MAY_CHANGE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100492 not_cookie:
493 return 0;
494}
495
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200496/* Fetch the request RDP cookie identified in the args, or any cookie if no arg
497 * is passed. It is usable both for ACL and for samples. Note: this decoder
498 * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument
Willy Tarreaub169eba2013-12-16 15:14:43 +0100499 * is a string (cookie name), other types will lead to undefined behaviour. The
500 * returned sample has type SMP_T_CSTR.
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200501 */
502int
503smp_fetch_rdp_cookie(struct proxy *px, struct session *s, void *l7, unsigned int opt,
504 const struct arg *args, struct sample *smp, const char *kw)
505{
506 return fetch_rdp_cookie_name(s, smp, args ? args->data.str.str : NULL, args ? args->data.str.len : 0);
507}
508
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100509/* returns either 1 or 0 depending on whether an RDP cookie is found or not */
510static int
511smp_fetch_rdp_cookie_cnt(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200512 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100513{
514 int ret;
515
Willy Tarreauef38c392013-07-22 16:29:32 +0200516 ret = smp_fetch_rdp_cookie(px, s, l7, opt, args, smp, kw);
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100517
518 if (smp->flags & SMP_F_MAY_CHANGE)
519 return 0;
520
521 smp->flags = SMP_F_VOLATILE;
522 smp->type = SMP_T_UINT;
523 smp->data.uint = ret;
524 return 1;
525}
526
527/* extracts part of a payload with offset and length at a given position */
528static int
529smp_fetch_payload_lv(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200530 const struct arg *arg_p, struct sample *smp, const char *kw)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100531{
532 unsigned int len_offset = arg_p[0].data.uint;
533 unsigned int len_size = arg_p[1].data.uint;
534 unsigned int buf_offset;
535 unsigned int buf_size = 0;
536 struct channel *chn;
537 int i;
538
539 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
540 /* by default buf offset == len offset + len size */
541 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
542
543 if (!s)
544 return 0;
545
546 chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? s->rep : s->req;
547
548 if (!chn)
549 return 0;
550
551 if (len_offset + len_size > chn->buf->i)
552 goto too_short;
553
554 for (i = 0; i < len_size; i++) {
555 buf_size = (buf_size << 8) + ((unsigned char *)chn->buf->p)[i + len_offset];
556 }
557
558 /* buf offset may be implicit, absolute or relative */
559 buf_offset = len_offset + len_size;
560 if (arg_p[2].type == ARGT_UINT)
561 buf_offset = arg_p[2].data.uint;
562 else if (arg_p[2].type == ARGT_SINT)
563 buf_offset += arg_p[2].data.sint;
564
565 if (!buf_size || buf_size > chn->buf->size || buf_offset + buf_size > chn->buf->size) {
566 /* will never match */
567 smp->flags = 0;
568 return 0;
569 }
570
571 if (buf_offset + buf_size > chn->buf->i)
572 goto too_short;
573
574 /* init chunk as read only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100575 smp->type = SMP_T_BIN;
576 smp->flags = SMP_F_VOLATILE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100577 chunk_initlen(&smp->data.str, chn->buf->p + buf_offset, 0, buf_size);
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100578 return 1;
579
580 too_short:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100581 smp->flags = SMP_F_MAY_CHANGE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100582 return 0;
583}
584
585/* extracts some payload at a fixed position and length */
586static int
587smp_fetch_payload(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200588 const struct arg *arg_p, struct sample *smp, const char *kw)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100589{
590 unsigned int buf_offset = arg_p[0].data.uint;
591 unsigned int buf_size = arg_p[1].data.uint;
592 struct channel *chn;
593
594 if (!s)
595 return 0;
596
597 chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? s->rep : s->req;
598
599 if (!chn)
600 return 0;
601
Willy Tarreau00f00842013-08-02 11:07:32 +0200602 if (buf_size > chn->buf->size || buf_offset + buf_size > chn->buf->size) {
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100603 /* will never match */
604 smp->flags = 0;
605 return 0;
606 }
607
608 if (buf_offset + buf_size > chn->buf->i)
609 goto too_short;
610
611 /* init chunk as read only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100612 smp->type = SMP_T_BIN;
613 smp->flags = SMP_F_VOLATILE | SMP_F_CONST;
Willy Tarreau00f00842013-08-02 11:07:32 +0200614 chunk_initlen(&smp->data.str, chn->buf->p + buf_offset, 0, buf_size ? buf_size : (chn->buf->i - buf_offset));
Willy Tarreau00f00842013-08-02 11:07:32 +0200615 if (!buf_size && !channel_full(chn) && !channel_input_closed(chn))
616 smp->flags |= SMP_F_MAY_CHANGE;
617
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100618 return 1;
619
620 too_short:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100621 smp->flags = SMP_F_MAY_CHANGE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100622 return 0;
623}
624
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100625/* This function is used to validate the arguments passed to a "payload_lv" fetch
626 * keyword. This keyword allows two positive integers and an optional signed one,
627 * with the second one being strictly positive and the third one being greater than
628 * the opposite of the two others if negative. It is assumed that the types are
629 * already the correct ones. Returns 0 on error, non-zero if OK. If <err_msg> is
630 * not NULL, it will be filled with a pointer to an error message in case of
631 * error, that the caller is responsible for freeing. The initial location must
632 * either be freeable or NULL.
633 */
634static int val_payload_lv(struct arg *arg, char **err_msg)
635{
636 if (!arg[1].data.uint) {
637 memprintf(err_msg, "payload length must be > 0");
638 return 0;
639 }
640
641 if (arg[2].type == ARGT_SINT &&
642 (int)(arg[0].data.uint + arg[1].data.uint + arg[2].data.sint) < 0) {
643 memprintf(err_msg, "payload offset too negative");
644 return 0;
645 }
646 return 1;
647}
648
649/************************************************************************/
650/* All supported sample and ACL keywords must be declared here. */
651/************************************************************************/
652
653/* Note: must not be declared <const> as its list will be overwritten.
654 * Note: fetches that may return multiple types must be declared as the lowest
655 * common denominator, the type that can be casted into all other ones. For
656 * instance IPv4/IPv6 must be declared IPv4.
657 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200658static struct sample_fetch_kw_list smp_kws = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100659 { "payload", smp_fetch_payload, ARG2(2,UINT,UINT), NULL, SMP_T_BIN, SMP_USE_L6REQ|SMP_USE_L6RES },
660 { "payload_lv", smp_fetch_payload_lv, ARG3(2,UINT,UINT,SINT), val_payload_lv, SMP_T_BIN, SMP_USE_L6REQ|SMP_USE_L6RES },
661 { "rdp_cookie", smp_fetch_rdp_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_L6REQ },
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100662 { "rdp_cookie_cnt", smp_fetch_rdp_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_L6REQ },
663 { "rep_ssl_hello_type", smp_fetch_ssl_hello_type, 0, NULL, SMP_T_UINT, SMP_USE_L6RES },
Willy Tarreau47e8eba2013-09-11 23:28:46 +0200664 { "req_len", smp_fetch_len, 0, NULL, SMP_T_UINT, SMP_USE_L6REQ },
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100665 { "req_ssl_hello_type", smp_fetch_ssl_hello_type, 0, NULL, SMP_T_UINT, SMP_USE_L6REQ },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100666 { "req_ssl_sni", smp_fetch_ssl_hello_sni, 0, NULL, SMP_T_STR, SMP_USE_L6REQ },
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100667 { "req_ssl_ver", smp_fetch_req_ssl_ver, 0, NULL, SMP_T_UINT, SMP_USE_L6REQ },
Willy Tarreaufa957342013-01-14 16:07:52 +0100668
Willy Tarreau47e8eba2013-09-11 23:28:46 +0200669 { "req.len", smp_fetch_len, 0, NULL, SMP_T_UINT, SMP_USE_L6REQ },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100670 { "req.payload", smp_fetch_payload, ARG2(2,UINT,UINT), NULL, SMP_T_BIN, SMP_USE_L6REQ },
671 { "req.payload_lv", smp_fetch_payload_lv, ARG3(2,UINT,UINT,SINT), val_payload_lv, SMP_T_BIN, SMP_USE_L6REQ },
672 { "req.rdp_cookie", smp_fetch_rdp_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_L6REQ },
Willy Tarreaufa957342013-01-14 16:07:52 +0100673 { "req.rdp_cookie_cnt", smp_fetch_rdp_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_L6REQ },
674 { "req.ssl_hello_type", smp_fetch_ssl_hello_type, 0, NULL, SMP_T_UINT, SMP_USE_L6REQ },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100675 { "req.ssl_sni", smp_fetch_ssl_hello_sni, 0, NULL, SMP_T_STR, SMP_USE_L6REQ },
Willy Tarreaufa957342013-01-14 16:07:52 +0100676 { "req.ssl_ver", smp_fetch_req_ssl_ver, 0, NULL, SMP_T_UINT, SMP_USE_L6REQ },
Willy Tarreau47e8eba2013-09-11 23:28:46 +0200677 { "res.len", smp_fetch_len, 0, NULL, SMP_T_UINT, SMP_USE_L6RES },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100678 { "res.payload", smp_fetch_payload, ARG2(2,UINT,UINT), NULL, SMP_T_BIN, SMP_USE_L6RES },
679 { "res.payload_lv", smp_fetch_payload_lv, ARG3(2,UINT,UINT,SINT), val_payload_lv, SMP_T_BIN, SMP_USE_L6RES },
Willy Tarreaufa957342013-01-14 16:07:52 +0100680 { "res.ssl_hello_type", smp_fetch_ssl_hello_type, 0, NULL, SMP_T_UINT, SMP_USE_L6RES },
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100681 { "wait_end", smp_fetch_wait_end, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
682 { /* END */ },
683}};
684
685
686/* Note: must not be declared <const> as its list will be overwritten.
687 * Please take care of keeping this list alphabetically sorted.
688 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200689static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +0100690 { "payload", "req.payload", PAT_MATCH_BIN },
691 { "payload_lv", "req.payload_lv", PAT_MATCH_BIN },
692 { "req_rdp_cookie", "req.rdp_cookie", PAT_MATCH_STR },
693 { "req_rdp_cookie_cnt", "req.rdp_cookie_cnt", PAT_MATCH_INT },
694 { "req_ssl_sni", "req.ssl_sni", PAT_MATCH_STR },
695 { "req_ssl_ver", "req.ssl_ver", PAT_MATCH_INT, pat_parse_dotted_ver },
696 { "req.ssl_ver", "req.ssl_ver", PAT_MATCH_INT, pat_parse_dotted_ver },
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100697 { /* END */ },
698}};
699
700
701__attribute__((constructor))
702static void __payload_init(void)
703{
704 sample_register_fetches(&smp_kws);
705 acl_register_keywords(&acl_kws);
706}
707
708/*
709 * Local variables:
710 * c-indent-level: 8
711 * c-basic-offset: 8
712 * End:
713 */