xcap_auth.c 14 KB

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