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