mmap() ipsec.secrets instead malloc(), proper error checking

This commit is contained in:
Martin Willi
2010-08-04 09:26:21 +02:00
parent 947298b302
commit 9587ece534
+30 -18
View File
@@ -14,10 +14,15 @@
* for more details. * for more details.
*/ */
#include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <limits.h> #include <limits.h>
#include <glob.h> #include <glob.h>
#include <libgen.h> #include <libgen.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include "stroke_cred.h" #include "stroke_cred.h"
#include "stroke_shared_key.h" #include "stroke_shared_key.h"
@@ -1032,30 +1037,36 @@ static bool load_shared(private_stroke_cred_t *this, chunk_t line, int line_nr,
static void load_secrets(private_stroke_cred_t *this, char *file, int level, static void load_secrets(private_stroke_cred_t *this, char *file, int level,
FILE *prompt) FILE *prompt)
{ {
size_t bytes; int line_nr = 0, fd;
int line_nr = 0; chunk_t src, line;
chunk_t chunk, src, line;
FILE *fd;
private_key_t *private; private_key_t *private;
shared_key_t *shared; shared_key_t *shared;
struct stat sb;
void *addr;
DBG1(DBG_CFG, "loading secrets from '%s'", file); DBG1(DBG_CFG, "loading secrets from '%s'", file);
fd = open(file, O_RDONLY);
fd = fopen(file, "r"); if (fd == -1)
if (fd == NULL)
{ {
DBG1(DBG_CFG, "opening secrets file '%s' failed", file); DBG1(DBG_CFG, "opening secrets file '%s' failed: %s", file,
strerror(errno));
return; return;
} }
if (fstat(fd, &sb) == -1)
/* TODO: do error checks */ {
fseek(fd, 0, SEEK_END); DBG1(DBG_LIB, "getting file size of '%s' failed: %s", file,
chunk.len = ftell(fd); strerror(errno));
rewind(fd); close(fd);
chunk.ptr = malloc(chunk.len); return;
bytes = fread(chunk.ptr, 1, chunk.len, fd); }
fclose(fd); addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
src = chunk; if (addr == MAP_FAILED)
{
DBG1(DBG_LIB, "mapping '%s' failed: %s", file, strerror(errno));
close(fd);
return;
}
src = chunk_create(addr, sb.st_size);
if (level == 0) if (level == 0)
{ {
@@ -1204,7 +1215,8 @@ static void load_secrets(private_stroke_cred_t *this, char *file, int level,
{ {
this->lock->unlock(this->lock); this->lock->unlock(this->lock);
} }
chunk_clear(&chunk); munmap(addr, sb.st_size);
close(fd);
} }
/** /**