Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Vault Shell v1.0
- -- by Majd123mc
- -- Licensed under CCPL which may be found here: https://github.com/dan200/ComputerCraft/blob/master/LICENSE
- os.loadAPI("sha256.lua")
- local _os_pullEvent = os.pullEvent
- os.pullEvent = os.pullEventRaw
- function printColor(text,color)
- local _color = term.getTextColor()
- term.setTextColor(color)
- print(text)
- term.setTextColor(_color)
- end
- function flushSettings()
- settings.save("/.settings")
- end
- function clearScreen()
- term.setCursorPos(1,1)
- term.clear()
- end
- -- Taken from shell.lua
- local function createShellEnv( sDir )
- local tEnv = {}
- tEnv[ "shell" ] = shell
- tEnv[ "multishell" ] = multishell
- local package = {}
- package.loaded = {
- _G = _G,
- bit32 = bit32,
- coroutine = coroutine,
- math = math,
- package = package,
- string = string,
- table = table,
- }
- package.path = "?;?.lua;?/init.lua;/rom/modules/main/?;/rom/modules/main/?.lua;/rom/modules/main/?/init.lua"
- if turtle then
- package.path = package.path..";/rom/modules/turtle/?;/rom/modules/turtle/?.lua;/rom/modules/turtle/?/init.lua"
- elseif command then
- package.path = package.path..";/rom/modules/command/?;/rom/modules/command/?.lua;/rom/modules/command/?/init.lua"
- end
- package.config = "/\n;\n?\n!\n-"
- package.preload = {}
- package.loaders = {
- function( name )
- if package.preload[name] then
- return package.preload[name]
- else
- return nil, "no field package.preload['" .. name .. "']"
- end
- end,
- function( name )
- local fname = string.gsub(name, "%.", "/")
- local sError = ""
- for pattern in string.gmatch(package.path, "[^;]+") do
- local sPath = string.gsub(pattern, "%?", fname)
- if sPath:sub(1,1) ~= "/" then
- sPath = fs.combine(sDir, sPath)
- end
- if fs.exists(sPath) and not fs.isDir(sPath) then
- local fnFile, sError = loadfile( sPath, tEnv )
- if fnFile then
- return fnFile, sPath
- else
- return nil, sError
- end
- else
- if #sError > 0 then
- sError = sError .. "\n"
- end
- sError = sError .. "no file '" .. sPath .. "'"
- end
- end
- return nil, sError
- end
- }
- local sentinel = {}
- local function require( name )
- if type( name ) ~= "string" then
- error( "bad argument #1 (expected string, got " .. type( name ) .. ")", 2 )
- end
- if package.loaded[name] == sentinel then
- error("Loop detected requiring '" .. name .. "'", 0)
- end
- if package.loaded[name] then
- return package.loaded[name]
- end
- local sError = "Error loading module '" .. name .. "':"
- for n,searcher in ipairs(package.loaders) do
- local loader, err = searcher(name)
- if loader then
- package.loaded[name] = sentinel
- local result = loader( err )
- if result ~= nil then
- package.loaded[name] = result
- return result
- else
- package.loaded[name] = true
- return true
- end
- else
- sError = sError .. "\n" .. err
- end
- end
- error(sError, 2)
- end
- tEnv["package"] = package
- tEnv["require"] = require
- return tEnv
- end
- -- Prompting
- function promptUsername()
- term.write("Username> ")
- local usr = read()
- if not usr then
- return nil
- end
- if not usr:match("[a-zA-Z0-9_]+") then
- print("Invalid username. Usernames may only contain letters, numbers, and underscores")
- return promptUsername()
- end
- return usr
- end
- function promptPassword()
- term.write("Password> ")
- return read("*")
- end
- function promptLogin()
- print("Please enter your credentials:")
- local usr = promptUsername()
- if not usr then return false end
- local pwd = promptPassword()
- return true,usr,pwd
- end
- function promptConfirmPwd(new)
- term.write((new and "New " or "") .. "Password> ")
- local pwd = read("*")
- term.write("Confirm> ")
- local con = read("*")
- if pwd == con then
- return pwd
- else
- print("Passowrds do not match. Please try again")
- return promptConfirmPwd()
- end
- end
- function promptChangePassword()
- print("Please enter username and new credentials")
- local usr = promptUsername()
- if not usr then
- return false
- elseif not userExists(usr) then
- print("No such user exists")
- return false
- end
- local pwd = promptConfirmPwd(true)
- return true,usr,pwd
- end
- function promptDeleteUser()
- print("Enter the username for the account to be deleted")
- local usr = promptUsername()
- if not usr then
- return false
- elseif not userExists(usr) then
- print("No such user exists")
- return false
- end
- return true, usr
- end
- function promptCreateUser()
- --print("It looks like this is the first time you have run Vault Shell")
- print("Please enter credentials for new account")
- local usr = promptUsername()
- if not usr then
- return false
- elseif userExists(usr) then
- print("A user with that name already exists. Please try again")
- return promptCreateUser()
- end
- local pwd = promptConfirmPwd()
- return true,usr,pwd
- end
- function promptSetup()
- print("It looks like this is the first time you have run Vault Shell")
- return promptCreateUser()
- end
- -- User accounts
- function getUserHash(usr)
- return settings.get("vsh.login." .. usr)
- end
- function userExists(usr)
- return getUserHash(usr) ~= nil
- end
- function verifyLogin(usr,pwd)
- local test_hash = getUserHash(usr)
- local input_hash = sha256.digestStr(pwd)
- return test_hash == input_hash
- end
- function createUser(usr,pwd)
- local hash = sha256.digestStr(pwd)
- settings.set("vsh.login." .. usr,hash)
- local users = settings.get("vsh.users") or {}
- table.insert(users, usr)
- settings.set("vsh.users", users)
- flushSettings()
- return true
- end
- function setPassword(usr, pwd)
- local hash = sha256.digestStr(pwd)
- settings.set("vsh.login." .. usr,hash)
- flushSettings()
- return true
- end
- function deleteUser(usr)
- settings.unset("vsh.login." .. usr)
- local users = settings.get("vsh.users") or {}
- for i, usr2 in ipairs(users) do
- if usr2 == usr then
- table.remove(users, i)
- end
- end
- settings.set("vsh.users", users)
- flushSettings()
- return true
- end
- -- Main
- function loginShell(usr)
- os.pullEvent = _os_pullEvent
- shell.run("rom/programs/shell.lua")
- os.pullEvent = os.pullEventRaw
- end
- function mainLogin()
- local success,usr,pwd = promptLogin()
- if not success then
- print("Please enter a username to log in")
- mainLogin()
- end
- local validLogin = verifyLogin(usr,pwd)
- if validLogin then
- print("Login successful!\n")
- _G.vsh.usr = usr
- loginShell(usr,pwd)
- _G.vsh.usr = nil
- clearScreen()
- mainLogin()
- else
- print("Invalid credentials. Please try again")
- mainLogin()
- end
- end
- function init()
- clearScreen()
- printColor("Vault Shell v1.0 - Majd123mc 2022\n",colors.yellow)
- multishell.setTitle(multishell.getCurrent(), "vsh")
- local setupComplete = settings.get("vsh.setup")
- if not setupComplete then
- local success,usr,pwd = promptSetup()
- if not success then
- print("You must create a user to use Vault Shell. Exiting to CraftOS")
- shell.run("sh")
- return
- end
- createUser(usr,pwd)
- settings.set("vsh.setup",true)
- flushSettings()
- print("Account created successfully. You must reenter your credentials to log in.\n")
- end
- mainLogin()
- end
- _G.vsh = _G.vsh or {}
- local arg = ...
- if not arg then
- if not _G.vsh.usr then
- init()
- else
- print("Already logged in. Type exit to log out")
- end
- elseif arg == "help" then
- print("vsh help - View help")
- print("vsh chpwd - Change password")
- print("vsh add - Add user")
- print("vsh del - Remove user")
- print("vsh users - List users")
- elseif arg == "chpwd" then
- local success, usr, pwd = promptChangePassword()
- if not success then
- return
- end
- createUser(usr, pwd)
- elseif arg == "add" then
- local success, usr, pwd = promptCreateUser()
- if not success then
- print("Error")
- return
- end
- createUser(usr, pwd)
- print("Account created successfully.")
- elseif arg == "del" then
- if #settings.get("vsh.users") <= 1 then
- print("At least one user must exist on the system")
- return
- end
- local success, usr = promptDeleteUser()
- if not success then
- return
- end
- deleteUser(usr)
- elseif arg == "users" then
- for _, user in ipairs(settings.get("vsh.users")) do
- print(user)
- end
- else
- print("Unknown argument " .. arg .. ". Type vsh help for more info")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement