diff options
author | Kim Alvefur <zash@zash.se> | 2017-01-23 19:34:14 +0100 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2017-01-23 19:34:14 +0100 |
commit | 468d3db1a1eb87026d20303eeda6ff46d459df80 (patch) | |
tree | ea895b1073f2ff2de7a08fc01f12797ab58c7125 /util-src | |
parent | 4c567124f67372cba0b688120eb726ab543b8bf1 (diff) | |
parent | b54ba7235144f22208ce22ca31da705a62531935 (diff) | |
download | prosody-468d3db1a1eb87026d20303eeda6ff46d459df80.tar.gz prosody-468d3db1a1eb87026d20303eeda6ff46d459df80.zip |
Merge 0.10->trunk
Diffstat (limited to 'util-src')
-rw-r--r-- | util-src/crand.c | 94 | ||||
-rw-r--r-- | util-src/encodings.c | 1 | ||||
-rw-r--r-- | util-src/hashes.c | 1 | ||||
-rw-r--r-- | util-src/pposix.c | 2 | ||||
-rw-r--r-- | util-src/ringbuffer.c | 28 | ||||
-rw-r--r-- | util-src/table.c | 1 |
6 files changed, 25 insertions, 102 deletions
diff --git a/util-src/crand.c b/util-src/crand.c index 177511ce..cc2047eb 100644 --- a/util-src/crand.c +++ b/util-src/crand.c @@ -1,7 +1,7 @@ /* Prosody IM --- Copyright (C) 2008-2016 Matthew Wild --- Copyright (C) 2008-2016 Waqas Hussain --- Copyright (C) 2016 Kim Alvefur +-- Copyright (C) 2008-2017 Matthew Wild +-- Copyright (C) 2008-2017 Waqas Hussain +-- Copyright (C) 2016-2017 Kim Alvefur -- -- This project is MIT/X11 licensed. Please see the -- COPYING file in the source package for more information. @@ -11,6 +11,12 @@ /* * crand.c * C PRNG interface +* +* The purpose of this module is to provide access to a PRNG in +* environments without /dev/urandom +* +* Caution! This has not been extensively tested. +* */ #include "lualib.h" @@ -19,15 +25,6 @@ #include <string.h> #include <errno.h> -/* - * TODO: Decide on fixed size or dynamically allocated buffer - */ -#if 1 -#include <stdlib.h> -#else -#define BUFLEN 256 -#endif - #if defined(WITH_GETRANDOM) #include <unistd.h> #include <sys/syscall.h> @@ -37,8 +34,11 @@ #error getrandom() requires Linux 3.17 or later #endif -/* Was this not supposed to be a function? */ -int getrandom(char *buf, size_t len, int flags) { +/* + * This acts like a read from /dev/urandom with the exception that it + * *does* block if the entropy pool is not yet initialized. + */ +int getrandom(void *buf, size_t len, int flags) { return syscall(SYS_getrandom, buf, len, flags); } @@ -51,39 +51,16 @@ int getrandom(char *buf, size_t len, int flags) { #endif int Lrandom(lua_State *L) { -#ifdef BUFLEN - unsigned char buf[BUFLEN]; -#else - unsigned char *buf; -#endif int ret = 0; - size_t len = (size_t)luaL_checkint(L, 1); -#ifdef BUFLEN - len = len > BUFLEN ? BUFLEN : len; -#else - buf = malloc(len); - - if(buf == NULL) { - lua_pushnil(L); - lua_pushstring(L, "out of memory"); - /* or it migth be better to - * return lua_error(L); - */ - return 2; - } -#endif + size_t len = (size_t)luaL_checkinteger(L, 1); + void *buf = lua_newuserdata(L, len); #if defined(WITH_GETRANDOM) ret = getrandom(buf, len, 0); if(ret < 0) { -#ifndef BUFLEN - free(buf); -#endif - lua_pushnil(L); lua_pushstring(L, strerror(errno)); - lua_pushinteger(L, errno); - return 3; + return lua_error(L); } #elif defined(WITH_ARC4RANDOM) @@ -95,40 +72,17 @@ int Lrandom(lua_State *L) { if(ret == 1) { ret = len; } else { -#ifndef BUFLEN - free(buf); -#endif - lua_pushnil(L); - lua_pushstring(L, "failed"); - /* lua_pushinteger(L, ERR_get_error()); */ - return 2; + /* TODO ERR_get_error() */ + lua_pushstring(L, "RAND_bytes() failed"); + return lua_error(L); } #endif - lua_pushlstring(L, (const char *)buf, ret); -#ifndef BUFLEN - free(buf); -#endif + lua_pushlstring(L, buf, ret); return 1; } -#ifdef ENABLE_SEEDING -int Lseed(lua_State *L) { - size_t len; - const char *seed = lua_tolstring(L, 1, &len); - -#if defined(WITH_OPENSSL) - RAND_add(seed, len, len); - return 0; -#else - lua_pushnil(L); - lua_pushliteral(L, "not-supported"); - return 2; -#endif -} -#endif - int luaopen_util_crand(lua_State *L) { #if (LUA_VERSION_NUM > 501) luaL_checkversion(L); @@ -136,10 +90,6 @@ int luaopen_util_crand(lua_State *L) { lua_newtable(L); lua_pushcfunction(L, Lrandom); lua_setfield(L, -2, "bytes"); -#ifdef ENABLE_SEEDING - lua_pushcfunction(L, Lseed); - lua_setfield(L, -2, "seed"); -#endif #if defined(WITH_GETRANDOM) lua_pushstring(L, "Linux"); @@ -151,7 +101,7 @@ int luaopen_util_crand(lua_State *L) { lua_setfield(L, -2, "_source"); #if defined(WITH_OPENSSL) && defined(_WIN32) - /* Do we need to seed this on Windows? */ + /* TODO Do we need to seed this on Windows? */ #endif return 1; diff --git a/util-src/encodings.c b/util-src/encodings.c index 6389b2be..4d6ac437 100644 --- a/util-src/encodings.c +++ b/util-src/encodings.c @@ -251,7 +251,6 @@ static const luaL_Reg Reg_utf8[] = { { NULL, NULL } }; - /***************** STRINGPREP *****************/ #ifdef USE_STRINGPREP_ICU diff --git a/util-src/hashes.c b/util-src/hashes.c index d6f848c7..697c632f 100644 --- a/util-src/hashes.c +++ b/util-src/hashes.c @@ -7,7 +7,6 @@ -- */ - /* * hashes.c * Lua library for sha1, sha256 and md5 hashes diff --git a/util-src/pposix.c b/util-src/pposix.c index 39d8742b..b6874318 100644 --- a/util-src/pposix.c +++ b/util-src/pposix.c @@ -595,8 +595,6 @@ int lc_getrlimit(lua_State* L) { return 2; } - - resource = luaL_checkstring(L, 1); rid = string2resource(resource); diff --git a/util-src/ringbuffer.c b/util-src/ringbuffer.c index d60a43d9..73a8616b 100644 --- a/util-src/ringbuffer.c +++ b/util-src/ringbuffer.c @@ -1,5 +1,4 @@ - #include <stdlib.h> #include <unistd.h> #include <string.h> @@ -8,15 +7,12 @@ #include <lua.h> #include <lauxlib.h> -#define MIN(a, b) ((a)>(b)?(b):(a)) -#define MAX(a, b) ((a)>(b)?(a):(b)) - typedef struct { size_t rpos; /* read position */ size_t wpos; /* write position */ size_t alen; /* allocated size */ size_t blen; /* current content size */ - char* buffer; + char buffer[]; } ringbuffer; char readchar(ringbuffer* b) { @@ -76,7 +72,6 @@ int rb_find(lua_State* L) { return 0; } - int rb_read(lua_State* L) { ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); int r = luaL_checkinteger(L, 2); @@ -104,7 +99,6 @@ int rb_read(lua_State* L) { return 1; } - int rb_readuntil(lua_State* L) { size_t l, m; ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); @@ -169,16 +163,12 @@ int rb_free(lua_State* L) { int rb_new(lua_State* L) { size_t size = luaL_optinteger(L, 1, sysconf(_SC_PAGESIZE)); - ringbuffer* b = lua_newuserdata(L, sizeof(ringbuffer)); + ringbuffer *b = lua_newuserdata(L, sizeof(ringbuffer) + size); + b->rpos = 0; b->wpos = 0; b->alen = size; b->blen = 0; - b->buffer = malloc(size); - - if(b->buffer == NULL) { - return 0; - } luaL_getmetatable(L, "ringbuffer_mt"); lua_setmetatable(L, -2); @@ -186,16 +176,6 @@ int rb_new(lua_State* L) { return 1; } -int rb_gc(lua_State* L) { - ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); - - if(b->buffer != NULL) { - free(b->buffer); - } - - return 0; -} - int luaopen_util_ringbuffer(lua_State* L) { #if (LUA_VERSION_NUM > 501) luaL_checkversion(L); @@ -205,8 +185,6 @@ int luaopen_util_ringbuffer(lua_State* L) { lua_setfield(L, -2, "__tostring"); lua_pushcfunction(L, rb_length); lua_setfield(L, -2, "__len"); - lua_pushcfunction(L, rb_gc); - lua_setfield(L, -2, "__gc"); lua_newtable(L); /* __index */ { diff --git a/util-src/table.c b/util-src/table.c index 93acae65..c9c09170 100644 --- a/util-src/table.c +++ b/util-src/table.c @@ -19,7 +19,6 @@ static int Lpack(lua_State* L) { return 1; } - int luaopen_util_table(lua_State* L) { #if (LUA_VERSION_NUM > 501) luaL_checkversion(L); |