blob: a0727d236059fc0cddaeed12989abf5c9acdc76d [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>
21#include <import/xxhash.h>
Willy Tarreau7e2c6472012-10-29 20:44:36 +010022
Willy Tarreaub2551052020-06-09 09:07:15 +020023#include <haproxy/api.h>
24#include <haproxy/arg.h>
Willy Tarreauac13aea2020-06-04 10:36:03 +020025#include <haproxy/auth.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/base64.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020027#include <haproxy/buf.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020028#include <haproxy/chunk.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>
Thierry FOURNIER01e09742016-12-26 11:46:11 +010046
Willy Tarreau1cf8f082014-02-07 12:14:54 +010047/* sample type names */
48const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020049 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010050 [SMP_T_BOOL] = "bool",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010051 [SMP_T_SINT] = "sint",
52 [SMP_T_ADDR] = "addr",
53 [SMP_T_IPV4] = "ipv4",
54 [SMP_T_IPV6] = "ipv6",
55 [SMP_T_STR] = "str",
56 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020057 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010058};
59
Willy Tarreau12785782012-04-27 21:37:17 +020060/* static sample used in sample_process() when <p> is NULL */
Emeric Brune5c918b2017-06-14 14:15:36 +020061static THREAD_LOCAL struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010062
Willy Tarreau12785782012-04-27 21:37:17 +020063/* list head of all known sample fetch keywords */
64static struct sample_fetch_kw_list sample_fetches = {
65 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010066};
67
Willy Tarreau12785782012-04-27 21:37:17 +020068/* list head of all known sample format conversion keywords */
69static struct sample_conv_kw_list sample_convs = {
70 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010071};
72
Willy Tarreau80aca902013-01-07 15:42:20 +010073const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
Willy Tarreaube2159b2021-03-26 11:56:11 +010074 [SMP_SRC_CONST] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
75 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
76 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
77 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
78 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
79 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +010080 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL_CFG_PARSER |
81 SMP_VAL_CLI_PARSER ),
Willy Tarreaube2159b2021-03-26 11:56:11 +010082
Willy Tarreau80aca902013-01-07 15:42:20 +010083 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
84 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
85 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
86 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
87 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
88 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +010089 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
90 SMP_VAL_CLI_PARSER ),
Willy Tarreau80aca902013-01-07 15:42:20 +010091
92 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
93 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
94 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
95 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
96 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
97 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +010098 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
99 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100100
101 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
102 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
103 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
104 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
105 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
106 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100107 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
108 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100109
110 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
111 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
112 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
113 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
114 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
115 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100116 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
117 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100118
119 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
120 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
121 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
122 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
123 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
124 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100125 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
126 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100127
128 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
129 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
130 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
131 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
132 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
133 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100134 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
135 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100136
137 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
138 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
139 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
140 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
141 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
142 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100143 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
144 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100145
146 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
147 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
148 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
149 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
150 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
151 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100152 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
153 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100154
155 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
156 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
157 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
158 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
159 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
160 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100161 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
162 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100163
164 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
165 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
166 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
167 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
168 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
169 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100170 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
171 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100172
173 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
174 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
175 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
176 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
177 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
178 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100179 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
180 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100181
182 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
183 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
184 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
185 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
186 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
187 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100188 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
189 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100190
191 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
192 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
193 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
194 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
195 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
196 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100197 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
198 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100199
200 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
201 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
202 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
203 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
204 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
205 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100206 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
207 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100208
209 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
210 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
211 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
212 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
213 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
214 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100215 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
216 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100217
218 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
219 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
220 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
221 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
222 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
223 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100224 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
225 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100226
227 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
228 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
229 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
230 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
231 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
232 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100233 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
234 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100235
236 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
237 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
238 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
239 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
240 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
241 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100242 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
243 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100244
245 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
246 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
247 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___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100251 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
252 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100253
254 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
255 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
256 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___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100260 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
261 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100262
263 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
264 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
265 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___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100269 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
270 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100271
272 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
273 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
274 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___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100278 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
279 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100280};
281
282static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
283 [SMP_SRC_INTRN] = "internal state",
284 [SMP_SRC_LISTN] = "listener",
285 [SMP_SRC_FTEND] = "frontend",
286 [SMP_SRC_L4CLI] = "client address",
287 [SMP_SRC_L5CLI] = "client-side connection",
288 [SMP_SRC_TRACK] = "track counters",
289 [SMP_SRC_L6REQ] = "request buffer",
290 [SMP_SRC_HRQHV] = "HTTP request headers",
291 [SMP_SRC_HRQHP] = "HTTP request",
292 [SMP_SRC_HRQBO] = "HTTP request body",
293 [SMP_SRC_BKEND] = "backend",
294 [SMP_SRC_SERVR] = "server",
295 [SMP_SRC_L4SRV] = "server address",
296 [SMP_SRC_L5SRV] = "server-side connection",
297 [SMP_SRC_L6RES] = "response buffer",
298 [SMP_SRC_HRSHV] = "HTTP response headers",
299 [SMP_SRC_HRSHP] = "HTTP response",
300 [SMP_SRC_HRSBO] = "HTTP response body",
301 [SMP_SRC_RQFIN] = "request buffer statistics",
302 [SMP_SRC_RSFIN] = "response buffer statistics",
303 [SMP_SRC_TXFIN] = "transaction statistics",
304 [SMP_SRC_SSFIN] = "session statistics",
305};
306
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100307static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
308 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
309 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
310 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
311 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
312 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
313 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
314 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
315 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
316 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
317 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
318 [SMP_CKP_BE_SRV_CON] = "server source selection",
319 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
320 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
321 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
322 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
323 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
324 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
325 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
326 [SMP_CKP_FE_LOG_END] = "logs",
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100327 [SMP_CKP_BE_CHK_RUL] = "backend tcp-check rule",
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100328};
329
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100330/* This function returns the type of the data returned by the sample_expr.
331 * It assumes that the <expr> and all of its converters are properly
332 * initialized.
333 */
334inline
335int smp_expr_output_type(struct sample_expr *expr)
336{
337 struct sample_conv_expr *smp_expr;
338
339 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
340 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
341 return smp_expr->conv->out_type;
342 }
343 return expr->fetch->out_type;
344}
345
346
Willy Tarreau80aca902013-01-07 15:42:20 +0100347/* fill the trash with a comma-delimited list of source names for the <use> bit
348 * field which must be composed of a non-null set of SMP_USE_* flags. The return
349 * value is the pointer to the string in the trash buffer.
350 */
351const char *sample_src_names(unsigned int use)
352{
353 int bit;
354
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200355 trash.data = 0;
356 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100357 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
358 if (!(use & ~((1 << bit) - 1)))
359 break; /* no more bits */
360
361 if (!(use & (1 << bit)))
362 continue; /* bit not set */
363
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200364 trash.data += snprintf(trash.area + trash.data,
365 trash.size - trash.data, "%s%s",
366 (use & ((1 << bit) - 1)) ? "," : "",
367 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100368 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200369 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100370}
371
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100372/* return a pointer to the correct sample checkpoint name, or "unknown" when
373 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
374 * if set.
375 */
376const char *sample_ckp_names(unsigned int use)
377{
378 int bit;
379
380 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
381 if (use & (1 << bit))
382 return fetch_ckp_names[bit];
383 return "unknown sample check place, please report this bug";
384}
385
Emeric Brun107ca302010-01-04 16:16:05 +0100386/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100387 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
388 * for next parsing sessions. The fetch keywords capabilities are also computed
389 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100390 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100391void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100392{
Willy Tarreau80aca902013-01-07 15:42:20 +0100393 struct sample_fetch *sf;
394 int bit;
395
396 for (sf = kwl->kw; sf->kw != NULL; sf++) {
397 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
398 if (sf->use & (1 << bit))
399 sf->val |= fetch_cap[bit];
400 }
Willy Tarreau2b718102021-04-21 07:32:39 +0200401 LIST_APPEND(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100402}
403
404/*
Willy Tarreau12785782012-04-27 21:37:17 +0200405 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100406 * parsing sessions.
407 */
Willy Tarreau12785782012-04-27 21:37:17 +0200408void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100409{
Willy Tarreau2b718102021-04-21 07:32:39 +0200410 LIST_APPEND(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100411}
412
413/*
Willy Tarreau12785782012-04-27 21:37:17 +0200414 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100415 * string of <len> in buffer <kw>.
416 *
417 */
Willy Tarreau12785782012-04-27 21:37:17 +0200418struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100419{
420 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200421 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100422
Willy Tarreau12785782012-04-27 21:37:17 +0200423 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100424 for (index = 0; kwl->kw[index].kw != NULL; index++) {
425 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
426 kwl->kw[index].kw[len] == '\0')
427 return &kwl->kw[index];
428 }
429 }
430 return NULL;
431}
432
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100433/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100434 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100435 * <idx> is the index of the next sample fetch entry. It is used as private
436 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100437 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100438 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100439 * the list is reached.
440 */
441struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
442{
443 struct sample_fetch_kw_list *kwl;
444 struct sample_fetch *base;
445
446 if (!current) {
447 /* Get first kwl entry. */
448 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
449 (*idx) = 0;
450 } else {
451 /* Get kwl corresponding to the curret entry. */
452 base = current + 1 - (*idx);
453 kwl = container_of(base, struct sample_fetch_kw_list, kw);
454 }
455
456 while (1) {
457
458 /* Check if kwl is the last entry. */
459 if (&kwl->list == &sample_fetches.list)
460 return NULL;
461
462 /* idx contain the next keyword. If it is available, return it. */
463 if (kwl->kw[*idx].kw) {
464 (*idx)++;
465 return &kwl->kw[(*idx)-1];
466 }
467
468 /* get next entry in the main list, and return NULL if the end is reached. */
469 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
470
471 /* Set index to 0, ans do one other loop. */
472 (*idx) = 0;
473 }
474}
475
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100476/* This function browses the list of available converters. <current> is
477 * the last used converter. If it is the first call, it must set to NULL.
478 * <idx> is the index of the next converter entry. It is used as private
479 * value. It is useless to initiate it.
480 *
481 * It returns always the next sample_conv entry, and NULL when the end of
482 * the list is reached.
483 */
484struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
485{
486 struct sample_conv_kw_list *kwl;
487 struct sample_conv *base;
488
489 if (!current) {
490 /* Get first kwl entry. */
491 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
492 (*idx) = 0;
493 } else {
494 /* Get kwl corresponding to the curret entry. */
495 base = current + 1 - (*idx);
496 kwl = container_of(base, struct sample_conv_kw_list, kw);
497 }
498
499 while (1) {
500 /* Check if kwl is the last entry. */
501 if (&kwl->list == &sample_convs.list)
502 return NULL;
503
504 /* idx contain the next keyword. If it is available, return it. */
505 if (kwl->kw[*idx].kw) {
506 (*idx)++;
507 return &kwl->kw[(*idx)-1];
508 }
509
510 /* get next entry in the main list, and return NULL if the end is reached. */
511 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
512
513 /* Set index to 0, ans do one other loop. */
514 (*idx) = 0;
515 }
516}
517
Emeric Brun107ca302010-01-04 16:16:05 +0100518/*
Willy Tarreau12785782012-04-27 21:37:17 +0200519 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100520 * string of <len> in buffer <kw>.
521 *
522 */
Willy Tarreau12785782012-04-27 21:37:17 +0200523struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100524{
525 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200526 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100527
Willy Tarreau12785782012-04-27 21:37:17 +0200528 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100529 for (index = 0; kwl->kw[index].kw != NULL; index++) {
530 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
531 kwl->kw[index].kw[len] == '\0')
532 return &kwl->kw[index];
533 }
534 }
535 return NULL;
536}
537
Emeric Brun107ca302010-01-04 16:16:05 +0100538/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200539/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100540/******************************************************************/
541
Willy Tarreau342acb42012-04-23 22:03:39 +0200542static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100543{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200544 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200545 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100546 return 1;
547}
548
Willy Tarreau342acb42012-04-23 22:03:39 +0200549static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100550{
Willy Tarreau83061a82018-07-13 11:56:34 +0200551 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100552
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200553 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100554 return 0;
555
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200556 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200557 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200558 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100559 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100560
561 return 1;
562}
563
Willy Tarreau342acb42012-04-23 22:03:39 +0200564static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100565{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200566 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200567 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100568 return 1;
569}
570
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200571static int c_ipv62ip(struct sample *smp)
572{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200573 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200574 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100575 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200576 return 1;
577}
578
Willy Tarreau342acb42012-04-23 22:03:39 +0200579static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100580{
Willy Tarreau83061a82018-07-13 11:56:34 +0200581 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100582
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200583 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100584 return 0;
585
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200586 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200587 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200588 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100589 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100590 return 1;
591}
592
593/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200594static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100595{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200596 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100597}
598*/
599
Willy Tarreau342acb42012-04-23 22:03:39 +0200600static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100601{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200602 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200603 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100604 return 1;
605}
606
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200607static int c_int2ipv6(struct sample *smp)
608{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200609 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
610 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200611 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200612 return 1;
613}
614
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100615static int c_str2addr(struct sample *smp)
616{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200617 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
618 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100619 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200620 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100621 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100622 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100623 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200624 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100625 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100626 return 1;
627}
628
Willy Tarreau342acb42012-04-23 22:03:39 +0200629static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100630{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200631 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100632 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200633 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100634 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100635 return 1;
636}
637
Willy Tarreau342acb42012-04-23 22:03:39 +0200638static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100639{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200640 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100641 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200642 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100643 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100644 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100645}
646
Emeric Brun4b9e8022014-11-03 18:17:10 +0100647/*
648 * The NULL char always enforces the end of string if it is met.
649 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100650 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200651static int c_bin2str(struct sample *smp)
652{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100653 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200654
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200655 for (i = 0; i < smp->data.u.str.data; i++) {
656 if (!smp->data.u.str.area[i]) {
657 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100658 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100659 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200660 }
Christopher Faulet472ad512020-04-30 09:57:40 +0200661 smp->data.type = SMP_T_STR;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200662 return 1;
663}
664
Willy Tarreau342acb42012-04-23 22:03:39 +0200665static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100666{
Willy Tarreau83061a82018-07-13 11:56:34 +0200667 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100668 char *pos;
669
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200670 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100671 if (!pos)
672 return 0;
673
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200674 trash->size = trash->size - (pos - trash->area);
675 trash->area = pos;
676 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200677 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200678 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100679 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100680 return 1;
681}
682
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800683/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200684 * For strings and binary blocks, it also provides a known allocated size with
685 * a length that is capped to the size, and ensures a trailing zero is always
686 * appended for strings. This is necessary for some operations which may
687 * require to extend the length. It returns 0 if it fails, 1 on success.
688 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100689int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200690{
Willy Tarreau83061a82018-07-13 11:56:34 +0200691 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200692
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200693 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100694 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100695 case SMP_T_SINT:
696 case SMP_T_ADDR:
697 case SMP_T_IPV4:
698 case SMP_T_IPV6:
699 /* These type are not const. */
700 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200701
Christopher Fauletec100512017-07-24 15:38:41 +0200702 case SMP_T_METH:
703 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
704 break;
705 /* Fall through */
706
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100707 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200708 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100709 trash->data = smp->data.type == SMP_T_STR ?
710 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200711 if (trash->data > trash->size - 1)
712 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200713
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100714 memcpy(trash->area, smp->data.type == SMP_T_STR ?
715 smp->data.u.str.area : smp->data.u.meth.str.area,
716 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200717 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200718 smp->data.u.str = *trash;
719 break;
720
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100721 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100722 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200723 trash->data = smp->data.u.str.data;
724 if (trash->data > trash->size)
725 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200726
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200727 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200728 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100729 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200730
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100731 default:
732 /* Other cases are unexpected. */
733 return 0;
734 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100735
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100736 /* remove const flag */
737 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200738 return 1;
739}
740
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100741int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100742{
743 return 1;
744}
745
Willy Tarreau342acb42012-04-23 22:03:39 +0200746static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100747{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200748 const char *str;
749 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100750
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200751 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100752 return 0;
753
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200754 str = smp->data.u.str.area;
755 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100756
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200757 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200758 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100759 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100760 return 1;
761}
762
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100763static int c_str2meth(struct sample *smp)
764{
765 enum http_meth_t meth;
766 int len;
767
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200768 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100769 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200770 len = smp->data.u.str.data;
771 smp->data.u.meth.str.area = smp->data.u.str.area;
772 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100773 }
774 else
775 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200776 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200777 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100778 return 1;
779}
780
781static int c_meth2str(struct sample *smp)
782{
783 int len;
784 enum http_meth_t meth;
785
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200786 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100787 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200788 len = smp->data.u.meth.str.data;
789 smp->data.u.str.area = smp->data.u.meth.str.area;
790 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200791 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100792 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200793 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100794 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200795 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200796 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200797 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100798 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200799 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100800 }
801 else {
802 /* Unknown method */
803 return 0;
804 }
805 return 1;
806}
807
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200808static int c_addr2bin(struct sample *smp)
809{
Willy Tarreau83061a82018-07-13 11:56:34 +0200810 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200811
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200812 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200813 chk->data = 4;
814 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200815 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200816 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200817 chk->data = 16;
818 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200819 }
820 else
821 return 0;
822
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200823 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200824 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200825 return 1;
826}
827
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200828static int c_int2bin(struct sample *smp)
829{
Willy Tarreau83061a82018-07-13 11:56:34 +0200830 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200831
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200832 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
833 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200834
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200835 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200836 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200837 return 1;
838}
839
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200840
Emeric Brun107ca302010-01-04 16:16:05 +0100841/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200842/* Sample casts matrix: */
843/* sample_casts[from type][to type] */
844/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100845/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100846
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100847sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200848/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
849/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
850/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200851/* 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 +0200852/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
853/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200854/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200855/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
856/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
857/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200858};
Emeric Brun107ca302010-01-04 16:16:05 +0100859
Emeric Brun107ca302010-01-04 16:16:05 +0100860/*
Willy Tarreau12785782012-04-27 21:37:17 +0200861 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100862 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200863 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200864 * The caller must have set al->ctx.
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100865 * If <endptr> is non-nul, it will be set to the first unparsed character
866 * (which may be the final '\0') on success. If it is nul, the expression
867 * must be properly terminated by a '\0' otherwise an error is reported.
Emeric Brun107ca302010-01-04 16:16:05 +0100868 */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100869struct 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 +0100870{
Willy Tarreau833cc792013-07-24 15:34:19 +0200871 const char *begw; /* beginning of word */
872 const char *endw; /* end of word */
873 const char *endt; /* end of term */
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +0100874 struct sample_expr *expr = NULL;
Willy Tarreau12785782012-04-27 21:37:17 +0200875 struct sample_fetch *fetch;
876 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100877 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200878 char *fkw = NULL;
879 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100880 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100881
Willy Tarreau833cc792013-07-24 15:34:19 +0200882 begw = str[*idx];
Willy Tarreaued2c6622020-02-14 18:27:10 +0100883 for (endw = begw; is_idchar(*endw); endw++)
884 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200885
886 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100887 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100888 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200889 }
Emeric Brun107ca302010-01-04 16:16:05 +0100890
Willy Tarreau833cc792013-07-24 15:34:19 +0200891 /* keep a copy of the current fetch keyword for error reporting */
892 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100893
Willy Tarreau833cc792013-07-24 15:34:19 +0200894 fetch = find_sample_fetch(begw, endw - begw);
895 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100896 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100897 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200898 }
Emeric Brun107ca302010-01-04 16:16:05 +0100899
Willy Tarreau833cc792013-07-24 15:34:19 +0200900 /* At this point, we have :
901 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100902 * - endw : end of the keyword, first character not part of keyword
Willy Tarreau833cc792013-07-24 15:34:19 +0200903 */
904
905 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100906 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100907 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200908 }
Emeric Brun107ca302010-01-04 16:16:05 +0100909 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200910
Vincent Bernat02779b62016-04-03 13:48:43 +0200911 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200912 if (!expr)
913 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100914
915 LIST_INIT(&(expr->conv_exprs));
916 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200917 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100918
Willy Tarreau689a1df2013-12-13 00:40:11 +0100919 /* Note that we call the argument parser even with an empty string,
920 * this allows it to automatically create entries for mandatory
921 * implicit arguments (eg: local proxy name).
922 */
923 al->kw = expr->fetch->kw;
924 al->conv = NULL;
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100925 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 +0100926 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
927 goto out_error;
928 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200929
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100930 /* now endt is our first char not part of the arg list, typically the
931 * comma after the sample fetch name or after the closing parenthesis,
932 * or the NUL char.
933 */
934
Willy Tarreau689a1df2013-12-13 00:40:11 +0100935 if (!expr->arg_p) {
936 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200937 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100938 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
939 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200940 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100941 }
942
Willy Tarreau833cc792013-07-24 15:34:19 +0200943 /* Now process the converters if any. We have two supported syntaxes
944 * for the converters, which can be combined :
945 * - comma-delimited list of converters just after the keyword and args ;
946 * - one converter per keyword
947 * The combination allows to have each keyword being a comma-delimited
948 * series of converters.
949 *
950 * We want to process the former first, then the latter. For this we start
951 * from the beginning of the supposed place in the exiting conv chain, which
952 * starts at the last comma (endt).
953 */
954
955 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200956 struct sample_conv_expr *conv_expr;
Willy Tarreau46dfd782019-12-17 10:25:29 +0100957 int err_arg;
958 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +0100959
Willy Tarreau833cc792013-07-24 15:34:19 +0200960 if (*endt && *endt != ',') {
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100961 if (endptr) {
962 /* end found, let's stop here */
963 break;
964 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200965 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100966 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200967 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100968 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100969 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200970 }
Emeric Brun107ca302010-01-04 16:16:05 +0100971
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100972 /* FIXME: how long should we support such idiocies ? Maybe we
973 * should already warn ?
974 */
Willy Tarreau833cc792013-07-24 15:34:19 +0200975 while (*endt == ',') /* then trailing commas */
976 endt++;
977
Willy Tarreau97108e02016-11-25 07:33:24 +0100978 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200979
980 if (!*begw) {
981 /* none ? skip to next string */
982 (*idx)++;
983 begw = str[*idx];
984 if (!begw || !*begw)
985 break;
986 }
987
Willy Tarreaued2c6622020-02-14 18:27:10 +0100988 for (endw = begw; is_idchar(*endw); endw++)
989 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200990
991 free(ckw);
992 ckw = my_strndup(begw, endw - begw);
993
994 conv = find_sample_conv(begw, endw - begw);
995 if (!conv) {
996 /* we found an isolated keyword that we don't know, it's not ours */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100997 if (begw == str[*idx]) {
998 endt = begw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200999 break;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001000 }
Willy Tarreau97108e02016-11-25 07:33:24 +01001001 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +02001002 goto out_error;
1003 }
Emeric Brun107ca302010-01-04 16:16:05 +01001004
Willy Tarreau833cc792013-07-24 15:34:19 +02001005 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001006 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001007 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001008 }
Emeric Brun107ca302010-01-04 16:16:05 +01001009
1010 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +02001011 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001012 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001013 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001014 }
Emeric Brun107ca302010-01-04 16:16:05 +01001015
1016 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +02001017 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +02001018 if (!conv_expr)
1019 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001020
Willy Tarreau2b718102021-04-21 07:32:39 +02001021 LIST_APPEND(&(expr->conv_exprs), &(conv_expr->list));
Emeric Brun107ca302010-01-04 16:16:05 +01001022 conv_expr->conv = conv;
1023
Willy Tarreau46dfd782019-12-17 10:25:29 +01001024 al->kw = expr->fetch->kw;
1025 al->conv = conv_expr->conv->kw;
Willy Tarreau80b53ff2020-02-14 08:40:37 +01001026 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 +01001027 if (argcnt < 0) {
1028 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
1029 goto out_error;
1030 }
Willy Tarreau9e92d322010-01-26 17:58:06 +01001031
Willy Tarreau46dfd782019-12-17 10:25:29 +01001032 if (argcnt && !conv->arg_mask) {
1033 memprintf(err_msg, "converter '%s' does not support any args", ckw);
1034 goto out_error;
1035 }
Willy Tarreau21d68a62012-04-20 15:52:36 +02001036
Willy Tarreau46dfd782019-12-17 10:25:29 +01001037 if (!conv_expr->arg_p)
1038 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001039
Willy Tarreau46dfd782019-12-17 10:25:29 +01001040 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1041 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001042 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001043 }
1044 }
Emeric Brun485479d2010-09-23 18:02:19 +02001045
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001046 if (endptr) {
1047 /* end found, let's stop here */
1048 *endptr = (char *)endt;
1049 }
1050
Willy Tarreau833cc792013-07-24 15:34:19 +02001051 out:
1052 free(fkw);
1053 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001054 return expr;
1055
1056out_error:
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +01001057 release_sample_expr(expr);
Willy Tarreau833cc792013-07-24 15:34:19 +02001058 expr = NULL;
1059 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001060}
1061
1062/*
Willy Tarreau12785782012-04-27 21:37:17 +02001063 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001064 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001065 * Returns a pointer on a typed sample structure containing the result or NULL if
1066 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001067 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001068 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001069 *
1070 * Note: the fetch functions are required to properly set the return type. The
1071 * conversion functions must do so too. However the cast functions do not need
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001072 * to since they're made to cast multiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001073 *
1074 * The caller may indicate in <opt> if it considers the result final or not.
1075 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1076 * if the result is stable or not, according to the following table :
1077 *
1078 * return MAY_CHANGE FINAL Meaning for the sample
1079 * NULL 0 * Not present and will never be (eg: header)
1080 * NULL 1 0 Not present yet, could change (eg: POST param)
1081 * NULL 1 1 Not present yet, will not change anymore
1082 * smp 0 * Present and will not change (eg: header)
1083 * smp 1 0 Present, may change (eg: request length)
1084 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001085 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001086struct sample *sample_process(struct proxy *px, struct session *sess,
1087 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001088 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001089{
Willy Tarreau12785782012-04-27 21:37:17 +02001090 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001091
Willy Tarreau18387e22013-07-25 12:02:38 +02001092 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001093 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001094 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001095 }
Emeric Brun107ca302010-01-04 16:16:05 +01001096
Willy Tarreau1777ea62016-03-10 16:15:46 +01001097 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001098 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001099 return NULL;
1100
Emeric Brun107ca302010-01-04 16:16:05 +01001101 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001102 /* we want to ensure that p->type can be casted into
1103 * conv_expr->conv->in_type. We have 3 possibilities :
1104 * - NULL => not castable.
1105 * - c_none => nothing to do (let's optimize it)
1106 * - other => apply cast and prepare to fail
1107 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001108 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001109 return NULL;
1110
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001111 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1112 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001113 return NULL;
1114
Willy Tarreau12e50112012-04-25 17:21:49 +02001115 /* OK cast succeeded */
1116
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001117 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001118 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001119 }
1120 return p;
1121}
1122
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001123/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001124 * Resolve all remaining arguments in proxy <p>. Returns the number of
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001125 * errors or 0 if everything is fine. If at least one error is met, it will
1126 * be appended to *err. If *err==NULL it will be allocated first.
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001127 */
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001128int smp_resolve_args(struct proxy *p, char **err)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001129{
1130 struct arg_list *cur, *bak;
1131 const char *ctx, *where;
1132 const char *conv_ctx, *conv_pre, *conv_pos;
1133 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001134 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001135 struct arg *arg;
1136 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001137 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001138
1139 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1140 struct proxy *px;
1141 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001142 struct stktable *t;
1143 char *pname, *sname, *stktname;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001144 char *err2;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001145
1146 arg = cur->arg;
1147
1148 /* prepare output messages */
1149 conv_pre = conv_pos = conv_ctx = "";
1150 if (cur->conv) {
1151 conv_ctx = cur->conv;
1152 conv_pre = "conversion keyword '";
1153 conv_pos = "' for ";
1154 }
1155
1156 where = "in";
1157 ctx = "sample fetch keyword";
1158 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001159 case ARGC_STK: where = "in stick rule in"; break;
1160 case ARGC_TRK: where = "in tracking rule in"; break;
1161 case ARGC_LOG: where = "in log-format string in"; break;
1162 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
Willy Tarreau57467b82021-09-02 19:43:20 +02001163 case ARGC_HRQ: where = "in http-request expression in"; break;
1164 case ARGC_HRS: where = "in http-response response in"; break;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001165 case ARGC_UIF: where = "in unique-id-format string in"; break;
1166 case ARGC_RDR: where = "in redirect format string in"; break;
1167 case ARGC_CAP: where = "in capture rule in"; break;
1168 case ARGC_ACL: ctx = "ACL keyword"; break;
1169 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001170 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001171 case ARGC_HERR: where = "in http-error directive in"; break;
Miroslav Zagorac7f8314c2020-12-09 16:31:48 +01001172 case ARGC_OT: where = "in ot-scope directive in"; break;
Willy Tarreau57467b82021-09-02 19:43:20 +02001173 case ARGC_TCO: where = "in tcp-request connection expression in"; break;
1174 case ARGC_TSE: where = "in tcp-request session expression in"; break;
1175 case ARGC_TRQ: where = "in tcp-request content expression in"; break;
1176 case ARGC_TRS: where = "in tcp-response content expression in"; break;
1177 case ARGC_TCK: where = "in tcp-check expression in"; break;
1178 case ARGC_CFG: where = "in configuration expression in"; break;
1179 case ARGC_CLI: where = "in CLI expression in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001180 }
1181
1182 /* set a few default settings */
1183 px = p;
1184 pname = p->id;
1185
1186 switch (arg->type) {
1187 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001188 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001189 memprintf(err, "%sparsing [%s:%d]: missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1190 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001191 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001192 cfgerr++;
1193 continue;
1194 }
1195
1196 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001197 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001198
1199 if (sname) {
1200 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001201 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001202
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001203 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001204 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001205 memprintf(err, "%sparsing [%s:%d]: unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1206 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001207 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001208 cfgerr++;
1209 break;
1210 }
1211 }
1212 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001213 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001214
1215 srv = findserver(px, sname);
1216 if (!srv) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001217 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",
1218 *err ? *err : "", cur->file, cur->line, sname, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001219 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001220 cfgerr++;
1221 break;
1222 }
1223
Amaury Denoyelle06269612021-08-23 14:05:07 +02001224 srv->flags |= SRV_F_NON_PURGEABLE;
1225
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001226 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001227 arg->unresolved = 0;
1228 arg->data.srv = srv;
1229 break;
1230
1231 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001232 if (arg->data.str.data) {
1233 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001234 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001235 }
1236
1237 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001238 memprintf(err, "%sparsing [%s:%d]: unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1239 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001240 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001241 cfgerr++;
1242 break;
1243 }
1244
1245 if (!(px->cap & PR_CAP_FE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001246 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",
1247 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001248 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001249 cfgerr++;
1250 break;
1251 }
1252
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001253 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001254 arg->unresolved = 0;
1255 arg->data.prx = px;
1256 break;
1257
1258 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001259 if (arg->data.str.data) {
1260 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001261 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001262 }
1263
1264 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001265 memprintf(err, "%sparsing [%s:%d]: unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1266 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001267 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001268 cfgerr++;
1269 break;
1270 }
1271
1272 if (!(px->cap & PR_CAP_BE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001273 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",
1274 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001275 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001276 cfgerr++;
1277 break;
1278 }
1279
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001280 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001281 arg->unresolved = 0;
1282 arg->data.prx = px;
1283 break;
1284
1285 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001286 if (arg->data.str.data)
1287 stktname = arg->data.str.area;
1288 else
1289 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001290
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001291 t = stktable_find_by_name(stktname);
1292 if (!t) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001293 memprintf(err, "%sparsing [%s:%d]: unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1294 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001295 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001296 cfgerr++;
1297 break;
1298 }
1299
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001300 if (!t->size) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001301 memprintf(err, "%sparsing [%s:%d]: no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1302 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001303 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001304 cfgerr++;
1305 break;
1306 }
1307
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001308 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001309 p->next_stkt_ref = t->proxies_list;
1310 t->proxies_list = p;
1311 }
1312
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001313 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001314 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001315 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001316 break;
1317
1318 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001319 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001320 memprintf(err, "%sparsing [%s:%d]: missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1321 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001322 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001323 cfgerr++;
1324 break;
1325 }
1326
1327 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001328 strcmp(p->uri_auth->userlist->name, arg->data.str.area) == 0)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001329 ul = p->uri_auth->userlist;
1330 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001331 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001332
1333 if (!ul) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001334 memprintf(err, "%sparsing [%s:%d]: unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1335 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001336 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001337 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001338 cfgerr++;
1339 break;
1340 }
1341
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001342 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001343 arg->unresolved = 0;
1344 arg->data.usr = ul;
1345 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001346
1347 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001348 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001349 memprintf(err, "%sparsing [%s:%d]: missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1350 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001351 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001352 cfgerr++;
1353 continue;
1354 }
1355
Willy Tarreau46947782015-01-19 19:00:58 +01001356 rflags = 0;
1357 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001358 err2 = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001359
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001360 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err2))) {
1361 memprintf(err, "%sparsing [%s:%d]: error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1362 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001363 arg->data.str.area,
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001364 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err2);
Willy Tarreau46947782015-01-19 19:00:58 +01001365 cfgerr++;
1366 continue;
1367 }
1368
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001369 chunk_destroy(&arg->data.str);
Willy Tarreau46947782015-01-19 19:00:58 +01001370 arg->unresolved = 0;
1371 arg->data.reg = reg;
1372 break;
1373
1374
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001375 }
1376
Willy Tarreau2b718102021-04-21 07:32:39 +02001377 LIST_DELETE(&cur->list);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001378 free(cur);
1379 } /* end of args processing */
1380
1381 return cfgerr;
1382}
1383
1384/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001385 * Process a fetch + format conversion as defined by the sample expression
1386 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001387 * not explicitly set to <smp_type>, but shall be compatible with it as
1388 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1389 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001390 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1391 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1392 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1393 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001394 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1395 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1396 * still hope to get it after waiting longer, and is not converted to string.
1397 * The possible output combinations are the following :
1398 *
1399 * return MAY_CHANGE FINAL Meaning for the sample
1400 * NULL * * Not present and will never be (eg: header)
1401 * smp 0 * Final value converted (eg: header)
1402 * smp 1 0 Not present yet, may appear later (eg: header)
1403 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001404 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001405struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001406 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001407 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001408{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001409 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001410
Willy Tarreau6c616e02014-06-25 16:56:41 +02001411 memset(smp, 0, sizeof(*smp));
1412
Willy Tarreau192252e2015-04-04 01:47:55 +02001413 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001414 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1415 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001416 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001417 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001418
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001419 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001420 return NULL;
1421
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001422 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001423 return NULL;
1424
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001425 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001426 return smp;
1427}
1428
Christopher Faulet476e5d02016-10-26 11:34:47 +02001429static void release_sample_arg(struct arg *p)
1430{
1431 struct arg *p_back = p;
1432
1433 if (!p)
1434 return;
1435
1436 while (p->type != ARGT_STOP) {
1437 if (p->type == ARGT_STR || p->unresolved) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001438 chunk_destroy(&p->data.str);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001439 p->unresolved = 0;
1440 }
1441 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001442 regex_free(p->data.reg);
1443 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001444 }
1445 p++;
1446 }
1447
1448 if (p_back != empty_arg_list)
1449 free(p_back);
1450}
1451
1452void release_sample_expr(struct sample_expr *expr)
1453{
1454 struct sample_conv_expr *conv_expr, *conv_exprb;
1455
1456 if (!expr)
1457 return;
1458
Tim Duesterhus867cd982020-07-04 11:49:39 +02001459 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001460 LIST_DELETE(&conv_expr->list);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001461 release_sample_arg(conv_expr->arg_p);
Tim Duesterhus867cd982020-07-04 11:49:39 +02001462 free(conv_expr);
1463 }
1464
Christopher Faulet476e5d02016-10-26 11:34:47 +02001465 release_sample_arg(expr->arg_p);
1466 free(expr);
1467}
1468
Emeric Brun107ca302010-01-04 16:16:05 +01001469/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001470/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001471/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001472/*****************************************************************/
1473
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001474static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1475{
1476 int i;
1477 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001478 struct buffer *buf;
1479 struct sink *sink;
1480 struct ist line;
1481 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001482
Willy Tarreau0851fd52019-12-17 10:07:25 +01001483 buf = alloc_trash_chunk();
1484 if (!buf)
1485 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001486
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001487 sink = (struct sink *)arg_p[1].data.ptr;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001488 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001489
Willy Tarreau0851fd52019-12-17 10:07:25 +01001490 pfx = arg_p[0].data.str.area;
1491 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001492
Willy Tarreau0851fd52019-12-17 10:07:25 +01001493 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1494 if (!sample_casts[smp->data.type][SMP_T_STR])
1495 goto nocast;
1496
1497 /* Copy sample fetch. This puts the sample as const, the
1498 * cast will copy data if a transformation is required.
1499 */
1500 memcpy(&tmp, smp, sizeof(struct sample));
1501 tmp.flags = SMP_F_CONST;
1502
1503 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1504 goto nocast;
1505
1506 /* Display the displayable chars*. */
1507 b_putchr(buf, '<');
1508 for (i = 0; i < tmp.data.u.str.data; i++) {
Willy Tarreau90807112020-02-25 08:16:33 +01001509 if (isprint((unsigned char)tmp.data.u.str.area[i]))
Willy Tarreau0851fd52019-12-17 10:07:25 +01001510 b_putchr(buf, tmp.data.u.str.area[i]);
1511 else
1512 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001513 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001514 b_putchr(buf, '>');
1515
1516 done:
1517 line = ist2(buf->area, buf->data);
Emeric Brun54648852020-07-06 15:54:06 +02001518 sink_write(sink, &line, 1, 0, 0, NULL);
Willy Tarreau0851fd52019-12-17 10:07:25 +01001519 end:
1520 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001521 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001522 nocast:
1523 chunk_appendf(buf, "(undisplayable)");
1524 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001525}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001526
1527// This function checks the "debug" converter's arguments.
1528static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1529 const char *file, int line, char **err)
1530{
1531 const char *name = "buf0";
1532 struct sink *sink = NULL;
1533
1534 if (args[0].type != ARGT_STR) {
1535 /* optional prefix */
1536 args[0].data.str.area = "";
1537 args[0].data.str.data = 0;
1538 }
1539
1540 if (args[1].type == ARGT_STR)
1541 name = args[1].data.str.area;
1542
1543 sink = sink_find(name);
1544 if (!sink) {
1545 memprintf(err, "No such sink '%s'", name);
1546 return 0;
1547 }
1548
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001549 chunk_destroy(&args[1].data.str);
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001550 args[1].type = ARGT_PTR;
1551 args[1].data.ptr = sink;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001552 return 1;
1553}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001554
Holger Just1bfc24b2017-05-06 00:56:53 +02001555static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1556{
Willy Tarreau83061a82018-07-13 11:56:34 +02001557 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001558 int bin_len;
1559
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001560 trash->data = 0;
1561 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1562 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001563 if (bin_len < 0)
1564 return 0;
1565
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001566 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001567 smp->data.u.str = *trash;
1568 smp->data.type = SMP_T_BIN;
1569 smp->flags &= ~SMP_F_CONST;
1570 return 1;
1571}
1572
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +02001573static int sample_conv_base64url2bin(const struct arg *arg_p, struct sample *smp, void *private)
1574{
1575 struct buffer *trash = get_trash_chunk();
1576 int bin_len;
1577
1578 trash->data = 0;
1579 bin_len = base64urldec(smp->data.u.str.area, smp->data.u.str.data,
1580 trash->area, trash->size);
1581 if (bin_len < 0)
1582 return 0;
1583
1584 trash->data = bin_len;
1585 smp->data.u.str = *trash;
1586 smp->data.type = SMP_T_BIN;
1587 smp->flags &= ~SMP_F_CONST;
1588 return 1;
1589}
1590
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001591static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001592{
Willy Tarreau83061a82018-07-13 11:56:34 +02001593 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001594 int b64_len;
1595
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001596 trash->data = 0;
1597 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1598 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001599 if (b64_len < 0)
1600 return 0;
1601
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001602 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001603 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001604 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001605 smp->flags &= ~SMP_F_CONST;
1606 return 1;
1607}
1608
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +02001609static int sample_conv_bin2base64url(const struct arg *arg_p, struct sample *smp, void *private)
1610{
1611 struct buffer *trash = get_trash_chunk();
1612 int b64_len;
1613
1614 trash->data = 0;
1615 b64_len = a2base64url(smp->data.u.str.area, smp->data.u.str.data,
1616 trash->area, trash->size);
1617 if (b64_len < 0)
1618 return 0;
1619
1620 trash->data = b64_len;
1621 smp->data.u.str = *trash;
1622 smp->data.type = SMP_T_STR;
1623 smp->flags &= ~SMP_F_CONST;
1624 return 1;
1625}
1626
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001627static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1628{
1629 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001630 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001631
1632 memset(&ctx, 0, sizeof(ctx));
1633
1634 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001635 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1636 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001637
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001638 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001639 smp->data.u.str = *trash;
1640 smp->data.type = SMP_T_BIN;
1641 smp->flags &= ~SMP_F_CONST;
1642 return 1;
1643}
1644
Tim Duesterhusd4376302019-06-17 12:41:44 +02001645#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01001646static int smp_check_sha2(struct arg *args, struct sample_conv *conv,
1647 const char *file, int line, char **err)
1648{
1649 if (args[0].type == ARGT_STOP)
1650 return 1;
1651 if (args[0].type != ARGT_SINT) {
1652 memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]);
1653 return 0;
1654 }
1655
1656 switch (args[0].data.sint) {
1657 case 224:
1658 case 256:
1659 case 384:
1660 case 512:
1661 /* this is okay */
1662 return 1;
1663 default:
1664 memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint);
1665 return 0;
1666 }
1667}
1668
Tim Duesterhusd4376302019-06-17 12:41:44 +02001669static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
1670{
1671 struct buffer *trash = get_trash_chunk();
1672 int bits = 256;
Christopher Faulete6e7a582021-01-29 11:29:28 +01001673 if (arg_p->data.sint)
Tim Duesterhusd4376302019-06-17 12:41:44 +02001674 bits = arg_p->data.sint;
1675
1676 switch (bits) {
1677 case 224: {
1678 SHA256_CTX ctx;
1679
1680 memset(&ctx, 0, sizeof(ctx));
1681
1682 SHA224_Init(&ctx);
1683 SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1684 SHA224_Final((unsigned char *) trash->area, &ctx);
1685 trash->data = SHA224_DIGEST_LENGTH;
1686 break;
1687 }
1688 case 256: {
1689 SHA256_CTX ctx;
1690
1691 memset(&ctx, 0, sizeof(ctx));
1692
1693 SHA256_Init(&ctx);
1694 SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1695 SHA256_Final((unsigned char *) trash->area, &ctx);
1696 trash->data = SHA256_DIGEST_LENGTH;
1697 break;
1698 }
1699 case 384: {
1700 SHA512_CTX ctx;
1701
1702 memset(&ctx, 0, sizeof(ctx));
1703
1704 SHA384_Init(&ctx);
1705 SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1706 SHA384_Final((unsigned char *) trash->area, &ctx);
1707 trash->data = SHA384_DIGEST_LENGTH;
1708 break;
1709 }
1710 case 512: {
1711 SHA512_CTX ctx;
1712
1713 memset(&ctx, 0, sizeof(ctx));
1714
1715 SHA512_Init(&ctx);
1716 SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1717 SHA512_Final((unsigned char *) trash->area, &ctx);
1718 trash->data = SHA512_DIGEST_LENGTH;
1719 break;
1720 }
1721 default:
1722 return 0;
1723 }
1724
1725 smp->data.u.str = *trash;
1726 smp->data.type = SMP_T_BIN;
1727 smp->flags &= ~SMP_F_CONST;
1728 return 1;
1729}
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001730
Dragan Dosen9e8db132021-02-22 10:03:53 +01001731/* This function returns a sample struct filled with an <arg> content.
1732 * If the <arg> contains a string, it is returned in the sample flagged as
1733 * SMP_F_CONST. If the <arg> contains a variable descriptor, the sample is
1734 * filled with the content of the variable by using vars_get_by_desc().
1735 *
1736 * Keep in mind that the sample content may be written to a pre-allocated
1737 * trash chunk as returned by get_trash_chunk().
1738 *
1739 * This function returns 0 if an error occurs, otherwise it returns 1.
1740 */
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001741static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
1742{
1743 switch (arg->type) {
1744 case ARGT_STR:
1745 smp->data.type = SMP_T_STR;
1746 smp->data.u.str = arg->data.str;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001747 smp->flags = SMP_F_CONST;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001748 return 1;
1749 case ARGT_VAR:
1750 if (!vars_get_by_desc(&arg->data.var, smp))
1751 return 0;
1752 if (!sample_casts[smp->data.type][SMP_T_STR])
1753 return 0;
1754 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
1755 return 0;
1756 return 1;
1757 default:
1758 return 0;
1759 }
1760}
1761
Dragan Dosen9e8db132021-02-22 10:03:53 +01001762/* This function checks an <arg> and fills it with a variable type if the
1763 * <arg> string contains a valid variable name. If failed, the function
1764 * tries to perform a base64 decode operation on the same string, and
1765 * fills the <arg> with the decoded content.
1766 *
1767 * Validation is skipped if the <arg> string is empty.
1768 *
1769 * This function returns 0 if the variable lookup fails and the specified
1770 * <arg> string is not a valid base64 encoded string, as well if
1771 * unexpected argument type is specified or memory allocation error
1772 * occurs. Otherwise it returns 1.
1773 */
1774static inline int sample_check_arg_base64(struct arg *arg, char **err)
1775{
1776 char *dec = NULL;
1777 int dec_size;
1778
1779 if (arg->type != ARGT_STR) {
1780 memprintf(err, "unexpected argument type");
1781 return 0;
1782 }
1783
1784 if (arg->data.str.data == 0) /* empty */
1785 return 1;
1786
1787 if (vars_check_arg(arg, NULL))
1788 return 1;
1789
1790 if (arg->data.str.data % 4) {
1791 memprintf(err, "argument needs to be base64 encoded, and "
1792 "can either be a string or a variable");
1793 return 0;
1794 }
1795
1796 dec_size = (arg->data.str.data / 4 * 3)
1797 - (arg->data.str.area[arg->data.str.data-1] == '=' ? 1 : 0)
1798 - (arg->data.str.area[arg->data.str.data-2] == '=' ? 1 : 0);
1799
1800 if ((dec = malloc(dec_size)) == NULL) {
1801 memprintf(err, "memory allocation error");
1802 return 0;
1803 }
1804
1805 dec_size = base64dec(arg->data.str.area, arg->data.str.data, dec, dec_size);
1806 if (dec_size < 0) {
1807 memprintf(err, "argument needs to be base64 encoded, and "
1808 "can either be a string or a variable");
1809 free(dec);
1810 return 0;
1811 }
1812
1813 /* base64 decoded */
1814 chunk_destroy(&arg->data.str);
1815 arg->data.str.area = dec;
1816 arg->data.str.data = dec_size;
1817 return 1;
1818}
1819
Ilya Shipitsin2c481d02021-03-26 23:35:31 +05001820#ifdef EVP_CIPH_GCM_MODE
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001821static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
1822 const char *file, int line, char **err)
1823{
1824 switch(args[0].data.sint) {
1825 case 128:
1826 case 192:
1827 case 256:
1828 break;
1829 default:
1830 memprintf(err, "key size must be 128, 192 or 256 (bits).");
1831 return 0;
1832 }
Dragan Dosen9e8db132021-02-22 10:03:53 +01001833
1834 /* Try to decode variables. */
1835 if (!sample_check_arg_base64(&args[1], err)) {
1836 memprintf(err, "failed to parse nonce : %s", *err);
1837 return 0;
1838 }
1839 if (!sample_check_arg_base64(&args[2], err)) {
1840 memprintf(err, "failed to parse key : %s", *err);
1841 return 0;
1842 }
1843 if (!sample_check_arg_base64(&args[3], err)) {
1844 memprintf(err, "failed to parse aead_tag : %s", *err);
1845 return 0;
1846 }
1847
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001848 return 1;
1849}
1850
1851/* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
1852static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
1853{
1854 struct sample nonce, key, aead_tag;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001855 struct buffer *smp_trash = NULL, *smp_trash_alloc = NULL;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001856 EVP_CIPHER_CTX *ctx;
1857 int dec_size, ret;
1858
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001859 smp_trash_alloc = alloc_trash_chunk();
1860 if (!smp_trash_alloc)
1861 return 0;
1862
Dragan Dosen9e8db132021-02-22 10:03:53 +01001863 /* smp copy */
1864 smp_trash_alloc->data = smp->data.u.str.data;
1865 if (unlikely(smp_trash_alloc->data > smp_trash_alloc->size))
1866 smp_trash_alloc->data = smp_trash_alloc->size;
1867 memcpy(smp_trash_alloc->area, smp->data.u.str.area, smp_trash_alloc->data);
1868
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001869 ctx = EVP_CIPHER_CTX_new();
1870
1871 if (!ctx)
1872 goto err;
1873
Dragan Dosen9e8db132021-02-22 10:03:53 +01001874 smp_trash = alloc_trash_chunk();
1875 if (!smp_trash)
1876 goto err;
1877
1878 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
1879 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001880 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001881
1882 if (arg_p[1].type == ARGT_VAR) {
1883 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
1884 if (dec_size < 0)
1885 goto err;
1886 smp_trash->data = dec_size;
1887 nonce.data.u.str = *smp_trash;
1888 }
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001889
1890 /* Set cipher type and mode */
1891 switch(arg_p[0].data.sint) {
1892 case 128:
1893 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
1894 break;
1895 case 192:
1896 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
1897 break;
1898 case 256:
1899 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
1900 break;
1901 }
1902
Dragan Dosen9e8db132021-02-22 10:03:53 +01001903 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, nonce.data.u.str.data, NULL);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001904
1905 /* Initialise IV */
Dragan Dosen9e8db132021-02-22 10:03:53 +01001906 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) nonce.data.u.str.area))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001907 goto err;
1908
Dragan Dosen9e8db132021-02-22 10:03:53 +01001909 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
1910 if (!sample_conv_var2smp_str(&arg_p[2], &key))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001911 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001912
1913 if (arg_p[2].type == ARGT_VAR) {
1914 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
1915 if (dec_size < 0)
1916 goto err;
1917 smp_trash->data = dec_size;
1918 key.data.u.str = *smp_trash;
1919 }
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001920
1921 /* Initialise key */
Dragan Dosen9e8db132021-02-22 10:03:53 +01001922 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) key.data.u.str.area, NULL))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001923 goto err;
1924
1925 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
Dragan Dosen9e8db132021-02-22 10:03:53 +01001926 (unsigned char *) smp_trash_alloc->area, (int) smp_trash_alloc->data))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001927 goto err;
1928
Dragan Dosen9e8db132021-02-22 10:03:53 +01001929 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
1930 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001931 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001932
1933 if (arg_p[3].type == ARGT_VAR) {
1934 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
1935 if (dec_size < 0)
1936 goto err;
1937 smp_trash_alloc->data = dec_size;
1938 aead_tag.data.u.str = *smp_trash_alloc;
1939 }
1940
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001941 dec_size = smp_trash->data;
1942
Dragan Dosen9e8db132021-02-22 10:03:53 +01001943 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, aead_tag.data.u.str.data, (void *) aead_tag.data.u.str.area);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001944 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
1945
1946 if (ret <= 0)
1947 goto err;
1948
1949 smp->data.u.str.data = dec_size + smp_trash->data;
1950 smp->data.u.str.area = smp_trash->area;
1951 smp->data.type = SMP_T_BIN;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001952 smp_dup(smp);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001953 free_trash_chunk(smp_trash_alloc);
Dragan Dosen9e8db132021-02-22 10:03:53 +01001954 free_trash_chunk(smp_trash);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001955 return 1;
1956
1957err:
1958 free_trash_chunk(smp_trash_alloc);
Dragan Dosen9e8db132021-02-22 10:03:53 +01001959 free_trash_chunk(smp_trash);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001960 return 0;
1961}
Ilya Shipitsin2c481d02021-03-26 23:35:31 +05001962#endif
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001963
Patrick Gansterer8e366512020-04-22 16:47:57 +02001964static int check_crypto_digest(struct arg *args, struct sample_conv *conv,
1965 const char *file, int line, char **err)
1966{
1967 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1968
1969 if (evp)
1970 return 1;
1971
1972 memprintf(err, "algorithm must be a valid OpenSSL message digest name.");
1973 return 0;
1974}
1975
1976static int sample_conv_crypto_digest(const struct arg *args, struct sample *smp, void *private)
1977{
1978 struct buffer *trash = get_trash_chunk();
1979 unsigned char *md = (unsigned char*) trash->area;
1980 unsigned int md_len = trash->size;
1981 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
1982 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1983
1984 if (!ctx)
1985 return 0;
1986
1987 if (!EVP_DigestInit_ex(ctx, evp, NULL) ||
1988 !EVP_DigestUpdate(ctx, smp->data.u.str.area, smp->data.u.str.data) ||
1989 !EVP_DigestFinal_ex(ctx, md, &md_len)) {
1990 EVP_MD_CTX_free(ctx);
1991 return 0;
1992 }
1993
1994 EVP_MD_CTX_free(ctx);
1995
1996 trash->data = md_len;
1997 smp->data.u.str = *trash;
1998 smp->data.type = SMP_T_BIN;
1999 smp->flags &= ~SMP_F_CONST;
2000 return 1;
2001}
2002
2003static int check_crypto_hmac(struct arg *args, struct sample_conv *conv,
2004 const char *file, int line, char **err)
2005{
2006 if (!check_crypto_digest(args, conv, file, line, err))
2007 return 0;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002008
2009 if (!sample_check_arg_base64(&args[1], err)) {
2010 memprintf(err, "failed to parse key : %s", *err);
2011 return 0;
2012 }
Patrick Gansterer8e366512020-04-22 16:47:57 +02002013
Patrick Gansterer8e366512020-04-22 16:47:57 +02002014 return 1;
2015}
2016
2017static int sample_conv_crypto_hmac(const struct arg *args, struct sample *smp, void *private)
2018{
2019 struct sample key;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002020 struct buffer *trash = NULL, *key_trash = NULL;
Patrick Gansterer8e366512020-04-22 16:47:57 +02002021 unsigned char *md;
2022 unsigned int md_len;
2023 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
2024 int dec_size;
2025
2026 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
2027 if (!sample_conv_var2smp_str(&args[1], &key))
2028 return 0;
2029
Dragan Dosen9e8db132021-02-22 10:03:53 +01002030 if (args[1].type == ARGT_VAR) {
2031 key_trash = alloc_trash_chunk();
2032 if (!key_trash)
2033 goto err;
2034
2035 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, key_trash->area, key_trash->size);
2036 if (dec_size < 0)
2037 goto err;
2038 key_trash->data = dec_size;
2039 key.data.u.str = *key_trash;
2040 }
Patrick Gansterer8e366512020-04-22 16:47:57 +02002041
Dragan Dosen9e8db132021-02-22 10:03:53 +01002042 trash = alloc_trash_chunk();
2043 if (!trash)
Patrick Gansterer8e366512020-04-22 16:47:57 +02002044 goto err;
2045
2046 md = (unsigned char*) trash->area;
2047 md_len = trash->size;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002048 if (!HMAC(evp, key.data.u.str.area, key.data.u.str.data, (const unsigned char*) smp->data.u.str.area,
2049 smp->data.u.str.data, md, &md_len))
Patrick Gansterer8e366512020-04-22 16:47:57 +02002050 goto err;
2051
2052 free_trash_chunk(key_trash);
2053
2054 trash->data = md_len;
2055 smp->data.u.str = *trash;
2056 smp->data.type = SMP_T_BIN;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002057 smp_dup(smp);
2058 free_trash_chunk(trash);
Patrick Gansterer8e366512020-04-22 16:47:57 +02002059 return 1;
2060
2061err:
2062 free_trash_chunk(key_trash);
Dragan Dosen9e8db132021-02-22 10:03:53 +01002063 free_trash_chunk(trash);
Patrick Gansterer8e366512020-04-22 16:47:57 +02002064 return 0;
2065}
2066
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02002067#endif /* USE_OPENSSL */
Tim Duesterhusd4376302019-06-17 12:41:44 +02002068
Marcin Deranek40ca09c2021-07-13 14:05:24 +02002069static int sample_conv_be2dec_check(struct arg *args, struct sample_conv *conv,
2070 const char *file, int line, char **err)
2071{
2072 if (args[1].data.sint <= 0 || args[1].data.sint > sizeof(unsigned long long)) {
2073 memprintf(err, "chunk_size out of [1..%ld] range (%lld)", sizeof(unsigned long long), args[1].data.sint);
2074 return 0;
2075 }
2076
2077 if (args[2].data.sint != 0 && args[2].data.sint != 1) {
2078 memprintf(err, "Unsupported truncate value (%lld)", args[2].data.sint);
2079 return 0;
2080 }
2081
2082 return 1;
2083}
2084
2085/* Converts big-endian binary input sample to a string containing an unsigned
2086 * integer number per <chunk_size> input bytes separated with <separator>.
2087 * Optional <truncate> flag indicates if input is truncated at <chunk_size>
2088 * boundaries.
2089 * Arguments: separator (string), chunk_size (integer), truncate (0,1)
2090 */
2091static int sample_conv_be2dec(const struct arg *args, struct sample *smp, void *private)
2092{
2093 struct buffer *trash = get_trash_chunk();
2094 const int last = args[2].data.sint ? smp->data.u.str.data - args[1].data.sint + 1 : smp->data.u.str.data;
2095 int max_size = trash->size - 2;
2096 int i;
2097 int start;
2098 int ptr = 0;
2099 unsigned long long number;
2100 char *pos;
2101
2102 trash->data = 0;
2103
2104 while (ptr < last && trash->data <= max_size) {
2105 start = trash->data;
2106 if (ptr) {
2107 /* Add separator */
2108 memcpy(trash->area + trash->data, args[0].data.str.area, args[0].data.str.data);
2109 trash->data += args[0].data.str.data;
2110 }
2111 else
2112 max_size -= args[0].data.str.data;
2113
2114 /* Add integer */
2115 for (number = 0, i = 0; i < args[1].data.sint && ptr < smp->data.u.str.data; i++)
2116 number = (number << 8) + (unsigned char)smp->data.u.str.area[ptr++];
2117
2118 pos = ulltoa(number, trash->area + trash->data, trash->size - trash->data);
2119 if (pos)
2120 trash->data = pos - trash->area;
2121 else {
2122 trash->data = start;
2123 break;
2124 }
2125 }
2126
2127 smp->data.u.str = *trash;
2128 smp->data.type = SMP_T_STR;
2129 smp->flags &= ~SMP_F_CONST;
2130 return 1;
2131}
2132
Marcin Deranekda0264a2021-07-13 14:08:56 +02002133static int sample_conv_be2hex_check(struct arg *args, struct sample_conv *conv,
2134 const char *file, int line, char **err)
2135{
2136 if (args[1].data.sint <= 0 && (args[0].data.str.data > 0 || args[2].data.sint != 0)) {
2137 memprintf(err, "chunk_size needs to be positive (%lld)", args[1].data.sint);
2138 return 0;
2139 }
2140
2141 if (args[2].data.sint != 0 && args[2].data.sint != 1) {
2142 memprintf(err, "Unsupported truncate value (%lld)", args[2].data.sint);
2143 return 0;
2144 }
2145
2146 return 1;
2147}
2148
2149/* Converts big-endian binary input sample to a hex string containing two hex
2150 * digits per input byte. <separator> is put every <chunk_size> binary input
2151 * bytes if specified. Optional <truncate> flag indicates if input is truncated
2152 * at <chunk_size> boundaries.
2153 * Arguments: separator (string), chunk_size (integer), truncate (0,1)
2154 */
2155static int sample_conv_be2hex(const struct arg *args, struct sample *smp, void *private)
2156{
2157 struct buffer *trash = get_trash_chunk();
2158 int chunk_size = args[1].data.sint;
2159 const int last = args[2].data.sint ? smp->data.u.str.data - chunk_size + 1 : smp->data.u.str.data;
2160 int i;
2161 int max_size;
2162 int ptr = 0;
2163 unsigned char c;
2164
2165 trash->data = 0;
2166 if (args[0].data.str.data == 0 && args[2].data.sint == 0)
2167 chunk_size = smp->data.u.str.data;
2168 max_size = trash->size - 2 * chunk_size;
2169
2170 while (ptr < last && trash->data <= max_size) {
2171 if (ptr) {
2172 /* Add separator */
2173 memcpy(trash->area + trash->data, args[0].data.str.area, args[0].data.str.data);
2174 trash->data += args[0].data.str.data;
2175 }
2176 else
2177 max_size -= args[0].data.str.data;
2178
2179 /* Add hex */
2180 for (i = 0; i < chunk_size && ptr < smp->data.u.str.data; i++) {
2181 c = smp->data.u.str.area[ptr++];
2182 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
2183 trash->area[trash->data++] = hextab[c & 0xF];
2184 }
2185 }
2186
2187 smp->data.u.str = *trash;
2188 smp->data.type = SMP_T_STR;
2189 smp->flags &= ~SMP_F_CONST;
2190 return 1;
2191}
2192
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002193static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002194{
Willy Tarreau83061a82018-07-13 11:56:34 +02002195 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002196 unsigned char c;
2197 int ptr = 0;
2198
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002199 trash->data = 0;
2200 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
2201 c = smp->data.u.str.area[ptr++];
2202 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
2203 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002204 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002205 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002206 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002207 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002208 return 1;
2209}
2210
Dragan Dosen3f957b22017-10-24 09:27:34 +02002211static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
2212{
2213 long long int n = 0;
2214 int i, c;
2215
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002216 for (i = 0; i < smp->data.u.str.data; i++) {
2217 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02002218 return 0;
2219 n = (n << 4) + c;
2220 }
2221
2222 smp->data.u.sint = n;
2223 smp->data.type = SMP_T_SINT;
2224 smp->flags &= ~SMP_F_CONST;
2225 return 1;
2226}
2227
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002228/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002229static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002230{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002231 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
2232 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002233 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002234 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002235 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002236 return 1;
2237}
2238
Willy Tarreau60a2ee72017-12-15 07:13:48 +01002239static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002240{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002241 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002242 smp->data.u.sint = i;
2243 smp->data.type = SMP_T_SINT;
2244 return 1;
2245}
2246
2247
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002248static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002249{
2250 int i;
2251
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002252 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002253 return 0;
2254
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002255 for (i = 0; i < smp->data.u.str.data; i++) {
2256 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
2257 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01002258 }
2259 return 1;
2260}
2261
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002262static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002263{
2264 int i;
2265
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002266 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02002267 return 0;
2268
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002269 for (i = 0; i < smp->data.u.str.data; i++) {
2270 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
2271 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01002272 }
2273 return 1;
2274}
2275
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002276/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
2277static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002278{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002279 /* Attempt to convert to IPv4 to apply the correct mask. */
2280 c_ipv62ip(smp);
2281
2282 if (smp->data.type == SMP_T_IPV4) {
2283 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
2284 smp->data.type = SMP_T_IPV4;
2285 }
2286 else if (smp->data.type == SMP_T_IPV6) {
2287 /* IPv6 cannot be converted without an IPv6 mask. */
2288 if (args[1].type != ARGT_IPV6)
2289 return 0;
2290
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +01002291 write_u64(&smp->data.u.ipv6.s6_addr[0],
2292 read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0]));
2293 write_u64(&smp->data.u.ipv6.s6_addr[8],
2294 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 +01002295 smp->data.type = SMP_T_IPV6;
2296 }
2297
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002298 return 1;
2299}
2300
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002301/* takes an UINT value on input supposed to represent the time since EPOCH,
2302 * adds an optional offset found in args[1] and emits a string representing
2303 * the local time in the format specified in args[1] using strftime().
2304 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002305static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002306{
Willy Tarreau83061a82018-07-13 11:56:34 +02002307 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002308 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002309 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002310 struct tm tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002311
2312 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002313 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002314 curr_date += args[1].data.sint;
2315
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002316 get_localtime(curr_date, &tm);
2317
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002318 temp = get_trash_chunk();
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002319 temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002320 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002321 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002322 return 1;
2323}
2324
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002325/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002326static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002327{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002328 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
2329 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002330 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002331 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002332 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002333 return 1;
2334}
2335
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002336/* takes an UINT value on input supposed to represent the time since EPOCH,
2337 * adds an optional offset found in args[1] and emits a string representing
2338 * the UTC date in the format specified in args[1] using strftime().
2339 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002340static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002341{
Willy Tarreau83061a82018-07-13 11:56:34 +02002342 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002343 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002344 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002345 struct tm tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002346
2347 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002348 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002349 curr_date += args[1].data.sint;
2350
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002351 get_gmtime(curr_date, &tm);
2352
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002353 temp = get_trash_chunk();
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002354 temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002355 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002356 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002357 return 1;
2358}
2359
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002360/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002361static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002362{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002363 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
2364 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002365 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002366 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002367 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002368 return 1;
2369}
2370
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002371/* hashes the binary input into a 32-bit unsigned int using xxh.
2372 * The seed of the hash defaults to 0 but can be changd in argument 1.
2373 */
2374static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
2375{
2376 unsigned int seed;
2377
Christopher Faulete6e7a582021-01-29 11:29:28 +01002378 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002379 seed = arg_p->data.sint;
2380 else
2381 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002382 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
2383 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002384 smp->data.type = SMP_T_SINT;
2385 return 1;
2386}
2387
2388/* hashes the binary input into a 64-bit unsigned int using xxh.
2389 * In fact, the function returns a 64 bit unsigned, but the sample
2390 * storage of haproxy only proposes 64-bits signed, so the value is
2391 * cast as signed. This cast doesn't impact the hash repartition.
2392 * The seed of the hash defaults to 0 but can be changd in argument 1.
2393 */
2394static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
2395{
2396 unsigned long long int seed;
2397
Christopher Faulete6e7a582021-01-29 11:29:28 +01002398 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002399 seed = (unsigned long long int)arg_p->data.sint;
2400 else
2401 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002402 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
2403 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002404 smp->data.type = SMP_T_SINT;
2405 return 1;
2406}
2407
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002408static int sample_conv_xxh3(const struct arg *arg_p, struct sample *smp, void *private)
2409{
2410 unsigned long long int seed;
2411
Christopher Faulete6e7a582021-01-29 11:29:28 +01002412 if (arg_p->data.sint)
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002413 seed = (unsigned long long int)arg_p->data.sint;
2414 else
2415 seed = 0;
2416 smp->data.u.sint = (long long int)XXH3(smp->data.u.str.area,
2417 smp->data.u.str.data, seed);
2418 smp->data.type = SMP_T_SINT;
2419 return 1;
2420}
2421
Willy Tarreau80599772015-01-20 19:35:24 +01002422/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002423static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01002424{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002425 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
2426 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002427 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002428 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002429 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01002430 return 1;
2431}
2432
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002433/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
2434static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
2435{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002436 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
2437 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002438 if (arg_p->data.sint)
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002439 smp->data.u.sint = full_hash(smp->data.u.sint);
2440 smp->data.type = SMP_T_SINT;
2441 return 1;
2442}
2443
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002444/* This function escape special json characters. The returned string can be
2445 * safely set between two '"' and used as json string. The json string is
2446 * defined like this:
2447 *
2448 * any Unicode character except '"' or '\' or control character
2449 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
2450 *
2451 * The enum input_type contain all the allowed mode for decoding the input
2452 * string.
2453 */
2454enum input_type {
2455 IT_ASCII = 0,
2456 IT_UTF8,
2457 IT_UTF8S,
2458 IT_UTF8P,
2459 IT_UTF8PS,
2460};
William Dauchy5417e892021-01-08 21:57:41 +01002461
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002462static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
2463 const char *file, int line, char **err)
2464{
Christopher Faulet95917132020-08-05 23:07:07 +02002465 enum input_type type;
2466
Christopher Faulet95917132020-08-05 23:07:07 +02002467 if (strcmp(arg->data.str.area, "") == 0)
2468 type = IT_ASCII;
2469 else if (strcmp(arg->data.str.area, "ascii") == 0)
2470 type = IT_ASCII;
2471 else if (strcmp(arg->data.str.area, "utf8") == 0)
2472 type = IT_UTF8;
2473 else if (strcmp(arg->data.str.area, "utf8s") == 0)
2474 type = IT_UTF8S;
2475 else if (strcmp(arg->data.str.area, "utf8p") == 0)
2476 type = IT_UTF8P;
2477 else if (strcmp(arg->data.str.area, "utf8ps") == 0)
2478 type = IT_UTF8PS;
2479 else {
2480 memprintf(err, "Unexpected input code type. "
2481 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
2482 return 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002483 }
2484
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002485 chunk_destroy(&arg->data.str);
Christopher Faulet95917132020-08-05 23:07:07 +02002486 arg->type = ARGT_SINT;
2487 arg->data.sint = type;
2488 return 1;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002489}
2490
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002491static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002492{
Willy Tarreau83061a82018-07-13 11:56:34 +02002493 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002494 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
2495 const char *str;
2496 int len;
2497 enum input_type input_type = IT_ASCII;
2498 unsigned int c;
2499 unsigned int ret;
2500 char *p;
2501
Christopher Faulete6e7a582021-01-29 11:29:28 +01002502 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002503
2504 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002505 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002506
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002507 p = smp->data.u.str.area;
2508 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002509
2510 if (input_type == IT_ASCII) {
2511 /* Read input as ASCII. */
2512 c = *(unsigned char *)p;
2513 p++;
2514 }
2515 else {
2516 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002517 ret = utf8_next(p,
2518 smp->data.u.str.data - ( p - smp->data.u.str.area),
2519 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002520 p += utf8_return_length(ret);
2521
2522 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2523 return 0;
2524 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2525 continue;
2526 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2527 return 0;
2528 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2529 continue;
2530
2531 /* Check too big values. */
2532 if ((unsigned int)c > 0xffff) {
2533 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2534 return 0;
2535 continue;
2536 }
2537 }
2538
2539 /* Convert character. */
2540 if (c == '"') {
2541 len = 2;
2542 str = "\\\"";
2543 }
2544 else if (c == '\\') {
2545 len = 2;
2546 str = "\\\\";
2547 }
2548 else if (c == '/') {
2549 len = 2;
2550 str = "\\/";
2551 }
2552 else if (c == '\b') {
2553 len = 2;
2554 str = "\\b";
2555 }
2556 else if (c == '\f') {
2557 len = 2;
2558 str = "\\f";
2559 }
2560 else if (c == '\r') {
2561 len = 2;
2562 str = "\\r";
2563 }
2564 else if (c == '\n') {
2565 len = 2;
2566 str = "\\n";
2567 }
2568 else if (c == '\t') {
2569 len = 2;
2570 str = "\\t";
2571 }
Willy Tarreau90807112020-02-25 08:16:33 +01002572 else if (c > 0xff || !isprint((unsigned char)c)) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002573 /* isprint generate a segfault if c is too big. The man says that
2574 * c must have the value of an unsigned char or EOF.
2575 */
2576 len = 6;
2577 _str[0] = '\\';
2578 _str[1] = 'u';
2579 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2580 str = _str;
2581 }
2582 else {
2583 len = 1;
Willy Tarreau5715da22020-02-25 08:37:37 +01002584 _str[0] = c;
2585 str = _str;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002586 }
2587
2588 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002589 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002590 return 0;
2591
2592 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002593 memcpy(temp->area + temp->data, str, len);
2594 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002595 }
2596
2597 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002598 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002599 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002600
2601 return 1;
2602}
2603
Emeric Brun54c4ac82014-11-03 15:32:43 +01002604/* This sample function is designed to extract some bytes from an input buffer.
2605 * First arg is the offset.
2606 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002607static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002608{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002609 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2610 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002611 return 1;
2612 }
2613
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002614 if (smp->data.u.str.size)
2615 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002616 smp->data.u.str.data -= arg_p[0].data.sint;
2617 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002618
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002619 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2620 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002621
2622 return 1;
2623}
2624
Emeric Brunf399b0d2014-11-03 17:07:03 +01002625static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2626 const char *file, int line, char **err)
2627{
2628 struct arg *arg = args;
2629
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002630 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002631 memprintf(err, "Unexpected arg type");
2632 return 0;
2633 }
2634
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002635 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002636 memprintf(err, "Unexpected value 0 for index");
2637 return 0;
2638 }
2639
2640 arg++;
2641
2642 if (arg->type != ARGT_STR) {
2643 memprintf(err, "Unexpected arg type");
2644 return 0;
2645 }
2646
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002647 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002648 memprintf(err, "Empty separators list");
2649 return 0;
2650 }
2651
2652 return 1;
2653}
2654
2655/* This sample function is designed to a return selected part of a string (field).
2656 * First arg is the index of the field (start at 1)
2657 * Second arg is a char list of separators (type string)
2658 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002659static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002660{
Marcin Deranek9631a282018-04-16 14:30:46 +02002661 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002662 char *start, *end;
2663 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002664 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002665
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002666 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002667 return 0;
2668
Marcin Deranek9631a282018-04-16 14:30:46 +02002669 if (arg_p[0].data.sint < 0) {
2670 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002671 end = start = smp->data.u.str.area + smp->data.u.str.data;
2672 while (start > smp->data.u.str.area) {
2673 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2674 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002675 if (field == arg_p[0].data.sint) {
2676 if (count == 1)
2677 goto found;
2678 else if (count > 1)
2679 count--;
2680 } else {
2681 end = start-1;
2682 field--;
2683 }
2684 break;
2685 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002686 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002687 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002688 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002689 } else {
2690 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002691 end = start = smp->data.u.str.area;
2692 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2693 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2694 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002695 if (field == arg_p[0].data.sint) {
2696 if (count == 1)
2697 goto found;
2698 else if (count > 1)
2699 count--;
2700 } else {
2701 start = end+1;
2702 field++;
2703 }
2704 break;
2705 }
2706 }
2707 end++;
2708 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002709 }
2710
2711 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002712 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002713 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002714 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002715 }
2716found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002717 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002718 /* If ret string is len 0, no need to
2719 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002720 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002721 return 1;
2722
Emeric Brunf399b0d2014-11-03 17:07:03 +01002723 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002724 Note: smp->data.u.str.size cannot be set to 0 */
2725 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002726 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002727
Thayne McCombsb2843052021-04-11 23:26:59 -06002728 smp->data.u.str.area = start;
2729
Emeric Brunf399b0d2014-11-03 17:07:03 +01002730 return 1;
2731}
2732
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002733/* This sample function is designed to return a word from a string.
2734 * First arg is the index of the word (start at 1)
2735 * Second arg is a char list of words separators (type string)
2736 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002737static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002738{
Marcin Deranek9631a282018-04-16 14:30:46 +02002739 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002740 char *start, *end;
2741 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002742 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002743
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002744 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002745 return 0;
2746
2747 word = 0;
2748 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002749 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002750 end = start = smp->data.u.str.area + smp->data.u.str.data;
2751 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002752 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002753 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2754 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002755 issep = 1;
2756 break;
2757 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002758 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002759 if (!inword) {
2760 if (!issep) {
2761 if (word != arg_p[0].data.sint) {
2762 word--;
2763 end = start;
2764 }
2765 inword = 1;
2766 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002767 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002768 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002769 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002770 if (count == 1)
2771 goto found;
2772 else if (count > 1)
2773 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002774 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002775 inword = 0;
2776 }
2777 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002778 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002779 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002780 end = start = smp->data.u.str.area;
2781 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002782 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002783 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2784 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002785 issep = 1;
2786 break;
2787 }
2788 }
2789 if (!inword) {
2790 if (!issep) {
2791 if (word != arg_p[0].data.sint) {
2792 word++;
2793 start = end;
2794 }
2795 inword = 1;
2796 }
2797 }
2798 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002799 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002800 if (count == 1)
2801 goto found;
2802 else if (count > 1)
2803 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002804 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002805 inword = 0;
2806 }
2807 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002808 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002809 }
2810
2811 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002812 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002813 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002814 return 1;
2815 }
2816found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002817 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002818 /* If ret string is len 0, no need to
2819 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002820 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002821 return 1;
2822
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002823 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002824
2825 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002826 Note: smp->data.u.str.size cannot be set to 0 */
2827 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002828 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002829
2830 return 1;
2831}
2832
Willy Tarreau7eda8492015-01-20 19:47:06 +01002833static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2834 const char *file, int line, char **err)
2835{
2836 struct arg *arg = args;
2837 char *p;
2838 int len;
2839
2840 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2841 arg[0].type_flags = 0;
2842
2843 if (arg[2].type != ARGT_STR)
2844 return 1;
2845
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002846 p = arg[2].data.str.area;
2847 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002848 while (len) {
2849 if (*p == 'i') {
2850 arg[0].type_flags |= ARGF_REG_ICASE;
2851 }
2852 else if (*p == 'g') {
2853 arg[0].type_flags |= ARGF_REG_GLOB;
2854 }
2855 else {
2856 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2857 return 0;
2858 }
2859 p++;
2860 len--;
2861 }
2862 return 1;
2863}
2864
2865/* This sample function is designed to do the equivalent of s/match/replace/ on
2866 * the input string. It applies a regex and restarts from the last matched
2867 * location until nothing matches anymore. First arg is the regex to apply to
2868 * the input string, second arg is the replacement expression.
2869 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002870static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002871{
2872 char *start, *end;
2873 struct my_regex *reg = arg_p[0].data.reg;
2874 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002875 struct buffer *trash = get_trash_chunk();
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002876 struct buffer *output;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002877 int flag, max;
2878 int found;
2879
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002880 start = smp->data.u.str.area;
2881 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002882
2883 flag = 0;
2884 while (1) {
2885 /* check for last round which is used to copy remaining parts
2886 * when not running in global replacement mode.
2887 */
2888 found = 0;
2889 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2890 /* Note: we can have start == end on empty strings or at the end */
2891 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2892 }
2893
2894 if (!found)
2895 pmatch[0].rm_so = end - start;
2896
2897 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002898 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002899 if (max && pmatch[0].rm_so > 0) {
2900 if (max > pmatch[0].rm_so)
2901 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002902 memcpy(trash->area + trash->data, start, max);
2903 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002904 }
2905
2906 if (!found)
2907 break;
2908
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002909 output = alloc_trash_chunk();
Willy Tarreau23997da2020-02-18 14:27:44 +01002910 if (!output)
2911 break;
2912
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002913 output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2914
Willy Tarreau7eda8492015-01-20 19:47:06 +01002915 /* replace the matching part */
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002916 max = output->size - output->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002917 if (max) {
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002918 if (max > output->data)
2919 max = output->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002920 memcpy(trash->area + trash->data,
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002921 output->area, max);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002922 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002923 }
2924
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002925 free_trash_chunk(output);
2926
Willy Tarreau7eda8492015-01-20 19:47:06 +01002927 /* stop here if we're done with this string */
2928 if (start >= end)
2929 break;
2930
2931 /* We have a special case for matches of length 0 (eg: "x*y*").
2932 * These ones are considered to match in front of a character,
2933 * so we have to copy that character and skip to the next one.
2934 */
2935 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002936 if (trash->data < trash->size)
2937 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002938 pmatch[0].rm_eo++;
2939 }
2940
2941 start += pmatch[0].rm_eo;
2942 flag |= REG_NOTBOL;
2943 }
2944
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002945 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002946 return 1;
2947}
2948
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002949/* This function check an operator entry. It expects a string.
2950 * The string can be an integer or a variable name.
2951 */
2952static int check_operator(struct arg *args, struct sample_conv *conv,
2953 const char *file, int line, char **err)
2954{
2955 const char *str;
2956 const char *end;
Christopher Faulet95917132020-08-05 23:07:07 +02002957 long long int i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002958
2959 /* Try to decode a variable. */
2960 if (vars_check_arg(&args[0], NULL))
2961 return 1;
2962
2963 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002964 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002965 end = str + strlen(str);
Christopher Faulet95917132020-08-05 23:07:07 +02002966 i = read_int64(&str, end);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002967 if (*str != '\0') {
2968 memprintf(err, "expects an integer or a variable name");
2969 return 0;
2970 }
Christopher Faulet95917132020-08-05 23:07:07 +02002971
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002972 chunk_destroy(&args[0].data.str);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002973 args[0].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02002974 args[0].data.sint = i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002975 return 1;
2976}
2977
Willy Tarreau6204cd92016-03-10 16:33:04 +01002978/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002979 * If the arg contain an integer, the integer is returned in the
2980 * sample. If the arg contains a variable descriptor, it returns the
2981 * variable value.
2982 *
2983 * This function returns 0 if an error occurs, otherwise it returns 1.
2984 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002985static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002986{
2987 switch (arg->type) {
2988 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002989 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002990 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002991 return 1;
2992 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002993 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002994 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002995 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002996 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002997 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002998 return 0;
2999 return 1;
3000 default:
3001 return 0;
3002 }
3003}
3004
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003005/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01003006 * result.
3007 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003008static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003009{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003010 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003011 return 1;
3012}
3013
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003014/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003015 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01003016 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003017static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003018{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003019 struct sample tmp;
3020
Willy Tarreau7560dd42016-03-10 16:28:58 +01003021 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01003022 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003023 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003024 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003025 return 1;
3026}
3027
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003028/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003029 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01003030 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003031static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003032{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003033 struct sample tmp;
3034
Willy Tarreau7560dd42016-03-10 16:28:58 +01003035 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01003036 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003037 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003038 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003039 return 1;
3040}
3041
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003042/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003043 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01003044 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003045static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003046{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003047 struct sample tmp;
3048
Willy Tarreau7560dd42016-03-10 16:28:58 +01003049 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01003050 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003051 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003052 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003053 return 1;
3054}
3055
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003056static inline long long int arith_add(long long int a, long long int b)
3057{
3058 /* Prevent overflow and makes capped calculus.
3059 * We must ensure that the check calculus doesn't
3060 * exceed the signed 64 bits limits.
3061 *
3062 * +----------+----------+
3063 * | a<0 | a>=0 |
3064 * +------+----------+----------+
3065 * | b<0 | MIN-a>b | no check |
3066 * +------+----------+----------+
3067 * | b>=0 | no check | MAX-a<b |
3068 * +------+----------+----------+
3069 */
3070 if ((a ^ b) >= 0) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003071 /* signs are different. */
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003072 if (a < 0) {
3073 if (LLONG_MIN - a > b)
3074 return LLONG_MIN;
3075 }
3076 if (LLONG_MAX - a < b)
3077 return LLONG_MAX;
3078 }
3079 return a + b;
3080}
3081
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003082/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003083 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01003084 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003085static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003086{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003087 struct sample tmp;
3088
Willy Tarreau7560dd42016-03-10 16:28:58 +01003089 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01003090 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003091 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003092 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01003093 return 1;
3094}
3095
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003096/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003097 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01003098 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003099static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003100 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003101{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003102 struct sample tmp;
3103
Willy Tarreau7560dd42016-03-10 16:28:58 +01003104 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01003105 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003106 return 0;
3107
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003108 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
3109 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
3110 * of -LLONG_MIN and correct the result.
3111 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003112 if (tmp.data.u.sint == LLONG_MIN) {
3113 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
3114 if (smp->data.u.sint < LLONG_MAX)
3115 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003116 return 1;
3117 }
3118
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003119 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003120 * the second operand.
3121 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003122 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01003123 return 1;
3124}
3125
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003126/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003127 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003128 * If the result makes an overflow, then the largest possible quantity is
3129 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003130 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003131static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003132 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003133{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003134 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003135 long long int c;
3136
Willy Tarreau7560dd42016-03-10 16:28:58 +01003137 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01003138 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003139 return 0;
3140
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003141 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003142 if (!smp->data.u.sint || !tmp.data.u.sint) {
3143 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003144 return 1;
3145 }
3146
3147 /* The multiply between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003148 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003149 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003150 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3151 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003152 return 1;
3153 }
3154
3155 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003156 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003157
3158 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003159 if (smp->data.u.sint != c / tmp.data.u.sint) {
3160 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
3161 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003162 return 1;
3163 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003164 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003165 return 1;
3166 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003167 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01003168 return 1;
3169}
3170
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003171/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003172 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003173 * If arg_p makes the result overflow, then the largest possible quantity is
3174 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003175 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003176static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003177 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003178{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003179 struct sample tmp;
3180
Willy Tarreau7560dd42016-03-10 16:28:58 +01003181 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01003182 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003183 return 0;
3184
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003185 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003186 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003187 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003188 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003189 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3190 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003191 return 1;
3192 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003193 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003194 return 1;
3195 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003196 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01003197 return 1;
3198}
3199
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003200/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003201 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003202 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003203 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003204static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003205 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003206{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003207 struct sample tmp;
3208
Willy Tarreau7560dd42016-03-10 16:28:58 +01003209 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01003210 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003211 return 0;
3212
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003213 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003214 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003215 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003216 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003217 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3218 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003219 return 1;
3220 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003221 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003222 return 1;
3223 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003224 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01003225 return 1;
3226}
3227
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003228/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01003229 * result.
3230 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003231static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003232 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003233{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003234 if (smp->data.u.sint == LLONG_MIN)
3235 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003236 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003237 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003238 return 1;
3239}
3240
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003241/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003242 * false. The output is a BOOL.
3243 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003244static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003245 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003246{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003247 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003248 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003249 return 1;
3250}
3251
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003252/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003253 * truee. The output is a BOOL.
3254 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003255static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003256 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003257{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003258 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003259 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003260 return 1;
3261}
3262
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003263/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003264 * The output is a BOOL.
3265 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003266static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003267 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003268{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003269 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003270 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003271 return 1;
3272}
3273
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003274/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003275 * The output is a BOOL.
3276 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003277static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003278 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003279{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003280 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003281 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003282 return 1;
3283}
3284
Willy Tarreau280f42b2018-02-19 15:34:12 +01003285/* appends an optional const string, an optional variable contents and another
3286 * optional const string to an existing string.
3287 */
3288static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
3289{
Willy Tarreau83061a82018-07-13 11:56:34 +02003290 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003291 struct sample tmp;
3292 int max;
3293
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003294 trash = alloc_trash_chunk();
William Dauchye9970102021-01-11 11:05:58 +01003295 if (!trash)
3296 return 0;
3297
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003298 trash->data = smp->data.u.str.data;
3299 if (trash->data > trash->size - 1)
3300 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003301
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003302 memcpy(trash->area, smp->data.u.str.area, trash->data);
3303 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003304
3305 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003306 max = arg_p[0].data.str.data;
3307 if (max > trash->size - 1 - trash->data)
3308 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003309
3310 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003311 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
3312 trash->data += max;
3313 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003314 }
3315
3316 /* append second string (variable) if it's found and we can turn it
3317 * into a string.
3318 */
3319 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3320 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
3321 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
3322 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
3323
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003324 max = tmp.data.u.str.data;
3325 if (max > trash->size - 1 - trash->data)
3326 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003327
3328 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003329 memcpy(trash->area + trash->data, tmp.data.u.str.area,
3330 max);
3331 trash->data += max;
3332 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003333 }
3334 }
3335
3336 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003337 max = arg_p[2].data.str.data;
3338 if (max > trash->size - 1 - trash->data)
3339 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003340
3341 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003342 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
3343 trash->data += max;
3344 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003345 }
3346
3347 smp->data.u.str = *trash;
3348 smp->data.type = SMP_T_STR;
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003349 smp_dup(smp);
3350 free_trash_chunk(trash);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003351 return 1;
3352}
3353
3354/* This function checks the "concat" converter's arguments and extracts the
3355 * variable name and its scope.
3356 */
3357static int smp_check_concat(struct arg *args, struct sample_conv *conv,
3358 const char *file, int line, char **err)
3359{
3360 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003361 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
3362 memprintf(err, "failed to register variable name '%s'",
3363 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003364 return 0;
3365 }
3366 return 1;
3367}
3368
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003369/* Compares string with a variable containing a string. Return value
Tim Duesterhusca097c12018-04-27 21:18:45 +02003370 * is compatible with strcmp(3)'s return value.
3371 */
3372static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
3373{
3374 struct sample tmp;
3375 int max, result;
3376
3377 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3378 if (arg_p[0].type != ARGT_VAR)
3379 return 0;
3380 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
3381 return 0;
3382 if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp))
3383 return 0;
3384
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003385 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
3386 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003387 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003388 if (smp->data.u.str.data != tmp.data.u.str.data) {
3389 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02003390 result = -1;
3391 }
3392 else {
3393 result = 1;
3394 }
3395 }
3396 }
3397
3398 smp->data.u.sint = result;
3399 smp->data.type = SMP_T_SINT;
3400 return 1;
3401}
3402
Willy Tarreau2a8a2f02021-05-09 11:59:49 +02003403#if defined(HAVE_CRYPTO_memcmp)
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003404/* Compares bytestring with a variable containing a bytestring. Return value
3405 * is `true` if both bytestrings are bytewise identical and `false` otherwise.
3406 *
3407 * Comparison will be performed in constant time if both bytestrings are of
3408 * the same length. If the lengths differ execution time will not be constant.
3409 */
3410static int sample_conv_secure_memcmp(const struct arg *arg_p, struct sample *smp, void *private)
3411{
3412 struct sample tmp;
3413 int result;
3414
3415 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3416 if (arg_p[0].type != ARGT_VAR)
3417 return 0;
3418 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
3419 return 0;
3420 if (!sample_casts[tmp.data.type][SMP_T_BIN](&tmp))
3421 return 0;
3422
3423 if (smp->data.u.str.data != tmp.data.u.str.data) {
3424 smp->data.u.sint = 0;
3425 smp->data.type = SMP_T_BOOL;
3426 return 1;
3427 }
3428
3429 /* The following comparison is performed in constant time. */
3430 result = CRYPTO_memcmp(smp->data.u.str.area, tmp.data.u.str.area, smp->data.u.str.data);
3431
3432 smp->data.u.sint = result == 0;
3433 smp->data.type = SMP_T_BOOL;
3434 return 1;
3435}
3436#endif
3437
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02003438/* Takes a boolean as input. Returns the first argument if that boolean is true and
3439 * the second argument otherwise.
3440 */
3441static int sample_conv_iif(const struct arg *arg_p, struct sample *smp, void *private)
3442{
3443 smp->data.type = SMP_T_STR;
3444 smp->flags |= SMP_F_CONST;
3445
3446 if (smp->data.u.sint) {
3447 smp->data.u.str.data = arg_p[0].data.str.data;
3448 smp->data.u.str.area = arg_p[0].data.str.area;
3449 }
3450 else {
3451 smp->data.u.str.data = arg_p[1].data.str.data;
3452 smp->data.u.str.area = arg_p[1].data.str.area;
3453 }
3454
3455 return 1;
3456}
3457
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003458#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
3459#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
3460#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
3461
3462/*
3463 * Extract the field value of an input binary sample. Takes a mandatory argument:
3464 * the protocol buffers field identifier (dotted notation) internally represented
3465 * as an array of unsigned integers and its size.
3466 * Return 1 if the field was found, 0 if not.
3467 */
3468static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
3469{
3470 unsigned char *pos;
3471 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003472
3473 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003474 grpc_left = smp->data.u.str.data;
3475
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01003476 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003477 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003478
3479 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
3480
3481 pos += GRPC_MSG_HEADER_SZ;
3482 grpc_left -= GRPC_MSG_HEADER_SZ;
3483
3484 if (grpc_left < left)
3485 return 0;
3486
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01003487 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
3488 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003489
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003490 grpc_left -= grpc_msg_len;
3491 }
3492
3493 return 0;
3494}
3495
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003496static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
3497{
3498 unsigned char *pos;
3499 size_t left;
3500
3501 pos = (unsigned char *)smp->data.u.str.area;
3502 left = smp->data.u.str.data;
3503
3504 return protobuf_field_lookup(arg_p, smp, &pos, &left);
3505}
3506
3507static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
3508 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003509{
3510 if (!args[1].type) {
3511 args[1].type = ARGT_SINT;
3512 args[1].data.sint = PBUF_T_BINARY;
3513 }
3514 else {
3515 int pbuf_type;
3516
3517 pbuf_type = protobuf_type(args[1].data.str.area);
3518 if (pbuf_type == -1) {
3519 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
3520 return 0;
3521 }
3522
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003523 chunk_destroy(&args[1].data.str);
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003524 args[1].type = ARGT_SINT;
3525 args[1].data.sint = pbuf_type;
3526 }
3527
3528 return 1;
3529}
3530
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003531/*
3532 * Extract the tag value of an input binary sample. Takes a mandatory argument:
3533 * the FIX protocol tag identifier.
3534 * Return 1 if the tag was found, 0 if not.
3535 */
3536static int sample_conv_fix_tag_value(const struct arg *arg_p, struct sample *smp, void *private)
3537{
3538 struct ist value;
3539
3540 smp->flags &= ~SMP_F_MAY_CHANGE;
3541 value = fix_tag_value(ist2(smp->data.u.str.area, smp->data.u.str.data),
3542 arg_p[0].data.sint);
3543 if (!istlen(value)) {
3544 if (!isttest(value)) {
3545 /* value != IST_NULL, need more data */
3546 smp->flags |= SMP_F_MAY_CHANGE;
3547 }
3548 return 0;
3549 }
3550
3551 smp->data.u.str = ist2buf(value);
3552 smp->flags |= SMP_F_CONST;
3553
3554 return 1;
3555}
3556
3557/* This function checks the "fix_tag_value" converter configuration.
3558 * It expects a "known" (by HAProxy) tag name or ID.
3559 * Tag string names are converted to their ID counterpart because this is the
3560 * format they are sent over the wire.
3561 */
3562static int sample_conv_fix_value_check(struct arg *args, struct sample_conv *conv,
3563 const char *file, int line, char **err)
3564{
3565 struct ist str;
3566 unsigned int tag;
3567
3568 str = ist2(args[0].data.str.area, args[0].data.str.data);
3569 tag = fix_tagid(str);
3570 if (!tag) {
3571 memprintf(err, "Unknown FIX tag name '%s'", args[0].data.str.area);
3572 return 0;
3573 }
3574
3575 chunk_destroy(&args[0].data.str);
3576 args[0].type = ARGT_SINT;
3577 args[0].data.sint = tag;
3578
3579 return 1;
3580}
3581
3582/*
3583 * Checks that a buffer contains a valid FIX message
3584 *
3585 * Return 1 if the check could be run, 0 if not.
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003586 * The result of the analyse itself is stored in <smp> as a boolean
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003587 */
3588static int sample_conv_fix_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3589{
3590 struct ist msg;
3591
3592 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3593
3594 smp->flags &= ~SMP_F_MAY_CHANGE;
3595 switch (fix_validate_message(msg)) {
3596 case FIX_VALID_MESSAGE:
3597 smp->data.type = SMP_T_BOOL;
3598 smp->data.u.sint = 1;
3599 return 1;
3600 case FIX_NEED_MORE_DATA:
3601 smp->flags |= SMP_F_MAY_CHANGE;
3602 return 0;
3603 case FIX_INVALID_MESSAGE:
3604 smp->data.type = SMP_T_BOOL;
3605 smp->data.u.sint = 0;
3606 return 1;
3607 }
3608 return 0;
3609}
3610
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003611/*
3612 * Extract the field value of an input binary sample containing an MQTT packet.
3613 * Takes 2 mandatory arguments:
3614 * - packet type
3615 * - field name
3616 *
3617 * return 1 if the field was found, 0 if not.
3618 */
3619static int sample_conv_mqtt_field_value(const struct arg *arg_p, struct sample *smp, void *private)
3620{
3621 struct ist pkt, value;
3622 int type, fieldname_id;
3623
3624 pkt = ist2(smp->data.u.str.area, smp->data.u.str.data);
3625 type = arg_p[0].data.sint;
3626 fieldname_id = arg_p[1].data.sint;
3627
3628 smp->flags &= ~SMP_F_MAY_CHANGE;
3629 value = mqtt_field_value(pkt, type, fieldname_id);
3630 if (!istlen(value)) {
3631 if (isttest(value)) {
3632 /* value != IST_NULL, need more data */
3633 smp->flags |= SMP_F_MAY_CHANGE;
3634 }
3635 return 0;
3636 }
3637
3638 smp->data.u.str = ist2buf(value);
3639 smp->flags |= SMP_F_CONST;
3640 return 1;
3641}
3642
3643/*
3644 * this function checks the "mqtt_field_value" converter configuration.
3645 * It expects a known packet type name or ID and a field name, in this order
3646 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003647 * Args[0] will be turned into a MQTT_CPT_* value for direct matching when parsing
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003648 * a packet.
3649 */
3650static int sample_conv_mqtt_field_value_check(struct arg *args, struct sample_conv *conv,
3651 const char *file, int line, char **err)
3652{
3653 int type, fieldname_id;
3654
3655 /* check the MQTT packet type is valid */
3656 type = mqtt_typeid(ist2(args[0].data.str.area, args[0].data.str.data));
3657 if (type == MQTT_CPT_INVALID) {
3658 memprintf(err, "Unknown MQTT type '%s'", args[0].data.str.area);
3659 return 0;
3660 }
3661
3662 /* check the field name belongs to the MQTT packet type */
3663 fieldname_id = mqtt_check_type_fieldname(type, ist2(args[1].data.str.area, args[1].data.str.data));
3664 if (fieldname_id == MQTT_FN_INVALID) {
3665 memprintf(err, "Unknown MQTT field name '%s' for packet type '%s'", args[1].data.str.area,
3666 args[0].data.str.area);
3667 return 0;
3668 }
3669
3670 /* save numeric counterparts of type and field name */
3671 chunk_destroy(&args[0].data.str);
3672 chunk_destroy(&args[1].data.str);
3673 args[0].type = ARGT_SINT;
3674 args[0].data.sint = type;
3675 args[1].type = ARGT_SINT;
3676 args[1].data.sint = fieldname_id;
3677
3678 return 1;
3679}
3680
3681/*
3682 * Checks that <smp> contains a valid MQTT message
3683 *
3684 * The function returns 1 if the check was run to its end, 0 otherwise.
3685 * The result of the analyse itself is stored in <smp> as a boolean.
3686 */
3687static int sample_conv_mqtt_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3688{
3689 struct ist msg;
3690
3691 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3692
3693 smp->flags &= ~SMP_F_MAY_CHANGE;
3694 switch (mqtt_validate_message(msg, NULL)) {
3695 case FIX_VALID_MESSAGE:
3696 smp->data.type = SMP_T_BOOL;
3697 smp->data.u.sint = 1;
3698 return 1;
3699 case FIX_NEED_MORE_DATA:
3700 smp->flags |= SMP_F_MAY_CHANGE;
3701 return 0;
3702 case FIX_INVALID_MESSAGE:
3703 smp->data.type = SMP_T_BOOL;
3704 smp->data.u.sint = 0;
3705 return 1;
3706 }
3707 return 0;
3708}
3709
Tim Duesterhusca097c12018-04-27 21:18:45 +02003710/* This function checks the "strcmp" converter's arguments and extracts the
3711 * variable name and its scope.
3712 */
3713static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
3714 const char *file, int line, char **err)
3715{
Willy Tarreaua1169b62021-05-08 06:50:28 +02003716 if (!args[0].data.str.data) {
3717 memprintf(err, "missing variable name");
3718 return 0;
3719 }
3720
Tim Duesterhusca097c12018-04-27 21:18:45 +02003721 /* Try to decode a variable. */
3722 if (vars_check_arg(&args[0], NULL))
3723 return 1;
3724
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003725 memprintf(err, "failed to register variable name '%s'",
3726 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003727 return 0;
3728}
3729
Willy Tarreau2a8a2f02021-05-09 11:59:49 +02003730#if defined(HAVE_CRYPTO_memcmp)
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003731/* This function checks the "secure_memcmp" converter's arguments and extracts the
3732 * variable name and its scope.
3733 */
3734static int smp_check_secure_memcmp(struct arg *args, struct sample_conv *conv,
3735 const char *file, int line, char **err)
3736{
Willy Tarreaua1169b62021-05-08 06:50:28 +02003737 if (!args[0].data.str.data) {
3738 memprintf(err, "missing variable name");
3739 return 0;
3740 }
3741
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003742 /* Try to decode a variable. */
3743 if (vars_check_arg(&args[0], NULL))
3744 return 1;
3745
3746 memprintf(err, "failed to register variable name '%s'",
3747 args[0].data.str.area);
3748 return 0;
3749}
3750#endif
3751
Christopher Faulet4ccc12f2020-04-01 09:08:32 +02003752/**/
3753static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private)
3754{
3755 struct buffer *tmp;
3756 uint32_t n;
3757
3758 n = htonl((uint32_t)smp->data.u.sint);
3759 tmp = get_trash_chunk();
3760
3761 memcpy(b_head(tmp), &n, 4);
3762 b_add(tmp, 4);
3763
3764 smp->data.u.str = *tmp;
3765 smp->data.type = SMP_T_BIN;
3766 return 1;
3767}
3768
Christopher Fauletea159d62020-04-01 16:21:44 +02003769/**/
3770static int sample_conv_cut_crlf(const struct arg *arg_p, struct sample *smp, void *private)
3771{
3772 char *p;
3773 size_t l;
3774
3775 p = smp->data.u.str.area;
3776 for (l = 0; l < smp->data.u.str.data; l++) {
3777 if (*(p+l) == '\r' || *(p+l) == '\n')
3778 break;
3779 }
3780 smp->data.u.str.data = l;
3781 return 1;
3782}
3783
Christopher Faulet51fc9d12020-04-01 17:24:41 +02003784/**/
3785static int sample_conv_ltrim(const struct arg *arg_p, struct sample *smp, void *private)
3786{
3787 char *delimiters, *p;
3788 size_t dlen, l;
3789
3790 delimiters = arg_p[0].data.str.area;
3791 dlen = arg_p[0].data.str.data;
3792
3793 l = smp->data.u.str.data;
3794 p = smp->data.u.str.area;
3795 while (l && memchr(delimiters, *p, dlen) != NULL) {
3796 p++;
3797 l--;
3798 }
3799
3800 smp->data.u.str.area = p;
3801 smp->data.u.str.data = l;
3802 return 1;
3803}
3804
Christopher Faulet568415a2020-04-01 17:24:47 +02003805/**/
3806static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *private)
3807{
3808 char *delimiters, *p;
3809 size_t dlen, l;
3810
3811 delimiters = arg_p[0].data.str.area;
3812 dlen = arg_p[0].data.str.data;
3813
3814 l = smp->data.u.str.data;
3815 p = smp->data.u.str.area + l - 1;
3816 while (l && memchr(delimiters, *p, dlen) != NULL) {
3817 p--;
3818 l--;
3819 }
3820
3821 smp->data.u.str.data = l;
3822 return 1;
3823}
3824
Alex51c8ad42021-04-15 16:45:15 +02003825/* This function checks the "json_query" converter's arguments. */
3826static int sample_check_json_query(struct arg *arg, struct sample_conv *conv,
3827 const char *file, int line, char **err)
3828{
3829 if (arg[0].data.str.data == 0) {
3830 memprintf(err, "json_path must not be empty");
3831 return 0;
3832 }
3833
3834 if (arg[1].data.str.data != 0) {
3835 if (strcmp(arg[1].data.str.area, "int") != 0) {
3836 memprintf(err, "output_type only supports \"int\" as argument");
3837 return 0;
3838 } else {
3839 arg[1].type = ARGT_SINT;
3840 arg[1].data.sint = 0;
3841 }
3842 }
3843 return 1;
3844}
3845
3846/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
3847 * the recommendation for interoperable integers in section 6 of RFC 7159.
3848 */
3849#define JSON_INT_MAX ((1LL << 53) - 1)
3850#define JSON_INT_MIN (-JSON_INT_MAX)
3851
3852/* This sample function get the value from a given json string.
3853 * The mjson library is used to parse the JSON struct
3854 */
3855static int sample_conv_json_query(const struct arg *args, struct sample *smp, void *private)
3856{
3857 struct buffer *trash = get_trash_chunk();
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003858 const char *token; /* holds the temporary string from mjson_find */
3859 int token_size; /* holds the length of <token> */
Alex51c8ad42021-04-15 16:45:15 +02003860
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003861 enum mjson_tok token_type;
Alex51c8ad42021-04-15 16:45:15 +02003862
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003863 token_type = mjson_find(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &token, &token_size);
3864
3865 switch (token_type) {
Alex51c8ad42021-04-15 16:45:15 +02003866 case MJSON_TOK_NUMBER:
3867 if (args[1].type == ARGT_SINT) {
Willy Tarreaufb601952021-05-14 08:51:53 +02003868 smp->data.u.sint = strtoll(token, NULL, 0);
Alex51c8ad42021-04-15 16:45:15 +02003869
3870 if (smp->data.u.sint < JSON_INT_MIN || smp->data.u.sint > JSON_INT_MAX)
3871 return 0;
3872
3873 smp->data.type = SMP_T_SINT;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003874
3875 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003876 } else {
3877 double double_val;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003878
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003879 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 +02003880 return 0;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003881
3882 trash->data = snprintf(trash->area,trash->size,"%g",double_val);
3883 smp->data.u.str = *trash;
3884 smp->data.type = SMP_T_STR;
3885
3886 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003887 }
Alex51c8ad42021-04-15 16:45:15 +02003888 case MJSON_TOK_TRUE:
3889 smp->data.type = SMP_T_BOOL;
3890 smp->data.u.sint = 1;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003891
3892 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003893 case MJSON_TOK_FALSE:
3894 smp->data.type = SMP_T_BOOL;
3895 smp->data.u.sint = 0;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003896
3897 return 1;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003898 case MJSON_TOK_STRING: {
3899 int len;
3900
3901 len = mjson_get_string(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, trash->area, trash->size);
3902
3903 if (len == -1) {
Alex51c8ad42021-04-15 16:45:15 +02003904 /* invalid string */
3905 return 0;
Alex51c8ad42021-04-15 16:45:15 +02003906 }
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003907
3908 trash->data = len;
3909 smp->data.u.str = *trash;
3910 smp->data.type = SMP_T_STR;
3911
3912 return 1;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003913 }
Tim Duesterhus8f3bc8f2021-04-15 18:14:32 +02003914 case MJSON_TOK_NULL:
3915 case MJSON_TOK_ARRAY:
3916 case MJSON_TOK_OBJECT:
3917 /* We cannot handle these. */
3918 return 0;
3919 case MJSON_TOK_INVALID:
3920 /* Nothing matches the query. */
3921 return 0;
3922 case MJSON_TOK_KEY:
3923 /* This is not a valid return value according to the
3924 * mjson documentation, but we handle it to benefit
3925 * from '-Wswitch'.
3926 */
Alex51c8ad42021-04-15 16:45:15 +02003927 return 0;
3928 }
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003929
3930 my_unreachable();
3931 return 0;
Alex51c8ad42021-04-15 16:45:15 +02003932}
3933
3934
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003935/************************************************************************/
3936/* All supported sample fetch functions must be declared here */
3937/************************************************************************/
3938
3939/* force TRUE to be returned at the fetch level */
3940static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003941smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003942{
Willy Tarreau280f42b2018-02-19 15:34:12 +01003943 if (!smp_make_rw(smp))
3944 return 0;
3945
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003946 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003947 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003948 return 1;
3949}
3950
3951/* force FALSE to be returned at the fetch level */
3952static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003953smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003954{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003955 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003956 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003957 return 1;
3958}
3959
3960/* retrieve environment variable $1 as a string */
3961static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003962smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003963{
3964 char *env;
3965
Christopher Faulete6e7a582021-01-29 11:29:28 +01003966 if (args[0].type != ARGT_STR)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003967 return 0;
3968
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003969 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003970 if (!env)
3971 return 0;
3972
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003973 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003974 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003975 smp->data.u.str.area = env;
3976 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003977 return 1;
3978}
3979
Damien Claisseae6f1252019-10-30 15:57:28 +00003980/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3981 * optional string representing the unit of the result: "s" for seconds, "ms" for
3982 * milliseconds and "us" for microseconds.
3983 * Returns 0 on error and non-zero if OK.
3984 */
3985int smp_check_date_unit(struct arg *args, char **err)
3986{
3987 if (args[1].type == ARGT_STR) {
Christopher Faulet95917132020-08-05 23:07:07 +02003988 long long int unit;
3989
Damien Claisseae6f1252019-10-30 15:57:28 +00003990 if (strcmp(args[1].data.str.area, "s") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003991 unit = TIME_UNIT_S;
Damien Claisseae6f1252019-10-30 15:57:28 +00003992 }
3993 else if (strcmp(args[1].data.str.area, "ms") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003994 unit = TIME_UNIT_MS;
Damien Claisseae6f1252019-10-30 15:57:28 +00003995 }
3996 else if (strcmp(args[1].data.str.area, "us") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003997 unit = TIME_UNIT_US;
Damien Claisseae6f1252019-10-30 15:57:28 +00003998 }
3999 else {
4000 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
4001 args[1].data.str.area);
4002 return 0;
4003 }
Christopher Faulet95917132020-08-05 23:07:07 +02004004
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004005 chunk_destroy(&args[1].data.str);
Damien Claisseae6f1252019-10-30 15:57:28 +00004006 args[1].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02004007 args[1].data.sint = unit;
Damien Claisseae6f1252019-10-30 15:57:28 +00004008 }
4009 else if (args[1].type != ARGT_STOP) {
4010 memprintf(err, "Unexpected arg type");
4011 return 0;
4012 }
4013
4014 return 1;
4015}
4016
4017/* retrieve the current local date in epoch time, converts it to milliseconds
4018 * or microseconds if asked to in optional args[1] unit param, and applies an
4019 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004020 */
4021static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004022smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004023{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004024 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004025
Damien Claisseae6f1252019-10-30 15:57:28 +00004026 /* report in milliseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01004027 if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
Damien Claisseae6f1252019-10-30 15:57:28 +00004028 smp->data.u.sint *= 1000;
4029 smp->data.u.sint += date.tv_usec / 1000;
4030 }
4031 /* report in microseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01004032 else if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
Damien Claisseae6f1252019-10-30 15:57:28 +00004033 smp->data.u.sint *= 1000000;
4034 smp->data.u.sint += date.tv_usec;
4035 }
4036
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004037 /* add offset */
Christopher Faulete6e7a582021-01-29 11:29:28 +01004038 if (args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004039 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004040
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004041 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004042 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4043 return 1;
4044}
4045
Etienne Carrierea792a0a2018-01-17 13:43:24 +01004046/* retrieve the current microsecond part of the date */
4047static int
4048smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
4049{
4050 smp->data.u.sint = date.tv_usec;
4051 smp->data.type = SMP_T_SINT;
4052 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4053 return 1;
4054}
4055
4056
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01004057/* returns the hostname */
4058static int
4059smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
4060{
4061 smp->data.type = SMP_T_STR;
4062 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004063 smp->data.u.str.area = hostname;
4064 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01004065 return 1;
4066}
4067
Willy Tarreau0f30d262014-11-24 16:02:05 +01004068/* returns the number of processes */
4069static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004070smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01004071{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004072 smp->data.type = SMP_T_SINT;
Willy Tarreau91358592021-06-15 08:08:04 +02004073 smp->data.u.sint = 1;
Willy Tarreau0f30d262014-11-24 16:02:05 +01004074 return 1;
4075}
4076
4077/* returns the number of the current process (between 1 and nbproc */
4078static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004079smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01004080{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004081 smp->data.type = SMP_T_SINT;
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004082 smp->data.u.sint = 1;
Willy Tarreau0f30d262014-11-24 16:02:05 +01004083 return 1;
4084}
4085
Christopher Faulet34adb2a2017-11-21 21:45:38 +01004086/* returns the number of the current thread (between 1 and nbthread */
4087static int
4088smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
4089{
4090 smp->data.type = SMP_T_SINT;
4091 smp->data.u.sint = tid;
4092 return 1;
4093}
4094
Willy Tarreau84310e22014-02-14 11:59:04 +01004095/* generate a random 32-bit integer for whatever purpose, with an optional
4096 * range specified in argument.
4097 */
4098static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004099smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01004100{
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01004101 smp->data.u.sint = ha_random32();
Willy Tarreau84310e22014-02-14 11:59:04 +01004102
4103 /* reduce if needed. Don't do a modulo, use all bits! */
Christopher Faulete6e7a582021-01-29 11:29:28 +01004104 if (args[0].type == ARGT_SINT)
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01004105 smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32;
Willy Tarreau84310e22014-02-14 11:59:04 +01004106
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004107 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01004108 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4109 return 1;
4110}
4111
Willy Tarreau0f30d262014-11-24 16:02:05 +01004112/* returns true if the current process is stopping */
4113static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004114smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01004115{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004116 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004117 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01004118 return 1;
4119}
4120
Willy Tarreau70fe9442018-11-22 16:07:39 +01004121/* returns the number of calls of the current stream's process_stream() */
4122static int
4123smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
4124{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004125 if (!smp->strm)
4126 return 0;
4127
Willy Tarreau70fe9442018-11-22 16:07:39 +01004128 smp->data.type = SMP_T_SINT;
4129 smp->data.u.sint = smp->strm->task->calls;
4130 return 1;
4131}
4132
4133/* returns the average number of nanoseconds spent processing the stream per call */
4134static int
4135smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
4136{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004137 if (!smp->strm)
4138 return 0;
4139
Willy Tarreau70fe9442018-11-22 16:07:39 +01004140 smp->data.type = SMP_T_SINT;
4141 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
4142 return 1;
4143}
4144
4145/* returns the total number of nanoseconds spent processing the stream */
4146static int
4147smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
4148{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004149 if (!smp->strm)
4150 return 0;
4151
Willy Tarreau70fe9442018-11-22 16:07:39 +01004152 smp->data.type = SMP_T_SINT;
4153 smp->data.u.sint = smp->strm->task->cpu_time;
4154 return 1;
4155}
4156
4157/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
4158static int
4159smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
4160{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004161 if (!smp->strm)
4162 return 0;
4163
Willy Tarreau70fe9442018-11-22 16:07:39 +01004164 smp->data.type = SMP_T_SINT;
4165 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
4166 return 1;
4167}
4168
4169/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
4170static int
4171smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
4172{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004173 if (!smp->strm)
4174 return 0;
4175
Willy Tarreau70fe9442018-11-22 16:07:39 +01004176 smp->data.type = SMP_T_SINT;
4177 smp->data.u.sint = smp->strm->task->lat_time;
4178 return 1;
4179}
4180
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004181static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
4182{
4183 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004184 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004185 smp->data.u.str.area = args[0].data.str.area;
4186 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004187 return 1;
4188}
4189
4190static int smp_check_const_bool(struct arg *args, char **err)
4191{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004192 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
4193 strcasecmp(args[0].data.str.area, "1") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004194 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004195 args[0].type = ARGT_SINT;
4196 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004197 return 1;
4198 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004199 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
4200 strcasecmp(args[0].data.str.area, "0") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004201 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004202 args[0].type = ARGT_SINT;
4203 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004204 return 1;
4205 }
4206 memprintf(err, "Expects 'true', 'false', '0' or '1'");
4207 return 0;
4208}
4209
4210static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
4211{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004212 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004213 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004214 return 1;
4215}
4216
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02004217static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004218{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004219 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004220 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004221 return 1;
4222}
4223
4224static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
4225{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004226 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004227 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004228 return 1;
4229}
4230
4231static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
4232{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004233 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004234 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004235 return 1;
4236}
4237
4238static int smp_check_const_bin(struct arg *args, char **err)
4239{
David Carlier64a16ab2016-04-08 10:37:02 +01004240 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004241 int binstrlen;
4242
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004243 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004244 return 0;
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004245 chunk_destroy(&args[0].data.str);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004246 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004247 args[0].data.str.area = binstr;
4248 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004249 return 1;
4250}
4251
4252static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
4253{
4254 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004255 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004256 smp->data.u.str.area = args[0].data.str.area;
4257 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004258 return 1;
4259}
4260
4261static int smp_check_const_meth(struct arg *args, char **err)
4262{
4263 enum http_meth_t meth;
4264 int i;
4265
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004266 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004267 if (meth != HTTP_METH_OTHER) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004268 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004269 args[0].type = ARGT_SINT;
4270 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004271 } else {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05004272 /* Check method avalaibility. A method is a token defined as :
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004273 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
4274 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
4275 * token = 1*tchar
4276 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004277 for (i = 0; i < args[0].data.str.data; i++) {
4278 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004279 memprintf(err, "expects valid method.");
4280 return 0;
4281 }
4282 }
4283 }
4284 return 1;
4285}
4286
4287static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
4288{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004289 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004290 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004291 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004292 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004293 smp->data.u.meth.str.area = "";
4294 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004295 } else {
4296 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004297 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004298 smp->data.u.meth.str.area = args[0].data.str.area;
4299 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004300 }
4301 return 1;
4302}
4303
Luca Schimweg8a694b82019-09-10 15:42:52 +02004304// This function checks the "uuid" sample's arguments.
4305// Function won't get called when no parameter is specified (maybe a bug?)
4306static int smp_check_uuid(struct arg *args, char **err)
4307{
4308 if (!args[0].type) {
4309 args[0].type = ARGT_SINT;
4310 args[0].data.sint = 4;
4311 }
4312 else if (args[0].data.sint != 4) {
4313 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
4314 return 0;
4315 }
4316
4317 return 1;
4318}
4319
4320// Generate a RFC4122 UUID (default is v4 = fully random)
4321static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
4322{
4323 if (args[0].data.sint == 4 || !args[0].type) {
Willy Tarreauee3bcdd2020-03-08 17:48:17 +01004324 ha_generate_uuid(&trash);
Luca Schimweg8a694b82019-09-10 15:42:52 +02004325 smp->data.type = SMP_T_STR;
4326 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4327 smp->data.u.str = trash;
4328 return 1;
4329 }
4330
4331 // more implementations of other uuid formats possible here
4332 return 0;
4333}
4334
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004335/* Note: must not be declared <const> as its list will be overwritten.
4336 * Note: fetches that may return multiple types must be declared as the lowest
4337 * common denominator, the type that can be casted into all other ones. For
4338 * instance IPv4/IPv6 must be declared IPv4.
4339 */
4340static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau0209c972021-03-26 12:03:11 +01004341 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4342 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4343 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_CONST },
4344 { "date", smp_fetch_date, ARG2(0,SINT,STR), smp_check_date_unit, SMP_T_SINT, SMP_USE_CONST },
4345 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4346 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_CONST },
4347 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_CONST },
4348 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4349 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4350 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_CONST },
Willy Tarreau0f30d262014-11-24 16:02:05 +01004351 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau0209c972021-03-26 12:03:11 +01004352 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_CONST },
Willy Tarreau70fe9442018-11-22 16:07:39 +01004353
4354 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4355 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4356 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4357 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4358 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004359
Willy Tarreau0209c972021-03-26 12:03:11 +01004360 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_CONST },
4361 { "bool", smp_fetch_const_bool, ARG1(1,STR), smp_check_const_bool, SMP_T_BOOL, SMP_USE_CONST },
4362 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_CONST },
4363 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_CONST },
4364 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_CONST },
4365 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_CONST },
4366 { "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 +02004367
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004368 { /* END */ },
4369}};
4370
Willy Tarreau0108d902018-11-25 19:14:37 +01004371INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
4372
Emeric Brun107ca302010-01-04 16:16:05 +01004373/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02004374static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Moemen MHEDHBI848216f2021-04-02 01:05:07 +02004375 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
4376 { "b64dec", sample_conv_base642bin, 0, NULL, SMP_T_STR, SMP_T_BIN },
4377 { "base64", sample_conv_bin2base64, 0, NULL, SMP_T_BIN, SMP_T_STR },
4378 { "ub64enc", sample_conv_bin2base64url,0, NULL, SMP_T_BIN, SMP_T_STR },
4379 { "ub64dec", sample_conv_base64url2bin,0, NULL, SMP_T_STR, SMP_T_BIN },
4380 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
4381 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
4382 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Marcin Deranek40ca09c2021-07-13 14:05:24 +02004383 { "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 +02004384 { "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 +02004385 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
4386 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
4387 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
4388 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4389 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4390 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4391 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4392 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4393 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4394 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4395 { "xxh3", sample_conv_xxh3, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4396 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4397 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4398 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
4399 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
4400 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4401 { "word", sample_conv_word, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4402 { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
4403 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02004404#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01004405 { "sha2", sample_conv_sha2, ARG1(0, SINT), smp_check_sha2, SMP_T_BIN, SMP_T_BIN },
Ilya Shipitsin2c481d02021-03-26 23:35:31 +05004406#ifdef EVP_CIPH_GCM_MODE
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02004407 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
4408#endif
Patrick Gansterer8e366512020-04-22 16:47:57 +02004409 { "digest", sample_conv_crypto_digest, ARG1(1,STR), check_crypto_digest, SMP_T_BIN, SMP_T_BIN },
4410 { "hmac", sample_conv_crypto_hmac, ARG2(2,STR,STR), check_crypto_hmac, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02004411#endif
Willy Tarreau280f42b2018-02-19 15:34:12 +01004412 { "concat", sample_conv_concat, ARG3(1,STR,STR,STR), smp_check_concat, SMP_T_STR, SMP_T_STR },
Tim Duesterhusca097c12018-04-27 21:18:45 +02004413 { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT },
Willy Tarreau2a8a2f02021-05-09 11:59:49 +02004414#if defined(HAVE_CRYPTO_memcmp)
Tim Duesterhusf38175c2020-06-09 11:48:42 +02004415 { "secure_memcmp", sample_conv_secure_memcmp, ARG1(1,STR), smp_check_secure_memcmp, SMP_T_BIN, SMP_T_BOOL },
4416#endif
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01004417
4418 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01004419 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
4420 { "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 +01004421
Baptiste Assmanne138dda2020-10-22 15:39:03 +02004422 /* FIX converters */
4423 { "fix_is_valid", sample_conv_fix_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4424 { "fix_tag_value", sample_conv_fix_tag_value, ARG1(1,STR), sample_conv_fix_value_check, SMP_T_BIN, SMP_T_BIN },
4425
Baptiste Assmanne279ca62020-10-27 18:10:06 +01004426 /* MQTT converters */
4427 { "mqtt_is_valid", sample_conv_mqtt_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4428 { "mqtt_field_value", sample_conv_mqtt_field_value, ARG2(2,STR,STR), sample_conv_mqtt_field_value_check, SMP_T_BIN, SMP_T_STR },
4429
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02004430 { "iif", sample_conv_iif, ARG2(2, STR, STR), NULL, SMP_T_BOOL, SMP_T_STR },
4431
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02004432 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4433 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4434 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4435 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
4436 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4437 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4438 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4439 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4440 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4441 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4442 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4443 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4444 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4445 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01004446
Christopher Fauletea159d62020-04-01 16:21:44 +02004447 { "htonl", sample_conv_htonl, 0, NULL, SMP_T_SINT, SMP_T_BIN },
4448 { "cut_crlf", sample_conv_cut_crlf, 0, NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet51fc9d12020-04-01 17:24:41 +02004449 { "ltrim", sample_conv_ltrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet568415a2020-04-01 17:24:47 +02004450 { "rtrim", sample_conv_rtrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Alex51c8ad42021-04-15 16:45:15 +02004451 { "json_query", sample_conv_json_query, ARG2(1,STR,STR), sample_check_json_query , SMP_T_STR, SMP_T_ANY },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02004452 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01004453}};
4454
Willy Tarreau0108d902018-11-25 19:14:37 +01004455INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);