blob: f43a79ed31473f607911fe55434805eab5143456 [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
Willy Tarreauf78813f2022-03-29 16:51:29 +0200437/* dump list of registered sample fetch keywords on stdout */
438void smp_dump_fetch_kw(void)
439{
440 struct sample_fetch_kw_list *kwl;
Willy Tarreau5e0f90a2022-03-30 11:30:36 +0200441 struct sample_fetch *kwp, *kw;
Willy Tarreauf78813f2022-03-29 16:51:29 +0200442 uint64_t mask;
443 int index;
444 int arg;
445 int bit;
446
447 for (bit = 0; bit <= SMP_CKP_ENTRIES + 1; bit++) {
448 putchar('#');
449 for (index = 0; bit + index <= SMP_CKP_ENTRIES; index++)
450 putchar(' ');
451 for (index = 0; index < bit && index < SMP_CKP_ENTRIES; index++)
452 printf((bit <= SMP_CKP_ENTRIES) ? "/ " : " |");
453 for (index = bit; bit < SMP_CKP_ENTRIES && index < SMP_CKP_ENTRIES + 2; index++)
454 if (index == bit)
455 putchar('_');
456 else if (index == bit + 1)
457 putchar('.');
458 else
459 putchar('-');
460 printf(" %s\n", (bit < SMP_CKP_ENTRIES) ? fetch_ckp_names[bit] : "");
461 }
462
Willy Tarreau5e0f90a2022-03-30 11:30:36 +0200463 for (kw = kwp = NULL;; kwp = kw) {
464 list_for_each_entry(kwl, &sample_fetches.list, list) {
465 for (index = 0; kwl->kw[index].kw != NULL; index++) {
466 if (strordered(kwp ? kwp->kw : NULL,
467 kwl->kw[index].kw,
468 kw != kwp ? kw->kw : NULL))
469 kw = &kwl->kw[index];
470 }
471 }
Willy Tarreauf78813f2022-03-29 16:51:29 +0200472
Willy Tarreau5e0f90a2022-03-30 11:30:36 +0200473 if (kw == kwp)
474 break;
Willy Tarreauf78813f2022-03-29 16:51:29 +0200475
Willy Tarreau5e0f90a2022-03-30 11:30:36 +0200476 printf("[ ");
477 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
478 printf("%s", (kw->val & (1 << bit)) ? "Y " : ". ");
479
480 printf("] %s", kw->kw);
481 if (kw->arg_mask) {
482 mask = kw->arg_mask >> ARGM_BITS;
483 printf("(");
484 for (arg = 0;
485 arg < ARGM_NBARGS && ((mask >> (arg * ARGT_BITS)) & ARGT_MASK);
486 arg++) {
487 if (arg == (kw->arg_mask & ARGM_MASK)) {
488 /* now dumping extra args */
489 printf("[");
Willy Tarreauf78813f2022-03-29 16:51:29 +0200490 }
Willy Tarreau5e0f90a2022-03-30 11:30:36 +0200491 if (arg)
492 printf(",");
493 printf("%s", arg_type_names[(mask >> (arg * ARGT_BITS)) & ARGT_MASK]);
Willy Tarreauf78813f2022-03-29 16:51:29 +0200494 }
Willy Tarreau5e0f90a2022-03-30 11:30:36 +0200495 if (arg > (kw->arg_mask & ARGM_MASK)) {
496 /* extra args were dumped */
497 printf("]");
498 }
499 printf(")");
Willy Tarreauf78813f2022-03-29 16:51:29 +0200500 }
Willy Tarreau5e0f90a2022-03-30 11:30:36 +0200501 printf(": %s", smp_to_type[kw->out_type]);
502 printf("\n");
Willy Tarreauf78813f2022-03-29 16:51:29 +0200503 }
504}
505
Willy Tarreau29d799d2022-03-29 16:59:49 +0200506/* dump list of registered sample converter keywords on stdout */
507void smp_dump_conv_kw(void)
508{
509 struct sample_conv_kw_list *kwl;
Willy Tarreau5e0f90a2022-03-30 11:30:36 +0200510 struct sample_conv *kwp, *kw;
Willy Tarreau29d799d2022-03-29 16:59:49 +0200511 uint64_t mask;
512 int index;
513 int arg;
514
Willy Tarreau5e0f90a2022-03-30 11:30:36 +0200515 for (kw = kwp = NULL;; kwp = kw) {
516 list_for_each_entry(kwl, &sample_convs.list, list) {
517 for (index = 0; kwl->kw[index].kw != NULL; index++) {
518 if (strordered(kwp ? kwp->kw : NULL,
519 kwl->kw[index].kw,
520 kw != kwp ? kw->kw : NULL))
521 kw = &kwl->kw[index];
522 }
523 }
524
525 if (kw == kwp)
526 break;
527
528 printf("%s", kw->kw);
529 if (kw->arg_mask) {
530 mask = kw->arg_mask >> ARGM_BITS;
531 printf("(");
532 for (arg = 0;
533 arg < ARGM_NBARGS && ((mask >> (arg * ARGT_BITS)) & ARGT_MASK);
534 arg++) {
535 if (arg == (kw->arg_mask & ARGM_MASK)) {
536 /* now dumping extra args */
537 printf("[");
Willy Tarreau29d799d2022-03-29 16:59:49 +0200538 }
Willy Tarreau5e0f90a2022-03-30 11:30:36 +0200539 if (arg)
540 printf(",");
541 printf("%s", arg_type_names[(mask >> (arg * ARGT_BITS)) & ARGT_MASK]);
Willy Tarreau29d799d2022-03-29 16:59:49 +0200542 }
Willy Tarreau5e0f90a2022-03-30 11:30:36 +0200543 if (arg > (kw->arg_mask & ARGM_MASK)) {
544 /* extra args were dumped */
545 printf("]");
546 }
547 printf(")");
Willy Tarreau29d799d2022-03-29 16:59:49 +0200548 }
Willy Tarreau5e0f90a2022-03-30 11:30:36 +0200549 printf(": %s => %s", smp_to_type[kw->out_type], smp_to_type[kw->in_type]);
550 printf("\n");
Willy Tarreau29d799d2022-03-29 16:59:49 +0200551 }
552}
553
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100554/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100555 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100556 * <idx> is the index of the next sample fetch entry. It is used as private
557 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100558 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100559 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100560 * the list is reached.
561 */
562struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
563{
564 struct sample_fetch_kw_list *kwl;
565 struct sample_fetch *base;
566
567 if (!current) {
568 /* Get first kwl entry. */
569 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
570 (*idx) = 0;
571 } else {
572 /* Get kwl corresponding to the curret entry. */
573 base = current + 1 - (*idx);
574 kwl = container_of(base, struct sample_fetch_kw_list, kw);
575 }
576
577 while (1) {
578
579 /* Check if kwl is the last entry. */
580 if (&kwl->list == &sample_fetches.list)
581 return NULL;
582
583 /* idx contain the next keyword. If it is available, return it. */
584 if (kwl->kw[*idx].kw) {
585 (*idx)++;
586 return &kwl->kw[(*idx)-1];
587 }
588
589 /* get next entry in the main list, and return NULL if the end is reached. */
590 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
591
592 /* Set index to 0, ans do one other loop. */
593 (*idx) = 0;
594 }
595}
596
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100597/* This function browses the list of available converters. <current> is
598 * the last used converter. If it is the first call, it must set to NULL.
599 * <idx> is the index of the next converter entry. It is used as private
600 * value. It is useless to initiate it.
601 *
602 * It returns always the next sample_conv entry, and NULL when the end of
603 * the list is reached.
604 */
605struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
606{
607 struct sample_conv_kw_list *kwl;
608 struct sample_conv *base;
609
610 if (!current) {
611 /* Get first kwl entry. */
612 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
613 (*idx) = 0;
614 } else {
615 /* Get kwl corresponding to the curret entry. */
616 base = current + 1 - (*idx);
617 kwl = container_of(base, struct sample_conv_kw_list, kw);
618 }
619
620 while (1) {
621 /* Check if kwl is the last entry. */
622 if (&kwl->list == &sample_convs.list)
623 return NULL;
624
625 /* idx contain the next keyword. If it is available, return it. */
626 if (kwl->kw[*idx].kw) {
627 (*idx)++;
628 return &kwl->kw[(*idx)-1];
629 }
630
631 /* get next entry in the main list, and return NULL if the end is reached. */
632 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
633
634 /* Set index to 0, ans do one other loop. */
635 (*idx) = 0;
636 }
637}
638
Emeric Brun107ca302010-01-04 16:16:05 +0100639/*
Willy Tarreau12785782012-04-27 21:37:17 +0200640 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100641 * string of <len> in buffer <kw>.
642 *
643 */
Willy Tarreau12785782012-04-27 21:37:17 +0200644struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100645{
646 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200647 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100648
Willy Tarreau12785782012-04-27 21:37:17 +0200649 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100650 for (index = 0; kwl->kw[index].kw != NULL; index++) {
651 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
652 kwl->kw[index].kw[len] == '\0')
653 return &kwl->kw[index];
654 }
655 }
656 return NULL;
657}
658
Emeric Brun107ca302010-01-04 16:16:05 +0100659/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200660/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100661/******************************************************************/
662
Willy Tarreau342acb42012-04-23 22:03:39 +0200663static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100664{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200665 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200666 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100667 return 1;
668}
669
Willy Tarreau342acb42012-04-23 22:03:39 +0200670static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100671{
Willy Tarreau83061a82018-07-13 11:56:34 +0200672 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100673
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200674 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100675 return 0;
676
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200677 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200678 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200679 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100680 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100681
682 return 1;
683}
684
Willy Tarreau342acb42012-04-23 22:03:39 +0200685static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100686{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200687 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200688 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100689 return 1;
690}
691
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200692static int c_ipv62ip(struct sample *smp)
693{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200694 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200695 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100696 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200697 return 1;
698}
699
Willy Tarreau342acb42012-04-23 22:03:39 +0200700static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100701{
Willy Tarreau83061a82018-07-13 11:56:34 +0200702 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100703
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200704 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100705 return 0;
706
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200707 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200708 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200709 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100710 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100711 return 1;
712}
713
714/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200715static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100716{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200717 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100718}
719*/
720
Willy Tarreau342acb42012-04-23 22:03:39 +0200721static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100722{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200723 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200724 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100725 return 1;
726}
727
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200728static int c_int2ipv6(struct sample *smp)
729{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200730 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
731 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200732 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200733 return 1;
734}
735
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100736static int c_str2addr(struct sample *smp)
737{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200738 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
739 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100740 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200741 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100742 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100743 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100744 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200745 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100746 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100747 return 1;
748}
749
Willy Tarreau342acb42012-04-23 22:03:39 +0200750static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100751{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200752 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100753 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200754 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100755 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100756 return 1;
757}
758
Willy Tarreau342acb42012-04-23 22:03:39 +0200759static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100760{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200761 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100762 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200763 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100764 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100765 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100766}
767
Emeric Brun4b9e8022014-11-03 18:17:10 +0100768/*
769 * The NULL char always enforces the end of string if it is met.
770 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100771 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200772static int c_bin2str(struct sample *smp)
773{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100774 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200775
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200776 for (i = 0; i < smp->data.u.str.data; i++) {
777 if (!smp->data.u.str.area[i]) {
778 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100779 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100780 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200781 }
Christopher Faulet472ad512020-04-30 09:57:40 +0200782 smp->data.type = SMP_T_STR;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200783 return 1;
784}
785
Willy Tarreau342acb42012-04-23 22:03:39 +0200786static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100787{
Willy Tarreau83061a82018-07-13 11:56:34 +0200788 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100789 char *pos;
790
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200791 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100792 if (!pos)
793 return 0;
794
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200795 trash->size = trash->size - (pos - trash->area);
796 trash->area = pos;
797 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200798 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200799 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100800 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100801 return 1;
802}
803
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800804/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200805 * For strings and binary blocks, it also provides a known allocated size with
806 * a length that is capped to the size, and ensures a trailing zero is always
807 * appended for strings. This is necessary for some operations which may
808 * require to extend the length. It returns 0 if it fails, 1 on success.
809 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100810int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200811{
Willy Tarreau83061a82018-07-13 11:56:34 +0200812 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200813
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200814 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100815 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100816 case SMP_T_SINT:
817 case SMP_T_ADDR:
818 case SMP_T_IPV4:
819 case SMP_T_IPV6:
820 /* These type are not const. */
821 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200822
Christopher Fauletec100512017-07-24 15:38:41 +0200823 case SMP_T_METH:
824 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
825 break;
826 /* Fall through */
827
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100828 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200829 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100830 trash->data = smp->data.type == SMP_T_STR ?
831 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200832 if (trash->data > trash->size - 1)
833 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200834
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100835 memcpy(trash->area, smp->data.type == SMP_T_STR ?
836 smp->data.u.str.area : smp->data.u.meth.str.area,
837 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200838 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200839 smp->data.u.str = *trash;
840 break;
841
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100842 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100843 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200844 trash->data = smp->data.u.str.data;
845 if (trash->data > trash->size)
846 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200847
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200848 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200849 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100850 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200851
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100852 default:
853 /* Other cases are unexpected. */
854 return 0;
855 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100856
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100857 /* remove const flag */
858 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200859 return 1;
860}
861
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100862int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100863{
864 return 1;
865}
866
Willy Tarreau342acb42012-04-23 22:03:39 +0200867static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100868{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200869 const char *str;
870 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100871
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200872 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100873 return 0;
874
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200875 str = smp->data.u.str.area;
876 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100877
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200878 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200879 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100880 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100881 return 1;
882}
883
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100884static int c_str2meth(struct sample *smp)
885{
886 enum http_meth_t meth;
887 int len;
888
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200889 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100890 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200891 len = smp->data.u.str.data;
892 smp->data.u.meth.str.area = smp->data.u.str.area;
893 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100894 }
895 else
896 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200897 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200898 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100899 return 1;
900}
901
902static int c_meth2str(struct sample *smp)
903{
904 int len;
905 enum http_meth_t meth;
906
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200907 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100908 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200909 len = smp->data.u.meth.str.data;
910 smp->data.u.str.area = smp->data.u.meth.str.area;
911 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200912 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100913 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200914 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100915 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200916 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200917 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200918 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100919 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200920 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100921 }
922 else {
923 /* Unknown method */
924 return 0;
925 }
926 return 1;
927}
928
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200929static int c_addr2bin(struct sample *smp)
930{
Willy Tarreau83061a82018-07-13 11:56:34 +0200931 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200932
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200933 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200934 chk->data = 4;
935 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200936 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200937 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200938 chk->data = 16;
939 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200940 }
941 else
942 return 0;
943
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200944 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200945 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200946 return 1;
947}
948
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200949static int c_int2bin(struct sample *smp)
950{
Willy Tarreau83061a82018-07-13 11:56:34 +0200951 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200952
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200953 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
954 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200955
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200956 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200957 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200958 return 1;
959}
960
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200961
Emeric Brun107ca302010-01-04 16:16:05 +0100962/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200963/* Sample casts matrix: */
964/* sample_casts[from type][to type] */
965/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100966/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100967
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100968sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200969/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
970/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
971/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200972/* 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 +0200973/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
974/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200975/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200976/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
977/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
978/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200979};
Emeric Brun107ca302010-01-04 16:16:05 +0100980
Emeric Brun107ca302010-01-04 16:16:05 +0100981/*
Willy Tarreau12785782012-04-27 21:37:17 +0200982 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100983 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200984 * Returns a pointer on allocated sample expression structure.
Christopher Faulet35926a12021-09-30 08:48:56 +0200985 * <al> is an arg_list serving as a list head to report missing dependencies.
986 * It may be NULL if such dependencies are not allowed. Otherwise, the caller
987 * must have set al->ctx if al is set.
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100988 * If <endptr> is non-nul, it will be set to the first unparsed character
989 * (which may be the final '\0') on success. If it is nul, the expression
990 * must be properly terminated by a '\0' otherwise an error is reported.
Emeric Brun107ca302010-01-04 16:16:05 +0100991 */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100992struct 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 +0100993{
Willy Tarreau833cc792013-07-24 15:34:19 +0200994 const char *begw; /* beginning of word */
995 const char *endw; /* end of word */
996 const char *endt; /* end of term */
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +0100997 struct sample_expr *expr = NULL;
Willy Tarreau12785782012-04-27 21:37:17 +0200998 struct sample_fetch *fetch;
999 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +01001000 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +02001001 char *fkw = NULL;
1002 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +01001003 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +01001004
Willy Tarreau833cc792013-07-24 15:34:19 +02001005 begw = str[*idx];
Willy Tarreaued2c6622020-02-14 18:27:10 +01001006 for (endw = begw; is_idchar(*endw); endw++)
1007 ;
Willy Tarreau833cc792013-07-24 15:34:19 +02001008
1009 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +01001010 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +01001011 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001012 }
Emeric Brun107ca302010-01-04 16:16:05 +01001013
Willy Tarreau833cc792013-07-24 15:34:19 +02001014 /* keep a copy of the current fetch keyword for error reporting */
1015 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +01001016
Willy Tarreau833cc792013-07-24 15:34:19 +02001017 fetch = find_sample_fetch(begw, endw - begw);
1018 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +01001019 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +01001020 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001021 }
Emeric Brun107ca302010-01-04 16:16:05 +01001022
Willy Tarreau833cc792013-07-24 15:34:19 +02001023 /* At this point, we have :
1024 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +01001025 * - endw : end of the keyword, first character not part of keyword
Willy Tarreau833cc792013-07-24 15:34:19 +02001026 */
1027
1028 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +01001029 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +01001030 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001031 }
Emeric Brun107ca302010-01-04 16:16:05 +01001032 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +02001033
Vincent Bernat02779b62016-04-03 13:48:43 +02001034 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +02001035 if (!expr)
1036 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001037
1038 LIST_INIT(&(expr->conv_exprs));
1039 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001040 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +01001041
Willy Tarreau689a1df2013-12-13 00:40:11 +01001042 /* Note that we call the argument parser even with an empty string,
1043 * this allows it to automatically create entries for mandatory
1044 * implicit arguments (eg: local proxy name).
1045 */
Christopher Faulet35926a12021-09-30 08:48:56 +02001046 if (al) {
1047 al->kw = expr->fetch->kw;
1048 al->conv = NULL;
1049 }
Willy Tarreau80b53ff2020-02-14 08:40:37 +01001050 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 +01001051 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
1052 goto out_error;
1053 }
Willy Tarreau2e845be2012-10-19 19:49:09 +02001054
Willy Tarreau80b53ff2020-02-14 08:40:37 +01001055 /* now endt is our first char not part of the arg list, typically the
1056 * comma after the sample fetch name or after the closing parenthesis,
1057 * or the NUL char.
1058 */
1059
Willy Tarreau689a1df2013-12-13 00:40:11 +01001060 if (!expr->arg_p) {
1061 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +02001062 }
Willy Tarreau689a1df2013-12-13 00:40:11 +01001063 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
1064 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001065 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001066 }
1067
Willy Tarreau833cc792013-07-24 15:34:19 +02001068 /* Now process the converters if any. We have two supported syntaxes
1069 * for the converters, which can be combined :
1070 * - comma-delimited list of converters just after the keyword and args ;
1071 * - one converter per keyword
1072 * The combination allows to have each keyword being a comma-delimited
1073 * series of converters.
1074 *
1075 * We want to process the former first, then the latter. For this we start
1076 * from the beginning of the supposed place in the exiting conv chain, which
1077 * starts at the last comma (endt).
1078 */
1079
1080 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +02001081 struct sample_conv_expr *conv_expr;
Willy Tarreau46dfd782019-12-17 10:25:29 +01001082 int err_arg;
1083 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +01001084
Willy Tarreau833cc792013-07-24 15:34:19 +02001085 if (*endt && *endt != ',') {
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001086 if (endptr) {
1087 /* end found, let's stop here */
1088 break;
1089 }
Willy Tarreau833cc792013-07-24 15:34:19 +02001090 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +01001091 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +02001092 else
Willy Tarreau975c1782013-12-12 23:16:54 +01001093 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +01001094 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001095 }
Emeric Brun107ca302010-01-04 16:16:05 +01001096
Willy Tarreau80b53ff2020-02-14 08:40:37 +01001097 /* FIXME: how long should we support such idiocies ? Maybe we
1098 * should already warn ?
1099 */
Willy Tarreau833cc792013-07-24 15:34:19 +02001100 while (*endt == ',') /* then trailing commas */
1101 endt++;
1102
Willy Tarreau97108e02016-11-25 07:33:24 +01001103 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +02001104
1105 if (!*begw) {
1106 /* none ? skip to next string */
1107 (*idx)++;
1108 begw = str[*idx];
1109 if (!begw || !*begw)
1110 break;
1111 }
1112
Willy Tarreaued2c6622020-02-14 18:27:10 +01001113 for (endw = begw; is_idchar(*endw); endw++)
1114 ;
Willy Tarreau833cc792013-07-24 15:34:19 +02001115
1116 free(ckw);
1117 ckw = my_strndup(begw, endw - begw);
1118
1119 conv = find_sample_conv(begw, endw - begw);
1120 if (!conv) {
1121 /* we found an isolated keyword that we don't know, it's not ours */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001122 if (begw == str[*idx]) {
1123 endt = begw;
Willy Tarreau833cc792013-07-24 15:34:19 +02001124 break;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001125 }
Willy Tarreau97108e02016-11-25 07:33:24 +01001126 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +02001127 goto out_error;
1128 }
Emeric Brun107ca302010-01-04 16:16:05 +01001129
Willy Tarreau833cc792013-07-24 15:34:19 +02001130 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001131 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001132 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001133 }
Emeric Brun107ca302010-01-04 16:16:05 +01001134
1135 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +02001136 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001137 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001138 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001139 }
Emeric Brun107ca302010-01-04 16:16:05 +01001140
1141 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +02001142 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +02001143 if (!conv_expr)
1144 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001145
Willy Tarreau2b718102021-04-21 07:32:39 +02001146 LIST_APPEND(&(expr->conv_exprs), &(conv_expr->list));
Emeric Brun107ca302010-01-04 16:16:05 +01001147 conv_expr->conv = conv;
1148
Christopher Faulet35926a12021-09-30 08:48:56 +02001149 if (al) {
1150 al->kw = expr->fetch->kw;
1151 al->conv = conv_expr->conv->kw;
1152 }
Willy Tarreau80b53ff2020-02-14 08:40:37 +01001153 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 +01001154 if (argcnt < 0) {
1155 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
1156 goto out_error;
1157 }
Willy Tarreau9e92d322010-01-26 17:58:06 +01001158
Willy Tarreau46dfd782019-12-17 10:25:29 +01001159 if (argcnt && !conv->arg_mask) {
1160 memprintf(err_msg, "converter '%s' does not support any args", ckw);
1161 goto out_error;
1162 }
Willy Tarreau21d68a62012-04-20 15:52:36 +02001163
Willy Tarreau46dfd782019-12-17 10:25:29 +01001164 if (!conv_expr->arg_p)
1165 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001166
Willy Tarreau46dfd782019-12-17 10:25:29 +01001167 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1168 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001169 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001170 }
1171 }
Emeric Brun485479d2010-09-23 18:02:19 +02001172
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001173 if (endptr) {
1174 /* end found, let's stop here */
1175 *endptr = (char *)endt;
1176 }
1177
Willy Tarreau833cc792013-07-24 15:34:19 +02001178 out:
1179 free(fkw);
1180 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001181 return expr;
1182
1183out_error:
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +01001184 release_sample_expr(expr);
Willy Tarreau833cc792013-07-24 15:34:19 +02001185 expr = NULL;
1186 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001187}
1188
1189/*
Willy Tarreau12785782012-04-27 21:37:17 +02001190 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001191 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001192 * Returns a pointer on a typed sample structure containing the result or NULL if
1193 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001194 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001195 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001196 *
1197 * Note: the fetch functions are required to properly set the return type. The
1198 * conversion functions must do so too. However the cast functions do not need
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001199 * to since they're made to cast multiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001200 *
1201 * The caller may indicate in <opt> if it considers the result final or not.
1202 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1203 * if the result is stable or not, according to the following table :
1204 *
1205 * return MAY_CHANGE FINAL Meaning for the sample
1206 * NULL 0 * Not present and will never be (eg: header)
1207 * NULL 1 0 Not present yet, could change (eg: POST param)
1208 * NULL 1 1 Not present yet, will not change anymore
1209 * smp 0 * Present and will not change (eg: header)
1210 * smp 1 0 Present, may change (eg: request length)
1211 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001212 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001213struct sample *sample_process(struct proxy *px, struct session *sess,
1214 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001215 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001216{
Willy Tarreau12785782012-04-27 21:37:17 +02001217 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001218
Willy Tarreau18387e22013-07-25 12:02:38 +02001219 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001220 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001221 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001222 }
Emeric Brun107ca302010-01-04 16:16:05 +01001223
Willy Tarreau1777ea62016-03-10 16:15:46 +01001224 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001225 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001226 return NULL;
1227
Emeric Brun107ca302010-01-04 16:16:05 +01001228 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001229 /* we want to ensure that p->type can be casted into
1230 * conv_expr->conv->in_type. We have 3 possibilities :
1231 * - NULL => not castable.
1232 * - c_none => nothing to do (let's optimize it)
1233 * - other => apply cast and prepare to fail
1234 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001235 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001236 return NULL;
1237
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001238 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1239 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001240 return NULL;
1241
Willy Tarreau12e50112012-04-25 17:21:49 +02001242 /* OK cast succeeded */
1243
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001244 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001245 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001246 }
1247 return p;
1248}
1249
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001250/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001251 * Resolve all remaining arguments in proxy <p>. Returns the number of
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001252 * errors or 0 if everything is fine. If at least one error is met, it will
1253 * be appended to *err. If *err==NULL it will be allocated first.
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001254 */
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001255int smp_resolve_args(struct proxy *p, char **err)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001256{
1257 struct arg_list *cur, *bak;
1258 const char *ctx, *where;
1259 const char *conv_ctx, *conv_pre, *conv_pos;
1260 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001261 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001262 struct arg *arg;
1263 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001264 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001265
1266 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1267 struct proxy *px;
1268 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001269 struct stktable *t;
1270 char *pname, *sname, *stktname;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001271 char *err2;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001272
1273 arg = cur->arg;
1274
1275 /* prepare output messages */
1276 conv_pre = conv_pos = conv_ctx = "";
1277 if (cur->conv) {
1278 conv_ctx = cur->conv;
1279 conv_pre = "conversion keyword '";
1280 conv_pos = "' for ";
1281 }
1282
1283 where = "in";
1284 ctx = "sample fetch keyword";
1285 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001286 case ARGC_STK: where = "in stick rule in"; break;
1287 case ARGC_TRK: where = "in tracking rule in"; break;
1288 case ARGC_LOG: where = "in log-format string in"; break;
1289 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
Willy Tarreau57467b82021-09-02 19:43:20 +02001290 case ARGC_HRQ: where = "in http-request expression in"; break;
1291 case ARGC_HRS: where = "in http-response response in"; break;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001292 case ARGC_UIF: where = "in unique-id-format string in"; break;
1293 case ARGC_RDR: where = "in redirect format string in"; break;
1294 case ARGC_CAP: where = "in capture rule in"; break;
1295 case ARGC_ACL: ctx = "ACL keyword"; break;
1296 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001297 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Willy Tarreau1a8636d2022-04-25 08:15:46 +02001298 case ARGC_UBK: where = "in use_backend expression in"; break;
1299 case ARGC_USRV: where = "in use-server expression in"; break;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001300 case ARGC_HERR: where = "in http-error directive in"; break;
Miroslav Zagorac7f8314c2020-12-09 16:31:48 +01001301 case ARGC_OT: where = "in ot-scope directive in"; break;
Willy Tarreau57467b82021-09-02 19:43:20 +02001302 case ARGC_TCO: where = "in tcp-request connection expression in"; break;
1303 case ARGC_TSE: where = "in tcp-request session expression in"; break;
1304 case ARGC_TRQ: where = "in tcp-request content expression in"; break;
1305 case ARGC_TRS: where = "in tcp-response content expression in"; break;
1306 case ARGC_TCK: where = "in tcp-check expression in"; break;
1307 case ARGC_CFG: where = "in configuration expression in"; break;
1308 case ARGC_CLI: where = "in CLI expression in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001309 }
1310
1311 /* set a few default settings */
1312 px = p;
1313 pname = p->id;
1314
1315 switch (arg->type) {
1316 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001317 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001318 memprintf(err, "%sparsing [%s:%d]: missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1319 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001320 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001321 cfgerr++;
1322 continue;
1323 }
1324
1325 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001326 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001327
1328 if (sname) {
1329 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001330 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001331
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001332 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001333 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001334 memprintf(err, "%sparsing [%s:%d]: unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1335 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001336 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001337 cfgerr++;
1338 break;
1339 }
1340 }
Christopher Faulet37a9e212021-10-12 18:48:05 +02001341 else {
1342 if (px->cap & PR_CAP_DEF) {
1343 memprintf(err, "%sparsing [%s:%d]: backend name must be set in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1344 *err ? *err : "", cur->file, cur->line,
1345 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1346 cfgerr++;
1347 break;
1348 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001349 sname = arg->data.str.area;
Christopher Faulet37a9e212021-10-12 18:48:05 +02001350 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001351
1352 srv = findserver(px, sname);
1353 if (!srv) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001354 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",
1355 *err ? *err : "", cur->file, cur->line, sname, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001356 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001357 cfgerr++;
1358 break;
1359 }
1360
Amaury Denoyelle06269612021-08-23 14:05:07 +02001361 srv->flags |= SRV_F_NON_PURGEABLE;
1362
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001363 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001364 arg->unresolved = 0;
1365 arg->data.srv = srv;
1366 break;
1367
1368 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001369 if (arg->data.str.data) {
1370 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001371 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001372 }
1373
1374 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001375 memprintf(err, "%sparsing [%s:%d]: unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1376 *err ? *err : "", cur->file, cur->line, pname,
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 Tarreaua4312fa2013-04-02 16:34:32 +02001378 cfgerr++;
1379 break;
1380 }
1381
1382 if (!(px->cap & PR_CAP_FE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001383 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",
1384 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001385 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001386 cfgerr++;
1387 break;
1388 }
1389
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001390 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001391 arg->unresolved = 0;
1392 arg->data.prx = px;
1393 break;
1394
1395 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001396 if (arg->data.str.data) {
1397 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001398 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001399 }
1400
1401 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001402 memprintf(err, "%sparsing [%s:%d]: unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1403 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001404 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001405 cfgerr++;
1406 break;
1407 }
1408
1409 if (!(px->cap & PR_CAP_BE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001410 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",
1411 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001412 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001413 cfgerr++;
1414 break;
1415 }
1416
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001417 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001418 arg->unresolved = 0;
1419 arg->data.prx = px;
1420 break;
1421
1422 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001423 if (arg->data.str.data)
1424 stktname = arg->data.str.area;
Christopher Faulet37a9e212021-10-12 18:48:05 +02001425 else {
1426 if (px->cap & PR_CAP_DEF) {
1427 memprintf(err, "%sparsing [%s:%d]: table name must be set in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1428 *err ? *err : "", cur->file, cur->line,
1429 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1430 cfgerr++;
1431 break;
1432 }
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001433 stktname = px->id;
Christopher Faulet37a9e212021-10-12 18:48:05 +02001434 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001435
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001436 t = stktable_find_by_name(stktname);
1437 if (!t) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001438 memprintf(err, "%sparsing [%s:%d]: unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1439 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001440 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001441 cfgerr++;
1442 break;
1443 }
1444
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001445 if (!t->size) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001446 memprintf(err, "%sparsing [%s:%d]: no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1447 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001448 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001449 cfgerr++;
1450 break;
1451 }
1452
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001453 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001454 p->next_stkt_ref = t->proxies_list;
1455 t->proxies_list = p;
1456 }
1457
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001458 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001459 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001460 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001461 break;
1462
1463 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001464 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001465 memprintf(err, "%sparsing [%s:%d]: missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1466 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001467 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001468 cfgerr++;
1469 break;
1470 }
1471
1472 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001473 strcmp(p->uri_auth->userlist->name, arg->data.str.area) == 0)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001474 ul = p->uri_auth->userlist;
1475 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001476 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001477
1478 if (!ul) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001479 memprintf(err, "%sparsing [%s:%d]: unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1480 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001481 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001482 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001483 cfgerr++;
1484 break;
1485 }
1486
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001487 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001488 arg->unresolved = 0;
1489 arg->data.usr = ul;
1490 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001491
1492 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001493 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001494 memprintf(err, "%sparsing [%s:%d]: missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1495 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001496 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001497 cfgerr++;
1498 continue;
1499 }
1500
Willy Tarreau46947782015-01-19 19:00:58 +01001501 rflags = 0;
1502 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001503 err2 = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001504
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001505 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err2))) {
1506 memprintf(err, "%sparsing [%s:%d]: error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1507 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001508 arg->data.str.area,
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001509 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err2);
Willy Tarreau46947782015-01-19 19:00:58 +01001510 cfgerr++;
1511 continue;
1512 }
1513
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001514 chunk_destroy(&arg->data.str);
Willy Tarreau46947782015-01-19 19:00:58 +01001515 arg->unresolved = 0;
1516 arg->data.reg = reg;
1517 break;
1518
1519
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001520 }
1521
Willy Tarreau2b718102021-04-21 07:32:39 +02001522 LIST_DELETE(&cur->list);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001523 free(cur);
1524 } /* end of args processing */
1525
1526 return cfgerr;
1527}
1528
1529/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001530 * Process a fetch + format conversion as defined by the sample expression
1531 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001532 * not explicitly set to <smp_type>, but shall be compatible with it as
1533 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1534 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001535 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1536 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1537 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1538 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001539 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1540 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1541 * still hope to get it after waiting longer, and is not converted to string.
1542 * The possible output combinations are the following :
1543 *
1544 * return MAY_CHANGE FINAL Meaning for the sample
1545 * NULL * * Not present and will never be (eg: header)
1546 * smp 0 * Final value converted (eg: header)
1547 * smp 1 0 Not present yet, may appear later (eg: header)
1548 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001549 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001550struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001551 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001552 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001553{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001554 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001555
Willy Tarreau6c616e02014-06-25 16:56:41 +02001556 memset(smp, 0, sizeof(*smp));
1557
Willy Tarreau192252e2015-04-04 01:47:55 +02001558 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001559 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1560 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001561 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001562 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001563
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001564 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001565 return NULL;
1566
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001567 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001568 return NULL;
1569
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001570 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001571 return smp;
1572}
1573
Christopher Faulet476e5d02016-10-26 11:34:47 +02001574static void release_sample_arg(struct arg *p)
1575{
1576 struct arg *p_back = p;
1577
1578 if (!p)
1579 return;
1580
1581 while (p->type != ARGT_STOP) {
1582 if (p->type == ARGT_STR || p->unresolved) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001583 chunk_destroy(&p->data.str);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001584 p->unresolved = 0;
1585 }
1586 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001587 regex_free(p->data.reg);
1588 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001589 }
1590 p++;
1591 }
1592
1593 if (p_back != empty_arg_list)
1594 free(p_back);
1595}
1596
1597void release_sample_expr(struct sample_expr *expr)
1598{
1599 struct sample_conv_expr *conv_expr, *conv_exprb;
1600
1601 if (!expr)
1602 return;
1603
Tim Duesterhus867cd982020-07-04 11:49:39 +02001604 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001605 LIST_DELETE(&conv_expr->list);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001606 release_sample_arg(conv_expr->arg_p);
Tim Duesterhus867cd982020-07-04 11:49:39 +02001607 free(conv_expr);
1608 }
1609
Christopher Faulet476e5d02016-10-26 11:34:47 +02001610 release_sample_arg(expr->arg_p);
1611 free(expr);
1612}
1613
Emeric Brun107ca302010-01-04 16:16:05 +01001614/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001615/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001616/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001617/*****************************************************************/
1618
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001619static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1620{
1621 int i;
1622 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001623 struct buffer *buf;
1624 struct sink *sink;
1625 struct ist line;
1626 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001627
Willy Tarreau0851fd52019-12-17 10:07:25 +01001628 buf = alloc_trash_chunk();
1629 if (!buf)
1630 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001631
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001632 sink = (struct sink *)arg_p[1].data.ptr;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001633 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001634
Willy Tarreau0851fd52019-12-17 10:07:25 +01001635 pfx = arg_p[0].data.str.area;
1636 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001637
Willy Tarreau0851fd52019-12-17 10:07:25 +01001638 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1639 if (!sample_casts[smp->data.type][SMP_T_STR])
1640 goto nocast;
1641
1642 /* Copy sample fetch. This puts the sample as const, the
1643 * cast will copy data if a transformation is required.
1644 */
1645 memcpy(&tmp, smp, sizeof(struct sample));
1646 tmp.flags = SMP_F_CONST;
1647
1648 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1649 goto nocast;
1650
1651 /* Display the displayable chars*. */
1652 b_putchr(buf, '<');
1653 for (i = 0; i < tmp.data.u.str.data; i++) {
Willy Tarreau90807112020-02-25 08:16:33 +01001654 if (isprint((unsigned char)tmp.data.u.str.area[i]))
Willy Tarreau0851fd52019-12-17 10:07:25 +01001655 b_putchr(buf, tmp.data.u.str.area[i]);
1656 else
1657 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001658 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001659 b_putchr(buf, '>');
1660
1661 done:
1662 line = ist2(buf->area, buf->data);
Emeric Brun54648852020-07-06 15:54:06 +02001663 sink_write(sink, &line, 1, 0, 0, NULL);
Willy Tarreau0851fd52019-12-17 10:07:25 +01001664 end:
1665 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001666 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001667 nocast:
1668 chunk_appendf(buf, "(undisplayable)");
1669 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001670}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001671
1672// This function checks the "debug" converter's arguments.
1673static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1674 const char *file, int line, char **err)
1675{
1676 const char *name = "buf0";
1677 struct sink *sink = NULL;
1678
1679 if (args[0].type != ARGT_STR) {
1680 /* optional prefix */
1681 args[0].data.str.area = "";
1682 args[0].data.str.data = 0;
1683 }
1684
1685 if (args[1].type == ARGT_STR)
1686 name = args[1].data.str.area;
1687
1688 sink = sink_find(name);
1689 if (!sink) {
1690 memprintf(err, "No such sink '%s'", name);
1691 return 0;
1692 }
1693
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001694 chunk_destroy(&args[1].data.str);
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001695 args[1].type = ARGT_PTR;
1696 args[1].data.ptr = sink;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001697 return 1;
1698}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001699
Holger Just1bfc24b2017-05-06 00:56:53 +02001700static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1701{
Willy Tarreau83061a82018-07-13 11:56:34 +02001702 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001703 int bin_len;
1704
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001705 trash->data = 0;
1706 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1707 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001708 if (bin_len < 0)
1709 return 0;
1710
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001711 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001712 smp->data.u.str = *trash;
1713 smp->data.type = SMP_T_BIN;
1714 smp->flags &= ~SMP_F_CONST;
1715 return 1;
1716}
1717
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +02001718static int sample_conv_base64url2bin(const struct arg *arg_p, struct sample *smp, void *private)
1719{
1720 struct buffer *trash = get_trash_chunk();
1721 int bin_len;
1722
1723 trash->data = 0;
1724 bin_len = base64urldec(smp->data.u.str.area, smp->data.u.str.data,
1725 trash->area, trash->size);
1726 if (bin_len < 0)
1727 return 0;
1728
1729 trash->data = bin_len;
1730 smp->data.u.str = *trash;
1731 smp->data.type = SMP_T_BIN;
1732 smp->flags &= ~SMP_F_CONST;
1733 return 1;
1734}
1735
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001736static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001737{
Willy Tarreau83061a82018-07-13 11:56:34 +02001738 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001739 int b64_len;
1740
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001741 trash->data = 0;
1742 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1743 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001744 if (b64_len < 0)
1745 return 0;
1746
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001747 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001748 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001749 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001750 smp->flags &= ~SMP_F_CONST;
1751 return 1;
1752}
1753
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +02001754static int sample_conv_bin2base64url(const struct arg *arg_p, struct sample *smp, void *private)
1755{
1756 struct buffer *trash = get_trash_chunk();
1757 int b64_len;
1758
1759 trash->data = 0;
1760 b64_len = a2base64url(smp->data.u.str.area, smp->data.u.str.data,
1761 trash->area, trash->size);
1762 if (b64_len < 0)
1763 return 0;
1764
1765 trash->data = b64_len;
1766 smp->data.u.str = *trash;
1767 smp->data.type = SMP_T_STR;
1768 smp->flags &= ~SMP_F_CONST;
1769 return 1;
1770}
1771
Willy Tarreau168e8de2021-10-06 15:29:00 +02001772/* This function returns a sample struct filled with the conversion of variable
1773 * <var> to sample type <type> (SMP_T_*), via a cast to the target type. If the
1774 * variable cannot be retrieved or casted, 0 is returned, otherwise 1.
1775 *
1776 * Keep in mind that the sample content may be written to a pre-allocated
1777 * trash chunk as returned by get_trash_chunk().
1778 */
1779int sample_conv_var2smp(const struct var_desc *var, struct sample *smp, int type)
1780{
1781 if (!vars_get_by_desc(var, smp, NULL))
1782 return 0;
1783 if (!sample_casts[smp->data.type][type])
1784 return 0;
1785 if (!sample_casts[smp->data.type][type](smp))
1786 return 0;
1787 return 1;
1788}
1789
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001790static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1791{
1792 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001793 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001794
1795 memset(&ctx, 0, sizeof(ctx));
1796
1797 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001798 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1799 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001800
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001801 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001802 smp->data.u.str = *trash;
1803 smp->data.type = SMP_T_BIN;
1804 smp->flags &= ~SMP_F_CONST;
1805 return 1;
1806}
1807
Dragan Dosen9e8db132021-02-22 10:03:53 +01001808/* This function returns a sample struct filled with an <arg> content.
1809 * If the <arg> contains a string, it is returned in the sample flagged as
1810 * SMP_F_CONST. If the <arg> contains a variable descriptor, the sample is
1811 * filled with the content of the variable by using vars_get_by_desc().
1812 *
1813 * Keep in mind that the sample content may be written to a pre-allocated
1814 * trash chunk as returned by get_trash_chunk().
1815 *
1816 * This function returns 0 if an error occurs, otherwise it returns 1.
1817 */
Willy Tarreau4034e2c2021-10-06 15:20:18 +02001818int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001819{
1820 switch (arg->type) {
1821 case ARGT_STR:
1822 smp->data.type = SMP_T_STR;
1823 smp->data.u.str = arg->data.str;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001824 smp->flags = SMP_F_CONST;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001825 return 1;
1826 case ARGT_VAR:
Willy Tarreau2476ff12021-10-06 15:30:52 +02001827 return sample_conv_var2smp(&arg->data.var, smp, SMP_T_STR);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001828 default:
1829 return 0;
1830 }
1831}
1832
Marcin Deranek40ca09c2021-07-13 14:05:24 +02001833static int sample_conv_be2dec_check(struct arg *args, struct sample_conv *conv,
1834 const char *file, int line, char **err)
1835{
1836 if (args[1].data.sint <= 0 || args[1].data.sint > sizeof(unsigned long long)) {
Willy Tarreauc5d0fc92021-09-15 10:30:40 +02001837 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 +02001838 return 0;
1839 }
1840
1841 if (args[2].data.sint != 0 && args[2].data.sint != 1) {
1842 memprintf(err, "Unsupported truncate value (%lld)", args[2].data.sint);
1843 return 0;
1844 }
1845
1846 return 1;
1847}
1848
1849/* Converts big-endian binary input sample to a string containing an unsigned
1850 * integer number per <chunk_size> input bytes separated with <separator>.
1851 * Optional <truncate> flag indicates if input is truncated at <chunk_size>
1852 * boundaries.
1853 * Arguments: separator (string), chunk_size (integer), truncate (0,1)
1854 */
1855static int sample_conv_be2dec(const struct arg *args, struct sample *smp, void *private)
1856{
1857 struct buffer *trash = get_trash_chunk();
1858 const int last = args[2].data.sint ? smp->data.u.str.data - args[1].data.sint + 1 : smp->data.u.str.data;
1859 int max_size = trash->size - 2;
1860 int i;
1861 int start;
1862 int ptr = 0;
1863 unsigned long long number;
1864 char *pos;
1865
1866 trash->data = 0;
1867
1868 while (ptr < last && trash->data <= max_size) {
1869 start = trash->data;
1870 if (ptr) {
1871 /* Add separator */
1872 memcpy(trash->area + trash->data, args[0].data.str.area, args[0].data.str.data);
1873 trash->data += args[0].data.str.data;
1874 }
1875 else
1876 max_size -= args[0].data.str.data;
1877
1878 /* Add integer */
1879 for (number = 0, i = 0; i < args[1].data.sint && ptr < smp->data.u.str.data; i++)
1880 number = (number << 8) + (unsigned char)smp->data.u.str.area[ptr++];
1881
1882 pos = ulltoa(number, trash->area + trash->data, trash->size - trash->data);
1883 if (pos)
1884 trash->data = pos - trash->area;
1885 else {
1886 trash->data = start;
1887 break;
1888 }
1889 }
1890
1891 smp->data.u.str = *trash;
1892 smp->data.type = SMP_T_STR;
1893 smp->flags &= ~SMP_F_CONST;
1894 return 1;
1895}
1896
Marcin Deranekda0264a2021-07-13 14:08:56 +02001897static int sample_conv_be2hex_check(struct arg *args, struct sample_conv *conv,
1898 const char *file, int line, char **err)
1899{
1900 if (args[1].data.sint <= 0 && (args[0].data.str.data > 0 || args[2].data.sint != 0)) {
1901 memprintf(err, "chunk_size needs to be positive (%lld)", args[1].data.sint);
1902 return 0;
1903 }
1904
1905 if (args[2].data.sint != 0 && args[2].data.sint != 1) {
1906 memprintf(err, "Unsupported truncate value (%lld)", args[2].data.sint);
1907 return 0;
1908 }
1909
1910 return 1;
1911}
1912
1913/* Converts big-endian binary input sample to a hex string containing two hex
1914 * digits per input byte. <separator> is put every <chunk_size> binary input
1915 * bytes if specified. Optional <truncate> flag indicates if input is truncated
1916 * at <chunk_size> boundaries.
1917 * Arguments: separator (string), chunk_size (integer), truncate (0,1)
1918 */
1919static int sample_conv_be2hex(const struct arg *args, struct sample *smp, void *private)
1920{
1921 struct buffer *trash = get_trash_chunk();
1922 int chunk_size = args[1].data.sint;
1923 const int last = args[2].data.sint ? smp->data.u.str.data - chunk_size + 1 : smp->data.u.str.data;
1924 int i;
1925 int max_size;
1926 int ptr = 0;
1927 unsigned char c;
1928
1929 trash->data = 0;
1930 if (args[0].data.str.data == 0 && args[2].data.sint == 0)
1931 chunk_size = smp->data.u.str.data;
1932 max_size = trash->size - 2 * chunk_size;
1933
1934 while (ptr < last && trash->data <= max_size) {
1935 if (ptr) {
1936 /* Add separator */
1937 memcpy(trash->area + trash->data, args[0].data.str.area, args[0].data.str.data);
1938 trash->data += args[0].data.str.data;
1939 }
1940 else
1941 max_size -= args[0].data.str.data;
1942
1943 /* Add hex */
1944 for (i = 0; i < chunk_size && ptr < smp->data.u.str.data; i++) {
1945 c = smp->data.u.str.area[ptr++];
1946 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1947 trash->area[trash->data++] = hextab[c & 0xF];
1948 }
1949 }
1950
1951 smp->data.u.str = *trash;
1952 smp->data.type = SMP_T_STR;
1953 smp->flags &= ~SMP_F_CONST;
1954 return 1;
1955}
1956
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001957static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001958{
Willy Tarreau83061a82018-07-13 11:56:34 +02001959 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001960 unsigned char c;
1961 int ptr = 0;
1962
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001963 trash->data = 0;
1964 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
1965 c = smp->data.u.str.area[ptr++];
1966 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1967 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001968 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001969 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001970 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001971 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001972 return 1;
1973}
1974
Dragan Dosen3f957b22017-10-24 09:27:34 +02001975static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1976{
1977 long long int n = 0;
1978 int i, c;
1979
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001980 for (i = 0; i < smp->data.u.str.data; i++) {
1981 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02001982 return 0;
1983 n = (n << 4) + c;
1984 }
1985
1986 smp->data.u.sint = n;
1987 smp->data.type = SMP_T_SINT;
1988 smp->flags &= ~SMP_F_CONST;
1989 return 1;
1990}
1991
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001992/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001993static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001994{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001995 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
1996 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01001997 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001998 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001999 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002000 return 1;
2001}
2002
Willy Tarreau60a2ee72017-12-15 07:13:48 +01002003static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002004{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002005 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002006 smp->data.u.sint = i;
2007 smp->data.type = SMP_T_SINT;
2008 return 1;
2009}
2010
2011
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002012static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002013{
2014 int i;
2015
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002016 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002017 return 0;
2018
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002019 for (i = 0; i < smp->data.u.str.data; i++) {
2020 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
2021 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01002022 }
2023 return 1;
2024}
2025
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002026static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002027{
2028 int i;
2029
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002030 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02002031 return 0;
2032
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002033 for (i = 0; i < smp->data.u.str.data; i++) {
2034 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
2035 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01002036 }
2037 return 1;
2038}
2039
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002040/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
2041static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002042{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002043 /* Attempt to convert to IPv4 to apply the correct mask. */
2044 c_ipv62ip(smp);
2045
2046 if (smp->data.type == SMP_T_IPV4) {
2047 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
2048 smp->data.type = SMP_T_IPV4;
2049 }
2050 else if (smp->data.type == SMP_T_IPV6) {
2051 /* IPv6 cannot be converted without an IPv6 mask. */
2052 if (args[1].type != ARGT_IPV6)
2053 return 0;
2054
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +01002055 write_u64(&smp->data.u.ipv6.s6_addr[0],
2056 read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0]));
2057 write_u64(&smp->data.u.ipv6.s6_addr[8],
2058 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 +01002059 smp->data.type = SMP_T_IPV6;
2060 }
2061
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002062 return 1;
2063}
2064
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002065/* takes an UINT value on input supposed to represent the time since EPOCH,
2066 * adds an optional offset found in args[1] and emits a string representing
2067 * the local time in the format specified in args[1] using strftime().
2068 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002069static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002070{
Willy Tarreau83061a82018-07-13 11:56:34 +02002071 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002072 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002073 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002074 struct tm tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002075
2076 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002077 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002078 curr_date += args[1].data.sint;
2079
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002080 get_localtime(curr_date, &tm);
2081
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002082 temp = get_trash_chunk();
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002083 temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002084 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002085 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002086 return 1;
2087}
2088
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002089/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002090static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002091{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002092 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
2093 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002094 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002095 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002096 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002097 return 1;
2098}
2099
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002100/* takes an UINT value on input supposed to represent the time since EPOCH,
2101 * adds an optional offset found in args[1] and emits a string representing
2102 * the UTC date in the format specified in args[1] using strftime().
2103 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002104static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002105{
Willy Tarreau83061a82018-07-13 11:56:34 +02002106 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002107 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002108 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002109 struct tm tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002110
2111 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002112 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002113 curr_date += args[1].data.sint;
2114
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002115 get_gmtime(curr_date, &tm);
2116
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002117 temp = get_trash_chunk();
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002118 temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002119 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002120 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002121 return 1;
2122}
2123
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002124/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002125static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002126{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002127 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
2128 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002129 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002130 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002131 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002132 return 1;
2133}
2134
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002135/* hashes the binary input into a 32-bit unsigned int using xxh.
2136 * The seed of the hash defaults to 0 but can be changd in argument 1.
2137 */
2138static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
2139{
2140 unsigned int seed;
2141
Christopher Faulete6e7a582021-01-29 11:29:28 +01002142 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002143 seed = arg_p->data.sint;
2144 else
2145 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002146 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
2147 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002148 smp->data.type = SMP_T_SINT;
2149 return 1;
2150}
2151
2152/* hashes the binary input into a 64-bit unsigned int using xxh.
2153 * In fact, the function returns a 64 bit unsigned, but the sample
2154 * storage of haproxy only proposes 64-bits signed, so the value is
2155 * cast as signed. This cast doesn't impact the hash repartition.
2156 * The seed of the hash defaults to 0 but can be changd in argument 1.
2157 */
2158static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
2159{
2160 unsigned long long int seed;
2161
Christopher Faulete6e7a582021-01-29 11:29:28 +01002162 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002163 seed = (unsigned long long int)arg_p->data.sint;
2164 else
2165 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002166 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
2167 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002168 smp->data.type = SMP_T_SINT;
2169 return 1;
2170}
2171
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002172static int sample_conv_xxh3(const struct arg *arg_p, struct sample *smp, void *private)
2173{
2174 unsigned long long int seed;
2175
Christopher Faulete6e7a582021-01-29 11:29:28 +01002176 if (arg_p->data.sint)
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002177 seed = (unsigned long long int)arg_p->data.sint;
2178 else
2179 seed = 0;
2180 smp->data.u.sint = (long long int)XXH3(smp->data.u.str.area,
2181 smp->data.u.str.data, seed);
2182 smp->data.type = SMP_T_SINT;
2183 return 1;
2184}
2185
Willy Tarreau80599772015-01-20 19:35:24 +01002186/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002187static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01002188{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002189 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
2190 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002191 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002192 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002193 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01002194 return 1;
2195}
2196
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002197/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
2198static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
2199{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002200 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
2201 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002202 if (arg_p->data.sint)
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002203 smp->data.u.sint = full_hash(smp->data.u.sint);
2204 smp->data.type = SMP_T_SINT;
2205 return 1;
2206}
2207
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002208/* This function escape special json characters. The returned string can be
2209 * safely set between two '"' and used as json string. The json string is
2210 * defined like this:
2211 *
2212 * any Unicode character except '"' or '\' or control character
2213 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
2214 *
2215 * The enum input_type contain all the allowed mode for decoding the input
2216 * string.
2217 */
2218enum input_type {
2219 IT_ASCII = 0,
2220 IT_UTF8,
2221 IT_UTF8S,
2222 IT_UTF8P,
2223 IT_UTF8PS,
2224};
William Dauchy5417e892021-01-08 21:57:41 +01002225
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002226static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
2227 const char *file, int line, char **err)
2228{
Christopher Faulet95917132020-08-05 23:07:07 +02002229 enum input_type type;
2230
Christopher Faulet95917132020-08-05 23:07:07 +02002231 if (strcmp(arg->data.str.area, "") == 0)
2232 type = IT_ASCII;
2233 else if (strcmp(arg->data.str.area, "ascii") == 0)
2234 type = IT_ASCII;
2235 else if (strcmp(arg->data.str.area, "utf8") == 0)
2236 type = IT_UTF8;
2237 else if (strcmp(arg->data.str.area, "utf8s") == 0)
2238 type = IT_UTF8S;
2239 else if (strcmp(arg->data.str.area, "utf8p") == 0)
2240 type = IT_UTF8P;
2241 else if (strcmp(arg->data.str.area, "utf8ps") == 0)
2242 type = IT_UTF8PS;
2243 else {
2244 memprintf(err, "Unexpected input code type. "
2245 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
2246 return 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002247 }
2248
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002249 chunk_destroy(&arg->data.str);
Christopher Faulet95917132020-08-05 23:07:07 +02002250 arg->type = ARGT_SINT;
2251 arg->data.sint = type;
2252 return 1;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002253}
2254
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002255static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002256{
Willy Tarreau83061a82018-07-13 11:56:34 +02002257 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002258 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
2259 const char *str;
2260 int len;
2261 enum input_type input_type = IT_ASCII;
2262 unsigned int c;
2263 unsigned int ret;
2264 char *p;
2265
Christopher Faulete6e7a582021-01-29 11:29:28 +01002266 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002267
2268 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002269 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002270
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002271 p = smp->data.u.str.area;
2272 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002273
2274 if (input_type == IT_ASCII) {
2275 /* Read input as ASCII. */
2276 c = *(unsigned char *)p;
2277 p++;
2278 }
2279 else {
2280 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002281 ret = utf8_next(p,
2282 smp->data.u.str.data - ( p - smp->data.u.str.area),
2283 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002284 p += utf8_return_length(ret);
2285
2286 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2287 return 0;
2288 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2289 continue;
2290 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2291 return 0;
2292 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2293 continue;
2294
2295 /* Check too big values. */
2296 if ((unsigned int)c > 0xffff) {
2297 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2298 return 0;
2299 continue;
2300 }
2301 }
2302
2303 /* Convert character. */
2304 if (c == '"') {
2305 len = 2;
2306 str = "\\\"";
2307 }
2308 else if (c == '\\') {
2309 len = 2;
2310 str = "\\\\";
2311 }
2312 else if (c == '/') {
2313 len = 2;
2314 str = "\\/";
2315 }
2316 else if (c == '\b') {
2317 len = 2;
2318 str = "\\b";
2319 }
2320 else if (c == '\f') {
2321 len = 2;
2322 str = "\\f";
2323 }
2324 else if (c == '\r') {
2325 len = 2;
2326 str = "\\r";
2327 }
2328 else if (c == '\n') {
2329 len = 2;
2330 str = "\\n";
2331 }
2332 else if (c == '\t') {
2333 len = 2;
2334 str = "\\t";
2335 }
Willy Tarreau90807112020-02-25 08:16:33 +01002336 else if (c > 0xff || !isprint((unsigned char)c)) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002337 /* isprint generate a segfault if c is too big. The man says that
2338 * c must have the value of an unsigned char or EOF.
2339 */
2340 len = 6;
2341 _str[0] = '\\';
2342 _str[1] = 'u';
2343 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2344 str = _str;
2345 }
2346 else {
2347 len = 1;
Willy Tarreau5715da22020-02-25 08:37:37 +01002348 _str[0] = c;
2349 str = _str;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002350 }
2351
2352 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002353 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002354 return 0;
2355
2356 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002357 memcpy(temp->area + temp->data, str, len);
2358 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002359 }
2360
2361 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002362 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002363 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002364
2365 return 1;
2366}
2367
Emeric Brun54c4ac82014-11-03 15:32:43 +01002368/* This sample function is designed to extract some bytes from an input buffer.
2369 * First arg is the offset.
2370 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002371static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002372{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002373 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2374 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002375 return 1;
2376 }
2377
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002378 if (smp->data.u.str.size)
2379 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002380 smp->data.u.str.data -= arg_p[0].data.sint;
2381 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002382
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002383 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2384 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002385
2386 return 1;
2387}
2388
Emeric Brunf399b0d2014-11-03 17:07:03 +01002389static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2390 const char *file, int line, char **err)
2391{
2392 struct arg *arg = args;
2393
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002394 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002395 memprintf(err, "Unexpected arg type");
2396 return 0;
2397 }
2398
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002399 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002400 memprintf(err, "Unexpected value 0 for index");
2401 return 0;
2402 }
2403
2404 arg++;
2405
2406 if (arg->type != ARGT_STR) {
2407 memprintf(err, "Unexpected arg type");
2408 return 0;
2409 }
2410
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002411 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002412 memprintf(err, "Empty separators list");
2413 return 0;
2414 }
2415
2416 return 1;
2417}
2418
2419/* This sample function is designed to a return selected part of a string (field).
2420 * First arg is the index of the field (start at 1)
2421 * Second arg is a char list of separators (type string)
2422 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002423static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002424{
Marcin Deranek9631a282018-04-16 14:30:46 +02002425 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002426 char *start, *end;
2427 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002428 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002429
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002430 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002431 return 0;
2432
Marcin Deranek9631a282018-04-16 14:30:46 +02002433 if (arg_p[0].data.sint < 0) {
2434 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002435 end = start = smp->data.u.str.area + smp->data.u.str.data;
2436 while (start > smp->data.u.str.area) {
2437 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2438 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002439 if (field == arg_p[0].data.sint) {
2440 if (count == 1)
2441 goto found;
2442 else if (count > 1)
2443 count--;
2444 } else {
2445 end = start-1;
2446 field--;
2447 }
2448 break;
2449 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002450 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002451 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002452 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002453 } else {
2454 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002455 end = start = smp->data.u.str.area;
2456 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2457 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2458 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002459 if (field == arg_p[0].data.sint) {
2460 if (count == 1)
2461 goto found;
2462 else if (count > 1)
2463 count--;
2464 } else {
2465 start = end+1;
2466 field++;
2467 }
2468 break;
2469 }
2470 }
2471 end++;
2472 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002473 }
2474
2475 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002476 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002477 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002478 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002479 }
2480found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002481 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002482 /* If ret string is len 0, no need to
2483 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002484 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002485 return 1;
2486
Emeric Brunf399b0d2014-11-03 17:07:03 +01002487 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002488 Note: smp->data.u.str.size cannot be set to 0 */
2489 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002490 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002491
Thayne McCombsb2843052021-04-11 23:26:59 -06002492 smp->data.u.str.area = start;
2493
Emeric Brunf399b0d2014-11-03 17:07:03 +01002494 return 1;
2495}
2496
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002497/* This sample function is designed to return a word from a string.
2498 * First arg is the index of the word (start at 1)
2499 * Second arg is a char list of words separators (type string)
2500 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002501static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002502{
Marcin Deranek9631a282018-04-16 14:30:46 +02002503 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002504 char *start, *end;
2505 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002506 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002507
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002508 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002509 return 0;
2510
2511 word = 0;
2512 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002513 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002514 end = start = smp->data.u.str.area + smp->data.u.str.data;
2515 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002516 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002517 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2518 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002519 issep = 1;
2520 break;
2521 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002522 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002523 if (!inword) {
2524 if (!issep) {
2525 if (word != arg_p[0].data.sint) {
2526 word--;
2527 end = start;
2528 }
2529 inword = 1;
2530 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002531 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002532 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002533 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002534 if (count == 1)
2535 goto found;
2536 else if (count > 1)
2537 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002538 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002539 inword = 0;
2540 }
2541 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002542 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002543 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002544 end = start = smp->data.u.str.area;
2545 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002546 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002547 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2548 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002549 issep = 1;
2550 break;
2551 }
2552 }
2553 if (!inword) {
2554 if (!issep) {
2555 if (word != arg_p[0].data.sint) {
2556 word++;
2557 start = end;
2558 }
2559 inword = 1;
2560 }
2561 }
2562 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002563 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002564 if (count == 1)
2565 goto found;
2566 else if (count > 1)
2567 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002568 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002569 inword = 0;
2570 }
2571 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002572 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002573 }
2574
2575 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002576 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002577 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002578 return 1;
2579 }
2580found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002581 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002582 /* If ret string is len 0, no need to
2583 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002584 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002585 return 1;
2586
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002587 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002588
2589 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002590 Note: smp->data.u.str.size cannot be set to 0 */
2591 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002592 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002593
2594 return 1;
2595}
2596
Willy Tarreau7eda8492015-01-20 19:47:06 +01002597static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2598 const char *file, int line, char **err)
2599{
2600 struct arg *arg = args;
2601 char *p;
2602 int len;
2603
2604 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2605 arg[0].type_flags = 0;
2606
2607 if (arg[2].type != ARGT_STR)
2608 return 1;
2609
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002610 p = arg[2].data.str.area;
2611 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002612 while (len) {
2613 if (*p == 'i') {
2614 arg[0].type_flags |= ARGF_REG_ICASE;
2615 }
2616 else if (*p == 'g') {
2617 arg[0].type_flags |= ARGF_REG_GLOB;
2618 }
2619 else {
2620 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2621 return 0;
2622 }
2623 p++;
2624 len--;
2625 }
2626 return 1;
2627}
2628
2629/* This sample function is designed to do the equivalent of s/match/replace/ on
2630 * the input string. It applies a regex and restarts from the last matched
2631 * location until nothing matches anymore. First arg is the regex to apply to
2632 * the input string, second arg is the replacement expression.
2633 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002634static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002635{
2636 char *start, *end;
2637 struct my_regex *reg = arg_p[0].data.reg;
2638 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002639 struct buffer *trash = get_trash_chunk();
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002640 struct buffer *output;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002641 int flag, max;
2642 int found;
2643
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002644 start = smp->data.u.str.area;
2645 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002646
2647 flag = 0;
2648 while (1) {
2649 /* check for last round which is used to copy remaining parts
2650 * when not running in global replacement mode.
2651 */
2652 found = 0;
2653 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2654 /* Note: we can have start == end on empty strings or at the end */
2655 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2656 }
2657
2658 if (!found)
2659 pmatch[0].rm_so = end - start;
2660
2661 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002662 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002663 if (max && pmatch[0].rm_so > 0) {
2664 if (max > pmatch[0].rm_so)
2665 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002666 memcpy(trash->area + trash->data, start, max);
2667 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002668 }
2669
2670 if (!found)
2671 break;
2672
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002673 output = alloc_trash_chunk();
Willy Tarreau23997da2020-02-18 14:27:44 +01002674 if (!output)
2675 break;
2676
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002677 output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2678
Willy Tarreau7eda8492015-01-20 19:47:06 +01002679 /* replace the matching part */
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002680 max = output->size - output->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002681 if (max) {
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002682 if (max > output->data)
2683 max = output->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002684 memcpy(trash->area + trash->data,
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002685 output->area, max);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002686 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002687 }
2688
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002689 free_trash_chunk(output);
2690
Willy Tarreau7eda8492015-01-20 19:47:06 +01002691 /* stop here if we're done with this string */
2692 if (start >= end)
2693 break;
2694
2695 /* We have a special case for matches of length 0 (eg: "x*y*").
2696 * These ones are considered to match in front of a character,
2697 * so we have to copy that character and skip to the next one.
2698 */
2699 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002700 if (trash->data < trash->size)
2701 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002702 pmatch[0].rm_eo++;
2703 }
2704
2705 start += pmatch[0].rm_eo;
2706 flag |= REG_NOTBOL;
2707 }
2708
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002709 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002710 return 1;
2711}
2712
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002713/* This function check an operator entry. It expects a string.
2714 * The string can be an integer or a variable name.
2715 */
2716static int check_operator(struct arg *args, struct sample_conv *conv,
2717 const char *file, int line, char **err)
2718{
2719 const char *str;
2720 const char *end;
Christopher Faulet95917132020-08-05 23:07:07 +02002721 long long int i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002722
2723 /* Try to decode a variable. */
2724 if (vars_check_arg(&args[0], NULL))
2725 return 1;
2726
2727 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002728 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002729 end = str + strlen(str);
Christopher Faulet95917132020-08-05 23:07:07 +02002730 i = read_int64(&str, end);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002731 if (*str != '\0') {
2732 memprintf(err, "expects an integer or a variable name");
2733 return 0;
2734 }
Christopher Faulet95917132020-08-05 23:07:07 +02002735
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002736 chunk_destroy(&args[0].data.str);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002737 args[0].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02002738 args[0].data.sint = i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002739 return 1;
2740}
2741
Willy Tarreau6204cd92016-03-10 16:33:04 +01002742/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002743 * If the arg contain an integer, the integer is returned in the
2744 * sample. If the arg contains a variable descriptor, it returns the
2745 * variable value.
2746 *
2747 * This function returns 0 if an error occurs, otherwise it returns 1.
2748 */
Willy Tarreaud9be5992021-10-06 15:19:05 +02002749int sample_conv_var2smp_sint(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002750{
2751 switch (arg->type) {
2752 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002753 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002754 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002755 return 1;
2756 case ARGT_VAR:
Willy Tarreau2476ff12021-10-06 15:30:52 +02002757 return sample_conv_var2smp(&arg->data.var, smp, SMP_T_SINT);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002758 default:
2759 return 0;
2760 }
2761}
2762
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002763/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002764 * result.
2765 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002766static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002767{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002768 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002769 return 1;
2770}
2771
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002772/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002773 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002774 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002775static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002776{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002777 struct sample tmp;
2778
Willy Tarreau7560dd42016-03-10 16:28:58 +01002779 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002780 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002781 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002782 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002783 return 1;
2784}
2785
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002786/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002787 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002788 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002789static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002790{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002791 struct sample tmp;
2792
Willy Tarreau7560dd42016-03-10 16:28:58 +01002793 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002794 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002795 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002796 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002797 return 1;
2798}
2799
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002800/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002801 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002802 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002803static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002804{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002805 struct sample tmp;
2806
Willy Tarreau7560dd42016-03-10 16:28:58 +01002807 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002808 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002809 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002810 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002811 return 1;
2812}
2813
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002814static inline long long int arith_add(long long int a, long long int b)
2815{
2816 /* Prevent overflow and makes capped calculus.
2817 * We must ensure that the check calculus doesn't
2818 * exceed the signed 64 bits limits.
2819 *
2820 * +----------+----------+
2821 * | a<0 | a>=0 |
2822 * +------+----------+----------+
2823 * | b<0 | MIN-a>b | no check |
2824 * +------+----------+----------+
2825 * | b>=0 | no check | MAX-a<b |
2826 * +------+----------+----------+
2827 */
2828 if ((a ^ b) >= 0) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002829 /* signs are different. */
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002830 if (a < 0) {
2831 if (LLONG_MIN - a > b)
2832 return LLONG_MIN;
2833 }
2834 if (LLONG_MAX - a < b)
2835 return LLONG_MAX;
2836 }
2837 return a + b;
2838}
2839
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002840/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002841 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002842 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002843static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002844{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002845 struct sample tmp;
2846
Willy Tarreau7560dd42016-03-10 16:28:58 +01002847 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002848 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002849 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002850 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002851 return 1;
2852}
2853
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002854/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002855 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002856 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002857static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002858 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002859{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002860 struct sample tmp;
2861
Willy Tarreau7560dd42016-03-10 16:28:58 +01002862 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002863 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002864 return 0;
2865
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002866 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2867 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2868 * of -LLONG_MIN and correct the result.
2869 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002870 if (tmp.data.u.sint == LLONG_MIN) {
2871 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2872 if (smp->data.u.sint < LLONG_MAX)
2873 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002874 return 1;
2875 }
2876
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002877 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002878 * the second operand.
2879 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002880 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002881 return 1;
2882}
2883
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002884/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002885 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002886 * If the result makes an overflow, then the largest possible quantity is
2887 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002888 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002889static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002890 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002891{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002892 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002893 long long int c;
2894
Willy Tarreau7560dd42016-03-10 16:28:58 +01002895 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002896 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002897 return 0;
2898
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002899 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002900 if (!smp->data.u.sint || !tmp.data.u.sint) {
2901 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002902 return 1;
2903 }
2904
2905 /* The multiply between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002906 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002907 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002908 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2909 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002910 return 1;
2911 }
2912
2913 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002914 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002915
2916 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002917 if (smp->data.u.sint != c / tmp.data.u.sint) {
2918 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2919 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002920 return 1;
2921 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002922 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002923 return 1;
2924 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002925 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002926 return 1;
2927}
2928
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002929/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002930 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002931 * If arg_p makes the result overflow, then the largest possible quantity is
2932 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002933 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002934static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002935 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002936{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002937 struct sample tmp;
2938
Willy Tarreau7560dd42016-03-10 16:28:58 +01002939 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002940 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002941 return 0;
2942
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002943 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002944 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002945 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002946 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002947 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2948 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002949 return 1;
2950 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002951 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002952 return 1;
2953 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002954 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002955 return 1;
2956}
2957
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002958/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002959 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002960 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002961 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002962static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002963 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002964{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002965 struct sample tmp;
2966
Willy Tarreau7560dd42016-03-10 16:28:58 +01002967 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002968 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002969 return 0;
2970
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002971 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002972 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002973 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002974 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002975 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2976 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002977 return 1;
2978 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002979 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002980 return 1;
2981 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002982 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002983 return 1;
2984}
2985
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002986/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002987 * result.
2988 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002989static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002990 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002991{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002992 if (smp->data.u.sint == LLONG_MIN)
2993 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002994 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002995 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002996 return 1;
2997}
2998
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002999/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003000 * false. The output is a BOOL.
3001 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003002static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003003 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003004{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003005 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003006 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003007 return 1;
3008}
3009
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003010/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003011 * truee. The output is a BOOL.
3012 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003013static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003014 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003015{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003016 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003017 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003018 return 1;
3019}
3020
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003021/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003022 * The output is a BOOL.
3023 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003024static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003025 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003026{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003027 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003028 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003029 return 1;
3030}
3031
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003032/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003033 * The output is a BOOL.
3034 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003035static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003036 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003037{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003038 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003039 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003040 return 1;
3041}
3042
Willy Tarreau280f42b2018-02-19 15:34:12 +01003043/* appends an optional const string, an optional variable contents and another
3044 * optional const string to an existing string.
3045 */
3046static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
3047{
Willy Tarreau83061a82018-07-13 11:56:34 +02003048 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003049 struct sample tmp;
3050 int max;
3051
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003052 trash = alloc_trash_chunk();
William Dauchye9970102021-01-11 11:05:58 +01003053 if (!trash)
3054 return 0;
3055
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003056 trash->data = smp->data.u.str.data;
3057 if (trash->data > trash->size - 1)
3058 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003059
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003060 memcpy(trash->area, smp->data.u.str.area, trash->data);
3061 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003062
3063 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003064 max = arg_p[0].data.str.data;
3065 if (max > trash->size - 1 - trash->data)
3066 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003067
3068 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003069 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
3070 trash->data += max;
3071 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003072 }
3073
3074 /* append second string (variable) if it's found and we can turn it
3075 * into a string.
3076 */
3077 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaue352b9d2021-09-03 11:52:38 +02003078 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 +01003079 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
3080 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
3081
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003082 max = tmp.data.u.str.data;
3083 if (max > trash->size - 1 - trash->data)
3084 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003085
3086 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003087 memcpy(trash->area + trash->data, tmp.data.u.str.area,
3088 max);
3089 trash->data += max;
3090 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003091 }
3092 }
3093
3094 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003095 max = arg_p[2].data.str.data;
3096 if (max > trash->size - 1 - trash->data)
3097 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003098
3099 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003100 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
3101 trash->data += max;
3102 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003103 }
3104
3105 smp->data.u.str = *trash;
3106 smp->data.type = SMP_T_STR;
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003107 smp_dup(smp);
3108 free_trash_chunk(trash);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003109 return 1;
3110}
3111
3112/* This function checks the "concat" converter's arguments and extracts the
3113 * variable name and its scope.
3114 */
3115static int smp_check_concat(struct arg *args, struct sample_conv *conv,
3116 const char *file, int line, char **err)
3117{
3118 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003119 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
3120 memprintf(err, "failed to register variable name '%s'",
3121 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003122 return 0;
3123 }
3124 return 1;
3125}
3126
Nikola Sale0dbf0382022-04-03 18:11:53 +02003127/* Append delimiter (only to a non empty input) followed by the optional
3128 * variable contents concatenated with the optional sufix.
3129 */
3130static int sample_conv_add_item(const struct arg *arg_p, struct sample *smp, void *private)
3131{
3132 struct buffer *tmpbuf;
3133 struct sample tmp;
3134 size_t max;
3135 int var_available;
3136
3137 tmpbuf = alloc_trash_chunk();
3138 if (!tmpbuf)
3139 return 0;
3140
3141 tmpbuf->data = smp->data.u.str.data;
3142 if (tmpbuf->data > tmpbuf->size - 1)
3143 tmpbuf->data = tmpbuf->size - 1;
3144
3145 memcpy(tmpbuf->area, smp->data.u.str.area, tmpbuf->data);
3146 tmpbuf->area[tmpbuf->data] = 0;
3147
3148 /* Check if variable is found and we can turn into a string. */
3149 var_available = 0;
3150 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3151 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp, NULL) &&
3152 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
3153 sample_casts[tmp.data.type][SMP_T_STR](&tmp)))
3154 var_available = 1;
3155
3156 /* Append delimiter only if input is not empty and either
3157 * the variable or the suffix are not empty
3158 */
3159 if (smp->data.u.str.data && ((var_available && tmp.data.u.str.data) ||
3160 arg_p[2].data.str.data)) {
3161 max = arg_p[0].data.str.data;
3162 if (max > tmpbuf->size - 1 - tmpbuf->data)
3163 max = tmpbuf->size - 1 - tmpbuf->data;
3164
3165 if (max) {
3166 memcpy(tmpbuf->area + tmpbuf->data, arg_p[0].data.str.area, max);
3167 tmpbuf->data += max;
3168 tmpbuf->area[tmpbuf->data] = 0;
3169 }
3170 }
3171
3172 /* Append variable contents if variable is found and turned into string. */
3173 if (var_available) {
3174 max = tmp.data.u.str.data;
3175 if (max > tmpbuf->size - 1 - tmpbuf->data)
3176 max = tmpbuf->size - 1 - tmpbuf->data;
3177
3178 if (max) {
3179 memcpy(tmpbuf->area + tmpbuf->data, tmp.data.u.str.area, max);
3180 tmpbuf->data += max;
3181 tmpbuf->area[tmpbuf->data] = 0;
3182 }
3183 }
3184
3185 /* Append optional suffix. */
3186 max = arg_p[2].data.str.data;
3187 if (max > tmpbuf->size - 1 - tmpbuf->data)
3188 max = tmpbuf->size - 1 - tmpbuf->data;
3189
3190 if (max) {
3191 memcpy(tmpbuf->area + tmpbuf->data, arg_p[2].data.str.area, max);
3192 tmpbuf->data += max;
3193 tmpbuf->area[tmpbuf->data] = 0;
3194 }
3195
3196 smp->data.u.str = *tmpbuf;
3197 smp->data.type = SMP_T_STR;
3198 smp_dup(smp);
3199 free_trash_chunk(tmpbuf);
3200 return 1;
3201}
3202
3203/* Check the "add_item" converter's arguments and extracts the
3204 * variable name and its scope.
3205 */
3206static int smp_check_add_item(struct arg *args, struct sample_conv *conv,
3207 const char *file, int line, char **err)
3208{
3209 /* Try to decode a variable. */
3210 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
3211 memprintf(err, "failed to register variable name '%s'",
3212 args[1].data.str.area);
3213 return 0;
3214 }
3215
3216 if (args[1].data.str.data == 0 && args[2].data.str.data == 0) {
3217 memprintf(err, "one of the optional arguments has to be nonempty");
3218 return 0;
3219 }
3220
3221 return 1;
3222}
3223
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003224/* Compares string with a variable containing a string. Return value
Tim Duesterhusca097c12018-04-27 21:18:45 +02003225 * is compatible with strcmp(3)'s return value.
3226 */
3227static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
3228{
3229 struct sample tmp;
3230 int max, result;
3231
3232 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3233 if (arg_p[0].type != ARGT_VAR)
3234 return 0;
Willy Tarreau2476ff12021-10-06 15:30:52 +02003235
3236 if (!sample_conv_var2smp(&arg_p[0].data.var, &tmp, SMP_T_STR))
Tim Duesterhusca097c12018-04-27 21:18:45 +02003237 return 0;
3238
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003239 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
3240 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003241 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003242 if (smp->data.u.str.data != tmp.data.u.str.data) {
3243 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02003244 result = -1;
3245 }
3246 else {
3247 result = 1;
3248 }
3249 }
3250 }
3251
3252 smp->data.u.sint = result;
3253 smp->data.type = SMP_T_SINT;
3254 return 1;
3255}
3256
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02003257/* Takes a boolean as input. Returns the first argument if that boolean is true and
3258 * the second argument otherwise.
3259 */
3260static int sample_conv_iif(const struct arg *arg_p, struct sample *smp, void *private)
3261{
3262 smp->data.type = SMP_T_STR;
3263 smp->flags |= SMP_F_CONST;
3264
3265 if (smp->data.u.sint) {
3266 smp->data.u.str.data = arg_p[0].data.str.data;
3267 smp->data.u.str.area = arg_p[0].data.str.area;
3268 }
3269 else {
3270 smp->data.u.str.data = arg_p[1].data.str.data;
3271 smp->data.u.str.area = arg_p[1].data.str.area;
3272 }
3273
3274 return 1;
3275}
3276
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003277#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
3278#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
3279#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
3280
3281/*
3282 * Extract the field value of an input binary sample. Takes a mandatory argument:
3283 * the protocol buffers field identifier (dotted notation) internally represented
3284 * as an array of unsigned integers and its size.
3285 * Return 1 if the field was found, 0 if not.
3286 */
3287static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
3288{
3289 unsigned char *pos;
3290 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003291
3292 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003293 grpc_left = smp->data.u.str.data;
3294
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01003295 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003296 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003297
3298 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
3299
3300 pos += GRPC_MSG_HEADER_SZ;
3301 grpc_left -= GRPC_MSG_HEADER_SZ;
3302
3303 if (grpc_left < left)
3304 return 0;
3305
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01003306 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
3307 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003308
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003309 grpc_left -= grpc_msg_len;
3310 }
3311
3312 return 0;
3313}
3314
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003315static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
3316{
3317 unsigned char *pos;
3318 size_t left;
3319
3320 pos = (unsigned char *)smp->data.u.str.area;
3321 left = smp->data.u.str.data;
3322
3323 return protobuf_field_lookup(arg_p, smp, &pos, &left);
3324}
3325
3326static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
3327 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003328{
3329 if (!args[1].type) {
3330 args[1].type = ARGT_SINT;
3331 args[1].data.sint = PBUF_T_BINARY;
3332 }
3333 else {
3334 int pbuf_type;
3335
3336 pbuf_type = protobuf_type(args[1].data.str.area);
3337 if (pbuf_type == -1) {
3338 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
3339 return 0;
3340 }
3341
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003342 chunk_destroy(&args[1].data.str);
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003343 args[1].type = ARGT_SINT;
3344 args[1].data.sint = pbuf_type;
3345 }
3346
3347 return 1;
3348}
3349
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003350/*
3351 * Extract the tag value of an input binary sample. Takes a mandatory argument:
3352 * the FIX protocol tag identifier.
3353 * Return 1 if the tag was found, 0 if not.
3354 */
3355static int sample_conv_fix_tag_value(const struct arg *arg_p, struct sample *smp, void *private)
3356{
3357 struct ist value;
3358
3359 smp->flags &= ~SMP_F_MAY_CHANGE;
3360 value = fix_tag_value(ist2(smp->data.u.str.area, smp->data.u.str.data),
3361 arg_p[0].data.sint);
3362 if (!istlen(value)) {
Christopher Fauleta5aa0822021-10-13 17:58:53 +02003363 if (isttest(value)) {
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003364 /* value != IST_NULL, need more data */
3365 smp->flags |= SMP_F_MAY_CHANGE;
3366 }
3367 return 0;
3368 }
3369
3370 smp->data.u.str = ist2buf(value);
3371 smp->flags |= SMP_F_CONST;
3372
3373 return 1;
3374}
3375
3376/* This function checks the "fix_tag_value" converter configuration.
3377 * It expects a "known" (by HAProxy) tag name or ID.
3378 * Tag string names are converted to their ID counterpart because this is the
3379 * format they are sent over the wire.
3380 */
3381static int sample_conv_fix_value_check(struct arg *args, struct sample_conv *conv,
3382 const char *file, int line, char **err)
3383{
3384 struct ist str;
3385 unsigned int tag;
3386
3387 str = ist2(args[0].data.str.area, args[0].data.str.data);
3388 tag = fix_tagid(str);
3389 if (!tag) {
3390 memprintf(err, "Unknown FIX tag name '%s'", args[0].data.str.area);
3391 return 0;
3392 }
3393
3394 chunk_destroy(&args[0].data.str);
3395 args[0].type = ARGT_SINT;
3396 args[0].data.sint = tag;
3397
3398 return 1;
3399}
3400
3401/*
3402 * Checks that a buffer contains a valid FIX message
3403 *
3404 * Return 1 if the check could be run, 0 if not.
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003405 * The result of the analyse itself is stored in <smp> as a boolean
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003406 */
3407static int sample_conv_fix_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3408{
3409 struct ist msg;
3410
3411 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3412
3413 smp->flags &= ~SMP_F_MAY_CHANGE;
3414 switch (fix_validate_message(msg)) {
3415 case FIX_VALID_MESSAGE:
3416 smp->data.type = SMP_T_BOOL;
3417 smp->data.u.sint = 1;
3418 return 1;
3419 case FIX_NEED_MORE_DATA:
3420 smp->flags |= SMP_F_MAY_CHANGE;
3421 return 0;
3422 case FIX_INVALID_MESSAGE:
3423 smp->data.type = SMP_T_BOOL;
3424 smp->data.u.sint = 0;
3425 return 1;
3426 }
3427 return 0;
3428}
3429
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003430/*
3431 * Extract the field value of an input binary sample containing an MQTT packet.
3432 * Takes 2 mandatory arguments:
3433 * - packet type
3434 * - field name
3435 *
3436 * return 1 if the field was found, 0 if not.
3437 */
3438static int sample_conv_mqtt_field_value(const struct arg *arg_p, struct sample *smp, void *private)
3439{
3440 struct ist pkt, value;
3441 int type, fieldname_id;
3442
3443 pkt = ist2(smp->data.u.str.area, smp->data.u.str.data);
3444 type = arg_p[0].data.sint;
3445 fieldname_id = arg_p[1].data.sint;
3446
3447 smp->flags &= ~SMP_F_MAY_CHANGE;
3448 value = mqtt_field_value(pkt, type, fieldname_id);
3449 if (!istlen(value)) {
3450 if (isttest(value)) {
3451 /* value != IST_NULL, need more data */
3452 smp->flags |= SMP_F_MAY_CHANGE;
3453 }
3454 return 0;
3455 }
3456
3457 smp->data.u.str = ist2buf(value);
3458 smp->flags |= SMP_F_CONST;
3459 return 1;
3460}
3461
3462/*
3463 * this function checks the "mqtt_field_value" converter configuration.
3464 * It expects a known packet type name or ID and a field name, in this order
3465 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003466 * Args[0] will be turned into a MQTT_CPT_* value for direct matching when parsing
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003467 * a packet.
3468 */
3469static int sample_conv_mqtt_field_value_check(struct arg *args, struct sample_conv *conv,
3470 const char *file, int line, char **err)
3471{
3472 int type, fieldname_id;
3473
3474 /* check the MQTT packet type is valid */
3475 type = mqtt_typeid(ist2(args[0].data.str.area, args[0].data.str.data));
3476 if (type == MQTT_CPT_INVALID) {
3477 memprintf(err, "Unknown MQTT type '%s'", args[0].data.str.area);
3478 return 0;
3479 }
3480
3481 /* check the field name belongs to the MQTT packet type */
3482 fieldname_id = mqtt_check_type_fieldname(type, ist2(args[1].data.str.area, args[1].data.str.data));
3483 if (fieldname_id == MQTT_FN_INVALID) {
3484 memprintf(err, "Unknown MQTT field name '%s' for packet type '%s'", args[1].data.str.area,
3485 args[0].data.str.area);
3486 return 0;
3487 }
3488
3489 /* save numeric counterparts of type and field name */
3490 chunk_destroy(&args[0].data.str);
3491 chunk_destroy(&args[1].data.str);
3492 args[0].type = ARGT_SINT;
3493 args[0].data.sint = type;
3494 args[1].type = ARGT_SINT;
3495 args[1].data.sint = fieldname_id;
3496
3497 return 1;
3498}
3499
3500/*
3501 * Checks that <smp> contains a valid MQTT message
3502 *
3503 * The function returns 1 if the check was run to its end, 0 otherwise.
3504 * The result of the analyse itself is stored in <smp> as a boolean.
3505 */
3506static int sample_conv_mqtt_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3507{
3508 struct ist msg;
3509
3510 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3511
3512 smp->flags &= ~SMP_F_MAY_CHANGE;
3513 switch (mqtt_validate_message(msg, NULL)) {
3514 case FIX_VALID_MESSAGE:
3515 smp->data.type = SMP_T_BOOL;
3516 smp->data.u.sint = 1;
3517 return 1;
3518 case FIX_NEED_MORE_DATA:
3519 smp->flags |= SMP_F_MAY_CHANGE;
3520 return 0;
3521 case FIX_INVALID_MESSAGE:
3522 smp->data.type = SMP_T_BOOL;
3523 smp->data.u.sint = 0;
3524 return 1;
3525 }
3526 return 0;
3527}
3528
Tim Duesterhusca097c12018-04-27 21:18:45 +02003529/* This function checks the "strcmp" converter's arguments and extracts the
3530 * variable name and its scope.
3531 */
3532static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
3533 const char *file, int line, char **err)
3534{
Willy Tarreaua1169b62021-05-08 06:50:28 +02003535 if (!args[0].data.str.data) {
3536 memprintf(err, "missing variable name");
3537 return 0;
3538 }
3539
Tim Duesterhusca097c12018-04-27 21:18:45 +02003540 /* Try to decode a variable. */
3541 if (vars_check_arg(&args[0], NULL))
3542 return 1;
3543
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003544 memprintf(err, "failed to register variable name '%s'",
3545 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003546 return 0;
3547}
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003548
Christopher Faulet4ccc12f2020-04-01 09:08:32 +02003549/**/
3550static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private)
3551{
3552 struct buffer *tmp;
3553 uint32_t n;
3554
3555 n = htonl((uint32_t)smp->data.u.sint);
3556 tmp = get_trash_chunk();
3557
3558 memcpy(b_head(tmp), &n, 4);
3559 b_add(tmp, 4);
3560
3561 smp->data.u.str = *tmp;
3562 smp->data.type = SMP_T_BIN;
3563 return 1;
3564}
3565
Christopher Fauletea159d62020-04-01 16:21:44 +02003566/**/
3567static int sample_conv_cut_crlf(const struct arg *arg_p, struct sample *smp, void *private)
3568{
3569 char *p;
3570 size_t l;
3571
3572 p = smp->data.u.str.area;
3573 for (l = 0; l < smp->data.u.str.data; l++) {
3574 if (*(p+l) == '\r' || *(p+l) == '\n')
3575 break;
3576 }
3577 smp->data.u.str.data = l;
3578 return 1;
3579}
3580
Christopher Faulet51fc9d12020-04-01 17:24:41 +02003581/**/
3582static int sample_conv_ltrim(const struct arg *arg_p, struct sample *smp, void *private)
3583{
3584 char *delimiters, *p;
3585 size_t dlen, l;
3586
3587 delimiters = arg_p[0].data.str.area;
3588 dlen = arg_p[0].data.str.data;
3589
3590 l = smp->data.u.str.data;
3591 p = smp->data.u.str.area;
3592 while (l && memchr(delimiters, *p, dlen) != NULL) {
3593 p++;
3594 l--;
3595 }
3596
3597 smp->data.u.str.area = p;
3598 smp->data.u.str.data = l;
3599 return 1;
3600}
3601
Christopher Faulet568415a2020-04-01 17:24:47 +02003602/**/
3603static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *private)
3604{
3605 char *delimiters, *p;
3606 size_t dlen, l;
3607
3608 delimiters = arg_p[0].data.str.area;
3609 dlen = arg_p[0].data.str.data;
3610
3611 l = smp->data.u.str.data;
3612 p = smp->data.u.str.area + l - 1;
3613 while (l && memchr(delimiters, *p, dlen) != NULL) {
3614 p--;
3615 l--;
3616 }
3617
3618 smp->data.u.str.data = l;
3619 return 1;
3620}
3621
Alex51c8ad42021-04-15 16:45:15 +02003622/* This function checks the "json_query" converter's arguments. */
3623static int sample_check_json_query(struct arg *arg, struct sample_conv *conv,
3624 const char *file, int line, char **err)
3625{
3626 if (arg[0].data.str.data == 0) {
3627 memprintf(err, "json_path must not be empty");
3628 return 0;
3629 }
3630
3631 if (arg[1].data.str.data != 0) {
3632 if (strcmp(arg[1].data.str.area, "int") != 0) {
3633 memprintf(err, "output_type only supports \"int\" as argument");
3634 return 0;
3635 } else {
3636 arg[1].type = ARGT_SINT;
3637 arg[1].data.sint = 0;
3638 }
3639 }
3640 return 1;
3641}
3642
3643/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
3644 * the recommendation for interoperable integers in section 6 of RFC 7159.
3645 */
3646#define JSON_INT_MAX ((1LL << 53) - 1)
3647#define JSON_INT_MIN (-JSON_INT_MAX)
3648
3649/* This sample function get the value from a given json string.
3650 * The mjson library is used to parse the JSON struct
3651 */
3652static int sample_conv_json_query(const struct arg *args, struct sample *smp, void *private)
3653{
3654 struct buffer *trash = get_trash_chunk();
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003655 const char *token; /* holds the temporary string from mjson_find */
3656 int token_size; /* holds the length of <token> */
Alex51c8ad42021-04-15 16:45:15 +02003657
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003658 enum mjson_tok token_type;
Alex51c8ad42021-04-15 16:45:15 +02003659
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003660 token_type = mjson_find(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &token, &token_size);
3661
3662 switch (token_type) {
Alex51c8ad42021-04-15 16:45:15 +02003663 case MJSON_TOK_NUMBER:
3664 if (args[1].type == ARGT_SINT) {
Willy Tarreaufb601952021-05-14 08:51:53 +02003665 smp->data.u.sint = strtoll(token, NULL, 0);
Alex51c8ad42021-04-15 16:45:15 +02003666
3667 if (smp->data.u.sint < JSON_INT_MIN || smp->data.u.sint > JSON_INT_MAX)
3668 return 0;
3669
3670 smp->data.type = SMP_T_SINT;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003671
3672 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003673 } else {
3674 double double_val;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003675
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003676 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 +02003677 return 0;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003678
3679 trash->data = snprintf(trash->area,trash->size,"%g",double_val);
3680 smp->data.u.str = *trash;
3681 smp->data.type = SMP_T_STR;
3682
3683 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003684 }
Alex51c8ad42021-04-15 16:45:15 +02003685 case MJSON_TOK_TRUE:
3686 smp->data.type = SMP_T_BOOL;
3687 smp->data.u.sint = 1;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003688
3689 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003690 case MJSON_TOK_FALSE:
3691 smp->data.type = SMP_T_BOOL;
3692 smp->data.u.sint = 0;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003693
3694 return 1;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003695 case MJSON_TOK_STRING: {
3696 int len;
3697
3698 len = mjson_get_string(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, trash->area, trash->size);
3699
3700 if (len == -1) {
Alex51c8ad42021-04-15 16:45:15 +02003701 /* invalid string */
3702 return 0;
Alex51c8ad42021-04-15 16:45:15 +02003703 }
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003704
3705 trash->data = len;
3706 smp->data.u.str = *trash;
3707 smp->data.type = SMP_T_STR;
3708
3709 return 1;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003710 }
Tim Duesterhus8f3bc8f2021-04-15 18:14:32 +02003711 case MJSON_TOK_NULL:
3712 case MJSON_TOK_ARRAY:
3713 case MJSON_TOK_OBJECT:
3714 /* We cannot handle these. */
3715 return 0;
3716 case MJSON_TOK_INVALID:
3717 /* Nothing matches the query. */
3718 return 0;
3719 case MJSON_TOK_KEY:
3720 /* This is not a valid return value according to the
3721 * mjson documentation, but we handle it to benefit
3722 * from '-Wswitch'.
3723 */
Alex51c8ad42021-04-15 16:45:15 +02003724 return 0;
3725 }
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003726
3727 my_unreachable();
3728 return 0;
Alex51c8ad42021-04-15 16:45:15 +02003729}
3730
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003731#ifdef USE_OPENSSL
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +02003732static int sample_conv_jwt_verify_check(struct arg *args, struct sample_conv *conv,
3733 const char *file, int line, char **err)
3734{
3735 vars_check_arg(&args[0], NULL);
3736 vars_check_arg(&args[1], NULL);
3737
3738 if (args[0].type == ARGT_STR) {
3739 enum jwt_alg alg = jwt_parse_alg(args[0].data.str.area, args[0].data.str.data);
3740
3741 switch(alg) {
3742 case JWT_ALG_DEFAULT:
Tim Duesterhusab896ee2021-10-29 18:06:55 +02003743 memprintf(err, "unknown JWT algorithm: %s", args[0].data.str.area);
3744 return 0;
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +02003745
3746 case JWS_ALG_PS256:
3747 case JWS_ALG_PS384:
3748 case JWS_ALG_PS512:
3749 memprintf(err, "RSASSA-PSS JWS signing not managed yet");
Tim Duesterhusab896ee2021-10-29 18:06:55 +02003750 return 0;
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +02003751
3752 default:
3753 break;
3754 }
3755 }
3756
3757 if (args[1].type == ARGT_STR) {
3758 jwt_tree_load_cert(args[1].data.str.area, args[1].data.str.data, err);
3759 }
3760
3761 return 1;
3762}
3763
3764/* Check that a JWT's signature is correct */
3765static int sample_conv_jwt_verify(const struct arg *args, struct sample *smp, void *private)
3766{
3767 struct sample alg_smp, key_smp;
3768
3769 smp->data.type = SMP_T_SINT;
3770 smp->data.u.sint = 0;
3771
3772 smp_set_owner(&alg_smp, smp->px, smp->sess, smp->strm, smp->opt);
3773 smp_set_owner(&key_smp, smp->px, smp->sess, smp->strm, smp->opt);
3774 if (!sample_conv_var2smp_str(&args[0], &alg_smp))
3775 return 0;
3776 if (!sample_conv_var2smp_str(&args[1], &key_smp))
3777 return 0;
3778
3779 smp->data.u.sint = jwt_verify(&smp->data.u.str, &alg_smp.data.u.str,
3780 &key_smp.data.u.str);
3781
3782 return 1;
3783}
3784
3785
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003786/*
3787 * Returns the decoded header or payload of a JWT if no parameter is given, or
3788 * the value of the specified field of the corresponding JWT subpart if a
3789 * parameter is given.
3790 */
3791static int sample_conv_jwt_member_query(const struct arg *args, struct sample *smp,
3792 void *private, enum jwt_elt member)
3793{
3794 struct jwt_item items[JWT_ELT_MAX] = { { 0 } };
3795 unsigned int item_num = member + 1; /* We don't need to tokenize the full token */
3796 struct buffer *decoded_header = get_trash_chunk();
3797 int retval = 0;
Willy Tarreaue20e0262021-10-15 12:10:24 +02003798 int ret;
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003799
3800 jwt_tokenize(&smp->data.u.str, items, &item_num);
3801
3802 if (item_num < member + 1)
3803 goto end;
3804
Willy Tarreaue20e0262021-10-15 12:10:24 +02003805 ret = base64urldec(items[member].start, items[member].length,
3806 decoded_header->area, decoded_header->size);
3807 if (ret == -1)
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003808 goto end;
3809
Willy Tarreaue20e0262021-10-15 12:10:24 +02003810 decoded_header->data = ret;
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003811 if (args[0].type != ARGT_STR) {
3812 smp->data.u.str = *decoded_header;
3813 smp->data.type = SMP_T_STR;
3814 goto end;
3815 }
3816
3817 /* We look for a specific field of the header or payload part of the JWT */
3818 smp->data.u.str = *decoded_header;
3819
3820 retval = sample_conv_json_query(args, smp, private);
3821
3822end:
3823 return retval;
3824}
3825
3826/* This function checks the "jwt_header_query" and "jwt_payload_query" converters' arguments.
3827 * It is based on the "json_query" converter's check with the only difference
3828 * being that the jwt converters can take 0 parameters as well.
3829 */
3830static int sample_conv_jwt_query_check(struct arg *arg, struct sample_conv *conv,
3831 const char *file, int line, char **err)
3832{
3833 if (arg[1].data.str.data != 0) {
3834 if (strcmp(arg[1].data.str.area, "int") != 0) {
3835 memprintf(err, "output_type only supports \"int\" as argument");
3836 return 0;
3837 } else {
3838 arg[1].type = ARGT_SINT;
3839 arg[1].data.sint = 0;
3840 }
3841 }
3842 return 1;
3843}
3844
3845/*
3846 * If no parameter is given, return the decoded header part of a JWT (the first
3847 * base64 encoded part, corresponding to the JOSE header).
3848 * If a parameter is given, this converter acts as a "json_query" on this
3849 * decoded JSON.
3850 */
3851static int sample_conv_jwt_header_query(const struct arg *args, struct sample *smp, void *private)
3852{
3853 return sample_conv_jwt_member_query(args, smp, private, JWT_ELT_JOSE);
3854}
3855
3856/*
3857 * If no parameter is given, return the decoded payload part of a JWT (the
3858 * second base64 encoded part, which contains all the claims). If a parameter
3859 * is given, this converter acts as a "json_query" on this decoded JSON.
3860 */
3861static int sample_conv_jwt_payload_query(const struct arg *args, struct sample *smp, void *private)
3862{
3863 return sample_conv_jwt_member_query(args, smp, private, JWT_ELT_CLAIMS);
3864}
3865
3866#endif /* USE_OPENSSL */
Alex51c8ad42021-04-15 16:45:15 +02003867
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003868/************************************************************************/
3869/* All supported sample fetch functions must be declared here */
3870/************************************************************************/
3871
3872/* force TRUE to be returned at the fetch level */
3873static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003874smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003875{
Willy Tarreau280f42b2018-02-19 15:34:12 +01003876 if (!smp_make_rw(smp))
3877 return 0;
3878
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003879 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003880 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003881 return 1;
3882}
3883
3884/* force FALSE to be returned at the fetch level */
3885static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003886smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003887{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003888 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003889 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003890 return 1;
3891}
3892
3893/* retrieve environment variable $1 as a string */
3894static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003895smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003896{
3897 char *env;
3898
Christopher Faulete6e7a582021-01-29 11:29:28 +01003899 if (args[0].type != ARGT_STR)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003900 return 0;
3901
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003902 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003903 if (!env)
3904 return 0;
3905
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003906 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003907 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003908 smp->data.u.str.area = env;
3909 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003910 return 1;
3911}
3912
Damien Claisseae6f1252019-10-30 15:57:28 +00003913/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3914 * optional string representing the unit of the result: "s" for seconds, "ms" for
3915 * milliseconds and "us" for microseconds.
3916 * Returns 0 on error and non-zero if OK.
3917 */
3918int smp_check_date_unit(struct arg *args, char **err)
3919{
3920 if (args[1].type == ARGT_STR) {
Christopher Faulet95917132020-08-05 23:07:07 +02003921 long long int unit;
3922
Damien Claisseae6f1252019-10-30 15:57:28 +00003923 if (strcmp(args[1].data.str.area, "s") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003924 unit = TIME_UNIT_S;
Damien Claisseae6f1252019-10-30 15:57:28 +00003925 }
3926 else if (strcmp(args[1].data.str.area, "ms") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003927 unit = TIME_UNIT_MS;
Damien Claisseae6f1252019-10-30 15:57:28 +00003928 }
3929 else if (strcmp(args[1].data.str.area, "us") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003930 unit = TIME_UNIT_US;
Damien Claisseae6f1252019-10-30 15:57:28 +00003931 }
3932 else {
3933 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
3934 args[1].data.str.area);
3935 return 0;
3936 }
Christopher Faulet95917132020-08-05 23:07:07 +02003937
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003938 chunk_destroy(&args[1].data.str);
Damien Claisseae6f1252019-10-30 15:57:28 +00003939 args[1].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02003940 args[1].data.sint = unit;
Damien Claisseae6f1252019-10-30 15:57:28 +00003941 }
3942 else if (args[1].type != ARGT_STOP) {
3943 memprintf(err, "Unexpected arg type");
3944 return 0;
3945 }
3946
3947 return 1;
3948}
3949
3950/* retrieve the current local date in epoch time, converts it to milliseconds
3951 * or microseconds if asked to in optional args[1] unit param, and applies an
3952 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003953 */
3954static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003955smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003956{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003957 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003958
Damien Claisseae6f1252019-10-30 15:57:28 +00003959 /* report in milliseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003960 if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003961 smp->data.u.sint *= 1000;
3962 smp->data.u.sint += date.tv_usec / 1000;
3963 }
3964 /* report in microseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003965 else if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003966 smp->data.u.sint *= 1000000;
3967 smp->data.u.sint += date.tv_usec;
3968 }
3969
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003970 /* add offset */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003971 if (args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003972 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003973
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003974 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003975 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3976 return 1;
3977}
3978
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003979/* retrieve the current microsecond part of the date */
3980static int
3981smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3982{
3983 smp->data.u.sint = date.tv_usec;
3984 smp->data.type = SMP_T_SINT;
3985 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3986 return 1;
3987}
3988
3989
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003990/* returns the hostname */
3991static int
3992smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3993{
3994 smp->data.type = SMP_T_STR;
3995 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003996 smp->data.u.str.area = hostname;
3997 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003998 return 1;
3999}
4000
Willy Tarreau0f30d262014-11-24 16:02:05 +01004001/* returns the number of processes */
4002static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004003smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01004004{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004005 smp->data.type = SMP_T_SINT;
Willy Tarreau91358592021-06-15 08:08:04 +02004006 smp->data.u.sint = 1;
Willy Tarreau0f30d262014-11-24 16:02:05 +01004007 return 1;
4008}
4009
4010/* returns the number of the current process (between 1 and nbproc */
4011static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004012smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01004013{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004014 smp->data.type = SMP_T_SINT;
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004015 smp->data.u.sint = 1;
Willy Tarreau0f30d262014-11-24 16:02:05 +01004016 return 1;
4017}
4018
Christopher Faulet34adb2a2017-11-21 21:45:38 +01004019/* returns the number of the current thread (between 1 and nbthread */
4020static int
4021smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
4022{
4023 smp->data.type = SMP_T_SINT;
4024 smp->data.u.sint = tid;
4025 return 1;
4026}
4027
Willy Tarreau84310e22014-02-14 11:59:04 +01004028/* generate a random 32-bit integer for whatever purpose, with an optional
4029 * range specified in argument.
4030 */
4031static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004032smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01004033{
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01004034 smp->data.u.sint = ha_random32();
Willy Tarreau84310e22014-02-14 11:59:04 +01004035
4036 /* reduce if needed. Don't do a modulo, use all bits! */
Christopher Faulete6e7a582021-01-29 11:29:28 +01004037 if (args[0].type == ARGT_SINT)
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01004038 smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32;
Willy Tarreau84310e22014-02-14 11:59:04 +01004039
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004040 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01004041 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4042 return 1;
4043}
4044
Willy Tarreau0f30d262014-11-24 16:02:05 +01004045/* returns true if the current process is stopping */
4046static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004047smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01004048{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004049 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004050 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01004051 return 1;
4052}
4053
Willy Tarreau70fe9442018-11-22 16:07:39 +01004054/* returns the number of calls of the current stream's process_stream() */
4055static int
4056smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
4057{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004058 if (!smp->strm)
4059 return 0;
4060
Willy Tarreau70fe9442018-11-22 16:07:39 +01004061 smp->data.type = SMP_T_SINT;
4062 smp->data.u.sint = smp->strm->task->calls;
4063 return 1;
4064}
4065
4066/* returns the average number of nanoseconds spent processing the stream per call */
4067static int
4068smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
4069{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004070 if (!smp->strm)
4071 return 0;
4072
Willy Tarreau70fe9442018-11-22 16:07:39 +01004073 smp->data.type = SMP_T_SINT;
4074 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
4075 return 1;
4076}
4077
4078/* returns the total number of nanoseconds spent processing the stream */
4079static int
4080smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
4081{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004082 if (!smp->strm)
4083 return 0;
4084
Willy Tarreau70fe9442018-11-22 16:07:39 +01004085 smp->data.type = SMP_T_SINT;
4086 smp->data.u.sint = smp->strm->task->cpu_time;
4087 return 1;
4088}
4089
4090/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
4091static int
4092smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
4093{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004094 if (!smp->strm)
4095 return 0;
4096
Willy Tarreau70fe9442018-11-22 16:07:39 +01004097 smp->data.type = SMP_T_SINT;
4098 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
4099 return 1;
4100}
4101
4102/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
4103static int
4104smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
4105{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004106 if (!smp->strm)
4107 return 0;
4108
Willy Tarreau70fe9442018-11-22 16:07:39 +01004109 smp->data.type = SMP_T_SINT;
4110 smp->data.u.sint = smp->strm->task->lat_time;
4111 return 1;
4112}
4113
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004114static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
4115{
4116 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004117 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004118 smp->data.u.str.area = args[0].data.str.area;
4119 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004120 return 1;
4121}
4122
4123static int smp_check_const_bool(struct arg *args, char **err)
4124{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004125 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
4126 strcasecmp(args[0].data.str.area, "1") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004127 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004128 args[0].type = ARGT_SINT;
4129 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004130 return 1;
4131 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004132 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
4133 strcasecmp(args[0].data.str.area, "0") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004134 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004135 args[0].type = ARGT_SINT;
4136 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004137 return 1;
4138 }
4139 memprintf(err, "Expects 'true', 'false', '0' or '1'");
4140 return 0;
4141}
4142
4143static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
4144{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004145 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004146 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004147 return 1;
4148}
4149
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02004150static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004151{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004152 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004153 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004154 return 1;
4155}
4156
4157static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
4158{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004159 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004160 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004161 return 1;
4162}
4163
4164static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
4165{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004166 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004167 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004168 return 1;
4169}
4170
4171static int smp_check_const_bin(struct arg *args, char **err)
4172{
David Carlier64a16ab2016-04-08 10:37:02 +01004173 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004174 int binstrlen;
4175
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004176 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004177 return 0;
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004178 chunk_destroy(&args[0].data.str);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004179 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004180 args[0].data.str.area = binstr;
4181 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004182 return 1;
4183}
4184
4185static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
4186{
4187 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004188 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004189 smp->data.u.str.area = args[0].data.str.area;
4190 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004191 return 1;
4192}
4193
4194static int smp_check_const_meth(struct arg *args, char **err)
4195{
4196 enum http_meth_t meth;
4197 int i;
4198
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004199 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004200 if (meth != HTTP_METH_OTHER) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004201 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004202 args[0].type = ARGT_SINT;
4203 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004204 } else {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05004205 /* Check method avalaibility. A method is a token defined as :
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004206 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
4207 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
4208 * token = 1*tchar
4209 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004210 for (i = 0; i < args[0].data.str.data; i++) {
4211 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004212 memprintf(err, "expects valid method.");
4213 return 0;
4214 }
4215 }
4216 }
4217 return 1;
4218}
4219
4220static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
4221{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004222 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004223 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004224 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004225 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004226 smp->data.u.meth.str.area = "";
4227 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004228 } else {
4229 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004230 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004231 smp->data.u.meth.str.area = args[0].data.str.area;
4232 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004233 }
4234 return 1;
4235}
4236
Luca Schimweg8a694b82019-09-10 15:42:52 +02004237// This function checks the "uuid" sample's arguments.
4238// Function won't get called when no parameter is specified (maybe a bug?)
4239static int smp_check_uuid(struct arg *args, char **err)
4240{
4241 if (!args[0].type) {
4242 args[0].type = ARGT_SINT;
4243 args[0].data.sint = 4;
4244 }
4245 else if (args[0].data.sint != 4) {
4246 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
4247 return 0;
4248 }
4249
4250 return 1;
4251}
4252
4253// Generate a RFC4122 UUID (default is v4 = fully random)
4254static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
4255{
4256 if (args[0].data.sint == 4 || !args[0].type) {
Willy Tarreauee3bcdd2020-03-08 17:48:17 +01004257 ha_generate_uuid(&trash);
Luca Schimweg8a694b82019-09-10 15:42:52 +02004258 smp->data.type = SMP_T_STR;
4259 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4260 smp->data.u.str = trash;
4261 return 1;
4262 }
4263
4264 // more implementations of other uuid formats possible here
4265 return 0;
4266}
4267
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004268/* Note: must not be declared <const> as its list will be overwritten.
4269 * Note: fetches that may return multiple types must be declared as the lowest
4270 * common denominator, the type that can be casted into all other ones. For
4271 * instance IPv4/IPv6 must be declared IPv4.
4272 */
4273static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau0209c972021-03-26 12:03:11 +01004274 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4275 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4276 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_CONST },
4277 { "date", smp_fetch_date, ARG2(0,SINT,STR), smp_check_date_unit, SMP_T_SINT, SMP_USE_CONST },
4278 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4279 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_CONST },
4280 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_CONST },
4281 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4282 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4283 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_CONST },
Willy Tarreau0f30d262014-11-24 16:02:05 +01004284 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau0209c972021-03-26 12:03:11 +01004285 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_CONST },
Willy Tarreau70fe9442018-11-22 16:07:39 +01004286
4287 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4288 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4289 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4290 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4291 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004292
Willy Tarreau0209c972021-03-26 12:03:11 +01004293 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_CONST },
4294 { "bool", smp_fetch_const_bool, ARG1(1,STR), smp_check_const_bool, SMP_T_BOOL, SMP_USE_CONST },
4295 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_CONST },
4296 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_CONST },
4297 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_CONST },
4298 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_CONST },
4299 { "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 +02004300
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004301 { /* END */ },
4302}};
4303
Willy Tarreau0108d902018-11-25 19:14:37 +01004304INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
4305
Emeric Brun107ca302010-01-04 16:16:05 +01004306/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02004307static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Nikola Sale0dbf0382022-04-03 18:11:53 +02004308 { "add_item",sample_conv_add_item, ARG3(2,STR,STR,STR), smp_check_add_item, SMP_T_STR, SMP_T_STR },
Moemen MHEDHBI848216f2021-04-02 01:05:07 +02004309 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
4310 { "b64dec", sample_conv_base642bin, 0, NULL, SMP_T_STR, SMP_T_BIN },
4311 { "base64", sample_conv_bin2base64, 0, NULL, SMP_T_BIN, SMP_T_STR },
Nikola Sale0dbf0382022-04-03 18:11:53 +02004312 { "concat", sample_conv_concat, ARG3(1,STR,STR,STR), smp_check_concat, SMP_T_STR, SMP_T_STR },
Moemen MHEDHBI848216f2021-04-02 01:05:07 +02004313 { "ub64enc", sample_conv_bin2base64url,0, NULL, SMP_T_BIN, SMP_T_STR },
4314 { "ub64dec", sample_conv_base64url2bin,0, NULL, SMP_T_STR, SMP_T_BIN },
4315 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
4316 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
4317 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Marcin Deranek40ca09c2021-07-13 14:05:24 +02004318 { "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 +02004319 { "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 +02004320 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
4321 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
4322 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
4323 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4324 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4325 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4326 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4327 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4328 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4329 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4330 { "xxh3", sample_conv_xxh3, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4331 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4332 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4333 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
4334 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
4335 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4336 { "word", sample_conv_word, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4337 { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
4338 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Willy Tarreau99ea1882021-10-06 15:37:17 +02004339 { "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 +01004340
4341 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01004342 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
4343 { "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 +01004344
Baptiste Assmanne138dda2020-10-22 15:39:03 +02004345 /* FIX converters */
4346 { "fix_is_valid", sample_conv_fix_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4347 { "fix_tag_value", sample_conv_fix_tag_value, ARG1(1,STR), sample_conv_fix_value_check, SMP_T_BIN, SMP_T_BIN },
4348
Baptiste Assmanne279ca62020-10-27 18:10:06 +01004349 /* MQTT converters */
4350 { "mqtt_is_valid", sample_conv_mqtt_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4351 { "mqtt_field_value", sample_conv_mqtt_field_value, ARG2(2,STR,STR), sample_conv_mqtt_field_value_check, SMP_T_BIN, SMP_T_STR },
4352
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02004353 { "iif", sample_conv_iif, ARG2(2, STR, STR), NULL, SMP_T_BOOL, SMP_T_STR },
4354
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02004355 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4356 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4357 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4358 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
4359 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4360 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4361 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4362 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4363 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4364 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4365 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4366 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4367 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4368 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01004369
Christopher Fauletea159d62020-04-01 16:21:44 +02004370 { "htonl", sample_conv_htonl, 0, NULL, SMP_T_SINT, SMP_T_BIN },
4371 { "cut_crlf", sample_conv_cut_crlf, 0, NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet51fc9d12020-04-01 17:24:41 +02004372 { "ltrim", sample_conv_ltrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet568415a2020-04-01 17:24:47 +02004373 { "rtrim", sample_conv_rtrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Alex51c8ad42021-04-15 16:45:15 +02004374 { "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 +02004375
4376#ifdef USE_OPENSSL
4377 /* JSON Web Token converters */
4378 { "jwt_header_query", sample_conv_jwt_header_query, ARG2(0,STR,STR), sample_conv_jwt_query_check, SMP_T_BIN, SMP_T_ANY },
4379 { "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 +02004380 { "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 +02004381#endif
Willy Tarreau9fcb9842012-04-20 14:45:49 +02004382 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01004383}};
4384
Willy Tarreau0108d902018-11-25 19:14:37 +01004385INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);