Commit 4881f4bf authored by Boleslav Brezovsky's avatar Boleslav Brezovsky Committed by Boleslav Březovský
Browse files

Added wrappers for hash functions to load data in C

parent 4e8363df
...@@ -35,7 +35,7 @@ local run_util = run_util ...@@ -35,7 +35,7 @@ local run_util = run_util
module "utils" module "utils"
-- luacheck: globals lines2set map set2arr arr2set cleanup_dirs slurp clone shallow_copy table_merge arr_append exception multi_index private filter_best strip table_overlay randstr arr_prune arr_inv -- luacheck: globals lines2set map set2arr arr2set cleanup_dirs slurp clone shallow_copy table_merge arr_append exception multi_index private filter_best strip table_overlay randstr arr_prune arr_inv file_exists
--[[ --[[
Convert provided text into set of lines. Doesn't care about the order. Convert provided text into set of lines. Doesn't care about the order.
...@@ -304,4 +304,17 @@ function table_overlay(table) ...@@ -304,4 +304,17 @@ function table_overlay(table)
}) })
end end
--[[
Check whether file exists
]]
function file_exists(name)
local f = io.open(name, "r")
if f ~= nil then
io.close(f)
return true
else
return false
end
end
return _M return _M
...@@ -44,8 +44,8 @@ local lstat = lstat ...@@ -44,8 +44,8 @@ local lstat = lstat
local mkdir = mkdir local mkdir = mkdir
local move = move local move = move
local ls = ls local ls = ls
local md5 = md5 local md5_file = md5_file
local sha256 = sha256 local sha256_file = sha256_file
local DBG = DBG local DBG = DBG
local WARN = WARN local WARN = WARN
local utils = require "utils" local utils = require "utils"
...@@ -521,11 +521,11 @@ function pkg_examine(dir) ...@@ -521,11 +521,11 @@ function pkg_examine(dir)
if cidx then if cidx then
for l in cidx:lines() do for l in cidx:lines() do
local fname = l:match("^%s*(/.*%S)%s*") local fname = l:match("^%s*(/.*%S)%s*")
local content, err = utils.slurp(data_dir .. fname) if utils.file_exists(data_dir .. fname) then
if not content then conffiles[fname] = sha256_file(data_dir .. fname)
error(err) else
error("File " .. fname .. " does not exist.")
end end
conffiles[fname] = sha256(content)
end end
cidx:close() cidx:close()
end end
...@@ -1045,28 +1045,27 @@ function config_modified(file, hash) ...@@ -1045,28 +1045,27 @@ function config_modified(file, hash)
local len = hash:len() local len = hash:len()
local hasher local hasher
if len == 32 then if len == 32 then
hasher = md5 hasher = md5_file
elseif len == 64 then elseif len == 64 then
hasher = sha256 hasher = sha256_file
elseif len > 32 and len < 64 then elseif len > 32 and len < 64 then
--[[ --[[
Something produces (produced?) truncated hashes in the status file. Something produces (produced?) truncated hashes in the status file.
Handle them. This is likely already fixed, but we don't want to Handle them. This is likely already fixed, but we don't want to
crash on system that still have these broken hashes around. crash on system that still have these broken hashes around.
]] ]]
hasher = function (content) hasher = function (file)
WARN("Truncated sha256 hash seen, using bug compat mode") WARN("Truncated sha256 hash seen, using bug compat mode")
return sha256(content):sub(1, len) return sha256_file(file):sub(1, len)
end end
else else
error("Can not determine hash algorithm to use for hash " .. hash) error("Can not determine hash algorithm to use for hash " .. hash)
end end
local content = utils.slurp(file) if utils.file_exists(file) then
if content then local got = hasher(file):lower()
local got = hasher(content):lower()
hash = hash:lower() hash = hash:lower()
DBG("Hashes: " .. got .. " " .. hash) DBG("Hashes: " .. got .. " " .. hash)
return hasher(content):lower() ~= hash:lower() return hasher(file):lower() ~= hash:lower()
else else
return nil return nil
end end
......
...@@ -41,6 +41,7 @@ ...@@ -41,6 +41,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <dirent.h> #include <dirent.h>
#include <stdio.h>
// The name used in lua registry to store stuff // The name used in lua registry to store stuff
#define REGISTRY_NAME "libupdater" #define REGISTRY_NAME "libupdater"
...@@ -687,6 +688,24 @@ static int lua_md5(lua_State *L) { ...@@ -687,6 +688,24 @@ static int lua_md5(lua_State *L) {
return 1; return 1;
} }
static int lua_md5_file(lua_State *L) {
size_t len;
const char *filename = luaL_checklstring(L, 1, &len);
FILE *f = fopen(filename, "rb");
fseek (f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
char *buffer = malloc(fsize + 1);
fread(buffer, fsize, 1, f);
fclose(f);
buffer[fsize] = 0;
uint8_t result[MD5_DIGEST_SIZE];
md5_buffer(buffer, fsize, result);
push_hex(L, result, sizeof result);
free(buffer);
return 1;
}
static int lua_sha256(lua_State *L) { static int lua_sha256(lua_State *L) {
size_t len; size_t len;
const char *buffer = luaL_checklstring(L, 1, &len); const char *buffer = luaL_checklstring(L, 1, &len);
...@@ -696,6 +715,25 @@ static int lua_sha256(lua_State *L) { ...@@ -696,6 +715,25 @@ static int lua_sha256(lua_State *L) {
return 1; return 1;
} }
static int lua_sha256_file(lua_State *L) {
size_t len;
const char *filename = luaL_checklstring(L, 1, &len);
FILE *f = fopen(filename, "rb");
fseek (f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
char *buffer = malloc(fsize + 1);
fread(buffer, fsize, 1, f);
fclose(f);
buffer[fsize] = 0;
uint8_t result[SHA256_DIGEST_SIZE];
sha256_buffer(buffer, fsize, result);
push_hex(L, result, sizeof result);
free(buffer);
return 1;
}
static int lua_reexec(lua_State *L) { static int lua_reexec(lua_State *L) {
size_t args_c = lua_gettop(L); size_t args_c = lua_gettop(L);
const char *args[args_c]; const char *args[args_c];
...@@ -771,7 +809,9 @@ static const struct injected_func injected_funcs[] = { ...@@ -771,7 +809,9 @@ static const struct injected_func injected_funcs[] = {
{ lua_sync, "sync" }, { lua_sync, "sync" },
{ lua_setenv, "setenv" }, { lua_setenv, "setenv" },
{ lua_md5, "md5" }, { lua_md5, "md5" },
{ lua_md5_file, "md5_file" },
{ lua_sha256, "sha256" }, { lua_sha256, "sha256" },
{ lua_sha256_file, "sha256_file" },
{ lua_reexec, "reexec" }, { lua_reexec, "reexec" },
{ lua_uri_internal_get, "uri_internal_get" }, { lua_uri_internal_get, "uri_internal_get" },
{ lua_system_reboot, "system_reboot" }, { lua_system_reboot, "system_reboot" },
......
...@@ -11,7 +11,7 @@ globals = { ...@@ -11,7 +11,7 @@ globals = {
-- lua51 doesn't contains? -- lua51 doesn't contains?
"_M", "_M",
-- From interpreter.c -- From interpreter.c
"log", "state_log_enabled", "state_dump", "run_command", "run_util", "download", "events_wait", "mkdtemp", "chdir", "getcwd", "mkdir", "move", "ls", "stat", "lstat", "sync", "setenv", "md5", "sha256", "reexec", "uri_internal_get", "system_reboot", "get_updater_version", "log", "state_log_enabled", "state_dump", "run_command", "run_util", "download", "events_wait", "mkdtemp", "chdir", "getcwd", "mkdir", "move", "ls", "stat", "lstat", "sync", "setenv", "md5", "sha256", "md5_file", "sha256_file", "reexec", "uri_internal_get", "system_reboot", "get_updater_version",
-- From logging -- From logging
"ERROR", "WARN", "INFO", "DBG", "TRACE", "DIE", "log_event", "c_pcall_error_handler", "ERROR", "WARN", "INFO", "DBG", "TRACE", "DIE", "log_event", "c_pcall_error_handler",
-- From dumper -- From dumper
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment