diff --git a/xml.c b/xml.c index bb2632f..c4f0493 100644 --- a/xml.c +++ b/xml.c @@ -36,6 +36,26 @@ #include "openconnect.h" +static int grab_string(xmlNode *xml_node, const char *varname, char **target, const char *hostname) +{ + char *content; + + if (strcmp((char *)xml_node->name, varname)) + return 0; + + content = (char *)xmlNodeGetContent(xml_node); + if (!content) + return 0; + + free(*target); + *target = content; + + if (hostname != NULL) + printf("Host \"%s\" has %s \"%s\"\n", hostname, varname, content); + + return 1; +} + int config_lookup_host(struct openconnect_info *vpninfo, const char *host) { int fd, i; @@ -88,53 +108,41 @@ int config_lookup_host(struct openconnect_info *vpninfo, const char *host) xml_node = xmlDocGetRootElement(xml_doc); for (xml_node = xml_node->children; xml_node; xml_node = xml_node->next) { - if (xml_node->type == XML_ELEMENT_NODE && - !strcmp((char *)xml_node->name, "ServerList")) { - - for (xml_node = xml_node->children; xml_node && !vpninfo->hostname; - xml_node = xml_node->next) { - - if (xml_node->type == XML_ELEMENT_NODE && - !strcmp((char *)xml_node->name, "HostEntry")) { - int match = 0; - - for (xml_node2 = xml_node->children; - match >= 0 && xml_node2; xml_node2 = xml_node2->next) { - - if (xml_node2->type != XML_ELEMENT_NODE) - continue; - - if (!match && !strcmp((char *)xml_node2->name, "HostName")) { - char *content = (char *)xmlNodeGetContent(xml_node2); - if (content && !strcmp(content, host)) - match = 1; - else - match = -1; - free(content); - } else if (match && - !strcmp((char *)xml_node2->name, "HostAddress")) { - char *content = (char *)xmlNodeGetContent(xml_node2); - if (content) { - vpninfo->hostname = content; - printf("Host \"%s\" has address \"%s\"\n", - host, content); - } - } else if (match && - !strcmp((char *)xml_node2->name, "UserGroup")) { - char *content = (char *)xmlNodeGetContent(xml_node2); - if (content) { - free(vpninfo->urlpath); - vpninfo->urlpath = content; - printf("Host \"%s\" has UserGroup \"%s\"\n", - host, content); - } - } + if (!(xml_node->type == XML_ELEMENT_NODE && + !strcmp((char *)xml_node->name, "ServerList"))) continue; + + for (xml_node = xml_node->children; xml_node && !vpninfo->hostname; + xml_node = xml_node->next) { + int match = 0; + + if (!(xml_node->type == XML_ELEMENT_NODE && + !strcmp((char *)xml_node->name, "HostEntry"))) continue; + + for (xml_node2 = xml_node->children; + match >= 0 && xml_node2; xml_node2 = xml_node2->next) { + + if (xml_node2->type != XML_ELEMENT_NODE) + continue; + + if (!match && !strcmp((char *)xml_node2->name, "HostName")) { + char *content = (char *)xmlNodeGetContent(xml_node2); + if (content && !strcmp(content, host)) { + match = 1; + vpninfo->hostname = content; + } + else { + match = -1; + free(content); } } - + else if (match && grab_string(xml_node2, "HostAddress", &vpninfo->hostname, host)); + else if (match && grab_string(xml_node2, "UserGroup", &vpninfo->urlpath, host)); + else if (match && grab_string(xml_node2, "AuthGroup", &vpninfo->authgroup, host)); + else if (match && grab_string(xml_node2, "UserName", &vpninfo->username, host)); + else if (match && grab_string(xml_node2, "PassWord", &vpninfo->password, NULL)); /* don't print */ } - break; } + break; } xmlFreeDoc(xml_doc);