blob: 835a181156568e86727e36dbd59bb0b3dd3d503f [file] [log] [blame]
Emeric Brun107ca302010-01-04 16:16:05 +01001/*
Willy Tarreaucd3b0942012-04-27 21:52:18 +02002 * Sample management functions.
Emeric Brun107ca302010-01-04 16:16:05 +01003 *
4 * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreaucd3b0942012-04-27 21:52:18 +02005 * Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
Emeric Brun107ca302010-01-04 16:16:05 +01006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Thierry FOURNIER317e1c42014-08-12 10:20:47 +020014#include <ctype.h>
Emeric Brun107ca302010-01-04 16:16:05 +010015#include <string.h>
16#include <arpa/inet.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020017#include <stdio.h>
Emeric Brun107ca302010-01-04 16:16:05 +010018
Willy Tarreaub2551052020-06-09 09:07:15 +020019#include <import/sha1.h>
20#include <import/xxhash.h>
Willy Tarreau7e2c6472012-10-29 20:44:36 +010021
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/api.h>
23#include <haproxy/arg.h>
Willy Tarreauac13aea2020-06-04 10:36:03 +020024#include <haproxy/auth.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020025#include <haproxy/base64.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020026#include <haproxy/buf.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020027#include <haproxy/chunk.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020028#include <haproxy/errors.h>
Baptiste Assmanne138dda2020-10-22 15:39:03 +020029#include <haproxy/fix.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020030#include <haproxy/global.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020031#include <haproxy/hash.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020032#include <haproxy/http.h>
Baptiste Assmanne138dda2020-10-22 15:39:03 +020033#include <haproxy/istbuf.h>
Baptiste Assmanne279ca62020-10-27 18:10:06 +010034#include <haproxy/mqtt.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020035#include <haproxy/net_helper.h>
Willy Tarreaub23e5952020-06-04 16:06:59 +020036#include <haproxy/protobuf.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020037#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020038#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020039#include <haproxy/sample.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020040#include <haproxy/sink.h>
41#include <haproxy/stick_table.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020042#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020043#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020044#include <haproxy/vars.h>
Thierry FOURNIER01e09742016-12-26 11:46:11 +010045
Willy Tarreau1cf8f082014-02-07 12:14:54 +010046/* sample type names */
47const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020048 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010049 [SMP_T_BOOL] = "bool",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010050 [SMP_T_SINT] = "sint",
51 [SMP_T_ADDR] = "addr",
52 [SMP_T_IPV4] = "ipv4",
53 [SMP_T_IPV6] = "ipv6",
54 [SMP_T_STR] = "str",
55 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020056 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010057};
58
Willy Tarreau12785782012-04-27 21:37:17 +020059/* static sample used in sample_process() when <p> is NULL */
Emeric Brune5c918b2017-06-14 14:15:36 +020060static THREAD_LOCAL struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010061
Willy Tarreau12785782012-04-27 21:37:17 +020062/* list head of all known sample fetch keywords */
63static struct sample_fetch_kw_list sample_fetches = {
64 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010065};
66
Willy Tarreau12785782012-04-27 21:37:17 +020067/* list head of all known sample format conversion keywords */
68static struct sample_conv_kw_list sample_convs = {
69 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010070};
71
Willy Tarreau80aca902013-01-07 15:42:20 +010072const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
Willy Tarreaube2159b2021-03-26 11:56:11 +010073 [SMP_SRC_CONST] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
74 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
75 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
76 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
77 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
78 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +010079 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL_CFG_PARSER |
80 SMP_VAL_CLI_PARSER ),
Willy Tarreaube2159b2021-03-26 11:56:11 +010081
Willy Tarreau80aca902013-01-07 15:42:20 +010082 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
83 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
84 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
85 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
86 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
87 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +010088 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
89 SMP_VAL_CLI_PARSER ),
Willy Tarreau80aca902013-01-07 15:42:20 +010090
91 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
92 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
93 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
94 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
95 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
96 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +010097 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
98 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +010099
100 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
101 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
102 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
103 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
104 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
105 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100106 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
107 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100108
109 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
110 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
111 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
112 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
113 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
114 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100115 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
116 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100117
118 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
119 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
120 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
121 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
122 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
123 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100124 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
125 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100126
127 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
128 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
129 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
130 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
131 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
132 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100133 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
134 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100135
136 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
137 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
138 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
139 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
140 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
141 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100142 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
143 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100144
145 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
146 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
147 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
148 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
149 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
150 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100151 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
152 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100153
154 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
155 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
156 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
157 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
158 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
159 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100160 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
161 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100162
163 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
164 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
165 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
166 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
167 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
168 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100169 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
170 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100171
172 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
173 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
174 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
175 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
176 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
177 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100178 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
179 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100180
181 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
182 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
183 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
184 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
185 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
186 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100187 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
188 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100189
190 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
191 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
192 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
193 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
194 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
195 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100196 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
197 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100198
199 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
200 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
201 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
202 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
203 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
204 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100205 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
206 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100207
208 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
209 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
210 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
211 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
212 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
213 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100214 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
215 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100216
217 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
218 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
219 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
220 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
221 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
222 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100223 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
224 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100225
226 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
227 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
228 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
229 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
230 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
231 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100232 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
233 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100234
235 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
236 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
237 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
238 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
239 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
240 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100241 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
242 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100243
244 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
245 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
246 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
247 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
248 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
249 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100250 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
251 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100252
253 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
254 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
255 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
256 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
257 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
258 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100259 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
260 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100261
262 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
263 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
264 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
265 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
266 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
267 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100268 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
269 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100270
271 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
272 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
273 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
274 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
275 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
276 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100277 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
278 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100279};
280
281static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
282 [SMP_SRC_INTRN] = "internal state",
283 [SMP_SRC_LISTN] = "listener",
284 [SMP_SRC_FTEND] = "frontend",
285 [SMP_SRC_L4CLI] = "client address",
286 [SMP_SRC_L5CLI] = "client-side connection",
287 [SMP_SRC_TRACK] = "track counters",
288 [SMP_SRC_L6REQ] = "request buffer",
289 [SMP_SRC_HRQHV] = "HTTP request headers",
290 [SMP_SRC_HRQHP] = "HTTP request",
291 [SMP_SRC_HRQBO] = "HTTP request body",
292 [SMP_SRC_BKEND] = "backend",
293 [SMP_SRC_SERVR] = "server",
294 [SMP_SRC_L4SRV] = "server address",
295 [SMP_SRC_L5SRV] = "server-side connection",
296 [SMP_SRC_L6RES] = "response buffer",
297 [SMP_SRC_HRSHV] = "HTTP response headers",
298 [SMP_SRC_HRSHP] = "HTTP response",
299 [SMP_SRC_HRSBO] = "HTTP response body",
300 [SMP_SRC_RQFIN] = "request buffer statistics",
301 [SMP_SRC_RSFIN] = "response buffer statistics",
302 [SMP_SRC_TXFIN] = "transaction statistics",
303 [SMP_SRC_SSFIN] = "session statistics",
304};
305
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100306static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
307 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
308 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
309 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
310 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
311 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
312 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
313 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
314 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
315 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
316 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
317 [SMP_CKP_BE_SRV_CON] = "server source selection",
318 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
319 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
320 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
321 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
322 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
323 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
324 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
325 [SMP_CKP_FE_LOG_END] = "logs",
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100326 [SMP_CKP_BE_CHK_RUL] = "backend tcp-check rule",
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100327};
328
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100329/* This function returns the type of the data returned by the sample_expr.
330 * It assumes that the <expr> and all of its converters are properly
331 * initialized.
332 */
333inline
334int smp_expr_output_type(struct sample_expr *expr)
335{
336 struct sample_conv_expr *smp_expr;
337
338 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
339 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
340 return smp_expr->conv->out_type;
341 }
342 return expr->fetch->out_type;
343}
344
345
Willy Tarreau80aca902013-01-07 15:42:20 +0100346/* fill the trash with a comma-delimited list of source names for the <use> bit
347 * field which must be composed of a non-null set of SMP_USE_* flags. The return
348 * value is the pointer to the string in the trash buffer.
349 */
350const char *sample_src_names(unsigned int use)
351{
352 int bit;
353
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200354 trash.data = 0;
355 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100356 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
357 if (!(use & ~((1 << bit) - 1)))
358 break; /* no more bits */
359
360 if (!(use & (1 << bit)))
361 continue; /* bit not set */
362
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200363 trash.data += snprintf(trash.area + trash.data,
364 trash.size - trash.data, "%s%s",
365 (use & ((1 << bit) - 1)) ? "," : "",
366 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100367 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200368 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100369}
370
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100371/* return a pointer to the correct sample checkpoint name, or "unknown" when
372 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
373 * if set.
374 */
375const char *sample_ckp_names(unsigned int use)
376{
377 int bit;
378
379 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
380 if (use & (1 << bit))
381 return fetch_ckp_names[bit];
382 return "unknown sample check place, please report this bug";
383}
384
Emeric Brun107ca302010-01-04 16:16:05 +0100385/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100386 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
387 * for next parsing sessions. The fetch keywords capabilities are also computed
388 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100389 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100390void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100391{
Willy Tarreau80aca902013-01-07 15:42:20 +0100392 struct sample_fetch *sf;
393 int bit;
394
395 for (sf = kwl->kw; sf->kw != NULL; sf++) {
396 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
397 if (sf->use & (1 << bit))
398 sf->val |= fetch_cap[bit];
399 }
400 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100401}
402
403/*
Willy Tarreau12785782012-04-27 21:37:17 +0200404 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100405 * parsing sessions.
406 */
Willy Tarreau12785782012-04-27 21:37:17 +0200407void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100408{
Willy Tarreau12785782012-04-27 21:37:17 +0200409 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100410}
411
412/*
Willy Tarreau12785782012-04-27 21:37:17 +0200413 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100414 * string of <len> in buffer <kw>.
415 *
416 */
Willy Tarreau12785782012-04-27 21:37:17 +0200417struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100418{
419 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200420 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100421
Willy Tarreau12785782012-04-27 21:37:17 +0200422 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100423 for (index = 0; kwl->kw[index].kw != NULL; index++) {
424 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
425 kwl->kw[index].kw[len] == '\0')
426 return &kwl->kw[index];
427 }
428 }
429 return NULL;
430}
431
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100432/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100433 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100434 * <idx> is the index of the next sample fetch entry. It is used as private
435 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100436 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100437 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100438 * the list is reached.
439 */
440struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
441{
442 struct sample_fetch_kw_list *kwl;
443 struct sample_fetch *base;
444
445 if (!current) {
446 /* Get first kwl entry. */
447 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
448 (*idx) = 0;
449 } else {
450 /* Get kwl corresponding to the curret entry. */
451 base = current + 1 - (*idx);
452 kwl = container_of(base, struct sample_fetch_kw_list, kw);
453 }
454
455 while (1) {
456
457 /* Check if kwl is the last entry. */
458 if (&kwl->list == &sample_fetches.list)
459 return NULL;
460
461 /* idx contain the next keyword. If it is available, return it. */
462 if (kwl->kw[*idx].kw) {
463 (*idx)++;
464 return &kwl->kw[(*idx)-1];
465 }
466
467 /* get next entry in the main list, and return NULL if the end is reached. */
468 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
469
470 /* Set index to 0, ans do one other loop. */
471 (*idx) = 0;
472 }
473}
474
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100475/* This function browses the list of available converters. <current> is
476 * the last used converter. If it is the first call, it must set to NULL.
477 * <idx> is the index of the next converter entry. It is used as private
478 * value. It is useless to initiate it.
479 *
480 * It returns always the next sample_conv entry, and NULL when the end of
481 * the list is reached.
482 */
483struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
484{
485 struct sample_conv_kw_list *kwl;
486 struct sample_conv *base;
487
488 if (!current) {
489 /* Get first kwl entry. */
490 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
491 (*idx) = 0;
492 } else {
493 /* Get kwl corresponding to the curret entry. */
494 base = current + 1 - (*idx);
495 kwl = container_of(base, struct sample_conv_kw_list, kw);
496 }
497
498 while (1) {
499 /* Check if kwl is the last entry. */
500 if (&kwl->list == &sample_convs.list)
501 return NULL;
502
503 /* idx contain the next keyword. If it is available, return it. */
504 if (kwl->kw[*idx].kw) {
505 (*idx)++;
506 return &kwl->kw[(*idx)-1];
507 }
508
509 /* get next entry in the main list, and return NULL if the end is reached. */
510 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
511
512 /* Set index to 0, ans do one other loop. */
513 (*idx) = 0;
514 }
515}
516
Emeric Brun107ca302010-01-04 16:16:05 +0100517/*
Willy Tarreau12785782012-04-27 21:37:17 +0200518 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100519 * string of <len> in buffer <kw>.
520 *
521 */
Willy Tarreau12785782012-04-27 21:37:17 +0200522struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100523{
524 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200525 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100526
Willy Tarreau12785782012-04-27 21:37:17 +0200527 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100528 for (index = 0; kwl->kw[index].kw != NULL; index++) {
529 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
530 kwl->kw[index].kw[len] == '\0')
531 return &kwl->kw[index];
532 }
533 }
534 return NULL;
535}
536
Emeric Brun107ca302010-01-04 16:16:05 +0100537/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200538/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100539/******************************************************************/
540
Willy Tarreau342acb42012-04-23 22:03:39 +0200541static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100542{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200543 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200544 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100545 return 1;
546}
547
Willy Tarreau342acb42012-04-23 22:03:39 +0200548static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100549{
Willy Tarreau83061a82018-07-13 11:56:34 +0200550 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100551
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200552 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100553 return 0;
554
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200555 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200556 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200557 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100558 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100559
560 return 1;
561}
562
Willy Tarreau342acb42012-04-23 22:03:39 +0200563static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100564{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200565 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200566 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100567 return 1;
568}
569
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200570static int c_ipv62ip(struct sample *smp)
571{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200572 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200573 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100574 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200575 return 1;
576}
577
Willy Tarreau342acb42012-04-23 22:03:39 +0200578static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100579{
Willy Tarreau83061a82018-07-13 11:56:34 +0200580 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100581
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200582 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100583 return 0;
584
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200585 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200586 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200587 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100588 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100589 return 1;
590}
591
592/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200593static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100594{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200595 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100596}
597*/
598
Willy Tarreau342acb42012-04-23 22:03:39 +0200599static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100600{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200601 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200602 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100603 return 1;
604}
605
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200606static int c_int2ipv6(struct sample *smp)
607{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200608 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
609 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200610 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200611 return 1;
612}
613
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100614static int c_str2addr(struct sample *smp)
615{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200616 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
617 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100618 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200619 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100620 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100621 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100622 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200623 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100624 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100625 return 1;
626}
627
Willy Tarreau342acb42012-04-23 22:03:39 +0200628static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100629{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200630 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100631 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200632 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100633 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100634 return 1;
635}
636
Willy Tarreau342acb42012-04-23 22:03:39 +0200637static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100638{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200639 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100640 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200641 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100642 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100643 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100644}
645
Emeric Brun4b9e8022014-11-03 18:17:10 +0100646/*
647 * The NULL char always enforces the end of string if it is met.
648 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100649 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200650static int c_bin2str(struct sample *smp)
651{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100652 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200653
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200654 for (i = 0; i < smp->data.u.str.data; i++) {
655 if (!smp->data.u.str.area[i]) {
656 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100657 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100658 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200659 }
Christopher Faulet472ad512020-04-30 09:57:40 +0200660 smp->data.type = SMP_T_STR;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200661 return 1;
662}
663
Willy Tarreau342acb42012-04-23 22:03:39 +0200664static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100665{
Willy Tarreau83061a82018-07-13 11:56:34 +0200666 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100667 char *pos;
668
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200669 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100670 if (!pos)
671 return 0;
672
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200673 trash->size = trash->size - (pos - trash->area);
674 trash->area = pos;
675 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200676 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200677 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100678 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100679 return 1;
680}
681
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800682/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200683 * For strings and binary blocks, it also provides a known allocated size with
684 * a length that is capped to the size, and ensures a trailing zero is always
685 * appended for strings. This is necessary for some operations which may
686 * require to extend the length. It returns 0 if it fails, 1 on success.
687 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100688int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200689{
Willy Tarreau83061a82018-07-13 11:56:34 +0200690 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200691
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200692 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100693 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100694 case SMP_T_SINT:
695 case SMP_T_ADDR:
696 case SMP_T_IPV4:
697 case SMP_T_IPV6:
698 /* These type are not const. */
699 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200700
Christopher Fauletec100512017-07-24 15:38:41 +0200701 case SMP_T_METH:
702 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
703 break;
704 /* Fall through */
705
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100706 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200707 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100708 trash->data = smp->data.type == SMP_T_STR ?
709 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200710 if (trash->data > trash->size - 1)
711 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200712
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100713 memcpy(trash->area, smp->data.type == SMP_T_STR ?
714 smp->data.u.str.area : smp->data.u.meth.str.area,
715 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200716 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200717 smp->data.u.str = *trash;
718 break;
719
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100720 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100721 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200722 trash->data = smp->data.u.str.data;
723 if (trash->data > trash->size)
724 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200725
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200726 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200727 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100728 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200729
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100730 default:
731 /* Other cases are unexpected. */
732 return 0;
733 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100734
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100735 /* remove const flag */
736 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200737 return 1;
738}
739
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100740int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100741{
742 return 1;
743}
744
Willy Tarreau342acb42012-04-23 22:03:39 +0200745static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100746{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200747 const char *str;
748 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100749
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200750 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100751 return 0;
752
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200753 str = smp->data.u.str.area;
754 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100755
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200756 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200757 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100758 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100759 return 1;
760}
761
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100762static int c_str2meth(struct sample *smp)
763{
764 enum http_meth_t meth;
765 int len;
766
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200767 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100768 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200769 len = smp->data.u.str.data;
770 smp->data.u.meth.str.area = smp->data.u.str.area;
771 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100772 }
773 else
774 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200775 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200776 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100777 return 1;
778}
779
780static int c_meth2str(struct sample *smp)
781{
782 int len;
783 enum http_meth_t meth;
784
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200785 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100786 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200787 len = smp->data.u.meth.str.data;
788 smp->data.u.str.area = smp->data.u.meth.str.area;
789 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200790 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100791 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200792 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100793 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200794 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200795 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200796 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100797 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200798 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100799 }
800 else {
801 /* Unknown method */
802 return 0;
803 }
804 return 1;
805}
806
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200807static int c_addr2bin(struct sample *smp)
808{
Willy Tarreau83061a82018-07-13 11:56:34 +0200809 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200810
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200811 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200812 chk->data = 4;
813 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200814 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200815 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200816 chk->data = 16;
817 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200818 }
819 else
820 return 0;
821
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200822 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200823 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200824 return 1;
825}
826
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200827static int c_int2bin(struct sample *smp)
828{
Willy Tarreau83061a82018-07-13 11:56:34 +0200829 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200830
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200831 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
832 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200833
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200834 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200835 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200836 return 1;
837}
838
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200839
Emeric Brun107ca302010-01-04 16:16:05 +0100840/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200841/* Sample casts matrix: */
842/* sample_casts[from type][to type] */
843/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100844/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100845
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100846sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200847/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
848/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
849/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200850/* SINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, c_int2ipv6, c_int2str, c_int2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200851/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
852/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200853/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200854/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
855/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
856/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200857};
Emeric Brun107ca302010-01-04 16:16:05 +0100858
Emeric Brun107ca302010-01-04 16:16:05 +0100859/*
Willy Tarreau12785782012-04-27 21:37:17 +0200860 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100861 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200862 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200863 * The caller must have set al->ctx.
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100864 * If <endptr> is non-nul, it will be set to the first unparsed character
865 * (which may be the final '\0') on success. If it is nul, the expression
866 * must be properly terminated by a '\0' otherwise an error is reported.
Emeric Brun107ca302010-01-04 16:16:05 +0100867 */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100868struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err_msg, struct arg_list *al, char **endptr)
Emeric Brun107ca302010-01-04 16:16:05 +0100869{
Willy Tarreau833cc792013-07-24 15:34:19 +0200870 const char *begw; /* beginning of word */
871 const char *endw; /* end of word */
872 const char *endt; /* end of term */
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +0100873 struct sample_expr *expr = NULL;
Willy Tarreau12785782012-04-27 21:37:17 +0200874 struct sample_fetch *fetch;
875 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100876 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200877 char *fkw = NULL;
878 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100879 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100880
Willy Tarreau833cc792013-07-24 15:34:19 +0200881 begw = str[*idx];
Willy Tarreaued2c6622020-02-14 18:27:10 +0100882 for (endw = begw; is_idchar(*endw); endw++)
883 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200884
885 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100886 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100887 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200888 }
Emeric Brun107ca302010-01-04 16:16:05 +0100889
Willy Tarreau833cc792013-07-24 15:34:19 +0200890 /* keep a copy of the current fetch keyword for error reporting */
891 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100892
Willy Tarreau833cc792013-07-24 15:34:19 +0200893 fetch = find_sample_fetch(begw, endw - begw);
894 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100895 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100896 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200897 }
Emeric Brun107ca302010-01-04 16:16:05 +0100898
Willy Tarreau833cc792013-07-24 15:34:19 +0200899 /* At this point, we have :
900 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100901 * - endw : end of the keyword, first character not part of keyword
Willy Tarreau833cc792013-07-24 15:34:19 +0200902 */
903
904 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100905 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100906 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200907 }
Emeric Brun107ca302010-01-04 16:16:05 +0100908 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200909
Vincent Bernat02779b62016-04-03 13:48:43 +0200910 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200911 if (!expr)
912 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100913
914 LIST_INIT(&(expr->conv_exprs));
915 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200916 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100917
Willy Tarreau689a1df2013-12-13 00:40:11 +0100918 /* Note that we call the argument parser even with an empty string,
919 * this allows it to automatically create entries for mandatory
920 * implicit arguments (eg: local proxy name).
921 */
922 al->kw = expr->fetch->kw;
923 al->conv = NULL;
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100924 if (make_arg_list(endw, -1, fetch->arg_mask, &expr->arg_p, err_msg, &endt, &err_arg, al) < 0) {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100925 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
926 goto out_error;
927 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200928
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100929 /* now endt is our first char not part of the arg list, typically the
930 * comma after the sample fetch name or after the closing parenthesis,
931 * or the NUL char.
932 */
933
Willy Tarreau689a1df2013-12-13 00:40:11 +0100934 if (!expr->arg_p) {
935 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200936 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100937 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
938 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200939 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100940 }
941
Willy Tarreau833cc792013-07-24 15:34:19 +0200942 /* Now process the converters if any. We have two supported syntaxes
943 * for the converters, which can be combined :
944 * - comma-delimited list of converters just after the keyword and args ;
945 * - one converter per keyword
946 * The combination allows to have each keyword being a comma-delimited
947 * series of converters.
948 *
949 * We want to process the former first, then the latter. For this we start
950 * from the beginning of the supposed place in the exiting conv chain, which
951 * starts at the last comma (endt).
952 */
953
954 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200955 struct sample_conv_expr *conv_expr;
Willy Tarreau46dfd782019-12-17 10:25:29 +0100956 int err_arg;
957 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +0100958
Willy Tarreau833cc792013-07-24 15:34:19 +0200959 if (*endt && *endt != ',') {
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100960 if (endptr) {
961 /* end found, let's stop here */
962 break;
963 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200964 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100965 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200966 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100967 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100968 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200969 }
Emeric Brun107ca302010-01-04 16:16:05 +0100970
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100971 /* FIXME: how long should we support such idiocies ? Maybe we
972 * should already warn ?
973 */
Willy Tarreau833cc792013-07-24 15:34:19 +0200974 while (*endt == ',') /* then trailing commas */
975 endt++;
976
Willy Tarreau97108e02016-11-25 07:33:24 +0100977 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200978
979 if (!*begw) {
980 /* none ? skip to next string */
981 (*idx)++;
982 begw = str[*idx];
983 if (!begw || !*begw)
984 break;
985 }
986
Willy Tarreaued2c6622020-02-14 18:27:10 +0100987 for (endw = begw; is_idchar(*endw); endw++)
988 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200989
990 free(ckw);
991 ckw = my_strndup(begw, endw - begw);
992
993 conv = find_sample_conv(begw, endw - begw);
994 if (!conv) {
995 /* we found an isolated keyword that we don't know, it's not ours */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100996 if (begw == str[*idx]) {
997 endt = begw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200998 break;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100999 }
Willy Tarreau97108e02016-11-25 07:33:24 +01001000 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +02001001 goto out_error;
1002 }
Emeric Brun107ca302010-01-04 16:16:05 +01001003
Willy Tarreau833cc792013-07-24 15:34:19 +02001004 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001005 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001006 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001007 }
Emeric Brun107ca302010-01-04 16:16:05 +01001008
1009 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +02001010 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001011 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001012 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001013 }
Emeric Brun107ca302010-01-04 16:16:05 +01001014
1015 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +02001016 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +02001017 if (!conv_expr)
1018 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001019
1020 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
1021 conv_expr->conv = conv;
1022
Willy Tarreau46dfd782019-12-17 10:25:29 +01001023 al->kw = expr->fetch->kw;
1024 al->conv = conv_expr->conv->kw;
Willy Tarreau80b53ff2020-02-14 08:40:37 +01001025 argcnt = make_arg_list(endw, -1, conv->arg_mask, &conv_expr->arg_p, err_msg, &endt, &err_arg, al);
Willy Tarreau46dfd782019-12-17 10:25:29 +01001026 if (argcnt < 0) {
1027 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
1028 goto out_error;
1029 }
Willy Tarreau9e92d322010-01-26 17:58:06 +01001030
Willy Tarreau46dfd782019-12-17 10:25:29 +01001031 if (argcnt && !conv->arg_mask) {
1032 memprintf(err_msg, "converter '%s' does not support any args", ckw);
1033 goto out_error;
1034 }
Willy Tarreau21d68a62012-04-20 15:52:36 +02001035
Willy Tarreau46dfd782019-12-17 10:25:29 +01001036 if (!conv_expr->arg_p)
1037 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001038
Willy Tarreau46dfd782019-12-17 10:25:29 +01001039 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1040 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001041 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001042 }
1043 }
Emeric Brun485479d2010-09-23 18:02:19 +02001044
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001045 if (endptr) {
1046 /* end found, let's stop here */
1047 *endptr = (char *)endt;
1048 }
1049
Willy Tarreau833cc792013-07-24 15:34:19 +02001050 out:
1051 free(fkw);
1052 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001053 return expr;
1054
1055out_error:
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +01001056 release_sample_expr(expr);
Willy Tarreau833cc792013-07-24 15:34:19 +02001057 expr = NULL;
1058 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001059}
1060
1061/*
Willy Tarreau12785782012-04-27 21:37:17 +02001062 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001063 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001064 * Returns a pointer on a typed sample structure containing the result or NULL if
1065 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001066 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001067 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001068 *
1069 * Note: the fetch functions are required to properly set the return type. The
1070 * conversion functions must do so too. However the cast functions do not need
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001071 * to since they're made to cast multiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001072 *
1073 * The caller may indicate in <opt> if it considers the result final or not.
1074 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1075 * if the result is stable or not, according to the following table :
1076 *
1077 * return MAY_CHANGE FINAL Meaning for the sample
1078 * NULL 0 * Not present and will never be (eg: header)
1079 * NULL 1 0 Not present yet, could change (eg: POST param)
1080 * NULL 1 1 Not present yet, will not change anymore
1081 * smp 0 * Present and will not change (eg: header)
1082 * smp 1 0 Present, may change (eg: request length)
1083 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001084 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001085struct sample *sample_process(struct proxy *px, struct session *sess,
1086 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001087 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001088{
Willy Tarreau12785782012-04-27 21:37:17 +02001089 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001090
Willy Tarreau18387e22013-07-25 12:02:38 +02001091 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001092 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001093 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001094 }
Emeric Brun107ca302010-01-04 16:16:05 +01001095
Willy Tarreau1777ea62016-03-10 16:15:46 +01001096 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001097 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001098 return NULL;
1099
Emeric Brun107ca302010-01-04 16:16:05 +01001100 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001101 /* we want to ensure that p->type can be casted into
1102 * conv_expr->conv->in_type. We have 3 possibilities :
1103 * - NULL => not castable.
1104 * - c_none => nothing to do (let's optimize it)
1105 * - other => apply cast and prepare to fail
1106 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001107 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001108 return NULL;
1109
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001110 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1111 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001112 return NULL;
1113
Willy Tarreau12e50112012-04-25 17:21:49 +02001114 /* OK cast succeeded */
1115
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001116 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001117 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001118 }
1119 return p;
1120}
1121
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001122/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001123 * Resolve all remaining arguments in proxy <p>. Returns the number of
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001124 * errors or 0 if everything is fine. If at least one error is met, it will
1125 * be appended to *err. If *err==NULL it will be allocated first.
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001126 */
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001127int smp_resolve_args(struct proxy *p, char **err)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001128{
1129 struct arg_list *cur, *bak;
1130 const char *ctx, *where;
1131 const char *conv_ctx, *conv_pre, *conv_pos;
1132 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001133 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001134 struct arg *arg;
1135 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001136 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001137
1138 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1139 struct proxy *px;
1140 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001141 struct stktable *t;
1142 char *pname, *sname, *stktname;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001143 char *err2;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001144
1145 arg = cur->arg;
1146
1147 /* prepare output messages */
1148 conv_pre = conv_pos = conv_ctx = "";
1149 if (cur->conv) {
1150 conv_ctx = cur->conv;
1151 conv_pre = "conversion keyword '";
1152 conv_pos = "' for ";
1153 }
1154
1155 where = "in";
1156 ctx = "sample fetch keyword";
1157 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001158 case ARGC_STK: where = "in stick rule in"; break;
1159 case ARGC_TRK: where = "in tracking rule in"; break;
1160 case ARGC_LOG: where = "in log-format string in"; break;
1161 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1162 case ARGC_HRQ: where = "in http-request header format string in"; break;
1163 case ARGC_HRS: where = "in http-response header format string in"; break;
1164 case ARGC_UIF: where = "in unique-id-format string in"; break;
1165 case ARGC_RDR: where = "in redirect format string in"; break;
1166 case ARGC_CAP: where = "in capture rule in"; break;
1167 case ARGC_ACL: ctx = "ACL keyword"; break;
1168 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001169 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001170 case ARGC_HERR: where = "in http-error directive in"; break;
Miroslav Zagorac7f8314c2020-12-09 16:31:48 +01001171 case ARGC_OT: where = "in ot-scope directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001172 }
1173
1174 /* set a few default settings */
1175 px = p;
1176 pname = p->id;
1177
1178 switch (arg->type) {
1179 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001180 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001181 memprintf(err, "%sparsing [%s:%d]: missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1182 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001183 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001184 cfgerr++;
1185 continue;
1186 }
1187
1188 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001189 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001190
1191 if (sname) {
1192 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001193 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001194
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001195 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001196 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001197 memprintf(err, "%sparsing [%s:%d]: unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1198 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001199 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001200 cfgerr++;
1201 break;
1202 }
1203 }
1204 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001205 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001206
1207 srv = findserver(px, sname);
1208 if (!srv) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001209 memprintf(err, "%sparsing [%s:%d]: unable to find server '%s' in proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1210 *err ? *err : "", cur->file, cur->line, sname, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001211 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001212 cfgerr++;
1213 break;
1214 }
1215
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001216 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001217 arg->unresolved = 0;
1218 arg->data.srv = srv;
1219 break;
1220
1221 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001222 if (arg->data.str.data) {
1223 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001224 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001225 }
1226
1227 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001228 memprintf(err, "%sparsing [%s:%d]: unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1229 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001230 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001231 cfgerr++;
1232 break;
1233 }
1234
1235 if (!(px->cap & PR_CAP_FE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001236 memprintf(err, "%sparsing [%s:%d]: proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1237 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001238 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001239 cfgerr++;
1240 break;
1241 }
1242
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001243 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001244 arg->unresolved = 0;
1245 arg->data.prx = px;
1246 break;
1247
1248 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001249 if (arg->data.str.data) {
1250 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001251 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001252 }
1253
1254 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001255 memprintf(err, "%sparsing [%s:%d]: unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1256 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001257 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001258 cfgerr++;
1259 break;
1260 }
1261
1262 if (!(px->cap & PR_CAP_BE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001263 memprintf(err, "%sparsing [%s:%d]: proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1264 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001265 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001266 cfgerr++;
1267 break;
1268 }
1269
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001270 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001271 arg->unresolved = 0;
1272 arg->data.prx = px;
1273 break;
1274
1275 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001276 if (arg->data.str.data)
1277 stktname = arg->data.str.area;
1278 else
1279 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001280
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001281 t = stktable_find_by_name(stktname);
1282 if (!t) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001283 memprintf(err, "%sparsing [%s:%d]: unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1284 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001285 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001286 cfgerr++;
1287 break;
1288 }
1289
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001290 if (!t->size) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001291 memprintf(err, "%sparsing [%s:%d]: no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1292 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001293 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001294 cfgerr++;
1295 break;
1296 }
1297
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001298 if (t->proxy && (p->bind_proc & ~t->proxy->bind_proc)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001299 memprintf(err, "%sparsing [%s:%d]: stick-table '%s' not present on all processes covered by proxy '%s'.\n",
1300 *err ? *err : "", cur->file, cur->line, t->proxy->id, p->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01001301 cfgerr++;
1302 break;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001303 }
1304
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001305 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001306 p->next_stkt_ref = t->proxies_list;
1307 t->proxies_list = p;
1308 }
1309
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001310 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001311 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001312 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001313 break;
1314
1315 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001316 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001317 memprintf(err, "%sparsing [%s:%d]: missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1318 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001319 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001320 cfgerr++;
1321 break;
1322 }
1323
1324 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001325 strcmp(p->uri_auth->userlist->name, arg->data.str.area) == 0)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001326 ul = p->uri_auth->userlist;
1327 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001328 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001329
1330 if (!ul) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001331 memprintf(err, "%sparsing [%s:%d]: unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1332 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001333 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001334 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001335 cfgerr++;
1336 break;
1337 }
1338
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001339 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001340 arg->unresolved = 0;
1341 arg->data.usr = ul;
1342 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001343
1344 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001345 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001346 memprintf(err, "%sparsing [%s:%d]: missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1347 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001348 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001349 cfgerr++;
1350 continue;
1351 }
1352
Willy Tarreau46947782015-01-19 19:00:58 +01001353 rflags = 0;
1354 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001355 err2 = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001356
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001357 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err2))) {
1358 memprintf(err, "%sparsing [%s:%d]: error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1359 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001360 arg->data.str.area,
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001361 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err2);
Willy Tarreau46947782015-01-19 19:00:58 +01001362 cfgerr++;
1363 continue;
1364 }
1365
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001366 chunk_destroy(&arg->data.str);
Willy Tarreau46947782015-01-19 19:00:58 +01001367 arg->unresolved = 0;
1368 arg->data.reg = reg;
1369 break;
1370
1371
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001372 }
1373
1374 LIST_DEL(&cur->list);
1375 free(cur);
1376 } /* end of args processing */
1377
1378 return cfgerr;
1379}
1380
1381/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001382 * Process a fetch + format conversion as defined by the sample expression
1383 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001384 * not explicitly set to <smp_type>, but shall be compatible with it as
1385 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1386 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001387 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1388 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1389 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1390 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001391 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1392 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1393 * still hope to get it after waiting longer, and is not converted to string.
1394 * The possible output combinations are the following :
1395 *
1396 * return MAY_CHANGE FINAL Meaning for the sample
1397 * NULL * * Not present and will never be (eg: header)
1398 * smp 0 * Final value converted (eg: header)
1399 * smp 1 0 Not present yet, may appear later (eg: header)
1400 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001401 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001402struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001403 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001404 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001405{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001406 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001407
Willy Tarreau6c616e02014-06-25 16:56:41 +02001408 memset(smp, 0, sizeof(*smp));
1409
Willy Tarreau192252e2015-04-04 01:47:55 +02001410 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001411 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1412 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001413 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001414 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001415
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001416 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001417 return NULL;
1418
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001419 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001420 return NULL;
1421
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001422 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001423 return smp;
1424}
1425
Christopher Faulet476e5d02016-10-26 11:34:47 +02001426static void release_sample_arg(struct arg *p)
1427{
1428 struct arg *p_back = p;
1429
1430 if (!p)
1431 return;
1432
1433 while (p->type != ARGT_STOP) {
1434 if (p->type == ARGT_STR || p->unresolved) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001435 chunk_destroy(&p->data.str);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001436 p->unresolved = 0;
1437 }
1438 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001439 regex_free(p->data.reg);
1440 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001441 }
1442 p++;
1443 }
1444
1445 if (p_back != empty_arg_list)
1446 free(p_back);
1447}
1448
1449void release_sample_expr(struct sample_expr *expr)
1450{
1451 struct sample_conv_expr *conv_expr, *conv_exprb;
1452
1453 if (!expr)
1454 return;
1455
Tim Duesterhus867cd982020-07-04 11:49:39 +02001456 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list) {
1457 LIST_DEL(&conv_expr->list);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001458 release_sample_arg(conv_expr->arg_p);
Tim Duesterhus867cd982020-07-04 11:49:39 +02001459 free(conv_expr);
1460 }
1461
Christopher Faulet476e5d02016-10-26 11:34:47 +02001462 release_sample_arg(expr->arg_p);
1463 free(expr);
1464}
1465
Emeric Brun107ca302010-01-04 16:16:05 +01001466/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001467/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001468/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001469/*****************************************************************/
1470
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001471static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1472{
1473 int i;
1474 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001475 struct buffer *buf;
1476 struct sink *sink;
1477 struct ist line;
1478 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001479
Willy Tarreau0851fd52019-12-17 10:07:25 +01001480 buf = alloc_trash_chunk();
1481 if (!buf)
1482 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001483
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001484 sink = (struct sink *)arg_p[1].data.ptr;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001485 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001486
Willy Tarreau0851fd52019-12-17 10:07:25 +01001487 pfx = arg_p[0].data.str.area;
1488 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001489
Willy Tarreau0851fd52019-12-17 10:07:25 +01001490 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1491 if (!sample_casts[smp->data.type][SMP_T_STR])
1492 goto nocast;
1493
1494 /* Copy sample fetch. This puts the sample as const, the
1495 * cast will copy data if a transformation is required.
1496 */
1497 memcpy(&tmp, smp, sizeof(struct sample));
1498 tmp.flags = SMP_F_CONST;
1499
1500 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1501 goto nocast;
1502
1503 /* Display the displayable chars*. */
1504 b_putchr(buf, '<');
1505 for (i = 0; i < tmp.data.u.str.data; i++) {
Willy Tarreau90807112020-02-25 08:16:33 +01001506 if (isprint((unsigned char)tmp.data.u.str.area[i]))
Willy Tarreau0851fd52019-12-17 10:07:25 +01001507 b_putchr(buf, tmp.data.u.str.area[i]);
1508 else
1509 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001510 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001511 b_putchr(buf, '>');
1512
1513 done:
1514 line = ist2(buf->area, buf->data);
Emeric Brun54648852020-07-06 15:54:06 +02001515 sink_write(sink, &line, 1, 0, 0, NULL);
Willy Tarreau0851fd52019-12-17 10:07:25 +01001516 end:
1517 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001518 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001519 nocast:
1520 chunk_appendf(buf, "(undisplayable)");
1521 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001522}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001523
1524// This function checks the "debug" converter's arguments.
1525static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1526 const char *file, int line, char **err)
1527{
1528 const char *name = "buf0";
1529 struct sink *sink = NULL;
1530
1531 if (args[0].type != ARGT_STR) {
1532 /* optional prefix */
1533 args[0].data.str.area = "";
1534 args[0].data.str.data = 0;
1535 }
1536
1537 if (args[1].type == ARGT_STR)
1538 name = args[1].data.str.area;
1539
1540 sink = sink_find(name);
1541 if (!sink) {
1542 memprintf(err, "No such sink '%s'", name);
1543 return 0;
1544 }
1545
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001546 chunk_destroy(&args[1].data.str);
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001547 args[1].type = ARGT_PTR;
1548 args[1].data.ptr = sink;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001549 return 1;
1550}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001551
Holger Just1bfc24b2017-05-06 00:56:53 +02001552static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1553{
Willy Tarreau83061a82018-07-13 11:56:34 +02001554 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001555 int bin_len;
1556
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001557 trash->data = 0;
1558 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1559 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001560 if (bin_len < 0)
1561 return 0;
1562
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001563 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001564 smp->data.u.str = *trash;
1565 smp->data.type = SMP_T_BIN;
1566 smp->flags &= ~SMP_F_CONST;
1567 return 1;
1568}
1569
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001570static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001571{
Willy Tarreau83061a82018-07-13 11:56:34 +02001572 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001573 int b64_len;
1574
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001575 trash->data = 0;
1576 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1577 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001578 if (b64_len < 0)
1579 return 0;
1580
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001581 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001582 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001583 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001584 smp->flags &= ~SMP_F_CONST;
1585 return 1;
1586}
1587
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001588static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1589{
1590 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001591 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001592
1593 memset(&ctx, 0, sizeof(ctx));
1594
1595 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001596 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1597 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001598
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001599 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001600 smp->data.u.str = *trash;
1601 smp->data.type = SMP_T_BIN;
1602 smp->flags &= ~SMP_F_CONST;
1603 return 1;
1604}
1605
Tim Duesterhusd4376302019-06-17 12:41:44 +02001606#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01001607static int smp_check_sha2(struct arg *args, struct sample_conv *conv,
1608 const char *file, int line, char **err)
1609{
1610 if (args[0].type == ARGT_STOP)
1611 return 1;
1612 if (args[0].type != ARGT_SINT) {
1613 memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]);
1614 return 0;
1615 }
1616
1617 switch (args[0].data.sint) {
1618 case 224:
1619 case 256:
1620 case 384:
1621 case 512:
1622 /* this is okay */
1623 return 1;
1624 default:
1625 memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint);
1626 return 0;
1627 }
1628}
1629
Tim Duesterhusd4376302019-06-17 12:41:44 +02001630static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
1631{
1632 struct buffer *trash = get_trash_chunk();
1633 int bits = 256;
Christopher Faulete6e7a582021-01-29 11:29:28 +01001634 if (arg_p->data.sint)
Tim Duesterhusd4376302019-06-17 12:41:44 +02001635 bits = arg_p->data.sint;
1636
1637 switch (bits) {
1638 case 224: {
1639 SHA256_CTX ctx;
1640
1641 memset(&ctx, 0, sizeof(ctx));
1642
1643 SHA224_Init(&ctx);
1644 SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1645 SHA224_Final((unsigned char *) trash->area, &ctx);
1646 trash->data = SHA224_DIGEST_LENGTH;
1647 break;
1648 }
1649 case 256: {
1650 SHA256_CTX ctx;
1651
1652 memset(&ctx, 0, sizeof(ctx));
1653
1654 SHA256_Init(&ctx);
1655 SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1656 SHA256_Final((unsigned char *) trash->area, &ctx);
1657 trash->data = SHA256_DIGEST_LENGTH;
1658 break;
1659 }
1660 case 384: {
1661 SHA512_CTX ctx;
1662
1663 memset(&ctx, 0, sizeof(ctx));
1664
1665 SHA384_Init(&ctx);
1666 SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1667 SHA384_Final((unsigned char *) trash->area, &ctx);
1668 trash->data = SHA384_DIGEST_LENGTH;
1669 break;
1670 }
1671 case 512: {
1672 SHA512_CTX ctx;
1673
1674 memset(&ctx, 0, sizeof(ctx));
1675
1676 SHA512_Init(&ctx);
1677 SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1678 SHA512_Final((unsigned char *) trash->area, &ctx);
1679 trash->data = SHA512_DIGEST_LENGTH;
1680 break;
1681 }
1682 default:
1683 return 0;
1684 }
1685
1686 smp->data.u.str = *trash;
1687 smp->data.type = SMP_T_BIN;
1688 smp->flags &= ~SMP_F_CONST;
1689 return 1;
1690}
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001691
Dragan Dosen9e8db132021-02-22 10:03:53 +01001692/* This function returns a sample struct filled with an <arg> content.
1693 * If the <arg> contains a string, it is returned in the sample flagged as
1694 * SMP_F_CONST. If the <arg> contains a variable descriptor, the sample is
1695 * filled with the content of the variable by using vars_get_by_desc().
1696 *
1697 * Keep in mind that the sample content may be written to a pre-allocated
1698 * trash chunk as returned by get_trash_chunk().
1699 *
1700 * This function returns 0 if an error occurs, otherwise it returns 1.
1701 */
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001702static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
1703{
1704 switch (arg->type) {
1705 case ARGT_STR:
1706 smp->data.type = SMP_T_STR;
1707 smp->data.u.str = arg->data.str;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001708 smp->flags = SMP_F_CONST;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001709 return 1;
1710 case ARGT_VAR:
1711 if (!vars_get_by_desc(&arg->data.var, smp))
1712 return 0;
1713 if (!sample_casts[smp->data.type][SMP_T_STR])
1714 return 0;
1715 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
1716 return 0;
1717 return 1;
1718 default:
1719 return 0;
1720 }
1721}
1722
Dragan Dosen9e8db132021-02-22 10:03:53 +01001723/* This function checks an <arg> and fills it with a variable type if the
1724 * <arg> string contains a valid variable name. If failed, the function
1725 * tries to perform a base64 decode operation on the same string, and
1726 * fills the <arg> with the decoded content.
1727 *
1728 * Validation is skipped if the <arg> string is empty.
1729 *
1730 * This function returns 0 if the variable lookup fails and the specified
1731 * <arg> string is not a valid base64 encoded string, as well if
1732 * unexpected argument type is specified or memory allocation error
1733 * occurs. Otherwise it returns 1.
1734 */
1735static inline int sample_check_arg_base64(struct arg *arg, char **err)
1736{
1737 char *dec = NULL;
1738 int dec_size;
1739
1740 if (arg->type != ARGT_STR) {
1741 memprintf(err, "unexpected argument type");
1742 return 0;
1743 }
1744
1745 if (arg->data.str.data == 0) /* empty */
1746 return 1;
1747
1748 if (vars_check_arg(arg, NULL))
1749 return 1;
1750
1751 if (arg->data.str.data % 4) {
1752 memprintf(err, "argument needs to be base64 encoded, and "
1753 "can either be a string or a variable");
1754 return 0;
1755 }
1756
1757 dec_size = (arg->data.str.data / 4 * 3)
1758 - (arg->data.str.area[arg->data.str.data-1] == '=' ? 1 : 0)
1759 - (arg->data.str.area[arg->data.str.data-2] == '=' ? 1 : 0);
1760
1761 if ((dec = malloc(dec_size)) == NULL) {
1762 memprintf(err, "memory allocation error");
1763 return 0;
1764 }
1765
1766 dec_size = base64dec(arg->data.str.area, arg->data.str.data, dec, dec_size);
1767 if (dec_size < 0) {
1768 memprintf(err, "argument needs to be base64 encoded, and "
1769 "can either be a string or a variable");
1770 free(dec);
1771 return 0;
1772 }
1773
1774 /* base64 decoded */
1775 chunk_destroy(&arg->data.str);
1776 arg->data.str.area = dec;
1777 arg->data.str.data = dec_size;
1778 return 1;
1779}
1780
Ilya Shipitsin2c481d02021-03-26 23:35:31 +05001781#ifdef EVP_CIPH_GCM_MODE
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001782static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
1783 const char *file, int line, char **err)
1784{
1785 switch(args[0].data.sint) {
1786 case 128:
1787 case 192:
1788 case 256:
1789 break;
1790 default:
1791 memprintf(err, "key size must be 128, 192 or 256 (bits).");
1792 return 0;
1793 }
Dragan Dosen9e8db132021-02-22 10:03:53 +01001794
1795 /* Try to decode variables. */
1796 if (!sample_check_arg_base64(&args[1], err)) {
1797 memprintf(err, "failed to parse nonce : %s", *err);
1798 return 0;
1799 }
1800 if (!sample_check_arg_base64(&args[2], err)) {
1801 memprintf(err, "failed to parse key : %s", *err);
1802 return 0;
1803 }
1804 if (!sample_check_arg_base64(&args[3], err)) {
1805 memprintf(err, "failed to parse aead_tag : %s", *err);
1806 return 0;
1807 }
1808
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001809 return 1;
1810}
1811
1812/* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
1813static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
1814{
1815 struct sample nonce, key, aead_tag;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001816 struct buffer *smp_trash = NULL, *smp_trash_alloc = NULL;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001817 EVP_CIPHER_CTX *ctx;
1818 int dec_size, ret;
1819
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001820 smp_trash_alloc = alloc_trash_chunk();
1821 if (!smp_trash_alloc)
1822 return 0;
1823
Dragan Dosen9e8db132021-02-22 10:03:53 +01001824 /* smp copy */
1825 smp_trash_alloc->data = smp->data.u.str.data;
1826 if (unlikely(smp_trash_alloc->data > smp_trash_alloc->size))
1827 smp_trash_alloc->data = smp_trash_alloc->size;
1828 memcpy(smp_trash_alloc->area, smp->data.u.str.area, smp_trash_alloc->data);
1829
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001830 ctx = EVP_CIPHER_CTX_new();
1831
1832 if (!ctx)
1833 goto err;
1834
Dragan Dosen9e8db132021-02-22 10:03:53 +01001835 smp_trash = alloc_trash_chunk();
1836 if (!smp_trash)
1837 goto err;
1838
1839 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
1840 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001841 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001842
1843 if (arg_p[1].type == ARGT_VAR) {
1844 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
1845 if (dec_size < 0)
1846 goto err;
1847 smp_trash->data = dec_size;
1848 nonce.data.u.str = *smp_trash;
1849 }
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001850
1851 /* Set cipher type and mode */
1852 switch(arg_p[0].data.sint) {
1853 case 128:
1854 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
1855 break;
1856 case 192:
1857 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
1858 break;
1859 case 256:
1860 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
1861 break;
1862 }
1863
Dragan Dosen9e8db132021-02-22 10:03:53 +01001864 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, nonce.data.u.str.data, NULL);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001865
1866 /* Initialise IV */
Dragan Dosen9e8db132021-02-22 10:03:53 +01001867 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) nonce.data.u.str.area))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001868 goto err;
1869
Dragan Dosen9e8db132021-02-22 10:03:53 +01001870 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
1871 if (!sample_conv_var2smp_str(&arg_p[2], &key))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001872 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001873
1874 if (arg_p[2].type == ARGT_VAR) {
1875 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
1876 if (dec_size < 0)
1877 goto err;
1878 smp_trash->data = dec_size;
1879 key.data.u.str = *smp_trash;
1880 }
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001881
1882 /* Initialise key */
Dragan Dosen9e8db132021-02-22 10:03:53 +01001883 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) key.data.u.str.area, NULL))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001884 goto err;
1885
1886 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
Dragan Dosen9e8db132021-02-22 10:03:53 +01001887 (unsigned char *) smp_trash_alloc->area, (int) smp_trash_alloc->data))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001888 goto err;
1889
Dragan Dosen9e8db132021-02-22 10:03:53 +01001890 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
1891 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001892 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001893
1894 if (arg_p[3].type == ARGT_VAR) {
1895 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
1896 if (dec_size < 0)
1897 goto err;
1898 smp_trash_alloc->data = dec_size;
1899 aead_tag.data.u.str = *smp_trash_alloc;
1900 }
1901
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001902 dec_size = smp_trash->data;
1903
Dragan Dosen9e8db132021-02-22 10:03:53 +01001904 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, aead_tag.data.u.str.data, (void *) aead_tag.data.u.str.area);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001905 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
1906
1907 if (ret <= 0)
1908 goto err;
1909
1910 smp->data.u.str.data = dec_size + smp_trash->data;
1911 smp->data.u.str.area = smp_trash->area;
1912 smp->data.type = SMP_T_BIN;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001913 smp_dup(smp);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001914 free_trash_chunk(smp_trash_alloc);
Dragan Dosen9e8db132021-02-22 10:03:53 +01001915 free_trash_chunk(smp_trash);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001916 return 1;
1917
1918err:
1919 free_trash_chunk(smp_trash_alloc);
Dragan Dosen9e8db132021-02-22 10:03:53 +01001920 free_trash_chunk(smp_trash);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001921 return 0;
1922}
Ilya Shipitsin2c481d02021-03-26 23:35:31 +05001923#endif
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001924
Patrick Gansterer8e366512020-04-22 16:47:57 +02001925static int check_crypto_digest(struct arg *args, struct sample_conv *conv,
1926 const char *file, int line, char **err)
1927{
1928 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1929
1930 if (evp)
1931 return 1;
1932
1933 memprintf(err, "algorithm must be a valid OpenSSL message digest name.");
1934 return 0;
1935}
1936
1937static int sample_conv_crypto_digest(const struct arg *args, struct sample *smp, void *private)
1938{
1939 struct buffer *trash = get_trash_chunk();
1940 unsigned char *md = (unsigned char*) trash->area;
1941 unsigned int md_len = trash->size;
1942 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
1943 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1944
1945 if (!ctx)
1946 return 0;
1947
1948 if (!EVP_DigestInit_ex(ctx, evp, NULL) ||
1949 !EVP_DigestUpdate(ctx, smp->data.u.str.area, smp->data.u.str.data) ||
1950 !EVP_DigestFinal_ex(ctx, md, &md_len)) {
1951 EVP_MD_CTX_free(ctx);
1952 return 0;
1953 }
1954
1955 EVP_MD_CTX_free(ctx);
1956
1957 trash->data = md_len;
1958 smp->data.u.str = *trash;
1959 smp->data.type = SMP_T_BIN;
1960 smp->flags &= ~SMP_F_CONST;
1961 return 1;
1962}
1963
1964static int check_crypto_hmac(struct arg *args, struct sample_conv *conv,
1965 const char *file, int line, char **err)
1966{
1967 if (!check_crypto_digest(args, conv, file, line, err))
1968 return 0;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001969
1970 if (!sample_check_arg_base64(&args[1], err)) {
1971 memprintf(err, "failed to parse key : %s", *err);
1972 return 0;
1973 }
Patrick Gansterer8e366512020-04-22 16:47:57 +02001974
Patrick Gansterer8e366512020-04-22 16:47:57 +02001975 return 1;
1976}
1977
1978static int sample_conv_crypto_hmac(const struct arg *args, struct sample *smp, void *private)
1979{
1980 struct sample key;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001981 struct buffer *trash = NULL, *key_trash = NULL;
Patrick Gansterer8e366512020-04-22 16:47:57 +02001982 unsigned char *md;
1983 unsigned int md_len;
1984 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1985 int dec_size;
1986
1987 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
1988 if (!sample_conv_var2smp_str(&args[1], &key))
1989 return 0;
1990
Dragan Dosen9e8db132021-02-22 10:03:53 +01001991 if (args[1].type == ARGT_VAR) {
1992 key_trash = alloc_trash_chunk();
1993 if (!key_trash)
1994 goto err;
1995
1996 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, key_trash->area, key_trash->size);
1997 if (dec_size < 0)
1998 goto err;
1999 key_trash->data = dec_size;
2000 key.data.u.str = *key_trash;
2001 }
Patrick Gansterer8e366512020-04-22 16:47:57 +02002002
Dragan Dosen9e8db132021-02-22 10:03:53 +01002003 trash = alloc_trash_chunk();
2004 if (!trash)
Patrick Gansterer8e366512020-04-22 16:47:57 +02002005 goto err;
2006
2007 md = (unsigned char*) trash->area;
2008 md_len = trash->size;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002009 if (!HMAC(evp, key.data.u.str.area, key.data.u.str.data, (const unsigned char*) smp->data.u.str.area,
2010 smp->data.u.str.data, md, &md_len))
Patrick Gansterer8e366512020-04-22 16:47:57 +02002011 goto err;
2012
2013 free_trash_chunk(key_trash);
2014
2015 trash->data = md_len;
2016 smp->data.u.str = *trash;
2017 smp->data.type = SMP_T_BIN;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002018 smp_dup(smp);
2019 free_trash_chunk(trash);
Patrick Gansterer8e366512020-04-22 16:47:57 +02002020 return 1;
2021
2022err:
2023 free_trash_chunk(key_trash);
Dragan Dosen9e8db132021-02-22 10:03:53 +01002024 free_trash_chunk(trash);
Patrick Gansterer8e366512020-04-22 16:47:57 +02002025 return 0;
2026}
2027
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02002028#endif /* USE_OPENSSL */
Tim Duesterhusd4376302019-06-17 12:41:44 +02002029
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002030static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002031{
Willy Tarreau83061a82018-07-13 11:56:34 +02002032 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002033 unsigned char c;
2034 int ptr = 0;
2035
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002036 trash->data = 0;
2037 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
2038 c = smp->data.u.str.area[ptr++];
2039 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
2040 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002041 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002042 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002043 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002044 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002045 return 1;
2046}
2047
Dragan Dosen3f957b22017-10-24 09:27:34 +02002048static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
2049{
2050 long long int n = 0;
2051 int i, c;
2052
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002053 for (i = 0; i < smp->data.u.str.data; i++) {
2054 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02002055 return 0;
2056 n = (n << 4) + c;
2057 }
2058
2059 smp->data.u.sint = n;
2060 smp->data.type = SMP_T_SINT;
2061 smp->flags &= ~SMP_F_CONST;
2062 return 1;
2063}
2064
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002065/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002066static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002067{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002068 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
2069 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002070 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002071 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002072 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002073 return 1;
2074}
2075
Willy Tarreau60a2ee72017-12-15 07:13:48 +01002076static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002077{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002078 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002079 smp->data.u.sint = i;
2080 smp->data.type = SMP_T_SINT;
2081 return 1;
2082}
2083
2084
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002085static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002086{
2087 int i;
2088
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002089 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002090 return 0;
2091
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002092 for (i = 0; i < smp->data.u.str.data; i++) {
2093 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
2094 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01002095 }
2096 return 1;
2097}
2098
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002099static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002100{
2101 int i;
2102
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002103 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02002104 return 0;
2105
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002106 for (i = 0; i < smp->data.u.str.data; i++) {
2107 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
2108 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01002109 }
2110 return 1;
2111}
2112
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002113/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
2114static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002115{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002116 /* Attempt to convert to IPv4 to apply the correct mask. */
2117 c_ipv62ip(smp);
2118
2119 if (smp->data.type == SMP_T_IPV4) {
2120 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
2121 smp->data.type = SMP_T_IPV4;
2122 }
2123 else if (smp->data.type == SMP_T_IPV6) {
2124 /* IPv6 cannot be converted without an IPv6 mask. */
2125 if (args[1].type != ARGT_IPV6)
2126 return 0;
2127
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +01002128 write_u64(&smp->data.u.ipv6.s6_addr[0],
2129 read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0]));
2130 write_u64(&smp->data.u.ipv6.s6_addr[8],
2131 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 +01002132 smp->data.type = SMP_T_IPV6;
2133 }
2134
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002135 return 1;
2136}
2137
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002138/* takes an UINT value on input supposed to represent the time since EPOCH,
2139 * adds an optional offset found in args[1] and emits a string representing
2140 * the local time in the format specified in args[1] using strftime().
2141 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002142static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002143{
Willy Tarreau83061a82018-07-13 11:56:34 +02002144 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002145 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002146 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002147 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002148
2149 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002150 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002151 curr_date += args[1].data.sint;
2152
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002153 tm = localtime(&curr_date);
2154 if (!tm)
2155 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002156 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002157 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
2158 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002159 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002160 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002161 return 1;
2162}
2163
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002164/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002165static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002166{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002167 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
2168 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002169 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002170 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002171 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002172 return 1;
2173}
2174
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002175/* takes an UINT value on input supposed to represent the time since EPOCH,
2176 * adds an optional offset found in args[1] and emits a string representing
2177 * the UTC date in the format specified in args[1] using strftime().
2178 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002179static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002180{
Willy Tarreau83061a82018-07-13 11:56:34 +02002181 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002182 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002183 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002184 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002185
2186 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002187 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002188 curr_date += args[1].data.sint;
2189
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002190 tm = gmtime(&curr_date);
2191 if (!tm)
2192 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002193 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002194 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
2195 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002196 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002197 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002198 return 1;
2199}
2200
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002201/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002202static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002203{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002204 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
2205 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002206 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002207 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002208 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002209 return 1;
2210}
2211
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002212/* hashes the binary input into a 32-bit unsigned int using xxh.
2213 * The seed of the hash defaults to 0 but can be changd in argument 1.
2214 */
2215static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
2216{
2217 unsigned int seed;
2218
Christopher Faulete6e7a582021-01-29 11:29:28 +01002219 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002220 seed = arg_p->data.sint;
2221 else
2222 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002223 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
2224 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002225 smp->data.type = SMP_T_SINT;
2226 return 1;
2227}
2228
2229/* hashes the binary input into a 64-bit unsigned int using xxh.
2230 * In fact, the function returns a 64 bit unsigned, but the sample
2231 * storage of haproxy only proposes 64-bits signed, so the value is
2232 * cast as signed. This cast doesn't impact the hash repartition.
2233 * The seed of the hash defaults to 0 but can be changd in argument 1.
2234 */
2235static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
2236{
2237 unsigned long long int seed;
2238
Christopher Faulete6e7a582021-01-29 11:29:28 +01002239 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002240 seed = (unsigned long long int)arg_p->data.sint;
2241 else
2242 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002243 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
2244 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002245 smp->data.type = SMP_T_SINT;
2246 return 1;
2247}
2248
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002249static int sample_conv_xxh3(const struct arg *arg_p, struct sample *smp, void *private)
2250{
2251 unsigned long long int seed;
2252
Christopher Faulete6e7a582021-01-29 11:29:28 +01002253 if (arg_p->data.sint)
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002254 seed = (unsigned long long int)arg_p->data.sint;
2255 else
2256 seed = 0;
2257 smp->data.u.sint = (long long int)XXH3(smp->data.u.str.area,
2258 smp->data.u.str.data, seed);
2259 smp->data.type = SMP_T_SINT;
2260 return 1;
2261}
2262
Willy Tarreau80599772015-01-20 19:35:24 +01002263/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002264static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01002265{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002266 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
2267 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002268 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002269 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002270 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01002271 return 1;
2272}
2273
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002274/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
2275static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
2276{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002277 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
2278 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002279 if (arg_p->data.sint)
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002280 smp->data.u.sint = full_hash(smp->data.u.sint);
2281 smp->data.type = SMP_T_SINT;
2282 return 1;
2283}
2284
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002285/* This function escape special json characters. The returned string can be
2286 * safely set between two '"' and used as json string. The json string is
2287 * defined like this:
2288 *
2289 * any Unicode character except '"' or '\' or control character
2290 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
2291 *
2292 * The enum input_type contain all the allowed mode for decoding the input
2293 * string.
2294 */
2295enum input_type {
2296 IT_ASCII = 0,
2297 IT_UTF8,
2298 IT_UTF8S,
2299 IT_UTF8P,
2300 IT_UTF8PS,
2301};
William Dauchy5417e892021-01-08 21:57:41 +01002302
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002303static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
2304 const char *file, int line, char **err)
2305{
Christopher Faulet95917132020-08-05 23:07:07 +02002306 enum input_type type;
2307
Christopher Faulet95917132020-08-05 23:07:07 +02002308 if (strcmp(arg->data.str.area, "") == 0)
2309 type = IT_ASCII;
2310 else if (strcmp(arg->data.str.area, "ascii") == 0)
2311 type = IT_ASCII;
2312 else if (strcmp(arg->data.str.area, "utf8") == 0)
2313 type = IT_UTF8;
2314 else if (strcmp(arg->data.str.area, "utf8s") == 0)
2315 type = IT_UTF8S;
2316 else if (strcmp(arg->data.str.area, "utf8p") == 0)
2317 type = IT_UTF8P;
2318 else if (strcmp(arg->data.str.area, "utf8ps") == 0)
2319 type = IT_UTF8PS;
2320 else {
2321 memprintf(err, "Unexpected input code type. "
2322 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
2323 return 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002324 }
2325
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002326 chunk_destroy(&arg->data.str);
Christopher Faulet95917132020-08-05 23:07:07 +02002327 arg->type = ARGT_SINT;
2328 arg->data.sint = type;
2329 return 1;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002330}
2331
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002332static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002333{
Willy Tarreau83061a82018-07-13 11:56:34 +02002334 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002335 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
2336 const char *str;
2337 int len;
2338 enum input_type input_type = IT_ASCII;
2339 unsigned int c;
2340 unsigned int ret;
2341 char *p;
2342
Christopher Faulete6e7a582021-01-29 11:29:28 +01002343 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002344
2345 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002346 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002347
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002348 p = smp->data.u.str.area;
2349 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002350
2351 if (input_type == IT_ASCII) {
2352 /* Read input as ASCII. */
2353 c = *(unsigned char *)p;
2354 p++;
2355 }
2356 else {
2357 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002358 ret = utf8_next(p,
2359 smp->data.u.str.data - ( p - smp->data.u.str.area),
2360 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002361 p += utf8_return_length(ret);
2362
2363 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2364 return 0;
2365 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2366 continue;
2367 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2368 return 0;
2369 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2370 continue;
2371
2372 /* Check too big values. */
2373 if ((unsigned int)c > 0xffff) {
2374 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2375 return 0;
2376 continue;
2377 }
2378 }
2379
2380 /* Convert character. */
2381 if (c == '"') {
2382 len = 2;
2383 str = "\\\"";
2384 }
2385 else if (c == '\\') {
2386 len = 2;
2387 str = "\\\\";
2388 }
2389 else if (c == '/') {
2390 len = 2;
2391 str = "\\/";
2392 }
2393 else if (c == '\b') {
2394 len = 2;
2395 str = "\\b";
2396 }
2397 else if (c == '\f') {
2398 len = 2;
2399 str = "\\f";
2400 }
2401 else if (c == '\r') {
2402 len = 2;
2403 str = "\\r";
2404 }
2405 else if (c == '\n') {
2406 len = 2;
2407 str = "\\n";
2408 }
2409 else if (c == '\t') {
2410 len = 2;
2411 str = "\\t";
2412 }
Willy Tarreau90807112020-02-25 08:16:33 +01002413 else if (c > 0xff || !isprint((unsigned char)c)) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002414 /* isprint generate a segfault if c is too big. The man says that
2415 * c must have the value of an unsigned char or EOF.
2416 */
2417 len = 6;
2418 _str[0] = '\\';
2419 _str[1] = 'u';
2420 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2421 str = _str;
2422 }
2423 else {
2424 len = 1;
Willy Tarreau5715da22020-02-25 08:37:37 +01002425 _str[0] = c;
2426 str = _str;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002427 }
2428
2429 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002430 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002431 return 0;
2432
2433 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002434 memcpy(temp->area + temp->data, str, len);
2435 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002436 }
2437
2438 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002439 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002440 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002441
2442 return 1;
2443}
2444
Emeric Brun54c4ac82014-11-03 15:32:43 +01002445/* This sample function is designed to extract some bytes from an input buffer.
2446 * First arg is the offset.
2447 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002448static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002449{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002450 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2451 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002452 return 1;
2453 }
2454
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002455 if (smp->data.u.str.size)
2456 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002457 smp->data.u.str.data -= arg_p[0].data.sint;
2458 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002459
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002460 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2461 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002462
2463 return 1;
2464}
2465
Emeric Brunf399b0d2014-11-03 17:07:03 +01002466static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2467 const char *file, int line, char **err)
2468{
2469 struct arg *arg = args;
2470
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002471 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002472 memprintf(err, "Unexpected arg type");
2473 return 0;
2474 }
2475
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002476 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002477 memprintf(err, "Unexpected value 0 for index");
2478 return 0;
2479 }
2480
2481 arg++;
2482
2483 if (arg->type != ARGT_STR) {
2484 memprintf(err, "Unexpected arg type");
2485 return 0;
2486 }
2487
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002488 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002489 memprintf(err, "Empty separators list");
2490 return 0;
2491 }
2492
2493 return 1;
2494}
2495
2496/* This sample function is designed to a return selected part of a string (field).
2497 * First arg is the index of the field (start at 1)
2498 * Second arg is a char list of separators (type string)
2499 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002500static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002501{
Marcin Deranek9631a282018-04-16 14:30:46 +02002502 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002503 char *start, *end;
2504 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002505 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002506
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002507 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002508 return 0;
2509
Marcin Deranek9631a282018-04-16 14:30:46 +02002510 if (arg_p[0].data.sint < 0) {
2511 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002512 end = start = smp->data.u.str.area + smp->data.u.str.data;
2513 while (start > smp->data.u.str.area) {
2514 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2515 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002516 if (field == arg_p[0].data.sint) {
2517 if (count == 1)
2518 goto found;
2519 else if (count > 1)
2520 count--;
2521 } else {
2522 end = start-1;
2523 field--;
2524 }
2525 break;
2526 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002527 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002528 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002529 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002530 } else {
2531 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002532 end = start = smp->data.u.str.area;
2533 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2534 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2535 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002536 if (field == arg_p[0].data.sint) {
2537 if (count == 1)
2538 goto found;
2539 else if (count > 1)
2540 count--;
2541 } else {
2542 start = end+1;
2543 field++;
2544 }
2545 break;
2546 }
2547 }
2548 end++;
2549 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002550 }
2551
2552 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002553 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002554 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002555 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002556 }
2557found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002558 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002559 /* If ret string is len 0, no need to
2560 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002561 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002562 return 1;
2563
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002564 smp->data.u.str.area = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002565
2566 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002567 Note: smp->data.u.str.size cannot be set to 0 */
2568 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002569 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002570
2571 return 1;
2572}
2573
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002574/* This sample function is designed to return a word from a string.
2575 * First arg is the index of the word (start at 1)
2576 * Second arg is a char list of words separators (type string)
2577 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002578static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002579{
Marcin Deranek9631a282018-04-16 14:30:46 +02002580 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002581 char *start, *end;
2582 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002583 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002584
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002585 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002586 return 0;
2587
2588 word = 0;
2589 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002590 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002591 end = start = smp->data.u.str.area + smp->data.u.str.data;
2592 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002593 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002594 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2595 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002596 issep = 1;
2597 break;
2598 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002599 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002600 if (!inword) {
2601 if (!issep) {
2602 if (word != arg_p[0].data.sint) {
2603 word--;
2604 end = start;
2605 }
2606 inword = 1;
2607 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002608 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002609 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002610 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002611 if (count == 1)
2612 goto found;
2613 else if (count > 1)
2614 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002615 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002616 inword = 0;
2617 }
2618 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002619 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002620 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002621 end = start = smp->data.u.str.area;
2622 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002623 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002624 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2625 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002626 issep = 1;
2627 break;
2628 }
2629 }
2630 if (!inword) {
2631 if (!issep) {
2632 if (word != arg_p[0].data.sint) {
2633 word++;
2634 start = end;
2635 }
2636 inword = 1;
2637 }
2638 }
2639 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002640 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002641 if (count == 1)
2642 goto found;
2643 else if (count > 1)
2644 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002645 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002646 inword = 0;
2647 }
2648 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002649 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002650 }
2651
2652 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002653 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002654 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002655 return 1;
2656 }
2657found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002658 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002659 /* If ret string is len 0, no need to
2660 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002661 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002662 return 1;
2663
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002664 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002665
2666 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002667 Note: smp->data.u.str.size cannot be set to 0 */
2668 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002669 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002670
2671 return 1;
2672}
2673
Willy Tarreau7eda8492015-01-20 19:47:06 +01002674static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2675 const char *file, int line, char **err)
2676{
2677 struct arg *arg = args;
2678 char *p;
2679 int len;
2680
2681 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2682 arg[0].type_flags = 0;
2683
2684 if (arg[2].type != ARGT_STR)
2685 return 1;
2686
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002687 p = arg[2].data.str.area;
2688 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002689 while (len) {
2690 if (*p == 'i') {
2691 arg[0].type_flags |= ARGF_REG_ICASE;
2692 }
2693 else if (*p == 'g') {
2694 arg[0].type_flags |= ARGF_REG_GLOB;
2695 }
2696 else {
2697 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2698 return 0;
2699 }
2700 p++;
2701 len--;
2702 }
2703 return 1;
2704}
2705
2706/* This sample function is designed to do the equivalent of s/match/replace/ on
2707 * the input string. It applies a regex and restarts from the last matched
2708 * location until nothing matches anymore. First arg is the regex to apply to
2709 * the input string, second arg is the replacement expression.
2710 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002711static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002712{
2713 char *start, *end;
2714 struct my_regex *reg = arg_p[0].data.reg;
2715 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002716 struct buffer *trash = get_trash_chunk();
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002717 struct buffer *output;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002718 int flag, max;
2719 int found;
2720
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002721 start = smp->data.u.str.area;
2722 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002723
2724 flag = 0;
2725 while (1) {
2726 /* check for last round which is used to copy remaining parts
2727 * when not running in global replacement mode.
2728 */
2729 found = 0;
2730 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2731 /* Note: we can have start == end on empty strings or at the end */
2732 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2733 }
2734
2735 if (!found)
2736 pmatch[0].rm_so = end - start;
2737
2738 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002739 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002740 if (max && pmatch[0].rm_so > 0) {
2741 if (max > pmatch[0].rm_so)
2742 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002743 memcpy(trash->area + trash->data, start, max);
2744 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002745 }
2746
2747 if (!found)
2748 break;
2749
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002750 output = alloc_trash_chunk();
Willy Tarreau23997da2020-02-18 14:27:44 +01002751 if (!output)
2752 break;
2753
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002754 output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2755
Willy Tarreau7eda8492015-01-20 19:47:06 +01002756 /* replace the matching part */
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002757 max = output->size - output->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002758 if (max) {
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002759 if (max > output->data)
2760 max = output->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002761 memcpy(trash->area + trash->data,
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002762 output->area, max);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002763 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002764 }
2765
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002766 free_trash_chunk(output);
2767
Willy Tarreau7eda8492015-01-20 19:47:06 +01002768 /* stop here if we're done with this string */
2769 if (start >= end)
2770 break;
2771
2772 /* We have a special case for matches of length 0 (eg: "x*y*").
2773 * These ones are considered to match in front of a character,
2774 * so we have to copy that character and skip to the next one.
2775 */
2776 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002777 if (trash->data < trash->size)
2778 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002779 pmatch[0].rm_eo++;
2780 }
2781
2782 start += pmatch[0].rm_eo;
2783 flag |= REG_NOTBOL;
2784 }
2785
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002786 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002787 return 1;
2788}
2789
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002790/* This function check an operator entry. It expects a string.
2791 * The string can be an integer or a variable name.
2792 */
2793static int check_operator(struct arg *args, struct sample_conv *conv,
2794 const char *file, int line, char **err)
2795{
2796 const char *str;
2797 const char *end;
Christopher Faulet95917132020-08-05 23:07:07 +02002798 long long int i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002799
2800 /* Try to decode a variable. */
2801 if (vars_check_arg(&args[0], NULL))
2802 return 1;
2803
2804 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002805 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002806 end = str + strlen(str);
Christopher Faulet95917132020-08-05 23:07:07 +02002807 i = read_int64(&str, end);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002808 if (*str != '\0') {
2809 memprintf(err, "expects an integer or a variable name");
2810 return 0;
2811 }
Christopher Faulet95917132020-08-05 23:07:07 +02002812
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002813 chunk_destroy(&args[0].data.str);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002814 args[0].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02002815 args[0].data.sint = i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002816 return 1;
2817}
2818
Willy Tarreau6204cd92016-03-10 16:33:04 +01002819/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002820 * If the arg contain an integer, the integer is returned in the
2821 * sample. If the arg contains a variable descriptor, it returns the
2822 * variable value.
2823 *
2824 * This function returns 0 if an error occurs, otherwise it returns 1.
2825 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002826static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002827{
2828 switch (arg->type) {
2829 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002830 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002831 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002832 return 1;
2833 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002834 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002835 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002836 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002837 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002838 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002839 return 0;
2840 return 1;
2841 default:
2842 return 0;
2843 }
2844}
2845
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002846/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002847 * result.
2848 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002849static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002850{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002851 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002852 return 1;
2853}
2854
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002855/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002856 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002857 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002858static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002859{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002860 struct sample tmp;
2861
Willy Tarreau7560dd42016-03-10 16:28:58 +01002862 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002863 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002864 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002865 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002866 return 1;
2867}
2868
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002869/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002870 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002871 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002872static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002873{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002874 struct sample tmp;
2875
Willy Tarreau7560dd42016-03-10 16:28:58 +01002876 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002877 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002878 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002879 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002880 return 1;
2881}
2882
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002883/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002884 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002885 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002886static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002887{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002888 struct sample tmp;
2889
Willy Tarreau7560dd42016-03-10 16:28:58 +01002890 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002891 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002892 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002893 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002894 return 1;
2895}
2896
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002897static inline long long int arith_add(long long int a, long long int b)
2898{
2899 /* Prevent overflow and makes capped calculus.
2900 * We must ensure that the check calculus doesn't
2901 * exceed the signed 64 bits limits.
2902 *
2903 * +----------+----------+
2904 * | a<0 | a>=0 |
2905 * +------+----------+----------+
2906 * | b<0 | MIN-a>b | no check |
2907 * +------+----------+----------+
2908 * | b>=0 | no check | MAX-a<b |
2909 * +------+----------+----------+
2910 */
2911 if ((a ^ b) >= 0) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002912 /* signs are different. */
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002913 if (a < 0) {
2914 if (LLONG_MIN - a > b)
2915 return LLONG_MIN;
2916 }
2917 if (LLONG_MAX - a < b)
2918 return LLONG_MAX;
2919 }
2920 return a + b;
2921}
2922
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002923/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002924 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002925 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002926static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002927{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002928 struct sample tmp;
2929
Willy Tarreau7560dd42016-03-10 16:28:58 +01002930 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002931 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002932 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002933 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002934 return 1;
2935}
2936
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002937/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002938 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002939 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002940static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002941 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002942{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002943 struct sample tmp;
2944
Willy Tarreau7560dd42016-03-10 16:28:58 +01002945 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002946 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002947 return 0;
2948
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002949 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2950 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2951 * of -LLONG_MIN and correct the result.
2952 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002953 if (tmp.data.u.sint == LLONG_MIN) {
2954 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2955 if (smp->data.u.sint < LLONG_MAX)
2956 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002957 return 1;
2958 }
2959
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002960 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002961 * the second operand.
2962 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002963 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002964 return 1;
2965}
2966
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002967/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002968 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002969 * If the result makes an overflow, then the largest possible quantity is
2970 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002971 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002972static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002973 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002974{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002975 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002976 long long int c;
2977
Willy Tarreau7560dd42016-03-10 16:28:58 +01002978 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002979 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002980 return 0;
2981
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002982 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002983 if (!smp->data.u.sint || !tmp.data.u.sint) {
2984 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002985 return 1;
2986 }
2987
2988 /* The multiply between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002989 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002990 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002991 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2992 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002993 return 1;
2994 }
2995
2996 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002997 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002998
2999 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003000 if (smp->data.u.sint != c / tmp.data.u.sint) {
3001 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
3002 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003003 return 1;
3004 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003005 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003006 return 1;
3007 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003008 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01003009 return 1;
3010}
3011
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003012/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003013 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003014 * If arg_p makes the result overflow, then the largest possible quantity is
3015 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003016 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003017static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003018 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003019{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003020 struct sample tmp;
3021
Willy Tarreau7560dd42016-03-10 16:28:58 +01003022 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01003023 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003024 return 0;
3025
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003026 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003027 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003028 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003029 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003030 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3031 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003032 return 1;
3033 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003034 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003035 return 1;
3036 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003037 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01003038 return 1;
3039}
3040
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003041/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003042 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003043 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003044 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003045static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003046 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003047{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003048 struct sample tmp;
3049
Willy Tarreau7560dd42016-03-10 16:28:58 +01003050 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01003051 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003052 return 0;
3053
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003054 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003055 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003056 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003057 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003058 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3059 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003060 return 1;
3061 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003062 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003063 return 1;
3064 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003065 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01003066 return 1;
3067}
3068
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003069/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01003070 * result.
3071 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003072static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003073 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003074{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003075 if (smp->data.u.sint == LLONG_MIN)
3076 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003077 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003078 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003079 return 1;
3080}
3081
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003082/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003083 * false. The output is a BOOL.
3084 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003085static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003086 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003087{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003088 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003089 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003090 return 1;
3091}
3092
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003093/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003094 * truee. The output is a BOOL.
3095 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003096static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003097 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003098{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003099 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003100 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003101 return 1;
3102}
3103
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003104/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003105 * The output is a BOOL.
3106 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003107static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003108 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003109{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003110 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003111 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003112 return 1;
3113}
3114
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003115/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003116 * The output is a BOOL.
3117 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003118static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003119 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003120{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003121 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003122 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003123 return 1;
3124}
3125
Willy Tarreau280f42b2018-02-19 15:34:12 +01003126/* appends an optional const string, an optional variable contents and another
3127 * optional const string to an existing string.
3128 */
3129static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
3130{
Willy Tarreau83061a82018-07-13 11:56:34 +02003131 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003132 struct sample tmp;
3133 int max;
3134
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003135 trash = alloc_trash_chunk();
William Dauchye9970102021-01-11 11:05:58 +01003136 if (!trash)
3137 return 0;
3138
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003139 trash->data = smp->data.u.str.data;
3140 if (trash->data > trash->size - 1)
3141 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003142
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003143 memcpy(trash->area, smp->data.u.str.area, trash->data);
3144 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003145
3146 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003147 max = arg_p[0].data.str.data;
3148 if (max > trash->size - 1 - trash->data)
3149 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003150
3151 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003152 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
3153 trash->data += max;
3154 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003155 }
3156
3157 /* append second string (variable) if it's found and we can turn it
3158 * into a string.
3159 */
3160 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3161 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
3162 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
3163 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
3164
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003165 max = tmp.data.u.str.data;
3166 if (max > trash->size - 1 - trash->data)
3167 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003168
3169 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003170 memcpy(trash->area + trash->data, tmp.data.u.str.area,
3171 max);
3172 trash->data += max;
3173 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003174 }
3175 }
3176
3177 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003178 max = arg_p[2].data.str.data;
3179 if (max > trash->size - 1 - trash->data)
3180 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003181
3182 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003183 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
3184 trash->data += max;
3185 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003186 }
3187
3188 smp->data.u.str = *trash;
3189 smp->data.type = SMP_T_STR;
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003190 smp_dup(smp);
3191 free_trash_chunk(trash);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003192 return 1;
3193}
3194
3195/* This function checks the "concat" converter's arguments and extracts the
3196 * variable name and its scope.
3197 */
3198static int smp_check_concat(struct arg *args, struct sample_conv *conv,
3199 const char *file, int line, char **err)
3200{
3201 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003202 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
3203 memprintf(err, "failed to register variable name '%s'",
3204 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003205 return 0;
3206 }
3207 return 1;
3208}
3209
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003210/* Compares string with a variable containing a string. Return value
Tim Duesterhusca097c12018-04-27 21:18:45 +02003211 * is compatible with strcmp(3)'s return value.
3212 */
3213static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
3214{
3215 struct sample tmp;
3216 int max, result;
3217
3218 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3219 if (arg_p[0].type != ARGT_VAR)
3220 return 0;
3221 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
3222 return 0;
3223 if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp))
3224 return 0;
3225
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003226 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
3227 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003228 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003229 if (smp->data.u.str.data != tmp.data.u.str.data) {
3230 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02003231 result = -1;
3232 }
3233 else {
3234 result = 1;
3235 }
3236 }
3237 }
3238
3239 smp->data.u.sint = result;
3240 smp->data.type = SMP_T_SINT;
3241 return 1;
3242}
3243
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003244#ifdef USE_OPENSSL
3245/* Compares bytestring with a variable containing a bytestring. Return value
3246 * is `true` if both bytestrings are bytewise identical and `false` otherwise.
3247 *
3248 * Comparison will be performed in constant time if both bytestrings are of
3249 * the same length. If the lengths differ execution time will not be constant.
3250 */
3251static int sample_conv_secure_memcmp(const struct arg *arg_p, struct sample *smp, void *private)
3252{
3253 struct sample tmp;
3254 int result;
3255
3256 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3257 if (arg_p[0].type != ARGT_VAR)
3258 return 0;
3259 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
3260 return 0;
3261 if (!sample_casts[tmp.data.type][SMP_T_BIN](&tmp))
3262 return 0;
3263
3264 if (smp->data.u.str.data != tmp.data.u.str.data) {
3265 smp->data.u.sint = 0;
3266 smp->data.type = SMP_T_BOOL;
3267 return 1;
3268 }
3269
3270 /* The following comparison is performed in constant time. */
3271 result = CRYPTO_memcmp(smp->data.u.str.area, tmp.data.u.str.area, smp->data.u.str.data);
3272
3273 smp->data.u.sint = result == 0;
3274 smp->data.type = SMP_T_BOOL;
3275 return 1;
3276}
3277#endif
3278
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02003279/* Takes a boolean as input. Returns the first argument if that boolean is true and
3280 * the second argument otherwise.
3281 */
3282static int sample_conv_iif(const struct arg *arg_p, struct sample *smp, void *private)
3283{
3284 smp->data.type = SMP_T_STR;
3285 smp->flags |= SMP_F_CONST;
3286
3287 if (smp->data.u.sint) {
3288 smp->data.u.str.data = arg_p[0].data.str.data;
3289 smp->data.u.str.area = arg_p[0].data.str.area;
3290 }
3291 else {
3292 smp->data.u.str.data = arg_p[1].data.str.data;
3293 smp->data.u.str.area = arg_p[1].data.str.area;
3294 }
3295
3296 return 1;
3297}
3298
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003299#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
3300#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
3301#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
3302
3303/*
3304 * Extract the field value of an input binary sample. Takes a mandatory argument:
3305 * the protocol buffers field identifier (dotted notation) internally represented
3306 * as an array of unsigned integers and its size.
3307 * Return 1 if the field was found, 0 if not.
3308 */
3309static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
3310{
3311 unsigned char *pos;
3312 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003313
3314 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003315 grpc_left = smp->data.u.str.data;
3316
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01003317 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003318 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003319
3320 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
3321
3322 pos += GRPC_MSG_HEADER_SZ;
3323 grpc_left -= GRPC_MSG_HEADER_SZ;
3324
3325 if (grpc_left < left)
3326 return 0;
3327
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01003328 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
3329 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003330
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003331 grpc_left -= grpc_msg_len;
3332 }
3333
3334 return 0;
3335}
3336
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003337static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
3338{
3339 unsigned char *pos;
3340 size_t left;
3341
3342 pos = (unsigned char *)smp->data.u.str.area;
3343 left = smp->data.u.str.data;
3344
3345 return protobuf_field_lookup(arg_p, smp, &pos, &left);
3346}
3347
3348static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
3349 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003350{
3351 if (!args[1].type) {
3352 args[1].type = ARGT_SINT;
3353 args[1].data.sint = PBUF_T_BINARY;
3354 }
3355 else {
3356 int pbuf_type;
3357
3358 pbuf_type = protobuf_type(args[1].data.str.area);
3359 if (pbuf_type == -1) {
3360 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
3361 return 0;
3362 }
3363
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003364 chunk_destroy(&args[1].data.str);
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003365 args[1].type = ARGT_SINT;
3366 args[1].data.sint = pbuf_type;
3367 }
3368
3369 return 1;
3370}
3371
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003372/*
3373 * Extract the tag value of an input binary sample. Takes a mandatory argument:
3374 * the FIX protocol tag identifier.
3375 * Return 1 if the tag was found, 0 if not.
3376 */
3377static int sample_conv_fix_tag_value(const struct arg *arg_p, struct sample *smp, void *private)
3378{
3379 struct ist value;
3380
3381 smp->flags &= ~SMP_F_MAY_CHANGE;
3382 value = fix_tag_value(ist2(smp->data.u.str.area, smp->data.u.str.data),
3383 arg_p[0].data.sint);
3384 if (!istlen(value)) {
3385 if (!isttest(value)) {
3386 /* value != IST_NULL, need more data */
3387 smp->flags |= SMP_F_MAY_CHANGE;
3388 }
3389 return 0;
3390 }
3391
3392 smp->data.u.str = ist2buf(value);
3393 smp->flags |= SMP_F_CONST;
3394
3395 return 1;
3396}
3397
3398/* This function checks the "fix_tag_value" converter configuration.
3399 * It expects a "known" (by HAProxy) tag name or ID.
3400 * Tag string names are converted to their ID counterpart because this is the
3401 * format they are sent over the wire.
3402 */
3403static int sample_conv_fix_value_check(struct arg *args, struct sample_conv *conv,
3404 const char *file, int line, char **err)
3405{
3406 struct ist str;
3407 unsigned int tag;
3408
3409 str = ist2(args[0].data.str.area, args[0].data.str.data);
3410 tag = fix_tagid(str);
3411 if (!tag) {
3412 memprintf(err, "Unknown FIX tag name '%s'", args[0].data.str.area);
3413 return 0;
3414 }
3415
3416 chunk_destroy(&args[0].data.str);
3417 args[0].type = ARGT_SINT;
3418 args[0].data.sint = tag;
3419
3420 return 1;
3421}
3422
3423/*
3424 * Checks that a buffer contains a valid FIX message
3425 *
3426 * Return 1 if the check could be run, 0 if not.
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003427 * The result of the analyse itself is stored in <smp> as a boolean
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003428 */
3429static int sample_conv_fix_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3430{
3431 struct ist msg;
3432
3433 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3434
3435 smp->flags &= ~SMP_F_MAY_CHANGE;
3436 switch (fix_validate_message(msg)) {
3437 case FIX_VALID_MESSAGE:
3438 smp->data.type = SMP_T_BOOL;
3439 smp->data.u.sint = 1;
3440 return 1;
3441 case FIX_NEED_MORE_DATA:
3442 smp->flags |= SMP_F_MAY_CHANGE;
3443 return 0;
3444 case FIX_INVALID_MESSAGE:
3445 smp->data.type = SMP_T_BOOL;
3446 smp->data.u.sint = 0;
3447 return 1;
3448 }
3449 return 0;
3450}
3451
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003452/*
3453 * Extract the field value of an input binary sample containing an MQTT packet.
3454 * Takes 2 mandatory arguments:
3455 * - packet type
3456 * - field name
3457 *
3458 * return 1 if the field was found, 0 if not.
3459 */
3460static int sample_conv_mqtt_field_value(const struct arg *arg_p, struct sample *smp, void *private)
3461{
3462 struct ist pkt, value;
3463 int type, fieldname_id;
3464
3465 pkt = ist2(smp->data.u.str.area, smp->data.u.str.data);
3466 type = arg_p[0].data.sint;
3467 fieldname_id = arg_p[1].data.sint;
3468
3469 smp->flags &= ~SMP_F_MAY_CHANGE;
3470 value = mqtt_field_value(pkt, type, fieldname_id);
3471 if (!istlen(value)) {
3472 if (isttest(value)) {
3473 /* value != IST_NULL, need more data */
3474 smp->flags |= SMP_F_MAY_CHANGE;
3475 }
3476 return 0;
3477 }
3478
3479 smp->data.u.str = ist2buf(value);
3480 smp->flags |= SMP_F_CONST;
3481 return 1;
3482}
3483
3484/*
3485 * this function checks the "mqtt_field_value" converter configuration.
3486 * It expects a known packet type name or ID and a field name, in this order
3487 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003488 * Args[0] will be turned into a MQTT_CPT_* value for direct matching when parsing
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003489 * a packet.
3490 */
3491static int sample_conv_mqtt_field_value_check(struct arg *args, struct sample_conv *conv,
3492 const char *file, int line, char **err)
3493{
3494 int type, fieldname_id;
3495
3496 /* check the MQTT packet type is valid */
3497 type = mqtt_typeid(ist2(args[0].data.str.area, args[0].data.str.data));
3498 if (type == MQTT_CPT_INVALID) {
3499 memprintf(err, "Unknown MQTT type '%s'", args[0].data.str.area);
3500 return 0;
3501 }
3502
3503 /* check the field name belongs to the MQTT packet type */
3504 fieldname_id = mqtt_check_type_fieldname(type, ist2(args[1].data.str.area, args[1].data.str.data));
3505 if (fieldname_id == MQTT_FN_INVALID) {
3506 memprintf(err, "Unknown MQTT field name '%s' for packet type '%s'", args[1].data.str.area,
3507 args[0].data.str.area);
3508 return 0;
3509 }
3510
3511 /* save numeric counterparts of type and field name */
3512 chunk_destroy(&args[0].data.str);
3513 chunk_destroy(&args[1].data.str);
3514 args[0].type = ARGT_SINT;
3515 args[0].data.sint = type;
3516 args[1].type = ARGT_SINT;
3517 args[1].data.sint = fieldname_id;
3518
3519 return 1;
3520}
3521
3522/*
3523 * Checks that <smp> contains a valid MQTT message
3524 *
3525 * The function returns 1 if the check was run to its end, 0 otherwise.
3526 * The result of the analyse itself is stored in <smp> as a boolean.
3527 */
3528static int sample_conv_mqtt_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3529{
3530 struct ist msg;
3531
3532 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3533
3534 smp->flags &= ~SMP_F_MAY_CHANGE;
3535 switch (mqtt_validate_message(msg, NULL)) {
3536 case FIX_VALID_MESSAGE:
3537 smp->data.type = SMP_T_BOOL;
3538 smp->data.u.sint = 1;
3539 return 1;
3540 case FIX_NEED_MORE_DATA:
3541 smp->flags |= SMP_F_MAY_CHANGE;
3542 return 0;
3543 case FIX_INVALID_MESSAGE:
3544 smp->data.type = SMP_T_BOOL;
3545 smp->data.u.sint = 0;
3546 return 1;
3547 }
3548 return 0;
3549}
3550
Tim Duesterhusca097c12018-04-27 21:18:45 +02003551/* This function checks the "strcmp" converter's arguments and extracts the
3552 * variable name and its scope.
3553 */
3554static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
3555 const char *file, int line, char **err)
3556{
3557 /* Try to decode a variable. */
3558 if (vars_check_arg(&args[0], NULL))
3559 return 1;
3560
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003561 memprintf(err, "failed to register variable name '%s'",
3562 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003563 return 0;
3564}
3565
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003566#ifdef USE_OPENSSL
3567/* This function checks the "secure_memcmp" converter's arguments and extracts the
3568 * variable name and its scope.
3569 */
3570static int smp_check_secure_memcmp(struct arg *args, struct sample_conv *conv,
3571 const char *file, int line, char **err)
3572{
3573 /* Try to decode a variable. */
3574 if (vars_check_arg(&args[0], NULL))
3575 return 1;
3576
3577 memprintf(err, "failed to register variable name '%s'",
3578 args[0].data.str.area);
3579 return 0;
3580}
3581#endif
3582
Christopher Faulet4ccc12f2020-04-01 09:08:32 +02003583/**/
3584static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private)
3585{
3586 struct buffer *tmp;
3587 uint32_t n;
3588
3589 n = htonl((uint32_t)smp->data.u.sint);
3590 tmp = get_trash_chunk();
3591
3592 memcpy(b_head(tmp), &n, 4);
3593 b_add(tmp, 4);
3594
3595 smp->data.u.str = *tmp;
3596 smp->data.type = SMP_T_BIN;
3597 return 1;
3598}
3599
Christopher Fauletea159d62020-04-01 16:21:44 +02003600/**/
3601static int sample_conv_cut_crlf(const struct arg *arg_p, struct sample *smp, void *private)
3602{
3603 char *p;
3604 size_t l;
3605
3606 p = smp->data.u.str.area;
3607 for (l = 0; l < smp->data.u.str.data; l++) {
3608 if (*(p+l) == '\r' || *(p+l) == '\n')
3609 break;
3610 }
3611 smp->data.u.str.data = l;
3612 return 1;
3613}
3614
Christopher Faulet51fc9d12020-04-01 17:24:41 +02003615/**/
3616static int sample_conv_ltrim(const struct arg *arg_p, struct sample *smp, void *private)
3617{
3618 char *delimiters, *p;
3619 size_t dlen, l;
3620
3621 delimiters = arg_p[0].data.str.area;
3622 dlen = arg_p[0].data.str.data;
3623
3624 l = smp->data.u.str.data;
3625 p = smp->data.u.str.area;
3626 while (l && memchr(delimiters, *p, dlen) != NULL) {
3627 p++;
3628 l--;
3629 }
3630
3631 smp->data.u.str.area = p;
3632 smp->data.u.str.data = l;
3633 return 1;
3634}
3635
Christopher Faulet568415a2020-04-01 17:24:47 +02003636/**/
3637static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *private)
3638{
3639 char *delimiters, *p;
3640 size_t dlen, l;
3641
3642 delimiters = arg_p[0].data.str.area;
3643 dlen = arg_p[0].data.str.data;
3644
3645 l = smp->data.u.str.data;
3646 p = smp->data.u.str.area + l - 1;
3647 while (l && memchr(delimiters, *p, dlen) != NULL) {
3648 p--;
3649 l--;
3650 }
3651
3652 smp->data.u.str.data = l;
3653 return 1;
3654}
3655
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003656/************************************************************************/
3657/* All supported sample fetch functions must be declared here */
3658/************************************************************************/
3659
3660/* force TRUE to be returned at the fetch level */
3661static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003662smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003663{
Willy Tarreau280f42b2018-02-19 15:34:12 +01003664 if (!smp_make_rw(smp))
3665 return 0;
3666
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003667 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003668 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003669 return 1;
3670}
3671
3672/* force FALSE to be returned at the fetch level */
3673static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003674smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003675{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003676 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003677 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003678 return 1;
3679}
3680
3681/* retrieve environment variable $1 as a string */
3682static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003683smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003684{
3685 char *env;
3686
Christopher Faulete6e7a582021-01-29 11:29:28 +01003687 if (args[0].type != ARGT_STR)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003688 return 0;
3689
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003690 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003691 if (!env)
3692 return 0;
3693
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003694 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003695 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003696 smp->data.u.str.area = env;
3697 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003698 return 1;
3699}
3700
Damien Claisseae6f1252019-10-30 15:57:28 +00003701/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3702 * optional string representing the unit of the result: "s" for seconds, "ms" for
3703 * milliseconds and "us" for microseconds.
3704 * Returns 0 on error and non-zero if OK.
3705 */
3706int smp_check_date_unit(struct arg *args, char **err)
3707{
3708 if (args[1].type == ARGT_STR) {
Christopher Faulet95917132020-08-05 23:07:07 +02003709 long long int unit;
3710
Damien Claisseae6f1252019-10-30 15:57:28 +00003711 if (strcmp(args[1].data.str.area, "s") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003712 unit = TIME_UNIT_S;
Damien Claisseae6f1252019-10-30 15:57:28 +00003713 }
3714 else if (strcmp(args[1].data.str.area, "ms") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003715 unit = TIME_UNIT_MS;
Damien Claisseae6f1252019-10-30 15:57:28 +00003716 }
3717 else if (strcmp(args[1].data.str.area, "us") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003718 unit = TIME_UNIT_US;
Damien Claisseae6f1252019-10-30 15:57:28 +00003719 }
3720 else {
3721 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
3722 args[1].data.str.area);
3723 return 0;
3724 }
Christopher Faulet95917132020-08-05 23:07:07 +02003725
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003726 chunk_destroy(&args[1].data.str);
Damien Claisseae6f1252019-10-30 15:57:28 +00003727 args[1].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02003728 args[1].data.sint = unit;
Damien Claisseae6f1252019-10-30 15:57:28 +00003729 }
3730 else if (args[1].type != ARGT_STOP) {
3731 memprintf(err, "Unexpected arg type");
3732 return 0;
3733 }
3734
3735 return 1;
3736}
3737
3738/* retrieve the current local date in epoch time, converts it to milliseconds
3739 * or microseconds if asked to in optional args[1] unit param, and applies an
3740 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003741 */
3742static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003743smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003744{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003745 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003746
Damien Claisseae6f1252019-10-30 15:57:28 +00003747 /* report in milliseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003748 if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003749 smp->data.u.sint *= 1000;
3750 smp->data.u.sint += date.tv_usec / 1000;
3751 }
3752 /* report in microseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003753 else if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003754 smp->data.u.sint *= 1000000;
3755 smp->data.u.sint += date.tv_usec;
3756 }
3757
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003758 /* add offset */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003759 if (args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003760 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003761
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003762 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003763 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3764 return 1;
3765}
3766
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003767/* retrieve the current microsecond part of the date */
3768static int
3769smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3770{
3771 smp->data.u.sint = date.tv_usec;
3772 smp->data.type = SMP_T_SINT;
3773 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3774 return 1;
3775}
3776
3777
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003778/* returns the hostname */
3779static int
3780smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3781{
3782 smp->data.type = SMP_T_STR;
3783 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003784 smp->data.u.str.area = hostname;
3785 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003786 return 1;
3787}
3788
Willy Tarreau0f30d262014-11-24 16:02:05 +01003789/* returns the number of processes */
3790static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003791smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003792{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003793 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003794 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003795 return 1;
3796}
3797
3798/* returns the number of the current process (between 1 and nbproc */
3799static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003800smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003801{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003802 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003803 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003804 return 1;
3805}
3806
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003807/* returns the number of the current thread (between 1 and nbthread */
3808static int
3809smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3810{
3811 smp->data.type = SMP_T_SINT;
3812 smp->data.u.sint = tid;
3813 return 1;
3814}
3815
Willy Tarreau84310e22014-02-14 11:59:04 +01003816/* generate a random 32-bit integer for whatever purpose, with an optional
3817 * range specified in argument.
3818 */
3819static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003820smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01003821{
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003822 smp->data.u.sint = ha_random32();
Willy Tarreau84310e22014-02-14 11:59:04 +01003823
3824 /* reduce if needed. Don't do a modulo, use all bits! */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003825 if (args[0].type == ARGT_SINT)
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003826 smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32;
Willy Tarreau84310e22014-02-14 11:59:04 +01003827
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003828 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01003829 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3830 return 1;
3831}
3832
Willy Tarreau0f30d262014-11-24 16:02:05 +01003833/* returns true if the current process is stopping */
3834static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003835smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003836{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003837 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003838 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003839 return 1;
3840}
3841
Willy Tarreau70fe9442018-11-22 16:07:39 +01003842/* returns the number of calls of the current stream's process_stream() */
3843static int
3844smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
3845{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003846 if (!smp->strm)
3847 return 0;
3848
Willy Tarreau70fe9442018-11-22 16:07:39 +01003849 smp->data.type = SMP_T_SINT;
3850 smp->data.u.sint = smp->strm->task->calls;
3851 return 1;
3852}
3853
3854/* returns the average number of nanoseconds spent processing the stream per call */
3855static int
3856smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3857{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003858 if (!smp->strm)
3859 return 0;
3860
Willy Tarreau70fe9442018-11-22 16:07:39 +01003861 smp->data.type = SMP_T_SINT;
3862 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
3863 return 1;
3864}
3865
3866/* returns the total number of nanoseconds spent processing the stream */
3867static int
3868smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3869{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003870 if (!smp->strm)
3871 return 0;
3872
Willy Tarreau70fe9442018-11-22 16:07:39 +01003873 smp->data.type = SMP_T_SINT;
3874 smp->data.u.sint = smp->strm->task->cpu_time;
3875 return 1;
3876}
3877
3878/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
3879static int
3880smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3881{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003882 if (!smp->strm)
3883 return 0;
3884
Willy Tarreau70fe9442018-11-22 16:07:39 +01003885 smp->data.type = SMP_T_SINT;
3886 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
3887 return 1;
3888}
3889
3890/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
3891static int
3892smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3893{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003894 if (!smp->strm)
3895 return 0;
3896
Willy Tarreau70fe9442018-11-22 16:07:39 +01003897 smp->data.type = SMP_T_SINT;
3898 smp->data.u.sint = smp->strm->task->lat_time;
3899 return 1;
3900}
3901
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003902static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
3903{
3904 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003905 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003906 smp->data.u.str.area = args[0].data.str.area;
3907 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003908 return 1;
3909}
3910
3911static int smp_check_const_bool(struct arg *args, char **err)
3912{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003913 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
3914 strcasecmp(args[0].data.str.area, "1") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003915 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003916 args[0].type = ARGT_SINT;
3917 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003918 return 1;
3919 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003920 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
3921 strcasecmp(args[0].data.str.area, "0") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003922 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003923 args[0].type = ARGT_SINT;
3924 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003925 return 1;
3926 }
3927 memprintf(err, "Expects 'true', 'false', '0' or '1'");
3928 return 0;
3929}
3930
3931static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
3932{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003933 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003934 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003935 return 1;
3936}
3937
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003938static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003939{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003940 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003941 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003942 return 1;
3943}
3944
3945static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
3946{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003947 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003948 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003949 return 1;
3950}
3951
3952static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
3953{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003954 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003955 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003956 return 1;
3957}
3958
3959static int smp_check_const_bin(struct arg *args, char **err)
3960{
David Carlier64a16ab2016-04-08 10:37:02 +01003961 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003962 int binstrlen;
3963
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003964 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003965 return 0;
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003966 chunk_destroy(&args[0].data.str);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003967 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003968 args[0].data.str.area = binstr;
3969 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003970 return 1;
3971}
3972
3973static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
3974{
3975 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003976 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003977 smp->data.u.str.area = args[0].data.str.area;
3978 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003979 return 1;
3980}
3981
3982static int smp_check_const_meth(struct arg *args, char **err)
3983{
3984 enum http_meth_t meth;
3985 int i;
3986
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003987 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003988 if (meth != HTTP_METH_OTHER) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003989 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003990 args[0].type = ARGT_SINT;
3991 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003992 } else {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003993 /* Check method avalaibility. A method is a token defined as :
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003994 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
3995 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
3996 * token = 1*tchar
3997 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003998 for (i = 0; i < args[0].data.str.data; i++) {
3999 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004000 memprintf(err, "expects valid method.");
4001 return 0;
4002 }
4003 }
4004 }
4005 return 1;
4006}
4007
4008static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
4009{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004010 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004011 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004012 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004013 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004014 smp->data.u.meth.str.area = "";
4015 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004016 } else {
4017 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004018 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004019 smp->data.u.meth.str.area = args[0].data.str.area;
4020 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004021 }
4022 return 1;
4023}
4024
Luca Schimweg8a694b82019-09-10 15:42:52 +02004025// This function checks the "uuid" sample's arguments.
4026// Function won't get called when no parameter is specified (maybe a bug?)
4027static int smp_check_uuid(struct arg *args, char **err)
4028{
4029 if (!args[0].type) {
4030 args[0].type = ARGT_SINT;
4031 args[0].data.sint = 4;
4032 }
4033 else if (args[0].data.sint != 4) {
4034 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
4035 return 0;
4036 }
4037
4038 return 1;
4039}
4040
4041// Generate a RFC4122 UUID (default is v4 = fully random)
4042static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
4043{
4044 if (args[0].data.sint == 4 || !args[0].type) {
Willy Tarreauee3bcdd2020-03-08 17:48:17 +01004045 ha_generate_uuid(&trash);
Luca Schimweg8a694b82019-09-10 15:42:52 +02004046 smp->data.type = SMP_T_STR;
4047 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4048 smp->data.u.str = trash;
4049 return 1;
4050 }
4051
4052 // more implementations of other uuid formats possible here
4053 return 0;
4054}
4055
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004056/* Note: must not be declared <const> as its list will be overwritten.
4057 * Note: fetches that may return multiple types must be declared as the lowest
4058 * common denominator, the type that can be casted into all other ones. For
4059 * instance IPv4/IPv6 must be declared IPv4.
4060 */
4061static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau0209c972021-03-26 12:03:11 +01004062 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4063 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4064 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_CONST },
4065 { "date", smp_fetch_date, ARG2(0,SINT,STR), smp_check_date_unit, SMP_T_SINT, SMP_USE_CONST },
4066 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4067 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_CONST },
4068 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_CONST },
4069 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4070 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4071 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_CONST },
Willy Tarreau0f30d262014-11-24 16:02:05 +01004072 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau0209c972021-03-26 12:03:11 +01004073 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_CONST },
Willy Tarreau70fe9442018-11-22 16:07:39 +01004074
4075 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4076 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4077 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4078 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4079 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004080
Willy Tarreau0209c972021-03-26 12:03:11 +01004081 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_CONST },
4082 { "bool", smp_fetch_const_bool, ARG1(1,STR), smp_check_const_bool, SMP_T_BOOL, SMP_USE_CONST },
4083 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_CONST },
4084 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_CONST },
4085 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_CONST },
4086 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_CONST },
4087 { "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 +02004088
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004089 { /* END */ },
4090}};
4091
Willy Tarreau0108d902018-11-25 19:14:37 +01004092INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
4093
Emeric Brun107ca302010-01-04 16:16:05 +01004094/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02004095static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau0851fd52019-12-17 10:07:25 +01004096 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
Holger Just1bfc24b2017-05-06 00:56:53 +02004097 { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN },
Emeric Brun53d1a982014-04-30 18:21:37 +02004098 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02004099 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
4100 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau60a2ee72017-12-15 07:13:48 +01004101 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01004102 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Dragan Dosen3f957b22017-10-24 09:27:34 +02004103 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
Tim Duesterhus1478aa72018-01-25 16:24:51 +01004104 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02004105 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4106 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004107 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01004108 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004109 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4110 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4111 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01004112 { "xxh3", sample_conv_xxh3, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER01e09742016-12-26 11:46:11 +01004113 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4114 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02004115 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004116 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
Marcin Deranek9631a282018-04-16 14:30:46 +02004117 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4118 { "word", sample_conv_word, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
Willy Tarreau7eda8492015-01-20 19:47:06 +01004119 { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02004120 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02004121#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01004122 { "sha2", sample_conv_sha2, ARG1(0, SINT), smp_check_sha2, SMP_T_BIN, SMP_T_BIN },
Ilya Shipitsin2c481d02021-03-26 23:35:31 +05004123#ifdef EVP_CIPH_GCM_MODE
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02004124 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
4125#endif
Patrick Gansterer8e366512020-04-22 16:47:57 +02004126 { "digest", sample_conv_crypto_digest, ARG1(1,STR), check_crypto_digest, SMP_T_BIN, SMP_T_BIN },
4127 { "hmac", sample_conv_crypto_hmac, ARG2(2,STR,STR), check_crypto_hmac, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02004128#endif
Willy Tarreau280f42b2018-02-19 15:34:12 +01004129 { "concat", sample_conv_concat, ARG3(1,STR,STR,STR), smp_check_concat, SMP_T_STR, SMP_T_STR },
Tim Duesterhusca097c12018-04-27 21:18:45 +02004130 { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT },
Tim Duesterhusf38175c2020-06-09 11:48:42 +02004131#ifdef USE_OPENSSL
4132 { "secure_memcmp", sample_conv_secure_memcmp, ARG1(1,STR), smp_check_secure_memcmp, SMP_T_BIN, SMP_T_BOOL },
4133#endif
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01004134
4135 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01004136 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
4137 { "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 +01004138
Baptiste Assmanne138dda2020-10-22 15:39:03 +02004139 /* FIX converters */
4140 { "fix_is_valid", sample_conv_fix_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4141 { "fix_tag_value", sample_conv_fix_tag_value, ARG1(1,STR), sample_conv_fix_value_check, SMP_T_BIN, SMP_T_BIN },
4142
Baptiste Assmanne279ca62020-10-27 18:10:06 +01004143 /* MQTT converters */
4144 { "mqtt_is_valid", sample_conv_mqtt_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4145 { "mqtt_field_value", sample_conv_mqtt_field_value, ARG2(2,STR,STR), sample_conv_mqtt_field_value_check, SMP_T_BIN, SMP_T_STR },
4146
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02004147 { "iif", sample_conv_iif, ARG2(2, STR, STR), NULL, SMP_T_BOOL, SMP_T_STR },
4148
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02004149 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4150 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4151 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4152 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
4153 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4154 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4155 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4156 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4157 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4158 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4159 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4160 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4161 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4162 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01004163
Christopher Fauletea159d62020-04-01 16:21:44 +02004164 { "htonl", sample_conv_htonl, 0, NULL, SMP_T_SINT, SMP_T_BIN },
4165 { "cut_crlf", sample_conv_cut_crlf, 0, NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet51fc9d12020-04-01 17:24:41 +02004166 { "ltrim", sample_conv_ltrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet568415a2020-04-01 17:24:47 +02004167 { "rtrim", sample_conv_rtrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02004168 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01004169}};
4170
Willy Tarreau0108d902018-11-25 19:14:37 +01004171INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);