diff --git a/halyde/kernel/modules/tsched.lua b/halyde/kernel/modules/tsched.lua index 6c7207c..c152d7b 100644 --- a/halyde/kernel/modules/tsched.lua +++ b/halyde/kernel/modules/tsched.lua @@ -68,23 +68,20 @@ function module.init() checkArg(1, func, "function") checkArg(2, name, "string") local task = coroutine.create(func) - local taskInfo = { ["task"] = task, ["name"] = name, ["id"] = idCounter } + local taskInfo = { ["task"] = task, ["name"] = name, ["id"] = idCounter} if type(tsched.currentTask) == "table" and type(tsched.currentTask.id) == "number" then taskInfo.parent = tsched.currentTask.id + taskInfo.user = tsched.currentTask.user end table.insert(tsched.tasks, taskInfo) idCounter = idCounter + 1 if taskInfo.parent then log.kernel.info( - string.format( - "[tsched] Created task %s with PID %d by parent with PID %d", - name, - idCounter - 1, - taskInfo.parent - ) + ("[tsched] Created task %s (PID %d) by parent PID %d as UID %d"):format(name, idCounter - 1, taskInfo.parent, taskInfo.user) ) else - log.kernel.info(string.format("[tsched] Created task %s with PID %d (no parent found)", name, idCounter - 1)) + taskInfo.user = 1 -- It's probably being run from kernel level + log.kernel.info(string.format("[tsched] Created task %s (PID %d) as UID 1 (no parent found)", name, idCounter - 1)) end return task, taskInfo end diff --git a/halyde/kernel/modules/user.lua b/halyde/kernel/modules/user.lua new file mode 100644 index 0000000..e7564a6 --- /dev/null +++ b/halyde/kernel/modules/user.lua @@ -0,0 +1,60 @@ +local module = {} + +function module.check() + return true -- The user system is kind of essential all the time... +end + +function module.init() + local fs = require("filesystem") + local md5 = require("md5") + local json = require("json") + local log = require("log") + + _PUBLIC.user = {} + + function _PUBLIC.user.addTask(func, name, userId, userPassword) + checkArg(1, func, "function") + checkArg(2, name, "string") + checkArg(3, userId, "number") + checkArg(4, userPassword, "string") + + local handle, data, tmpdata = fs.open("/halyde/kernel/userreg.json"), "", nil + repeat + tmpdata = handle:read(math.huge) + data = data .. (tmpdata or "") + until not tmpdata + + local userRegistry = json.decode(data) + + if not userRegistry[userId] then + return false, "No such UID" + end + + local salt = md5.sumhexa(userRegistry[userId].name) -- A little bit of salt and pepper + local passwordHash = md5.sumhexa(userPassword .. salt) + if passwordHash ~= userRegistry[userId].hash then + wait(3) -- Something to hopefully shove away brute forcers + return false, "Password incorrect" + end + + local task = coroutine.create(func) + local taskInfo = { ["task"] = task, ["name"] = name, ["id"] = #_PUBLIC.tsched.getTasks() + 1, ["user"] = userId} + if type(tsched.currentTask) == "table" and type(tsched.currentTask.id) == "number" then + taskInfo.parent = tsched.currentTask.id + end + table.insert(tsched.tasks, taskInfo) + if taskInfo.parent then + log.kernel.info( + ("[tsched (user)] Created task %s (PID %d) by parent PID %d as UID %d"):format(name, #_PUBLIC.tsched.getTasks(), taskInfo.parent, taskInfo.user) + ) + log.kernel.info(string.format("[tsched (user)] Created task %s (PID %d) as UID %d (no parent found)", name, #_PUBLIC.tsched.getTasks(), taskInfo.user)) + end + return task, taskInfo + end +end + +function module.exit() -- Ok bro + _PUBLIC.user = nil +end + +return module diff --git a/halyde/kernel/userreg.json b/halyde/kernel/userreg.json new file mode 100644 index 0000000..1663859 --- /dev/null +++ b/halyde/kernel/userreg.json @@ -0,0 +1 @@ +[{"name":"admin","hash":"c0e024d9200b5705bc4804722636378a"},{"name":"user","hash":"c040e68c4d44c8f8c48746bca89c1b21"}] diff --git a/home/test.lua b/home/test.lua index 0e36f56..4473c0b 100644 --- a/home/test.lua +++ b/home/test.lua @@ -1,6 +1,6 @@ -local pid = tsched.getCurrentTask().id -local shareTable = ipc.shareWith(pid) -shareTable.gabbagool = "Pigeon Pizza! Wow!" -print(shareTable.gabbagool) -print(pid) -print(ipc.shared[pid].gabbagool) +local args = {...} + +print(user.addTask(function() + print("I eat rocks") + print(tsched.getCurrentTask()) +end, "testerpester", tonumber(args[1]), args[2]))