xcap_client.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * $Id: xcap_client.c 2230 2007-06-06 07:13:20Z anca_vamanu $
  3. *
  4. * xcap_client module - XCAP client for Kamailio
  5. *
  6. * Copyright (C) 2007 Voice Sistem S.R.L.
  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-08-20 initial version (anca)
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <sys/types.h>
  32. #include <sys/ipc.h>
  33. #include <unistd.h>
  34. #include <fcntl.h>
  35. #include <time.h>
  36. #include <curl/curl.h>
  37. #include "../../pt.h"
  38. #include "../../lib/srdb1/db.h"
  39. #include "../../sr_module.h"
  40. #include "../../dprint.h"
  41. #include "../../error.h"
  42. #include "../../ut.h"
  43. #include "../../mem/mem.h"
  44. #include "../../mem/shm_mem.h"
  45. #include "../../lib/kmi/mi.h"
  46. #include "../presence/utils_func.h"
  47. #include "xcap_functions.h"
  48. #include "xcap_client.h"
  49. MODULE_VERSION
  50. #define XCAP_TABLE_VERSION 4
  51. static int mod_init(void);
  52. static int child_init(int rank);
  53. static void destroy(void);
  54. struct mi_root* refreshXcapDoc(struct mi_root* cmd, void* param);
  55. int get_auid_flag(str auid);
  56. str xcap_db_table = str_init("xcap");
  57. str xcap_db_url = str_init(DEFAULT_DB_URL);
  58. xcap_callback_t* xcapcb_list= NULL;
  59. int periodical_query= 1;
  60. unsigned int query_period= 100;
  61. str str_source_col = str_init("source");
  62. str str_path_col = str_init("path");
  63. str str_doc_col = str_init("doc");
  64. str str_etag_col = str_init("etag");
  65. str str_username_col = str_init("username");
  66. str str_domain_col = str_init("domain");
  67. str str_doc_type_col = str_init("doc_type");
  68. str str_doc_uri_col = str_init("doc_uri");
  69. str str_port_col = str_init("port");
  70. /* database connection */
  71. db1_con_t *xcap_db = NULL;
  72. db_func_t xcap_dbf;
  73. void query_xcap_update(unsigned int ticks, void* param);
  74. static param_export_t params[]={
  75. { "db_url", PARAM_STR, &xcap_db_url },
  76. { "xcap_table", PARAM_STR, &xcap_db_table },
  77. { "periodical_query", INT_PARAM, &periodical_query },
  78. { "query_period", INT_PARAM, &query_period },
  79. { 0, 0, 0 }
  80. };
  81. static cmd_export_t cmds[]=
  82. {
  83. {"bind_xcap", (cmd_function)bind_xcap, 1, 0, 0, 0},
  84. { 0, 0, 0, 0, 0, 0}
  85. };
  86. static mi_export_t mi_cmds[] = {
  87. { "refreshXcapDoc", refreshXcapDoc, 0, 0, 0},
  88. { 0, 0, 0, 0, 0}
  89. };
  90. /** module exports */
  91. struct module_exports exports= {
  92. "xcap_client", /* module name */
  93. DEFAULT_DLFLAGS, /* dlopen flags */
  94. cmds, /* exported functions */
  95. params, /* exported parameters */
  96. 0, /* exported statistics */
  97. mi_cmds, /* exported MI functions */
  98. 0, /* exported pseudo-variables */
  99. 0, /* extra processes */
  100. mod_init, /* module initialization function */
  101. 0, /* response handling function */
  102. (destroy_function) destroy, /* destroy function */
  103. child_init /* per-child init function */
  104. };
  105. /**
  106. * init module function
  107. */
  108. static int mod_init(void)
  109. {
  110. if(register_mi_mod(exports.name, mi_cmds)!=0)
  111. {
  112. LM_ERR("failed to register MI commands\n");
  113. return -1;
  114. }
  115. /* binding to mysql module */
  116. if (db_bind_mod(&xcap_db_url, &xcap_dbf))
  117. {
  118. LM_ERR("Database module not found\n");
  119. return -1;
  120. }
  121. if (!DB_CAPABILITY(xcap_dbf, DB_CAP_ALL)) {
  122. LM_ERR("Database module does not implement all functions"
  123. " needed by the module\n");
  124. return -1;
  125. }
  126. xcap_db = xcap_dbf.init(&xcap_db_url);
  127. if (!xcap_db)
  128. {
  129. LM_ERR("while connecting to database\n");
  130. return -1;
  131. }
  132. if(db_check_table_version(&xcap_dbf, xcap_db, &xcap_db_table,
  133. XCAP_TABLE_VERSION) < 0) {
  134. LM_ERR("error during table version check.\n");
  135. return -1;
  136. }
  137. xcap_dbf.close(xcap_db);
  138. xcap_db = NULL;
  139. curl_global_init(CURL_GLOBAL_ALL);
  140. if(periodical_query)
  141. {
  142. register_timer(query_xcap_update, 0, query_period);
  143. }
  144. return 0;
  145. }
  146. static int child_init(int rank)
  147. {
  148. if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
  149. return 0; /* do nothing for the main process */
  150. if((xcap_db = xcap_dbf.init(&xcap_db_url))==NULL)
  151. {
  152. LM_ERR("cannot connect to db\n");
  153. return -1;
  154. }
  155. return 0;
  156. }
  157. static void destroy(void)
  158. {
  159. curl_global_cleanup();
  160. if(xcap_db != NULL)
  161. xcap_dbf.close(xcap_db);
  162. }
  163. void query_xcap_update(unsigned int ticks, void* param)
  164. {
  165. db_key_t query_cols[3], update_cols[3];
  166. db_val_t query_vals[3], update_vals[3];
  167. db_key_t result_cols[7];
  168. int n_result_cols = 0, n_query_cols= 0, n_update_cols= 0;
  169. db1_res_t* result= NULL;
  170. int user_col, domain_col, doc_type_col, etag_col, doc_uri_col, port_col;
  171. db_row_t *row ;
  172. db_val_t *row_vals ;
  173. unsigned int port;
  174. char* etag, *path, *new_etag= NULL, *doc= NULL;
  175. int u_doc_col, u_etag_col;
  176. str user, domain, uri;
  177. int i;
  178. /* query the ones I have to handle */
  179. query_cols[n_query_cols] = &str_source_col;
  180. query_vals[n_query_cols].type = DB1_INT;
  181. query_vals[n_query_cols].nul = 0;
  182. query_vals[n_query_cols].val.int_val= XCAP_CL_MOD;
  183. n_query_cols++;
  184. query_cols[n_query_cols] = &str_path_col;
  185. query_vals[n_query_cols].type = DB1_STR;
  186. query_vals[n_query_cols].nul = 0;
  187. update_cols[u_doc_col=n_update_cols] = &str_doc_col;
  188. update_vals[n_update_cols].type = DB1_STRING;
  189. update_vals[n_update_cols].nul = 0;
  190. n_update_cols++;
  191. update_cols[u_etag_col=n_update_cols] = &str_etag_col;
  192. update_vals[n_update_cols].type = DB1_STRING;
  193. update_vals[n_update_cols].nul = 0;
  194. n_update_cols++;
  195. result_cols[user_col= n_result_cols++] = &str_username_col;
  196. result_cols[domain_col=n_result_cols++] = &str_domain_col;
  197. result_cols[doc_type_col=n_result_cols++] = &str_doc_type_col;
  198. result_cols[etag_col=n_result_cols++] = &str_etag_col;
  199. result_cols[doc_uri_col= n_result_cols++] = &str_doc_uri_col;
  200. result_cols[port_col= n_result_cols++] = &str_port_col;
  201. if (xcap_dbf.use_table(xcap_db, &xcap_db_table) < 0)
  202. {
  203. LM_ERR("in use_table-[table]= %.*s\n", xcap_db_table.len, xcap_db_table.s);
  204. goto error;
  205. }
  206. if(xcap_dbf.query(xcap_db, query_cols, 0, query_vals, result_cols, 1,
  207. n_result_cols, 0, &result)< 0)
  208. {
  209. LM_ERR("in sql query\n");
  210. goto error;
  211. }
  212. if(result== NULL)
  213. {
  214. LM_ERR("in sql query- null result\n");
  215. return;
  216. }
  217. if(result->n<= 0)
  218. {
  219. xcap_dbf.free_result(xcap_db, result);
  220. return;
  221. }
  222. n_query_cols++;
  223. /* ask if updated */
  224. for(i= 0; i< result->n; i++)
  225. {
  226. row = &result->rows[i];
  227. row_vals = ROW_VALUES(row);
  228. path= (char*)row_vals[doc_uri_col].val.string_val;
  229. port= row_vals[port_col].val.int_val;
  230. etag= (char*)row_vals[etag_col].val.string_val;
  231. user.s= (char*)row_vals[user_col].val.string_val;
  232. user.len= strlen(user.s);
  233. domain.s= (char*)row_vals[domain_col].val.string_val;
  234. domain.len= strlen(domain.s);
  235. /* send HTTP request */
  236. doc= send_http_get(path, port, etag, IF_NONE_MATCH, &new_etag);
  237. if(doc== NULL)
  238. {
  239. LM_DBG("document not update\n");
  240. continue;
  241. }
  242. if(new_etag== NULL)
  243. {
  244. LM_ERR("etag not found\n");
  245. pkg_free(doc);
  246. goto error;
  247. }
  248. /* update in xcap db table */
  249. update_vals[u_doc_col].val.string_val= doc;
  250. update_vals[u_etag_col].val.string_val= etag;
  251. if(xcap_dbf.update(xcap_db, query_cols, 0, query_vals, update_cols,
  252. update_vals, n_query_cols, n_update_cols)< 0)
  253. {
  254. LM_ERR("in sql update\n");
  255. pkg_free(doc);
  256. goto error;
  257. }
  258. /* call registered callbacks */
  259. if(uandd_to_uri(user, domain, &uri)< 0)
  260. {
  261. LM_ERR("converting user and domain to uri\n");
  262. pkg_free(doc);
  263. goto error;
  264. }
  265. run_xcap_update_cb(row_vals[doc_type_col].val.int_val, uri, doc);
  266. pkg_free(doc);
  267. }
  268. xcap_dbf.free_result(xcap_db, result);
  269. return;
  270. error:
  271. if(result)
  272. xcap_dbf.free_result(xcap_db, result);
  273. }
  274. int parse_doc_url(str doc_url, char** serv_addr, xcap_doc_sel_t* doc_sel)
  275. {
  276. char* sl, *str_type;
  277. sl= strchr(doc_url.s, '/');
  278. *sl= '\0';
  279. *serv_addr= doc_url.s;
  280. sl++;
  281. doc_sel->auid.s= sl;
  282. sl= strchr(sl, '/');
  283. doc_sel->auid.len= sl- doc_sel->auid.s;
  284. sl++;
  285. str_type= sl;
  286. sl= strchr(sl, '/');
  287. *sl= '\0';
  288. if(strcasecmp(str_type, "users")== 0)
  289. doc_sel->type= USERS_TYPE;
  290. else
  291. if(strcasecmp(str_type, "group")== 0)
  292. doc_sel->type= GLOBAL_TYPE;
  293. sl++;
  294. return 0;
  295. }
  296. /*
  297. * mi cmd: refreshXcapDoc
  298. * <document uri>
  299. * <xcap_port>
  300. * */
  301. struct mi_root* refreshXcapDoc(struct mi_root* cmd, void* param)
  302. {
  303. struct mi_node* node= NULL;
  304. str doc_url;
  305. xcap_doc_sel_t doc_sel;
  306. char* serv_addr;
  307. char* stream= NULL;
  308. int type;
  309. unsigned int xcap_port;
  310. char* etag= NULL;
  311. node = cmd->node.kids;
  312. if(node == NULL)
  313. return 0;
  314. doc_url = node->value;
  315. if(doc_url.s == NULL || doc_url.len== 0)
  316. {
  317. LM_ERR("empty uri\n");
  318. return init_mi_tree(404, "Empty document URL", 20);
  319. }
  320. node= node->next;
  321. if(node== NULL)
  322. return 0;
  323. if(node->value.s== NULL || node->value.len== 0)
  324. {
  325. LM_ERR("port number\n");
  326. return init_mi_tree(404, "Empty document URL", 20);
  327. }
  328. if(str2int(&node->value, &xcap_port)< 0)
  329. {
  330. LM_ERR("while converting string to int\n");
  331. goto error;
  332. }
  333. if(node->next!= NULL)
  334. return 0;
  335. /* send GET HTTP request to the server */
  336. stream= send_http_get(doc_url.s, xcap_port, NULL, 0, &etag);
  337. if(stream== NULL)
  338. {
  339. LM_ERR("in http get\n");
  340. return 0;
  341. }
  342. /* call registered functions with document argument */
  343. if(parse_doc_url(doc_url, &serv_addr, &doc_sel)< 0)
  344. {
  345. LM_ERR("parsing document url\n");
  346. return 0;
  347. }
  348. type= get_auid_flag(doc_sel.auid);
  349. if(type< 0)
  350. {
  351. LM_ERR("incorect auid: %.*s\n",
  352. doc_sel.auid.len, doc_sel.auid.s);
  353. goto error;
  354. }
  355. run_xcap_update_cb(type, doc_sel.xid, stream);
  356. return init_mi_tree(200, "OK", 2);
  357. error:
  358. if(stream)
  359. pkg_free(stream);
  360. return 0;
  361. }
  362. #define STR_MATCH(s1, s2) ((s1).len==(s2).len && memcmp((s1).s, (s2).s, (s1).len)==0)
  363. int get_auid_flag(str auid)
  364. {
  365. static str pres_rules = str_init("pres-rules");
  366. static str rls_services = str_init("rls-services");
  367. if (STR_MATCH(auid, pres_rules))
  368. return PRES_RULES;
  369. else if (STR_MATCH(auid, rls_services))
  370. return RESOURCE_LIST;
  371. return -1;
  372. }