blob: b5d8478bab98c6aa5c6bbc0af4ba27974fe59133 [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,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010033 const struct arg *args, struct sample *smp, const char *kw, void *private)
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,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010047 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +010048{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010049 struct channel *chn;
50
51 if (!s)
52 return 0;
Willy Tarreau47e8eba2013-09-11 23:28:46 +020053
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010054 chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &s->res : &s->req;
55 if (!chn->buf)
Willy Tarreaud4c33c82013-01-07 21:59:07 +010056 return 0;
57
58 smp->type = SMP_T_UINT;
Willy Tarreau47e8eba2013-09-11 23:28:46 +020059 smp->data.uint = chn->buf->i;
Willy Tarreaud4c33c82013-01-07 21:59:07 +010060 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
61 return 1;
62}
63
64/* returns the type of SSL hello message (mainly used to detect an SSL hello) */
65static int
66smp_fetch_ssl_hello_type(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010067 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +010068{
69 int hs_len;
70 int hs_type, bleft;
71 struct channel *chn;
72 const unsigned char *data;
73
74 if (!s)
75 goto not_ssl_hello;
76
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010077 chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &s->res : &s->req;
78 if (!chn->buf)
Willy Tarreau83f25922014-11-26 13:24:24 +010079 goto not_ssl_hello;
80
Willy Tarreaud4c33c82013-01-07 21:59:07 +010081 bleft = chn->buf->i;
82 data = (const unsigned char *)chn->buf->p;
83
84 if (!bleft)
85 goto too_short;
86
87 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
88 /* SSLv3 header format */
89 if (bleft < 9)
90 goto too_short;
91
92 /* ssl version 3 */
93 if ((data[1] << 16) + data[2] < 0x00030000)
94 goto not_ssl_hello;
95
96 /* ssl message len must present handshake type and len */
97 if ((data[3] << 8) + data[4] < 4)
98 goto not_ssl_hello;
99
100 /* format introduced with SSLv3 */
101
102 hs_type = (int)data[5];
103 hs_len = ( data[6] << 16 ) + ( data[7] << 8 ) + data[8];
104
105 /* not a full handshake */
106 if (bleft < (9 + hs_len))
107 goto too_short;
108
109 }
110 else {
111 goto not_ssl_hello;
112 }
113
114 smp->type = SMP_T_UINT;
115 smp->data.uint = hs_type;
116 smp->flags = SMP_F_VOLATILE;
117
118 return 1;
119
120 too_short:
121 smp->flags = SMP_F_MAY_CHANGE;
122
123 not_ssl_hello:
124
125 return 0;
126}
127
128/* Return the version of the SSL protocol in the request. It supports both
129 * SSLv3 (TLSv1) header format for any message, and SSLv2 header format for
130 * the hello message. The SSLv3 format is described in RFC 2246 p49, and the
131 * SSLv2 format is described here, and completed p67 of RFC 2246 :
132 * http://wp.netscape.com/eng/security/SSL_2.html
133 *
134 * Note: this decoder only works with non-wrapping data.
135 */
136static int
137smp_fetch_req_ssl_ver(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +0100138 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100139{
140 int version, bleft, msg_len;
141 const unsigned char *data;
142
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100143 if (!s || !s->req.buf)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100144 return 0;
145
146 msg_len = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100147 bleft = s->req.buf->i;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100148 if (!bleft)
149 goto too_short;
150
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100151 data = (const unsigned char *)s->req.buf->p;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100152 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
153 /* SSLv3 header format */
154 if (bleft < 5)
155 goto too_short;
156
157 version = (data[1] << 16) + data[2]; /* version: major, minor */
158 msg_len = (data[3] << 8) + data[4]; /* record length */
159
160 /* format introduced with SSLv3 */
161 if (version < 0x00030000)
162 goto not_ssl;
163
164 /* message length between 1 and 2^14 + 2048 */
165 if (msg_len < 1 || msg_len > ((1<<14) + 2048))
166 goto not_ssl;
167
168 bleft -= 5; data += 5;
169 } else {
170 /* SSLv2 header format, only supported for hello (msg type 1) */
171 int rlen, plen, cilen, silen, chlen;
172
173 if (*data & 0x80) {
174 if (bleft < 3)
175 goto too_short;
176 /* short header format : 15 bits for length */
177 rlen = ((data[0] & 0x7F) << 8) | data[1];
178 plen = 0;
179 bleft -= 2; data += 2;
180 } else {
181 if (bleft < 4)
182 goto too_short;
183 /* long header format : 14 bits for length + pad length */
184 rlen = ((data[0] & 0x3F) << 8) | data[1];
185 plen = data[2];
186 bleft -= 3; data += 2;
187 }
188
189 if (*data != 0x01)
190 goto not_ssl;
191 bleft--; data++;
192
193 if (bleft < 8)
194 goto too_short;
195 version = (data[0] << 16) + data[1]; /* version: major, minor */
196 cilen = (data[2] << 8) + data[3]; /* cipher len, multiple of 3 */
197 silen = (data[4] << 8) + data[5]; /* session_id_len: 0 or 16 */
198 chlen = (data[6] << 8) + data[7]; /* 16<=challenge length<=32 */
199
200 bleft -= 8; data += 8;
201 if (cilen % 3 != 0)
202 goto not_ssl;
203 if (silen && silen != 16)
204 goto not_ssl;
205 if (chlen < 16 || chlen > 32)
206 goto not_ssl;
207 if (rlen != 9 + cilen + silen + chlen)
208 goto not_ssl;
209
210 /* focus on the remaining data length */
211 msg_len = cilen + silen + chlen + plen;
212 }
213 /* We could recursively check that the buffer ends exactly on an SSL
214 * fragment boundary and that a possible next segment is still SSL,
215 * but that's a bit pointless. However, we could still check that
216 * all the part of the request which fits in a buffer is already
217 * there.
218 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100219 if (msg_len > channel_recv_limit(&s->req) + s->req.buf->data - s->req.buf->p)
220 msg_len = channel_recv_limit(&s->req) + s->req.buf->data - s->req.buf->p;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100221
222 if (bleft < msg_len)
223 goto too_short;
224
225 /* OK that's enough. We have at least the whole message, and we have
226 * the protocol version.
227 */
228 smp->type = SMP_T_UINT;
229 smp->data.uint = version;
230 smp->flags = SMP_F_VOLATILE;
231 return 1;
232
233 too_short:
234 smp->flags = SMP_F_MAY_CHANGE;
235 not_ssl:
236 return 0;
237}
238
239/* Try to extract the Server Name Indication that may be presented in a TLS
240 * client hello handshake message. The format of the message is the following
241 * (cf RFC5246 + RFC6066) :
242 * TLS frame :
243 * - uint8 type = 0x16 (Handshake)
244 * - uint16 version >= 0x0301 (TLSv1)
245 * - uint16 length (frame length)
246 * - TLS handshake :
247 * - uint8 msg_type = 0x01 (ClientHello)
248 * - uint24 length (handshake message length)
249 * - ClientHello :
250 * - uint16 client_version >= 0x0301 (TLSv1)
251 * - uint8 Random[32] (4 first ones are timestamp)
252 * - SessionID :
253 * - uint8 session_id_len (0..32) (SessionID len in bytes)
254 * - uint8 session_id[session_id_len]
255 * - CipherSuite :
256 * - uint16 cipher_len >= 2 (Cipher length in bytes)
257 * - uint16 ciphers[cipher_len/2]
258 * - CompressionMethod :
259 * - uint8 compression_len >= 1 (# of supported methods)
260 * - uint8 compression_methods[compression_len]
261 * - optional client_extension_len (in bytes)
262 * - optional sequence of ClientHelloExtensions (as many bytes as above):
263 * - uint16 extension_type = 0 for server_name
264 * - uint16 extension_len
265 * - opaque extension_data[extension_len]
266 * - uint16 server_name_list_len (# of bytes here)
267 * - opaque server_names[server_name_list_len bytes]
268 * - uint8 name_type = 0 for host_name
269 * - uint16 name_len
270 * - opaque hostname[name_len bytes]
271 */
272static int
273smp_fetch_ssl_hello_sni(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +0100274 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100275{
276 int hs_len, ext_len, bleft;
277 struct channel *chn;
278 unsigned char *data;
279
280 if (!s)
281 goto not_ssl_hello;
282
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100283 chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &s->res : &s->req;
284 if (!chn->buf)
Willy Tarreau83f25922014-11-26 13:24:24 +0100285 goto not_ssl_hello;
286
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100287 bleft = chn->buf->i;
288 data = (unsigned char *)chn->buf->p;
289
290 /* Check for SSL/TLS Handshake */
291 if (!bleft)
292 goto too_short;
293 if (*data != 0x16)
294 goto not_ssl_hello;
295
Lukas Tribus57d22972014-04-10 21:36:22 +0200296 /* Check for SSLv3 or later (SSL version >= 3.0) in the record layer*/
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100297 if (bleft < 3)
298 goto too_short;
Lukas Tribus57d22972014-04-10 21:36:22 +0200299 if (data[1] < 0x03)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100300 goto not_ssl_hello;
301
302 if (bleft < 5)
303 goto too_short;
304 hs_len = (data[3] << 8) + data[4];
305 if (hs_len < 1 + 3 + 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
306 goto not_ssl_hello; /* too short to have an extension */
307
308 data += 5; /* enter TLS handshake */
309 bleft -= 5;
310
311 /* Check for a complete client hello starting at <data> */
312 if (bleft < 1)
313 goto too_short;
314 if (data[0] != 0x01) /* msg_type = Client Hello */
315 goto not_ssl_hello;
316
317 /* Check the Hello's length */
318 if (bleft < 4)
319 goto too_short;
320 hs_len = (data[1] << 16) + (data[2] << 8) + data[3];
321 if (hs_len < 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
322 goto not_ssl_hello; /* too short to have an extension */
323
324 /* We want the full handshake here */
325 if (bleft < hs_len)
326 goto too_short;
327
328 data += 4;
329 /* Start of the ClientHello message */
330 if (data[0] < 0x03 || data[1] < 0x01) /* TLSv1 minimum */
331 goto not_ssl_hello;
332
333 ext_len = data[34]; /* session_id_len */
334 if (ext_len > 32 || ext_len > (hs_len - 35)) /* check for correct session_id len */
335 goto not_ssl_hello;
336
337 /* Jump to cipher suite */
338 hs_len -= 35 + ext_len;
339 data += 35 + ext_len;
340
341 if (hs_len < 4 || /* minimum one cipher */
342 (ext_len = (data[0] << 8) + data[1]) < 2 || /* minimum 2 bytes for a cipher */
343 ext_len > hs_len)
344 goto not_ssl_hello;
345
346 /* Jump to the compression methods */
347 hs_len -= 2 + ext_len;
348 data += 2 + ext_len;
349
350 if (hs_len < 2 || /* minimum one compression method */
351 data[0] < 1 || data[0] > hs_len) /* minimum 1 bytes for a method */
352 goto not_ssl_hello;
353
354 /* Jump to the extensions */
355 hs_len -= 1 + data[0];
356 data += 1 + data[0];
357
358 if (hs_len < 2 || /* minimum one extension list length */
359 (ext_len = (data[0] << 8) + data[1]) > hs_len - 2) /* list too long */
360 goto not_ssl_hello;
361
362 hs_len = ext_len; /* limit ourselves to the extension length */
363 data += 2;
364
365 while (hs_len >= 4) {
366 int ext_type, name_type, srv_len, name_len;
367
368 ext_type = (data[0] << 8) + data[1];
369 ext_len = (data[2] << 8) + data[3];
370
371 if (ext_len > hs_len - 4) /* Extension too long */
372 goto not_ssl_hello;
373
374 if (ext_type == 0) { /* Server name */
375 if (ext_len < 2) /* need one list length */
376 goto not_ssl_hello;
377
378 srv_len = (data[4] << 8) + data[5];
379 if (srv_len < 4 || srv_len > hs_len - 6)
380 goto not_ssl_hello; /* at least 4 bytes per server name */
381
382 name_type = data[6];
383 name_len = (data[7] << 8) + data[8];
384
385 if (name_type == 0) { /* hostname */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100386 smp->type = SMP_T_STR;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100387 smp->data.str.str = (char *)data + 9;
388 smp->data.str.len = name_len;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100389 smp->flags = SMP_F_VOLATILE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100390 return 1;
391 }
392 }
393
394 hs_len -= 4 + ext_len;
395 data += 4 + ext_len;
396 }
397 /* server name not found */
398 goto not_ssl_hello;
399
400 too_short:
401 smp->flags = SMP_F_MAY_CHANGE;
402
403 not_ssl_hello:
404
405 return 0;
406}
407
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200408/* Fetch the request RDP cookie identified in <cname>:<clen>, or any cookie if
Willy Tarreaub169eba2013-12-16 15:14:43 +0100409 * <clen> is empty (cname is then ignored). It returns the data into sample <smp>
410 * of type SMP_T_CSTR. Note: this decoder only works with non-wrapping data.
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100411 */
412int
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200413fetch_rdp_cookie_name(struct session *s, struct sample *smp, const char *cname, int clen)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100414{
415 int bleft;
416 const unsigned char *data;
417
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100418 if (!s || !s->req.buf)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100419 return 0;
420
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100421 smp->flags = SMP_F_CONST;
422 smp->type = SMP_T_STR;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100423
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100424 bleft = s->req.buf->i;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100425 if (bleft <= 11)
426 goto too_short;
427
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100428 data = (const unsigned char *)s->req.buf->p + 11;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100429 bleft -= 11;
430
431 if (bleft <= 7)
432 goto too_short;
433
434 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
435 goto not_cookie;
436
437 data += 7;
438 bleft -= 7;
439
440 while (bleft > 0 && *data == ' ') {
441 data++;
442 bleft--;
443 }
444
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200445 if (clen) {
446 if (bleft <= clen)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100447 goto too_short;
448
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200449 if ((data[clen] != '=') ||
450 strncasecmp(cname, (const char *)data, clen) != 0)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100451 goto not_cookie;
452
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200453 data += clen + 1;
454 bleft -= clen + 1;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100455 } else {
456 while (bleft > 0 && *data != '=') {
457 if (*data == '\r' || *data == '\n')
458 goto not_cookie;
459 data++;
460 bleft--;
461 }
462
463 if (bleft < 1)
464 goto too_short;
465
466 if (*data != '=')
467 goto not_cookie;
468
469 data++;
470 bleft--;
471 }
472
473 /* data points to cookie value */
474 smp->data.str.str = (char *)data;
475 smp->data.str.len = 0;
476
477 while (bleft > 0 && *data != '\r') {
478 data++;
479 bleft--;
480 }
481
482 if (bleft < 2)
483 goto too_short;
484
485 if (data[0] != '\r' || data[1] != '\n')
486 goto not_cookie;
487
488 smp->data.str.len = (char *)data - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100489 smp->flags = SMP_F_VOLATILE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100490 return 1;
491
492 too_short:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100493 smp->flags = SMP_F_MAY_CHANGE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100494 not_cookie:
495 return 0;
496}
497
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200498/* Fetch the request RDP cookie identified in the args, or any cookie if no arg
499 * is passed. It is usable both for ACL and for samples. Note: this decoder
500 * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument
Willy Tarreaub169eba2013-12-16 15:14:43 +0100501 * is a string (cookie name), other types will lead to undefined behaviour. The
502 * returned sample has type SMP_T_CSTR.
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200503 */
504int
505smp_fetch_rdp_cookie(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +0100506 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200507{
508 return fetch_rdp_cookie_name(s, smp, args ? args->data.str.str : NULL, args ? args->data.str.len : 0);
509}
510
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100511/* returns either 1 or 0 depending on whether an RDP cookie is found or not */
512static int
513smp_fetch_rdp_cookie_cnt(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +0100514 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100515{
516 int ret;
517
Thierry FOURNIERf41a8092014-12-07 18:37:57 +0100518 ret = smp_fetch_rdp_cookie(px, s, l7, opt, args, smp, kw, private);
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100519
520 if (smp->flags & SMP_F_MAY_CHANGE)
521 return 0;
522
523 smp->flags = SMP_F_VOLATILE;
524 smp->type = SMP_T_UINT;
525 smp->data.uint = ret;
526 return 1;
527}
528
529/* extracts part of a payload with offset and length at a given position */
530static int
531smp_fetch_payload_lv(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +0100532 const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100533{
534 unsigned int len_offset = arg_p[0].data.uint;
535 unsigned int len_size = arg_p[1].data.uint;
536 unsigned int buf_offset;
537 unsigned int buf_size = 0;
538 struct channel *chn;
539 int i;
540
541 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
542 /* by default buf offset == len offset + len size */
543 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
544
545 if (!s)
546 return 0;
547
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100548 chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &s->res : &s->req;
549 if (!chn->buf)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100550 return 0;
551
552 if (len_offset + len_size > chn->buf->i)
553 goto too_short;
554
555 for (i = 0; i < len_size; i++) {
556 buf_size = (buf_size << 8) + ((unsigned char *)chn->buf->p)[i + len_offset];
557 }
558
559 /* buf offset may be implicit, absolute or relative */
560 buf_offset = len_offset + len_size;
561 if (arg_p[2].type == ARGT_UINT)
562 buf_offset = arg_p[2].data.uint;
563 else if (arg_p[2].type == ARGT_SINT)
564 buf_offset += arg_p[2].data.sint;
565
566 if (!buf_size || buf_size > chn->buf->size || buf_offset + buf_size > chn->buf->size) {
567 /* will never match */
568 smp->flags = 0;
569 return 0;
570 }
571
572 if (buf_offset + buf_size > chn->buf->i)
573 goto too_short;
574
575 /* init chunk as read only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100576 smp->type = SMP_T_BIN;
577 smp->flags = SMP_F_VOLATILE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100578 chunk_initlen(&smp->data.str, chn->buf->p + buf_offset, 0, buf_size);
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100579 return 1;
580
581 too_short:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100582 smp->flags = SMP_F_MAY_CHANGE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100583 return 0;
584}
585
586/* extracts some payload at a fixed position and length */
587static int
588smp_fetch_payload(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +0100589 const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100590{
591 unsigned int buf_offset = arg_p[0].data.uint;
592 unsigned int buf_size = arg_p[1].data.uint;
593 struct channel *chn;
594
595 if (!s)
596 return 0;
597
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100598 chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &s->res : &s->req;
599 if (!chn->buf)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100600 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 Tarreau3889fff2015-01-13 20:20:10 +0100615 if (!buf_size && channel_may_recv(chn) && !channel_input_closed(chn))
Willy Tarreau00f00842013-08-02 11:07:32 +0200616 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 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +0100634int val_payload_lv(struct arg *arg, char **err_msg)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100635{
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 */