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