Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Disk Partitions Trystan Cannon
- 13 September 2013
- This program allows users to access only specific
- folders provided a certain password.
- These folders act as the user's workspaces of sorts
- where they have read/write privileges and nowhere else.
- Each user has a folder which contains their password file
- and another folder which is their particular partition.
- Strategy:
- Override the fs api so that the user must call
- a function to validate their password. If it's
- validated, then the user which is currently
- logged in can use their workspace.
- ]]
- -- Variables ---------------------------
- local userDirectory = "/partitionUserDirectory" -- Location where all user files are stored.
- local userTable = {} -- [username] = password
- local loggedInUserWorkspace = nil -- The path to the workspace for the current user.
- local loggedInUsername = nil -- The id of the user which is currently logged.
- local nativeAPIs = {}
- -- Variables ---------------------------
- --[[
- Replaces the native fs functions with modified ones to suit
- the needs of this program. All functions which are replaced are
- stored in the nativeAPIs table so they can be restored later.
- ]]
- local function modifyAndBackupFsApi()
- nativeAPIs.fs = {
- exists = _G.fs.exists,
- open = _G.fs.open,
- list = _G.fs.list,
- delete = _G.fs.delete,
- makeDir = _G.fs.makeDir,
- isDir = _G.fs.isDir
- }
- _G.fs.exists = function (path)
- if fs.isReadOnly (path) then
- return true
- end
- path = path:gsub (shell.resolve (loggedInUserWorkspace), '')
- return nativeAPIs.fs.exists (loggedInUserWorkspace .. '/' .. path)
- end
- _G.fs.open = function (path, mode)
- if fs.isReadOnly (path) then
- return nativeAPIs.fs.open (path, mode)
- end
- path = path:gsub (shell.resolve (loggedInUserWorkspace), '')
- path = loggedInUserWorkspace .. '/' .. path
- return nativeAPIs.fs.open (path, mode)
- end
- _G.fs.list = function (path)
- path = loggedInUserWorkspace .. '/' .. path
- return nativeAPIs.fs.list (path)
- end
- _G.fs.delete = function (path)
- path = loggedInUserWorkspace .. '/' .. path:gsub (shell.resolve (loggedInUserWorkspace), '')
- return nativeAPIs.fs.delete (path)
- end
- _G.fs.makeDir = function (path)
- path = path:gsub (shell.resolve (loggedInUserWorkspace), '')
- return nativeAPIs.fs.makeDir (loggedInUserWorkspace .. '/' .. path)
- end
- _G.fs.isDir = function (path)
- path = path:gsub (shell.resolve (loggedInUserWorkspace), '')
- return nativeAPIs.fs.isDir (loggedInUserWorkspace .. '/' .. path)
- end
- -- edit creates a directory in that is the user and their workspace inside the workspace
- -- because of fs.combine or some shit. Maybe we should override fs.combine with some magic.
- end
- --[[
- Replaces the native shell functions with modified ones to suit
- the needs of this program. All functions which are replaced are
- stored in the nativeAPIs table so they can be restored later.
- ]]
- local function modifyAndBackupShellApi()
- nativeAPIs.shell = {
- setDir = shell.setDir,
- dir = shell.dir
- }
- shell.setDir = function (path)
- local parameters = { path = path }
- path = rawget (parameters, "path"):gsub (shell.resolve (loggedInUserWorkspace), '')
- if nativeAPIs.fs.exists (loggedInUserWorkspace .. '/' .. path) then
- nativeAPIs.shell.setDir (loggedInUserWorkspace .. '/' .. path)
- end
- end
- shell.dir = function()
- local dir = nativeAPIs.shell.dir():gsub (loggedInUserWorkspace, "")
- return dir:match ("%/+(.+)") or ""
- end
- end
- --[[
- Restores all of the native APIs stored in the nativeAPIs table.
- ]]
- local function restoreNativeAPIs()
- for apiName, api in pairs (nativeAPIs) do
- for functionName, _function in pairs (api) do
- (_G[apiName] or getfenv (1)[apiName])[functionName] = _function
- end
- end
- end
- --[[
- Registers a user under the given username and password.
- ]]
- function _G.registerUser (username, password)
- if nativeAPIs.fs.exists (userDirectory .. '/' .. username) then
- return
- end
- nativeAPIs.fs.makeDir (userDirectory .. '/' .. username)
- nativeAPIs.fs.makeDir (userDirectory .. '/' .. username .. "/workspace")
- local passwordFileHandle = nativeAPIs.fs.open (userDirectory .. '/' .. username .. "/password", 'w')
- passwordFileHandle.writeLine (tostring (password))
- passwordFileHandle.close()
- userTable[username] = tostring (password)
- end
- --[[
- Logs in the user with the given credentials and shifts the workspace
- to that user's workspace.
- Returns true or false depending on the success of this operation.
- ]]
- function _G.loginUser (username, password)
- local parameters = { password = password }
- if userTable[username] and userTable[username] == tostring (rawget (parameters, "password")) then
- loggedInUsername = username
- loggedInUserWorkspace = userDirectory .. '/' .. username .. "/workspace"
- shell.setDir ("")
- return true
- end
- return false
- end
- --[[
- Initializes the user partition portion of the file system.
- Creates necessary folders, etc.
- If the necessary folders exist, then the user table is loaded.
- NOTE: This should be called before the apis are modified.
- ]]
- local function initializePartitionFileSystem()
- if not fs.exists (userDirectory) then
- fs.makeDir (userDirectory)
- return
- end
- -- Load the user table since the user directory seems to exist.
- for _, user in pairs (fs.list (userDirectory)) do
- local passwordFileHandle = fs.open (userDirectory .. '/' .. user .. "/password", 'r')
- if passwordFileHandle then
- userTable[user] = passwordFileHandle.readLine()
- passwordFileHandle.close()
- end
- end
- end
- initializePartitionFileSystem()
- modifyAndBackupFsApi()
- modifyAndBackupShellApi()
- registerUser ("Default", "default")
- if not loginUser ("Default", "default") then
- print ("Try starting the program again.")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement