Browse Source

uac_redirect: use inter-module acc api instead of config export lookup

- use acc functions via inter-module acc api, intead of config file
function export lookup, to get the right signature and avoid workaround
of fixing a modparam like a function param and improper access of
expected fields by offset
- GH #2953
Daniel-Constantin Mierla 3 years ago
parent
commit
a0ef1ccd17

+ 1 - 4
src/modules/uac_redirect/doc/uac_redirect_admin.xml

@@ -228,10 +228,7 @@ modparam("uac_redirect","accept_filter",".*@siphub\.net")
 				<para><emphasis>acc_db_request</emphasis></para>
 				</listitem>
 				<listitem>
-				<para><emphasis>acc_rad_request</emphasis></para>
-				</listitem>
-				<listitem>
-				<para><emphasis>acc_diam_request</emphasis></para>
+				<para><emphasis>acc_request</emphasis></para>
 				</listitem>
 			</itemizedlist>
 			<para>

+ 17 - 12
src/modules/uac_redirect/rd_funcs.c

@@ -38,11 +38,11 @@ extern int _redirect_q_value;
 #define MAX_CONTACTS_PER_REPLY   16
 
 static int shmcontact2dset(struct sip_msg *req, struct sip_msg *shrpl,
-		long max, struct acc_param *reason, unsigned int bflags);
+		long max, str *reason, unsigned int bflags);
 
 
 int get_redirect( struct sip_msg *msg , int maxt, int maxb,
-									struct acc_param *reason, unsigned int bflags)
+									str *reason, unsigned int bflags)
 {
 	struct cell *t;
 	str backup_uri;
@@ -51,7 +51,6 @@ int get_redirect( struct sip_msg *msg , int maxt, int maxb,
 	int n;
 	int i;
 	int first_branch;
-	char code_buf[INT2STR_MAX_LEN];
 
 	/* get transaction */
 	t = rd_tmb.t_gett();
@@ -90,12 +89,6 @@ int get_redirect( struct sip_msg *msg , int maxt, int maxb,
 		}
 		if (max==0)
 			continue;
-		if(reason!=NULL)
-		{
-			/* put the response code into the acc_param reason struct */
-			reason->code = t->uac[i].last_received;
-			reason->code_s.s = int2bstr((unsigned long)reason->code, code_buf, &reason->code_s.len);
-		}
 		/* get the contact from it */
 		n = shmcontact2dset( msg, t->uac[i].reply, max, reason, bflags);
 		if ( n<0 ) {
@@ -187,7 +180,7 @@ static int sort_contacts(hdr_field_t *chdr, contact_t **ct_array,
  *            n - ok and n contacts added
  */
 static int shmcontact2dset(struct sip_msg *req, struct sip_msg *sh_rpl,
-								long max, struct acc_param *reason, unsigned int bflags)
+								long max, str *reason, unsigned int bflags)
 {
 	static struct sip_msg  dup_rpl;
 	static contact_t *scontacts[MAX_CONTACTS_PER_REPLY];
@@ -307,11 +300,23 @@ static int shmcontact2dset(struct sip_msg *req, struct sip_msg *sh_rpl,
 				LM_ERR("failed to add contact to dset\n");
 			} else {
 				added++;
-				if (rd_acc_fct!=0 && reason) {
+				if (_uacred_accb.acc_request!=NULL && reason) {
 					/* log the redirect */
 					req->new_uri =  scontacts[i]->uri;
 					//FIXME
-					rd_acc_fct( req, (char*)reason, acc_db_table);
+					if(uacred_acc_fct_s.len==11
+							&& strncmp(uacred_acc_fct_s.s,
+								"acc_request", 11)==0) {
+						_uacred_accb.acc_request(req, reason, &uacred_acc_db_table);
+					} else if(uacred_acc_fct_s.len==15
+							&& strncmp(uacred_acc_fct_s.s,
+								"acc_log_request", 15)==0) {
+						_uacred_accb.acc_log_request(req, reason);
+					} else if(uacred_acc_fct_s.len==14
+							&& strncmp(uacred_acc_fct_s.s,
+								"acc_db_request", 14)==0) {
+						_uacred_accb.acc_db_request(req, reason, &uacred_acc_db_table);
+					}
 				}
 			}
 		} else {

+ 5 - 3
src/modules/uac_redirect/rd_funcs.h

@@ -32,14 +32,16 @@
 typedef int (*tm_get_trans_f)( struct sip_msg*, struct cell**);
 
 extern struct tm_binds rd_tmb;
-extern cmd_function   rd_acc_fct;
 
-extern char *acc_db_table;
+extern str uacred_acc_db_table;
+extern str uacred_acc_fct_s;
+
+extern acc_api_t _uacred_accb;
 
 extern int flags_hdr_mode;
 
 int get_redirect( struct sip_msg *msg , int maxt, int maxb,
-		struct acc_param *reason, unsigned int bflags);
+		str *reason, unsigned int bflags);
 
 #endif
 

+ 25 - 66
src/modules/uac_redirect/uac_redirect.c

@@ -27,7 +27,9 @@
 #include "../../core/dprint.h"
 #include "../../core/mem/mem.h"
 #include "../../core/utils/sruid.h"
+#include "../../modules/acc/acc_api.h"
 #include "../../modules/tm/tm_load.h"
+#include "../../core/mod_fix.h"
 #include "../../core/kemi.h"
 #include "rd_funcs.h"
 #include "rd_filter.h"
@@ -36,11 +38,10 @@ MODULE_VERSION
 
 /* internal global variables */
 struct tm_binds rd_tmb;           /*imported functions from tm */
-cmd_function    rd_acc_fct = 0;   /*imported function from acc */
 
 /* global parameter variables */
-char *acc_db_table = "acc";
-char *acc_fct_s    = "acc_log_request";
+str uacred_acc_db_table = str_init("acc");
+str uacred_acc_fct_s    = str_init("acc_log_request");
 
 /* private parameter variables */
 char *deny_filter_s = 0;
@@ -59,6 +60,8 @@ int _redirect_q_value = DEFAULT_Q_VALUE;
 /* sruid to get internal uid */
 sruid_t _redirect_sruid;
 
+acc_api_t _uacred_accb = {0};
+
 
 static int redirect_init(void);
 static int child_init(int rank);
@@ -87,8 +90,8 @@ static param_export_t params[] = {
 	{"deny_filter",     PARAM_STRING,  &deny_filter_s    },
 	{"accept_filter",   PARAM_STRING,  &accept_filter_s  },
 	{"default_filter",  PARAM_STRING,  &def_filter_s     },
-	{"acc_function",    PARAM_STRING,  &acc_fct_s        },
-	{"acc_db_table",    PARAM_STRING,  &acc_db_table     },
+	{"acc_function",    PARAM_STR,  &uacred_acc_fct_s        },
+	{"acc_db_table",    PARAM_STR,  &uacred_acc_db_table     },
 	{"bflags",    		INT_PARAM,  &bflags			  },
 	{"flags_hdr_mode",	INT_PARAM,  &flags_hdr_mode	  },
 	{"q_value",         INT_PARAM,  &_redirect_q_value   },
@@ -141,8 +144,6 @@ int get_nr_max(char *s, unsigned char *max)
 static int get_redirect_fixup(void** param, int param_no)
 {
 	unsigned char maxb,maxt;
-	struct acc_param *accp;
-	cmd_function fct;
 	char *p;
 	char *s;
 
@@ -165,37 +166,14 @@ static int get_redirect_fixup(void** param, int param_no)
 		*param=(void*)(long)( (((unsigned short)maxt)<<8) | maxb);
 	} else if (param_no==2) {
 		/* acc function loaded? */
-		if (rd_acc_fct==0) {
-			/* must import the acc stuff */
-			if (acc_fct_s==0 || acc_fct_s[0]==0) {
-				LM_ERR("acc support enabled, but no acc function defined\n");
-				return E_UNSPEC;
-			}
-			fct = find_export(acc_fct_s, 2, REQUEST_ROUTE);
-			if ( fct==0 )
-				fct = find_export(acc_fct_s, 1, REQUEST_ROUTE);
-			if ( fct==0 ) {
-				LM_ERR("cannot import %s function; is acc loaded and proper "
-					"compiled?\n", acc_fct_s);
+		if (_uacred_accb.acc_request==NULL) {
+			/* bind the ACC API */
+			if(acc_load_api(&_uacred_accb) < 0) {
+				LM_ERR("cannot bind to ACC API\n");
 				return E_UNSPEC;
 			}
-			rd_acc_fct = fct;
-		}
-		/* set the reason str */
-		accp = (struct acc_param*)pkg_malloc(sizeof(struct acc_param));
-		if (accp==0) {
-			PKG_MEM_ERROR;
-			return E_UNSPEC;
-		}
-		memset( accp, 0, sizeof(struct acc_param));
-		if (s!=0 && *s!=0) {
-			accp->reason.s = s;
-			accp->reason.len = strlen(s);
-		} else {
-			accp->reason.s = "n/a";
-			accp->reason.len = 3;
 		}
-		*param=(void*)accp;
+		return fixup_spve_null(param, 1);
 	}
 
 	return 0;
@@ -264,8 +242,6 @@ static int regexp_compile(char *re_s, regex_t **re)
 static int redirect_init(void)
 {
 	regex_t *filter;
-	void *p;
-	cmd_function fct;
 
 	/* load the TM API */
 	if (load_tm_api(&rd_tmb)!=0) {
@@ -273,13 +249,11 @@ static int redirect_init(void)
 		goto error;
 	}
 
-	p = (void*)acc_db_table;
-	/* fixup table name */
-	if(fixup_var_pve_str_12(&p, 1)<0) {
-		LM_ERR("failed to fixup acc db table\n");
-		goto error;
+	/* bind the ACC API */
+	if(acc_load_api(&_uacred_accb) < 0) {
+		LM_ERR("cannot bind to ACC API\n");
+		return -1;
 	}
-	acc_db_table = p;
 
 	/* init filter */
 	init_filters();
@@ -312,21 +286,6 @@ static int redirect_init(void)
 	if(sruid_init(&_redirect_sruid, '-', "rdir", SRUID_INC)<0)
 		return -1;
 
-	if(rd_acc_fct == 0) {
-		/* import the acc stuff */
-		if(acc_fct_s != 0 && acc_fct_s[0] == '\0') {
-			fct = find_export(acc_fct_s, 2, REQUEST_ROUTE);
-			if(fct == 0)
-				fct = find_export(acc_fct_s, 1, REQUEST_ROUTE);
-			if(fct == 0) {
-				LM_ERR("cannot import %s function; is acc loaded and"
-						" configured\n", acc_fct_s);
-				return E_UNSPEC;
-			}
-			rd_acc_fct = fct;
-		}
-	}
-
 	return 0;
 error:
 	return -1;
@@ -379,11 +338,17 @@ static int w_get_redirect2(struct sip_msg* msg, char *max_c, char *reason)
 {
 	int n;
 	unsigned short max;
+	str sreason;
+
+	if(fixup_get_svalue(msg, (gparam_t*)reason, &sreason)<0) {
+		LM_ERR("failed to get reason parameter\n");
+		return -1;
+	}
 
 	msg_tracer( msg, 0);
 	/* get the contacts */
 	max = (unsigned short)(long)max_c;
-	n = get_redirect(msg , (max>>8)&0xff, max&0xff, (struct acc_param*)reason, bflags);
+	n = get_redirect(msg , (max>>8)&0xff, max&0xff, &sreason, bflags);
 	reset_filters();
 	/* reset the tracer */
 	msg_tracer( msg, 1);
@@ -401,16 +366,10 @@ static int ki_get_redirects_acc(sip_msg_t* msg, int max_c, int max_b,
 		str *reason)
 {
 	int n;
-	acc_param_t accp;
 
-	if(reason && reason->len>0) {
-		memset(&accp, 0, sizeof(acc_param_t));
-		accp.reason.s = reason->s;
-		accp.reason.len = reason->len;
-	}
 	msg_tracer(msg, 0);
 	/* get the contacts */
-	n = get_redirect(msg, max_c, max_b, (reason && reason->len>0)?&accp:NULL,
+	n = get_redirect(msg, max_c, max_b, (reason && reason->len>0)?reason:NULL,
 			bflags);
 	reset_filters();
 	/* reset the tracer */