summaryrefslogtreecommitdiffstats
path: root/contrib/ffaup/ffaup.c
blob: c75ff7bf397c36fa5eb0193111a4ea0da4c90b64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/* ffaup.c
 * This is a function module for URL parsing.
 *
 * File begun on 2021/10/23 by TBertin
 *
 * Copyright 2007-2021 Theo Bertin for Advens
 *
 * This file is part of rsyslog.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *       -or-
 *       see COPYING.ASL20 in the source distribution
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdint.h>
#include <stddef.h>
#ifndef _AIX
#include <typedefs.h>
#endif
#include <sys/types.h>
#include <string.h>
#include <faup/faup.h>
#include <faup/decode.h>
#include <faup/options.h>
#include <faup/output.h>

#include "config.h"
#include "rsyslog.h"
#include "parserif.h"
#include "module-template.h"
#include "rainerscript.h"



MODULE_TYPE_FUNCTION
MODULE_TYPE_NOKEEP
DEF_FMOD_STATIC_DATA

faup_options_t *glbOptions = NULL;

enum _faup_parse_type_t {
	FAUP_PARSE_ALL,
	FAUP_PARSE_SCHEME,
	FAUP_PARSE_CREDENTIAL,
	FAUP_PARSE_SUBDOMAIN,
	FAUP_PARSE_DOMAIN,
	FAUP_PARSE_DOMAIN_WITHOUT_TLD,
	FAUP_PARSE_HOST,
	FAUP_PARSE_TLD,
	FAUP_PARSE_PORT,
	FAUP_PARSE_RESOURCE_PATH,
	FAUP_PARSE_QUERY_STRING,
	FAUP_PARSE_FRAGMENT
};
typedef enum _faup_parse_type_t faup_parse_type_t;


static inline sbool check_param_count_faup(unsigned short nParams) {
	return nParams != 1;
}


static void ATTR_NONNULL()
do_faup_parse(struct cnffunc *__restrict__ const func, struct svar *__restrict__ const ret,
		void *__restrict__ const usrptr, wti_t *__restrict__ const pWti, const faup_parse_type_t parse_type) {
	struct svar srcVal;
	int bMustFree;
	cnfexprEval(func->expr[0], &srcVal, usrptr, pWti);
	char *url = (char*) var2CString(&srcVal, &bMustFree);

	/*
	We use the faup_handler_t struct directly instead of calling faup_init to avoid overhead and useless
	allocations. Using one handler is not possible as the lib is not thread-safe and thread-locale solutions
	still cause some issues.
	If the faup_init function changes significantly in the future, it may cause issue.
	(Validated with faup v1.5 and faup-master fecf768603e713bc903c56c8df0870fae14e3f93)
	*/
	faup_handler_t fh = {0};
	fh.options = glbOptions;

	// default, return 0
	ret->datatype = 'N';
	ret->d.n = 0;

	if(!faup_decode(&fh, url, strlen(url))) {
		parser_errmsg("faup: could not parse the value\n");
		// No returned error code, so the reason doesn't matter
		FINALIZE;
	}

	switch(parse_type) {
		case FAUP_PARSE_ALL:
			ret->datatype = 'J';
			ret->d.json = json_object_new_object();
			json_object_object_add(
				ret->d.json,
				"scheme",
				json_object_new_string_len(
					url + faup_get_scheme_pos(&fh),
					faup_get_scheme_size(&fh)));
			json_object_object_add(
				ret->d.json,
				"credential",
				json_object_new_string_len(
					url + faup_get_credential_pos(&fh),
					faup_get_credential_size(&fh)));
			json_object_object_add(
				ret->d.json,
				"subdomain",
				json_object_new_string_len(
					url + faup_get_subdomain_pos(&fh),
					faup_get_subdomain_size(&fh)));
			json_object_object_add(
				ret->d.json,
				"domain",
				json_object_new_string_len(
					url + faup_get_domain_pos(&fh),
					faup_get_domain_size(&fh)));
			json_object_object_add(
				ret->d.json,
				"domain_without_tld",
				json_object_new_string_len(
					url + faup_get_domain_without_tld_pos(&fh),
					faup_get_domain_without_tld_size(&fh)));
			json_object_object_add(
				ret->d.json,
				"host",
				json_object_new_string_len(
					url + faup_get_host_pos(&fh),
					faup_get_host_size(&fh)));
			json_object_object_add(
				ret->d.json,
				"tld",
				json_object_new_string_len(
					url + faup_get_tld_pos(&fh),
					faup_get_tld_size(&fh)));
			json_object_object_add(
				ret->d.json,
				"port",
				json_object_new_string_len(
					url + faup_get_port_pos(&fh),
					faup_get_port_size(&fh)));
			json_object_object_add(
				ret->d.json,
				"resource_path",
				json_object_new_string_len(
					url + faup_get_resource_path_pos(&fh),
					faup_get_resource_path_size(&fh)));
			json_object_object_add(
				ret->d.json,
				"query_string",
				json_object_new_string_len(
					url + faup_get_query_string_pos(&fh),
					faup_get_query_string_size(&fh)));
			json_object_object_add(
				ret->d.json,
				"fragment",
				json_object_new_string_len(
					url + faup_get_fragment_pos(&fh),
					faup_get_fragment_size(&fh)));
			break;
		case FAUP_PARSE_SCHEME:
			ret->datatype = 'S';
			ret->d.estr = es_newStrFromCStr(
				url + faup_get_scheme_pos(&fh), faup_get_scheme_size(&fh));
			break;
		case FAUP_PARSE_CREDENTIAL:
			ret->datatype = 'S';
			ret->d.estr = es_newStrFromCStr(
				url + faup_get_credential_pos(&fh), faup_get_credential_size(&fh));
			break;
		case FAUP_PARSE_SUBDOMAIN:
			ret->datatype = 'S';
			ret->d.estr = es_newStrFromCStr(
				url + faup_get_subdomain_pos(&fh), faup_get_subdomain_size(&fh));
			break;
		case FAUP_PARSE_DOMAIN:
			ret->datatype = 'S';
			ret->d.estr = es_newStrFromCStr(
				url + faup_get_domain_pos(&fh), faup_get_domain_size(&fh));
			break;
		case FAUP_PARSE_DOMAIN_WITHOUT_TLD:
			ret->datatype = 'S';
			ret->d.estr = es_newStrFromCStr(
				url + faup_get_domain_without_tld_pos(&fh), faup_get_domain_without_tld_size(&fh));
			break;
		case FAUP_PARSE_HOST:
			ret->datatype = 'S';
			ret->d.estr = es_newStrFromCStr(
				url + faup_get_host_pos(&fh), faup_get_host_size(&fh));
			break;
		case FAUP_PARSE_TLD:
			ret->datatype = 'S';
			ret->d.estr = es_newStrFromCStr(
				url + faup_get_tld_pos(&fh), faup_get_tld_size(&fh));
			break;
		case FAUP_PARSE_PORT:
			ret->datatype = 'S';
			ret->d.estr = es_newStrFromCStr(
				url + faup_get_port_pos(&fh), faup_get_port_size(&fh));
			break;
		case FAUP_PARSE_RESOURCE_PATH:
			ret->datatype = 'S';
			ret->d.estr = es_newStrFromCStr(
				url + faup_get_resource_path_pos(&fh), faup_get_resource_path_size(&fh));
			break;
		case FAUP_PARSE_QUERY_STRING:
			ret->datatype = 'S';
			ret->d.estr = es_newStrFromCStr(
				url + faup_get_query_string_pos(&fh), faup_get_query_string_size(&fh));
			break;
		case FAUP_PARSE_FRAGMENT:
			ret->datatype = 'S';
			ret->d.estr = es_newStrFromCStr(
				url + faup_get_fragment_pos(&fh), faup_get_fragment_size(&fh));
			break;
	}

finalize_it:
	if(bMustFree) {
		free(url);
	}
	varFreeMembers(&srcVal);
}


static void ATTR_NONNULL()
do_faup_parse_full(struct cnffunc *__restrict__ const func, struct svar *__restrict__ const ret,
		void *__restrict__ const usrptr, wti_t *__restrict__ const pWti) {
	do_faup_parse(func, ret, usrptr, pWti, FAUP_PARSE_ALL);
}


static void ATTR_NONNULL()
do_faup_parse_scheme(struct cnffunc *__restrict__ const func, struct svar *__restrict__ const ret,
		void *__restrict__ const usrptr, wti_t *__restrict__ const pWti) {
	do_faup_parse(func, ret, usrptr, pWti, FAUP_PARSE_SCHEME);
}


static void ATTR_NONNULL()
do_faup_parse_credential(struct cnffunc *__restrict__ const func, struct svar *__restrict__ const ret,
		void *__restrict__ const usrptr, wti_t *__restrict__ const pWti) {
	do_faup_parse(func, ret, usrptr, pWti, FAUP_PARSE_CREDENTIAL);
}


static void ATTR_NONNULL()
do_faup_parse_subdomain(struct cnffunc *__restrict__ const func, struct svar *__restrict__ const ret,
		void *__restrict__ const usrptr, wti_t *__restrict__ const pWti) {
	do_faup_parse(func, ret, usrptr, pWti, FAUP_PARSE_SUBDOMAIN);
}


static void ATTR_NONNULL()
do_faup_parse_domain(struct cnffunc *__restrict__ const func, struct svar *__restrict__ const ret,
		void *__restrict__ const usrptr, wti_t *__restrict__ const pWti) {
	do_faup_parse(func, ret, usrptr, pWti, FAUP_PARSE_DOMAIN);
}


static void ATTR_NONNULL()
do_faup_parse_domain_without_tld(struct cnffunc *__restrict__ const func, struct svar *__restrict__ const ret,
		void *__restrict__ const usrptr, wti_t *__restrict__ const pWti) {
	do_faup_parse(func, ret, usrptr, pWti, FAUP_PARSE_DOMAIN_WITHOUT_TLD);
}


static void ATTR_NONNULL()
do_faup_parse_host(struct cnffunc *__restrict__ const func, struct svar *__restrict__ const ret,
		void *__restrict__ const usrptr, wti_t *__restrict__ const pWti) {
	do_faup_parse(func, ret, usrptr, pWti, FAUP_PARSE_HOST);
}


static void ATTR_NONNULL()
do_faup_parse_tld(struct cnffunc *__restrict__ const func, struct svar *__restrict__ const ret,
		void *__restrict__ const usrptr, wti_t *__restrict__ const pWti) {
	do_faup_parse(func, ret, usrptr, pWti, FAUP_PARSE_TLD);
}


static void ATTR_NONNULL()
do_faup_parse_port(struct cnffunc *__restrict__ const func, struct svar *__restrict__ const ret,
		void *__restrict__ const usrptr, wti_t *__restrict__ const pWti) {
	do_faup_parse(func, ret, usrptr, pWti, FAUP_PARSE_PORT);
}


static void ATTR_NONNULL()
do_faup_parse_resource_path(struct cnffunc *__restrict__ const func, struct svar *__restrict__ const ret,
		void *__restrict__ const usrptr, wti_t *__restrict__ const pWti) {
	do_faup_parse(func, ret, usrptr, pWti, FAUP_PARSE_RESOURCE_PATH);
}


static void ATTR_NONNULL()
do_faup_parse_query_string(struct cnffunc *__restrict__ const func, struct svar *__restrict__ const ret,
		void *__restrict__ const usrptr, wti_t *__restrict__ const pWti) {
	do_faup_parse(func, ret, usrptr, pWti, FAUP_PARSE_QUERY_STRING);
}


static void ATTR_NONNULL()
do_faup_parse_fragment(struct cnffunc *__restrict__ const func, struct svar *__restrict__ const ret,
		void *__restrict__ const usrptr, wti_t *__restrict__ const pWti) {
	do_faup_parse(func, ret, usrptr, pWti, FAUP_PARSE_FRAGMENT);
}


static rsRetVal ATTR_NONNULL(1)
initFunc_faup_parse(struct cnffunc *const func)
{
	DEFiRet;

	// Rsyslog cannot free the funcdata object,
	// a library-specific freeing function will be used during destruction
	func->destructable_funcdata = 0;
	if(check_param_count_faup(func->nParams)) {
		parser_errmsg("ffaup: ffaup(key) insufficient params.\n");
		ABORT_FINALIZE(RS_RET_INVLD_NBR_ARGUMENTS);
	}

finalize_it:
	RETiRet;
}


static struct scriptFunct functions[] = {
		{"faup", 1, 1, do_faup_parse_full, initFunc_faup_parse, NULL},
		{"faup_scheme", 1, 1, do_faup_parse_scheme, initFunc_faup_parse, NULL},
		{"faup_credential", 1, 1, do_faup_parse_credential, initFunc_faup_parse, NULL},
		{"faup_subdomain", 1, 1, do_faup_parse_subdomain, initFunc_faup_parse, NULL},
		{"faup_domain", 1, 1, do_faup_parse_domain, initFunc_faup_parse, NULL},
		{"faup_domain_without_tld", 1, 1, do_faup_parse_domain_without_tld, initFunc_faup_parse, NULL},
		{"faup_host", 1, 1, do_faup_parse_host, initFunc_faup_parse, NULL},
		{"faup_tld", 1, 1, do_faup_parse_tld, initFunc_faup_parse, NULL},
		{"faup_port", 1, 1, do_faup_parse_port, initFunc_faup_parse, NULL},
		{"faup_resource_path", 1, 1, do_faup_parse_resource_path, initFunc_faup_parse, NULL},
		{"faup_query_string", 1, 1, do_faup_parse_query_string, initFunc_faup_parse, NULL},
		{"faup_fragment", 1, 1, do_faup_parse_fragment, initFunc_faup_parse, NULL},
		{NULL, 0, 0, NULL, NULL, NULL} //last element to check end of array
};

BEGINgetFunctArray
CODESTARTgetFunctArray
	dbgprintf("Faup: ffaup\n");
	*version = 1;
	*functArray = functions;
ENDgetFunctArray


BEGINmodExit
CODESTARTmodExit
	dbgprintf("ffaup: freeing options\n");
	if(glbOptions){
		faup_options_free(glbOptions);
		glbOptions = NULL;
	}
ENDmodExit


BEGINqueryEtryPt
CODESTARTqueryEtryPt
CODEqueryEtryPt_STD_FMOD_QUERIES
ENDqueryEtryPt


BEGINmodInit()
CODESTARTmodInit
	*ipIFVersProvided = CURR_MOD_IF_VERSION; /* we only support the current interface specification */

	dbgprintf("ffaup: initializing options\n");
	glbOptions = faup_options_new();
	if(!glbOptions) {
		parser_errmsg("ffaup: could not initialize options\n");
		ABORT_FINALIZE(RS_RET_FAUP_INIT_OPTIONS_FAILED);
	}
	// Don't generate a string output, and don't load LUA modules
	// This is useful only for the faup executable
	glbOptions->output = FAUP_OUTPUT_NONE;
	glbOptions->exec_modules = FAUP_MODULES_NOEXEC;
CODEmodInit_QueryRegCFSLineHdlr
	dbgprintf("rsyslog ffaup init called, compiled with version %s\n", VERSION);
ENDmodInit