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
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.
......@@ -304,4 +304,17 @@ function table_overlay(table)
})
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
......@@ -44,8 +44,8 @@ local lstat = lstat
local mkdir = mkdir
local move = move
local ls = ls
local md5 = md5
local sha256 = sha256
local md5_file = md5_file
local sha256_file = sha256_file
local DBG = DBG
local WARN = WARN
local utils = require "utils"
......@@ -521,11 +521,11 @@ function pkg_examine(dir)
if cidx then
for l in cidx:lines() do
local fname = l:match("^%s*(/.*%S)%s*")
local content, err = utils.slurp(data_dir .. fname)
if not content then
error(err)
if utils.file_exists(data_dir .. fname) then
conffiles[fname] = sha256_file(data_dir .. fname)
else
error("File " .. fname .. " does not exist.")
end
conffiles[fname] = sha256(content)
end
cidx:close()
end
......@@ -1045,28 +1045,27 @@ function config_modified(file, hash)
local len = hash:len()
local hasher
if len == 32 then
hasher = md5
hasher = md5_file
elseif len == 64 then
hasher = sha256
hasher = sha256_file
elseif len > 32 and len < 64 then
--[[
Something produces (produced?) truncated hashes in the status file.
Handle them. This is likely already fixed, but we don't want to
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")
return sha256(content):sub(1, len)
return sha256_file(file):sub(1, len)
end
else
error("Can not determine hash algorithm to use for hash " .. hash)
end
local content = utils.slurp(file)
if content then
local got = hasher(content):lower()
if utils.file_exists(file) then
local got = hasher(file):lower()
hash = hash:lower()
DBG("Hashes: " .. got .. " " .. hash)
return hasher(content):lower() ~= hash:lower()
return hasher(file):lower() ~= hash:lower()
else
return nil
end
......
......@@ -41,6 +41,7 @@
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <stdio.h>
// The name used in lua registry to store stuff
#define REGISTRY_NAME "libupdater"
......@@ -687,6 +688,24 @@ static int lua_md5(lua_State *L) {
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) {
size_t len;
const char *buffer = luaL_checklstring(L, 1, &len);
......@@ -696,6 +715,25 @@ static int lua_sha256(lua_State *L) {
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) {
size_t args_c = lua_gettop(L);
const char *args[args_c];
......@@ -771,7 +809,9 @@ static const struct injected_func injected_funcs[] = {
{ lua_sync, "sync" },
{ lua_setenv, "setenv" },
{ lua_md5, "md5" },
{ lua_md5_file, "md5_file" },
{ lua_sha256, "sha256" },
{ lua_sha256_file, "sha256_file" },
{ lua_reexec, "reexec" },
{ lua_uri_internal_get, "uri_internal_get" },
{ lua_system_reboot, "system_reboot" },
......
......@@ -11,7 +11,7 @@ globals = {
-- lua51 doesn't contains?
"_M",
-- 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
"ERROR", "WARN", "INFO", "DBG", "TRACE", "DIE", "log_event", "c_pcall_error_handler",
-- 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