From c1385db7609a61b29aa3e07a450e8f2fa2876023 Mon Sep 17 00:00:00 2001 From: Heikki Hannikainen Date: Fri, 14 Apr 2017 00:28:11 +0300 Subject: [PATCH] Move random string generation to random.c --- src/Makefile.in | 2 +- src/aprsc.c | 53 +--------------------------------------- src/random.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ src/random.h | 9 +++++++ 4 files changed, 75 insertions(+), 53 deletions(-) create mode 100644 src/random.c create mode 100644 src/random.h diff --git a/src/Makefile.in b/src/Makefile.in index c7258bb..402f8df 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -163,7 +163,7 @@ OBJS = aprsc.o accept.o worker.o errno_aprsc.o \ messaging.o \ config.o netlib.o xpoll.o acl.o \ cfgfile.o passcode.o uplink.o \ - rwlock.o hmalloc.o hlog.o \ + rwlock.o hmalloc.o hlog.o random.o \ keyhash.o \ filter.o cellmalloc.o historydb.o \ counterdata.o status.o cJSON.o \ diff --git a/src/aprsc.c b/src/aprsc.c index 8324ece..4ef3d1c 100644 --- a/src/aprsc.c +++ b/src/aprsc.c @@ -44,6 +44,7 @@ #include "status.h" #include "http.h" #include "version.h" +#include "random.h" #include "dupecheck.h" #include "filter.h" @@ -554,58 +555,6 @@ static void check_uid(void) * source and converting the binary data to lower-case alphanumeric */ -static int urandom_open(void) -{ - int fd; - - if ((fd = open("/dev/urandom", O_RDONLY)) == -1) { - hlog(LOG_ERR, "open(/dev/urandom) failed: %s", strerror(errno)); - } - - return fd; -} - -static int urandom_alphanumeric(int fd, unsigned char *buf, int buflen) -{ - int l; - int len = buflen - 1; - unsigned char c; - - if (fd >= 0) { - /* generate instance id */ - l = read(fd, buf, len); - if (l != len) { - hlog(LOG_ERR, "read(/dev/urandom, %d) failed: %s", len, strerror(errno)); - close(fd); - fd = -1; - } - } - - if (fd < 0) { - /* urandom failed for us, use something inferior */ - for (l = 0; l < len; l++) { - // coverity[dont_call] // squelch warning: not security sensitive use of random() - buf[l] = random() % 256; - } - } - - for (l = 0; l < len; l++) { - /* 256 is not divisible by 36, the distribution is slightly skewed, - * but that's not serious. - */ - c = buf[l] % (26 + 10); /* letters and numbers */ - if (c < 10) - c += 48; /* number */ - else - c = c - 10 + 97; /* letter */ - buf[l] = c; - } - - buf[len] = 0; - - return len; -} - static void generate_instance_id(void) { int fd, l; diff --git a/src/random.c b/src/random.c new file mode 100644 index 0000000..f635b9a --- /dev/null +++ b/src/random.c @@ -0,0 +1,64 @@ + +#include +#include +#include +#include +#include +#include +#include + +#include "random.h" +#include "hlog.h" + +int urandom_open(void) +{ + int fd; + + if ((fd = open("/dev/urandom", O_RDONLY)) == -1) { + hlog(LOG_ERR, "open(/dev/urandom) failed: %s", strerror(errno)); + } + + return fd; +} + +int urandom_alphanumeric(int fd, unsigned char *buf, int buflen) +{ + int l; + int len = buflen - 1; + unsigned char c; + + if (fd >= 0) { + /* generate instance id */ + l = read(fd, buf, len); + if (l != len) { + hlog(LOG_ERR, "read(/dev/urandom, %d) failed: %s", len, strerror(errno)); + close(fd); + fd = -1; + } + } + + if (fd < 0) { + /* urandom failed for us, use something inferior */ + for (l = 0; l < len; l++) { + // coverity[dont_call] // squelch warning: not security sensitive use of random() + buf[l] = random() % 256; + } + } + + for (l = 0; l < len; l++) { + /* 256 is not divisible by 36, the distribution is slightly skewed, + * but that's not serious. + */ + c = buf[l] % (26 + 10); /* letters and numbers */ + if (c < 10) + c += 48; /* number */ + else + c = c - 10 + 97; /* letter */ + buf[l] = c; + } + + buf[len] = 0; + + return len; +} + diff --git a/src/random.h b/src/random.h new file mode 100644 index 0000000..b2cc158 --- /dev/null +++ b/src/random.h @@ -0,0 +1,9 @@ + +#ifndef RANDOM_H +#define RANDOM_H + +extern int urandom_open(void); +extern int urandom_alphanumeric(int fd, unsigned char *buf, int buflen); + +#endif +