Last active
November 8, 2019 14:38
-
-
Save gmarcos87/f7a491bf8b4236ceab1d77088ad54823 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env lua | |
--[[ | |
Copyright 2017 Marcos Gutierrez <gmarcos87@gmail.com> | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-3.0 | |
]]-- | |
local json = require 'luci.json' | |
local ft = require 'firstbootwizard.functools' | |
--[[ | |
UTILITIES SECTION | |
]] | |
local function shell(command) | |
local handle = io.popen(command) | |
local result = handle:read("*a") | |
handle:close() | |
return result | |
end | |
local function printJson (obj) | |
print(json.encode(obj)) | |
end | |
local function file_exists(file) | |
local f = io.open(file, "rb") | |
if f then f:close() end | |
return f ~= nil | |
end | |
local function get_text_file(file) | |
if not file_exists(file) then return nil end | |
local text_file = io.open(file,'rb') | |
local content = text_file:read "*a" | |
text_file:close() | |
return content | |
end | |
local function getSharedStateTable() | |
local content = get_text_file('./keys.json') | |
if content == nil then return {} end | |
return json.decode(get_text_file('./keys.json')) | |
end | |
local function checkSignature(pub, key, sig) | |
local isValid = shell("echo "..key.." > /tmp/test_valid_key.key; printf 'Signature\n"..sig.."' > /tmp/test_valid_key.key.sig; signify -V -p "..pub.." -m /tmp/test_valid_key.key 2>&1") | |
return isValid:gsub("\n","") == 'OK' | |
end | |
--[[ | |
KEY HANDLER CODE | |
]] | |
local sharedStateTable = getSharedStateTable() | |
print('\n\nsharedStateTable') | |
printJson(sharedStateTable) | |
local onlyKeys = ft.filter( | |
function(item) | |
return item.type == 'key' | |
end, | |
sharedStateTable) | |
local onlyKeysToInstall = ft.filter( | |
function(item) | |
local invalid = ft.filter(function(key) return key.data.key == item.data.key and key.data.valid == false end,onlyKeys) | |
return table.getn(invalid) == 0 | |
end, | |
onlyKeys | |
) | |
print('\n\nonlyKeysToInstall') | |
printJson(onlyKeysToInstall) | |
local onlyKeysToInstallSigned = ft.filter( | |
function(item) | |
return checkSignature('/root/soporteremoto.pub', item.data.key,item.data.sig) | |
end, | |
onlyKeysToInstall | |
) | |
print('\n\nonlyKeysToInstallSigned') | |
printJson(onlyKeysToInstallSigned) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment