1 /*
2 ** Zabbix
3 ** Copyright (C) 2001-2021 Zabbix SIA
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 **/
19
20 #include "common.h"
21 #include "db.h"
22 #include "log.h"
23 #include "zbxregexp.h"
24 #include "zbxhttp.h"
25
26 #include "httpmacro.h"
27
28 #define REGEXP_PREFIX "regex:"
29 #define REGEXP_PREFIX_SIZE ZBX_CONST_STRLEN(REGEXP_PREFIX)
30
31 /******************************************************************************
32 * *
33 * Function: httpmacro_cmp_func *
34 * *
35 * Purpose: compare two macros by name *
36 * *
37 * Parameters: d1 - [IN] the first macro *
38 * d2 - [IN] the second macro *
39 * *
40 * Return value: <0 - the first macro name is 'less' than second *
41 * 0 - the macro names are equal *
42 * >0 - the first macro name is 'greater' than second *
43 * *
44 * Author: Andris Zeila *
45 * *
46 ******************************************************************************/
httpmacro_cmp_func(const void * d1,const void * d2)47 static int httpmacro_cmp_func(const void *d1, const void *d2)
48 {
49 const zbx_ptr_pair_t *pair1 = (const zbx_ptr_pair_t *)d1;
50 const zbx_ptr_pair_t *pair2 = (const zbx_ptr_pair_t *)d2;
51
52 return strcmp((char *)pair1->first, (char *)pair2->first);
53 }
54
55 /******************************************************************************
56 * *
57 * Function: httpmacro_append_pair *
58 * *
59 * Purpose: appends key/value pair to the http test macro cache. *
60 * If the value format is 'regex:<pattern>', then regular expression *
61 * match is performed against the supplied data value and specified *
62 * pattern. The first captured group is assigned to the macro value. *
63 * *
64 * Parameters: httptest - [IN/OUT] the http test data *
65 * pkey - [IN] a pointer to the macro name (key) data *
66 * nkey - [IN] the macro name (key) size *
67 * pvalue - [IN] a pointer to the macro value data *
68 * nvalue - [IN] the value size *
69 * data - [IN] the data for regexp matching (optional) *
70 * err_str - [OUT] the error message (optional) *
71 * *
72 * Return value: SUCCEED - the key/value pair was added successfully *
73 * FAIL - key/value pair adding to cache failed. *
74 * The failure reason can be either empty key/value, *
75 * wrong key format or failed regular expression *
76 * match. *
77 * *
78 * Author: Andris Zeila *
79 * *
80 ******************************************************************************/
httpmacro_append_pair(zbx_httptest_t * httptest,const char * pkey,size_t nkey,const char * pvalue,size_t nvalue,const char * data,char ** err_str)81 static int httpmacro_append_pair(zbx_httptest_t *httptest, const char *pkey, size_t nkey,
82 const char *pvalue, size_t nvalue, const char *data, char **err_str)
83 {
84 char *value_str = NULL;
85 size_t key_size = 0, key_offset = 0, value_size = 0, value_offset = 0;
86 zbx_ptr_pair_t pair = {NULL, NULL};
87 int index, ret = FAIL;
88
89 zabbix_log(LOG_LEVEL_DEBUG, "In %s() pkey:'%.*s' pvalue:'%.*s'",
90 __func__, (int)nkey, pkey, (int)nvalue, pvalue);
91
92 if (0 == nkey && 0 != nvalue)
93 {
94 zabbix_log(LOG_LEVEL_DEBUG, "%s() missing variable name (only value provided): \"%.*s\"",
95 __func__, (int)nvalue, pvalue);
96
97 if (NULL != err_str && NULL == *err_str)
98 {
99 *err_str = zbx_dsprintf(*err_str, "missing variable name (only value provided):"
100 " \"%.*s\"", (int)nvalue, pvalue);
101 }
102
103 goto out;
104 }
105
106 if ('{' != pkey[0] || '}' != pkey[nkey - 1])
107 {
108 zabbix_log(LOG_LEVEL_DEBUG, "%s() \"%.*s\" not enclosed in {}", __func__, (int)nkey, pkey);
109
110 if (NULL != err_str && NULL == *err_str)
111 *err_str = zbx_dsprintf(*err_str, "\"%.*s\" not enclosed in {}", (int)nkey, pkey);
112
113 goto out;
114 }
115
116 /* get macro value */
117 zbx_strncpy_alloc(&value_str, &value_size, &value_offset, pvalue, nvalue);
118 if (0 == strncmp(REGEXP_PREFIX, value_str, REGEXP_PREFIX_SIZE))
119 {
120 int rc;
121 /* The value contains regexp pattern, retrieve the first captured group or fail. */
122 /* The \@ sequence is a special construct to fail if the pattern matches but does */
123 /* not contain groups to capture. */
124
125 rc = zbx_mregexp_sub(data, value_str + REGEXP_PREFIX_SIZE, "\\@", (char **)&pair.second);
126 zbx_free(value_str);
127
128 if (SUCCEED != rc || NULL == pair.second)
129 {
130 zabbix_log(LOG_LEVEL_DEBUG, "%s() cannot extract the value of \"%.*s\" from response",
131 __func__, (int)nkey, pkey);
132
133 if (NULL != err_str && NULL == *err_str)
134 {
135 *err_str = zbx_dsprintf(*err_str, "cannot extract the value of \"%.*s\""
136 " from response", (int)nkey, pkey);
137 }
138
139 goto out;
140 }
141 }
142 else
143 pair.second = value_str;
144
145 /* get macro name */
146 zbx_strncpy_alloc((char **)&pair.first, &key_size, &key_offset, pkey, nkey);
147
148 /* remove existing macro if necessary */
149 index = zbx_vector_ptr_pair_search(&httptest->macros, pair, httpmacro_cmp_func);
150 if (FAIL != index)
151 {
152 zbx_ptr_pair_t *ppair = &httptest->macros.values[index];
153
154 zbx_free(ppair->first);
155 zbx_free(ppair->second);
156 zbx_vector_ptr_pair_remove_noorder(&httptest->macros, index);
157 }
158 zbx_vector_ptr_pair_append(&httptest->macros, pair);
159
160 zabbix_log(LOG_LEVEL_DEBUG, "append macro '%s'='%s' in cache", (char *)pair.first, (char *)pair.second);
161
162 ret = SUCCEED;
163 out:
164 zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_result_string(ret));
165
166 return ret;
167 }
168
169 /******************************************************************************
170 * *
171 * Function: http_substitute_variables *
172 * *
173 * Purpose: substitute variables in input string with their values from http *
174 * test config *
175 * *
176 * Parameters: httptest - [IN] the http test data *
177 * data - [IN/OUT] string to substitute macros in *
178 * *
179 * Author: Alexei Vladishev, Andris Zeila *
180 * *
181 ******************************************************************************/
http_substitute_variables(const zbx_httptest_t * httptest,char ** data)182 int http_substitute_variables(const zbx_httptest_t *httptest, char **data)
183 {
184 char replace_char, *substitute;
185 size_t left, right, len, offset;
186 int index, ret = SUCCEED;
187 zbx_ptr_pair_t pair = {NULL, NULL};
188
189 zabbix_log(LOG_LEVEL_DEBUG, "In %s() data:'%s'", __func__, *data);
190
191 for (left = 0; '\0' != (*data)[left]; left++)
192 {
193 if ('{' != (*data)[left])
194 continue;
195
196 offset = ('{' == (*data)[left + 1] ? 1 : 0);
197
198 for (right = left + 1; '\0' != (*data)[right] && '}' != (*data)[right]; right++)
199 ;
200
201 if ('}' != (*data)[right])
202 break;
203
204 replace_char = (*data)[right + 1];
205 (*data)[right + 1] = '\0';
206
207 pair.first = *data + left + offset;
208 index = zbx_vector_ptr_pair_search(&httptest->macros, pair, httpmacro_cmp_func);
209
210 (*data)[right + 1] = replace_char;
211
212 if (FAIL == index)
213 continue;
214
215 substitute = (char *)httptest->macros.values[index].second;
216
217 if ('.' == replace_char && 1 == offset)
218 {
219 right += 2;
220 offset = right;
221
222 for (; '\0' != (*data)[right] && '}' != (*data)[right]; right++)
223 ;
224
225 if ('}' != (*data)[right])
226 break;
227
228 len = right - offset;
229
230 if (ZBX_CONST_STRLEN("urlencode()") == len && 0 == strncmp(*data + offset, "urlencode()", len))
231 {
232 /* http_variable_urlencode cannot fail (except for "out of memory") */
233 /* so no check is needed */
234 substitute = NULL;
235 zbx_http_url_encode((char *)httptest->macros.values[index].second, &substitute);
236 }
237 else if (ZBX_CONST_STRLEN("urldecode()") == len &&
238 0 == strncmp(*data + offset, "urldecode()", len))
239 {
240 /* on error substitute will remain unchanged */
241 substitute = NULL;
242 if (FAIL == (ret = zbx_http_url_decode((char *)httptest->macros.values[index].second,
243 &substitute)))
244 {
245 break;
246 }
247 }
248 else
249 continue;
250
251 }
252 else
253 left += offset;
254
255 zbx_replace_string(data, left, &right, substitute);
256 if (substitute != (char *)httptest->macros.values[index].second)
257 zbx_free(substitute);
258
259 left = right;
260 }
261
262 zabbix_log(LOG_LEVEL_DEBUG, "End of %s() data:'%s'", __func__, *data);
263
264 return ret;
265 }
266
267 /******************************************************************************
268 * *
269 * Function: http_process_variables *
270 * *
271 * Purpose: parses http test/step variable string and stores results into *
272 * httptest macro cache. *
273 * The variables are specified as {<key>}=><value> pairs *
274 * If the value format is 'regex:<pattern>', then regular expression *
275 * match is performed against the supplied data value and specified *
276 * pattern. The first captured group is assigned to the macro value. *
277 * *
278 * Parameters: httptest - [IN/OUT] the http test data *
279 * variables - [IN] the variable vector *
280 * data - [IN] the data for variable regexp matching *
281 * (optional). *
282 * err_str - [OUT] the error message (optional) *
283 * *
284 * Return value: SUCCEED - the variables were processed successfully *
285 * FAIL - the variable processing failed (regexp match *
286 * failed). *
287 * *
288 * Author: Andris Zeila *
289 * *
290 ******************************************************************************/
http_process_variables(zbx_httptest_t * httptest,zbx_vector_ptr_pair_t * variables,const char * data,char ** err_str)291 int http_process_variables(zbx_httptest_t *httptest, zbx_vector_ptr_pair_t *variables, const char *data,
292 char **err_str)
293 {
294 char *key, *value;
295 int i, ret = FAIL;
296
297 zabbix_log(LOG_LEVEL_DEBUG, "In %s() %d variables", __func__, variables->values_num);
298
299 for (i = 0; i < variables->values_num; i++)
300 {
301 key = (char *)variables->values[i].first;
302 value = (char *)variables->values[i].second;
303 if (FAIL == httpmacro_append_pair(httptest, key, strlen(key), value, strlen(value), data, err_str))
304 goto out;
305 }
306
307 ret = SUCCEED;
308 out:
309 zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_result_string(ret));
310
311 return ret;
312 }
313