From 2ab42c8dbf95ea250e4dd16b21b12c2b6dddb5b1 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 17 May 2013 14:52:52 +0100 Subject: util.ip: Automatically determine protocol of IP address if none specified. Return error if invalid. [Backported from 0.10] --- util/ip.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'util') diff --git a/util/ip.lua b/util/ip.lua index 856bf034..226432cc 100644 --- a/util/ip.lua +++ b/util/ip.lua @@ -12,7 +12,15 @@ local ip_mt = { __index = function (ip, key) return (ip_methods[key])(ip); end, local hex2bits = { ["0"] = "0000", ["1"] = "0001", ["2"] = "0010", ["3"] = "0011", ["4"] = "0100", ["5"] = "0101", ["6"] = "0110", ["7"] = "0111", ["8"] = "1000", ["9"] = "1001", ["A"] = "1010", ["B"] = "1011", ["C"] = "1100", ["D"] = "1101", ["E"] = "1110", ["F"] = "1111" }; local function new_ip(ipStr, proto) - if proto ~= "IPv4" and proto ~= "IPv6" then + if not proto then + local sep = ipStr:match("^%x+(.)"); + if sep == ":" then proto = "IPv6" + elseif sep == "." then proto = "IPv4" + end + if not proto then + return nil, "invalid address"; + end + elseif proto ~= "IPv4" and proto ~= "IPv6" then return nil, "invalid protocol"; end if proto == "IPv6" and ipStr:find('.', 1, true) then -- cgit v1.2.3 From 2b6402720494ef8632a2f26970b9cc935dee8d41 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sat, 18 May 2013 21:41:17 +0100 Subject: util.ip: Fix protocol detection of IPv6 addresses beginning with : [Backported from 0.10] --- util/ip.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'util') diff --git a/util/ip.lua b/util/ip.lua index 226432cc..043303ee 100644 --- a/util/ip.lua +++ b/util/ip.lua @@ -14,8 +14,10 @@ local hex2bits = { ["0"] = "0000", ["1"] = "0001", ["2"] = "0010", ["3"] = "0011 local function new_ip(ipStr, proto) if not proto then local sep = ipStr:match("^%x+(.)"); - if sep == ":" then proto = "IPv6" - elseif sep == "." then proto = "IPv4" + if sep == ":" or (not(sep) and ipStr:sub(1,1) == ":") then + proto = "IPv6" + elseif sep == "." then + proto = "IPv4" end if not proto then return nil, "invalid address"; -- cgit v1.2.3 From e03302f412a0c4fa2c239251851baf9e99fbff3a Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 4 Jan 2016 17:47:40 +0100 Subject: util.ip: Support zone id syntax in IPv6 addresses --- util/ip.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'util') diff --git a/util/ip.lua b/util/ip.lua index 043303ee..acfd7f24 100644 --- a/util/ip.lua +++ b/util/ip.lua @@ -25,6 +25,10 @@ local function new_ip(ipStr, proto) elseif proto ~= "IPv4" and proto ~= "IPv6" then return nil, "invalid protocol"; end + local zone; + if proto == "IPv6" and ipStr:find('%', 1, true) then + ipStr, zone = ipStr:match("^(.-)%%(.*)"); + end if proto == "IPv6" and ipStr:find('.', 1, true) then local changed; ipStr, changed = ipStr:gsub(":(%d+)%.(%d+)%.(%d+)%.(%d+)$", function(a,b,c,d) @@ -33,7 +37,7 @@ local function new_ip(ipStr, proto) if changed ~= 1 then return nil, "invalid-address"; end end - return setmetatable({ addr = ipStr, proto = proto }, ip_mt); + return setmetatable({ addr = ipStr, proto = proto, zone = zone }, ip_mt); end local function toBits(ip) -- cgit v1.2.3 From 8a8f379272deeb7dbaeb35c3aadb0b03def8849e Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 6 Jan 2016 02:46:47 +0100 Subject: util.uuid: Use /dev/urandom --- util/uuid.lua | 43 ++++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 27 deletions(-) (limited to 'util') diff --git a/util/uuid.lua b/util/uuid.lua index bb70d000..58f792fd 100644 --- a/util/uuid.lua +++ b/util/uuid.lua @@ -6,44 +6,33 @@ -- COPYING file in the source package for more information. -- - -local tostring = tostring; -local os_time = os.time; -local os_clock = os.clock; -local sha1 = require "util.hashes".sha1; +local error = error; +local round_up = math.ceil; +local urandom, urandom_err = io.open("/dev/urandom", "r+"); module "uuid" -local last_uniq_time = 0; -local function uniq_time() - local new_uniq_time = os_time(); - if last_uniq_time >= new_uniq_time then new_uniq_time = last_uniq_time + 1; end - last_uniq_time = new_uniq_time; - return new_uniq_time; -end - -local function new_random(x) - return sha1(x..os_clock()..tostring({}), true); -end - -local buffer = new_random(uniq_time()); -local function _seed(x) - buffer = new_random(buffer..x); -end local function get_nibbles(n) - if #buffer < n then _seed(uniq_time()); end - local r = buffer:sub(0, n); - buffer = buffer:sub(n+1); - return r; + local binary_random = urandom:read(round_up(n/2)); + local hex_random = binary_random:gsub(".", + function (x) return ("%02x"):format(x:byte()) end); + return hex_random:sub(1, n); end local function get_twobits() - return ("%x"):format(get_nibbles(1):byte() % 4 + 8); + return ("%x"):format(urandom:read(1):byte() % 4 + 8); end function generate() + if not urandom then + error("Unable to obtain a secure random number generator, please see https://prosody.im/doc/random ("..urandom_err..")"); + end -- generate RFC 4122 complaint UUIDs (version 4 - random) return get_nibbles(8).."-"..get_nibbles(4).."-4"..get_nibbles(3).."-"..(get_twobits())..get_nibbles(3).."-"..get_nibbles(12); end -seed = _seed; + +function seed(x) + urandom:write(x); + urandom:flush(); +end return _M; -- cgit v1.2.3 From c515c93724a691a3fdbcc97e7684c09fbeaa338f Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 6 Jan 2016 03:28:31 +0100 Subject: util.uuid: Take random byte directly instead of the low bits from the ascii value of a hex nibble --- util/uuid.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'util') diff --git a/util/uuid.lua b/util/uuid.lua index e10fc0f7..f4fd21f6 100644 --- a/util/uuid.lua +++ b/util/uuid.lua @@ -16,7 +16,7 @@ local function get_nibbles(n) end local function get_twobits() - return ("%x"):format(get_nibbles(1):byte() % 4 + 8); + return ("%x"):format(random_bytes(1):byte() % 4 + 8); end local function generate() -- cgit v1.2.3 From b057664428d529b182ac9a69800402d2f517b43b Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 6 Jan 2016 03:28:56 +0100 Subject: util.random: Use /dev/urandom --- util/random.lua | 28 ++++------------------------ 1 file changed, 4 insertions(+), 24 deletions(-) (limited to 'util') diff --git a/util/random.lua b/util/random.lua index 5938a94f..4963e98c 100644 --- a/util/random.lua +++ b/util/random.lua @@ -6,35 +6,15 @@ -- COPYING file in the source package for more information. -- -local tostring = tostring; -local os_time = os.time; -local os_clock = os.clock; -local ceil = math.ceil; -local H = require "util.hashes".sha512; - -local last_uniq_time = 0; -local function uniq_time() - local new_uniq_time = os_time(); - if last_uniq_time >= new_uniq_time then new_uniq_time = last_uniq_time + 1; end - last_uniq_time = new_uniq_time; - return new_uniq_time; -end - -local function new_random(x) - return H(x..os_clock()..tostring({})); -end - -local buffer = new_random(uniq_time()); +local urandom = assert(io.open("/dev/urandom", "r+")); local function seed(x) - buffer = new_random(buffer..x); + urandom:write(x); + urandom:flush(); end local function bytes(n) - if #buffer < n+4 then seed(uniq_time()); end - local r = buffer:sub(1, n); - buffer = buffer:sub(n+1); - return r; + return urandom:read(n); end return { -- cgit v1.2.3