Skip to content

Commit

Permalink
xml: Make a generic function to read a file into a string
Browse files Browse the repository at this point in the history
This replaces load_xml_conf_file(); it can be called by main.c for other
purposes.

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
  • Loading branch information
cernekee committed Aug 2, 2014
1 parent 24c3fb4 commit 8d7853a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 38 deletions.
2 changes: 2 additions & 0 deletions openconnect-internal.h
Expand Up @@ -620,6 +620,8 @@ int keepalive_action(struct keepalive_info *ka, int *timeout);
int ka_stalled_action(struct keepalive_info *ka, int *timeout);

/* xml.c */
ssize_t read_file_into_string(struct openconnect_info *vpninfo, const char *fname,
char **ptr);
int config_lookup_host(struct openconnect_info *vpninfo, const char *host);

/* auth.c */
Expand Down
75 changes: 37 additions & 38 deletions xml.c
Expand Up @@ -31,60 +31,59 @@

#include "openconnect-internal.h"

static int load_xml_conf_file(struct openconnect_info *vpninfo, char **ptr,
size_t *size)
ssize_t read_file_into_string(struct openconnect_info *vpninfo, const char *fname,
char **ptr)
{
int fd, len;
struct stat st;
int fd;
char *xmlfile = NULL;
char *buf;

fd = open(vpninfo->xmlconfig, O_RDONLY);
/* FIXME: UTF-8 fname may need conversion */
fd = open(fname, O_RDONLY);
if (fd < 0) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to open XML config file: %s\n"),
strerror(errno));
return 0;
_("Failed to open %s: %s\n"),
fname, strerror(errno));
return -ENOENT;
}

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

xmlfile = malloc(st.st_size);
if (!xmlfile) {
len = st.st_size;
buf = malloc(len + 1);
if (!buf) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to allocate %lu bytes for XML config file\n"),
(unsigned long)st.st_size);
goto err;
_("Failed to allocate %d bytes for %s\n"),
len + 1, fname);
close(fd);
return -ENOMEM;
}

if (read(fd, xmlfile, st.st_size) != st.st_size) {
if (read(fd, buf, len) != len) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to read XML config file: %s\n"),
strerror(errno));
goto err;
}

*ptr = xmlfile;
*size = st.st_size;

_("Failed to read %s: %s\n"),
fname, strerror(errno));
free(buf);
close(fd);
return -EIO;
}

return 1;

err:
buf[len] = 0;
close(fd);
free(xmlfile);
return -1;
*ptr = buf;
return len;
}

int config_lookup_host(struct openconnect_info *vpninfo, const char *host)
{
int i, ret;
size_t size;
int i;
ssize_t size;
char *xmlfile;
unsigned char sha1[SHA1_SIZE];
xmlDocPtr xml_doc;
Expand All @@ -93,12 +92,12 @@ int config_lookup_host(struct openconnect_info *vpninfo, const char *host)
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;
size = read_file_into_string(vpninfo, vpninfo->xmlconfig, &xmlfile);
if (size == -ENOENT) {
fprintf(stderr, _("Treating host \"%s\" as a raw hostname\n"), host);
return 0;
} else if (size <= 0) {
return size;
}

if (openconnect_sha1(sha1, xmlfile, size)) {
Expand Down

0 comments on commit 8d7853a

Please sign in to comment.