Skip to content

Commit

Permalink
Move read_file_into_string() to ssl.c and rename it
Browse files Browse the repository at this point in the history
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 848ceff
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 67 deletions.
1 change: 1 addition & 0 deletions libopenconnect.map.in
Expand Up @@ -109,6 +109,7 @@ OPENCONNECT_PRIVATE {
openconnect_open_utf8;
openconnect_sha1;
openconnect_version_str;
openconnect_read_file;
local:
*;
};
7 changes: 4 additions & 3 deletions main.c
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions openconnect-internal.h
Expand Up @@ -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);
Expand Down Expand Up @@ -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 */
Expand Down
60 changes: 60 additions & 0 deletions ssl.c
Expand Up @@ -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);
Expand Down
63 changes: 1 addition & 62 deletions xml.c
Expand Up @@ -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>
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 848ceff

Please sign in to comment.