blob: ad15d048557a03fdd2e6a3d8927f4d0693e285cd [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
Alex51c8ad42021-04-15 16:45:15 +020019#include <import/mjson.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <import/sha1.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 Tarreau286631a2021-10-06 11:29:03 +020036#include <haproxy/openssl-compat.h>
Willy Tarreaub23e5952020-06-04 16:06:59 +020037#include <haproxy/protobuf.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020038#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020039#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020040#include <haproxy/sample.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020041#include <haproxy/sink.h>
42#include <haproxy/stick_table.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020043#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020044#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020045#include <haproxy/vars.h>
Tim Duesterhusd5fc8fc2021-09-11 17:51:13 +020046#include <haproxy/xxhash.h>
Thierry FOURNIER01e09742016-12-26 11:46:11 +010047
Willy Tarreau1cf8f082014-02-07 12:14:54 +010048/* sample type names */
49const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020050 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010051 [SMP_T_BOOL] = "bool",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010052 [SMP_T_SINT] = "sint",
53 [SMP_T_ADDR] = "addr",
54 [SMP_T_IPV4] = "ipv4",
55 [SMP_T_IPV6] = "ipv6",
56 [SMP_T_STR] = "str",
57 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020058 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010059};
60
Willy Tarreau12785782012-04-27 21:37:17 +020061/* static sample used in sample_process() when <p> is NULL */
Emeric Brune5c918b2017-06-14 14:15:36 +020062static THREAD_LOCAL struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010063
Willy Tarreau12785782012-04-27 21:37:17 +020064/* list head of all known sample fetch keywords */
65static struct sample_fetch_kw_list sample_fetches = {
66 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010067};
68
Willy Tarreau12785782012-04-27 21:37:17 +020069/* list head of all known sample format conversion keywords */
70static struct sample_conv_kw_list sample_convs = {
71 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010072};
73
Willy Tarreau80aca902013-01-07 15:42:20 +010074const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
Willy Tarreaube2159b2021-03-26 11:56:11 +010075 [SMP_SRC_CONST] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
76 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
77 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
78 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
79 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
80 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +010081 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL_CFG_PARSER |
82 SMP_VAL_CLI_PARSER ),
Willy Tarreaube2159b2021-03-26 11:56:11 +010083
Willy Tarreau80aca902013-01-07 15:42:20 +010084 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
85 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
86 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
87 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
88 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
89 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +010090 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
91 SMP_VAL_CLI_PARSER ),
Willy Tarreau80aca902013-01-07 15:42:20 +010092
93 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
94 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
95 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
96 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
97 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
98 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +010099 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
100 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100101
102 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
103 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
104 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
105 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
106 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
107 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100108 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
109 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100110
111 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
112 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
113 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
114 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
115 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
116 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100117 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
118 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100119
120 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
121 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
122 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
123 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
124 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
125 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100126 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
127 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100128
129 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
130 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
131 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
132 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
133 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
134 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100135 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
136 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100137
138 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
139 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
140 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
141 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
142 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
143 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100144 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
145 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100146
147 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
148 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
149 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
150 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
151 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
152 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100153 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
154 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100155
156 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
157 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
158 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
159 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
160 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
161 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100162 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
163 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100164
165 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
166 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
167 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
168 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
169 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
170 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100171 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
172 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100173
174 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
175 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
176 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
177 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
178 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
179 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100180 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
181 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100182
183 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
184 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
185 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
186 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
187 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
188 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100189 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
190 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100191
192 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
193 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
194 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
195 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
196 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
197 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100198 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
199 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100200
201 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
202 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
203 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
204 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
205 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
206 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100207 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
208 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100209
210 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
211 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
212 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
213 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
214 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
215 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100216 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
217 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100218
219 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
220 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
221 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
222 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
223 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
224 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100225 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
226 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100227
228 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
229 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
230 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
231 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
232 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
233 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100234 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
235 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100236
237 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
238 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
239 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
240 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
241 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
242 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100243 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
244 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100245
246 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
247 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
248 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
249 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
250 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
251 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100252 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
253 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100254
255 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
256 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
257 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
258 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
259 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
260 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100261 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
262 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100263
264 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
265 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
266 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
267 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
268 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
269 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100270 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
271 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100272
273 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
274 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
275 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
276 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
277 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
278 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100279 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
280 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100281};
282
283static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
284 [SMP_SRC_INTRN] = "internal state",
285 [SMP_SRC_LISTN] = "listener",
286 [SMP_SRC_FTEND] = "frontend",
287 [SMP_SRC_L4CLI] = "client address",
288 [SMP_SRC_L5CLI] = "client-side connection",
289 [SMP_SRC_TRACK] = "track counters",
290 [SMP_SRC_L6REQ] = "request buffer",
291 [SMP_SRC_HRQHV] = "HTTP request headers",
292 [SMP_SRC_HRQHP] = "HTTP request",
293 [SMP_SRC_HRQBO] = "HTTP request body",
294 [SMP_SRC_BKEND] = "backend",
295 [SMP_SRC_SERVR] = "server",
296 [SMP_SRC_L4SRV] = "server address",
297 [SMP_SRC_L5SRV] = "server-side connection",
298 [SMP_SRC_L6RES] = "response buffer",
299 [SMP_SRC_HRSHV] = "HTTP response headers",
300 [SMP_SRC_HRSHP] = "HTTP response",
301 [SMP_SRC_HRSBO] = "HTTP response body",
302 [SMP_SRC_RQFIN] = "request buffer statistics",
303 [SMP_SRC_RSFIN] = "response buffer statistics",
304 [SMP_SRC_TXFIN] = "transaction statistics",
305 [SMP_SRC_SSFIN] = "session statistics",
306};
307
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100308static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
309 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
310 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
311 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
312 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
313 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
314 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
315 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
316 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
317 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
318 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
319 [SMP_CKP_BE_SRV_CON] = "server source selection",
320 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
321 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
322 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
323 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
324 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
325 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
326 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
327 [SMP_CKP_FE_LOG_END] = "logs",
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100328 [SMP_CKP_BE_CHK_RUL] = "backend tcp-check rule",
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100329};
330
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100331/* This function returns the type of the data returned by the sample_expr.
332 * It assumes that the <expr> and all of its converters are properly
333 * initialized.
334 */
335inline
336int smp_expr_output_type(struct sample_expr *expr)
337{
338 struct sample_conv_expr *smp_expr;
339
340 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
341 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
342 return smp_expr->conv->out_type;
343 }
344 return expr->fetch->out_type;
345}
346
347
Willy Tarreau80aca902013-01-07 15:42:20 +0100348/* fill the trash with a comma-delimited list of source names for the <use> bit
349 * field which must be composed of a non-null set of SMP_USE_* flags. The return
350 * value is the pointer to the string in the trash buffer.
351 */
352const char *sample_src_names(unsigned int use)
353{
354 int bit;
355
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200356 trash.data = 0;
357 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100358 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
359 if (!(use & ~((1 << bit) - 1)))
360 break; /* no more bits */
361
362 if (!(use & (1 << bit)))
363 continue; /* bit not set */
364
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200365 trash.data += snprintf(trash.area + trash.data,
366 trash.size - trash.data, "%s%s",
367 (use & ((1 << bit) - 1)) ? "," : "",
368 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100369 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200370 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100371}
372
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100373/* return a pointer to the correct sample checkpoint name, or "unknown" when
374 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
375 * if set.
376 */
377const char *sample_ckp_names(unsigned int use)
378{
379 int bit;
380
381 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
382 if (use & (1 << bit))
383 return fetch_ckp_names[bit];
384 return "unknown sample check place, please report this bug";
385}
386
Emeric Brun107ca302010-01-04 16:16:05 +0100387/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100388 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
389 * for next parsing sessions. The fetch keywords capabilities are also computed
390 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100391 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100392void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100393{
Willy Tarreau80aca902013-01-07 15:42:20 +0100394 struct sample_fetch *sf;
395 int bit;
396
397 for (sf = kwl->kw; sf->kw != NULL; sf++) {
398 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
399 if (sf->use & (1 << bit))
400 sf->val |= fetch_cap[bit];
401 }
Willy Tarreau2b718102021-04-21 07:32:39 +0200402 LIST_APPEND(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100403}
404
405/*
Willy Tarreau12785782012-04-27 21:37:17 +0200406 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100407 * parsing sessions.
408 */
Willy Tarreau12785782012-04-27 21:37:17 +0200409void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100410{
Willy Tarreau2b718102021-04-21 07:32:39 +0200411 LIST_APPEND(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100412}
413
414/*
Willy Tarreau12785782012-04-27 21:37:17 +0200415 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100416 * string of <len> in buffer <kw>.
417 *
418 */
Willy Tarreau12785782012-04-27 21:37:17 +0200419struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100420{
421 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200422 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100423
Willy Tarreau12785782012-04-27 21:37:17 +0200424 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100425 for (index = 0; kwl->kw[index].kw != NULL; index++) {
426 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
427 kwl->kw[index].kw[len] == '\0')
428 return &kwl->kw[index];
429 }
430 }
431 return NULL;
432}
433
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100434/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100435 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100436 * <idx> is the index of the next sample fetch entry. It is used as private
437 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100438 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100439 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100440 * the list is reached.
441 */
442struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
443{
444 struct sample_fetch_kw_list *kwl;
445 struct sample_fetch *base;
446
447 if (!current) {
448 /* Get first kwl entry. */
449 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
450 (*idx) = 0;
451 } else {
452 /* Get kwl corresponding to the curret entry. */
453 base = current + 1 - (*idx);
454 kwl = container_of(base, struct sample_fetch_kw_list, kw);
455 }
456
457 while (1) {
458
459 /* Check if kwl is the last entry. */
460 if (&kwl->list == &sample_fetches.list)
461 return NULL;
462
463 /* idx contain the next keyword. If it is available, return it. */
464 if (kwl->kw[*idx].kw) {
465 (*idx)++;
466 return &kwl->kw[(*idx)-1];
467 }
468
469 /* get next entry in the main list, and return NULL if the end is reached. */
470 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
471
472 /* Set index to 0, ans do one other loop. */
473 (*idx) = 0;
474 }
475}
476
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100477/* This function browses the list of available converters. <current> is
478 * the last used converter. If it is the first call, it must set to NULL.
479 * <idx> is the index of the next converter entry. It is used as private
480 * value. It is useless to initiate it.
481 *
482 * It returns always the next sample_conv entry, and NULL when the end of
483 * the list is reached.
484 */
485struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
486{
487 struct sample_conv_kw_list *kwl;
488 struct sample_conv *base;
489
490 if (!current) {
491 /* Get first kwl entry. */
492 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
493 (*idx) = 0;
494 } else {
495 /* Get kwl corresponding to the curret entry. */
496 base = current + 1 - (*idx);
497 kwl = container_of(base, struct sample_conv_kw_list, kw);
498 }
499
500 while (1) {
501 /* Check if kwl is the last entry. */
502 if (&kwl->list == &sample_convs.list)
503 return NULL;
504
505 /* idx contain the next keyword. If it is available, return it. */
506 if (kwl->kw[*idx].kw) {
507 (*idx)++;
508 return &kwl->kw[(*idx)-1];
509 }
510
511 /* get next entry in the main list, and return NULL if the end is reached. */
512 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
513
514 /* Set index to 0, ans do one other loop. */
515 (*idx) = 0;
516 }
517}
518
Emeric Brun107ca302010-01-04 16:16:05 +0100519/*
Willy Tarreau12785782012-04-27 21:37:17 +0200520 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100521 * string of <len> in buffer <kw>.
522 *
523 */
Willy Tarreau12785782012-04-27 21:37:17 +0200524struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100525{
526 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200527 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100528
Willy Tarreau12785782012-04-27 21:37:17 +0200529 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100530 for (index = 0; kwl->kw[index].kw != NULL; index++) {
531 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
532 kwl->kw[index].kw[len] == '\0')
533 return &kwl->kw[index];
534 }
535 }
536 return NULL;
537}
538
Emeric Brun107ca302010-01-04 16:16:05 +0100539/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200540/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100541/******************************************************************/
542
Willy Tarreau342acb42012-04-23 22:03:39 +0200543static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100544{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200545 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200546 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100547 return 1;
548}
549
Willy Tarreau342acb42012-04-23 22:03:39 +0200550static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100551{
Willy Tarreau83061a82018-07-13 11:56:34 +0200552 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100553
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200554 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100555 return 0;
556
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200557 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200558 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200559 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100560 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100561
562 return 1;
563}
564
Willy Tarreau342acb42012-04-23 22:03:39 +0200565static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100566{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200567 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200568 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100569 return 1;
570}
571
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200572static int c_ipv62ip(struct sample *smp)
573{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200574 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200575 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100576 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200577 return 1;
578}
579
Willy Tarreau342acb42012-04-23 22:03:39 +0200580static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100581{
Willy Tarreau83061a82018-07-13 11:56:34 +0200582 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100583
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200584 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100585 return 0;
586
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200587 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200588 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200589 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100590 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100591 return 1;
592}
593
594/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200595static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100596{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200597 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100598}
599*/
600
Willy Tarreau342acb42012-04-23 22:03:39 +0200601static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100602{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200603 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200604 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100605 return 1;
606}
607
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200608static int c_int2ipv6(struct sample *smp)
609{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200610 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
611 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200612 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200613 return 1;
614}
615
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100616static int c_str2addr(struct sample *smp)
617{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200618 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
619 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100620 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200621 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100622 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100623 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100624 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200625 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100626 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100627 return 1;
628}
629
Willy Tarreau342acb42012-04-23 22:03:39 +0200630static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100631{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200632 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100633 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200634 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100635 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100636 return 1;
637}
638
Willy Tarreau342acb42012-04-23 22:03:39 +0200639static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100640{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200641 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100642 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200643 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100644 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100645 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100646}
647
Emeric Brun4b9e8022014-11-03 18:17:10 +0100648/*
649 * The NULL char always enforces the end of string if it is met.
650 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100651 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200652static int c_bin2str(struct sample *smp)
653{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100654 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200655
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200656 for (i = 0; i < smp->data.u.str.data; i++) {
657 if (!smp->data.u.str.area[i]) {
658 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100659 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100660 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200661 }
Christopher Faulet472ad512020-04-30 09:57:40 +0200662 smp->data.type = SMP_T_STR;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200663 return 1;
664}
665
Willy Tarreau342acb42012-04-23 22:03:39 +0200666static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100667{
Willy Tarreau83061a82018-07-13 11:56:34 +0200668 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100669 char *pos;
670
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200671 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100672 if (!pos)
673 return 0;
674
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200675 trash->size = trash->size - (pos - trash->area);
676 trash->area = pos;
677 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200678 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200679 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100680 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100681 return 1;
682}
683
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800684/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200685 * For strings and binary blocks, it also provides a known allocated size with
686 * a length that is capped to the size, and ensures a trailing zero is always
687 * appended for strings. This is necessary for some operations which may
688 * require to extend the length. It returns 0 if it fails, 1 on success.
689 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100690int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200691{
Willy Tarreau83061a82018-07-13 11:56:34 +0200692 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200693
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200694 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100695 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100696 case SMP_T_SINT:
697 case SMP_T_ADDR:
698 case SMP_T_IPV4:
699 case SMP_T_IPV6:
700 /* These type are not const. */
701 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200702
Christopher Fauletec100512017-07-24 15:38:41 +0200703 case SMP_T_METH:
704 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
705 break;
706 /* Fall through */
707
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100708 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200709 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100710 trash->data = smp->data.type == SMP_T_STR ?
711 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200712 if (trash->data > trash->size - 1)
713 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200714
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100715 memcpy(trash->area, smp->data.type == SMP_T_STR ?
716 smp->data.u.str.area : smp->data.u.meth.str.area,
717 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200718 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200719 smp->data.u.str = *trash;
720 break;
721
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100722 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100723 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200724 trash->data = smp->data.u.str.data;
725 if (trash->data > trash->size)
726 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200727
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200728 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200729 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100730 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200731
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100732 default:
733 /* Other cases are unexpected. */
734 return 0;
735 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100736
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100737 /* remove const flag */
738 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200739 return 1;
740}
741
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100742int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100743{
744 return 1;
745}
746
Willy Tarreau342acb42012-04-23 22:03:39 +0200747static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100748{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200749 const char *str;
750 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100751
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200752 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100753 return 0;
754
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200755 str = smp->data.u.str.area;
756 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100757
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200758 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200759 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100760 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100761 return 1;
762}
763
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100764static int c_str2meth(struct sample *smp)
765{
766 enum http_meth_t meth;
767 int len;
768
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200769 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100770 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200771 len = smp->data.u.str.data;
772 smp->data.u.meth.str.area = smp->data.u.str.area;
773 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100774 }
775 else
776 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200777 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200778 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100779 return 1;
780}
781
782static int c_meth2str(struct sample *smp)
783{
784 int len;
785 enum http_meth_t meth;
786
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200787 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100788 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200789 len = smp->data.u.meth.str.data;
790 smp->data.u.str.area = smp->data.u.meth.str.area;
791 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200792 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100793 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200794 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100795 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200796 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200797 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200798 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100799 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200800 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100801 }
802 else {
803 /* Unknown method */
804 return 0;
805 }
806 return 1;
807}
808
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200809static int c_addr2bin(struct sample *smp)
810{
Willy Tarreau83061a82018-07-13 11:56:34 +0200811 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200812
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200813 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200814 chk->data = 4;
815 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200816 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200817 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200818 chk->data = 16;
819 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200820 }
821 else
822 return 0;
823
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200824 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200825 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200826 return 1;
827}
828
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200829static int c_int2bin(struct sample *smp)
830{
Willy Tarreau83061a82018-07-13 11:56:34 +0200831 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200832
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200833 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
834 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200835
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200836 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200837 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200838 return 1;
839}
840
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200841
Emeric Brun107ca302010-01-04 16:16:05 +0100842/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200843/* Sample casts matrix: */
844/* sample_casts[from type][to type] */
845/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100846/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100847
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100848sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200849/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
850/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
851/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200852/* 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 +0200853/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
854/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200855/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200856/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
857/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
858/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200859};
Emeric Brun107ca302010-01-04 16:16:05 +0100860
Emeric Brun107ca302010-01-04 16:16:05 +0100861/*
Willy Tarreau12785782012-04-27 21:37:17 +0200862 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100863 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200864 * Returns a pointer on allocated sample expression structure.
Christopher Faulet35926a12021-09-30 08:48:56 +0200865 * <al> is an arg_list serving as a list head to report missing dependencies.
866 * It may be NULL if such dependencies are not allowed. Otherwise, the caller
867 * must have set al->ctx if al is set.
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100868 * If <endptr> is non-nul, it will be set to the first unparsed character
869 * (which may be the final '\0') on success. If it is nul, the expression
870 * must be properly terminated by a '\0' otherwise an error is reported.
Emeric Brun107ca302010-01-04 16:16:05 +0100871 */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100872struct 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 +0100873{
Willy Tarreau833cc792013-07-24 15:34:19 +0200874 const char *begw; /* beginning of word */
875 const char *endw; /* end of word */
876 const char *endt; /* end of term */
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +0100877 struct sample_expr *expr = NULL;
Willy Tarreau12785782012-04-27 21:37:17 +0200878 struct sample_fetch *fetch;
879 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100880 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200881 char *fkw = NULL;
882 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100883 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100884
Willy Tarreau833cc792013-07-24 15:34:19 +0200885 begw = str[*idx];
Willy Tarreaued2c6622020-02-14 18:27:10 +0100886 for (endw = begw; is_idchar(*endw); endw++)
887 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200888
889 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100890 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100891 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200892 }
Emeric Brun107ca302010-01-04 16:16:05 +0100893
Willy Tarreau833cc792013-07-24 15:34:19 +0200894 /* keep a copy of the current fetch keyword for error reporting */
895 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100896
Willy Tarreau833cc792013-07-24 15:34:19 +0200897 fetch = find_sample_fetch(begw, endw - begw);
898 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100899 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100900 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200901 }
Emeric Brun107ca302010-01-04 16:16:05 +0100902
Willy Tarreau833cc792013-07-24 15:34:19 +0200903 /* At this point, we have :
904 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100905 * - endw : end of the keyword, first character not part of keyword
Willy Tarreau833cc792013-07-24 15:34:19 +0200906 */
907
908 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100909 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100910 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200911 }
Emeric Brun107ca302010-01-04 16:16:05 +0100912 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200913
Vincent Bernat02779b62016-04-03 13:48:43 +0200914 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200915 if (!expr)
916 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100917
918 LIST_INIT(&(expr->conv_exprs));
919 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200920 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100921
Willy Tarreau689a1df2013-12-13 00:40:11 +0100922 /* Note that we call the argument parser even with an empty string,
923 * this allows it to automatically create entries for mandatory
924 * implicit arguments (eg: local proxy name).
925 */
Christopher Faulet35926a12021-09-30 08:48:56 +0200926 if (al) {
927 al->kw = expr->fetch->kw;
928 al->conv = NULL;
929 }
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100930 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 +0100931 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
932 goto out_error;
933 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200934
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100935 /* now endt is our first char not part of the arg list, typically the
936 * comma after the sample fetch name or after the closing parenthesis,
937 * or the NUL char.
938 */
939
Willy Tarreau689a1df2013-12-13 00:40:11 +0100940 if (!expr->arg_p) {
941 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200942 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100943 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
944 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200945 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100946 }
947
Willy Tarreau833cc792013-07-24 15:34:19 +0200948 /* Now process the converters if any. We have two supported syntaxes
949 * for the converters, which can be combined :
950 * - comma-delimited list of converters just after the keyword and args ;
951 * - one converter per keyword
952 * The combination allows to have each keyword being a comma-delimited
953 * series of converters.
954 *
955 * We want to process the former first, then the latter. For this we start
956 * from the beginning of the supposed place in the exiting conv chain, which
957 * starts at the last comma (endt).
958 */
959
960 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200961 struct sample_conv_expr *conv_expr;
Willy Tarreau46dfd782019-12-17 10:25:29 +0100962 int err_arg;
963 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +0100964
Willy Tarreau833cc792013-07-24 15:34:19 +0200965 if (*endt && *endt != ',') {
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100966 if (endptr) {
967 /* end found, let's stop here */
968 break;
969 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200970 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100971 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200972 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100973 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100974 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200975 }
Emeric Brun107ca302010-01-04 16:16:05 +0100976
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100977 /* FIXME: how long should we support such idiocies ? Maybe we
978 * should already warn ?
979 */
Willy Tarreau833cc792013-07-24 15:34:19 +0200980 while (*endt == ',') /* then trailing commas */
981 endt++;
982
Willy Tarreau97108e02016-11-25 07:33:24 +0100983 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200984
985 if (!*begw) {
986 /* none ? skip to next string */
987 (*idx)++;
988 begw = str[*idx];
989 if (!begw || !*begw)
990 break;
991 }
992
Willy Tarreaued2c6622020-02-14 18:27:10 +0100993 for (endw = begw; is_idchar(*endw); endw++)
994 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200995
996 free(ckw);
997 ckw = my_strndup(begw, endw - begw);
998
999 conv = find_sample_conv(begw, endw - begw);
1000 if (!conv) {
1001 /* we found an isolated keyword that we don't know, it's not ours */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001002 if (begw == str[*idx]) {
1003 endt = begw;
Willy Tarreau833cc792013-07-24 15:34:19 +02001004 break;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001005 }
Willy Tarreau97108e02016-11-25 07:33:24 +01001006 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +02001007 goto out_error;
1008 }
Emeric Brun107ca302010-01-04 16:16:05 +01001009
Willy Tarreau833cc792013-07-24 15:34:19 +02001010 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001011 memprintf(err_msg, "returns type of converter '%s' is unknown", 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 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +02001016 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001017 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001018 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001019 }
Emeric Brun107ca302010-01-04 16:16:05 +01001020
1021 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +02001022 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +02001023 if (!conv_expr)
1024 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001025
Willy Tarreau2b718102021-04-21 07:32:39 +02001026 LIST_APPEND(&(expr->conv_exprs), &(conv_expr->list));
Emeric Brun107ca302010-01-04 16:16:05 +01001027 conv_expr->conv = conv;
1028
Christopher Faulet35926a12021-09-30 08:48:56 +02001029 if (al) {
1030 al->kw = expr->fetch->kw;
1031 al->conv = conv_expr->conv->kw;
1032 }
Willy Tarreau80b53ff2020-02-14 08:40:37 +01001033 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 +01001034 if (argcnt < 0) {
1035 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
1036 goto out_error;
1037 }
Willy Tarreau9e92d322010-01-26 17:58:06 +01001038
Willy Tarreau46dfd782019-12-17 10:25:29 +01001039 if (argcnt && !conv->arg_mask) {
1040 memprintf(err_msg, "converter '%s' does not support any args", ckw);
1041 goto out_error;
1042 }
Willy Tarreau21d68a62012-04-20 15:52:36 +02001043
Willy Tarreau46dfd782019-12-17 10:25:29 +01001044 if (!conv_expr->arg_p)
1045 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001046
Willy Tarreau46dfd782019-12-17 10:25:29 +01001047 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1048 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001049 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001050 }
1051 }
Emeric Brun485479d2010-09-23 18:02:19 +02001052
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001053 if (endptr) {
1054 /* end found, let's stop here */
1055 *endptr = (char *)endt;
1056 }
1057
Willy Tarreau833cc792013-07-24 15:34:19 +02001058 out:
1059 free(fkw);
1060 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001061 return expr;
1062
1063out_error:
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +01001064 release_sample_expr(expr);
Willy Tarreau833cc792013-07-24 15:34:19 +02001065 expr = NULL;
1066 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001067}
1068
1069/*
Willy Tarreau12785782012-04-27 21:37:17 +02001070 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001071 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001072 * Returns a pointer on a typed sample structure containing the result or NULL if
1073 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001074 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001075 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001076 *
1077 * Note: the fetch functions are required to properly set the return type. The
1078 * conversion functions must do so too. However the cast functions do not need
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001079 * to since they're made to cast multiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001080 *
1081 * The caller may indicate in <opt> if it considers the result final or not.
1082 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1083 * if the result is stable or not, according to the following table :
1084 *
1085 * return MAY_CHANGE FINAL Meaning for the sample
1086 * NULL 0 * Not present and will never be (eg: header)
1087 * NULL 1 0 Not present yet, could change (eg: POST param)
1088 * NULL 1 1 Not present yet, will not change anymore
1089 * smp 0 * Present and will not change (eg: header)
1090 * smp 1 0 Present, may change (eg: request length)
1091 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001092 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001093struct sample *sample_process(struct proxy *px, struct session *sess,
1094 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001095 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001096{
Willy Tarreau12785782012-04-27 21:37:17 +02001097 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001098
Willy Tarreau18387e22013-07-25 12:02:38 +02001099 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001100 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001101 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001102 }
Emeric Brun107ca302010-01-04 16:16:05 +01001103
Willy Tarreau1777ea62016-03-10 16:15:46 +01001104 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001105 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001106 return NULL;
1107
Emeric Brun107ca302010-01-04 16:16:05 +01001108 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001109 /* we want to ensure that p->type can be casted into
1110 * conv_expr->conv->in_type. We have 3 possibilities :
1111 * - NULL => not castable.
1112 * - c_none => nothing to do (let's optimize it)
1113 * - other => apply cast and prepare to fail
1114 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001115 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001116 return NULL;
1117
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001118 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1119 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001120 return NULL;
1121
Willy Tarreau12e50112012-04-25 17:21:49 +02001122 /* OK cast succeeded */
1123
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001124 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001125 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001126 }
1127 return p;
1128}
1129
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001130/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001131 * Resolve all remaining arguments in proxy <p>. Returns the number of
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001132 * errors or 0 if everything is fine. If at least one error is met, it will
1133 * be appended to *err. If *err==NULL it will be allocated first.
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001134 */
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001135int smp_resolve_args(struct proxy *p, char **err)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001136{
1137 struct arg_list *cur, *bak;
1138 const char *ctx, *where;
1139 const char *conv_ctx, *conv_pre, *conv_pos;
1140 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001141 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001142 struct arg *arg;
1143 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001144 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001145
1146 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1147 struct proxy *px;
1148 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001149 struct stktable *t;
1150 char *pname, *sname, *stktname;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001151 char *err2;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001152
1153 arg = cur->arg;
1154
1155 /* prepare output messages */
1156 conv_pre = conv_pos = conv_ctx = "";
1157 if (cur->conv) {
1158 conv_ctx = cur->conv;
1159 conv_pre = "conversion keyword '";
1160 conv_pos = "' for ";
1161 }
1162
1163 where = "in";
1164 ctx = "sample fetch keyword";
1165 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001166 case ARGC_STK: where = "in stick rule in"; break;
1167 case ARGC_TRK: where = "in tracking rule in"; break;
1168 case ARGC_LOG: where = "in log-format string in"; break;
1169 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
Willy Tarreau57467b82021-09-02 19:43:20 +02001170 case ARGC_HRQ: where = "in http-request expression in"; break;
1171 case ARGC_HRS: where = "in http-response response in"; break;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001172 case ARGC_UIF: where = "in unique-id-format string in"; break;
1173 case ARGC_RDR: where = "in redirect format string in"; break;
1174 case ARGC_CAP: where = "in capture rule in"; break;
1175 case ARGC_ACL: ctx = "ACL keyword"; break;
1176 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001177 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001178 case ARGC_HERR: where = "in http-error directive in"; break;
Miroslav Zagorac7f8314c2020-12-09 16:31:48 +01001179 case ARGC_OT: where = "in ot-scope directive in"; break;
Willy Tarreau57467b82021-09-02 19:43:20 +02001180 case ARGC_TCO: where = "in tcp-request connection expression in"; break;
1181 case ARGC_TSE: where = "in tcp-request session expression in"; break;
1182 case ARGC_TRQ: where = "in tcp-request content expression in"; break;
1183 case ARGC_TRS: where = "in tcp-response content expression in"; break;
1184 case ARGC_TCK: where = "in tcp-check expression in"; break;
1185 case ARGC_CFG: where = "in configuration expression in"; break;
1186 case ARGC_CLI: where = "in CLI expression in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001187 }
1188
1189 /* set a few default settings */
1190 px = p;
1191 pname = p->id;
1192
1193 switch (arg->type) {
1194 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001195 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001196 memprintf(err, "%sparsing [%s:%d]: missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1197 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001198 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001199 cfgerr++;
1200 continue;
1201 }
1202
1203 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001204 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001205
1206 if (sname) {
1207 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001208 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001209
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001210 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001211 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001212 memprintf(err, "%sparsing [%s:%d]: unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1213 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001214 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001215 cfgerr++;
1216 break;
1217 }
1218 }
1219 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001220 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001221
1222 srv = findserver(px, sname);
1223 if (!srv) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001224 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",
1225 *err ? *err : "", cur->file, cur->line, sname, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001226 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001227 cfgerr++;
1228 break;
1229 }
1230
Amaury Denoyelle06269612021-08-23 14:05:07 +02001231 srv->flags |= SRV_F_NON_PURGEABLE;
1232
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001233 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001234 arg->unresolved = 0;
1235 arg->data.srv = srv;
1236 break;
1237
1238 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001239 if (arg->data.str.data) {
1240 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001241 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001242 }
1243
1244 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001245 memprintf(err, "%sparsing [%s:%d]: unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1246 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001247 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001248 cfgerr++;
1249 break;
1250 }
1251
1252 if (!(px->cap & PR_CAP_FE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001253 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",
1254 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001255 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001256 cfgerr++;
1257 break;
1258 }
1259
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001260 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001261 arg->unresolved = 0;
1262 arg->data.prx = px;
1263 break;
1264
1265 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001266 if (arg->data.str.data) {
1267 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001268 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001269 }
1270
1271 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001272 memprintf(err, "%sparsing [%s:%d]: unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1273 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001274 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001275 cfgerr++;
1276 break;
1277 }
1278
1279 if (!(px->cap & PR_CAP_BE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001280 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",
1281 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001282 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001283 cfgerr++;
1284 break;
1285 }
1286
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001287 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001288 arg->unresolved = 0;
1289 arg->data.prx = px;
1290 break;
1291
1292 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001293 if (arg->data.str.data)
1294 stktname = arg->data.str.area;
1295 else
1296 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001297
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001298 t = stktable_find_by_name(stktname);
1299 if (!t) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001300 memprintf(err, "%sparsing [%s:%d]: unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1301 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001302 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001303 cfgerr++;
1304 break;
1305 }
1306
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001307 if (!t->size) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001308 memprintf(err, "%sparsing [%s:%d]: no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1309 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001310 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001311 cfgerr++;
1312 break;
1313 }
1314
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001315 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001316 p->next_stkt_ref = t->proxies_list;
1317 t->proxies_list = p;
1318 }
1319
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001320 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001321 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001322 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001323 break;
1324
1325 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001326 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001327 memprintf(err, "%sparsing [%s:%d]: missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1328 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001329 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001330 cfgerr++;
1331 break;
1332 }
1333
1334 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001335 strcmp(p->uri_auth->userlist->name, arg->data.str.area) == 0)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001336 ul = p->uri_auth->userlist;
1337 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001338 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001339
1340 if (!ul) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001341 memprintf(err, "%sparsing [%s:%d]: unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1342 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001343 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001344 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001345 cfgerr++;
1346 break;
1347 }
1348
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001349 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001350 arg->unresolved = 0;
1351 arg->data.usr = ul;
1352 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001353
1354 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001355 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001356 memprintf(err, "%sparsing [%s:%d]: missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1357 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001358 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001359 cfgerr++;
1360 continue;
1361 }
1362
Willy Tarreau46947782015-01-19 19:00:58 +01001363 rflags = 0;
1364 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001365 err2 = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001366
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001367 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err2))) {
1368 memprintf(err, "%sparsing [%s:%d]: error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1369 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001370 arg->data.str.area,
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001371 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err2);
Willy Tarreau46947782015-01-19 19:00:58 +01001372 cfgerr++;
1373 continue;
1374 }
1375
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001376 chunk_destroy(&arg->data.str);
Willy Tarreau46947782015-01-19 19:00:58 +01001377 arg->unresolved = 0;
1378 arg->data.reg = reg;
1379 break;
1380
1381
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001382 }
1383
Willy Tarreau2b718102021-04-21 07:32:39 +02001384 LIST_DELETE(&cur->list);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001385 free(cur);
1386 } /* end of args processing */
1387
1388 return cfgerr;
1389}
1390
1391/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001392 * Process a fetch + format conversion as defined by the sample expression
1393 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001394 * not explicitly set to <smp_type>, but shall be compatible with it as
1395 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1396 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001397 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1398 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1399 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1400 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001401 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1402 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1403 * still hope to get it after waiting longer, and is not converted to string.
1404 * The possible output combinations are the following :
1405 *
1406 * return MAY_CHANGE FINAL Meaning for the sample
1407 * NULL * * Not present and will never be (eg: header)
1408 * smp 0 * Final value converted (eg: header)
1409 * smp 1 0 Not present yet, may appear later (eg: header)
1410 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001411 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001412struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001413 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001414 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001415{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001416 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001417
Willy Tarreau6c616e02014-06-25 16:56:41 +02001418 memset(smp, 0, sizeof(*smp));
1419
Willy Tarreau192252e2015-04-04 01:47:55 +02001420 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001421 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1422 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001423 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001424 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001425
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001426 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001427 return NULL;
1428
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001429 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001430 return NULL;
1431
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001432 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001433 return smp;
1434}
1435
Christopher Faulet476e5d02016-10-26 11:34:47 +02001436static void release_sample_arg(struct arg *p)
1437{
1438 struct arg *p_back = p;
1439
1440 if (!p)
1441 return;
1442
1443 while (p->type != ARGT_STOP) {
1444 if (p->type == ARGT_STR || p->unresolved) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001445 chunk_destroy(&p->data.str);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001446 p->unresolved = 0;
1447 }
1448 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001449 regex_free(p->data.reg);
1450 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001451 }
1452 p++;
1453 }
1454
1455 if (p_back != empty_arg_list)
1456 free(p_back);
1457}
1458
1459void release_sample_expr(struct sample_expr *expr)
1460{
1461 struct sample_conv_expr *conv_expr, *conv_exprb;
1462
1463 if (!expr)
1464 return;
1465
Tim Duesterhus867cd982020-07-04 11:49:39 +02001466 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001467 LIST_DELETE(&conv_expr->list);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001468 release_sample_arg(conv_expr->arg_p);
Tim Duesterhus867cd982020-07-04 11:49:39 +02001469 free(conv_expr);
1470 }
1471
Christopher Faulet476e5d02016-10-26 11:34:47 +02001472 release_sample_arg(expr->arg_p);
1473 free(expr);
1474}
1475
Emeric Brun107ca302010-01-04 16:16:05 +01001476/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001477/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001478/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001479/*****************************************************************/
1480
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001481static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1482{
1483 int i;
1484 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001485 struct buffer *buf;
1486 struct sink *sink;
1487 struct ist line;
1488 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001489
Willy Tarreau0851fd52019-12-17 10:07:25 +01001490 buf = alloc_trash_chunk();
1491 if (!buf)
1492 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001493
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001494 sink = (struct sink *)arg_p[1].data.ptr;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001495 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001496
Willy Tarreau0851fd52019-12-17 10:07:25 +01001497 pfx = arg_p[0].data.str.area;
1498 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001499
Willy Tarreau0851fd52019-12-17 10:07:25 +01001500 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1501 if (!sample_casts[smp->data.type][SMP_T_STR])
1502 goto nocast;
1503
1504 /* Copy sample fetch. This puts the sample as const, the
1505 * cast will copy data if a transformation is required.
1506 */
1507 memcpy(&tmp, smp, sizeof(struct sample));
1508 tmp.flags = SMP_F_CONST;
1509
1510 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1511 goto nocast;
1512
1513 /* Display the displayable chars*. */
1514 b_putchr(buf, '<');
1515 for (i = 0; i < tmp.data.u.str.data; i++) {
Willy Tarreau90807112020-02-25 08:16:33 +01001516 if (isprint((unsigned char)tmp.data.u.str.area[i]))
Willy Tarreau0851fd52019-12-17 10:07:25 +01001517 b_putchr(buf, tmp.data.u.str.area[i]);
1518 else
1519 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001520 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001521 b_putchr(buf, '>');
1522
1523 done:
1524 line = ist2(buf->area, buf->data);
Emeric Brun54648852020-07-06 15:54:06 +02001525 sink_write(sink, &line, 1, 0, 0, NULL);
Willy Tarreau0851fd52019-12-17 10:07:25 +01001526 end:
1527 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001528 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001529 nocast:
1530 chunk_appendf(buf, "(undisplayable)");
1531 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001532}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001533
1534// This function checks the "debug" converter's arguments.
1535static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1536 const char *file, int line, char **err)
1537{
1538 const char *name = "buf0";
1539 struct sink *sink = NULL;
1540
1541 if (args[0].type != ARGT_STR) {
1542 /* optional prefix */
1543 args[0].data.str.area = "";
1544 args[0].data.str.data = 0;
1545 }
1546
1547 if (args[1].type == ARGT_STR)
1548 name = args[1].data.str.area;
1549
1550 sink = sink_find(name);
1551 if (!sink) {
1552 memprintf(err, "No such sink '%s'", name);
1553 return 0;
1554 }
1555
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001556 chunk_destroy(&args[1].data.str);
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001557 args[1].type = ARGT_PTR;
1558 args[1].data.ptr = sink;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001559 return 1;
1560}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001561
Holger Just1bfc24b2017-05-06 00:56:53 +02001562static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1563{
Willy Tarreau83061a82018-07-13 11:56:34 +02001564 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001565 int bin_len;
1566
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001567 trash->data = 0;
1568 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1569 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001570 if (bin_len < 0)
1571 return 0;
1572
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001573 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001574 smp->data.u.str = *trash;
1575 smp->data.type = SMP_T_BIN;
1576 smp->flags &= ~SMP_F_CONST;
1577 return 1;
1578}
1579
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +02001580static int sample_conv_base64url2bin(const struct arg *arg_p, struct sample *smp, void *private)
1581{
1582 struct buffer *trash = get_trash_chunk();
1583 int bin_len;
1584
1585 trash->data = 0;
1586 bin_len = base64urldec(smp->data.u.str.area, smp->data.u.str.data,
1587 trash->area, trash->size);
1588 if (bin_len < 0)
1589 return 0;
1590
1591 trash->data = bin_len;
1592 smp->data.u.str = *trash;
1593 smp->data.type = SMP_T_BIN;
1594 smp->flags &= ~SMP_F_CONST;
1595 return 1;
1596}
1597
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001598static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001599{
Willy Tarreau83061a82018-07-13 11:56:34 +02001600 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001601 int b64_len;
1602
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001603 trash->data = 0;
1604 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1605 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001606 if (b64_len < 0)
1607 return 0;
1608
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001609 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001610 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001611 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001612 smp->flags &= ~SMP_F_CONST;
1613 return 1;
1614}
1615
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +02001616static int sample_conv_bin2base64url(const struct arg *arg_p, struct sample *smp, void *private)
1617{
1618 struct buffer *trash = get_trash_chunk();
1619 int b64_len;
1620
1621 trash->data = 0;
1622 b64_len = a2base64url(smp->data.u.str.area, smp->data.u.str.data,
1623 trash->area, trash->size);
1624 if (b64_len < 0)
1625 return 0;
1626
1627 trash->data = b64_len;
1628 smp->data.u.str = *trash;
1629 smp->data.type = SMP_T_STR;
1630 smp->flags &= ~SMP_F_CONST;
1631 return 1;
1632}
1633
Willy Tarreau168e8de2021-10-06 15:29:00 +02001634/* This function returns a sample struct filled with the conversion of variable
1635 * <var> to sample type <type> (SMP_T_*), via a cast to the target type. If the
1636 * variable cannot be retrieved or casted, 0 is returned, otherwise 1.
1637 *
1638 * Keep in mind that the sample content may be written to a pre-allocated
1639 * trash chunk as returned by get_trash_chunk().
1640 */
1641int sample_conv_var2smp(const struct var_desc *var, struct sample *smp, int type)
1642{
1643 if (!vars_get_by_desc(var, smp, NULL))
1644 return 0;
1645 if (!sample_casts[smp->data.type][type])
1646 return 0;
1647 if (!sample_casts[smp->data.type][type](smp))
1648 return 0;
1649 return 1;
1650}
1651
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001652static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1653{
1654 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001655 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001656
1657 memset(&ctx, 0, sizeof(ctx));
1658
1659 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001660 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1661 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001662
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001663 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001664 smp->data.u.str = *trash;
1665 smp->data.type = SMP_T_BIN;
1666 smp->flags &= ~SMP_F_CONST;
1667 return 1;
1668}
1669
Tim Duesterhusd4376302019-06-17 12:41:44 +02001670#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01001671static int smp_check_sha2(struct arg *args, struct sample_conv *conv,
1672 const char *file, int line, char **err)
1673{
1674 if (args[0].type == ARGT_STOP)
1675 return 1;
1676 if (args[0].type != ARGT_SINT) {
1677 memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]);
1678 return 0;
1679 }
1680
1681 switch (args[0].data.sint) {
1682 case 224:
1683 case 256:
1684 case 384:
1685 case 512:
1686 /* this is okay */
1687 return 1;
1688 default:
1689 memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint);
1690 return 0;
1691 }
1692}
1693
Tim Duesterhusd4376302019-06-17 12:41:44 +02001694static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
1695{
1696 struct buffer *trash = get_trash_chunk();
1697 int bits = 256;
Christopher Faulete6e7a582021-01-29 11:29:28 +01001698 if (arg_p->data.sint)
Tim Duesterhusd4376302019-06-17 12:41:44 +02001699 bits = arg_p->data.sint;
1700
1701 switch (bits) {
1702 case 224: {
1703 SHA256_CTX ctx;
1704
1705 memset(&ctx, 0, sizeof(ctx));
1706
1707 SHA224_Init(&ctx);
1708 SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1709 SHA224_Final((unsigned char *) trash->area, &ctx);
1710 trash->data = SHA224_DIGEST_LENGTH;
1711 break;
1712 }
1713 case 256: {
1714 SHA256_CTX ctx;
1715
1716 memset(&ctx, 0, sizeof(ctx));
1717
1718 SHA256_Init(&ctx);
1719 SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1720 SHA256_Final((unsigned char *) trash->area, &ctx);
1721 trash->data = SHA256_DIGEST_LENGTH;
1722 break;
1723 }
1724 case 384: {
1725 SHA512_CTX ctx;
1726
1727 memset(&ctx, 0, sizeof(ctx));
1728
1729 SHA384_Init(&ctx);
1730 SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1731 SHA384_Final((unsigned char *) trash->area, &ctx);
1732 trash->data = SHA384_DIGEST_LENGTH;
1733 break;
1734 }
1735 case 512: {
1736 SHA512_CTX ctx;
1737
1738 memset(&ctx, 0, sizeof(ctx));
1739
1740 SHA512_Init(&ctx);
1741 SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1742 SHA512_Final((unsigned char *) trash->area, &ctx);
1743 trash->data = SHA512_DIGEST_LENGTH;
1744 break;
1745 }
1746 default:
1747 return 0;
1748 }
1749
1750 smp->data.u.str = *trash;
1751 smp->data.type = SMP_T_BIN;
1752 smp->flags &= ~SMP_F_CONST;
1753 return 1;
1754}
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001755
Dragan Dosen9e8db132021-02-22 10:03:53 +01001756/* This function returns a sample struct filled with an <arg> content.
1757 * If the <arg> contains a string, it is returned in the sample flagged as
1758 * SMP_F_CONST. If the <arg> contains a variable descriptor, the sample is
1759 * filled with the content of the variable by using vars_get_by_desc().
1760 *
1761 * Keep in mind that the sample content may be written to a pre-allocated
1762 * trash chunk as returned by get_trash_chunk().
1763 *
1764 * This function returns 0 if an error occurs, otherwise it returns 1.
1765 */
Willy Tarreau4034e2c2021-10-06 15:20:18 +02001766int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001767{
1768 switch (arg->type) {
1769 case ARGT_STR:
1770 smp->data.type = SMP_T_STR;
1771 smp->data.u.str = arg->data.str;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001772 smp->flags = SMP_F_CONST;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001773 return 1;
1774 case ARGT_VAR:
Willy Tarreau2476ff12021-10-06 15:30:52 +02001775 return sample_conv_var2smp(&arg->data.var, smp, SMP_T_STR);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001776 default:
1777 return 0;
1778 }
1779}
1780
Dragan Dosen9e8db132021-02-22 10:03:53 +01001781/* This function checks an <arg> and fills it with a variable type if the
1782 * <arg> string contains a valid variable name. If failed, the function
1783 * tries to perform a base64 decode operation on the same string, and
1784 * fills the <arg> with the decoded content.
1785 *
1786 * Validation is skipped if the <arg> string is empty.
1787 *
1788 * This function returns 0 if the variable lookup fails and the specified
1789 * <arg> string is not a valid base64 encoded string, as well if
1790 * unexpected argument type is specified or memory allocation error
1791 * occurs. Otherwise it returns 1.
1792 */
1793static inline int sample_check_arg_base64(struct arg *arg, char **err)
1794{
1795 char *dec = NULL;
1796 int dec_size;
1797
1798 if (arg->type != ARGT_STR) {
1799 memprintf(err, "unexpected argument type");
1800 return 0;
1801 }
1802
1803 if (arg->data.str.data == 0) /* empty */
1804 return 1;
1805
1806 if (vars_check_arg(arg, NULL))
1807 return 1;
1808
1809 if (arg->data.str.data % 4) {
1810 memprintf(err, "argument needs to be base64 encoded, and "
1811 "can either be a string or a variable");
1812 return 0;
1813 }
1814
1815 dec_size = (arg->data.str.data / 4 * 3)
1816 - (arg->data.str.area[arg->data.str.data-1] == '=' ? 1 : 0)
1817 - (arg->data.str.area[arg->data.str.data-2] == '=' ? 1 : 0);
1818
1819 if ((dec = malloc(dec_size)) == NULL) {
1820 memprintf(err, "memory allocation error");
1821 return 0;
1822 }
1823
1824 dec_size = base64dec(arg->data.str.area, arg->data.str.data, dec, dec_size);
1825 if (dec_size < 0) {
1826 memprintf(err, "argument needs to be base64 encoded, and "
1827 "can either be a string or a variable");
1828 free(dec);
1829 return 0;
1830 }
1831
1832 /* base64 decoded */
1833 chunk_destroy(&arg->data.str);
1834 arg->data.str.area = dec;
1835 arg->data.str.data = dec_size;
1836 return 1;
1837}
1838
Ilya Shipitsin2c481d02021-03-26 23:35:31 +05001839#ifdef EVP_CIPH_GCM_MODE
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001840static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
1841 const char *file, int line, char **err)
1842{
1843 switch(args[0].data.sint) {
1844 case 128:
1845 case 192:
1846 case 256:
1847 break;
1848 default:
1849 memprintf(err, "key size must be 128, 192 or 256 (bits).");
1850 return 0;
1851 }
Dragan Dosen9e8db132021-02-22 10:03:53 +01001852
1853 /* Try to decode variables. */
1854 if (!sample_check_arg_base64(&args[1], err)) {
1855 memprintf(err, "failed to parse nonce : %s", *err);
1856 return 0;
1857 }
1858 if (!sample_check_arg_base64(&args[2], err)) {
1859 memprintf(err, "failed to parse key : %s", *err);
1860 return 0;
1861 }
1862 if (!sample_check_arg_base64(&args[3], err)) {
1863 memprintf(err, "failed to parse aead_tag : %s", *err);
1864 return 0;
1865 }
1866
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001867 return 1;
1868}
1869
1870/* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
1871static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
1872{
1873 struct sample nonce, key, aead_tag;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001874 struct buffer *smp_trash = NULL, *smp_trash_alloc = NULL;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001875 EVP_CIPHER_CTX *ctx;
1876 int dec_size, ret;
1877
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001878 smp_trash_alloc = alloc_trash_chunk();
1879 if (!smp_trash_alloc)
1880 return 0;
1881
Dragan Dosen9e8db132021-02-22 10:03:53 +01001882 /* smp copy */
1883 smp_trash_alloc->data = smp->data.u.str.data;
1884 if (unlikely(smp_trash_alloc->data > smp_trash_alloc->size))
1885 smp_trash_alloc->data = smp_trash_alloc->size;
1886 memcpy(smp_trash_alloc->area, smp->data.u.str.area, smp_trash_alloc->data);
1887
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001888 ctx = EVP_CIPHER_CTX_new();
1889
1890 if (!ctx)
1891 goto err;
1892
Dragan Dosen9e8db132021-02-22 10:03:53 +01001893 smp_trash = alloc_trash_chunk();
1894 if (!smp_trash)
1895 goto err;
1896
1897 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
1898 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001899 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001900
1901 if (arg_p[1].type == ARGT_VAR) {
1902 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
1903 if (dec_size < 0)
1904 goto err;
1905 smp_trash->data = dec_size;
1906 nonce.data.u.str = *smp_trash;
1907 }
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001908
1909 /* Set cipher type and mode */
1910 switch(arg_p[0].data.sint) {
1911 case 128:
1912 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
1913 break;
1914 case 192:
1915 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
1916 break;
1917 case 256:
1918 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
1919 break;
1920 }
1921
Dragan Dosen9e8db132021-02-22 10:03:53 +01001922 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, nonce.data.u.str.data, NULL);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001923
1924 /* Initialise IV */
Dragan Dosen9e8db132021-02-22 10:03:53 +01001925 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) nonce.data.u.str.area))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001926 goto err;
1927
Dragan Dosen9e8db132021-02-22 10:03:53 +01001928 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
1929 if (!sample_conv_var2smp_str(&arg_p[2], &key))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001930 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001931
1932 if (arg_p[2].type == ARGT_VAR) {
1933 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
1934 if (dec_size < 0)
1935 goto err;
1936 smp_trash->data = dec_size;
1937 key.data.u.str = *smp_trash;
1938 }
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001939
1940 /* Initialise key */
Dragan Dosen9e8db132021-02-22 10:03:53 +01001941 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) key.data.u.str.area, NULL))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001942 goto err;
1943
1944 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
Dragan Dosen9e8db132021-02-22 10:03:53 +01001945 (unsigned char *) smp_trash_alloc->area, (int) smp_trash_alloc->data))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001946 goto err;
1947
Dragan Dosen9e8db132021-02-22 10:03:53 +01001948 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
1949 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001950 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001951
1952 if (arg_p[3].type == ARGT_VAR) {
1953 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
1954 if (dec_size < 0)
1955 goto err;
1956 smp_trash_alloc->data = dec_size;
1957 aead_tag.data.u.str = *smp_trash_alloc;
1958 }
1959
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001960 dec_size = smp_trash->data;
1961
Dragan Dosen9e8db132021-02-22 10:03:53 +01001962 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 +02001963 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
1964
1965 if (ret <= 0)
1966 goto err;
1967
1968 smp->data.u.str.data = dec_size + smp_trash->data;
1969 smp->data.u.str.area = smp_trash->area;
1970 smp->data.type = SMP_T_BIN;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001971 smp_dup(smp);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001972 free_trash_chunk(smp_trash_alloc);
Dragan Dosen9e8db132021-02-22 10:03:53 +01001973 free_trash_chunk(smp_trash);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001974 return 1;
1975
1976err:
1977 free_trash_chunk(smp_trash_alloc);
Dragan Dosen9e8db132021-02-22 10:03:53 +01001978 free_trash_chunk(smp_trash);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001979 return 0;
1980}
Ilya Shipitsin2c481d02021-03-26 23:35:31 +05001981#endif
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001982
Patrick Gansterer8e366512020-04-22 16:47:57 +02001983static int check_crypto_digest(struct arg *args, struct sample_conv *conv,
1984 const char *file, int line, char **err)
1985{
1986 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1987
1988 if (evp)
1989 return 1;
1990
1991 memprintf(err, "algorithm must be a valid OpenSSL message digest name.");
1992 return 0;
1993}
1994
1995static int sample_conv_crypto_digest(const struct arg *args, struct sample *smp, void *private)
1996{
1997 struct buffer *trash = get_trash_chunk();
1998 unsigned char *md = (unsigned char*) trash->area;
1999 unsigned int md_len = trash->size;
2000 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
2001 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
2002
2003 if (!ctx)
2004 return 0;
2005
2006 if (!EVP_DigestInit_ex(ctx, evp, NULL) ||
2007 !EVP_DigestUpdate(ctx, smp->data.u.str.area, smp->data.u.str.data) ||
2008 !EVP_DigestFinal_ex(ctx, md, &md_len)) {
2009 EVP_MD_CTX_free(ctx);
2010 return 0;
2011 }
2012
2013 EVP_MD_CTX_free(ctx);
2014
2015 trash->data = md_len;
2016 smp->data.u.str = *trash;
2017 smp->data.type = SMP_T_BIN;
2018 smp->flags &= ~SMP_F_CONST;
2019 return 1;
2020}
2021
2022static int check_crypto_hmac(struct arg *args, struct sample_conv *conv,
2023 const char *file, int line, char **err)
2024{
2025 if (!check_crypto_digest(args, conv, file, line, err))
2026 return 0;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002027
2028 if (!sample_check_arg_base64(&args[1], err)) {
2029 memprintf(err, "failed to parse key : %s", *err);
2030 return 0;
2031 }
Patrick Gansterer8e366512020-04-22 16:47:57 +02002032
Patrick Gansterer8e366512020-04-22 16:47:57 +02002033 return 1;
2034}
2035
2036static int sample_conv_crypto_hmac(const struct arg *args, struct sample *smp, void *private)
2037{
2038 struct sample key;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002039 struct buffer *trash = NULL, *key_trash = NULL;
Patrick Gansterer8e366512020-04-22 16:47:57 +02002040 unsigned char *md;
2041 unsigned int md_len;
2042 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
2043 int dec_size;
2044
2045 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
2046 if (!sample_conv_var2smp_str(&args[1], &key))
2047 return 0;
2048
Dragan Dosen9e8db132021-02-22 10:03:53 +01002049 if (args[1].type == ARGT_VAR) {
2050 key_trash = alloc_trash_chunk();
2051 if (!key_trash)
2052 goto err;
2053
2054 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, key_trash->area, key_trash->size);
2055 if (dec_size < 0)
2056 goto err;
2057 key_trash->data = dec_size;
2058 key.data.u.str = *key_trash;
2059 }
Patrick Gansterer8e366512020-04-22 16:47:57 +02002060
Dragan Dosen9e8db132021-02-22 10:03:53 +01002061 trash = alloc_trash_chunk();
2062 if (!trash)
Patrick Gansterer8e366512020-04-22 16:47:57 +02002063 goto err;
2064
2065 md = (unsigned char*) trash->area;
2066 md_len = trash->size;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002067 if (!HMAC(evp, key.data.u.str.area, key.data.u.str.data, (const unsigned char*) smp->data.u.str.area,
2068 smp->data.u.str.data, md, &md_len))
Patrick Gansterer8e366512020-04-22 16:47:57 +02002069 goto err;
2070
2071 free_trash_chunk(key_trash);
2072
2073 trash->data = md_len;
2074 smp->data.u.str = *trash;
2075 smp->data.type = SMP_T_BIN;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002076 smp_dup(smp);
2077 free_trash_chunk(trash);
Patrick Gansterer8e366512020-04-22 16:47:57 +02002078 return 1;
2079
2080err:
2081 free_trash_chunk(key_trash);
Dragan Dosen9e8db132021-02-22 10:03:53 +01002082 free_trash_chunk(trash);
Patrick Gansterer8e366512020-04-22 16:47:57 +02002083 return 0;
2084}
2085
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02002086#endif /* USE_OPENSSL */
Tim Duesterhusd4376302019-06-17 12:41:44 +02002087
Marcin Deranek40ca09c2021-07-13 14:05:24 +02002088static int sample_conv_be2dec_check(struct arg *args, struct sample_conv *conv,
2089 const char *file, int line, char **err)
2090{
2091 if (args[1].data.sint <= 0 || args[1].data.sint > sizeof(unsigned long long)) {
Willy Tarreauc5d0fc92021-09-15 10:30:40 +02002092 memprintf(err, "chunk_size out of [1..%u] range (%lld)", (uint)sizeof(unsigned long long), args[1].data.sint);
Marcin Deranek40ca09c2021-07-13 14:05:24 +02002093 return 0;
2094 }
2095
2096 if (args[2].data.sint != 0 && args[2].data.sint != 1) {
2097 memprintf(err, "Unsupported truncate value (%lld)", args[2].data.sint);
2098 return 0;
2099 }
2100
2101 return 1;
2102}
2103
2104/* Converts big-endian binary input sample to a string containing an unsigned
2105 * integer number per <chunk_size> input bytes separated with <separator>.
2106 * Optional <truncate> flag indicates if input is truncated at <chunk_size>
2107 * boundaries.
2108 * Arguments: separator (string), chunk_size (integer), truncate (0,1)
2109 */
2110static int sample_conv_be2dec(const struct arg *args, struct sample *smp, void *private)
2111{
2112 struct buffer *trash = get_trash_chunk();
2113 const int last = args[2].data.sint ? smp->data.u.str.data - args[1].data.sint + 1 : smp->data.u.str.data;
2114 int max_size = trash->size - 2;
2115 int i;
2116 int start;
2117 int ptr = 0;
2118 unsigned long long number;
2119 char *pos;
2120
2121 trash->data = 0;
2122
2123 while (ptr < last && trash->data <= max_size) {
2124 start = trash->data;
2125 if (ptr) {
2126 /* Add separator */
2127 memcpy(trash->area + trash->data, args[0].data.str.area, args[0].data.str.data);
2128 trash->data += args[0].data.str.data;
2129 }
2130 else
2131 max_size -= args[0].data.str.data;
2132
2133 /* Add integer */
2134 for (number = 0, i = 0; i < args[1].data.sint && ptr < smp->data.u.str.data; i++)
2135 number = (number << 8) + (unsigned char)smp->data.u.str.area[ptr++];
2136
2137 pos = ulltoa(number, trash->area + trash->data, trash->size - trash->data);
2138 if (pos)
2139 trash->data = pos - trash->area;
2140 else {
2141 trash->data = start;
2142 break;
2143 }
2144 }
2145
2146 smp->data.u.str = *trash;
2147 smp->data.type = SMP_T_STR;
2148 smp->flags &= ~SMP_F_CONST;
2149 return 1;
2150}
2151
Marcin Deranekda0264a2021-07-13 14:08:56 +02002152static int sample_conv_be2hex_check(struct arg *args, struct sample_conv *conv,
2153 const char *file, int line, char **err)
2154{
2155 if (args[1].data.sint <= 0 && (args[0].data.str.data > 0 || args[2].data.sint != 0)) {
2156 memprintf(err, "chunk_size needs to be positive (%lld)", args[1].data.sint);
2157 return 0;
2158 }
2159
2160 if (args[2].data.sint != 0 && args[2].data.sint != 1) {
2161 memprintf(err, "Unsupported truncate value (%lld)", args[2].data.sint);
2162 return 0;
2163 }
2164
2165 return 1;
2166}
2167
2168/* Converts big-endian binary input sample to a hex string containing two hex
2169 * digits per input byte. <separator> is put every <chunk_size> binary input
2170 * bytes if specified. Optional <truncate> flag indicates if input is truncated
2171 * at <chunk_size> boundaries.
2172 * Arguments: separator (string), chunk_size (integer), truncate (0,1)
2173 */
2174static int sample_conv_be2hex(const struct arg *args, struct sample *smp, void *private)
2175{
2176 struct buffer *trash = get_trash_chunk();
2177 int chunk_size = args[1].data.sint;
2178 const int last = args[2].data.sint ? smp->data.u.str.data - chunk_size + 1 : smp->data.u.str.data;
2179 int i;
2180 int max_size;
2181 int ptr = 0;
2182 unsigned char c;
2183
2184 trash->data = 0;
2185 if (args[0].data.str.data == 0 && args[2].data.sint == 0)
2186 chunk_size = smp->data.u.str.data;
2187 max_size = trash->size - 2 * chunk_size;
2188
2189 while (ptr < last && trash->data <= max_size) {
2190 if (ptr) {
2191 /* Add separator */
2192 memcpy(trash->area + trash->data, args[0].data.str.area, args[0].data.str.data);
2193 trash->data += args[0].data.str.data;
2194 }
2195 else
2196 max_size -= args[0].data.str.data;
2197
2198 /* Add hex */
2199 for (i = 0; i < chunk_size && ptr < smp->data.u.str.data; i++) {
2200 c = smp->data.u.str.area[ptr++];
2201 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
2202 trash->area[trash->data++] = hextab[c & 0xF];
2203 }
2204 }
2205
2206 smp->data.u.str = *trash;
2207 smp->data.type = SMP_T_STR;
2208 smp->flags &= ~SMP_F_CONST;
2209 return 1;
2210}
2211
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002212static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002213{
Willy Tarreau83061a82018-07-13 11:56:34 +02002214 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002215 unsigned char c;
2216 int ptr = 0;
2217
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002218 trash->data = 0;
2219 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
2220 c = smp->data.u.str.area[ptr++];
2221 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
2222 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002223 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002224 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002225 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002226 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002227 return 1;
2228}
2229
Dragan Dosen3f957b22017-10-24 09:27:34 +02002230static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
2231{
2232 long long int n = 0;
2233 int i, c;
2234
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002235 for (i = 0; i < smp->data.u.str.data; i++) {
2236 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02002237 return 0;
2238 n = (n << 4) + c;
2239 }
2240
2241 smp->data.u.sint = n;
2242 smp->data.type = SMP_T_SINT;
2243 smp->flags &= ~SMP_F_CONST;
2244 return 1;
2245}
2246
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002247/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002248static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002249{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002250 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
2251 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002252 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002253 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002254 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002255 return 1;
2256}
2257
Willy Tarreau60a2ee72017-12-15 07:13:48 +01002258static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002259{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002260 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002261 smp->data.u.sint = i;
2262 smp->data.type = SMP_T_SINT;
2263 return 1;
2264}
2265
2266
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002267static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002268{
2269 int i;
2270
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002271 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002272 return 0;
2273
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002274 for (i = 0; i < smp->data.u.str.data; i++) {
2275 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
2276 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01002277 }
2278 return 1;
2279}
2280
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002281static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002282{
2283 int i;
2284
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002285 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02002286 return 0;
2287
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002288 for (i = 0; i < smp->data.u.str.data; i++) {
2289 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
2290 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01002291 }
2292 return 1;
2293}
2294
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002295/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
2296static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002297{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002298 /* Attempt to convert to IPv4 to apply the correct mask. */
2299 c_ipv62ip(smp);
2300
2301 if (smp->data.type == SMP_T_IPV4) {
2302 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
2303 smp->data.type = SMP_T_IPV4;
2304 }
2305 else if (smp->data.type == SMP_T_IPV6) {
2306 /* IPv6 cannot be converted without an IPv6 mask. */
2307 if (args[1].type != ARGT_IPV6)
2308 return 0;
2309
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +01002310 write_u64(&smp->data.u.ipv6.s6_addr[0],
2311 read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0]));
2312 write_u64(&smp->data.u.ipv6.s6_addr[8],
2313 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 +01002314 smp->data.type = SMP_T_IPV6;
2315 }
2316
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002317 return 1;
2318}
2319
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002320/* takes an UINT value on input supposed to represent the time since EPOCH,
2321 * adds an optional offset found in args[1] and emits a string representing
2322 * the local time in the format specified in args[1] using strftime().
2323 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002324static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002325{
Willy Tarreau83061a82018-07-13 11:56:34 +02002326 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002327 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002328 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002329 struct tm tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002330
2331 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002332 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002333 curr_date += args[1].data.sint;
2334
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002335 get_localtime(curr_date, &tm);
2336
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002337 temp = get_trash_chunk();
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002338 temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002339 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002340 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002341 return 1;
2342}
2343
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002344/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002345static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002346{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002347 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
2348 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002349 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002350 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002351 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002352 return 1;
2353}
2354
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002355/* takes an UINT value on input supposed to represent the time since EPOCH,
2356 * adds an optional offset found in args[1] and emits a string representing
2357 * the UTC date in the format specified in args[1] using strftime().
2358 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002359static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002360{
Willy Tarreau83061a82018-07-13 11:56:34 +02002361 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002362 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002363 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002364 struct tm tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002365
2366 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002367 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002368 curr_date += args[1].data.sint;
2369
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002370 get_gmtime(curr_date, &tm);
2371
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002372 temp = get_trash_chunk();
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002373 temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002374 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002375 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002376 return 1;
2377}
2378
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002379/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002380static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002381{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002382 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
2383 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002384 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002385 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002386 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002387 return 1;
2388}
2389
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002390/* hashes the binary input into a 32-bit unsigned int using xxh.
2391 * The seed of the hash defaults to 0 but can be changd in argument 1.
2392 */
2393static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
2394{
2395 unsigned int seed;
2396
Christopher Faulete6e7a582021-01-29 11:29:28 +01002397 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002398 seed = arg_p->data.sint;
2399 else
2400 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002401 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
2402 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002403 smp->data.type = SMP_T_SINT;
2404 return 1;
2405}
2406
2407/* hashes the binary input into a 64-bit unsigned int using xxh.
2408 * In fact, the function returns a 64 bit unsigned, but the sample
2409 * storage of haproxy only proposes 64-bits signed, so the value is
2410 * cast as signed. This cast doesn't impact the hash repartition.
2411 * The seed of the hash defaults to 0 but can be changd in argument 1.
2412 */
2413static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
2414{
2415 unsigned long long int seed;
2416
Christopher Faulete6e7a582021-01-29 11:29:28 +01002417 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002418 seed = (unsigned long long int)arg_p->data.sint;
2419 else
2420 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002421 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
2422 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002423 smp->data.type = SMP_T_SINT;
2424 return 1;
2425}
2426
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002427static int sample_conv_xxh3(const struct arg *arg_p, struct sample *smp, void *private)
2428{
2429 unsigned long long int seed;
2430
Christopher Faulete6e7a582021-01-29 11:29:28 +01002431 if (arg_p->data.sint)
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002432 seed = (unsigned long long int)arg_p->data.sint;
2433 else
2434 seed = 0;
2435 smp->data.u.sint = (long long int)XXH3(smp->data.u.str.area,
2436 smp->data.u.str.data, seed);
2437 smp->data.type = SMP_T_SINT;
2438 return 1;
2439}
2440
Willy Tarreau80599772015-01-20 19:35:24 +01002441/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002442static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01002443{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002444 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
2445 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002446 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002447 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002448 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01002449 return 1;
2450}
2451
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002452/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
2453static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
2454{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002455 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
2456 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002457 if (arg_p->data.sint)
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002458 smp->data.u.sint = full_hash(smp->data.u.sint);
2459 smp->data.type = SMP_T_SINT;
2460 return 1;
2461}
2462
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002463/* This function escape special json characters. The returned string can be
2464 * safely set between two '"' and used as json string. The json string is
2465 * defined like this:
2466 *
2467 * any Unicode character except '"' or '\' or control character
2468 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
2469 *
2470 * The enum input_type contain all the allowed mode for decoding the input
2471 * string.
2472 */
2473enum input_type {
2474 IT_ASCII = 0,
2475 IT_UTF8,
2476 IT_UTF8S,
2477 IT_UTF8P,
2478 IT_UTF8PS,
2479};
William Dauchy5417e892021-01-08 21:57:41 +01002480
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002481static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
2482 const char *file, int line, char **err)
2483{
Christopher Faulet95917132020-08-05 23:07:07 +02002484 enum input_type type;
2485
Christopher Faulet95917132020-08-05 23:07:07 +02002486 if (strcmp(arg->data.str.area, "") == 0)
2487 type = IT_ASCII;
2488 else if (strcmp(arg->data.str.area, "ascii") == 0)
2489 type = IT_ASCII;
2490 else if (strcmp(arg->data.str.area, "utf8") == 0)
2491 type = IT_UTF8;
2492 else if (strcmp(arg->data.str.area, "utf8s") == 0)
2493 type = IT_UTF8S;
2494 else if (strcmp(arg->data.str.area, "utf8p") == 0)
2495 type = IT_UTF8P;
2496 else if (strcmp(arg->data.str.area, "utf8ps") == 0)
2497 type = IT_UTF8PS;
2498 else {
2499 memprintf(err, "Unexpected input code type. "
2500 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
2501 return 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002502 }
2503
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002504 chunk_destroy(&arg->data.str);
Christopher Faulet95917132020-08-05 23:07:07 +02002505 arg->type = ARGT_SINT;
2506 arg->data.sint = type;
2507 return 1;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002508}
2509
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002510static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002511{
Willy Tarreau83061a82018-07-13 11:56:34 +02002512 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002513 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
2514 const char *str;
2515 int len;
2516 enum input_type input_type = IT_ASCII;
2517 unsigned int c;
2518 unsigned int ret;
2519 char *p;
2520
Christopher Faulete6e7a582021-01-29 11:29:28 +01002521 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002522
2523 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002524 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002525
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002526 p = smp->data.u.str.area;
2527 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002528
2529 if (input_type == IT_ASCII) {
2530 /* Read input as ASCII. */
2531 c = *(unsigned char *)p;
2532 p++;
2533 }
2534 else {
2535 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002536 ret = utf8_next(p,
2537 smp->data.u.str.data - ( p - smp->data.u.str.area),
2538 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002539 p += utf8_return_length(ret);
2540
2541 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2542 return 0;
2543 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2544 continue;
2545 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2546 return 0;
2547 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2548 continue;
2549
2550 /* Check too big values. */
2551 if ((unsigned int)c > 0xffff) {
2552 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2553 return 0;
2554 continue;
2555 }
2556 }
2557
2558 /* Convert character. */
2559 if (c == '"') {
2560 len = 2;
2561 str = "\\\"";
2562 }
2563 else if (c == '\\') {
2564 len = 2;
2565 str = "\\\\";
2566 }
2567 else if (c == '/') {
2568 len = 2;
2569 str = "\\/";
2570 }
2571 else if (c == '\b') {
2572 len = 2;
2573 str = "\\b";
2574 }
2575 else if (c == '\f') {
2576 len = 2;
2577 str = "\\f";
2578 }
2579 else if (c == '\r') {
2580 len = 2;
2581 str = "\\r";
2582 }
2583 else if (c == '\n') {
2584 len = 2;
2585 str = "\\n";
2586 }
2587 else if (c == '\t') {
2588 len = 2;
2589 str = "\\t";
2590 }
Willy Tarreau90807112020-02-25 08:16:33 +01002591 else if (c > 0xff || !isprint((unsigned char)c)) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002592 /* isprint generate a segfault if c is too big. The man says that
2593 * c must have the value of an unsigned char or EOF.
2594 */
2595 len = 6;
2596 _str[0] = '\\';
2597 _str[1] = 'u';
2598 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2599 str = _str;
2600 }
2601 else {
2602 len = 1;
Willy Tarreau5715da22020-02-25 08:37:37 +01002603 _str[0] = c;
2604 str = _str;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002605 }
2606
2607 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002608 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002609 return 0;
2610
2611 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002612 memcpy(temp->area + temp->data, str, len);
2613 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002614 }
2615
2616 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002617 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002618 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002619
2620 return 1;
2621}
2622
Emeric Brun54c4ac82014-11-03 15:32:43 +01002623/* This sample function is designed to extract some bytes from an input buffer.
2624 * First arg is the offset.
2625 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002626static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002627{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002628 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2629 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002630 return 1;
2631 }
2632
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002633 if (smp->data.u.str.size)
2634 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002635 smp->data.u.str.data -= arg_p[0].data.sint;
2636 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002637
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002638 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2639 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002640
2641 return 1;
2642}
2643
Emeric Brunf399b0d2014-11-03 17:07:03 +01002644static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2645 const char *file, int line, char **err)
2646{
2647 struct arg *arg = args;
2648
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002649 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002650 memprintf(err, "Unexpected arg type");
2651 return 0;
2652 }
2653
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002654 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002655 memprintf(err, "Unexpected value 0 for index");
2656 return 0;
2657 }
2658
2659 arg++;
2660
2661 if (arg->type != ARGT_STR) {
2662 memprintf(err, "Unexpected arg type");
2663 return 0;
2664 }
2665
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002666 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002667 memprintf(err, "Empty separators list");
2668 return 0;
2669 }
2670
2671 return 1;
2672}
2673
2674/* This sample function is designed to a return selected part of a string (field).
2675 * First arg is the index of the field (start at 1)
2676 * Second arg is a char list of separators (type string)
2677 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002678static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002679{
Marcin Deranek9631a282018-04-16 14:30:46 +02002680 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002681 char *start, *end;
2682 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002683 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002684
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002685 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002686 return 0;
2687
Marcin Deranek9631a282018-04-16 14:30:46 +02002688 if (arg_p[0].data.sint < 0) {
2689 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002690 end = start = smp->data.u.str.area + smp->data.u.str.data;
2691 while (start > smp->data.u.str.area) {
2692 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2693 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002694 if (field == arg_p[0].data.sint) {
2695 if (count == 1)
2696 goto found;
2697 else if (count > 1)
2698 count--;
2699 } else {
2700 end = start-1;
2701 field--;
2702 }
2703 break;
2704 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002705 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002706 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002707 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002708 } else {
2709 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002710 end = start = smp->data.u.str.area;
2711 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2712 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2713 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002714 if (field == arg_p[0].data.sint) {
2715 if (count == 1)
2716 goto found;
2717 else if (count > 1)
2718 count--;
2719 } else {
2720 start = end+1;
2721 field++;
2722 }
2723 break;
2724 }
2725 }
2726 end++;
2727 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002728 }
2729
2730 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002731 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002732 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002733 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002734 }
2735found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002736 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002737 /* If ret string is len 0, no need to
2738 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002739 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002740 return 1;
2741
Emeric Brunf399b0d2014-11-03 17:07:03 +01002742 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002743 Note: smp->data.u.str.size cannot be set to 0 */
2744 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002745 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002746
Thayne McCombsb2843052021-04-11 23:26:59 -06002747 smp->data.u.str.area = start;
2748
Emeric Brunf399b0d2014-11-03 17:07:03 +01002749 return 1;
2750}
2751
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002752/* This sample function is designed to return a word from a string.
2753 * First arg is the index of the word (start at 1)
2754 * Second arg is a char list of words separators (type string)
2755 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002756static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002757{
Marcin Deranek9631a282018-04-16 14:30:46 +02002758 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002759 char *start, *end;
2760 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002761 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002762
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002763 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002764 return 0;
2765
2766 word = 0;
2767 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002768 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002769 end = start = smp->data.u.str.area + smp->data.u.str.data;
2770 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002771 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002772 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2773 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002774 issep = 1;
2775 break;
2776 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002777 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002778 if (!inword) {
2779 if (!issep) {
2780 if (word != arg_p[0].data.sint) {
2781 word--;
2782 end = start;
2783 }
2784 inword = 1;
2785 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002786 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002787 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002788 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002789 if (count == 1)
2790 goto found;
2791 else if (count > 1)
2792 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002793 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002794 inword = 0;
2795 }
2796 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002797 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002798 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002799 end = start = smp->data.u.str.area;
2800 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002801 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002802 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2803 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002804 issep = 1;
2805 break;
2806 }
2807 }
2808 if (!inword) {
2809 if (!issep) {
2810 if (word != arg_p[0].data.sint) {
2811 word++;
2812 start = end;
2813 }
2814 inword = 1;
2815 }
2816 }
2817 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002818 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002819 if (count == 1)
2820 goto found;
2821 else if (count > 1)
2822 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002823 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002824 inword = 0;
2825 }
2826 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002827 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002828 }
2829
2830 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002831 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002832 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002833 return 1;
2834 }
2835found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002836 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002837 /* If ret string is len 0, no need to
2838 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002839 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002840 return 1;
2841
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002842 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002843
2844 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002845 Note: smp->data.u.str.size cannot be set to 0 */
2846 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002847 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002848
2849 return 1;
2850}
2851
Willy Tarreau7eda8492015-01-20 19:47:06 +01002852static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2853 const char *file, int line, char **err)
2854{
2855 struct arg *arg = args;
2856 char *p;
2857 int len;
2858
2859 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2860 arg[0].type_flags = 0;
2861
2862 if (arg[2].type != ARGT_STR)
2863 return 1;
2864
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002865 p = arg[2].data.str.area;
2866 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002867 while (len) {
2868 if (*p == 'i') {
2869 arg[0].type_flags |= ARGF_REG_ICASE;
2870 }
2871 else if (*p == 'g') {
2872 arg[0].type_flags |= ARGF_REG_GLOB;
2873 }
2874 else {
2875 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2876 return 0;
2877 }
2878 p++;
2879 len--;
2880 }
2881 return 1;
2882}
2883
2884/* This sample function is designed to do the equivalent of s/match/replace/ on
2885 * the input string. It applies a regex and restarts from the last matched
2886 * location until nothing matches anymore. First arg is the regex to apply to
2887 * the input string, second arg is the replacement expression.
2888 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002889static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002890{
2891 char *start, *end;
2892 struct my_regex *reg = arg_p[0].data.reg;
2893 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002894 struct buffer *trash = get_trash_chunk();
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002895 struct buffer *output;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002896 int flag, max;
2897 int found;
2898
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002899 start = smp->data.u.str.area;
2900 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002901
2902 flag = 0;
2903 while (1) {
2904 /* check for last round which is used to copy remaining parts
2905 * when not running in global replacement mode.
2906 */
2907 found = 0;
2908 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2909 /* Note: we can have start == end on empty strings or at the end */
2910 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2911 }
2912
2913 if (!found)
2914 pmatch[0].rm_so = end - start;
2915
2916 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002917 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002918 if (max && pmatch[0].rm_so > 0) {
2919 if (max > pmatch[0].rm_so)
2920 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002921 memcpy(trash->area + trash->data, start, max);
2922 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002923 }
2924
2925 if (!found)
2926 break;
2927
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002928 output = alloc_trash_chunk();
Willy Tarreau23997da2020-02-18 14:27:44 +01002929 if (!output)
2930 break;
2931
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002932 output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2933
Willy Tarreau7eda8492015-01-20 19:47:06 +01002934 /* replace the matching part */
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002935 max = output->size - output->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002936 if (max) {
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002937 if (max > output->data)
2938 max = output->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002939 memcpy(trash->area + trash->data,
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002940 output->area, max);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002941 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002942 }
2943
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002944 free_trash_chunk(output);
2945
Willy Tarreau7eda8492015-01-20 19:47:06 +01002946 /* stop here if we're done with this string */
2947 if (start >= end)
2948 break;
2949
2950 /* We have a special case for matches of length 0 (eg: "x*y*").
2951 * These ones are considered to match in front of a character,
2952 * so we have to copy that character and skip to the next one.
2953 */
2954 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002955 if (trash->data < trash->size)
2956 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002957 pmatch[0].rm_eo++;
2958 }
2959
2960 start += pmatch[0].rm_eo;
2961 flag |= REG_NOTBOL;
2962 }
2963
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002964 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002965 return 1;
2966}
2967
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002968/* This function check an operator entry. It expects a string.
2969 * The string can be an integer or a variable name.
2970 */
2971static int check_operator(struct arg *args, struct sample_conv *conv,
2972 const char *file, int line, char **err)
2973{
2974 const char *str;
2975 const char *end;
Christopher Faulet95917132020-08-05 23:07:07 +02002976 long long int i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002977
2978 /* Try to decode a variable. */
2979 if (vars_check_arg(&args[0], NULL))
2980 return 1;
2981
2982 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002983 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002984 end = str + strlen(str);
Christopher Faulet95917132020-08-05 23:07:07 +02002985 i = read_int64(&str, end);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002986 if (*str != '\0') {
2987 memprintf(err, "expects an integer or a variable name");
2988 return 0;
2989 }
Christopher Faulet95917132020-08-05 23:07:07 +02002990
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002991 chunk_destroy(&args[0].data.str);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002992 args[0].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02002993 args[0].data.sint = i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002994 return 1;
2995}
2996
Willy Tarreau6204cd92016-03-10 16:33:04 +01002997/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002998 * If the arg contain an integer, the integer is returned in the
2999 * sample. If the arg contains a variable descriptor, it returns the
3000 * variable value.
3001 *
3002 * This function returns 0 if an error occurs, otherwise it returns 1.
3003 */
Willy Tarreaud9be5992021-10-06 15:19:05 +02003004int sample_conv_var2smp_sint(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003005{
3006 switch (arg->type) {
3007 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003008 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003009 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003010 return 1;
3011 case ARGT_VAR:
Willy Tarreau2476ff12021-10-06 15:30:52 +02003012 return sample_conv_var2smp(&arg->data.var, smp, SMP_T_SINT);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003013 default:
3014 return 0;
3015 }
3016}
3017
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003018/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01003019 * result.
3020 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003021static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003022{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003023 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003024 return 1;
3025}
3026
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003027/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003028 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01003029 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003030static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003031{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003032 struct sample tmp;
3033
Willy Tarreau7560dd42016-03-10 16:28:58 +01003034 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02003035 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003036 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003037 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003038 return 1;
3039}
3040
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003041/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003042 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01003043 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003044static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003045{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003046 struct sample tmp;
3047
Willy Tarreau7560dd42016-03-10 16:28:58 +01003048 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02003049 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003050 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003051 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003052 return 1;
3053}
3054
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003055/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003056 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01003057 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003058static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003059{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003060 struct sample tmp;
3061
Willy Tarreau7560dd42016-03-10 16:28:58 +01003062 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02003063 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003064 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003065 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003066 return 1;
3067}
3068
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003069static inline long long int arith_add(long long int a, long long int b)
3070{
3071 /* Prevent overflow and makes capped calculus.
3072 * We must ensure that the check calculus doesn't
3073 * exceed the signed 64 bits limits.
3074 *
3075 * +----------+----------+
3076 * | a<0 | a>=0 |
3077 * +------+----------+----------+
3078 * | b<0 | MIN-a>b | no check |
3079 * +------+----------+----------+
3080 * | b>=0 | no check | MAX-a<b |
3081 * +------+----------+----------+
3082 */
3083 if ((a ^ b) >= 0) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003084 /* signs are different. */
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003085 if (a < 0) {
3086 if (LLONG_MIN - a > b)
3087 return LLONG_MIN;
3088 }
3089 if (LLONG_MAX - a < b)
3090 return LLONG_MAX;
3091 }
3092 return a + b;
3093}
3094
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003095/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003096 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01003097 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003098static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003099{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003100 struct sample tmp;
3101
Willy Tarreau7560dd42016-03-10 16:28:58 +01003102 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02003103 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003104 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003105 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01003106 return 1;
3107}
3108
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003109/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003110 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01003111 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003112static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003113 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003114{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003115 struct sample tmp;
3116
Willy Tarreau7560dd42016-03-10 16:28:58 +01003117 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02003118 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003119 return 0;
3120
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003121 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
3122 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
3123 * of -LLONG_MIN and correct the result.
3124 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003125 if (tmp.data.u.sint == LLONG_MIN) {
3126 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
3127 if (smp->data.u.sint < LLONG_MAX)
3128 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003129 return 1;
3130 }
3131
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003132 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003133 * the second operand.
3134 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003135 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01003136 return 1;
3137}
3138
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003139/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003140 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003141 * If the result makes an overflow, then the largest possible quantity is
3142 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003143 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003144static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003145 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003146{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003147 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003148 long long int c;
3149
Willy Tarreau7560dd42016-03-10 16:28:58 +01003150 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02003151 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003152 return 0;
3153
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003154 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003155 if (!smp->data.u.sint || !tmp.data.u.sint) {
3156 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003157 return 1;
3158 }
3159
3160 /* The multiply between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003161 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003162 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003163 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3164 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003165 return 1;
3166 }
3167
3168 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003169 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003170
3171 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003172 if (smp->data.u.sint != c / tmp.data.u.sint) {
3173 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
3174 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003175 return 1;
3176 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003177 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003178 return 1;
3179 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003180 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01003181 return 1;
3182}
3183
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003184/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003185 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003186 * If arg_p makes the result overflow, then the largest possible quantity is
3187 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003188 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003189static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003190 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003191{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003192 struct sample tmp;
3193
Willy Tarreau7560dd42016-03-10 16:28:58 +01003194 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02003195 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003196 return 0;
3197
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003198 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003199 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003200 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003201 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003202 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3203 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003204 return 1;
3205 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003206 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003207 return 1;
3208 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003209 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01003210 return 1;
3211}
3212
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003213/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003214 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003215 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003216 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003217static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003218 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003219{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003220 struct sample tmp;
3221
Willy Tarreau7560dd42016-03-10 16:28:58 +01003222 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02003223 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003224 return 0;
3225
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003226 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003227 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003228 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003229 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003230 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3231 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003232 return 1;
3233 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003234 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003235 return 1;
3236 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003237 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01003238 return 1;
3239}
3240
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003241/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01003242 * result.
3243 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003244static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003245 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003246{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003247 if (smp->data.u.sint == LLONG_MIN)
3248 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003249 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003250 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003251 return 1;
3252}
3253
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003254/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003255 * false. The output is a BOOL.
3256 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003257static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003258 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003259{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003260 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003261 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003262 return 1;
3263}
3264
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003265/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003266 * truee. The output is a BOOL.
3267 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003268static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003269 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003270{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003271 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003272 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003273 return 1;
3274}
3275
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003276/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003277 * The output is a BOOL.
3278 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003279static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003280 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003281{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003282 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003283 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003284 return 1;
3285}
3286
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003287/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003288 * The output is a BOOL.
3289 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003290static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003291 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003292{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003293 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003294 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003295 return 1;
3296}
3297
Willy Tarreau280f42b2018-02-19 15:34:12 +01003298/* appends an optional const string, an optional variable contents and another
3299 * optional const string to an existing string.
3300 */
3301static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
3302{
Willy Tarreau83061a82018-07-13 11:56:34 +02003303 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003304 struct sample tmp;
3305 int max;
3306
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003307 trash = alloc_trash_chunk();
William Dauchye9970102021-01-11 11:05:58 +01003308 if (!trash)
3309 return 0;
3310
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003311 trash->data = smp->data.u.str.data;
3312 if (trash->data > trash->size - 1)
3313 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003314
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003315 memcpy(trash->area, smp->data.u.str.area, trash->data);
3316 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003317
3318 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003319 max = arg_p[0].data.str.data;
3320 if (max > trash->size - 1 - trash->data)
3321 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003322
3323 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003324 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
3325 trash->data += max;
3326 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003327 }
3328
3329 /* append second string (variable) if it's found and we can turn it
3330 * into a string.
3331 */
3332 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaue352b9d2021-09-03 11:52:38 +02003333 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp, NULL) &&
Willy Tarreau280f42b2018-02-19 15:34:12 +01003334 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
3335 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
3336
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003337 max = tmp.data.u.str.data;
3338 if (max > trash->size - 1 - trash->data)
3339 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003340
3341 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003342 memcpy(trash->area + trash->data, tmp.data.u.str.area,
3343 max);
3344 trash->data += max;
3345 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003346 }
3347 }
3348
3349 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003350 max = arg_p[2].data.str.data;
3351 if (max > trash->size - 1 - trash->data)
3352 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003353
3354 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003355 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
3356 trash->data += max;
3357 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003358 }
3359
3360 smp->data.u.str = *trash;
3361 smp->data.type = SMP_T_STR;
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003362 smp_dup(smp);
3363 free_trash_chunk(trash);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003364 return 1;
3365}
3366
3367/* This function checks the "concat" converter's arguments and extracts the
3368 * variable name and its scope.
3369 */
3370static int smp_check_concat(struct arg *args, struct sample_conv *conv,
3371 const char *file, int line, char **err)
3372{
3373 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003374 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
3375 memprintf(err, "failed to register variable name '%s'",
3376 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003377 return 0;
3378 }
3379 return 1;
3380}
3381
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003382/* Compares string with a variable containing a string. Return value
Tim Duesterhusca097c12018-04-27 21:18:45 +02003383 * is compatible with strcmp(3)'s return value.
3384 */
3385static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
3386{
3387 struct sample tmp;
3388 int max, result;
3389
3390 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3391 if (arg_p[0].type != ARGT_VAR)
3392 return 0;
Willy Tarreau2476ff12021-10-06 15:30:52 +02003393
3394 if (!sample_conv_var2smp(&arg_p[0].data.var, &tmp, SMP_T_STR))
Tim Duesterhusca097c12018-04-27 21:18:45 +02003395 return 0;
3396
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003397 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
3398 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003399 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003400 if (smp->data.u.str.data != tmp.data.u.str.data) {
3401 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02003402 result = -1;
3403 }
3404 else {
3405 result = 1;
3406 }
3407 }
3408 }
3409
3410 smp->data.u.sint = result;
3411 smp->data.type = SMP_T_SINT;
3412 return 1;
3413}
3414
Willy Tarreau2a8a2f02021-05-09 11:59:49 +02003415#if defined(HAVE_CRYPTO_memcmp)
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003416/* Compares bytestring with a variable containing a bytestring. Return value
3417 * is `true` if both bytestrings are bytewise identical and `false` otherwise.
3418 *
3419 * Comparison will be performed in constant time if both bytestrings are of
3420 * the same length. If the lengths differ execution time will not be constant.
3421 */
3422static int sample_conv_secure_memcmp(const struct arg *arg_p, struct sample *smp, void *private)
3423{
3424 struct sample tmp;
3425 int result;
3426
3427 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3428 if (arg_p[0].type != ARGT_VAR)
3429 return 0;
Willy Tarreau2476ff12021-10-06 15:30:52 +02003430
3431 if (!sample_conv_var2smp(&arg_p[0].data.var, &tmp, SMP_T_BIN))
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003432 return 0;
3433
3434 if (smp->data.u.str.data != tmp.data.u.str.data) {
3435 smp->data.u.sint = 0;
3436 smp->data.type = SMP_T_BOOL;
3437 return 1;
3438 }
3439
3440 /* The following comparison is performed in constant time. */
3441 result = CRYPTO_memcmp(smp->data.u.str.area, tmp.data.u.str.area, smp->data.u.str.data);
3442
3443 smp->data.u.sint = result == 0;
3444 smp->data.type = SMP_T_BOOL;
3445 return 1;
3446}
3447#endif
3448
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02003449/* Takes a boolean as input. Returns the first argument if that boolean is true and
3450 * the second argument otherwise.
3451 */
3452static int sample_conv_iif(const struct arg *arg_p, struct sample *smp, void *private)
3453{
3454 smp->data.type = SMP_T_STR;
3455 smp->flags |= SMP_F_CONST;
3456
3457 if (smp->data.u.sint) {
3458 smp->data.u.str.data = arg_p[0].data.str.data;
3459 smp->data.u.str.area = arg_p[0].data.str.area;
3460 }
3461 else {
3462 smp->data.u.str.data = arg_p[1].data.str.data;
3463 smp->data.u.str.area = arg_p[1].data.str.area;
3464 }
3465
3466 return 1;
3467}
3468
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003469#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
3470#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
3471#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
3472
3473/*
3474 * Extract the field value of an input binary sample. Takes a mandatory argument:
3475 * the protocol buffers field identifier (dotted notation) internally represented
3476 * as an array of unsigned integers and its size.
3477 * Return 1 if the field was found, 0 if not.
3478 */
3479static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
3480{
3481 unsigned char *pos;
3482 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003483
3484 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003485 grpc_left = smp->data.u.str.data;
3486
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01003487 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003488 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003489
3490 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
3491
3492 pos += GRPC_MSG_HEADER_SZ;
3493 grpc_left -= GRPC_MSG_HEADER_SZ;
3494
3495 if (grpc_left < left)
3496 return 0;
3497
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01003498 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
3499 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003500
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003501 grpc_left -= grpc_msg_len;
3502 }
3503
3504 return 0;
3505}
3506
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003507static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
3508{
3509 unsigned char *pos;
3510 size_t left;
3511
3512 pos = (unsigned char *)smp->data.u.str.area;
3513 left = smp->data.u.str.data;
3514
3515 return protobuf_field_lookup(arg_p, smp, &pos, &left);
3516}
3517
3518static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
3519 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003520{
3521 if (!args[1].type) {
3522 args[1].type = ARGT_SINT;
3523 args[1].data.sint = PBUF_T_BINARY;
3524 }
3525 else {
3526 int pbuf_type;
3527
3528 pbuf_type = protobuf_type(args[1].data.str.area);
3529 if (pbuf_type == -1) {
3530 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
3531 return 0;
3532 }
3533
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003534 chunk_destroy(&args[1].data.str);
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003535 args[1].type = ARGT_SINT;
3536 args[1].data.sint = pbuf_type;
3537 }
3538
3539 return 1;
3540}
3541
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003542/*
3543 * Extract the tag value of an input binary sample. Takes a mandatory argument:
3544 * the FIX protocol tag identifier.
3545 * Return 1 if the tag was found, 0 if not.
3546 */
3547static int sample_conv_fix_tag_value(const struct arg *arg_p, struct sample *smp, void *private)
3548{
3549 struct ist value;
3550
3551 smp->flags &= ~SMP_F_MAY_CHANGE;
3552 value = fix_tag_value(ist2(smp->data.u.str.area, smp->data.u.str.data),
3553 arg_p[0].data.sint);
3554 if (!istlen(value)) {
3555 if (!isttest(value)) {
3556 /* value != IST_NULL, need more data */
3557 smp->flags |= SMP_F_MAY_CHANGE;
3558 }
3559 return 0;
3560 }
3561
3562 smp->data.u.str = ist2buf(value);
3563 smp->flags |= SMP_F_CONST;
3564
3565 return 1;
3566}
3567
3568/* This function checks the "fix_tag_value" converter configuration.
3569 * It expects a "known" (by HAProxy) tag name or ID.
3570 * Tag string names are converted to their ID counterpart because this is the
3571 * format they are sent over the wire.
3572 */
3573static int sample_conv_fix_value_check(struct arg *args, struct sample_conv *conv,
3574 const char *file, int line, char **err)
3575{
3576 struct ist str;
3577 unsigned int tag;
3578
3579 str = ist2(args[0].data.str.area, args[0].data.str.data);
3580 tag = fix_tagid(str);
3581 if (!tag) {
3582 memprintf(err, "Unknown FIX tag name '%s'", args[0].data.str.area);
3583 return 0;
3584 }
3585
3586 chunk_destroy(&args[0].data.str);
3587 args[0].type = ARGT_SINT;
3588 args[0].data.sint = tag;
3589
3590 return 1;
3591}
3592
3593/*
3594 * Checks that a buffer contains a valid FIX message
3595 *
3596 * Return 1 if the check could be run, 0 if not.
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003597 * The result of the analyse itself is stored in <smp> as a boolean
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003598 */
3599static int sample_conv_fix_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3600{
3601 struct ist msg;
3602
3603 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3604
3605 smp->flags &= ~SMP_F_MAY_CHANGE;
3606 switch (fix_validate_message(msg)) {
3607 case FIX_VALID_MESSAGE:
3608 smp->data.type = SMP_T_BOOL;
3609 smp->data.u.sint = 1;
3610 return 1;
3611 case FIX_NEED_MORE_DATA:
3612 smp->flags |= SMP_F_MAY_CHANGE;
3613 return 0;
3614 case FIX_INVALID_MESSAGE:
3615 smp->data.type = SMP_T_BOOL;
3616 smp->data.u.sint = 0;
3617 return 1;
3618 }
3619 return 0;
3620}
3621
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003622/*
3623 * Extract the field value of an input binary sample containing an MQTT packet.
3624 * Takes 2 mandatory arguments:
3625 * - packet type
3626 * - field name
3627 *
3628 * return 1 if the field was found, 0 if not.
3629 */
3630static int sample_conv_mqtt_field_value(const struct arg *arg_p, struct sample *smp, void *private)
3631{
3632 struct ist pkt, value;
3633 int type, fieldname_id;
3634
3635 pkt = ist2(smp->data.u.str.area, smp->data.u.str.data);
3636 type = arg_p[0].data.sint;
3637 fieldname_id = arg_p[1].data.sint;
3638
3639 smp->flags &= ~SMP_F_MAY_CHANGE;
3640 value = mqtt_field_value(pkt, type, fieldname_id);
3641 if (!istlen(value)) {
3642 if (isttest(value)) {
3643 /* value != IST_NULL, need more data */
3644 smp->flags |= SMP_F_MAY_CHANGE;
3645 }
3646 return 0;
3647 }
3648
3649 smp->data.u.str = ist2buf(value);
3650 smp->flags |= SMP_F_CONST;
3651 return 1;
3652}
3653
3654/*
3655 * this function checks the "mqtt_field_value" converter configuration.
3656 * It expects a known packet type name or ID and a field name, in this order
3657 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003658 * Args[0] will be turned into a MQTT_CPT_* value for direct matching when parsing
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003659 * a packet.
3660 */
3661static int sample_conv_mqtt_field_value_check(struct arg *args, struct sample_conv *conv,
3662 const char *file, int line, char **err)
3663{
3664 int type, fieldname_id;
3665
3666 /* check the MQTT packet type is valid */
3667 type = mqtt_typeid(ist2(args[0].data.str.area, args[0].data.str.data));
3668 if (type == MQTT_CPT_INVALID) {
3669 memprintf(err, "Unknown MQTT type '%s'", args[0].data.str.area);
3670 return 0;
3671 }
3672
3673 /* check the field name belongs to the MQTT packet type */
3674 fieldname_id = mqtt_check_type_fieldname(type, ist2(args[1].data.str.area, args[1].data.str.data));
3675 if (fieldname_id == MQTT_FN_INVALID) {
3676 memprintf(err, "Unknown MQTT field name '%s' for packet type '%s'", args[1].data.str.area,
3677 args[0].data.str.area);
3678 return 0;
3679 }
3680
3681 /* save numeric counterparts of type and field name */
3682 chunk_destroy(&args[0].data.str);
3683 chunk_destroy(&args[1].data.str);
3684 args[0].type = ARGT_SINT;
3685 args[0].data.sint = type;
3686 args[1].type = ARGT_SINT;
3687 args[1].data.sint = fieldname_id;
3688
3689 return 1;
3690}
3691
3692/*
3693 * Checks that <smp> contains a valid MQTT message
3694 *
3695 * The function returns 1 if the check was run to its end, 0 otherwise.
3696 * The result of the analyse itself is stored in <smp> as a boolean.
3697 */
3698static int sample_conv_mqtt_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3699{
3700 struct ist msg;
3701
3702 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3703
3704 smp->flags &= ~SMP_F_MAY_CHANGE;
3705 switch (mqtt_validate_message(msg, NULL)) {
3706 case FIX_VALID_MESSAGE:
3707 smp->data.type = SMP_T_BOOL;
3708 smp->data.u.sint = 1;
3709 return 1;
3710 case FIX_NEED_MORE_DATA:
3711 smp->flags |= SMP_F_MAY_CHANGE;
3712 return 0;
3713 case FIX_INVALID_MESSAGE:
3714 smp->data.type = SMP_T_BOOL;
3715 smp->data.u.sint = 0;
3716 return 1;
3717 }
3718 return 0;
3719}
3720
Tim Duesterhusca097c12018-04-27 21:18:45 +02003721/* This function checks the "strcmp" converter's arguments and extracts the
3722 * variable name and its scope.
3723 */
3724static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
3725 const char *file, int line, char **err)
3726{
Willy Tarreaua1169b62021-05-08 06:50:28 +02003727 if (!args[0].data.str.data) {
3728 memprintf(err, "missing variable name");
3729 return 0;
3730 }
3731
Tim Duesterhusca097c12018-04-27 21:18:45 +02003732 /* Try to decode a variable. */
3733 if (vars_check_arg(&args[0], NULL))
3734 return 1;
3735
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003736 memprintf(err, "failed to register variable name '%s'",
3737 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003738 return 0;
3739}
3740
Willy Tarreau2a8a2f02021-05-09 11:59:49 +02003741#if defined(HAVE_CRYPTO_memcmp)
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003742/* This function checks the "secure_memcmp" converter's arguments and extracts the
3743 * variable name and its scope.
3744 */
3745static int smp_check_secure_memcmp(struct arg *args, struct sample_conv *conv,
3746 const char *file, int line, char **err)
3747{
Willy Tarreaua1169b62021-05-08 06:50:28 +02003748 if (!args[0].data.str.data) {
3749 memprintf(err, "missing variable name");
3750 return 0;
3751 }
3752
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003753 /* Try to decode a variable. */
3754 if (vars_check_arg(&args[0], NULL))
3755 return 1;
3756
3757 memprintf(err, "failed to register variable name '%s'",
3758 args[0].data.str.area);
3759 return 0;
3760}
3761#endif
3762
Christopher Faulet4ccc12f2020-04-01 09:08:32 +02003763/**/
3764static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private)
3765{
3766 struct buffer *tmp;
3767 uint32_t n;
3768
3769 n = htonl((uint32_t)smp->data.u.sint);
3770 tmp = get_trash_chunk();
3771
3772 memcpy(b_head(tmp), &n, 4);
3773 b_add(tmp, 4);
3774
3775 smp->data.u.str = *tmp;
3776 smp->data.type = SMP_T_BIN;
3777 return 1;
3778}
3779
Christopher Fauletea159d62020-04-01 16:21:44 +02003780/**/
3781static int sample_conv_cut_crlf(const struct arg *arg_p, struct sample *smp, void *private)
3782{
3783 char *p;
3784 size_t l;
3785
3786 p = smp->data.u.str.area;
3787 for (l = 0; l < smp->data.u.str.data; l++) {
3788 if (*(p+l) == '\r' || *(p+l) == '\n')
3789 break;
3790 }
3791 smp->data.u.str.data = l;
3792 return 1;
3793}
3794
Christopher Faulet51fc9d12020-04-01 17:24:41 +02003795/**/
3796static int sample_conv_ltrim(const struct arg *arg_p, struct sample *smp, void *private)
3797{
3798 char *delimiters, *p;
3799 size_t dlen, l;
3800
3801 delimiters = arg_p[0].data.str.area;
3802 dlen = arg_p[0].data.str.data;
3803
3804 l = smp->data.u.str.data;
3805 p = smp->data.u.str.area;
3806 while (l && memchr(delimiters, *p, dlen) != NULL) {
3807 p++;
3808 l--;
3809 }
3810
3811 smp->data.u.str.area = p;
3812 smp->data.u.str.data = l;
3813 return 1;
3814}
3815
Christopher Faulet568415a2020-04-01 17:24:47 +02003816/**/
3817static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *private)
3818{
3819 char *delimiters, *p;
3820 size_t dlen, l;
3821
3822 delimiters = arg_p[0].data.str.area;
3823 dlen = arg_p[0].data.str.data;
3824
3825 l = smp->data.u.str.data;
3826 p = smp->data.u.str.area + l - 1;
3827 while (l && memchr(delimiters, *p, dlen) != NULL) {
3828 p--;
3829 l--;
3830 }
3831
3832 smp->data.u.str.data = l;
3833 return 1;
3834}
3835
Alex51c8ad42021-04-15 16:45:15 +02003836/* This function checks the "json_query" converter's arguments. */
3837static int sample_check_json_query(struct arg *arg, struct sample_conv *conv,
3838 const char *file, int line, char **err)
3839{
3840 if (arg[0].data.str.data == 0) {
3841 memprintf(err, "json_path must not be empty");
3842 return 0;
3843 }
3844
3845 if (arg[1].data.str.data != 0) {
3846 if (strcmp(arg[1].data.str.area, "int") != 0) {
3847 memprintf(err, "output_type only supports \"int\" as argument");
3848 return 0;
3849 } else {
3850 arg[1].type = ARGT_SINT;
3851 arg[1].data.sint = 0;
3852 }
3853 }
3854 return 1;
3855}
3856
3857/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
3858 * the recommendation for interoperable integers in section 6 of RFC 7159.
3859 */
3860#define JSON_INT_MAX ((1LL << 53) - 1)
3861#define JSON_INT_MIN (-JSON_INT_MAX)
3862
3863/* This sample function get the value from a given json string.
3864 * The mjson library is used to parse the JSON struct
3865 */
3866static int sample_conv_json_query(const struct arg *args, struct sample *smp, void *private)
3867{
3868 struct buffer *trash = get_trash_chunk();
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003869 const char *token; /* holds the temporary string from mjson_find */
3870 int token_size; /* holds the length of <token> */
Alex51c8ad42021-04-15 16:45:15 +02003871
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003872 enum mjson_tok token_type;
Alex51c8ad42021-04-15 16:45:15 +02003873
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003874 token_type = mjson_find(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &token, &token_size);
3875
3876 switch (token_type) {
Alex51c8ad42021-04-15 16:45:15 +02003877 case MJSON_TOK_NUMBER:
3878 if (args[1].type == ARGT_SINT) {
Willy Tarreaufb601952021-05-14 08:51:53 +02003879 smp->data.u.sint = strtoll(token, NULL, 0);
Alex51c8ad42021-04-15 16:45:15 +02003880
3881 if (smp->data.u.sint < JSON_INT_MIN || smp->data.u.sint > JSON_INT_MAX)
3882 return 0;
3883
3884 smp->data.type = SMP_T_SINT;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003885
3886 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003887 } else {
3888 double double_val;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003889
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003890 if (mjson_get_number(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &double_val) == 0)
Alex51c8ad42021-04-15 16:45:15 +02003891 return 0;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003892
3893 trash->data = snprintf(trash->area,trash->size,"%g",double_val);
3894 smp->data.u.str = *trash;
3895 smp->data.type = SMP_T_STR;
3896
3897 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003898 }
Alex51c8ad42021-04-15 16:45:15 +02003899 case MJSON_TOK_TRUE:
3900 smp->data.type = SMP_T_BOOL;
3901 smp->data.u.sint = 1;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003902
3903 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003904 case MJSON_TOK_FALSE:
3905 smp->data.type = SMP_T_BOOL;
3906 smp->data.u.sint = 0;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003907
3908 return 1;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003909 case MJSON_TOK_STRING: {
3910 int len;
3911
3912 len = mjson_get_string(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, trash->area, trash->size);
3913
3914 if (len == -1) {
Alex51c8ad42021-04-15 16:45:15 +02003915 /* invalid string */
3916 return 0;
Alex51c8ad42021-04-15 16:45:15 +02003917 }
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003918
3919 trash->data = len;
3920 smp->data.u.str = *trash;
3921 smp->data.type = SMP_T_STR;
3922
3923 return 1;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003924 }
Tim Duesterhus8f3bc8f2021-04-15 18:14:32 +02003925 case MJSON_TOK_NULL:
3926 case MJSON_TOK_ARRAY:
3927 case MJSON_TOK_OBJECT:
3928 /* We cannot handle these. */
3929 return 0;
3930 case MJSON_TOK_INVALID:
3931 /* Nothing matches the query. */
3932 return 0;
3933 case MJSON_TOK_KEY:
3934 /* This is not a valid return value according to the
3935 * mjson documentation, but we handle it to benefit
3936 * from '-Wswitch'.
3937 */
Alex51c8ad42021-04-15 16:45:15 +02003938 return 0;
3939 }
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003940
3941 my_unreachable();
3942 return 0;
Alex51c8ad42021-04-15 16:45:15 +02003943}
3944
3945
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003946/************************************************************************/
3947/* All supported sample fetch functions must be declared here */
3948/************************************************************************/
3949
3950/* force TRUE to be returned at the fetch level */
3951static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003952smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003953{
Willy Tarreau280f42b2018-02-19 15:34:12 +01003954 if (!smp_make_rw(smp))
3955 return 0;
3956
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003957 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003958 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003959 return 1;
3960}
3961
3962/* force FALSE to be returned at the fetch level */
3963static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003964smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003965{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003966 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003967 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003968 return 1;
3969}
3970
3971/* retrieve environment variable $1 as a string */
3972static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003973smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003974{
3975 char *env;
3976
Christopher Faulete6e7a582021-01-29 11:29:28 +01003977 if (args[0].type != ARGT_STR)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003978 return 0;
3979
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003980 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003981 if (!env)
3982 return 0;
3983
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003984 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003985 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003986 smp->data.u.str.area = env;
3987 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003988 return 1;
3989}
3990
Damien Claisseae6f1252019-10-30 15:57:28 +00003991/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3992 * optional string representing the unit of the result: "s" for seconds, "ms" for
3993 * milliseconds and "us" for microseconds.
3994 * Returns 0 on error and non-zero if OK.
3995 */
3996int smp_check_date_unit(struct arg *args, char **err)
3997{
3998 if (args[1].type == ARGT_STR) {
Christopher Faulet95917132020-08-05 23:07:07 +02003999 long long int unit;
4000
Damien Claisseae6f1252019-10-30 15:57:28 +00004001 if (strcmp(args[1].data.str.area, "s") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02004002 unit = TIME_UNIT_S;
Damien Claisseae6f1252019-10-30 15:57:28 +00004003 }
4004 else if (strcmp(args[1].data.str.area, "ms") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02004005 unit = TIME_UNIT_MS;
Damien Claisseae6f1252019-10-30 15:57:28 +00004006 }
4007 else if (strcmp(args[1].data.str.area, "us") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02004008 unit = TIME_UNIT_US;
Damien Claisseae6f1252019-10-30 15:57:28 +00004009 }
4010 else {
4011 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
4012 args[1].data.str.area);
4013 return 0;
4014 }
Christopher Faulet95917132020-08-05 23:07:07 +02004015
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004016 chunk_destroy(&args[1].data.str);
Damien Claisseae6f1252019-10-30 15:57:28 +00004017 args[1].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02004018 args[1].data.sint = unit;
Damien Claisseae6f1252019-10-30 15:57:28 +00004019 }
4020 else if (args[1].type != ARGT_STOP) {
4021 memprintf(err, "Unexpected arg type");
4022 return 0;
4023 }
4024
4025 return 1;
4026}
4027
4028/* retrieve the current local date in epoch time, converts it to milliseconds
4029 * or microseconds if asked to in optional args[1] unit param, and applies an
4030 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004031 */
4032static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004033smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004034{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004035 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004036
Damien Claisseae6f1252019-10-30 15:57:28 +00004037 /* report in milliseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01004038 if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
Damien Claisseae6f1252019-10-30 15:57:28 +00004039 smp->data.u.sint *= 1000;
4040 smp->data.u.sint += date.tv_usec / 1000;
4041 }
4042 /* report in microseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01004043 else if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
Damien Claisseae6f1252019-10-30 15:57:28 +00004044 smp->data.u.sint *= 1000000;
4045 smp->data.u.sint += date.tv_usec;
4046 }
4047
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004048 /* add offset */
Christopher Faulete6e7a582021-01-29 11:29:28 +01004049 if (args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004050 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004051
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004052 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004053 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4054 return 1;
4055}
4056
Etienne Carrierea792a0a2018-01-17 13:43:24 +01004057/* retrieve the current microsecond part of the date */
4058static int
4059smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
4060{
4061 smp->data.u.sint = date.tv_usec;
4062 smp->data.type = SMP_T_SINT;
4063 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4064 return 1;
4065}
4066
4067
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01004068/* returns the hostname */
4069static int
4070smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
4071{
4072 smp->data.type = SMP_T_STR;
4073 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004074 smp->data.u.str.area = hostname;
4075 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01004076 return 1;
4077}
4078
Willy Tarreau0f30d262014-11-24 16:02:05 +01004079/* returns the number of processes */
4080static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004081smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01004082{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004083 smp->data.type = SMP_T_SINT;
Willy Tarreau91358592021-06-15 08:08:04 +02004084 smp->data.u.sint = 1;
Willy Tarreau0f30d262014-11-24 16:02:05 +01004085 return 1;
4086}
4087
4088/* returns the number of the current process (between 1 and nbproc */
4089static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004090smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01004091{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004092 smp->data.type = SMP_T_SINT;
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004093 smp->data.u.sint = 1;
Willy Tarreau0f30d262014-11-24 16:02:05 +01004094 return 1;
4095}
4096
Christopher Faulet34adb2a2017-11-21 21:45:38 +01004097/* returns the number of the current thread (between 1 and nbthread */
4098static int
4099smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
4100{
4101 smp->data.type = SMP_T_SINT;
4102 smp->data.u.sint = tid;
4103 return 1;
4104}
4105
Willy Tarreau84310e22014-02-14 11:59:04 +01004106/* generate a random 32-bit integer for whatever purpose, with an optional
4107 * range specified in argument.
4108 */
4109static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004110smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01004111{
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01004112 smp->data.u.sint = ha_random32();
Willy Tarreau84310e22014-02-14 11:59:04 +01004113
4114 /* reduce if needed. Don't do a modulo, use all bits! */
Christopher Faulete6e7a582021-01-29 11:29:28 +01004115 if (args[0].type == ARGT_SINT)
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01004116 smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32;
Willy Tarreau84310e22014-02-14 11:59:04 +01004117
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004118 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01004119 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4120 return 1;
4121}
4122
Willy Tarreau0f30d262014-11-24 16:02:05 +01004123/* returns true if the current process is stopping */
4124static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004125smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01004126{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004127 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004128 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01004129 return 1;
4130}
4131
Willy Tarreau70fe9442018-11-22 16:07:39 +01004132/* returns the number of calls of the current stream's process_stream() */
4133static int
4134smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
4135{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004136 if (!smp->strm)
4137 return 0;
4138
Willy Tarreau70fe9442018-11-22 16:07:39 +01004139 smp->data.type = SMP_T_SINT;
4140 smp->data.u.sint = smp->strm->task->calls;
4141 return 1;
4142}
4143
4144/* returns the average number of nanoseconds spent processing the stream per call */
4145static int
4146smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
4147{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004148 if (!smp->strm)
4149 return 0;
4150
Willy Tarreau70fe9442018-11-22 16:07:39 +01004151 smp->data.type = SMP_T_SINT;
4152 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
4153 return 1;
4154}
4155
4156/* returns the total number of nanoseconds spent processing the stream */
4157static int
4158smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
4159{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004160 if (!smp->strm)
4161 return 0;
4162
Willy Tarreau70fe9442018-11-22 16:07:39 +01004163 smp->data.type = SMP_T_SINT;
4164 smp->data.u.sint = smp->strm->task->cpu_time;
4165 return 1;
4166}
4167
4168/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
4169static int
4170smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
4171{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004172 if (!smp->strm)
4173 return 0;
4174
Willy Tarreau70fe9442018-11-22 16:07:39 +01004175 smp->data.type = SMP_T_SINT;
4176 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
4177 return 1;
4178}
4179
4180/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
4181static int
4182smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
4183{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004184 if (!smp->strm)
4185 return 0;
4186
Willy Tarreau70fe9442018-11-22 16:07:39 +01004187 smp->data.type = SMP_T_SINT;
4188 smp->data.u.sint = smp->strm->task->lat_time;
4189 return 1;
4190}
4191
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004192static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
4193{
4194 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004195 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004196 smp->data.u.str.area = args[0].data.str.area;
4197 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004198 return 1;
4199}
4200
4201static int smp_check_const_bool(struct arg *args, char **err)
4202{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004203 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
4204 strcasecmp(args[0].data.str.area, "1") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004205 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004206 args[0].type = ARGT_SINT;
4207 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004208 return 1;
4209 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004210 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
4211 strcasecmp(args[0].data.str.area, "0") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004212 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004213 args[0].type = ARGT_SINT;
4214 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004215 return 1;
4216 }
4217 memprintf(err, "Expects 'true', 'false', '0' or '1'");
4218 return 0;
4219}
4220
4221static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
4222{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004223 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004224 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004225 return 1;
4226}
4227
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02004228static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004229{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004230 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004231 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004232 return 1;
4233}
4234
4235static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
4236{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004237 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004238 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004239 return 1;
4240}
4241
4242static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
4243{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004244 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004245 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004246 return 1;
4247}
4248
4249static int smp_check_const_bin(struct arg *args, char **err)
4250{
David Carlier64a16ab2016-04-08 10:37:02 +01004251 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004252 int binstrlen;
4253
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004254 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004255 return 0;
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004256 chunk_destroy(&args[0].data.str);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004257 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004258 args[0].data.str.area = binstr;
4259 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004260 return 1;
4261}
4262
4263static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
4264{
4265 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004266 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004267 smp->data.u.str.area = args[0].data.str.area;
4268 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004269 return 1;
4270}
4271
4272static int smp_check_const_meth(struct arg *args, char **err)
4273{
4274 enum http_meth_t meth;
4275 int i;
4276
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004277 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004278 if (meth != HTTP_METH_OTHER) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004279 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004280 args[0].type = ARGT_SINT;
4281 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004282 } else {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05004283 /* Check method avalaibility. A method is a token defined as :
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004284 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
4285 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
4286 * token = 1*tchar
4287 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004288 for (i = 0; i < args[0].data.str.data; i++) {
4289 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004290 memprintf(err, "expects valid method.");
4291 return 0;
4292 }
4293 }
4294 }
4295 return 1;
4296}
4297
4298static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
4299{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004300 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004301 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004302 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004303 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004304 smp->data.u.meth.str.area = "";
4305 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004306 } else {
4307 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004308 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004309 smp->data.u.meth.str.area = args[0].data.str.area;
4310 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004311 }
4312 return 1;
4313}
4314
Luca Schimweg8a694b82019-09-10 15:42:52 +02004315// This function checks the "uuid" sample's arguments.
4316// Function won't get called when no parameter is specified (maybe a bug?)
4317static int smp_check_uuid(struct arg *args, char **err)
4318{
4319 if (!args[0].type) {
4320 args[0].type = ARGT_SINT;
4321 args[0].data.sint = 4;
4322 }
4323 else if (args[0].data.sint != 4) {
4324 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
4325 return 0;
4326 }
4327
4328 return 1;
4329}
4330
4331// Generate a RFC4122 UUID (default is v4 = fully random)
4332static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
4333{
4334 if (args[0].data.sint == 4 || !args[0].type) {
Willy Tarreauee3bcdd2020-03-08 17:48:17 +01004335 ha_generate_uuid(&trash);
Luca Schimweg8a694b82019-09-10 15:42:52 +02004336 smp->data.type = SMP_T_STR;
4337 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4338 smp->data.u.str = trash;
4339 return 1;
4340 }
4341
4342 // more implementations of other uuid formats possible here
4343 return 0;
4344}
4345
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004346/* Note: must not be declared <const> as its list will be overwritten.
4347 * Note: fetches that may return multiple types must be declared as the lowest
4348 * common denominator, the type that can be casted into all other ones. For
4349 * instance IPv4/IPv6 must be declared IPv4.
4350 */
4351static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau0209c972021-03-26 12:03:11 +01004352 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4353 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4354 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_CONST },
4355 { "date", smp_fetch_date, ARG2(0,SINT,STR), smp_check_date_unit, SMP_T_SINT, SMP_USE_CONST },
4356 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4357 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_CONST },
4358 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_CONST },
4359 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4360 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4361 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_CONST },
Willy Tarreau0f30d262014-11-24 16:02:05 +01004362 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau0209c972021-03-26 12:03:11 +01004363 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_CONST },
Willy Tarreau70fe9442018-11-22 16:07:39 +01004364
4365 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4366 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4367 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4368 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4369 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004370
Willy Tarreau0209c972021-03-26 12:03:11 +01004371 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_CONST },
4372 { "bool", smp_fetch_const_bool, ARG1(1,STR), smp_check_const_bool, SMP_T_BOOL, SMP_USE_CONST },
4373 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_CONST },
4374 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_CONST },
4375 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_CONST },
4376 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_CONST },
4377 { "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 +02004378
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004379 { /* END */ },
4380}};
4381
Willy Tarreau0108d902018-11-25 19:14:37 +01004382INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
4383
Emeric Brun107ca302010-01-04 16:16:05 +01004384/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02004385static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Moemen MHEDHBI848216f2021-04-02 01:05:07 +02004386 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
4387 { "b64dec", sample_conv_base642bin, 0, NULL, SMP_T_STR, SMP_T_BIN },
4388 { "base64", sample_conv_bin2base64, 0, NULL, SMP_T_BIN, SMP_T_STR },
4389 { "ub64enc", sample_conv_bin2base64url,0, NULL, SMP_T_BIN, SMP_T_STR },
4390 { "ub64dec", sample_conv_base64url2bin,0, NULL, SMP_T_STR, SMP_T_BIN },
4391 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
4392 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
4393 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Marcin Deranek40ca09c2021-07-13 14:05:24 +02004394 { "be2dec", sample_conv_be2dec, ARG3(1,STR,SINT,SINT), sample_conv_be2dec_check, SMP_T_BIN, SMP_T_STR },
Marcin Deranekda0264a2021-07-13 14:08:56 +02004395 { "be2hex", sample_conv_be2hex, ARG3(1,STR,SINT,SINT), sample_conv_be2hex_check, SMP_T_BIN, SMP_T_STR },
Moemen MHEDHBI848216f2021-04-02 01:05:07 +02004396 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
4397 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
4398 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
4399 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4400 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4401 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4402 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4403 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4404 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4405 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4406 { "xxh3", sample_conv_xxh3, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4407 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4408 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4409 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
4410 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
4411 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4412 { "word", sample_conv_word, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4413 { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
4414 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02004415#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01004416 { "sha2", sample_conv_sha2, ARG1(0, SINT), smp_check_sha2, SMP_T_BIN, SMP_T_BIN },
Ilya Shipitsin2c481d02021-03-26 23:35:31 +05004417#ifdef EVP_CIPH_GCM_MODE
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02004418 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
4419#endif
Patrick Gansterer8e366512020-04-22 16:47:57 +02004420 { "digest", sample_conv_crypto_digest, ARG1(1,STR), check_crypto_digest, SMP_T_BIN, SMP_T_BIN },
4421 { "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 +02004422#endif
Willy Tarreau280f42b2018-02-19 15:34:12 +01004423 { "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 +02004424 { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT },
Willy Tarreau2a8a2f02021-05-09 11:59:49 +02004425#if defined(HAVE_CRYPTO_memcmp)
Tim Duesterhusf38175c2020-06-09 11:48:42 +02004426 { "secure_memcmp", sample_conv_secure_memcmp, ARG1(1,STR), smp_check_secure_memcmp, SMP_T_BIN, SMP_T_BOOL },
4427#endif
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01004428
4429 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01004430 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
4431 { "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 +01004432
Baptiste Assmanne138dda2020-10-22 15:39:03 +02004433 /* FIX converters */
4434 { "fix_is_valid", sample_conv_fix_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4435 { "fix_tag_value", sample_conv_fix_tag_value, ARG1(1,STR), sample_conv_fix_value_check, SMP_T_BIN, SMP_T_BIN },
4436
Baptiste Assmanne279ca62020-10-27 18:10:06 +01004437 /* MQTT converters */
4438 { "mqtt_is_valid", sample_conv_mqtt_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4439 { "mqtt_field_value", sample_conv_mqtt_field_value, ARG2(2,STR,STR), sample_conv_mqtt_field_value_check, SMP_T_BIN, SMP_T_STR },
4440
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02004441 { "iif", sample_conv_iif, ARG2(2, STR, STR), NULL, SMP_T_BOOL, SMP_T_STR },
4442
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02004443 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4444 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4445 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4446 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
4447 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4448 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4449 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4450 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4451 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4452 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4453 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4454 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4455 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4456 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01004457
Christopher Fauletea159d62020-04-01 16:21:44 +02004458 { "htonl", sample_conv_htonl, 0, NULL, SMP_T_SINT, SMP_T_BIN },
4459 { "cut_crlf", sample_conv_cut_crlf, 0, NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet51fc9d12020-04-01 17:24:41 +02004460 { "ltrim", sample_conv_ltrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet568415a2020-04-01 17:24:47 +02004461 { "rtrim", sample_conv_rtrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Alex51c8ad42021-04-15 16:45:15 +02004462 { "json_query", sample_conv_json_query, ARG2(1,STR,STR), sample_check_json_query , SMP_T_STR, SMP_T_ANY },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02004463 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01004464}};
4465
Willy Tarreau0108d902018-11-25 19:14:37 +01004466INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);