123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- /*
- * Copyright (C) 2005 iptelorg GmbH
- *
- * This file is part of ser, a free SIP server.
- *
- * ser is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- * [email protected]
- *
- * ser is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <xcap/xcap_client.h>
- #include <cds/dstring.h>
- #include <cds/memory.h>
- #include <cds/logger.h>
- #include <cds/serialize.h>
- static const str_t *get_xcap_doc_dir(xcap_document_type_t doc_type)
- {
- static str_t pres_rules = STR_STATIC_INIT("pres-rules");
- static str_t im_rules = STR_STATIC_INIT("im-rules");
- static str_t rls_services = STR_STATIC_INIT("rls-services");
- static str_t resource_lists = STR_STATIC_INIT("resource-lists");
- switch (doc_type) {
- case xcap_doc_pres_rules: return &pres_rules;
- case xcap_doc_im_rules: return &im_rules;
- case xcap_doc_rls_services: return &rls_services;
- case xcap_doc_resource_lists: return &resource_lists;
- /* when new doc_type added, there will be a warning -> add it there */
- }
- WARN_LOG("unknow XCAP document type\n");
- return NULL;
- }
- static const str_t *get_default_user_doc(xcap_document_type_t doc_type)
- {
- static str_t pres_rules = STR_STATIC_INIT("presence-rules.xml");
- static str_t im_rules = STR_STATIC_INIT("im-rules.xml");
- static str_t rls_services = STR_STATIC_INIT("rls-services.xml");
- static str_t resource_lists = STR_STATIC_INIT("resource-list.xml");
- switch (doc_type) {
- case xcap_doc_pres_rules: return &pres_rules;
- case xcap_doc_im_rules: return &im_rules;
- case xcap_doc_rls_services: return &rls_services;
- case xcap_doc_resource_lists: return &resource_lists;
- /* when new doc_type added, there will be a warning -> add it there */
- }
- WARN_LOG("unknow XCAP document type\n");
- return NULL;
- }
- static int ends_with_separator(str_t *s)
- {
- if (!is_str_empty(s))
- if (s->s[s->len - 1] == '/') return 1;
- return 0;
- }
- char *xcap_uri_for_users_document(xcap_document_type_t doc_type,
- const str_t *username,
- const str_t*filename,
- xcap_query_params_t *params)
- {
- dstring_t s;
- /* int res = RES_OK; */
- int l = 0;
- char *dst = NULL;
- dstr_init(&s, 128);
- if (params) {
- dstr_append_str(&s, ¶ms->xcap_root);
- if (!ends_with_separator(¶ms->xcap_root))
- dstr_append(&s, "/", 1);
- }
- else dstr_append(&s, "/", 1);
- dstr_append_str(&s, get_xcap_doc_dir(doc_type));
- dstr_append_zt(&s, "/users/");
- dstr_append_str(&s, username);
- dstr_append(&s, "/", 1);
- if (filename) dstr_append_str(&s, filename);
- else {
- /* default filename if NULL */
- dstr_append_str(&s, get_default_user_doc(doc_type));
- }
- /* res = dstr_get_str(&s, dst); */
-
- l = dstr_get_data_length(&s);
- if (l > 0) {
- dst = (char *)cds_malloc(l + 1);
- if (dst) {
- dstr_get_data(&s, dst);
- dst[l] = 0;
- }
- else ERROR_LOG("can't allocate memory (%d bytes)\n", l);
- }
-
- dstr_destroy(&s);
- return dst;
- }
- char *xcap_uri_for_global_document(xcap_document_type_t doc_type,
- const str_t *filename,
- xcap_query_params_t *params)
- {
- dstring_t s;
- /* int res = RES_OK; */
- char *dst = NULL;
- int l = 0;
- dstr_init(&s, 128);
- if (params) {
- dstr_append_str(&s, ¶ms->xcap_root);
- if (!ends_with_separator(¶ms->xcap_root))
- dstr_append(&s, "/", 1);
- }
- else dstr_append(&s, "/", 1);
- dstr_append_str(&s, get_xcap_doc_dir(doc_type));
- if (filename) {
- dstr_append_zt(&s, "/global/");
- dstr_append_str(&s, filename);
- }
- else {
- /* default filename if NULL */
- dstr_append_zt(&s, "/global/index");
- }
- /* res = dstr_get_str(&s, dst); */
-
- l = dstr_get_data_length(&s);
- if (l > 0) {
- dst = (char *)cds_malloc(l + 1);
- if (dst) {
- dstr_get_data(&s, dst);
- dst[l] = 0;
- }
- }
-
- dstr_destroy(&s);
- return dst;
- }
- #ifdef SER
- #include "sr_module.h"
- int xcap_query(const char *uri,
- xcap_query_params_t *params, char **buf, int *bsize)
- {
- static xcap_query_func query = NULL;
- static int initialized = 0;
- if (!initialized) {
- query = (xcap_query_func)find_export("xcap_query", 0, -1);
- initialized = 1;
- if (!query) WARN_LOG("No XCAP query support! (Missing module?)\n");
- }
- if (!query) {
- /* no function for doing XCAP queries */
- return -1;
- }
-
- /* all XCAP queries are done through XCAP module */
- return query(uri, params, buf, bsize);
- }
- #else /* compiled WITHOUT SER */
- #include <curl/curl.h>
- static size_t write_data_func(void *ptr, size_t size, size_t nmemb, void *stream)
- {
- int s = size * nmemb;
- /* TRACE_LOG("%d bytes writen\n", s);*/
- if (s != 0) {
- if (dstr_append((dstring_t*)stream, ptr, s) != 0) {
- ERROR_LOG("can't append %d bytes into data buffer\n", s);
- return 0;
- }
- }
- return s;
- }
- int xcap_query(const char *uri, xcap_query_params_t *params, char **buf, int *bsize)
- {
- CURLcode res = -1;
- static CURL *handle = NULL;
- dstring_t data;
- char *auth = NULL;
- int i;
- long auth_methods;
-
- if (!uri) {
- ERROR_LOG("BUG: no uri given\n");
- return -1;
- }
- if (!buf) {
- ERROR_LOG("BUG: no buf given\n");
- return -1;
- }
- i = 0;
- if (params) {
- if (params->auth_user.s) i += params->auth_user.len;
- if (params->auth_pass.s) i += params->auth_pass.len;
- }
- if (i > 0) {
- /* do authentication */
- auth = (char *)cds_malloc(i + 2);
- if (!auth) return -1;
- sprintf(auth, "%s:%s", params->auth_user.s ? params->auth_user.s: "",
- params->auth_pass.s ? params->auth_pass.s: "");
- }
- auth_methods = CURLAUTH_BASIC | CURLAUTH_DIGEST;
-
- dstr_init(&data, 512);
-
- if (!handle) handle = curl_easy_init();
- if (handle) {
- curl_easy_setopt(handle, CURLOPT_URL, uri);
- /* TRACE_LOG("uri: %s\n", uri ? uri : "<null>"); */
-
- /* do not store data into a file - store them in memory */
- curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data_func);
- curl_easy_setopt(handle, CURLOPT_WRITEDATA, &data);
- #ifdef CURLOPT_MUTE
- /* be quiet */
- curl_easy_setopt(handle, CURLOPT_MUTE, 1);
- #endif /* CURLOPT_MUTE */
-
- /* non-2xx => error */
- curl_easy_setopt(handle, CURLOPT_FAILONERROR, 1);
- /* auth */
- curl_easy_setopt(handle, CURLOPT_HTTPAUTH, auth_methods); /* TODO possibility of selection */
- curl_easy_setopt(handle, CURLOPT_NETRC, CURL_NETRC_IGNORED);
- curl_easy_setopt(handle, CURLOPT_USERPWD, auth);
- /* SSL */
- if (params) {
- if (params->enable_unverified_ssl_peer) {
- curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0);
- curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0);
- }
- }
-
- /* follow redirects (needed for apache mod_speling - case insesitive names) */
- curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1);
-
- /* curl_easy_setopt(handle, CURLOPT_TCP_NODELAY, 1);
- curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, 10);*/
-
- /* Accept headers */
-
- res = curl_easy_perform(handle);
- /* curl_easy_cleanup(handle); */ /* FIXME: experimental */
- }
- else ERROR_LOG("can't initialize curl handle\n");
- if (res == 0) {
- *bsize = dstr_get_data_length(&data);
- if (*bsize) {
- *buf = (char*)cds_malloc(*bsize);
- if (!*buf) {
- ERROR_LOG("can't allocate %d bytes\n", *bsize);
- res = -1;
- *bsize = 0;
- }
- else dstr_get_data(&data, *buf);
- }
- }
- else DEBUG_LOG("curl error: %d\n", res);
- dstr_destroy(&data);
- if (auth) cds_free(auth);
- return res;
- }
- #endif
- void free_xcap_params_content(xcap_query_params_t *params)
- {
- if (params) {
- str_free_content(¶ms->xcap_root);
- str_free_content(¶ms->auth_user);
- str_free_content(¶ms->auth_pass);
- memset(params, 0, sizeof(*params));
- }
- }
- int dup_xcap_params(xcap_query_params_t *dst, xcap_query_params_t *src)
- {
- int res = -10;
-
- if (dst) memset(dst, 0, sizeof(*dst));
-
- if (src && dst) {
- res = 0;
-
- res = str_dup(&dst->xcap_root, &src->xcap_root);
- if (res == 0) res = str_dup(&dst->auth_user, &src->auth_user);
- if (res == 0) res = str_dup(&dst->auth_pass, &src->auth_pass);
-
- if (res != 0) free_xcap_params_content(dst);
- }
-
- return res;
- }
- int get_inline_xcap_buf_len(xcap_query_params_t *params)
- {
- int len;
-
- /* counts the length for data buffer storing values of
- * xcap parameter members */
- if (!params) {
- ERROR_LOG("BUG: empty params given\n");
- return 0;
- }
- len = params->xcap_root.len;
- len += params->auth_user.len;
- len += params->auth_pass.len;
- return len;
- }
- int dup_xcap_params_inline(xcap_query_params_t *dst, xcap_query_params_t *src, char *data_buffer)
- {
- int res = -10;
-
- /* copies structure into existing buffer */
- if (dst) {
- memset(dst, 0, sizeof(*dst));
- res = 0;
- }
-
- if (src && dst) {
- dst->xcap_root.s = data_buffer;
- str_cpy(&dst->xcap_root, &src->xcap_root);
- dst->auth_user.s = after_str_ptr(&dst->xcap_root);
- str_cpy(&dst->auth_user, &src->auth_user);
- dst->auth_pass.s = after_str_ptr(&dst->auth_user);
- str_cpy(&dst->auth_pass, &src->auth_pass);
- }
- return res;
- }
- int serialize_xcap_params(sstream_t *ss, xcap_query_params_t *xp)
- {
- int res = 0;
-
- if (is_input_sstream(ss)) {
- memset(xp, 0, sizeof(*xp));
- }
- res = serialize_str(ss, &xp->xcap_root) | res;
- res = serialize_str(ss, &xp->auth_user) | res;
- res = serialize_str(ss, &xp->auth_pass) | res;
- return res;
- }
- int str2xcap_params(xcap_query_params_t *dst, const str_t *src)
- {
- int res = 0;
- sstream_t store;
- if (!src) return -1;
-
- init_input_sstream(&store, src->s, src->len);
- if (serialize_xcap_params(&store, dst) != 0) {
- ERROR_LOG("can't de-serialize xcap_params\n");
- res = -1;
- }
- destroy_sstream(&store);
-
- return res;
- }
- int xcap_params2str(str_t *dst, xcap_query_params_t *src)
- {
- int res = 0;
- sstream_t store;
-
- init_output_sstream(&store, 256);
-
- if (serialize_xcap_params(&store, src) != 0) {
- ERROR_LOG("can't serialize dialog\n");
- res = -1;
- }
- else {
- if (get_serialized_sstream(&store, dst) != 0) {
- ERROR_LOG("can't get serialized data\n");
- res = -1;
- }
- }
- destroy_sstream(&store);
- return res;
- }
|