blob: 1be9459930aa4e2fce67616b222305edea50023a [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 Tarreau7e2c6472012-10-29 20:44:36 +010019#include <types/global.h>
20
Willy Tarreauc7e42382012-08-24 19:22:53 +020021#include <common/chunk.h>
Willy Tarreau23ec4ca2014-07-15 20:15:37 +020022#include <common/hash.h>
Willy Tarreau35b51c62018-09-10 15:38:55 +020023#include <common/http.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010024#include <common/initcall.h>
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +010025#include <common/net_helper.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020026#include <common/standard.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020027#include <common/uri_auth.h>
Emeric Brun53d1a982014-04-30 18:21:37 +020028#include <common/base64.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020029
Willy Tarreau9fcb9842012-04-20 14:45:49 +020030#include <proto/arg.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020031#include <proto/auth.h>
32#include <proto/log.h>
33#include <proto/proxy.h>
Frédéric Lécaillefd95c622019-02-26 14:09:08 +010034#include <proto/protocol_buffers.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020035#include <proto/sample.h>
Willy Tarreau0851fd52019-12-17 10:07:25 +010036#include <proto/sink.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020037#include <proto/stick_table.h>
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +020038#include <proto/vars.h>
Emeric Brun107ca302010-01-04 16:16:05 +010039
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +020040#include <import/sha1.h>
Thierry FOURNIER01e09742016-12-26 11:46:11 +010041#include <import/xxhash.h>
42
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
1037 * to since they're made to cast mutiple 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;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001135 }
1136
1137 /* set a few default settings */
1138 px = p;
1139 pname = p->id;
1140
1141 switch (arg->type) {
1142 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001143 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001144 ha_alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1145 cur->file, cur->line,
1146 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001147 cfgerr++;
1148 continue;
1149 }
1150
1151 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001152 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001153
1154 if (sname) {
1155 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001156 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001157
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001158 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001159 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001160 ha_alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1161 cur->file, cur->line, pname,
1162 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001163 cfgerr++;
1164 break;
1165 }
1166 }
1167 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001168 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001169
1170 srv = findserver(px, sname);
1171 if (!srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001172 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",
1173 cur->file, cur->line, sname, pname,
1174 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001175 cfgerr++;
1176 break;
1177 }
1178
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001179 free(arg->data.str.area);
1180 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001181 arg->unresolved = 0;
1182 arg->data.srv = srv;
1183 break;
1184
1185 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001186 if (arg->data.str.data) {
1187 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001188 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001189 }
1190
1191 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001192 ha_alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1193 cur->file, cur->line, pname,
1194 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001195 cfgerr++;
1196 break;
1197 }
1198
1199 if (!(px->cap & PR_CAP_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001200 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",
1201 cur->file, cur->line, pname,
1202 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001203 cfgerr++;
1204 break;
1205 }
1206
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001207 free(arg->data.str.area);
1208 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001209 arg->unresolved = 0;
1210 arg->data.prx = px;
1211 break;
1212
1213 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001214 if (arg->data.str.data) {
1215 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001216 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001217 }
1218
1219 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001220 ha_alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1221 cur->file, cur->line, pname,
1222 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001223 cfgerr++;
1224 break;
1225 }
1226
1227 if (!(px->cap & PR_CAP_BE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001228 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",
1229 cur->file, cur->line, pname,
1230 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001231 cfgerr++;
1232 break;
1233 }
1234
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001235 free(arg->data.str.area);
1236 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001237 arg->unresolved = 0;
1238 arg->data.prx = px;
1239 break;
1240
1241 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001242 if (arg->data.str.data)
1243 stktname = arg->data.str.area;
1244 else
1245 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001246
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001247 t = stktable_find_by_name(stktname);
1248 if (!t) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001249 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 +01001250 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001251 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001252 cfgerr++;
1253 break;
1254 }
1255
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001256 if (!t->size) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001257 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 +01001258 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001259 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001260 cfgerr++;
1261 break;
1262 }
1263
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001264 if (t->proxy && (p->bind_proc & ~t->proxy->bind_proc)) {
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001265 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 +01001266 cur->file, cur->line, t->proxy->id, p->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01001267 cfgerr++;
1268 break;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001269 }
1270
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001271 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001272 p->next_stkt_ref = t->proxies_list;
1273 t->proxies_list = p;
1274 }
1275
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001276 free(arg->data.str.area);
1277 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001278 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001279 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001280 break;
1281
1282 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001283 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001284 ha_alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1285 cur->file, cur->line,
1286 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001287 cfgerr++;
1288 break;
1289 }
1290
1291 if (p->uri_auth && p->uri_auth->userlist &&
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001292 !strcmp(p->uri_auth->userlist->name, arg->data.str.area))
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001293 ul = p->uri_auth->userlist;
1294 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001295 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001296
1297 if (!ul) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001298 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 +02001299 cur->file, cur->line,
1300 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001301 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001302 cfgerr++;
1303 break;
1304 }
1305
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001306 free(arg->data.str.area);
1307 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001308 arg->unresolved = 0;
1309 arg->data.usr = ul;
1310 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001311
1312 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001313 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001314 ha_alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1315 cur->file, cur->line,
1316 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001317 cfgerr++;
1318 continue;
1319 }
1320
Willy Tarreau46947782015-01-19 19:00:58 +01001321 rflags = 0;
1322 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1323 err = NULL;
1324
Dragan Dosen26743032019-04-30 15:54:36 +02001325 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001326 ha_alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1327 cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001328 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001329 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
Willy Tarreau46947782015-01-19 19:00:58 +01001330 cfgerr++;
1331 continue;
1332 }
1333
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001334 free(arg->data.str.area);
1335 arg->data.str.area = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001336 arg->unresolved = 0;
1337 arg->data.reg = reg;
1338 break;
1339
1340
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001341 }
1342
1343 LIST_DEL(&cur->list);
1344 free(cur);
1345 } /* end of args processing */
1346
1347 return cfgerr;
1348}
1349
1350/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001351 * Process a fetch + format conversion as defined by the sample expression
1352 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001353 * not explicitly set to <smp_type>, but shall be compatible with it as
1354 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1355 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001356 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1357 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1358 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1359 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001360 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1361 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1362 * still hope to get it after waiting longer, and is not converted to string.
1363 * The possible output combinations are the following :
1364 *
1365 * return MAY_CHANGE FINAL Meaning for the sample
1366 * NULL * * Not present and will never be (eg: header)
1367 * smp 0 * Final value converted (eg: header)
1368 * smp 1 0 Not present yet, may appear later (eg: header)
1369 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001370 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001371struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001372 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001373 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001374{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001375 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001376
Willy Tarreau6c616e02014-06-25 16:56:41 +02001377 memset(smp, 0, sizeof(*smp));
1378
Willy Tarreau192252e2015-04-04 01:47:55 +02001379 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001380 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1381 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001382 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001383 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001384
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001385 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001386 return NULL;
1387
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001388 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001389 return NULL;
1390
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001391 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001392 return smp;
1393}
1394
Christopher Faulet476e5d02016-10-26 11:34:47 +02001395static void release_sample_arg(struct arg *p)
1396{
1397 struct arg *p_back = p;
1398
1399 if (!p)
1400 return;
1401
1402 while (p->type != ARGT_STOP) {
1403 if (p->type == ARGT_STR || p->unresolved) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001404 free(p->data.str.area);
1405 p->data.str.area = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001406 p->unresolved = 0;
1407 }
1408 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001409 regex_free(p->data.reg);
1410 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001411 }
1412 p++;
1413 }
1414
1415 if (p_back != empty_arg_list)
1416 free(p_back);
1417}
1418
1419void release_sample_expr(struct sample_expr *expr)
1420{
1421 struct sample_conv_expr *conv_expr, *conv_exprb;
1422
1423 if (!expr)
1424 return;
1425
1426 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list)
1427 release_sample_arg(conv_expr->arg_p);
1428 release_sample_arg(expr->arg_p);
1429 free(expr);
1430}
1431
Emeric Brun107ca302010-01-04 16:16:05 +01001432/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001433/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001434/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001435/*****************************************************************/
1436
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001437static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1438{
1439 int i;
1440 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001441 struct buffer *buf;
1442 struct sink *sink;
1443 struct ist line;
1444 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001445
Willy Tarreau0851fd52019-12-17 10:07:25 +01001446 buf = alloc_trash_chunk();
1447 if (!buf)
1448 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001449
Willy Tarreau0851fd52019-12-17 10:07:25 +01001450 sink = (struct sink *)arg_p[1].data.str.area;
1451 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001452
Willy Tarreau0851fd52019-12-17 10:07:25 +01001453 pfx = arg_p[0].data.str.area;
1454 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001455
Willy Tarreau0851fd52019-12-17 10:07:25 +01001456 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1457 if (!sample_casts[smp->data.type][SMP_T_STR])
1458 goto nocast;
1459
1460 /* Copy sample fetch. This puts the sample as const, the
1461 * cast will copy data if a transformation is required.
1462 */
1463 memcpy(&tmp, smp, sizeof(struct sample));
1464 tmp.flags = SMP_F_CONST;
1465
1466 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1467 goto nocast;
1468
1469 /* Display the displayable chars*. */
1470 b_putchr(buf, '<');
1471 for (i = 0; i < tmp.data.u.str.data; i++) {
Willy Tarreau90807112020-02-25 08:16:33 +01001472 if (isprint((unsigned char)tmp.data.u.str.area[i]))
Willy Tarreau0851fd52019-12-17 10:07:25 +01001473 b_putchr(buf, tmp.data.u.str.area[i]);
1474 else
1475 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001476 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001477 b_putchr(buf, '>');
1478
1479 done:
1480 line = ist2(buf->area, buf->data);
1481 sink_write(sink, &line, 1);
1482 end:
1483 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001484 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001485 nocast:
1486 chunk_appendf(buf, "(undisplayable)");
1487 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001488}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001489
1490// This function checks the "debug" converter's arguments.
1491static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1492 const char *file, int line, char **err)
1493{
1494 const char *name = "buf0";
1495 struct sink *sink = NULL;
1496
1497 if (args[0].type != ARGT_STR) {
1498 /* optional prefix */
1499 args[0].data.str.area = "";
1500 args[0].data.str.data = 0;
1501 }
1502
1503 if (args[1].type == ARGT_STR)
1504 name = args[1].data.str.area;
1505
1506 sink = sink_find(name);
1507 if (!sink) {
1508 memprintf(err, "No such sink '%s'", name);
1509 return 0;
1510 }
1511
1512 args[1].data.str.area = (char *)sink;
1513 args[1].data.str.data = 0; // that's not a string anymore
1514 return 1;
1515}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001516
Holger Just1bfc24b2017-05-06 00:56:53 +02001517static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1518{
Willy Tarreau83061a82018-07-13 11:56:34 +02001519 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001520 int bin_len;
1521
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001522 trash->data = 0;
1523 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1524 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001525 if (bin_len < 0)
1526 return 0;
1527
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001528 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001529 smp->data.u.str = *trash;
1530 smp->data.type = SMP_T_BIN;
1531 smp->flags &= ~SMP_F_CONST;
1532 return 1;
1533}
1534
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001535static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001536{
Willy Tarreau83061a82018-07-13 11:56:34 +02001537 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001538 int b64_len;
1539
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001540 trash->data = 0;
1541 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1542 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001543 if (b64_len < 0)
1544 return 0;
1545
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001546 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001547 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001548 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001549 smp->flags &= ~SMP_F_CONST;
1550 return 1;
1551}
1552
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001553static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1554{
1555 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001556 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001557
1558 memset(&ctx, 0, sizeof(ctx));
1559
1560 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001561 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1562 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001563
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001564 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001565 smp->data.u.str = *trash;
1566 smp->data.type = SMP_T_BIN;
1567 smp->flags &= ~SMP_F_CONST;
1568 return 1;
1569}
1570
Tim Duesterhusd4376302019-06-17 12:41:44 +02001571#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01001572static int smp_check_sha2(struct arg *args, struct sample_conv *conv,
1573 const char *file, int line, char **err)
1574{
1575 if (args[0].type == ARGT_STOP)
1576 return 1;
1577 if (args[0].type != ARGT_SINT) {
1578 memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]);
1579 return 0;
1580 }
1581
1582 switch (args[0].data.sint) {
1583 case 224:
1584 case 256:
1585 case 384:
1586 case 512:
1587 /* this is okay */
1588 return 1;
1589 default:
1590 memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint);
1591 return 0;
1592 }
1593}
1594
Tim Duesterhusd4376302019-06-17 12:41:44 +02001595static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
1596{
1597 struct buffer *trash = get_trash_chunk();
1598 int bits = 256;
1599 if (arg_p && arg_p->data.sint)
1600 bits = arg_p->data.sint;
1601
1602 switch (bits) {
1603 case 224: {
1604 SHA256_CTX ctx;
1605
1606 memset(&ctx, 0, sizeof(ctx));
1607
1608 SHA224_Init(&ctx);
1609 SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1610 SHA224_Final((unsigned char *) trash->area, &ctx);
1611 trash->data = SHA224_DIGEST_LENGTH;
1612 break;
1613 }
1614 case 256: {
1615 SHA256_CTX ctx;
1616
1617 memset(&ctx, 0, sizeof(ctx));
1618
1619 SHA256_Init(&ctx);
1620 SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1621 SHA256_Final((unsigned char *) trash->area, &ctx);
1622 trash->data = SHA256_DIGEST_LENGTH;
1623 break;
1624 }
1625 case 384: {
1626 SHA512_CTX ctx;
1627
1628 memset(&ctx, 0, sizeof(ctx));
1629
1630 SHA384_Init(&ctx);
1631 SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1632 SHA384_Final((unsigned char *) trash->area, &ctx);
1633 trash->data = SHA384_DIGEST_LENGTH;
1634 break;
1635 }
1636 case 512: {
1637 SHA512_CTX ctx;
1638
1639 memset(&ctx, 0, sizeof(ctx));
1640
1641 SHA512_Init(&ctx);
1642 SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1643 SHA512_Final((unsigned char *) trash->area, &ctx);
1644 trash->data = SHA512_DIGEST_LENGTH;
1645 break;
1646 }
1647 default:
1648 return 0;
1649 }
1650
1651 smp->data.u.str = *trash;
1652 smp->data.type = SMP_T_BIN;
1653 smp->flags &= ~SMP_F_CONST;
1654 return 1;
1655}
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001656
1657#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
1658static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
1659{
1660 switch (arg->type) {
1661 case ARGT_STR:
1662 smp->data.type = SMP_T_STR;
1663 smp->data.u.str = arg->data.str;
1664 return 1;
1665 case ARGT_VAR:
1666 if (!vars_get_by_desc(&arg->data.var, smp))
1667 return 0;
1668 if (!sample_casts[smp->data.type][SMP_T_STR])
1669 return 0;
1670 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
1671 return 0;
1672 return 1;
1673 default:
1674 return 0;
1675 }
1676}
1677
1678static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
1679 const char *file, int line, char **err)
1680{
1681 switch(args[0].data.sint) {
1682 case 128:
1683 case 192:
1684 case 256:
1685 break;
1686 default:
1687 memprintf(err, "key size must be 128, 192 or 256 (bits).");
1688 return 0;
1689 }
1690 /* Try to decode a variable. */
1691 vars_check_arg(&args[1], NULL);
1692 vars_check_arg(&args[2], NULL);
1693 vars_check_arg(&args[3], NULL);
1694 return 1;
1695}
1696
1697/* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
1698static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
1699{
1700 struct sample nonce, key, aead_tag;
1701 struct buffer *smp_trash, *smp_trash_alloc;
1702 EVP_CIPHER_CTX *ctx;
1703 int dec_size, ret;
1704
1705 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
1706 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
1707 return 0;
1708
1709 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
1710 if (!sample_conv_var2smp_str(&arg_p[2], &key))
1711 return 0;
1712
1713 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
1714 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
1715 return 0;
1716
1717 smp_trash = get_trash_chunk();
1718 smp_trash_alloc = alloc_trash_chunk();
1719 if (!smp_trash_alloc)
1720 return 0;
1721
1722 ctx = EVP_CIPHER_CTX_new();
1723
1724 if (!ctx)
1725 goto err;
1726
1727 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
1728 if (dec_size < 0)
1729 goto err;
1730 smp_trash->data = dec_size;
1731
1732 /* Set cipher type and mode */
1733 switch(arg_p[0].data.sint) {
1734 case 128:
1735 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
1736 break;
1737 case 192:
1738 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
1739 break;
1740 case 256:
1741 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
1742 break;
1743 }
1744
1745 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL);
1746
1747 /* Initialise IV */
1748 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area))
1749 goto err;
1750
1751 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
1752 if (dec_size < 0)
1753 goto err;
1754 smp_trash->data = dec_size;
1755
1756 /* Initialise key */
1757 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL))
1758 goto err;
1759
1760 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
1761 (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data))
1762 goto err;
1763
1764 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
1765 if (dec_size < 0)
1766 goto err;
1767 smp_trash_alloc->data = dec_size;
1768 dec_size = smp_trash->data;
1769
1770 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area);
1771 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
1772
1773 if (ret <= 0)
1774 goto err;
1775
1776 smp->data.u.str.data = dec_size + smp_trash->data;
1777 smp->data.u.str.area = smp_trash->area;
1778 smp->data.type = SMP_T_BIN;
1779 smp->flags &= ~SMP_F_CONST;
1780 free_trash_chunk(smp_trash_alloc);
1781 return 1;
1782
1783err:
1784 free_trash_chunk(smp_trash_alloc);
1785 return 0;
1786}
1787#endif /* HA_OPENSSL_VERSION_NUMBER */
1788
1789#endif /* USE_OPENSSL */
Tim Duesterhusd4376302019-06-17 12:41:44 +02001790
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001791static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001792{
Willy Tarreau83061a82018-07-13 11:56:34 +02001793 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001794 unsigned char c;
1795 int ptr = 0;
1796
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001797 trash->data = 0;
1798 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
1799 c = smp->data.u.str.area[ptr++];
1800 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1801 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001802 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001803 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001804 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001805 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001806 return 1;
1807}
1808
Dragan Dosen3f957b22017-10-24 09:27:34 +02001809static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1810{
1811 long long int n = 0;
1812 int i, c;
1813
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001814 for (i = 0; i < smp->data.u.str.data; i++) {
1815 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02001816 return 0;
1817 n = (n << 4) + c;
1818 }
1819
1820 smp->data.u.sint = n;
1821 smp->data.type = SMP_T_SINT;
1822 smp->flags &= ~SMP_F_CONST;
1823 return 1;
1824}
1825
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001826/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001827static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001828{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001829 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
1830 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001831 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001832 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001833 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001834 return 1;
1835}
1836
Willy Tarreau60a2ee72017-12-15 07:13:48 +01001837static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001838{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001839 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001840 smp->data.u.sint = i;
1841 smp->data.type = SMP_T_SINT;
1842 return 1;
1843}
1844
1845
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001846static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001847{
1848 int i;
1849
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001850 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001851 return 0;
1852
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001853 for (i = 0; i < smp->data.u.str.data; i++) {
1854 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
1855 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001856 }
1857 return 1;
1858}
1859
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001860static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001861{
1862 int i;
1863
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001864 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001865 return 0;
1866
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001867 for (i = 0; i < smp->data.u.str.data; i++) {
1868 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
1869 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001870 }
1871 return 1;
1872}
1873
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001874/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
1875static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001876{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001877 /* Attempt to convert to IPv4 to apply the correct mask. */
1878 c_ipv62ip(smp);
1879
1880 if (smp->data.type == SMP_T_IPV4) {
1881 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
1882 smp->data.type = SMP_T_IPV4;
1883 }
1884 else if (smp->data.type == SMP_T_IPV6) {
1885 /* IPv6 cannot be converted without an IPv6 mask. */
1886 if (args[1].type != ARGT_IPV6)
1887 return 0;
1888
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +01001889 write_u64(&smp->data.u.ipv6.s6_addr[0],
1890 read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0]));
1891 write_u64(&smp->data.u.ipv6.s6_addr[8],
1892 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 +01001893 smp->data.type = SMP_T_IPV6;
1894 }
1895
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001896 return 1;
1897}
1898
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001899/* takes an UINT value on input supposed to represent the time since EPOCH,
1900 * adds an optional offset found in args[1] and emits a string representing
1901 * the local time in the format specified in args[1] using strftime().
1902 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001903static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001904{
Willy Tarreau83061a82018-07-13 11:56:34 +02001905 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001906 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001907 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001908 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001909
1910 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001911 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001912 curr_date += args[1].data.sint;
1913
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001914 tm = localtime(&curr_date);
1915 if (!tm)
1916 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001917 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001918 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1919 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001920 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001921 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001922 return 1;
1923}
1924
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001925/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001926static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001927{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001928 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
1929 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001930 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001931 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001932 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001933 return 1;
1934}
1935
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001936/* takes an UINT value on input supposed to represent the time since EPOCH,
1937 * adds an optional offset found in args[1] and emits a string representing
1938 * the UTC date in the format specified in args[1] using strftime().
1939 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001940static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001941{
Willy Tarreau83061a82018-07-13 11:56:34 +02001942 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001943 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001944 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001945 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001946
1947 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001948 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001949 curr_date += args[1].data.sint;
1950
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001951 tm = gmtime(&curr_date);
1952 if (!tm)
1953 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001954 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001955 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1956 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001957 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001958 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001959 return 1;
1960}
1961
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001962/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001963static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001964{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001965 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
1966 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001967 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001968 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001969 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001970 return 1;
1971}
1972
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001973/* hashes the binary input into a 32-bit unsigned int using xxh.
1974 * The seed of the hash defaults to 0 but can be changd in argument 1.
1975 */
1976static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
1977{
1978 unsigned int seed;
1979
1980 if (arg_p && arg_p->data.sint)
1981 seed = arg_p->data.sint;
1982 else
1983 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001984 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
1985 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001986 smp->data.type = SMP_T_SINT;
1987 return 1;
1988}
1989
1990/* hashes the binary input into a 64-bit unsigned int using xxh.
1991 * In fact, the function returns a 64 bit unsigned, but the sample
1992 * storage of haproxy only proposes 64-bits signed, so the value is
1993 * cast as signed. This cast doesn't impact the hash repartition.
1994 * The seed of the hash defaults to 0 but can be changd in argument 1.
1995 */
1996static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
1997{
1998 unsigned long long int seed;
1999
2000 if (arg_p && arg_p->data.sint)
2001 seed = (unsigned long long int)arg_p->data.sint;
2002 else
2003 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002004 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
2005 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002006 smp->data.type = SMP_T_SINT;
2007 return 1;
2008}
2009
Willy Tarreau80599772015-01-20 19:35:24 +01002010/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002011static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01002012{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002013 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
2014 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002015 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002016 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002017 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01002018 return 1;
2019}
2020
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002021/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
2022static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
2023{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002024 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
2025 smp->data.u.str.data);
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002026 if (arg_p && arg_p->data.sint)
2027 smp->data.u.sint = full_hash(smp->data.u.sint);
2028 smp->data.type = SMP_T_SINT;
2029 return 1;
2030}
2031
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002032/* This function escape special json characters. The returned string can be
2033 * safely set between two '"' and used as json string. The json string is
2034 * defined like this:
2035 *
2036 * any Unicode character except '"' or '\' or control character
2037 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
2038 *
2039 * The enum input_type contain all the allowed mode for decoding the input
2040 * string.
2041 */
2042enum input_type {
2043 IT_ASCII = 0,
2044 IT_UTF8,
2045 IT_UTF8S,
2046 IT_UTF8P,
2047 IT_UTF8PS,
2048};
2049static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
2050 const char *file, int line, char **err)
2051{
2052 if (!arg) {
2053 memprintf(err, "Unexpected empty arg list");
2054 return 0;
2055 }
2056
2057 if (arg->type != ARGT_STR) {
2058 memprintf(err, "Unexpected arg type");
2059 return 0;
2060 }
2061
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002062 if (strcmp(arg->data.str.area, "") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002063 arg->type = ARGT_SINT;
2064 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002065 return 1;
2066 }
2067
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002068 else if (strcmp(arg->data.str.area, "ascii") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002069 arg->type = ARGT_SINT;
2070 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002071 return 1;
2072 }
2073
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002074 else if (strcmp(arg->data.str.area, "utf8") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002075 arg->type = ARGT_SINT;
2076 arg->data.sint = IT_UTF8;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002077 return 1;
2078 }
2079
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002080 else if (strcmp(arg->data.str.area, "utf8s") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002081 arg->type = ARGT_SINT;
2082 arg->data.sint = IT_UTF8S;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002083 return 1;
2084 }
2085
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002086 else if (strcmp(arg->data.str.area, "utf8p") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002087 arg->type = ARGT_SINT;
2088 arg->data.sint = IT_UTF8P;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002089 return 1;
2090 }
2091
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002092 else if (strcmp(arg->data.str.area, "utf8ps") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002093 arg->type = ARGT_SINT;
2094 arg->data.sint = IT_UTF8PS;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002095 return 1;
2096 }
2097
Thierry FOURNIER / OZON.IOa69c9122016-11-23 01:13:57 +01002098 memprintf(err, "Unexpected input code type. "
2099 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002100 return 0;
2101}
2102
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002103static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002104{
Willy Tarreau83061a82018-07-13 11:56:34 +02002105 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002106 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
2107 const char *str;
2108 int len;
2109 enum input_type input_type = IT_ASCII;
2110 unsigned int c;
2111 unsigned int ret;
2112 char *p;
2113
2114 if (arg_p)
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002115 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002116
2117 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002118 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002119
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002120 p = smp->data.u.str.area;
2121 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002122
2123 if (input_type == IT_ASCII) {
2124 /* Read input as ASCII. */
2125 c = *(unsigned char *)p;
2126 p++;
2127 }
2128 else {
2129 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002130 ret = utf8_next(p,
2131 smp->data.u.str.data - ( p - smp->data.u.str.area),
2132 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002133 p += utf8_return_length(ret);
2134
2135 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2136 return 0;
2137 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2138 continue;
2139 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2140 return 0;
2141 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2142 continue;
2143
2144 /* Check too big values. */
2145 if ((unsigned int)c > 0xffff) {
2146 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2147 return 0;
2148 continue;
2149 }
2150 }
2151
2152 /* Convert character. */
2153 if (c == '"') {
2154 len = 2;
2155 str = "\\\"";
2156 }
2157 else if (c == '\\') {
2158 len = 2;
2159 str = "\\\\";
2160 }
2161 else if (c == '/') {
2162 len = 2;
2163 str = "\\/";
2164 }
2165 else if (c == '\b') {
2166 len = 2;
2167 str = "\\b";
2168 }
2169 else if (c == '\f') {
2170 len = 2;
2171 str = "\\f";
2172 }
2173 else if (c == '\r') {
2174 len = 2;
2175 str = "\\r";
2176 }
2177 else if (c == '\n') {
2178 len = 2;
2179 str = "\\n";
2180 }
2181 else if (c == '\t') {
2182 len = 2;
2183 str = "\\t";
2184 }
Willy Tarreau90807112020-02-25 08:16:33 +01002185 else if (c > 0xff || !isprint((unsigned char)c)) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002186 /* isprint generate a segfault if c is too big. The man says that
2187 * c must have the value of an unsigned char or EOF.
2188 */
2189 len = 6;
2190 _str[0] = '\\';
2191 _str[1] = 'u';
2192 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2193 str = _str;
2194 }
2195 else {
2196 len = 1;
Willy Tarreau5715da22020-02-25 08:37:37 +01002197 _str[0] = c;
2198 str = _str;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002199 }
2200
2201 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002202 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002203 return 0;
2204
2205 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002206 memcpy(temp->area + temp->data, str, len);
2207 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002208 }
2209
2210 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002211 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002212 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002213
2214 return 1;
2215}
2216
Emeric Brun54c4ac82014-11-03 15:32:43 +01002217/* This sample function is designed to extract some bytes from an input buffer.
2218 * First arg is the offset.
2219 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002220static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002221{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002222 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2223 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002224 return 1;
2225 }
2226
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002227 if (smp->data.u.str.size)
2228 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002229 smp->data.u.str.data -= arg_p[0].data.sint;
2230 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002231
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002232 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2233 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002234
2235 return 1;
2236}
2237
Emeric Brunf399b0d2014-11-03 17:07:03 +01002238static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2239 const char *file, int line, char **err)
2240{
2241 struct arg *arg = args;
2242
2243 if (!arg) {
2244 memprintf(err, "Unexpected empty arg list");
2245 return 0;
2246 }
2247
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002248 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002249 memprintf(err, "Unexpected arg type");
2250 return 0;
2251 }
2252
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002253 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002254 memprintf(err, "Unexpected value 0 for index");
2255 return 0;
2256 }
2257
2258 arg++;
2259
2260 if (arg->type != ARGT_STR) {
2261 memprintf(err, "Unexpected arg type");
2262 return 0;
2263 }
2264
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002265 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002266 memprintf(err, "Empty separators list");
2267 return 0;
2268 }
2269
2270 return 1;
2271}
2272
2273/* This sample function is designed to a return selected part of a string (field).
2274 * First arg is the index of the field (start at 1)
2275 * Second arg is a char list of separators (type string)
2276 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002277static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002278{
Marcin Deranek9631a282018-04-16 14:30:46 +02002279 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002280 char *start, *end;
2281 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002282 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002283
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002284 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002285 return 0;
2286
Marcin Deranek9631a282018-04-16 14:30:46 +02002287 if (arg_p[0].data.sint < 0) {
2288 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002289 end = start = smp->data.u.str.area + smp->data.u.str.data;
2290 while (start > smp->data.u.str.area) {
2291 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2292 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002293 if (field == arg_p[0].data.sint) {
2294 if (count == 1)
2295 goto found;
2296 else if (count > 1)
2297 count--;
2298 } else {
2299 end = start-1;
2300 field--;
2301 }
2302 break;
2303 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002304 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002305 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002306 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002307 } else {
2308 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002309 end = start = smp->data.u.str.area;
2310 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2311 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2312 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002313 if (field == arg_p[0].data.sint) {
2314 if (count == 1)
2315 goto found;
2316 else if (count > 1)
2317 count--;
2318 } else {
2319 start = end+1;
2320 field++;
2321 }
2322 break;
2323 }
2324 }
2325 end++;
2326 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002327 }
2328
2329 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002330 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002331 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002332 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002333 }
2334found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002335 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002336 /* If ret string is len 0, no need to
2337 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002338 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002339 return 1;
2340
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002341 smp->data.u.str.area = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002342
2343 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002344 Note: smp->data.u.str.size cannot be set to 0 */
2345 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002346 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002347
2348 return 1;
2349}
2350
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002351/* This sample function is designed to return a word from a string.
2352 * First arg is the index of the word (start at 1)
2353 * Second arg is a char list of words separators (type string)
2354 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002355static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002356{
Marcin Deranek9631a282018-04-16 14:30:46 +02002357 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002358 char *start, *end;
2359 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002360 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002361
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002362 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002363 return 0;
2364
2365 word = 0;
2366 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002367 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002368 end = start = smp->data.u.str.area + smp->data.u.str.data;
2369 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002370 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002371 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2372 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002373 issep = 1;
2374 break;
2375 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002376 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002377 if (!inword) {
2378 if (!issep) {
2379 if (word != arg_p[0].data.sint) {
2380 word--;
2381 end = start;
2382 }
2383 inword = 1;
2384 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002385 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002386 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002387 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002388 if (count == 1)
2389 goto found;
2390 else if (count > 1)
2391 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002392 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002393 inword = 0;
2394 }
2395 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002396 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002397 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002398 end = start = smp->data.u.str.area;
2399 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002400 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002401 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2402 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002403 issep = 1;
2404 break;
2405 }
2406 }
2407 if (!inword) {
2408 if (!issep) {
2409 if (word != arg_p[0].data.sint) {
2410 word++;
2411 start = end;
2412 }
2413 inword = 1;
2414 }
2415 }
2416 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002417 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002418 if (count == 1)
2419 goto found;
2420 else if (count > 1)
2421 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002422 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002423 inword = 0;
2424 }
2425 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002426 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002427 }
2428
2429 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002430 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002431 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002432 return 1;
2433 }
2434found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002435 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002436 /* If ret string is len 0, no need to
2437 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002438 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002439 return 1;
2440
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002441 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002442
2443 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002444 Note: smp->data.u.str.size cannot be set to 0 */
2445 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002446 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002447
2448 return 1;
2449}
2450
Willy Tarreau7eda8492015-01-20 19:47:06 +01002451static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2452 const char *file, int line, char **err)
2453{
2454 struct arg *arg = args;
2455 char *p;
2456 int len;
2457
2458 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2459 arg[0].type_flags = 0;
2460
2461 if (arg[2].type != ARGT_STR)
2462 return 1;
2463
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002464 p = arg[2].data.str.area;
2465 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002466 while (len) {
2467 if (*p == 'i') {
2468 arg[0].type_flags |= ARGF_REG_ICASE;
2469 }
2470 else if (*p == 'g') {
2471 arg[0].type_flags |= ARGF_REG_GLOB;
2472 }
2473 else {
2474 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2475 return 0;
2476 }
2477 p++;
2478 len--;
2479 }
2480 return 1;
2481}
2482
2483/* This sample function is designed to do the equivalent of s/match/replace/ on
2484 * the input string. It applies a regex and restarts from the last matched
2485 * location until nothing matches anymore. First arg is the regex to apply to
2486 * the input string, second arg is the replacement expression.
2487 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002488static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002489{
2490 char *start, *end;
2491 struct my_regex *reg = arg_p[0].data.reg;
2492 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002493 struct buffer *trash = get_trash_chunk();
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002494 struct buffer *output;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002495 int flag, max;
2496 int found;
2497
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002498 start = smp->data.u.str.area;
2499 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002500
2501 flag = 0;
2502 while (1) {
2503 /* check for last round which is used to copy remaining parts
2504 * when not running in global replacement mode.
2505 */
2506 found = 0;
2507 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2508 /* Note: we can have start == end on empty strings or at the end */
2509 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2510 }
2511
2512 if (!found)
2513 pmatch[0].rm_so = end - start;
2514
2515 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002516 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002517 if (max && pmatch[0].rm_so > 0) {
2518 if (max > pmatch[0].rm_so)
2519 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002520 memcpy(trash->area + trash->data, start, max);
2521 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002522 }
2523
2524 if (!found)
2525 break;
2526
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002527 output = alloc_trash_chunk();
Willy Tarreau23997da2020-02-18 14:27:44 +01002528 if (!output)
2529 break;
2530
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002531 output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2532
Willy Tarreau7eda8492015-01-20 19:47:06 +01002533 /* replace the matching part */
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002534 max = output->size - output->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002535 if (max) {
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002536 if (max > output->data)
2537 max = output->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002538 memcpy(trash->area + trash->data,
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002539 output->area, max);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002540 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002541 }
2542
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002543 free_trash_chunk(output);
2544
Willy Tarreau7eda8492015-01-20 19:47:06 +01002545 /* stop here if we're done with this string */
2546 if (start >= end)
2547 break;
2548
2549 /* We have a special case for matches of length 0 (eg: "x*y*").
2550 * These ones are considered to match in front of a character,
2551 * so we have to copy that character and skip to the next one.
2552 */
2553 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002554 if (trash->data < trash->size)
2555 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002556 pmatch[0].rm_eo++;
2557 }
2558
2559 start += pmatch[0].rm_eo;
2560 flag |= REG_NOTBOL;
2561 }
2562
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002563 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002564 return 1;
2565}
2566
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002567/* This function check an operator entry. It expects a string.
2568 * The string can be an integer or a variable name.
2569 */
2570static int check_operator(struct arg *args, struct sample_conv *conv,
2571 const char *file, int line, char **err)
2572{
2573 const char *str;
2574 const char *end;
2575
2576 /* Try to decode a variable. */
2577 if (vars_check_arg(&args[0], NULL))
2578 return 1;
2579
2580 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002581 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002582 end = str + strlen(str);
2583 args[0].data.sint = read_int64(&str, end);
2584 if (*str != '\0') {
2585 memprintf(err, "expects an integer or a variable name");
2586 return 0;
2587 }
2588 args[0].type = ARGT_SINT;
2589 return 1;
2590}
2591
Willy Tarreau6204cd92016-03-10 16:33:04 +01002592/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002593 * If the arg contain an integer, the integer is returned in the
2594 * sample. If the arg contains a variable descriptor, it returns the
2595 * variable value.
2596 *
2597 * This function returns 0 if an error occurs, otherwise it returns 1.
2598 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002599static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002600{
2601 switch (arg->type) {
2602 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002603 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002604 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002605 return 1;
2606 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002607 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002608 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002609 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002610 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002611 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002612 return 0;
2613 return 1;
2614 default:
2615 return 0;
2616 }
2617}
2618
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002619/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002620 * result.
2621 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002622static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002623{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002624 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002625 return 1;
2626}
2627
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002628/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002629 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002630 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002631static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002632{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002633 struct sample tmp;
2634
Willy Tarreau7560dd42016-03-10 16:28:58 +01002635 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002636 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002637 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002638 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002639 return 1;
2640}
2641
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002642/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002643 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002644 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002645static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002646{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002647 struct sample tmp;
2648
Willy Tarreau7560dd42016-03-10 16:28:58 +01002649 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002650 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002651 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002652 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002653 return 1;
2654}
2655
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002656/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002657 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002658 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002659static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002660{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002661 struct sample tmp;
2662
Willy Tarreau7560dd42016-03-10 16:28:58 +01002663 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002664 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002665 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002666 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002667 return 1;
2668}
2669
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002670static inline long long int arith_add(long long int a, long long int b)
2671{
2672 /* Prevent overflow and makes capped calculus.
2673 * We must ensure that the check calculus doesn't
2674 * exceed the signed 64 bits limits.
2675 *
2676 * +----------+----------+
2677 * | a<0 | a>=0 |
2678 * +------+----------+----------+
2679 * | b<0 | MIN-a>b | no check |
2680 * +------+----------+----------+
2681 * | b>=0 | no check | MAX-a<b |
2682 * +------+----------+----------+
2683 */
2684 if ((a ^ b) >= 0) {
2685 /* signs are differents. */
2686 if (a < 0) {
2687 if (LLONG_MIN - a > b)
2688 return LLONG_MIN;
2689 }
2690 if (LLONG_MAX - a < b)
2691 return LLONG_MAX;
2692 }
2693 return a + b;
2694}
2695
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002696/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002697 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002698 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002699static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002700{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002701 struct sample tmp;
2702
Willy Tarreau7560dd42016-03-10 16:28:58 +01002703 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002704 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002705 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002706 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002707 return 1;
2708}
2709
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002710/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002711 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002712 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002713static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002714 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002715{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002716 struct sample tmp;
2717
Willy Tarreau7560dd42016-03-10 16:28:58 +01002718 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002719 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002720 return 0;
2721
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002722 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2723 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2724 * of -LLONG_MIN and correct the result.
2725 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002726 if (tmp.data.u.sint == LLONG_MIN) {
2727 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2728 if (smp->data.u.sint < LLONG_MAX)
2729 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002730 return 1;
2731 }
2732
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002733 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002734 * the second operand.
2735 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002736 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002737 return 1;
2738}
2739
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002740/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002741 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002742 * If the result makes an overflow, then the largest possible quantity is
2743 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002744 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002745static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002746 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002747{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002748 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002749 long long int c;
2750
Willy Tarreau7560dd42016-03-10 16:28:58 +01002751 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002752 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002753 return 0;
2754
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002755 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002756 if (!smp->data.u.sint || !tmp.data.u.sint) {
2757 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002758 return 1;
2759 }
2760
2761 /* The multiply between LLONG_MIN and -1 returns a
2762 * "floting point exception".
2763 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002764 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2765 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002766 return 1;
2767 }
2768
2769 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002770 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002771
2772 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002773 if (smp->data.u.sint != c / tmp.data.u.sint) {
2774 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2775 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002776 return 1;
2777 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002778 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002779 return 1;
2780 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002781 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002782 return 1;
2783}
2784
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002785/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002786 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002787 * If arg_p makes the result overflow, then the largest possible quantity is
2788 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002789 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002790static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002791 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002792{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002793 struct sample tmp;
2794
Willy Tarreau7560dd42016-03-10 16:28:58 +01002795 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002796 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002797 return 0;
2798
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002799 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002800 /* The divide between LLONG_MIN and -1 returns a
2801 * "floting point exception".
2802 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002803 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2804 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002805 return 1;
2806 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002807 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002808 return 1;
2809 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002810 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002811 return 1;
2812}
2813
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002814/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002815 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002816 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002817 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002818static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002819 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002820{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002821 struct sample tmp;
2822
Willy Tarreau7560dd42016-03-10 16:28:58 +01002823 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002824 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002825 return 0;
2826
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002827 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002828 /* The divide between LLONG_MIN and -1 returns a
2829 * "floting point exception".
2830 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002831 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2832 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002833 return 1;
2834 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002835 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002836 return 1;
2837 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002838 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002839 return 1;
2840}
2841
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002842/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002843 * result.
2844 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002845static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002846 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002847{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002848 if (smp->data.u.sint == LLONG_MIN)
2849 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002850 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002851 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002852 return 1;
2853}
2854
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002855/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002856 * false. The output is a BOOL.
2857 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002858static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002859 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002860{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002861 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002862 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002863 return 1;
2864}
2865
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002866/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002867 * truee. The output is a BOOL.
2868 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002869static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002870 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002871{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002872 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002873 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002874 return 1;
2875}
2876
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002877/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002878 * The output is a BOOL.
2879 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002880static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002881 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002882{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002883 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002884 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002885 return 1;
2886}
2887
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002888/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002889 * The output is a BOOL.
2890 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002891static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002892 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002893{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002894 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002895 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002896 return 1;
2897}
2898
Willy Tarreau280f42b2018-02-19 15:34:12 +01002899/* appends an optional const string, an optional variable contents and another
2900 * optional const string to an existing string.
2901 */
2902static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
2903{
Willy Tarreau83061a82018-07-13 11:56:34 +02002904 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002905 struct sample tmp;
2906 int max;
2907
2908 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002909 trash->data = smp->data.u.str.data;
2910 if (trash->data > trash->size - 1)
2911 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002912
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002913 memcpy(trash->area, smp->data.u.str.area, trash->data);
2914 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002915
2916 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002917 max = arg_p[0].data.str.data;
2918 if (max > trash->size - 1 - trash->data)
2919 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002920
2921 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002922 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
2923 trash->data += max;
2924 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002925 }
2926
2927 /* append second string (variable) if it's found and we can turn it
2928 * into a string.
2929 */
2930 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2931 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
2932 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
2933 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
2934
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002935 max = tmp.data.u.str.data;
2936 if (max > trash->size - 1 - trash->data)
2937 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002938
2939 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002940 memcpy(trash->area + trash->data, tmp.data.u.str.area,
2941 max);
2942 trash->data += max;
2943 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002944 }
2945 }
2946
2947 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002948 max = arg_p[2].data.str.data;
2949 if (max > trash->size - 1 - trash->data)
2950 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002951
2952 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002953 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
2954 trash->data += max;
2955 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002956 }
2957
2958 smp->data.u.str = *trash;
2959 smp->data.type = SMP_T_STR;
2960 return 1;
2961}
2962
2963/* This function checks the "concat" converter's arguments and extracts the
2964 * variable name and its scope.
2965 */
2966static int smp_check_concat(struct arg *args, struct sample_conv *conv,
2967 const char *file, int line, char **err)
2968{
2969 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002970 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
2971 memprintf(err, "failed to register variable name '%s'",
2972 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01002973 return 0;
2974 }
2975 return 1;
2976}
2977
Tim Duesterhusca097c12018-04-27 21:18:45 +02002978/* compares string with a variable containing a string. Return value
2979 * is compatible with strcmp(3)'s return value.
2980 */
2981static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
2982{
2983 struct sample tmp;
2984 int max, result;
2985
2986 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2987 if (arg_p[0].type != ARGT_VAR)
2988 return 0;
2989 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
2990 return 0;
2991 if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp))
2992 return 0;
2993
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002994 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
2995 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002996 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002997 if (smp->data.u.str.data != tmp.data.u.str.data) {
2998 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02002999 result = -1;
3000 }
3001 else {
3002 result = 1;
3003 }
3004 }
3005 }
3006
3007 smp->data.u.sint = result;
3008 smp->data.type = SMP_T_SINT;
3009 return 1;
3010}
3011
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003012#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
3013#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
3014#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
3015
3016/*
3017 * Extract the field value of an input binary sample. Takes a mandatory argument:
3018 * the protocol buffers field identifier (dotted notation) internally represented
3019 * as an array of unsigned integers and its size.
3020 * Return 1 if the field was found, 0 if not.
3021 */
3022static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
3023{
3024 unsigned char *pos;
3025 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003026
3027 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003028 grpc_left = smp->data.u.str.data;
3029
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01003030 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003031 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003032
3033 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
3034
3035 pos += GRPC_MSG_HEADER_SZ;
3036 grpc_left -= GRPC_MSG_HEADER_SZ;
3037
3038 if (grpc_left < left)
3039 return 0;
3040
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01003041 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
3042 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003043
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003044 grpc_left -= grpc_msg_len;
3045 }
3046
3047 return 0;
3048}
3049
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003050static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
3051{
3052 unsigned char *pos;
3053 size_t left;
3054
3055 pos = (unsigned char *)smp->data.u.str.area;
3056 left = smp->data.u.str.data;
3057
3058 return protobuf_field_lookup(arg_p, smp, &pos, &left);
3059}
3060
3061static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
3062 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003063{
3064 if (!args[1].type) {
3065 args[1].type = ARGT_SINT;
3066 args[1].data.sint = PBUF_T_BINARY;
3067 }
3068 else {
3069 int pbuf_type;
3070
3071 pbuf_type = protobuf_type(args[1].data.str.area);
3072 if (pbuf_type == -1) {
3073 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
3074 return 0;
3075 }
3076
3077 args[1].type = ARGT_SINT;
3078 args[1].data.sint = pbuf_type;
3079 }
3080
3081 return 1;
3082}
3083
Tim Duesterhusca097c12018-04-27 21:18:45 +02003084/* This function checks the "strcmp" converter's arguments and extracts the
3085 * variable name and its scope.
3086 */
3087static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
3088 const char *file, int line, char **err)
3089{
3090 /* Try to decode a variable. */
3091 if (vars_check_arg(&args[0], NULL))
3092 return 1;
3093
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003094 memprintf(err, "failed to register variable name '%s'",
3095 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003096 return 0;
3097}
3098
Christopher Faulet4ccc12f2020-04-01 09:08:32 +02003099/**/
3100static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private)
3101{
3102 struct buffer *tmp;
3103 uint32_t n;
3104
3105 n = htonl((uint32_t)smp->data.u.sint);
3106 tmp = get_trash_chunk();
3107
3108 memcpy(b_head(tmp), &n, 4);
3109 b_add(tmp, 4);
3110
3111 smp->data.u.str = *tmp;
3112 smp->data.type = SMP_T_BIN;
3113 return 1;
3114}
3115
Christopher Fauletea159d62020-04-01 16:21:44 +02003116/**/
3117static int sample_conv_cut_crlf(const struct arg *arg_p, struct sample *smp, void *private)
3118{
3119 char *p;
3120 size_t l;
3121
3122 p = smp->data.u.str.area;
3123 for (l = 0; l < smp->data.u.str.data; l++) {
3124 if (*(p+l) == '\r' || *(p+l) == '\n')
3125 break;
3126 }
3127 smp->data.u.str.data = l;
3128 return 1;
3129}
3130
Christopher Faulet51fc9d12020-04-01 17:24:41 +02003131/**/
3132static int sample_conv_ltrim(const struct arg *arg_p, struct sample *smp, void *private)
3133{
3134 char *delimiters, *p;
3135 size_t dlen, l;
3136
3137 delimiters = arg_p[0].data.str.area;
3138 dlen = arg_p[0].data.str.data;
3139
3140 l = smp->data.u.str.data;
3141 p = smp->data.u.str.area;
3142 while (l && memchr(delimiters, *p, dlen) != NULL) {
3143 p++;
3144 l--;
3145 }
3146
3147 smp->data.u.str.area = p;
3148 smp->data.u.str.data = l;
3149 return 1;
3150}
3151
Christopher Faulet568415a2020-04-01 17:24:47 +02003152/**/
3153static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *private)
3154{
3155 char *delimiters, *p;
3156 size_t dlen, l;
3157
3158 delimiters = arg_p[0].data.str.area;
3159 dlen = arg_p[0].data.str.data;
3160
3161 l = smp->data.u.str.data;
3162 p = smp->data.u.str.area + l - 1;
3163 while (l && memchr(delimiters, *p, dlen) != NULL) {
3164 p--;
3165 l--;
3166 }
3167
3168 smp->data.u.str.data = l;
3169 return 1;
3170}
3171
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003172/************************************************************************/
3173/* All supported sample fetch functions must be declared here */
3174/************************************************************************/
3175
3176/* force TRUE to be returned at the fetch level */
3177static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003178smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003179{
Willy Tarreau280f42b2018-02-19 15:34:12 +01003180 if (!smp_make_rw(smp))
3181 return 0;
3182
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003183 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003184 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003185 return 1;
3186}
3187
3188/* force FALSE to be returned at the fetch level */
3189static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003190smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003191{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003192 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003193 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003194 return 1;
3195}
3196
3197/* retrieve environment variable $1 as a string */
3198static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003199smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003200{
3201 char *env;
3202
3203 if (!args || args[0].type != ARGT_STR)
3204 return 0;
3205
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003206 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003207 if (!env)
3208 return 0;
3209
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003210 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003211 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003212 smp->data.u.str.area = env;
3213 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003214 return 1;
3215}
3216
Damien Claisseae6f1252019-10-30 15:57:28 +00003217/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3218 * optional string representing the unit of the result: "s" for seconds, "ms" for
3219 * milliseconds and "us" for microseconds.
3220 * Returns 0 on error and non-zero if OK.
3221 */
3222int smp_check_date_unit(struct arg *args, char **err)
3223{
3224 if (args[1].type == ARGT_STR) {
3225 if (strcmp(args[1].data.str.area, "s") == 0) {
3226 args[1].data.sint = TIME_UNIT_S;
3227 }
3228 else if (strcmp(args[1].data.str.area, "ms") == 0) {
3229 args[1].data.sint = TIME_UNIT_MS;
3230 }
3231 else if (strcmp(args[1].data.str.area, "us") == 0) {
3232 args[1].data.sint = TIME_UNIT_US;
3233 }
3234 else {
3235 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
3236 args[1].data.str.area);
3237 return 0;
3238 }
3239 free(args[1].data.str.area);
3240 args[1].data.str.area = NULL;
3241 args[1].type = ARGT_SINT;
3242 }
3243 else if (args[1].type != ARGT_STOP) {
3244 memprintf(err, "Unexpected arg type");
3245 return 0;
3246 }
3247
3248 return 1;
3249}
3250
3251/* retrieve the current local date in epoch time, converts it to milliseconds
3252 * or microseconds if asked to in optional args[1] unit param, and applies an
3253 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003254 */
3255static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003256smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003257{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003258 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003259
Damien Claisseae6f1252019-10-30 15:57:28 +00003260 /* report in milliseconds */
3261 if (args && args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
3262 smp->data.u.sint *= 1000;
3263 smp->data.u.sint += date.tv_usec / 1000;
3264 }
3265 /* report in microseconds */
3266 else if (args && args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
3267 smp->data.u.sint *= 1000000;
3268 smp->data.u.sint += date.tv_usec;
3269 }
3270
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003271 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003272 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003273 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003274
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003275 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003276 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3277 return 1;
3278}
3279
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003280/* retrieve the current microsecond part of the date */
3281static int
3282smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3283{
3284 smp->data.u.sint = date.tv_usec;
3285 smp->data.type = SMP_T_SINT;
3286 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3287 return 1;
3288}
3289
3290
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003291/* returns the hostname */
3292static int
3293smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3294{
3295 smp->data.type = SMP_T_STR;
3296 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003297 smp->data.u.str.area = hostname;
3298 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003299 return 1;
3300}
3301
Willy Tarreau0f30d262014-11-24 16:02:05 +01003302/* returns the number of processes */
3303static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003304smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003305{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003306 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003307 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003308 return 1;
3309}
3310
3311/* returns the number of the current process (between 1 and nbproc */
3312static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003313smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003314{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003315 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003316 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003317 return 1;
3318}
3319
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003320/* returns the number of the current thread (between 1 and nbthread */
3321static int
3322smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3323{
3324 smp->data.type = SMP_T_SINT;
3325 smp->data.u.sint = tid;
3326 return 1;
3327}
3328
Willy Tarreau84310e22014-02-14 11:59:04 +01003329/* generate a random 32-bit integer for whatever purpose, with an optional
3330 * range specified in argument.
3331 */
3332static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003333smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01003334{
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003335 smp->data.u.sint = ha_random32();
Willy Tarreau84310e22014-02-14 11:59:04 +01003336
3337 /* reduce if needed. Don't do a modulo, use all bits! */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003338 if (args && args[0].type == ARGT_SINT)
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003339 smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32;
Willy Tarreau84310e22014-02-14 11:59:04 +01003340
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003341 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01003342 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3343 return 1;
3344}
3345
Willy Tarreau0f30d262014-11-24 16:02:05 +01003346/* returns true if the current process is stopping */
3347static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003348smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003349{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003350 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003351 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003352 return 1;
3353}
3354
Willy Tarreau70fe9442018-11-22 16:07:39 +01003355/* returns the number of calls of the current stream's process_stream() */
3356static int
3357smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
3358{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003359 if (!smp->strm)
3360 return 0;
3361
Willy Tarreau70fe9442018-11-22 16:07:39 +01003362 smp->data.type = SMP_T_SINT;
3363 smp->data.u.sint = smp->strm->task->calls;
3364 return 1;
3365}
3366
3367/* returns the average number of nanoseconds spent processing the stream per call */
3368static int
3369smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3370{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003371 if (!smp->strm)
3372 return 0;
3373
Willy Tarreau70fe9442018-11-22 16:07:39 +01003374 smp->data.type = SMP_T_SINT;
3375 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
3376 return 1;
3377}
3378
3379/* returns the total number of nanoseconds spent processing the stream */
3380static int
3381smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3382{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003383 if (!smp->strm)
3384 return 0;
3385
Willy Tarreau70fe9442018-11-22 16:07:39 +01003386 smp->data.type = SMP_T_SINT;
3387 smp->data.u.sint = smp->strm->task->cpu_time;
3388 return 1;
3389}
3390
3391/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
3392static int
3393smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3394{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003395 if (!smp->strm)
3396 return 0;
3397
Willy Tarreau70fe9442018-11-22 16:07:39 +01003398 smp->data.type = SMP_T_SINT;
3399 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
3400 return 1;
3401}
3402
3403/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
3404static int
3405smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3406{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003407 if (!smp->strm)
3408 return 0;
3409
Willy Tarreau70fe9442018-11-22 16:07:39 +01003410 smp->data.type = SMP_T_SINT;
3411 smp->data.u.sint = smp->strm->task->lat_time;
3412 return 1;
3413}
3414
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003415static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
3416{
3417 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003418 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003419 smp->data.u.str.area = args[0].data.str.area;
3420 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003421 return 1;
3422}
3423
3424static int smp_check_const_bool(struct arg *args, char **err)
3425{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003426 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
3427 strcasecmp(args[0].data.str.area, "1") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003428 args[0].type = ARGT_SINT;
3429 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003430 return 1;
3431 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003432 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
3433 strcasecmp(args[0].data.str.area, "0") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003434 args[0].type = ARGT_SINT;
3435 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003436 return 1;
3437 }
3438 memprintf(err, "Expects 'true', 'false', '0' or '1'");
3439 return 0;
3440}
3441
3442static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
3443{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003444 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003445 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003446 return 1;
3447}
3448
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003449static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003450{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003451 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003452 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003453 return 1;
3454}
3455
3456static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
3457{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003458 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003459 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003460 return 1;
3461}
3462
3463static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
3464{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003465 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003466 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003467 return 1;
3468}
3469
3470static int smp_check_const_bin(struct arg *args, char **err)
3471{
David Carlier64a16ab2016-04-08 10:37:02 +01003472 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003473 int binstrlen;
3474
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003475 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003476 return 0;
3477 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003478 args[0].data.str.area = binstr;
3479 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003480 return 1;
3481}
3482
3483static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
3484{
3485 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003486 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003487 smp->data.u.str.area = args[0].data.str.area;
3488 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003489 return 1;
3490}
3491
3492static int smp_check_const_meth(struct arg *args, char **err)
3493{
3494 enum http_meth_t meth;
3495 int i;
3496
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003497 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003498 if (meth != HTTP_METH_OTHER) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003499 args[0].type = ARGT_SINT;
3500 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003501 } else {
3502 /* Check method avalaibility. A methos is a token defined as :
3503 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
3504 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
3505 * token = 1*tchar
3506 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003507 for (i = 0; i < args[0].data.str.data; i++) {
3508 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003509 memprintf(err, "expects valid method.");
3510 return 0;
3511 }
3512 }
3513 }
3514 return 1;
3515}
3516
3517static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
3518{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003519 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003520 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003521 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003522 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003523 smp->data.u.meth.str.area = "";
3524 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003525 } else {
3526 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003527 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003528 smp->data.u.meth.str.area = args[0].data.str.area;
3529 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003530 }
3531 return 1;
3532}
3533
Luca Schimweg8a694b82019-09-10 15:42:52 +02003534// This function checks the "uuid" sample's arguments.
3535// Function won't get called when no parameter is specified (maybe a bug?)
3536static int smp_check_uuid(struct arg *args, char **err)
3537{
3538 if (!args[0].type) {
3539 args[0].type = ARGT_SINT;
3540 args[0].data.sint = 4;
3541 }
3542 else if (args[0].data.sint != 4) {
3543 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
3544 return 0;
3545 }
3546
3547 return 1;
3548}
3549
3550// Generate a RFC4122 UUID (default is v4 = fully random)
3551static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
3552{
3553 if (args[0].data.sint == 4 || !args[0].type) {
Willy Tarreauee3bcdd2020-03-08 17:48:17 +01003554 ha_generate_uuid(&trash);
Luca Schimweg8a694b82019-09-10 15:42:52 +02003555 smp->data.type = SMP_T_STR;
3556 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3557 smp->data.u.str = trash;
3558 return 1;
3559 }
3560
3561 // more implementations of other uuid formats possible here
3562 return 0;
3563}
3564
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003565/* Note: must not be declared <const> as its list will be overwritten.
3566 * Note: fetches that may return multiple types must be declared as the lowest
3567 * common denominator, the type that can be casted into all other ones. For
3568 * instance IPv4/IPv6 must be declared IPv4.
3569 */
3570static struct sample_fetch_kw_list smp_kws = {ILH, {
3571 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
3572 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003573 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Damien Claisseae6f1252019-10-30 15:57:28 +00003574 { "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 +01003575 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003576 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003577 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3578 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003579 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003580 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01003581 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003582 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Luca Schimweg8a694b82019-09-10 15:42:52 +02003583 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003584
3585 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3586 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3587 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3588 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3589 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003590
3591 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
3592 { "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 +02003593 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003594 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
3595 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
3596 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
3597 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
3598
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003599 { /* END */ },
3600}};
3601
Willy Tarreau0108d902018-11-25 19:14:37 +01003602INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
3603
Emeric Brun107ca302010-01-04 16:16:05 +01003604/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003605static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau0851fd52019-12-17 10:07:25 +01003606 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
Holger Just1bfc24b2017-05-06 00:56:53 +02003607 { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN },
Emeric Brun53d1a982014-04-30 18:21:37 +02003608 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02003609 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
3610 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau60a2ee72017-12-15 07:13:48 +01003611 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01003612 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Dragan Dosen3f957b22017-10-24 09:27:34 +02003613 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
Tim Duesterhus1478aa72018-01-25 16:24:51 +01003614 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003615 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
3616 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003617 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01003618 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003619 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3620 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3621 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER01e09742016-12-26 11:46:11 +01003622 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3623 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02003624 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003625 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
Marcin Deranek9631a282018-04-16 14:30:46 +02003626 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
3627 { "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 +01003628 { "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 +02003629 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02003630#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01003631 { "sha2", sample_conv_sha2, ARG1(0, SINT), smp_check_sha2, SMP_T_BIN, SMP_T_BIN },
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02003632#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
3633 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
3634#endif
Tim Duesterhusd4376302019-06-17 12:41:44 +02003635#endif
Willy Tarreau280f42b2018-02-19 15:34:12 +01003636 { "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 +02003637 { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT },
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003638
3639 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003640 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
3641 { "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 +01003642
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003643 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3644 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3645 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3646 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
3647 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3648 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3649 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3650 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3651 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3652 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3653 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3654 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3655 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3656 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01003657
Christopher Fauletea159d62020-04-01 16:21:44 +02003658 { "htonl", sample_conv_htonl, 0, NULL, SMP_T_SINT, SMP_T_BIN },
3659 { "cut_crlf", sample_conv_cut_crlf, 0, NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet51fc9d12020-04-01 17:24:41 +02003660 { "ltrim", sample_conv_ltrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet568415a2020-04-01 17:24:47 +02003661 { "rtrim", sample_conv_rtrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02003662 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01003663}};
3664
Willy Tarreau0108d902018-11-25 19:14:37 +01003665INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);