1 /*
2  * Copyright (C) 2001-2003 FhG Fokus
3  *
4  * This file is part of Kamailio, a free SIP server.
5  *
6  * Kamailio is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version
10  *
11  * Kamailio is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 
21 /**
22  * @file
23  * @brief Kamailio core :: modules loading, structures declarations and utilities
24  * @ingroup core
25  * Module: \ref core
26  */
27 
28 
29 #include "sr_module.h"
30 #include "mod_fix.h"
31 #include "dprint.h"
32 #include "error.h"
33 #include "mem/mem.h"
34 #include "core_cmd.h"
35 #include "ut.h"
36 #include "re.h"
37 #include "route_struct.h"
38 #include "flags.h"
39 #include "trim.h"
40 #include "pvapi.h"
41 #include "globals.h"
42 #include "rpc_lookup.h"
43 #include "sr_compat.h"
44 #include "ppcfg.h"
45 #include "async_task.h"
46 #include "shm_init.h"
47 #include "daemonize.h"
48 
49 #include <sys/stat.h>
50 #include <regex.h>
51 #include <dlfcn.h>
52 #include <strings.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <stddef.h> /* for offsetof */
56 
57 
58 struct sr_module* modules=0;
59 
60 /*We need to define this symbol on Solaris becuase libcurl relies on libnspr which looks for this symbol.
61   If it is not defined, dynamic module loading (dlsym) fails */
62 #ifdef __OS_solaris
63 	int nspr_use_zone_allocator = 0;
64 #endif
65 
66 
67 #ifdef STATIC_EXEC
68 	extern struct module_exports exec_exports;
69 #endif
70 #ifdef STATIC_TM
71 	extern struct module_exports tm_exports;
72 #endif
73 
74 #ifdef STATIC_MAXFWD
75 	extern struct module_exports maxfwd_exports;
76 #endif
77 
78 #ifdef STATIC_AUTH
79 	extern struct module_exports auth_exports;
80 #endif
81 
82 #ifdef STATIC_RR
83 	extern struct module_exports rr_exports;
84 #endif
85 
86 #ifdef STATIC_USRLOC
87 	extern struct module_exports usrloc_exports;
88 #endif
89 
90 #ifdef STATIC_SL
91 	extern struct module_exports sl_exports;
92 #endif
93 
94 #ifndef offsetof
95 #warning "use null pointer dereference for offsetof"
96 #define offsetof(st, m) \
97 		((size_t) ( (char *)&((st *)(0))->m - (char *)0 ))
98 #endif
99 
100 int mod_response_cbk_no=0;
101 response_function* mod_response_cbks=0;
102 
103 /* number of usec to wait before initializing a module */
104 static unsigned int modinit_delay = 0;
105 
set_modinit_delay(unsigned int v)106 unsigned int set_modinit_delay(unsigned int v)
107 {
108 	unsigned int r;
109 	r =  modinit_delay;
110 	modinit_delay = v;
111 	return r;
112 }
113 
114 /* shut down phase for instance - kept in shared memory */
115 static int *_ksr_shutdown_phase = NULL;
116 
ksr_shutdown_phase_init(void)117 int ksr_shutdown_phase_init(void)
118 {
119 	if((_ksr_shutdown_phase == NULL) && (shm_initialized())) {
120 		_ksr_shutdown_phase = (int*)shm_mallocxz(sizeof(int));
121 	}
122 	return 0;
123 }
124 /**
125  * return destroy modules phase state
126  */
ksr_shutdown_phase(void)127 int ksr_shutdown_phase(void)
128 {
129 	return (_ksr_shutdown_phase)?(*_ksr_shutdown_phase):0;
130 }
131 
132 /* keep state if server is in destroy modules phase */
133 static int _sr_destroy_modules_phase = 0;
134 
135 /**
136  * return destroy modules phase state
137  */
destroy_modules_phase(void)138 int destroy_modules_phase(void)
139 {
140 	return _sr_destroy_modules_phase;
141 }
142 
143 /**
144  * if bit 1 set, SIP worker processes handle RPC commands as well
145  * if bit 2 set, RPC worker processes handle SIP commands as well
146  */
147 static int child_sip_rpc_mode = 0;
148 
149 #define CHILD_SIP_RPC	1<<0
150 #define CHILD_RPC_SIP	1<<1
151 
set_child_sip_rpc_mode(void)152 void set_child_sip_rpc_mode(void)
153 {
154 	child_sip_rpc_mode |= CHILD_SIP_RPC;
155 }
156 
set_child_rpc_sip_mode(void)157 void set_child_rpc_sip_mode(void)
158 {
159 	child_sip_rpc_mode |= CHILD_RPC_SIP;
160 }
161 
is_rpc_worker(int rank)162 int is_rpc_worker(int rank)
163 {
164 	if(rank==PROC_RPC
165 			|| (rank>PROC_MAIN && (child_sip_rpc_mode&CHILD_SIP_RPC)!=0))
166 		return 1;
167 	return 0;
168 }
169 
is_sip_worker(int rank)170 int is_sip_worker(int rank)
171 {
172 	if(rank>PROC_MAIN
173 			|| ((rank==PROC_RPC || rank==PROC_NOCHLDINIT)
174 					&& (child_sip_rpc_mode&CHILD_RPC_SIP)!=0))
175 		return 1;
176 	return 0;
177 }
178 
179 /* initializes statically built (compiled in) modules*/
register_builtin_modules()180 int register_builtin_modules()
181 {
182 	int ret;
183 
184 	ret=0;
185 #ifdef STATIC_TM
186 	ret=register_module(MODULE_INTERFACE_VER, &tm_exports,"built-in", 0);
187 	if (ret<0) return ret;
188 #endif
189 
190 #ifdef STATIC_EXEC
191 	ret=register_module(MODULE_INTERFACE_VER, &exec_exports,"built-in", 0);
192 	if (ret<0) return ret;
193 #endif
194 
195 #ifdef STATIC_MAXFWD
196 	ret=register_module(MODULE_INTERFACE_VER, &maxfwd_exports, "built-in", 0);
197 	if (ret<0) return ret;
198 #endif
199 
200 #ifdef STATIC_AUTH
201 	ret=register_module(MODULE_INTERFACE_VER, &auth_exports, "built-in", 0);
202 	if (ret<0) return ret;
203 #endif
204 
205 #ifdef STATIC_RR
206 	ret=register_module(MODULE_INTERFACE_VER, &rr_exports, "built-in", 0);
207 	if (ret<0) return ret;
208 #endif
209 
210 #ifdef STATIC_USRLOC
211 	ret=register_module(MODULE_INTERFACE_VER, &usrloc_exports, "built-in", 0);
212 	if (ret<0) return ret;
213 #endif
214 
215 #ifdef STATIC_SL
216 	ret=register_module(MODULE_INTERFACE_VER, &sl_exports, "built-in", 0);
217 	if (ret<0) return ret;
218 #endif
219 
220 	return ret;
221 }
222 
223 
224 /* registers a module,  register_f= module register  functions
225  * returns <0 on error, 0 on success */
register_module(module_exports_t * e,char * path,void * handle)226 static int register_module(module_exports_t* e, char* path, void* handle)
227 {
228 	int ret, i;
229 	struct sr_module* mod;
230 	char defmod[64];
231 	int n = 0;
232 
233 	ret=-1;
234 
235 	/* add module to the list */
236 	if ((mod=pkg_malloc(sizeof(struct sr_module)))==0){
237 		PKG_MEM_ERROR;
238 		ret=E_OUT_OF_MEM;
239 		goto error;
240 	}
241 	memset(mod, 0, sizeof(struct sr_module));
242 	mod->path=path;
243 	mod->handle=handle;
244 
245 	/* copy and convert fields */
246 	mod->exports.name = e->name;
247 
248 	mod->exports.dlflags = e->dlflags;
249 
250 	if(e->cmds) {
251 		for (n=0; e->cmds[n].name; n++);
252 	}
253 	mod->exports.cmds = pkg_malloc(sizeof(ksr_cmd_export_t)*(n+1));
254 	if (mod->exports.cmds==0) {
255 		PKG_MEM_ERROR;
256 		ret=E_OUT_OF_MEM;
257 		goto error;
258 	}
259 	memset(mod->exports.cmds, 0, sizeof(ksr_cmd_export_t)*(n+1));
260 	for (i=0; i < n; i++) {
261 		mod->exports.cmds[i].name = e->cmds[i].name;
262 		mod->exports.cmds[i].function = e->cmds[i].function;
263 		mod->exports.cmds[i].param_no = e->cmds[i].param_no;
264 		mod->exports.cmds[i].fixup = e->cmds[i].fixup;
265 		mod->exports.cmds[i].free_fixup = e->cmds[i].free_fixup;
266 		mod->exports.cmds[i].flags = e->cmds[i].flags;
267 
268 		mod->exports.cmds[i].fixup_flags = 0;
269 		mod->exports.cmds[i].module_exports = mod;
270 		/* fill known free fixups */
271 		if (mod->exports.cmds[i].fixup && mod->exports.cmds[i].free_fixup == 0) {
272 			mod->exports.cmds[i].free_fixup
273 					= get_fixup_free(mod->exports.cmds[i].fixup);
274 		}
275 	}
276 
277 	mod->exports.params = e->params;
278 	mod->exports.rpc_methods = e->rpc_methods;
279 	mod->exports.pv_items = e->pv_items;
280 	mod->exports.response_f = e->response_f;
281 	mod->exports.init_mod_f = e->init_mod_f;
282 	mod->exports.init_child_f = e->init_child_f;
283 	mod->exports.destroy_mod_f = e->destroy_mod_f;
284 
285 	if (mod->exports.pv_items) {
286 		/* register module pseudo-variables for kamailio modules */
287 		LM_DBG("register PV from: %s\n", mod->exports.name);
288 		if (register_pvars_mod(mod->exports.name, mod->exports.pv_items)!=0) {
289 			LM_ERR("failed to register pseudo-variables for module %s (%s)\n",
290 				mod->exports.name, path);
291 			ret = E_UNSPEC;
292 			goto error;
293 		}
294 	}
295 	if (mod->exports.rpc_methods){
296 		/* register rpcs for ser modules */
297 		i=rpc_register_array(mod->exports.rpc_methods);
298 		if (i<0){
299 			LM_ERR("failed to register RPCs for module %s (%s)\n",
300 					mod->exports.name, path);
301 			ret = E_UNSPEC;
302 			goto error;
303 		}else if (i>0){
304 			LM_ERR("%d duplicate RPCs name detected while registering RPCs"
305 					" declared in module %s (%s)\n",
306 					i, mod->exports.name, path);
307 			ret = E_UNSPEC;
308 			goto error;
309 		}
310 		/* i==0 => success */
311 	}
312 
313 	/* add cfg define for each module: MOD_modulename */
314 	if(strlen(mod->exports.name)>=60) {
315 		LM_ERR("too long module name: %s\n", mod->exports.name);
316 		goto error;
317 	}
318 	strcpy(defmod, "MOD_");
319 	strcat(defmod, mod->exports.name);
320 	pp_define_set_type(0);
321 	if(pp_define(strlen(defmod), defmod)<0) {
322 		LM_ERR("unable to set cfg define for module: %s\n",
323 				mod->exports.name);
324 		goto error;
325 	}
326 
327 	/* link module in the list */
328 	mod->next=modules;
329 	modules=mod;
330 	return 0;
331 error:
332 	if (mod)
333 		pkg_free(mod);
334 	return ret;
335 }
336 
version_control(void * handle,char * path)337 static inline int version_control(void *handle, char *path)
338 {
339 	char **m_ver;
340 	char **m_flags;
341 	char* error;
342 
343 	m_ver=(char **)dlsym(handle, "module_version");
344 	if ((error=(char *)dlerror())!=0) {
345 		LM_ERR("no version info in module <%s>: %s\n", path, error);
346 		return 0;
347 	}
348 	m_flags=(char **)dlsym(handle, "module_flags");
349 	if ((error=(char *)dlerror())!=0) {
350 		LM_ERR("no compile flags info in module <%s>: %s\n", path, error);
351 		return 0;
352 	}
353 	if (!m_ver || !(*m_ver)) {
354 		LM_ERR("no version in module <%s>\n", path );
355 		return 0;
356 	}
357 	if (!m_flags || !(*m_flags)) {
358 		LM_ERR("no compile flags in module <%s>\n", path );
359 		return 0;
360 	}
361 
362 	if (strcmp(SER_FULL_VERSION, *m_ver)==0){
363 		if (strcmp(SER_COMPILE_FLAGS, *m_flags)==0)
364 			return 1;
365 		else {
366 			LM_ERR("module compile flags mismatch for %s "
367 						" \ncore: %s \nmodule: %s\n",
368 						path, SER_COMPILE_FLAGS, *m_flags);
369 			return 0;
370 		}
371 	}
372 	LM_ERR("module version mismatch for %s; "
373 		"core: %s; module: %s\n", path, SER_FULL_VERSION, *m_ver );
374 	return 0;
375 }
376 
377 /**
378  * \brief load a sr module
379  *
380  * tries to load the module specified by mod_path.
381  * If mod_path is 'modname' or 'modname.so' then
382  *  \<MODS_DIR\>/\<modname\>.so will be tried and if this fails
383  *  \<MODS_DIR\>/\<modname\>/\<modname\>.so
384  * If mod_path contain a '/' it is assumed to be the
385  * path to the module and tried first. If fails and mod_path is not
386  * absolute path (not starting with '/') then will try:
387  * \<MODS_DIR\>/mod_path
388  * @param mod_path path or module name
389  * @return 0 on success , <0 on error
390  */
load_module(char * mod_path)391 int load_module(char* mod_path)
392 {
393 	void* handle;
394 	char* error;
395 	mod_register_function mr;
396 	module_exports_t* exp;
397 	struct sr_module* t;
398 	struct stat stat_buf;
399 	str modname;
400 	char* mdir;
401 	char* nxt_mdir;
402 	char* path;
403 	int mdir_len;
404 	int len;
405 	int dlflags;
406 	int new_dlflags;
407 	int retries;
408 	int path_type;
409 	str expref;
410 	char exbuf[64];
411 
412 #ifndef RTLD_NOW
413 /* for openbsd */
414 #define RTLD_NOW DL_LAZY
415 #endif
416 	path=mod_path;
417 	path_type = 0;
418 	modname.s = path;
419 	modname.len = strlen(mod_path);
420 	if(modname.len>3 && strcmp(modname.s+modname.len-3, ".so")==0) {
421 		path_type = 1;
422 		modname.len -= 3;
423 	}
424 	if (!strchr(path, '/'))
425 		path_type |= 2;
426 	if((path_type&2) || path[0] != '/') {
427 		/* module name was given, we try to construct the path */
428 		mdir=mods_dir; /* search path */
429 		do{
430 			nxt_mdir=strchr(mdir, ':');
431 			if (nxt_mdir) mdir_len=(int)(nxt_mdir-mdir);
432 			else mdir_len=strlen(mdir);
433 
434 			if(path_type&2) {
435 				/* try path <MODS_DIR>/<modname>.so */
436 				path = (char*)pkg_malloc(mdir_len + 1 /* "/" */ +
437 									modname.len + 3 /* ".so" */ + 1);
438 				if (path==0) {
439 					PKG_MEM_ERROR;
440 					goto error;
441 				}
442 				memcpy(path, mdir, mdir_len);
443 				len = mdir_len;
444 				if (len != 0 && path[len - 1] != '/'){
445 					path[len]='/';
446 					len++;
447 				}
448 				path[len]=0;
449 				strcat(path, modname.s);
450 				if(!(path_type&1))
451 					strcat(path, ".so");
452 
453 				if (stat(path, &stat_buf) == -1) {
454 					LM_DBG("module file not found <%s>\n", path);
455 					pkg_free(path);
456 
457 					/* try path <MODS_DIR>/<modname>/<modname>.so */
458 					path = (char*)pkg_malloc(
459 						mdir_len + 1 /* "/" */ +
460 						modname.len + 1 /* "/" */ +
461 						modname.len + 3 /* ".so" */ + 1);
462 					if (path==0) {
463 						PKG_MEM_ERROR;
464 						goto error;
465 					}
466 					memcpy(path, mdir, mdir_len);
467 					len = mdir_len;
468 					if (len != 0 && path[len - 1] != '/') {
469 						path[len]='/';
470 						len++;
471 					}
472 					path[len]=0;
473 					strncat(path, modname.s, modname.len);
474 					strcat(path, "/");
475 					strcat(path, modname.s);
476 					if(!(path_type&1))
477 						strcat(path, ".so");
478 
479 					if (stat(path, &stat_buf) == -1) {
480 						LM_DBG("module file not found <%s>\n", path);
481 						pkg_free(path);
482 						path=0;
483 					}
484 				}
485 			} else {
486 				/* try mod_path - S compat */
487 				if(path==mod_path) {
488 					if (stat(path, &stat_buf) == -1) {
489 						LM_DBG("module file not found <%s>\n", path);
490 						path=0;
491 					}
492 				}
493 				if(path==0) {
494 					/* try path <MODS_DIR>/mod_path - K compat */
495 					path = (char*)pkg_malloc(mdir_len + 1 /* "/" */ +
496 									strlen(mod_path) + 1);
497 					if (path==0) {
498 						PKG_MEM_ERROR;
499 						goto error;
500 					}
501 					memcpy(path, mdir, mdir_len);
502 					len = mdir_len;
503 					if (len != 0 && path[len - 1] != '/'){
504 						path[len]='/';
505 						len++;
506 					}
507 					path[len]=0;
508 					strcat(path, mod_path);
509 
510 					if (stat(path, &stat_buf) == -1) {
511 						LM_DBG("module file not found <%s>\n", path);
512 						pkg_free(path);
513 						path=0;
514 					}
515 				}
516 			}
517 			mdir=nxt_mdir?nxt_mdir+1:0;
518 		}while(path==0 && mdir);
519 		if (path==0){
520 			LM_ERR("could not find module <%.*s> in <%s>\n",
521 						modname.len, modname.s, mods_dir);
522 			goto error;
523 		}
524 	}
525 	LM_DBG("trying to load <%s>\n", path);
526 
527 	retries=2;
528 	dlflags=RTLD_NOW;
529 reload:
530 	handle=dlopen(path, dlflags); /* resolve all symbols now */
531 	if (handle==0){
532 		LM_ERR("could not open module <%s>: %s\n", path, dlerror());
533 		goto error;
534 	}
535 
536 	for(t=modules;t; t=t->next){
537 		if (t->handle==handle){
538 			LM_WARN("attempting to load the same module twice (%s)\n", path);
539 			goto skip;
540 		}
541 	}
542 	/* version control */
543 	if (!version_control(handle, path)) {
544 		ksr_exit(-1);
545 	}
546 	/* launch register */
547 	mr = (mod_register_function)dlsym(handle, "mod_register");
548 	if (((error =(char*)dlerror())==0) && mr) {
549 		/* no error call it */
550 		new_dlflags=dlflags;
551 		if (mr(path, &new_dlflags, 0, 0)!=0) {
552 			LM_ERR("%s: mod_register failed\n", path);
553 			goto error1;
554 		}
555 		if (new_dlflags!=dlflags && new_dlflags!=0) {
556 			/* we have to reload the module */
557 			dlclose(handle);
558 			dlflags=new_dlflags;
559 			retries--;
560 			if (retries>0) goto reload;
561 			LM_ERR("%s: cannot agree on the dlflags\n", path);
562 			goto error;
563 		}
564 	}
565 	exp = (module_exports_t*)dlsym(handle, "exports");
566 	if(exp==NULL) {
567 		error =(char*)dlerror();
568 		LM_DBG("attempt to lookup exports structure failed - dlerror: %s\n",
569 				(error)?error:"none");
570 		/* 'exports' structure not found, look up for '_modulename_exports' */
571 		mdir = strrchr(mod_path, '/');
572 		if (!mdir) {
573 			expref.s = mod_path;
574 		} else {
575 			expref.s = mdir+1;
576 		}
577 		expref.len = strlen(expref.s);
578 		if(expref.len>3 && strcmp(expref.s+expref.len-3, ".so")==0)
579 			expref.len -= 3;
580 		snprintf(exbuf, 62, "_%.*s_exports", expref.len, expref.s);
581 		LM_DBG("looking up exports with name: %s\n", exbuf);
582 		exp = (module_exports_t*)dlsym(handle, exbuf);
583 		if(exp==NULL || (error =(char*)dlerror())!=0 ){
584 			LM_ERR("failure for exports symbol: %s - dlerror: %s\n",
585 					exbuf, (error)?error:"none");
586 			goto error1;
587 		}
588 	}
589 	/* hack to allow for kamailio style dlflags inside exports */
590 	new_dlflags = exp->dlflags;
591 	if (new_dlflags!=dlflags && new_dlflags!=DEFAULT_DLFLAGS) {
592 		/* we have to reload the module */
593 		dlclose(handle);
594 		DEBUG("%s: exports dlflags interface is deprecated and it will not"
595 				" be supported in newer versions; consider using"
596 				" mod_register() instead\n", path);
597 		dlflags=new_dlflags;
598 		retries--;
599 		if (retries>0) goto reload;
600 		LM_ERR("%s: cannot agree on the dlflags\n", path);
601 		goto error;
602 	}
603 	if (register_module(exp, path, handle)<0) goto error1;
604 	return 0;
605 
606 error1:
607 	dlclose(handle);
608 error:
609 skip:
610 	if (path && path!=mod_path)
611 		pkg_free(path);
612 	return -1;
613 }
614 
615 /**
616  * test if command flags are compatible with route block flags (type)
617  * - decide if the command is allowed to run within a specific route block
618  * - return: 1 if allowed; 0 if not allowed
619  */
sr_cmd_flags_match(int cflags,int rflags)620 static inline int sr_cmd_flags_match(int cflags, int rflags)
621 {
622 	if((cflags & rflags) == rflags) {
623 		return 1;
624 	}
625 	if((rflags==EVENT_ROUTE) && (cflags & EVENT_ROUTE)) {
626 		return 1;
627 	}
628 	return 0;
629 }
630 
631 /* searches the module list for function name in module mod and returns
632  *  a pointer to the "name" function record union or 0 if not found
633  * mod==0 is a wildcard matching all modules
634  * flags parameter is OR value of all flags that must match
635  */
find_mod_export_record(char * mod,char * name,int param_no,int flags)636 ksr_cmd_export_t* find_mod_export_record(char* mod, char* name,
637 											int param_no, int flags)
638 {
639 	struct sr_module* t;
640 	ksr_cmd_export_t* cmd;
641 
642 	for(t=modules;t;t=t->next){
643 		if (mod!=0 && (strcmp(t->exports.name, mod) !=0))
644 			continue;
645 		if (t->exports.cmds)
646 			for(cmd=&t->exports.cmds[0]; cmd->name; cmd++) {
647 				if((strcmp(name, cmd->name) == 0) &&
648 					((cmd->param_no == param_no) ||
649 					(cmd->param_no==VAR_PARAM_NO)) &&
650 					(sr_cmd_flags_match(cmd->flags, flags)==1)
651 				){
652 					LM_DBG("found export of <%s> in module %s [%s]\n",
653 						name, t->exports.name, t->path);
654 					return cmd;
655 				}
656 			}
657 	}
658 	LM_DBG("export of <%s> not found (flags %d)\n", name, flags);
659 	return 0;
660 }
661 
662 
663 
664 /* searches the module list for function name and returns
665  *  a pointer to the "name" function record union or 0 if not found
666  * mod==0 is a wildcard matching all modules
667  * flags parameter is OR value of all flags that must match
668  */
find_export_record(char * name,int param_no,int flags)669 ksr_cmd_export_t* find_export_record(char* name, int param_no, int flags)
670 {
671 	return find_mod_export_record(0, name, param_no, flags);
672 }
673 
674 
675 
find_export(char * name,int param_no,int flags)676 cmd_function find_export(char* name, int param_no, int flags)
677 {
678 	ksr_cmd_export_t* cmd;
679 
680 	cmd = find_export_record(name, param_no, flags);
681 	return cmd?cmd->function:0;
682 }
683 
684 
find_rpc_export(char * name,int flags)685 rpc_export_t* find_rpc_export(char* name, int flags)
686 {
687 	return rpc_lookup((char*)name, strlen(name));
688 }
689 
690 
691 /*
692  * searches the module list and returns pointer to "name" function in module
693  * "mod"
694  * 0 if not found
695  * flags parameter is OR value of all flags that must match
696  */
find_mod_export(char * mod,char * name,int param_no,int flags)697 cmd_function find_mod_export(char* mod, char* name, int param_no, int flags)
698 {
699 	ksr_cmd_export_t* cmd;
700 
701 	cmd=find_mod_export_record(mod, name, param_no, flags);
702 	if (cmd)
703 		return cmd->function;
704 
705 	LM_DBG("<%s> in module <%s> not found\n", name, mod);
706 	return 0;
707 }
708 
709 
find_module_by_name(char * mod)710 struct sr_module* find_module_by_name(char* mod) {
711 	struct sr_module* t;
712 
713 	for(t = modules; t; t = t->next) {
714 		if (strcmp(mod, t->exports.name) == 0) {
715 			return t;
716 		}
717 	}
718 	LM_DBG("module <%s> not found\n", mod);
719 	return 0;
720 }
721 
get_loaded_modules(void)722 sr_module_t* get_loaded_modules(void) {
723 	return modules;
724 }
725 
726 /*!
727  * \brief Find a parameter with given type
728  * \param mod module
729  * \param name parameter name
730  * \param type_mask parameter mask
731  * \param param_type parameter type
732  * \return parameter address in memory, if there is no such parameter, NULL is returned
733  */
find_param_export(struct sr_module * mod,char * name,modparam_t type_mask,modparam_t * param_type)734 void* find_param_export(struct sr_module* mod, char* name,
735 						modparam_t type_mask, modparam_t *param_type)
736 {
737 	param_export_t* param;
738 
739 	if (!mod)
740 		return 0;
741 	for(param = mod->exports.params ;param && param->name ; param++) {
742 		if ((strcmp(name, param->name) == 0) &&
743 			((param->type & PARAM_TYPE_MASK(type_mask)) != 0)) {
744 			LM_DBG("found <%s> in module %s [%s]\n",
745 				name, mod->exports.name, mod->path);
746 			*param_type = param->type;
747 			return param->param_pointer;
748 		}
749 	}
750 	LM_DBG("parameter <%s> not found in module <%s>\n",
751 			name, mod->exports.name);
752 	return 0;
753 }
754 
755 
destroy_modules()756 void destroy_modules()
757 {
758 	struct sr_module* t, *foo;
759 
760 	_sr_destroy_modules_phase = 1;
761 	if(_ksr_shutdown_phase!=NULL) {
762 		*_ksr_shutdown_phase = 1;
763 	}
764 
765 	LM_DBG("starting modules destroy phase\n");
766 
767 	/* call first destroy function from each module */
768 	t=modules;
769 	while(t) {
770 		foo=t->next;
771 		if (t->exports.destroy_mod_f){
772 			t->exports.destroy_mod_f();
773 		}
774 		t=foo;
775 	}
776 	/* free module exports structures */
777 	t=modules;
778 	while(t) {
779 		foo=t->next;
780 		pkg_free(t);
781 		t=foo;
782 	}
783 	modules=0;
784 	if (mod_response_cbks){
785 		pkg_free(mod_response_cbks);
786 		mod_response_cbks=0;
787 	}
788 }
789 
790 
791 /* recursive module child initialization; (recursion is used to
792  * process the module linear list in the same order in
793  * which modules are loaded in config file
794  */
795 
init_mod_child(struct sr_module * m,int rank)796 static int init_mod_child( struct sr_module* m, int rank )
797 {
798 	if (m) {
799 		/* iterate through the list; if error occurs,
800 		 * propagate it up the stack
801 		 */
802 		if (init_mod_child(m->next, rank)!=0) return -1;
803 		if (m->exports.init_child_f) {
804 			LM_DBG("idx %d rank %d: %s [%s]\n", process_no, rank,
805 					m->exports.name, my_desc());
806 			if (m->exports.init_child_f(rank)<0) {
807 				LM_ERR("error while initializing module %s (%s)"
808 						" (idx: %d rank: %d desc: [%s])\n",
809 						m->exports.name, m->path, process_no, rank, my_desc());
810 				return -1;
811 			} else {
812 				/* module correctly initialized */
813 				return 0;
814 			}
815 		}
816 		/* no init function -- proceed with success */
817 		return 0;
818 	} else {
819 		/* end of list */
820 		return 0;
821 	}
822 }
823 
824 
825 /*
826  * per-child initialization
827  */
init_child(int rank)828 int init_child(int rank)
829 {
830 	int ret;
831 	char* type;
832 
833 	switch(rank) {
834 	case PROC_MAIN:       type = "PROC_MAIN";       break;
835 	case PROC_TIMER:      type = "PROC_TIMER";      break;
836 	case PROC_RPC:        type = "PROC_RPC";        break;
837 	case PROC_TCP_MAIN:   type = "PROC_TCP_MAIN";   break;
838 	case PROC_UNIXSOCK:   type = "PROC_UNIXSOCK";   break;
839 	case PROC_ATTENDANT:  type = "PROC_ATTENDANT";  break;
840 	case PROC_INIT:       type = "PROC_INIT";       break;
841 	case PROC_NOCHLDINIT: type = "PROC_NOCHLDINIT"; break;
842 	case PROC_SIPINIT:    type = "PROC_SIPINIT";    break;
843 	case PROC_SIPRPC:     type = "PROC_SIPRPC";     break;
844 	default:              type = "CHILD";           break;
845 	}
846 	LM_DBG("initializing %s with rank %d\n", type, rank);
847 
848 	if(async_task_child_init(rank)<0)
849 		return -1;
850 
851 	ret = init_mod_child(modules, rank);
852 	if(rank!=PROC_INIT) {
853 		pt[process_no].status = 1;
854 	}
855 	return ret;
856 }
857 
858 
859 
860 /* recursive module initialization; (recursion is used to
861  * process the module linear list in the same order in
862  * which modules are loaded in config file
863 */
864 
init_mod(struct sr_module * m)865 static int init_mod( struct sr_module* m )
866 {
867 	if (m) {
868 		/* iterate through the list; if error occurs,
869 		 * propagate it up the stack
870 		 */
871 		if (init_mod(m->next)!=0) return -1;
872 			if (m->exports.init_mod_f) {
873 				LM_DBG("%s\n", m->exports.name);
874 				if (m->exports.init_mod_f()!=0) {
875 					LM_ERR("Error while initializing module %s (%s)\n",
876 								m->exports.name, m->path);
877 					return -1;
878 				} else {
879 					/* module correctly initialized */
880 					return 0;
881 				}
882 			}
883 			/* no init function -- proceed with success */
884 			return 0;
885 	} else {
886 		/* end of list */
887 		return 0;
888 	}
889 }
890 
891 /*
892  * Initialize all loaded modules, the initialization
893  * is done *AFTER* the configuration file is parsed
894  */
init_modules(void)895 int init_modules(void)
896 {
897 	struct sr_module* t;
898 	int i;
899 
900 	if(async_task_init()<0)
901 		return -1;
902 
903 	i = init_mod(modules);
904 	if(i!=0)
905 		return i;
906 
907 	for(t = modules; t; t = t->next)
908 		if (t->exports.response_f)
909 			mod_response_cbk_no++;
910 	mod_response_cbks=pkg_malloc(mod_response_cbk_no *
911 									sizeof(response_function));
912 	if (mod_response_cbks==0){
913 		PKG_MEM_ERROR;
914 		return -1;
915 	}
916 	for (t=modules, i=0; t && (i<mod_response_cbk_no); t=t->next)
917 		if (t->exports.response_f) {
918 			mod_response_cbks[i]=t->exports.response_f;
919 			i++;
920 		}
921 
922 	return 0;
923 }
924 
925 
fixup_get_param(void ** cur_param,int cur_param_no,int required_param_no)926 action_u_t *fixup_get_param(void **cur_param, int cur_param_no,
927 							int required_param_no)
928 {
929 	action_u_t *a;
930 	/* cur_param points to a->u.string, get pointer to a */
931 	a = (void*) ((char *)cur_param - offsetof(action_u_t, u.string));
932 	return a + required_param_no - cur_param_no;
933 }
934 
fixup_get_param_count(void ** cur_param,int cur_param_no)935 int fixup_get_param_count(void **cur_param, int cur_param_no)
936 {
937 	action_u_t *a;
938 	a = fixup_get_param(cur_param, cur_param_no, 0);
939 	if (a)
940 		return a->u.number;
941 	else
942 		return -1;
943 }
944 
945 
946 
947 /** get a pointer to a parameter internal type.
948  * @param param
949  * @return pointer to the parameter internal type.
950  */
fixup_get_param_ptype(void ** param)951 action_param_type* fixup_get_param_ptype(void** param)
952 {
953 	action_u_t* a;
954 	a = (void*)((char*)param - offsetof(action_u_t, u.string));
955 	return &a->type;
956 }
957 
958 
959 /** get a parameter internal type.
960  * @see fixup_get_param_ptype().
961  * @return paramter internal type.
962  */
fixup_get_param_type(void ** param)963 action_param_type fixup_get_param_type(void** param)
964 {
965 	return *fixup_get_param_ptype(param);
966 }
967 
968 
969 
970 /* fixes flag params (resolves possible named flags)
971  * use PARAM_USE_FUNC|PARAM_STRING as a param. type and create
972  * a wrapper function that does just:
973  * return fix_flag(type, val, "my_module", "my_param", &flag_var)
974  * see also param_func_t.
975  */
fix_flag(modparam_t type,void * val,char * mod_name,char * param_name,int * flag)976 int fix_flag( modparam_t type, void* val,
977 					char* mod_name, char* param_name, int* flag)
978 {
979 	int num;
980 	int err;
981 	int f, len;
982 	char* s;
983 	char *p;
984 
985 	if ((type & PARAM_STRING)==0){
986 		LM_CRIT("%s: fix_flag(%s): bad parameter type\n",
987 					mod_name, param_name);
988 		return -1;
989 	}
990 	s=(char*)val;
991 	len=strlen(s);
992 	f=-1;
993 	/* try to see if it's a number */
994 	num = str2s(s, len, &err);
995 	if (err != 0) {
996 		/* see if it's in the name:<no> format */
997 		p=strchr(s, ':');
998 		if (p){
999 			f= str2s(p+1, strlen(p+1), &err);
1000 			if (err!=0){
1001 				LM_ERR("%s: invalid %s format: \"%s\"",
1002 						mod_name, param_name, s);
1003 				return -1;
1004 			}
1005 			*p=0;
1006 		}
1007 		if ((num=get_flag_no(s, len))<0){
1008 			/* not declared yet, declare it */
1009 			num=register_flag(s, f);
1010 		}
1011 		if (num<0){
1012 			LM_ERR("%s: bad %s %s\n", mod_name, param_name, s);
1013 			return -1;
1014 		} else if ((f>0) && (num!=f)){
1015 			LM_ERR("%s: flag %s already defined"
1016 					" as %d (and not %d), using %s:%d\n",
1017 					mod_name, s, num, f, s, num);
1018 		}
1019 	}
1020 	*flag=num;
1021 	return 0;
1022 }
1023 
1024 /*
1025  * Common function parameter fixups
1026  */
1027 
1028 /** Generic parameter fixup function.
1029  *  Creates a fparam_t structure.
1030  *  @param type  contains allowed parameter types
1031  *  @param param is the parameter that will be fixed-up
1032  *
1033  * @return
1034  *    0 on success,
1035  *    1 if the param doesn't match the specified type
1036  *    <0 on failure
1037  */
fix_param(int type,void ** param)1038 int fix_param(int type, void** param)
1039 {
1040 	fparam_t* p;
1041 	str name, s;
1042 	int num;
1043 	int err;
1044 
1045 	p = (fparam_t*)pkg_malloc(sizeof(fparam_t));
1046 	if (!p) {
1047 		PKG_MEM_ERROR;
1048 		return E_OUT_OF_MEM;
1049 	}
1050 	memset(p, 0, sizeof(fparam_t));
1051 	p->orig = *param;
1052 
1053 	switch(type) {
1054 		case FPARAM_UNSPEC:
1055 			LM_ERR("Invalid type value\n");
1056 			goto error;
1057 		case FPARAM_STRING:
1058 			p->v.asciiz = *param;
1059 			/* no break */
1060 		case FPARAM_STR:
1061 			p->v.str.s = (char*)*param;
1062 			p->v.str.len = strlen(p->v.str.s);
1063 			p->fixed = &p->v;
1064 			break;
1065 		case FPARAM_INT:
1066 			s.s = (char*)*param;
1067 			s.len = strlen(s.s);
1068 			err = str2sint(&s, &num);
1069 			if (err == 0) {
1070 				p->v.i = (int)num;
1071 			} else {
1072 				/* Not a number */
1073 				pkg_free(p);
1074 				return 1;
1075 			}
1076 			p->fixed = (void*)(long)num;
1077 			break;
1078 		case FPARAM_REGEX:
1079 			if ((p->v.regex = pkg_malloc(sizeof(regex_t))) == 0) {
1080 				PKG_MEM_ERROR;
1081 				goto error;
1082 			}
1083 			if (regcomp(p->v.regex, *param,
1084 						REG_EXTENDED|REG_ICASE|REG_NEWLINE)) {
1085 				pkg_free(p->v.regex);
1086 				p->v.regex=0;
1087 				/* not a valid regex */
1088 				goto no_match;
1089 			}
1090 			p->fixed = p->v.regex;
1091 			break;
1092 		case FPARAM_AVP:
1093 			name.s = (char*)*param;
1094 			name.len = strlen(name.s);
1095 			trim(&name);
1096 			if (!name.len || name.s[0] != '$') {
1097 				/* Not an AVP identifier */
1098 				goto no_match;
1099 			}
1100 			name.s++;
1101 			name.len--;
1102 			if (parse_avp_ident(&name, &p->v.avp) < 0) {
1103 				/* invalid avp identifier (=> no match) */
1104 				goto no_match;
1105 			}
1106 			p->fixed = &p->v;
1107 			break;
1108 		case FPARAM_SELECT:
1109 			name.s = (char*)*param;
1110 			name.len = strlen(name.s);
1111 			trim(&name);
1112 			if (!name.len || name.s[0] != '@') {
1113 				/* Not a select identifier */
1114 				goto no_match;
1115 			}
1116 			if (parse_select(&name.s, &p->v.select) < 0) {
1117 				LM_ERR("Error while parsing select identifier\n");
1118 				goto error;
1119 			}
1120 			p->fixed = &p->v;
1121 			break;
1122 		case FPARAM_SUBST:
1123 			s.s = *param;
1124 			s.len = strlen(s.s);
1125 			p->v.subst = subst_parser(&s);
1126 			if (!p->v.subst) {
1127 				LM_ERR("Error while parsing regex substitution\n");
1128 				goto error;
1129 			}
1130 			p->fixed = &p->v;
1131 			break;
1132 		case FPARAM_PVS:
1133 			name.s = (char*)*param;
1134 			name.len = strlen(name.s);
1135 			trim(&name);
1136 			if (!name.len || name.s[0] != '$'){
1137 				/* not a pvs identifier */
1138 				goto no_match;
1139 			}
1140 			p->v.pvs=pkg_malloc(sizeof(pv_spec_t));
1141 			if (p->v.pvs==0){
1142 				PKG_MEM_ERROR;
1143 				goto error;
1144 			}
1145 			if (pv_parse_spec2(&name, p->v.pvs, 1)==0){
1146 				/* not a valid pvs identifier (but it might be an avp) */
1147 				pkg_free(p->v.pvs);
1148 				p->v.pvs=0;
1149 				goto no_match;
1150 			}
1151 			p->fixed = p->v.pvs;
1152 			break;
1153 		case FPARAM_PVE:
1154 			name.s = (char*)*param;
1155 			name.len = strlen(name.s);
1156 			if (pv_parse_format(&name, &p->v.pve)<0){
1157 				LM_ERR("bad PVE format: \"%.*s\"\n", name.len, name.s);
1158 				goto error;
1159 			}
1160 			p->fixed = &p->v;
1161 			break;
1162 	}
1163 
1164 	p->type = type;
1165 	*param = (void*)p;
1166 	return 0;
1167 
1168 no_match:
1169 	pkg_free(p);
1170 	return 1;
1171 error:
1172 	pkg_free(p);
1173 	return E_UNSPEC;
1174 }
1175 
1176 
1177 
1178 /** fparam_t free function.
1179  *  Frees the "content" of a fparam, but not the fparam itself.
1180  *  Note: it doesn't free fp->orig!
1181  *  Assumes pkg_malloc'ed content.
1182  *  @param fp -  fparam to be freed
1183  *
1184  */
fparam_free_contents(fparam_t * fp)1185 void fparam_free_contents(fparam_t* fp)
1186 {
1187 
1188 	if (fp==0)
1189 		return;
1190 	switch(fp->type) {
1191 		case FPARAM_UNSPEC:
1192 		case FPARAM_STRING: /* asciiz string, not str */
1193 		case FPARAM_INT:
1194 		case FPARAM_STR:
1195 			/* nothing to do */
1196 			break;
1197 		case FPARAM_REGEX:
1198 			if (fp->v.regex){
1199 				regfree(fp->v.regex);
1200 				pkg_free(fp->v.regex);
1201 				fp->v.regex=0;
1202 			}
1203 			break;
1204 		case FPARAM_AVP:
1205 			free_avp_name(&fp->v.avp.flags, &fp->v.avp.name);
1206 			break;
1207 		case FPARAM_SELECT:
1208 			if (fp->v.select){
1209 				free_select(fp->v.select);
1210 				fp->v.select=0;
1211 			}
1212 			break;
1213 		case FPARAM_SUBST:
1214 			if (fp->v.subst){
1215 				subst_expr_free(fp->v.subst);
1216 				fp->v.subst=0;
1217 			}
1218 			break;
1219 		case FPARAM_PVS:
1220 			if (fp->v.pvs){
1221 				pv_spec_free(fp->v.pvs);
1222 				fp->v.pvs=0;
1223 			}
1224 			break;
1225 		case FPARAM_PVE:
1226 			if (fp->v.pve){
1227 				pv_elem_free_all(fp->v.pve);
1228 				fp->v.pve=0;
1229 			}
1230 			break;
1231 	}
1232 }
1233 
1234 
1235 /**
1236  * @brief Generic free fixup type function for a fixed fparam
1237  *
1238  * Generic free fixup type function for a fixed fparam. It will free whatever
1239  * was allocated during the initial fparam fixup and restore the original param
1240  * value.
1241  * @param param freed parameters
1242  */
fparam_free_restore(void ** param)1243 void fparam_free_restore(void** param)
1244 {
1245 	fparam_t *fp;
1246 	void *orig;
1247 
1248 	fp = *param;
1249 	orig = fp->orig;
1250 	fp->orig = 0;
1251 	fparam_free_contents(fp);
1252 	pkg_free(fp);
1253 	*param = orig;
1254 }
1255 
1256 
1257 
1258 /** fix a param to one of the given types (mask).
1259  *
1260  * @param types - bitmap of the allowed types (e.g. FPARAM_INT|FPARAM_STR)
1261  * @param param - value/result
1262  * @return - 0 on success, -1 on error, 1 if param doesn't
1263  *           match any of the types
1264  */
fix_param_types(int types,void ** param)1265 int fix_param_types(int types, void** param)
1266 {
1267 	int ret;
1268 	int t;
1269 
1270 	if (fixup_get_param_type(param) == STRING_RVE_ST &&
1271 			(types & (FPARAM_INT|FPARAM_STR|FPARAM_STRING))) {
1272 		/* if called with a RVE already converted to string =>
1273 		 * don't try AVP, PVAR or SELECT (to avoid double
1274 		 * deref., e.g.: $foo="$bar"; f($foo) ) */
1275 		types &= ~ (FPARAM_AVP|FPARAM_PVS|FPARAM_SELECT|FPARAM_PVE);
1276 	}
1277 	for (t=types & ~(types-1); types; types&=(types-1), t=types & ~(types-1)){
1278 		if ((ret=fix_param(t, param))<=0) return ret;
1279 	}
1280 	return E_UNSPEC;
1281 }
1282 
1283 
1284 
1285 /*
1286  * Fixup variable string, the parameter can be
1287  * AVP, SELECT, or ordinary string. AVP and select
1288  * identifiers will be resolved to their values during
1289  * runtime
1290  *
1291  * The parameter value will be converted to fparam structure
1292  * This function returns -1 on an error
1293  */
fixup_var_str_12(void ** param,int param_no)1294 int fixup_var_str_12(void** param, int param_no)
1295 {
1296 	int ret;
1297 	if (fixup_get_param_type(param) != STRING_RVE_ST) {
1298 		/* if called with a RVE already converted to string =>
1299 		 * don't try AVP, PVAR or SELECT (to avoid double
1300 		 * deref., e.g.: $foo="$bar"; f($foo) ) */
1301 		if ((ret = fix_param(FPARAM_PVS, param)) <= 0) return ret;
1302 		if ((ret = fix_param(FPARAM_AVP, param)) <= 0) return ret;
1303 		if ((ret = fix_param(FPARAM_SELECT, param)) <= 0) return ret;
1304 	}
1305 	if ((ret = fix_param(FPARAM_STR, param)) <= 0) return ret;
1306 	LM_ERR("Error while fixing parameter, PV, AVP, SELECT, and str conversions"
1307 			" failed\n");
1308 	return -1;
1309 }
1310 
1311 /* Same as fixup_var_str_12 but applies to the 1st parameter only */
fixup_var_str_1(void ** param,int param_no)1312 int fixup_var_str_1(void** param, int param_no)
1313 {
1314 	if (param_no == 1) return fixup_var_str_12(param, param_no);
1315 	else return 0;
1316 }
1317 
1318 /* Same as fixup_var_str_12 but applies to the 2nd parameter only */
fixup_var_str_2(void ** param,int param_no)1319 int fixup_var_str_2(void** param, int param_no)
1320 {
1321 	if (param_no == 2) return fixup_var_str_12(param, param_no);
1322 	else return 0;
1323 }
1324 
1325 /** fixup variable-pve-only-string.
1326  * The parameter can be a PVE (pv based format string)
1327  * or string.
1328  * non-static PVEs  identifiers will be resolved to
1329  * their values during runtime.
1330  * The parameter value will be converted to fparam structure
1331  * @param  param - double pointer to param, as for normal fixup functions.
1332  * @param  param_no - parameter number, ignored.
1333  * @return -1 on an error, 0 on success.
1334  */
fixup_var_pve_12(void ** param,int param_no)1335 int fixup_var_pve_12(void** param, int param_no)
1336 {
1337 	int ret;
1338 	fparam_t* fp;
1339 	if (fixup_get_param_type(param) != STRING_RVE_ST) {
1340 		/* if called with a RVE already converted to string =>
1341 		 * don't try PVE again (to avoid double
1342 		 * deref., e.g.: $foo="$bar"; f($foo) ) */
1343 		if ((ret = fix_param(FPARAM_PVE, param)) <= 0) {
1344 			if (ret < 0)
1345 				return ret;
1346 			/* check if it resolved to a dynamic or "static" PVE.
1347 			 * If the resulting PVE is static (normal string), discard
1348 			 * it and use the normal string fixup (faster at runtime) */
1349 			fp = (fparam_t*)*param;
1350 			if (fp->v.pve->spec == 0 || fp->v.pve->spec->getf == 0)
1351 				fparam_free_restore(param); /* fallback to STR below */
1352 			else
1353 				return ret; /* dynamic PVE => return */
1354 		}
1355 
1356 	}
1357 	if ((ret = fix_param(FPARAM_STR, param)) <= 0) return ret;
1358 	LM_ERR("Error while fixing parameter - PVE or str conversions failed\n");
1359 	return -1;
1360 }
1361 
1362 
1363 
1364 /** fixup variable-pve-string.
1365  * The parameter can be a PVAR, AVP, SELECT, PVE (pv based format string)
1366  * or string.
1367  * PVAR, AVP and select and non-static PVEs  identifiers will be resolved to
1368  * their values during runtime.
1369  * The parameter value will be converted to fparam structure
1370  * @param  param - double pointer to param, as for normal fixup functions.
1371  * @param  param_no - parameter number, ignored.
1372  * @return -1 on an error, 0 on success.
1373  */
fixup_var_pve_str_12(void ** param,int param_no)1374 int fixup_var_pve_str_12(void** param, int param_no)
1375 {
1376 	int ret;
1377 	fparam_t* fp;
1378 	if (fixup_get_param_type(param) != STRING_RVE_ST) {
1379 		/* if called with a RVE already converted to string =>
1380 		 * don't try AVP, PVAR, SELECT or PVE again (to avoid double
1381 		 * deref., e.g.: $foo="$bar"; f($foo) ) */
1382 		if ((ret = fix_param(FPARAM_PVS, param)) <= 0) return ret;
1383 		if ((ret = fix_param(FPARAM_AVP, param)) <= 0) return ret;
1384 		if ((ret = fix_param(FPARAM_SELECT, param)) <= 0) return ret;
1385 		if ((ret = fix_param(FPARAM_PVE, param)) <= 0) {
1386 			if (ret < 0)
1387 				return ret;
1388 			/* check if it resolved to a dynamic or "static" PVE.
1389 			 * If the resulting PVE is static (normal string), discard
1390 			 * it and use the normal string fixup (faster at runtime) */
1391 			fp = (fparam_t*)*param;
1392 			if (fp->v.pve->spec == 0 || fp->v.pve->spec->getf == 0)
1393 				fparam_free_restore(param); /* fallback to STR below */
1394 			else
1395 				return ret; /* dynamic PVE => return */
1396 		}
1397 
1398 	}
1399 	if ((ret = fix_param(FPARAM_STR, param)) <= 0) return ret;
1400 	LM_ERR("Error while fixing parameter, PV, AVP, SELECT, and str conversions"
1401 			" failed\n");
1402 	return -1;
1403 }
1404 
1405 /* Same as fixup_var_pve_str_12 but applies to the 1st parameter only */
fixup_var_pve_str_1(void ** param,int param_no)1406 int fixup_var_pve_str_1(void** param, int param_no)
1407 {
1408 	if (param_no == 1) return fixup_var_pve_str_12(param, param_no);
1409 	else return 0;
1410 }
1411 
1412 /* Same as fixup_var_pve_str_12 but applies to the 2nd parameter only */
fixup_var_pve_str_2(void ** param,int param_no)1413 int fixup_var_pve_str_2(void** param, int param_no)
1414 {
1415 	if (param_no == 2) return fixup_var_pve_str_12(param, param_no);
1416 	else return 0;
1417 }
1418 
1419 
1420 
1421 /*
1422  * Fixup variable integer, the parameter can be
1423  * AVP, SELECT, or ordinary integer. AVP and select
1424  * identifiers will be resolved to their values and
1425  * converted to int if necessary during runtime
1426  *
1427  * The parameter value will be converted to fparam structure
1428  * This function returns -1 on an error
1429  */
fixup_var_int_12(void ** param,int param_no)1430 int fixup_var_int_12(void** param, int param_no)
1431 {
1432 	int ret;
1433 	if (fixup_get_param_type(param) != STRING_RVE_ST) {
1434 		/* if called with a RVE already converted to string =>
1435 		 * don't try AVP, PVAR or SELECT (to avoid double
1436 		 * deref., e.g.: $foo="$bar"; f($foo) ) */
1437 		if ((ret = fix_param(FPARAM_PVS, param)) <= 0) return ret;
1438 		if ((ret = fix_param(FPARAM_AVP, param)) <= 0) return ret;
1439 		if ((ret = fix_param(FPARAM_SELECT, param)) <= 0) return ret;
1440 	}
1441 	if ((ret = fix_param(FPARAM_INT, param)) <= 0) return ret;
1442 	LM_ERR("Error while fixing parameter, PV, AVP, SELECT, and int conversions"
1443 			" failed\n");
1444 	return -1;
1445 }
1446 
1447 /* Same as fixup_var_int_12 but applies to the 1st parameter only */
fixup_var_int_1(void ** param,int param_no)1448 int fixup_var_int_1(void** param, int param_no)
1449 {
1450 	if (param_no == 1) return fixup_var_int_12(param, param_no);
1451 	else return 0;
1452 }
1453 
1454 /* Same as fixup_var_int_12 but applies to the 2nd parameter only */
fixup_var_int_2(void ** param,int param_no)1455 int fixup_var_int_2(void** param, int param_no)
1456 {
1457 	if (param_no == 2) return fixup_var_int_12(param, param_no);
1458 	else return 0;
1459 }
1460 
1461 
1462 /*
1463  * The parameter must be a regular expression which must compile, the
1464  * parameter will be converted to compiled regex
1465  */
fixup_regex_12(void ** param,int param_no)1466 int fixup_regex_12(void** param, int param_no)
1467 {
1468 	int ret;
1469 
1470 	if ((ret = fix_param(FPARAM_REGEX, param)) <= 0) return ret;
1471 	LM_ERR("Error while compiling regex in function parameter\n");
1472 	return -1;
1473 }
1474 
1475 /* Same as fixup_regex_12 but applies to the 1st parameter only */
fixup_regex_1(void ** param,int param_no)1476 int fixup_regex_1(void** param, int param_no)
1477 {
1478 	if (param_no == 1) return fixup_regex_12(param, param_no);
1479 	else return 0;
1480 }
1481 
1482 /* Same as fixup_regex_12 but applies to the 2nd parameter only */
fixup_regex_2(void ** param,int param_no)1483 int fixup_regex_2(void** param, int param_no)
1484 {
1485 	if (param_no == 2) return fixup_regex_12(param, param_no);
1486 	else return 0;
1487 }
1488 
1489 /*
1490  * The string parameter will be converted to integer
1491  */
fixup_int_12(void ** param,int param_no)1492 int fixup_int_12(void** param, int param_no)
1493 {
1494 	int ret;
1495 
1496 	if ((ret = fix_param(FPARAM_INT, param)) <= 0) return ret;
1497 	LM_ERR("Cannot function parameter to integer\n");
1498 	return -1;
1499 
1500 }
1501 
1502 /* Same as fixup_int_12 but applies to the 1st parameter only */
fixup_int_1(void ** param,int param_no)1503 int fixup_int_1(void** param, int param_no)
1504 {
1505 	if (param_no == 1) return fixup_int_12(param, param_no);
1506 	else return 0;
1507 }
1508 
1509 /* Same as fixup_int_12 but applies to the 2nd parameter only */
fixup_int_2(void ** param,int param_no)1510 int fixup_int_2(void** param, int param_no)
1511 {
1512 	if (param_no == 2) return fixup_int_12(param, param_no);
1513 	else return 0;
1514 }
1515 
1516 /*
1517  * Parse the parameter as static string, do not resolve
1518  * AVPs or selects, convert the parameter to str structure
1519  */
fixup_str_12(void ** param,int param_no)1520 int fixup_str_12(void** param, int param_no)
1521 {
1522 	int ret;
1523 
1524 	if ((ret = fix_param(FPARAM_STR, param)) <= 0) return ret;
1525 	LM_ERR("Cannot function parameter to string\n");
1526 	return -1;
1527 }
1528 
1529 /* Same as fixup_str_12 but applies to the 1st parameter only */
fixup_str_1(void ** param,int param_no)1530 int fixup_str_1(void** param, int param_no)
1531 {
1532 	if (param_no == 1) return fixup_str_12(param, param_no);
1533 	else return 0;
1534 }
1535 
1536 /* Same as fixup_str_12 but applies to the 2nd parameter only */
fixup_str_2(void ** param,int param_no)1537 int fixup_str_2(void** param, int param_no)
1538 {
1539 	if (param_no == 2) return fixup_str_12(param, param_no);
1540 	else return 0;
1541 }
1542 
1543 
1544 
1545 /** Get the function parameter value as string.
1546  *  @return  0 - Success
1547  *          -1 - Cannot get value
1548  */
get_str_fparam(str * dst,struct sip_msg * msg,fparam_t * param)1549 int get_str_fparam(str* dst, struct sip_msg* msg, fparam_t* param)
1550 {
1551 	int_str val;
1552 	int ret;
1553 	avp_t* avp;
1554 	pv_value_t pv_val;
1555 
1556 	switch(param->type) {
1557 		case FPARAM_REGEX:
1558 		case FPARAM_UNSPEC:
1559 		case FPARAM_INT:
1560 			return -1;
1561 		case FPARAM_STRING:
1562 			dst->s = param->v.asciiz;
1563 			dst->len = strlen(param->v.asciiz);
1564 			break;
1565 		case FPARAM_STR:
1566 			*dst = param->v.str;
1567 			break;
1568 		case FPARAM_AVP:
1569 			avp = search_first_avp(param->v.avp.flags, param->v.avp.name,
1570 									&val, 0);
1571 			if (unlikely(!avp)) {
1572 				LM_DBG("Could not find AVP from function parameter '%s'\n",
1573 						param->orig);
1574 				return -1;
1575 			}
1576 			if (likely(avp->flags & AVP_VAL_STR)) {
1577 				*dst = val.s;
1578 			} else {
1579 				/* The caller does not know of what type the AVP will be so
1580 				 * convert int AVPs into string here
1581 				 */
1582 				dst->s = int2str(val.n, &dst->len);
1583 			}
1584 			break;
1585 		case FPARAM_SELECT:
1586 			ret = run_select(dst, param->v.select, msg);
1587 			if (unlikely(ret < 0 || ret > 0)) return -1;
1588 			break;
1589 		case FPARAM_PVS:
1590 			if (likely((pv_get_spec_value(msg, param->v.pvs, &pv_val)==0) &&
1591 						((pv_val.flags&(PV_VAL_NULL|PV_VAL_STR))==PV_VAL_STR))){
1592 					*dst=pv_val.rs;
1593 			}else{
1594 				LM_ERR("Could not convert PV to str\n");
1595 				return -1;
1596 			}
1597 			break;
1598 		case FPARAM_PVE:
1599 			dst->s=pv_get_buffer();
1600 			dst->len=pv_get_buffer_size();
1601 			if (unlikely(pv_printf(msg, param->v.pve, dst->s, &dst->len)!=0)){
1602 				LM_ERR("Could not convert the PV-formated string to str\n");
1603 				dst->len=0;
1604 				return -1;
1605 			};
1606 			break;
1607 	}
1608 	return 0;
1609 }
1610 
1611 
1612 /** Get the function parameter value as integer.
1613  *  @return  0 - Success
1614  *          -1 - Cannot get value
1615  */
get_int_fparam(int * dst,struct sip_msg * msg,fparam_t * param)1616 int get_int_fparam(int* dst, struct sip_msg* msg, fparam_t* param)
1617 {
1618 	int_str val;
1619 	int ret;
1620 	avp_t* avp;
1621 	str tmp;
1622 	pv_value_t pv_val;
1623 
1624 	switch(param->type) {
1625 		case FPARAM_INT:
1626 			*dst = param->v.i;
1627 			return 0;
1628 		case FPARAM_REGEX:
1629 		case FPARAM_UNSPEC:
1630 		case FPARAM_STRING:
1631 		case FPARAM_STR:
1632 			LM_ERR("Unsupported param type for int value: %d\n", param->type);
1633 			return -1;
1634 		case FPARAM_AVP:
1635 			avp = search_first_avp(param->v.avp.flags, param->v.avp.name,
1636 									&val, 0);
1637 			if (unlikely(!avp)) {
1638 				LM_DBG("Could not find AVP from function parameter '%s'\n",
1639 						param->orig);
1640 				return -1;
1641 			}
1642 			if (avp->flags & AVP_VAL_STR) {
1643 				if (str2int(&val.s, (unsigned int*)dst) < 0) {
1644 					LM_ERR("Could not convert AVP string value to int\n");
1645 					return -1;
1646 				}
1647 			} else {
1648 				*dst = val.n;
1649 			}
1650 			break;
1651 		case FPARAM_SELECT:
1652 			ret = run_select(&tmp, param->v.select, msg);
1653 			if (unlikely(ret < 0 || ret > 0)) return -1;
1654 			if (unlikely(str2int(&tmp, (unsigned int*)dst) < 0)) {
1655 				LM_ERR("Could not convert select result to int\n");
1656 				return -1;
1657 			}
1658 			break;
1659 		case FPARAM_PVS:
1660 			if (likely((pv_get_spec_value(msg, param->v.pvs, &pv_val)==0) &&
1661 						((pv_val.flags&(PV_VAL_NULL|PV_VAL_INT))==PV_VAL_INT))){
1662 					*dst=pv_val.ri;
1663 			}else{
1664 				LM_ERR("Could not convert PV to int\n");
1665 				return -1;
1666 			}
1667 			break;
1668 		case FPARAM_PVE:
1669 			LM_ERR("Unsupported param type for int value: %d\n", param->type);
1670 			return -1;
1671 		default:
1672 			LM_ERR("Unexpected param type: %d\n", param->type);
1673 			return -1;
1674 	}
1675 	return 0;
1676 }
1677 
1678 /** Get the function parameter value as string or/and integer (if possible).
1679  *  @return  0 - Success
1680  *          -1 - Cannot get value
1681  */
get_is_fparam(int * i_dst,str * s_dst,struct sip_msg * msg,fparam_t * param,unsigned int * flags)1682 int get_is_fparam(int* i_dst, str* s_dst, struct sip_msg* msg, fparam_t* param, unsigned int *flags)
1683 {
1684 	int_str val;
1685 	int ret;
1686 	avp_t* avp;
1687 	str tmp;
1688 	pv_value_t pv_val;
1689 
1690 	*flags = 0;
1691 	switch(param->type) {
1692 		case FPARAM_INT:
1693 			*i_dst = param->v.i;
1694 			*flags |= PARAM_INT;
1695 			return 0;
1696 		case FPARAM_REGEX:
1697 		case FPARAM_UNSPEC:
1698 		case FPARAM_STRING:
1699 			s_dst->s = param->v.asciiz;
1700 			s_dst->len = strlen(param->v.asciiz);
1701 			*flags |= PARAM_STR;
1702 			break;
1703 		case FPARAM_STR:
1704 			*s_dst = param->v.str;
1705 			*flags |= PARAM_STR;
1706 			break;
1707 		case FPARAM_AVP:
1708 			avp = search_first_avp(param->v.avp.flags, param->v.avp.name,
1709 									&val, 0);
1710 			if (unlikely(!avp)) {
1711 				LM_DBG("Could not find AVP from function parameter '%s'\n",
1712 						param->orig);
1713 				return -1;
1714 			}
1715 			if (avp->flags & AVP_VAL_STR) {
1716 				*s_dst = val.s;
1717 				*flags |= PARAM_STR;
1718 				if (str2int(&val.s, (unsigned int*)i_dst) < 0) {
1719 					LM_ERR("Could not convert AVP string value to int\n");
1720 					return -1;
1721 				}
1722 			} else {
1723 				*i_dst = val.n;
1724 				*flags |= PARAM_INT;
1725 			}
1726 			break;
1727 		case FPARAM_SELECT:
1728 			ret = run_select(&tmp, param->v.select, msg);
1729 			if (unlikely(ret < 0 || ret > 0)) return -1;
1730 			if (unlikely(str2int(&tmp, (unsigned int*)i_dst) < 0)) {
1731 				LM_ERR("Could not convert select result to int\n");
1732 				return -1;
1733 			}
1734 			*flags |= PARAM_INT;
1735 			break;
1736 		case FPARAM_PVS:
1737 			if (likely(pv_get_spec_value(msg, param->v.pvs, &pv_val)==0)) {
1738 				if ((pv_val.flags&(PV_VAL_NULL|PV_VAL_INT))==PV_VAL_INT){
1739 					*i_dst=pv_val.ri;
1740 					*flags |= PARAM_INT;
1741 				}
1742 				if ((pv_val.flags&(PV_VAL_NULL|PV_VAL_STR))==PV_VAL_STR){
1743 					*s_dst=pv_val.rs;
1744 					*flags |= PARAM_STR;
1745 				}
1746 			}else{
1747 				LM_ERR("Could not get PV\n");
1748 				return -1;
1749 			}
1750 			break;
1751 		case FPARAM_PVE:
1752 			s_dst->s=pv_get_buffer();
1753 			s_dst->len=pv_get_buffer_size();
1754 			if (unlikely(pv_printf(msg, param->v.pve, s_dst->s, &s_dst->len)!=0)){
1755 				LM_ERR("Could not convert the PV-formated string to str\n");
1756 				s_dst->len=0;
1757 				return -1;
1758 			}
1759 			*flags |= PARAM_STR;
1760 			break;
1761 	}
1762 
1763 	/* Let's convert to int, if possible */
1764 	if (!(*flags & PARAM_INT) && (*flags & PARAM_STR) && str2sint(s_dst, i_dst) == 0)
1765 		*flags |= PARAM_INT;
1766 
1767 	if (!*flags) return -1;
1768 
1769 	return 0;
1770 }
1771 
1772 /**
1773  * Retrieve the compiled RegExp.
1774  * @return: 0 for success, negative on error.
1775  */
get_regex_fparam(regex_t * dst,struct sip_msg * msg,fparam_t * param)1776 int get_regex_fparam(regex_t *dst, struct sip_msg* msg, fparam_t* param)
1777 {
1778 	switch (param->type) {
1779 		case FPARAM_REGEX:
1780 			*dst = *param->v.regex;
1781 			return 0;
1782 		default:
1783 			LM_ERR("unexpected parameter type (%d), instead of regexp.\n",
1784 					param->type);
1785 	}
1786 	return -1;
1787 }
1788 
1789 
1790 
1791 /** generic free fixup function for "pure" fparam type fixups.
1792  * @param  param - double pointer to param, as for normal fixup functions.
1793  * @param  param_no - parameter number, ignored.
1794  * @return 0 on success (always).
1795  */
fixup_free_fparam_all(void ** param,int param_no)1796 int fixup_free_fparam_all(void** param, int param_no)
1797 {
1798 	fparam_free_restore(param);
1799 	return 0;
1800 }
1801 
1802 
1803 
1804 /** generic free fixup function for "pure"  first parameter fparam type fixups.
1805  * @param  param - double pointer to param, as for normal fixup functions.
1806  * @param  param_no - parameter number: the function will work only for
1807  *                     param_no == 1 (first parameter).
1808  * @return 0 on success (always).
1809  */
fixup_free_fparam_1(void ** param,int param_no)1810 int fixup_free_fparam_1(void** param, int param_no)
1811 {
1812 	if (param_no == 1)
1813 		fparam_free_restore(param);
1814 	return 0;
1815 }
1816 
1817 
1818 
1819 /** generic free fixup function for "pure"  2nd parameter fparam type fixups.
1820  * @param  param - double pointer to param, as for normal fixup functions.
1821  * @param  param_no - parameter number: the function will work only for
1822  *                     param_no == 2 (2nd parameter).
1823  * @return 0 on success (always).
1824  */
fixup_free_fparam_2(void ** param,int param_no)1825 int fixup_free_fparam_2(void** param, int param_no)
1826 {
1827 	if (param_no == 2)
1828 		fparam_free_restore(param);
1829 	return 0;
1830 }
1831 
1832 
1833 
1834 /** returns true if a fixup is a fparam_t* one.
1835  * Used to automatically detect "pure" fparam fixups that can be used with non
1836  * contant RVEs.
1837  * @param f - function pointer
1838  * @return 1 for fparam fixups, 0 for others.
1839  */
is_fparam_rve_fixup(fixup_function f)1840 int is_fparam_rve_fixup(fixup_function f)
1841 {
1842 	if (f == fixup_var_str_12 ||
1843 		f == fixup_var_str_1 ||
1844 		f == fixup_var_str_2 ||
1845 		f == fixup_var_pve_str_12 ||
1846 		f == fixup_var_pve_str_1 ||
1847 		f == fixup_var_pve_str_2 ||
1848 		f == fixup_var_int_12 ||
1849 		f == fixup_var_int_1 ||
1850 		f == fixup_var_int_2 ||
1851 		f == fixup_int_12 ||
1852 		f == fixup_int_1 ||
1853 		f == fixup_int_2 ||
1854 		f == fixup_str_12 ||
1855 		f == fixup_str_1 ||
1856 		f == fixup_str_2 ||
1857 		f == fixup_regex_12 ||
1858 		f == fixup_regex_1 ||
1859 		f == fixup_regex_2
1860 		)
1861 		return 1;
1862 	return 0;
1863 }
1864 
1865 
1866 /**
1867  * @brief returns the corresponding fixup_free* for various known fixup types
1868  *
1869  * Returns the corresponding fixup_free* for various known fixup types.
1870  * Used to automatically fill in free_fixup* functions.
1871  * @param f fixup function pointer
1872  * @return free fixup function pointer on success, 0 on failure (unknown
1873  * fixup or no free fixup function).
1874  */
get_fixup_free(fixup_function f)1875 free_fixup_function get_fixup_free(fixup_function f)
1876 {
1877 	free_fixup_function ret;
1878 	/* "pure" fparam, all parameters */
1879 	if (f == fixup_var_str_12 ||
1880 		f == fixup_var_pve_str_12 ||
1881 		f == fixup_var_int_12 ||
1882 		f == fixup_int_12 ||
1883 		f == fixup_str_12 ||
1884 		f == fixup_regex_12)
1885 		return fixup_free_fparam_all;
1886 
1887 	/* "pure" fparam, 1st parameter */
1888 	if (f == fixup_var_str_1 ||
1889 		f == fixup_var_pve_str_1 ||
1890 		f == fixup_var_int_1 ||
1891 		f == fixup_int_1 ||
1892 		f == fixup_str_1 ||
1893 		f == fixup_regex_1)
1894 		return fixup_free_fparam_1;
1895 
1896 	/* "pure" fparam, 2nd parameters */
1897 	if (f == fixup_var_str_2 ||
1898 		f == fixup_var_pve_str_2 ||
1899 		f == fixup_var_int_2 ||
1900 		f == fixup_int_2 ||
1901 		f == fixup_str_2 ||
1902 		f == fixup_regex_2)
1903 		return fixup_free_fparam_2;
1904 
1905 	/* mod_fix.h kamailio style fixups */
1906 	if ((ret = mod_fix_get_fixup_free(f)) != 0)
1907 		return ret;
1908 
1909 	/* unknown */
1910 	return 0;
1911 }
1912