blob: 7ef6d97e224f389c6012b5bf499a1465046aa680 [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
Willy Tarreau0108d902018-11-25 19:14:37 +010016#include <common/initcall.h>
Willy Tarreaud716f9b2017-10-13 11:03:15 +020017#include <common/net_helper.h>
Christopher Fauletf0216da2018-12-14 13:44:53 +010018#include <common/htx.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010019#include <proto/acl.h>
20#include <proto/arg.h>
21#include <proto/channel.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010022#include <proto/pattern.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010023#include <proto/payload.h>
24#include <proto/sample.h>
Christopher Fauletf0216da2018-12-14 13:44:53 +010025#include <proto/proto_http.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010026
27
28/************************************************************************/
29/* All supported sample fetch functions must be declared here */
30/************************************************************************/
31
32/* wait for more data as long as possible, then return TRUE. This should be
33 * used with content inspection.
34 */
35static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020036smp_fetch_wait_end(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +010037{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020038 if (!(smp->opt & SMP_OPT_FINAL)) {
Willy Tarreaud4c33c82013-01-07 21:59:07 +010039 smp->flags |= SMP_F_MAY_CHANGE;
40 return 0;
41 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020042 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020043 smp->data.u.sint = 1;
Willy Tarreaud4c33c82013-01-07 21:59:07 +010044 return 1;
45}
46
47/* return the number of bytes in the request buffer */
48static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020049smp_fetch_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +010050{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010051 struct channel *chn;
52
Willy Tarreaube508f12016-03-10 11:47:01 +010053 if (!smp->strm)
54 return 0;
55
Thierry FOURNIER0786d052015-05-11 15:42:45 +020056 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020057 smp->data.type = SMP_T_SINT;
Christopher Fauletf0216da2018-12-14 13:44:53 +010058 if (IS_HTX_SMP(smp)) {
59 struct htx *htx = htxbuf(&chn->buf);
60 smp->data.u.sint = htx->data - co_data(chn);
61 }
62 else
63 smp->data.u.sint = ci_data(chn);
Willy Tarreaud4c33c82013-01-07 21:59:07 +010064 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
65 return 1;
66}
67
Pradeep Jindalbb2acf52015-09-29 10:12:57 +053068/* Returns 0 if the client didn't send a SessionTicket Extension
69 * Returns 1 if the client sent SessionTicket Extension
70 * Returns 2 if the client also sent non-zero length SessionTicket
71 * Returns SMP_T_SINT data type
72 */
73static int
74smp_fetch_req_ssl_st_ext(const struct arg *args, struct sample *smp, const char *kw, void *private)
75{
76 int hs_len, ext_len, bleft;
77 struct channel *chn;
78 unsigned char *data;
79
Willy Tarreaube508f12016-03-10 11:47:01 +010080 if (!smp->strm)
81 goto not_ssl_hello;
82
Pradeep Jindalbb2acf52015-09-29 10:12:57 +053083 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Willy Tarreaufc0785d2018-06-19 07:19:56 +020084 bleft = ci_data(chn);
85 data = (unsigned char *)ci_head(chn);
Pradeep Jindalbb2acf52015-09-29 10:12:57 +053086
87 /* Check for SSL/TLS Handshake */
88 if (!bleft)
89 goto too_short;
90 if (*data != 0x16)
91 goto not_ssl_hello;
92
93 /* Check for SSLv3 or later (SSL version >= 3.0) in the record layer*/
94 if (bleft < 3)
95 goto too_short;
96 if (data[1] < 0x03)
97 goto not_ssl_hello;
98
99 if (bleft < 5)
100 goto too_short;
101 hs_len = (data[3] << 8) + data[4];
102 if (hs_len < 1 + 3 + 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
103 goto not_ssl_hello; /* too short to have an extension */
104
105 data += 5; /* enter TLS handshake */
106 bleft -= 5;
107
108 /* Check for a complete client hello starting at <data> */
109 if (bleft < 1)
110 goto too_short;
111 if (data[0] != 0x01) /* msg_type = Client Hello */
112 goto not_ssl_hello;
113
114 /* Check the Hello's length */
115 if (bleft < 4)
116 goto too_short;
117 hs_len = (data[1] << 16) + (data[2] << 8) + data[3];
118 if (hs_len < 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
119 goto not_ssl_hello; /* too short to have an extension */
120
121 /* We want the full handshake here */
122 if (bleft < hs_len)
123 goto too_short;
124
125 data += 4;
126 /* Start of the ClientHello message */
127 if (data[0] < 0x03 || data[1] < 0x01) /* TLSv1 minimum */
128 goto not_ssl_hello;
129
130 ext_len = data[34]; /* session_id_len */
131 if (ext_len > 32 || ext_len > (hs_len - 35)) /* check for correct session_id len */
132 goto not_ssl_hello;
133
134 /* Jump to cipher suite */
135 hs_len -= 35 + ext_len;
136 data += 35 + ext_len;
137
138 if (hs_len < 4 || /* minimum one cipher */
139 (ext_len = (data[0] << 8) + data[1]) < 2 || /* minimum 2 bytes for a cipher */
140 ext_len > hs_len)
141 goto not_ssl_hello;
142
143 /* Jump to the compression methods */
144 hs_len -= 2 + ext_len;
145 data += 2 + ext_len;
146
147 if (hs_len < 2 || /* minimum one compression method */
148 data[0] < 1 || data[0] > hs_len) /* minimum 1 bytes for a method */
149 goto not_ssl_hello;
150
151 /* Jump to the extensions */
152 hs_len -= 1 + data[0];
153 data += 1 + data[0];
154
155 if (hs_len < 2 || /* minimum one extension list length */
156 (ext_len = (data[0] << 8) + data[1]) > hs_len - 2) /* list too long */
157 goto not_ssl_hello;
158
159 hs_len = ext_len; /* limit ourselves to the extension length */
160 data += 2;
161
162 while (hs_len >= 4) {
163 int ext_type, ext_len;
164
165 ext_type = (data[0] << 8) + data[1];
166 ext_len = (data[2] << 8) + data[3];
167
168 if (ext_len > hs_len - 4) /* Extension too long */
169 goto not_ssl_hello;
170
171 /* SesstionTicket extension */
172 if (ext_type == 35) {
173 smp->data.type = SMP_T_SINT;
174 /* SessionTicket also present */
175 if (ext_len > 0)
176 smp->data.u.sint = 2;
177 /* SessionTicket absent */
178 else
179 smp->data.u.sint = 1;
180 smp->flags = SMP_F_VOLATILE;
181 return 1;
182 }
183
184 hs_len -= 4 + ext_len;
185 data += 4 + ext_len;
186 }
187 /* SessionTicket Extension not found */
188 smp->data.type = SMP_T_SINT;
189 smp->data.u.sint = 0;
190 smp->flags = SMP_F_VOLATILE;
191 return 1;
192
Pradeep Jindalbb2acf52015-09-29 10:12:57 +0530193 too_short:
194 smp->flags = SMP_F_MAY_CHANGE;
195
196 not_ssl_hello:
197 return 0;
198}
199
Nenad Merdanovic5fc7d7e2015-07-07 22:00:17 +0200200/* Returns TRUE if the client sent Supported Elliptic Curves Extension (0x000a)
201 * Mainly used to detect if client supports ECC cipher suites.
202 */
203static int
204smp_fetch_req_ssl_ec_ext(const struct arg *args, struct sample *smp, const char *kw, void *private)
205{
206 int hs_len, ext_len, bleft;
207 struct channel *chn;
208 unsigned char *data;
209
Willy Tarreaube508f12016-03-10 11:47:01 +0100210 if (!smp->strm)
211 goto not_ssl_hello;
212
Nenad Merdanovic5fc7d7e2015-07-07 22:00:17 +0200213 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Willy Tarreaufc0785d2018-06-19 07:19:56 +0200214 bleft = ci_data(chn);
215 data = (unsigned char *)ci_head(chn);
Nenad Merdanovic5fc7d7e2015-07-07 22:00:17 +0200216
217 /* Check for SSL/TLS Handshake */
218 if (!bleft)
219 goto too_short;
220 if (*data != 0x16)
221 goto not_ssl_hello;
222
223 /* Check for SSLv3 or later (SSL version >= 3.0) in the record layer*/
224 if (bleft < 3)
225 goto too_short;
226 if (data[1] < 0x03)
227 goto not_ssl_hello;
228
229 if (bleft < 5)
230 goto too_short;
231 hs_len = (data[3] << 8) + data[4];
232 if (hs_len < 1 + 3 + 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
233 goto not_ssl_hello; /* too short to have an extension */
234
235 data += 5; /* enter TLS handshake */
236 bleft -= 5;
237
238 /* Check for a complete client hello starting at <data> */
239 if (bleft < 1)
240 goto too_short;
241 if (data[0] != 0x01) /* msg_type = Client Hello */
242 goto not_ssl_hello;
243
244 /* Check the Hello's length */
245 if (bleft < 4)
246 goto too_short;
247 hs_len = (data[1] << 16) + (data[2] << 8) + data[3];
248 if (hs_len < 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
249 goto not_ssl_hello; /* too short to have an extension */
250
251 /* We want the full handshake here */
252 if (bleft < hs_len)
253 goto too_short;
254
255 data += 4;
256 /* Start of the ClientHello message */
257 if (data[0] < 0x03 || data[1] < 0x01) /* TLSv1 minimum */
258 goto not_ssl_hello;
259
260 ext_len = data[34]; /* session_id_len */
261 if (ext_len > 32 || ext_len > (hs_len - 35)) /* check for correct session_id len */
262 goto not_ssl_hello;
263
264 /* Jump to cipher suite */
265 hs_len -= 35 + ext_len;
266 data += 35 + ext_len;
267
268 if (hs_len < 4 || /* minimum one cipher */
269 (ext_len = (data[0] << 8) + data[1]) < 2 || /* minimum 2 bytes for a cipher */
270 ext_len > hs_len)
271 goto not_ssl_hello;
272
273 /* Jump to the compression methods */
274 hs_len -= 2 + ext_len;
275 data += 2 + ext_len;
276
277 if (hs_len < 2 || /* minimum one compression method */
278 data[0] < 1 || data[0] > hs_len) /* minimum 1 bytes for a method */
279 goto not_ssl_hello;
280
281 /* Jump to the extensions */
282 hs_len -= 1 + data[0];
283 data += 1 + data[0];
284
285 if (hs_len < 2 || /* minimum one extension list length */
286 (ext_len = (data[0] << 8) + data[1]) > hs_len - 2) /* list too long */
287 goto not_ssl_hello;
288
289 hs_len = ext_len; /* limit ourselves to the extension length */
290 data += 2;
291
292 while (hs_len >= 4) {
293 int ext_type, ext_len;
294
295 ext_type = (data[0] << 8) + data[1];
296 ext_len = (data[2] << 8) + data[3];
297
298 if (ext_len > hs_len - 4) /* Extension too long */
299 goto not_ssl_hello;
300
301 /* Elliptic curves extension */
302 if (ext_type == 10) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200303 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200304 smp->data.u.sint = 1;
Nenad Merdanovic8a39a1f2015-07-15 12:51:11 +0200305 smp->flags = SMP_F_VOLATILE;
Nenad Merdanovic5fc7d7e2015-07-07 22:00:17 +0200306 return 1;
307 }
308
309 hs_len -= 4 + ext_len;
310 data += 4 + ext_len;
311 }
312 /* server name not found */
313 goto not_ssl_hello;
314
315 too_short:
316 smp->flags = SMP_F_MAY_CHANGE;
317
318 not_ssl_hello:
319
320 return 0;
321}
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100322/* returns the type of SSL hello message (mainly used to detect an SSL hello) */
323static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200324smp_fetch_ssl_hello_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100325{
326 int hs_len;
327 int hs_type, bleft;
328 struct channel *chn;
329 const unsigned char *data;
330
Willy Tarreaube508f12016-03-10 11:47:01 +0100331 if (!smp->strm)
332 goto not_ssl_hello;
333
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200334 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Willy Tarreaufc0785d2018-06-19 07:19:56 +0200335 bleft = ci_data(chn);
336 data = (const unsigned char *)ci_head(chn);
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100337
338 if (!bleft)
339 goto too_short;
340
341 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
342 /* SSLv3 header format */
343 if (bleft < 9)
344 goto too_short;
345
346 /* ssl version 3 */
347 if ((data[1] << 16) + data[2] < 0x00030000)
348 goto not_ssl_hello;
349
350 /* ssl message len must present handshake type and len */
351 if ((data[3] << 8) + data[4] < 4)
352 goto not_ssl_hello;
353
354 /* format introduced with SSLv3 */
355
356 hs_type = (int)data[5];
357 hs_len = ( data[6] << 16 ) + ( data[7] << 8 ) + data[8];
358
359 /* not a full handshake */
360 if (bleft < (9 + hs_len))
361 goto too_short;
362
363 }
364 else {
365 goto not_ssl_hello;
366 }
367
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200368 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200369 smp->data.u.sint = hs_type;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100370 smp->flags = SMP_F_VOLATILE;
371
372 return 1;
373
374 too_short:
375 smp->flags = SMP_F_MAY_CHANGE;
376
377 not_ssl_hello:
378
379 return 0;
380}
381
382/* Return the version of the SSL protocol in the request. It supports both
383 * SSLv3 (TLSv1) header format for any message, and SSLv2 header format for
384 * the hello message. The SSLv3 format is described in RFC 2246 p49, and the
385 * SSLv2 format is described here, and completed p67 of RFC 2246 :
386 * http://wp.netscape.com/eng/security/SSL_2.html
387 *
388 * Note: this decoder only works with non-wrapping data.
389 */
390static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200391smp_fetch_req_ssl_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100392{
393 int version, bleft, msg_len;
394 const unsigned char *data;
Willy Tarreaube508f12016-03-10 11:47:01 +0100395 struct channel *req;
396
397 if (!smp->strm)
398 return 0;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100399
Willy Tarreaube508f12016-03-10 11:47:01 +0100400 req = &smp->strm->req;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100401 msg_len = 0;
Willy Tarreaufc0785d2018-06-19 07:19:56 +0200402 bleft = ci_data(req);
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100403 if (!bleft)
404 goto too_short;
405
Willy Tarreaufc0785d2018-06-19 07:19:56 +0200406 data = (const unsigned char *)ci_head(req);
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100407 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
408 /* SSLv3 header format */
Lukas Tribusc93242c2015-11-05 13:59:30 +0100409 if (bleft < 11)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100410 goto too_short;
411
Lukas Tribusc93242c2015-11-05 13:59:30 +0100412 version = (data[1] << 16) + data[2]; /* record layer version: major, minor */
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100413 msg_len = (data[3] << 8) + data[4]; /* record length */
414
415 /* format introduced with SSLv3 */
416 if (version < 0x00030000)
417 goto not_ssl;
418
Lukas Tribusc93242c2015-11-05 13:59:30 +0100419 /* message length between 6 and 2^14 + 2048 */
420 if (msg_len < 6 || msg_len > ((1<<14) + 2048))
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100421 goto not_ssl;
422
423 bleft -= 5; data += 5;
Lukas Tribusc93242c2015-11-05 13:59:30 +0100424
425 /* return the client hello client version, not the record layer version */
426 version = (data[4] << 16) + data[5]; /* client hello version: major, minor */
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100427 } else {
428 /* SSLv2 header format, only supported for hello (msg type 1) */
429 int rlen, plen, cilen, silen, chlen;
430
431 if (*data & 0x80) {
432 if (bleft < 3)
433 goto too_short;
434 /* short header format : 15 bits for length */
435 rlen = ((data[0] & 0x7F) << 8) | data[1];
436 plen = 0;
437 bleft -= 2; data += 2;
438 } else {
439 if (bleft < 4)
440 goto too_short;
441 /* long header format : 14 bits for length + pad length */
442 rlen = ((data[0] & 0x3F) << 8) | data[1];
443 plen = data[2];
Willy Tarreau74967f62016-08-30 14:39:46 +0200444 bleft -= 3; data += 3;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100445 }
446
447 if (*data != 0x01)
448 goto not_ssl;
449 bleft--; data++;
450
451 if (bleft < 8)
452 goto too_short;
453 version = (data[0] << 16) + data[1]; /* version: major, minor */
454 cilen = (data[2] << 8) + data[3]; /* cipher len, multiple of 3 */
455 silen = (data[4] << 8) + data[5]; /* session_id_len: 0 or 16 */
456 chlen = (data[6] << 8) + data[7]; /* 16<=challenge length<=32 */
457
458 bleft -= 8; data += 8;
459 if (cilen % 3 != 0)
460 goto not_ssl;
461 if (silen && silen != 16)
462 goto not_ssl;
463 if (chlen < 16 || chlen > 32)
464 goto not_ssl;
465 if (rlen != 9 + cilen + silen + chlen)
466 goto not_ssl;
467
468 /* focus on the remaining data length */
469 msg_len = cilen + silen + chlen + plen;
470 }
471 /* We could recursively check that the buffer ends exactly on an SSL
472 * fragment boundary and that a possible next segment is still SSL,
473 * but that's a bit pointless. However, we could still check that
474 * all the part of the request which fits in a buffer is already
475 * there.
476 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200477 if (msg_len > channel_recv_limit(req) + b_orig(&req->buf) - ci_head(req))
478 msg_len = channel_recv_limit(req) + b_orig(&req->buf) - ci_head(req);
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100479
480 if (bleft < msg_len)
481 goto too_short;
482
483 /* OK that's enough. We have at least the whole message, and we have
484 * the protocol version.
485 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200486 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200487 smp->data.u.sint = version;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100488 smp->flags = SMP_F_VOLATILE;
489 return 1;
490
491 too_short:
492 smp->flags = SMP_F_MAY_CHANGE;
493 not_ssl:
494 return 0;
495}
496
497/* Try to extract the Server Name Indication that may be presented in a TLS
498 * client hello handshake message. The format of the message is the following
499 * (cf RFC5246 + RFC6066) :
500 * TLS frame :
501 * - uint8 type = 0x16 (Handshake)
502 * - uint16 version >= 0x0301 (TLSv1)
503 * - uint16 length (frame length)
504 * - TLS handshake :
505 * - uint8 msg_type = 0x01 (ClientHello)
506 * - uint24 length (handshake message length)
507 * - ClientHello :
508 * - uint16 client_version >= 0x0301 (TLSv1)
509 * - uint8 Random[32] (4 first ones are timestamp)
510 * - SessionID :
511 * - uint8 session_id_len (0..32) (SessionID len in bytes)
512 * - uint8 session_id[session_id_len]
513 * - CipherSuite :
514 * - uint16 cipher_len >= 2 (Cipher length in bytes)
515 * - uint16 ciphers[cipher_len/2]
516 * - CompressionMethod :
517 * - uint8 compression_len >= 1 (# of supported methods)
518 * - uint8 compression_methods[compression_len]
519 * - optional client_extension_len (in bytes)
520 * - optional sequence of ClientHelloExtensions (as many bytes as above):
521 * - uint16 extension_type = 0 for server_name
522 * - uint16 extension_len
523 * - opaque extension_data[extension_len]
524 * - uint16 server_name_list_len (# of bytes here)
525 * - opaque server_names[server_name_list_len bytes]
526 * - uint8 name_type = 0 for host_name
527 * - uint16 name_len
528 * - opaque hostname[name_len bytes]
529 */
530static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200531smp_fetch_ssl_hello_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100532{
533 int hs_len, ext_len, bleft;
534 struct channel *chn;
535 unsigned char *data;
536
Willy Tarreaube508f12016-03-10 11:47:01 +0100537 if (!smp->strm)
538 goto not_ssl_hello;
539
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200540 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Willy Tarreaufc0785d2018-06-19 07:19:56 +0200541 bleft = ci_data(chn);
542 data = (unsigned char *)ci_head(chn);
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100543
544 /* Check for SSL/TLS Handshake */
545 if (!bleft)
546 goto too_short;
547 if (*data != 0x16)
548 goto not_ssl_hello;
549
Lukas Tribus57d22972014-04-10 21:36:22 +0200550 /* Check for SSLv3 or later (SSL version >= 3.0) in the record layer*/
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100551 if (bleft < 3)
552 goto too_short;
Lukas Tribus57d22972014-04-10 21:36:22 +0200553 if (data[1] < 0x03)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100554 goto not_ssl_hello;
555
556 if (bleft < 5)
557 goto too_short;
558 hs_len = (data[3] << 8) + data[4];
559 if (hs_len < 1 + 3 + 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
560 goto not_ssl_hello; /* too short to have an extension */
561
562 data += 5; /* enter TLS handshake */
563 bleft -= 5;
564
565 /* Check for a complete client hello starting at <data> */
566 if (bleft < 1)
567 goto too_short;
568 if (data[0] != 0x01) /* msg_type = Client Hello */
569 goto not_ssl_hello;
570
571 /* Check the Hello's length */
572 if (bleft < 4)
573 goto too_short;
574 hs_len = (data[1] << 16) + (data[2] << 8) + data[3];
575 if (hs_len < 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
576 goto not_ssl_hello; /* too short to have an extension */
577
578 /* We want the full handshake here */
579 if (bleft < hs_len)
580 goto too_short;
581
582 data += 4;
583 /* Start of the ClientHello message */
584 if (data[0] < 0x03 || data[1] < 0x01) /* TLSv1 minimum */
585 goto not_ssl_hello;
586
587 ext_len = data[34]; /* session_id_len */
588 if (ext_len > 32 || ext_len > (hs_len - 35)) /* check for correct session_id len */
589 goto not_ssl_hello;
590
591 /* Jump to cipher suite */
592 hs_len -= 35 + ext_len;
593 data += 35 + ext_len;
594
595 if (hs_len < 4 || /* minimum one cipher */
596 (ext_len = (data[0] << 8) + data[1]) < 2 || /* minimum 2 bytes for a cipher */
597 ext_len > hs_len)
598 goto not_ssl_hello;
599
600 /* Jump to the compression methods */
601 hs_len -= 2 + ext_len;
602 data += 2 + ext_len;
603
604 if (hs_len < 2 || /* minimum one compression method */
605 data[0] < 1 || data[0] > hs_len) /* minimum 1 bytes for a method */
606 goto not_ssl_hello;
607
608 /* Jump to the extensions */
609 hs_len -= 1 + data[0];
610 data += 1 + data[0];
611
612 if (hs_len < 2 || /* minimum one extension list length */
613 (ext_len = (data[0] << 8) + data[1]) > hs_len - 2) /* list too long */
614 goto not_ssl_hello;
615
616 hs_len = ext_len; /* limit ourselves to the extension length */
617 data += 2;
618
619 while (hs_len >= 4) {
620 int ext_type, name_type, srv_len, name_len;
621
622 ext_type = (data[0] << 8) + data[1];
623 ext_len = (data[2] << 8) + data[3];
624
625 if (ext_len > hs_len - 4) /* Extension too long */
626 goto not_ssl_hello;
627
628 if (ext_type == 0) { /* Server name */
629 if (ext_len < 2) /* need one list length */
630 goto not_ssl_hello;
631
632 srv_len = (data[4] << 8) + data[5];
633 if (srv_len < 4 || srv_len > hs_len - 6)
634 goto not_ssl_hello; /* at least 4 bytes per server name */
635
636 name_type = data[6];
637 name_len = (data[7] << 8) + data[8];
638
639 if (name_type == 0) { /* hostname */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200640 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200641 smp->data.u.str.area = (char *)data + 9;
642 smp->data.u.str.data = name_len;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100643 smp->flags = SMP_F_VOLATILE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100644 return 1;
645 }
646 }
647
648 hs_len -= 4 + ext_len;
649 data += 4 + ext_len;
650 }
651 /* server name not found */
652 goto not_ssl_hello;
653
654 too_short:
655 smp->flags = SMP_F_MAY_CHANGE;
656
657 not_ssl_hello:
658
659 return 0;
660}
661
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200662/* Fetch the request RDP cookie identified in <cname>:<clen>, or any cookie if
Willy Tarreaub169eba2013-12-16 15:14:43 +0100663 * <clen> is empty (cname is then ignored). It returns the data into sample <smp>
664 * of type SMP_T_CSTR. Note: this decoder only works with non-wrapping data.
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100665 */
666int
Willy Tarreau87b09662015-04-03 00:22:06 +0200667fetch_rdp_cookie_name(struct stream *s, struct sample *smp, const char *cname, int clen)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100668{
669 int bleft;
670 const unsigned char *data;
671
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100672 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200673 smp->data.type = SMP_T_STR;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100674
Willy Tarreaufc0785d2018-06-19 07:19:56 +0200675 bleft = ci_data(&s->req);
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100676 if (bleft <= 11)
677 goto too_short;
678
Willy Tarreaufc0785d2018-06-19 07:19:56 +0200679 data = (const unsigned char *)ci_head(&s->req) + 11;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100680 bleft -= 11;
681
682 if (bleft <= 7)
683 goto too_short;
684
685 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
686 goto not_cookie;
687
688 data += 7;
689 bleft -= 7;
690
691 while (bleft > 0 && *data == ' ') {
692 data++;
693 bleft--;
694 }
695
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200696 if (clen) {
697 if (bleft <= clen)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100698 goto too_short;
699
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200700 if ((data[clen] != '=') ||
701 strncasecmp(cname, (const char *)data, clen) != 0)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100702 goto not_cookie;
703
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200704 data += clen + 1;
705 bleft -= clen + 1;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100706 } else {
707 while (bleft > 0 && *data != '=') {
708 if (*data == '\r' || *data == '\n')
709 goto not_cookie;
710 data++;
711 bleft--;
712 }
713
714 if (bleft < 1)
715 goto too_short;
716
717 if (*data != '=')
718 goto not_cookie;
719
720 data++;
721 bleft--;
722 }
723
724 /* data points to cookie value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200725 smp->data.u.str.area = (char *)data;
726 smp->data.u.str.data = 0;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100727
728 while (bleft > 0 && *data != '\r') {
729 data++;
730 bleft--;
731 }
732
733 if (bleft < 2)
734 goto too_short;
735
736 if (data[0] != '\r' || data[1] != '\n')
737 goto not_cookie;
738
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200739 smp->data.u.str.data = (char *)data - smp->data.u.str.area;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100740 smp->flags = SMP_F_VOLATILE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100741 return 1;
742
743 too_short:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100744 smp->flags = SMP_F_MAY_CHANGE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100745 not_cookie:
746 return 0;
747}
748
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200749/* Fetch the request RDP cookie identified in the args, or any cookie if no arg
750 * is passed. It is usable both for ACL and for samples. Note: this decoder
751 * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument
Willy Tarreaub169eba2013-12-16 15:14:43 +0100752 * is a string (cookie name), other types will lead to undefined behaviour. The
753 * returned sample has type SMP_T_CSTR.
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200754 */
755int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200756smp_fetch_rdp_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200757{
Willy Tarreaube508f12016-03-10 11:47:01 +0100758 if (!smp->strm)
759 return 0;
760
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200761 return fetch_rdp_cookie_name(smp->strm, smp,
762 args ? args->data.str.area : NULL,
763 args ? args->data.str.data : 0);
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200764}
765
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100766/* returns either 1 or 0 depending on whether an RDP cookie is found or not */
767static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200768smp_fetch_rdp_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100769{
770 int ret;
771
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200772 ret = smp_fetch_rdp_cookie(args, smp, kw, private);
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100773
774 if (smp->flags & SMP_F_MAY_CHANGE)
775 return 0;
776
777 smp->flags = SMP_F_VOLATILE;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200778 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200779 smp->data.u.sint = ret;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100780 return 1;
781}
782
783/* extracts part of a payload with offset and length at a given position */
784static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200785smp_fetch_payload_lv(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100786{
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200787 unsigned int len_offset = arg_p[0].data.sint;
788 unsigned int len_size = arg_p[1].data.sint;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100789 unsigned int buf_offset;
790 unsigned int buf_size = 0;
791 struct channel *chn;
792 int i;
793
794 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
795 /* by default buf offset == len offset + len size */
796 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
797
Willy Tarreaube508f12016-03-10 11:47:01 +0100798 if (!smp->strm)
799 return 0;
800
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200801 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Willy Tarreaufc0785d2018-06-19 07:19:56 +0200802 if (len_offset + len_size > ci_data(chn))
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100803 goto too_short;
804
805 for (i = 0; i < len_size; i++) {
Willy Tarreaufc0785d2018-06-19 07:19:56 +0200806 buf_size = (buf_size << 8) + ((unsigned char *)ci_head(chn))[i + len_offset];
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100807 }
808
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200809 /* buf offset may be implicit, absolute or relative. If the LSB
810 * is set, then the offset is relative otherwise it is absolute.
811 */
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100812 buf_offset = len_offset + len_size;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200813 if (arg_p[2].type == ARGT_SINT) {
814 if (arg_p[2].data.sint & 1)
815 buf_offset += arg_p[2].data.sint >> 1;
816 else
817 buf_offset = arg_p[2].data.sint >> 1;
818 }
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100819
Willy Tarreaud7bdcb82015-09-24 16:33:10 +0200820 if (!buf_size || buf_size > global.tune.bufsize || buf_offset + buf_size > global.tune.bufsize) {
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100821 /* will never match */
822 smp->flags = 0;
823 return 0;
824 }
825
Willy Tarreaufc0785d2018-06-19 07:19:56 +0200826 if (buf_offset + buf_size > ci_data(chn))
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100827 goto too_short;
828
829 /* init chunk as read only */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200830 smp->data.type = SMP_T_BIN;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100831 smp->flags = SMP_F_VOLATILE | SMP_F_CONST;
Willy Tarreaufc0785d2018-06-19 07:19:56 +0200832 chunk_initlen(&smp->data.u.str, ci_head(chn) + buf_offset, 0, buf_size);
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100833 return 1;
834
835 too_short:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100836 smp->flags = SMP_F_MAY_CHANGE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100837 return 0;
838}
839
840/* extracts some payload at a fixed position and length */
841static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200842smp_fetch_payload(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100843{
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200844 unsigned int buf_offset = arg_p[0].data.sint;
845 unsigned int buf_size = arg_p[1].data.sint;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100846 struct channel *chn;
847
Willy Tarreaube508f12016-03-10 11:47:01 +0100848 if (!smp->strm)
849 return 0;
850
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200851 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Felipe Guerreiro Barbosa Ruiz00f55522017-03-16 17:01:41 -0300852 if (buf_size > global.tune.bufsize || buf_offset + buf_size > global.tune.bufsize) {
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100853 /* will never match */
854 smp->flags = 0;
855 return 0;
856 }
857
Willy Tarreaufc0785d2018-06-19 07:19:56 +0200858 if (buf_offset + buf_size > ci_data(chn))
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100859 goto too_short;
860
861 /* init chunk as read only */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200862 smp->data.type = SMP_T_BIN;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100863 smp->flags = SMP_F_VOLATILE | SMP_F_CONST;
Willy Tarreaufc0785d2018-06-19 07:19:56 +0200864 chunk_initlen(&smp->data.u.str, ci_head(chn) + buf_offset, 0, buf_size ? buf_size : (ci_data(chn) - buf_offset));
Willy Tarreau3889fff2015-01-13 20:20:10 +0100865 if (!buf_size && channel_may_recv(chn) && !channel_input_closed(chn))
Willy Tarreau00f00842013-08-02 11:07:32 +0200866 smp->flags |= SMP_F_MAY_CHANGE;
867
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100868 return 1;
869
870 too_short:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100871 smp->flags = SMP_F_MAY_CHANGE | SMP_F_CONST;
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100872 return 0;
873}
874
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100875/* This function is used to validate the arguments passed to a "payload_lv" fetch
876 * keyword. This keyword allows two positive integers and an optional signed one,
877 * with the second one being strictly positive and the third one being greater than
878 * the opposite of the two others if negative. It is assumed that the types are
879 * already the correct ones. Returns 0 on error, non-zero if OK. If <err_msg> is
880 * not NULL, it will be filled with a pointer to an error message in case of
881 * error, that the caller is responsible for freeing. The initial location must
882 * either be freeable or NULL.
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200883 *
884 * Note that offset2 is stored with SINT type, but its not directly usable as is.
885 * The value is contained in the 63 MSB and the LSB is used as a flag for marking
886 * the "relative" property of the value.
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100887 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +0100888int val_payload_lv(struct arg *arg, char **err_msg)
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100889{
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200890 int relative = 0;
891 const char *str;
892
893 if (arg[0].data.sint < 0) {
894 memprintf(err_msg, "payload offset1 must be positive");
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100895 return 0;
896 }
897
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200898 if (!arg[1].data.sint) {
899 memprintf(err_msg, "payload length must be > 0");
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100900 return 0;
901 }
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200902
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200903 if (arg[2].type == ARGT_STR && arg[2].data.str.data > 0) {
904 if (arg[2].data.str.area[0] == '+' || arg[2].data.str.area[0] == '-')
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200905 relative = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200906 str = arg[2].data.str.area;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200907 arg[2].type = ARGT_SINT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200908 arg[2].data.sint = read_int64(&str,
909 str + arg[2].data.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200910 if (*str != '\0') {
911 memprintf(err_msg, "payload offset2 is not a number");
912 return 0;
913 }
914 if (arg[0].data.sint + arg[1].data.sint + arg[2].data.sint < 0) {
915 memprintf(err_msg, "payload offset2 too negative");
916 return 0;
917 }
918 if (relative)
919 arg[2].data.sint = ( arg[2].data.sint << 1 ) + 1;
920 }
Willy Tarreaud4c33c82013-01-07 21:59:07 +0100921 return 1;
922}
923
Willy Tarreaud716f9b2017-10-13 11:03:15 +0200924/* extracts the parameter value of a distcc token */
925static int
926smp_fetch_distcc_param(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
927{
928 unsigned int match_tok = arg_p[0].data.sint;
929 unsigned int match_occ = arg_p[1].data.sint;
930 unsigned int token;
931 unsigned int param;
932 unsigned int body;
933 unsigned int ofs;
934 unsigned int occ;
935 struct channel *chn;
936 int i;
937
938 /* Format is (token[,occ]). occ starts at 1. */
939
940 if (!smp->strm)
941 return 0;
942
943 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
944
945 ofs = 0; occ = 0;
946 while (1) {
Willy Tarreaufc0785d2018-06-19 07:19:56 +0200947 if (ofs + 12 > ci_data(chn)) {
Willy Tarreaud716f9b2017-10-13 11:03:15 +0200948 /* not there yet but could it at least fit ? */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200949 if (!chn->buf.size)
Willy Tarreaud716f9b2017-10-13 11:03:15 +0200950 goto too_short;
951
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200952 if (ofs + 12 <= channel_recv_limit(chn) + b_orig(&chn->buf) - ci_head(chn))
Willy Tarreaud716f9b2017-10-13 11:03:15 +0200953 goto too_short;
954
955 goto no_match;
956 }
957
Willy Tarreaufc0785d2018-06-19 07:19:56 +0200958 token = read_n32(ci_head(chn) + ofs);
Willy Tarreaud716f9b2017-10-13 11:03:15 +0200959 ofs += 4;
960
961 for (i = param = 0; i < 8; i++) {
Willy Tarreaufc0785d2018-06-19 07:19:56 +0200962 int c = hex2i(ci_head(chn)[ofs + i]);
Willy Tarreaud716f9b2017-10-13 11:03:15 +0200963
964 if (c < 0)
965 goto no_match;
966 param = (param << 4) + c;
967 }
968 ofs += 8;
969
970 /* these tokens don't have a body */
971 if (token != 0x41524743 /* ARGC */ && token != 0x44495354 /* DIST */ &&
972 token != 0x4E46494C /* NFIL */ && token != 0x53544154 /* STAT */ &&
973 token != 0x444F4E45 /* DONE */)
974 body = param;
975 else
976 body = 0;
977
978 if (token == match_tok) {
979 occ++;
980 if (!match_occ || match_occ == occ) {
981 /* found */
982 smp->data.type = SMP_T_SINT;
983 smp->data.u.sint = param;
984 smp->flags = SMP_F_VOLATILE | SMP_F_CONST;
985 return 1;
986 }
987 }
988 ofs += body;
989 }
990
991 too_short:
992 smp->flags = SMP_F_MAY_CHANGE | SMP_F_CONST;
993 return 0;
994 no_match:
995 /* will never match (end of buffer, or bad contents) */
996 smp->flags = 0;
997 return 0;
998
999}
1000
1001/* extracts the (possibly truncated) body of a distcc token */
1002static int
1003smp_fetch_distcc_body(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1004{
1005 unsigned int match_tok = arg_p[0].data.sint;
1006 unsigned int match_occ = arg_p[1].data.sint;
1007 unsigned int token;
1008 unsigned int param;
1009 unsigned int ofs;
1010 unsigned int occ;
1011 unsigned int body;
1012 struct channel *chn;
1013 int i;
1014
1015 /* Format is (token[,occ]). occ starts at 1. */
1016
1017 if (!smp->strm)
1018 return 0;
1019
1020 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1021
1022 ofs = 0; occ = 0;
1023 while (1) {
Willy Tarreaufc0785d2018-06-19 07:19:56 +02001024 if (ofs + 12 > ci_data(chn)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001025 if (!chn->buf.size)
Willy Tarreaud716f9b2017-10-13 11:03:15 +02001026 goto too_short;
1027
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001028 if (ofs + 12 <= channel_recv_limit(chn) + b_orig(&chn->buf) - ci_head(chn))
Willy Tarreaud716f9b2017-10-13 11:03:15 +02001029 goto too_short;
1030
1031 goto no_match;
1032 }
1033
Willy Tarreaufc0785d2018-06-19 07:19:56 +02001034 token = read_n32(ci_head(chn) + ofs);
Willy Tarreaud716f9b2017-10-13 11:03:15 +02001035 ofs += 4;
1036
1037 for (i = param = 0; i < 8; i++) {
Willy Tarreaufc0785d2018-06-19 07:19:56 +02001038 int c = hex2i(ci_head(chn)[ofs + i]);
Willy Tarreaud716f9b2017-10-13 11:03:15 +02001039
1040 if (c < 0)
1041 goto no_match;
1042 param = (param << 4) + c;
1043 }
1044 ofs += 8;
1045
1046 /* these tokens don't have a body */
1047 if (token != 0x41524743 /* ARGC */ && token != 0x44495354 /* DIST */ &&
1048 token != 0x4E46494C /* NFIL */ && token != 0x53544154 /* STAT */ &&
1049 token != 0x444F4E45 /* DONE */)
1050 body = param;
1051 else
1052 body = 0;
1053
1054 if (token == match_tok) {
1055 occ++;
1056 if (!match_occ || match_occ == occ) {
1057 /* found */
1058
1059 smp->data.type = SMP_T_BIN;
1060 smp->flags = SMP_F_VOLATILE | SMP_F_CONST;
1061
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001062 if (ofs + body > ci_head(chn) - b_orig(&chn->buf) + ci_data(chn)) {
Willy Tarreaud716f9b2017-10-13 11:03:15 +02001063 /* incomplete body */
1064
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001065 if (ofs + body > channel_recv_limit(chn) + b_orig(&chn->buf) - ci_head(chn)) {
Willy Tarreaud716f9b2017-10-13 11:03:15 +02001066 /* truncate it to whatever will fit */
1067 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001068 body = channel_recv_limit(chn) + b_orig(&chn->buf) - ci_head(chn) - ofs;
Willy Tarreaud716f9b2017-10-13 11:03:15 +02001069 }
1070 }
1071
Willy Tarreaufc0785d2018-06-19 07:19:56 +02001072 chunk_initlen(&smp->data.u.str, ci_head(chn) + ofs, 0, body);
Willy Tarreaud716f9b2017-10-13 11:03:15 +02001073 return 1;
1074 }
1075 }
1076 ofs += body;
1077 }
1078
1079 too_short:
1080 smp->flags = SMP_F_MAY_CHANGE | SMP_F_CONST;
1081 return 0;
1082 no_match:
1083 /* will never match (end of buffer, or bad contents) */
1084 smp->flags = 0;
1085 return 0;
1086
1087}
1088
1089/* This function is used to validate the arguments passed to a "distcc_param" or
1090 * "distcc_body" sample fetch keyword. They take a mandatory token name of exactly
1091 * 4 characters, followed by an optional occurrence number starting at 1. It is
1092 * assumed that the types are already the correct ones. Returns 0 on error, non-
1093 * zero if OK. If <err_msg> is not NULL, it will be filled with a pointer to an
1094 * error message in case of error, that the caller is responsible for freeing.
1095 * The initial location must either be freeable or NULL.
1096 */
1097int val_distcc(struct arg *arg, char **err_msg)
1098{
1099 unsigned int token;
1100
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001101 if (arg[0].data.str.data != 4) {
Willy Tarreaud716f9b2017-10-13 11:03:15 +02001102 memprintf(err_msg, "token name must be exactly 4 characters");
1103 return 0;
1104 }
1105
1106 /* convert the token name to an unsigned int (one byte per character,
1107 * big endian format).
1108 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001109 token = (arg[0].data.str.area[0] << 24) + (arg[0].data.str.area[1] << 16) +
1110 (arg[0].data.str.area[2] << 8) + (arg[0].data.str.area[3] << 0);
Willy Tarreaud716f9b2017-10-13 11:03:15 +02001111
1112 arg[0].type = ARGT_SINT;
1113 arg[0].data.sint = token;
1114
1115 if (arg[1].type != ARGT_SINT) {
1116 arg[1].type = ARGT_SINT;
1117 arg[1].data.sint = 0;
1118 }
1119 return 1;
1120}
1121
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001122/************************************************************************/
1123/* All supported sample and ACL keywords must be declared here. */
1124/************************************************************************/
1125
1126/* Note: must not be declared <const> as its list will be overwritten.
1127 * Note: fetches that may return multiple types must be declared as the lowest
1128 * common denominator, the type that can be casted into all other ones. For
1129 * instance IPv4/IPv6 must be declared IPv4.
1130 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001131static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreaud716f9b2017-10-13 11:03:15 +02001132 { "distcc_body", smp_fetch_distcc_body, ARG2(1,STR,SINT), val_distcc, SMP_T_BIN, SMP_USE_L6REQ|SMP_USE_L6RES },
1133 { "distcc_param", smp_fetch_distcc_param, ARG2(1,STR,SINT), val_distcc, SMP_T_SINT, SMP_USE_L6REQ|SMP_USE_L6RES },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001134 { "payload", smp_fetch_payload, ARG2(2,SINT,SINT), NULL, SMP_T_BIN, SMP_USE_L6REQ|SMP_USE_L6RES },
1135 { "payload_lv", smp_fetch_payload_lv, ARG3(2,SINT,SINT,STR), val_payload_lv, SMP_T_BIN, SMP_USE_L6REQ|SMP_USE_L6RES },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001136 { "rdp_cookie", smp_fetch_rdp_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_L6REQ },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001137 { "rdp_cookie_cnt", smp_fetch_rdp_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L6REQ },
1138 { "rep_ssl_hello_type", smp_fetch_ssl_hello_type, 0, NULL, SMP_T_SINT, SMP_USE_L6RES },
1139 { "req_len", smp_fetch_len, 0, NULL, SMP_T_SINT, SMP_USE_L6REQ },
1140 { "req_ssl_hello_type", smp_fetch_ssl_hello_type, 0, NULL, SMP_T_SINT, SMP_USE_L6REQ },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001141 { "req_ssl_sni", smp_fetch_ssl_hello_sni, 0, NULL, SMP_T_STR, SMP_USE_L6REQ },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001142 { "req_ssl_ver", smp_fetch_req_ssl_ver, 0, NULL, SMP_T_SINT, SMP_USE_L6REQ },
Willy Tarreaufa957342013-01-14 16:07:52 +01001143
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001144 { "req.len", smp_fetch_len, 0, NULL, SMP_T_SINT, SMP_USE_L6REQ },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001145 { "req.payload", smp_fetch_payload, ARG2(2,SINT,SINT), NULL, SMP_T_BIN, SMP_USE_L6REQ },
1146 { "req.payload_lv", smp_fetch_payload_lv, ARG3(2,SINT,SINT,STR), val_payload_lv, SMP_T_BIN, SMP_USE_L6REQ },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001147 { "req.rdp_cookie", smp_fetch_rdp_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_L6REQ },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001148 { "req.rdp_cookie_cnt", smp_fetch_rdp_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L6REQ },
Nenad Merdanovic5fc7d7e2015-07-07 22:00:17 +02001149 { "req.ssl_ec_ext", smp_fetch_req_ssl_ec_ext, 0, NULL, SMP_T_BOOL, SMP_USE_L6REQ },
Pradeep Jindalbb2acf52015-09-29 10:12:57 +05301150 { "req.ssl_st_ext", smp_fetch_req_ssl_st_ext, 0, NULL, SMP_T_SINT, SMP_USE_L6REQ },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001151 { "req.ssl_hello_type", smp_fetch_ssl_hello_type, 0, NULL, SMP_T_SINT, SMP_USE_L6REQ },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001152 { "req.ssl_sni", smp_fetch_ssl_hello_sni, 0, NULL, SMP_T_STR, SMP_USE_L6REQ },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001153 { "req.ssl_ver", smp_fetch_req_ssl_ver, 0, NULL, SMP_T_SINT, SMP_USE_L6REQ },
1154 { "res.len", smp_fetch_len, 0, NULL, SMP_T_SINT, SMP_USE_L6RES },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001155 { "res.payload", smp_fetch_payload, ARG2(2,SINT,SINT), NULL, SMP_T_BIN, SMP_USE_L6RES },
1156 { "res.payload_lv", smp_fetch_payload_lv, ARG3(2,SINT,SINT,STR), val_payload_lv, SMP_T_BIN, SMP_USE_L6RES },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001157 { "res.ssl_hello_type", smp_fetch_ssl_hello_type, 0, NULL, SMP_T_SINT, SMP_USE_L6RES },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001158 { "wait_end", smp_fetch_wait_end, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
1159 { /* END */ },
1160}};
1161
Willy Tarreau0108d902018-11-25 19:14:37 +01001162INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001163
1164/* Note: must not be declared <const> as its list will be overwritten.
1165 * Please take care of keeping this list alphabetically sorted.
1166 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001167static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +01001168 { "payload", "req.payload", PAT_MATCH_BIN },
1169 { "payload_lv", "req.payload_lv", PAT_MATCH_BIN },
1170 { "req_rdp_cookie", "req.rdp_cookie", PAT_MATCH_STR },
1171 { "req_rdp_cookie_cnt", "req.rdp_cookie_cnt", PAT_MATCH_INT },
1172 { "req_ssl_sni", "req.ssl_sni", PAT_MATCH_STR },
1173 { "req_ssl_ver", "req.ssl_ver", PAT_MATCH_INT, pat_parse_dotted_ver },
1174 { "req.ssl_ver", "req.ssl_ver", PAT_MATCH_INT, pat_parse_dotted_ver },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001175 { /* END */ },
1176}};
1177
Willy Tarreau0108d902018-11-25 19:14:37 +01001178INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001179
1180/*
1181 * Local variables:
1182 * c-indent-level: 8
1183 * c-basic-offset: 8
1184 * End:
1185 */