Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1 | /* |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 2 | * Sample management functions. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 5 | * Copyright (C) 2012 Willy Tarreau <w@1wt.eu> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | */ |
| 13 | |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 14 | #include <ctype.h> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 15 | #include <string.h> |
| 16 | #include <arpa/inet.h> |
Willy Tarreau | dd2f85e | 2012-09-02 22:34:23 +0200 | [diff] [blame] | 17 | #include <stdio.h> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 18 | |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 19 | #include <types/global.h> |
| 20 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 21 | #include <common/chunk.h> |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 22 | #include <common/hash.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 23 | #include <common/standard.h> |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 24 | #include <common/uri_auth.h> |
Emeric Brun | 53d1a98 | 2014-04-30 18:21:37 +0200 | [diff] [blame] | 25 | #include <common/base64.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 26 | |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 27 | #include <proto/arg.h> |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 28 | #include <proto/auth.h> |
| 29 | #include <proto/log.h> |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 30 | #include <proto/proto_http.h> |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 31 | #include <proto/proxy.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 32 | #include <proto/sample.h> |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 33 | #include <proto/stick_table.h> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 34 | |
Willy Tarreau | 1cf8f08 | 2014-02-07 12:14:54 +0100 | [diff] [blame] | 35 | /* sample type names */ |
| 36 | const char *smp_to_type[SMP_TYPES] = { |
| 37 | [SMP_T_BOOL] = "bool", |
| 38 | [SMP_T_UINT] = "uint", |
| 39 | [SMP_T_SINT] = "sint", |
| 40 | [SMP_T_ADDR] = "addr", |
| 41 | [SMP_T_IPV4] = "ipv4", |
| 42 | [SMP_T_IPV6] = "ipv6", |
| 43 | [SMP_T_STR] = "str", |
| 44 | [SMP_T_BIN] = "bin", |
Willy Tarreau | 1cf8f08 | 2014-02-07 12:14:54 +0100 | [diff] [blame] | 45 | }; |
| 46 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 47 | /* static sample used in sample_process() when <p> is NULL */ |
Willy Tarreau | b4a88f0 | 2012-04-23 21:35:11 +0200 | [diff] [blame] | 48 | static struct sample temp_smp; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 49 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 50 | /* list head of all known sample fetch keywords */ |
| 51 | static struct sample_fetch_kw_list sample_fetches = { |
| 52 | .list = LIST_HEAD_INIT(sample_fetches.list) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 53 | }; |
| 54 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 55 | /* list head of all known sample format conversion keywords */ |
| 56 | static struct sample_conv_kw_list sample_convs = { |
| 57 | .list = LIST_HEAD_INIT(sample_convs.list) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 58 | }; |
| 59 | |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 60 | const unsigned int fetch_cap[SMP_SRC_ENTRIES] = { |
| 61 | [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 62 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 63 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 64 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 65 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 66 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 67 | SMP_VAL_FE_LOG_END), |
| 68 | |
| 69 | [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 70 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 71 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 72 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 73 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 74 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 75 | SMP_VAL_FE_LOG_END), |
| 76 | |
| 77 | [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 78 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 79 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 80 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 81 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 82 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 83 | SMP_VAL_FE_LOG_END), |
| 84 | |
| 85 | [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 86 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 87 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 88 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 89 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 90 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 91 | SMP_VAL_FE_LOG_END), |
| 92 | |
| 93 | [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 94 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 95 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 96 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 97 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 98 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 99 | SMP_VAL_FE_LOG_END), |
| 100 | |
| 101 | [SMP_SRC_TRACK] = (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 | |
| 107 | SMP_VAL_FE_LOG_END), |
| 108 | |
| 109 | [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT | |
| 110 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 111 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 112 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ | |
| 113 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 114 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 115 | SMP_VAL___________), |
| 116 | |
| 117 | [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT | |
| 118 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 119 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 120 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ | |
| 121 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 122 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 123 | SMP_VAL___________), |
| 124 | |
| 125 | [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT | |
| 126 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 127 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 128 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 129 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 130 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 131 | SMP_VAL_FE_LOG_END), |
| 132 | |
| 133 | [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 134 | SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 135 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 136 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ | |
| 137 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 138 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 139 | SMP_VAL___________), |
| 140 | |
| 141 | [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 142 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 143 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 144 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 145 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 146 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 147 | SMP_VAL_FE_LOG_END), |
| 148 | |
| 149 | [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 150 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 151 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 152 | SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 153 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 154 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 155 | SMP_VAL_FE_LOG_END), |
| 156 | |
| 157 | [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 158 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 159 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 160 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 161 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 162 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 163 | SMP_VAL_FE_LOG_END), |
| 164 | |
| 165 | [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 166 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 167 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 168 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 169 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 170 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 171 | SMP_VAL_FE_LOG_END), |
| 172 | |
| 173 | [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 174 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 175 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 176 | SMP_VAL___________ | SMP_VAL___________ | 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 | |
| 179 | SMP_VAL___________), |
| 180 | |
| 181 | [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 182 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 183 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 184 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 185 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 186 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 187 | SMP_VAL___________), |
| 188 | |
| 189 | [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 190 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 191 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 192 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 193 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 194 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 195 | SMP_VAL_FE_LOG_END), |
| 196 | |
| 197 | [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 198 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 199 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 200 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 201 | SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 202 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 203 | SMP_VAL___________), |
| 204 | |
| 205 | [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 206 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 207 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 208 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 209 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 210 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 211 | SMP_VAL_FE_LOG_END), |
| 212 | |
| 213 | [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 214 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 215 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 216 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 217 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 218 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 219 | SMP_VAL_FE_LOG_END), |
| 220 | |
| 221 | [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 222 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 223 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 224 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 225 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 226 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 227 | SMP_VAL_FE_LOG_END), |
| 228 | |
| 229 | [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 230 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 231 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 232 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 233 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 234 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 235 | SMP_VAL_FE_LOG_END), |
| 236 | }; |
| 237 | |
| 238 | static const char *fetch_src_names[SMP_SRC_ENTRIES] = { |
| 239 | [SMP_SRC_INTRN] = "internal state", |
| 240 | [SMP_SRC_LISTN] = "listener", |
| 241 | [SMP_SRC_FTEND] = "frontend", |
| 242 | [SMP_SRC_L4CLI] = "client address", |
| 243 | [SMP_SRC_L5CLI] = "client-side connection", |
| 244 | [SMP_SRC_TRACK] = "track counters", |
| 245 | [SMP_SRC_L6REQ] = "request buffer", |
| 246 | [SMP_SRC_HRQHV] = "HTTP request headers", |
| 247 | [SMP_SRC_HRQHP] = "HTTP request", |
| 248 | [SMP_SRC_HRQBO] = "HTTP request body", |
| 249 | [SMP_SRC_BKEND] = "backend", |
| 250 | [SMP_SRC_SERVR] = "server", |
| 251 | [SMP_SRC_L4SRV] = "server address", |
| 252 | [SMP_SRC_L5SRV] = "server-side connection", |
| 253 | [SMP_SRC_L6RES] = "response buffer", |
| 254 | [SMP_SRC_HRSHV] = "HTTP response headers", |
| 255 | [SMP_SRC_HRSHP] = "HTTP response", |
| 256 | [SMP_SRC_HRSBO] = "HTTP response body", |
| 257 | [SMP_SRC_RQFIN] = "request buffer statistics", |
| 258 | [SMP_SRC_RSFIN] = "response buffer statistics", |
| 259 | [SMP_SRC_TXFIN] = "transaction statistics", |
| 260 | [SMP_SRC_SSFIN] = "session statistics", |
| 261 | }; |
| 262 | |
Willy Tarreau | bf8e251 | 2013-03-25 14:52:41 +0100 | [diff] [blame] | 263 | static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = { |
| 264 | [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule", |
| 265 | [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule", |
| 266 | [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule", |
| 267 | [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule", |
| 268 | [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule", |
| 269 | [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule", |
| 270 | [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule", |
| 271 | [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule", |
| 272 | [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule", |
| 273 | [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule", |
| 274 | [SMP_CKP_BE_SRV_CON] = "server source selection", |
| 275 | [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule", |
| 276 | [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule", |
| 277 | [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule", |
| 278 | [SMP_CKP_BE_STO_RUL] = "backend stick-store rule", |
| 279 | [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule", |
| 280 | [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule", |
| 281 | [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule", |
| 282 | [SMP_CKP_FE_LOG_END] = "logs", |
| 283 | }; |
| 284 | |
Thierry FOURNIER | 348971e | 2013-11-21 10:50:10 +0100 | [diff] [blame] | 285 | /* This function returns the type of the data returned by the sample_expr. |
| 286 | * It assumes that the <expr> and all of its converters are properly |
| 287 | * initialized. |
| 288 | */ |
| 289 | inline |
| 290 | int smp_expr_output_type(struct sample_expr *expr) |
| 291 | { |
| 292 | struct sample_conv_expr *smp_expr; |
| 293 | |
| 294 | if (!LIST_ISEMPTY(&expr->conv_exprs)) { |
| 295 | smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list); |
| 296 | return smp_expr->conv->out_type; |
| 297 | } |
| 298 | return expr->fetch->out_type; |
| 299 | } |
| 300 | |
| 301 | |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 302 | /* fill the trash with a comma-delimited list of source names for the <use> bit |
| 303 | * field which must be composed of a non-null set of SMP_USE_* flags. The return |
| 304 | * value is the pointer to the string in the trash buffer. |
| 305 | */ |
| 306 | const char *sample_src_names(unsigned int use) |
| 307 | { |
| 308 | int bit; |
| 309 | |
| 310 | trash.len = 0; |
| 311 | trash.str[0] = '\0'; |
| 312 | for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) { |
| 313 | if (!(use & ~((1 << bit) - 1))) |
| 314 | break; /* no more bits */ |
| 315 | |
| 316 | if (!(use & (1 << bit))) |
| 317 | continue; /* bit not set */ |
| 318 | |
| 319 | trash.len += snprintf(trash.str + trash.len, trash.size - trash.len, "%s%s", |
| 320 | (use & ((1 << bit) - 1)) ? "," : "", |
| 321 | fetch_src_names[bit]); |
| 322 | } |
| 323 | return trash.str; |
| 324 | } |
| 325 | |
Willy Tarreau | bf8e251 | 2013-03-25 14:52:41 +0100 | [diff] [blame] | 326 | /* return a pointer to the correct sample checkpoint name, or "unknown" when |
| 327 | * the flags are invalid. Only the lowest bit is used, higher bits are ignored |
| 328 | * if set. |
| 329 | */ |
| 330 | const char *sample_ckp_names(unsigned int use) |
| 331 | { |
| 332 | int bit; |
| 333 | |
| 334 | for (bit = 0; bit < SMP_CKP_ENTRIES; bit++) |
| 335 | if (use & (1 << bit)) |
| 336 | return fetch_ckp_names[bit]; |
| 337 | return "unknown sample check place, please report this bug"; |
| 338 | } |
| 339 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 340 | /* |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 341 | * Registers the sample fetch keyword list <kwl> as a list of valid keywords |
| 342 | * for next parsing sessions. The fetch keywords capabilities are also computed |
| 343 | * from their ->use field. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 344 | */ |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 345 | void sample_register_fetches(struct sample_fetch_kw_list *kwl) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 346 | { |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 347 | struct sample_fetch *sf; |
| 348 | int bit; |
| 349 | |
| 350 | for (sf = kwl->kw; sf->kw != NULL; sf++) { |
| 351 | for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) |
| 352 | if (sf->use & (1 << bit)) |
| 353 | sf->val |= fetch_cap[bit]; |
| 354 | } |
| 355 | LIST_ADDQ(&sample_fetches.list, &kwl->list); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 359 | * 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] | 360 | * parsing sessions. |
| 361 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 362 | void sample_register_convs(struct sample_conv_kw_list *pckl) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 363 | { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 364 | LIST_ADDQ(&sample_convs.list, &pckl->list); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 368 | * Returns the pointer on sample fetch keyword structure identified by |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 369 | * string of <len> in buffer <kw>. |
| 370 | * |
| 371 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 372 | struct sample_fetch *find_sample_fetch(const char *kw, int len) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 373 | { |
| 374 | int index; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 375 | struct sample_fetch_kw_list *kwl; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 376 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 377 | list_for_each_entry(kwl, &sample_fetches.list, list) { |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 378 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 379 | if (strncmp(kwl->kw[index].kw, kw, len) == 0 && |
| 380 | kwl->kw[index].kw[len] == '\0') |
| 381 | return &kwl->kw[index]; |
| 382 | } |
| 383 | } |
| 384 | return NULL; |
| 385 | } |
| 386 | |
| 387 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 388 | * Returns the pointer on sample format conversion keyword structure identified by |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 389 | * string of <len> in buffer <kw>. |
| 390 | * |
| 391 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 392 | struct sample_conv *find_sample_conv(const char *kw, int len) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 393 | { |
| 394 | int index; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 395 | struct sample_conv_kw_list *kwl; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 396 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 397 | list_for_each_entry(kwl, &sample_convs.list, list) { |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 398 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 399 | if (strncmp(kwl->kw[index].kw, kw, len) == 0 && |
| 400 | kwl->kw[index].kw[len] == '\0') |
| 401 | return &kwl->kw[index]; |
| 402 | } |
| 403 | } |
| 404 | return NULL; |
| 405 | } |
| 406 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 407 | /******************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 408 | /* Sample casts functions */ |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 409 | /* Note: these functions do *NOT* set the output type on the */ |
| 410 | /* sample, the caller is responsible for doing this on return. */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 411 | /******************************************************************/ |
| 412 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 413 | static int c_ip2int(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 414 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 415 | smp->data.uint = ntohl(smp->data.ipv4.s_addr); |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 416 | smp->type = SMP_T_UINT; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 417 | return 1; |
| 418 | } |
| 419 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 420 | static int c_ip2str(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 421 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 422 | struct chunk *trash = get_trash_chunk(); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 423 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 424 | if (!inet_ntop(AF_INET, (void *)&smp->data.ipv4, trash->str, trash->size)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 425 | return 0; |
| 426 | |
| 427 | trash->len = strlen(trash->str); |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 428 | smp->data.str = *trash; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 429 | smp->type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 430 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 431 | |
| 432 | return 1; |
| 433 | } |
| 434 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 435 | static int c_ip2ipv6(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 436 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 437 | v4tov6(&smp->data.ipv6, &smp->data.ipv4); |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 438 | smp->type = SMP_T_IPV6; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 439 | return 1; |
| 440 | } |
| 441 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 442 | static int c_ipv62str(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 443 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 444 | struct chunk *trash = get_trash_chunk(); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 445 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 446 | if (!inet_ntop(AF_INET6, (void *)&smp->data.ipv6, trash->str, trash->size)) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 447 | return 0; |
| 448 | |
| 449 | trash->len = strlen(trash->str); |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 450 | smp->data.str = *trash; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 451 | smp->type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 452 | smp->flags &= ~SMP_F_CONST; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 453 | return 1; |
| 454 | } |
| 455 | |
| 456 | /* |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 457 | static int c_ipv62ip(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 458 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 459 | return v6tov4(&smp->data.ipv4, &smp->data.ipv6); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 460 | } |
| 461 | */ |
| 462 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 463 | static int c_int2ip(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 464 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 465 | smp->data.ipv4.s_addr = htonl(smp->data.uint); |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 466 | smp->type = SMP_T_IPV4; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 467 | return 1; |
| 468 | } |
| 469 | |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 470 | static int c_str2addr(struct sample *smp) |
| 471 | { |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 472 | if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4)) { |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 473 | if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6)) |
| 474 | return 0; |
| 475 | smp->type = SMP_T_IPV6; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 476 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 477 | return 1; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 478 | } |
| 479 | smp->type = SMP_T_IPV4; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 480 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 481 | return 1; |
| 482 | } |
| 483 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 484 | static int c_str2ip(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 485 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 486 | if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 487 | return 0; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 488 | smp->type = SMP_T_IPV4; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 489 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 490 | return 1; |
| 491 | } |
| 492 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 493 | static int c_str2ipv6(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 494 | { |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 495 | if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6)) |
| 496 | return 0; |
| 497 | smp->type = SMP_T_IPV6; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 498 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 499 | return 1; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 500 | } |
| 501 | |
Emeric Brun | 4b9e802 | 2014-11-03 18:17:10 +0100 | [diff] [blame] | 502 | /* |
| 503 | * The NULL char always enforces the end of string if it is met. |
| 504 | * Data is never changed, so we can ignore the CONST case |
Thierry FOURNIER | e87cac1 | 2014-03-12 15:07:59 +0100 | [diff] [blame] | 505 | */ |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 506 | static int c_bin2str(struct sample *smp) |
| 507 | { |
Emeric Brun | 4b9e802 | 2014-11-03 18:17:10 +0100 | [diff] [blame] | 508 | int i; |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 509 | |
Emeric Brun | 4b9e802 | 2014-11-03 18:17:10 +0100 | [diff] [blame] | 510 | for (i = 0; i < smp->data.str.len; i++) { |
| 511 | if (!smp->data.str.str[i]) { |
| 512 | smp->data.str.len = i; |
Thierry FOURNIER | e87cac1 | 2014-03-12 15:07:59 +0100 | [diff] [blame] | 513 | break; |
Emeric Brun | 4b9e802 | 2014-11-03 18:17:10 +0100 | [diff] [blame] | 514 | } |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 515 | } |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 516 | return 1; |
| 517 | } |
| 518 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 519 | static int c_int2str(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 520 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 521 | struct chunk *trash = get_trash_chunk(); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 522 | char *pos; |
| 523 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 524 | pos = ultoa_r(smp->data.uint, trash->str, trash->size); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 525 | |
| 526 | if (!pos) |
| 527 | return 0; |
| 528 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 529 | trash->size = trash->size - (pos - trash->str); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 530 | trash->str = pos; |
| 531 | trash->len = strlen(pos); |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 532 | smp->data.str = *trash; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 533 | smp->type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 534 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 535 | return 1; |
| 536 | } |
| 537 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 538 | /* This function duplicates data and removes the flag "const". */ |
| 539 | int smp_dup(struct sample *smp) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 540 | { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 541 | struct chunk *trash; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 542 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 543 | /* If the const flag is not set, we don't need to duplicate the |
| 544 | * pattern as it can be modified in place. |
| 545 | */ |
| 546 | if (!(smp->flags & SMP_F_CONST)) |
| 547 | return 1; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 548 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 549 | switch (smp->type) { |
| 550 | case SMP_T_BOOL: |
| 551 | case SMP_T_UINT: |
| 552 | case SMP_T_SINT: |
| 553 | case SMP_T_ADDR: |
| 554 | case SMP_T_IPV4: |
| 555 | case SMP_T_IPV6: |
| 556 | /* These type are not const. */ |
| 557 | break; |
| 558 | case SMP_T_STR: |
| 559 | case SMP_T_BIN: |
| 560 | /* Duplicate data. */ |
| 561 | trash = get_trash_chunk(); |
| 562 | trash->len = smp->data.str.len < trash->size ? smp->data.str.len : trash->size; |
| 563 | memcpy(trash->str, smp->data.str.str, trash->len); |
| 564 | smp->data.str = *trash; |
| 565 | break; |
| 566 | default: |
| 567 | /* Other cases are unexpected. */ |
| 568 | return 0; |
| 569 | } |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 570 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 571 | /* remove const flag */ |
| 572 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 573 | return 1; |
| 574 | } |
| 575 | |
Thierry FOURNIER | 0e9af55 | 2013-12-14 14:55:04 +0100 | [diff] [blame] | 576 | int c_none(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 577 | { |
| 578 | return 1; |
| 579 | } |
| 580 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 581 | static int c_str2int(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 582 | { |
| 583 | int i; |
| 584 | uint32_t ret = 0; |
| 585 | |
Thierry FOURNIER | 60bb020 | 2014-01-27 18:20:48 +0100 | [diff] [blame] | 586 | if (smp->data.str.len == 0) |
| 587 | return 0; |
| 588 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 589 | for (i = 0; i < smp->data.str.len; i++) { |
| 590 | uint32_t val = smp->data.str.str[i] - '0'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 591 | |
Thierry FOURNIER | 60bb020 | 2014-01-27 18:20:48 +0100 | [diff] [blame] | 592 | if (val > 9) { |
| 593 | if (i == 0) |
| 594 | return 0; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 595 | break; |
Thierry FOURNIER | 60bb020 | 2014-01-27 18:20:48 +0100 | [diff] [blame] | 596 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 597 | |
| 598 | ret = ret * 10 + val; |
| 599 | } |
| 600 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 601 | smp->data.uint = ret; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 602 | smp->type = SMP_T_UINT; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 603 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 604 | return 1; |
| 605 | } |
| 606 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 607 | static int c_str2meth(struct sample *smp) |
| 608 | { |
| 609 | enum http_meth_t meth; |
| 610 | int len; |
| 611 | |
| 612 | meth = find_http_meth(smp->data.str.str, smp->data.str.len); |
| 613 | if (meth == HTTP_METH_OTHER) { |
| 614 | len = smp->data.str.len; |
| 615 | smp->data.meth.str.str = smp->data.str.str; |
| 616 | smp->data.meth.str.len = len; |
| 617 | } |
| 618 | else |
| 619 | smp->flags &= ~SMP_F_CONST; |
| 620 | smp->data.meth.meth = meth; |
| 621 | smp->type = SMP_T_METH; |
| 622 | return 1; |
| 623 | } |
| 624 | |
| 625 | static int c_meth2str(struct sample *smp) |
| 626 | { |
| 627 | int len; |
| 628 | enum http_meth_t meth; |
| 629 | |
| 630 | if (smp->data.meth.meth == HTTP_METH_OTHER) { |
| 631 | /* The method is unknown. Copy the original pointer. */ |
| 632 | len = smp->data.meth.str.len; |
| 633 | smp->data.str.str = smp->data.meth.str.str; |
| 634 | smp->data.str.len = len; |
| 635 | smp->type = SMP_T_STR; |
| 636 | } |
| 637 | else if (smp->data.meth.meth < HTTP_METH_OTHER) { |
| 638 | /* The method is known, copy the pointer containing the string. */ |
| 639 | meth = smp->data.meth.meth; |
| 640 | smp->data.str.str = http_known_methods[meth].name; |
| 641 | smp->data.str.len = http_known_methods[meth].len; |
| 642 | smp->flags |= SMP_F_CONST; |
| 643 | smp->type = SMP_T_STR; |
| 644 | } |
| 645 | else { |
| 646 | /* Unknown method */ |
| 647 | return 0; |
| 648 | } |
| 649 | return 1; |
| 650 | } |
| 651 | |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 652 | static int c_addr2bin(struct sample *smp) |
| 653 | { |
| 654 | struct chunk *chk = get_trash_chunk(); |
| 655 | |
| 656 | if (smp->type == SMP_T_IPV4) { |
| 657 | chk->len = 4; |
| 658 | memcpy(chk->str, &smp->data.ipv4, chk->len); |
| 659 | } |
| 660 | else if (smp->type == SMP_T_IPV6) { |
| 661 | chk->len = 16; |
| 662 | memcpy(chk->str, &smp->data.ipv6, chk->len); |
| 663 | } |
| 664 | else |
| 665 | return 0; |
| 666 | |
| 667 | smp->data.str = *chk; |
| 668 | smp->type = SMP_T_BIN; |
| 669 | return 1; |
| 670 | } |
| 671 | |
Willy Tarreau | bbfd1a2 | 2014-07-15 21:19:08 +0200 | [diff] [blame] | 672 | static int c_int2bin(struct sample *smp) |
| 673 | { |
| 674 | struct chunk *chk = get_trash_chunk(); |
| 675 | |
| 676 | *(unsigned int *)chk->str = htonl(smp->data.uint); |
| 677 | chk->len = 4; |
| 678 | |
| 679 | smp->data.str = *chk; |
| 680 | smp->type = SMP_T_BIN; |
| 681 | return 1; |
| 682 | } |
| 683 | |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 684 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 685 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 686 | /* Sample casts matrix: */ |
| 687 | /* sample_casts[from type][to type] */ |
| 688 | /* NULL pointer used for impossible sample casts */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 689 | /*****************************************************************/ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 690 | |
Thierry FOURNIER | 8af6ff1 | 2013-11-21 10:53:12 +0100 | [diff] [blame] | 691 | sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = { |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 692 | /* to: BOOL UINT SINT ADDR IPV4 IPV6 STR BIN METH */ |
| 693 | /* from: BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, }, |
Willy Tarreau | bbfd1a2 | 2014-07-15 21:19:08 +0200 | [diff] [blame] | 694 | /* UINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_int2str, c_int2bin, NULL, }, |
| 695 | /* SINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_int2str, c_int2bin, NULL, }, |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 696 | /* ADDR */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, }, |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 697 | /* IPV4 */ { NULL, c_ip2int, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, }, |
| 698 | /* IPV6 */ { NULL, NULL, NULL, c_none, NULL, c_none, c_ipv62str, c_addr2bin, NULL, }, |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 699 | /* STR */ { c_str2int, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, }, |
| 700 | /* BIN */ { NULL, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, }, |
| 701 | /* METH */ { NULL, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }, |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 702 | }; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 703 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 704 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 705 | * Parse a sample expression configuration: |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 706 | * fetch keyword followed by format conversion keywords. |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 707 | * Returns a pointer on allocated sample expression structure. |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 708 | * The caller must have set al->ctx. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 709 | */ |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 710 | struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err_msg, struct arg_list *al) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 711 | { |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 712 | const char *begw; /* beginning of word */ |
| 713 | const char *endw; /* end of word */ |
| 714 | const char *endt; /* end of term */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 715 | struct sample_expr *expr; |
| 716 | struct sample_fetch *fetch; |
| 717 | struct sample_conv *conv; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 718 | unsigned long prev_type; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 719 | char *fkw = NULL; |
| 720 | char *ckw = NULL; |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 721 | int err_arg; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 722 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 723 | begw = str[*idx]; |
| 724 | for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++); |
| 725 | |
| 726 | if (endw == begw) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 727 | memprintf(err_msg, "missing fetch method"); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 728 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 729 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 730 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 731 | /* keep a copy of the current fetch keyword for error reporting */ |
| 732 | fkw = my_strndup(begw, endw - begw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 733 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 734 | fetch = find_sample_fetch(begw, endw - begw); |
| 735 | if (!fetch) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 736 | memprintf(err_msg, "unknown fetch method '%s'", fkw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 737 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 738 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 739 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 740 | endt = endw; |
| 741 | if (*endt == '(') { |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 742 | /* look for the end of this term and skip the opening parenthesis */ |
| 743 | endt = ++endw; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 744 | while (*endt && *endt != ')') |
| 745 | endt++; |
| 746 | if (*endt != ')') { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 747 | memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 748 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 749 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 750 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 751 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 752 | /* At this point, we have : |
| 753 | * - begw : beginning of the keyword |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 754 | * - endw : end of the keyword, first character not part of keyword |
| 755 | * nor the opening parenthesis (so first character of args |
| 756 | * if present). |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 757 | * - endt : end of the term (=endw or last parenthesis if args are present) |
| 758 | */ |
| 759 | |
| 760 | if (fetch->out_type >= SMP_TYPES) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 761 | memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 762 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 763 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 764 | prev_type = fetch->out_type; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 765 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 766 | expr = calloc(1, sizeof(struct sample_expr)); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 767 | if (!expr) |
| 768 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 769 | |
| 770 | LIST_INIT(&(expr->conv_exprs)); |
| 771 | expr->fetch = fetch; |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 772 | expr->arg_p = empty_arg_list; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 773 | |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 774 | /* Note that we call the argument parser even with an empty string, |
| 775 | * this allows it to automatically create entries for mandatory |
| 776 | * implicit arguments (eg: local proxy name). |
| 777 | */ |
| 778 | al->kw = expr->fetch->kw; |
| 779 | al->conv = NULL; |
| 780 | if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) { |
| 781 | memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg); |
| 782 | goto out_error; |
| 783 | } |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 784 | |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 785 | if (!expr->arg_p) { |
| 786 | expr->arg_p = empty_arg_list; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 787 | } |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 788 | else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) { |
| 789 | 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] | 790 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 791 | } |
| 792 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 793 | /* Now process the converters if any. We have two supported syntaxes |
| 794 | * for the converters, which can be combined : |
| 795 | * - comma-delimited list of converters just after the keyword and args ; |
| 796 | * - one converter per keyword |
| 797 | * The combination allows to have each keyword being a comma-delimited |
| 798 | * series of converters. |
| 799 | * |
| 800 | * We want to process the former first, then the latter. For this we start |
| 801 | * from the beginning of the supposed place in the exiting conv chain, which |
| 802 | * starts at the last comma (endt). |
| 803 | */ |
| 804 | |
| 805 | while (1) { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 806 | struct sample_conv_expr *conv_expr; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 807 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 808 | if (*endt == ')') /* skip last closing parenthesis */ |
| 809 | endt++; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 810 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 811 | if (*endt && *endt != ',') { |
| 812 | if (ckw) |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 813 | memprintf(err_msg, "missing comma after conv keyword '%s'", ckw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 814 | else |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 815 | memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 816 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 817 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 818 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 819 | while (*endt == ',') /* then trailing commas */ |
| 820 | endt++; |
| 821 | |
| 822 | begw = endt; /* start of conv keyword */ |
| 823 | |
| 824 | if (!*begw) { |
| 825 | /* none ? skip to next string */ |
| 826 | (*idx)++; |
| 827 | begw = str[*idx]; |
| 828 | if (!begw || !*begw) |
| 829 | break; |
| 830 | } |
| 831 | |
| 832 | for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++); |
| 833 | |
| 834 | free(ckw); |
| 835 | ckw = my_strndup(begw, endw - begw); |
| 836 | |
| 837 | conv = find_sample_conv(begw, endw - begw); |
| 838 | if (!conv) { |
| 839 | /* we found an isolated keyword that we don't know, it's not ours */ |
| 840 | if (begw == str[*idx]) |
| 841 | break; |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 842 | memprintf(err_msg, "unknown conv method '%s'", ckw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 843 | goto out_error; |
| 844 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 845 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 846 | endt = endw; |
| 847 | if (*endt == '(') { |
| 848 | /* look for the end of this term */ |
| 849 | while (*endt && *endt != ')') |
| 850 | endt++; |
| 851 | if (*endt != ')') { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 852 | memprintf(err_msg, "syntax error: missing ')' after conv keyword '%s'", ckw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 853 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 854 | } |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 858 | memprintf(err_msg, "returns type of conv method '%s' is unknown", ckw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 859 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 860 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 861 | |
| 862 | /* If impossible type conversion */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 863 | if (!sample_casts[prev_type][conv->in_type]) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 864 | memprintf(err_msg, "conv method '%s' cannot be applied", ckw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 865 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 866 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 867 | |
| 868 | prev_type = conv->out_type; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 869 | conv_expr = calloc(1, sizeof(struct sample_conv_expr)); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 870 | if (!conv_expr) |
| 871 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 872 | |
| 873 | LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list)); |
| 874 | conv_expr->conv = conv; |
| 875 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 876 | if (endt != endw) { |
Willy Tarreau | b27c0d3 | 2012-04-20 16:04:47 +0200 | [diff] [blame] | 877 | int err_arg; |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 878 | |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 879 | if (!conv->arg_mask) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 880 | memprintf(err_msg, "conv method '%s' does not support any args", ckw); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 881 | goto out_error; |
| 882 | } |
Willy Tarreau | 9e92d32 | 2010-01-26 17:58:06 +0100 | [diff] [blame] | 883 | |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 884 | al->kw = expr->fetch->kw; |
| 885 | al->conv = conv_expr->conv->kw; |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 886 | if (make_arg_list(endw + 1, endt - endw - 1, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al) < 0) { |
| 887 | memprintf(err_msg, "invalid arg %d in conv method '%s' : %s", err_arg+1, ckw, *err_msg); |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 888 | goto out_error; |
| 889 | } |
| 890 | |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 891 | if (!conv_expr->arg_p) |
| 892 | conv_expr->arg_p = empty_arg_list; |
| 893 | |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 894 | if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 895 | memprintf(err_msg, "invalid args in conv method '%s' : %s", ckw, *err_msg); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 896 | goto out_error; |
| 897 | } |
| 898 | } |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 899 | else if (ARGM(conv->arg_mask)) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 900 | memprintf(err_msg, "missing args for conv method '%s'", ckw); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 901 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 902 | } |
| 903 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 904 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 905 | out: |
| 906 | free(fkw); |
| 907 | free(ckw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 908 | return expr; |
| 909 | |
| 910 | out_error: |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 911 | /* TODO: prune_sample_expr(expr); */ |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 912 | expr = NULL; |
| 913 | goto out; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 917 | * Process a fetch + format conversion of defined by the sample expression <expr> |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 918 | * on request or response considering the <opt> parameter. |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 919 | * Returns a pointer on a typed sample structure containing the result or NULL if |
| 920 | * sample is not found or when format conversion failed. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 921 | * 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] | 922 | * 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] | 923 | * |
| 924 | * Note: the fetch functions are required to properly set the return type. The |
| 925 | * conversion functions must do so too. However the cast functions do not need |
| 926 | * to since they're made to cast mutiple types according to what is required. |
Willy Tarreau | 6bcb0a8 | 2014-07-30 08:56:35 +0200 | [diff] [blame] | 927 | * |
| 928 | * The caller may indicate in <opt> if it considers the result final or not. |
| 929 | * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify |
| 930 | * if the result is stable or not, according to the following table : |
| 931 | * |
| 932 | * return MAY_CHANGE FINAL Meaning for the sample |
| 933 | * NULL 0 * Not present and will never be (eg: header) |
| 934 | * NULL 1 0 Not present yet, could change (eg: POST param) |
| 935 | * NULL 1 1 Not present yet, will not change anymore |
| 936 | * smp 0 * Present and will not change (eg: header) |
| 937 | * smp 1 0 Present, may change (eg: request length) |
| 938 | * smp 1 1 Present, last known value (eg: request length) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 939 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 940 | struct sample *sample_process(struct proxy *px, struct session *l4, void *l7, |
| 941 | unsigned int opt, |
| 942 | struct sample_expr *expr, struct sample *p) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 943 | { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 944 | struct sample_conv_expr *conv_expr; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 945 | |
Willy Tarreau | 18387e2 | 2013-07-25 12:02:38 +0200 | [diff] [blame] | 946 | if (p == NULL) { |
Willy Tarreau | b4a88f0 | 2012-04-23 21:35:11 +0200 | [diff] [blame] | 947 | p = &temp_smp; |
Willy Tarreau | 6c616e0 | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 948 | memset(p, 0, sizeof(*p)); |
Willy Tarreau | 18387e2 | 2013-07-25 12:02:38 +0200 | [diff] [blame] | 949 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 950 | |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 951 | if (!expr->fetch->process(px, l4, l7, opt, expr->arg_p, p, expr->fetch->kw)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 952 | return NULL; |
| 953 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 954 | list_for_each_entry(conv_expr, &expr->conv_exprs, list) { |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 955 | /* we want to ensure that p->type can be casted into |
| 956 | * conv_expr->conv->in_type. We have 3 possibilities : |
| 957 | * - NULL => not castable. |
| 958 | * - c_none => nothing to do (let's optimize it) |
| 959 | * - other => apply cast and prepare to fail |
| 960 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 961 | if (!sample_casts[p->type][conv_expr->conv->in_type]) |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 962 | return NULL; |
| 963 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 964 | if (sample_casts[p->type][conv_expr->conv->in_type] != c_none && |
| 965 | !sample_casts[p->type][conv_expr->conv->in_type](p)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 966 | return NULL; |
| 967 | |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 968 | /* OK cast succeeded */ |
| 969 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 970 | if (!conv_expr->conv->process(conv_expr->arg_p, p)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 971 | return NULL; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 972 | } |
| 973 | return p; |
| 974 | } |
| 975 | |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 976 | /* |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 977 | * Resolve all remaining arguments in proxy <p>. Returns the number of |
| 978 | * errors or 0 if everything is fine. |
| 979 | */ |
| 980 | int smp_resolve_args(struct proxy *p) |
| 981 | { |
| 982 | struct arg_list *cur, *bak; |
| 983 | const char *ctx, *where; |
| 984 | const char *conv_ctx, *conv_pre, *conv_pos; |
| 985 | struct userlist *ul; |
| 986 | struct arg *arg; |
| 987 | int cfgerr = 0; |
| 988 | |
| 989 | list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) { |
| 990 | struct proxy *px; |
| 991 | struct server *srv; |
| 992 | char *pname, *sname; |
| 993 | |
| 994 | arg = cur->arg; |
| 995 | |
| 996 | /* prepare output messages */ |
| 997 | conv_pre = conv_pos = conv_ctx = ""; |
| 998 | if (cur->conv) { |
| 999 | conv_ctx = cur->conv; |
| 1000 | conv_pre = "conversion keyword '"; |
| 1001 | conv_pos = "' for "; |
| 1002 | } |
| 1003 | |
| 1004 | where = "in"; |
| 1005 | ctx = "sample fetch keyword"; |
| 1006 | switch (cur->ctx) { |
| 1007 | case ARGC_STK:where = "in stick rule in"; break; |
| 1008 | case ARGC_TRK: where = "in tracking rule in"; break; |
| 1009 | case ARGC_LOG: where = "in log-format string in"; break; |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 1010 | case ARGC_HRQ: where = "in http-request header format string in"; break; |
| 1011 | case ARGC_HRS: where = "in http-response header format string in"; break; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1012 | case ARGC_UIF: where = "in unique-id-format string in"; break; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 1013 | case ARGC_RDR: where = "in redirect format string in"; break; |
Willy Tarreau | 3a4ac42 | 2014-06-13 16:17:14 +0200 | [diff] [blame] | 1014 | case ARGC_CAP: where = "in capture rule in"; break; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1015 | case ARGC_ACL: ctx = "ACL keyword"; break; |
| 1016 | } |
| 1017 | |
| 1018 | /* set a few default settings */ |
| 1019 | px = p; |
| 1020 | pname = p->id; |
| 1021 | |
| 1022 | switch (arg->type) { |
| 1023 | case ARGT_SRV: |
| 1024 | if (!arg->data.str.len) { |
| 1025 | Alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1026 | cur->file, cur->line, |
| 1027 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1028 | cfgerr++; |
| 1029 | continue; |
| 1030 | } |
| 1031 | |
| 1032 | /* we support two formats : "bck/srv" and "srv" */ |
| 1033 | sname = strrchr(arg->data.str.str, '/'); |
| 1034 | |
| 1035 | if (sname) { |
| 1036 | *sname++ = '\0'; |
| 1037 | pname = arg->data.str.str; |
| 1038 | |
| 1039 | px = findproxy(pname, PR_CAP_BE); |
| 1040 | if (!px) { |
| 1041 | Alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1042 | cur->file, cur->line, pname, |
| 1043 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1044 | cfgerr++; |
| 1045 | break; |
| 1046 | } |
| 1047 | } |
| 1048 | else |
| 1049 | sname = arg->data.str.str; |
| 1050 | |
| 1051 | srv = findserver(px, sname); |
| 1052 | if (!srv) { |
| 1053 | Alert("parsing [%s:%d] : unable to find server '%s' in proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1054 | cur->file, cur->line, sname, pname, |
| 1055 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1056 | cfgerr++; |
| 1057 | break; |
| 1058 | } |
| 1059 | |
| 1060 | free(arg->data.str.str); |
| 1061 | arg->data.str.str = NULL; |
| 1062 | arg->unresolved = 0; |
| 1063 | arg->data.srv = srv; |
| 1064 | break; |
| 1065 | |
| 1066 | case ARGT_FE: |
| 1067 | if (arg->data.str.len) { |
| 1068 | pname = arg->data.str.str; |
| 1069 | px = findproxy(pname, PR_CAP_FE); |
| 1070 | } |
| 1071 | |
| 1072 | if (!px) { |
| 1073 | Alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1074 | cur->file, cur->line, pname, |
| 1075 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1076 | cfgerr++; |
| 1077 | break; |
| 1078 | } |
| 1079 | |
| 1080 | if (!(px->cap & PR_CAP_FE)) { |
| 1081 | Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n", |
| 1082 | cur->file, cur->line, pname, |
| 1083 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1084 | cfgerr++; |
| 1085 | break; |
| 1086 | } |
| 1087 | |
| 1088 | free(arg->data.str.str); |
| 1089 | arg->data.str.str = NULL; |
| 1090 | arg->unresolved = 0; |
| 1091 | arg->data.prx = px; |
| 1092 | break; |
| 1093 | |
| 1094 | case ARGT_BE: |
| 1095 | if (arg->data.str.len) { |
| 1096 | pname = arg->data.str.str; |
| 1097 | px = findproxy(pname, PR_CAP_BE); |
| 1098 | } |
| 1099 | |
| 1100 | if (!px) { |
| 1101 | Alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1102 | cur->file, cur->line, pname, |
| 1103 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1104 | cfgerr++; |
| 1105 | break; |
| 1106 | } |
| 1107 | |
| 1108 | if (!(px->cap & PR_CAP_BE)) { |
| 1109 | Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n", |
| 1110 | cur->file, cur->line, pname, |
| 1111 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1112 | cfgerr++; |
| 1113 | break; |
| 1114 | } |
| 1115 | |
| 1116 | free(arg->data.str.str); |
| 1117 | arg->data.str.str = NULL; |
| 1118 | arg->unresolved = 0; |
| 1119 | arg->data.prx = px; |
| 1120 | break; |
| 1121 | |
| 1122 | case ARGT_TAB: |
| 1123 | if (arg->data.str.len) { |
| 1124 | pname = arg->data.str.str; |
| 1125 | px = find_stktable(pname); |
| 1126 | } |
| 1127 | |
| 1128 | if (!px) { |
| 1129 | Alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1130 | cur->file, cur->line, pname, |
| 1131 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1132 | cfgerr++; |
| 1133 | break; |
| 1134 | } |
| 1135 | |
| 1136 | if (!px->table.size) { |
| 1137 | Alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1138 | cur->file, cur->line, pname, |
| 1139 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1140 | cfgerr++; |
| 1141 | break; |
| 1142 | } |
| 1143 | |
| 1144 | free(arg->data.str.str); |
| 1145 | arg->data.str.str = NULL; |
| 1146 | arg->unresolved = 0; |
| 1147 | arg->data.prx = px; |
| 1148 | break; |
| 1149 | |
| 1150 | case ARGT_USR: |
| 1151 | if (!arg->data.str.len) { |
| 1152 | Alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1153 | cur->file, cur->line, |
| 1154 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1155 | cfgerr++; |
| 1156 | break; |
| 1157 | } |
| 1158 | |
| 1159 | if (p->uri_auth && p->uri_auth->userlist && |
| 1160 | !strcmp(p->uri_auth->userlist->name, arg->data.str.str)) |
| 1161 | ul = p->uri_auth->userlist; |
| 1162 | else |
| 1163 | ul = auth_find_userlist(arg->data.str.str); |
| 1164 | |
| 1165 | if (!ul) { |
| 1166 | Alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1167 | cur->file, cur->line, arg->data.str.str, |
| 1168 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1169 | cfgerr++; |
| 1170 | break; |
| 1171 | } |
| 1172 | |
| 1173 | free(arg->data.str.str); |
| 1174 | arg->data.str.str = NULL; |
| 1175 | arg->unresolved = 0; |
| 1176 | arg->data.usr = ul; |
| 1177 | break; |
| 1178 | } |
| 1179 | |
| 1180 | LIST_DEL(&cur->list); |
| 1181 | free(cur); |
| 1182 | } /* end of args processing */ |
| 1183 | |
| 1184 | return cfgerr; |
| 1185 | } |
| 1186 | |
| 1187 | /* |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1188 | * Process a fetch + format conversion as defined by the sample expression |
| 1189 | * <expr> on request or response considering the <opt> parameter. The output is |
| 1190 | * always of type string. If a stable sample can be fetched, or an unstable one |
| 1191 | * when <opt> contains SMP_OPT_FINAL, the sample is converted to a string and |
| 1192 | * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found |
| 1193 | * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is |
| 1194 | * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to |
| 1195 | * 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] | 1196 | * be converted, NULL is returned. The caller MUST NOT use the sample if the |
| 1197 | * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is |
| 1198 | * still hope to get it after waiting longer, and is not converted to string. |
| 1199 | * The possible output combinations are the following : |
| 1200 | * |
| 1201 | * return MAY_CHANGE FINAL Meaning for the sample |
| 1202 | * NULL * * Not present and will never be (eg: header) |
| 1203 | * smp 0 * Final value converted (eg: header) |
| 1204 | * smp 1 0 Not present yet, may appear later (eg: header) |
| 1205 | * smp 1 1 never happens (either flag is cleared on output) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1206 | */ |
| 1207 | struct sample *sample_fetch_string(struct proxy *px, struct session *l4, void *l7, |
| 1208 | unsigned int opt, struct sample_expr *expr) |
| 1209 | { |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1210 | struct sample *smp = &temp_smp; |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1211 | |
Willy Tarreau | 6c616e0 | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 1212 | memset(smp, 0, sizeof(*smp)); |
| 1213 | |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1214 | if (!sample_process(px, l4, l7, opt, expr, smp)) { |
| 1215 | if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL)) |
| 1216 | return smp; |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1217 | return NULL; |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1218 | } |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1219 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1220 | if (!sample_casts[smp->type][SMP_T_STR]) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1221 | return NULL; |
| 1222 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1223 | if (!sample_casts[smp->type][SMP_T_STR](smp)) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1224 | return NULL; |
| 1225 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1226 | smp->type = SMP_T_STR; |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1227 | smp->flags &= ~SMP_F_MAY_CHANGE; |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1228 | return smp; |
| 1229 | } |
| 1230 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1231 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1232 | /* Sample format convert functions */ |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 1233 | /* These functions set the data type on return. */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1234 | /*****************************************************************/ |
| 1235 | |
Emeric Brun | 53d1a98 | 2014-04-30 18:21:37 +0200 | [diff] [blame] | 1236 | static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp) |
| 1237 | { |
| 1238 | struct chunk *trash = get_trash_chunk(); |
| 1239 | int b64_len; |
| 1240 | |
| 1241 | trash->len = 0; |
| 1242 | b64_len = a2base64(smp->data.str.str, smp->data.str.len, trash->str, trash->size); |
| 1243 | if (b64_len < 0) |
| 1244 | return 0; |
| 1245 | |
| 1246 | trash->len = b64_len; |
| 1247 | smp->data.str = *trash; |
| 1248 | smp->type = SMP_T_STR; |
| 1249 | smp->flags &= ~SMP_F_CONST; |
| 1250 | return 1; |
| 1251 | } |
| 1252 | |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 1253 | static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp) |
| 1254 | { |
| 1255 | struct chunk *trash = get_trash_chunk(); |
| 1256 | unsigned char c; |
| 1257 | int ptr = 0; |
| 1258 | |
| 1259 | trash->len = 0; |
| 1260 | while (ptr < smp->data.str.len && trash->len <= trash->size - 2) { |
| 1261 | c = smp->data.str.str[ptr++]; |
| 1262 | trash->str[trash->len++] = hextab[(c >> 4) & 0xF]; |
| 1263 | trash->str[trash->len++] = hextab[c & 0xF]; |
| 1264 | } |
| 1265 | smp->data.str = *trash; |
| 1266 | smp->type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1267 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 1268 | return 1; |
| 1269 | } |
| 1270 | |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1271 | /* hashes the binary input into a 32-bit unsigned int */ |
| 1272 | static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp) |
| 1273 | { |
| 1274 | smp->data.uint = hash_djb2(smp->data.str.str, smp->data.str.len); |
| 1275 | if (arg_p && arg_p->data.uint) |
| 1276 | smp->data.uint = full_hash(smp->data.uint); |
| 1277 | smp->type = SMP_T_UINT; |
| 1278 | return 1; |
| 1279 | } |
| 1280 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1281 | static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1282 | { |
| 1283 | int i; |
| 1284 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1285 | if (!smp_dup(smp)) |
| 1286 | return 0; |
| 1287 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 1288 | if (!smp->data.str.size) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1289 | return 0; |
| 1290 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 1291 | for (i = 0; i < smp->data.str.len; i++) { |
| 1292 | if ((smp->data.str.str[i] >= 'A') && (smp->data.str.str[i] <= 'Z')) |
| 1293 | smp->data.str.str[i] += 'a' - 'A'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1294 | } |
| 1295 | return 1; |
| 1296 | } |
| 1297 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1298 | static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1299 | { |
| 1300 | int i; |
| 1301 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1302 | if (!smp_dup(smp)) |
| 1303 | return 0; |
| 1304 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 1305 | if (!smp->data.str.size) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1306 | return 0; |
| 1307 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 1308 | for (i = 0; i < smp->data.str.len; i++) { |
| 1309 | if ((smp->data.str.str[i] >= 'a') && (smp->data.str.str[i] <= 'z')) |
| 1310 | smp->data.str.str[i] += 'A' - 'a'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1311 | } |
| 1312 | return 1; |
| 1313 | } |
| 1314 | |
Willy Tarreau | f995410 | 2012-04-20 14:03:29 +0200 | [diff] [blame] | 1315 | /* takes the netmask in arg_p */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1316 | static int sample_conv_ipmask(const struct arg *arg_p, struct sample *smp) |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 1317 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 1318 | smp->data.ipv4.s_addr &= arg_p->data.ipv4.s_addr; |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 1319 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 1320 | return 1; |
| 1321 | } |
| 1322 | |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1323 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 1324 | * adds an optional offset found in args[1] and emits a string representing |
| 1325 | * the local time in the format specified in args[1] using strftime(). |
| 1326 | */ |
| 1327 | static int sample_conv_ltime(const struct arg *args, struct sample *smp) |
| 1328 | { |
| 1329 | struct chunk *temp; |
| 1330 | time_t curr_date = smp->data.uint; |
| 1331 | |
| 1332 | /* add offset */ |
| 1333 | if (args[1].type == ARGT_SINT || args[1].type == ARGT_UINT) |
| 1334 | curr_date += args[1].data.sint; |
| 1335 | |
| 1336 | temp = get_trash_chunk(); |
| 1337 | temp->len = strftime(temp->str, temp->size, args[0].data.str.str, localtime(&curr_date)); |
| 1338 | smp->data.str = *temp; |
| 1339 | smp->type = SMP_T_STR; |
| 1340 | return 1; |
| 1341 | } |
| 1342 | |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1343 | /* hashes the binary input into a 32-bit unsigned int */ |
| 1344 | static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp) |
| 1345 | { |
| 1346 | smp->data.uint = hash_sdbm(smp->data.str.str, smp->data.str.len); |
| 1347 | if (arg_p && arg_p->data.uint) |
| 1348 | smp->data.uint = full_hash(smp->data.uint); |
| 1349 | smp->type = SMP_T_UINT; |
| 1350 | return 1; |
| 1351 | } |
| 1352 | |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1353 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 1354 | * adds an optional offset found in args[1] and emits a string representing |
| 1355 | * the UTC date in the format specified in args[1] using strftime(). |
| 1356 | */ |
| 1357 | static int sample_conv_utime(const struct arg *args, struct sample *smp) |
| 1358 | { |
| 1359 | struct chunk *temp; |
| 1360 | time_t curr_date = smp->data.uint; |
| 1361 | |
| 1362 | /* add offset */ |
| 1363 | if (args[1].type == ARGT_SINT || args[1].type == ARGT_UINT) |
| 1364 | curr_date += args[1].data.sint; |
| 1365 | |
| 1366 | temp = get_trash_chunk(); |
| 1367 | temp->len = strftime(temp->str, temp->size, args[0].data.str.str, gmtime(&curr_date)); |
| 1368 | smp->data.str = *temp; |
| 1369 | smp->type = SMP_T_STR; |
| 1370 | return 1; |
| 1371 | } |
| 1372 | |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1373 | /* hashes the binary input into a 32-bit unsigned int */ |
| 1374 | static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp) |
| 1375 | { |
| 1376 | smp->data.uint = hash_wt6(smp->data.str.str, smp->data.str.len); |
| 1377 | if (arg_p && arg_p->data.uint) |
| 1378 | smp->data.uint = full_hash(smp->data.uint); |
| 1379 | smp->type = SMP_T_UINT; |
| 1380 | return 1; |
| 1381 | } |
| 1382 | |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1383 | /* This function escape special json characters. The returned string can be |
| 1384 | * safely set between two '"' and used as json string. The json string is |
| 1385 | * defined like this: |
| 1386 | * |
| 1387 | * any Unicode character except '"' or '\' or control character |
| 1388 | * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits |
| 1389 | * |
| 1390 | * The enum input_type contain all the allowed mode for decoding the input |
| 1391 | * string. |
| 1392 | */ |
| 1393 | enum input_type { |
| 1394 | IT_ASCII = 0, |
| 1395 | IT_UTF8, |
| 1396 | IT_UTF8S, |
| 1397 | IT_UTF8P, |
| 1398 | IT_UTF8PS, |
| 1399 | }; |
| 1400 | static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv, |
| 1401 | const char *file, int line, char **err) |
| 1402 | { |
| 1403 | if (!arg) { |
| 1404 | memprintf(err, "Unexpected empty arg list"); |
| 1405 | return 0; |
| 1406 | } |
| 1407 | |
| 1408 | if (arg->type != ARGT_STR) { |
| 1409 | memprintf(err, "Unexpected arg type"); |
| 1410 | return 0; |
| 1411 | } |
| 1412 | |
| 1413 | if (strcmp(arg->data.str.str, "") == 0) { |
| 1414 | arg->type = ARGT_UINT; |
| 1415 | arg->data.uint = IT_ASCII; |
| 1416 | return 1; |
| 1417 | } |
| 1418 | |
| 1419 | else if (strcmp(arg->data.str.str, "ascii") == 0) { |
| 1420 | arg->type = ARGT_UINT; |
| 1421 | arg->data.uint = IT_ASCII; |
| 1422 | return 1; |
| 1423 | } |
| 1424 | |
| 1425 | else if (strcmp(arg->data.str.str, "utf8") == 0) { |
| 1426 | arg->type = ARGT_UINT; |
| 1427 | arg->data.uint = IT_UTF8; |
| 1428 | return 1; |
| 1429 | } |
| 1430 | |
| 1431 | else if (strcmp(arg->data.str.str, "utf8s") == 0) { |
| 1432 | arg->type = ARGT_UINT; |
| 1433 | arg->data.uint = IT_UTF8S; |
| 1434 | return 1; |
| 1435 | } |
| 1436 | |
| 1437 | else if (strcmp(arg->data.str.str, "utf8p") == 0) { |
| 1438 | arg->type = ARGT_UINT; |
| 1439 | arg->data.uint = IT_UTF8P; |
| 1440 | return 1; |
| 1441 | } |
| 1442 | |
| 1443 | else if (strcmp(arg->data.str.str, "utf8ps") == 0) { |
| 1444 | arg->type = ARGT_UINT; |
| 1445 | arg->data.uint = IT_UTF8PS; |
| 1446 | return 1; |
| 1447 | } |
| 1448 | |
| 1449 | memprintf(err, "Unexpected input code type at file '%s', line %d. " |
| 1450 | "Allowed value are 'ascii', 'utf8', 'utf8p' and 'utf8pp'", file, line); |
| 1451 | return 0; |
| 1452 | } |
| 1453 | |
| 1454 | static int sample_conv_json(const struct arg *arg_p, struct sample *smp) |
| 1455 | { |
| 1456 | struct chunk *temp; |
| 1457 | char _str[7]; /* \u + 4 hex digit + null char for sprintf. */ |
| 1458 | const char *str; |
| 1459 | int len; |
| 1460 | enum input_type input_type = IT_ASCII; |
| 1461 | unsigned int c; |
| 1462 | unsigned int ret; |
| 1463 | char *p; |
| 1464 | |
| 1465 | if (arg_p) |
| 1466 | input_type = arg_p->data.uint; |
| 1467 | |
| 1468 | temp = get_trash_chunk(); |
| 1469 | temp->len = 0; |
| 1470 | |
| 1471 | p = smp->data.str.str; |
| 1472 | while (p < smp->data.str.str + smp->data.str.len) { |
| 1473 | |
| 1474 | if (input_type == IT_ASCII) { |
| 1475 | /* Read input as ASCII. */ |
| 1476 | c = *(unsigned char *)p; |
| 1477 | p++; |
| 1478 | } |
| 1479 | else { |
| 1480 | /* Read input as UTF8. */ |
| 1481 | ret = utf8_next(p, smp->data.str.len - ( p - smp->data.str.str ), &c); |
| 1482 | p += utf8_return_length(ret); |
| 1483 | |
| 1484 | if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK) |
| 1485 | return 0; |
| 1486 | if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK) |
| 1487 | continue; |
| 1488 | if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ)) |
| 1489 | return 0; |
| 1490 | if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ)) |
| 1491 | continue; |
| 1492 | |
| 1493 | /* Check too big values. */ |
| 1494 | if ((unsigned int)c > 0xffff) { |
| 1495 | if (input_type == IT_UTF8 || input_type == IT_UTF8P) |
| 1496 | return 0; |
| 1497 | continue; |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | /* Convert character. */ |
| 1502 | if (c == '"') { |
| 1503 | len = 2; |
| 1504 | str = "\\\""; |
| 1505 | } |
| 1506 | else if (c == '\\') { |
| 1507 | len = 2; |
| 1508 | str = "\\\\"; |
| 1509 | } |
| 1510 | else if (c == '/') { |
| 1511 | len = 2; |
| 1512 | str = "\\/"; |
| 1513 | } |
| 1514 | else if (c == '\b') { |
| 1515 | len = 2; |
| 1516 | str = "\\b"; |
| 1517 | } |
| 1518 | else if (c == '\f') { |
| 1519 | len = 2; |
| 1520 | str = "\\f"; |
| 1521 | } |
| 1522 | else if (c == '\r') { |
| 1523 | len = 2; |
| 1524 | str = "\\r"; |
| 1525 | } |
| 1526 | else if (c == '\n') { |
| 1527 | len = 2; |
| 1528 | str = "\\n"; |
| 1529 | } |
| 1530 | else if (c == '\t') { |
| 1531 | len = 2; |
| 1532 | str = "\\t"; |
| 1533 | } |
| 1534 | else if (c > 0xff || !isprint(c)) { |
| 1535 | /* isprint generate a segfault if c is too big. The man says that |
| 1536 | * c must have the value of an unsigned char or EOF. |
| 1537 | */ |
| 1538 | len = 6; |
| 1539 | _str[0] = '\\'; |
| 1540 | _str[1] = 'u'; |
| 1541 | snprintf(&_str[2], 5, "%04x", (unsigned short)c); |
| 1542 | str = _str; |
| 1543 | } |
| 1544 | else { |
| 1545 | len = 1; |
| 1546 | str = (char *)&c; |
| 1547 | } |
| 1548 | |
| 1549 | /* Check length */ |
| 1550 | if (temp->len + len > temp->size) |
| 1551 | return 0; |
| 1552 | |
| 1553 | /* Copy string. */ |
| 1554 | memcpy(temp->str + temp->len, str, len); |
| 1555 | temp->len += len; |
| 1556 | } |
| 1557 | |
| 1558 | smp->flags &= ~SMP_F_CONST; |
| 1559 | smp->data.str = *temp; |
| 1560 | smp->type = SMP_T_STR; |
| 1561 | |
| 1562 | return 1; |
| 1563 | } |
| 1564 | |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 1565 | /* This sample function is designed to extract some bytes from an input buffer. |
| 1566 | * First arg is the offset. |
| 1567 | * Optional second arg is the length to truncate */ |
| 1568 | static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp) |
| 1569 | { |
| 1570 | if (smp->data.str.len <= arg_p[0].data.uint) { |
| 1571 | smp->data.str.len = 0; |
| 1572 | return 1; |
| 1573 | } |
| 1574 | |
| 1575 | if (smp->data.str.size) |
| 1576 | smp->data.str.size -= arg_p[0].data.uint; |
| 1577 | smp->data.str.len -= arg_p[0].data.uint; |
| 1578 | smp->data.str.str += arg_p[0].data.uint; |
| 1579 | |
| 1580 | if ((arg_p[1].type == ARGT_UINT) && (arg_p[1].data.uint < smp->data.str.len)) |
| 1581 | smp->data.str.len = arg_p[1].data.uint; |
| 1582 | |
| 1583 | return 1; |
| 1584 | } |
| 1585 | |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 1586 | static int sample_conv_field_check(struct arg *args, struct sample_conv *conv, |
| 1587 | const char *file, int line, char **err) |
| 1588 | { |
| 1589 | struct arg *arg = args; |
| 1590 | |
| 1591 | if (!arg) { |
| 1592 | memprintf(err, "Unexpected empty arg list"); |
| 1593 | return 0; |
| 1594 | } |
| 1595 | |
| 1596 | if (arg->type != ARGT_UINT) { |
| 1597 | memprintf(err, "Unexpected arg type"); |
| 1598 | return 0; |
| 1599 | } |
| 1600 | |
| 1601 | if (!arg->data.uint) { |
| 1602 | memprintf(err, "Unexpected value 0 for index"); |
| 1603 | return 0; |
| 1604 | } |
| 1605 | |
| 1606 | arg++; |
| 1607 | |
| 1608 | if (arg->type != ARGT_STR) { |
| 1609 | memprintf(err, "Unexpected arg type"); |
| 1610 | return 0; |
| 1611 | } |
| 1612 | |
| 1613 | if (!arg->data.str.len) { |
| 1614 | memprintf(err, "Empty separators list"); |
| 1615 | return 0; |
| 1616 | } |
| 1617 | |
| 1618 | return 1; |
| 1619 | } |
| 1620 | |
| 1621 | /* This sample function is designed to a return selected part of a string (field). |
| 1622 | * First arg is the index of the field (start at 1) |
| 1623 | * Second arg is a char list of separators (type string) |
| 1624 | */ |
| 1625 | static int sample_conv_field(const struct arg *arg_p, struct sample *smp) |
| 1626 | { |
| 1627 | unsigned int field; |
| 1628 | char *start, *end; |
| 1629 | int i; |
| 1630 | |
| 1631 | if (!arg_p[0].data.uint) |
| 1632 | return 0; |
| 1633 | |
| 1634 | field = 1; |
| 1635 | end = start = smp->data.str.str; |
| 1636 | while (end - smp->data.str.str < smp->data.str.len) { |
| 1637 | |
| 1638 | for (i = 0 ; i < arg_p[1].data.str.len ; i++) { |
| 1639 | if (*end == arg_p[1].data.str.str[i]) { |
| 1640 | if (field == arg_p[0].data.uint) |
| 1641 | goto found; |
| 1642 | start = end+1; |
| 1643 | field++; |
| 1644 | break; |
| 1645 | } |
| 1646 | } |
| 1647 | end++; |
| 1648 | } |
| 1649 | |
| 1650 | /* Field not found */ |
| 1651 | if (field != arg_p[0].data.uint) { |
| 1652 | smp->data.str.len = 0; |
| 1653 | return 1; |
| 1654 | } |
| 1655 | found: |
| 1656 | smp->data.str.len = end - start; |
| 1657 | /* If ret string is len 0, no need to |
| 1658 | change pointers or to update size */ |
| 1659 | if (!smp->data.str.len) |
| 1660 | return 1; |
| 1661 | |
| 1662 | smp->data.str.str = start; |
| 1663 | |
| 1664 | /* Compute remaining size if needed |
| 1665 | Note: smp->data.str.size cannot be set to 0 */ |
| 1666 | if (smp->data.str.size) |
| 1667 | smp->data.str.size -= start - smp->data.str.str; |
| 1668 | |
| 1669 | return 1; |
| 1670 | } |
| 1671 | |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 1672 | /* This sample function is designed to return a word from a string. |
| 1673 | * First arg is the index of the word (start at 1) |
| 1674 | * Second arg is a char list of words separators (type string) |
| 1675 | */ |
| 1676 | static int sample_conv_word(const struct arg *arg_p, struct sample *smp) |
| 1677 | { |
| 1678 | unsigned int word; |
| 1679 | char *start, *end; |
| 1680 | int i, issep, inword; |
| 1681 | |
| 1682 | if (!arg_p[0].data.uint) |
| 1683 | return 0; |
| 1684 | |
| 1685 | word = 0; |
| 1686 | inword = 0; |
| 1687 | end = start = smp->data.str.str; |
| 1688 | while (end - smp->data.str.str < smp->data.str.len) { |
| 1689 | issep = 0; |
| 1690 | for (i = 0 ; i < arg_p[1].data.str.len ; i++) { |
| 1691 | if (*end == arg_p[1].data.str.str[i]) { |
| 1692 | issep = 1; |
| 1693 | break; |
| 1694 | } |
| 1695 | } |
| 1696 | if (!inword) { |
| 1697 | if (!issep) { |
| 1698 | word++; |
| 1699 | start = end; |
| 1700 | inword = 1; |
| 1701 | } |
| 1702 | } |
| 1703 | else if (issep) { |
| 1704 | if (word == arg_p[0].data.uint) |
| 1705 | goto found; |
| 1706 | inword = 0; |
| 1707 | } |
| 1708 | end++; |
| 1709 | } |
| 1710 | |
| 1711 | /* Field not found */ |
| 1712 | if (word != arg_p[0].data.uint) { |
| 1713 | smp->data.str.len = 0; |
| 1714 | return 1; |
| 1715 | } |
| 1716 | found: |
| 1717 | smp->data.str.len = end - start; |
| 1718 | /* If ret string is len 0, no need to |
| 1719 | change pointers or to update size */ |
| 1720 | if (!smp->data.str.len) |
| 1721 | return 1; |
| 1722 | |
| 1723 | smp->data.str.str = start; |
| 1724 | |
| 1725 | /* Compute remaining size if needed |
| 1726 | Note: smp->data.str.size cannot be set to 0 */ |
| 1727 | if (smp->data.str.size) |
| 1728 | smp->data.str.size -= start - smp->data.str.str; |
| 1729 | |
| 1730 | return 1; |
| 1731 | } |
| 1732 | |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 1733 | /************************************************************************/ |
| 1734 | /* All supported sample fetch functions must be declared here */ |
| 1735 | /************************************************************************/ |
| 1736 | |
| 1737 | /* force TRUE to be returned at the fetch level */ |
| 1738 | static int |
| 1739 | smp_fetch_true(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1740 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 1741 | { |
| 1742 | smp->type = SMP_T_BOOL; |
| 1743 | smp->data.uint = 1; |
| 1744 | return 1; |
| 1745 | } |
| 1746 | |
| 1747 | /* force FALSE to be returned at the fetch level */ |
| 1748 | static int |
| 1749 | smp_fetch_false(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1750 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 1751 | { |
| 1752 | smp->type = SMP_T_BOOL; |
| 1753 | smp->data.uint = 0; |
| 1754 | return 1; |
| 1755 | } |
| 1756 | |
| 1757 | /* retrieve environment variable $1 as a string */ |
| 1758 | static int |
| 1759 | smp_fetch_env(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1760 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 1761 | { |
| 1762 | char *env; |
| 1763 | |
| 1764 | if (!args || args[0].type != ARGT_STR) |
| 1765 | return 0; |
| 1766 | |
| 1767 | env = getenv(args[0].data.str.str); |
| 1768 | if (!env) |
| 1769 | return 0; |
| 1770 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1771 | smp->type = SMP_T_STR; |
| 1772 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 1773 | smp->data.str.str = env; |
| 1774 | smp->data.str.len = strlen(env); |
| 1775 | return 1; |
| 1776 | } |
| 1777 | |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 1778 | /* retrieve the current local date in epoch time, and applies an optional offset |
| 1779 | * of args[0] seconds. |
| 1780 | */ |
| 1781 | static int |
| 1782 | smp_fetch_date(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1783 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 1784 | { |
| 1785 | smp->data.uint = date.tv_sec; |
| 1786 | |
| 1787 | /* add offset */ |
| 1788 | if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT)) |
| 1789 | smp->data.uint += args[0].data.sint; |
| 1790 | |
| 1791 | smp->type = SMP_T_UINT; |
| 1792 | smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE; |
| 1793 | return 1; |
| 1794 | } |
| 1795 | |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 1796 | /* returns the number of processes */ |
| 1797 | static int |
| 1798 | smp_fetch_nbproc(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
| 1799 | const struct arg *args, struct sample *smp, const char *kw) |
| 1800 | { |
| 1801 | smp->type = SMP_T_UINT; |
| 1802 | smp->data.uint = global.nbproc; |
| 1803 | return 1; |
| 1804 | } |
| 1805 | |
| 1806 | /* returns the number of the current process (between 1 and nbproc */ |
| 1807 | static int |
| 1808 | smp_fetch_proc(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
| 1809 | const struct arg *args, struct sample *smp, const char *kw) |
| 1810 | { |
| 1811 | smp->type = SMP_T_UINT; |
| 1812 | smp->data.uint = relative_pid; |
| 1813 | return 1; |
| 1814 | } |
| 1815 | |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 1816 | /* generate a random 32-bit integer for whatever purpose, with an optional |
| 1817 | * range specified in argument. |
| 1818 | */ |
| 1819 | static int |
| 1820 | smp_fetch_rand(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
| 1821 | const struct arg *args, struct sample *smp, const char *kw) |
| 1822 | { |
| 1823 | smp->data.uint = random(); |
| 1824 | |
| 1825 | /* reduce if needed. Don't do a modulo, use all bits! */ |
| 1826 | if (args && args[0].type == ARGT_UINT) |
Vincent Bernat | 1228dc0 | 2014-12-10 10:31:37 +0100 | [diff] [blame] | 1827 | smp->data.uint = ((uint64_t)smp->data.uint * args[0].data.uint) / ((u64)RAND_MAX+1); |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 1828 | |
| 1829 | smp->type = SMP_T_UINT; |
| 1830 | smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE; |
| 1831 | return 1; |
| 1832 | } |
| 1833 | |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 1834 | /* returns true if the current process is stopping */ |
| 1835 | static int |
| 1836 | smp_fetch_stopping(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
| 1837 | const struct arg *args, struct sample *smp, const char *kw) |
| 1838 | { |
| 1839 | smp->type = SMP_T_BOOL; |
| 1840 | smp->data.uint = stopping; |
| 1841 | return 1; |
| 1842 | } |
| 1843 | |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 1844 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1845 | * Note: fetches that may return multiple types must be declared as the lowest |
| 1846 | * common denominator, the type that can be casted into all other ones. For |
| 1847 | * instance IPv4/IPv6 must be declared IPv4. |
| 1848 | */ |
| 1849 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
| 1850 | { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN }, |
| 1851 | { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1852 | { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN }, |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 1853 | { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_USE_INTRN }, |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 1854 | { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_UINT, SMP_USE_INTRN }, |
| 1855 | { "proc", smp_fetch_proc, 0, NULL, SMP_T_UINT, SMP_USE_INTRN }, |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 1856 | { "rand", smp_fetch_rand, ARG1(0,UINT), NULL, SMP_T_UINT, SMP_USE_INTRN }, |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 1857 | { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN }, |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 1858 | { /* END */ }, |
| 1859 | }}; |
| 1860 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1861 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 1862 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Emeric Brun | 53d1a98 | 2014-04-30 18:21:37 +0200 | [diff] [blame] | 1863 | { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR }, |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1864 | { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR }, |
| 1865 | { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR }, |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 1866 | { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR }, |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1867 | { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 }, |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1868 | { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR }, |
| 1869 | { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR }, |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1870 | { "djb2", sample_conv_djb2, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT }, |
| 1871 | { "sdbm", sample_conv_sdbm, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT }, |
| 1872 | { "wt6", sample_conv_wt6, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT }, |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1873 | { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR }, |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 1874 | { "bytes", sample_conv_bytes, ARG2(1,UINT,UINT), NULL, SMP_T_BIN, SMP_T_BIN }, |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 1875 | { "field", sample_conv_field, ARG2(2,UINT,STR), sample_conv_field_check, SMP_T_STR, SMP_T_STR }, |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 1876 | { "word", sample_conv_word, ARG2(2,UINT,STR), sample_conv_field_check, SMP_T_STR, SMP_T_STR }, |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 1877 | { NULL, NULL, 0, 0, 0 }, |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1878 | }}; |
| 1879 | |
| 1880 | __attribute__((constructor)) |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1881 | static void __sample_init(void) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1882 | { |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 1883 | /* register sample fetch and format conversion keywords */ |
| 1884 | sample_register_fetches(&smp_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1885 | sample_register_convs(&sample_conv_kws); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1886 | } |