2 * OpenConnect (SSL + DTLS) VPN client
4 * Copyright © 2008-2011 Intel Corporation.
5 * Copyright © 2008 Nick Andrew <nick@nick-andrew.net>
7 * Author: David Woodhouse <dwmw2@infradead.org>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * version 2.1, as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to:
21 * Free Software Foundation, Inc.
22 * 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301 USA
30 #include <sys/types.h>
33 #include <libxml/parser.h>
34 #include <libxml/tree.h>
37 #include "openconnect-internal.h"
39 int config_lookup_host(struct openconnect_info *vpninfo, const char *host)
44 unsigned char sha1[SHA1_SIZE];
46 xmlNode *xml_node, *xml_node2;
48 if (!vpninfo->xmlconfig)
51 fd = open(vpninfo->xmlconfig, O_RDONLY);
53 perror(_("Open XML config file"));
54 fprintf(stderr, _("Treating host \"%s\" as a raw hostname\n"),
60 perror(_("fstat XML config file"));
65 xmlfile = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
66 if (xmlfile == MAP_FAILED) {
67 perror(_("mmap XML config file"));
72 if (openconnect_sha1(sha1, xmlfile, st.st_size)) {
73 fprintf(stderr, _("Failed to SHA1 existing file\n"));
78 for (i = 0; i < SHA1_SIZE; i++)
79 sprintf(&vpninfo->xmlsha1[i*2], "%02x", sha1[i]);
81 vpn_progress(vpninfo, PRG_TRACE, _("XML config file SHA1: %s\n"),
84 xml_doc = xmlReadMemory(xmlfile, st.st_size, "noname.xml", NULL, 0);
85 munmap(xmlfile, st.st_size);
88 fprintf(stderr, _("Failed to parse XML config file %s\n"),
90 fprintf(stderr, _("Treating host \"%s\" as a raw hostname\n"),
94 xml_node = xmlDocGetRootElement(xml_doc);
96 for (xml_node = xml_node->children; xml_node; xml_node = xml_node->next) {
97 if (xml_node->type == XML_ELEMENT_NODE &&
98 !strcmp((char *)xml_node->name, "ServerList")) {
100 for (xml_node = xml_node->children; xml_node && !vpninfo->hostname;
101 xml_node = xml_node->next) {
103 if (xml_node->type == XML_ELEMENT_NODE &&
104 !strcmp((char *)xml_node->name, "HostEntry")) {
107 for (xml_node2 = xml_node->children;
108 match >= 0 && xml_node2; xml_node2 = xml_node2->next) {
110 if (xml_node2->type != XML_ELEMENT_NODE)
113 if (!match && !strcmp((char *)xml_node2->name, "HostName")) {
114 char *content = (char *)xmlNodeGetContent(xml_node2);
115 if (content && !strcmp(content, host))
121 !strcmp((char *)xml_node2->name, "HostAddress")) {
122 char *content = (char *)xmlNodeGetContent(xml_node2);
124 vpninfo->hostname = content;
125 printf(_("Host \"%s\" has address \"%s\"\n"),
129 !strcmp((char *)xml_node2->name, "UserGroup")) {
130 char *content = (char *)xmlNodeGetContent(xml_node2);
132 free(vpninfo->urlpath);
133 vpninfo->urlpath = content;
134 printf(_("Host \"%s\" has UserGroup \"%s\"\n"),
147 if (!vpninfo->hostname) {
148 fprintf(stderr, _("Host \"%s\" not listed in config; treating as raw hostname\n"),