Browse code

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 authored on 26/11/2021 14:32:54
Showing 4 changed files
... ...
@@ -228,10 +228,7 @@ modparam("uac_redirect","accept_filter",".*@siphub\.net")
228 228
 				<para><emphasis>acc_db_request</emphasis></para>
229 229
 				</listitem>
230 230
 				<listitem>
231
-				<para><emphasis>acc_rad_request</emphasis></para>
232
-				</listitem>
233
-				<listitem>
234
-				<para><emphasis>acc_diam_request</emphasis></para>
231
+				<para><emphasis>acc_request</emphasis></para>
235 232
 				</listitem>
236 233
 			</itemizedlist>
237 234
 			<para>
... ...
@@ -38,11 +38,11 @@ extern int _redirect_q_value;
38 38
 #define MAX_CONTACTS_PER_REPLY   16
39 39
 
40 40
 static int shmcontact2dset(struct sip_msg *req, struct sip_msg *shrpl,
41
-		long max, struct acc_param *reason, unsigned int bflags);
41
+		long max, str *reason, unsigned int bflags);
42 42
 
43 43
 
44 44
 int get_redirect( struct sip_msg *msg , int maxt, int maxb,
45
-									struct acc_param *reason, unsigned int bflags)
45
+									str *reason, unsigned int bflags)
46 46
 {
47 47
 	struct cell *t;
48 48
 	str backup_uri;
... ...
@@ -51,7 +51,6 @@ int get_redirect( struct sip_msg *msg , int maxt, int maxb,
51 51
 	int n;
52 52
 	int i;
53 53
 	int first_branch;
54
-	char code_buf[INT2STR_MAX_LEN];
55 54
 
56 55
 	/* get transaction */
57 56
 	t = rd_tmb.t_gett();
... ...
@@ -90,12 +89,6 @@ int get_redirect( struct sip_msg *msg , int maxt, int maxb,
90 89
 		}
91 90
 		if (max==0)
92 91
 			continue;
93
-		if(reason!=NULL)
94
-		{
95
-			/* put the response code into the acc_param reason struct */
96
-			reason->code = t->uac[i].last_received;
97
-			reason->code_s.s = int2bstr((unsigned long)reason->code, code_buf, &reason->code_s.len);
98
-		}
99 92
 		/* get the contact from it */
100 93
 		n = shmcontact2dset( msg, t->uac[i].reply, max, reason, bflags);
101 94
 		if ( n<0 ) {
... ...
@@ -187,7 +180,7 @@ static int sort_contacts(hdr_field_t *chdr, contact_t **ct_array,
187 180
  *            n - ok and n contacts added
188 181
  */
189 182
 static int shmcontact2dset(struct sip_msg *req, struct sip_msg *sh_rpl,
190
-								long max, struct acc_param *reason, unsigned int bflags)
183
+								long max, str *reason, unsigned int bflags)
191 184
 {
192 185
 	static struct sip_msg  dup_rpl;
193 186
 	static contact_t *scontacts[MAX_CONTACTS_PER_REPLY];
... ...
@@ -307,11 +300,23 @@ static int shmcontact2dset(struct sip_msg *req, struct sip_msg *sh_rpl,
307 300
 				LM_ERR("failed to add contact to dset\n");
308 301
 			} else {
309 302
 				added++;
310
-				if (rd_acc_fct!=0 && reason) {
303
+				if (_uacred_accb.acc_request!=NULL && reason) {
311 304
 					/* log the redirect */
312 305
 					req->new_uri =  scontacts[i]->uri;
313 306
 					//FIXME
314
-					rd_acc_fct( req, (char*)reason, acc_db_table);
307
+					if(uacred_acc_fct_s.len==11
308
+							&& strncmp(uacred_acc_fct_s.s,
309
+								"acc_request", 11)==0) {
310
+						_uacred_accb.acc_request(req, reason, &uacred_acc_db_table);
311
+					} else if(uacred_acc_fct_s.len==15
312
+							&& strncmp(uacred_acc_fct_s.s,
313
+								"acc_log_request", 15)==0) {
314
+						_uacred_accb.acc_log_request(req, reason);
315
+					} else if(uacred_acc_fct_s.len==14
316
+							&& strncmp(uacred_acc_fct_s.s,
317
+								"acc_db_request", 14)==0) {
318
+						_uacred_accb.acc_db_request(req, reason, &uacred_acc_db_table);
319
+					}
315 320
 				}
316 321
 			}
317 322
 		} else {
... ...
@@ -32,14 +32,16 @@
32 32
 typedef int (*tm_get_trans_f)( struct sip_msg*, struct cell**);
33 33
 
34 34
 extern struct tm_binds rd_tmb;
35
-extern cmd_function   rd_acc_fct;
36 35
 
37
-extern char *acc_db_table;
36
+extern str uacred_acc_db_table;
37
+extern str uacred_acc_fct_s;
38
+
39
+extern acc_api_t _uacred_accb;
38 40
 
39 41
 extern int flags_hdr_mode;
40 42
 
41 43
 int get_redirect( struct sip_msg *msg , int maxt, int maxb,
42
-		struct acc_param *reason, unsigned int bflags);
44
+		str *reason, unsigned int bflags);
43 45
 
44 46
 #endif
45 47
 
... ...
@@ -27,7 +27,9 @@
27 27
 #include "../../core/dprint.h"
28 28
 #include "../../core/mem/mem.h"
29 29
 #include "../../core/utils/sruid.h"
30
+#include "../../modules/acc/acc_api.h"
30 31
 #include "../../modules/tm/tm_load.h"
32
+#include "../../core/mod_fix.h"
31 33
 #include "../../core/kemi.h"
32 34
 #include "rd_funcs.h"
33 35
 #include "rd_filter.h"
... ...
@@ -36,11 +38,10 @@ MODULE_VERSION
36 38
 
37 39
 /* internal global variables */
38 40
 struct tm_binds rd_tmb;           /*imported functions from tm */
39
-cmd_function    rd_acc_fct = 0;   /*imported function from acc */
40 41
 
41 42
 /* global parameter variables */
42
-char *acc_db_table = "acc";
43
-char *acc_fct_s    = "acc_log_request";
43
+str uacred_acc_db_table = str_init("acc");
44
+str uacred_acc_fct_s    = str_init("acc_log_request");
44 45
 
45 46
 /* private parameter variables */
46 47
 char *deny_filter_s = 0;
... ...
@@ -59,6 +60,8 @@ int _redirect_q_value = DEFAULT_Q_VALUE;
59 60
 /* sruid to get internal uid */
60 61
 sruid_t _redirect_sruid;
61 62
 
63
+acc_api_t _uacred_accb = {0};
64
+
62 65
 
63 66
 static int redirect_init(void);
64 67
 static int child_init(int rank);
... ...
@@ -87,8 +90,8 @@ static param_export_t params[] = {
87 90
 	{"deny_filter",     PARAM_STRING,  &deny_filter_s    },
88 91
 	{"accept_filter",   PARAM_STRING,  &accept_filter_s  },
89 92
 	{"default_filter",  PARAM_STRING,  &def_filter_s     },
90
-	{"acc_function",    PARAM_STRING,  &acc_fct_s        },
91
-	{"acc_db_table",    PARAM_STRING,  &acc_db_table     },
93
+	{"acc_function",    PARAM_STR,  &uacred_acc_fct_s        },
94
+	{"acc_db_table",    PARAM_STR,  &uacred_acc_db_table     },
92 95
 	{"bflags",    		INT_PARAM,  &bflags			  },
93 96
 	{"flags_hdr_mode",	INT_PARAM,  &flags_hdr_mode	  },
94 97
 	{"q_value",         INT_PARAM,  &_redirect_q_value   },
... ...
@@ -141,8 +144,6 @@ int get_nr_max(char *s, unsigned char *max)
141 144
 static int get_redirect_fixup(void** param, int param_no)
142 145
 {
143 146
 	unsigned char maxb,maxt;
144
-	struct acc_param *accp;
145
-	cmd_function fct;
146 147
 	char *p;
147 148
 	char *s;
148 149
 
... ...
@@ -165,37 +166,14 @@ static int get_redirect_fixup(void** param, int param_no)
165 166
 		*param=(void*)(long)( (((unsigned short)maxt)<<8) | maxb);
166 167
 	} else if (param_no==2) {
167 168
 		/* acc function loaded? */
168
-		if (rd_acc_fct==0) {
169
-			/* must import the acc stuff */
170
-			if (acc_fct_s==0 || acc_fct_s[0]==0) {
171
-				LM_ERR("acc support enabled, but no acc function defined\n");
172
-				return E_UNSPEC;
173
-			}
174
-			fct = find_export(acc_fct_s, 2, REQUEST_ROUTE);
175
-			if ( fct==0 )
176
-				fct = find_export(acc_fct_s, 1, REQUEST_ROUTE);
177
-			if ( fct==0 ) {
178
-				LM_ERR("cannot import %s function; is acc loaded and proper "
179
-					"compiled?\n", acc_fct_s);
169
+		if (_uacred_accb.acc_request==NULL) {
170
+			/* bind the ACC API */
171
+			if(acc_load_api(&_uacred_accb) < 0) {
172
+				LM_ERR("cannot bind to ACC API\n");
180 173
 				return E_UNSPEC;
181 174
 			}
182
-			rd_acc_fct = fct;
183
-		}
184
-		/* set the reason str */
185
-		accp = (struct acc_param*)pkg_malloc(sizeof(struct acc_param));
186
-		if (accp==0) {
187
-			PKG_MEM_ERROR;
188
-			return E_UNSPEC;
189
-		}
190
-		memset( accp, 0, sizeof(struct acc_param));
191
-		if (s!=0 && *s!=0) {
192
-			accp->reason.s = s;
193
-			accp->reason.len = strlen(s);
194
-		} else {
195
-			accp->reason.s = "n/a";
196
-			accp->reason.len = 3;
197 175
 		}
198
-		*param=(void*)accp;
176
+		return fixup_spve_null(param, 1);
199 177
 	}
200 178
 
201 179
 	return 0;
... ...
@@ -264,8 +242,6 @@ static int regexp_compile(char *re_s, regex_t **re)
264 242
 static int redirect_init(void)
265 243
 {
266 244
 	regex_t *filter;
267
-	void *p;
268
-	cmd_function fct;
269 245
 
270 246
 	/* load the TM API */
271 247
 	if (load_tm_api(&rd_tmb)!=0) {
... ...
@@ -273,13 +249,11 @@ static int redirect_init(void)
273 249
 		goto error;
274 250
 	}
275 251
 
276
-	p = (void*)acc_db_table;
277
-	/* fixup table name */
278
-	if(fixup_var_pve_str_12(&p, 1)<0) {
279
-		LM_ERR("failed to fixup acc db table\n");
280
-		goto error;
252
+	/* bind the ACC API */
253
+	if(acc_load_api(&_uacred_accb) < 0) {
254
+		LM_ERR("cannot bind to ACC API\n");
255
+		return -1;
281 256
 	}
282
-	acc_db_table = p;
283 257
 
284 258
 	/* init filter */
285 259
 	init_filters();
... ...
@@ -312,21 +286,6 @@ static int redirect_init(void)
312 286
 	if(sruid_init(&_redirect_sruid, '-', "rdir", SRUID_INC)<0)
313 287
 		return -1;
314 288
 
315
-	if(rd_acc_fct == 0) {
316
-		/* import the acc stuff */
317
-		if(acc_fct_s != 0 && acc_fct_s[0] == '\0') {
318
-			fct = find_export(acc_fct_s, 2, REQUEST_ROUTE);
319
-			if(fct == 0)
320
-				fct = find_export(acc_fct_s, 1, REQUEST_ROUTE);
321
-			if(fct == 0) {
322
-				LM_ERR("cannot import %s function; is acc loaded and"
323
-						" configured\n", acc_fct_s);
324
-				return E_UNSPEC;
325
-			}
326
-			rd_acc_fct = fct;
327
-		}
328
-	}
329
-
330 289
 	return 0;
331 290
 error:
332 291
 	return -1;
... ...
@@ -379,11 +338,17 @@ static int w_get_redirect2(struct sip_msg* msg, char *max_c, char *reason)
379 338
 {
380 339
 	int n;
381 340
 	unsigned short max;
341
+	str sreason;
342
+
343
+	if(fixup_get_svalue(msg, (gparam_t*)reason, &sreason)<0) {
344
+		LM_ERR("failed to get reason parameter\n");
345
+		return -1;
346
+	}
382 347
 
383 348
 	msg_tracer( msg, 0);
384 349
 	/* get the contacts */
385 350
 	max = (unsigned short)(long)max_c;
386
-	n = get_redirect(msg , (max>>8)&0xff, max&0xff, (struct acc_param*)reason, bflags);
351
+	n = get_redirect(msg , (max>>8)&0xff, max&0xff, &sreason, bflags);
387 352
 	reset_filters();
388 353
 	/* reset the tracer */
389 354
 	msg_tracer( msg, 1);
... ...
@@ -401,16 +366,10 @@ static int ki_get_redirects_acc(sip_msg_t* msg, int max_c, int max_b,
401 366
 		str *reason)
402 367
 {
403 368
 	int n;
404
-	acc_param_t accp;
405 369
 
406
-	if(reason && reason->len>0) {
407
-		memset(&accp, 0, sizeof(acc_param_t));
408
-		accp.reason.s = reason->s;
409
-		accp.reason.len = reason->len;
410
-	}
411 370
 	msg_tracer(msg, 0);
412 371
 	/* get the contacts */
413
-	n = get_redirect(msg, max_c, max_b, (reason && reason->len>0)?&accp:NULL,
372
+	n = get_redirect(msg, max_c, max_b, (reason && reason->len>0)?reason:NULL,
414 373
 			bflags);
415 374
 	reset_filters();
416 375
 	/* reset the tracer */