Advertisement
PaymentOption

CC Partitions

Sep 15th, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.48 KB | None | 0 0
  1. --[[
  2.     Disk Partitions     Trystan Cannon
  3.                         13 September 2013
  4.                        
  5.     This program allows users to access only specific
  6.     folders provided a certain password.
  7.     These folders act as the user's workspaces of sorts
  8.     where they have read/write privileges and nowhere else.
  9.    
  10.     Each user has a folder which contains their password file
  11.     and another folder which is their particular partition.
  12.    
  13.     Strategy:
  14.         Override the fs api so that the user must call
  15.         a function to validate their password. If it's
  16.         validated, then the user which is currently
  17.         logged in can use their workspace.
  18. ]]
  19.  
  20. -- Variables ---------------------------
  21. local userDirectory = "/partitionUserDirectory" -- Location where all user files are stored.
  22. local userTable     = {}  -- [username] = password
  23.  
  24. local loggedInUserWorkspace = nil -- The path to the workspace for the current user.
  25. local loggedInUsername      = nil -- The id of the user which is currently logged.
  26.  
  27. local nativeAPIs = {}
  28. -- Variables ---------------------------
  29.  
  30.  
  31.  
  32. --[[
  33.     Replaces the native fs functions with modified ones to suit
  34.     the needs of this program. All functions which are replaced are
  35.     stored in the nativeAPIs table so they can be restored later.
  36. ]]
  37. local function modifyAndBackupFsApi()
  38.     nativeAPIs.fs = {
  39.         exists  = _G.fs.exists,
  40.         open    = _G.fs.open,
  41.         list    = _G.fs.list,
  42.         delete  = _G.fs.delete,
  43.         makeDir = _G.fs.makeDir,
  44.         isDir   = _G.fs.isDir
  45.     }
  46.    
  47.     _G.fs.exists = function (path)
  48.         if fs.isReadOnly (path) then
  49.             return true
  50.         end
  51.        
  52.         path = path:gsub (shell.resolve (loggedInUserWorkspace), '')
  53.         return nativeAPIs.fs.exists (loggedInUserWorkspace .. '/' .. path)
  54.     end
  55.    
  56.     _G.fs.open = function (path, mode)
  57.         if fs.isReadOnly (path) then
  58.             return nativeAPIs.fs.open (path, mode)
  59.         end
  60.        
  61.         path = path:gsub (shell.resolve (loggedInUserWorkspace), '')
  62.         path = loggedInUserWorkspace .. '/' .. path
  63.        
  64.         return nativeAPIs.fs.open (path, mode)
  65.     end
  66.    
  67.     _G.fs.list = function (path)
  68.         path = loggedInUserWorkspace .. '/' .. path
  69.         return nativeAPIs.fs.list (path)
  70.     end
  71.    
  72.     _G.fs.delete = function (path)
  73.         path = loggedInUserWorkspace .. '/' .. path:gsub (shell.resolve (loggedInUserWorkspace), '')
  74.         return nativeAPIs.fs.delete (path)
  75.     end
  76.    
  77.     _G.fs.makeDir = function (path)
  78.         path = path:gsub (shell.resolve (loggedInUserWorkspace), '')
  79.         return nativeAPIs.fs.makeDir (loggedInUserWorkspace .. '/' .. path)
  80.     end
  81.    
  82.     _G.fs.isDir = function (path)
  83.         path = path:gsub (shell.resolve (loggedInUserWorkspace), '')
  84.         return nativeAPIs.fs.isDir (loggedInUserWorkspace .. '/' .. path)
  85.     end
  86.    
  87.     -- edit creates a directory in that is the user and their workspace inside the workspace
  88.     -- because of fs.combine or some shit. Maybe we should override fs.combine with some magic.
  89. end
  90.  
  91. --[[
  92.     Replaces the native shell functions with modified ones to suit
  93.     the needs of this program. All functions which are replaced are
  94.     stored in the nativeAPIs table so they can be restored later.
  95. ]]
  96. local function modifyAndBackupShellApi()
  97.     nativeAPIs.shell = {
  98.         setDir = shell.setDir,
  99.         dir    = shell.dir
  100.     }
  101.    
  102.     shell.setDir = function (path)
  103.         local parameters = { path = path }
  104.         path = rawget (parameters, "path"):gsub (shell.resolve (loggedInUserWorkspace), '')
  105.        
  106.         if nativeAPIs.fs.exists (loggedInUserWorkspace .. '/' .. path) then
  107.             nativeAPIs.shell.setDir (loggedInUserWorkspace .. '/' .. path)
  108.         end
  109.     end
  110.    
  111.     shell.dir = function()
  112.         local dir = nativeAPIs.shell.dir():gsub (loggedInUserWorkspace, "")
  113.         return dir:match ("%/+(.+)") or ""
  114.     end
  115. end
  116.  
  117. --[[
  118.     Restores all of the native APIs stored in the nativeAPIs table.
  119. ]]
  120. local function restoreNativeAPIs()
  121.     for apiName, api in pairs (nativeAPIs) do
  122.         for functionName, _function in pairs (api) do
  123.             (_G[apiName] or getfenv (1)[apiName])[functionName] = _function
  124.         end
  125.     end
  126. end
  127.  
  128. --[[
  129.     Registers a user under the given username and password.
  130. ]]
  131. function _G.registerUser (username, password)
  132.     if nativeAPIs.fs.exists (userDirectory .. '/' .. username) then
  133.         return
  134.     end
  135.    
  136.     nativeAPIs.fs.makeDir (userDirectory .. '/' .. username)
  137.     nativeAPIs.fs.makeDir (userDirectory .. '/' .. username .. "/workspace")
  138.    
  139.     local passwordFileHandle = nativeAPIs.fs.open (userDirectory .. '/' .. username .. "/password", 'w')
  140.     passwordFileHandle.writeLine (tostring (password))
  141.     passwordFileHandle.close()
  142.    
  143.     userTable[username] = tostring (password)
  144. end
  145.  
  146. --[[
  147.     Logs in the user with the given credentials and shifts the workspace
  148.     to that user's workspace.
  149.    
  150.     Returns true or false depending on the success of this operation.
  151. ]]
  152. function _G.loginUser (username, password)
  153.     local parameters = { password = password }
  154.    
  155.     if userTable[username] and userTable[username] == tostring (rawget (parameters, "password")) then
  156.         loggedInUsername      = username
  157.         loggedInUserWorkspace = userDirectory .. '/' .. username .. "/workspace"
  158.        
  159.         shell.setDir ("")
  160.         return true
  161.     end
  162.    
  163.     return false
  164. end
  165.  
  166. --[[
  167.     Initializes the user partition portion of the file system.
  168.     Creates necessary folders, etc.
  169.    
  170.     If the necessary folders exist, then the user table is loaded.
  171.    
  172.     NOTE: This should be called before the apis are modified.
  173. ]]
  174. local function initializePartitionFileSystem()
  175.     if not fs.exists (userDirectory) then
  176.         fs.makeDir (userDirectory)
  177.         return
  178.     end
  179.    
  180.     -- Load the user table since the user directory seems to exist.
  181.     for _, user in pairs (fs.list (userDirectory)) do
  182.         local passwordFileHandle = fs.open (userDirectory .. '/' .. user .. "/password", 'r')
  183.        
  184.         if passwordFileHandle then
  185.             userTable[user] = passwordFileHandle.readLine()
  186.             passwordFileHandle.close()
  187.         end
  188.     end
  189. end
  190.  
  191. initializePartitionFileSystem()
  192. modifyAndBackupFsApi()
  193. modifyAndBackupShellApi()
  194.  
  195. registerUser ("Default", "default")
  196. if not loginUser ("Default", "default") then
  197.     print ("Try starting the program again.")
  198. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement