Move random string generation to random.c

This commit is contained in:
Heikki Hannikainen 2017-04-14 00:28:11 +03:00
parent 45fc046f5e
commit c1385db760
4 changed files with 75 additions and 53 deletions

View File

@ -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 \

View File

@ -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;

64
src/random.c Normal file
View File

@ -0,0 +1,64 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#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;
}

9
src/random.h Normal file
View File

@ -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