added a proper user system, modified userspace utils to use it, etc etc.

This commit is contained in:
Izaya 2017-09-14 15:12:00 +10:00
parent 9cec183470
commit 6a822d6779
5 changed files with 88 additions and 11 deletions

View File

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

View File

@ -3,7 +3,7 @@ local si = tA[1]
spawn("lua shell",function()
_ENV = shutil.genenv()
coroutine.yield()
log(login())
log(pcall(login))
print("\f"..MOTD)
print(_VERSION)
while true do

View File

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

View File

@ -101,12 +101,91 @@ do -- so local works
return tT[pid].n,tT[pid].p,tT[pid].u
end
end
function os.setuser(user)
tT[cT].u = user
end
function os.genenv()
local et = {}
setmetatable(et,{__index=_G})
return et
end
-- user stuff from here
local ut = {}
local function flushut()
local f = fs.open("/boot/sys/users.dat","wb")
if f then
for k,v in pairs(ut) do
fs.write(f,k.."\t"..v[1].."\t"..v[2].."\n")
end
fs.close(f)
return true
end
return false
end
function os.readut()
local f=fs.open("/boot/sys/users.dat","rb")
if not f then return false end
local C=fs.readall(f)
fs.close(f)
log(C)
for line in C:gmatch("[^\n]+") do
local username,hpass,salt = line:match("(.+)\t(.+)\t(.+)")
if username and hpass and salt then
ut[username] = {hpass,salt}
log(username,hpass,salt)
end
end
end
function os.setuid(user)
if tT[cT].u == "superuser" and ut[user] then
tT[cT].u = user
return true
end
return false
end
function os.getuid(pid)
pid = pid or cT
return tT[pid].u
end
function os.users()
local t = {}
for k,v in pairs(ut) do
t[#t+1] = k
end
return t
end
function os.verifyuser(username,pass)
if sha then
if ut[username] then
if sha.sha256(pass..ut[username][2]) == ut[username][1] then
return true
end
end
return false
end
return true
end
function os.gensalt(len)
local S = ""
for i = 1, len do
S=S..string.char(math.random(32,126))
end
return S
end
function os.setuser(username,hpass,salt,...)
if tT[cT].u == "superuser" then
if hpass == nil then
ut[username] = nil
else
ut[username] = {hpass, salt,...}
flushut()
end
end
end
function os.su(user,pass)
if os.verifyuser(user,pass) then
log(tT[cT].u .. " su'd to "..user,6,1,true)
tT[cT].u = user
return true
end
return false
end
spawn("read users",os.readut)
end

View File

@ -14,10 +14,11 @@ function shutil.cat(p)
end
function shutil.ps(f)
local f=f or ""
print("PID\tName")
print("PID\tUser\tName")
for k,v in pairs(os.tasks()) do
local _,_,uid = os.taskinfo(k)
if v:find(f) then
print(tostring(k).."\t"..tostring(v))
print(tostring(k).."\t"..tostring(uid:sub(1,6)).."\t"..tostring(v))
end
end
end