Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1 | /* |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 2 | * Sample management functions. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 5 | * Copyright (C) 2012 Willy Tarreau <w@1wt.eu> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | */ |
| 13 | |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 14 | #include <ctype.h> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 15 | #include <string.h> |
| 16 | #include <arpa/inet.h> |
Willy Tarreau | dd2f85e | 2012-09-02 22:34:23 +0200 | [diff] [blame] | 17 | #include <stdio.h> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 18 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 19 | #include <import/sha1.h> |
| 20 | #include <import/xxhash.h> |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 21 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 22 | #include <haproxy/api.h> |
| 23 | #include <haproxy/arg.h> |
Willy Tarreau | ac13aea | 2020-06-04 10:36:03 +0200 | [diff] [blame] | 24 | #include <haproxy/auth.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 25 | #include <haproxy/base64.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 26 | #include <haproxy/buf.h> |
Willy Tarreau | c13ed53 | 2020-06-02 10:22:45 +0200 | [diff] [blame] | 27 | #include <haproxy/chunk.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 28 | #include <haproxy/errors.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 29 | #include <haproxy/global.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 30 | #include <haproxy/hash.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 31 | #include <haproxy/http.h> |
Willy Tarreau | 6131d6a | 2020-06-02 16:48:09 +0200 | [diff] [blame] | 32 | #include <haproxy/net_helper.h> |
Willy Tarreau | b23e595 | 2020-06-04 16:06:59 +0200 | [diff] [blame] | 33 | #include <haproxy/protobuf.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 34 | #include <haproxy/proxy.h> |
Willy Tarreau | 7cd8b6e | 2020-06-02 17:32:26 +0200 | [diff] [blame] | 35 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 36 | #include <haproxy/sample.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 37 | #include <haproxy/sink.h> |
| 38 | #include <haproxy/stick_table.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 39 | #include <haproxy/tools.h> |
Willy Tarreau | 8c42b8a | 2020-06-04 19:27:34 +0200 | [diff] [blame] | 40 | #include <haproxy/uri_auth-t.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 41 | #include <haproxy/vars.h> |
Thierry FOURNIER | 01e0974 | 2016-12-26 11:46:11 +0100 | [diff] [blame] | 42 | |
Willy Tarreau | 1cf8f08 | 2014-02-07 12:14:54 +0100 | [diff] [blame] | 43 | /* sample type names */ |
| 44 | const char *smp_to_type[SMP_TYPES] = { |
Thierry FOURNIER | 9c627e8 | 2015-06-03 20:12:35 +0200 | [diff] [blame] | 45 | [SMP_T_ANY] = "any", |
Willy Tarreau | 1cf8f08 | 2014-02-07 12:14:54 +0100 | [diff] [blame] | 46 | [SMP_T_BOOL] = "bool", |
Willy Tarreau | 1cf8f08 | 2014-02-07 12:14:54 +0100 | [diff] [blame] | 47 | [SMP_T_SINT] = "sint", |
| 48 | [SMP_T_ADDR] = "addr", |
| 49 | [SMP_T_IPV4] = "ipv4", |
| 50 | [SMP_T_IPV6] = "ipv6", |
| 51 | [SMP_T_STR] = "str", |
| 52 | [SMP_T_BIN] = "bin", |
Thierry FOURNIER | 4c2479e | 2015-06-03 20:12:04 +0200 | [diff] [blame] | 53 | [SMP_T_METH] = "meth", |
Willy Tarreau | 1cf8f08 | 2014-02-07 12:14:54 +0100 | [diff] [blame] | 54 | }; |
| 55 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 56 | /* static sample used in sample_process() when <p> is NULL */ |
Emeric Brun | e5c918b | 2017-06-14 14:15:36 +0200 | [diff] [blame] | 57 | static THREAD_LOCAL struct sample temp_smp; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 58 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 59 | /* list head of all known sample fetch keywords */ |
| 60 | static struct sample_fetch_kw_list sample_fetches = { |
| 61 | .list = LIST_HEAD_INIT(sample_fetches.list) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 62 | }; |
| 63 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 64 | /* list head of all known sample format conversion keywords */ |
| 65 | static struct sample_conv_kw_list sample_convs = { |
| 66 | .list = LIST_HEAD_INIT(sample_convs.list) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 67 | }; |
| 68 | |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 69 | const unsigned int fetch_cap[SMP_SRC_ENTRIES] = { |
| 70 | [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 71 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 72 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 73 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 74 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 75 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 76 | SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 77 | |
| 78 | [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 79 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 80 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 81 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 82 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 83 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 84 | SMP_VAL_FE_LOG_END | SMP_VAL___________), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 85 | |
| 86 | [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 87 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 88 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 89 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 90 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 91 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 92 | SMP_VAL_FE_LOG_END | SMP_VAL___________), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 93 | |
| 94 | [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 95 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 96 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 97 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 98 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 99 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 100 | SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 101 | |
| 102 | [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 103 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 104 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 105 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 106 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 107 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 108 | SMP_VAL_FE_LOG_END | SMP_VAL___________), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 109 | |
| 110 | [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 111 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 112 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 113 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 114 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 115 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 116 | SMP_VAL_FE_LOG_END | SMP_VAL___________), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 117 | |
| 118 | [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT | |
| 119 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 120 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 121 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ | |
| 122 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 123 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 124 | SMP_VAL___________ | SMP_VAL___________), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 125 | |
| 126 | [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT | |
| 127 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 128 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 129 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ | |
| 130 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 131 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 132 | SMP_VAL___________ | SMP_VAL___________), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 133 | |
| 134 | [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT | |
| 135 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 136 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 137 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 138 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 139 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 140 | SMP_VAL_FE_LOG_END | SMP_VAL___________), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 141 | |
| 142 | [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 143 | SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 144 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 145 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ | |
| 146 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 147 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 148 | SMP_VAL___________ | SMP_VAL___________), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 149 | |
| 150 | [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 151 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 152 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 153 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 154 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 155 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 156 | SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 157 | |
| 158 | [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 159 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 160 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 161 | SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 162 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 163 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 164 | SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 165 | |
| 166 | [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 167 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 168 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 169 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 170 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 171 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 172 | SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 173 | |
| 174 | [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 175 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 176 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 177 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 178 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 179 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 180 | SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 181 | |
| 182 | [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 183 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 184 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 185 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 186 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 187 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 188 | SMP_VAL___________ | SMP_VAL_BE_CHK_RUL), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 189 | |
| 190 | [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 191 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 192 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 193 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 194 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 195 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 196 | SMP_VAL___________ | SMP_VAL_BE_CHK_RUL), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 197 | |
| 198 | [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 199 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 200 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 201 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 202 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 203 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 204 | SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 205 | |
| 206 | [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 207 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 208 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 209 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 210 | SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 211 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 212 | SMP_VAL___________ | SMP_VAL_BE_CHK_RUL), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 213 | |
| 214 | [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 215 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 216 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 217 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 218 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 219 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 220 | SMP_VAL_FE_LOG_END | SMP_VAL___________), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 221 | |
| 222 | [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 223 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 224 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 225 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 226 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 227 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 228 | SMP_VAL_FE_LOG_END | SMP_VAL___________), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 229 | |
| 230 | [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 231 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 232 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 233 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 234 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 235 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 236 | SMP_VAL_FE_LOG_END | SMP_VAL___________), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 237 | |
| 238 | [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 239 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 240 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 241 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 242 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 243 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 244 | SMP_VAL_FE_LOG_END | SMP_VAL___________), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | static const char *fetch_src_names[SMP_SRC_ENTRIES] = { |
| 248 | [SMP_SRC_INTRN] = "internal state", |
| 249 | [SMP_SRC_LISTN] = "listener", |
| 250 | [SMP_SRC_FTEND] = "frontend", |
| 251 | [SMP_SRC_L4CLI] = "client address", |
| 252 | [SMP_SRC_L5CLI] = "client-side connection", |
| 253 | [SMP_SRC_TRACK] = "track counters", |
| 254 | [SMP_SRC_L6REQ] = "request buffer", |
| 255 | [SMP_SRC_HRQHV] = "HTTP request headers", |
| 256 | [SMP_SRC_HRQHP] = "HTTP request", |
| 257 | [SMP_SRC_HRQBO] = "HTTP request body", |
| 258 | [SMP_SRC_BKEND] = "backend", |
| 259 | [SMP_SRC_SERVR] = "server", |
| 260 | [SMP_SRC_L4SRV] = "server address", |
| 261 | [SMP_SRC_L5SRV] = "server-side connection", |
| 262 | [SMP_SRC_L6RES] = "response buffer", |
| 263 | [SMP_SRC_HRSHV] = "HTTP response headers", |
| 264 | [SMP_SRC_HRSHP] = "HTTP response", |
| 265 | [SMP_SRC_HRSBO] = "HTTP response body", |
| 266 | [SMP_SRC_RQFIN] = "request buffer statistics", |
| 267 | [SMP_SRC_RSFIN] = "response buffer statistics", |
| 268 | [SMP_SRC_TXFIN] = "transaction statistics", |
| 269 | [SMP_SRC_SSFIN] = "session statistics", |
| 270 | }; |
| 271 | |
Willy Tarreau | bf8e251 | 2013-03-25 14:52:41 +0100 | [diff] [blame] | 272 | static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = { |
| 273 | [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule", |
| 274 | [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule", |
| 275 | [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule", |
| 276 | [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule", |
| 277 | [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule", |
| 278 | [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule", |
| 279 | [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule", |
| 280 | [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule", |
| 281 | [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule", |
| 282 | [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule", |
| 283 | [SMP_CKP_BE_SRV_CON] = "server source selection", |
| 284 | [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule", |
| 285 | [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule", |
| 286 | [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule", |
| 287 | [SMP_CKP_BE_STO_RUL] = "backend stick-store rule", |
| 288 | [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule", |
| 289 | [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule", |
| 290 | [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule", |
| 291 | [SMP_CKP_FE_LOG_END] = "logs", |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 292 | [SMP_CKP_BE_CHK_RUL] = "backend tcp-check rule", |
Willy Tarreau | bf8e251 | 2013-03-25 14:52:41 +0100 | [diff] [blame] | 293 | }; |
| 294 | |
Thierry FOURNIER | 348971e | 2013-11-21 10:50:10 +0100 | [diff] [blame] | 295 | /* This function returns the type of the data returned by the sample_expr. |
| 296 | * It assumes that the <expr> and all of its converters are properly |
| 297 | * initialized. |
| 298 | */ |
| 299 | inline |
| 300 | int smp_expr_output_type(struct sample_expr *expr) |
| 301 | { |
| 302 | struct sample_conv_expr *smp_expr; |
| 303 | |
| 304 | if (!LIST_ISEMPTY(&expr->conv_exprs)) { |
| 305 | smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list); |
| 306 | return smp_expr->conv->out_type; |
| 307 | } |
| 308 | return expr->fetch->out_type; |
| 309 | } |
| 310 | |
| 311 | |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 312 | /* fill the trash with a comma-delimited list of source names for the <use> bit |
| 313 | * field which must be composed of a non-null set of SMP_USE_* flags. The return |
| 314 | * value is the pointer to the string in the trash buffer. |
| 315 | */ |
| 316 | const char *sample_src_names(unsigned int use) |
| 317 | { |
| 318 | int bit; |
| 319 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 320 | trash.data = 0; |
| 321 | trash.area[0] = '\0'; |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 322 | for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) { |
| 323 | if (!(use & ~((1 << bit) - 1))) |
| 324 | break; /* no more bits */ |
| 325 | |
| 326 | if (!(use & (1 << bit))) |
| 327 | continue; /* bit not set */ |
| 328 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 329 | trash.data += snprintf(trash.area + trash.data, |
| 330 | trash.size - trash.data, "%s%s", |
| 331 | (use & ((1 << bit) - 1)) ? "," : "", |
| 332 | fetch_src_names[bit]); |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 333 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 334 | return trash.area; |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 335 | } |
| 336 | |
Willy Tarreau | bf8e251 | 2013-03-25 14:52:41 +0100 | [diff] [blame] | 337 | /* return a pointer to the correct sample checkpoint name, or "unknown" when |
| 338 | * the flags are invalid. Only the lowest bit is used, higher bits are ignored |
| 339 | * if set. |
| 340 | */ |
| 341 | const char *sample_ckp_names(unsigned int use) |
| 342 | { |
| 343 | int bit; |
| 344 | |
| 345 | for (bit = 0; bit < SMP_CKP_ENTRIES; bit++) |
| 346 | if (use & (1 << bit)) |
| 347 | return fetch_ckp_names[bit]; |
| 348 | return "unknown sample check place, please report this bug"; |
| 349 | } |
| 350 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 351 | /* |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 352 | * Registers the sample fetch keyword list <kwl> as a list of valid keywords |
| 353 | * for next parsing sessions. The fetch keywords capabilities are also computed |
| 354 | * from their ->use field. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 355 | */ |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 356 | void sample_register_fetches(struct sample_fetch_kw_list *kwl) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 357 | { |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 358 | struct sample_fetch *sf; |
| 359 | int bit; |
| 360 | |
| 361 | for (sf = kwl->kw; sf->kw != NULL; sf++) { |
| 362 | for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) |
| 363 | if (sf->use & (1 << bit)) |
| 364 | sf->val |= fetch_cap[bit]; |
| 365 | } |
| 366 | LIST_ADDQ(&sample_fetches.list, &kwl->list); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 370 | * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 371 | * parsing sessions. |
| 372 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 373 | void sample_register_convs(struct sample_conv_kw_list *pckl) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 374 | { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 375 | LIST_ADDQ(&sample_convs.list, &pckl->list); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 379 | * Returns the pointer on sample fetch keyword structure identified by |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 380 | * string of <len> in buffer <kw>. |
| 381 | * |
| 382 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 383 | struct sample_fetch *find_sample_fetch(const char *kw, int len) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 384 | { |
| 385 | int index; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 386 | struct sample_fetch_kw_list *kwl; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 387 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 388 | list_for_each_entry(kwl, &sample_fetches.list, list) { |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 389 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 390 | if (strncmp(kwl->kw[index].kw, kw, len) == 0 && |
| 391 | kwl->kw[index].kw[len] == '\0') |
| 392 | return &kwl->kw[index]; |
| 393 | } |
| 394 | } |
| 395 | return NULL; |
| 396 | } |
| 397 | |
Tim Duesterhus | c555ee0 | 2018-01-25 16:24:44 +0100 | [diff] [blame] | 398 | /* This function browses the list of available sample fetches. <current> is |
Thierry FOURNIER | 4d9a1d1 | 2014-12-08 14:49:19 +0100 | [diff] [blame] | 399 | * the last used sample fetch. If it is the first call, it must set to NULL. |
Tim Duesterhus | c555ee0 | 2018-01-25 16:24:44 +0100 | [diff] [blame] | 400 | * <idx> is the index of the next sample fetch entry. It is used as private |
| 401 | * value. It is useless to initiate it. |
Thierry FOURNIER | 4d9a1d1 | 2014-12-08 14:49:19 +0100 | [diff] [blame] | 402 | * |
Tim Duesterhus | c555ee0 | 2018-01-25 16:24:44 +0100 | [diff] [blame] | 403 | * It returns always the new fetch_sample entry, and NULL when the end of |
Thierry FOURNIER | 4d9a1d1 | 2014-12-08 14:49:19 +0100 | [diff] [blame] | 404 | * the list is reached. |
| 405 | */ |
| 406 | struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx) |
| 407 | { |
| 408 | struct sample_fetch_kw_list *kwl; |
| 409 | struct sample_fetch *base; |
| 410 | |
| 411 | if (!current) { |
| 412 | /* Get first kwl entry. */ |
| 413 | kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list); |
| 414 | (*idx) = 0; |
| 415 | } else { |
| 416 | /* Get kwl corresponding to the curret entry. */ |
| 417 | base = current + 1 - (*idx); |
| 418 | kwl = container_of(base, struct sample_fetch_kw_list, kw); |
| 419 | } |
| 420 | |
| 421 | while (1) { |
| 422 | |
| 423 | /* Check if kwl is the last entry. */ |
| 424 | if (&kwl->list == &sample_fetches.list) |
| 425 | return NULL; |
| 426 | |
| 427 | /* idx contain the next keyword. If it is available, return it. */ |
| 428 | if (kwl->kw[*idx].kw) { |
| 429 | (*idx)++; |
| 430 | return &kwl->kw[(*idx)-1]; |
| 431 | } |
| 432 | |
| 433 | /* get next entry in the main list, and return NULL if the end is reached. */ |
| 434 | kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list); |
| 435 | |
| 436 | /* Set index to 0, ans do one other loop. */ |
| 437 | (*idx) = 0; |
| 438 | } |
| 439 | } |
| 440 | |
Thierry FOURNIER | 8fd1376 | 2015-03-10 23:56:48 +0100 | [diff] [blame] | 441 | /* This function browses the list of available converters. <current> is |
| 442 | * the last used converter. If it is the first call, it must set to NULL. |
| 443 | * <idx> is the index of the next converter entry. It is used as private |
| 444 | * value. It is useless to initiate it. |
| 445 | * |
| 446 | * It returns always the next sample_conv entry, and NULL when the end of |
| 447 | * the list is reached. |
| 448 | */ |
| 449 | struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx) |
| 450 | { |
| 451 | struct sample_conv_kw_list *kwl; |
| 452 | struct sample_conv *base; |
| 453 | |
| 454 | if (!current) { |
| 455 | /* Get first kwl entry. */ |
| 456 | kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list); |
| 457 | (*idx) = 0; |
| 458 | } else { |
| 459 | /* Get kwl corresponding to the curret entry. */ |
| 460 | base = current + 1 - (*idx); |
| 461 | kwl = container_of(base, struct sample_conv_kw_list, kw); |
| 462 | } |
| 463 | |
| 464 | while (1) { |
| 465 | /* Check if kwl is the last entry. */ |
| 466 | if (&kwl->list == &sample_convs.list) |
| 467 | return NULL; |
| 468 | |
| 469 | /* idx contain the next keyword. If it is available, return it. */ |
| 470 | if (kwl->kw[*idx].kw) { |
| 471 | (*idx)++; |
| 472 | return &kwl->kw[(*idx)-1]; |
| 473 | } |
| 474 | |
| 475 | /* get next entry in the main list, and return NULL if the end is reached. */ |
| 476 | kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list); |
| 477 | |
| 478 | /* Set index to 0, ans do one other loop. */ |
| 479 | (*idx) = 0; |
| 480 | } |
| 481 | } |
| 482 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 483 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 484 | * Returns the pointer on sample format conversion keyword structure identified by |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 485 | * string of <len> in buffer <kw>. |
| 486 | * |
| 487 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 488 | struct sample_conv *find_sample_conv(const char *kw, int len) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 489 | { |
| 490 | int index; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 491 | struct sample_conv_kw_list *kwl; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 492 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 493 | list_for_each_entry(kwl, &sample_convs.list, list) { |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 494 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 495 | if (strncmp(kwl->kw[index].kw, kw, len) == 0 && |
| 496 | kwl->kw[index].kw[len] == '\0') |
| 497 | return &kwl->kw[index]; |
| 498 | } |
| 499 | } |
| 500 | return NULL; |
| 501 | } |
| 502 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 503 | /******************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 504 | /* Sample casts functions */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 505 | /******************************************************************/ |
| 506 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 507 | static int c_ip2int(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 508 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 509 | smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 510 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 511 | return 1; |
| 512 | } |
| 513 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 514 | static int c_ip2str(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 515 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 516 | struct buffer *trash = get_trash_chunk(); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 517 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 518 | if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 519 | return 0; |
| 520 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 521 | trash->data = strlen(trash->area); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 522 | smp->data.u.str = *trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 523 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 524 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 525 | |
| 526 | return 1; |
| 527 | } |
| 528 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 529 | static int c_ip2ipv6(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 530 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 531 | v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 532 | smp->data.type = SMP_T_IPV6; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 533 | return 1; |
| 534 | } |
| 535 | |
Thierry FOURNIER | cc4d171 | 2015-07-24 09:04:56 +0200 | [diff] [blame] | 536 | static int c_ipv62ip(struct sample *smp) |
| 537 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 538 | if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6)) |
Thierry FOURNIER | cc4d171 | 2015-07-24 09:04:56 +0200 | [diff] [blame] | 539 | return 0; |
Tim Duesterhus | bf5ce02 | 2018-01-25 16:24:46 +0100 | [diff] [blame] | 540 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | cc4d171 | 2015-07-24 09:04:56 +0200 | [diff] [blame] | 541 | return 1; |
| 542 | } |
| 543 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 544 | static int c_ipv62str(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 545 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 546 | struct buffer *trash = get_trash_chunk(); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 547 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 548 | if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size)) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 549 | return 0; |
| 550 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 551 | trash->data = strlen(trash->area); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 552 | smp->data.u.str = *trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 553 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 554 | smp->flags &= ~SMP_F_CONST; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 555 | return 1; |
| 556 | } |
| 557 | |
| 558 | /* |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 559 | static int c_ipv62ip(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 560 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 561 | return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 562 | } |
| 563 | */ |
| 564 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 565 | static int c_int2ip(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 566 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 567 | smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 568 | smp->data.type = SMP_T_IPV4; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 569 | return 1; |
| 570 | } |
| 571 | |
Thierry FOURNIER | cc4d171 | 2015-07-24 09:04:56 +0200 | [diff] [blame] | 572 | static int c_int2ipv6(struct sample *smp) |
| 573 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 574 | smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint); |
| 575 | v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 576 | smp->data.type = SMP_T_IPV6; |
Thierry FOURNIER | cc4d171 | 2015-07-24 09:04:56 +0200 | [diff] [blame] | 577 | return 1; |
| 578 | } |
| 579 | |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 580 | static int c_str2addr(struct sample *smp) |
| 581 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 582 | if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) { |
| 583 | if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6)) |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 584 | return 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 585 | smp->data.type = SMP_T_IPV6; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 586 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 587 | return 1; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 588 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 589 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 590 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 591 | return 1; |
| 592 | } |
| 593 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 594 | static int c_str2ip(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 595 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 596 | if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 597 | return 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 598 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 599 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 600 | return 1; |
| 601 | } |
| 602 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 603 | static int c_str2ipv6(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 604 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 605 | if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6)) |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 606 | return 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 607 | smp->data.type = SMP_T_IPV6; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 608 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 609 | return 1; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 610 | } |
| 611 | |
Emeric Brun | 4b9e802 | 2014-11-03 18:17:10 +0100 | [diff] [blame] | 612 | /* |
| 613 | * The NULL char always enforces the end of string if it is met. |
| 614 | * Data is never changed, so we can ignore the CONST case |
Thierry FOURNIER | e87cac1 | 2014-03-12 15:07:59 +0100 | [diff] [blame] | 615 | */ |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 616 | static int c_bin2str(struct sample *smp) |
| 617 | { |
Emeric Brun | 4b9e802 | 2014-11-03 18:17:10 +0100 | [diff] [blame] | 618 | int i; |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 619 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 620 | for (i = 0; i < smp->data.u.str.data; i++) { |
| 621 | if (!smp->data.u.str.area[i]) { |
| 622 | smp->data.u.str.data = i; |
Thierry FOURNIER | e87cac1 | 2014-03-12 15:07:59 +0100 | [diff] [blame] | 623 | break; |
Emeric Brun | 4b9e802 | 2014-11-03 18:17:10 +0100 | [diff] [blame] | 624 | } |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 625 | } |
Christopher Faulet | 472ad51 | 2020-04-30 09:57:40 +0200 | [diff] [blame] | 626 | smp->data.type = SMP_T_STR; |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 627 | return 1; |
| 628 | } |
| 629 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 630 | static int c_int2str(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 631 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 632 | struct buffer *trash = get_trash_chunk(); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 633 | char *pos; |
| 634 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 635 | pos = lltoa_r(smp->data.u.sint, trash->area, trash->size); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 636 | if (!pos) |
| 637 | return 0; |
| 638 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 639 | trash->size = trash->size - (pos - trash->area); |
| 640 | trash->area = pos; |
| 641 | trash->data = strlen(pos); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 642 | smp->data.u.str = *trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 643 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 644 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 645 | return 1; |
| 646 | } |
| 647 | |
Joseph Herlant | 757f5ad | 2018-11-15 12:14:56 -0800 | [diff] [blame] | 648 | /* This function unconditionally duplicates data and removes the "const" flag. |
Willy Tarreau | ad63582 | 2016-08-08 19:21:09 +0200 | [diff] [blame] | 649 | * For strings and binary blocks, it also provides a known allocated size with |
| 650 | * a length that is capped to the size, and ensures a trailing zero is always |
| 651 | * appended for strings. This is necessary for some operations which may |
| 652 | * require to extend the length. It returns 0 if it fails, 1 on success. |
| 653 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 654 | int smp_dup(struct sample *smp) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 655 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 656 | struct buffer *trash; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 657 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 658 | switch (smp->data.type) { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 659 | case SMP_T_BOOL: |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 660 | case SMP_T_SINT: |
| 661 | case SMP_T_ADDR: |
| 662 | case SMP_T_IPV4: |
| 663 | case SMP_T_IPV6: |
| 664 | /* These type are not const. */ |
| 665 | break; |
Willy Tarreau | ad63582 | 2016-08-08 19:21:09 +0200 | [diff] [blame] | 666 | |
Christopher Faulet | ec10051 | 2017-07-24 15:38:41 +0200 | [diff] [blame] | 667 | case SMP_T_METH: |
| 668 | if (smp->data.u.meth.meth != HTTP_METH_OTHER) |
| 669 | break; |
| 670 | /* Fall through */ |
| 671 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 672 | case SMP_T_STR: |
Willy Tarreau | ad63582 | 2016-08-08 19:21:09 +0200 | [diff] [blame] | 673 | trash = get_trash_chunk(); |
Olivier Houchard | 4468f1c | 2018-12-07 15:23:41 +0100 | [diff] [blame] | 674 | trash->data = smp->data.type == SMP_T_STR ? |
| 675 | smp->data.u.str.data : smp->data.u.meth.str.data; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 676 | if (trash->data > trash->size - 1) |
| 677 | trash->data = trash->size - 1; |
Willy Tarreau | ad63582 | 2016-08-08 19:21:09 +0200 | [diff] [blame] | 678 | |
Olivier Houchard | 4468f1c | 2018-12-07 15:23:41 +0100 | [diff] [blame] | 679 | memcpy(trash->area, smp->data.type == SMP_T_STR ? |
| 680 | smp->data.u.str.area : smp->data.u.meth.str.area, |
| 681 | trash->data); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 682 | trash->area[trash->data] = 0; |
Willy Tarreau | ad63582 | 2016-08-08 19:21:09 +0200 | [diff] [blame] | 683 | smp->data.u.str = *trash; |
| 684 | break; |
| 685 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 686 | case SMP_T_BIN: |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 687 | trash = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 688 | trash->data = smp->data.u.str.data; |
| 689 | if (trash->data > trash->size) |
| 690 | trash->data = trash->size; |
Willy Tarreau | ad63582 | 2016-08-08 19:21:09 +0200 | [diff] [blame] | 691 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 692 | memcpy(trash->area, smp->data.u.str.area, trash->data); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 693 | smp->data.u.str = *trash; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 694 | break; |
Christopher Faulet | ec10051 | 2017-07-24 15:38:41 +0200 | [diff] [blame] | 695 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 696 | default: |
| 697 | /* Other cases are unexpected. */ |
| 698 | return 0; |
| 699 | } |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 700 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 701 | /* remove const flag */ |
| 702 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 703 | return 1; |
| 704 | } |
| 705 | |
Thierry FOURNIER | 0e9af55 | 2013-12-14 14:55:04 +0100 | [diff] [blame] | 706 | int c_none(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 707 | { |
| 708 | return 1; |
| 709 | } |
| 710 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 711 | static int c_str2int(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 712 | { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 713 | const char *str; |
| 714 | const char *end; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 715 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 716 | if (smp->data.u.str.data == 0) |
Thierry FOURNIER | 60bb020 | 2014-01-27 18:20:48 +0100 | [diff] [blame] | 717 | return 0; |
| 718 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 719 | str = smp->data.u.str.area; |
| 720 | end = smp->data.u.str.area + smp->data.u.str.data; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 721 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 722 | smp->data.u.sint = read_int64(&str, end); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 723 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 724 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 725 | return 1; |
| 726 | } |
| 727 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 728 | static int c_str2meth(struct sample *smp) |
| 729 | { |
| 730 | enum http_meth_t meth; |
| 731 | int len; |
| 732 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 733 | meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 734 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 735 | len = smp->data.u.str.data; |
| 736 | smp->data.u.meth.str.area = smp->data.u.str.area; |
| 737 | smp->data.u.meth.str.data = len; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 738 | } |
| 739 | else |
| 740 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 741 | smp->data.u.meth.meth = meth; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 742 | smp->data.type = SMP_T_METH; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 743 | return 1; |
| 744 | } |
| 745 | |
| 746 | static int c_meth2str(struct sample *smp) |
| 747 | { |
| 748 | int len; |
| 749 | enum http_meth_t meth; |
| 750 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 751 | if (smp->data.u.meth.meth == HTTP_METH_OTHER) { |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 752 | /* The method is unknown. Copy the original pointer. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 753 | len = smp->data.u.meth.str.data; |
| 754 | smp->data.u.str.area = smp->data.u.meth.str.area; |
| 755 | smp->data.u.str.data = len; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 756 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 757 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 758 | else if (smp->data.u.meth.meth < HTTP_METH_OTHER) { |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 759 | /* The method is known, copy the pointer containing the string. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 760 | meth = smp->data.u.meth.meth; |
Willy Tarreau | 35b51c6 | 2018-09-10 15:38:55 +0200 | [diff] [blame] | 761 | smp->data.u.str.area = http_known_methods[meth].ptr; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 762 | smp->data.u.str.data = http_known_methods[meth].len; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 763 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 764 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 765 | } |
| 766 | else { |
| 767 | /* Unknown method */ |
| 768 | return 0; |
| 769 | } |
| 770 | return 1; |
| 771 | } |
| 772 | |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 773 | static int c_addr2bin(struct sample *smp) |
| 774 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 775 | struct buffer *chk = get_trash_chunk(); |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 776 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 777 | if (smp->data.type == SMP_T_IPV4) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 778 | chk->data = 4; |
| 779 | memcpy(chk->area, &smp->data.u.ipv4, chk->data); |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 780 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 781 | else if (smp->data.type == SMP_T_IPV6) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 782 | chk->data = 16; |
| 783 | memcpy(chk->area, &smp->data.u.ipv6, chk->data); |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 784 | } |
| 785 | else |
| 786 | return 0; |
| 787 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 788 | smp->data.u.str = *chk; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 789 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 790 | return 1; |
| 791 | } |
| 792 | |
Willy Tarreau | bbfd1a2 | 2014-07-15 21:19:08 +0200 | [diff] [blame] | 793 | static int c_int2bin(struct sample *smp) |
| 794 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 795 | struct buffer *chk = get_trash_chunk(); |
Willy Tarreau | bbfd1a2 | 2014-07-15 21:19:08 +0200 | [diff] [blame] | 796 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 797 | *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint); |
| 798 | chk->data = 8; |
Willy Tarreau | bbfd1a2 | 2014-07-15 21:19:08 +0200 | [diff] [blame] | 799 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 800 | smp->data.u.str = *chk; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 801 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | bbfd1a2 | 2014-07-15 21:19:08 +0200 | [diff] [blame] | 802 | return 1; |
| 803 | } |
| 804 | |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 805 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 806 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 807 | /* Sample casts matrix: */ |
| 808 | /* sample_casts[from type][to type] */ |
| 809 | /* NULL pointer used for impossible sample casts */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 810 | /*****************************************************************/ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 811 | |
Thierry FOURNIER | 8af6ff1 | 2013-11-21 10:53:12 +0100 | [diff] [blame] | 812 | sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 813 | /* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */ |
| 814 | /* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, }, |
| 815 | /* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, }, |
Thierry FOURNIER | cc4d171 | 2015-07-24 09:04:56 +0200 | [diff] [blame] | 816 | /* SINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, c_int2ipv6, c_int2str, c_int2bin, NULL, }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 817 | /* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, }, |
| 818 | /* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, }, |
Thierry FOURNIER | cc4d171 | 2015-07-24 09:04:56 +0200 | [diff] [blame] | 819 | /* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 820 | /* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, }, |
| 821 | /* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, }, |
| 822 | /* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, } |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 823 | }; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 824 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 825 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 826 | * Parse a sample expression configuration: |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 827 | * fetch keyword followed by format conversion keywords. |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 828 | * Returns a pointer on allocated sample expression structure. |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 829 | * The caller must have set al->ctx. |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 830 | * If <endptr> is non-nul, it will be set to the first unparsed character |
| 831 | * (which may be the final '\0') on success. If it is nul, the expression |
| 832 | * must be properly terminated by a '\0' otherwise an error is reported. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 833 | */ |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 834 | struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err_msg, struct arg_list *al, char **endptr) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 835 | { |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 836 | const char *begw; /* beginning of word */ |
| 837 | const char *endw; /* end of word */ |
| 838 | const char *endt; /* end of term */ |
Remi Tricot-Le Breton | 54c8877 | 2021-01-12 14:55:12 +0100 | [diff] [blame] | 839 | struct sample_expr *expr = NULL; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 840 | struct sample_fetch *fetch; |
| 841 | struct sample_conv *conv; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 842 | unsigned long prev_type; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 843 | char *fkw = NULL; |
| 844 | char *ckw = NULL; |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 845 | int err_arg; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 846 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 847 | begw = str[*idx]; |
Willy Tarreau | ed2c662 | 2020-02-14 18:27:10 +0100 | [diff] [blame] | 848 | for (endw = begw; is_idchar(*endw); endw++) |
| 849 | ; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 850 | |
| 851 | if (endw == begw) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 852 | memprintf(err_msg, "missing fetch method"); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 853 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 854 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 855 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 856 | /* keep a copy of the current fetch keyword for error reporting */ |
| 857 | fkw = my_strndup(begw, endw - begw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 858 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 859 | fetch = find_sample_fetch(begw, endw - begw); |
| 860 | if (!fetch) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 861 | memprintf(err_msg, "unknown fetch method '%s'", fkw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 862 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 863 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 864 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 865 | /* At this point, we have : |
| 866 | * - begw : beginning of the keyword |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 867 | * - endw : end of the keyword, first character not part of keyword |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 868 | */ |
| 869 | |
| 870 | if (fetch->out_type >= SMP_TYPES) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 871 | memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 872 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 873 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 874 | prev_type = fetch->out_type; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 875 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 876 | expr = calloc(1, sizeof(*expr)); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 877 | if (!expr) |
| 878 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 879 | |
| 880 | LIST_INIT(&(expr->conv_exprs)); |
| 881 | expr->fetch = fetch; |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 882 | expr->arg_p = empty_arg_list; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 883 | |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 884 | /* Note that we call the argument parser even with an empty string, |
| 885 | * this allows it to automatically create entries for mandatory |
| 886 | * implicit arguments (eg: local proxy name). |
| 887 | */ |
| 888 | al->kw = expr->fetch->kw; |
| 889 | al->conv = NULL; |
Willy Tarreau | 80b53ff | 2020-02-14 08:40:37 +0100 | [diff] [blame] | 890 | if (make_arg_list(endw, -1, fetch->arg_mask, &expr->arg_p, err_msg, &endt, &err_arg, al) < 0) { |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 891 | memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg); |
| 892 | goto out_error; |
| 893 | } |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 894 | |
Willy Tarreau | 80b53ff | 2020-02-14 08:40:37 +0100 | [diff] [blame] | 895 | /* now endt is our first char not part of the arg list, typically the |
| 896 | * comma after the sample fetch name or after the closing parenthesis, |
| 897 | * or the NUL char. |
| 898 | */ |
| 899 | |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 900 | if (!expr->arg_p) { |
| 901 | expr->arg_p = empty_arg_list; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 902 | } |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 903 | else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) { |
| 904 | memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 905 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 906 | } |
| 907 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 908 | /* Now process the converters if any. We have two supported syntaxes |
| 909 | * for the converters, which can be combined : |
| 910 | * - comma-delimited list of converters just after the keyword and args ; |
| 911 | * - one converter per keyword |
| 912 | * The combination allows to have each keyword being a comma-delimited |
| 913 | * series of converters. |
| 914 | * |
| 915 | * We want to process the former first, then the latter. For this we start |
| 916 | * from the beginning of the supposed place in the exiting conv chain, which |
| 917 | * starts at the last comma (endt). |
| 918 | */ |
| 919 | |
| 920 | while (1) { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 921 | struct sample_conv_expr *conv_expr; |
Willy Tarreau | 46dfd78 | 2019-12-17 10:25:29 +0100 | [diff] [blame] | 922 | int err_arg; |
| 923 | int argcnt; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 924 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 925 | if (*endt && *endt != ',') { |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 926 | if (endptr) { |
| 927 | /* end found, let's stop here */ |
| 928 | break; |
| 929 | } |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 930 | if (ckw) |
Willy Tarreau | 97108e0 | 2016-11-25 07:33:24 +0100 | [diff] [blame] | 931 | memprintf(err_msg, "missing comma after converter '%s'", ckw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 932 | else |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 933 | memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 934 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 935 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 936 | |
Willy Tarreau | 80b53ff | 2020-02-14 08:40:37 +0100 | [diff] [blame] | 937 | /* FIXME: how long should we support such idiocies ? Maybe we |
| 938 | * should already warn ? |
| 939 | */ |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 940 | while (*endt == ',') /* then trailing commas */ |
| 941 | endt++; |
| 942 | |
Willy Tarreau | 97108e0 | 2016-11-25 07:33:24 +0100 | [diff] [blame] | 943 | begw = endt; /* start of converter */ |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 944 | |
| 945 | if (!*begw) { |
| 946 | /* none ? skip to next string */ |
| 947 | (*idx)++; |
| 948 | begw = str[*idx]; |
| 949 | if (!begw || !*begw) |
| 950 | break; |
| 951 | } |
| 952 | |
Willy Tarreau | ed2c662 | 2020-02-14 18:27:10 +0100 | [diff] [blame] | 953 | for (endw = begw; is_idchar(*endw); endw++) |
| 954 | ; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 955 | |
| 956 | free(ckw); |
| 957 | ckw = my_strndup(begw, endw - begw); |
| 958 | |
| 959 | conv = find_sample_conv(begw, endw - begw); |
| 960 | if (!conv) { |
| 961 | /* we found an isolated keyword that we don't know, it's not ours */ |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 962 | if (begw == str[*idx]) { |
| 963 | endt = begw; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 964 | break; |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 965 | } |
Willy Tarreau | 97108e0 | 2016-11-25 07:33:24 +0100 | [diff] [blame] | 966 | memprintf(err_msg, "unknown converter '%s'", ckw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 967 | goto out_error; |
| 968 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 969 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 970 | if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) { |
Willy Tarreau | 97108e0 | 2016-11-25 07:33:24 +0100 | [diff] [blame] | 971 | memprintf(err_msg, "returns type of converter '%s' is unknown", ckw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 972 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 973 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 974 | |
| 975 | /* If impossible type conversion */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 976 | if (!sample_casts[prev_type][conv->in_type]) { |
Willy Tarreau | 97108e0 | 2016-11-25 07:33:24 +0100 | [diff] [blame] | 977 | memprintf(err_msg, "converter '%s' cannot be applied", ckw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 978 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 979 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 980 | |
| 981 | prev_type = conv->out_type; |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 982 | conv_expr = calloc(1, sizeof(*conv_expr)); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 983 | if (!conv_expr) |
| 984 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 985 | |
| 986 | LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list)); |
| 987 | conv_expr->conv = conv; |
| 988 | |
Willy Tarreau | 46dfd78 | 2019-12-17 10:25:29 +0100 | [diff] [blame] | 989 | al->kw = expr->fetch->kw; |
| 990 | al->conv = conv_expr->conv->kw; |
Willy Tarreau | 80b53ff | 2020-02-14 08:40:37 +0100 | [diff] [blame] | 991 | argcnt = make_arg_list(endw, -1, conv->arg_mask, &conv_expr->arg_p, err_msg, &endt, &err_arg, al); |
Willy Tarreau | 46dfd78 | 2019-12-17 10:25:29 +0100 | [diff] [blame] | 992 | if (argcnt < 0) { |
| 993 | memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg); |
| 994 | goto out_error; |
| 995 | } |
Willy Tarreau | 9e92d32 | 2010-01-26 17:58:06 +0100 | [diff] [blame] | 996 | |
Willy Tarreau | 46dfd78 | 2019-12-17 10:25:29 +0100 | [diff] [blame] | 997 | if (argcnt && !conv->arg_mask) { |
| 998 | memprintf(err_msg, "converter '%s' does not support any args", ckw); |
| 999 | goto out_error; |
| 1000 | } |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 1001 | |
Willy Tarreau | 46dfd78 | 2019-12-17 10:25:29 +0100 | [diff] [blame] | 1002 | if (!conv_expr->arg_p) |
| 1003 | conv_expr->arg_p = empty_arg_list; |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 1004 | |
Willy Tarreau | 46dfd78 | 2019-12-17 10:25:29 +0100 | [diff] [blame] | 1005 | if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) { |
| 1006 | memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1007 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1008 | } |
| 1009 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1010 | |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 1011 | if (endptr) { |
| 1012 | /* end found, let's stop here */ |
| 1013 | *endptr = (char *)endt; |
| 1014 | } |
| 1015 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 1016 | out: |
| 1017 | free(fkw); |
| 1018 | free(ckw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1019 | return expr; |
| 1020 | |
| 1021 | out_error: |
Remi Tricot-Le Breton | 54c8877 | 2021-01-12 14:55:12 +0100 | [diff] [blame] | 1022 | release_sample_expr(expr); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 1023 | expr = NULL; |
| 1024 | goto out; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1028 | * Process a fetch + format conversion of defined by the sample expression <expr> |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 1029 | * on request or response considering the <opt> parameter. |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1030 | * Returns a pointer on a typed sample structure containing the result or NULL if |
| 1031 | * sample is not found or when format conversion failed. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1032 | * If <p> is not null, function returns results in structure pointed by <p>. |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1033 | * If <p> is null, functions returns a pointer on a static sample structure. |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 1034 | * |
| 1035 | * Note: the fetch functions are required to properly set the return type. The |
| 1036 | * conversion functions must do so too. However the cast functions do not need |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1037 | * to since they're made to cast multiple types according to what is required. |
Willy Tarreau | 6bcb0a8 | 2014-07-30 08:56:35 +0200 | [diff] [blame] | 1038 | * |
| 1039 | * The caller may indicate in <opt> if it considers the result final or not. |
| 1040 | * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify |
| 1041 | * if the result is stable or not, according to the following table : |
| 1042 | * |
| 1043 | * return MAY_CHANGE FINAL Meaning for the sample |
| 1044 | * NULL 0 * Not present and will never be (eg: header) |
| 1045 | * NULL 1 0 Not present yet, could change (eg: POST param) |
| 1046 | * NULL 1 1 Not present yet, will not change anymore |
| 1047 | * smp 0 * Present and will not change (eg: header) |
| 1048 | * smp 1 0 Present, may change (eg: request length) |
| 1049 | * smp 1 1 Present, last known value (eg: request length) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1050 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1051 | struct sample *sample_process(struct proxy *px, struct session *sess, |
| 1052 | struct stream *strm, unsigned int opt, |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1053 | struct sample_expr *expr, struct sample *p) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1054 | { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1055 | struct sample_conv_expr *conv_expr; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1056 | |
Willy Tarreau | 18387e2 | 2013-07-25 12:02:38 +0200 | [diff] [blame] | 1057 | if (p == NULL) { |
Willy Tarreau | b4a88f0 | 2012-04-23 21:35:11 +0200 | [diff] [blame] | 1058 | p = &temp_smp; |
Willy Tarreau | 6c616e0 | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 1059 | memset(p, 0, sizeof(*p)); |
Willy Tarreau | 18387e2 | 2013-07-25 12:02:38 +0200 | [diff] [blame] | 1060 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1061 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 1062 | smp_set_owner(p, px, sess, strm, opt); |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 1063 | if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1064 | return NULL; |
| 1065 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1066 | list_for_each_entry(conv_expr, &expr->conv_exprs, list) { |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 1067 | /* we want to ensure that p->type can be casted into |
| 1068 | * conv_expr->conv->in_type. We have 3 possibilities : |
| 1069 | * - NULL => not castable. |
| 1070 | * - c_none => nothing to do (let's optimize it) |
| 1071 | * - other => apply cast and prepare to fail |
| 1072 | */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1073 | if (!sample_casts[p->data.type][conv_expr->conv->in_type]) |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 1074 | return NULL; |
| 1075 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1076 | if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none && |
| 1077 | !sample_casts[p->data.type][conv_expr->conv->in_type](p)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1078 | return NULL; |
| 1079 | |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 1080 | /* OK cast succeeded */ |
| 1081 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1082 | if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1083 | return NULL; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1084 | } |
| 1085 | return p; |
| 1086 | } |
| 1087 | |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1088 | /* |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1089 | * Resolve all remaining arguments in proxy <p>. Returns the number of |
| 1090 | * errors or 0 if everything is fine. |
| 1091 | */ |
| 1092 | int smp_resolve_args(struct proxy *p) |
| 1093 | { |
| 1094 | struct arg_list *cur, *bak; |
| 1095 | const char *ctx, *where; |
| 1096 | const char *conv_ctx, *conv_pre, *conv_pos; |
| 1097 | struct userlist *ul; |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1098 | struct my_regex *reg; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1099 | struct arg *arg; |
| 1100 | int cfgerr = 0; |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1101 | int rflags; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1102 | |
| 1103 | list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) { |
| 1104 | struct proxy *px; |
| 1105 | struct server *srv; |
Frédéric Lécaille | 1b8e68e | 2019-03-14 07:07:41 +0100 | [diff] [blame] | 1106 | struct stktable *t; |
| 1107 | char *pname, *sname, *stktname; |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1108 | char *err; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1109 | |
| 1110 | arg = cur->arg; |
| 1111 | |
| 1112 | /* prepare output messages */ |
| 1113 | conv_pre = conv_pos = conv_ctx = ""; |
| 1114 | if (cur->conv) { |
| 1115 | conv_ctx = cur->conv; |
| 1116 | conv_pre = "conversion keyword '"; |
| 1117 | conv_pos = "' for "; |
| 1118 | } |
| 1119 | |
| 1120 | where = "in"; |
| 1121 | ctx = "sample fetch keyword"; |
| 1122 | switch (cur->ctx) { |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1123 | case ARGC_STK: where = "in stick rule in"; break; |
| 1124 | case ARGC_TRK: where = "in tracking rule in"; break; |
| 1125 | case ARGC_LOG: where = "in log-format string in"; break; |
| 1126 | case ARGC_LOGSD: where = "in log-format-sd string in"; break; |
| 1127 | case ARGC_HRQ: where = "in http-request header format string in"; break; |
| 1128 | case ARGC_HRS: where = "in http-response header format string in"; break; |
| 1129 | case ARGC_UIF: where = "in unique-id-format string in"; break; |
| 1130 | case ARGC_RDR: where = "in redirect format string in"; break; |
| 1131 | case ARGC_CAP: where = "in capture rule in"; break; |
| 1132 | case ARGC_ACL: ctx = "ACL keyword"; break; |
| 1133 | case ARGC_SRV: where = "in server directive in"; break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1134 | case ARGC_SPOE: where = "in spoe-message directive in"; break; |
Christopher Faulet | 3b967c1 | 2020-05-15 15:47:44 +0200 | [diff] [blame] | 1135 | case ARGC_HERR: where = "in http-error directive in"; break; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | /* set a few default settings */ |
| 1139 | px = p; |
| 1140 | pname = p->id; |
| 1141 | |
| 1142 | switch (arg->type) { |
| 1143 | case ARGT_SRV: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1144 | if (!arg->data.str.data) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1145 | ha_alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1146 | cur->file, cur->line, |
| 1147 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1148 | cfgerr++; |
| 1149 | continue; |
| 1150 | } |
| 1151 | |
| 1152 | /* we support two formats : "bck/srv" and "srv" */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1153 | sname = strrchr(arg->data.str.area, '/'); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1154 | |
| 1155 | if (sname) { |
| 1156 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1157 | pname = arg->data.str.area; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1158 | |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 1159 | px = proxy_be_by_name(pname); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1160 | if (!px) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1161 | ha_alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1162 | cur->file, cur->line, pname, |
| 1163 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1164 | cfgerr++; |
| 1165 | break; |
| 1166 | } |
| 1167 | } |
| 1168 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1169 | sname = arg->data.str.area; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1170 | |
| 1171 | srv = findserver(px, sname); |
| 1172 | if (!srv) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1173 | ha_alert("parsing [%s:%d] : unable to find server '%s' in proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1174 | cur->file, cur->line, sname, pname, |
| 1175 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1176 | cfgerr++; |
| 1177 | break; |
| 1178 | } |
| 1179 | |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1180 | chunk_destroy(&arg->data.str); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1181 | arg->unresolved = 0; |
| 1182 | arg->data.srv = srv; |
| 1183 | break; |
| 1184 | |
| 1185 | case ARGT_FE: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1186 | if (arg->data.str.data) { |
| 1187 | pname = arg->data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 1188 | px = proxy_fe_by_name(pname); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | if (!px) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1192 | ha_alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1193 | cur->file, cur->line, pname, |
| 1194 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1195 | cfgerr++; |
| 1196 | break; |
| 1197 | } |
| 1198 | |
| 1199 | if (!(px->cap & PR_CAP_FE)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1200 | ha_alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n", |
| 1201 | cur->file, cur->line, pname, |
| 1202 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1203 | cfgerr++; |
| 1204 | break; |
| 1205 | } |
| 1206 | |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1207 | chunk_destroy(&arg->data.str); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1208 | arg->unresolved = 0; |
| 1209 | arg->data.prx = px; |
| 1210 | break; |
| 1211 | |
| 1212 | case ARGT_BE: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1213 | if (arg->data.str.data) { |
| 1214 | pname = arg->data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 1215 | px = proxy_be_by_name(pname); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | if (!px) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1219 | ha_alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1220 | cur->file, cur->line, pname, |
| 1221 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1222 | cfgerr++; |
| 1223 | break; |
| 1224 | } |
| 1225 | |
| 1226 | if (!(px->cap & PR_CAP_BE)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1227 | ha_alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n", |
| 1228 | cur->file, cur->line, pname, |
| 1229 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1230 | cfgerr++; |
| 1231 | break; |
| 1232 | } |
| 1233 | |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1234 | chunk_destroy(&arg->data.str); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1235 | arg->unresolved = 0; |
| 1236 | arg->data.prx = px; |
| 1237 | break; |
| 1238 | |
| 1239 | case ARGT_TAB: |
Frédéric Lécaille | 9417f45 | 2019-06-20 09:31:04 +0200 | [diff] [blame] | 1240 | if (arg->data.str.data) |
| 1241 | stktname = arg->data.str.area; |
| 1242 | else |
| 1243 | stktname = px->id; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1244 | |
Frédéric Lécaille | 1b8e68e | 2019-03-14 07:07:41 +0100 | [diff] [blame] | 1245 | t = stktable_find_by_name(stktname); |
| 1246 | if (!t) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1247 | ha_alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
Frédéric Lécaille | 1b8e68e | 2019-03-14 07:07:41 +0100 | [diff] [blame] | 1248 | cur->file, cur->line, stktname, |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1249 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1250 | cfgerr++; |
| 1251 | break; |
| 1252 | } |
| 1253 | |
Frédéric Lécaille | 1b8e68e | 2019-03-14 07:07:41 +0100 | [diff] [blame] | 1254 | if (!t->size) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1255 | ha_alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
Frédéric Lécaille | 1b8e68e | 2019-03-14 07:07:41 +0100 | [diff] [blame] | 1256 | cur->file, cur->line, stktname, |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1257 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1258 | cfgerr++; |
| 1259 | break; |
| 1260 | } |
| 1261 | |
Frédéric Lécaille | 1b8e68e | 2019-03-14 07:07:41 +0100 | [diff] [blame] | 1262 | if (t->proxy && (p->bind_proc & ~t->proxy->bind_proc)) { |
Willy Tarreau | 151e1ca | 2019-02-05 11:38:38 +0100 | [diff] [blame] | 1263 | ha_alert("parsing [%s:%d] : stick-table '%s' not present on all processes covered by proxy '%s'.\n", |
Frédéric Lécaille | 1b8e68e | 2019-03-14 07:07:41 +0100 | [diff] [blame] | 1264 | cur->file, cur->line, t->proxy->id, p->id); |
Willy Tarreau | 1a0fe3b | 2019-02-06 10:25:07 +0100 | [diff] [blame] | 1265 | cfgerr++; |
| 1266 | break; |
Willy Tarreau | 151e1ca | 2019-02-05 11:38:38 +0100 | [diff] [blame] | 1267 | } |
| 1268 | |
Frédéric Lécaille | be36793 | 2019-08-07 09:28:39 +0200 | [diff] [blame] | 1269 | if (!in_proxies_list(t->proxies_list, p)) { |
Frédéric Lécaille | 015e4d7 | 2019-03-19 14:55:01 +0100 | [diff] [blame] | 1270 | p->next_stkt_ref = t->proxies_list; |
| 1271 | t->proxies_list = p; |
| 1272 | } |
| 1273 | |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1274 | chunk_destroy(&arg->data.str); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1275 | arg->unresolved = 0; |
Frédéric Lécaille | 1b8e68e | 2019-03-14 07:07:41 +0100 | [diff] [blame] | 1276 | arg->data.t = t; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1277 | break; |
| 1278 | |
| 1279 | case ARGT_USR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1280 | if (!arg->data.str.data) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1281 | ha_alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1282 | cur->file, cur->line, |
| 1283 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1284 | cfgerr++; |
| 1285 | break; |
| 1286 | } |
| 1287 | |
| 1288 | if (p->uri_auth && p->uri_auth->userlist && |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1289 | !strcmp(p->uri_auth->userlist->name, arg->data.str.area)) |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1290 | ul = p->uri_auth->userlist; |
| 1291 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1292 | ul = auth_find_userlist(arg->data.str.area); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1293 | |
| 1294 | if (!ul) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1295 | ha_alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1296 | cur->file, cur->line, |
| 1297 | arg->data.str.area, |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1298 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1299 | cfgerr++; |
| 1300 | break; |
| 1301 | } |
| 1302 | |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1303 | chunk_destroy(&arg->data.str); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1304 | arg->unresolved = 0; |
| 1305 | arg->data.usr = ul; |
| 1306 | break; |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1307 | |
| 1308 | case ARGT_REG: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1309 | if (!arg->data.str.data) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1310 | ha_alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1311 | cur->file, cur->line, |
| 1312 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1313 | cfgerr++; |
| 1314 | continue; |
| 1315 | } |
| 1316 | |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1317 | rflags = 0; |
| 1318 | rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0; |
| 1319 | err = NULL; |
| 1320 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1321 | if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1322 | ha_alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n", |
| 1323 | cur->file, cur->line, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1324 | arg->data.str.area, |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1325 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err); |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1326 | cfgerr++; |
| 1327 | continue; |
| 1328 | } |
| 1329 | |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1330 | chunk_destroy(&arg->data.str); |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1331 | arg->unresolved = 0; |
| 1332 | arg->data.reg = reg; |
| 1333 | break; |
| 1334 | |
| 1335 | |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | LIST_DEL(&cur->list); |
| 1339 | free(cur); |
| 1340 | } /* end of args processing */ |
| 1341 | |
| 1342 | return cfgerr; |
| 1343 | } |
| 1344 | |
| 1345 | /* |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1346 | * Process a fetch + format conversion as defined by the sample expression |
| 1347 | * <expr> on request or response considering the <opt> parameter. The output is |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 1348 | * not explicitly set to <smp_type>, but shall be compatible with it as |
| 1349 | * specified by 'sample_casts' table. If a stable sample can be fetched, or an |
| 1350 | * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1351 | * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found |
| 1352 | * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is |
| 1353 | * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to |
| 1354 | * take actions (eg: wait longer). If a sample could not be found or could not |
Willy Tarreau | 6bcb0a8 | 2014-07-30 08:56:35 +0200 | [diff] [blame] | 1355 | * be converted, NULL is returned. The caller MUST NOT use the sample if the |
| 1356 | * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is |
| 1357 | * still hope to get it after waiting longer, and is not converted to string. |
| 1358 | * The possible output combinations are the following : |
| 1359 | * |
| 1360 | * return MAY_CHANGE FINAL Meaning for the sample |
| 1361 | * NULL * * Not present and will never be (eg: header) |
| 1362 | * smp 0 * Final value converted (eg: header) |
| 1363 | * smp 1 0 Not present yet, may appear later (eg: header) |
| 1364 | * smp 1 1 never happens (either flag is cleared on output) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1365 | */ |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 1366 | struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess, |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1367 | struct stream *strm, unsigned int opt, |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 1368 | struct sample_expr *expr, int smp_type) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1369 | { |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1370 | struct sample *smp = &temp_smp; |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1371 | |
Willy Tarreau | 6c616e0 | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 1372 | memset(smp, 0, sizeof(*smp)); |
| 1373 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1374 | if (!sample_process(px, sess, strm, opt, expr, smp)) { |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1375 | if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL)) |
| 1376 | return smp; |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1377 | return NULL; |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1378 | } |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1379 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1380 | if (!sample_casts[smp->data.type][smp_type]) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1381 | return NULL; |
| 1382 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1383 | if (!sample_casts[smp->data.type][smp_type](smp)) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1384 | return NULL; |
| 1385 | |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1386 | smp->flags &= ~SMP_F_MAY_CHANGE; |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1387 | return smp; |
| 1388 | } |
| 1389 | |
Christopher Faulet | 476e5d0 | 2016-10-26 11:34:47 +0200 | [diff] [blame] | 1390 | static void release_sample_arg(struct arg *p) |
| 1391 | { |
| 1392 | struct arg *p_back = p; |
| 1393 | |
| 1394 | if (!p) |
| 1395 | return; |
| 1396 | |
| 1397 | while (p->type != ARGT_STOP) { |
| 1398 | if (p->type == ARGT_STR || p->unresolved) { |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1399 | chunk_destroy(&p->data.str); |
Christopher Faulet | 476e5d0 | 2016-10-26 11:34:47 +0200 | [diff] [blame] | 1400 | p->unresolved = 0; |
| 1401 | } |
| 1402 | else if (p->type == ARGT_REG) { |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1403 | regex_free(p->data.reg); |
| 1404 | p->data.reg = NULL; |
Christopher Faulet | 476e5d0 | 2016-10-26 11:34:47 +0200 | [diff] [blame] | 1405 | } |
| 1406 | p++; |
| 1407 | } |
| 1408 | |
| 1409 | if (p_back != empty_arg_list) |
| 1410 | free(p_back); |
| 1411 | } |
| 1412 | |
| 1413 | void release_sample_expr(struct sample_expr *expr) |
| 1414 | { |
| 1415 | struct sample_conv_expr *conv_expr, *conv_exprb; |
| 1416 | |
| 1417 | if (!expr) |
| 1418 | return; |
| 1419 | |
Tim Duesterhus | 867cd98 | 2020-07-04 11:49:39 +0200 | [diff] [blame] | 1420 | list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list) { |
| 1421 | LIST_DEL(&conv_expr->list); |
Christopher Faulet | 476e5d0 | 2016-10-26 11:34:47 +0200 | [diff] [blame] | 1422 | release_sample_arg(conv_expr->arg_p); |
Tim Duesterhus | 867cd98 | 2020-07-04 11:49:39 +0200 | [diff] [blame] | 1423 | free(conv_expr); |
| 1424 | } |
| 1425 | |
Christopher Faulet | 476e5d0 | 2016-10-26 11:34:47 +0200 | [diff] [blame] | 1426 | release_sample_arg(expr->arg_p); |
| 1427 | free(expr); |
| 1428 | } |
| 1429 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1430 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1431 | /* Sample format convert functions */ |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 1432 | /* These functions set the data type on return. */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1433 | /*****************************************************************/ |
| 1434 | |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1435 | static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private) |
| 1436 | { |
| 1437 | int i; |
| 1438 | struct sample tmp; |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 1439 | struct buffer *buf; |
| 1440 | struct sink *sink; |
| 1441 | struct ist line; |
| 1442 | char *pfx; |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1443 | |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 1444 | buf = alloc_trash_chunk(); |
| 1445 | if (!buf) |
| 1446 | goto end; |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1447 | |
Christopher Faulet | b45bf8e | 2020-08-07 14:00:23 +0200 | [diff] [blame] | 1448 | sink = (struct sink *)arg_p[1].data.ptr; |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 1449 | BUG_ON(!sink); |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1450 | |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 1451 | pfx = arg_p[0].data.str.area; |
| 1452 | BUG_ON(!pfx); |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1453 | |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 1454 | chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]); |
| 1455 | if (!sample_casts[smp->data.type][SMP_T_STR]) |
| 1456 | goto nocast; |
| 1457 | |
| 1458 | /* Copy sample fetch. This puts the sample as const, the |
| 1459 | * cast will copy data if a transformation is required. |
| 1460 | */ |
| 1461 | memcpy(&tmp, smp, sizeof(struct sample)); |
| 1462 | tmp.flags = SMP_F_CONST; |
| 1463 | |
| 1464 | if (!sample_casts[smp->data.type][SMP_T_STR](&tmp)) |
| 1465 | goto nocast; |
| 1466 | |
| 1467 | /* Display the displayable chars*. */ |
| 1468 | b_putchr(buf, '<'); |
| 1469 | for (i = 0; i < tmp.data.u.str.data; i++) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1470 | if (isprint((unsigned char)tmp.data.u.str.area[i])) |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 1471 | b_putchr(buf, tmp.data.u.str.area[i]); |
| 1472 | else |
| 1473 | b_putchr(buf, '.'); |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1474 | } |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 1475 | b_putchr(buf, '>'); |
| 1476 | |
| 1477 | done: |
| 1478 | line = ist2(buf->area, buf->data); |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1479 | sink_write(sink, &line, 1, 0, 0, NULL); |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 1480 | end: |
| 1481 | free_trash_chunk(buf); |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1482 | return 1; |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 1483 | nocast: |
| 1484 | chunk_appendf(buf, "(undisplayable)"); |
| 1485 | goto done; |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1486 | } |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 1487 | |
| 1488 | // This function checks the "debug" converter's arguments. |
| 1489 | static int smp_check_debug(struct arg *args, struct sample_conv *conv, |
| 1490 | const char *file, int line, char **err) |
| 1491 | { |
| 1492 | const char *name = "buf0"; |
| 1493 | struct sink *sink = NULL; |
| 1494 | |
| 1495 | if (args[0].type != ARGT_STR) { |
| 1496 | /* optional prefix */ |
| 1497 | args[0].data.str.area = ""; |
| 1498 | args[0].data.str.data = 0; |
| 1499 | } |
| 1500 | |
| 1501 | if (args[1].type == ARGT_STR) |
| 1502 | name = args[1].data.str.area; |
| 1503 | |
| 1504 | sink = sink_find(name); |
| 1505 | if (!sink) { |
| 1506 | memprintf(err, "No such sink '%s'", name); |
| 1507 | return 0; |
| 1508 | } |
| 1509 | |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1510 | chunk_destroy(&args[1].data.str); |
Christopher Faulet | b45bf8e | 2020-08-07 14:00:23 +0200 | [diff] [blame] | 1511 | args[1].type = ARGT_PTR; |
| 1512 | args[1].data.ptr = sink; |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 1513 | return 1; |
| 1514 | } |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1515 | |
Holger Just | 1bfc24b | 2017-05-06 00:56:53 +0200 | [diff] [blame] | 1516 | static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private) |
| 1517 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1518 | struct buffer *trash = get_trash_chunk(); |
Holger Just | 1bfc24b | 2017-05-06 00:56:53 +0200 | [diff] [blame] | 1519 | int bin_len; |
| 1520 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1521 | trash->data = 0; |
| 1522 | bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data, |
| 1523 | trash->area, trash->size); |
Holger Just | 1bfc24b | 2017-05-06 00:56:53 +0200 | [diff] [blame] | 1524 | if (bin_len < 0) |
| 1525 | return 0; |
| 1526 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1527 | trash->data = bin_len; |
Holger Just | 1bfc24b | 2017-05-06 00:56:53 +0200 | [diff] [blame] | 1528 | smp->data.u.str = *trash; |
| 1529 | smp->data.type = SMP_T_BIN; |
| 1530 | smp->flags &= ~SMP_F_CONST; |
| 1531 | return 1; |
| 1532 | } |
| 1533 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1534 | static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private) |
Emeric Brun | 53d1a98 | 2014-04-30 18:21:37 +0200 | [diff] [blame] | 1535 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1536 | struct buffer *trash = get_trash_chunk(); |
Emeric Brun | 53d1a98 | 2014-04-30 18:21:37 +0200 | [diff] [blame] | 1537 | int b64_len; |
| 1538 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1539 | trash->data = 0; |
| 1540 | b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data, |
| 1541 | trash->area, trash->size); |
Emeric Brun | 53d1a98 | 2014-04-30 18:21:37 +0200 | [diff] [blame] | 1542 | if (b64_len < 0) |
| 1543 | return 0; |
| 1544 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1545 | trash->data = b64_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1546 | smp->data.u.str = *trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1547 | smp->data.type = SMP_T_STR; |
Emeric Brun | 53d1a98 | 2014-04-30 18:21:37 +0200 | [diff] [blame] | 1548 | smp->flags &= ~SMP_F_CONST; |
| 1549 | return 1; |
| 1550 | } |
| 1551 | |
Dragan Dosen | 6e5a9ca | 2017-10-24 09:18:23 +0200 | [diff] [blame] | 1552 | static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private) |
| 1553 | { |
| 1554 | blk_SHA_CTX ctx; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1555 | struct buffer *trash = get_trash_chunk(); |
Dragan Dosen | 6e5a9ca | 2017-10-24 09:18:23 +0200 | [diff] [blame] | 1556 | |
| 1557 | memset(&ctx, 0, sizeof(ctx)); |
| 1558 | |
| 1559 | blk_SHA1_Init(&ctx); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1560 | blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data); |
| 1561 | blk_SHA1_Final((unsigned char *) trash->area, &ctx); |
Dragan Dosen | 6e5a9ca | 2017-10-24 09:18:23 +0200 | [diff] [blame] | 1562 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1563 | trash->data = 20; |
Dragan Dosen | 6e5a9ca | 2017-10-24 09:18:23 +0200 | [diff] [blame] | 1564 | smp->data.u.str = *trash; |
| 1565 | smp->data.type = SMP_T_BIN; |
| 1566 | smp->flags &= ~SMP_F_CONST; |
| 1567 | return 1; |
| 1568 | } |
| 1569 | |
Tim Duesterhus | d437630 | 2019-06-17 12:41:44 +0200 | [diff] [blame] | 1570 | #ifdef USE_OPENSSL |
Tim Duesterhus | cd37324 | 2019-12-17 12:31:20 +0100 | [diff] [blame] | 1571 | static int smp_check_sha2(struct arg *args, struct sample_conv *conv, |
| 1572 | const char *file, int line, char **err) |
| 1573 | { |
| 1574 | if (args[0].type == ARGT_STOP) |
| 1575 | return 1; |
| 1576 | if (args[0].type != ARGT_SINT) { |
| 1577 | memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]); |
| 1578 | return 0; |
| 1579 | } |
| 1580 | |
| 1581 | switch (args[0].data.sint) { |
| 1582 | case 224: |
| 1583 | case 256: |
| 1584 | case 384: |
| 1585 | case 512: |
| 1586 | /* this is okay */ |
| 1587 | return 1; |
| 1588 | default: |
| 1589 | memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint); |
| 1590 | return 0; |
| 1591 | } |
| 1592 | } |
| 1593 | |
Tim Duesterhus | d437630 | 2019-06-17 12:41:44 +0200 | [diff] [blame] | 1594 | static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private) |
| 1595 | { |
| 1596 | struct buffer *trash = get_trash_chunk(); |
| 1597 | int bits = 256; |
| 1598 | if (arg_p && arg_p->data.sint) |
| 1599 | bits = arg_p->data.sint; |
| 1600 | |
| 1601 | switch (bits) { |
| 1602 | case 224: { |
| 1603 | SHA256_CTX ctx; |
| 1604 | |
| 1605 | memset(&ctx, 0, sizeof(ctx)); |
| 1606 | |
| 1607 | SHA224_Init(&ctx); |
| 1608 | SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data); |
| 1609 | SHA224_Final((unsigned char *) trash->area, &ctx); |
| 1610 | trash->data = SHA224_DIGEST_LENGTH; |
| 1611 | break; |
| 1612 | } |
| 1613 | case 256: { |
| 1614 | SHA256_CTX ctx; |
| 1615 | |
| 1616 | memset(&ctx, 0, sizeof(ctx)); |
| 1617 | |
| 1618 | SHA256_Init(&ctx); |
| 1619 | SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data); |
| 1620 | SHA256_Final((unsigned char *) trash->area, &ctx); |
| 1621 | trash->data = SHA256_DIGEST_LENGTH; |
| 1622 | break; |
| 1623 | } |
| 1624 | case 384: { |
| 1625 | SHA512_CTX ctx; |
| 1626 | |
| 1627 | memset(&ctx, 0, sizeof(ctx)); |
| 1628 | |
| 1629 | SHA384_Init(&ctx); |
| 1630 | SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data); |
| 1631 | SHA384_Final((unsigned char *) trash->area, &ctx); |
| 1632 | trash->data = SHA384_DIGEST_LENGTH; |
| 1633 | break; |
| 1634 | } |
| 1635 | case 512: { |
| 1636 | SHA512_CTX ctx; |
| 1637 | |
| 1638 | memset(&ctx, 0, sizeof(ctx)); |
| 1639 | |
| 1640 | SHA512_Init(&ctx); |
| 1641 | SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data); |
| 1642 | SHA512_Final((unsigned char *) trash->area, &ctx); |
| 1643 | trash->data = SHA512_DIGEST_LENGTH; |
| 1644 | break; |
| 1645 | } |
| 1646 | default: |
| 1647 | return 0; |
| 1648 | } |
| 1649 | |
| 1650 | smp->data.u.str = *trash; |
| 1651 | smp->data.type = SMP_T_BIN; |
| 1652 | smp->flags &= ~SMP_F_CONST; |
| 1653 | return 1; |
| 1654 | } |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1655 | |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1656 | /* This function returns a sample struct filled with an <arg> content. |
| 1657 | * If the <arg> contains a string, it is returned in the sample flagged as |
| 1658 | * SMP_F_CONST. If the <arg> contains a variable descriptor, the sample is |
| 1659 | * filled with the content of the variable by using vars_get_by_desc(). |
| 1660 | * |
| 1661 | * Keep in mind that the sample content may be written to a pre-allocated |
| 1662 | * trash chunk as returned by get_trash_chunk(). |
| 1663 | * |
| 1664 | * This function returns 0 if an error occurs, otherwise it returns 1. |
| 1665 | */ |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1666 | static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp) |
| 1667 | { |
| 1668 | switch (arg->type) { |
| 1669 | case ARGT_STR: |
| 1670 | smp->data.type = SMP_T_STR; |
| 1671 | smp->data.u.str = arg->data.str; |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1672 | smp->flags = SMP_F_CONST; |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1673 | return 1; |
| 1674 | case ARGT_VAR: |
| 1675 | if (!vars_get_by_desc(&arg->data.var, smp)) |
| 1676 | return 0; |
| 1677 | if (!sample_casts[smp->data.type][SMP_T_STR]) |
| 1678 | return 0; |
| 1679 | if (!sample_casts[smp->data.type][SMP_T_STR](smp)) |
| 1680 | return 0; |
| 1681 | return 1; |
| 1682 | default: |
| 1683 | return 0; |
| 1684 | } |
| 1685 | } |
| 1686 | |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1687 | /* This function checks an <arg> and fills it with a variable type if the |
| 1688 | * <arg> string contains a valid variable name. If failed, the function |
| 1689 | * tries to perform a base64 decode operation on the same string, and |
| 1690 | * fills the <arg> with the decoded content. |
| 1691 | * |
| 1692 | * Validation is skipped if the <arg> string is empty. |
| 1693 | * |
| 1694 | * This function returns 0 if the variable lookup fails and the specified |
| 1695 | * <arg> string is not a valid base64 encoded string, as well if |
| 1696 | * unexpected argument type is specified or memory allocation error |
| 1697 | * occurs. Otherwise it returns 1. |
| 1698 | */ |
| 1699 | static inline int sample_check_arg_base64(struct arg *arg, char **err) |
| 1700 | { |
| 1701 | char *dec = NULL; |
| 1702 | int dec_size; |
| 1703 | |
| 1704 | if (arg->type != ARGT_STR) { |
| 1705 | memprintf(err, "unexpected argument type"); |
| 1706 | return 0; |
| 1707 | } |
| 1708 | |
| 1709 | if (arg->data.str.data == 0) /* empty */ |
| 1710 | return 1; |
| 1711 | |
| 1712 | if (vars_check_arg(arg, NULL)) |
| 1713 | return 1; |
| 1714 | |
| 1715 | if (arg->data.str.data % 4) { |
| 1716 | memprintf(err, "argument needs to be base64 encoded, and " |
| 1717 | "can either be a string or a variable"); |
| 1718 | return 0; |
| 1719 | } |
| 1720 | |
| 1721 | dec_size = (arg->data.str.data / 4 * 3) |
| 1722 | - (arg->data.str.area[arg->data.str.data-1] == '=' ? 1 : 0) |
| 1723 | - (arg->data.str.area[arg->data.str.data-2] == '=' ? 1 : 0); |
| 1724 | |
| 1725 | if ((dec = malloc(dec_size)) == NULL) { |
| 1726 | memprintf(err, "memory allocation error"); |
| 1727 | return 0; |
| 1728 | } |
| 1729 | |
| 1730 | dec_size = base64dec(arg->data.str.area, arg->data.str.data, dec, dec_size); |
| 1731 | if (dec_size < 0) { |
| 1732 | memprintf(err, "argument needs to be base64 encoded, and " |
| 1733 | "can either be a string or a variable"); |
| 1734 | free(dec); |
| 1735 | return 0; |
| 1736 | } |
| 1737 | |
| 1738 | /* base64 decoded */ |
| 1739 | chunk_destroy(&arg->data.str); |
| 1740 | arg->data.str.area = dec; |
| 1741 | arg->data.str.data = dec_size; |
| 1742 | return 1; |
| 1743 | } |
| 1744 | |
Patrick Gansterer | 8e36651 | 2020-04-22 16:47:57 +0200 | [diff] [blame] | 1745 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL) |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1746 | static int check_aes_gcm(struct arg *args, struct sample_conv *conv, |
| 1747 | const char *file, int line, char **err) |
| 1748 | { |
| 1749 | switch(args[0].data.sint) { |
| 1750 | case 128: |
| 1751 | case 192: |
| 1752 | case 256: |
| 1753 | break; |
| 1754 | default: |
| 1755 | memprintf(err, "key size must be 128, 192 or 256 (bits)."); |
| 1756 | return 0; |
| 1757 | } |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1758 | |
| 1759 | /* Try to decode variables. */ |
| 1760 | if (!sample_check_arg_base64(&args[1], err)) { |
| 1761 | memprintf(err, "failed to parse nonce : %s", *err); |
| 1762 | return 0; |
| 1763 | } |
| 1764 | if (!sample_check_arg_base64(&args[2], err)) { |
| 1765 | memprintf(err, "failed to parse key : %s", *err); |
| 1766 | return 0; |
| 1767 | } |
| 1768 | if (!sample_check_arg_base64(&args[3], err)) { |
| 1769 | memprintf(err, "failed to parse aead_tag : %s", *err); |
| 1770 | return 0; |
| 1771 | } |
| 1772 | |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1773 | return 1; |
| 1774 | } |
| 1775 | |
| 1776 | /* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */ |
| 1777 | static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private) |
| 1778 | { |
| 1779 | struct sample nonce, key, aead_tag; |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1780 | struct buffer *smp_trash = NULL, *smp_trash_alloc = NULL; |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1781 | EVP_CIPHER_CTX *ctx; |
| 1782 | int dec_size, ret; |
| 1783 | |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1784 | smp_trash_alloc = alloc_trash_chunk(); |
| 1785 | if (!smp_trash_alloc) |
| 1786 | return 0; |
| 1787 | |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1788 | /* smp copy */ |
| 1789 | smp_trash_alloc->data = smp->data.u.str.data; |
| 1790 | if (unlikely(smp_trash_alloc->data > smp_trash_alloc->size)) |
| 1791 | smp_trash_alloc->data = smp_trash_alloc->size; |
| 1792 | memcpy(smp_trash_alloc->area, smp->data.u.str.area, smp_trash_alloc->data); |
| 1793 | |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1794 | ctx = EVP_CIPHER_CTX_new(); |
| 1795 | |
| 1796 | if (!ctx) |
| 1797 | goto err; |
| 1798 | |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1799 | smp_trash = alloc_trash_chunk(); |
| 1800 | if (!smp_trash) |
| 1801 | goto err; |
| 1802 | |
| 1803 | smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt); |
| 1804 | if (!sample_conv_var2smp_str(&arg_p[1], &nonce)) |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1805 | goto err; |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1806 | |
| 1807 | if (arg_p[1].type == ARGT_VAR) { |
| 1808 | dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size); |
| 1809 | if (dec_size < 0) |
| 1810 | goto err; |
| 1811 | smp_trash->data = dec_size; |
| 1812 | nonce.data.u.str = *smp_trash; |
| 1813 | } |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1814 | |
| 1815 | /* Set cipher type and mode */ |
| 1816 | switch(arg_p[0].data.sint) { |
| 1817 | case 128: |
| 1818 | EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL); |
| 1819 | break; |
| 1820 | case 192: |
| 1821 | EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL); |
| 1822 | break; |
| 1823 | case 256: |
| 1824 | EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL); |
| 1825 | break; |
| 1826 | } |
| 1827 | |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1828 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, nonce.data.u.str.data, NULL); |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1829 | |
| 1830 | /* Initialise IV */ |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1831 | if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) nonce.data.u.str.area)) |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1832 | goto err; |
| 1833 | |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1834 | smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt); |
| 1835 | if (!sample_conv_var2smp_str(&arg_p[2], &key)) |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1836 | goto err; |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1837 | |
| 1838 | if (arg_p[2].type == ARGT_VAR) { |
| 1839 | dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size); |
| 1840 | if (dec_size < 0) |
| 1841 | goto err; |
| 1842 | smp_trash->data = dec_size; |
| 1843 | key.data.u.str = *smp_trash; |
| 1844 | } |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1845 | |
| 1846 | /* Initialise key */ |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1847 | if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) key.data.u.str.area, NULL)) |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1848 | goto err; |
| 1849 | |
| 1850 | if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data, |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1851 | (unsigned char *) smp_trash_alloc->area, (int) smp_trash_alloc->data)) |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1852 | goto err; |
| 1853 | |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1854 | smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt); |
| 1855 | if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag)) |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1856 | goto err; |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1857 | |
| 1858 | if (arg_p[3].type == ARGT_VAR) { |
| 1859 | dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size); |
| 1860 | if (dec_size < 0) |
| 1861 | goto err; |
| 1862 | smp_trash_alloc->data = dec_size; |
| 1863 | aead_tag.data.u.str = *smp_trash_alloc; |
| 1864 | } |
| 1865 | |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1866 | dec_size = smp_trash->data; |
| 1867 | |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1868 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, aead_tag.data.u.str.data, (void *) aead_tag.data.u.str.area); |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1869 | ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data); |
| 1870 | |
| 1871 | if (ret <= 0) |
| 1872 | goto err; |
| 1873 | |
| 1874 | smp->data.u.str.data = dec_size + smp_trash->data; |
| 1875 | smp->data.u.str.area = smp_trash->area; |
| 1876 | smp->data.type = SMP_T_BIN; |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1877 | smp_dup(smp); |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1878 | free_trash_chunk(smp_trash_alloc); |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1879 | free_trash_chunk(smp_trash); |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1880 | return 1; |
| 1881 | |
| 1882 | err: |
| 1883 | free_trash_chunk(smp_trash_alloc); |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1884 | free_trash_chunk(smp_trash); |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1885 | return 0; |
| 1886 | } |
| 1887 | #endif /* HA_OPENSSL_VERSION_NUMBER */ |
| 1888 | |
Patrick Gansterer | 8e36651 | 2020-04-22 16:47:57 +0200 | [diff] [blame] | 1889 | static int check_crypto_digest(struct arg *args, struct sample_conv *conv, |
| 1890 | const char *file, int line, char **err) |
| 1891 | { |
| 1892 | const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area); |
| 1893 | |
| 1894 | if (evp) |
| 1895 | return 1; |
| 1896 | |
| 1897 | memprintf(err, "algorithm must be a valid OpenSSL message digest name."); |
| 1898 | return 0; |
| 1899 | } |
| 1900 | |
| 1901 | static int sample_conv_crypto_digest(const struct arg *args, struct sample *smp, void *private) |
| 1902 | { |
| 1903 | struct buffer *trash = get_trash_chunk(); |
| 1904 | unsigned char *md = (unsigned char*) trash->area; |
| 1905 | unsigned int md_len = trash->size; |
| 1906 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); |
| 1907 | const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area); |
| 1908 | |
| 1909 | if (!ctx) |
| 1910 | return 0; |
| 1911 | |
| 1912 | if (!EVP_DigestInit_ex(ctx, evp, NULL) || |
| 1913 | !EVP_DigestUpdate(ctx, smp->data.u.str.area, smp->data.u.str.data) || |
| 1914 | !EVP_DigestFinal_ex(ctx, md, &md_len)) { |
| 1915 | EVP_MD_CTX_free(ctx); |
| 1916 | return 0; |
| 1917 | } |
| 1918 | |
| 1919 | EVP_MD_CTX_free(ctx); |
| 1920 | |
| 1921 | trash->data = md_len; |
| 1922 | smp->data.u.str = *trash; |
| 1923 | smp->data.type = SMP_T_BIN; |
| 1924 | smp->flags &= ~SMP_F_CONST; |
| 1925 | return 1; |
| 1926 | } |
| 1927 | |
| 1928 | static int check_crypto_hmac(struct arg *args, struct sample_conv *conv, |
| 1929 | const char *file, int line, char **err) |
| 1930 | { |
| 1931 | if (!check_crypto_digest(args, conv, file, line, err)) |
| 1932 | return 0; |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1933 | |
| 1934 | if (!sample_check_arg_base64(&args[1], err)) { |
| 1935 | memprintf(err, "failed to parse key : %s", *err); |
| 1936 | return 0; |
| 1937 | } |
Patrick Gansterer | 8e36651 | 2020-04-22 16:47:57 +0200 | [diff] [blame] | 1938 | |
Patrick Gansterer | 8e36651 | 2020-04-22 16:47:57 +0200 | [diff] [blame] | 1939 | return 1; |
| 1940 | } |
| 1941 | |
| 1942 | static int sample_conv_crypto_hmac(const struct arg *args, struct sample *smp, void *private) |
| 1943 | { |
| 1944 | struct sample key; |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1945 | struct buffer *trash = NULL, *key_trash = NULL; |
Patrick Gansterer | 8e36651 | 2020-04-22 16:47:57 +0200 | [diff] [blame] | 1946 | unsigned char *md; |
| 1947 | unsigned int md_len; |
| 1948 | const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area); |
| 1949 | int dec_size; |
| 1950 | |
| 1951 | smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt); |
| 1952 | if (!sample_conv_var2smp_str(&args[1], &key)) |
| 1953 | return 0; |
| 1954 | |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1955 | if (args[1].type == ARGT_VAR) { |
| 1956 | key_trash = alloc_trash_chunk(); |
| 1957 | if (!key_trash) |
| 1958 | goto err; |
| 1959 | |
| 1960 | dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, key_trash->area, key_trash->size); |
| 1961 | if (dec_size < 0) |
| 1962 | goto err; |
| 1963 | key_trash->data = dec_size; |
| 1964 | key.data.u.str = *key_trash; |
| 1965 | } |
Patrick Gansterer | 8e36651 | 2020-04-22 16:47:57 +0200 | [diff] [blame] | 1966 | |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1967 | trash = alloc_trash_chunk(); |
| 1968 | if (!trash) |
Patrick Gansterer | 8e36651 | 2020-04-22 16:47:57 +0200 | [diff] [blame] | 1969 | goto err; |
| 1970 | |
| 1971 | md = (unsigned char*) trash->area; |
| 1972 | md_len = trash->size; |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1973 | if (!HMAC(evp, key.data.u.str.area, key.data.u.str.data, (const unsigned char*) smp->data.u.str.area, |
| 1974 | smp->data.u.str.data, md, &md_len)) |
Patrick Gansterer | 8e36651 | 2020-04-22 16:47:57 +0200 | [diff] [blame] | 1975 | goto err; |
| 1976 | |
| 1977 | free_trash_chunk(key_trash); |
| 1978 | |
| 1979 | trash->data = md_len; |
| 1980 | smp->data.u.str = *trash; |
| 1981 | smp->data.type = SMP_T_BIN; |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1982 | smp_dup(smp); |
| 1983 | free_trash_chunk(trash); |
Patrick Gansterer | 8e36651 | 2020-04-22 16:47:57 +0200 | [diff] [blame] | 1984 | return 1; |
| 1985 | |
| 1986 | err: |
| 1987 | free_trash_chunk(key_trash); |
Dragan Dosen | 11a23ed | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1988 | free_trash_chunk(trash); |
Patrick Gansterer | 8e36651 | 2020-04-22 16:47:57 +0200 | [diff] [blame] | 1989 | return 0; |
| 1990 | } |
| 1991 | |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1992 | #endif /* USE_OPENSSL */ |
Tim Duesterhus | d437630 | 2019-06-17 12:41:44 +0200 | [diff] [blame] | 1993 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1994 | static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private) |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 1995 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1996 | struct buffer *trash = get_trash_chunk(); |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 1997 | unsigned char c; |
| 1998 | int ptr = 0; |
| 1999 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2000 | trash->data = 0; |
| 2001 | while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) { |
| 2002 | c = smp->data.u.str.area[ptr++]; |
| 2003 | trash->area[trash->data++] = hextab[(c >> 4) & 0xF]; |
| 2004 | trash->area[trash->data++] = hextab[c & 0xF]; |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 2005 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2006 | smp->data.u.str = *trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2007 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2008 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 2009 | return 1; |
| 2010 | } |
| 2011 | |
Dragan Dosen | 3f957b2 | 2017-10-24 09:27:34 +0200 | [diff] [blame] | 2012 | static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private) |
| 2013 | { |
| 2014 | long long int n = 0; |
| 2015 | int i, c; |
| 2016 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2017 | for (i = 0; i < smp->data.u.str.data; i++) { |
| 2018 | if ((c = hex2i(smp->data.u.str.area[i])) < 0) |
Dragan Dosen | 3f957b2 | 2017-10-24 09:27:34 +0200 | [diff] [blame] | 2019 | return 0; |
| 2020 | n = (n << 4) + c; |
| 2021 | } |
| 2022 | |
| 2023 | smp->data.u.sint = n; |
| 2024 | smp->data.type = SMP_T_SINT; |
| 2025 | smp->flags &= ~SMP_F_CONST; |
| 2026 | return 1; |
| 2027 | } |
| 2028 | |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 2029 | /* hashes the binary input into a 32-bit unsigned int */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2030 | static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 2031 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2032 | smp->data.u.sint = hash_djb2(smp->data.u.str.area, |
| 2033 | smp->data.u.str.data); |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2034 | if (arg_p && arg_p->data.sint) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2035 | smp->data.u.sint = full_hash(smp->data.u.sint); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2036 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 2037 | return 1; |
| 2038 | } |
| 2039 | |
Willy Tarreau | 60a2ee7 | 2017-12-15 07:13:48 +0100 | [diff] [blame] | 2040 | static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private) |
Etienne Carriere | ed0d24e | 2017-12-13 13:41:34 +0100 | [diff] [blame] | 2041 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2042 | int i = smp->data.u.str.data; |
Etienne Carriere | ed0d24e | 2017-12-13 13:41:34 +0100 | [diff] [blame] | 2043 | smp->data.u.sint = i; |
| 2044 | smp->data.type = SMP_T_SINT; |
| 2045 | return 1; |
| 2046 | } |
| 2047 | |
| 2048 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2049 | static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 2050 | { |
| 2051 | int i; |
| 2052 | |
Willy Tarreau | f0645dc | 2016-08-09 14:29:38 +0200 | [diff] [blame] | 2053 | if (!smp_make_rw(smp)) |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2054 | return 0; |
| 2055 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2056 | for (i = 0; i < smp->data.u.str.data; i++) { |
| 2057 | if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z')) |
| 2058 | smp->data.u.str.area[i] += 'a' - 'A'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 2059 | } |
| 2060 | return 1; |
| 2061 | } |
| 2062 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2063 | static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 2064 | { |
| 2065 | int i; |
| 2066 | |
Willy Tarreau | f0645dc | 2016-08-09 14:29:38 +0200 | [diff] [blame] | 2067 | if (!smp_make_rw(smp)) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 2068 | return 0; |
| 2069 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2070 | for (i = 0; i < smp->data.u.str.data; i++) { |
| 2071 | if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z')) |
| 2072 | smp->data.u.str.area[i] += 'A' - 'a'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 2073 | } |
| 2074 | return 1; |
| 2075 | } |
| 2076 | |
Tim Duesterhus | 1478aa7 | 2018-01-25 16:24:51 +0100 | [diff] [blame] | 2077 | /* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */ |
| 2078 | static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private) |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 2079 | { |
Tim Duesterhus | 1478aa7 | 2018-01-25 16:24:51 +0100 | [diff] [blame] | 2080 | /* Attempt to convert to IPv4 to apply the correct mask. */ |
| 2081 | c_ipv62ip(smp); |
| 2082 | |
| 2083 | if (smp->data.type == SMP_T_IPV4) { |
| 2084 | smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr; |
| 2085 | smp->data.type = SMP_T_IPV4; |
| 2086 | } |
| 2087 | else if (smp->data.type == SMP_T_IPV6) { |
| 2088 | /* IPv6 cannot be converted without an IPv6 mask. */ |
| 2089 | if (args[1].type != ARGT_IPV6) |
| 2090 | return 0; |
| 2091 | |
Willy Tarreau | a8b7ecd | 2020-02-25 09:43:22 +0100 | [diff] [blame] | 2092 | write_u64(&smp->data.u.ipv6.s6_addr[0], |
| 2093 | read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0])); |
| 2094 | write_u64(&smp->data.u.ipv6.s6_addr[8], |
| 2095 | read_u64(&smp->data.u.ipv6.s6_addr[8]) & read_u64(&args[1].data.ipv6.s6_addr[8])); |
Tim Duesterhus | 1478aa7 | 2018-01-25 16:24:51 +0100 | [diff] [blame] | 2096 | smp->data.type = SMP_T_IPV6; |
| 2097 | } |
| 2098 | |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 2099 | return 1; |
| 2100 | } |
| 2101 | |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 2102 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 2103 | * adds an optional offset found in args[1] and emits a string representing |
| 2104 | * the local time in the format specified in args[1] using strftime(). |
| 2105 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2106 | static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private) |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 2107 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2108 | struct buffer *temp; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2109 | /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2110 | time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL; |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 2111 | struct tm *tm; |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 2112 | |
| 2113 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2114 | if (args[1].type == ARGT_SINT) |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 2115 | curr_date += args[1].data.sint; |
| 2116 | |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 2117 | tm = localtime(&curr_date); |
| 2118 | if (!tm) |
| 2119 | return 0; |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 2120 | temp = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2121 | temp->data = strftime(temp->area, temp->size, args[0].data.str.area, |
| 2122 | tm); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2123 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2124 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 2125 | return 1; |
| 2126 | } |
| 2127 | |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 2128 | /* hashes the binary input into a 32-bit unsigned int */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2129 | static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 2130 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2131 | smp->data.u.sint = hash_sdbm(smp->data.u.str.area, |
| 2132 | smp->data.u.str.data); |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2133 | if (arg_p && arg_p->data.sint) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2134 | smp->data.u.sint = full_hash(smp->data.u.sint); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2135 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 2136 | return 1; |
| 2137 | } |
| 2138 | |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 2139 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 2140 | * adds an optional offset found in args[1] and emits a string representing |
| 2141 | * the UTC date in the format specified in args[1] using strftime(). |
| 2142 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2143 | static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private) |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 2144 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2145 | struct buffer *temp; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2146 | /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2147 | time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL; |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 2148 | struct tm *tm; |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 2149 | |
| 2150 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2151 | if (args[1].type == ARGT_SINT) |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 2152 | curr_date += args[1].data.sint; |
| 2153 | |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 2154 | tm = gmtime(&curr_date); |
| 2155 | if (!tm) |
| 2156 | return 0; |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 2157 | temp = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2158 | temp->data = strftime(temp->area, temp->size, args[0].data.str.area, |
| 2159 | tm); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2160 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2161 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 2162 | return 1; |
| 2163 | } |
| 2164 | |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 2165 | /* hashes the binary input into a 32-bit unsigned int */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2166 | static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 2167 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2168 | smp->data.u.sint = hash_wt6(smp->data.u.str.area, |
| 2169 | smp->data.u.str.data); |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2170 | if (arg_p && arg_p->data.sint) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2171 | smp->data.u.sint = full_hash(smp->data.u.sint); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2172 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 2173 | return 1; |
| 2174 | } |
| 2175 | |
Thierry FOURNIER | 01e0974 | 2016-12-26 11:46:11 +0100 | [diff] [blame] | 2176 | /* hashes the binary input into a 32-bit unsigned int using xxh. |
| 2177 | * The seed of the hash defaults to 0 but can be changd in argument 1. |
| 2178 | */ |
| 2179 | static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private) |
| 2180 | { |
| 2181 | unsigned int seed; |
| 2182 | |
| 2183 | if (arg_p && arg_p->data.sint) |
| 2184 | seed = arg_p->data.sint; |
| 2185 | else |
| 2186 | seed = 0; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2187 | smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data, |
| 2188 | seed); |
Thierry FOURNIER | 01e0974 | 2016-12-26 11:46:11 +0100 | [diff] [blame] | 2189 | smp->data.type = SMP_T_SINT; |
| 2190 | return 1; |
| 2191 | } |
| 2192 | |
| 2193 | /* hashes the binary input into a 64-bit unsigned int using xxh. |
| 2194 | * In fact, the function returns a 64 bit unsigned, but the sample |
| 2195 | * storage of haproxy only proposes 64-bits signed, so the value is |
| 2196 | * cast as signed. This cast doesn't impact the hash repartition. |
| 2197 | * The seed of the hash defaults to 0 but can be changd in argument 1. |
| 2198 | */ |
| 2199 | static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private) |
| 2200 | { |
| 2201 | unsigned long long int seed; |
| 2202 | |
| 2203 | if (arg_p && arg_p->data.sint) |
| 2204 | seed = (unsigned long long int)arg_p->data.sint; |
| 2205 | else |
| 2206 | seed = 0; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2207 | smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area, |
| 2208 | smp->data.u.str.data, seed); |
Thierry FOURNIER | 01e0974 | 2016-12-26 11:46:11 +0100 | [diff] [blame] | 2209 | smp->data.type = SMP_T_SINT; |
| 2210 | return 1; |
| 2211 | } |
| 2212 | |
Willy Tarreau | 8059977 | 2015-01-20 19:35:24 +0100 | [diff] [blame] | 2213 | /* hashes the binary input into a 32-bit unsigned int */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2214 | static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 8059977 | 2015-01-20 19:35:24 +0100 | [diff] [blame] | 2215 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2216 | smp->data.u.sint = hash_crc32(smp->data.u.str.area, |
| 2217 | smp->data.u.str.data); |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2218 | if (arg_p && arg_p->data.sint) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2219 | smp->data.u.sint = full_hash(smp->data.u.sint); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2220 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 8059977 | 2015-01-20 19:35:24 +0100 | [diff] [blame] | 2221 | return 1; |
| 2222 | } |
| 2223 | |
Emmanuel Hocdet | 50791a7 | 2018-03-21 11:19:01 +0100 | [diff] [blame] | 2224 | /* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */ |
| 2225 | static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private) |
| 2226 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2227 | smp->data.u.sint = hash_crc32c(smp->data.u.str.area, |
| 2228 | smp->data.u.str.data); |
Emmanuel Hocdet | 50791a7 | 2018-03-21 11:19:01 +0100 | [diff] [blame] | 2229 | if (arg_p && arg_p->data.sint) |
| 2230 | smp->data.u.sint = full_hash(smp->data.u.sint); |
| 2231 | smp->data.type = SMP_T_SINT; |
| 2232 | return 1; |
| 2233 | } |
| 2234 | |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2235 | /* This function escape special json characters. The returned string can be |
| 2236 | * safely set between two '"' and used as json string. The json string is |
| 2237 | * defined like this: |
| 2238 | * |
| 2239 | * any Unicode character except '"' or '\' or control character |
| 2240 | * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits |
| 2241 | * |
| 2242 | * The enum input_type contain all the allowed mode for decoding the input |
| 2243 | * string. |
| 2244 | */ |
| 2245 | enum input_type { |
| 2246 | IT_ASCII = 0, |
| 2247 | IT_UTF8, |
| 2248 | IT_UTF8S, |
| 2249 | IT_UTF8P, |
| 2250 | IT_UTF8PS, |
| 2251 | }; |
| 2252 | static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv, |
| 2253 | const char *file, int line, char **err) |
| 2254 | { |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 2255 | enum input_type type; |
| 2256 | |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2257 | if (!arg) { |
| 2258 | memprintf(err, "Unexpected empty arg list"); |
| 2259 | return 0; |
| 2260 | } |
| 2261 | |
| 2262 | if (arg->type != ARGT_STR) { |
| 2263 | memprintf(err, "Unexpected arg type"); |
| 2264 | return 0; |
| 2265 | } |
| 2266 | |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 2267 | if (strcmp(arg->data.str.area, "") == 0) |
| 2268 | type = IT_ASCII; |
| 2269 | else if (strcmp(arg->data.str.area, "ascii") == 0) |
| 2270 | type = IT_ASCII; |
| 2271 | else if (strcmp(arg->data.str.area, "utf8") == 0) |
| 2272 | type = IT_UTF8; |
| 2273 | else if (strcmp(arg->data.str.area, "utf8s") == 0) |
| 2274 | type = IT_UTF8S; |
| 2275 | else if (strcmp(arg->data.str.area, "utf8p") == 0) |
| 2276 | type = IT_UTF8P; |
| 2277 | else if (strcmp(arg->data.str.area, "utf8ps") == 0) |
| 2278 | type = IT_UTF8PS; |
| 2279 | else { |
| 2280 | memprintf(err, "Unexpected input code type. " |
| 2281 | "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'"); |
| 2282 | return 0; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2283 | } |
| 2284 | |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 2285 | chunk_destroy(&arg->data.str); |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 2286 | arg->type = ARGT_SINT; |
| 2287 | arg->data.sint = type; |
| 2288 | return 1; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2289 | } |
| 2290 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2291 | static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private) |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2292 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2293 | struct buffer *temp; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2294 | char _str[7]; /* \u + 4 hex digit + null char for sprintf. */ |
| 2295 | const char *str; |
| 2296 | int len; |
| 2297 | enum input_type input_type = IT_ASCII; |
| 2298 | unsigned int c; |
| 2299 | unsigned int ret; |
| 2300 | char *p; |
| 2301 | |
| 2302 | if (arg_p) |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2303 | input_type = arg_p->data.sint; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2304 | |
| 2305 | temp = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2306 | temp->data = 0; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2307 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2308 | p = smp->data.u.str.area; |
| 2309 | while (p < smp->data.u.str.area + smp->data.u.str.data) { |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2310 | |
| 2311 | if (input_type == IT_ASCII) { |
| 2312 | /* Read input as ASCII. */ |
| 2313 | c = *(unsigned char *)p; |
| 2314 | p++; |
| 2315 | } |
| 2316 | else { |
| 2317 | /* Read input as UTF8. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2318 | ret = utf8_next(p, |
| 2319 | smp->data.u.str.data - ( p - smp->data.u.str.area), |
| 2320 | &c); |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2321 | p += utf8_return_length(ret); |
| 2322 | |
| 2323 | if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK) |
| 2324 | return 0; |
| 2325 | if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK) |
| 2326 | continue; |
| 2327 | if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ)) |
| 2328 | return 0; |
| 2329 | if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ)) |
| 2330 | continue; |
| 2331 | |
| 2332 | /* Check too big values. */ |
| 2333 | if ((unsigned int)c > 0xffff) { |
| 2334 | if (input_type == IT_UTF8 || input_type == IT_UTF8P) |
| 2335 | return 0; |
| 2336 | continue; |
| 2337 | } |
| 2338 | } |
| 2339 | |
| 2340 | /* Convert character. */ |
| 2341 | if (c == '"') { |
| 2342 | len = 2; |
| 2343 | str = "\\\""; |
| 2344 | } |
| 2345 | else if (c == '\\') { |
| 2346 | len = 2; |
| 2347 | str = "\\\\"; |
| 2348 | } |
| 2349 | else if (c == '/') { |
| 2350 | len = 2; |
| 2351 | str = "\\/"; |
| 2352 | } |
| 2353 | else if (c == '\b') { |
| 2354 | len = 2; |
| 2355 | str = "\\b"; |
| 2356 | } |
| 2357 | else if (c == '\f') { |
| 2358 | len = 2; |
| 2359 | str = "\\f"; |
| 2360 | } |
| 2361 | else if (c == '\r') { |
| 2362 | len = 2; |
| 2363 | str = "\\r"; |
| 2364 | } |
| 2365 | else if (c == '\n') { |
| 2366 | len = 2; |
| 2367 | str = "\\n"; |
| 2368 | } |
| 2369 | else if (c == '\t') { |
| 2370 | len = 2; |
| 2371 | str = "\\t"; |
| 2372 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 2373 | else if (c > 0xff || !isprint((unsigned char)c)) { |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2374 | /* isprint generate a segfault if c is too big. The man says that |
| 2375 | * c must have the value of an unsigned char or EOF. |
| 2376 | */ |
| 2377 | len = 6; |
| 2378 | _str[0] = '\\'; |
| 2379 | _str[1] = 'u'; |
| 2380 | snprintf(&_str[2], 5, "%04x", (unsigned short)c); |
| 2381 | str = _str; |
| 2382 | } |
| 2383 | else { |
| 2384 | len = 1; |
Willy Tarreau | 5715da2 | 2020-02-25 08:37:37 +0100 | [diff] [blame] | 2385 | _str[0] = c; |
| 2386 | str = _str; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2387 | } |
| 2388 | |
| 2389 | /* Check length */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2390 | if (temp->data + len > temp->size) |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2391 | return 0; |
| 2392 | |
| 2393 | /* Copy string. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2394 | memcpy(temp->area + temp->data, str, len); |
| 2395 | temp->data += len; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2396 | } |
| 2397 | |
| 2398 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2399 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2400 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2401 | |
| 2402 | return 1; |
| 2403 | } |
| 2404 | |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 2405 | /* This sample function is designed to extract some bytes from an input buffer. |
| 2406 | * First arg is the offset. |
| 2407 | * Optional second arg is the length to truncate */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2408 | static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private) |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 2409 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2410 | if (smp->data.u.str.data <= arg_p[0].data.sint) { |
| 2411 | smp->data.u.str.data = 0; |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 2412 | return 1; |
| 2413 | } |
| 2414 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2415 | if (smp->data.u.str.size) |
| 2416 | smp->data.u.str.size -= arg_p[0].data.sint; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2417 | smp->data.u.str.data -= arg_p[0].data.sint; |
| 2418 | smp->data.u.str.area += arg_p[0].data.sint; |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 2419 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2420 | if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data)) |
| 2421 | smp->data.u.str.data = arg_p[1].data.sint; |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 2422 | |
| 2423 | return 1; |
| 2424 | } |
| 2425 | |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2426 | static int sample_conv_field_check(struct arg *args, struct sample_conv *conv, |
| 2427 | const char *file, int line, char **err) |
| 2428 | { |
| 2429 | struct arg *arg = args; |
| 2430 | |
| 2431 | if (!arg) { |
| 2432 | memprintf(err, "Unexpected empty arg list"); |
| 2433 | return 0; |
| 2434 | } |
| 2435 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2436 | if (arg->type != ARGT_SINT) { |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2437 | memprintf(err, "Unexpected arg type"); |
| 2438 | return 0; |
| 2439 | } |
| 2440 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2441 | if (!arg->data.sint) { |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2442 | memprintf(err, "Unexpected value 0 for index"); |
| 2443 | return 0; |
| 2444 | } |
| 2445 | |
| 2446 | arg++; |
| 2447 | |
| 2448 | if (arg->type != ARGT_STR) { |
| 2449 | memprintf(err, "Unexpected arg type"); |
| 2450 | return 0; |
| 2451 | } |
| 2452 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2453 | if (!arg->data.str.data) { |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2454 | memprintf(err, "Empty separators list"); |
| 2455 | return 0; |
| 2456 | } |
| 2457 | |
| 2458 | return 1; |
| 2459 | } |
| 2460 | |
| 2461 | /* This sample function is designed to a return selected part of a string (field). |
| 2462 | * First arg is the index of the field (start at 1) |
| 2463 | * Second arg is a char list of separators (type string) |
| 2464 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2465 | static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private) |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2466 | { |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2467 | int field; |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2468 | char *start, *end; |
| 2469 | int i; |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2470 | int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1; |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2471 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2472 | if (!arg_p[0].data.sint) |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2473 | return 0; |
| 2474 | |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2475 | if (arg_p[0].data.sint < 0) { |
| 2476 | field = -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2477 | end = start = smp->data.u.str.area + smp->data.u.str.data; |
| 2478 | while (start > smp->data.u.str.area) { |
| 2479 | for (i = 0 ; i < arg_p[1].data.str.data; i++) { |
| 2480 | if (*(start-1) == arg_p[1].data.str.area[i]) { |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2481 | if (field == arg_p[0].data.sint) { |
| 2482 | if (count == 1) |
| 2483 | goto found; |
| 2484 | else if (count > 1) |
| 2485 | count--; |
| 2486 | } else { |
| 2487 | end = start-1; |
| 2488 | field--; |
| 2489 | } |
| 2490 | break; |
| 2491 | } |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2492 | } |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2493 | start--; |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2494 | } |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2495 | } else { |
| 2496 | field = 1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2497 | end = start = smp->data.u.str.area; |
| 2498 | while (end - smp->data.u.str.area < smp->data.u.str.data) { |
| 2499 | for (i = 0 ; i < arg_p[1].data.str.data; i++) { |
| 2500 | if (*end == arg_p[1].data.str.area[i]) { |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2501 | if (field == arg_p[0].data.sint) { |
| 2502 | if (count == 1) |
| 2503 | goto found; |
| 2504 | else if (count > 1) |
| 2505 | count--; |
| 2506 | } else { |
| 2507 | start = end+1; |
| 2508 | field++; |
| 2509 | } |
| 2510 | break; |
| 2511 | } |
| 2512 | } |
| 2513 | end++; |
| 2514 | } |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | /* Field not found */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2518 | if (field != arg_p[0].data.sint) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2519 | smp->data.u.str.data = 0; |
Tim Duesterhus | 4381d26 | 2019-10-16 15:11:15 +0200 | [diff] [blame] | 2520 | return 0; |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2521 | } |
| 2522 | found: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2523 | smp->data.u.str.data = end - start; |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2524 | /* If ret string is len 0, no need to |
| 2525 | change pointers or to update size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2526 | if (!smp->data.u.str.data) |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2527 | return 1; |
| 2528 | |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2529 | /* Compute remaining size if needed |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2530 | Note: smp->data.u.str.size cannot be set to 0 */ |
| 2531 | if (smp->data.u.str.size) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2532 | smp->data.u.str.size -= start - smp->data.u.str.area; |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2533 | |
Thayne McCombs | ffa7846 | 2021-04-11 23:26:59 -0600 | [diff] [blame^] | 2534 | smp->data.u.str.area = start; |
| 2535 | |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2536 | return 1; |
| 2537 | } |
| 2538 | |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2539 | /* This sample function is designed to return a word from a string. |
| 2540 | * First arg is the index of the word (start at 1) |
| 2541 | * Second arg is a char list of words separators (type string) |
| 2542 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2543 | static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private) |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2544 | { |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2545 | int word; |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2546 | char *start, *end; |
| 2547 | int i, issep, inword; |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2548 | int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1; |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2549 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2550 | if (!arg_p[0].data.sint) |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2551 | return 0; |
| 2552 | |
| 2553 | word = 0; |
| 2554 | inword = 0; |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2555 | if (arg_p[0].data.sint < 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2556 | end = start = smp->data.u.str.area + smp->data.u.str.data; |
| 2557 | while (start > smp->data.u.str.area) { |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2558 | issep = 0; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2559 | for (i = 0 ; i < arg_p[1].data.str.data; i++) { |
| 2560 | if (*(start-1) == arg_p[1].data.str.area[i]) { |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2561 | issep = 1; |
| 2562 | break; |
| 2563 | } |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2564 | } |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2565 | if (!inword) { |
| 2566 | if (!issep) { |
| 2567 | if (word != arg_p[0].data.sint) { |
| 2568 | word--; |
| 2569 | end = start; |
| 2570 | } |
| 2571 | inword = 1; |
| 2572 | } |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2573 | } |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2574 | else if (issep) { |
Willy Tarreau | 9eb2a4a | 2018-04-19 10:33:28 +0200 | [diff] [blame] | 2575 | if (word == arg_p[0].data.sint) { |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2576 | if (count == 1) |
| 2577 | goto found; |
| 2578 | else if (count > 1) |
| 2579 | count--; |
Willy Tarreau | 9eb2a4a | 2018-04-19 10:33:28 +0200 | [diff] [blame] | 2580 | } |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2581 | inword = 0; |
| 2582 | } |
| 2583 | start--; |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2584 | } |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2585 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2586 | end = start = smp->data.u.str.area; |
| 2587 | while (end - smp->data.u.str.area < smp->data.u.str.data) { |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2588 | issep = 0; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2589 | for (i = 0 ; i < arg_p[1].data.str.data; i++) { |
| 2590 | if (*end == arg_p[1].data.str.area[i]) { |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2591 | issep = 1; |
| 2592 | break; |
| 2593 | } |
| 2594 | } |
| 2595 | if (!inword) { |
| 2596 | if (!issep) { |
| 2597 | if (word != arg_p[0].data.sint) { |
| 2598 | word++; |
| 2599 | start = end; |
| 2600 | } |
| 2601 | inword = 1; |
| 2602 | } |
| 2603 | } |
| 2604 | else if (issep) { |
Willy Tarreau | 9eb2a4a | 2018-04-19 10:33:28 +0200 | [diff] [blame] | 2605 | if (word == arg_p[0].data.sint) { |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2606 | if (count == 1) |
| 2607 | goto found; |
| 2608 | else if (count > 1) |
| 2609 | count--; |
Willy Tarreau | 9eb2a4a | 2018-04-19 10:33:28 +0200 | [diff] [blame] | 2610 | } |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2611 | inword = 0; |
| 2612 | } |
| 2613 | end++; |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2614 | } |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2615 | } |
| 2616 | |
| 2617 | /* Field not found */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2618 | if (word != arg_p[0].data.sint) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2619 | smp->data.u.str.data = 0; |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2620 | return 1; |
| 2621 | } |
| 2622 | found: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2623 | smp->data.u.str.data = end - start; |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2624 | /* If ret string is len 0, no need to |
| 2625 | change pointers or to update size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2626 | if (!smp->data.u.str.data) |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2627 | return 1; |
| 2628 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2629 | smp->data.u.str.area = start; |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2630 | |
| 2631 | /* Compute remaining size if needed |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2632 | Note: smp->data.u.str.size cannot be set to 0 */ |
| 2633 | if (smp->data.u.str.size) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2634 | smp->data.u.str.size -= start - smp->data.u.str.area; |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2635 | |
| 2636 | return 1; |
| 2637 | } |
| 2638 | |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2639 | static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv, |
| 2640 | const char *file, int line, char **err) |
| 2641 | { |
| 2642 | struct arg *arg = args; |
| 2643 | char *p; |
| 2644 | int len; |
| 2645 | |
| 2646 | /* arg0 is a regex, it uses type_flag for ICASE and global match */ |
| 2647 | arg[0].type_flags = 0; |
| 2648 | |
| 2649 | if (arg[2].type != ARGT_STR) |
| 2650 | return 1; |
| 2651 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2652 | p = arg[2].data.str.area; |
| 2653 | len = arg[2].data.str.data; |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2654 | while (len) { |
| 2655 | if (*p == 'i') { |
| 2656 | arg[0].type_flags |= ARGF_REG_ICASE; |
| 2657 | } |
| 2658 | else if (*p == 'g') { |
| 2659 | arg[0].type_flags |= ARGF_REG_GLOB; |
| 2660 | } |
| 2661 | else { |
| 2662 | memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p); |
| 2663 | return 0; |
| 2664 | } |
| 2665 | p++; |
| 2666 | len--; |
| 2667 | } |
| 2668 | return 1; |
| 2669 | } |
| 2670 | |
| 2671 | /* This sample function is designed to do the equivalent of s/match/replace/ on |
| 2672 | * the input string. It applies a regex and restarts from the last matched |
| 2673 | * location until nothing matches anymore. First arg is the regex to apply to |
| 2674 | * the input string, second arg is the replacement expression. |
| 2675 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2676 | static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2677 | { |
| 2678 | char *start, *end; |
| 2679 | struct my_regex *reg = arg_p[0].data.reg; |
| 2680 | regmatch_t pmatch[MAX_MATCH]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2681 | struct buffer *trash = get_trash_chunk(); |
Jerome Magnin | 07e1e3c | 2020-02-16 19:20:19 +0100 | [diff] [blame] | 2682 | struct buffer *output; |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2683 | int flag, max; |
| 2684 | int found; |
| 2685 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2686 | start = smp->data.u.str.area; |
| 2687 | end = start + smp->data.u.str.data; |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2688 | |
| 2689 | flag = 0; |
| 2690 | while (1) { |
| 2691 | /* check for last round which is used to copy remaining parts |
| 2692 | * when not running in global replacement mode. |
| 2693 | */ |
| 2694 | found = 0; |
| 2695 | if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) { |
| 2696 | /* Note: we can have start == end on empty strings or at the end */ |
| 2697 | found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag); |
| 2698 | } |
| 2699 | |
| 2700 | if (!found) |
| 2701 | pmatch[0].rm_so = end - start; |
| 2702 | |
| 2703 | /* copy the heading non-matching part (which may also be the tail if nothing matches) */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2704 | max = trash->size - trash->data; |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2705 | if (max && pmatch[0].rm_so > 0) { |
| 2706 | if (max > pmatch[0].rm_so) |
| 2707 | max = pmatch[0].rm_so; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2708 | memcpy(trash->area + trash->data, start, max); |
| 2709 | trash->data += max; |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | if (!found) |
| 2713 | break; |
| 2714 | |
Jerome Magnin | 07e1e3c | 2020-02-16 19:20:19 +0100 | [diff] [blame] | 2715 | output = alloc_trash_chunk(); |
Willy Tarreau | 23997da | 2020-02-18 14:27:44 +0100 | [diff] [blame] | 2716 | if (!output) |
| 2717 | break; |
| 2718 | |
Jerome Magnin | 07e1e3c | 2020-02-16 19:20:19 +0100 | [diff] [blame] | 2719 | output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch); |
| 2720 | |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2721 | /* replace the matching part */ |
Jerome Magnin | 07e1e3c | 2020-02-16 19:20:19 +0100 | [diff] [blame] | 2722 | max = output->size - output->data; |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2723 | if (max) { |
Jerome Magnin | 07e1e3c | 2020-02-16 19:20:19 +0100 | [diff] [blame] | 2724 | if (max > output->data) |
| 2725 | max = output->data; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2726 | memcpy(trash->area + trash->data, |
Jerome Magnin | 07e1e3c | 2020-02-16 19:20:19 +0100 | [diff] [blame] | 2727 | output->area, max); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2728 | trash->data += max; |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2729 | } |
| 2730 | |
Jerome Magnin | 07e1e3c | 2020-02-16 19:20:19 +0100 | [diff] [blame] | 2731 | free_trash_chunk(output); |
| 2732 | |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2733 | /* stop here if we're done with this string */ |
| 2734 | if (start >= end) |
| 2735 | break; |
| 2736 | |
| 2737 | /* We have a special case for matches of length 0 (eg: "x*y*"). |
| 2738 | * These ones are considered to match in front of a character, |
| 2739 | * so we have to copy that character and skip to the next one. |
| 2740 | */ |
| 2741 | if (!pmatch[0].rm_eo) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2742 | if (trash->data < trash->size) |
| 2743 | trash->area[trash->data++] = start[pmatch[0].rm_eo]; |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2744 | pmatch[0].rm_eo++; |
| 2745 | } |
| 2746 | |
| 2747 | start += pmatch[0].rm_eo; |
| 2748 | flag |= REG_NOTBOL; |
| 2749 | } |
| 2750 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2751 | smp->data.u.str = *trash; |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2752 | return 1; |
| 2753 | } |
| 2754 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2755 | /* This function check an operator entry. It expects a string. |
| 2756 | * The string can be an integer or a variable name. |
| 2757 | */ |
| 2758 | static int check_operator(struct arg *args, struct sample_conv *conv, |
| 2759 | const char *file, int line, char **err) |
| 2760 | { |
| 2761 | const char *str; |
| 2762 | const char *end; |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 2763 | long long int i; |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2764 | |
| 2765 | /* Try to decode a variable. */ |
| 2766 | if (vars_check_arg(&args[0], NULL)) |
| 2767 | return 1; |
| 2768 | |
| 2769 | /* Try to convert an integer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2770 | str = args[0].data.str.area; |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2771 | end = str + strlen(str); |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 2772 | i = read_int64(&str, end); |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2773 | if (*str != '\0') { |
| 2774 | memprintf(err, "expects an integer or a variable name"); |
| 2775 | return 0; |
| 2776 | } |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 2777 | |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 2778 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2779 | args[0].type = ARGT_SINT; |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 2780 | args[0].data.sint = i; |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2781 | return 1; |
| 2782 | } |
| 2783 | |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 2784 | /* This function returns a sample struct filled with an arg content. |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2785 | * If the arg contain an integer, the integer is returned in the |
| 2786 | * sample. If the arg contains a variable descriptor, it returns the |
| 2787 | * variable value. |
| 2788 | * |
| 2789 | * This function returns 0 if an error occurs, otherwise it returns 1. |
| 2790 | */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 2791 | static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2792 | { |
| 2793 | switch (arg->type) { |
| 2794 | case ARGT_SINT: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2795 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2796 | smp->data.u.sint = arg->data.sint; |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2797 | return 1; |
| 2798 | case ARGT_VAR: |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 2799 | if (!vars_get_by_desc(&arg->data.var, smp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2800 | return 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2801 | if (!sample_casts[smp->data.type][SMP_T_SINT]) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2802 | return 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2803 | if (!sample_casts[smp->data.type][SMP_T_SINT](smp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2804 | return 0; |
| 2805 | return 1; |
| 2806 | default: |
| 2807 | return 0; |
| 2808 | } |
| 2809 | } |
| 2810 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2811 | /* Takes a SINT on input, applies a binary twos complement and returns the SINT |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2812 | * result. |
| 2813 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2814 | static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2815 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2816 | smp->data.u.sint = ~smp->data.u.sint; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2817 | return 1; |
| 2818 | } |
| 2819 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2820 | /* Takes a SINT on input, applies a binary "and" with the SINT directly in |
Joseph Herlant | 757f5ad | 2018-11-15 12:14:56 -0800 | [diff] [blame] | 2821 | * arg_p or in the variable described in arg_p, and returns the SINT result. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2822 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2823 | static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2824 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2825 | struct sample tmp; |
| 2826 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2827 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 2828 | if (!sample_conv_var2smp(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2829 | return 0; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2830 | smp->data.u.sint &= tmp.data.u.sint; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2831 | return 1; |
| 2832 | } |
| 2833 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2834 | /* Takes a SINT on input, applies a binary "or" with the SINT directly in |
Joseph Herlant | 757f5ad | 2018-11-15 12:14:56 -0800 | [diff] [blame] | 2835 | * arg_p or in the variable described in arg_p, and returns the SINT result. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2836 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2837 | static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2838 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2839 | struct sample tmp; |
| 2840 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2841 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 2842 | if (!sample_conv_var2smp(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2843 | return 0; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2844 | smp->data.u.sint |= tmp.data.u.sint; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2845 | return 1; |
| 2846 | } |
| 2847 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2848 | /* Takes a SINT on input, applies a binary "xor" with the SINT directly in |
Joseph Herlant | 757f5ad | 2018-11-15 12:14:56 -0800 | [diff] [blame] | 2849 | * arg_p or in the variable described in arg_p, and returns the SINT result. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2850 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2851 | static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2852 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2853 | struct sample tmp; |
| 2854 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2855 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 2856 | if (!sample_conv_var2smp(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2857 | return 0; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2858 | smp->data.u.sint ^= tmp.data.u.sint; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2859 | return 1; |
| 2860 | } |
| 2861 | |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2862 | static inline long long int arith_add(long long int a, long long int b) |
| 2863 | { |
| 2864 | /* Prevent overflow and makes capped calculus. |
| 2865 | * We must ensure that the check calculus doesn't |
| 2866 | * exceed the signed 64 bits limits. |
| 2867 | * |
| 2868 | * +----------+----------+ |
| 2869 | * | a<0 | a>=0 | |
| 2870 | * +------+----------+----------+ |
| 2871 | * | b<0 | MIN-a>b | no check | |
| 2872 | * +------+----------+----------+ |
| 2873 | * | b>=0 | no check | MAX-a<b | |
| 2874 | * +------+----------+----------+ |
| 2875 | */ |
| 2876 | if ((a ^ b) >= 0) { |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 2877 | /* signs are different. */ |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2878 | if (a < 0) { |
| 2879 | if (LLONG_MIN - a > b) |
| 2880 | return LLONG_MIN; |
| 2881 | } |
| 2882 | if (LLONG_MAX - a < b) |
| 2883 | return LLONG_MAX; |
| 2884 | } |
| 2885 | return a + b; |
| 2886 | } |
| 2887 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2888 | /* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in |
Joseph Herlant | 757f5ad | 2018-11-15 12:14:56 -0800 | [diff] [blame] | 2889 | * arg_p or in the variable described in arg_p, and returns the SINT result. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2890 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2891 | static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2892 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2893 | struct sample tmp; |
| 2894 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2895 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 2896 | if (!sample_conv_var2smp(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2897 | return 0; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2898 | smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint); |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2899 | return 1; |
| 2900 | } |
| 2901 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2902 | /* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in |
Joseph Herlant | 757f5ad | 2018-11-15 12:14:56 -0800 | [diff] [blame] | 2903 | * arg_p or in the variable described in arg_p, and returns the SINT result. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2904 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2905 | static int sample_conv_arith_sub(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2906 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2907 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2908 | struct sample tmp; |
| 2909 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2910 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 2911 | if (!sample_conv_var2smp(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2912 | return 0; |
| 2913 | |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2914 | /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater |
| 2915 | * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place |
| 2916 | * of -LLONG_MIN and correct the result. |
| 2917 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2918 | if (tmp.data.u.sint == LLONG_MIN) { |
| 2919 | smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX); |
| 2920 | if (smp->data.u.sint < LLONG_MAX) |
| 2921 | smp->data.u.sint++; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2922 | return 1; |
| 2923 | } |
| 2924 | |
Joseph Herlant | 757f5ad | 2018-11-15 12:14:56 -0800 | [diff] [blame] | 2925 | /* standard subtraction: we use the "add" function and negate |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2926 | * the second operand. |
| 2927 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2928 | smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint); |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2929 | return 1; |
| 2930 | } |
| 2931 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2932 | /* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in |
Joseph Herlant | 757f5ad | 2018-11-15 12:14:56 -0800 | [diff] [blame] | 2933 | * arg_p or in the variable described in arg_p, and returns the SINT result. |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2934 | * If the result makes an overflow, then the largest possible quantity is |
| 2935 | * returned. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2936 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2937 | static int sample_conv_arith_mul(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2938 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2939 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2940 | struct sample tmp; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2941 | long long int c; |
| 2942 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2943 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 2944 | if (!sample_conv_var2smp(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2945 | return 0; |
| 2946 | |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2947 | /* prevent divide by 0 during the check */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2948 | if (!smp->data.u.sint || !tmp.data.u.sint) { |
| 2949 | smp->data.u.sint = 0; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2950 | return 1; |
| 2951 | } |
| 2952 | |
| 2953 | /* The multiply between LLONG_MIN and -1 returns a |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 2954 | * "floating point exception". |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2955 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2956 | if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) { |
| 2957 | smp->data.u.sint = LLONG_MAX; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2958 | return 1; |
| 2959 | } |
| 2960 | |
| 2961 | /* execute standard multiplication. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2962 | c = smp->data.u.sint * tmp.data.u.sint; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2963 | |
| 2964 | /* check for overflow and makes capped multiply. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2965 | if (smp->data.u.sint != c / tmp.data.u.sint) { |
| 2966 | if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) { |
| 2967 | smp->data.u.sint = LLONG_MAX; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2968 | return 1; |
| 2969 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2970 | smp->data.u.sint = LLONG_MIN; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2971 | return 1; |
| 2972 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2973 | smp->data.u.sint = c; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2974 | return 1; |
| 2975 | } |
| 2976 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2977 | /* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in |
Joseph Herlant | 757f5ad | 2018-11-15 12:14:56 -0800 | [diff] [blame] | 2978 | * arg_p or in the variable described in arg_p, and returns the SINT result. |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2979 | * If arg_p makes the result overflow, then the largest possible quantity is |
| 2980 | * returned. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2981 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2982 | static int sample_conv_arith_div(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2983 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2984 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2985 | struct sample tmp; |
| 2986 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2987 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 2988 | if (!sample_conv_var2smp(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2989 | return 0; |
| 2990 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2991 | if (tmp.data.u.sint) { |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2992 | /* The divide between LLONG_MIN and -1 returns a |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 2993 | * "floating point exception". |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2994 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2995 | if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) { |
| 2996 | smp->data.u.sint = LLONG_MAX; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2997 | return 1; |
| 2998 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2999 | smp->data.u.sint /= tmp.data.u.sint; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 3000 | return 1; |
| 3001 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3002 | smp->data.u.sint = LLONG_MAX; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3003 | return 1; |
| 3004 | } |
| 3005 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 3006 | /* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in |
Joseph Herlant | 757f5ad | 2018-11-15 12:14:56 -0800 | [diff] [blame] | 3007 | * arg_p or in the variable described in arg_p, and returns the SINT result. |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 3008 | * If arg_p makes the result overflow, then 0 is returned. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3009 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3010 | static int sample_conv_arith_mod(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 3011 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3012 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 3013 | struct sample tmp; |
| 3014 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 3015 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 3016 | if (!sample_conv_var2smp(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 3017 | return 0; |
| 3018 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3019 | if (tmp.data.u.sint) { |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 3020 | /* The divide between LLONG_MIN and -1 returns a |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 3021 | * "floating point exception". |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 3022 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3023 | if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) { |
| 3024 | smp->data.u.sint = 0; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 3025 | return 1; |
| 3026 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3027 | smp->data.u.sint %= tmp.data.u.sint; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 3028 | return 1; |
| 3029 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3030 | smp->data.u.sint = 0; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3031 | return 1; |
| 3032 | } |
| 3033 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 3034 | /* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3035 | * result. |
| 3036 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3037 | static int sample_conv_arith_neg(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 3038 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3039 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3040 | if (smp->data.u.sint == LLONG_MIN) |
| 3041 | smp->data.u.sint = LLONG_MAX; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 3042 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3043 | smp->data.u.sint = -smp->data.u.sint; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3044 | return 1; |
| 3045 | } |
| 3046 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 3047 | /* Takes a SINT on input, returns true is the value is non-null, otherwise |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3048 | * false. The output is a BOOL. |
| 3049 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3050 | static int sample_conv_arith_bool(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 3051 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3052 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3053 | smp->data.u.sint = !!smp->data.u.sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3054 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3055 | return 1; |
| 3056 | } |
| 3057 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 3058 | /* Takes a SINT on input, returns false is the value is non-null, otherwise |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3059 | * truee. The output is a BOOL. |
| 3060 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3061 | static int sample_conv_arith_not(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 3062 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3063 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3064 | smp->data.u.sint = !smp->data.u.sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3065 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3066 | return 1; |
| 3067 | } |
| 3068 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 3069 | /* Takes a SINT on input, returns true is the value is odd, otherwise false. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3070 | * The output is a BOOL. |
| 3071 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3072 | static int sample_conv_arith_odd(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 3073 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3074 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3075 | smp->data.u.sint = smp->data.u.sint & 1; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3076 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3077 | return 1; |
| 3078 | } |
| 3079 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 3080 | /* Takes a SINT on input, returns true is the value is even, otherwise false. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3081 | * The output is a BOOL. |
| 3082 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3083 | static int sample_conv_arith_even(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 3084 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3085 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3086 | smp->data.u.sint = !(smp->data.u.sint & 1); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3087 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3088 | return 1; |
| 3089 | } |
| 3090 | |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 3091 | /* appends an optional const string, an optional variable contents and another |
| 3092 | * optional const string to an existing string. |
| 3093 | */ |
| 3094 | static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private) |
| 3095 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3096 | struct buffer *trash; |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 3097 | struct sample tmp; |
| 3098 | int max; |
| 3099 | |
Willy Tarreau | 79f4360 | 2021-01-08 16:08:43 +0100 | [diff] [blame] | 3100 | trash = alloc_trash_chunk(); |
William Dauchy | c965683 | 2021-01-11 11:05:58 +0100 | [diff] [blame] | 3101 | if (!trash) |
| 3102 | return 0; |
| 3103 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3104 | trash->data = smp->data.u.str.data; |
| 3105 | if (trash->data > trash->size - 1) |
| 3106 | trash->data = trash->size - 1; |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 3107 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3108 | memcpy(trash->area, smp->data.u.str.area, trash->data); |
| 3109 | trash->area[trash->data] = 0; |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 3110 | |
| 3111 | /* append first string */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3112 | max = arg_p[0].data.str.data; |
| 3113 | if (max > trash->size - 1 - trash->data) |
| 3114 | max = trash->size - 1 - trash->data; |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 3115 | |
| 3116 | if (max) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3117 | memcpy(trash->area + trash->data, arg_p[0].data.str.area, max); |
| 3118 | trash->data += max; |
| 3119 | trash->area[trash->data] = 0; |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 3120 | } |
| 3121 | |
| 3122 | /* append second string (variable) if it's found and we can turn it |
| 3123 | * into a string. |
| 3124 | */ |
| 3125 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
| 3126 | if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) && |
| 3127 | (sample_casts[tmp.data.type][SMP_T_STR] == c_none || |
| 3128 | sample_casts[tmp.data.type][SMP_T_STR](&tmp))) { |
| 3129 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3130 | max = tmp.data.u.str.data; |
| 3131 | if (max > trash->size - 1 - trash->data) |
| 3132 | max = trash->size - 1 - trash->data; |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 3133 | |
| 3134 | if (max) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3135 | memcpy(trash->area + trash->data, tmp.data.u.str.area, |
| 3136 | max); |
| 3137 | trash->data += max; |
| 3138 | trash->area[trash->data] = 0; |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 3139 | } |
| 3140 | } |
| 3141 | |
| 3142 | /* append third string */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3143 | max = arg_p[2].data.str.data; |
| 3144 | if (max > trash->size - 1 - trash->data) |
| 3145 | max = trash->size - 1 - trash->data; |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 3146 | |
| 3147 | if (max) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3148 | memcpy(trash->area + trash->data, arg_p[2].data.str.area, max); |
| 3149 | trash->data += max; |
| 3150 | trash->area[trash->data] = 0; |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | smp->data.u.str = *trash; |
| 3154 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 79f4360 | 2021-01-08 16:08:43 +0100 | [diff] [blame] | 3155 | smp_dup(smp); |
| 3156 | free_trash_chunk(trash); |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 3157 | return 1; |
| 3158 | } |
| 3159 | |
| 3160 | /* This function checks the "concat" converter's arguments and extracts the |
| 3161 | * variable name and its scope. |
| 3162 | */ |
| 3163 | static int smp_check_concat(struct arg *args, struct sample_conv *conv, |
| 3164 | const char *file, int line, char **err) |
| 3165 | { |
| 3166 | /* Try to decode a variable. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3167 | if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) { |
| 3168 | memprintf(err, "failed to register variable name '%s'", |
| 3169 | args[1].data.str.area); |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 3170 | return 0; |
| 3171 | } |
| 3172 | return 1; |
| 3173 | } |
| 3174 | |
Tim Duesterhus | f38175c | 2020-06-09 11:48:42 +0200 | [diff] [blame] | 3175 | /* Compares string with a variable containing a string. Return value |
Tim Duesterhus | ca097c1 | 2018-04-27 21:18:45 +0200 | [diff] [blame] | 3176 | * is compatible with strcmp(3)'s return value. |
| 3177 | */ |
| 3178 | static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private) |
| 3179 | { |
| 3180 | struct sample tmp; |
| 3181 | int max, result; |
| 3182 | |
| 3183 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
| 3184 | if (arg_p[0].type != ARGT_VAR) |
| 3185 | return 0; |
| 3186 | if (!vars_get_by_desc(&arg_p[0].data.var, &tmp)) |
| 3187 | return 0; |
| 3188 | if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp)) |
| 3189 | return 0; |
| 3190 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3191 | max = MIN(smp->data.u.str.data, tmp.data.u.str.data); |
| 3192 | result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max); |
Tim Duesterhus | ca097c1 | 2018-04-27 21:18:45 +0200 | [diff] [blame] | 3193 | if (result == 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3194 | if (smp->data.u.str.data != tmp.data.u.str.data) { |
| 3195 | if (smp->data.u.str.data < tmp.data.u.str.data) { |
Tim Duesterhus | ca097c1 | 2018-04-27 21:18:45 +0200 | [diff] [blame] | 3196 | result = -1; |
| 3197 | } |
| 3198 | else { |
| 3199 | result = 1; |
| 3200 | } |
| 3201 | } |
| 3202 | } |
| 3203 | |
| 3204 | smp->data.u.sint = result; |
| 3205 | smp->data.type = SMP_T_SINT; |
| 3206 | return 1; |
| 3207 | } |
| 3208 | |
Tim Duesterhus | f38175c | 2020-06-09 11:48:42 +0200 | [diff] [blame] | 3209 | #ifdef USE_OPENSSL |
| 3210 | /* Compares bytestring with a variable containing a bytestring. Return value |
| 3211 | * is `true` if both bytestrings are bytewise identical and `false` otherwise. |
| 3212 | * |
| 3213 | * Comparison will be performed in constant time if both bytestrings are of |
| 3214 | * the same length. If the lengths differ execution time will not be constant. |
| 3215 | */ |
| 3216 | static int sample_conv_secure_memcmp(const struct arg *arg_p, struct sample *smp, void *private) |
| 3217 | { |
| 3218 | struct sample tmp; |
| 3219 | int result; |
| 3220 | |
| 3221 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
| 3222 | if (arg_p[0].type != ARGT_VAR) |
| 3223 | return 0; |
| 3224 | if (!vars_get_by_desc(&arg_p[0].data.var, &tmp)) |
| 3225 | return 0; |
| 3226 | if (!sample_casts[tmp.data.type][SMP_T_BIN](&tmp)) |
| 3227 | return 0; |
| 3228 | |
| 3229 | if (smp->data.u.str.data != tmp.data.u.str.data) { |
| 3230 | smp->data.u.sint = 0; |
| 3231 | smp->data.type = SMP_T_BOOL; |
| 3232 | return 1; |
| 3233 | } |
| 3234 | |
| 3235 | /* The following comparison is performed in constant time. */ |
| 3236 | result = CRYPTO_memcmp(smp->data.u.str.area, tmp.data.u.str.area, smp->data.u.str.data); |
| 3237 | |
| 3238 | smp->data.u.sint = result == 0; |
| 3239 | smp->data.type = SMP_T_BOOL; |
| 3240 | return 1; |
| 3241 | } |
| 3242 | #endif |
| 3243 | |
Tim Duesterhus | 3943e4f | 2020-09-11 14:25:23 +0200 | [diff] [blame] | 3244 | /* Takes a boolean as input. Returns the first argument if that boolean is true and |
| 3245 | * the second argument otherwise. |
| 3246 | */ |
| 3247 | static int sample_conv_iif(const struct arg *arg_p, struct sample *smp, void *private) |
| 3248 | { |
| 3249 | smp->data.type = SMP_T_STR; |
| 3250 | smp->flags |= SMP_F_CONST; |
| 3251 | |
| 3252 | if (smp->data.u.sint) { |
| 3253 | smp->data.u.str.data = arg_p[0].data.str.data; |
| 3254 | smp->data.u.str.area = arg_p[0].data.str.area; |
| 3255 | } |
| 3256 | else { |
| 3257 | smp->data.u.str.data = arg_p[1].data.str.data; |
| 3258 | smp->data.u.str.area = arg_p[1].data.str.area; |
| 3259 | } |
| 3260 | |
| 3261 | return 1; |
| 3262 | } |
| 3263 | |
Frédéric Lécaille | 50290fb | 2019-02-27 14:34:51 +0100 | [diff] [blame] | 3264 | #define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */ |
| 3265 | #define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */ |
| 3266 | #define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ) |
| 3267 | |
| 3268 | /* |
| 3269 | * Extract the field value of an input binary sample. Takes a mandatory argument: |
| 3270 | * the protocol buffers field identifier (dotted notation) internally represented |
| 3271 | * as an array of unsigned integers and its size. |
| 3272 | * Return 1 if the field was found, 0 if not. |
| 3273 | */ |
| 3274 | static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private) |
| 3275 | { |
| 3276 | unsigned char *pos; |
| 3277 | size_t grpc_left; |
Frédéric Lécaille | 50290fb | 2019-02-27 14:34:51 +0100 | [diff] [blame] | 3278 | |
| 3279 | pos = (unsigned char *)smp->data.u.str.area; |
Frédéric Lécaille | 50290fb | 2019-02-27 14:34:51 +0100 | [diff] [blame] | 3280 | grpc_left = smp->data.u.str.data; |
| 3281 | |
Frédéric Lécaille | 7c93e88 | 2019-03-04 07:33:41 +0100 | [diff] [blame] | 3282 | while (grpc_left > GRPC_MSG_HEADER_SZ) { |
Frédéric Lécaille | 50290fb | 2019-02-27 14:34:51 +0100 | [diff] [blame] | 3283 | size_t grpc_msg_len, left; |
Frédéric Lécaille | 50290fb | 2019-02-27 14:34:51 +0100 | [diff] [blame] | 3284 | |
| 3285 | grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ)); |
| 3286 | |
| 3287 | pos += GRPC_MSG_HEADER_SZ; |
| 3288 | grpc_left -= GRPC_MSG_HEADER_SZ; |
| 3289 | |
| 3290 | if (grpc_left < left) |
| 3291 | return 0; |
| 3292 | |
Frédéric Lécaille | 5f33f85 | 2019-03-06 08:03:44 +0100 | [diff] [blame] | 3293 | if (protobuf_field_lookup(arg_p, smp, &pos, &left)) |
| 3294 | return 1; |
Frédéric Lécaille | 50290fb | 2019-02-27 14:34:51 +0100 | [diff] [blame] | 3295 | |
Frédéric Lécaille | 50290fb | 2019-02-27 14:34:51 +0100 | [diff] [blame] | 3296 | grpc_left -= grpc_msg_len; |
| 3297 | } |
| 3298 | |
| 3299 | return 0; |
| 3300 | } |
| 3301 | |
Frédéric Lécaille | bfe6138 | 2019-03-06 14:34:36 +0100 | [diff] [blame] | 3302 | static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private) |
| 3303 | { |
| 3304 | unsigned char *pos; |
| 3305 | size_t left; |
| 3306 | |
| 3307 | pos = (unsigned char *)smp->data.u.str.area; |
| 3308 | left = smp->data.u.str.data; |
| 3309 | |
| 3310 | return protobuf_field_lookup(arg_p, smp, &pos, &left); |
| 3311 | } |
| 3312 | |
| 3313 | static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv, |
| 3314 | const char *file, int line, char **err) |
Frédéric Lécaille | 756d97f | 2019-03-04 19:03:48 +0100 | [diff] [blame] | 3315 | { |
| 3316 | if (!args[1].type) { |
| 3317 | args[1].type = ARGT_SINT; |
| 3318 | args[1].data.sint = PBUF_T_BINARY; |
| 3319 | } |
| 3320 | else { |
| 3321 | int pbuf_type; |
| 3322 | |
| 3323 | pbuf_type = protobuf_type(args[1].data.str.area); |
| 3324 | if (pbuf_type == -1) { |
| 3325 | memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area); |
| 3326 | return 0; |
| 3327 | } |
| 3328 | |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 3329 | chunk_destroy(&args[1].data.str); |
Frédéric Lécaille | 756d97f | 2019-03-04 19:03:48 +0100 | [diff] [blame] | 3330 | args[1].type = ARGT_SINT; |
| 3331 | args[1].data.sint = pbuf_type; |
| 3332 | } |
| 3333 | |
| 3334 | return 1; |
| 3335 | } |
| 3336 | |
Tim Duesterhus | ca097c1 | 2018-04-27 21:18:45 +0200 | [diff] [blame] | 3337 | /* This function checks the "strcmp" converter's arguments and extracts the |
| 3338 | * variable name and its scope. |
| 3339 | */ |
| 3340 | static int smp_check_strcmp(struct arg *args, struct sample_conv *conv, |
| 3341 | const char *file, int line, char **err) |
| 3342 | { |
| 3343 | /* Try to decode a variable. */ |
| 3344 | if (vars_check_arg(&args[0], NULL)) |
| 3345 | return 1; |
| 3346 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3347 | memprintf(err, "failed to register variable name '%s'", |
| 3348 | args[0].data.str.area); |
Tim Duesterhus | ca097c1 | 2018-04-27 21:18:45 +0200 | [diff] [blame] | 3349 | return 0; |
| 3350 | } |
| 3351 | |
Tim Duesterhus | f38175c | 2020-06-09 11:48:42 +0200 | [diff] [blame] | 3352 | #ifdef USE_OPENSSL |
| 3353 | /* This function checks the "secure_memcmp" converter's arguments and extracts the |
| 3354 | * variable name and its scope. |
| 3355 | */ |
| 3356 | static int smp_check_secure_memcmp(struct arg *args, struct sample_conv *conv, |
| 3357 | const char *file, int line, char **err) |
| 3358 | { |
| 3359 | /* Try to decode a variable. */ |
| 3360 | if (vars_check_arg(&args[0], NULL)) |
| 3361 | return 1; |
| 3362 | |
| 3363 | memprintf(err, "failed to register variable name '%s'", |
| 3364 | args[0].data.str.area); |
| 3365 | return 0; |
| 3366 | } |
| 3367 | #endif |
| 3368 | |
Christopher Faulet | 4ccc12f | 2020-04-01 09:08:32 +0200 | [diff] [blame] | 3369 | /**/ |
| 3370 | static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private) |
| 3371 | { |
| 3372 | struct buffer *tmp; |
| 3373 | uint32_t n; |
| 3374 | |
| 3375 | n = htonl((uint32_t)smp->data.u.sint); |
| 3376 | tmp = get_trash_chunk(); |
| 3377 | |
| 3378 | memcpy(b_head(tmp), &n, 4); |
| 3379 | b_add(tmp, 4); |
| 3380 | |
| 3381 | smp->data.u.str = *tmp; |
| 3382 | smp->data.type = SMP_T_BIN; |
| 3383 | return 1; |
| 3384 | } |
| 3385 | |
Christopher Faulet | ea159d6 | 2020-04-01 16:21:44 +0200 | [diff] [blame] | 3386 | /**/ |
| 3387 | static int sample_conv_cut_crlf(const struct arg *arg_p, struct sample *smp, void *private) |
| 3388 | { |
| 3389 | char *p; |
| 3390 | size_t l; |
| 3391 | |
| 3392 | p = smp->data.u.str.area; |
| 3393 | for (l = 0; l < smp->data.u.str.data; l++) { |
| 3394 | if (*(p+l) == '\r' || *(p+l) == '\n') |
| 3395 | break; |
| 3396 | } |
| 3397 | smp->data.u.str.data = l; |
| 3398 | return 1; |
| 3399 | } |
| 3400 | |
Christopher Faulet | 51fc9d1 | 2020-04-01 17:24:41 +0200 | [diff] [blame] | 3401 | /**/ |
| 3402 | static int sample_conv_ltrim(const struct arg *arg_p, struct sample *smp, void *private) |
| 3403 | { |
| 3404 | char *delimiters, *p; |
| 3405 | size_t dlen, l; |
| 3406 | |
| 3407 | delimiters = arg_p[0].data.str.area; |
| 3408 | dlen = arg_p[0].data.str.data; |
| 3409 | |
| 3410 | l = smp->data.u.str.data; |
| 3411 | p = smp->data.u.str.area; |
| 3412 | while (l && memchr(delimiters, *p, dlen) != NULL) { |
| 3413 | p++; |
| 3414 | l--; |
| 3415 | } |
| 3416 | |
| 3417 | smp->data.u.str.area = p; |
| 3418 | smp->data.u.str.data = l; |
| 3419 | return 1; |
| 3420 | } |
| 3421 | |
Christopher Faulet | 568415a | 2020-04-01 17:24:47 +0200 | [diff] [blame] | 3422 | /**/ |
| 3423 | static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *private) |
| 3424 | { |
| 3425 | char *delimiters, *p; |
| 3426 | size_t dlen, l; |
| 3427 | |
| 3428 | delimiters = arg_p[0].data.str.area; |
| 3429 | dlen = arg_p[0].data.str.data; |
| 3430 | |
| 3431 | l = smp->data.u.str.data; |
| 3432 | p = smp->data.u.str.area + l - 1; |
| 3433 | while (l && memchr(delimiters, *p, dlen) != NULL) { |
| 3434 | p--; |
| 3435 | l--; |
| 3436 | } |
| 3437 | |
| 3438 | smp->data.u.str.data = l; |
| 3439 | return 1; |
| 3440 | } |
| 3441 | |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 3442 | /************************************************************************/ |
| 3443 | /* All supported sample fetch functions must be declared here */ |
| 3444 | /************************************************************************/ |
| 3445 | |
| 3446 | /* force TRUE to be returned at the fetch level */ |
| 3447 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3448 | smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 3449 | { |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 3450 | if (!smp_make_rw(smp)) |
| 3451 | return 0; |
| 3452 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3453 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3454 | smp->data.u.sint = 1; |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 3455 | return 1; |
| 3456 | } |
| 3457 | |
| 3458 | /* force FALSE to be returned at the fetch level */ |
| 3459 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3460 | smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 3461 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3462 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3463 | smp->data.u.sint = 0; |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 3464 | return 1; |
| 3465 | } |
| 3466 | |
| 3467 | /* retrieve environment variable $1 as a string */ |
| 3468 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3469 | smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 3470 | { |
| 3471 | char *env; |
| 3472 | |
| 3473 | if (!args || args[0].type != ARGT_STR) |
| 3474 | return 0; |
| 3475 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3476 | env = getenv(args[0].data.str.area); |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 3477 | if (!env) |
| 3478 | return 0; |
| 3479 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3480 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 3481 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3482 | smp->data.u.str.area = env; |
| 3483 | smp->data.u.str.data = strlen(env); |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 3484 | return 1; |
| 3485 | } |
| 3486 | |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 3487 | /* Validates the data unit argument passed to "date" fetch. Argument 1 support an |
| 3488 | * optional string representing the unit of the result: "s" for seconds, "ms" for |
| 3489 | * milliseconds and "us" for microseconds. |
| 3490 | * Returns 0 on error and non-zero if OK. |
| 3491 | */ |
| 3492 | int smp_check_date_unit(struct arg *args, char **err) |
| 3493 | { |
| 3494 | if (args[1].type == ARGT_STR) { |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 3495 | long long int unit; |
| 3496 | |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 3497 | if (strcmp(args[1].data.str.area, "s") == 0) { |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 3498 | unit = TIME_UNIT_S; |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 3499 | } |
| 3500 | else if (strcmp(args[1].data.str.area, "ms") == 0) { |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 3501 | unit = TIME_UNIT_MS; |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 3502 | } |
| 3503 | else if (strcmp(args[1].data.str.area, "us") == 0) { |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 3504 | unit = TIME_UNIT_US; |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 3505 | } |
| 3506 | else { |
| 3507 | memprintf(err, "expects 's', 'ms' or 'us', got '%s'", |
| 3508 | args[1].data.str.area); |
| 3509 | return 0; |
| 3510 | } |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 3511 | |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 3512 | chunk_destroy(&args[1].data.str); |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 3513 | args[1].type = ARGT_SINT; |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 3514 | args[1].data.sint = unit; |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 3515 | } |
| 3516 | else if (args[1].type != ARGT_STOP) { |
| 3517 | memprintf(err, "Unexpected arg type"); |
| 3518 | return 0; |
| 3519 | } |
| 3520 | |
| 3521 | return 1; |
| 3522 | } |
| 3523 | |
| 3524 | /* retrieve the current local date in epoch time, converts it to milliseconds |
| 3525 | * or microseconds if asked to in optional args[1] unit param, and applies an |
| 3526 | * optional args[0] offset. |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 3527 | */ |
| 3528 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3529 | smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 3530 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3531 | smp->data.u.sint = date.tv_sec; |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 3532 | |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 3533 | /* report in milliseconds */ |
| 3534 | if (args && args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) { |
| 3535 | smp->data.u.sint *= 1000; |
| 3536 | smp->data.u.sint += date.tv_usec / 1000; |
| 3537 | } |
| 3538 | /* report in microseconds */ |
| 3539 | else if (args && args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) { |
| 3540 | smp->data.u.sint *= 1000000; |
| 3541 | smp->data.u.sint += date.tv_usec; |
| 3542 | } |
| 3543 | |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 3544 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 3545 | if (args && args[0].type == ARGT_SINT) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3546 | smp->data.u.sint += args[0].data.sint; |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 3547 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3548 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 3549 | smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE; |
| 3550 | return 1; |
| 3551 | } |
| 3552 | |
Etienne Carriere | a792a0a | 2018-01-17 13:43:24 +0100 | [diff] [blame] | 3553 | /* retrieve the current microsecond part of the date */ |
| 3554 | static int |
| 3555 | smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3556 | { |
| 3557 | smp->data.u.sint = date.tv_usec; |
| 3558 | smp->data.type = SMP_T_SINT; |
| 3559 | smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE; |
| 3560 | return 1; |
| 3561 | } |
| 3562 | |
| 3563 | |
Nenad Merdanovic | 807a6e7 | 2017-03-12 22:00:00 +0100 | [diff] [blame] | 3564 | /* returns the hostname */ |
| 3565 | static int |
| 3566 | smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3567 | { |
| 3568 | smp->data.type = SMP_T_STR; |
| 3569 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3570 | smp->data.u.str.area = hostname; |
| 3571 | smp->data.u.str.data = strlen(hostname); |
Nenad Merdanovic | 807a6e7 | 2017-03-12 22:00:00 +0100 | [diff] [blame] | 3572 | return 1; |
| 3573 | } |
| 3574 | |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 3575 | /* returns the number of processes */ |
| 3576 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3577 | smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 3578 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3579 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3580 | smp->data.u.sint = global.nbproc; |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 3581 | return 1; |
| 3582 | } |
| 3583 | |
| 3584 | /* returns the number of the current process (between 1 and nbproc */ |
| 3585 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3586 | smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 3587 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3588 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3589 | smp->data.u.sint = relative_pid; |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 3590 | return 1; |
| 3591 | } |
| 3592 | |
Christopher Faulet | 34adb2a | 2017-11-21 21:45:38 +0100 | [diff] [blame] | 3593 | /* returns the number of the current thread (between 1 and nbthread */ |
| 3594 | static int |
| 3595 | smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3596 | { |
| 3597 | smp->data.type = SMP_T_SINT; |
| 3598 | smp->data.u.sint = tid; |
| 3599 | return 1; |
| 3600 | } |
| 3601 | |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 3602 | /* generate a random 32-bit integer for whatever purpose, with an optional |
| 3603 | * range specified in argument. |
| 3604 | */ |
| 3605 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3606 | smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 3607 | { |
Willy Tarreau | aa8bbc1 | 2020-03-08 18:01:10 +0100 | [diff] [blame] | 3608 | smp->data.u.sint = ha_random32(); |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 3609 | |
| 3610 | /* reduce if needed. Don't do a modulo, use all bits! */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 3611 | if (args && args[0].type == ARGT_SINT) |
Willy Tarreau | aa8bbc1 | 2020-03-08 18:01:10 +0100 | [diff] [blame] | 3612 | smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32; |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 3613 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3614 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 3615 | smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE; |
| 3616 | return 1; |
| 3617 | } |
| 3618 | |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 3619 | /* returns true if the current process is stopping */ |
| 3620 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3621 | smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 3622 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3623 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3624 | smp->data.u.sint = stopping; |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 3625 | return 1; |
| 3626 | } |
| 3627 | |
Willy Tarreau | 70fe944 | 2018-11-22 16:07:39 +0100 | [diff] [blame] | 3628 | /* returns the number of calls of the current stream's process_stream() */ |
| 3629 | static int |
| 3630 | smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3631 | { |
Willy Tarreau | e0dd210 | 2020-04-29 11:59:02 +0200 | [diff] [blame] | 3632 | if (!smp->strm) |
| 3633 | return 0; |
| 3634 | |
Willy Tarreau | 70fe944 | 2018-11-22 16:07:39 +0100 | [diff] [blame] | 3635 | smp->data.type = SMP_T_SINT; |
| 3636 | smp->data.u.sint = smp->strm->task->calls; |
| 3637 | return 1; |
| 3638 | } |
| 3639 | |
| 3640 | /* returns the average number of nanoseconds spent processing the stream per call */ |
| 3641 | static int |
| 3642 | smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3643 | { |
Willy Tarreau | e0dd210 | 2020-04-29 11:59:02 +0200 | [diff] [blame] | 3644 | if (!smp->strm) |
| 3645 | return 0; |
| 3646 | |
Willy Tarreau | 70fe944 | 2018-11-22 16:07:39 +0100 | [diff] [blame] | 3647 | smp->data.type = SMP_T_SINT; |
| 3648 | smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0; |
| 3649 | return 1; |
| 3650 | } |
| 3651 | |
| 3652 | /* returns the total number of nanoseconds spent processing the stream */ |
| 3653 | static int |
| 3654 | smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3655 | { |
Willy Tarreau | e0dd210 | 2020-04-29 11:59:02 +0200 | [diff] [blame] | 3656 | if (!smp->strm) |
| 3657 | return 0; |
| 3658 | |
Willy Tarreau | 70fe944 | 2018-11-22 16:07:39 +0100 | [diff] [blame] | 3659 | smp->data.type = SMP_T_SINT; |
| 3660 | smp->data.u.sint = smp->strm->task->cpu_time; |
| 3661 | return 1; |
| 3662 | } |
| 3663 | |
| 3664 | /* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */ |
| 3665 | static int |
| 3666 | smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3667 | { |
Willy Tarreau | e0dd210 | 2020-04-29 11:59:02 +0200 | [diff] [blame] | 3668 | if (!smp->strm) |
| 3669 | return 0; |
| 3670 | |
Willy Tarreau | 70fe944 | 2018-11-22 16:07:39 +0100 | [diff] [blame] | 3671 | smp->data.type = SMP_T_SINT; |
| 3672 | smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0; |
| 3673 | return 1; |
| 3674 | } |
| 3675 | |
| 3676 | /* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */ |
| 3677 | static int |
| 3678 | smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3679 | { |
Willy Tarreau | e0dd210 | 2020-04-29 11:59:02 +0200 | [diff] [blame] | 3680 | if (!smp->strm) |
| 3681 | return 0; |
| 3682 | |
Willy Tarreau | 70fe944 | 2018-11-22 16:07:39 +0100 | [diff] [blame] | 3683 | smp->data.type = SMP_T_SINT; |
| 3684 | smp->data.u.sint = smp->strm->task->lat_time; |
| 3685 | return 1; |
| 3686 | } |
| 3687 | |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3688 | static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3689 | { |
| 3690 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3691 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3692 | smp->data.u.str.area = args[0].data.str.area; |
| 3693 | smp->data.u.str.data = args[0].data.str.data; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3694 | return 1; |
| 3695 | } |
| 3696 | |
| 3697 | static int smp_check_const_bool(struct arg *args, char **err) |
| 3698 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3699 | if (strcasecmp(args[0].data.str.area, "true") == 0 || |
| 3700 | strcasecmp(args[0].data.str.area, "1") == 0) { |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 3701 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 3702 | args[0].type = ARGT_SINT; |
| 3703 | args[0].data.sint = 1; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3704 | return 1; |
| 3705 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3706 | if (strcasecmp(args[0].data.str.area, "false") == 0 || |
| 3707 | strcasecmp(args[0].data.str.area, "0") == 0) { |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 3708 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 3709 | args[0].type = ARGT_SINT; |
| 3710 | args[0].data.sint = 0; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3711 | return 1; |
| 3712 | } |
| 3713 | memprintf(err, "Expects 'true', 'false', '0' or '1'"); |
| 3714 | return 0; |
| 3715 | } |
| 3716 | |
| 3717 | static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3718 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3719 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3720 | smp->data.u.sint = args[0].data.sint; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3721 | return 1; |
| 3722 | } |
| 3723 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 3724 | static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3725 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3726 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3727 | smp->data.u.sint = args[0].data.sint; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3728 | return 1; |
| 3729 | } |
| 3730 | |
| 3731 | static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3732 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3733 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3734 | smp->data.u.ipv4 = args[0].data.ipv4; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3735 | return 1; |
| 3736 | } |
| 3737 | |
| 3738 | static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3739 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3740 | smp->data.type = SMP_T_IPV6; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3741 | smp->data.u.ipv6 = args[0].data.ipv6; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3742 | return 1; |
| 3743 | } |
| 3744 | |
| 3745 | static int smp_check_const_bin(struct arg *args, char **err) |
| 3746 | { |
David Carlier | 64a16ab | 2016-04-08 10:37:02 +0100 | [diff] [blame] | 3747 | char *binstr = NULL; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3748 | int binstrlen; |
| 3749 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3750 | if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err)) |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3751 | return 0; |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 3752 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3753 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3754 | args[0].data.str.area = binstr; |
| 3755 | args[0].data.str.data = binstrlen; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3756 | return 1; |
| 3757 | } |
| 3758 | |
| 3759 | static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3760 | { |
| 3761 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3762 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3763 | smp->data.u.str.area = args[0].data.str.area; |
| 3764 | smp->data.u.str.data = args[0].data.str.data; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3765 | return 1; |
| 3766 | } |
| 3767 | |
| 3768 | static int smp_check_const_meth(struct arg *args, char **err) |
| 3769 | { |
| 3770 | enum http_meth_t meth; |
| 3771 | int i; |
| 3772 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3773 | meth = find_http_meth(args[0].data.str.area, args[0].data.str.data); |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3774 | if (meth != HTTP_METH_OTHER) { |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 3775 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 3776 | args[0].type = ARGT_SINT; |
| 3777 | args[0].data.sint = meth; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3778 | } else { |
Ilya Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 3779 | /* Check method avalaibility. A method is a token defined as : |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3780 | * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." / |
| 3781 | * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA |
| 3782 | * token = 1*tchar |
| 3783 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3784 | for (i = 0; i < args[0].data.str.data; i++) { |
| 3785 | if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) { |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3786 | memprintf(err, "expects valid method."); |
| 3787 | return 0; |
| 3788 | } |
| 3789 | } |
| 3790 | } |
| 3791 | return 1; |
| 3792 | } |
| 3793 | |
| 3794 | static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3795 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3796 | smp->data.type = SMP_T_METH; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 3797 | if (args[0].type == ARGT_SINT) { |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3798 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3799 | smp->data.u.meth.meth = args[0].data.sint; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3800 | smp->data.u.meth.str.area = ""; |
| 3801 | smp->data.u.meth.str.data = 0; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3802 | } else { |
| 3803 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3804 | smp->data.u.meth.meth = HTTP_METH_OTHER; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3805 | smp->data.u.meth.str.area = args[0].data.str.area; |
| 3806 | smp->data.u.meth.str.data = args[0].data.str.data; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3807 | } |
| 3808 | return 1; |
| 3809 | } |
| 3810 | |
Luca Schimweg | 8a694b8 | 2019-09-10 15:42:52 +0200 | [diff] [blame] | 3811 | // This function checks the "uuid" sample's arguments. |
| 3812 | // Function won't get called when no parameter is specified (maybe a bug?) |
| 3813 | static int smp_check_uuid(struct arg *args, char **err) |
| 3814 | { |
| 3815 | if (!args[0].type) { |
| 3816 | args[0].type = ARGT_SINT; |
| 3817 | args[0].data.sint = 4; |
| 3818 | } |
| 3819 | else if (args[0].data.sint != 4) { |
| 3820 | memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint); |
| 3821 | return 0; |
| 3822 | } |
| 3823 | |
| 3824 | return 1; |
| 3825 | } |
| 3826 | |
| 3827 | // Generate a RFC4122 UUID (default is v4 = fully random) |
| 3828 | static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3829 | { |
| 3830 | if (args[0].data.sint == 4 || !args[0].type) { |
Willy Tarreau | ee3bcdd | 2020-03-08 17:48:17 +0100 | [diff] [blame] | 3831 | ha_generate_uuid(&trash); |
Luca Schimweg | 8a694b8 | 2019-09-10 15:42:52 +0200 | [diff] [blame] | 3832 | smp->data.type = SMP_T_STR; |
| 3833 | smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE; |
| 3834 | smp->data.u.str = trash; |
| 3835 | return 1; |
| 3836 | } |
| 3837 | |
| 3838 | // more implementations of other uuid formats possible here |
| 3839 | return 0; |
| 3840 | } |
| 3841 | |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 3842 | /* Note: must not be declared <const> as its list will be overwritten. |
| 3843 | * Note: fetches that may return multiple types must be declared as the lowest |
| 3844 | * common denominator, the type that can be casted into all other ones. For |
| 3845 | * instance IPv4/IPv6 must be declared IPv4. |
| 3846 | */ |
| 3847 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
| 3848 | { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN }, |
| 3849 | { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 3850 | { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN }, |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 3851 | { "date", smp_fetch_date, ARG2(0,SINT,STR), smp_check_date_unit, SMP_T_SINT, SMP_USE_INTRN }, |
Etienne Carriere | a792a0a | 2018-01-17 13:43:24 +0100 | [diff] [blame] | 3852 | { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_INTRN }, |
Nenad Merdanovic | 807a6e7 | 2017-03-12 22:00:00 +0100 | [diff] [blame] | 3853 | { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_INTRN }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 3854 | { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN }, |
| 3855 | { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN }, |
Christopher Faulet | 34adb2a | 2017-11-21 21:45:38 +0100 | [diff] [blame] | 3856 | { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_INTRN }, |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 3857 | { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN }, |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 3858 | { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN }, |
Willy Tarreau | 70fe944 | 2018-11-22 16:07:39 +0100 | [diff] [blame] | 3859 | { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN }, |
Luca Schimweg | 8a694b8 | 2019-09-10 15:42:52 +0200 | [diff] [blame] | 3860 | { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_INTRN }, |
Willy Tarreau | 70fe944 | 2018-11-22 16:07:39 +0100 | [diff] [blame] | 3861 | |
| 3862 | { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN }, |
| 3863 | { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN }, |
| 3864 | { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN }, |
| 3865 | { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN }, |
| 3866 | { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN }, |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3867 | |
| 3868 | { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN }, |
| 3869 | { "bool", smp_fetch_const_bool, ARG1(1,STR), smp_check_const_bool, SMP_T_BOOL, SMP_USE_INTRN }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 3870 | { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN }, |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3871 | { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN }, |
| 3872 | { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN }, |
| 3873 | { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN }, |
| 3874 | { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN }, |
| 3875 | |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 3876 | { /* END */ }, |
| 3877 | }}; |
| 3878 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 3879 | INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws); |
| 3880 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 3881 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 3882 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 3883 | { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY }, |
Holger Just | 1bfc24b | 2017-05-06 00:56:53 +0200 | [diff] [blame] | 3884 | { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN }, |
Emeric Brun | 53d1a98 | 2014-04-30 18:21:37 +0200 | [diff] [blame] | 3885 | { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR }, |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 3886 | { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR }, |
| 3887 | { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR }, |
Willy Tarreau | 60a2ee7 | 2017-12-15 07:13:48 +0100 | [diff] [blame] | 3888 | { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT }, |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 3889 | { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR }, |
Dragan Dosen | 3f957b2 | 2017-10-24 09:27:34 +0200 | [diff] [blame] | 3890 | { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT }, |
Tim Duesterhus | 1478aa7 | 2018-01-25 16:24:51 +0100 | [diff] [blame] | 3891 | { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 3892 | { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR }, |
| 3893 | { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR }, |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 3894 | { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT }, |
Emmanuel Hocdet | 50791a7 | 2018-03-21 11:19:01 +0100 | [diff] [blame] | 3895 | { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT }, |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 3896 | { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT }, |
| 3897 | { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT }, |
| 3898 | { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT }, |
Thierry FOURNIER | 01e0974 | 2016-12-26 11:46:11 +0100 | [diff] [blame] | 3899 | { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT }, |
| 3900 | { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT }, |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 3901 | { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR }, |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 3902 | { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN }, |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 3903 | { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR }, |
| 3904 | { "word", sample_conv_word, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR }, |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 3905 | { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR }, |
Dragan Dosen | 6e5a9ca | 2017-10-24 09:18:23 +0200 | [diff] [blame] | 3906 | { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN }, |
Tim Duesterhus | d437630 | 2019-06-17 12:41:44 +0200 | [diff] [blame] | 3907 | #ifdef USE_OPENSSL |
Tim Duesterhus | cd37324 | 2019-12-17 12:31:20 +0100 | [diff] [blame] | 3908 | { "sha2", sample_conv_sha2, ARG1(0, SINT), smp_check_sha2, SMP_T_BIN, SMP_T_BIN }, |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 3909 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL) |
| 3910 | { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN }, |
| 3911 | #endif |
Patrick Gansterer | 8e36651 | 2020-04-22 16:47:57 +0200 | [diff] [blame] | 3912 | { "digest", sample_conv_crypto_digest, ARG1(1,STR), check_crypto_digest, SMP_T_BIN, SMP_T_BIN }, |
| 3913 | { "hmac", sample_conv_crypto_hmac, ARG2(2,STR,STR), check_crypto_hmac, SMP_T_BIN, SMP_T_BIN }, |
Tim Duesterhus | d437630 | 2019-06-17 12:41:44 +0200 | [diff] [blame] | 3914 | #endif |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 3915 | { "concat", sample_conv_concat, ARG3(1,STR,STR,STR), smp_check_concat, SMP_T_STR, SMP_T_STR }, |
Tim Duesterhus | ca097c1 | 2018-04-27 21:18:45 +0200 | [diff] [blame] | 3916 | { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT }, |
Tim Duesterhus | f38175c | 2020-06-09 11:48:42 +0200 | [diff] [blame] | 3917 | #ifdef USE_OPENSSL |
| 3918 | { "secure_memcmp", sample_conv_secure_memcmp, ARG1(1,STR), smp_check_secure_memcmp, SMP_T_BIN, SMP_T_BOOL }, |
| 3919 | #endif |
Frédéric Lécaille | 50290fb | 2019-02-27 14:34:51 +0100 | [diff] [blame] | 3920 | |
| 3921 | /* gRPC converters. */ |
Frédéric Lécaille | bfe6138 | 2019-03-06 14:34:36 +0100 | [diff] [blame] | 3922 | { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN }, |
| 3923 | { "protobuf", sample_conv_protobuf, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN }, |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3924 | |
Tim Duesterhus | 3943e4f | 2020-09-11 14:25:23 +0200 | [diff] [blame] | 3925 | { "iif", sample_conv_iif, ARG2(2, STR, STR), NULL, SMP_T_BOOL, SMP_T_STR }, |
| 3926 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 3927 | { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 3928 | { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 3929 | { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 3930 | { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT }, |
| 3931 | { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL }, |
| 3932 | { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL }, |
| 3933 | { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL }, |
| 3934 | { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL }, |
| 3935 | { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 3936 | { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 3937 | { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 3938 | { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 3939 | { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 3940 | { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT }, |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3941 | |
Christopher Faulet | ea159d6 | 2020-04-01 16:21:44 +0200 | [diff] [blame] | 3942 | { "htonl", sample_conv_htonl, 0, NULL, SMP_T_SINT, SMP_T_BIN }, |
| 3943 | { "cut_crlf", sample_conv_cut_crlf, 0, NULL, SMP_T_STR, SMP_T_STR }, |
Christopher Faulet | 51fc9d1 | 2020-04-01 17:24:41 +0200 | [diff] [blame] | 3944 | { "ltrim", sample_conv_ltrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR }, |
Christopher Faulet | 568415a | 2020-04-01 17:24:47 +0200 | [diff] [blame] | 3945 | { "rtrim", sample_conv_rtrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR }, |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 3946 | { NULL, NULL, 0, 0, 0 }, |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 3947 | }}; |
| 3948 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 3949 | INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws); |