blob: c4b7da97128acb6f90d5e2bc14adb3a97b286e33 [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;
441 struct sample_fetch *kw;
442 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
463 list_for_each_entry(kwl, &sample_fetches.list, list) {
464 for (index = 0; kwl->kw[index].kw != NULL; index++) {
465 kw = &kwl->kw[index];
466
467 printf("[ ");
468 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
469 printf("%s", (kw->val & (1 << bit)) ? "Y " : ". ");
470
471 printf("] %s", kw->kw);
472 if (kw->arg_mask) {
473 mask = kw->arg_mask >> ARGM_BITS;
474 printf("(");
475 for (arg = 0;
476 arg < ARGM_NBARGS && ((mask >> (arg * ARGT_BITS)) & ARGT_MASK);
477 arg++) {
478 if (arg == (kw->arg_mask & ARGM_MASK)) {
479 /* now dumping extra args */
480 printf("[");
481 }
482 if (arg)
483 printf(",");
484 printf("%s", arg_type_names[(mask >> (arg * ARGT_BITS)) & ARGT_MASK]);
485 }
486 if (arg > (kw->arg_mask & ARGM_MASK)) {
487 /* extra args were dumped */
488 printf("]");
489 }
490 printf(")");
491 }
492 printf(": %s", smp_to_type[kw->out_type]);
493 printf("\n");
494 }
495 }
496}
497
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100498/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100499 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100500 * <idx> is the index of the next sample fetch entry. It is used as private
501 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100502 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100503 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100504 * the list is reached.
505 */
506struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
507{
508 struct sample_fetch_kw_list *kwl;
509 struct sample_fetch *base;
510
511 if (!current) {
512 /* Get first kwl entry. */
513 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
514 (*idx) = 0;
515 } else {
516 /* Get kwl corresponding to the curret entry. */
517 base = current + 1 - (*idx);
518 kwl = container_of(base, struct sample_fetch_kw_list, kw);
519 }
520
521 while (1) {
522
523 /* Check if kwl is the last entry. */
524 if (&kwl->list == &sample_fetches.list)
525 return NULL;
526
527 /* idx contain the next keyword. If it is available, return it. */
528 if (kwl->kw[*idx].kw) {
529 (*idx)++;
530 return &kwl->kw[(*idx)-1];
531 }
532
533 /* get next entry in the main list, and return NULL if the end is reached. */
534 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
535
536 /* Set index to 0, ans do one other loop. */
537 (*idx) = 0;
538 }
539}
540
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100541/* This function browses the list of available converters. <current> is
542 * the last used converter. If it is the first call, it must set to NULL.
543 * <idx> is the index of the next converter entry. It is used as private
544 * value. It is useless to initiate it.
545 *
546 * It returns always the next sample_conv entry, and NULL when the end of
547 * the list is reached.
548 */
549struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
550{
551 struct sample_conv_kw_list *kwl;
552 struct sample_conv *base;
553
554 if (!current) {
555 /* Get first kwl entry. */
556 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
557 (*idx) = 0;
558 } else {
559 /* Get kwl corresponding to the curret entry. */
560 base = current + 1 - (*idx);
561 kwl = container_of(base, struct sample_conv_kw_list, kw);
562 }
563
564 while (1) {
565 /* Check if kwl is the last entry. */
566 if (&kwl->list == &sample_convs.list)
567 return NULL;
568
569 /* idx contain the next keyword. If it is available, return it. */
570 if (kwl->kw[*idx].kw) {
571 (*idx)++;
572 return &kwl->kw[(*idx)-1];
573 }
574
575 /* get next entry in the main list, and return NULL if the end is reached. */
576 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
577
578 /* Set index to 0, ans do one other loop. */
579 (*idx) = 0;
580 }
581}
582
Emeric Brun107ca302010-01-04 16:16:05 +0100583/*
Willy Tarreau12785782012-04-27 21:37:17 +0200584 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100585 * string of <len> in buffer <kw>.
586 *
587 */
Willy Tarreau12785782012-04-27 21:37:17 +0200588struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100589{
590 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200591 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100592
Willy Tarreau12785782012-04-27 21:37:17 +0200593 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100594 for (index = 0; kwl->kw[index].kw != NULL; index++) {
595 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
596 kwl->kw[index].kw[len] == '\0')
597 return &kwl->kw[index];
598 }
599 }
600 return NULL;
601}
602
Emeric Brun107ca302010-01-04 16:16:05 +0100603/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200604/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100605/******************************************************************/
606
Willy Tarreau342acb42012-04-23 22:03:39 +0200607static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100608{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200609 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200610 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100611 return 1;
612}
613
Willy Tarreau342acb42012-04-23 22:03:39 +0200614static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100615{
Willy Tarreau83061a82018-07-13 11:56:34 +0200616 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100617
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200618 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100619 return 0;
620
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200621 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200622 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200623 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100624 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100625
626 return 1;
627}
628
Willy Tarreau342acb42012-04-23 22:03:39 +0200629static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100630{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200631 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200632 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100633 return 1;
634}
635
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200636static int c_ipv62ip(struct sample *smp)
637{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200638 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200639 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100640 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200641 return 1;
642}
643
Willy Tarreau342acb42012-04-23 22:03:39 +0200644static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100645{
Willy Tarreau83061a82018-07-13 11:56:34 +0200646 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100647
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200648 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100649 return 0;
650
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200651 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200652 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200653 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100654 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100655 return 1;
656}
657
658/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200659static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100660{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200661 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100662}
663*/
664
Willy Tarreau342acb42012-04-23 22:03:39 +0200665static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100666{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200667 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200668 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100669 return 1;
670}
671
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200672static int c_int2ipv6(struct sample *smp)
673{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200674 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
675 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200676 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200677 return 1;
678}
679
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100680static int c_str2addr(struct sample *smp)
681{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200682 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
683 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100684 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200685 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100686 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100687 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100688 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200689 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100690 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100691 return 1;
692}
693
Willy Tarreau342acb42012-04-23 22:03:39 +0200694static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100695{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200696 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100697 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200698 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100699 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100700 return 1;
701}
702
Willy Tarreau342acb42012-04-23 22:03:39 +0200703static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100704{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200705 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100706 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200707 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100708 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100709 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100710}
711
Emeric Brun4b9e8022014-11-03 18:17:10 +0100712/*
713 * The NULL char always enforces the end of string if it is met.
714 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100715 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200716static int c_bin2str(struct sample *smp)
717{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100718 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200719
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200720 for (i = 0; i < smp->data.u.str.data; i++) {
721 if (!smp->data.u.str.area[i]) {
722 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100723 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100724 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200725 }
Christopher Faulet472ad512020-04-30 09:57:40 +0200726 smp->data.type = SMP_T_STR;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200727 return 1;
728}
729
Willy Tarreau342acb42012-04-23 22:03:39 +0200730static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100731{
Willy Tarreau83061a82018-07-13 11:56:34 +0200732 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100733 char *pos;
734
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200735 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100736 if (!pos)
737 return 0;
738
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200739 trash->size = trash->size - (pos - trash->area);
740 trash->area = pos;
741 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200742 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200743 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100744 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100745 return 1;
746}
747
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800748/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200749 * For strings and binary blocks, it also provides a known allocated size with
750 * a length that is capped to the size, and ensures a trailing zero is always
751 * appended for strings. This is necessary for some operations which may
752 * require to extend the length. It returns 0 if it fails, 1 on success.
753 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100754int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200755{
Willy Tarreau83061a82018-07-13 11:56:34 +0200756 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200757
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200758 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100759 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100760 case SMP_T_SINT:
761 case SMP_T_ADDR:
762 case SMP_T_IPV4:
763 case SMP_T_IPV6:
764 /* These type are not const. */
765 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200766
Christopher Fauletec100512017-07-24 15:38:41 +0200767 case SMP_T_METH:
768 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
769 break;
770 /* Fall through */
771
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100772 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200773 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100774 trash->data = smp->data.type == SMP_T_STR ?
775 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200776 if (trash->data > trash->size - 1)
777 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200778
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100779 memcpy(trash->area, smp->data.type == SMP_T_STR ?
780 smp->data.u.str.area : smp->data.u.meth.str.area,
781 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200782 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200783 smp->data.u.str = *trash;
784 break;
785
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100786 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100787 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200788 trash->data = smp->data.u.str.data;
789 if (trash->data > trash->size)
790 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200791
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200792 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200793 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100794 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200795
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100796 default:
797 /* Other cases are unexpected. */
798 return 0;
799 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100800
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100801 /* remove const flag */
802 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200803 return 1;
804}
805
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100806int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100807{
808 return 1;
809}
810
Willy Tarreau342acb42012-04-23 22:03:39 +0200811static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100812{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200813 const char *str;
814 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100815
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200816 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100817 return 0;
818
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200819 str = smp->data.u.str.area;
820 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100821
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200822 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200823 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100824 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100825 return 1;
826}
827
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100828static int c_str2meth(struct sample *smp)
829{
830 enum http_meth_t meth;
831 int len;
832
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200833 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100834 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200835 len = smp->data.u.str.data;
836 smp->data.u.meth.str.area = smp->data.u.str.area;
837 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100838 }
839 else
840 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200841 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200842 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100843 return 1;
844}
845
846static int c_meth2str(struct sample *smp)
847{
848 int len;
849 enum http_meth_t meth;
850
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200851 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100852 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200853 len = smp->data.u.meth.str.data;
854 smp->data.u.str.area = smp->data.u.meth.str.area;
855 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200856 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100857 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200858 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100859 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200860 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200861 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200862 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100863 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200864 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100865 }
866 else {
867 /* Unknown method */
868 return 0;
869 }
870 return 1;
871}
872
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200873static int c_addr2bin(struct sample *smp)
874{
Willy Tarreau83061a82018-07-13 11:56:34 +0200875 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200876
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200877 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200878 chk->data = 4;
879 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200880 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200881 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200882 chk->data = 16;
883 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200884 }
885 else
886 return 0;
887
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200888 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200889 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200890 return 1;
891}
892
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200893static int c_int2bin(struct sample *smp)
894{
Willy Tarreau83061a82018-07-13 11:56:34 +0200895 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200896
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200897 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
898 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200899
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200900 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200901 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200902 return 1;
903}
904
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200905
Emeric Brun107ca302010-01-04 16:16:05 +0100906/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200907/* Sample casts matrix: */
908/* sample_casts[from type][to type] */
909/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100910/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100911
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100912sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200913/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
914/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
915/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200916/* 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 +0200917/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
918/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200919/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200920/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
921/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
922/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200923};
Emeric Brun107ca302010-01-04 16:16:05 +0100924
Emeric Brun107ca302010-01-04 16:16:05 +0100925/*
Willy Tarreau12785782012-04-27 21:37:17 +0200926 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100927 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200928 * Returns a pointer on allocated sample expression structure.
Christopher Faulet35926a12021-09-30 08:48:56 +0200929 * <al> is an arg_list serving as a list head to report missing dependencies.
930 * It may be NULL if such dependencies are not allowed. Otherwise, the caller
931 * must have set al->ctx if al is set.
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100932 * If <endptr> is non-nul, it will be set to the first unparsed character
933 * (which may be the final '\0') on success. If it is nul, the expression
934 * must be properly terminated by a '\0' otherwise an error is reported.
Emeric Brun107ca302010-01-04 16:16:05 +0100935 */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100936struct 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 +0100937{
Willy Tarreau833cc792013-07-24 15:34:19 +0200938 const char *begw; /* beginning of word */
939 const char *endw; /* end of word */
940 const char *endt; /* end of term */
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +0100941 struct sample_expr *expr = NULL;
Willy Tarreau12785782012-04-27 21:37:17 +0200942 struct sample_fetch *fetch;
943 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100944 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200945 char *fkw = NULL;
946 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100947 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100948
Willy Tarreau833cc792013-07-24 15:34:19 +0200949 begw = str[*idx];
Willy Tarreaued2c6622020-02-14 18:27:10 +0100950 for (endw = begw; is_idchar(*endw); endw++)
951 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200952
953 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100954 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100955 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200956 }
Emeric Brun107ca302010-01-04 16:16:05 +0100957
Willy Tarreau833cc792013-07-24 15:34:19 +0200958 /* keep a copy of the current fetch keyword for error reporting */
959 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100960
Willy Tarreau833cc792013-07-24 15:34:19 +0200961 fetch = find_sample_fetch(begw, endw - begw);
962 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100963 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100964 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200965 }
Emeric Brun107ca302010-01-04 16:16:05 +0100966
Willy Tarreau833cc792013-07-24 15:34:19 +0200967 /* At this point, we have :
968 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100969 * - endw : end of the keyword, first character not part of keyword
Willy Tarreau833cc792013-07-24 15:34:19 +0200970 */
971
972 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100973 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100974 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200975 }
Emeric Brun107ca302010-01-04 16:16:05 +0100976 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200977
Vincent Bernat02779b62016-04-03 13:48:43 +0200978 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200979 if (!expr)
980 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100981
982 LIST_INIT(&(expr->conv_exprs));
983 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200984 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100985
Willy Tarreau689a1df2013-12-13 00:40:11 +0100986 /* Note that we call the argument parser even with an empty string,
987 * this allows it to automatically create entries for mandatory
988 * implicit arguments (eg: local proxy name).
989 */
Christopher Faulet35926a12021-09-30 08:48:56 +0200990 if (al) {
991 al->kw = expr->fetch->kw;
992 al->conv = NULL;
993 }
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100994 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 +0100995 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
996 goto out_error;
997 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200998
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100999 /* now endt is our first char not part of the arg list, typically the
1000 * comma after the sample fetch name or after the closing parenthesis,
1001 * or the NUL char.
1002 */
1003
Willy Tarreau689a1df2013-12-13 00:40:11 +01001004 if (!expr->arg_p) {
1005 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +02001006 }
Willy Tarreau689a1df2013-12-13 00:40:11 +01001007 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
1008 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001009 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001010 }
1011
Willy Tarreau833cc792013-07-24 15:34:19 +02001012 /* Now process the converters if any. We have two supported syntaxes
1013 * for the converters, which can be combined :
1014 * - comma-delimited list of converters just after the keyword and args ;
1015 * - one converter per keyword
1016 * The combination allows to have each keyword being a comma-delimited
1017 * series of converters.
1018 *
1019 * We want to process the former first, then the latter. For this we start
1020 * from the beginning of the supposed place in the exiting conv chain, which
1021 * starts at the last comma (endt).
1022 */
1023
1024 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +02001025 struct sample_conv_expr *conv_expr;
Willy Tarreau46dfd782019-12-17 10:25:29 +01001026 int err_arg;
1027 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +01001028
Willy Tarreau833cc792013-07-24 15:34:19 +02001029 if (*endt && *endt != ',') {
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001030 if (endptr) {
1031 /* end found, let's stop here */
1032 break;
1033 }
Willy Tarreau833cc792013-07-24 15:34:19 +02001034 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +01001035 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +02001036 else
Willy Tarreau975c1782013-12-12 23:16:54 +01001037 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +01001038 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001039 }
Emeric Brun107ca302010-01-04 16:16:05 +01001040
Willy Tarreau80b53ff2020-02-14 08:40:37 +01001041 /* FIXME: how long should we support such idiocies ? Maybe we
1042 * should already warn ?
1043 */
Willy Tarreau833cc792013-07-24 15:34:19 +02001044 while (*endt == ',') /* then trailing commas */
1045 endt++;
1046
Willy Tarreau97108e02016-11-25 07:33:24 +01001047 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +02001048
1049 if (!*begw) {
1050 /* none ? skip to next string */
1051 (*idx)++;
1052 begw = str[*idx];
1053 if (!begw || !*begw)
1054 break;
1055 }
1056
Willy Tarreaued2c6622020-02-14 18:27:10 +01001057 for (endw = begw; is_idchar(*endw); endw++)
1058 ;
Willy Tarreau833cc792013-07-24 15:34:19 +02001059
1060 free(ckw);
1061 ckw = my_strndup(begw, endw - begw);
1062
1063 conv = find_sample_conv(begw, endw - begw);
1064 if (!conv) {
1065 /* we found an isolated keyword that we don't know, it's not ours */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001066 if (begw == str[*idx]) {
1067 endt = begw;
Willy Tarreau833cc792013-07-24 15:34:19 +02001068 break;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001069 }
Willy Tarreau97108e02016-11-25 07:33:24 +01001070 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +02001071 goto out_error;
1072 }
Emeric Brun107ca302010-01-04 16:16:05 +01001073
Willy Tarreau833cc792013-07-24 15:34:19 +02001074 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001075 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001076 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001077 }
Emeric Brun107ca302010-01-04 16:16:05 +01001078
1079 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +02001080 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001081 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001082 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001083 }
Emeric Brun107ca302010-01-04 16:16:05 +01001084
1085 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +02001086 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +02001087 if (!conv_expr)
1088 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001089
Willy Tarreau2b718102021-04-21 07:32:39 +02001090 LIST_APPEND(&(expr->conv_exprs), &(conv_expr->list));
Emeric Brun107ca302010-01-04 16:16:05 +01001091 conv_expr->conv = conv;
1092
Christopher Faulet35926a12021-09-30 08:48:56 +02001093 if (al) {
1094 al->kw = expr->fetch->kw;
1095 al->conv = conv_expr->conv->kw;
1096 }
Willy Tarreau80b53ff2020-02-14 08:40:37 +01001097 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 +01001098 if (argcnt < 0) {
1099 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
1100 goto out_error;
1101 }
Willy Tarreau9e92d322010-01-26 17:58:06 +01001102
Willy Tarreau46dfd782019-12-17 10:25:29 +01001103 if (argcnt && !conv->arg_mask) {
1104 memprintf(err_msg, "converter '%s' does not support any args", ckw);
1105 goto out_error;
1106 }
Willy Tarreau21d68a62012-04-20 15:52:36 +02001107
Willy Tarreau46dfd782019-12-17 10:25:29 +01001108 if (!conv_expr->arg_p)
1109 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001110
Willy Tarreau46dfd782019-12-17 10:25:29 +01001111 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1112 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001113 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001114 }
1115 }
Emeric Brun485479d2010-09-23 18:02:19 +02001116
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001117 if (endptr) {
1118 /* end found, let's stop here */
1119 *endptr = (char *)endt;
1120 }
1121
Willy Tarreau833cc792013-07-24 15:34:19 +02001122 out:
1123 free(fkw);
1124 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001125 return expr;
1126
1127out_error:
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +01001128 release_sample_expr(expr);
Willy Tarreau833cc792013-07-24 15:34:19 +02001129 expr = NULL;
1130 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001131}
1132
1133/*
Willy Tarreau12785782012-04-27 21:37:17 +02001134 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001135 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001136 * Returns a pointer on a typed sample structure containing the result or NULL if
1137 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001138 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001139 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001140 *
1141 * Note: the fetch functions are required to properly set the return type. The
1142 * conversion functions must do so too. However the cast functions do not need
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001143 * to since they're made to cast multiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001144 *
1145 * The caller may indicate in <opt> if it considers the result final or not.
1146 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1147 * if the result is stable or not, according to the following table :
1148 *
1149 * return MAY_CHANGE FINAL Meaning for the sample
1150 * NULL 0 * Not present and will never be (eg: header)
1151 * NULL 1 0 Not present yet, could change (eg: POST param)
1152 * NULL 1 1 Not present yet, will not change anymore
1153 * smp 0 * Present and will not change (eg: header)
1154 * smp 1 0 Present, may change (eg: request length)
1155 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001156 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001157struct sample *sample_process(struct proxy *px, struct session *sess,
1158 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001159 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001160{
Willy Tarreau12785782012-04-27 21:37:17 +02001161 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001162
Willy Tarreau18387e22013-07-25 12:02:38 +02001163 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001164 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001165 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001166 }
Emeric Brun107ca302010-01-04 16:16:05 +01001167
Willy Tarreau1777ea62016-03-10 16:15:46 +01001168 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001169 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001170 return NULL;
1171
Emeric Brun107ca302010-01-04 16:16:05 +01001172 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001173 /* we want to ensure that p->type can be casted into
1174 * conv_expr->conv->in_type. We have 3 possibilities :
1175 * - NULL => not castable.
1176 * - c_none => nothing to do (let's optimize it)
1177 * - other => apply cast and prepare to fail
1178 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001179 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001180 return NULL;
1181
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001182 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1183 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001184 return NULL;
1185
Willy Tarreau12e50112012-04-25 17:21:49 +02001186 /* OK cast succeeded */
1187
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001188 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001189 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001190 }
1191 return p;
1192}
1193
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001194/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001195 * Resolve all remaining arguments in proxy <p>. Returns the number of
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001196 * errors or 0 if everything is fine. If at least one error is met, it will
1197 * be appended to *err. If *err==NULL it will be allocated first.
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001198 */
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001199int smp_resolve_args(struct proxy *p, char **err)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001200{
1201 struct arg_list *cur, *bak;
1202 const char *ctx, *where;
1203 const char *conv_ctx, *conv_pre, *conv_pos;
1204 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001205 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001206 struct arg *arg;
1207 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001208 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001209
1210 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1211 struct proxy *px;
1212 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001213 struct stktable *t;
1214 char *pname, *sname, *stktname;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001215 char *err2;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001216
1217 arg = cur->arg;
1218
1219 /* prepare output messages */
1220 conv_pre = conv_pos = conv_ctx = "";
1221 if (cur->conv) {
1222 conv_ctx = cur->conv;
1223 conv_pre = "conversion keyword '";
1224 conv_pos = "' for ";
1225 }
1226
1227 where = "in";
1228 ctx = "sample fetch keyword";
1229 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001230 case ARGC_STK: where = "in stick rule in"; break;
1231 case ARGC_TRK: where = "in tracking rule in"; break;
1232 case ARGC_LOG: where = "in log-format string in"; break;
1233 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
Willy Tarreau57467b82021-09-02 19:43:20 +02001234 case ARGC_HRQ: where = "in http-request expression in"; break;
1235 case ARGC_HRS: where = "in http-response response in"; break;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001236 case ARGC_UIF: where = "in unique-id-format string in"; break;
1237 case ARGC_RDR: where = "in redirect format string in"; break;
1238 case ARGC_CAP: where = "in capture rule in"; break;
1239 case ARGC_ACL: ctx = "ACL keyword"; break;
1240 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001241 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001242 case ARGC_HERR: where = "in http-error directive in"; break;
Miroslav Zagorac7f8314c2020-12-09 16:31:48 +01001243 case ARGC_OT: where = "in ot-scope directive in"; break;
Willy Tarreau57467b82021-09-02 19:43:20 +02001244 case ARGC_TCO: where = "in tcp-request connection expression in"; break;
1245 case ARGC_TSE: where = "in tcp-request session expression in"; break;
1246 case ARGC_TRQ: where = "in tcp-request content expression in"; break;
1247 case ARGC_TRS: where = "in tcp-response content expression in"; break;
1248 case ARGC_TCK: where = "in tcp-check expression in"; break;
1249 case ARGC_CFG: where = "in configuration expression in"; break;
1250 case ARGC_CLI: where = "in CLI expression in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001251 }
1252
1253 /* set a few default settings */
1254 px = p;
1255 pname = p->id;
1256
1257 switch (arg->type) {
1258 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001259 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001260 memprintf(err, "%sparsing [%s:%d]: missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1261 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001262 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001263 cfgerr++;
1264 continue;
1265 }
1266
1267 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001268 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001269
1270 if (sname) {
1271 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001272 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001273
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001274 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001275 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001276 memprintf(err, "%sparsing [%s:%d]: unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1277 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001278 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001279 cfgerr++;
1280 break;
1281 }
1282 }
Christopher Faulet37a9e212021-10-12 18:48:05 +02001283 else {
1284 if (px->cap & PR_CAP_DEF) {
1285 memprintf(err, "%sparsing [%s:%d]: backend name must be set in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1286 *err ? *err : "", cur->file, cur->line,
1287 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1288 cfgerr++;
1289 break;
1290 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001291 sname = arg->data.str.area;
Christopher Faulet37a9e212021-10-12 18:48:05 +02001292 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001293
1294 srv = findserver(px, sname);
1295 if (!srv) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001296 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",
1297 *err ? *err : "", cur->file, cur->line, sname, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001298 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001299 cfgerr++;
1300 break;
1301 }
1302
Amaury Denoyelle06269612021-08-23 14:05:07 +02001303 srv->flags |= SRV_F_NON_PURGEABLE;
1304
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001305 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001306 arg->unresolved = 0;
1307 arg->data.srv = srv;
1308 break;
1309
1310 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001311 if (arg->data.str.data) {
1312 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001313 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001314 }
1315
1316 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001317 memprintf(err, "%sparsing [%s:%d]: unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1318 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001319 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001320 cfgerr++;
1321 break;
1322 }
1323
1324 if (!(px->cap & PR_CAP_FE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001325 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",
1326 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001327 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001328 cfgerr++;
1329 break;
1330 }
1331
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001332 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001333 arg->unresolved = 0;
1334 arg->data.prx = px;
1335 break;
1336
1337 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001338 if (arg->data.str.data) {
1339 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001340 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001341 }
1342
1343 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001344 memprintf(err, "%sparsing [%s:%d]: unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1345 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001346 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001347 cfgerr++;
1348 break;
1349 }
1350
1351 if (!(px->cap & PR_CAP_BE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001352 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",
1353 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001354 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001355 cfgerr++;
1356 break;
1357 }
1358
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001359 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001360 arg->unresolved = 0;
1361 arg->data.prx = px;
1362 break;
1363
1364 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001365 if (arg->data.str.data)
1366 stktname = arg->data.str.area;
Christopher Faulet37a9e212021-10-12 18:48:05 +02001367 else {
1368 if (px->cap & PR_CAP_DEF) {
1369 memprintf(err, "%sparsing [%s:%d]: table name must be set in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1370 *err ? *err : "", cur->file, cur->line,
1371 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1372 cfgerr++;
1373 break;
1374 }
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001375 stktname = px->id;
Christopher Faulet37a9e212021-10-12 18:48:05 +02001376 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001377
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001378 t = stktable_find_by_name(stktname);
1379 if (!t) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001380 memprintf(err, "%sparsing [%s:%d]: unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1381 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001382 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001383 cfgerr++;
1384 break;
1385 }
1386
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001387 if (!t->size) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001388 memprintf(err, "%sparsing [%s:%d]: no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1389 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001390 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001391 cfgerr++;
1392 break;
1393 }
1394
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001395 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001396 p->next_stkt_ref = t->proxies_list;
1397 t->proxies_list = p;
1398 }
1399
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001400 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001401 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001402 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001403 break;
1404
1405 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001406 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001407 memprintf(err, "%sparsing [%s:%d]: missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1408 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001409 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001410 cfgerr++;
1411 break;
1412 }
1413
1414 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001415 strcmp(p->uri_auth->userlist->name, arg->data.str.area) == 0)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001416 ul = p->uri_auth->userlist;
1417 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001418 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001419
1420 if (!ul) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001421 memprintf(err, "%sparsing [%s:%d]: unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1422 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001423 arg->data.str.area,
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.usr = ul;
1432 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001433
1434 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001435 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001436 memprintf(err, "%sparsing [%s:%d]: missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1437 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001438 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001439 cfgerr++;
1440 continue;
1441 }
1442
Willy Tarreau46947782015-01-19 19:00:58 +01001443 rflags = 0;
1444 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001445 err2 = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001446
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001447 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err2))) {
1448 memprintf(err, "%sparsing [%s:%d]: error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1449 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001450 arg->data.str.area,
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001451 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err2);
Willy Tarreau46947782015-01-19 19:00:58 +01001452 cfgerr++;
1453 continue;
1454 }
1455
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001456 chunk_destroy(&arg->data.str);
Willy Tarreau46947782015-01-19 19:00:58 +01001457 arg->unresolved = 0;
1458 arg->data.reg = reg;
1459 break;
1460
1461
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001462 }
1463
Willy Tarreau2b718102021-04-21 07:32:39 +02001464 LIST_DELETE(&cur->list);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001465 free(cur);
1466 } /* end of args processing */
1467
1468 return cfgerr;
1469}
1470
1471/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001472 * Process a fetch + format conversion as defined by the sample expression
1473 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001474 * not explicitly set to <smp_type>, but shall be compatible with it as
1475 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1476 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001477 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1478 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1479 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1480 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001481 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1482 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1483 * still hope to get it after waiting longer, and is not converted to string.
1484 * The possible output combinations are the following :
1485 *
1486 * return MAY_CHANGE FINAL Meaning for the sample
1487 * NULL * * Not present and will never be (eg: header)
1488 * smp 0 * Final value converted (eg: header)
1489 * smp 1 0 Not present yet, may appear later (eg: header)
1490 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001491 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001492struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001493 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001494 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001495{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001496 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001497
Willy Tarreau6c616e02014-06-25 16:56:41 +02001498 memset(smp, 0, sizeof(*smp));
1499
Willy Tarreau192252e2015-04-04 01:47:55 +02001500 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001501 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1502 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001503 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001504 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001505
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001506 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001507 return NULL;
1508
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001509 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001510 return NULL;
1511
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001512 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001513 return smp;
1514}
1515
Christopher Faulet476e5d02016-10-26 11:34:47 +02001516static void release_sample_arg(struct arg *p)
1517{
1518 struct arg *p_back = p;
1519
1520 if (!p)
1521 return;
1522
1523 while (p->type != ARGT_STOP) {
1524 if (p->type == ARGT_STR || p->unresolved) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001525 chunk_destroy(&p->data.str);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001526 p->unresolved = 0;
1527 }
1528 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001529 regex_free(p->data.reg);
1530 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001531 }
1532 p++;
1533 }
1534
1535 if (p_back != empty_arg_list)
1536 free(p_back);
1537}
1538
1539void release_sample_expr(struct sample_expr *expr)
1540{
1541 struct sample_conv_expr *conv_expr, *conv_exprb;
1542
1543 if (!expr)
1544 return;
1545
Tim Duesterhus867cd982020-07-04 11:49:39 +02001546 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001547 LIST_DELETE(&conv_expr->list);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001548 release_sample_arg(conv_expr->arg_p);
Tim Duesterhus867cd982020-07-04 11:49:39 +02001549 free(conv_expr);
1550 }
1551
Christopher Faulet476e5d02016-10-26 11:34:47 +02001552 release_sample_arg(expr->arg_p);
1553 free(expr);
1554}
1555
Emeric Brun107ca302010-01-04 16:16:05 +01001556/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001557/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001558/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001559/*****************************************************************/
1560
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001561static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1562{
1563 int i;
1564 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001565 struct buffer *buf;
1566 struct sink *sink;
1567 struct ist line;
1568 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001569
Willy Tarreau0851fd52019-12-17 10:07:25 +01001570 buf = alloc_trash_chunk();
1571 if (!buf)
1572 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001573
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001574 sink = (struct sink *)arg_p[1].data.ptr;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001575 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001576
Willy Tarreau0851fd52019-12-17 10:07:25 +01001577 pfx = arg_p[0].data.str.area;
1578 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001579
Willy Tarreau0851fd52019-12-17 10:07:25 +01001580 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1581 if (!sample_casts[smp->data.type][SMP_T_STR])
1582 goto nocast;
1583
1584 /* Copy sample fetch. This puts the sample as const, the
1585 * cast will copy data if a transformation is required.
1586 */
1587 memcpy(&tmp, smp, sizeof(struct sample));
1588 tmp.flags = SMP_F_CONST;
1589
1590 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1591 goto nocast;
1592
1593 /* Display the displayable chars*. */
1594 b_putchr(buf, '<');
1595 for (i = 0; i < tmp.data.u.str.data; i++) {
Willy Tarreau90807112020-02-25 08:16:33 +01001596 if (isprint((unsigned char)tmp.data.u.str.area[i]))
Willy Tarreau0851fd52019-12-17 10:07:25 +01001597 b_putchr(buf, tmp.data.u.str.area[i]);
1598 else
1599 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001600 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001601 b_putchr(buf, '>');
1602
1603 done:
1604 line = ist2(buf->area, buf->data);
Emeric Brun54648852020-07-06 15:54:06 +02001605 sink_write(sink, &line, 1, 0, 0, NULL);
Willy Tarreau0851fd52019-12-17 10:07:25 +01001606 end:
1607 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001608 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001609 nocast:
1610 chunk_appendf(buf, "(undisplayable)");
1611 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001612}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001613
1614// This function checks the "debug" converter's arguments.
1615static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1616 const char *file, int line, char **err)
1617{
1618 const char *name = "buf0";
1619 struct sink *sink = NULL;
1620
1621 if (args[0].type != ARGT_STR) {
1622 /* optional prefix */
1623 args[0].data.str.area = "";
1624 args[0].data.str.data = 0;
1625 }
1626
1627 if (args[1].type == ARGT_STR)
1628 name = args[1].data.str.area;
1629
1630 sink = sink_find(name);
1631 if (!sink) {
1632 memprintf(err, "No such sink '%s'", name);
1633 return 0;
1634 }
1635
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001636 chunk_destroy(&args[1].data.str);
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001637 args[1].type = ARGT_PTR;
1638 args[1].data.ptr = sink;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001639 return 1;
1640}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001641
Holger Just1bfc24b2017-05-06 00:56:53 +02001642static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1643{
Willy Tarreau83061a82018-07-13 11:56:34 +02001644 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001645 int bin_len;
1646
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001647 trash->data = 0;
1648 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1649 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001650 if (bin_len < 0)
1651 return 0;
1652
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001653 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001654 smp->data.u.str = *trash;
1655 smp->data.type = SMP_T_BIN;
1656 smp->flags &= ~SMP_F_CONST;
1657 return 1;
1658}
1659
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +02001660static int sample_conv_base64url2bin(const struct arg *arg_p, struct sample *smp, void *private)
1661{
1662 struct buffer *trash = get_trash_chunk();
1663 int bin_len;
1664
1665 trash->data = 0;
1666 bin_len = base64urldec(smp->data.u.str.area, smp->data.u.str.data,
1667 trash->area, trash->size);
1668 if (bin_len < 0)
1669 return 0;
1670
1671 trash->data = bin_len;
1672 smp->data.u.str = *trash;
1673 smp->data.type = SMP_T_BIN;
1674 smp->flags &= ~SMP_F_CONST;
1675 return 1;
1676}
1677
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001678static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001679{
Willy Tarreau83061a82018-07-13 11:56:34 +02001680 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001681 int b64_len;
1682
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001683 trash->data = 0;
1684 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1685 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001686 if (b64_len < 0)
1687 return 0;
1688
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001689 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001690 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001691 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001692 smp->flags &= ~SMP_F_CONST;
1693 return 1;
1694}
1695
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +02001696static int sample_conv_bin2base64url(const struct arg *arg_p, struct sample *smp, void *private)
1697{
1698 struct buffer *trash = get_trash_chunk();
1699 int b64_len;
1700
1701 trash->data = 0;
1702 b64_len = a2base64url(smp->data.u.str.area, smp->data.u.str.data,
1703 trash->area, trash->size);
1704 if (b64_len < 0)
1705 return 0;
1706
1707 trash->data = b64_len;
1708 smp->data.u.str = *trash;
1709 smp->data.type = SMP_T_STR;
1710 smp->flags &= ~SMP_F_CONST;
1711 return 1;
1712}
1713
Willy Tarreau168e8de2021-10-06 15:29:00 +02001714/* This function returns a sample struct filled with the conversion of variable
1715 * <var> to sample type <type> (SMP_T_*), via a cast to the target type. If the
1716 * variable cannot be retrieved or casted, 0 is returned, otherwise 1.
1717 *
1718 * Keep in mind that the sample content may be written to a pre-allocated
1719 * trash chunk as returned by get_trash_chunk().
1720 */
1721int sample_conv_var2smp(const struct var_desc *var, struct sample *smp, int type)
1722{
1723 if (!vars_get_by_desc(var, smp, NULL))
1724 return 0;
1725 if (!sample_casts[smp->data.type][type])
1726 return 0;
1727 if (!sample_casts[smp->data.type][type](smp))
1728 return 0;
1729 return 1;
1730}
1731
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001732static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1733{
1734 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001735 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001736
1737 memset(&ctx, 0, sizeof(ctx));
1738
1739 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001740 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1741 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001742
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001743 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001744 smp->data.u.str = *trash;
1745 smp->data.type = SMP_T_BIN;
1746 smp->flags &= ~SMP_F_CONST;
1747 return 1;
1748}
1749
Dragan Dosen9e8db132021-02-22 10:03:53 +01001750/* This function returns a sample struct filled with an <arg> content.
1751 * If the <arg> contains a string, it is returned in the sample flagged as
1752 * SMP_F_CONST. If the <arg> contains a variable descriptor, the sample is
1753 * filled with the content of the variable by using vars_get_by_desc().
1754 *
1755 * Keep in mind that the sample content may be written to a pre-allocated
1756 * trash chunk as returned by get_trash_chunk().
1757 *
1758 * This function returns 0 if an error occurs, otherwise it returns 1.
1759 */
Willy Tarreau4034e2c2021-10-06 15:20:18 +02001760int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001761{
1762 switch (arg->type) {
1763 case ARGT_STR:
1764 smp->data.type = SMP_T_STR;
1765 smp->data.u.str = arg->data.str;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001766 smp->flags = SMP_F_CONST;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001767 return 1;
1768 case ARGT_VAR:
Willy Tarreau2476ff12021-10-06 15:30:52 +02001769 return sample_conv_var2smp(&arg->data.var, smp, SMP_T_STR);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001770 default:
1771 return 0;
1772 }
1773}
1774
Marcin Deranek40ca09c2021-07-13 14:05:24 +02001775static int sample_conv_be2dec_check(struct arg *args, struct sample_conv *conv,
1776 const char *file, int line, char **err)
1777{
1778 if (args[1].data.sint <= 0 || args[1].data.sint > sizeof(unsigned long long)) {
Willy Tarreauc5d0fc92021-09-15 10:30:40 +02001779 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 +02001780 return 0;
1781 }
1782
1783 if (args[2].data.sint != 0 && args[2].data.sint != 1) {
1784 memprintf(err, "Unsupported truncate value (%lld)", args[2].data.sint);
1785 return 0;
1786 }
1787
1788 return 1;
1789}
1790
1791/* Converts big-endian binary input sample to a string containing an unsigned
1792 * integer number per <chunk_size> input bytes separated with <separator>.
1793 * Optional <truncate> flag indicates if input is truncated at <chunk_size>
1794 * boundaries.
1795 * Arguments: separator (string), chunk_size (integer), truncate (0,1)
1796 */
1797static int sample_conv_be2dec(const struct arg *args, struct sample *smp, void *private)
1798{
1799 struct buffer *trash = get_trash_chunk();
1800 const int last = args[2].data.sint ? smp->data.u.str.data - args[1].data.sint + 1 : smp->data.u.str.data;
1801 int max_size = trash->size - 2;
1802 int i;
1803 int start;
1804 int ptr = 0;
1805 unsigned long long number;
1806 char *pos;
1807
1808 trash->data = 0;
1809
1810 while (ptr < last && trash->data <= max_size) {
1811 start = trash->data;
1812 if (ptr) {
1813 /* Add separator */
1814 memcpy(trash->area + trash->data, args[0].data.str.area, args[0].data.str.data);
1815 trash->data += args[0].data.str.data;
1816 }
1817 else
1818 max_size -= args[0].data.str.data;
1819
1820 /* Add integer */
1821 for (number = 0, i = 0; i < args[1].data.sint && ptr < smp->data.u.str.data; i++)
1822 number = (number << 8) + (unsigned char)smp->data.u.str.area[ptr++];
1823
1824 pos = ulltoa(number, trash->area + trash->data, trash->size - trash->data);
1825 if (pos)
1826 trash->data = pos - trash->area;
1827 else {
1828 trash->data = start;
1829 break;
1830 }
1831 }
1832
1833 smp->data.u.str = *trash;
1834 smp->data.type = SMP_T_STR;
1835 smp->flags &= ~SMP_F_CONST;
1836 return 1;
1837}
1838
Marcin Deranekda0264a2021-07-13 14:08:56 +02001839static int sample_conv_be2hex_check(struct arg *args, struct sample_conv *conv,
1840 const char *file, int line, char **err)
1841{
1842 if (args[1].data.sint <= 0 && (args[0].data.str.data > 0 || args[2].data.sint != 0)) {
1843 memprintf(err, "chunk_size needs to be positive (%lld)", args[1].data.sint);
1844 return 0;
1845 }
1846
1847 if (args[2].data.sint != 0 && args[2].data.sint != 1) {
1848 memprintf(err, "Unsupported truncate value (%lld)", args[2].data.sint);
1849 return 0;
1850 }
1851
1852 return 1;
1853}
1854
1855/* Converts big-endian binary input sample to a hex string containing two hex
1856 * digits per input byte. <separator> is put every <chunk_size> binary input
1857 * bytes if specified. Optional <truncate> flag indicates if input is truncated
1858 * at <chunk_size> boundaries.
1859 * Arguments: separator (string), chunk_size (integer), truncate (0,1)
1860 */
1861static int sample_conv_be2hex(const struct arg *args, struct sample *smp, void *private)
1862{
1863 struct buffer *trash = get_trash_chunk();
1864 int chunk_size = args[1].data.sint;
1865 const int last = args[2].data.sint ? smp->data.u.str.data - chunk_size + 1 : smp->data.u.str.data;
1866 int i;
1867 int max_size;
1868 int ptr = 0;
1869 unsigned char c;
1870
1871 trash->data = 0;
1872 if (args[0].data.str.data == 0 && args[2].data.sint == 0)
1873 chunk_size = smp->data.u.str.data;
1874 max_size = trash->size - 2 * chunk_size;
1875
1876 while (ptr < last && trash->data <= max_size) {
1877 if (ptr) {
1878 /* Add separator */
1879 memcpy(trash->area + trash->data, args[0].data.str.area, args[0].data.str.data);
1880 trash->data += args[0].data.str.data;
1881 }
1882 else
1883 max_size -= args[0].data.str.data;
1884
1885 /* Add hex */
1886 for (i = 0; i < chunk_size && ptr < smp->data.u.str.data; i++) {
1887 c = smp->data.u.str.area[ptr++];
1888 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1889 trash->area[trash->data++] = hextab[c & 0xF];
1890 }
1891 }
1892
1893 smp->data.u.str = *trash;
1894 smp->data.type = SMP_T_STR;
1895 smp->flags &= ~SMP_F_CONST;
1896 return 1;
1897}
1898
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001899static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001900{
Willy Tarreau83061a82018-07-13 11:56:34 +02001901 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001902 unsigned char c;
1903 int ptr = 0;
1904
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001905 trash->data = 0;
1906 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
1907 c = smp->data.u.str.area[ptr++];
1908 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1909 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001910 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001911 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001912 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001913 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001914 return 1;
1915}
1916
Dragan Dosen3f957b22017-10-24 09:27:34 +02001917static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1918{
1919 long long int n = 0;
1920 int i, c;
1921
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001922 for (i = 0; i < smp->data.u.str.data; i++) {
1923 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02001924 return 0;
1925 n = (n << 4) + c;
1926 }
1927
1928 smp->data.u.sint = n;
1929 smp->data.type = SMP_T_SINT;
1930 smp->flags &= ~SMP_F_CONST;
1931 return 1;
1932}
1933
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001934/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001935static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001936{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001937 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
1938 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01001939 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001940 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001941 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001942 return 1;
1943}
1944
Willy Tarreau60a2ee72017-12-15 07:13:48 +01001945static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001946{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001947 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001948 smp->data.u.sint = i;
1949 smp->data.type = SMP_T_SINT;
1950 return 1;
1951}
1952
1953
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001954static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001955{
1956 int i;
1957
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001958 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001959 return 0;
1960
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001961 for (i = 0; i < smp->data.u.str.data; i++) {
1962 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
1963 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001964 }
1965 return 1;
1966}
1967
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001968static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001969{
1970 int i;
1971
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001972 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001973 return 0;
1974
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001975 for (i = 0; i < smp->data.u.str.data; i++) {
1976 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
1977 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001978 }
1979 return 1;
1980}
1981
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001982/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
1983static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001984{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001985 /* Attempt to convert to IPv4 to apply the correct mask. */
1986 c_ipv62ip(smp);
1987
1988 if (smp->data.type == SMP_T_IPV4) {
1989 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
1990 smp->data.type = SMP_T_IPV4;
1991 }
1992 else if (smp->data.type == SMP_T_IPV6) {
1993 /* IPv6 cannot be converted without an IPv6 mask. */
1994 if (args[1].type != ARGT_IPV6)
1995 return 0;
1996
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +01001997 write_u64(&smp->data.u.ipv6.s6_addr[0],
1998 read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0]));
1999 write_u64(&smp->data.u.ipv6.s6_addr[8],
2000 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 +01002001 smp->data.type = SMP_T_IPV6;
2002 }
2003
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002004 return 1;
2005}
2006
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002007/* takes an UINT value on input supposed to represent the time since EPOCH,
2008 * adds an optional offset found in args[1] and emits a string representing
2009 * the local time in the format specified in args[1] using strftime().
2010 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002011static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002012{
Willy Tarreau83061a82018-07-13 11:56:34 +02002013 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002014 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002015 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002016 struct tm tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002017
2018 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002019 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002020 curr_date += args[1].data.sint;
2021
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002022 get_localtime(curr_date, &tm);
2023
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002024 temp = get_trash_chunk();
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002025 temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002026 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002027 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002028 return 1;
2029}
2030
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002031/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002032static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002033{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002034 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
2035 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002036 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002037 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002038 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002039 return 1;
2040}
2041
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002042/* takes an UINT value on input supposed to represent the time since EPOCH,
2043 * adds an optional offset found in args[1] and emits a string representing
2044 * the UTC date in the format specified in args[1] using strftime().
2045 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002046static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002047{
Willy Tarreau83061a82018-07-13 11:56:34 +02002048 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002049 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002050 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002051 struct tm tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002052
2053 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002054 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002055 curr_date += args[1].data.sint;
2056
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002057 get_gmtime(curr_date, &tm);
2058
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002059 temp = get_trash_chunk();
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002060 temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002061 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002062 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002063 return 1;
2064}
2065
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002066/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002067static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002068{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002069 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
2070 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002071 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002072 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002073 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002074 return 1;
2075}
2076
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002077/* hashes the binary input into a 32-bit unsigned int using xxh.
2078 * The seed of the hash defaults to 0 but can be changd in argument 1.
2079 */
2080static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
2081{
2082 unsigned int seed;
2083
Christopher Faulete6e7a582021-01-29 11:29:28 +01002084 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002085 seed = arg_p->data.sint;
2086 else
2087 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002088 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
2089 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002090 smp->data.type = SMP_T_SINT;
2091 return 1;
2092}
2093
2094/* hashes the binary input into a 64-bit unsigned int using xxh.
2095 * In fact, the function returns a 64 bit unsigned, but the sample
2096 * storage of haproxy only proposes 64-bits signed, so the value is
2097 * cast as signed. This cast doesn't impact the hash repartition.
2098 * The seed of the hash defaults to 0 but can be changd in argument 1.
2099 */
2100static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
2101{
2102 unsigned long long int seed;
2103
Christopher Faulete6e7a582021-01-29 11:29:28 +01002104 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002105 seed = (unsigned long long int)arg_p->data.sint;
2106 else
2107 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002108 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
2109 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002110 smp->data.type = SMP_T_SINT;
2111 return 1;
2112}
2113
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002114static int sample_conv_xxh3(const struct arg *arg_p, struct sample *smp, void *private)
2115{
2116 unsigned long long int seed;
2117
Christopher Faulete6e7a582021-01-29 11:29:28 +01002118 if (arg_p->data.sint)
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002119 seed = (unsigned long long int)arg_p->data.sint;
2120 else
2121 seed = 0;
2122 smp->data.u.sint = (long long int)XXH3(smp->data.u.str.area,
2123 smp->data.u.str.data, seed);
2124 smp->data.type = SMP_T_SINT;
2125 return 1;
2126}
2127
Willy Tarreau80599772015-01-20 19:35:24 +01002128/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002129static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01002130{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002131 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
2132 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002133 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002134 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002135 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01002136 return 1;
2137}
2138
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002139/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
2140static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
2141{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002142 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
2143 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002144 if (arg_p->data.sint)
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002145 smp->data.u.sint = full_hash(smp->data.u.sint);
2146 smp->data.type = SMP_T_SINT;
2147 return 1;
2148}
2149
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002150/* This function escape special json characters. The returned string can be
2151 * safely set between two '"' and used as json string. The json string is
2152 * defined like this:
2153 *
2154 * any Unicode character except '"' or '\' or control character
2155 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
2156 *
2157 * The enum input_type contain all the allowed mode for decoding the input
2158 * string.
2159 */
2160enum input_type {
2161 IT_ASCII = 0,
2162 IT_UTF8,
2163 IT_UTF8S,
2164 IT_UTF8P,
2165 IT_UTF8PS,
2166};
William Dauchy5417e892021-01-08 21:57:41 +01002167
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002168static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
2169 const char *file, int line, char **err)
2170{
Christopher Faulet95917132020-08-05 23:07:07 +02002171 enum input_type type;
2172
Christopher Faulet95917132020-08-05 23:07:07 +02002173 if (strcmp(arg->data.str.area, "") == 0)
2174 type = IT_ASCII;
2175 else if (strcmp(arg->data.str.area, "ascii") == 0)
2176 type = IT_ASCII;
2177 else if (strcmp(arg->data.str.area, "utf8") == 0)
2178 type = IT_UTF8;
2179 else if (strcmp(arg->data.str.area, "utf8s") == 0)
2180 type = IT_UTF8S;
2181 else if (strcmp(arg->data.str.area, "utf8p") == 0)
2182 type = IT_UTF8P;
2183 else if (strcmp(arg->data.str.area, "utf8ps") == 0)
2184 type = IT_UTF8PS;
2185 else {
2186 memprintf(err, "Unexpected input code type. "
2187 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
2188 return 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002189 }
2190
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002191 chunk_destroy(&arg->data.str);
Christopher Faulet95917132020-08-05 23:07:07 +02002192 arg->type = ARGT_SINT;
2193 arg->data.sint = type;
2194 return 1;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002195}
2196
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002197static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002198{
Willy Tarreau83061a82018-07-13 11:56:34 +02002199 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002200 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
2201 const char *str;
2202 int len;
2203 enum input_type input_type = IT_ASCII;
2204 unsigned int c;
2205 unsigned int ret;
2206 char *p;
2207
Christopher Faulete6e7a582021-01-29 11:29:28 +01002208 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002209
2210 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002211 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002212
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002213 p = smp->data.u.str.area;
2214 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002215
2216 if (input_type == IT_ASCII) {
2217 /* Read input as ASCII. */
2218 c = *(unsigned char *)p;
2219 p++;
2220 }
2221 else {
2222 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002223 ret = utf8_next(p,
2224 smp->data.u.str.data - ( p - smp->data.u.str.area),
2225 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002226 p += utf8_return_length(ret);
2227
2228 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2229 return 0;
2230 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2231 continue;
2232 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2233 return 0;
2234 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2235 continue;
2236
2237 /* Check too big values. */
2238 if ((unsigned int)c > 0xffff) {
2239 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2240 return 0;
2241 continue;
2242 }
2243 }
2244
2245 /* Convert character. */
2246 if (c == '"') {
2247 len = 2;
2248 str = "\\\"";
2249 }
2250 else if (c == '\\') {
2251 len = 2;
2252 str = "\\\\";
2253 }
2254 else if (c == '/') {
2255 len = 2;
2256 str = "\\/";
2257 }
2258 else if (c == '\b') {
2259 len = 2;
2260 str = "\\b";
2261 }
2262 else if (c == '\f') {
2263 len = 2;
2264 str = "\\f";
2265 }
2266 else if (c == '\r') {
2267 len = 2;
2268 str = "\\r";
2269 }
2270 else if (c == '\n') {
2271 len = 2;
2272 str = "\\n";
2273 }
2274 else if (c == '\t') {
2275 len = 2;
2276 str = "\\t";
2277 }
Willy Tarreau90807112020-02-25 08:16:33 +01002278 else if (c > 0xff || !isprint((unsigned char)c)) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002279 /* isprint generate a segfault if c is too big. The man says that
2280 * c must have the value of an unsigned char or EOF.
2281 */
2282 len = 6;
2283 _str[0] = '\\';
2284 _str[1] = 'u';
2285 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2286 str = _str;
2287 }
2288 else {
2289 len = 1;
Willy Tarreau5715da22020-02-25 08:37:37 +01002290 _str[0] = c;
2291 str = _str;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002292 }
2293
2294 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002295 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002296 return 0;
2297
2298 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002299 memcpy(temp->area + temp->data, str, len);
2300 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002301 }
2302
2303 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002304 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002305 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002306
2307 return 1;
2308}
2309
Emeric Brun54c4ac82014-11-03 15:32:43 +01002310/* This sample function is designed to extract some bytes from an input buffer.
2311 * First arg is the offset.
2312 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002313static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002314{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002315 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2316 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002317 return 1;
2318 }
2319
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002320 if (smp->data.u.str.size)
2321 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002322 smp->data.u.str.data -= arg_p[0].data.sint;
2323 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002324
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002325 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2326 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002327
2328 return 1;
2329}
2330
Emeric Brunf399b0d2014-11-03 17:07:03 +01002331static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2332 const char *file, int line, char **err)
2333{
2334 struct arg *arg = args;
2335
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002336 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002337 memprintf(err, "Unexpected arg type");
2338 return 0;
2339 }
2340
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002341 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002342 memprintf(err, "Unexpected value 0 for index");
2343 return 0;
2344 }
2345
2346 arg++;
2347
2348 if (arg->type != ARGT_STR) {
2349 memprintf(err, "Unexpected arg type");
2350 return 0;
2351 }
2352
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002353 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002354 memprintf(err, "Empty separators list");
2355 return 0;
2356 }
2357
2358 return 1;
2359}
2360
2361/* This sample function is designed to a return selected part of a string (field).
2362 * First arg is the index of the field (start at 1)
2363 * Second arg is a char list of separators (type string)
2364 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002365static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002366{
Marcin Deranek9631a282018-04-16 14:30:46 +02002367 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002368 char *start, *end;
2369 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002370 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002371
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002372 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002373 return 0;
2374
Marcin Deranek9631a282018-04-16 14:30:46 +02002375 if (arg_p[0].data.sint < 0) {
2376 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002377 end = start = smp->data.u.str.area + smp->data.u.str.data;
2378 while (start > smp->data.u.str.area) {
2379 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2380 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002381 if (field == arg_p[0].data.sint) {
2382 if (count == 1)
2383 goto found;
2384 else if (count > 1)
2385 count--;
2386 } else {
2387 end = start-1;
2388 field--;
2389 }
2390 break;
2391 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002392 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002393 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002394 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002395 } else {
2396 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002397 end = start = smp->data.u.str.area;
2398 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2399 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2400 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002401 if (field == arg_p[0].data.sint) {
2402 if (count == 1)
2403 goto found;
2404 else if (count > 1)
2405 count--;
2406 } else {
2407 start = end+1;
2408 field++;
2409 }
2410 break;
2411 }
2412 }
2413 end++;
2414 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002415 }
2416
2417 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002418 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002419 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002420 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002421 }
2422found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002423 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002424 /* If ret string is len 0, no need to
2425 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002426 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002427 return 1;
2428
Emeric Brunf399b0d2014-11-03 17:07:03 +01002429 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002430 Note: smp->data.u.str.size cannot be set to 0 */
2431 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002432 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002433
Thayne McCombsb2843052021-04-11 23:26:59 -06002434 smp->data.u.str.area = start;
2435
Emeric Brunf399b0d2014-11-03 17:07:03 +01002436 return 1;
2437}
2438
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002439/* This sample function is designed to return a word from a string.
2440 * First arg is the index of the word (start at 1)
2441 * Second arg is a char list of words separators (type string)
2442 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002443static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002444{
Marcin Deranek9631a282018-04-16 14:30:46 +02002445 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002446 char *start, *end;
2447 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002448 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002449
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002450 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002451 return 0;
2452
2453 word = 0;
2454 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002455 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002456 end = start = smp->data.u.str.area + smp->data.u.str.data;
2457 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002458 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002459 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2460 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002461 issep = 1;
2462 break;
2463 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002464 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002465 if (!inword) {
2466 if (!issep) {
2467 if (word != arg_p[0].data.sint) {
2468 word--;
2469 end = start;
2470 }
2471 inword = 1;
2472 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002473 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002474 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002475 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002476 if (count == 1)
2477 goto found;
2478 else if (count > 1)
2479 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002480 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002481 inword = 0;
2482 }
2483 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002484 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002485 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002486 end = start = smp->data.u.str.area;
2487 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002488 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002489 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2490 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002491 issep = 1;
2492 break;
2493 }
2494 }
2495 if (!inword) {
2496 if (!issep) {
2497 if (word != arg_p[0].data.sint) {
2498 word++;
2499 start = end;
2500 }
2501 inword = 1;
2502 }
2503 }
2504 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002505 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002506 if (count == 1)
2507 goto found;
2508 else if (count > 1)
2509 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002510 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002511 inword = 0;
2512 }
2513 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002514 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002515 }
2516
2517 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002518 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002519 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002520 return 1;
2521 }
2522found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002523 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002524 /* If ret string is len 0, no need to
2525 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002526 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002527 return 1;
2528
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002529 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002530
2531 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002532 Note: smp->data.u.str.size cannot be set to 0 */
2533 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002534 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002535
2536 return 1;
2537}
2538
Willy Tarreau7eda8492015-01-20 19:47:06 +01002539static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2540 const char *file, int line, char **err)
2541{
2542 struct arg *arg = args;
2543 char *p;
2544 int len;
2545
2546 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2547 arg[0].type_flags = 0;
2548
2549 if (arg[2].type != ARGT_STR)
2550 return 1;
2551
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002552 p = arg[2].data.str.area;
2553 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002554 while (len) {
2555 if (*p == 'i') {
2556 arg[0].type_flags |= ARGF_REG_ICASE;
2557 }
2558 else if (*p == 'g') {
2559 arg[0].type_flags |= ARGF_REG_GLOB;
2560 }
2561 else {
2562 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2563 return 0;
2564 }
2565 p++;
2566 len--;
2567 }
2568 return 1;
2569}
2570
2571/* This sample function is designed to do the equivalent of s/match/replace/ on
2572 * the input string. It applies a regex and restarts from the last matched
2573 * location until nothing matches anymore. First arg is the regex to apply to
2574 * the input string, second arg is the replacement expression.
2575 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002576static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002577{
2578 char *start, *end;
2579 struct my_regex *reg = arg_p[0].data.reg;
2580 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002581 struct buffer *trash = get_trash_chunk();
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002582 struct buffer *output;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002583 int flag, max;
2584 int found;
2585
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002586 start = smp->data.u.str.area;
2587 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002588
2589 flag = 0;
2590 while (1) {
2591 /* check for last round which is used to copy remaining parts
2592 * when not running in global replacement mode.
2593 */
2594 found = 0;
2595 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2596 /* Note: we can have start == end on empty strings or at the end */
2597 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2598 }
2599
2600 if (!found)
2601 pmatch[0].rm_so = end - start;
2602
2603 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002604 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002605 if (max && pmatch[0].rm_so > 0) {
2606 if (max > pmatch[0].rm_so)
2607 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002608 memcpy(trash->area + trash->data, start, max);
2609 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002610 }
2611
2612 if (!found)
2613 break;
2614
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002615 output = alloc_trash_chunk();
Willy Tarreau23997da2020-02-18 14:27:44 +01002616 if (!output)
2617 break;
2618
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002619 output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2620
Willy Tarreau7eda8492015-01-20 19:47:06 +01002621 /* replace the matching part */
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002622 max = output->size - output->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002623 if (max) {
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002624 if (max > output->data)
2625 max = output->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002626 memcpy(trash->area + trash->data,
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002627 output->area, max);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002628 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002629 }
2630
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002631 free_trash_chunk(output);
2632
Willy Tarreau7eda8492015-01-20 19:47:06 +01002633 /* stop here if we're done with this string */
2634 if (start >= end)
2635 break;
2636
2637 /* We have a special case for matches of length 0 (eg: "x*y*").
2638 * These ones are considered to match in front of a character,
2639 * so we have to copy that character and skip to the next one.
2640 */
2641 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002642 if (trash->data < trash->size)
2643 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002644 pmatch[0].rm_eo++;
2645 }
2646
2647 start += pmatch[0].rm_eo;
2648 flag |= REG_NOTBOL;
2649 }
2650
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002651 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002652 return 1;
2653}
2654
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002655/* This function check an operator entry. It expects a string.
2656 * The string can be an integer or a variable name.
2657 */
2658static int check_operator(struct arg *args, struct sample_conv *conv,
2659 const char *file, int line, char **err)
2660{
2661 const char *str;
2662 const char *end;
Christopher Faulet95917132020-08-05 23:07:07 +02002663 long long int i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002664
2665 /* Try to decode a variable. */
2666 if (vars_check_arg(&args[0], NULL))
2667 return 1;
2668
2669 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002670 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002671 end = str + strlen(str);
Christopher Faulet95917132020-08-05 23:07:07 +02002672 i = read_int64(&str, end);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002673 if (*str != '\0') {
2674 memprintf(err, "expects an integer or a variable name");
2675 return 0;
2676 }
Christopher Faulet95917132020-08-05 23:07:07 +02002677
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002678 chunk_destroy(&args[0].data.str);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002679 args[0].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02002680 args[0].data.sint = i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002681 return 1;
2682}
2683
Willy Tarreau6204cd92016-03-10 16:33:04 +01002684/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002685 * If the arg contain an integer, the integer is returned in the
2686 * sample. If the arg contains a variable descriptor, it returns the
2687 * variable value.
2688 *
2689 * This function returns 0 if an error occurs, otherwise it returns 1.
2690 */
Willy Tarreaud9be5992021-10-06 15:19:05 +02002691int sample_conv_var2smp_sint(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002692{
2693 switch (arg->type) {
2694 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002695 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002696 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002697 return 1;
2698 case ARGT_VAR:
Willy Tarreau2476ff12021-10-06 15:30:52 +02002699 return sample_conv_var2smp(&arg->data.var, smp, SMP_T_SINT);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002700 default:
2701 return 0;
2702 }
2703}
2704
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002705/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002706 * result.
2707 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002708static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002709{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002710 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002711 return 1;
2712}
2713
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002714/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002715 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002716 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002717static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002718{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002719 struct sample tmp;
2720
Willy Tarreau7560dd42016-03-10 16:28:58 +01002721 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002722 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002723 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002724 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002725 return 1;
2726}
2727
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002728/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002729 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002730 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002731static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002732{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002733 struct sample tmp;
2734
Willy Tarreau7560dd42016-03-10 16:28:58 +01002735 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002736 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002737 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002738 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002739 return 1;
2740}
2741
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002742/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002743 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002744 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002745static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002746{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002747 struct sample tmp;
2748
Willy Tarreau7560dd42016-03-10 16:28:58 +01002749 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002750 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002751 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002752 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002753 return 1;
2754}
2755
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002756static inline long long int arith_add(long long int a, long long int b)
2757{
2758 /* Prevent overflow and makes capped calculus.
2759 * We must ensure that the check calculus doesn't
2760 * exceed the signed 64 bits limits.
2761 *
2762 * +----------+----------+
2763 * | a<0 | a>=0 |
2764 * +------+----------+----------+
2765 * | b<0 | MIN-a>b | no check |
2766 * +------+----------+----------+
2767 * | b>=0 | no check | MAX-a<b |
2768 * +------+----------+----------+
2769 */
2770 if ((a ^ b) >= 0) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002771 /* signs are different. */
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002772 if (a < 0) {
2773 if (LLONG_MIN - a > b)
2774 return LLONG_MIN;
2775 }
2776 if (LLONG_MAX - a < b)
2777 return LLONG_MAX;
2778 }
2779 return a + b;
2780}
2781
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002782/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002783 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002784 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002785static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002786{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002787 struct sample tmp;
2788
Willy Tarreau7560dd42016-03-10 16:28:58 +01002789 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002790 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002791 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002792 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002793 return 1;
2794}
2795
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002796/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002797 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002798 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002799static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002800 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002801{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002802 struct sample tmp;
2803
Willy Tarreau7560dd42016-03-10 16:28:58 +01002804 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002805 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002806 return 0;
2807
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002808 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2809 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2810 * of -LLONG_MIN and correct the result.
2811 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002812 if (tmp.data.u.sint == LLONG_MIN) {
2813 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2814 if (smp->data.u.sint < LLONG_MAX)
2815 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002816 return 1;
2817 }
2818
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002819 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002820 * the second operand.
2821 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002822 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002823 return 1;
2824}
2825
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002826/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002827 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002828 * If the result makes an overflow, then the largest possible quantity is
2829 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002830 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002831static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002832 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002833{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002834 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002835 long long int c;
2836
Willy Tarreau7560dd42016-03-10 16:28:58 +01002837 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002838 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002839 return 0;
2840
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002841 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002842 if (!smp->data.u.sint || !tmp.data.u.sint) {
2843 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002844 return 1;
2845 }
2846
2847 /* The multiply between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002848 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002849 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002850 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2851 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002852 return 1;
2853 }
2854
2855 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002856 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002857
2858 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002859 if (smp->data.u.sint != c / tmp.data.u.sint) {
2860 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2861 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002862 return 1;
2863 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002864 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002865 return 1;
2866 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002867 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002868 return 1;
2869}
2870
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002871/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002872 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002873 * If arg_p makes the result overflow, then the largest possible quantity is
2874 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002875 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002876static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002877 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002878{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002879 struct sample tmp;
2880
Willy Tarreau7560dd42016-03-10 16:28:58 +01002881 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002882 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002883 return 0;
2884
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002885 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002886 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002887 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002888 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002889 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2890 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002891 return 1;
2892 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002893 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002894 return 1;
2895 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002896 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002897 return 1;
2898}
2899
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002900/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002901 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002902 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002903 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002904static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002905 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002906{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002907 struct sample tmp;
2908
Willy Tarreau7560dd42016-03-10 16:28:58 +01002909 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02002910 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002911 return 0;
2912
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002913 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002914 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002915 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002916 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002917 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2918 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002919 return 1;
2920 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002921 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002922 return 1;
2923 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002924 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002925 return 1;
2926}
2927
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002928/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002929 * result.
2930 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002931static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002932 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002933{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002934 if (smp->data.u.sint == LLONG_MIN)
2935 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002936 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002937 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002938 return 1;
2939}
2940
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002941/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002942 * false. The output is a BOOL.
2943 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002944static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002945 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002946{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002947 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002948 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002949 return 1;
2950}
2951
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002952/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002953 * truee. The output is a BOOL.
2954 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002955static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002956 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002957{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002958 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002959 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002960 return 1;
2961}
2962
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002963/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002964 * The output is a BOOL.
2965 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002966static int sample_conv_arith_odd(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 FOURNIER136f9d32015-08-19 09:07:19 +02002969 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002970 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002971 return 1;
2972}
2973
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002974/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002975 * The output is a BOOL.
2976 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002977static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002978 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002979{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002980 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002981 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002982 return 1;
2983}
2984
Willy Tarreau280f42b2018-02-19 15:34:12 +01002985/* appends an optional const string, an optional variable contents and another
2986 * optional const string to an existing string.
2987 */
2988static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
2989{
Willy Tarreau83061a82018-07-13 11:56:34 +02002990 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002991 struct sample tmp;
2992 int max;
2993
Willy Tarreau591fc3a2021-01-08 16:08:43 +01002994 trash = alloc_trash_chunk();
William Dauchye9970102021-01-11 11:05:58 +01002995 if (!trash)
2996 return 0;
2997
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002998 trash->data = smp->data.u.str.data;
2999 if (trash->data > trash->size - 1)
3000 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003001
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003002 memcpy(trash->area, smp->data.u.str.area, trash->data);
3003 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003004
3005 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003006 max = arg_p[0].data.str.data;
3007 if (max > trash->size - 1 - trash->data)
3008 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003009
3010 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003011 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
3012 trash->data += max;
3013 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003014 }
3015
3016 /* append second string (variable) if it's found and we can turn it
3017 * into a string.
3018 */
3019 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaue352b9d2021-09-03 11:52:38 +02003020 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 +01003021 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
3022 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
3023
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003024 max = tmp.data.u.str.data;
3025 if (max > trash->size - 1 - trash->data)
3026 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003027
3028 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003029 memcpy(trash->area + trash->data, tmp.data.u.str.area,
3030 max);
3031 trash->data += max;
3032 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003033 }
3034 }
3035
3036 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003037 max = arg_p[2].data.str.data;
3038 if (max > trash->size - 1 - trash->data)
3039 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003040
3041 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003042 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
3043 trash->data += max;
3044 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003045 }
3046
3047 smp->data.u.str = *trash;
3048 smp->data.type = SMP_T_STR;
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003049 smp_dup(smp);
3050 free_trash_chunk(trash);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003051 return 1;
3052}
3053
3054/* This function checks the "concat" converter's arguments and extracts the
3055 * variable name and its scope.
3056 */
3057static int smp_check_concat(struct arg *args, struct sample_conv *conv,
3058 const char *file, int line, char **err)
3059{
3060 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003061 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
3062 memprintf(err, "failed to register variable name '%s'",
3063 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003064 return 0;
3065 }
3066 return 1;
3067}
3068
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003069/* Compares string with a variable containing a string. Return value
Tim Duesterhusca097c12018-04-27 21:18:45 +02003070 * is compatible with strcmp(3)'s return value.
3071 */
3072static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
3073{
3074 struct sample tmp;
3075 int max, result;
3076
3077 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3078 if (arg_p[0].type != ARGT_VAR)
3079 return 0;
Willy Tarreau2476ff12021-10-06 15:30:52 +02003080
3081 if (!sample_conv_var2smp(&arg_p[0].data.var, &tmp, SMP_T_STR))
Tim Duesterhusca097c12018-04-27 21:18:45 +02003082 return 0;
3083
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003084 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
3085 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003086 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003087 if (smp->data.u.str.data != tmp.data.u.str.data) {
3088 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02003089 result = -1;
3090 }
3091 else {
3092 result = 1;
3093 }
3094 }
3095 }
3096
3097 smp->data.u.sint = result;
3098 smp->data.type = SMP_T_SINT;
3099 return 1;
3100}
3101
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02003102/* Takes a boolean as input. Returns the first argument if that boolean is true and
3103 * the second argument otherwise.
3104 */
3105static int sample_conv_iif(const struct arg *arg_p, struct sample *smp, void *private)
3106{
3107 smp->data.type = SMP_T_STR;
3108 smp->flags |= SMP_F_CONST;
3109
3110 if (smp->data.u.sint) {
3111 smp->data.u.str.data = arg_p[0].data.str.data;
3112 smp->data.u.str.area = arg_p[0].data.str.area;
3113 }
3114 else {
3115 smp->data.u.str.data = arg_p[1].data.str.data;
3116 smp->data.u.str.area = arg_p[1].data.str.area;
3117 }
3118
3119 return 1;
3120}
3121
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003122#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
3123#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
3124#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
3125
3126/*
3127 * Extract the field value of an input binary sample. Takes a mandatory argument:
3128 * the protocol buffers field identifier (dotted notation) internally represented
3129 * as an array of unsigned integers and its size.
3130 * Return 1 if the field was found, 0 if not.
3131 */
3132static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
3133{
3134 unsigned char *pos;
3135 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003136
3137 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003138 grpc_left = smp->data.u.str.data;
3139
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01003140 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003141 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003142
3143 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
3144
3145 pos += GRPC_MSG_HEADER_SZ;
3146 grpc_left -= GRPC_MSG_HEADER_SZ;
3147
3148 if (grpc_left < left)
3149 return 0;
3150
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01003151 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
3152 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003153
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003154 grpc_left -= grpc_msg_len;
3155 }
3156
3157 return 0;
3158}
3159
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003160static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
3161{
3162 unsigned char *pos;
3163 size_t left;
3164
3165 pos = (unsigned char *)smp->data.u.str.area;
3166 left = smp->data.u.str.data;
3167
3168 return protobuf_field_lookup(arg_p, smp, &pos, &left);
3169}
3170
3171static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
3172 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003173{
3174 if (!args[1].type) {
3175 args[1].type = ARGT_SINT;
3176 args[1].data.sint = PBUF_T_BINARY;
3177 }
3178 else {
3179 int pbuf_type;
3180
3181 pbuf_type = protobuf_type(args[1].data.str.area);
3182 if (pbuf_type == -1) {
3183 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
3184 return 0;
3185 }
3186
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003187 chunk_destroy(&args[1].data.str);
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003188 args[1].type = ARGT_SINT;
3189 args[1].data.sint = pbuf_type;
3190 }
3191
3192 return 1;
3193}
3194
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003195/*
3196 * Extract the tag value of an input binary sample. Takes a mandatory argument:
3197 * the FIX protocol tag identifier.
3198 * Return 1 if the tag was found, 0 if not.
3199 */
3200static int sample_conv_fix_tag_value(const struct arg *arg_p, struct sample *smp, void *private)
3201{
3202 struct ist value;
3203
3204 smp->flags &= ~SMP_F_MAY_CHANGE;
3205 value = fix_tag_value(ist2(smp->data.u.str.area, smp->data.u.str.data),
3206 arg_p[0].data.sint);
3207 if (!istlen(value)) {
Christopher Fauleta5aa0822021-10-13 17:58:53 +02003208 if (isttest(value)) {
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003209 /* value != IST_NULL, need more data */
3210 smp->flags |= SMP_F_MAY_CHANGE;
3211 }
3212 return 0;
3213 }
3214
3215 smp->data.u.str = ist2buf(value);
3216 smp->flags |= SMP_F_CONST;
3217
3218 return 1;
3219}
3220
3221/* This function checks the "fix_tag_value" converter configuration.
3222 * It expects a "known" (by HAProxy) tag name or ID.
3223 * Tag string names are converted to their ID counterpart because this is the
3224 * format they are sent over the wire.
3225 */
3226static int sample_conv_fix_value_check(struct arg *args, struct sample_conv *conv,
3227 const char *file, int line, char **err)
3228{
3229 struct ist str;
3230 unsigned int tag;
3231
3232 str = ist2(args[0].data.str.area, args[0].data.str.data);
3233 tag = fix_tagid(str);
3234 if (!tag) {
3235 memprintf(err, "Unknown FIX tag name '%s'", args[0].data.str.area);
3236 return 0;
3237 }
3238
3239 chunk_destroy(&args[0].data.str);
3240 args[0].type = ARGT_SINT;
3241 args[0].data.sint = tag;
3242
3243 return 1;
3244}
3245
3246/*
3247 * Checks that a buffer contains a valid FIX message
3248 *
3249 * Return 1 if the check could be run, 0 if not.
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003250 * The result of the analyse itself is stored in <smp> as a boolean
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003251 */
3252static int sample_conv_fix_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3253{
3254 struct ist msg;
3255
3256 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3257
3258 smp->flags &= ~SMP_F_MAY_CHANGE;
3259 switch (fix_validate_message(msg)) {
3260 case FIX_VALID_MESSAGE:
3261 smp->data.type = SMP_T_BOOL;
3262 smp->data.u.sint = 1;
3263 return 1;
3264 case FIX_NEED_MORE_DATA:
3265 smp->flags |= SMP_F_MAY_CHANGE;
3266 return 0;
3267 case FIX_INVALID_MESSAGE:
3268 smp->data.type = SMP_T_BOOL;
3269 smp->data.u.sint = 0;
3270 return 1;
3271 }
3272 return 0;
3273}
3274
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003275/*
3276 * Extract the field value of an input binary sample containing an MQTT packet.
3277 * Takes 2 mandatory arguments:
3278 * - packet type
3279 * - field name
3280 *
3281 * return 1 if the field was found, 0 if not.
3282 */
3283static int sample_conv_mqtt_field_value(const struct arg *arg_p, struct sample *smp, void *private)
3284{
3285 struct ist pkt, value;
3286 int type, fieldname_id;
3287
3288 pkt = ist2(smp->data.u.str.area, smp->data.u.str.data);
3289 type = arg_p[0].data.sint;
3290 fieldname_id = arg_p[1].data.sint;
3291
3292 smp->flags &= ~SMP_F_MAY_CHANGE;
3293 value = mqtt_field_value(pkt, type, fieldname_id);
3294 if (!istlen(value)) {
3295 if (isttest(value)) {
3296 /* value != IST_NULL, need more data */
3297 smp->flags |= SMP_F_MAY_CHANGE;
3298 }
3299 return 0;
3300 }
3301
3302 smp->data.u.str = ist2buf(value);
3303 smp->flags |= SMP_F_CONST;
3304 return 1;
3305}
3306
3307/*
3308 * this function checks the "mqtt_field_value" converter configuration.
3309 * It expects a known packet type name or ID and a field name, in this order
3310 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003311 * Args[0] will be turned into a MQTT_CPT_* value for direct matching when parsing
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003312 * a packet.
3313 */
3314static int sample_conv_mqtt_field_value_check(struct arg *args, struct sample_conv *conv,
3315 const char *file, int line, char **err)
3316{
3317 int type, fieldname_id;
3318
3319 /* check the MQTT packet type is valid */
3320 type = mqtt_typeid(ist2(args[0].data.str.area, args[0].data.str.data));
3321 if (type == MQTT_CPT_INVALID) {
3322 memprintf(err, "Unknown MQTT type '%s'", args[0].data.str.area);
3323 return 0;
3324 }
3325
3326 /* check the field name belongs to the MQTT packet type */
3327 fieldname_id = mqtt_check_type_fieldname(type, ist2(args[1].data.str.area, args[1].data.str.data));
3328 if (fieldname_id == MQTT_FN_INVALID) {
3329 memprintf(err, "Unknown MQTT field name '%s' for packet type '%s'", args[1].data.str.area,
3330 args[0].data.str.area);
3331 return 0;
3332 }
3333
3334 /* save numeric counterparts of type and field name */
3335 chunk_destroy(&args[0].data.str);
3336 chunk_destroy(&args[1].data.str);
3337 args[0].type = ARGT_SINT;
3338 args[0].data.sint = type;
3339 args[1].type = ARGT_SINT;
3340 args[1].data.sint = fieldname_id;
3341
3342 return 1;
3343}
3344
3345/*
3346 * Checks that <smp> contains a valid MQTT message
3347 *
3348 * The function returns 1 if the check was run to its end, 0 otherwise.
3349 * The result of the analyse itself is stored in <smp> as a boolean.
3350 */
3351static int sample_conv_mqtt_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3352{
3353 struct ist msg;
3354
3355 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3356
3357 smp->flags &= ~SMP_F_MAY_CHANGE;
3358 switch (mqtt_validate_message(msg, NULL)) {
3359 case FIX_VALID_MESSAGE:
3360 smp->data.type = SMP_T_BOOL;
3361 smp->data.u.sint = 1;
3362 return 1;
3363 case FIX_NEED_MORE_DATA:
3364 smp->flags |= SMP_F_MAY_CHANGE;
3365 return 0;
3366 case FIX_INVALID_MESSAGE:
3367 smp->data.type = SMP_T_BOOL;
3368 smp->data.u.sint = 0;
3369 return 1;
3370 }
3371 return 0;
3372}
3373
Tim Duesterhusca097c12018-04-27 21:18:45 +02003374/* This function checks the "strcmp" converter's arguments and extracts the
3375 * variable name and its scope.
3376 */
3377static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
3378 const char *file, int line, char **err)
3379{
Willy Tarreaua1169b62021-05-08 06:50:28 +02003380 if (!args[0].data.str.data) {
3381 memprintf(err, "missing variable name");
3382 return 0;
3383 }
3384
Tim Duesterhusca097c12018-04-27 21:18:45 +02003385 /* Try to decode a variable. */
3386 if (vars_check_arg(&args[0], NULL))
3387 return 1;
3388
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003389 memprintf(err, "failed to register variable name '%s'",
3390 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003391 return 0;
3392}
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003393
Christopher Faulet4ccc12f2020-04-01 09:08:32 +02003394/**/
3395static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private)
3396{
3397 struct buffer *tmp;
3398 uint32_t n;
3399
3400 n = htonl((uint32_t)smp->data.u.sint);
3401 tmp = get_trash_chunk();
3402
3403 memcpy(b_head(tmp), &n, 4);
3404 b_add(tmp, 4);
3405
3406 smp->data.u.str = *tmp;
3407 smp->data.type = SMP_T_BIN;
3408 return 1;
3409}
3410
Christopher Fauletea159d62020-04-01 16:21:44 +02003411/**/
3412static int sample_conv_cut_crlf(const struct arg *arg_p, struct sample *smp, void *private)
3413{
3414 char *p;
3415 size_t l;
3416
3417 p = smp->data.u.str.area;
3418 for (l = 0; l < smp->data.u.str.data; l++) {
3419 if (*(p+l) == '\r' || *(p+l) == '\n')
3420 break;
3421 }
3422 smp->data.u.str.data = l;
3423 return 1;
3424}
3425
Christopher Faulet51fc9d12020-04-01 17:24:41 +02003426/**/
3427static int sample_conv_ltrim(const struct arg *arg_p, struct sample *smp, void *private)
3428{
3429 char *delimiters, *p;
3430 size_t dlen, l;
3431
3432 delimiters = arg_p[0].data.str.area;
3433 dlen = arg_p[0].data.str.data;
3434
3435 l = smp->data.u.str.data;
3436 p = smp->data.u.str.area;
3437 while (l && memchr(delimiters, *p, dlen) != NULL) {
3438 p++;
3439 l--;
3440 }
3441
3442 smp->data.u.str.area = p;
3443 smp->data.u.str.data = l;
3444 return 1;
3445}
3446
Christopher Faulet568415a2020-04-01 17:24:47 +02003447/**/
3448static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *private)
3449{
3450 char *delimiters, *p;
3451 size_t dlen, l;
3452
3453 delimiters = arg_p[0].data.str.area;
3454 dlen = arg_p[0].data.str.data;
3455
3456 l = smp->data.u.str.data;
3457 p = smp->data.u.str.area + l - 1;
3458 while (l && memchr(delimiters, *p, dlen) != NULL) {
3459 p--;
3460 l--;
3461 }
3462
3463 smp->data.u.str.data = l;
3464 return 1;
3465}
3466
Alex51c8ad42021-04-15 16:45:15 +02003467/* This function checks the "json_query" converter's arguments. */
3468static int sample_check_json_query(struct arg *arg, struct sample_conv *conv,
3469 const char *file, int line, char **err)
3470{
3471 if (arg[0].data.str.data == 0) {
3472 memprintf(err, "json_path must not be empty");
3473 return 0;
3474 }
3475
3476 if (arg[1].data.str.data != 0) {
3477 if (strcmp(arg[1].data.str.area, "int") != 0) {
3478 memprintf(err, "output_type only supports \"int\" as argument");
3479 return 0;
3480 } else {
3481 arg[1].type = ARGT_SINT;
3482 arg[1].data.sint = 0;
3483 }
3484 }
3485 return 1;
3486}
3487
3488/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
3489 * the recommendation for interoperable integers in section 6 of RFC 7159.
3490 */
3491#define JSON_INT_MAX ((1LL << 53) - 1)
3492#define JSON_INT_MIN (-JSON_INT_MAX)
3493
3494/* This sample function get the value from a given json string.
3495 * The mjson library is used to parse the JSON struct
3496 */
3497static int sample_conv_json_query(const struct arg *args, struct sample *smp, void *private)
3498{
3499 struct buffer *trash = get_trash_chunk();
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003500 const char *token; /* holds the temporary string from mjson_find */
3501 int token_size; /* holds the length of <token> */
Alex51c8ad42021-04-15 16:45:15 +02003502
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003503 enum mjson_tok token_type;
Alex51c8ad42021-04-15 16:45:15 +02003504
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003505 token_type = mjson_find(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &token, &token_size);
3506
3507 switch (token_type) {
Alex51c8ad42021-04-15 16:45:15 +02003508 case MJSON_TOK_NUMBER:
3509 if (args[1].type == ARGT_SINT) {
Willy Tarreaufb601952021-05-14 08:51:53 +02003510 smp->data.u.sint = strtoll(token, NULL, 0);
Alex51c8ad42021-04-15 16:45:15 +02003511
3512 if (smp->data.u.sint < JSON_INT_MIN || smp->data.u.sint > JSON_INT_MAX)
3513 return 0;
3514
3515 smp->data.type = SMP_T_SINT;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003516
3517 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003518 } else {
3519 double double_val;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003520
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003521 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 +02003522 return 0;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003523
3524 trash->data = snprintf(trash->area,trash->size,"%g",double_val);
3525 smp->data.u.str = *trash;
3526 smp->data.type = SMP_T_STR;
3527
3528 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003529 }
Alex51c8ad42021-04-15 16:45:15 +02003530 case MJSON_TOK_TRUE:
3531 smp->data.type = SMP_T_BOOL;
3532 smp->data.u.sint = 1;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003533
3534 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003535 case MJSON_TOK_FALSE:
3536 smp->data.type = SMP_T_BOOL;
3537 smp->data.u.sint = 0;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003538
3539 return 1;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003540 case MJSON_TOK_STRING: {
3541 int len;
3542
3543 len = mjson_get_string(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, trash->area, trash->size);
3544
3545 if (len == -1) {
Alex51c8ad42021-04-15 16:45:15 +02003546 /* invalid string */
3547 return 0;
Alex51c8ad42021-04-15 16:45:15 +02003548 }
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003549
3550 trash->data = len;
3551 smp->data.u.str = *trash;
3552 smp->data.type = SMP_T_STR;
3553
3554 return 1;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003555 }
Tim Duesterhus8f3bc8f2021-04-15 18:14:32 +02003556 case MJSON_TOK_NULL:
3557 case MJSON_TOK_ARRAY:
3558 case MJSON_TOK_OBJECT:
3559 /* We cannot handle these. */
3560 return 0;
3561 case MJSON_TOK_INVALID:
3562 /* Nothing matches the query. */
3563 return 0;
3564 case MJSON_TOK_KEY:
3565 /* This is not a valid return value according to the
3566 * mjson documentation, but we handle it to benefit
3567 * from '-Wswitch'.
3568 */
Alex51c8ad42021-04-15 16:45:15 +02003569 return 0;
3570 }
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003571
3572 my_unreachable();
3573 return 0;
Alex51c8ad42021-04-15 16:45:15 +02003574}
3575
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003576#ifdef USE_OPENSSL
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +02003577static int sample_conv_jwt_verify_check(struct arg *args, struct sample_conv *conv,
3578 const char *file, int line, char **err)
3579{
3580 vars_check_arg(&args[0], NULL);
3581 vars_check_arg(&args[1], NULL);
3582
3583 if (args[0].type == ARGT_STR) {
3584 enum jwt_alg alg = jwt_parse_alg(args[0].data.str.area, args[0].data.str.data);
3585
3586 switch(alg) {
3587 case JWT_ALG_DEFAULT:
Tim Duesterhusab896ee2021-10-29 18:06:55 +02003588 memprintf(err, "unknown JWT algorithm: %s", args[0].data.str.area);
3589 return 0;
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +02003590
3591 case JWS_ALG_PS256:
3592 case JWS_ALG_PS384:
3593 case JWS_ALG_PS512:
3594 memprintf(err, "RSASSA-PSS JWS signing not managed yet");
Tim Duesterhusab896ee2021-10-29 18:06:55 +02003595 return 0;
Remi Tricot-Le Breton130e1422021-10-01 15:36:58 +02003596
3597 default:
3598 break;
3599 }
3600 }
3601
3602 if (args[1].type == ARGT_STR) {
3603 jwt_tree_load_cert(args[1].data.str.area, args[1].data.str.data, err);
3604 }
3605
3606 return 1;
3607}
3608
3609/* Check that a JWT's signature is correct */
3610static int sample_conv_jwt_verify(const struct arg *args, struct sample *smp, void *private)
3611{
3612 struct sample alg_smp, key_smp;
3613
3614 smp->data.type = SMP_T_SINT;
3615 smp->data.u.sint = 0;
3616
3617 smp_set_owner(&alg_smp, smp->px, smp->sess, smp->strm, smp->opt);
3618 smp_set_owner(&key_smp, smp->px, smp->sess, smp->strm, smp->opt);
3619 if (!sample_conv_var2smp_str(&args[0], &alg_smp))
3620 return 0;
3621 if (!sample_conv_var2smp_str(&args[1], &key_smp))
3622 return 0;
3623
3624 smp->data.u.sint = jwt_verify(&smp->data.u.str, &alg_smp.data.u.str,
3625 &key_smp.data.u.str);
3626
3627 return 1;
3628}
3629
3630
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003631/*
3632 * Returns the decoded header or payload of a JWT if no parameter is given, or
3633 * the value of the specified field of the corresponding JWT subpart if a
3634 * parameter is given.
3635 */
3636static int sample_conv_jwt_member_query(const struct arg *args, struct sample *smp,
3637 void *private, enum jwt_elt member)
3638{
3639 struct jwt_item items[JWT_ELT_MAX] = { { 0 } };
3640 unsigned int item_num = member + 1; /* We don't need to tokenize the full token */
3641 struct buffer *decoded_header = get_trash_chunk();
3642 int retval = 0;
Willy Tarreaue20e0262021-10-15 12:10:24 +02003643 int ret;
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003644
3645 jwt_tokenize(&smp->data.u.str, items, &item_num);
3646
3647 if (item_num < member + 1)
3648 goto end;
3649
Willy Tarreaue20e0262021-10-15 12:10:24 +02003650 ret = base64urldec(items[member].start, items[member].length,
3651 decoded_header->area, decoded_header->size);
3652 if (ret == -1)
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003653 goto end;
3654
Willy Tarreaue20e0262021-10-15 12:10:24 +02003655 decoded_header->data = ret;
Remi Tricot-Le Breton0a72f5e2021-10-01 15:36:57 +02003656 if (args[0].type != ARGT_STR) {
3657 smp->data.u.str = *decoded_header;
3658 smp->data.type = SMP_T_STR;
3659 goto end;
3660 }
3661
3662 /* We look for a specific field of the header or payload part of the JWT */
3663 smp->data.u.str = *decoded_header;
3664
3665 retval = sample_conv_json_query(args, smp, private);
3666
3667end:
3668 return retval;
3669}
3670
3671/* This function checks the "jwt_header_query" and "jwt_payload_query" converters' arguments.
3672 * It is based on the "json_query" converter's check with the only difference
3673 * being that the jwt converters can take 0 parameters as well.
3674 */
3675static int sample_conv_jwt_query_check(struct arg *arg, struct sample_conv *conv,
3676 const char *file, int line, char **err)
3677{
3678 if (arg[1].data.str.data != 0) {
3679 if (strcmp(arg[1].data.str.area, "int") != 0) {
3680 memprintf(err, "output_type only supports \"int\" as argument");
3681 return 0;
3682 } else {
3683 arg[1].type = ARGT_SINT;
3684 arg[1].data.sint = 0;
3685 }
3686 }
3687 return 1;
3688}
3689
3690/*
3691 * If no parameter is given, return the decoded header part of a JWT (the first
3692 * base64 encoded part, corresponding to the JOSE header).
3693 * If a parameter is given, this converter acts as a "json_query" on this
3694 * decoded JSON.
3695 */
3696static int sample_conv_jwt_header_query(const struct arg *args, struct sample *smp, void *private)
3697{
3698 return sample_conv_jwt_member_query(args, smp, private, JWT_ELT_JOSE);
3699}
3700
3701/*
3702 * If no parameter is given, return the decoded payload part of a JWT (the
3703 * second base64 encoded part, which contains all the claims). If a parameter
3704 * is given, this converter acts as a "json_query" on this decoded JSON.
3705 */
3706static int sample_conv_jwt_payload_query(const struct arg *args, struct sample *smp, void *private)
3707{
3708 return sample_conv_jwt_member_query(args, smp, private, JWT_ELT_CLAIMS);
3709}
3710
3711#endif /* USE_OPENSSL */
Alex51c8ad42021-04-15 16:45:15 +02003712
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003713/************************************************************************/
3714/* All supported sample fetch functions must be declared here */
3715/************************************************************************/
3716
3717/* force TRUE to be returned at the fetch level */
3718static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003719smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003720{
Willy Tarreau280f42b2018-02-19 15:34:12 +01003721 if (!smp_make_rw(smp))
3722 return 0;
3723
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003724 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003725 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003726 return 1;
3727}
3728
3729/* force FALSE to be returned at the fetch level */
3730static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003731smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003732{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003733 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003734 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003735 return 1;
3736}
3737
3738/* retrieve environment variable $1 as a string */
3739static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003740smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003741{
3742 char *env;
3743
Christopher Faulete6e7a582021-01-29 11:29:28 +01003744 if (args[0].type != ARGT_STR)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003745 return 0;
3746
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003747 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003748 if (!env)
3749 return 0;
3750
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003751 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003752 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003753 smp->data.u.str.area = env;
3754 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003755 return 1;
3756}
3757
Damien Claisseae6f1252019-10-30 15:57:28 +00003758/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3759 * optional string representing the unit of the result: "s" for seconds, "ms" for
3760 * milliseconds and "us" for microseconds.
3761 * Returns 0 on error and non-zero if OK.
3762 */
3763int smp_check_date_unit(struct arg *args, char **err)
3764{
3765 if (args[1].type == ARGT_STR) {
Christopher Faulet95917132020-08-05 23:07:07 +02003766 long long int unit;
3767
Damien Claisseae6f1252019-10-30 15:57:28 +00003768 if (strcmp(args[1].data.str.area, "s") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003769 unit = TIME_UNIT_S;
Damien Claisseae6f1252019-10-30 15:57:28 +00003770 }
3771 else if (strcmp(args[1].data.str.area, "ms") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003772 unit = TIME_UNIT_MS;
Damien Claisseae6f1252019-10-30 15:57:28 +00003773 }
3774 else if (strcmp(args[1].data.str.area, "us") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003775 unit = TIME_UNIT_US;
Damien Claisseae6f1252019-10-30 15:57:28 +00003776 }
3777 else {
3778 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
3779 args[1].data.str.area);
3780 return 0;
3781 }
Christopher Faulet95917132020-08-05 23:07:07 +02003782
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003783 chunk_destroy(&args[1].data.str);
Damien Claisseae6f1252019-10-30 15:57:28 +00003784 args[1].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02003785 args[1].data.sint = unit;
Damien Claisseae6f1252019-10-30 15:57:28 +00003786 }
3787 else if (args[1].type != ARGT_STOP) {
3788 memprintf(err, "Unexpected arg type");
3789 return 0;
3790 }
3791
3792 return 1;
3793}
3794
3795/* retrieve the current local date in epoch time, converts it to milliseconds
3796 * or microseconds if asked to in optional args[1] unit param, and applies an
3797 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003798 */
3799static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003800smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003801{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003802 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003803
Damien Claisseae6f1252019-10-30 15:57:28 +00003804 /* report in milliseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003805 if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003806 smp->data.u.sint *= 1000;
3807 smp->data.u.sint += date.tv_usec / 1000;
3808 }
3809 /* report in microseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003810 else if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003811 smp->data.u.sint *= 1000000;
3812 smp->data.u.sint += date.tv_usec;
3813 }
3814
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003815 /* add offset */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003816 if (args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003817 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003818
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003819 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003820 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3821 return 1;
3822}
3823
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003824/* retrieve the current microsecond part of the date */
3825static int
3826smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3827{
3828 smp->data.u.sint = date.tv_usec;
3829 smp->data.type = SMP_T_SINT;
3830 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3831 return 1;
3832}
3833
3834
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003835/* returns the hostname */
3836static int
3837smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3838{
3839 smp->data.type = SMP_T_STR;
3840 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003841 smp->data.u.str.area = hostname;
3842 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003843 return 1;
3844}
3845
Willy Tarreau0f30d262014-11-24 16:02:05 +01003846/* returns the number of processes */
3847static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003848smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003849{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003850 smp->data.type = SMP_T_SINT;
Willy Tarreau91358592021-06-15 08:08:04 +02003851 smp->data.u.sint = 1;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003852 return 1;
3853}
3854
3855/* returns the number of the current process (between 1 and nbproc */
3856static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003857smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003858{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003859 smp->data.type = SMP_T_SINT;
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003860 smp->data.u.sint = 1;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003861 return 1;
3862}
3863
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003864/* returns the number of the current thread (between 1 and nbthread */
3865static int
3866smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3867{
3868 smp->data.type = SMP_T_SINT;
3869 smp->data.u.sint = tid;
3870 return 1;
3871}
3872
Willy Tarreau84310e22014-02-14 11:59:04 +01003873/* generate a random 32-bit integer for whatever purpose, with an optional
3874 * range specified in argument.
3875 */
3876static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003877smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01003878{
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003879 smp->data.u.sint = ha_random32();
Willy Tarreau84310e22014-02-14 11:59:04 +01003880
3881 /* reduce if needed. Don't do a modulo, use all bits! */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003882 if (args[0].type == ARGT_SINT)
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003883 smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32;
Willy Tarreau84310e22014-02-14 11:59:04 +01003884
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003885 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01003886 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3887 return 1;
3888}
3889
Willy Tarreau0f30d262014-11-24 16:02:05 +01003890/* returns true if the current process is stopping */
3891static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003892smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003893{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003894 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003895 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003896 return 1;
3897}
3898
Willy Tarreau70fe9442018-11-22 16:07:39 +01003899/* returns the number of calls of the current stream's process_stream() */
3900static int
3901smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
3902{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003903 if (!smp->strm)
3904 return 0;
3905
Willy Tarreau70fe9442018-11-22 16:07:39 +01003906 smp->data.type = SMP_T_SINT;
3907 smp->data.u.sint = smp->strm->task->calls;
3908 return 1;
3909}
3910
3911/* returns the average number of nanoseconds spent processing the stream per call */
3912static int
3913smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3914{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003915 if (!smp->strm)
3916 return 0;
3917
Willy Tarreau70fe9442018-11-22 16:07:39 +01003918 smp->data.type = SMP_T_SINT;
3919 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
3920 return 1;
3921}
3922
3923/* returns the total number of nanoseconds spent processing the stream */
3924static int
3925smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3926{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003927 if (!smp->strm)
3928 return 0;
3929
Willy Tarreau70fe9442018-11-22 16:07:39 +01003930 smp->data.type = SMP_T_SINT;
3931 smp->data.u.sint = smp->strm->task->cpu_time;
3932 return 1;
3933}
3934
3935/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
3936static int
3937smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3938{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003939 if (!smp->strm)
3940 return 0;
3941
Willy Tarreau70fe9442018-11-22 16:07:39 +01003942 smp->data.type = SMP_T_SINT;
3943 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
3944 return 1;
3945}
3946
3947/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
3948static int
3949smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3950{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003951 if (!smp->strm)
3952 return 0;
3953
Willy Tarreau70fe9442018-11-22 16:07:39 +01003954 smp->data.type = SMP_T_SINT;
3955 smp->data.u.sint = smp->strm->task->lat_time;
3956 return 1;
3957}
3958
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003959static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
3960{
3961 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003962 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003963 smp->data.u.str.area = args[0].data.str.area;
3964 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003965 return 1;
3966}
3967
3968static int smp_check_const_bool(struct arg *args, char **err)
3969{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003970 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
3971 strcasecmp(args[0].data.str.area, "1") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003972 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003973 args[0].type = ARGT_SINT;
3974 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003975 return 1;
3976 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003977 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
3978 strcasecmp(args[0].data.str.area, "0") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003979 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003980 args[0].type = ARGT_SINT;
3981 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003982 return 1;
3983 }
3984 memprintf(err, "Expects 'true', 'false', '0' or '1'");
3985 return 0;
3986}
3987
3988static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
3989{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003990 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003991 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003992 return 1;
3993}
3994
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003995static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003996{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003997 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003998 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003999 return 1;
4000}
4001
4002static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
4003{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004004 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004005 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004006 return 1;
4007}
4008
4009static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
4010{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004011 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004012 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004013 return 1;
4014}
4015
4016static int smp_check_const_bin(struct arg *args, char **err)
4017{
David Carlier64a16ab2016-04-08 10:37:02 +01004018 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004019 int binstrlen;
4020
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004021 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004022 return 0;
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004023 chunk_destroy(&args[0].data.str);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004024 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004025 args[0].data.str.area = binstr;
4026 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004027 return 1;
4028}
4029
4030static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
4031{
4032 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004033 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004034 smp->data.u.str.area = args[0].data.str.area;
4035 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004036 return 1;
4037}
4038
4039static int smp_check_const_meth(struct arg *args, char **err)
4040{
4041 enum http_meth_t meth;
4042 int i;
4043
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004044 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004045 if (meth != HTTP_METH_OTHER) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004046 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004047 args[0].type = ARGT_SINT;
4048 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004049 } else {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05004050 /* Check method avalaibility. A method is a token defined as :
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004051 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
4052 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
4053 * token = 1*tchar
4054 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004055 for (i = 0; i < args[0].data.str.data; i++) {
4056 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004057 memprintf(err, "expects valid method.");
4058 return 0;
4059 }
4060 }
4061 }
4062 return 1;
4063}
4064
4065static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
4066{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004067 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004068 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004069 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004070 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004071 smp->data.u.meth.str.area = "";
4072 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004073 } else {
4074 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004075 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004076 smp->data.u.meth.str.area = args[0].data.str.area;
4077 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004078 }
4079 return 1;
4080}
4081
Luca Schimweg8a694b82019-09-10 15:42:52 +02004082// This function checks the "uuid" sample's arguments.
4083// Function won't get called when no parameter is specified (maybe a bug?)
4084static int smp_check_uuid(struct arg *args, char **err)
4085{
4086 if (!args[0].type) {
4087 args[0].type = ARGT_SINT;
4088 args[0].data.sint = 4;
4089 }
4090 else if (args[0].data.sint != 4) {
4091 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
4092 return 0;
4093 }
4094
4095 return 1;
4096}
4097
4098// Generate a RFC4122 UUID (default is v4 = fully random)
4099static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
4100{
4101 if (args[0].data.sint == 4 || !args[0].type) {
Willy Tarreauee3bcdd2020-03-08 17:48:17 +01004102 ha_generate_uuid(&trash);
Luca Schimweg8a694b82019-09-10 15:42:52 +02004103 smp->data.type = SMP_T_STR;
4104 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4105 smp->data.u.str = trash;
4106 return 1;
4107 }
4108
4109 // more implementations of other uuid formats possible here
4110 return 0;
4111}
4112
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004113/* Note: must not be declared <const> as its list will be overwritten.
4114 * Note: fetches that may return multiple types must be declared as the lowest
4115 * common denominator, the type that can be casted into all other ones. For
4116 * instance IPv4/IPv6 must be declared IPv4.
4117 */
4118static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau0209c972021-03-26 12:03:11 +01004119 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4120 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4121 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_CONST },
4122 { "date", smp_fetch_date, ARG2(0,SINT,STR), smp_check_date_unit, SMP_T_SINT, SMP_USE_CONST },
4123 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4124 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_CONST },
4125 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_CONST },
4126 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4127 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4128 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_CONST },
Willy Tarreau0f30d262014-11-24 16:02:05 +01004129 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau0209c972021-03-26 12:03:11 +01004130 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_CONST },
Willy Tarreau70fe9442018-11-22 16:07:39 +01004131
4132 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4133 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4134 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4135 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4136 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004137
Willy Tarreau0209c972021-03-26 12:03:11 +01004138 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_CONST },
4139 { "bool", smp_fetch_const_bool, ARG1(1,STR), smp_check_const_bool, SMP_T_BOOL, SMP_USE_CONST },
4140 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_CONST },
4141 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_CONST },
4142 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_CONST },
4143 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_CONST },
4144 { "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 +02004145
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004146 { /* END */ },
4147}};
4148
Willy Tarreau0108d902018-11-25 19:14:37 +01004149INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
4150
Emeric Brun107ca302010-01-04 16:16:05 +01004151/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02004152static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Moemen MHEDHBI848216f2021-04-02 01:05:07 +02004153 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
4154 { "b64dec", sample_conv_base642bin, 0, NULL, SMP_T_STR, SMP_T_BIN },
4155 { "base64", sample_conv_bin2base64, 0, NULL, SMP_T_BIN, SMP_T_STR },
4156 { "ub64enc", sample_conv_bin2base64url,0, NULL, SMP_T_BIN, SMP_T_STR },
4157 { "ub64dec", sample_conv_base64url2bin,0, NULL, SMP_T_STR, SMP_T_BIN },
4158 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
4159 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
4160 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Marcin Deranek40ca09c2021-07-13 14:05:24 +02004161 { "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 +02004162 { "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 +02004163 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
4164 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
4165 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
4166 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4167 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4168 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4169 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4170 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4171 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4172 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4173 { "xxh3", sample_conv_xxh3, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4174 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4175 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4176 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
4177 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
4178 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4179 { "word", sample_conv_word, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4180 { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
4181 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Willy Tarreau99ea1882021-10-06 15:37:17 +02004182 { "concat", sample_conv_concat, ARG3(1,STR,STR,STR), smp_check_concat, SMP_T_STR, SMP_T_STR },
4183 { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT },
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01004184
4185 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01004186 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
4187 { "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 +01004188
Baptiste Assmanne138dda2020-10-22 15:39:03 +02004189 /* FIX converters */
4190 { "fix_is_valid", sample_conv_fix_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4191 { "fix_tag_value", sample_conv_fix_tag_value, ARG1(1,STR), sample_conv_fix_value_check, SMP_T_BIN, SMP_T_BIN },
4192
Baptiste Assmanne279ca62020-10-27 18:10:06 +01004193 /* MQTT converters */
4194 { "mqtt_is_valid", sample_conv_mqtt_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4195 { "mqtt_field_value", sample_conv_mqtt_field_value, ARG2(2,STR,STR), sample_conv_mqtt_field_value_check, SMP_T_BIN, SMP_T_STR },
4196
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02004197 { "iif", sample_conv_iif, ARG2(2, STR, STR), NULL, SMP_T_BOOL, SMP_T_STR },
4198
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02004199 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4200 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4201 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4202 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
4203 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4204 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4205 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4206 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4207 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4208 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4209 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4210 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4211 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4212 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01004213
Christopher Fauletea159d62020-04-01 16:21:44 +02004214 { "htonl", sample_conv_htonl, 0, NULL, SMP_T_SINT, SMP_T_BIN },
4215 { "cut_crlf", sample_conv_cut_crlf, 0, NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet51fc9d12020-04-01 17:24:41 +02004216 { "ltrim", sample_conv_ltrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet568415a2020-04-01 17:24:47 +02004217 { "rtrim", sample_conv_rtrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Alex51c8ad42021-04-15 16:45:15 +02004218 { "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 +02004219
4220#ifdef USE_OPENSSL
4221 /* JSON Web Token converters */
4222 { "jwt_header_query", sample_conv_jwt_header_query, ARG2(0,STR,STR), sample_conv_jwt_query_check, SMP_T_BIN, SMP_T_ANY },
4223 { "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 +02004224 { "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 +02004225#endif
Willy Tarreau9fcb9842012-04-20 14:45:49 +02004226 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01004227}};
4228
Willy Tarreau0108d902018-11-25 19:14:37 +01004229INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);