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 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 16 | #include <common/initcall.h> |
Willy Tarreau | d716f9b | 2017-10-13 11:03:15 +0200 | [diff] [blame] | 17 | #include <common/net_helper.h> |
Christopher Faulet | f0216da | 2018-12-14 13:44:53 +0100 | [diff] [blame] | 18 | #include <common/htx.h> |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 19 | #include <proto/acl.h> |
| 20 | #include <proto/arg.h> |
| 21 | #include <proto/channel.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 22 | #include <proto/pattern.h> |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 23 | #include <proto/payload.h> |
| 24 | #include <proto/sample.h> |
Christopher Faulet | f0216da | 2018-12-14 13:44:53 +0100 | [diff] [blame] | 25 | #include <proto/proto_http.h> |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 26 | |
| 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 | */ |
| 35 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 36 | smp_fetch_wait_end(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 37 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 38 | if (!(smp->opt & SMP_OPT_FINAL)) { |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 39 | smp->flags |= SMP_F_MAY_CHANGE; |
| 40 | return 0; |
| 41 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 42 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 43 | smp->data.u.sint = 1; |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 44 | return 1; |
| 45 | } |
| 46 | |
| 47 | /* return the number of bytes in the request buffer */ |
| 48 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 49 | smp_fetch_len(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 50 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 51 | struct channel *chn; |
| 52 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 53 | if (!smp->strm) |
| 54 | return 0; |
| 55 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 56 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 57 | smp->data.type = SMP_T_SINT; |
Christopher Faulet | f0216da | 2018-12-14 13:44:53 +0100 | [diff] [blame] | 58 | 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 Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 64 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 65 | return 1; |
| 66 | } |
| 67 | |
Pradeep Jindal | bb2acf5 | 2015-09-29 10:12:57 +0530 | [diff] [blame] | 68 | /* 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 | */ |
| 73 | static int |
| 74 | smp_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 Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 80 | if (!smp->strm) |
| 81 | goto not_ssl_hello; |
| 82 | |
Pradeep Jindal | bb2acf5 | 2015-09-29 10:12:57 +0530 | [diff] [blame] | 83 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
Willy Tarreau | fc0785d | 2018-06-19 07:19:56 +0200 | [diff] [blame] | 84 | bleft = ci_data(chn); |
| 85 | data = (unsigned char *)ci_head(chn); |
Pradeep Jindal | bb2acf5 | 2015-09-29 10:12:57 +0530 | [diff] [blame] | 86 | |
| 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 Jindal | bb2acf5 | 2015-09-29 10:12:57 +0530 | [diff] [blame] | 193 | too_short: |
| 194 | smp->flags = SMP_F_MAY_CHANGE; |
| 195 | |
| 196 | not_ssl_hello: |
| 197 | return 0; |
| 198 | } |
| 199 | |
Nenad Merdanovic | 5fc7d7e | 2015-07-07 22:00:17 +0200 | [diff] [blame] | 200 | /* Returns TRUE if the client sent Supported Elliptic Curves Extension (0x000a) |
| 201 | * Mainly used to detect if client supports ECC cipher suites. |
| 202 | */ |
| 203 | static int |
| 204 | smp_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 Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 210 | if (!smp->strm) |
| 211 | goto not_ssl_hello; |
| 212 | |
Nenad Merdanovic | 5fc7d7e | 2015-07-07 22:00:17 +0200 | [diff] [blame] | 213 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
Willy Tarreau | fc0785d | 2018-06-19 07:19:56 +0200 | [diff] [blame] | 214 | bleft = ci_data(chn); |
| 215 | data = (unsigned char *)ci_head(chn); |
Nenad Merdanovic | 5fc7d7e | 2015-07-07 22:00:17 +0200 | [diff] [blame] | 216 | |
| 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 FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 303 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 304 | smp->data.u.sint = 1; |
Nenad Merdanovic | 8a39a1f | 2015-07-15 12:51:11 +0200 | [diff] [blame] | 305 | smp->flags = SMP_F_VOLATILE; |
Nenad Merdanovic | 5fc7d7e | 2015-07-07 22:00:17 +0200 | [diff] [blame] | 306 | 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 Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 322 | /* returns the type of SSL hello message (mainly used to detect an SSL hello) */ |
| 323 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 324 | smp_fetch_ssl_hello_type(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 325 | { |
| 326 | int hs_len; |
| 327 | int hs_type, bleft; |
| 328 | struct channel *chn; |
| 329 | const unsigned char *data; |
| 330 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 331 | if (!smp->strm) |
| 332 | goto not_ssl_hello; |
| 333 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 334 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
Willy Tarreau | fc0785d | 2018-06-19 07:19:56 +0200 | [diff] [blame] | 335 | bleft = ci_data(chn); |
| 336 | data = (const unsigned char *)ci_head(chn); |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 337 | |
| 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 FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 368 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 369 | smp->data.u.sint = hs_type; |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 370 | 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 | */ |
| 390 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 391 | smp_fetch_req_ssl_ver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 392 | { |
| 393 | int version, bleft, msg_len; |
| 394 | const unsigned char *data; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 395 | struct channel *req; |
| 396 | |
| 397 | if (!smp->strm) |
| 398 | return 0; |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 399 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 400 | req = &smp->strm->req; |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 401 | msg_len = 0; |
Willy Tarreau | fc0785d | 2018-06-19 07:19:56 +0200 | [diff] [blame] | 402 | bleft = ci_data(req); |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 403 | if (!bleft) |
| 404 | goto too_short; |
| 405 | |
Willy Tarreau | fc0785d | 2018-06-19 07:19:56 +0200 | [diff] [blame] | 406 | data = (const unsigned char *)ci_head(req); |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 407 | if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) { |
| 408 | /* SSLv3 header format */ |
Lukas Tribus | c93242c | 2015-11-05 13:59:30 +0100 | [diff] [blame] | 409 | if (bleft < 11) |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 410 | goto too_short; |
| 411 | |
Lukas Tribus | c93242c | 2015-11-05 13:59:30 +0100 | [diff] [blame] | 412 | version = (data[1] << 16) + data[2]; /* record layer version: major, minor */ |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 413 | 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 Tribus | c93242c | 2015-11-05 13:59:30 +0100 | [diff] [blame] | 419 | /* message length between 6 and 2^14 + 2048 */ |
| 420 | if (msg_len < 6 || msg_len > ((1<<14) + 2048)) |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 421 | goto not_ssl; |
| 422 | |
| 423 | bleft -= 5; data += 5; |
Lukas Tribus | c93242c | 2015-11-05 13:59:30 +0100 | [diff] [blame] | 424 | |
| 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 Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 427 | } 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 Tarreau | 74967f6 | 2016-08-30 14:39:46 +0200 | [diff] [blame] | 444 | bleft -= 3; data += 3; |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 445 | } |
| 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 Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 477 | 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 Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 479 | |
| 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 FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 486 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 487 | smp->data.u.sint = version; |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 488 | 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 | */ |
| 530 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 531 | smp_fetch_ssl_hello_sni(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 532 | { |
| 533 | int hs_len, ext_len, bleft; |
| 534 | struct channel *chn; |
| 535 | unsigned char *data; |
| 536 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 537 | if (!smp->strm) |
| 538 | goto not_ssl_hello; |
| 539 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 540 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
Willy Tarreau | fc0785d | 2018-06-19 07:19:56 +0200 | [diff] [blame] | 541 | bleft = ci_data(chn); |
| 542 | data = (unsigned char *)ci_head(chn); |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 543 | |
| 544 | /* Check for SSL/TLS Handshake */ |
| 545 | if (!bleft) |
| 546 | goto too_short; |
| 547 | if (*data != 0x16) |
| 548 | goto not_ssl_hello; |
| 549 | |
Lukas Tribus | 57d2297 | 2014-04-10 21:36:22 +0200 | [diff] [blame] | 550 | /* 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] | 551 | if (bleft < 3) |
| 552 | goto too_short; |
Lukas Tribus | 57d2297 | 2014-04-10 21:36:22 +0200 | [diff] [blame] | 553 | if (data[1] < 0x03) |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 554 | 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 FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 640 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 641 | smp->data.u.str.area = (char *)data + 9; |
| 642 | smp->data.u.str.data = name_len; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 643 | smp->flags = SMP_F_VOLATILE | SMP_F_CONST; |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 644 | 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 Tarreau | cadd8c9 | 2013-07-22 18:09:52 +0200 | [diff] [blame] | 662 | /* 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] | 663 | * <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 Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 665 | */ |
| 666 | int |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 667 | 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] | 668 | { |
| 669 | int bleft; |
| 670 | const unsigned char *data; |
| 671 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 672 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 673 | smp->data.type = SMP_T_STR; |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 674 | |
Willy Tarreau | fc0785d | 2018-06-19 07:19:56 +0200 | [diff] [blame] | 675 | bleft = ci_data(&s->req); |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 676 | if (bleft <= 11) |
| 677 | goto too_short; |
| 678 | |
Willy Tarreau | fc0785d | 2018-06-19 07:19:56 +0200 | [diff] [blame] | 679 | data = (const unsigned char *)ci_head(&s->req) + 11; |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 680 | 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 Tarreau | cadd8c9 | 2013-07-22 18:09:52 +0200 | [diff] [blame] | 696 | if (clen) { |
| 697 | if (bleft <= clen) |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 698 | goto too_short; |
| 699 | |
Willy Tarreau | cadd8c9 | 2013-07-22 18:09:52 +0200 | [diff] [blame] | 700 | if ((data[clen] != '=') || |
| 701 | strncasecmp(cname, (const char *)data, clen) != 0) |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 702 | goto not_cookie; |
| 703 | |
Willy Tarreau | cadd8c9 | 2013-07-22 18:09:52 +0200 | [diff] [blame] | 704 | data += clen + 1; |
| 705 | bleft -= clen + 1; |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 706 | } 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 Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 725 | smp->data.u.str.area = (char *)data; |
| 726 | smp->data.u.str.data = 0; |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 727 | |
| 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 Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 739 | smp->data.u.str.data = (char *)data - smp->data.u.str.area; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 740 | smp->flags = SMP_F_VOLATILE | SMP_F_CONST; |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 741 | return 1; |
| 742 | |
| 743 | too_short: |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 744 | smp->flags = SMP_F_MAY_CHANGE | SMP_F_CONST; |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 745 | not_cookie: |
| 746 | return 0; |
| 747 | } |
| 748 | |
Willy Tarreau | cadd8c9 | 2013-07-22 18:09:52 +0200 | [diff] [blame] | 749 | /* 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 Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 752 | * is a string (cookie name), other types will lead to undefined behaviour. The |
| 753 | * returned sample has type SMP_T_CSTR. |
Willy Tarreau | cadd8c9 | 2013-07-22 18:09:52 +0200 | [diff] [blame] | 754 | */ |
| 755 | int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 756 | smp_fetch_rdp_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | cadd8c9 | 2013-07-22 18:09:52 +0200 | [diff] [blame] | 757 | { |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 758 | if (!smp->strm) |
| 759 | return 0; |
| 760 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 761 | return fetch_rdp_cookie_name(smp->strm, smp, |
| 762 | args ? args->data.str.area : NULL, |
| 763 | args ? args->data.str.data : 0); |
Willy Tarreau | cadd8c9 | 2013-07-22 18:09:52 +0200 | [diff] [blame] | 764 | } |
| 765 | |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 766 | /* returns either 1 or 0 depending on whether an RDP cookie is found or not */ |
| 767 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 768 | smp_fetch_rdp_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 769 | { |
| 770 | int ret; |
| 771 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 772 | ret = smp_fetch_rdp_cookie(args, smp, kw, private); |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 773 | |
| 774 | if (smp->flags & SMP_F_MAY_CHANGE) |
| 775 | return 0; |
| 776 | |
| 777 | smp->flags = SMP_F_VOLATILE; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 778 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 779 | smp->data.u.sint = ret; |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 780 | return 1; |
| 781 | } |
| 782 | |
| 783 | /* extracts part of a payload with offset and length at a given position */ |
| 784 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 785 | smp_fetch_payload_lv(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] | 786 | { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 787 | unsigned int len_offset = arg_p[0].data.sint; |
| 788 | unsigned int len_size = arg_p[1].data.sint; |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 789 | 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 Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 798 | if (!smp->strm) |
| 799 | return 0; |
| 800 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 801 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
Willy Tarreau | fc0785d | 2018-06-19 07:19:56 +0200 | [diff] [blame] | 802 | if (len_offset + len_size > ci_data(chn)) |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 803 | goto too_short; |
| 804 | |
| 805 | for (i = 0; i < len_size; i++) { |
Willy Tarreau | fc0785d | 2018-06-19 07:19:56 +0200 | [diff] [blame] | 806 | buf_size = (buf_size << 8) + ((unsigned char *)ci_head(chn))[i + len_offset]; |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 807 | } |
| 808 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 809 | /* 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 Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 812 | buf_offset = len_offset + len_size; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 813 | 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 Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 819 | |
Willy Tarreau | d7bdcb8 | 2015-09-24 16:33:10 +0200 | [diff] [blame] | 820 | if (!buf_size || buf_size > global.tune.bufsize || buf_offset + buf_size > global.tune.bufsize) { |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 821 | /* will never match */ |
| 822 | smp->flags = 0; |
| 823 | return 0; |
| 824 | } |
| 825 | |
Willy Tarreau | fc0785d | 2018-06-19 07:19:56 +0200 | [diff] [blame] | 826 | if (buf_offset + buf_size > ci_data(chn)) |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 827 | goto too_short; |
| 828 | |
| 829 | /* init chunk as read only */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 830 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 831 | smp->flags = SMP_F_VOLATILE | SMP_F_CONST; |
Willy Tarreau | fc0785d | 2018-06-19 07:19:56 +0200 | [diff] [blame] | 832 | chunk_initlen(&smp->data.u.str, ci_head(chn) + buf_offset, 0, buf_size); |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 833 | return 1; |
| 834 | |
| 835 | too_short: |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 836 | smp->flags = SMP_F_MAY_CHANGE | SMP_F_CONST; |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | /* extracts some payload at a fixed position and length */ |
| 841 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 842 | smp_fetch_payload(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] | 843 | { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 844 | unsigned int buf_offset = arg_p[0].data.sint; |
| 845 | unsigned int buf_size = arg_p[1].data.sint; |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 846 | struct channel *chn; |
| 847 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 848 | if (!smp->strm) |
| 849 | return 0; |
| 850 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 851 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
Felipe Guerreiro Barbosa Ruiz | 00f5552 | 2017-03-16 17:01:41 -0300 | [diff] [blame] | 852 | if (buf_size > global.tune.bufsize || buf_offset + buf_size > global.tune.bufsize) { |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 853 | /* will never match */ |
| 854 | smp->flags = 0; |
| 855 | return 0; |
| 856 | } |
| 857 | |
Willy Tarreau | fc0785d | 2018-06-19 07:19:56 +0200 | [diff] [blame] | 858 | if (buf_offset + buf_size > ci_data(chn)) |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 859 | goto too_short; |
| 860 | |
| 861 | /* init chunk as read only */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 862 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 863 | smp->flags = SMP_F_VOLATILE | SMP_F_CONST; |
Willy Tarreau | fc0785d | 2018-06-19 07:19:56 +0200 | [diff] [blame] | 864 | chunk_initlen(&smp->data.u.str, ci_head(chn) + buf_offset, 0, buf_size ? buf_size : (ci_data(chn) - buf_offset)); |
Willy Tarreau | 3889fff | 2015-01-13 20:20:10 +0100 | [diff] [blame] | 865 | if (!buf_size && channel_may_recv(chn) && !channel_input_closed(chn)) |
Willy Tarreau | 00f0084 | 2013-08-02 11:07:32 +0200 | [diff] [blame] | 866 | smp->flags |= SMP_F_MAY_CHANGE; |
| 867 | |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 868 | return 1; |
| 869 | |
| 870 | too_short: |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 871 | smp->flags = SMP_F_MAY_CHANGE | SMP_F_CONST; |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 872 | return 0; |
| 873 | } |
| 874 | |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 875 | /* 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 FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 883 | * |
| 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 Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 887 | */ |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 888 | int val_payload_lv(struct arg *arg, char **err_msg) |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 889 | { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 890 | 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 Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 895 | return 0; |
| 896 | } |
| 897 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 898 | if (!arg[1].data.sint) { |
| 899 | memprintf(err_msg, "payload length must be > 0"); |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 900 | return 0; |
| 901 | } |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 902 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 903 | 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 FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 905 | relative = 1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 906 | str = arg[2].data.str.area; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 907 | arg[2].type = ARGT_SINT; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 908 | arg[2].data.sint = read_int64(&str, |
| 909 | str + arg[2].data.str.data); |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 910 | 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 Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 921 | return 1; |
| 922 | } |
| 923 | |
Willy Tarreau | d716f9b | 2017-10-13 11:03:15 +0200 | [diff] [blame] | 924 | /* extracts the parameter value of a distcc token */ |
| 925 | static int |
| 926 | smp_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 Tarreau | fc0785d | 2018-06-19 07:19:56 +0200 | [diff] [blame] | 947 | if (ofs + 12 > ci_data(chn)) { |
Willy Tarreau | d716f9b | 2017-10-13 11:03:15 +0200 | [diff] [blame] | 948 | /* not there yet but could it at least fit ? */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 949 | if (!chn->buf.size) |
Willy Tarreau | d716f9b | 2017-10-13 11:03:15 +0200 | [diff] [blame] | 950 | goto too_short; |
| 951 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 952 | if (ofs + 12 <= channel_recv_limit(chn) + b_orig(&chn->buf) - ci_head(chn)) |
Willy Tarreau | d716f9b | 2017-10-13 11:03:15 +0200 | [diff] [blame] | 953 | goto too_short; |
| 954 | |
| 955 | goto no_match; |
| 956 | } |
| 957 | |
Willy Tarreau | fc0785d | 2018-06-19 07:19:56 +0200 | [diff] [blame] | 958 | token = read_n32(ci_head(chn) + ofs); |
Willy Tarreau | d716f9b | 2017-10-13 11:03:15 +0200 | [diff] [blame] | 959 | ofs += 4; |
| 960 | |
| 961 | for (i = param = 0; i < 8; i++) { |
Willy Tarreau | fc0785d | 2018-06-19 07:19:56 +0200 | [diff] [blame] | 962 | int c = hex2i(ci_head(chn)[ofs + i]); |
Willy Tarreau | d716f9b | 2017-10-13 11:03:15 +0200 | [diff] [blame] | 963 | |
| 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 */ |
| 1002 | static int |
| 1003 | smp_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 Tarreau | fc0785d | 2018-06-19 07:19:56 +0200 | [diff] [blame] | 1024 | if (ofs + 12 > ci_data(chn)) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1025 | if (!chn->buf.size) |
Willy Tarreau | d716f9b | 2017-10-13 11:03:15 +0200 | [diff] [blame] | 1026 | goto too_short; |
| 1027 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1028 | if (ofs + 12 <= channel_recv_limit(chn) + b_orig(&chn->buf) - ci_head(chn)) |
Willy Tarreau | d716f9b | 2017-10-13 11:03:15 +0200 | [diff] [blame] | 1029 | goto too_short; |
| 1030 | |
| 1031 | goto no_match; |
| 1032 | } |
| 1033 | |
Willy Tarreau | fc0785d | 2018-06-19 07:19:56 +0200 | [diff] [blame] | 1034 | token = read_n32(ci_head(chn) + ofs); |
Willy Tarreau | d716f9b | 2017-10-13 11:03:15 +0200 | [diff] [blame] | 1035 | ofs += 4; |
| 1036 | |
| 1037 | for (i = param = 0; i < 8; i++) { |
Willy Tarreau | fc0785d | 2018-06-19 07:19:56 +0200 | [diff] [blame] | 1038 | int c = hex2i(ci_head(chn)[ofs + i]); |
Willy Tarreau | d716f9b | 2017-10-13 11:03:15 +0200 | [diff] [blame] | 1039 | |
| 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 Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1062 | if (ofs + body > ci_head(chn) - b_orig(&chn->buf) + ci_data(chn)) { |
Willy Tarreau | d716f9b | 2017-10-13 11:03:15 +0200 | [diff] [blame] | 1063 | /* incomplete body */ |
| 1064 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1065 | if (ofs + body > channel_recv_limit(chn) + b_orig(&chn->buf) - ci_head(chn)) { |
Willy Tarreau | d716f9b | 2017-10-13 11:03:15 +0200 | [diff] [blame] | 1066 | /* truncate it to whatever will fit */ |
| 1067 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1068 | body = channel_recv_limit(chn) + b_orig(&chn->buf) - ci_head(chn) - ofs; |
Willy Tarreau | d716f9b | 2017-10-13 11:03:15 +0200 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | |
Willy Tarreau | fc0785d | 2018-06-19 07:19:56 +0200 | [diff] [blame] | 1072 | chunk_initlen(&smp->data.u.str, ci_head(chn) + ofs, 0, body); |
Willy Tarreau | d716f9b | 2017-10-13 11:03:15 +0200 | [diff] [blame] | 1073 | 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 | */ |
| 1097 | int val_distcc(struct arg *arg, char **err_msg) |
| 1098 | { |
| 1099 | unsigned int token; |
| 1100 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1101 | if (arg[0].data.str.data != 4) { |
Willy Tarreau | d716f9b | 2017-10-13 11:03:15 +0200 | [diff] [blame] | 1102 | 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 Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1109 | 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 Tarreau | d716f9b | 2017-10-13 11:03:15 +0200 | [diff] [blame] | 1111 | |
| 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 Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 1122 | /************************************************************************/ |
| 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 Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 1131 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
Willy Tarreau | d716f9b | 2017-10-13 11:03:15 +0200 | [diff] [blame] | 1132 | { "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 FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1134 | { "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 FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1136 | { "rdp_cookie", smp_fetch_rdp_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_L6REQ }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1137 | { "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 FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1141 | { "req_ssl_sni", smp_fetch_ssl_hello_sni, 0, NULL, SMP_T_STR, SMP_USE_L6REQ }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1142 | { "req_ssl_ver", smp_fetch_req_ssl_ver, 0, NULL, SMP_T_SINT, SMP_USE_L6REQ }, |
Willy Tarreau | fa95734 | 2013-01-14 16:07:52 +0100 | [diff] [blame] | 1143 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1144 | { "req.len", smp_fetch_len, 0, NULL, SMP_T_SINT, SMP_USE_L6REQ }, |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1145 | { "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 FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1147 | { "req.rdp_cookie", smp_fetch_rdp_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_L6REQ }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1148 | { "req.rdp_cookie_cnt", smp_fetch_rdp_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L6REQ }, |
Nenad Merdanovic | 5fc7d7e | 2015-07-07 22:00:17 +0200 | [diff] [blame] | 1149 | { "req.ssl_ec_ext", smp_fetch_req_ssl_ec_ext, 0, NULL, SMP_T_BOOL, SMP_USE_L6REQ }, |
Pradeep Jindal | bb2acf5 | 2015-09-29 10:12:57 +0530 | [diff] [blame] | 1150 | { "req.ssl_st_ext", smp_fetch_req_ssl_st_ext, 0, NULL, SMP_T_SINT, SMP_USE_L6REQ }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1151 | { "req.ssl_hello_type", smp_fetch_ssl_hello_type, 0, NULL, SMP_T_SINT, SMP_USE_L6REQ }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1152 | { "req.ssl_sni", smp_fetch_ssl_hello_sni, 0, NULL, SMP_T_STR, SMP_USE_L6REQ }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1153 | { "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 FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1155 | { "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 FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1157 | { "res.ssl_hello_type", smp_fetch_ssl_hello_type, 0, NULL, SMP_T_SINT, SMP_USE_L6RES }, |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 1158 | { "wait_end", smp_fetch_wait_end, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN }, |
| 1159 | { /* END */ }, |
| 1160 | }}; |
| 1161 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1162 | INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws); |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 1163 | |
| 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 Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 1167 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 1168 | { "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 Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 1175 | { /* END */ }, |
| 1176 | }}; |
| 1177 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1178 | INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws); |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 1179 | |
| 1180 | /* |
| 1181 | * Local variables: |
| 1182 | * c-indent-level: 8 |
| 1183 | * c-basic-offset: 8 |
| 1184 | * End: |
| 1185 | */ |