made login() use userlib and automatically load users and stuff. shiny.

This commit is contained in:
Izaya 2017-09-09 21:26:29 +10:00
parent 48f3576581
commit 1406044789
4 changed files with 73 additions and 15 deletions

View File

@ -16,6 +16,7 @@ modules/util/motd.lua
modules/lib/readline.lua modules/lib/readline.lua
modules/lib/shutil.lua modules/lib/shutil.lua
modules/lib/sha256.lua modules/lib/sha256.lua
modules/lib/userlib.lua
modules/net/net-ext.lua modules/net/net-ext.lua
modules/applications/login.lua modules/applications/login.lua
modules/applications/luash.lua modules/applications/luash.lua

View File

@ -1,14 +1,15 @@
function login() function login()
if _G.PASS_HASH and _G.PASS_SALT then if not userlib then return true end
io.write("\f") local un,pw,cc = "","",false
io.write(_G.MOTD.."\nPassword: ") if #userlib.users() > 0 then
local pt = io.read() repeat
if pt then io.write("\f"..MOTD.."\n"..net.id.." login: ")
if sha.sha256(pt.._G.PASS_SALT) == _G.PASS_HASH then un = io.read()
io.write("Password: ")
pw = io.read("*")
cc = userlib.verify(un,pw)
until cc
os.setuser(un)
return true return true
end end
end
return false
end
return true
end end

View File

@ -1,9 +1,7 @@
function luash(si) function luash(si)
spawn("lua shell",function() spawn("lua shell",function()
if login then coroutine.yield()
repeat log(login())
until login()
end
print("\f"..MOTD) print("\f"..MOTD)
print(_VERSION) print(_VERSION)
while true do while true do

58
modules/lib/userlib.lua Normal file
View File

@ -0,0 +1,58 @@
do
_G.userlib = {}
local ut = {}
function userlib.users()
local t = {}
for k,v in pairs(ut) do
t[#t+1] = k
end
return t
end
function userlib.verify(username,pass)
if ut[username] then
if sha.sha256(pass..ut[username][2]) == ut[username][1] then
return true
end
end
return false
end
function userlib.gensalt(len)
local S = ""
for i = 1, len do
S=S..string.char(math.random(32,126))
end
return S
end
function userlib.setuser(username,hpass,salt,...)
ut[username] = {hpass, salt,...}
end
function userlib.deluser(username)
ut[username] = nil
end
function userlib.readpwd(path)
local f=io.open(path,"rb")
if not f then return false end
local C=f:read("*a")
for line in C:gmatch("[^\n]+") do
local username,hpass,salt = line:match("(.+)\t(.+)\t(.+)")
log(username,hpass,salt)
if username and hpass and salt then
ut[username] = {hpass,salt}
end
end
end
function userlib.writepwd()
local S=""
for k,v in pairs(ut) do
S=S..k.."\t"..v[1].."\t"..v[2].."\n"
end
return S
end
function userlib.write(path)
local f=io.open(path,"wb")
if not f then return false end
f:write(userlib.writepwd())
f:close()
end
spawn("load users",function() userlib.readpwd("/boot/sys/users.dat") end)
end