blob: 7db5fde89477bdd1a1f6c703c79a3d153580ea07 [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 Tarreau0d71d2f2022-03-29 16:39:24 +0200330 [SMP_CKP_CFG_PARSER] = "configuration parser",
331 [SMP_CKP_CLI_PARSER] = "CLI parser",
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100332};
333
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100334/* This function returns the type of the data returned by the sample_expr.
335 * It assumes that the <expr> and all of its converters are properly
336 * initialized.
337 */
338inline
339int smp_expr_output_type(struct sample_expr *expr)
340{
341 struct sample_conv_expr *smp_expr;
342
343 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
344 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
345 return smp_expr->conv->out_type;
346 }
347 return expr->fetch->out_type;
348}
349
350
Willy Tarreau80aca902013-01-07 15:42:20 +0100351/* fill the trash with a comma-delimited list of source names for the <use> bit
352 * field which must be composed of a non-null set of SMP_USE_* flags. The return
353 * value is the pointer to the string in the trash buffer.
354 */
355const char *sample_src_names(unsigned int use)
356{
357 int bit;
358
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200359 trash.data = 0;
360 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100361 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
362 if (!(use & ~((1 << bit) - 1)))
363 break; /* no more bits */
364
365 if (!(use & (1 << bit)))
366 continue; /* bit not set */
367
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200368 trash.data += snprintf(trash.area + trash.data,
369 trash.size - trash.data, "%s%s",
370 (use & ((1 << bit) - 1)) ? "," : "",
371 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100372 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200373 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100374}
375
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100376/* return a pointer to the correct sample checkpoint name, or "unknown" when
377 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
378 * if set.
379 */
380const char *sample_ckp_names(unsigned int use)
381{
382 int bit;
383
384 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
385 if (use & (1 << bit))
386 return fetch_ckp_names[bit];
387 return "unknown sample check place, please report this bug";
388}
389
Emeric Brun107ca302010-01-04 16:16:05 +0100390/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100391 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
392 * for next parsing sessions. The fetch keywords capabilities are also computed
393 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100394 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100395void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100396{
Willy Tarreau80aca902013-01-07 15:42:20 +0100397 struct sample_fetch *sf;
398 int bit;
399
400 for (sf = kwl->kw; sf->kw != NULL; sf++) {
401 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
402 if (sf->use & (1 << bit))
403 sf->val |= fetch_cap[bit];
404 }
Willy Tarreau2b718102021-04-21 07:32:39 +0200405 LIST_APPEND(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100406}
407
408/*
Willy Tarreau12785782012-04-27 21:37:17 +0200409 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100410 * parsing sessions.
411 */
Willy Tarreau12785782012-04-27 21:37:17 +0200412void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100413{
Willy Tarreau2b718102021-04-21 07:32:39 +0200414 LIST_APPEND(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100415}
416
417/*
Willy Tarreau12785782012-04-27 21:37:17 +0200418 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100419 * string of <len> in buffer <kw>.
420 *
421 */
Willy Tarreau12785782012-04-27 21:37:17 +0200422struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100423{
424 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200425 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100426
Willy Tarreau12785782012-04-27 21:37:17 +0200427 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100428 for (index = 0; kwl->kw[index].kw != NULL; index++) {
429 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
430 kwl->kw[index].kw[len] == '\0')
431 return &kwl->kw[index];
432 }
433 }
434 return NULL;
435}
436
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100437/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100438 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100439 * <idx> is the index of the next sample fetch entry. It is used as private
440 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100441 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100442 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100443 * the list is reached.
444 */
445struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
446{
447 struct sample_fetch_kw_list *kwl;
448 struct sample_fetch *base;
449
450 if (!current) {
451 /* Get first kwl entry. */
452 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
453 (*idx) = 0;
454 } else {
455 /* Get kwl corresponding to the curret entry. */
456 base = current + 1 - (*idx);
457 kwl = container_of(base, struct sample_fetch_kw_list, kw);
458 }
459
460 while (1) {
461
462 /* Check if kwl is the last entry. */
463 if (&kwl->list == &sample_fetches.list)
464 return NULL;
465
466 /* idx contain the next keyword. If it is available, return it. */
467 if (kwl->kw[*idx].kw) {
468 (*idx)++;
469 return &kwl->kw[(*idx)-1];
470 }
471
472 /* get next entry in the main list, and return NULL if the end is reached. */
473 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
474
475 /* Set index to 0, ans do one other loop. */
476 (*idx) = 0;
477 }
478}
479
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100480/* This function browses the list of available converters. <current> is
481 * the last used converter. If it is the first call, it must set to NULL.
482 * <idx> is the index of the next converter entry. It is used as private
483 * value. It is useless to initiate it.
484 *
485 * It returns always the next sample_conv entry, and NULL when the end of
486 * the list is reached.
487 */
488struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
489{
490 struct sample_conv_kw_list *kwl;
491 struct sample_conv *base;
492
493 if (!current) {
494 /* Get first kwl entry. */
495 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
496 (*idx) = 0;
497 } else {
498 /* Get kwl corresponding to the curret entry. */
499 base = current + 1 - (*idx);
500 kwl = container_of(base, struct sample_conv_kw_list, kw);
501 }
502
503 while (1) {
504 /* Check if kwl is the last entry. */
505 if (&kwl->list == &sample_convs.list)
506 return NULL;
507
508 /* idx contain the next keyword. If it is available, return it. */
509 if (kwl->kw[*idx].kw) {
510 (*idx)++;
511 return &kwl->kw[(*idx)-1];
512 }
513
514 /* get next entry in the main list, and return NULL if the end is reached. */
515 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
516
517 /* Set index to 0, ans do one other loop. */
518 (*idx) = 0;
519 }
520}
521
Emeric Brun107ca302010-01-04 16:16:05 +0100522/*
Willy Tarreau12785782012-04-27 21:37:17 +0200523 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100524 * string of <len> in buffer <kw>.
525 *
526 */
Willy Tarreau12785782012-04-27 21:37:17 +0200527struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100528{
529 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200530 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100531
Willy Tarreau12785782012-04-27 21:37:17 +0200532 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100533 for (index = 0; kwl->kw[index].kw != NULL; index++) {
534 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
535 kwl->kw[index].kw[len] == '\0')
536 return &kwl->kw[index];
537 }
538 }
539 return NULL;
540}
541
Emeric Brun107ca302010-01-04 16:16:05 +0100542/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200543/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100544/******************************************************************/
545
Willy Tarreau342acb42012-04-23 22:03:39 +0200546static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100547{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200548 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200549 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100550 return 1;
551}
552
Willy Tarreau342acb42012-04-23 22:03:39 +0200553static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100554{
Willy Tarreau83061a82018-07-13 11:56:34 +0200555 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100556
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200557 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100558 return 0;
559
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200560 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200561 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200562 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100563 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100564
565 return 1;
566}
567
Willy Tarreau342acb42012-04-23 22:03:39 +0200568static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100569{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200570 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200571 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100572 return 1;
573}
574
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200575static int c_ipv62ip(struct sample *smp)
576{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200577 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200578 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100579 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200580 return 1;
581}
582
Willy Tarreau342acb42012-04-23 22:03:39 +0200583static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100584{
Willy Tarreau83061a82018-07-13 11:56:34 +0200585 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100586
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200587 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100588 return 0;
589
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200590 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200591 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200592 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100593 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100594 return 1;
595}
596
597/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200598static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100599{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200600 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100601}
602*/
603
Willy Tarreau342acb42012-04-23 22:03:39 +0200604static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100605{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200606 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200607 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100608 return 1;
609}
610
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200611static int c_int2ipv6(struct sample *smp)
612{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200613 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
614 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200615 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200616 return 1;
617}
618
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100619static int c_str2addr(struct sample *smp)
620{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200621 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
622 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100623 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200624 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100625 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100626 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100627 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200628 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100629 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100630 return 1;
631}
632
Willy Tarreau342acb42012-04-23 22:03:39 +0200633static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100634{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200635 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100636 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200637 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100638 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100639 return 1;
640}
641
Willy Tarreau342acb42012-04-23 22:03:39 +0200642static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100643{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200644 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100645 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200646 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100647 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100648 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100649}
650
Emeric Brun4b9e8022014-11-03 18:17:10 +0100651/*
652 * The NULL char always enforces the end of string if it is met.
653 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100654 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200655static int c_bin2str(struct sample *smp)
656{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100657 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200658
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200659 for (i = 0; i < smp->data.u.str.data; i++) {
660 if (!smp->data.u.str.area[i]) {
661 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100662 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100663 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200664 }
Christopher Faulet472ad512020-04-30 09:57:40 +0200665 smp->data.type = SMP_T_STR;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200666 return 1;
667}
668
Willy Tarreau342acb42012-04-23 22:03:39 +0200669static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100670{
Willy Tarreau83061a82018-07-13 11:56:34 +0200671 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100672 char *pos;
673
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200674 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100675 if (!pos)
676 return 0;
677
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200678 trash->size = trash->size - (pos - trash->area);
679 trash->area = pos;
680 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200681 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200682 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100683 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100684 return 1;
685}
686
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800687/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200688 * For strings and binary blocks, it also provides a known allocated size with
689 * a length that is capped to the size, and ensures a trailing zero is always
690 * appended for strings. This is necessary for some operations which may
691 * require to extend the length. It returns 0 if it fails, 1 on success.
692 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100693int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200694{
Willy Tarreau83061a82018-07-13 11:56:34 +0200695 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200696
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200697 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100698 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100699 case SMP_T_SINT:
700 case SMP_T_ADDR:
701 case SMP_T_IPV4:
702 case SMP_T_IPV6:
703 /* These type are not const. */
704 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200705
Christopher Fauletec100512017-07-24 15:38:41 +0200706 case SMP_T_METH:
707 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
708 break;
709 /* Fall through */
710
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100711 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200712 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100713 trash->data = smp->data.type == SMP_T_STR ?
714 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200715 if (trash->data > trash->size - 1)
716 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200717
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100718 memcpy(trash->area, smp->data.type == SMP_T_STR ?
719 smp->data.u.str.area : smp->data.u.meth.str.area,
720 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200721 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200722 smp->data.u.str = *trash;
723 break;
724
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100725 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100726 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200727 trash->data = smp->data.u.str.data;
728 if (trash->data > trash->size)
729 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200730
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200731 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200732 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100733 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200734
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100735 default:
736 /* Other cases are unexpected. */
737 return 0;
738 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100739
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100740 /* remove const flag */
741 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200742 return 1;
743}
744
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100745int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100746{
747 return 1;
748}
749
Willy Tarreau342acb42012-04-23 22:03:39 +0200750static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100751{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200752 const char *str;
753 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100754
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200755 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100756 return 0;
757
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200758 str = smp->data.u.str.area;
759 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100760
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200761 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200762 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100763 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100764 return 1;
765}
766
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100767static int c_str2meth(struct sample *smp)
768{
769 enum http_meth_t meth;
770 int len;
771
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200772 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100773 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200774 len = smp->data.u.str.data;
775 smp->data.u.meth.str.area = smp->data.u.str.area;
776 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100777 }
778 else
779 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200780 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200781 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100782 return 1;
783}
784
785static int c_meth2str(struct sample *smp)
786{
787 int len;
788 enum http_meth_t meth;
789
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200790 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100791 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200792 len = smp->data.u.meth.str.data;
793 smp->data.u.str.area = smp->data.u.meth.str.area;
794 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200795 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100796 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200797 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100798 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200799 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200800 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200801 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100802 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200803 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100804 }
805 else {
806 /* Unknown method */
807 return 0;
808 }
809 return 1;
810}
811
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200812static int c_addr2bin(struct sample *smp)
813{
Willy Tarreau83061a82018-07-13 11:56:34 +0200814 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200815
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200816 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200817 chk->data = 4;
818 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200819 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200820 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200821 chk->data = 16;
822 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200823 }
824 else
825 return 0;
826
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200827 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200828 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200829 return 1;
830}
831
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200832static int c_int2bin(struct sample *smp)
833{
Willy Tarreau83061a82018-07-13 11:56:34 +0200834 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200835
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200836 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
837 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200838
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200839 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200840 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200841 return 1;
842}
843
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200844
Emeric Brun107ca302010-01-04 16:16:05 +0100845/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200846/* Sample casts matrix: */
847/* sample_casts[from type][to type] */
848/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100849/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100850
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100851sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200852/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
853/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
854/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200855/* 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 +0200856/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
857/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200858/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200859/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
860/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
861/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200862};
Emeric Brun107ca302010-01-04 16:16:05 +0100863
Emeric Brun107ca302010-01-04 16:16:05 +0100864/*
Willy Tarreau12785782012-04-27 21:37:17 +0200865 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100866 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200867 * Returns a pointer on allocated sample expression structure.
Christopher Faulet35926a12021-09-30 08:48:56 +0200868 * <al> is an arg_list serving as a list head to report missing dependencies.
869 * It may be NULL if such dependencies are not allowed. Otherwise, the caller
870 * must have set al->ctx if al is set.
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100871 * If <endptr> is non-nul, it will be set to the first unparsed character
872 * (which may be the final '\0') on success. If it is nul, the expression
873 * must be properly terminated by a '\0' otherwise an error is reported.
Emeric Brun107ca302010-01-04 16:16:05 +0100874 */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100875struct 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 +0100876{
Willy Tarreau833cc792013-07-24 15:34:19 +0200877 const char *begw; /* beginning of word */
878 const char *endw; /* end of word */
879 const char *endt; /* end of term */
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +0100880 struct sample_expr *expr = NULL;
Willy Tarreau12785782012-04-27 21:37:17 +0200881 struct sample_fetch *fetch;
882 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100883 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200884 char *fkw = NULL;
885 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100886 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100887
Willy Tarreau833cc792013-07-24 15:34:19 +0200888 begw = str[*idx];
Willy Tarreaued2c6622020-02-14 18:27:10 +0100889 for (endw = begw; is_idchar(*endw); endw++)
890 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200891
892 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100893 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100894 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200895 }
Emeric Brun107ca302010-01-04 16:16:05 +0100896
Willy Tarreau833cc792013-07-24 15:34:19 +0200897 /* keep a copy of the current fetch keyword for error reporting */
898 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100899
Willy Tarreau833cc792013-07-24 15:34:19 +0200900 fetch = find_sample_fetch(begw, endw - begw);
901 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100902 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100903 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200904 }
Emeric Brun107ca302010-01-04 16:16:05 +0100905
Willy Tarreau833cc792013-07-24 15:34:19 +0200906 /* At this point, we have :
907 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100908 * - endw : end of the keyword, first character not part of keyword
Willy Tarreau833cc792013-07-24 15:34:19 +0200909 */
910
911 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100912 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100913 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200914 }
Emeric Brun107ca302010-01-04 16:16:05 +0100915 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200916
Vincent Bernat02779b62016-04-03 13:48:43 +0200917 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200918 if (!expr)
919 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100920
921 LIST_INIT(&(expr->conv_exprs));
922 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200923 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100924
Willy Tarreau689a1df2013-12-13 00:40:11 +0100925 /* Note that we call the argument parser even with an empty string,
926 * this allows it to automatically create entries for mandatory
927 * implicit arguments (eg: local proxy name).
928 */
Christopher Faulet35926a12021-09-30 08:48:56 +0200929 if (al) {
930 al->kw = expr->fetch->kw;
931 al->conv = NULL;
932 }
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100933 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 +0100934 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
935 goto out_error;
936 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200937
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100938 /* now endt is our first char not part of the arg list, typically the
939 * comma after the sample fetch name or after the closing parenthesis,
940 * or the NUL char.
941 */
942
Willy Tarreau689a1df2013-12-13 00:40:11 +0100943 if (!expr->arg_p) {
944 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200945 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100946 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
947 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200948 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100949 }
950
Willy Tarreau833cc792013-07-24 15:34:19 +0200951 /* Now process the converters if any. We have two supported syntaxes
952 * for the converters, which can be combined :
953 * - comma-delimited list of converters just after the keyword and args ;
954 * - one converter per keyword
955 * The combination allows to have each keyword being a comma-delimited
956 * series of converters.
957 *
958 * We want to process the former first, then the latter. For this we start
959 * from the beginning of the supposed place in the exiting conv chain, which
960 * starts at the last comma (endt).
961 */
962
963 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200964 struct sample_conv_expr *conv_expr;
Willy Tarreau46dfd782019-12-17 10:25:29 +0100965 int err_arg;
966 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +0100967
Willy Tarreau833cc792013-07-24 15:34:19 +0200968 if (*endt && *endt != ',') {
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100969 if (endptr) {
970 /* end found, let's stop here */
971 break;
972 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200973 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100974 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200975 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100976 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100977 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200978 }
Emeric Brun107ca302010-01-04 16:16:05 +0100979
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100980 /* FIXME: how long should we support such idiocies ? Maybe we
981 * should already warn ?
982 */
Willy Tarreau833cc792013-07-24 15:34:19 +0200983 while (*endt == ',') /* then trailing commas */
984 endt++;
985
Willy Tarreau97108e02016-11-25 07:33:24 +0100986 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200987
988 if (!*begw) {
989 /* none ? skip to next string */
990 (*idx)++;
991 begw = str[*idx];
992 if (!begw || !*begw)
993 break;
994 }
995
Willy Tarreaued2c6622020-02-14 18:27:10 +0100996 for (endw = begw; is_idchar(*endw); endw++)
997 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200998
999 free(ckw);
1000 ckw = my_strndup(begw, endw - begw);
1001
1002 conv = find_sample_conv(begw, endw - begw);
1003 if (!conv) {
1004 /* we found an isolated keyword that we don't know, it's not ours */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001005 if (begw == str[*idx]) {
1006 endt = begw;
Willy Tarreau833cc792013-07-24 15:34:19 +02001007 break;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001008 }
Willy Tarreau97108e02016-11-25 07:33:24 +01001009 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +02001010 goto out_error;
1011 }
Emeric Brun107ca302010-01-04 16:16:05 +01001012
Willy Tarreau833cc792013-07-24 15:34:19 +02001013 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001014 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001015 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001016 }
Emeric Brun107ca302010-01-04 16:16:05 +01001017
1018 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +02001019 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001020 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001021 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001022 }
Emeric Brun107ca302010-01-04 16:16:05 +01001023
1024 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +02001025 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +02001026 if (!conv_expr)
1027 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001028
Willy Tarreau2b718102021-04-21 07:32:39 +02001029 LIST_APPEND(&(expr->conv_exprs), &(conv_expr->list));
Emeric Brun107ca302010-01-04 16:16:05 +01001030 conv_expr->conv = conv;
1031
Christopher Faulet35926a12021-09-30 08:48:56 +02001032 if (al) {
1033 al->kw = expr->fetch->kw;
1034 al->conv = conv_expr->conv->kw;
1035 }
Willy Tarreau80b53ff2020-02-14 08:40:37 +01001036 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 +01001037 if (argcnt < 0) {
1038 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
1039 goto out_error;
1040 }
Willy Tarreau9e92d322010-01-26 17:58:06 +01001041
Willy Tarreau46dfd782019-12-17 10:25:29 +01001042 if (argcnt && !conv->arg_mask) {
1043 memprintf(err_msg, "converter '%s' does not support any args", ckw);
1044 goto out_error;
1045 }
Willy Tarreau21d68a62012-04-20 15:52:36 +02001046
Willy Tarreau46dfd782019-12-17 10:25:29 +01001047 if (!conv_expr->arg_p)
1048 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001049
Willy Tarreau46dfd782019-12-17 10:25:29 +01001050 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1051 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001052 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001053 }
1054 }
Emeric Brun485479d2010-09-23 18:02:19 +02001055
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001056 if (endptr) {
1057 /* end found, let's stop here */
1058 *endptr = (char *)endt;
1059 }
1060
Willy Tarreau833cc792013-07-24 15:34:19 +02001061 out:
1062 free(fkw);
1063 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001064 return expr;
1065
1066out_error:
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +01001067 release_sample_expr(expr);
Willy Tarreau833cc792013-07-24 15:34:19 +02001068 expr = NULL;
1069 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001070}
1071
1072/*
Willy Tarreau12785782012-04-27 21:37:17 +02001073 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001074 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001075 * Returns a pointer on a typed sample structure containing the result or NULL if
1076 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001077 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001078 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001079 *
1080 * Note: the fetch functions are required to properly set the return type. The
1081 * conversion functions must do so too. However the cast functions do not need
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001082 * to since they're made to cast multiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001083 *
1084 * The caller may indicate in <opt> if it considers the result final or not.
1085 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1086 * if the result is stable or not, according to the following table :
1087 *
1088 * return MAY_CHANGE FINAL Meaning for the sample
1089 * NULL 0 * Not present and will never be (eg: header)
1090 * NULL 1 0 Not present yet, could change (eg: POST param)
1091 * NULL 1 1 Not present yet, will not change anymore
1092 * smp 0 * Present and will not change (eg: header)
1093 * smp 1 0 Present, may change (eg: request length)
1094 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001095 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001096struct sample *sample_process(struct proxy *px, struct session *sess,
1097 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001098 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001099{
Willy Tarreau12785782012-04-27 21:37:17 +02001100 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001101
Willy Tarreau18387e22013-07-25 12:02:38 +02001102 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001103 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001104 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001105 }
Emeric Brun107ca302010-01-04 16:16:05 +01001106
Willy Tarreau1777ea62016-03-10 16:15:46 +01001107 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001108 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001109 return NULL;
1110
Emeric Brun107ca302010-01-04 16:16:05 +01001111 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001112 /* we want to ensure that p->type can be casted into
1113 * conv_expr->conv->in_type. We have 3 possibilities :
1114 * - NULL => not castable.
1115 * - c_none => nothing to do (let's optimize it)
1116 * - other => apply cast and prepare to fail
1117 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001118 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001119 return NULL;
1120
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001121 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1122 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001123 return NULL;
1124
Willy Tarreau12e50112012-04-25 17:21:49 +02001125 /* OK cast succeeded */
1126
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001127 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001128 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001129 }
1130 return p;
1131}
1132
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001133/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001134 * Resolve all remaining arguments in proxy <p>. Returns the number of
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001135 * errors or 0 if everything is fine. If at least one error is met, it will
1136 * be appended to *err. If *err==NULL it will be allocated first.
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001137 */
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001138int smp_resolve_args(struct proxy *p, char **err)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001139{
1140 struct arg_list *cur, *bak;
1141 const char *ctx, *where;
1142 const char *conv_ctx, *conv_pre, *conv_pos;
1143 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001144 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001145 struct arg *arg;
1146 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001147 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001148
1149 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1150 struct proxy *px;
1151 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001152 struct stktable *t;
1153 char *pname, *sname, *stktname;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001154 char *err2;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001155
1156 arg = cur->arg;
1157
1158 /* prepare output messages */
1159 conv_pre = conv_pos = conv_ctx = "";
1160 if (cur->conv) {
1161 conv_ctx = cur->conv;
1162 conv_pre = "conversion keyword '";
1163 conv_pos = "' for ";
1164 }
1165
1166 where = "in";
1167 ctx = "sample fetch keyword";
1168 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001169 case ARGC_STK: where = "in stick rule in"; break;
1170 case ARGC_TRK: where = "in tracking rule in"; break;
1171 case ARGC_LOG: where = "in log-format string in"; break;
1172 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
Willy Tarreau57467b82021-09-02 19:43:20 +02001173 case ARGC_HRQ: where = "in http-request expression in"; break;
1174 case ARGC_HRS: where = "in http-response response in"; break;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001175 case ARGC_UIF: where = "in unique-id-format string in"; break;
1176 case ARGC_RDR: where = "in redirect format string in"; break;
1177 case ARGC_CAP: where = "in capture rule in"; break;
1178 case ARGC_ACL: ctx = "ACL keyword"; break;
1179 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001180 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001181 case ARGC_HERR: where = "in http-error directive in"; break;
Miroslav Zagorac7f8314c2020-12-09 16:31:48 +01001182 case ARGC_OT: where = "in ot-scope directive in"; break;
Willy Tarreau57467b82021-09-02 19:43:20 +02001183 case ARGC_TCO: where = "in tcp-request connection expression in"; break;
1184 case ARGC_TSE: where = "in tcp-request session expression in"; break;
1185 case ARGC_TRQ: where = "in tcp-request content expression in"; break;
1186 case ARGC_TRS: where = "in tcp-response content expression in"; break;
1187 case ARGC_TCK: where = "in tcp-check expression in"; break;
1188 case ARGC_CFG: where = "in configuration expression in"; break;
1189 case ARGC_CLI: where = "in CLI expression in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001190 }
1191
1192 /* set a few default settings */
1193 px = p;
1194 pname = p->id;
1195
1196 switch (arg->type) {
1197 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001198 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001199 memprintf(err, "%sparsing [%s:%d]: missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1200 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001201 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001202 cfgerr++;
1203 continue;
1204 }
1205
1206 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001207 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001208
1209 if (sname) {
1210 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001211 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001212
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001213 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001214 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001215 memprintf(err, "%sparsing [%s:%d]: unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1216 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001217 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001218 cfgerr++;
1219 break;
1220 }
1221 }
Christopher Faulet37a9e212021-10-12 18:48:05 +02001222 else {
1223 if (px->cap & PR_CAP_DEF) {
1224 memprintf(err, "%sparsing [%s:%d]: backend name must be set in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1225 *err ? *err : "", cur->file, cur->line,
1226 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1227 cfgerr++;
1228 break;
1229 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001230 sname = arg->data.str.area;
Christopher Faulet37a9e212021-10-12 18:48:05 +02001231 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001232
1233 srv = findserver(px, sname);
1234 if (!srv) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001235 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",
1236 *err ? *err : "", cur->file, cur->line, sname, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001237 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001238 cfgerr++;
1239 break;
1240 }
1241
Amaury Denoyelle06269612021-08-23 14:05:07 +02001242 srv->flags |= SRV_F_NON_PURGEABLE;
1243
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001244 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001245 arg->unresolved = 0;
1246 arg->data.srv = srv;
1247 break;
1248
1249 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001250 if (arg->data.str.data) {
1251 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001252 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001253 }
1254
1255 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001256 memprintf(err, "%sparsing [%s:%d]: unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1257 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001258 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001259 cfgerr++;
1260 break;
1261 }
1262
1263 if (!(px->cap & PR_CAP_FE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001264 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",
1265 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001266 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001267 cfgerr++;
1268 break;
1269 }
1270
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001271 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001272 arg->unresolved = 0;
1273 arg->data.prx = px;
1274 break;
1275
1276 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001277 if (arg->data.str.data) {
1278 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001279 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001280 }
1281
1282 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001283 memprintf(err, "%sparsing [%s:%d]: unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1284 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001285 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001286 cfgerr++;
1287 break;
1288 }
1289
1290 if (!(px->cap & PR_CAP_BE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001291 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",
1292 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001293 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001294 cfgerr++;
1295 break;
1296 }
1297
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001298 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001299 arg->unresolved = 0;
1300 arg->data.prx = px;
1301 break;
1302
1303 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001304 if (arg->data.str.data)
1305 stktname = arg->data.str.area;
Christopher Faulet37a9e212021-10-12 18:48:05 +02001306 else {
1307 if (px->cap & PR_CAP_DEF) {
1308 memprintf(err, "%sparsing [%s:%d]: table name must be set in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1309 *err ? *err : "", cur->file, cur->line,
1310 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1311 cfgerr++;
1312 break;
1313 }
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001314 stktname = px->id;
Christopher Faulet37a9e212021-10-12 18:48:05 +02001315 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001316
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001317 t = stktable_find_by_name(stktname);
1318 if (!t) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001319 memprintf(err, "%sparsing [%s:%d]: unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1320 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001321 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001322 cfgerr++;
1323 break;
1324 }
1325
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001326 if (!t->size) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001327 memprintf(err, "%sparsing [%s:%d]: no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1328 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001329 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001330 cfgerr++;
1331 break;
1332 }
1333
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001334 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001335 p->next_stkt_ref = t->proxies_list;
1336 t->proxies_list = p;
1337 }
1338
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001339 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001340 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001341 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001342 break;
1343
1344 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001345 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001346 memprintf(err, "%sparsing [%s:%d]: missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1347 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001348 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001349 cfgerr++;
1350 break;
1351 }
1352
1353 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001354 strcmp(p->uri_auth->userlist->name, arg->data.str.area) == 0)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001355 ul = p->uri_auth->userlist;
1356 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001357 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001358
1359 if (!ul) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001360 memprintf(err, "%sparsing [%s:%d]: unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1361 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001362 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001363 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001364 cfgerr++;
1365 break;
1366 }
1367
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001368 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001369 arg->unresolved = 0;
1370 arg->data.usr = ul;
1371 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001372
1373 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001374 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001375 memprintf(err, "%sparsing [%s:%d]: missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1376 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001377 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001378 cfgerr++;
1379 continue;
1380 }
1381
Willy Tarreau46947782015-01-19 19:00:58 +01001382 rflags = 0;
1383 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001384 err2 = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001385
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001386 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err2))) {
1387 memprintf(err, "%sparsing [%s:%d]: error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1388 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001389 arg->data.str.area,
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001390 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err2);
Willy Tarreau46947782015-01-19 19:00:58 +01001391 cfgerr++;
1392 continue;
1393 }
1394
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001395 chunk_destroy(&arg->data.str);
Willy Tarreau46947782015-01-19 19:00:58 +01001396 arg->unresolved = 0;
1397 arg->data.reg = reg;
1398 break;
1399
1400
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001401 }
1402
Willy Tarreau2b718102021-04-21 07:32:39 +02001403 LIST_DELETE(&cur->list);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001404 free(cur);
1405 } /* end of args processing */
1406
1407 return cfgerr;
1408}
1409
1410/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001411 * Process a fetch + format conversion as defined by the sample expression
1412 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001413 * not explicitly set to <smp_type>, but shall be compatible with it as
1414 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1415 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001416 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1417 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1418 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1419 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001420 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1421 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1422 * still hope to get it after waiting longer, and is not converted to string.
1423 * The possible output combinations are the following :
1424 *
1425 * return MAY_CHANGE FINAL Meaning for the sample
1426 * NULL * * Not present and will never be (eg: header)
1427 * smp 0 * Final value converted (eg: header)
1428 * smp 1 0 Not present yet, may appear later (eg: header)
1429 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001430 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001431struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001432 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001433 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001434{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001435 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001436
Willy Tarreau6c616e02014-06-25 16:56:41 +02001437 memset(smp, 0, sizeof(*smp));
1438
Willy Tarreau192252e2015-04-04 01:47:55 +02001439 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001440 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1441 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001442 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001443 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001444
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001445 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001446 return NULL;
1447
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001448 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001449 return NULL;
1450
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001451 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001452 return smp;
1453}
1454
Christopher Faulet476e5d02016-10-26 11:34:47 +02001455static void release_sample_arg(struct arg *p)
1456{
1457 struct arg *p_back = p;
1458
1459 if (!p)
1460 return;
1461
1462 while (p->type != ARGT_STOP) {
1463 if (p->type == ARGT_STR || p->unresolved) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001464 chunk_destroy(&p->data.str);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001465 p->unresolved = 0;
1466 }
1467 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001468 regex_free(p->data.reg);
1469 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001470 }
1471 p++;
1472 }
1473
1474 if (p_back != empty_arg_list)
1475 free(p_back);
1476}
1477
1478void release_sample_expr(struct sample_expr *expr)
1479{
1480 struct sample_conv_expr *conv_expr, *conv_exprb;
1481
1482 if (!expr)
1483 return;
1484
Tim Duesterhus867cd982020-07-04 11:49:39 +02001485 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001486 LIST_DELETE(&conv_expr->list);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001487 release_sample_arg(conv_expr->arg_p);
Tim Duesterhus867cd982020-07-04 11:49:39 +02001488 free(conv_expr);
1489 }
1490
Christopher Faulet476e5d02016-10-26 11:34:47 +02001491 release_sample_arg(expr->arg_p);
1492 free(expr);
1493}
1494
Emeric Brun107ca302010-01-04 16:16:05 +01001495/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001496/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001497/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001498/*****************************************************************/
1499
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001500static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1501{
1502 int i;
1503 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001504 struct buffer *buf;
1505 struct sink *sink;
1506 struct ist line;
1507 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001508
Willy Tarreau0851fd52019-12-17 10:07:25 +01001509 buf = alloc_trash_chunk();
1510 if (!buf)
1511 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001512
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001513 sink = (struct sink *)arg_p[1].data.ptr;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001514 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001515
Willy Tarreau0851fd52019-12-17 10:07:25 +01001516 pfx = arg_p[0].data.str.area;
1517 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001518
Willy Tarreau0851fd52019-12-17 10:07:25 +01001519 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1520 if (!sample_casts[smp->data.type][SMP_T_STR])
1521 goto nocast;
1522
1523 /* Copy sample fetch. This puts the sample as const, the
1524 * cast will copy data if a transformation is required.
1525 */
1526 memcpy(&tmp, smp, sizeof(struct sample));
1527 tmp.flags = SMP_F_CONST;
1528
1529 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1530 goto nocast;
1531
1532 /* Display the displayable chars*. */
1533 b_putchr(buf, '<');
1534 for (i = 0; i < tmp.data.u.str.data; i++) {
Willy Tarreau90807112020-02-25 08:16:33 +01001535 if (isprint((unsigned char)tmp.data.u.str.area[i]))
Willy Tarreau0851fd52019-12-17 10:07:25 +01001536 b_putchr(buf, tmp.data.u.str.area[i]);
1537 else
1538 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001539 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001540 b_putchr(buf, '>');
1541
1542 done:
1543 line = ist2(buf->area, buf->data);
Emeric Brun54648852020-07-06 15:54:06 +02001544 sink_write(sink, &line, 1, 0, 0, NULL);
Willy Tarreau0851fd52019-12-17 10:07:25 +01001545 end:
1546 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001547 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001548 nocast:
1549 chunk_appendf(buf, "(undisplayable)");
1550 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001551}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001552
1553// This function checks the "debug" converter's arguments.
1554static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1555 const char *file, int line, char **err)
1556{
1557 const char *name = "buf0";
1558 struct sink *sink = NULL;
1559
1560 if (args[0].type != ARGT_STR) {
1561 /* optional prefix */
1562 args[0].data.str.area = "";
1563 args[0].data.str.data = 0;
1564 }
1565
1566 if (args[1].type == ARGT_STR)
1567 name = args[1].data.str.area;
1568
1569 sink = sink_find(name);
1570 if (!sink) {
1571 memprintf(err, "No such sink '%s'", name);
1572 return 0;
1573 }
1574
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001575 chunk_destroy(&args[1].data.str);
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001576 args[1].type = ARGT_PTR;
1577 args[1].data.ptr = sink;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001578 return 1;
1579}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001580
Holger Just1bfc24b2017-05-06 00:56:53 +02001581static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1582{
Willy Tarreau83061a82018-07-13 11:56:34 +02001583 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001584 int bin_len;
1585
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001586 trash->data = 0;
1587 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1588 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001589 if (bin_len < 0)
1590 return 0;
1591
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001592 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001593 smp->data.u.str = *trash;
1594 smp->data.type = SMP_T_BIN;
1595 smp->flags &= ~SMP_F_CONST;
1596 return 1;
1597}
1598
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +02001599static int sample_conv_base64url2bin(const struct arg *arg_p, struct sample *smp, void *private)
1600{
1601 struct buffer *trash = get_trash_chunk();
1602 int bin_len;
1603
1604 trash->data = 0;
1605 bin_len = base64urldec(smp->data.u.str.area, smp->data.u.str.data,
1606 trash->area, trash->size);
1607 if (bin_len < 0)
1608 return 0;
1609
1610 trash->data = bin_len;
1611 smp->data.u.str = *trash;
1612 smp->data.type = SMP_T_BIN;
1613 smp->flags &= ~SMP_F_CONST;
1614 return 1;
1615}
1616
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001617static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001618{
Willy Tarreau83061a82018-07-13 11:56:34 +02001619 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001620 int b64_len;
1621
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001622 trash->data = 0;
1623 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1624 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001625 if (b64_len < 0)
1626 return 0;
1627
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001628 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001629 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001630 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001631 smp->flags &= ~SMP_F_CONST;
1632 return 1;
1633}
1634
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +02001635static int sample_conv_bin2base64url(const struct arg *arg_p, struct sample *smp, void *private)
1636{
1637 struct buffer *trash = get_trash_chunk();
1638 int b64_len;
1639
1640 trash->data = 0;
1641 b64_len = a2base64url(smp->data.u.str.area, smp->data.u.str.data,
1642 trash->area, trash->size);
1643 if (b64_len < 0)
1644 return 0;
1645
1646 trash->data = b64_len;
1647 smp->data.u.str = *trash;
1648 smp->data.type = SMP_T_STR;
1649 smp->flags &= ~SMP_F_CONST;
1650 return 1;
1651}
1652
Willy Tarreau168e8de2021-10-06 15:29:00 +02001653/* This function returns a sample struct filled with the conversion of variable
1654 * <var> to sample type <type> (SMP_T_*), via a cast to the target type. If the
1655 * variable cannot be retrieved or casted, 0 is returned, otherwise 1.
1656 *
1657 * Keep in mind that the sample content may be written to a pre-allocated
1658 * trash chunk as returned by get_trash_chunk().
1659 */
1660int sample_conv_var2smp(const struct var_desc *var, struct sample *smp, int type)
1661{
1662 if (!vars_get_by_desc(var, smp, NULL))
1663 return 0;
1664 if (!sample_casts[smp->data.type][type])
1665 return 0;
1666 if (!sample_casts[smp->data.type][type](smp))
1667 return 0;
1668 return 1;
1669}
1670
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001671static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1672{
1673 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001674 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001675
1676 memset(&ctx, 0, sizeof(ctx));
1677
1678 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001679 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1680 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001681
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001682 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001683 smp->data.u.str = *trash;
1684 smp->data.type = SMP_T_BIN;
1685 smp->flags &= ~SMP_F_CONST;
1686 return 1;
1687}
1688
Dragan Dosen9e8db132021-02-22 10:03:53 +01001689/* This function returns a sample struct filled with an <arg> content.
1690 * If the <arg> contains a string, it is returned in the sample flagged as
1691 * SMP_F_CONST. If the <arg> contains a variable descriptor, the sample is
1692 * filled with the content of the variable by using vars_get_by_desc().
1693 *
1694 * Keep in mind that the sample content may be written to a pre-allocated
1695 * trash chunk as returned by get_trash_chunk().
1696 *
1697 * This function returns 0 if an error occurs, otherwise it returns 1.
1698 */
Willy Tarreau4034e2c2021-10-06 15:20:18 +02001699int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001700{
1701 switch (arg->type) {
1702 case ARGT_STR:
1703 smp->data.type = SMP_T_STR;
1704 smp->data.u.str = arg->data.str;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001705 smp->flags = SMP_F_CONST;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001706 return 1;
1707 case ARGT_VAR:
Willy Tarreau2476ff12021-10-06 15:30:52 +02001708 return sample_conv_var2smp(&arg->data.var, smp, SMP_T_STR);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001709 default:
1710 return 0;
1711 }
1712}
1713
Marcin Deranek40ca09c2021-07-13 14:05:24 +02001714static int sample_conv_be2dec_check(struct arg *args, struct sample_conv *conv,
1715 const char *file, int line, char **err)
1716{
1717 if (args[1].data.sint <= 0 || args[1].data.sint > sizeof(unsigned long long)) {
Willy Tarreauc5d0fc92021-09-15 10:30:40 +02001718 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 +02001719 return 0;
1720 }
1721
1722 if (args[2].data.sint != 0 && args[2].data.sint != 1) {
1723 memprintf(err, "Unsupported truncate value (%lld)", args[2].data.sint);
1724 return 0;
1725 }
1726
1727 return 1;
1728}
1729
1730/* Converts big-endian binary input sample to a string containing an unsigned
1731 * integer number per <chunk_size> input bytes separated with <separator>.
1732 * Optional <truncate> flag indicates if input is truncated at <chunk_size>
1733 * boundaries.
1734 * Arguments: separator (string), chunk_size (integer), truncate (0,1)
1735 */
1736static int sample_conv_be2dec(const struct arg *args, struct sample *smp, void *private)
1737{
1738 struct buffer *trash = get_trash_chunk();
1739 const int last = args[2].data.sint ? smp->data.u.str.data - args[1].data.sint + 1 : smp->data.u.str.data;
1740 int max_size = trash->size - 2;
1741 int i;
1742 int start;
1743 int ptr = 0;
1744 unsigned long long number;
1745 char *pos;
1746
1747 trash->data = 0;
1748
1749 while (ptr < last && trash->data <= max_size) {
1750 start = trash->data;
1751 if (ptr) {
1752 /* Add separator */
1753 memcpy(trash->area + trash->data, args[0].data.str.area, args[0].data.str.data);
1754 trash->data += args[0].data.str.data;
1755 }
1756 else
1757 max_size -= args[0].data.str.data;
1758
1759 /* Add integer */
1760 for (number = 0, i = 0; i < args[1].data.sint && ptr < smp->data.u.str.data; i++)
1761 number = (number << 8) + (unsigned char)smp->data.u.str.area[ptr++];
1762
1763 pos = ulltoa(number, trash->area + trash->data, trash->size - trash->data);
1764 if (pos)
1765 trash->data = pos - trash->area;
1766 else {
1767 trash->data = start;
1768 break;
1769 }
1770 }
1771
1772 smp->data.u.str = *trash;
1773 smp->data.type = SMP_T_STR;
1774 smp->flags &= ~SMP_F_CONST;
1775 return 1;
1776}
1777
Marcin Deranekda0264a2021-07-13 14:08:56 +02001778static int sample_conv_be2hex_check(struct arg *args, struct sample_conv *conv,
1779 const char *file, int line, char **err)
1780{
1781 if (args[1].data.sint <= 0 && (args[0].data.str.data > 0 || args[2].data.sint != 0)) {
1782 memprintf(err, "chunk_size needs to be positive (%lld)", args[1].data.sint);
1783 return 0;
1784 }
1785
1786 if (args[2].data.sint != 0 && args[2].data.sint != 1) {
1787 memprintf(err, "Unsupported truncate value (%lld)", args[2].data.sint);
1788 return 0;
1789 }
1790
1791 return 1;
1792}
1793
1794/* Converts big-endian binary input sample to a hex string containing two hex
1795 * digits per input byte. <separator> is put every <chunk_size> binary input
1796 * bytes if specified. Optional <truncate> flag indicates if input is truncated
1797 * at <chunk_size> boundaries.
1798 * Arguments: separator (string), chunk_size (integer), truncate (0,1)
1799 */
1800static int sample_conv_be2hex(const struct arg *args, struct sample *smp, void *private)
1801{
1802 struct buffer *trash = get_trash_chunk();
1803 int chunk_size = args[1].data.sint;
1804 const int last = args[2].data.sint ? smp->data.u.str.data - chunk_size + 1 : smp->data.u.str.data;
1805 int i;
1806 int max_size;
1807 int ptr = 0;
1808 unsigned char c;
1809
1810 trash->data = 0;
1811 if (args[0].data.str.data == 0 && args[2].data.sint == 0)
1812 chunk_size = smp->data.u.str.data;
1813 max_size = trash->size - 2 * chunk_size;
1814
1815 while (ptr < last && trash->data <= max_size) {
1816 if (ptr) {
1817 /* Add separator */
1818 memcpy(trash->area + trash->data, args[0].data.str.area, args[0].data.str.data);
1819 trash->data += args[0].data.str.data;
1820 }
1821 else
1822 max_size -= args[0].data.str.data;
1823
1824 /* Add hex */
1825 for (i = 0; i < chunk_size && ptr < smp->data.u.str.data; i++) {
1826 c = smp->data.u.str.area[ptr++];
1827 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1828 trash->area[trash->data++] = hextab[c & 0xF];
1829 }
1830 }
1831
1832 smp->data.u.str = *trash;
1833 smp->data.type = SMP_T_STR;
1834 smp->flags &= ~SMP_F_CONST;
1835 return 1;
1836}
1837
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001838static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001839{
Willy Tarreau83061a82018-07-13 11:56:34 +02001840 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001841 unsigned char c;
1842 int ptr = 0;
1843
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001844 trash->data = 0;
1845 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
1846 c = smp->data.u.str.area[ptr++];
1847 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1848 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001849 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001850 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001851 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001852 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001853 return 1;
1854}
1855
Dragan Dosen3f957b22017-10-24 09:27:34 +02001856static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1857{
1858 long long int n = 0;
1859 int i, c;
1860
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001861 for (i = 0; i < smp->data.u.str.data; i++) {
1862 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02001863 return 0;
1864 n = (n << 4) + c;
1865 }
1866
1867 smp->data.u.sint = n;
1868 smp->data.type = SMP_T_SINT;
1869 smp->flags &= ~SMP_F_CONST;
1870 return 1;
1871}
1872
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001873/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001874static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001875{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001876 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
1877 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01001878 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001879 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001880 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001881 return 1;
1882}
1883
Willy Tarreau60a2ee72017-12-15 07:13:48 +01001884static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001885{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001886 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001887 smp->data.u.sint = i;
1888 smp->data.type = SMP_T_SINT;
1889 return 1;
1890}
1891
1892
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001893static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001894{
1895 int i;
1896
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001897 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001898 return 0;
1899
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001900 for (i = 0; i < smp->data.u.str.data; i++) {
1901 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
1902 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001903 }
1904 return 1;
1905}
1906
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001907static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001908{
1909 int i;
1910
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001911 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001912 return 0;
1913
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001914 for (i = 0; i < smp->data.u.str.data; i++) {
1915 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
1916 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001917 }
1918 return 1;
1919}
1920
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001921/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
1922static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001923{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001924 /* Attempt to convert to IPv4 to apply the correct mask. */
1925 c_ipv62ip(smp);
1926
1927 if (smp->data.type == SMP_T_IPV4) {
1928 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
1929 smp->data.type = SMP_T_IPV4;
1930 }
1931 else if (smp->data.type == SMP_T_IPV6) {
1932 /* IPv6 cannot be converted without an IPv6 mask. */
1933 if (args[1].type != ARGT_IPV6)
1934 return 0;
1935
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +01001936 write_u64(&smp->data.u.ipv6.s6_addr[0],
1937 read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0]));
1938 write_u64(&smp->data.u.ipv6.s6_addr[8],
1939 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 +01001940 smp->data.type = SMP_T_IPV6;
1941 }
1942
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001943 return 1;
1944}
1945
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001946/* takes an UINT value on input supposed to represent the time since EPOCH,
1947 * adds an optional offset found in args[1] and emits a string representing
1948 * the local time in the format specified in args[1] using strftime().
1949 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001950static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001951{
Willy Tarreau83061a82018-07-13 11:56:34 +02001952 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001953 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001954 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Tim Duesterhus1f269c12021-08-28 23:57:01 +02001955 struct tm tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001956
1957 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001958 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001959 curr_date += args[1].data.sint;
1960
Tim Duesterhus1f269c12021-08-28 23:57:01 +02001961 get_localtime(curr_date, &tm);
1962
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001963 temp = get_trash_chunk();
Tim Duesterhus1f269c12021-08-28 23:57:01 +02001964 temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001965 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001966 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001967 return 1;
1968}
1969
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001970/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001971static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001972{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001973 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
1974 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01001975 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001976 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001977 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001978 return 1;
1979}
1980
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001981/* takes an UINT value on input supposed to represent the time since EPOCH,
1982 * adds an optional offset found in args[1] and emits a string representing
1983 * the UTC date in the format specified in args[1] using strftime().
1984 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001985static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001986{
Willy Tarreau83061a82018-07-13 11:56:34 +02001987 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001988 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001989 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Tim Duesterhus1f269c12021-08-28 23:57:01 +02001990 struct tm tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001991
1992 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001993 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001994 curr_date += args[1].data.sint;
1995
Tim Duesterhus1f269c12021-08-28 23:57:01 +02001996 get_gmtime(curr_date, &tm);
1997
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001998 temp = get_trash_chunk();
Tim Duesterhus1f269c12021-08-28 23:57:01 +02001999 temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002000 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002001 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002002 return 1;
2003}
2004
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002005/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002006static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002007{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002008 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
2009 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002010 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002011 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002012 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002013 return 1;
2014}
2015
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002016/* hashes the binary input into a 32-bit unsigned int using xxh.
2017 * The seed of the hash defaults to 0 but can be changd in argument 1.
2018 */
2019static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
2020{
2021 unsigned int seed;
2022
Christopher Faulete6e7a582021-01-29 11:29:28 +01002023 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002024 seed = arg_p->data.sint;
2025 else
2026 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002027 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
2028 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002029 smp->data.type = SMP_T_SINT;
2030 return 1;
2031}
2032
2033/* hashes the binary input into a 64-bit unsigned int using xxh.
2034 * In fact, the function returns a 64 bit unsigned, but the sample
2035 * storage of haproxy only proposes 64-bits signed, so the value is
2036 * cast as signed. This cast doesn't impact the hash repartition.
2037 * The seed of the hash defaults to 0 but can be changd in argument 1.
2038 */
2039static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
2040{
2041 unsigned long long int seed;
2042
Christopher Faulete6e7a582021-01-29 11:29:28 +01002043 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002044 seed = (unsigned long long int)arg_p->data.sint;
2045 else
2046 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002047 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
2048 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002049 smp->data.type = SMP_T_SINT;
2050 return 1;
2051}
2052
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002053static int sample_conv_xxh3(const struct arg *arg_p, struct sample *smp, void *private)
2054{
2055 unsigned long long int seed;
2056
Christopher Faulete6e7a582021-01-29 11:29:28 +01002057 if (arg_p->data.sint)
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002058 seed = (unsigned long long int)arg_p->data.sint;
2059 else
2060 seed = 0;
2061 smp->data.u.sint = (long long int)XXH3(smp->data.u.str.area,
2062 smp->data.u.str.data, seed);
2063 smp->data.type = SMP_T_SINT;
2064 return 1;
2065}
2066
Willy Tarreau80599772015-01-20 19:35:24 +01002067/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002068static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01002069{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002070 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
2071 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002072 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002073 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002074 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01002075 return 1;
2076}
2077
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002078/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
2079static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
2080{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002081 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
2082 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002083 if (arg_p->data.sint)
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002084 smp->data.u.sint = full_hash(smp->data.u.sint);
2085 smp->data.type = SMP_T_SINT;
2086 return 1;
2087}
2088
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002089/* This function escape special json characters. The returned string can be
2090 * safely set between two '"' and used as json string. The json string is
2091 * defined like this:
2092 *
2093 * any Unicode character except '"' or '\' or control character
2094 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
2095 *
2096 * The enum input_type contain all the allowed mode for decoding the input
2097 * string.
2098 */
2099enum input_type {
2100 IT_ASCII = 0,
2101 IT_UTF8,
2102 IT_UTF8S,
2103 IT_UTF8P,
2104 IT_UTF8PS,
2105};
William Dauchy5417e892021-01-08 21:57:41 +01002106
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002107static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
2108 const char *file, int line, char **err)
2109{
Christopher Faulet95917132020-08-05 23:07:07 +02002110 enum input_type type;
2111
Christopher Faulet95917132020-08-05 23:07:07 +02002112 if (strcmp(arg->data.str.area, "") == 0)
2113 type = IT_ASCII;
2114 else if (strcmp(arg->data.str.area, "ascii") == 0)
2115 type = IT_ASCII;
2116 else if (strcmp(arg->data.str.area, "utf8") == 0)
2117 type = IT_UTF8;
2118 else if (strcmp(arg->data.str.area, "utf8s") == 0)
2119 type = IT_UTF8S;
2120 else if (strcmp(arg->data.str.area, "utf8p") == 0)
2121 type = IT_UTF8P;
2122 else if (strcmp(arg->data.str.area, "utf8ps") == 0)
2123 type = IT_UTF8PS;
2124 else {
2125 memprintf(err, "Unexpected input code type. "
2126 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
2127 return 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002128 }
2129
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002130 chunk_destroy(&arg->data.str);
Christopher Faulet95917132020-08-05 23:07:07 +02002131 arg->type = ARGT_SINT;
2132 arg->data.sint = type;
2133 return 1;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002134}
2135
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002136static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002137{
Willy Tarreau83061a82018-07-13 11:56:34 +02002138 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002139 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
2140 const char *str;
2141 int len;
2142 enum input_type input_type = IT_ASCII;
2143 unsigned int c;
2144 unsigned int ret;
2145 char *p;
2146
Christopher Faulete6e7a582021-01-29 11:29:28 +01002147 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002148
2149 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002150 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002151
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002152 p = smp->data.u.str.area;
2153 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002154
2155 if (input_type == IT_ASCII) {
2156 /* Read input as ASCII. */
2157 c = *(unsigned char *)p;
2158 p++;
2159 }
2160 else {
2161 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002162 ret = utf8_next(p,
2163 smp->data.u.str.data - ( p - smp->data.u.str.area),
2164 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002165 p += utf8_return_length(ret);
2166
2167 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2168 return 0;
2169 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2170 continue;
2171 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2172 return 0;
2173 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2174 continue;
2175
2176 /* Check too big values. */
2177 if ((unsigned int)c > 0xffff) {
2178 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2179 return 0;
2180 continue;
2181 }
2182 }
2183
2184 /* Convert character. */
2185 if (c == '"') {
2186 len = 2;
2187 str = "\\\"";
2188 }
2189 else if (c == '\\') {
2190 len = 2;
2191 str = "\\\\";
2192 }
2193 else if (c == '/') {
2194 len = 2;
2195 str = "\\/";
2196 }
2197 else if (c == '\b') {
2198 len = 2;
2199 str = "\\b";
2200 }
2201 else if (c == '\f') {
2202 len = 2;
2203 str = "\\f";
2204 }
2205 else if (c == '\r') {
2206 len = 2;
2207 str = "\\r";
2208 }
2209 else if (c == '\n') {
2210 len = 2;
2211 str = "\\n";
2212 }
2213 else if (c == '\t') {
2214 len = 2;
2215 str = "\\t";
2216 }
Willy Tarreau90807112020-02-25 08:16:33 +01002217 else if (c > 0xff || !isprint((unsigned char)c)) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002218 /* isprint generate a segfault if c is too big. The man says that
2219 * c must have the value of an unsigned char or EOF.
2220 */
2221 len = 6;
2222 _str[0] = '\\';
2223 _str[1] = 'u';
2224 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2225 str = _str;
2226 }
2227 else {
2228 len = 1;
Willy Tarreau5715da22020-02-25 08:37:37 +01002229 _str[0] = c;
2230 str = _str;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002231 }
2232
2233 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002234 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002235 return 0;
2236
2237 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002238 memcpy(temp->area + temp->data, str, len);
2239 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002240 }
2241
2242 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002243 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002244 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002245
2246 return 1;
2247}
2248
Emeric Brun54c4ac82014-11-03 15:32:43 +01002249/* This sample function is designed to extract some bytes from an input buffer.
2250 * First arg is the offset.
2251 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002252static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002253{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002254 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2255 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002256 return 1;
2257 }
2258
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002259 if (smp->data.u.str.size)
2260 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002261 smp->data.u.str.data -= arg_p[0].data.sint;
2262 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002263
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002264 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2265 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002266
2267 return 1;
2268}
2269
Emeric Brunf399b0d2014-11-03 17:07:03 +01002270static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2271 const char *file, int line, char **err)
2272{
2273 struct arg *arg = args;
2274
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002275 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002276 memprintf(err, "Unexpected arg type");
2277 return 0;
2278 }
2279
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002280 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002281 memprintf(err, "Unexpected value 0 for index");
2282 return 0;
2283 }
2284
2285 arg++;
2286
2287 if (arg->type != ARGT_STR) {
2288 memprintf(err, "Unexpected arg type");
2289 return 0;
2290 }
2291
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002292 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002293 memprintf(err, "Empty separators list");
2294 return 0;
2295 }
2296
2297 return 1;
2298}
2299
2300/* This sample function is designed to a return selected part of a string (field).
2301 * First arg is the index of the field (start at 1)
2302 * Second arg is a char list of separators (type string)
2303 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002304static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002305{
Marcin Deranek9631a282018-04-16 14:30:46 +02002306 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002307 char *start, *end;
2308 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002309 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002310
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002311 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002312 return 0;
2313
Marcin Deranek9631a282018-04-16 14:30:46 +02002314 if (arg_p[0].data.sint < 0) {
2315 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002316 end = start = smp->data.u.str.area + smp->data.u.str.data;
2317 while (start > smp->data.u.str.area) {
2318 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2319 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002320 if (field == arg_p[0].data.sint) {
2321 if (count == 1)
2322 goto found;
2323 else if (count > 1)
2324 count--;
2325 } else {
2326 end = start-1;
2327 field--;
2328 }
2329 break;
2330 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002331 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002332 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002333 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002334 } else {
2335 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002336 end = start = smp->data.u.str.area;
2337 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2338 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2339 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002340 if (field == arg_p[0].data.sint) {
2341 if (count == 1)
2342 goto found;
2343 else if (count > 1)
2344 count--;
2345 } else {
2346 start = end+1;
2347 field++;
2348 }
2349 break;
2350 }
2351 }
2352 end++;
2353 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002354 }
2355
2356 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002357 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002358 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002359 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002360 }
2361found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002362 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002363 /* If ret string is len 0, no need to
2364 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002365 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002366 return 1;
2367
Emeric Brunf399b0d2014-11-03 17:07:03 +01002368 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002369 Note: smp->data.u.str.size cannot be set to 0 */
2370 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002371 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002372
Thayne McCombsb2843052021-04-11 23:26:59 -06002373 smp->data.u.str.area = start;
2374
Emeric Brunf399b0d2014-11-03 17:07:03 +01002375 return 1;
2376}
2377
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002378/* This sample function is designed to return a word from a string.
2379 * First arg is the index of the word (start at 1)
2380 * Second arg is a char list of words separators (type string)
2381 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002382static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002383{
Marcin Deranek9631a282018-04-16 14:30:46 +02002384 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002385 char *start, *end;
2386 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002387 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002388
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002389 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002390 return 0;
2391
2392 word = 0;
2393 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002394 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002395 end = start = smp->data.u.str.area + smp->data.u.str.data;
2396 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002397 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002398 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2399 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002400 issep = 1;
2401 break;
2402 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002403 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002404 if (!inword) {
2405 if (!issep) {
2406 if (word != arg_p[0].data.sint) {
2407 word--;
2408 end = start;
2409 }
2410 inword = 1;
2411 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002412 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002413 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002414 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002415 if (count == 1)
2416 goto found;
2417 else if (count > 1)
2418 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002419 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002420 inword = 0;
2421 }
2422 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002423 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002424 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002425 end = start = smp->data.u.str.area;
2426 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002427 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002428 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2429 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002430 issep = 1;
2431 break;
2432 }
2433 }
2434 if (!inword) {
2435 if (!issep) {
2436 if (word != arg_p[0].data.sint) {
2437 word++;
2438 start = end;
2439 }
2440 inword = 1;
2441 }
2442 }
2443 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002444 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002445 if (count == 1)
2446 goto found;
2447 else if (count > 1)
2448 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002449 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002450 inword = 0;
2451 }
2452 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002453 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002454 }
2455
2456 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002457 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002458 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002459 return 1;
2460 }
2461found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002462 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002463 /* If ret string is len 0, no need to
2464 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002465 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002466 return 1;
2467
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002468 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002469
2470 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002471 Note: smp->data.u.str.size cannot be set to 0 */
2472 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002473 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002474
2475 return 1;
2476}
2477
Willy Tarreau7eda8492015-01-20 19:47:06 +01002478static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2479 const char *file, int line, char **err)
2480{
2481 struct arg *arg = args;
2482 char *p;
2483 int len;
2484
2485 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2486 arg[0].type_flags = 0;
2487
2488 if (arg[2].type != ARGT_STR)
2489 return 1;
2490
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002491 p = arg[2].data.str.area;
2492 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002493 while (len) {
2494 if (*p == 'i') {
2495 arg[0].type_flags |= ARGF_REG_ICASE;
2496 }
2497 else if (*p == 'g') {
2498 arg[0].type_flags |= ARGF_REG_GLOB;
2499 }
2500 else {
2501 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2502 return 0;
2503 }
2504 p++;
2505 len--;
2506 }
2507 return 1;
2508}
2509
2510/* This sample function is designed to do the equivalent of s/match/replace/ on
2511 * the input string. It applies a regex and restarts from the last matched
2512 * location until nothing matches anymore. First arg is the regex to apply to
2513 * the input string, second arg is the replacement expression.
2514 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002515static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002516{
2517 char *start, *end;
2518 struct my_regex *reg = arg_p[0].data.reg;
2519 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002520 struct buffer *trash = get_trash_chunk();
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002521 struct buffer *output;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002522 int flag, max;
2523 int found;
2524
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002525 start = smp->data.u.str.area;
2526 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002527
2528 flag = 0;
2529 while (1) {
2530 /* check for last round which is used to copy remaining parts
2531 * when not running in global replacement mode.
2532 */
2533 found = 0;
2534 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2535 /* Note: we can have start == end on empty strings or at the end */
2536 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2537 }
2538
2539 if (!found)
2540 pmatch[0].rm_so = end - start;
2541
2542 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002543 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002544 if (max && pmatch[0].rm_so > 0) {
2545 if (max > pmatch[0].rm_so)
2546 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002547 memcpy(trash->area + trash->data, start, max);
2548 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002549 }
2550
2551 if (!found)
2552 break;
2553
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002554 output = alloc_trash_chunk();
Willy Tarreau23997da2020-02-18 14:27:44 +01002555 if (!output)
2556 break;
2557
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002558 output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2559
Willy Tarreau7eda8492015-01-20 19:47:06 +01002560 /* replace the matching part */
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002561 max = output->size - output->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002562 if (max) {
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002563 if (max > output->data)
2564 max = output->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002565 memcpy(trash->area + trash->data,
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002566 output->area, max);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002567 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002568 }
2569
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002570 free_trash_chunk(output);
2571
Willy Tarreau7eda8492015-01-20 19:47:06 +01002572 /* stop here if we're done with this string */
2573 if (start >= end)
2574 break;
2575
2576 /* We have a special case for matches of length 0 (eg: "x*y*").
2577 * These ones are considered to match in front of a character,
2578 * so we have to copy that character and skip to the next one.
2579 */
2580 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002581 if (trash->data < trash->size)
2582 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002583 pmatch[0].rm_eo++;
2584 }
2585
2586 start += pmatch[0].rm_eo;
2587 flag |= REG_NOTBOL;
2588 }
2589
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002590 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002591 return 1;
2592}
2593
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002594/* This function check an operator entry. It expects a string.
2595 * The string can be an integer or a variable name.
2596 */
2597static int check_operator(struct arg *args, struct sample_conv *conv,
2598 const char *file, int line, char **err)
2599{
2600 const char *str;
2601 const char *end;
Christopher Faulet95917132020-08-05 23:07:07 +02002602 long long int i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002603
2604 /* Try to decode a variable. */
2605 if (vars_check_arg(&args[0], NULL))
2606 return 1;
2607
2608 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002609 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002610 end = str + strlen(str);
Christopher Faulet95917132020-08-05 23:07:07 +02002611 i = read_int64(&str, end);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002612 if (*str != '\0') {
2613 memprintf(err, "expects an integer or a variable name");
2614 return 0;
2615 }
Christopher Faulet95917132020-08-05 23:07:07 +02002616
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002617 chunk_destroy(&args[0].data.str);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002618 args[0].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02002619 args[0].data.sint = i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002620 return 1;
2621}
2622
Willy Tarreau6204cd92016-03-10 16:33:04 +01002623/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002624 * If the arg contain an integer, the integer is returned in the
2625 * sample. If the arg contains a variable descriptor, it returns the
2626 * variable value.
2627 *
2628 * This function returns 0 if an error occurs, otherwise it returns 1.
2629 */
Willy Tarreaud9be5992021-10-06 15:19:05 +02002630int sample_conv_var2smp_sint(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002631{
2632 switch (arg->type) {
2633 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002634 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002635 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002636 return 1;
2637 case ARGT_VAR:
Willy Tarreau2476ff12021-10-06 15:30:52 +02002638 return sample_conv_var2smp(&arg->data.var, smp, SMP_T_SINT);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002639 default:
2640 return 0;
2641 }
2642}
2643
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002644/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002645 * result.
2646 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002647static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002648{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002649 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002650 return 1;
2651}
2652
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002653/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002654 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002655 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002656static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002657{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002658 struct sample tmp;
2659
Willy Tarreau7560dd42016-03-10 16:28:58 +01002660 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002661 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002662 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002663 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002664 return 1;
2665}
2666
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002667/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002668 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002669 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002670static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002671{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002672 struct sample tmp;
2673
Willy Tarreau7560dd42016-03-10 16:28:58 +01002674 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002675 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002676 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002677 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002678 return 1;
2679}
2680
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002681/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002682 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002683 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002684static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002685{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002686 struct sample tmp;
2687
Willy Tarreau7560dd42016-03-10 16:28:58 +01002688 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002689 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002690 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002691 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002692 return 1;
2693}
2694
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002695static inline long long int arith_add(long long int a, long long int b)
2696{
2697 /* Prevent overflow and makes capped calculus.
2698 * We must ensure that the check calculus doesn't
2699 * exceed the signed 64 bits limits.
2700 *
2701 * +----------+----------+
2702 * | a<0 | a>=0 |
2703 * +------+----------+----------+
2704 * | b<0 | MIN-a>b | no check |
2705 * +------+----------+----------+
2706 * | b>=0 | no check | MAX-a<b |
2707 * +------+----------+----------+
2708 */
2709 if ((a ^ b) >= 0) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002710 /* signs are different. */
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002711 if (a < 0) {
2712 if (LLONG_MIN - a > b)
2713 return LLONG_MIN;
2714 }
2715 if (LLONG_MAX - a < b)
2716 return LLONG_MAX;
2717 }
2718 return a + b;
2719}
2720
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002721/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002722 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002723 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002724static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002725{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002726 struct sample tmp;
2727
Willy Tarreau7560dd42016-03-10 16:28:58 +01002728 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002729 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002730 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002731 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002732 return 1;
2733}
2734
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002735/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002736 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002737 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002738static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002739 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002740{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002741 struct sample tmp;
2742
Willy Tarreau7560dd42016-03-10 16:28:58 +01002743 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002744 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002745 return 0;
2746
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002747 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2748 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2749 * of -LLONG_MIN and correct the result.
2750 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002751 if (tmp.data.u.sint == LLONG_MIN) {
2752 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2753 if (smp->data.u.sint < LLONG_MAX)
2754 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002755 return 1;
2756 }
2757
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002758 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002759 * the second operand.
2760 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002761 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002762 return 1;
2763}
2764
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002765/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002766 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002767 * If the result makes an overflow, then the largest possible quantity is
2768 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002769 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002770static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002771 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002772{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002773 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002774 long long int c;
2775
Willy Tarreau7560dd42016-03-10 16:28:58 +01002776 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002777 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002778 return 0;
2779
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002780 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002781 if (!smp->data.u.sint || !tmp.data.u.sint) {
2782 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002783 return 1;
2784 }
2785
2786 /* The multiply between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002787 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002788 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002789 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2790 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002791 return 1;
2792 }
2793
2794 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002795 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002796
2797 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002798 if (smp->data.u.sint != c / tmp.data.u.sint) {
2799 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2800 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002801 return 1;
2802 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002803 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002804 return 1;
2805 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002806 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002807 return 1;
2808}
2809
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002810/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002811 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002812 * If arg_p makes the result overflow, then the largest possible quantity is
2813 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002814 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002815static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002816 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002817{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002818 struct sample tmp;
2819
Willy Tarreau7560dd42016-03-10 16:28:58 +01002820 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002821 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002822 return 0;
2823
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002824 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002825 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002826 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002827 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002828 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2829 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002830 return 1;
2831 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002832 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002833 return 1;
2834 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002835 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002836 return 1;
2837}
2838
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002839/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002840 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002841 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002842 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002843static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002844 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002845{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002846 struct sample tmp;
2847
Willy Tarreau7560dd42016-03-10 16:28:58 +01002848 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002849 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002850 return 0;
2851
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002852 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002853 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002854 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002855 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002856 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2857 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002858 return 1;
2859 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002860 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002861 return 1;
2862 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002863 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002864 return 1;
2865}
2866
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002867/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002868 * result.
2869 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002870static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002871 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002872{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002873 if (smp->data.u.sint == LLONG_MIN)
2874 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002875 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002876 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002877 return 1;
2878}
2879
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002880/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002881 * false. The output is a BOOL.
2882 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002883static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002884 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002885{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002886 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002887 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002888 return 1;
2889}
2890
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002891/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002892 * truee. The output is a BOOL.
2893 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002894static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002895 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002896{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002897 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002898 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002899 return 1;
2900}
2901
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002902/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002903 * The output is a BOOL.
2904 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002905static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002906 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002907{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002908 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002909 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002910 return 1;
2911}
2912
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002913/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002914 * The output is a BOOL.
2915 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002916static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002917 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002918{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002919 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002920 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002921 return 1;
2922}
2923
Willy Tarreau280f42b2018-02-19 15:34:12 +01002924/* appends an optional const string, an optional variable contents and another
2925 * optional const string to an existing string.
2926 */
2927static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
2928{
Willy Tarreau83061a82018-07-13 11:56:34 +02002929 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002930 struct sample tmp;
2931 int max;
2932
Willy Tarreau591fc3a2021-01-08 16:08:43 +01002933 trash = alloc_trash_chunk();
William Dauchye9970102021-01-11 11:05:58 +01002934 if (!trash)
2935 return 0;
2936
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002937 trash->data = smp->data.u.str.data;
2938 if (trash->data > trash->size - 1)
2939 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002940
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002941 memcpy(trash->area, smp->data.u.str.area, trash->data);
2942 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002943
2944 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002945 max = arg_p[0].data.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, arg_p[0].data.str.area, max);
2951 trash->data += max;
2952 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002953 }
2954
2955 /* append second string (variable) if it's found and we can turn it
2956 * into a string.
2957 */
2958 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaue352b9d2021-09-03 11:52:38 +02002959 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 +01002960 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
2961 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
2962
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002963 max = tmp.data.u.str.data;
2964 if (max > trash->size - 1 - trash->data)
2965 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002966
2967 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002968 memcpy(trash->area + trash->data, tmp.data.u.str.area,
2969 max);
2970 trash->data += max;
2971 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002972 }
2973 }
2974
2975 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002976 max = arg_p[2].data.str.data;
2977 if (max > trash->size - 1 - trash->data)
2978 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002979
2980 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002981 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
2982 trash->data += max;
2983 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002984 }
2985
2986 smp->data.u.str = *trash;
2987 smp->data.type = SMP_T_STR;
Willy Tarreau591fc3a2021-01-08 16:08:43 +01002988 smp_dup(smp);
2989 free_trash_chunk(trash);
Willy Tarreau280f42b2018-02-19 15:34:12 +01002990 return 1;
2991}
2992
2993/* This function checks the "concat" converter's arguments and extracts the
2994 * variable name and its scope.
2995 */
2996static int smp_check_concat(struct arg *args, struct sample_conv *conv,
2997 const char *file, int line, char **err)
2998{
2999 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003000 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
3001 memprintf(err, "failed to register variable name '%s'",
3002 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003003 return 0;
3004 }
3005 return 1;
3006}
3007
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003008/* Compares string with a variable containing a string. Return value
Tim Duesterhusca097c12018-04-27 21:18:45 +02003009 * is compatible with strcmp(3)'s return value.
3010 */
3011static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
3012{
3013 struct sample tmp;
3014 int max, result;
3015
3016 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3017 if (arg_p[0].type != ARGT_VAR)
3018 return 0;
Willy Tarreau2476ff12021-10-06 15:30:52 +02003019
3020 if (!sample_conv_var2smp(&arg_p[0].data.var, &tmp, SMP_T_STR))
Tim Duesterhusca097c12018-04-27 21:18:45 +02003021 return 0;
3022
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003023 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
3024 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003025 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003026 if (smp->data.u.str.data != tmp.data.u.str.data) {
3027 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02003028 result = -1;
3029 }
3030 else {
3031 result = 1;
3032 }
3033 }
3034 }
3035
3036 smp->data.u.sint = result;
3037 smp->data.type = SMP_T_SINT;
3038 return 1;
3039}
3040
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02003041/* Takes a boolean as input. Returns the first argument if that boolean is true and
3042 * the second argument otherwise.
3043 */
3044static int sample_conv_iif(const struct arg *arg_p, struct sample *smp, void *private)
3045{
3046 smp->data.type = SMP_T_STR;
3047 smp->flags |= SMP_F_CONST;
3048
3049 if (smp->data.u.sint) {
3050 smp->data.u.str.data = arg_p[0].data.str.data;
3051 smp->data.u.str.area = arg_p[0].data.str.area;
3052 }
3053 else {
3054 smp->data.u.str.data = arg_p[1].data.str.data;
3055 smp->data.u.str.area = arg_p[1].data.str.area;
3056 }
3057
3058 return 1;
3059}
3060
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003061#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
3062#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
3063#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
3064
3065/*
3066 * Extract the field value of an input binary sample. Takes a mandatory argument:
3067 * the protocol buffers field identifier (dotted notation) internally represented
3068 * as an array of unsigned integers and its size.
3069 * Return 1 if the field was found, 0 if not.
3070 */
3071static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
3072{
3073 unsigned char *pos;
3074 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003075
3076 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003077 grpc_left = smp->data.u.str.data;
3078
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01003079 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003080 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003081
3082 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
3083
3084 pos += GRPC_MSG_HEADER_SZ;
3085 grpc_left -= GRPC_MSG_HEADER_SZ;
3086
3087 if (grpc_left < left)
3088 return 0;
3089
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01003090 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
3091 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003092
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003093 grpc_left -= grpc_msg_len;
3094 }
3095
3096 return 0;
3097}
3098
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003099static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
3100{
3101 unsigned char *pos;
3102 size_t left;
3103
3104 pos = (unsigned char *)smp->data.u.str.area;
3105 left = smp->data.u.str.data;
3106
3107 return protobuf_field_lookup(arg_p, smp, &pos, &left);
3108}
3109
3110static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
3111 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003112{
3113 if (!args[1].type) {
3114 args[1].type = ARGT_SINT;
3115 args[1].data.sint = PBUF_T_BINARY;
3116 }
3117 else {
3118 int pbuf_type;
3119
3120 pbuf_type = protobuf_type(args[1].data.str.area);
3121 if (pbuf_type == -1) {
3122 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
3123 return 0;
3124 }
3125
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003126 chunk_destroy(&args[1].data.str);
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003127 args[1].type = ARGT_SINT;
3128 args[1].data.sint = pbuf_type;
3129 }
3130
3131 return 1;
3132}
3133
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003134/*
3135 * Extract the tag value of an input binary sample. Takes a mandatory argument:
3136 * the FIX protocol tag identifier.
3137 * Return 1 if the tag was found, 0 if not.
3138 */
3139static int sample_conv_fix_tag_value(const struct arg *arg_p, struct sample *smp, void *private)
3140{
3141 struct ist value;
3142
3143 smp->flags &= ~SMP_F_MAY_CHANGE;
3144 value = fix_tag_value(ist2(smp->data.u.str.area, smp->data.u.str.data),
3145 arg_p[0].data.sint);
3146 if (!istlen(value)) {
Christopher Fauleta5aa0822021-10-13 17:58:53 +02003147 if (isttest(value)) {
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003148 /* value != IST_NULL, need more data */
3149 smp->flags |= SMP_F_MAY_CHANGE;
3150 }
3151 return 0;
3152 }
3153
3154 smp->data.u.str = ist2buf(value);
3155 smp->flags |= SMP_F_CONST;
3156
3157 return 1;
3158}
3159
3160/* This function checks the "fix_tag_value" converter configuration.
3161 * It expects a "known" (by HAProxy) tag name or ID.
3162 * Tag string names are converted to their ID counterpart because this is the
3163 * format they are sent over the wire.
3164 */
3165static int sample_conv_fix_value_check(struct arg *args, struct sample_conv *conv,
3166 const char *file, int line, char **err)
3167{
3168 struct ist str;
3169 unsigned int tag;
3170
3171 str = ist2(args[0].data.str.area, args[0].data.str.data);
3172 tag = fix_tagid(str);
3173 if (!tag) {
3174 memprintf(err, "Unknown FIX tag name '%s'", args[0].data.str.area);
3175 return 0;
3176 }
3177
3178 chunk_destroy(&args[0].data.str);
3179 args[0].type = ARGT_SINT;
3180 args[0].data.sint = tag;
3181
3182 return 1;
3183}
3184
3185/*
3186 * Checks that a buffer contains a valid FIX message
3187 *
3188 * Return 1 if the check could be run, 0 if not.
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003189 * The result of the analyse itself is stored in <smp> as a boolean
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003190 */
3191static int sample_conv_fix_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3192{
3193 struct ist msg;
3194
3195 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3196
3197 smp->flags &= ~SMP_F_MAY_CHANGE;
3198 switch (fix_validate_message(msg)) {
3199 case FIX_VALID_MESSAGE:
3200 smp->data.type = SMP_T_BOOL;
3201 smp->data.u.sint = 1;
3202 return 1;
3203 case FIX_NEED_MORE_DATA:
3204 smp->flags |= SMP_F_MAY_CHANGE;
3205 return 0;
3206 case FIX_INVALID_MESSAGE:
3207 smp->data.type = SMP_T_BOOL;
3208 smp->data.u.sint = 0;
3209 return 1;
3210 }
3211 return 0;
3212}
3213
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003214/*
3215 * Extract the field value of an input binary sample containing an MQTT packet.
3216 * Takes 2 mandatory arguments:
3217 * - packet type
3218 * - field name
3219 *
3220 * return 1 if the field was found, 0 if not.
3221 */
3222static int sample_conv_mqtt_field_value(const struct arg *arg_p, struct sample *smp, void *private)
3223{
3224 struct ist pkt, value;
3225 int type, fieldname_id;
3226
3227 pkt = ist2(smp->data.u.str.area, smp->data.u.str.data);
3228 type = arg_p[0].data.sint;
3229 fieldname_id = arg_p[1].data.sint;
3230
3231 smp->flags &= ~SMP_F_MAY_CHANGE;
3232 value = mqtt_field_value(pkt, type, fieldname_id);
3233 if (!istlen(value)) {
3234 if (isttest(value)) {
3235 /* value != IST_NULL, need more data */
3236 smp->flags |= SMP_F_MAY_CHANGE;
3237 }
3238 return 0;
3239 }
3240
3241 smp->data.u.str = ist2buf(value);
3242 smp->flags |= SMP_F_CONST;
3243 return 1;
3244}
3245
3246/*
3247 * this function checks the "mqtt_field_value" converter configuration.
3248 * It expects a known packet type name or ID and a field name, in this order
3249 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003250 * Args[0] will be turned into a MQTT_CPT_* value for direct matching when parsing
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003251 * a packet.
3252 */
3253static int sample_conv_mqtt_field_value_check(struct arg *args, struct sample_conv *conv,
3254 const char *file, int line, char **err)
3255{
3256 int type, fieldname_id;
3257
3258 /* check the MQTT packet type is valid */
3259 type = mqtt_typeid(ist2(args[0].data.str.area, args[0].data.str.data));
3260 if (type == MQTT_CPT_INVALID) {
3261 memprintf(err, "Unknown MQTT type '%s'", args[0].data.str.area);
3262 return 0;
3263 }
3264
3265 /* check the field name belongs to the MQTT packet type */
3266 fieldname_id = mqtt_check_type_fieldname(type, ist2(args[1].data.str.area, args[1].data.str.data));
3267 if (fieldname_id == MQTT_FN_INVALID) {
3268 memprintf(err, "Unknown MQTT field name '%s' for packet type '%s'", args[1].data.str.area,
3269 args[0].data.str.area);
3270 return 0;
3271 }
3272
3273 /* save numeric counterparts of type and field name */
3274 chunk_destroy(&args[0].data.str);
3275 chunk_destroy(&args[1].data.str);
3276 args[0].type = ARGT_SINT;
3277 args[0].data.sint = type;
3278 args[1].type = ARGT_SINT;
3279 args[1].data.sint = fieldname_id;
3280
3281 return 1;
3282}
3283
3284/*
3285 * Checks that <smp> contains a valid MQTT message
3286 *
3287 * The function returns 1 if the check was run to its end, 0 otherwise.
3288 * The result of the analyse itself is stored in <smp> as a boolean.
3289 */
3290static int sample_conv_mqtt_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3291{
3292 struct ist msg;
3293
3294 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3295
3296 smp->flags &= ~SMP_F_MAY_CHANGE;
3297 switch (mqtt_validate_message(msg, NULL)) {
3298 case FIX_VALID_MESSAGE:
3299 smp->data.type = SMP_T_BOOL;
3300 smp->data.u.sint = 1;
3301 return 1;
3302 case FIX_NEED_MORE_DATA:
3303 smp->flags |= SMP_F_MAY_CHANGE;
3304 return 0;
3305 case FIX_INVALID_MESSAGE:
3306 smp->data.type = SMP_T_BOOL;
3307 smp->data.u.sint = 0;
3308 return 1;
3309 }
3310 return 0;
3311}
3312
Tim Duesterhusca097c12018-04-27 21:18:45 +02003313/* This function checks the "strcmp" converter's arguments and extracts the
3314 * variable name and its scope.
3315 */
3316static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
3317 const char *file, int line, char **err)
3318{
Willy Tarreaua1169b62021-05-08 06:50:28 +02003319 if (!args[0].data.str.data) {
3320 memprintf(err, "missing variable name");
3321 return 0;
3322 }
3323
Tim Duesterhusca097c12018-04-27 21:18:45 +02003324 /* Try to decode a variable. */
3325 if (vars_check_arg(&args[0], NULL))
3326 return 1;
3327
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003328 memprintf(err, "failed to register variable name '%s'",
3329 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003330 return 0;
3331}
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003332
Christopher Faulet4ccc12f2020-04-01 09:08:32 +02003333/**/
3334static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private)
3335{
3336 struct buffer *tmp;
3337 uint32_t n;
3338
3339 n = htonl((uint32_t)smp->data.u.sint);
3340 tmp = get_trash_chunk();
3341
3342 memcpy(b_head(tmp), &n, 4);
3343 b_add(tmp, 4);
3344
3345 smp->data.u.str = *tmp;
3346 smp->data.type = SMP_T_BIN;
3347 return 1;
3348}
3349
Christopher Fauletea159d62020-04-01 16:21:44 +02003350/**/
3351static int sample_conv_cut_crlf(const struct arg *arg_p, struct sample *smp, void *private)
3352{
3353 char *p;
3354 size_t l;
3355
3356 p = smp->data.u.str.area;
3357 for (l = 0; l < smp->data.u.str.data; l++) {
3358 if (*(p+l) == '\r' || *(p+l) == '\n')
3359 break;
3360 }
3361 smp->data.u.str.data = l;
3362 return 1;
3363}
3364
Christopher Faulet51fc9d12020-04-01 17:24:41 +02003365/**/
3366static int sample_conv_ltrim(const struct arg *arg_p, struct sample *smp, void *private)
3367{
3368 char *delimiters, *p;
3369 size_t dlen, l;
3370
3371 delimiters = arg_p[0].data.str.area;
3372 dlen = arg_p[0].data.str.data;
3373
3374 l = smp->data.u.str.data;
3375 p = smp->data.u.str.area;
3376 while (l && memchr(delimiters, *p, dlen) != NULL) {
3377 p++;
3378 l--;
3379 }
3380
3381 smp->data.u.str.area = p;
3382 smp->data.u.str.data = l;
3383 return 1;
3384}
3385
Christopher Faulet568415a2020-04-01 17:24:47 +02003386/**/
3387static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *private)
3388{
3389 char *delimiters, *p;
3390 size_t dlen, l;
3391
3392 delimiters = arg_p[0].data.str.area;
3393 dlen = arg_p[0].data.str.data;
3394
3395 l = smp->data.u.str.data;
3396 p = smp->data.u.str.area + l - 1;
3397 while (l && memchr(delimiters, *p, dlen) != NULL) {
3398 p--;
3399 l--;
3400 }
3401
3402 smp->data.u.str.data = l;
3403 return 1;
3404}
3405
Alex51c8ad42021-04-15 16:45:15 +02003406/* This function checks the "json_query" converter's arguments. */
3407static int sample_check_json_query(struct arg *arg, struct sample_conv *conv,
3408 const char *file, int line, char **err)
3409{
3410 if (arg[0].data.str.data == 0) {
3411 memprintf(err, "json_path must not be empty");
3412 return 0;
3413 }
3414
3415 if (arg[1].data.str.data != 0) {
3416 if (strcmp(arg[1].data.str.area, "int") != 0) {
3417 memprintf(err, "output_type only supports \"int\" as argument");
3418 return 0;
3419 } else {
3420 arg[1].type = ARGT_SINT;
3421 arg[1].data.sint = 0;
3422 }
3423 }
3424 return 1;
3425}
3426
3427/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
3428 * the recommendation for interoperable integers in section 6 of RFC 7159.
3429 */
3430#define JSON_INT_MAX ((1LL << 53) - 1)
3431#define JSON_INT_MIN (-JSON_INT_MAX)
3432
3433/* This sample function get the value from a given json string.
3434 * The mjson library is used to parse the JSON struct
3435 */
3436static int sample_conv_json_query(const struct arg *args, struct sample *smp, void *private)
3437{
3438 struct buffer *trash = get_trash_chunk();
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003439 const char *token; /* holds the temporary string from mjson_find */
3440 int token_size; /* holds the length of <token> */
Alex51c8ad42021-04-15 16:45:15 +02003441
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003442 enum mjson_tok token_type;
Alex51c8ad42021-04-15 16:45:15 +02003443
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003444 token_type = mjson_find(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &token, &token_size);
3445
3446 switch (token_type) {
Alex51c8ad42021-04-15 16:45:15 +02003447 case MJSON_TOK_NUMBER:
3448 if (args[1].type == ARGT_SINT) {
Willy Tarreaufb601952021-05-14 08:51:53 +02003449 smp->data.u.sint = strtoll(token, NULL, 0);
Alex51c8ad42021-04-15 16:45:15 +02003450
3451 if (smp->data.u.sint < JSON_INT_MIN || smp->data.u.sint > JSON_INT_MAX)
3452 return 0;
3453
3454 smp->data.type = SMP_T_SINT;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003455
3456 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003457 } else {
3458 double double_val;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003459
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003460 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 +02003461 return 0;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003462
3463 trash->data = snprintf(trash->area,trash->size,"%g",double_val);
3464 smp->data.u.str = *trash;
3465 smp->data.type = SMP_T_STR;
3466
3467 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003468 }
Alex51c8ad42021-04-15 16:45:15 +02003469 case MJSON_TOK_TRUE:
3470 smp->data.type = SMP_T_BOOL;
3471 smp->data.u.sint = 1;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003472
3473 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003474 case MJSON_TOK_FALSE:
3475 smp->data.type = SMP_T_BOOL;
3476 smp->data.u.sint = 0;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003477
3478 return 1;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003479 case MJSON_TOK_STRING: {
3480 int len;
3481
3482 len = mjson_get_string(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, trash->area, trash->size);
3483
3484 if (len == -1) {
Alex51c8ad42021-04-15 16:45:15 +02003485 /* invalid string */
3486 return 0;
Alex51c8ad42021-04-15 16:45:15 +02003487 }
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003488
3489 trash->data = len;
3490 smp->data.u.str = *trash;
3491 smp->data.type = SMP_T_STR;
3492
3493 return 1;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003494 }
Tim Duesterhus8f3bc8f2021-04-15 18:14:32 +02003495 case MJSON_TOK_NULL:
3496 case MJSON_TOK_ARRAY:
3497 case MJSON_TOK_OBJECT:
3498 /* We cannot handle these. */
3499 return 0;
3500 case MJSON_TOK_INVALID:
3501 /* Nothing matches the query. */
3502 return 0;
3503 case MJSON_TOK_KEY:
3504 /* This is not a valid return value according to the
3505 * mjson documentation, but we handle it to benefit
3506 * from '-Wswitch'.
3507 */
Alex51c8ad42021-04-15 16:45:15 +02003508 return 0;
3509 }
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003510
3511 my_unreachable();
3512 return 0;
Alex51c8ad42021-04-15 16:45:15 +02003513}
3514
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003515#ifdef USE_OPENSSL
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +02003516static int sample_conv_jwt_verify_check(struct arg *args, struct sample_conv *conv,
3517 const char *file, int line, char **err)
3518{
3519 vars_check_arg(&args[0], NULL);
3520 vars_check_arg(&args[1], NULL);
3521
3522 if (args[0].type == ARGT_STR) {
3523 enum jwt_alg alg = jwt_parse_alg(args[0].data.str.area, args[0].data.str.data);
3524
3525 switch(alg) {
3526 case JWT_ALG_DEFAULT:
Tim Duesterhusab896ee2021-10-29 18:06:55 +02003527 memprintf(err, "unknown JWT algorithm: %s", args[0].data.str.area);
3528 return 0;
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +02003529
3530 case JWS_ALG_PS256:
3531 case JWS_ALG_PS384:
3532 case JWS_ALG_PS512:
3533 memprintf(err, "RSASSA-PSS JWS signing not managed yet");
Tim Duesterhusab896ee2021-10-29 18:06:55 +02003534 return 0;
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +02003535
3536 default:
3537 break;
3538 }
3539 }
3540
3541 if (args[1].type == ARGT_STR) {
3542 jwt_tree_load_cert(args[1].data.str.area, args[1].data.str.data, err);
3543 }
3544
3545 return 1;
3546}
3547
3548/* Check that a JWT's signature is correct */
3549static int sample_conv_jwt_verify(const struct arg *args, struct sample *smp, void *private)
3550{
3551 struct sample alg_smp, key_smp;
3552
3553 smp->data.type = SMP_T_SINT;
3554 smp->data.u.sint = 0;
3555
3556 smp_set_owner(&alg_smp, smp->px, smp->sess, smp->strm, smp->opt);
3557 smp_set_owner(&key_smp, smp->px, smp->sess, smp->strm, smp->opt);
3558 if (!sample_conv_var2smp_str(&args[0], &alg_smp))
3559 return 0;
3560 if (!sample_conv_var2smp_str(&args[1], &key_smp))
3561 return 0;
3562
3563 smp->data.u.sint = jwt_verify(&smp->data.u.str, &alg_smp.data.u.str,
3564 &key_smp.data.u.str);
3565
3566 return 1;
3567}
3568
3569
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003570/*
3571 * Returns the decoded header or payload of a JWT if no parameter is given, or
3572 * the value of the specified field of the corresponding JWT subpart if a
3573 * parameter is given.
3574 */
3575static int sample_conv_jwt_member_query(const struct arg *args, struct sample *smp,
3576 void *private, enum jwt_elt member)
3577{
3578 struct jwt_item items[JWT_ELT_MAX] = { { 0 } };
3579 unsigned int item_num = member + 1; /* We don't need to tokenize the full token */
3580 struct buffer *decoded_header = get_trash_chunk();
3581 int retval = 0;
Willy Tarreaue20e0262021-10-15 12:10:24 +02003582 int ret;
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003583
3584 jwt_tokenize(&smp->data.u.str, items, &item_num);
3585
3586 if (item_num < member + 1)
3587 goto end;
3588
Willy Tarreaue20e0262021-10-15 12:10:24 +02003589 ret = base64urldec(items[member].start, items[member].length,
3590 decoded_header->area, decoded_header->size);
3591 if (ret == -1)
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003592 goto end;
3593
Willy Tarreaue20e0262021-10-15 12:10:24 +02003594 decoded_header->data = ret;
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003595 if (args[0].type != ARGT_STR) {
3596 smp->data.u.str = *decoded_header;
3597 smp->data.type = SMP_T_STR;
3598 goto end;
3599 }
3600
3601 /* We look for a specific field of the header or payload part of the JWT */
3602 smp->data.u.str = *decoded_header;
3603
3604 retval = sample_conv_json_query(args, smp, private);
3605
3606end:
3607 return retval;
3608}
3609
3610/* This function checks the "jwt_header_query" and "jwt_payload_query" converters' arguments.
3611 * It is based on the "json_query" converter's check with the only difference
3612 * being that the jwt converters can take 0 parameters as well.
3613 */
3614static int sample_conv_jwt_query_check(struct arg *arg, struct sample_conv *conv,
3615 const char *file, int line, char **err)
3616{
3617 if (arg[1].data.str.data != 0) {
3618 if (strcmp(arg[1].data.str.area, "int") != 0) {
3619 memprintf(err, "output_type only supports \"int\" as argument");
3620 return 0;
3621 } else {
3622 arg[1].type = ARGT_SINT;
3623 arg[1].data.sint = 0;
3624 }
3625 }
3626 return 1;
3627}
3628
3629/*
3630 * If no parameter is given, return the decoded header part of a JWT (the first
3631 * base64 encoded part, corresponding to the JOSE header).
3632 * If a parameter is given, this converter acts as a "json_query" on this
3633 * decoded JSON.
3634 */
3635static int sample_conv_jwt_header_query(const struct arg *args, struct sample *smp, void *private)
3636{
3637 return sample_conv_jwt_member_query(args, smp, private, JWT_ELT_JOSE);
3638}
3639
3640/*
3641 * If no parameter is given, return the decoded payload part of a JWT (the
3642 * second base64 encoded part, which contains all the claims). If a parameter
3643 * is given, this converter acts as a "json_query" on this decoded JSON.
3644 */
3645static int sample_conv_jwt_payload_query(const struct arg *args, struct sample *smp, void *private)
3646{
3647 return sample_conv_jwt_member_query(args, smp, private, JWT_ELT_CLAIMS);
3648}
3649
3650#endif /* USE_OPENSSL */
Alex51c8ad42021-04-15 16:45:15 +02003651
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003652/************************************************************************/
3653/* All supported sample fetch functions must be declared here */
3654/************************************************************************/
3655
3656/* force TRUE to be returned at the fetch level */
3657static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003658smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003659{
Willy Tarreau280f42b2018-02-19 15:34:12 +01003660 if (!smp_make_rw(smp))
3661 return 0;
3662
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003663 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003664 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003665 return 1;
3666}
3667
3668/* force FALSE to be returned at the fetch level */
3669static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003670smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003671{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003672 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003673 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003674 return 1;
3675}
3676
3677/* retrieve environment variable $1 as a string */
3678static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003679smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003680{
3681 char *env;
3682
Christopher Faulete6e7a582021-01-29 11:29:28 +01003683 if (args[0].type != ARGT_STR)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003684 return 0;
3685
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003686 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003687 if (!env)
3688 return 0;
3689
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003690 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003691 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003692 smp->data.u.str.area = env;
3693 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003694 return 1;
3695}
3696
Damien Claisseae6f1252019-10-30 15:57:28 +00003697/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3698 * optional string representing the unit of the result: "s" for seconds, "ms" for
3699 * milliseconds and "us" for microseconds.
3700 * Returns 0 on error and non-zero if OK.
3701 */
3702int smp_check_date_unit(struct arg *args, char **err)
3703{
3704 if (args[1].type == ARGT_STR) {
Christopher Faulet95917132020-08-05 23:07:07 +02003705 long long int unit;
3706
Damien Claisseae6f1252019-10-30 15:57:28 +00003707 if (strcmp(args[1].data.str.area, "s") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003708 unit = TIME_UNIT_S;
Damien Claisseae6f1252019-10-30 15:57:28 +00003709 }
3710 else if (strcmp(args[1].data.str.area, "ms") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003711 unit = TIME_UNIT_MS;
Damien Claisseae6f1252019-10-30 15:57:28 +00003712 }
3713 else if (strcmp(args[1].data.str.area, "us") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003714 unit = TIME_UNIT_US;
Damien Claisseae6f1252019-10-30 15:57:28 +00003715 }
3716 else {
3717 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
3718 args[1].data.str.area);
3719 return 0;
3720 }
Christopher Faulet95917132020-08-05 23:07:07 +02003721
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003722 chunk_destroy(&args[1].data.str);
Damien Claisseae6f1252019-10-30 15:57:28 +00003723 args[1].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02003724 args[1].data.sint = unit;
Damien Claisseae6f1252019-10-30 15:57:28 +00003725 }
3726 else if (args[1].type != ARGT_STOP) {
3727 memprintf(err, "Unexpected arg type");
3728 return 0;
3729 }
3730
3731 return 1;
3732}
3733
3734/* retrieve the current local date in epoch time, converts it to milliseconds
3735 * or microseconds if asked to in optional args[1] unit param, and applies an
3736 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003737 */
3738static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003739smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003740{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003741 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003742
Damien Claisseae6f1252019-10-30 15:57:28 +00003743 /* report in milliseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003744 if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003745 smp->data.u.sint *= 1000;
3746 smp->data.u.sint += date.tv_usec / 1000;
3747 }
3748 /* report in microseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003749 else if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003750 smp->data.u.sint *= 1000000;
3751 smp->data.u.sint += date.tv_usec;
3752 }
3753
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003754 /* add offset */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003755 if (args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003756 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003757
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003758 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003759 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3760 return 1;
3761}
3762
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003763/* retrieve the current microsecond part of the date */
3764static int
3765smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3766{
3767 smp->data.u.sint = date.tv_usec;
3768 smp->data.type = SMP_T_SINT;
3769 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3770 return 1;
3771}
3772
3773
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003774/* returns the hostname */
3775static int
3776smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3777{
3778 smp->data.type = SMP_T_STR;
3779 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003780 smp->data.u.str.area = hostname;
3781 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003782 return 1;
3783}
3784
Willy Tarreau0f30d262014-11-24 16:02:05 +01003785/* returns the number of processes */
3786static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003787smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003788{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003789 smp->data.type = SMP_T_SINT;
Willy Tarreau91358592021-06-15 08:08:04 +02003790 smp->data.u.sint = 1;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003791 return 1;
3792}
3793
3794/* returns the number of the current process (between 1 and nbproc */
3795static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003796smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003797{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003798 smp->data.type = SMP_T_SINT;
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003799 smp->data.u.sint = 1;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003800 return 1;
3801}
3802
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003803/* returns the number of the current thread (between 1 and nbthread */
3804static int
3805smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3806{
3807 smp->data.type = SMP_T_SINT;
3808 smp->data.u.sint = tid;
3809 return 1;
3810}
3811
Willy Tarreau84310e22014-02-14 11:59:04 +01003812/* generate a random 32-bit integer for whatever purpose, with an optional
3813 * range specified in argument.
3814 */
3815static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003816smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01003817{
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003818 smp->data.u.sint = ha_random32();
Willy Tarreau84310e22014-02-14 11:59:04 +01003819
3820 /* reduce if needed. Don't do a modulo, use all bits! */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003821 if (args[0].type == ARGT_SINT)
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003822 smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32;
Willy Tarreau84310e22014-02-14 11:59:04 +01003823
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003824 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01003825 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3826 return 1;
3827}
3828
Willy Tarreau0f30d262014-11-24 16:02:05 +01003829/* returns true if the current process is stopping */
3830static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003831smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003832{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003833 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003834 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003835 return 1;
3836}
3837
Willy Tarreau70fe9442018-11-22 16:07:39 +01003838/* returns the number of calls of the current stream's process_stream() */
3839static int
3840smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
3841{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003842 if (!smp->strm)
3843 return 0;
3844
Willy Tarreau70fe9442018-11-22 16:07:39 +01003845 smp->data.type = SMP_T_SINT;
3846 smp->data.u.sint = smp->strm->task->calls;
3847 return 1;
3848}
3849
3850/* returns the average number of nanoseconds spent processing the stream per call */
3851static int
3852smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3853{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003854 if (!smp->strm)
3855 return 0;
3856
Willy Tarreau70fe9442018-11-22 16:07:39 +01003857 smp->data.type = SMP_T_SINT;
3858 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
3859 return 1;
3860}
3861
3862/* returns the total number of nanoseconds spent processing the stream */
3863static int
3864smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3865{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003866 if (!smp->strm)
3867 return 0;
3868
Willy Tarreau70fe9442018-11-22 16:07:39 +01003869 smp->data.type = SMP_T_SINT;
3870 smp->data.u.sint = smp->strm->task->cpu_time;
3871 return 1;
3872}
3873
3874/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
3875static int
3876smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3877{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003878 if (!smp->strm)
3879 return 0;
3880
Willy Tarreau70fe9442018-11-22 16:07:39 +01003881 smp->data.type = SMP_T_SINT;
3882 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
3883 return 1;
3884}
3885
3886/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
3887static int
3888smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3889{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003890 if (!smp->strm)
3891 return 0;
3892
Willy Tarreau70fe9442018-11-22 16:07:39 +01003893 smp->data.type = SMP_T_SINT;
3894 smp->data.u.sint = smp->strm->task->lat_time;
3895 return 1;
3896}
3897
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003898static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
3899{
3900 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003901 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003902 smp->data.u.str.area = args[0].data.str.area;
3903 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003904 return 1;
3905}
3906
3907static int smp_check_const_bool(struct arg *args, char **err)
3908{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003909 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
3910 strcasecmp(args[0].data.str.area, "1") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003911 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003912 args[0].type = ARGT_SINT;
3913 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003914 return 1;
3915 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003916 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
3917 strcasecmp(args[0].data.str.area, "0") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003918 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003919 args[0].type = ARGT_SINT;
3920 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003921 return 1;
3922 }
3923 memprintf(err, "Expects 'true', 'false', '0' or '1'");
3924 return 0;
3925}
3926
3927static int smp_fetch_const_bool(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_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003930 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003931 return 1;
3932}
3933
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003934static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003935{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003936 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003937 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003938 return 1;
3939}
3940
3941static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
3942{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003943 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003944 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003945 return 1;
3946}
3947
3948static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
3949{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003950 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003951 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003952 return 1;
3953}
3954
3955static int smp_check_const_bin(struct arg *args, char **err)
3956{
David Carlier64a16ab2016-04-08 10:37:02 +01003957 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003958 int binstrlen;
3959
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003960 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003961 return 0;
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003962 chunk_destroy(&args[0].data.str);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003963 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003964 args[0].data.str.area = binstr;
3965 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003966 return 1;
3967}
3968
3969static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
3970{
3971 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003972 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003973 smp->data.u.str.area = args[0].data.str.area;
3974 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003975 return 1;
3976}
3977
3978static int smp_check_const_meth(struct arg *args, char **err)
3979{
3980 enum http_meth_t meth;
3981 int i;
3982
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003983 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003984 if (meth != HTTP_METH_OTHER) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003985 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003986 args[0].type = ARGT_SINT;
3987 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003988 } else {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003989 /* Check method avalaibility. A method is a token defined as :
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003990 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
3991 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
3992 * token = 1*tchar
3993 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003994 for (i = 0; i < args[0].data.str.data; i++) {
3995 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003996 memprintf(err, "expects valid method.");
3997 return 0;
3998 }
3999 }
4000 }
4001 return 1;
4002}
4003
4004static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
4005{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004006 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004007 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004008 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004009 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004010 smp->data.u.meth.str.area = "";
4011 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004012 } else {
4013 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004014 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004015 smp->data.u.meth.str.area = args[0].data.str.area;
4016 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004017 }
4018 return 1;
4019}
4020
Luca Schimweg8a694b82019-09-10 15:42:52 +02004021// This function checks the "uuid" sample's arguments.
4022// Function won't get called when no parameter is specified (maybe a bug?)
4023static int smp_check_uuid(struct arg *args, char **err)
4024{
4025 if (!args[0].type) {
4026 args[0].type = ARGT_SINT;
4027 args[0].data.sint = 4;
4028 }
4029 else if (args[0].data.sint != 4) {
4030 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
4031 return 0;
4032 }
4033
4034 return 1;
4035}
4036
4037// Generate a RFC4122 UUID (default is v4 = fully random)
4038static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
4039{
4040 if (args[0].data.sint == 4 || !args[0].type) {
Willy Tarreauee3bcdd2020-03-08 17:48:17 +01004041 ha_generate_uuid(&trash);
Luca Schimweg8a694b82019-09-10 15:42:52 +02004042 smp->data.type = SMP_T_STR;
4043 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4044 smp->data.u.str = trash;
4045 return 1;
4046 }
4047
4048 // more implementations of other uuid formats possible here
4049 return 0;
4050}
4051
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004052/* Note: must not be declared <const> as its list will be overwritten.
4053 * Note: fetches that may return multiple types must be declared as the lowest
4054 * common denominator, the type that can be casted into all other ones. For
4055 * instance IPv4/IPv6 must be declared IPv4.
4056 */
4057static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau0209c972021-03-26 12:03:11 +01004058 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4059 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4060 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_CONST },
4061 { "date", smp_fetch_date, ARG2(0,SINT,STR), smp_check_date_unit, SMP_T_SINT, SMP_USE_CONST },
4062 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4063 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_CONST },
4064 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_CONST },
4065 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4066 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4067 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_CONST },
Willy Tarreau0f30d262014-11-24 16:02:05 +01004068 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau0209c972021-03-26 12:03:11 +01004069 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_CONST },
Willy Tarreau70fe9442018-11-22 16:07:39 +01004070
4071 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4072 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4073 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4074 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4075 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004076
Willy Tarreau0209c972021-03-26 12:03:11 +01004077 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_CONST },
4078 { "bool", smp_fetch_const_bool, ARG1(1,STR), smp_check_const_bool, SMP_T_BOOL, SMP_USE_CONST },
4079 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_CONST },
4080 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_CONST },
4081 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_CONST },
4082 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_CONST },
4083 { "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 +02004084
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004085 { /* END */ },
4086}};
4087
Willy Tarreau0108d902018-11-25 19:14:37 +01004088INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
4089
Emeric Brun107ca302010-01-04 16:16:05 +01004090/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02004091static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Moemen MHEDHBI848216f2021-04-02 01:05:07 +02004092 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
4093 { "b64dec", sample_conv_base642bin, 0, NULL, SMP_T_STR, SMP_T_BIN },
4094 { "base64", sample_conv_bin2base64, 0, NULL, SMP_T_BIN, SMP_T_STR },
4095 { "ub64enc", sample_conv_bin2base64url,0, NULL, SMP_T_BIN, SMP_T_STR },
4096 { "ub64dec", sample_conv_base64url2bin,0, NULL, SMP_T_STR, SMP_T_BIN },
4097 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
4098 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
4099 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Marcin Deranek40ca09c2021-07-13 14:05:24 +02004100 { "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 +02004101 { "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 +02004102 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
4103 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
4104 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
4105 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4106 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4107 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4108 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4109 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4110 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4111 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4112 { "xxh3", sample_conv_xxh3, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4113 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4114 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4115 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
4116 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
4117 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4118 { "word", sample_conv_word, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4119 { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
4120 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Willy Tarreau99ea1882021-10-06 15:37:17 +02004121 { "concat", sample_conv_concat, ARG3(1,STR,STR,STR), smp_check_concat, SMP_T_STR, SMP_T_STR },
4122 { "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 +01004123
4124 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01004125 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
4126 { "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 +01004127
Baptiste Assmanne138dda2020-10-22 15:39:03 +02004128 /* FIX converters */
4129 { "fix_is_valid", sample_conv_fix_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4130 { "fix_tag_value", sample_conv_fix_tag_value, ARG1(1,STR), sample_conv_fix_value_check, SMP_T_BIN, SMP_T_BIN },
4131
Baptiste Assmanne279ca62020-10-27 18:10:06 +01004132 /* MQTT converters */
4133 { "mqtt_is_valid", sample_conv_mqtt_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4134 { "mqtt_field_value", sample_conv_mqtt_field_value, ARG2(2,STR,STR), sample_conv_mqtt_field_value_check, SMP_T_BIN, SMP_T_STR },
4135
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02004136 { "iif", sample_conv_iif, ARG2(2, STR, STR), NULL, SMP_T_BOOL, SMP_T_STR },
4137
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02004138 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4139 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4140 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4141 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
4142 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4143 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4144 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4145 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4146 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4147 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4148 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4149 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4150 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4151 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01004152
Christopher Fauletea159d62020-04-01 16:21:44 +02004153 { "htonl", sample_conv_htonl, 0, NULL, SMP_T_SINT, SMP_T_BIN },
4154 { "cut_crlf", sample_conv_cut_crlf, 0, NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet51fc9d12020-04-01 17:24:41 +02004155 { "ltrim", sample_conv_ltrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet568415a2020-04-01 17:24:47 +02004156 { "rtrim", sample_conv_rtrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Alex51c8ad42021-04-15 16:45:15 +02004157 { "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 +02004158
4159#ifdef USE_OPENSSL
4160 /* JSON Web Token converters */
4161 { "jwt_header_query", sample_conv_jwt_header_query, ARG2(0,STR,STR), sample_conv_jwt_query_check, SMP_T_BIN, SMP_T_ANY },
4162 { "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 +02004163 { "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 +02004164#endif
Willy Tarreau9fcb9842012-04-20 14:45:49 +02004165 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01004166}};
4167
Willy Tarreau0108d902018-11-25 19:14:37 +01004168INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);