Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Do not use mmap() for reading XML config file
It doesn't work on Windows and it's too ugly and not worthwhile to make
it conditional. Based on a patch from Nikos Mavrogiannopoulos.

Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
  • Loading branch information
David Woodhouse authored and David Woodhouse committed Feb 5, 2014
1 parent 7d58c7f commit 113965a
Showing 1 changed file with 55 additions and 21 deletions.
76 changes: 55 additions & 21 deletions xml.c
Expand Up @@ -29,49 +29,82 @@
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <string.h>

#include "openconnect-internal.h"

int config_lookup_host(struct openconnect_info *vpninfo, const char *host)
static int load_xml_conf_file(struct openconnect_info *vpninfo, char **ptr,
size_t *size)
{
int fd, i;
struct stat st;
int fd;
char *xmlfile;
unsigned char sha1[SHA1_SIZE];
xmlDocPtr xml_doc;
xmlNode *xml_node, *xml_node2;

if (!vpninfo->xmlconfig)
return 0;

fd = open(vpninfo->xmlconfig, O_RDONLY);
if (fd < 0) {
perror(_("Open XML config file"));
fprintf(stderr, _("Treating host \"%s\" as a raw hostname\n"),
host);
vpn_progress(vpninfo, PRG_ERR,
_("Failed to open XML config file: %s\n"),
strerror(errno));
return 0;
}

if (fstat(fd, &st)) {
perror(_("fstat XML config file"));
vpn_progress(vpninfo, PRG_ERR,
_("Failed to fstat() XML config file: %s\n"),
strerror(errno));
close(fd);
return -1;
}

xmlfile = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (xmlfile == MAP_FAILED) {
perror(_("mmap XML config file"));
xmlfile = malloc(st.st_size);
if (!xmlfile) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to allocate %lu bytes for XML config file\n"),
(unsigned long)st.st_size);
return -1;
}

if (read(fd, xmlfile, st.st_size) != st.st_size) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to read XML config file: %s\n"),
strerror(errno));
close(fd);
free(xmlfile);
return -1;
}

if (openconnect_sha1(sha1, xmlfile, st.st_size)) {
*ptr = xmlfile;
*size = st.st_size;

close(fd);

return 1;
}

int config_lookup_host(struct openconnect_info *vpninfo, const char *host)
{
int i, ret;
size_t size;
char *xmlfile;
unsigned char sha1[SHA1_SIZE];
xmlDocPtr xml_doc;
xmlNode *xml_node, *xml_node2;

if (!vpninfo->xmlconfig)
return 0;

ret = load_xml_conf_file(vpninfo, &xmlfile, &size);
if (ret <= 0) {
if (ret == 0)
fprintf(stderr, _("Treating host \"%s\" as a raw hostname\n"),
host);
return ret;
}

if (openconnect_sha1(sha1, xmlfile, size)) {
fprintf(stderr, _("Failed to SHA1 existing file\n"));
close(fd);
return -1;
}

Expand All @@ -81,9 +114,10 @@ int config_lookup_host(struct openconnect_info *vpninfo, const char *host)
vpn_progress(vpninfo, PRG_TRACE, _("XML config file SHA1: %s\n"),
vpninfo->xmlsha1);

xml_doc = xmlReadMemory(xmlfile, st.st_size, "noname.xml", NULL, 0);
munmap(xmlfile, st.st_size);
close(fd);
xml_doc = xmlReadMemory(xmlfile, size, "noname.xml", NULL, 0);

free(xmlfile);

if (!xml_doc) {
fprintf(stderr, _("Failed to parse XML config file %s\n"),
vpninfo->xmlconfig);
Expand Down

0 comments on commit 113965a

Please sign in to comment.