pv_xml.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2009 Daniel-Constantin Mierla (asipto.com)
  5. *
  6. * This file is part of kamailio, a free SIP server.
  7. *
  8. * Kamailio is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <libxml/tree.h>
  26. #include <libxml/parser.h>
  27. #include <libxml/xpath.h>
  28. #include <libxml/xpathInternals.h>
  29. #include "../../mem/mem.h"
  30. #include "../../parser/parse_param.h"
  31. #include "../../hashes.h"
  32. #include "../../dprint.h"
  33. #include "pv_xml.h"
  34. int pv_xml_buf_size = 4095;
  35. typedef struct _pv_xml {
  36. str docname;
  37. unsigned int docid;
  38. str inbuf;
  39. str outbuf;
  40. int updated;
  41. xmlDocPtr doc;
  42. xmlXPathContextPtr xpathCtx;
  43. xmlXPathObjectPtr xpathObj;
  44. struct _pv_xml *next;
  45. } pv_xml_t;
  46. typedef struct _pv_xml_spec {
  47. str docname;
  48. pv_xml_t *xdoc;
  49. int type;
  50. pv_elem_t *pve;
  51. } pv_xml_spec_t;
  52. pv_xml_t *_pv_xml_root = NULL;
  53. param_t *_pv_xml_ns_root = NULL;
  54. pv_xml_t *pv_xml_get_struct(str *name)
  55. {
  56. unsigned int docid;
  57. pv_xml_t *it;
  58. docid = get_hash1_raw(name->s, name->len);
  59. it = _pv_xml_root;
  60. while(it!=NULL)
  61. {
  62. if(docid == it->docid && name->len==it->docname.len
  63. && strncmp(name->s, it->docname.s, name->len)==0)
  64. {
  65. LM_DBG("doc found [%.*s]\n", name->len, name->s);
  66. return it;
  67. }
  68. it = it->next;
  69. }
  70. it = (pv_xml_t*)pkg_malloc(sizeof(pv_xml_t)+2*(pv_xml_buf_size+1));
  71. if(it==NULL)
  72. {
  73. LM_ERR("no more pkg\n");
  74. return NULL;
  75. }
  76. memset(it, 0, sizeof(pv_xml_t)+2*(pv_xml_buf_size+1));
  77. it->docid = docid;
  78. it->docname = *name;
  79. it->inbuf.s = (char*)it + sizeof(pv_xml_t);
  80. it->outbuf.s = it->inbuf.s + pv_xml_buf_size+1;
  81. it->next = _pv_xml_root;
  82. _pv_xml_root = it;
  83. return it;
  84. }
  85. int pv_xpath_nodes_eval(pv_xml_t *xdoc)
  86. {
  87. int size;
  88. int i;
  89. xmlNodeSetPtr nodes;
  90. char *p;
  91. xmlChar *keyword;
  92. xmlBufferPtr psBuf;
  93. if(xdoc==NULL || xdoc->doc==NULL || xdoc->xpathCtx==NULL
  94. || xdoc->xpathObj==NULL)
  95. return -1;
  96. nodes = xdoc->xpathObj->nodesetval;
  97. if(nodes==NULL)
  98. {
  99. xdoc->outbuf.len = 0;
  100. xdoc->outbuf.s[xdoc->outbuf.len] = '\0';
  101. return 0;
  102. }
  103. size = nodes->nodeNr;
  104. p = xdoc->outbuf.s;
  105. for(i = 0; i < size; ++i)
  106. {
  107. if(nodes->nodeTab[i]==NULL)
  108. continue;
  109. if(i!=0)
  110. {
  111. *p = ',';
  112. p++;
  113. }
  114. if(nodes->nodeTab[i]->type == XML_ATTRIBUTE_NODE)
  115. {
  116. keyword = xmlNodeListGetString(xdoc->doc,
  117. nodes->nodeTab[i]->children, 0);
  118. if(keyword != NULL)
  119. {
  120. strcpy(p, (char*)keyword);
  121. p += strlen((char*)keyword);
  122. xmlFree(keyword);
  123. keyword = NULL;
  124. }
  125. } else {
  126. if(nodes->nodeTab[i]->content!=NULL)
  127. {
  128. strcpy(p, (char*)nodes->nodeTab[i]->content);
  129. p += strlen((char*)nodes->nodeTab[i]->content);
  130. } else {
  131. psBuf = xmlBufferCreate();
  132. if(psBuf != NULL && xmlNodeDump(psBuf, xdoc->doc,
  133. nodes->nodeTab[i], 0, 0)>0)
  134. {
  135. strcpy(p, (char*)xmlBufferContent(psBuf));
  136. p += strlen((char*)xmlBufferContent(psBuf));
  137. }
  138. if(psBuf != NULL) xmlBufferFree(psBuf);
  139. psBuf = NULL;
  140. }
  141. }
  142. }
  143. xdoc->outbuf.len = p - xdoc->outbuf.s;
  144. xdoc->outbuf.s[xdoc->outbuf.len] = '\0';
  145. return 0;
  146. }
  147. int pv_xpath_nodes_update(pv_xml_t *xdoc, str *val)
  148. {
  149. xmlNodeSetPtr nodes;
  150. const xmlChar* value;
  151. int size;
  152. int i;
  153. if(xdoc==NULL || xdoc->doc==NULL || xdoc->xpathCtx==NULL
  154. || xdoc->xpathObj==NULL || val==NULL)
  155. return -1;
  156. if(val->len>pv_xml_buf_size)
  157. {
  158. LM_ERR("internal buffer overflow - %d\n", val->len);
  159. return -1;
  160. }
  161. nodes = xdoc->xpathObj->nodesetval;
  162. if(nodes==NULL)
  163. return 0;
  164. size = nodes->nodeNr;
  165. value = (const xmlChar*)xdoc->outbuf.s;
  166. memcpy(xdoc->outbuf.s, val->s, val->len);
  167. xdoc->outbuf.s[val->len] = '\0';
  168. xdoc->outbuf.len = val->len;
  169. /*
  170. * NOTE: the nodes are processed in reverse order, i.e. reverse document
  171. * order because xmlNodeSetContent can actually free up descendant
  172. * of the node and such nodes may have been selected too ! Handling
  173. * in reverse order ensure that descendant are accessed first, before
  174. * they get removed. Mixing XPath and modifications on a tree must be
  175. * done carefully !
  176. */
  177. for(i = size - 1; i >= 0; i--) {
  178. if(nodes->nodeTab[i]==NULL)
  179. continue;
  180. xmlNodeSetContent(nodes->nodeTab[i], value);
  181. /*
  182. * All the elements returned by an XPath query are pointers to
  183. * elements from the tree *except* namespace nodes where the XPath
  184. * semantic is different from the implementation in libxml2 tree.
  185. * As a result when a returned node set is freed when
  186. * xmlXPathFreeObject() is called, that routine must check the
  187. * element type. But node from the returned set may have been removed
  188. * by xmlNodeSetContent() resulting in access to freed data.
  189. * This can be exercised by running
  190. * valgrind xpath2 test3.xml '//discarded' discarded
  191. * There is 2 ways around it:
  192. * - make a copy of the pointers to the nodes from the result set
  193. * then call xmlXPathFreeObject() and then modify the nodes
  194. * or
  195. * - remove the reference to the modified nodes from the node set
  196. * as they are processed, if they are not namespace nodes.
  197. */
  198. if (nodes->nodeTab[i]->type != XML_NAMESPACE_DECL)
  199. nodes->nodeTab[i] = NULL;
  200. }
  201. xdoc->outbuf.s[0] = '\0';
  202. xdoc->outbuf.len = 0;
  203. return 0;
  204. }
  205. void pv_xml_register_ns(xmlXPathContextPtr xpathCtx)
  206. {
  207. param_t *ns;
  208. ns = _pv_xml_ns_root;
  209. while(ns) {
  210. xmlXPathRegisterNs(xpathCtx, (xmlChar*)ns->name.s,
  211. (xmlChar*)ns->body.s);
  212. ns = ns->next;
  213. }
  214. }
  215. int pv_get_xml(struct sip_msg *msg, pv_param_t *param,
  216. pv_value_t *res)
  217. {
  218. pv_xml_spec_t *pxs = NULL;
  219. str xpaths;
  220. int size = 0;
  221. xmlChar *xmem = NULL;
  222. pxs = (pv_xml_spec_t*)param->pvn.u.dname;
  223. if(pxs->xdoc==NULL)
  224. return -1;
  225. switch(pxs->type) {
  226. case 0:
  227. /* get document */
  228. if(pxs->xdoc->inbuf.len<=0)
  229. return pv_get_null(msg, param, res);
  230. if(pxs->xdoc->doc == NULL || pxs->xdoc->updated == 0)
  231. return pv_get_strval(msg, param, res, &pxs->xdoc->inbuf);
  232. xmlDocDumpMemory(pxs->xdoc->doc, &xmem, &size);
  233. if(xmem!=NULL)
  234. {
  235. if(size>pv_xml_buf_size)
  236. {
  237. xmlFree(xmem);
  238. return pv_get_null(msg, param, res);
  239. }
  240. memcpy(pxs->xdoc->outbuf.s, xmem, size);
  241. pxs->xdoc->outbuf.s[size] = '\0';
  242. pxs->xdoc->outbuf.len = size;
  243. xmlFree(xmem);
  244. return pv_get_strval(msg, param, res, &pxs->xdoc->outbuf);
  245. }
  246. return pv_get_null(msg, param, res);
  247. break;
  248. case 1:
  249. /* get xpath element */
  250. if(pxs->xdoc->doc == NULL)
  251. {
  252. if(pxs->xdoc->inbuf.len<=0)
  253. return pv_get_null(msg, param, res);
  254. pxs->xdoc->doc = xmlParseMemory(pxs->xdoc->inbuf.s,
  255. pxs->xdoc->inbuf.len);
  256. if(pxs->xdoc->doc == NULL)
  257. return pv_get_null(msg, param, res);
  258. }
  259. if(pxs->xdoc->xpathCtx == NULL)
  260. {
  261. pxs->xdoc->xpathCtx = xmlXPathNewContext(pxs->xdoc->doc);
  262. if(pxs->xdoc->xpathCtx == NULL)
  263. {
  264. LM_ERR("unable to create new XPath context\n");
  265. xmlFreeDoc(pxs->xdoc->doc);
  266. pxs->xdoc->doc = NULL;
  267. return pv_get_null(msg, param, res);
  268. }
  269. }
  270. if(pv_printf_s(msg, pxs->pve, &xpaths)!=0)
  271. {
  272. LM_ERR("cannot get xpath string\n");
  273. return pv_get_null(msg, param, res);
  274. }
  275. /* Evaluate xpath expression */
  276. pv_xml_register_ns(pxs->xdoc->xpathCtx);
  277. pxs->xdoc->xpathObj = xmlXPathEvalExpression(
  278. (const xmlChar*)xpaths.s, pxs->xdoc->xpathCtx);
  279. if(pxs->xdoc->xpathObj == NULL)
  280. {
  281. LM_ERR("unable to evaluate xpath expression [%s/%d]\n",
  282. xpaths.s, xpaths.len);
  283. xmlXPathFreeContext(pxs->xdoc->xpathCtx);
  284. xmlFreeDoc(pxs->xdoc->doc);
  285. pxs->xdoc->xpathCtx = NULL;
  286. pxs->xdoc->doc = NULL;
  287. return pv_get_null(msg, param, res);
  288. }
  289. /* Print results */
  290. if(pv_xpath_nodes_eval(pxs->xdoc)<0)
  291. {
  292. xmlXPathFreeObject(pxs->xdoc->xpathObj);
  293. xmlXPathFreeContext(pxs->xdoc->xpathCtx);
  294. xmlFreeDoc(pxs->xdoc->doc);
  295. pxs->xdoc->xpathObj = NULL;
  296. pxs->xdoc->xpathCtx = NULL;
  297. pxs->xdoc->doc = NULL;
  298. return pv_get_null(msg, param, res);
  299. }
  300. xmlXPathFreeObject(pxs->xdoc->xpathObj);
  301. pxs->xdoc->xpathObj = NULL;
  302. return pv_get_strval(msg, param, res, &pxs->xdoc->outbuf);
  303. break;
  304. default:
  305. return pv_get_null(msg, param, res);
  306. }
  307. return pv_get_null(msg, param, res);
  308. }
  309. int pv_set_xml(struct sip_msg* msg, pv_param_t *param,
  310. int op, pv_value_t *val)
  311. {
  312. pv_xml_spec_t *pxs = NULL;
  313. str xpaths;
  314. pxs = (pv_xml_spec_t*)param->pvn.u.dname;
  315. if(pxs->xdoc==NULL)
  316. return -1;
  317. if(!(val->flags&PV_VAL_STR))
  318. return -1;
  319. switch(pxs->type) {
  320. case 0:
  321. /* set document */
  322. if(pxs->xdoc->doc!=NULL)
  323. {
  324. if(pxs->xdoc->xpathCtx!=NULL)
  325. {
  326. xmlXPathFreeContext(pxs->xdoc->xpathCtx);
  327. pxs->xdoc->xpathCtx = NULL;
  328. }
  329. xmlFreeDoc(pxs->xdoc->doc);
  330. pxs->xdoc->doc = NULL;
  331. }
  332. if(val->rs.len>pv_xml_buf_size)
  333. {
  334. LM_ERR("local buffer overflow - %d\n", val->rs.len);
  335. return -1;
  336. }
  337. memcpy(pxs->xdoc->inbuf.s, val->rs.s, val->rs.len);
  338. pxs->xdoc->inbuf.s[val->rs.len] = '\0';
  339. pxs->xdoc->inbuf.len = val->rs.len;
  340. pxs->xdoc->updated = 0;
  341. return 0;
  342. break;
  343. case 1:
  344. /* set xpath element */
  345. if(pxs->xdoc->doc == NULL)
  346. {
  347. if(pxs->xdoc->inbuf.len<=0)
  348. return -1;
  349. pxs->xdoc->doc = xmlParseMemory(pxs->xdoc->inbuf.s,
  350. pxs->xdoc->inbuf.len);
  351. if(pxs->xdoc->doc == NULL)
  352. return -1;
  353. }
  354. if(pxs->xdoc->xpathCtx == NULL)
  355. {
  356. pxs->xdoc->xpathCtx = xmlXPathNewContext(pxs->xdoc->doc);
  357. if(pxs->xdoc->xpathCtx == NULL)
  358. {
  359. LM_ERR("unable to create new XPath context\n");
  360. xmlFreeDoc(pxs->xdoc->doc);
  361. pxs->xdoc->doc = NULL;
  362. return -1;
  363. }
  364. }
  365. if(pv_printf_s(msg, pxs->pve, &xpaths)!=0)
  366. {
  367. LM_ERR("cannot get xpath string\n");
  368. return -1;
  369. }
  370. /* Evaluate xpath expression */
  371. pxs->xdoc->xpathObj = xmlXPathEvalExpression(
  372. (const xmlChar*)xpaths.s, pxs->xdoc->xpathCtx);
  373. if(pxs->xdoc->xpathObj == NULL)
  374. {
  375. LM_ERR("unable to evaluate xpath expression [%s]\n", xpaths.s);
  376. xmlXPathFreeContext(pxs->xdoc->xpathCtx);
  377. xmlFreeDoc(pxs->xdoc->doc);
  378. pxs->xdoc->xpathCtx = NULL;
  379. pxs->xdoc->doc = NULL;
  380. return -1;
  381. }
  382. /* Set value */
  383. if(pv_xpath_nodes_update(pxs->xdoc, &val->rs)<0)
  384. {
  385. LM_ERR("unable to update xpath [%s] - [%.*s]\n", xpaths.s,
  386. val->rs.len, val->rs.s);
  387. xmlXPathFreeObject(pxs->xdoc->xpathObj);
  388. xmlXPathFreeContext(pxs->xdoc->xpathCtx);
  389. xmlFreeDoc(pxs->xdoc->doc);
  390. pxs->xdoc->xpathObj = NULL;
  391. pxs->xdoc->xpathCtx = NULL;
  392. pxs->xdoc->doc = NULL;
  393. return -1;
  394. }
  395. pxs->xdoc->updated = 1;
  396. xmlXPathFreeObject(pxs->xdoc->xpathObj);
  397. pxs->xdoc->xpathObj = NULL;
  398. return 0;
  399. break;
  400. default:
  401. return -1;
  402. }
  403. return 0;
  404. }
  405. int pv_parse_xml_name(pv_spec_p sp, str *in)
  406. {
  407. pv_xml_spec_t *pxs = NULL;
  408. char *p;
  409. str pvs;
  410. if(in->s==NULL || in->len<=0)
  411. return -1;
  412. pxs = (pv_xml_spec_t*)pkg_malloc(sizeof(pv_xml_spec_t));
  413. if(pxs==NULL)
  414. return -1;
  415. memset(pxs, 0, sizeof(pv_xml_spec_t));
  416. p = in->s;
  417. while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  418. p++;
  419. if(p>in->s+in->len || *p=='\0')
  420. goto error;
  421. pxs->docname.s = p;
  422. while(p < in->s + in->len)
  423. {
  424. if(*p=='=' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r')
  425. break;
  426. p++;
  427. }
  428. if(p>in->s+in->len || *p=='\0')
  429. goto error;
  430. pxs->docname.len = p - pxs->docname.s;
  431. if(*p!='=')
  432. {
  433. while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  434. p++;
  435. if(p>in->s+in->len || *p=='\0' || *p!='=')
  436. goto error;
  437. }
  438. p++;
  439. if(*p!='>')
  440. goto error;
  441. p++;
  442. pvs.len = in->len - (int)(p - in->s);
  443. pvs.s = p;
  444. LM_DBG("xmldoc [%.*s] - key [%.*s]\n", pxs->docname.len, pxs->docname.s,
  445. pvs.len, pvs.s);
  446. if(pvs.len>=3 && strncmp(pvs.s, "doc", 3)==0) {
  447. pxs->type = 0;
  448. } else if(pvs.len>6 && strncmp(pvs.s, "xpath:", 6)==0) {
  449. pvs.s += 6;
  450. pvs.len -= 6;
  451. pxs->type = 1;
  452. LM_DBG("*** xpath expr [%.*s]\n", pvs.len, pvs.s);
  453. if(pv_parse_format(&pvs, &pxs->pve)<0 || pxs->pve==NULL)
  454. {
  455. LM_ERR("wrong xpath format [%.*s]\n", in->len, in->s);
  456. goto error;
  457. }
  458. } else {
  459. LM_ERR("unknown key type [%.*s]\n", in->len, in->s);
  460. goto error;
  461. }
  462. pxs->xdoc = pv_xml_get_struct(&pxs->docname);
  463. sp->pvp.pvn.u.dname = (void*)pxs;
  464. sp->pvp.pvn.type = PV_NAME_OTHER;
  465. return 0;
  466. error:
  467. if(pxs!=NULL)
  468. pkg_free(pxs);
  469. return -1;
  470. }
  471. int pv_xml_ns_param(modparam_t type, void *val)
  472. {
  473. char *p;
  474. param_t *ns;
  475. if(val==NULL)
  476. goto error;
  477. ns = (param_t*)pkg_malloc(sizeof(param_t));
  478. if(ns==NULL)
  479. {
  480. LM_ERR("no more pkg\n");
  481. goto error;
  482. }
  483. memset(ns, 0, sizeof(param_t));
  484. p = strchr((const char*)val, '=');
  485. if(p==NULL)
  486. {
  487. ns->name.s = "";
  488. ns->body.s = (char*)val;
  489. ns->body.len = strlen(ns->body.s);
  490. } else {
  491. *p = 0;
  492. p++;
  493. ns->name.s = (char*)val;
  494. ns->name.len = strlen(ns->name.s);
  495. ns->body.s = p;
  496. ns->body.len = strlen(ns->body.s);
  497. }
  498. ns->next = _pv_xml_ns_root;
  499. _pv_xml_ns_root = ns;
  500. return 0;
  501. error:
  502. return -1;
  503. }