blob: bda590875c457c029ecf98f57c46dc180587ea6a [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 Tarreaub23e5952020-06-04 16:06:59 +020036#include <haproxy/protobuf.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020037#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020038#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020039#include <haproxy/sample.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020040#include <haproxy/sink.h>
41#include <haproxy/stick_table.h>
Willy Tarreau9310f482021-10-06 16:18:40 +020042#include <haproxy/time.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
Dragan Dosen9e8db132021-02-22 10:03:53 +01001670/* This function returns a sample struct filled with an <arg> content.
1671 * If the <arg> contains a string, it is returned in the sample flagged as
1672 * SMP_F_CONST. If the <arg> contains a variable descriptor, the sample is
1673 * filled with the content of the variable by using vars_get_by_desc().
1674 *
1675 * Keep in mind that the sample content may be written to a pre-allocated
1676 * trash chunk as returned by get_trash_chunk().
1677 *
1678 * This function returns 0 if an error occurs, otherwise it returns 1.
1679 */
Willy Tarreau4034e2c2021-10-06 15:20:18 +02001680int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001681{
1682 switch (arg->type) {
1683 case ARGT_STR:
1684 smp->data.type = SMP_T_STR;
1685 smp->data.u.str = arg->data.str;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001686 smp->flags = SMP_F_CONST;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001687 return 1;
1688 case ARGT_VAR:
Willy Tarreau2476ff12021-10-06 15:30:52 +02001689 return sample_conv_var2smp(&arg->data.var, smp, SMP_T_STR);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001690 default:
1691 return 0;
1692 }
1693}
1694
Marcin Deranek40ca09c2021-07-13 14:05:24 +02001695static int sample_conv_be2dec_check(struct arg *args, struct sample_conv *conv,
1696 const char *file, int line, char **err)
1697{
1698 if (args[1].data.sint <= 0 || args[1].data.sint > sizeof(unsigned long long)) {
Willy Tarreauc5d0fc92021-09-15 10:30:40 +02001699 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 +02001700 return 0;
1701 }
1702
1703 if (args[2].data.sint != 0 && args[2].data.sint != 1) {
1704 memprintf(err, "Unsupported truncate value (%lld)", args[2].data.sint);
1705 return 0;
1706 }
1707
1708 return 1;
1709}
1710
1711/* Converts big-endian binary input sample to a string containing an unsigned
1712 * integer number per <chunk_size> input bytes separated with <separator>.
1713 * Optional <truncate> flag indicates if input is truncated at <chunk_size>
1714 * boundaries.
1715 * Arguments: separator (string), chunk_size (integer), truncate (0,1)
1716 */
1717static int sample_conv_be2dec(const struct arg *args, struct sample *smp, void *private)
1718{
1719 struct buffer *trash = get_trash_chunk();
1720 const int last = args[2].data.sint ? smp->data.u.str.data - args[1].data.sint + 1 : smp->data.u.str.data;
1721 int max_size = trash->size - 2;
1722 int i;
1723 int start;
1724 int ptr = 0;
1725 unsigned long long number;
1726 char *pos;
1727
1728 trash->data = 0;
1729
1730 while (ptr < last && trash->data <= max_size) {
1731 start = trash->data;
1732 if (ptr) {
1733 /* Add separator */
1734 memcpy(trash->area + trash->data, args[0].data.str.area, args[0].data.str.data);
1735 trash->data += args[0].data.str.data;
1736 }
1737 else
1738 max_size -= args[0].data.str.data;
1739
1740 /* Add integer */
1741 for (number = 0, i = 0; i < args[1].data.sint && ptr < smp->data.u.str.data; i++)
1742 number = (number << 8) + (unsigned char)smp->data.u.str.area[ptr++];
1743
1744 pos = ulltoa(number, trash->area + trash->data, trash->size - trash->data);
1745 if (pos)
1746 trash->data = pos - trash->area;
1747 else {
1748 trash->data = start;
1749 break;
1750 }
1751 }
1752
1753 smp->data.u.str = *trash;
1754 smp->data.type = SMP_T_STR;
1755 smp->flags &= ~SMP_F_CONST;
1756 return 1;
1757}
1758
Marcin Deranekda0264a2021-07-13 14:08:56 +02001759static int sample_conv_be2hex_check(struct arg *args, struct sample_conv *conv,
1760 const char *file, int line, char **err)
1761{
1762 if (args[1].data.sint <= 0 && (args[0].data.str.data > 0 || args[2].data.sint != 0)) {
1763 memprintf(err, "chunk_size needs to be positive (%lld)", args[1].data.sint);
1764 return 0;
1765 }
1766
1767 if (args[2].data.sint != 0 && args[2].data.sint != 1) {
1768 memprintf(err, "Unsupported truncate value (%lld)", args[2].data.sint);
1769 return 0;
1770 }
1771
1772 return 1;
1773}
1774
1775/* Converts big-endian binary input sample to a hex string containing two hex
1776 * digits per input byte. <separator> is put every <chunk_size> binary input
1777 * bytes if specified. Optional <truncate> flag indicates if input is truncated
1778 * at <chunk_size> boundaries.
1779 * Arguments: separator (string), chunk_size (integer), truncate (0,1)
1780 */
1781static int sample_conv_be2hex(const struct arg *args, struct sample *smp, void *private)
1782{
1783 struct buffer *trash = get_trash_chunk();
1784 int chunk_size = args[1].data.sint;
1785 const int last = args[2].data.sint ? smp->data.u.str.data - chunk_size + 1 : smp->data.u.str.data;
1786 int i;
1787 int max_size;
1788 int ptr = 0;
1789 unsigned char c;
1790
1791 trash->data = 0;
1792 if (args[0].data.str.data == 0 && args[2].data.sint == 0)
1793 chunk_size = smp->data.u.str.data;
1794 max_size = trash->size - 2 * chunk_size;
1795
1796 while (ptr < last && trash->data <= max_size) {
1797 if (ptr) {
1798 /* Add separator */
1799 memcpy(trash->area + trash->data, args[0].data.str.area, args[0].data.str.data);
1800 trash->data += args[0].data.str.data;
1801 }
1802 else
1803 max_size -= args[0].data.str.data;
1804
1805 /* Add hex */
1806 for (i = 0; i < chunk_size && ptr < smp->data.u.str.data; i++) {
1807 c = smp->data.u.str.area[ptr++];
1808 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1809 trash->area[trash->data++] = hextab[c & 0xF];
1810 }
1811 }
1812
1813 smp->data.u.str = *trash;
1814 smp->data.type = SMP_T_STR;
1815 smp->flags &= ~SMP_F_CONST;
1816 return 1;
1817}
1818
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001819static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001820{
Willy Tarreau83061a82018-07-13 11:56:34 +02001821 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001822 unsigned char c;
1823 int ptr = 0;
1824
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001825 trash->data = 0;
1826 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
1827 c = smp->data.u.str.area[ptr++];
1828 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1829 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001830 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001831 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001832 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001833 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001834 return 1;
1835}
1836
Dragan Dosen3f957b22017-10-24 09:27:34 +02001837static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1838{
1839 long long int n = 0;
1840 int i, c;
1841
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001842 for (i = 0; i < smp->data.u.str.data; i++) {
1843 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02001844 return 0;
1845 n = (n << 4) + c;
1846 }
1847
1848 smp->data.u.sint = n;
1849 smp->data.type = SMP_T_SINT;
1850 smp->flags &= ~SMP_F_CONST;
1851 return 1;
1852}
1853
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001854/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001855static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001856{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001857 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
1858 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01001859 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001860 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001861 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001862 return 1;
1863}
1864
Willy Tarreau60a2ee72017-12-15 07:13:48 +01001865static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001866{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001867 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001868 smp->data.u.sint = i;
1869 smp->data.type = SMP_T_SINT;
1870 return 1;
1871}
1872
1873
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001874static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001875{
1876 int i;
1877
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001878 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001879 return 0;
1880
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001881 for (i = 0; i < smp->data.u.str.data; i++) {
1882 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
1883 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001884 }
1885 return 1;
1886}
1887
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001888static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001889{
1890 int i;
1891
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001892 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001893 return 0;
1894
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001895 for (i = 0; i < smp->data.u.str.data; i++) {
1896 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
1897 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001898 }
1899 return 1;
1900}
1901
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001902/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
1903static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001904{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001905 /* Attempt to convert to IPv4 to apply the correct mask. */
1906 c_ipv62ip(smp);
1907
1908 if (smp->data.type == SMP_T_IPV4) {
1909 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
1910 smp->data.type = SMP_T_IPV4;
1911 }
1912 else if (smp->data.type == SMP_T_IPV6) {
1913 /* IPv6 cannot be converted without an IPv6 mask. */
1914 if (args[1].type != ARGT_IPV6)
1915 return 0;
1916
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +01001917 write_u64(&smp->data.u.ipv6.s6_addr[0],
1918 read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0]));
1919 write_u64(&smp->data.u.ipv6.s6_addr[8],
1920 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 +01001921 smp->data.type = SMP_T_IPV6;
1922 }
1923
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001924 return 1;
1925}
1926
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001927/* takes an UINT value on input supposed to represent the time since EPOCH,
1928 * adds an optional offset found in args[1] and emits a string representing
1929 * the local time in the format specified in args[1] using strftime().
1930 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001931static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001932{
Willy Tarreau83061a82018-07-13 11:56:34 +02001933 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001934 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001935 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Tim Duesterhus1f269c12021-08-28 23:57:01 +02001936 struct tm tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001937
1938 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001939 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001940 curr_date += args[1].data.sint;
1941
Tim Duesterhus1f269c12021-08-28 23:57:01 +02001942 get_localtime(curr_date, &tm);
1943
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001944 temp = get_trash_chunk();
Tim Duesterhus1f269c12021-08-28 23:57:01 +02001945 temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001946 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001947 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001948 return 1;
1949}
1950
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001951/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001952static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001953{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001954 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
1955 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01001956 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001957 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001958 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001959 return 1;
1960}
1961
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001962/* takes an UINT value on input supposed to represent the time since EPOCH,
1963 * adds an optional offset found in args[1] and emits a string representing
1964 * the UTC date in the format specified in args[1] using strftime().
1965 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001966static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001967{
Willy Tarreau83061a82018-07-13 11:56:34 +02001968 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001969 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001970 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Tim Duesterhus1f269c12021-08-28 23:57:01 +02001971 struct tm tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001972
1973 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001974 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001975 curr_date += args[1].data.sint;
1976
Tim Duesterhus1f269c12021-08-28 23:57:01 +02001977 get_gmtime(curr_date, &tm);
1978
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001979 temp = get_trash_chunk();
Tim Duesterhus1f269c12021-08-28 23:57:01 +02001980 temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001981 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001982 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001983 return 1;
1984}
1985
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001986/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001987static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001988{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001989 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
1990 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01001991 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001992 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001993 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001994 return 1;
1995}
1996
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001997/* hashes the binary input into a 32-bit unsigned int using xxh.
1998 * The seed of the hash defaults to 0 but can be changd in argument 1.
1999 */
2000static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
2001{
2002 unsigned int seed;
2003
Christopher Faulete6e7a582021-01-29 11:29:28 +01002004 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002005 seed = arg_p->data.sint;
2006 else
2007 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002008 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
2009 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002010 smp->data.type = SMP_T_SINT;
2011 return 1;
2012}
2013
2014/* hashes the binary input into a 64-bit unsigned int using xxh.
2015 * In fact, the function returns a 64 bit unsigned, but the sample
2016 * storage of haproxy only proposes 64-bits signed, so the value is
2017 * cast as signed. This cast doesn't impact the hash repartition.
2018 * The seed of the hash defaults to 0 but can be changd in argument 1.
2019 */
2020static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
2021{
2022 unsigned long long int seed;
2023
Christopher Faulete6e7a582021-01-29 11:29:28 +01002024 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002025 seed = (unsigned long long int)arg_p->data.sint;
2026 else
2027 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002028 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
2029 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002030 smp->data.type = SMP_T_SINT;
2031 return 1;
2032}
2033
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002034static int sample_conv_xxh3(const struct arg *arg_p, struct sample *smp, void *private)
2035{
2036 unsigned long long int seed;
2037
Christopher Faulete6e7a582021-01-29 11:29:28 +01002038 if (arg_p->data.sint)
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002039 seed = (unsigned long long int)arg_p->data.sint;
2040 else
2041 seed = 0;
2042 smp->data.u.sint = (long long int)XXH3(smp->data.u.str.area,
2043 smp->data.u.str.data, seed);
2044 smp->data.type = SMP_T_SINT;
2045 return 1;
2046}
2047
Willy Tarreau80599772015-01-20 19:35:24 +01002048/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002049static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01002050{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002051 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
2052 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002053 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002054 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002055 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01002056 return 1;
2057}
2058
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002059/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
2060static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
2061{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002062 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
2063 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002064 if (arg_p->data.sint)
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002065 smp->data.u.sint = full_hash(smp->data.u.sint);
2066 smp->data.type = SMP_T_SINT;
2067 return 1;
2068}
2069
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002070/* This function escape special json characters. The returned string can be
2071 * safely set between two '"' and used as json string. The json string is
2072 * defined like this:
2073 *
2074 * any Unicode character except '"' or '\' or control character
2075 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
2076 *
2077 * The enum input_type contain all the allowed mode for decoding the input
2078 * string.
2079 */
2080enum input_type {
2081 IT_ASCII = 0,
2082 IT_UTF8,
2083 IT_UTF8S,
2084 IT_UTF8P,
2085 IT_UTF8PS,
2086};
William Dauchy5417e892021-01-08 21:57:41 +01002087
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002088static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
2089 const char *file, int line, char **err)
2090{
Christopher Faulet95917132020-08-05 23:07:07 +02002091 enum input_type type;
2092
Christopher Faulet95917132020-08-05 23:07:07 +02002093 if (strcmp(arg->data.str.area, "") == 0)
2094 type = IT_ASCII;
2095 else if (strcmp(arg->data.str.area, "ascii") == 0)
2096 type = IT_ASCII;
2097 else if (strcmp(arg->data.str.area, "utf8") == 0)
2098 type = IT_UTF8;
2099 else if (strcmp(arg->data.str.area, "utf8s") == 0)
2100 type = IT_UTF8S;
2101 else if (strcmp(arg->data.str.area, "utf8p") == 0)
2102 type = IT_UTF8P;
2103 else if (strcmp(arg->data.str.area, "utf8ps") == 0)
2104 type = IT_UTF8PS;
2105 else {
2106 memprintf(err, "Unexpected input code type. "
2107 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
2108 return 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002109 }
2110
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002111 chunk_destroy(&arg->data.str);
Christopher Faulet95917132020-08-05 23:07:07 +02002112 arg->type = ARGT_SINT;
2113 arg->data.sint = type;
2114 return 1;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002115}
2116
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002117static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002118{
Willy Tarreau83061a82018-07-13 11:56:34 +02002119 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002120 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
2121 const char *str;
2122 int len;
2123 enum input_type input_type = IT_ASCII;
2124 unsigned int c;
2125 unsigned int ret;
2126 char *p;
2127
Christopher Faulete6e7a582021-01-29 11:29:28 +01002128 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002129
2130 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002131 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002132
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002133 p = smp->data.u.str.area;
2134 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002135
2136 if (input_type == IT_ASCII) {
2137 /* Read input as ASCII. */
2138 c = *(unsigned char *)p;
2139 p++;
2140 }
2141 else {
2142 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002143 ret = utf8_next(p,
2144 smp->data.u.str.data - ( p - smp->data.u.str.area),
2145 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002146 p += utf8_return_length(ret);
2147
2148 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2149 return 0;
2150 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2151 continue;
2152 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2153 return 0;
2154 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2155 continue;
2156
2157 /* Check too big values. */
2158 if ((unsigned int)c > 0xffff) {
2159 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2160 return 0;
2161 continue;
2162 }
2163 }
2164
2165 /* Convert character. */
2166 if (c == '"') {
2167 len = 2;
2168 str = "\\\"";
2169 }
2170 else if (c == '\\') {
2171 len = 2;
2172 str = "\\\\";
2173 }
2174 else if (c == '/') {
2175 len = 2;
2176 str = "\\/";
2177 }
2178 else if (c == '\b') {
2179 len = 2;
2180 str = "\\b";
2181 }
2182 else if (c == '\f') {
2183 len = 2;
2184 str = "\\f";
2185 }
2186 else if (c == '\r') {
2187 len = 2;
2188 str = "\\r";
2189 }
2190 else if (c == '\n') {
2191 len = 2;
2192 str = "\\n";
2193 }
2194 else if (c == '\t') {
2195 len = 2;
2196 str = "\\t";
2197 }
Willy Tarreau90807112020-02-25 08:16:33 +01002198 else if (c > 0xff || !isprint((unsigned char)c)) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002199 /* isprint generate a segfault if c is too big. The man says that
2200 * c must have the value of an unsigned char or EOF.
2201 */
2202 len = 6;
2203 _str[0] = '\\';
2204 _str[1] = 'u';
2205 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2206 str = _str;
2207 }
2208 else {
2209 len = 1;
Willy Tarreau5715da22020-02-25 08:37:37 +01002210 _str[0] = c;
2211 str = _str;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002212 }
2213
2214 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002215 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002216 return 0;
2217
2218 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002219 memcpy(temp->area + temp->data, str, len);
2220 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002221 }
2222
2223 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002224 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002225 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002226
2227 return 1;
2228}
2229
Emeric Brun54c4ac82014-11-03 15:32:43 +01002230/* This sample function is designed to extract some bytes from an input buffer.
2231 * First arg is the offset.
2232 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002233static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002234{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002235 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2236 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002237 return 1;
2238 }
2239
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002240 if (smp->data.u.str.size)
2241 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002242 smp->data.u.str.data -= arg_p[0].data.sint;
2243 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002244
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002245 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2246 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002247
2248 return 1;
2249}
2250
Emeric Brunf399b0d2014-11-03 17:07:03 +01002251static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2252 const char *file, int line, char **err)
2253{
2254 struct arg *arg = args;
2255
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002256 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002257 memprintf(err, "Unexpected arg type");
2258 return 0;
2259 }
2260
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002261 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002262 memprintf(err, "Unexpected value 0 for index");
2263 return 0;
2264 }
2265
2266 arg++;
2267
2268 if (arg->type != ARGT_STR) {
2269 memprintf(err, "Unexpected arg type");
2270 return 0;
2271 }
2272
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002273 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002274 memprintf(err, "Empty separators list");
2275 return 0;
2276 }
2277
2278 return 1;
2279}
2280
2281/* This sample function is designed to a return selected part of a string (field).
2282 * First arg is the index of the field (start at 1)
2283 * Second arg is a char list of separators (type string)
2284 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002285static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002286{
Marcin Deranek9631a282018-04-16 14:30:46 +02002287 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002288 char *start, *end;
2289 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002290 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002291
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002292 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002293 return 0;
2294
Marcin Deranek9631a282018-04-16 14:30:46 +02002295 if (arg_p[0].data.sint < 0) {
2296 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002297 end = start = smp->data.u.str.area + smp->data.u.str.data;
2298 while (start > smp->data.u.str.area) {
2299 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2300 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002301 if (field == arg_p[0].data.sint) {
2302 if (count == 1)
2303 goto found;
2304 else if (count > 1)
2305 count--;
2306 } else {
2307 end = start-1;
2308 field--;
2309 }
2310 break;
2311 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002312 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002313 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002314 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002315 } else {
2316 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002317 end = start = smp->data.u.str.area;
2318 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2319 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2320 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002321 if (field == arg_p[0].data.sint) {
2322 if (count == 1)
2323 goto found;
2324 else if (count > 1)
2325 count--;
2326 } else {
2327 start = end+1;
2328 field++;
2329 }
2330 break;
2331 }
2332 }
2333 end++;
2334 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002335 }
2336
2337 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002338 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002339 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002340 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002341 }
2342found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002343 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002344 /* If ret string is len 0, no need to
2345 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002346 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002347 return 1;
2348
Emeric Brunf399b0d2014-11-03 17:07:03 +01002349 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002350 Note: smp->data.u.str.size cannot be set to 0 */
2351 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002352 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002353
Thayne McCombsb2843052021-04-11 23:26:59 -06002354 smp->data.u.str.area = start;
2355
Emeric Brunf399b0d2014-11-03 17:07:03 +01002356 return 1;
2357}
2358
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002359/* This sample function is designed to return a word from a string.
2360 * First arg is the index of the word (start at 1)
2361 * Second arg is a char list of words separators (type string)
2362 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002363static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002364{
Marcin Deranek9631a282018-04-16 14:30:46 +02002365 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002366 char *start, *end;
2367 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002368 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002369
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002370 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002371 return 0;
2372
2373 word = 0;
2374 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002375 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002376 end = start = smp->data.u.str.area + smp->data.u.str.data;
2377 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002378 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002379 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2380 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002381 issep = 1;
2382 break;
2383 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002384 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002385 if (!inword) {
2386 if (!issep) {
2387 if (word != arg_p[0].data.sint) {
2388 word--;
2389 end = start;
2390 }
2391 inword = 1;
2392 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002393 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002394 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002395 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002396 if (count == 1)
2397 goto found;
2398 else if (count > 1)
2399 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002400 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002401 inword = 0;
2402 }
2403 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002404 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002405 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002406 end = start = smp->data.u.str.area;
2407 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002408 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002409 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2410 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002411 issep = 1;
2412 break;
2413 }
2414 }
2415 if (!inword) {
2416 if (!issep) {
2417 if (word != arg_p[0].data.sint) {
2418 word++;
2419 start = end;
2420 }
2421 inword = 1;
2422 }
2423 }
2424 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002425 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002426 if (count == 1)
2427 goto found;
2428 else if (count > 1)
2429 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002430 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002431 inword = 0;
2432 }
2433 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002434 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002435 }
2436
2437 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002438 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002439 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002440 return 1;
2441 }
2442found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002443 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002444 /* If ret string is len 0, no need to
2445 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002446 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002447 return 1;
2448
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002449 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002450
2451 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002452 Note: smp->data.u.str.size cannot be set to 0 */
2453 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002454 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002455
2456 return 1;
2457}
2458
Willy Tarreau7eda8492015-01-20 19:47:06 +01002459static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2460 const char *file, int line, char **err)
2461{
2462 struct arg *arg = args;
2463 char *p;
2464 int len;
2465
2466 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2467 arg[0].type_flags = 0;
2468
2469 if (arg[2].type != ARGT_STR)
2470 return 1;
2471
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002472 p = arg[2].data.str.area;
2473 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002474 while (len) {
2475 if (*p == 'i') {
2476 arg[0].type_flags |= ARGF_REG_ICASE;
2477 }
2478 else if (*p == 'g') {
2479 arg[0].type_flags |= ARGF_REG_GLOB;
2480 }
2481 else {
2482 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2483 return 0;
2484 }
2485 p++;
2486 len--;
2487 }
2488 return 1;
2489}
2490
2491/* This sample function is designed to do the equivalent of s/match/replace/ on
2492 * the input string. It applies a regex and restarts from the last matched
2493 * location until nothing matches anymore. First arg is the regex to apply to
2494 * the input string, second arg is the replacement expression.
2495 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002496static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002497{
2498 char *start, *end;
2499 struct my_regex *reg = arg_p[0].data.reg;
2500 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002501 struct buffer *trash = get_trash_chunk();
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002502 struct buffer *output;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002503 int flag, max;
2504 int found;
2505
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002506 start = smp->data.u.str.area;
2507 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002508
2509 flag = 0;
2510 while (1) {
2511 /* check for last round which is used to copy remaining parts
2512 * when not running in global replacement mode.
2513 */
2514 found = 0;
2515 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2516 /* Note: we can have start == end on empty strings or at the end */
2517 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2518 }
2519
2520 if (!found)
2521 pmatch[0].rm_so = end - start;
2522
2523 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002524 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002525 if (max && pmatch[0].rm_so > 0) {
2526 if (max > pmatch[0].rm_so)
2527 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002528 memcpy(trash->area + trash->data, start, max);
2529 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002530 }
2531
2532 if (!found)
2533 break;
2534
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002535 output = alloc_trash_chunk();
Willy Tarreau23997da2020-02-18 14:27:44 +01002536 if (!output)
2537 break;
2538
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002539 output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2540
Willy Tarreau7eda8492015-01-20 19:47:06 +01002541 /* replace the matching part */
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002542 max = output->size - output->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002543 if (max) {
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002544 if (max > output->data)
2545 max = output->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002546 memcpy(trash->area + trash->data,
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002547 output->area, max);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002548 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002549 }
2550
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002551 free_trash_chunk(output);
2552
Willy Tarreau7eda8492015-01-20 19:47:06 +01002553 /* stop here if we're done with this string */
2554 if (start >= end)
2555 break;
2556
2557 /* We have a special case for matches of length 0 (eg: "x*y*").
2558 * These ones are considered to match in front of a character,
2559 * so we have to copy that character and skip to the next one.
2560 */
2561 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002562 if (trash->data < trash->size)
2563 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002564 pmatch[0].rm_eo++;
2565 }
2566
2567 start += pmatch[0].rm_eo;
2568 flag |= REG_NOTBOL;
2569 }
2570
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002571 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002572 return 1;
2573}
2574
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002575/* This function check an operator entry. It expects a string.
2576 * The string can be an integer or a variable name.
2577 */
2578static int check_operator(struct arg *args, struct sample_conv *conv,
2579 const char *file, int line, char **err)
2580{
2581 const char *str;
2582 const char *end;
Christopher Faulet95917132020-08-05 23:07:07 +02002583 long long int i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002584
2585 /* Try to decode a variable. */
2586 if (vars_check_arg(&args[0], NULL))
2587 return 1;
2588
2589 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002590 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002591 end = str + strlen(str);
Christopher Faulet95917132020-08-05 23:07:07 +02002592 i = read_int64(&str, end);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002593 if (*str != '\0') {
2594 memprintf(err, "expects an integer or a variable name");
2595 return 0;
2596 }
Christopher Faulet95917132020-08-05 23:07:07 +02002597
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002598 chunk_destroy(&args[0].data.str);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002599 args[0].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02002600 args[0].data.sint = i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002601 return 1;
2602}
2603
Willy Tarreau6204cd92016-03-10 16:33:04 +01002604/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002605 * If the arg contain an integer, the integer is returned in the
2606 * sample. If the arg contains a variable descriptor, it returns the
2607 * variable value.
2608 *
2609 * This function returns 0 if an error occurs, otherwise it returns 1.
2610 */
Willy Tarreaud9be5992021-10-06 15:19:05 +02002611int sample_conv_var2smp_sint(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002612{
2613 switch (arg->type) {
2614 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002615 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002616 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002617 return 1;
2618 case ARGT_VAR:
Willy Tarreau2476ff12021-10-06 15:30:52 +02002619 return sample_conv_var2smp(&arg->data.var, smp, SMP_T_SINT);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002620 default:
2621 return 0;
2622 }
2623}
2624
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002625/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002626 * result.
2627 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002628static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002629{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002630 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002631 return 1;
2632}
2633
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002634/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002635 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002636 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002637static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002638{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002639 struct sample tmp;
2640
Willy Tarreau7560dd42016-03-10 16:28:58 +01002641 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002642 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002643 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002644 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002645 return 1;
2646}
2647
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002648/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002649 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002650 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002651static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002652{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002653 struct sample tmp;
2654
Willy Tarreau7560dd42016-03-10 16:28:58 +01002655 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002656 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002657 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002658 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002659 return 1;
2660}
2661
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002662/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002663 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002664 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002665static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002666{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002667 struct sample tmp;
2668
Willy Tarreau7560dd42016-03-10 16:28:58 +01002669 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002670 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002671 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002672 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002673 return 1;
2674}
2675
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002676static inline long long int arith_add(long long int a, long long int b)
2677{
2678 /* Prevent overflow and makes capped calculus.
2679 * We must ensure that the check calculus doesn't
2680 * exceed the signed 64 bits limits.
2681 *
2682 * +----------+----------+
2683 * | a<0 | a>=0 |
2684 * +------+----------+----------+
2685 * | b<0 | MIN-a>b | no check |
2686 * +------+----------+----------+
2687 * | b>=0 | no check | MAX-a<b |
2688 * +------+----------+----------+
2689 */
2690 if ((a ^ b) >= 0) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002691 /* signs are different. */
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002692 if (a < 0) {
2693 if (LLONG_MIN - a > b)
2694 return LLONG_MIN;
2695 }
2696 if (LLONG_MAX - a < b)
2697 return LLONG_MAX;
2698 }
2699 return a + b;
2700}
2701
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002702/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002703 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002704 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002705static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002706{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002707 struct sample tmp;
2708
Willy Tarreau7560dd42016-03-10 16:28:58 +01002709 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002710 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002711 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002712 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002713 return 1;
2714}
2715
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002716/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002717 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002718 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002719static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002720 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002721{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002722 struct sample tmp;
2723
Willy Tarreau7560dd42016-03-10 16:28:58 +01002724 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002725 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002726 return 0;
2727
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002728 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2729 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2730 * of -LLONG_MIN and correct the result.
2731 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002732 if (tmp.data.u.sint == LLONG_MIN) {
2733 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2734 if (smp->data.u.sint < LLONG_MAX)
2735 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002736 return 1;
2737 }
2738
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002739 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002740 * the second operand.
2741 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002742 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002743 return 1;
2744}
2745
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002746/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002747 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002748 * If the result makes an overflow, then the largest possible quantity is
2749 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002750 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002751static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002752 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002753{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002754 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002755 long long int c;
2756
Willy Tarreau7560dd42016-03-10 16:28:58 +01002757 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002758 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002759 return 0;
2760
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002761 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002762 if (!smp->data.u.sint || !tmp.data.u.sint) {
2763 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002764 return 1;
2765 }
2766
2767 /* The multiply between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002768 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002769 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002770 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2771 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002772 return 1;
2773 }
2774
2775 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002776 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002777
2778 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002779 if (smp->data.u.sint != c / tmp.data.u.sint) {
2780 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2781 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002782 return 1;
2783 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002784 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002785 return 1;
2786 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002787 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002788 return 1;
2789}
2790
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002791/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002792 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002793 * If arg_p makes the result overflow, then the largest possible quantity is
2794 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002795 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002796static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002797 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002798{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002799 struct sample tmp;
2800
Willy Tarreau7560dd42016-03-10 16:28:58 +01002801 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002802 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002803 return 0;
2804
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002805 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002806 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002807 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002808 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002809 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2810 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002811 return 1;
2812 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002813 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002814 return 1;
2815 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002816 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002817 return 1;
2818}
2819
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002820/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002821 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002822 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002823 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002824static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002825 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002826{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002827 struct sample tmp;
2828
Willy Tarreau7560dd42016-03-10 16:28:58 +01002829 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002830 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002831 return 0;
2832
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002833 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002834 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002835 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002836 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002837 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2838 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002839 return 1;
2840 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002841 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002842 return 1;
2843 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002844 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002845 return 1;
2846}
2847
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002848/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002849 * result.
2850 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002851static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002852 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002853{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002854 if (smp->data.u.sint == LLONG_MIN)
2855 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002856 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002857 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002858 return 1;
2859}
2860
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002861/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002862 * false. The output is a BOOL.
2863 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002864static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002865 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002866{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002867 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002868 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002869 return 1;
2870}
2871
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002872/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002873 * truee. The output is a BOOL.
2874 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002875static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002876 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002877{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002878 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002879 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002880 return 1;
2881}
2882
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002883/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002884 * The output is a BOOL.
2885 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002886static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002887 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002888{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002889 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002890 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002891 return 1;
2892}
2893
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002894/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002895 * The output is a BOOL.
2896 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002897static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002898 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002899{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002900 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002901 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002902 return 1;
2903}
2904
Willy Tarreau280f42b2018-02-19 15:34:12 +01002905/* appends an optional const string, an optional variable contents and another
2906 * optional const string to an existing string.
2907 */
2908static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
2909{
Willy Tarreau83061a82018-07-13 11:56:34 +02002910 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002911 struct sample tmp;
2912 int max;
2913
Willy Tarreau591fc3a2021-01-08 16:08:43 +01002914 trash = alloc_trash_chunk();
William Dauchye9970102021-01-11 11:05:58 +01002915 if (!trash)
2916 return 0;
2917
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002918 trash->data = smp->data.u.str.data;
2919 if (trash->data > trash->size - 1)
2920 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002921
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002922 memcpy(trash->area, smp->data.u.str.area, trash->data);
2923 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002924
2925 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002926 max = arg_p[0].data.str.data;
2927 if (max > trash->size - 1 - trash->data)
2928 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002929
2930 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002931 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
2932 trash->data += max;
2933 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002934 }
2935
2936 /* append second string (variable) if it's found and we can turn it
2937 * into a string.
2938 */
2939 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaue352b9d2021-09-03 11:52:38 +02002940 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 +01002941 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
2942 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
2943
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002944 max = tmp.data.u.str.data;
2945 if (max > trash->size - 1 - trash->data)
2946 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002947
2948 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002949 memcpy(trash->area + trash->data, tmp.data.u.str.area,
2950 max);
2951 trash->data += max;
2952 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002953 }
2954 }
2955
2956 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002957 max = arg_p[2].data.str.data;
2958 if (max > trash->size - 1 - trash->data)
2959 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002960
2961 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002962 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
2963 trash->data += max;
2964 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002965 }
2966
2967 smp->data.u.str = *trash;
2968 smp->data.type = SMP_T_STR;
Willy Tarreau591fc3a2021-01-08 16:08:43 +01002969 smp_dup(smp);
2970 free_trash_chunk(trash);
Willy Tarreau280f42b2018-02-19 15:34:12 +01002971 return 1;
2972}
2973
2974/* This function checks the "concat" converter's arguments and extracts the
2975 * variable name and its scope.
2976 */
2977static int smp_check_concat(struct arg *args, struct sample_conv *conv,
2978 const char *file, int line, char **err)
2979{
2980 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002981 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
2982 memprintf(err, "failed to register variable name '%s'",
2983 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01002984 return 0;
2985 }
2986 return 1;
2987}
2988
Tim Duesterhusf38175c2020-06-09 11:48:42 +02002989/* Compares string with a variable containing a string. Return value
Tim Duesterhusca097c12018-04-27 21:18:45 +02002990 * is compatible with strcmp(3)'s return value.
2991 */
2992static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
2993{
2994 struct sample tmp;
2995 int max, result;
2996
2997 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2998 if (arg_p[0].type != ARGT_VAR)
2999 return 0;
Willy Tarreau2476ff12021-10-06 15:30:52 +02003000
3001 if (!sample_conv_var2smp(&arg_p[0].data.var, &tmp, SMP_T_STR))
Tim Duesterhusca097c12018-04-27 21:18:45 +02003002 return 0;
3003
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003004 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
3005 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003006 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003007 if (smp->data.u.str.data != tmp.data.u.str.data) {
3008 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02003009 result = -1;
3010 }
3011 else {
3012 result = 1;
3013 }
3014 }
3015 }
3016
3017 smp->data.u.sint = result;
3018 smp->data.type = SMP_T_SINT;
3019 return 1;
3020}
3021
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02003022/* Takes a boolean as input. Returns the first argument if that boolean is true and
3023 * the second argument otherwise.
3024 */
3025static int sample_conv_iif(const struct arg *arg_p, struct sample *smp, void *private)
3026{
3027 smp->data.type = SMP_T_STR;
3028 smp->flags |= SMP_F_CONST;
3029
3030 if (smp->data.u.sint) {
3031 smp->data.u.str.data = arg_p[0].data.str.data;
3032 smp->data.u.str.area = arg_p[0].data.str.area;
3033 }
3034 else {
3035 smp->data.u.str.data = arg_p[1].data.str.data;
3036 smp->data.u.str.area = arg_p[1].data.str.area;
3037 }
3038
3039 return 1;
3040}
3041
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003042#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
3043#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
3044#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
3045
3046/*
3047 * Extract the field value of an input binary sample. Takes a mandatory argument:
3048 * the protocol buffers field identifier (dotted notation) internally represented
3049 * as an array of unsigned integers and its size.
3050 * Return 1 if the field was found, 0 if not.
3051 */
3052static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
3053{
3054 unsigned char *pos;
3055 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003056
3057 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003058 grpc_left = smp->data.u.str.data;
3059
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01003060 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003061 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003062
3063 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
3064
3065 pos += GRPC_MSG_HEADER_SZ;
3066 grpc_left -= GRPC_MSG_HEADER_SZ;
3067
3068 if (grpc_left < left)
3069 return 0;
3070
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01003071 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
3072 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003073
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003074 grpc_left -= grpc_msg_len;
3075 }
3076
3077 return 0;
3078}
3079
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003080static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
3081{
3082 unsigned char *pos;
3083 size_t left;
3084
3085 pos = (unsigned char *)smp->data.u.str.area;
3086 left = smp->data.u.str.data;
3087
3088 return protobuf_field_lookup(arg_p, smp, &pos, &left);
3089}
3090
3091static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
3092 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003093{
3094 if (!args[1].type) {
3095 args[1].type = ARGT_SINT;
3096 args[1].data.sint = PBUF_T_BINARY;
3097 }
3098 else {
3099 int pbuf_type;
3100
3101 pbuf_type = protobuf_type(args[1].data.str.area);
3102 if (pbuf_type == -1) {
3103 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
3104 return 0;
3105 }
3106
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003107 chunk_destroy(&args[1].data.str);
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003108 args[1].type = ARGT_SINT;
3109 args[1].data.sint = pbuf_type;
3110 }
3111
3112 return 1;
3113}
3114
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003115/*
3116 * Extract the tag value of an input binary sample. Takes a mandatory argument:
3117 * the FIX protocol tag identifier.
3118 * Return 1 if the tag was found, 0 if not.
3119 */
3120static int sample_conv_fix_tag_value(const struct arg *arg_p, struct sample *smp, void *private)
3121{
3122 struct ist value;
3123
3124 smp->flags &= ~SMP_F_MAY_CHANGE;
3125 value = fix_tag_value(ist2(smp->data.u.str.area, smp->data.u.str.data),
3126 arg_p[0].data.sint);
3127 if (!istlen(value)) {
3128 if (!isttest(value)) {
3129 /* value != IST_NULL, need more data */
3130 smp->flags |= SMP_F_MAY_CHANGE;
3131 }
3132 return 0;
3133 }
3134
3135 smp->data.u.str = ist2buf(value);
3136 smp->flags |= SMP_F_CONST;
3137
3138 return 1;
3139}
3140
3141/* This function checks the "fix_tag_value" converter configuration.
3142 * It expects a "known" (by HAProxy) tag name or ID.
3143 * Tag string names are converted to their ID counterpart because this is the
3144 * format they are sent over the wire.
3145 */
3146static int sample_conv_fix_value_check(struct arg *args, struct sample_conv *conv,
3147 const char *file, int line, char **err)
3148{
3149 struct ist str;
3150 unsigned int tag;
3151
3152 str = ist2(args[0].data.str.area, args[0].data.str.data);
3153 tag = fix_tagid(str);
3154 if (!tag) {
3155 memprintf(err, "Unknown FIX tag name '%s'", args[0].data.str.area);
3156 return 0;
3157 }
3158
3159 chunk_destroy(&args[0].data.str);
3160 args[0].type = ARGT_SINT;
3161 args[0].data.sint = tag;
3162
3163 return 1;
3164}
3165
3166/*
3167 * Checks that a buffer contains a valid FIX message
3168 *
3169 * Return 1 if the check could be run, 0 if not.
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003170 * The result of the analyse itself is stored in <smp> as a boolean
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003171 */
3172static int sample_conv_fix_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3173{
3174 struct ist msg;
3175
3176 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3177
3178 smp->flags &= ~SMP_F_MAY_CHANGE;
3179 switch (fix_validate_message(msg)) {
3180 case FIX_VALID_MESSAGE:
3181 smp->data.type = SMP_T_BOOL;
3182 smp->data.u.sint = 1;
3183 return 1;
3184 case FIX_NEED_MORE_DATA:
3185 smp->flags |= SMP_F_MAY_CHANGE;
3186 return 0;
3187 case FIX_INVALID_MESSAGE:
3188 smp->data.type = SMP_T_BOOL;
3189 smp->data.u.sint = 0;
3190 return 1;
3191 }
3192 return 0;
3193}
3194
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003195/*
3196 * Extract the field value of an input binary sample containing an MQTT packet.
3197 * Takes 2 mandatory arguments:
3198 * - packet type
3199 * - field name
3200 *
3201 * return 1 if the field was found, 0 if not.
3202 */
3203static int sample_conv_mqtt_field_value(const struct arg *arg_p, struct sample *smp, void *private)
3204{
3205 struct ist pkt, value;
3206 int type, fieldname_id;
3207
3208 pkt = ist2(smp->data.u.str.area, smp->data.u.str.data);
3209 type = arg_p[0].data.sint;
3210 fieldname_id = arg_p[1].data.sint;
3211
3212 smp->flags &= ~SMP_F_MAY_CHANGE;
3213 value = mqtt_field_value(pkt, type, fieldname_id);
3214 if (!istlen(value)) {
3215 if (isttest(value)) {
3216 /* value != IST_NULL, need more data */
3217 smp->flags |= SMP_F_MAY_CHANGE;
3218 }
3219 return 0;
3220 }
3221
3222 smp->data.u.str = ist2buf(value);
3223 smp->flags |= SMP_F_CONST;
3224 return 1;
3225}
3226
3227/*
3228 * this function checks the "mqtt_field_value" converter configuration.
3229 * It expects a known packet type name or ID and a field name, in this order
3230 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003231 * Args[0] will be turned into a MQTT_CPT_* value for direct matching when parsing
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003232 * a packet.
3233 */
3234static int sample_conv_mqtt_field_value_check(struct arg *args, struct sample_conv *conv,
3235 const char *file, int line, char **err)
3236{
3237 int type, fieldname_id;
3238
3239 /* check the MQTT packet type is valid */
3240 type = mqtt_typeid(ist2(args[0].data.str.area, args[0].data.str.data));
3241 if (type == MQTT_CPT_INVALID) {
3242 memprintf(err, "Unknown MQTT type '%s'", args[0].data.str.area);
3243 return 0;
3244 }
3245
3246 /* check the field name belongs to the MQTT packet type */
3247 fieldname_id = mqtt_check_type_fieldname(type, ist2(args[1].data.str.area, args[1].data.str.data));
3248 if (fieldname_id == MQTT_FN_INVALID) {
3249 memprintf(err, "Unknown MQTT field name '%s' for packet type '%s'", args[1].data.str.area,
3250 args[0].data.str.area);
3251 return 0;
3252 }
3253
3254 /* save numeric counterparts of type and field name */
3255 chunk_destroy(&args[0].data.str);
3256 chunk_destroy(&args[1].data.str);
3257 args[0].type = ARGT_SINT;
3258 args[0].data.sint = type;
3259 args[1].type = ARGT_SINT;
3260 args[1].data.sint = fieldname_id;
3261
3262 return 1;
3263}
3264
3265/*
3266 * Checks that <smp> contains a valid MQTT message
3267 *
3268 * The function returns 1 if the check was run to its end, 0 otherwise.
3269 * The result of the analyse itself is stored in <smp> as a boolean.
3270 */
3271static int sample_conv_mqtt_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3272{
3273 struct ist msg;
3274
3275 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3276
3277 smp->flags &= ~SMP_F_MAY_CHANGE;
3278 switch (mqtt_validate_message(msg, NULL)) {
3279 case FIX_VALID_MESSAGE:
3280 smp->data.type = SMP_T_BOOL;
3281 smp->data.u.sint = 1;
3282 return 1;
3283 case FIX_NEED_MORE_DATA:
3284 smp->flags |= SMP_F_MAY_CHANGE;
3285 return 0;
3286 case FIX_INVALID_MESSAGE:
3287 smp->data.type = SMP_T_BOOL;
3288 smp->data.u.sint = 0;
3289 return 1;
3290 }
3291 return 0;
3292}
3293
Tim Duesterhusca097c12018-04-27 21:18:45 +02003294/* This function checks the "strcmp" converter's arguments and extracts the
3295 * variable name and its scope.
3296 */
3297static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
3298 const char *file, int line, char **err)
3299{
Willy Tarreaua1169b62021-05-08 06:50:28 +02003300 if (!args[0].data.str.data) {
3301 memprintf(err, "missing variable name");
3302 return 0;
3303 }
3304
Tim Duesterhusca097c12018-04-27 21:18:45 +02003305 /* Try to decode a variable. */
3306 if (vars_check_arg(&args[0], NULL))
3307 return 1;
3308
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003309 memprintf(err, "failed to register variable name '%s'",
3310 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003311 return 0;
3312}
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003313
Christopher Faulet4ccc12f2020-04-01 09:08:32 +02003314/**/
3315static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private)
3316{
3317 struct buffer *tmp;
3318 uint32_t n;
3319
3320 n = htonl((uint32_t)smp->data.u.sint);
3321 tmp = get_trash_chunk();
3322
3323 memcpy(b_head(tmp), &n, 4);
3324 b_add(tmp, 4);
3325
3326 smp->data.u.str = *tmp;
3327 smp->data.type = SMP_T_BIN;
3328 return 1;
3329}
3330
Christopher Fauletea159d62020-04-01 16:21:44 +02003331/**/
3332static int sample_conv_cut_crlf(const struct arg *arg_p, struct sample *smp, void *private)
3333{
3334 char *p;
3335 size_t l;
3336
3337 p = smp->data.u.str.area;
3338 for (l = 0; l < smp->data.u.str.data; l++) {
3339 if (*(p+l) == '\r' || *(p+l) == '\n')
3340 break;
3341 }
3342 smp->data.u.str.data = l;
3343 return 1;
3344}
3345
Christopher Faulet51fc9d12020-04-01 17:24:41 +02003346/**/
3347static int sample_conv_ltrim(const struct arg *arg_p, struct sample *smp, void *private)
3348{
3349 char *delimiters, *p;
3350 size_t dlen, l;
3351
3352 delimiters = arg_p[0].data.str.area;
3353 dlen = arg_p[0].data.str.data;
3354
3355 l = smp->data.u.str.data;
3356 p = smp->data.u.str.area;
3357 while (l && memchr(delimiters, *p, dlen) != NULL) {
3358 p++;
3359 l--;
3360 }
3361
3362 smp->data.u.str.area = p;
3363 smp->data.u.str.data = l;
3364 return 1;
3365}
3366
Christopher Faulet568415a2020-04-01 17:24:47 +02003367/**/
3368static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *private)
3369{
3370 char *delimiters, *p;
3371 size_t dlen, l;
3372
3373 delimiters = arg_p[0].data.str.area;
3374 dlen = arg_p[0].data.str.data;
3375
3376 l = smp->data.u.str.data;
3377 p = smp->data.u.str.area + l - 1;
3378 while (l && memchr(delimiters, *p, dlen) != NULL) {
3379 p--;
3380 l--;
3381 }
3382
3383 smp->data.u.str.data = l;
3384 return 1;
3385}
3386
Alex51c8ad42021-04-15 16:45:15 +02003387/* This function checks the "json_query" converter's arguments. */
3388static int sample_check_json_query(struct arg *arg, struct sample_conv *conv,
3389 const char *file, int line, char **err)
3390{
3391 if (arg[0].data.str.data == 0) {
3392 memprintf(err, "json_path must not be empty");
3393 return 0;
3394 }
3395
3396 if (arg[1].data.str.data != 0) {
3397 if (strcmp(arg[1].data.str.area, "int") != 0) {
3398 memprintf(err, "output_type only supports \"int\" as argument");
3399 return 0;
3400 } else {
3401 arg[1].type = ARGT_SINT;
3402 arg[1].data.sint = 0;
3403 }
3404 }
3405 return 1;
3406}
3407
3408/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
3409 * the recommendation for interoperable integers in section 6 of RFC 7159.
3410 */
3411#define JSON_INT_MAX ((1LL << 53) - 1)
3412#define JSON_INT_MIN (-JSON_INT_MAX)
3413
3414/* This sample function get the value from a given json string.
3415 * The mjson library is used to parse the JSON struct
3416 */
3417static int sample_conv_json_query(const struct arg *args, struct sample *smp, void *private)
3418{
3419 struct buffer *trash = get_trash_chunk();
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003420 const char *token; /* holds the temporary string from mjson_find */
3421 int token_size; /* holds the length of <token> */
Alex51c8ad42021-04-15 16:45:15 +02003422
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003423 enum mjson_tok token_type;
Alex51c8ad42021-04-15 16:45:15 +02003424
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003425 token_type = mjson_find(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &token, &token_size);
3426
3427 switch (token_type) {
Alex51c8ad42021-04-15 16:45:15 +02003428 case MJSON_TOK_NUMBER:
3429 if (args[1].type == ARGT_SINT) {
Willy Tarreaufb601952021-05-14 08:51:53 +02003430 smp->data.u.sint = strtoll(token, NULL, 0);
Alex51c8ad42021-04-15 16:45:15 +02003431
3432 if (smp->data.u.sint < JSON_INT_MIN || smp->data.u.sint > JSON_INT_MAX)
3433 return 0;
3434
3435 smp->data.type = SMP_T_SINT;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003436
3437 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003438 } else {
3439 double double_val;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003440
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003441 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 +02003442 return 0;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003443
3444 trash->data = snprintf(trash->area,trash->size,"%g",double_val);
3445 smp->data.u.str = *trash;
3446 smp->data.type = SMP_T_STR;
3447
3448 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003449 }
Alex51c8ad42021-04-15 16:45:15 +02003450 case MJSON_TOK_TRUE:
3451 smp->data.type = SMP_T_BOOL;
3452 smp->data.u.sint = 1;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003453
3454 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003455 case MJSON_TOK_FALSE:
3456 smp->data.type = SMP_T_BOOL;
3457 smp->data.u.sint = 0;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003458
3459 return 1;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003460 case MJSON_TOK_STRING: {
3461 int len;
3462
3463 len = mjson_get_string(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, trash->area, trash->size);
3464
3465 if (len == -1) {
Alex51c8ad42021-04-15 16:45:15 +02003466 /* invalid string */
3467 return 0;
Alex51c8ad42021-04-15 16:45:15 +02003468 }
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003469
3470 trash->data = len;
3471 smp->data.u.str = *trash;
3472 smp->data.type = SMP_T_STR;
3473
3474 return 1;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003475 }
Tim Duesterhus8f3bc8f2021-04-15 18:14:32 +02003476 case MJSON_TOK_NULL:
3477 case MJSON_TOK_ARRAY:
3478 case MJSON_TOK_OBJECT:
3479 /* We cannot handle these. */
3480 return 0;
3481 case MJSON_TOK_INVALID:
3482 /* Nothing matches the query. */
3483 return 0;
3484 case MJSON_TOK_KEY:
3485 /* This is not a valid return value according to the
3486 * mjson documentation, but we handle it to benefit
3487 * from '-Wswitch'.
3488 */
Alex51c8ad42021-04-15 16:45:15 +02003489 return 0;
3490 }
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003491
3492 my_unreachable();
3493 return 0;
Alex51c8ad42021-04-15 16:45:15 +02003494}
3495
3496
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003497/************************************************************************/
3498/* All supported sample fetch functions must be declared here */
3499/************************************************************************/
3500
3501/* force TRUE to be returned at the fetch level */
3502static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003503smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003504{
Willy Tarreau280f42b2018-02-19 15:34:12 +01003505 if (!smp_make_rw(smp))
3506 return 0;
3507
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003508 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003509 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003510 return 1;
3511}
3512
3513/* force FALSE to be returned at the fetch level */
3514static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003515smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003516{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003517 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003518 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003519 return 1;
3520}
3521
3522/* retrieve environment variable $1 as a string */
3523static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003524smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003525{
3526 char *env;
3527
Christopher Faulete6e7a582021-01-29 11:29:28 +01003528 if (args[0].type != ARGT_STR)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003529 return 0;
3530
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003531 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003532 if (!env)
3533 return 0;
3534
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003535 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003536 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003537 smp->data.u.str.area = env;
3538 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003539 return 1;
3540}
3541
Damien Claisseae6f1252019-10-30 15:57:28 +00003542/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3543 * optional string representing the unit of the result: "s" for seconds, "ms" for
3544 * milliseconds and "us" for microseconds.
3545 * Returns 0 on error and non-zero if OK.
3546 */
3547int smp_check_date_unit(struct arg *args, char **err)
3548{
3549 if (args[1].type == ARGT_STR) {
Christopher Faulet95917132020-08-05 23:07:07 +02003550 long long int unit;
3551
Damien Claisseae6f1252019-10-30 15:57:28 +00003552 if (strcmp(args[1].data.str.area, "s") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003553 unit = TIME_UNIT_S;
Damien Claisseae6f1252019-10-30 15:57:28 +00003554 }
3555 else if (strcmp(args[1].data.str.area, "ms") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003556 unit = TIME_UNIT_MS;
Damien Claisseae6f1252019-10-30 15:57:28 +00003557 }
3558 else if (strcmp(args[1].data.str.area, "us") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003559 unit = TIME_UNIT_US;
Damien Claisseae6f1252019-10-30 15:57:28 +00003560 }
3561 else {
3562 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
3563 args[1].data.str.area);
3564 return 0;
3565 }
Christopher Faulet95917132020-08-05 23:07:07 +02003566
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003567 chunk_destroy(&args[1].data.str);
Damien Claisseae6f1252019-10-30 15:57:28 +00003568 args[1].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02003569 args[1].data.sint = unit;
Damien Claisseae6f1252019-10-30 15:57:28 +00003570 }
3571 else if (args[1].type != ARGT_STOP) {
3572 memprintf(err, "Unexpected arg type");
3573 return 0;
3574 }
3575
3576 return 1;
3577}
3578
3579/* retrieve the current local date in epoch time, converts it to milliseconds
3580 * or microseconds if asked to in optional args[1] unit param, and applies an
3581 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003582 */
3583static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003584smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003585{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003586 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003587
Damien Claisseae6f1252019-10-30 15:57:28 +00003588 /* report in milliseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003589 if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003590 smp->data.u.sint *= 1000;
3591 smp->data.u.sint += date.tv_usec / 1000;
3592 }
3593 /* report in microseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003594 else if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003595 smp->data.u.sint *= 1000000;
3596 smp->data.u.sint += date.tv_usec;
3597 }
3598
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003599 /* add offset */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003600 if (args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003601 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003602
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003603 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003604 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3605 return 1;
3606}
3607
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003608/* retrieve the current microsecond part of the date */
3609static int
3610smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3611{
3612 smp->data.u.sint = date.tv_usec;
3613 smp->data.type = SMP_T_SINT;
3614 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3615 return 1;
3616}
3617
3618
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003619/* returns the hostname */
3620static int
3621smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3622{
3623 smp->data.type = SMP_T_STR;
3624 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003625 smp->data.u.str.area = hostname;
3626 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003627 return 1;
3628}
3629
Willy Tarreau0f30d262014-11-24 16:02:05 +01003630/* returns the number of processes */
3631static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003632smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003633{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003634 smp->data.type = SMP_T_SINT;
Willy Tarreau91358592021-06-15 08:08:04 +02003635 smp->data.u.sint = 1;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003636 return 1;
3637}
3638
3639/* returns the number of the current process (between 1 and nbproc */
3640static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003641smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003642{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003643 smp->data.type = SMP_T_SINT;
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003644 smp->data.u.sint = 1;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003645 return 1;
3646}
3647
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003648/* returns the number of the current thread (between 1 and nbthread */
3649static int
3650smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3651{
3652 smp->data.type = SMP_T_SINT;
3653 smp->data.u.sint = tid;
3654 return 1;
3655}
3656
Willy Tarreau84310e22014-02-14 11:59:04 +01003657/* generate a random 32-bit integer for whatever purpose, with an optional
3658 * range specified in argument.
3659 */
3660static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003661smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01003662{
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003663 smp->data.u.sint = ha_random32();
Willy Tarreau84310e22014-02-14 11:59:04 +01003664
3665 /* reduce if needed. Don't do a modulo, use all bits! */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003666 if (args[0].type == ARGT_SINT)
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003667 smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32;
Willy Tarreau84310e22014-02-14 11:59:04 +01003668
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003669 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01003670 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3671 return 1;
3672}
3673
Willy Tarreau0f30d262014-11-24 16:02:05 +01003674/* returns true if the current process is stopping */
3675static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003676smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003677{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003678 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003679 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003680 return 1;
3681}
3682
Willy Tarreau70fe9442018-11-22 16:07:39 +01003683/* returns the number of calls of the current stream's process_stream() */
3684static int
3685smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
3686{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003687 if (!smp->strm)
3688 return 0;
3689
Willy Tarreau70fe9442018-11-22 16:07:39 +01003690 smp->data.type = SMP_T_SINT;
3691 smp->data.u.sint = smp->strm->task->calls;
3692 return 1;
3693}
3694
3695/* returns the average number of nanoseconds spent processing the stream per call */
3696static int
3697smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3698{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003699 if (!smp->strm)
3700 return 0;
3701
Willy Tarreau70fe9442018-11-22 16:07:39 +01003702 smp->data.type = SMP_T_SINT;
3703 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
3704 return 1;
3705}
3706
3707/* returns the total number of nanoseconds spent processing the stream */
3708static int
3709smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3710{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003711 if (!smp->strm)
3712 return 0;
3713
Willy Tarreau70fe9442018-11-22 16:07:39 +01003714 smp->data.type = SMP_T_SINT;
3715 smp->data.u.sint = smp->strm->task->cpu_time;
3716 return 1;
3717}
3718
3719/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
3720static int
3721smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3722{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003723 if (!smp->strm)
3724 return 0;
3725
Willy Tarreau70fe9442018-11-22 16:07:39 +01003726 smp->data.type = SMP_T_SINT;
3727 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
3728 return 1;
3729}
3730
3731/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
3732static int
3733smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3734{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003735 if (!smp->strm)
3736 return 0;
3737
Willy Tarreau70fe9442018-11-22 16:07:39 +01003738 smp->data.type = SMP_T_SINT;
3739 smp->data.u.sint = smp->strm->task->lat_time;
3740 return 1;
3741}
3742
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003743static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
3744{
3745 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003746 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003747 smp->data.u.str.area = args[0].data.str.area;
3748 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003749 return 1;
3750}
3751
3752static int smp_check_const_bool(struct arg *args, char **err)
3753{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003754 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
3755 strcasecmp(args[0].data.str.area, "1") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003756 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003757 args[0].type = ARGT_SINT;
3758 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003759 return 1;
3760 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003761 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
3762 strcasecmp(args[0].data.str.area, "0") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003763 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003764 args[0].type = ARGT_SINT;
3765 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003766 return 1;
3767 }
3768 memprintf(err, "Expects 'true', 'false', '0' or '1'");
3769 return 0;
3770}
3771
3772static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
3773{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003774 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003775 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003776 return 1;
3777}
3778
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003779static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003780{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003781 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003782 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003783 return 1;
3784}
3785
3786static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
3787{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003788 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003789 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003790 return 1;
3791}
3792
3793static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
3794{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003795 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003796 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003797 return 1;
3798}
3799
3800static int smp_check_const_bin(struct arg *args, char **err)
3801{
David Carlier64a16ab2016-04-08 10:37:02 +01003802 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003803 int binstrlen;
3804
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003805 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003806 return 0;
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003807 chunk_destroy(&args[0].data.str);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003808 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003809 args[0].data.str.area = binstr;
3810 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003811 return 1;
3812}
3813
3814static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
3815{
3816 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003817 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003818 smp->data.u.str.area = args[0].data.str.area;
3819 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003820 return 1;
3821}
3822
3823static int smp_check_const_meth(struct arg *args, char **err)
3824{
3825 enum http_meth_t meth;
3826 int i;
3827
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003828 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003829 if (meth != HTTP_METH_OTHER) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003830 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003831 args[0].type = ARGT_SINT;
3832 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003833 } else {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003834 /* Check method avalaibility. A method is a token defined as :
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003835 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
3836 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
3837 * token = 1*tchar
3838 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003839 for (i = 0; i < args[0].data.str.data; i++) {
3840 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003841 memprintf(err, "expects valid method.");
3842 return 0;
3843 }
3844 }
3845 }
3846 return 1;
3847}
3848
3849static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
3850{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003851 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003852 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003853 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003854 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003855 smp->data.u.meth.str.area = "";
3856 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003857 } else {
3858 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003859 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003860 smp->data.u.meth.str.area = args[0].data.str.area;
3861 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003862 }
3863 return 1;
3864}
3865
Luca Schimweg8a694b82019-09-10 15:42:52 +02003866// This function checks the "uuid" sample's arguments.
3867// Function won't get called when no parameter is specified (maybe a bug?)
3868static int smp_check_uuid(struct arg *args, char **err)
3869{
3870 if (!args[0].type) {
3871 args[0].type = ARGT_SINT;
3872 args[0].data.sint = 4;
3873 }
3874 else if (args[0].data.sint != 4) {
3875 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
3876 return 0;
3877 }
3878
3879 return 1;
3880}
3881
3882// Generate a RFC4122 UUID (default is v4 = fully random)
3883static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
3884{
3885 if (args[0].data.sint == 4 || !args[0].type) {
Willy Tarreauee3bcdd2020-03-08 17:48:17 +01003886 ha_generate_uuid(&trash);
Luca Schimweg8a694b82019-09-10 15:42:52 +02003887 smp->data.type = SMP_T_STR;
3888 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3889 smp->data.u.str = trash;
3890 return 1;
3891 }
3892
3893 // more implementations of other uuid formats possible here
3894 return 0;
3895}
3896
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003897/* Note: must not be declared <const> as its list will be overwritten.
3898 * Note: fetches that may return multiple types must be declared as the lowest
3899 * common denominator, the type that can be casted into all other ones. For
3900 * instance IPv4/IPv6 must be declared IPv4.
3901 */
3902static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau0209c972021-03-26 12:03:11 +01003903 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
3904 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
3905 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_CONST },
3906 { "date", smp_fetch_date, ARG2(0,SINT,STR), smp_check_date_unit, SMP_T_SINT, SMP_USE_CONST },
3907 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
3908 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_CONST },
3909 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_CONST },
3910 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
3911 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
3912 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_CONST },
Willy Tarreau0f30d262014-11-24 16:02:05 +01003913 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau0209c972021-03-26 12:03:11 +01003914 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_CONST },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003915
3916 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3917 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3918 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3919 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3920 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003921
Willy Tarreau0209c972021-03-26 12:03:11 +01003922 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_CONST },
3923 { "bool", smp_fetch_const_bool, ARG1(1,STR), smp_check_const_bool, SMP_T_BOOL, SMP_USE_CONST },
3924 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_CONST },
3925 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_CONST },
3926 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_CONST },
3927 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_CONST },
3928 { "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 +02003929
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003930 { /* END */ },
3931}};
3932
Willy Tarreau0108d902018-11-25 19:14:37 +01003933INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
3934
Emeric Brun107ca302010-01-04 16:16:05 +01003935/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003936static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Moemen MHEDHBI848216f2021-04-02 01:05:07 +02003937 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
3938 { "b64dec", sample_conv_base642bin, 0, NULL, SMP_T_STR, SMP_T_BIN },
3939 { "base64", sample_conv_bin2base64, 0, NULL, SMP_T_BIN, SMP_T_STR },
3940 { "ub64enc", sample_conv_bin2base64url,0, NULL, SMP_T_BIN, SMP_T_STR },
3941 { "ub64dec", sample_conv_base64url2bin,0, NULL, SMP_T_STR, SMP_T_BIN },
3942 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
3943 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
3944 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Marcin Deranek40ca09c2021-07-13 14:05:24 +02003945 { "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 +02003946 { "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 +02003947 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
3948 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
3949 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
3950 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
3951 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
3952 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3953 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3954 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3955 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3956 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3957 { "xxh3", sample_conv_xxh3, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3958 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3959 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3960 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
3961 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
3962 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
3963 { "word", sample_conv_word, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
3964 { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
3965 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Willy Tarreau99ea1882021-10-06 15:37:17 +02003966 { "concat", sample_conv_concat, ARG3(1,STR,STR,STR), smp_check_concat, SMP_T_STR, SMP_T_STR },
3967 { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT },
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003968
3969 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003970 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
3971 { "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 +01003972
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003973 /* FIX converters */
3974 { "fix_is_valid", sample_conv_fix_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
3975 { "fix_tag_value", sample_conv_fix_tag_value, ARG1(1,STR), sample_conv_fix_value_check, SMP_T_BIN, SMP_T_BIN },
3976
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003977 /* MQTT converters */
3978 { "mqtt_is_valid", sample_conv_mqtt_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
3979 { "mqtt_field_value", sample_conv_mqtt_field_value, ARG2(2,STR,STR), sample_conv_mqtt_field_value_check, SMP_T_BIN, SMP_T_STR },
3980
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02003981 { "iif", sample_conv_iif, ARG2(2, STR, STR), NULL, SMP_T_BOOL, SMP_T_STR },
3982
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003983 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3984 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3985 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3986 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
3987 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3988 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3989 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3990 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3991 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3992 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3993 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3994 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3995 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3996 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01003997
Christopher Fauletea159d62020-04-01 16:21:44 +02003998 { "htonl", sample_conv_htonl, 0, NULL, SMP_T_SINT, SMP_T_BIN },
3999 { "cut_crlf", sample_conv_cut_crlf, 0, NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet51fc9d12020-04-01 17:24:41 +02004000 { "ltrim", sample_conv_ltrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet568415a2020-04-01 17:24:47 +02004001 { "rtrim", sample_conv_rtrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Alex51c8ad42021-04-15 16:45:15 +02004002 { "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 +02004003 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01004004}};
4005
Willy Tarreau0108d902018-11-25 19:14:37 +01004006INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);