blob: 7337ba06a9b2efaf432f970e293ef32d8c9cf970 [file] [log] [blame]
Emeric Brun107ca302010-01-04 16:16:05 +01001/*
Willy Tarreaucd3b0942012-04-27 21:52:18 +02002 * Sample management functions.
Emeric Brun107ca302010-01-04 16:16:05 +01003 *
4 * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreaucd3b0942012-04-27 21:52:18 +02005 * Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
Emeric Brun107ca302010-01-04 16:16:05 +01006 *
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 FOURNIER317e1c42014-08-12 10:20:47 +020014#include <ctype.h>
Emeric Brun107ca302010-01-04 16:16:05 +010015#include <string.h>
16#include <arpa/inet.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020017#include <stdio.h>
Emeric Brun107ca302010-01-04 16:16:05 +010018
Willy Tarreaub2551052020-06-09 09:07:15 +020019#include <import/sha1.h>
20#include <import/xxhash.h>
Willy Tarreau7e2c6472012-10-29 20:44:36 +010021
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/api.h>
23#include <haproxy/arg.h>
Willy Tarreauac13aea2020-06-04 10:36:03 +020024#include <haproxy/auth.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020025#include <haproxy/base64.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020026#include <haproxy/buf.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020027#include <haproxy/chunk.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020028#include <haproxy/errors.h>
Baptiste Assmanne138dda2020-10-22 15:39:03 +020029#include <haproxy/fix.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020030#include <haproxy/global.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020031#include <haproxy/hash.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020032#include <haproxy/http.h>
Baptiste Assmanne138dda2020-10-22 15:39:03 +020033#include <haproxy/istbuf.h>
Baptiste Assmanne279ca62020-10-27 18:10:06 +010034#include <haproxy/mqtt.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020035#include <haproxy/net_helper.h>
Willy Tarreaub23e5952020-06-04 16:06:59 +020036#include <haproxy/protobuf.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020037#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020038#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020039#include <haproxy/sample.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020040#include <haproxy/sink.h>
41#include <haproxy/stick_table.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020042#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020043#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020044#include <haproxy/vars.h>
Thierry FOURNIER01e09742016-12-26 11:46:11 +010045
Willy Tarreau1cf8f082014-02-07 12:14:54 +010046/* sample type names */
47const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020048 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010049 [SMP_T_BOOL] = "bool",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010050 [SMP_T_SINT] = "sint",
51 [SMP_T_ADDR] = "addr",
52 [SMP_T_IPV4] = "ipv4",
53 [SMP_T_IPV6] = "ipv6",
54 [SMP_T_STR] = "str",
55 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020056 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010057};
58
Willy Tarreau12785782012-04-27 21:37:17 +020059/* static sample used in sample_process() when <p> is NULL */
Emeric Brune5c918b2017-06-14 14:15:36 +020060static THREAD_LOCAL struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010061
Willy Tarreau12785782012-04-27 21:37:17 +020062/* list head of all known sample fetch keywords */
63static struct sample_fetch_kw_list sample_fetches = {
64 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010065};
66
Willy Tarreau12785782012-04-27 21:37:17 +020067/* list head of all known sample format conversion keywords */
68static struct sample_conv_kw_list sample_convs = {
69 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010070};
71
Willy Tarreau80aca902013-01-07 15:42:20 +010072const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
Willy Tarreaube2159b2021-03-26 11:56:11 +010073 [SMP_SRC_CONST] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
74 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
75 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
76 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
77 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
78 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +010079 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL_CFG_PARSER |
80 SMP_VAL_CLI_PARSER ),
Willy Tarreaube2159b2021-03-26 11:56:11 +010081
Willy Tarreau80aca902013-01-07 15:42:20 +010082 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
83 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
84 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
85 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
86 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
87 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +010088 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
89 SMP_VAL_CLI_PARSER ),
Willy Tarreau80aca902013-01-07 15:42:20 +010090
91 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
92 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
93 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
94 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
95 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
96 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +010097 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
98 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +010099
100 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
101 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
102 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
103 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
104 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
105 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100106 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
107 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100108
109 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | 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_BE_RES_CNT |
113 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
114 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100115 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
116 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100117
118 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
119 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
120 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
121 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
122 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
123 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100124 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
125 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100126
127 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
128 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
129 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
130 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
131 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
132 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100133 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
134 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100135
136 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
137 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
138 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
139 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
140 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
141 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100142 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
143 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100144
145 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
146 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
147 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
148 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
149 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
150 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100151 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
152 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100153
154 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
155 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
156 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
157 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
158 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
159 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100160 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
161 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100162
163 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
164 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
165 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
166 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
167 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
168 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100169 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
170 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100171
172 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
173 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
174 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
175 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
176 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
177 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100178 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
179 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100180
181 [SMP_SRC_SERVR] = (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_BE_SRV_CON | 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 |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100187 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
188 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100189
190 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
191 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
192 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
193 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
194 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
195 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100196 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
197 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100198
199 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
200 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
201 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
202 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
203 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
204 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100205 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
206 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100207
208 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
209 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
210 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
211 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
212 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
213 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100214 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
215 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100216
217 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
218 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
219 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
220 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
221 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
222 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100223 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
224 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100225
226 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
227 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
228 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
229 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
230 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
231 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100232 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
233 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100234
235 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
236 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
237 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
238 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
239 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
240 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100241 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
242 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100243
244 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
245 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
246 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
247 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
248 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
249 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100250 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
251 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100252
253 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
254 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
255 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
256 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
257 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
258 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100259 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
260 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100261
262 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
263 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
264 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
265 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
266 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
267 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100268 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
269 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100270
271 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
272 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
273 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
274 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
275 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
276 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100277 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
278 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100279};
280
281static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
282 [SMP_SRC_INTRN] = "internal state",
283 [SMP_SRC_LISTN] = "listener",
284 [SMP_SRC_FTEND] = "frontend",
285 [SMP_SRC_L4CLI] = "client address",
286 [SMP_SRC_L5CLI] = "client-side connection",
287 [SMP_SRC_TRACK] = "track counters",
288 [SMP_SRC_L6REQ] = "request buffer",
289 [SMP_SRC_HRQHV] = "HTTP request headers",
290 [SMP_SRC_HRQHP] = "HTTP request",
291 [SMP_SRC_HRQBO] = "HTTP request body",
292 [SMP_SRC_BKEND] = "backend",
293 [SMP_SRC_SERVR] = "server",
294 [SMP_SRC_L4SRV] = "server address",
295 [SMP_SRC_L5SRV] = "server-side connection",
296 [SMP_SRC_L6RES] = "response buffer",
297 [SMP_SRC_HRSHV] = "HTTP response headers",
298 [SMP_SRC_HRSHP] = "HTTP response",
299 [SMP_SRC_HRSBO] = "HTTP response body",
300 [SMP_SRC_RQFIN] = "request buffer statistics",
301 [SMP_SRC_RSFIN] = "response buffer statistics",
302 [SMP_SRC_TXFIN] = "transaction statistics",
303 [SMP_SRC_SSFIN] = "session statistics",
304};
305
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100306static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
307 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
308 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
309 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
310 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
311 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
312 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
313 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
314 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
315 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
316 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
317 [SMP_CKP_BE_SRV_CON] = "server source selection",
318 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
319 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
320 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
321 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
322 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
323 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
324 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
325 [SMP_CKP_FE_LOG_END] = "logs",
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100326 [SMP_CKP_BE_CHK_RUL] = "backend tcp-check rule",
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100327};
328
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100329/* This function returns the type of the data returned by the sample_expr.
330 * It assumes that the <expr> and all of its converters are properly
331 * initialized.
332 */
333inline
334int smp_expr_output_type(struct sample_expr *expr)
335{
336 struct sample_conv_expr *smp_expr;
337
338 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
339 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
340 return smp_expr->conv->out_type;
341 }
342 return expr->fetch->out_type;
343}
344
345
Willy Tarreau80aca902013-01-07 15:42:20 +0100346/* fill the trash with a comma-delimited list of source names for the <use> bit
347 * field which must be composed of a non-null set of SMP_USE_* flags. The return
348 * value is the pointer to the string in the trash buffer.
349 */
350const char *sample_src_names(unsigned int use)
351{
352 int bit;
353
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200354 trash.data = 0;
355 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100356 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
357 if (!(use & ~((1 << bit) - 1)))
358 break; /* no more bits */
359
360 if (!(use & (1 << bit)))
361 continue; /* bit not set */
362
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200363 trash.data += snprintf(trash.area + trash.data,
364 trash.size - trash.data, "%s%s",
365 (use & ((1 << bit) - 1)) ? "," : "",
366 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100367 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200368 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100369}
370
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100371/* return a pointer to the correct sample checkpoint name, or "unknown" when
372 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
373 * if set.
374 */
375const char *sample_ckp_names(unsigned int use)
376{
377 int bit;
378
379 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
380 if (use & (1 << bit))
381 return fetch_ckp_names[bit];
382 return "unknown sample check place, please report this bug";
383}
384
Emeric Brun107ca302010-01-04 16:16:05 +0100385/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100386 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
387 * for next parsing sessions. The fetch keywords capabilities are also computed
388 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100389 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100390void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100391{
Willy Tarreau80aca902013-01-07 15:42:20 +0100392 struct sample_fetch *sf;
393 int bit;
394
395 for (sf = kwl->kw; sf->kw != NULL; sf++) {
396 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
397 if (sf->use & (1 << bit))
398 sf->val |= fetch_cap[bit];
399 }
400 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100401}
402
403/*
Willy Tarreau12785782012-04-27 21:37:17 +0200404 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100405 * parsing sessions.
406 */
Willy Tarreau12785782012-04-27 21:37:17 +0200407void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100408{
Willy Tarreau12785782012-04-27 21:37:17 +0200409 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100410}
411
412/*
Willy Tarreau12785782012-04-27 21:37:17 +0200413 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100414 * string of <len> in buffer <kw>.
415 *
416 */
Willy Tarreau12785782012-04-27 21:37:17 +0200417struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100418{
419 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200420 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100421
Willy Tarreau12785782012-04-27 21:37:17 +0200422 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100423 for (index = 0; kwl->kw[index].kw != NULL; index++) {
424 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
425 kwl->kw[index].kw[len] == '\0')
426 return &kwl->kw[index];
427 }
428 }
429 return NULL;
430}
431
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100432/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100433 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100434 * <idx> is the index of the next sample fetch entry. It is used as private
435 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100436 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100437 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100438 * the list is reached.
439 */
440struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
441{
442 struct sample_fetch_kw_list *kwl;
443 struct sample_fetch *base;
444
445 if (!current) {
446 /* Get first kwl entry. */
447 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
448 (*idx) = 0;
449 } else {
450 /* Get kwl corresponding to the curret entry. */
451 base = current + 1 - (*idx);
452 kwl = container_of(base, struct sample_fetch_kw_list, kw);
453 }
454
455 while (1) {
456
457 /* Check if kwl is the last entry. */
458 if (&kwl->list == &sample_fetches.list)
459 return NULL;
460
461 /* idx contain the next keyword. If it is available, return it. */
462 if (kwl->kw[*idx].kw) {
463 (*idx)++;
464 return &kwl->kw[(*idx)-1];
465 }
466
467 /* get next entry in the main list, and return NULL if the end is reached. */
468 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
469
470 /* Set index to 0, ans do one other loop. */
471 (*idx) = 0;
472 }
473}
474
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100475/* This function browses the list of available converters. <current> is
476 * the last used converter. If it is the first call, it must set to NULL.
477 * <idx> is the index of the next converter entry. It is used as private
478 * value. It is useless to initiate it.
479 *
480 * It returns always the next sample_conv entry, and NULL when the end of
481 * the list is reached.
482 */
483struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
484{
485 struct sample_conv_kw_list *kwl;
486 struct sample_conv *base;
487
488 if (!current) {
489 /* Get first kwl entry. */
490 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
491 (*idx) = 0;
492 } else {
493 /* Get kwl corresponding to the curret entry. */
494 base = current + 1 - (*idx);
495 kwl = container_of(base, struct sample_conv_kw_list, kw);
496 }
497
498 while (1) {
499 /* Check if kwl is the last entry. */
500 if (&kwl->list == &sample_convs.list)
501 return NULL;
502
503 /* idx contain the next keyword. If it is available, return it. */
504 if (kwl->kw[*idx].kw) {
505 (*idx)++;
506 return &kwl->kw[(*idx)-1];
507 }
508
509 /* get next entry in the main list, and return NULL if the end is reached. */
510 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
511
512 /* Set index to 0, ans do one other loop. */
513 (*idx) = 0;
514 }
515}
516
Emeric Brun107ca302010-01-04 16:16:05 +0100517/*
Willy Tarreau12785782012-04-27 21:37:17 +0200518 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100519 * string of <len> in buffer <kw>.
520 *
521 */
Willy Tarreau12785782012-04-27 21:37:17 +0200522struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100523{
524 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200525 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100526
Willy Tarreau12785782012-04-27 21:37:17 +0200527 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100528 for (index = 0; kwl->kw[index].kw != NULL; index++) {
529 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
530 kwl->kw[index].kw[len] == '\0')
531 return &kwl->kw[index];
532 }
533 }
534 return NULL;
535}
536
Emeric Brun107ca302010-01-04 16:16:05 +0100537/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200538/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100539/******************************************************************/
540
Willy Tarreau342acb42012-04-23 22:03:39 +0200541static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100542{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200543 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200544 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100545 return 1;
546}
547
Willy Tarreau342acb42012-04-23 22:03:39 +0200548static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100549{
Willy Tarreau83061a82018-07-13 11:56:34 +0200550 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100551
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200552 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100553 return 0;
554
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200555 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200556 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200557 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100558 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100559
560 return 1;
561}
562
Willy Tarreau342acb42012-04-23 22:03:39 +0200563static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100564{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200565 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200566 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100567 return 1;
568}
569
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200570static int c_ipv62ip(struct sample *smp)
571{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200572 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200573 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100574 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200575 return 1;
576}
577
Willy Tarreau342acb42012-04-23 22:03:39 +0200578static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100579{
Willy Tarreau83061a82018-07-13 11:56:34 +0200580 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100581
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200582 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100583 return 0;
584
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200585 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200586 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200587 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100588 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100589 return 1;
590}
591
592/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200593static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100594{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200595 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100596}
597*/
598
Willy Tarreau342acb42012-04-23 22:03:39 +0200599static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100600{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200601 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200602 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100603 return 1;
604}
605
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200606static int c_int2ipv6(struct sample *smp)
607{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200608 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
609 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200610 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200611 return 1;
612}
613
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100614static int c_str2addr(struct sample *smp)
615{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200616 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
617 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100618 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200619 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100620 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100621 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100622 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200623 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100624 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100625 return 1;
626}
627
Willy Tarreau342acb42012-04-23 22:03:39 +0200628static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100629{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200630 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100631 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200632 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100633 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100634 return 1;
635}
636
Willy Tarreau342acb42012-04-23 22:03:39 +0200637static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100638{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200639 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100640 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200641 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100642 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100643 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100644}
645
Emeric Brun4b9e8022014-11-03 18:17:10 +0100646/*
647 * The NULL char always enforces the end of string if it is met.
648 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100649 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200650static int c_bin2str(struct sample *smp)
651{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100652 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200653
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200654 for (i = 0; i < smp->data.u.str.data; i++) {
655 if (!smp->data.u.str.area[i]) {
656 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100657 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100658 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200659 }
Christopher Faulet472ad512020-04-30 09:57:40 +0200660 smp->data.type = SMP_T_STR;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200661 return 1;
662}
663
Willy Tarreau342acb42012-04-23 22:03:39 +0200664static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100665{
Willy Tarreau83061a82018-07-13 11:56:34 +0200666 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100667 char *pos;
668
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200669 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100670 if (!pos)
671 return 0;
672
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200673 trash->size = trash->size - (pos - trash->area);
674 trash->area = pos;
675 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200676 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200677 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100678 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100679 return 1;
680}
681
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800682/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200683 * For strings and binary blocks, it also provides a known allocated size with
684 * a length that is capped to the size, and ensures a trailing zero is always
685 * appended for strings. This is necessary for some operations which may
686 * require to extend the length. It returns 0 if it fails, 1 on success.
687 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100688int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200689{
Willy Tarreau83061a82018-07-13 11:56:34 +0200690 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200691
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200692 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100693 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100694 case SMP_T_SINT:
695 case SMP_T_ADDR:
696 case SMP_T_IPV4:
697 case SMP_T_IPV6:
698 /* These type are not const. */
699 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200700
Christopher Fauletec100512017-07-24 15:38:41 +0200701 case SMP_T_METH:
702 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
703 break;
704 /* Fall through */
705
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100706 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200707 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100708 trash->data = smp->data.type == SMP_T_STR ?
709 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200710 if (trash->data > trash->size - 1)
711 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200712
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100713 memcpy(trash->area, smp->data.type == SMP_T_STR ?
714 smp->data.u.str.area : smp->data.u.meth.str.area,
715 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200716 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200717 smp->data.u.str = *trash;
718 break;
719
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100720 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100721 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200722 trash->data = smp->data.u.str.data;
723 if (trash->data > trash->size)
724 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200725
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200726 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200727 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100728 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200729
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100730 default:
731 /* Other cases are unexpected. */
732 return 0;
733 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100734
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100735 /* remove const flag */
736 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200737 return 1;
738}
739
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100740int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100741{
742 return 1;
743}
744
Willy Tarreau342acb42012-04-23 22:03:39 +0200745static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100746{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200747 const char *str;
748 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100749
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200750 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100751 return 0;
752
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200753 str = smp->data.u.str.area;
754 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100755
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200756 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200757 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100758 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100759 return 1;
760}
761
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100762static int c_str2meth(struct sample *smp)
763{
764 enum http_meth_t meth;
765 int len;
766
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200767 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100768 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200769 len = smp->data.u.str.data;
770 smp->data.u.meth.str.area = smp->data.u.str.area;
771 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100772 }
773 else
774 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200775 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200776 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100777 return 1;
778}
779
780static int c_meth2str(struct sample *smp)
781{
782 int len;
783 enum http_meth_t meth;
784
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200785 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100786 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200787 len = smp->data.u.meth.str.data;
788 smp->data.u.str.area = smp->data.u.meth.str.area;
789 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200790 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100791 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200792 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100793 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200794 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200795 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200796 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100797 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200798 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100799 }
800 else {
801 /* Unknown method */
802 return 0;
803 }
804 return 1;
805}
806
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200807static int c_addr2bin(struct sample *smp)
808{
Willy Tarreau83061a82018-07-13 11:56:34 +0200809 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200810
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200811 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200812 chk->data = 4;
813 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200814 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200815 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200816 chk->data = 16;
817 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200818 }
819 else
820 return 0;
821
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200822 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200823 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200824 return 1;
825}
826
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200827static int c_int2bin(struct sample *smp)
828{
Willy Tarreau83061a82018-07-13 11:56:34 +0200829 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200830
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200831 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
832 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200833
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200834 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200835 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200836 return 1;
837}
838
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200839
Emeric Brun107ca302010-01-04 16:16:05 +0100840/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200841/* Sample casts matrix: */
842/* sample_casts[from type][to type] */
843/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100844/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100845
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100846sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200847/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
848/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
849/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200850/* SINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, c_int2ipv6, c_int2str, c_int2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200851/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
852/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200853/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200854/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
855/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
856/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200857};
Emeric Brun107ca302010-01-04 16:16:05 +0100858
Emeric Brun107ca302010-01-04 16:16:05 +0100859/*
Willy Tarreau12785782012-04-27 21:37:17 +0200860 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100861 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200862 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200863 * The caller must have set al->ctx.
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100864 * If <endptr> is non-nul, it will be set to the first unparsed character
865 * (which may be the final '\0') on success. If it is nul, the expression
866 * must be properly terminated by a '\0' otherwise an error is reported.
Emeric Brun107ca302010-01-04 16:16:05 +0100867 */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100868struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err_msg, struct arg_list *al, char **endptr)
Emeric Brun107ca302010-01-04 16:16:05 +0100869{
Willy Tarreau833cc792013-07-24 15:34:19 +0200870 const char *begw; /* beginning of word */
871 const char *endw; /* end of word */
872 const char *endt; /* end of term */
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +0100873 struct sample_expr *expr = NULL;
Willy Tarreau12785782012-04-27 21:37:17 +0200874 struct sample_fetch *fetch;
875 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100876 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200877 char *fkw = NULL;
878 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100879 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100880
Willy Tarreau833cc792013-07-24 15:34:19 +0200881 begw = str[*idx];
Willy Tarreaued2c6622020-02-14 18:27:10 +0100882 for (endw = begw; is_idchar(*endw); endw++)
883 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200884
885 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100886 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100887 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200888 }
Emeric Brun107ca302010-01-04 16:16:05 +0100889
Willy Tarreau833cc792013-07-24 15:34:19 +0200890 /* keep a copy of the current fetch keyword for error reporting */
891 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100892
Willy Tarreau833cc792013-07-24 15:34:19 +0200893 fetch = find_sample_fetch(begw, endw - begw);
894 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100895 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100896 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200897 }
Emeric Brun107ca302010-01-04 16:16:05 +0100898
Willy Tarreau833cc792013-07-24 15:34:19 +0200899 /* At this point, we have :
900 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100901 * - endw : end of the keyword, first character not part of keyword
Willy Tarreau833cc792013-07-24 15:34:19 +0200902 */
903
904 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100905 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100906 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200907 }
Emeric Brun107ca302010-01-04 16:16:05 +0100908 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200909
Vincent Bernat02779b62016-04-03 13:48:43 +0200910 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200911 if (!expr)
912 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100913
914 LIST_INIT(&(expr->conv_exprs));
915 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200916 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100917
Willy Tarreau689a1df2013-12-13 00:40:11 +0100918 /* Note that we call the argument parser even with an empty string,
919 * this allows it to automatically create entries for mandatory
920 * implicit arguments (eg: local proxy name).
921 */
922 al->kw = expr->fetch->kw;
923 al->conv = NULL;
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100924 if (make_arg_list(endw, -1, fetch->arg_mask, &expr->arg_p, err_msg, &endt, &err_arg, al) < 0) {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100925 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
926 goto out_error;
927 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200928
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100929 /* now endt is our first char not part of the arg list, typically the
930 * comma after the sample fetch name or after the closing parenthesis,
931 * or the NUL char.
932 */
933
Willy Tarreau689a1df2013-12-13 00:40:11 +0100934 if (!expr->arg_p) {
935 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200936 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100937 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
938 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200939 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100940 }
941
Willy Tarreau833cc792013-07-24 15:34:19 +0200942 /* Now process the converters if any. We have two supported syntaxes
943 * for the converters, which can be combined :
944 * - comma-delimited list of converters just after the keyword and args ;
945 * - one converter per keyword
946 * The combination allows to have each keyword being a comma-delimited
947 * series of converters.
948 *
949 * We want to process the former first, then the latter. For this we start
950 * from the beginning of the supposed place in the exiting conv chain, which
951 * starts at the last comma (endt).
952 */
953
954 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200955 struct sample_conv_expr *conv_expr;
Willy Tarreau46dfd782019-12-17 10:25:29 +0100956 int err_arg;
957 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +0100958
Willy Tarreau833cc792013-07-24 15:34:19 +0200959 if (*endt && *endt != ',') {
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100960 if (endptr) {
961 /* end found, let's stop here */
962 break;
963 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200964 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100965 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200966 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100967 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100968 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200969 }
Emeric Brun107ca302010-01-04 16:16:05 +0100970
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100971 /* FIXME: how long should we support such idiocies ? Maybe we
972 * should already warn ?
973 */
Willy Tarreau833cc792013-07-24 15:34:19 +0200974 while (*endt == ',') /* then trailing commas */
975 endt++;
976
Willy Tarreau97108e02016-11-25 07:33:24 +0100977 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200978
979 if (!*begw) {
980 /* none ? skip to next string */
981 (*idx)++;
982 begw = str[*idx];
983 if (!begw || !*begw)
984 break;
985 }
986
Willy Tarreaued2c6622020-02-14 18:27:10 +0100987 for (endw = begw; is_idchar(*endw); endw++)
988 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200989
990 free(ckw);
991 ckw = my_strndup(begw, endw - begw);
992
993 conv = find_sample_conv(begw, endw - begw);
994 if (!conv) {
995 /* we found an isolated keyword that we don't know, it's not ours */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100996 if (begw == str[*idx]) {
997 endt = begw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200998 break;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100999 }
Willy Tarreau97108e02016-11-25 07:33:24 +01001000 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +02001001 goto out_error;
1002 }
Emeric Brun107ca302010-01-04 16:16:05 +01001003
Willy Tarreau833cc792013-07-24 15:34:19 +02001004 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001005 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001006 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001007 }
Emeric Brun107ca302010-01-04 16:16:05 +01001008
1009 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +02001010 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001011 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001012 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001013 }
Emeric Brun107ca302010-01-04 16:16:05 +01001014
1015 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +02001016 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +02001017 if (!conv_expr)
1018 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001019
1020 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
1021 conv_expr->conv = conv;
1022
Willy Tarreau46dfd782019-12-17 10:25:29 +01001023 al->kw = expr->fetch->kw;
1024 al->conv = conv_expr->conv->kw;
Willy Tarreau80b53ff2020-02-14 08:40:37 +01001025 argcnt = make_arg_list(endw, -1, conv->arg_mask, &conv_expr->arg_p, err_msg, &endt, &err_arg, al);
Willy Tarreau46dfd782019-12-17 10:25:29 +01001026 if (argcnt < 0) {
1027 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
1028 goto out_error;
1029 }
Willy Tarreau9e92d322010-01-26 17:58:06 +01001030
Willy Tarreau46dfd782019-12-17 10:25:29 +01001031 if (argcnt && !conv->arg_mask) {
1032 memprintf(err_msg, "converter '%s' does not support any args", ckw);
1033 goto out_error;
1034 }
Willy Tarreau21d68a62012-04-20 15:52:36 +02001035
Willy Tarreau46dfd782019-12-17 10:25:29 +01001036 if (!conv_expr->arg_p)
1037 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001038
Willy Tarreau46dfd782019-12-17 10:25:29 +01001039 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1040 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001041 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001042 }
1043 }
Emeric Brun485479d2010-09-23 18:02:19 +02001044
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001045 if (endptr) {
1046 /* end found, let's stop here */
1047 *endptr = (char *)endt;
1048 }
1049
Willy Tarreau833cc792013-07-24 15:34:19 +02001050 out:
1051 free(fkw);
1052 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001053 return expr;
1054
1055out_error:
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +01001056 release_sample_expr(expr);
Willy Tarreau833cc792013-07-24 15:34:19 +02001057 expr = NULL;
1058 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001059}
1060
1061/*
Willy Tarreau12785782012-04-27 21:37:17 +02001062 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001063 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001064 * Returns a pointer on a typed sample structure containing the result or NULL if
1065 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001066 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001067 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001068 *
1069 * Note: the fetch functions are required to properly set the return type. The
1070 * conversion functions must do so too. However the cast functions do not need
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001071 * to since they're made to cast multiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001072 *
1073 * The caller may indicate in <opt> if it considers the result final or not.
1074 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1075 * if the result is stable or not, according to the following table :
1076 *
1077 * return MAY_CHANGE FINAL Meaning for the sample
1078 * NULL 0 * Not present and will never be (eg: header)
1079 * NULL 1 0 Not present yet, could change (eg: POST param)
1080 * NULL 1 1 Not present yet, will not change anymore
1081 * smp 0 * Present and will not change (eg: header)
1082 * smp 1 0 Present, may change (eg: request length)
1083 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001084 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001085struct sample *sample_process(struct proxy *px, struct session *sess,
1086 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001087 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001088{
Willy Tarreau12785782012-04-27 21:37:17 +02001089 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001090
Willy Tarreau18387e22013-07-25 12:02:38 +02001091 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001092 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001093 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001094 }
Emeric Brun107ca302010-01-04 16:16:05 +01001095
Willy Tarreau1777ea62016-03-10 16:15:46 +01001096 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001097 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001098 return NULL;
1099
Emeric Brun107ca302010-01-04 16:16:05 +01001100 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001101 /* we want to ensure that p->type can be casted into
1102 * conv_expr->conv->in_type. We have 3 possibilities :
1103 * - NULL => not castable.
1104 * - c_none => nothing to do (let's optimize it)
1105 * - other => apply cast and prepare to fail
1106 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001107 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001108 return NULL;
1109
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001110 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1111 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001112 return NULL;
1113
Willy Tarreau12e50112012-04-25 17:21:49 +02001114 /* OK cast succeeded */
1115
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001116 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001117 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001118 }
1119 return p;
1120}
1121
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001122/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001123 * Resolve all remaining arguments in proxy <p>. Returns the number of
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001124 * errors or 0 if everything is fine. If at least one error is met, it will
1125 * be appended to *err. If *err==NULL it will be allocated first.
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001126 */
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001127int smp_resolve_args(struct proxy *p, char **err)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001128{
1129 struct arg_list *cur, *bak;
1130 const char *ctx, *where;
1131 const char *conv_ctx, *conv_pre, *conv_pos;
1132 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001133 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001134 struct arg *arg;
1135 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001136 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001137
1138 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1139 struct proxy *px;
1140 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001141 struct stktable *t;
1142 char *pname, *sname, *stktname;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001143 char *err2;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001144
1145 arg = cur->arg;
1146
1147 /* prepare output messages */
1148 conv_pre = conv_pos = conv_ctx = "";
1149 if (cur->conv) {
1150 conv_ctx = cur->conv;
1151 conv_pre = "conversion keyword '";
1152 conv_pos = "' for ";
1153 }
1154
1155 where = "in";
1156 ctx = "sample fetch keyword";
1157 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001158 case ARGC_STK: where = "in stick rule in"; break;
1159 case ARGC_TRK: where = "in tracking rule in"; break;
1160 case ARGC_LOG: where = "in log-format string in"; break;
1161 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1162 case ARGC_HRQ: where = "in http-request header format string in"; break;
1163 case ARGC_HRS: where = "in http-response header format string in"; break;
1164 case ARGC_UIF: where = "in unique-id-format string in"; break;
1165 case ARGC_RDR: where = "in redirect format string in"; break;
1166 case ARGC_CAP: where = "in capture rule in"; break;
1167 case ARGC_ACL: ctx = "ACL keyword"; break;
1168 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001169 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001170 case ARGC_HERR: where = "in http-error directive in"; break;
Miroslav Zagorac7f8314c2020-12-09 16:31:48 +01001171 case ARGC_OT: where = "in ot-scope directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001172 }
1173
1174 /* set a few default settings */
1175 px = p;
1176 pname = p->id;
1177
1178 switch (arg->type) {
1179 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001180 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001181 memprintf(err, "%sparsing [%s:%d]: missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1182 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001183 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001184 cfgerr++;
1185 continue;
1186 }
1187
1188 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001189 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001190
1191 if (sname) {
1192 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001193 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001194
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001195 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001196 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001197 memprintf(err, "%sparsing [%s:%d]: unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1198 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001199 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001200 cfgerr++;
1201 break;
1202 }
1203 }
1204 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001205 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001206
1207 srv = findserver(px, sname);
1208 if (!srv) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001209 memprintf(err, "%sparsing [%s:%d]: unable to find server '%s' in proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1210 *err ? *err : "", cur->file, cur->line, sname, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001211 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001212 cfgerr++;
1213 break;
1214 }
1215
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001216 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001217 arg->unresolved = 0;
1218 arg->data.srv = srv;
1219 break;
1220
1221 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001222 if (arg->data.str.data) {
1223 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001224 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001225 }
1226
1227 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001228 memprintf(err, "%sparsing [%s:%d]: unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1229 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001230 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001231 cfgerr++;
1232 break;
1233 }
1234
1235 if (!(px->cap & PR_CAP_FE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001236 memprintf(err, "%sparsing [%s:%d]: proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1237 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001238 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001239 cfgerr++;
1240 break;
1241 }
1242
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001243 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001244 arg->unresolved = 0;
1245 arg->data.prx = px;
1246 break;
1247
1248 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001249 if (arg->data.str.data) {
1250 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001251 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001252 }
1253
1254 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001255 memprintf(err, "%sparsing [%s:%d]: unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1256 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001257 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001258 cfgerr++;
1259 break;
1260 }
1261
1262 if (!(px->cap & PR_CAP_BE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001263 memprintf(err, "%sparsing [%s:%d]: proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1264 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001265 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001266 cfgerr++;
1267 break;
1268 }
1269
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001270 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001271 arg->unresolved = 0;
1272 arg->data.prx = px;
1273 break;
1274
1275 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001276 if (arg->data.str.data)
1277 stktname = arg->data.str.area;
1278 else
1279 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001280
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001281 t = stktable_find_by_name(stktname);
1282 if (!t) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001283 memprintf(err, "%sparsing [%s:%d]: unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1284 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001285 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001286 cfgerr++;
1287 break;
1288 }
1289
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001290 if (!t->size) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001291 memprintf(err, "%sparsing [%s:%d]: no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1292 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001293 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001294 cfgerr++;
1295 break;
1296 }
1297
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001298 if (t->proxy && (p->bind_proc & ~t->proxy->bind_proc)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001299 memprintf(err, "%sparsing [%s:%d]: stick-table '%s' not present on all processes covered by proxy '%s'.\n",
1300 *err ? *err : "", cur->file, cur->line, t->proxy->id, p->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01001301 cfgerr++;
1302 break;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001303 }
1304
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001305 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001306 p->next_stkt_ref = t->proxies_list;
1307 t->proxies_list = p;
1308 }
1309
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001310 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001311 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001312 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001313 break;
1314
1315 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001316 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001317 memprintf(err, "%sparsing [%s:%d]: missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1318 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001319 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001320 cfgerr++;
1321 break;
1322 }
1323
1324 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001325 strcmp(p->uri_auth->userlist->name, arg->data.str.area) == 0)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001326 ul = p->uri_auth->userlist;
1327 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001328 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001329
1330 if (!ul) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001331 memprintf(err, "%sparsing [%s:%d]: unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1332 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001333 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001334 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001335 cfgerr++;
1336 break;
1337 }
1338
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001339 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001340 arg->unresolved = 0;
1341 arg->data.usr = ul;
1342 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001343
1344 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001345 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001346 memprintf(err, "%sparsing [%s:%d]: missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1347 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001348 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001349 cfgerr++;
1350 continue;
1351 }
1352
Willy Tarreau46947782015-01-19 19:00:58 +01001353 rflags = 0;
1354 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001355 err2 = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001356
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001357 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err2))) {
1358 memprintf(err, "%sparsing [%s:%d]: error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1359 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001360 arg->data.str.area,
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001361 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err2);
Willy Tarreau46947782015-01-19 19:00:58 +01001362 cfgerr++;
1363 continue;
1364 }
1365
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001366 chunk_destroy(&arg->data.str);
Willy Tarreau46947782015-01-19 19:00:58 +01001367 arg->unresolved = 0;
1368 arg->data.reg = reg;
1369 break;
1370
1371
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001372 }
1373
1374 LIST_DEL(&cur->list);
1375 free(cur);
1376 } /* end of args processing */
1377
1378 return cfgerr;
1379}
1380
1381/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001382 * Process a fetch + format conversion as defined by the sample expression
1383 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001384 * not explicitly set to <smp_type>, but shall be compatible with it as
1385 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1386 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001387 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1388 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1389 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1390 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001391 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1392 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1393 * still hope to get it after waiting longer, and is not converted to string.
1394 * The possible output combinations are the following :
1395 *
1396 * return MAY_CHANGE FINAL Meaning for the sample
1397 * NULL * * Not present and will never be (eg: header)
1398 * smp 0 * Final value converted (eg: header)
1399 * smp 1 0 Not present yet, may appear later (eg: header)
1400 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001401 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001402struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001403 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001404 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001405{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001406 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001407
Willy Tarreau6c616e02014-06-25 16:56:41 +02001408 memset(smp, 0, sizeof(*smp));
1409
Willy Tarreau192252e2015-04-04 01:47:55 +02001410 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001411 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1412 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001413 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001414 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001415
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001416 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001417 return NULL;
1418
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001419 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001420 return NULL;
1421
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001422 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001423 return smp;
1424}
1425
Christopher Faulet476e5d02016-10-26 11:34:47 +02001426static void release_sample_arg(struct arg *p)
1427{
1428 struct arg *p_back = p;
1429
1430 if (!p)
1431 return;
1432
1433 while (p->type != ARGT_STOP) {
1434 if (p->type == ARGT_STR || p->unresolved) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001435 chunk_destroy(&p->data.str);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001436 p->unresolved = 0;
1437 }
1438 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001439 regex_free(p->data.reg);
1440 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001441 }
1442 p++;
1443 }
1444
1445 if (p_back != empty_arg_list)
1446 free(p_back);
1447}
1448
1449void release_sample_expr(struct sample_expr *expr)
1450{
1451 struct sample_conv_expr *conv_expr, *conv_exprb;
1452
1453 if (!expr)
1454 return;
1455
Tim Duesterhus867cd982020-07-04 11:49:39 +02001456 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list) {
1457 LIST_DEL(&conv_expr->list);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001458 release_sample_arg(conv_expr->arg_p);
Tim Duesterhus867cd982020-07-04 11:49:39 +02001459 free(conv_expr);
1460 }
1461
Christopher Faulet476e5d02016-10-26 11:34:47 +02001462 release_sample_arg(expr->arg_p);
1463 free(expr);
1464}
1465
Emeric Brun107ca302010-01-04 16:16:05 +01001466/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001467/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001468/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001469/*****************************************************************/
1470
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001471static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1472{
1473 int i;
1474 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001475 struct buffer *buf;
1476 struct sink *sink;
1477 struct ist line;
1478 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001479
Willy Tarreau0851fd52019-12-17 10:07:25 +01001480 buf = alloc_trash_chunk();
1481 if (!buf)
1482 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001483
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001484 sink = (struct sink *)arg_p[1].data.ptr;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001485 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001486
Willy Tarreau0851fd52019-12-17 10:07:25 +01001487 pfx = arg_p[0].data.str.area;
1488 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001489
Willy Tarreau0851fd52019-12-17 10:07:25 +01001490 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1491 if (!sample_casts[smp->data.type][SMP_T_STR])
1492 goto nocast;
1493
1494 /* Copy sample fetch. This puts the sample as const, the
1495 * cast will copy data if a transformation is required.
1496 */
1497 memcpy(&tmp, smp, sizeof(struct sample));
1498 tmp.flags = SMP_F_CONST;
1499
1500 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1501 goto nocast;
1502
1503 /* Display the displayable chars*. */
1504 b_putchr(buf, '<');
1505 for (i = 0; i < tmp.data.u.str.data; i++) {
Willy Tarreau90807112020-02-25 08:16:33 +01001506 if (isprint((unsigned char)tmp.data.u.str.area[i]))
Willy Tarreau0851fd52019-12-17 10:07:25 +01001507 b_putchr(buf, tmp.data.u.str.area[i]);
1508 else
1509 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001510 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001511 b_putchr(buf, '>');
1512
1513 done:
1514 line = ist2(buf->area, buf->data);
Emeric Brun54648852020-07-06 15:54:06 +02001515 sink_write(sink, &line, 1, 0, 0, NULL);
Willy Tarreau0851fd52019-12-17 10:07:25 +01001516 end:
1517 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001518 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001519 nocast:
1520 chunk_appendf(buf, "(undisplayable)");
1521 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001522}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001523
1524// This function checks the "debug" converter's arguments.
1525static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1526 const char *file, int line, char **err)
1527{
1528 const char *name = "buf0";
1529 struct sink *sink = NULL;
1530
1531 if (args[0].type != ARGT_STR) {
1532 /* optional prefix */
1533 args[0].data.str.area = "";
1534 args[0].data.str.data = 0;
1535 }
1536
1537 if (args[1].type == ARGT_STR)
1538 name = args[1].data.str.area;
1539
1540 sink = sink_find(name);
1541 if (!sink) {
1542 memprintf(err, "No such sink '%s'", name);
1543 return 0;
1544 }
1545
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001546 chunk_destroy(&args[1].data.str);
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001547 args[1].type = ARGT_PTR;
1548 args[1].data.ptr = sink;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001549 return 1;
1550}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001551
Holger Just1bfc24b2017-05-06 00:56:53 +02001552static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1553{
Willy Tarreau83061a82018-07-13 11:56:34 +02001554 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001555 int bin_len;
1556
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001557 trash->data = 0;
1558 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1559 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001560 if (bin_len < 0)
1561 return 0;
1562
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001563 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001564 smp->data.u.str = *trash;
1565 smp->data.type = SMP_T_BIN;
1566 smp->flags &= ~SMP_F_CONST;
1567 return 1;
1568}
1569
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +02001570static int sample_conv_base64url2bin(const struct arg *arg_p, struct sample *smp, void *private)
1571{
1572 struct buffer *trash = get_trash_chunk();
1573 int bin_len;
1574
1575 trash->data = 0;
1576 bin_len = base64urldec(smp->data.u.str.area, smp->data.u.str.data,
1577 trash->area, trash->size);
1578 if (bin_len < 0)
1579 return 0;
1580
1581 trash->data = bin_len;
1582 smp->data.u.str = *trash;
1583 smp->data.type = SMP_T_BIN;
1584 smp->flags &= ~SMP_F_CONST;
1585 return 1;
1586}
1587
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001588static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001589{
Willy Tarreau83061a82018-07-13 11:56:34 +02001590 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001591 int b64_len;
1592
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001593 trash->data = 0;
1594 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1595 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001596 if (b64_len < 0)
1597 return 0;
1598
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001599 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001600 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001601 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001602 smp->flags &= ~SMP_F_CONST;
1603 return 1;
1604}
1605
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +02001606static int sample_conv_bin2base64url(const struct arg *arg_p, struct sample *smp, void *private)
1607{
1608 struct buffer *trash = get_trash_chunk();
1609 int b64_len;
1610
1611 trash->data = 0;
1612 b64_len = a2base64url(smp->data.u.str.area, smp->data.u.str.data,
1613 trash->area, trash->size);
1614 if (b64_len < 0)
1615 return 0;
1616
1617 trash->data = b64_len;
1618 smp->data.u.str = *trash;
1619 smp->data.type = SMP_T_STR;
1620 smp->flags &= ~SMP_F_CONST;
1621 return 1;
1622}
1623
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001624static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1625{
1626 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001627 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001628
1629 memset(&ctx, 0, sizeof(ctx));
1630
1631 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001632 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1633 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001634
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001635 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001636 smp->data.u.str = *trash;
1637 smp->data.type = SMP_T_BIN;
1638 smp->flags &= ~SMP_F_CONST;
1639 return 1;
1640}
1641
Tim Duesterhusd4376302019-06-17 12:41:44 +02001642#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01001643static int smp_check_sha2(struct arg *args, struct sample_conv *conv,
1644 const char *file, int line, char **err)
1645{
1646 if (args[0].type == ARGT_STOP)
1647 return 1;
1648 if (args[0].type != ARGT_SINT) {
1649 memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]);
1650 return 0;
1651 }
1652
1653 switch (args[0].data.sint) {
1654 case 224:
1655 case 256:
1656 case 384:
1657 case 512:
1658 /* this is okay */
1659 return 1;
1660 default:
1661 memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint);
1662 return 0;
1663 }
1664}
1665
Tim Duesterhusd4376302019-06-17 12:41:44 +02001666static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
1667{
1668 struct buffer *trash = get_trash_chunk();
1669 int bits = 256;
Christopher Faulete6e7a582021-01-29 11:29:28 +01001670 if (arg_p->data.sint)
Tim Duesterhusd4376302019-06-17 12:41:44 +02001671 bits = arg_p->data.sint;
1672
1673 switch (bits) {
1674 case 224: {
1675 SHA256_CTX ctx;
1676
1677 memset(&ctx, 0, sizeof(ctx));
1678
1679 SHA224_Init(&ctx);
1680 SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1681 SHA224_Final((unsigned char *) trash->area, &ctx);
1682 trash->data = SHA224_DIGEST_LENGTH;
1683 break;
1684 }
1685 case 256: {
1686 SHA256_CTX ctx;
1687
1688 memset(&ctx, 0, sizeof(ctx));
1689
1690 SHA256_Init(&ctx);
1691 SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1692 SHA256_Final((unsigned char *) trash->area, &ctx);
1693 trash->data = SHA256_DIGEST_LENGTH;
1694 break;
1695 }
1696 case 384: {
1697 SHA512_CTX ctx;
1698
1699 memset(&ctx, 0, sizeof(ctx));
1700
1701 SHA384_Init(&ctx);
1702 SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1703 SHA384_Final((unsigned char *) trash->area, &ctx);
1704 trash->data = SHA384_DIGEST_LENGTH;
1705 break;
1706 }
1707 case 512: {
1708 SHA512_CTX ctx;
1709
1710 memset(&ctx, 0, sizeof(ctx));
1711
1712 SHA512_Init(&ctx);
1713 SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1714 SHA512_Final((unsigned char *) trash->area, &ctx);
1715 trash->data = SHA512_DIGEST_LENGTH;
1716 break;
1717 }
1718 default:
1719 return 0;
1720 }
1721
1722 smp->data.u.str = *trash;
1723 smp->data.type = SMP_T_BIN;
1724 smp->flags &= ~SMP_F_CONST;
1725 return 1;
1726}
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001727
Dragan Dosen9e8db132021-02-22 10:03:53 +01001728/* This function returns a sample struct filled with an <arg> content.
1729 * If the <arg> contains a string, it is returned in the sample flagged as
1730 * SMP_F_CONST. If the <arg> contains a variable descriptor, the sample is
1731 * filled with the content of the variable by using vars_get_by_desc().
1732 *
1733 * Keep in mind that the sample content may be written to a pre-allocated
1734 * trash chunk as returned by get_trash_chunk().
1735 *
1736 * This function returns 0 if an error occurs, otherwise it returns 1.
1737 */
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001738static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
1739{
1740 switch (arg->type) {
1741 case ARGT_STR:
1742 smp->data.type = SMP_T_STR;
1743 smp->data.u.str = arg->data.str;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001744 smp->flags = SMP_F_CONST;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001745 return 1;
1746 case ARGT_VAR:
1747 if (!vars_get_by_desc(&arg->data.var, smp))
1748 return 0;
1749 if (!sample_casts[smp->data.type][SMP_T_STR])
1750 return 0;
1751 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
1752 return 0;
1753 return 1;
1754 default:
1755 return 0;
1756 }
1757}
1758
Dragan Dosen9e8db132021-02-22 10:03:53 +01001759/* This function checks an <arg> and fills it with a variable type if the
1760 * <arg> string contains a valid variable name. If failed, the function
1761 * tries to perform a base64 decode operation on the same string, and
1762 * fills the <arg> with the decoded content.
1763 *
1764 * Validation is skipped if the <arg> string is empty.
1765 *
1766 * This function returns 0 if the variable lookup fails and the specified
1767 * <arg> string is not a valid base64 encoded string, as well if
1768 * unexpected argument type is specified or memory allocation error
1769 * occurs. Otherwise it returns 1.
1770 */
1771static inline int sample_check_arg_base64(struct arg *arg, char **err)
1772{
1773 char *dec = NULL;
1774 int dec_size;
1775
1776 if (arg->type != ARGT_STR) {
1777 memprintf(err, "unexpected argument type");
1778 return 0;
1779 }
1780
1781 if (arg->data.str.data == 0) /* empty */
1782 return 1;
1783
1784 if (vars_check_arg(arg, NULL))
1785 return 1;
1786
1787 if (arg->data.str.data % 4) {
1788 memprintf(err, "argument needs to be base64 encoded, and "
1789 "can either be a string or a variable");
1790 return 0;
1791 }
1792
1793 dec_size = (arg->data.str.data / 4 * 3)
1794 - (arg->data.str.area[arg->data.str.data-1] == '=' ? 1 : 0)
1795 - (arg->data.str.area[arg->data.str.data-2] == '=' ? 1 : 0);
1796
1797 if ((dec = malloc(dec_size)) == NULL) {
1798 memprintf(err, "memory allocation error");
1799 return 0;
1800 }
1801
1802 dec_size = base64dec(arg->data.str.area, arg->data.str.data, dec, dec_size);
1803 if (dec_size < 0) {
1804 memprintf(err, "argument needs to be base64 encoded, and "
1805 "can either be a string or a variable");
1806 free(dec);
1807 return 0;
1808 }
1809
1810 /* base64 decoded */
1811 chunk_destroy(&arg->data.str);
1812 arg->data.str.area = dec;
1813 arg->data.str.data = dec_size;
1814 return 1;
1815}
1816
Ilya Shipitsin2c481d02021-03-26 23:35:31 +05001817#ifdef EVP_CIPH_GCM_MODE
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001818static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
1819 const char *file, int line, char **err)
1820{
1821 switch(args[0].data.sint) {
1822 case 128:
1823 case 192:
1824 case 256:
1825 break;
1826 default:
1827 memprintf(err, "key size must be 128, 192 or 256 (bits).");
1828 return 0;
1829 }
Dragan Dosen9e8db132021-02-22 10:03:53 +01001830
1831 /* Try to decode variables. */
1832 if (!sample_check_arg_base64(&args[1], err)) {
1833 memprintf(err, "failed to parse nonce : %s", *err);
1834 return 0;
1835 }
1836 if (!sample_check_arg_base64(&args[2], err)) {
1837 memprintf(err, "failed to parse key : %s", *err);
1838 return 0;
1839 }
1840 if (!sample_check_arg_base64(&args[3], err)) {
1841 memprintf(err, "failed to parse aead_tag : %s", *err);
1842 return 0;
1843 }
1844
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001845 return 1;
1846}
1847
1848/* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
1849static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
1850{
1851 struct sample nonce, key, aead_tag;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001852 struct buffer *smp_trash = NULL, *smp_trash_alloc = NULL;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001853 EVP_CIPHER_CTX *ctx;
1854 int dec_size, ret;
1855
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001856 smp_trash_alloc = alloc_trash_chunk();
1857 if (!smp_trash_alloc)
1858 return 0;
1859
Dragan Dosen9e8db132021-02-22 10:03:53 +01001860 /* smp copy */
1861 smp_trash_alloc->data = smp->data.u.str.data;
1862 if (unlikely(smp_trash_alloc->data > smp_trash_alloc->size))
1863 smp_trash_alloc->data = smp_trash_alloc->size;
1864 memcpy(smp_trash_alloc->area, smp->data.u.str.area, smp_trash_alloc->data);
1865
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001866 ctx = EVP_CIPHER_CTX_new();
1867
1868 if (!ctx)
1869 goto err;
1870
Dragan Dosen9e8db132021-02-22 10:03:53 +01001871 smp_trash = alloc_trash_chunk();
1872 if (!smp_trash)
1873 goto err;
1874
1875 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
1876 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001877 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001878
1879 if (arg_p[1].type == ARGT_VAR) {
1880 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
1881 if (dec_size < 0)
1882 goto err;
1883 smp_trash->data = dec_size;
1884 nonce.data.u.str = *smp_trash;
1885 }
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001886
1887 /* Set cipher type and mode */
1888 switch(arg_p[0].data.sint) {
1889 case 128:
1890 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
1891 break;
1892 case 192:
1893 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
1894 break;
1895 case 256:
1896 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
1897 break;
1898 }
1899
Dragan Dosen9e8db132021-02-22 10:03:53 +01001900 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, nonce.data.u.str.data, NULL);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001901
1902 /* Initialise IV */
Dragan Dosen9e8db132021-02-22 10:03:53 +01001903 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) nonce.data.u.str.area))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001904 goto err;
1905
Dragan Dosen9e8db132021-02-22 10:03:53 +01001906 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
1907 if (!sample_conv_var2smp_str(&arg_p[2], &key))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001908 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001909
1910 if (arg_p[2].type == ARGT_VAR) {
1911 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
1912 if (dec_size < 0)
1913 goto err;
1914 smp_trash->data = dec_size;
1915 key.data.u.str = *smp_trash;
1916 }
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001917
1918 /* Initialise key */
Dragan Dosen9e8db132021-02-22 10:03:53 +01001919 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) key.data.u.str.area, NULL))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001920 goto err;
1921
1922 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
Dragan Dosen9e8db132021-02-22 10:03:53 +01001923 (unsigned char *) smp_trash_alloc->area, (int) smp_trash_alloc->data))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001924 goto err;
1925
Dragan Dosen9e8db132021-02-22 10:03:53 +01001926 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
1927 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001928 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001929
1930 if (arg_p[3].type == ARGT_VAR) {
1931 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
1932 if (dec_size < 0)
1933 goto err;
1934 smp_trash_alloc->data = dec_size;
1935 aead_tag.data.u.str = *smp_trash_alloc;
1936 }
1937
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001938 dec_size = smp_trash->data;
1939
Dragan Dosen9e8db132021-02-22 10:03:53 +01001940 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, aead_tag.data.u.str.data, (void *) aead_tag.data.u.str.area);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001941 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
1942
1943 if (ret <= 0)
1944 goto err;
1945
1946 smp->data.u.str.data = dec_size + smp_trash->data;
1947 smp->data.u.str.area = smp_trash->area;
1948 smp->data.type = SMP_T_BIN;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001949 smp_dup(smp);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001950 free_trash_chunk(smp_trash_alloc);
Dragan Dosen9e8db132021-02-22 10:03:53 +01001951 free_trash_chunk(smp_trash);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001952 return 1;
1953
1954err:
1955 free_trash_chunk(smp_trash_alloc);
Dragan Dosen9e8db132021-02-22 10:03:53 +01001956 free_trash_chunk(smp_trash);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001957 return 0;
1958}
Ilya Shipitsin2c481d02021-03-26 23:35:31 +05001959#endif
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001960
Patrick Gansterer8e366512020-04-22 16:47:57 +02001961static int check_crypto_digest(struct arg *args, struct sample_conv *conv,
1962 const char *file, int line, char **err)
1963{
1964 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1965
1966 if (evp)
1967 return 1;
1968
1969 memprintf(err, "algorithm must be a valid OpenSSL message digest name.");
1970 return 0;
1971}
1972
1973static int sample_conv_crypto_digest(const struct arg *args, struct sample *smp, void *private)
1974{
1975 struct buffer *trash = get_trash_chunk();
1976 unsigned char *md = (unsigned char*) trash->area;
1977 unsigned int md_len = trash->size;
1978 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
1979 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1980
1981 if (!ctx)
1982 return 0;
1983
1984 if (!EVP_DigestInit_ex(ctx, evp, NULL) ||
1985 !EVP_DigestUpdate(ctx, smp->data.u.str.area, smp->data.u.str.data) ||
1986 !EVP_DigestFinal_ex(ctx, md, &md_len)) {
1987 EVP_MD_CTX_free(ctx);
1988 return 0;
1989 }
1990
1991 EVP_MD_CTX_free(ctx);
1992
1993 trash->data = md_len;
1994 smp->data.u.str = *trash;
1995 smp->data.type = SMP_T_BIN;
1996 smp->flags &= ~SMP_F_CONST;
1997 return 1;
1998}
1999
2000static int check_crypto_hmac(struct arg *args, struct sample_conv *conv,
2001 const char *file, int line, char **err)
2002{
2003 if (!check_crypto_digest(args, conv, file, line, err))
2004 return 0;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002005
2006 if (!sample_check_arg_base64(&args[1], err)) {
2007 memprintf(err, "failed to parse key : %s", *err);
2008 return 0;
2009 }
Patrick Gansterer8e366512020-04-22 16:47:57 +02002010
Patrick Gansterer8e366512020-04-22 16:47:57 +02002011 return 1;
2012}
2013
2014static int sample_conv_crypto_hmac(const struct arg *args, struct sample *smp, void *private)
2015{
2016 struct sample key;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002017 struct buffer *trash = NULL, *key_trash = NULL;
Patrick Gansterer8e366512020-04-22 16:47:57 +02002018 unsigned char *md;
2019 unsigned int md_len;
2020 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
2021 int dec_size;
2022
2023 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
2024 if (!sample_conv_var2smp_str(&args[1], &key))
2025 return 0;
2026
Dragan Dosen9e8db132021-02-22 10:03:53 +01002027 if (args[1].type == ARGT_VAR) {
2028 key_trash = alloc_trash_chunk();
2029 if (!key_trash)
2030 goto err;
2031
2032 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, key_trash->area, key_trash->size);
2033 if (dec_size < 0)
2034 goto err;
2035 key_trash->data = dec_size;
2036 key.data.u.str = *key_trash;
2037 }
Patrick Gansterer8e366512020-04-22 16:47:57 +02002038
Dragan Dosen9e8db132021-02-22 10:03:53 +01002039 trash = alloc_trash_chunk();
2040 if (!trash)
Patrick Gansterer8e366512020-04-22 16:47:57 +02002041 goto err;
2042
2043 md = (unsigned char*) trash->area;
2044 md_len = trash->size;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002045 if (!HMAC(evp, key.data.u.str.area, key.data.u.str.data, (const unsigned char*) smp->data.u.str.area,
2046 smp->data.u.str.data, md, &md_len))
Patrick Gansterer8e366512020-04-22 16:47:57 +02002047 goto err;
2048
2049 free_trash_chunk(key_trash);
2050
2051 trash->data = md_len;
2052 smp->data.u.str = *trash;
2053 smp->data.type = SMP_T_BIN;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002054 smp_dup(smp);
2055 free_trash_chunk(trash);
Patrick Gansterer8e366512020-04-22 16:47:57 +02002056 return 1;
2057
2058err:
2059 free_trash_chunk(key_trash);
Dragan Dosen9e8db132021-02-22 10:03:53 +01002060 free_trash_chunk(trash);
Patrick Gansterer8e366512020-04-22 16:47:57 +02002061 return 0;
2062}
2063
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02002064#endif /* USE_OPENSSL */
Tim Duesterhusd4376302019-06-17 12:41:44 +02002065
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002066static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002067{
Willy Tarreau83061a82018-07-13 11:56:34 +02002068 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002069 unsigned char c;
2070 int ptr = 0;
2071
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002072 trash->data = 0;
2073 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
2074 c = smp->data.u.str.area[ptr++];
2075 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
2076 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002077 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002078 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002079 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002080 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002081 return 1;
2082}
2083
Dragan Dosen3f957b22017-10-24 09:27:34 +02002084static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
2085{
2086 long long int n = 0;
2087 int i, c;
2088
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002089 for (i = 0; i < smp->data.u.str.data; i++) {
2090 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02002091 return 0;
2092 n = (n << 4) + c;
2093 }
2094
2095 smp->data.u.sint = n;
2096 smp->data.type = SMP_T_SINT;
2097 smp->flags &= ~SMP_F_CONST;
2098 return 1;
2099}
2100
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002101/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002102static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002103{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002104 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
2105 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002106 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002107 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002108 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002109 return 1;
2110}
2111
Willy Tarreau60a2ee72017-12-15 07:13:48 +01002112static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002113{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002114 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002115 smp->data.u.sint = i;
2116 smp->data.type = SMP_T_SINT;
2117 return 1;
2118}
2119
2120
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002121static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002122{
2123 int i;
2124
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002125 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002126 return 0;
2127
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002128 for (i = 0; i < smp->data.u.str.data; i++) {
2129 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
2130 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01002131 }
2132 return 1;
2133}
2134
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002135static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002136{
2137 int i;
2138
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002139 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02002140 return 0;
2141
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002142 for (i = 0; i < smp->data.u.str.data; i++) {
2143 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
2144 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01002145 }
2146 return 1;
2147}
2148
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002149/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
2150static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002151{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002152 /* Attempt to convert to IPv4 to apply the correct mask. */
2153 c_ipv62ip(smp);
2154
2155 if (smp->data.type == SMP_T_IPV4) {
2156 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
2157 smp->data.type = SMP_T_IPV4;
2158 }
2159 else if (smp->data.type == SMP_T_IPV6) {
2160 /* IPv6 cannot be converted without an IPv6 mask. */
2161 if (args[1].type != ARGT_IPV6)
2162 return 0;
2163
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +01002164 write_u64(&smp->data.u.ipv6.s6_addr[0],
2165 read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0]));
2166 write_u64(&smp->data.u.ipv6.s6_addr[8],
2167 read_u64(&smp->data.u.ipv6.s6_addr[8]) & read_u64(&args[1].data.ipv6.s6_addr[8]));
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002168 smp->data.type = SMP_T_IPV6;
2169 }
2170
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002171 return 1;
2172}
2173
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002174/* takes an UINT value on input supposed to represent the time since EPOCH,
2175 * adds an optional offset found in args[1] and emits a string representing
2176 * the local time in the format specified in args[1] using strftime().
2177 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002178static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002179{
Willy Tarreau83061a82018-07-13 11:56:34 +02002180 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002181 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002182 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002183 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002184
2185 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002186 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002187 curr_date += args[1].data.sint;
2188
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002189 tm = localtime(&curr_date);
2190 if (!tm)
2191 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002192 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002193 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
2194 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002195 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002196 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002197 return 1;
2198}
2199
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002200/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002201static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002202{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002203 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
2204 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002205 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002206 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002207 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002208 return 1;
2209}
2210
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002211/* takes an UINT value on input supposed to represent the time since EPOCH,
2212 * adds an optional offset found in args[1] and emits a string representing
2213 * the UTC date in the format specified in args[1] using strftime().
2214 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002215static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002216{
Willy Tarreau83061a82018-07-13 11:56:34 +02002217 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002218 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002219 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002220 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002221
2222 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002223 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002224 curr_date += args[1].data.sint;
2225
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002226 tm = gmtime(&curr_date);
2227 if (!tm)
2228 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002229 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002230 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
2231 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002232 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002233 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002234 return 1;
2235}
2236
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002237/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002238static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002239{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002240 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
2241 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002242 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002243 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002244 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002245 return 1;
2246}
2247
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002248/* hashes the binary input into a 32-bit unsigned int using xxh.
2249 * The seed of the hash defaults to 0 but can be changd in argument 1.
2250 */
2251static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
2252{
2253 unsigned int seed;
2254
Christopher Faulete6e7a582021-01-29 11:29:28 +01002255 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002256 seed = arg_p->data.sint;
2257 else
2258 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002259 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
2260 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002261 smp->data.type = SMP_T_SINT;
2262 return 1;
2263}
2264
2265/* hashes the binary input into a 64-bit unsigned int using xxh.
2266 * In fact, the function returns a 64 bit unsigned, but the sample
2267 * storage of haproxy only proposes 64-bits signed, so the value is
2268 * cast as signed. This cast doesn't impact the hash repartition.
2269 * The seed of the hash defaults to 0 but can be changd in argument 1.
2270 */
2271static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
2272{
2273 unsigned long long int seed;
2274
Christopher Faulete6e7a582021-01-29 11:29:28 +01002275 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002276 seed = (unsigned long long int)arg_p->data.sint;
2277 else
2278 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002279 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
2280 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002281 smp->data.type = SMP_T_SINT;
2282 return 1;
2283}
2284
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002285static int sample_conv_xxh3(const struct arg *arg_p, struct sample *smp, void *private)
2286{
2287 unsigned long long int seed;
2288
Christopher Faulete6e7a582021-01-29 11:29:28 +01002289 if (arg_p->data.sint)
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002290 seed = (unsigned long long int)arg_p->data.sint;
2291 else
2292 seed = 0;
2293 smp->data.u.sint = (long long int)XXH3(smp->data.u.str.area,
2294 smp->data.u.str.data, seed);
2295 smp->data.type = SMP_T_SINT;
2296 return 1;
2297}
2298
Willy Tarreau80599772015-01-20 19:35:24 +01002299/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002300static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01002301{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002302 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
2303 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002304 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002305 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002306 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01002307 return 1;
2308}
2309
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002310/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
2311static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
2312{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002313 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
2314 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002315 if (arg_p->data.sint)
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002316 smp->data.u.sint = full_hash(smp->data.u.sint);
2317 smp->data.type = SMP_T_SINT;
2318 return 1;
2319}
2320
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002321/* This function escape special json characters. The returned string can be
2322 * safely set between two '"' and used as json string. The json string is
2323 * defined like this:
2324 *
2325 * any Unicode character except '"' or '\' or control character
2326 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
2327 *
2328 * The enum input_type contain all the allowed mode for decoding the input
2329 * string.
2330 */
2331enum input_type {
2332 IT_ASCII = 0,
2333 IT_UTF8,
2334 IT_UTF8S,
2335 IT_UTF8P,
2336 IT_UTF8PS,
2337};
William Dauchy5417e892021-01-08 21:57:41 +01002338
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002339static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
2340 const char *file, int line, char **err)
2341{
Christopher Faulet95917132020-08-05 23:07:07 +02002342 enum input_type type;
2343
Christopher Faulet95917132020-08-05 23:07:07 +02002344 if (strcmp(arg->data.str.area, "") == 0)
2345 type = IT_ASCII;
2346 else if (strcmp(arg->data.str.area, "ascii") == 0)
2347 type = IT_ASCII;
2348 else if (strcmp(arg->data.str.area, "utf8") == 0)
2349 type = IT_UTF8;
2350 else if (strcmp(arg->data.str.area, "utf8s") == 0)
2351 type = IT_UTF8S;
2352 else if (strcmp(arg->data.str.area, "utf8p") == 0)
2353 type = IT_UTF8P;
2354 else if (strcmp(arg->data.str.area, "utf8ps") == 0)
2355 type = IT_UTF8PS;
2356 else {
2357 memprintf(err, "Unexpected input code type. "
2358 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
2359 return 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002360 }
2361
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002362 chunk_destroy(&arg->data.str);
Christopher Faulet95917132020-08-05 23:07:07 +02002363 arg->type = ARGT_SINT;
2364 arg->data.sint = type;
2365 return 1;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002366}
2367
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002368static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002369{
Willy Tarreau83061a82018-07-13 11:56:34 +02002370 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002371 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
2372 const char *str;
2373 int len;
2374 enum input_type input_type = IT_ASCII;
2375 unsigned int c;
2376 unsigned int ret;
2377 char *p;
2378
Christopher Faulete6e7a582021-01-29 11:29:28 +01002379 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002380
2381 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002382 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002383
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002384 p = smp->data.u.str.area;
2385 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002386
2387 if (input_type == IT_ASCII) {
2388 /* Read input as ASCII. */
2389 c = *(unsigned char *)p;
2390 p++;
2391 }
2392 else {
2393 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002394 ret = utf8_next(p,
2395 smp->data.u.str.data - ( p - smp->data.u.str.area),
2396 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002397 p += utf8_return_length(ret);
2398
2399 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2400 return 0;
2401 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2402 continue;
2403 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2404 return 0;
2405 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2406 continue;
2407
2408 /* Check too big values. */
2409 if ((unsigned int)c > 0xffff) {
2410 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2411 return 0;
2412 continue;
2413 }
2414 }
2415
2416 /* Convert character. */
2417 if (c == '"') {
2418 len = 2;
2419 str = "\\\"";
2420 }
2421 else if (c == '\\') {
2422 len = 2;
2423 str = "\\\\";
2424 }
2425 else if (c == '/') {
2426 len = 2;
2427 str = "\\/";
2428 }
2429 else if (c == '\b') {
2430 len = 2;
2431 str = "\\b";
2432 }
2433 else if (c == '\f') {
2434 len = 2;
2435 str = "\\f";
2436 }
2437 else if (c == '\r') {
2438 len = 2;
2439 str = "\\r";
2440 }
2441 else if (c == '\n') {
2442 len = 2;
2443 str = "\\n";
2444 }
2445 else if (c == '\t') {
2446 len = 2;
2447 str = "\\t";
2448 }
Willy Tarreau90807112020-02-25 08:16:33 +01002449 else if (c > 0xff || !isprint((unsigned char)c)) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002450 /* isprint generate a segfault if c is too big. The man says that
2451 * c must have the value of an unsigned char or EOF.
2452 */
2453 len = 6;
2454 _str[0] = '\\';
2455 _str[1] = 'u';
2456 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2457 str = _str;
2458 }
2459 else {
2460 len = 1;
Willy Tarreau5715da22020-02-25 08:37:37 +01002461 _str[0] = c;
2462 str = _str;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002463 }
2464
2465 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002466 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002467 return 0;
2468
2469 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002470 memcpy(temp->area + temp->data, str, len);
2471 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002472 }
2473
2474 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002475 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002476 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002477
2478 return 1;
2479}
2480
Emeric Brun54c4ac82014-11-03 15:32:43 +01002481/* This sample function is designed to extract some bytes from an input buffer.
2482 * First arg is the offset.
2483 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002484static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002485{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002486 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2487 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002488 return 1;
2489 }
2490
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002491 if (smp->data.u.str.size)
2492 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002493 smp->data.u.str.data -= arg_p[0].data.sint;
2494 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002495
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002496 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2497 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002498
2499 return 1;
2500}
2501
Emeric Brunf399b0d2014-11-03 17:07:03 +01002502static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2503 const char *file, int line, char **err)
2504{
2505 struct arg *arg = args;
2506
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002507 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002508 memprintf(err, "Unexpected arg type");
2509 return 0;
2510 }
2511
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002512 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002513 memprintf(err, "Unexpected value 0 for index");
2514 return 0;
2515 }
2516
2517 arg++;
2518
2519 if (arg->type != ARGT_STR) {
2520 memprintf(err, "Unexpected arg type");
2521 return 0;
2522 }
2523
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002524 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002525 memprintf(err, "Empty separators list");
2526 return 0;
2527 }
2528
2529 return 1;
2530}
2531
2532/* This sample function is designed to a return selected part of a string (field).
2533 * First arg is the index of the field (start at 1)
2534 * Second arg is a char list of separators (type string)
2535 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002536static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002537{
Marcin Deranek9631a282018-04-16 14:30:46 +02002538 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002539 char *start, *end;
2540 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002541 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002542
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002543 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002544 return 0;
2545
Marcin Deranek9631a282018-04-16 14:30:46 +02002546 if (arg_p[0].data.sint < 0) {
2547 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002548 end = start = smp->data.u.str.area + smp->data.u.str.data;
2549 while (start > smp->data.u.str.area) {
2550 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2551 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002552 if (field == arg_p[0].data.sint) {
2553 if (count == 1)
2554 goto found;
2555 else if (count > 1)
2556 count--;
2557 } else {
2558 end = start-1;
2559 field--;
2560 }
2561 break;
2562 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002563 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002564 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002565 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002566 } else {
2567 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002568 end = start = smp->data.u.str.area;
2569 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2570 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2571 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002572 if (field == arg_p[0].data.sint) {
2573 if (count == 1)
2574 goto found;
2575 else if (count > 1)
2576 count--;
2577 } else {
2578 start = end+1;
2579 field++;
2580 }
2581 break;
2582 }
2583 }
2584 end++;
2585 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002586 }
2587
2588 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002589 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002590 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002591 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002592 }
2593found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002594 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002595 /* If ret string is len 0, no need to
2596 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002597 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002598 return 1;
2599
Emeric Brunf399b0d2014-11-03 17:07:03 +01002600 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002601 Note: smp->data.u.str.size cannot be set to 0 */
2602 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002603 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002604
Thayne McCombsb2843052021-04-11 23:26:59 -06002605 smp->data.u.str.area = start;
2606
Emeric Brunf399b0d2014-11-03 17:07:03 +01002607 return 1;
2608}
2609
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002610/* This sample function is designed to return a word from a string.
2611 * First arg is the index of the word (start at 1)
2612 * Second arg is a char list of words separators (type string)
2613 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002614static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002615{
Marcin Deranek9631a282018-04-16 14:30:46 +02002616 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002617 char *start, *end;
2618 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002619 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002620
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002621 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002622 return 0;
2623
2624 word = 0;
2625 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002626 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002627 end = start = smp->data.u.str.area + smp->data.u.str.data;
2628 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002629 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002630 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2631 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002632 issep = 1;
2633 break;
2634 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002635 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002636 if (!inword) {
2637 if (!issep) {
2638 if (word != arg_p[0].data.sint) {
2639 word--;
2640 end = start;
2641 }
2642 inword = 1;
2643 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002644 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002645 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002646 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002647 if (count == 1)
2648 goto found;
2649 else if (count > 1)
2650 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002651 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002652 inword = 0;
2653 }
2654 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002655 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002656 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002657 end = start = smp->data.u.str.area;
2658 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002659 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002660 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2661 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002662 issep = 1;
2663 break;
2664 }
2665 }
2666 if (!inword) {
2667 if (!issep) {
2668 if (word != arg_p[0].data.sint) {
2669 word++;
2670 start = end;
2671 }
2672 inword = 1;
2673 }
2674 }
2675 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002676 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002677 if (count == 1)
2678 goto found;
2679 else if (count > 1)
2680 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002681 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002682 inword = 0;
2683 }
2684 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002685 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002686 }
2687
2688 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002689 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002690 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002691 return 1;
2692 }
2693found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002694 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002695 /* If ret string is len 0, no need to
2696 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002697 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002698 return 1;
2699
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002700 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002701
2702 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002703 Note: smp->data.u.str.size cannot be set to 0 */
2704 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002705 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002706
2707 return 1;
2708}
2709
Willy Tarreau7eda8492015-01-20 19:47:06 +01002710static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2711 const char *file, int line, char **err)
2712{
2713 struct arg *arg = args;
2714 char *p;
2715 int len;
2716
2717 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2718 arg[0].type_flags = 0;
2719
2720 if (arg[2].type != ARGT_STR)
2721 return 1;
2722
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002723 p = arg[2].data.str.area;
2724 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002725 while (len) {
2726 if (*p == 'i') {
2727 arg[0].type_flags |= ARGF_REG_ICASE;
2728 }
2729 else if (*p == 'g') {
2730 arg[0].type_flags |= ARGF_REG_GLOB;
2731 }
2732 else {
2733 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2734 return 0;
2735 }
2736 p++;
2737 len--;
2738 }
2739 return 1;
2740}
2741
2742/* This sample function is designed to do the equivalent of s/match/replace/ on
2743 * the input string. It applies a regex and restarts from the last matched
2744 * location until nothing matches anymore. First arg is the regex to apply to
2745 * the input string, second arg is the replacement expression.
2746 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002747static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002748{
2749 char *start, *end;
2750 struct my_regex *reg = arg_p[0].data.reg;
2751 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002752 struct buffer *trash = get_trash_chunk();
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002753 struct buffer *output;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002754 int flag, max;
2755 int found;
2756
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002757 start = smp->data.u.str.area;
2758 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002759
2760 flag = 0;
2761 while (1) {
2762 /* check for last round which is used to copy remaining parts
2763 * when not running in global replacement mode.
2764 */
2765 found = 0;
2766 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2767 /* Note: we can have start == end on empty strings or at the end */
2768 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2769 }
2770
2771 if (!found)
2772 pmatch[0].rm_so = end - start;
2773
2774 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002775 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002776 if (max && pmatch[0].rm_so > 0) {
2777 if (max > pmatch[0].rm_so)
2778 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002779 memcpy(trash->area + trash->data, start, max);
2780 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002781 }
2782
2783 if (!found)
2784 break;
2785
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002786 output = alloc_trash_chunk();
Willy Tarreau23997da2020-02-18 14:27:44 +01002787 if (!output)
2788 break;
2789
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002790 output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2791
Willy Tarreau7eda8492015-01-20 19:47:06 +01002792 /* replace the matching part */
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002793 max = output->size - output->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002794 if (max) {
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002795 if (max > output->data)
2796 max = output->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002797 memcpy(trash->area + trash->data,
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002798 output->area, max);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002799 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002800 }
2801
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002802 free_trash_chunk(output);
2803
Willy Tarreau7eda8492015-01-20 19:47:06 +01002804 /* stop here if we're done with this string */
2805 if (start >= end)
2806 break;
2807
2808 /* We have a special case for matches of length 0 (eg: "x*y*").
2809 * These ones are considered to match in front of a character,
2810 * so we have to copy that character and skip to the next one.
2811 */
2812 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002813 if (trash->data < trash->size)
2814 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002815 pmatch[0].rm_eo++;
2816 }
2817
2818 start += pmatch[0].rm_eo;
2819 flag |= REG_NOTBOL;
2820 }
2821
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002822 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002823 return 1;
2824}
2825
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002826/* This function check an operator entry. It expects a string.
2827 * The string can be an integer or a variable name.
2828 */
2829static int check_operator(struct arg *args, struct sample_conv *conv,
2830 const char *file, int line, char **err)
2831{
2832 const char *str;
2833 const char *end;
Christopher Faulet95917132020-08-05 23:07:07 +02002834 long long int i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002835
2836 /* Try to decode a variable. */
2837 if (vars_check_arg(&args[0], NULL))
2838 return 1;
2839
2840 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002841 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002842 end = str + strlen(str);
Christopher Faulet95917132020-08-05 23:07:07 +02002843 i = read_int64(&str, end);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002844 if (*str != '\0') {
2845 memprintf(err, "expects an integer or a variable name");
2846 return 0;
2847 }
Christopher Faulet95917132020-08-05 23:07:07 +02002848
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002849 chunk_destroy(&args[0].data.str);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002850 args[0].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02002851 args[0].data.sint = i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002852 return 1;
2853}
2854
Willy Tarreau6204cd92016-03-10 16:33:04 +01002855/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002856 * If the arg contain an integer, the integer is returned in the
2857 * sample. If the arg contains a variable descriptor, it returns the
2858 * variable value.
2859 *
2860 * This function returns 0 if an error occurs, otherwise it returns 1.
2861 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002862static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002863{
2864 switch (arg->type) {
2865 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002866 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002867 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002868 return 1;
2869 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002870 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002871 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002872 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002873 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002874 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002875 return 0;
2876 return 1;
2877 default:
2878 return 0;
2879 }
2880}
2881
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002882/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002883 * result.
2884 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002885static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002886{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002887 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002888 return 1;
2889}
2890
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002891/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002892 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002893 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002894static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002895{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002896 struct sample tmp;
2897
Willy Tarreau7560dd42016-03-10 16:28:58 +01002898 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002899 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002900 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002901 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002902 return 1;
2903}
2904
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002905/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002906 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002907 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002908static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002909{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002910 struct sample tmp;
2911
Willy Tarreau7560dd42016-03-10 16:28:58 +01002912 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002913 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002914 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002915 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002916 return 1;
2917}
2918
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002919/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002920 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002921 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002922static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002923{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002924 struct sample tmp;
2925
Willy Tarreau7560dd42016-03-10 16:28:58 +01002926 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002927 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002928 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002929 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002930 return 1;
2931}
2932
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002933static inline long long int arith_add(long long int a, long long int b)
2934{
2935 /* Prevent overflow and makes capped calculus.
2936 * We must ensure that the check calculus doesn't
2937 * exceed the signed 64 bits limits.
2938 *
2939 * +----------+----------+
2940 * | a<0 | a>=0 |
2941 * +------+----------+----------+
2942 * | b<0 | MIN-a>b | no check |
2943 * +------+----------+----------+
2944 * | b>=0 | no check | MAX-a<b |
2945 * +------+----------+----------+
2946 */
2947 if ((a ^ b) >= 0) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002948 /* signs are different. */
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002949 if (a < 0) {
2950 if (LLONG_MIN - a > b)
2951 return LLONG_MIN;
2952 }
2953 if (LLONG_MAX - a < b)
2954 return LLONG_MAX;
2955 }
2956 return a + b;
2957}
2958
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002959/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002960 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002961 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002962static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002963{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002964 struct sample tmp;
2965
Willy Tarreau7560dd42016-03-10 16:28:58 +01002966 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002967 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002968 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002969 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002970 return 1;
2971}
2972
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002973/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002974 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002975 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002976static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002977 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002978{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002979 struct sample tmp;
2980
Willy Tarreau7560dd42016-03-10 16:28:58 +01002981 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002982 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002983 return 0;
2984
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002985 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2986 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2987 * of -LLONG_MIN and correct the result.
2988 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002989 if (tmp.data.u.sint == LLONG_MIN) {
2990 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2991 if (smp->data.u.sint < LLONG_MAX)
2992 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002993 return 1;
2994 }
2995
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002996 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002997 * the second operand.
2998 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002999 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01003000 return 1;
3001}
3002
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003003/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003004 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003005 * If the result makes an overflow, then the largest possible quantity is
3006 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003007 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003008static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003009 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003010{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003011 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003012 long long int c;
3013
Willy Tarreau7560dd42016-03-10 16:28:58 +01003014 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01003015 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003016 return 0;
3017
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003018 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003019 if (!smp->data.u.sint || !tmp.data.u.sint) {
3020 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003021 return 1;
3022 }
3023
3024 /* The multiply between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003025 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003026 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003027 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3028 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003029 return 1;
3030 }
3031
3032 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003033 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003034
3035 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003036 if (smp->data.u.sint != c / tmp.data.u.sint) {
3037 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
3038 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003039 return 1;
3040 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003041 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003042 return 1;
3043 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003044 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01003045 return 1;
3046}
3047
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003048/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003049 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003050 * If arg_p makes the result overflow, then the largest possible quantity is
3051 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003052 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003053static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003054 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003055{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003056 struct sample tmp;
3057
Willy Tarreau7560dd42016-03-10 16:28:58 +01003058 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01003059 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003060 return 0;
3061
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003062 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003063 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003064 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003065 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003066 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3067 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003068 return 1;
3069 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003070 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003071 return 1;
3072 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003073 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01003074 return 1;
3075}
3076
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003077/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003078 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003079 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003080 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003081static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003082 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003083{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003084 struct sample tmp;
3085
Willy Tarreau7560dd42016-03-10 16:28:58 +01003086 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01003087 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003088 return 0;
3089
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003090 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003091 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003092 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003093 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003094 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3095 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003096 return 1;
3097 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003098 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003099 return 1;
3100 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003101 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01003102 return 1;
3103}
3104
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003105/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01003106 * result.
3107 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003108static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003109 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003110{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003111 if (smp->data.u.sint == LLONG_MIN)
3112 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003113 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003114 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003115 return 1;
3116}
3117
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003118/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003119 * false. The output is a BOOL.
3120 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003121static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003122 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003123{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003124 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003125 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003126 return 1;
3127}
3128
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003129/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003130 * truee. The output is a BOOL.
3131 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003132static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003133 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003134{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003135 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003136 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003137 return 1;
3138}
3139
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003140/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003141 * The output is a BOOL.
3142 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003143static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003144 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003145{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003146 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003147 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003148 return 1;
3149}
3150
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003151/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003152 * The output is a BOOL.
3153 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003154static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003155 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003156{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003157 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003158 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003159 return 1;
3160}
3161
Willy Tarreau280f42b2018-02-19 15:34:12 +01003162/* appends an optional const string, an optional variable contents and another
3163 * optional const string to an existing string.
3164 */
3165static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
3166{
Willy Tarreau83061a82018-07-13 11:56:34 +02003167 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003168 struct sample tmp;
3169 int max;
3170
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003171 trash = alloc_trash_chunk();
William Dauchye9970102021-01-11 11:05:58 +01003172 if (!trash)
3173 return 0;
3174
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003175 trash->data = smp->data.u.str.data;
3176 if (trash->data > trash->size - 1)
3177 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003178
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003179 memcpy(trash->area, smp->data.u.str.area, trash->data);
3180 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003181
3182 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003183 max = arg_p[0].data.str.data;
3184 if (max > trash->size - 1 - trash->data)
3185 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003186
3187 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003188 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
3189 trash->data += max;
3190 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003191 }
3192
3193 /* append second string (variable) if it's found and we can turn it
3194 * into a string.
3195 */
3196 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3197 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
3198 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
3199 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
3200
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003201 max = tmp.data.u.str.data;
3202 if (max > trash->size - 1 - trash->data)
3203 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003204
3205 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003206 memcpy(trash->area + trash->data, tmp.data.u.str.area,
3207 max);
3208 trash->data += max;
3209 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003210 }
3211 }
3212
3213 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003214 max = arg_p[2].data.str.data;
3215 if (max > trash->size - 1 - trash->data)
3216 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003217
3218 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003219 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
3220 trash->data += max;
3221 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003222 }
3223
3224 smp->data.u.str = *trash;
3225 smp->data.type = SMP_T_STR;
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003226 smp_dup(smp);
3227 free_trash_chunk(trash);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003228 return 1;
3229}
3230
3231/* This function checks the "concat" converter's arguments and extracts the
3232 * variable name and its scope.
3233 */
3234static int smp_check_concat(struct arg *args, struct sample_conv *conv,
3235 const char *file, int line, char **err)
3236{
3237 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003238 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
3239 memprintf(err, "failed to register variable name '%s'",
3240 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003241 return 0;
3242 }
3243 return 1;
3244}
3245
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003246/* Compares string with a variable containing a string. Return value
Tim Duesterhusca097c12018-04-27 21:18:45 +02003247 * is compatible with strcmp(3)'s return value.
3248 */
3249static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
3250{
3251 struct sample tmp;
3252 int max, result;
3253
3254 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3255 if (arg_p[0].type != ARGT_VAR)
3256 return 0;
3257 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
3258 return 0;
3259 if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp))
3260 return 0;
3261
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003262 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
3263 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003264 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003265 if (smp->data.u.str.data != tmp.data.u.str.data) {
3266 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02003267 result = -1;
3268 }
3269 else {
3270 result = 1;
3271 }
3272 }
3273 }
3274
3275 smp->data.u.sint = result;
3276 smp->data.type = SMP_T_SINT;
3277 return 1;
3278}
3279
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003280#ifdef USE_OPENSSL
3281/* Compares bytestring with a variable containing a bytestring. Return value
3282 * is `true` if both bytestrings are bytewise identical and `false` otherwise.
3283 *
3284 * Comparison will be performed in constant time if both bytestrings are of
3285 * the same length. If the lengths differ execution time will not be constant.
3286 */
3287static int sample_conv_secure_memcmp(const struct arg *arg_p, struct sample *smp, void *private)
3288{
3289 struct sample tmp;
3290 int result;
3291
3292 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3293 if (arg_p[0].type != ARGT_VAR)
3294 return 0;
3295 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
3296 return 0;
3297 if (!sample_casts[tmp.data.type][SMP_T_BIN](&tmp))
3298 return 0;
3299
3300 if (smp->data.u.str.data != tmp.data.u.str.data) {
3301 smp->data.u.sint = 0;
3302 smp->data.type = SMP_T_BOOL;
3303 return 1;
3304 }
3305
3306 /* The following comparison is performed in constant time. */
3307 result = CRYPTO_memcmp(smp->data.u.str.area, tmp.data.u.str.area, smp->data.u.str.data);
3308
3309 smp->data.u.sint = result == 0;
3310 smp->data.type = SMP_T_BOOL;
3311 return 1;
3312}
3313#endif
3314
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02003315/* Takes a boolean as input. Returns the first argument if that boolean is true and
3316 * the second argument otherwise.
3317 */
3318static int sample_conv_iif(const struct arg *arg_p, struct sample *smp, void *private)
3319{
3320 smp->data.type = SMP_T_STR;
3321 smp->flags |= SMP_F_CONST;
3322
3323 if (smp->data.u.sint) {
3324 smp->data.u.str.data = arg_p[0].data.str.data;
3325 smp->data.u.str.area = arg_p[0].data.str.area;
3326 }
3327 else {
3328 smp->data.u.str.data = arg_p[1].data.str.data;
3329 smp->data.u.str.area = arg_p[1].data.str.area;
3330 }
3331
3332 return 1;
3333}
3334
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003335#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
3336#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
3337#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
3338
3339/*
3340 * Extract the field value of an input binary sample. Takes a mandatory argument:
3341 * the protocol buffers field identifier (dotted notation) internally represented
3342 * as an array of unsigned integers and its size.
3343 * Return 1 if the field was found, 0 if not.
3344 */
3345static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
3346{
3347 unsigned char *pos;
3348 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003349
3350 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003351 grpc_left = smp->data.u.str.data;
3352
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01003353 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003354 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003355
3356 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
3357
3358 pos += GRPC_MSG_HEADER_SZ;
3359 grpc_left -= GRPC_MSG_HEADER_SZ;
3360
3361 if (grpc_left < left)
3362 return 0;
3363
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01003364 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
3365 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003366
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003367 grpc_left -= grpc_msg_len;
3368 }
3369
3370 return 0;
3371}
3372
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003373static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
3374{
3375 unsigned char *pos;
3376 size_t left;
3377
3378 pos = (unsigned char *)smp->data.u.str.area;
3379 left = smp->data.u.str.data;
3380
3381 return protobuf_field_lookup(arg_p, smp, &pos, &left);
3382}
3383
3384static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
3385 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003386{
3387 if (!args[1].type) {
3388 args[1].type = ARGT_SINT;
3389 args[1].data.sint = PBUF_T_BINARY;
3390 }
3391 else {
3392 int pbuf_type;
3393
3394 pbuf_type = protobuf_type(args[1].data.str.area);
3395 if (pbuf_type == -1) {
3396 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
3397 return 0;
3398 }
3399
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003400 chunk_destroy(&args[1].data.str);
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003401 args[1].type = ARGT_SINT;
3402 args[1].data.sint = pbuf_type;
3403 }
3404
3405 return 1;
3406}
3407
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003408/*
3409 * Extract the tag value of an input binary sample. Takes a mandatory argument:
3410 * the FIX protocol tag identifier.
3411 * Return 1 if the tag was found, 0 if not.
3412 */
3413static int sample_conv_fix_tag_value(const struct arg *arg_p, struct sample *smp, void *private)
3414{
3415 struct ist value;
3416
3417 smp->flags &= ~SMP_F_MAY_CHANGE;
3418 value = fix_tag_value(ist2(smp->data.u.str.area, smp->data.u.str.data),
3419 arg_p[0].data.sint);
3420 if (!istlen(value)) {
3421 if (!isttest(value)) {
3422 /* value != IST_NULL, need more data */
3423 smp->flags |= SMP_F_MAY_CHANGE;
3424 }
3425 return 0;
3426 }
3427
3428 smp->data.u.str = ist2buf(value);
3429 smp->flags |= SMP_F_CONST;
3430
3431 return 1;
3432}
3433
3434/* This function checks the "fix_tag_value" converter configuration.
3435 * It expects a "known" (by HAProxy) tag name or ID.
3436 * Tag string names are converted to their ID counterpart because this is the
3437 * format they are sent over the wire.
3438 */
3439static int sample_conv_fix_value_check(struct arg *args, struct sample_conv *conv,
3440 const char *file, int line, char **err)
3441{
3442 struct ist str;
3443 unsigned int tag;
3444
3445 str = ist2(args[0].data.str.area, args[0].data.str.data);
3446 tag = fix_tagid(str);
3447 if (!tag) {
3448 memprintf(err, "Unknown FIX tag name '%s'", args[0].data.str.area);
3449 return 0;
3450 }
3451
3452 chunk_destroy(&args[0].data.str);
3453 args[0].type = ARGT_SINT;
3454 args[0].data.sint = tag;
3455
3456 return 1;
3457}
3458
3459/*
3460 * Checks that a buffer contains a valid FIX message
3461 *
3462 * Return 1 if the check could be run, 0 if not.
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003463 * The result of the analyse itself is stored in <smp> as a boolean
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003464 */
3465static int sample_conv_fix_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3466{
3467 struct ist msg;
3468
3469 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3470
3471 smp->flags &= ~SMP_F_MAY_CHANGE;
3472 switch (fix_validate_message(msg)) {
3473 case FIX_VALID_MESSAGE:
3474 smp->data.type = SMP_T_BOOL;
3475 smp->data.u.sint = 1;
3476 return 1;
3477 case FIX_NEED_MORE_DATA:
3478 smp->flags |= SMP_F_MAY_CHANGE;
3479 return 0;
3480 case FIX_INVALID_MESSAGE:
3481 smp->data.type = SMP_T_BOOL;
3482 smp->data.u.sint = 0;
3483 return 1;
3484 }
3485 return 0;
3486}
3487
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003488/*
3489 * Extract the field value of an input binary sample containing an MQTT packet.
3490 * Takes 2 mandatory arguments:
3491 * - packet type
3492 * - field name
3493 *
3494 * return 1 if the field was found, 0 if not.
3495 */
3496static int sample_conv_mqtt_field_value(const struct arg *arg_p, struct sample *smp, void *private)
3497{
3498 struct ist pkt, value;
3499 int type, fieldname_id;
3500
3501 pkt = ist2(smp->data.u.str.area, smp->data.u.str.data);
3502 type = arg_p[0].data.sint;
3503 fieldname_id = arg_p[1].data.sint;
3504
3505 smp->flags &= ~SMP_F_MAY_CHANGE;
3506 value = mqtt_field_value(pkt, type, fieldname_id);
3507 if (!istlen(value)) {
3508 if (isttest(value)) {
3509 /* value != IST_NULL, need more data */
3510 smp->flags |= SMP_F_MAY_CHANGE;
3511 }
3512 return 0;
3513 }
3514
3515 smp->data.u.str = ist2buf(value);
3516 smp->flags |= SMP_F_CONST;
3517 return 1;
3518}
3519
3520/*
3521 * this function checks the "mqtt_field_value" converter configuration.
3522 * It expects a known packet type name or ID and a field name, in this order
3523 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003524 * Args[0] will be turned into a MQTT_CPT_* value for direct matching when parsing
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003525 * a packet.
3526 */
3527static int sample_conv_mqtt_field_value_check(struct arg *args, struct sample_conv *conv,
3528 const char *file, int line, char **err)
3529{
3530 int type, fieldname_id;
3531
3532 /* check the MQTT packet type is valid */
3533 type = mqtt_typeid(ist2(args[0].data.str.area, args[0].data.str.data));
3534 if (type == MQTT_CPT_INVALID) {
3535 memprintf(err, "Unknown MQTT type '%s'", args[0].data.str.area);
3536 return 0;
3537 }
3538
3539 /* check the field name belongs to the MQTT packet type */
3540 fieldname_id = mqtt_check_type_fieldname(type, ist2(args[1].data.str.area, args[1].data.str.data));
3541 if (fieldname_id == MQTT_FN_INVALID) {
3542 memprintf(err, "Unknown MQTT field name '%s' for packet type '%s'", args[1].data.str.area,
3543 args[0].data.str.area);
3544 return 0;
3545 }
3546
3547 /* save numeric counterparts of type and field name */
3548 chunk_destroy(&args[0].data.str);
3549 chunk_destroy(&args[1].data.str);
3550 args[0].type = ARGT_SINT;
3551 args[0].data.sint = type;
3552 args[1].type = ARGT_SINT;
3553 args[1].data.sint = fieldname_id;
3554
3555 return 1;
3556}
3557
3558/*
3559 * Checks that <smp> contains a valid MQTT message
3560 *
3561 * The function returns 1 if the check was run to its end, 0 otherwise.
3562 * The result of the analyse itself is stored in <smp> as a boolean.
3563 */
3564static int sample_conv_mqtt_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3565{
3566 struct ist msg;
3567
3568 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3569
3570 smp->flags &= ~SMP_F_MAY_CHANGE;
3571 switch (mqtt_validate_message(msg, NULL)) {
3572 case FIX_VALID_MESSAGE:
3573 smp->data.type = SMP_T_BOOL;
3574 smp->data.u.sint = 1;
3575 return 1;
3576 case FIX_NEED_MORE_DATA:
3577 smp->flags |= SMP_F_MAY_CHANGE;
3578 return 0;
3579 case FIX_INVALID_MESSAGE:
3580 smp->data.type = SMP_T_BOOL;
3581 smp->data.u.sint = 0;
3582 return 1;
3583 }
3584 return 0;
3585}
3586
Tim Duesterhusca097c12018-04-27 21:18:45 +02003587/* This function checks the "strcmp" converter's arguments and extracts the
3588 * variable name and its scope.
3589 */
3590static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
3591 const char *file, int line, char **err)
3592{
3593 /* Try to decode a variable. */
3594 if (vars_check_arg(&args[0], NULL))
3595 return 1;
3596
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003597 memprintf(err, "failed to register variable name '%s'",
3598 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003599 return 0;
3600}
3601
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003602#ifdef USE_OPENSSL
3603/* This function checks the "secure_memcmp" converter's arguments and extracts the
3604 * variable name and its scope.
3605 */
3606static int smp_check_secure_memcmp(struct arg *args, struct sample_conv *conv,
3607 const char *file, int line, char **err)
3608{
3609 /* Try to decode a variable. */
3610 if (vars_check_arg(&args[0], NULL))
3611 return 1;
3612
3613 memprintf(err, "failed to register variable name '%s'",
3614 args[0].data.str.area);
3615 return 0;
3616}
3617#endif
3618
Christopher Faulet4ccc12f2020-04-01 09:08:32 +02003619/**/
3620static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private)
3621{
3622 struct buffer *tmp;
3623 uint32_t n;
3624
3625 n = htonl((uint32_t)smp->data.u.sint);
3626 tmp = get_trash_chunk();
3627
3628 memcpy(b_head(tmp), &n, 4);
3629 b_add(tmp, 4);
3630
3631 smp->data.u.str = *tmp;
3632 smp->data.type = SMP_T_BIN;
3633 return 1;
3634}
3635
Christopher Fauletea159d62020-04-01 16:21:44 +02003636/**/
3637static int sample_conv_cut_crlf(const struct arg *arg_p, struct sample *smp, void *private)
3638{
3639 char *p;
3640 size_t l;
3641
3642 p = smp->data.u.str.area;
3643 for (l = 0; l < smp->data.u.str.data; l++) {
3644 if (*(p+l) == '\r' || *(p+l) == '\n')
3645 break;
3646 }
3647 smp->data.u.str.data = l;
3648 return 1;
3649}
3650
Christopher Faulet51fc9d12020-04-01 17:24:41 +02003651/**/
3652static int sample_conv_ltrim(const struct arg *arg_p, struct sample *smp, void *private)
3653{
3654 char *delimiters, *p;
3655 size_t dlen, l;
3656
3657 delimiters = arg_p[0].data.str.area;
3658 dlen = arg_p[0].data.str.data;
3659
3660 l = smp->data.u.str.data;
3661 p = smp->data.u.str.area;
3662 while (l && memchr(delimiters, *p, dlen) != NULL) {
3663 p++;
3664 l--;
3665 }
3666
3667 smp->data.u.str.area = p;
3668 smp->data.u.str.data = l;
3669 return 1;
3670}
3671
Christopher Faulet568415a2020-04-01 17:24:47 +02003672/**/
3673static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *private)
3674{
3675 char *delimiters, *p;
3676 size_t dlen, l;
3677
3678 delimiters = arg_p[0].data.str.area;
3679 dlen = arg_p[0].data.str.data;
3680
3681 l = smp->data.u.str.data;
3682 p = smp->data.u.str.area + l - 1;
3683 while (l && memchr(delimiters, *p, dlen) != NULL) {
3684 p--;
3685 l--;
3686 }
3687
3688 smp->data.u.str.data = l;
3689 return 1;
3690}
3691
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003692/************************************************************************/
3693/* All supported sample fetch functions must be declared here */
3694/************************************************************************/
3695
3696/* force TRUE to be returned at the fetch level */
3697static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003698smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003699{
Willy Tarreau280f42b2018-02-19 15:34:12 +01003700 if (!smp_make_rw(smp))
3701 return 0;
3702
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003703 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003704 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003705 return 1;
3706}
3707
3708/* force FALSE to be returned at the fetch level */
3709static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003710smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003711{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003712 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003713 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003714 return 1;
3715}
3716
3717/* retrieve environment variable $1 as a string */
3718static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003719smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003720{
3721 char *env;
3722
Christopher Faulete6e7a582021-01-29 11:29:28 +01003723 if (args[0].type != ARGT_STR)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003724 return 0;
3725
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003726 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003727 if (!env)
3728 return 0;
3729
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003730 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003731 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003732 smp->data.u.str.area = env;
3733 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003734 return 1;
3735}
3736
Damien Claisseae6f1252019-10-30 15:57:28 +00003737/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3738 * optional string representing the unit of the result: "s" for seconds, "ms" for
3739 * milliseconds and "us" for microseconds.
3740 * Returns 0 on error and non-zero if OK.
3741 */
3742int smp_check_date_unit(struct arg *args, char **err)
3743{
3744 if (args[1].type == ARGT_STR) {
Christopher Faulet95917132020-08-05 23:07:07 +02003745 long long int unit;
3746
Damien Claisseae6f1252019-10-30 15:57:28 +00003747 if (strcmp(args[1].data.str.area, "s") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003748 unit = TIME_UNIT_S;
Damien Claisseae6f1252019-10-30 15:57:28 +00003749 }
3750 else if (strcmp(args[1].data.str.area, "ms") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003751 unit = TIME_UNIT_MS;
Damien Claisseae6f1252019-10-30 15:57:28 +00003752 }
3753 else if (strcmp(args[1].data.str.area, "us") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003754 unit = TIME_UNIT_US;
Damien Claisseae6f1252019-10-30 15:57:28 +00003755 }
3756 else {
3757 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
3758 args[1].data.str.area);
3759 return 0;
3760 }
Christopher Faulet95917132020-08-05 23:07:07 +02003761
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003762 chunk_destroy(&args[1].data.str);
Damien Claisseae6f1252019-10-30 15:57:28 +00003763 args[1].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02003764 args[1].data.sint = unit;
Damien Claisseae6f1252019-10-30 15:57:28 +00003765 }
3766 else if (args[1].type != ARGT_STOP) {
3767 memprintf(err, "Unexpected arg type");
3768 return 0;
3769 }
3770
3771 return 1;
3772}
3773
3774/* retrieve the current local date in epoch time, converts it to milliseconds
3775 * or microseconds if asked to in optional args[1] unit param, and applies an
3776 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003777 */
3778static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003779smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003780{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003781 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003782
Damien Claisseae6f1252019-10-30 15:57:28 +00003783 /* report in milliseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003784 if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003785 smp->data.u.sint *= 1000;
3786 smp->data.u.sint += date.tv_usec / 1000;
3787 }
3788 /* report in microseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003789 else if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003790 smp->data.u.sint *= 1000000;
3791 smp->data.u.sint += date.tv_usec;
3792 }
3793
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003794 /* add offset */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003795 if (args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003796 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003797
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003798 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003799 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3800 return 1;
3801}
3802
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003803/* retrieve the current microsecond part of the date */
3804static int
3805smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3806{
3807 smp->data.u.sint = date.tv_usec;
3808 smp->data.type = SMP_T_SINT;
3809 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3810 return 1;
3811}
3812
3813
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003814/* returns the hostname */
3815static int
3816smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3817{
3818 smp->data.type = SMP_T_STR;
3819 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003820 smp->data.u.str.area = hostname;
3821 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003822 return 1;
3823}
3824
Willy Tarreau0f30d262014-11-24 16:02:05 +01003825/* returns the number of processes */
3826static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003827smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003828{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003829 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003830 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003831 return 1;
3832}
3833
3834/* returns the number of the current process (between 1 and nbproc */
3835static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003836smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003837{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003838 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003839 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003840 return 1;
3841}
3842
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003843/* returns the number of the current thread (between 1 and nbthread */
3844static int
3845smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3846{
3847 smp->data.type = SMP_T_SINT;
3848 smp->data.u.sint = tid;
3849 return 1;
3850}
3851
Willy Tarreau84310e22014-02-14 11:59:04 +01003852/* generate a random 32-bit integer for whatever purpose, with an optional
3853 * range specified in argument.
3854 */
3855static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003856smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01003857{
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003858 smp->data.u.sint = ha_random32();
Willy Tarreau84310e22014-02-14 11:59:04 +01003859
3860 /* reduce if needed. Don't do a modulo, use all bits! */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003861 if (args[0].type == ARGT_SINT)
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003862 smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32;
Willy Tarreau84310e22014-02-14 11:59:04 +01003863
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003864 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01003865 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3866 return 1;
3867}
3868
Willy Tarreau0f30d262014-11-24 16:02:05 +01003869/* returns true if the current process is stopping */
3870static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003871smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003872{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003873 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003874 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003875 return 1;
3876}
3877
Willy Tarreau70fe9442018-11-22 16:07:39 +01003878/* returns the number of calls of the current stream's process_stream() */
3879static int
3880smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
3881{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003882 if (!smp->strm)
3883 return 0;
3884
Willy Tarreau70fe9442018-11-22 16:07:39 +01003885 smp->data.type = SMP_T_SINT;
3886 smp->data.u.sint = smp->strm->task->calls;
3887 return 1;
3888}
3889
3890/* returns the average number of nanoseconds spent processing the stream per call */
3891static int
3892smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3893{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003894 if (!smp->strm)
3895 return 0;
3896
Willy Tarreau70fe9442018-11-22 16:07:39 +01003897 smp->data.type = SMP_T_SINT;
3898 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
3899 return 1;
3900}
3901
3902/* returns the total number of nanoseconds spent processing the stream */
3903static int
3904smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3905{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003906 if (!smp->strm)
3907 return 0;
3908
Willy Tarreau70fe9442018-11-22 16:07:39 +01003909 smp->data.type = SMP_T_SINT;
3910 smp->data.u.sint = smp->strm->task->cpu_time;
3911 return 1;
3912}
3913
3914/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
3915static int
3916smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3917{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003918 if (!smp->strm)
3919 return 0;
3920
Willy Tarreau70fe9442018-11-22 16:07:39 +01003921 smp->data.type = SMP_T_SINT;
3922 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
3923 return 1;
3924}
3925
3926/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
3927static int
3928smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3929{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003930 if (!smp->strm)
3931 return 0;
3932
Willy Tarreau70fe9442018-11-22 16:07:39 +01003933 smp->data.type = SMP_T_SINT;
3934 smp->data.u.sint = smp->strm->task->lat_time;
3935 return 1;
3936}
3937
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003938static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
3939{
3940 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003941 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003942 smp->data.u.str.area = args[0].data.str.area;
3943 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003944 return 1;
3945}
3946
3947static int smp_check_const_bool(struct arg *args, char **err)
3948{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003949 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
3950 strcasecmp(args[0].data.str.area, "1") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003951 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003952 args[0].type = ARGT_SINT;
3953 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003954 return 1;
3955 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003956 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
3957 strcasecmp(args[0].data.str.area, "0") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003958 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003959 args[0].type = ARGT_SINT;
3960 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003961 return 1;
3962 }
3963 memprintf(err, "Expects 'true', 'false', '0' or '1'");
3964 return 0;
3965}
3966
3967static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
3968{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003969 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003970 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003971 return 1;
3972}
3973
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003974static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003975{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003976 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003977 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003978 return 1;
3979}
3980
3981static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
3982{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003983 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003984 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003985 return 1;
3986}
3987
3988static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
3989{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003990 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003991 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003992 return 1;
3993}
3994
3995static int smp_check_const_bin(struct arg *args, char **err)
3996{
David Carlier64a16ab2016-04-08 10:37:02 +01003997 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003998 int binstrlen;
3999
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004000 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004001 return 0;
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004002 chunk_destroy(&args[0].data.str);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004003 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004004 args[0].data.str.area = binstr;
4005 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004006 return 1;
4007}
4008
4009static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
4010{
4011 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004012 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004013 smp->data.u.str.area = args[0].data.str.area;
4014 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004015 return 1;
4016}
4017
4018static int smp_check_const_meth(struct arg *args, char **err)
4019{
4020 enum http_meth_t meth;
4021 int i;
4022
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004023 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004024 if (meth != HTTP_METH_OTHER) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004025 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004026 args[0].type = ARGT_SINT;
4027 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004028 } else {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05004029 /* Check method avalaibility. A method is a token defined as :
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004030 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
4031 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
4032 * token = 1*tchar
4033 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004034 for (i = 0; i < args[0].data.str.data; i++) {
4035 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004036 memprintf(err, "expects valid method.");
4037 return 0;
4038 }
4039 }
4040 }
4041 return 1;
4042}
4043
4044static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
4045{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004046 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004047 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004048 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004049 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004050 smp->data.u.meth.str.area = "";
4051 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004052 } else {
4053 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004054 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004055 smp->data.u.meth.str.area = args[0].data.str.area;
4056 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004057 }
4058 return 1;
4059}
4060
Luca Schimweg8a694b82019-09-10 15:42:52 +02004061// This function checks the "uuid" sample's arguments.
4062// Function won't get called when no parameter is specified (maybe a bug?)
4063static int smp_check_uuid(struct arg *args, char **err)
4064{
4065 if (!args[0].type) {
4066 args[0].type = ARGT_SINT;
4067 args[0].data.sint = 4;
4068 }
4069 else if (args[0].data.sint != 4) {
4070 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
4071 return 0;
4072 }
4073
4074 return 1;
4075}
4076
4077// Generate a RFC4122 UUID (default is v4 = fully random)
4078static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
4079{
4080 if (args[0].data.sint == 4 || !args[0].type) {
Willy Tarreauee3bcdd2020-03-08 17:48:17 +01004081 ha_generate_uuid(&trash);
Luca Schimweg8a694b82019-09-10 15:42:52 +02004082 smp->data.type = SMP_T_STR;
4083 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4084 smp->data.u.str = trash;
4085 return 1;
4086 }
4087
4088 // more implementations of other uuid formats possible here
4089 return 0;
4090}
4091
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004092/* Note: must not be declared <const> as its list will be overwritten.
4093 * Note: fetches that may return multiple types must be declared as the lowest
4094 * common denominator, the type that can be casted into all other ones. For
4095 * instance IPv4/IPv6 must be declared IPv4.
4096 */
4097static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau0209c972021-03-26 12:03:11 +01004098 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4099 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4100 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_CONST },
4101 { "date", smp_fetch_date, ARG2(0,SINT,STR), smp_check_date_unit, SMP_T_SINT, SMP_USE_CONST },
4102 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4103 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_CONST },
4104 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_CONST },
4105 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4106 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4107 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_CONST },
Willy Tarreau0f30d262014-11-24 16:02:05 +01004108 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau0209c972021-03-26 12:03:11 +01004109 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_CONST },
Willy Tarreau70fe9442018-11-22 16:07:39 +01004110
4111 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4112 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4113 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4114 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4115 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004116
Willy Tarreau0209c972021-03-26 12:03:11 +01004117 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_CONST },
4118 { "bool", smp_fetch_const_bool, ARG1(1,STR), smp_check_const_bool, SMP_T_BOOL, SMP_USE_CONST },
4119 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_CONST },
4120 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_CONST },
4121 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_CONST },
4122 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_CONST },
4123 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_CONST },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004124
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004125 { /* END */ },
4126}};
4127
Willy Tarreau0108d902018-11-25 19:14:37 +01004128INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
4129
Emeric Brun107ca302010-01-04 16:16:05 +01004130/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02004131static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Moemen MHEDHBI848216f2021-04-02 01:05:07 +02004132 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
4133 { "b64dec", sample_conv_base642bin, 0, NULL, SMP_T_STR, SMP_T_BIN },
4134 { "base64", sample_conv_bin2base64, 0, NULL, SMP_T_BIN, SMP_T_STR },
4135 { "ub64enc", sample_conv_bin2base64url,0, NULL, SMP_T_BIN, SMP_T_STR },
4136 { "ub64dec", sample_conv_base64url2bin,0, NULL, SMP_T_STR, SMP_T_BIN },
4137 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
4138 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
4139 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
4140 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
4141 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
4142 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
4143 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4144 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4145 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4146 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4147 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4148 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4149 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4150 { "xxh3", sample_conv_xxh3, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4151 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4152 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4153 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
4154 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
4155 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4156 { "word", sample_conv_word, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4157 { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
4158 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02004159#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01004160 { "sha2", sample_conv_sha2, ARG1(0, SINT), smp_check_sha2, SMP_T_BIN, SMP_T_BIN },
Ilya Shipitsin2c481d02021-03-26 23:35:31 +05004161#ifdef EVP_CIPH_GCM_MODE
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02004162 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
4163#endif
Patrick Gansterer8e366512020-04-22 16:47:57 +02004164 { "digest", sample_conv_crypto_digest, ARG1(1,STR), check_crypto_digest, SMP_T_BIN, SMP_T_BIN },
4165 { "hmac", sample_conv_crypto_hmac, ARG2(2,STR,STR), check_crypto_hmac, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02004166#endif
Willy Tarreau280f42b2018-02-19 15:34:12 +01004167 { "concat", sample_conv_concat, ARG3(1,STR,STR,STR), smp_check_concat, SMP_T_STR, SMP_T_STR },
Tim Duesterhusca097c12018-04-27 21:18:45 +02004168 { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT },
Tim Duesterhusf38175c2020-06-09 11:48:42 +02004169#ifdef USE_OPENSSL
4170 { "secure_memcmp", sample_conv_secure_memcmp, ARG1(1,STR), smp_check_secure_memcmp, SMP_T_BIN, SMP_T_BOOL },
4171#endif
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01004172
4173 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01004174 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
4175 { "protobuf", sample_conv_protobuf, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
Willy Tarreau97707872015-01-27 15:12:13 +01004176
Baptiste Assmanne138dda2020-10-22 15:39:03 +02004177 /* FIX converters */
4178 { "fix_is_valid", sample_conv_fix_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4179 { "fix_tag_value", sample_conv_fix_tag_value, ARG1(1,STR), sample_conv_fix_value_check, SMP_T_BIN, SMP_T_BIN },
4180
Baptiste Assmanne279ca62020-10-27 18:10:06 +01004181 /* MQTT converters */
4182 { "mqtt_is_valid", sample_conv_mqtt_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4183 { "mqtt_field_value", sample_conv_mqtt_field_value, ARG2(2,STR,STR), sample_conv_mqtt_field_value_check, SMP_T_BIN, SMP_T_STR },
4184
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02004185 { "iif", sample_conv_iif, ARG2(2, STR, STR), NULL, SMP_T_BOOL, SMP_T_STR },
4186
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02004187 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4188 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4189 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4190 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
4191 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4192 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4193 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4194 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4195 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4196 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4197 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4198 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4199 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4200 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01004201
Christopher Fauletea159d62020-04-01 16:21:44 +02004202 { "htonl", sample_conv_htonl, 0, NULL, SMP_T_SINT, SMP_T_BIN },
4203 { "cut_crlf", sample_conv_cut_crlf, 0, NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet51fc9d12020-04-01 17:24:41 +02004204 { "ltrim", sample_conv_ltrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet568415a2020-04-01 17:24:47 +02004205 { "rtrim", sample_conv_rtrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02004206 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01004207}};
4208
Willy Tarreau0108d902018-11-25 19:14:37 +01004209INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);