Skip to content
Permalink
Browse files
Move read_file_into_string() to ssl.c and rename it
We'll want to use this from stoken.c too.

Signed-off-by: David Woodhouse <dwmw2@infradead.org>
  • Loading branch information
dwmw2 committed Dec 29, 2019
1 parent 33a87b1 commit 848ceff1da1edb4f798f0fb6e989d0590f62e160
Showing with 68 additions and 67 deletions.
  1. +1 −0 libopenconnect.map.in
  2. +4 −3 main.c
  3. +2 −2 openconnect-internal.h
  4. +60 −0 ssl.c
  5. +1 −62 xml.c
@@ -109,6 +109,7 @@ OPENCONNECT_PRIVATE {
openconnect_open_utf8;
openconnect_sha1;
openconnect_version_str;
openconnect_read_file;
local:
*;
};
7 main.c
@@ -2123,7 +2123,7 @@ static int lock_token(void *tokdata)
int err;

/* FIXME: Actually lock the file */
err = read_file_into_string(vpninfo, token_filename, &file_token);
err = openconnect_read_file(vpninfo, token_filename, &file_token);
if (err < 0)
return err;

@@ -2170,13 +2170,14 @@ static void init_token(struct openconnect_info *vpninfo,
int ret;
char *file_token = NULL;

if (token_str) {
if (token_str && (token_mode == OC_TOKEN_MODE_TOTP ||
token_mode == OC_TOKEN_MODE_HOTP)) {
switch(token_str[0]) {
case '@':
token_str++;
/* fall through... */
case '/':
if (read_file_into_string(vpninfo, token_str,
if (openconnect_read_file(vpninfo, token_str,
&file_token) < 0)
exit(1);
break;
@@ -959,6 +959,8 @@ int openconnect_open_utf8(struct openconnect_info *vpninfo,
const char *fname, int mode);
FILE *openconnect_fopen_utf8(struct openconnect_info *vpninfo,
const char *fname, const char *mode);
ssize_t openconnect_read_file(struct openconnect_info *vpninfo, const char *fname,
char **ptr);
int udp_sockaddr(struct openconnect_info *vpninfo, int port);
int udp_connect(struct openconnect_info *vpninfo);
int ssl_reconnect(struct openconnect_info *vpninfo);
@@ -1025,8 +1027,6 @@ int ka_stalled_action(struct keepalive_info *ka, int *timeout);
int ka_check_deadline(int *timeout, time_t now, time_t due);

/* 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);

/* oath.c */
60 ssl.c
@@ -918,6 +918,66 @@ FILE *openconnect_fopen_utf8(struct openconnect_info *vpninfo, const char *fname
return fdopen(fd, mode);
}

ssize_t openconnect_read_file(struct openconnect_info *vpninfo, const char *fname,
char **ptr)
{
int fd, len;
struct stat st;
char *buf;

fd = openconnect_open_utf8(vpninfo, fname, O_RDONLY|O_BINARY);
if (fd < 0) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to open %s: %s\n"),
fname, strerror(errno));
return -ENOENT;
}

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

if (st.st_size == 0) {
vpn_progress(vpninfo, PRG_INFO, _("File %s is empty\n"),
vpninfo->xmlconfig);
close(fd);
return -ENOENT;
}
if (st.st_size >= INT_MAX || st.st_size < 0) {
vpn_progress(vpninfo, PRG_INFO, _("File %s has suspicious size %zd\n"),
vpninfo->xmlconfig, (ssize_t)st.st_size);
close(fd);
return -EIO;
}
len = st.st_size;
buf = malloc(len + 1);
if (!buf) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to allocate %d bytes for %s\n"),
len + 1, fname);
close(fd);
return -ENOMEM;
}

if (read(fd, buf, len) != len) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to read %s: %s\n"),
fname, strerror(errno));
free(buf);
close(fd);
return -EIO;
}

buf[len] = 0;
close(fd);
*ptr = buf;
return len;
}

int udp_sockaddr(struct openconnect_info *vpninfo, int port)
{
free(vpninfo->dtls_addr);
63 xml.c
@@ -23,7 +23,6 @@
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <string.h>
@@ -32,66 +31,6 @@

#include "openconnect-internal.h"

ssize_t read_file_into_string(struct openconnect_info *vpninfo, const char *fname,
char **ptr)
{
int fd, len;
struct stat st;
char *buf;

fd = openconnect_open_utf8(vpninfo, fname, O_RDONLY|O_BINARY);
if (fd < 0) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to open %s: %s\n"),
fname, strerror(errno));
return -ENOENT;
}

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

if (st.st_size == 0) {
vpn_progress(vpninfo, PRG_INFO, _("XML file %s is empty\n"),
vpninfo->xmlconfig);
close(fd);
return -ENOENT;
}
if (st.st_size >= INT_MAX || st.st_size < 0) {
vpn_progress(vpninfo, PRG_INFO, _("XML file %s has suspicious size %zd\n"),
vpninfo->xmlconfig, (ssize_t)st.st_size);
close(fd);
return -EIO;
}
len = st.st_size;
buf = malloc(len + 1);
if (!buf) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to allocate %d bytes for %s\n"),
len + 1, fname);
close(fd);
return -ENOMEM;
}

if (read(fd, buf, len) != len) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to read %s: %s\n"),
fname, strerror(errno));
free(buf);
close(fd);
return -EIO;
}

buf[len] = 0;
close(fd);
*ptr = buf;
return len;
}

static char *fetch_and_trim(xmlNode *node)
{
char *str = (char *)xmlNodeGetContent(node), *p;
@@ -131,7 +70,7 @@ int config_lookup_host(struct openconnect_info *vpninfo, const char *host)
if (!vpninfo->xmlconfig)
return 0;

size = read_file_into_string(vpninfo, vpninfo->xmlconfig, &xmlfile);
size = openconnect_read_file(vpninfo, vpninfo->xmlconfig, &xmlfile);
if (size == -ENOENT) {
fprintf(stderr, _("Treating host \"%s\" as a raw hostname\n"), host);
return 0;

0 comments on commit 848ceff

Please sign in to comment.