xcap_auth.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. * History:
  25. * --------
  26. * 2007-04-11 initial version (anca)
  27. * 2009-06-03 util version (jh)
  28. */
  29. /*!
  30. * \file
  31. * \brief SIP-router utils ::
  32. * \ingroup utils
  33. * Module: \ref utils
  34. */
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <time.h>
  39. #include <libxml/parser.h>
  40. #include "../../str.h"
  41. #include "../../dprint.h"
  42. #include "../../pvar.h"
  43. #include "../../parser/parse_uri.h"
  44. #include "../../modules/presence/subscribe.h"
  45. #include "../../modules/presence/utils_func.h"
  46. #include "../../modules/presence/hash.h"
  47. #include "../../modules/xcap_client/xcap_callbacks.h"
  48. #include "utils.h"
  49. #include "pidf.h"
  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. uandd_to_uri(subs->from_user, subs->from_domain, &w_uri);
  63. if (w_uri.s == NULL) {
  64. LM_ERR("while creating uri\n");
  65. return NULL;
  66. }
  67. ruleset_node = xmlDocGetNodeByName(xcap_tree, "ruleset", NULL);
  68. if (ruleset_node == NULL) {
  69. LM_DBG("ruleset_node NULL\n");
  70. goto error;
  71. }
  72. for (node1 = ruleset_node->children; node1; node1 = node1->next) {
  73. if (xmlStrcasecmp(node1->name, (unsigned char*)"text") == 0)
  74. continue;
  75. /* process conditions */
  76. LM_DBG("node1->name= %s\n", node1->name);
  77. cond_node = xmlNodeGetChildByName(node1, "conditions");
  78. if(cond_node == NULL) {
  79. LM_DBG("cond node NULL\n");
  80. goto error;
  81. }
  82. LM_DBG("cond_node->name= %s\n", cond_node->name);
  83. validity_node = xmlNodeGetChildByName(cond_node, "validity");
  84. if (validity_node != NULL) {
  85. LM_DBG("found validity tag\n");
  86. t= time(NULL);
  87. /* search all from-until pair */
  88. for (time_node = validity_node->children; time_node;
  89. time_node = time_node->next) {
  90. if (xmlStrcasecmp(time_node->name, (unsigned char*)"from")!= 0)
  91. continue;
  92. time_cont= (char*)xmlNodeGetContent(time_node);
  93. t_init= xml_parse_dateTime(time_cont);
  94. xmlFree(time_cont);
  95. if (t_init< 0) {
  96. LM_ERR("failed to parse xml dateTime\n");
  97. goto error;
  98. }
  99. if (t< t_init) {
  100. LM_DBG("the lower time limit is not respected\n");
  101. continue;
  102. }
  103. time_node= time_node->next;
  104. while (1) {
  105. if (time_node == NULL) {
  106. LM_ERR("bad formatted xml doc:until child not found in"
  107. " validity pair\n");
  108. goto error;
  109. }
  110. if( xmlStrcasecmp(time_node->name,
  111. (unsigned char*)"until")== 0)
  112. break;
  113. time_node= time_node->next;
  114. }
  115. time_cont = (char*)xmlNodeGetContent(time_node);
  116. t_fin= xml_parse_dateTime(time_cont);
  117. xmlFree(time_cont);
  118. if (t_fin< 0) {
  119. LM_ERR("failed to parse xml dateTime\n");
  120. goto error;
  121. }
  122. if (t <= t_fin) {
  123. LM_DBG("the rule is active at this time\n");
  124. valid= 1;
  125. }
  126. }
  127. if (!valid) {
  128. LM_DBG("the rule is not active at this time\n");
  129. continue;
  130. }
  131. }
  132. identity_node = xmlNodeGetChildByName(cond_node, "identity");
  133. if (identity_node == NULL) {
  134. LM_ERR("didn't find identity tag\n");
  135. goto error;
  136. }
  137. iden_child = xmlNodeGetChildByName(identity_node, "one");
  138. if(iden_child) {
  139. for (node2 = identity_node->children; node2; node2 = node2->next) {
  140. if(xmlStrcasecmp(node2->name, (unsigned char*)"one")!= 0)
  141. continue;
  142. id = xmlNodeGetAttrContentByName(node2, "id");
  143. if(id== NULL) {
  144. LM_ERR("while extracting attribute\n");
  145. goto error;
  146. }
  147. if ((strlen(id)== w_uri.len &&
  148. (strncmp(id, w_uri.s, w_uri.len)==0))) {
  149. apply_rule = 1;
  150. xmlFree(id);
  151. break;
  152. }
  153. xmlFree(id);
  154. }
  155. }
  156. /* search for many node*/
  157. iden_child = xmlNodeGetChildByName(identity_node, "many");
  158. if (iden_child) {
  159. domain = NULL;
  160. for (node2 = identity_node->children; node2; node2 = node2->next) {
  161. if (xmlStrcasecmp(node2->name, (unsigned char*)"many") != 0)
  162. continue;
  163. domain = xmlNodeGetAttrContentByName(node2, "domain");
  164. if(domain == NULL) {
  165. LM_DBG("No domain attribute to many\n");
  166. } else {
  167. LM_DBG("<many domain= %s>\n", domain);
  168. if((strlen(domain)!= subs->from_domain.len &&
  169. strncmp(domain, subs->from_domain.s,
  170. subs->from_domain.len) )) {
  171. xmlFree(domain);
  172. continue;
  173. }
  174. }
  175. xmlFree(domain);
  176. apply_rule = 1;
  177. if (node2->children == NULL) /* there is no exception */
  178. break;
  179. for (except_node = node2->children; except_node;
  180. except_node= except_node->next) {
  181. if(xmlStrcasecmp(except_node->name,
  182. (unsigned char*)"except"))
  183. continue;
  184. id = xmlNodeGetAttrContentByName(except_node, "id");
  185. if (id != NULL) {
  186. if((strlen(id)- 1== w_uri.len &&
  187. (strncmp(id, w_uri.s, w_uri.len)==0))) {
  188. xmlFree(id);
  189. apply_rule = 0;
  190. break;
  191. }
  192. xmlFree(id);
  193. } else {
  194. domain = NULL;
  195. domain = xmlNodeGetAttrContentByName(except_node,
  196. "domain");
  197. if(domain!=NULL) {
  198. LM_DBG("Found except domain= %s\n- strlen(domain)= %d\n",
  199. domain, (int)strlen(domain));
  200. if (strlen(domain)==subs->from_domain.len &&
  201. (strncmp(domain,subs->from_domain.s ,
  202. subs->from_domain.len)==0)) {
  203. LM_DBG("except domain match\n");
  204. xmlFree(domain);
  205. apply_rule = 0;
  206. break;
  207. }
  208. xmlFree(domain);
  209. }
  210. }
  211. }
  212. if (apply_rule == 1) /* if a match was found no need to keep searching*/
  213. break;
  214. }
  215. }
  216. if (apply_rule ==1)
  217. break;
  218. }
  219. LM_DBG("apply_rule= %d\n", apply_rule);
  220. if(w_uri.s!=NULL)
  221. pkg_free(w_uri.s);
  222. if( !apply_rule || !node1)
  223. return NULL;
  224. return node1;
  225. error:
  226. if(w_uri.s)
  227. pkg_free(w_uri.s);
  228. return NULL;
  229. }
  230. int pres_watcher_allowed(subs_t* subs)
  231. {
  232. xmlDocPtr xcap_tree= NULL;
  233. xmlNodePtr node= NULL, actions_node = NULL;
  234. xmlNodePtr sub_handling_node = NULL;
  235. char* sub_handling = NULL;
  236. subs->status= PENDING_STATUS;
  237. subs->reason.s= NULL;
  238. subs->reason.len= 0;
  239. if (subs->auth_rules_doc== NULL)
  240. return 0;
  241. xcap_tree= xmlParseMemory(subs->auth_rules_doc->s,
  242. subs->auth_rules_doc->len);
  243. if (xcap_tree== NULL) {
  244. LM_ERR("parsing xml memory\n");
  245. return -1;
  246. }
  247. node= get_rule_node(subs, xcap_tree);
  248. if (node== NULL) {
  249. xmlFreeDoc(xcap_tree);
  250. return 0;
  251. }
  252. /* process actions */
  253. actions_node = xmlNodeGetChildByName(node, "actions");
  254. if (actions_node == NULL) {
  255. LM_DBG("actions_node NULL\n");
  256. xmlFreeDoc(xcap_tree);
  257. return 0;
  258. }
  259. LM_DBG("actions_node->name= %s\n", actions_node->name);
  260. sub_handling_node = xmlNodeGetChildByName(actions_node, "sub-handling");
  261. if (sub_handling_node== NULL) {
  262. LM_DBG("sub_handling_node NULL\n");
  263. xmlFreeDoc(xcap_tree);
  264. return 0;
  265. }
  266. sub_handling = (char*)xmlNodeGetContent(sub_handling_node);
  267. LM_DBG("sub_handling_node->name= %s\n", sub_handling_node->name);
  268. LM_DBG("sub_handling_node->content= %s\n", sub_handling);
  269. if (sub_handling == NULL) {
  270. LM_ERR("Couldn't get sub-handling content\n");
  271. xmlFreeDoc(xcap_tree);
  272. return -1;
  273. }
  274. if (strncmp((char*)sub_handling, "block", 5) == 0) {
  275. subs->status = TERMINATED_STATUS;;
  276. subs->reason.s= "rejected";
  277. subs->reason.len = 8;
  278. } else
  279. if (strncmp((char*)sub_handling, "confirm", 7) == 0) {
  280. subs->status = PENDING_STATUS;
  281. } else
  282. if (strncmp((char*)sub_handling , "polite-block", 12) == 0) {
  283. subs->status = ACTIVE_STATUS;
  284. subs->reason.s= "polite-block";
  285. subs->reason.len = 12;
  286. }
  287. else
  288. if (strncmp((char*)sub_handling, "allow", 5) == 0) {
  289. subs->status = ACTIVE_STATUS;
  290. subs->reason.s = NULL;
  291. }
  292. else {
  293. LM_ERR("unknown subscription handling action\n");
  294. xmlFreeDoc(xcap_tree);
  295. xmlFree(sub_handling);
  296. return -1;
  297. }
  298. xmlFreeDoc(xcap_tree);
  299. xmlFree(sub_handling);
  300. return 0;
  301. }
  302. int get_rules_doc(str* user, str* domain, int type, str** rules_doc)
  303. {
  304. db_key_t query_cols[5];
  305. db_val_t query_vals[5];
  306. db_key_t result_cols[3];
  307. int n_query_cols = 0;
  308. db1_res_t *result = 0;
  309. db_row_t *row;
  310. db_val_t *row_vals;
  311. str body;
  312. str* doc= NULL;
  313. int n_result_cols= 0, xcap_doc_col;
  314. static str tmp1 = str_init("username");
  315. static str tmp2 = str_init("domain");
  316. static str tmp3 = str_init("doc_type");
  317. static str tmp4 = str_init("doc");
  318. LM_DBG("[user]= %.*s\t[domain]= %.*s",
  319. user->len, user->s, domain->len, domain->s);
  320. query_cols[n_query_cols] = &tmp1;
  321. query_vals[n_query_cols].type = DB1_STR;
  322. query_vals[n_query_cols].nul = 0;
  323. query_vals[n_query_cols].val.str_val = *user;
  324. n_query_cols++;
  325. query_cols[n_query_cols] = &tmp2;
  326. query_vals[n_query_cols].type = DB1_STR;
  327. query_vals[n_query_cols].nul = 0;
  328. query_vals[n_query_cols].val.str_val = *domain;
  329. n_query_cols++;
  330. query_cols[n_query_cols] = &tmp3;
  331. query_vals[n_query_cols].type = DB1_INT;
  332. query_vals[n_query_cols].nul = 0;
  333. query_vals[n_query_cols].val.int_val= type;
  334. n_query_cols++;
  335. result_cols[xcap_doc_col= n_result_cols++] = &tmp4;
  336. if (pres_dbf.query(pres_dbh, query_cols, 0 , query_vals, result_cols,
  337. n_query_cols, 1, 0, &result) < 0) {
  338. LM_ERR("while querying table xcap for [user]=%.*s\t[domain]= %.*s\n",
  339. user->len, user->s, domain->len, domain->s);
  340. if (result)
  341. pres_dbf.free_result(pres_dbh, result);
  342. return -1;
  343. }
  344. if(result == NULL)
  345. return -1;
  346. if (result->n <= 0) {
  347. LM_DBG("No document found in db table for [user]=%.*s"
  348. "\t[domain]= %.*s\t[doc_type]= %d\n",user->len, user->s,
  349. domain->len, domain->s, type);
  350. pres_dbf.free_result(pres_dbh, result);
  351. return 0;
  352. }
  353. row = &result->rows[xcap_doc_col];
  354. row_vals = ROW_VALUES(row);
  355. body.s = (char*)row_vals[0].val.string_val;
  356. if (body.s== NULL) {
  357. LM_ERR("Xcap doc NULL\n");
  358. goto error;
  359. }
  360. body.len = strlen(body.s);
  361. if (body.len== 0) {
  362. LM_ERR("Xcap doc empty\n");
  363. goto error;
  364. }
  365. LM_DBG("xcap document:\n%.*s", body.len,body.s);
  366. doc= (str*)pkg_malloc(sizeof(str));
  367. if (doc== NULL) {
  368. ERR_MEM(PKG_MEM_STR);
  369. }
  370. doc->s= (char*)pkg_malloc(body.len* sizeof(char));
  371. if (doc->s== NULL) {
  372. pkg_free(doc);
  373. ERR_MEM(PKG_MEM_STR);
  374. }
  375. memcpy(doc->s, body.s, body.len);
  376. doc->len= body.len;
  377. *rules_doc= doc;
  378. if (result)
  379. pres_dbf.free_result(pres_dbh, result);
  380. return 0;
  381. error:
  382. if (result)
  383. pres_dbf.free_result(pres_dbh, result);
  384. return -1;
  385. }
  386. /*
  387. * Checks from presence server xcap table if watcher is authorized
  388. * to subscribe event 'presence' of presentity.
  389. */
  390. int xcap_auth_status(struct sip_msg* _msg, char* _sp1, char* _sp2)
  391. {
  392. pv_spec_t *sp;
  393. pv_value_t pv_val;
  394. str watcher_uri, presentity_uri;
  395. struct sip_uri uri;
  396. str* rules_doc = NULL;
  397. subs_t subs;
  398. int res;
  399. if (pres_dbh == 0) {
  400. LM_ERR("function is disabled, to enable define pres_db_url\n");
  401. return -1;
  402. }
  403. sp = (pv_spec_t *)_sp1;
  404. if (sp && (pv_get_spec_value(_msg, sp, &pv_val) == 0)) {
  405. if (pv_val.flags & PV_VAL_STR) {
  406. watcher_uri = pv_val.rs;
  407. if (watcher_uri.len == 0 || watcher_uri.s == NULL) {
  408. LM_ERR("missing watcher uri\n");
  409. return -1;
  410. }
  411. } else {
  412. LM_ERR("watcher pseudo variable value is not string\n");
  413. return -1;
  414. }
  415. } else {
  416. LM_ERR("cannot get watcher pseudo variable value\n");
  417. return -1;
  418. }
  419. sp = (pv_spec_t *)_sp2;
  420. if (sp && (pv_get_spec_value(_msg, sp, &pv_val) == 0)) {
  421. if (pv_val.flags & PV_VAL_STR) {
  422. presentity_uri = pv_val.rs;
  423. if (presentity_uri.len == 0 || presentity_uri.s == NULL) {
  424. LM_DBG("missing presentity uri\n");
  425. return -1;
  426. }
  427. } else {
  428. LM_ERR("presentity pseudo variable value is not string\n");
  429. return -1;
  430. }
  431. } else {
  432. LM_ERR("cannot get presentity pseudo variable value\n");
  433. return -1;
  434. }
  435. if (parse_uri(presentity_uri.s, presentity_uri.len, &uri) < 0) {
  436. LM_ERR("failed to parse presentity uri\n");
  437. return -1;
  438. }
  439. res = get_rules_doc(&uri.user, &uri.host, PRES_RULES, &rules_doc);
  440. if ((res < 0) || (rules_doc == NULL) || (rules_doc->s == NULL)) {
  441. LM_DBG("no xcap rules doc found for presentity uri\n");
  442. return PENDING_STATUS;
  443. }
  444. if (parse_uri(watcher_uri.s, watcher_uri.len, &uri) < 0) {
  445. LM_ERR("failed to parse watcher uri\n");
  446. goto err;
  447. }
  448. subs.from_user = uri.user;
  449. subs.from_domain = uri.host;
  450. subs.pres_uri = presentity_uri;
  451. subs.auth_rules_doc = rules_doc;
  452. if (pres_watcher_allowed(&subs) < 0) {
  453. LM_ERR("getting status from rules document\n");
  454. goto err;
  455. }
  456. LM_DBG("auth status of watcher <%.*s> on presentity <%.*s> is %d\n",
  457. watcher_uri.len, watcher_uri.s, presentity_uri.len, presentity_uri.s,
  458. subs.status);
  459. pkg_free(rules_doc->s);
  460. pkg_free(rules_doc);
  461. return subs.status;
  462. err:
  463. pkg_free(rules_doc->s);
  464. pkg_free(rules_doc);
  465. return -1;
  466. }