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