ZNZNCOOP

protect

Nov 30th, 2015
109
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. computer=require("computer")
  2. getPath=require("filesystem").path
  3. fs=require("component").proxy(computer.getBootAddress())
  4.  
  5. function computer.addUser()
  6.   return nil, "Function 'addUser' is disabled"
  7. end
  8.  
  9. local protectList
  10. local function checkPath(path)
  11.   return true
  12. end
  13.  
  14. local function whiteList(path)
  15.   if fs.isDirectory(path) then path=path.."/" end
  16.   if protectList["/"] then return true end
  17.   repeat
  18.     if protectList[path] then return true end
  19.     path=getPath(path)
  20.   until path=="/"
  21.   return nil, "no such file, or permission denied"
  22. end
  23.  
  24. local function blackList(path)
  25.   if fs.isDirectory(path) then path=path.."/" end
  26.   if protectList["/"] then return nil, "no such file, or permission denied" end
  27.   repeat
  28.     if protectList[path] then return nil, "no such file, or permission denied" end
  29.     path=getPath(path)
  30.   until path=="/"
  31.   return true
  32. end
  33.  
  34. do
  35.   local env = {}
  36.   local config = loadfile("/etc/protect.cfg", nil, env)
  37.   if config then
  38.     pcall(config)
  39.     if env.writeEnabled then
  40.       protectList={}
  41.       for i=1,#env.writeEnabled do
  42.         protectList[env.writeEnabled[i]]=true
  43.       end
  44.       checkPath=whiteList
  45.     end
  46.     if env.writeDisabled then
  47.       protectList={}
  48.       for i=1,#env.writeDisabled do
  49.         protectList[env.writeDisabled[i]]=true
  50.       end
  51.       checkPath=blackList
  52.     end
  53.   end
  54. end
  55.  
  56. local nativeFS={}
  57.  
  58. nativeFS.makeDirectory=fs.makeDirectory
  59. function fs.makeDirectory(path)
  60.   local ok,err=checkPath(path)
  61.   if not ok then return nil, err end
  62.   return nativeFS.makeDirectory(path)
  63. end
  64.  
  65. nativeFS.remove=fs.remove
  66. function fs.remove(path)
  67.   local ok,err=checkPath(path)
  68.   if not ok then return nil, err end
  69.   return nativeFS.remove(path)
  70. end
  71.  
  72. nativeFS.rename=fs.rename
  73. function fs.rename(from,to)
  74.   local ok,err=checkPath(from)
  75.   if not ok then return nil, err end
  76.   ok,err=checkPath(to)
  77.   if not ok then return nil, err end
  78.   return nativeFS.rename(from,to)
  79. end
  80.  
  81. nativeFS.open=fs.open
  82. function fs.open(path, mode)
  83.   if mode=="w" or mode=="wb" or mode=="a" or mode=="ab" then
  84.     local ok,err=checkPath(path)
  85.     if not ok then return nil, err end
  86.   end
  87.   return nativeFS.open(path,mode)
  88. end
RAW Paste Data