blob: 6bc8e5fdab02b5240d74bb879ec5b43712cca956 [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 Tarreau9b340a02022-03-29 16:39:24 +0200328 [SMP_CKP_CFG_PARSER] = "configuration parser",
329 [SMP_CKP_CLI_PARSER] = "CLI parser",
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100330};
331
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100332/* This function returns the type of the data returned by the sample_expr.
333 * It assumes that the <expr> and all of its converters are properly
334 * initialized.
335 */
336inline
337int smp_expr_output_type(struct sample_expr *expr)
338{
339 struct sample_conv_expr *smp_expr;
340
341 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
342 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
343 return smp_expr->conv->out_type;
344 }
345 return expr->fetch->out_type;
346}
347
348
Willy Tarreau80aca902013-01-07 15:42:20 +0100349/* fill the trash with a comma-delimited list of source names for the <use> bit
350 * field which must be composed of a non-null set of SMP_USE_* flags. The return
351 * value is the pointer to the string in the trash buffer.
352 */
353const char *sample_src_names(unsigned int use)
354{
355 int bit;
356
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200357 trash.data = 0;
358 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100359 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
360 if (!(use & ~((1 << bit) - 1)))
361 break; /* no more bits */
362
363 if (!(use & (1 << bit)))
364 continue; /* bit not set */
365
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200366 trash.data += snprintf(trash.area + trash.data,
367 trash.size - trash.data, "%s%s",
368 (use & ((1 << bit) - 1)) ? "," : "",
369 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100370 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200371 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100372}
373
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100374/* return a pointer to the correct sample checkpoint name, or "unknown" when
375 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
376 * if set.
377 */
378const char *sample_ckp_names(unsigned int use)
379{
380 int bit;
381
382 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
383 if (use & (1 << bit))
384 return fetch_ckp_names[bit];
385 return "unknown sample check place, please report this bug";
386}
387
Emeric Brun107ca302010-01-04 16:16:05 +0100388/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100389 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
390 * for next parsing sessions. The fetch keywords capabilities are also computed
391 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100392 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100393void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100394{
Willy Tarreau80aca902013-01-07 15:42:20 +0100395 struct sample_fetch *sf;
396 int bit;
397
398 for (sf = kwl->kw; sf->kw != NULL; sf++) {
399 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
400 if (sf->use & (1 << bit))
401 sf->val |= fetch_cap[bit];
402 }
Willy Tarreau2b718102021-04-21 07:32:39 +0200403 LIST_APPEND(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100404}
405
406/*
Willy Tarreau12785782012-04-27 21:37:17 +0200407 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100408 * parsing sessions.
409 */
Willy Tarreau12785782012-04-27 21:37:17 +0200410void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100411{
Willy Tarreau2b718102021-04-21 07:32:39 +0200412 LIST_APPEND(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100413}
414
415/*
Willy Tarreau12785782012-04-27 21:37:17 +0200416 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100417 * string of <len> in buffer <kw>.
418 *
419 */
Willy Tarreau12785782012-04-27 21:37:17 +0200420struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100421{
422 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200423 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100424
Willy Tarreau12785782012-04-27 21:37:17 +0200425 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100426 for (index = 0; kwl->kw[index].kw != NULL; index++) {
427 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
428 kwl->kw[index].kw[len] == '\0')
429 return &kwl->kw[index];
430 }
431 }
432 return NULL;
433}
434
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100435/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100436 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100437 * <idx> is the index of the next sample fetch entry. It is used as private
438 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100439 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100440 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100441 * the list is reached.
442 */
443struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
444{
445 struct sample_fetch_kw_list *kwl;
446 struct sample_fetch *base;
447
448 if (!current) {
449 /* Get first kwl entry. */
450 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
451 (*idx) = 0;
452 } else {
453 /* Get kwl corresponding to the curret entry. */
454 base = current + 1 - (*idx);
455 kwl = container_of(base, struct sample_fetch_kw_list, kw);
456 }
457
458 while (1) {
459
460 /* Check if kwl is the last entry. */
461 if (&kwl->list == &sample_fetches.list)
462 return NULL;
463
464 /* idx contain the next keyword. If it is available, return it. */
465 if (kwl->kw[*idx].kw) {
466 (*idx)++;
467 return &kwl->kw[(*idx)-1];
468 }
469
470 /* get next entry in the main list, and return NULL if the end is reached. */
471 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
472
473 /* Set index to 0, ans do one other loop. */
474 (*idx) = 0;
475 }
476}
477
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100478/* This function browses the list of available converters. <current> is
479 * the last used converter. If it is the first call, it must set to NULL.
480 * <idx> is the index of the next converter entry. It is used as private
481 * value. It is useless to initiate it.
482 *
483 * It returns always the next sample_conv entry, and NULL when the end of
484 * the list is reached.
485 */
486struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
487{
488 struct sample_conv_kw_list *kwl;
489 struct sample_conv *base;
490
491 if (!current) {
492 /* Get first kwl entry. */
493 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
494 (*idx) = 0;
495 } else {
496 /* Get kwl corresponding to the curret entry. */
497 base = current + 1 - (*idx);
498 kwl = container_of(base, struct sample_conv_kw_list, kw);
499 }
500
501 while (1) {
502 /* Check if kwl is the last entry. */
503 if (&kwl->list == &sample_convs.list)
504 return NULL;
505
506 /* idx contain the next keyword. If it is available, return it. */
507 if (kwl->kw[*idx].kw) {
508 (*idx)++;
509 return &kwl->kw[(*idx)-1];
510 }
511
512 /* get next entry in the main list, and return NULL if the end is reached. */
513 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
514
515 /* Set index to 0, ans do one other loop. */
516 (*idx) = 0;
517 }
518}
519
Emeric Brun107ca302010-01-04 16:16:05 +0100520/*
Willy Tarreau12785782012-04-27 21:37:17 +0200521 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100522 * string of <len> in buffer <kw>.
523 *
524 */
Willy Tarreau12785782012-04-27 21:37:17 +0200525struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100526{
527 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200528 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100529
Willy Tarreau12785782012-04-27 21:37:17 +0200530 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100531 for (index = 0; kwl->kw[index].kw != NULL; index++) {
532 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
533 kwl->kw[index].kw[len] == '\0')
534 return &kwl->kw[index];
535 }
536 }
537 return NULL;
538}
539
Emeric Brun107ca302010-01-04 16:16:05 +0100540/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200541/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100542/******************************************************************/
543
Willy Tarreau342acb42012-04-23 22:03:39 +0200544static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100545{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200546 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200547 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100548 return 1;
549}
550
Willy Tarreau342acb42012-04-23 22:03:39 +0200551static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100552{
Willy Tarreau83061a82018-07-13 11:56:34 +0200553 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100554
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200555 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100556 return 0;
557
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200558 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200559 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200560 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100561 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100562
563 return 1;
564}
565
Willy Tarreau342acb42012-04-23 22:03:39 +0200566static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100567{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200568 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200569 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100570 return 1;
571}
572
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200573static int c_ipv62ip(struct sample *smp)
574{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200575 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200576 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100577 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200578 return 1;
579}
580
Willy Tarreau342acb42012-04-23 22:03:39 +0200581static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100582{
Willy Tarreau83061a82018-07-13 11:56:34 +0200583 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100584
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200585 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100586 return 0;
587
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200588 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200589 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200590 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100591 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100592 return 1;
593}
594
595/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200596static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100597{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200598 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100599}
600*/
601
Willy Tarreau342acb42012-04-23 22:03:39 +0200602static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100603{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200604 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200605 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100606 return 1;
607}
608
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200609static int c_int2ipv6(struct sample *smp)
610{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200611 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
612 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200613 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200614 return 1;
615}
616
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100617static int c_str2addr(struct sample *smp)
618{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200619 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
620 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100621 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200622 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100623 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100624 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100625 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200626 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100627 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100628 return 1;
629}
630
Willy Tarreau342acb42012-04-23 22:03:39 +0200631static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100632{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200633 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100634 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200635 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100636 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100637 return 1;
638}
639
Willy Tarreau342acb42012-04-23 22:03:39 +0200640static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100641{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200642 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100643 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200644 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100645 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100646 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100647}
648
Emeric Brun4b9e8022014-11-03 18:17:10 +0100649/*
650 * The NULL char always enforces the end of string if it is met.
651 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100652 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200653static int c_bin2str(struct sample *smp)
654{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100655 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200656
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200657 for (i = 0; i < smp->data.u.str.data; i++) {
658 if (!smp->data.u.str.area[i]) {
659 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100660 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100661 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200662 }
Christopher Faulet472ad512020-04-30 09:57:40 +0200663 smp->data.type = SMP_T_STR;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200664 return 1;
665}
666
Willy Tarreau342acb42012-04-23 22:03:39 +0200667static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100668{
Willy Tarreau83061a82018-07-13 11:56:34 +0200669 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100670 char *pos;
671
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200672 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100673 if (!pos)
674 return 0;
675
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200676 trash->size = trash->size - (pos - trash->area);
677 trash->area = pos;
678 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200679 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200680 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100681 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100682 return 1;
683}
684
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800685/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200686 * For strings and binary blocks, it also provides a known allocated size with
687 * a length that is capped to the size, and ensures a trailing zero is always
688 * appended for strings. This is necessary for some operations which may
689 * require to extend the length. It returns 0 if it fails, 1 on success.
690 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100691int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200692{
Willy Tarreau83061a82018-07-13 11:56:34 +0200693 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200694
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200695 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100696 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100697 case SMP_T_SINT:
698 case SMP_T_ADDR:
699 case SMP_T_IPV4:
700 case SMP_T_IPV6:
701 /* These type are not const. */
702 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200703
Christopher Fauletec100512017-07-24 15:38:41 +0200704 case SMP_T_METH:
705 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
706 break;
707 /* Fall through */
708
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100709 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200710 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100711 trash->data = smp->data.type == SMP_T_STR ?
712 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200713 if (trash->data > trash->size - 1)
714 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200715
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100716 memcpy(trash->area, smp->data.type == SMP_T_STR ?
717 smp->data.u.str.area : smp->data.u.meth.str.area,
718 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200719 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200720 smp->data.u.str = *trash;
721 break;
722
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100723 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100724 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200725 trash->data = smp->data.u.str.data;
726 if (trash->data > trash->size)
727 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200728
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200729 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200730 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100731 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200732
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100733 default:
734 /* Other cases are unexpected. */
735 return 0;
736 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100737
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100738 /* remove const flag */
739 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200740 return 1;
741}
742
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100743int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100744{
745 return 1;
746}
747
Willy Tarreau342acb42012-04-23 22:03:39 +0200748static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100749{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200750 const char *str;
751 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100752
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200753 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100754 return 0;
755
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200756 str = smp->data.u.str.area;
757 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100758
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200759 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200760 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100761 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100762 return 1;
763}
764
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100765static int c_str2meth(struct sample *smp)
766{
767 enum http_meth_t meth;
768 int len;
769
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200770 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100771 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200772 len = smp->data.u.str.data;
773 smp->data.u.meth.str.area = smp->data.u.str.area;
774 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100775 }
776 else
777 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200778 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200779 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100780 return 1;
781}
782
783static int c_meth2str(struct sample *smp)
784{
785 int len;
786 enum http_meth_t meth;
787
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200788 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100789 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200790 len = smp->data.u.meth.str.data;
791 smp->data.u.str.area = smp->data.u.meth.str.area;
792 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200793 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100794 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200795 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100796 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200797 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200798 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200799 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100800 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200801 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100802 }
803 else {
804 /* Unknown method */
805 return 0;
806 }
807 return 1;
808}
809
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200810static int c_addr2bin(struct sample *smp)
811{
Willy Tarreau83061a82018-07-13 11:56:34 +0200812 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200813
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200814 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200815 chk->data = 4;
816 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200817 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200818 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200819 chk->data = 16;
820 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200821 }
822 else
823 return 0;
824
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200825 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200826 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200827 return 1;
828}
829
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200830static int c_int2bin(struct sample *smp)
831{
Willy Tarreau83061a82018-07-13 11:56:34 +0200832 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200833
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200834 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
835 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200836
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200837 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200838 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200839 return 1;
840}
841
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200842
Emeric Brun107ca302010-01-04 16:16:05 +0100843/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200844/* Sample casts matrix: */
845/* sample_casts[from type][to type] */
846/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100847/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100848
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100849sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200850/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
851/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
852/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200853/* 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 +0200854/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
855/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200856/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200857/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
858/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
859/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200860};
Emeric Brun107ca302010-01-04 16:16:05 +0100861
Emeric Brun107ca302010-01-04 16:16:05 +0100862/*
Willy Tarreau12785782012-04-27 21:37:17 +0200863 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100864 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200865 * Returns a pointer on allocated sample expression structure.
Christopher Faulet45bb15f2021-09-30 08:48:56 +0200866 * <al> is an arg_list serving as a list head to report missing dependencies.
867 * It may be NULL if such dependencies are not allowed. Otherwise, the caller
868 * must have set al->ctx if al is set.
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100869 * If <endptr> is non-nul, it will be set to the first unparsed character
870 * (which may be the final '\0') on success. If it is nul, the expression
871 * must be properly terminated by a '\0' otherwise an error is reported.
Emeric Brun107ca302010-01-04 16:16:05 +0100872 */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100873struct 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 +0100874{
Willy Tarreau833cc792013-07-24 15:34:19 +0200875 const char *begw; /* beginning of word */
876 const char *endw; /* end of word */
877 const char *endt; /* end of term */
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +0100878 struct sample_expr *expr = NULL;
Willy Tarreau12785782012-04-27 21:37:17 +0200879 struct sample_fetch *fetch;
880 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100881 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200882 char *fkw = NULL;
883 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100884 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100885
Willy Tarreau833cc792013-07-24 15:34:19 +0200886 begw = str[*idx];
Willy Tarreaued2c6622020-02-14 18:27:10 +0100887 for (endw = begw; is_idchar(*endw); endw++)
888 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200889
890 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100891 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100892 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200893 }
Emeric Brun107ca302010-01-04 16:16:05 +0100894
Willy Tarreau833cc792013-07-24 15:34:19 +0200895 /* keep a copy of the current fetch keyword for error reporting */
896 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100897
Willy Tarreau833cc792013-07-24 15:34:19 +0200898 fetch = find_sample_fetch(begw, endw - begw);
899 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100900 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100901 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200902 }
Emeric Brun107ca302010-01-04 16:16:05 +0100903
Willy Tarreau833cc792013-07-24 15:34:19 +0200904 /* At this point, we have :
905 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100906 * - endw : end of the keyword, first character not part of keyword
Willy Tarreau833cc792013-07-24 15:34:19 +0200907 */
908
909 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100910 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100911 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200912 }
Emeric Brun107ca302010-01-04 16:16:05 +0100913 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200914
Vincent Bernat02779b62016-04-03 13:48:43 +0200915 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200916 if (!expr)
917 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100918
919 LIST_INIT(&(expr->conv_exprs));
920 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200921 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100922
Willy Tarreau689a1df2013-12-13 00:40:11 +0100923 /* Note that we call the argument parser even with an empty string,
924 * this allows it to automatically create entries for mandatory
925 * implicit arguments (eg: local proxy name).
926 */
Christopher Faulet45bb15f2021-09-30 08:48:56 +0200927 if (al) {
928 al->kw = expr->fetch->kw;
929 al->conv = NULL;
930 }
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100931 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 +0100932 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
933 goto out_error;
934 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200935
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100936 /* now endt is our first char not part of the arg list, typically the
937 * comma after the sample fetch name or after the closing parenthesis,
938 * or the NUL char.
939 */
940
Willy Tarreau689a1df2013-12-13 00:40:11 +0100941 if (!expr->arg_p) {
942 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200943 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100944 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
945 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200946 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100947 }
948
Willy Tarreau833cc792013-07-24 15:34:19 +0200949 /* Now process the converters if any. We have two supported syntaxes
950 * for the converters, which can be combined :
951 * - comma-delimited list of converters just after the keyword and args ;
952 * - one converter per keyword
953 * The combination allows to have each keyword being a comma-delimited
954 * series of converters.
955 *
956 * We want to process the former first, then the latter. For this we start
957 * from the beginning of the supposed place in the exiting conv chain, which
958 * starts at the last comma (endt).
959 */
960
961 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200962 struct sample_conv_expr *conv_expr;
Willy Tarreau46dfd782019-12-17 10:25:29 +0100963 int err_arg;
964 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +0100965
Willy Tarreau833cc792013-07-24 15:34:19 +0200966 if (*endt && *endt != ',') {
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100967 if (endptr) {
968 /* end found, let's stop here */
969 break;
970 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200971 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100972 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200973 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100974 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100975 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200976 }
Emeric Brun107ca302010-01-04 16:16:05 +0100977
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100978 /* FIXME: how long should we support such idiocies ? Maybe we
979 * should already warn ?
980 */
Willy Tarreau833cc792013-07-24 15:34:19 +0200981 while (*endt == ',') /* then trailing commas */
982 endt++;
983
Willy Tarreau97108e02016-11-25 07:33:24 +0100984 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200985
986 if (!*begw) {
987 /* none ? skip to next string */
988 (*idx)++;
989 begw = str[*idx];
990 if (!begw || !*begw)
991 break;
992 }
993
Willy Tarreaued2c6622020-02-14 18:27:10 +0100994 for (endw = begw; is_idchar(*endw); endw++)
995 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200996
997 free(ckw);
998 ckw = my_strndup(begw, endw - begw);
999
1000 conv = find_sample_conv(begw, endw - begw);
1001 if (!conv) {
1002 /* we found an isolated keyword that we don't know, it's not ours */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001003 if (begw == str[*idx]) {
1004 endt = begw;
Willy Tarreau833cc792013-07-24 15:34:19 +02001005 break;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001006 }
Willy Tarreau97108e02016-11-25 07:33:24 +01001007 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +02001008 goto out_error;
1009 }
Emeric Brun107ca302010-01-04 16:16:05 +01001010
Willy Tarreau833cc792013-07-24 15:34:19 +02001011 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001012 memprintf(err_msg, "returns type of converter '%s' is unknown", 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 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +02001017 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001018 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001019 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001020 }
Emeric Brun107ca302010-01-04 16:16:05 +01001021
1022 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +02001023 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +02001024 if (!conv_expr)
1025 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001026
Willy Tarreau2b718102021-04-21 07:32:39 +02001027 LIST_APPEND(&(expr->conv_exprs), &(conv_expr->list));
Emeric Brun107ca302010-01-04 16:16:05 +01001028 conv_expr->conv = conv;
1029
Christopher Faulet45bb15f2021-09-30 08:48:56 +02001030 if (al) {
1031 al->kw = expr->fetch->kw;
1032 al->conv = conv_expr->conv->kw;
1033 }
Willy Tarreau80b53ff2020-02-14 08:40:37 +01001034 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 +01001035 if (argcnt < 0) {
1036 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
1037 goto out_error;
1038 }
Willy Tarreau9e92d322010-01-26 17:58:06 +01001039
Willy Tarreau46dfd782019-12-17 10:25:29 +01001040 if (argcnt && !conv->arg_mask) {
1041 memprintf(err_msg, "converter '%s' does not support any args", ckw);
1042 goto out_error;
1043 }
Willy Tarreau21d68a62012-04-20 15:52:36 +02001044
Willy Tarreau46dfd782019-12-17 10:25:29 +01001045 if (!conv_expr->arg_p)
1046 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001047
Willy Tarreau46dfd782019-12-17 10:25:29 +01001048 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1049 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001050 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001051 }
1052 }
Emeric Brun485479d2010-09-23 18:02:19 +02001053
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001054 if (endptr) {
1055 /* end found, let's stop here */
1056 *endptr = (char *)endt;
1057 }
1058
Willy Tarreau833cc792013-07-24 15:34:19 +02001059 out:
1060 free(fkw);
1061 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001062 return expr;
1063
1064out_error:
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +01001065 release_sample_expr(expr);
Willy Tarreau833cc792013-07-24 15:34:19 +02001066 expr = NULL;
1067 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001068}
1069
1070/*
Willy Tarreau12785782012-04-27 21:37:17 +02001071 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001072 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001073 * Returns a pointer on a typed sample structure containing the result or NULL if
1074 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001075 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001076 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001077 *
1078 * Note: the fetch functions are required to properly set the return type. The
1079 * conversion functions must do so too. However the cast functions do not need
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001080 * to since they're made to cast multiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001081 *
1082 * The caller may indicate in <opt> if it considers the result final or not.
1083 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1084 * if the result is stable or not, according to the following table :
1085 *
1086 * return MAY_CHANGE FINAL Meaning for the sample
1087 * NULL 0 * Not present and will never be (eg: header)
1088 * NULL 1 0 Not present yet, could change (eg: POST param)
1089 * NULL 1 1 Not present yet, will not change anymore
1090 * smp 0 * Present and will not change (eg: header)
1091 * smp 1 0 Present, may change (eg: request length)
1092 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001093 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001094struct sample *sample_process(struct proxy *px, struct session *sess,
1095 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001096 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001097{
Willy Tarreau12785782012-04-27 21:37:17 +02001098 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001099
Willy Tarreau18387e22013-07-25 12:02:38 +02001100 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001101 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001102 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001103 }
Emeric Brun107ca302010-01-04 16:16:05 +01001104
Willy Tarreau1777ea62016-03-10 16:15:46 +01001105 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001106 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001107 return NULL;
1108
Emeric Brun107ca302010-01-04 16:16:05 +01001109 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001110 /* we want to ensure that p->type can be casted into
1111 * conv_expr->conv->in_type. We have 3 possibilities :
1112 * - NULL => not castable.
1113 * - c_none => nothing to do (let's optimize it)
1114 * - other => apply cast and prepare to fail
1115 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001116 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001117 return NULL;
1118
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001119 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1120 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001121 return NULL;
1122
Willy Tarreau12e50112012-04-25 17:21:49 +02001123 /* OK cast succeeded */
1124
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001125 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001126 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001127 }
1128 return p;
1129}
1130
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001131/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001132 * Resolve all remaining arguments in proxy <p>. Returns the number of
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001133 * errors or 0 if everything is fine. If at least one error is met, it will
1134 * be appended to *err. If *err==NULL it will be allocated first.
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001135 */
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001136int smp_resolve_args(struct proxy *p, char **err)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001137{
1138 struct arg_list *cur, *bak;
1139 const char *ctx, *where;
1140 const char *conv_ctx, *conv_pre, *conv_pos;
1141 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001142 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001143 struct arg *arg;
1144 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001145 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001146
1147 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1148 struct proxy *px;
1149 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001150 struct stktable *t;
1151 char *pname, *sname, *stktname;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001152 char *err2;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001153
1154 arg = cur->arg;
1155
1156 /* prepare output messages */
1157 conv_pre = conv_pos = conv_ctx = "";
1158 if (cur->conv) {
1159 conv_ctx = cur->conv;
1160 conv_pre = "conversion keyword '";
1161 conv_pos = "' for ";
1162 }
1163
1164 where = "in";
1165 ctx = "sample fetch keyword";
1166 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001167 case ARGC_STK: where = "in stick rule in"; break;
1168 case ARGC_TRK: where = "in tracking rule in"; break;
1169 case ARGC_LOG: where = "in log-format string in"; break;
1170 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
Willy Tarreaud9b331c2021-09-02 19:43:20 +02001171 case ARGC_HRQ: where = "in http-request expression in"; break;
1172 case ARGC_HRS: where = "in http-response response in"; break;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001173 case ARGC_UIF: where = "in unique-id-format string in"; break;
1174 case ARGC_RDR: where = "in redirect format string in"; break;
1175 case ARGC_CAP: where = "in capture rule in"; break;
1176 case ARGC_ACL: ctx = "ACL keyword"; break;
1177 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001178 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001179 case ARGC_HERR: where = "in http-error directive in"; break;
Miroslav Zagorac7f8314c2020-12-09 16:31:48 +01001180 case ARGC_OT: where = "in ot-scope directive in"; break;
Willy Tarreaud9b331c2021-09-02 19:43:20 +02001181 case ARGC_TCO: where = "in tcp-request connection expression in"; break;
1182 case ARGC_TSE: where = "in tcp-request session expression in"; break;
1183 case ARGC_TRQ: where = "in tcp-request content expression in"; break;
1184 case ARGC_TRS: where = "in tcp-response content expression in"; break;
1185 case ARGC_TCK: where = "in tcp-check expression in"; break;
1186 case ARGC_CFG: where = "in configuration expression in"; break;
1187 case ARGC_CLI: where = "in CLI expression in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001188 }
1189
1190 /* set a few default settings */
1191 px = p;
1192 pname = p->id;
1193
1194 switch (arg->type) {
1195 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001196 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001197 memprintf(err, "%sparsing [%s:%d]: missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1198 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001199 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001200 cfgerr++;
1201 continue;
1202 }
1203
1204 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001205 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001206
1207 if (sname) {
1208 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001209 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001210
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001211 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001212 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001213 memprintf(err, "%sparsing [%s:%d]: unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1214 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001215 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001216 cfgerr++;
1217 break;
1218 }
1219 }
1220 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001221 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001222
1223 srv = findserver(px, sname);
1224 if (!srv) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001225 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",
1226 *err ? *err : "", cur->file, cur->line, sname, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001227 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001228 cfgerr++;
1229 break;
1230 }
1231
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001232 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001233 arg->unresolved = 0;
1234 arg->data.srv = srv;
1235 break;
1236
1237 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001238 if (arg->data.str.data) {
1239 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001240 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001241 }
1242
1243 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001244 memprintf(err, "%sparsing [%s:%d]: unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1245 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001246 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001247 cfgerr++;
1248 break;
1249 }
1250
1251 if (!(px->cap & PR_CAP_FE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001252 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",
1253 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001254 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001255 cfgerr++;
1256 break;
1257 }
1258
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001259 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001260 arg->unresolved = 0;
1261 arg->data.prx = px;
1262 break;
1263
1264 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001265 if (arg->data.str.data) {
1266 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001267 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001268 }
1269
1270 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001271 memprintf(err, "%sparsing [%s:%d]: unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1272 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001273 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001274 cfgerr++;
1275 break;
1276 }
1277
1278 if (!(px->cap & PR_CAP_BE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001279 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",
1280 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001281 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001282 cfgerr++;
1283 break;
1284 }
1285
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001286 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001287 arg->unresolved = 0;
1288 arg->data.prx = px;
1289 break;
1290
1291 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001292 if (arg->data.str.data)
1293 stktname = arg->data.str.area;
1294 else
1295 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001296
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001297 t = stktable_find_by_name(stktname);
1298 if (!t) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001299 memprintf(err, "%sparsing [%s:%d]: unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1300 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001301 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001302 cfgerr++;
1303 break;
1304 }
1305
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001306 if (!t->size) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001307 memprintf(err, "%sparsing [%s:%d]: no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1308 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001309 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001310 cfgerr++;
1311 break;
1312 }
1313
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001314 if (t->proxy && (p->bind_proc & ~t->proxy->bind_proc)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001315 memprintf(err, "%sparsing [%s:%d]: stick-table '%s' not present on all processes covered by proxy '%s'.\n",
1316 *err ? *err : "", cur->file, cur->line, t->proxy->id, p->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01001317 cfgerr++;
1318 break;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001319 }
1320
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001321 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001322 p->next_stkt_ref = t->proxies_list;
1323 t->proxies_list = p;
1324 }
1325
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001326 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001327 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001328 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001329 break;
1330
1331 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001332 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001333 memprintf(err, "%sparsing [%s:%d]: missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1334 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001335 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001336 cfgerr++;
1337 break;
1338 }
1339
1340 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001341 strcmp(p->uri_auth->userlist->name, arg->data.str.area) == 0)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001342 ul = p->uri_auth->userlist;
1343 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001344 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001345
1346 if (!ul) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001347 memprintf(err, "%sparsing [%s:%d]: unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1348 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001349 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001350 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001351 cfgerr++;
1352 break;
1353 }
1354
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001355 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001356 arg->unresolved = 0;
1357 arg->data.usr = ul;
1358 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001359
1360 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001361 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001362 memprintf(err, "%sparsing [%s:%d]: missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1363 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001364 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001365 cfgerr++;
1366 continue;
1367 }
1368
Willy Tarreau46947782015-01-19 19:00:58 +01001369 rflags = 0;
1370 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001371 err2 = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001372
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001373 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err2))) {
1374 memprintf(err, "%sparsing [%s:%d]: error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1375 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001376 arg->data.str.area,
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001377 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err2);
Willy Tarreau46947782015-01-19 19:00:58 +01001378 cfgerr++;
1379 continue;
1380 }
1381
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001382 chunk_destroy(&arg->data.str);
Willy Tarreau46947782015-01-19 19:00:58 +01001383 arg->unresolved = 0;
1384 arg->data.reg = reg;
1385 break;
1386
1387
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001388 }
1389
Willy Tarreau2b718102021-04-21 07:32:39 +02001390 LIST_DELETE(&cur->list);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001391 free(cur);
1392 } /* end of args processing */
1393
1394 return cfgerr;
1395}
1396
1397/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001398 * Process a fetch + format conversion as defined by the sample expression
1399 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001400 * not explicitly set to <smp_type>, but shall be compatible with it as
1401 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1402 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001403 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1404 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1405 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1406 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001407 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1408 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1409 * still hope to get it after waiting longer, and is not converted to string.
1410 * The possible output combinations are the following :
1411 *
1412 * return MAY_CHANGE FINAL Meaning for the sample
1413 * NULL * * Not present and will never be (eg: header)
1414 * smp 0 * Final value converted (eg: header)
1415 * smp 1 0 Not present yet, may appear later (eg: header)
1416 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001417 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001418struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001419 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001420 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001421{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001422 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001423
Willy Tarreau6c616e02014-06-25 16:56:41 +02001424 memset(smp, 0, sizeof(*smp));
1425
Willy Tarreau192252e2015-04-04 01:47:55 +02001426 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001427 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1428 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001429 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001430 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001431
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001432 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001433 return NULL;
1434
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001435 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001436 return NULL;
1437
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001438 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001439 return smp;
1440}
1441
Christopher Faulet476e5d02016-10-26 11:34:47 +02001442static void release_sample_arg(struct arg *p)
1443{
1444 struct arg *p_back = p;
1445
1446 if (!p)
1447 return;
1448
1449 while (p->type != ARGT_STOP) {
1450 if (p->type == ARGT_STR || p->unresolved) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001451 chunk_destroy(&p->data.str);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001452 p->unresolved = 0;
1453 }
1454 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001455 regex_free(p->data.reg);
1456 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001457 }
1458 p++;
1459 }
1460
1461 if (p_back != empty_arg_list)
1462 free(p_back);
1463}
1464
1465void release_sample_expr(struct sample_expr *expr)
1466{
1467 struct sample_conv_expr *conv_expr, *conv_exprb;
1468
1469 if (!expr)
1470 return;
1471
Tim Duesterhus867cd982020-07-04 11:49:39 +02001472 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001473 LIST_DELETE(&conv_expr->list);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001474 release_sample_arg(conv_expr->arg_p);
Tim Duesterhus867cd982020-07-04 11:49:39 +02001475 free(conv_expr);
1476 }
1477
Christopher Faulet476e5d02016-10-26 11:34:47 +02001478 release_sample_arg(expr->arg_p);
1479 free(expr);
1480}
1481
Emeric Brun107ca302010-01-04 16:16:05 +01001482/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001483/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001484/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001485/*****************************************************************/
1486
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001487static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1488{
1489 int i;
1490 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001491 struct buffer *buf;
1492 struct sink *sink;
1493 struct ist line;
1494 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001495
Willy Tarreau0851fd52019-12-17 10:07:25 +01001496 buf = alloc_trash_chunk();
1497 if (!buf)
1498 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001499
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001500 sink = (struct sink *)arg_p[1].data.ptr;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001501 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001502
Willy Tarreau0851fd52019-12-17 10:07:25 +01001503 pfx = arg_p[0].data.str.area;
1504 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001505
Willy Tarreau0851fd52019-12-17 10:07:25 +01001506 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1507 if (!sample_casts[smp->data.type][SMP_T_STR])
1508 goto nocast;
1509
1510 /* Copy sample fetch. This puts the sample as const, the
1511 * cast will copy data if a transformation is required.
1512 */
1513 memcpy(&tmp, smp, sizeof(struct sample));
1514 tmp.flags = SMP_F_CONST;
1515
1516 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1517 goto nocast;
1518
1519 /* Display the displayable chars*. */
1520 b_putchr(buf, '<');
1521 for (i = 0; i < tmp.data.u.str.data; i++) {
Willy Tarreau90807112020-02-25 08:16:33 +01001522 if (isprint((unsigned char)tmp.data.u.str.area[i]))
Willy Tarreau0851fd52019-12-17 10:07:25 +01001523 b_putchr(buf, tmp.data.u.str.area[i]);
1524 else
1525 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001526 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001527 b_putchr(buf, '>');
1528
1529 done:
1530 line = ist2(buf->area, buf->data);
Aurelien DARRAGONc2423132023-06-26 16:44:41 +02001531 sink_write(sink, 0, &line, 1, 0, 0, NULL);
Willy Tarreau0851fd52019-12-17 10:07:25 +01001532 end:
1533 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001534 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001535 nocast:
1536 chunk_appendf(buf, "(undisplayable)");
1537 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001538}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001539
1540// This function checks the "debug" converter's arguments.
1541static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1542 const char *file, int line, char **err)
1543{
1544 const char *name = "buf0";
1545 struct sink *sink = NULL;
1546
1547 if (args[0].type != ARGT_STR) {
1548 /* optional prefix */
1549 args[0].data.str.area = "";
1550 args[0].data.str.data = 0;
1551 }
1552
1553 if (args[1].type == ARGT_STR)
1554 name = args[1].data.str.area;
1555
1556 sink = sink_find(name);
1557 if (!sink) {
1558 memprintf(err, "No such sink '%s'", name);
1559 return 0;
1560 }
1561
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001562 chunk_destroy(&args[1].data.str);
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001563 args[1].type = ARGT_PTR;
1564 args[1].data.ptr = sink;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001565 return 1;
1566}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001567
Holger Just1bfc24b2017-05-06 00:56:53 +02001568static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1569{
Willy Tarreau83061a82018-07-13 11:56:34 +02001570 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001571 int bin_len;
1572
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001573 trash->data = 0;
1574 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1575 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001576 if (bin_len < 0)
1577 return 0;
1578
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001579 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001580 smp->data.u.str = *trash;
1581 smp->data.type = SMP_T_BIN;
1582 smp->flags &= ~SMP_F_CONST;
1583 return 1;
1584}
1585
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +02001586static int sample_conv_base64url2bin(const struct arg *arg_p, struct sample *smp, void *private)
1587{
1588 struct buffer *trash = get_trash_chunk();
1589 int bin_len;
1590
1591 trash->data = 0;
1592 bin_len = base64urldec(smp->data.u.str.area, smp->data.u.str.data,
1593 trash->area, trash->size);
1594 if (bin_len < 0)
1595 return 0;
1596
1597 trash->data = bin_len;
1598 smp->data.u.str = *trash;
1599 smp->data.type = SMP_T_BIN;
1600 smp->flags &= ~SMP_F_CONST;
1601 return 1;
1602}
1603
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001604static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001605{
Willy Tarreau83061a82018-07-13 11:56:34 +02001606 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001607 int b64_len;
1608
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001609 trash->data = 0;
1610 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1611 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001612 if (b64_len < 0)
1613 return 0;
1614
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001615 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001616 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001617 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001618 smp->flags &= ~SMP_F_CONST;
1619 return 1;
1620}
1621
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +02001622static int sample_conv_bin2base64url(const struct arg *arg_p, struct sample *smp, void *private)
1623{
1624 struct buffer *trash = get_trash_chunk();
1625 int b64_len;
1626
1627 trash->data = 0;
1628 b64_len = a2base64url(smp->data.u.str.area, smp->data.u.str.data,
1629 trash->area, trash->size);
1630 if (b64_len < 0)
1631 return 0;
1632
1633 trash->data = b64_len;
1634 smp->data.u.str = *trash;
1635 smp->data.type = SMP_T_STR;
1636 smp->flags &= ~SMP_F_CONST;
1637 return 1;
1638}
1639
Willy Tarreauea573502021-10-06 15:29:00 +02001640/* This function returns a sample struct filled with the conversion of variable
1641 * <var> to sample type <type> (SMP_T_*), via a cast to the target type. If the
1642 * variable cannot be retrieved or casted, 0 is returned, otherwise 1.
1643 *
1644 * Keep in mind that the sample content may be written to a pre-allocated
1645 * trash chunk as returned by get_trash_chunk().
1646 */
1647int sample_conv_var2smp(const struct var_desc *var, struct sample *smp, int type)
1648{
1649 if (!vars_get_by_desc(var, smp))
1650 return 0;
1651 if (!sample_casts[smp->data.type][type])
1652 return 0;
1653 if (!sample_casts[smp->data.type][type](smp))
1654 return 0;
1655 return 1;
1656}
1657
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001658static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1659{
1660 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001661 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001662
1663 memset(&ctx, 0, sizeof(ctx));
1664
1665 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001666 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1667 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001668
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001669 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001670 smp->data.u.str = *trash;
1671 smp->data.type = SMP_T_BIN;
1672 smp->flags &= ~SMP_F_CONST;
1673 return 1;
1674}
1675
Tim Duesterhusd4376302019-06-17 12:41:44 +02001676#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01001677static int smp_check_sha2(struct arg *args, struct sample_conv *conv,
1678 const char *file, int line, char **err)
1679{
1680 if (args[0].type == ARGT_STOP)
1681 return 1;
1682 if (args[0].type != ARGT_SINT) {
1683 memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]);
1684 return 0;
1685 }
1686
1687 switch (args[0].data.sint) {
1688 case 224:
1689 case 256:
1690 case 384:
1691 case 512:
1692 /* this is okay */
1693 return 1;
1694 default:
1695 memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint);
1696 return 0;
1697 }
1698}
1699
Tim Duesterhusd4376302019-06-17 12:41:44 +02001700static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
1701{
1702 struct buffer *trash = get_trash_chunk();
1703 int bits = 256;
Christopher Faulete6e7a582021-01-29 11:29:28 +01001704 if (arg_p->data.sint)
Tim Duesterhusd4376302019-06-17 12:41:44 +02001705 bits = arg_p->data.sint;
1706
1707 switch (bits) {
1708 case 224: {
1709 SHA256_CTX ctx;
1710
1711 memset(&ctx, 0, sizeof(ctx));
1712
1713 SHA224_Init(&ctx);
1714 SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1715 SHA224_Final((unsigned char *) trash->area, &ctx);
1716 trash->data = SHA224_DIGEST_LENGTH;
1717 break;
1718 }
1719 case 256: {
1720 SHA256_CTX ctx;
1721
1722 memset(&ctx, 0, sizeof(ctx));
1723
1724 SHA256_Init(&ctx);
1725 SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1726 SHA256_Final((unsigned char *) trash->area, &ctx);
1727 trash->data = SHA256_DIGEST_LENGTH;
1728 break;
1729 }
1730 case 384: {
1731 SHA512_CTX ctx;
1732
1733 memset(&ctx, 0, sizeof(ctx));
1734
1735 SHA384_Init(&ctx);
1736 SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1737 SHA384_Final((unsigned char *) trash->area, &ctx);
1738 trash->data = SHA384_DIGEST_LENGTH;
1739 break;
1740 }
1741 case 512: {
1742 SHA512_CTX ctx;
1743
1744 memset(&ctx, 0, sizeof(ctx));
1745
1746 SHA512_Init(&ctx);
1747 SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1748 SHA512_Final((unsigned char *) trash->area, &ctx);
1749 trash->data = SHA512_DIGEST_LENGTH;
1750 break;
1751 }
1752 default:
1753 return 0;
1754 }
1755
1756 smp->data.u.str = *trash;
1757 smp->data.type = SMP_T_BIN;
1758 smp->flags &= ~SMP_F_CONST;
1759 return 1;
1760}
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001761
Dragan Dosen9e8db132021-02-22 10:03:53 +01001762/* This function returns a sample struct filled with an <arg> content.
1763 * If the <arg> contains a string, it is returned in the sample flagged as
1764 * SMP_F_CONST. If the <arg> contains a variable descriptor, the sample is
1765 * filled with the content of the variable by using vars_get_by_desc().
1766 *
1767 * Keep in mind that the sample content may be written to a pre-allocated
1768 * trash chunk as returned by get_trash_chunk().
1769 *
1770 * This function returns 0 if an error occurs, otherwise it returns 1.
1771 */
Willy Tarreau05cd2e62021-10-06 15:20:18 +02001772int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001773{
1774 switch (arg->type) {
1775 case ARGT_STR:
1776 smp->data.type = SMP_T_STR;
1777 smp->data.u.str = arg->data.str;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001778 smp->flags = SMP_F_CONST;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001779 return 1;
1780 case ARGT_VAR:
Willy Tarreau8fdaf252021-10-06 15:30:52 +02001781 return sample_conv_var2smp(&arg->data.var, smp, SMP_T_STR);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001782 default:
1783 return 0;
1784 }
1785}
1786
Dragan Dosen9e8db132021-02-22 10:03:53 +01001787/* This function checks an <arg> and fills it with a variable type if the
1788 * <arg> string contains a valid variable name. If failed, the function
1789 * tries to perform a base64 decode operation on the same string, and
1790 * fills the <arg> with the decoded content.
1791 *
1792 * Validation is skipped if the <arg> string is empty.
1793 *
1794 * This function returns 0 if the variable lookup fails and the specified
1795 * <arg> string is not a valid base64 encoded string, as well if
1796 * unexpected argument type is specified or memory allocation error
1797 * occurs. Otherwise it returns 1.
1798 */
1799static inline int sample_check_arg_base64(struct arg *arg, char **err)
1800{
1801 char *dec = NULL;
1802 int dec_size;
1803
1804 if (arg->type != ARGT_STR) {
1805 memprintf(err, "unexpected argument type");
1806 return 0;
1807 }
1808
1809 if (arg->data.str.data == 0) /* empty */
1810 return 1;
1811
1812 if (vars_check_arg(arg, NULL))
1813 return 1;
1814
1815 if (arg->data.str.data % 4) {
1816 memprintf(err, "argument needs to be base64 encoded, and "
1817 "can either be a string or a variable");
1818 return 0;
1819 }
1820
1821 dec_size = (arg->data.str.data / 4 * 3)
1822 - (arg->data.str.area[arg->data.str.data-1] == '=' ? 1 : 0)
1823 - (arg->data.str.area[arg->data.str.data-2] == '=' ? 1 : 0);
1824
1825 if ((dec = malloc(dec_size)) == NULL) {
1826 memprintf(err, "memory allocation error");
1827 return 0;
1828 }
1829
1830 dec_size = base64dec(arg->data.str.area, arg->data.str.data, dec, dec_size);
1831 if (dec_size < 0) {
1832 memprintf(err, "argument needs to be base64 encoded, and "
1833 "can either be a string or a variable");
1834 free(dec);
1835 return 0;
1836 }
1837
1838 /* base64 decoded */
1839 chunk_destroy(&arg->data.str);
1840 arg->data.str.area = dec;
1841 arg->data.str.data = dec_size;
1842 return 1;
1843}
1844
Ilya Shipitsin2c481d02021-03-26 23:35:31 +05001845#ifdef EVP_CIPH_GCM_MODE
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001846static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
1847 const char *file, int line, char **err)
1848{
1849 switch(args[0].data.sint) {
1850 case 128:
1851 case 192:
1852 case 256:
1853 break;
1854 default:
1855 memprintf(err, "key size must be 128, 192 or 256 (bits).");
1856 return 0;
1857 }
Dragan Dosen9e8db132021-02-22 10:03:53 +01001858
1859 /* Try to decode variables. */
1860 if (!sample_check_arg_base64(&args[1], err)) {
1861 memprintf(err, "failed to parse nonce : %s", *err);
1862 return 0;
1863 }
1864 if (!sample_check_arg_base64(&args[2], err)) {
1865 memprintf(err, "failed to parse key : %s", *err);
1866 return 0;
1867 }
1868 if (!sample_check_arg_base64(&args[3], err)) {
1869 memprintf(err, "failed to parse aead_tag : %s", *err);
1870 return 0;
1871 }
1872
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001873 return 1;
1874}
1875
1876/* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
1877static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
1878{
1879 struct sample nonce, key, aead_tag;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001880 struct buffer *smp_trash = NULL, *smp_trash_alloc = NULL;
Dragan Dosen9b98c9d2024-03-11 18:10:01 +01001881 EVP_CIPHER_CTX *ctx = NULL;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001882 int dec_size, ret;
1883
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001884 smp_trash_alloc = alloc_trash_chunk();
1885 if (!smp_trash_alloc)
1886 return 0;
1887
Dragan Dosen9e8db132021-02-22 10:03:53 +01001888 /* smp copy */
1889 smp_trash_alloc->data = smp->data.u.str.data;
1890 if (unlikely(smp_trash_alloc->data > smp_trash_alloc->size))
1891 smp_trash_alloc->data = smp_trash_alloc->size;
1892 memcpy(smp_trash_alloc->area, smp->data.u.str.area, smp_trash_alloc->data);
1893
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001894 ctx = EVP_CIPHER_CTX_new();
1895
1896 if (!ctx)
1897 goto err;
1898
Dragan Dosen9e8db132021-02-22 10:03:53 +01001899 smp_trash = alloc_trash_chunk();
1900 if (!smp_trash)
1901 goto err;
1902
1903 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
1904 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001905 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001906
1907 if (arg_p[1].type == ARGT_VAR) {
1908 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
1909 if (dec_size < 0)
1910 goto err;
1911 smp_trash->data = dec_size;
1912 nonce.data.u.str = *smp_trash;
1913 }
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001914
1915 /* Set cipher type and mode */
1916 switch(arg_p[0].data.sint) {
1917 case 128:
1918 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
1919 break;
1920 case 192:
1921 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
1922 break;
1923 case 256:
1924 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
1925 break;
1926 }
1927
Dragan Dosen9e8db132021-02-22 10:03:53 +01001928 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, nonce.data.u.str.data, NULL);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001929
1930 /* Initialise IV */
Dragan Dosen9e8db132021-02-22 10:03:53 +01001931 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) nonce.data.u.str.area))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001932 goto err;
1933
Dragan Dosen9e8db132021-02-22 10:03:53 +01001934 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
1935 if (!sample_conv_var2smp_str(&arg_p[2], &key))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001936 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001937
1938 if (arg_p[2].type == ARGT_VAR) {
1939 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
1940 if (dec_size < 0)
1941 goto err;
1942 smp_trash->data = dec_size;
1943 key.data.u.str = *smp_trash;
1944 }
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001945
1946 /* Initialise key */
Dragan Dosen9e8db132021-02-22 10:03:53 +01001947 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) key.data.u.str.area, NULL))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001948 goto err;
1949
1950 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
Dragan Dosen9e8db132021-02-22 10:03:53 +01001951 (unsigned char *) smp_trash_alloc->area, (int) smp_trash_alloc->data))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001952 goto err;
1953
Dragan Dosen9e8db132021-02-22 10:03:53 +01001954 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
1955 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001956 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001957
1958 if (arg_p[3].type == ARGT_VAR) {
1959 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
1960 if (dec_size < 0)
1961 goto err;
1962 smp_trash_alloc->data = dec_size;
1963 aead_tag.data.u.str = *smp_trash_alloc;
1964 }
1965
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001966 dec_size = smp_trash->data;
1967
Dragan Dosen9e8db132021-02-22 10:03:53 +01001968 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 +02001969 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
1970
1971 if (ret <= 0)
1972 goto err;
1973
1974 smp->data.u.str.data = dec_size + smp_trash->data;
1975 smp->data.u.str.area = smp_trash->area;
1976 smp->data.type = SMP_T_BIN;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001977 smp_dup(smp);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001978 free_trash_chunk(smp_trash_alloc);
Dragan Dosen9e8db132021-02-22 10:03:53 +01001979 free_trash_chunk(smp_trash);
Dragan Dosen9b98c9d2024-03-11 18:10:01 +01001980 EVP_CIPHER_CTX_free(ctx);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001981 return 1;
1982
1983err:
1984 free_trash_chunk(smp_trash_alloc);
Dragan Dosen9e8db132021-02-22 10:03:53 +01001985 free_trash_chunk(smp_trash);
Dragan Dosen9b98c9d2024-03-11 18:10:01 +01001986 EVP_CIPHER_CTX_free(ctx);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001987 return 0;
1988}
Ilya Shipitsin2c481d02021-03-26 23:35:31 +05001989#endif
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001990
Patrick Gansterer8e366512020-04-22 16:47:57 +02001991static int check_crypto_digest(struct arg *args, struct sample_conv *conv,
1992 const char *file, int line, char **err)
1993{
1994 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1995
1996 if (evp)
1997 return 1;
1998
1999 memprintf(err, "algorithm must be a valid OpenSSL message digest name.");
2000 return 0;
2001}
2002
2003static int sample_conv_crypto_digest(const struct arg *args, struct sample *smp, void *private)
2004{
2005 struct buffer *trash = get_trash_chunk();
2006 unsigned char *md = (unsigned char*) trash->area;
2007 unsigned int md_len = trash->size;
2008 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
2009 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
2010
2011 if (!ctx)
2012 return 0;
2013
2014 if (!EVP_DigestInit_ex(ctx, evp, NULL) ||
2015 !EVP_DigestUpdate(ctx, smp->data.u.str.area, smp->data.u.str.data) ||
2016 !EVP_DigestFinal_ex(ctx, md, &md_len)) {
2017 EVP_MD_CTX_free(ctx);
2018 return 0;
2019 }
2020
2021 EVP_MD_CTX_free(ctx);
2022
2023 trash->data = md_len;
2024 smp->data.u.str = *trash;
2025 smp->data.type = SMP_T_BIN;
2026 smp->flags &= ~SMP_F_CONST;
2027 return 1;
2028}
2029
2030static int check_crypto_hmac(struct arg *args, struct sample_conv *conv,
2031 const char *file, int line, char **err)
2032{
2033 if (!check_crypto_digest(args, conv, file, line, err))
2034 return 0;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002035
2036 if (!sample_check_arg_base64(&args[1], err)) {
2037 memprintf(err, "failed to parse key : %s", *err);
2038 return 0;
2039 }
Patrick Gansterer8e366512020-04-22 16:47:57 +02002040
Patrick Gansterer8e366512020-04-22 16:47:57 +02002041 return 1;
2042}
2043
2044static int sample_conv_crypto_hmac(const struct arg *args, struct sample *smp, void *private)
2045{
2046 struct sample key;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002047 struct buffer *trash = NULL, *key_trash = NULL;
Patrick Gansterer8e366512020-04-22 16:47:57 +02002048 unsigned char *md;
2049 unsigned int md_len;
2050 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
2051 int dec_size;
2052
2053 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
2054 if (!sample_conv_var2smp_str(&args[1], &key))
2055 return 0;
2056
Dragan Dosen9e8db132021-02-22 10:03:53 +01002057 if (args[1].type == ARGT_VAR) {
2058 key_trash = alloc_trash_chunk();
2059 if (!key_trash)
2060 goto err;
2061
2062 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, key_trash->area, key_trash->size);
2063 if (dec_size < 0)
2064 goto err;
2065 key_trash->data = dec_size;
2066 key.data.u.str = *key_trash;
2067 }
Patrick Gansterer8e366512020-04-22 16:47:57 +02002068
Dragan Dosen9e8db132021-02-22 10:03:53 +01002069 trash = alloc_trash_chunk();
2070 if (!trash)
Patrick Gansterer8e366512020-04-22 16:47:57 +02002071 goto err;
2072
2073 md = (unsigned char*) trash->area;
2074 md_len = trash->size;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002075 if (!HMAC(evp, key.data.u.str.area, key.data.u.str.data, (const unsigned char*) smp->data.u.str.area,
2076 smp->data.u.str.data, md, &md_len))
Patrick Gansterer8e366512020-04-22 16:47:57 +02002077 goto err;
2078
2079 free_trash_chunk(key_trash);
2080
2081 trash->data = md_len;
2082 smp->data.u.str = *trash;
2083 smp->data.type = SMP_T_BIN;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002084 smp_dup(smp);
2085 free_trash_chunk(trash);
Patrick Gansterer8e366512020-04-22 16:47:57 +02002086 return 1;
2087
2088err:
2089 free_trash_chunk(key_trash);
Dragan Dosen9e8db132021-02-22 10:03:53 +01002090 free_trash_chunk(trash);
Patrick Gansterer8e366512020-04-22 16:47:57 +02002091 return 0;
2092}
2093
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02002094#endif /* USE_OPENSSL */
Tim Duesterhusd4376302019-06-17 12:41:44 +02002095
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002096static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002097{
Willy Tarreau83061a82018-07-13 11:56:34 +02002098 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002099 unsigned char c;
2100 int ptr = 0;
2101
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002102 trash->data = 0;
2103 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
2104 c = smp->data.u.str.area[ptr++];
2105 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
2106 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002107 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002108 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002109 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002110 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002111 return 1;
2112}
2113
Dragan Dosen3f957b22017-10-24 09:27:34 +02002114static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
2115{
2116 long long int n = 0;
2117 int i, c;
2118
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002119 for (i = 0; i < smp->data.u.str.data; i++) {
2120 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02002121 return 0;
2122 n = (n << 4) + c;
2123 }
2124
2125 smp->data.u.sint = n;
2126 smp->data.type = SMP_T_SINT;
2127 smp->flags &= ~SMP_F_CONST;
2128 return 1;
2129}
2130
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002131/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002132static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002133{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002134 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
2135 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002136 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002137 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002138 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002139 return 1;
2140}
2141
Willy Tarreau60a2ee72017-12-15 07:13:48 +01002142static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002143{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002144 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002145 smp->data.u.sint = i;
2146 smp->data.type = SMP_T_SINT;
2147 return 1;
2148}
2149
2150
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002151static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002152{
2153 int i;
2154
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002155 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002156 return 0;
2157
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002158 for (i = 0; i < smp->data.u.str.data; i++) {
2159 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
2160 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01002161 }
2162 return 1;
2163}
2164
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002165static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002166{
2167 int i;
2168
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002169 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02002170 return 0;
2171
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002172 for (i = 0; i < smp->data.u.str.data; i++) {
2173 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
2174 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01002175 }
2176 return 1;
2177}
2178
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002179/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
2180static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002181{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002182 /* Attempt to convert to IPv4 to apply the correct mask. */
2183 c_ipv62ip(smp);
2184
2185 if (smp->data.type == SMP_T_IPV4) {
2186 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
2187 smp->data.type = SMP_T_IPV4;
2188 }
2189 else if (smp->data.type == SMP_T_IPV6) {
2190 /* IPv6 cannot be converted without an IPv6 mask. */
2191 if (args[1].type != ARGT_IPV6)
2192 return 0;
2193
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +01002194 write_u64(&smp->data.u.ipv6.s6_addr[0],
2195 read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0]));
2196 write_u64(&smp->data.u.ipv6.s6_addr[8],
2197 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 +01002198 smp->data.type = SMP_T_IPV6;
2199 }
2200
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002201 return 1;
2202}
2203
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002204/* takes an UINT value on input supposed to represent the time since EPOCH,
2205 * adds an optional offset found in args[1] and emits a string representing
2206 * the local time in the format specified in args[1] using strftime().
2207 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002208static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002209{
Willy Tarreau83061a82018-07-13 11:56:34 +02002210 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002211 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002212 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Tim Duesterhus9da4b1f2021-08-28 23:57:01 +02002213 struct tm tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002214
2215 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002216 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002217 curr_date += args[1].data.sint;
2218
Tim Duesterhus9da4b1f2021-08-28 23:57:01 +02002219 get_localtime(curr_date, &tm);
2220
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002221 temp = get_trash_chunk();
Tim Duesterhus9da4b1f2021-08-28 23:57:01 +02002222 temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002223 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002224 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002225 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_sdbm(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_sdbm(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 Tarreau0dbfdba2014-07-10 16:37:47 +02002239/* takes an UINT value on input supposed to represent the time since EPOCH,
2240 * adds an optional offset found in args[1] and emits a string representing
2241 * the UTC date in the format specified in args[1] using strftime().
2242 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002243static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002244{
Willy Tarreau83061a82018-07-13 11:56:34 +02002245 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002246 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002247 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Tim Duesterhus9da4b1f2021-08-28 23:57:01 +02002248 struct tm tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002249
2250 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002251 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002252 curr_date += args[1].data.sint;
2253
Tim Duesterhus9da4b1f2021-08-28 23:57:01 +02002254 get_gmtime(curr_date, &tm);
2255
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002256 temp = get_trash_chunk();
Tim Duesterhus9da4b1f2021-08-28 23:57:01 +02002257 temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002258 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002259 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002260 return 1;
2261}
2262
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002263/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002264static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002265{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002266 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
2267 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002268 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002269 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002270 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002271 return 1;
2272}
2273
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002274/* hashes the binary input into a 32-bit unsigned int using xxh.
2275 * The seed of the hash defaults to 0 but can be changd in argument 1.
2276 */
2277static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
2278{
2279 unsigned int seed;
2280
Christopher Faulete6e7a582021-01-29 11:29:28 +01002281 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002282 seed = arg_p->data.sint;
2283 else
2284 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002285 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
2286 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002287 smp->data.type = SMP_T_SINT;
2288 return 1;
2289}
2290
2291/* hashes the binary input into a 64-bit unsigned int using xxh.
2292 * In fact, the function returns a 64 bit unsigned, but the sample
2293 * storage of haproxy only proposes 64-bits signed, so the value is
2294 * cast as signed. This cast doesn't impact the hash repartition.
2295 * The seed of the hash defaults to 0 but can be changd in argument 1.
2296 */
2297static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
2298{
2299 unsigned long long int seed;
2300
Christopher Faulete6e7a582021-01-29 11:29:28 +01002301 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002302 seed = (unsigned long long int)arg_p->data.sint;
2303 else
2304 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002305 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
2306 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002307 smp->data.type = SMP_T_SINT;
2308 return 1;
2309}
2310
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002311static int sample_conv_xxh3(const struct arg *arg_p, struct sample *smp, void *private)
2312{
2313 unsigned long long int seed;
2314
Christopher Faulete6e7a582021-01-29 11:29:28 +01002315 if (arg_p->data.sint)
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002316 seed = (unsigned long long int)arg_p->data.sint;
2317 else
2318 seed = 0;
2319 smp->data.u.sint = (long long int)XXH3(smp->data.u.str.area,
2320 smp->data.u.str.data, seed);
2321 smp->data.type = SMP_T_SINT;
2322 return 1;
2323}
2324
Willy Tarreau80599772015-01-20 19:35:24 +01002325/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002326static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01002327{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002328 smp->data.u.sint = hash_crc32(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 Tarreau80599772015-01-20 19:35:24 +01002333 return 1;
2334}
2335
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002336/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
2337static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
2338{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002339 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
2340 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002341 if (arg_p->data.sint)
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002342 smp->data.u.sint = full_hash(smp->data.u.sint);
2343 smp->data.type = SMP_T_SINT;
2344 return 1;
2345}
2346
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002347/* This function escape special json characters. The returned string can be
2348 * safely set between two '"' and used as json string. The json string is
2349 * defined like this:
2350 *
2351 * any Unicode character except '"' or '\' or control character
2352 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
2353 *
2354 * The enum input_type contain all the allowed mode for decoding the input
2355 * string.
2356 */
2357enum input_type {
2358 IT_ASCII = 0,
2359 IT_UTF8,
2360 IT_UTF8S,
2361 IT_UTF8P,
2362 IT_UTF8PS,
2363};
William Dauchy5417e892021-01-08 21:57:41 +01002364
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002365static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
2366 const char *file, int line, char **err)
2367{
Christopher Faulet95917132020-08-05 23:07:07 +02002368 enum input_type type;
2369
Christopher Faulet95917132020-08-05 23:07:07 +02002370 if (strcmp(arg->data.str.area, "") == 0)
2371 type = IT_ASCII;
2372 else if (strcmp(arg->data.str.area, "ascii") == 0)
2373 type = IT_ASCII;
2374 else if (strcmp(arg->data.str.area, "utf8") == 0)
2375 type = IT_UTF8;
2376 else if (strcmp(arg->data.str.area, "utf8s") == 0)
2377 type = IT_UTF8S;
2378 else if (strcmp(arg->data.str.area, "utf8p") == 0)
2379 type = IT_UTF8P;
2380 else if (strcmp(arg->data.str.area, "utf8ps") == 0)
2381 type = IT_UTF8PS;
2382 else {
2383 memprintf(err, "Unexpected input code type. "
2384 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
2385 return 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002386 }
2387
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002388 chunk_destroy(&arg->data.str);
Christopher Faulet95917132020-08-05 23:07:07 +02002389 arg->type = ARGT_SINT;
2390 arg->data.sint = type;
2391 return 1;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002392}
2393
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002394static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002395{
Willy Tarreau83061a82018-07-13 11:56:34 +02002396 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002397 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
2398 const char *str;
2399 int len;
2400 enum input_type input_type = IT_ASCII;
2401 unsigned int c;
2402 unsigned int ret;
2403 char *p;
2404
Christopher Faulete6e7a582021-01-29 11:29:28 +01002405 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002406
2407 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002408 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002409
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002410 p = smp->data.u.str.area;
2411 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002412
2413 if (input_type == IT_ASCII) {
2414 /* Read input as ASCII. */
2415 c = *(unsigned char *)p;
2416 p++;
2417 }
2418 else {
2419 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002420 ret = utf8_next(p,
2421 smp->data.u.str.data - ( p - smp->data.u.str.area),
2422 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002423 p += utf8_return_length(ret);
2424
2425 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2426 return 0;
2427 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2428 continue;
2429 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2430 return 0;
2431 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2432 continue;
2433
2434 /* Check too big values. */
2435 if ((unsigned int)c > 0xffff) {
2436 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2437 return 0;
2438 continue;
2439 }
2440 }
2441
2442 /* Convert character. */
2443 if (c == '"') {
2444 len = 2;
2445 str = "\\\"";
2446 }
2447 else if (c == '\\') {
2448 len = 2;
2449 str = "\\\\";
2450 }
2451 else if (c == '/') {
2452 len = 2;
2453 str = "\\/";
2454 }
2455 else if (c == '\b') {
2456 len = 2;
2457 str = "\\b";
2458 }
2459 else if (c == '\f') {
2460 len = 2;
2461 str = "\\f";
2462 }
2463 else if (c == '\r') {
2464 len = 2;
2465 str = "\\r";
2466 }
2467 else if (c == '\n') {
2468 len = 2;
2469 str = "\\n";
2470 }
2471 else if (c == '\t') {
2472 len = 2;
2473 str = "\\t";
2474 }
Willy Tarreau90807112020-02-25 08:16:33 +01002475 else if (c > 0xff || !isprint((unsigned char)c)) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002476 /* isprint generate a segfault if c is too big. The man says that
2477 * c must have the value of an unsigned char or EOF.
2478 */
2479 len = 6;
2480 _str[0] = '\\';
2481 _str[1] = 'u';
2482 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2483 str = _str;
2484 }
2485 else {
2486 len = 1;
Willy Tarreau5715da22020-02-25 08:37:37 +01002487 _str[0] = c;
2488 str = _str;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002489 }
2490
2491 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002492 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002493 return 0;
2494
2495 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002496 memcpy(temp->area + temp->data, str, len);
2497 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002498 }
2499
2500 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002501 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002502 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002503
2504 return 1;
2505}
2506
Emeric Brun54c4ac82014-11-03 15:32:43 +01002507/* This sample function is designed to extract some bytes from an input buffer.
2508 * First arg is the offset.
2509 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002510static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002511{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002512 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2513 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002514 return 1;
2515 }
2516
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002517 if (smp->data.u.str.size)
2518 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002519 smp->data.u.str.data -= arg_p[0].data.sint;
2520 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002521
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002522 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2523 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002524
2525 return 1;
2526}
2527
Emeric Brunf399b0d2014-11-03 17:07:03 +01002528static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2529 const char *file, int line, char **err)
2530{
2531 struct arg *arg = args;
2532
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002533 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002534 memprintf(err, "Unexpected arg type");
2535 return 0;
2536 }
2537
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002538 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002539 memprintf(err, "Unexpected value 0 for index");
2540 return 0;
2541 }
2542
2543 arg++;
2544
2545 if (arg->type != ARGT_STR) {
2546 memprintf(err, "Unexpected arg type");
2547 return 0;
2548 }
2549
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002550 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002551 memprintf(err, "Empty separators list");
2552 return 0;
2553 }
2554
2555 return 1;
2556}
2557
2558/* This sample function is designed to a return selected part of a string (field).
2559 * First arg is the index of the field (start at 1)
2560 * Second arg is a char list of separators (type string)
2561 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002562static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002563{
Marcin Deranek9631a282018-04-16 14:30:46 +02002564 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002565 char *start, *end;
2566 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002567 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002568
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002569 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002570 return 0;
2571
Marcin Deranek9631a282018-04-16 14:30:46 +02002572 if (arg_p[0].data.sint < 0) {
2573 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002574 end = start = smp->data.u.str.area + smp->data.u.str.data;
2575 while (start > smp->data.u.str.area) {
2576 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2577 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002578 if (field == arg_p[0].data.sint) {
2579 if (count == 1)
2580 goto found;
2581 else if (count > 1)
2582 count--;
2583 } else {
2584 end = start-1;
2585 field--;
2586 }
2587 break;
2588 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002589 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002590 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002591 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002592 } else {
2593 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002594 end = start = smp->data.u.str.area;
2595 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2596 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2597 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002598 if (field == arg_p[0].data.sint) {
2599 if (count == 1)
2600 goto found;
2601 else if (count > 1)
2602 count--;
2603 } else {
2604 start = end+1;
2605 field++;
2606 }
2607 break;
2608 }
2609 }
2610 end++;
2611 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002612 }
2613
2614 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002615 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002616 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002617 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002618 }
2619found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002620 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002621 /* If ret string is len 0, no need to
2622 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002623 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002624 return 1;
2625
Emeric Brunf399b0d2014-11-03 17:07:03 +01002626 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002627 Note: smp->data.u.str.size cannot be set to 0 */
2628 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002629 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002630
Thayne McCombsb2843052021-04-11 23:26:59 -06002631 smp->data.u.str.area = start;
2632
Emeric Brunf399b0d2014-11-03 17:07:03 +01002633 return 1;
2634}
2635
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002636/* This sample function is designed to return a word from a string.
2637 * First arg is the index of the word (start at 1)
2638 * Second arg is a char list of words separators (type string)
2639 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002640static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002641{
Marcin Deranek9631a282018-04-16 14:30:46 +02002642 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002643 char *start, *end;
2644 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002645 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002646
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002647 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002648 return 0;
2649
2650 word = 0;
2651 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002652 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002653 end = start = smp->data.u.str.area + smp->data.u.str.data;
2654 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002655 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002656 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2657 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002658 issep = 1;
2659 break;
2660 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002661 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002662 if (!inword) {
2663 if (!issep) {
2664 if (word != arg_p[0].data.sint) {
2665 word--;
2666 end = start;
2667 }
2668 inword = 1;
2669 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002670 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002671 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002672 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002673 if (count == 1)
2674 goto found;
2675 else if (count > 1)
2676 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002677 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002678 inword = 0;
2679 }
2680 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002681 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002682 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002683 end = start = smp->data.u.str.area;
2684 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002685 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002686 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2687 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002688 issep = 1;
2689 break;
2690 }
2691 }
2692 if (!inword) {
2693 if (!issep) {
2694 if (word != arg_p[0].data.sint) {
2695 word++;
2696 start = end;
2697 }
2698 inword = 1;
2699 }
2700 }
2701 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002702 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002703 if (count == 1)
2704 goto found;
2705 else if (count > 1)
2706 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002707 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002708 inword = 0;
2709 }
2710 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002711 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002712 }
2713
2714 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002715 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002716 smp->data.u.str.data = 0;
Tim Duesterhus3ebaeca2023-11-30 16:41:17 +01002717 return 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002718 }
2719found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002720 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002721 /* If ret string is len 0, no need to
2722 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002723 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002724 return 1;
2725
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002726
2727 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002728 Note: smp->data.u.str.size cannot be set to 0 */
2729 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002730 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002731
Thayne McCombsdaa8cd12022-05-25 22:58:51 -06002732 smp->data.u.str.area = start;
2733
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002734 return 1;
2735}
2736
Willy Tarreau7eda8492015-01-20 19:47:06 +01002737static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2738 const char *file, int line, char **err)
2739{
2740 struct arg *arg = args;
2741 char *p;
2742 int len;
2743
2744 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2745 arg[0].type_flags = 0;
2746
2747 if (arg[2].type != ARGT_STR)
2748 return 1;
2749
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002750 p = arg[2].data.str.area;
2751 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002752 while (len) {
2753 if (*p == 'i') {
2754 arg[0].type_flags |= ARGF_REG_ICASE;
2755 }
2756 else if (*p == 'g') {
2757 arg[0].type_flags |= ARGF_REG_GLOB;
2758 }
2759 else {
2760 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2761 return 0;
2762 }
2763 p++;
2764 len--;
2765 }
2766 return 1;
2767}
2768
2769/* This sample function is designed to do the equivalent of s/match/replace/ on
2770 * the input string. It applies a regex and restarts from the last matched
2771 * location until nothing matches anymore. First arg is the regex to apply to
2772 * the input string, second arg is the replacement expression.
2773 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002774static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002775{
2776 char *start, *end;
2777 struct my_regex *reg = arg_p[0].data.reg;
2778 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002779 struct buffer *trash = get_trash_chunk();
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002780 struct buffer *output;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002781 int flag, max;
2782 int found;
2783
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002784 start = smp->data.u.str.area;
2785 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002786
2787 flag = 0;
2788 while (1) {
2789 /* check for last round which is used to copy remaining parts
2790 * when not running in global replacement mode.
2791 */
2792 found = 0;
2793 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2794 /* Note: we can have start == end on empty strings or at the end */
2795 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2796 }
2797
2798 if (!found)
2799 pmatch[0].rm_so = end - start;
2800
2801 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002802 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002803 if (max && pmatch[0].rm_so > 0) {
2804 if (max > pmatch[0].rm_so)
2805 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002806 memcpy(trash->area + trash->data, start, max);
2807 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002808 }
2809
2810 if (!found)
2811 break;
2812
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002813 output = alloc_trash_chunk();
Willy Tarreau23997da2020-02-18 14:27:44 +01002814 if (!output)
2815 break;
2816
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002817 output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2818
Willy Tarreau7eda8492015-01-20 19:47:06 +01002819 /* replace the matching part */
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002820 max = output->size - output->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002821 if (max) {
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002822 if (max > output->data)
2823 max = output->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002824 memcpy(trash->area + trash->data,
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002825 output->area, max);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002826 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002827 }
2828
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002829 free_trash_chunk(output);
2830
Willy Tarreau7eda8492015-01-20 19:47:06 +01002831 /* stop here if we're done with this string */
2832 if (start >= end)
2833 break;
2834
2835 /* We have a special case for matches of length 0 (eg: "x*y*").
2836 * These ones are considered to match in front of a character,
2837 * so we have to copy that character and skip to the next one.
2838 */
2839 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002840 if (trash->data < trash->size)
2841 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002842 pmatch[0].rm_eo++;
2843 }
2844
2845 start += pmatch[0].rm_eo;
2846 flag |= REG_NOTBOL;
2847 }
2848
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002849 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002850 return 1;
2851}
2852
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002853/* This function check an operator entry. It expects a string.
2854 * The string can be an integer or a variable name.
2855 */
2856static int check_operator(struct arg *args, struct sample_conv *conv,
2857 const char *file, int line, char **err)
2858{
2859 const char *str;
2860 const char *end;
Christopher Faulet95917132020-08-05 23:07:07 +02002861 long long int i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002862
2863 /* Try to decode a variable. */
2864 if (vars_check_arg(&args[0], NULL))
2865 return 1;
2866
2867 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002868 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002869 end = str + strlen(str);
Christopher Faulet95917132020-08-05 23:07:07 +02002870 i = read_int64(&str, end);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002871 if (*str != '\0') {
2872 memprintf(err, "expects an integer or a variable name");
2873 return 0;
2874 }
Christopher Faulet95917132020-08-05 23:07:07 +02002875
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002876 chunk_destroy(&args[0].data.str);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002877 args[0].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02002878 args[0].data.sint = i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002879 return 1;
2880}
2881
Willy Tarreau6204cd92016-03-10 16:33:04 +01002882/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002883 * If the arg contain an integer, the integer is returned in the
2884 * sample. If the arg contains a variable descriptor, it returns the
2885 * variable value.
2886 *
2887 * This function returns 0 if an error occurs, otherwise it returns 1.
2888 */
Willy Tarreau9cfb71f2021-10-06 15:19:05 +02002889int sample_conv_var2smp_sint(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002890{
2891 switch (arg->type) {
2892 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002893 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002894 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002895 return 1;
2896 case ARGT_VAR:
Willy Tarreau8fdaf252021-10-06 15:30:52 +02002897 return sample_conv_var2smp(&arg->data.var, smp, SMP_T_SINT);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002898 default:
2899 return 0;
2900 }
2901}
2902
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002903/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002904 * result.
2905 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002906static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002907{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002908 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002909 return 1;
2910}
2911
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002912/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002913 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002914 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002915static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002916{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002917 struct sample tmp;
2918
Willy Tarreau7560dd42016-03-10 16:28:58 +01002919 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau9cfb71f2021-10-06 15:19:05 +02002920 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002921 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002922 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002923 return 1;
2924}
2925
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002926/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002927 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002928 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002929static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002930{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002931 struct sample tmp;
2932
Willy Tarreau7560dd42016-03-10 16:28:58 +01002933 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau9cfb71f2021-10-06 15:19:05 +02002934 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002935 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002936 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002937 return 1;
2938}
2939
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002940/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002941 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002942 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002943static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002944{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002945 struct sample tmp;
2946
Willy Tarreau7560dd42016-03-10 16:28:58 +01002947 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau9cfb71f2021-10-06 15:19:05 +02002948 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002949 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002950 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002951 return 1;
2952}
2953
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002954static inline long long int arith_add(long long int a, long long int b)
2955{
2956 /* Prevent overflow and makes capped calculus.
2957 * We must ensure that the check calculus doesn't
2958 * exceed the signed 64 bits limits.
2959 *
2960 * +----------+----------+
2961 * | a<0 | a>=0 |
2962 * +------+----------+----------+
2963 * | b<0 | MIN-a>b | no check |
2964 * +------+----------+----------+
2965 * | b>=0 | no check | MAX-a<b |
2966 * +------+----------+----------+
2967 */
2968 if ((a ^ b) >= 0) {
Christopher Fauletfbd88d42023-07-17 08:35:11 +02002969 /* signs are same. */
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002970 if (a < 0) {
2971 if (LLONG_MIN - a > b)
2972 return LLONG_MIN;
2973 }
Christopher Fauletfbd88d42023-07-17 08:35:11 +02002974 else if (LLONG_MAX - a < b)
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002975 return LLONG_MAX;
2976 }
2977 return a + b;
2978}
2979
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002980/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002981 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002982 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002983static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002984{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002985 struct sample tmp;
2986
Willy Tarreau7560dd42016-03-10 16:28:58 +01002987 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau9cfb71f2021-10-06 15:19:05 +02002988 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002989 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002990 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002991 return 1;
2992}
2993
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002994/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002995 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002996 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002997static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002998 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002999{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003000 struct sample tmp;
3001
Willy Tarreau7560dd42016-03-10 16:28:58 +01003002 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau9cfb71f2021-10-06 15:19:05 +02003003 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003004 return 0;
3005
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003006 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
3007 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
3008 * of -LLONG_MIN and correct the result.
3009 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003010 if (tmp.data.u.sint == LLONG_MIN) {
3011 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
3012 if (smp->data.u.sint < LLONG_MAX)
3013 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003014 return 1;
3015 }
3016
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003017 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003018 * the second operand.
3019 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003020 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01003021 return 1;
3022}
3023
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003024/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003025 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003026 * If the result makes an overflow, then the largest possible quantity is
3027 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003028 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003029static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003030 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003031{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003032 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003033 long long int c;
3034
Willy Tarreau7560dd42016-03-10 16:28:58 +01003035 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau9cfb71f2021-10-06 15:19:05 +02003036 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003037 return 0;
3038
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003039 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003040 if (!smp->data.u.sint || !tmp.data.u.sint) {
3041 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003042 return 1;
3043 }
3044
3045 /* The multiply between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003046 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003047 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003048 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3049 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003050 return 1;
3051 }
3052
3053 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003054 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003055
3056 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003057 if (smp->data.u.sint != c / tmp.data.u.sint) {
3058 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
3059 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003060 return 1;
3061 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003062 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003063 return 1;
3064 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003065 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01003066 return 1;
3067}
3068
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003069/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003070 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003071 * If arg_p makes the result overflow, then the largest possible quantity is
3072 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003073 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003074static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003075 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003076{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003077 struct sample tmp;
3078
Willy Tarreau7560dd42016-03-10 16:28:58 +01003079 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau9cfb71f2021-10-06 15:19:05 +02003080 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003081 return 0;
3082
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003083 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003084 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003085 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003086 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003087 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3088 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003089 return 1;
3090 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003091 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003092 return 1;
3093 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003094 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01003095 return 1;
3096}
3097
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003098/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003099 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003100 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003101 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003102static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003103 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003104{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003105 struct sample tmp;
3106
Willy Tarreau7560dd42016-03-10 16:28:58 +01003107 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau9cfb71f2021-10-06 15:19:05 +02003108 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003109 return 0;
3110
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003111 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003112 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003113 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003114 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003115 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3116 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003117 return 1;
3118 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003119 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003120 return 1;
3121 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003122 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01003123 return 1;
3124}
3125
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003126/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01003127 * result.
3128 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003129static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003130 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003131{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003132 if (smp->data.u.sint == LLONG_MIN)
3133 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003134 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003135 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003136 return 1;
3137}
3138
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003139/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003140 * false. The output is a BOOL.
3141 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003142static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003143 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003144{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003145 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003146 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003147 return 1;
3148}
3149
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003150/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003151 * truee. The output is a BOOL.
3152 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003153static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003154 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003155{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003156 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003157 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003158 return 1;
3159}
3160
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003161/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003162 * The output is a BOOL.
3163 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003164static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003165 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003166{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003167 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003168 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003169 return 1;
3170}
3171
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003172/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003173 * The output is a BOOL.
3174 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003175static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003176 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003177{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003178 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003179 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003180 return 1;
3181}
3182
Willy Tarreau280f42b2018-02-19 15:34:12 +01003183/* appends an optional const string, an optional variable contents and another
3184 * optional const string to an existing string.
3185 */
3186static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
3187{
Willy Tarreau83061a82018-07-13 11:56:34 +02003188 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003189 struct sample tmp;
3190 int max;
3191
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003192 trash = alloc_trash_chunk();
William Dauchye9970102021-01-11 11:05:58 +01003193 if (!trash)
3194 return 0;
3195
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003196 trash->data = smp->data.u.str.data;
3197 if (trash->data > trash->size - 1)
3198 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003199
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003200 memcpy(trash->area, smp->data.u.str.area, trash->data);
3201 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003202
3203 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003204 max = arg_p[0].data.str.data;
3205 if (max > trash->size - 1 - trash->data)
3206 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003207
3208 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003209 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
3210 trash->data += max;
3211 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003212 }
3213
3214 /* append second string (variable) if it's found and we can turn it
3215 * into a string.
3216 */
3217 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3218 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
3219 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
3220 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
3221
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003222 max = tmp.data.u.str.data;
3223 if (max > trash->size - 1 - trash->data)
3224 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003225
3226 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003227 memcpy(trash->area + trash->data, tmp.data.u.str.area,
3228 max);
3229 trash->data += max;
3230 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003231 }
3232 }
3233
3234 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003235 max = arg_p[2].data.str.data;
3236 if (max > trash->size - 1 - trash->data)
3237 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003238
3239 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003240 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
3241 trash->data += max;
3242 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003243 }
3244
3245 smp->data.u.str = *trash;
3246 smp->data.type = SMP_T_STR;
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003247 smp_dup(smp);
3248 free_trash_chunk(trash);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003249 return 1;
3250}
3251
3252/* This function checks the "concat" converter's arguments and extracts the
3253 * variable name and its scope.
3254 */
3255static int smp_check_concat(struct arg *args, struct sample_conv *conv,
3256 const char *file, int line, char **err)
3257{
3258 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003259 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
3260 memprintf(err, "failed to register variable name '%s'",
3261 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003262 return 0;
3263 }
3264 return 1;
3265}
3266
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003267/* Compares string with a variable containing a string. Return value
Tim Duesterhusca097c12018-04-27 21:18:45 +02003268 * is compatible with strcmp(3)'s return value.
3269 */
3270static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
3271{
3272 struct sample tmp;
3273 int max, result;
3274
3275 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3276 if (arg_p[0].type != ARGT_VAR)
3277 return 0;
Willy Tarreau8fdaf252021-10-06 15:30:52 +02003278
3279 if (!sample_conv_var2smp(&arg_p[0].data.var, &tmp, SMP_T_STR))
Tim Duesterhusca097c12018-04-27 21:18:45 +02003280 return 0;
3281
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003282 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
3283 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003284 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003285 if (smp->data.u.str.data != tmp.data.u.str.data) {
3286 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02003287 result = -1;
3288 }
3289 else {
3290 result = 1;
3291 }
3292 }
3293 }
3294
3295 smp->data.u.sint = result;
3296 smp->data.type = SMP_T_SINT;
3297 return 1;
3298}
3299
Willy Tarreau2a8a2f02021-05-09 11:59:49 +02003300#if defined(HAVE_CRYPTO_memcmp)
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003301/* Compares bytestring with a variable containing a bytestring. Return value
3302 * is `true` if both bytestrings are bytewise identical and `false` otherwise.
3303 *
3304 * Comparison will be performed in constant time if both bytestrings are of
3305 * the same length. If the lengths differ execution time will not be constant.
3306 */
3307static int sample_conv_secure_memcmp(const struct arg *arg_p, struct sample *smp, void *private)
3308{
3309 struct sample tmp;
3310 int result;
3311
3312 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3313 if (arg_p[0].type != ARGT_VAR)
3314 return 0;
Willy Tarreau8fdaf252021-10-06 15:30:52 +02003315
3316 if (!sample_conv_var2smp(&arg_p[0].data.var, &tmp, SMP_T_BIN))
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003317 return 0;
3318
3319 if (smp->data.u.str.data != tmp.data.u.str.data) {
3320 smp->data.u.sint = 0;
3321 smp->data.type = SMP_T_BOOL;
3322 return 1;
3323 }
3324
3325 /* The following comparison is performed in constant time. */
3326 result = CRYPTO_memcmp(smp->data.u.str.area, tmp.data.u.str.area, smp->data.u.str.data);
3327
3328 smp->data.u.sint = result == 0;
3329 smp->data.type = SMP_T_BOOL;
3330 return 1;
3331}
3332#endif
3333
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02003334/* Takes a boolean as input. Returns the first argument if that boolean is true and
3335 * the second argument otherwise.
3336 */
3337static int sample_conv_iif(const struct arg *arg_p, struct sample *smp, void *private)
3338{
3339 smp->data.type = SMP_T_STR;
3340 smp->flags |= SMP_F_CONST;
3341
3342 if (smp->data.u.sint) {
3343 smp->data.u.str.data = arg_p[0].data.str.data;
3344 smp->data.u.str.area = arg_p[0].data.str.area;
3345 }
3346 else {
3347 smp->data.u.str.data = arg_p[1].data.str.data;
3348 smp->data.u.str.area = arg_p[1].data.str.area;
3349 }
3350
3351 return 1;
3352}
3353
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003354#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
3355#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
3356#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
3357
3358/*
3359 * Extract the field value of an input binary sample. Takes a mandatory argument:
3360 * the protocol buffers field identifier (dotted notation) internally represented
3361 * as an array of unsigned integers and its size.
3362 * Return 1 if the field was found, 0 if not.
3363 */
3364static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
3365{
3366 unsigned char *pos;
3367 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003368
3369 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003370 grpc_left = smp->data.u.str.data;
3371
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01003372 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003373 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003374
3375 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
3376
3377 pos += GRPC_MSG_HEADER_SZ;
3378 grpc_left -= GRPC_MSG_HEADER_SZ;
3379
3380 if (grpc_left < left)
3381 return 0;
3382
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01003383 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
3384 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003385
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003386 grpc_left -= grpc_msg_len;
3387 }
3388
3389 return 0;
3390}
3391
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003392static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
3393{
3394 unsigned char *pos;
3395 size_t left;
3396
3397 pos = (unsigned char *)smp->data.u.str.area;
3398 left = smp->data.u.str.data;
3399
3400 return protobuf_field_lookup(arg_p, smp, &pos, &left);
3401}
3402
3403static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
3404 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003405{
3406 if (!args[1].type) {
3407 args[1].type = ARGT_SINT;
3408 args[1].data.sint = PBUF_T_BINARY;
3409 }
3410 else {
3411 int pbuf_type;
3412
3413 pbuf_type = protobuf_type(args[1].data.str.area);
3414 if (pbuf_type == -1) {
3415 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
3416 return 0;
3417 }
3418
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003419 chunk_destroy(&args[1].data.str);
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003420 args[1].type = ARGT_SINT;
3421 args[1].data.sint = pbuf_type;
3422 }
3423
3424 return 1;
3425}
3426
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003427/*
3428 * Extract the tag value of an input binary sample. Takes a mandatory argument:
3429 * the FIX protocol tag identifier.
3430 * Return 1 if the tag was found, 0 if not.
3431 */
3432static int sample_conv_fix_tag_value(const struct arg *arg_p, struct sample *smp, void *private)
3433{
3434 struct ist value;
3435
3436 smp->flags &= ~SMP_F_MAY_CHANGE;
3437 value = fix_tag_value(ist2(smp->data.u.str.area, smp->data.u.str.data),
3438 arg_p[0].data.sint);
3439 if (!istlen(value)) {
Christopher Faulet08da2732021-10-13 17:58:53 +02003440 if (isttest(value)) {
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003441 /* value != IST_NULL, need more data */
3442 smp->flags |= SMP_F_MAY_CHANGE;
3443 }
3444 return 0;
3445 }
3446
3447 smp->data.u.str = ist2buf(value);
3448 smp->flags |= SMP_F_CONST;
3449
3450 return 1;
3451}
3452
3453/* This function checks the "fix_tag_value" converter configuration.
3454 * It expects a "known" (by HAProxy) tag name or ID.
3455 * Tag string names are converted to their ID counterpart because this is the
3456 * format they are sent over the wire.
3457 */
3458static int sample_conv_fix_value_check(struct arg *args, struct sample_conv *conv,
3459 const char *file, int line, char **err)
3460{
3461 struct ist str;
3462 unsigned int tag;
3463
3464 str = ist2(args[0].data.str.area, args[0].data.str.data);
3465 tag = fix_tagid(str);
3466 if (!tag) {
3467 memprintf(err, "Unknown FIX tag name '%s'", args[0].data.str.area);
3468 return 0;
3469 }
3470
3471 chunk_destroy(&args[0].data.str);
3472 args[0].type = ARGT_SINT;
3473 args[0].data.sint = tag;
3474
3475 return 1;
3476}
3477
3478/*
3479 * Checks that a buffer contains a valid FIX message
3480 *
3481 * Return 1 if the check could be run, 0 if not.
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003482 * The result of the analyse itself is stored in <smp> as a boolean
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003483 */
3484static int sample_conv_fix_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3485{
3486 struct ist msg;
3487
3488 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3489
3490 smp->flags &= ~SMP_F_MAY_CHANGE;
3491 switch (fix_validate_message(msg)) {
3492 case FIX_VALID_MESSAGE:
3493 smp->data.type = SMP_T_BOOL;
3494 smp->data.u.sint = 1;
3495 return 1;
3496 case FIX_NEED_MORE_DATA:
3497 smp->flags |= SMP_F_MAY_CHANGE;
3498 return 0;
3499 case FIX_INVALID_MESSAGE:
3500 smp->data.type = SMP_T_BOOL;
3501 smp->data.u.sint = 0;
3502 return 1;
3503 }
3504 return 0;
3505}
3506
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003507/*
3508 * Extract the field value of an input binary sample containing an MQTT packet.
3509 * Takes 2 mandatory arguments:
3510 * - packet type
3511 * - field name
3512 *
3513 * return 1 if the field was found, 0 if not.
3514 */
3515static int sample_conv_mqtt_field_value(const struct arg *arg_p, struct sample *smp, void *private)
3516{
3517 struct ist pkt, value;
3518 int type, fieldname_id;
3519
3520 pkt = ist2(smp->data.u.str.area, smp->data.u.str.data);
3521 type = arg_p[0].data.sint;
3522 fieldname_id = arg_p[1].data.sint;
3523
3524 smp->flags &= ~SMP_F_MAY_CHANGE;
3525 value = mqtt_field_value(pkt, type, fieldname_id);
3526 if (!istlen(value)) {
3527 if (isttest(value)) {
3528 /* value != IST_NULL, need more data */
3529 smp->flags |= SMP_F_MAY_CHANGE;
3530 }
3531 return 0;
3532 }
3533
3534 smp->data.u.str = ist2buf(value);
3535 smp->flags |= SMP_F_CONST;
3536 return 1;
3537}
3538
3539/*
3540 * this function checks the "mqtt_field_value" converter configuration.
3541 * It expects a known packet type name or ID and a field name, in this order
3542 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003543 * Args[0] will be turned into a MQTT_CPT_* value for direct matching when parsing
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003544 * a packet.
3545 */
3546static int sample_conv_mqtt_field_value_check(struct arg *args, struct sample_conv *conv,
3547 const char *file, int line, char **err)
3548{
3549 int type, fieldname_id;
3550
3551 /* check the MQTT packet type is valid */
3552 type = mqtt_typeid(ist2(args[0].data.str.area, args[0].data.str.data));
3553 if (type == MQTT_CPT_INVALID) {
3554 memprintf(err, "Unknown MQTT type '%s'", args[0].data.str.area);
3555 return 0;
3556 }
3557
3558 /* check the field name belongs to the MQTT packet type */
3559 fieldname_id = mqtt_check_type_fieldname(type, ist2(args[1].data.str.area, args[1].data.str.data));
3560 if (fieldname_id == MQTT_FN_INVALID) {
3561 memprintf(err, "Unknown MQTT field name '%s' for packet type '%s'", args[1].data.str.area,
3562 args[0].data.str.area);
3563 return 0;
3564 }
3565
3566 /* save numeric counterparts of type and field name */
3567 chunk_destroy(&args[0].data.str);
3568 chunk_destroy(&args[1].data.str);
3569 args[0].type = ARGT_SINT;
3570 args[0].data.sint = type;
3571 args[1].type = ARGT_SINT;
3572 args[1].data.sint = fieldname_id;
3573
3574 return 1;
3575}
3576
3577/*
3578 * Checks that <smp> contains a valid MQTT message
3579 *
3580 * The function returns 1 if the check was run to its end, 0 otherwise.
3581 * The result of the analyse itself is stored in <smp> as a boolean.
3582 */
3583static int sample_conv_mqtt_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3584{
3585 struct ist msg;
3586
3587 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3588
3589 smp->flags &= ~SMP_F_MAY_CHANGE;
3590 switch (mqtt_validate_message(msg, NULL)) {
3591 case FIX_VALID_MESSAGE:
3592 smp->data.type = SMP_T_BOOL;
3593 smp->data.u.sint = 1;
3594 return 1;
3595 case FIX_NEED_MORE_DATA:
3596 smp->flags |= SMP_F_MAY_CHANGE;
3597 return 0;
3598 case FIX_INVALID_MESSAGE:
3599 smp->data.type = SMP_T_BOOL;
3600 smp->data.u.sint = 0;
3601 return 1;
3602 }
3603 return 0;
3604}
3605
Tim Duesterhusca097c12018-04-27 21:18:45 +02003606/* This function checks the "strcmp" converter's arguments and extracts the
3607 * variable name and its scope.
3608 */
3609static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
3610 const char *file, int line, char **err)
3611{
Willy Tarreaua1169b62021-05-08 06:50:28 +02003612 if (!args[0].data.str.data) {
3613 memprintf(err, "missing variable name");
3614 return 0;
3615 }
3616
Tim Duesterhusca097c12018-04-27 21:18:45 +02003617 /* Try to decode a variable. */
3618 if (vars_check_arg(&args[0], NULL))
3619 return 1;
3620
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003621 memprintf(err, "failed to register variable name '%s'",
3622 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003623 return 0;
3624}
3625
Willy Tarreau2a8a2f02021-05-09 11:59:49 +02003626#if defined(HAVE_CRYPTO_memcmp)
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003627/* This function checks the "secure_memcmp" converter's arguments and extracts the
3628 * variable name and its scope.
3629 */
3630static int smp_check_secure_memcmp(struct arg *args, struct sample_conv *conv,
3631 const char *file, int line, char **err)
3632{
Willy Tarreaua1169b62021-05-08 06:50:28 +02003633 if (!args[0].data.str.data) {
3634 memprintf(err, "missing variable name");
3635 return 0;
3636 }
3637
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003638 /* Try to decode a variable. */
3639 if (vars_check_arg(&args[0], NULL))
3640 return 1;
3641
3642 memprintf(err, "failed to register variable name '%s'",
3643 args[0].data.str.area);
3644 return 0;
3645}
3646#endif
3647
Christopher Faulet4ccc12f2020-04-01 09:08:32 +02003648/**/
3649static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private)
3650{
3651 struct buffer *tmp;
3652 uint32_t n;
3653
3654 n = htonl((uint32_t)smp->data.u.sint);
3655 tmp = get_trash_chunk();
3656
3657 memcpy(b_head(tmp), &n, 4);
3658 b_add(tmp, 4);
3659
3660 smp->data.u.str = *tmp;
3661 smp->data.type = SMP_T_BIN;
3662 return 1;
3663}
3664
Christopher Fauletea159d62020-04-01 16:21:44 +02003665/**/
3666static int sample_conv_cut_crlf(const struct arg *arg_p, struct sample *smp, void *private)
3667{
3668 char *p;
3669 size_t l;
3670
3671 p = smp->data.u.str.area;
3672 for (l = 0; l < smp->data.u.str.data; l++) {
3673 if (*(p+l) == '\r' || *(p+l) == '\n')
3674 break;
3675 }
3676 smp->data.u.str.data = l;
3677 return 1;
3678}
3679
Christopher Faulet51fc9d12020-04-01 17:24:41 +02003680/**/
3681static int sample_conv_ltrim(const struct arg *arg_p, struct sample *smp, void *private)
3682{
3683 char *delimiters, *p;
3684 size_t dlen, l;
3685
3686 delimiters = arg_p[0].data.str.area;
3687 dlen = arg_p[0].data.str.data;
3688
3689 l = smp->data.u.str.data;
3690 p = smp->data.u.str.area;
3691 while (l && memchr(delimiters, *p, dlen) != NULL) {
3692 p++;
3693 l--;
3694 }
3695
3696 smp->data.u.str.area = p;
3697 smp->data.u.str.data = l;
3698 return 1;
3699}
3700
Christopher Faulet568415a2020-04-01 17:24:47 +02003701/**/
3702static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *private)
3703{
3704 char *delimiters, *p;
3705 size_t dlen, l;
3706
3707 delimiters = arg_p[0].data.str.area;
3708 dlen = arg_p[0].data.str.data;
3709
3710 l = smp->data.u.str.data;
3711 p = smp->data.u.str.area + l - 1;
3712 while (l && memchr(delimiters, *p, dlen) != NULL) {
3713 p--;
3714 l--;
3715 }
3716
3717 smp->data.u.str.data = l;
3718 return 1;
3719}
3720
Alex51c8ad42021-04-15 16:45:15 +02003721/* This function checks the "json_query" converter's arguments. */
3722static int sample_check_json_query(struct arg *arg, struct sample_conv *conv,
3723 const char *file, int line, char **err)
3724{
3725 if (arg[0].data.str.data == 0) {
3726 memprintf(err, "json_path must not be empty");
3727 return 0;
3728 }
3729
3730 if (arg[1].data.str.data != 0) {
3731 if (strcmp(arg[1].data.str.area, "int") != 0) {
3732 memprintf(err, "output_type only supports \"int\" as argument");
3733 return 0;
3734 } else {
3735 arg[1].type = ARGT_SINT;
3736 arg[1].data.sint = 0;
3737 }
3738 }
3739 return 1;
3740}
3741
3742/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
3743 * the recommendation for interoperable integers in section 6 of RFC 7159.
3744 */
3745#define JSON_INT_MAX ((1LL << 53) - 1)
3746#define JSON_INT_MIN (-JSON_INT_MAX)
3747
3748/* This sample function get the value from a given json string.
3749 * The mjson library is used to parse the JSON struct
3750 */
3751static int sample_conv_json_query(const struct arg *args, struct sample *smp, void *private)
3752{
3753 struct buffer *trash = get_trash_chunk();
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003754 const char *token; /* holds the temporary string from mjson_find */
3755 int token_size; /* holds the length of <token> */
Alex51c8ad42021-04-15 16:45:15 +02003756
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003757 enum mjson_tok token_type;
Alex51c8ad42021-04-15 16:45:15 +02003758
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003759 token_type = mjson_find(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &token, &token_size);
3760
3761 switch (token_type) {
Alex51c8ad42021-04-15 16:45:15 +02003762 case MJSON_TOK_NUMBER:
3763 if (args[1].type == ARGT_SINT) {
Willy Tarreaufb601952021-05-14 08:51:53 +02003764 smp->data.u.sint = strtoll(token, NULL, 0);
Alex51c8ad42021-04-15 16:45:15 +02003765
3766 if (smp->data.u.sint < JSON_INT_MIN || smp->data.u.sint > JSON_INT_MAX)
3767 return 0;
3768
3769 smp->data.type = SMP_T_SINT;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003770
3771 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003772 } else {
3773 double double_val;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003774
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003775 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 +02003776 return 0;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003777
3778 trash->data = snprintf(trash->area,trash->size,"%g",double_val);
3779 smp->data.u.str = *trash;
3780 smp->data.type = SMP_T_STR;
3781
3782 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003783 }
Alex51c8ad42021-04-15 16:45:15 +02003784 case MJSON_TOK_TRUE:
3785 smp->data.type = SMP_T_BOOL;
3786 smp->data.u.sint = 1;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003787
3788 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003789 case MJSON_TOK_FALSE:
3790 smp->data.type = SMP_T_BOOL;
3791 smp->data.u.sint = 0;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003792
3793 return 1;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003794 case MJSON_TOK_STRING: {
3795 int len;
3796
3797 len = mjson_get_string(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, trash->area, trash->size);
3798
3799 if (len == -1) {
Alex51c8ad42021-04-15 16:45:15 +02003800 /* invalid string */
3801 return 0;
Alex51c8ad42021-04-15 16:45:15 +02003802 }
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003803
3804 trash->data = len;
3805 smp->data.u.str = *trash;
3806 smp->data.type = SMP_T_STR;
3807
3808 return 1;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003809 }
Tim Duesterhus8f3bc8f2021-04-15 18:14:32 +02003810 case MJSON_TOK_NULL:
3811 case MJSON_TOK_ARRAY:
3812 case MJSON_TOK_OBJECT:
3813 /* We cannot handle these. */
3814 return 0;
3815 case MJSON_TOK_INVALID:
3816 /* Nothing matches the query. */
3817 return 0;
3818 case MJSON_TOK_KEY:
3819 /* This is not a valid return value according to the
3820 * mjson documentation, but we handle it to benefit
3821 * from '-Wswitch'.
3822 */
Alex51c8ad42021-04-15 16:45:15 +02003823 return 0;
3824 }
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003825
3826 my_unreachable();
3827 return 0;
Alex51c8ad42021-04-15 16:45:15 +02003828}
3829
3830
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003831/************************************************************************/
3832/* All supported sample fetch functions must be declared here */
3833/************************************************************************/
3834
3835/* force TRUE to be returned at the fetch level */
3836static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003837smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003838{
Willy Tarreau280f42b2018-02-19 15:34:12 +01003839 if (!smp_make_rw(smp))
3840 return 0;
3841
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003842 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003843 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003844 return 1;
3845}
3846
3847/* force FALSE to be returned at the fetch level */
3848static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003849smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003850{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003851 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003852 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003853 return 1;
3854}
3855
3856/* retrieve environment variable $1 as a string */
3857static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003858smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003859{
3860 char *env;
3861
Christopher Faulete6e7a582021-01-29 11:29:28 +01003862 if (args[0].type != ARGT_STR)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003863 return 0;
3864
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003865 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003866 if (!env)
3867 return 0;
3868
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003869 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003870 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003871 smp->data.u.str.area = env;
3872 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003873 return 1;
3874}
3875
Damien Claisseae6f1252019-10-30 15:57:28 +00003876/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3877 * optional string representing the unit of the result: "s" for seconds, "ms" for
3878 * milliseconds and "us" for microseconds.
3879 * Returns 0 on error and non-zero if OK.
3880 */
3881int smp_check_date_unit(struct arg *args, char **err)
3882{
3883 if (args[1].type == ARGT_STR) {
Christopher Faulet95917132020-08-05 23:07:07 +02003884 long long int unit;
3885
Damien Claisseae6f1252019-10-30 15:57:28 +00003886 if (strcmp(args[1].data.str.area, "s") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003887 unit = TIME_UNIT_S;
Damien Claisseae6f1252019-10-30 15:57:28 +00003888 }
3889 else if (strcmp(args[1].data.str.area, "ms") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003890 unit = TIME_UNIT_MS;
Damien Claisseae6f1252019-10-30 15:57:28 +00003891 }
3892 else if (strcmp(args[1].data.str.area, "us") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003893 unit = TIME_UNIT_US;
Damien Claisseae6f1252019-10-30 15:57:28 +00003894 }
3895 else {
3896 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
3897 args[1].data.str.area);
3898 return 0;
3899 }
Christopher Faulet95917132020-08-05 23:07:07 +02003900
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003901 chunk_destroy(&args[1].data.str);
Damien Claisseae6f1252019-10-30 15:57:28 +00003902 args[1].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02003903 args[1].data.sint = unit;
Damien Claisseae6f1252019-10-30 15:57:28 +00003904 }
3905 else if (args[1].type != ARGT_STOP) {
3906 memprintf(err, "Unexpected arg type");
3907 return 0;
3908 }
3909
3910 return 1;
3911}
3912
3913/* retrieve the current local date in epoch time, converts it to milliseconds
3914 * or microseconds if asked to in optional args[1] unit param, and applies an
3915 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003916 */
3917static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003918smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003919{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003920 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003921
Damien Claisseae6f1252019-10-30 15:57:28 +00003922 /* report in milliseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003923 if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003924 smp->data.u.sint *= 1000;
3925 smp->data.u.sint += date.tv_usec / 1000;
3926 }
3927 /* report in microseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003928 else if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003929 smp->data.u.sint *= 1000000;
3930 smp->data.u.sint += date.tv_usec;
3931 }
3932
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003933 /* add offset */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003934 if (args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003935 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003936
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003937 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003938 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3939 return 1;
3940}
3941
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003942/* retrieve the current microsecond part of the date */
3943static int
3944smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3945{
3946 smp->data.u.sint = date.tv_usec;
3947 smp->data.type = SMP_T_SINT;
3948 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3949 return 1;
3950}
3951
3952
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003953/* returns the hostname */
3954static int
3955smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3956{
3957 smp->data.type = SMP_T_STR;
3958 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003959 smp->data.u.str.area = hostname;
3960 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003961 return 1;
3962}
3963
Willy Tarreau0f30d262014-11-24 16:02:05 +01003964/* returns the number of processes */
3965static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003966smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003967{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003968 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003969 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003970 return 1;
3971}
3972
3973/* returns the number of the current process (between 1 and nbproc */
3974static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003975smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003976{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003977 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003978 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003979 return 1;
3980}
3981
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003982/* returns the number of the current thread (between 1 and nbthread */
3983static int
3984smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3985{
3986 smp->data.type = SMP_T_SINT;
3987 smp->data.u.sint = tid;
3988 return 1;
3989}
3990
Willy Tarreau84310e22014-02-14 11:59:04 +01003991/* generate a random 32-bit integer for whatever purpose, with an optional
3992 * range specified in argument.
3993 */
3994static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003995smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01003996{
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003997 smp->data.u.sint = ha_random32();
Willy Tarreau84310e22014-02-14 11:59:04 +01003998
3999 /* reduce if needed. Don't do a modulo, use all bits! */
Christopher Faulete6e7a582021-01-29 11:29:28 +01004000 if (args[0].type == ARGT_SINT)
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01004001 smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32;
Willy Tarreau84310e22014-02-14 11:59:04 +01004002
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004003 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01004004 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4005 return 1;
4006}
4007
Willy Tarreau0f30d262014-11-24 16:02:05 +01004008/* returns true if the current process is stopping */
4009static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004010smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01004011{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004012 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004013 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01004014 return 1;
4015}
4016
Willy Tarreau70fe9442018-11-22 16:07:39 +01004017/* returns the number of calls of the current stream's process_stream() */
4018static int
4019smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
4020{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004021 if (!smp->strm)
4022 return 0;
4023
Willy Tarreau70fe9442018-11-22 16:07:39 +01004024 smp->data.type = SMP_T_SINT;
4025 smp->data.u.sint = smp->strm->task->calls;
4026 return 1;
4027}
4028
4029/* returns the average number of nanoseconds spent processing the stream per call */
4030static int
4031smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
4032{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004033 if (!smp->strm)
4034 return 0;
4035
Willy Tarreau70fe9442018-11-22 16:07:39 +01004036 smp->data.type = SMP_T_SINT;
4037 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
4038 return 1;
4039}
4040
4041/* returns the total number of nanoseconds spent processing the stream */
4042static int
4043smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
4044{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004045 if (!smp->strm)
4046 return 0;
4047
Willy Tarreau70fe9442018-11-22 16:07:39 +01004048 smp->data.type = SMP_T_SINT;
4049 smp->data.u.sint = smp->strm->task->cpu_time;
4050 return 1;
4051}
4052
4053/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
4054static int
4055smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
4056{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004057 if (!smp->strm)
4058 return 0;
4059
Willy Tarreau70fe9442018-11-22 16:07:39 +01004060 smp->data.type = SMP_T_SINT;
4061 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
4062 return 1;
4063}
4064
4065/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
4066static int
4067smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
4068{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004069 if (!smp->strm)
4070 return 0;
4071
Willy Tarreau70fe9442018-11-22 16:07:39 +01004072 smp->data.type = SMP_T_SINT;
4073 smp->data.u.sint = smp->strm->task->lat_time;
4074 return 1;
4075}
4076
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004077static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
4078{
4079 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004080 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004081 smp->data.u.str.area = args[0].data.str.area;
4082 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004083 return 1;
4084}
4085
4086static int smp_check_const_bool(struct arg *args, char **err)
4087{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004088 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
4089 strcasecmp(args[0].data.str.area, "1") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004090 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004091 args[0].type = ARGT_SINT;
4092 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004093 return 1;
4094 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004095 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
4096 strcasecmp(args[0].data.str.area, "0") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004097 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004098 args[0].type = ARGT_SINT;
4099 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004100 return 1;
4101 }
4102 memprintf(err, "Expects 'true', 'false', '0' or '1'");
4103 return 0;
4104}
4105
4106static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
4107{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004108 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004109 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004110 return 1;
4111}
4112
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02004113static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004114{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004115 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004116 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004117 return 1;
4118}
4119
4120static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
4121{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004122 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004123 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004124 return 1;
4125}
4126
4127static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
4128{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004129 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004130 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004131 return 1;
4132}
4133
4134static int smp_check_const_bin(struct arg *args, char **err)
4135{
David Carlier64a16ab2016-04-08 10:37:02 +01004136 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004137 int binstrlen;
4138
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004139 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004140 return 0;
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004141 chunk_destroy(&args[0].data.str);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004142 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004143 args[0].data.str.area = binstr;
4144 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004145 return 1;
4146}
4147
4148static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
4149{
4150 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004151 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004152 smp->data.u.str.area = args[0].data.str.area;
4153 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004154 return 1;
4155}
4156
4157static int smp_check_const_meth(struct arg *args, char **err)
4158{
4159 enum http_meth_t meth;
4160 int i;
4161
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004162 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004163 if (meth != HTTP_METH_OTHER) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004164 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004165 args[0].type = ARGT_SINT;
4166 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004167 } else {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05004168 /* Check method avalaibility. A method is a token defined as :
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004169 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
4170 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
4171 * token = 1*tchar
4172 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004173 for (i = 0; i < args[0].data.str.data; i++) {
4174 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004175 memprintf(err, "expects valid method.");
4176 return 0;
4177 }
4178 }
4179 }
4180 return 1;
4181}
4182
4183static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
4184{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004185 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004186 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004187 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004188 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004189 smp->data.u.meth.str.area = "";
4190 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004191 } else {
4192 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004193 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004194 smp->data.u.meth.str.area = args[0].data.str.area;
4195 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004196 }
4197 return 1;
4198}
4199
Luca Schimweg8a694b82019-09-10 15:42:52 +02004200// This function checks the "uuid" sample's arguments.
4201// Function won't get called when no parameter is specified (maybe a bug?)
4202static int smp_check_uuid(struct arg *args, char **err)
4203{
4204 if (!args[0].type) {
4205 args[0].type = ARGT_SINT;
4206 args[0].data.sint = 4;
4207 }
4208 else if (args[0].data.sint != 4) {
4209 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
4210 return 0;
4211 }
4212
4213 return 1;
4214}
4215
4216// Generate a RFC4122 UUID (default is v4 = fully random)
4217static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
4218{
4219 if (args[0].data.sint == 4 || !args[0].type) {
Willy Tarreauee3bcdd2020-03-08 17:48:17 +01004220 ha_generate_uuid(&trash);
Luca Schimweg8a694b82019-09-10 15:42:52 +02004221 smp->data.type = SMP_T_STR;
4222 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4223 smp->data.u.str = trash;
4224 return 1;
4225 }
4226
4227 // more implementations of other uuid formats possible here
4228 return 0;
4229}
4230
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004231/* Note: must not be declared <const> as its list will be overwritten.
4232 * Note: fetches that may return multiple types must be declared as the lowest
4233 * common denominator, the type that can be casted into all other ones. For
4234 * instance IPv4/IPv6 must be declared IPv4.
4235 */
4236static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau0209c972021-03-26 12:03:11 +01004237 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4238 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4239 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_CONST },
4240 { "date", smp_fetch_date, ARG2(0,SINT,STR), smp_check_date_unit, SMP_T_SINT, SMP_USE_CONST },
4241 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4242 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_CONST },
4243 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_CONST },
4244 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4245 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4246 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_CONST },
Willy Tarreau0f30d262014-11-24 16:02:05 +01004247 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau0209c972021-03-26 12:03:11 +01004248 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_CONST },
Willy Tarreau70fe9442018-11-22 16:07:39 +01004249
4250 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4251 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4252 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4253 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4254 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004255
Willy Tarreau0209c972021-03-26 12:03:11 +01004256 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_CONST },
4257 { "bool", smp_fetch_const_bool, ARG1(1,STR), smp_check_const_bool, SMP_T_BOOL, SMP_USE_CONST },
4258 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_CONST },
4259 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_CONST },
4260 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_CONST },
4261 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_CONST },
4262 { "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 +02004263
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004264 { /* END */ },
4265}};
4266
Willy Tarreau0108d902018-11-25 19:14:37 +01004267INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
4268
Emeric Brun107ca302010-01-04 16:16:05 +01004269/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02004270static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Moemen MHEDHBI848216f2021-04-02 01:05:07 +02004271 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
4272 { "b64dec", sample_conv_base642bin, 0, NULL, SMP_T_STR, SMP_T_BIN },
4273 { "base64", sample_conv_bin2base64, 0, NULL, SMP_T_BIN, SMP_T_STR },
4274 { "ub64enc", sample_conv_bin2base64url,0, NULL, SMP_T_BIN, SMP_T_STR },
4275 { "ub64dec", sample_conv_base64url2bin,0, NULL, SMP_T_STR, SMP_T_BIN },
4276 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
4277 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
4278 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
4279 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
4280 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
4281 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
4282 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4283 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4284 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4285 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4286 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4287 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4288 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4289 { "xxh3", sample_conv_xxh3, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4290 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4291 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4292 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
4293 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
4294 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4295 { "word", sample_conv_word, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4296 { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
4297 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02004298#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01004299 { "sha2", sample_conv_sha2, ARG1(0, SINT), smp_check_sha2, SMP_T_BIN, SMP_T_BIN },
Ilya Shipitsin2c481d02021-03-26 23:35:31 +05004300#ifdef EVP_CIPH_GCM_MODE
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02004301 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
4302#endif
Patrick Gansterer8e366512020-04-22 16:47:57 +02004303 { "digest", sample_conv_crypto_digest, ARG1(1,STR), check_crypto_digest, SMP_T_BIN, SMP_T_BIN },
4304 { "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 +02004305#endif
Willy Tarreau280f42b2018-02-19 15:34:12 +01004306 { "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 +02004307 { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT },
Willy Tarreau2a8a2f02021-05-09 11:59:49 +02004308#if defined(HAVE_CRYPTO_memcmp)
Tim Duesterhusf38175c2020-06-09 11:48:42 +02004309 { "secure_memcmp", sample_conv_secure_memcmp, ARG1(1,STR), smp_check_secure_memcmp, SMP_T_BIN, SMP_T_BOOL },
4310#endif
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01004311
4312 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01004313 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
4314 { "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 +01004315
Baptiste Assmanne138dda2020-10-22 15:39:03 +02004316 /* FIX converters */
4317 { "fix_is_valid", sample_conv_fix_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4318 { "fix_tag_value", sample_conv_fix_tag_value, ARG1(1,STR), sample_conv_fix_value_check, SMP_T_BIN, SMP_T_BIN },
4319
Baptiste Assmanne279ca62020-10-27 18:10:06 +01004320 /* MQTT converters */
4321 { "mqtt_is_valid", sample_conv_mqtt_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4322 { "mqtt_field_value", sample_conv_mqtt_field_value, ARG2(2,STR,STR), sample_conv_mqtt_field_value_check, SMP_T_BIN, SMP_T_STR },
4323
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02004324 { "iif", sample_conv_iif, ARG2(2, STR, STR), NULL, SMP_T_BOOL, SMP_T_STR },
4325
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02004326 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4327 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4328 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4329 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
4330 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4331 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4332 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4333 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4334 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4335 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4336 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4337 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4338 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4339 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01004340
Christopher Fauletea159d62020-04-01 16:21:44 +02004341 { "htonl", sample_conv_htonl, 0, NULL, SMP_T_SINT, SMP_T_BIN },
4342 { "cut_crlf", sample_conv_cut_crlf, 0, NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet51fc9d12020-04-01 17:24:41 +02004343 { "ltrim", sample_conv_ltrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet568415a2020-04-01 17:24:47 +02004344 { "rtrim", sample_conv_rtrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Alex51c8ad42021-04-15 16:45:15 +02004345 { "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 +02004346 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01004347}};
4348
Willy Tarreau0108d902018-11-25 19:14:37 +01004349INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);