blob: 5b78125fc0da0323443a12acb2454d62defa0e90 [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] = {
Willy Tarreaube2159b2021-03-26 11:56:11 +010073 [SMP_SRC_CONST] = (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 |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +010079 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL_CFG_PARSER),
Willy Tarreaube2159b2021-03-26 11:56:11 +010080
Willy Tarreau80aca902013-01-07 15:42:20 +010081 [SMP_SRC_INTRN] = (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 |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +010087 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +010088
89 [SMP_SRC_LISTN] = (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 |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +010095 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +010096
97 [SMP_SRC_FTEND] = (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 |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +0100103 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100104
105 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | 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 |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +0100111 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100112
113 [SMP_SRC_L5CLI] = (SMP_VAL___________ | 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 |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +0100119 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100120
121 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | 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_BE_RES_CNT |
125 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
126 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +0100127 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100128
129 [SMP_SRC_L6REQ] = (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___________ |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +0100135 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100136
137 [SMP_SRC_HRQHV] = (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___________ |
141 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
142 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +0100143 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100144
145 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
146 SMP_VAL_FE_HRQ_HDR | 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_BE_RES_CNT |
149 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
150 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +0100151 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100152
153 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
154 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
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___________ |
157 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
158 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +0100159 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100160
161 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
162 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
163 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
164 SMP_VAL_BE_SET_SRV | 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 |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +0100167 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100168
169 [SMP_SRC_SERVR] = (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_BE_SRV_CON | 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 |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +0100175 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100176
177 [SMP_SRC_L4SRV] = (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 |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +0100183 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100184
185 [SMP_SRC_L5SRV] = (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 |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +0100191 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100192
193 [SMP_SRC_L6RES] = (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 |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +0100199 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100200
201 [SMP_SRC_HRSHV] = (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 |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +0100207 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100208
209 [SMP_SRC_HRSHP] = (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_BE_RES_CNT |
213 SMP_VAL_BE_HRS_HDR | 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 |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +0100215 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100216
217 [SMP_SRC_HRSBO] = (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_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
222 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +0100223 SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100224
225 [SMP_SRC_RQFIN] = (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___________ |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +0100231 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100232
233 [SMP_SRC_RSFIN] = (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___________ |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +0100239 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100240
241 [SMP_SRC_TXFIN] = (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___________ |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +0100247 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100248
249 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
250 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
251 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
252 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
253 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
254 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Willy Tarreauf9a7a8f2021-03-26 11:09:38 +0100255 SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100256};
257
258static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
259 [SMP_SRC_INTRN] = "internal state",
260 [SMP_SRC_LISTN] = "listener",
261 [SMP_SRC_FTEND] = "frontend",
262 [SMP_SRC_L4CLI] = "client address",
263 [SMP_SRC_L5CLI] = "client-side connection",
264 [SMP_SRC_TRACK] = "track counters",
265 [SMP_SRC_L6REQ] = "request buffer",
266 [SMP_SRC_HRQHV] = "HTTP request headers",
267 [SMP_SRC_HRQHP] = "HTTP request",
268 [SMP_SRC_HRQBO] = "HTTP request body",
269 [SMP_SRC_BKEND] = "backend",
270 [SMP_SRC_SERVR] = "server",
271 [SMP_SRC_L4SRV] = "server address",
272 [SMP_SRC_L5SRV] = "server-side connection",
273 [SMP_SRC_L6RES] = "response buffer",
274 [SMP_SRC_HRSHV] = "HTTP response headers",
275 [SMP_SRC_HRSHP] = "HTTP response",
276 [SMP_SRC_HRSBO] = "HTTP response body",
277 [SMP_SRC_RQFIN] = "request buffer statistics",
278 [SMP_SRC_RSFIN] = "response buffer statistics",
279 [SMP_SRC_TXFIN] = "transaction statistics",
280 [SMP_SRC_SSFIN] = "session statistics",
281};
282
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100283static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
284 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
285 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
286 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
287 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
288 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
289 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
290 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
291 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
292 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
293 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
294 [SMP_CKP_BE_SRV_CON] = "server source selection",
295 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
296 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
297 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
298 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
299 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
300 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
301 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
302 [SMP_CKP_FE_LOG_END] = "logs",
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100303 [SMP_CKP_BE_CHK_RUL] = "backend tcp-check rule",
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100304};
305
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100306/* This function returns the type of the data returned by the sample_expr.
307 * It assumes that the <expr> and all of its converters are properly
308 * initialized.
309 */
310inline
311int smp_expr_output_type(struct sample_expr *expr)
312{
313 struct sample_conv_expr *smp_expr;
314
315 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
316 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
317 return smp_expr->conv->out_type;
318 }
319 return expr->fetch->out_type;
320}
321
322
Willy Tarreau80aca902013-01-07 15:42:20 +0100323/* fill the trash with a comma-delimited list of source names for the <use> bit
324 * field which must be composed of a non-null set of SMP_USE_* flags. The return
325 * value is the pointer to the string in the trash buffer.
326 */
327const char *sample_src_names(unsigned int use)
328{
329 int bit;
330
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200331 trash.data = 0;
332 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100333 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
334 if (!(use & ~((1 << bit) - 1)))
335 break; /* no more bits */
336
337 if (!(use & (1 << bit)))
338 continue; /* bit not set */
339
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200340 trash.data += snprintf(trash.area + trash.data,
341 trash.size - trash.data, "%s%s",
342 (use & ((1 << bit) - 1)) ? "," : "",
343 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100344 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200345 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100346}
347
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100348/* return a pointer to the correct sample checkpoint name, or "unknown" when
349 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
350 * if set.
351 */
352const char *sample_ckp_names(unsigned int use)
353{
354 int bit;
355
356 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
357 if (use & (1 << bit))
358 return fetch_ckp_names[bit];
359 return "unknown sample check place, please report this bug";
360}
361
Emeric Brun107ca302010-01-04 16:16:05 +0100362/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100363 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
364 * for next parsing sessions. The fetch keywords capabilities are also computed
365 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100366 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100367void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100368{
Willy Tarreau80aca902013-01-07 15:42:20 +0100369 struct sample_fetch *sf;
370 int bit;
371
372 for (sf = kwl->kw; sf->kw != NULL; sf++) {
373 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
374 if (sf->use & (1 << bit))
375 sf->val |= fetch_cap[bit];
376 }
377 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100378}
379
380/*
Willy Tarreau12785782012-04-27 21:37:17 +0200381 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100382 * parsing sessions.
383 */
Willy Tarreau12785782012-04-27 21:37:17 +0200384void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100385{
Willy Tarreau12785782012-04-27 21:37:17 +0200386 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100387}
388
389/*
Willy Tarreau12785782012-04-27 21:37:17 +0200390 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100391 * string of <len> in buffer <kw>.
392 *
393 */
Willy Tarreau12785782012-04-27 21:37:17 +0200394struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100395{
396 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200397 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100398
Willy Tarreau12785782012-04-27 21:37:17 +0200399 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100400 for (index = 0; kwl->kw[index].kw != NULL; index++) {
401 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
402 kwl->kw[index].kw[len] == '\0')
403 return &kwl->kw[index];
404 }
405 }
406 return NULL;
407}
408
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100409/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100410 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100411 * <idx> is the index of the next sample fetch entry. It is used as private
412 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100413 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100414 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100415 * the list is reached.
416 */
417struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
418{
419 struct sample_fetch_kw_list *kwl;
420 struct sample_fetch *base;
421
422 if (!current) {
423 /* Get first kwl entry. */
424 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
425 (*idx) = 0;
426 } else {
427 /* Get kwl corresponding to the curret entry. */
428 base = current + 1 - (*idx);
429 kwl = container_of(base, struct sample_fetch_kw_list, kw);
430 }
431
432 while (1) {
433
434 /* Check if kwl is the last entry. */
435 if (&kwl->list == &sample_fetches.list)
436 return NULL;
437
438 /* idx contain the next keyword. If it is available, return it. */
439 if (kwl->kw[*idx].kw) {
440 (*idx)++;
441 return &kwl->kw[(*idx)-1];
442 }
443
444 /* get next entry in the main list, and return NULL if the end is reached. */
445 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
446
447 /* Set index to 0, ans do one other loop. */
448 (*idx) = 0;
449 }
450}
451
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100452/* This function browses the list of available converters. <current> is
453 * the last used converter. If it is the first call, it must set to NULL.
454 * <idx> is the index of the next converter entry. It is used as private
455 * value. It is useless to initiate it.
456 *
457 * It returns always the next sample_conv entry, and NULL when the end of
458 * the list is reached.
459 */
460struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
461{
462 struct sample_conv_kw_list *kwl;
463 struct sample_conv *base;
464
465 if (!current) {
466 /* Get first kwl entry. */
467 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
468 (*idx) = 0;
469 } else {
470 /* Get kwl corresponding to the curret entry. */
471 base = current + 1 - (*idx);
472 kwl = container_of(base, struct sample_conv_kw_list, kw);
473 }
474
475 while (1) {
476 /* Check if kwl is the last entry. */
477 if (&kwl->list == &sample_convs.list)
478 return NULL;
479
480 /* idx contain the next keyword. If it is available, return it. */
481 if (kwl->kw[*idx].kw) {
482 (*idx)++;
483 return &kwl->kw[(*idx)-1];
484 }
485
486 /* get next entry in the main list, and return NULL if the end is reached. */
487 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
488
489 /* Set index to 0, ans do one other loop. */
490 (*idx) = 0;
491 }
492}
493
Emeric Brun107ca302010-01-04 16:16:05 +0100494/*
Willy Tarreau12785782012-04-27 21:37:17 +0200495 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100496 * string of <len> in buffer <kw>.
497 *
498 */
Willy Tarreau12785782012-04-27 21:37:17 +0200499struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100500{
501 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200502 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100503
Willy Tarreau12785782012-04-27 21:37:17 +0200504 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100505 for (index = 0; kwl->kw[index].kw != NULL; index++) {
506 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
507 kwl->kw[index].kw[len] == '\0')
508 return &kwl->kw[index];
509 }
510 }
511 return NULL;
512}
513
Emeric Brun107ca302010-01-04 16:16:05 +0100514/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200515/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100516/******************************************************************/
517
Willy Tarreau342acb42012-04-23 22:03:39 +0200518static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100519{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200520 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200521 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100522 return 1;
523}
524
Willy Tarreau342acb42012-04-23 22:03:39 +0200525static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100526{
Willy Tarreau83061a82018-07-13 11:56:34 +0200527 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100528
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200529 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100530 return 0;
531
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200532 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200533 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200534 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100535 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100536
537 return 1;
538}
539
Willy Tarreau342acb42012-04-23 22:03:39 +0200540static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100541{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200542 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200543 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100544 return 1;
545}
546
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200547static int c_ipv62ip(struct sample *smp)
548{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200549 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200550 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100551 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200552 return 1;
553}
554
Willy Tarreau342acb42012-04-23 22:03:39 +0200555static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100556{
Willy Tarreau83061a82018-07-13 11:56:34 +0200557 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100558
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200559 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100560 return 0;
561
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200562 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200563 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200564 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100565 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100566 return 1;
567}
568
569/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200570static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100571{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200572 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100573}
574*/
575
Willy Tarreau342acb42012-04-23 22:03:39 +0200576static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100577{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200578 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200579 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100580 return 1;
581}
582
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200583static int c_int2ipv6(struct sample *smp)
584{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200585 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
586 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200587 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200588 return 1;
589}
590
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100591static int c_str2addr(struct sample *smp)
592{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200593 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
594 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100595 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200596 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100597 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100598 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100599 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200600 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100601 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100602 return 1;
603}
604
Willy Tarreau342acb42012-04-23 22:03:39 +0200605static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100606{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200607 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100608 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200609 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100610 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100611 return 1;
612}
613
Willy Tarreau342acb42012-04-23 22:03:39 +0200614static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100615{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200616 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100617 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200618 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100619 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100620 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100621}
622
Emeric Brun4b9e8022014-11-03 18:17:10 +0100623/*
624 * The NULL char always enforces the end of string if it is met.
625 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100626 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200627static int c_bin2str(struct sample *smp)
628{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100629 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200630
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200631 for (i = 0; i < smp->data.u.str.data; i++) {
632 if (!smp->data.u.str.area[i]) {
633 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100634 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100635 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200636 }
Christopher Faulet472ad512020-04-30 09:57:40 +0200637 smp->data.type = SMP_T_STR;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200638 return 1;
639}
640
Willy Tarreau342acb42012-04-23 22:03:39 +0200641static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100642{
Willy Tarreau83061a82018-07-13 11:56:34 +0200643 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100644 char *pos;
645
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200646 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100647 if (!pos)
648 return 0;
649
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200650 trash->size = trash->size - (pos - trash->area);
651 trash->area = pos;
652 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200653 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200654 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100655 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100656 return 1;
657}
658
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800659/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200660 * For strings and binary blocks, it also provides a known allocated size with
661 * a length that is capped to the size, and ensures a trailing zero is always
662 * appended for strings. This is necessary for some operations which may
663 * require to extend the length. It returns 0 if it fails, 1 on success.
664 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100665int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200666{
Willy Tarreau83061a82018-07-13 11:56:34 +0200667 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200668
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200669 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100670 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100671 case SMP_T_SINT:
672 case SMP_T_ADDR:
673 case SMP_T_IPV4:
674 case SMP_T_IPV6:
675 /* These type are not const. */
676 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200677
Christopher Fauletec100512017-07-24 15:38:41 +0200678 case SMP_T_METH:
679 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
680 break;
681 /* Fall through */
682
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100683 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200684 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100685 trash->data = smp->data.type == SMP_T_STR ?
686 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200687 if (trash->data > trash->size - 1)
688 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200689
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100690 memcpy(trash->area, smp->data.type == SMP_T_STR ?
691 smp->data.u.str.area : smp->data.u.meth.str.area,
692 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200693 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200694 smp->data.u.str = *trash;
695 break;
696
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100697 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100698 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200699 trash->data = smp->data.u.str.data;
700 if (trash->data > trash->size)
701 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200702
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200703 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200704 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100705 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200706
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100707 default:
708 /* Other cases are unexpected. */
709 return 0;
710 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100711
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100712 /* remove const flag */
713 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200714 return 1;
715}
716
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100717int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100718{
719 return 1;
720}
721
Willy Tarreau342acb42012-04-23 22:03:39 +0200722static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100723{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200724 const char *str;
725 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100726
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200727 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100728 return 0;
729
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200730 str = smp->data.u.str.area;
731 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100732
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200733 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200734 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100735 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100736 return 1;
737}
738
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100739static int c_str2meth(struct sample *smp)
740{
741 enum http_meth_t meth;
742 int len;
743
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200744 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100745 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200746 len = smp->data.u.str.data;
747 smp->data.u.meth.str.area = smp->data.u.str.area;
748 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100749 }
750 else
751 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200752 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200753 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100754 return 1;
755}
756
757static int c_meth2str(struct sample *smp)
758{
759 int len;
760 enum http_meth_t meth;
761
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200762 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100763 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200764 len = smp->data.u.meth.str.data;
765 smp->data.u.str.area = smp->data.u.meth.str.area;
766 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200767 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100768 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200769 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100770 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200771 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200772 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200773 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100774 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200775 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100776 }
777 else {
778 /* Unknown method */
779 return 0;
780 }
781 return 1;
782}
783
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200784static int c_addr2bin(struct sample *smp)
785{
Willy Tarreau83061a82018-07-13 11:56:34 +0200786 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200787
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200788 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200789 chk->data = 4;
790 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200791 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200792 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200793 chk->data = 16;
794 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200795 }
796 else
797 return 0;
798
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200799 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200800 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200801 return 1;
802}
803
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200804static int c_int2bin(struct sample *smp)
805{
Willy Tarreau83061a82018-07-13 11:56:34 +0200806 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200807
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200808 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
809 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200810
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200811 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200812 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200813 return 1;
814}
815
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200816
Emeric Brun107ca302010-01-04 16:16:05 +0100817/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200818/* Sample casts matrix: */
819/* sample_casts[from type][to type] */
820/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100821/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100822
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100823sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200824/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
825/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
826/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200827/* 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 +0200828/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
829/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200830/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200831/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
832/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
833/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200834};
Emeric Brun107ca302010-01-04 16:16:05 +0100835
Emeric Brun107ca302010-01-04 16:16:05 +0100836/*
Willy Tarreau12785782012-04-27 21:37:17 +0200837 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100838 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200839 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200840 * The caller must have set al->ctx.
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100841 * If <endptr> is non-nul, it will be set to the first unparsed character
842 * (which may be the final '\0') on success. If it is nul, the expression
843 * must be properly terminated by a '\0' otherwise an error is reported.
Emeric Brun107ca302010-01-04 16:16:05 +0100844 */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100845struct 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 +0100846{
Willy Tarreau833cc792013-07-24 15:34:19 +0200847 const char *begw; /* beginning of word */
848 const char *endw; /* end of word */
849 const char *endt; /* end of term */
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +0100850 struct sample_expr *expr = NULL;
Willy Tarreau12785782012-04-27 21:37:17 +0200851 struct sample_fetch *fetch;
852 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100853 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200854 char *fkw = NULL;
855 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100856 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100857
Willy Tarreau833cc792013-07-24 15:34:19 +0200858 begw = str[*idx];
Willy Tarreaued2c6622020-02-14 18:27:10 +0100859 for (endw = begw; is_idchar(*endw); endw++)
860 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200861
862 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100863 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100864 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200865 }
Emeric Brun107ca302010-01-04 16:16:05 +0100866
Willy Tarreau833cc792013-07-24 15:34:19 +0200867 /* keep a copy of the current fetch keyword for error reporting */
868 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100869
Willy Tarreau833cc792013-07-24 15:34:19 +0200870 fetch = find_sample_fetch(begw, endw - begw);
871 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100872 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100873 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200874 }
Emeric Brun107ca302010-01-04 16:16:05 +0100875
Willy Tarreau833cc792013-07-24 15:34:19 +0200876 /* At this point, we have :
877 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100878 * - endw : end of the keyword, first character not part of keyword
Willy Tarreau833cc792013-07-24 15:34:19 +0200879 */
880
881 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100882 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100883 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200884 }
Emeric Brun107ca302010-01-04 16:16:05 +0100885 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200886
Vincent Bernat02779b62016-04-03 13:48:43 +0200887 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200888 if (!expr)
889 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100890
891 LIST_INIT(&(expr->conv_exprs));
892 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200893 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100894
Willy Tarreau689a1df2013-12-13 00:40:11 +0100895 /* Note that we call the argument parser even with an empty string,
896 * this allows it to automatically create entries for mandatory
897 * implicit arguments (eg: local proxy name).
898 */
899 al->kw = expr->fetch->kw;
900 al->conv = NULL;
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100901 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 +0100902 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
903 goto out_error;
904 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200905
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100906 /* now endt is our first char not part of the arg list, typically the
907 * comma after the sample fetch name or after the closing parenthesis,
908 * or the NUL char.
909 */
910
Willy Tarreau689a1df2013-12-13 00:40:11 +0100911 if (!expr->arg_p) {
912 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200913 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100914 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
915 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200916 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100917 }
918
Willy Tarreau833cc792013-07-24 15:34:19 +0200919 /* Now process the converters if any. We have two supported syntaxes
920 * for the converters, which can be combined :
921 * - comma-delimited list of converters just after the keyword and args ;
922 * - one converter per keyword
923 * The combination allows to have each keyword being a comma-delimited
924 * series of converters.
925 *
926 * We want to process the former first, then the latter. For this we start
927 * from the beginning of the supposed place in the exiting conv chain, which
928 * starts at the last comma (endt).
929 */
930
931 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200932 struct sample_conv_expr *conv_expr;
Willy Tarreau46dfd782019-12-17 10:25:29 +0100933 int err_arg;
934 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +0100935
Willy Tarreau833cc792013-07-24 15:34:19 +0200936 if (*endt && *endt != ',') {
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100937 if (endptr) {
938 /* end found, let's stop here */
939 break;
940 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200941 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100942 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200943 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100944 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100945 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200946 }
Emeric Brun107ca302010-01-04 16:16:05 +0100947
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100948 /* FIXME: how long should we support such idiocies ? Maybe we
949 * should already warn ?
950 */
Willy Tarreau833cc792013-07-24 15:34:19 +0200951 while (*endt == ',') /* then trailing commas */
952 endt++;
953
Willy Tarreau97108e02016-11-25 07:33:24 +0100954 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200955
956 if (!*begw) {
957 /* none ? skip to next string */
958 (*idx)++;
959 begw = str[*idx];
960 if (!begw || !*begw)
961 break;
962 }
963
Willy Tarreaued2c6622020-02-14 18:27:10 +0100964 for (endw = begw; is_idchar(*endw); endw++)
965 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200966
967 free(ckw);
968 ckw = my_strndup(begw, endw - begw);
969
970 conv = find_sample_conv(begw, endw - begw);
971 if (!conv) {
972 /* we found an isolated keyword that we don't know, it's not ours */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100973 if (begw == str[*idx]) {
974 endt = begw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200975 break;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100976 }
Willy Tarreau97108e02016-11-25 07:33:24 +0100977 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200978 goto out_error;
979 }
Emeric Brun107ca302010-01-04 16:16:05 +0100980
Willy Tarreau833cc792013-07-24 15:34:19 +0200981 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100982 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100983 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200984 }
Emeric Brun107ca302010-01-04 16:16:05 +0100985
986 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200987 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100988 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100989 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200990 }
Emeric Brun107ca302010-01-04 16:16:05 +0100991
992 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +0200993 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200994 if (!conv_expr)
995 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100996
997 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
998 conv_expr->conv = conv;
999
Willy Tarreau46dfd782019-12-17 10:25:29 +01001000 al->kw = expr->fetch->kw;
1001 al->conv = conv_expr->conv->kw;
Willy Tarreau80b53ff2020-02-14 08:40:37 +01001002 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 +01001003 if (argcnt < 0) {
1004 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
1005 goto out_error;
1006 }
Willy Tarreau9e92d322010-01-26 17:58:06 +01001007
Willy Tarreau46dfd782019-12-17 10:25:29 +01001008 if (argcnt && !conv->arg_mask) {
1009 memprintf(err_msg, "converter '%s' does not support any args", ckw);
1010 goto out_error;
1011 }
Willy Tarreau21d68a62012-04-20 15:52:36 +02001012
Willy Tarreau46dfd782019-12-17 10:25:29 +01001013 if (!conv_expr->arg_p)
1014 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001015
Willy Tarreau46dfd782019-12-17 10:25:29 +01001016 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1017 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001018 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001019 }
1020 }
Emeric Brun485479d2010-09-23 18:02:19 +02001021
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001022 if (endptr) {
1023 /* end found, let's stop here */
1024 *endptr = (char *)endt;
1025 }
1026
Willy Tarreau833cc792013-07-24 15:34:19 +02001027 out:
1028 free(fkw);
1029 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001030 return expr;
1031
1032out_error:
Remi Tricot-Le Breton22e0d9b2021-01-12 14:55:12 +01001033 release_sample_expr(expr);
Willy Tarreau833cc792013-07-24 15:34:19 +02001034 expr = NULL;
1035 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001036}
1037
1038/*
Willy Tarreau12785782012-04-27 21:37:17 +02001039 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001040 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001041 * Returns a pointer on a typed sample structure containing the result or NULL if
1042 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001043 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001044 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001045 *
1046 * Note: the fetch functions are required to properly set the return type. The
1047 * conversion functions must do so too. However the cast functions do not need
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001048 * to since they're made to cast multiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001049 *
1050 * The caller may indicate in <opt> if it considers the result final or not.
1051 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1052 * if the result is stable or not, according to the following table :
1053 *
1054 * return MAY_CHANGE FINAL Meaning for the sample
1055 * NULL 0 * Not present and will never be (eg: header)
1056 * NULL 1 0 Not present yet, could change (eg: POST param)
1057 * NULL 1 1 Not present yet, will not change anymore
1058 * smp 0 * Present and will not change (eg: header)
1059 * smp 1 0 Present, may change (eg: request length)
1060 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001061 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001062struct sample *sample_process(struct proxy *px, struct session *sess,
1063 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001064 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001065{
Willy Tarreau12785782012-04-27 21:37:17 +02001066 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001067
Willy Tarreau18387e22013-07-25 12:02:38 +02001068 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001069 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001070 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001071 }
Emeric Brun107ca302010-01-04 16:16:05 +01001072
Willy Tarreau1777ea62016-03-10 16:15:46 +01001073 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001074 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001075 return NULL;
1076
Emeric Brun107ca302010-01-04 16:16:05 +01001077 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001078 /* we want to ensure that p->type can be casted into
1079 * conv_expr->conv->in_type. We have 3 possibilities :
1080 * - NULL => not castable.
1081 * - c_none => nothing to do (let's optimize it)
1082 * - other => apply cast and prepare to fail
1083 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001084 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001085 return NULL;
1086
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001087 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1088 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001089 return NULL;
1090
Willy Tarreau12e50112012-04-25 17:21:49 +02001091 /* OK cast succeeded */
1092
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001093 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001094 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001095 }
1096 return p;
1097}
1098
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001099/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001100 * Resolve all remaining arguments in proxy <p>. Returns the number of
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001101 * errors or 0 if everything is fine. If at least one error is met, it will
1102 * be appended to *err. If *err==NULL it will be allocated first.
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001103 */
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001104int smp_resolve_args(struct proxy *p, char **err)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001105{
1106 struct arg_list *cur, *bak;
1107 const char *ctx, *where;
1108 const char *conv_ctx, *conv_pre, *conv_pos;
1109 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001110 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001111 struct arg *arg;
1112 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001113 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001114
1115 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1116 struct proxy *px;
1117 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001118 struct stktable *t;
1119 char *pname, *sname, *stktname;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001120 char *err2;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001121
1122 arg = cur->arg;
1123
1124 /* prepare output messages */
1125 conv_pre = conv_pos = conv_ctx = "";
1126 if (cur->conv) {
1127 conv_ctx = cur->conv;
1128 conv_pre = "conversion keyword '";
1129 conv_pos = "' for ";
1130 }
1131
1132 where = "in";
1133 ctx = "sample fetch keyword";
1134 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001135 case ARGC_STK: where = "in stick rule in"; break;
1136 case ARGC_TRK: where = "in tracking rule in"; break;
1137 case ARGC_LOG: where = "in log-format string in"; break;
1138 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1139 case ARGC_HRQ: where = "in http-request header format string in"; break;
1140 case ARGC_HRS: where = "in http-response header format string in"; break;
1141 case ARGC_UIF: where = "in unique-id-format string in"; break;
1142 case ARGC_RDR: where = "in redirect format string in"; break;
1143 case ARGC_CAP: where = "in capture rule in"; break;
1144 case ARGC_ACL: ctx = "ACL keyword"; break;
1145 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001146 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001147 case ARGC_HERR: where = "in http-error directive in"; break;
Miroslav Zagorac7f8314c2020-12-09 16:31:48 +01001148 case ARGC_OT: where = "in ot-scope directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001149 }
1150
1151 /* set a few default settings */
1152 px = p;
1153 pname = p->id;
1154
1155 switch (arg->type) {
1156 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001157 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001158 memprintf(err, "%sparsing [%s:%d]: missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1159 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001160 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001161 cfgerr++;
1162 continue;
1163 }
1164
1165 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001166 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001167
1168 if (sname) {
1169 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001170 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001171
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001172 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001173 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001174 memprintf(err, "%sparsing [%s:%d]: unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1175 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001176 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001177 cfgerr++;
1178 break;
1179 }
1180 }
1181 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001182 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001183
1184 srv = findserver(px, sname);
1185 if (!srv) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001186 memprintf(err, "%sparsing [%s:%d]: unable to find server '%s' in proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1187 *err ? *err : "", cur->file, cur->line, sname, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001188 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001189 cfgerr++;
1190 break;
1191 }
1192
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001193 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001194 arg->unresolved = 0;
1195 arg->data.srv = srv;
1196 break;
1197
1198 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001199 if (arg->data.str.data) {
1200 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001201 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001202 }
1203
1204 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001205 memprintf(err, "%sparsing [%s:%d]: unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1206 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001207 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001208 cfgerr++;
1209 break;
1210 }
1211
1212 if (!(px->cap & PR_CAP_FE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001213 memprintf(err, "%sparsing [%s:%d]: proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1214 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001215 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001216 cfgerr++;
1217 break;
1218 }
1219
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001220 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001221 arg->unresolved = 0;
1222 arg->data.prx = px;
1223 break;
1224
1225 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001226 if (arg->data.str.data) {
1227 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001228 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001229 }
1230
1231 if (!px) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001232 memprintf(err, "%sparsing [%s:%d]: unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1233 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001234 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001235 cfgerr++;
1236 break;
1237 }
1238
1239 if (!(px->cap & PR_CAP_BE)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001240 memprintf(err, "%sparsing [%s:%d]: proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1241 *err ? *err : "", cur->file, cur->line, pname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001242 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001243 cfgerr++;
1244 break;
1245 }
1246
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001247 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001248 arg->unresolved = 0;
1249 arg->data.prx = px;
1250 break;
1251
1252 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001253 if (arg->data.str.data)
1254 stktname = arg->data.str.area;
1255 else
1256 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001257
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001258 t = stktable_find_by_name(stktname);
1259 if (!t) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001260 memprintf(err, "%sparsing [%s:%d]: unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1261 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001262 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001263 cfgerr++;
1264 break;
1265 }
1266
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001267 if (!t->size) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001268 memprintf(err, "%sparsing [%s:%d]: no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1269 *err ? *err : "", cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001270 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001271 cfgerr++;
1272 break;
1273 }
1274
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001275 if (t->proxy && (p->bind_proc & ~t->proxy->bind_proc)) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001276 memprintf(err, "%sparsing [%s:%d]: stick-table '%s' not present on all processes covered by proxy '%s'.\n",
1277 *err ? *err : "", cur->file, cur->line, t->proxy->id, p->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01001278 cfgerr++;
1279 break;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001280 }
1281
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001282 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001283 p->next_stkt_ref = t->proxies_list;
1284 t->proxies_list = p;
1285 }
1286
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001287 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001288 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001289 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001290 break;
1291
1292 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001293 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001294 memprintf(err, "%sparsing [%s:%d]: missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1295 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001296 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001297 cfgerr++;
1298 break;
1299 }
1300
1301 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001302 strcmp(p->uri_auth->userlist->name, arg->data.str.area) == 0)
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001303 ul = p->uri_auth->userlist;
1304 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001305 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001306
1307 if (!ul) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001308 memprintf(err, "%sparsing [%s:%d]: unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1309 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001310 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001311 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001312 cfgerr++;
1313 break;
1314 }
1315
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001316 chunk_destroy(&arg->data.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001317 arg->unresolved = 0;
1318 arg->data.usr = ul;
1319 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001320
1321 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001322 if (!arg->data.str.data) {
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001323 memprintf(err, "%sparsing [%s:%d]: missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1324 *err ? *err : "", cur->file, cur->line,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001325 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001326 cfgerr++;
1327 continue;
1328 }
1329
Willy Tarreau46947782015-01-19 19:00:58 +01001330 rflags = 0;
1331 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001332 err2 = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001333
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001334 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err2))) {
1335 memprintf(err, "%sparsing [%s:%d]: error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1336 *err ? *err : "", cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001337 arg->data.str.area,
Willy Tarreau77e6a4e2021-03-26 16:11:55 +01001338 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err2);
Willy Tarreau46947782015-01-19 19:00:58 +01001339 cfgerr++;
1340 continue;
1341 }
1342
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001343 chunk_destroy(&arg->data.str);
Willy Tarreau46947782015-01-19 19:00:58 +01001344 arg->unresolved = 0;
1345 arg->data.reg = reg;
1346 break;
1347
1348
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001349 }
1350
1351 LIST_DEL(&cur->list);
1352 free(cur);
1353 } /* end of args processing */
1354
1355 return cfgerr;
1356}
1357
1358/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001359 * Process a fetch + format conversion as defined by the sample expression
1360 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001361 * not explicitly set to <smp_type>, but shall be compatible with it as
1362 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1363 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001364 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1365 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1366 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1367 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001368 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1369 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1370 * still hope to get it after waiting longer, and is not converted to string.
1371 * The possible output combinations are the following :
1372 *
1373 * return MAY_CHANGE FINAL Meaning for the sample
1374 * NULL * * Not present and will never be (eg: header)
1375 * smp 0 * Final value converted (eg: header)
1376 * smp 1 0 Not present yet, may appear later (eg: header)
1377 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001378 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001379struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001380 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001381 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001382{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001383 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001384
Willy Tarreau6c616e02014-06-25 16:56:41 +02001385 memset(smp, 0, sizeof(*smp));
1386
Willy Tarreau192252e2015-04-04 01:47:55 +02001387 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001388 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1389 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001390 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001391 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001392
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001393 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001394 return NULL;
1395
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001396 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001397 return NULL;
1398
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001399 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001400 return smp;
1401}
1402
Christopher Faulet476e5d02016-10-26 11:34:47 +02001403static void release_sample_arg(struct arg *p)
1404{
1405 struct arg *p_back = p;
1406
1407 if (!p)
1408 return;
1409
1410 while (p->type != ARGT_STOP) {
1411 if (p->type == ARGT_STR || p->unresolved) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001412 chunk_destroy(&p->data.str);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001413 p->unresolved = 0;
1414 }
1415 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001416 regex_free(p->data.reg);
1417 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001418 }
1419 p++;
1420 }
1421
1422 if (p_back != empty_arg_list)
1423 free(p_back);
1424}
1425
1426void release_sample_expr(struct sample_expr *expr)
1427{
1428 struct sample_conv_expr *conv_expr, *conv_exprb;
1429
1430 if (!expr)
1431 return;
1432
Tim Duesterhus867cd982020-07-04 11:49:39 +02001433 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list) {
1434 LIST_DEL(&conv_expr->list);
Christopher Faulet476e5d02016-10-26 11:34:47 +02001435 release_sample_arg(conv_expr->arg_p);
Tim Duesterhus867cd982020-07-04 11:49:39 +02001436 free(conv_expr);
1437 }
1438
Christopher Faulet476e5d02016-10-26 11:34:47 +02001439 release_sample_arg(expr->arg_p);
1440 free(expr);
1441}
1442
Emeric Brun107ca302010-01-04 16:16:05 +01001443/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001444/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001445/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001446/*****************************************************************/
1447
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001448static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1449{
1450 int i;
1451 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001452 struct buffer *buf;
1453 struct sink *sink;
1454 struct ist line;
1455 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001456
Willy Tarreau0851fd52019-12-17 10:07:25 +01001457 buf = alloc_trash_chunk();
1458 if (!buf)
1459 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001460
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001461 sink = (struct sink *)arg_p[1].data.ptr;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001462 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001463
Willy Tarreau0851fd52019-12-17 10:07:25 +01001464 pfx = arg_p[0].data.str.area;
1465 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001466
Willy Tarreau0851fd52019-12-17 10:07:25 +01001467 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1468 if (!sample_casts[smp->data.type][SMP_T_STR])
1469 goto nocast;
1470
1471 /* Copy sample fetch. This puts the sample as const, the
1472 * cast will copy data if a transformation is required.
1473 */
1474 memcpy(&tmp, smp, sizeof(struct sample));
1475 tmp.flags = SMP_F_CONST;
1476
1477 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1478 goto nocast;
1479
1480 /* Display the displayable chars*. */
1481 b_putchr(buf, '<');
1482 for (i = 0; i < tmp.data.u.str.data; i++) {
Willy Tarreau90807112020-02-25 08:16:33 +01001483 if (isprint((unsigned char)tmp.data.u.str.area[i]))
Willy Tarreau0851fd52019-12-17 10:07:25 +01001484 b_putchr(buf, tmp.data.u.str.area[i]);
1485 else
1486 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001487 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001488 b_putchr(buf, '>');
1489
1490 done:
1491 line = ist2(buf->area, buf->data);
Emeric Brun54648852020-07-06 15:54:06 +02001492 sink_write(sink, &line, 1, 0, 0, NULL);
Willy Tarreau0851fd52019-12-17 10:07:25 +01001493 end:
1494 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001495 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001496 nocast:
1497 chunk_appendf(buf, "(undisplayable)");
1498 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001499}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001500
1501// This function checks the "debug" converter's arguments.
1502static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1503 const char *file, int line, char **err)
1504{
1505 const char *name = "buf0";
1506 struct sink *sink = NULL;
1507
1508 if (args[0].type != ARGT_STR) {
1509 /* optional prefix */
1510 args[0].data.str.area = "";
1511 args[0].data.str.data = 0;
1512 }
1513
1514 if (args[1].type == ARGT_STR)
1515 name = args[1].data.str.area;
1516
1517 sink = sink_find(name);
1518 if (!sink) {
1519 memprintf(err, "No such sink '%s'", name);
1520 return 0;
1521 }
1522
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001523 chunk_destroy(&args[1].data.str);
Christopher Fauletb45bf8e2020-08-07 14:00:23 +02001524 args[1].type = ARGT_PTR;
1525 args[1].data.ptr = sink;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001526 return 1;
1527}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001528
Holger Just1bfc24b2017-05-06 00:56:53 +02001529static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1530{
Willy Tarreau83061a82018-07-13 11:56:34 +02001531 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001532 int bin_len;
1533
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001534 trash->data = 0;
1535 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1536 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001537 if (bin_len < 0)
1538 return 0;
1539
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001540 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001541 smp->data.u.str = *trash;
1542 smp->data.type = SMP_T_BIN;
1543 smp->flags &= ~SMP_F_CONST;
1544 return 1;
1545}
1546
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001547static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001548{
Willy Tarreau83061a82018-07-13 11:56:34 +02001549 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001550 int b64_len;
1551
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001552 trash->data = 0;
1553 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1554 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001555 if (b64_len < 0)
1556 return 0;
1557
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001558 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001559 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001560 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001561 smp->flags &= ~SMP_F_CONST;
1562 return 1;
1563}
1564
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001565static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1566{
1567 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001568 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001569
1570 memset(&ctx, 0, sizeof(ctx));
1571
1572 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001573 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1574 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001575
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001576 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001577 smp->data.u.str = *trash;
1578 smp->data.type = SMP_T_BIN;
1579 smp->flags &= ~SMP_F_CONST;
1580 return 1;
1581}
1582
Tim Duesterhusd4376302019-06-17 12:41:44 +02001583#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01001584static int smp_check_sha2(struct arg *args, struct sample_conv *conv,
1585 const char *file, int line, char **err)
1586{
1587 if (args[0].type == ARGT_STOP)
1588 return 1;
1589 if (args[0].type != ARGT_SINT) {
1590 memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]);
1591 return 0;
1592 }
1593
1594 switch (args[0].data.sint) {
1595 case 224:
1596 case 256:
1597 case 384:
1598 case 512:
1599 /* this is okay */
1600 return 1;
1601 default:
1602 memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint);
1603 return 0;
1604 }
1605}
1606
Tim Duesterhusd4376302019-06-17 12:41:44 +02001607static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
1608{
1609 struct buffer *trash = get_trash_chunk();
1610 int bits = 256;
Christopher Faulete6e7a582021-01-29 11:29:28 +01001611 if (arg_p->data.sint)
Tim Duesterhusd4376302019-06-17 12:41:44 +02001612 bits = arg_p->data.sint;
1613
1614 switch (bits) {
1615 case 224: {
1616 SHA256_CTX ctx;
1617
1618 memset(&ctx, 0, sizeof(ctx));
1619
1620 SHA224_Init(&ctx);
1621 SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1622 SHA224_Final((unsigned char *) trash->area, &ctx);
1623 trash->data = SHA224_DIGEST_LENGTH;
1624 break;
1625 }
1626 case 256: {
1627 SHA256_CTX ctx;
1628
1629 memset(&ctx, 0, sizeof(ctx));
1630
1631 SHA256_Init(&ctx);
1632 SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1633 SHA256_Final((unsigned char *) trash->area, &ctx);
1634 trash->data = SHA256_DIGEST_LENGTH;
1635 break;
1636 }
1637 case 384: {
1638 SHA512_CTX ctx;
1639
1640 memset(&ctx, 0, sizeof(ctx));
1641
1642 SHA384_Init(&ctx);
1643 SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1644 SHA384_Final((unsigned char *) trash->area, &ctx);
1645 trash->data = SHA384_DIGEST_LENGTH;
1646 break;
1647 }
1648 case 512: {
1649 SHA512_CTX ctx;
1650
1651 memset(&ctx, 0, sizeof(ctx));
1652
1653 SHA512_Init(&ctx);
1654 SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1655 SHA512_Final((unsigned char *) trash->area, &ctx);
1656 trash->data = SHA512_DIGEST_LENGTH;
1657 break;
1658 }
1659 default:
1660 return 0;
1661 }
1662
1663 smp->data.u.str = *trash;
1664 smp->data.type = SMP_T_BIN;
1665 smp->flags &= ~SMP_F_CONST;
1666 return 1;
1667}
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001668
Dragan Dosen9e8db132021-02-22 10:03:53 +01001669/* This function returns a sample struct filled with an <arg> content.
1670 * If the <arg> contains a string, it is returned in the sample flagged as
1671 * SMP_F_CONST. If the <arg> contains a variable descriptor, the sample is
1672 * filled with the content of the variable by using vars_get_by_desc().
1673 *
1674 * Keep in mind that the sample content may be written to a pre-allocated
1675 * trash chunk as returned by get_trash_chunk().
1676 *
1677 * This function returns 0 if an error occurs, otherwise it returns 1.
1678 */
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001679static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
1680{
1681 switch (arg->type) {
1682 case ARGT_STR:
1683 smp->data.type = SMP_T_STR;
1684 smp->data.u.str = arg->data.str;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001685 smp->flags = SMP_F_CONST;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001686 return 1;
1687 case ARGT_VAR:
1688 if (!vars_get_by_desc(&arg->data.var, smp))
1689 return 0;
1690 if (!sample_casts[smp->data.type][SMP_T_STR])
1691 return 0;
1692 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
1693 return 0;
1694 return 1;
1695 default:
1696 return 0;
1697 }
1698}
1699
Dragan Dosen9e8db132021-02-22 10:03:53 +01001700/* This function checks an <arg> and fills it with a variable type if the
1701 * <arg> string contains a valid variable name. If failed, the function
1702 * tries to perform a base64 decode operation on the same string, and
1703 * fills the <arg> with the decoded content.
1704 *
1705 * Validation is skipped if the <arg> string is empty.
1706 *
1707 * This function returns 0 if the variable lookup fails and the specified
1708 * <arg> string is not a valid base64 encoded string, as well if
1709 * unexpected argument type is specified or memory allocation error
1710 * occurs. Otherwise it returns 1.
1711 */
1712static inline int sample_check_arg_base64(struct arg *arg, char **err)
1713{
1714 char *dec = NULL;
1715 int dec_size;
1716
1717 if (arg->type != ARGT_STR) {
1718 memprintf(err, "unexpected argument type");
1719 return 0;
1720 }
1721
1722 if (arg->data.str.data == 0) /* empty */
1723 return 1;
1724
1725 if (vars_check_arg(arg, NULL))
1726 return 1;
1727
1728 if (arg->data.str.data % 4) {
1729 memprintf(err, "argument needs to be base64 encoded, and "
1730 "can either be a string or a variable");
1731 return 0;
1732 }
1733
1734 dec_size = (arg->data.str.data / 4 * 3)
1735 - (arg->data.str.area[arg->data.str.data-1] == '=' ? 1 : 0)
1736 - (arg->data.str.area[arg->data.str.data-2] == '=' ? 1 : 0);
1737
1738 if ((dec = malloc(dec_size)) == NULL) {
1739 memprintf(err, "memory allocation error");
1740 return 0;
1741 }
1742
1743 dec_size = base64dec(arg->data.str.area, arg->data.str.data, dec, dec_size);
1744 if (dec_size < 0) {
1745 memprintf(err, "argument needs to be base64 encoded, and "
1746 "can either be a string or a variable");
1747 free(dec);
1748 return 0;
1749 }
1750
1751 /* base64 decoded */
1752 chunk_destroy(&arg->data.str);
1753 arg->data.str.area = dec;
1754 arg->data.str.data = dec_size;
1755 return 1;
1756}
1757
Patrick Gansterer8e366512020-04-22 16:47:57 +02001758#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001759static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
1760 const char *file, int line, char **err)
1761{
1762 switch(args[0].data.sint) {
1763 case 128:
1764 case 192:
1765 case 256:
1766 break;
1767 default:
1768 memprintf(err, "key size must be 128, 192 or 256 (bits).");
1769 return 0;
1770 }
Dragan Dosen9e8db132021-02-22 10:03:53 +01001771
1772 /* Try to decode variables. */
1773 if (!sample_check_arg_base64(&args[1], err)) {
1774 memprintf(err, "failed to parse nonce : %s", *err);
1775 return 0;
1776 }
1777 if (!sample_check_arg_base64(&args[2], err)) {
1778 memprintf(err, "failed to parse key : %s", *err);
1779 return 0;
1780 }
1781 if (!sample_check_arg_base64(&args[3], err)) {
1782 memprintf(err, "failed to parse aead_tag : %s", *err);
1783 return 0;
1784 }
1785
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001786 return 1;
1787}
1788
1789/* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
1790static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
1791{
1792 struct sample nonce, key, aead_tag;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001793 struct buffer *smp_trash = NULL, *smp_trash_alloc = NULL;
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001794 EVP_CIPHER_CTX *ctx;
1795 int dec_size, ret;
1796
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001797 smp_trash_alloc = alloc_trash_chunk();
1798 if (!smp_trash_alloc)
1799 return 0;
1800
Dragan Dosen9e8db132021-02-22 10:03:53 +01001801 /* smp copy */
1802 smp_trash_alloc->data = smp->data.u.str.data;
1803 if (unlikely(smp_trash_alloc->data > smp_trash_alloc->size))
1804 smp_trash_alloc->data = smp_trash_alloc->size;
1805 memcpy(smp_trash_alloc->area, smp->data.u.str.area, smp_trash_alloc->data);
1806
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001807 ctx = EVP_CIPHER_CTX_new();
1808
1809 if (!ctx)
1810 goto err;
1811
Dragan Dosen9e8db132021-02-22 10:03:53 +01001812 smp_trash = alloc_trash_chunk();
1813 if (!smp_trash)
1814 goto err;
1815
1816 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
1817 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001818 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001819
1820 if (arg_p[1].type == ARGT_VAR) {
1821 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
1822 if (dec_size < 0)
1823 goto err;
1824 smp_trash->data = dec_size;
1825 nonce.data.u.str = *smp_trash;
1826 }
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001827
1828 /* Set cipher type and mode */
1829 switch(arg_p[0].data.sint) {
1830 case 128:
1831 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
1832 break;
1833 case 192:
1834 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
1835 break;
1836 case 256:
1837 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
1838 break;
1839 }
1840
Dragan Dosen9e8db132021-02-22 10:03:53 +01001841 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, nonce.data.u.str.data, NULL);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001842
1843 /* Initialise IV */
Dragan Dosen9e8db132021-02-22 10:03:53 +01001844 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) nonce.data.u.str.area))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001845 goto err;
1846
Dragan Dosen9e8db132021-02-22 10:03:53 +01001847 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
1848 if (!sample_conv_var2smp_str(&arg_p[2], &key))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001849 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001850
1851 if (arg_p[2].type == ARGT_VAR) {
1852 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
1853 if (dec_size < 0)
1854 goto err;
1855 smp_trash->data = dec_size;
1856 key.data.u.str = *smp_trash;
1857 }
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001858
1859 /* Initialise key */
Dragan Dosen9e8db132021-02-22 10:03:53 +01001860 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) key.data.u.str.area, NULL))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001861 goto err;
1862
1863 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
Dragan Dosen9e8db132021-02-22 10:03:53 +01001864 (unsigned char *) smp_trash_alloc->area, (int) smp_trash_alloc->data))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001865 goto err;
1866
Dragan Dosen9e8db132021-02-22 10:03:53 +01001867 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
1868 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001869 goto err;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001870
1871 if (arg_p[3].type == ARGT_VAR) {
1872 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
1873 if (dec_size < 0)
1874 goto err;
1875 smp_trash_alloc->data = dec_size;
1876 aead_tag.data.u.str = *smp_trash_alloc;
1877 }
1878
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001879 dec_size = smp_trash->data;
1880
Dragan Dosen9e8db132021-02-22 10:03:53 +01001881 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 +02001882 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
1883
1884 if (ret <= 0)
1885 goto err;
1886
1887 smp->data.u.str.data = dec_size + smp_trash->data;
1888 smp->data.u.str.area = smp_trash->area;
1889 smp->data.type = SMP_T_BIN;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001890 smp_dup(smp);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001891 free_trash_chunk(smp_trash_alloc);
Dragan Dosen9e8db132021-02-22 10:03:53 +01001892 free_trash_chunk(smp_trash);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001893 return 1;
1894
1895err:
1896 free_trash_chunk(smp_trash_alloc);
Dragan Dosen9e8db132021-02-22 10:03:53 +01001897 free_trash_chunk(smp_trash);
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02001898 return 0;
1899}
1900#endif /* HA_OPENSSL_VERSION_NUMBER */
1901
Patrick Gansterer8e366512020-04-22 16:47:57 +02001902static int check_crypto_digest(struct arg *args, struct sample_conv *conv,
1903 const char *file, int line, char **err)
1904{
1905 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1906
1907 if (evp)
1908 return 1;
1909
1910 memprintf(err, "algorithm must be a valid OpenSSL message digest name.");
1911 return 0;
1912}
1913
1914static int sample_conv_crypto_digest(const struct arg *args, struct sample *smp, void *private)
1915{
1916 struct buffer *trash = get_trash_chunk();
1917 unsigned char *md = (unsigned char*) trash->area;
1918 unsigned int md_len = trash->size;
1919 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
1920 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1921
1922 if (!ctx)
1923 return 0;
1924
1925 if (!EVP_DigestInit_ex(ctx, evp, NULL) ||
1926 !EVP_DigestUpdate(ctx, smp->data.u.str.area, smp->data.u.str.data) ||
1927 !EVP_DigestFinal_ex(ctx, md, &md_len)) {
1928 EVP_MD_CTX_free(ctx);
1929 return 0;
1930 }
1931
1932 EVP_MD_CTX_free(ctx);
1933
1934 trash->data = md_len;
1935 smp->data.u.str = *trash;
1936 smp->data.type = SMP_T_BIN;
1937 smp->flags &= ~SMP_F_CONST;
1938 return 1;
1939}
1940
1941static int check_crypto_hmac(struct arg *args, struct sample_conv *conv,
1942 const char *file, int line, char **err)
1943{
1944 if (!check_crypto_digest(args, conv, file, line, err))
1945 return 0;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001946
1947 if (!sample_check_arg_base64(&args[1], err)) {
1948 memprintf(err, "failed to parse key : %s", *err);
1949 return 0;
1950 }
Patrick Gansterer8e366512020-04-22 16:47:57 +02001951
Patrick Gansterer8e366512020-04-22 16:47:57 +02001952 return 1;
1953}
1954
1955static int sample_conv_crypto_hmac(const struct arg *args, struct sample *smp, void *private)
1956{
1957 struct sample key;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001958 struct buffer *trash = NULL, *key_trash = NULL;
Patrick Gansterer8e366512020-04-22 16:47:57 +02001959 unsigned char *md;
1960 unsigned int md_len;
1961 const EVP_MD *evp = EVP_get_digestbyname(args[0].data.str.area);
1962 int dec_size;
1963
1964 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
1965 if (!sample_conv_var2smp_str(&args[1], &key))
1966 return 0;
1967
Dragan Dosen9e8db132021-02-22 10:03:53 +01001968 if (args[1].type == ARGT_VAR) {
1969 key_trash = alloc_trash_chunk();
1970 if (!key_trash)
1971 goto err;
1972
1973 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, key_trash->area, key_trash->size);
1974 if (dec_size < 0)
1975 goto err;
1976 key_trash->data = dec_size;
1977 key.data.u.str = *key_trash;
1978 }
Patrick Gansterer8e366512020-04-22 16:47:57 +02001979
Dragan Dosen9e8db132021-02-22 10:03:53 +01001980 trash = alloc_trash_chunk();
1981 if (!trash)
Patrick Gansterer8e366512020-04-22 16:47:57 +02001982 goto err;
1983
1984 md = (unsigned char*) trash->area;
1985 md_len = trash->size;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001986 if (!HMAC(evp, key.data.u.str.area, key.data.u.str.data, (const unsigned char*) smp->data.u.str.area,
1987 smp->data.u.str.data, md, &md_len))
Patrick Gansterer8e366512020-04-22 16:47:57 +02001988 goto err;
1989
1990 free_trash_chunk(key_trash);
1991
1992 trash->data = md_len;
1993 smp->data.u.str = *trash;
1994 smp->data.type = SMP_T_BIN;
Dragan Dosen9e8db132021-02-22 10:03:53 +01001995 smp_dup(smp);
1996 free_trash_chunk(trash);
Patrick Gansterer8e366512020-04-22 16:47:57 +02001997 return 1;
1998
1999err:
2000 free_trash_chunk(key_trash);
Dragan Dosen9e8db132021-02-22 10:03:53 +01002001 free_trash_chunk(trash);
Patrick Gansterer8e366512020-04-22 16:47:57 +02002002 return 0;
2003}
2004
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02002005#endif /* USE_OPENSSL */
Tim Duesterhusd4376302019-06-17 12:41:44 +02002006
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002007static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002008{
Willy Tarreau83061a82018-07-13 11:56:34 +02002009 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002010 unsigned char c;
2011 int ptr = 0;
2012
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002013 trash->data = 0;
2014 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
2015 c = smp->data.u.str.area[ptr++];
2016 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
2017 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002018 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002019 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002020 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002021 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002022 return 1;
2023}
2024
Dragan Dosen3f957b22017-10-24 09:27:34 +02002025static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
2026{
2027 long long int n = 0;
2028 int i, c;
2029
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002030 for (i = 0; i < smp->data.u.str.data; i++) {
2031 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02002032 return 0;
2033 n = (n << 4) + c;
2034 }
2035
2036 smp->data.u.sint = n;
2037 smp->data.type = SMP_T_SINT;
2038 smp->flags &= ~SMP_F_CONST;
2039 return 1;
2040}
2041
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002042/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002043static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002044{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002045 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
2046 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002047 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002048 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002049 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002050 return 1;
2051}
2052
Willy Tarreau60a2ee72017-12-15 07:13:48 +01002053static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002054{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002055 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01002056 smp->data.u.sint = i;
2057 smp->data.type = SMP_T_SINT;
2058 return 1;
2059}
2060
2061
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002062static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002063{
2064 int i;
2065
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002066 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002067 return 0;
2068
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002069 for (i = 0; i < smp->data.u.str.data; i++) {
2070 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
2071 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01002072 }
2073 return 1;
2074}
2075
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002076static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01002077{
2078 int i;
2079
Willy Tarreauf0645dc2016-08-09 14:29:38 +02002080 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02002081 return 0;
2082
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002083 for (i = 0; i < smp->data.u.str.data; i++) {
2084 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
2085 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01002086 }
2087 return 1;
2088}
2089
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002090/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
2091static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002092{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01002093 /* Attempt to convert to IPv4 to apply the correct mask. */
2094 c_ipv62ip(smp);
2095
2096 if (smp->data.type == SMP_T_IPV4) {
2097 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
2098 smp->data.type = SMP_T_IPV4;
2099 }
2100 else if (smp->data.type == SMP_T_IPV6) {
2101 /* IPv6 cannot be converted without an IPv6 mask. */
2102 if (args[1].type != ARGT_IPV6)
2103 return 0;
2104
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +01002105 write_u64(&smp->data.u.ipv6.s6_addr[0],
2106 read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0]));
2107 write_u64(&smp->data.u.ipv6.s6_addr[8],
2108 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 +01002109 smp->data.type = SMP_T_IPV6;
2110 }
2111
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01002112 return 1;
2113}
2114
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002115/* takes an UINT value on input supposed to represent the time since EPOCH,
2116 * adds an optional offset found in args[1] and emits a string representing
2117 * the local time in the format specified in args[1] using strftime().
2118 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002119static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002120{
Willy Tarreau83061a82018-07-13 11:56:34 +02002121 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002122 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002123 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002124 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002125
2126 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002127 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002128 curr_date += args[1].data.sint;
2129
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002130 tm = localtime(&curr_date);
2131 if (!tm)
2132 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002133 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002134 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
2135 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002136 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002137 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002138 return 1;
2139}
2140
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002141/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002142static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002143{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002144 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
2145 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002146 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002147 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002148 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002149 return 1;
2150}
2151
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002152/* takes an UINT value on input supposed to represent the time since EPOCH,
2153 * adds an optional offset found in args[1] and emits a string representing
2154 * the UTC date in the format specified in args[1] using strftime().
2155 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002156static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002157{
Willy Tarreau83061a82018-07-13 11:56:34 +02002158 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002159 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002160 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002161 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002162
2163 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002164 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002165 curr_date += args[1].data.sint;
2166
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02002167 tm = gmtime(&curr_date);
2168 if (!tm)
2169 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002170 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002171 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
2172 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002173 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002174 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002175 return 1;
2176}
2177
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002178/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002179static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002180{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002181 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
2182 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002183 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002184 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002185 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002186 return 1;
2187}
2188
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002189/* hashes the binary input into a 32-bit unsigned int using xxh.
2190 * The seed of the hash defaults to 0 but can be changd in argument 1.
2191 */
2192static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
2193{
2194 unsigned int seed;
2195
Christopher Faulete6e7a582021-01-29 11:29:28 +01002196 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002197 seed = arg_p->data.sint;
2198 else
2199 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002200 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
2201 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002202 smp->data.type = SMP_T_SINT;
2203 return 1;
2204}
2205
2206/* hashes the binary input into a 64-bit unsigned int using xxh.
2207 * In fact, the function returns a 64 bit unsigned, but the sample
2208 * storage of haproxy only proposes 64-bits signed, so the value is
2209 * cast as signed. This cast doesn't impact the hash repartition.
2210 * The seed of the hash defaults to 0 but can be changd in argument 1.
2211 */
2212static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
2213{
2214 unsigned long long int seed;
2215
Christopher Faulete6e7a582021-01-29 11:29:28 +01002216 if (arg_p->data.sint)
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002217 seed = (unsigned long long int)arg_p->data.sint;
2218 else
2219 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002220 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
2221 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01002222 smp->data.type = SMP_T_SINT;
2223 return 1;
2224}
2225
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002226static int sample_conv_xxh3(const struct arg *arg_p, struct sample *smp, void *private)
2227{
2228 unsigned long long int seed;
2229
Christopher Faulete6e7a582021-01-29 11:29:28 +01002230 if (arg_p->data.sint)
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01002231 seed = (unsigned long long int)arg_p->data.sint;
2232 else
2233 seed = 0;
2234 smp->data.u.sint = (long long int)XXH3(smp->data.u.str.area,
2235 smp->data.u.str.data, seed);
2236 smp->data.type = SMP_T_SINT;
2237 return 1;
2238}
2239
Willy Tarreau80599772015-01-20 19:35:24 +01002240/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002241static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01002242{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002243 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
2244 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002245 if (arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002246 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002247 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01002248 return 1;
2249}
2250
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002251/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
2252static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
2253{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002254 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
2255 smp->data.u.str.data);
Christopher Faulete6e7a582021-01-29 11:29:28 +01002256 if (arg_p->data.sint)
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01002257 smp->data.u.sint = full_hash(smp->data.u.sint);
2258 smp->data.type = SMP_T_SINT;
2259 return 1;
2260}
2261
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002262/* This function escape special json characters. The returned string can be
2263 * safely set between two '"' and used as json string. The json string is
2264 * defined like this:
2265 *
2266 * any Unicode character except '"' or '\' or control character
2267 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
2268 *
2269 * The enum input_type contain all the allowed mode for decoding the input
2270 * string.
2271 */
2272enum input_type {
2273 IT_ASCII = 0,
2274 IT_UTF8,
2275 IT_UTF8S,
2276 IT_UTF8P,
2277 IT_UTF8PS,
2278};
William Dauchy5417e892021-01-08 21:57:41 +01002279
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002280static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
2281 const char *file, int line, char **err)
2282{
Christopher Faulet95917132020-08-05 23:07:07 +02002283 enum input_type type;
2284
Christopher Faulet95917132020-08-05 23:07:07 +02002285 if (strcmp(arg->data.str.area, "") == 0)
2286 type = IT_ASCII;
2287 else if (strcmp(arg->data.str.area, "ascii") == 0)
2288 type = IT_ASCII;
2289 else if (strcmp(arg->data.str.area, "utf8") == 0)
2290 type = IT_UTF8;
2291 else if (strcmp(arg->data.str.area, "utf8s") == 0)
2292 type = IT_UTF8S;
2293 else if (strcmp(arg->data.str.area, "utf8p") == 0)
2294 type = IT_UTF8P;
2295 else if (strcmp(arg->data.str.area, "utf8ps") == 0)
2296 type = IT_UTF8PS;
2297 else {
2298 memprintf(err, "Unexpected input code type. "
2299 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
2300 return 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002301 }
2302
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002303 chunk_destroy(&arg->data.str);
Christopher Faulet95917132020-08-05 23:07:07 +02002304 arg->type = ARGT_SINT;
2305 arg->data.sint = type;
2306 return 1;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002307}
2308
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002309static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002310{
Willy Tarreau83061a82018-07-13 11:56:34 +02002311 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002312 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
2313 const char *str;
2314 int len;
2315 enum input_type input_type = IT_ASCII;
2316 unsigned int c;
2317 unsigned int ret;
2318 char *p;
2319
Christopher Faulete6e7a582021-01-29 11:29:28 +01002320 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002321
2322 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002323 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002324
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002325 p = smp->data.u.str.area;
2326 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002327
2328 if (input_type == IT_ASCII) {
2329 /* Read input as ASCII. */
2330 c = *(unsigned char *)p;
2331 p++;
2332 }
2333 else {
2334 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002335 ret = utf8_next(p,
2336 smp->data.u.str.data - ( p - smp->data.u.str.area),
2337 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002338 p += utf8_return_length(ret);
2339
2340 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2341 return 0;
2342 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2343 continue;
2344 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2345 return 0;
2346 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2347 continue;
2348
2349 /* Check too big values. */
2350 if ((unsigned int)c > 0xffff) {
2351 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2352 return 0;
2353 continue;
2354 }
2355 }
2356
2357 /* Convert character. */
2358 if (c == '"') {
2359 len = 2;
2360 str = "\\\"";
2361 }
2362 else if (c == '\\') {
2363 len = 2;
2364 str = "\\\\";
2365 }
2366 else if (c == '/') {
2367 len = 2;
2368 str = "\\/";
2369 }
2370 else if (c == '\b') {
2371 len = 2;
2372 str = "\\b";
2373 }
2374 else if (c == '\f') {
2375 len = 2;
2376 str = "\\f";
2377 }
2378 else if (c == '\r') {
2379 len = 2;
2380 str = "\\r";
2381 }
2382 else if (c == '\n') {
2383 len = 2;
2384 str = "\\n";
2385 }
2386 else if (c == '\t') {
2387 len = 2;
2388 str = "\\t";
2389 }
Willy Tarreau90807112020-02-25 08:16:33 +01002390 else if (c > 0xff || !isprint((unsigned char)c)) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002391 /* isprint generate a segfault if c is too big. The man says that
2392 * c must have the value of an unsigned char or EOF.
2393 */
2394 len = 6;
2395 _str[0] = '\\';
2396 _str[1] = 'u';
2397 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2398 str = _str;
2399 }
2400 else {
2401 len = 1;
Willy Tarreau5715da22020-02-25 08:37:37 +01002402 _str[0] = c;
2403 str = _str;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002404 }
2405
2406 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002407 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002408 return 0;
2409
2410 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002411 memcpy(temp->area + temp->data, str, len);
2412 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002413 }
2414
2415 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002416 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002417 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002418
2419 return 1;
2420}
2421
Emeric Brun54c4ac82014-11-03 15:32:43 +01002422/* This sample function is designed to extract some bytes from an input buffer.
2423 * First arg is the offset.
2424 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002425static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002426{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002427 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2428 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002429 return 1;
2430 }
2431
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002432 if (smp->data.u.str.size)
2433 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002434 smp->data.u.str.data -= arg_p[0].data.sint;
2435 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002436
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002437 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2438 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002439
2440 return 1;
2441}
2442
Emeric Brunf399b0d2014-11-03 17:07:03 +01002443static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2444 const char *file, int line, char **err)
2445{
2446 struct arg *arg = args;
2447
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002448 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002449 memprintf(err, "Unexpected arg type");
2450 return 0;
2451 }
2452
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002453 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002454 memprintf(err, "Unexpected value 0 for index");
2455 return 0;
2456 }
2457
2458 arg++;
2459
2460 if (arg->type != ARGT_STR) {
2461 memprintf(err, "Unexpected arg type");
2462 return 0;
2463 }
2464
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002465 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002466 memprintf(err, "Empty separators list");
2467 return 0;
2468 }
2469
2470 return 1;
2471}
2472
2473/* This sample function is designed to a return selected part of a string (field).
2474 * First arg is the index of the field (start at 1)
2475 * Second arg is a char list of separators (type string)
2476 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002477static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002478{
Marcin Deranek9631a282018-04-16 14:30:46 +02002479 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002480 char *start, *end;
2481 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002482 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002483
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002484 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002485 return 0;
2486
Marcin Deranek9631a282018-04-16 14:30:46 +02002487 if (arg_p[0].data.sint < 0) {
2488 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002489 end = start = smp->data.u.str.area + smp->data.u.str.data;
2490 while (start > smp->data.u.str.area) {
2491 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2492 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002493 if (field == arg_p[0].data.sint) {
2494 if (count == 1)
2495 goto found;
2496 else if (count > 1)
2497 count--;
2498 } else {
2499 end = start-1;
2500 field--;
2501 }
2502 break;
2503 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002504 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002505 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002506 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002507 } else {
2508 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002509 end = start = smp->data.u.str.area;
2510 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2511 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2512 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002513 if (field == arg_p[0].data.sint) {
2514 if (count == 1)
2515 goto found;
2516 else if (count > 1)
2517 count--;
2518 } else {
2519 start = end+1;
2520 field++;
2521 }
2522 break;
2523 }
2524 }
2525 end++;
2526 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002527 }
2528
2529 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002530 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002531 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002532 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002533 }
2534found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002535 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002536 /* If ret string is len 0, no need to
2537 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002538 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002539 return 1;
2540
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002541 smp->data.u.str.area = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002542
2543 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002544 Note: smp->data.u.str.size cannot be set to 0 */
2545 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002546 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002547
2548 return 1;
2549}
2550
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002551/* This sample function is designed to return a word from a string.
2552 * First arg is the index of the word (start at 1)
2553 * Second arg is a char list of words separators (type string)
2554 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002555static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002556{
Marcin Deranek9631a282018-04-16 14:30:46 +02002557 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002558 char *start, *end;
2559 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002560 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002561
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002562 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002563 return 0;
2564
2565 word = 0;
2566 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002567 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002568 end = start = smp->data.u.str.area + smp->data.u.str.data;
2569 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002570 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002571 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2572 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002573 issep = 1;
2574 break;
2575 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002576 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002577 if (!inword) {
2578 if (!issep) {
2579 if (word != arg_p[0].data.sint) {
2580 word--;
2581 end = start;
2582 }
2583 inword = 1;
2584 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002585 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002586 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002587 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002588 if (count == 1)
2589 goto found;
2590 else if (count > 1)
2591 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002592 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002593 inword = 0;
2594 }
2595 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002596 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002597 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002598 end = start = smp->data.u.str.area;
2599 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002600 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002601 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2602 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002603 issep = 1;
2604 break;
2605 }
2606 }
2607 if (!inword) {
2608 if (!issep) {
2609 if (word != arg_p[0].data.sint) {
2610 word++;
2611 start = end;
2612 }
2613 inword = 1;
2614 }
2615 }
2616 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002617 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002618 if (count == 1)
2619 goto found;
2620 else if (count > 1)
2621 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002622 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002623 inword = 0;
2624 }
2625 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002626 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002627 }
2628
2629 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002630 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002631 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002632 return 1;
2633 }
2634found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002635 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002636 /* If ret string is len 0, no need to
2637 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002638 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002639 return 1;
2640
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002641 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002642
2643 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002644 Note: smp->data.u.str.size cannot be set to 0 */
2645 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002646 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002647
2648 return 1;
2649}
2650
Willy Tarreau7eda8492015-01-20 19:47:06 +01002651static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2652 const char *file, int line, char **err)
2653{
2654 struct arg *arg = args;
2655 char *p;
2656 int len;
2657
2658 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2659 arg[0].type_flags = 0;
2660
2661 if (arg[2].type != ARGT_STR)
2662 return 1;
2663
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002664 p = arg[2].data.str.area;
2665 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002666 while (len) {
2667 if (*p == 'i') {
2668 arg[0].type_flags |= ARGF_REG_ICASE;
2669 }
2670 else if (*p == 'g') {
2671 arg[0].type_flags |= ARGF_REG_GLOB;
2672 }
2673 else {
2674 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2675 return 0;
2676 }
2677 p++;
2678 len--;
2679 }
2680 return 1;
2681}
2682
2683/* This sample function is designed to do the equivalent of s/match/replace/ on
2684 * the input string. It applies a regex and restarts from the last matched
2685 * location until nothing matches anymore. First arg is the regex to apply to
2686 * the input string, second arg is the replacement expression.
2687 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002688static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002689{
2690 char *start, *end;
2691 struct my_regex *reg = arg_p[0].data.reg;
2692 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002693 struct buffer *trash = get_trash_chunk();
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002694 struct buffer *output;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002695 int flag, max;
2696 int found;
2697
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002698 start = smp->data.u.str.area;
2699 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002700
2701 flag = 0;
2702 while (1) {
2703 /* check for last round which is used to copy remaining parts
2704 * when not running in global replacement mode.
2705 */
2706 found = 0;
2707 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2708 /* Note: we can have start == end on empty strings or at the end */
2709 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2710 }
2711
2712 if (!found)
2713 pmatch[0].rm_so = end - start;
2714
2715 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002716 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002717 if (max && pmatch[0].rm_so > 0) {
2718 if (max > pmatch[0].rm_so)
2719 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002720 memcpy(trash->area + trash->data, start, max);
2721 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002722 }
2723
2724 if (!found)
2725 break;
2726
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002727 output = alloc_trash_chunk();
Willy Tarreau23997da2020-02-18 14:27:44 +01002728 if (!output)
2729 break;
2730
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002731 output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2732
Willy Tarreau7eda8492015-01-20 19:47:06 +01002733 /* replace the matching part */
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002734 max = output->size - output->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002735 if (max) {
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002736 if (max > output->data)
2737 max = output->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002738 memcpy(trash->area + trash->data,
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002739 output->area, max);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002740 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002741 }
2742
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002743 free_trash_chunk(output);
2744
Willy Tarreau7eda8492015-01-20 19:47:06 +01002745 /* stop here if we're done with this string */
2746 if (start >= end)
2747 break;
2748
2749 /* We have a special case for matches of length 0 (eg: "x*y*").
2750 * These ones are considered to match in front of a character,
2751 * so we have to copy that character and skip to the next one.
2752 */
2753 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002754 if (trash->data < trash->size)
2755 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002756 pmatch[0].rm_eo++;
2757 }
2758
2759 start += pmatch[0].rm_eo;
2760 flag |= REG_NOTBOL;
2761 }
2762
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002763 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002764 return 1;
2765}
2766
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002767/* This function check an operator entry. It expects a string.
2768 * The string can be an integer or a variable name.
2769 */
2770static int check_operator(struct arg *args, struct sample_conv *conv,
2771 const char *file, int line, char **err)
2772{
2773 const char *str;
2774 const char *end;
Christopher Faulet95917132020-08-05 23:07:07 +02002775 long long int i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002776
2777 /* Try to decode a variable. */
2778 if (vars_check_arg(&args[0], NULL))
2779 return 1;
2780
2781 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002782 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002783 end = str + strlen(str);
Christopher Faulet95917132020-08-05 23:07:07 +02002784 i = read_int64(&str, end);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002785 if (*str != '\0') {
2786 memprintf(err, "expects an integer or a variable name");
2787 return 0;
2788 }
Christopher Faulet95917132020-08-05 23:07:07 +02002789
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002790 chunk_destroy(&args[0].data.str);
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002791 args[0].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02002792 args[0].data.sint = i;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002793 return 1;
2794}
2795
Willy Tarreau6204cd92016-03-10 16:33:04 +01002796/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002797 * If the arg contain an integer, the integer is returned in the
2798 * sample. If the arg contains a variable descriptor, it returns the
2799 * variable value.
2800 *
2801 * This function returns 0 if an error occurs, otherwise it returns 1.
2802 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002803static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002804{
2805 switch (arg->type) {
2806 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002807 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002808 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002809 return 1;
2810 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002811 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002812 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002813 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002814 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002815 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002816 return 0;
2817 return 1;
2818 default:
2819 return 0;
2820 }
2821}
2822
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002823/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002824 * result.
2825 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002826static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002827{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002828 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002829 return 1;
2830}
2831
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002832/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002833 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002834 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002835static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002836{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002837 struct sample tmp;
2838
Willy Tarreau7560dd42016-03-10 16:28:58 +01002839 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002840 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002841 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002842 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002843 return 1;
2844}
2845
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002846/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002847 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002848 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002849static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002850{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002851 struct sample tmp;
2852
Willy Tarreau7560dd42016-03-10 16:28:58 +01002853 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002854 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002855 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002856 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002857 return 1;
2858}
2859
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002860/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002861 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002862 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002863static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002864{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002865 struct sample tmp;
2866
Willy Tarreau7560dd42016-03-10 16:28:58 +01002867 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002868 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002869 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002870 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002871 return 1;
2872}
2873
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002874static inline long long int arith_add(long long int a, long long int b)
2875{
2876 /* Prevent overflow and makes capped calculus.
2877 * We must ensure that the check calculus doesn't
2878 * exceed the signed 64 bits limits.
2879 *
2880 * +----------+----------+
2881 * | a<0 | a>=0 |
2882 * +------+----------+----------+
2883 * | b<0 | MIN-a>b | no check |
2884 * +------+----------+----------+
2885 * | b>=0 | no check | MAX-a<b |
2886 * +------+----------+----------+
2887 */
2888 if ((a ^ b) >= 0) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002889 /* signs are different. */
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002890 if (a < 0) {
2891 if (LLONG_MIN - a > b)
2892 return LLONG_MIN;
2893 }
2894 if (LLONG_MAX - a < b)
2895 return LLONG_MAX;
2896 }
2897 return a + b;
2898}
2899
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002900/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002901 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002902 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002903static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002904{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002905 struct sample tmp;
2906
Willy Tarreau7560dd42016-03-10 16:28:58 +01002907 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002908 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002909 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002910 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002911 return 1;
2912}
2913
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002914/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002915 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002916 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002917static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002918 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002919{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002920 struct sample tmp;
2921
Willy Tarreau7560dd42016-03-10 16:28:58 +01002922 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002923 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002924 return 0;
2925
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002926 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2927 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2928 * of -LLONG_MIN and correct the result.
2929 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002930 if (tmp.data.u.sint == LLONG_MIN) {
2931 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2932 if (smp->data.u.sint < LLONG_MAX)
2933 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002934 return 1;
2935 }
2936
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002937 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002938 * the second operand.
2939 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002940 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002941 return 1;
2942}
2943
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002944/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002945 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002946 * If the result makes an overflow, then the largest possible quantity is
2947 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002948 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002949static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002950 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002951{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002952 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002953 long long int c;
2954
Willy Tarreau7560dd42016-03-10 16:28:58 +01002955 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002956 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002957 return 0;
2958
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002959 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002960 if (!smp->data.u.sint || !tmp.data.u.sint) {
2961 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002962 return 1;
2963 }
2964
2965 /* The multiply between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002966 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002967 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002968 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2969 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002970 return 1;
2971 }
2972
2973 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002974 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002975
2976 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002977 if (smp->data.u.sint != c / tmp.data.u.sint) {
2978 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2979 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002980 return 1;
2981 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002982 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002983 return 1;
2984 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002985 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002986 return 1;
2987}
2988
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002989/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002990 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002991 * If arg_p makes the result overflow, then the largest possible quantity is
2992 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002993 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002994static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002995 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002996{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002997 struct sample tmp;
2998
Willy Tarreau7560dd42016-03-10 16:28:58 +01002999 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01003000 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003001 return 0;
3002
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003003 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003004 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003005 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003006 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003007 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3008 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003009 return 1;
3010 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003011 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003012 return 1;
3013 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003014 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01003015 return 1;
3016}
3017
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003018/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08003019 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003020 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01003021 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003022static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003023 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003024{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003025 struct sample tmp;
3026
Willy Tarreau7560dd42016-03-10 16:28:58 +01003027 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01003028 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003029 return 0;
3030
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003031 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003032 /* The divide between LLONG_MIN and -1 returns a
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003033 * "floating point exception".
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003034 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003035 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
3036 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003037 return 1;
3038 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003039 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003040 return 1;
3041 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003042 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01003043 return 1;
3044}
3045
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003046/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01003047 * result.
3048 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003049static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003050 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003051{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003052 if (smp->data.u.sint == LLONG_MIN)
3053 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02003054 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003055 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01003056 return 1;
3057}
3058
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003059/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003060 * false. The output is a BOOL.
3061 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003062static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003063 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003064{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003065 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003066 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003067 return 1;
3068}
3069
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003070/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01003071 * truee. The output is a BOOL.
3072 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003073static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003074 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003075{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003076 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003077 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003078 return 1;
3079}
3080
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003081/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003082 * The output is a BOOL.
3083 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003084static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003085 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003086{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003087 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003088 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003089 return 1;
3090}
3091
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003092/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01003093 * The output is a BOOL.
3094 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003095static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01003096 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01003097{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003098 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003099 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01003100 return 1;
3101}
3102
Willy Tarreau280f42b2018-02-19 15:34:12 +01003103/* appends an optional const string, an optional variable contents and another
3104 * optional const string to an existing string.
3105 */
3106static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
3107{
Willy Tarreau83061a82018-07-13 11:56:34 +02003108 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003109 struct sample tmp;
3110 int max;
3111
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003112 trash = alloc_trash_chunk();
William Dauchye9970102021-01-11 11:05:58 +01003113 if (!trash)
3114 return 0;
3115
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003116 trash->data = smp->data.u.str.data;
3117 if (trash->data > trash->size - 1)
3118 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003119
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003120 memcpy(trash->area, smp->data.u.str.area, trash->data);
3121 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003122
3123 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003124 max = arg_p[0].data.str.data;
3125 if (max > trash->size - 1 - trash->data)
3126 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003127
3128 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003129 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
3130 trash->data += max;
3131 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003132 }
3133
3134 /* append second string (variable) if it's found and we can turn it
3135 * into a string.
3136 */
3137 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3138 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
3139 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
3140 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
3141
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003142 max = tmp.data.u.str.data;
3143 if (max > trash->size - 1 - trash->data)
3144 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003145
3146 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003147 memcpy(trash->area + trash->data, tmp.data.u.str.area,
3148 max);
3149 trash->data += max;
3150 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003151 }
3152 }
3153
3154 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003155 max = arg_p[2].data.str.data;
3156 if (max > trash->size - 1 - trash->data)
3157 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003158
3159 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003160 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
3161 trash->data += max;
3162 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01003163 }
3164
3165 smp->data.u.str = *trash;
3166 smp->data.type = SMP_T_STR;
Willy Tarreau591fc3a2021-01-08 16:08:43 +01003167 smp_dup(smp);
3168 free_trash_chunk(trash);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003169 return 1;
3170}
3171
3172/* This function checks the "concat" converter's arguments and extracts the
3173 * variable name and its scope.
3174 */
3175static int smp_check_concat(struct arg *args, struct sample_conv *conv,
3176 const char *file, int line, char **err)
3177{
3178 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003179 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
3180 memprintf(err, "failed to register variable name '%s'",
3181 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01003182 return 0;
3183 }
3184 return 1;
3185}
3186
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003187/* Compares string with a variable containing a string. Return value
Tim Duesterhusca097c12018-04-27 21:18:45 +02003188 * is compatible with strcmp(3)'s return value.
3189 */
3190static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
3191{
3192 struct sample tmp;
3193 int max, result;
3194
3195 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3196 if (arg_p[0].type != ARGT_VAR)
3197 return 0;
3198 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
3199 return 0;
3200 if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp))
3201 return 0;
3202
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003203 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
3204 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003205 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003206 if (smp->data.u.str.data != tmp.data.u.str.data) {
3207 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02003208 result = -1;
3209 }
3210 else {
3211 result = 1;
3212 }
3213 }
3214 }
3215
3216 smp->data.u.sint = result;
3217 smp->data.type = SMP_T_SINT;
3218 return 1;
3219}
3220
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003221#ifdef USE_OPENSSL
3222/* Compares bytestring with a variable containing a bytestring. Return value
3223 * is `true` if both bytestrings are bytewise identical and `false` otherwise.
3224 *
3225 * Comparison will be performed in constant time if both bytestrings are of
3226 * the same length. If the lengths differ execution time will not be constant.
3227 */
3228static int sample_conv_secure_memcmp(const struct arg *arg_p, struct sample *smp, void *private)
3229{
3230 struct sample tmp;
3231 int result;
3232
3233 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
3234 if (arg_p[0].type != ARGT_VAR)
3235 return 0;
3236 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
3237 return 0;
3238 if (!sample_casts[tmp.data.type][SMP_T_BIN](&tmp))
3239 return 0;
3240
3241 if (smp->data.u.str.data != tmp.data.u.str.data) {
3242 smp->data.u.sint = 0;
3243 smp->data.type = SMP_T_BOOL;
3244 return 1;
3245 }
3246
3247 /* The following comparison is performed in constant time. */
3248 result = CRYPTO_memcmp(smp->data.u.str.area, tmp.data.u.str.area, smp->data.u.str.data);
3249
3250 smp->data.u.sint = result == 0;
3251 smp->data.type = SMP_T_BOOL;
3252 return 1;
3253}
3254#endif
3255
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02003256/* Takes a boolean as input. Returns the first argument if that boolean is true and
3257 * the second argument otherwise.
3258 */
3259static int sample_conv_iif(const struct arg *arg_p, struct sample *smp, void *private)
3260{
3261 smp->data.type = SMP_T_STR;
3262 smp->flags |= SMP_F_CONST;
3263
3264 if (smp->data.u.sint) {
3265 smp->data.u.str.data = arg_p[0].data.str.data;
3266 smp->data.u.str.area = arg_p[0].data.str.area;
3267 }
3268 else {
3269 smp->data.u.str.data = arg_p[1].data.str.data;
3270 smp->data.u.str.area = arg_p[1].data.str.area;
3271 }
3272
3273 return 1;
3274}
3275
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003276#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
3277#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
3278#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
3279
3280/*
3281 * Extract the field value of an input binary sample. Takes a mandatory argument:
3282 * the protocol buffers field identifier (dotted notation) internally represented
3283 * as an array of unsigned integers and its size.
3284 * Return 1 if the field was found, 0 if not.
3285 */
3286static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
3287{
3288 unsigned char *pos;
3289 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003290
3291 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003292 grpc_left = smp->data.u.str.data;
3293
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01003294 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003295 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003296
3297 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
3298
3299 pos += GRPC_MSG_HEADER_SZ;
3300 grpc_left -= GRPC_MSG_HEADER_SZ;
3301
3302 if (grpc_left < left)
3303 return 0;
3304
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01003305 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
3306 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003307
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003308 grpc_left -= grpc_msg_len;
3309 }
3310
3311 return 0;
3312}
3313
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003314static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
3315{
3316 unsigned char *pos;
3317 size_t left;
3318
3319 pos = (unsigned char *)smp->data.u.str.area;
3320 left = smp->data.u.str.data;
3321
3322 return protobuf_field_lookup(arg_p, smp, &pos, &left);
3323}
3324
3325static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
3326 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003327{
3328 if (!args[1].type) {
3329 args[1].type = ARGT_SINT;
3330 args[1].data.sint = PBUF_T_BINARY;
3331 }
3332 else {
3333 int pbuf_type;
3334
3335 pbuf_type = protobuf_type(args[1].data.str.area);
3336 if (pbuf_type == -1) {
3337 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
3338 return 0;
3339 }
3340
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003341 chunk_destroy(&args[1].data.str);
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01003342 args[1].type = ARGT_SINT;
3343 args[1].data.sint = pbuf_type;
3344 }
3345
3346 return 1;
3347}
3348
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003349/*
3350 * Extract the tag value of an input binary sample. Takes a mandatory argument:
3351 * the FIX protocol tag identifier.
3352 * Return 1 if the tag was found, 0 if not.
3353 */
3354static int sample_conv_fix_tag_value(const struct arg *arg_p, struct sample *smp, void *private)
3355{
3356 struct ist value;
3357
3358 smp->flags &= ~SMP_F_MAY_CHANGE;
3359 value = fix_tag_value(ist2(smp->data.u.str.area, smp->data.u.str.data),
3360 arg_p[0].data.sint);
3361 if (!istlen(value)) {
3362 if (!isttest(value)) {
3363 /* value != IST_NULL, need more data */
3364 smp->flags |= SMP_F_MAY_CHANGE;
3365 }
3366 return 0;
3367 }
3368
3369 smp->data.u.str = ist2buf(value);
3370 smp->flags |= SMP_F_CONST;
3371
3372 return 1;
3373}
3374
3375/* This function checks the "fix_tag_value" converter configuration.
3376 * It expects a "known" (by HAProxy) tag name or ID.
3377 * Tag string names are converted to their ID counterpart because this is the
3378 * format they are sent over the wire.
3379 */
3380static int sample_conv_fix_value_check(struct arg *args, struct sample_conv *conv,
3381 const char *file, int line, char **err)
3382{
3383 struct ist str;
3384 unsigned int tag;
3385
3386 str = ist2(args[0].data.str.area, args[0].data.str.data);
3387 tag = fix_tagid(str);
3388 if (!tag) {
3389 memprintf(err, "Unknown FIX tag name '%s'", args[0].data.str.area);
3390 return 0;
3391 }
3392
3393 chunk_destroy(&args[0].data.str);
3394 args[0].type = ARGT_SINT;
3395 args[0].data.sint = tag;
3396
3397 return 1;
3398}
3399
3400/*
3401 * Checks that a buffer contains a valid FIX message
3402 *
3403 * Return 1 if the check could be run, 0 if not.
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003404 * The result of the analyse itself is stored in <smp> as a boolean
Baptiste Assmanne138dda2020-10-22 15:39:03 +02003405 */
3406static int sample_conv_fix_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3407{
3408 struct ist msg;
3409
3410 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3411
3412 smp->flags &= ~SMP_F_MAY_CHANGE;
3413 switch (fix_validate_message(msg)) {
3414 case FIX_VALID_MESSAGE:
3415 smp->data.type = SMP_T_BOOL;
3416 smp->data.u.sint = 1;
3417 return 1;
3418 case FIX_NEED_MORE_DATA:
3419 smp->flags |= SMP_F_MAY_CHANGE;
3420 return 0;
3421 case FIX_INVALID_MESSAGE:
3422 smp->data.type = SMP_T_BOOL;
3423 smp->data.u.sint = 0;
3424 return 1;
3425 }
3426 return 0;
3427}
3428
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003429/*
3430 * Extract the field value of an input binary sample containing an MQTT packet.
3431 * Takes 2 mandatory arguments:
3432 * - packet type
3433 * - field name
3434 *
3435 * return 1 if the field was found, 0 if not.
3436 */
3437static int sample_conv_mqtt_field_value(const struct arg *arg_p, struct sample *smp, void *private)
3438{
3439 struct ist pkt, value;
3440 int type, fieldname_id;
3441
3442 pkt = ist2(smp->data.u.str.area, smp->data.u.str.data);
3443 type = arg_p[0].data.sint;
3444 fieldname_id = arg_p[1].data.sint;
3445
3446 smp->flags &= ~SMP_F_MAY_CHANGE;
3447 value = mqtt_field_value(pkt, type, fieldname_id);
3448 if (!istlen(value)) {
3449 if (isttest(value)) {
3450 /* value != IST_NULL, need more data */
3451 smp->flags |= SMP_F_MAY_CHANGE;
3452 }
3453 return 0;
3454 }
3455
3456 smp->data.u.str = ist2buf(value);
3457 smp->flags |= SMP_F_CONST;
3458 return 1;
3459}
3460
3461/*
3462 * this function checks the "mqtt_field_value" converter configuration.
3463 * It expects a known packet type name or ID and a field name, in this order
3464 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05003465 * Args[0] will be turned into a MQTT_CPT_* value for direct matching when parsing
Baptiste Assmanne279ca62020-10-27 18:10:06 +01003466 * a packet.
3467 */
3468static int sample_conv_mqtt_field_value_check(struct arg *args, struct sample_conv *conv,
3469 const char *file, int line, char **err)
3470{
3471 int type, fieldname_id;
3472
3473 /* check the MQTT packet type is valid */
3474 type = mqtt_typeid(ist2(args[0].data.str.area, args[0].data.str.data));
3475 if (type == MQTT_CPT_INVALID) {
3476 memprintf(err, "Unknown MQTT type '%s'", args[0].data.str.area);
3477 return 0;
3478 }
3479
3480 /* check the field name belongs to the MQTT packet type */
3481 fieldname_id = mqtt_check_type_fieldname(type, ist2(args[1].data.str.area, args[1].data.str.data));
3482 if (fieldname_id == MQTT_FN_INVALID) {
3483 memprintf(err, "Unknown MQTT field name '%s' for packet type '%s'", args[1].data.str.area,
3484 args[0].data.str.area);
3485 return 0;
3486 }
3487
3488 /* save numeric counterparts of type and field name */
3489 chunk_destroy(&args[0].data.str);
3490 chunk_destroy(&args[1].data.str);
3491 args[0].type = ARGT_SINT;
3492 args[0].data.sint = type;
3493 args[1].type = ARGT_SINT;
3494 args[1].data.sint = fieldname_id;
3495
3496 return 1;
3497}
3498
3499/*
3500 * Checks that <smp> contains a valid MQTT message
3501 *
3502 * The function returns 1 if the check was run to its end, 0 otherwise.
3503 * The result of the analyse itself is stored in <smp> as a boolean.
3504 */
3505static int sample_conv_mqtt_is_valid(const struct arg *arg_p, struct sample *smp, void *private)
3506{
3507 struct ist msg;
3508
3509 msg = ist2(smp->data.u.str.area, smp->data.u.str.data);
3510
3511 smp->flags &= ~SMP_F_MAY_CHANGE;
3512 switch (mqtt_validate_message(msg, NULL)) {
3513 case FIX_VALID_MESSAGE:
3514 smp->data.type = SMP_T_BOOL;
3515 smp->data.u.sint = 1;
3516 return 1;
3517 case FIX_NEED_MORE_DATA:
3518 smp->flags |= SMP_F_MAY_CHANGE;
3519 return 0;
3520 case FIX_INVALID_MESSAGE:
3521 smp->data.type = SMP_T_BOOL;
3522 smp->data.u.sint = 0;
3523 return 1;
3524 }
3525 return 0;
3526}
3527
Tim Duesterhusca097c12018-04-27 21:18:45 +02003528/* This function checks the "strcmp" converter's arguments and extracts the
3529 * variable name and its scope.
3530 */
3531static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
3532 const char *file, int line, char **err)
3533{
3534 /* Try to decode a variable. */
3535 if (vars_check_arg(&args[0], NULL))
3536 return 1;
3537
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003538 memprintf(err, "failed to register variable name '%s'",
3539 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02003540 return 0;
3541}
3542
Tim Duesterhusf38175c2020-06-09 11:48:42 +02003543#ifdef USE_OPENSSL
3544/* This function checks the "secure_memcmp" converter's arguments and extracts the
3545 * variable name and its scope.
3546 */
3547static int smp_check_secure_memcmp(struct arg *args, struct sample_conv *conv,
3548 const char *file, int line, char **err)
3549{
3550 /* Try to decode a variable. */
3551 if (vars_check_arg(&args[0], NULL))
3552 return 1;
3553
3554 memprintf(err, "failed to register variable name '%s'",
3555 args[0].data.str.area);
3556 return 0;
3557}
3558#endif
3559
Christopher Faulet4ccc12f2020-04-01 09:08:32 +02003560/**/
3561static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private)
3562{
3563 struct buffer *tmp;
3564 uint32_t n;
3565
3566 n = htonl((uint32_t)smp->data.u.sint);
3567 tmp = get_trash_chunk();
3568
3569 memcpy(b_head(tmp), &n, 4);
3570 b_add(tmp, 4);
3571
3572 smp->data.u.str = *tmp;
3573 smp->data.type = SMP_T_BIN;
3574 return 1;
3575}
3576
Christopher Fauletea159d62020-04-01 16:21:44 +02003577/**/
3578static int sample_conv_cut_crlf(const struct arg *arg_p, struct sample *smp, void *private)
3579{
3580 char *p;
3581 size_t l;
3582
3583 p = smp->data.u.str.area;
3584 for (l = 0; l < smp->data.u.str.data; l++) {
3585 if (*(p+l) == '\r' || *(p+l) == '\n')
3586 break;
3587 }
3588 smp->data.u.str.data = l;
3589 return 1;
3590}
3591
Christopher Faulet51fc9d12020-04-01 17:24:41 +02003592/**/
3593static int sample_conv_ltrim(const struct arg *arg_p, struct sample *smp, void *private)
3594{
3595 char *delimiters, *p;
3596 size_t dlen, l;
3597
3598 delimiters = arg_p[0].data.str.area;
3599 dlen = arg_p[0].data.str.data;
3600
3601 l = smp->data.u.str.data;
3602 p = smp->data.u.str.area;
3603 while (l && memchr(delimiters, *p, dlen) != NULL) {
3604 p++;
3605 l--;
3606 }
3607
3608 smp->data.u.str.area = p;
3609 smp->data.u.str.data = l;
3610 return 1;
3611}
3612
Christopher Faulet568415a2020-04-01 17:24:47 +02003613/**/
3614static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *private)
3615{
3616 char *delimiters, *p;
3617 size_t dlen, l;
3618
3619 delimiters = arg_p[0].data.str.area;
3620 dlen = arg_p[0].data.str.data;
3621
3622 l = smp->data.u.str.data;
3623 p = smp->data.u.str.area + l - 1;
3624 while (l && memchr(delimiters, *p, dlen) != NULL) {
3625 p--;
3626 l--;
3627 }
3628
3629 smp->data.u.str.data = l;
3630 return 1;
3631}
3632
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003633/************************************************************************/
3634/* All supported sample fetch functions must be declared here */
3635/************************************************************************/
3636
3637/* force TRUE to be returned at the fetch level */
3638static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003639smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003640{
Willy Tarreau280f42b2018-02-19 15:34:12 +01003641 if (!smp_make_rw(smp))
3642 return 0;
3643
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 = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003646 return 1;
3647}
3648
3649/* force FALSE to be returned at the fetch level */
3650static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003651smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003652{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003653 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003654 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003655 return 1;
3656}
3657
3658/* retrieve environment variable $1 as a string */
3659static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003660smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003661{
3662 char *env;
3663
Christopher Faulete6e7a582021-01-29 11:29:28 +01003664 if (args[0].type != ARGT_STR)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003665 return 0;
3666
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003667 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003668 if (!env)
3669 return 0;
3670
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003671 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003672 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003673 smp->data.u.str.area = env;
3674 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003675 return 1;
3676}
3677
Damien Claisseae6f1252019-10-30 15:57:28 +00003678/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3679 * optional string representing the unit of the result: "s" for seconds, "ms" for
3680 * milliseconds and "us" for microseconds.
3681 * Returns 0 on error and non-zero if OK.
3682 */
3683int smp_check_date_unit(struct arg *args, char **err)
3684{
3685 if (args[1].type == ARGT_STR) {
Christopher Faulet95917132020-08-05 23:07:07 +02003686 long long int unit;
3687
Damien Claisseae6f1252019-10-30 15:57:28 +00003688 if (strcmp(args[1].data.str.area, "s") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003689 unit = TIME_UNIT_S;
Damien Claisseae6f1252019-10-30 15:57:28 +00003690 }
3691 else if (strcmp(args[1].data.str.area, "ms") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003692 unit = TIME_UNIT_MS;
Damien Claisseae6f1252019-10-30 15:57:28 +00003693 }
3694 else if (strcmp(args[1].data.str.area, "us") == 0) {
Christopher Faulet95917132020-08-05 23:07:07 +02003695 unit = TIME_UNIT_US;
Damien Claisseae6f1252019-10-30 15:57:28 +00003696 }
3697 else {
3698 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
3699 args[1].data.str.area);
3700 return 0;
3701 }
Christopher Faulet95917132020-08-05 23:07:07 +02003702
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003703 chunk_destroy(&args[1].data.str);
Damien Claisseae6f1252019-10-30 15:57:28 +00003704 args[1].type = ARGT_SINT;
Christopher Faulet95917132020-08-05 23:07:07 +02003705 args[1].data.sint = unit;
Damien Claisseae6f1252019-10-30 15:57:28 +00003706 }
3707 else if (args[1].type != ARGT_STOP) {
3708 memprintf(err, "Unexpected arg type");
3709 return 0;
3710 }
3711
3712 return 1;
3713}
3714
3715/* retrieve the current local date in epoch time, converts it to milliseconds
3716 * or microseconds if asked to in optional args[1] unit param, and applies an
3717 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003718 */
3719static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003720smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003721{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003722 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003723
Damien Claisseae6f1252019-10-30 15:57:28 +00003724 /* report in milliseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003725 if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003726 smp->data.u.sint *= 1000;
3727 smp->data.u.sint += date.tv_usec / 1000;
3728 }
3729 /* report in microseconds */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003730 else if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
Damien Claisseae6f1252019-10-30 15:57:28 +00003731 smp->data.u.sint *= 1000000;
3732 smp->data.u.sint += date.tv_usec;
3733 }
3734
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003735 /* add offset */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003736 if (args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003737 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003738
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003739 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003740 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3741 return 1;
3742}
3743
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003744/* retrieve the current microsecond part of the date */
3745static int
3746smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3747{
3748 smp->data.u.sint = date.tv_usec;
3749 smp->data.type = SMP_T_SINT;
3750 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3751 return 1;
3752}
3753
3754
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003755/* returns the hostname */
3756static int
3757smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3758{
3759 smp->data.type = SMP_T_STR;
3760 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003761 smp->data.u.str.area = hostname;
3762 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003763 return 1;
3764}
3765
Willy Tarreau0f30d262014-11-24 16:02:05 +01003766/* returns the number of processes */
3767static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003768smp_fetch_nbproc(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 = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003772 return 1;
3773}
3774
3775/* returns the number of the current process (between 1 and nbproc */
3776static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003777smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003778{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003779 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003780 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003781 return 1;
3782}
3783
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003784/* returns the number of the current thread (between 1 and nbthread */
3785static int
3786smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3787{
3788 smp->data.type = SMP_T_SINT;
3789 smp->data.u.sint = tid;
3790 return 1;
3791}
3792
Willy Tarreau84310e22014-02-14 11:59:04 +01003793/* generate a random 32-bit integer for whatever purpose, with an optional
3794 * range specified in argument.
3795 */
3796static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003797smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01003798{
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003799 smp->data.u.sint = ha_random32();
Willy Tarreau84310e22014-02-14 11:59:04 +01003800
3801 /* reduce if needed. Don't do a modulo, use all bits! */
Christopher Faulete6e7a582021-01-29 11:29:28 +01003802 if (args[0].type == ARGT_SINT)
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003803 smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32;
Willy Tarreau84310e22014-02-14 11:59:04 +01003804
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003805 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01003806 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3807 return 1;
3808}
3809
Willy Tarreau0f30d262014-11-24 16:02:05 +01003810/* returns true if the current process is stopping */
3811static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003812smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003813{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003814 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003815 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003816 return 1;
3817}
3818
Willy Tarreau70fe9442018-11-22 16:07:39 +01003819/* returns the number of calls of the current stream's process_stream() */
3820static int
3821smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
3822{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003823 if (!smp->strm)
3824 return 0;
3825
Willy Tarreau70fe9442018-11-22 16:07:39 +01003826 smp->data.type = SMP_T_SINT;
3827 smp->data.u.sint = smp->strm->task->calls;
3828 return 1;
3829}
3830
3831/* returns the average number of nanoseconds spent processing the stream per call */
3832static int
3833smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3834{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003835 if (!smp->strm)
3836 return 0;
3837
Willy Tarreau70fe9442018-11-22 16:07:39 +01003838 smp->data.type = SMP_T_SINT;
3839 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
3840 return 1;
3841}
3842
3843/* returns the total number of nanoseconds spent processing the stream */
3844static int
3845smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3846{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003847 if (!smp->strm)
3848 return 0;
3849
Willy Tarreau70fe9442018-11-22 16:07:39 +01003850 smp->data.type = SMP_T_SINT;
3851 smp->data.u.sint = smp->strm->task->cpu_time;
3852 return 1;
3853}
3854
3855/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
3856static int
3857smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3858{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003859 if (!smp->strm)
3860 return 0;
3861
Willy Tarreau70fe9442018-11-22 16:07:39 +01003862 smp->data.type = SMP_T_SINT;
3863 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
3864 return 1;
3865}
3866
3867/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
3868static int
3869smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3870{
Willy Tarreaue0dd2102020-04-29 11:59:02 +02003871 if (!smp->strm)
3872 return 0;
3873
Willy Tarreau70fe9442018-11-22 16:07:39 +01003874 smp->data.type = SMP_T_SINT;
3875 smp->data.u.sint = smp->strm->task->lat_time;
3876 return 1;
3877}
3878
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003879static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
3880{
3881 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003882 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003883 smp->data.u.str.area = args[0].data.str.area;
3884 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003885 return 1;
3886}
3887
3888static int smp_check_const_bool(struct arg *args, char **err)
3889{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003890 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
3891 strcasecmp(args[0].data.str.area, "1") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003892 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003893 args[0].type = ARGT_SINT;
3894 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003895 return 1;
3896 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003897 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
3898 strcasecmp(args[0].data.str.area, "0") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003899 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003900 args[0].type = ARGT_SINT;
3901 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003902 return 1;
3903 }
3904 memprintf(err, "Expects 'true', 'false', '0' or '1'");
3905 return 0;
3906}
3907
3908static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
3909{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003910 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003911 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003912 return 1;
3913}
3914
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003915static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003916{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003917 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003918 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003919 return 1;
3920}
3921
3922static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
3923{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003924 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003925 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003926 return 1;
3927}
3928
3929static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
3930{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003931 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003932 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003933 return 1;
3934}
3935
3936static int smp_check_const_bin(struct arg *args, char **err)
3937{
David Carlier64a16ab2016-04-08 10:37:02 +01003938 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003939 int binstrlen;
3940
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003941 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003942 return 0;
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003943 chunk_destroy(&args[0].data.str);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003944 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003945 args[0].data.str.area = binstr;
3946 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003947 return 1;
3948}
3949
3950static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
3951{
3952 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003953 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003954 smp->data.u.str.area = args[0].data.str.area;
3955 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003956 return 1;
3957}
3958
3959static int smp_check_const_meth(struct arg *args, char **err)
3960{
3961 enum http_meth_t meth;
3962 int i;
3963
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003964 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003965 if (meth != HTTP_METH_OTHER) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02003966 chunk_destroy(&args[0].data.str);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003967 args[0].type = ARGT_SINT;
3968 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003969 } else {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003970 /* Check method avalaibility. A method is a token defined as :
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003971 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
3972 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
3973 * token = 1*tchar
3974 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003975 for (i = 0; i < args[0].data.str.data; i++) {
3976 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003977 memprintf(err, "expects valid method.");
3978 return 0;
3979 }
3980 }
3981 }
3982 return 1;
3983}
3984
3985static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
3986{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003987 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003988 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003989 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003990 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003991 smp->data.u.meth.str.area = "";
3992 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003993 } else {
3994 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003995 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003996 smp->data.u.meth.str.area = args[0].data.str.area;
3997 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003998 }
3999 return 1;
4000}
4001
Luca Schimweg8a694b82019-09-10 15:42:52 +02004002// This function checks the "uuid" sample's arguments.
4003// Function won't get called when no parameter is specified (maybe a bug?)
4004static int smp_check_uuid(struct arg *args, char **err)
4005{
4006 if (!args[0].type) {
4007 args[0].type = ARGT_SINT;
4008 args[0].data.sint = 4;
4009 }
4010 else if (args[0].data.sint != 4) {
4011 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
4012 return 0;
4013 }
4014
4015 return 1;
4016}
4017
4018// Generate a RFC4122 UUID (default is v4 = fully random)
4019static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
4020{
4021 if (args[0].data.sint == 4 || !args[0].type) {
Willy Tarreauee3bcdd2020-03-08 17:48:17 +01004022 ha_generate_uuid(&trash);
Luca Schimweg8a694b82019-09-10 15:42:52 +02004023 smp->data.type = SMP_T_STR;
4024 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
4025 smp->data.u.str = trash;
4026 return 1;
4027 }
4028
4029 // more implementations of other uuid formats possible here
4030 return 0;
4031}
4032
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004033/* Note: must not be declared <const> as its list will be overwritten.
4034 * Note: fetches that may return multiple types must be declared as the lowest
4035 * common denominator, the type that can be casted into all other ones. For
4036 * instance IPv4/IPv6 must be declared IPv4.
4037 */
4038static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau0209c972021-03-26 12:03:11 +01004039 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4040 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_CONST },
4041 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_CONST },
4042 { "date", smp_fetch_date, ARG2(0,SINT,STR), smp_check_date_unit, SMP_T_SINT, SMP_USE_CONST },
4043 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4044 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_CONST },
4045 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_CONST },
4046 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4047 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_CONST },
4048 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_CONST },
Willy Tarreau0f30d262014-11-24 16:02:05 +01004049 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau0209c972021-03-26 12:03:11 +01004050 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_CONST },
Willy Tarreau70fe9442018-11-22 16:07:39 +01004051
4052 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4053 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4054 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4055 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
4056 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004057
Willy Tarreau0209c972021-03-26 12:03:11 +01004058 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_CONST },
4059 { "bool", smp_fetch_const_bool, ARG1(1,STR), smp_check_const_bool, SMP_T_BOOL, SMP_USE_CONST },
4060 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_CONST },
4061 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_CONST },
4062 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_CONST },
4063 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_CONST },
4064 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_CONST },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02004065
Willy Tarreau5b8ad222013-07-25 12:17:57 +02004066 { /* END */ },
4067}};
4068
Willy Tarreau0108d902018-11-25 19:14:37 +01004069INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
4070
Emeric Brun107ca302010-01-04 16:16:05 +01004071/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02004072static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau0851fd52019-12-17 10:07:25 +01004073 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
Holger Just1bfc24b2017-05-06 00:56:53 +02004074 { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN },
Emeric Brun53d1a982014-04-30 18:21:37 +02004075 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02004076 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
4077 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau60a2ee72017-12-15 07:13:48 +01004078 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01004079 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Dragan Dosen3f957b22017-10-24 09:27:34 +02004080 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
Tim Duesterhus1478aa72018-01-25 16:24:51 +01004081 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02004082 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
4083 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004084 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01004085 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004086 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4087 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4088 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Dragan Dosen04bf0cc2020-12-22 21:44:33 +01004089 { "xxh3", sample_conv_xxh3, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER01e09742016-12-26 11:46:11 +01004090 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
4091 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02004092 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02004093 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
Marcin Deranek9631a282018-04-16 14:30:46 +02004094 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
4095 { "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 +01004096 { "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 +02004097 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02004098#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01004099 { "sha2", sample_conv_sha2, ARG1(0, SINT), smp_check_sha2, SMP_T_BIN, SMP_T_BIN },
Patrick Ganstererb399bfb2018-06-17 11:21:11 +02004100#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
4101 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
4102#endif
Patrick Gansterer8e366512020-04-22 16:47:57 +02004103 { "digest", sample_conv_crypto_digest, ARG1(1,STR), check_crypto_digest, SMP_T_BIN, SMP_T_BIN },
4104 { "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 +02004105#endif
Willy Tarreau280f42b2018-02-19 15:34:12 +01004106 { "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 +02004107 { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT },
Tim Duesterhusf38175c2020-06-09 11:48:42 +02004108#ifdef USE_OPENSSL
4109 { "secure_memcmp", sample_conv_secure_memcmp, ARG1(1,STR), smp_check_secure_memcmp, SMP_T_BIN, SMP_T_BOOL },
4110#endif
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01004111
4112 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01004113 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
4114 { "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 +01004115
Baptiste Assmanne138dda2020-10-22 15:39:03 +02004116 /* FIX converters */
4117 { "fix_is_valid", sample_conv_fix_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4118 { "fix_tag_value", sample_conv_fix_tag_value, ARG1(1,STR), sample_conv_fix_value_check, SMP_T_BIN, SMP_T_BIN },
4119
Baptiste Assmanne279ca62020-10-27 18:10:06 +01004120 /* MQTT converters */
4121 { "mqtt_is_valid", sample_conv_mqtt_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL },
4122 { "mqtt_field_value", sample_conv_mqtt_field_value, ARG2(2,STR,STR), sample_conv_mqtt_field_value_check, SMP_T_BIN, SMP_T_STR },
4123
Tim Duesterhus3943e4f2020-09-11 14:25:23 +02004124 { "iif", sample_conv_iif, ARG2(2, STR, STR), NULL, SMP_T_BOOL, SMP_T_STR },
4125
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02004126 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4127 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4128 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4129 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
4130 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4131 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4132 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4133 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
4134 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4135 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4136 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4137 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4138 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
4139 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01004140
Christopher Fauletea159d62020-04-01 16:21:44 +02004141 { "htonl", sample_conv_htonl, 0, NULL, SMP_T_SINT, SMP_T_BIN },
4142 { "cut_crlf", sample_conv_cut_crlf, 0, NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet51fc9d12020-04-01 17:24:41 +02004143 { "ltrim", sample_conv_ltrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Christopher Faulet568415a2020-04-01 17:24:47 +02004144 { "rtrim", sample_conv_rtrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02004145 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01004146}};
4147
Willy Tarreau0108d902018-11-25 19:14:37 +01004148INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);