blob: b05259bbb73fe2902ea45fc3af7747b49c9108a0 [file] [log] [blame]
Emeric Brun107ca302010-01-04 16:16:05 +01001/*
Willy Tarreaucd3b0942012-04-27 21:52:18 +02002 * Sample management functions.
Emeric Brun107ca302010-01-04 16:16:05 +01003 *
4 * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreaucd3b0942012-04-27 21:52:18 +02005 * Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
Emeric Brun107ca302010-01-04 16:16:05 +01006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Thierry FOURNIER317e1c42014-08-12 10:20:47 +020014#include <ctype.h>
Emeric Brun107ca302010-01-04 16:16:05 +010015#include <string.h>
16#include <arpa/inet.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020017#include <stdio.h>
Emeric Brun107ca302010-01-04 16:16:05 +010018
Willy Tarreaub2551052020-06-09 09:07:15 +020019#include <import/sha1.h>
20#include <import/xxhash.h>
Willy Tarreau7e2c6472012-10-29 20:44:36 +010021
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/api.h>
23#include <haproxy/arg.h>
Willy Tarreauac13aea2020-06-04 10:36:03 +020024#include <haproxy/auth.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020025#include <haproxy/base64.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020026#include <haproxy/buf.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020027#include <haproxy/chunk.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020028#include <haproxy/errors.h>
Baptiste Assmanne138dda2020-10-22 15:39:03 +020029#include <haproxy/fix.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020030#include <haproxy/global.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020031#include <haproxy/hash.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020032#include <haproxy/http.h>
Baptiste Assmanne138dda2020-10-22 15:39:03 +020033#include <haproxy/istbuf.h>
Baptiste Assmanne279ca62020-10-27 18:10:06 +010034#include <haproxy/mqtt.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020035#include <haproxy/net_helper.h>
Willy Tarreaub23e5952020-06-04 16:06:59 +020036#include <haproxy/protobuf.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020037#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020038#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020039#include <haproxy/sample.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020040#include <haproxy/sink.h>
41#include <haproxy/stick_table.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020042#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020043#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020044#include <haproxy/vars.h>
Thierry FOURNIER01e09742016-12-26 11:46:11 +010045
Willy Tarreau1cf8f082014-02-07 12:14:54 +010046/* sample type names */
47const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020048 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010049 [SMP_T_BOOL] = "bool",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010050 [SMP_T_SINT] = "sint",
51 [SMP_T_ADDR] = "addr",
52 [SMP_T_IPV4] = "ipv4",
53 [SMP_T_IPV6] = "ipv6",
54 [SMP_T_STR] = "str",
55 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020056 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010057};
58
Willy Tarreau12785782012-04-27 21:37:17 +020059/* static sample used in sample_process() when <p> is NULL */
Emeric Brune5c918b2017-06-14 14:15:36 +020060static THREAD_LOCAL struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010061
Willy Tarreau12785782012-04-27 21:37:17 +020062/* list head of all known sample fetch keywords */
63static struct sample_fetch_kw_list sample_fetches = {
64 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010065};
66
Willy Tarreau12785782012-04-27 21:37:17 +020067/* list head of all known sample format conversion keywords */
68static struct sample_conv_kw_list sample_convs = {
69 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010070};
71
Willy Tarreau80aca902013-01-07 15:42:20 +010072const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
73 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
74 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
75 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
76 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
77 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
78 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +010079 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +010080
81 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
82 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
83 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
84 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
85 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
86 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +010087 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +010088
89 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
90 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
91 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
92 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
93 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
94 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +010095 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +010096
97 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
98 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
99 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
100 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
101 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
102 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100103 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100104
105 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
106 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
107 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
108 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
109 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
110 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100111 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100112
113 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
114 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
115 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
116 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
117 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
118 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100119 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100120
121 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
122 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
123 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
124 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
125 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
126 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100127 SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100128
129 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
130 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
131 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
132 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
133 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
134 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100135 SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100136
137 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
138 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
139 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
140 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
141 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
142 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100143 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100144
145 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
146 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
147 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
148 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
149 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
150 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100151 SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100152
153 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
154 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
155 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
156 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
157 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
158 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100159 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100160
161 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
162 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
163 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
164 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
165 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
166 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100167 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100168
169 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
170 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
171 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
172 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
173 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
174 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100175 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100176
177 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
178 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
179 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
180 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
181 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
182 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100183 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100184
185 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
186 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
187 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
188 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
189 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
190 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Christopher Faulete596d182020-05-05 17:46:34 +0200191 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100192
193 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
194 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
195 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
196 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
197 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
198 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Christopher Faulete596d182020-05-05 17:46:34 +0200199 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100200
201 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
202 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
203 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
204 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
205 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
206 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Christopher Faulete596d182020-05-05 17:46:34 +0200207 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100208
209 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
210 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
211 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
212 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
213 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
214 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Christopher Faulete596d182020-05-05 17:46:34 +0200215 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100216
217 [SMP_SRC_RQFIN] = (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___________ |
221 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
222 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100223 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100224
225 [SMP_SRC_RSFIN] = (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___________ |
229 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
230 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100231 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100232
233 [SMP_SRC_TXFIN] = (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___________ |
237 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
238 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100239 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100240
241 [SMP_SRC_SSFIN] = (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___________ |
245 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
246 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100247 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100248};
249
250static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
251 [SMP_SRC_INTRN] = "internal state",
252 [SMP_SRC_LISTN] = "listener",
253 [SMP_SRC_FTEND] = "frontend",
254 [SMP_SRC_L4CLI] = "client address",
255 [SMP_SRC_L5CLI] = "client-side connection",
256 [SMP_SRC_TRACK] = "track counters",
257 [SMP_SRC_L6REQ] = "request buffer",
258 [SMP_SRC_HRQHV] = "HTTP request headers",
259 [SMP_SRC_HRQHP] = "HTTP request",
260 [SMP_SRC_HRQBO] = "HTTP request body",
261 [SMP_SRC_BKEND] = "backend",
262 [SMP_SRC_SERVR] = "server",
263 [SMP_SRC_L4SRV] = "server address",
264 [SMP_SRC_L5SRV] = "server-side connection",
265 [SMP_SRC_L6RES] = "response buffer",
266 [SMP_SRC_HRSHV] = "HTTP response headers",
267 [SMP_SRC_HRSHP] = "HTTP response",
268 [SMP_SRC_HRSBO] = "HTTP response body",
269 [SMP_SRC_RQFIN] = "request buffer statistics",
270 [SMP_SRC_RSFIN] = "response buffer statistics",
271 [SMP_SRC_TXFIN] = "transaction statistics",
272 [SMP_SRC_SSFIN] = "session statistics",
273};
274
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100275static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
276 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
277 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
278 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
279 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
280 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
281 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
282 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
283 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
284 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
285 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
286 [SMP_CKP_BE_SRV_CON] = "server source selection",
287 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
288 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
289 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
290 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
291 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
292 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
293 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
294 [SMP_CKP_FE_LOG_END] = "logs",
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100295 [SMP_CKP_BE_CHK_RUL] = "backend tcp-check rule",
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100296};
297
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100298/* This function returns the type of the data returned by the sample_expr.
299 * It assumes that the <expr> and all of its converters are properly
300 * initialized.
301 */
302inline
303int smp_expr_output_type(struct sample_expr *expr)
304{
305 struct sample_conv_expr *smp_expr;
306
307 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
308 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
309 return smp_expr->conv->out_type;
310 }
311 return expr->fetch->out_type;
312}
313
314
Willy Tarreau80aca902013-01-07 15:42:20 +0100315/* fill the trash with a comma-delimited list of source names for the <use> bit
316 * field which must be composed of a non-null set of SMP_USE_* flags. The return
317 * value is the pointer to the string in the trash buffer.
318 */
319const char *sample_src_names(unsigned int use)
320{
321 int bit;
322
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200323 trash.data = 0;
324 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100325 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
326 if (!(use & ~((1 << bit) - 1)))
327 break; /* no more bits */
328
329 if (!(use & (1 << bit)))
330 continue; /* bit not set */
331
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200332 trash.data += snprintf(trash.area + trash.data,
333 trash.size - trash.data, "%s%s",
334 (use & ((1 << bit) - 1)) ? "," : "",
335 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100336 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200337 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100338}
339
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100340/* return a pointer to the correct sample checkpoint name, or "unknown" when
341 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
342 * if set.
343 */
344const char *sample_ckp_names(unsigned int use)
345{
346 int bit;
347
348 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
349 if (use & (1 << bit))
350 return fetch_ckp_names[bit];
351 return "unknown sample check place, please report this bug";
352}
353
Emeric Brun107ca302010-01-04 16:16:05 +0100354/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100355 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
356 * for next parsing sessions. The fetch keywords capabilities are also computed
357 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100358 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100359void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100360{
Willy Tarreau80aca902013-01-07 15:42:20 +0100361 struct sample_fetch *sf;
362 int bit;
363
364 for (sf = kwl->kw; sf->kw != NULL; sf++) {
365 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
366 if (sf->use & (1 << bit))
367 sf->val |= fetch_cap[bit];
368 }
369 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100370}
371
372/*
Willy Tarreau12785782012-04-27 21:37:17 +0200373 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100374 * parsing sessions.
375 */
Willy Tarreau12785782012-04-27 21:37:17 +0200376void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100377{
Willy Tarreau12785782012-04-27 21:37:17 +0200378 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100379}
380
381/*
Willy Tarreau12785782012-04-27 21:37:17 +0200382 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100383 * string of <len> in buffer <kw>.
384 *
385 */
Willy Tarreau12785782012-04-27 21:37:17 +0200386struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100387{
388 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200389 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100390
Willy Tarreau12785782012-04-27 21:37:17 +0200391 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100392 for (index = 0; kwl->kw[index].kw != NULL; index++) {
393 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
394 kwl->kw[index].kw[len] == '\0')
395 return &kwl->kw[index];
396 }
397 }
398 return NULL;
399}
400
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100401/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100402 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100403 * <idx> is the index of the next sample fetch entry. It is used as private
404 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100405 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100406 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100407 * the list is reached.
408 */
409struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
410{
411 struct sample_fetch_kw_list *kwl;
412 struct sample_fetch *base;
413
414 if (!current) {
415 /* Get first kwl entry. */
416 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
417 (*idx) = 0;
418 } else {
419 /* Get kwl corresponding to the curret entry. */
420 base = current + 1 - (*idx);
421 kwl = container_of(base, struct sample_fetch_kw_list, kw);
422 }
423
424 while (1) {
425
426 /* Check if kwl is the last entry. */
427 if (&kwl->list == &sample_fetches.list)
428 return NULL;
429
430 /* idx contain the next keyword. If it is available, return it. */
431 if (kwl->kw[*idx].kw) {
432 (*idx)++;
433 return &kwl->kw[(*idx)-1];
434 }
435
436 /* get next entry in the main list, and return NULL if the end is reached. */
437 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
438
439 /* Set index to 0, ans do one other loop. */
440 (*idx) = 0;
441 }
442}
443
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100444/* This function browses the list of available converters. <current> is
445 * the last used converter. If it is the first call, it must set to NULL.
446 * <idx> is the index of the next converter entry. It is used as private
447 * value. It is useless to initiate it.
448 *
449 * It returns always the next sample_conv entry, and NULL when the end of
450 * the list is reached.
451 */
452struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
453{
454 struct sample_conv_kw_list *kwl;
455 struct sample_conv *base;
456
457 if (!current) {
458 /* Get first kwl entry. */
459 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
460 (*idx) = 0;
461 } else {
462 /* Get kwl corresponding to the curret entry. */
463 base = current + 1 - (*idx);
464 kwl = container_of(base, struct sample_conv_kw_list, kw);
465 }
466
467 while (1) {
468 /* Check if kwl is the last entry. */
469 if (&kwl->list == &sample_convs.list)
470 return NULL;
471
472 /* idx contain the next keyword. If it is available, return it. */
473 if (kwl->kw[*idx].kw) {
474 (*idx)++;
475 return &kwl->kw[(*idx)-1];
476 }
477
478 /* get next entry in the main list, and return NULL if the end is reached. */
479 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
480
481 /* Set index to 0, ans do one other loop. */
482 (*idx) = 0;
483 }
484}
485
Emeric Brun107ca302010-01-04 16:16:05 +0100486/*
Willy Tarreau12785782012-04-27 21:37:17 +0200487 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100488 * string of <len> in buffer <kw>.
489 *
490 */
Willy Tarreau12785782012-04-27 21:37:17 +0200491struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100492{
493 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200494 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100495
Willy Tarreau12785782012-04-27 21:37:17 +0200496 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100497 for (index = 0; kwl->kw[index].kw != NULL; index++) {
498 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
499 kwl->kw[index].kw[len] == '\0')
500 return &kwl->kw[index];
501 }
502 }
503 return NULL;
504}
505
Emeric Brun107ca302010-01-04 16:16:05 +0100506/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200507/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100508/******************************************************************/
509
Willy Tarreau342acb42012-04-23 22:03:39 +0200510static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100511{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200512 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200513 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100514 return 1;
515}
516
Willy Tarreau342acb42012-04-23 22:03:39 +0200517static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100518{
Willy Tarreau83061a82018-07-13 11:56:34 +0200519 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100520
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200521 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100522 return 0;
523
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200524 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200525 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200526 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100527 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100528
529 return 1;
530}
531
Willy Tarreau342acb42012-04-23 22:03:39 +0200532static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100533{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200534 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200535 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100536 return 1;
537}
538
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200539static int c_ipv62ip(struct sample *smp)
540{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200541 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200542 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100543 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200544 return 1;
545}
546
Willy Tarreau342acb42012-04-23 22:03:39 +0200547static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100548{
Willy Tarreau83061a82018-07-13 11:56:34 +0200549 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100550
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200551 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100552 return 0;
553
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200554 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200555 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200556 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100557 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100558 return 1;
559}
560
561/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200562static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100563{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200564 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100565}
566*/
567
Willy Tarreau342acb42012-04-23 22:03:39 +0200568static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100569{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200570 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200571 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100572 return 1;
573}
574
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200575static int c_int2ipv6(struct sample *smp)
576{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200577 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
578 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200579 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200580 return 1;
581}
582
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100583static int c_str2addr(struct sample *smp)
584{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200585 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
586 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100587 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200588 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100589 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100590 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100591 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200592 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100593 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100594 return 1;
595}
596
Willy Tarreau342acb42012-04-23 22:03:39 +0200597static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100598{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200599 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100600 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200601 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100602 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100603 return 1;
604}
605
Willy Tarreau342acb42012-04-23 22:03:39 +0200606static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100607{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200608 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100609 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200610 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100611 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100612 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100613}
614
Emeric Brun4b9e8022014-11-03 18:17:10 +0100615/*
616 * The NULL char always enforces the end of string if it is met.
617 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100618 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200619static int c_bin2str(struct sample *smp)
620{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100621 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200622
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200623 for (i = 0; i < smp->data.u.str.data; i++) {
624 if (!smp->data.u.str.area[i]) {
625 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100626 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100627 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200628 }
Christopher Faulet472ad512020-04-30 09:57:40 +0200629 smp->data.type = SMP_T_STR;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200630 return 1;
631}
632
Willy Tarreau342acb42012-04-23 22:03:39 +0200633static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100634{
Willy Tarreau83061a82018-07-13 11:56:34 +0200635 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100636 char *pos;
637
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200638 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100639 if (!pos)
640 return 0;
641
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200642 trash->size = trash->size - (pos - trash->area);
643 trash->area = pos;
644 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200645 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200646 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100647 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100648 return 1;
649}
650
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800651/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200652 * For strings and binary blocks, it also provides a known allocated size with
653 * a length that is capped to the size, and ensures a trailing zero is always
654 * appended for strings. This is necessary for some operations which may
655 * require to extend the length. It returns 0 if it fails, 1 on success.
656 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100657int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200658{
Willy Tarreau83061a82018-07-13 11:56:34 +0200659 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200660
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200661 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100662 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100663 case SMP_T_SINT:
664 case SMP_T_ADDR:
665 case SMP_T_IPV4:
666 case SMP_T_IPV6:
667 /* These type are not const. */
668 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200669
Christopher Fauletec100512017-07-24 15:38:41 +0200670 case SMP_T_METH:
671 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
672 break;
673 /* Fall through */
674
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100675 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200676 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100677 trash->data = smp->data.type == SMP_T_STR ?
678 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200679 if (trash->data > trash->size - 1)
680 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200681
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100682 memcpy(trash->area, smp->data.type == SMP_T_STR ?
683 smp->data.u.str.area : smp->data.u.meth.str.area,
684 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200685 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200686 smp->data.u.str = *trash;
687 break;
688
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100689 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100690 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200691 trash->data = smp->data.u.str.data;
692 if (trash->data > trash->size)
693 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200694
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200695 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200696 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100697 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200698
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100699 default:
700 /* Other cases are unexpected. */
701 return 0;
702 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100703
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100704 /* remove const flag */
705 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200706 return 1;
707}
708
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100709int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100710{
711 return 1;
712}
713
Willy Tarreau342acb42012-04-23 22:03:39 +0200714static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100715{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200716 const char *str;
717 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100718
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200719 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100720 return 0;
721
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200722 str = smp->data.u.str.area;
723 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100724
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200725 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200726 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100727 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100728 return 1;
729}
730
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100731static int c_str2meth(struct sample *smp)
732{
733 enum http_meth_t meth;
734 int len;
735
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200736 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100737 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200738 len = smp->data.u.str.data;
739 smp->data.u.meth.str.area = smp->data.u.str.area;
740 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100741 }
742 else
743 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200744 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200745 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100746 return 1;
747}
748
749static int c_meth2str(struct sample *smp)
750{
751 int len;
752 enum http_meth_t meth;
753
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200754 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100755 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200756 len = smp->data.u.meth.str.data;
757 smp->data.u.str.area = smp->data.u.meth.str.area;
758 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200759 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100760 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200761 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100762 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200763 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200764 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200765 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100766 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200767 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100768 }
769 else {
770 /* Unknown method */
771 return 0;
772 }
773 return 1;
774}
775
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200776static int c_addr2bin(struct sample *smp)
777{
Willy Tarreau83061a82018-07-13 11:56:34 +0200778 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200779
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200780 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200781 chk->data = 4;
782 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200783 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200784 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200785 chk->data = 16;
786 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200787 }
788 else
789 return 0;
790
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200791 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200792 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200793 return 1;
794}
795
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200796static int c_int2bin(struct sample *smp)
797{
Willy Tarreau83061a82018-07-13 11:56:34 +0200798 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200799
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200800 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
801 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200802
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200803 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200804 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200805 return 1;
806}
807
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200808
Emeric Brun107ca302010-01-04 16:16:05 +0100809/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200810/* Sample casts matrix: */
811/* sample_casts[from type][to type] */
812/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100813/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100814
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100815sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200816/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
817/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
818/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200819/* 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 +0200820/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
821/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200822/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200823/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
824/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
825/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200826};
Emeric Brun107ca302010-01-04 16:16:05 +0100827
Emeric Brun107ca302010-01-04 16:16:05 +0100828/*
Willy Tarreau12785782012-04-27 21:37:17 +0200829 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100830 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200831 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200832 * The caller must have set al->ctx.
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100833 * If <endptr> is non-nul, it will be set to the first unparsed character
834 * (which may be the final '\0') on success. If it is nul, the expression
835 * must be properly terminated by a '\0' otherwise an error is reported.
Emeric Brun107ca302010-01-04 16:16:05 +0100836 */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100837struct 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 +0100838{
Willy Tarreau833cc792013-07-24 15:34:19 +0200839 const char *begw; /* beginning of word */
840 const char *endw; /* end of word */
841 const char *endt; /* end of term */
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +0100842 struct sample_expr *expr = NULL;
Willy Tarreau12785782012-04-27 21:37:17 +0200843 struct sample_fetch *fetch;
844 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100845 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200846 char *fkw = NULL;
847 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100848 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100849
Willy Tarreau833cc792013-07-24 15:34:19 +0200850 begw = str[*idx];
Willy Tarreaued2c6622020-02-14 18:27:10 +0100851 for (endw = begw; is_idchar(*endw); endw++)
852 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200853
854 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100855 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100856 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200857 }
Emeric Brun107ca302010-01-04 16:16:05 +0100858
Willy Tarreau833cc792013-07-24 15:34:19 +0200859 /* keep a copy of the current fetch keyword for error reporting */
860 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100861
Willy Tarreau833cc792013-07-24 15:34:19 +0200862 fetch = find_sample_fetch(begw, endw - begw);
863 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100864 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100865 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200866 }
Emeric Brun107ca302010-01-04 16:16:05 +0100867
Willy Tarreau833cc792013-07-24 15:34:19 +0200868 /* At this point, we have :
869 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100870 * - endw : end of the keyword, first character not part of keyword
Willy Tarreau833cc792013-07-24 15:34:19 +0200871 */
872
873 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100874 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100875 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200876 }
Emeric Brun107ca302010-01-04 16:16:05 +0100877 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200878
Vincent Bernat02779b62016-04-03 13:48:43 +0200879 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200880 if (!expr)
881 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100882
883 LIST_INIT(&(expr->conv_exprs));
884 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200885 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100886
Willy Tarreau689a1df2013-12-13 00:40:11 +0100887 /* Note that we call the argument parser even with an empty string,
888 * this allows it to automatically create entries for mandatory
889 * implicit arguments (eg: local proxy name).
890 */
891 al->kw = expr->fetch->kw;
892 al->conv = NULL;
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100893 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 +0100894 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
895 goto out_error;
896 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200897
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100898 /* now endt is our first char not part of the arg list, typically the
899 * comma after the sample fetch name or after the closing parenthesis,
900 * or the NUL char.
901 */
902
Willy Tarreau689a1df2013-12-13 00:40:11 +0100903 if (!expr->arg_p) {
904 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200905 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100906 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
907 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200908 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100909 }
910
Willy Tarreau833cc792013-07-24 15:34:19 +0200911 /* Now process the converters if any. We have two supported syntaxes
912 * for the converters, which can be combined :
913 * - comma-delimited list of converters just after the keyword and args ;
914 * - one converter per keyword
915 * The combination allows to have each keyword being a comma-delimited
916 * series of converters.
917 *
918 * We want to process the former first, then the latter. For this we start
919 * from the beginning of the supposed place in the exiting conv chain, which
920 * starts at the last comma (endt).
921 */
922
923 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200924 struct sample_conv_expr *conv_expr;
Willy Tarreau46dfd782019-12-17 10:25:29 +0100925 int err_arg;
926 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +0100927
Willy Tarreau833cc792013-07-24 15:34:19 +0200928 if (*endt && *endt != ',') {
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100929 if (endptr) {
930 /* end found, let's stop here */
931 break;
932 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200933 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100934 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200935 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100936 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100937 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200938 }
Emeric Brun107ca302010-01-04 16:16:05 +0100939
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100940 /* FIXME: how long should we support such idiocies ? Maybe we
941 * should already warn ?
942 */
Willy Tarreau833cc792013-07-24 15:34:19 +0200943 while (*endt == ',') /* then trailing commas */
944 endt++;
945
Willy Tarreau97108e02016-11-25 07:33:24 +0100946 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200947
948 if (!*begw) {
949 /* none ? skip to next string */
950 (*idx)++;
951 begw = str[*idx];
952 if (!begw || !*begw)
953 break;
954 }
955
Willy Tarreaued2c6622020-02-14 18:27:10 +0100956 for (endw = begw; is_idchar(*endw); endw++)
957 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200958
959 free(ckw);
960 ckw = my_strndup(begw, endw - begw);
961
962 conv = find_sample_conv(begw, endw - begw);
963 if (!conv) {
964 /* we found an isolated keyword that we don't know, it's not ours */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100965 if (begw == str[*idx]) {
966 endt = begw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200967 break;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100968 }
Willy Tarreau97108e02016-11-25 07:33:24 +0100969 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200970 goto out_error;
971 }
Emeric Brun107ca302010-01-04 16:16:05 +0100972
Willy Tarreau833cc792013-07-24 15:34:19 +0200973 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100974 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100975 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200976 }
Emeric Brun107ca302010-01-04 16:16:05 +0100977
978 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200979 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100980 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100981 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200982 }
Emeric Brun107ca302010-01-04 16:16:05 +0100983
984 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +0200985 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200986 if (!conv_expr)
987 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100988
989 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
990 conv_expr->conv = conv;
991
Willy Tarreau46dfd782019-12-17 10:25:29 +0100992 al->kw = expr->fetch->kw;
993 al->conv = conv_expr->conv->kw;
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100994 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 +0100995 if (argcnt < 0) {
996 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
997 goto out_error;
998 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100999
Willy Tarreau46dfd782019-12-17 10:25:29 +01001000 if (argcnt && !conv->arg_mask) {
1001 memprintf(err_msg, "converter '%s' does not support any args", ckw);
1002 goto out_error;
1003 }
Willy Tarreau21d68a62012-04-20 15:52:36 +02001004
Willy Tarreau46dfd782019-12-17 10:25:29 +01001005 if (!conv_expr->arg_p)
1006 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001007
Willy Tarreau46dfd782019-12-17 10:25:29 +01001008 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1009 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001010 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001011 }
1012 }
Emeric Brun485479d2010-09-23 18:02:19 +02001013
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001014 if (endptr) {
1015 /* end found, let's stop here */
1016 *endptr = (char *)endt;
1017 }
1018
Willy Tarreau833cc792013-07-24 15:34:19 +02001019 out:
1020 free(fkw);
1021 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001022 return expr;
1023
1024out_error:
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +01001025 release_sample_expr(expr);
Willy Tarreau833cc792013-07-24 15:34:19 +02001026 expr = NULL;
1027 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001028}
1029
1030/*
Willy Tarreau12785782012-04-27 21:37:17 +02001031 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001032 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001033 * Returns a pointer on a typed sample structure containing the result or NULL if
1034 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001035 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001036 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001037 *
1038 * Note: the fetch functions are required to properly set the return type. The
1039 * conversion functions must do so too. However the cast functions do not need
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001040 * to since they're made to cast multiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001041 *
1042 * The caller may indicate in <opt> if it considers the result final or not.
1043 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1044 * if the result is stable or not, according to the following table :
1045 *
1046 * return MAY_CHANGE FINAL Meaning for the sample
1047 * NULL 0 * Not present and will never be (eg: header)
1048 * NULL 1 0 Not present yet, could change (eg: POST param)
1049 * NULL 1 1 Not present yet, will not change anymore
1050 * smp 0 * Present and will not change (eg: header)
1051 * smp 1 0 Present, may change (eg: request length)
1052 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001053 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001054struct sample *sample_process(struct proxy *px, struct session *sess,
1055 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001056 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001057{
Willy Tarreau12785782012-04-27 21:37:17 +02001058 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001059
Willy Tarreau18387e22013-07-25 12:02:38 +02001060 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001061 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001062 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001063 }
Emeric Brun107ca302010-01-04 16:16:05 +01001064
Willy Tarreau1777ea62016-03-10 16:15:46 +01001065 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001066 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001067 return NULL;
1068
Emeric Brun107ca302010-01-04 16:16:05 +01001069 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001070 /* we want to ensure that p->type can be casted into
1071 * conv_expr->conv->in_type. We have 3 possibilities :
1072 * - NULL => not castable.
1073 * - c_none => nothing to do (let's optimize it)
1074 * - other => apply cast and prepare to fail
1075 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001076 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001077 return NULL;
1078
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001079 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1080 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001081 return NULL;
1082
Willy Tarreau12e50112012-04-25 17:21:49 +02001083 /* OK cast succeeded */
1084
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001085 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001086 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001087 }
1088 return p;
1089}
1090
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001091/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001092 * Resolve all remaining arguments in proxy <p>. Returns the number of
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001093 * errors or 0 if everything is fine. If at least one error is met, it will
1094 * be appended to *err. If *err==NULL it will be allocated first.
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001095 */
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001096int smp_resolve_args(struct proxy *p, char **err)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001097{
1098 struct arg_list *cur, *bak;
1099 const char *ctx, *where;
1100 const char *conv_ctx, *conv_pre, *conv_pos;
1101 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001102 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001103 struct arg *arg;
1104 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001105 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001106
1107 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1108 struct proxy *px;
1109 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001110 struct stktable *t;
1111 char *pname, *sname, *stktname;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001112 char *err2;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001113
1114 arg = cur->arg;
1115
1116 /* prepare output messages */
1117 conv_pre = conv_pos = conv_ctx = "";
1118 if (cur->conv) {
1119 conv_ctx = cur->conv;
1120 conv_pre = "conversion keyword '";
1121 conv_pos = "' for ";
1122 }
1123
1124 where = "in";
1125 ctx = "sample fetch keyword";
1126 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001127 case ARGC_STK: where = "in stick rule in"; break;
1128 case ARGC_TRK: where = "in tracking rule in"; break;
1129 case ARGC_LOG: where = "in log-format string in"; break;
1130 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1131 case ARGC_HRQ: where = "in http-request header format string in"; break;
1132 case ARGC_HRS: where = "in http-response header format string in"; break;
1133 case ARGC_UIF: where = "in unique-id-format string in"; break;
1134 case ARGC_RDR: where = "in redirect format string in"; break;
1135 case ARGC_CAP: where = "in capture rule in"; break;
1136 case ARGC_ACL: ctx = "ACL keyword"; break;
1137 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001138 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001139 case ARGC_HERR: where = "in http-error directive in"; break;
Miroslav Zagorac7f8314c2020-12-09 16:31:48 +01001140 case ARGC_OT: where = "in ot-scope directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001141 }
1142
1143 /* set a few default settings */
1144 px = p;
1145 pname = p->id;
1146
1147 switch (arg->type) {
1148 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001149 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001150 memprintf(err, "%sparsing [%s:%d]: missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1151 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001152 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001153 cfgerr++;
1154 continue;
1155 }
1156
1157 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001158 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001159
1160 if (sname) {
1161 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001162 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001163
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001164 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001165 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001166 memprintf(err, "%sparsing [%s:%d]: unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1167 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001168 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001169 cfgerr++;
1170 break;
1171 }
1172 }
1173 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001174 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001175
1176 srv = findserver(px, sname);
1177 if (!srv) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001178 memprintf(err, "%sparsing [%s:%d]: unable to find server '%s' in proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1179 *err ? *err : "", cur->file, cur->line, sname, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001180 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001181 cfgerr++;
1182 break;
1183 }
1184
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001185 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001186 arg->unresolved = 0;
1187 arg->data.srv = srv;
1188 break;
1189
1190 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001191 if (arg->data.str.data) {
1192 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001193 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001194 }
1195
1196 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001197 memprintf(err, "%sparsing [%s:%d]: unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1198 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001199 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001200 cfgerr++;
1201 break;
1202 }
1203
1204 if (!(px->cap & PR_CAP_FE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001205 memprintf(err, "%sparsing [%s:%d]: proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1206 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001207 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001208 cfgerr++;
1209 break;
1210 }
1211
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001212 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001213 arg->unresolved = 0;
1214 arg->data.prx = px;
1215 break;
1216
1217 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001218 if (arg->data.str.data) {
1219 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001220 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001221 }
1222
1223 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001224 memprintf(err, "%sparsing [%s:%d]: unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1225 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001226 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001227 cfgerr++;
1228 break;
1229 }
1230
1231 if (!(px->cap & PR_CAP_BE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001232 memprintf(err, "%sparsing [%s:%d]: proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1233 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001234 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001235 cfgerr++;
1236 break;
1237 }
1238
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001239 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001240 arg->unresolved = 0;
1241 arg->data.prx = px;
1242 break;
1243
1244 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001245 if (arg->data.str.data)
1246 stktname = arg->data.str.area;
1247 else
1248 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001249
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001250 t = stktable_find_by_name(stktname);
1251 if (!t) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001252 memprintf(err, "%sparsing [%s:%d]: unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1253 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001254 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001255 cfgerr++;
1256 break;
1257 }
1258
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001259 if (!t->size) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001260 memprintf(err, "%sparsing [%s:%d]: no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1261 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001262 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001263 cfgerr++;
1264 break;
1265 }
1266
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001267 if (t->proxy && (p->bind_proc & ~t->proxy->bind_proc)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001268 memprintf(err, "%sparsing [%s:%d]: stick-table '%s' not present on all processes covered by proxy '%s'.\n",
1269 *err ? *err : "", cur->file, cur->line, t->proxy->id, p->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01001270 cfgerr++;
1271 break;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001272 }
1273
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001274 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001275 p->next_stkt_ref = t->proxies_list;
1276 t->proxies_list = p;
1277 }
1278
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001279 chunk_destroy(&arg->data.str);
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) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001286 memprintf(err, "%sparsing [%s:%d]: missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1287 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001288 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 &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001294 strcmp(p->uri_auth->userlist->name, arg->data.str.area) == 0)
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) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001300 memprintf(err, "%sparsing [%s:%d]: unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1301 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001302 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
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001308 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001309 arg->unresolved = 0;
1310 arg->data.usr = ul;
1311 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001312
1313 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001314 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001315 memprintf(err, "%sparsing [%s:%d]: missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1316 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001317 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001318 cfgerr++;
1319 continue;
1320 }
1321
Willy Tarreau46947782015-01-19 19:00:58 +01001322 rflags = 0;
1323 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001324 err2 = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001325
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001326 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err2))) {
1327 memprintf(err, "%sparsing [%s:%d]: error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1328 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001329 arg->data.str.area,
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001330 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err2);
Willy Tarreau46947782015-01-19 19:00:58 +01001331 cfgerr++;
1332 continue;
1333 }
1334
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001335 chunk_destroy(&arg->data.str);
Willy Tarreau46947782015-01-19 19:00:58 +01001336 arg->unresolved = 0;
1337 arg->data.reg = reg;
1338 break;
1339
1340
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001341 }
1342
1343 LIST_DEL(&cur->list);
1344 free(cur);
1345 } /* end of args processing */
1346
1347 return cfgerr;
1348}
1349
1350/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001351 * Process a fetch + format conversion as defined by the sample expression
1352 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001353 * not explicitly set to <smp_type>, but shall be compatible with it as
1354 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1355 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001356 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1357 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1358 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1359 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001360 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1361 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1362 * still hope to get it after waiting longer, and is not converted to string.
1363 * The possible output combinations are the following :
1364 *
1365 * return MAY_CHANGE FINAL Meaning for the sample
1366 * NULL * * Not present and will never be (eg: header)
1367 * smp 0 * Final value converted (eg: header)
1368 * smp 1 0 Not present yet, may appear later (eg: header)
1369 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001370 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001371struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001372 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001373 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001374{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001375 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001376
Willy Tarreau6c616e02014-06-25 16:56:41 +02001377 memset(smp, 0, sizeof(*smp));
1378
Willy Tarreau192252e2015-04-04 01:47:55 +02001379 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001380 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1381 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001382 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001383 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001384
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001385 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001386 return NULL;
1387
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001388 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001389 return NULL;
1390
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001391 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001392 return smp;
1393}
1394
Christopher Faulet476e5d02016-10-26 11:34:47 +02001395static void release_sample_arg(struct arg *p)
1396{
1397 struct arg *p_back = p;
1398
1399 if (!p)
1400 return;
1401
1402 while (p->type != ARGT_STOP) {
1403 if (p->type == ARGT_STR || p->unresolved) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001404 chunk_destroy(&p->data.str);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001405 p->unresolved = 0;
1406 }
1407 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001408 regex_free(p->data.reg);
1409 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001410 }
1411 p++;
1412 }
1413
1414 if (p_back != empty_arg_list)
1415 free(p_back);
1416}
1417
1418void release_sample_expr(struct sample_expr *expr)
1419{
1420 struct sample_conv_expr *conv_expr, *conv_exprb;
1421
1422 if (!expr)
1423 return;
1424
Tim Duesterhus867cd982020-07-04 11:49:39 +02001425 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list) {
1426 LIST_DEL(&conv_expr->list);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001427 release_sample_arg(conv_expr->arg_p);
Tim Duesterhus867cd982020-07-04 11:49:39 +02001428 free(conv_expr);
1429 }
1430
Christopher Faulet476e5d02016-10-26 11:34:47 +02001431 release_sample_arg(expr->arg_p);
1432 free(expr);
1433}
1434
Emeric Brun107ca302010-01-04 16:16:05 +01001435/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001436/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001437/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001438/*****************************************************************/
1439
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001440static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1441{
1442 int i;
1443 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001444 struct buffer *buf;
1445 struct sink *sink;
1446 struct ist line;
1447 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001448
Willy Tarreau0851fd52019-12-17 10:07:25 +01001449 buf = alloc_trash_chunk();
1450 if (!buf)
1451 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001452
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001453 sink = (struct sink *)arg_p[1].data.ptr;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001454 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001455
Willy Tarreau0851fd52019-12-17 10:07:25 +01001456 pfx = arg_p[0].data.str.area;
1457 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001458
Willy Tarreau0851fd52019-12-17 10:07:25 +01001459 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1460 if (!sample_casts[smp->data.type][SMP_T_STR])
1461 goto nocast;
1462
1463 /* Copy sample fetch. This puts the sample as const, the
1464 * cast will copy data if a transformation is required.
1465 */
1466 memcpy(&tmp, smp, sizeof(struct sample));
1467 tmp.flags = SMP_F_CONST;
1468
1469 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1470 goto nocast;
1471
1472 /* Display the displayable chars*. */
1473 b_putchr(buf, '<');
1474 for (i = 0; i < tmp.data.u.str.data; i++) {
Willy Tarreau90807112020-02-25 08:16:33 +01001475 if (isprint((unsigned char)tmp.data.u.str.area[i]))
Willy Tarreau0851fd52019-12-17 10:07:25 +01001476 b_putchr(buf, tmp.data.u.str.area[i]);
1477 else
1478 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001479 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001480 b_putchr(buf, '>');
1481
1482 done:
1483 line = ist2(buf->area, buf->data);
Emeric Brun54648852020-07-06 15:54:06 +02001484 sink_write(sink, &line, 1, 0, 0, NULL);
Willy Tarreau0851fd52019-12-17 10:07:25 +01001485 end:
1486 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001487 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001488 nocast:
1489 chunk_appendf(buf, "(undisplayable)");
1490 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001491}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001492
1493// This function checks the "debug" converter's arguments.
1494static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1495 const char *file, int line, char **err)
1496{
1497 const char *name = "buf0";
1498 struct sink *sink = NULL;
1499
1500 if (args[0].type != ARGT_STR) {
1501 /* optional prefix */
1502 args[0].data.str.area = "";
1503 args[0].data.str.data = 0;
1504 }
1505
1506 if (args[1].type == ARGT_STR)
1507 name = args[1].data.str.area;
1508
1509 sink = sink_find(name);
1510 if (!sink) {
1511 memprintf(err, "No such sink '%s'", name);
1512 return 0;
1513 }
1514
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001515 chunk_destroy(&args[1].data.str);
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001516 args[1].type = ARGT_PTR;
1517 args[1].data.ptr = sink;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001518 return 1;
1519}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001520
Holger Just1bfc24b2017-05-06 00:56:53 +02001521static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1522{
Willy Tarreau83061a82018-07-13 11:56:34 +02001523 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001524 int bin_len;
1525
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001526 trash->data = 0;
1527 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1528 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001529 if (bin_len < 0)
1530 return 0;
1531
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001532 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001533 smp->data.u.str = *trash;
1534 smp->data.type = SMP_T_BIN;
1535 smp->flags &= ~SMP_F_CONST;
1536 return 1;
1537}
1538
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001539static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001540{
Willy Tarreau83061a82018-07-13 11:56:34 +02001541 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001542 int b64_len;
1543
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001544 trash->data = 0;
1545 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1546 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001547 if (b64_len < 0)
1548 return 0;
1549
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001550 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001551 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001552 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001553 smp->flags &= ~SMP_F_CONST;
1554 return 1;
1555}
1556
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001557static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1558{
1559 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001560 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001561
1562 memset(&ctx, 0, sizeof(ctx));
1563
1564 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001565 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1566 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001567
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001568 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001569 smp->data.u.str = *trash;
1570 smp->data.type = SMP_T_BIN;
1571 smp->flags &= ~SMP_F_CONST;
1572 return 1;
1573}
1574
Tim Duesterhusd4376302019-06-17 12:41:44 +02001575#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01001576static int smp_check_sha2(struct arg *args, struct sample_conv *conv,
1577 const char *file, int line, char **err)
1578{
1579 if (args[0].type == ARGT_STOP)
1580 return 1;
1581 if (args[0].type != ARGT_SINT) {
1582 memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]);
1583 return 0;
1584 }
1585
1586 switch (args[0].data.sint) {
1587 case 224:
1588 case 256:
1589 case 384:
1590 case 512:
1591 /* this is okay */
1592 return 1;
1593 default:
1594 memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint);
1595 return 0;
1596 }
1597}
1598
Tim Duesterhusd4376302019-06-17 12:41:44 +02001599static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
1600{
1601 struct buffer *trash = get_trash_chunk();
1602 int bits = 256;
Christopher Faulete6e7a582021-01-29 11:29:28 +01001603 if (arg_p->data.sint)
Tim Duesterhusd4376302019-06-17 12:41:44 +02001604 bits = arg_p->data.sint;
1605
1606 switch (bits) {
1607 case 224: {
1608 SHA256_CTX ctx;
1609
1610 memset(&ctx, 0, sizeof(ctx));
1611
1612 SHA224_Init(&ctx);
1613 SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1614 SHA224_Final((unsigned char *) trash->area, &ctx);
1615 trash->data = SHA224_DIGEST_LENGTH;
1616 break;
1617 }
1618 case 256: {
1619 SHA256_CTX ctx;
1620
1621 memset(&ctx, 0, sizeof(ctx));
1622
1623 SHA256_Init(&ctx);
1624 SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1625 SHA256_Final((unsigned char *) trash->area, &ctx);
1626 trash->data = SHA256_DIGEST_LENGTH;
1627 break;
1628 }
1629 case 384: {
1630 SHA512_CTX ctx;
1631
1632 memset(&ctx, 0, sizeof(ctx));
1633
1634 SHA384_Init(&ctx);
1635 SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1636 SHA384_Final((unsigned char *) trash->area, &ctx);
1637 trash->data = SHA384_DIGEST_LENGTH;
1638 break;
1639 }
1640 case 512: {
1641 SHA512_CTX ctx;
1642
1643 memset(&ctx, 0, sizeof(ctx));
1644
1645 SHA512_Init(&ctx);
1646 SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1647 SHA512_Final((unsigned char *) trash->area, &ctx);
1648 trash->data = SHA512_DIGEST_LENGTH;
1649 break;
1650 }
1651 default:
1652 return 0;
1653 }
1654
1655 smp->data.u.str = *trash;
1656 smp->data.type = SMP_T_BIN;
1657 smp->flags &= ~SMP_F_CONST;
1658 return 1;
1659}
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001660
Dragan Dosen9e8db132021-02-22 10:03:53 +01001661/* This function returns a sample struct filled with an <arg> content.
1662 * If the <arg> contains a string, it is returned in the sample flagged as
1663 * SMP_F_CONST. If the <arg> contains a variable descriptor, the sample is
1664 * filled with the content of the variable by using vars_get_by_desc().
1665 *
1666 * Keep in mind that the sample content may be written to a pre-allocated
1667 * trash chunk as returned by get_trash_chunk().
1668 *
1669 * This function returns 0 if an error occurs, otherwise it returns 1.
1670 */
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001671static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
1672{
1673 switch (arg->type) {
1674 case ARGT_STR:
1675 smp->data.type = SMP_T_STR;
1676 smp->data.u.str = arg->data.str;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001677 smp->flags = SMP_F_CONST;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001678 return 1;
1679 case ARGT_VAR:
1680 if (!vars_get_by_desc(&arg->data.var, smp))
1681 return 0;
1682 if (!sample_casts[smp->data.type][SMP_T_STR])
1683 return 0;
1684 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
1685 return 0;
1686 return 1;
1687 default:
1688 return 0;
1689 }
1690}
1691
Dragan Dosen9e8db132021-02-22 10:03:53 +01001692/* This function checks an <arg> and fills it with a variable type if the
1693 * <arg> string contains a valid variable name. If failed, the function
1694 * tries to perform a base64 decode operation on the same string, and
1695 * fills the <arg> with the decoded content.
1696 *
1697 * Validation is skipped if the <arg> string is empty.
1698 *
1699 * This function returns 0 if the variable lookup fails and the specified
1700 * <arg> string is not a valid base64 encoded string, as well if
1701 * unexpected argument type is specified or memory allocation error
1702 * occurs. Otherwise it returns 1.
1703 */
1704static inline int sample_check_arg_base64(struct arg *arg, char **err)
1705{
1706 char *dec = NULL;
1707 int dec_size;
1708
1709 if (arg->type != ARGT_STR) {
1710 memprintf(err, "unexpected argument type");
1711 return 0;
1712 }
1713
1714 if (arg->data.str.data == 0) /* empty */
1715 return 1;
1716
1717 if (vars_check_arg(arg, NULL))
1718 return 1;
1719
1720 if (arg->data.str.data % 4) {
1721 memprintf(err, "argument needs to be base64 encoded, and "
1722 "can either be a string or a variable");
1723 return 0;
1724 }
1725
1726 dec_size = (arg->data.str.data / 4 * 3)
1727 - (arg->data.str.area[arg->data.str.data-1] == '=' ? 1 : 0)
1728 - (arg->data.str.area[arg->data.str.data-2] == '=' ? 1 : 0);
1729
1730 if ((dec = malloc(dec_size)) == NULL) {
1731 memprintf(err, "memory allocation error");
1732 return 0;
1733 }
1734
1735 dec_size = base64dec(arg->data.str.area, arg->data.str.data, dec, dec_size);
1736 if (dec_size < 0) {
1737 memprintf(err, "argument needs to be base64 encoded, and "
1738 "can either be a string or a variable");
1739 free(dec);
1740 return 0;
1741 }
1742
1743 /* base64 decoded */
1744 chunk_destroy(&arg->data.str);
1745 arg->data.str.area = dec;
1746 arg->data.str.data = dec_size;
1747 return 1;
1748}
1749
Patrick Gansterer8e366512020-04-22 16:47:57 +02001750#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001751static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
1752 const char *file, int line, char **err)
1753{
1754 switch(args[0].data.sint) {
1755 case 128:
1756 case 192:
1757 case 256:
1758 break;
1759 default:
1760 memprintf(err, "key size must be 128, 192 or 256 (bits).");
1761 return 0;
1762 }
Dragan Dosen9e8db132021-02-22 10:03:53 +01001763
1764 /* Try to decode variables. */
1765 if (!sample_check_arg_base64(&args[1], err)) {
1766 memprintf(err, "failed to parse nonce : %s", *err);
1767 return 0;
1768 }
1769 if (!sample_check_arg_base64(&args[2], err)) {
1770 memprintf(err, "failed to parse key : %s", *err);
1771 return 0;
1772 }
1773 if (!sample_check_arg_base64(&args[3], err)) {
1774 memprintf(err, "failed to parse aead_tag : %s", *err);
1775 return 0;
1776 }
1777
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001778 return 1;
1779}
1780
1781/* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
1782static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
1783{
1784 struct sample nonce, key, aead_tag;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001785 struct buffer *smp_trash = NULL, *smp_trash_alloc = NULL;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001786 EVP_CIPHER_CTX *ctx;
1787 int dec_size, ret;
1788
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001789 smp_trash_alloc = alloc_trash_chunk();
1790 if (!smp_trash_alloc)
1791 return 0;
1792
Dragan Dosen9e8db132021-02-22 10:03:53 +01001793 /* smp copy */
1794 smp_trash_alloc->data = smp->data.u.str.data;
1795 if (unlikely(smp_trash_alloc->data > smp_trash_alloc->size))
1796 smp_trash_alloc->data = smp_trash_alloc->size;
1797 memcpy(smp_trash_alloc->area, smp->data.u.str.area, smp_trash_alloc->data);
1798
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001799 ctx = EVP_CIPHER_CTX_new();
1800
1801 if (!ctx)
1802 goto err;
1803
Dragan Dosen9e8db132021-02-22 10:03:53 +01001804 smp_trash = alloc_trash_chunk();
1805 if (!smp_trash)
1806 goto err;
1807
1808 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
1809 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001810 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001811
1812 if (arg_p[1].type == ARGT_VAR) {
1813 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
1814 if (dec_size < 0)
1815 goto err;
1816 smp_trash->data = dec_size;
1817 nonce.data.u.str = *smp_trash;
1818 }
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001819
1820 /* Set cipher type and mode */
1821 switch(arg_p[0].data.sint) {
1822 case 128:
1823 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
1824 break;
1825 case 192:
1826 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
1827 break;
1828 case 256:
1829 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
1830 break;
1831 }
1832
Dragan Dosen9e8db132021-02-22 10:03:53 +01001833 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, nonce.data.u.str.data, NULL);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001834
1835 /* Initialise IV */
Dragan Dosen9e8db132021-02-22 10:03:53 +01001836 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) nonce.data.u.str.area))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001837 goto err;
1838
Dragan Dosen9e8db132021-02-22 10:03:53 +01001839 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
1840 if (!sample_conv_var2smp_str(&arg_p[2], &key))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001841 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001842
1843 if (arg_p[2].type == ARGT_VAR) {
1844 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
1845 if (dec_size < 0)
1846 goto err;
1847 smp_trash->data = dec_size;
1848 key.data.u.str = *smp_trash;
1849 }
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001850
1851 /* Initialise key */
Dragan Dosen9e8db132021-02-22 10:03:53 +01001852 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) key.data.u.str.area, NULL))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001853 goto err;
1854
1855 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
Dragan Dosen9e8db132021-02-22 10:03:53 +01001856 (unsigned char *) smp_trash_alloc->area, (int) smp_trash_alloc->data))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001857 goto err;
1858
Dragan Dosen9e8db132021-02-22 10:03:53 +01001859 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
1860 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001861 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001862
1863 if (arg_p[3].type == ARGT_VAR) {
1864 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
1865 if (dec_size < 0)
1866 goto err;
1867 smp_trash_alloc->data = dec_size;
1868 aead_tag.data.u.str = *smp_trash_alloc;
1869 }
1870
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001871 dec_size = smp_trash->data;
1872
Dragan Dosen9e8db132021-02-22 10:03:53 +01001873 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, aead_tag.data.u.str.data, (void *) aead_tag.data.u.str.area);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001874 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
1875
1876 if (ret <= 0)
1877 goto err;
1878
1879 smp->data.u.str.data = dec_size + smp_trash->data;
1880 smp->data.u.str.area = smp_trash->area;
1881 smp->data.type = SMP_T_BIN;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001882 smp_dup(smp);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001883 free_trash_chunk(smp_trash_alloc);
Dragan Dosen9e8db132021-02-22 10:03:53 +01001884 free_trash_chunk(smp_trash);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001885 return 1;
1886
1887err:
1888 free_trash_chunk(smp_trash_alloc);
Dragan Dosen9e8db132021-02-22 10:03:53 +01001889 free_trash_chunk(smp_trash);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001890 return 0;
1891}
1892#endif /* HA_OPENSSL_VERSION_NUMBER */
1893
Patrick Gansterer8e366512020-04-22 16:47:57 +02001894static int check_crypto_digest(struct arg *args, struct sample_conv *conv,
1895 const char *file, int line, char **err)
1896{
1897 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1898
1899 if (evp)
1900 return 1;
1901
1902 memprintf(err, "algorithm must be a valid OpenSSL message digest name.");
1903 return 0;
1904}
1905
1906static int sample_conv_crypto_digest(const struct arg *args, struct sample *smp, void *private)
1907{
1908 struct buffer *trash = get_trash_chunk();
1909 unsigned char *md = (unsigned char*) trash->area;
1910 unsigned int md_len = trash->size;
1911 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
1912 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1913
1914 if (!ctx)
1915 return 0;
1916
1917 if (!EVP_DigestInit_ex(ctx, evp, NULL) ||
1918 !EVP_DigestUpdate(ctx, smp->data.u.str.area, smp->data.u.str.data) ||
1919 !EVP_DigestFinal_ex(ctx, md, &md_len)) {
1920 EVP_MD_CTX_free(ctx);
1921 return 0;
1922 }
1923
1924 EVP_MD_CTX_free(ctx);
1925
1926 trash->data = md_len;
1927 smp->data.u.str = *trash;
1928 smp->data.type = SMP_T_BIN;
1929 smp->flags &= ~SMP_F_CONST;
1930 return 1;
1931}
1932
1933static int check_crypto_hmac(struct arg *args, struct sample_conv *conv,
1934 const char *file, int line, char **err)
1935{
1936 if (!check_crypto_digest(args, conv, file, line, err))
1937 return 0;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001938
1939 if (!sample_check_arg_base64(&args[1], err)) {
1940 memprintf(err, "failed to parse key : %s", *err);
1941 return 0;
1942 }
Patrick Gansterer8e366512020-04-22 16:47:57 +02001943
Patrick Gansterer8e366512020-04-22 16:47:57 +02001944 return 1;
1945}
1946
1947static int sample_conv_crypto_hmac(const struct arg *args, struct sample *smp, void *private)
1948{
1949 struct sample key;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001950 struct buffer *trash = NULL, *key_trash = NULL;
Patrick Gansterer8e366512020-04-22 16:47:57 +02001951 unsigned char *md;
1952 unsigned int md_len;
1953 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1954 int dec_size;
1955
1956 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
1957 if (!sample_conv_var2smp_str(&args[1], &key))
1958 return 0;
1959
Dragan Dosen9e8db132021-02-22 10:03:53 +01001960 if (args[1].type == ARGT_VAR) {
1961 key_trash = alloc_trash_chunk();
1962 if (!key_trash)
1963 goto err;
1964
1965 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, key_trash->area, key_trash->size);
1966 if (dec_size < 0)
1967 goto err;
1968 key_trash->data = dec_size;
1969 key.data.u.str = *key_trash;
1970 }
Patrick Gansterer8e366512020-04-22 16:47:57 +02001971
Dragan Dosen9e8db132021-02-22 10:03:53 +01001972 trash = alloc_trash_chunk();
1973 if (!trash)
Patrick Gansterer8e366512020-04-22 16:47:57 +02001974 goto err;
1975
1976 md = (unsigned char*) trash->area;
1977 md_len = trash->size;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001978 if (!HMAC(evp, key.data.u.str.area, key.data.u.str.data, (const unsigned char*) smp->data.u.str.area,
1979 smp->data.u.str.data, md, &md_len))
Patrick Gansterer8e366512020-04-22 16:47:57 +02001980 goto err;
1981
1982 free_trash_chunk(key_trash);
1983
1984 trash->data = md_len;
1985 smp->data.u.str = *trash;
1986 smp->data.type = SMP_T_BIN;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001987 smp_dup(smp);
1988 free_trash_chunk(trash);
Patrick Gansterer8e366512020-04-22 16:47:57 +02001989 return 1;
1990
1991err:
1992 free_trash_chunk(key_trash);
Dragan Dosen9e8db132021-02-22 10:03:53 +01001993 free_trash_chunk(trash);
Patrick Gansterer8e366512020-04-22 16:47:57 +02001994 return 0;
1995}
1996
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001997#endif /* USE_OPENSSL */
Tim Duesterhusd4376302019-06-17 12:41:44 +02001998
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001999static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002000{
Willy Tarreau83061a82018-07-13 11:56:34 +02002001 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002002 unsigned char c;
2003 int ptr = 0;
2004
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002005 trash->data = 0;
2006 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
2007 c = smp->data.u.str.area[ptr++];
2008 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
2009 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002010 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002011 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002012 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002013 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002014 return 1;
2015}
2016
Dragan Dosen3f957b22017-10-24 09:27:34 +02002017static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
2018{
2019 long long int n = 0;
2020 int i, c;
2021
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002022 for (i = 0; i < smp->data.u.str.data; i++) {
2023 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02002024 return 0;
2025 n = (n << 4) + c;
2026 }
2027
2028 smp->data.u.sint = n;
2029 smp->data.type = SMP_T_SINT;
2030 smp->flags &= ~SMP_F_CONST;
2031 return 1;
2032}
2033
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002034/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002035static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002036{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002037 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
2038 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002039 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002040 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002041 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002042 return 1;
2043}
2044
Willy Tarreau60a2ee72017-12-15 07:13:48 +01002045static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002046{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002047 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002048 smp->data.u.sint = i;
2049 smp->data.type = SMP_T_SINT;
2050 return 1;
2051}
2052
2053
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002054static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002055{
2056 int i;
2057
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002058 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002059 return 0;
2060
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002061 for (i = 0; i < smp->data.u.str.data; i++) {
2062 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
2063 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01002064 }
2065 return 1;
2066}
2067
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002068static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002069{
2070 int i;
2071
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002072 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02002073 return 0;
2074
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002075 for (i = 0; i < smp->data.u.str.data; i++) {
2076 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
2077 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01002078 }
2079 return 1;
2080}
2081
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002082/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
2083static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002084{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002085 /* Attempt to convert to IPv4 to apply the correct mask. */
2086 c_ipv62ip(smp);
2087
2088 if (smp->data.type == SMP_T_IPV4) {
2089 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
2090 smp->data.type = SMP_T_IPV4;
2091 }
2092 else if (smp->data.type == SMP_T_IPV6) {
2093 /* IPv6 cannot be converted without an IPv6 mask. */
2094 if (args[1].type != ARGT_IPV6)
2095 return 0;
2096
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +01002097 write_u64(&smp->data.u.ipv6.s6_addr[0],
2098 read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0]));
2099 write_u64(&smp->data.u.ipv6.s6_addr[8],
2100 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 +01002101 smp->data.type = SMP_T_IPV6;
2102 }
2103
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002104 return 1;
2105}
2106
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002107/* takes an UINT value on input supposed to represent the time since EPOCH,
2108 * adds an optional offset found in args[1] and emits a string representing
2109 * the local time in the format specified in args[1] using strftime().
2110 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002111static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002112{
Willy Tarreau83061a82018-07-13 11:56:34 +02002113 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002114 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002115 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002116 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002117
2118 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002119 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002120 curr_date += args[1].data.sint;
2121
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002122 tm = localtime(&curr_date);
2123 if (!tm)
2124 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002125 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002126 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
2127 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002128 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002129 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002130 return 1;
2131}
2132
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002133/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002134static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002135{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002136 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
2137 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002138 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002139 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002140 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002141 return 1;
2142}
2143
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002144/* takes an UINT value on input supposed to represent the time since EPOCH,
2145 * adds an optional offset found in args[1] and emits a string representing
2146 * the UTC date in the format specified in args[1] using strftime().
2147 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002148static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002149{
Willy Tarreau83061a82018-07-13 11:56:34 +02002150 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002151 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002152 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002153 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002154
2155 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002156 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002157 curr_date += args[1].data.sint;
2158
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002159 tm = gmtime(&curr_date);
2160 if (!tm)
2161 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002162 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002163 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
2164 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002165 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002166 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002167 return 1;
2168}
2169
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002170/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002171static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002172{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002173 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
2174 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002175 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002176 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002177 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002178 return 1;
2179}
2180
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002181/* hashes the binary input into a 32-bit unsigned int using xxh.
2182 * The seed of the hash defaults to 0 but can be changd in argument 1.
2183 */
2184static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
2185{
2186 unsigned int seed;
2187
Christopher Faulete6e7a582021-01-29 11:29:28 +01002188 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002189 seed = arg_p->data.sint;
2190 else
2191 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002192 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
2193 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002194 smp->data.type = SMP_T_SINT;
2195 return 1;
2196}
2197
2198/* hashes the binary input into a 64-bit unsigned int using xxh.
2199 * In fact, the function returns a 64 bit unsigned, but the sample
2200 * storage of haproxy only proposes 64-bits signed, so the value is
2201 * cast as signed. This cast doesn't impact the hash repartition.
2202 * The seed of the hash defaults to 0 but can be changd in argument 1.
2203 */
2204static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
2205{
2206 unsigned long long int seed;
2207
Christopher Faulete6e7a582021-01-29 11:29:28 +01002208 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002209 seed = (unsigned long long int)arg_p->data.sint;
2210 else
2211 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002212 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
2213 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002214 smp->data.type = SMP_T_SINT;
2215 return 1;
2216}
2217
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002218static int sample_conv_xxh3(const struct arg *arg_p, struct sample *smp, void *private)
2219{
2220 unsigned long long int seed;
2221
Christopher Faulete6e7a582021-01-29 11:29:28 +01002222 if (arg_p->data.sint)
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002223 seed = (unsigned long long int)arg_p->data.sint;
2224 else
2225 seed = 0;
2226 smp->data.u.sint = (long long int)XXH3(smp->data.u.str.area,
2227 smp->data.u.str.data, seed);
2228 smp->data.type = SMP_T_SINT;
2229 return 1;
2230}
2231
Willy Tarreau80599772015-01-20 19:35:24 +01002232/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002233static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01002234{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002235 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
2236 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002237 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002238 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002239 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01002240 return 1;
2241}
2242
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002243/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
2244static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
2245{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002246 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
2247 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002248 if (arg_p->data.sint)
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002249 smp->data.u.sint = full_hash(smp->data.u.sint);
2250 smp->data.type = SMP_T_SINT;
2251 return 1;
2252}
2253
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002254/* This function escape special json characters. The returned string can be
2255 * safely set between two '"' and used as json string. The json string is
2256 * defined like this:
2257 *
2258 * any Unicode character except '"' or '\' or control character
2259 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
2260 *
2261 * The enum input_type contain all the allowed mode for decoding the input
2262 * string.
2263 */
2264enum input_type {
2265 IT_ASCII = 0,
2266 IT_UTF8,
2267 IT_UTF8S,
2268 IT_UTF8P,
2269 IT_UTF8PS,
2270};
William Dauchy5417e892021-01-08 21:57:41 +01002271
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002272static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
2273 const char *file, int line, char **err)
2274{
Christopher Faulet95917132020-08-05 23:07:07 +02002275 enum input_type type;
2276
Christopher Faulet95917132020-08-05 23:07:07 +02002277 if (strcmp(arg->data.str.area, "") == 0)
2278 type = IT_ASCII;
2279 else if (strcmp(arg->data.str.area, "ascii") == 0)
2280 type = IT_ASCII;
2281 else if (strcmp(arg->data.str.area, "utf8") == 0)
2282 type = IT_UTF8;
2283 else if (strcmp(arg->data.str.area, "utf8s") == 0)
2284 type = IT_UTF8S;
2285 else if (strcmp(arg->data.str.area, "utf8p") == 0)
2286 type = IT_UTF8P;
2287 else if (strcmp(arg->data.str.area, "utf8ps") == 0)
2288 type = IT_UTF8PS;
2289 else {
2290 memprintf(err, "Unexpected input code type. "
2291 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
2292 return 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002293 }
2294
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002295 chunk_destroy(&arg->data.str);
Christopher Faulet95917132020-08-05 23:07:07 +02002296 arg->type = ARGT_SINT;
2297 arg->data.sint = type;
2298 return 1;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002299}
2300
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002301static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002302{
Willy Tarreau83061a82018-07-13 11:56:34 +02002303 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002304 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
2305 const char *str;
2306 int len;
2307 enum input_type input_type = IT_ASCII;
2308 unsigned int c;
2309 unsigned int ret;
2310 char *p;
2311
Christopher Faulete6e7a582021-01-29 11:29:28 +01002312 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002313
2314 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002315 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002316
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002317 p = smp->data.u.str.area;
2318 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002319
2320 if (input_type == IT_ASCII) {
2321 /* Read input as ASCII. */
2322 c = *(unsigned char *)p;
2323 p++;
2324 }
2325 else {
2326 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002327 ret = utf8_next(p,
2328 smp->data.u.str.data - ( p - smp->data.u.str.area),
2329 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002330 p += utf8_return_length(ret);
2331
2332 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2333 return 0;
2334 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2335 continue;
2336 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2337 return 0;
2338 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2339 continue;
2340
2341 /* Check too big values. */
2342 if ((unsigned int)c > 0xffff) {
2343 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2344 return 0;
2345 continue;
2346 }
2347 }
2348
2349 /* Convert character. */
2350 if (c == '"') {
2351 len = 2;
2352 str = "\\\"";
2353 }
2354 else if (c == '\\') {
2355 len = 2;
2356 str = "\\\\";
2357 }
2358 else if (c == '/') {
2359 len = 2;
2360 str = "\\/";
2361 }
2362 else if (c == '\b') {
2363 len = 2;
2364 str = "\\b";
2365 }
2366 else if (c == '\f') {
2367 len = 2;
2368 str = "\\f";
2369 }
2370 else if (c == '\r') {
2371 len = 2;
2372 str = "\\r";
2373 }
2374 else if (c == '\n') {
2375 len = 2;
2376 str = "\\n";
2377 }
2378 else if (c == '\t') {
2379 len = 2;
2380 str = "\\t";
2381 }
Willy Tarreau90807112020-02-25 08:16:33 +01002382 else if (c > 0xff || !isprint((unsigned char)c)) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002383 /* isprint generate a segfault if c is too big. The man says that
2384 * c must have the value of an unsigned char or EOF.
2385 */
2386 len = 6;
2387 _str[0] = '\\';
2388 _str[1] = 'u';
2389 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2390 str = _str;
2391 }
2392 else {
2393 len = 1;
Willy Tarreau5715da22020-02-25 08:37:37 +01002394 _str[0] = c;
2395 str = _str;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002396 }
2397
2398 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002399 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002400 return 0;
2401
2402 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002403 memcpy(temp->area + temp->data, str, len);
2404 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002405 }
2406
2407 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002408 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002409 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002410
2411 return 1;
2412}
2413
Emeric Brun54c4ac82014-11-03 15:32:43 +01002414/* This sample function is designed to extract some bytes from an input buffer.
2415 * First arg is the offset.
2416 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002417static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002418{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002419 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2420 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002421 return 1;
2422 }
2423
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002424 if (smp->data.u.str.size)
2425 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002426 smp->data.u.str.data -= arg_p[0].data.sint;
2427 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002428
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002429 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2430 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002431
2432 return 1;
2433}
2434
Emeric Brunf399b0d2014-11-03 17:07:03 +01002435static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2436 const char *file, int line, char **err)
2437{
2438 struct arg *arg = args;
2439
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002440 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002441 memprintf(err, "Unexpected arg type");
2442 return 0;
2443 }
2444
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002445 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002446 memprintf(err, "Unexpected value 0 for index");
2447 return 0;
2448 }
2449
2450 arg++;
2451
2452 if (arg->type != ARGT_STR) {
2453 memprintf(err, "Unexpected arg type");
2454 return 0;
2455 }
2456
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002457 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002458 memprintf(err, "Empty separators list");
2459 return 0;
2460 }
2461
2462 return 1;
2463}
2464
2465/* This sample function is designed to a return selected part of a string (field).
2466 * First arg is the index of the field (start at 1)
2467 * Second arg is a char list of separators (type string)
2468 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002469static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002470{
Marcin Deranek9631a282018-04-16 14:30:46 +02002471 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002472 char *start, *end;
2473 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002474 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002475
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002476 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002477 return 0;
2478
Marcin Deranek9631a282018-04-16 14:30:46 +02002479 if (arg_p[0].data.sint < 0) {
2480 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002481 end = start = smp->data.u.str.area + smp->data.u.str.data;
2482 while (start > smp->data.u.str.area) {
2483 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2484 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002485 if (field == arg_p[0].data.sint) {
2486 if (count == 1)
2487 goto found;
2488 else if (count > 1)
2489 count--;
2490 } else {
2491 end = start-1;
2492 field--;
2493 }
2494 break;
2495 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002496 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002497 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002498 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002499 } else {
2500 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002501 end = start = smp->data.u.str.area;
2502 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2503 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2504 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002505 if (field == arg_p[0].data.sint) {
2506 if (count == 1)
2507 goto found;
2508 else if (count > 1)
2509 count--;
2510 } else {
2511 start = end+1;
2512 field++;
2513 }
2514 break;
2515 }
2516 }
2517 end++;
2518 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002519 }
2520
2521 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002522 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002523 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002524 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002525 }
2526found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002527 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002528 /* If ret string is len 0, no need to
2529 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002530 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002531 return 1;
2532
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002533 smp->data.u.str.area = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002534
2535 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002536 Note: smp->data.u.str.size cannot be set to 0 */
2537 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002538 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002539
2540 return 1;
2541}
2542
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002543/* This sample function is designed to return a word from a string.
2544 * First arg is the index of the word (start at 1)
2545 * Second arg is a char list of words separators (type string)
2546 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002547static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002548{
Marcin Deranek9631a282018-04-16 14:30:46 +02002549 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002550 char *start, *end;
2551 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002552 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002553
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002554 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002555 return 0;
2556
2557 word = 0;
2558 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002559 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002560 end = start = smp->data.u.str.area + smp->data.u.str.data;
2561 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002562 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002563 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2564 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002565 issep = 1;
2566 break;
2567 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002568 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002569 if (!inword) {
2570 if (!issep) {
2571 if (word != arg_p[0].data.sint) {
2572 word--;
2573 end = start;
2574 }
2575 inword = 1;
2576 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002577 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002578 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002579 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002580 if (count == 1)
2581 goto found;
2582 else if (count > 1)
2583 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002584 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002585 inword = 0;
2586 }
2587 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002588 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002589 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002590 end = start = smp->data.u.str.area;
2591 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002592 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002593 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2594 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002595 issep = 1;
2596 break;
2597 }
2598 }
2599 if (!inword) {
2600 if (!issep) {
2601 if (word != arg_p[0].data.sint) {
2602 word++;
2603 start = end;
2604 }
2605 inword = 1;
2606 }
2607 }
2608 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002609 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002610 if (count == 1)
2611 goto found;
2612 else if (count > 1)
2613 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002614 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002615 inword = 0;
2616 }
2617 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002618 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002619 }
2620
2621 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002622 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002623 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002624 return 1;
2625 }
2626found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002627 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002628 /* If ret string is len 0, no need to
2629 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002630 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002631 return 1;
2632
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002633 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002634
2635 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002636 Note: smp->data.u.str.size cannot be set to 0 */
2637 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002638 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002639
2640 return 1;
2641}
2642
Willy Tarreau7eda8492015-01-20 19:47:06 +01002643static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2644 const char *file, int line, char **err)
2645{
2646 struct arg *arg = args;
2647 char *p;
2648 int len;
2649
2650 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2651 arg[0].type_flags = 0;
2652
2653 if (arg[2].type != ARGT_STR)
2654 return 1;
2655
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002656 p = arg[2].data.str.area;
2657 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002658 while (len) {
2659 if (*p == 'i') {
2660 arg[0].type_flags |= ARGF_REG_ICASE;
2661 }
2662 else if (*p == 'g') {
2663 arg[0].type_flags |= ARGF_REG_GLOB;
2664 }
2665 else {
2666 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2667 return 0;
2668 }
2669 p++;
2670 len--;
2671 }
2672 return 1;
2673}
2674
2675/* This sample function is designed to do the equivalent of s/match/replace/ on
2676 * the input string. It applies a regex and restarts from the last matched
2677 * location until nothing matches anymore. First arg is the regex to apply to
2678 * the input string, second arg is the replacement expression.
2679 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002680static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002681{
2682 char *start, *end;
2683 struct my_regex *reg = arg_p[0].data.reg;
2684 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002685 struct buffer *trash = get_trash_chunk();
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002686 struct buffer *output;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002687 int flag, max;
2688 int found;
2689
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002690 start = smp->data.u.str.area;
2691 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002692
2693 flag = 0;
2694 while (1) {
2695 /* check for last round which is used to copy remaining parts
2696 * when not running in global replacement mode.
2697 */
2698 found = 0;
2699 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2700 /* Note: we can have start == end on empty strings or at the end */
2701 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2702 }
2703
2704 if (!found)
2705 pmatch[0].rm_so = end - start;
2706
2707 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002708 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002709 if (max && pmatch[0].rm_so > 0) {
2710 if (max > pmatch[0].rm_so)
2711 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002712 memcpy(trash->area + trash->data, start, max);
2713 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002714 }
2715
2716 if (!found)
2717 break;
2718
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002719 output = alloc_trash_chunk();
Willy Tarreau23997da2020-02-18 14:27:44 +01002720 if (!output)
2721 break;
2722
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002723 output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2724
Willy Tarreau7eda8492015-01-20 19:47:06 +01002725 /* replace the matching part */
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002726 max = output->size - output->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002727 if (max) {
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002728 if (max > output->data)
2729 max = output->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002730 memcpy(trash->area + trash->data,
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002731 output->area, max);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002732 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002733 }
2734
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002735 free_trash_chunk(output);
2736
Willy Tarreau7eda8492015-01-20 19:47:06 +01002737 /* stop here if we're done with this string */
2738 if (start >= end)
2739 break;
2740
2741 /* We have a special case for matches of length 0 (eg: "x*y*").
2742 * These ones are considered to match in front of a character,
2743 * so we have to copy that character and skip to the next one.
2744 */
2745 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002746 if (trash->data < trash->size)
2747 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002748 pmatch[0].rm_eo++;
2749 }
2750
2751 start += pmatch[0].rm_eo;
2752 flag |= REG_NOTBOL;
2753 }
2754
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002755 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002756 return 1;
2757}
2758
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002759/* This function check an operator entry. It expects a string.
2760 * The string can be an integer or a variable name.
2761 */
2762static int check_operator(struct arg *args, struct sample_conv *conv,
2763 const char *file, int line, char **err)
2764{
2765 const char *str;
2766 const char *end;
Christopher Faulet95917132020-08-05 23:07:07 +02002767 long long int i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002768
2769 /* Try to decode a variable. */
2770 if (vars_check_arg(&args[0], NULL))
2771 return 1;
2772
2773 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002774 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002775 end = str + strlen(str);
Christopher Faulet95917132020-08-05 23:07:07 +02002776 i = read_int64(&str, end);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002777 if (*str != '\0') {
2778 memprintf(err, "expects an integer or a variable name");
2779 return 0;
2780 }
Christopher Faulet95917132020-08-05 23:07:07 +02002781
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002782 chunk_destroy(&args[0].data.str);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002783 args[0].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02002784 args[0].data.sint = i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002785 return 1;
2786}
2787
Willy Tarreau6204cd92016-03-10 16:33:04 +01002788/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002789 * If the arg contain an integer, the integer is returned in the
2790 * sample. If the arg contains a variable descriptor, it returns the
2791 * variable value.
2792 *
2793 * This function returns 0 if an error occurs, otherwise it returns 1.
2794 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002795static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002796{
2797 switch (arg->type) {
2798 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002799 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002800 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002801 return 1;
2802 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002803 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002804 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002805 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002806 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002807 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002808 return 0;
2809 return 1;
2810 default:
2811 return 0;
2812 }
2813}
2814
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002815/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002816 * result.
2817 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002818static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002819{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002820 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002821 return 1;
2822}
2823
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002824/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002825 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002826 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002827static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002828{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002829 struct sample tmp;
2830
Willy Tarreau7560dd42016-03-10 16:28:58 +01002831 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002832 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002833 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002834 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002835 return 1;
2836}
2837
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002838/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002839 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002840 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002841static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002842{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002843 struct sample tmp;
2844
Willy Tarreau7560dd42016-03-10 16:28:58 +01002845 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002846 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002847 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002848 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002849 return 1;
2850}
2851
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002852/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002853 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002854 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002855static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002856{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002857 struct sample tmp;
2858
Willy Tarreau7560dd42016-03-10 16:28:58 +01002859 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002860 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002861 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002862 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002863 return 1;
2864}
2865
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002866static inline long long int arith_add(long long int a, long long int b)
2867{
2868 /* Prevent overflow and makes capped calculus.
2869 * We must ensure that the check calculus doesn't
2870 * exceed the signed 64 bits limits.
2871 *
2872 * +----------+----------+
2873 * | a<0 | a>=0 |
2874 * +------+----------+----------+
2875 * | b<0 | MIN-a>b | no check |
2876 * +------+----------+----------+
2877 * | b>=0 | no check | MAX-a<b |
2878 * +------+----------+----------+
2879 */
2880 if ((a ^ b) >= 0) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002881 /* signs are different. */
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002882 if (a < 0) {
2883 if (LLONG_MIN - a > b)
2884 return LLONG_MIN;
2885 }
2886 if (LLONG_MAX - a < b)
2887 return LLONG_MAX;
2888 }
2889 return a + b;
2890}
2891
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002892/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002893 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002894 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002895static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002896{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002897 struct sample tmp;
2898
Willy Tarreau7560dd42016-03-10 16:28:58 +01002899 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002900 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002901 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002902 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002903 return 1;
2904}
2905
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002906/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002907 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002908 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002909static int sample_conv_arith_sub(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 FOURNIER00c005c2015-07-08 01:10:21 +02002918 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2919 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2920 * of -LLONG_MIN and correct the result.
2921 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002922 if (tmp.data.u.sint == LLONG_MIN) {
2923 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2924 if (smp->data.u.sint < LLONG_MAX)
2925 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002926 return 1;
2927 }
2928
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002929 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002930 * the second operand.
2931 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002932 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002933 return 1;
2934}
2935
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002936/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002937 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002938 * If the result makes an overflow, then the largest possible quantity is
2939 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002940 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002941static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002942 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002943{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002944 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002945 long long int c;
2946
Willy Tarreau7560dd42016-03-10 16:28:58 +01002947 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002948 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002949 return 0;
2950
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002951 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002952 if (!smp->data.u.sint || !tmp.data.u.sint) {
2953 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002954 return 1;
2955 }
2956
2957 /* The multiply between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002958 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002959 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002960 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2961 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002962 return 1;
2963 }
2964
2965 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002966 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002967
2968 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002969 if (smp->data.u.sint != c / tmp.data.u.sint) {
2970 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2971 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002972 return 1;
2973 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002974 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002975 return 1;
2976 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002977 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002978 return 1;
2979}
2980
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002981/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002982 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002983 * If arg_p makes the result overflow, then the largest possible quantity is
2984 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002985 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002986static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002987 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002988{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002989 struct sample tmp;
2990
Willy Tarreau7560dd42016-03-10 16:28:58 +01002991 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002992 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002993 return 0;
2994
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002995 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002996 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002997 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002998 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002999 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3000 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003001 return 1;
3002 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003003 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003004 return 1;
3005 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003006 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01003007 return 1;
3008}
3009
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003010/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003011 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003012 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003013 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003014static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003015 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003016{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003017 struct sample tmp;
3018
Willy Tarreau7560dd42016-03-10 16:28:58 +01003019 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01003020 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003021 return 0;
3022
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003023 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003024 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003025 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003026 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003027 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3028 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003029 return 1;
3030 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003031 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003032 return 1;
3033 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003034 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01003035 return 1;
3036}
3037
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003038/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01003039 * result.
3040 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003041static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003042 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003043{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003044 if (smp->data.u.sint == LLONG_MIN)
3045 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003046 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003047 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003048 return 1;
3049}
3050
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003051/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003052 * false. The output is a BOOL.
3053 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003054static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003055 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003056{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003057 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003058 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003059 return 1;
3060}
3061
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003062/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003063 * truee. The output is a BOOL.
3064 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003065static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003066 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003067{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003068 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003069 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003070 return 1;
3071}
3072
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003073/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003074 * The output is a BOOL.
3075 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003076static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003077 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003078{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003079 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003080 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003081 return 1;
3082}
3083
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003084/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003085 * The output is a BOOL.
3086 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003087static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003088 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003089{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003090 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003091 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003092 return 1;
3093}
3094
Willy Tarreau280f42b2018-02-19 15:34:12 +01003095/* appends an optional const string, an optional variable contents and another
3096 * optional const string to an existing string.
3097 */
3098static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
3099{
Willy Tarreau83061a82018-07-13 11:56:34 +02003100 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003101 struct sample tmp;
3102 int max;
3103
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003104 trash = alloc_trash_chunk();
William Dauchye9970102021-01-11 11:05:58 +01003105 if (!trash)
3106 return 0;
3107
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003108 trash->data = smp->data.u.str.data;
3109 if (trash->data > trash->size - 1)
3110 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003111
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003112 memcpy(trash->area, smp->data.u.str.area, trash->data);
3113 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003114
3115 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003116 max = arg_p[0].data.str.data;
3117 if (max > trash->size - 1 - trash->data)
3118 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003119
3120 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003121 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
3122 trash->data += max;
3123 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003124 }
3125
3126 /* append second string (variable) if it's found and we can turn it
3127 * into a string.
3128 */
3129 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3130 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
3131 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
3132 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
3133
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003134 max = tmp.data.u.str.data;
3135 if (max > trash->size - 1 - trash->data)
3136 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003137
3138 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003139 memcpy(trash->area + trash->data, tmp.data.u.str.area,
3140 max);
3141 trash->data += max;
3142 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003143 }
3144 }
3145
3146 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003147 max = arg_p[2].data.str.data;
3148 if (max > trash->size - 1 - trash->data)
3149 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003150
3151 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003152 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
3153 trash->data += max;
3154 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003155 }
3156
3157 smp->data.u.str = *trash;
3158 smp->data.type = SMP_T_STR;
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003159 smp_dup(smp);
3160 free_trash_chunk(trash);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003161 return 1;
3162}
3163
3164/* This function checks the "concat" converter's arguments and extracts the
3165 * variable name and its scope.
3166 */
3167static int smp_check_concat(struct arg *args, struct sample_conv *conv,
3168 const char *file, int line, char **err)
3169{
3170 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003171 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
3172 memprintf(err, "failed to register variable name '%s'",
3173 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003174 return 0;
3175 }
3176 return 1;
3177}
3178
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003179/* Compares string with a variable containing a string. Return value
Tim Duesterhusca097c12018-04-27 21:18:45 +02003180 * is compatible with strcmp(3)'s return value.
3181 */
3182static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
3183{
3184 struct sample tmp;
3185 int max, result;
3186
3187 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3188 if (arg_p[0].type != ARGT_VAR)
3189 return 0;
3190 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
3191 return 0;
3192 if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp))
3193 return 0;
3194
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003195 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
3196 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003197 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003198 if (smp->data.u.str.data != tmp.data.u.str.data) {
3199 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02003200 result = -1;
3201 }
3202 else {
3203 result = 1;
3204 }
3205 }
3206 }
3207
3208 smp->data.u.sint = result;
3209 smp->data.type = SMP_T_SINT;
3210 return 1;
3211}
3212
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003213#ifdef USE_OPENSSL
3214/* Compares bytestring with a variable containing a bytestring. Return value
3215 * is `true` if both bytestrings are bytewise identical and `false` otherwise.
3216 *
3217 * Comparison will be performed in constant time if both bytestrings are of
3218 * the same length. If the lengths differ execution time will not be constant.
3219 */
3220static int sample_conv_secure_memcmp(const struct arg *arg_p, struct sample *smp, void *private)
3221{
3222 struct sample tmp;
3223 int result;
3224
3225 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3226 if (arg_p[0].type != ARGT_VAR)
3227 return 0;
3228 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
3229 return 0;
3230 if (!sample_casts[tmp.data.type][SMP_T_BIN](&tmp))
3231 return 0;
3232
3233 if (smp->data.u.str.data != tmp.data.u.str.data) {
3234 smp->data.u.sint = 0;
3235 smp->data.type = SMP_T_BOOL;
3236 return 1;
3237 }
3238
3239 /* The following comparison is performed in constant time. */
3240 result = CRYPTO_memcmp(smp->data.u.str.area, tmp.data.u.str.area, smp->data.u.str.data);
3241
3242 smp->data.u.sint = result == 0;
3243 smp->data.type = SMP_T_BOOL;
3244 return 1;
3245}
3246#endif
3247
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02003248/* Takes a boolean as input. Returns the first argument if that boolean is true and
3249 * the second argument otherwise.
3250 */
3251static int sample_conv_iif(const struct arg *arg_p, struct sample *smp, void *private)
3252{
3253 smp->data.type = SMP_T_STR;
3254 smp->flags |= SMP_F_CONST;
3255
3256 if (smp->data.u.sint) {
3257 smp->data.u.str.data = arg_p[0].data.str.data;
3258 smp->data.u.str.area = arg_p[0].data.str.area;
3259 }
3260 else {
3261 smp->data.u.str.data = arg_p[1].data.str.data;
3262 smp->data.u.str.area = arg_p[1].data.str.area;
3263 }
3264
3265 return 1;
3266}
3267
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003268#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
3269#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
3270#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
3271
3272/*
3273 * Extract the field value of an input binary sample. Takes a mandatory argument:
3274 * the protocol buffers field identifier (dotted notation) internally represented
3275 * as an array of unsigned integers and its size.
3276 * Return 1 if the field was found, 0 if not.
3277 */
3278static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
3279{
3280 unsigned char *pos;
3281 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003282
3283 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003284 grpc_left = smp->data.u.str.data;
3285
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01003286 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003287 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003288
3289 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
3290
3291 pos += GRPC_MSG_HEADER_SZ;
3292 grpc_left -= GRPC_MSG_HEADER_SZ;
3293
3294 if (grpc_left < left)
3295 return 0;
3296
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01003297 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
3298 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003299
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003300 grpc_left -= grpc_msg_len;
3301 }
3302
3303 return 0;
3304}
3305
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003306static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
3307{
3308 unsigned char *pos;
3309 size_t left;
3310
3311 pos = (unsigned char *)smp->data.u.str.area;
3312 left = smp->data.u.str.data;
3313
3314 return protobuf_field_lookup(arg_p, smp, &pos, &left);
3315}
3316
3317static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
3318 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003319{
3320 if (!args[1].type) {
3321 args[1].type = ARGT_SINT;
3322 args[1].data.sint = PBUF_T_BINARY;
3323 }
3324 else {
3325 int pbuf_type;
3326
3327 pbuf_type = protobuf_type(args[1].data.str.area);
3328 if (pbuf_type == -1) {
3329 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
3330 return 0;
3331 }
3332
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003333 chunk_destroy(&args[1].data.str);
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003334 args[1].type = ARGT_SINT;
3335 args[1].data.sint = pbuf_type;
3336 }
3337
3338 return 1;
3339}
3340
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003341/*
3342 * Extract the tag value of an input binary sample. Takes a mandatory argument:
3343 * the FIX protocol tag identifier.
3344 * Return 1 if the tag was found, 0 if not.
3345 */
3346static int sample_conv_fix_tag_value(const struct arg *arg_p, struct sample *smp, void *private)
3347{
3348 struct ist value;
3349
3350 smp->flags &= ~SMP_F_MAY_CHANGE;
3351 value = fix_tag_value(ist2(smp->data.u.str.area, smp->data.u.str.data),
3352 arg_p[0].data.sint);
3353 if (!istlen(value)) {
3354 if (!isttest(value)) {
3355 /* value != IST_NULL, need more data */
3356 smp->flags |= SMP_F_MAY_CHANGE;
3357 }
3358 return 0;
3359 }
3360
3361 smp->data.u.str = ist2buf(value);
3362 smp->flags |= SMP_F_CONST;
3363
3364 return 1;
3365}
3366
3367/* This function checks the "fix_tag_value" converter configuration.
3368 * It expects a "known" (by HAProxy) tag name or ID.
3369 * Tag string names are converted to their ID counterpart because this is the
3370 * format they are sent over the wire.
3371 */
3372static int sample_conv_fix_value_check(struct arg *args, struct sample_conv *conv,
3373 const char *file, int line, char **err)
3374{
3375 struct ist str;
3376 unsigned int tag;
3377
3378 str = ist2(args[0].data.str.area, args[0].data.str.data);
3379 tag = fix_tagid(str);
3380 if (!tag) {
3381 memprintf(err, "Unknown FIX tag name '%s'", args[0].data.str.area);
3382 return 0;
3383 }
3384
3385 chunk_destroy(&args[0].data.str);
3386 args[0].type = ARGT_SINT;
3387 args[0].data.sint = tag;
3388
3389 return 1;
3390}
3391
3392/*
3393 * Checks that a buffer contains a valid FIX message
3394 *
3395 * Return 1 if the check could be run, 0 if not.
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003396 * The result of the analyse itself is stored in <smp> as a boolean
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003397 */
3398static int sample_conv_fix_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3399{
3400 struct ist msg;
3401
3402 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3403
3404 smp->flags &= ~SMP_F_MAY_CHANGE;
3405 switch (fix_validate_message(msg)) {
3406 case FIX_VALID_MESSAGE:
3407 smp->data.type = SMP_T_BOOL;
3408 smp->data.u.sint = 1;
3409 return 1;
3410 case FIX_NEED_MORE_DATA:
3411 smp->flags |= SMP_F_MAY_CHANGE;
3412 return 0;
3413 case FIX_INVALID_MESSAGE:
3414 smp->data.type = SMP_T_BOOL;
3415 smp->data.u.sint = 0;
3416 return 1;
3417 }
3418 return 0;
3419}
3420
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003421/*
3422 * Extract the field value of an input binary sample containing an MQTT packet.
3423 * Takes 2 mandatory arguments:
3424 * - packet type
3425 * - field name
3426 *
3427 * return 1 if the field was found, 0 if not.
3428 */
3429static int sample_conv_mqtt_field_value(const struct arg *arg_p, struct sample *smp, void *private)
3430{
3431 struct ist pkt, value;
3432 int type, fieldname_id;
3433
3434 pkt = ist2(smp->data.u.str.area, smp->data.u.str.data);
3435 type = arg_p[0].data.sint;
3436 fieldname_id = arg_p[1].data.sint;
3437
3438 smp->flags &= ~SMP_F_MAY_CHANGE;
3439 value = mqtt_field_value(pkt, type, fieldname_id);
3440 if (!istlen(value)) {
3441 if (isttest(value)) {
3442 /* value != IST_NULL, need more data */
3443 smp->flags |= SMP_F_MAY_CHANGE;
3444 }
3445 return 0;
3446 }
3447
3448 smp->data.u.str = ist2buf(value);
3449 smp->flags |= SMP_F_CONST;
3450 return 1;
3451}
3452
3453/*
3454 * this function checks the "mqtt_field_value" converter configuration.
3455 * It expects a known packet type name or ID and a field name, in this order
3456 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003457 * Args[0] will be turned into a MQTT_CPT_* value for direct matching when parsing
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003458 * a packet.
3459 */
3460static int sample_conv_mqtt_field_value_check(struct arg *args, struct sample_conv *conv,
3461 const char *file, int line, char **err)
3462{
3463 int type, fieldname_id;
3464
3465 /* check the MQTT packet type is valid */
3466 type = mqtt_typeid(ist2(args[0].data.str.area, args[0].data.str.data));
3467 if (type == MQTT_CPT_INVALID) {
3468 memprintf(err, "Unknown MQTT type '%s'", args[0].data.str.area);
3469 return 0;
3470 }
3471
3472 /* check the field name belongs to the MQTT packet type */
3473 fieldname_id = mqtt_check_type_fieldname(type, ist2(args[1].data.str.area, args[1].data.str.data));
3474 if (fieldname_id == MQTT_FN_INVALID) {
3475 memprintf(err, "Unknown MQTT field name '%s' for packet type '%s'", args[1].data.str.area,
3476 args[0].data.str.area);
3477 return 0;
3478 }
3479
3480 /* save numeric counterparts of type and field name */
3481 chunk_destroy(&args[0].data.str);
3482 chunk_destroy(&args[1].data.str);
3483 args[0].type = ARGT_SINT;
3484 args[0].data.sint = type;
3485 args[1].type = ARGT_SINT;
3486 args[1].data.sint = fieldname_id;
3487
3488 return 1;
3489}
3490
3491/*
3492 * Checks that <smp> contains a valid MQTT message
3493 *
3494 * The function returns 1 if the check was run to its end, 0 otherwise.
3495 * The result of the analyse itself is stored in <smp> as a boolean.
3496 */
3497static int sample_conv_mqtt_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3498{
3499 struct ist msg;
3500
3501 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3502
3503 smp->flags &= ~SMP_F_MAY_CHANGE;
3504 switch (mqtt_validate_message(msg, NULL)) {
3505 case FIX_VALID_MESSAGE:
3506 smp->data.type = SMP_T_BOOL;
3507 smp->data.u.sint = 1;
3508 return 1;
3509 case FIX_NEED_MORE_DATA:
3510 smp->flags |= SMP_F_MAY_CHANGE;
3511 return 0;
3512 case FIX_INVALID_MESSAGE:
3513 smp->data.type = SMP_T_BOOL;
3514 smp->data.u.sint = 0;
3515 return 1;
3516 }
3517 return 0;
3518}
3519
Tim Duesterhusca097c12018-04-27 21:18:45 +02003520/* This function checks the "strcmp" converter's arguments and extracts the
3521 * variable name and its scope.
3522 */
3523static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
3524 const char *file, int line, char **err)
3525{
3526 /* Try to decode a variable. */
3527 if (vars_check_arg(&args[0], NULL))
3528 return 1;
3529
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003530 memprintf(err, "failed to register variable name '%s'",
3531 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003532 return 0;
3533}
3534
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003535#ifdef USE_OPENSSL
3536/* This function checks the "secure_memcmp" converter's arguments and extracts the
3537 * variable name and its scope.
3538 */
3539static int smp_check_secure_memcmp(struct arg *args, struct sample_conv *conv,
3540 const char *file, int line, char **err)
3541{
3542 /* Try to decode a variable. */
3543 if (vars_check_arg(&args[0], NULL))
3544 return 1;
3545
3546 memprintf(err, "failed to register variable name '%s'",
3547 args[0].data.str.area);
3548 return 0;
3549}
3550#endif
3551
Christopher Faulet4ccc12f2020-04-01 09:08:32 +02003552/**/
3553static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private)
3554{
3555 struct buffer *tmp;
3556 uint32_t n;
3557
3558 n = htonl((uint32_t)smp->data.u.sint);
3559 tmp = get_trash_chunk();
3560
3561 memcpy(b_head(tmp), &n, 4);
3562 b_add(tmp, 4);
3563
3564 smp->data.u.str = *tmp;
3565 smp->data.type = SMP_T_BIN;
3566 return 1;
3567}
3568
Christopher Fauletea159d62020-04-01 16:21:44 +02003569/**/
3570static int sample_conv_cut_crlf(const struct arg *arg_p, struct sample *smp, void *private)
3571{
3572 char *p;
3573 size_t l;
3574
3575 p = smp->data.u.str.area;
3576 for (l = 0; l < smp->data.u.str.data; l++) {
3577 if (*(p+l) == '\r' || *(p+l) == '\n')
3578 break;
3579 }
3580 smp->data.u.str.data = l;
3581 return 1;
3582}
3583
Christopher Faulet51fc9d12020-04-01 17:24:41 +02003584/**/
3585static int sample_conv_ltrim(const struct arg *arg_p, struct sample *smp, void *private)
3586{
3587 char *delimiters, *p;
3588 size_t dlen, l;
3589
3590 delimiters = arg_p[0].data.str.area;
3591 dlen = arg_p[0].data.str.data;
3592
3593 l = smp->data.u.str.data;
3594 p = smp->data.u.str.area;
3595 while (l && memchr(delimiters, *p, dlen) != NULL) {
3596 p++;
3597 l--;
3598 }
3599
3600 smp->data.u.str.area = p;
3601 smp->data.u.str.data = l;
3602 return 1;
3603}
3604
Christopher Faulet568415a2020-04-01 17:24:47 +02003605/**/
3606static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *private)
3607{
3608 char *delimiters, *p;
3609 size_t dlen, l;
3610
3611 delimiters = arg_p[0].data.str.area;
3612 dlen = arg_p[0].data.str.data;
3613
3614 l = smp->data.u.str.data;
3615 p = smp->data.u.str.area + l - 1;
3616 while (l && memchr(delimiters, *p, dlen) != NULL) {
3617 p--;
3618 l--;
3619 }
3620
3621 smp->data.u.str.data = l;
3622 return 1;
3623}
3624
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003625/************************************************************************/
3626/* All supported sample fetch functions must be declared here */
3627/************************************************************************/
3628
3629/* force TRUE to be returned at the fetch level */
3630static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003631smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003632{
Willy Tarreau280f42b2018-02-19 15:34:12 +01003633 if (!smp_make_rw(smp))
3634 return 0;
3635
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003636 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003637 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003638 return 1;
3639}
3640
3641/* force FALSE to be returned at the fetch level */
3642static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003643smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003644{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003645 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003646 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003647 return 1;
3648}
3649
3650/* retrieve environment variable $1 as a string */
3651static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003652smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003653{
3654 char *env;
3655
Christopher Faulete6e7a582021-01-29 11:29:28 +01003656 if (args[0].type != ARGT_STR)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003657 return 0;
3658
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003659 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003660 if (!env)
3661 return 0;
3662
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003663 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003664 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003665 smp->data.u.str.area = env;
3666 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003667 return 1;
3668}
3669
Damien Claisseae6f1252019-10-30 15:57:28 +00003670/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3671 * optional string representing the unit of the result: "s" for seconds, "ms" for
3672 * milliseconds and "us" for microseconds.
3673 * Returns 0 on error and non-zero if OK.
3674 */
3675int smp_check_date_unit(struct arg *args, char **err)
3676{
3677 if (args[1].type == ARGT_STR) {
Christopher Faulet95917132020-08-05 23:07:07 +02003678 long long int unit;
3679
Damien Claisseae6f1252019-10-30 15:57:28 +00003680 if (strcmp(args[1].data.str.area, "s") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003681 unit = TIME_UNIT_S;
Damien Claisseae6f1252019-10-30 15:57:28 +00003682 }
3683 else if (strcmp(args[1].data.str.area, "ms") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003684 unit = TIME_UNIT_MS;
Damien Claisseae6f1252019-10-30 15:57:28 +00003685 }
3686 else if (strcmp(args[1].data.str.area, "us") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003687 unit = TIME_UNIT_US;
Damien Claisseae6f1252019-10-30 15:57:28 +00003688 }
3689 else {
3690 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
3691 args[1].data.str.area);
3692 return 0;
3693 }
Christopher Faulet95917132020-08-05 23:07:07 +02003694
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003695 chunk_destroy(&args[1].data.str);
Damien Claisseae6f1252019-10-30 15:57:28 +00003696 args[1].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02003697 args[1].data.sint = unit;
Damien Claisseae6f1252019-10-30 15:57:28 +00003698 }
3699 else if (args[1].type != ARGT_STOP) {
3700 memprintf(err, "Unexpected arg type");
3701 return 0;
3702 }
3703
3704 return 1;
3705}
3706
3707/* retrieve the current local date in epoch time, converts it to milliseconds
3708 * or microseconds if asked to in optional args[1] unit param, and applies an
3709 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003710 */
3711static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003712smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003713{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003714 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003715
Damien Claisseae6f1252019-10-30 15:57:28 +00003716 /* report in milliseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003717 if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003718 smp->data.u.sint *= 1000;
3719 smp->data.u.sint += date.tv_usec / 1000;
3720 }
3721 /* report in microseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003722 else if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003723 smp->data.u.sint *= 1000000;
3724 smp->data.u.sint += date.tv_usec;
3725 }
3726
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003727 /* add offset */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003728 if (args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003729 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003730
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003731 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003732 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3733 return 1;
3734}
3735
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003736/* retrieve the current microsecond part of the date */
3737static int
3738smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3739{
3740 smp->data.u.sint = date.tv_usec;
3741 smp->data.type = SMP_T_SINT;
3742 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3743 return 1;
3744}
3745
3746
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003747/* returns the hostname */
3748static int
3749smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3750{
3751 smp->data.type = SMP_T_STR;
3752 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003753 smp->data.u.str.area = hostname;
3754 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003755 return 1;
3756}
3757
Willy Tarreau0f30d262014-11-24 16:02:05 +01003758/* returns the number of processes */
3759static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003760smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003761{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003762 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003763 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003764 return 1;
3765}
3766
3767/* returns the number of the current process (between 1 and nbproc */
3768static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003769smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003770{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003771 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003772 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003773 return 1;
3774}
3775
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003776/* returns the number of the current thread (between 1 and nbthread */
3777static int
3778smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3779{
3780 smp->data.type = SMP_T_SINT;
3781 smp->data.u.sint = tid;
3782 return 1;
3783}
3784
Willy Tarreau84310e22014-02-14 11:59:04 +01003785/* generate a random 32-bit integer for whatever purpose, with an optional
3786 * range specified in argument.
3787 */
3788static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003789smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01003790{
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003791 smp->data.u.sint = ha_random32();
Willy Tarreau84310e22014-02-14 11:59:04 +01003792
3793 /* reduce if needed. Don't do a modulo, use all bits! */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003794 if (args[0].type == ARGT_SINT)
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003795 smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32;
Willy Tarreau84310e22014-02-14 11:59:04 +01003796
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003797 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01003798 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3799 return 1;
3800}
3801
Willy Tarreau0f30d262014-11-24 16:02:05 +01003802/* returns true if the current process is stopping */
3803static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003804smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003805{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003806 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003807 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003808 return 1;
3809}
3810
Willy Tarreau70fe9442018-11-22 16:07:39 +01003811/* returns the number of calls of the current stream's process_stream() */
3812static int
3813smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
3814{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003815 if (!smp->strm)
3816 return 0;
3817
Willy Tarreau70fe9442018-11-22 16:07:39 +01003818 smp->data.type = SMP_T_SINT;
3819 smp->data.u.sint = smp->strm->task->calls;
3820 return 1;
3821}
3822
3823/* returns the average number of nanoseconds spent processing the stream per call */
3824static int
3825smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3826{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003827 if (!smp->strm)
3828 return 0;
3829
Willy Tarreau70fe9442018-11-22 16:07:39 +01003830 smp->data.type = SMP_T_SINT;
3831 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
3832 return 1;
3833}
3834
3835/* returns the total number of nanoseconds spent processing the stream */
3836static int
3837smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3838{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003839 if (!smp->strm)
3840 return 0;
3841
Willy Tarreau70fe9442018-11-22 16:07:39 +01003842 smp->data.type = SMP_T_SINT;
3843 smp->data.u.sint = smp->strm->task->cpu_time;
3844 return 1;
3845}
3846
3847/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
3848static int
3849smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3850{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003851 if (!smp->strm)
3852 return 0;
3853
Willy Tarreau70fe9442018-11-22 16:07:39 +01003854 smp->data.type = SMP_T_SINT;
3855 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
3856 return 1;
3857}
3858
3859/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
3860static int
3861smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3862{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003863 if (!smp->strm)
3864 return 0;
3865
Willy Tarreau70fe9442018-11-22 16:07:39 +01003866 smp->data.type = SMP_T_SINT;
3867 smp->data.u.sint = smp->strm->task->lat_time;
3868 return 1;
3869}
3870
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003871static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
3872{
3873 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003874 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003875 smp->data.u.str.area = args[0].data.str.area;
3876 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003877 return 1;
3878}
3879
3880static int smp_check_const_bool(struct arg *args, char **err)
3881{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003882 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
3883 strcasecmp(args[0].data.str.area, "1") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003884 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003885 args[0].type = ARGT_SINT;
3886 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003887 return 1;
3888 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003889 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
3890 strcasecmp(args[0].data.str.area, "0") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003891 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003892 args[0].type = ARGT_SINT;
3893 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003894 return 1;
3895 }
3896 memprintf(err, "Expects 'true', 'false', '0' or '1'");
3897 return 0;
3898}
3899
3900static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
3901{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003902 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003903 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003904 return 1;
3905}
3906
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003907static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003908{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003909 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003910 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003911 return 1;
3912}
3913
3914static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
3915{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003916 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003917 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003918 return 1;
3919}
3920
3921static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
3922{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003923 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003924 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003925 return 1;
3926}
3927
3928static int smp_check_const_bin(struct arg *args, char **err)
3929{
David Carlier64a16ab2016-04-08 10:37:02 +01003930 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003931 int binstrlen;
3932
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003933 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003934 return 0;
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003935 chunk_destroy(&args[0].data.str);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003936 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003937 args[0].data.str.area = binstr;
3938 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003939 return 1;
3940}
3941
3942static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
3943{
3944 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003945 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003946 smp->data.u.str.area = args[0].data.str.area;
3947 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003948 return 1;
3949}
3950
3951static int smp_check_const_meth(struct arg *args, char **err)
3952{
3953 enum http_meth_t meth;
3954 int i;
3955
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003956 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003957 if (meth != HTTP_METH_OTHER) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003958 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003959 args[0].type = ARGT_SINT;
3960 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003961 } else {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003962 /* Check method avalaibility. A method is a token defined as :
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003963 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
3964 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
3965 * token = 1*tchar
3966 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003967 for (i = 0; i < args[0].data.str.data; i++) {
3968 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003969 memprintf(err, "expects valid method.");
3970 return 0;
3971 }
3972 }
3973 }
3974 return 1;
3975}
3976
3977static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
3978{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003979 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003980 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003981 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003982 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003983 smp->data.u.meth.str.area = "";
3984 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003985 } else {
3986 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003987 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003988 smp->data.u.meth.str.area = args[0].data.str.area;
3989 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003990 }
3991 return 1;
3992}
3993
Luca Schimweg8a694b82019-09-10 15:42:52 +02003994// This function checks the "uuid" sample's arguments.
3995// Function won't get called when no parameter is specified (maybe a bug?)
3996static int smp_check_uuid(struct arg *args, char **err)
3997{
3998 if (!args[0].type) {
3999 args[0].type = ARGT_SINT;
4000 args[0].data.sint = 4;
4001 }
4002 else if (args[0].data.sint != 4) {
4003 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
4004 return 0;
4005 }
4006
4007 return 1;
4008}
4009
4010// Generate a RFC4122 UUID (default is v4 = fully random)
4011static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
4012{
4013 if (args[0].data.sint == 4 || !args[0].type) {
Willy Tarreauee3bcdd2020-03-08 17:48:17 +01004014 ha_generate_uuid(&trash);
Luca Schimweg8a694b82019-09-10 15:42:52 +02004015 smp->data.type = SMP_T_STR;
4016 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4017 smp->data.u.str = trash;
4018 return 1;
4019 }
4020
4021 // more implementations of other uuid formats possible here
4022 return 0;
4023}
4024
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004025/* Note: must not be declared <const> as its list will be overwritten.
4026 * Note: fetches that may return multiple types must be declared as the lowest
4027 * common denominator, the type that can be casted into all other ones. For
4028 * instance IPv4/IPv6 must be declared IPv4.
4029 */
4030static struct sample_fetch_kw_list smp_kws = {ILH, {
4031 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
4032 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01004033 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Damien Claisseae6f1252019-10-30 15:57:28 +00004034 { "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 +01004035 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01004036 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02004037 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4038 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Christopher Faulet34adb2a2017-11-21 21:45:38 +01004039 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004040 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01004041 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Luca Schimweg8a694b82019-09-10 15:42:52 +02004042 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01004043
4044 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4045 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4046 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4047 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4048 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004049
4050 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
4051 { "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 +02004052 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004053 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
4054 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
4055 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
4056 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
4057
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004058 { /* END */ },
4059}};
4060
Willy Tarreau0108d902018-11-25 19:14:37 +01004061INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
4062
Emeric Brun107ca302010-01-04 16:16:05 +01004063/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02004064static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau0851fd52019-12-17 10:07:25 +01004065 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
Holger Just1bfc24b2017-05-06 00:56:53 +02004066 { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN },
Emeric Brun53d1a982014-04-30 18:21:37 +02004067 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02004068 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
4069 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau60a2ee72017-12-15 07:13:48 +01004070 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01004071 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Dragan Dosen3f957b22017-10-24 09:27:34 +02004072 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
Tim Duesterhus1478aa72018-01-25 16:24:51 +01004073 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02004074 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4075 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004076 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01004077 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004078 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4079 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4080 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01004081 { "xxh3", sample_conv_xxh3, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER01e09742016-12-26 11:46:11 +01004082 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4083 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02004084 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004085 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
Marcin Deranek9631a282018-04-16 14:30:46 +02004086 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4087 { "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 +01004088 { "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 +02004089 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02004090#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01004091 { "sha2", sample_conv_sha2, ARG1(0, SINT), smp_check_sha2, SMP_T_BIN, SMP_T_BIN },
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02004092#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
4093 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
4094#endif
Patrick Gansterer8e366512020-04-22 16:47:57 +02004095 { "digest", sample_conv_crypto_digest, ARG1(1,STR), check_crypto_digest, SMP_T_BIN, SMP_T_BIN },
4096 { "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 +02004097#endif
Willy Tarreau280f42b2018-02-19 15:34:12 +01004098 { "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 +02004099 { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT },
Tim Duesterhusf38175c2020-06-09 11:48:42 +02004100#ifdef USE_OPENSSL
4101 { "secure_memcmp", sample_conv_secure_memcmp, ARG1(1,STR), smp_check_secure_memcmp, SMP_T_BIN, SMP_T_BOOL },
4102#endif
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01004103
4104 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01004105 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
4106 { "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 +01004107
Baptiste Assmanne138dda2020-10-22 15:39:03 +02004108 /* FIX converters */
4109 { "fix_is_valid", sample_conv_fix_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4110 { "fix_tag_value", sample_conv_fix_tag_value, ARG1(1,STR), sample_conv_fix_value_check, SMP_T_BIN, SMP_T_BIN },
4111
Baptiste Assmanne279ca62020-10-27 18:10:06 +01004112 /* MQTT converters */
4113 { "mqtt_is_valid", sample_conv_mqtt_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4114 { "mqtt_field_value", sample_conv_mqtt_field_value, ARG2(2,STR,STR), sample_conv_mqtt_field_value_check, SMP_T_BIN, SMP_T_STR },
4115
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02004116 { "iif", sample_conv_iif, ARG2(2, STR, STR), NULL, SMP_T_BOOL, SMP_T_STR },
4117
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02004118 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4119 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4120 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4121 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
4122 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4123 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4124 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4125 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4126 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4127 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4128 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4129 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4130 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4131 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01004132
Christopher Fauletea159d62020-04-01 16:21:44 +02004133 { "htonl", sample_conv_htonl, 0, NULL, SMP_T_SINT, SMP_T_BIN },
4134 { "cut_crlf", sample_conv_cut_crlf, 0, NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet51fc9d12020-04-01 17:24:41 +02004135 { "ltrim", sample_conv_ltrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet568415a2020-04-01 17:24:47 +02004136 { "rtrim", sample_conv_rtrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02004137 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01004138}};
4139
Willy Tarreau0108d902018-11-25 19:14:37 +01004140INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);