blob: 3c312184a84bacbc99060f5dcf7201117e566786 [file] [log] [blame]
Emeric Brun107ca302010-01-04 16:16:05 +01001/*
Willy Tarreaucd3b0942012-04-27 21:52:18 +02002 * Sample management functions.
Emeric Brun107ca302010-01-04 16:16:05 +01003 *
4 * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreaucd3b0942012-04-27 21:52:18 +02005 * Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
Emeric Brun107ca302010-01-04 16:16:05 +01006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Thierry FOURNIER317e1c42014-08-12 10:20:47 +020014#include <ctype.h>
Emeric Brun107ca302010-01-04 16:16:05 +010015#include <string.h>
16#include <arpa/inet.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020017#include <stdio.h>
Emeric Brun107ca302010-01-04 16:16:05 +010018
Alex51c8ad42021-04-15 16:45:15 +020019#include <import/mjson.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <import/sha1.h>
21#include <import/xxhash.h>
Willy Tarreau7e2c6472012-10-29 20:44:36 +010022
Willy Tarreaub2551052020-06-09 09:07:15 +020023#include <haproxy/api.h>
24#include <haproxy/arg.h>
Willy Tarreauac13aea2020-06-04 10:36:03 +020025#include <haproxy/auth.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/base64.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020027#include <haproxy/buf.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020028#include <haproxy/chunk.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020029#include <haproxy/errors.h>
Baptiste Assmanne138dda2020-10-22 15:39:03 +020030#include <haproxy/fix.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020031#include <haproxy/global.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020032#include <haproxy/hash.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020033#include <haproxy/http.h>
Baptiste Assmanne138dda2020-10-22 15:39:03 +020034#include <haproxy/istbuf.h>
Baptiste Assmanne279ca62020-10-27 18:10:06 +010035#include <haproxy/mqtt.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020036#include <haproxy/net_helper.h>
Willy Tarreaub23e5952020-06-04 16:06:59 +020037#include <haproxy/protobuf.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020038#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020039#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020040#include <haproxy/sample.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020041#include <haproxy/sink.h>
42#include <haproxy/stick_table.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020043#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020044#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020045#include <haproxy/vars.h>
Thierry FOURNIER01e09742016-12-26 11:46:11 +010046
Willy Tarreau1cf8f082014-02-07 12:14:54 +010047/* sample type names */
48const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020049 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010050 [SMP_T_BOOL] = "bool",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010051 [SMP_T_SINT] = "sint",
52 [SMP_T_ADDR] = "addr",
53 [SMP_T_IPV4] = "ipv4",
54 [SMP_T_IPV6] = "ipv6",
55 [SMP_T_STR] = "str",
56 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020057 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010058};
59
Willy Tarreau12785782012-04-27 21:37:17 +020060/* static sample used in sample_process() when <p> is NULL */
Emeric Brune5c918b2017-06-14 14:15:36 +020061static THREAD_LOCAL struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010062
Willy Tarreau12785782012-04-27 21:37:17 +020063/* list head of all known sample fetch keywords */
64static struct sample_fetch_kw_list sample_fetches = {
65 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010066};
67
Willy Tarreau12785782012-04-27 21:37:17 +020068/* list head of all known sample format conversion keywords */
69static struct sample_conv_kw_list sample_convs = {
70 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010071};
72
Willy Tarreau80aca902013-01-07 15:42:20 +010073const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
Willy Tarreaube2159b2021-03-26 11:56:11 +010074 [SMP_SRC_CONST] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
75 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
76 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
77 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
78 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
79 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +010080 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL_CFG_PARSER |
81 SMP_VAL_CLI_PARSER ),
Willy Tarreaube2159b2021-03-26 11:56:11 +010082
Willy Tarreau80aca902013-01-07 15:42:20 +010083 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
84 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
85 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
86 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
87 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
88 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +010089 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
90 SMP_VAL_CLI_PARSER ),
Willy Tarreau80aca902013-01-07 15:42:20 +010091
92 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
93 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
94 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
95 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
96 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
97 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +010098 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
99 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100100
101 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
102 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
103 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
104 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
105 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
106 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100107 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
108 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100109
110 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
111 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
112 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
113 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
114 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
115 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100116 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
117 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100118
119 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
120 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
121 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
122 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
123 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
124 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100125 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
126 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100127
128 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
129 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
130 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
131 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
132 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
133 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100134 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
135 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100136
137 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
138 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
139 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
140 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
141 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
142 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100143 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
144 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100145
146 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
147 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
148 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
149 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
150 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
151 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100152 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
153 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100154
155 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
156 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
157 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
158 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
159 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
160 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100161 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
162 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100163
164 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
165 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
166 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
167 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
168 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
169 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100170 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
171 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100172
173 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
174 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
175 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
176 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
177 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
178 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100179 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
180 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100181
182 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
183 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
184 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
185 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
186 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
187 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100188 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
189 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100190
191 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
192 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
193 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
194 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
195 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
196 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100197 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
198 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100199
200 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
201 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
202 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
203 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
204 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
205 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100206 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
207 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100208
209 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
210 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
211 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
212 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
213 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
214 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100215 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
216 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100217
218 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
219 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
220 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
221 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
222 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
223 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100224 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
225 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100226
227 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
228 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
229 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
230 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
231 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
232 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100233 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
234 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100235
236 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
237 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
238 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
239 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
240 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
241 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100242 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ |
243 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100244
245 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
246 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
247 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
248 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
249 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
250 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100251 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
252 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100253
254 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
255 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
256 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
257 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
258 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
259 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100260 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
261 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100262
263 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
264 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
265 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
266 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
267 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
268 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100269 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
270 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100271
272 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
273 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
274 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
275 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
276 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
277 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreaudb5e0db2021-03-26 15:29:35 +0100278 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ |
279 SMP_VAL___________ ),
Willy Tarreau80aca902013-01-07 15:42:20 +0100280};
281
282static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
283 [SMP_SRC_INTRN] = "internal state",
284 [SMP_SRC_LISTN] = "listener",
285 [SMP_SRC_FTEND] = "frontend",
286 [SMP_SRC_L4CLI] = "client address",
287 [SMP_SRC_L5CLI] = "client-side connection",
288 [SMP_SRC_TRACK] = "track counters",
289 [SMP_SRC_L6REQ] = "request buffer",
290 [SMP_SRC_HRQHV] = "HTTP request headers",
291 [SMP_SRC_HRQHP] = "HTTP request",
292 [SMP_SRC_HRQBO] = "HTTP request body",
293 [SMP_SRC_BKEND] = "backend",
294 [SMP_SRC_SERVR] = "server",
295 [SMP_SRC_L4SRV] = "server address",
296 [SMP_SRC_L5SRV] = "server-side connection",
297 [SMP_SRC_L6RES] = "response buffer",
298 [SMP_SRC_HRSHV] = "HTTP response headers",
299 [SMP_SRC_HRSHP] = "HTTP response",
300 [SMP_SRC_HRSBO] = "HTTP response body",
301 [SMP_SRC_RQFIN] = "request buffer statistics",
302 [SMP_SRC_RSFIN] = "response buffer statistics",
303 [SMP_SRC_TXFIN] = "transaction statistics",
304 [SMP_SRC_SSFIN] = "session statistics",
305};
306
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100307static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
308 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
309 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
310 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
311 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
312 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
313 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
314 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
315 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
316 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
317 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
318 [SMP_CKP_BE_SRV_CON] = "server source selection",
319 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
320 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
321 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
322 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
323 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
324 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
325 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
326 [SMP_CKP_FE_LOG_END] = "logs",
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100327 [SMP_CKP_BE_CHK_RUL] = "backend tcp-check rule",
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100328};
329
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100330/* This function returns the type of the data returned by the sample_expr.
331 * It assumes that the <expr> and all of its converters are properly
332 * initialized.
333 */
334inline
335int smp_expr_output_type(struct sample_expr *expr)
336{
337 struct sample_conv_expr *smp_expr;
338
339 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
340 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
341 return smp_expr->conv->out_type;
342 }
343 return expr->fetch->out_type;
344}
345
346
Willy Tarreau80aca902013-01-07 15:42:20 +0100347/* fill the trash with a comma-delimited list of source names for the <use> bit
348 * field which must be composed of a non-null set of SMP_USE_* flags. The return
349 * value is the pointer to the string in the trash buffer.
350 */
351const char *sample_src_names(unsigned int use)
352{
353 int bit;
354
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200355 trash.data = 0;
356 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100357 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
358 if (!(use & ~((1 << bit) - 1)))
359 break; /* no more bits */
360
361 if (!(use & (1 << bit)))
362 continue; /* bit not set */
363
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200364 trash.data += snprintf(trash.area + trash.data,
365 trash.size - trash.data, "%s%s",
366 (use & ((1 << bit) - 1)) ? "," : "",
367 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100368 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200369 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100370}
371
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100372/* return a pointer to the correct sample checkpoint name, or "unknown" when
373 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
374 * if set.
375 */
376const char *sample_ckp_names(unsigned int use)
377{
378 int bit;
379
380 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
381 if (use & (1 << bit))
382 return fetch_ckp_names[bit];
383 return "unknown sample check place, please report this bug";
384}
385
Emeric Brun107ca302010-01-04 16:16:05 +0100386/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100387 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
388 * for next parsing sessions. The fetch keywords capabilities are also computed
389 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100390 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100391void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100392{
Willy Tarreau80aca902013-01-07 15:42:20 +0100393 struct sample_fetch *sf;
394 int bit;
395
396 for (sf = kwl->kw; sf->kw != NULL; sf++) {
397 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
398 if (sf->use & (1 << bit))
399 sf->val |= fetch_cap[bit];
400 }
Willy Tarreau2b718102021-04-21 07:32:39 +0200401 LIST_APPEND(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100402}
403
404/*
Willy Tarreau12785782012-04-27 21:37:17 +0200405 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100406 * parsing sessions.
407 */
Willy Tarreau12785782012-04-27 21:37:17 +0200408void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100409{
Willy Tarreau2b718102021-04-21 07:32:39 +0200410 LIST_APPEND(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100411}
412
413/*
Willy Tarreau12785782012-04-27 21:37:17 +0200414 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100415 * string of <len> in buffer <kw>.
416 *
417 */
Willy Tarreau12785782012-04-27 21:37:17 +0200418struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100419{
420 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200421 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100422
Willy Tarreau12785782012-04-27 21:37:17 +0200423 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100424 for (index = 0; kwl->kw[index].kw != NULL; index++) {
425 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
426 kwl->kw[index].kw[len] == '\0')
427 return &kwl->kw[index];
428 }
429 }
430 return NULL;
431}
432
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100433/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100434 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100435 * <idx> is the index of the next sample fetch entry. It is used as private
436 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100437 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100438 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100439 * the list is reached.
440 */
441struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
442{
443 struct sample_fetch_kw_list *kwl;
444 struct sample_fetch *base;
445
446 if (!current) {
447 /* Get first kwl entry. */
448 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
449 (*idx) = 0;
450 } else {
451 /* Get kwl corresponding to the curret entry. */
452 base = current + 1 - (*idx);
453 kwl = container_of(base, struct sample_fetch_kw_list, kw);
454 }
455
456 while (1) {
457
458 /* Check if kwl is the last entry. */
459 if (&kwl->list == &sample_fetches.list)
460 return NULL;
461
462 /* idx contain the next keyword. If it is available, return it. */
463 if (kwl->kw[*idx].kw) {
464 (*idx)++;
465 return &kwl->kw[(*idx)-1];
466 }
467
468 /* get next entry in the main list, and return NULL if the end is reached. */
469 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
470
471 /* Set index to 0, ans do one other loop. */
472 (*idx) = 0;
473 }
474}
475
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100476/* This function browses the list of available converters. <current> is
477 * the last used converter. If it is the first call, it must set to NULL.
478 * <idx> is the index of the next converter entry. It is used as private
479 * value. It is useless to initiate it.
480 *
481 * It returns always the next sample_conv entry, and NULL when the end of
482 * the list is reached.
483 */
484struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
485{
486 struct sample_conv_kw_list *kwl;
487 struct sample_conv *base;
488
489 if (!current) {
490 /* Get first kwl entry. */
491 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
492 (*idx) = 0;
493 } else {
494 /* Get kwl corresponding to the curret entry. */
495 base = current + 1 - (*idx);
496 kwl = container_of(base, struct sample_conv_kw_list, kw);
497 }
498
499 while (1) {
500 /* Check if kwl is the last entry. */
501 if (&kwl->list == &sample_convs.list)
502 return NULL;
503
504 /* idx contain the next keyword. If it is available, return it. */
505 if (kwl->kw[*idx].kw) {
506 (*idx)++;
507 return &kwl->kw[(*idx)-1];
508 }
509
510 /* get next entry in the main list, and return NULL if the end is reached. */
511 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
512
513 /* Set index to 0, ans do one other loop. */
514 (*idx) = 0;
515 }
516}
517
Emeric Brun107ca302010-01-04 16:16:05 +0100518/*
Willy Tarreau12785782012-04-27 21:37:17 +0200519 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100520 * string of <len> in buffer <kw>.
521 *
522 */
Willy Tarreau12785782012-04-27 21:37:17 +0200523struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100524{
525 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200526 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100527
Willy Tarreau12785782012-04-27 21:37:17 +0200528 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100529 for (index = 0; kwl->kw[index].kw != NULL; index++) {
530 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
531 kwl->kw[index].kw[len] == '\0')
532 return &kwl->kw[index];
533 }
534 }
535 return NULL;
536}
537
Emeric Brun107ca302010-01-04 16:16:05 +0100538/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200539/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100540/******************************************************************/
541
Willy Tarreau342acb42012-04-23 22:03:39 +0200542static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100543{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200544 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200545 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100546 return 1;
547}
548
Willy Tarreau342acb42012-04-23 22:03:39 +0200549static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100550{
Willy Tarreau83061a82018-07-13 11:56:34 +0200551 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100552
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200553 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100554 return 0;
555
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200556 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200557 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200558 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100559 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100560
561 return 1;
562}
563
Willy Tarreau342acb42012-04-23 22:03:39 +0200564static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100565{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200566 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200567 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100568 return 1;
569}
570
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200571static int c_ipv62ip(struct sample *smp)
572{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200573 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200574 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100575 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200576 return 1;
577}
578
Willy Tarreau342acb42012-04-23 22:03:39 +0200579static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100580{
Willy Tarreau83061a82018-07-13 11:56:34 +0200581 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100582
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200583 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100584 return 0;
585
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200586 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200587 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200588 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100589 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100590 return 1;
591}
592
593/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200594static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100595{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200596 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100597}
598*/
599
Willy Tarreau342acb42012-04-23 22:03:39 +0200600static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100601{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200602 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200603 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100604 return 1;
605}
606
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200607static int c_int2ipv6(struct sample *smp)
608{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200609 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
610 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200611 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200612 return 1;
613}
614
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100615static int c_str2addr(struct sample *smp)
616{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200617 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
618 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100619 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200620 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100621 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100622 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100623 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200624 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100625 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100626 return 1;
627}
628
Willy Tarreau342acb42012-04-23 22:03:39 +0200629static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100630{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200631 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100632 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200633 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100634 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100635 return 1;
636}
637
Willy Tarreau342acb42012-04-23 22:03:39 +0200638static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100639{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200640 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100641 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200642 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100643 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100644 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100645}
646
Emeric Brun4b9e8022014-11-03 18:17:10 +0100647/*
648 * The NULL char always enforces the end of string if it is met.
649 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100650 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200651static int c_bin2str(struct sample *smp)
652{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100653 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200654
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200655 for (i = 0; i < smp->data.u.str.data; i++) {
656 if (!smp->data.u.str.area[i]) {
657 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100658 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100659 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200660 }
Christopher Faulet472ad512020-04-30 09:57:40 +0200661 smp->data.type = SMP_T_STR;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200662 return 1;
663}
664
Willy Tarreau342acb42012-04-23 22:03:39 +0200665static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100666{
Willy Tarreau83061a82018-07-13 11:56:34 +0200667 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100668 char *pos;
669
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200670 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100671 if (!pos)
672 return 0;
673
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200674 trash->size = trash->size - (pos - trash->area);
675 trash->area = pos;
676 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200677 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200678 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100679 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100680 return 1;
681}
682
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800683/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200684 * For strings and binary blocks, it also provides a known allocated size with
685 * a length that is capped to the size, and ensures a trailing zero is always
686 * appended for strings. This is necessary for some operations which may
687 * require to extend the length. It returns 0 if it fails, 1 on success.
688 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100689int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200690{
Willy Tarreau83061a82018-07-13 11:56:34 +0200691 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200692
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200693 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100694 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100695 case SMP_T_SINT:
696 case SMP_T_ADDR:
697 case SMP_T_IPV4:
698 case SMP_T_IPV6:
699 /* These type are not const. */
700 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200701
Christopher Fauletec100512017-07-24 15:38:41 +0200702 case SMP_T_METH:
703 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
704 break;
705 /* Fall through */
706
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100707 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200708 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100709 trash->data = smp->data.type == SMP_T_STR ?
710 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200711 if (trash->data > trash->size - 1)
712 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200713
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100714 memcpy(trash->area, smp->data.type == SMP_T_STR ?
715 smp->data.u.str.area : smp->data.u.meth.str.area,
716 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200717 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200718 smp->data.u.str = *trash;
719 break;
720
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100721 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100722 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200723 trash->data = smp->data.u.str.data;
724 if (trash->data > trash->size)
725 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200726
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200727 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200728 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100729 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200730
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100731 default:
732 /* Other cases are unexpected. */
733 return 0;
734 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100735
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100736 /* remove const flag */
737 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200738 return 1;
739}
740
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100741int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100742{
743 return 1;
744}
745
Willy Tarreau342acb42012-04-23 22:03:39 +0200746static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100747{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200748 const char *str;
749 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100750
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200751 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100752 return 0;
753
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200754 str = smp->data.u.str.area;
755 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100756
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200757 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200758 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100759 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100760 return 1;
761}
762
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100763static int c_str2meth(struct sample *smp)
764{
765 enum http_meth_t meth;
766 int len;
767
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200768 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100769 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200770 len = smp->data.u.str.data;
771 smp->data.u.meth.str.area = smp->data.u.str.area;
772 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100773 }
774 else
775 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200776 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200777 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100778 return 1;
779}
780
781static int c_meth2str(struct sample *smp)
782{
783 int len;
784 enum http_meth_t meth;
785
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200786 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100787 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200788 len = smp->data.u.meth.str.data;
789 smp->data.u.str.area = smp->data.u.meth.str.area;
790 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200791 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100792 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200793 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100794 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200795 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200796 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200797 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100798 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200799 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100800 }
801 else {
802 /* Unknown method */
803 return 0;
804 }
805 return 1;
806}
807
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200808static int c_addr2bin(struct sample *smp)
809{
Willy Tarreau83061a82018-07-13 11:56:34 +0200810 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200811
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200812 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200813 chk->data = 4;
814 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200815 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200816 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200817 chk->data = 16;
818 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200819 }
820 else
821 return 0;
822
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200823 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200824 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200825 return 1;
826}
827
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200828static int c_int2bin(struct sample *smp)
829{
Willy Tarreau83061a82018-07-13 11:56:34 +0200830 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200831
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200832 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
833 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200834
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200835 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200836 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200837 return 1;
838}
839
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200840
Emeric Brun107ca302010-01-04 16:16:05 +0100841/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200842/* Sample casts matrix: */
843/* sample_casts[from type][to type] */
844/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100845/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100846
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100847sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200848/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
849/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
850/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200851/* SINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, c_int2ipv6, c_int2str, c_int2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200852/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
853/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200854/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200855/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
856/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
857/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200858};
Emeric Brun107ca302010-01-04 16:16:05 +0100859
Emeric Brun107ca302010-01-04 16:16:05 +0100860/*
Willy Tarreau12785782012-04-27 21:37:17 +0200861 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100862 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200863 * Returns a pointer on allocated sample expression structure.
Christopher Faulet45bb15f2021-09-30 08:48:56 +0200864 * <al> is an arg_list serving as a list head to report missing dependencies.
865 * It may be NULL if such dependencies are not allowed. Otherwise, the caller
866 * must have set al->ctx if al is set.
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100867 * If <endptr> is non-nul, it will be set to the first unparsed character
868 * (which may be the final '\0') on success. If it is nul, the expression
869 * must be properly terminated by a '\0' otherwise an error is reported.
Emeric Brun107ca302010-01-04 16:16:05 +0100870 */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100871struct 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 +0100872{
Willy Tarreau833cc792013-07-24 15:34:19 +0200873 const char *begw; /* beginning of word */
874 const char *endw; /* end of word */
875 const char *endt; /* end of term */
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +0100876 struct sample_expr *expr = NULL;
Willy Tarreau12785782012-04-27 21:37:17 +0200877 struct sample_fetch *fetch;
878 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100879 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200880 char *fkw = NULL;
881 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100882 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100883
Willy Tarreau833cc792013-07-24 15:34:19 +0200884 begw = str[*idx];
Willy Tarreaued2c6622020-02-14 18:27:10 +0100885 for (endw = begw; is_idchar(*endw); endw++)
886 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200887
888 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100889 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100890 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200891 }
Emeric Brun107ca302010-01-04 16:16:05 +0100892
Willy Tarreau833cc792013-07-24 15:34:19 +0200893 /* keep a copy of the current fetch keyword for error reporting */
894 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100895
Willy Tarreau833cc792013-07-24 15:34:19 +0200896 fetch = find_sample_fetch(begw, endw - begw);
897 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100898 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100899 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200900 }
Emeric Brun107ca302010-01-04 16:16:05 +0100901
Willy Tarreau833cc792013-07-24 15:34:19 +0200902 /* At this point, we have :
903 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100904 * - endw : end of the keyword, first character not part of keyword
Willy Tarreau833cc792013-07-24 15:34:19 +0200905 */
906
907 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100908 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100909 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200910 }
Emeric Brun107ca302010-01-04 16:16:05 +0100911 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200912
Vincent Bernat02779b62016-04-03 13:48:43 +0200913 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200914 if (!expr)
915 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100916
917 LIST_INIT(&(expr->conv_exprs));
918 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200919 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100920
Willy Tarreau689a1df2013-12-13 00:40:11 +0100921 /* Note that we call the argument parser even with an empty string,
922 * this allows it to automatically create entries for mandatory
923 * implicit arguments (eg: local proxy name).
924 */
Christopher Faulet45bb15f2021-09-30 08:48:56 +0200925 if (al) {
926 al->kw = expr->fetch->kw;
927 al->conv = NULL;
928 }
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100929 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 +0100930 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
931 goto out_error;
932 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200933
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100934 /* now endt is our first char not part of the arg list, typically the
935 * comma after the sample fetch name or after the closing parenthesis,
936 * or the NUL char.
937 */
938
Willy Tarreau689a1df2013-12-13 00:40:11 +0100939 if (!expr->arg_p) {
940 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200941 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100942 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
943 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200944 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100945 }
946
Willy Tarreau833cc792013-07-24 15:34:19 +0200947 /* Now process the converters if any. We have two supported syntaxes
948 * for the converters, which can be combined :
949 * - comma-delimited list of converters just after the keyword and args ;
950 * - one converter per keyword
951 * The combination allows to have each keyword being a comma-delimited
952 * series of converters.
953 *
954 * We want to process the former first, then the latter. For this we start
955 * from the beginning of the supposed place in the exiting conv chain, which
956 * starts at the last comma (endt).
957 */
958
959 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200960 struct sample_conv_expr *conv_expr;
Willy Tarreau46dfd782019-12-17 10:25:29 +0100961 int err_arg;
962 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +0100963
Willy Tarreau833cc792013-07-24 15:34:19 +0200964 if (*endt && *endt != ',') {
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100965 if (endptr) {
966 /* end found, let's stop here */
967 break;
968 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200969 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100970 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200971 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100972 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100973 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200974 }
Emeric Brun107ca302010-01-04 16:16:05 +0100975
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100976 /* FIXME: how long should we support such idiocies ? Maybe we
977 * should already warn ?
978 */
Willy Tarreau833cc792013-07-24 15:34:19 +0200979 while (*endt == ',') /* then trailing commas */
980 endt++;
981
Willy Tarreau97108e02016-11-25 07:33:24 +0100982 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200983
984 if (!*begw) {
985 /* none ? skip to next string */
986 (*idx)++;
987 begw = str[*idx];
988 if (!begw || !*begw)
989 break;
990 }
991
Willy Tarreaued2c6622020-02-14 18:27:10 +0100992 for (endw = begw; is_idchar(*endw); endw++)
993 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200994
995 free(ckw);
996 ckw = my_strndup(begw, endw - begw);
997
998 conv = find_sample_conv(begw, endw - begw);
999 if (!conv) {
1000 /* we found an isolated keyword that we don't know, it's not ours */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001001 if (begw == str[*idx]) {
1002 endt = begw;
Willy Tarreau833cc792013-07-24 15:34:19 +02001003 break;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001004 }
Willy Tarreau97108e02016-11-25 07:33:24 +01001005 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +02001006 goto out_error;
1007 }
Emeric Brun107ca302010-01-04 16:16:05 +01001008
Willy Tarreau833cc792013-07-24 15:34:19 +02001009 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001010 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001011 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001012 }
Emeric Brun107ca302010-01-04 16:16:05 +01001013
1014 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +02001015 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001016 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001017 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +02001018 }
Emeric Brun107ca302010-01-04 16:16:05 +01001019
1020 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +02001021 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +02001022 if (!conv_expr)
1023 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001024
Willy Tarreau2b718102021-04-21 07:32:39 +02001025 LIST_APPEND(&(expr->conv_exprs), &(conv_expr->list));
Emeric Brun107ca302010-01-04 16:16:05 +01001026 conv_expr->conv = conv;
1027
Christopher Faulet45bb15f2021-09-30 08:48:56 +02001028 if (al) {
1029 al->kw = expr->fetch->kw;
1030 al->conv = conv_expr->conv->kw;
1031 }
Willy Tarreau80b53ff2020-02-14 08:40:37 +01001032 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 +01001033 if (argcnt < 0) {
1034 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
1035 goto out_error;
1036 }
Willy Tarreau9e92d322010-01-26 17:58:06 +01001037
Willy Tarreau46dfd782019-12-17 10:25:29 +01001038 if (argcnt && !conv->arg_mask) {
1039 memprintf(err_msg, "converter '%s' does not support any args", ckw);
1040 goto out_error;
1041 }
Willy Tarreau21d68a62012-04-20 15:52:36 +02001042
Willy Tarreau46dfd782019-12-17 10:25:29 +01001043 if (!conv_expr->arg_p)
1044 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001045
Willy Tarreau46dfd782019-12-17 10:25:29 +01001046 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1047 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001048 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001049 }
1050 }
Emeric Brun485479d2010-09-23 18:02:19 +02001051
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001052 if (endptr) {
1053 /* end found, let's stop here */
1054 *endptr = (char *)endt;
1055 }
1056
Willy Tarreau833cc792013-07-24 15:34:19 +02001057 out:
1058 free(fkw);
1059 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001060 return expr;
1061
1062out_error:
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +01001063 release_sample_expr(expr);
Willy Tarreau833cc792013-07-24 15:34:19 +02001064 expr = NULL;
1065 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001066}
1067
1068/*
Willy Tarreau12785782012-04-27 21:37:17 +02001069 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001070 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001071 * Returns a pointer on a typed sample structure containing the result or NULL if
1072 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001073 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001074 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001075 *
1076 * Note: the fetch functions are required to properly set the return type. The
1077 * conversion functions must do so too. However the cast functions do not need
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001078 * to since they're made to cast multiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001079 *
1080 * The caller may indicate in <opt> if it considers the result final or not.
1081 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1082 * if the result is stable or not, according to the following table :
1083 *
1084 * return MAY_CHANGE FINAL Meaning for the sample
1085 * NULL 0 * Not present and will never be (eg: header)
1086 * NULL 1 0 Not present yet, could change (eg: POST param)
1087 * NULL 1 1 Not present yet, will not change anymore
1088 * smp 0 * Present and will not change (eg: header)
1089 * smp 1 0 Present, may change (eg: request length)
1090 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001091 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001092struct sample *sample_process(struct proxy *px, struct session *sess,
1093 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001094 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001095{
Willy Tarreau12785782012-04-27 21:37:17 +02001096 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001097
Willy Tarreau18387e22013-07-25 12:02:38 +02001098 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001099 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001100 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001101 }
Emeric Brun107ca302010-01-04 16:16:05 +01001102
Willy Tarreau1777ea62016-03-10 16:15:46 +01001103 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001104 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001105 return NULL;
1106
Emeric Brun107ca302010-01-04 16:16:05 +01001107 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001108 /* we want to ensure that p->type can be casted into
1109 * conv_expr->conv->in_type. We have 3 possibilities :
1110 * - NULL => not castable.
1111 * - c_none => nothing to do (let's optimize it)
1112 * - other => apply cast and prepare to fail
1113 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001114 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001115 return NULL;
1116
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001117 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1118 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001119 return NULL;
1120
Willy Tarreau12e50112012-04-25 17:21:49 +02001121 /* OK cast succeeded */
1122
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001123 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001124 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001125 }
1126 return p;
1127}
1128
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001129/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001130 * Resolve all remaining arguments in proxy <p>. Returns the number of
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001131 * errors or 0 if everything is fine. If at least one error is met, it will
1132 * be appended to *err. If *err==NULL it will be allocated first.
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001133 */
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001134int smp_resolve_args(struct proxy *p, char **err)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001135{
1136 struct arg_list *cur, *bak;
1137 const char *ctx, *where;
1138 const char *conv_ctx, *conv_pre, *conv_pos;
1139 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001140 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001141 struct arg *arg;
1142 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001143 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001144
1145 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1146 struct proxy *px;
1147 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001148 struct stktable *t;
1149 char *pname, *sname, *stktname;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001150 char *err2;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001151
1152 arg = cur->arg;
1153
1154 /* prepare output messages */
1155 conv_pre = conv_pos = conv_ctx = "";
1156 if (cur->conv) {
1157 conv_ctx = cur->conv;
1158 conv_pre = "conversion keyword '";
1159 conv_pos = "' for ";
1160 }
1161
1162 where = "in";
1163 ctx = "sample fetch keyword";
1164 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001165 case ARGC_STK: where = "in stick rule in"; break;
1166 case ARGC_TRK: where = "in tracking rule in"; break;
1167 case ARGC_LOG: where = "in log-format string in"; break;
1168 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
Willy Tarreaud9b331c2021-09-02 19:43:20 +02001169 case ARGC_HRQ: where = "in http-request expression in"; break;
1170 case ARGC_HRS: where = "in http-response response in"; break;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001171 case ARGC_UIF: where = "in unique-id-format string in"; break;
1172 case ARGC_RDR: where = "in redirect format string in"; break;
1173 case ARGC_CAP: where = "in capture rule in"; break;
1174 case ARGC_ACL: ctx = "ACL keyword"; break;
1175 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001176 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001177 case ARGC_HERR: where = "in http-error directive in"; break;
Miroslav Zagorac7f8314c2020-12-09 16:31:48 +01001178 case ARGC_OT: where = "in ot-scope directive in"; break;
Willy Tarreaud9b331c2021-09-02 19:43:20 +02001179 case ARGC_TCO: where = "in tcp-request connection expression in"; break;
1180 case ARGC_TSE: where = "in tcp-request session expression in"; break;
1181 case ARGC_TRQ: where = "in tcp-request content expression in"; break;
1182 case ARGC_TRS: where = "in tcp-response content expression in"; break;
1183 case ARGC_TCK: where = "in tcp-check expression in"; break;
1184 case ARGC_CFG: where = "in configuration expression in"; break;
1185 case ARGC_CLI: where = "in CLI expression in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001186 }
1187
1188 /* set a few default settings */
1189 px = p;
1190 pname = p->id;
1191
1192 switch (arg->type) {
1193 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001194 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001195 memprintf(err, "%sparsing [%s:%d]: missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1196 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001197 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001198 cfgerr++;
1199 continue;
1200 }
1201
1202 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001203 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001204
1205 if (sname) {
1206 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001207 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001208
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001209 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001210 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001211 memprintf(err, "%sparsing [%s:%d]: unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1212 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001213 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001214 cfgerr++;
1215 break;
1216 }
1217 }
1218 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001219 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001220
1221 srv = findserver(px, sname);
1222 if (!srv) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001223 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",
1224 *err ? *err : "", cur->file, cur->line, sname, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001225 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001226 cfgerr++;
1227 break;
1228 }
1229
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001230 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001231 arg->unresolved = 0;
1232 arg->data.srv = srv;
1233 break;
1234
1235 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001236 if (arg->data.str.data) {
1237 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001238 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001239 }
1240
1241 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001242 memprintf(err, "%sparsing [%s:%d]: unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1243 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001244 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001245 cfgerr++;
1246 break;
1247 }
1248
1249 if (!(px->cap & PR_CAP_FE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001250 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",
1251 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001252 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001253 cfgerr++;
1254 break;
1255 }
1256
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001257 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001258 arg->unresolved = 0;
1259 arg->data.prx = px;
1260 break;
1261
1262 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001263 if (arg->data.str.data) {
1264 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001265 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001266 }
1267
1268 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001269 memprintf(err, "%sparsing [%s:%d]: unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1270 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001271 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001272 cfgerr++;
1273 break;
1274 }
1275
1276 if (!(px->cap & PR_CAP_BE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001277 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",
1278 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001279 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001280 cfgerr++;
1281 break;
1282 }
1283
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001284 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001285 arg->unresolved = 0;
1286 arg->data.prx = px;
1287 break;
1288
1289 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001290 if (arg->data.str.data)
1291 stktname = arg->data.str.area;
1292 else
1293 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001294
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001295 t = stktable_find_by_name(stktname);
1296 if (!t) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001297 memprintf(err, "%sparsing [%s:%d]: unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1298 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001299 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001300 cfgerr++;
1301 break;
1302 }
1303
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001304 if (!t->size) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001305 memprintf(err, "%sparsing [%s:%d]: no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1306 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001307 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001308 cfgerr++;
1309 break;
1310 }
1311
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001312 if (t->proxy && (p->bind_proc & ~t->proxy->bind_proc)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001313 memprintf(err, "%sparsing [%s:%d]: stick-table '%s' not present on all processes covered by proxy '%s'.\n",
1314 *err ? *err : "", cur->file, cur->line, t->proxy->id, p->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01001315 cfgerr++;
1316 break;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001317 }
1318
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001319 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001320 p->next_stkt_ref = t->proxies_list;
1321 t->proxies_list = p;
1322 }
1323
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001324 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001325 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001326 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001327 break;
1328
1329 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001330 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001331 memprintf(err, "%sparsing [%s:%d]: missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1332 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001333 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001334 cfgerr++;
1335 break;
1336 }
1337
1338 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001339 strcmp(p->uri_auth->userlist->name, arg->data.str.area) == 0)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001340 ul = p->uri_auth->userlist;
1341 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001342 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001343
1344 if (!ul) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001345 memprintf(err, "%sparsing [%s:%d]: unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1346 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001347 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001348 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001349 cfgerr++;
1350 break;
1351 }
1352
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001353 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001354 arg->unresolved = 0;
1355 arg->data.usr = ul;
1356 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001357
1358 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001359 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001360 memprintf(err, "%sparsing [%s:%d]: missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1361 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001362 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001363 cfgerr++;
1364 continue;
1365 }
1366
Willy Tarreau46947782015-01-19 19:00:58 +01001367 rflags = 0;
1368 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001369 err2 = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001370
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001371 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err2))) {
1372 memprintf(err, "%sparsing [%s:%d]: error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1373 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001374 arg->data.str.area,
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001375 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err2);
Willy Tarreau46947782015-01-19 19:00:58 +01001376 cfgerr++;
1377 continue;
1378 }
1379
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001380 chunk_destroy(&arg->data.str);
Willy Tarreau46947782015-01-19 19:00:58 +01001381 arg->unresolved = 0;
1382 arg->data.reg = reg;
1383 break;
1384
1385
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001386 }
1387
Willy Tarreau2b718102021-04-21 07:32:39 +02001388 LIST_DELETE(&cur->list);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001389 free(cur);
1390 } /* end of args processing */
1391
1392 return cfgerr;
1393}
1394
1395/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001396 * Process a fetch + format conversion as defined by the sample expression
1397 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001398 * not explicitly set to <smp_type>, but shall be compatible with it as
1399 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1400 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001401 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1402 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1403 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1404 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001405 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1406 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1407 * still hope to get it after waiting longer, and is not converted to string.
1408 * The possible output combinations are the following :
1409 *
1410 * return MAY_CHANGE FINAL Meaning for the sample
1411 * NULL * * Not present and will never be (eg: header)
1412 * smp 0 * Final value converted (eg: header)
1413 * smp 1 0 Not present yet, may appear later (eg: header)
1414 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001415 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001416struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001417 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001418 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001419{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001420 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001421
Willy Tarreau6c616e02014-06-25 16:56:41 +02001422 memset(smp, 0, sizeof(*smp));
1423
Willy Tarreau192252e2015-04-04 01:47:55 +02001424 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001425 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1426 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001427 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001428 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001429
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001430 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001431 return NULL;
1432
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001433 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001434 return NULL;
1435
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001436 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001437 return smp;
1438}
1439
Christopher Faulet476e5d02016-10-26 11:34:47 +02001440static void release_sample_arg(struct arg *p)
1441{
1442 struct arg *p_back = p;
1443
1444 if (!p)
1445 return;
1446
1447 while (p->type != ARGT_STOP) {
1448 if (p->type == ARGT_STR || p->unresolved) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001449 chunk_destroy(&p->data.str);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001450 p->unresolved = 0;
1451 }
1452 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001453 regex_free(p->data.reg);
1454 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001455 }
1456 p++;
1457 }
1458
1459 if (p_back != empty_arg_list)
1460 free(p_back);
1461}
1462
1463void release_sample_expr(struct sample_expr *expr)
1464{
1465 struct sample_conv_expr *conv_expr, *conv_exprb;
1466
1467 if (!expr)
1468 return;
1469
Tim Duesterhus867cd982020-07-04 11:49:39 +02001470 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001471 LIST_DELETE(&conv_expr->list);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001472 release_sample_arg(conv_expr->arg_p);
Tim Duesterhus867cd982020-07-04 11:49:39 +02001473 free(conv_expr);
1474 }
1475
Christopher Faulet476e5d02016-10-26 11:34:47 +02001476 release_sample_arg(expr->arg_p);
1477 free(expr);
1478}
1479
Emeric Brun107ca302010-01-04 16:16:05 +01001480/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001481/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001482/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001483/*****************************************************************/
1484
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001485static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1486{
1487 int i;
1488 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001489 struct buffer *buf;
1490 struct sink *sink;
1491 struct ist line;
1492 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001493
Willy Tarreau0851fd52019-12-17 10:07:25 +01001494 buf = alloc_trash_chunk();
1495 if (!buf)
1496 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001497
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001498 sink = (struct sink *)arg_p[1].data.ptr;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001499 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001500
Willy Tarreau0851fd52019-12-17 10:07:25 +01001501 pfx = arg_p[0].data.str.area;
1502 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001503
Willy Tarreau0851fd52019-12-17 10:07:25 +01001504 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1505 if (!sample_casts[smp->data.type][SMP_T_STR])
1506 goto nocast;
1507
1508 /* Copy sample fetch. This puts the sample as const, the
1509 * cast will copy data if a transformation is required.
1510 */
1511 memcpy(&tmp, smp, sizeof(struct sample));
1512 tmp.flags = SMP_F_CONST;
1513
1514 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1515 goto nocast;
1516
1517 /* Display the displayable chars*. */
1518 b_putchr(buf, '<');
1519 for (i = 0; i < tmp.data.u.str.data; i++) {
Willy Tarreau90807112020-02-25 08:16:33 +01001520 if (isprint((unsigned char)tmp.data.u.str.area[i]))
Willy Tarreau0851fd52019-12-17 10:07:25 +01001521 b_putchr(buf, tmp.data.u.str.area[i]);
1522 else
1523 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001524 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001525 b_putchr(buf, '>');
1526
1527 done:
1528 line = ist2(buf->area, buf->data);
Emeric Brun54648852020-07-06 15:54:06 +02001529 sink_write(sink, &line, 1, 0, 0, NULL);
Willy Tarreau0851fd52019-12-17 10:07:25 +01001530 end:
1531 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001532 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001533 nocast:
1534 chunk_appendf(buf, "(undisplayable)");
1535 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001536}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001537
1538// This function checks the "debug" converter's arguments.
1539static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1540 const char *file, int line, char **err)
1541{
1542 const char *name = "buf0";
1543 struct sink *sink = NULL;
1544
1545 if (args[0].type != ARGT_STR) {
1546 /* optional prefix */
1547 args[0].data.str.area = "";
1548 args[0].data.str.data = 0;
1549 }
1550
1551 if (args[1].type == ARGT_STR)
1552 name = args[1].data.str.area;
1553
1554 sink = sink_find(name);
1555 if (!sink) {
1556 memprintf(err, "No such sink '%s'", name);
1557 return 0;
1558 }
1559
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001560 chunk_destroy(&args[1].data.str);
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001561 args[1].type = ARGT_PTR;
1562 args[1].data.ptr = sink;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001563 return 1;
1564}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001565
Holger Just1bfc24b2017-05-06 00:56:53 +02001566static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1567{
Willy Tarreau83061a82018-07-13 11:56:34 +02001568 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001569 int bin_len;
1570
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001571 trash->data = 0;
1572 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1573 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001574 if (bin_len < 0)
1575 return 0;
1576
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001577 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001578 smp->data.u.str = *trash;
1579 smp->data.type = SMP_T_BIN;
1580 smp->flags &= ~SMP_F_CONST;
1581 return 1;
1582}
1583
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +02001584static int sample_conv_base64url2bin(const struct arg *arg_p, struct sample *smp, void *private)
1585{
1586 struct buffer *trash = get_trash_chunk();
1587 int bin_len;
1588
1589 trash->data = 0;
1590 bin_len = base64urldec(smp->data.u.str.area, smp->data.u.str.data,
1591 trash->area, trash->size);
1592 if (bin_len < 0)
1593 return 0;
1594
1595 trash->data = bin_len;
1596 smp->data.u.str = *trash;
1597 smp->data.type = SMP_T_BIN;
1598 smp->flags &= ~SMP_F_CONST;
1599 return 1;
1600}
1601
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001602static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001603{
Willy Tarreau83061a82018-07-13 11:56:34 +02001604 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001605 int b64_len;
1606
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001607 trash->data = 0;
1608 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1609 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001610 if (b64_len < 0)
1611 return 0;
1612
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001613 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001614 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001615 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001616 smp->flags &= ~SMP_F_CONST;
1617 return 1;
1618}
1619
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +02001620static int sample_conv_bin2base64url(const struct arg *arg_p, struct sample *smp, void *private)
1621{
1622 struct buffer *trash = get_trash_chunk();
1623 int b64_len;
1624
1625 trash->data = 0;
1626 b64_len = a2base64url(smp->data.u.str.area, smp->data.u.str.data,
1627 trash->area, trash->size);
1628 if (b64_len < 0)
1629 return 0;
1630
1631 trash->data = b64_len;
1632 smp->data.u.str = *trash;
1633 smp->data.type = SMP_T_STR;
1634 smp->flags &= ~SMP_F_CONST;
1635 return 1;
1636}
1637
Willy Tarreauea573502021-10-06 15:29:00 +02001638/* This function returns a sample struct filled with the conversion of variable
1639 * <var> to sample type <type> (SMP_T_*), via a cast to the target type. If the
1640 * variable cannot be retrieved or casted, 0 is returned, otherwise 1.
1641 *
1642 * Keep in mind that the sample content may be written to a pre-allocated
1643 * trash chunk as returned by get_trash_chunk().
1644 */
1645int sample_conv_var2smp(const struct var_desc *var, struct sample *smp, int type)
1646{
1647 if (!vars_get_by_desc(var, smp))
1648 return 0;
1649 if (!sample_casts[smp->data.type][type])
1650 return 0;
1651 if (!sample_casts[smp->data.type][type](smp))
1652 return 0;
1653 return 1;
1654}
1655
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001656static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1657{
1658 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001659 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001660
1661 memset(&ctx, 0, sizeof(ctx));
1662
1663 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001664 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1665 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001666
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001667 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001668 smp->data.u.str = *trash;
1669 smp->data.type = SMP_T_BIN;
1670 smp->flags &= ~SMP_F_CONST;
1671 return 1;
1672}
1673
Tim Duesterhusd4376302019-06-17 12:41:44 +02001674#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01001675static int smp_check_sha2(struct arg *args, struct sample_conv *conv,
1676 const char *file, int line, char **err)
1677{
1678 if (args[0].type == ARGT_STOP)
1679 return 1;
1680 if (args[0].type != ARGT_SINT) {
1681 memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]);
1682 return 0;
1683 }
1684
1685 switch (args[0].data.sint) {
1686 case 224:
1687 case 256:
1688 case 384:
1689 case 512:
1690 /* this is okay */
1691 return 1;
1692 default:
1693 memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint);
1694 return 0;
1695 }
1696}
1697
Tim Duesterhusd4376302019-06-17 12:41:44 +02001698static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
1699{
1700 struct buffer *trash = get_trash_chunk();
1701 int bits = 256;
Christopher Faulete6e7a582021-01-29 11:29:28 +01001702 if (arg_p->data.sint)
Tim Duesterhusd4376302019-06-17 12:41:44 +02001703 bits = arg_p->data.sint;
1704
1705 switch (bits) {
1706 case 224: {
1707 SHA256_CTX ctx;
1708
1709 memset(&ctx, 0, sizeof(ctx));
1710
1711 SHA224_Init(&ctx);
1712 SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1713 SHA224_Final((unsigned char *) trash->area, &ctx);
1714 trash->data = SHA224_DIGEST_LENGTH;
1715 break;
1716 }
1717 case 256: {
1718 SHA256_CTX ctx;
1719
1720 memset(&ctx, 0, sizeof(ctx));
1721
1722 SHA256_Init(&ctx);
1723 SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1724 SHA256_Final((unsigned char *) trash->area, &ctx);
1725 trash->data = SHA256_DIGEST_LENGTH;
1726 break;
1727 }
1728 case 384: {
1729 SHA512_CTX ctx;
1730
1731 memset(&ctx, 0, sizeof(ctx));
1732
1733 SHA384_Init(&ctx);
1734 SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1735 SHA384_Final((unsigned char *) trash->area, &ctx);
1736 trash->data = SHA384_DIGEST_LENGTH;
1737 break;
1738 }
1739 case 512: {
1740 SHA512_CTX ctx;
1741
1742 memset(&ctx, 0, sizeof(ctx));
1743
1744 SHA512_Init(&ctx);
1745 SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1746 SHA512_Final((unsigned char *) trash->area, &ctx);
1747 trash->data = SHA512_DIGEST_LENGTH;
1748 break;
1749 }
1750 default:
1751 return 0;
1752 }
1753
1754 smp->data.u.str = *trash;
1755 smp->data.type = SMP_T_BIN;
1756 smp->flags &= ~SMP_F_CONST;
1757 return 1;
1758}
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001759
Dragan Dosen9e8db132021-02-22 10:03:53 +01001760/* This function returns a sample struct filled with an <arg> content.
1761 * If the <arg> contains a string, it is returned in the sample flagged as
1762 * SMP_F_CONST. If the <arg> contains a variable descriptor, the sample is
1763 * filled with the content of the variable by using vars_get_by_desc().
1764 *
1765 * Keep in mind that the sample content may be written to a pre-allocated
1766 * trash chunk as returned by get_trash_chunk().
1767 *
1768 * This function returns 0 if an error occurs, otherwise it returns 1.
1769 */
Willy Tarreau05cd2e62021-10-06 15:20:18 +02001770int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001771{
1772 switch (arg->type) {
1773 case ARGT_STR:
1774 smp->data.type = SMP_T_STR;
1775 smp->data.u.str = arg->data.str;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001776 smp->flags = SMP_F_CONST;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001777 return 1;
1778 case ARGT_VAR:
1779 if (!vars_get_by_desc(&arg->data.var, smp))
1780 return 0;
1781 if (!sample_casts[smp->data.type][SMP_T_STR])
1782 return 0;
1783 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
1784 return 0;
1785 return 1;
1786 default:
1787 return 0;
1788 }
1789}
1790
Dragan Dosen9e8db132021-02-22 10:03:53 +01001791/* This function checks an <arg> and fills it with a variable type if the
1792 * <arg> string contains a valid variable name. If failed, the function
1793 * tries to perform a base64 decode operation on the same string, and
1794 * fills the <arg> with the decoded content.
1795 *
1796 * Validation is skipped if the <arg> string is empty.
1797 *
1798 * This function returns 0 if the variable lookup fails and the specified
1799 * <arg> string is not a valid base64 encoded string, as well if
1800 * unexpected argument type is specified or memory allocation error
1801 * occurs. Otherwise it returns 1.
1802 */
1803static inline int sample_check_arg_base64(struct arg *arg, char **err)
1804{
1805 char *dec = NULL;
1806 int dec_size;
1807
1808 if (arg->type != ARGT_STR) {
1809 memprintf(err, "unexpected argument type");
1810 return 0;
1811 }
1812
1813 if (arg->data.str.data == 0) /* empty */
1814 return 1;
1815
1816 if (vars_check_arg(arg, NULL))
1817 return 1;
1818
1819 if (arg->data.str.data % 4) {
1820 memprintf(err, "argument needs to be base64 encoded, and "
1821 "can either be a string or a variable");
1822 return 0;
1823 }
1824
1825 dec_size = (arg->data.str.data / 4 * 3)
1826 - (arg->data.str.area[arg->data.str.data-1] == '=' ? 1 : 0)
1827 - (arg->data.str.area[arg->data.str.data-2] == '=' ? 1 : 0);
1828
1829 if ((dec = malloc(dec_size)) == NULL) {
1830 memprintf(err, "memory allocation error");
1831 return 0;
1832 }
1833
1834 dec_size = base64dec(arg->data.str.area, arg->data.str.data, dec, dec_size);
1835 if (dec_size < 0) {
1836 memprintf(err, "argument needs to be base64 encoded, and "
1837 "can either be a string or a variable");
1838 free(dec);
1839 return 0;
1840 }
1841
1842 /* base64 decoded */
1843 chunk_destroy(&arg->data.str);
1844 arg->data.str.area = dec;
1845 arg->data.str.data = dec_size;
1846 return 1;
1847}
1848
Ilya Shipitsin2c481d02021-03-26 23:35:31 +05001849#ifdef EVP_CIPH_GCM_MODE
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001850static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
1851 const char *file, int line, char **err)
1852{
1853 switch(args[0].data.sint) {
1854 case 128:
1855 case 192:
1856 case 256:
1857 break;
1858 default:
1859 memprintf(err, "key size must be 128, 192 or 256 (bits).");
1860 return 0;
1861 }
Dragan Dosen9e8db132021-02-22 10:03:53 +01001862
1863 /* Try to decode variables. */
1864 if (!sample_check_arg_base64(&args[1], err)) {
1865 memprintf(err, "failed to parse nonce : %s", *err);
1866 return 0;
1867 }
1868 if (!sample_check_arg_base64(&args[2], err)) {
1869 memprintf(err, "failed to parse key : %s", *err);
1870 return 0;
1871 }
1872 if (!sample_check_arg_base64(&args[3], err)) {
1873 memprintf(err, "failed to parse aead_tag : %s", *err);
1874 return 0;
1875 }
1876
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001877 return 1;
1878}
1879
1880/* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
1881static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
1882{
1883 struct sample nonce, key, aead_tag;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001884 struct buffer *smp_trash = NULL, *smp_trash_alloc = NULL;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001885 EVP_CIPHER_CTX *ctx;
1886 int dec_size, ret;
1887
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001888 smp_trash_alloc = alloc_trash_chunk();
1889 if (!smp_trash_alloc)
1890 return 0;
1891
Dragan Dosen9e8db132021-02-22 10:03:53 +01001892 /* smp copy */
1893 smp_trash_alloc->data = smp->data.u.str.data;
1894 if (unlikely(smp_trash_alloc->data > smp_trash_alloc->size))
1895 smp_trash_alloc->data = smp_trash_alloc->size;
1896 memcpy(smp_trash_alloc->area, smp->data.u.str.area, smp_trash_alloc->data);
1897
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001898 ctx = EVP_CIPHER_CTX_new();
1899
1900 if (!ctx)
1901 goto err;
1902
Dragan Dosen9e8db132021-02-22 10:03:53 +01001903 smp_trash = alloc_trash_chunk();
1904 if (!smp_trash)
1905 goto err;
1906
1907 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
1908 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001909 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001910
1911 if (arg_p[1].type == ARGT_VAR) {
1912 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
1913 if (dec_size < 0)
1914 goto err;
1915 smp_trash->data = dec_size;
1916 nonce.data.u.str = *smp_trash;
1917 }
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001918
1919 /* Set cipher type and mode */
1920 switch(arg_p[0].data.sint) {
1921 case 128:
1922 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
1923 break;
1924 case 192:
1925 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
1926 break;
1927 case 256:
1928 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
1929 break;
1930 }
1931
Dragan Dosen9e8db132021-02-22 10:03:53 +01001932 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, nonce.data.u.str.data, NULL);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001933
1934 /* Initialise IV */
Dragan Dosen9e8db132021-02-22 10:03:53 +01001935 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) nonce.data.u.str.area))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001936 goto err;
1937
Dragan Dosen9e8db132021-02-22 10:03:53 +01001938 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
1939 if (!sample_conv_var2smp_str(&arg_p[2], &key))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001940 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001941
1942 if (arg_p[2].type == ARGT_VAR) {
1943 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
1944 if (dec_size < 0)
1945 goto err;
1946 smp_trash->data = dec_size;
1947 key.data.u.str = *smp_trash;
1948 }
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001949
1950 /* Initialise key */
Dragan Dosen9e8db132021-02-22 10:03:53 +01001951 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) key.data.u.str.area, NULL))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001952 goto err;
1953
1954 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
Dragan Dosen9e8db132021-02-22 10:03:53 +01001955 (unsigned char *) smp_trash_alloc->area, (int) smp_trash_alloc->data))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001956 goto err;
1957
Dragan Dosen9e8db132021-02-22 10:03:53 +01001958 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
1959 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001960 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001961
1962 if (arg_p[3].type == ARGT_VAR) {
1963 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
1964 if (dec_size < 0)
1965 goto err;
1966 smp_trash_alloc->data = dec_size;
1967 aead_tag.data.u.str = *smp_trash_alloc;
1968 }
1969
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001970 dec_size = smp_trash->data;
1971
Dragan Dosen9e8db132021-02-22 10:03:53 +01001972 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 +02001973 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
1974
1975 if (ret <= 0)
1976 goto err;
1977
1978 smp->data.u.str.data = dec_size + smp_trash->data;
1979 smp->data.u.str.area = smp_trash->area;
1980 smp->data.type = SMP_T_BIN;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001981 smp_dup(smp);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001982 free_trash_chunk(smp_trash_alloc);
Dragan Dosen9e8db132021-02-22 10:03:53 +01001983 free_trash_chunk(smp_trash);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001984 return 1;
1985
1986err:
1987 free_trash_chunk(smp_trash_alloc);
Dragan Dosen9e8db132021-02-22 10:03:53 +01001988 free_trash_chunk(smp_trash);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001989 return 0;
1990}
Ilya Shipitsin2c481d02021-03-26 23:35:31 +05001991#endif
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001992
Patrick Gansterer8e366512020-04-22 16:47:57 +02001993static int check_crypto_digest(struct arg *args, struct sample_conv *conv,
1994 const char *file, int line, char **err)
1995{
1996 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1997
1998 if (evp)
1999 return 1;
2000
2001 memprintf(err, "algorithm must be a valid OpenSSL message digest name.");
2002 return 0;
2003}
2004
2005static int sample_conv_crypto_digest(const struct arg *args, struct sample *smp, void *private)
2006{
2007 struct buffer *trash = get_trash_chunk();
2008 unsigned char *md = (unsigned char*) trash->area;
2009 unsigned int md_len = trash->size;
2010 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
2011 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
2012
2013 if (!ctx)
2014 return 0;
2015
2016 if (!EVP_DigestInit_ex(ctx, evp, NULL) ||
2017 !EVP_DigestUpdate(ctx, smp->data.u.str.area, smp->data.u.str.data) ||
2018 !EVP_DigestFinal_ex(ctx, md, &md_len)) {
2019 EVP_MD_CTX_free(ctx);
2020 return 0;
2021 }
2022
2023 EVP_MD_CTX_free(ctx);
2024
2025 trash->data = md_len;
2026 smp->data.u.str = *trash;
2027 smp->data.type = SMP_T_BIN;
2028 smp->flags &= ~SMP_F_CONST;
2029 return 1;
2030}
2031
2032static int check_crypto_hmac(struct arg *args, struct sample_conv *conv,
2033 const char *file, int line, char **err)
2034{
2035 if (!check_crypto_digest(args, conv, file, line, err))
2036 return 0;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002037
2038 if (!sample_check_arg_base64(&args[1], err)) {
2039 memprintf(err, "failed to parse key : %s", *err);
2040 return 0;
2041 }
Patrick Gansterer8e366512020-04-22 16:47:57 +02002042
Patrick Gansterer8e366512020-04-22 16:47:57 +02002043 return 1;
2044}
2045
2046static int sample_conv_crypto_hmac(const struct arg *args, struct sample *smp, void *private)
2047{
2048 struct sample key;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002049 struct buffer *trash = NULL, *key_trash = NULL;
Patrick Gansterer8e366512020-04-22 16:47:57 +02002050 unsigned char *md;
2051 unsigned int md_len;
2052 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
2053 int dec_size;
2054
2055 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
2056 if (!sample_conv_var2smp_str(&args[1], &key))
2057 return 0;
2058
Dragan Dosen9e8db132021-02-22 10:03:53 +01002059 if (args[1].type == ARGT_VAR) {
2060 key_trash = alloc_trash_chunk();
2061 if (!key_trash)
2062 goto err;
2063
2064 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, key_trash->area, key_trash->size);
2065 if (dec_size < 0)
2066 goto err;
2067 key_trash->data = dec_size;
2068 key.data.u.str = *key_trash;
2069 }
Patrick Gansterer8e366512020-04-22 16:47:57 +02002070
Dragan Dosen9e8db132021-02-22 10:03:53 +01002071 trash = alloc_trash_chunk();
2072 if (!trash)
Patrick Gansterer8e366512020-04-22 16:47:57 +02002073 goto err;
2074
2075 md = (unsigned char*) trash->area;
2076 md_len = trash->size;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002077 if (!HMAC(evp, key.data.u.str.area, key.data.u.str.data, (const unsigned char*) smp->data.u.str.area,
2078 smp->data.u.str.data, md, &md_len))
Patrick Gansterer8e366512020-04-22 16:47:57 +02002079 goto err;
2080
2081 free_trash_chunk(key_trash);
2082
2083 trash->data = md_len;
2084 smp->data.u.str = *trash;
2085 smp->data.type = SMP_T_BIN;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002086 smp_dup(smp);
2087 free_trash_chunk(trash);
Patrick Gansterer8e366512020-04-22 16:47:57 +02002088 return 1;
2089
2090err:
2091 free_trash_chunk(key_trash);
Dragan Dosen9e8db132021-02-22 10:03:53 +01002092 free_trash_chunk(trash);
Patrick Gansterer8e366512020-04-22 16:47:57 +02002093 return 0;
2094}
2095
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02002096#endif /* USE_OPENSSL */
Tim Duesterhusd4376302019-06-17 12:41:44 +02002097
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002098static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002099{
Willy Tarreau83061a82018-07-13 11:56:34 +02002100 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002101 unsigned char c;
2102 int ptr = 0;
2103
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002104 trash->data = 0;
2105 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
2106 c = smp->data.u.str.area[ptr++];
2107 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
2108 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002109 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002110 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002111 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002112 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002113 return 1;
2114}
2115
Dragan Dosen3f957b22017-10-24 09:27:34 +02002116static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
2117{
2118 long long int n = 0;
2119 int i, c;
2120
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002121 for (i = 0; i < smp->data.u.str.data; i++) {
2122 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02002123 return 0;
2124 n = (n << 4) + c;
2125 }
2126
2127 smp->data.u.sint = n;
2128 smp->data.type = SMP_T_SINT;
2129 smp->flags &= ~SMP_F_CONST;
2130 return 1;
2131}
2132
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002133/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002134static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002135{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002136 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
2137 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002138 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002139 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002140 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002141 return 1;
2142}
2143
Willy Tarreau60a2ee72017-12-15 07:13:48 +01002144static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002145{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002146 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002147 smp->data.u.sint = i;
2148 smp->data.type = SMP_T_SINT;
2149 return 1;
2150}
2151
2152
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002153static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002154{
2155 int i;
2156
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002157 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002158 return 0;
2159
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002160 for (i = 0; i < smp->data.u.str.data; i++) {
2161 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
2162 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01002163 }
2164 return 1;
2165}
2166
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002167static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002168{
2169 int i;
2170
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002171 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02002172 return 0;
2173
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002174 for (i = 0; i < smp->data.u.str.data; i++) {
2175 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
2176 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01002177 }
2178 return 1;
2179}
2180
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002181/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
2182static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002183{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002184 /* Attempt to convert to IPv4 to apply the correct mask. */
2185 c_ipv62ip(smp);
2186
2187 if (smp->data.type == SMP_T_IPV4) {
2188 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
2189 smp->data.type = SMP_T_IPV4;
2190 }
2191 else if (smp->data.type == SMP_T_IPV6) {
2192 /* IPv6 cannot be converted without an IPv6 mask. */
2193 if (args[1].type != ARGT_IPV6)
2194 return 0;
2195
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +01002196 write_u64(&smp->data.u.ipv6.s6_addr[0],
2197 read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0]));
2198 write_u64(&smp->data.u.ipv6.s6_addr[8],
2199 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 +01002200 smp->data.type = SMP_T_IPV6;
2201 }
2202
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002203 return 1;
2204}
2205
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002206/* takes an UINT value on input supposed to represent the time since EPOCH,
2207 * adds an optional offset found in args[1] and emits a string representing
2208 * the local time in the format specified in args[1] using strftime().
2209 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002210static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002211{
Willy Tarreau83061a82018-07-13 11:56:34 +02002212 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002213 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002214 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Tim Duesterhus9da4b1f2021-08-28 23:57:01 +02002215 struct tm tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002216
2217 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002218 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002219 curr_date += args[1].data.sint;
2220
Tim Duesterhus9da4b1f2021-08-28 23:57:01 +02002221 get_localtime(curr_date, &tm);
2222
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002223 temp = get_trash_chunk();
Tim Duesterhus9da4b1f2021-08-28 23:57:01 +02002224 temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002225 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002226 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002227 return 1;
2228}
2229
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002230/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002231static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002232{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002233 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
2234 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002235 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002236 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002237 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002238 return 1;
2239}
2240
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002241/* takes an UINT value on input supposed to represent the time since EPOCH,
2242 * adds an optional offset found in args[1] and emits a string representing
2243 * the UTC date in the format specified in args[1] using strftime().
2244 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002245static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002246{
Willy Tarreau83061a82018-07-13 11:56:34 +02002247 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002248 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002249 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Tim Duesterhus9da4b1f2021-08-28 23:57:01 +02002250 struct tm tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002251
2252 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002253 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002254 curr_date += args[1].data.sint;
2255
Tim Duesterhus9da4b1f2021-08-28 23:57:01 +02002256 get_gmtime(curr_date, &tm);
2257
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002258 temp = get_trash_chunk();
Tim Duesterhus9da4b1f2021-08-28 23:57:01 +02002259 temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002260 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002261 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002262 return 1;
2263}
2264
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002265/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002266static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002267{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002268 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
2269 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002270 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002271 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002272 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002273 return 1;
2274}
2275
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002276/* hashes the binary input into a 32-bit unsigned int using xxh.
2277 * The seed of the hash defaults to 0 but can be changd in argument 1.
2278 */
2279static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
2280{
2281 unsigned int seed;
2282
Christopher Faulete6e7a582021-01-29 11:29:28 +01002283 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002284 seed = arg_p->data.sint;
2285 else
2286 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002287 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
2288 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002289 smp->data.type = SMP_T_SINT;
2290 return 1;
2291}
2292
2293/* hashes the binary input into a 64-bit unsigned int using xxh.
2294 * In fact, the function returns a 64 bit unsigned, but the sample
2295 * storage of haproxy only proposes 64-bits signed, so the value is
2296 * cast as signed. This cast doesn't impact the hash repartition.
2297 * The seed of the hash defaults to 0 but can be changd in argument 1.
2298 */
2299static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
2300{
2301 unsigned long long int seed;
2302
Christopher Faulete6e7a582021-01-29 11:29:28 +01002303 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002304 seed = (unsigned long long int)arg_p->data.sint;
2305 else
2306 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002307 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
2308 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002309 smp->data.type = SMP_T_SINT;
2310 return 1;
2311}
2312
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002313static int sample_conv_xxh3(const struct arg *arg_p, struct sample *smp, void *private)
2314{
2315 unsigned long long int seed;
2316
Christopher Faulete6e7a582021-01-29 11:29:28 +01002317 if (arg_p->data.sint)
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002318 seed = (unsigned long long int)arg_p->data.sint;
2319 else
2320 seed = 0;
2321 smp->data.u.sint = (long long int)XXH3(smp->data.u.str.area,
2322 smp->data.u.str.data, seed);
2323 smp->data.type = SMP_T_SINT;
2324 return 1;
2325}
2326
Willy Tarreau80599772015-01-20 19:35:24 +01002327/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002328static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01002329{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002330 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
2331 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002332 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002333 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002334 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01002335 return 1;
2336}
2337
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002338/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
2339static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
2340{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002341 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
2342 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002343 if (arg_p->data.sint)
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002344 smp->data.u.sint = full_hash(smp->data.u.sint);
2345 smp->data.type = SMP_T_SINT;
2346 return 1;
2347}
2348
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002349/* This function escape special json characters. The returned string can be
2350 * safely set between two '"' and used as json string. The json string is
2351 * defined like this:
2352 *
2353 * any Unicode character except '"' or '\' or control character
2354 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
2355 *
2356 * The enum input_type contain all the allowed mode for decoding the input
2357 * string.
2358 */
2359enum input_type {
2360 IT_ASCII = 0,
2361 IT_UTF8,
2362 IT_UTF8S,
2363 IT_UTF8P,
2364 IT_UTF8PS,
2365};
William Dauchy5417e892021-01-08 21:57:41 +01002366
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002367static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
2368 const char *file, int line, char **err)
2369{
Christopher Faulet95917132020-08-05 23:07:07 +02002370 enum input_type type;
2371
Christopher Faulet95917132020-08-05 23:07:07 +02002372 if (strcmp(arg->data.str.area, "") == 0)
2373 type = IT_ASCII;
2374 else if (strcmp(arg->data.str.area, "ascii") == 0)
2375 type = IT_ASCII;
2376 else if (strcmp(arg->data.str.area, "utf8") == 0)
2377 type = IT_UTF8;
2378 else if (strcmp(arg->data.str.area, "utf8s") == 0)
2379 type = IT_UTF8S;
2380 else if (strcmp(arg->data.str.area, "utf8p") == 0)
2381 type = IT_UTF8P;
2382 else if (strcmp(arg->data.str.area, "utf8ps") == 0)
2383 type = IT_UTF8PS;
2384 else {
2385 memprintf(err, "Unexpected input code type. "
2386 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
2387 return 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002388 }
2389
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002390 chunk_destroy(&arg->data.str);
Christopher Faulet95917132020-08-05 23:07:07 +02002391 arg->type = ARGT_SINT;
2392 arg->data.sint = type;
2393 return 1;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002394}
2395
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002396static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002397{
Willy Tarreau83061a82018-07-13 11:56:34 +02002398 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002399 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
2400 const char *str;
2401 int len;
2402 enum input_type input_type = IT_ASCII;
2403 unsigned int c;
2404 unsigned int ret;
2405 char *p;
2406
Christopher Faulete6e7a582021-01-29 11:29:28 +01002407 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002408
2409 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002410 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002411
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002412 p = smp->data.u.str.area;
2413 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002414
2415 if (input_type == IT_ASCII) {
2416 /* Read input as ASCII. */
2417 c = *(unsigned char *)p;
2418 p++;
2419 }
2420 else {
2421 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002422 ret = utf8_next(p,
2423 smp->data.u.str.data - ( p - smp->data.u.str.area),
2424 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002425 p += utf8_return_length(ret);
2426
2427 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2428 return 0;
2429 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2430 continue;
2431 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2432 return 0;
2433 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2434 continue;
2435
2436 /* Check too big values. */
2437 if ((unsigned int)c > 0xffff) {
2438 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2439 return 0;
2440 continue;
2441 }
2442 }
2443
2444 /* Convert character. */
2445 if (c == '"') {
2446 len = 2;
2447 str = "\\\"";
2448 }
2449 else if (c == '\\') {
2450 len = 2;
2451 str = "\\\\";
2452 }
2453 else if (c == '/') {
2454 len = 2;
2455 str = "\\/";
2456 }
2457 else if (c == '\b') {
2458 len = 2;
2459 str = "\\b";
2460 }
2461 else if (c == '\f') {
2462 len = 2;
2463 str = "\\f";
2464 }
2465 else if (c == '\r') {
2466 len = 2;
2467 str = "\\r";
2468 }
2469 else if (c == '\n') {
2470 len = 2;
2471 str = "\\n";
2472 }
2473 else if (c == '\t') {
2474 len = 2;
2475 str = "\\t";
2476 }
Willy Tarreau90807112020-02-25 08:16:33 +01002477 else if (c > 0xff || !isprint((unsigned char)c)) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002478 /* isprint generate a segfault if c is too big. The man says that
2479 * c must have the value of an unsigned char or EOF.
2480 */
2481 len = 6;
2482 _str[0] = '\\';
2483 _str[1] = 'u';
2484 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2485 str = _str;
2486 }
2487 else {
2488 len = 1;
Willy Tarreau5715da22020-02-25 08:37:37 +01002489 _str[0] = c;
2490 str = _str;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002491 }
2492
2493 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002494 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002495 return 0;
2496
2497 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002498 memcpy(temp->area + temp->data, str, len);
2499 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002500 }
2501
2502 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002503 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002504 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002505
2506 return 1;
2507}
2508
Emeric Brun54c4ac82014-11-03 15:32:43 +01002509/* This sample function is designed to extract some bytes from an input buffer.
2510 * First arg is the offset.
2511 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002512static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002513{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002514 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2515 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002516 return 1;
2517 }
2518
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002519 if (smp->data.u.str.size)
2520 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002521 smp->data.u.str.data -= arg_p[0].data.sint;
2522 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002523
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002524 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2525 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002526
2527 return 1;
2528}
2529
Emeric Brunf399b0d2014-11-03 17:07:03 +01002530static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2531 const char *file, int line, char **err)
2532{
2533 struct arg *arg = args;
2534
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002535 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002536 memprintf(err, "Unexpected arg type");
2537 return 0;
2538 }
2539
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002540 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002541 memprintf(err, "Unexpected value 0 for index");
2542 return 0;
2543 }
2544
2545 arg++;
2546
2547 if (arg->type != ARGT_STR) {
2548 memprintf(err, "Unexpected arg type");
2549 return 0;
2550 }
2551
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002552 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002553 memprintf(err, "Empty separators list");
2554 return 0;
2555 }
2556
2557 return 1;
2558}
2559
2560/* This sample function is designed to a return selected part of a string (field).
2561 * First arg is the index of the field (start at 1)
2562 * Second arg is a char list of separators (type string)
2563 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002564static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002565{
Marcin Deranek9631a282018-04-16 14:30:46 +02002566 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002567 char *start, *end;
2568 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002569 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002570
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002571 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002572 return 0;
2573
Marcin Deranek9631a282018-04-16 14:30:46 +02002574 if (arg_p[0].data.sint < 0) {
2575 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002576 end = start = smp->data.u.str.area + smp->data.u.str.data;
2577 while (start > smp->data.u.str.area) {
2578 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2579 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002580 if (field == arg_p[0].data.sint) {
2581 if (count == 1)
2582 goto found;
2583 else if (count > 1)
2584 count--;
2585 } else {
2586 end = start-1;
2587 field--;
2588 }
2589 break;
2590 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002591 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002592 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002593 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002594 } else {
2595 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002596 end = start = smp->data.u.str.area;
2597 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2598 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2599 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002600 if (field == arg_p[0].data.sint) {
2601 if (count == 1)
2602 goto found;
2603 else if (count > 1)
2604 count--;
2605 } else {
2606 start = end+1;
2607 field++;
2608 }
2609 break;
2610 }
2611 }
2612 end++;
2613 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002614 }
2615
2616 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002617 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002618 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002619 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002620 }
2621found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002622 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002623 /* If ret string is len 0, no need to
2624 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002625 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002626 return 1;
2627
Emeric Brunf399b0d2014-11-03 17:07:03 +01002628 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002629 Note: smp->data.u.str.size cannot be set to 0 */
2630 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002631 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002632
Thayne McCombsb2843052021-04-11 23:26:59 -06002633 smp->data.u.str.area = start;
2634
Emeric Brunf399b0d2014-11-03 17:07:03 +01002635 return 1;
2636}
2637
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002638/* This sample function is designed to return a word from a string.
2639 * First arg is the index of the word (start at 1)
2640 * Second arg is a char list of words separators (type string)
2641 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002642static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002643{
Marcin Deranek9631a282018-04-16 14:30:46 +02002644 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002645 char *start, *end;
2646 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002647 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002648
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002649 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002650 return 0;
2651
2652 word = 0;
2653 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002654 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002655 end = start = smp->data.u.str.area + smp->data.u.str.data;
2656 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002657 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002658 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2659 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002660 issep = 1;
2661 break;
2662 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002663 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002664 if (!inword) {
2665 if (!issep) {
2666 if (word != arg_p[0].data.sint) {
2667 word--;
2668 end = start;
2669 }
2670 inword = 1;
2671 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002672 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002673 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002674 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002675 if (count == 1)
2676 goto found;
2677 else if (count > 1)
2678 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002679 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002680 inword = 0;
2681 }
2682 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002683 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002684 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002685 end = start = smp->data.u.str.area;
2686 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002687 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002688 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2689 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002690 issep = 1;
2691 break;
2692 }
2693 }
2694 if (!inword) {
2695 if (!issep) {
2696 if (word != arg_p[0].data.sint) {
2697 word++;
2698 start = end;
2699 }
2700 inword = 1;
2701 }
2702 }
2703 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002704 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002705 if (count == 1)
2706 goto found;
2707 else if (count > 1)
2708 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002709 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002710 inword = 0;
2711 }
2712 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002713 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002714 }
2715
2716 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002717 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002718 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002719 return 1;
2720 }
2721found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002722 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002723 /* If ret string is len 0, no need to
2724 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002725 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002726 return 1;
2727
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002728 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002729
2730 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002731 Note: smp->data.u.str.size cannot be set to 0 */
2732 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002733 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002734
2735 return 1;
2736}
2737
Willy Tarreau7eda8492015-01-20 19:47:06 +01002738static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2739 const char *file, int line, char **err)
2740{
2741 struct arg *arg = args;
2742 char *p;
2743 int len;
2744
2745 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2746 arg[0].type_flags = 0;
2747
2748 if (arg[2].type != ARGT_STR)
2749 return 1;
2750
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002751 p = arg[2].data.str.area;
2752 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002753 while (len) {
2754 if (*p == 'i') {
2755 arg[0].type_flags |= ARGF_REG_ICASE;
2756 }
2757 else if (*p == 'g') {
2758 arg[0].type_flags |= ARGF_REG_GLOB;
2759 }
2760 else {
2761 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2762 return 0;
2763 }
2764 p++;
2765 len--;
2766 }
2767 return 1;
2768}
2769
2770/* This sample function is designed to do the equivalent of s/match/replace/ on
2771 * the input string. It applies a regex and restarts from the last matched
2772 * location until nothing matches anymore. First arg is the regex to apply to
2773 * the input string, second arg is the replacement expression.
2774 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002775static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002776{
2777 char *start, *end;
2778 struct my_regex *reg = arg_p[0].data.reg;
2779 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002780 struct buffer *trash = get_trash_chunk();
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002781 struct buffer *output;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002782 int flag, max;
2783 int found;
2784
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002785 start = smp->data.u.str.area;
2786 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002787
2788 flag = 0;
2789 while (1) {
2790 /* check for last round which is used to copy remaining parts
2791 * when not running in global replacement mode.
2792 */
2793 found = 0;
2794 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2795 /* Note: we can have start == end on empty strings or at the end */
2796 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2797 }
2798
2799 if (!found)
2800 pmatch[0].rm_so = end - start;
2801
2802 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002803 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002804 if (max && pmatch[0].rm_so > 0) {
2805 if (max > pmatch[0].rm_so)
2806 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002807 memcpy(trash->area + trash->data, start, max);
2808 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002809 }
2810
2811 if (!found)
2812 break;
2813
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002814 output = alloc_trash_chunk();
Willy Tarreau23997da2020-02-18 14:27:44 +01002815 if (!output)
2816 break;
2817
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002818 output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2819
Willy Tarreau7eda8492015-01-20 19:47:06 +01002820 /* replace the matching part */
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002821 max = output->size - output->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002822 if (max) {
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002823 if (max > output->data)
2824 max = output->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002825 memcpy(trash->area + trash->data,
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002826 output->area, max);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002827 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002828 }
2829
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002830 free_trash_chunk(output);
2831
Willy Tarreau7eda8492015-01-20 19:47:06 +01002832 /* stop here if we're done with this string */
2833 if (start >= end)
2834 break;
2835
2836 /* We have a special case for matches of length 0 (eg: "x*y*").
2837 * These ones are considered to match in front of a character,
2838 * so we have to copy that character and skip to the next one.
2839 */
2840 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002841 if (trash->data < trash->size)
2842 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002843 pmatch[0].rm_eo++;
2844 }
2845
2846 start += pmatch[0].rm_eo;
2847 flag |= REG_NOTBOL;
2848 }
2849
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002850 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002851 return 1;
2852}
2853
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002854/* This function check an operator entry. It expects a string.
2855 * The string can be an integer or a variable name.
2856 */
2857static int check_operator(struct arg *args, struct sample_conv *conv,
2858 const char *file, int line, char **err)
2859{
2860 const char *str;
2861 const char *end;
Christopher Faulet95917132020-08-05 23:07:07 +02002862 long long int i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002863
2864 /* Try to decode a variable. */
2865 if (vars_check_arg(&args[0], NULL))
2866 return 1;
2867
2868 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002869 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002870 end = str + strlen(str);
Christopher Faulet95917132020-08-05 23:07:07 +02002871 i = read_int64(&str, end);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002872 if (*str != '\0') {
2873 memprintf(err, "expects an integer or a variable name");
2874 return 0;
2875 }
Christopher Faulet95917132020-08-05 23:07:07 +02002876
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002877 chunk_destroy(&args[0].data.str);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002878 args[0].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02002879 args[0].data.sint = i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002880 return 1;
2881}
2882
Willy Tarreau6204cd92016-03-10 16:33:04 +01002883/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002884 * If the arg contain an integer, the integer is returned in the
2885 * sample. If the arg contains a variable descriptor, it returns the
2886 * variable value.
2887 *
2888 * This function returns 0 if an error occurs, otherwise it returns 1.
2889 */
Willy Tarreau9cfb71f2021-10-06 15:19:05 +02002890int sample_conv_var2smp_sint(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002891{
2892 switch (arg->type) {
2893 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002894 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002895 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002896 return 1;
2897 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002898 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002899 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002900 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002901 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002902 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002903 return 0;
2904 return 1;
2905 default:
2906 return 0;
2907 }
2908}
2909
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002910/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002911 * result.
2912 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002913static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002914{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002915 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002916 return 1;
2917}
2918
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002919/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002920 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002921 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002922static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002923{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002924 struct sample tmp;
2925
Willy Tarreau7560dd42016-03-10 16:28:58 +01002926 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau9cfb71f2021-10-06 15:19:05 +02002927 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002928 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002929 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002930 return 1;
2931}
2932
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002933/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002934 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002935 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002936static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002937{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002938 struct sample tmp;
2939
Willy Tarreau7560dd42016-03-10 16:28:58 +01002940 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau9cfb71f2021-10-06 15:19:05 +02002941 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002942 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002943 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002944 return 1;
2945}
2946
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002947/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002948 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002949 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002950static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002951{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002952 struct sample tmp;
2953
Willy Tarreau7560dd42016-03-10 16:28:58 +01002954 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau9cfb71f2021-10-06 15:19:05 +02002955 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002956 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002957 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002958 return 1;
2959}
2960
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002961static inline long long int arith_add(long long int a, long long int b)
2962{
2963 /* Prevent overflow and makes capped calculus.
2964 * We must ensure that the check calculus doesn't
2965 * exceed the signed 64 bits limits.
2966 *
2967 * +----------+----------+
2968 * | a<0 | a>=0 |
2969 * +------+----------+----------+
2970 * | b<0 | MIN-a>b | no check |
2971 * +------+----------+----------+
2972 * | b>=0 | no check | MAX-a<b |
2973 * +------+----------+----------+
2974 */
2975 if ((a ^ b) >= 0) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002976 /* signs are different. */
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002977 if (a < 0) {
2978 if (LLONG_MIN - a > b)
2979 return LLONG_MIN;
2980 }
2981 if (LLONG_MAX - a < b)
2982 return LLONG_MAX;
2983 }
2984 return a + b;
2985}
2986
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002987/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002988 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002989 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002990static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002991{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002992 struct sample tmp;
2993
Willy Tarreau7560dd42016-03-10 16:28:58 +01002994 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau9cfb71f2021-10-06 15:19:05 +02002995 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002996 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002997 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002998 return 1;
2999}
3000
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003001/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003002 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01003003 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003004static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003005 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003006{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003007 struct sample tmp;
3008
Willy Tarreau7560dd42016-03-10 16:28:58 +01003009 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau9cfb71f2021-10-06 15:19:05 +02003010 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003011 return 0;
3012
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003013 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
3014 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
3015 * of -LLONG_MIN and correct the result.
3016 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003017 if (tmp.data.u.sint == LLONG_MIN) {
3018 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
3019 if (smp->data.u.sint < LLONG_MAX)
3020 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003021 return 1;
3022 }
3023
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003024 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003025 * the second operand.
3026 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003027 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01003028 return 1;
3029}
3030
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003031/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003032 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003033 * If the result makes an overflow, then the largest possible quantity is
3034 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003035 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003036static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003037 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003038{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003039 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003040 long long int c;
3041
Willy Tarreau7560dd42016-03-10 16:28:58 +01003042 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau9cfb71f2021-10-06 15:19:05 +02003043 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003044 return 0;
3045
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003046 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003047 if (!smp->data.u.sint || !tmp.data.u.sint) {
3048 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003049 return 1;
3050 }
3051
3052 /* The multiply between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003053 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003054 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003055 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3056 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003057 return 1;
3058 }
3059
3060 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003061 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003062
3063 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003064 if (smp->data.u.sint != c / tmp.data.u.sint) {
3065 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
3066 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003067 return 1;
3068 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003069 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003070 return 1;
3071 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003072 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01003073 return 1;
3074}
3075
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003076/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003077 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003078 * If arg_p makes the result overflow, then the largest possible quantity is
3079 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003080 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003081static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003082 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003083{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003084 struct sample tmp;
3085
Willy Tarreau7560dd42016-03-10 16:28:58 +01003086 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau9cfb71f2021-10-06 15:19:05 +02003087 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003088 return 0;
3089
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003090 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003091 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003092 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003093 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003094 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3095 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003096 return 1;
3097 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003098 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003099 return 1;
3100 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003101 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01003102 return 1;
3103}
3104
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003105/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003106 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003107 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003108 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003109static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003110 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003111{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003112 struct sample tmp;
3113
Willy Tarreau7560dd42016-03-10 16:28:58 +01003114 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau9cfb71f2021-10-06 15:19:05 +02003115 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003116 return 0;
3117
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003118 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003119 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003120 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003121 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003122 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3123 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003124 return 1;
3125 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003126 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003127 return 1;
3128 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003129 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01003130 return 1;
3131}
3132
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003133/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01003134 * result.
3135 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003136static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003137 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003138{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003139 if (smp->data.u.sint == LLONG_MIN)
3140 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003141 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003142 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003143 return 1;
3144}
3145
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003146/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003147 * false. The output is a BOOL.
3148 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003149static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003150 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003151{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003152 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003153 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003154 return 1;
3155}
3156
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003157/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003158 * truee. The output is a BOOL.
3159 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003160static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003161 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003162{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003163 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003164 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003165 return 1;
3166}
3167
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003168/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003169 * The output is a BOOL.
3170 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003171static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003172 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003173{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003174 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003175 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003176 return 1;
3177}
3178
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003179/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003180 * The output is a BOOL.
3181 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003182static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003183 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003184{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003185 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003186 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003187 return 1;
3188}
3189
Willy Tarreau280f42b2018-02-19 15:34:12 +01003190/* appends an optional const string, an optional variable contents and another
3191 * optional const string to an existing string.
3192 */
3193static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
3194{
Willy Tarreau83061a82018-07-13 11:56:34 +02003195 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003196 struct sample tmp;
3197 int max;
3198
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003199 trash = alloc_trash_chunk();
William Dauchye9970102021-01-11 11:05:58 +01003200 if (!trash)
3201 return 0;
3202
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003203 trash->data = smp->data.u.str.data;
3204 if (trash->data > trash->size - 1)
3205 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003206
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003207 memcpy(trash->area, smp->data.u.str.area, trash->data);
3208 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003209
3210 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003211 max = arg_p[0].data.str.data;
3212 if (max > trash->size - 1 - trash->data)
3213 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003214
3215 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003216 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
3217 trash->data += max;
3218 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003219 }
3220
3221 /* append second string (variable) if it's found and we can turn it
3222 * into a string.
3223 */
3224 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3225 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
3226 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
3227 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
3228
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003229 max = tmp.data.u.str.data;
3230 if (max > trash->size - 1 - trash->data)
3231 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003232
3233 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003234 memcpy(trash->area + trash->data, tmp.data.u.str.area,
3235 max);
3236 trash->data += max;
3237 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003238 }
3239 }
3240
3241 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003242 max = arg_p[2].data.str.data;
3243 if (max > trash->size - 1 - trash->data)
3244 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003245
3246 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003247 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
3248 trash->data += max;
3249 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003250 }
3251
3252 smp->data.u.str = *trash;
3253 smp->data.type = SMP_T_STR;
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003254 smp_dup(smp);
3255 free_trash_chunk(trash);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003256 return 1;
3257}
3258
3259/* This function checks the "concat" converter's arguments and extracts the
3260 * variable name and its scope.
3261 */
3262static int smp_check_concat(struct arg *args, struct sample_conv *conv,
3263 const char *file, int line, char **err)
3264{
3265 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003266 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
3267 memprintf(err, "failed to register variable name '%s'",
3268 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003269 return 0;
3270 }
3271 return 1;
3272}
3273
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003274/* Compares string with a variable containing a string. Return value
Tim Duesterhusca097c12018-04-27 21:18:45 +02003275 * is compatible with strcmp(3)'s return value.
3276 */
3277static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
3278{
3279 struct sample tmp;
3280 int max, result;
3281
3282 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3283 if (arg_p[0].type != ARGT_VAR)
3284 return 0;
3285 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
3286 return 0;
3287 if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp))
3288 return 0;
3289
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003290 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
3291 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003292 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003293 if (smp->data.u.str.data != tmp.data.u.str.data) {
3294 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02003295 result = -1;
3296 }
3297 else {
3298 result = 1;
3299 }
3300 }
3301 }
3302
3303 smp->data.u.sint = result;
3304 smp->data.type = SMP_T_SINT;
3305 return 1;
3306}
3307
Willy Tarreau2a8a2f02021-05-09 11:59:49 +02003308#if defined(HAVE_CRYPTO_memcmp)
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003309/* Compares bytestring with a variable containing a bytestring. Return value
3310 * is `true` if both bytestrings are bytewise identical and `false` otherwise.
3311 *
3312 * Comparison will be performed in constant time if both bytestrings are of
3313 * the same length. If the lengths differ execution time will not be constant.
3314 */
3315static int sample_conv_secure_memcmp(const struct arg *arg_p, struct sample *smp, void *private)
3316{
3317 struct sample tmp;
3318 int result;
3319
3320 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3321 if (arg_p[0].type != ARGT_VAR)
3322 return 0;
3323 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
3324 return 0;
3325 if (!sample_casts[tmp.data.type][SMP_T_BIN](&tmp))
3326 return 0;
3327
3328 if (smp->data.u.str.data != tmp.data.u.str.data) {
3329 smp->data.u.sint = 0;
3330 smp->data.type = SMP_T_BOOL;
3331 return 1;
3332 }
3333
3334 /* The following comparison is performed in constant time. */
3335 result = CRYPTO_memcmp(smp->data.u.str.area, tmp.data.u.str.area, smp->data.u.str.data);
3336
3337 smp->data.u.sint = result == 0;
3338 smp->data.type = SMP_T_BOOL;
3339 return 1;
3340}
3341#endif
3342
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02003343/* Takes a boolean as input. Returns the first argument if that boolean is true and
3344 * the second argument otherwise.
3345 */
3346static int sample_conv_iif(const struct arg *arg_p, struct sample *smp, void *private)
3347{
3348 smp->data.type = SMP_T_STR;
3349 smp->flags |= SMP_F_CONST;
3350
3351 if (smp->data.u.sint) {
3352 smp->data.u.str.data = arg_p[0].data.str.data;
3353 smp->data.u.str.area = arg_p[0].data.str.area;
3354 }
3355 else {
3356 smp->data.u.str.data = arg_p[1].data.str.data;
3357 smp->data.u.str.area = arg_p[1].data.str.area;
3358 }
3359
3360 return 1;
3361}
3362
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003363#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
3364#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
3365#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
3366
3367/*
3368 * Extract the field value of an input binary sample. Takes a mandatory argument:
3369 * the protocol buffers field identifier (dotted notation) internally represented
3370 * as an array of unsigned integers and its size.
3371 * Return 1 if the field was found, 0 if not.
3372 */
3373static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
3374{
3375 unsigned char *pos;
3376 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003377
3378 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003379 grpc_left = smp->data.u.str.data;
3380
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01003381 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003382 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003383
3384 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
3385
3386 pos += GRPC_MSG_HEADER_SZ;
3387 grpc_left -= GRPC_MSG_HEADER_SZ;
3388
3389 if (grpc_left < left)
3390 return 0;
3391
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01003392 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
3393 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003394
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003395 grpc_left -= grpc_msg_len;
3396 }
3397
3398 return 0;
3399}
3400
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003401static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
3402{
3403 unsigned char *pos;
3404 size_t left;
3405
3406 pos = (unsigned char *)smp->data.u.str.area;
3407 left = smp->data.u.str.data;
3408
3409 return protobuf_field_lookup(arg_p, smp, &pos, &left);
3410}
3411
3412static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
3413 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003414{
3415 if (!args[1].type) {
3416 args[1].type = ARGT_SINT;
3417 args[1].data.sint = PBUF_T_BINARY;
3418 }
3419 else {
3420 int pbuf_type;
3421
3422 pbuf_type = protobuf_type(args[1].data.str.area);
3423 if (pbuf_type == -1) {
3424 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
3425 return 0;
3426 }
3427
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003428 chunk_destroy(&args[1].data.str);
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003429 args[1].type = ARGT_SINT;
3430 args[1].data.sint = pbuf_type;
3431 }
3432
3433 return 1;
3434}
3435
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003436/*
3437 * Extract the tag value of an input binary sample. Takes a mandatory argument:
3438 * the FIX protocol tag identifier.
3439 * Return 1 if the tag was found, 0 if not.
3440 */
3441static int sample_conv_fix_tag_value(const struct arg *arg_p, struct sample *smp, void *private)
3442{
3443 struct ist value;
3444
3445 smp->flags &= ~SMP_F_MAY_CHANGE;
3446 value = fix_tag_value(ist2(smp->data.u.str.area, smp->data.u.str.data),
3447 arg_p[0].data.sint);
3448 if (!istlen(value)) {
3449 if (!isttest(value)) {
3450 /* value != IST_NULL, need more data */
3451 smp->flags |= SMP_F_MAY_CHANGE;
3452 }
3453 return 0;
3454 }
3455
3456 smp->data.u.str = ist2buf(value);
3457 smp->flags |= SMP_F_CONST;
3458
3459 return 1;
3460}
3461
3462/* This function checks the "fix_tag_value" converter configuration.
3463 * It expects a "known" (by HAProxy) tag name or ID.
3464 * Tag string names are converted to their ID counterpart because this is the
3465 * format they are sent over the wire.
3466 */
3467static int sample_conv_fix_value_check(struct arg *args, struct sample_conv *conv,
3468 const char *file, int line, char **err)
3469{
3470 struct ist str;
3471 unsigned int tag;
3472
3473 str = ist2(args[0].data.str.area, args[0].data.str.data);
3474 tag = fix_tagid(str);
3475 if (!tag) {
3476 memprintf(err, "Unknown FIX tag name '%s'", args[0].data.str.area);
3477 return 0;
3478 }
3479
3480 chunk_destroy(&args[0].data.str);
3481 args[0].type = ARGT_SINT;
3482 args[0].data.sint = tag;
3483
3484 return 1;
3485}
3486
3487/*
3488 * Checks that a buffer contains a valid FIX message
3489 *
3490 * Return 1 if the check could be run, 0 if not.
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003491 * The result of the analyse itself is stored in <smp> as a boolean
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003492 */
3493static int sample_conv_fix_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3494{
3495 struct ist msg;
3496
3497 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3498
3499 smp->flags &= ~SMP_F_MAY_CHANGE;
3500 switch (fix_validate_message(msg)) {
3501 case FIX_VALID_MESSAGE:
3502 smp->data.type = SMP_T_BOOL;
3503 smp->data.u.sint = 1;
3504 return 1;
3505 case FIX_NEED_MORE_DATA:
3506 smp->flags |= SMP_F_MAY_CHANGE;
3507 return 0;
3508 case FIX_INVALID_MESSAGE:
3509 smp->data.type = SMP_T_BOOL;
3510 smp->data.u.sint = 0;
3511 return 1;
3512 }
3513 return 0;
3514}
3515
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003516/*
3517 * Extract the field value of an input binary sample containing an MQTT packet.
3518 * Takes 2 mandatory arguments:
3519 * - packet type
3520 * - field name
3521 *
3522 * return 1 if the field was found, 0 if not.
3523 */
3524static int sample_conv_mqtt_field_value(const struct arg *arg_p, struct sample *smp, void *private)
3525{
3526 struct ist pkt, value;
3527 int type, fieldname_id;
3528
3529 pkt = ist2(smp->data.u.str.area, smp->data.u.str.data);
3530 type = arg_p[0].data.sint;
3531 fieldname_id = arg_p[1].data.sint;
3532
3533 smp->flags &= ~SMP_F_MAY_CHANGE;
3534 value = mqtt_field_value(pkt, type, fieldname_id);
3535 if (!istlen(value)) {
3536 if (isttest(value)) {
3537 /* value != IST_NULL, need more data */
3538 smp->flags |= SMP_F_MAY_CHANGE;
3539 }
3540 return 0;
3541 }
3542
3543 smp->data.u.str = ist2buf(value);
3544 smp->flags |= SMP_F_CONST;
3545 return 1;
3546}
3547
3548/*
3549 * this function checks the "mqtt_field_value" converter configuration.
3550 * It expects a known packet type name or ID and a field name, in this order
3551 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003552 * Args[0] will be turned into a MQTT_CPT_* value for direct matching when parsing
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003553 * a packet.
3554 */
3555static int sample_conv_mqtt_field_value_check(struct arg *args, struct sample_conv *conv,
3556 const char *file, int line, char **err)
3557{
3558 int type, fieldname_id;
3559
3560 /* check the MQTT packet type is valid */
3561 type = mqtt_typeid(ist2(args[0].data.str.area, args[0].data.str.data));
3562 if (type == MQTT_CPT_INVALID) {
3563 memprintf(err, "Unknown MQTT type '%s'", args[0].data.str.area);
3564 return 0;
3565 }
3566
3567 /* check the field name belongs to the MQTT packet type */
3568 fieldname_id = mqtt_check_type_fieldname(type, ist2(args[1].data.str.area, args[1].data.str.data));
3569 if (fieldname_id == MQTT_FN_INVALID) {
3570 memprintf(err, "Unknown MQTT field name '%s' for packet type '%s'", args[1].data.str.area,
3571 args[0].data.str.area);
3572 return 0;
3573 }
3574
3575 /* save numeric counterparts of type and field name */
3576 chunk_destroy(&args[0].data.str);
3577 chunk_destroy(&args[1].data.str);
3578 args[0].type = ARGT_SINT;
3579 args[0].data.sint = type;
3580 args[1].type = ARGT_SINT;
3581 args[1].data.sint = fieldname_id;
3582
3583 return 1;
3584}
3585
3586/*
3587 * Checks that <smp> contains a valid MQTT message
3588 *
3589 * The function returns 1 if the check was run to its end, 0 otherwise.
3590 * The result of the analyse itself is stored in <smp> as a boolean.
3591 */
3592static int sample_conv_mqtt_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3593{
3594 struct ist msg;
3595
3596 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3597
3598 smp->flags &= ~SMP_F_MAY_CHANGE;
3599 switch (mqtt_validate_message(msg, NULL)) {
3600 case FIX_VALID_MESSAGE:
3601 smp->data.type = SMP_T_BOOL;
3602 smp->data.u.sint = 1;
3603 return 1;
3604 case FIX_NEED_MORE_DATA:
3605 smp->flags |= SMP_F_MAY_CHANGE;
3606 return 0;
3607 case FIX_INVALID_MESSAGE:
3608 smp->data.type = SMP_T_BOOL;
3609 smp->data.u.sint = 0;
3610 return 1;
3611 }
3612 return 0;
3613}
3614
Tim Duesterhusca097c12018-04-27 21:18:45 +02003615/* This function checks the "strcmp" converter's arguments and extracts the
3616 * variable name and its scope.
3617 */
3618static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
3619 const char *file, int line, char **err)
3620{
Willy Tarreaua1169b62021-05-08 06:50:28 +02003621 if (!args[0].data.str.data) {
3622 memprintf(err, "missing variable name");
3623 return 0;
3624 }
3625
Tim Duesterhusca097c12018-04-27 21:18:45 +02003626 /* Try to decode a variable. */
3627 if (vars_check_arg(&args[0], NULL))
3628 return 1;
3629
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003630 memprintf(err, "failed to register variable name '%s'",
3631 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003632 return 0;
3633}
3634
Willy Tarreau2a8a2f02021-05-09 11:59:49 +02003635#if defined(HAVE_CRYPTO_memcmp)
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003636/* This function checks the "secure_memcmp" converter's arguments and extracts the
3637 * variable name and its scope.
3638 */
3639static int smp_check_secure_memcmp(struct arg *args, struct sample_conv *conv,
3640 const char *file, int line, char **err)
3641{
Willy Tarreaua1169b62021-05-08 06:50:28 +02003642 if (!args[0].data.str.data) {
3643 memprintf(err, "missing variable name");
3644 return 0;
3645 }
3646
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003647 /* Try to decode a variable. */
3648 if (vars_check_arg(&args[0], NULL))
3649 return 1;
3650
3651 memprintf(err, "failed to register variable name '%s'",
3652 args[0].data.str.area);
3653 return 0;
3654}
3655#endif
3656
Christopher Faulet4ccc12f2020-04-01 09:08:32 +02003657/**/
3658static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private)
3659{
3660 struct buffer *tmp;
3661 uint32_t n;
3662
3663 n = htonl((uint32_t)smp->data.u.sint);
3664 tmp = get_trash_chunk();
3665
3666 memcpy(b_head(tmp), &n, 4);
3667 b_add(tmp, 4);
3668
3669 smp->data.u.str = *tmp;
3670 smp->data.type = SMP_T_BIN;
3671 return 1;
3672}
3673
Christopher Fauletea159d62020-04-01 16:21:44 +02003674/**/
3675static int sample_conv_cut_crlf(const struct arg *arg_p, struct sample *smp, void *private)
3676{
3677 char *p;
3678 size_t l;
3679
3680 p = smp->data.u.str.area;
3681 for (l = 0; l < smp->data.u.str.data; l++) {
3682 if (*(p+l) == '\r' || *(p+l) == '\n')
3683 break;
3684 }
3685 smp->data.u.str.data = l;
3686 return 1;
3687}
3688
Christopher Faulet51fc9d12020-04-01 17:24:41 +02003689/**/
3690static int sample_conv_ltrim(const struct arg *arg_p, struct sample *smp, void *private)
3691{
3692 char *delimiters, *p;
3693 size_t dlen, l;
3694
3695 delimiters = arg_p[0].data.str.area;
3696 dlen = arg_p[0].data.str.data;
3697
3698 l = smp->data.u.str.data;
3699 p = smp->data.u.str.area;
3700 while (l && memchr(delimiters, *p, dlen) != NULL) {
3701 p++;
3702 l--;
3703 }
3704
3705 smp->data.u.str.area = p;
3706 smp->data.u.str.data = l;
3707 return 1;
3708}
3709
Christopher Faulet568415a2020-04-01 17:24:47 +02003710/**/
3711static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *private)
3712{
3713 char *delimiters, *p;
3714 size_t dlen, l;
3715
3716 delimiters = arg_p[0].data.str.area;
3717 dlen = arg_p[0].data.str.data;
3718
3719 l = smp->data.u.str.data;
3720 p = smp->data.u.str.area + l - 1;
3721 while (l && memchr(delimiters, *p, dlen) != NULL) {
3722 p--;
3723 l--;
3724 }
3725
3726 smp->data.u.str.data = l;
3727 return 1;
3728}
3729
Alex51c8ad42021-04-15 16:45:15 +02003730/* This function checks the "json_query" converter's arguments. */
3731static int sample_check_json_query(struct arg *arg, struct sample_conv *conv,
3732 const char *file, int line, char **err)
3733{
3734 if (arg[0].data.str.data == 0) {
3735 memprintf(err, "json_path must not be empty");
3736 return 0;
3737 }
3738
3739 if (arg[1].data.str.data != 0) {
3740 if (strcmp(arg[1].data.str.area, "int") != 0) {
3741 memprintf(err, "output_type only supports \"int\" as argument");
3742 return 0;
3743 } else {
3744 arg[1].type = ARGT_SINT;
3745 arg[1].data.sint = 0;
3746 }
3747 }
3748 return 1;
3749}
3750
3751/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
3752 * the recommendation for interoperable integers in section 6 of RFC 7159.
3753 */
3754#define JSON_INT_MAX ((1LL << 53) - 1)
3755#define JSON_INT_MIN (-JSON_INT_MAX)
3756
3757/* This sample function get the value from a given json string.
3758 * The mjson library is used to parse the JSON struct
3759 */
3760static int sample_conv_json_query(const struct arg *args, struct sample *smp, void *private)
3761{
3762 struct buffer *trash = get_trash_chunk();
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003763 const char *token; /* holds the temporary string from mjson_find */
3764 int token_size; /* holds the length of <token> */
Alex51c8ad42021-04-15 16:45:15 +02003765
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003766 enum mjson_tok token_type;
Alex51c8ad42021-04-15 16:45:15 +02003767
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003768 token_type = mjson_find(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &token, &token_size);
3769
3770 switch (token_type) {
Alex51c8ad42021-04-15 16:45:15 +02003771 case MJSON_TOK_NUMBER:
3772 if (args[1].type == ARGT_SINT) {
Willy Tarreaufb601952021-05-14 08:51:53 +02003773 smp->data.u.sint = strtoll(token, NULL, 0);
Alex51c8ad42021-04-15 16:45:15 +02003774
3775 if (smp->data.u.sint < JSON_INT_MIN || smp->data.u.sint > JSON_INT_MAX)
3776 return 0;
3777
3778 smp->data.type = SMP_T_SINT;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003779
3780 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003781 } else {
3782 double double_val;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003783
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003784 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 +02003785 return 0;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003786
3787 trash->data = snprintf(trash->area,trash->size,"%g",double_val);
3788 smp->data.u.str = *trash;
3789 smp->data.type = SMP_T_STR;
3790
3791 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003792 }
Alex51c8ad42021-04-15 16:45:15 +02003793 case MJSON_TOK_TRUE:
3794 smp->data.type = SMP_T_BOOL;
3795 smp->data.u.sint = 1;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003796
3797 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003798 case MJSON_TOK_FALSE:
3799 smp->data.type = SMP_T_BOOL;
3800 smp->data.u.sint = 0;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003801
3802 return 1;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003803 case MJSON_TOK_STRING: {
3804 int len;
3805
3806 len = mjson_get_string(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, trash->area, trash->size);
3807
3808 if (len == -1) {
Alex51c8ad42021-04-15 16:45:15 +02003809 /* invalid string */
3810 return 0;
Alex51c8ad42021-04-15 16:45:15 +02003811 }
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003812
3813 trash->data = len;
3814 smp->data.u.str = *trash;
3815 smp->data.type = SMP_T_STR;
3816
3817 return 1;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003818 }
Tim Duesterhus8f3bc8f2021-04-15 18:14:32 +02003819 case MJSON_TOK_NULL:
3820 case MJSON_TOK_ARRAY:
3821 case MJSON_TOK_OBJECT:
3822 /* We cannot handle these. */
3823 return 0;
3824 case MJSON_TOK_INVALID:
3825 /* Nothing matches the query. */
3826 return 0;
3827 case MJSON_TOK_KEY:
3828 /* This is not a valid return value according to the
3829 * mjson documentation, but we handle it to benefit
3830 * from '-Wswitch'.
3831 */
Alex51c8ad42021-04-15 16:45:15 +02003832 return 0;
3833 }
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003834
3835 my_unreachable();
3836 return 0;
Alex51c8ad42021-04-15 16:45:15 +02003837}
3838
3839
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003840/************************************************************************/
3841/* All supported sample fetch functions must be declared here */
3842/************************************************************************/
3843
3844/* force TRUE to be returned at the fetch level */
3845static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003846smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003847{
Willy Tarreau280f42b2018-02-19 15:34:12 +01003848 if (!smp_make_rw(smp))
3849 return 0;
3850
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 = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003853 return 1;
3854}
3855
3856/* force FALSE to be returned at the fetch level */
3857static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003858smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003859{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003860 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003861 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003862 return 1;
3863}
3864
3865/* retrieve environment variable $1 as a string */
3866static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003867smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003868{
3869 char *env;
3870
Christopher Faulete6e7a582021-01-29 11:29:28 +01003871 if (args[0].type != ARGT_STR)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003872 return 0;
3873
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003874 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003875 if (!env)
3876 return 0;
3877
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003878 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003879 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003880 smp->data.u.str.area = env;
3881 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003882 return 1;
3883}
3884
Damien Claisseae6f1252019-10-30 15:57:28 +00003885/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3886 * optional string representing the unit of the result: "s" for seconds, "ms" for
3887 * milliseconds and "us" for microseconds.
3888 * Returns 0 on error and non-zero if OK.
3889 */
3890int smp_check_date_unit(struct arg *args, char **err)
3891{
3892 if (args[1].type == ARGT_STR) {
Christopher Faulet95917132020-08-05 23:07:07 +02003893 long long int unit;
3894
Damien Claisseae6f1252019-10-30 15:57:28 +00003895 if (strcmp(args[1].data.str.area, "s") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003896 unit = TIME_UNIT_S;
Damien Claisseae6f1252019-10-30 15:57:28 +00003897 }
3898 else if (strcmp(args[1].data.str.area, "ms") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003899 unit = TIME_UNIT_MS;
Damien Claisseae6f1252019-10-30 15:57:28 +00003900 }
3901 else if (strcmp(args[1].data.str.area, "us") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003902 unit = TIME_UNIT_US;
Damien Claisseae6f1252019-10-30 15:57:28 +00003903 }
3904 else {
3905 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
3906 args[1].data.str.area);
3907 return 0;
3908 }
Christopher Faulet95917132020-08-05 23:07:07 +02003909
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003910 chunk_destroy(&args[1].data.str);
Damien Claisseae6f1252019-10-30 15:57:28 +00003911 args[1].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02003912 args[1].data.sint = unit;
Damien Claisseae6f1252019-10-30 15:57:28 +00003913 }
3914 else if (args[1].type != ARGT_STOP) {
3915 memprintf(err, "Unexpected arg type");
3916 return 0;
3917 }
3918
3919 return 1;
3920}
3921
3922/* retrieve the current local date in epoch time, converts it to milliseconds
3923 * or microseconds if asked to in optional args[1] unit param, and applies an
3924 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003925 */
3926static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003927smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003928{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003929 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003930
Damien Claisseae6f1252019-10-30 15:57:28 +00003931 /* report in milliseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003932 if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003933 smp->data.u.sint *= 1000;
3934 smp->data.u.sint += date.tv_usec / 1000;
3935 }
3936 /* report in microseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003937 else if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003938 smp->data.u.sint *= 1000000;
3939 smp->data.u.sint += date.tv_usec;
3940 }
3941
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003942 /* add offset */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003943 if (args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003944 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003945
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003946 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003947 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3948 return 1;
3949}
3950
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003951/* retrieve the current microsecond part of the date */
3952static int
3953smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3954{
3955 smp->data.u.sint = date.tv_usec;
3956 smp->data.type = SMP_T_SINT;
3957 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3958 return 1;
3959}
3960
3961
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003962/* returns the hostname */
3963static int
3964smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3965{
3966 smp->data.type = SMP_T_STR;
3967 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003968 smp->data.u.str.area = hostname;
3969 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003970 return 1;
3971}
3972
Willy Tarreau0f30d262014-11-24 16:02:05 +01003973/* returns the number of processes */
3974static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003975smp_fetch_nbproc(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 = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003979 return 1;
3980}
3981
3982/* returns the number of the current process (between 1 and nbproc */
3983static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003984smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003985{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003986 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003987 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003988 return 1;
3989}
3990
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003991/* returns the number of the current thread (between 1 and nbthread */
3992static int
3993smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3994{
3995 smp->data.type = SMP_T_SINT;
3996 smp->data.u.sint = tid;
3997 return 1;
3998}
3999
Willy Tarreau84310e22014-02-14 11:59:04 +01004000/* generate a random 32-bit integer for whatever purpose, with an optional
4001 * range specified in argument.
4002 */
4003static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004004smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01004005{
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01004006 smp->data.u.sint = ha_random32();
Willy Tarreau84310e22014-02-14 11:59:04 +01004007
4008 /* reduce if needed. Don't do a modulo, use all bits! */
Christopher Faulete6e7a582021-01-29 11:29:28 +01004009 if (args[0].type == ARGT_SINT)
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01004010 smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32;
Willy Tarreau84310e22014-02-14 11:59:04 +01004011
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004012 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01004013 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4014 return 1;
4015}
4016
Willy Tarreau0f30d262014-11-24 16:02:05 +01004017/* returns true if the current process is stopping */
4018static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004019smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01004020{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004021 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004022 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01004023 return 1;
4024}
4025
Willy Tarreau70fe9442018-11-22 16:07:39 +01004026/* returns the number of calls of the current stream's process_stream() */
4027static int
4028smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
4029{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004030 if (!smp->strm)
4031 return 0;
4032
Willy Tarreau70fe9442018-11-22 16:07:39 +01004033 smp->data.type = SMP_T_SINT;
4034 smp->data.u.sint = smp->strm->task->calls;
4035 return 1;
4036}
4037
4038/* returns the average number of nanoseconds spent processing the stream per call */
4039static int
4040smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
4041{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004042 if (!smp->strm)
4043 return 0;
4044
Willy Tarreau70fe9442018-11-22 16:07:39 +01004045 smp->data.type = SMP_T_SINT;
4046 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
4047 return 1;
4048}
4049
4050/* returns the total number of nanoseconds spent processing the stream */
4051static int
4052smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
4053{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004054 if (!smp->strm)
4055 return 0;
4056
Willy Tarreau70fe9442018-11-22 16:07:39 +01004057 smp->data.type = SMP_T_SINT;
4058 smp->data.u.sint = smp->strm->task->cpu_time;
4059 return 1;
4060}
4061
4062/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
4063static int
4064smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
4065{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004066 if (!smp->strm)
4067 return 0;
4068
Willy Tarreau70fe9442018-11-22 16:07:39 +01004069 smp->data.type = SMP_T_SINT;
4070 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
4071 return 1;
4072}
4073
4074/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
4075static int
4076smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
4077{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004078 if (!smp->strm)
4079 return 0;
4080
Willy Tarreau70fe9442018-11-22 16:07:39 +01004081 smp->data.type = SMP_T_SINT;
4082 smp->data.u.sint = smp->strm->task->lat_time;
4083 return 1;
4084}
4085
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004086static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
4087{
4088 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004089 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004090 smp->data.u.str.area = args[0].data.str.area;
4091 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004092 return 1;
4093}
4094
4095static int smp_check_const_bool(struct arg *args, char **err)
4096{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004097 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
4098 strcasecmp(args[0].data.str.area, "1") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004099 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004100 args[0].type = ARGT_SINT;
4101 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004102 return 1;
4103 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004104 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
4105 strcasecmp(args[0].data.str.area, "0") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004106 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004107 args[0].type = ARGT_SINT;
4108 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004109 return 1;
4110 }
4111 memprintf(err, "Expects 'true', 'false', '0' or '1'");
4112 return 0;
4113}
4114
4115static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
4116{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004117 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004118 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004119 return 1;
4120}
4121
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02004122static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004123{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004124 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004125 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004126 return 1;
4127}
4128
4129static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
4130{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004131 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004132 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004133 return 1;
4134}
4135
4136static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
4137{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004138 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004139 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004140 return 1;
4141}
4142
4143static int smp_check_const_bin(struct arg *args, char **err)
4144{
David Carlier64a16ab2016-04-08 10:37:02 +01004145 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004146 int binstrlen;
4147
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004148 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004149 return 0;
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004150 chunk_destroy(&args[0].data.str);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004151 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004152 args[0].data.str.area = binstr;
4153 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004154 return 1;
4155}
4156
4157static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
4158{
4159 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004160 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004161 smp->data.u.str.area = args[0].data.str.area;
4162 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004163 return 1;
4164}
4165
4166static int smp_check_const_meth(struct arg *args, char **err)
4167{
4168 enum http_meth_t meth;
4169 int i;
4170
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004171 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004172 if (meth != HTTP_METH_OTHER) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004173 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004174 args[0].type = ARGT_SINT;
4175 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004176 } else {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05004177 /* Check method avalaibility. A method is a token defined as :
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004178 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
4179 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
4180 * token = 1*tchar
4181 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004182 for (i = 0; i < args[0].data.str.data; i++) {
4183 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004184 memprintf(err, "expects valid method.");
4185 return 0;
4186 }
4187 }
4188 }
4189 return 1;
4190}
4191
4192static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
4193{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004194 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004195 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004196 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004197 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004198 smp->data.u.meth.str.area = "";
4199 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004200 } else {
4201 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004202 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004203 smp->data.u.meth.str.area = args[0].data.str.area;
4204 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004205 }
4206 return 1;
4207}
4208
Luca Schimweg8a694b82019-09-10 15:42:52 +02004209// This function checks the "uuid" sample's arguments.
4210// Function won't get called when no parameter is specified (maybe a bug?)
4211static int smp_check_uuid(struct arg *args, char **err)
4212{
4213 if (!args[0].type) {
4214 args[0].type = ARGT_SINT;
4215 args[0].data.sint = 4;
4216 }
4217 else if (args[0].data.sint != 4) {
4218 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
4219 return 0;
4220 }
4221
4222 return 1;
4223}
4224
4225// Generate a RFC4122 UUID (default is v4 = fully random)
4226static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
4227{
4228 if (args[0].data.sint == 4 || !args[0].type) {
Willy Tarreauee3bcdd2020-03-08 17:48:17 +01004229 ha_generate_uuid(&trash);
Luca Schimweg8a694b82019-09-10 15:42:52 +02004230 smp->data.type = SMP_T_STR;
4231 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4232 smp->data.u.str = trash;
4233 return 1;
4234 }
4235
4236 // more implementations of other uuid formats possible here
4237 return 0;
4238}
4239
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004240/* Note: must not be declared <const> as its list will be overwritten.
4241 * Note: fetches that may return multiple types must be declared as the lowest
4242 * common denominator, the type that can be casted into all other ones. For
4243 * instance IPv4/IPv6 must be declared IPv4.
4244 */
4245static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau0209c972021-03-26 12:03:11 +01004246 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4247 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4248 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_CONST },
4249 { "date", smp_fetch_date, ARG2(0,SINT,STR), smp_check_date_unit, SMP_T_SINT, SMP_USE_CONST },
4250 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4251 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_CONST },
4252 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_CONST },
4253 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4254 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4255 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_CONST },
Willy Tarreau0f30d262014-11-24 16:02:05 +01004256 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau0209c972021-03-26 12:03:11 +01004257 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_CONST },
Willy Tarreau70fe9442018-11-22 16:07:39 +01004258
4259 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4260 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4261 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4262 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4263 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004264
Willy Tarreau0209c972021-03-26 12:03:11 +01004265 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_CONST },
4266 { "bool", smp_fetch_const_bool, ARG1(1,STR), smp_check_const_bool, SMP_T_BOOL, SMP_USE_CONST },
4267 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_CONST },
4268 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_CONST },
4269 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_CONST },
4270 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_CONST },
4271 { "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 +02004272
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004273 { /* END */ },
4274}};
4275
Willy Tarreau0108d902018-11-25 19:14:37 +01004276INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
4277
Emeric Brun107ca302010-01-04 16:16:05 +01004278/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02004279static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Moemen MHEDHBI848216f2021-04-02 01:05:07 +02004280 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
4281 { "b64dec", sample_conv_base642bin, 0, NULL, SMP_T_STR, SMP_T_BIN },
4282 { "base64", sample_conv_bin2base64, 0, NULL, SMP_T_BIN, SMP_T_STR },
4283 { "ub64enc", sample_conv_bin2base64url,0, NULL, SMP_T_BIN, SMP_T_STR },
4284 { "ub64dec", sample_conv_base64url2bin,0, NULL, SMP_T_STR, SMP_T_BIN },
4285 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
4286 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
4287 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
4288 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
4289 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
4290 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
4291 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4292 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4293 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4294 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4295 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4296 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4297 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4298 { "xxh3", sample_conv_xxh3, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4299 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4300 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4301 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
4302 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
4303 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4304 { "word", sample_conv_word, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4305 { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
4306 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02004307#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01004308 { "sha2", sample_conv_sha2, ARG1(0, SINT), smp_check_sha2, SMP_T_BIN, SMP_T_BIN },
Ilya Shipitsin2c481d02021-03-26 23:35:31 +05004309#ifdef EVP_CIPH_GCM_MODE
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02004310 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
4311#endif
Patrick Gansterer8e366512020-04-22 16:47:57 +02004312 { "digest", sample_conv_crypto_digest, ARG1(1,STR), check_crypto_digest, SMP_T_BIN, SMP_T_BIN },
4313 { "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 +02004314#endif
Willy Tarreau280f42b2018-02-19 15:34:12 +01004315 { "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 +02004316 { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT },
Willy Tarreau2a8a2f02021-05-09 11:59:49 +02004317#if defined(HAVE_CRYPTO_memcmp)
Tim Duesterhusf38175c2020-06-09 11:48:42 +02004318 { "secure_memcmp", sample_conv_secure_memcmp, ARG1(1,STR), smp_check_secure_memcmp, SMP_T_BIN, SMP_T_BOOL },
4319#endif
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01004320
4321 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01004322 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
4323 { "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 +01004324
Baptiste Assmanne138dda2020-10-22 15:39:03 +02004325 /* FIX converters */
4326 { "fix_is_valid", sample_conv_fix_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4327 { "fix_tag_value", sample_conv_fix_tag_value, ARG1(1,STR), sample_conv_fix_value_check, SMP_T_BIN, SMP_T_BIN },
4328
Baptiste Assmanne279ca62020-10-27 18:10:06 +01004329 /* MQTT converters */
4330 { "mqtt_is_valid", sample_conv_mqtt_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4331 { "mqtt_field_value", sample_conv_mqtt_field_value, ARG2(2,STR,STR), sample_conv_mqtt_field_value_check, SMP_T_BIN, SMP_T_STR },
4332
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02004333 { "iif", sample_conv_iif, ARG2(2, STR, STR), NULL, SMP_T_BOOL, SMP_T_STR },
4334
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02004335 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4336 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4337 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4338 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
4339 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4340 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4341 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4342 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4343 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4344 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4345 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4346 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4347 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4348 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01004349
Christopher Fauletea159d62020-04-01 16:21:44 +02004350 { "htonl", sample_conv_htonl, 0, NULL, SMP_T_SINT, SMP_T_BIN },
4351 { "cut_crlf", sample_conv_cut_crlf, 0, NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet51fc9d12020-04-01 17:24:41 +02004352 { "ltrim", sample_conv_ltrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet568415a2020-04-01 17:24:47 +02004353 { "rtrim", sample_conv_rtrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Alex51c8ad42021-04-15 16:45:15 +02004354 { "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 +02004355 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01004356}};
4357
Willy Tarreau0108d902018-11-25 19:14:37 +01004358INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);