parse_common_rules.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (C) 2005 iptelorg GmbH
  3. *
  4. * This file is part of ser, a free SIP server.
  5. *
  6. * ser is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * For a license to use the ser software under conditions
  12. * other than those described here, or to purchase support for this
  13. * software, please contact iptel.org by e-mail at the following addresses:
  14. * [email protected]
  15. *
  16. * ser is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include <xcap/parse_common_rules.h>
  26. #include <xcap/xcap_result_codes.h>
  27. #include <cds/dstring.h>
  28. #include <cds/memory.h>
  29. #include <cds/logger.h>
  30. #include <cds/list.h>
  31. #include <string.h>
  32. #include <xcap/xml_utils.h>
  33. char *common_policy_ns = NULL;
  34. static int read_sphere(xmlNode *n, cp_sphere_t **dst)
  35. {
  36. *dst = (cp_sphere_t*)cds_malloc(sizeof(cp_sphere_t));
  37. if (!(*dst)) return RES_MEMORY_ERR;
  38. memset(*dst, 0, sizeof(**dst));
  39. (*dst)->next = NULL;
  40. str_dup_zt(&(*dst)->value, get_node_value(n));
  41. return RES_OK;
  42. }
  43. static int read_validity(xmlNode *n, cp_validity_t **dst)
  44. {
  45. const char *from, *to;
  46. *dst = (cp_validity_t*)cds_malloc(sizeof(cp_validity_t));
  47. if (!(*dst)) return RES_MEMORY_ERR;
  48. memset(*dst, 0, sizeof(**dst));
  49. from = get_node_value(find_node(n, "from", common_policy_ns));
  50. to = get_node_value(find_node(n, "to", common_policy_ns));
  51. (*dst)->from = xmltime2time(from);
  52. (*dst)->to = xmltime2time(to);
  53. return RES_OK;
  54. }
  55. static int read_id(xmlNode *n, cp_id_t **dst)
  56. {
  57. *dst = (cp_id_t*)cds_malloc(sizeof(cp_id_t));
  58. if (!(*dst)) return RES_MEMORY_ERR;
  59. memset(*dst, 0, sizeof(**dst));
  60. (*dst)->next = NULL;
  61. get_str_attr(n, "entity", &(*dst)->entity);
  62. if ((*dst)->entity.len == 0) {
  63. /* hack - eyeBeams format differs from draft ! */
  64. str_dup_zt(&(*dst)->entity, get_node_value(n));
  65. }
  66. return RES_OK;
  67. }
  68. static int read_domain(xmlNode *n, cp_domain_t **dst)
  69. {
  70. *dst = (cp_domain_t*)cds_malloc(sizeof(cp_domain_t));
  71. if (!(*dst)) return RES_MEMORY_ERR;
  72. memset(*dst, 0, sizeof(**dst));
  73. (*dst)->next = NULL;
  74. get_str_attr(n, "domain", &(*dst)->domain);
  75. return RES_OK;
  76. }
  77. static int read_except(xmlNode *n, cp_except_t **dst)
  78. {
  79. *dst = (cp_except_t*)cds_malloc(sizeof(cp_except_t));
  80. if (!(*dst)) return RES_MEMORY_ERR;
  81. memset(*dst, 0, sizeof(**dst));
  82. (*dst)->next = NULL;
  83. get_str_attr(n, "entity", &(*dst)->entity);
  84. return RES_OK;
  85. }
  86. static int read_except_domain(xmlNode *n, cp_except_domain_t **dst)
  87. {
  88. *dst = (cp_except_domain_t*)cds_malloc(sizeof(cp_except_domain_t));
  89. if (!*dst) return RES_MEMORY_ERR;
  90. memset(*dst, 0, sizeof(**dst));
  91. (*dst)->next = NULL;
  92. get_str_attr(n, "domain", &(*dst)->domain);
  93. return RES_OK;
  94. }
  95. static int read_any_identity(xmlNode *an, cp_any_identity_t **dst)
  96. {
  97. cp_domain_t *domain, *last_domain = NULL;
  98. cp_except_domain_t *except, *last_except = NULL;
  99. xmlNode *n;
  100. int res = RES_OK;
  101. *dst = (cp_any_identity_t*)cds_malloc(sizeof(cp_any_identity_t));
  102. if (!*dst) return RES_MEMORY_ERR;
  103. memset(*dst, 0, sizeof(**dst));
  104. n = an->children;
  105. while (n) {
  106. if (n->type == XML_ELEMENT_NODE) {
  107. if (cmp_node(n, "domain", common_policy_ns) >= 0) {
  108. res = read_domain(n, &domain);
  109. if (res != 0) break;
  110. LINKED_LIST_ADD((*dst)->domains, last_domain, domain);
  111. }
  112. else if (cmp_node(n, "except-domain", common_policy_ns) >= 0) {
  113. res = read_except_domain(n, &except);
  114. if (res != 0) break;
  115. LINKED_LIST_ADD((*dst)->except_domains, last_except, except);
  116. }
  117. }
  118. n = n->next;
  119. }
  120. return res;
  121. }
  122. static int read_identity(xmlNode *idn, cp_identity_t **dst)
  123. {
  124. cp_id_t *id, *last_id = NULL;
  125. cp_domain_t *domain, *last_domain = NULL;
  126. cp_except_t *except, *last_except = NULL;
  127. xmlNode *n;
  128. int res = RES_OK;
  129. *dst = (cp_identity_t*)cds_malloc(sizeof(cp_identity_t));
  130. if (!*dst) return RES_MEMORY_ERR;
  131. memset(*dst, 0, sizeof(**dst));
  132. n = idn->children;
  133. while (n) {
  134. if (n->type == XML_ELEMENT_NODE) {
  135. if (cmp_node(n, "id", common_policy_ns) >= 0) {
  136. res = read_id(n, &id);
  137. if (res != 0) break;
  138. LINKED_LIST_ADD((*dst)->ids, last_id, id);
  139. }
  140. else if (cmp_node(n, "domain", common_policy_ns) >= 0) {
  141. res = read_domain(n, &domain);
  142. if (res != 0) break;
  143. LINKED_LIST_ADD((*dst)->domains, last_domain, domain);
  144. }
  145. else if (cmp_node(n, "except", common_policy_ns) >= 0) {
  146. res = read_except(n, &except);
  147. if (res != 0) break;
  148. LINKED_LIST_ADD((*dst)->excepts, last_except, except);
  149. }
  150. else if (cmp_node(n, "any-identity", common_policy_ns) >= 0) {
  151. res = read_any_identity(n, &(*dst)->any_identity);
  152. if (res != 0) break;
  153. }
  154. }
  155. n = n->next;
  156. }
  157. return res;
  158. }
  159. static int read_conditions(xmlNode *cn, cp_conditions_t **dst)
  160. {
  161. xmlNode *n;
  162. int res = RES_OK;
  163. cp_sphere_t *sphere, * last_sphere = NULL;
  164. if ((!cn) || (!dst)) return RES_INTERNAL_ERR;
  165. *dst = (cp_conditions_t*)cds_malloc(sizeof(cp_conditions_t));
  166. if (!(*dst)) return RES_MEMORY_ERR;
  167. memset(*dst, 0, sizeof(cp_conditions_t));
  168. n = cn->children;
  169. while (n) {
  170. if (n->type == XML_ELEMENT_NODE) {
  171. if (cmp_node(n, "validity", common_policy_ns) >= 0) {
  172. /* FIXME: free existing validity */
  173. res = read_validity(n, &(*dst)->validity);
  174. if (res != 0) break;
  175. }
  176. else {
  177. if (cmp_node(n, "identity", common_policy_ns) >= 0) {
  178. /* FIXME: free existing identity */
  179. res = read_identity(n, &(*dst)->identity);
  180. if (res != 0) break;
  181. }
  182. else {
  183. if (cmp_node(n, "sphere", common_policy_ns) >= 0) {
  184. res = read_sphere(n, &sphere);
  185. if (res != 0) break;
  186. LINKED_LIST_ADD((*dst)->spheres, last_sphere, sphere);
  187. }
  188. /* else process other elements ? */
  189. }
  190. }
  191. }
  192. n = n->next;
  193. }
  194. return res;
  195. }
  196. static int read_transformations(xmlNode *tn, cp_transformations_t **dst)
  197. {
  198. int res = RES_OK;
  199. if ((!tn) || (!dst)) return RES_INTERNAL_ERR;
  200. *dst = (cp_transformations_t*)cds_malloc(sizeof(cp_transformations_t));
  201. if (!*dst) return RES_MEMORY_ERR;
  202. memset(*dst, 0, sizeof(cp_transformations_t));
  203. DEBUG_LOG("transformations for pres_rules not used\n");
  204. return res;
  205. }
  206. static int read_rule(xmlNode *rn, cp_rule_t **dst,
  207. cp_read_actions_func read_actions,
  208. cp_free_actions_func free_actions)
  209. {
  210. xmlNode *n;
  211. int res = RES_OK;
  212. if ((!rn) || (!dst)) return RES_INTERNAL_ERR;
  213. *dst = (cp_rule_t*)cds_malloc(sizeof(cp_rule_t));
  214. if (!*dst) return RES_MEMORY_ERR;
  215. memset(*dst, 0, sizeof(cp_rule_t));
  216. get_str_attr(rn, "id", &(*dst)->id);
  217. n = find_node(rn, "actions", common_policy_ns);
  218. if (n && (res == 0) && read_actions) res = read_actions(n, &(*dst)->actions);
  219. n = find_node(rn, "conditions", common_policy_ns);
  220. if (n && (res == 0)) res = read_conditions(n, &(*dst)->conditions);
  221. n = find_node(rn, "transformations", common_policy_ns);
  222. if (n && (res == 0)) res = read_transformations(n, &(*dst)->transformations);
  223. if (res != 0) {
  224. free_cp_rule(*dst, free_actions);
  225. *dst = NULL;
  226. return res;
  227. }
  228. return 0;
  229. }
  230. static int read_common_rules(xmlNode *root, cp_ruleset_t **dst,
  231. cp_read_actions_func read_actions, cp_free_actions_func free_actions)
  232. {
  233. cp_ruleset_t *rs = NULL;
  234. cp_rule_t *r, *last = NULL;
  235. xmlNode *n;
  236. int res = RES_OK;
  237. if (!dst) return RES_INTERNAL_ERR;
  238. else *dst = NULL;
  239. if (!root) return RES_INTERNAL_ERR;
  240. if (cmp_node(root, "ruleset", common_policy_ns) < 0) {
  241. ERROR_LOG("document is not a ruleset \n");
  242. return RES_INTERNAL_ERR;
  243. }
  244. rs = (cp_ruleset_t*)cds_malloc(sizeof(cp_ruleset_t));
  245. if (!rs) return RES_MEMORY_ERR;
  246. *dst = rs;
  247. memset(rs, 0, sizeof(*rs));
  248. /* read rules in ruleset */
  249. n = root->children;
  250. while (n) {
  251. if (n->type == XML_ELEMENT_NODE) {
  252. if (cmp_node(n, "rule", common_policy_ns) >= 0) {
  253. res = read_rule(n, &r, read_actions, free_actions);
  254. if (res == 0) {
  255. if (r) LINKED_LIST_ADD(rs->rules, last, r);
  256. }
  257. else break;
  258. }
  259. }
  260. n = n->next;
  261. }
  262. return res;
  263. }
  264. int parse_common_rules(const char *data, int dsize, cp_ruleset_t **dst,
  265. cp_read_actions_func read_actions, cp_free_actions_func free_actions)
  266. {
  267. int res = 0;
  268. xmlDocPtr doc; /* the resulting document tree */
  269. if (dst) *dst = NULL;
  270. doc = xmlReadMemory(data, dsize, NULL, NULL, xml_parser_flags);
  271. if (doc == NULL) {
  272. ERROR_LOG("can't parse document\n");
  273. return RES_INTERNAL_ERR;
  274. }
  275. res = read_common_rules(xmlDocGetRootElement(doc), dst,
  276. read_actions, free_actions);
  277. if ((res != RES_OK) && (dst)) {
  278. /* may be set => must be freed */
  279. free_common_rules(*dst, free_actions);
  280. *dst = NULL;
  281. }
  282. xmlFreeDoc(doc);
  283. return res;
  284. }