blob: a80ac10c637f5c75125f1ccbd5ffd6dcc999eaef [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
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020032smp_fetch_wait_end(unsigned int opt, const struct arg *args, struct sample *smp,
33 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
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020046smp_fetch_len(unsigned int opt, const struct arg *args, struct sample *smp,
47 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
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020051 chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010052 if (!chn->buf)
Willy Tarreaud4c33c82013-01-07 21:59:07 +010053 return 0;
54
55 smp->type = SMP_T_UINT;
Willy Tarreau47e8eba2013-09-11 23:28:46 +020056 smp->data.uint = chn->buf->i;
Willy Tarreaud4c33c82013-01-07 21:59:07 +010057 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
58 return 1;
59}
60
61/* returns the type of SSL hello message (mainly used to detect an SSL hello) */
62static int
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020063smp_fetch_ssl_hello_type(unsigned int opt, const struct arg *args, struct sample *smp,
64 const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +010065{
66 int hs_len;
67 int hs_type, bleft;
68 struct channel *chn;
69 const unsigned char *data;
70
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020071 chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010072 if (!chn->buf)
Willy Tarreau83f25922014-11-26 13:24:24 +010073 goto not_ssl_hello;
74
Willy Tarreaud4c33c82013-01-07 21:59:07 +010075 bleft = chn->buf->i;
76 data = (const unsigned char *)chn->buf->p;
77
78 if (!bleft)
79 goto too_short;
80
81 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
82 /* SSLv3 header format */
83 if (bleft < 9)
84 goto too_short;
85
86 /* ssl version 3 */
87 if ((data[1] << 16) + data[2] < 0x00030000)
88 goto not_ssl_hello;
89
90 /* ssl message len must present handshake type and len */
91 if ((data[3] << 8) + data[4] < 4)
92 goto not_ssl_hello;
93
94 /* format introduced with SSLv3 */
95
96 hs_type = (int)data[5];
97 hs_len = ( data[6] << 16 ) + ( data[7] << 8 ) + data[8];
98
99 /* not a full handshake */
100 if (bleft < (9 + hs_len))
101 goto too_short;
102
103 }
104 else {
105 goto not_ssl_hello;
106 }
107
108 smp->type = SMP_T_UINT;
109 smp->data.uint = hs_type;
110 smp->flags = SMP_F_VOLATILE;
111
112 return 1;
113
114 too_short:
115 smp->flags = SMP_F_MAY_CHANGE;
116
117 not_ssl_hello:
118
119 return 0;
120}
121
122/* Return the version of the SSL protocol in the request. It supports both
123 * SSLv3 (TLSv1) header format for any message, and SSLv2 header format for
124 * the hello message. The SSLv3 format is described in RFC 2246 p49, and the
125 * SSLv2 format is described here, and completed p67 of RFC 2246 :
126 * http://wp.netscape.com/eng/security/SSL_2.html
127 *
128 * Note: this decoder only works with non-wrapping data.
129 */
130static int
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200131smp_fetch_req_ssl_ver(unsigned int opt, const struct arg *args, struct sample *smp,
132 const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100133{
134 int version, bleft, msg_len;
135 const unsigned char *data;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200136 struct channel *req = &smp->strm->req;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100137
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200138 if (!req->buf)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100139 return 0;
140
141 msg_len = 0;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200142 bleft = req->buf->i;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100143 if (!bleft)
144 goto too_short;
145
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200146 data = (const unsigned char *)req->buf->p;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100147 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
148 /* SSLv3 header format */
149 if (bleft < 5)
150 goto too_short;
151
152 version = (data[1] << 16) + data[2]; /* version: major, minor */
153 msg_len = (data[3] << 8) + data[4]; /* record length */
154
155 /* format introduced with SSLv3 */
156 if (version < 0x00030000)
157 goto not_ssl;
158
159 /* message length between 1 and 2^14 + 2048 */
160 if (msg_len < 1 || msg_len > ((1<<14) + 2048))
161 goto not_ssl;
162
163 bleft -= 5; data += 5;
164 } else {
165 /* SSLv2 header format, only supported for hello (msg type 1) */
166 int rlen, plen, cilen, silen, chlen;
167
168 if (*data & 0x80) {
169 if (bleft < 3)
170 goto too_short;
171 /* short header format : 15 bits for length */
172 rlen = ((data[0] & 0x7F) << 8) | data[1];
173 plen = 0;
174 bleft -= 2; data += 2;
175 } else {
176 if (bleft < 4)
177 goto too_short;
178 /* long header format : 14 bits for length + pad length */
179 rlen = ((data[0] & 0x3F) << 8) | data[1];
180 plen = data[2];
181 bleft -= 3; data += 2;
182 }
183
184 if (*data != 0x01)
185 goto not_ssl;
186 bleft--; data++;
187
188 if (bleft < 8)
189 goto too_short;
190 version = (data[0] << 16) + data[1]; /* version: major, minor */
191 cilen = (data[2] << 8) + data[3]; /* cipher len, multiple of 3 */
192 silen = (data[4] << 8) + data[5]; /* session_id_len: 0 or 16 */
193 chlen = (data[6] << 8) + data[7]; /* 16<=challenge length<=32 */
194
195 bleft -= 8; data += 8;
196 if (cilen % 3 != 0)
197 goto not_ssl;
198 if (silen && silen != 16)
199 goto not_ssl;
200 if (chlen < 16 || chlen > 32)
201 goto not_ssl;
202 if (rlen != 9 + cilen + silen + chlen)
203 goto not_ssl;
204
205 /* focus on the remaining data length */
206 msg_len = cilen + silen + chlen + plen;
207 }
208 /* We could recursively check that the buffer ends exactly on an SSL
209 * fragment boundary and that a possible next segment is still SSL,
210 * but that's a bit pointless. However, we could still check that
211 * all the part of the request which fits in a buffer is already
212 * there.
213 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200214 if (msg_len > channel_recv_limit(req) + req->buf->data - req->buf->p)
215 msg_len = channel_recv_limit(req) + req->buf->data - req->buf->p;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100216
217 if (bleft < msg_len)
218 goto too_short;
219
220 /* OK that's enough. We have at least the whole message, and we have
221 * the protocol version.
222 */
223 smp->type = SMP_T_UINT;
224 smp->data.uint = version;
225 smp->flags = SMP_F_VOLATILE;
226 return 1;
227
228 too_short:
229 smp->flags = SMP_F_MAY_CHANGE;
230 not_ssl:
231 return 0;
232}
233
234/* Try to extract the Server Name Indication that may be presented in a TLS
235 * client hello handshake message. The format of the message is the following
236 * (cf RFC5246 + RFC6066) :
237 * TLS frame :
238 * - uint8 type = 0x16 (Handshake)
239 * - uint16 version >= 0x0301 (TLSv1)
240 * - uint16 length (frame length)
241 * - TLS handshake :
242 * - uint8 msg_type = 0x01 (ClientHello)
243 * - uint24 length (handshake message length)
244 * - ClientHello :
245 * - uint16 client_version >= 0x0301 (TLSv1)
246 * - uint8 Random[32] (4 first ones are timestamp)
247 * - SessionID :
248 * - uint8 session_id_len (0..32) (SessionID len in bytes)
249 * - uint8 session_id[session_id_len]
250 * - CipherSuite :
251 * - uint16 cipher_len >= 2 (Cipher length in bytes)
252 * - uint16 ciphers[cipher_len/2]
253 * - CompressionMethod :
254 * - uint8 compression_len >= 1 (# of supported methods)
255 * - uint8 compression_methods[compression_len]
256 * - optional client_extension_len (in bytes)
257 * - optional sequence of ClientHelloExtensions (as many bytes as above):
258 * - uint16 extension_type = 0 for server_name
259 * - uint16 extension_len
260 * - opaque extension_data[extension_len]
261 * - uint16 server_name_list_len (# of bytes here)
262 * - opaque server_names[server_name_list_len bytes]
263 * - uint8 name_type = 0 for host_name
264 * - uint16 name_len
265 * - opaque hostname[name_len bytes]
266 */
267static int
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200268smp_fetch_ssl_hello_sni(unsigned int opt, const struct arg *args, struct sample *smp,
269 const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100270{
271 int hs_len, ext_len, bleft;
272 struct channel *chn;
273 unsigned char *data;
274
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200275 chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100276 if (!chn->buf)
Willy Tarreau83f25922014-11-26 13:24:24 +0100277 goto not_ssl_hello;
278
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100279 bleft = chn->buf->i;
280 data = (unsigned char *)chn->buf->p;
281
282 /* Check for SSL/TLS Handshake */
283 if (!bleft)
284 goto too_short;
285 if (*data != 0x16)
286 goto not_ssl_hello;
287
Lukas Tribus57d22972014-04-10 21:36:22 +0200288 /* Check for SSLv3 or later (SSL version >= 3.0) in the record layer*/
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100289 if (bleft < 3)
290 goto too_short;
Lukas Tribus57d22972014-04-10 21:36:22 +0200291 if (data[1] < 0x03)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100292 goto not_ssl_hello;
293
294 if (bleft < 5)
295 goto too_short;
296 hs_len = (data[3] << 8) + data[4];
297 if (hs_len < 1 + 3 + 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
298 goto not_ssl_hello; /* too short to have an extension */
299
300 data += 5; /* enter TLS handshake */
301 bleft -= 5;
302
303 /* Check for a complete client hello starting at <data> */
304 if (bleft < 1)
305 goto too_short;
306 if (data[0] != 0x01) /* msg_type = Client Hello */
307 goto not_ssl_hello;
308
309 /* Check the Hello's length */
310 if (bleft < 4)
311 goto too_short;
312 hs_len = (data[1] << 16) + (data[2] << 8) + data[3];
313 if (hs_len < 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
314 goto not_ssl_hello; /* too short to have an extension */
315
316 /* We want the full handshake here */
317 if (bleft < hs_len)
318 goto too_short;
319
320 data += 4;
321 /* Start of the ClientHello message */
322 if (data[0] < 0x03 || data[1] < 0x01) /* TLSv1 minimum */
323 goto not_ssl_hello;
324
325 ext_len = data[34]; /* session_id_len */
326 if (ext_len > 32 || ext_len > (hs_len - 35)) /* check for correct session_id len */
327 goto not_ssl_hello;
328
329 /* Jump to cipher suite */
330 hs_len -= 35 + ext_len;
331 data += 35 + ext_len;
332
333 if (hs_len < 4 || /* minimum one cipher */
334 (ext_len = (data[0] << 8) + data[1]) < 2 || /* minimum 2 bytes for a cipher */
335 ext_len > hs_len)
336 goto not_ssl_hello;
337
338 /* Jump to the compression methods */
339 hs_len -= 2 + ext_len;
340 data += 2 + ext_len;
341
342 if (hs_len < 2 || /* minimum one compression method */
343 data[0] < 1 || data[0] > hs_len) /* minimum 1 bytes for a method */
344 goto not_ssl_hello;
345
346 /* Jump to the extensions */
347 hs_len -= 1 + data[0];
348 data += 1 + data[0];
349
350 if (hs_len < 2 || /* minimum one extension list length */
351 (ext_len = (data[0] << 8) + data[1]) > hs_len - 2) /* list too long */
352 goto not_ssl_hello;
353
354 hs_len = ext_len; /* limit ourselves to the extension length */
355 data += 2;
356
357 while (hs_len >= 4) {
358 int ext_type, name_type, srv_len, name_len;
359
360 ext_type = (data[0] << 8) + data[1];
361 ext_len = (data[2] << 8) + data[3];
362
363 if (ext_len > hs_len - 4) /* Extension too long */
364 goto not_ssl_hello;
365
366 if (ext_type == 0) { /* Server name */
367 if (ext_len < 2) /* need one list length */
368 goto not_ssl_hello;
369
370 srv_len = (data[4] << 8) + data[5];
371 if (srv_len < 4 || srv_len > hs_len - 6)
372 goto not_ssl_hello; /* at least 4 bytes per server name */
373
374 name_type = data[6];
375 name_len = (data[7] << 8) + data[8];
376
377 if (name_type == 0) { /* hostname */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100378 smp->type = SMP_T_STR;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100379 smp->data.str.str = (char *)data + 9;
380 smp->data.str.len = name_len;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100381 smp->flags = SMP_F_VOLATILE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100382 return 1;
383 }
384 }
385
386 hs_len -= 4 + ext_len;
387 data += 4 + ext_len;
388 }
389 /* server name not found */
390 goto not_ssl_hello;
391
392 too_short:
393 smp->flags = SMP_F_MAY_CHANGE;
394
395 not_ssl_hello:
396
397 return 0;
398}
399
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200400/* Fetch the request RDP cookie identified in <cname>:<clen>, or any cookie if
Willy Tarreaub169eba2013-12-16 15:14:43 +0100401 * <clen> is empty (cname is then ignored). It returns the data into sample <smp>
402 * of type SMP_T_CSTR. Note: this decoder only works with non-wrapping data.
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100403 */
404int
Willy Tarreau87b09662015-04-03 00:22:06 +0200405fetch_rdp_cookie_name(struct stream *s, struct sample *smp, const char *cname, int clen)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100406{
407 int bleft;
408 const unsigned char *data;
409
Willy Tarreau53c9b4d2015-04-03 21:38:18 +0200410 if (!s->req.buf)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100411 return 0;
412
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100413 smp->flags = SMP_F_CONST;
414 smp->type = SMP_T_STR;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100415
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100416 bleft = s->req.buf->i;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100417 if (bleft <= 11)
418 goto too_short;
419
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100420 data = (const unsigned char *)s->req.buf->p + 11;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100421 bleft -= 11;
422
423 if (bleft <= 7)
424 goto too_short;
425
426 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
427 goto not_cookie;
428
429 data += 7;
430 bleft -= 7;
431
432 while (bleft > 0 && *data == ' ') {
433 data++;
434 bleft--;
435 }
436
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200437 if (clen) {
438 if (bleft <= clen)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100439 goto too_short;
440
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200441 if ((data[clen] != '=') ||
442 strncasecmp(cname, (const char *)data, clen) != 0)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100443 goto not_cookie;
444
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200445 data += clen + 1;
446 bleft -= clen + 1;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100447 } else {
448 while (bleft > 0 && *data != '=') {
449 if (*data == '\r' || *data == '\n')
450 goto not_cookie;
451 data++;
452 bleft--;
453 }
454
455 if (bleft < 1)
456 goto too_short;
457
458 if (*data != '=')
459 goto not_cookie;
460
461 data++;
462 bleft--;
463 }
464
465 /* data points to cookie value */
466 smp->data.str.str = (char *)data;
467 smp->data.str.len = 0;
468
469 while (bleft > 0 && *data != '\r') {
470 data++;
471 bleft--;
472 }
473
474 if (bleft < 2)
475 goto too_short;
476
477 if (data[0] != '\r' || data[1] != '\n')
478 goto not_cookie;
479
480 smp->data.str.len = (char *)data - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100481 smp->flags = SMP_F_VOLATILE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100482 return 1;
483
484 too_short:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100485 smp->flags = SMP_F_MAY_CHANGE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100486 not_cookie:
487 return 0;
488}
489
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200490/* Fetch the request RDP cookie identified in the args, or any cookie if no arg
491 * is passed. It is usable both for ACL and for samples. Note: this decoder
492 * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument
Willy Tarreaub169eba2013-12-16 15:14:43 +0100493 * is a string (cookie name), other types will lead to undefined behaviour. The
494 * returned sample has type SMP_T_CSTR.
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200495 */
496int
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200497smp_fetch_rdp_cookie(unsigned int opt, const struct arg *args, struct sample *smp,
498 const char *kw, void *private)
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200499{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200500 return fetch_rdp_cookie_name(smp->strm, smp, args ? args->data.str.str : NULL, args ? args->data.str.len : 0);
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200501}
502
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100503/* returns either 1 or 0 depending on whether an RDP cookie is found or not */
504static int
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200505smp_fetch_rdp_cookie_cnt(unsigned int opt, const struct arg *args, struct sample *smp,
506 const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100507{
508 int ret;
509
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200510 ret = smp_fetch_rdp_cookie(opt, args, smp, kw, private);
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100511
512 if (smp->flags & SMP_F_MAY_CHANGE)
513 return 0;
514
515 smp->flags = SMP_F_VOLATILE;
516 smp->type = SMP_T_UINT;
517 smp->data.uint = ret;
518 return 1;
519}
520
521/* extracts part of a payload with offset and length at a given position */
522static int
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200523smp_fetch_payload_lv(unsigned int opt, const struct arg *arg_p, struct sample *smp,
524 const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100525{
526 unsigned int len_offset = arg_p[0].data.uint;
527 unsigned int len_size = arg_p[1].data.uint;
528 unsigned int buf_offset;
529 unsigned int buf_size = 0;
530 struct channel *chn;
531 int i;
532
533 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
534 /* by default buf offset == len offset + len size */
535 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
536
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200537 chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100538 if (!chn->buf)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100539 return 0;
540
541 if (len_offset + len_size > chn->buf->i)
542 goto too_short;
543
544 for (i = 0; i < len_size; i++) {
545 buf_size = (buf_size << 8) + ((unsigned char *)chn->buf->p)[i + len_offset];
546 }
547
548 /* buf offset may be implicit, absolute or relative */
549 buf_offset = len_offset + len_size;
550 if (arg_p[2].type == ARGT_UINT)
551 buf_offset = arg_p[2].data.uint;
552 else if (arg_p[2].type == ARGT_SINT)
553 buf_offset += arg_p[2].data.sint;
554
555 if (!buf_size || buf_size > chn->buf->size || buf_offset + buf_size > chn->buf->size) {
556 /* will never match */
557 smp->flags = 0;
558 return 0;
559 }
560
561 if (buf_offset + buf_size > chn->buf->i)
562 goto too_short;
563
564 /* init chunk as read only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100565 smp->type = SMP_T_BIN;
566 smp->flags = SMP_F_VOLATILE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100567 chunk_initlen(&smp->data.str, chn->buf->p + buf_offset, 0, buf_size);
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100568 return 1;
569
570 too_short:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100571 smp->flags = SMP_F_MAY_CHANGE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100572 return 0;
573}
574
575/* extracts some payload at a fixed position and length */
576static int
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200577smp_fetch_payload(unsigned int opt, const struct arg *arg_p, struct sample *smp,
578 const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100579{
580 unsigned int buf_offset = arg_p[0].data.uint;
581 unsigned int buf_size = arg_p[1].data.uint;
582 struct channel *chn;
583
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200584 chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100585 if (!chn->buf)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100586 return 0;
587
Willy Tarreau00f00842013-08-02 11:07:32 +0200588 if (buf_size > chn->buf->size || buf_offset + buf_size > chn->buf->size) {
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100589 /* will never match */
590 smp->flags = 0;
591 return 0;
592 }
593
594 if (buf_offset + buf_size > chn->buf->i)
595 goto too_short;
596
597 /* init chunk as read only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100598 smp->type = SMP_T_BIN;
599 smp->flags = SMP_F_VOLATILE | SMP_F_CONST;
Willy Tarreau00f00842013-08-02 11:07:32 +0200600 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 +0100601 if (!buf_size && channel_may_recv(chn) && !channel_input_closed(chn))
Willy Tarreau00f00842013-08-02 11:07:32 +0200602 smp->flags |= SMP_F_MAY_CHANGE;
603
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100604 return 1;
605
606 too_short:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100607 smp->flags = SMP_F_MAY_CHANGE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100608 return 0;
609}
610
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100611/* This function is used to validate the arguments passed to a "payload_lv" fetch
612 * keyword. This keyword allows two positive integers and an optional signed one,
613 * with the second one being strictly positive and the third one being greater than
614 * the opposite of the two others if negative. It is assumed that the types are
615 * already the correct ones. Returns 0 on error, non-zero if OK. If <err_msg> is
616 * not NULL, it will be filled with a pointer to an error message in case of
617 * error, that the caller is responsible for freeing. The initial location must
618 * either be freeable or NULL.
619 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +0100620int val_payload_lv(struct arg *arg, char **err_msg)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100621{
622 if (!arg[1].data.uint) {
623 memprintf(err_msg, "payload length must be > 0");
624 return 0;
625 }
626
627 if (arg[2].type == ARGT_SINT &&
628 (int)(arg[0].data.uint + arg[1].data.uint + arg[2].data.sint) < 0) {
629 memprintf(err_msg, "payload offset too negative");
630 return 0;
631 }
632 return 1;
633}
634
635/************************************************************************/
636/* All supported sample and ACL keywords must be declared here. */
637/************************************************************************/
638
639/* Note: must not be declared <const> as its list will be overwritten.
640 * Note: fetches that may return multiple types must be declared as the lowest
641 * common denominator, the type that can be casted into all other ones. For
642 * instance IPv4/IPv6 must be declared IPv4.
643 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200644static struct sample_fetch_kw_list smp_kws = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100645 { "payload", smp_fetch_payload, ARG2(2,UINT,UINT), NULL, SMP_T_BIN, SMP_USE_L6REQ|SMP_USE_L6RES },
646 { "payload_lv", smp_fetch_payload_lv, ARG3(2,UINT,UINT,SINT), val_payload_lv, SMP_T_BIN, SMP_USE_L6REQ|SMP_USE_L6RES },
647 { "rdp_cookie", smp_fetch_rdp_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_L6REQ },
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100648 { "rdp_cookie_cnt", smp_fetch_rdp_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_L6REQ },
649 { "rep_ssl_hello_type", smp_fetch_ssl_hello_type, 0, NULL, SMP_T_UINT, SMP_USE_L6RES },
Willy Tarreau47e8eba2013-09-11 23:28:46 +0200650 { "req_len", smp_fetch_len, 0, NULL, SMP_T_UINT, SMP_USE_L6REQ },
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100651 { "req_ssl_hello_type", smp_fetch_ssl_hello_type, 0, NULL, SMP_T_UINT, SMP_USE_L6REQ },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100652 { "req_ssl_sni", smp_fetch_ssl_hello_sni, 0, NULL, SMP_T_STR, SMP_USE_L6REQ },
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100653 { "req_ssl_ver", smp_fetch_req_ssl_ver, 0, NULL, SMP_T_UINT, SMP_USE_L6REQ },
Willy Tarreaufa957342013-01-14 16:07:52 +0100654
Willy Tarreau47e8eba2013-09-11 23:28:46 +0200655 { "req.len", smp_fetch_len, 0, NULL, SMP_T_UINT, SMP_USE_L6REQ },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100656 { "req.payload", smp_fetch_payload, ARG2(2,UINT,UINT), NULL, SMP_T_BIN, SMP_USE_L6REQ },
657 { "req.payload_lv", smp_fetch_payload_lv, ARG3(2,UINT,UINT,SINT), val_payload_lv, SMP_T_BIN, SMP_USE_L6REQ },
658 { "req.rdp_cookie", smp_fetch_rdp_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_L6REQ },
Willy Tarreaufa957342013-01-14 16:07:52 +0100659 { "req.rdp_cookie_cnt", smp_fetch_rdp_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_L6REQ },
660 { "req.ssl_hello_type", smp_fetch_ssl_hello_type, 0, NULL, SMP_T_UINT, SMP_USE_L6REQ },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100661 { "req.ssl_sni", smp_fetch_ssl_hello_sni, 0, NULL, SMP_T_STR, SMP_USE_L6REQ },
Willy Tarreaufa957342013-01-14 16:07:52 +0100662 { "req.ssl_ver", smp_fetch_req_ssl_ver, 0, NULL, SMP_T_UINT, SMP_USE_L6REQ },
Willy Tarreau47e8eba2013-09-11 23:28:46 +0200663 { "res.len", smp_fetch_len, 0, NULL, SMP_T_UINT, SMP_USE_L6RES },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100664 { "res.payload", smp_fetch_payload, ARG2(2,UINT,UINT), NULL, SMP_T_BIN, SMP_USE_L6RES },
665 { "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 +0100666 { "res.ssl_hello_type", smp_fetch_ssl_hello_type, 0, NULL, SMP_T_UINT, SMP_USE_L6RES },
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100667 { "wait_end", smp_fetch_wait_end, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
668 { /* END */ },
669}};
670
671
672/* Note: must not be declared <const> as its list will be overwritten.
673 * Please take care of keeping this list alphabetically sorted.
674 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200675static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +0100676 { "payload", "req.payload", PAT_MATCH_BIN },
677 { "payload_lv", "req.payload_lv", PAT_MATCH_BIN },
678 { "req_rdp_cookie", "req.rdp_cookie", PAT_MATCH_STR },
679 { "req_rdp_cookie_cnt", "req.rdp_cookie_cnt", PAT_MATCH_INT },
680 { "req_ssl_sni", "req.ssl_sni", PAT_MATCH_STR },
681 { "req_ssl_ver", "req.ssl_ver", PAT_MATCH_INT, pat_parse_dotted_ver },
682 { "req.ssl_ver", "req.ssl_ver", PAT_MATCH_INT, pat_parse_dotted_ver },
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100683 { /* END */ },
684}};
685
686
687__attribute__((constructor))
688static void __payload_init(void)
689{
690 sample_register_fetches(&smp_kws);
691 acl_register_keywords(&acl_kws);
692}
693
694/*
695 * Local variables:
696 * c-indent-level: 8
697 * c-basic-offset: 8
698 * End:
699 */