blob: ea60b935bdda1e68d3add2d2fda9c37d93581dd5 [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
1093 * errors or 0 if everything is fine.
1094 */
1095int smp_resolve_args(struct proxy *p)
1096{
1097 struct arg_list *cur, *bak;
1098 const char *ctx, *where;
1099 const char *conv_ctx, *conv_pre, *conv_pos;
1100 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001101 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001102 struct arg *arg;
1103 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001104 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001105
1106 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1107 struct proxy *px;
1108 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001109 struct stktable *t;
1110 char *pname, *sname, *stktname;
Willy Tarreau46947782015-01-19 19:00:58 +01001111 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001112
1113 arg = cur->arg;
1114
1115 /* prepare output messages */
1116 conv_pre = conv_pos = conv_ctx = "";
1117 if (cur->conv) {
1118 conv_ctx = cur->conv;
1119 conv_pre = "conversion keyword '";
1120 conv_pos = "' for ";
1121 }
1122
1123 where = "in";
1124 ctx = "sample fetch keyword";
1125 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001126 case ARGC_STK: where = "in stick rule in"; break;
1127 case ARGC_TRK: where = "in tracking rule in"; break;
1128 case ARGC_LOG: where = "in log-format string in"; break;
1129 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1130 case ARGC_HRQ: where = "in http-request header format string in"; break;
1131 case ARGC_HRS: where = "in http-response header format string in"; break;
1132 case ARGC_UIF: where = "in unique-id-format string in"; break;
1133 case ARGC_RDR: where = "in redirect format string in"; break;
1134 case ARGC_CAP: where = "in capture rule in"; break;
1135 case ARGC_ACL: ctx = "ACL keyword"; break;
1136 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001137 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001138 case ARGC_HERR: where = "in http-error directive in"; break;
Miroslav Zagorac7f8314c2020-12-09 16:31:48 +01001139 case ARGC_OT: where = "in ot-scope directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001140 }
1141
1142 /* set a few default settings */
1143 px = p;
1144 pname = p->id;
1145
1146 switch (arg->type) {
1147 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001148 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001149 ha_alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1150 cur->file, cur->line,
1151 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001152 cfgerr++;
1153 continue;
1154 }
1155
1156 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001157 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001158
1159 if (sname) {
1160 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001161 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001162
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001163 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001164 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001165 ha_alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1166 cur->file, cur->line, pname,
1167 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001168 cfgerr++;
1169 break;
1170 }
1171 }
1172 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001173 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001174
1175 srv = findserver(px, sname);
1176 if (!srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001177 ha_alert("parsing [%s:%d] : unable to find server '%s' in proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1178 cur->file, cur->line, sname, pname,
1179 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001180 cfgerr++;
1181 break;
1182 }
1183
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001184 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001185 arg->unresolved = 0;
1186 arg->data.srv = srv;
1187 break;
1188
1189 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001190 if (arg->data.str.data) {
1191 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001192 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001193 }
1194
1195 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001196 ha_alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1197 cur->file, cur->line, pname,
1198 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001199 cfgerr++;
1200 break;
1201 }
1202
1203 if (!(px->cap & PR_CAP_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001204 ha_alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1205 cur->file, cur->line, pname,
1206 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001207 cfgerr++;
1208 break;
1209 }
1210
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001211 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001212 arg->unresolved = 0;
1213 arg->data.prx = px;
1214 break;
1215
1216 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001217 if (arg->data.str.data) {
1218 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001219 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001220 }
1221
1222 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001223 ha_alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1224 cur->file, cur->line, pname,
1225 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001226 cfgerr++;
1227 break;
1228 }
1229
1230 if (!(px->cap & PR_CAP_BE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001231 ha_alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1232 cur->file, cur->line, pname,
1233 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001234 cfgerr++;
1235 break;
1236 }
1237
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001238 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001239 arg->unresolved = 0;
1240 arg->data.prx = px;
1241 break;
1242
1243 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001244 if (arg->data.str.data)
1245 stktname = arg->data.str.area;
1246 else
1247 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001248
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001249 t = stktable_find_by_name(stktname);
1250 if (!t) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001251 ha_alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001252 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001253 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001254 cfgerr++;
1255 break;
1256 }
1257
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001258 if (!t->size) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001259 ha_alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001260 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001261 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001262 cfgerr++;
1263 break;
1264 }
1265
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001266 if (t->proxy && (p->bind_proc & ~t->proxy->bind_proc)) {
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001267 ha_alert("parsing [%s:%d] : stick-table '%s' not present on all processes covered by proxy '%s'.\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001268 cur->file, cur->line, t->proxy->id, p->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01001269 cfgerr++;
1270 break;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001271 }
1272
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001273 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001274 p->next_stkt_ref = t->proxies_list;
1275 t->proxies_list = p;
1276 }
1277
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001278 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001279 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001280 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001281 break;
1282
1283 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001284 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001285 ha_alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1286 cur->file, cur->line,
1287 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001288 cfgerr++;
1289 break;
1290 }
1291
1292 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001293 strcmp(p->uri_auth->userlist->name, arg->data.str.area) == 0)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001294 ul = p->uri_auth->userlist;
1295 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001296 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001297
1298 if (!ul) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001299 ha_alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001300 cur->file, cur->line,
1301 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001302 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001303 cfgerr++;
1304 break;
1305 }
1306
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001307 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001308 arg->unresolved = 0;
1309 arg->data.usr = ul;
1310 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001311
1312 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001313 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001314 ha_alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1315 cur->file, cur->line,
1316 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001317 cfgerr++;
1318 continue;
1319 }
1320
Willy Tarreau46947782015-01-19 19:00:58 +01001321 rflags = 0;
1322 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1323 err = NULL;
1324
Dragan Dosen26743032019-04-30 15:54:36 +02001325 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001326 ha_alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1327 cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001328 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001329 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
Willy Tarreau46947782015-01-19 19:00:58 +01001330 cfgerr++;
1331 continue;
1332 }
1333
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001334 chunk_destroy(&arg->data.str);
Willy Tarreau46947782015-01-19 19:00:58 +01001335 arg->unresolved = 0;
1336 arg->data.reg = reg;
1337 break;
1338
1339
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001340 }
1341
1342 LIST_DEL(&cur->list);
1343 free(cur);
1344 } /* end of args processing */
1345
1346 return cfgerr;
1347}
1348
1349/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001350 * Process a fetch + format conversion as defined by the sample expression
1351 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001352 * not explicitly set to <smp_type>, but shall be compatible with it as
1353 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1354 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001355 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1356 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1357 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1358 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001359 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1360 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1361 * still hope to get it after waiting longer, and is not converted to string.
1362 * The possible output combinations are the following :
1363 *
1364 * return MAY_CHANGE FINAL Meaning for the sample
1365 * NULL * * Not present and will never be (eg: header)
1366 * smp 0 * Final value converted (eg: header)
1367 * smp 1 0 Not present yet, may appear later (eg: header)
1368 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001369 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001370struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001371 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001372 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001373{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001374 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001375
Willy Tarreau6c616e02014-06-25 16:56:41 +02001376 memset(smp, 0, sizeof(*smp));
1377
Willy Tarreau192252e2015-04-04 01:47:55 +02001378 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001379 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1380 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001381 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001382 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001383
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001384 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001385 return NULL;
1386
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001387 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001388 return NULL;
1389
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001390 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001391 return smp;
1392}
1393
Christopher Faulet476e5d02016-10-26 11:34:47 +02001394static void release_sample_arg(struct arg *p)
1395{
1396 struct arg *p_back = p;
1397
1398 if (!p)
1399 return;
1400
1401 while (p->type != ARGT_STOP) {
1402 if (p->type == ARGT_STR || p->unresolved) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001403 chunk_destroy(&p->data.str);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001404 p->unresolved = 0;
1405 }
1406 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001407 regex_free(p->data.reg);
1408 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001409 }
1410 p++;
1411 }
1412
1413 if (p_back != empty_arg_list)
1414 free(p_back);
1415}
1416
1417void release_sample_expr(struct sample_expr *expr)
1418{
1419 struct sample_conv_expr *conv_expr, *conv_exprb;
1420
1421 if (!expr)
1422 return;
1423
Tim Duesterhus867cd982020-07-04 11:49:39 +02001424 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list) {
1425 LIST_DEL(&conv_expr->list);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001426 release_sample_arg(conv_expr->arg_p);
Tim Duesterhus867cd982020-07-04 11:49:39 +02001427 free(conv_expr);
1428 }
1429
Christopher Faulet476e5d02016-10-26 11:34:47 +02001430 release_sample_arg(expr->arg_p);
1431 free(expr);
1432}
1433
Emeric Brun107ca302010-01-04 16:16:05 +01001434/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001435/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001436/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001437/*****************************************************************/
1438
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001439static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1440{
1441 int i;
1442 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001443 struct buffer *buf;
1444 struct sink *sink;
1445 struct ist line;
1446 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001447
Willy Tarreau0851fd52019-12-17 10:07:25 +01001448 buf = alloc_trash_chunk();
1449 if (!buf)
1450 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001451
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001452 sink = (struct sink *)arg_p[1].data.ptr;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001453 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001454
Willy Tarreau0851fd52019-12-17 10:07:25 +01001455 pfx = arg_p[0].data.str.area;
1456 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001457
Willy Tarreau0851fd52019-12-17 10:07:25 +01001458 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1459 if (!sample_casts[smp->data.type][SMP_T_STR])
1460 goto nocast;
1461
1462 /* Copy sample fetch. This puts the sample as const, the
1463 * cast will copy data if a transformation is required.
1464 */
1465 memcpy(&tmp, smp, sizeof(struct sample));
1466 tmp.flags = SMP_F_CONST;
1467
1468 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1469 goto nocast;
1470
1471 /* Display the displayable chars*. */
1472 b_putchr(buf, '<');
1473 for (i = 0; i < tmp.data.u.str.data; i++) {
Willy Tarreau90807112020-02-25 08:16:33 +01001474 if (isprint((unsigned char)tmp.data.u.str.area[i]))
Willy Tarreau0851fd52019-12-17 10:07:25 +01001475 b_putchr(buf, tmp.data.u.str.area[i]);
1476 else
1477 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001478 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001479 b_putchr(buf, '>');
1480
1481 done:
1482 line = ist2(buf->area, buf->data);
Emeric Brun54648852020-07-06 15:54:06 +02001483 sink_write(sink, &line, 1, 0, 0, NULL);
Willy Tarreau0851fd52019-12-17 10:07:25 +01001484 end:
1485 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001486 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001487 nocast:
1488 chunk_appendf(buf, "(undisplayable)");
1489 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001490}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001491
1492// This function checks the "debug" converter's arguments.
1493static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1494 const char *file, int line, char **err)
1495{
1496 const char *name = "buf0";
1497 struct sink *sink = NULL;
1498
1499 if (args[0].type != ARGT_STR) {
1500 /* optional prefix */
1501 args[0].data.str.area = "";
1502 args[0].data.str.data = 0;
1503 }
1504
1505 if (args[1].type == ARGT_STR)
1506 name = args[1].data.str.area;
1507
1508 sink = sink_find(name);
1509 if (!sink) {
1510 memprintf(err, "No such sink '%s'", name);
1511 return 0;
1512 }
1513
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001514 chunk_destroy(&args[1].data.str);
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001515 args[1].type = ARGT_PTR;
1516 args[1].data.ptr = sink;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001517 return 1;
1518}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001519
Holger Just1bfc24b2017-05-06 00:56:53 +02001520static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1521{
Willy Tarreau83061a82018-07-13 11:56:34 +02001522 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001523 int bin_len;
1524
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001525 trash->data = 0;
1526 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1527 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001528 if (bin_len < 0)
1529 return 0;
1530
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001531 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001532 smp->data.u.str = *trash;
1533 smp->data.type = SMP_T_BIN;
1534 smp->flags &= ~SMP_F_CONST;
1535 return 1;
1536}
1537
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001538static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001539{
Willy Tarreau83061a82018-07-13 11:56:34 +02001540 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001541 int b64_len;
1542
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001543 trash->data = 0;
1544 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1545 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001546 if (b64_len < 0)
1547 return 0;
1548
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001549 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001550 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001551 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001552 smp->flags &= ~SMP_F_CONST;
1553 return 1;
1554}
1555
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001556static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1557{
1558 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001559 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001560
1561 memset(&ctx, 0, sizeof(ctx));
1562
1563 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001564 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1565 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001566
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001567 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001568 smp->data.u.str = *trash;
1569 smp->data.type = SMP_T_BIN;
1570 smp->flags &= ~SMP_F_CONST;
1571 return 1;
1572}
1573
Tim Duesterhusd4376302019-06-17 12:41:44 +02001574#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01001575static int smp_check_sha2(struct arg *args, struct sample_conv *conv,
1576 const char *file, int line, char **err)
1577{
1578 if (args[0].type == ARGT_STOP)
1579 return 1;
1580 if (args[0].type != ARGT_SINT) {
1581 memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]);
1582 return 0;
1583 }
1584
1585 switch (args[0].data.sint) {
1586 case 224:
1587 case 256:
1588 case 384:
1589 case 512:
1590 /* this is okay */
1591 return 1;
1592 default:
1593 memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint);
1594 return 0;
1595 }
1596}
1597
Tim Duesterhusd4376302019-06-17 12:41:44 +02001598static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
1599{
1600 struct buffer *trash = get_trash_chunk();
1601 int bits = 256;
Christopher Faulete6e7a582021-01-29 11:29:28 +01001602 if (arg_p->data.sint)
Tim Duesterhusd4376302019-06-17 12:41:44 +02001603 bits = arg_p->data.sint;
1604
1605 switch (bits) {
1606 case 224: {
1607 SHA256_CTX ctx;
1608
1609 memset(&ctx, 0, sizeof(ctx));
1610
1611 SHA224_Init(&ctx);
1612 SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1613 SHA224_Final((unsigned char *) trash->area, &ctx);
1614 trash->data = SHA224_DIGEST_LENGTH;
1615 break;
1616 }
1617 case 256: {
1618 SHA256_CTX ctx;
1619
1620 memset(&ctx, 0, sizeof(ctx));
1621
1622 SHA256_Init(&ctx);
1623 SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1624 SHA256_Final((unsigned char *) trash->area, &ctx);
1625 trash->data = SHA256_DIGEST_LENGTH;
1626 break;
1627 }
1628 case 384: {
1629 SHA512_CTX ctx;
1630
1631 memset(&ctx, 0, sizeof(ctx));
1632
1633 SHA384_Init(&ctx);
1634 SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1635 SHA384_Final((unsigned char *) trash->area, &ctx);
1636 trash->data = SHA384_DIGEST_LENGTH;
1637 break;
1638 }
1639 case 512: {
1640 SHA512_CTX ctx;
1641
1642 memset(&ctx, 0, sizeof(ctx));
1643
1644 SHA512_Init(&ctx);
1645 SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1646 SHA512_Final((unsigned char *) trash->area, &ctx);
1647 trash->data = SHA512_DIGEST_LENGTH;
1648 break;
1649 }
1650 default:
1651 return 0;
1652 }
1653
1654 smp->data.u.str = *trash;
1655 smp->data.type = SMP_T_BIN;
1656 smp->flags &= ~SMP_F_CONST;
1657 return 1;
1658}
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001659
Dragan Dosen9e8db132021-02-22 10:03:53 +01001660/* This function returns a sample struct filled with an <arg> content.
1661 * If the <arg> contains a string, it is returned in the sample flagged as
1662 * SMP_F_CONST. If the <arg> contains a variable descriptor, the sample is
1663 * filled with the content of the variable by using vars_get_by_desc().
1664 *
1665 * Keep in mind that the sample content may be written to a pre-allocated
1666 * trash chunk as returned by get_trash_chunk().
1667 *
1668 * This function returns 0 if an error occurs, otherwise it returns 1.
1669 */
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001670static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
1671{
1672 switch (arg->type) {
1673 case ARGT_STR:
1674 smp->data.type = SMP_T_STR;
1675 smp->data.u.str = arg->data.str;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001676 smp->flags = SMP_F_CONST;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001677 return 1;
1678 case ARGT_VAR:
1679 if (!vars_get_by_desc(&arg->data.var, smp))
1680 return 0;
1681 if (!sample_casts[smp->data.type][SMP_T_STR])
1682 return 0;
1683 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
1684 return 0;
1685 return 1;
1686 default:
1687 return 0;
1688 }
1689}
1690
Dragan Dosen9e8db132021-02-22 10:03:53 +01001691/* This function checks an <arg> and fills it with a variable type if the
1692 * <arg> string contains a valid variable name. If failed, the function
1693 * tries to perform a base64 decode operation on the same string, and
1694 * fills the <arg> with the decoded content.
1695 *
1696 * Validation is skipped if the <arg> string is empty.
1697 *
1698 * This function returns 0 if the variable lookup fails and the specified
1699 * <arg> string is not a valid base64 encoded string, as well if
1700 * unexpected argument type is specified or memory allocation error
1701 * occurs. Otherwise it returns 1.
1702 */
1703static inline int sample_check_arg_base64(struct arg *arg, char **err)
1704{
1705 char *dec = NULL;
1706 int dec_size;
1707
1708 if (arg->type != ARGT_STR) {
1709 memprintf(err, "unexpected argument type");
1710 return 0;
1711 }
1712
1713 if (arg->data.str.data == 0) /* empty */
1714 return 1;
1715
1716 if (vars_check_arg(arg, NULL))
1717 return 1;
1718
1719 if (arg->data.str.data % 4) {
1720 memprintf(err, "argument needs to be base64 encoded, and "
1721 "can either be a string or a variable");
1722 return 0;
1723 }
1724
1725 dec_size = (arg->data.str.data / 4 * 3)
1726 - (arg->data.str.area[arg->data.str.data-1] == '=' ? 1 : 0)
1727 - (arg->data.str.area[arg->data.str.data-2] == '=' ? 1 : 0);
1728
1729 if ((dec = malloc(dec_size)) == NULL) {
1730 memprintf(err, "memory allocation error");
1731 return 0;
1732 }
1733
1734 dec_size = base64dec(arg->data.str.area, arg->data.str.data, dec, dec_size);
1735 if (dec_size < 0) {
1736 memprintf(err, "argument needs to be base64 encoded, and "
1737 "can either be a string or a variable");
1738 free(dec);
1739 return 0;
1740 }
1741
1742 /* base64 decoded */
1743 chunk_destroy(&arg->data.str);
1744 arg->data.str.area = dec;
1745 arg->data.str.data = dec_size;
1746 return 1;
1747}
1748
Patrick Gansterer8e366512020-04-22 16:47:57 +02001749#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001750static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
1751 const char *file, int line, char **err)
1752{
1753 switch(args[0].data.sint) {
1754 case 128:
1755 case 192:
1756 case 256:
1757 break;
1758 default:
1759 memprintf(err, "key size must be 128, 192 or 256 (bits).");
1760 return 0;
1761 }
Dragan Dosen9e8db132021-02-22 10:03:53 +01001762
1763 /* Try to decode variables. */
1764 if (!sample_check_arg_base64(&args[1], err)) {
1765 memprintf(err, "failed to parse nonce : %s", *err);
1766 return 0;
1767 }
1768 if (!sample_check_arg_base64(&args[2], err)) {
1769 memprintf(err, "failed to parse key : %s", *err);
1770 return 0;
1771 }
1772 if (!sample_check_arg_base64(&args[3], err)) {
1773 memprintf(err, "failed to parse aead_tag : %s", *err);
1774 return 0;
1775 }
1776
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001777 return 1;
1778}
1779
1780/* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
1781static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
1782{
1783 struct sample nonce, key, aead_tag;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001784 struct buffer *smp_trash = NULL, *smp_trash_alloc = NULL;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001785 EVP_CIPHER_CTX *ctx;
1786 int dec_size, ret;
1787
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001788 smp_trash_alloc = alloc_trash_chunk();
1789 if (!smp_trash_alloc)
1790 return 0;
1791
Dragan Dosen9e8db132021-02-22 10:03:53 +01001792 /* smp copy */
1793 smp_trash_alloc->data = smp->data.u.str.data;
1794 if (unlikely(smp_trash_alloc->data > smp_trash_alloc->size))
1795 smp_trash_alloc->data = smp_trash_alloc->size;
1796 memcpy(smp_trash_alloc->area, smp->data.u.str.area, smp_trash_alloc->data);
1797
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001798 ctx = EVP_CIPHER_CTX_new();
1799
1800 if (!ctx)
1801 goto err;
1802
Dragan Dosen9e8db132021-02-22 10:03:53 +01001803 smp_trash = alloc_trash_chunk();
1804 if (!smp_trash)
1805 goto err;
1806
1807 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
1808 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001809 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001810
1811 if (arg_p[1].type == ARGT_VAR) {
1812 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
1813 if (dec_size < 0)
1814 goto err;
1815 smp_trash->data = dec_size;
1816 nonce.data.u.str = *smp_trash;
1817 }
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001818
1819 /* Set cipher type and mode */
1820 switch(arg_p[0].data.sint) {
1821 case 128:
1822 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
1823 break;
1824 case 192:
1825 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
1826 break;
1827 case 256:
1828 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
1829 break;
1830 }
1831
Dragan Dosen9e8db132021-02-22 10:03:53 +01001832 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, nonce.data.u.str.data, NULL);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001833
1834 /* Initialise IV */
Dragan Dosen9e8db132021-02-22 10:03:53 +01001835 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) nonce.data.u.str.area))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001836 goto err;
1837
Dragan Dosen9e8db132021-02-22 10:03:53 +01001838 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
1839 if (!sample_conv_var2smp_str(&arg_p[2], &key))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001840 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001841
1842 if (arg_p[2].type == ARGT_VAR) {
1843 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
1844 if (dec_size < 0)
1845 goto err;
1846 smp_trash->data = dec_size;
1847 key.data.u.str = *smp_trash;
1848 }
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001849
1850 /* Initialise key */
Dragan Dosen9e8db132021-02-22 10:03:53 +01001851 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) key.data.u.str.area, NULL))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001852 goto err;
1853
1854 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
Dragan Dosen9e8db132021-02-22 10:03:53 +01001855 (unsigned char *) smp_trash_alloc->area, (int) smp_trash_alloc->data))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001856 goto err;
1857
Dragan Dosen9e8db132021-02-22 10:03:53 +01001858 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
1859 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001860 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001861
1862 if (arg_p[3].type == ARGT_VAR) {
1863 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
1864 if (dec_size < 0)
1865 goto err;
1866 smp_trash_alloc->data = dec_size;
1867 aead_tag.data.u.str = *smp_trash_alloc;
1868 }
1869
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001870 dec_size = smp_trash->data;
1871
Dragan Dosen9e8db132021-02-22 10:03:53 +01001872 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 +02001873 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
1874
1875 if (ret <= 0)
1876 goto err;
1877
1878 smp->data.u.str.data = dec_size + smp_trash->data;
1879 smp->data.u.str.area = smp_trash->area;
1880 smp->data.type = SMP_T_BIN;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001881 smp_dup(smp);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001882 free_trash_chunk(smp_trash_alloc);
Dragan Dosen9e8db132021-02-22 10:03:53 +01001883 free_trash_chunk(smp_trash);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001884 return 1;
1885
1886err:
1887 free_trash_chunk(smp_trash_alloc);
Dragan Dosen9e8db132021-02-22 10:03:53 +01001888 free_trash_chunk(smp_trash);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001889 return 0;
1890}
1891#endif /* HA_OPENSSL_VERSION_NUMBER */
1892
Patrick Gansterer8e366512020-04-22 16:47:57 +02001893static int check_crypto_digest(struct arg *args, struct sample_conv *conv,
1894 const char *file, int line, char **err)
1895{
1896 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1897
1898 if (evp)
1899 return 1;
1900
1901 memprintf(err, "algorithm must be a valid OpenSSL message digest name.");
1902 return 0;
1903}
1904
1905static int sample_conv_crypto_digest(const struct arg *args, struct sample *smp, void *private)
1906{
1907 struct buffer *trash = get_trash_chunk();
1908 unsigned char *md = (unsigned char*) trash->area;
1909 unsigned int md_len = trash->size;
1910 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
1911 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1912
1913 if (!ctx)
1914 return 0;
1915
1916 if (!EVP_DigestInit_ex(ctx, evp, NULL) ||
1917 !EVP_DigestUpdate(ctx, smp->data.u.str.area, smp->data.u.str.data) ||
1918 !EVP_DigestFinal_ex(ctx, md, &md_len)) {
1919 EVP_MD_CTX_free(ctx);
1920 return 0;
1921 }
1922
1923 EVP_MD_CTX_free(ctx);
1924
1925 trash->data = md_len;
1926 smp->data.u.str = *trash;
1927 smp->data.type = SMP_T_BIN;
1928 smp->flags &= ~SMP_F_CONST;
1929 return 1;
1930}
1931
1932static int check_crypto_hmac(struct arg *args, struct sample_conv *conv,
1933 const char *file, int line, char **err)
1934{
1935 if (!check_crypto_digest(args, conv, file, line, err))
1936 return 0;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001937
1938 if (!sample_check_arg_base64(&args[1], err)) {
1939 memprintf(err, "failed to parse key : %s", *err);
1940 return 0;
1941 }
Patrick Gansterer8e366512020-04-22 16:47:57 +02001942
Patrick Gansterer8e366512020-04-22 16:47:57 +02001943 return 1;
1944}
1945
1946static int sample_conv_crypto_hmac(const struct arg *args, struct sample *smp, void *private)
1947{
1948 struct sample key;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001949 struct buffer *trash = NULL, *key_trash = NULL;
Patrick Gansterer8e366512020-04-22 16:47:57 +02001950 unsigned char *md;
1951 unsigned int md_len;
1952 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1953 int dec_size;
1954
1955 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
1956 if (!sample_conv_var2smp_str(&args[1], &key))
1957 return 0;
1958
Dragan Dosen9e8db132021-02-22 10:03:53 +01001959 if (args[1].type == ARGT_VAR) {
1960 key_trash = alloc_trash_chunk();
1961 if (!key_trash)
1962 goto err;
1963
1964 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, key_trash->area, key_trash->size);
1965 if (dec_size < 0)
1966 goto err;
1967 key_trash->data = dec_size;
1968 key.data.u.str = *key_trash;
1969 }
Patrick Gansterer8e366512020-04-22 16:47:57 +02001970
Dragan Dosen9e8db132021-02-22 10:03:53 +01001971 trash = alloc_trash_chunk();
1972 if (!trash)
Patrick Gansterer8e366512020-04-22 16:47:57 +02001973 goto err;
1974
1975 md = (unsigned char*) trash->area;
1976 md_len = trash->size;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001977 if (!HMAC(evp, key.data.u.str.area, key.data.u.str.data, (const unsigned char*) smp->data.u.str.area,
1978 smp->data.u.str.data, md, &md_len))
Patrick Gansterer8e366512020-04-22 16:47:57 +02001979 goto err;
1980
1981 free_trash_chunk(key_trash);
1982
1983 trash->data = md_len;
1984 smp->data.u.str = *trash;
1985 smp->data.type = SMP_T_BIN;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001986 smp_dup(smp);
1987 free_trash_chunk(trash);
Patrick Gansterer8e366512020-04-22 16:47:57 +02001988 return 1;
1989
1990err:
1991 free_trash_chunk(key_trash);
Dragan Dosen9e8db132021-02-22 10:03:53 +01001992 free_trash_chunk(trash);
Patrick Gansterer8e366512020-04-22 16:47:57 +02001993 return 0;
1994}
1995
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001996#endif /* USE_OPENSSL */
Tim Duesterhusd4376302019-06-17 12:41:44 +02001997
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001998static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001999{
Willy Tarreau83061a82018-07-13 11:56:34 +02002000 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002001 unsigned char c;
2002 int ptr = 0;
2003
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002004 trash->data = 0;
2005 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
2006 c = smp->data.u.str.area[ptr++];
2007 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
2008 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002009 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002010 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002011 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002012 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002013 return 1;
2014}
2015
Dragan Dosen3f957b22017-10-24 09:27:34 +02002016static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
2017{
2018 long long int n = 0;
2019 int i, c;
2020
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002021 for (i = 0; i < smp->data.u.str.data; i++) {
2022 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02002023 return 0;
2024 n = (n << 4) + c;
2025 }
2026
2027 smp->data.u.sint = n;
2028 smp->data.type = SMP_T_SINT;
2029 smp->flags &= ~SMP_F_CONST;
2030 return 1;
2031}
2032
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002033/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002034static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002035{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002036 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
2037 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002038 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002039 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002040 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002041 return 1;
2042}
2043
Willy Tarreau60a2ee72017-12-15 07:13:48 +01002044static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002045{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002046 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002047 smp->data.u.sint = i;
2048 smp->data.type = SMP_T_SINT;
2049 return 1;
2050}
2051
2052
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002053static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002054{
2055 int i;
2056
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002057 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002058 return 0;
2059
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002060 for (i = 0; i < smp->data.u.str.data; i++) {
2061 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
2062 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01002063 }
2064 return 1;
2065}
2066
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002067static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002068{
2069 int i;
2070
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002071 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02002072 return 0;
2073
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002074 for (i = 0; i < smp->data.u.str.data; i++) {
2075 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
2076 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01002077 }
2078 return 1;
2079}
2080
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002081/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
2082static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002083{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002084 /* Attempt to convert to IPv4 to apply the correct mask. */
2085 c_ipv62ip(smp);
2086
2087 if (smp->data.type == SMP_T_IPV4) {
2088 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
2089 smp->data.type = SMP_T_IPV4;
2090 }
2091 else if (smp->data.type == SMP_T_IPV6) {
2092 /* IPv6 cannot be converted without an IPv6 mask. */
2093 if (args[1].type != ARGT_IPV6)
2094 return 0;
2095
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +01002096 write_u64(&smp->data.u.ipv6.s6_addr[0],
2097 read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0]));
2098 write_u64(&smp->data.u.ipv6.s6_addr[8],
2099 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 +01002100 smp->data.type = SMP_T_IPV6;
2101 }
2102
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002103 return 1;
2104}
2105
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002106/* takes an UINT value on input supposed to represent the time since EPOCH,
2107 * adds an optional offset found in args[1] and emits a string representing
2108 * the local time in the format specified in args[1] using strftime().
2109 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002110static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002111{
Willy Tarreau83061a82018-07-13 11:56:34 +02002112 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002113 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002114 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002115 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002116
2117 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002118 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002119 curr_date += args[1].data.sint;
2120
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002121 tm = localtime(&curr_date);
2122 if (!tm)
2123 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002124 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002125 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
2126 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002127 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002128 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002129 return 1;
2130}
2131
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002132/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002133static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002134{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002135 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
2136 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002137 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002138 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002139 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002140 return 1;
2141}
2142
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002143/* takes an UINT value on input supposed to represent the time since EPOCH,
2144 * adds an optional offset found in args[1] and emits a string representing
2145 * the UTC date in the format specified in args[1] using strftime().
2146 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002147static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002148{
Willy Tarreau83061a82018-07-13 11:56:34 +02002149 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002150 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002151 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002152 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002153
2154 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002155 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002156 curr_date += args[1].data.sint;
2157
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002158 tm = gmtime(&curr_date);
2159 if (!tm)
2160 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002161 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002162 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
2163 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002164 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002165 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002166 return 1;
2167}
2168
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002169/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002170static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002171{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002172 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
2173 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002174 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002175 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002176 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002177 return 1;
2178}
2179
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002180/* hashes the binary input into a 32-bit unsigned int using xxh.
2181 * The seed of the hash defaults to 0 but can be changd in argument 1.
2182 */
2183static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
2184{
2185 unsigned int seed;
2186
Christopher Faulete6e7a582021-01-29 11:29:28 +01002187 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002188 seed = arg_p->data.sint;
2189 else
2190 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002191 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
2192 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002193 smp->data.type = SMP_T_SINT;
2194 return 1;
2195}
2196
2197/* hashes the binary input into a 64-bit unsigned int using xxh.
2198 * In fact, the function returns a 64 bit unsigned, but the sample
2199 * storage of haproxy only proposes 64-bits signed, so the value is
2200 * cast as signed. This cast doesn't impact the hash repartition.
2201 * The seed of the hash defaults to 0 but can be changd in argument 1.
2202 */
2203static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
2204{
2205 unsigned long long int seed;
2206
Christopher Faulete6e7a582021-01-29 11:29:28 +01002207 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002208 seed = (unsigned long long int)arg_p->data.sint;
2209 else
2210 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002211 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
2212 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002213 smp->data.type = SMP_T_SINT;
2214 return 1;
2215}
2216
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002217static int sample_conv_xxh3(const struct arg *arg_p, struct sample *smp, void *private)
2218{
2219 unsigned long long int seed;
2220
Christopher Faulete6e7a582021-01-29 11:29:28 +01002221 if (arg_p->data.sint)
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002222 seed = (unsigned long long int)arg_p->data.sint;
2223 else
2224 seed = 0;
2225 smp->data.u.sint = (long long int)XXH3(smp->data.u.str.area,
2226 smp->data.u.str.data, seed);
2227 smp->data.type = SMP_T_SINT;
2228 return 1;
2229}
2230
Willy Tarreau80599772015-01-20 19:35:24 +01002231/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002232static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01002233{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002234 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
2235 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002236 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002237 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002238 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01002239 return 1;
2240}
2241
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002242/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
2243static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
2244{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002245 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
2246 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002247 if (arg_p->data.sint)
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002248 smp->data.u.sint = full_hash(smp->data.u.sint);
2249 smp->data.type = SMP_T_SINT;
2250 return 1;
2251}
2252
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002253/* This function escape special json characters. The returned string can be
2254 * safely set between two '"' and used as json string. The json string is
2255 * defined like this:
2256 *
2257 * any Unicode character except '"' or '\' or control character
2258 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
2259 *
2260 * The enum input_type contain all the allowed mode for decoding the input
2261 * string.
2262 */
2263enum input_type {
2264 IT_ASCII = 0,
2265 IT_UTF8,
2266 IT_UTF8S,
2267 IT_UTF8P,
2268 IT_UTF8PS,
2269};
William Dauchy5417e892021-01-08 21:57:41 +01002270
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002271static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
2272 const char *file, int line, char **err)
2273{
Christopher Faulet95917132020-08-05 23:07:07 +02002274 enum input_type type;
2275
Christopher Faulet95917132020-08-05 23:07:07 +02002276 if (strcmp(arg->data.str.area, "") == 0)
2277 type = IT_ASCII;
2278 else if (strcmp(arg->data.str.area, "ascii") == 0)
2279 type = IT_ASCII;
2280 else if (strcmp(arg->data.str.area, "utf8") == 0)
2281 type = IT_UTF8;
2282 else if (strcmp(arg->data.str.area, "utf8s") == 0)
2283 type = IT_UTF8S;
2284 else if (strcmp(arg->data.str.area, "utf8p") == 0)
2285 type = IT_UTF8P;
2286 else if (strcmp(arg->data.str.area, "utf8ps") == 0)
2287 type = IT_UTF8PS;
2288 else {
2289 memprintf(err, "Unexpected input code type. "
2290 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
2291 return 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002292 }
2293
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002294 chunk_destroy(&arg->data.str);
Christopher Faulet95917132020-08-05 23:07:07 +02002295 arg->type = ARGT_SINT;
2296 arg->data.sint = type;
2297 return 1;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002298}
2299
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002300static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002301{
Willy Tarreau83061a82018-07-13 11:56:34 +02002302 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002303 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
2304 const char *str;
2305 int len;
2306 enum input_type input_type = IT_ASCII;
2307 unsigned int c;
2308 unsigned int ret;
2309 char *p;
2310
Christopher Faulete6e7a582021-01-29 11:29:28 +01002311 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002312
2313 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002314 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002315
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002316 p = smp->data.u.str.area;
2317 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002318
2319 if (input_type == IT_ASCII) {
2320 /* Read input as ASCII. */
2321 c = *(unsigned char *)p;
2322 p++;
2323 }
2324 else {
2325 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002326 ret = utf8_next(p,
2327 smp->data.u.str.data - ( p - smp->data.u.str.area),
2328 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002329 p += utf8_return_length(ret);
2330
2331 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2332 return 0;
2333 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2334 continue;
2335 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2336 return 0;
2337 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2338 continue;
2339
2340 /* Check too big values. */
2341 if ((unsigned int)c > 0xffff) {
2342 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2343 return 0;
2344 continue;
2345 }
2346 }
2347
2348 /* Convert character. */
2349 if (c == '"') {
2350 len = 2;
2351 str = "\\\"";
2352 }
2353 else if (c == '\\') {
2354 len = 2;
2355 str = "\\\\";
2356 }
2357 else if (c == '/') {
2358 len = 2;
2359 str = "\\/";
2360 }
2361 else if (c == '\b') {
2362 len = 2;
2363 str = "\\b";
2364 }
2365 else if (c == '\f') {
2366 len = 2;
2367 str = "\\f";
2368 }
2369 else if (c == '\r') {
2370 len = 2;
2371 str = "\\r";
2372 }
2373 else if (c == '\n') {
2374 len = 2;
2375 str = "\\n";
2376 }
2377 else if (c == '\t') {
2378 len = 2;
2379 str = "\\t";
2380 }
Willy Tarreau90807112020-02-25 08:16:33 +01002381 else if (c > 0xff || !isprint((unsigned char)c)) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002382 /* isprint generate a segfault if c is too big. The man says that
2383 * c must have the value of an unsigned char or EOF.
2384 */
2385 len = 6;
2386 _str[0] = '\\';
2387 _str[1] = 'u';
2388 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2389 str = _str;
2390 }
2391 else {
2392 len = 1;
Willy Tarreau5715da22020-02-25 08:37:37 +01002393 _str[0] = c;
2394 str = _str;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002395 }
2396
2397 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002398 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002399 return 0;
2400
2401 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002402 memcpy(temp->area + temp->data, str, len);
2403 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002404 }
2405
2406 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002407 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002408 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002409
2410 return 1;
2411}
2412
Emeric Brun54c4ac82014-11-03 15:32:43 +01002413/* This sample function is designed to extract some bytes from an input buffer.
2414 * First arg is the offset.
2415 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002416static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002417{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002418 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2419 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002420 return 1;
2421 }
2422
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002423 if (smp->data.u.str.size)
2424 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002425 smp->data.u.str.data -= arg_p[0].data.sint;
2426 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002427
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002428 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2429 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002430
2431 return 1;
2432}
2433
Emeric Brunf399b0d2014-11-03 17:07:03 +01002434static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2435 const char *file, int line, char **err)
2436{
2437 struct arg *arg = args;
2438
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002439 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002440 memprintf(err, "Unexpected arg type");
2441 return 0;
2442 }
2443
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002444 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002445 memprintf(err, "Unexpected value 0 for index");
2446 return 0;
2447 }
2448
2449 arg++;
2450
2451 if (arg->type != ARGT_STR) {
2452 memprintf(err, "Unexpected arg type");
2453 return 0;
2454 }
2455
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002456 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002457 memprintf(err, "Empty separators list");
2458 return 0;
2459 }
2460
2461 return 1;
2462}
2463
2464/* This sample function is designed to a return selected part of a string (field).
2465 * First arg is the index of the field (start at 1)
2466 * Second arg is a char list of separators (type string)
2467 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002468static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002469{
Marcin Deranek9631a282018-04-16 14:30:46 +02002470 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002471 char *start, *end;
2472 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002473 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002474
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002475 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002476 return 0;
2477
Marcin Deranek9631a282018-04-16 14:30:46 +02002478 if (arg_p[0].data.sint < 0) {
2479 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002480 end = start = smp->data.u.str.area + smp->data.u.str.data;
2481 while (start > smp->data.u.str.area) {
2482 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2483 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002484 if (field == arg_p[0].data.sint) {
2485 if (count == 1)
2486 goto found;
2487 else if (count > 1)
2488 count--;
2489 } else {
2490 end = start-1;
2491 field--;
2492 }
2493 break;
2494 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002495 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002496 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002497 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002498 } else {
2499 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002500 end = start = smp->data.u.str.area;
2501 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2502 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2503 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002504 if (field == arg_p[0].data.sint) {
2505 if (count == 1)
2506 goto found;
2507 else if (count > 1)
2508 count--;
2509 } else {
2510 start = end+1;
2511 field++;
2512 }
2513 break;
2514 }
2515 }
2516 end++;
2517 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002518 }
2519
2520 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002521 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002522 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002523 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002524 }
2525found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002526 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002527 /* If ret string is len 0, no need to
2528 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002529 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002530 return 1;
2531
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002532 smp->data.u.str.area = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002533
2534 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002535 Note: smp->data.u.str.size cannot be set to 0 */
2536 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002537 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002538
2539 return 1;
2540}
2541
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002542/* This sample function is designed to return a word from a string.
2543 * First arg is the index of the word (start at 1)
2544 * Second arg is a char list of words separators (type string)
2545 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002546static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002547{
Marcin Deranek9631a282018-04-16 14:30:46 +02002548 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002549 char *start, *end;
2550 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002551 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002552
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002553 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002554 return 0;
2555
2556 word = 0;
2557 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002558 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002559 end = start = smp->data.u.str.area + smp->data.u.str.data;
2560 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002561 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002562 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2563 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002564 issep = 1;
2565 break;
2566 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002567 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002568 if (!inword) {
2569 if (!issep) {
2570 if (word != arg_p[0].data.sint) {
2571 word--;
2572 end = start;
2573 }
2574 inword = 1;
2575 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002576 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002577 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002578 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002579 if (count == 1)
2580 goto found;
2581 else if (count > 1)
2582 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002583 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002584 inword = 0;
2585 }
2586 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002587 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002588 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002589 end = start = smp->data.u.str.area;
2590 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002591 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002592 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2593 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002594 issep = 1;
2595 break;
2596 }
2597 }
2598 if (!inword) {
2599 if (!issep) {
2600 if (word != arg_p[0].data.sint) {
2601 word++;
2602 start = end;
2603 }
2604 inword = 1;
2605 }
2606 }
2607 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002608 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002609 if (count == 1)
2610 goto found;
2611 else if (count > 1)
2612 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002613 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002614 inword = 0;
2615 }
2616 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002617 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002618 }
2619
2620 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002621 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002622 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002623 return 1;
2624 }
2625found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002626 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002627 /* If ret string is len 0, no need to
2628 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002629 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002630 return 1;
2631
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002632 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002633
2634 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002635 Note: smp->data.u.str.size cannot be set to 0 */
2636 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002637 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002638
2639 return 1;
2640}
2641
Willy Tarreau7eda8492015-01-20 19:47:06 +01002642static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2643 const char *file, int line, char **err)
2644{
2645 struct arg *arg = args;
2646 char *p;
2647 int len;
2648
2649 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2650 arg[0].type_flags = 0;
2651
2652 if (arg[2].type != ARGT_STR)
2653 return 1;
2654
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002655 p = arg[2].data.str.area;
2656 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002657 while (len) {
2658 if (*p == 'i') {
2659 arg[0].type_flags |= ARGF_REG_ICASE;
2660 }
2661 else if (*p == 'g') {
2662 arg[0].type_flags |= ARGF_REG_GLOB;
2663 }
2664 else {
2665 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2666 return 0;
2667 }
2668 p++;
2669 len--;
2670 }
2671 return 1;
2672}
2673
2674/* This sample function is designed to do the equivalent of s/match/replace/ on
2675 * the input string. It applies a regex and restarts from the last matched
2676 * location until nothing matches anymore. First arg is the regex to apply to
2677 * the input string, second arg is the replacement expression.
2678 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002679static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002680{
2681 char *start, *end;
2682 struct my_regex *reg = arg_p[0].data.reg;
2683 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002684 struct buffer *trash = get_trash_chunk();
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002685 struct buffer *output;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002686 int flag, max;
2687 int found;
2688
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002689 start = smp->data.u.str.area;
2690 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002691
2692 flag = 0;
2693 while (1) {
2694 /* check for last round which is used to copy remaining parts
2695 * when not running in global replacement mode.
2696 */
2697 found = 0;
2698 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2699 /* Note: we can have start == end on empty strings or at the end */
2700 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2701 }
2702
2703 if (!found)
2704 pmatch[0].rm_so = end - start;
2705
2706 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002707 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002708 if (max && pmatch[0].rm_so > 0) {
2709 if (max > pmatch[0].rm_so)
2710 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002711 memcpy(trash->area + trash->data, start, max);
2712 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002713 }
2714
2715 if (!found)
2716 break;
2717
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002718 output = alloc_trash_chunk();
Willy Tarreau23997da2020-02-18 14:27:44 +01002719 if (!output)
2720 break;
2721
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002722 output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2723
Willy Tarreau7eda8492015-01-20 19:47:06 +01002724 /* replace the matching part */
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002725 max = output->size - output->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002726 if (max) {
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002727 if (max > output->data)
2728 max = output->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002729 memcpy(trash->area + trash->data,
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002730 output->area, max);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002731 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002732 }
2733
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002734 free_trash_chunk(output);
2735
Willy Tarreau7eda8492015-01-20 19:47:06 +01002736 /* stop here if we're done with this string */
2737 if (start >= end)
2738 break;
2739
2740 /* We have a special case for matches of length 0 (eg: "x*y*").
2741 * These ones are considered to match in front of a character,
2742 * so we have to copy that character and skip to the next one.
2743 */
2744 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002745 if (trash->data < trash->size)
2746 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002747 pmatch[0].rm_eo++;
2748 }
2749
2750 start += pmatch[0].rm_eo;
2751 flag |= REG_NOTBOL;
2752 }
2753
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002754 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002755 return 1;
2756}
2757
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002758/* This function check an operator entry. It expects a string.
2759 * The string can be an integer or a variable name.
2760 */
2761static int check_operator(struct arg *args, struct sample_conv *conv,
2762 const char *file, int line, char **err)
2763{
2764 const char *str;
2765 const char *end;
Christopher Faulet95917132020-08-05 23:07:07 +02002766 long long int i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002767
2768 /* Try to decode a variable. */
2769 if (vars_check_arg(&args[0], NULL))
2770 return 1;
2771
2772 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002773 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002774 end = str + strlen(str);
Christopher Faulet95917132020-08-05 23:07:07 +02002775 i = read_int64(&str, end);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002776 if (*str != '\0') {
2777 memprintf(err, "expects an integer or a variable name");
2778 return 0;
2779 }
Christopher Faulet95917132020-08-05 23:07:07 +02002780
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002781 chunk_destroy(&args[0].data.str);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002782 args[0].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02002783 args[0].data.sint = i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002784 return 1;
2785}
2786
Willy Tarreau6204cd92016-03-10 16:33:04 +01002787/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002788 * If the arg contain an integer, the integer is returned in the
2789 * sample. If the arg contains a variable descriptor, it returns the
2790 * variable value.
2791 *
2792 * This function returns 0 if an error occurs, otherwise it returns 1.
2793 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002794static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002795{
2796 switch (arg->type) {
2797 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002798 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002799 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002800 return 1;
2801 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002802 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002803 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002804 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002805 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002806 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002807 return 0;
2808 return 1;
2809 default:
2810 return 0;
2811 }
2812}
2813
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002814/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002815 * result.
2816 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002817static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002818{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002819 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002820 return 1;
2821}
2822
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002823/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002824 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002825 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002826static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002827{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002828 struct sample tmp;
2829
Willy Tarreau7560dd42016-03-10 16:28:58 +01002830 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002831 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002832 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002833 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002834 return 1;
2835}
2836
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002837/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002838 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002839 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002840static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002841{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002842 struct sample tmp;
2843
Willy Tarreau7560dd42016-03-10 16:28:58 +01002844 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002845 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002846 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002847 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002848 return 1;
2849}
2850
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002851/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002852 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002853 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002854static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002855{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002856 struct sample tmp;
2857
Willy Tarreau7560dd42016-03-10 16:28:58 +01002858 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002859 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002860 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002861 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002862 return 1;
2863}
2864
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002865static inline long long int arith_add(long long int a, long long int b)
2866{
2867 /* Prevent overflow and makes capped calculus.
2868 * We must ensure that the check calculus doesn't
2869 * exceed the signed 64 bits limits.
2870 *
2871 * +----------+----------+
2872 * | a<0 | a>=0 |
2873 * +------+----------+----------+
2874 * | b<0 | MIN-a>b | no check |
2875 * +------+----------+----------+
2876 * | b>=0 | no check | MAX-a<b |
2877 * +------+----------+----------+
2878 */
2879 if ((a ^ b) >= 0) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002880 /* signs are different. */
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002881 if (a < 0) {
2882 if (LLONG_MIN - a > b)
2883 return LLONG_MIN;
2884 }
2885 if (LLONG_MAX - a < b)
2886 return LLONG_MAX;
2887 }
2888 return a + b;
2889}
2890
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002891/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002892 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002893 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002894static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002895{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002896 struct sample tmp;
2897
Willy Tarreau7560dd42016-03-10 16:28:58 +01002898 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002899 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002900 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002901 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002902 return 1;
2903}
2904
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002905/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002906 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002907 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002908static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002909 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002910{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002911 struct sample tmp;
2912
Willy Tarreau7560dd42016-03-10 16:28:58 +01002913 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002914 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002915 return 0;
2916
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002917 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2918 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2919 * of -LLONG_MIN and correct the result.
2920 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002921 if (tmp.data.u.sint == LLONG_MIN) {
2922 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2923 if (smp->data.u.sint < LLONG_MAX)
2924 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002925 return 1;
2926 }
2927
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002928 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002929 * the second operand.
2930 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002931 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002932 return 1;
2933}
2934
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002935/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002936 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002937 * If the result makes an overflow, then the largest possible quantity is
2938 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002939 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002940static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002941 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002942{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002943 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002944 long long int c;
2945
Willy Tarreau7560dd42016-03-10 16:28:58 +01002946 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002947 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002948 return 0;
2949
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002950 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002951 if (!smp->data.u.sint || !tmp.data.u.sint) {
2952 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002953 return 1;
2954 }
2955
2956 /* The multiply between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002957 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002958 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002959 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2960 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002961 return 1;
2962 }
2963
2964 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002965 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002966
2967 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002968 if (smp->data.u.sint != c / tmp.data.u.sint) {
2969 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2970 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002971 return 1;
2972 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002973 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002974 return 1;
2975 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002976 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002977 return 1;
2978}
2979
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002980/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002981 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002982 * If arg_p makes the result overflow, then the largest possible quantity is
2983 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002984 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002985static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002986 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002987{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002988 struct sample tmp;
2989
Willy Tarreau7560dd42016-03-10 16:28:58 +01002990 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002991 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002992 return 0;
2993
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002994 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002995 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002996 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002997 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002998 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2999 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003000 return 1;
3001 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003002 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003003 return 1;
3004 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003005 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01003006 return 1;
3007}
3008
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003009/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003010 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003011 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003012 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003013static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003014 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003015{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003016 struct sample tmp;
3017
Willy Tarreau7560dd42016-03-10 16:28:58 +01003018 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01003019 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003020 return 0;
3021
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003022 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003023 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003024 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003025 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003026 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3027 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003028 return 1;
3029 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003030 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003031 return 1;
3032 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003033 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01003034 return 1;
3035}
3036
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003037/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01003038 * result.
3039 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003040static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003041 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003042{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003043 if (smp->data.u.sint == LLONG_MIN)
3044 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003045 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003046 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003047 return 1;
3048}
3049
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003050/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003051 * false. The output is a BOOL.
3052 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003053static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003054 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003055{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003056 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003057 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003058 return 1;
3059}
3060
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003061/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003062 * truee. The output is a BOOL.
3063 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003064static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003065 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003066{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003067 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003068 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003069 return 1;
3070}
3071
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003072/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003073 * The output is a BOOL.
3074 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003075static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003076 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003077{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003078 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003079 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003080 return 1;
3081}
3082
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003083/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003084 * The output is a BOOL.
3085 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003086static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003087 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003088{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003089 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003090 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003091 return 1;
3092}
3093
Willy Tarreau280f42b2018-02-19 15:34:12 +01003094/* appends an optional const string, an optional variable contents and another
3095 * optional const string to an existing string.
3096 */
3097static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
3098{
Willy Tarreau83061a82018-07-13 11:56:34 +02003099 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003100 struct sample tmp;
3101 int max;
3102
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003103 trash = alloc_trash_chunk();
William Dauchye9970102021-01-11 11:05:58 +01003104 if (!trash)
3105 return 0;
3106
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003107 trash->data = smp->data.u.str.data;
3108 if (trash->data > trash->size - 1)
3109 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003110
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003111 memcpy(trash->area, smp->data.u.str.area, trash->data);
3112 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003113
3114 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003115 max = arg_p[0].data.str.data;
3116 if (max > trash->size - 1 - trash->data)
3117 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003118
3119 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003120 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
3121 trash->data += max;
3122 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003123 }
3124
3125 /* append second string (variable) if it's found and we can turn it
3126 * into a string.
3127 */
3128 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3129 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
3130 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
3131 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
3132
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003133 max = tmp.data.u.str.data;
3134 if (max > trash->size - 1 - trash->data)
3135 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003136
3137 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003138 memcpy(trash->area + trash->data, tmp.data.u.str.area,
3139 max);
3140 trash->data += max;
3141 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003142 }
3143 }
3144
3145 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003146 max = arg_p[2].data.str.data;
3147 if (max > trash->size - 1 - trash->data)
3148 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003149
3150 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003151 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
3152 trash->data += max;
3153 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003154 }
3155
3156 smp->data.u.str = *trash;
3157 smp->data.type = SMP_T_STR;
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003158 smp_dup(smp);
3159 free_trash_chunk(trash);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003160 return 1;
3161}
3162
3163/* This function checks the "concat" converter's arguments and extracts the
3164 * variable name and its scope.
3165 */
3166static int smp_check_concat(struct arg *args, struct sample_conv *conv,
3167 const char *file, int line, char **err)
3168{
3169 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003170 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
3171 memprintf(err, "failed to register variable name '%s'",
3172 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003173 return 0;
3174 }
3175 return 1;
3176}
3177
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003178/* Compares string with a variable containing a string. Return value
Tim Duesterhusca097c12018-04-27 21:18:45 +02003179 * is compatible with strcmp(3)'s return value.
3180 */
3181static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
3182{
3183 struct sample tmp;
3184 int max, result;
3185
3186 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3187 if (arg_p[0].type != ARGT_VAR)
3188 return 0;
3189 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
3190 return 0;
3191 if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp))
3192 return 0;
3193
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003194 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
3195 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003196 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003197 if (smp->data.u.str.data != tmp.data.u.str.data) {
3198 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02003199 result = -1;
3200 }
3201 else {
3202 result = 1;
3203 }
3204 }
3205 }
3206
3207 smp->data.u.sint = result;
3208 smp->data.type = SMP_T_SINT;
3209 return 1;
3210}
3211
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003212#ifdef USE_OPENSSL
3213/* Compares bytestring with a variable containing a bytestring. Return value
3214 * is `true` if both bytestrings are bytewise identical and `false` otherwise.
3215 *
3216 * Comparison will be performed in constant time if both bytestrings are of
3217 * the same length. If the lengths differ execution time will not be constant.
3218 */
3219static int sample_conv_secure_memcmp(const struct arg *arg_p, struct sample *smp, void *private)
3220{
3221 struct sample tmp;
3222 int result;
3223
3224 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3225 if (arg_p[0].type != ARGT_VAR)
3226 return 0;
3227 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
3228 return 0;
3229 if (!sample_casts[tmp.data.type][SMP_T_BIN](&tmp))
3230 return 0;
3231
3232 if (smp->data.u.str.data != tmp.data.u.str.data) {
3233 smp->data.u.sint = 0;
3234 smp->data.type = SMP_T_BOOL;
3235 return 1;
3236 }
3237
3238 /* The following comparison is performed in constant time. */
3239 result = CRYPTO_memcmp(smp->data.u.str.area, tmp.data.u.str.area, smp->data.u.str.data);
3240
3241 smp->data.u.sint = result == 0;
3242 smp->data.type = SMP_T_BOOL;
3243 return 1;
3244}
3245#endif
3246
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02003247/* Takes a boolean as input. Returns the first argument if that boolean is true and
3248 * the second argument otherwise.
3249 */
3250static int sample_conv_iif(const struct arg *arg_p, struct sample *smp, void *private)
3251{
3252 smp->data.type = SMP_T_STR;
3253 smp->flags |= SMP_F_CONST;
3254
3255 if (smp->data.u.sint) {
3256 smp->data.u.str.data = arg_p[0].data.str.data;
3257 smp->data.u.str.area = arg_p[0].data.str.area;
3258 }
3259 else {
3260 smp->data.u.str.data = arg_p[1].data.str.data;
3261 smp->data.u.str.area = arg_p[1].data.str.area;
3262 }
3263
3264 return 1;
3265}
3266
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003267#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
3268#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
3269#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
3270
3271/*
3272 * Extract the field value of an input binary sample. Takes a mandatory argument:
3273 * the protocol buffers field identifier (dotted notation) internally represented
3274 * as an array of unsigned integers and its size.
3275 * Return 1 if the field was found, 0 if not.
3276 */
3277static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
3278{
3279 unsigned char *pos;
3280 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003281
3282 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003283 grpc_left = smp->data.u.str.data;
3284
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01003285 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003286 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003287
3288 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
3289
3290 pos += GRPC_MSG_HEADER_SZ;
3291 grpc_left -= GRPC_MSG_HEADER_SZ;
3292
3293 if (grpc_left < left)
3294 return 0;
3295
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01003296 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
3297 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003298
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003299 grpc_left -= grpc_msg_len;
3300 }
3301
3302 return 0;
3303}
3304
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003305static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
3306{
3307 unsigned char *pos;
3308 size_t left;
3309
3310 pos = (unsigned char *)smp->data.u.str.area;
3311 left = smp->data.u.str.data;
3312
3313 return protobuf_field_lookup(arg_p, smp, &pos, &left);
3314}
3315
3316static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
3317 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003318{
3319 if (!args[1].type) {
3320 args[1].type = ARGT_SINT;
3321 args[1].data.sint = PBUF_T_BINARY;
3322 }
3323 else {
3324 int pbuf_type;
3325
3326 pbuf_type = protobuf_type(args[1].data.str.area);
3327 if (pbuf_type == -1) {
3328 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
3329 return 0;
3330 }
3331
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003332 chunk_destroy(&args[1].data.str);
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003333 args[1].type = ARGT_SINT;
3334 args[1].data.sint = pbuf_type;
3335 }
3336
3337 return 1;
3338}
3339
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003340/*
3341 * Extract the tag value of an input binary sample. Takes a mandatory argument:
3342 * the FIX protocol tag identifier.
3343 * Return 1 if the tag was found, 0 if not.
3344 */
3345static int sample_conv_fix_tag_value(const struct arg *arg_p, struct sample *smp, void *private)
3346{
3347 struct ist value;
3348
3349 smp->flags &= ~SMP_F_MAY_CHANGE;
3350 value = fix_tag_value(ist2(smp->data.u.str.area, smp->data.u.str.data),
3351 arg_p[0].data.sint);
3352 if (!istlen(value)) {
3353 if (!isttest(value)) {
3354 /* value != IST_NULL, need more data */
3355 smp->flags |= SMP_F_MAY_CHANGE;
3356 }
3357 return 0;
3358 }
3359
3360 smp->data.u.str = ist2buf(value);
3361 smp->flags |= SMP_F_CONST;
3362
3363 return 1;
3364}
3365
3366/* This function checks the "fix_tag_value" converter configuration.
3367 * It expects a "known" (by HAProxy) tag name or ID.
3368 * Tag string names are converted to their ID counterpart because this is the
3369 * format they are sent over the wire.
3370 */
3371static int sample_conv_fix_value_check(struct arg *args, struct sample_conv *conv,
3372 const char *file, int line, char **err)
3373{
3374 struct ist str;
3375 unsigned int tag;
3376
3377 str = ist2(args[0].data.str.area, args[0].data.str.data);
3378 tag = fix_tagid(str);
3379 if (!tag) {
3380 memprintf(err, "Unknown FIX tag name '%s'", args[0].data.str.area);
3381 return 0;
3382 }
3383
3384 chunk_destroy(&args[0].data.str);
3385 args[0].type = ARGT_SINT;
3386 args[0].data.sint = tag;
3387
3388 return 1;
3389}
3390
3391/*
3392 * Checks that a buffer contains a valid FIX message
3393 *
3394 * Return 1 if the check could be run, 0 if not.
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003395 * The result of the analyse itself is stored in <smp> as a boolean
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003396 */
3397static int sample_conv_fix_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3398{
3399 struct ist msg;
3400
3401 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3402
3403 smp->flags &= ~SMP_F_MAY_CHANGE;
3404 switch (fix_validate_message(msg)) {
3405 case FIX_VALID_MESSAGE:
3406 smp->data.type = SMP_T_BOOL;
3407 smp->data.u.sint = 1;
3408 return 1;
3409 case FIX_NEED_MORE_DATA:
3410 smp->flags |= SMP_F_MAY_CHANGE;
3411 return 0;
3412 case FIX_INVALID_MESSAGE:
3413 smp->data.type = SMP_T_BOOL;
3414 smp->data.u.sint = 0;
3415 return 1;
3416 }
3417 return 0;
3418}
3419
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003420/*
3421 * Extract the field value of an input binary sample containing an MQTT packet.
3422 * Takes 2 mandatory arguments:
3423 * - packet type
3424 * - field name
3425 *
3426 * return 1 if the field was found, 0 if not.
3427 */
3428static int sample_conv_mqtt_field_value(const struct arg *arg_p, struct sample *smp, void *private)
3429{
3430 struct ist pkt, value;
3431 int type, fieldname_id;
3432
3433 pkt = ist2(smp->data.u.str.area, smp->data.u.str.data);
3434 type = arg_p[0].data.sint;
3435 fieldname_id = arg_p[1].data.sint;
3436
3437 smp->flags &= ~SMP_F_MAY_CHANGE;
3438 value = mqtt_field_value(pkt, type, fieldname_id);
3439 if (!istlen(value)) {
3440 if (isttest(value)) {
3441 /* value != IST_NULL, need more data */
3442 smp->flags |= SMP_F_MAY_CHANGE;
3443 }
3444 return 0;
3445 }
3446
3447 smp->data.u.str = ist2buf(value);
3448 smp->flags |= SMP_F_CONST;
3449 return 1;
3450}
3451
3452/*
3453 * this function checks the "mqtt_field_value" converter configuration.
3454 * It expects a known packet type name or ID and a field name, in this order
3455 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003456 * Args[0] will be turned into a MQTT_CPT_* value for direct matching when parsing
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003457 * a packet.
3458 */
3459static int sample_conv_mqtt_field_value_check(struct arg *args, struct sample_conv *conv,
3460 const char *file, int line, char **err)
3461{
3462 int type, fieldname_id;
3463
3464 /* check the MQTT packet type is valid */
3465 type = mqtt_typeid(ist2(args[0].data.str.area, args[0].data.str.data));
3466 if (type == MQTT_CPT_INVALID) {
3467 memprintf(err, "Unknown MQTT type '%s'", args[0].data.str.area);
3468 return 0;
3469 }
3470
3471 /* check the field name belongs to the MQTT packet type */
3472 fieldname_id = mqtt_check_type_fieldname(type, ist2(args[1].data.str.area, args[1].data.str.data));
3473 if (fieldname_id == MQTT_FN_INVALID) {
3474 memprintf(err, "Unknown MQTT field name '%s' for packet type '%s'", args[1].data.str.area,
3475 args[0].data.str.area);
3476 return 0;
3477 }
3478
3479 /* save numeric counterparts of type and field name */
3480 chunk_destroy(&args[0].data.str);
3481 chunk_destroy(&args[1].data.str);
3482 args[0].type = ARGT_SINT;
3483 args[0].data.sint = type;
3484 args[1].type = ARGT_SINT;
3485 args[1].data.sint = fieldname_id;
3486
3487 return 1;
3488}
3489
3490/*
3491 * Checks that <smp> contains a valid MQTT message
3492 *
3493 * The function returns 1 if the check was run to its end, 0 otherwise.
3494 * The result of the analyse itself is stored in <smp> as a boolean.
3495 */
3496static int sample_conv_mqtt_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3497{
3498 struct ist msg;
3499
3500 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3501
3502 smp->flags &= ~SMP_F_MAY_CHANGE;
3503 switch (mqtt_validate_message(msg, NULL)) {
3504 case FIX_VALID_MESSAGE:
3505 smp->data.type = SMP_T_BOOL;
3506 smp->data.u.sint = 1;
3507 return 1;
3508 case FIX_NEED_MORE_DATA:
3509 smp->flags |= SMP_F_MAY_CHANGE;
3510 return 0;
3511 case FIX_INVALID_MESSAGE:
3512 smp->data.type = SMP_T_BOOL;
3513 smp->data.u.sint = 0;
3514 return 1;
3515 }
3516 return 0;
3517}
3518
Tim Duesterhusca097c12018-04-27 21:18:45 +02003519/* This function checks the "strcmp" converter's arguments and extracts the
3520 * variable name and its scope.
3521 */
3522static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
3523 const char *file, int line, char **err)
3524{
3525 /* Try to decode a variable. */
3526 if (vars_check_arg(&args[0], NULL))
3527 return 1;
3528
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003529 memprintf(err, "failed to register variable name '%s'",
3530 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003531 return 0;
3532}
3533
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003534#ifdef USE_OPENSSL
3535/* This function checks the "secure_memcmp" converter's arguments and extracts the
3536 * variable name and its scope.
3537 */
3538static int smp_check_secure_memcmp(struct arg *args, struct sample_conv *conv,
3539 const char *file, int line, char **err)
3540{
3541 /* Try to decode a variable. */
3542 if (vars_check_arg(&args[0], NULL))
3543 return 1;
3544
3545 memprintf(err, "failed to register variable name '%s'",
3546 args[0].data.str.area);
3547 return 0;
3548}
3549#endif
3550
Christopher Faulet4ccc12f2020-04-01 09:08:32 +02003551/**/
3552static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private)
3553{
3554 struct buffer *tmp;
3555 uint32_t n;
3556
3557 n = htonl((uint32_t)smp->data.u.sint);
3558 tmp = get_trash_chunk();
3559
3560 memcpy(b_head(tmp), &n, 4);
3561 b_add(tmp, 4);
3562
3563 smp->data.u.str = *tmp;
3564 smp->data.type = SMP_T_BIN;
3565 return 1;
3566}
3567
Christopher Fauletea159d62020-04-01 16:21:44 +02003568/**/
3569static int sample_conv_cut_crlf(const struct arg *arg_p, struct sample *smp, void *private)
3570{
3571 char *p;
3572 size_t l;
3573
3574 p = smp->data.u.str.area;
3575 for (l = 0; l < smp->data.u.str.data; l++) {
3576 if (*(p+l) == '\r' || *(p+l) == '\n')
3577 break;
3578 }
3579 smp->data.u.str.data = l;
3580 return 1;
3581}
3582
Christopher Faulet51fc9d12020-04-01 17:24:41 +02003583/**/
3584static int sample_conv_ltrim(const struct arg *arg_p, struct sample *smp, void *private)
3585{
3586 char *delimiters, *p;
3587 size_t dlen, l;
3588
3589 delimiters = arg_p[0].data.str.area;
3590 dlen = arg_p[0].data.str.data;
3591
3592 l = smp->data.u.str.data;
3593 p = smp->data.u.str.area;
3594 while (l && memchr(delimiters, *p, dlen) != NULL) {
3595 p++;
3596 l--;
3597 }
3598
3599 smp->data.u.str.area = p;
3600 smp->data.u.str.data = l;
3601 return 1;
3602}
3603
Christopher Faulet568415a2020-04-01 17:24:47 +02003604/**/
3605static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *private)
3606{
3607 char *delimiters, *p;
3608 size_t dlen, l;
3609
3610 delimiters = arg_p[0].data.str.area;
3611 dlen = arg_p[0].data.str.data;
3612
3613 l = smp->data.u.str.data;
3614 p = smp->data.u.str.area + l - 1;
3615 while (l && memchr(delimiters, *p, dlen) != NULL) {
3616 p--;
3617 l--;
3618 }
3619
3620 smp->data.u.str.data = l;
3621 return 1;
3622}
3623
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003624/************************************************************************/
3625/* All supported sample fetch functions must be declared here */
3626/************************************************************************/
3627
3628/* force TRUE to be returned at the fetch level */
3629static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003630smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003631{
Willy Tarreau280f42b2018-02-19 15:34:12 +01003632 if (!smp_make_rw(smp))
3633 return 0;
3634
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003635 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003636 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003637 return 1;
3638}
3639
3640/* force FALSE to be returned at the fetch level */
3641static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003642smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003643{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003644 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003645 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003646 return 1;
3647}
3648
3649/* retrieve environment variable $1 as a string */
3650static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003651smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003652{
3653 char *env;
3654
Christopher Faulete6e7a582021-01-29 11:29:28 +01003655 if (args[0].type != ARGT_STR)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003656 return 0;
3657
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003658 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003659 if (!env)
3660 return 0;
3661
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003662 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003663 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003664 smp->data.u.str.area = env;
3665 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003666 return 1;
3667}
3668
Damien Claisseae6f1252019-10-30 15:57:28 +00003669/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3670 * optional string representing the unit of the result: "s" for seconds, "ms" for
3671 * milliseconds and "us" for microseconds.
3672 * Returns 0 on error and non-zero if OK.
3673 */
3674int smp_check_date_unit(struct arg *args, char **err)
3675{
3676 if (args[1].type == ARGT_STR) {
Christopher Faulet95917132020-08-05 23:07:07 +02003677 long long int unit;
3678
Damien Claisseae6f1252019-10-30 15:57:28 +00003679 if (strcmp(args[1].data.str.area, "s") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003680 unit = TIME_UNIT_S;
Damien Claisseae6f1252019-10-30 15:57:28 +00003681 }
3682 else if (strcmp(args[1].data.str.area, "ms") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003683 unit = TIME_UNIT_MS;
Damien Claisseae6f1252019-10-30 15:57:28 +00003684 }
3685 else if (strcmp(args[1].data.str.area, "us") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003686 unit = TIME_UNIT_US;
Damien Claisseae6f1252019-10-30 15:57:28 +00003687 }
3688 else {
3689 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
3690 args[1].data.str.area);
3691 return 0;
3692 }
Christopher Faulet95917132020-08-05 23:07:07 +02003693
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003694 chunk_destroy(&args[1].data.str);
Damien Claisseae6f1252019-10-30 15:57:28 +00003695 args[1].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02003696 args[1].data.sint = unit;
Damien Claisseae6f1252019-10-30 15:57:28 +00003697 }
3698 else if (args[1].type != ARGT_STOP) {
3699 memprintf(err, "Unexpected arg type");
3700 return 0;
3701 }
3702
3703 return 1;
3704}
3705
3706/* retrieve the current local date in epoch time, converts it to milliseconds
3707 * or microseconds if asked to in optional args[1] unit param, and applies an
3708 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003709 */
3710static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003711smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003712{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003713 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003714
Damien Claisseae6f1252019-10-30 15:57:28 +00003715 /* report in milliseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003716 if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003717 smp->data.u.sint *= 1000;
3718 smp->data.u.sint += date.tv_usec / 1000;
3719 }
3720 /* report in microseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003721 else if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003722 smp->data.u.sint *= 1000000;
3723 smp->data.u.sint += date.tv_usec;
3724 }
3725
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003726 /* add offset */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003727 if (args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003728 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003729
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003730 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003731 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3732 return 1;
3733}
3734
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003735/* retrieve the current microsecond part of the date */
3736static int
3737smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3738{
3739 smp->data.u.sint = date.tv_usec;
3740 smp->data.type = SMP_T_SINT;
3741 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3742 return 1;
3743}
3744
3745
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003746/* returns the hostname */
3747static int
3748smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3749{
3750 smp->data.type = SMP_T_STR;
3751 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003752 smp->data.u.str.area = hostname;
3753 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003754 return 1;
3755}
3756
Willy Tarreau0f30d262014-11-24 16:02:05 +01003757/* returns the number of processes */
3758static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003759smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003760{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003761 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003762 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003763 return 1;
3764}
3765
3766/* returns the number of the current process (between 1 and nbproc */
3767static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003768smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003769{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003770 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003771 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003772 return 1;
3773}
3774
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003775/* returns the number of the current thread (between 1 and nbthread */
3776static int
3777smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3778{
3779 smp->data.type = SMP_T_SINT;
3780 smp->data.u.sint = tid;
3781 return 1;
3782}
3783
Willy Tarreau84310e22014-02-14 11:59:04 +01003784/* generate a random 32-bit integer for whatever purpose, with an optional
3785 * range specified in argument.
3786 */
3787static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003788smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01003789{
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003790 smp->data.u.sint = ha_random32();
Willy Tarreau84310e22014-02-14 11:59:04 +01003791
3792 /* reduce if needed. Don't do a modulo, use all bits! */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003793 if (args[0].type == ARGT_SINT)
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003794 smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32;
Willy Tarreau84310e22014-02-14 11:59:04 +01003795
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003796 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01003797 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3798 return 1;
3799}
3800
Willy Tarreau0f30d262014-11-24 16:02:05 +01003801/* returns true if the current process is stopping */
3802static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003803smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003804{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003805 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003806 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003807 return 1;
3808}
3809
Willy Tarreau70fe9442018-11-22 16:07:39 +01003810/* returns the number of calls of the current stream's process_stream() */
3811static int
3812smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
3813{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003814 if (!smp->strm)
3815 return 0;
3816
Willy Tarreau70fe9442018-11-22 16:07:39 +01003817 smp->data.type = SMP_T_SINT;
3818 smp->data.u.sint = smp->strm->task->calls;
3819 return 1;
3820}
3821
3822/* returns the average number of nanoseconds spent processing the stream per call */
3823static int
3824smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3825{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003826 if (!smp->strm)
3827 return 0;
3828
Willy Tarreau70fe9442018-11-22 16:07:39 +01003829 smp->data.type = SMP_T_SINT;
3830 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
3831 return 1;
3832}
3833
3834/* returns the total number of nanoseconds spent processing the stream */
3835static int
3836smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3837{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003838 if (!smp->strm)
3839 return 0;
3840
Willy Tarreau70fe9442018-11-22 16:07:39 +01003841 smp->data.type = SMP_T_SINT;
3842 smp->data.u.sint = smp->strm->task->cpu_time;
3843 return 1;
3844}
3845
3846/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
3847static int
3848smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3849{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003850 if (!smp->strm)
3851 return 0;
3852
Willy Tarreau70fe9442018-11-22 16:07:39 +01003853 smp->data.type = SMP_T_SINT;
3854 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
3855 return 1;
3856}
3857
3858/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
3859static int
3860smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3861{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003862 if (!smp->strm)
3863 return 0;
3864
Willy Tarreau70fe9442018-11-22 16:07:39 +01003865 smp->data.type = SMP_T_SINT;
3866 smp->data.u.sint = smp->strm->task->lat_time;
3867 return 1;
3868}
3869
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003870static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
3871{
3872 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003873 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003874 smp->data.u.str.area = args[0].data.str.area;
3875 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003876 return 1;
3877}
3878
3879static int smp_check_const_bool(struct arg *args, char **err)
3880{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003881 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
3882 strcasecmp(args[0].data.str.area, "1") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003883 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003884 args[0].type = ARGT_SINT;
3885 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003886 return 1;
3887 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003888 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
3889 strcasecmp(args[0].data.str.area, "0") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003890 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003891 args[0].type = ARGT_SINT;
3892 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003893 return 1;
3894 }
3895 memprintf(err, "Expects 'true', 'false', '0' or '1'");
3896 return 0;
3897}
3898
3899static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
3900{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003901 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003902 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003903 return 1;
3904}
3905
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003906static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003907{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003908 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003909 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003910 return 1;
3911}
3912
3913static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
3914{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003915 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003916 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003917 return 1;
3918}
3919
3920static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
3921{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003922 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003923 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003924 return 1;
3925}
3926
3927static int smp_check_const_bin(struct arg *args, char **err)
3928{
David Carlier64a16ab2016-04-08 10:37:02 +01003929 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003930 int binstrlen;
3931
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003932 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003933 return 0;
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003934 chunk_destroy(&args[0].data.str);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003935 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003936 args[0].data.str.area = binstr;
3937 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003938 return 1;
3939}
3940
3941static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
3942{
3943 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003944 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003945 smp->data.u.str.area = args[0].data.str.area;
3946 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003947 return 1;
3948}
3949
3950static int smp_check_const_meth(struct arg *args, char **err)
3951{
3952 enum http_meth_t meth;
3953 int i;
3954
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003955 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003956 if (meth != HTTP_METH_OTHER) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003957 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003958 args[0].type = ARGT_SINT;
3959 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003960 } else {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003961 /* Check method avalaibility. A method is a token defined as :
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003962 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
3963 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
3964 * token = 1*tchar
3965 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003966 for (i = 0; i < args[0].data.str.data; i++) {
3967 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003968 memprintf(err, "expects valid method.");
3969 return 0;
3970 }
3971 }
3972 }
3973 return 1;
3974}
3975
3976static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
3977{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003978 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003979 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003980 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003981 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003982 smp->data.u.meth.str.area = "";
3983 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003984 } else {
3985 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003986 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003987 smp->data.u.meth.str.area = args[0].data.str.area;
3988 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003989 }
3990 return 1;
3991}
3992
Luca Schimweg8a694b82019-09-10 15:42:52 +02003993// This function checks the "uuid" sample's arguments.
3994// Function won't get called when no parameter is specified (maybe a bug?)
3995static int smp_check_uuid(struct arg *args, char **err)
3996{
3997 if (!args[0].type) {
3998 args[0].type = ARGT_SINT;
3999 args[0].data.sint = 4;
4000 }
4001 else if (args[0].data.sint != 4) {
4002 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
4003 return 0;
4004 }
4005
4006 return 1;
4007}
4008
4009// Generate a RFC4122 UUID (default is v4 = fully random)
4010static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
4011{
4012 if (args[0].data.sint == 4 || !args[0].type) {
Willy Tarreauee3bcdd2020-03-08 17:48:17 +01004013 ha_generate_uuid(&trash);
Luca Schimweg8a694b82019-09-10 15:42:52 +02004014 smp->data.type = SMP_T_STR;
4015 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4016 smp->data.u.str = trash;
4017 return 1;
4018 }
4019
4020 // more implementations of other uuid formats possible here
4021 return 0;
4022}
4023
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004024/* Note: must not be declared <const> as its list will be overwritten.
4025 * Note: fetches that may return multiple types must be declared as the lowest
4026 * common denominator, the type that can be casted into all other ones. For
4027 * instance IPv4/IPv6 must be declared IPv4.
4028 */
4029static struct sample_fetch_kw_list smp_kws = {ILH, {
4030 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
4031 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01004032 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Damien Claisseae6f1252019-10-30 15:57:28 +00004033 { "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 +01004034 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01004035 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02004036 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4037 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Christopher Faulet34adb2a2017-11-21 21:45:38 +01004038 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004039 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01004040 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +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);