MudkipTheEpic

Sandbox Program

Mar 8th, 2013
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.92 KB | None | 0 0
  1. local args={...}
  2. local sandboxed = args[1]
  3. local privTable={}
  4.  
  5. --Debug:
  6. --print(sandboxed)
  7.  
  8. --Check if loaded...
  9. if fs.setReadOnly then error("Already loaded!!!",2) end
  10.  
  11. privTable["aliases"]=shell.aliases()
  12.  
  13. --Backup FS declaration...
  14. local oFs={}
  15. for k, v in pairs(fs) do
  16.     oFs[k]=v
  17. end
  18.  
  19. --Backup others declaration...
  20. local oldDfile=dofile
  21. local oldLfile=loadfile
  22. local err = error
  23.  
  24.  
  25.  
  26. --Security declarations...
  27. local oldsenv=setfenv
  28.  
  29. local rT={[1] = sandboxed}
  30.  
  31. local function check(filename)
  32.     for k,v in pairs(rT) do
  33.         if v==filename then return true end
  34.     end
  35.     return false
  36. end
  37.  
  38. local function isAllowed()
  39.     --stub function, change it to fit your OS'es needs.
  40.     return false
  41. end
  42.  
  43. local function errorout()
  44.     err("Insufficient permissions.",3)
  45. end
  46.  
  47. --Start with the declaring...
  48.  
  49. function fs.open(f,v)
  50.     f=sandboxed..f or sandboxed
  51.     if isAllowed() then return oFs.open(f,v) end
  52.     if v ~= ("r" or "rb" or nil) then
  53.         if check(f) then
  54.             errorout()
  55.         end
  56.     end
  57.     return oFs.open(f,v)
  58. end
  59.  
  60. function fs.exists(f)
  61.     f=sandboxed..f or sandboxed
  62.     return oFs.exists(f)
  63. end
  64.  
  65. function fs.isDir(f)
  66.     f=sandboxed..f or sandboxed
  67.     return oFs.isDir(f)
  68. end
  69.  
  70. function fs.delete(f)
  71.     f=sandboxed..f or sandboxed
  72.     if isAllowed() then return oFs.delete(f) end
  73.     if check(f) then
  74.         errorout()
  75.     end
  76.     return oFs.delete(f)
  77. end
  78.  
  79. function fs.copy(f,v)
  80.     f=sandboxed..f or sandboxed
  81.     v=sandboxed..v or sandboxed
  82.     if isAllowed() then return oFs.copy(f,v) end
  83.     if check(f) then
  84.         errorout()
  85.     end
  86.     return oFs.copy(f,v)
  87. end
  88.  
  89. function fs.move(f,v)
  90.     f=sandboxed..f or sandboxed
  91.     v=sandboxed..v or sandboxed
  92.     if isAllowed() then return oFs.move(f,v) end
  93.     if check(f) then
  94.         errorout()
  95.     end
  96.     return oFs.move(f,v)
  97. end
  98.  
  99. function fs.isReadOnly(f)
  100.     f=sandboxed..f or sandboxed
  101.     if check(f) then
  102.         return true
  103.     end
  104.     return oFs.isReadOnly(f)
  105. end
  106.  
  107. function fs.setReadOnly(file, bool)
  108.     file=sandboxed..file or sandboxed
  109.     if not isAllowed() then errorout() return nil end
  110.     if not type(bool) == "boolean" then error("Boolean expected, got "..type(bool)..".") end
  111.     if bool then
  112.         if not check(file) then
  113.             table.insert(rT, file)
  114.         end
  115.         return true
  116.     else
  117.         for k,v in pairs(rT) do
  118.             if v == file then
  119.                 table.remove(rT, k)
  120.                 local derp = true
  121.             end
  122.         end
  123.         if derp then return true end
  124.         return false
  125.     end
  126. end
  127.  
  128. function fs.list(dir)
  129.     if type(dir)=="string" then
  130.         if string.sub(dir,#dir,#dir) == "/" then dir = string.sub(dir,1,#dir-1) end
  131.     end
  132.     dir=dir..sandboxed or sandboxed
  133.     return oFs.list(dir)
  134. end
  135.  
  136. function setfenv(...)
  137.     return oldsenv(...)
  138. end
  139.  
  140.  
  141. function restore()
  142.     if not isAllowed() then errorout() end
  143.     fs={}
  144.     for k,v in pairs(oFs) do
  145.         fs[k]=v
  146.     end
  147.     dofile=oldDfile
  148.     loadfile=oldLfile
  149.     setfenv=oldsenv
  150.     restore=nil
  151.     for k,v in pairs(privTable.aliases) do
  152.         shell.setAlias(k,v)
  153.     end
  154. end
  155.  
  156. --Clear aliases...
  157. for k,v in pairs(privTable.aliases) do
  158.     shell.clearAlias(k,v)
  159. end
Advertisement
Add Comment
Please, Sign In to add comment