blob: 5b52dfd2f9a2edf2b4196086ede6a5127cfb8b8f [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
Willy Tarreaub2551052020-06-09 09:07:15 +020019#include <import/sha1.h>
20#include <import/xxhash.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>
Willy Tarreauf268ee82020-06-04 17:05:57 +020029#include <haproxy/global.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020030#include <haproxy/hash.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020031#include <haproxy/http.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020032#include <haproxy/net_helper.h>
Willy Tarreaub23e5952020-06-04 16:06:59 +020033#include <haproxy/protobuf.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020034#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020035#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020036#include <haproxy/sample.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020037#include <haproxy/sink.h>
38#include <haproxy/stick_table.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020039#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020040#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020041#include <haproxy/vars.h>
Thierry FOURNIER01e09742016-12-26 11:46:11 +010042
Willy Tarreau1cf8f082014-02-07 12:14:54 +010043/* sample type names */
44const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020045 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010046 [SMP_T_BOOL] = "bool",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010047 [SMP_T_SINT] = "sint",
48 [SMP_T_ADDR] = "addr",
49 [SMP_T_IPV4] = "ipv4",
50 [SMP_T_IPV6] = "ipv6",
51 [SMP_T_STR] = "str",
52 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020053 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010054};
55
Willy Tarreau12785782012-04-27 21:37:17 +020056/* static sample used in sample_process() when <p> is NULL */
Emeric Brune5c918b2017-06-14 14:15:36 +020057static THREAD_LOCAL struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010058
Willy Tarreau12785782012-04-27 21:37:17 +020059/* list head of all known sample fetch keywords */
60static struct sample_fetch_kw_list sample_fetches = {
61 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010062};
63
Willy Tarreau12785782012-04-27 21:37:17 +020064/* list head of all known sample format conversion keywords */
65static struct sample_conv_kw_list sample_convs = {
66 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010067};
68
Willy Tarreau80aca902013-01-07 15:42:20 +010069const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
70 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
71 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
72 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
73 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
74 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
75 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +010076 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +010077
78 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
79 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
80 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
81 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
82 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
83 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +010084 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +010085
86 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
87 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
88 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
89 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
90 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
91 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +010092 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +010093
94 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
95 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
96 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
97 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
98 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
99 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100100 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100101
102 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
103 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
104 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
105 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
106 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
107 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100108 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100109
110 [SMP_SRC_TRACK] = (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 |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100116 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100117
118 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
119 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
120 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
121 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
122 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
123 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100124 SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100125
126 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
127 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
128 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
129 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
130 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
131 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100132 SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100133
134 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
135 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
136 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
137 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
138 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
139 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100140 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100141
142 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
143 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
144 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
145 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
146 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
147 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100148 SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100149
150 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
151 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
152 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
153 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
154 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
155 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100156 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100157
158 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
159 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
160 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
161 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
162 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
163 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100164 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100165
166 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
167 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
168 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
169 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
170 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
171 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100172 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100173
174 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
175 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
176 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
177 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
178 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
179 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100180 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100181
182 [SMP_SRC_L6RES] = (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___________ | 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 |
Christopher Faulete596d182020-05-05 17:46:34 +0200188 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100189
190 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
191 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
192 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
193 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
194 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
195 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Christopher Faulete596d182020-05-05 17:46:34 +0200196 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100197
198 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
199 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
200 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
201 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
202 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
203 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Christopher Faulete596d182020-05-05 17:46:34 +0200204 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100205
206 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
207 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
208 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
209 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
210 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
211 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Christopher Faulete596d182020-05-05 17:46:34 +0200212 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100213
214 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
215 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
216 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
217 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
218 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
219 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100220 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100221
222 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
223 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
224 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
225 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
226 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
227 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100228 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100229
230 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
231 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
232 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
233 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
234 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
235 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100236 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100237
238 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
239 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
240 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
241 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
242 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
243 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100244 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100245};
246
247static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
248 [SMP_SRC_INTRN] = "internal state",
249 [SMP_SRC_LISTN] = "listener",
250 [SMP_SRC_FTEND] = "frontend",
251 [SMP_SRC_L4CLI] = "client address",
252 [SMP_SRC_L5CLI] = "client-side connection",
253 [SMP_SRC_TRACK] = "track counters",
254 [SMP_SRC_L6REQ] = "request buffer",
255 [SMP_SRC_HRQHV] = "HTTP request headers",
256 [SMP_SRC_HRQHP] = "HTTP request",
257 [SMP_SRC_HRQBO] = "HTTP request body",
258 [SMP_SRC_BKEND] = "backend",
259 [SMP_SRC_SERVR] = "server",
260 [SMP_SRC_L4SRV] = "server address",
261 [SMP_SRC_L5SRV] = "server-side connection",
262 [SMP_SRC_L6RES] = "response buffer",
263 [SMP_SRC_HRSHV] = "HTTP response headers",
264 [SMP_SRC_HRSHP] = "HTTP response",
265 [SMP_SRC_HRSBO] = "HTTP response body",
266 [SMP_SRC_RQFIN] = "request buffer statistics",
267 [SMP_SRC_RSFIN] = "response buffer statistics",
268 [SMP_SRC_TXFIN] = "transaction statistics",
269 [SMP_SRC_SSFIN] = "session statistics",
270};
271
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100272static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
273 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
274 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
275 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
276 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
277 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
278 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
279 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
280 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
281 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
282 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
283 [SMP_CKP_BE_SRV_CON] = "server source selection",
284 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
285 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
286 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
287 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
288 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
289 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
290 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
291 [SMP_CKP_FE_LOG_END] = "logs",
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100292 [SMP_CKP_BE_CHK_RUL] = "backend tcp-check rule",
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100293};
294
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100295/* This function returns the type of the data returned by the sample_expr.
296 * It assumes that the <expr> and all of its converters are properly
297 * initialized.
298 */
299inline
300int smp_expr_output_type(struct sample_expr *expr)
301{
302 struct sample_conv_expr *smp_expr;
303
304 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
305 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
306 return smp_expr->conv->out_type;
307 }
308 return expr->fetch->out_type;
309}
310
311
Willy Tarreau80aca902013-01-07 15:42:20 +0100312/* fill the trash with a comma-delimited list of source names for the <use> bit
313 * field which must be composed of a non-null set of SMP_USE_* flags. The return
314 * value is the pointer to the string in the trash buffer.
315 */
316const char *sample_src_names(unsigned int use)
317{
318 int bit;
319
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200320 trash.data = 0;
321 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100322 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
323 if (!(use & ~((1 << bit) - 1)))
324 break; /* no more bits */
325
326 if (!(use & (1 << bit)))
327 continue; /* bit not set */
328
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200329 trash.data += snprintf(trash.area + trash.data,
330 trash.size - trash.data, "%s%s",
331 (use & ((1 << bit) - 1)) ? "," : "",
332 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100333 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200334 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100335}
336
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100337/* return a pointer to the correct sample checkpoint name, or "unknown" when
338 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
339 * if set.
340 */
341const char *sample_ckp_names(unsigned int use)
342{
343 int bit;
344
345 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
346 if (use & (1 << bit))
347 return fetch_ckp_names[bit];
348 return "unknown sample check place, please report this bug";
349}
350
Emeric Brun107ca302010-01-04 16:16:05 +0100351/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100352 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
353 * for next parsing sessions. The fetch keywords capabilities are also computed
354 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100355 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100356void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100357{
Willy Tarreau80aca902013-01-07 15:42:20 +0100358 struct sample_fetch *sf;
359 int bit;
360
361 for (sf = kwl->kw; sf->kw != NULL; sf++) {
362 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
363 if (sf->use & (1 << bit))
364 sf->val |= fetch_cap[bit];
365 }
366 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100367}
368
369/*
Willy Tarreau12785782012-04-27 21:37:17 +0200370 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100371 * parsing sessions.
372 */
Willy Tarreau12785782012-04-27 21:37:17 +0200373void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100374{
Willy Tarreau12785782012-04-27 21:37:17 +0200375 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100376}
377
378/*
Willy Tarreau12785782012-04-27 21:37:17 +0200379 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100380 * string of <len> in buffer <kw>.
381 *
382 */
Willy Tarreau12785782012-04-27 21:37:17 +0200383struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100384{
385 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200386 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100387
Willy Tarreau12785782012-04-27 21:37:17 +0200388 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100389 for (index = 0; kwl->kw[index].kw != NULL; index++) {
390 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
391 kwl->kw[index].kw[len] == '\0')
392 return &kwl->kw[index];
393 }
394 }
395 return NULL;
396}
397
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100398/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100399 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100400 * <idx> is the index of the next sample fetch entry. It is used as private
401 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100402 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100403 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100404 * the list is reached.
405 */
406struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
407{
408 struct sample_fetch_kw_list *kwl;
409 struct sample_fetch *base;
410
411 if (!current) {
412 /* Get first kwl entry. */
413 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
414 (*idx) = 0;
415 } else {
416 /* Get kwl corresponding to the curret entry. */
417 base = current + 1 - (*idx);
418 kwl = container_of(base, struct sample_fetch_kw_list, kw);
419 }
420
421 while (1) {
422
423 /* Check if kwl is the last entry. */
424 if (&kwl->list == &sample_fetches.list)
425 return NULL;
426
427 /* idx contain the next keyword. If it is available, return it. */
428 if (kwl->kw[*idx].kw) {
429 (*idx)++;
430 return &kwl->kw[(*idx)-1];
431 }
432
433 /* get next entry in the main list, and return NULL if the end is reached. */
434 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
435
436 /* Set index to 0, ans do one other loop. */
437 (*idx) = 0;
438 }
439}
440
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100441/* This function browses the list of available converters. <current> is
442 * the last used converter. If it is the first call, it must set to NULL.
443 * <idx> is the index of the next converter entry. It is used as private
444 * value. It is useless to initiate it.
445 *
446 * It returns always the next sample_conv entry, and NULL when the end of
447 * the list is reached.
448 */
449struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
450{
451 struct sample_conv_kw_list *kwl;
452 struct sample_conv *base;
453
454 if (!current) {
455 /* Get first kwl entry. */
456 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
457 (*idx) = 0;
458 } else {
459 /* Get kwl corresponding to the curret entry. */
460 base = current + 1 - (*idx);
461 kwl = container_of(base, struct sample_conv_kw_list, kw);
462 }
463
464 while (1) {
465 /* Check if kwl is the last entry. */
466 if (&kwl->list == &sample_convs.list)
467 return NULL;
468
469 /* idx contain the next keyword. If it is available, return it. */
470 if (kwl->kw[*idx].kw) {
471 (*idx)++;
472 return &kwl->kw[(*idx)-1];
473 }
474
475 /* get next entry in the main list, and return NULL if the end is reached. */
476 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
477
478 /* Set index to 0, ans do one other loop. */
479 (*idx) = 0;
480 }
481}
482
Emeric Brun107ca302010-01-04 16:16:05 +0100483/*
Willy Tarreau12785782012-04-27 21:37:17 +0200484 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100485 * string of <len> in buffer <kw>.
486 *
487 */
Willy Tarreau12785782012-04-27 21:37:17 +0200488struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100489{
490 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200491 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100492
Willy Tarreau12785782012-04-27 21:37:17 +0200493 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100494 for (index = 0; kwl->kw[index].kw != NULL; index++) {
495 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
496 kwl->kw[index].kw[len] == '\0')
497 return &kwl->kw[index];
498 }
499 }
500 return NULL;
501}
502
Emeric Brun107ca302010-01-04 16:16:05 +0100503/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200504/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100505/******************************************************************/
506
Willy Tarreau342acb42012-04-23 22:03:39 +0200507static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100508{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200509 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200510 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100511 return 1;
512}
513
Willy Tarreau342acb42012-04-23 22:03:39 +0200514static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100515{
Willy Tarreau83061a82018-07-13 11:56:34 +0200516 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100517
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200518 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100519 return 0;
520
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200521 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200522 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200523 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100524 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100525
526 return 1;
527}
528
Willy Tarreau342acb42012-04-23 22:03:39 +0200529static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100530{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200531 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200532 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100533 return 1;
534}
535
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200536static int c_ipv62ip(struct sample *smp)
537{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200538 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200539 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100540 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200541 return 1;
542}
543
Willy Tarreau342acb42012-04-23 22:03:39 +0200544static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100545{
Willy Tarreau83061a82018-07-13 11:56:34 +0200546 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100547
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200548 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100549 return 0;
550
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200551 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200552 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200553 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100554 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100555 return 1;
556}
557
558/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200559static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100560{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200561 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100562}
563*/
564
Willy Tarreau342acb42012-04-23 22:03:39 +0200565static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100566{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200567 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200568 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100569 return 1;
570}
571
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200572static int c_int2ipv6(struct sample *smp)
573{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200574 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
575 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200576 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200577 return 1;
578}
579
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100580static int c_str2addr(struct sample *smp)
581{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200582 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
583 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100584 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200585 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100586 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100587 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100588 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200589 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100590 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100591 return 1;
592}
593
Willy Tarreau342acb42012-04-23 22:03:39 +0200594static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100595{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200596 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100597 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200598 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100599 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100600 return 1;
601}
602
Willy Tarreau342acb42012-04-23 22:03:39 +0200603static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100604{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200605 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100606 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200607 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100608 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100609 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100610}
611
Emeric Brun4b9e8022014-11-03 18:17:10 +0100612/*
613 * The NULL char always enforces the end of string if it is met.
614 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100615 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200616static int c_bin2str(struct sample *smp)
617{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100618 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200619
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200620 for (i = 0; i < smp->data.u.str.data; i++) {
621 if (!smp->data.u.str.area[i]) {
622 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100623 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100624 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200625 }
Christopher Faulet472ad512020-04-30 09:57:40 +0200626 smp->data.type = SMP_T_STR;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200627 return 1;
628}
629
Willy Tarreau342acb42012-04-23 22:03:39 +0200630static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100631{
Willy Tarreau83061a82018-07-13 11:56:34 +0200632 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100633 char *pos;
634
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200635 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100636 if (!pos)
637 return 0;
638
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200639 trash->size = trash->size - (pos - trash->area);
640 trash->area = pos;
641 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200642 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200643 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100644 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100645 return 1;
646}
647
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800648/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200649 * For strings and binary blocks, it also provides a known allocated size with
650 * a length that is capped to the size, and ensures a trailing zero is always
651 * appended for strings. This is necessary for some operations which may
652 * require to extend the length. It returns 0 if it fails, 1 on success.
653 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100654int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200655{
Willy Tarreau83061a82018-07-13 11:56:34 +0200656 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200657
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200658 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100659 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100660 case SMP_T_SINT:
661 case SMP_T_ADDR:
662 case SMP_T_IPV4:
663 case SMP_T_IPV6:
664 /* These type are not const. */
665 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200666
Christopher Fauletec100512017-07-24 15:38:41 +0200667 case SMP_T_METH:
668 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
669 break;
670 /* Fall through */
671
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100672 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200673 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100674 trash->data = smp->data.type == SMP_T_STR ?
675 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200676 if (trash->data > trash->size - 1)
677 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200678
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100679 memcpy(trash->area, smp->data.type == SMP_T_STR ?
680 smp->data.u.str.area : smp->data.u.meth.str.area,
681 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200682 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200683 smp->data.u.str = *trash;
684 break;
685
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100686 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100687 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200688 trash->data = smp->data.u.str.data;
689 if (trash->data > trash->size)
690 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200691
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200692 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200693 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100694 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200695
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100696 default:
697 /* Other cases are unexpected. */
698 return 0;
699 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100700
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100701 /* remove const flag */
702 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200703 return 1;
704}
705
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100706int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100707{
708 return 1;
709}
710
Willy Tarreau342acb42012-04-23 22:03:39 +0200711static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100712{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200713 const char *str;
714 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100715
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200716 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100717 return 0;
718
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200719 str = smp->data.u.str.area;
720 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100721
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200722 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200723 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100724 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100725 return 1;
726}
727
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100728static int c_str2meth(struct sample *smp)
729{
730 enum http_meth_t meth;
731 int len;
732
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200733 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100734 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200735 len = smp->data.u.str.data;
736 smp->data.u.meth.str.area = smp->data.u.str.area;
737 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100738 }
739 else
740 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200741 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200742 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100743 return 1;
744}
745
746static int c_meth2str(struct sample *smp)
747{
748 int len;
749 enum http_meth_t meth;
750
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200751 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100752 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200753 len = smp->data.u.meth.str.data;
754 smp->data.u.str.area = smp->data.u.meth.str.area;
755 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200756 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100757 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200758 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100759 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200760 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200761 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200762 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100763 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200764 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100765 }
766 else {
767 /* Unknown method */
768 return 0;
769 }
770 return 1;
771}
772
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200773static int c_addr2bin(struct sample *smp)
774{
Willy Tarreau83061a82018-07-13 11:56:34 +0200775 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200776
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200777 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200778 chk->data = 4;
779 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200780 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200781 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200782 chk->data = 16;
783 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200784 }
785 else
786 return 0;
787
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200788 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200789 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200790 return 1;
791}
792
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200793static int c_int2bin(struct sample *smp)
794{
Willy Tarreau83061a82018-07-13 11:56:34 +0200795 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200796
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200797 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
798 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200799
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200800 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200801 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200802 return 1;
803}
804
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200805
Emeric Brun107ca302010-01-04 16:16:05 +0100806/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200807/* Sample casts matrix: */
808/* sample_casts[from type][to type] */
809/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100810/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100811
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100812sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200813/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
814/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
815/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200816/* 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 +0200817/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
818/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200819/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200820/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
821/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
822/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200823};
Emeric Brun107ca302010-01-04 16:16:05 +0100824
Emeric Brun107ca302010-01-04 16:16:05 +0100825/*
Willy Tarreau12785782012-04-27 21:37:17 +0200826 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100827 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200828 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200829 * The caller must have set al->ctx.
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100830 * If <endptr> is non-nul, it will be set to the first unparsed character
831 * (which may be the final '\0') on success. If it is nul, the expression
832 * must be properly terminated by a '\0' otherwise an error is reported.
Emeric Brun107ca302010-01-04 16:16:05 +0100833 */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100834struct 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 +0100835{
Willy Tarreau833cc792013-07-24 15:34:19 +0200836 const char *begw; /* beginning of word */
837 const char *endw; /* end of word */
838 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200839 struct sample_expr *expr;
840 struct sample_fetch *fetch;
841 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100842 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200843 char *fkw = NULL;
844 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100845 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100846
Willy Tarreau833cc792013-07-24 15:34:19 +0200847 begw = str[*idx];
Willy Tarreaued2c6622020-02-14 18:27:10 +0100848 for (endw = begw; is_idchar(*endw); endw++)
849 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200850
851 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100852 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100853 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200854 }
Emeric Brun107ca302010-01-04 16:16:05 +0100855
Willy Tarreau833cc792013-07-24 15:34:19 +0200856 /* keep a copy of the current fetch keyword for error reporting */
857 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100858
Willy Tarreau833cc792013-07-24 15:34:19 +0200859 fetch = find_sample_fetch(begw, endw - begw);
860 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100861 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100862 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200863 }
Emeric Brun107ca302010-01-04 16:16:05 +0100864
Willy Tarreau833cc792013-07-24 15:34:19 +0200865 /* At this point, we have :
866 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100867 * - endw : end of the keyword, first character not part of keyword
Willy Tarreau833cc792013-07-24 15:34:19 +0200868 */
869
870 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100871 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100872 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200873 }
Emeric Brun107ca302010-01-04 16:16:05 +0100874 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200875
Vincent Bernat02779b62016-04-03 13:48:43 +0200876 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200877 if (!expr)
878 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100879
880 LIST_INIT(&(expr->conv_exprs));
881 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200882 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100883
Willy Tarreau689a1df2013-12-13 00:40:11 +0100884 /* Note that we call the argument parser even with an empty string,
885 * this allows it to automatically create entries for mandatory
886 * implicit arguments (eg: local proxy name).
887 */
888 al->kw = expr->fetch->kw;
889 al->conv = NULL;
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100890 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 +0100891 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
892 goto out_error;
893 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200894
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100895 /* now endt is our first char not part of the arg list, typically the
896 * comma after the sample fetch name or after the closing parenthesis,
897 * or the NUL char.
898 */
899
Willy Tarreau689a1df2013-12-13 00:40:11 +0100900 if (!expr->arg_p) {
901 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200902 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100903 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
904 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200905 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100906 }
907
Willy Tarreau833cc792013-07-24 15:34:19 +0200908 /* Now process the converters if any. We have two supported syntaxes
909 * for the converters, which can be combined :
910 * - comma-delimited list of converters just after the keyword and args ;
911 * - one converter per keyword
912 * The combination allows to have each keyword being a comma-delimited
913 * series of converters.
914 *
915 * We want to process the former first, then the latter. For this we start
916 * from the beginning of the supposed place in the exiting conv chain, which
917 * starts at the last comma (endt).
918 */
919
920 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200921 struct sample_conv_expr *conv_expr;
Willy Tarreau46dfd782019-12-17 10:25:29 +0100922 int err_arg;
923 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +0100924
Willy Tarreau833cc792013-07-24 15:34:19 +0200925 if (*endt && *endt != ',') {
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100926 if (endptr) {
927 /* end found, let's stop here */
928 break;
929 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200930 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100931 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200932 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100933 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100934 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200935 }
Emeric Brun107ca302010-01-04 16:16:05 +0100936
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100937 /* FIXME: how long should we support such idiocies ? Maybe we
938 * should already warn ?
939 */
Willy Tarreau833cc792013-07-24 15:34:19 +0200940 while (*endt == ',') /* then trailing commas */
941 endt++;
942
Willy Tarreau97108e02016-11-25 07:33:24 +0100943 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200944
945 if (!*begw) {
946 /* none ? skip to next string */
947 (*idx)++;
948 begw = str[*idx];
949 if (!begw || !*begw)
950 break;
951 }
952
Willy Tarreaued2c6622020-02-14 18:27:10 +0100953 for (endw = begw; is_idchar(*endw); endw++)
954 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200955
956 free(ckw);
957 ckw = my_strndup(begw, endw - begw);
958
959 conv = find_sample_conv(begw, endw - begw);
960 if (!conv) {
961 /* we found an isolated keyword that we don't know, it's not ours */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100962 if (begw == str[*idx]) {
963 endt = begw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200964 break;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100965 }
Willy Tarreau97108e02016-11-25 07:33:24 +0100966 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200967 goto out_error;
968 }
Emeric Brun107ca302010-01-04 16:16:05 +0100969
Willy Tarreau833cc792013-07-24 15:34:19 +0200970 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100971 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100972 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200973 }
Emeric Brun107ca302010-01-04 16:16:05 +0100974
975 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200976 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100977 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100978 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200979 }
Emeric Brun107ca302010-01-04 16:16:05 +0100980
981 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +0200982 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200983 if (!conv_expr)
984 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100985
986 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
987 conv_expr->conv = conv;
988
Willy Tarreau46dfd782019-12-17 10:25:29 +0100989 al->kw = expr->fetch->kw;
990 al->conv = conv_expr->conv->kw;
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100991 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 +0100992 if (argcnt < 0) {
993 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
994 goto out_error;
995 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100996
Willy Tarreau46dfd782019-12-17 10:25:29 +0100997 if (argcnt && !conv->arg_mask) {
998 memprintf(err_msg, "converter '%s' does not support any args", ckw);
999 goto out_error;
1000 }
Willy Tarreau21d68a62012-04-20 15:52:36 +02001001
Willy Tarreau46dfd782019-12-17 10:25:29 +01001002 if (!conv_expr->arg_p)
1003 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001004
Willy Tarreau46dfd782019-12-17 10:25:29 +01001005 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1006 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001007 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001008 }
1009 }
Emeric Brun485479d2010-09-23 18:02:19 +02001010
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001011 if (endptr) {
1012 /* end found, let's stop here */
1013 *endptr = (char *)endt;
1014 }
1015
Willy Tarreau833cc792013-07-24 15:34:19 +02001016 out:
1017 free(fkw);
1018 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001019 return expr;
1020
1021out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001022 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001023 expr = NULL;
1024 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001025}
1026
1027/*
Willy Tarreau12785782012-04-27 21:37:17 +02001028 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001029 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001030 * Returns a pointer on a typed sample structure containing the result or NULL if
1031 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001032 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001033 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001034 *
1035 * Note: the fetch functions are required to properly set the return type. The
1036 * conversion functions must do so too. However the cast functions do not need
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001037 * to since they're made to cast multiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001038 *
1039 * The caller may indicate in <opt> if it considers the result final or not.
1040 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1041 * if the result is stable or not, according to the following table :
1042 *
1043 * return MAY_CHANGE FINAL Meaning for the sample
1044 * NULL 0 * Not present and will never be (eg: header)
1045 * NULL 1 0 Not present yet, could change (eg: POST param)
1046 * NULL 1 1 Not present yet, will not change anymore
1047 * smp 0 * Present and will not change (eg: header)
1048 * smp 1 0 Present, may change (eg: request length)
1049 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001050 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001051struct sample *sample_process(struct proxy *px, struct session *sess,
1052 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001053 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001054{
Willy Tarreau12785782012-04-27 21:37:17 +02001055 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001056
Willy Tarreau18387e22013-07-25 12:02:38 +02001057 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001058 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001059 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001060 }
Emeric Brun107ca302010-01-04 16:16:05 +01001061
Willy Tarreau1777ea62016-03-10 16:15:46 +01001062 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001063 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001064 return NULL;
1065
Emeric Brun107ca302010-01-04 16:16:05 +01001066 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001067 /* we want to ensure that p->type can be casted into
1068 * conv_expr->conv->in_type. We have 3 possibilities :
1069 * - NULL => not castable.
1070 * - c_none => nothing to do (let's optimize it)
1071 * - other => apply cast and prepare to fail
1072 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001073 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001074 return NULL;
1075
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001076 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1077 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001078 return NULL;
1079
Willy Tarreau12e50112012-04-25 17:21:49 +02001080 /* OK cast succeeded */
1081
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001082 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001083 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001084 }
1085 return p;
1086}
1087
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001088/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001089 * Resolve all remaining arguments in proxy <p>. Returns the number of
1090 * errors or 0 if everything is fine.
1091 */
1092int smp_resolve_args(struct proxy *p)
1093{
1094 struct arg_list *cur, *bak;
1095 const char *ctx, *where;
1096 const char *conv_ctx, *conv_pre, *conv_pos;
1097 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001098 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001099 struct arg *arg;
1100 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001101 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001102
1103 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1104 struct proxy *px;
1105 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001106 struct stktable *t;
1107 char *pname, *sname, *stktname;
Willy Tarreau46947782015-01-19 19:00:58 +01001108 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001109
1110 arg = cur->arg;
1111
1112 /* prepare output messages */
1113 conv_pre = conv_pos = conv_ctx = "";
1114 if (cur->conv) {
1115 conv_ctx = cur->conv;
1116 conv_pre = "conversion keyword '";
1117 conv_pos = "' for ";
1118 }
1119
1120 where = "in";
1121 ctx = "sample fetch keyword";
1122 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001123 case ARGC_STK: where = "in stick rule in"; break;
1124 case ARGC_TRK: where = "in tracking rule in"; break;
1125 case ARGC_LOG: where = "in log-format string in"; break;
1126 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1127 case ARGC_HRQ: where = "in http-request header format string in"; break;
1128 case ARGC_HRS: where = "in http-response header format string in"; break;
1129 case ARGC_UIF: where = "in unique-id-format string in"; break;
1130 case ARGC_RDR: where = "in redirect format string in"; break;
1131 case ARGC_CAP: where = "in capture rule in"; break;
1132 case ARGC_ACL: ctx = "ACL keyword"; break;
1133 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001134 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001135 case ARGC_HERR: where = "in http-error directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001136 }
1137
1138 /* set a few default settings */
1139 px = p;
1140 pname = p->id;
1141
1142 switch (arg->type) {
1143 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001144 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001145 ha_alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1146 cur->file, cur->line,
1147 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001148 cfgerr++;
1149 continue;
1150 }
1151
1152 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001153 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001154
1155 if (sname) {
1156 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001157 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001158
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001159 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001160 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001161 ha_alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1162 cur->file, cur->line, pname,
1163 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001164 cfgerr++;
1165 break;
1166 }
1167 }
1168 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001169 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001170
1171 srv = findserver(px, sname);
1172 if (!srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001173 ha_alert("parsing [%s:%d] : unable to find server '%s' in proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1174 cur->file, cur->line, sname, pname,
1175 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001176 cfgerr++;
1177 break;
1178 }
1179
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001180 free(arg->data.str.area);
1181 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001182 arg->unresolved = 0;
1183 arg->data.srv = srv;
1184 break;
1185
1186 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001187 if (arg->data.str.data) {
1188 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001189 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001190 }
1191
1192 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001193 ha_alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1194 cur->file, cur->line, pname,
1195 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001196 cfgerr++;
1197 break;
1198 }
1199
1200 if (!(px->cap & PR_CAP_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001201 ha_alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1202 cur->file, cur->line, pname,
1203 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001204 cfgerr++;
1205 break;
1206 }
1207
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001208 free(arg->data.str.area);
1209 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001210 arg->unresolved = 0;
1211 arg->data.prx = px;
1212 break;
1213
1214 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001215 if (arg->data.str.data) {
1216 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001217 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001218 }
1219
1220 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001221 ha_alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1222 cur->file, cur->line, pname,
1223 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001224 cfgerr++;
1225 break;
1226 }
1227
1228 if (!(px->cap & PR_CAP_BE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001229 ha_alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1230 cur->file, cur->line, pname,
1231 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001232 cfgerr++;
1233 break;
1234 }
1235
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001236 free(arg->data.str.area);
1237 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001238 arg->unresolved = 0;
1239 arg->data.prx = px;
1240 break;
1241
1242 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001243 if (arg->data.str.data)
1244 stktname = arg->data.str.area;
1245 else
1246 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001247
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001248 t = stktable_find_by_name(stktname);
1249 if (!t) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001250 ha_alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001251 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001252 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001253 cfgerr++;
1254 break;
1255 }
1256
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001257 if (!t->size) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001258 ha_alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001259 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001260 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001261 cfgerr++;
1262 break;
1263 }
1264
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001265 if (t->proxy && (p->bind_proc & ~t->proxy->bind_proc)) {
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001266 ha_alert("parsing [%s:%d] : stick-table '%s' not present on all processes covered by proxy '%s'.\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001267 cur->file, cur->line, t->proxy->id, p->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01001268 cfgerr++;
1269 break;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001270 }
1271
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001272 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001273 p->next_stkt_ref = t->proxies_list;
1274 t->proxies_list = p;
1275 }
1276
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001277 free(arg->data.str.area);
1278 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001279 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001280 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001281 break;
1282
1283 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001284 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001285 ha_alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1286 cur->file, cur->line,
1287 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001288 cfgerr++;
1289 break;
1290 }
1291
1292 if (p->uri_auth && p->uri_auth->userlist &&
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001293 !strcmp(p->uri_auth->userlist->name, arg->data.str.area))
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001294 ul = p->uri_auth->userlist;
1295 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001296 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001297
1298 if (!ul) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001299 ha_alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001300 cur->file, cur->line,
1301 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001302 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001303 cfgerr++;
1304 break;
1305 }
1306
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001307 free(arg->data.str.area);
1308 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001309 arg->unresolved = 0;
1310 arg->data.usr = ul;
1311 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001312
1313 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001314 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001315 ha_alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1316 cur->file, cur->line,
1317 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001318 cfgerr++;
1319 continue;
1320 }
1321
Willy Tarreau46947782015-01-19 19:00:58 +01001322 rflags = 0;
1323 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1324 err = NULL;
1325
Dragan Dosen26743032019-04-30 15:54:36 +02001326 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001327 ha_alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1328 cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001329 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001330 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
Willy Tarreau46947782015-01-19 19:00:58 +01001331 cfgerr++;
1332 continue;
1333 }
1334
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001335 free(arg->data.str.area);
1336 arg->data.str.area = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001337 arg->unresolved = 0;
1338 arg->data.reg = reg;
1339 break;
1340
1341
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001342 }
1343
1344 LIST_DEL(&cur->list);
1345 free(cur);
1346 } /* end of args processing */
1347
1348 return cfgerr;
1349}
1350
1351/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001352 * Process a fetch + format conversion as defined by the sample expression
1353 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001354 * not explicitly set to <smp_type>, but shall be compatible with it as
1355 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1356 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001357 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1358 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1359 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1360 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001361 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1362 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1363 * still hope to get it after waiting longer, and is not converted to string.
1364 * The possible output combinations are the following :
1365 *
1366 * return MAY_CHANGE FINAL Meaning for the sample
1367 * NULL * * Not present and will never be (eg: header)
1368 * smp 0 * Final value converted (eg: header)
1369 * smp 1 0 Not present yet, may appear later (eg: header)
1370 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001371 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001372struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001373 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001374 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001375{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001376 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001377
Willy Tarreau6c616e02014-06-25 16:56:41 +02001378 memset(smp, 0, sizeof(*smp));
1379
Willy Tarreau192252e2015-04-04 01:47:55 +02001380 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001381 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1382 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001383 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001384 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001385
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001386 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001387 return NULL;
1388
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001389 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001390 return NULL;
1391
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001392 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001393 return smp;
1394}
1395
Christopher Faulet476e5d02016-10-26 11:34:47 +02001396static void release_sample_arg(struct arg *p)
1397{
1398 struct arg *p_back = p;
1399
1400 if (!p)
1401 return;
1402
1403 while (p->type != ARGT_STOP) {
1404 if (p->type == ARGT_STR || p->unresolved) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001405 free(p->data.str.area);
1406 p->data.str.area = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001407 p->unresolved = 0;
1408 }
1409 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001410 regex_free(p->data.reg);
1411 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001412 }
1413 p++;
1414 }
1415
1416 if (p_back != empty_arg_list)
1417 free(p_back);
1418}
1419
1420void release_sample_expr(struct sample_expr *expr)
1421{
1422 struct sample_conv_expr *conv_expr, *conv_exprb;
1423
1424 if (!expr)
1425 return;
1426
Tim Duesterhus867cd982020-07-04 11:49:39 +02001427 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list) {
1428 LIST_DEL(&conv_expr->list);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001429 release_sample_arg(conv_expr->arg_p);
Tim Duesterhus867cd982020-07-04 11:49:39 +02001430 free(conv_expr);
1431 }
1432
Christopher Faulet476e5d02016-10-26 11:34:47 +02001433 release_sample_arg(expr->arg_p);
1434 free(expr);
1435}
1436
Emeric Brun107ca302010-01-04 16:16:05 +01001437/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001438/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001439/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001440/*****************************************************************/
1441
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001442static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1443{
1444 int i;
1445 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001446 struct buffer *buf;
1447 struct sink *sink;
1448 struct ist line;
1449 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001450
Willy Tarreau0851fd52019-12-17 10:07:25 +01001451 buf = alloc_trash_chunk();
1452 if (!buf)
1453 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001454
Willy Tarreau0851fd52019-12-17 10:07:25 +01001455 sink = (struct sink *)arg_p[1].data.str.area;
1456 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001457
Willy Tarreau0851fd52019-12-17 10:07:25 +01001458 pfx = arg_p[0].data.str.area;
1459 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001460
Willy Tarreau0851fd52019-12-17 10:07:25 +01001461 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1462 if (!sample_casts[smp->data.type][SMP_T_STR])
1463 goto nocast;
1464
1465 /* Copy sample fetch. This puts the sample as const, the
1466 * cast will copy data if a transformation is required.
1467 */
1468 memcpy(&tmp, smp, sizeof(struct sample));
1469 tmp.flags = SMP_F_CONST;
1470
1471 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1472 goto nocast;
1473
1474 /* Display the displayable chars*. */
1475 b_putchr(buf, '<');
1476 for (i = 0; i < tmp.data.u.str.data; i++) {
Willy Tarreau90807112020-02-25 08:16:33 +01001477 if (isprint((unsigned char)tmp.data.u.str.area[i]))
Willy Tarreau0851fd52019-12-17 10:07:25 +01001478 b_putchr(buf, tmp.data.u.str.area[i]);
1479 else
1480 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001481 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001482 b_putchr(buf, '>');
1483
1484 done:
1485 line = ist2(buf->area, buf->data);
Emeric Brunbd163812020-05-06 14:33:46 +02001486 sink_write(sink, &line, 1, 0, 0, NULL, NULL, NULL);
Willy Tarreau0851fd52019-12-17 10:07:25 +01001487 end:
1488 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001489 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001490 nocast:
1491 chunk_appendf(buf, "(undisplayable)");
1492 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001493}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001494
1495// This function checks the "debug" converter's arguments.
1496static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1497 const char *file, int line, char **err)
1498{
1499 const char *name = "buf0";
1500 struct sink *sink = NULL;
1501
1502 if (args[0].type != ARGT_STR) {
1503 /* optional prefix */
1504 args[0].data.str.area = "";
1505 args[0].data.str.data = 0;
1506 }
1507
1508 if (args[1].type == ARGT_STR)
1509 name = args[1].data.str.area;
1510
1511 sink = sink_find(name);
1512 if (!sink) {
1513 memprintf(err, "No such sink '%s'", name);
1514 return 0;
1515 }
1516
1517 args[1].data.str.area = (char *)sink;
1518 args[1].data.str.data = 0; // that's not a string anymore
1519 return 1;
1520}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001521
Holger Just1bfc24b2017-05-06 00:56:53 +02001522static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1523{
Willy Tarreau83061a82018-07-13 11:56:34 +02001524 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001525 int bin_len;
1526
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001527 trash->data = 0;
1528 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1529 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001530 if (bin_len < 0)
1531 return 0;
1532
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001533 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001534 smp->data.u.str = *trash;
1535 smp->data.type = SMP_T_BIN;
1536 smp->flags &= ~SMP_F_CONST;
1537 return 1;
1538}
1539
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001540static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001541{
Willy Tarreau83061a82018-07-13 11:56:34 +02001542 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001543 int b64_len;
1544
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001545 trash->data = 0;
1546 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1547 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001548 if (b64_len < 0)
1549 return 0;
1550
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001551 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001552 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001553 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001554 smp->flags &= ~SMP_F_CONST;
1555 return 1;
1556}
1557
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001558static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1559{
1560 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001561 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001562
1563 memset(&ctx, 0, sizeof(ctx));
1564
1565 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001566 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1567 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001568
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001569 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001570 smp->data.u.str = *trash;
1571 smp->data.type = SMP_T_BIN;
1572 smp->flags &= ~SMP_F_CONST;
1573 return 1;
1574}
1575
Tim Duesterhusd4376302019-06-17 12:41:44 +02001576#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01001577static int smp_check_sha2(struct arg *args, struct sample_conv *conv,
1578 const char *file, int line, char **err)
1579{
1580 if (args[0].type == ARGT_STOP)
1581 return 1;
1582 if (args[0].type != ARGT_SINT) {
1583 memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]);
1584 return 0;
1585 }
1586
1587 switch (args[0].data.sint) {
1588 case 224:
1589 case 256:
1590 case 384:
1591 case 512:
1592 /* this is okay */
1593 return 1;
1594 default:
1595 memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint);
1596 return 0;
1597 }
1598}
1599
Tim Duesterhusd4376302019-06-17 12:41:44 +02001600static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
1601{
1602 struct buffer *trash = get_trash_chunk();
1603 int bits = 256;
1604 if (arg_p && arg_p->data.sint)
1605 bits = arg_p->data.sint;
1606
1607 switch (bits) {
1608 case 224: {
1609 SHA256_CTX ctx;
1610
1611 memset(&ctx, 0, sizeof(ctx));
1612
1613 SHA224_Init(&ctx);
1614 SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1615 SHA224_Final((unsigned char *) trash->area, &ctx);
1616 trash->data = SHA224_DIGEST_LENGTH;
1617 break;
1618 }
1619 case 256: {
1620 SHA256_CTX ctx;
1621
1622 memset(&ctx, 0, sizeof(ctx));
1623
1624 SHA256_Init(&ctx);
1625 SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1626 SHA256_Final((unsigned char *) trash->area, &ctx);
1627 trash->data = SHA256_DIGEST_LENGTH;
1628 break;
1629 }
1630 case 384: {
1631 SHA512_CTX ctx;
1632
1633 memset(&ctx, 0, sizeof(ctx));
1634
1635 SHA384_Init(&ctx);
1636 SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1637 SHA384_Final((unsigned char *) trash->area, &ctx);
1638 trash->data = SHA384_DIGEST_LENGTH;
1639 break;
1640 }
1641 case 512: {
1642 SHA512_CTX ctx;
1643
1644 memset(&ctx, 0, sizeof(ctx));
1645
1646 SHA512_Init(&ctx);
1647 SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1648 SHA512_Final((unsigned char *) trash->area, &ctx);
1649 trash->data = SHA512_DIGEST_LENGTH;
1650 break;
1651 }
1652 default:
1653 return 0;
1654 }
1655
1656 smp->data.u.str = *trash;
1657 smp->data.type = SMP_T_BIN;
1658 smp->flags &= ~SMP_F_CONST;
1659 return 1;
1660}
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001661
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001662static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
1663{
1664 switch (arg->type) {
1665 case ARGT_STR:
1666 smp->data.type = SMP_T_STR;
1667 smp->data.u.str = arg->data.str;
1668 return 1;
1669 case ARGT_VAR:
1670 if (!vars_get_by_desc(&arg->data.var, smp))
1671 return 0;
1672 if (!sample_casts[smp->data.type][SMP_T_STR])
1673 return 0;
1674 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
1675 return 0;
1676 return 1;
1677 default:
1678 return 0;
1679 }
1680}
1681
Patrick Gansterer8e366512020-04-22 16:47:57 +02001682#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001683static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
1684 const char *file, int line, char **err)
1685{
1686 switch(args[0].data.sint) {
1687 case 128:
1688 case 192:
1689 case 256:
1690 break;
1691 default:
1692 memprintf(err, "key size must be 128, 192 or 256 (bits).");
1693 return 0;
1694 }
1695 /* Try to decode a variable. */
1696 vars_check_arg(&args[1], NULL);
1697 vars_check_arg(&args[2], NULL);
1698 vars_check_arg(&args[3], NULL);
1699 return 1;
1700}
1701
1702/* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
1703static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
1704{
1705 struct sample nonce, key, aead_tag;
1706 struct buffer *smp_trash, *smp_trash_alloc;
1707 EVP_CIPHER_CTX *ctx;
1708 int dec_size, ret;
1709
1710 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
1711 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
1712 return 0;
1713
1714 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
1715 if (!sample_conv_var2smp_str(&arg_p[2], &key))
1716 return 0;
1717
1718 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
1719 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
1720 return 0;
1721
1722 smp_trash = get_trash_chunk();
1723 smp_trash_alloc = alloc_trash_chunk();
1724 if (!smp_trash_alloc)
1725 return 0;
1726
1727 ctx = EVP_CIPHER_CTX_new();
1728
1729 if (!ctx)
1730 goto err;
1731
1732 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
1733 if (dec_size < 0)
1734 goto err;
1735 smp_trash->data = dec_size;
1736
1737 /* Set cipher type and mode */
1738 switch(arg_p[0].data.sint) {
1739 case 128:
1740 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
1741 break;
1742 case 192:
1743 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
1744 break;
1745 case 256:
1746 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
1747 break;
1748 }
1749
1750 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL);
1751
1752 /* Initialise IV */
1753 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area))
1754 goto err;
1755
1756 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
1757 if (dec_size < 0)
1758 goto err;
1759 smp_trash->data = dec_size;
1760
1761 /* Initialise key */
1762 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL))
1763 goto err;
1764
1765 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
1766 (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data))
1767 goto err;
1768
1769 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
1770 if (dec_size < 0)
1771 goto err;
1772 smp_trash_alloc->data = dec_size;
1773 dec_size = smp_trash->data;
1774
1775 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area);
1776 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
1777
1778 if (ret <= 0)
1779 goto err;
1780
1781 smp->data.u.str.data = dec_size + smp_trash->data;
1782 smp->data.u.str.area = smp_trash->area;
1783 smp->data.type = SMP_T_BIN;
1784 smp->flags &= ~SMP_F_CONST;
1785 free_trash_chunk(smp_trash_alloc);
1786 return 1;
1787
1788err:
1789 free_trash_chunk(smp_trash_alloc);
1790 return 0;
1791}
1792#endif /* HA_OPENSSL_VERSION_NUMBER */
1793
Patrick Gansterer8e366512020-04-22 16:47:57 +02001794static int check_crypto_digest(struct arg *args, struct sample_conv *conv,
1795 const char *file, int line, char **err)
1796{
1797 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1798
1799 if (evp)
1800 return 1;
1801
1802 memprintf(err, "algorithm must be a valid OpenSSL message digest name.");
1803 return 0;
1804}
1805
1806static int sample_conv_crypto_digest(const struct arg *args, struct sample *smp, void *private)
1807{
1808 struct buffer *trash = get_trash_chunk();
1809 unsigned char *md = (unsigned char*) trash->area;
1810 unsigned int md_len = trash->size;
1811 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
1812 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1813
1814 if (!ctx)
1815 return 0;
1816
1817 if (!EVP_DigestInit_ex(ctx, evp, NULL) ||
1818 !EVP_DigestUpdate(ctx, smp->data.u.str.area, smp->data.u.str.data) ||
1819 !EVP_DigestFinal_ex(ctx, md, &md_len)) {
1820 EVP_MD_CTX_free(ctx);
1821 return 0;
1822 }
1823
1824 EVP_MD_CTX_free(ctx);
1825
1826 trash->data = md_len;
1827 smp->data.u.str = *trash;
1828 smp->data.type = SMP_T_BIN;
1829 smp->flags &= ~SMP_F_CONST;
1830 return 1;
1831}
1832
1833static int check_crypto_hmac(struct arg *args, struct sample_conv *conv,
1834 const char *file, int line, char **err)
1835{
1836 if (!check_crypto_digest(args, conv, file, line, err))
1837 return 0;
1838
1839 vars_check_arg(&args[1], NULL);
1840 return 1;
1841}
1842
1843static int sample_conv_crypto_hmac(const struct arg *args, struct sample *smp, void *private)
1844{
1845 struct sample key;
1846 struct buffer *trash, *key_trash;
1847 unsigned char *md;
1848 unsigned int md_len;
1849 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1850 int dec_size;
1851
1852 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
1853 if (!sample_conv_var2smp_str(&args[1], &key))
1854 return 0;
1855
1856 trash = get_trash_chunk();
1857 key_trash = alloc_trash_chunk();
1858 if (!key_trash)
1859 return 0;
1860
1861 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, key_trash->area, key_trash->size);
1862 if (dec_size < 0)
1863 goto err;
1864
1865 md = (unsigned char*) trash->area;
1866 md_len = trash->size;
1867 if (!HMAC(evp, key_trash->area, dec_size, (const unsigned char*) smp->data.u.str.area, smp->data.u.str.data, md, &md_len))
1868 goto err;
1869
1870 free_trash_chunk(key_trash);
1871
1872 trash->data = md_len;
1873 smp->data.u.str = *trash;
1874 smp->data.type = SMP_T_BIN;
1875 smp->flags &= ~SMP_F_CONST;
1876 return 1;
1877
1878err:
1879 free_trash_chunk(key_trash);
1880 return 0;
1881}
1882
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001883#endif /* USE_OPENSSL */
Tim Duesterhusd4376302019-06-17 12:41:44 +02001884
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001885static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001886{
Willy Tarreau83061a82018-07-13 11:56:34 +02001887 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001888 unsigned char c;
1889 int ptr = 0;
1890
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001891 trash->data = 0;
1892 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
1893 c = smp->data.u.str.area[ptr++];
1894 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1895 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001896 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001897 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001898 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001899 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001900 return 1;
1901}
1902
Dragan Dosen3f957b22017-10-24 09:27:34 +02001903static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1904{
1905 long long int n = 0;
1906 int i, c;
1907
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001908 for (i = 0; i < smp->data.u.str.data; i++) {
1909 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02001910 return 0;
1911 n = (n << 4) + c;
1912 }
1913
1914 smp->data.u.sint = n;
1915 smp->data.type = SMP_T_SINT;
1916 smp->flags &= ~SMP_F_CONST;
1917 return 1;
1918}
1919
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001920/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001921static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001922{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001923 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
1924 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001925 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001926 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001927 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001928 return 1;
1929}
1930
Willy Tarreau60a2ee72017-12-15 07:13:48 +01001931static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001932{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001933 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001934 smp->data.u.sint = i;
1935 smp->data.type = SMP_T_SINT;
1936 return 1;
1937}
1938
1939
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001940static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001941{
1942 int i;
1943
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001944 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001945 return 0;
1946
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001947 for (i = 0; i < smp->data.u.str.data; i++) {
1948 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
1949 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001950 }
1951 return 1;
1952}
1953
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001954static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001955{
1956 int i;
1957
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001958 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001959 return 0;
1960
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001961 for (i = 0; i < smp->data.u.str.data; i++) {
1962 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
1963 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001964 }
1965 return 1;
1966}
1967
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001968/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
1969static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001970{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001971 /* Attempt to convert to IPv4 to apply the correct mask. */
1972 c_ipv62ip(smp);
1973
1974 if (smp->data.type == SMP_T_IPV4) {
1975 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
1976 smp->data.type = SMP_T_IPV4;
1977 }
1978 else if (smp->data.type == SMP_T_IPV6) {
1979 /* IPv6 cannot be converted without an IPv6 mask. */
1980 if (args[1].type != ARGT_IPV6)
1981 return 0;
1982
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +01001983 write_u64(&smp->data.u.ipv6.s6_addr[0],
1984 read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0]));
1985 write_u64(&smp->data.u.ipv6.s6_addr[8],
1986 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 +01001987 smp->data.type = SMP_T_IPV6;
1988 }
1989
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001990 return 1;
1991}
1992
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001993/* takes an UINT value on input supposed to represent the time since EPOCH,
1994 * adds an optional offset found in args[1] and emits a string representing
1995 * the local time in the format specified in args[1] using strftime().
1996 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001997static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001998{
Willy Tarreau83061a82018-07-13 11:56:34 +02001999 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002000 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002001 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002002 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002003
2004 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002005 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002006 curr_date += args[1].data.sint;
2007
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002008 tm = localtime(&curr_date);
2009 if (!tm)
2010 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002011 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002012 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
2013 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002014 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002015 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002016 return 1;
2017}
2018
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002019/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002020static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002021{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002022 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
2023 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002024 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002025 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002026 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002027 return 1;
2028}
2029
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002030/* takes an UINT value on input supposed to represent the time since EPOCH,
2031 * adds an optional offset found in args[1] and emits a string representing
2032 * the UTC date in the format specified in args[1] using strftime().
2033 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002034static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002035{
Willy Tarreau83061a82018-07-13 11:56:34 +02002036 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002037 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002038 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002039 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002040
2041 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002042 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002043 curr_date += args[1].data.sint;
2044
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002045 tm = gmtime(&curr_date);
2046 if (!tm)
2047 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002048 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002049 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
2050 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002051 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002052 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002053 return 1;
2054}
2055
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002056/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002057static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002058{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002059 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
2060 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002061 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002062 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002063 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002064 return 1;
2065}
2066
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002067/* hashes the binary input into a 32-bit unsigned int using xxh.
2068 * The seed of the hash defaults to 0 but can be changd in argument 1.
2069 */
2070static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
2071{
2072 unsigned int seed;
2073
2074 if (arg_p && arg_p->data.sint)
2075 seed = arg_p->data.sint;
2076 else
2077 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002078 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
2079 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002080 smp->data.type = SMP_T_SINT;
2081 return 1;
2082}
2083
2084/* hashes the binary input into a 64-bit unsigned int using xxh.
2085 * In fact, the function returns a 64 bit unsigned, but the sample
2086 * storage of haproxy only proposes 64-bits signed, so the value is
2087 * cast as signed. This cast doesn't impact the hash repartition.
2088 * The seed of the hash defaults to 0 but can be changd in argument 1.
2089 */
2090static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
2091{
2092 unsigned long long int seed;
2093
2094 if (arg_p && arg_p->data.sint)
2095 seed = (unsigned long long int)arg_p->data.sint;
2096 else
2097 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002098 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
2099 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002100 smp->data.type = SMP_T_SINT;
2101 return 1;
2102}
2103
Willy Tarreau80599772015-01-20 19:35:24 +01002104/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002105static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01002106{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002107 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
2108 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002109 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002110 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002111 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01002112 return 1;
2113}
2114
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002115/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
2116static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
2117{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002118 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
2119 smp->data.u.str.data);
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002120 if (arg_p && arg_p->data.sint)
2121 smp->data.u.sint = full_hash(smp->data.u.sint);
2122 smp->data.type = SMP_T_SINT;
2123 return 1;
2124}
2125
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002126/* This function escape special json characters. The returned string can be
2127 * safely set between two '"' and used as json string. The json string is
2128 * defined like this:
2129 *
2130 * any Unicode character except '"' or '\' or control character
2131 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
2132 *
2133 * The enum input_type contain all the allowed mode for decoding the input
2134 * string.
2135 */
2136enum input_type {
2137 IT_ASCII = 0,
2138 IT_UTF8,
2139 IT_UTF8S,
2140 IT_UTF8P,
2141 IT_UTF8PS,
2142};
2143static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
2144 const char *file, int line, char **err)
2145{
2146 if (!arg) {
2147 memprintf(err, "Unexpected empty arg list");
2148 return 0;
2149 }
2150
2151 if (arg->type != ARGT_STR) {
2152 memprintf(err, "Unexpected arg type");
2153 return 0;
2154 }
2155
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002156 if (strcmp(arg->data.str.area, "") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002157 arg->type = ARGT_SINT;
2158 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002159 return 1;
2160 }
2161
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002162 else if (strcmp(arg->data.str.area, "ascii") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002163 arg->type = ARGT_SINT;
2164 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002165 return 1;
2166 }
2167
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002168 else if (strcmp(arg->data.str.area, "utf8") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002169 arg->type = ARGT_SINT;
2170 arg->data.sint = IT_UTF8;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002171 return 1;
2172 }
2173
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002174 else if (strcmp(arg->data.str.area, "utf8s") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002175 arg->type = ARGT_SINT;
2176 arg->data.sint = IT_UTF8S;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002177 return 1;
2178 }
2179
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002180 else if (strcmp(arg->data.str.area, "utf8p") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002181 arg->type = ARGT_SINT;
2182 arg->data.sint = IT_UTF8P;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002183 return 1;
2184 }
2185
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002186 else if (strcmp(arg->data.str.area, "utf8ps") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002187 arg->type = ARGT_SINT;
2188 arg->data.sint = IT_UTF8PS;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002189 return 1;
2190 }
2191
Thierry FOURNIER / OZON.IOa69c9122016-11-23 01:13:57 +01002192 memprintf(err, "Unexpected input code type. "
2193 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002194 return 0;
2195}
2196
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002197static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002198{
Willy Tarreau83061a82018-07-13 11:56:34 +02002199 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002200 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
2201 const char *str;
2202 int len;
2203 enum input_type input_type = IT_ASCII;
2204 unsigned int c;
2205 unsigned int ret;
2206 char *p;
2207
2208 if (arg_p)
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002209 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002210
2211 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002212 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002213
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002214 p = smp->data.u.str.area;
2215 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002216
2217 if (input_type == IT_ASCII) {
2218 /* Read input as ASCII. */
2219 c = *(unsigned char *)p;
2220 p++;
2221 }
2222 else {
2223 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002224 ret = utf8_next(p,
2225 smp->data.u.str.data - ( p - smp->data.u.str.area),
2226 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002227 p += utf8_return_length(ret);
2228
2229 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2230 return 0;
2231 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2232 continue;
2233 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2234 return 0;
2235 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2236 continue;
2237
2238 /* Check too big values. */
2239 if ((unsigned int)c > 0xffff) {
2240 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2241 return 0;
2242 continue;
2243 }
2244 }
2245
2246 /* Convert character. */
2247 if (c == '"') {
2248 len = 2;
2249 str = "\\\"";
2250 }
2251 else if (c == '\\') {
2252 len = 2;
2253 str = "\\\\";
2254 }
2255 else if (c == '/') {
2256 len = 2;
2257 str = "\\/";
2258 }
2259 else if (c == '\b') {
2260 len = 2;
2261 str = "\\b";
2262 }
2263 else if (c == '\f') {
2264 len = 2;
2265 str = "\\f";
2266 }
2267 else if (c == '\r') {
2268 len = 2;
2269 str = "\\r";
2270 }
2271 else if (c == '\n') {
2272 len = 2;
2273 str = "\\n";
2274 }
2275 else if (c == '\t') {
2276 len = 2;
2277 str = "\\t";
2278 }
Willy Tarreau90807112020-02-25 08:16:33 +01002279 else if (c > 0xff || !isprint((unsigned char)c)) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002280 /* isprint generate a segfault if c is too big. The man says that
2281 * c must have the value of an unsigned char or EOF.
2282 */
2283 len = 6;
2284 _str[0] = '\\';
2285 _str[1] = 'u';
2286 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2287 str = _str;
2288 }
2289 else {
2290 len = 1;
Willy Tarreau5715da22020-02-25 08:37:37 +01002291 _str[0] = c;
2292 str = _str;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002293 }
2294
2295 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002296 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002297 return 0;
2298
2299 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002300 memcpy(temp->area + temp->data, str, len);
2301 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002302 }
2303
2304 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002305 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002306 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002307
2308 return 1;
2309}
2310
Emeric Brun54c4ac82014-11-03 15:32:43 +01002311/* This sample function is designed to extract some bytes from an input buffer.
2312 * First arg is the offset.
2313 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002314static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002315{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002316 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2317 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002318 return 1;
2319 }
2320
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002321 if (smp->data.u.str.size)
2322 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002323 smp->data.u.str.data -= arg_p[0].data.sint;
2324 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002325
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002326 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2327 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002328
2329 return 1;
2330}
2331
Emeric Brunf399b0d2014-11-03 17:07:03 +01002332static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2333 const char *file, int line, char **err)
2334{
2335 struct arg *arg = args;
2336
2337 if (!arg) {
2338 memprintf(err, "Unexpected empty arg list");
2339 return 0;
2340 }
2341
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002342 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002343 memprintf(err, "Unexpected arg type");
2344 return 0;
2345 }
2346
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002347 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002348 memprintf(err, "Unexpected value 0 for index");
2349 return 0;
2350 }
2351
2352 arg++;
2353
2354 if (arg->type != ARGT_STR) {
2355 memprintf(err, "Unexpected arg type");
2356 return 0;
2357 }
2358
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002359 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002360 memprintf(err, "Empty separators list");
2361 return 0;
2362 }
2363
2364 return 1;
2365}
2366
2367/* This sample function is designed to a return selected part of a string (field).
2368 * First arg is the index of the field (start at 1)
2369 * Second arg is a char list of separators (type string)
2370 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002371static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002372{
Marcin Deranek9631a282018-04-16 14:30:46 +02002373 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002374 char *start, *end;
2375 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002376 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002377
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002378 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002379 return 0;
2380
Marcin Deranek9631a282018-04-16 14:30:46 +02002381 if (arg_p[0].data.sint < 0) {
2382 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002383 end = start = smp->data.u.str.area + smp->data.u.str.data;
2384 while (start > smp->data.u.str.area) {
2385 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2386 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002387 if (field == arg_p[0].data.sint) {
2388 if (count == 1)
2389 goto found;
2390 else if (count > 1)
2391 count--;
2392 } else {
2393 end = start-1;
2394 field--;
2395 }
2396 break;
2397 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002398 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002399 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002400 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002401 } else {
2402 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002403 end = start = smp->data.u.str.area;
2404 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2405 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2406 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002407 if (field == arg_p[0].data.sint) {
2408 if (count == 1)
2409 goto found;
2410 else if (count > 1)
2411 count--;
2412 } else {
2413 start = end+1;
2414 field++;
2415 }
2416 break;
2417 }
2418 }
2419 end++;
2420 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002421 }
2422
2423 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002424 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002425 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002426 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002427 }
2428found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002429 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002430 /* If ret string is len 0, no need to
2431 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002432 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002433 return 1;
2434
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002435 smp->data.u.str.area = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002436
2437 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002438 Note: smp->data.u.str.size cannot be set to 0 */
2439 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002440 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002441
2442 return 1;
2443}
2444
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002445/* This sample function is designed to return a word from a string.
2446 * First arg is the index of the word (start at 1)
2447 * Second arg is a char list of words separators (type string)
2448 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002449static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002450{
Marcin Deranek9631a282018-04-16 14:30:46 +02002451 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002452 char *start, *end;
2453 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002454 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002455
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002456 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002457 return 0;
2458
2459 word = 0;
2460 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002461 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002462 end = start = smp->data.u.str.area + smp->data.u.str.data;
2463 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002464 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002465 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2466 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002467 issep = 1;
2468 break;
2469 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002470 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002471 if (!inword) {
2472 if (!issep) {
2473 if (word != arg_p[0].data.sint) {
2474 word--;
2475 end = start;
2476 }
2477 inword = 1;
2478 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002479 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002480 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002481 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002482 if (count == 1)
2483 goto found;
2484 else if (count > 1)
2485 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002486 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002487 inword = 0;
2488 }
2489 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002490 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002491 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002492 end = start = smp->data.u.str.area;
2493 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002494 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002495 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2496 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002497 issep = 1;
2498 break;
2499 }
2500 }
2501 if (!inword) {
2502 if (!issep) {
2503 if (word != arg_p[0].data.sint) {
2504 word++;
2505 start = end;
2506 }
2507 inword = 1;
2508 }
2509 }
2510 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002511 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002512 if (count == 1)
2513 goto found;
2514 else if (count > 1)
2515 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002516 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002517 inword = 0;
2518 }
2519 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002520 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002521 }
2522
2523 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002524 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002525 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002526 return 1;
2527 }
2528found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002529 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002530 /* If ret string is len 0, no need to
2531 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002532 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002533 return 1;
2534
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002535 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002536
2537 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002538 Note: smp->data.u.str.size cannot be set to 0 */
2539 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002540 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002541
2542 return 1;
2543}
2544
Willy Tarreau7eda8492015-01-20 19:47:06 +01002545static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2546 const char *file, int line, char **err)
2547{
2548 struct arg *arg = args;
2549 char *p;
2550 int len;
2551
2552 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2553 arg[0].type_flags = 0;
2554
2555 if (arg[2].type != ARGT_STR)
2556 return 1;
2557
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002558 p = arg[2].data.str.area;
2559 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002560 while (len) {
2561 if (*p == 'i') {
2562 arg[0].type_flags |= ARGF_REG_ICASE;
2563 }
2564 else if (*p == 'g') {
2565 arg[0].type_flags |= ARGF_REG_GLOB;
2566 }
2567 else {
2568 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2569 return 0;
2570 }
2571 p++;
2572 len--;
2573 }
2574 return 1;
2575}
2576
2577/* This sample function is designed to do the equivalent of s/match/replace/ on
2578 * the input string. It applies a regex and restarts from the last matched
2579 * location until nothing matches anymore. First arg is the regex to apply to
2580 * the input string, second arg is the replacement expression.
2581 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002582static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002583{
2584 char *start, *end;
2585 struct my_regex *reg = arg_p[0].data.reg;
2586 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002587 struct buffer *trash = get_trash_chunk();
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002588 struct buffer *output;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002589 int flag, max;
2590 int found;
2591
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002592 start = smp->data.u.str.area;
2593 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002594
2595 flag = 0;
2596 while (1) {
2597 /* check for last round which is used to copy remaining parts
2598 * when not running in global replacement mode.
2599 */
2600 found = 0;
2601 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2602 /* Note: we can have start == end on empty strings or at the end */
2603 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2604 }
2605
2606 if (!found)
2607 pmatch[0].rm_so = end - start;
2608
2609 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002610 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002611 if (max && pmatch[0].rm_so > 0) {
2612 if (max > pmatch[0].rm_so)
2613 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002614 memcpy(trash->area + trash->data, start, max);
2615 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002616 }
2617
2618 if (!found)
2619 break;
2620
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002621 output = alloc_trash_chunk();
Willy Tarreau23997da2020-02-18 14:27:44 +01002622 if (!output)
2623 break;
2624
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002625 output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2626
Willy Tarreau7eda8492015-01-20 19:47:06 +01002627 /* replace the matching part */
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002628 max = output->size - output->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002629 if (max) {
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002630 if (max > output->data)
2631 max = output->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002632 memcpy(trash->area + trash->data,
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002633 output->area, max);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002634 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002635 }
2636
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002637 free_trash_chunk(output);
2638
Willy Tarreau7eda8492015-01-20 19:47:06 +01002639 /* stop here if we're done with this string */
2640 if (start >= end)
2641 break;
2642
2643 /* We have a special case for matches of length 0 (eg: "x*y*").
2644 * These ones are considered to match in front of a character,
2645 * so we have to copy that character and skip to the next one.
2646 */
2647 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002648 if (trash->data < trash->size)
2649 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002650 pmatch[0].rm_eo++;
2651 }
2652
2653 start += pmatch[0].rm_eo;
2654 flag |= REG_NOTBOL;
2655 }
2656
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002657 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002658 return 1;
2659}
2660
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002661/* This function check an operator entry. It expects a string.
2662 * The string can be an integer or a variable name.
2663 */
2664static int check_operator(struct arg *args, struct sample_conv *conv,
2665 const char *file, int line, char **err)
2666{
2667 const char *str;
2668 const char *end;
2669
2670 /* Try to decode a variable. */
2671 if (vars_check_arg(&args[0], NULL))
2672 return 1;
2673
2674 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002675 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002676 end = str + strlen(str);
2677 args[0].data.sint = read_int64(&str, end);
2678 if (*str != '\0') {
2679 memprintf(err, "expects an integer or a variable name");
2680 return 0;
2681 }
2682 args[0].type = ARGT_SINT;
2683 return 1;
2684}
2685
Willy Tarreau6204cd92016-03-10 16:33:04 +01002686/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002687 * If the arg contain an integer, the integer is returned in the
2688 * sample. If the arg contains a variable descriptor, it returns the
2689 * variable value.
2690 *
2691 * This function returns 0 if an error occurs, otherwise it returns 1.
2692 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002693static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002694{
2695 switch (arg->type) {
2696 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002697 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002698 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002699 return 1;
2700 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002701 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002702 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002703 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002704 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002705 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002706 return 0;
2707 return 1;
2708 default:
2709 return 0;
2710 }
2711}
2712
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002713/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002714 * result.
2715 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002716static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002717{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002718 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002719 return 1;
2720}
2721
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002722/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002723 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002724 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002725static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002726{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002727 struct sample tmp;
2728
Willy Tarreau7560dd42016-03-10 16:28:58 +01002729 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002730 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002731 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002732 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002733 return 1;
2734}
2735
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002736/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002737 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002738 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002739static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002740{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002741 struct sample tmp;
2742
Willy Tarreau7560dd42016-03-10 16:28:58 +01002743 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002744 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002745 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002746 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002747 return 1;
2748}
2749
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002750/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002751 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002752 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002753static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002754{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002755 struct sample tmp;
2756
Willy Tarreau7560dd42016-03-10 16:28:58 +01002757 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002758 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002759 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002760 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002761 return 1;
2762}
2763
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002764static inline long long int arith_add(long long int a, long long int b)
2765{
2766 /* Prevent overflow and makes capped calculus.
2767 * We must ensure that the check calculus doesn't
2768 * exceed the signed 64 bits limits.
2769 *
2770 * +----------+----------+
2771 * | a<0 | a>=0 |
2772 * +------+----------+----------+
2773 * | b<0 | MIN-a>b | no check |
2774 * +------+----------+----------+
2775 * | b>=0 | no check | MAX-a<b |
2776 * +------+----------+----------+
2777 */
2778 if ((a ^ b) >= 0) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002779 /* signs are different. */
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002780 if (a < 0) {
2781 if (LLONG_MIN - a > b)
2782 return LLONG_MIN;
2783 }
2784 if (LLONG_MAX - a < b)
2785 return LLONG_MAX;
2786 }
2787 return a + b;
2788}
2789
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002790/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002791 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002792 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002793static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002794{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002795 struct sample tmp;
2796
Willy Tarreau7560dd42016-03-10 16:28:58 +01002797 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002798 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002799 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002800 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002801 return 1;
2802}
2803
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002804/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002805 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002806 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002807static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002808 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002809{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002810 struct sample tmp;
2811
Willy Tarreau7560dd42016-03-10 16:28:58 +01002812 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002813 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002814 return 0;
2815
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002816 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2817 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2818 * of -LLONG_MIN and correct the result.
2819 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002820 if (tmp.data.u.sint == LLONG_MIN) {
2821 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2822 if (smp->data.u.sint < LLONG_MAX)
2823 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002824 return 1;
2825 }
2826
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002827 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002828 * the second operand.
2829 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002830 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002831 return 1;
2832}
2833
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002834/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002835 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002836 * If the result makes an overflow, then the largest possible quantity is
2837 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002838 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002839static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002840 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002841{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002842 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002843 long long int c;
2844
Willy Tarreau7560dd42016-03-10 16:28:58 +01002845 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002846 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002847 return 0;
2848
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002849 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002850 if (!smp->data.u.sint || !tmp.data.u.sint) {
2851 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002852 return 1;
2853 }
2854
2855 /* The multiply between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002856 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002857 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002858 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2859 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002860 return 1;
2861 }
2862
2863 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002864 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002865
2866 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002867 if (smp->data.u.sint != c / tmp.data.u.sint) {
2868 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2869 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002870 return 1;
2871 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002872 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002873 return 1;
2874 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002875 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002876 return 1;
2877}
2878
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002879/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002880 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002881 * If arg_p makes the result overflow, then the largest possible quantity is
2882 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002883 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002884static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002885 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002886{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002887 struct sample tmp;
2888
Willy Tarreau7560dd42016-03-10 16:28:58 +01002889 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002890 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002891 return 0;
2892
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002893 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002894 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002895 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002896 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002897 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2898 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002899 return 1;
2900 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002901 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002902 return 1;
2903 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002904 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002905 return 1;
2906}
2907
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002908/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002909 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002910 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002911 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002912static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002913 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002914{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002915 struct sample tmp;
2916
Willy Tarreau7560dd42016-03-10 16:28:58 +01002917 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002918 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002919 return 0;
2920
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002921 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002922 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002923 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002924 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002925 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2926 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002927 return 1;
2928 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002929 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002930 return 1;
2931 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002932 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002933 return 1;
2934}
2935
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002936/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002937 * result.
2938 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002939static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002940 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002941{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002942 if (smp->data.u.sint == LLONG_MIN)
2943 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002944 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002945 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002946 return 1;
2947}
2948
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002949/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002950 * false. The output is a BOOL.
2951 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002952static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002953 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002954{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002955 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002956 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002957 return 1;
2958}
2959
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002960/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002961 * truee. The output is a BOOL.
2962 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002963static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002964 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002965{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002966 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002967 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002968 return 1;
2969}
2970
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002971/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002972 * The output is a BOOL.
2973 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002974static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002975 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002976{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002977 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002978 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002979 return 1;
2980}
2981
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002982/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002983 * The output is a BOOL.
2984 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002985static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002986 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002987{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002988 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002989 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002990 return 1;
2991}
2992
Willy Tarreau280f42b2018-02-19 15:34:12 +01002993/* appends an optional const string, an optional variable contents and another
2994 * optional const string to an existing string.
2995 */
2996static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
2997{
Willy Tarreau83061a82018-07-13 11:56:34 +02002998 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002999 struct sample tmp;
3000 int max;
3001
3002 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003003 trash->data = smp->data.u.str.data;
3004 if (trash->data > trash->size - 1)
3005 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003006
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003007 memcpy(trash->area, smp->data.u.str.area, trash->data);
3008 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003009
3010 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003011 max = arg_p[0].data.str.data;
3012 if (max > trash->size - 1 - trash->data)
3013 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003014
3015 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003016 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
3017 trash->data += max;
3018 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003019 }
3020
3021 /* append second string (variable) if it's found and we can turn it
3022 * into a string.
3023 */
3024 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3025 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
3026 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
3027 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
3028
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003029 max = tmp.data.u.str.data;
3030 if (max > trash->size - 1 - trash->data)
3031 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003032
3033 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003034 memcpy(trash->area + trash->data, tmp.data.u.str.area,
3035 max);
3036 trash->data += max;
3037 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003038 }
3039 }
3040
3041 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003042 max = arg_p[2].data.str.data;
3043 if (max > trash->size - 1 - trash->data)
3044 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003045
3046 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003047 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
3048 trash->data += max;
3049 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003050 }
3051
3052 smp->data.u.str = *trash;
3053 smp->data.type = SMP_T_STR;
3054 return 1;
3055}
3056
3057/* This function checks the "concat" converter's arguments and extracts the
3058 * variable name and its scope.
3059 */
3060static int smp_check_concat(struct arg *args, struct sample_conv *conv,
3061 const char *file, int line, char **err)
3062{
3063 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003064 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
3065 memprintf(err, "failed to register variable name '%s'",
3066 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003067 return 0;
3068 }
3069 return 1;
3070}
3071
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003072/* Compares string with a variable containing a string. Return value
Tim Duesterhusca097c12018-04-27 21:18:45 +02003073 * is compatible with strcmp(3)'s return value.
3074 */
3075static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
3076{
3077 struct sample tmp;
3078 int max, result;
3079
3080 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3081 if (arg_p[0].type != ARGT_VAR)
3082 return 0;
3083 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
3084 return 0;
3085 if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp))
3086 return 0;
3087
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003088 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
3089 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003090 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003091 if (smp->data.u.str.data != tmp.data.u.str.data) {
3092 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02003093 result = -1;
3094 }
3095 else {
3096 result = 1;
3097 }
3098 }
3099 }
3100
3101 smp->data.u.sint = result;
3102 smp->data.type = SMP_T_SINT;
3103 return 1;
3104}
3105
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003106#ifdef USE_OPENSSL
3107/* Compares bytestring with a variable containing a bytestring. Return value
3108 * is `true` if both bytestrings are bytewise identical and `false` otherwise.
3109 *
3110 * Comparison will be performed in constant time if both bytestrings are of
3111 * the same length. If the lengths differ execution time will not be constant.
3112 */
3113static int sample_conv_secure_memcmp(const struct arg *arg_p, struct sample *smp, void *private)
3114{
3115 struct sample tmp;
3116 int result;
3117
3118 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3119 if (arg_p[0].type != ARGT_VAR)
3120 return 0;
3121 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
3122 return 0;
3123 if (!sample_casts[tmp.data.type][SMP_T_BIN](&tmp))
3124 return 0;
3125
3126 if (smp->data.u.str.data != tmp.data.u.str.data) {
3127 smp->data.u.sint = 0;
3128 smp->data.type = SMP_T_BOOL;
3129 return 1;
3130 }
3131
3132 /* The following comparison is performed in constant time. */
3133 result = CRYPTO_memcmp(smp->data.u.str.area, tmp.data.u.str.area, smp->data.u.str.data);
3134
3135 smp->data.u.sint = result == 0;
3136 smp->data.type = SMP_T_BOOL;
3137 return 1;
3138}
3139#endif
3140
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003141#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
3142#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
3143#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
3144
3145/*
3146 * Extract the field value of an input binary sample. Takes a mandatory argument:
3147 * the protocol buffers field identifier (dotted notation) internally represented
3148 * as an array of unsigned integers and its size.
3149 * Return 1 if the field was found, 0 if not.
3150 */
3151static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
3152{
3153 unsigned char *pos;
3154 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003155
3156 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003157 grpc_left = smp->data.u.str.data;
3158
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01003159 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003160 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003161
3162 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
3163
3164 pos += GRPC_MSG_HEADER_SZ;
3165 grpc_left -= GRPC_MSG_HEADER_SZ;
3166
3167 if (grpc_left < left)
3168 return 0;
3169
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01003170 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
3171 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003172
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003173 grpc_left -= grpc_msg_len;
3174 }
3175
3176 return 0;
3177}
3178
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003179static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
3180{
3181 unsigned char *pos;
3182 size_t left;
3183
3184 pos = (unsigned char *)smp->data.u.str.area;
3185 left = smp->data.u.str.data;
3186
3187 return protobuf_field_lookup(arg_p, smp, &pos, &left);
3188}
3189
3190static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
3191 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003192{
3193 if (!args[1].type) {
3194 args[1].type = ARGT_SINT;
3195 args[1].data.sint = PBUF_T_BINARY;
3196 }
3197 else {
3198 int pbuf_type;
3199
3200 pbuf_type = protobuf_type(args[1].data.str.area);
3201 if (pbuf_type == -1) {
3202 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
3203 return 0;
3204 }
3205
3206 args[1].type = ARGT_SINT;
3207 args[1].data.sint = pbuf_type;
3208 }
3209
3210 return 1;
3211}
3212
Tim Duesterhusca097c12018-04-27 21:18:45 +02003213/* This function checks the "strcmp" converter's arguments and extracts the
3214 * variable name and its scope.
3215 */
3216static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
3217 const char *file, int line, char **err)
3218{
3219 /* Try to decode a variable. */
3220 if (vars_check_arg(&args[0], NULL))
3221 return 1;
3222
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003223 memprintf(err, "failed to register variable name '%s'",
3224 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003225 return 0;
3226}
3227
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003228#ifdef USE_OPENSSL
3229/* This function checks the "secure_memcmp" converter's arguments and extracts the
3230 * variable name and its scope.
3231 */
3232static int smp_check_secure_memcmp(struct arg *args, struct sample_conv *conv,
3233 const char *file, int line, char **err)
3234{
3235 /* Try to decode a variable. */
3236 if (vars_check_arg(&args[0], NULL))
3237 return 1;
3238
3239 memprintf(err, "failed to register variable name '%s'",
3240 args[0].data.str.area);
3241 return 0;
3242}
3243#endif
3244
Christopher Faulet4ccc12f2020-04-01 09:08:32 +02003245/**/
3246static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private)
3247{
3248 struct buffer *tmp;
3249 uint32_t n;
3250
3251 n = htonl((uint32_t)smp->data.u.sint);
3252 tmp = get_trash_chunk();
3253
3254 memcpy(b_head(tmp), &n, 4);
3255 b_add(tmp, 4);
3256
3257 smp->data.u.str = *tmp;
3258 smp->data.type = SMP_T_BIN;
3259 return 1;
3260}
3261
Christopher Fauletea159d62020-04-01 16:21:44 +02003262/**/
3263static int sample_conv_cut_crlf(const struct arg *arg_p, struct sample *smp, void *private)
3264{
3265 char *p;
3266 size_t l;
3267
3268 p = smp->data.u.str.area;
3269 for (l = 0; l < smp->data.u.str.data; l++) {
3270 if (*(p+l) == '\r' || *(p+l) == '\n')
3271 break;
3272 }
3273 smp->data.u.str.data = l;
3274 return 1;
3275}
3276
Christopher Faulet51fc9d12020-04-01 17:24:41 +02003277/**/
3278static int sample_conv_ltrim(const struct arg *arg_p, struct sample *smp, void *private)
3279{
3280 char *delimiters, *p;
3281 size_t dlen, l;
3282
3283 delimiters = arg_p[0].data.str.area;
3284 dlen = arg_p[0].data.str.data;
3285
3286 l = smp->data.u.str.data;
3287 p = smp->data.u.str.area;
3288 while (l && memchr(delimiters, *p, dlen) != NULL) {
3289 p++;
3290 l--;
3291 }
3292
3293 smp->data.u.str.area = p;
3294 smp->data.u.str.data = l;
3295 return 1;
3296}
3297
Christopher Faulet568415a2020-04-01 17:24:47 +02003298/**/
3299static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *private)
3300{
3301 char *delimiters, *p;
3302 size_t dlen, l;
3303
3304 delimiters = arg_p[0].data.str.area;
3305 dlen = arg_p[0].data.str.data;
3306
3307 l = smp->data.u.str.data;
3308 p = smp->data.u.str.area + l - 1;
3309 while (l && memchr(delimiters, *p, dlen) != NULL) {
3310 p--;
3311 l--;
3312 }
3313
3314 smp->data.u.str.data = l;
3315 return 1;
3316}
3317
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003318/************************************************************************/
3319/* All supported sample fetch functions must be declared here */
3320/************************************************************************/
3321
3322/* force TRUE to be returned at the fetch level */
3323static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003324smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003325{
Willy Tarreau280f42b2018-02-19 15:34:12 +01003326 if (!smp_make_rw(smp))
3327 return 0;
3328
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003329 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003330 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003331 return 1;
3332}
3333
3334/* force FALSE to be returned at the fetch level */
3335static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003336smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003337{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003338 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003339 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003340 return 1;
3341}
3342
3343/* retrieve environment variable $1 as a string */
3344static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003345smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003346{
3347 char *env;
3348
3349 if (!args || args[0].type != ARGT_STR)
3350 return 0;
3351
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003352 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003353 if (!env)
3354 return 0;
3355
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003356 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003357 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003358 smp->data.u.str.area = env;
3359 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003360 return 1;
3361}
3362
Damien Claisseae6f1252019-10-30 15:57:28 +00003363/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3364 * optional string representing the unit of the result: "s" for seconds, "ms" for
3365 * milliseconds and "us" for microseconds.
3366 * Returns 0 on error and non-zero if OK.
3367 */
3368int smp_check_date_unit(struct arg *args, char **err)
3369{
3370 if (args[1].type == ARGT_STR) {
3371 if (strcmp(args[1].data.str.area, "s") == 0) {
3372 args[1].data.sint = TIME_UNIT_S;
3373 }
3374 else if (strcmp(args[1].data.str.area, "ms") == 0) {
3375 args[1].data.sint = TIME_UNIT_MS;
3376 }
3377 else if (strcmp(args[1].data.str.area, "us") == 0) {
3378 args[1].data.sint = TIME_UNIT_US;
3379 }
3380 else {
3381 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
3382 args[1].data.str.area);
3383 return 0;
3384 }
3385 free(args[1].data.str.area);
3386 args[1].data.str.area = NULL;
3387 args[1].type = ARGT_SINT;
3388 }
3389 else if (args[1].type != ARGT_STOP) {
3390 memprintf(err, "Unexpected arg type");
3391 return 0;
3392 }
3393
3394 return 1;
3395}
3396
3397/* retrieve the current local date in epoch time, converts it to milliseconds
3398 * or microseconds if asked to in optional args[1] unit param, and applies an
3399 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003400 */
3401static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003402smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003403{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003404 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003405
Damien Claisseae6f1252019-10-30 15:57:28 +00003406 /* report in milliseconds */
3407 if (args && args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
3408 smp->data.u.sint *= 1000;
3409 smp->data.u.sint += date.tv_usec / 1000;
3410 }
3411 /* report in microseconds */
3412 else if (args && args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
3413 smp->data.u.sint *= 1000000;
3414 smp->data.u.sint += date.tv_usec;
3415 }
3416
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003417 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003418 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003419 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003420
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003421 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003422 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3423 return 1;
3424}
3425
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003426/* retrieve the current microsecond part of the date */
3427static int
3428smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3429{
3430 smp->data.u.sint = date.tv_usec;
3431 smp->data.type = SMP_T_SINT;
3432 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3433 return 1;
3434}
3435
3436
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003437/* returns the hostname */
3438static int
3439smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3440{
3441 smp->data.type = SMP_T_STR;
3442 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003443 smp->data.u.str.area = hostname;
3444 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003445 return 1;
3446}
3447
Willy Tarreau0f30d262014-11-24 16:02:05 +01003448/* returns the number of processes */
3449static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003450smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003451{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003452 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003453 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003454 return 1;
3455}
3456
3457/* returns the number of the current process (between 1 and nbproc */
3458static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003459smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003460{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003461 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003462 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003463 return 1;
3464}
3465
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003466/* returns the number of the current thread (between 1 and nbthread */
3467static int
3468smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3469{
3470 smp->data.type = SMP_T_SINT;
3471 smp->data.u.sint = tid;
3472 return 1;
3473}
3474
Willy Tarreau84310e22014-02-14 11:59:04 +01003475/* generate a random 32-bit integer for whatever purpose, with an optional
3476 * range specified in argument.
3477 */
3478static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003479smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01003480{
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003481 smp->data.u.sint = ha_random32();
Willy Tarreau84310e22014-02-14 11:59:04 +01003482
3483 /* reduce if needed. Don't do a modulo, use all bits! */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003484 if (args && args[0].type == ARGT_SINT)
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003485 smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32;
Willy Tarreau84310e22014-02-14 11:59:04 +01003486
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003487 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01003488 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3489 return 1;
3490}
3491
Willy Tarreau0f30d262014-11-24 16:02:05 +01003492/* returns true if the current process is stopping */
3493static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003494smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003495{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003496 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003497 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003498 return 1;
3499}
3500
Willy Tarreau70fe9442018-11-22 16:07:39 +01003501/* returns the number of calls of the current stream's process_stream() */
3502static int
3503smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
3504{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003505 if (!smp->strm)
3506 return 0;
3507
Willy Tarreau70fe9442018-11-22 16:07:39 +01003508 smp->data.type = SMP_T_SINT;
3509 smp->data.u.sint = smp->strm->task->calls;
3510 return 1;
3511}
3512
3513/* returns the average number of nanoseconds spent processing the stream per call */
3514static int
3515smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3516{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003517 if (!smp->strm)
3518 return 0;
3519
Willy Tarreau70fe9442018-11-22 16:07:39 +01003520 smp->data.type = SMP_T_SINT;
3521 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
3522 return 1;
3523}
3524
3525/* returns the total number of nanoseconds spent processing the stream */
3526static int
3527smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3528{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003529 if (!smp->strm)
3530 return 0;
3531
Willy Tarreau70fe9442018-11-22 16:07:39 +01003532 smp->data.type = SMP_T_SINT;
3533 smp->data.u.sint = smp->strm->task->cpu_time;
3534 return 1;
3535}
3536
3537/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
3538static int
3539smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3540{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003541 if (!smp->strm)
3542 return 0;
3543
Willy Tarreau70fe9442018-11-22 16:07:39 +01003544 smp->data.type = SMP_T_SINT;
3545 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
3546 return 1;
3547}
3548
3549/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
3550static int
3551smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3552{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003553 if (!smp->strm)
3554 return 0;
3555
Willy Tarreau70fe9442018-11-22 16:07:39 +01003556 smp->data.type = SMP_T_SINT;
3557 smp->data.u.sint = smp->strm->task->lat_time;
3558 return 1;
3559}
3560
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003561static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
3562{
3563 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003564 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003565 smp->data.u.str.area = args[0].data.str.area;
3566 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003567 return 1;
3568}
3569
3570static int smp_check_const_bool(struct arg *args, char **err)
3571{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003572 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
3573 strcasecmp(args[0].data.str.area, "1") == 0) {
Tim Duesterhusc7d8a862020-07-04 11:49:45 +02003574 free(args[0].data.str.area);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003575 args[0].type = ARGT_SINT;
3576 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003577 return 1;
3578 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003579 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
3580 strcasecmp(args[0].data.str.area, "0") == 0) {
Tim Duesterhusc7d8a862020-07-04 11:49:45 +02003581 free(args[0].data.str.area);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003582 args[0].type = ARGT_SINT;
3583 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003584 return 1;
3585 }
3586 memprintf(err, "Expects 'true', 'false', '0' or '1'");
3587 return 0;
3588}
3589
3590static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
3591{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003592 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003593 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003594 return 1;
3595}
3596
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003597static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003598{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003599 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003600 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003601 return 1;
3602}
3603
3604static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
3605{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003606 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003607 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003608 return 1;
3609}
3610
3611static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
3612{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003613 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003614 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003615 return 1;
3616}
3617
3618static int smp_check_const_bin(struct arg *args, char **err)
3619{
David Carlier64a16ab2016-04-08 10:37:02 +01003620 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003621 int binstrlen;
3622
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003623 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003624 return 0;
3625 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003626 args[0].data.str.area = binstr;
3627 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003628 return 1;
3629}
3630
3631static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
3632{
3633 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003634 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003635 smp->data.u.str.area = args[0].data.str.area;
3636 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003637 return 1;
3638}
3639
3640static int smp_check_const_meth(struct arg *args, char **err)
3641{
3642 enum http_meth_t meth;
3643 int i;
3644
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003645 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003646 if (meth != HTTP_METH_OTHER) {
Tim Duesterhus041a6262020-07-04 11:49:46 +02003647 free(args[0].data.str.area);
3648
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003649 args[0].type = ARGT_SINT;
3650 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003651 } else {
3652 /* Check method avalaibility. A methos is a token defined as :
3653 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
3654 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
3655 * token = 1*tchar
3656 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003657 for (i = 0; i < args[0].data.str.data; i++) {
3658 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003659 memprintf(err, "expects valid method.");
3660 return 0;
3661 }
3662 }
3663 }
3664 return 1;
3665}
3666
3667static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
3668{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003669 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003670 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003671 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003672 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003673 smp->data.u.meth.str.area = "";
3674 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003675 } else {
3676 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003677 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003678 smp->data.u.meth.str.area = args[0].data.str.area;
3679 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003680 }
3681 return 1;
3682}
3683
Luca Schimweg8a694b82019-09-10 15:42:52 +02003684// This function checks the "uuid" sample's arguments.
3685// Function won't get called when no parameter is specified (maybe a bug?)
3686static int smp_check_uuid(struct arg *args, char **err)
3687{
3688 if (!args[0].type) {
3689 args[0].type = ARGT_SINT;
3690 args[0].data.sint = 4;
3691 }
3692 else if (args[0].data.sint != 4) {
3693 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
3694 return 0;
3695 }
3696
3697 return 1;
3698}
3699
3700// Generate a RFC4122 UUID (default is v4 = fully random)
3701static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
3702{
3703 if (args[0].data.sint == 4 || !args[0].type) {
Willy Tarreauee3bcdd2020-03-08 17:48:17 +01003704 ha_generate_uuid(&trash);
Luca Schimweg8a694b82019-09-10 15:42:52 +02003705 smp->data.type = SMP_T_STR;
3706 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3707 smp->data.u.str = trash;
3708 return 1;
3709 }
3710
3711 // more implementations of other uuid formats possible here
3712 return 0;
3713}
3714
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003715/* Note: must not be declared <const> as its list will be overwritten.
3716 * Note: fetches that may return multiple types must be declared as the lowest
3717 * common denominator, the type that can be casted into all other ones. For
3718 * instance IPv4/IPv6 must be declared IPv4.
3719 */
3720static struct sample_fetch_kw_list smp_kws = {ILH, {
3721 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
3722 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003723 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Damien Claisseae6f1252019-10-30 15:57:28 +00003724 { "date", smp_fetch_date, ARG2(0,SINT,STR), smp_check_date_unit, SMP_T_SINT, SMP_USE_INTRN },
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003725 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003726 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003727 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3728 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003729 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003730 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01003731 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003732 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Luca Schimweg8a694b82019-09-10 15:42:52 +02003733 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003734
3735 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3736 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3737 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3738 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3739 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003740
3741 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
3742 { "bool", smp_fetch_const_bool, ARG1(1,STR), smp_check_const_bool, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003743 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003744 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
3745 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
3746 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
3747 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
3748
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003749 { /* END */ },
3750}};
3751
Willy Tarreau0108d902018-11-25 19:14:37 +01003752INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
3753
Emeric Brun107ca302010-01-04 16:16:05 +01003754/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003755static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau0851fd52019-12-17 10:07:25 +01003756 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
Holger Just1bfc24b2017-05-06 00:56:53 +02003757 { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN },
Emeric Brun53d1a982014-04-30 18:21:37 +02003758 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02003759 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
3760 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau60a2ee72017-12-15 07:13:48 +01003761 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01003762 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Dragan Dosen3f957b22017-10-24 09:27:34 +02003763 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
Tim Duesterhus1478aa72018-01-25 16:24:51 +01003764 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003765 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
3766 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003767 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01003768 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003769 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3770 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3771 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER01e09742016-12-26 11:46:11 +01003772 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3773 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02003774 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003775 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
Marcin Deranek9631a282018-04-16 14:30:46 +02003776 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
3777 { "word", sample_conv_word, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
Willy Tarreau7eda8492015-01-20 19:47:06 +01003778 { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02003779 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02003780#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01003781 { "sha2", sample_conv_sha2, ARG1(0, SINT), smp_check_sha2, SMP_T_BIN, SMP_T_BIN },
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02003782#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
3783 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
3784#endif
Patrick Gansterer8e366512020-04-22 16:47:57 +02003785 { "digest", sample_conv_crypto_digest, ARG1(1,STR), check_crypto_digest, SMP_T_BIN, SMP_T_BIN },
3786 { "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 +02003787#endif
Willy Tarreau280f42b2018-02-19 15:34:12 +01003788 { "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 +02003789 { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT },
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003790#ifdef USE_OPENSSL
3791 { "secure_memcmp", sample_conv_secure_memcmp, ARG1(1,STR), smp_check_secure_memcmp, SMP_T_BIN, SMP_T_BOOL },
3792#endif
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003793
3794 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003795 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
3796 { "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 +01003797
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003798 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3799 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3800 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3801 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
3802 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3803 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3804 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3805 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3806 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3807 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3808 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3809 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3810 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3811 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01003812
Christopher Fauletea159d62020-04-01 16:21:44 +02003813 { "htonl", sample_conv_htonl, 0, NULL, SMP_T_SINT, SMP_T_BIN },
3814 { "cut_crlf", sample_conv_cut_crlf, 0, NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet51fc9d12020-04-01 17:24:41 +02003815 { "ltrim", sample_conv_ltrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet568415a2020-04-01 17:24:47 +02003816 { "rtrim", sample_conv_rtrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02003817 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01003818}};
3819
Willy Tarreau0108d902018-11-25 19:14:37 +01003820INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);