blob: 47ccfbb0b364026a8e1f93493c5cdfaaeaec0c5c [file] [log] [blame]
Emeric Brun107ca302010-01-04 16:16:05 +01001/*
Willy Tarreaucd3b0942012-04-27 21:52:18 +02002 * Sample management functions.
Emeric Brun107ca302010-01-04 16:16:05 +01003 *
4 * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreaucd3b0942012-04-27 21:52:18 +02005 * Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
Emeric Brun107ca302010-01-04 16:16:05 +01006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Thierry FOURNIER317e1c42014-08-12 10:20:47 +020014#include <ctype.h>
Emeric Brun107ca302010-01-04 16:16:05 +010015#include <string.h>
16#include <arpa/inet.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020017#include <stdio.h>
Emeric Brun107ca302010-01-04 16:16:05 +010018
Alex51c8ad42021-04-15 16:45:15 +020019#include <import/mjson.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <import/sha1.h>
Willy Tarreau7e2c6472012-10-29 20:44:36 +010021
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/api.h>
23#include <haproxy/arg.h>
Willy Tarreauac13aea2020-06-04 10:36:03 +020024#include <haproxy/auth.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020025#include <haproxy/base64.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020026#include <haproxy/buf.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020027#include <haproxy/chunk.h>
Willy Tarreau55542642021-10-08 09:33:24 +020028#include <haproxy/clock.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020029#include <haproxy/errors.h>
Baptiste Assmanne138dda2020-10-22 15:39:03 +020030#include <haproxy/fix.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020031#include <haproxy/global.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020032#include <haproxy/hash.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020033#include <haproxy/http.h>
Baptiste Assmanne138dda2020-10-22 15:39:03 +020034#include <haproxy/istbuf.h>
Baptiste Assmanne279ca62020-10-27 18:10:06 +010035#include <haproxy/mqtt.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020036#include <haproxy/net_helper.h>
Willy Tarreaub23e5952020-06-04 16:06:59 +020037#include <haproxy/protobuf.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020038#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020039#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020040#include <haproxy/sample.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020041#include <haproxy/sink.h>
42#include <haproxy/stick_table.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020043#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020044#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020045#include <haproxy/vars.h>
Tim Duesterhusd5fc8fc2021-09-11 17:51:13 +020046#include <haproxy/xxhash.h>
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +020047#include <haproxy/jwt.h>
Thierry FOURNIER01e09742016-12-26 11:46:11 +010048
Willy Tarreau1cf8f082014-02-07 12:14:54 +010049/* sample type names */
50const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020051 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010052 [SMP_T_BOOL] = "bool",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010053 [SMP_T_SINT] = "sint",
54 [SMP_T_ADDR] = "addr",
55 [SMP_T_IPV4] = "ipv4",
56 [SMP_T_IPV6] = "ipv6",
57 [SMP_T_STR] = "str",
58 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020059 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010060};
61
Willy Tarreau12785782012-04-27 21:37:17 +020062/* static sample used in sample_process() when <p> is NULL */
Emeric Brune5c918b2017-06-14 14:15:36 +020063static THREAD_LOCAL struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010064
Willy Tarreau12785782012-04-27 21:37:17 +020065/* list head of all known sample fetch keywords */
66static struct sample_fetch_kw_list sample_fetches = {
67 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010068};
69
Willy Tarreau12785782012-04-27 21:37:17 +020070/* list head of all known sample format conversion keywords */
71static struct sample_conv_kw_list sample_convs = {
72 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010073};
74
Willy Tarreau80aca902013-01-07 15:42:20 +010075const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
Willy Tarreaube2159b2021-03-26 11:56:11 +010076 [SMP_SRC_CONST] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
77 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
78 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
79 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
80 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
81 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +010082 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL_CFG_PARSER |
83 SMP_VAL_CLI_PARSER ),
Willy Tarreaube2159b2021-03-26 11:56:11 +010084
Willy Tarreau80aca902013-01-07 15:42:20 +010085 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
86 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
87 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
88 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
89 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
90 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +010091 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
92 SMP_VAL_CLI_PARSER ),
Willy Tarreau80aca902013-01-07 15:42:20 +010093
94 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
95 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
96 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
97 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
98 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
99 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100100 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
101 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100102
103 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
104 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
105 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
106 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
107 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
108 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100109 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
110 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100111
112 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
113 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
114 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
115 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
116 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
117 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100118 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
119 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100120
121 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
122 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
123 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
124 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
125 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
126 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100127 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
128 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100129
130 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
131 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
132 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
133 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
134 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
135 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100136 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
137 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100138
139 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
140 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
141 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
142 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
143 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
144 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100145 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
146 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100147
148 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
149 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
150 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
151 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
152 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
153 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100154 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
155 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100156
157 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
158 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
159 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
160 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
161 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
162 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100163 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
164 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100165
166 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
167 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
168 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
169 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
170 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
171 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100172 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
173 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100174
175 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
176 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
177 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
178 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
179 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
180 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100181 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
182 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100183
184 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
185 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
186 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
187 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
188 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
189 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100190 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
191 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100192
193 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
194 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
195 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
196 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
197 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
198 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100199 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
200 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100201
202 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
203 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
204 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
205 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
206 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
207 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100208 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
209 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100210
211 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
212 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
213 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
214 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
215 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
216 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100217 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
218 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100219
220 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
221 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
222 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
223 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
224 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
225 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100226 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
227 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100228
229 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
230 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
231 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
232 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
233 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
234 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100235 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
236 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100237
238 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
239 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
240 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
241 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
242 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
243 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100244 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
245 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100246
247 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
248 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
249 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
250 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
251 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
252 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100253 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
254 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100255
256 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
257 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
258 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
259 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
260 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
261 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100262 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
263 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100264
265 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
266 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
267 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
268 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
269 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
270 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100271 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
272 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100273
274 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
275 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
276 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
277 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
278 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
279 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100280 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
281 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100282};
283
284static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
285 [SMP_SRC_INTRN] = "internal state",
286 [SMP_SRC_LISTN] = "listener",
287 [SMP_SRC_FTEND] = "frontend",
288 [SMP_SRC_L4CLI] = "client address",
289 [SMP_SRC_L5CLI] = "client-side connection",
290 [SMP_SRC_TRACK] = "track counters",
291 [SMP_SRC_L6REQ] = "request buffer",
292 [SMP_SRC_HRQHV] = "HTTP request headers",
293 [SMP_SRC_HRQHP] = "HTTP request",
294 [SMP_SRC_HRQBO] = "HTTP request body",
295 [SMP_SRC_BKEND] = "backend",
296 [SMP_SRC_SERVR] = "server",
297 [SMP_SRC_L4SRV] = "server address",
298 [SMP_SRC_L5SRV] = "server-side connection",
299 [SMP_SRC_L6RES] = "response buffer",
300 [SMP_SRC_HRSHV] = "HTTP response headers",
301 [SMP_SRC_HRSHP] = "HTTP response",
302 [SMP_SRC_HRSBO] = "HTTP response body",
303 [SMP_SRC_RQFIN] = "request buffer statistics",
304 [SMP_SRC_RSFIN] = "response buffer statistics",
305 [SMP_SRC_TXFIN] = "transaction statistics",
306 [SMP_SRC_SSFIN] = "session statistics",
307};
308
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100309static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
310 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
311 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
312 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
313 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
314 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
315 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
316 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
317 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
318 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
319 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
320 [SMP_CKP_BE_SRV_CON] = "server source selection",
321 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
322 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
323 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
324 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
325 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
326 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
327 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
328 [SMP_CKP_FE_LOG_END] = "logs",
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100329 [SMP_CKP_BE_CHK_RUL] = "backend tcp-check rule",
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100330};
331
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100332/* This function returns the type of the data returned by the sample_expr.
333 * It assumes that the <expr> and all of its converters are properly
334 * initialized.
335 */
336inline
337int smp_expr_output_type(struct sample_expr *expr)
338{
339 struct sample_conv_expr *smp_expr;
340
341 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
342 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
343 return smp_expr->conv->out_type;
344 }
345 return expr->fetch->out_type;
346}
347
348
Willy Tarreau80aca902013-01-07 15:42:20 +0100349/* fill the trash with a comma-delimited list of source names for the <use> bit
350 * field which must be composed of a non-null set of SMP_USE_* flags. The return
351 * value is the pointer to the string in the trash buffer.
352 */
353const char *sample_src_names(unsigned int use)
354{
355 int bit;
356
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200357 trash.data = 0;
358 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100359 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
360 if (!(use & ~((1 << bit) - 1)))
361 break; /* no more bits */
362
363 if (!(use & (1 << bit)))
364 continue; /* bit not set */
365
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200366 trash.data += snprintf(trash.area + trash.data,
367 trash.size - trash.data, "%s%s",
368 (use & ((1 << bit) - 1)) ? "," : "",
369 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100370 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200371 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100372}
373
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100374/* return a pointer to the correct sample checkpoint name, or "unknown" when
375 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
376 * if set.
377 */
378const char *sample_ckp_names(unsigned int use)
379{
380 int bit;
381
382 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
383 if (use & (1 << bit))
384 return fetch_ckp_names[bit];
385 return "unknown sample check place, please report this bug";
386}
387
Emeric Brun107ca302010-01-04 16:16:05 +0100388/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100389 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
390 * for next parsing sessions. The fetch keywords capabilities are also computed
391 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100392 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100393void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100394{
Willy Tarreau80aca902013-01-07 15:42:20 +0100395 struct sample_fetch *sf;
396 int bit;
397
398 for (sf = kwl->kw; sf->kw != NULL; sf++) {
399 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
400 if (sf->use & (1 << bit))
401 sf->val |= fetch_cap[bit];
402 }
Willy Tarreau2b718102021-04-21 07:32:39 +0200403 LIST_APPEND(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100404}
405
406/*
Willy Tarreau12785782012-04-27 21:37:17 +0200407 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100408 * parsing sessions.
409 */
Willy Tarreau12785782012-04-27 21:37:17 +0200410void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100411{
Willy Tarreau2b718102021-04-21 07:32:39 +0200412 LIST_APPEND(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100413}
414
415/*
Willy Tarreau12785782012-04-27 21:37:17 +0200416 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100417 * string of <len> in buffer <kw>.
418 *
419 */
Willy Tarreau12785782012-04-27 21:37:17 +0200420struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100421{
422 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200423 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100424
Willy Tarreau12785782012-04-27 21:37:17 +0200425 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100426 for (index = 0; kwl->kw[index].kw != NULL; index++) {
427 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
428 kwl->kw[index].kw[len] == '\0')
429 return &kwl->kw[index];
430 }
431 }
432 return NULL;
433}
434
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100435/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100436 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100437 * <idx> is the index of the next sample fetch entry. It is used as private
438 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100439 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100440 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100441 * the list is reached.
442 */
443struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
444{
445 struct sample_fetch_kw_list *kwl;
446 struct sample_fetch *base;
447
448 if (!current) {
449 /* Get first kwl entry. */
450 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
451 (*idx) = 0;
452 } else {
453 /* Get kwl corresponding to the curret entry. */
454 base = current + 1 - (*idx);
455 kwl = container_of(base, struct sample_fetch_kw_list, kw);
456 }
457
458 while (1) {
459
460 /* Check if kwl is the last entry. */
461 if (&kwl->list == &sample_fetches.list)
462 return NULL;
463
464 /* idx contain the next keyword. If it is available, return it. */
465 if (kwl->kw[*idx].kw) {
466 (*idx)++;
467 return &kwl->kw[(*idx)-1];
468 }
469
470 /* get next entry in the main list, and return NULL if the end is reached. */
471 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
472
473 /* Set index to 0, ans do one other loop. */
474 (*idx) = 0;
475 }
476}
477
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100478/* This function browses the list of available converters. <current> is
479 * the last used converter. If it is the first call, it must set to NULL.
480 * <idx> is the index of the next converter entry. It is used as private
481 * value. It is useless to initiate it.
482 *
483 * It returns always the next sample_conv entry, and NULL when the end of
484 * the list is reached.
485 */
486struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
487{
488 struct sample_conv_kw_list *kwl;
489 struct sample_conv *base;
490
491 if (!current) {
492 /* Get first kwl entry. */
493 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
494 (*idx) = 0;
495 } else {
496 /* Get kwl corresponding to the curret entry. */
497 base = current + 1 - (*idx);
498 kwl = container_of(base, struct sample_conv_kw_list, kw);
499 }
500
501 while (1) {
502 /* Check if kwl is the last entry. */
503 if (&kwl->list == &sample_convs.list)
504 return NULL;
505
506 /* idx contain the next keyword. If it is available, return it. */
507 if (kwl->kw[*idx].kw) {
508 (*idx)++;
509 return &kwl->kw[(*idx)-1];
510 }
511
512 /* get next entry in the main list, and return NULL if the end is reached. */
513 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
514
515 /* Set index to 0, ans do one other loop. */
516 (*idx) = 0;
517 }
518}
519
Emeric Brun107ca302010-01-04 16:16:05 +0100520/*
Willy Tarreau12785782012-04-27 21:37:17 +0200521 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100522 * string of <len> in buffer <kw>.
523 *
524 */
Willy Tarreau12785782012-04-27 21:37:17 +0200525struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100526{
527 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200528 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100529
Willy Tarreau12785782012-04-27 21:37:17 +0200530 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100531 for (index = 0; kwl->kw[index].kw != NULL; index++) {
532 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
533 kwl->kw[index].kw[len] == '\0')
534 return &kwl->kw[index];
535 }
536 }
537 return NULL;
538}
539
Emeric Brun107ca302010-01-04 16:16:05 +0100540/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200541/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100542/******************************************************************/
543
Willy Tarreau342acb42012-04-23 22:03:39 +0200544static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100545{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200546 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200547 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100548 return 1;
549}
550
Willy Tarreau342acb42012-04-23 22:03:39 +0200551static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100552{
Willy Tarreau83061a82018-07-13 11:56:34 +0200553 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100554
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200555 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100556 return 0;
557
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200558 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200559 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200560 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100561 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100562
563 return 1;
564}
565
Willy Tarreau342acb42012-04-23 22:03:39 +0200566static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100567{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200568 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200569 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100570 return 1;
571}
572
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200573static int c_ipv62ip(struct sample *smp)
574{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200575 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200576 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100577 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200578 return 1;
579}
580
Willy Tarreau342acb42012-04-23 22:03:39 +0200581static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100582{
Willy Tarreau83061a82018-07-13 11:56:34 +0200583 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100584
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200585 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100586 return 0;
587
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200588 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200589 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200590 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100591 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100592 return 1;
593}
594
595/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200596static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100597{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200598 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100599}
600*/
601
Willy Tarreau342acb42012-04-23 22:03:39 +0200602static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100603{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200604 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200605 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100606 return 1;
607}
608
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200609static int c_int2ipv6(struct sample *smp)
610{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200611 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
612 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200613 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200614 return 1;
615}
616
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100617static int c_str2addr(struct sample *smp)
618{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200619 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
620 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100621 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200622 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100623 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100624 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100625 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200626 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100627 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100628 return 1;
629}
630
Willy Tarreau342acb42012-04-23 22:03:39 +0200631static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100632{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200633 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100634 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200635 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100636 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100637 return 1;
638}
639
Willy Tarreau342acb42012-04-23 22:03:39 +0200640static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100641{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200642 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100643 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200644 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100645 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100646 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100647}
648
Emeric Brun4b9e8022014-11-03 18:17:10 +0100649/*
650 * The NULL char always enforces the end of string if it is met.
651 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100652 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200653static int c_bin2str(struct sample *smp)
654{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100655 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200656
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200657 for (i = 0; i < smp->data.u.str.data; i++) {
658 if (!smp->data.u.str.area[i]) {
659 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100660 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100661 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200662 }
Christopher Faulet472ad512020-04-30 09:57:40 +0200663 smp->data.type = SMP_T_STR;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200664 return 1;
665}
666
Willy Tarreau342acb42012-04-23 22:03:39 +0200667static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100668{
Willy Tarreau83061a82018-07-13 11:56:34 +0200669 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100670 char *pos;
671
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200672 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100673 if (!pos)
674 return 0;
675
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200676 trash->size = trash->size - (pos - trash->area);
677 trash->area = pos;
678 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200679 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200680 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100681 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100682 return 1;
683}
684
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800685/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200686 * For strings and binary blocks, it also provides a known allocated size with
687 * a length that is capped to the size, and ensures a trailing zero is always
688 * appended for strings. This is necessary for some operations which may
689 * require to extend the length. It returns 0 if it fails, 1 on success.
690 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100691int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200692{
Willy Tarreau83061a82018-07-13 11:56:34 +0200693 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200694
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200695 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100696 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100697 case SMP_T_SINT:
698 case SMP_T_ADDR:
699 case SMP_T_IPV4:
700 case SMP_T_IPV6:
701 /* These type are not const. */
702 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200703
Christopher Fauletec100512017-07-24 15:38:41 +0200704 case SMP_T_METH:
705 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
706 break;
707 /* Fall through */
708
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100709 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200710 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100711 trash->data = smp->data.type == SMP_T_STR ?
712 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200713 if (trash->data > trash->size - 1)
714 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200715
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100716 memcpy(trash->area, smp->data.type == SMP_T_STR ?
717 smp->data.u.str.area : smp->data.u.meth.str.area,
718 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200719 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200720 smp->data.u.str = *trash;
721 break;
722
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100723 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100724 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200725 trash->data = smp->data.u.str.data;
726 if (trash->data > trash->size)
727 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200728
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200729 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200730 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100731 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200732
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100733 default:
734 /* Other cases are unexpected. */
735 return 0;
736 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100737
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100738 /* remove const flag */
739 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200740 return 1;
741}
742
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100743int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100744{
745 return 1;
746}
747
Willy Tarreau342acb42012-04-23 22:03:39 +0200748static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100749{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200750 const char *str;
751 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100752
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200753 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100754 return 0;
755
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200756 str = smp->data.u.str.area;
757 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100758
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200759 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200760 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100761 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100762 return 1;
763}
764
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100765static int c_str2meth(struct sample *smp)
766{
767 enum http_meth_t meth;
768 int len;
769
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200770 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100771 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200772 len = smp->data.u.str.data;
773 smp->data.u.meth.str.area = smp->data.u.str.area;
774 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100775 }
776 else
777 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200778 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200779 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100780 return 1;
781}
782
783static int c_meth2str(struct sample *smp)
784{
785 int len;
786 enum http_meth_t meth;
787
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200788 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100789 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200790 len = smp->data.u.meth.str.data;
791 smp->data.u.str.area = smp->data.u.meth.str.area;
792 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200793 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100794 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200795 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100796 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200797 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200798 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200799 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100800 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200801 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100802 }
803 else {
804 /* Unknown method */
805 return 0;
806 }
807 return 1;
808}
809
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200810static int c_addr2bin(struct sample *smp)
811{
Willy Tarreau83061a82018-07-13 11:56:34 +0200812 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200813
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200814 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200815 chk->data = 4;
816 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200817 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200818 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200819 chk->data = 16;
820 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200821 }
822 else
823 return 0;
824
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200825 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200826 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200827 return 1;
828}
829
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200830static int c_int2bin(struct sample *smp)
831{
Willy Tarreau83061a82018-07-13 11:56:34 +0200832 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200833
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200834 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
835 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200836
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200837 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200838 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200839 return 1;
840}
841
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200842
Emeric Brun107ca302010-01-04 16:16:05 +0100843/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200844/* Sample casts matrix: */
845/* sample_casts[from type][to type] */
846/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100847/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100848
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100849sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200850/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
851/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
852/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200853/* 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 +0200854/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
855/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200856/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200857/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
858/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
859/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200860};
Emeric Brun107ca302010-01-04 16:16:05 +0100861
Emeric Brun107ca302010-01-04 16:16:05 +0100862/*
Willy Tarreau12785782012-04-27 21:37:17 +0200863 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100864 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200865 * Returns a pointer on allocated sample expression structure.
Christopher Faulet35926a12021-09-30 08:48:56 +0200866 * <al> is an arg_list serving as a list head to report missing dependencies.
867 * It may be NULL if such dependencies are not allowed. Otherwise, the caller
868 * must have set al->ctx if al is set.
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100869 * If <endptr> is non-nul, it will be set to the first unparsed character
870 * (which may be the final '\0') on success. If it is nul, the expression
871 * must be properly terminated by a '\0' otherwise an error is reported.
Emeric Brun107ca302010-01-04 16:16:05 +0100872 */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100873struct 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 +0100874{
Willy Tarreau833cc792013-07-24 15:34:19 +0200875 const char *begw; /* beginning of word */
876 const char *endw; /* end of word */
877 const char *endt; /* end of term */
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +0100878 struct sample_expr *expr = NULL;
Willy Tarreau12785782012-04-27 21:37:17 +0200879 struct sample_fetch *fetch;
880 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100881 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200882 char *fkw = NULL;
883 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100884 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100885
Willy Tarreau833cc792013-07-24 15:34:19 +0200886 begw = str[*idx];
Willy Tarreaued2c6622020-02-14 18:27:10 +0100887 for (endw = begw; is_idchar(*endw); endw++)
888 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200889
890 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100891 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100892 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200893 }
Emeric Brun107ca302010-01-04 16:16:05 +0100894
Willy Tarreau833cc792013-07-24 15:34:19 +0200895 /* keep a copy of the current fetch keyword for error reporting */
896 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100897
Willy Tarreau833cc792013-07-24 15:34:19 +0200898 fetch = find_sample_fetch(begw, endw - begw);
899 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100900 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100901 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200902 }
Emeric Brun107ca302010-01-04 16:16:05 +0100903
Willy Tarreau833cc792013-07-24 15:34:19 +0200904 /* At this point, we have :
905 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100906 * - endw : end of the keyword, first character not part of keyword
Willy Tarreau833cc792013-07-24 15:34:19 +0200907 */
908
909 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100910 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100911 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200912 }
Emeric Brun107ca302010-01-04 16:16:05 +0100913 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200914
Vincent Bernat02779b62016-04-03 13:48:43 +0200915 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200916 if (!expr)
917 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100918
919 LIST_INIT(&(expr->conv_exprs));
920 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200921 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100922
Willy Tarreau689a1df2013-12-13 00:40:11 +0100923 /* Note that we call the argument parser even with an empty string,
924 * this allows it to automatically create entries for mandatory
925 * implicit arguments (eg: local proxy name).
926 */
Christopher Faulet35926a12021-09-30 08:48:56 +0200927 if (al) {
928 al->kw = expr->fetch->kw;
929 al->conv = NULL;
930 }
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100931 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 +0100932 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
933 goto out_error;
934 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200935
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100936 /* now endt is our first char not part of the arg list, typically the
937 * comma after the sample fetch name or after the closing parenthesis,
938 * or the NUL char.
939 */
940
Willy Tarreau689a1df2013-12-13 00:40:11 +0100941 if (!expr->arg_p) {
942 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200943 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100944 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
945 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200946 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100947 }
948
Willy Tarreau833cc792013-07-24 15:34:19 +0200949 /* Now process the converters if any. We have two supported syntaxes
950 * for the converters, which can be combined :
951 * - comma-delimited list of converters just after the keyword and args ;
952 * - one converter per keyword
953 * The combination allows to have each keyword being a comma-delimited
954 * series of converters.
955 *
956 * We want to process the former first, then the latter. For this we start
957 * from the beginning of the supposed place in the exiting conv chain, which
958 * starts at the last comma (endt).
959 */
960
961 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200962 struct sample_conv_expr *conv_expr;
Willy Tarreau46dfd782019-12-17 10:25:29 +0100963 int err_arg;
964 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +0100965
Willy Tarreau833cc792013-07-24 15:34:19 +0200966 if (*endt && *endt != ',') {
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100967 if (endptr) {
968 /* end found, let's stop here */
969 break;
970 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200971 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100972 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200973 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100974 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100975 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200976 }
Emeric Brun107ca302010-01-04 16:16:05 +0100977
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100978 /* FIXME: how long should we support such idiocies ? Maybe we
979 * should already warn ?
980 */
Willy Tarreau833cc792013-07-24 15:34:19 +0200981 while (*endt == ',') /* then trailing commas */
982 endt++;
983
Willy Tarreau97108e02016-11-25 07:33:24 +0100984 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200985
986 if (!*begw) {
987 /* none ? skip to next string */
988 (*idx)++;
989 begw = str[*idx];
990 if (!begw || !*begw)
991 break;
992 }
993
Willy Tarreaued2c6622020-02-14 18:27:10 +0100994 for (endw = begw; is_idchar(*endw); endw++)
995 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200996
997 free(ckw);
998 ckw = my_strndup(begw, endw - begw);
999
1000 conv = find_sample_conv(begw, endw - begw);
1001 if (!conv) {
1002 /* we found an isolated keyword that we don't know, it's not ours */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001003 if (begw == str[*idx]) {
1004 endt = begw;
Willy Tarreau833cc792013-07-24 15:34:19 +02001005 break;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001006 }
Willy Tarreau97108e02016-11-25 07:33:24 +01001007 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +02001008 goto out_error;
1009 }
Emeric Brun107ca302010-01-04 16:16:05 +01001010
Willy Tarreau833cc792013-07-24 15:34:19 +02001011 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001012 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001013 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001014 }
Emeric Brun107ca302010-01-04 16:16:05 +01001015
1016 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +02001017 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001018 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001019 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001020 }
Emeric Brun107ca302010-01-04 16:16:05 +01001021
1022 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +02001023 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +02001024 if (!conv_expr)
1025 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001026
Willy Tarreau2b718102021-04-21 07:32:39 +02001027 LIST_APPEND(&(expr->conv_exprs), &(conv_expr->list));
Emeric Brun107ca302010-01-04 16:16:05 +01001028 conv_expr->conv = conv;
1029
Christopher Faulet35926a12021-09-30 08:48:56 +02001030 if (al) {
1031 al->kw = expr->fetch->kw;
1032 al->conv = conv_expr->conv->kw;
1033 }
Willy Tarreau80b53ff2020-02-14 08:40:37 +01001034 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 +01001035 if (argcnt < 0) {
1036 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
1037 goto out_error;
1038 }
Willy Tarreau9e92d322010-01-26 17:58:06 +01001039
Willy Tarreau46dfd782019-12-17 10:25:29 +01001040 if (argcnt && !conv->arg_mask) {
1041 memprintf(err_msg, "converter '%s' does not support any args", ckw);
1042 goto out_error;
1043 }
Willy Tarreau21d68a62012-04-20 15:52:36 +02001044
Willy Tarreau46dfd782019-12-17 10:25:29 +01001045 if (!conv_expr->arg_p)
1046 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001047
Willy Tarreau46dfd782019-12-17 10:25:29 +01001048 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1049 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001050 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001051 }
1052 }
Emeric Brun485479d2010-09-23 18:02:19 +02001053
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001054 if (endptr) {
1055 /* end found, let's stop here */
1056 *endptr = (char *)endt;
1057 }
1058
Willy Tarreau833cc792013-07-24 15:34:19 +02001059 out:
1060 free(fkw);
1061 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001062 return expr;
1063
1064out_error:
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +01001065 release_sample_expr(expr);
Willy Tarreau833cc792013-07-24 15:34:19 +02001066 expr = NULL;
1067 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001068}
1069
1070/*
Willy Tarreau12785782012-04-27 21:37:17 +02001071 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001072 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001073 * Returns a pointer on a typed sample structure containing the result or NULL if
1074 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001075 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001076 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001077 *
1078 * Note: the fetch functions are required to properly set the return type. The
1079 * conversion functions must do so too. However the cast functions do not need
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001080 * to since they're made to cast multiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001081 *
1082 * The caller may indicate in <opt> if it considers the result final or not.
1083 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1084 * if the result is stable or not, according to the following table :
1085 *
1086 * return MAY_CHANGE FINAL Meaning for the sample
1087 * NULL 0 * Not present and will never be (eg: header)
1088 * NULL 1 0 Not present yet, could change (eg: POST param)
1089 * NULL 1 1 Not present yet, will not change anymore
1090 * smp 0 * Present and will not change (eg: header)
1091 * smp 1 0 Present, may change (eg: request length)
1092 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001093 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001094struct sample *sample_process(struct proxy *px, struct session *sess,
1095 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001096 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001097{
Willy Tarreau12785782012-04-27 21:37:17 +02001098 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001099
Willy Tarreau18387e22013-07-25 12:02:38 +02001100 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001101 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001102 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001103 }
Emeric Brun107ca302010-01-04 16:16:05 +01001104
Willy Tarreau1777ea62016-03-10 16:15:46 +01001105 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001106 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001107 return NULL;
1108
Emeric Brun107ca302010-01-04 16:16:05 +01001109 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001110 /* we want to ensure that p->type can be casted into
1111 * conv_expr->conv->in_type. We have 3 possibilities :
1112 * - NULL => not castable.
1113 * - c_none => nothing to do (let's optimize it)
1114 * - other => apply cast and prepare to fail
1115 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001116 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001117 return NULL;
1118
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001119 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1120 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001121 return NULL;
1122
Willy Tarreau12e50112012-04-25 17:21:49 +02001123 /* OK cast succeeded */
1124
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001125 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001126 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001127 }
1128 return p;
1129}
1130
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001131/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001132 * Resolve all remaining arguments in proxy <p>. Returns the number of
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001133 * errors or 0 if everything is fine. If at least one error is met, it will
1134 * be appended to *err. If *err==NULL it will be allocated first.
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001135 */
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001136int smp_resolve_args(struct proxy *p, char **err)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001137{
1138 struct arg_list *cur, *bak;
1139 const char *ctx, *where;
1140 const char *conv_ctx, *conv_pre, *conv_pos;
1141 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001142 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001143 struct arg *arg;
1144 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001145 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001146
1147 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1148 struct proxy *px;
1149 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001150 struct stktable *t;
1151 char *pname, *sname, *stktname;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001152 char *err2;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001153
1154 arg = cur->arg;
1155
1156 /* prepare output messages */
1157 conv_pre = conv_pos = conv_ctx = "";
1158 if (cur->conv) {
1159 conv_ctx = cur->conv;
1160 conv_pre = "conversion keyword '";
1161 conv_pos = "' for ";
1162 }
1163
1164 where = "in";
1165 ctx = "sample fetch keyword";
1166 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001167 case ARGC_STK: where = "in stick rule in"; break;
1168 case ARGC_TRK: where = "in tracking rule in"; break;
1169 case ARGC_LOG: where = "in log-format string in"; break;
1170 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
Willy Tarreau57467b82021-09-02 19:43:20 +02001171 case ARGC_HRQ: where = "in http-request expression in"; break;
1172 case ARGC_HRS: where = "in http-response response in"; break;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001173 case ARGC_UIF: where = "in unique-id-format string in"; break;
1174 case ARGC_RDR: where = "in redirect format string in"; break;
1175 case ARGC_CAP: where = "in capture rule in"; break;
1176 case ARGC_ACL: ctx = "ACL keyword"; break;
1177 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001178 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001179 case ARGC_HERR: where = "in http-error directive in"; break;
Miroslav Zagorac7f8314c2020-12-09 16:31:48 +01001180 case ARGC_OT: where = "in ot-scope directive in"; break;
Willy Tarreau57467b82021-09-02 19:43:20 +02001181 case ARGC_TCO: where = "in tcp-request connection expression in"; break;
1182 case ARGC_TSE: where = "in tcp-request session expression in"; break;
1183 case ARGC_TRQ: where = "in tcp-request content expression in"; break;
1184 case ARGC_TRS: where = "in tcp-response content expression in"; break;
1185 case ARGC_TCK: where = "in tcp-check expression in"; break;
1186 case ARGC_CFG: where = "in configuration expression in"; break;
1187 case ARGC_CLI: where = "in CLI expression in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001188 }
1189
1190 /* set a few default settings */
1191 px = p;
1192 pname = p->id;
1193
1194 switch (arg->type) {
1195 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001196 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001197 memprintf(err, "%sparsing [%s:%d]: missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1198 *err ? *err : "", cur->file, cur->line,
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 continue;
1202 }
1203
1204 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001205 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001206
1207 if (sname) {
1208 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001209 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001210
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001211 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001212 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001213 memprintf(err, "%sparsing [%s:%d]: unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1214 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001215 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001216 cfgerr++;
1217 break;
1218 }
1219 }
1220 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001221 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001222
1223 srv = findserver(px, sname);
1224 if (!srv) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001225 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",
1226 *err ? *err : "", cur->file, cur->line, sname, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001227 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001228 cfgerr++;
1229 break;
1230 }
1231
Amaury Denoyelle06269612021-08-23 14:05:07 +02001232 srv->flags |= SRV_F_NON_PURGEABLE;
1233
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001234 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001235 arg->unresolved = 0;
1236 arg->data.srv = srv;
1237 break;
1238
1239 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001240 if (arg->data.str.data) {
1241 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001242 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001243 }
1244
1245 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001246 memprintf(err, "%sparsing [%s:%d]: unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1247 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001248 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001249 cfgerr++;
1250 break;
1251 }
1252
1253 if (!(px->cap & PR_CAP_FE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001254 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",
1255 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001256 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001257 cfgerr++;
1258 break;
1259 }
1260
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001261 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001262 arg->unresolved = 0;
1263 arg->data.prx = px;
1264 break;
1265
1266 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001267 if (arg->data.str.data) {
1268 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001269 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001270 }
1271
1272 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001273 memprintf(err, "%sparsing [%s:%d]: unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1274 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001275 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001276 cfgerr++;
1277 break;
1278 }
1279
1280 if (!(px->cap & PR_CAP_BE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001281 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",
1282 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001283 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001284 cfgerr++;
1285 break;
1286 }
1287
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001288 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001289 arg->unresolved = 0;
1290 arg->data.prx = px;
1291 break;
1292
1293 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001294 if (arg->data.str.data)
1295 stktname = arg->data.str.area;
1296 else
1297 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001298
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001299 t = stktable_find_by_name(stktname);
1300 if (!t) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001301 memprintf(err, "%sparsing [%s:%d]: unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1302 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001303 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001304 cfgerr++;
1305 break;
1306 }
1307
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001308 if (!t->size) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001309 memprintf(err, "%sparsing [%s:%d]: no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1310 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001311 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001312 cfgerr++;
1313 break;
1314 }
1315
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001316 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001317 p->next_stkt_ref = t->proxies_list;
1318 t->proxies_list = p;
1319 }
1320
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001321 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001322 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001323 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001324 break;
1325
1326 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001327 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001328 memprintf(err, "%sparsing [%s:%d]: missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1329 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001330 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001331 cfgerr++;
1332 break;
1333 }
1334
1335 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001336 strcmp(p->uri_auth->userlist->name, arg->data.str.area) == 0)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001337 ul = p->uri_auth->userlist;
1338 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001339 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001340
1341 if (!ul) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001342 memprintf(err, "%sparsing [%s:%d]: unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1343 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001344 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001345 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001346 cfgerr++;
1347 break;
1348 }
1349
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001350 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001351 arg->unresolved = 0;
1352 arg->data.usr = ul;
1353 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001354
1355 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001356 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001357 memprintf(err, "%sparsing [%s:%d]: missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1358 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001359 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001360 cfgerr++;
1361 continue;
1362 }
1363
Willy Tarreau46947782015-01-19 19:00:58 +01001364 rflags = 0;
1365 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001366 err2 = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001367
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001368 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err2))) {
1369 memprintf(err, "%sparsing [%s:%d]: error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1370 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001371 arg->data.str.area,
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001372 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err2);
Willy Tarreau46947782015-01-19 19:00:58 +01001373 cfgerr++;
1374 continue;
1375 }
1376
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001377 chunk_destroy(&arg->data.str);
Willy Tarreau46947782015-01-19 19:00:58 +01001378 arg->unresolved = 0;
1379 arg->data.reg = reg;
1380 break;
1381
1382
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001383 }
1384
Willy Tarreau2b718102021-04-21 07:32:39 +02001385 LIST_DELETE(&cur->list);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001386 free(cur);
1387 } /* end of args processing */
1388
1389 return cfgerr;
1390}
1391
1392/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001393 * Process a fetch + format conversion as defined by the sample expression
1394 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001395 * not explicitly set to <smp_type>, but shall be compatible with it as
1396 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1397 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001398 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1399 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1400 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1401 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001402 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1403 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1404 * still hope to get it after waiting longer, and is not converted to string.
1405 * The possible output combinations are the following :
1406 *
1407 * return MAY_CHANGE FINAL Meaning for the sample
1408 * NULL * * Not present and will never be (eg: header)
1409 * smp 0 * Final value converted (eg: header)
1410 * smp 1 0 Not present yet, may appear later (eg: header)
1411 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001412 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001413struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001414 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001415 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001416{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001417 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001418
Willy Tarreau6c616e02014-06-25 16:56:41 +02001419 memset(smp, 0, sizeof(*smp));
1420
Willy Tarreau192252e2015-04-04 01:47:55 +02001421 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001422 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1423 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001424 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001425 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001426
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001427 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001428 return NULL;
1429
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001430 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001431 return NULL;
1432
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001433 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001434 return smp;
1435}
1436
Christopher Faulet476e5d02016-10-26 11:34:47 +02001437static void release_sample_arg(struct arg *p)
1438{
1439 struct arg *p_back = p;
1440
1441 if (!p)
1442 return;
1443
1444 while (p->type != ARGT_STOP) {
1445 if (p->type == ARGT_STR || p->unresolved) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001446 chunk_destroy(&p->data.str);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001447 p->unresolved = 0;
1448 }
1449 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001450 regex_free(p->data.reg);
1451 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001452 }
1453 p++;
1454 }
1455
1456 if (p_back != empty_arg_list)
1457 free(p_back);
1458}
1459
1460void release_sample_expr(struct sample_expr *expr)
1461{
1462 struct sample_conv_expr *conv_expr, *conv_exprb;
1463
1464 if (!expr)
1465 return;
1466
Tim Duesterhus867cd982020-07-04 11:49:39 +02001467 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001468 LIST_DELETE(&conv_expr->list);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001469 release_sample_arg(conv_expr->arg_p);
Tim Duesterhus867cd982020-07-04 11:49:39 +02001470 free(conv_expr);
1471 }
1472
Christopher Faulet476e5d02016-10-26 11:34:47 +02001473 release_sample_arg(expr->arg_p);
1474 free(expr);
1475}
1476
Emeric Brun107ca302010-01-04 16:16:05 +01001477/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001478/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001479/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001480/*****************************************************************/
1481
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001482static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1483{
1484 int i;
1485 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001486 struct buffer *buf;
1487 struct sink *sink;
1488 struct ist line;
1489 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001490
Willy Tarreau0851fd52019-12-17 10:07:25 +01001491 buf = alloc_trash_chunk();
1492 if (!buf)
1493 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001494
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001495 sink = (struct sink *)arg_p[1].data.ptr;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001496 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001497
Willy Tarreau0851fd52019-12-17 10:07:25 +01001498 pfx = arg_p[0].data.str.area;
1499 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001500
Willy Tarreau0851fd52019-12-17 10:07:25 +01001501 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1502 if (!sample_casts[smp->data.type][SMP_T_STR])
1503 goto nocast;
1504
1505 /* Copy sample fetch. This puts the sample as const, the
1506 * cast will copy data if a transformation is required.
1507 */
1508 memcpy(&tmp, smp, sizeof(struct sample));
1509 tmp.flags = SMP_F_CONST;
1510
1511 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1512 goto nocast;
1513
1514 /* Display the displayable chars*. */
1515 b_putchr(buf, '<');
1516 for (i = 0; i < tmp.data.u.str.data; i++) {
Willy Tarreau90807112020-02-25 08:16:33 +01001517 if (isprint((unsigned char)tmp.data.u.str.area[i]))
Willy Tarreau0851fd52019-12-17 10:07:25 +01001518 b_putchr(buf, tmp.data.u.str.area[i]);
1519 else
1520 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001521 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001522 b_putchr(buf, '>');
1523
1524 done:
1525 line = ist2(buf->area, buf->data);
Emeric Brun54648852020-07-06 15:54:06 +02001526 sink_write(sink, &line, 1, 0, 0, NULL);
Willy Tarreau0851fd52019-12-17 10:07:25 +01001527 end:
1528 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001529 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001530 nocast:
1531 chunk_appendf(buf, "(undisplayable)");
1532 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001533}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001534
1535// This function checks the "debug" converter's arguments.
1536static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1537 const char *file, int line, char **err)
1538{
1539 const char *name = "buf0";
1540 struct sink *sink = NULL;
1541
1542 if (args[0].type != ARGT_STR) {
1543 /* optional prefix */
1544 args[0].data.str.area = "";
1545 args[0].data.str.data = 0;
1546 }
1547
1548 if (args[1].type == ARGT_STR)
1549 name = args[1].data.str.area;
1550
1551 sink = sink_find(name);
1552 if (!sink) {
1553 memprintf(err, "No such sink '%s'", name);
1554 return 0;
1555 }
1556
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001557 chunk_destroy(&args[1].data.str);
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001558 args[1].type = ARGT_PTR;
1559 args[1].data.ptr = sink;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001560 return 1;
1561}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001562
Holger Just1bfc24b2017-05-06 00:56:53 +02001563static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1564{
Willy Tarreau83061a82018-07-13 11:56:34 +02001565 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001566 int bin_len;
1567
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001568 trash->data = 0;
1569 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1570 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001571 if (bin_len < 0)
1572 return 0;
1573
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001574 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001575 smp->data.u.str = *trash;
1576 smp->data.type = SMP_T_BIN;
1577 smp->flags &= ~SMP_F_CONST;
1578 return 1;
1579}
1580
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +02001581static int sample_conv_base64url2bin(const struct arg *arg_p, struct sample *smp, void *private)
1582{
1583 struct buffer *trash = get_trash_chunk();
1584 int bin_len;
1585
1586 trash->data = 0;
1587 bin_len = base64urldec(smp->data.u.str.area, smp->data.u.str.data,
1588 trash->area, trash->size);
1589 if (bin_len < 0)
1590 return 0;
1591
1592 trash->data = bin_len;
1593 smp->data.u.str = *trash;
1594 smp->data.type = SMP_T_BIN;
1595 smp->flags &= ~SMP_F_CONST;
1596 return 1;
1597}
1598
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001599static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001600{
Willy Tarreau83061a82018-07-13 11:56:34 +02001601 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001602 int b64_len;
1603
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001604 trash->data = 0;
1605 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1606 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001607 if (b64_len < 0)
1608 return 0;
1609
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001610 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001611 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001612 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001613 smp->flags &= ~SMP_F_CONST;
1614 return 1;
1615}
1616
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +02001617static int sample_conv_bin2base64url(const struct arg *arg_p, struct sample *smp, void *private)
1618{
1619 struct buffer *trash = get_trash_chunk();
1620 int b64_len;
1621
1622 trash->data = 0;
1623 b64_len = a2base64url(smp->data.u.str.area, smp->data.u.str.data,
1624 trash->area, trash->size);
1625 if (b64_len < 0)
1626 return 0;
1627
1628 trash->data = b64_len;
1629 smp->data.u.str = *trash;
1630 smp->data.type = SMP_T_STR;
1631 smp->flags &= ~SMP_F_CONST;
1632 return 1;
1633}
1634
Willy Tarreau168e8de2021-10-06 15:29:00 +02001635/* This function returns a sample struct filled with the conversion of variable
1636 * <var> to sample type <type> (SMP_T_*), via a cast to the target type. If the
1637 * variable cannot be retrieved or casted, 0 is returned, otherwise 1.
1638 *
1639 * Keep in mind that the sample content may be written to a pre-allocated
1640 * trash chunk as returned by get_trash_chunk().
1641 */
1642int sample_conv_var2smp(const struct var_desc *var, struct sample *smp, int type)
1643{
1644 if (!vars_get_by_desc(var, smp, NULL))
1645 return 0;
1646 if (!sample_casts[smp->data.type][type])
1647 return 0;
1648 if (!sample_casts[smp->data.type][type](smp))
1649 return 0;
1650 return 1;
1651}
1652
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001653static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1654{
1655 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001656 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001657
1658 memset(&ctx, 0, sizeof(ctx));
1659
1660 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001661 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1662 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001663
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001664 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001665 smp->data.u.str = *trash;
1666 smp->data.type = SMP_T_BIN;
1667 smp->flags &= ~SMP_F_CONST;
1668 return 1;
1669}
1670
Dragan Dosen9e8db132021-02-22 10:03:53 +01001671/* This function returns a sample struct filled with an <arg> content.
1672 * If the <arg> contains a string, it is returned in the sample flagged as
1673 * SMP_F_CONST. If the <arg> contains a variable descriptor, the sample is
1674 * filled with the content of the variable by using vars_get_by_desc().
1675 *
1676 * Keep in mind that the sample content may be written to a pre-allocated
1677 * trash chunk as returned by get_trash_chunk().
1678 *
1679 * This function returns 0 if an error occurs, otherwise it returns 1.
1680 */
Willy Tarreau4034e2c2021-10-06 15:20:18 +02001681int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001682{
1683 switch (arg->type) {
1684 case ARGT_STR:
1685 smp->data.type = SMP_T_STR;
1686 smp->data.u.str = arg->data.str;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001687 smp->flags = SMP_F_CONST;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001688 return 1;
1689 case ARGT_VAR:
Willy Tarreau2476ff12021-10-06 15:30:52 +02001690 return sample_conv_var2smp(&arg->data.var, smp, SMP_T_STR);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001691 default:
1692 return 0;
1693 }
1694}
1695
Marcin Deranek40ca09c2021-07-13 14:05:24 +02001696static int sample_conv_be2dec_check(struct arg *args, struct sample_conv *conv,
1697 const char *file, int line, char **err)
1698{
1699 if (args[1].data.sint <= 0 || args[1].data.sint > sizeof(unsigned long long)) {
Willy Tarreauc5d0fc92021-09-15 10:30:40 +02001700 memprintf(err, "chunk_size out of [1..%u] range (%lld)", (uint)sizeof(unsigned long long), args[1].data.sint);
Marcin Deranek40ca09c2021-07-13 14:05:24 +02001701 return 0;
1702 }
1703
1704 if (args[2].data.sint != 0 && args[2].data.sint != 1) {
1705 memprintf(err, "Unsupported truncate value (%lld)", args[2].data.sint);
1706 return 0;
1707 }
1708
1709 return 1;
1710}
1711
1712/* Converts big-endian binary input sample to a string containing an unsigned
1713 * integer number per <chunk_size> input bytes separated with <separator>.
1714 * Optional <truncate> flag indicates if input is truncated at <chunk_size>
1715 * boundaries.
1716 * Arguments: separator (string), chunk_size (integer), truncate (0,1)
1717 */
1718static int sample_conv_be2dec(const struct arg *args, struct sample *smp, void *private)
1719{
1720 struct buffer *trash = get_trash_chunk();
1721 const int last = args[2].data.sint ? smp->data.u.str.data - args[1].data.sint + 1 : smp->data.u.str.data;
1722 int max_size = trash->size - 2;
1723 int i;
1724 int start;
1725 int ptr = 0;
1726 unsigned long long number;
1727 char *pos;
1728
1729 trash->data = 0;
1730
1731 while (ptr < last && trash->data <= max_size) {
1732 start = trash->data;
1733 if (ptr) {
1734 /* Add separator */
1735 memcpy(trash->area + trash->data, args[0].data.str.area, args[0].data.str.data);
1736 trash->data += args[0].data.str.data;
1737 }
1738 else
1739 max_size -= args[0].data.str.data;
1740
1741 /* Add integer */
1742 for (number = 0, i = 0; i < args[1].data.sint && ptr < smp->data.u.str.data; i++)
1743 number = (number << 8) + (unsigned char)smp->data.u.str.area[ptr++];
1744
1745 pos = ulltoa(number, trash->area + trash->data, trash->size - trash->data);
1746 if (pos)
1747 trash->data = pos - trash->area;
1748 else {
1749 trash->data = start;
1750 break;
1751 }
1752 }
1753
1754 smp->data.u.str = *trash;
1755 smp->data.type = SMP_T_STR;
1756 smp->flags &= ~SMP_F_CONST;
1757 return 1;
1758}
1759
Marcin Deranekda0264a2021-07-13 14:08:56 +02001760static int sample_conv_be2hex_check(struct arg *args, struct sample_conv *conv,
1761 const char *file, int line, char **err)
1762{
1763 if (args[1].data.sint <= 0 && (args[0].data.str.data > 0 || args[2].data.sint != 0)) {
1764 memprintf(err, "chunk_size needs to be positive (%lld)", args[1].data.sint);
1765 return 0;
1766 }
1767
1768 if (args[2].data.sint != 0 && args[2].data.sint != 1) {
1769 memprintf(err, "Unsupported truncate value (%lld)", args[2].data.sint);
1770 return 0;
1771 }
1772
1773 return 1;
1774}
1775
1776/* Converts big-endian binary input sample to a hex string containing two hex
1777 * digits per input byte. <separator> is put every <chunk_size> binary input
1778 * bytes if specified. Optional <truncate> flag indicates if input is truncated
1779 * at <chunk_size> boundaries.
1780 * Arguments: separator (string), chunk_size (integer), truncate (0,1)
1781 */
1782static int sample_conv_be2hex(const struct arg *args, struct sample *smp, void *private)
1783{
1784 struct buffer *trash = get_trash_chunk();
1785 int chunk_size = args[1].data.sint;
1786 const int last = args[2].data.sint ? smp->data.u.str.data - chunk_size + 1 : smp->data.u.str.data;
1787 int i;
1788 int max_size;
1789 int ptr = 0;
1790 unsigned char c;
1791
1792 trash->data = 0;
1793 if (args[0].data.str.data == 0 && args[2].data.sint == 0)
1794 chunk_size = smp->data.u.str.data;
1795 max_size = trash->size - 2 * chunk_size;
1796
1797 while (ptr < last && trash->data <= max_size) {
1798 if (ptr) {
1799 /* Add separator */
1800 memcpy(trash->area + trash->data, args[0].data.str.area, args[0].data.str.data);
1801 trash->data += args[0].data.str.data;
1802 }
1803 else
1804 max_size -= args[0].data.str.data;
1805
1806 /* Add hex */
1807 for (i = 0; i < chunk_size && ptr < smp->data.u.str.data; i++) {
1808 c = smp->data.u.str.area[ptr++];
1809 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1810 trash->area[trash->data++] = hextab[c & 0xF];
1811 }
1812 }
1813
1814 smp->data.u.str = *trash;
1815 smp->data.type = SMP_T_STR;
1816 smp->flags &= ~SMP_F_CONST;
1817 return 1;
1818}
1819
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001820static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001821{
Willy Tarreau83061a82018-07-13 11:56:34 +02001822 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001823 unsigned char c;
1824 int ptr = 0;
1825
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001826 trash->data = 0;
1827 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
1828 c = smp->data.u.str.area[ptr++];
1829 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1830 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001831 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001832 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001833 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001834 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001835 return 1;
1836}
1837
Dragan Dosen3f957b22017-10-24 09:27:34 +02001838static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1839{
1840 long long int n = 0;
1841 int i, c;
1842
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001843 for (i = 0; i < smp->data.u.str.data; i++) {
1844 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02001845 return 0;
1846 n = (n << 4) + c;
1847 }
1848
1849 smp->data.u.sint = n;
1850 smp->data.type = SMP_T_SINT;
1851 smp->flags &= ~SMP_F_CONST;
1852 return 1;
1853}
1854
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001855/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001856static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001857{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001858 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
1859 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01001860 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001861 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001862 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001863 return 1;
1864}
1865
Willy Tarreau60a2ee72017-12-15 07:13:48 +01001866static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001867{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001868 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001869 smp->data.u.sint = i;
1870 smp->data.type = SMP_T_SINT;
1871 return 1;
1872}
1873
1874
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001875static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001876{
1877 int i;
1878
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001879 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001880 return 0;
1881
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001882 for (i = 0; i < smp->data.u.str.data; i++) {
1883 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
1884 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001885 }
1886 return 1;
1887}
1888
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001889static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001890{
1891 int i;
1892
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001893 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001894 return 0;
1895
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001896 for (i = 0; i < smp->data.u.str.data; i++) {
1897 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
1898 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001899 }
1900 return 1;
1901}
1902
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001903/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
1904static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001905{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001906 /* Attempt to convert to IPv4 to apply the correct mask. */
1907 c_ipv62ip(smp);
1908
1909 if (smp->data.type == SMP_T_IPV4) {
1910 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
1911 smp->data.type = SMP_T_IPV4;
1912 }
1913 else if (smp->data.type == SMP_T_IPV6) {
1914 /* IPv6 cannot be converted without an IPv6 mask. */
1915 if (args[1].type != ARGT_IPV6)
1916 return 0;
1917
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +01001918 write_u64(&smp->data.u.ipv6.s6_addr[0],
1919 read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0]));
1920 write_u64(&smp->data.u.ipv6.s6_addr[8],
1921 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 +01001922 smp->data.type = SMP_T_IPV6;
1923 }
1924
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001925 return 1;
1926}
1927
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001928/* takes an UINT value on input supposed to represent the time since EPOCH,
1929 * adds an optional offset found in args[1] and emits a string representing
1930 * the local time in the format specified in args[1] using strftime().
1931 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001932static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001933{
Willy Tarreau83061a82018-07-13 11:56:34 +02001934 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001935 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001936 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Tim Duesterhus1f269c12021-08-28 23:57:01 +02001937 struct tm tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001938
1939 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001940 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001941 curr_date += args[1].data.sint;
1942
Tim Duesterhus1f269c12021-08-28 23:57:01 +02001943 get_localtime(curr_date, &tm);
1944
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001945 temp = get_trash_chunk();
Tim Duesterhus1f269c12021-08-28 23:57:01 +02001946 temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001947 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001948 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001949 return 1;
1950}
1951
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001952/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001953static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001954{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001955 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
1956 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01001957 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001958 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001959 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001960 return 1;
1961}
1962
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001963/* takes an UINT value on input supposed to represent the time since EPOCH,
1964 * adds an optional offset found in args[1] and emits a string representing
1965 * the UTC date in the format specified in args[1] using strftime().
1966 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001967static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001968{
Willy Tarreau83061a82018-07-13 11:56:34 +02001969 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001970 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001971 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Tim Duesterhus1f269c12021-08-28 23:57:01 +02001972 struct tm tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001973
1974 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001975 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001976 curr_date += args[1].data.sint;
1977
Tim Duesterhus1f269c12021-08-28 23:57:01 +02001978 get_gmtime(curr_date, &tm);
1979
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001980 temp = get_trash_chunk();
Tim Duesterhus1f269c12021-08-28 23:57:01 +02001981 temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001982 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001983 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001984 return 1;
1985}
1986
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001987/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001988static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001989{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001990 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
1991 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01001992 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001993 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001994 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001995 return 1;
1996}
1997
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001998/* hashes the binary input into a 32-bit unsigned int using xxh.
1999 * The seed of the hash defaults to 0 but can be changd in argument 1.
2000 */
2001static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
2002{
2003 unsigned int seed;
2004
Christopher Faulete6e7a582021-01-29 11:29:28 +01002005 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002006 seed = arg_p->data.sint;
2007 else
2008 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002009 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
2010 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002011 smp->data.type = SMP_T_SINT;
2012 return 1;
2013}
2014
2015/* hashes the binary input into a 64-bit unsigned int using xxh.
2016 * In fact, the function returns a 64 bit unsigned, but the sample
2017 * storage of haproxy only proposes 64-bits signed, so the value is
2018 * cast as signed. This cast doesn't impact the hash repartition.
2019 * The seed of the hash defaults to 0 but can be changd in argument 1.
2020 */
2021static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
2022{
2023 unsigned long long int seed;
2024
Christopher Faulete6e7a582021-01-29 11:29:28 +01002025 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002026 seed = (unsigned long long int)arg_p->data.sint;
2027 else
2028 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002029 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
2030 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002031 smp->data.type = SMP_T_SINT;
2032 return 1;
2033}
2034
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002035static int sample_conv_xxh3(const struct arg *arg_p, struct sample *smp, void *private)
2036{
2037 unsigned long long int seed;
2038
Christopher Faulete6e7a582021-01-29 11:29:28 +01002039 if (arg_p->data.sint)
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002040 seed = (unsigned long long int)arg_p->data.sint;
2041 else
2042 seed = 0;
2043 smp->data.u.sint = (long long int)XXH3(smp->data.u.str.area,
2044 smp->data.u.str.data, seed);
2045 smp->data.type = SMP_T_SINT;
2046 return 1;
2047}
2048
Willy Tarreau80599772015-01-20 19:35:24 +01002049/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002050static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01002051{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002052 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
2053 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002054 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002055 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002056 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01002057 return 1;
2058}
2059
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002060/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
2061static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
2062{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002063 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
2064 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002065 if (arg_p->data.sint)
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002066 smp->data.u.sint = full_hash(smp->data.u.sint);
2067 smp->data.type = SMP_T_SINT;
2068 return 1;
2069}
2070
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002071/* This function escape special json characters. The returned string can be
2072 * safely set between two '"' and used as json string. The json string is
2073 * defined like this:
2074 *
2075 * any Unicode character except '"' or '\' or control character
2076 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
2077 *
2078 * The enum input_type contain all the allowed mode for decoding the input
2079 * string.
2080 */
2081enum input_type {
2082 IT_ASCII = 0,
2083 IT_UTF8,
2084 IT_UTF8S,
2085 IT_UTF8P,
2086 IT_UTF8PS,
2087};
William Dauchy5417e892021-01-08 21:57:41 +01002088
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002089static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
2090 const char *file, int line, char **err)
2091{
Christopher Faulet95917132020-08-05 23:07:07 +02002092 enum input_type type;
2093
Christopher Faulet95917132020-08-05 23:07:07 +02002094 if (strcmp(arg->data.str.area, "") == 0)
2095 type = IT_ASCII;
2096 else if (strcmp(arg->data.str.area, "ascii") == 0)
2097 type = IT_ASCII;
2098 else if (strcmp(arg->data.str.area, "utf8") == 0)
2099 type = IT_UTF8;
2100 else if (strcmp(arg->data.str.area, "utf8s") == 0)
2101 type = IT_UTF8S;
2102 else if (strcmp(arg->data.str.area, "utf8p") == 0)
2103 type = IT_UTF8P;
2104 else if (strcmp(arg->data.str.area, "utf8ps") == 0)
2105 type = IT_UTF8PS;
2106 else {
2107 memprintf(err, "Unexpected input code type. "
2108 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
2109 return 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002110 }
2111
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002112 chunk_destroy(&arg->data.str);
Christopher Faulet95917132020-08-05 23:07:07 +02002113 arg->type = ARGT_SINT;
2114 arg->data.sint = type;
2115 return 1;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002116}
2117
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002118static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002119{
Willy Tarreau83061a82018-07-13 11:56:34 +02002120 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002121 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
2122 const char *str;
2123 int len;
2124 enum input_type input_type = IT_ASCII;
2125 unsigned int c;
2126 unsigned int ret;
2127 char *p;
2128
Christopher Faulete6e7a582021-01-29 11:29:28 +01002129 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002130
2131 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002132 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002133
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002134 p = smp->data.u.str.area;
2135 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002136
2137 if (input_type == IT_ASCII) {
2138 /* Read input as ASCII. */
2139 c = *(unsigned char *)p;
2140 p++;
2141 }
2142 else {
2143 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002144 ret = utf8_next(p,
2145 smp->data.u.str.data - ( p - smp->data.u.str.area),
2146 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002147 p += utf8_return_length(ret);
2148
2149 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2150 return 0;
2151 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2152 continue;
2153 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2154 return 0;
2155 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2156 continue;
2157
2158 /* Check too big values. */
2159 if ((unsigned int)c > 0xffff) {
2160 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2161 return 0;
2162 continue;
2163 }
2164 }
2165
2166 /* Convert character. */
2167 if (c == '"') {
2168 len = 2;
2169 str = "\\\"";
2170 }
2171 else if (c == '\\') {
2172 len = 2;
2173 str = "\\\\";
2174 }
2175 else if (c == '/') {
2176 len = 2;
2177 str = "\\/";
2178 }
2179 else if (c == '\b') {
2180 len = 2;
2181 str = "\\b";
2182 }
2183 else if (c == '\f') {
2184 len = 2;
2185 str = "\\f";
2186 }
2187 else if (c == '\r') {
2188 len = 2;
2189 str = "\\r";
2190 }
2191 else if (c == '\n') {
2192 len = 2;
2193 str = "\\n";
2194 }
2195 else if (c == '\t') {
2196 len = 2;
2197 str = "\\t";
2198 }
Willy Tarreau90807112020-02-25 08:16:33 +01002199 else if (c > 0xff || !isprint((unsigned char)c)) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002200 /* isprint generate a segfault if c is too big. The man says that
2201 * c must have the value of an unsigned char or EOF.
2202 */
2203 len = 6;
2204 _str[0] = '\\';
2205 _str[1] = 'u';
2206 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2207 str = _str;
2208 }
2209 else {
2210 len = 1;
Willy Tarreau5715da22020-02-25 08:37:37 +01002211 _str[0] = c;
2212 str = _str;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002213 }
2214
2215 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002216 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002217 return 0;
2218
2219 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002220 memcpy(temp->area + temp->data, str, len);
2221 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002222 }
2223
2224 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002225 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002226 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002227
2228 return 1;
2229}
2230
Emeric Brun54c4ac82014-11-03 15:32:43 +01002231/* This sample function is designed to extract some bytes from an input buffer.
2232 * First arg is the offset.
2233 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002234static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002235{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002236 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2237 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002238 return 1;
2239 }
2240
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002241 if (smp->data.u.str.size)
2242 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002243 smp->data.u.str.data -= arg_p[0].data.sint;
2244 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002245
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002246 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2247 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002248
2249 return 1;
2250}
2251
Emeric Brunf399b0d2014-11-03 17:07:03 +01002252static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2253 const char *file, int line, char **err)
2254{
2255 struct arg *arg = args;
2256
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002257 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002258 memprintf(err, "Unexpected arg type");
2259 return 0;
2260 }
2261
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002262 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002263 memprintf(err, "Unexpected value 0 for index");
2264 return 0;
2265 }
2266
2267 arg++;
2268
2269 if (arg->type != ARGT_STR) {
2270 memprintf(err, "Unexpected arg type");
2271 return 0;
2272 }
2273
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002274 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002275 memprintf(err, "Empty separators list");
2276 return 0;
2277 }
2278
2279 return 1;
2280}
2281
2282/* This sample function is designed to a return selected part of a string (field).
2283 * First arg is the index of the field (start at 1)
2284 * Second arg is a char list of separators (type string)
2285 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002286static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002287{
Marcin Deranek9631a282018-04-16 14:30:46 +02002288 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002289 char *start, *end;
2290 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002291 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002292
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002293 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002294 return 0;
2295
Marcin Deranek9631a282018-04-16 14:30:46 +02002296 if (arg_p[0].data.sint < 0) {
2297 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002298 end = start = smp->data.u.str.area + smp->data.u.str.data;
2299 while (start > smp->data.u.str.area) {
2300 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2301 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002302 if (field == arg_p[0].data.sint) {
2303 if (count == 1)
2304 goto found;
2305 else if (count > 1)
2306 count--;
2307 } else {
2308 end = start-1;
2309 field--;
2310 }
2311 break;
2312 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002313 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002314 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002315 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002316 } else {
2317 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002318 end = start = smp->data.u.str.area;
2319 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2320 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2321 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002322 if (field == arg_p[0].data.sint) {
2323 if (count == 1)
2324 goto found;
2325 else if (count > 1)
2326 count--;
2327 } else {
2328 start = end+1;
2329 field++;
2330 }
2331 break;
2332 }
2333 }
2334 end++;
2335 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002336 }
2337
2338 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002339 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002340 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002341 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002342 }
2343found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002344 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002345 /* If ret string is len 0, no need to
2346 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002347 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002348 return 1;
2349
Emeric Brunf399b0d2014-11-03 17:07:03 +01002350 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002351 Note: smp->data.u.str.size cannot be set to 0 */
2352 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002353 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002354
Thayne McCombsb2843052021-04-11 23:26:59 -06002355 smp->data.u.str.area = start;
2356
Emeric Brunf399b0d2014-11-03 17:07:03 +01002357 return 1;
2358}
2359
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002360/* This sample function is designed to return a word from a string.
2361 * First arg is the index of the word (start at 1)
2362 * Second arg is a char list of words separators (type string)
2363 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002364static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002365{
Marcin Deranek9631a282018-04-16 14:30:46 +02002366 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002367 char *start, *end;
2368 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002369 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002370
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002371 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002372 return 0;
2373
2374 word = 0;
2375 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002376 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002377 end = start = smp->data.u.str.area + smp->data.u.str.data;
2378 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002379 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002380 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2381 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002382 issep = 1;
2383 break;
2384 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002385 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002386 if (!inword) {
2387 if (!issep) {
2388 if (word != arg_p[0].data.sint) {
2389 word--;
2390 end = start;
2391 }
2392 inword = 1;
2393 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002394 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002395 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002396 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002397 if (count == 1)
2398 goto found;
2399 else if (count > 1)
2400 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002401 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002402 inword = 0;
2403 }
2404 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002405 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002406 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002407 end = start = smp->data.u.str.area;
2408 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002409 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002410 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2411 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002412 issep = 1;
2413 break;
2414 }
2415 }
2416 if (!inword) {
2417 if (!issep) {
2418 if (word != arg_p[0].data.sint) {
2419 word++;
2420 start = end;
2421 }
2422 inword = 1;
2423 }
2424 }
2425 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002426 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002427 if (count == 1)
2428 goto found;
2429 else if (count > 1)
2430 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002431 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002432 inword = 0;
2433 }
2434 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002435 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002436 }
2437
2438 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002439 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002440 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002441 return 1;
2442 }
2443found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002444 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002445 /* If ret string is len 0, no need to
2446 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002447 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002448 return 1;
2449
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002450 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002451
2452 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002453 Note: smp->data.u.str.size cannot be set to 0 */
2454 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002455 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002456
2457 return 1;
2458}
2459
Willy Tarreau7eda8492015-01-20 19:47:06 +01002460static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2461 const char *file, int line, char **err)
2462{
2463 struct arg *arg = args;
2464 char *p;
2465 int len;
2466
2467 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2468 arg[0].type_flags = 0;
2469
2470 if (arg[2].type != ARGT_STR)
2471 return 1;
2472
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002473 p = arg[2].data.str.area;
2474 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002475 while (len) {
2476 if (*p == 'i') {
2477 arg[0].type_flags |= ARGF_REG_ICASE;
2478 }
2479 else if (*p == 'g') {
2480 arg[0].type_flags |= ARGF_REG_GLOB;
2481 }
2482 else {
2483 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2484 return 0;
2485 }
2486 p++;
2487 len--;
2488 }
2489 return 1;
2490}
2491
2492/* This sample function is designed to do the equivalent of s/match/replace/ on
2493 * the input string. It applies a regex and restarts from the last matched
2494 * location until nothing matches anymore. First arg is the regex to apply to
2495 * the input string, second arg is the replacement expression.
2496 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002497static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002498{
2499 char *start, *end;
2500 struct my_regex *reg = arg_p[0].data.reg;
2501 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002502 struct buffer *trash = get_trash_chunk();
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002503 struct buffer *output;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002504 int flag, max;
2505 int found;
2506
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002507 start = smp->data.u.str.area;
2508 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002509
2510 flag = 0;
2511 while (1) {
2512 /* check for last round which is used to copy remaining parts
2513 * when not running in global replacement mode.
2514 */
2515 found = 0;
2516 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2517 /* Note: we can have start == end on empty strings or at the end */
2518 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2519 }
2520
2521 if (!found)
2522 pmatch[0].rm_so = end - start;
2523
2524 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002525 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002526 if (max && pmatch[0].rm_so > 0) {
2527 if (max > pmatch[0].rm_so)
2528 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002529 memcpy(trash->area + trash->data, start, max);
2530 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002531 }
2532
2533 if (!found)
2534 break;
2535
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002536 output = alloc_trash_chunk();
Willy Tarreau23997da2020-02-18 14:27:44 +01002537 if (!output)
2538 break;
2539
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002540 output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2541
Willy Tarreau7eda8492015-01-20 19:47:06 +01002542 /* replace the matching part */
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002543 max = output->size - output->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002544 if (max) {
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002545 if (max > output->data)
2546 max = output->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002547 memcpy(trash->area + trash->data,
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002548 output->area, max);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002549 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002550 }
2551
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002552 free_trash_chunk(output);
2553
Willy Tarreau7eda8492015-01-20 19:47:06 +01002554 /* stop here if we're done with this string */
2555 if (start >= end)
2556 break;
2557
2558 /* We have a special case for matches of length 0 (eg: "x*y*").
2559 * These ones are considered to match in front of a character,
2560 * so we have to copy that character and skip to the next one.
2561 */
2562 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002563 if (trash->data < trash->size)
2564 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002565 pmatch[0].rm_eo++;
2566 }
2567
2568 start += pmatch[0].rm_eo;
2569 flag |= REG_NOTBOL;
2570 }
2571
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002572 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002573 return 1;
2574}
2575
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002576/* This function check an operator entry. It expects a string.
2577 * The string can be an integer or a variable name.
2578 */
2579static int check_operator(struct arg *args, struct sample_conv *conv,
2580 const char *file, int line, char **err)
2581{
2582 const char *str;
2583 const char *end;
Christopher Faulet95917132020-08-05 23:07:07 +02002584 long long int i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002585
2586 /* Try to decode a variable. */
2587 if (vars_check_arg(&args[0], NULL))
2588 return 1;
2589
2590 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002591 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002592 end = str + strlen(str);
Christopher Faulet95917132020-08-05 23:07:07 +02002593 i = read_int64(&str, end);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002594 if (*str != '\0') {
2595 memprintf(err, "expects an integer or a variable name");
2596 return 0;
2597 }
Christopher Faulet95917132020-08-05 23:07:07 +02002598
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002599 chunk_destroy(&args[0].data.str);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002600 args[0].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02002601 args[0].data.sint = i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002602 return 1;
2603}
2604
Willy Tarreau6204cd92016-03-10 16:33:04 +01002605/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002606 * If the arg contain an integer, the integer is returned in the
2607 * sample. If the arg contains a variable descriptor, it returns the
2608 * variable value.
2609 *
2610 * This function returns 0 if an error occurs, otherwise it returns 1.
2611 */
Willy Tarreaud9be5992021-10-06 15:19:05 +02002612int sample_conv_var2smp_sint(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002613{
2614 switch (arg->type) {
2615 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002616 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002617 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002618 return 1;
2619 case ARGT_VAR:
Willy Tarreau2476ff12021-10-06 15:30:52 +02002620 return sample_conv_var2smp(&arg->data.var, smp, SMP_T_SINT);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002621 default:
2622 return 0;
2623 }
2624}
2625
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002626/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002627 * result.
2628 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002629static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002630{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002631 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002632 return 1;
2633}
2634
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002635/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002636 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002637 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002638static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002639{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002640 struct sample tmp;
2641
Willy Tarreau7560dd42016-03-10 16:28:58 +01002642 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002643 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002644 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002645 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002646 return 1;
2647}
2648
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002649/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002650 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002651 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002652static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002653{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002654 struct sample tmp;
2655
Willy Tarreau7560dd42016-03-10 16:28:58 +01002656 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002657 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002658 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002659 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002660 return 1;
2661}
2662
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002663/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002664 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002665 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002666static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002667{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002668 struct sample tmp;
2669
Willy Tarreau7560dd42016-03-10 16:28:58 +01002670 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002671 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002672 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002673 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002674 return 1;
2675}
2676
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002677static inline long long int arith_add(long long int a, long long int b)
2678{
2679 /* Prevent overflow and makes capped calculus.
2680 * We must ensure that the check calculus doesn't
2681 * exceed the signed 64 bits limits.
2682 *
2683 * +----------+----------+
2684 * | a<0 | a>=0 |
2685 * +------+----------+----------+
2686 * | b<0 | MIN-a>b | no check |
2687 * +------+----------+----------+
2688 * | b>=0 | no check | MAX-a<b |
2689 * +------+----------+----------+
2690 */
2691 if ((a ^ b) >= 0) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002692 /* signs are different. */
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002693 if (a < 0) {
2694 if (LLONG_MIN - a > b)
2695 return LLONG_MIN;
2696 }
2697 if (LLONG_MAX - a < b)
2698 return LLONG_MAX;
2699 }
2700 return a + b;
2701}
2702
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002703/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002704 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002705 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002706static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002707{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002708 struct sample tmp;
2709
Willy Tarreau7560dd42016-03-10 16:28:58 +01002710 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002711 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002712 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002713 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002714 return 1;
2715}
2716
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002717/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002718 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002719 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002720static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002721 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002722{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002723 struct sample tmp;
2724
Willy Tarreau7560dd42016-03-10 16:28:58 +01002725 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002726 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002727 return 0;
2728
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002729 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2730 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2731 * of -LLONG_MIN and correct the result.
2732 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002733 if (tmp.data.u.sint == LLONG_MIN) {
2734 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2735 if (smp->data.u.sint < LLONG_MAX)
2736 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002737 return 1;
2738 }
2739
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002740 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002741 * the second operand.
2742 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002743 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002744 return 1;
2745}
2746
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002747/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002748 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002749 * If the result makes an overflow, then the largest possible quantity is
2750 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002751 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002752static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002753 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002754{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002755 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002756 long long int c;
2757
Willy Tarreau7560dd42016-03-10 16:28:58 +01002758 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002759 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002760 return 0;
2761
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002762 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002763 if (!smp->data.u.sint || !tmp.data.u.sint) {
2764 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002765 return 1;
2766 }
2767
2768 /* The multiply between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002769 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002770 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002771 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2772 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002773 return 1;
2774 }
2775
2776 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002777 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002778
2779 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002780 if (smp->data.u.sint != c / tmp.data.u.sint) {
2781 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2782 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002783 return 1;
2784 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002785 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002786 return 1;
2787 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002788 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002789 return 1;
2790}
2791
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002792/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002793 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002794 * If arg_p makes the result overflow, then the largest possible quantity is
2795 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002796 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002797static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002798 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002799{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002800 struct sample tmp;
2801
Willy Tarreau7560dd42016-03-10 16:28:58 +01002802 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002803 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002804 return 0;
2805
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002806 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002807 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002808 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002809 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002810 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2811 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002812 return 1;
2813 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002814 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002815 return 1;
2816 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002817 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002818 return 1;
2819}
2820
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002821/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002822 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002823 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002824 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002825static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002826 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002827{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002828 struct sample tmp;
2829
Willy Tarreau7560dd42016-03-10 16:28:58 +01002830 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002831 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002832 return 0;
2833
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002834 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002835 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002836 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002837 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002838 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2839 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002840 return 1;
2841 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002842 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002843 return 1;
2844 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002845 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002846 return 1;
2847}
2848
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002849/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002850 * result.
2851 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002852static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002853 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002854{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002855 if (smp->data.u.sint == LLONG_MIN)
2856 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002857 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002858 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002859 return 1;
2860}
2861
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002862/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002863 * false. The output is a BOOL.
2864 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002865static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002866 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002867{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002868 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002869 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002870 return 1;
2871}
2872
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002873/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002874 * truee. The output is a BOOL.
2875 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002876static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002877 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002878{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002879 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002880 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002881 return 1;
2882}
2883
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002884/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002885 * The output is a BOOL.
2886 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002887static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002888 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002889{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002890 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002891 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002892 return 1;
2893}
2894
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002895/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002896 * The output is a BOOL.
2897 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002898static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002899 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002900{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002901 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002902 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002903 return 1;
2904}
2905
Willy Tarreau280f42b2018-02-19 15:34:12 +01002906/* appends an optional const string, an optional variable contents and another
2907 * optional const string to an existing string.
2908 */
2909static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
2910{
Willy Tarreau83061a82018-07-13 11:56:34 +02002911 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002912 struct sample tmp;
2913 int max;
2914
Willy Tarreau591fc3a2021-01-08 16:08:43 +01002915 trash = alloc_trash_chunk();
William Dauchye9970102021-01-11 11:05:58 +01002916 if (!trash)
2917 return 0;
2918
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002919 trash->data = smp->data.u.str.data;
2920 if (trash->data > trash->size - 1)
2921 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002922
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002923 memcpy(trash->area, smp->data.u.str.area, trash->data);
2924 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002925
2926 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002927 max = arg_p[0].data.str.data;
2928 if (max > trash->size - 1 - trash->data)
2929 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002930
2931 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002932 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
2933 trash->data += max;
2934 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002935 }
2936
2937 /* append second string (variable) if it's found and we can turn it
2938 * into a string.
2939 */
2940 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaue352b9d2021-09-03 11:52:38 +02002941 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp, NULL) &&
Willy Tarreau280f42b2018-02-19 15:34:12 +01002942 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
2943 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
2944
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002945 max = tmp.data.u.str.data;
2946 if (max > trash->size - 1 - trash->data)
2947 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002948
2949 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002950 memcpy(trash->area + trash->data, tmp.data.u.str.area,
2951 max);
2952 trash->data += max;
2953 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002954 }
2955 }
2956
2957 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002958 max = arg_p[2].data.str.data;
2959 if (max > trash->size - 1 - trash->data)
2960 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002961
2962 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002963 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
2964 trash->data += max;
2965 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002966 }
2967
2968 smp->data.u.str = *trash;
2969 smp->data.type = SMP_T_STR;
Willy Tarreau591fc3a2021-01-08 16:08:43 +01002970 smp_dup(smp);
2971 free_trash_chunk(trash);
Willy Tarreau280f42b2018-02-19 15:34:12 +01002972 return 1;
2973}
2974
2975/* This function checks the "concat" converter's arguments and extracts the
2976 * variable name and its scope.
2977 */
2978static int smp_check_concat(struct arg *args, struct sample_conv *conv,
2979 const char *file, int line, char **err)
2980{
2981 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002982 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
2983 memprintf(err, "failed to register variable name '%s'",
2984 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01002985 return 0;
2986 }
2987 return 1;
2988}
2989
Tim Duesterhusf38175c2020-06-09 11:48:42 +02002990/* Compares string with a variable containing a string. Return value
Tim Duesterhusca097c12018-04-27 21:18:45 +02002991 * is compatible with strcmp(3)'s return value.
2992 */
2993static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
2994{
2995 struct sample tmp;
2996 int max, result;
2997
2998 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2999 if (arg_p[0].type != ARGT_VAR)
3000 return 0;
Willy Tarreau2476ff12021-10-06 15:30:52 +02003001
3002 if (!sample_conv_var2smp(&arg_p[0].data.var, &tmp, SMP_T_STR))
Tim Duesterhusca097c12018-04-27 21:18:45 +02003003 return 0;
3004
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003005 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
3006 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003007 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003008 if (smp->data.u.str.data != tmp.data.u.str.data) {
3009 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02003010 result = -1;
3011 }
3012 else {
3013 result = 1;
3014 }
3015 }
3016 }
3017
3018 smp->data.u.sint = result;
3019 smp->data.type = SMP_T_SINT;
3020 return 1;
3021}
3022
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02003023/* Takes a boolean as input. Returns the first argument if that boolean is true and
3024 * the second argument otherwise.
3025 */
3026static int sample_conv_iif(const struct arg *arg_p, struct sample *smp, void *private)
3027{
3028 smp->data.type = SMP_T_STR;
3029 smp->flags |= SMP_F_CONST;
3030
3031 if (smp->data.u.sint) {
3032 smp->data.u.str.data = arg_p[0].data.str.data;
3033 smp->data.u.str.area = arg_p[0].data.str.area;
3034 }
3035 else {
3036 smp->data.u.str.data = arg_p[1].data.str.data;
3037 smp->data.u.str.area = arg_p[1].data.str.area;
3038 }
3039
3040 return 1;
3041}
3042
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003043#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
3044#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
3045#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
3046
3047/*
3048 * Extract the field value of an input binary sample. Takes a mandatory argument:
3049 * the protocol buffers field identifier (dotted notation) internally represented
3050 * as an array of unsigned integers and its size.
3051 * Return 1 if the field was found, 0 if not.
3052 */
3053static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
3054{
3055 unsigned char *pos;
3056 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003057
3058 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003059 grpc_left = smp->data.u.str.data;
3060
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01003061 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003062 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003063
3064 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
3065
3066 pos += GRPC_MSG_HEADER_SZ;
3067 grpc_left -= GRPC_MSG_HEADER_SZ;
3068
3069 if (grpc_left < left)
3070 return 0;
3071
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01003072 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
3073 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003074
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003075 grpc_left -= grpc_msg_len;
3076 }
3077
3078 return 0;
3079}
3080
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003081static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
3082{
3083 unsigned char *pos;
3084 size_t left;
3085
3086 pos = (unsigned char *)smp->data.u.str.area;
3087 left = smp->data.u.str.data;
3088
3089 return protobuf_field_lookup(arg_p, smp, &pos, &left);
3090}
3091
3092static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
3093 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003094{
3095 if (!args[1].type) {
3096 args[1].type = ARGT_SINT;
3097 args[1].data.sint = PBUF_T_BINARY;
3098 }
3099 else {
3100 int pbuf_type;
3101
3102 pbuf_type = protobuf_type(args[1].data.str.area);
3103 if (pbuf_type == -1) {
3104 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
3105 return 0;
3106 }
3107
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003108 chunk_destroy(&args[1].data.str);
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003109 args[1].type = ARGT_SINT;
3110 args[1].data.sint = pbuf_type;
3111 }
3112
3113 return 1;
3114}
3115
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003116/*
3117 * Extract the tag value of an input binary sample. Takes a mandatory argument:
3118 * the FIX protocol tag identifier.
3119 * Return 1 if the tag was found, 0 if not.
3120 */
3121static int sample_conv_fix_tag_value(const struct arg *arg_p, struct sample *smp, void *private)
3122{
3123 struct ist value;
3124
3125 smp->flags &= ~SMP_F_MAY_CHANGE;
3126 value = fix_tag_value(ist2(smp->data.u.str.area, smp->data.u.str.data),
3127 arg_p[0].data.sint);
3128 if (!istlen(value)) {
3129 if (!isttest(value)) {
3130 /* value != IST_NULL, need more data */
3131 smp->flags |= SMP_F_MAY_CHANGE;
3132 }
3133 return 0;
3134 }
3135
3136 smp->data.u.str = ist2buf(value);
3137 smp->flags |= SMP_F_CONST;
3138
3139 return 1;
3140}
3141
3142/* This function checks the "fix_tag_value" converter configuration.
3143 * It expects a "known" (by HAProxy) tag name or ID.
3144 * Tag string names are converted to their ID counterpart because this is the
3145 * format they are sent over the wire.
3146 */
3147static int sample_conv_fix_value_check(struct arg *args, struct sample_conv *conv,
3148 const char *file, int line, char **err)
3149{
3150 struct ist str;
3151 unsigned int tag;
3152
3153 str = ist2(args[0].data.str.area, args[0].data.str.data);
3154 tag = fix_tagid(str);
3155 if (!tag) {
3156 memprintf(err, "Unknown FIX tag name '%s'", args[0].data.str.area);
3157 return 0;
3158 }
3159
3160 chunk_destroy(&args[0].data.str);
3161 args[0].type = ARGT_SINT;
3162 args[0].data.sint = tag;
3163
3164 return 1;
3165}
3166
3167/*
3168 * Checks that a buffer contains a valid FIX message
3169 *
3170 * Return 1 if the check could be run, 0 if not.
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003171 * The result of the analyse itself is stored in <smp> as a boolean
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003172 */
3173static int sample_conv_fix_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3174{
3175 struct ist msg;
3176
3177 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3178
3179 smp->flags &= ~SMP_F_MAY_CHANGE;
3180 switch (fix_validate_message(msg)) {
3181 case FIX_VALID_MESSAGE:
3182 smp->data.type = SMP_T_BOOL;
3183 smp->data.u.sint = 1;
3184 return 1;
3185 case FIX_NEED_MORE_DATA:
3186 smp->flags |= SMP_F_MAY_CHANGE;
3187 return 0;
3188 case FIX_INVALID_MESSAGE:
3189 smp->data.type = SMP_T_BOOL;
3190 smp->data.u.sint = 0;
3191 return 1;
3192 }
3193 return 0;
3194}
3195
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003196/*
3197 * Extract the field value of an input binary sample containing an MQTT packet.
3198 * Takes 2 mandatory arguments:
3199 * - packet type
3200 * - field name
3201 *
3202 * return 1 if the field was found, 0 if not.
3203 */
3204static int sample_conv_mqtt_field_value(const struct arg *arg_p, struct sample *smp, void *private)
3205{
3206 struct ist pkt, value;
3207 int type, fieldname_id;
3208
3209 pkt = ist2(smp->data.u.str.area, smp->data.u.str.data);
3210 type = arg_p[0].data.sint;
3211 fieldname_id = arg_p[1].data.sint;
3212
3213 smp->flags &= ~SMP_F_MAY_CHANGE;
3214 value = mqtt_field_value(pkt, type, fieldname_id);
3215 if (!istlen(value)) {
3216 if (isttest(value)) {
3217 /* value != IST_NULL, need more data */
3218 smp->flags |= SMP_F_MAY_CHANGE;
3219 }
3220 return 0;
3221 }
3222
3223 smp->data.u.str = ist2buf(value);
3224 smp->flags |= SMP_F_CONST;
3225 return 1;
3226}
3227
3228/*
3229 * this function checks the "mqtt_field_value" converter configuration.
3230 * It expects a known packet type name or ID and a field name, in this order
3231 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003232 * Args[0] will be turned into a MQTT_CPT_* value for direct matching when parsing
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003233 * a packet.
3234 */
3235static int sample_conv_mqtt_field_value_check(struct arg *args, struct sample_conv *conv,
3236 const char *file, int line, char **err)
3237{
3238 int type, fieldname_id;
3239
3240 /* check the MQTT packet type is valid */
3241 type = mqtt_typeid(ist2(args[0].data.str.area, args[0].data.str.data));
3242 if (type == MQTT_CPT_INVALID) {
3243 memprintf(err, "Unknown MQTT type '%s'", args[0].data.str.area);
3244 return 0;
3245 }
3246
3247 /* check the field name belongs to the MQTT packet type */
3248 fieldname_id = mqtt_check_type_fieldname(type, ist2(args[1].data.str.area, args[1].data.str.data));
3249 if (fieldname_id == MQTT_FN_INVALID) {
3250 memprintf(err, "Unknown MQTT field name '%s' for packet type '%s'", args[1].data.str.area,
3251 args[0].data.str.area);
3252 return 0;
3253 }
3254
3255 /* save numeric counterparts of type and field name */
3256 chunk_destroy(&args[0].data.str);
3257 chunk_destroy(&args[1].data.str);
3258 args[0].type = ARGT_SINT;
3259 args[0].data.sint = type;
3260 args[1].type = ARGT_SINT;
3261 args[1].data.sint = fieldname_id;
3262
3263 return 1;
3264}
3265
3266/*
3267 * Checks that <smp> contains a valid MQTT message
3268 *
3269 * The function returns 1 if the check was run to its end, 0 otherwise.
3270 * The result of the analyse itself is stored in <smp> as a boolean.
3271 */
3272static int sample_conv_mqtt_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3273{
3274 struct ist msg;
3275
3276 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3277
3278 smp->flags &= ~SMP_F_MAY_CHANGE;
3279 switch (mqtt_validate_message(msg, NULL)) {
3280 case FIX_VALID_MESSAGE:
3281 smp->data.type = SMP_T_BOOL;
3282 smp->data.u.sint = 1;
3283 return 1;
3284 case FIX_NEED_MORE_DATA:
3285 smp->flags |= SMP_F_MAY_CHANGE;
3286 return 0;
3287 case FIX_INVALID_MESSAGE:
3288 smp->data.type = SMP_T_BOOL;
3289 smp->data.u.sint = 0;
3290 return 1;
3291 }
3292 return 0;
3293}
3294
Tim Duesterhusca097c12018-04-27 21:18:45 +02003295/* This function checks the "strcmp" converter's arguments and extracts the
3296 * variable name and its scope.
3297 */
3298static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
3299 const char *file, int line, char **err)
3300{
Willy Tarreaua1169b62021-05-08 06:50:28 +02003301 if (!args[0].data.str.data) {
3302 memprintf(err, "missing variable name");
3303 return 0;
3304 }
3305
Tim Duesterhusca097c12018-04-27 21:18:45 +02003306 /* Try to decode a variable. */
3307 if (vars_check_arg(&args[0], NULL))
3308 return 1;
3309
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003310 memprintf(err, "failed to register variable name '%s'",
3311 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003312 return 0;
3313}
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003314
Christopher Faulet4ccc12f2020-04-01 09:08:32 +02003315/**/
3316static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private)
3317{
3318 struct buffer *tmp;
3319 uint32_t n;
3320
3321 n = htonl((uint32_t)smp->data.u.sint);
3322 tmp = get_trash_chunk();
3323
3324 memcpy(b_head(tmp), &n, 4);
3325 b_add(tmp, 4);
3326
3327 smp->data.u.str = *tmp;
3328 smp->data.type = SMP_T_BIN;
3329 return 1;
3330}
3331
Christopher Fauletea159d62020-04-01 16:21:44 +02003332/**/
3333static int sample_conv_cut_crlf(const struct arg *arg_p, struct sample *smp, void *private)
3334{
3335 char *p;
3336 size_t l;
3337
3338 p = smp->data.u.str.area;
3339 for (l = 0; l < smp->data.u.str.data; l++) {
3340 if (*(p+l) == '\r' || *(p+l) == '\n')
3341 break;
3342 }
3343 smp->data.u.str.data = l;
3344 return 1;
3345}
3346
Christopher Faulet51fc9d12020-04-01 17:24:41 +02003347/**/
3348static int sample_conv_ltrim(const struct arg *arg_p, struct sample *smp, void *private)
3349{
3350 char *delimiters, *p;
3351 size_t dlen, l;
3352
3353 delimiters = arg_p[0].data.str.area;
3354 dlen = arg_p[0].data.str.data;
3355
3356 l = smp->data.u.str.data;
3357 p = smp->data.u.str.area;
3358 while (l && memchr(delimiters, *p, dlen) != NULL) {
3359 p++;
3360 l--;
3361 }
3362
3363 smp->data.u.str.area = p;
3364 smp->data.u.str.data = l;
3365 return 1;
3366}
3367
Christopher Faulet568415a2020-04-01 17:24:47 +02003368/**/
3369static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *private)
3370{
3371 char *delimiters, *p;
3372 size_t dlen, l;
3373
3374 delimiters = arg_p[0].data.str.area;
3375 dlen = arg_p[0].data.str.data;
3376
3377 l = smp->data.u.str.data;
3378 p = smp->data.u.str.area + l - 1;
3379 while (l && memchr(delimiters, *p, dlen) != NULL) {
3380 p--;
3381 l--;
3382 }
3383
3384 smp->data.u.str.data = l;
3385 return 1;
3386}
3387
Alex51c8ad42021-04-15 16:45:15 +02003388/* This function checks the "json_query" converter's arguments. */
3389static int sample_check_json_query(struct arg *arg, struct sample_conv *conv,
3390 const char *file, int line, char **err)
3391{
3392 if (arg[0].data.str.data == 0) {
3393 memprintf(err, "json_path must not be empty");
3394 return 0;
3395 }
3396
3397 if (arg[1].data.str.data != 0) {
3398 if (strcmp(arg[1].data.str.area, "int") != 0) {
3399 memprintf(err, "output_type only supports \"int\" as argument");
3400 return 0;
3401 } else {
3402 arg[1].type = ARGT_SINT;
3403 arg[1].data.sint = 0;
3404 }
3405 }
3406 return 1;
3407}
3408
3409/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
3410 * the recommendation for interoperable integers in section 6 of RFC 7159.
3411 */
3412#define JSON_INT_MAX ((1LL << 53) - 1)
3413#define JSON_INT_MIN (-JSON_INT_MAX)
3414
3415/* This sample function get the value from a given json string.
3416 * The mjson library is used to parse the JSON struct
3417 */
3418static int sample_conv_json_query(const struct arg *args, struct sample *smp, void *private)
3419{
3420 struct buffer *trash = get_trash_chunk();
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003421 const char *token; /* holds the temporary string from mjson_find */
3422 int token_size; /* holds the length of <token> */
Alex51c8ad42021-04-15 16:45:15 +02003423
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003424 enum mjson_tok token_type;
Alex51c8ad42021-04-15 16:45:15 +02003425
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003426 token_type = mjson_find(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &token, &token_size);
3427
3428 switch (token_type) {
Alex51c8ad42021-04-15 16:45:15 +02003429 case MJSON_TOK_NUMBER:
3430 if (args[1].type == ARGT_SINT) {
Willy Tarreaufb601952021-05-14 08:51:53 +02003431 smp->data.u.sint = strtoll(token, NULL, 0);
Alex51c8ad42021-04-15 16:45:15 +02003432
3433 if (smp->data.u.sint < JSON_INT_MIN || smp->data.u.sint > JSON_INT_MAX)
3434 return 0;
3435
3436 smp->data.type = SMP_T_SINT;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003437
3438 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003439 } else {
3440 double double_val;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003441
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003442 if (mjson_get_number(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &double_val) == 0)
Alex51c8ad42021-04-15 16:45:15 +02003443 return 0;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003444
3445 trash->data = snprintf(trash->area,trash->size,"%g",double_val);
3446 smp->data.u.str = *trash;
3447 smp->data.type = SMP_T_STR;
3448
3449 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003450 }
Alex51c8ad42021-04-15 16:45:15 +02003451 case MJSON_TOK_TRUE:
3452 smp->data.type = SMP_T_BOOL;
3453 smp->data.u.sint = 1;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003454
3455 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003456 case MJSON_TOK_FALSE:
3457 smp->data.type = SMP_T_BOOL;
3458 smp->data.u.sint = 0;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003459
3460 return 1;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003461 case MJSON_TOK_STRING: {
3462 int len;
3463
3464 len = mjson_get_string(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, trash->area, trash->size);
3465
3466 if (len == -1) {
Alex51c8ad42021-04-15 16:45:15 +02003467 /* invalid string */
3468 return 0;
Alex51c8ad42021-04-15 16:45:15 +02003469 }
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003470
3471 trash->data = len;
3472 smp->data.u.str = *trash;
3473 smp->data.type = SMP_T_STR;
3474
3475 return 1;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003476 }
Tim Duesterhus8f3bc8f2021-04-15 18:14:32 +02003477 case MJSON_TOK_NULL:
3478 case MJSON_TOK_ARRAY:
3479 case MJSON_TOK_OBJECT:
3480 /* We cannot handle these. */
3481 return 0;
3482 case MJSON_TOK_INVALID:
3483 /* Nothing matches the query. */
3484 return 0;
3485 case MJSON_TOK_KEY:
3486 /* This is not a valid return value according to the
3487 * mjson documentation, but we handle it to benefit
3488 * from '-Wswitch'.
3489 */
Alex51c8ad42021-04-15 16:45:15 +02003490 return 0;
3491 }
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003492
3493 my_unreachable();
3494 return 0;
Alex51c8ad42021-04-15 16:45:15 +02003495}
3496
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003497#ifdef USE_OPENSSL
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +02003498static int sample_conv_jwt_verify_check(struct arg *args, struct sample_conv *conv,
3499 const char *file, int line, char **err)
3500{
3501 vars_check_arg(&args[0], NULL);
3502 vars_check_arg(&args[1], NULL);
3503
3504 if (args[0].type == ARGT_STR) {
3505 enum jwt_alg alg = jwt_parse_alg(args[0].data.str.area, args[0].data.str.data);
3506
3507 switch(alg) {
3508 case JWT_ALG_DEFAULT:
3509 memprintf(err, "unknown JWT algorithm : %s", *err);
3510 break;
3511
3512 case JWS_ALG_PS256:
3513 case JWS_ALG_PS384:
3514 case JWS_ALG_PS512:
3515 memprintf(err, "RSASSA-PSS JWS signing not managed yet");
3516 break;
3517
3518 default:
3519 break;
3520 }
3521 }
3522
3523 if (args[1].type == ARGT_STR) {
3524 jwt_tree_load_cert(args[1].data.str.area, args[1].data.str.data, err);
3525 }
3526
3527 return 1;
3528}
3529
3530/* Check that a JWT's signature is correct */
3531static int sample_conv_jwt_verify(const struct arg *args, struct sample *smp, void *private)
3532{
3533 struct sample alg_smp, key_smp;
3534
3535 smp->data.type = SMP_T_SINT;
3536 smp->data.u.sint = 0;
3537
3538 smp_set_owner(&alg_smp, smp->px, smp->sess, smp->strm, smp->opt);
3539 smp_set_owner(&key_smp, smp->px, smp->sess, smp->strm, smp->opt);
3540 if (!sample_conv_var2smp_str(&args[0], &alg_smp))
3541 return 0;
3542 if (!sample_conv_var2smp_str(&args[1], &key_smp))
3543 return 0;
3544
3545 smp->data.u.sint = jwt_verify(&smp->data.u.str, &alg_smp.data.u.str,
3546 &key_smp.data.u.str);
3547
3548 return 1;
3549}
3550
3551
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003552/*
3553 * Returns the decoded header or payload of a JWT if no parameter is given, or
3554 * the value of the specified field of the corresponding JWT subpart if a
3555 * parameter is given.
3556 */
3557static int sample_conv_jwt_member_query(const struct arg *args, struct sample *smp,
3558 void *private, enum jwt_elt member)
3559{
3560 struct jwt_item items[JWT_ELT_MAX] = { { 0 } };
3561 unsigned int item_num = member + 1; /* We don't need to tokenize the full token */
3562 struct buffer *decoded_header = get_trash_chunk();
3563 int retval = 0;
Willy Tarreaue20e0262021-10-15 12:10:24 +02003564 int ret;
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003565
3566 jwt_tokenize(&smp->data.u.str, items, &item_num);
3567
3568 if (item_num < member + 1)
3569 goto end;
3570
3571 decoded_header = alloc_trash_chunk();
3572 if (!decoded_header)
3573 goto end;
3574
Willy Tarreaue20e0262021-10-15 12:10:24 +02003575 ret = base64urldec(items[member].start, items[member].length,
3576 decoded_header->area, decoded_header->size);
3577 if (ret == -1)
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003578 goto end;
3579
Willy Tarreaue20e0262021-10-15 12:10:24 +02003580 decoded_header->data = ret;
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003581 if (args[0].type != ARGT_STR) {
3582 smp->data.u.str = *decoded_header;
3583 smp->data.type = SMP_T_STR;
3584 goto end;
3585 }
3586
3587 /* We look for a specific field of the header or payload part of the JWT */
3588 smp->data.u.str = *decoded_header;
3589
3590 retval = sample_conv_json_query(args, smp, private);
3591
3592end:
3593 return retval;
3594}
3595
3596/* This function checks the "jwt_header_query" and "jwt_payload_query" converters' arguments.
3597 * It is based on the "json_query" converter's check with the only difference
3598 * being that the jwt converters can take 0 parameters as well.
3599 */
3600static int sample_conv_jwt_query_check(struct arg *arg, struct sample_conv *conv,
3601 const char *file, int line, char **err)
3602{
3603 if (arg[1].data.str.data != 0) {
3604 if (strcmp(arg[1].data.str.area, "int") != 0) {
3605 memprintf(err, "output_type only supports \"int\" as argument");
3606 return 0;
3607 } else {
3608 arg[1].type = ARGT_SINT;
3609 arg[1].data.sint = 0;
3610 }
3611 }
3612 return 1;
3613}
3614
3615/*
3616 * If no parameter is given, return the decoded header part of a JWT (the first
3617 * base64 encoded part, corresponding to the JOSE header).
3618 * If a parameter is given, this converter acts as a "json_query" on this
3619 * decoded JSON.
3620 */
3621static int sample_conv_jwt_header_query(const struct arg *args, struct sample *smp, void *private)
3622{
3623 return sample_conv_jwt_member_query(args, smp, private, JWT_ELT_JOSE);
3624}
3625
3626/*
3627 * If no parameter is given, return the decoded payload part of a JWT (the
3628 * second base64 encoded part, which contains all the claims). If a parameter
3629 * is given, this converter acts as a "json_query" on this decoded JSON.
3630 */
3631static int sample_conv_jwt_payload_query(const struct arg *args, struct sample *smp, void *private)
3632{
3633 return sample_conv_jwt_member_query(args, smp, private, JWT_ELT_CLAIMS);
3634}
3635
3636#endif /* USE_OPENSSL */
Alex51c8ad42021-04-15 16:45:15 +02003637
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003638/************************************************************************/
3639/* All supported sample fetch functions must be declared here */
3640/************************************************************************/
3641
3642/* force TRUE to be returned at the fetch level */
3643static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003644smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003645{
Willy Tarreau280f42b2018-02-19 15:34:12 +01003646 if (!smp_make_rw(smp))
3647 return 0;
3648
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003649 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003650 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003651 return 1;
3652}
3653
3654/* force FALSE to be returned at the fetch level */
3655static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003656smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003657{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003658 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003659 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003660 return 1;
3661}
3662
3663/* retrieve environment variable $1 as a string */
3664static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003665smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003666{
3667 char *env;
3668
Christopher Faulete6e7a582021-01-29 11:29:28 +01003669 if (args[0].type != ARGT_STR)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003670 return 0;
3671
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003672 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003673 if (!env)
3674 return 0;
3675
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003676 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003677 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003678 smp->data.u.str.area = env;
3679 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003680 return 1;
3681}
3682
Damien Claisseae6f1252019-10-30 15:57:28 +00003683/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3684 * optional string representing the unit of the result: "s" for seconds, "ms" for
3685 * milliseconds and "us" for microseconds.
3686 * Returns 0 on error and non-zero if OK.
3687 */
3688int smp_check_date_unit(struct arg *args, char **err)
3689{
3690 if (args[1].type == ARGT_STR) {
Christopher Faulet95917132020-08-05 23:07:07 +02003691 long long int unit;
3692
Damien Claisseae6f1252019-10-30 15:57:28 +00003693 if (strcmp(args[1].data.str.area, "s") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003694 unit = TIME_UNIT_S;
Damien Claisseae6f1252019-10-30 15:57:28 +00003695 }
3696 else if (strcmp(args[1].data.str.area, "ms") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003697 unit = TIME_UNIT_MS;
Damien Claisseae6f1252019-10-30 15:57:28 +00003698 }
3699 else if (strcmp(args[1].data.str.area, "us") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003700 unit = TIME_UNIT_US;
Damien Claisseae6f1252019-10-30 15:57:28 +00003701 }
3702 else {
3703 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
3704 args[1].data.str.area);
3705 return 0;
3706 }
Christopher Faulet95917132020-08-05 23:07:07 +02003707
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003708 chunk_destroy(&args[1].data.str);
Damien Claisseae6f1252019-10-30 15:57:28 +00003709 args[1].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02003710 args[1].data.sint = unit;
Damien Claisseae6f1252019-10-30 15:57:28 +00003711 }
3712 else if (args[1].type != ARGT_STOP) {
3713 memprintf(err, "Unexpected arg type");
3714 return 0;
3715 }
3716
3717 return 1;
3718}
3719
3720/* retrieve the current local date in epoch time, converts it to milliseconds
3721 * or microseconds if asked to in optional args[1] unit param, and applies an
3722 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003723 */
3724static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003725smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003726{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003727 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003728
Damien Claisseae6f1252019-10-30 15:57:28 +00003729 /* report in milliseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003730 if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003731 smp->data.u.sint *= 1000;
3732 smp->data.u.sint += date.tv_usec / 1000;
3733 }
3734 /* report in microseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003735 else if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003736 smp->data.u.sint *= 1000000;
3737 smp->data.u.sint += date.tv_usec;
3738 }
3739
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003740 /* add offset */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003741 if (args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003742 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003743
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003744 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003745 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3746 return 1;
3747}
3748
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003749/* retrieve the current microsecond part of the date */
3750static int
3751smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3752{
3753 smp->data.u.sint = date.tv_usec;
3754 smp->data.type = SMP_T_SINT;
3755 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3756 return 1;
3757}
3758
3759
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003760/* returns the hostname */
3761static int
3762smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3763{
3764 smp->data.type = SMP_T_STR;
3765 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003766 smp->data.u.str.area = hostname;
3767 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003768 return 1;
3769}
3770
Willy Tarreau0f30d262014-11-24 16:02:05 +01003771/* returns the number of processes */
3772static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003773smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003774{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003775 smp->data.type = SMP_T_SINT;
Willy Tarreau91358592021-06-15 08:08:04 +02003776 smp->data.u.sint = 1;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003777 return 1;
3778}
3779
3780/* returns the number of the current process (between 1 and nbproc */
3781static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003782smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003783{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003784 smp->data.type = SMP_T_SINT;
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003785 smp->data.u.sint = 1;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003786 return 1;
3787}
3788
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003789/* returns the number of the current thread (between 1 and nbthread */
3790static int
3791smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3792{
3793 smp->data.type = SMP_T_SINT;
3794 smp->data.u.sint = tid;
3795 return 1;
3796}
3797
Willy Tarreau84310e22014-02-14 11:59:04 +01003798/* generate a random 32-bit integer for whatever purpose, with an optional
3799 * range specified in argument.
3800 */
3801static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003802smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01003803{
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003804 smp->data.u.sint = ha_random32();
Willy Tarreau84310e22014-02-14 11:59:04 +01003805
3806 /* reduce if needed. Don't do a modulo, use all bits! */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003807 if (args[0].type == ARGT_SINT)
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003808 smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32;
Willy Tarreau84310e22014-02-14 11:59:04 +01003809
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003810 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01003811 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3812 return 1;
3813}
3814
Willy Tarreau0f30d262014-11-24 16:02:05 +01003815/* returns true if the current process is stopping */
3816static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003817smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003818{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003819 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003820 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003821 return 1;
3822}
3823
Willy Tarreau70fe9442018-11-22 16:07:39 +01003824/* returns the number of calls of the current stream's process_stream() */
3825static int
3826smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
3827{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003828 if (!smp->strm)
3829 return 0;
3830
Willy Tarreau70fe9442018-11-22 16:07:39 +01003831 smp->data.type = SMP_T_SINT;
3832 smp->data.u.sint = smp->strm->task->calls;
3833 return 1;
3834}
3835
3836/* returns the average number of nanoseconds spent processing the stream per call */
3837static int
3838smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3839{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003840 if (!smp->strm)
3841 return 0;
3842
Willy Tarreau70fe9442018-11-22 16:07:39 +01003843 smp->data.type = SMP_T_SINT;
3844 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
3845 return 1;
3846}
3847
3848/* returns the total number of nanoseconds spent processing the stream */
3849static int
3850smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3851{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003852 if (!smp->strm)
3853 return 0;
3854
Willy Tarreau70fe9442018-11-22 16:07:39 +01003855 smp->data.type = SMP_T_SINT;
3856 smp->data.u.sint = smp->strm->task->cpu_time;
3857 return 1;
3858}
3859
3860/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
3861static int
3862smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3863{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003864 if (!smp->strm)
3865 return 0;
3866
Willy Tarreau70fe9442018-11-22 16:07:39 +01003867 smp->data.type = SMP_T_SINT;
3868 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
3869 return 1;
3870}
3871
3872/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
3873static int
3874smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3875{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003876 if (!smp->strm)
3877 return 0;
3878
Willy Tarreau70fe9442018-11-22 16:07:39 +01003879 smp->data.type = SMP_T_SINT;
3880 smp->data.u.sint = smp->strm->task->lat_time;
3881 return 1;
3882}
3883
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003884static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
3885{
3886 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003887 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003888 smp->data.u.str.area = args[0].data.str.area;
3889 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003890 return 1;
3891}
3892
3893static int smp_check_const_bool(struct arg *args, char **err)
3894{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003895 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
3896 strcasecmp(args[0].data.str.area, "1") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003897 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003898 args[0].type = ARGT_SINT;
3899 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003900 return 1;
3901 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003902 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
3903 strcasecmp(args[0].data.str.area, "0") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003904 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003905 args[0].type = ARGT_SINT;
3906 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003907 return 1;
3908 }
3909 memprintf(err, "Expects 'true', 'false', '0' or '1'");
3910 return 0;
3911}
3912
3913static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
3914{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003915 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003916 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003917 return 1;
3918}
3919
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003920static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003921{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003922 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003923 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003924 return 1;
3925}
3926
3927static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
3928{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003929 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003930 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003931 return 1;
3932}
3933
3934static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
3935{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003936 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003937 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003938 return 1;
3939}
3940
3941static int smp_check_const_bin(struct arg *args, char **err)
3942{
David Carlier64a16ab2016-04-08 10:37:02 +01003943 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003944 int binstrlen;
3945
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003946 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003947 return 0;
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003948 chunk_destroy(&args[0].data.str);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003949 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003950 args[0].data.str.area = binstr;
3951 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003952 return 1;
3953}
3954
3955static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
3956{
3957 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003958 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003959 smp->data.u.str.area = args[0].data.str.area;
3960 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003961 return 1;
3962}
3963
3964static int smp_check_const_meth(struct arg *args, char **err)
3965{
3966 enum http_meth_t meth;
3967 int i;
3968
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003969 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003970 if (meth != HTTP_METH_OTHER) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003971 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003972 args[0].type = ARGT_SINT;
3973 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003974 } else {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003975 /* Check method avalaibility. A method is a token defined as :
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003976 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
3977 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
3978 * token = 1*tchar
3979 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003980 for (i = 0; i < args[0].data.str.data; i++) {
3981 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003982 memprintf(err, "expects valid method.");
3983 return 0;
3984 }
3985 }
3986 }
3987 return 1;
3988}
3989
3990static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
3991{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003992 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003993 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003994 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003995 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003996 smp->data.u.meth.str.area = "";
3997 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003998 } else {
3999 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004000 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004001 smp->data.u.meth.str.area = args[0].data.str.area;
4002 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004003 }
4004 return 1;
4005}
4006
Luca Schimweg8a694b82019-09-10 15:42:52 +02004007// This function checks the "uuid" sample's arguments.
4008// Function won't get called when no parameter is specified (maybe a bug?)
4009static int smp_check_uuid(struct arg *args, char **err)
4010{
4011 if (!args[0].type) {
4012 args[0].type = ARGT_SINT;
4013 args[0].data.sint = 4;
4014 }
4015 else if (args[0].data.sint != 4) {
4016 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
4017 return 0;
4018 }
4019
4020 return 1;
4021}
4022
4023// Generate a RFC4122 UUID (default is v4 = fully random)
4024static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
4025{
4026 if (args[0].data.sint == 4 || !args[0].type) {
Willy Tarreauee3bcdd2020-03-08 17:48:17 +01004027 ha_generate_uuid(&trash);
Luca Schimweg8a694b82019-09-10 15:42:52 +02004028 smp->data.type = SMP_T_STR;
4029 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4030 smp->data.u.str = trash;
4031 return 1;
4032 }
4033
4034 // more implementations of other uuid formats possible here
4035 return 0;
4036}
4037
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004038/* Note: must not be declared <const> as its list will be overwritten.
4039 * Note: fetches that may return multiple types must be declared as the lowest
4040 * common denominator, the type that can be casted into all other ones. For
4041 * instance IPv4/IPv6 must be declared IPv4.
4042 */
4043static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau0209c972021-03-26 12:03:11 +01004044 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4045 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4046 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_CONST },
4047 { "date", smp_fetch_date, ARG2(0,SINT,STR), smp_check_date_unit, SMP_T_SINT, SMP_USE_CONST },
4048 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4049 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_CONST },
4050 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_CONST },
4051 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4052 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4053 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_CONST },
Willy Tarreau0f30d262014-11-24 16:02:05 +01004054 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau0209c972021-03-26 12:03:11 +01004055 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_CONST },
Willy Tarreau70fe9442018-11-22 16:07:39 +01004056
4057 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4058 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4059 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4060 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4061 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004062
Willy Tarreau0209c972021-03-26 12:03:11 +01004063 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_CONST },
4064 { "bool", smp_fetch_const_bool, ARG1(1,STR), smp_check_const_bool, SMP_T_BOOL, SMP_USE_CONST },
4065 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_CONST },
4066 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_CONST },
4067 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_CONST },
4068 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_CONST },
4069 { "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 +02004070
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004071 { /* END */ },
4072}};
4073
Willy Tarreau0108d902018-11-25 19:14:37 +01004074INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
4075
Emeric Brun107ca302010-01-04 16:16:05 +01004076/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02004077static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Moemen MHEDHBI848216f2021-04-02 01:05:07 +02004078 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
4079 { "b64dec", sample_conv_base642bin, 0, NULL, SMP_T_STR, SMP_T_BIN },
4080 { "base64", sample_conv_bin2base64, 0, NULL, SMP_T_BIN, SMP_T_STR },
4081 { "ub64enc", sample_conv_bin2base64url,0, NULL, SMP_T_BIN, SMP_T_STR },
4082 { "ub64dec", sample_conv_base64url2bin,0, NULL, SMP_T_STR, SMP_T_BIN },
4083 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
4084 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
4085 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Marcin Deranek40ca09c2021-07-13 14:05:24 +02004086 { "be2dec", sample_conv_be2dec, ARG3(1,STR,SINT,SINT), sample_conv_be2dec_check, SMP_T_BIN, SMP_T_STR },
Marcin Deranekda0264a2021-07-13 14:08:56 +02004087 { "be2hex", sample_conv_be2hex, ARG3(1,STR,SINT,SINT), sample_conv_be2hex_check, SMP_T_BIN, SMP_T_STR },
Moemen MHEDHBI848216f2021-04-02 01:05:07 +02004088 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
4089 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
4090 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
4091 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4092 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4093 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4094 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4095 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4096 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4097 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4098 { "xxh3", sample_conv_xxh3, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4099 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4100 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4101 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
4102 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
4103 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4104 { "word", sample_conv_word, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4105 { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
4106 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Willy Tarreau99ea1882021-10-06 15:37:17 +02004107 { "concat", sample_conv_concat, ARG3(1,STR,STR,STR), smp_check_concat, SMP_T_STR, SMP_T_STR },
4108 { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT },
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01004109
4110 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01004111 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
4112 { "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 +01004113
Baptiste Assmanne138dda2020-10-22 15:39:03 +02004114 /* FIX converters */
4115 { "fix_is_valid", sample_conv_fix_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4116 { "fix_tag_value", sample_conv_fix_tag_value, ARG1(1,STR), sample_conv_fix_value_check, SMP_T_BIN, SMP_T_BIN },
4117
Baptiste Assmanne279ca62020-10-27 18:10:06 +01004118 /* MQTT converters */
4119 { "mqtt_is_valid", sample_conv_mqtt_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4120 { "mqtt_field_value", sample_conv_mqtt_field_value, ARG2(2,STR,STR), sample_conv_mqtt_field_value_check, SMP_T_BIN, SMP_T_STR },
4121
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02004122 { "iif", sample_conv_iif, ARG2(2, STR, STR), NULL, SMP_T_BOOL, SMP_T_STR },
4123
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02004124 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4125 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4126 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4127 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
4128 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4129 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4130 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4131 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4132 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4133 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4134 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4135 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4136 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4137 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01004138
Christopher Fauletea159d62020-04-01 16:21:44 +02004139 { "htonl", sample_conv_htonl, 0, NULL, SMP_T_SINT, SMP_T_BIN },
4140 { "cut_crlf", sample_conv_cut_crlf, 0, NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet51fc9d12020-04-01 17:24:41 +02004141 { "ltrim", sample_conv_ltrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet568415a2020-04-01 17:24:47 +02004142 { "rtrim", sample_conv_rtrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Alex51c8ad42021-04-15 16:45:15 +02004143 { "json_query", sample_conv_json_query, ARG2(1,STR,STR), sample_check_json_query , SMP_T_STR, SMP_T_ANY },
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02004144
4145#ifdef USE_OPENSSL
4146 /* JSON Web Token converters */
4147 { "jwt_header_query", sample_conv_jwt_header_query, ARG2(0,STR,STR), sample_conv_jwt_query_check, SMP_T_BIN, SMP_T_ANY },
4148 { "jwt_payload_query", sample_conv_jwt_payload_query, ARG2(0,STR,STR), sample_conv_jwt_query_check, SMP_T_BIN, SMP_T_ANY },
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +02004149 { "jwt_verify", sample_conv_jwt_verify, ARG2(2,STR,STR), sample_conv_jwt_verify_check, SMP_T_BIN, SMP_T_SINT },
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02004150#endif
Willy Tarreau9fcb9842012-04-20 14:45:49 +02004151 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01004152}};
4153
Willy Tarreau0108d902018-11-25 19:14:37 +01004154INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);