int p_get_xcap_doc(str* user, str* domain, int type, str** doc) { str *etag = NULL; if (xcapDbGetDoc(user, domain, type, NULL, NULL, doc, &etag) < 0) { LM_ERR("whie fetching XCAP document from DB\n"); return -1; } if (*doc == NULL) { if(integrated_xcap_server) return 0; if (http_get_xcap_doc(user, domain, type, doc) < 0) return 0; } pkg_free(etag->s); pkg_free(etag); return 0; }
/* * Function that searches a resource list document for the user and then * looks in the document for the service uri * returns: * 0: success ( service uri found) * -1: server error * doc : * NULL: document or service uri not found * pointer to xmlDocPtr structure if service uri found * */ int get_resource_list(str* service_uri, str owner_user, str owner_domain, xmlNodePtr* service_node, xmlDocPtr* rl_doc) { str *doc = NULL; str *etag = NULL; xmlDocPtr xml_doc = NULL; xmlNodePtr snode = NULL; *rl_doc = NULL; *service_node = NULL; if (xcapDbGetDoc(&owner_user, &owner_domain, RLS_SERVICES, NULL, NULL, &doc, &etag) < 0) { LM_ERR("while getting RLS document from DB\n"); goto error; } if (doc == NULL) { LM_DBG("No rl document found in database\n"); if (rls_integrated_xcap_server) goto done; /* Use xcap_client to try to fetch the document */ if (http_get_resource_list(&owner_user, &owner_domain, &doc) < 0) goto done; } /* Document is loaded in doc either via DB or HTTP */ LM_DBG("rls_services document:\n%.*s\n", doc->len, doc->s); xml_doc = xmlParseMemory(doc->s, doc->len); if(xml_doc == NULL) { LM_ERR("while parsing XML memory\n"); goto error; } snode = search_service_uri(xml_doc, service_uri); if (snode == NULL) { LM_DBG("service uri %.*s not found in rl document for user" " sip:%.*s@%.*s\n", service_uri->len, service_uri->s, owner_user.len, owner_user.s, owner_domain.len, owner_domain.s); xmlFreeDoc(xml_doc); goto done; } *rl_doc = xml_doc; *service_node = snode; done: if (doc != NULL) { if (doc->s != NULL) pkg_free(doc->s); pkg_free(doc); } if (etag != NULL) { if (etag->s != NULL) pkg_free(etag->s); pkg_free(etag); } return 0; error: if (doc != NULL) { if (doc->s != NULL) pkg_free(doc->s); pkg_free(doc); } if (etag != NULL) { if (etag->s != NULL) pkg_free(etag->s); pkg_free(etag); } if(xml_doc) xmlFreeDoc(xml_doc); return -1; }
static inline int get_resource_list(str *username, str *domain, str *filename, str *selector, xmlNodePtr *rl_node, xmlDocPtr *xmldoc) { static char path_buf[MAX_PATH_LEN+1]; int checked = 0; str path; str *doc = NULL; str *etag = NULL; xmlXPathContextPtr xpathCtx = NULL; xmlXPathObjectPtr xpathObj = NULL; if (filename==NULL || username==NULL || domain==NULL) { LM_ERR("invalid parameters\n"); return -1; } if (xcapDbGetDoc(username, domain, RESOURCE_LISTS, filename, NULL, &doc, &etag) < 0) { LM_DBG("No rl document found\n"); return -1; } LM_DBG("rl document:\n%.*s\n", doc->len, doc->s); path.s = path_buf; path.len = 0; if (selector->s) { while (checked < selector->len && path.len <= MAX_PATH_LEN) { if (selector->s[checked] == '/') { memcpy(path.s+path.len, "/xmlns:", 7); path.len += 7; } else { path.s[path.len++] = selector->s[checked]; } checked++; } path.s[path.len] = '\0'; LM_DBG("path: %.*s", path.len, path.s); } *xmldoc = xmlParseMemory(doc->s, doc->len); if (*xmldoc == NULL) { LM_ERR("while parsing XML memory\n"); goto error; } if(path.len == 0) { LM_ERR("no path specified\n"); goto error; } /* TODO: move this to xcap module? */ xpathCtx = xmlXPathNewContext(*xmldoc); if (xpathCtx == NULL) { LM_ERR("unable to create new XPath context"); goto error; } if (xmlXPathRegisterNs(xpathCtx, BAD_CAST "xmlns", BAD_CAST "urn:ietf:params:xml:ns:resource-lists") != 0) { LM_ERR("unable to register xmlns\n"); goto error; } xpathObj = xmlXPathEvalExpression(BAD_CAST path.s, xpathCtx); if (xpathObj == NULL) { LM_ERR("unable to evaluate path\n"); goto error; } if (xpathObj->nodesetval == NULL || xpathObj->nodesetval->nodeNr <= 0) { LM_ERR("no nodes found\n"); goto error; } if (xpathObj->nodesetval->nodeTab[0] != NULL && xpathObj->nodesetval->nodeTab[0]->type != XML_ELEMENT_NODE) { LM_ERR("no nodes of the correct type found\n"); goto error; } *rl_node = xpathObj->nodesetval->nodeTab[0]; xmlXPathFreeObject(xpathObj); xmlXPathFreeContext(xpathCtx); pkg_free(doc->s); pkg_free(doc); pkg_free(etag->s); pkg_free(etag); return 0; error: if (doc != NULL) { if (doc->s != NULL) pkg_free(doc->s); pkg_free(doc); } if (etag != NULL) { if (etag->s != NULL) pkg_free(etag->s); pkg_free(etag); } if (xpathObj) xmlXPathFreeObject(xpathObj); if (xpathCtx) xmlXPathFreeContext(xpathCtx); if (*xmldoc) xmlFreeDoc(*xmldoc); return -1; }