xcap_client.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <xcap/xcap_client.h>
  29. #include <cds/dstring.h>
  30. #include <cds/memory.h>
  31. #include <cds/logger.h>
  32. #include <cds/serialize.h>
  33. static const str_t *get_xcap_doc_dir(xcap_document_type_t doc_type)
  34. {
  35. static str_t pres_rules = STR_STATIC_INIT("pres-rules");
  36. static str_t im_rules = STR_STATIC_INIT("im-rules");
  37. static str_t rls_services = STR_STATIC_INIT("rls-services");
  38. static str_t resource_lists = STR_STATIC_INIT("resource-lists");
  39. switch (doc_type) {
  40. case xcap_doc_pres_rules: return &pres_rules;
  41. case xcap_doc_im_rules: return &im_rules;
  42. case xcap_doc_rls_services: return &rls_services;
  43. case xcap_doc_resource_lists: return &resource_lists;
  44. /* when new doc_type added, there will be a warning -> add it there */
  45. }
  46. WARN_LOG("unknow XCAP document type\n");
  47. return NULL;
  48. }
  49. static const str_t *get_default_user_doc(xcap_document_type_t doc_type)
  50. {
  51. static str_t pres_rules = STR_STATIC_INIT("presence-rules.xml");
  52. static str_t im_rules = STR_STATIC_INIT("im-rules.xml");
  53. static str_t rls_services = STR_STATIC_INIT("rls-services.xml");
  54. static str_t resource_lists = STR_STATIC_INIT("resource-list.xml");
  55. switch (doc_type) {
  56. case xcap_doc_pres_rules: return &pres_rules;
  57. case xcap_doc_im_rules: return &im_rules;
  58. case xcap_doc_rls_services: return &rls_services;
  59. case xcap_doc_resource_lists: return &resource_lists;
  60. /* when new doc_type added, there will be a warning -> add it there */
  61. }
  62. WARN_LOG("unknow XCAP document type\n");
  63. return NULL;
  64. }
  65. static int ends_with_separator(str_t *s)
  66. {
  67. if (!is_str_empty(s))
  68. if (s->s[s->len - 1] == '/') return 1;
  69. return 0;
  70. }
  71. char *xcap_uri_for_users_document(xcap_document_type_t doc_type,
  72. const str_t *username,
  73. const str_t*filename,
  74. xcap_query_params_t *params)
  75. {
  76. dstring_t s;
  77. /* int res = RES_OK; */
  78. int l = 0;
  79. char *dst = NULL;
  80. dstr_init(&s, 128);
  81. if (params) {
  82. dstr_append_str(&s, &params->xcap_root);
  83. if (!ends_with_separator(&params->xcap_root))
  84. dstr_append(&s, "/", 1);
  85. }
  86. else dstr_append(&s, "/", 1);
  87. dstr_append_str(&s, get_xcap_doc_dir(doc_type));
  88. dstr_append_zt(&s, "/users/");
  89. dstr_append_str(&s, username);
  90. dstr_append(&s, "/", 1);
  91. if (filename) dstr_append_str(&s, filename);
  92. else {
  93. /* default filename if NULL */
  94. dstr_append_str(&s, get_default_user_doc(doc_type));
  95. }
  96. /* res = dstr_get_str(&s, dst); */
  97. l = dstr_get_data_length(&s);
  98. if (l > 0) {
  99. dst = (char *)cds_malloc(l + 1);
  100. if (dst) {
  101. dstr_get_data(&s, dst);
  102. dst[l] = 0;
  103. }
  104. else ERROR_LOG("can't allocate memory (%d bytes)\n", l);
  105. }
  106. dstr_destroy(&s);
  107. return dst;
  108. }
  109. char *xcap_uri_for_global_document(xcap_document_type_t doc_type,
  110. const str_t *filename,
  111. xcap_query_params_t *params)
  112. {
  113. dstring_t s;
  114. /* int res = RES_OK; */
  115. char *dst = NULL;
  116. int l = 0;
  117. dstr_init(&s, 128);
  118. if (params) {
  119. dstr_append_str(&s, &params->xcap_root);
  120. if (!ends_with_separator(&params->xcap_root))
  121. dstr_append(&s, "/", 1);
  122. }
  123. else dstr_append(&s, "/", 1);
  124. dstr_append_str(&s, get_xcap_doc_dir(doc_type));
  125. if (filename) {
  126. dstr_append_zt(&s, "/global/");
  127. dstr_append_str(&s, filename);
  128. }
  129. else {
  130. /* default filename if NULL */
  131. dstr_append_zt(&s, "/global/index");
  132. }
  133. /* res = dstr_get_str(&s, dst); */
  134. l = dstr_get_data_length(&s);
  135. if (l > 0) {
  136. dst = (char *)cds_malloc(l + 1);
  137. if (dst) {
  138. dstr_get_data(&s, dst);
  139. dst[l] = 0;
  140. }
  141. }
  142. dstr_destroy(&s);
  143. return dst;
  144. }
  145. #ifdef SER
  146. #include "sr_module.h"
  147. int xcap_query(const char *uri,
  148. xcap_query_params_t *params, char **buf, int *bsize)
  149. {
  150. static xcap_query_func query = NULL;
  151. static int initialized = 0;
  152. if (!initialized) {
  153. query = (xcap_query_func)find_export("xcap_query", 0, -1);
  154. initialized = 1;
  155. if (!query) WARN_LOG("No XCAP query support! (Missing module?)\n");
  156. }
  157. if (!query) {
  158. /* no function for doing XCAP queries */
  159. return -1;
  160. }
  161. /* all XCAP queries are done through XCAP module */
  162. return query(uri, params, buf, bsize);
  163. }
  164. #else /* compiled WITHOUT SER */
  165. #include <curl/curl.h>
  166. static size_t write_data_func(void *ptr, size_t size, size_t nmemb, void *stream)
  167. {
  168. int s = size * nmemb;
  169. /* TRACE_LOG("%d bytes writen\n", s);*/
  170. if (s != 0) {
  171. if (dstr_append((dstring_t*)stream, ptr, s) != 0) {
  172. ERROR_LOG("can't append %d bytes into data buffer\n", s);
  173. return 0;
  174. }
  175. }
  176. return s;
  177. }
  178. int xcap_query(const char *uri, xcap_query_params_t *params, char **buf, int *bsize)
  179. {
  180. CURLcode res = -1;
  181. static CURL *handle = NULL;
  182. dstring_t data;
  183. char *auth = NULL;
  184. int i;
  185. long auth_methods;
  186. if (!uri) {
  187. ERROR_LOG("BUG: no uri given\n");
  188. return -1;
  189. }
  190. if (!buf) {
  191. ERROR_LOG("BUG: no buf given\n");
  192. return -1;
  193. }
  194. i = 0;
  195. if (params) {
  196. if (params->auth_user.s) i += params->auth_user.len;
  197. if (params->auth_pass.s) i += params->auth_pass.len;
  198. }
  199. if (i > 0) {
  200. /* do authentication */
  201. auth = (char *)cds_malloc(i + 2);
  202. if (!auth) return -1;
  203. sprintf(auth, "%s:%s", params->auth_user.s ? params->auth_user.s: "",
  204. params->auth_pass.s ? params->auth_pass.s: "");
  205. }
  206. auth_methods = CURLAUTH_BASIC | CURLAUTH_DIGEST;
  207. dstr_init(&data, 512);
  208. if (!handle) handle = curl_easy_init();
  209. if (handle) {
  210. curl_easy_setopt(handle, CURLOPT_URL, uri);
  211. /* TRACE_LOG("uri: %s\n", uri ? uri : "<null>"); */
  212. /* do not store data into a file - store them in memory */
  213. curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data_func);
  214. curl_easy_setopt(handle, CURLOPT_WRITEDATA, &data);
  215. #ifdef CURLOPT_MUTE
  216. /* be quiet */
  217. curl_easy_setopt(handle, CURLOPT_MUTE, 1);
  218. #endif /* CURLOPT_MUTE */
  219. /* non-2xx => error */
  220. curl_easy_setopt(handle, CURLOPT_FAILONERROR, 1);
  221. /* auth */
  222. curl_easy_setopt(handle, CURLOPT_HTTPAUTH, auth_methods); /* TODO possibility of selection */
  223. curl_easy_setopt(handle, CURLOPT_NETRC, CURL_NETRC_IGNORED);
  224. curl_easy_setopt(handle, CURLOPT_USERPWD, auth);
  225. /* SSL */
  226. if (params) {
  227. if (params->enable_unverified_ssl_peer) {
  228. curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0);
  229. curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0);
  230. }
  231. }
  232. /* follow redirects (needed for apache mod_speling - case insesitive names) */
  233. curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1);
  234. /* curl_easy_setopt(handle, CURLOPT_TCP_NODELAY, 1);
  235. curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, 10);*/
  236. /* Accept headers */
  237. res = curl_easy_perform(handle);
  238. /* curl_easy_cleanup(handle); */ /* FIXME: experimental */
  239. }
  240. else ERROR_LOG("can't initialize curl handle\n");
  241. if (res == 0) {
  242. *bsize = dstr_get_data_length(&data);
  243. if (*bsize) {
  244. *buf = (char*)cds_malloc(*bsize);
  245. if (!*buf) {
  246. ERROR_LOG("can't allocate %d bytes\n", *bsize);
  247. res = -1;
  248. *bsize = 0;
  249. }
  250. else dstr_get_data(&data, *buf);
  251. }
  252. }
  253. else DEBUG_LOG("curl error: %d\n", res);
  254. dstr_destroy(&data);
  255. if (auth) cds_free(auth);
  256. return res;
  257. }
  258. #endif
  259. void free_xcap_params_content(xcap_query_params_t *params)
  260. {
  261. if (params) {
  262. str_free_content(&params->xcap_root);
  263. str_free_content(&params->auth_user);
  264. str_free_content(&params->auth_pass);
  265. memset(params, 0, sizeof(*params));
  266. }
  267. }
  268. int dup_xcap_params(xcap_query_params_t *dst, xcap_query_params_t *src)
  269. {
  270. int res = -10;
  271. if (dst) memset(dst, 0, sizeof(*dst));
  272. if (src && dst) {
  273. res = 0;
  274. res = str_dup(&dst->xcap_root, &src->xcap_root);
  275. if (res == 0) res = str_dup(&dst->auth_user, &src->auth_user);
  276. if (res == 0) res = str_dup(&dst->auth_pass, &src->auth_pass);
  277. if (res != 0) free_xcap_params_content(dst);
  278. }
  279. return res;
  280. }
  281. int get_inline_xcap_buf_len(xcap_query_params_t *params)
  282. {
  283. int len;
  284. /* counts the length for data buffer storing values of
  285. * xcap parameter members */
  286. if (!params) {
  287. ERROR_LOG("BUG: empty params given\n");
  288. return 0;
  289. }
  290. len = params->xcap_root.len;
  291. len += params->auth_user.len;
  292. len += params->auth_pass.len;
  293. return len;
  294. }
  295. int dup_xcap_params_inline(xcap_query_params_t *dst, xcap_query_params_t *src, char *data_buffer)
  296. {
  297. int res = -10;
  298. /* copies structure into existing buffer */
  299. if (dst) {
  300. memset(dst, 0, sizeof(*dst));
  301. res = 0;
  302. }
  303. if (src && dst) {
  304. dst->xcap_root.s = data_buffer;
  305. str_cpy(&dst->xcap_root, &src->xcap_root);
  306. dst->auth_user.s = after_str_ptr(&dst->xcap_root);
  307. str_cpy(&dst->auth_user, &src->auth_user);
  308. dst->auth_pass.s = after_str_ptr(&dst->auth_user);
  309. str_cpy(&dst->auth_pass, &src->auth_pass);
  310. }
  311. return res;
  312. }
  313. int serialize_xcap_params(sstream_t *ss, xcap_query_params_t *xp)
  314. {
  315. int res = 0;
  316. if (is_input_sstream(ss)) {
  317. memset(xp, 0, sizeof(*xp));
  318. }
  319. res = serialize_str(ss, &xp->xcap_root) | res;
  320. res = serialize_str(ss, &xp->auth_user) | res;
  321. res = serialize_str(ss, &xp->auth_pass) | res;
  322. return res;
  323. }
  324. int str2xcap_params(xcap_query_params_t *dst, const str_t *src)
  325. {
  326. int res = 0;
  327. sstream_t store;
  328. if (!src) return -1;
  329. init_input_sstream(&store, src->s, src->len);
  330. if (serialize_xcap_params(&store, dst) != 0) {
  331. ERROR_LOG("can't de-serialize xcap_params\n");
  332. res = -1;
  333. }
  334. destroy_sstream(&store);
  335. return res;
  336. }
  337. int xcap_params2str(str_t *dst, xcap_query_params_t *src)
  338. {
  339. int res = 0;
  340. sstream_t store;
  341. init_output_sstream(&store, 256);
  342. if (serialize_xcap_params(&store, src) != 0) {
  343. ERROR_LOG("can't serialize dialog\n");
  344. res = -1;
  345. }
  346. else {
  347. if (get_serialized_sstream(&store, dst) != 0) {
  348. ERROR_LOG("can't get serialized data\n");
  349. res = -1;
  350. }
  351. }
  352. destroy_sstream(&store);
  353. return res;
  354. }