From 6a822d6779c7cdc09fdbeb801f08e30f3d0d0941 Mon Sep 17 00:00:00 2001 From: XeonSquared Date: Thu, 14 Sep 2017 15:12:00 +1000 Subject: [PATCH] added a proper user system, modified userspace utils to use it, etc etc. --- configs/everything.cfg | 1 - exec/luash.lua | 2 +- modules/applications/login.lua | 6 +-- modules/base/header.lua | 85 ++++++++++++++++++++++++++++++++-- modules/lib/shutil.lua | 5 +- 5 files changed, 88 insertions(+), 11 deletions(-) diff --git a/configs/everything.cfg b/configs/everything.cfg index 1eba678..0ca7298 100755 --- a/configs/everything.cfg +++ b/configs/everything.cfg @@ -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 diff --git a/exec/luash.lua b/exec/luash.lua index d8330c3..731a904 100644 --- a/exec/luash.lua +++ b/exec/luash.lua @@ -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 diff --git a/modules/applications/login.lua b/modules/applications/login.lua index 3915c3e..49d7580 100644 --- a/modules/applications/login.lua +++ b/modules/applications/login.lua @@ -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 diff --git a/modules/base/header.lua b/modules/base/header.lua index 0df6dbc..364e41f 100644 --- a/modules/base/header.lua +++ b/modules/base/header.lua @@ -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 diff --git a/modules/lib/shutil.lua b/modules/lib/shutil.lua index bdb4870..78bdc2b 100644 --- a/modules/lib/shutil.lua +++ b/modules/lib/shutil.lua @@ -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