Browse code

utils: handle error case for xcap auth status to free allocated structs

Daniel-Constantin Mierla authored on 20/07/2017 07:28:32
Showing 1 changed files
... ...
@@ -465,6 +465,12 @@ int ki_xcap_auth_status(sip_msg_t* _msg, str* watcher_uri, str* presentity_uri)
465 465
 	res = get_rules_doc(&uri.user, &uri.host, PRES_RULES, &rules_doc);
466 466
 	if ((res < 0) || (rules_doc == NULL) || (rules_doc->s == NULL)) {
467 467
 		LM_DBG("no xcap rules doc found for presentity uri\n");
468
+		if(rules_doc) {
469
+			if(rules_doc->s) {
470
+				pkg_free(rules_doc->s);
471
+			}
472
+			pkg_free(rules_doc);
473
+		}
468 474
 		return PENDING_STATUS;
469 475
 	}
470 476
 
Browse code

utils: use spve fixup - more flexibility in allowing strings and variables

Daniel-Constantin Mierla authored on 28/04/2017 09:16:26
Showing 1 changed files
... ...
@@ -39,6 +39,7 @@
39 39
 #include "../../core/str.h"
40 40
 #include "../../core/dprint.h"
41 41
 #include "../../core/pvar.h"
42
+#include "../../core/mod_fix.h"
42 43
 #include "../../core/parser/parse_uri.h"
43 44
 #include "../../modules/presence/subscribe.h"
44 45
 #include "../../modules/presence/utils_func.h"
... ...
@@ -445,11 +446,8 @@ error:
445 446
  * Checks from presence server xcap table if watcher is authorized
446 447
  * to subscribe event 'presence' of presentity.
447 448
  */
448
-int xcap_auth_status(struct sip_msg* _msg, char* _sp1, char* _sp2)
449
+int ki_xcap_auth_status(sip_msg_t* _msg, str* watcher_uri, str* presentity_uri)
449 450
 {
450
-	pv_spec_t *sp;
451
-	pv_value_t pv_val;
452
-	str watcher_uri, presentity_uri;
453 451
 	struct sip_uri uri;
454 452
 	str* rules_doc = NULL;
455 453
 	subs_t subs;
... ...
@@ -460,43 +458,7 @@ int xcap_auth_status(struct sip_msg* _msg, char* _sp1, char* _sp2)
460 458
 		return -1;
461 459
 	}
462 460
 
463
-	sp = (pv_spec_t *)_sp1;
464
-
465
-	if (sp && (pv_get_spec_value(_msg, sp, &pv_val) == 0)) {
466
-		if (pv_val.flags & PV_VAL_STR) {
467
-			watcher_uri = pv_val.rs;
468
-			if (watcher_uri.len == 0 || watcher_uri.s == NULL) {
469
-				LM_ERR("missing watcher uri\n");
470
-				return -1;
471
-			}
472
-		} else {
473
-			LM_ERR("watcher pseudo variable value is not string\n");
474
-			return -1;
475
-		}
476
-	} else {
477
-		LM_ERR("cannot get watcher pseudo variable value\n");
478
-		return -1;
479
-	}
480
-
481
-	sp = (pv_spec_t *)_sp2;
482
-
483
-	if (sp && (pv_get_spec_value(_msg, sp, &pv_val) == 0)) {
484
-		if (pv_val.flags & PV_VAL_STR) {
485
-			presentity_uri = pv_val.rs;
486
-			if (presentity_uri.len == 0 || presentity_uri.s == NULL) {
487
-				LM_DBG("missing presentity uri\n");
488
-				return -1;
489
-			}
490
-		} else {
491
-			LM_ERR("presentity pseudo variable value is not string\n");
492
-			return -1;
493
-		}
494
-	} else {
495
-		LM_ERR("cannot get presentity pseudo variable value\n");
496
-		return -1;
497
-	}
498
-
499
-	if (parse_uri(presentity_uri.s, presentity_uri.len, &uri) < 0) {
461
+	if (parse_uri(presentity_uri->s, presentity_uri->len, &uri) < 0) {
500 462
 		LM_ERR("failed to parse presentity uri\n");
501 463
 		return -1;
502 464
 	}
... ...
@@ -506,21 +468,22 @@ int xcap_auth_status(struct sip_msg* _msg, char* _sp1, char* _sp2)
506 468
 		return PENDING_STATUS;
507 469
 	}
508 470
 
509
-	if (parse_uri(watcher_uri.s, watcher_uri.len, &uri) < 0) {
471
+	if (parse_uri(watcher_uri->s, watcher_uri->len, &uri) < 0) {
510 472
 		LM_ERR("failed to parse watcher uri\n");
511 473
 		goto err;
512 474
 	}
513 475
 
514 476
 	subs.from_user = uri.user;
515 477
 	subs.from_domain = uri.host;
516
-	subs.pres_uri = presentity_uri;
478
+	subs.pres_uri = *presentity_uri;
517 479
 	subs.auth_rules_doc = rules_doc;
518 480
 	if (pres_watcher_allowed(&subs) < 0) {
519 481
 		LM_ERR("getting status from rules document\n");
520 482
 		goto err;
521 483
 	}
522 484
 	LM_DBG("auth status of watcher <%.*s> on presentity <%.*s> is %d\n",
523
-			watcher_uri.len, watcher_uri.s, presentity_uri.len, presentity_uri.s,
485
+			watcher_uri->len, watcher_uri->s,
486
+			presentity_uri->len, presentity_uri->s,
524 487
 			subs.status);
525 488
 	pkg_free(rules_doc->s);
526 489
 	pkg_free(rules_doc);
... ...
@@ -531,3 +494,19 @@ err:
531 494
 	pkg_free(rules_doc);
532 495
 	return -1;
533 496
 }
497
+
498
+int w_xcap_auth_status(struct sip_msg* _msg, char* _sp1, char* _sp2)
499
+{
500
+	str watcher_uri, presentity_uri;
501
+
502
+	if(fixup_get_svalue(_msg, (gparam_t*)_sp1, &watcher_uri)<0) {
503
+		LM_ERR("cannot get the watcher uri\n");
504
+		return -1;
505
+	}
506
+	if(fixup_get_svalue(_msg, (gparam_t*)_sp2, &presentity_uri)<0) {
507
+		LM_ERR("cannot get the presentity uri\n");
508
+		return -1;
509
+	}
510
+
511
+	return ki_xcap_auth_status(_msg, &watcher_uri, &presentity_uri);
512
+}
534 513
\ No newline at end of file
Browse code

core, lib, modules: updated include paths for header files

Daniel-Constantin Mierla authored on 07/12/2016 11:07:22
Showing 1 changed files
... ...
@@ -36,10 +36,10 @@
36 36
 #include <time.h>
37 37
 #include <libxml/parser.h>
38 38
 
39
-#include "../../str.h"
40
-#include "../../dprint.h"
41
-#include "../../pvar.h"
42
-#include "../../parser/parse_uri.h"
39
+#include "../../core/str.h"
40
+#include "../../core/dprint.h"
41
+#include "../../core/pvar.h"
42
+#include "../../core/parser/parse_uri.h"
43 43
 #include "../../modules/presence/subscribe.h"
44 44
 #include "../../modules/presence/utils_func.h"
45 45
 #include "../../modules/presence/hash.h"
Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,533 @@
1
+/*
2
+ * xcap_auth.c
3
+ *
4
+ * Copyright (C) 2007 Voice Sistem S.R.L.
5
+ *
6
+ * Copyright (C) 2009 Juha Heinanen
7
+ *
8
+ * This file is part of Kamailio, a free SIP server.
9
+ *
10
+ * Kamailio is free software; you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation; either version 2 of the License, or
13
+ * (at your option) any later version
14
+ *
15
+ * Kamailio is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License
21
+ * along with this program; if not, write to the Free Software
22
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23
+ *
24
+ */
25
+
26
+/*!
27
+ * \file
28
+ * \brief Kamailio utils ::
29
+ * \ingroup utils
30
+ * Module: \ref utils
31
+ */
32
+
33
+#include <stdio.h>
34
+#include <stdlib.h>
35
+#include <string.h>
36
+#include <time.h>
37
+#include <libxml/parser.h>
38
+
39
+#include "../../str.h"
40
+#include "../../dprint.h"
41
+#include "../../pvar.h"
42
+#include "../../parser/parse_uri.h"
43
+#include "../../modules/presence/subscribe.h"
44
+#include "../../modules/presence/utils_func.h"
45
+#include "../../modules/presence/hash.h"
46
+#include "../../modules/xcap_client/xcap_callbacks.h"
47
+#include "utils.h"
48
+#include "pidf.h"
49
+
50
+xmlNodePtr get_rule_node(subs_t* subs, xmlDocPtr xcap_tree)
51
+{
52
+	str w_uri = {0, 0};
53
+	char* id = NULL, *domain = NULL, *time_cont= NULL;
54
+	int apply_rule = -1;
55
+	xmlNodePtr ruleset_node = NULL, node1= NULL, node2= NULL;
56
+	xmlNodePtr cond_node = NULL, except_node = NULL;
57
+	xmlNodePtr identity_node = NULL;
58
+	xmlNodePtr iden_child;
59
+	xmlNodePtr validity_node, time_node;
60
+	time_t t_init, t_fin, t;
61
+	int valid= 0;
62
+
63
+	uandd_to_uri(subs->from_user, subs->from_domain, &w_uri);
64
+	if (w_uri.s == NULL) {
65
+		LM_ERR("while creating uri\n");
66
+		return NULL;
67
+	}
68
+	ruleset_node = xmlDocGetNodeByName(xcap_tree, "ruleset", NULL);
69
+	if (ruleset_node == NULL) {
70
+		LM_DBG("ruleset_node NULL\n");
71
+		goto error;
72
+	}
73
+	for (node1 = ruleset_node->children; node1; node1 = node1->next) {
74
+		if (xmlStrcasecmp(node1->name, (unsigned char*)"text") == 0)
75
+			continue;
76
+
77
+		/* process conditions */
78
+		LM_DBG("node1->name= %s\n", node1->name);
79
+
80
+		cond_node = xmlNodeGetChildByName(node1, "conditions");
81
+		if(cond_node == NULL) {
82
+			LM_DBG("cond node NULL\n");
83
+			goto error;
84
+		}
85
+		LM_DBG("cond_node->name= %s\n", cond_node->name);
86
+
87
+		validity_node = xmlNodeGetChildByName(cond_node, "validity");
88
+		if (validity_node != NULL) {
89
+			LM_DBG("found validity tag\n");
90
+
91
+			t= time(NULL);
92
+
93
+			/* search all from-until pair */
94
+			for (time_node = validity_node->children; time_node;
95
+					time_node = time_node->next) {
96
+				if (xmlStrcasecmp(time_node->name, (unsigned char*)"from")!= 0)
97
+					continue;
98
+
99
+				time_cont= (char*)xmlNodeGetContent(time_node);
100
+				t_init= xml_parse_dateTime(time_cont);
101
+				xmlFree(time_cont);
102
+				if (t_init< 0) {
103
+					LM_ERR("failed to parse xml dateTime\n");
104
+					goto error;
105
+				}
106
+
107
+				if (t< t_init) {
108
+					LM_DBG("the lower time limit is not respected\n");
109
+					continue;
110
+				}
111
+
112
+				time_node= time_node->next;
113
+				while (1) {
114
+					if (time_node == NULL) {
115
+						LM_ERR("bad formatted xml doc:until child not found in"
116
+								" validity pair\n");
117
+						goto error;
118
+					}
119
+					if( xmlStrcasecmp(time_node->name,
120
+								(unsigned char*)"until")== 0)
121
+						break;
122
+					time_node= time_node->next;
123
+				}
124
+
125
+				time_cont = (char*)xmlNodeGetContent(time_node);
126
+				t_fin= xml_parse_dateTime(time_cont);
127
+				xmlFree(time_cont);
128
+
129
+				if (t_fin< 0) {
130
+					LM_ERR("failed to parse xml dateTime\n");
131
+					goto error;
132
+				}
133
+
134
+				if (t <= t_fin) {
135
+					LM_DBG("the rule is active at this time\n");
136
+					valid= 1;
137
+				}
138
+
139
+			}
140
+
141
+			if (!valid) {
142
+				LM_DBG("the rule is not active at this time\n");
143
+				continue;
144
+			}
145
+
146
+		}
147
+
148
+		identity_node = xmlNodeGetChildByName(cond_node, "identity");
149
+		if (identity_node == NULL) {
150
+			LM_ERR("didn't find identity tag\n");
151
+			goto error;
152
+		}
153
+
154
+		iden_child = xmlNodeGetChildByName(identity_node, "one");
155
+		if(iden_child) {
156
+			for (node2 = identity_node->children; node2; node2 = node2->next) {
157
+				if(xmlStrcasecmp(node2->name, (unsigned char*)"one")!= 0)
158
+					continue;
159
+
160
+				id = xmlNodeGetAttrContentByName(node2, "id");
161
+				if(id== NULL) {
162
+					LM_ERR("while extracting attribute\n");
163
+					goto error;
164
+				}
165
+				if ((strlen(id)== w_uri.len &&
166
+							(strncmp(id, w_uri.s, w_uri.len)==0))) {
167
+					apply_rule = 1;
168
+					xmlFree(id);
169
+					break;
170
+				}
171
+				xmlFree(id);
172
+			}
173
+		}
174
+
175
+		/* search for many node*/
176
+		iden_child = xmlNodeGetChildByName(identity_node, "many");
177
+		if (iden_child)	{
178
+			domain = NULL;
179
+			for (node2 = identity_node->children; node2; node2 = node2->next) {
180
+				if (xmlStrcasecmp(node2->name, (unsigned char*)"many") != 0)
181
+					continue;
182
+
183
+				domain = xmlNodeGetAttrContentByName(node2, "domain");
184
+				if(domain == NULL) {
185
+					LM_DBG("No domain attribute to many\n");
186
+				} else	{
187
+					LM_DBG("<many domain= %s>\n", domain);
188
+					if((strlen(domain)!= subs->from_domain.len &&
189
+								strncmp(domain, subs->from_domain.s,
190
+									subs->from_domain.len) )) {
191
+						xmlFree(domain);
192
+						continue;
193
+					}
194
+				}
195
+				xmlFree(domain);
196
+				apply_rule = 1;
197
+				if (node2->children == NULL)       /* there is no exception */
198
+					break;
199
+
200
+				for (except_node = node2->children; except_node;
201
+						except_node= except_node->next) {
202
+					if(xmlStrcasecmp(except_node->name,
203
+								(unsigned char*)"except"))
204
+						continue;
205
+
206
+					id = xmlNodeGetAttrContentByName(except_node, "id");
207
+					if (id != NULL) {
208
+						if((strlen(id)- 1== w_uri.len &&
209
+									(strncmp(id, w_uri.s, w_uri.len)==0))) {
210
+							xmlFree(id);
211
+							apply_rule = 0;
212
+							break;
213
+						}
214
+						xmlFree(id);
215
+					} else {
216
+						domain = NULL;
217
+						domain = xmlNodeGetAttrContentByName(except_node,
218
+								"domain");
219
+						if(domain!=NULL) {
220
+							LM_DBG("Found except domain= %s\n- strlen(domain)= %d\n",
221
+									domain, (int)strlen(domain));
222
+							if (strlen(domain)==subs->from_domain.len &&
223
+									(strncmp(domain,subs->from_domain.s ,
224
+											subs->from_domain.len)==0)) {
225
+								LM_DBG("except domain match\n");
226
+								xmlFree(domain);
227
+								apply_rule = 0;
228
+								break;
229
+							}
230
+							xmlFree(domain);
231
+						}
232
+					}
233
+				}
234
+				if (apply_rule == 1)  /* if a match was found no need to keep searching*/
235
+					break;
236
+			}
237
+		}
238
+		if (apply_rule ==1)
239
+			break;
240
+	}
241
+
242
+	LM_DBG("apply_rule= %d\n", apply_rule);
243
+	if(w_uri.s!=NULL)
244
+		pkg_free(w_uri.s);
245
+
246
+	if( !apply_rule || !node1)
247
+		return NULL;
248
+
249
+	return node1;
250
+
251
+error:
252
+	if(w_uri.s)
253
+		pkg_free(w_uri.s);
254
+	return NULL;
255
+}
256
+
257
+int pres_watcher_allowed(subs_t* subs)
258
+{
259
+	xmlDocPtr xcap_tree= NULL;
260
+	xmlNodePtr node= NULL,  actions_node = NULL;
261
+	xmlNodePtr sub_handling_node = NULL;
262
+	char* sub_handling = NULL;
263
+
264
+	subs->status= PENDING_STATUS;
265
+	subs->reason.s= NULL;
266
+	subs->reason.len= 0;
267
+
268
+	if (subs->auth_rules_doc== NULL)
269
+		return 0;
270
+
271
+	xcap_tree= xmlParseMemory(subs->auth_rules_doc->s,
272
+			subs->auth_rules_doc->len);
273
+	if (xcap_tree== NULL) {
274
+		LM_ERR("parsing xml memory\n");
275
+		return -1;
276
+	}
277
+
278
+	node= get_rule_node(subs, xcap_tree);
279
+	if (node== NULL) {
280
+		xmlFreeDoc(xcap_tree);
281
+		return 0;
282
+	}
283
+
284
+	/* process actions */
285
+	actions_node = xmlNodeGetChildByName(node, "actions");
286
+	if (actions_node == NULL) {
287
+		LM_DBG("actions_node NULL\n");
288
+		xmlFreeDoc(xcap_tree);
289
+		return 0;
290
+	}
291
+	LM_DBG("actions_node->name= %s\n", actions_node->name);
292
+
293
+	sub_handling_node = xmlNodeGetChildByName(actions_node, "sub-handling");
294
+	if (sub_handling_node== NULL) {
295
+		LM_DBG("sub_handling_node NULL\n");
296
+		xmlFreeDoc(xcap_tree);
297
+		return 0;
298
+	}
299
+	sub_handling = (char*)xmlNodeGetContent(sub_handling_node);
300
+	LM_DBG("sub_handling_node->name= %s\n", sub_handling_node->name);
301
+	LM_DBG("sub_handling_node->content= %s\n", sub_handling);
302
+
303
+	if (sub_handling == NULL) {
304
+		LM_ERR("Couldn't get sub-handling content\n");
305
+		xmlFreeDoc(xcap_tree);
306
+		return -1;
307
+	}
308
+	if (strncmp((char*)sub_handling, "block", 5) == 0) {
309
+		subs->status = TERMINATED_STATUS;
310
+		subs->reason.s= "rejected";
311
+		subs->reason.len = 8;
312
+	} else {
313
+		if (strncmp((char*)sub_handling, "confirm", 7) == 0) {
314
+			subs->status = PENDING_STATUS;
315
+		} else {
316
+			if (strncmp((char*)sub_handling , "polite-block", 12) == 0) {
317
+				subs->status = ACTIVE_STATUS;
318
+				subs->reason.s= "polite-block";
319
+				subs->reason.len = 12;
320
+			} else {
321
+				if (strncmp((char*)sub_handling, "allow", 5) == 0) {
322
+					subs->status = ACTIVE_STATUS;
323
+					subs->reason.s = NULL;
324
+				} else {
325
+					LM_ERR("unknown subscription handling action\n");
326
+					xmlFreeDoc(xcap_tree);
327
+					xmlFree(sub_handling);
328
+					return -1;
329
+				}
330
+			}
331
+		}
332
+	}
333
+
334
+	xmlFreeDoc(xcap_tree);
335
+	xmlFree(sub_handling);
336
+
337
+	return 0;
338
+
339
+}
340
+
341
+int get_rules_doc(str* user, str* domain, int type, str** rules_doc)
342
+{
343
+	db_key_t query_cols[5];
344
+	db_val_t query_vals[5];
345
+	db_key_t result_cols[3];
346
+	int n_query_cols = 0;
347
+	db1_res_t *result = 0;
348
+	db_row_t *row;
349
+	db_val_t *row_vals;
350
+	str body;
351
+	str* doc= NULL;
352
+	int n_result_cols= 0, xcap_doc_col;
353
+	static str tmp1 = str_init("username");
354
+	static str tmp2 = str_init("domain");
355
+	static str tmp3 = str_init("doc_type");
356
+	static str tmp4 = str_init("doc");
357
+
358
+	LM_DBG("[user]= %.*s\t[domain]= %.*s",
359
+			user->len, user->s, domain->len, domain->s);
360
+
361
+	query_cols[n_query_cols] = &tmp1;
362
+	query_vals[n_query_cols].type = DB1_STR;
363
+	query_vals[n_query_cols].nul = 0;
364
+	query_vals[n_query_cols].val.str_val = *user;
365
+	n_query_cols++;
366
+
367
+	query_cols[n_query_cols] = &tmp2;
368
+	query_vals[n_query_cols].type = DB1_STR;
369
+	query_vals[n_query_cols].nul = 0;
370
+	query_vals[n_query_cols].val.str_val = *domain;
371
+	n_query_cols++;
372
+
373
+	query_cols[n_query_cols] = &tmp3;
374
+	query_vals[n_query_cols].type = DB1_INT;
375
+	query_vals[n_query_cols].nul = 0;
376
+	query_vals[n_query_cols].val.int_val= type;
377
+	n_query_cols++;
378
+
379
+	result_cols[xcap_doc_col= n_result_cols++] = &tmp4;
380
+
381
+	if (pres_dbf.query(pres_dbh, query_cols, 0 , query_vals, result_cols,
382
+				n_query_cols, 1, 0, &result) < 0) {
383
+		LM_ERR("while querying table xcap for [user]=%.*s\t[domain]= %.*s\n",
384
+				user->len, user->s, domain->len, domain->s);
385
+		if (result)
386
+			pres_dbf.free_result(pres_dbh, result);
387
+		return -1;
388
+	}
389
+
390
+	if(result == NULL)
391
+		return -1;
392
+
393
+	if (result->n <= 0) {
394
+		LM_DBG("No document found in db table for [user]=%.*s"
395
+				"\t[domain]= %.*s\t[doc_type]= %d\n",user->len, user->s,
396
+				domain->len, domain->s, type);
397
+		pres_dbf.free_result(pres_dbh, result);
398
+		return 0;
399
+	}
400
+
401
+	row = &result->rows[xcap_doc_col];
402
+	row_vals = ROW_VALUES(row);
403
+
404
+	body.s = (char*)row_vals[0].val.string_val;
405
+	if (body.s== NULL) {
406
+		LM_ERR("Xcap doc NULL\n");
407
+		goto error;
408
+	}
409
+	body.len = strlen(body.s);
410
+	if (body.len== 0) {
411
+		LM_ERR("Xcap doc empty\n");
412
+		goto error;
413
+	}
414
+	LM_DBG("xcap document:\n%.*s", body.len,body.s);
415
+
416
+	doc= (str*)pkg_malloc(sizeof(str));
417
+	if (doc== NULL) {
418
+		ERR_MEM(PKG_MEM_STR);
419
+	}
420
+	doc->s= (char*)pkg_malloc(body.len* sizeof(char));
421
+	if (doc->s== NULL) {
422
+		pkg_free(doc);
423
+		ERR_MEM(PKG_MEM_STR);
424
+	}
425
+	memcpy(doc->s, body.s, body.len);
426
+	doc->len= body.len;
427
+
428
+	*rules_doc= doc;
429
+
430
+	if (result)
431
+		pres_dbf.free_result(pres_dbh, result);
432
+
433
+	return 0;
434
+
435
+error:
436
+	if (result)
437
+		pres_dbf.free_result(pres_dbh, result);
438
+
439
+	return -1;
440
+
441
+}
442
+
443
+
444
+/*
445
+ * Checks from presence server xcap table if watcher is authorized
446
+ * to subscribe event 'presence' of presentity.
447
+ */
448
+int xcap_auth_status(struct sip_msg* _msg, char* _sp1, char* _sp2)
449
+{
450
+	pv_spec_t *sp;
451
+	pv_value_t pv_val;
452
+	str watcher_uri, presentity_uri;
453
+	struct sip_uri uri;
454
+	str* rules_doc = NULL;
455
+	subs_t subs;
456
+	int res;
457
+
458
+	if (pres_dbh == 0) {
459
+		LM_ERR("function is disabled, to enable define pres_db_url\n");
460
+		return -1;
461
+	}
462
+
463
+	sp = (pv_spec_t *)_sp1;
464
+
465
+	if (sp && (pv_get_spec_value(_msg, sp, &pv_val) == 0)) {
466
+		if (pv_val.flags & PV_VAL_STR) {
467
+			watcher_uri = pv_val.rs;
468
+			if (watcher_uri.len == 0 || watcher_uri.s == NULL) {
469
+				LM_ERR("missing watcher uri\n");
470
+				return -1;
471
+			}
472
+		} else {
473
+			LM_ERR("watcher pseudo variable value is not string\n");
474
+			return -1;
475
+		}
476
+	} else {
477
+		LM_ERR("cannot get watcher pseudo variable value\n");
478
+		return -1;
479
+	}
480
+
481
+	sp = (pv_spec_t *)_sp2;
482
+
483
+	if (sp && (pv_get_spec_value(_msg, sp, &pv_val) == 0)) {
484
+		if (pv_val.flags & PV_VAL_STR) {
485
+			presentity_uri = pv_val.rs;
486
+			if (presentity_uri.len == 0 || presentity_uri.s == NULL) {
487
+				LM_DBG("missing presentity uri\n");
488
+				return -1;
489
+			}
490
+		} else {
491
+			LM_ERR("presentity pseudo variable value is not string\n");
492
+			return -1;
493
+		}
494
+	} else {
495
+		LM_ERR("cannot get presentity pseudo variable value\n");
496
+		return -1;
497
+	}
498
+
499
+	if (parse_uri(presentity_uri.s, presentity_uri.len, &uri) < 0) {
500
+		LM_ERR("failed to parse presentity uri\n");
501
+		return -1;
502
+	}
503
+	res = get_rules_doc(&uri.user, &uri.host, PRES_RULES, &rules_doc);
504
+	if ((res < 0) || (rules_doc == NULL) || (rules_doc->s == NULL)) {
505
+		LM_DBG("no xcap rules doc found for presentity uri\n");
506
+		return PENDING_STATUS;
507
+	}
508
+
509
+	if (parse_uri(watcher_uri.s, watcher_uri.len, &uri) < 0) {
510
+		LM_ERR("failed to parse watcher uri\n");
511
+		goto err;
512
+	}
513
+
514
+	subs.from_user = uri.user;
515
+	subs.from_domain = uri.host;
516
+	subs.pres_uri = presentity_uri;
517
+	subs.auth_rules_doc = rules_doc;
518
+	if (pres_watcher_allowed(&subs) < 0) {
519
+		LM_ERR("getting status from rules document\n");
520
+		goto err;
521
+	}
522
+	LM_DBG("auth status of watcher <%.*s> on presentity <%.*s> is %d\n",
523
+			watcher_uri.len, watcher_uri.s, presentity_uri.len, presentity_uri.s,
524
+			subs.status);
525
+	pkg_free(rules_doc->s);
526
+	pkg_free(rules_doc);
527
+	return subs.status;
528
+
529
+err:
530
+	pkg_free(rules_doc->s);
531
+	pkg_free(rules_doc);
532
+	return -1;
533
+}