blob: 78f56089c2fb917b8e907eabb20a0057f21d9c8f [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 FOURNIER0786d052015-05-11 15:42:45 +020032smp_fetch_wait_end(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +010033{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020034 if (!(smp->opt & SMP_OPT_FINAL)) {
Willy Tarreaud4c33c82013-01-07 21:59:07 +010035 smp->flags |= SMP_F_MAY_CHANGE;
36 return 0;
37 }
38 smp->type = SMP_T_BOOL;
39 smp->data.uint = 1;
40 return 1;
41}
42
43/* return the number of bytes in the request buffer */
44static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020045smp_fetch_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +010046{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010047 struct channel *chn;
48
Thierry FOURNIER0786d052015-05-11 15:42:45 +020049 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010050 if (!chn->buf)
Willy Tarreaud4c33c82013-01-07 21:59:07 +010051 return 0;
52
53 smp->type = SMP_T_UINT;
Willy Tarreau47e8eba2013-09-11 23:28:46 +020054 smp->data.uint = chn->buf->i;
Willy Tarreaud4c33c82013-01-07 21:59:07 +010055 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
56 return 1;
57}
58
Nenad Merdanovic5fc7d7e2015-07-07 22:00:17 +020059/* Returns TRUE if the client sent Supported Elliptic Curves Extension (0x000a)
60 * Mainly used to detect if client supports ECC cipher suites.
61 */
62static int
63smp_fetch_req_ssl_ec_ext(const struct arg *args, struct sample *smp, const char *kw, void *private)
64{
65 int hs_len, ext_len, bleft;
66 struct channel *chn;
67 unsigned char *data;
68
69 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
70 if (!chn->buf)
71 goto not_ssl_hello;
72
73 bleft = chn->buf->i;
74 data = (unsigned char *)chn->buf->p;
75
76 /* Check for SSL/TLS Handshake */
77 if (!bleft)
78 goto too_short;
79 if (*data != 0x16)
80 goto not_ssl_hello;
81
82 /* Check for SSLv3 or later (SSL version >= 3.0) in the record layer*/
83 if (bleft < 3)
84 goto too_short;
85 if (data[1] < 0x03)
86 goto not_ssl_hello;
87
88 if (bleft < 5)
89 goto too_short;
90 hs_len = (data[3] << 8) + data[4];
91 if (hs_len < 1 + 3 + 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
92 goto not_ssl_hello; /* too short to have an extension */
93
94 data += 5; /* enter TLS handshake */
95 bleft -= 5;
96
97 /* Check for a complete client hello starting at <data> */
98 if (bleft < 1)
99 goto too_short;
100 if (data[0] != 0x01) /* msg_type = Client Hello */
101 goto not_ssl_hello;
102
103 /* Check the Hello's length */
104 if (bleft < 4)
105 goto too_short;
106 hs_len = (data[1] << 16) + (data[2] << 8) + data[3];
107 if (hs_len < 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
108 goto not_ssl_hello; /* too short to have an extension */
109
110 /* We want the full handshake here */
111 if (bleft < hs_len)
112 goto too_short;
113
114 data += 4;
115 /* Start of the ClientHello message */
116 if (data[0] < 0x03 || data[1] < 0x01) /* TLSv1 minimum */
117 goto not_ssl_hello;
118
119 ext_len = data[34]; /* session_id_len */
120 if (ext_len > 32 || ext_len > (hs_len - 35)) /* check for correct session_id len */
121 goto not_ssl_hello;
122
123 /* Jump to cipher suite */
124 hs_len -= 35 + ext_len;
125 data += 35 + ext_len;
126
127 if (hs_len < 4 || /* minimum one cipher */
128 (ext_len = (data[0] << 8) + data[1]) < 2 || /* minimum 2 bytes for a cipher */
129 ext_len > hs_len)
130 goto not_ssl_hello;
131
132 /* Jump to the compression methods */
133 hs_len -= 2 + ext_len;
134 data += 2 + ext_len;
135
136 if (hs_len < 2 || /* minimum one compression method */
137 data[0] < 1 || data[0] > hs_len) /* minimum 1 bytes for a method */
138 goto not_ssl_hello;
139
140 /* Jump to the extensions */
141 hs_len -= 1 + data[0];
142 data += 1 + data[0];
143
144 if (hs_len < 2 || /* minimum one extension list length */
145 (ext_len = (data[0] << 8) + data[1]) > hs_len - 2) /* list too long */
146 goto not_ssl_hello;
147
148 hs_len = ext_len; /* limit ourselves to the extension length */
149 data += 2;
150
151 while (hs_len >= 4) {
152 int ext_type, ext_len;
153
154 ext_type = (data[0] << 8) + data[1];
155 ext_len = (data[2] << 8) + data[3];
156
157 if (ext_len > hs_len - 4) /* Extension too long */
158 goto not_ssl_hello;
159
160 /* Elliptic curves extension */
161 if (ext_type == 10) {
162 smp->type = SMP_T_BOOL;
163 smp->data.uint = 1;
164 return 1;
165 }
166
167 hs_len -= 4 + ext_len;
168 data += 4 + ext_len;
169 }
170 /* server name not found */
171 goto not_ssl_hello;
172
173 too_short:
174 smp->flags = SMP_F_MAY_CHANGE;
175
176 not_ssl_hello:
177
178 return 0;
179}
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100180/* returns the type of SSL hello message (mainly used to detect an SSL hello) */
181static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200182smp_fetch_ssl_hello_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100183{
184 int hs_len;
185 int hs_type, bleft;
186 struct channel *chn;
187 const unsigned char *data;
188
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200189 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100190 if (!chn->buf)
Willy Tarreau83f25922014-11-26 13:24:24 +0100191 goto not_ssl_hello;
192
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100193 bleft = chn->buf->i;
194 data = (const unsigned char *)chn->buf->p;
195
196 if (!bleft)
197 goto too_short;
198
199 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
200 /* SSLv3 header format */
201 if (bleft < 9)
202 goto too_short;
203
204 /* ssl version 3 */
205 if ((data[1] << 16) + data[2] < 0x00030000)
206 goto not_ssl_hello;
207
208 /* ssl message len must present handshake type and len */
209 if ((data[3] << 8) + data[4] < 4)
210 goto not_ssl_hello;
211
212 /* format introduced with SSLv3 */
213
214 hs_type = (int)data[5];
215 hs_len = ( data[6] << 16 ) + ( data[7] << 8 ) + data[8];
216
217 /* not a full handshake */
218 if (bleft < (9 + hs_len))
219 goto too_short;
220
221 }
222 else {
223 goto not_ssl_hello;
224 }
225
226 smp->type = SMP_T_UINT;
227 smp->data.uint = hs_type;
228 smp->flags = SMP_F_VOLATILE;
229
230 return 1;
231
232 too_short:
233 smp->flags = SMP_F_MAY_CHANGE;
234
235 not_ssl_hello:
236
237 return 0;
238}
239
240/* Return the version of the SSL protocol in the request. It supports both
241 * SSLv3 (TLSv1) header format for any message, and SSLv2 header format for
242 * the hello message. The SSLv3 format is described in RFC 2246 p49, and the
243 * SSLv2 format is described here, and completed p67 of RFC 2246 :
244 * http://wp.netscape.com/eng/security/SSL_2.html
245 *
246 * Note: this decoder only works with non-wrapping data.
247 */
248static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200249smp_fetch_req_ssl_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100250{
251 int version, bleft, msg_len;
252 const unsigned char *data;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200253 struct channel *req = &smp->strm->req;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100254
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200255 if (!req->buf)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100256 return 0;
257
258 msg_len = 0;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200259 bleft = req->buf->i;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100260 if (!bleft)
261 goto too_short;
262
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200263 data = (const unsigned char *)req->buf->p;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100264 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
265 /* SSLv3 header format */
266 if (bleft < 5)
267 goto too_short;
268
269 version = (data[1] << 16) + data[2]; /* version: major, minor */
270 msg_len = (data[3] << 8) + data[4]; /* record length */
271
272 /* format introduced with SSLv3 */
273 if (version < 0x00030000)
274 goto not_ssl;
275
276 /* message length between 1 and 2^14 + 2048 */
277 if (msg_len < 1 || msg_len > ((1<<14) + 2048))
278 goto not_ssl;
279
280 bleft -= 5; data += 5;
281 } else {
282 /* SSLv2 header format, only supported for hello (msg type 1) */
283 int rlen, plen, cilen, silen, chlen;
284
285 if (*data & 0x80) {
286 if (bleft < 3)
287 goto too_short;
288 /* short header format : 15 bits for length */
289 rlen = ((data[0] & 0x7F) << 8) | data[1];
290 plen = 0;
291 bleft -= 2; data += 2;
292 } else {
293 if (bleft < 4)
294 goto too_short;
295 /* long header format : 14 bits for length + pad length */
296 rlen = ((data[0] & 0x3F) << 8) | data[1];
297 plen = data[2];
298 bleft -= 3; data += 2;
299 }
300
301 if (*data != 0x01)
302 goto not_ssl;
303 bleft--; data++;
304
305 if (bleft < 8)
306 goto too_short;
307 version = (data[0] << 16) + data[1]; /* version: major, minor */
308 cilen = (data[2] << 8) + data[3]; /* cipher len, multiple of 3 */
309 silen = (data[4] << 8) + data[5]; /* session_id_len: 0 or 16 */
310 chlen = (data[6] << 8) + data[7]; /* 16<=challenge length<=32 */
311
312 bleft -= 8; data += 8;
313 if (cilen % 3 != 0)
314 goto not_ssl;
315 if (silen && silen != 16)
316 goto not_ssl;
317 if (chlen < 16 || chlen > 32)
318 goto not_ssl;
319 if (rlen != 9 + cilen + silen + chlen)
320 goto not_ssl;
321
322 /* focus on the remaining data length */
323 msg_len = cilen + silen + chlen + plen;
324 }
325 /* We could recursively check that the buffer ends exactly on an SSL
326 * fragment boundary and that a possible next segment is still SSL,
327 * but that's a bit pointless. However, we could still check that
328 * all the part of the request which fits in a buffer is already
329 * there.
330 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200331 if (msg_len > channel_recv_limit(req) + req->buf->data - req->buf->p)
332 msg_len = channel_recv_limit(req) + req->buf->data - req->buf->p;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100333
334 if (bleft < msg_len)
335 goto too_short;
336
337 /* OK that's enough. We have at least the whole message, and we have
338 * the protocol version.
339 */
340 smp->type = SMP_T_UINT;
341 smp->data.uint = version;
342 smp->flags = SMP_F_VOLATILE;
343 return 1;
344
345 too_short:
346 smp->flags = SMP_F_MAY_CHANGE;
347 not_ssl:
348 return 0;
349}
350
351/* Try to extract the Server Name Indication that may be presented in a TLS
352 * client hello handshake message. The format of the message is the following
353 * (cf RFC5246 + RFC6066) :
354 * TLS frame :
355 * - uint8 type = 0x16 (Handshake)
356 * - uint16 version >= 0x0301 (TLSv1)
357 * - uint16 length (frame length)
358 * - TLS handshake :
359 * - uint8 msg_type = 0x01 (ClientHello)
360 * - uint24 length (handshake message length)
361 * - ClientHello :
362 * - uint16 client_version >= 0x0301 (TLSv1)
363 * - uint8 Random[32] (4 first ones are timestamp)
364 * - SessionID :
365 * - uint8 session_id_len (0..32) (SessionID len in bytes)
366 * - uint8 session_id[session_id_len]
367 * - CipherSuite :
368 * - uint16 cipher_len >= 2 (Cipher length in bytes)
369 * - uint16 ciphers[cipher_len/2]
370 * - CompressionMethod :
371 * - uint8 compression_len >= 1 (# of supported methods)
372 * - uint8 compression_methods[compression_len]
373 * - optional client_extension_len (in bytes)
374 * - optional sequence of ClientHelloExtensions (as many bytes as above):
375 * - uint16 extension_type = 0 for server_name
376 * - uint16 extension_len
377 * - opaque extension_data[extension_len]
378 * - uint16 server_name_list_len (# of bytes here)
379 * - opaque server_names[server_name_list_len bytes]
380 * - uint8 name_type = 0 for host_name
381 * - uint16 name_len
382 * - opaque hostname[name_len bytes]
383 */
384static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200385smp_fetch_ssl_hello_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100386{
387 int hs_len, ext_len, bleft;
388 struct channel *chn;
389 unsigned char *data;
390
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200391 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100392 if (!chn->buf)
Willy Tarreau83f25922014-11-26 13:24:24 +0100393 goto not_ssl_hello;
394
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100395 bleft = chn->buf->i;
396 data = (unsigned char *)chn->buf->p;
397
398 /* Check for SSL/TLS Handshake */
399 if (!bleft)
400 goto too_short;
401 if (*data != 0x16)
402 goto not_ssl_hello;
403
Lukas Tribus57d22972014-04-10 21:36:22 +0200404 /* Check for SSLv3 or later (SSL version >= 3.0) in the record layer*/
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100405 if (bleft < 3)
406 goto too_short;
Lukas Tribus57d22972014-04-10 21:36:22 +0200407 if (data[1] < 0x03)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100408 goto not_ssl_hello;
409
410 if (bleft < 5)
411 goto too_short;
412 hs_len = (data[3] << 8) + data[4];
413 if (hs_len < 1 + 3 + 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
414 goto not_ssl_hello; /* too short to have an extension */
415
416 data += 5; /* enter TLS handshake */
417 bleft -= 5;
418
419 /* Check for a complete client hello starting at <data> */
420 if (bleft < 1)
421 goto too_short;
422 if (data[0] != 0x01) /* msg_type = Client Hello */
423 goto not_ssl_hello;
424
425 /* Check the Hello's length */
426 if (bleft < 4)
427 goto too_short;
428 hs_len = (data[1] << 16) + (data[2] << 8) + data[3];
429 if (hs_len < 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
430 goto not_ssl_hello; /* too short to have an extension */
431
432 /* We want the full handshake here */
433 if (bleft < hs_len)
434 goto too_short;
435
436 data += 4;
437 /* Start of the ClientHello message */
438 if (data[0] < 0x03 || data[1] < 0x01) /* TLSv1 minimum */
439 goto not_ssl_hello;
440
441 ext_len = data[34]; /* session_id_len */
442 if (ext_len > 32 || ext_len > (hs_len - 35)) /* check for correct session_id len */
443 goto not_ssl_hello;
444
445 /* Jump to cipher suite */
446 hs_len -= 35 + ext_len;
447 data += 35 + ext_len;
448
449 if (hs_len < 4 || /* minimum one cipher */
450 (ext_len = (data[0] << 8) + data[1]) < 2 || /* minimum 2 bytes for a cipher */
451 ext_len > hs_len)
452 goto not_ssl_hello;
453
454 /* Jump to the compression methods */
455 hs_len -= 2 + ext_len;
456 data += 2 + ext_len;
457
458 if (hs_len < 2 || /* minimum one compression method */
459 data[0] < 1 || data[0] > hs_len) /* minimum 1 bytes for a method */
460 goto not_ssl_hello;
461
462 /* Jump to the extensions */
463 hs_len -= 1 + data[0];
464 data += 1 + data[0];
465
466 if (hs_len < 2 || /* minimum one extension list length */
467 (ext_len = (data[0] << 8) + data[1]) > hs_len - 2) /* list too long */
468 goto not_ssl_hello;
469
470 hs_len = ext_len; /* limit ourselves to the extension length */
471 data += 2;
472
473 while (hs_len >= 4) {
474 int ext_type, name_type, srv_len, name_len;
475
476 ext_type = (data[0] << 8) + data[1];
477 ext_len = (data[2] << 8) + data[3];
478
479 if (ext_len > hs_len - 4) /* Extension too long */
480 goto not_ssl_hello;
481
482 if (ext_type == 0) { /* Server name */
483 if (ext_len < 2) /* need one list length */
484 goto not_ssl_hello;
485
486 srv_len = (data[4] << 8) + data[5];
487 if (srv_len < 4 || srv_len > hs_len - 6)
488 goto not_ssl_hello; /* at least 4 bytes per server name */
489
490 name_type = data[6];
491 name_len = (data[7] << 8) + data[8];
492
493 if (name_type == 0) { /* hostname */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100494 smp->type = SMP_T_STR;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100495 smp->data.str.str = (char *)data + 9;
496 smp->data.str.len = name_len;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100497 smp->flags = SMP_F_VOLATILE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100498 return 1;
499 }
500 }
501
502 hs_len -= 4 + ext_len;
503 data += 4 + ext_len;
504 }
505 /* server name not found */
506 goto not_ssl_hello;
507
508 too_short:
509 smp->flags = SMP_F_MAY_CHANGE;
510
511 not_ssl_hello:
512
513 return 0;
514}
515
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200516/* Fetch the request RDP cookie identified in <cname>:<clen>, or any cookie if
Willy Tarreaub169eba2013-12-16 15:14:43 +0100517 * <clen> is empty (cname is then ignored). It returns the data into sample <smp>
518 * of type SMP_T_CSTR. Note: this decoder only works with non-wrapping data.
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100519 */
520int
Willy Tarreau87b09662015-04-03 00:22:06 +0200521fetch_rdp_cookie_name(struct stream *s, struct sample *smp, const char *cname, int clen)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100522{
523 int bleft;
524 const unsigned char *data;
525
Willy Tarreau53c9b4d2015-04-03 21:38:18 +0200526 if (!s->req.buf)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100527 return 0;
528
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100529 smp->flags = SMP_F_CONST;
530 smp->type = SMP_T_STR;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100531
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100532 bleft = s->req.buf->i;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100533 if (bleft <= 11)
534 goto too_short;
535
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100536 data = (const unsigned char *)s->req.buf->p + 11;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100537 bleft -= 11;
538
539 if (bleft <= 7)
540 goto too_short;
541
542 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
543 goto not_cookie;
544
545 data += 7;
546 bleft -= 7;
547
548 while (bleft > 0 && *data == ' ') {
549 data++;
550 bleft--;
551 }
552
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200553 if (clen) {
554 if (bleft <= clen)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100555 goto too_short;
556
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200557 if ((data[clen] != '=') ||
558 strncasecmp(cname, (const char *)data, clen) != 0)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100559 goto not_cookie;
560
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200561 data += clen + 1;
562 bleft -= clen + 1;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100563 } else {
564 while (bleft > 0 && *data != '=') {
565 if (*data == '\r' || *data == '\n')
566 goto not_cookie;
567 data++;
568 bleft--;
569 }
570
571 if (bleft < 1)
572 goto too_short;
573
574 if (*data != '=')
575 goto not_cookie;
576
577 data++;
578 bleft--;
579 }
580
581 /* data points to cookie value */
582 smp->data.str.str = (char *)data;
583 smp->data.str.len = 0;
584
585 while (bleft > 0 && *data != '\r') {
586 data++;
587 bleft--;
588 }
589
590 if (bleft < 2)
591 goto too_short;
592
593 if (data[0] != '\r' || data[1] != '\n')
594 goto not_cookie;
595
596 smp->data.str.len = (char *)data - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100597 smp->flags = SMP_F_VOLATILE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100598 return 1;
599
600 too_short:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100601 smp->flags = SMP_F_MAY_CHANGE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100602 not_cookie:
603 return 0;
604}
605
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200606/* Fetch the request RDP cookie identified in the args, or any cookie if no arg
607 * is passed. It is usable both for ACL and for samples. Note: this decoder
608 * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument
Willy Tarreaub169eba2013-12-16 15:14:43 +0100609 * is a string (cookie name), other types will lead to undefined behaviour. The
610 * returned sample has type SMP_T_CSTR.
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200611 */
612int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200613smp_fetch_rdp_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200614{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200615 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 +0200616}
617
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100618/* returns either 1 or 0 depending on whether an RDP cookie is found or not */
619static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200620smp_fetch_rdp_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100621{
622 int ret;
623
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200624 ret = smp_fetch_rdp_cookie(args, smp, kw, private);
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100625
626 if (smp->flags & SMP_F_MAY_CHANGE)
627 return 0;
628
629 smp->flags = SMP_F_VOLATILE;
630 smp->type = SMP_T_UINT;
631 smp->data.uint = ret;
632 return 1;
633}
634
635/* extracts part of a payload with offset and length at a given position */
636static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200637smp_fetch_payload_lv(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100638{
639 unsigned int len_offset = arg_p[0].data.uint;
640 unsigned int len_size = arg_p[1].data.uint;
641 unsigned int buf_offset;
642 unsigned int buf_size = 0;
643 struct channel *chn;
644 int i;
645
646 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
647 /* by default buf offset == len offset + len size */
648 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
649
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200650 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100651 if (!chn->buf)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100652 return 0;
653
654 if (len_offset + len_size > chn->buf->i)
655 goto too_short;
656
657 for (i = 0; i < len_size; i++) {
658 buf_size = (buf_size << 8) + ((unsigned char *)chn->buf->p)[i + len_offset];
659 }
660
661 /* buf offset may be implicit, absolute or relative */
662 buf_offset = len_offset + len_size;
663 if (arg_p[2].type == ARGT_UINT)
664 buf_offset = arg_p[2].data.uint;
665 else if (arg_p[2].type == ARGT_SINT)
666 buf_offset += arg_p[2].data.sint;
667
668 if (!buf_size || buf_size > chn->buf->size || buf_offset + buf_size > chn->buf->size) {
669 /* will never match */
670 smp->flags = 0;
671 return 0;
672 }
673
674 if (buf_offset + buf_size > chn->buf->i)
675 goto too_short;
676
677 /* init chunk as read only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100678 smp->type = SMP_T_BIN;
679 smp->flags = SMP_F_VOLATILE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100680 chunk_initlen(&smp->data.str, chn->buf->p + buf_offset, 0, buf_size);
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100681 return 1;
682
683 too_short:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100684 smp->flags = SMP_F_MAY_CHANGE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100685 return 0;
686}
687
688/* extracts some payload at a fixed position and length */
689static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200690smp_fetch_payload(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100691{
692 unsigned int buf_offset = arg_p[0].data.uint;
693 unsigned int buf_size = arg_p[1].data.uint;
694 struct channel *chn;
695
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200696 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100697 if (!chn->buf)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100698 return 0;
699
Willy Tarreau00f00842013-08-02 11:07:32 +0200700 if (buf_size > chn->buf->size || buf_offset + buf_size > chn->buf->size) {
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100701 /* will never match */
702 smp->flags = 0;
703 return 0;
704 }
705
706 if (buf_offset + buf_size > chn->buf->i)
707 goto too_short;
708
709 /* init chunk as read only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100710 smp->type = SMP_T_BIN;
711 smp->flags = SMP_F_VOLATILE | SMP_F_CONST;
Willy Tarreau00f00842013-08-02 11:07:32 +0200712 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 +0100713 if (!buf_size && channel_may_recv(chn) && !channel_input_closed(chn))
Willy Tarreau00f00842013-08-02 11:07:32 +0200714 smp->flags |= SMP_F_MAY_CHANGE;
715
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100716 return 1;
717
718 too_short:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100719 smp->flags = SMP_F_MAY_CHANGE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100720 return 0;
721}
722
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100723/* This function is used to validate the arguments passed to a "payload_lv" fetch
724 * keyword. This keyword allows two positive integers and an optional signed one,
725 * with the second one being strictly positive and the third one being greater than
726 * the opposite of the two others if negative. It is assumed that the types are
727 * already the correct ones. Returns 0 on error, non-zero if OK. If <err_msg> is
728 * not NULL, it will be filled with a pointer to an error message in case of
729 * error, that the caller is responsible for freeing. The initial location must
730 * either be freeable or NULL.
731 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +0100732int val_payload_lv(struct arg *arg, char **err_msg)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100733{
734 if (!arg[1].data.uint) {
735 memprintf(err_msg, "payload length must be > 0");
736 return 0;
737 }
738
739 if (arg[2].type == ARGT_SINT &&
740 (int)(arg[0].data.uint + arg[1].data.uint + arg[2].data.sint) < 0) {
741 memprintf(err_msg, "payload offset too negative");
742 return 0;
743 }
744 return 1;
745}
746
747/************************************************************************/
748/* All supported sample and ACL keywords must be declared here. */
749/************************************************************************/
750
751/* Note: must not be declared <const> as its list will be overwritten.
752 * Note: fetches that may return multiple types must be declared as the lowest
753 * common denominator, the type that can be casted into all other ones. For
754 * instance IPv4/IPv6 must be declared IPv4.
755 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200756static struct sample_fetch_kw_list smp_kws = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100757 { "payload", smp_fetch_payload, ARG2(2,UINT,UINT), NULL, SMP_T_BIN, SMP_USE_L6REQ|SMP_USE_L6RES },
758 { "payload_lv", smp_fetch_payload_lv, ARG3(2,UINT,UINT,SINT), val_payload_lv, SMP_T_BIN, SMP_USE_L6REQ|SMP_USE_L6RES },
759 { "rdp_cookie", smp_fetch_rdp_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_L6REQ },
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100760 { "rdp_cookie_cnt", smp_fetch_rdp_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_L6REQ },
761 { "rep_ssl_hello_type", smp_fetch_ssl_hello_type, 0, NULL, SMP_T_UINT, SMP_USE_L6RES },
Willy Tarreau47e8eba2013-09-11 23:28:46 +0200762 { "req_len", smp_fetch_len, 0, NULL, SMP_T_UINT, SMP_USE_L6REQ },
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100763 { "req_ssl_hello_type", smp_fetch_ssl_hello_type, 0, NULL, SMP_T_UINT, SMP_USE_L6REQ },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100764 { "req_ssl_sni", smp_fetch_ssl_hello_sni, 0, NULL, SMP_T_STR, SMP_USE_L6REQ },
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100765 { "req_ssl_ver", smp_fetch_req_ssl_ver, 0, NULL, SMP_T_UINT, SMP_USE_L6REQ },
Willy Tarreaufa957342013-01-14 16:07:52 +0100766
Willy Tarreau47e8eba2013-09-11 23:28:46 +0200767 { "req.len", smp_fetch_len, 0, NULL, SMP_T_UINT, SMP_USE_L6REQ },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100768 { "req.payload", smp_fetch_payload, ARG2(2,UINT,UINT), NULL, SMP_T_BIN, SMP_USE_L6REQ },
769 { "req.payload_lv", smp_fetch_payload_lv, ARG3(2,UINT,UINT,SINT), val_payload_lv, SMP_T_BIN, SMP_USE_L6REQ },
770 { "req.rdp_cookie", smp_fetch_rdp_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_L6REQ },
Willy Tarreaufa957342013-01-14 16:07:52 +0100771 { "req.rdp_cookie_cnt", smp_fetch_rdp_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_L6REQ },
Nenad Merdanovic5fc7d7e2015-07-07 22:00:17 +0200772 { "req.ssl_ec_ext", smp_fetch_req_ssl_ec_ext, 0, NULL, SMP_T_BOOL, SMP_USE_L6REQ },
Willy Tarreaufa957342013-01-14 16:07:52 +0100773 { "req.ssl_hello_type", smp_fetch_ssl_hello_type, 0, NULL, SMP_T_UINT, SMP_USE_L6REQ },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100774 { "req.ssl_sni", smp_fetch_ssl_hello_sni, 0, NULL, SMP_T_STR, SMP_USE_L6REQ },
Willy Tarreaufa957342013-01-14 16:07:52 +0100775 { "req.ssl_ver", smp_fetch_req_ssl_ver, 0, NULL, SMP_T_UINT, SMP_USE_L6REQ },
Willy Tarreau47e8eba2013-09-11 23:28:46 +0200776 { "res.len", smp_fetch_len, 0, NULL, SMP_T_UINT, SMP_USE_L6RES },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100777 { "res.payload", smp_fetch_payload, ARG2(2,UINT,UINT), NULL, SMP_T_BIN, SMP_USE_L6RES },
778 { "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 +0100779 { "res.ssl_hello_type", smp_fetch_ssl_hello_type, 0, NULL, SMP_T_UINT, SMP_USE_L6RES },
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100780 { "wait_end", smp_fetch_wait_end, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
781 { /* END */ },
782}};
783
784
785/* Note: must not be declared <const> as its list will be overwritten.
786 * Please take care of keeping this list alphabetically sorted.
787 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200788static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +0100789 { "payload", "req.payload", PAT_MATCH_BIN },
790 { "payload_lv", "req.payload_lv", PAT_MATCH_BIN },
791 { "req_rdp_cookie", "req.rdp_cookie", PAT_MATCH_STR },
792 { "req_rdp_cookie_cnt", "req.rdp_cookie_cnt", PAT_MATCH_INT },
793 { "req_ssl_sni", "req.ssl_sni", PAT_MATCH_STR },
794 { "req_ssl_ver", "req.ssl_ver", PAT_MATCH_INT, pat_parse_dotted_ver },
795 { "req.ssl_ver", "req.ssl_ver", PAT_MATCH_INT, pat_parse_dotted_ver },
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100796 { /* END */ },
797}};
798
799
800__attribute__((constructor))
801static void __payload_init(void)
802{
803 sample_register_fetches(&smp_kws);
804 acl_register_keywords(&acl_kws);
805}
806
807/*
808 * Local variables:
809 * c-indent-level: 8
810 * c-basic-offset: 8
811 * End:
812 */