blob: 2e04c74c9f436ec3badb7f79916b1f359dd174ae [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;
Willy Tarreau7ed05972022-11-14 06:59:59 +0100826 __fallthrough;
Christopher Fauletec100512017-07-24 15:38:41 +0200827
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 Tarreaub9f30f32022-04-25 10:46:16 +0200961static int c_bool2bin(struct sample *smp)
962{
963 struct buffer *chk = get_trash_chunk();
964
965 *(unsigned long long int *)chk->area = my_htonll(!!smp->data.u.sint);
966 chk->data = 8;
967 smp->data.u.str = *chk;
968 smp->data.type = SMP_T_BIN;
969 return 1;
970}
971
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200972
Emeric Brun107ca302010-01-04 16:16:05 +0100973/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200974/* Sample casts matrix: */
975/* sample_casts[from type][to type] */
976/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100977/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100978
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100979sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200980/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
981/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
Willy Tarreaub9f30f32022-04-25 10:46:16 +0200982/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, c_bool2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200983/* 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 +0200984/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
985/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200986/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200987/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
988/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
989/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200990};
Emeric Brun107ca302010-01-04 16:16:05 +0100991
Emeric Brun107ca302010-01-04 16:16:05 +0100992/*
Willy Tarreau12785782012-04-27 21:37:17 +0200993 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100994 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200995 * Returns a pointer on allocated sample expression structure.
Christopher Faulet35926a12021-09-30 08:48:56 +0200996 * <al> is an arg_list serving as a list head to report missing dependencies.
997 * It may be NULL if such dependencies are not allowed. Otherwise, the caller
998 * must have set al->ctx if al is set.
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100999 * If <endptr> is non-nul, it will be set to the first unparsed character
1000 * (which may be the final '\0') on success. If it is nul, the expression
1001 * must be properly terminated by a '\0' otherwise an error is reported.
Emeric Brun107ca302010-01-04 16:16:05 +01001002 */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001003struct 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 +01001004{
Willy Tarreau833cc792013-07-24 15:34:19 +02001005 const char *begw; /* beginning of word */
1006 const char *endw; /* end of word */
1007 const char *endt; /* end of term */
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +01001008 struct sample_expr *expr = NULL;
Willy Tarreau12785782012-04-27 21:37:17 +02001009 struct sample_fetch *fetch;
1010 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +01001011 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +02001012 char *fkw = NULL;
1013 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +01001014 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +01001015
Willy Tarreau833cc792013-07-24 15:34:19 +02001016 begw = str[*idx];
Willy Tarreaued2c6622020-02-14 18:27:10 +01001017 for (endw = begw; is_idchar(*endw); endw++)
1018 ;
Willy Tarreau833cc792013-07-24 15:34:19 +02001019
1020 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +01001021 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +01001022 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001023 }
Emeric Brun107ca302010-01-04 16:16:05 +01001024
Willy Tarreau833cc792013-07-24 15:34:19 +02001025 /* keep a copy of the current fetch keyword for error reporting */
1026 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +01001027
Willy Tarreau833cc792013-07-24 15:34:19 +02001028 fetch = find_sample_fetch(begw, endw - begw);
1029 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +01001030 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +01001031 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001032 }
Emeric Brun107ca302010-01-04 16:16:05 +01001033
Willy Tarreau833cc792013-07-24 15:34:19 +02001034 /* At this point, we have :
1035 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +01001036 * - endw : end of the keyword, first character not part of keyword
Willy Tarreau833cc792013-07-24 15:34:19 +02001037 */
1038
1039 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +01001040 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +01001041 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001042 }
Emeric Brun107ca302010-01-04 16:16:05 +01001043 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +02001044
Vincent Bernat02779b62016-04-03 13:48:43 +02001045 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +02001046 if (!expr)
1047 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001048
1049 LIST_INIT(&(expr->conv_exprs));
1050 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001051 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +01001052
Willy Tarreau689a1df2013-12-13 00:40:11 +01001053 /* Note that we call the argument parser even with an empty string,
1054 * this allows it to automatically create entries for mandatory
1055 * implicit arguments (eg: local proxy name).
1056 */
Christopher Faulet35926a12021-09-30 08:48:56 +02001057 if (al) {
1058 al->kw = expr->fetch->kw;
1059 al->conv = NULL;
1060 }
Willy Tarreau80b53ff2020-02-14 08:40:37 +01001061 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 +01001062 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
1063 goto out_error;
1064 }
Willy Tarreau2e845be2012-10-19 19:49:09 +02001065
Willy Tarreau80b53ff2020-02-14 08:40:37 +01001066 /* now endt is our first char not part of the arg list, typically the
1067 * comma after the sample fetch name or after the closing parenthesis,
1068 * or the NUL char.
1069 */
1070
Willy Tarreau689a1df2013-12-13 00:40:11 +01001071 if (!expr->arg_p) {
1072 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +02001073 }
Willy Tarreau689a1df2013-12-13 00:40:11 +01001074 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
1075 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001076 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001077 }
1078
Willy Tarreau833cc792013-07-24 15:34:19 +02001079 /* Now process the converters if any. We have two supported syntaxes
1080 * for the converters, which can be combined :
1081 * - comma-delimited list of converters just after the keyword and args ;
1082 * - one converter per keyword
1083 * The combination allows to have each keyword being a comma-delimited
1084 * series of converters.
1085 *
1086 * We want to process the former first, then the latter. For this we start
1087 * from the beginning of the supposed place in the exiting conv chain, which
1088 * starts at the last comma (endt).
1089 */
1090
1091 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +02001092 struct sample_conv_expr *conv_expr;
Willy Tarreau46dfd782019-12-17 10:25:29 +01001093 int err_arg;
1094 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +01001095
Willy Tarreau833cc792013-07-24 15:34:19 +02001096 if (*endt && *endt != ',') {
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001097 if (endptr) {
1098 /* end found, let's stop here */
1099 break;
1100 }
Willy Tarreau833cc792013-07-24 15:34:19 +02001101 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +01001102 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +02001103 else
Willy Tarreau975c1782013-12-12 23:16:54 +01001104 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +01001105 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001106 }
Emeric Brun107ca302010-01-04 16:16:05 +01001107
Willy Tarreau80b53ff2020-02-14 08:40:37 +01001108 /* FIXME: how long should we support such idiocies ? Maybe we
1109 * should already warn ?
1110 */
Willy Tarreau833cc792013-07-24 15:34:19 +02001111 while (*endt == ',') /* then trailing commas */
1112 endt++;
1113
Willy Tarreau97108e02016-11-25 07:33:24 +01001114 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +02001115
1116 if (!*begw) {
1117 /* none ? skip to next string */
1118 (*idx)++;
1119 begw = str[*idx];
1120 if (!begw || !*begw)
1121 break;
1122 }
1123
Willy Tarreaued2c6622020-02-14 18:27:10 +01001124 for (endw = begw; is_idchar(*endw); endw++)
1125 ;
Willy Tarreau833cc792013-07-24 15:34:19 +02001126
1127 free(ckw);
1128 ckw = my_strndup(begw, endw - begw);
1129
1130 conv = find_sample_conv(begw, endw - begw);
1131 if (!conv) {
1132 /* we found an isolated keyword that we don't know, it's not ours */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001133 if (begw == str[*idx]) {
1134 endt = begw;
Willy Tarreau833cc792013-07-24 15:34:19 +02001135 break;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001136 }
Willy Tarreau97108e02016-11-25 07:33:24 +01001137 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +02001138 goto out_error;
1139 }
Emeric Brun107ca302010-01-04 16:16:05 +01001140
Willy Tarreau833cc792013-07-24 15:34:19 +02001141 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001142 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001143 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001144 }
Emeric Brun107ca302010-01-04 16:16:05 +01001145
1146 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +02001147 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001148 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001149 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001150 }
Emeric Brun107ca302010-01-04 16:16:05 +01001151
1152 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +02001153 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +02001154 if (!conv_expr)
1155 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001156
Willy Tarreau2b718102021-04-21 07:32:39 +02001157 LIST_APPEND(&(expr->conv_exprs), &(conv_expr->list));
Emeric Brun107ca302010-01-04 16:16:05 +01001158 conv_expr->conv = conv;
1159
Christopher Faulet35926a12021-09-30 08:48:56 +02001160 if (al) {
1161 al->kw = expr->fetch->kw;
1162 al->conv = conv_expr->conv->kw;
1163 }
Willy Tarreau80b53ff2020-02-14 08:40:37 +01001164 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 +01001165 if (argcnt < 0) {
1166 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
1167 goto out_error;
1168 }
Willy Tarreau9e92d322010-01-26 17:58:06 +01001169
Willy Tarreau46dfd782019-12-17 10:25:29 +01001170 if (argcnt && !conv->arg_mask) {
1171 memprintf(err_msg, "converter '%s' does not support any args", ckw);
1172 goto out_error;
1173 }
Willy Tarreau21d68a62012-04-20 15:52:36 +02001174
Willy Tarreau46dfd782019-12-17 10:25:29 +01001175 if (!conv_expr->arg_p)
1176 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001177
Willy Tarreau46dfd782019-12-17 10:25:29 +01001178 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1179 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001180 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001181 }
1182 }
Emeric Brun485479d2010-09-23 18:02:19 +02001183
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001184 if (endptr) {
1185 /* end found, let's stop here */
1186 *endptr = (char *)endt;
1187 }
1188
Willy Tarreau833cc792013-07-24 15:34:19 +02001189 out:
1190 free(fkw);
1191 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001192 return expr;
1193
1194out_error:
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +01001195 release_sample_expr(expr);
Willy Tarreau833cc792013-07-24 15:34:19 +02001196 expr = NULL;
1197 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001198}
1199
1200/*
Willy Tarreau12785782012-04-27 21:37:17 +02001201 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001202 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001203 * Returns a pointer on a typed sample structure containing the result or NULL if
1204 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001205 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001206 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001207 *
1208 * Note: the fetch functions are required to properly set the return type. The
1209 * conversion functions must do so too. However the cast functions do not need
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001210 * to since they're made to cast multiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001211 *
1212 * The caller may indicate in <opt> if it considers the result final or not.
1213 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1214 * if the result is stable or not, according to the following table :
1215 *
1216 * return MAY_CHANGE FINAL Meaning for the sample
1217 * NULL 0 * Not present and will never be (eg: header)
1218 * NULL 1 0 Not present yet, could change (eg: POST param)
1219 * NULL 1 1 Not present yet, will not change anymore
1220 * smp 0 * Present and will not change (eg: header)
1221 * smp 1 0 Present, may change (eg: request length)
1222 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001223 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001224struct sample *sample_process(struct proxy *px, struct session *sess,
1225 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001226 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001227{
Willy Tarreau12785782012-04-27 21:37:17 +02001228 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001229
Willy Tarreau18387e22013-07-25 12:02:38 +02001230 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001231 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001232 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001233 }
Emeric Brun107ca302010-01-04 16:16:05 +01001234
Willy Tarreau1777ea62016-03-10 16:15:46 +01001235 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001236 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001237 return NULL;
1238
Emeric Brun107ca302010-01-04 16:16:05 +01001239 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001240 /* we want to ensure that p->type can be casted into
1241 * conv_expr->conv->in_type. We have 3 possibilities :
1242 * - NULL => not castable.
1243 * - c_none => nothing to do (let's optimize it)
1244 * - other => apply cast and prepare to fail
1245 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001246 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001247 return NULL;
1248
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001249 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1250 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001251 return NULL;
1252
Willy Tarreau12e50112012-04-25 17:21:49 +02001253 /* OK cast succeeded */
1254
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001255 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001256 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001257 }
1258 return p;
1259}
1260
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001261/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001262 * Resolve all remaining arguments in proxy <p>. Returns the number of
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001263 * errors or 0 if everything is fine. If at least one error is met, it will
1264 * be appended to *err. If *err==NULL it will be allocated first.
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001265 */
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001266int smp_resolve_args(struct proxy *p, char **err)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001267{
1268 struct arg_list *cur, *bak;
1269 const char *ctx, *where;
1270 const char *conv_ctx, *conv_pre, *conv_pos;
1271 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001272 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001273 struct arg *arg;
1274 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001275 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001276
1277 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1278 struct proxy *px;
1279 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001280 struct stktable *t;
1281 char *pname, *sname, *stktname;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001282 char *err2;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001283
1284 arg = cur->arg;
1285
1286 /* prepare output messages */
1287 conv_pre = conv_pos = conv_ctx = "";
1288 if (cur->conv) {
1289 conv_ctx = cur->conv;
1290 conv_pre = "conversion keyword '";
1291 conv_pos = "' for ";
1292 }
1293
1294 where = "in";
1295 ctx = "sample fetch keyword";
1296 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001297 case ARGC_STK: where = "in stick rule in"; break;
1298 case ARGC_TRK: where = "in tracking rule in"; break;
1299 case ARGC_LOG: where = "in log-format string in"; break;
1300 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
Willy Tarreau57467b82021-09-02 19:43:20 +02001301 case ARGC_HRQ: where = "in http-request expression in"; break;
1302 case ARGC_HRS: where = "in http-response response in"; break;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001303 case ARGC_UIF: where = "in unique-id-format string in"; break;
1304 case ARGC_RDR: where = "in redirect format string in"; break;
1305 case ARGC_CAP: where = "in capture rule in"; break;
1306 case ARGC_ACL: ctx = "ACL keyword"; break;
1307 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001308 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Willy Tarreau1a8636d2022-04-25 08:15:46 +02001309 case ARGC_UBK: where = "in use_backend expression in"; break;
Willy Tarreau7c9a0fe2022-04-25 10:25:34 +02001310 case ARGC_USRV: where = "in use-server or balance expression in"; break;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001311 case ARGC_HERR: where = "in http-error directive in"; break;
Miroslav Zagorac7f8314c2020-12-09 16:31:48 +01001312 case ARGC_OT: where = "in ot-scope directive in"; break;
Aurelien DARRAGON5f7f5fe2022-12-28 11:30:51 +01001313 case ARGC_OPT: where = "in option directive in"; break;
Willy Tarreau57467b82021-09-02 19:43:20 +02001314 case ARGC_TCO: where = "in tcp-request connection expression in"; break;
1315 case ARGC_TSE: where = "in tcp-request session expression in"; break;
1316 case ARGC_TRQ: where = "in tcp-request content expression in"; break;
1317 case ARGC_TRS: where = "in tcp-response content expression in"; break;
1318 case ARGC_TCK: where = "in tcp-check expression in"; break;
1319 case ARGC_CFG: where = "in configuration expression in"; break;
1320 case ARGC_CLI: where = "in CLI expression in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001321 }
1322
1323 /* set a few default settings */
1324 px = p;
1325 pname = p->id;
1326
1327 switch (arg->type) {
1328 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001329 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001330 memprintf(err, "%sparsing [%s:%d]: missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1331 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001332 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001333 cfgerr++;
1334 continue;
1335 }
1336
1337 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001338 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001339
1340 if (sname) {
1341 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001342 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001343
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001344 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001345 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001346 memprintf(err, "%sparsing [%s:%d]: unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1347 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001348 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001349 cfgerr++;
1350 break;
1351 }
1352 }
Christopher Faulet37a9e212021-10-12 18:48:05 +02001353 else {
1354 if (px->cap & PR_CAP_DEF) {
1355 memprintf(err, "%sparsing [%s:%d]: backend name must be set in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1356 *err ? *err : "", cur->file, cur->line,
1357 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1358 cfgerr++;
1359 break;
1360 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001361 sname = arg->data.str.area;
Christopher Faulet37a9e212021-10-12 18:48:05 +02001362 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001363
1364 srv = findserver(px, sname);
1365 if (!srv) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001366 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",
1367 *err ? *err : "", cur->file, cur->line, sname, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001368 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001369 cfgerr++;
1370 break;
1371 }
1372
Amaury Denoyelle06269612021-08-23 14:05:07 +02001373 srv->flags |= SRV_F_NON_PURGEABLE;
1374
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001375 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001376 arg->unresolved = 0;
1377 arg->data.srv = srv;
1378 break;
1379
1380 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001381 if (arg->data.str.data) {
1382 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001383 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001384 }
1385
1386 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001387 memprintf(err, "%sparsing [%s:%d]: unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1388 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001389 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001390 cfgerr++;
1391 break;
1392 }
1393
1394 if (!(px->cap & PR_CAP_FE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001395 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",
1396 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001397 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001398 cfgerr++;
1399 break;
1400 }
1401
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001402 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001403 arg->unresolved = 0;
1404 arg->data.prx = px;
1405 break;
1406
1407 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001408 if (arg->data.str.data) {
1409 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001410 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001411 }
1412
1413 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001414 memprintf(err, "%sparsing [%s:%d]: unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1415 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001416 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001417 cfgerr++;
1418 break;
1419 }
1420
1421 if (!(px->cap & PR_CAP_BE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001422 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",
1423 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001424 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001425 cfgerr++;
1426 break;
1427 }
1428
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001429 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001430 arg->unresolved = 0;
1431 arg->data.prx = px;
1432 break;
1433
1434 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001435 if (arg->data.str.data)
1436 stktname = arg->data.str.area;
Christopher Faulet37a9e212021-10-12 18:48:05 +02001437 else {
1438 if (px->cap & PR_CAP_DEF) {
1439 memprintf(err, "%sparsing [%s:%d]: table name must be set in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1440 *err ? *err : "", cur->file, cur->line,
1441 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1442 cfgerr++;
1443 break;
1444 }
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001445 stktname = px->id;
Christopher Faulet37a9e212021-10-12 18:48:05 +02001446 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001447
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001448 t = stktable_find_by_name(stktname);
1449 if (!t) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001450 memprintf(err, "%sparsing [%s:%d]: unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1451 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001452 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001453 cfgerr++;
1454 break;
1455 }
1456
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001457 if (!t->size) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001458 memprintf(err, "%sparsing [%s:%d]: no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1459 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001460 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001461 cfgerr++;
1462 break;
1463 }
1464
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001465 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001466 p->next_stkt_ref = t->proxies_list;
1467 t->proxies_list = p;
1468 }
1469
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001470 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001471 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001472 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001473 break;
1474
1475 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001476 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001477 memprintf(err, "%sparsing [%s:%d]: missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1478 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001479 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001480 cfgerr++;
1481 break;
1482 }
1483
1484 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001485 strcmp(p->uri_auth->userlist->name, arg->data.str.area) == 0)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001486 ul = p->uri_auth->userlist;
1487 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001488 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001489
1490 if (!ul) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001491 memprintf(err, "%sparsing [%s:%d]: unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1492 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001493 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001494 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001495 cfgerr++;
1496 break;
1497 }
1498
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001499 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001500 arg->unresolved = 0;
1501 arg->data.usr = ul;
1502 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001503
1504 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001505 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001506 memprintf(err, "%sparsing [%s:%d]: missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1507 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001508 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001509 cfgerr++;
1510 continue;
1511 }
1512
Willy Tarreau46947782015-01-19 19:00:58 +01001513 rflags = 0;
1514 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001515 err2 = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001516
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001517 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err2))) {
1518 memprintf(err, "%sparsing [%s:%d]: error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1519 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001520 arg->data.str.area,
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001521 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err2);
Willy Tarreau46947782015-01-19 19:00:58 +01001522 cfgerr++;
1523 continue;
1524 }
1525
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001526 chunk_destroy(&arg->data.str);
Willy Tarreau46947782015-01-19 19:00:58 +01001527 arg->unresolved = 0;
1528 arg->data.reg = reg;
1529 break;
1530
1531
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001532 }
1533
Willy Tarreau2b718102021-04-21 07:32:39 +02001534 LIST_DELETE(&cur->list);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001535 free(cur);
1536 } /* end of args processing */
1537
1538 return cfgerr;
1539}
1540
1541/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001542 * Process a fetch + format conversion as defined by the sample expression
1543 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001544 * not explicitly set to <smp_type>, but shall be compatible with it as
1545 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1546 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001547 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1548 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1549 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1550 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001551 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1552 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1553 * still hope to get it after waiting longer, and is not converted to string.
1554 * The possible output combinations are the following :
1555 *
1556 * return MAY_CHANGE FINAL Meaning for the sample
1557 * NULL * * Not present and will never be (eg: header)
1558 * smp 0 * Final value converted (eg: header)
1559 * smp 1 0 Not present yet, may appear later (eg: header)
1560 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001561 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001562struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001563 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001564 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001565{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001566 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001567
Willy Tarreau6c616e02014-06-25 16:56:41 +02001568 memset(smp, 0, sizeof(*smp));
1569
Willy Tarreau192252e2015-04-04 01:47:55 +02001570 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001571 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1572 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001573 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001574 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001575
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001576 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001577 return NULL;
1578
Willy Tarreau3705dee2022-04-25 10:02:11 +02001579 if (sample_casts[smp->data.type][smp_type] != c_none &&
1580 !sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001581 return NULL;
1582
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001583 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001584 return smp;
1585}
1586
Christopher Faulet476e5d02016-10-26 11:34:47 +02001587static void release_sample_arg(struct arg *p)
1588{
1589 struct arg *p_back = p;
1590
1591 if (!p)
1592 return;
1593
1594 while (p->type != ARGT_STOP) {
1595 if (p->type == ARGT_STR || p->unresolved) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001596 chunk_destroy(&p->data.str);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001597 p->unresolved = 0;
1598 }
1599 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001600 regex_free(p->data.reg);
1601 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001602 }
1603 p++;
1604 }
1605
1606 if (p_back != empty_arg_list)
1607 free(p_back);
1608}
1609
1610void release_sample_expr(struct sample_expr *expr)
1611{
1612 struct sample_conv_expr *conv_expr, *conv_exprb;
1613
1614 if (!expr)
1615 return;
1616
Tim Duesterhus867cd982020-07-04 11:49:39 +02001617 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001618 LIST_DELETE(&conv_expr->list);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001619 release_sample_arg(conv_expr->arg_p);
Tim Duesterhus867cd982020-07-04 11:49:39 +02001620 free(conv_expr);
1621 }
1622
Christopher Faulet476e5d02016-10-26 11:34:47 +02001623 release_sample_arg(expr->arg_p);
1624 free(expr);
1625}
1626
Emeric Brun107ca302010-01-04 16:16:05 +01001627/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001628/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001629/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001630/*****************************************************************/
1631
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001632static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1633{
1634 int i;
1635 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001636 struct buffer *buf;
1637 struct sink *sink;
1638 struct ist line;
1639 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001640
Willy Tarreau0851fd52019-12-17 10:07:25 +01001641 buf = alloc_trash_chunk();
1642 if (!buf)
1643 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001644
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001645 sink = (struct sink *)arg_p[1].data.ptr;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001646 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001647
Willy Tarreau0851fd52019-12-17 10:07:25 +01001648 pfx = arg_p[0].data.str.area;
1649 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001650
Willy Tarreau0851fd52019-12-17 10:07:25 +01001651 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1652 if (!sample_casts[smp->data.type][SMP_T_STR])
1653 goto nocast;
1654
1655 /* Copy sample fetch. This puts the sample as const, the
1656 * cast will copy data if a transformation is required.
1657 */
1658 memcpy(&tmp, smp, sizeof(struct sample));
1659 tmp.flags = SMP_F_CONST;
1660
1661 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1662 goto nocast;
1663
1664 /* Display the displayable chars*. */
1665 b_putchr(buf, '<');
1666 for (i = 0; i < tmp.data.u.str.data; i++) {
Willy Tarreau90807112020-02-25 08:16:33 +01001667 if (isprint((unsigned char)tmp.data.u.str.area[i]))
Willy Tarreau0851fd52019-12-17 10:07:25 +01001668 b_putchr(buf, tmp.data.u.str.area[i]);
1669 else
1670 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001671 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001672 b_putchr(buf, '>');
1673
1674 done:
1675 line = ist2(buf->area, buf->data);
Emeric Brun54648852020-07-06 15:54:06 +02001676 sink_write(sink, &line, 1, 0, 0, NULL);
Willy Tarreau0851fd52019-12-17 10:07:25 +01001677 end:
1678 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001679 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001680 nocast:
1681 chunk_appendf(buf, "(undisplayable)");
1682 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001683}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001684
1685// This function checks the "debug" converter's arguments.
1686static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1687 const char *file, int line, char **err)
1688{
1689 const char *name = "buf0";
1690 struct sink *sink = NULL;
1691
1692 if (args[0].type != ARGT_STR) {
1693 /* optional prefix */
1694 args[0].data.str.area = "";
1695 args[0].data.str.data = 0;
1696 }
1697
1698 if (args[1].type == ARGT_STR)
1699 name = args[1].data.str.area;
1700
1701 sink = sink_find(name);
1702 if (!sink) {
1703 memprintf(err, "No such sink '%s'", name);
1704 return 0;
1705 }
1706
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001707 chunk_destroy(&args[1].data.str);
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001708 args[1].type = ARGT_PTR;
1709 args[1].data.ptr = sink;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001710 return 1;
1711}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001712
Holger Just1bfc24b2017-05-06 00:56:53 +02001713static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1714{
Willy Tarreau83061a82018-07-13 11:56:34 +02001715 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001716 int bin_len;
1717
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001718 trash->data = 0;
1719 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1720 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001721 if (bin_len < 0)
1722 return 0;
1723
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001724 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001725 smp->data.u.str = *trash;
1726 smp->data.type = SMP_T_BIN;
1727 smp->flags &= ~SMP_F_CONST;
1728 return 1;
1729}
1730
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +02001731static int sample_conv_base64url2bin(const struct arg *arg_p, struct sample *smp, void *private)
1732{
1733 struct buffer *trash = get_trash_chunk();
1734 int bin_len;
1735
1736 trash->data = 0;
1737 bin_len = base64urldec(smp->data.u.str.area, smp->data.u.str.data,
1738 trash->area, trash->size);
1739 if (bin_len < 0)
1740 return 0;
1741
1742 trash->data = bin_len;
1743 smp->data.u.str = *trash;
1744 smp->data.type = SMP_T_BIN;
1745 smp->flags &= ~SMP_F_CONST;
1746 return 1;
1747}
1748
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001749static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001750{
Willy Tarreau83061a82018-07-13 11:56:34 +02001751 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001752 int b64_len;
1753
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001754 trash->data = 0;
1755 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1756 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001757 if (b64_len < 0)
1758 return 0;
1759
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001760 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001761 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001762 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001763 smp->flags &= ~SMP_F_CONST;
1764 return 1;
1765}
1766
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +02001767static int sample_conv_bin2base64url(const struct arg *arg_p, struct sample *smp, void *private)
1768{
1769 struct buffer *trash = get_trash_chunk();
1770 int b64_len;
1771
1772 trash->data = 0;
1773 b64_len = a2base64url(smp->data.u.str.area, smp->data.u.str.data,
1774 trash->area, trash->size);
1775 if (b64_len < 0)
1776 return 0;
1777
1778 trash->data = b64_len;
1779 smp->data.u.str = *trash;
1780 smp->data.type = SMP_T_STR;
1781 smp->flags &= ~SMP_F_CONST;
1782 return 1;
1783}
1784
Willy Tarreau168e8de2021-10-06 15:29:00 +02001785/* This function returns a sample struct filled with the conversion of variable
1786 * <var> to sample type <type> (SMP_T_*), via a cast to the target type. If the
1787 * variable cannot be retrieved or casted, 0 is returned, otherwise 1.
1788 *
1789 * Keep in mind that the sample content may be written to a pre-allocated
1790 * trash chunk as returned by get_trash_chunk().
1791 */
1792int sample_conv_var2smp(const struct var_desc *var, struct sample *smp, int type)
1793{
1794 if (!vars_get_by_desc(var, smp, NULL))
1795 return 0;
1796 if (!sample_casts[smp->data.type][type])
1797 return 0;
1798 if (!sample_casts[smp->data.type][type](smp))
1799 return 0;
1800 return 1;
1801}
1802
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001803static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1804{
1805 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001806 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001807
1808 memset(&ctx, 0, sizeof(ctx));
1809
1810 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001811 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1812 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001813
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001814 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001815 smp->data.u.str = *trash;
1816 smp->data.type = SMP_T_BIN;
1817 smp->flags &= ~SMP_F_CONST;
1818 return 1;
1819}
1820
Dragan Dosen9e8db132021-02-22 10:03:53 +01001821/* This function returns a sample struct filled with an <arg> content.
1822 * If the <arg> contains a string, it is returned in the sample flagged as
1823 * SMP_F_CONST. If the <arg> contains a variable descriptor, the sample is
1824 * filled with the content of the variable by using vars_get_by_desc().
1825 *
1826 * Keep in mind that the sample content may be written to a pre-allocated
1827 * trash chunk as returned by get_trash_chunk().
1828 *
1829 * This function returns 0 if an error occurs, otherwise it returns 1.
1830 */
Willy Tarreau4034e2c2021-10-06 15:20:18 +02001831int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001832{
1833 switch (arg->type) {
1834 case ARGT_STR:
1835 smp->data.type = SMP_T_STR;
1836 smp->data.u.str = arg->data.str;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001837 smp->flags = SMP_F_CONST;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001838 return 1;
1839 case ARGT_VAR:
Willy Tarreau2476ff12021-10-06 15:30:52 +02001840 return sample_conv_var2smp(&arg->data.var, smp, SMP_T_STR);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001841 default:
1842 return 0;
1843 }
1844}
1845
Marcin Deranek40ca09c2021-07-13 14:05:24 +02001846static int sample_conv_be2dec_check(struct arg *args, struct sample_conv *conv,
1847 const char *file, int line, char **err)
1848{
1849 if (args[1].data.sint <= 0 || args[1].data.sint > sizeof(unsigned long long)) {
Willy Tarreauc5d0fc92021-09-15 10:30:40 +02001850 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 +02001851 return 0;
1852 }
1853
1854 if (args[2].data.sint != 0 && args[2].data.sint != 1) {
1855 memprintf(err, "Unsupported truncate value (%lld)", args[2].data.sint);
1856 return 0;
1857 }
1858
1859 return 1;
1860}
1861
1862/* Converts big-endian binary input sample to a string containing an unsigned
1863 * integer number per <chunk_size> input bytes separated with <separator>.
1864 * Optional <truncate> flag indicates if input is truncated at <chunk_size>
1865 * boundaries.
1866 * Arguments: separator (string), chunk_size (integer), truncate (0,1)
1867 */
1868static int sample_conv_be2dec(const struct arg *args, struct sample *smp, void *private)
1869{
1870 struct buffer *trash = get_trash_chunk();
1871 const int last = args[2].data.sint ? smp->data.u.str.data - args[1].data.sint + 1 : smp->data.u.str.data;
1872 int max_size = trash->size - 2;
1873 int i;
1874 int start;
1875 int ptr = 0;
1876 unsigned long long number;
1877 char *pos;
1878
1879 trash->data = 0;
1880
1881 while (ptr < last && trash->data <= max_size) {
1882 start = trash->data;
1883 if (ptr) {
1884 /* Add separator */
1885 memcpy(trash->area + trash->data, args[0].data.str.area, args[0].data.str.data);
1886 trash->data += args[0].data.str.data;
1887 }
1888 else
1889 max_size -= args[0].data.str.data;
1890
1891 /* Add integer */
1892 for (number = 0, i = 0; i < args[1].data.sint && ptr < smp->data.u.str.data; i++)
1893 number = (number << 8) + (unsigned char)smp->data.u.str.area[ptr++];
1894
1895 pos = ulltoa(number, trash->area + trash->data, trash->size - trash->data);
1896 if (pos)
1897 trash->data = pos - trash->area;
1898 else {
1899 trash->data = start;
1900 break;
1901 }
1902 }
1903
1904 smp->data.u.str = *trash;
1905 smp->data.type = SMP_T_STR;
1906 smp->flags &= ~SMP_F_CONST;
1907 return 1;
1908}
1909
Marcin Deranekda0264a2021-07-13 14:08:56 +02001910static int sample_conv_be2hex_check(struct arg *args, struct sample_conv *conv,
1911 const char *file, int line, char **err)
1912{
1913 if (args[1].data.sint <= 0 && (args[0].data.str.data > 0 || args[2].data.sint != 0)) {
1914 memprintf(err, "chunk_size needs to be positive (%lld)", args[1].data.sint);
1915 return 0;
1916 }
1917
1918 if (args[2].data.sint != 0 && args[2].data.sint != 1) {
1919 memprintf(err, "Unsupported truncate value (%lld)", args[2].data.sint);
1920 return 0;
1921 }
1922
1923 return 1;
1924}
1925
1926/* Converts big-endian binary input sample to a hex string containing two hex
1927 * digits per input byte. <separator> is put every <chunk_size> binary input
1928 * bytes if specified. Optional <truncate> flag indicates if input is truncated
1929 * at <chunk_size> boundaries.
1930 * Arguments: separator (string), chunk_size (integer), truncate (0,1)
1931 */
1932static int sample_conv_be2hex(const struct arg *args, struct sample *smp, void *private)
1933{
1934 struct buffer *trash = get_trash_chunk();
1935 int chunk_size = args[1].data.sint;
1936 const int last = args[2].data.sint ? smp->data.u.str.data - chunk_size + 1 : smp->data.u.str.data;
1937 int i;
1938 int max_size;
1939 int ptr = 0;
1940 unsigned char c;
1941
1942 trash->data = 0;
1943 if (args[0].data.str.data == 0 && args[2].data.sint == 0)
1944 chunk_size = smp->data.u.str.data;
1945 max_size = trash->size - 2 * chunk_size;
1946
1947 while (ptr < last && trash->data <= max_size) {
1948 if (ptr) {
1949 /* Add separator */
1950 memcpy(trash->area + trash->data, args[0].data.str.area, args[0].data.str.data);
1951 trash->data += args[0].data.str.data;
1952 }
1953 else
1954 max_size -= args[0].data.str.data;
1955
1956 /* Add hex */
1957 for (i = 0; i < chunk_size && ptr < smp->data.u.str.data; i++) {
1958 c = smp->data.u.str.area[ptr++];
1959 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1960 trash->area[trash->data++] = hextab[c & 0xF];
1961 }
1962 }
1963
1964 smp->data.u.str = *trash;
1965 smp->data.type = SMP_T_STR;
1966 smp->flags &= ~SMP_F_CONST;
1967 return 1;
1968}
1969
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001970static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001971{
Willy Tarreau83061a82018-07-13 11:56:34 +02001972 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001973 unsigned char c;
1974 int ptr = 0;
1975
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001976 trash->data = 0;
1977 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
1978 c = smp->data.u.str.area[ptr++];
1979 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1980 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001981 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001982 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001983 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001984 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001985 return 1;
1986}
1987
Dragan Dosen3f957b22017-10-24 09:27:34 +02001988static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1989{
1990 long long int n = 0;
1991 int i, c;
1992
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001993 for (i = 0; i < smp->data.u.str.data; i++) {
1994 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02001995 return 0;
1996 n = (n << 4) + c;
1997 }
1998
1999 smp->data.u.sint = n;
2000 smp->data.type = SMP_T_SINT;
2001 smp->flags &= ~SMP_F_CONST;
2002 return 1;
2003}
2004
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002005/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002006static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002007{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002008 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
2009 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002010 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002011 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002012 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002013 return 1;
2014}
2015
Willy Tarreau60a2ee72017-12-15 07:13:48 +01002016static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002017{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002018 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002019 smp->data.u.sint = i;
2020 smp->data.type = SMP_T_SINT;
2021 return 1;
2022}
2023
2024
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002025static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002026{
2027 int i;
2028
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002029 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002030 return 0;
2031
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002032 for (i = 0; i < smp->data.u.str.data; i++) {
2033 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
2034 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01002035 }
2036 return 1;
2037}
2038
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002039static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002040{
2041 int i;
2042
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002043 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02002044 return 0;
2045
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002046 for (i = 0; i < smp->data.u.str.data; i++) {
2047 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
2048 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01002049 }
2050 return 1;
2051}
2052
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002053/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
2054static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002055{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002056 /* Attempt to convert to IPv4 to apply the correct mask. */
2057 c_ipv62ip(smp);
2058
2059 if (smp->data.type == SMP_T_IPV4) {
2060 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
2061 smp->data.type = SMP_T_IPV4;
2062 }
2063 else if (smp->data.type == SMP_T_IPV6) {
2064 /* IPv6 cannot be converted without an IPv6 mask. */
2065 if (args[1].type != ARGT_IPV6)
2066 return 0;
2067
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +01002068 write_u64(&smp->data.u.ipv6.s6_addr[0],
2069 read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0]));
2070 write_u64(&smp->data.u.ipv6.s6_addr[8],
2071 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 +01002072 smp->data.type = SMP_T_IPV6;
2073 }
2074
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002075 return 1;
2076}
2077
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002078/* takes an UINT value on input supposed to represent the time since EPOCH,
2079 * adds an optional offset found in args[1] and emits a string representing
2080 * the local time in the format specified in args[1] using strftime().
2081 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002082static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002083{
Willy Tarreau83061a82018-07-13 11:56:34 +02002084 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002085 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002086 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002087 struct tm tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002088
2089 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002090 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002091 curr_date += args[1].data.sint;
2092
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002093 get_localtime(curr_date, &tm);
2094
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002095 temp = get_trash_chunk();
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002096 temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002097 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002098 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002099 return 1;
2100}
2101
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002102/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002103static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002104{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002105 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
2106 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002107 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002108 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002109 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002110 return 1;
2111}
2112
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002113/* takes an UINT value on input supposed to represent the time since EPOCH,
2114 * adds an optional offset found in args[1] and emits a string representing
2115 * the UTC date in the format specified in args[1] using strftime().
2116 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002117static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002118{
Willy Tarreau83061a82018-07-13 11:56:34 +02002119 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002120 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002121 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002122 struct tm tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002123
2124 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002125 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002126 curr_date += args[1].data.sint;
2127
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002128 get_gmtime(curr_date, &tm);
2129
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002130 temp = get_trash_chunk();
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002131 temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002132 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002133 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002134 return 1;
2135}
2136
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002137/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002138static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002139{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002140 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
2141 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002142 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002143 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002144 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002145 return 1;
2146}
2147
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002148/* hashes the binary input into a 32-bit unsigned int using xxh.
2149 * The seed of the hash defaults to 0 but can be changd in argument 1.
2150 */
2151static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
2152{
2153 unsigned int seed;
2154
Christopher Faulete6e7a582021-01-29 11:29:28 +01002155 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002156 seed = arg_p->data.sint;
2157 else
2158 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002159 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
2160 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002161 smp->data.type = SMP_T_SINT;
2162 return 1;
2163}
2164
2165/* hashes the binary input into a 64-bit unsigned int using xxh.
2166 * In fact, the function returns a 64 bit unsigned, but the sample
2167 * storage of haproxy only proposes 64-bits signed, so the value is
2168 * cast as signed. This cast doesn't impact the hash repartition.
2169 * The seed of the hash defaults to 0 but can be changd in argument 1.
2170 */
2171static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
2172{
2173 unsigned long long int seed;
2174
Christopher Faulete6e7a582021-01-29 11:29:28 +01002175 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002176 seed = (unsigned long long int)arg_p->data.sint;
2177 else
2178 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002179 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
2180 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002181 smp->data.type = SMP_T_SINT;
2182 return 1;
2183}
2184
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002185static int sample_conv_xxh3(const struct arg *arg_p, struct sample *smp, void *private)
2186{
2187 unsigned long long int seed;
2188
Christopher Faulete6e7a582021-01-29 11:29:28 +01002189 if (arg_p->data.sint)
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002190 seed = (unsigned long long int)arg_p->data.sint;
2191 else
2192 seed = 0;
2193 smp->data.u.sint = (long long int)XXH3(smp->data.u.str.area,
2194 smp->data.u.str.data, seed);
2195 smp->data.type = SMP_T_SINT;
2196 return 1;
2197}
2198
Willy Tarreau80599772015-01-20 19:35:24 +01002199/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002200static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01002201{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002202 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
2203 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002204 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002205 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002206 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01002207 return 1;
2208}
2209
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002210/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
2211static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
2212{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002213 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
2214 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002215 if (arg_p->data.sint)
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002216 smp->data.u.sint = full_hash(smp->data.u.sint);
2217 smp->data.type = SMP_T_SINT;
2218 return 1;
2219}
2220
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002221/* This function escape special json characters. The returned string can be
2222 * safely set between two '"' and used as json string. The json string is
2223 * defined like this:
2224 *
2225 * any Unicode character except '"' or '\' or control character
2226 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
2227 *
2228 * The enum input_type contain all the allowed mode for decoding the input
2229 * string.
2230 */
2231enum input_type {
2232 IT_ASCII = 0,
2233 IT_UTF8,
2234 IT_UTF8S,
2235 IT_UTF8P,
2236 IT_UTF8PS,
2237};
William Dauchy5417e892021-01-08 21:57:41 +01002238
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002239static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
2240 const char *file, int line, char **err)
2241{
Christopher Faulet95917132020-08-05 23:07:07 +02002242 enum input_type type;
2243
Christopher Faulet95917132020-08-05 23:07:07 +02002244 if (strcmp(arg->data.str.area, "") == 0)
2245 type = IT_ASCII;
2246 else if (strcmp(arg->data.str.area, "ascii") == 0)
2247 type = IT_ASCII;
2248 else if (strcmp(arg->data.str.area, "utf8") == 0)
2249 type = IT_UTF8;
2250 else if (strcmp(arg->data.str.area, "utf8s") == 0)
2251 type = IT_UTF8S;
2252 else if (strcmp(arg->data.str.area, "utf8p") == 0)
2253 type = IT_UTF8P;
2254 else if (strcmp(arg->data.str.area, "utf8ps") == 0)
2255 type = IT_UTF8PS;
2256 else {
2257 memprintf(err, "Unexpected input code type. "
2258 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
2259 return 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002260 }
2261
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002262 chunk_destroy(&arg->data.str);
Christopher Faulet95917132020-08-05 23:07:07 +02002263 arg->type = ARGT_SINT;
2264 arg->data.sint = type;
2265 return 1;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002266}
2267
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002268static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002269{
Willy Tarreau83061a82018-07-13 11:56:34 +02002270 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002271 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
2272 const char *str;
2273 int len;
2274 enum input_type input_type = IT_ASCII;
2275 unsigned int c;
2276 unsigned int ret;
2277 char *p;
2278
Christopher Faulete6e7a582021-01-29 11:29:28 +01002279 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002280
2281 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002282 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002283
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002284 p = smp->data.u.str.area;
2285 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002286
2287 if (input_type == IT_ASCII) {
2288 /* Read input as ASCII. */
2289 c = *(unsigned char *)p;
2290 p++;
2291 }
2292 else {
2293 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002294 ret = utf8_next(p,
2295 smp->data.u.str.data - ( p - smp->data.u.str.area),
2296 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002297 p += utf8_return_length(ret);
2298
2299 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2300 return 0;
2301 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2302 continue;
2303 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2304 return 0;
2305 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2306 continue;
2307
2308 /* Check too big values. */
2309 if ((unsigned int)c > 0xffff) {
2310 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2311 return 0;
2312 continue;
2313 }
2314 }
2315
2316 /* Convert character. */
2317 if (c == '"') {
2318 len = 2;
2319 str = "\\\"";
2320 }
2321 else if (c == '\\') {
2322 len = 2;
2323 str = "\\\\";
2324 }
2325 else if (c == '/') {
2326 len = 2;
2327 str = "\\/";
2328 }
2329 else if (c == '\b') {
2330 len = 2;
2331 str = "\\b";
2332 }
2333 else if (c == '\f') {
2334 len = 2;
2335 str = "\\f";
2336 }
2337 else if (c == '\r') {
2338 len = 2;
2339 str = "\\r";
2340 }
2341 else if (c == '\n') {
2342 len = 2;
2343 str = "\\n";
2344 }
2345 else if (c == '\t') {
2346 len = 2;
2347 str = "\\t";
2348 }
Willy Tarreau90807112020-02-25 08:16:33 +01002349 else if (c > 0xff || !isprint((unsigned char)c)) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002350 /* isprint generate a segfault if c is too big. The man says that
2351 * c must have the value of an unsigned char or EOF.
2352 */
2353 len = 6;
2354 _str[0] = '\\';
2355 _str[1] = 'u';
2356 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2357 str = _str;
2358 }
2359 else {
2360 len = 1;
Willy Tarreau5715da22020-02-25 08:37:37 +01002361 _str[0] = c;
2362 str = _str;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002363 }
2364
2365 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002366 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002367 return 0;
2368
2369 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002370 memcpy(temp->area + temp->data, str, len);
2371 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002372 }
2373
2374 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002375 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002376 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002377
2378 return 1;
2379}
2380
Emeric Brun54c4ac82014-11-03 15:32:43 +01002381/* This sample function is designed to extract some bytes from an input buffer.
2382 * First arg is the offset.
2383 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002384static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002385{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002386 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2387 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002388 return 1;
2389 }
2390
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002391 if (smp->data.u.str.size)
2392 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002393 smp->data.u.str.data -= arg_p[0].data.sint;
2394 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002395
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002396 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2397 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002398
2399 return 1;
2400}
2401
Emeric Brunf399b0d2014-11-03 17:07:03 +01002402static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2403 const char *file, int line, char **err)
2404{
2405 struct arg *arg = args;
2406
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002407 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002408 memprintf(err, "Unexpected arg type");
2409 return 0;
2410 }
2411
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002412 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002413 memprintf(err, "Unexpected value 0 for index");
2414 return 0;
2415 }
2416
2417 arg++;
2418
2419 if (arg->type != ARGT_STR) {
2420 memprintf(err, "Unexpected arg type");
2421 return 0;
2422 }
2423
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002424 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002425 memprintf(err, "Empty separators list");
2426 return 0;
2427 }
2428
2429 return 1;
2430}
2431
2432/* This sample function is designed to a return selected part of a string (field).
2433 * First arg is the index of the field (start at 1)
2434 * Second arg is a char list of separators (type string)
2435 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002436static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002437{
Marcin Deranek9631a282018-04-16 14:30:46 +02002438 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002439 char *start, *end;
2440 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002441 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002442
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002443 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002444 return 0;
2445
Marcin Deranek9631a282018-04-16 14:30:46 +02002446 if (arg_p[0].data.sint < 0) {
2447 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002448 end = start = smp->data.u.str.area + smp->data.u.str.data;
2449 while (start > smp->data.u.str.area) {
2450 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2451 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002452 if (field == arg_p[0].data.sint) {
2453 if (count == 1)
2454 goto found;
2455 else if (count > 1)
2456 count--;
2457 } else {
2458 end = start-1;
2459 field--;
2460 }
2461 break;
2462 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002463 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002464 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002465 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002466 } else {
2467 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002468 end = start = smp->data.u.str.area;
2469 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2470 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2471 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002472 if (field == arg_p[0].data.sint) {
2473 if (count == 1)
2474 goto found;
2475 else if (count > 1)
2476 count--;
2477 } else {
2478 start = end+1;
2479 field++;
2480 }
2481 break;
2482 }
2483 }
2484 end++;
2485 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002486 }
2487
2488 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002489 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002490 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002491 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002492 }
2493found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002494 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002495 /* If ret string is len 0, no need to
2496 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002497 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002498 return 1;
2499
Emeric Brunf399b0d2014-11-03 17:07:03 +01002500 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002501 Note: smp->data.u.str.size cannot be set to 0 */
2502 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002503 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002504
Thayne McCombsb2843052021-04-11 23:26:59 -06002505 smp->data.u.str.area = start;
2506
Emeric Brunf399b0d2014-11-03 17:07:03 +01002507 return 1;
2508}
2509
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002510/* This sample function is designed to return a word from a string.
2511 * First arg is the index of the word (start at 1)
2512 * Second arg is a char list of words separators (type string)
2513 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002514static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002515{
Marcin Deranek9631a282018-04-16 14:30:46 +02002516 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002517 char *start, *end;
2518 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002519 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002520
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002521 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002522 return 0;
2523
2524 word = 0;
2525 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002526 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002527 end = start = smp->data.u.str.area + smp->data.u.str.data;
2528 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002529 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002530 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2531 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002532 issep = 1;
2533 break;
2534 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002535 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002536 if (!inword) {
2537 if (!issep) {
2538 if (word != arg_p[0].data.sint) {
2539 word--;
2540 end = start;
2541 }
2542 inword = 1;
2543 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002544 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002545 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002546 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002547 if (count == 1)
2548 goto found;
2549 else if (count > 1)
2550 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002551 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002552 inword = 0;
2553 }
2554 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002555 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002556 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002557 end = start = smp->data.u.str.area;
2558 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002559 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002560 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2561 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002562 issep = 1;
2563 break;
2564 }
2565 }
2566 if (!inword) {
2567 if (!issep) {
2568 if (word != arg_p[0].data.sint) {
2569 word++;
2570 start = end;
2571 }
2572 inword = 1;
2573 }
2574 }
2575 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002576 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002577 if (count == 1)
2578 goto found;
2579 else if (count > 1)
2580 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002581 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002582 inword = 0;
2583 }
2584 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002585 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002586 }
2587
2588 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002589 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002590 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002591 return 1;
2592 }
2593found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002594 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002595 /* If ret string is len 0, no need to
2596 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002597 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002598 return 1;
2599
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002600
2601 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002602 Note: smp->data.u.str.size cannot be set to 0 */
2603 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002604 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002605
Thayne McCombs6a0d2172022-05-25 22:58:51 -06002606 smp->data.u.str.area = start;
2607
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002608 return 1;
2609}
2610
Thayne McCombs02cf4ec2022-12-14 00:19:59 -07002611static int sample_conv_param_check(struct arg *arg, struct sample_conv *conv,
2612 const char *file, int line, char **err)
2613{
2614 if (arg[1].type == ARGT_STR && arg[1].data.str.data != 1) {
2615 memprintf(err, "Delimiter must be exactly 1 character.");
2616 return 0;
2617 }
2618
2619 return 1;
2620}
2621
2622static int sample_conv_param(const struct arg *arg_p, struct sample *smp, void *private)
2623{
2624 char *pos, *end, *pend, *equal;
2625 char delim = '&';
2626 const char *name = arg_p[0].data.str.area;
2627 size_t name_l = arg_p[0].data.str.data;
2628
2629 if (arg_p[1].type == ARGT_STR)
2630 delim = *arg_p[1].data.str.area;
2631
2632 pos = smp->data.u.str.area;
2633 end = pos + smp->data.u.str.data;
2634 while (pos < end) {
2635 equal = pos + name_l;
2636 /* Parameter not found */
2637 if (equal > end)
2638 break;
2639
2640 if (equal == end || *equal == delim) {
2641 if (memcmp(pos, name, name_l) == 0) {
2642 /* input contains parameter, but no value is supplied */
2643 smp->data.u.str.data = 0;
2644 return 1;
2645 }
2646 pos = equal + 1;
2647 continue;
2648 }
2649
2650 if (*equal == '=' && memcmp(pos, name, name_l) == 0) {
2651 pos = equal + 1;
2652 pend = memchr(pos, delim, end - pos);
2653 if (pend == NULL)
2654 pend = end;
2655
2656 if (smp->data.u.str.size)
2657 smp->data.u.str.size -= pos - smp->data.u.str.area;
2658 smp->data.u.str.area = pos;
2659 smp->data.u.str.data = pend - pos;
2660 return 1;
2661 }
2662 /* find the next delimiter and set position to character after that */
2663 pos = memchr(pos, delim, end - pos);
2664 if (pos == NULL)
2665 pos = end;
2666 else
2667 pos++;
2668 }
2669 /* Parameter not found */
2670 smp->data.u.str.data = 0;
2671 return 0;
2672}
2673
Willy Tarreau7eda8492015-01-20 19:47:06 +01002674static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2675 const char *file, int line, char **err)
2676{
2677 struct arg *arg = args;
2678 char *p;
2679 int len;
2680
2681 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2682 arg[0].type_flags = 0;
2683
2684 if (arg[2].type != ARGT_STR)
2685 return 1;
2686
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002687 p = arg[2].data.str.area;
2688 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002689 while (len) {
2690 if (*p == 'i') {
2691 arg[0].type_flags |= ARGF_REG_ICASE;
2692 }
2693 else if (*p == 'g') {
2694 arg[0].type_flags |= ARGF_REG_GLOB;
2695 }
2696 else {
2697 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2698 return 0;
2699 }
2700 p++;
2701 len--;
2702 }
2703 return 1;
2704}
2705
2706/* This sample function is designed to do the equivalent of s/match/replace/ on
2707 * the input string. It applies a regex and restarts from the last matched
2708 * location until nothing matches anymore. First arg is the regex to apply to
2709 * the input string, second arg is the replacement expression.
2710 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002711static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002712{
2713 char *start, *end;
2714 struct my_regex *reg = arg_p[0].data.reg;
2715 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002716 struct buffer *trash = get_trash_chunk();
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002717 struct buffer *output;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002718 int flag, max;
2719 int found;
2720
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002721 start = smp->data.u.str.area;
2722 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002723
2724 flag = 0;
2725 while (1) {
2726 /* check for last round which is used to copy remaining parts
2727 * when not running in global replacement mode.
2728 */
2729 found = 0;
2730 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2731 /* Note: we can have start == end on empty strings or at the end */
2732 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2733 }
2734
2735 if (!found)
2736 pmatch[0].rm_so = end - start;
2737
2738 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002739 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002740 if (max && pmatch[0].rm_so > 0) {
2741 if (max > pmatch[0].rm_so)
2742 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002743 memcpy(trash->area + trash->data, start, max);
2744 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002745 }
2746
2747 if (!found)
2748 break;
2749
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002750 output = alloc_trash_chunk();
Willy Tarreau23997da2020-02-18 14:27:44 +01002751 if (!output)
2752 break;
2753
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002754 output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2755
Willy Tarreau7eda8492015-01-20 19:47:06 +01002756 /* replace the matching part */
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002757 max = output->size - output->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002758 if (max) {
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002759 if (max > output->data)
2760 max = output->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002761 memcpy(trash->area + trash->data,
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002762 output->area, max);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002763 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002764 }
2765
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002766 free_trash_chunk(output);
2767
Willy Tarreau7eda8492015-01-20 19:47:06 +01002768 /* stop here if we're done with this string */
2769 if (start >= end)
2770 break;
2771
2772 /* We have a special case for matches of length 0 (eg: "x*y*").
2773 * These ones are considered to match in front of a character,
2774 * so we have to copy that character and skip to the next one.
2775 */
2776 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002777 if (trash->data < trash->size)
2778 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002779 pmatch[0].rm_eo++;
2780 }
2781
2782 start += pmatch[0].rm_eo;
2783 flag |= REG_NOTBOL;
2784 }
2785
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002786 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002787 return 1;
2788}
2789
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002790/* This function check an operator entry. It expects a string.
2791 * The string can be an integer or a variable name.
2792 */
2793static int check_operator(struct arg *args, struct sample_conv *conv,
2794 const char *file, int line, char **err)
2795{
2796 const char *str;
2797 const char *end;
Christopher Faulet95917132020-08-05 23:07:07 +02002798 long long int i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002799
2800 /* Try to decode a variable. */
2801 if (vars_check_arg(&args[0], NULL))
2802 return 1;
2803
2804 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002805 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002806 end = str + strlen(str);
Christopher Faulet95917132020-08-05 23:07:07 +02002807 i = read_int64(&str, end);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002808 if (*str != '\0') {
2809 memprintf(err, "expects an integer or a variable name");
2810 return 0;
2811 }
Christopher Faulet95917132020-08-05 23:07:07 +02002812
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002813 chunk_destroy(&args[0].data.str);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002814 args[0].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02002815 args[0].data.sint = i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002816 return 1;
2817}
2818
Willy Tarreau6204cd92016-03-10 16:33:04 +01002819/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002820 * If the arg contain an integer, the integer is returned in the
2821 * sample. If the arg contains a variable descriptor, it returns the
2822 * variable value.
2823 *
2824 * This function returns 0 if an error occurs, otherwise it returns 1.
2825 */
Willy Tarreaud9be5992021-10-06 15:19:05 +02002826int sample_conv_var2smp_sint(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002827{
2828 switch (arg->type) {
2829 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002830 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002831 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002832 return 1;
2833 case ARGT_VAR:
Willy Tarreau2476ff12021-10-06 15:30:52 +02002834 return sample_conv_var2smp(&arg->data.var, smp, SMP_T_SINT);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002835 default:
2836 return 0;
2837 }
2838}
2839
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002840/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002841 * result.
2842 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002843static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002844{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002845 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002846 return 1;
2847}
2848
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002849/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002850 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002851 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002852static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002853{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002854 struct sample tmp;
2855
Willy Tarreau7560dd42016-03-10 16:28:58 +01002856 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002857 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002858 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002859 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002860 return 1;
2861}
2862
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002863/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002864 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002865 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002866static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002867{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002868 struct sample tmp;
2869
Willy Tarreau7560dd42016-03-10 16:28:58 +01002870 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002871 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002872 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002873 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002874 return 1;
2875}
2876
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002877/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002878 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002879 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002880static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002881{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002882 struct sample tmp;
2883
Willy Tarreau7560dd42016-03-10 16:28:58 +01002884 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002885 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002886 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002887 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002888 return 1;
2889}
2890
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002891static inline long long int arith_add(long long int a, long long int b)
2892{
2893 /* Prevent overflow and makes capped calculus.
2894 * We must ensure that the check calculus doesn't
2895 * exceed the signed 64 bits limits.
2896 *
2897 * +----------+----------+
2898 * | a<0 | a>=0 |
2899 * +------+----------+----------+
2900 * | b<0 | MIN-a>b | no check |
2901 * +------+----------+----------+
2902 * | b>=0 | no check | MAX-a<b |
2903 * +------+----------+----------+
2904 */
2905 if ((a ^ b) >= 0) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002906 /* signs are different. */
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002907 if (a < 0) {
2908 if (LLONG_MIN - a > b)
2909 return LLONG_MIN;
2910 }
2911 if (LLONG_MAX - a < b)
2912 return LLONG_MAX;
2913 }
2914 return a + b;
2915}
2916
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002917/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002918 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002919 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002920static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002921{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002922 struct sample tmp;
2923
Willy Tarreau7560dd42016-03-10 16:28:58 +01002924 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002925 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002926 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002927 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002928 return 1;
2929}
2930
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002931/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002932 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002933 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002934static int sample_conv_arith_sub(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 FOURNIER00c005c2015-07-08 01:10:21 +02002943 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2944 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2945 * of -LLONG_MIN and correct the result.
2946 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002947 if (tmp.data.u.sint == LLONG_MIN) {
2948 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2949 if (smp->data.u.sint < LLONG_MAX)
2950 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002951 return 1;
2952 }
2953
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002954 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002955 * the second operand.
2956 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002957 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002958 return 1;
2959}
2960
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002961/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002962 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002963 * If the result makes an overflow, then the largest possible quantity is
2964 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002965 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002966static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002967 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002968{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002969 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002970 long long int c;
2971
Willy Tarreau7560dd42016-03-10 16:28:58 +01002972 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002973 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002974 return 0;
2975
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002976 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002977 if (!smp->data.u.sint || !tmp.data.u.sint) {
2978 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002979 return 1;
2980 }
2981
2982 /* The multiply between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002983 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002984 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002985 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2986 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002987 return 1;
2988 }
2989
2990 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002991 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002992
2993 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002994 if (smp->data.u.sint != c / tmp.data.u.sint) {
2995 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2996 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002997 return 1;
2998 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002999 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003000 return 1;
3001 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003002 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01003003 return 1;
3004}
3005
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003006/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003007 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003008 * If arg_p makes the result overflow, then the largest possible quantity is
3009 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003010 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003011static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003012 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003013{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003014 struct sample tmp;
3015
Willy Tarreau7560dd42016-03-10 16:28:58 +01003016 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02003017 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003018 return 0;
3019
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003020 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003021 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003022 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003023 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003024 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3025 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003026 return 1;
3027 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003028 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003029 return 1;
3030 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003031 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01003032 return 1;
3033}
3034
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003035/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003036 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003037 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003038 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003039static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003040 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003041{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003042 struct sample tmp;
3043
Willy Tarreau7560dd42016-03-10 16:28:58 +01003044 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02003045 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003046 return 0;
3047
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003048 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003049 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003050 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003051 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003052 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3053 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003054 return 1;
3055 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003056 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003057 return 1;
3058 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003059 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01003060 return 1;
3061}
3062
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003063/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01003064 * result.
3065 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003066static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003067 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003068{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003069 if (smp->data.u.sint == LLONG_MIN)
3070 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003071 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003072 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003073 return 1;
3074}
3075
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003076/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003077 * false. The output is a BOOL.
3078 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003079static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003080 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003081{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003082 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003083 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003084 return 1;
3085}
3086
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003087/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003088 * truee. The output is a BOOL.
3089 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003090static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003091 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003092{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003093 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003094 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003095 return 1;
3096}
3097
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003098/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003099 * The output is a BOOL.
3100 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003101static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003102 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003103{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003104 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003105 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003106 return 1;
3107}
3108
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003109/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003110 * The output is a BOOL.
3111 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003112static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003113 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003114{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003115 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003116 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003117 return 1;
3118}
3119
Willy Tarreau280f42b2018-02-19 15:34:12 +01003120/* appends an optional const string, an optional variable contents and another
3121 * optional const string to an existing string.
3122 */
3123static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
3124{
Willy Tarreau83061a82018-07-13 11:56:34 +02003125 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003126 struct sample tmp;
3127 int max;
3128
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003129 trash = alloc_trash_chunk();
William Dauchye9970102021-01-11 11:05:58 +01003130 if (!trash)
3131 return 0;
3132
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003133 trash->data = smp->data.u.str.data;
3134 if (trash->data > trash->size - 1)
3135 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003136
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003137 memcpy(trash->area, smp->data.u.str.area, trash->data);
3138 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003139
3140 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003141 max = arg_p[0].data.str.data;
3142 if (max > trash->size - 1 - trash->data)
3143 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003144
3145 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003146 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
3147 trash->data += max;
3148 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003149 }
3150
3151 /* append second string (variable) if it's found and we can turn it
3152 * into a string.
3153 */
3154 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaue352b9d2021-09-03 11:52:38 +02003155 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 +01003156 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
3157 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
3158
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003159 max = tmp.data.u.str.data;
3160 if (max > trash->size - 1 - trash->data)
3161 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003162
3163 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003164 memcpy(trash->area + trash->data, tmp.data.u.str.area,
3165 max);
3166 trash->data += max;
3167 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003168 }
3169 }
3170
3171 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003172 max = arg_p[2].data.str.data;
3173 if (max > trash->size - 1 - trash->data)
3174 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003175
3176 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003177 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
3178 trash->data += max;
3179 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003180 }
3181
3182 smp->data.u.str = *trash;
3183 smp->data.type = SMP_T_STR;
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003184 smp_dup(smp);
3185 free_trash_chunk(trash);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003186 return 1;
3187}
3188
3189/* This function checks the "concat" converter's arguments and extracts the
3190 * variable name and its scope.
3191 */
3192static int smp_check_concat(struct arg *args, struct sample_conv *conv,
3193 const char *file, int line, char **err)
3194{
3195 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003196 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
3197 memprintf(err, "failed to register variable name '%s'",
3198 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003199 return 0;
3200 }
3201 return 1;
3202}
3203
Nikola Sale0dbf0382022-04-03 18:11:53 +02003204/* Append delimiter (only to a non empty input) followed by the optional
3205 * variable contents concatenated with the optional sufix.
3206 */
3207static int sample_conv_add_item(const struct arg *arg_p, struct sample *smp, void *private)
3208{
3209 struct buffer *tmpbuf;
3210 struct sample tmp;
3211 size_t max;
3212 int var_available;
3213
3214 tmpbuf = alloc_trash_chunk();
3215 if (!tmpbuf)
3216 return 0;
3217
3218 tmpbuf->data = smp->data.u.str.data;
3219 if (tmpbuf->data > tmpbuf->size - 1)
3220 tmpbuf->data = tmpbuf->size - 1;
3221
3222 memcpy(tmpbuf->area, smp->data.u.str.area, tmpbuf->data);
3223 tmpbuf->area[tmpbuf->data] = 0;
3224
3225 /* Check if variable is found and we can turn into a string. */
3226 var_available = 0;
3227 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3228 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp, NULL) &&
3229 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
3230 sample_casts[tmp.data.type][SMP_T_STR](&tmp)))
3231 var_available = 1;
3232
3233 /* Append delimiter only if input is not empty and either
3234 * the variable or the suffix are not empty
3235 */
3236 if (smp->data.u.str.data && ((var_available && tmp.data.u.str.data) ||
3237 arg_p[2].data.str.data)) {
3238 max = arg_p[0].data.str.data;
3239 if (max > tmpbuf->size - 1 - tmpbuf->data)
3240 max = tmpbuf->size - 1 - tmpbuf->data;
3241
3242 if (max) {
3243 memcpy(tmpbuf->area + tmpbuf->data, arg_p[0].data.str.area, max);
3244 tmpbuf->data += max;
3245 tmpbuf->area[tmpbuf->data] = 0;
3246 }
3247 }
3248
3249 /* Append variable contents if variable is found and turned into string. */
3250 if (var_available) {
3251 max = tmp.data.u.str.data;
3252 if (max > tmpbuf->size - 1 - tmpbuf->data)
3253 max = tmpbuf->size - 1 - tmpbuf->data;
3254
3255 if (max) {
3256 memcpy(tmpbuf->area + tmpbuf->data, tmp.data.u.str.area, max);
3257 tmpbuf->data += max;
3258 tmpbuf->area[tmpbuf->data] = 0;
3259 }
3260 }
3261
3262 /* Append optional suffix. */
3263 max = arg_p[2].data.str.data;
3264 if (max > tmpbuf->size - 1 - tmpbuf->data)
3265 max = tmpbuf->size - 1 - tmpbuf->data;
3266
3267 if (max) {
3268 memcpy(tmpbuf->area + tmpbuf->data, arg_p[2].data.str.area, max);
3269 tmpbuf->data += max;
3270 tmpbuf->area[tmpbuf->data] = 0;
3271 }
3272
3273 smp->data.u.str = *tmpbuf;
3274 smp->data.type = SMP_T_STR;
3275 smp_dup(smp);
3276 free_trash_chunk(tmpbuf);
3277 return 1;
3278}
3279
3280/* Check the "add_item" converter's arguments and extracts the
3281 * variable name and its scope.
3282 */
3283static int smp_check_add_item(struct arg *args, struct sample_conv *conv,
3284 const char *file, int line, char **err)
3285{
3286 /* Try to decode a variable. */
3287 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
3288 memprintf(err, "failed to register variable name '%s'",
3289 args[1].data.str.area);
3290 return 0;
3291 }
3292
3293 if (args[1].data.str.data == 0 && args[2].data.str.data == 0) {
3294 memprintf(err, "one of the optional arguments has to be nonempty");
3295 return 0;
3296 }
3297
3298 return 1;
3299}
3300
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003301/* Compares string with a variable containing a string. Return value
Tim Duesterhusca097c12018-04-27 21:18:45 +02003302 * is compatible with strcmp(3)'s return value.
3303 */
3304static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
3305{
3306 struct sample tmp;
3307 int max, result;
3308
3309 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3310 if (arg_p[0].type != ARGT_VAR)
3311 return 0;
Willy Tarreau2476ff12021-10-06 15:30:52 +02003312
3313 if (!sample_conv_var2smp(&arg_p[0].data.var, &tmp, SMP_T_STR))
Tim Duesterhusca097c12018-04-27 21:18:45 +02003314 return 0;
3315
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003316 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
3317 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003318 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003319 if (smp->data.u.str.data != tmp.data.u.str.data) {
3320 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02003321 result = -1;
3322 }
3323 else {
3324 result = 1;
3325 }
3326 }
3327 }
3328
3329 smp->data.u.sint = result;
3330 smp->data.type = SMP_T_SINT;
3331 return 1;
3332}
William Lallemanddd754cb2022-08-26 16:21:28 +02003333/*
3334 * This converter can takes a Host header value as defined by rfc9110#section-7.2
3335 * Host = uri-host [ ":" port ] ;
3336 * It returns the uri-host value in lowecase with the port stripped.
3337 */
3338static int sample_conv_host_only(const struct arg *arg_p, struct sample *smp, void *private)
3339{
3340 /* Working cases: hostname00, hostname00:80, 127.0.0.1, 127.0.0.1:80, [::1], [::1]:80 */
3341 char *beg = smp->data.u.str.area;
3342 char *end = smp->data.u.str.area + smp->data.u.str.data - 1;
3343 char *p;
3344
3345 for (p = end; p >= beg; p--) {
3346 if (*p == ':' || *p == ']')
3347 break;
3348 }
3349
3350 if (p >= beg && *p == ':')
3351 smp->data.u.str.data = p - beg;
3352 /* if no port part was found, the hostname is the whole string */
3353
3354 smp->data.type = SMP_T_STR;
3355
3356 return sample_conv_str2lower(arg_p, smp, NULL);
3357}
3358
3359/*
3360 * This converter can takes a Host header value as defined by rfc9110#section-7.2
3361 * Host = uri-host [ ":" port ] ;
3362 * It returns the port value as a int.
3363 */
3364static int sample_conv_port_only(const struct arg *arg_p, struct sample *smp, void *private)
3365{
3366 /* Working cases: hostname00, hostname00:80, 127.0.0.1, 127.0.0.1:80, [::1], [::1]:80 */
3367 char *beg = smp->data.u.str.area;
3368 char *end = smp->data.u.str.area + smp->data.u.str.data - 1;
3369 char *p;
3370
3371 for (p = end; p >= beg; p--) {
3372 if (*p == ':' || *p == ']')
3373 break;
3374 }
3375
3376 smp->data.type = SMP_T_SINT;
3377 if (p >= beg && *p == ':' && ++p <= end) {
3378 smp->data.u.sint = strl2ui(p, smp->data.u.str.data + smp->data.u.str.area - p);
3379 } else {
3380 smp->data.u.sint = 0;
3381 }
3382 return 1;
3383}
3384
Tim Duesterhusca097c12018-04-27 21:18:45 +02003385
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02003386/* Takes a boolean as input. Returns the first argument if that boolean is true and
3387 * the second argument otherwise.
3388 */
3389static int sample_conv_iif(const struct arg *arg_p, struct sample *smp, void *private)
3390{
3391 smp->data.type = SMP_T_STR;
3392 smp->flags |= SMP_F_CONST;
3393
3394 if (smp->data.u.sint) {
3395 smp->data.u.str.data = arg_p[0].data.str.data;
3396 smp->data.u.str.area = arg_p[0].data.str.area;
3397 }
3398 else {
3399 smp->data.u.str.data = arg_p[1].data.str.data;
3400 smp->data.u.str.area = arg_p[1].data.str.area;
3401 }
3402
3403 return 1;
3404}
3405
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003406#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
3407#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
3408#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
3409
3410/*
3411 * Extract the field value of an input binary sample. Takes a mandatory argument:
3412 * the protocol buffers field identifier (dotted notation) internally represented
3413 * as an array of unsigned integers and its size.
3414 * Return 1 if the field was found, 0 if not.
3415 */
3416static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
3417{
3418 unsigned char *pos;
3419 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003420
3421 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003422 grpc_left = smp->data.u.str.data;
3423
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01003424 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003425 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003426
3427 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
3428
3429 pos += GRPC_MSG_HEADER_SZ;
3430 grpc_left -= GRPC_MSG_HEADER_SZ;
3431
3432 if (grpc_left < left)
3433 return 0;
3434
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01003435 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
3436 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003437
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003438 grpc_left -= grpc_msg_len;
3439 }
3440
3441 return 0;
3442}
3443
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003444static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
3445{
3446 unsigned char *pos;
3447 size_t left;
3448
3449 pos = (unsigned char *)smp->data.u.str.area;
3450 left = smp->data.u.str.data;
3451
3452 return protobuf_field_lookup(arg_p, smp, &pos, &left);
3453}
3454
3455static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
3456 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003457{
3458 if (!args[1].type) {
3459 args[1].type = ARGT_SINT;
3460 args[1].data.sint = PBUF_T_BINARY;
3461 }
3462 else {
3463 int pbuf_type;
3464
3465 pbuf_type = protobuf_type(args[1].data.str.area);
3466 if (pbuf_type == -1) {
3467 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
3468 return 0;
3469 }
3470
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003471 chunk_destroy(&args[1].data.str);
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003472 args[1].type = ARGT_SINT;
3473 args[1].data.sint = pbuf_type;
3474 }
3475
3476 return 1;
3477}
3478
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003479/*
3480 * Extract the tag value of an input binary sample. Takes a mandatory argument:
3481 * the FIX protocol tag identifier.
3482 * Return 1 if the tag was found, 0 if not.
3483 */
3484static int sample_conv_fix_tag_value(const struct arg *arg_p, struct sample *smp, void *private)
3485{
3486 struct ist value;
3487
3488 smp->flags &= ~SMP_F_MAY_CHANGE;
3489 value = fix_tag_value(ist2(smp->data.u.str.area, smp->data.u.str.data),
3490 arg_p[0].data.sint);
3491 if (!istlen(value)) {
Christopher Fauleta5aa0822021-10-13 17:58:53 +02003492 if (isttest(value)) {
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003493 /* value != IST_NULL, need more data */
3494 smp->flags |= SMP_F_MAY_CHANGE;
3495 }
3496 return 0;
3497 }
3498
3499 smp->data.u.str = ist2buf(value);
3500 smp->flags |= SMP_F_CONST;
3501
3502 return 1;
3503}
3504
3505/* This function checks the "fix_tag_value" converter configuration.
3506 * It expects a "known" (by HAProxy) tag name or ID.
3507 * Tag string names are converted to their ID counterpart because this is the
3508 * format they are sent over the wire.
3509 */
3510static int sample_conv_fix_value_check(struct arg *args, struct sample_conv *conv,
3511 const char *file, int line, char **err)
3512{
3513 struct ist str;
3514 unsigned int tag;
3515
3516 str = ist2(args[0].data.str.area, args[0].data.str.data);
3517 tag = fix_tagid(str);
3518 if (!tag) {
3519 memprintf(err, "Unknown FIX tag name '%s'", args[0].data.str.area);
3520 return 0;
3521 }
3522
3523 chunk_destroy(&args[0].data.str);
3524 args[0].type = ARGT_SINT;
3525 args[0].data.sint = tag;
3526
3527 return 1;
3528}
3529
3530/*
3531 * Checks that a buffer contains a valid FIX message
3532 *
3533 * Return 1 if the check could be run, 0 if not.
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003534 * The result of the analyse itself is stored in <smp> as a boolean
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003535 */
3536static int sample_conv_fix_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3537{
3538 struct ist msg;
3539
3540 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3541
3542 smp->flags &= ~SMP_F_MAY_CHANGE;
3543 switch (fix_validate_message(msg)) {
3544 case FIX_VALID_MESSAGE:
3545 smp->data.type = SMP_T_BOOL;
3546 smp->data.u.sint = 1;
3547 return 1;
3548 case FIX_NEED_MORE_DATA:
3549 smp->flags |= SMP_F_MAY_CHANGE;
3550 return 0;
3551 case FIX_INVALID_MESSAGE:
3552 smp->data.type = SMP_T_BOOL;
3553 smp->data.u.sint = 0;
3554 return 1;
3555 }
3556 return 0;
3557}
3558
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003559/*
3560 * Extract the field value of an input binary sample containing an MQTT packet.
3561 * Takes 2 mandatory arguments:
3562 * - packet type
3563 * - field name
3564 *
3565 * return 1 if the field was found, 0 if not.
3566 */
3567static int sample_conv_mqtt_field_value(const struct arg *arg_p, struct sample *smp, void *private)
3568{
3569 struct ist pkt, value;
3570 int type, fieldname_id;
3571
3572 pkt = ist2(smp->data.u.str.area, smp->data.u.str.data);
3573 type = arg_p[0].data.sint;
3574 fieldname_id = arg_p[1].data.sint;
3575
3576 smp->flags &= ~SMP_F_MAY_CHANGE;
3577 value = mqtt_field_value(pkt, type, fieldname_id);
3578 if (!istlen(value)) {
3579 if (isttest(value)) {
3580 /* value != IST_NULL, need more data */
3581 smp->flags |= SMP_F_MAY_CHANGE;
3582 }
3583 return 0;
3584 }
3585
3586 smp->data.u.str = ist2buf(value);
3587 smp->flags |= SMP_F_CONST;
3588 return 1;
3589}
3590
3591/*
3592 * this function checks the "mqtt_field_value" converter configuration.
3593 * It expects a known packet type name or ID and a field name, in this order
3594 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003595 * Args[0] will be turned into a MQTT_CPT_* value for direct matching when parsing
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003596 * a packet.
3597 */
3598static int sample_conv_mqtt_field_value_check(struct arg *args, struct sample_conv *conv,
3599 const char *file, int line, char **err)
3600{
3601 int type, fieldname_id;
3602
3603 /* check the MQTT packet type is valid */
3604 type = mqtt_typeid(ist2(args[0].data.str.area, args[0].data.str.data));
3605 if (type == MQTT_CPT_INVALID) {
3606 memprintf(err, "Unknown MQTT type '%s'", args[0].data.str.area);
3607 return 0;
3608 }
3609
3610 /* check the field name belongs to the MQTT packet type */
3611 fieldname_id = mqtt_check_type_fieldname(type, ist2(args[1].data.str.area, args[1].data.str.data));
3612 if (fieldname_id == MQTT_FN_INVALID) {
3613 memprintf(err, "Unknown MQTT field name '%s' for packet type '%s'", args[1].data.str.area,
3614 args[0].data.str.area);
3615 return 0;
3616 }
3617
3618 /* save numeric counterparts of type and field name */
3619 chunk_destroy(&args[0].data.str);
3620 chunk_destroy(&args[1].data.str);
3621 args[0].type = ARGT_SINT;
3622 args[0].data.sint = type;
3623 args[1].type = ARGT_SINT;
3624 args[1].data.sint = fieldname_id;
3625
3626 return 1;
3627}
3628
3629/*
3630 * Checks that <smp> contains a valid MQTT message
3631 *
3632 * The function returns 1 if the check was run to its end, 0 otherwise.
3633 * The result of the analyse itself is stored in <smp> as a boolean.
3634 */
3635static int sample_conv_mqtt_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3636{
3637 struct ist msg;
3638
3639 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3640
3641 smp->flags &= ~SMP_F_MAY_CHANGE;
3642 switch (mqtt_validate_message(msg, NULL)) {
3643 case FIX_VALID_MESSAGE:
3644 smp->data.type = SMP_T_BOOL;
3645 smp->data.u.sint = 1;
3646 return 1;
3647 case FIX_NEED_MORE_DATA:
3648 smp->flags |= SMP_F_MAY_CHANGE;
3649 return 0;
3650 case FIX_INVALID_MESSAGE:
3651 smp->data.type = SMP_T_BOOL;
3652 smp->data.u.sint = 0;
3653 return 1;
3654 }
3655 return 0;
3656}
3657
Tim Duesterhusca097c12018-04-27 21:18:45 +02003658/* This function checks the "strcmp" converter's arguments and extracts the
3659 * variable name and its scope.
3660 */
3661static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
3662 const char *file, int line, char **err)
3663{
Willy Tarreaua1169b62021-05-08 06:50:28 +02003664 if (!args[0].data.str.data) {
3665 memprintf(err, "missing variable name");
3666 return 0;
3667 }
3668
Tim Duesterhusca097c12018-04-27 21:18:45 +02003669 /* Try to decode a variable. */
3670 if (vars_check_arg(&args[0], NULL))
3671 return 1;
3672
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003673 memprintf(err, "failed to register variable name '%s'",
3674 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003675 return 0;
3676}
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003677
Christopher Faulet4ccc12f2020-04-01 09:08:32 +02003678/**/
3679static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private)
3680{
3681 struct buffer *tmp;
3682 uint32_t n;
3683
3684 n = htonl((uint32_t)smp->data.u.sint);
3685 tmp = get_trash_chunk();
3686
3687 memcpy(b_head(tmp), &n, 4);
3688 b_add(tmp, 4);
3689
3690 smp->data.u.str = *tmp;
3691 smp->data.type = SMP_T_BIN;
3692 return 1;
3693}
3694
Christopher Fauletea159d62020-04-01 16:21:44 +02003695/**/
3696static int sample_conv_cut_crlf(const struct arg *arg_p, struct sample *smp, void *private)
3697{
3698 char *p;
3699 size_t l;
3700
3701 p = smp->data.u.str.area;
3702 for (l = 0; l < smp->data.u.str.data; l++) {
3703 if (*(p+l) == '\r' || *(p+l) == '\n')
3704 break;
3705 }
3706 smp->data.u.str.data = l;
3707 return 1;
3708}
3709
Christopher Faulet51fc9d12020-04-01 17:24:41 +02003710/**/
3711static int sample_conv_ltrim(const struct arg *arg_p, struct sample *smp, void *private)
3712{
3713 char *delimiters, *p;
3714 size_t dlen, l;
3715
3716 delimiters = arg_p[0].data.str.area;
3717 dlen = arg_p[0].data.str.data;
3718
3719 l = smp->data.u.str.data;
3720 p = smp->data.u.str.area;
3721 while (l && memchr(delimiters, *p, dlen) != NULL) {
3722 p++;
3723 l--;
3724 }
3725
3726 smp->data.u.str.area = p;
3727 smp->data.u.str.data = l;
3728 return 1;
3729}
3730
Christopher Faulet568415a2020-04-01 17:24:47 +02003731/**/
3732static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *private)
3733{
3734 char *delimiters, *p;
3735 size_t dlen, l;
3736
3737 delimiters = arg_p[0].data.str.area;
3738 dlen = arg_p[0].data.str.data;
3739
3740 l = smp->data.u.str.data;
3741 p = smp->data.u.str.area + l - 1;
3742 while (l && memchr(delimiters, *p, dlen) != NULL) {
3743 p--;
3744 l--;
3745 }
3746
3747 smp->data.u.str.data = l;
3748 return 1;
3749}
3750
Alex51c8ad42021-04-15 16:45:15 +02003751/* This function checks the "json_query" converter's arguments. */
3752static int sample_check_json_query(struct arg *arg, struct sample_conv *conv,
3753 const char *file, int line, char **err)
3754{
3755 if (arg[0].data.str.data == 0) {
3756 memprintf(err, "json_path must not be empty");
3757 return 0;
3758 }
3759
3760 if (arg[1].data.str.data != 0) {
3761 if (strcmp(arg[1].data.str.area, "int") != 0) {
3762 memprintf(err, "output_type only supports \"int\" as argument");
3763 return 0;
3764 } else {
3765 arg[1].type = ARGT_SINT;
3766 arg[1].data.sint = 0;
3767 }
3768 }
3769 return 1;
3770}
3771
3772/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
3773 * the recommendation for interoperable integers in section 6 of RFC 7159.
3774 */
3775#define JSON_INT_MAX ((1LL << 53) - 1)
3776#define JSON_INT_MIN (-JSON_INT_MAX)
3777
3778/* This sample function get the value from a given json string.
3779 * The mjson library is used to parse the JSON struct
3780 */
3781static int sample_conv_json_query(const struct arg *args, struct sample *smp, void *private)
3782{
3783 struct buffer *trash = get_trash_chunk();
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003784 const char *token; /* holds the temporary string from mjson_find */
3785 int token_size; /* holds the length of <token> */
Alex51c8ad42021-04-15 16:45:15 +02003786
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003787 enum mjson_tok token_type;
Alex51c8ad42021-04-15 16:45:15 +02003788
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003789 token_type = mjson_find(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &token, &token_size);
3790
3791 switch (token_type) {
Alex51c8ad42021-04-15 16:45:15 +02003792 case MJSON_TOK_NUMBER:
3793 if (args[1].type == ARGT_SINT) {
Willy Tarreaufb601952021-05-14 08:51:53 +02003794 smp->data.u.sint = strtoll(token, NULL, 0);
Alex51c8ad42021-04-15 16:45:15 +02003795
3796 if (smp->data.u.sint < JSON_INT_MIN || smp->data.u.sint > JSON_INT_MAX)
3797 return 0;
3798
3799 smp->data.type = SMP_T_SINT;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003800
3801 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003802 } else {
3803 double double_val;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003804
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003805 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 +02003806 return 0;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003807
3808 trash->data = snprintf(trash->area,trash->size,"%g",double_val);
3809 smp->data.u.str = *trash;
3810 smp->data.type = SMP_T_STR;
3811
3812 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003813 }
Alex51c8ad42021-04-15 16:45:15 +02003814 case MJSON_TOK_TRUE:
3815 smp->data.type = SMP_T_BOOL;
3816 smp->data.u.sint = 1;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003817
3818 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003819 case MJSON_TOK_FALSE:
3820 smp->data.type = SMP_T_BOOL;
3821 smp->data.u.sint = 0;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003822
3823 return 1;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003824 case MJSON_TOK_STRING: {
3825 int len;
3826
3827 len = mjson_get_string(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, trash->area, trash->size);
3828
3829 if (len == -1) {
Alex51c8ad42021-04-15 16:45:15 +02003830 /* invalid string */
3831 return 0;
Alex51c8ad42021-04-15 16:45:15 +02003832 }
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003833
3834 trash->data = len;
3835 smp->data.u.str = *trash;
3836 smp->data.type = SMP_T_STR;
3837
3838 return 1;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003839 }
Tim Duesterhus8f3bc8f2021-04-15 18:14:32 +02003840 case MJSON_TOK_NULL:
3841 case MJSON_TOK_ARRAY:
3842 case MJSON_TOK_OBJECT:
3843 /* We cannot handle these. */
3844 return 0;
3845 case MJSON_TOK_INVALID:
3846 /* Nothing matches the query. */
3847 return 0;
3848 case MJSON_TOK_KEY:
3849 /* This is not a valid return value according to the
3850 * mjson documentation, but we handle it to benefit
3851 * from '-Wswitch'.
3852 */
Alex51c8ad42021-04-15 16:45:15 +02003853 return 0;
3854 }
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003855
3856 my_unreachable();
3857 return 0;
Alex51c8ad42021-04-15 16:45:15 +02003858}
3859
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003860#ifdef USE_OPENSSL
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +02003861static int sample_conv_jwt_verify_check(struct arg *args, struct sample_conv *conv,
3862 const char *file, int line, char **err)
3863{
3864 vars_check_arg(&args[0], NULL);
3865 vars_check_arg(&args[1], NULL);
3866
3867 if (args[0].type == ARGT_STR) {
3868 enum jwt_alg alg = jwt_parse_alg(args[0].data.str.area, args[0].data.str.data);
3869
3870 switch(alg) {
3871 case JWT_ALG_DEFAULT:
Tim Duesterhusab896ee2021-10-29 18:06:55 +02003872 memprintf(err, "unknown JWT algorithm: %s", args[0].data.str.area);
3873 return 0;
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +02003874
3875 case JWS_ALG_PS256:
3876 case JWS_ALG_PS384:
3877 case JWS_ALG_PS512:
3878 memprintf(err, "RSASSA-PSS JWS signing not managed yet");
Tim Duesterhusab896ee2021-10-29 18:06:55 +02003879 return 0;
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +02003880
3881 default:
3882 break;
3883 }
3884 }
3885
3886 if (args[1].type == ARGT_STR) {
3887 jwt_tree_load_cert(args[1].data.str.area, args[1].data.str.data, err);
3888 }
3889
3890 return 1;
3891}
3892
3893/* Check that a JWT's signature is correct */
3894static int sample_conv_jwt_verify(const struct arg *args, struct sample *smp, void *private)
3895{
3896 struct sample alg_smp, key_smp;
3897
3898 smp->data.type = SMP_T_SINT;
3899 smp->data.u.sint = 0;
3900
3901 smp_set_owner(&alg_smp, smp->px, smp->sess, smp->strm, smp->opt);
3902 smp_set_owner(&key_smp, smp->px, smp->sess, smp->strm, smp->opt);
3903 if (!sample_conv_var2smp_str(&args[0], &alg_smp))
3904 return 0;
3905 if (!sample_conv_var2smp_str(&args[1], &key_smp))
3906 return 0;
3907
3908 smp->data.u.sint = jwt_verify(&smp->data.u.str, &alg_smp.data.u.str,
3909 &key_smp.data.u.str);
3910
3911 return 1;
3912}
3913
3914
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003915/*
3916 * Returns the decoded header or payload of a JWT if no parameter is given, or
3917 * the value of the specified field of the corresponding JWT subpart if a
3918 * parameter is given.
3919 */
3920static int sample_conv_jwt_member_query(const struct arg *args, struct sample *smp,
3921 void *private, enum jwt_elt member)
3922{
3923 struct jwt_item items[JWT_ELT_MAX] = { { 0 } };
3924 unsigned int item_num = member + 1; /* We don't need to tokenize the full token */
3925 struct buffer *decoded_header = get_trash_chunk();
3926 int retval = 0;
Willy Tarreaue20e0262021-10-15 12:10:24 +02003927 int ret;
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003928
3929 jwt_tokenize(&smp->data.u.str, items, &item_num);
3930
3931 if (item_num < member + 1)
3932 goto end;
3933
Willy Tarreaue20e0262021-10-15 12:10:24 +02003934 ret = base64urldec(items[member].start, items[member].length,
3935 decoded_header->area, decoded_header->size);
3936 if (ret == -1)
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003937 goto end;
3938
Willy Tarreaue20e0262021-10-15 12:10:24 +02003939 decoded_header->data = ret;
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003940 if (args[0].type != ARGT_STR) {
3941 smp->data.u.str = *decoded_header;
3942 smp->data.type = SMP_T_STR;
3943 goto end;
3944 }
3945
3946 /* We look for a specific field of the header or payload part of the JWT */
3947 smp->data.u.str = *decoded_header;
3948
3949 retval = sample_conv_json_query(args, smp, private);
3950
3951end:
3952 return retval;
3953}
3954
3955/* This function checks the "jwt_header_query" and "jwt_payload_query" converters' arguments.
3956 * It is based on the "json_query" converter's check with the only difference
3957 * being that the jwt converters can take 0 parameters as well.
3958 */
3959static int sample_conv_jwt_query_check(struct arg *arg, struct sample_conv *conv,
3960 const char *file, int line, char **err)
3961{
3962 if (arg[1].data.str.data != 0) {
3963 if (strcmp(arg[1].data.str.area, "int") != 0) {
3964 memprintf(err, "output_type only supports \"int\" as argument");
3965 return 0;
3966 } else {
3967 arg[1].type = ARGT_SINT;
3968 arg[1].data.sint = 0;
3969 }
3970 }
3971 return 1;
3972}
3973
3974/*
3975 * If no parameter is given, return the decoded header part of a JWT (the first
3976 * base64 encoded part, corresponding to the JOSE header).
3977 * If a parameter is given, this converter acts as a "json_query" on this
3978 * decoded JSON.
3979 */
3980static int sample_conv_jwt_header_query(const struct arg *args, struct sample *smp, void *private)
3981{
3982 return sample_conv_jwt_member_query(args, smp, private, JWT_ELT_JOSE);
3983}
3984
3985/*
3986 * If no parameter is given, return the decoded payload part of a JWT (the
3987 * second base64 encoded part, which contains all the claims). If a parameter
3988 * is given, this converter acts as a "json_query" on this decoded JSON.
3989 */
3990static int sample_conv_jwt_payload_query(const struct arg *args, struct sample *smp, void *private)
3991{
3992 return sample_conv_jwt_member_query(args, smp, private, JWT_ELT_CLAIMS);
3993}
3994
3995#endif /* USE_OPENSSL */
Alex51c8ad42021-04-15 16:45:15 +02003996
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003997/************************************************************************/
3998/* All supported sample fetch functions must be declared here */
3999/************************************************************************/
4000
4001/* force TRUE to be returned at the fetch level */
4002static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004003smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004004{
Willy Tarreau280f42b2018-02-19 15:34:12 +01004005 if (!smp_make_rw(smp))
4006 return 0;
4007
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004008 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004009 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004010 return 1;
4011}
4012
4013/* force FALSE to be returned at the fetch level */
4014static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004015smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004016{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004017 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004018 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004019 return 1;
4020}
4021
4022/* retrieve environment variable $1 as a string */
4023static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004024smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004025{
4026 char *env;
4027
Christopher Faulete6e7a582021-01-29 11:29:28 +01004028 if (args[0].type != ARGT_STR)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004029 return 0;
4030
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004031 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004032 if (!env)
4033 return 0;
4034
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004035 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01004036 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004037 smp->data.u.str.area = env;
4038 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004039 return 1;
4040}
4041
Damien Claisseae6f1252019-10-30 15:57:28 +00004042/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
4043 * optional string representing the unit of the result: "s" for seconds, "ms" for
4044 * milliseconds and "us" for microseconds.
4045 * Returns 0 on error and non-zero if OK.
4046 */
4047int smp_check_date_unit(struct arg *args, char **err)
4048{
4049 if (args[1].type == ARGT_STR) {
Christopher Faulet95917132020-08-05 23:07:07 +02004050 long long int unit;
4051
Damien Claisseae6f1252019-10-30 15:57:28 +00004052 if (strcmp(args[1].data.str.area, "s") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02004053 unit = TIME_UNIT_S;
Damien Claisseae6f1252019-10-30 15:57:28 +00004054 }
4055 else if (strcmp(args[1].data.str.area, "ms") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02004056 unit = TIME_UNIT_MS;
Damien Claisseae6f1252019-10-30 15:57:28 +00004057 }
4058 else if (strcmp(args[1].data.str.area, "us") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02004059 unit = TIME_UNIT_US;
Damien Claisseae6f1252019-10-30 15:57:28 +00004060 }
4061 else {
4062 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
4063 args[1].data.str.area);
4064 return 0;
4065 }
Christopher Faulet95917132020-08-05 23:07:07 +02004066
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004067 chunk_destroy(&args[1].data.str);
Damien Claisseae6f1252019-10-30 15:57:28 +00004068 args[1].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02004069 args[1].data.sint = unit;
Damien Claisseae6f1252019-10-30 15:57:28 +00004070 }
4071 else if (args[1].type != ARGT_STOP) {
4072 memprintf(err, "Unexpected arg type");
4073 return 0;
4074 }
4075
4076 return 1;
4077}
4078
4079/* retrieve the current local date in epoch time, converts it to milliseconds
4080 * or microseconds if asked to in optional args[1] unit param, and applies an
4081 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004082 */
4083static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004084smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004085{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004086 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004087
Damien Claisseae6f1252019-10-30 15:57:28 +00004088 /* report in milliseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01004089 if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
Damien Claisseae6f1252019-10-30 15:57:28 +00004090 smp->data.u.sint *= 1000;
4091 smp->data.u.sint += date.tv_usec / 1000;
4092 }
4093 /* report in microseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01004094 else if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
Damien Claisseae6f1252019-10-30 15:57:28 +00004095 smp->data.u.sint *= 1000000;
4096 smp->data.u.sint += date.tv_usec;
4097 }
4098
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004099 /* add offset */
Christopher Faulete6e7a582021-01-29 11:29:28 +01004100 if (args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004101 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004102
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004103 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004104 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4105 return 1;
4106}
4107
Etienne Carrierea792a0a2018-01-17 13:43:24 +01004108/* retrieve the current microsecond part of the date */
4109static int
4110smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
4111{
4112 smp->data.u.sint = date.tv_usec;
4113 smp->data.type = SMP_T_SINT;
4114 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4115 return 1;
4116}
4117
4118
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01004119/* returns the hostname */
4120static int
4121smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
4122{
4123 smp->data.type = SMP_T_STR;
4124 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004125 smp->data.u.str.area = hostname;
4126 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01004127 return 1;
4128}
4129
Willy Tarreau0f30d262014-11-24 16:02:05 +01004130/* returns the number of processes */
4131static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004132smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01004133{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004134 smp->data.type = SMP_T_SINT;
Willy Tarreau91358592021-06-15 08:08:04 +02004135 smp->data.u.sint = 1;
Willy Tarreau0f30d262014-11-24 16:02:05 +01004136 return 1;
4137}
4138
4139/* returns the number of the current process (between 1 and nbproc */
4140static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004141smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01004142{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004143 smp->data.type = SMP_T_SINT;
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004144 smp->data.u.sint = 1;
Willy Tarreau0f30d262014-11-24 16:02:05 +01004145 return 1;
4146}
4147
Christopher Faulet34adb2a2017-11-21 21:45:38 +01004148/* returns the number of the current thread (between 1 and nbthread */
4149static int
4150smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
4151{
4152 smp->data.type = SMP_T_SINT;
4153 smp->data.u.sint = tid;
4154 return 1;
4155}
4156
Willy Tarreau84310e22014-02-14 11:59:04 +01004157/* generate a random 32-bit integer for whatever purpose, with an optional
4158 * range specified in argument.
4159 */
4160static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004161smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01004162{
Willy Tarreau95f40c62022-11-24 14:54:29 +01004163 smp->data.u.sint = statistical_prng();
Willy Tarreau84310e22014-02-14 11:59:04 +01004164
4165 /* reduce if needed. Don't do a modulo, use all bits! */
Christopher Faulete6e7a582021-01-29 11:29:28 +01004166 if (args[0].type == ARGT_SINT)
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01004167 smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32;
Willy Tarreau84310e22014-02-14 11:59:04 +01004168
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004169 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01004170 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4171 return 1;
4172}
4173
Willy Tarreau0f30d262014-11-24 16:02:05 +01004174/* returns true if the current process is stopping */
4175static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004176smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01004177{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004178 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004179 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01004180 return 1;
4181}
4182
Willy Tarreau70fe9442018-11-22 16:07:39 +01004183/* returns the number of calls of the current stream's process_stream() */
4184static int
4185smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
4186{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004187 if (!smp->strm)
4188 return 0;
4189
Willy Tarreau70fe9442018-11-22 16:07:39 +01004190 smp->data.type = SMP_T_SINT;
4191 smp->data.u.sint = smp->strm->task->calls;
4192 return 1;
4193}
4194
4195/* returns the average number of nanoseconds spent processing the stream per call */
4196static int
4197smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
4198{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004199 if (!smp->strm)
4200 return 0;
4201
Willy Tarreau70fe9442018-11-22 16:07:39 +01004202 smp->data.type = SMP_T_SINT;
Willy Tarreau6a28a302022-09-07 09:17:45 +02004203 smp->data.u.sint = smp->strm->task->calls ? smp->strm->cpu_time / smp->strm->task->calls : 0;
Willy Tarreau70fe9442018-11-22 16:07:39 +01004204 return 1;
4205}
4206
4207/* returns the total number of nanoseconds spent processing the stream */
4208static int
4209smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
4210{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004211 if (!smp->strm)
4212 return 0;
4213
Willy Tarreau70fe9442018-11-22 16:07:39 +01004214 smp->data.type = SMP_T_SINT;
Willy Tarreau6a28a302022-09-07 09:17:45 +02004215 smp->data.u.sint = smp->strm->cpu_time;
Willy Tarreau70fe9442018-11-22 16:07:39 +01004216 return 1;
4217}
4218
4219/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
4220static int
4221smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
4222{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004223 if (!smp->strm)
4224 return 0;
4225
Willy Tarreau70fe9442018-11-22 16:07:39 +01004226 smp->data.type = SMP_T_SINT;
Willy Tarreau6a28a302022-09-07 09:17:45 +02004227 smp->data.u.sint = smp->strm->task->calls ? smp->strm->lat_time / smp->strm->task->calls : 0;
Willy Tarreau70fe9442018-11-22 16:07:39 +01004228 return 1;
4229}
4230
4231/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
4232static int
4233smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
4234{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004235 if (!smp->strm)
4236 return 0;
4237
Willy Tarreau70fe9442018-11-22 16:07:39 +01004238 smp->data.type = SMP_T_SINT;
Willy Tarreau6a28a302022-09-07 09:17:45 +02004239 smp->data.u.sint = smp->strm->lat_time;
Willy Tarreau70fe9442018-11-22 16:07:39 +01004240 return 1;
4241}
4242
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004243static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
4244{
4245 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004246 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004247 smp->data.u.str.area = args[0].data.str.area;
4248 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004249 return 1;
4250}
4251
4252static int smp_check_const_bool(struct arg *args, char **err)
4253{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004254 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
4255 strcasecmp(args[0].data.str.area, "1") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004256 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004257 args[0].type = ARGT_SINT;
4258 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004259 return 1;
4260 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004261 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
4262 strcasecmp(args[0].data.str.area, "0") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004263 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004264 args[0].type = ARGT_SINT;
4265 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004266 return 1;
4267 }
4268 memprintf(err, "Expects 'true', 'false', '0' or '1'");
4269 return 0;
4270}
4271
4272static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
4273{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004274 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004275 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004276 return 1;
4277}
4278
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02004279static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004280{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004281 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004282 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004283 return 1;
4284}
4285
4286static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
4287{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004288 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004289 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004290 return 1;
4291}
4292
4293static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
4294{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004295 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004296 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004297 return 1;
4298}
4299
4300static int smp_check_const_bin(struct arg *args, char **err)
4301{
David Carlier64a16ab2016-04-08 10:37:02 +01004302 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004303 int binstrlen;
4304
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004305 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004306 return 0;
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004307 chunk_destroy(&args[0].data.str);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004308 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004309 args[0].data.str.area = binstr;
4310 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004311 return 1;
4312}
4313
4314static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
4315{
4316 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004317 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004318 smp->data.u.str.area = args[0].data.str.area;
4319 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004320 return 1;
4321}
4322
4323static int smp_check_const_meth(struct arg *args, char **err)
4324{
4325 enum http_meth_t meth;
4326 int i;
4327
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004328 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004329 if (meth != HTTP_METH_OTHER) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004330 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004331 args[0].type = ARGT_SINT;
4332 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004333 } else {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05004334 /* Check method avalaibility. A method is a token defined as :
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004335 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
4336 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
4337 * token = 1*tchar
4338 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004339 for (i = 0; i < args[0].data.str.data; i++) {
4340 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004341 memprintf(err, "expects valid method.");
4342 return 0;
4343 }
4344 }
4345 }
4346 return 1;
4347}
4348
4349static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
4350{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004351 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004352 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004353 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004354 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004355 smp->data.u.meth.str.area = "";
4356 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004357 } else {
4358 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004359 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004360 smp->data.u.meth.str.area = args[0].data.str.area;
4361 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004362 }
4363 return 1;
4364}
4365
Luca Schimweg8a694b82019-09-10 15:42:52 +02004366// This function checks the "uuid" sample's arguments.
4367// Function won't get called when no parameter is specified (maybe a bug?)
4368static int smp_check_uuid(struct arg *args, char **err)
4369{
4370 if (!args[0].type) {
4371 args[0].type = ARGT_SINT;
4372 args[0].data.sint = 4;
4373 }
4374 else if (args[0].data.sint != 4) {
4375 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
4376 return 0;
4377 }
4378
4379 return 1;
4380}
4381
4382// Generate a RFC4122 UUID (default is v4 = fully random)
4383static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
4384{
4385 if (args[0].data.sint == 4 || !args[0].type) {
Willy Tarreauee3bcdd2020-03-08 17:48:17 +01004386 ha_generate_uuid(&trash);
Luca Schimweg8a694b82019-09-10 15:42:52 +02004387 smp->data.type = SMP_T_STR;
4388 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4389 smp->data.u.str = trash;
4390 return 1;
4391 }
4392
4393 // more implementations of other uuid formats possible here
4394 return 0;
4395}
4396
Frédéric Lécaille33d11c42023-01-12 17:55:45 +01004397/* Check if QUIC support was compiled and was not disabled by "no-quic" global option */
4398static int smp_fetch_quic_enabled(const struct arg *args, struct sample *smp, const char *kw, void *private)
4399{
4400 smp->data.type = SMP_T_BOOL;
4401 smp->flags = 0;
4402#ifdef USE_QUIC
4403 smp->data.u.sint = !(global.tune.options & GTUNE_NO_QUIC);
4404#else
4405 smp->data.u.sint = 0;
4406#endif
4407 return smp->data.u.sint;
4408}
4409
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004410/* Note: must not be declared <const> as its list will be overwritten.
4411 * Note: fetches that may return multiple types must be declared as the lowest
4412 * common denominator, the type that can be casted into all other ones. For
4413 * instance IPv4/IPv6 must be declared IPv4.
4414 */
4415static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau0209c972021-03-26 12:03:11 +01004416 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4417 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4418 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_CONST },
4419 { "date", smp_fetch_date, ARG2(0,SINT,STR), smp_check_date_unit, SMP_T_SINT, SMP_USE_CONST },
4420 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4421 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_CONST },
4422 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_CONST },
4423 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
Frédéric Lécaille33d11c42023-01-12 17:55:45 +01004424 { "quic_enabled", smp_fetch_quic_enabled, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
Willy Tarreau0209c972021-03-26 12:03:11 +01004425 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4426 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_CONST },
Willy Tarreau0f30d262014-11-24 16:02:05 +01004427 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau0209c972021-03-26 12:03:11 +01004428 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_CONST },
Willy Tarreau70fe9442018-11-22 16:07:39 +01004429
4430 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4431 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4432 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4433 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4434 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004435
Willy Tarreau0209c972021-03-26 12:03:11 +01004436 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_CONST },
4437 { "bool", smp_fetch_const_bool, ARG1(1,STR), smp_check_const_bool, SMP_T_BOOL, SMP_USE_CONST },
4438 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_CONST },
4439 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_CONST },
4440 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_CONST },
4441 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_CONST },
4442 { "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 +02004443
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004444 { /* END */ },
4445}};
4446
Willy Tarreau0108d902018-11-25 19:14:37 +01004447INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
4448
Emeric Brun107ca302010-01-04 16:16:05 +01004449/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02004450static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Nikola Sale0dbf0382022-04-03 18:11:53 +02004451 { "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 +02004452 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
4453 { "b64dec", sample_conv_base642bin, 0, NULL, SMP_T_STR, SMP_T_BIN },
4454 { "base64", sample_conv_bin2base64, 0, NULL, SMP_T_BIN, SMP_T_STR },
Nikola Sale0dbf0382022-04-03 18:11:53 +02004455 { "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 +02004456 { "ub64enc", sample_conv_bin2base64url,0, NULL, SMP_T_BIN, SMP_T_STR },
4457 { "ub64dec", sample_conv_base64url2bin,0, NULL, SMP_T_STR, SMP_T_BIN },
4458 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
4459 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
4460 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Marcin Deranek40ca09c2021-07-13 14:05:24 +02004461 { "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 +02004462 { "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 +02004463 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
4464 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
4465 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
4466 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4467 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4468 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4469 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4470 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4471 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4472 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4473 { "xxh3", sample_conv_xxh3, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4474 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4475 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4476 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
4477 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
4478 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4479 { "word", sample_conv_word, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
Thayne McCombs02cf4ec2022-12-14 00:19:59 -07004480 { "param", sample_conv_param, ARG2(1,STR,STR), sample_conv_param_check, SMP_T_STR, SMP_T_STR },
Moemen MHEDHBI848216f2021-04-02 01:05:07 +02004481 { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
4482 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Willy Tarreau99ea1882021-10-06 15:37:17 +02004483 { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT },
William Lallemanddd754cb2022-08-26 16:21:28 +02004484 { "host_only", sample_conv_host_only, 0, NULL, SMP_T_STR, SMP_T_STR },
4485 { "port_only", sample_conv_port_only, 0, NULL, SMP_T_STR, SMP_T_SINT },
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01004486
4487 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01004488 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
4489 { "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 +01004490
Baptiste Assmanne138dda2020-10-22 15:39:03 +02004491 /* FIX converters */
4492 { "fix_is_valid", sample_conv_fix_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4493 { "fix_tag_value", sample_conv_fix_tag_value, ARG1(1,STR), sample_conv_fix_value_check, SMP_T_BIN, SMP_T_BIN },
4494
Baptiste Assmanne279ca62020-10-27 18:10:06 +01004495 /* MQTT converters */
4496 { "mqtt_is_valid", sample_conv_mqtt_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4497 { "mqtt_field_value", sample_conv_mqtt_field_value, ARG2(2,STR,STR), sample_conv_mqtt_field_value_check, SMP_T_BIN, SMP_T_STR },
4498
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02004499 { "iif", sample_conv_iif, ARG2(2, STR, STR), NULL, SMP_T_BOOL, SMP_T_STR },
4500
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02004501 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4502 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4503 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4504 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
4505 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4506 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4507 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4508 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4509 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4510 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4511 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4512 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4513 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4514 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01004515
Christopher Fauletea159d62020-04-01 16:21:44 +02004516 { "htonl", sample_conv_htonl, 0, NULL, SMP_T_SINT, SMP_T_BIN },
4517 { "cut_crlf", sample_conv_cut_crlf, 0, NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet51fc9d12020-04-01 17:24:41 +02004518 { "ltrim", sample_conv_ltrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet568415a2020-04-01 17:24:47 +02004519 { "rtrim", sample_conv_rtrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Alex51c8ad42021-04-15 16:45:15 +02004520 { "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 +02004521
4522#ifdef USE_OPENSSL
4523 /* JSON Web Token converters */
4524 { "jwt_header_query", sample_conv_jwt_header_query, ARG2(0,STR,STR), sample_conv_jwt_query_check, SMP_T_BIN, SMP_T_ANY },
4525 { "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 +02004526 { "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 +02004527#endif
Willy Tarreau9fcb9842012-04-20 14:45:49 +02004528 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01004529}};
4530
Willy Tarreau0108d902018-11-25 19:14:37 +01004531INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);