blob: 14615a1ba1ec33563b87bccac7356a29f9cf98a9 [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 Tarreau4c7e4b72020-05-27 12:58:42 +020019#include <haproxy/api.h>
Willy Tarreau7e2c6472012-10-29 20:44:36 +010020#include <types/global.h>
21
Willy Tarreauc13ed532020-06-02 10:22:45 +020022#include <haproxy/chunk.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020023#include <haproxy/hash.h>
Willy Tarreau35b51c62018-09-10 15:38:55 +020024#include <common/http.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020025#include <haproxy/net_helper.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020026#include <haproxy/regex.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020027#include <common/standard.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020028#include <common/uri_auth.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020029#include <haproxy/base64.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020030
Willy Tarreau9fcb9842012-04-20 14:45:49 +020031#include <proto/arg.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020032#include <proto/auth.h>
33#include <proto/log.h>
34#include <proto/proxy.h>
Frédéric Lécaillefd95c622019-02-26 14:09:08 +010035#include <proto/protocol_buffers.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020036#include <proto/sample.h>
Willy Tarreau0851fd52019-12-17 10:07:25 +010037#include <proto/sink.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020038#include <proto/stick_table.h>
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +020039#include <proto/vars.h>
Emeric Brun107ca302010-01-04 16:16:05 +010040
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +020041#include <import/sha1.h>
Thierry FOURNIER01e09742016-12-26 11:46:11 +010042#include <import/xxhash.h>
43
Willy Tarreau1cf8f082014-02-07 12:14:54 +010044/* sample type names */
45const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020046 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010047 [SMP_T_BOOL] = "bool",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010048 [SMP_T_SINT] = "sint",
49 [SMP_T_ADDR] = "addr",
50 [SMP_T_IPV4] = "ipv4",
51 [SMP_T_IPV6] = "ipv6",
52 [SMP_T_STR] = "str",
53 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020054 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010055};
56
Willy Tarreau12785782012-04-27 21:37:17 +020057/* static sample used in sample_process() when <p> is NULL */
Emeric Brune5c918b2017-06-14 14:15:36 +020058static THREAD_LOCAL struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010059
Willy Tarreau12785782012-04-27 21:37:17 +020060/* list head of all known sample fetch keywords */
61static struct sample_fetch_kw_list sample_fetches = {
62 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010063};
64
Willy Tarreau12785782012-04-27 21:37:17 +020065/* list head of all known sample format conversion keywords */
66static struct sample_conv_kw_list sample_convs = {
67 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010068};
69
Willy Tarreau80aca902013-01-07 15:42:20 +010070const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
71 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
72 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
73 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
74 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
75 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
76 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +010077 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +010078
79 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
80 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
81 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
82 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
83 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
84 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +010085 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +010086
87 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
88 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
89 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
90 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
91 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
92 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +010093 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +010094
95 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
96 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
97 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
98 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
99 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
100 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100101 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100102
103 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
104 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
105 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
106 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
107 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
108 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100109 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100110
111 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
112 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
113 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
114 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
115 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
116 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100117 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100118
119 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
120 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
121 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
122 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
123 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
124 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100125 SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100126
127 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
128 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
129 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
130 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
131 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
132 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100133 SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100134
135 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
136 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
137 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
138 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
139 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
140 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100141 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100142
143 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
144 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
145 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
146 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
147 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
148 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100149 SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100150
151 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
152 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
153 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
154 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
155 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
156 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100157 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100158
159 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
160 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
161 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
162 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
163 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
164 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100165 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100166
167 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
168 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
169 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
170 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
171 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
172 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100173 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100174
175 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
176 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
177 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
178 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
179 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
180 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100181 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100182
183 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
184 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
185 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
186 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
187 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
188 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Christopher Faulete596d182020-05-05 17:46:34 +0200189 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100190
191 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
192 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
193 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
194 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
195 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
196 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Christopher Faulete596d182020-05-05 17:46:34 +0200197 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100198
199 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
200 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
201 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
202 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
203 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
204 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Christopher Faulete596d182020-05-05 17:46:34 +0200205 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100206
207 [SMP_SRC_HRSBO] = (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___________ | SMP_VAL___________ |
211 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
212 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Christopher Faulete596d182020-05-05 17:46:34 +0200213 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100214
215 [SMP_SRC_RQFIN] = (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___________ |
220 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100221 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100222
223 [SMP_SRC_RSFIN] = (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___________ |
228 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100229 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100230
231 [SMP_SRC_TXFIN] = (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___________ |
236 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100237 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100238
239 [SMP_SRC_SSFIN] = (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___________ |
244 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100245 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100246};
247
248static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
249 [SMP_SRC_INTRN] = "internal state",
250 [SMP_SRC_LISTN] = "listener",
251 [SMP_SRC_FTEND] = "frontend",
252 [SMP_SRC_L4CLI] = "client address",
253 [SMP_SRC_L5CLI] = "client-side connection",
254 [SMP_SRC_TRACK] = "track counters",
255 [SMP_SRC_L6REQ] = "request buffer",
256 [SMP_SRC_HRQHV] = "HTTP request headers",
257 [SMP_SRC_HRQHP] = "HTTP request",
258 [SMP_SRC_HRQBO] = "HTTP request body",
259 [SMP_SRC_BKEND] = "backend",
260 [SMP_SRC_SERVR] = "server",
261 [SMP_SRC_L4SRV] = "server address",
262 [SMP_SRC_L5SRV] = "server-side connection",
263 [SMP_SRC_L6RES] = "response buffer",
264 [SMP_SRC_HRSHV] = "HTTP response headers",
265 [SMP_SRC_HRSHP] = "HTTP response",
266 [SMP_SRC_HRSBO] = "HTTP response body",
267 [SMP_SRC_RQFIN] = "request buffer statistics",
268 [SMP_SRC_RSFIN] = "response buffer statistics",
269 [SMP_SRC_TXFIN] = "transaction statistics",
270 [SMP_SRC_SSFIN] = "session statistics",
271};
272
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100273static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
274 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
275 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
276 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
277 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
278 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
279 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
280 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
281 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
282 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
283 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
284 [SMP_CKP_BE_SRV_CON] = "server source selection",
285 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
286 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
287 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
288 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
289 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
290 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
291 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
292 [SMP_CKP_FE_LOG_END] = "logs",
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100293 [SMP_CKP_BE_CHK_RUL] = "backend tcp-check rule",
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100294};
295
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100296/* This function returns the type of the data returned by the sample_expr.
297 * It assumes that the <expr> and all of its converters are properly
298 * initialized.
299 */
300inline
301int smp_expr_output_type(struct sample_expr *expr)
302{
303 struct sample_conv_expr *smp_expr;
304
305 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
306 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
307 return smp_expr->conv->out_type;
308 }
309 return expr->fetch->out_type;
310}
311
312
Willy Tarreau80aca902013-01-07 15:42:20 +0100313/* fill the trash with a comma-delimited list of source names for the <use> bit
314 * field which must be composed of a non-null set of SMP_USE_* flags. The return
315 * value is the pointer to the string in the trash buffer.
316 */
317const char *sample_src_names(unsigned int use)
318{
319 int bit;
320
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200321 trash.data = 0;
322 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100323 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
324 if (!(use & ~((1 << bit) - 1)))
325 break; /* no more bits */
326
327 if (!(use & (1 << bit)))
328 continue; /* bit not set */
329
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200330 trash.data += snprintf(trash.area + trash.data,
331 trash.size - trash.data, "%s%s",
332 (use & ((1 << bit) - 1)) ? "," : "",
333 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100334 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200335 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100336}
337
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100338/* return a pointer to the correct sample checkpoint name, or "unknown" when
339 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
340 * if set.
341 */
342const char *sample_ckp_names(unsigned int use)
343{
344 int bit;
345
346 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
347 if (use & (1 << bit))
348 return fetch_ckp_names[bit];
349 return "unknown sample check place, please report this bug";
350}
351
Emeric Brun107ca302010-01-04 16:16:05 +0100352/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100353 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
354 * for next parsing sessions. The fetch keywords capabilities are also computed
355 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100356 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100357void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100358{
Willy Tarreau80aca902013-01-07 15:42:20 +0100359 struct sample_fetch *sf;
360 int bit;
361
362 for (sf = kwl->kw; sf->kw != NULL; sf++) {
363 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
364 if (sf->use & (1 << bit))
365 sf->val |= fetch_cap[bit];
366 }
367 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100368}
369
370/*
Willy Tarreau12785782012-04-27 21:37:17 +0200371 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100372 * parsing sessions.
373 */
Willy Tarreau12785782012-04-27 21:37:17 +0200374void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100375{
Willy Tarreau12785782012-04-27 21:37:17 +0200376 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100377}
378
379/*
Willy Tarreau12785782012-04-27 21:37:17 +0200380 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100381 * string of <len> in buffer <kw>.
382 *
383 */
Willy Tarreau12785782012-04-27 21:37:17 +0200384struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100385{
386 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200387 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100388
Willy Tarreau12785782012-04-27 21:37:17 +0200389 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100390 for (index = 0; kwl->kw[index].kw != NULL; index++) {
391 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
392 kwl->kw[index].kw[len] == '\0')
393 return &kwl->kw[index];
394 }
395 }
396 return NULL;
397}
398
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100399/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100400 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100401 * <idx> is the index of the next sample fetch entry. It is used as private
402 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100403 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100404 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100405 * the list is reached.
406 */
407struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
408{
409 struct sample_fetch_kw_list *kwl;
410 struct sample_fetch *base;
411
412 if (!current) {
413 /* Get first kwl entry. */
414 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
415 (*idx) = 0;
416 } else {
417 /* Get kwl corresponding to the curret entry. */
418 base = current + 1 - (*idx);
419 kwl = container_of(base, struct sample_fetch_kw_list, kw);
420 }
421
422 while (1) {
423
424 /* Check if kwl is the last entry. */
425 if (&kwl->list == &sample_fetches.list)
426 return NULL;
427
428 /* idx contain the next keyword. If it is available, return it. */
429 if (kwl->kw[*idx].kw) {
430 (*idx)++;
431 return &kwl->kw[(*idx)-1];
432 }
433
434 /* get next entry in the main list, and return NULL if the end is reached. */
435 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
436
437 /* Set index to 0, ans do one other loop. */
438 (*idx) = 0;
439 }
440}
441
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100442/* This function browses the list of available converters. <current> is
443 * the last used converter. If it is the first call, it must set to NULL.
444 * <idx> is the index of the next converter entry. It is used as private
445 * value. It is useless to initiate it.
446 *
447 * It returns always the next sample_conv entry, and NULL when the end of
448 * the list is reached.
449 */
450struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
451{
452 struct sample_conv_kw_list *kwl;
453 struct sample_conv *base;
454
455 if (!current) {
456 /* Get first kwl entry. */
457 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
458 (*idx) = 0;
459 } else {
460 /* Get kwl corresponding to the curret entry. */
461 base = current + 1 - (*idx);
462 kwl = container_of(base, struct sample_conv_kw_list, kw);
463 }
464
465 while (1) {
466 /* Check if kwl is the last entry. */
467 if (&kwl->list == &sample_convs.list)
468 return NULL;
469
470 /* idx contain the next keyword. If it is available, return it. */
471 if (kwl->kw[*idx].kw) {
472 (*idx)++;
473 return &kwl->kw[(*idx)-1];
474 }
475
476 /* get next entry in the main list, and return NULL if the end is reached. */
477 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
478
479 /* Set index to 0, ans do one other loop. */
480 (*idx) = 0;
481 }
482}
483
Emeric Brun107ca302010-01-04 16:16:05 +0100484/*
Willy Tarreau12785782012-04-27 21:37:17 +0200485 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100486 * string of <len> in buffer <kw>.
487 *
488 */
Willy Tarreau12785782012-04-27 21:37:17 +0200489struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100490{
491 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200492 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100493
Willy Tarreau12785782012-04-27 21:37:17 +0200494 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100495 for (index = 0; kwl->kw[index].kw != NULL; index++) {
496 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
497 kwl->kw[index].kw[len] == '\0')
498 return &kwl->kw[index];
499 }
500 }
501 return NULL;
502}
503
Emeric Brun107ca302010-01-04 16:16:05 +0100504/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200505/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100506/******************************************************************/
507
Willy Tarreau342acb42012-04-23 22:03:39 +0200508static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100509{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200510 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200511 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100512 return 1;
513}
514
Willy Tarreau342acb42012-04-23 22:03:39 +0200515static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100516{
Willy Tarreau83061a82018-07-13 11:56:34 +0200517 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100518
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200519 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100520 return 0;
521
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200522 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200523 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200524 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100525 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100526
527 return 1;
528}
529
Willy Tarreau342acb42012-04-23 22:03:39 +0200530static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100531{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200532 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200533 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100534 return 1;
535}
536
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200537static int c_ipv62ip(struct sample *smp)
538{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200539 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200540 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100541 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200542 return 1;
543}
544
Willy Tarreau342acb42012-04-23 22:03:39 +0200545static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100546{
Willy Tarreau83061a82018-07-13 11:56:34 +0200547 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100548
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200549 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100550 return 0;
551
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200552 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200553 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200554 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100555 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100556 return 1;
557}
558
559/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200560static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100561{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200562 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100563}
564*/
565
Willy Tarreau342acb42012-04-23 22:03:39 +0200566static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100567{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200568 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200569 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100570 return 1;
571}
572
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200573static int c_int2ipv6(struct sample *smp)
574{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200575 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
576 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200577 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200578 return 1;
579}
580
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100581static int c_str2addr(struct sample *smp)
582{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200583 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
584 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100585 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200586 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100587 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100588 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100589 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200590 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100591 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100592 return 1;
593}
594
Willy Tarreau342acb42012-04-23 22:03:39 +0200595static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100596{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200597 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100598 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200599 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100600 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100601 return 1;
602}
603
Willy Tarreau342acb42012-04-23 22:03:39 +0200604static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100605{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200606 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100607 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200608 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100609 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100610 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100611}
612
Emeric Brun4b9e8022014-11-03 18:17:10 +0100613/*
614 * The NULL char always enforces the end of string if it is met.
615 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100616 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200617static int c_bin2str(struct sample *smp)
618{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100619 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200620
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200621 for (i = 0; i < smp->data.u.str.data; i++) {
622 if (!smp->data.u.str.area[i]) {
623 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100624 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100625 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200626 }
Christopher Faulet472ad512020-04-30 09:57:40 +0200627 smp->data.type = SMP_T_STR;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200628 return 1;
629}
630
Willy Tarreau342acb42012-04-23 22:03:39 +0200631static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100632{
Willy Tarreau83061a82018-07-13 11:56:34 +0200633 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100634 char *pos;
635
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200636 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100637 if (!pos)
638 return 0;
639
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200640 trash->size = trash->size - (pos - trash->area);
641 trash->area = pos;
642 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200643 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200644 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100645 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100646 return 1;
647}
648
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800649/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200650 * For strings and binary blocks, it also provides a known allocated size with
651 * a length that is capped to the size, and ensures a trailing zero is always
652 * appended for strings. This is necessary for some operations which may
653 * require to extend the length. It returns 0 if it fails, 1 on success.
654 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100655int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200656{
Willy Tarreau83061a82018-07-13 11:56:34 +0200657 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200658
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200659 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100660 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100661 case SMP_T_SINT:
662 case SMP_T_ADDR:
663 case SMP_T_IPV4:
664 case SMP_T_IPV6:
665 /* These type are not const. */
666 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200667
Christopher Fauletec100512017-07-24 15:38:41 +0200668 case SMP_T_METH:
669 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
670 break;
671 /* Fall through */
672
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100673 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200674 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100675 trash->data = smp->data.type == SMP_T_STR ?
676 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200677 if (trash->data > trash->size - 1)
678 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200679
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100680 memcpy(trash->area, smp->data.type == SMP_T_STR ?
681 smp->data.u.str.area : smp->data.u.meth.str.area,
682 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200683 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200684 smp->data.u.str = *trash;
685 break;
686
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100687 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100688 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200689 trash->data = smp->data.u.str.data;
690 if (trash->data > trash->size)
691 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200692
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200693 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200694 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100695 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200696
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100697 default:
698 /* Other cases are unexpected. */
699 return 0;
700 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100701
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100702 /* remove const flag */
703 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200704 return 1;
705}
706
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100707int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100708{
709 return 1;
710}
711
Willy Tarreau342acb42012-04-23 22:03:39 +0200712static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100713{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200714 const char *str;
715 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100716
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200717 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100718 return 0;
719
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200720 str = smp->data.u.str.area;
721 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100722
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200723 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200724 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100725 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100726 return 1;
727}
728
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100729static int c_str2meth(struct sample *smp)
730{
731 enum http_meth_t meth;
732 int len;
733
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200734 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100735 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200736 len = smp->data.u.str.data;
737 smp->data.u.meth.str.area = smp->data.u.str.area;
738 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100739 }
740 else
741 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200742 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200743 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100744 return 1;
745}
746
747static int c_meth2str(struct sample *smp)
748{
749 int len;
750 enum http_meth_t meth;
751
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200752 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100753 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200754 len = smp->data.u.meth.str.data;
755 smp->data.u.str.area = smp->data.u.meth.str.area;
756 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200757 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100758 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200759 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100760 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200761 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200762 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200763 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100764 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200765 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100766 }
767 else {
768 /* Unknown method */
769 return 0;
770 }
771 return 1;
772}
773
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200774static int c_addr2bin(struct sample *smp)
775{
Willy Tarreau83061a82018-07-13 11:56:34 +0200776 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200777
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200778 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200779 chk->data = 4;
780 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200781 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200782 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200783 chk->data = 16;
784 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200785 }
786 else
787 return 0;
788
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200789 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200790 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200791 return 1;
792}
793
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200794static int c_int2bin(struct sample *smp)
795{
Willy Tarreau83061a82018-07-13 11:56:34 +0200796 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200797
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200798 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
799 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200800
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200801 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200802 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200803 return 1;
804}
805
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200806
Emeric Brun107ca302010-01-04 16:16:05 +0100807/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200808/* Sample casts matrix: */
809/* sample_casts[from type][to type] */
810/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100811/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100812
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100813sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200814/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
815/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
816/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200817/* 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 +0200818/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
819/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200820/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200821/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
822/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
823/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200824};
Emeric Brun107ca302010-01-04 16:16:05 +0100825
Emeric Brun107ca302010-01-04 16:16:05 +0100826/*
Willy Tarreau12785782012-04-27 21:37:17 +0200827 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100828 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200829 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200830 * The caller must have set al->ctx.
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100831 * If <endptr> is non-nul, it will be set to the first unparsed character
832 * (which may be the final '\0') on success. If it is nul, the expression
833 * must be properly terminated by a '\0' otherwise an error is reported.
Emeric Brun107ca302010-01-04 16:16:05 +0100834 */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100835struct 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 +0100836{
Willy Tarreau833cc792013-07-24 15:34:19 +0200837 const char *begw; /* beginning of word */
838 const char *endw; /* end of word */
839 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200840 struct sample_expr *expr;
841 struct sample_fetch *fetch;
842 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100843 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200844 char *fkw = NULL;
845 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100846 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100847
Willy Tarreau833cc792013-07-24 15:34:19 +0200848 begw = str[*idx];
Willy Tarreaued2c6622020-02-14 18:27:10 +0100849 for (endw = begw; is_idchar(*endw); endw++)
850 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200851
852 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100853 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100854 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200855 }
Emeric Brun107ca302010-01-04 16:16:05 +0100856
Willy Tarreau833cc792013-07-24 15:34:19 +0200857 /* keep a copy of the current fetch keyword for error reporting */
858 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100859
Willy Tarreau833cc792013-07-24 15:34:19 +0200860 fetch = find_sample_fetch(begw, endw - begw);
861 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100862 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100863 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200864 }
Emeric Brun107ca302010-01-04 16:16:05 +0100865
Willy Tarreau833cc792013-07-24 15:34:19 +0200866 /* At this point, we have :
867 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100868 * - endw : end of the keyword, first character not part of keyword
Willy Tarreau833cc792013-07-24 15:34:19 +0200869 */
870
871 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100872 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100873 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200874 }
Emeric Brun107ca302010-01-04 16:16:05 +0100875 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200876
Vincent Bernat02779b62016-04-03 13:48:43 +0200877 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200878 if (!expr)
879 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100880
881 LIST_INIT(&(expr->conv_exprs));
882 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200883 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100884
Willy Tarreau689a1df2013-12-13 00:40:11 +0100885 /* Note that we call the argument parser even with an empty string,
886 * this allows it to automatically create entries for mandatory
887 * implicit arguments (eg: local proxy name).
888 */
889 al->kw = expr->fetch->kw;
890 al->conv = NULL;
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100891 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 +0100892 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
893 goto out_error;
894 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200895
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100896 /* now endt is our first char not part of the arg list, typically the
897 * comma after the sample fetch name or after the closing parenthesis,
898 * or the NUL char.
899 */
900
Willy Tarreau689a1df2013-12-13 00:40:11 +0100901 if (!expr->arg_p) {
902 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200903 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100904 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
905 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200906 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100907 }
908
Willy Tarreau833cc792013-07-24 15:34:19 +0200909 /* Now process the converters if any. We have two supported syntaxes
910 * for the converters, which can be combined :
911 * - comma-delimited list of converters just after the keyword and args ;
912 * - one converter per keyword
913 * The combination allows to have each keyword being a comma-delimited
914 * series of converters.
915 *
916 * We want to process the former first, then the latter. For this we start
917 * from the beginning of the supposed place in the exiting conv chain, which
918 * starts at the last comma (endt).
919 */
920
921 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200922 struct sample_conv_expr *conv_expr;
Willy Tarreau46dfd782019-12-17 10:25:29 +0100923 int err_arg;
924 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +0100925
Willy Tarreau833cc792013-07-24 15:34:19 +0200926 if (*endt && *endt != ',') {
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100927 if (endptr) {
928 /* end found, let's stop here */
929 break;
930 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200931 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100932 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200933 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100934 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100935 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200936 }
Emeric Brun107ca302010-01-04 16:16:05 +0100937
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100938 /* FIXME: how long should we support such idiocies ? Maybe we
939 * should already warn ?
940 */
Willy Tarreau833cc792013-07-24 15:34:19 +0200941 while (*endt == ',') /* then trailing commas */
942 endt++;
943
Willy Tarreau97108e02016-11-25 07:33:24 +0100944 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200945
946 if (!*begw) {
947 /* none ? skip to next string */
948 (*idx)++;
949 begw = str[*idx];
950 if (!begw || !*begw)
951 break;
952 }
953
Willy Tarreaued2c6622020-02-14 18:27:10 +0100954 for (endw = begw; is_idchar(*endw); endw++)
955 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200956
957 free(ckw);
958 ckw = my_strndup(begw, endw - begw);
959
960 conv = find_sample_conv(begw, endw - begw);
961 if (!conv) {
962 /* we found an isolated keyword that we don't know, it's not ours */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100963 if (begw == str[*idx]) {
964 endt = begw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200965 break;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100966 }
Willy Tarreau97108e02016-11-25 07:33:24 +0100967 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200968 goto out_error;
969 }
Emeric Brun107ca302010-01-04 16:16:05 +0100970
Willy Tarreau833cc792013-07-24 15:34:19 +0200971 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100972 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100973 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200974 }
Emeric Brun107ca302010-01-04 16:16:05 +0100975
976 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200977 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100978 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100979 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200980 }
Emeric Brun107ca302010-01-04 16:16:05 +0100981
982 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +0200983 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200984 if (!conv_expr)
985 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100986
987 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
988 conv_expr->conv = conv;
989
Willy Tarreau46dfd782019-12-17 10:25:29 +0100990 al->kw = expr->fetch->kw;
991 al->conv = conv_expr->conv->kw;
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100992 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 +0100993 if (argcnt < 0) {
994 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
995 goto out_error;
996 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100997
Willy Tarreau46dfd782019-12-17 10:25:29 +0100998 if (argcnt && !conv->arg_mask) {
999 memprintf(err_msg, "converter '%s' does not support any args", ckw);
1000 goto out_error;
1001 }
Willy Tarreau21d68a62012-04-20 15:52:36 +02001002
Willy Tarreau46dfd782019-12-17 10:25:29 +01001003 if (!conv_expr->arg_p)
1004 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001005
Willy Tarreau46dfd782019-12-17 10:25:29 +01001006 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1007 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001008 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001009 }
1010 }
Emeric Brun485479d2010-09-23 18:02:19 +02001011
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001012 if (endptr) {
1013 /* end found, let's stop here */
1014 *endptr = (char *)endt;
1015 }
1016
Willy Tarreau833cc792013-07-24 15:34:19 +02001017 out:
1018 free(fkw);
1019 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001020 return expr;
1021
1022out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001023 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001024 expr = NULL;
1025 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001026}
1027
1028/*
Willy Tarreau12785782012-04-27 21:37:17 +02001029 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001030 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001031 * Returns a pointer on a typed sample structure containing the result or NULL if
1032 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001033 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001034 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001035 *
1036 * Note: the fetch functions are required to properly set the return type. The
1037 * conversion functions must do so too. However the cast functions do not need
1038 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001039 *
1040 * The caller may indicate in <opt> if it considers the result final or not.
1041 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1042 * if the result is stable or not, according to the following table :
1043 *
1044 * return MAY_CHANGE FINAL Meaning for the sample
1045 * NULL 0 * Not present and will never be (eg: header)
1046 * NULL 1 0 Not present yet, could change (eg: POST param)
1047 * NULL 1 1 Not present yet, will not change anymore
1048 * smp 0 * Present and will not change (eg: header)
1049 * smp 1 0 Present, may change (eg: request length)
1050 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001051 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001052struct sample *sample_process(struct proxy *px, struct session *sess,
1053 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001054 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001055{
Willy Tarreau12785782012-04-27 21:37:17 +02001056 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001057
Willy Tarreau18387e22013-07-25 12:02:38 +02001058 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001059 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001060 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001061 }
Emeric Brun107ca302010-01-04 16:16:05 +01001062
Willy Tarreau1777ea62016-03-10 16:15:46 +01001063 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001064 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001065 return NULL;
1066
Emeric Brun107ca302010-01-04 16:16:05 +01001067 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001068 /* we want to ensure that p->type can be casted into
1069 * conv_expr->conv->in_type. We have 3 possibilities :
1070 * - NULL => not castable.
1071 * - c_none => nothing to do (let's optimize it)
1072 * - other => apply cast and prepare to fail
1073 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001074 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001075 return NULL;
1076
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001077 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1078 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001079 return NULL;
1080
Willy Tarreau12e50112012-04-25 17:21:49 +02001081 /* OK cast succeeded */
1082
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001083 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001084 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001085 }
1086 return p;
1087}
1088
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001089/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001090 * Resolve all remaining arguments in proxy <p>. Returns the number of
1091 * errors or 0 if everything is fine.
1092 */
1093int smp_resolve_args(struct proxy *p)
1094{
1095 struct arg_list *cur, *bak;
1096 const char *ctx, *where;
1097 const char *conv_ctx, *conv_pre, *conv_pos;
1098 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001099 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001100 struct arg *arg;
1101 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001102 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001103
1104 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1105 struct proxy *px;
1106 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001107 struct stktable *t;
1108 char *pname, *sname, *stktname;
Willy Tarreau46947782015-01-19 19:00:58 +01001109 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001110
1111 arg = cur->arg;
1112
1113 /* prepare output messages */
1114 conv_pre = conv_pos = conv_ctx = "";
1115 if (cur->conv) {
1116 conv_ctx = cur->conv;
1117 conv_pre = "conversion keyword '";
1118 conv_pos = "' for ";
1119 }
1120
1121 where = "in";
1122 ctx = "sample fetch keyword";
1123 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001124 case ARGC_STK: where = "in stick rule in"; break;
1125 case ARGC_TRK: where = "in tracking rule in"; break;
1126 case ARGC_LOG: where = "in log-format string in"; break;
1127 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1128 case ARGC_HRQ: where = "in http-request header format string in"; break;
1129 case ARGC_HRS: where = "in http-response header format string in"; break;
1130 case ARGC_UIF: where = "in unique-id-format string in"; break;
1131 case ARGC_RDR: where = "in redirect format string in"; break;
1132 case ARGC_CAP: where = "in capture rule in"; break;
1133 case ARGC_ACL: ctx = "ACL keyword"; break;
1134 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001135 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001136 case ARGC_HERR: where = "in http-error directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001137 }
1138
1139 /* set a few default settings */
1140 px = p;
1141 pname = p->id;
1142
1143 switch (arg->type) {
1144 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001145 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001146 ha_alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1147 cur->file, cur->line,
1148 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001149 cfgerr++;
1150 continue;
1151 }
1152
1153 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001154 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001155
1156 if (sname) {
1157 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001158 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001159
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001160 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001161 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001162 ha_alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1163 cur->file, cur->line, pname,
1164 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001165 cfgerr++;
1166 break;
1167 }
1168 }
1169 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001170 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001171
1172 srv = findserver(px, sname);
1173 if (!srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001174 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",
1175 cur->file, cur->line, sname, pname,
1176 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001177 cfgerr++;
1178 break;
1179 }
1180
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001181 free(arg->data.str.area);
1182 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001183 arg->unresolved = 0;
1184 arg->data.srv = srv;
1185 break;
1186
1187 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001188 if (arg->data.str.data) {
1189 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001190 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001191 }
1192
1193 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001194 ha_alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1195 cur->file, cur->line, pname,
1196 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001197 cfgerr++;
1198 break;
1199 }
1200
1201 if (!(px->cap & PR_CAP_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001202 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",
1203 cur->file, cur->line, pname,
1204 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001205 cfgerr++;
1206 break;
1207 }
1208
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001209 free(arg->data.str.area);
1210 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001211 arg->unresolved = 0;
1212 arg->data.prx = px;
1213 break;
1214
1215 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001216 if (arg->data.str.data) {
1217 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001218 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001219 }
1220
1221 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001222 ha_alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1223 cur->file, cur->line, pname,
1224 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001225 cfgerr++;
1226 break;
1227 }
1228
1229 if (!(px->cap & PR_CAP_BE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001230 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",
1231 cur->file, cur->line, pname,
1232 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001233 cfgerr++;
1234 break;
1235 }
1236
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001237 free(arg->data.str.area);
1238 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001239 arg->unresolved = 0;
1240 arg->data.prx = px;
1241 break;
1242
1243 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001244 if (arg->data.str.data)
1245 stktname = arg->data.str.area;
1246 else
1247 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001248
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001249 t = stktable_find_by_name(stktname);
1250 if (!t) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001251 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 +01001252 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001253 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001254 cfgerr++;
1255 break;
1256 }
1257
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001258 if (!t->size) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001259 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 +01001260 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001261 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001262 cfgerr++;
1263 break;
1264 }
1265
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001266 if (t->proxy && (p->bind_proc & ~t->proxy->bind_proc)) {
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001267 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 +01001268 cur->file, cur->line, t->proxy->id, p->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01001269 cfgerr++;
1270 break;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001271 }
1272
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001273 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001274 p->next_stkt_ref = t->proxies_list;
1275 t->proxies_list = p;
1276 }
1277
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001278 free(arg->data.str.area);
1279 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001280 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001281 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001282 break;
1283
1284 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001285 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001286 ha_alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1287 cur->file, cur->line,
1288 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001289 cfgerr++;
1290 break;
1291 }
1292
1293 if (p->uri_auth && p->uri_auth->userlist &&
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001294 !strcmp(p->uri_auth->userlist->name, arg->data.str.area))
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001295 ul = p->uri_auth->userlist;
1296 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001297 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001298
1299 if (!ul) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001300 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 +02001301 cur->file, cur->line,
1302 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001303 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001304 cfgerr++;
1305 break;
1306 }
1307
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001308 free(arg->data.str.area);
1309 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001310 arg->unresolved = 0;
1311 arg->data.usr = ul;
1312 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001313
1314 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001315 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001316 ha_alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1317 cur->file, cur->line,
1318 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001319 cfgerr++;
1320 continue;
1321 }
1322
Willy Tarreau46947782015-01-19 19:00:58 +01001323 rflags = 0;
1324 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1325 err = NULL;
1326
Dragan Dosen26743032019-04-30 15:54:36 +02001327 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001328 ha_alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1329 cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001330 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001331 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
Willy Tarreau46947782015-01-19 19:00:58 +01001332 cfgerr++;
1333 continue;
1334 }
1335
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001336 free(arg->data.str.area);
1337 arg->data.str.area = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001338 arg->unresolved = 0;
1339 arg->data.reg = reg;
1340 break;
1341
1342
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001343 }
1344
1345 LIST_DEL(&cur->list);
1346 free(cur);
1347 } /* end of args processing */
1348
1349 return cfgerr;
1350}
1351
1352/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001353 * Process a fetch + format conversion as defined by the sample expression
1354 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001355 * not explicitly set to <smp_type>, but shall be compatible with it as
1356 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1357 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001358 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1359 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1360 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1361 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001362 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1363 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1364 * still hope to get it after waiting longer, and is not converted to string.
1365 * The possible output combinations are the following :
1366 *
1367 * return MAY_CHANGE FINAL Meaning for the sample
1368 * NULL * * Not present and will never be (eg: header)
1369 * smp 0 * Final value converted (eg: header)
1370 * smp 1 0 Not present yet, may appear later (eg: header)
1371 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001372 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001373struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001374 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001375 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001376{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001377 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001378
Willy Tarreau6c616e02014-06-25 16:56:41 +02001379 memset(smp, 0, sizeof(*smp));
1380
Willy Tarreau192252e2015-04-04 01:47:55 +02001381 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001382 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1383 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001384 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001385 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001386
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001387 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001388 return NULL;
1389
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001390 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001391 return NULL;
1392
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001393 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001394 return smp;
1395}
1396
Christopher Faulet476e5d02016-10-26 11:34:47 +02001397static void release_sample_arg(struct arg *p)
1398{
1399 struct arg *p_back = p;
1400
1401 if (!p)
1402 return;
1403
1404 while (p->type != ARGT_STOP) {
1405 if (p->type == ARGT_STR || p->unresolved) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001406 free(p->data.str.area);
1407 p->data.str.area = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001408 p->unresolved = 0;
1409 }
1410 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001411 regex_free(p->data.reg);
1412 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001413 }
1414 p++;
1415 }
1416
1417 if (p_back != empty_arg_list)
1418 free(p_back);
1419}
1420
1421void release_sample_expr(struct sample_expr *expr)
1422{
1423 struct sample_conv_expr *conv_expr, *conv_exprb;
1424
1425 if (!expr)
1426 return;
1427
1428 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list)
1429 release_sample_arg(conv_expr->arg_p);
1430 release_sample_arg(expr->arg_p);
1431 free(expr);
1432}
1433
Emeric Brun107ca302010-01-04 16:16:05 +01001434/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001435/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001436/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001437/*****************************************************************/
1438
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001439static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1440{
1441 int i;
1442 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001443 struct buffer *buf;
1444 struct sink *sink;
1445 struct ist line;
1446 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001447
Willy Tarreau0851fd52019-12-17 10:07:25 +01001448 buf = alloc_trash_chunk();
1449 if (!buf)
1450 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001451
Willy Tarreau0851fd52019-12-17 10:07:25 +01001452 sink = (struct sink *)arg_p[1].data.str.area;
1453 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001454
Willy Tarreau0851fd52019-12-17 10:07:25 +01001455 pfx = arg_p[0].data.str.area;
1456 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001457
Willy Tarreau0851fd52019-12-17 10:07:25 +01001458 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1459 if (!sample_casts[smp->data.type][SMP_T_STR])
1460 goto nocast;
1461
1462 /* Copy sample fetch. This puts the sample as const, the
1463 * cast will copy data if a transformation is required.
1464 */
1465 memcpy(&tmp, smp, sizeof(struct sample));
1466 tmp.flags = SMP_F_CONST;
1467
1468 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1469 goto nocast;
1470
1471 /* Display the displayable chars*. */
1472 b_putchr(buf, '<');
1473 for (i = 0; i < tmp.data.u.str.data; i++) {
Willy Tarreau90807112020-02-25 08:16:33 +01001474 if (isprint((unsigned char)tmp.data.u.str.area[i]))
Willy Tarreau0851fd52019-12-17 10:07:25 +01001475 b_putchr(buf, tmp.data.u.str.area[i]);
1476 else
1477 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001478 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001479 b_putchr(buf, '>');
1480
1481 done:
1482 line = ist2(buf->area, buf->data);
Emeric Brunbd163812020-05-06 14:33:46 +02001483 sink_write(sink, &line, 1, 0, 0, NULL, NULL, NULL);
Willy Tarreau0851fd52019-12-17 10:07:25 +01001484 end:
1485 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001486 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001487 nocast:
1488 chunk_appendf(buf, "(undisplayable)");
1489 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001490}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001491
1492// This function checks the "debug" converter's arguments.
1493static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1494 const char *file, int line, char **err)
1495{
1496 const char *name = "buf0";
1497 struct sink *sink = NULL;
1498
1499 if (args[0].type != ARGT_STR) {
1500 /* optional prefix */
1501 args[0].data.str.area = "";
1502 args[0].data.str.data = 0;
1503 }
1504
1505 if (args[1].type == ARGT_STR)
1506 name = args[1].data.str.area;
1507
1508 sink = sink_find(name);
1509 if (!sink) {
1510 memprintf(err, "No such sink '%s'", name);
1511 return 0;
1512 }
1513
1514 args[1].data.str.area = (char *)sink;
1515 args[1].data.str.data = 0; // that's not a string anymore
1516 return 1;
1517}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001518
Holger Just1bfc24b2017-05-06 00:56:53 +02001519static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1520{
Willy Tarreau83061a82018-07-13 11:56:34 +02001521 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001522 int bin_len;
1523
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001524 trash->data = 0;
1525 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1526 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001527 if (bin_len < 0)
1528 return 0;
1529
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001530 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001531 smp->data.u.str = *trash;
1532 smp->data.type = SMP_T_BIN;
1533 smp->flags &= ~SMP_F_CONST;
1534 return 1;
1535}
1536
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001537static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001538{
Willy Tarreau83061a82018-07-13 11:56:34 +02001539 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001540 int b64_len;
1541
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001542 trash->data = 0;
1543 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1544 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001545 if (b64_len < 0)
1546 return 0;
1547
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001548 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001549 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001550 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001551 smp->flags &= ~SMP_F_CONST;
1552 return 1;
1553}
1554
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001555static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1556{
1557 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001558 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001559
1560 memset(&ctx, 0, sizeof(ctx));
1561
1562 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001563 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1564 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001565
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001566 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001567 smp->data.u.str = *trash;
1568 smp->data.type = SMP_T_BIN;
1569 smp->flags &= ~SMP_F_CONST;
1570 return 1;
1571}
1572
Tim Duesterhusd4376302019-06-17 12:41:44 +02001573#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01001574static int smp_check_sha2(struct arg *args, struct sample_conv *conv,
1575 const char *file, int line, char **err)
1576{
1577 if (args[0].type == ARGT_STOP)
1578 return 1;
1579 if (args[0].type != ARGT_SINT) {
1580 memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]);
1581 return 0;
1582 }
1583
1584 switch (args[0].data.sint) {
1585 case 224:
1586 case 256:
1587 case 384:
1588 case 512:
1589 /* this is okay */
1590 return 1;
1591 default:
1592 memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint);
1593 return 0;
1594 }
1595}
1596
Tim Duesterhusd4376302019-06-17 12:41:44 +02001597static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
1598{
1599 struct buffer *trash = get_trash_chunk();
1600 int bits = 256;
1601 if (arg_p && arg_p->data.sint)
1602 bits = arg_p->data.sint;
1603
1604 switch (bits) {
1605 case 224: {
1606 SHA256_CTX ctx;
1607
1608 memset(&ctx, 0, sizeof(ctx));
1609
1610 SHA224_Init(&ctx);
1611 SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1612 SHA224_Final((unsigned char *) trash->area, &ctx);
1613 trash->data = SHA224_DIGEST_LENGTH;
1614 break;
1615 }
1616 case 256: {
1617 SHA256_CTX ctx;
1618
1619 memset(&ctx, 0, sizeof(ctx));
1620
1621 SHA256_Init(&ctx);
1622 SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1623 SHA256_Final((unsigned char *) trash->area, &ctx);
1624 trash->data = SHA256_DIGEST_LENGTH;
1625 break;
1626 }
1627 case 384: {
1628 SHA512_CTX ctx;
1629
1630 memset(&ctx, 0, sizeof(ctx));
1631
1632 SHA384_Init(&ctx);
1633 SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1634 SHA384_Final((unsigned char *) trash->area, &ctx);
1635 trash->data = SHA384_DIGEST_LENGTH;
1636 break;
1637 }
1638 case 512: {
1639 SHA512_CTX ctx;
1640
1641 memset(&ctx, 0, sizeof(ctx));
1642
1643 SHA512_Init(&ctx);
1644 SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1645 SHA512_Final((unsigned char *) trash->area, &ctx);
1646 trash->data = SHA512_DIGEST_LENGTH;
1647 break;
1648 }
1649 default:
1650 return 0;
1651 }
1652
1653 smp->data.u.str = *trash;
1654 smp->data.type = SMP_T_BIN;
1655 smp->flags &= ~SMP_F_CONST;
1656 return 1;
1657}
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001658
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001659static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
1660{
1661 switch (arg->type) {
1662 case ARGT_STR:
1663 smp->data.type = SMP_T_STR;
1664 smp->data.u.str = arg->data.str;
1665 return 1;
1666 case ARGT_VAR:
1667 if (!vars_get_by_desc(&arg->data.var, smp))
1668 return 0;
1669 if (!sample_casts[smp->data.type][SMP_T_STR])
1670 return 0;
1671 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
1672 return 0;
1673 return 1;
1674 default:
1675 return 0;
1676 }
1677}
1678
Patrick Gansterer8e366512020-04-22 16:47:57 +02001679#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001680static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
1681 const char *file, int line, char **err)
1682{
1683 switch(args[0].data.sint) {
1684 case 128:
1685 case 192:
1686 case 256:
1687 break;
1688 default:
1689 memprintf(err, "key size must be 128, 192 or 256 (bits).");
1690 return 0;
1691 }
1692 /* Try to decode a variable. */
1693 vars_check_arg(&args[1], NULL);
1694 vars_check_arg(&args[2], NULL);
1695 vars_check_arg(&args[3], NULL);
1696 return 1;
1697}
1698
1699/* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
1700static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
1701{
1702 struct sample nonce, key, aead_tag;
1703 struct buffer *smp_trash, *smp_trash_alloc;
1704 EVP_CIPHER_CTX *ctx;
1705 int dec_size, ret;
1706
1707 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
1708 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
1709 return 0;
1710
1711 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
1712 if (!sample_conv_var2smp_str(&arg_p[2], &key))
1713 return 0;
1714
1715 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
1716 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
1717 return 0;
1718
1719 smp_trash = get_trash_chunk();
1720 smp_trash_alloc = alloc_trash_chunk();
1721 if (!smp_trash_alloc)
1722 return 0;
1723
1724 ctx = EVP_CIPHER_CTX_new();
1725
1726 if (!ctx)
1727 goto err;
1728
1729 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
1730 if (dec_size < 0)
1731 goto err;
1732 smp_trash->data = dec_size;
1733
1734 /* Set cipher type and mode */
1735 switch(arg_p[0].data.sint) {
1736 case 128:
1737 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
1738 break;
1739 case 192:
1740 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
1741 break;
1742 case 256:
1743 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
1744 break;
1745 }
1746
1747 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL);
1748
1749 /* Initialise IV */
1750 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area))
1751 goto err;
1752
1753 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
1754 if (dec_size < 0)
1755 goto err;
1756 smp_trash->data = dec_size;
1757
1758 /* Initialise key */
1759 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL))
1760 goto err;
1761
1762 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
1763 (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data))
1764 goto err;
1765
1766 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
1767 if (dec_size < 0)
1768 goto err;
1769 smp_trash_alloc->data = dec_size;
1770 dec_size = smp_trash->data;
1771
1772 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area);
1773 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
1774
1775 if (ret <= 0)
1776 goto err;
1777
1778 smp->data.u.str.data = dec_size + smp_trash->data;
1779 smp->data.u.str.area = smp_trash->area;
1780 smp->data.type = SMP_T_BIN;
1781 smp->flags &= ~SMP_F_CONST;
1782 free_trash_chunk(smp_trash_alloc);
1783 return 1;
1784
1785err:
1786 free_trash_chunk(smp_trash_alloc);
1787 return 0;
1788}
1789#endif /* HA_OPENSSL_VERSION_NUMBER */
1790
Patrick Gansterer8e366512020-04-22 16:47:57 +02001791static int check_crypto_digest(struct arg *args, struct sample_conv *conv,
1792 const char *file, int line, char **err)
1793{
1794 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1795
1796 if (evp)
1797 return 1;
1798
1799 memprintf(err, "algorithm must be a valid OpenSSL message digest name.");
1800 return 0;
1801}
1802
1803static int sample_conv_crypto_digest(const struct arg *args, struct sample *smp, void *private)
1804{
1805 struct buffer *trash = get_trash_chunk();
1806 unsigned char *md = (unsigned char*) trash->area;
1807 unsigned int md_len = trash->size;
1808 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
1809 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1810
1811 if (!ctx)
1812 return 0;
1813
1814 if (!EVP_DigestInit_ex(ctx, evp, NULL) ||
1815 !EVP_DigestUpdate(ctx, smp->data.u.str.area, smp->data.u.str.data) ||
1816 !EVP_DigestFinal_ex(ctx, md, &md_len)) {
1817 EVP_MD_CTX_free(ctx);
1818 return 0;
1819 }
1820
1821 EVP_MD_CTX_free(ctx);
1822
1823 trash->data = md_len;
1824 smp->data.u.str = *trash;
1825 smp->data.type = SMP_T_BIN;
1826 smp->flags &= ~SMP_F_CONST;
1827 return 1;
1828}
1829
1830static int check_crypto_hmac(struct arg *args, struct sample_conv *conv,
1831 const char *file, int line, char **err)
1832{
1833 if (!check_crypto_digest(args, conv, file, line, err))
1834 return 0;
1835
1836 vars_check_arg(&args[1], NULL);
1837 return 1;
1838}
1839
1840static int sample_conv_crypto_hmac(const struct arg *args, struct sample *smp, void *private)
1841{
1842 struct sample key;
1843 struct buffer *trash, *key_trash;
1844 unsigned char *md;
1845 unsigned int md_len;
1846 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1847 int dec_size;
1848
1849 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
1850 if (!sample_conv_var2smp_str(&args[1], &key))
1851 return 0;
1852
1853 trash = get_trash_chunk();
1854 key_trash = alloc_trash_chunk();
1855 if (!key_trash)
1856 return 0;
1857
1858 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, key_trash->area, key_trash->size);
1859 if (dec_size < 0)
1860 goto err;
1861
1862 md = (unsigned char*) trash->area;
1863 md_len = trash->size;
1864 if (!HMAC(evp, key_trash->area, dec_size, (const unsigned char*) smp->data.u.str.area, smp->data.u.str.data, md, &md_len))
1865 goto err;
1866
1867 free_trash_chunk(key_trash);
1868
1869 trash->data = md_len;
1870 smp->data.u.str = *trash;
1871 smp->data.type = SMP_T_BIN;
1872 smp->flags &= ~SMP_F_CONST;
1873 return 1;
1874
1875err:
1876 free_trash_chunk(key_trash);
1877 return 0;
1878}
1879
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001880#endif /* USE_OPENSSL */
Tim Duesterhusd4376302019-06-17 12:41:44 +02001881
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001882static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001883{
Willy Tarreau83061a82018-07-13 11:56:34 +02001884 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001885 unsigned char c;
1886 int ptr = 0;
1887
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001888 trash->data = 0;
1889 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
1890 c = smp->data.u.str.area[ptr++];
1891 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1892 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001893 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001894 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001895 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001896 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001897 return 1;
1898}
1899
Dragan Dosen3f957b22017-10-24 09:27:34 +02001900static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1901{
1902 long long int n = 0;
1903 int i, c;
1904
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001905 for (i = 0; i < smp->data.u.str.data; i++) {
1906 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02001907 return 0;
1908 n = (n << 4) + c;
1909 }
1910
1911 smp->data.u.sint = n;
1912 smp->data.type = SMP_T_SINT;
1913 smp->flags &= ~SMP_F_CONST;
1914 return 1;
1915}
1916
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001917/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001918static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001919{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001920 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
1921 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001922 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001923 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001924 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001925 return 1;
1926}
1927
Willy Tarreau60a2ee72017-12-15 07:13:48 +01001928static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001929{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001930 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001931 smp->data.u.sint = i;
1932 smp->data.type = SMP_T_SINT;
1933 return 1;
1934}
1935
1936
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001937static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001938{
1939 int i;
1940
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001941 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001942 return 0;
1943
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001944 for (i = 0; i < smp->data.u.str.data; i++) {
1945 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
1946 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001947 }
1948 return 1;
1949}
1950
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001951static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001952{
1953 int i;
1954
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001955 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001956 return 0;
1957
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001958 for (i = 0; i < smp->data.u.str.data; i++) {
1959 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
1960 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001961 }
1962 return 1;
1963}
1964
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001965/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
1966static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001967{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001968 /* Attempt to convert to IPv4 to apply the correct mask. */
1969 c_ipv62ip(smp);
1970
1971 if (smp->data.type == SMP_T_IPV4) {
1972 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
1973 smp->data.type = SMP_T_IPV4;
1974 }
1975 else if (smp->data.type == SMP_T_IPV6) {
1976 /* IPv6 cannot be converted without an IPv6 mask. */
1977 if (args[1].type != ARGT_IPV6)
1978 return 0;
1979
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +01001980 write_u64(&smp->data.u.ipv6.s6_addr[0],
1981 read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0]));
1982 write_u64(&smp->data.u.ipv6.s6_addr[8],
1983 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 +01001984 smp->data.type = SMP_T_IPV6;
1985 }
1986
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001987 return 1;
1988}
1989
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001990/* takes an UINT value on input supposed to represent the time since EPOCH,
1991 * adds an optional offset found in args[1] and emits a string representing
1992 * the local time in the format specified in args[1] using strftime().
1993 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001994static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001995{
Willy Tarreau83061a82018-07-13 11:56:34 +02001996 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001997 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001998 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001999 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002000
2001 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002002 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002003 curr_date += args[1].data.sint;
2004
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002005 tm = localtime(&curr_date);
2006 if (!tm)
2007 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002008 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002009 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
2010 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002011 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002012 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002013 return 1;
2014}
2015
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002016/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002017static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002018{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002019 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
2020 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002021 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002022 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002023 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002024 return 1;
2025}
2026
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002027/* takes an UINT value on input supposed to represent the time since EPOCH,
2028 * adds an optional offset found in args[1] and emits a string representing
2029 * the UTC date in the format specified in args[1] using strftime().
2030 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002031static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002032{
Willy Tarreau83061a82018-07-13 11:56:34 +02002033 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002034 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002035 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002036 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002037
2038 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002039 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002040 curr_date += args[1].data.sint;
2041
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002042 tm = gmtime(&curr_date);
2043 if (!tm)
2044 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002045 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002046 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
2047 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002048 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002049 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002050 return 1;
2051}
2052
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002053/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002054static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002055{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002056 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
2057 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002058 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002059 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002060 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002061 return 1;
2062}
2063
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002064/* hashes the binary input into a 32-bit unsigned int using xxh.
2065 * The seed of the hash defaults to 0 but can be changd in argument 1.
2066 */
2067static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
2068{
2069 unsigned int seed;
2070
2071 if (arg_p && arg_p->data.sint)
2072 seed = arg_p->data.sint;
2073 else
2074 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002075 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
2076 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002077 smp->data.type = SMP_T_SINT;
2078 return 1;
2079}
2080
2081/* hashes the binary input into a 64-bit unsigned int using xxh.
2082 * In fact, the function returns a 64 bit unsigned, but the sample
2083 * storage of haproxy only proposes 64-bits signed, so the value is
2084 * cast as signed. This cast doesn't impact the hash repartition.
2085 * The seed of the hash defaults to 0 but can be changd in argument 1.
2086 */
2087static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
2088{
2089 unsigned long long int seed;
2090
2091 if (arg_p && arg_p->data.sint)
2092 seed = (unsigned long long int)arg_p->data.sint;
2093 else
2094 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002095 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
2096 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002097 smp->data.type = SMP_T_SINT;
2098 return 1;
2099}
2100
Willy Tarreau80599772015-01-20 19:35:24 +01002101/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002102static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01002103{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002104 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
2105 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002106 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002107 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002108 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01002109 return 1;
2110}
2111
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002112/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
2113static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
2114{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002115 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
2116 smp->data.u.str.data);
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002117 if (arg_p && arg_p->data.sint)
2118 smp->data.u.sint = full_hash(smp->data.u.sint);
2119 smp->data.type = SMP_T_SINT;
2120 return 1;
2121}
2122
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002123/* This function escape special json characters. The returned string can be
2124 * safely set between two '"' and used as json string. The json string is
2125 * defined like this:
2126 *
2127 * any Unicode character except '"' or '\' or control character
2128 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
2129 *
2130 * The enum input_type contain all the allowed mode for decoding the input
2131 * string.
2132 */
2133enum input_type {
2134 IT_ASCII = 0,
2135 IT_UTF8,
2136 IT_UTF8S,
2137 IT_UTF8P,
2138 IT_UTF8PS,
2139};
2140static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
2141 const char *file, int line, char **err)
2142{
2143 if (!arg) {
2144 memprintf(err, "Unexpected empty arg list");
2145 return 0;
2146 }
2147
2148 if (arg->type != ARGT_STR) {
2149 memprintf(err, "Unexpected arg type");
2150 return 0;
2151 }
2152
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002153 if (strcmp(arg->data.str.area, "") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002154 arg->type = ARGT_SINT;
2155 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002156 return 1;
2157 }
2158
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002159 else if (strcmp(arg->data.str.area, "ascii") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002160 arg->type = ARGT_SINT;
2161 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002162 return 1;
2163 }
2164
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002165 else if (strcmp(arg->data.str.area, "utf8") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002166 arg->type = ARGT_SINT;
2167 arg->data.sint = IT_UTF8;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002168 return 1;
2169 }
2170
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002171 else if (strcmp(arg->data.str.area, "utf8s") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002172 arg->type = ARGT_SINT;
2173 arg->data.sint = IT_UTF8S;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002174 return 1;
2175 }
2176
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002177 else if (strcmp(arg->data.str.area, "utf8p") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002178 arg->type = ARGT_SINT;
2179 arg->data.sint = IT_UTF8P;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002180 return 1;
2181 }
2182
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002183 else if (strcmp(arg->data.str.area, "utf8ps") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002184 arg->type = ARGT_SINT;
2185 arg->data.sint = IT_UTF8PS;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002186 return 1;
2187 }
2188
Thierry FOURNIER / OZON.IOa69c9122016-11-23 01:13:57 +01002189 memprintf(err, "Unexpected input code type. "
2190 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002191 return 0;
2192}
2193
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002194static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002195{
Willy Tarreau83061a82018-07-13 11:56:34 +02002196 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002197 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
2198 const char *str;
2199 int len;
2200 enum input_type input_type = IT_ASCII;
2201 unsigned int c;
2202 unsigned int ret;
2203 char *p;
2204
2205 if (arg_p)
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002206 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002207
2208 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002209 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002210
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002211 p = smp->data.u.str.area;
2212 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002213
2214 if (input_type == IT_ASCII) {
2215 /* Read input as ASCII. */
2216 c = *(unsigned char *)p;
2217 p++;
2218 }
2219 else {
2220 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002221 ret = utf8_next(p,
2222 smp->data.u.str.data - ( p - smp->data.u.str.area),
2223 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002224 p += utf8_return_length(ret);
2225
2226 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2227 return 0;
2228 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2229 continue;
2230 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2231 return 0;
2232 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2233 continue;
2234
2235 /* Check too big values. */
2236 if ((unsigned int)c > 0xffff) {
2237 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2238 return 0;
2239 continue;
2240 }
2241 }
2242
2243 /* Convert character. */
2244 if (c == '"') {
2245 len = 2;
2246 str = "\\\"";
2247 }
2248 else if (c == '\\') {
2249 len = 2;
2250 str = "\\\\";
2251 }
2252 else if (c == '/') {
2253 len = 2;
2254 str = "\\/";
2255 }
2256 else if (c == '\b') {
2257 len = 2;
2258 str = "\\b";
2259 }
2260 else if (c == '\f') {
2261 len = 2;
2262 str = "\\f";
2263 }
2264 else if (c == '\r') {
2265 len = 2;
2266 str = "\\r";
2267 }
2268 else if (c == '\n') {
2269 len = 2;
2270 str = "\\n";
2271 }
2272 else if (c == '\t') {
2273 len = 2;
2274 str = "\\t";
2275 }
Willy Tarreau90807112020-02-25 08:16:33 +01002276 else if (c > 0xff || !isprint((unsigned char)c)) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002277 /* isprint generate a segfault if c is too big. The man says that
2278 * c must have the value of an unsigned char or EOF.
2279 */
2280 len = 6;
2281 _str[0] = '\\';
2282 _str[1] = 'u';
2283 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2284 str = _str;
2285 }
2286 else {
2287 len = 1;
Willy Tarreau5715da22020-02-25 08:37:37 +01002288 _str[0] = c;
2289 str = _str;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002290 }
2291
2292 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002293 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002294 return 0;
2295
2296 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002297 memcpy(temp->area + temp->data, str, len);
2298 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002299 }
2300
2301 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002302 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002303 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002304
2305 return 1;
2306}
2307
Emeric Brun54c4ac82014-11-03 15:32:43 +01002308/* This sample function is designed to extract some bytes from an input buffer.
2309 * First arg is the offset.
2310 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002311static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002312{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002313 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2314 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002315 return 1;
2316 }
2317
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002318 if (smp->data.u.str.size)
2319 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002320 smp->data.u.str.data -= arg_p[0].data.sint;
2321 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002322
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002323 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2324 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002325
2326 return 1;
2327}
2328
Emeric Brunf399b0d2014-11-03 17:07:03 +01002329static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2330 const char *file, int line, char **err)
2331{
2332 struct arg *arg = args;
2333
2334 if (!arg) {
2335 memprintf(err, "Unexpected empty arg list");
2336 return 0;
2337 }
2338
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002339 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002340 memprintf(err, "Unexpected arg type");
2341 return 0;
2342 }
2343
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002344 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002345 memprintf(err, "Unexpected value 0 for index");
2346 return 0;
2347 }
2348
2349 arg++;
2350
2351 if (arg->type != ARGT_STR) {
2352 memprintf(err, "Unexpected arg type");
2353 return 0;
2354 }
2355
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002356 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002357 memprintf(err, "Empty separators list");
2358 return 0;
2359 }
2360
2361 return 1;
2362}
2363
2364/* This sample function is designed to a return selected part of a string (field).
2365 * First arg is the index of the field (start at 1)
2366 * Second arg is a char list of separators (type string)
2367 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002368static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002369{
Marcin Deranek9631a282018-04-16 14:30:46 +02002370 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002371 char *start, *end;
2372 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002373 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002374
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002375 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002376 return 0;
2377
Marcin Deranek9631a282018-04-16 14:30:46 +02002378 if (arg_p[0].data.sint < 0) {
2379 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002380 end = start = smp->data.u.str.area + smp->data.u.str.data;
2381 while (start > smp->data.u.str.area) {
2382 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2383 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002384 if (field == arg_p[0].data.sint) {
2385 if (count == 1)
2386 goto found;
2387 else if (count > 1)
2388 count--;
2389 } else {
2390 end = start-1;
2391 field--;
2392 }
2393 break;
2394 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002395 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002396 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002397 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002398 } else {
2399 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002400 end = start = smp->data.u.str.area;
2401 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2402 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2403 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002404 if (field == arg_p[0].data.sint) {
2405 if (count == 1)
2406 goto found;
2407 else if (count > 1)
2408 count--;
2409 } else {
2410 start = end+1;
2411 field++;
2412 }
2413 break;
2414 }
2415 }
2416 end++;
2417 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002418 }
2419
2420 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002421 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002422 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002423 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002424 }
2425found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002426 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002427 /* If ret string is len 0, no need to
2428 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002429 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002430 return 1;
2431
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002432 smp->data.u.str.area = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002433
2434 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002435 Note: smp->data.u.str.size cannot be set to 0 */
2436 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002437 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002438
2439 return 1;
2440}
2441
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002442/* This sample function is designed to return a word from a string.
2443 * First arg is the index of the word (start at 1)
2444 * Second arg is a char list of words separators (type string)
2445 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002446static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002447{
Marcin Deranek9631a282018-04-16 14:30:46 +02002448 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002449 char *start, *end;
2450 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002451 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002452
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002453 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002454 return 0;
2455
2456 word = 0;
2457 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002458 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002459 end = start = smp->data.u.str.area + smp->data.u.str.data;
2460 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002461 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002462 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2463 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002464 issep = 1;
2465 break;
2466 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002467 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002468 if (!inword) {
2469 if (!issep) {
2470 if (word != arg_p[0].data.sint) {
2471 word--;
2472 end = start;
2473 }
2474 inword = 1;
2475 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002476 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002477 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002478 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002479 if (count == 1)
2480 goto found;
2481 else if (count > 1)
2482 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002483 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002484 inword = 0;
2485 }
2486 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002487 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002488 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002489 end = start = smp->data.u.str.area;
2490 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002491 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002492 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2493 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002494 issep = 1;
2495 break;
2496 }
2497 }
2498 if (!inword) {
2499 if (!issep) {
2500 if (word != arg_p[0].data.sint) {
2501 word++;
2502 start = end;
2503 }
2504 inword = 1;
2505 }
2506 }
2507 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002508 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002509 if (count == 1)
2510 goto found;
2511 else if (count > 1)
2512 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002513 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002514 inword = 0;
2515 }
2516 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002517 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002518 }
2519
2520 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002521 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002522 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002523 return 1;
2524 }
2525found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002526 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002527 /* If ret string is len 0, no need to
2528 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002529 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002530 return 1;
2531
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002532 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002533
2534 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002535 Note: smp->data.u.str.size cannot be set to 0 */
2536 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002537 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002538
2539 return 1;
2540}
2541
Willy Tarreau7eda8492015-01-20 19:47:06 +01002542static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2543 const char *file, int line, char **err)
2544{
2545 struct arg *arg = args;
2546 char *p;
2547 int len;
2548
2549 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2550 arg[0].type_flags = 0;
2551
2552 if (arg[2].type != ARGT_STR)
2553 return 1;
2554
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002555 p = arg[2].data.str.area;
2556 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002557 while (len) {
2558 if (*p == 'i') {
2559 arg[0].type_flags |= ARGF_REG_ICASE;
2560 }
2561 else if (*p == 'g') {
2562 arg[0].type_flags |= ARGF_REG_GLOB;
2563 }
2564 else {
2565 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2566 return 0;
2567 }
2568 p++;
2569 len--;
2570 }
2571 return 1;
2572}
2573
2574/* This sample function is designed to do the equivalent of s/match/replace/ on
2575 * the input string. It applies a regex and restarts from the last matched
2576 * location until nothing matches anymore. First arg is the regex to apply to
2577 * the input string, second arg is the replacement expression.
2578 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002579static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002580{
2581 char *start, *end;
2582 struct my_regex *reg = arg_p[0].data.reg;
2583 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002584 struct buffer *trash = get_trash_chunk();
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002585 struct buffer *output;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002586 int flag, max;
2587 int found;
2588
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002589 start = smp->data.u.str.area;
2590 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002591
2592 flag = 0;
2593 while (1) {
2594 /* check for last round which is used to copy remaining parts
2595 * when not running in global replacement mode.
2596 */
2597 found = 0;
2598 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2599 /* Note: we can have start == end on empty strings or at the end */
2600 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2601 }
2602
2603 if (!found)
2604 pmatch[0].rm_so = end - start;
2605
2606 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002607 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002608 if (max && pmatch[0].rm_so > 0) {
2609 if (max > pmatch[0].rm_so)
2610 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002611 memcpy(trash->area + trash->data, start, max);
2612 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002613 }
2614
2615 if (!found)
2616 break;
2617
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002618 output = alloc_trash_chunk();
Willy Tarreau23997da2020-02-18 14:27:44 +01002619 if (!output)
2620 break;
2621
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002622 output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2623
Willy Tarreau7eda8492015-01-20 19:47:06 +01002624 /* replace the matching part */
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002625 max = output->size - output->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002626 if (max) {
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002627 if (max > output->data)
2628 max = output->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002629 memcpy(trash->area + trash->data,
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002630 output->area, max);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002631 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002632 }
2633
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002634 free_trash_chunk(output);
2635
Willy Tarreau7eda8492015-01-20 19:47:06 +01002636 /* stop here if we're done with this string */
2637 if (start >= end)
2638 break;
2639
2640 /* We have a special case for matches of length 0 (eg: "x*y*").
2641 * These ones are considered to match in front of a character,
2642 * so we have to copy that character and skip to the next one.
2643 */
2644 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002645 if (trash->data < trash->size)
2646 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002647 pmatch[0].rm_eo++;
2648 }
2649
2650 start += pmatch[0].rm_eo;
2651 flag |= REG_NOTBOL;
2652 }
2653
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002654 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002655 return 1;
2656}
2657
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002658/* This function check an operator entry. It expects a string.
2659 * The string can be an integer or a variable name.
2660 */
2661static int check_operator(struct arg *args, struct sample_conv *conv,
2662 const char *file, int line, char **err)
2663{
2664 const char *str;
2665 const char *end;
2666
2667 /* Try to decode a variable. */
2668 if (vars_check_arg(&args[0], NULL))
2669 return 1;
2670
2671 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002672 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002673 end = str + strlen(str);
2674 args[0].data.sint = read_int64(&str, end);
2675 if (*str != '\0') {
2676 memprintf(err, "expects an integer or a variable name");
2677 return 0;
2678 }
2679 args[0].type = ARGT_SINT;
2680 return 1;
2681}
2682
Willy Tarreau6204cd92016-03-10 16:33:04 +01002683/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002684 * If the arg contain an integer, the integer is returned in the
2685 * sample. If the arg contains a variable descriptor, it returns the
2686 * variable value.
2687 *
2688 * This function returns 0 if an error occurs, otherwise it returns 1.
2689 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002690static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002691{
2692 switch (arg->type) {
2693 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002694 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002695 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002696 return 1;
2697 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002698 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002699 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002700 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002701 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002702 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002703 return 0;
2704 return 1;
2705 default:
2706 return 0;
2707 }
2708}
2709
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002710/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002711 * result.
2712 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002713static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002714{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002715 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002716 return 1;
2717}
2718
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002719/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002720 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002721 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002722static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002723{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002724 struct sample tmp;
2725
Willy Tarreau7560dd42016-03-10 16:28:58 +01002726 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002727 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002728 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002729 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002730 return 1;
2731}
2732
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002733/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002734 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002735 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002736static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002737{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002738 struct sample tmp;
2739
Willy Tarreau7560dd42016-03-10 16:28:58 +01002740 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002741 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002742 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002743 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002744 return 1;
2745}
2746
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002747/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002748 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002749 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002750static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002751{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002752 struct sample tmp;
2753
Willy Tarreau7560dd42016-03-10 16:28:58 +01002754 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002755 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002756 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002757 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002758 return 1;
2759}
2760
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002761static inline long long int arith_add(long long int a, long long int b)
2762{
2763 /* Prevent overflow and makes capped calculus.
2764 * We must ensure that the check calculus doesn't
2765 * exceed the signed 64 bits limits.
2766 *
2767 * +----------+----------+
2768 * | a<0 | a>=0 |
2769 * +------+----------+----------+
2770 * | b<0 | MIN-a>b | no check |
2771 * +------+----------+----------+
2772 * | b>=0 | no check | MAX-a<b |
2773 * +------+----------+----------+
2774 */
2775 if ((a ^ b) >= 0) {
2776 /* signs are differents. */
2777 if (a < 0) {
2778 if (LLONG_MIN - a > b)
2779 return LLONG_MIN;
2780 }
2781 if (LLONG_MAX - a < b)
2782 return LLONG_MAX;
2783 }
2784 return a + b;
2785}
2786
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002787/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002788 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002789 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002790static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002791{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002792 struct sample tmp;
2793
Willy Tarreau7560dd42016-03-10 16:28:58 +01002794 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002795 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002796 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002797 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002798 return 1;
2799}
2800
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002801/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002802 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002803 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002804static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002805 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002806{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002807 struct sample tmp;
2808
Willy Tarreau7560dd42016-03-10 16:28:58 +01002809 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002810 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002811 return 0;
2812
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002813 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2814 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2815 * of -LLONG_MIN and correct the result.
2816 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002817 if (tmp.data.u.sint == LLONG_MIN) {
2818 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2819 if (smp->data.u.sint < LLONG_MAX)
2820 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002821 return 1;
2822 }
2823
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002824 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002825 * the second operand.
2826 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002827 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002828 return 1;
2829}
2830
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002831/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002832 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002833 * If the result makes an overflow, then the largest possible quantity is
2834 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002835 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002836static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002837 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002838{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002839 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002840 long long int c;
2841
Willy Tarreau7560dd42016-03-10 16:28:58 +01002842 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002843 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002844 return 0;
2845
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002846 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002847 if (!smp->data.u.sint || !tmp.data.u.sint) {
2848 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002849 return 1;
2850 }
2851
2852 /* The multiply between LLONG_MIN and -1 returns a
2853 * "floting point exception".
2854 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002855 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2856 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002857 return 1;
2858 }
2859
2860 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002861 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002862
2863 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002864 if (smp->data.u.sint != c / tmp.data.u.sint) {
2865 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2866 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002867 return 1;
2868 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002869 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002870 return 1;
2871 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002872 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002873 return 1;
2874}
2875
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002876/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002877 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002878 * If arg_p makes the result overflow, then the largest possible quantity is
2879 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002880 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002881static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002882 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002883{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002884 struct sample tmp;
2885
Willy Tarreau7560dd42016-03-10 16:28:58 +01002886 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002887 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002888 return 0;
2889
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002890 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002891 /* The divide between LLONG_MIN and -1 returns a
2892 * "floting point exception".
2893 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002894 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2895 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002896 return 1;
2897 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002898 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002899 return 1;
2900 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002901 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002902 return 1;
2903}
2904
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002905/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002906 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002907 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002908 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002909static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002910 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002911{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002912 struct sample tmp;
2913
Willy Tarreau7560dd42016-03-10 16:28:58 +01002914 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002915 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002916 return 0;
2917
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002918 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002919 /* The divide between LLONG_MIN and -1 returns a
2920 * "floting point exception".
2921 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002922 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2923 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002924 return 1;
2925 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002926 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002927 return 1;
2928 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002929 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002930 return 1;
2931}
2932
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002933/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002934 * result.
2935 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002936static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002937 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002938{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002939 if (smp->data.u.sint == LLONG_MIN)
2940 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002941 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002942 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002943 return 1;
2944}
2945
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002946/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002947 * false. The output is a BOOL.
2948 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002949static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002950 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002951{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002952 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002953 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002954 return 1;
2955}
2956
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002957/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002958 * truee. The output is a BOOL.
2959 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002960static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002961 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002962{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002963 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002964 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002965 return 1;
2966}
2967
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002968/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002969 * The output is a BOOL.
2970 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002971static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002972 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002973{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002974 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002975 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002976 return 1;
2977}
2978
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002979/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002980 * The output is a BOOL.
2981 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002982static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002983 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002984{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002985 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002986 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002987 return 1;
2988}
2989
Willy Tarreau280f42b2018-02-19 15:34:12 +01002990/* appends an optional const string, an optional variable contents and another
2991 * optional const string to an existing string.
2992 */
2993static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
2994{
Willy Tarreau83061a82018-07-13 11:56:34 +02002995 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002996 struct sample tmp;
2997 int max;
2998
2999 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003000 trash->data = smp->data.u.str.data;
3001 if (trash->data > trash->size - 1)
3002 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003003
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003004 memcpy(trash->area, smp->data.u.str.area, trash->data);
3005 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003006
3007 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003008 max = arg_p[0].data.str.data;
3009 if (max > trash->size - 1 - trash->data)
3010 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003011
3012 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003013 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
3014 trash->data += max;
3015 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003016 }
3017
3018 /* append second string (variable) if it's found and we can turn it
3019 * into a string.
3020 */
3021 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3022 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
3023 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
3024 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
3025
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003026 max = tmp.data.u.str.data;
3027 if (max > trash->size - 1 - trash->data)
3028 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003029
3030 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003031 memcpy(trash->area + trash->data, tmp.data.u.str.area,
3032 max);
3033 trash->data += max;
3034 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003035 }
3036 }
3037
3038 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003039 max = arg_p[2].data.str.data;
3040 if (max > trash->size - 1 - trash->data)
3041 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003042
3043 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003044 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
3045 trash->data += max;
3046 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003047 }
3048
3049 smp->data.u.str = *trash;
3050 smp->data.type = SMP_T_STR;
3051 return 1;
3052}
3053
3054/* This function checks the "concat" converter's arguments and extracts the
3055 * variable name and its scope.
3056 */
3057static int smp_check_concat(struct arg *args, struct sample_conv *conv,
3058 const char *file, int line, char **err)
3059{
3060 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003061 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
3062 memprintf(err, "failed to register variable name '%s'",
3063 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003064 return 0;
3065 }
3066 return 1;
3067}
3068
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003069/* Compares string with a variable containing a string. Return value
Tim Duesterhusca097c12018-04-27 21:18:45 +02003070 * is compatible with strcmp(3)'s return value.
3071 */
3072static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
3073{
3074 struct sample tmp;
3075 int max, result;
3076
3077 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3078 if (arg_p[0].type != ARGT_VAR)
3079 return 0;
3080 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
3081 return 0;
3082 if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp))
3083 return 0;
3084
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003085 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
3086 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003087 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003088 if (smp->data.u.str.data != tmp.data.u.str.data) {
3089 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02003090 result = -1;
3091 }
3092 else {
3093 result = 1;
3094 }
3095 }
3096 }
3097
3098 smp->data.u.sint = result;
3099 smp->data.type = SMP_T_SINT;
3100 return 1;
3101}
3102
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003103#ifdef USE_OPENSSL
3104/* Compares bytestring with a variable containing a bytestring. Return value
3105 * is `true` if both bytestrings are bytewise identical and `false` otherwise.
3106 *
3107 * Comparison will be performed in constant time if both bytestrings are of
3108 * the same length. If the lengths differ execution time will not be constant.
3109 */
3110static int sample_conv_secure_memcmp(const struct arg *arg_p, struct sample *smp, void *private)
3111{
3112 struct sample tmp;
3113 int result;
3114
3115 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3116 if (arg_p[0].type != ARGT_VAR)
3117 return 0;
3118 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
3119 return 0;
3120 if (!sample_casts[tmp.data.type][SMP_T_BIN](&tmp))
3121 return 0;
3122
3123 if (smp->data.u.str.data != tmp.data.u.str.data) {
3124 smp->data.u.sint = 0;
3125 smp->data.type = SMP_T_BOOL;
3126 return 1;
3127 }
3128
3129 /* The following comparison is performed in constant time. */
3130 result = CRYPTO_memcmp(smp->data.u.str.area, tmp.data.u.str.area, smp->data.u.str.data);
3131
3132 smp->data.u.sint = result == 0;
3133 smp->data.type = SMP_T_BOOL;
3134 return 1;
3135}
3136#endif
3137
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003138#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
3139#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
3140#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
3141
3142/*
3143 * Extract the field value of an input binary sample. Takes a mandatory argument:
3144 * the protocol buffers field identifier (dotted notation) internally represented
3145 * as an array of unsigned integers and its size.
3146 * Return 1 if the field was found, 0 if not.
3147 */
3148static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
3149{
3150 unsigned char *pos;
3151 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003152
3153 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003154 grpc_left = smp->data.u.str.data;
3155
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01003156 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003157 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003158
3159 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
3160
3161 pos += GRPC_MSG_HEADER_SZ;
3162 grpc_left -= GRPC_MSG_HEADER_SZ;
3163
3164 if (grpc_left < left)
3165 return 0;
3166
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01003167 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
3168 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003169
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003170 grpc_left -= grpc_msg_len;
3171 }
3172
3173 return 0;
3174}
3175
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003176static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
3177{
3178 unsigned char *pos;
3179 size_t left;
3180
3181 pos = (unsigned char *)smp->data.u.str.area;
3182 left = smp->data.u.str.data;
3183
3184 return protobuf_field_lookup(arg_p, smp, &pos, &left);
3185}
3186
3187static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
3188 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003189{
3190 if (!args[1].type) {
3191 args[1].type = ARGT_SINT;
3192 args[1].data.sint = PBUF_T_BINARY;
3193 }
3194 else {
3195 int pbuf_type;
3196
3197 pbuf_type = protobuf_type(args[1].data.str.area);
3198 if (pbuf_type == -1) {
3199 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
3200 return 0;
3201 }
3202
3203 args[1].type = ARGT_SINT;
3204 args[1].data.sint = pbuf_type;
3205 }
3206
3207 return 1;
3208}
3209
Tim Duesterhusca097c12018-04-27 21:18:45 +02003210/* This function checks the "strcmp" converter's arguments and extracts the
3211 * variable name and its scope.
3212 */
3213static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
3214 const char *file, int line, char **err)
3215{
3216 /* Try to decode a variable. */
3217 if (vars_check_arg(&args[0], NULL))
3218 return 1;
3219
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003220 memprintf(err, "failed to register variable name '%s'",
3221 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003222 return 0;
3223}
3224
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003225#ifdef USE_OPENSSL
3226/* This function checks the "secure_memcmp" converter's arguments and extracts the
3227 * variable name and its scope.
3228 */
3229static int smp_check_secure_memcmp(struct arg *args, struct sample_conv *conv,
3230 const char *file, int line, char **err)
3231{
3232 /* Try to decode a variable. */
3233 if (vars_check_arg(&args[0], NULL))
3234 return 1;
3235
3236 memprintf(err, "failed to register variable name '%s'",
3237 args[0].data.str.area);
3238 return 0;
3239}
3240#endif
3241
Christopher Faulet4ccc12f2020-04-01 09:08:32 +02003242/**/
3243static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private)
3244{
3245 struct buffer *tmp;
3246 uint32_t n;
3247
3248 n = htonl((uint32_t)smp->data.u.sint);
3249 tmp = get_trash_chunk();
3250
3251 memcpy(b_head(tmp), &n, 4);
3252 b_add(tmp, 4);
3253
3254 smp->data.u.str = *tmp;
3255 smp->data.type = SMP_T_BIN;
3256 return 1;
3257}
3258
Christopher Fauletea159d62020-04-01 16:21:44 +02003259/**/
3260static int sample_conv_cut_crlf(const struct arg *arg_p, struct sample *smp, void *private)
3261{
3262 char *p;
3263 size_t l;
3264
3265 p = smp->data.u.str.area;
3266 for (l = 0; l < smp->data.u.str.data; l++) {
3267 if (*(p+l) == '\r' || *(p+l) == '\n')
3268 break;
3269 }
3270 smp->data.u.str.data = l;
3271 return 1;
3272}
3273
Christopher Faulet51fc9d12020-04-01 17:24:41 +02003274/**/
3275static int sample_conv_ltrim(const struct arg *arg_p, struct sample *smp, void *private)
3276{
3277 char *delimiters, *p;
3278 size_t dlen, l;
3279
3280 delimiters = arg_p[0].data.str.area;
3281 dlen = arg_p[0].data.str.data;
3282
3283 l = smp->data.u.str.data;
3284 p = smp->data.u.str.area;
3285 while (l && memchr(delimiters, *p, dlen) != NULL) {
3286 p++;
3287 l--;
3288 }
3289
3290 smp->data.u.str.area = p;
3291 smp->data.u.str.data = l;
3292 return 1;
3293}
3294
Christopher Faulet568415a2020-04-01 17:24:47 +02003295/**/
3296static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *private)
3297{
3298 char *delimiters, *p;
3299 size_t dlen, l;
3300
3301 delimiters = arg_p[0].data.str.area;
3302 dlen = arg_p[0].data.str.data;
3303
3304 l = smp->data.u.str.data;
3305 p = smp->data.u.str.area + l - 1;
3306 while (l && memchr(delimiters, *p, dlen) != NULL) {
3307 p--;
3308 l--;
3309 }
3310
3311 smp->data.u.str.data = l;
3312 return 1;
3313}
3314
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003315/************************************************************************/
3316/* All supported sample fetch functions must be declared here */
3317/************************************************************************/
3318
3319/* force TRUE to be returned at the fetch level */
3320static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003321smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003322{
Willy Tarreau280f42b2018-02-19 15:34:12 +01003323 if (!smp_make_rw(smp))
3324 return 0;
3325
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003326 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003327 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003328 return 1;
3329}
3330
3331/* force FALSE to be returned at the fetch level */
3332static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003333smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003334{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003335 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003336 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003337 return 1;
3338}
3339
3340/* retrieve environment variable $1 as a string */
3341static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003342smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003343{
3344 char *env;
3345
3346 if (!args || args[0].type != ARGT_STR)
3347 return 0;
3348
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003349 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003350 if (!env)
3351 return 0;
3352
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003353 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003354 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003355 smp->data.u.str.area = env;
3356 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003357 return 1;
3358}
3359
Damien Claisseae6f1252019-10-30 15:57:28 +00003360/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3361 * optional string representing the unit of the result: "s" for seconds, "ms" for
3362 * milliseconds and "us" for microseconds.
3363 * Returns 0 on error and non-zero if OK.
3364 */
3365int smp_check_date_unit(struct arg *args, char **err)
3366{
3367 if (args[1].type == ARGT_STR) {
3368 if (strcmp(args[1].data.str.area, "s") == 0) {
3369 args[1].data.sint = TIME_UNIT_S;
3370 }
3371 else if (strcmp(args[1].data.str.area, "ms") == 0) {
3372 args[1].data.sint = TIME_UNIT_MS;
3373 }
3374 else if (strcmp(args[1].data.str.area, "us") == 0) {
3375 args[1].data.sint = TIME_UNIT_US;
3376 }
3377 else {
3378 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
3379 args[1].data.str.area);
3380 return 0;
3381 }
3382 free(args[1].data.str.area);
3383 args[1].data.str.area = NULL;
3384 args[1].type = ARGT_SINT;
3385 }
3386 else if (args[1].type != ARGT_STOP) {
3387 memprintf(err, "Unexpected arg type");
3388 return 0;
3389 }
3390
3391 return 1;
3392}
3393
3394/* retrieve the current local date in epoch time, converts it to milliseconds
3395 * or microseconds if asked to in optional args[1] unit param, and applies an
3396 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003397 */
3398static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003399smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003400{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003401 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003402
Damien Claisseae6f1252019-10-30 15:57:28 +00003403 /* report in milliseconds */
3404 if (args && args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
3405 smp->data.u.sint *= 1000;
3406 smp->data.u.sint += date.tv_usec / 1000;
3407 }
3408 /* report in microseconds */
3409 else if (args && args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
3410 smp->data.u.sint *= 1000000;
3411 smp->data.u.sint += date.tv_usec;
3412 }
3413
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003414 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003415 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003416 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003417
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003418 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003419 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3420 return 1;
3421}
3422
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003423/* retrieve the current microsecond part of the date */
3424static int
3425smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3426{
3427 smp->data.u.sint = date.tv_usec;
3428 smp->data.type = SMP_T_SINT;
3429 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3430 return 1;
3431}
3432
3433
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003434/* returns the hostname */
3435static int
3436smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3437{
3438 smp->data.type = SMP_T_STR;
3439 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003440 smp->data.u.str.area = hostname;
3441 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003442 return 1;
3443}
3444
Willy Tarreau0f30d262014-11-24 16:02:05 +01003445/* returns the number of processes */
3446static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003447smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003448{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003449 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003450 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003451 return 1;
3452}
3453
3454/* returns the number of the current process (between 1 and nbproc */
3455static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003456smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003457{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003458 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003459 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003460 return 1;
3461}
3462
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003463/* returns the number of the current thread (between 1 and nbthread */
3464static int
3465smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3466{
3467 smp->data.type = SMP_T_SINT;
3468 smp->data.u.sint = tid;
3469 return 1;
3470}
3471
Willy Tarreau84310e22014-02-14 11:59:04 +01003472/* generate a random 32-bit integer for whatever purpose, with an optional
3473 * range specified in argument.
3474 */
3475static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003476smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01003477{
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003478 smp->data.u.sint = ha_random32();
Willy Tarreau84310e22014-02-14 11:59:04 +01003479
3480 /* reduce if needed. Don't do a modulo, use all bits! */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003481 if (args && args[0].type == ARGT_SINT)
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003482 smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32;
Willy Tarreau84310e22014-02-14 11:59:04 +01003483
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003484 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01003485 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3486 return 1;
3487}
3488
Willy Tarreau0f30d262014-11-24 16:02:05 +01003489/* returns true if the current process is stopping */
3490static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003491smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003492{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003493 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003494 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003495 return 1;
3496}
3497
Willy Tarreau70fe9442018-11-22 16:07:39 +01003498/* returns the number of calls of the current stream's process_stream() */
3499static int
3500smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
3501{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003502 if (!smp->strm)
3503 return 0;
3504
Willy Tarreau70fe9442018-11-22 16:07:39 +01003505 smp->data.type = SMP_T_SINT;
3506 smp->data.u.sint = smp->strm->task->calls;
3507 return 1;
3508}
3509
3510/* returns the average number of nanoseconds spent processing the stream per call */
3511static int
3512smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3513{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003514 if (!smp->strm)
3515 return 0;
3516
Willy Tarreau70fe9442018-11-22 16:07:39 +01003517 smp->data.type = SMP_T_SINT;
3518 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
3519 return 1;
3520}
3521
3522/* returns the total number of nanoseconds spent processing the stream */
3523static int
3524smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3525{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003526 if (!smp->strm)
3527 return 0;
3528
Willy Tarreau70fe9442018-11-22 16:07:39 +01003529 smp->data.type = SMP_T_SINT;
3530 smp->data.u.sint = smp->strm->task->cpu_time;
3531 return 1;
3532}
3533
3534/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
3535static int
3536smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3537{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003538 if (!smp->strm)
3539 return 0;
3540
Willy Tarreau70fe9442018-11-22 16:07:39 +01003541 smp->data.type = SMP_T_SINT;
3542 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
3543 return 1;
3544}
3545
3546/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
3547static int
3548smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3549{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003550 if (!smp->strm)
3551 return 0;
3552
Willy Tarreau70fe9442018-11-22 16:07:39 +01003553 smp->data.type = SMP_T_SINT;
3554 smp->data.u.sint = smp->strm->task->lat_time;
3555 return 1;
3556}
3557
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003558static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
3559{
3560 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003561 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003562 smp->data.u.str.area = args[0].data.str.area;
3563 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003564 return 1;
3565}
3566
3567static int smp_check_const_bool(struct arg *args, char **err)
3568{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003569 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
3570 strcasecmp(args[0].data.str.area, "1") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003571 args[0].type = ARGT_SINT;
3572 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003573 return 1;
3574 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003575 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
3576 strcasecmp(args[0].data.str.area, "0") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003577 args[0].type = ARGT_SINT;
3578 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003579 return 1;
3580 }
3581 memprintf(err, "Expects 'true', 'false', '0' or '1'");
3582 return 0;
3583}
3584
3585static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
3586{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003587 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003588 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003589 return 1;
3590}
3591
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003592static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003593{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003594 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003595 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003596 return 1;
3597}
3598
3599static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
3600{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003601 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003602 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003603 return 1;
3604}
3605
3606static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
3607{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003608 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003609 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003610 return 1;
3611}
3612
3613static int smp_check_const_bin(struct arg *args, char **err)
3614{
David Carlier64a16ab2016-04-08 10:37:02 +01003615 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003616 int binstrlen;
3617
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003618 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003619 return 0;
3620 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003621 args[0].data.str.area = binstr;
3622 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003623 return 1;
3624}
3625
3626static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
3627{
3628 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003629 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003630 smp->data.u.str.area = args[0].data.str.area;
3631 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003632 return 1;
3633}
3634
3635static int smp_check_const_meth(struct arg *args, char **err)
3636{
3637 enum http_meth_t meth;
3638 int i;
3639
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003640 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003641 if (meth != HTTP_METH_OTHER) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003642 args[0].type = ARGT_SINT;
3643 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003644 } else {
3645 /* Check method avalaibility. A methos is a token defined as :
3646 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
3647 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
3648 * token = 1*tchar
3649 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003650 for (i = 0; i < args[0].data.str.data; i++) {
3651 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003652 memprintf(err, "expects valid method.");
3653 return 0;
3654 }
3655 }
3656 }
3657 return 1;
3658}
3659
3660static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
3661{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003662 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003663 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003664 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003665 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003666 smp->data.u.meth.str.area = "";
3667 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003668 } else {
3669 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003670 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003671 smp->data.u.meth.str.area = args[0].data.str.area;
3672 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003673 }
3674 return 1;
3675}
3676
Luca Schimweg8a694b82019-09-10 15:42:52 +02003677// This function checks the "uuid" sample's arguments.
3678// Function won't get called when no parameter is specified (maybe a bug?)
3679static int smp_check_uuid(struct arg *args, char **err)
3680{
3681 if (!args[0].type) {
3682 args[0].type = ARGT_SINT;
3683 args[0].data.sint = 4;
3684 }
3685 else if (args[0].data.sint != 4) {
3686 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
3687 return 0;
3688 }
3689
3690 return 1;
3691}
3692
3693// Generate a RFC4122 UUID (default is v4 = fully random)
3694static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
3695{
3696 if (args[0].data.sint == 4 || !args[0].type) {
Willy Tarreauee3bcdd2020-03-08 17:48:17 +01003697 ha_generate_uuid(&trash);
Luca Schimweg8a694b82019-09-10 15:42:52 +02003698 smp->data.type = SMP_T_STR;
3699 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3700 smp->data.u.str = trash;
3701 return 1;
3702 }
3703
3704 // more implementations of other uuid formats possible here
3705 return 0;
3706}
3707
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003708/* Note: must not be declared <const> as its list will be overwritten.
3709 * Note: fetches that may return multiple types must be declared as the lowest
3710 * common denominator, the type that can be casted into all other ones. For
3711 * instance IPv4/IPv6 must be declared IPv4.
3712 */
3713static struct sample_fetch_kw_list smp_kws = {ILH, {
3714 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
3715 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003716 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Damien Claisseae6f1252019-10-30 15:57:28 +00003717 { "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 +01003718 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003719 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003720 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3721 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003722 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003723 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01003724 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003725 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Luca Schimweg8a694b82019-09-10 15:42:52 +02003726 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003727
3728 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3729 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3730 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3731 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3732 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003733
3734 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
3735 { "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 +02003736 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003737 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
3738 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
3739 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
3740 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
3741
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003742 { /* END */ },
3743}};
3744
Willy Tarreau0108d902018-11-25 19:14:37 +01003745INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
3746
Emeric Brun107ca302010-01-04 16:16:05 +01003747/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003748static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau0851fd52019-12-17 10:07:25 +01003749 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
Holger Just1bfc24b2017-05-06 00:56:53 +02003750 { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN },
Emeric Brun53d1a982014-04-30 18:21:37 +02003751 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02003752 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
3753 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau60a2ee72017-12-15 07:13:48 +01003754 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01003755 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Dragan Dosen3f957b22017-10-24 09:27:34 +02003756 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
Tim Duesterhus1478aa72018-01-25 16:24:51 +01003757 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003758 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
3759 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003760 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01003761 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003762 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3763 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3764 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER01e09742016-12-26 11:46:11 +01003765 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3766 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02003767 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003768 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
Marcin Deranek9631a282018-04-16 14:30:46 +02003769 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
3770 { "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 +01003771 { "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 +02003772 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02003773#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01003774 { "sha2", sample_conv_sha2, ARG1(0, SINT), smp_check_sha2, SMP_T_BIN, SMP_T_BIN },
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02003775#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
3776 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
3777#endif
Patrick Gansterer8e366512020-04-22 16:47:57 +02003778 { "digest", sample_conv_crypto_digest, ARG1(1,STR), check_crypto_digest, SMP_T_BIN, SMP_T_BIN },
3779 { "hmac", sample_conv_crypto_hmac, ARG2(2,STR,STR), check_crypto_hmac, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02003780#endif
Willy Tarreau280f42b2018-02-19 15:34:12 +01003781 { "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 +02003782 { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT },
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003783#ifdef USE_OPENSSL
3784 { "secure_memcmp", sample_conv_secure_memcmp, ARG1(1,STR), smp_check_secure_memcmp, SMP_T_BIN, SMP_T_BOOL },
3785#endif
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003786
3787 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003788 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
3789 { "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 +01003790
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003791 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3792 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3793 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3794 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
3795 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3796 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3797 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3798 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3799 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3800 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3801 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3802 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3803 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3804 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01003805
Christopher Fauletea159d62020-04-01 16:21:44 +02003806 { "htonl", sample_conv_htonl, 0, NULL, SMP_T_SINT, SMP_T_BIN },
3807 { "cut_crlf", sample_conv_cut_crlf, 0, NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet51fc9d12020-04-01 17:24:41 +02003808 { "ltrim", sample_conv_ltrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet568415a2020-04-01 17:24:47 +02003809 { "rtrim", sample_conv_rtrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02003810 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01003811}};
3812
Willy Tarreau0108d902018-11-25 19:14:37 +01003813INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);