blob: 00b820241123e8e9d95bc8b9a17860e2cdd590b0 [file] [log] [blame]
Emeric Brun107ca302010-01-04 16:16:05 +01001/*
Willy Tarreaucd3b0942012-04-27 21:52:18 +02002 * Sample management functions.
Emeric Brun107ca302010-01-04 16:16:05 +01003 *
4 * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreaucd3b0942012-04-27 21:52:18 +02005 * Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
Emeric Brun107ca302010-01-04 16:16:05 +01006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Thierry FOURNIER317e1c42014-08-12 10:20:47 +020014#include <ctype.h>
Emeric Brun107ca302010-01-04 16:16:05 +010015#include <string.h>
16#include <arpa/inet.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020017#include <stdio.h>
Emeric Brun107ca302010-01-04 16:16:05 +010018
Alex51c8ad42021-04-15 16:45:15 +020019#include <import/mjson.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <import/sha1.h>
Willy Tarreau7e2c6472012-10-29 20:44:36 +010021
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/api.h>
23#include <haproxy/arg.h>
Willy Tarreauac13aea2020-06-04 10:36:03 +020024#include <haproxy/auth.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020025#include <haproxy/base64.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020026#include <haproxy/buf.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020027#include <haproxy/chunk.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020028#include <haproxy/errors.h>
Baptiste Assmanne138dda2020-10-22 15:39:03 +020029#include <haproxy/fix.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020030#include <haproxy/global.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020031#include <haproxy/hash.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020032#include <haproxy/http.h>
Baptiste Assmanne138dda2020-10-22 15:39:03 +020033#include <haproxy/istbuf.h>
Baptiste Assmanne279ca62020-10-27 18:10:06 +010034#include <haproxy/mqtt.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020035#include <haproxy/net_helper.h>
Willy Tarreaub23e5952020-06-04 16:06:59 +020036#include <haproxy/protobuf.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020037#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020038#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020039#include <haproxy/sample.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020040#include <haproxy/sink.h>
41#include <haproxy/stick_table.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020042#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020043#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020044#include <haproxy/vars.h>
Tim Duesterhusd5fc8fc2021-09-11 17:51:13 +020045#include <haproxy/xxhash.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 Faulet35926a12021-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 Faulet35926a12021-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 Faulet35926a12021-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 Tarreau57467b82021-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 Tarreau57467b82021-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
Amaury Denoyelle06269612021-08-23 14:05:07 +02001230 srv->flags |= SRV_F_NON_PURGEABLE;
1231
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001232 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001233 arg->unresolved = 0;
1234 arg->data.srv = srv;
1235 break;
1236
1237 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001238 if (arg->data.str.data) {
1239 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001240 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001241 }
1242
1243 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001244 memprintf(err, "%sparsing [%s:%d]: unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1245 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001246 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001247 cfgerr++;
1248 break;
1249 }
1250
1251 if (!(px->cap & PR_CAP_FE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001252 memprintf(err, "%sparsing [%s:%d]: proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1253 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001254 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001255 cfgerr++;
1256 break;
1257 }
1258
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001259 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001260 arg->unresolved = 0;
1261 arg->data.prx = px;
1262 break;
1263
1264 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001265 if (arg->data.str.data) {
1266 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001267 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001268 }
1269
1270 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001271 memprintf(err, "%sparsing [%s:%d]: unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1272 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001273 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001274 cfgerr++;
1275 break;
1276 }
1277
1278 if (!(px->cap & PR_CAP_BE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001279 memprintf(err, "%sparsing [%s:%d]: proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1280 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001281 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001282 cfgerr++;
1283 break;
1284 }
1285
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001286 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001287 arg->unresolved = 0;
1288 arg->data.prx = px;
1289 break;
1290
1291 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001292 if (arg->data.str.data)
1293 stktname = arg->data.str.area;
1294 else
1295 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001296
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001297 t = stktable_find_by_name(stktname);
1298 if (!t) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001299 memprintf(err, "%sparsing [%s:%d]: unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1300 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001301 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001302 cfgerr++;
1303 break;
1304 }
1305
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001306 if (!t->size) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001307 memprintf(err, "%sparsing [%s:%d]: no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1308 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001309 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001310 cfgerr++;
1311 break;
1312 }
1313
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001314 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001315 p->next_stkt_ref = t->proxies_list;
1316 t->proxies_list = p;
1317 }
1318
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001319 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001320 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001321 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001322 break;
1323
1324 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001325 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001326 memprintf(err, "%sparsing [%s:%d]: missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1327 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001328 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001329 cfgerr++;
1330 break;
1331 }
1332
1333 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001334 strcmp(p->uri_auth->userlist->name, arg->data.str.area) == 0)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001335 ul = p->uri_auth->userlist;
1336 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001337 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001338
1339 if (!ul) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001340 memprintf(err, "%sparsing [%s:%d]: unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1341 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001342 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001343 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001344 cfgerr++;
1345 break;
1346 }
1347
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001348 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001349 arg->unresolved = 0;
1350 arg->data.usr = ul;
1351 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001352
1353 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001354 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001355 memprintf(err, "%sparsing [%s:%d]: missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1356 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001357 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001358 cfgerr++;
1359 continue;
1360 }
1361
Willy Tarreau46947782015-01-19 19:00:58 +01001362 rflags = 0;
1363 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001364 err2 = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001365
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001366 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err2))) {
1367 memprintf(err, "%sparsing [%s:%d]: error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1368 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001369 arg->data.str.area,
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001370 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err2);
Willy Tarreau46947782015-01-19 19:00:58 +01001371 cfgerr++;
1372 continue;
1373 }
1374
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001375 chunk_destroy(&arg->data.str);
Willy Tarreau46947782015-01-19 19:00:58 +01001376 arg->unresolved = 0;
1377 arg->data.reg = reg;
1378 break;
1379
1380
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001381 }
1382
Willy Tarreau2b718102021-04-21 07:32:39 +02001383 LIST_DELETE(&cur->list);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001384 free(cur);
1385 } /* end of args processing */
1386
1387 return cfgerr;
1388}
1389
1390/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001391 * Process a fetch + format conversion as defined by the sample expression
1392 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001393 * not explicitly set to <smp_type>, but shall be compatible with it as
1394 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1395 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001396 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1397 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1398 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1399 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001400 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1401 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1402 * still hope to get it after waiting longer, and is not converted to string.
1403 * The possible output combinations are the following :
1404 *
1405 * return MAY_CHANGE FINAL Meaning for the sample
1406 * NULL * * Not present and will never be (eg: header)
1407 * smp 0 * Final value converted (eg: header)
1408 * smp 1 0 Not present yet, may appear later (eg: header)
1409 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001410 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001411struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001412 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001413 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001414{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001415 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001416
Willy Tarreau6c616e02014-06-25 16:56:41 +02001417 memset(smp, 0, sizeof(*smp));
1418
Willy Tarreau192252e2015-04-04 01:47:55 +02001419 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001420 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1421 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001422 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001423 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001424
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001425 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001426 return NULL;
1427
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001428 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001429 return NULL;
1430
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001431 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001432 return smp;
1433}
1434
Christopher Faulet476e5d02016-10-26 11:34:47 +02001435static void release_sample_arg(struct arg *p)
1436{
1437 struct arg *p_back = p;
1438
1439 if (!p)
1440 return;
1441
1442 while (p->type != ARGT_STOP) {
1443 if (p->type == ARGT_STR || p->unresolved) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001444 chunk_destroy(&p->data.str);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001445 p->unresolved = 0;
1446 }
1447 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001448 regex_free(p->data.reg);
1449 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001450 }
1451 p++;
1452 }
1453
1454 if (p_back != empty_arg_list)
1455 free(p_back);
1456}
1457
1458void release_sample_expr(struct sample_expr *expr)
1459{
1460 struct sample_conv_expr *conv_expr, *conv_exprb;
1461
1462 if (!expr)
1463 return;
1464
Tim Duesterhus867cd982020-07-04 11:49:39 +02001465 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001466 LIST_DELETE(&conv_expr->list);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001467 release_sample_arg(conv_expr->arg_p);
Tim Duesterhus867cd982020-07-04 11:49:39 +02001468 free(conv_expr);
1469 }
1470
Christopher Faulet476e5d02016-10-26 11:34:47 +02001471 release_sample_arg(expr->arg_p);
1472 free(expr);
1473}
1474
Emeric Brun107ca302010-01-04 16:16:05 +01001475/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001476/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001477/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001478/*****************************************************************/
1479
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001480static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1481{
1482 int i;
1483 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001484 struct buffer *buf;
1485 struct sink *sink;
1486 struct ist line;
1487 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001488
Willy Tarreau0851fd52019-12-17 10:07:25 +01001489 buf = alloc_trash_chunk();
1490 if (!buf)
1491 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001492
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001493 sink = (struct sink *)arg_p[1].data.ptr;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001494 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001495
Willy Tarreau0851fd52019-12-17 10:07:25 +01001496 pfx = arg_p[0].data.str.area;
1497 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001498
Willy Tarreau0851fd52019-12-17 10:07:25 +01001499 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1500 if (!sample_casts[smp->data.type][SMP_T_STR])
1501 goto nocast;
1502
1503 /* Copy sample fetch. This puts the sample as const, the
1504 * cast will copy data if a transformation is required.
1505 */
1506 memcpy(&tmp, smp, sizeof(struct sample));
1507 tmp.flags = SMP_F_CONST;
1508
1509 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1510 goto nocast;
1511
1512 /* Display the displayable chars*. */
1513 b_putchr(buf, '<');
1514 for (i = 0; i < tmp.data.u.str.data; i++) {
Willy Tarreau90807112020-02-25 08:16:33 +01001515 if (isprint((unsigned char)tmp.data.u.str.area[i]))
Willy Tarreau0851fd52019-12-17 10:07:25 +01001516 b_putchr(buf, tmp.data.u.str.area[i]);
1517 else
1518 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001519 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001520 b_putchr(buf, '>');
1521
1522 done:
1523 line = ist2(buf->area, buf->data);
Emeric Brun54648852020-07-06 15:54:06 +02001524 sink_write(sink, &line, 1, 0, 0, NULL);
Willy Tarreau0851fd52019-12-17 10:07:25 +01001525 end:
1526 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001527 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001528 nocast:
1529 chunk_appendf(buf, "(undisplayable)");
1530 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001531}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001532
1533// This function checks the "debug" converter's arguments.
1534static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1535 const char *file, int line, char **err)
1536{
1537 const char *name = "buf0";
1538 struct sink *sink = NULL;
1539
1540 if (args[0].type != ARGT_STR) {
1541 /* optional prefix */
1542 args[0].data.str.area = "";
1543 args[0].data.str.data = 0;
1544 }
1545
1546 if (args[1].type == ARGT_STR)
1547 name = args[1].data.str.area;
1548
1549 sink = sink_find(name);
1550 if (!sink) {
1551 memprintf(err, "No such sink '%s'", name);
1552 return 0;
1553 }
1554
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001555 chunk_destroy(&args[1].data.str);
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001556 args[1].type = ARGT_PTR;
1557 args[1].data.ptr = sink;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001558 return 1;
1559}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001560
Holger Just1bfc24b2017-05-06 00:56:53 +02001561static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1562{
Willy Tarreau83061a82018-07-13 11:56:34 +02001563 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001564 int bin_len;
1565
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001566 trash->data = 0;
1567 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1568 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001569 if (bin_len < 0)
1570 return 0;
1571
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001572 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001573 smp->data.u.str = *trash;
1574 smp->data.type = SMP_T_BIN;
1575 smp->flags &= ~SMP_F_CONST;
1576 return 1;
1577}
1578
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +02001579static int sample_conv_base64url2bin(const struct arg *arg_p, struct sample *smp, void *private)
1580{
1581 struct buffer *trash = get_trash_chunk();
1582 int bin_len;
1583
1584 trash->data = 0;
1585 bin_len = base64urldec(smp->data.u.str.area, smp->data.u.str.data,
1586 trash->area, trash->size);
1587 if (bin_len < 0)
1588 return 0;
1589
1590 trash->data = bin_len;
1591 smp->data.u.str = *trash;
1592 smp->data.type = SMP_T_BIN;
1593 smp->flags &= ~SMP_F_CONST;
1594 return 1;
1595}
1596
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001597static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001598{
Willy Tarreau83061a82018-07-13 11:56:34 +02001599 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001600 int b64_len;
1601
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001602 trash->data = 0;
1603 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1604 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001605 if (b64_len < 0)
1606 return 0;
1607
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001608 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001609 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001610 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001611 smp->flags &= ~SMP_F_CONST;
1612 return 1;
1613}
1614
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +02001615static int sample_conv_bin2base64url(const struct arg *arg_p, struct sample *smp, void *private)
1616{
1617 struct buffer *trash = get_trash_chunk();
1618 int b64_len;
1619
1620 trash->data = 0;
1621 b64_len = a2base64url(smp->data.u.str.area, smp->data.u.str.data,
1622 trash->area, trash->size);
1623 if (b64_len < 0)
1624 return 0;
1625
1626 trash->data = b64_len;
1627 smp->data.u.str = *trash;
1628 smp->data.type = SMP_T_STR;
1629 smp->flags &= ~SMP_F_CONST;
1630 return 1;
1631}
1632
Willy Tarreau168e8de2021-10-06 15:29:00 +02001633/* This function returns a sample struct filled with the conversion of variable
1634 * <var> to sample type <type> (SMP_T_*), via a cast to the target type. If the
1635 * variable cannot be retrieved or casted, 0 is returned, otherwise 1.
1636 *
1637 * Keep in mind that the sample content may be written to a pre-allocated
1638 * trash chunk as returned by get_trash_chunk().
1639 */
1640int sample_conv_var2smp(const struct var_desc *var, struct sample *smp, int type)
1641{
1642 if (!vars_get_by_desc(var, smp, NULL))
1643 return 0;
1644 if (!sample_casts[smp->data.type][type])
1645 return 0;
1646 if (!sample_casts[smp->data.type][type](smp))
1647 return 0;
1648 return 1;
1649}
1650
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001651static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1652{
1653 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001654 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001655
1656 memset(&ctx, 0, sizeof(ctx));
1657
1658 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001659 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1660 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001661
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001662 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001663 smp->data.u.str = *trash;
1664 smp->data.type = SMP_T_BIN;
1665 smp->flags &= ~SMP_F_CONST;
1666 return 1;
1667}
1668
Tim Duesterhusd4376302019-06-17 12:41:44 +02001669#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01001670static int smp_check_sha2(struct arg *args, struct sample_conv *conv,
1671 const char *file, int line, char **err)
1672{
1673 if (args[0].type == ARGT_STOP)
1674 return 1;
1675 if (args[0].type != ARGT_SINT) {
1676 memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]);
1677 return 0;
1678 }
1679
1680 switch (args[0].data.sint) {
1681 case 224:
1682 case 256:
1683 case 384:
1684 case 512:
1685 /* this is okay */
1686 return 1;
1687 default:
1688 memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint);
1689 return 0;
1690 }
1691}
1692
Tim Duesterhusd4376302019-06-17 12:41:44 +02001693static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
1694{
1695 struct buffer *trash = get_trash_chunk();
1696 int bits = 256;
Christopher Faulete6e7a582021-01-29 11:29:28 +01001697 if (arg_p->data.sint)
Tim Duesterhusd4376302019-06-17 12:41:44 +02001698 bits = arg_p->data.sint;
1699
1700 switch (bits) {
1701 case 224: {
1702 SHA256_CTX ctx;
1703
1704 memset(&ctx, 0, sizeof(ctx));
1705
1706 SHA224_Init(&ctx);
1707 SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1708 SHA224_Final((unsigned char *) trash->area, &ctx);
1709 trash->data = SHA224_DIGEST_LENGTH;
1710 break;
1711 }
1712 case 256: {
1713 SHA256_CTX ctx;
1714
1715 memset(&ctx, 0, sizeof(ctx));
1716
1717 SHA256_Init(&ctx);
1718 SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1719 SHA256_Final((unsigned char *) trash->area, &ctx);
1720 trash->data = SHA256_DIGEST_LENGTH;
1721 break;
1722 }
1723 case 384: {
1724 SHA512_CTX ctx;
1725
1726 memset(&ctx, 0, sizeof(ctx));
1727
1728 SHA384_Init(&ctx);
1729 SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1730 SHA384_Final((unsigned char *) trash->area, &ctx);
1731 trash->data = SHA384_DIGEST_LENGTH;
1732 break;
1733 }
1734 case 512: {
1735 SHA512_CTX ctx;
1736
1737 memset(&ctx, 0, sizeof(ctx));
1738
1739 SHA512_Init(&ctx);
1740 SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1741 SHA512_Final((unsigned char *) trash->area, &ctx);
1742 trash->data = SHA512_DIGEST_LENGTH;
1743 break;
1744 }
1745 default:
1746 return 0;
1747 }
1748
1749 smp->data.u.str = *trash;
1750 smp->data.type = SMP_T_BIN;
1751 smp->flags &= ~SMP_F_CONST;
1752 return 1;
1753}
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001754
Dragan Dosen9e8db132021-02-22 10:03:53 +01001755/* This function returns a sample struct filled with an <arg> content.
1756 * If the <arg> contains a string, it is returned in the sample flagged as
1757 * SMP_F_CONST. If the <arg> contains a variable descriptor, the sample is
1758 * filled with the content of the variable by using vars_get_by_desc().
1759 *
1760 * Keep in mind that the sample content may be written to a pre-allocated
1761 * trash chunk as returned by get_trash_chunk().
1762 *
1763 * This function returns 0 if an error occurs, otherwise it returns 1.
1764 */
Willy Tarreau4034e2c2021-10-06 15:20:18 +02001765int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001766{
1767 switch (arg->type) {
1768 case ARGT_STR:
1769 smp->data.type = SMP_T_STR;
1770 smp->data.u.str = arg->data.str;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001771 smp->flags = SMP_F_CONST;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001772 return 1;
1773 case ARGT_VAR:
Willy Tarreau2476ff12021-10-06 15:30:52 +02001774 return sample_conv_var2smp(&arg->data.var, smp, SMP_T_STR);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001775 default:
1776 return 0;
1777 }
1778}
1779
Dragan Dosen9e8db132021-02-22 10:03:53 +01001780/* This function checks an <arg> and fills it with a variable type if the
1781 * <arg> string contains a valid variable name. If failed, the function
1782 * tries to perform a base64 decode operation on the same string, and
1783 * fills the <arg> with the decoded content.
1784 *
1785 * Validation is skipped if the <arg> string is empty.
1786 *
1787 * This function returns 0 if the variable lookup fails and the specified
1788 * <arg> string is not a valid base64 encoded string, as well if
1789 * unexpected argument type is specified or memory allocation error
1790 * occurs. Otherwise it returns 1.
1791 */
1792static inline int sample_check_arg_base64(struct arg *arg, char **err)
1793{
1794 char *dec = NULL;
1795 int dec_size;
1796
1797 if (arg->type != ARGT_STR) {
1798 memprintf(err, "unexpected argument type");
1799 return 0;
1800 }
1801
1802 if (arg->data.str.data == 0) /* empty */
1803 return 1;
1804
1805 if (vars_check_arg(arg, NULL))
1806 return 1;
1807
1808 if (arg->data.str.data % 4) {
1809 memprintf(err, "argument needs to be base64 encoded, and "
1810 "can either be a string or a variable");
1811 return 0;
1812 }
1813
1814 dec_size = (arg->data.str.data / 4 * 3)
1815 - (arg->data.str.area[arg->data.str.data-1] == '=' ? 1 : 0)
1816 - (arg->data.str.area[arg->data.str.data-2] == '=' ? 1 : 0);
1817
1818 if ((dec = malloc(dec_size)) == NULL) {
1819 memprintf(err, "memory allocation error");
1820 return 0;
1821 }
1822
1823 dec_size = base64dec(arg->data.str.area, arg->data.str.data, dec, dec_size);
1824 if (dec_size < 0) {
1825 memprintf(err, "argument needs to be base64 encoded, and "
1826 "can either be a string or a variable");
1827 free(dec);
1828 return 0;
1829 }
1830
1831 /* base64 decoded */
1832 chunk_destroy(&arg->data.str);
1833 arg->data.str.area = dec;
1834 arg->data.str.data = dec_size;
1835 return 1;
1836}
1837
Ilya Shipitsin2c481d02021-03-26 23:35:31 +05001838#ifdef EVP_CIPH_GCM_MODE
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001839static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
1840 const char *file, int line, char **err)
1841{
1842 switch(args[0].data.sint) {
1843 case 128:
1844 case 192:
1845 case 256:
1846 break;
1847 default:
1848 memprintf(err, "key size must be 128, 192 or 256 (bits).");
1849 return 0;
1850 }
Dragan Dosen9e8db132021-02-22 10:03:53 +01001851
1852 /* Try to decode variables. */
1853 if (!sample_check_arg_base64(&args[1], err)) {
1854 memprintf(err, "failed to parse nonce : %s", *err);
1855 return 0;
1856 }
1857 if (!sample_check_arg_base64(&args[2], err)) {
1858 memprintf(err, "failed to parse key : %s", *err);
1859 return 0;
1860 }
1861 if (!sample_check_arg_base64(&args[3], err)) {
1862 memprintf(err, "failed to parse aead_tag : %s", *err);
1863 return 0;
1864 }
1865
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001866 return 1;
1867}
1868
1869/* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
1870static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
1871{
1872 struct sample nonce, key, aead_tag;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001873 struct buffer *smp_trash = NULL, *smp_trash_alloc = NULL;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001874 EVP_CIPHER_CTX *ctx;
1875 int dec_size, ret;
1876
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001877 smp_trash_alloc = alloc_trash_chunk();
1878 if (!smp_trash_alloc)
1879 return 0;
1880
Dragan Dosen9e8db132021-02-22 10:03:53 +01001881 /* smp copy */
1882 smp_trash_alloc->data = smp->data.u.str.data;
1883 if (unlikely(smp_trash_alloc->data > smp_trash_alloc->size))
1884 smp_trash_alloc->data = smp_trash_alloc->size;
1885 memcpy(smp_trash_alloc->area, smp->data.u.str.area, smp_trash_alloc->data);
1886
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001887 ctx = EVP_CIPHER_CTX_new();
1888
1889 if (!ctx)
1890 goto err;
1891
Dragan Dosen9e8db132021-02-22 10:03:53 +01001892 smp_trash = alloc_trash_chunk();
1893 if (!smp_trash)
1894 goto err;
1895
1896 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
1897 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001898 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001899
1900 if (arg_p[1].type == ARGT_VAR) {
1901 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
1902 if (dec_size < 0)
1903 goto err;
1904 smp_trash->data = dec_size;
1905 nonce.data.u.str = *smp_trash;
1906 }
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001907
1908 /* Set cipher type and mode */
1909 switch(arg_p[0].data.sint) {
1910 case 128:
1911 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
1912 break;
1913 case 192:
1914 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
1915 break;
1916 case 256:
1917 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
1918 break;
1919 }
1920
Dragan Dosen9e8db132021-02-22 10:03:53 +01001921 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, nonce.data.u.str.data, NULL);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001922
1923 /* Initialise IV */
Dragan Dosen9e8db132021-02-22 10:03:53 +01001924 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) nonce.data.u.str.area))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001925 goto err;
1926
Dragan Dosen9e8db132021-02-22 10:03:53 +01001927 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
1928 if (!sample_conv_var2smp_str(&arg_p[2], &key))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001929 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001930
1931 if (arg_p[2].type == ARGT_VAR) {
1932 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
1933 if (dec_size < 0)
1934 goto err;
1935 smp_trash->data = dec_size;
1936 key.data.u.str = *smp_trash;
1937 }
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001938
1939 /* Initialise key */
Dragan Dosen9e8db132021-02-22 10:03:53 +01001940 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) key.data.u.str.area, NULL))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001941 goto err;
1942
1943 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
Dragan Dosen9e8db132021-02-22 10:03:53 +01001944 (unsigned char *) smp_trash_alloc->area, (int) smp_trash_alloc->data))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001945 goto err;
1946
Dragan Dosen9e8db132021-02-22 10:03:53 +01001947 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
1948 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001949 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001950
1951 if (arg_p[3].type == ARGT_VAR) {
1952 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
1953 if (dec_size < 0)
1954 goto err;
1955 smp_trash_alloc->data = dec_size;
1956 aead_tag.data.u.str = *smp_trash_alloc;
1957 }
1958
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001959 dec_size = smp_trash->data;
1960
Dragan Dosen9e8db132021-02-22 10:03:53 +01001961 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 +02001962 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
1963
1964 if (ret <= 0)
1965 goto err;
1966
1967 smp->data.u.str.data = dec_size + smp_trash->data;
1968 smp->data.u.str.area = smp_trash->area;
1969 smp->data.type = SMP_T_BIN;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001970 smp_dup(smp);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001971 free_trash_chunk(smp_trash_alloc);
Dragan Dosen9e8db132021-02-22 10:03:53 +01001972 free_trash_chunk(smp_trash);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001973 return 1;
1974
1975err:
1976 free_trash_chunk(smp_trash_alloc);
Dragan Dosen9e8db132021-02-22 10:03:53 +01001977 free_trash_chunk(smp_trash);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001978 return 0;
1979}
Ilya Shipitsin2c481d02021-03-26 23:35:31 +05001980#endif
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001981
Patrick Gansterer8e366512020-04-22 16:47:57 +02001982static int check_crypto_digest(struct arg *args, struct sample_conv *conv,
1983 const char *file, int line, char **err)
1984{
1985 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1986
1987 if (evp)
1988 return 1;
1989
1990 memprintf(err, "algorithm must be a valid OpenSSL message digest name.");
1991 return 0;
1992}
1993
1994static int sample_conv_crypto_digest(const struct arg *args, struct sample *smp, void *private)
1995{
1996 struct buffer *trash = get_trash_chunk();
1997 unsigned char *md = (unsigned char*) trash->area;
1998 unsigned int md_len = trash->size;
1999 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
2000 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
2001
2002 if (!ctx)
2003 return 0;
2004
2005 if (!EVP_DigestInit_ex(ctx, evp, NULL) ||
2006 !EVP_DigestUpdate(ctx, smp->data.u.str.area, smp->data.u.str.data) ||
2007 !EVP_DigestFinal_ex(ctx, md, &md_len)) {
2008 EVP_MD_CTX_free(ctx);
2009 return 0;
2010 }
2011
2012 EVP_MD_CTX_free(ctx);
2013
2014 trash->data = md_len;
2015 smp->data.u.str = *trash;
2016 smp->data.type = SMP_T_BIN;
2017 smp->flags &= ~SMP_F_CONST;
2018 return 1;
2019}
2020
2021static int check_crypto_hmac(struct arg *args, struct sample_conv *conv,
2022 const char *file, int line, char **err)
2023{
2024 if (!check_crypto_digest(args, conv, file, line, err))
2025 return 0;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002026
2027 if (!sample_check_arg_base64(&args[1], err)) {
2028 memprintf(err, "failed to parse key : %s", *err);
2029 return 0;
2030 }
Patrick Gansterer8e366512020-04-22 16:47:57 +02002031
Patrick Gansterer8e366512020-04-22 16:47:57 +02002032 return 1;
2033}
2034
2035static int sample_conv_crypto_hmac(const struct arg *args, struct sample *smp, void *private)
2036{
2037 struct sample key;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002038 struct buffer *trash = NULL, *key_trash = NULL;
Patrick Gansterer8e366512020-04-22 16:47:57 +02002039 unsigned char *md;
2040 unsigned int md_len;
2041 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
2042 int dec_size;
2043
2044 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
2045 if (!sample_conv_var2smp_str(&args[1], &key))
2046 return 0;
2047
Dragan Dosen9e8db132021-02-22 10:03:53 +01002048 if (args[1].type == ARGT_VAR) {
2049 key_trash = alloc_trash_chunk();
2050 if (!key_trash)
2051 goto err;
2052
2053 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, key_trash->area, key_trash->size);
2054 if (dec_size < 0)
2055 goto err;
2056 key_trash->data = dec_size;
2057 key.data.u.str = *key_trash;
2058 }
Patrick Gansterer8e366512020-04-22 16:47:57 +02002059
Dragan Dosen9e8db132021-02-22 10:03:53 +01002060 trash = alloc_trash_chunk();
2061 if (!trash)
Patrick Gansterer8e366512020-04-22 16:47:57 +02002062 goto err;
2063
2064 md = (unsigned char*) trash->area;
2065 md_len = trash->size;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002066 if (!HMAC(evp, key.data.u.str.area, key.data.u.str.data, (const unsigned char*) smp->data.u.str.area,
2067 smp->data.u.str.data, md, &md_len))
Patrick Gansterer8e366512020-04-22 16:47:57 +02002068 goto err;
2069
2070 free_trash_chunk(key_trash);
2071
2072 trash->data = md_len;
2073 smp->data.u.str = *trash;
2074 smp->data.type = SMP_T_BIN;
Dragan Dosen9e8db132021-02-22 10:03:53 +01002075 smp_dup(smp);
2076 free_trash_chunk(trash);
Patrick Gansterer8e366512020-04-22 16:47:57 +02002077 return 1;
2078
2079err:
2080 free_trash_chunk(key_trash);
Dragan Dosen9e8db132021-02-22 10:03:53 +01002081 free_trash_chunk(trash);
Patrick Gansterer8e366512020-04-22 16:47:57 +02002082 return 0;
2083}
2084
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02002085#endif /* USE_OPENSSL */
Tim Duesterhusd4376302019-06-17 12:41:44 +02002086
Marcin Deranek40ca09c2021-07-13 14:05:24 +02002087static int sample_conv_be2dec_check(struct arg *args, struct sample_conv *conv,
2088 const char *file, int line, char **err)
2089{
2090 if (args[1].data.sint <= 0 || args[1].data.sint > sizeof(unsigned long long)) {
Willy Tarreauc5d0fc92021-09-15 10:30:40 +02002091 memprintf(err, "chunk_size out of [1..%u] range (%lld)", (uint)sizeof(unsigned long long), args[1].data.sint);
Marcin Deranek40ca09c2021-07-13 14:05:24 +02002092 return 0;
2093 }
2094
2095 if (args[2].data.sint != 0 && args[2].data.sint != 1) {
2096 memprintf(err, "Unsupported truncate value (%lld)", args[2].data.sint);
2097 return 0;
2098 }
2099
2100 return 1;
2101}
2102
2103/* Converts big-endian binary input sample to a string containing an unsigned
2104 * integer number per <chunk_size> input bytes separated with <separator>.
2105 * Optional <truncate> flag indicates if input is truncated at <chunk_size>
2106 * boundaries.
2107 * Arguments: separator (string), chunk_size (integer), truncate (0,1)
2108 */
2109static int sample_conv_be2dec(const struct arg *args, struct sample *smp, void *private)
2110{
2111 struct buffer *trash = get_trash_chunk();
2112 const int last = args[2].data.sint ? smp->data.u.str.data - args[1].data.sint + 1 : smp->data.u.str.data;
2113 int max_size = trash->size - 2;
2114 int i;
2115 int start;
2116 int ptr = 0;
2117 unsigned long long number;
2118 char *pos;
2119
2120 trash->data = 0;
2121
2122 while (ptr < last && trash->data <= max_size) {
2123 start = trash->data;
2124 if (ptr) {
2125 /* Add separator */
2126 memcpy(trash->area + trash->data, args[0].data.str.area, args[0].data.str.data);
2127 trash->data += args[0].data.str.data;
2128 }
2129 else
2130 max_size -= args[0].data.str.data;
2131
2132 /* Add integer */
2133 for (number = 0, i = 0; i < args[1].data.sint && ptr < smp->data.u.str.data; i++)
2134 number = (number << 8) + (unsigned char)smp->data.u.str.area[ptr++];
2135
2136 pos = ulltoa(number, trash->area + trash->data, trash->size - trash->data);
2137 if (pos)
2138 trash->data = pos - trash->area;
2139 else {
2140 trash->data = start;
2141 break;
2142 }
2143 }
2144
2145 smp->data.u.str = *trash;
2146 smp->data.type = SMP_T_STR;
2147 smp->flags &= ~SMP_F_CONST;
2148 return 1;
2149}
2150
Marcin Deranekda0264a2021-07-13 14:08:56 +02002151static int sample_conv_be2hex_check(struct arg *args, struct sample_conv *conv,
2152 const char *file, int line, char **err)
2153{
2154 if (args[1].data.sint <= 0 && (args[0].data.str.data > 0 || args[2].data.sint != 0)) {
2155 memprintf(err, "chunk_size needs to be positive (%lld)", args[1].data.sint);
2156 return 0;
2157 }
2158
2159 if (args[2].data.sint != 0 && args[2].data.sint != 1) {
2160 memprintf(err, "Unsupported truncate value (%lld)", args[2].data.sint);
2161 return 0;
2162 }
2163
2164 return 1;
2165}
2166
2167/* Converts big-endian binary input sample to a hex string containing two hex
2168 * digits per input byte. <separator> is put every <chunk_size> binary input
2169 * bytes if specified. Optional <truncate> flag indicates if input is truncated
2170 * at <chunk_size> boundaries.
2171 * Arguments: separator (string), chunk_size (integer), truncate (0,1)
2172 */
2173static int sample_conv_be2hex(const struct arg *args, struct sample *smp, void *private)
2174{
2175 struct buffer *trash = get_trash_chunk();
2176 int chunk_size = args[1].data.sint;
2177 const int last = args[2].data.sint ? smp->data.u.str.data - chunk_size + 1 : smp->data.u.str.data;
2178 int i;
2179 int max_size;
2180 int ptr = 0;
2181 unsigned char c;
2182
2183 trash->data = 0;
2184 if (args[0].data.str.data == 0 && args[2].data.sint == 0)
2185 chunk_size = smp->data.u.str.data;
2186 max_size = trash->size - 2 * chunk_size;
2187
2188 while (ptr < last && trash->data <= max_size) {
2189 if (ptr) {
2190 /* Add separator */
2191 memcpy(trash->area + trash->data, args[0].data.str.area, args[0].data.str.data);
2192 trash->data += args[0].data.str.data;
2193 }
2194 else
2195 max_size -= args[0].data.str.data;
2196
2197 /* Add hex */
2198 for (i = 0; i < chunk_size && ptr < smp->data.u.str.data; i++) {
2199 c = smp->data.u.str.area[ptr++];
2200 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
2201 trash->area[trash->data++] = hextab[c & 0xF];
2202 }
2203 }
2204
2205 smp->data.u.str = *trash;
2206 smp->data.type = SMP_T_STR;
2207 smp->flags &= ~SMP_F_CONST;
2208 return 1;
2209}
2210
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002211static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002212{
Willy Tarreau83061a82018-07-13 11:56:34 +02002213 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002214 unsigned char c;
2215 int ptr = 0;
2216
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002217 trash->data = 0;
2218 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
2219 c = smp->data.u.str.area[ptr++];
2220 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
2221 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002222 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002223 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002224 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002225 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002226 return 1;
2227}
2228
Dragan Dosen3f957b22017-10-24 09:27:34 +02002229static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
2230{
2231 long long int n = 0;
2232 int i, c;
2233
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002234 for (i = 0; i < smp->data.u.str.data; i++) {
2235 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02002236 return 0;
2237 n = (n << 4) + c;
2238 }
2239
2240 smp->data.u.sint = n;
2241 smp->data.type = SMP_T_SINT;
2242 smp->flags &= ~SMP_F_CONST;
2243 return 1;
2244}
2245
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002246/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002247static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002248{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002249 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
2250 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002251 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002252 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002253 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002254 return 1;
2255}
2256
Willy Tarreau60a2ee72017-12-15 07:13:48 +01002257static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002258{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002259 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002260 smp->data.u.sint = i;
2261 smp->data.type = SMP_T_SINT;
2262 return 1;
2263}
2264
2265
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002266static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002267{
2268 int i;
2269
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002270 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002271 return 0;
2272
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002273 for (i = 0; i < smp->data.u.str.data; i++) {
2274 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
2275 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01002276 }
2277 return 1;
2278}
2279
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002280static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002281{
2282 int i;
2283
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002284 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02002285 return 0;
2286
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002287 for (i = 0; i < smp->data.u.str.data; i++) {
2288 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
2289 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01002290 }
2291 return 1;
2292}
2293
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002294/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
2295static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002296{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002297 /* Attempt to convert to IPv4 to apply the correct mask. */
2298 c_ipv62ip(smp);
2299
2300 if (smp->data.type == SMP_T_IPV4) {
2301 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
2302 smp->data.type = SMP_T_IPV4;
2303 }
2304 else if (smp->data.type == SMP_T_IPV6) {
2305 /* IPv6 cannot be converted without an IPv6 mask. */
2306 if (args[1].type != ARGT_IPV6)
2307 return 0;
2308
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +01002309 write_u64(&smp->data.u.ipv6.s6_addr[0],
2310 read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0]));
2311 write_u64(&smp->data.u.ipv6.s6_addr[8],
2312 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 +01002313 smp->data.type = SMP_T_IPV6;
2314 }
2315
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002316 return 1;
2317}
2318
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002319/* takes an UINT value on input supposed to represent the time since EPOCH,
2320 * adds an optional offset found in args[1] and emits a string representing
2321 * the local time in the format specified in args[1] using strftime().
2322 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002323static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002324{
Willy Tarreau83061a82018-07-13 11:56:34 +02002325 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002326 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002327 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002328 struct tm tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002329
2330 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002331 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002332 curr_date += args[1].data.sint;
2333
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002334 get_localtime(curr_date, &tm);
2335
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002336 temp = get_trash_chunk();
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002337 temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002338 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002339 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002340 return 1;
2341}
2342
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002343/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002344static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002345{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002346 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
2347 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002348 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002349 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002350 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002351 return 1;
2352}
2353
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002354/* takes an UINT value on input supposed to represent the time since EPOCH,
2355 * adds an optional offset found in args[1] and emits a string representing
2356 * the UTC date in the format specified in args[1] using strftime().
2357 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002358static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002359{
Willy Tarreau83061a82018-07-13 11:56:34 +02002360 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002361 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002362 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002363 struct tm tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002364
2365 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002366 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002367 curr_date += args[1].data.sint;
2368
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002369 get_gmtime(curr_date, &tm);
2370
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002371 temp = get_trash_chunk();
Tim Duesterhus1f269c12021-08-28 23:57:01 +02002372 temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002373 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002374 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002375 return 1;
2376}
2377
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002378/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002379static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002380{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002381 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
2382 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002383 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002384 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002385 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002386 return 1;
2387}
2388
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002389/* hashes the binary input into a 32-bit unsigned int using xxh.
2390 * The seed of the hash defaults to 0 but can be changd in argument 1.
2391 */
2392static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
2393{
2394 unsigned int seed;
2395
Christopher Faulete6e7a582021-01-29 11:29:28 +01002396 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002397 seed = arg_p->data.sint;
2398 else
2399 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002400 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
2401 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002402 smp->data.type = SMP_T_SINT;
2403 return 1;
2404}
2405
2406/* hashes the binary input into a 64-bit unsigned int using xxh.
2407 * In fact, the function returns a 64 bit unsigned, but the sample
2408 * storage of haproxy only proposes 64-bits signed, so the value is
2409 * cast as signed. This cast doesn't impact the hash repartition.
2410 * The seed of the hash defaults to 0 but can be changd in argument 1.
2411 */
2412static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
2413{
2414 unsigned long long int seed;
2415
Christopher Faulete6e7a582021-01-29 11:29:28 +01002416 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002417 seed = (unsigned long long int)arg_p->data.sint;
2418 else
2419 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002420 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
2421 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002422 smp->data.type = SMP_T_SINT;
2423 return 1;
2424}
2425
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002426static int sample_conv_xxh3(const struct arg *arg_p, struct sample *smp, void *private)
2427{
2428 unsigned long long int seed;
2429
Christopher Faulete6e7a582021-01-29 11:29:28 +01002430 if (arg_p->data.sint)
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002431 seed = (unsigned long long int)arg_p->data.sint;
2432 else
2433 seed = 0;
2434 smp->data.u.sint = (long long int)XXH3(smp->data.u.str.area,
2435 smp->data.u.str.data, seed);
2436 smp->data.type = SMP_T_SINT;
2437 return 1;
2438}
2439
Willy Tarreau80599772015-01-20 19:35:24 +01002440/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002441static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01002442{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002443 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
2444 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002445 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002446 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002447 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01002448 return 1;
2449}
2450
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002451/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
2452static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
2453{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002454 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
2455 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002456 if (arg_p->data.sint)
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002457 smp->data.u.sint = full_hash(smp->data.u.sint);
2458 smp->data.type = SMP_T_SINT;
2459 return 1;
2460}
2461
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002462/* This function escape special json characters. The returned string can be
2463 * safely set between two '"' and used as json string. The json string is
2464 * defined like this:
2465 *
2466 * any Unicode character except '"' or '\' or control character
2467 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
2468 *
2469 * The enum input_type contain all the allowed mode for decoding the input
2470 * string.
2471 */
2472enum input_type {
2473 IT_ASCII = 0,
2474 IT_UTF8,
2475 IT_UTF8S,
2476 IT_UTF8P,
2477 IT_UTF8PS,
2478};
William Dauchy5417e892021-01-08 21:57:41 +01002479
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002480static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
2481 const char *file, int line, char **err)
2482{
Christopher Faulet95917132020-08-05 23:07:07 +02002483 enum input_type type;
2484
Christopher Faulet95917132020-08-05 23:07:07 +02002485 if (strcmp(arg->data.str.area, "") == 0)
2486 type = IT_ASCII;
2487 else if (strcmp(arg->data.str.area, "ascii") == 0)
2488 type = IT_ASCII;
2489 else if (strcmp(arg->data.str.area, "utf8") == 0)
2490 type = IT_UTF8;
2491 else if (strcmp(arg->data.str.area, "utf8s") == 0)
2492 type = IT_UTF8S;
2493 else if (strcmp(arg->data.str.area, "utf8p") == 0)
2494 type = IT_UTF8P;
2495 else if (strcmp(arg->data.str.area, "utf8ps") == 0)
2496 type = IT_UTF8PS;
2497 else {
2498 memprintf(err, "Unexpected input code type. "
2499 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
2500 return 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002501 }
2502
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002503 chunk_destroy(&arg->data.str);
Christopher Faulet95917132020-08-05 23:07:07 +02002504 arg->type = ARGT_SINT;
2505 arg->data.sint = type;
2506 return 1;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002507}
2508
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002509static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002510{
Willy Tarreau83061a82018-07-13 11:56:34 +02002511 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002512 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
2513 const char *str;
2514 int len;
2515 enum input_type input_type = IT_ASCII;
2516 unsigned int c;
2517 unsigned int ret;
2518 char *p;
2519
Christopher Faulete6e7a582021-01-29 11:29:28 +01002520 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002521
2522 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002523 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002524
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002525 p = smp->data.u.str.area;
2526 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002527
2528 if (input_type == IT_ASCII) {
2529 /* Read input as ASCII. */
2530 c = *(unsigned char *)p;
2531 p++;
2532 }
2533 else {
2534 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002535 ret = utf8_next(p,
2536 smp->data.u.str.data - ( p - smp->data.u.str.area),
2537 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002538 p += utf8_return_length(ret);
2539
2540 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2541 return 0;
2542 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2543 continue;
2544 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2545 return 0;
2546 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2547 continue;
2548
2549 /* Check too big values. */
2550 if ((unsigned int)c > 0xffff) {
2551 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2552 return 0;
2553 continue;
2554 }
2555 }
2556
2557 /* Convert character. */
2558 if (c == '"') {
2559 len = 2;
2560 str = "\\\"";
2561 }
2562 else if (c == '\\') {
2563 len = 2;
2564 str = "\\\\";
2565 }
2566 else if (c == '/') {
2567 len = 2;
2568 str = "\\/";
2569 }
2570 else if (c == '\b') {
2571 len = 2;
2572 str = "\\b";
2573 }
2574 else if (c == '\f') {
2575 len = 2;
2576 str = "\\f";
2577 }
2578 else if (c == '\r') {
2579 len = 2;
2580 str = "\\r";
2581 }
2582 else if (c == '\n') {
2583 len = 2;
2584 str = "\\n";
2585 }
2586 else if (c == '\t') {
2587 len = 2;
2588 str = "\\t";
2589 }
Willy Tarreau90807112020-02-25 08:16:33 +01002590 else if (c > 0xff || !isprint((unsigned char)c)) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002591 /* isprint generate a segfault if c is too big. The man says that
2592 * c must have the value of an unsigned char or EOF.
2593 */
2594 len = 6;
2595 _str[0] = '\\';
2596 _str[1] = 'u';
2597 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2598 str = _str;
2599 }
2600 else {
2601 len = 1;
Willy Tarreau5715da22020-02-25 08:37:37 +01002602 _str[0] = c;
2603 str = _str;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002604 }
2605
2606 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002607 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002608 return 0;
2609
2610 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002611 memcpy(temp->area + temp->data, str, len);
2612 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002613 }
2614
2615 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002616 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002617 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002618
2619 return 1;
2620}
2621
Emeric Brun54c4ac82014-11-03 15:32:43 +01002622/* This sample function is designed to extract some bytes from an input buffer.
2623 * First arg is the offset.
2624 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002625static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002626{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002627 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2628 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002629 return 1;
2630 }
2631
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002632 if (smp->data.u.str.size)
2633 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002634 smp->data.u.str.data -= arg_p[0].data.sint;
2635 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002636
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002637 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2638 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002639
2640 return 1;
2641}
2642
Emeric Brunf399b0d2014-11-03 17:07:03 +01002643static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2644 const char *file, int line, char **err)
2645{
2646 struct arg *arg = args;
2647
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002648 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002649 memprintf(err, "Unexpected arg type");
2650 return 0;
2651 }
2652
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002653 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002654 memprintf(err, "Unexpected value 0 for index");
2655 return 0;
2656 }
2657
2658 arg++;
2659
2660 if (arg->type != ARGT_STR) {
2661 memprintf(err, "Unexpected arg type");
2662 return 0;
2663 }
2664
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002665 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002666 memprintf(err, "Empty separators list");
2667 return 0;
2668 }
2669
2670 return 1;
2671}
2672
2673/* This sample function is designed to a return selected part of a string (field).
2674 * First arg is the index of the field (start at 1)
2675 * Second arg is a char list of separators (type string)
2676 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002677static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002678{
Marcin Deranek9631a282018-04-16 14:30:46 +02002679 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002680 char *start, *end;
2681 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002682 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002683
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002684 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002685 return 0;
2686
Marcin Deranek9631a282018-04-16 14:30:46 +02002687 if (arg_p[0].data.sint < 0) {
2688 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002689 end = start = smp->data.u.str.area + smp->data.u.str.data;
2690 while (start > smp->data.u.str.area) {
2691 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2692 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002693 if (field == arg_p[0].data.sint) {
2694 if (count == 1)
2695 goto found;
2696 else if (count > 1)
2697 count--;
2698 } else {
2699 end = start-1;
2700 field--;
2701 }
2702 break;
2703 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002704 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002705 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002706 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002707 } else {
2708 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002709 end = start = smp->data.u.str.area;
2710 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2711 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2712 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002713 if (field == arg_p[0].data.sint) {
2714 if (count == 1)
2715 goto found;
2716 else if (count > 1)
2717 count--;
2718 } else {
2719 start = end+1;
2720 field++;
2721 }
2722 break;
2723 }
2724 }
2725 end++;
2726 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002727 }
2728
2729 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002730 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002731 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002732 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002733 }
2734found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002735 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002736 /* If ret string is len 0, no need to
2737 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002738 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002739 return 1;
2740
Emeric Brunf399b0d2014-11-03 17:07:03 +01002741 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002742 Note: smp->data.u.str.size cannot be set to 0 */
2743 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002744 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002745
Thayne McCombsb2843052021-04-11 23:26:59 -06002746 smp->data.u.str.area = start;
2747
Emeric Brunf399b0d2014-11-03 17:07:03 +01002748 return 1;
2749}
2750
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002751/* This sample function is designed to return a word from a string.
2752 * First arg is the index of the word (start at 1)
2753 * Second arg is a char list of words separators (type string)
2754 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002755static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002756{
Marcin Deranek9631a282018-04-16 14:30:46 +02002757 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002758 char *start, *end;
2759 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002760 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002761
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002762 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002763 return 0;
2764
2765 word = 0;
2766 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002767 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002768 end = start = smp->data.u.str.area + smp->data.u.str.data;
2769 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002770 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002771 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2772 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002773 issep = 1;
2774 break;
2775 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002776 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002777 if (!inword) {
2778 if (!issep) {
2779 if (word != arg_p[0].data.sint) {
2780 word--;
2781 end = start;
2782 }
2783 inword = 1;
2784 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002785 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002786 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002787 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002788 if (count == 1)
2789 goto found;
2790 else if (count > 1)
2791 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002792 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002793 inword = 0;
2794 }
2795 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002796 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002797 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002798 end = start = smp->data.u.str.area;
2799 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002800 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002801 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2802 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002803 issep = 1;
2804 break;
2805 }
2806 }
2807 if (!inword) {
2808 if (!issep) {
2809 if (word != arg_p[0].data.sint) {
2810 word++;
2811 start = end;
2812 }
2813 inword = 1;
2814 }
2815 }
2816 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002817 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002818 if (count == 1)
2819 goto found;
2820 else if (count > 1)
2821 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002822 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002823 inword = 0;
2824 }
2825 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002826 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002827 }
2828
2829 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002830 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002831 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002832 return 1;
2833 }
2834found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002835 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002836 /* If ret string is len 0, no need to
2837 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002838 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002839 return 1;
2840
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002841 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002842
2843 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002844 Note: smp->data.u.str.size cannot be set to 0 */
2845 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002846 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002847
2848 return 1;
2849}
2850
Willy Tarreau7eda8492015-01-20 19:47:06 +01002851static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2852 const char *file, int line, char **err)
2853{
2854 struct arg *arg = args;
2855 char *p;
2856 int len;
2857
2858 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2859 arg[0].type_flags = 0;
2860
2861 if (arg[2].type != ARGT_STR)
2862 return 1;
2863
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002864 p = arg[2].data.str.area;
2865 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002866 while (len) {
2867 if (*p == 'i') {
2868 arg[0].type_flags |= ARGF_REG_ICASE;
2869 }
2870 else if (*p == 'g') {
2871 arg[0].type_flags |= ARGF_REG_GLOB;
2872 }
2873 else {
2874 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2875 return 0;
2876 }
2877 p++;
2878 len--;
2879 }
2880 return 1;
2881}
2882
2883/* This sample function is designed to do the equivalent of s/match/replace/ on
2884 * the input string. It applies a regex and restarts from the last matched
2885 * location until nothing matches anymore. First arg is the regex to apply to
2886 * the input string, second arg is the replacement expression.
2887 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002888static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002889{
2890 char *start, *end;
2891 struct my_regex *reg = arg_p[0].data.reg;
2892 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002893 struct buffer *trash = get_trash_chunk();
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002894 struct buffer *output;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002895 int flag, max;
2896 int found;
2897
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002898 start = smp->data.u.str.area;
2899 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002900
2901 flag = 0;
2902 while (1) {
2903 /* check for last round which is used to copy remaining parts
2904 * when not running in global replacement mode.
2905 */
2906 found = 0;
2907 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2908 /* Note: we can have start == end on empty strings or at the end */
2909 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2910 }
2911
2912 if (!found)
2913 pmatch[0].rm_so = end - start;
2914
2915 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002916 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002917 if (max && pmatch[0].rm_so > 0) {
2918 if (max > pmatch[0].rm_so)
2919 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002920 memcpy(trash->area + trash->data, start, max);
2921 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002922 }
2923
2924 if (!found)
2925 break;
2926
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002927 output = alloc_trash_chunk();
Willy Tarreau23997da2020-02-18 14:27:44 +01002928 if (!output)
2929 break;
2930
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002931 output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2932
Willy Tarreau7eda8492015-01-20 19:47:06 +01002933 /* replace the matching part */
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002934 max = output->size - output->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002935 if (max) {
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002936 if (max > output->data)
2937 max = output->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002938 memcpy(trash->area + trash->data,
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002939 output->area, max);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002940 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002941 }
2942
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002943 free_trash_chunk(output);
2944
Willy Tarreau7eda8492015-01-20 19:47:06 +01002945 /* stop here if we're done with this string */
2946 if (start >= end)
2947 break;
2948
2949 /* We have a special case for matches of length 0 (eg: "x*y*").
2950 * These ones are considered to match in front of a character,
2951 * so we have to copy that character and skip to the next one.
2952 */
2953 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002954 if (trash->data < trash->size)
2955 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002956 pmatch[0].rm_eo++;
2957 }
2958
2959 start += pmatch[0].rm_eo;
2960 flag |= REG_NOTBOL;
2961 }
2962
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002963 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002964 return 1;
2965}
2966
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002967/* This function check an operator entry. It expects a string.
2968 * The string can be an integer or a variable name.
2969 */
2970static int check_operator(struct arg *args, struct sample_conv *conv,
2971 const char *file, int line, char **err)
2972{
2973 const char *str;
2974 const char *end;
Christopher Faulet95917132020-08-05 23:07:07 +02002975 long long int i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002976
2977 /* Try to decode a variable. */
2978 if (vars_check_arg(&args[0], NULL))
2979 return 1;
2980
2981 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002982 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002983 end = str + strlen(str);
Christopher Faulet95917132020-08-05 23:07:07 +02002984 i = read_int64(&str, end);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002985 if (*str != '\0') {
2986 memprintf(err, "expects an integer or a variable name");
2987 return 0;
2988 }
Christopher Faulet95917132020-08-05 23:07:07 +02002989
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002990 chunk_destroy(&args[0].data.str);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002991 args[0].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02002992 args[0].data.sint = i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002993 return 1;
2994}
2995
Willy Tarreau6204cd92016-03-10 16:33:04 +01002996/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002997 * If the arg contain an integer, the integer is returned in the
2998 * sample. If the arg contains a variable descriptor, it returns the
2999 * variable value.
3000 *
3001 * This function returns 0 if an error occurs, otherwise it returns 1.
3002 */
Willy Tarreaud9be5992021-10-06 15:19:05 +02003003int sample_conv_var2smp_sint(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003004{
3005 switch (arg->type) {
3006 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003007 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003008 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003009 return 1;
3010 case ARGT_VAR:
Willy Tarreau2476ff12021-10-06 15:30:52 +02003011 return sample_conv_var2smp(&arg->data.var, smp, SMP_T_SINT);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003012 default:
3013 return 0;
3014 }
3015}
3016
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003017/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01003018 * result.
3019 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003020static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003021{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003022 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003023 return 1;
3024}
3025
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003026/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003027 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01003028 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003029static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003030{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003031 struct sample tmp;
3032
Willy Tarreau7560dd42016-03-10 16:28:58 +01003033 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02003034 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003035 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003036 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003037 return 1;
3038}
3039
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003040/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003041 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01003042 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003043static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003044{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003045 struct sample tmp;
3046
Willy Tarreau7560dd42016-03-10 16:28:58 +01003047 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02003048 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003049 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003050 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003051 return 1;
3052}
3053
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003054/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003055 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01003056 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003057static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003058{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003059 struct sample tmp;
3060
Willy Tarreau7560dd42016-03-10 16:28:58 +01003061 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02003062 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003063 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003064 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003065 return 1;
3066}
3067
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003068static inline long long int arith_add(long long int a, long long int b)
3069{
3070 /* Prevent overflow and makes capped calculus.
3071 * We must ensure that the check calculus doesn't
3072 * exceed the signed 64 bits limits.
3073 *
3074 * +----------+----------+
3075 * | a<0 | a>=0 |
3076 * +------+----------+----------+
3077 * | b<0 | MIN-a>b | no check |
3078 * +------+----------+----------+
3079 * | b>=0 | no check | MAX-a<b |
3080 * +------+----------+----------+
3081 */
3082 if ((a ^ b) >= 0) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003083 /* signs are different. */
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003084 if (a < 0) {
3085 if (LLONG_MIN - a > b)
3086 return LLONG_MIN;
3087 }
3088 if (LLONG_MAX - a < b)
3089 return LLONG_MAX;
3090 }
3091 return a + b;
3092}
3093
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003094/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003095 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01003096 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003097static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003098{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003099 struct sample tmp;
3100
Willy Tarreau7560dd42016-03-10 16:28:58 +01003101 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02003102 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003103 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003104 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01003105 return 1;
3106}
3107
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003108/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003109 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01003110 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003111static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003112 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003113{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003114 struct sample tmp;
3115
Willy Tarreau7560dd42016-03-10 16:28:58 +01003116 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02003117 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003118 return 0;
3119
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003120 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
3121 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
3122 * of -LLONG_MIN and correct the result.
3123 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003124 if (tmp.data.u.sint == LLONG_MIN) {
3125 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
3126 if (smp->data.u.sint < LLONG_MAX)
3127 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003128 return 1;
3129 }
3130
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003131 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003132 * the second operand.
3133 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003134 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01003135 return 1;
3136}
3137
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003138/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003139 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003140 * If the result makes an overflow, then the largest possible quantity is
3141 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003142 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003143static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003144 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003145{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003146 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003147 long long int c;
3148
Willy Tarreau7560dd42016-03-10 16:28:58 +01003149 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02003150 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003151 return 0;
3152
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003153 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003154 if (!smp->data.u.sint || !tmp.data.u.sint) {
3155 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003156 return 1;
3157 }
3158
3159 /* The multiply between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003160 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003161 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003162 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3163 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003164 return 1;
3165 }
3166
3167 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003168 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003169
3170 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003171 if (smp->data.u.sint != c / tmp.data.u.sint) {
3172 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
3173 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003174 return 1;
3175 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003176 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003177 return 1;
3178 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003179 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01003180 return 1;
3181}
3182
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003183/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003184 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003185 * If arg_p makes the result overflow, then the largest possible quantity is
3186 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003187 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003188static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003189 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003190{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003191 struct sample tmp;
3192
Willy Tarreau7560dd42016-03-10 16:28:58 +01003193 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02003194 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003195 return 0;
3196
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003197 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003198 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003199 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003200 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003201 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3202 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003203 return 1;
3204 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003205 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003206 return 1;
3207 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003208 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01003209 return 1;
3210}
3211
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003212/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003213 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003214 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003215 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003216static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003217 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003218{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003219 struct sample tmp;
3220
Willy Tarreau7560dd42016-03-10 16:28:58 +01003221 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaud9be5992021-10-06 15:19:05 +02003222 if (!sample_conv_var2smp_sint(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003223 return 0;
3224
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003225 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003226 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003227 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003228 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003229 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3230 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003231 return 1;
3232 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003233 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003234 return 1;
3235 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003236 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01003237 return 1;
3238}
3239
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003240/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01003241 * result.
3242 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003243static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003244 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003245{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003246 if (smp->data.u.sint == LLONG_MIN)
3247 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003248 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003249 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003250 return 1;
3251}
3252
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003253/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003254 * false. The output is a BOOL.
3255 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003256static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003257 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003258{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003259 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003260 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003261 return 1;
3262}
3263
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003264/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003265 * truee. The output is a BOOL.
3266 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003267static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003268 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003269{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003270 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003271 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003272 return 1;
3273}
3274
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003275/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003276 * The output is a BOOL.
3277 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003278static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003279 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003280{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003281 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003282 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003283 return 1;
3284}
3285
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003286/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003287 * The output is a BOOL.
3288 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003289static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003290 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003291{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003292 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003293 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003294 return 1;
3295}
3296
Willy Tarreau280f42b2018-02-19 15:34:12 +01003297/* appends an optional const string, an optional variable contents and another
3298 * optional const string to an existing string.
3299 */
3300static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
3301{
Willy Tarreau83061a82018-07-13 11:56:34 +02003302 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003303 struct sample tmp;
3304 int max;
3305
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003306 trash = alloc_trash_chunk();
William Dauchye9970102021-01-11 11:05:58 +01003307 if (!trash)
3308 return 0;
3309
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003310 trash->data = smp->data.u.str.data;
3311 if (trash->data > trash->size - 1)
3312 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003313
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003314 memcpy(trash->area, smp->data.u.str.area, trash->data);
3315 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003316
3317 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003318 max = arg_p[0].data.str.data;
3319 if (max > trash->size - 1 - trash->data)
3320 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003321
3322 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003323 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
3324 trash->data += max;
3325 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003326 }
3327
3328 /* append second string (variable) if it's found and we can turn it
3329 * into a string.
3330 */
3331 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreaue352b9d2021-09-03 11:52:38 +02003332 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp, NULL) &&
Willy Tarreau280f42b2018-02-19 15:34:12 +01003333 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
3334 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
3335
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003336 max = tmp.data.u.str.data;
3337 if (max > trash->size - 1 - trash->data)
3338 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003339
3340 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003341 memcpy(trash->area + trash->data, tmp.data.u.str.area,
3342 max);
3343 trash->data += max;
3344 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003345 }
3346 }
3347
3348 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003349 max = arg_p[2].data.str.data;
3350 if (max > trash->size - 1 - trash->data)
3351 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003352
3353 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003354 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
3355 trash->data += max;
3356 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003357 }
3358
3359 smp->data.u.str = *trash;
3360 smp->data.type = SMP_T_STR;
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003361 smp_dup(smp);
3362 free_trash_chunk(trash);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003363 return 1;
3364}
3365
3366/* This function checks the "concat" converter's arguments and extracts the
3367 * variable name and its scope.
3368 */
3369static int smp_check_concat(struct arg *args, struct sample_conv *conv,
3370 const char *file, int line, char **err)
3371{
3372 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003373 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
3374 memprintf(err, "failed to register variable name '%s'",
3375 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003376 return 0;
3377 }
3378 return 1;
3379}
3380
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003381/* Compares string with a variable containing a string. Return value
Tim Duesterhusca097c12018-04-27 21:18:45 +02003382 * is compatible with strcmp(3)'s return value.
3383 */
3384static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
3385{
3386 struct sample tmp;
3387 int max, result;
3388
3389 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3390 if (arg_p[0].type != ARGT_VAR)
3391 return 0;
Willy Tarreau2476ff12021-10-06 15:30:52 +02003392
3393 if (!sample_conv_var2smp(&arg_p[0].data.var, &tmp, SMP_T_STR))
Tim Duesterhusca097c12018-04-27 21:18:45 +02003394 return 0;
3395
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003396 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
3397 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003398 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003399 if (smp->data.u.str.data != tmp.data.u.str.data) {
3400 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02003401 result = -1;
3402 }
3403 else {
3404 result = 1;
3405 }
3406 }
3407 }
3408
3409 smp->data.u.sint = result;
3410 smp->data.type = SMP_T_SINT;
3411 return 1;
3412}
3413
Willy Tarreau2a8a2f02021-05-09 11:59:49 +02003414#if defined(HAVE_CRYPTO_memcmp)
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003415/* Compares bytestring with a variable containing a bytestring. Return value
3416 * is `true` if both bytestrings are bytewise identical and `false` otherwise.
3417 *
3418 * Comparison will be performed in constant time if both bytestrings are of
3419 * the same length. If the lengths differ execution time will not be constant.
3420 */
3421static int sample_conv_secure_memcmp(const struct arg *arg_p, struct sample *smp, void *private)
3422{
3423 struct sample tmp;
3424 int result;
3425
3426 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3427 if (arg_p[0].type != ARGT_VAR)
3428 return 0;
Willy Tarreau2476ff12021-10-06 15:30:52 +02003429
3430 if (!sample_conv_var2smp(&arg_p[0].data.var, &tmp, SMP_T_BIN))
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003431 return 0;
3432
3433 if (smp->data.u.str.data != tmp.data.u.str.data) {
3434 smp->data.u.sint = 0;
3435 smp->data.type = SMP_T_BOOL;
3436 return 1;
3437 }
3438
3439 /* The following comparison is performed in constant time. */
3440 result = CRYPTO_memcmp(smp->data.u.str.area, tmp.data.u.str.area, smp->data.u.str.data);
3441
3442 smp->data.u.sint = result == 0;
3443 smp->data.type = SMP_T_BOOL;
3444 return 1;
3445}
3446#endif
3447
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02003448/* Takes a boolean as input. Returns the first argument if that boolean is true and
3449 * the second argument otherwise.
3450 */
3451static int sample_conv_iif(const struct arg *arg_p, struct sample *smp, void *private)
3452{
3453 smp->data.type = SMP_T_STR;
3454 smp->flags |= SMP_F_CONST;
3455
3456 if (smp->data.u.sint) {
3457 smp->data.u.str.data = arg_p[0].data.str.data;
3458 smp->data.u.str.area = arg_p[0].data.str.area;
3459 }
3460 else {
3461 smp->data.u.str.data = arg_p[1].data.str.data;
3462 smp->data.u.str.area = arg_p[1].data.str.area;
3463 }
3464
3465 return 1;
3466}
3467
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003468#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
3469#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
3470#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
3471
3472/*
3473 * Extract the field value of an input binary sample. Takes a mandatory argument:
3474 * the protocol buffers field identifier (dotted notation) internally represented
3475 * as an array of unsigned integers and its size.
3476 * Return 1 if the field was found, 0 if not.
3477 */
3478static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
3479{
3480 unsigned char *pos;
3481 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003482
3483 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003484 grpc_left = smp->data.u.str.data;
3485
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01003486 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003487 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003488
3489 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
3490
3491 pos += GRPC_MSG_HEADER_SZ;
3492 grpc_left -= GRPC_MSG_HEADER_SZ;
3493
3494 if (grpc_left < left)
3495 return 0;
3496
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01003497 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
3498 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003499
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003500 grpc_left -= grpc_msg_len;
3501 }
3502
3503 return 0;
3504}
3505
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003506static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
3507{
3508 unsigned char *pos;
3509 size_t left;
3510
3511 pos = (unsigned char *)smp->data.u.str.area;
3512 left = smp->data.u.str.data;
3513
3514 return protobuf_field_lookup(arg_p, smp, &pos, &left);
3515}
3516
3517static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
3518 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003519{
3520 if (!args[1].type) {
3521 args[1].type = ARGT_SINT;
3522 args[1].data.sint = PBUF_T_BINARY;
3523 }
3524 else {
3525 int pbuf_type;
3526
3527 pbuf_type = protobuf_type(args[1].data.str.area);
3528 if (pbuf_type == -1) {
3529 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
3530 return 0;
3531 }
3532
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003533 chunk_destroy(&args[1].data.str);
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003534 args[1].type = ARGT_SINT;
3535 args[1].data.sint = pbuf_type;
3536 }
3537
3538 return 1;
3539}
3540
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003541/*
3542 * Extract the tag value of an input binary sample. Takes a mandatory argument:
3543 * the FIX protocol tag identifier.
3544 * Return 1 if the tag was found, 0 if not.
3545 */
3546static int sample_conv_fix_tag_value(const struct arg *arg_p, struct sample *smp, void *private)
3547{
3548 struct ist value;
3549
3550 smp->flags &= ~SMP_F_MAY_CHANGE;
3551 value = fix_tag_value(ist2(smp->data.u.str.area, smp->data.u.str.data),
3552 arg_p[0].data.sint);
3553 if (!istlen(value)) {
3554 if (!isttest(value)) {
3555 /* value != IST_NULL, need more data */
3556 smp->flags |= SMP_F_MAY_CHANGE;
3557 }
3558 return 0;
3559 }
3560
3561 smp->data.u.str = ist2buf(value);
3562 smp->flags |= SMP_F_CONST;
3563
3564 return 1;
3565}
3566
3567/* This function checks the "fix_tag_value" converter configuration.
3568 * It expects a "known" (by HAProxy) tag name or ID.
3569 * Tag string names are converted to their ID counterpart because this is the
3570 * format they are sent over the wire.
3571 */
3572static int sample_conv_fix_value_check(struct arg *args, struct sample_conv *conv,
3573 const char *file, int line, char **err)
3574{
3575 struct ist str;
3576 unsigned int tag;
3577
3578 str = ist2(args[0].data.str.area, args[0].data.str.data);
3579 tag = fix_tagid(str);
3580 if (!tag) {
3581 memprintf(err, "Unknown FIX tag name '%s'", args[0].data.str.area);
3582 return 0;
3583 }
3584
3585 chunk_destroy(&args[0].data.str);
3586 args[0].type = ARGT_SINT;
3587 args[0].data.sint = tag;
3588
3589 return 1;
3590}
3591
3592/*
3593 * Checks that a buffer contains a valid FIX message
3594 *
3595 * Return 1 if the check could be run, 0 if not.
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003596 * The result of the analyse itself is stored in <smp> as a boolean
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003597 */
3598static int sample_conv_fix_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3599{
3600 struct ist msg;
3601
3602 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3603
3604 smp->flags &= ~SMP_F_MAY_CHANGE;
3605 switch (fix_validate_message(msg)) {
3606 case FIX_VALID_MESSAGE:
3607 smp->data.type = SMP_T_BOOL;
3608 smp->data.u.sint = 1;
3609 return 1;
3610 case FIX_NEED_MORE_DATA:
3611 smp->flags |= SMP_F_MAY_CHANGE;
3612 return 0;
3613 case FIX_INVALID_MESSAGE:
3614 smp->data.type = SMP_T_BOOL;
3615 smp->data.u.sint = 0;
3616 return 1;
3617 }
3618 return 0;
3619}
3620
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003621/*
3622 * Extract the field value of an input binary sample containing an MQTT packet.
3623 * Takes 2 mandatory arguments:
3624 * - packet type
3625 * - field name
3626 *
3627 * return 1 if the field was found, 0 if not.
3628 */
3629static int sample_conv_mqtt_field_value(const struct arg *arg_p, struct sample *smp, void *private)
3630{
3631 struct ist pkt, value;
3632 int type, fieldname_id;
3633
3634 pkt = ist2(smp->data.u.str.area, smp->data.u.str.data);
3635 type = arg_p[0].data.sint;
3636 fieldname_id = arg_p[1].data.sint;
3637
3638 smp->flags &= ~SMP_F_MAY_CHANGE;
3639 value = mqtt_field_value(pkt, type, fieldname_id);
3640 if (!istlen(value)) {
3641 if (isttest(value)) {
3642 /* value != IST_NULL, need more data */
3643 smp->flags |= SMP_F_MAY_CHANGE;
3644 }
3645 return 0;
3646 }
3647
3648 smp->data.u.str = ist2buf(value);
3649 smp->flags |= SMP_F_CONST;
3650 return 1;
3651}
3652
3653/*
3654 * this function checks the "mqtt_field_value" converter configuration.
3655 * It expects a known packet type name or ID and a field name, in this order
3656 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003657 * Args[0] will be turned into a MQTT_CPT_* value for direct matching when parsing
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003658 * a packet.
3659 */
3660static int sample_conv_mqtt_field_value_check(struct arg *args, struct sample_conv *conv,
3661 const char *file, int line, char **err)
3662{
3663 int type, fieldname_id;
3664
3665 /* check the MQTT packet type is valid */
3666 type = mqtt_typeid(ist2(args[0].data.str.area, args[0].data.str.data));
3667 if (type == MQTT_CPT_INVALID) {
3668 memprintf(err, "Unknown MQTT type '%s'", args[0].data.str.area);
3669 return 0;
3670 }
3671
3672 /* check the field name belongs to the MQTT packet type */
3673 fieldname_id = mqtt_check_type_fieldname(type, ist2(args[1].data.str.area, args[1].data.str.data));
3674 if (fieldname_id == MQTT_FN_INVALID) {
3675 memprintf(err, "Unknown MQTT field name '%s' for packet type '%s'", args[1].data.str.area,
3676 args[0].data.str.area);
3677 return 0;
3678 }
3679
3680 /* save numeric counterparts of type and field name */
3681 chunk_destroy(&args[0].data.str);
3682 chunk_destroy(&args[1].data.str);
3683 args[0].type = ARGT_SINT;
3684 args[0].data.sint = type;
3685 args[1].type = ARGT_SINT;
3686 args[1].data.sint = fieldname_id;
3687
3688 return 1;
3689}
3690
3691/*
3692 * Checks that <smp> contains a valid MQTT message
3693 *
3694 * The function returns 1 if the check was run to its end, 0 otherwise.
3695 * The result of the analyse itself is stored in <smp> as a boolean.
3696 */
3697static int sample_conv_mqtt_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3698{
3699 struct ist msg;
3700
3701 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3702
3703 smp->flags &= ~SMP_F_MAY_CHANGE;
3704 switch (mqtt_validate_message(msg, NULL)) {
3705 case FIX_VALID_MESSAGE:
3706 smp->data.type = SMP_T_BOOL;
3707 smp->data.u.sint = 1;
3708 return 1;
3709 case FIX_NEED_MORE_DATA:
3710 smp->flags |= SMP_F_MAY_CHANGE;
3711 return 0;
3712 case FIX_INVALID_MESSAGE:
3713 smp->data.type = SMP_T_BOOL;
3714 smp->data.u.sint = 0;
3715 return 1;
3716 }
3717 return 0;
3718}
3719
Tim Duesterhusca097c12018-04-27 21:18:45 +02003720/* This function checks the "strcmp" converter's arguments and extracts the
3721 * variable name and its scope.
3722 */
3723static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
3724 const char *file, int line, char **err)
3725{
Willy Tarreaua1169b62021-05-08 06:50:28 +02003726 if (!args[0].data.str.data) {
3727 memprintf(err, "missing variable name");
3728 return 0;
3729 }
3730
Tim Duesterhusca097c12018-04-27 21:18:45 +02003731 /* Try to decode a variable. */
3732 if (vars_check_arg(&args[0], NULL))
3733 return 1;
3734
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003735 memprintf(err, "failed to register variable name '%s'",
3736 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003737 return 0;
3738}
3739
Willy Tarreau2a8a2f02021-05-09 11:59:49 +02003740#if defined(HAVE_CRYPTO_memcmp)
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003741/* This function checks the "secure_memcmp" converter's arguments and extracts the
3742 * variable name and its scope.
3743 */
3744static int smp_check_secure_memcmp(struct arg *args, struct sample_conv *conv,
3745 const char *file, int line, char **err)
3746{
Willy Tarreaua1169b62021-05-08 06:50:28 +02003747 if (!args[0].data.str.data) {
3748 memprintf(err, "missing variable name");
3749 return 0;
3750 }
3751
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003752 /* Try to decode a variable. */
3753 if (vars_check_arg(&args[0], NULL))
3754 return 1;
3755
3756 memprintf(err, "failed to register variable name '%s'",
3757 args[0].data.str.area);
3758 return 0;
3759}
3760#endif
3761
Christopher Faulet4ccc12f2020-04-01 09:08:32 +02003762/**/
3763static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private)
3764{
3765 struct buffer *tmp;
3766 uint32_t n;
3767
3768 n = htonl((uint32_t)smp->data.u.sint);
3769 tmp = get_trash_chunk();
3770
3771 memcpy(b_head(tmp), &n, 4);
3772 b_add(tmp, 4);
3773
3774 smp->data.u.str = *tmp;
3775 smp->data.type = SMP_T_BIN;
3776 return 1;
3777}
3778
Christopher Fauletea159d62020-04-01 16:21:44 +02003779/**/
3780static int sample_conv_cut_crlf(const struct arg *arg_p, struct sample *smp, void *private)
3781{
3782 char *p;
3783 size_t l;
3784
3785 p = smp->data.u.str.area;
3786 for (l = 0; l < smp->data.u.str.data; l++) {
3787 if (*(p+l) == '\r' || *(p+l) == '\n')
3788 break;
3789 }
3790 smp->data.u.str.data = l;
3791 return 1;
3792}
3793
Christopher Faulet51fc9d12020-04-01 17:24:41 +02003794/**/
3795static int sample_conv_ltrim(const struct arg *arg_p, struct sample *smp, void *private)
3796{
3797 char *delimiters, *p;
3798 size_t dlen, l;
3799
3800 delimiters = arg_p[0].data.str.area;
3801 dlen = arg_p[0].data.str.data;
3802
3803 l = smp->data.u.str.data;
3804 p = smp->data.u.str.area;
3805 while (l && memchr(delimiters, *p, dlen) != NULL) {
3806 p++;
3807 l--;
3808 }
3809
3810 smp->data.u.str.area = p;
3811 smp->data.u.str.data = l;
3812 return 1;
3813}
3814
Christopher Faulet568415a2020-04-01 17:24:47 +02003815/**/
3816static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *private)
3817{
3818 char *delimiters, *p;
3819 size_t dlen, l;
3820
3821 delimiters = arg_p[0].data.str.area;
3822 dlen = arg_p[0].data.str.data;
3823
3824 l = smp->data.u.str.data;
3825 p = smp->data.u.str.area + l - 1;
3826 while (l && memchr(delimiters, *p, dlen) != NULL) {
3827 p--;
3828 l--;
3829 }
3830
3831 smp->data.u.str.data = l;
3832 return 1;
3833}
3834
Alex51c8ad42021-04-15 16:45:15 +02003835/* This function checks the "json_query" converter's arguments. */
3836static int sample_check_json_query(struct arg *arg, struct sample_conv *conv,
3837 const char *file, int line, char **err)
3838{
3839 if (arg[0].data.str.data == 0) {
3840 memprintf(err, "json_path must not be empty");
3841 return 0;
3842 }
3843
3844 if (arg[1].data.str.data != 0) {
3845 if (strcmp(arg[1].data.str.area, "int") != 0) {
3846 memprintf(err, "output_type only supports \"int\" as argument");
3847 return 0;
3848 } else {
3849 arg[1].type = ARGT_SINT;
3850 arg[1].data.sint = 0;
3851 }
3852 }
3853 return 1;
3854}
3855
3856/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
3857 * the recommendation for interoperable integers in section 6 of RFC 7159.
3858 */
3859#define JSON_INT_MAX ((1LL << 53) - 1)
3860#define JSON_INT_MIN (-JSON_INT_MAX)
3861
3862/* This sample function get the value from a given json string.
3863 * The mjson library is used to parse the JSON struct
3864 */
3865static int sample_conv_json_query(const struct arg *args, struct sample *smp, void *private)
3866{
3867 struct buffer *trash = get_trash_chunk();
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003868 const char *token; /* holds the temporary string from mjson_find */
3869 int token_size; /* holds the length of <token> */
Alex51c8ad42021-04-15 16:45:15 +02003870
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003871 enum mjson_tok token_type;
Alex51c8ad42021-04-15 16:45:15 +02003872
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003873 token_type = mjson_find(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &token, &token_size);
3874
3875 switch (token_type) {
Alex51c8ad42021-04-15 16:45:15 +02003876 case MJSON_TOK_NUMBER:
3877 if (args[1].type == ARGT_SINT) {
Willy Tarreaufb601952021-05-14 08:51:53 +02003878 smp->data.u.sint = strtoll(token, NULL, 0);
Alex51c8ad42021-04-15 16:45:15 +02003879
3880 if (smp->data.u.sint < JSON_INT_MIN || smp->data.u.sint > JSON_INT_MAX)
3881 return 0;
3882
3883 smp->data.type = SMP_T_SINT;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003884
3885 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003886 } else {
3887 double double_val;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003888
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003889 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 +02003890 return 0;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003891
3892 trash->data = snprintf(trash->area,trash->size,"%g",double_val);
3893 smp->data.u.str = *trash;
3894 smp->data.type = SMP_T_STR;
3895
3896 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003897 }
Alex51c8ad42021-04-15 16:45:15 +02003898 case MJSON_TOK_TRUE:
3899 smp->data.type = SMP_T_BOOL;
3900 smp->data.u.sint = 1;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003901
3902 return 1;
Alex51c8ad42021-04-15 16:45:15 +02003903 case MJSON_TOK_FALSE:
3904 smp->data.type = SMP_T_BOOL;
3905 smp->data.u.sint = 0;
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003906
3907 return 1;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003908 case MJSON_TOK_STRING: {
3909 int len;
3910
3911 len = mjson_get_string(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, trash->area, trash->size);
3912
3913 if (len == -1) {
Alex51c8ad42021-04-15 16:45:15 +02003914 /* invalid string */
3915 return 0;
Alex51c8ad42021-04-15 16:45:15 +02003916 }
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003917
3918 trash->data = len;
3919 smp->data.u.str = *trash;
3920 smp->data.type = SMP_T_STR;
3921
3922 return 1;
Tim Duesterhus4809c8c2021-04-15 18:08:48 +02003923 }
Tim Duesterhus8f3bc8f2021-04-15 18:14:32 +02003924 case MJSON_TOK_NULL:
3925 case MJSON_TOK_ARRAY:
3926 case MJSON_TOK_OBJECT:
3927 /* We cannot handle these. */
3928 return 0;
3929 case MJSON_TOK_INVALID:
3930 /* Nothing matches the query. */
3931 return 0;
3932 case MJSON_TOK_KEY:
3933 /* This is not a valid return value according to the
3934 * mjson documentation, but we handle it to benefit
3935 * from '-Wswitch'.
3936 */
Alex51c8ad42021-04-15 16:45:15 +02003937 return 0;
3938 }
Tim Duesterhus3b9cdf12021-04-15 18:40:06 +02003939
3940 my_unreachable();
3941 return 0;
Alex51c8ad42021-04-15 16:45:15 +02003942}
3943
3944
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003945/************************************************************************/
3946/* All supported sample fetch functions must be declared here */
3947/************************************************************************/
3948
3949/* force TRUE to be returned at the fetch level */
3950static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003951smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003952{
Willy Tarreau280f42b2018-02-19 15:34:12 +01003953 if (!smp_make_rw(smp))
3954 return 0;
3955
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003956 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003957 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003958 return 1;
3959}
3960
3961/* force FALSE to be returned at the fetch level */
3962static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003963smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003964{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003965 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003966 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003967 return 1;
3968}
3969
3970/* retrieve environment variable $1 as a string */
3971static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003972smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003973{
3974 char *env;
3975
Christopher Faulete6e7a582021-01-29 11:29:28 +01003976 if (args[0].type != ARGT_STR)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003977 return 0;
3978
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003979 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003980 if (!env)
3981 return 0;
3982
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003983 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003984 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003985 smp->data.u.str.area = env;
3986 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003987 return 1;
3988}
3989
Damien Claisseae6f1252019-10-30 15:57:28 +00003990/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3991 * optional string representing the unit of the result: "s" for seconds, "ms" for
3992 * milliseconds and "us" for microseconds.
3993 * Returns 0 on error and non-zero if OK.
3994 */
3995int smp_check_date_unit(struct arg *args, char **err)
3996{
3997 if (args[1].type == ARGT_STR) {
Christopher Faulet95917132020-08-05 23:07:07 +02003998 long long int unit;
3999
Damien Claisseae6f1252019-10-30 15:57:28 +00004000 if (strcmp(args[1].data.str.area, "s") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02004001 unit = TIME_UNIT_S;
Damien Claisseae6f1252019-10-30 15:57:28 +00004002 }
4003 else if (strcmp(args[1].data.str.area, "ms") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02004004 unit = TIME_UNIT_MS;
Damien Claisseae6f1252019-10-30 15:57:28 +00004005 }
4006 else if (strcmp(args[1].data.str.area, "us") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02004007 unit = TIME_UNIT_US;
Damien Claisseae6f1252019-10-30 15:57:28 +00004008 }
4009 else {
4010 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
4011 args[1].data.str.area);
4012 return 0;
4013 }
Christopher Faulet95917132020-08-05 23:07:07 +02004014
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004015 chunk_destroy(&args[1].data.str);
Damien Claisseae6f1252019-10-30 15:57:28 +00004016 args[1].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02004017 args[1].data.sint = unit;
Damien Claisseae6f1252019-10-30 15:57:28 +00004018 }
4019 else if (args[1].type != ARGT_STOP) {
4020 memprintf(err, "Unexpected arg type");
4021 return 0;
4022 }
4023
4024 return 1;
4025}
4026
4027/* retrieve the current local date in epoch time, converts it to milliseconds
4028 * or microseconds if asked to in optional args[1] unit param, and applies an
4029 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004030 */
4031static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004032smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004033{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004034 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004035
Damien Claisseae6f1252019-10-30 15:57:28 +00004036 /* report in milliseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01004037 if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
Damien Claisseae6f1252019-10-30 15:57:28 +00004038 smp->data.u.sint *= 1000;
4039 smp->data.u.sint += date.tv_usec / 1000;
4040 }
4041 /* report in microseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01004042 else if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
Damien Claisseae6f1252019-10-30 15:57:28 +00004043 smp->data.u.sint *= 1000000;
4044 smp->data.u.sint += date.tv_usec;
4045 }
4046
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004047 /* add offset */
Christopher Faulete6e7a582021-01-29 11:29:28 +01004048 if (args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004049 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004050
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004051 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02004052 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4053 return 1;
4054}
4055
Etienne Carrierea792a0a2018-01-17 13:43:24 +01004056/* retrieve the current microsecond part of the date */
4057static int
4058smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
4059{
4060 smp->data.u.sint = date.tv_usec;
4061 smp->data.type = SMP_T_SINT;
4062 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4063 return 1;
4064}
4065
4066
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01004067/* returns the hostname */
4068static int
4069smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
4070{
4071 smp->data.type = SMP_T_STR;
4072 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004073 smp->data.u.str.area = hostname;
4074 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01004075 return 1;
4076}
4077
Willy Tarreau0f30d262014-11-24 16:02:05 +01004078/* returns the number of processes */
4079static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004080smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01004081{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004082 smp->data.type = SMP_T_SINT;
Willy Tarreau91358592021-06-15 08:08:04 +02004083 smp->data.u.sint = 1;
Willy Tarreau0f30d262014-11-24 16:02:05 +01004084 return 1;
4085}
4086
4087/* returns the number of the current process (between 1 and nbproc */
4088static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004089smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01004090{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004091 smp->data.type = SMP_T_SINT;
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004092 smp->data.u.sint = 1;
Willy Tarreau0f30d262014-11-24 16:02:05 +01004093 return 1;
4094}
4095
Christopher Faulet34adb2a2017-11-21 21:45:38 +01004096/* returns the number of the current thread (between 1 and nbthread */
4097static int
4098smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
4099{
4100 smp->data.type = SMP_T_SINT;
4101 smp->data.u.sint = tid;
4102 return 1;
4103}
4104
Willy Tarreau84310e22014-02-14 11:59:04 +01004105/* generate a random 32-bit integer for whatever purpose, with an optional
4106 * range specified in argument.
4107 */
4108static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004109smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01004110{
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01004111 smp->data.u.sint = ha_random32();
Willy Tarreau84310e22014-02-14 11:59:04 +01004112
4113 /* reduce if needed. Don't do a modulo, use all bits! */
Christopher Faulete6e7a582021-01-29 11:29:28 +01004114 if (args[0].type == ARGT_SINT)
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01004115 smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32;
Willy Tarreau84310e22014-02-14 11:59:04 +01004116
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004117 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01004118 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4119 return 1;
4120}
4121
Willy Tarreau0f30d262014-11-24 16:02:05 +01004122/* returns true if the current process is stopping */
4123static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004124smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01004125{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004126 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004127 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01004128 return 1;
4129}
4130
Willy Tarreau70fe9442018-11-22 16:07:39 +01004131/* returns the number of calls of the current stream's process_stream() */
4132static int
4133smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
4134{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004135 if (!smp->strm)
4136 return 0;
4137
Willy Tarreau70fe9442018-11-22 16:07:39 +01004138 smp->data.type = SMP_T_SINT;
4139 smp->data.u.sint = smp->strm->task->calls;
4140 return 1;
4141}
4142
4143/* returns the average number of nanoseconds spent processing the stream per call */
4144static int
4145smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
4146{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004147 if (!smp->strm)
4148 return 0;
4149
Willy Tarreau70fe9442018-11-22 16:07:39 +01004150 smp->data.type = SMP_T_SINT;
4151 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
4152 return 1;
4153}
4154
4155/* returns the total number of nanoseconds spent processing the stream */
4156static int
4157smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
4158{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004159 if (!smp->strm)
4160 return 0;
4161
Willy Tarreau70fe9442018-11-22 16:07:39 +01004162 smp->data.type = SMP_T_SINT;
4163 smp->data.u.sint = smp->strm->task->cpu_time;
4164 return 1;
4165}
4166
4167/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
4168static int
4169smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
4170{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004171 if (!smp->strm)
4172 return 0;
4173
Willy Tarreau70fe9442018-11-22 16:07:39 +01004174 smp->data.type = SMP_T_SINT;
4175 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
4176 return 1;
4177}
4178
4179/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
4180static int
4181smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
4182{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02004183 if (!smp->strm)
4184 return 0;
4185
Willy Tarreau70fe9442018-11-22 16:07:39 +01004186 smp->data.type = SMP_T_SINT;
4187 smp->data.u.sint = smp->strm->task->lat_time;
4188 return 1;
4189}
4190
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004191static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
4192{
4193 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004194 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004195 smp->data.u.str.area = args[0].data.str.area;
4196 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004197 return 1;
4198}
4199
4200static int smp_check_const_bool(struct arg *args, char **err)
4201{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004202 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
4203 strcasecmp(args[0].data.str.area, "1") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004204 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004205 args[0].type = ARGT_SINT;
4206 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004207 return 1;
4208 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004209 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
4210 strcasecmp(args[0].data.str.area, "0") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004211 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004212 args[0].type = ARGT_SINT;
4213 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004214 return 1;
4215 }
4216 memprintf(err, "Expects 'true', 'false', '0' or '1'");
4217 return 0;
4218}
4219
4220static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
4221{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004222 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004223 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004224 return 1;
4225}
4226
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02004227static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004228{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004229 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004230 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004231 return 1;
4232}
4233
4234static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
4235{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004236 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004237 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004238 return 1;
4239}
4240
4241static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
4242{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004243 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004244 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004245 return 1;
4246}
4247
4248static int smp_check_const_bin(struct arg *args, char **err)
4249{
David Carlier64a16ab2016-04-08 10:37:02 +01004250 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004251 int binstrlen;
4252
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004253 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004254 return 0;
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004255 chunk_destroy(&args[0].data.str);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004256 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004257 args[0].data.str.area = binstr;
4258 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004259 return 1;
4260}
4261
4262static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
4263{
4264 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004265 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004266 smp->data.u.str.area = args[0].data.str.area;
4267 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004268 return 1;
4269}
4270
4271static int smp_check_const_meth(struct arg *args, char **err)
4272{
4273 enum http_meth_t meth;
4274 int i;
4275
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004276 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004277 if (meth != HTTP_METH_OTHER) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02004278 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004279 args[0].type = ARGT_SINT;
4280 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004281 } else {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05004282 /* Check method avalaibility. A method is a token defined as :
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004283 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
4284 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
4285 * token = 1*tchar
4286 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004287 for (i = 0; i < args[0].data.str.data; i++) {
4288 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004289 memprintf(err, "expects valid method.");
4290 return 0;
4291 }
4292 }
4293 }
4294 return 1;
4295}
4296
4297static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
4298{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004299 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004300 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004301 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004302 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004303 smp->data.u.meth.str.area = "";
4304 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004305 } else {
4306 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02004307 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004308 smp->data.u.meth.str.area = args[0].data.str.area;
4309 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004310 }
4311 return 1;
4312}
4313
Luca Schimweg8a694b82019-09-10 15:42:52 +02004314// This function checks the "uuid" sample's arguments.
4315// Function won't get called when no parameter is specified (maybe a bug?)
4316static int smp_check_uuid(struct arg *args, char **err)
4317{
4318 if (!args[0].type) {
4319 args[0].type = ARGT_SINT;
4320 args[0].data.sint = 4;
4321 }
4322 else if (args[0].data.sint != 4) {
4323 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
4324 return 0;
4325 }
4326
4327 return 1;
4328}
4329
4330// Generate a RFC4122 UUID (default is v4 = fully random)
4331static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
4332{
4333 if (args[0].data.sint == 4 || !args[0].type) {
Willy Tarreauee3bcdd2020-03-08 17:48:17 +01004334 ha_generate_uuid(&trash);
Luca Schimweg8a694b82019-09-10 15:42:52 +02004335 smp->data.type = SMP_T_STR;
4336 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4337 smp->data.u.str = trash;
4338 return 1;
4339 }
4340
4341 // more implementations of other uuid formats possible here
4342 return 0;
4343}
4344
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004345/* Note: must not be declared <const> as its list will be overwritten.
4346 * Note: fetches that may return multiple types must be declared as the lowest
4347 * common denominator, the type that can be casted into all other ones. For
4348 * instance IPv4/IPv6 must be declared IPv4.
4349 */
4350static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau0209c972021-03-26 12:03:11 +01004351 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4352 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4353 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_CONST },
4354 { "date", smp_fetch_date, ARG2(0,SINT,STR), smp_check_date_unit, SMP_T_SINT, SMP_USE_CONST },
4355 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4356 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_CONST },
4357 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_CONST },
4358 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4359 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4360 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_CONST },
Willy Tarreau0f30d262014-11-24 16:02:05 +01004361 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau0209c972021-03-26 12:03:11 +01004362 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_CONST },
Willy Tarreau70fe9442018-11-22 16:07:39 +01004363
4364 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4365 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4366 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4367 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4368 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004369
Willy Tarreau0209c972021-03-26 12:03:11 +01004370 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_CONST },
4371 { "bool", smp_fetch_const_bool, ARG1(1,STR), smp_check_const_bool, SMP_T_BOOL, SMP_USE_CONST },
4372 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_CONST },
4373 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_CONST },
4374 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_CONST },
4375 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_CONST },
4376 { "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 +02004377
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004378 { /* END */ },
4379}};
4380
Willy Tarreau0108d902018-11-25 19:14:37 +01004381INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
4382
Emeric Brun107ca302010-01-04 16:16:05 +01004383/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02004384static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Moemen MHEDHBI848216f2021-04-02 01:05:07 +02004385 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
4386 { "b64dec", sample_conv_base642bin, 0, NULL, SMP_T_STR, SMP_T_BIN },
4387 { "base64", sample_conv_bin2base64, 0, NULL, SMP_T_BIN, SMP_T_STR },
4388 { "ub64enc", sample_conv_bin2base64url,0, NULL, SMP_T_BIN, SMP_T_STR },
4389 { "ub64dec", sample_conv_base64url2bin,0, NULL, SMP_T_STR, SMP_T_BIN },
4390 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
4391 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
4392 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Marcin Deranek40ca09c2021-07-13 14:05:24 +02004393 { "be2dec", sample_conv_be2dec, ARG3(1,STR,SINT,SINT), sample_conv_be2dec_check, SMP_T_BIN, SMP_T_STR },
Marcin Deranekda0264a2021-07-13 14:08:56 +02004394 { "be2hex", sample_conv_be2hex, ARG3(1,STR,SINT,SINT), sample_conv_be2hex_check, SMP_T_BIN, SMP_T_STR },
Moemen MHEDHBI848216f2021-04-02 01:05:07 +02004395 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
4396 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
4397 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
4398 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4399 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4400 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4401 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4402 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4403 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4404 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4405 { "xxh3", sample_conv_xxh3, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4406 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4407 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4408 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
4409 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
4410 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4411 { "word", sample_conv_word, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4412 { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
4413 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02004414#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01004415 { "sha2", sample_conv_sha2, ARG1(0, SINT), smp_check_sha2, SMP_T_BIN, SMP_T_BIN },
Ilya Shipitsin2c481d02021-03-26 23:35:31 +05004416#ifdef EVP_CIPH_GCM_MODE
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02004417 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
4418#endif
Patrick Gansterer8e366512020-04-22 16:47:57 +02004419 { "digest", sample_conv_crypto_digest, ARG1(1,STR), check_crypto_digest, SMP_T_BIN, SMP_T_BIN },
4420 { "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 +02004421#endif
Willy Tarreau280f42b2018-02-19 15:34:12 +01004422 { "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 +02004423 { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT },
Willy Tarreau2a8a2f02021-05-09 11:59:49 +02004424#if defined(HAVE_CRYPTO_memcmp)
Tim Duesterhusf38175c2020-06-09 11:48:42 +02004425 { "secure_memcmp", sample_conv_secure_memcmp, ARG1(1,STR), smp_check_secure_memcmp, SMP_T_BIN, SMP_T_BOOL },
4426#endif
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01004427
4428 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01004429 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
4430 { "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 +01004431
Baptiste Assmanne138dda2020-10-22 15:39:03 +02004432 /* FIX converters */
4433 { "fix_is_valid", sample_conv_fix_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4434 { "fix_tag_value", sample_conv_fix_tag_value, ARG1(1,STR), sample_conv_fix_value_check, SMP_T_BIN, SMP_T_BIN },
4435
Baptiste Assmanne279ca62020-10-27 18:10:06 +01004436 /* MQTT converters */
4437 { "mqtt_is_valid", sample_conv_mqtt_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4438 { "mqtt_field_value", sample_conv_mqtt_field_value, ARG2(2,STR,STR), sample_conv_mqtt_field_value_check, SMP_T_BIN, SMP_T_STR },
4439
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02004440 { "iif", sample_conv_iif, ARG2(2, STR, STR), NULL, SMP_T_BOOL, SMP_T_STR },
4441
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02004442 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4443 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4444 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4445 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
4446 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4447 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4448 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4449 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4450 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4451 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4452 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4453 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4454 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4455 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01004456
Christopher Fauletea159d62020-04-01 16:21:44 +02004457 { "htonl", sample_conv_htonl, 0, NULL, SMP_T_SINT, SMP_T_BIN },
4458 { "cut_crlf", sample_conv_cut_crlf, 0, NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet51fc9d12020-04-01 17:24:41 +02004459 { "ltrim", sample_conv_ltrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet568415a2020-04-01 17:24:47 +02004460 { "rtrim", sample_conv_rtrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Alex51c8ad42021-04-15 16:45:15 +02004461 { "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 +02004462 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01004463}};
4464
Willy Tarreau0108d902018-11-25 19:14:37 +01004465INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);