MudkipTheEpic

Sandbox Tests

Mar 9th, 2013
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.31 KB | None | 0 0
  1. sandbox={}
  2. function sandbox.load(...)
  3. --[[
  4. OsRunner: by MudkipTheEpic
  5. Feel free to redistribute and modify this, but keep these lines in please.
  6. Thanks,
  7.   Mudkip
  8. ]]--
  9.  
  10. local firstTime=false
  11.  
  12. local oResolve=shell.resolve
  13. local function queue(word)
  14.     for p=1,#word do
  15.         os.queueEvent("char",word:sub(p,p))
  16.     end
  17.     os.queueEvent("key",keys.enter)
  18. end
  19.  
  20. local function activateInstaller(filepath)
  21.     if filepath then
  22.         shell.run(filepath)
  23.     end
  24. end
  25.  
  26.  
  27. sandbox=nil
  28. printStuff=false --Change to see printed stuff or not.
  29.  
  30. --Backup FS declaration...
  31. local oFs={}
  32. for k, v in pairs(fs) do
  33.     oFs[k]=v
  34. end
  35.  
  36. --Backup others declaration...
  37. local oldDfile=dofile
  38. local oldLfile=loadfile
  39. local err = error
  40. local oldReboot=os.reboot
  41. local oldShutdown=os.shutdown
  42.  
  43.  
  44.  
  45. local oldsenv=setfenv
  46. local oldmeta=setmetatable
  47. local orset=rawset
  48. local bPair=pairs
  49.  
  50. local function centerPrint(text, ny)
  51.     if type(text) == "table" then for _, v in pairs(text) do centerPrint(v) end
  52. else
  53.     local x, y = term.getCursorPos()
  54.     local w, h = term.getSize()
  55.     term.setCursorPos(w/2 - text:len()/2, ny or y)
  56.     print(text)
  57. end
  58. end
  59.  
  60. local function centerWrite(text, ny)
  61.     if type(text) == "table" then for _, v in pairs(text) do centerWrite(v) end
  62. else
  63.     local x, y = term.getCursorPos()
  64.     local w, h = term.getSize()
  65.     term.setCursorPos(w/2 - text:len()/2, ny or y)
  66.     write(text)
  67. end
  68. end
  69.  
  70. local function md(d)
  71.     os.queueEvent(" ")
  72.     os.pullEventRaw()
  73.     return fs.makeDir(dir.."/"..d)
  74. end
  75.  
  76. local function cp(f)
  77.     os.queueEvent(" ")
  78.     os.pullEventRaw()
  79.     return pcall(fs.copy,f,dir.."/"..f)
  80. end
  81.  
  82. local function clear()
  83.     term.clear()
  84.     term.setCursorPos(1,1)
  85. end
  86.  
  87. local function sandbox(directory)
  88.     local sandboxed = directory
  89.  
  90.     local rT={[1] = sandboxed, [2] = sandboxed.."rom"}
  91.  
  92.     local function check(filename)
  93.         if filename:sub(1,sandboxed:len()+4) == (sandboxed.."rom/" or sandboxed.."rom\\") then return true end
  94.         for k,v in bPair(rT) do
  95.             if v==filename then return true end
  96.         end
  97.         return false
  98.     end
  99.  
  100.     local function isAllowed()
  101.         return false
  102.     end
  103.  
  104.     local function errorout()
  105.         err("Access denied",3)
  106.     end
  107.  
  108.     local function returnResolved(path)
  109.         if path==nil then return sandboxed end
  110.         local nPath=oResolve(path)
  111.         if (nPath:sub(1,2)==".." and #nPath==2) or (nPath:sub(1,3)=="../") then return nil end
  112.         return sandboxed..path
  113.     end
  114.  
  115.  
  116.     function fs.open(f,v)
  117.         f=returnResolved(f)
  118.         if f==nil then return nil end
  119.         if isAllowed() then return oFs.open(f,v) end
  120.         if v ~= ("r" or "rb" or nil) then
  121.             if check(f) then
  122.                 errorout()
  123.             end
  124.         end
  125.         return oFs.open(f,v)
  126.     end
  127.  
  128.     function fs.exists(f)
  129.         f=returnResolved(f)
  130.         if f==nil then return false end
  131.         return oFs.exists(f)
  132.     end
  133.  
  134.     function fs.isDir(f)
  135.         f=returnResolved(f)
  136.         if f==nil then return false end
  137.         return oFs.isDir(f)
  138.     end
  139.  
  140.     function fs.delete(f)
  141.         f=returnResolved(f)
  142.         if f==nil then err("Invalid Path",2) return nil end
  143.         if isAllowed() then return oFs.delete(f) end
  144.         if check(f) then
  145.             errorout()
  146.         end
  147.         return oFs.delete(f)
  148.     end
  149.  
  150.     function fs.copy(f,v)
  151.         f=returnResolved(f)
  152.         v=returnResolved(v)
  153.         if f==nil or v==nil then err("Invalid Path",2) end
  154.         if isAllowed() then return oFs.copy(f,v) end
  155.         if check(f) then
  156.             errorout()
  157.         end
  158.         return oFs.copy(f,v)
  159.     end
  160.  
  161.     function fs.move(f,v)
  162.         f=returnResolved(f)
  163.         v=returnResolved(v)
  164.         if f==nil or v==nil then err("Invalid Path",2) end
  165.         if isAllowed() then return oFs.move(f,v) end
  166.         if check(f) then
  167.             errorout()
  168.         end
  169.         return oFs.move(f,v)
  170.     end
  171.  
  172.     function fs.isReadOnly(f)
  173.         f=returnResolved(f)
  174.         if f==nil then return false end
  175.         if check(f) then
  176.             return true
  177.         end
  178.         return oFs.isReadOnly(f)
  179.     end
  180.  
  181.  
  182.     function fs.list(dir)
  183.         dir=returnResolved(dir)
  184.         if dir==nil then err("Invalid Path",2) end
  185.         return oFs.list(dir)
  186.     end
  187.  
  188.     function fs.getName(file)
  189.         file=sandboxed..file or sandboxed
  190.         if file==sandboxed then return "root" end
  191.         return oFs.getName(file)
  192.     end
  193.  
  194.     function fs.makeDir(dir)
  195.         dir=returnResolved(dir)
  196.         if dir==nil then err("Invalid Path",2) end
  197.         return oFs.makeDir(dir)
  198.     end
  199.    
  200.     function fs.getSize(filepath)
  201.         filepath=returnResolved(filepath)
  202.         if filepath==nil then err("Invalid Path",2) end
  203.         return oFs.getSize(filepath)
  204.     end
  205.  
  206.     function fs.getDrive(filepath)
  207.         filepath=returnResolved(filepath)
  208.         if filepath==nil then err("Invalid Path",2) end
  209.         return oFs.getDrive(filepath)
  210.     end
  211.  
  212.     --[[
  213.     function setfenv(func,...)
  214.         for k,v in bPair(protectedFuncs) do
  215.             if v==func then errorout() end
  216.         end
  217.         return oldsenv(func,...)
  218.     end
  219.  
  220.     function setmetatable(table,...)
  221.         if table==fs or table==_G then errorout() end
  222.         return oldmeta(table,...)
  223.     end
  224.  
  225.     function rawset(table,...)
  226.         if table==fs then errorout() end
  227.         return orset(table,...)
  228.     end
  229.     ]]
  230.     function os.reboot()
  231.         local writeReboot=oFs.open(".OSRunner/rebootPath","w")
  232.         writeReboot.write(sandboxed:sub(1,#sandboxed-1))
  233.         writeReboot.close()
  234.         oldReboot()
  235.     end
  236. --[[
  237.     local protectedFuncs={setfenv,setmetatable,rawset,bPair}
  238.     for k,v in bPair(fs) do
  239.         table.insert(protectedFuncs,v)
  240.     end
  241.     ]]
  242. end
  243.  
  244. local function restore()
  245.     _G.fs={}
  246.     for k,v in bPair(oFs) do
  247.         _G.fs[k]=v
  248.     end
  249.     _G.setfenv=oldsenv
  250.     _G.os.reboot=oldReboot
  251.     _G.os.shutdown=oldShutdown
  252.     _G.setmetatable=oldmeta
  253.     _G.rawset=orset
  254. end
  255.  
  256. --Main program begins here...
  257.  
  258. local args={...}
  259. dir=args[1].."/"
  260. OS=args[2] or os.version()
  261. filepath=args[3]
  262. urlinstaller=args[4]
  263.  
  264.  
  265.  
  266. if not fs.isDir(dir) then local firstTime=true
  267.     fs.delete(dir) fs.makeDir(dir)
  268.     term.setBackgroundColor(colors.black)
  269.     term.setTextColor(colors.white)
  270.     clear()
  271.     local mx,my=term.getSize()
  272.     term.setCursorPos(1, math.floor(my/2))
  273.     centerPrint("Creating OS Virtual Environment")
  274.     centerPrint("Please wait...")
  275.     if printStuff then centerPrint("Creating OS Environment for directory "..dir..".") end
  276.  
  277.     md("rom")
  278.     md("rom/programs")
  279.     md("rom/programs/secret")
  280.     md("rom/apis")
  281.     md("rom/help")
  282.     cp("rom/startup")
  283.     for k,v in pairs(fs.list("rom/programs")) do
  284.         cp("rom/programs/"..v)
  285.     end
  286.     for k,v in pairs(fs.list("rom/apis")) do
  287.         cp("rom/apis/"..v)
  288.     end
  289.     for k,v in pairs(fs.list("rom/help")) do
  290.         cp("rom/help/"..v)
  291.     end
  292.  
  293.     if urlinstaller then
  294.         local gI=http.get(urlinstaller)
  295.         local text=gI.readAll()
  296.         gI.close()
  297.         local wI=fs.open(dir..filepath,"w")
  298.         wI.write(text)
  299.         wI.close()
  300.     end
  301.  
  302.  
  303. else
  304.     print("Loading OS Environment for directory "..dir..".")
  305.  
  306. end
  307.  
  308. term.setBackgroundColor(colors.black)
  309. term.setTextColor(colors.white)
  310. term.clear()
  311. term.setCursorPos(1,1)
  312.  
  313. --Insert option to select OS'es...
  314.  
  315.  
  316. if printStuff then write("Running environment for OS: \""..OS.."\" in 3")
  317.     for i=1,2 do
  318.         sleep(.33)
  319.         write(".")
  320.     end
  321.     sleep(0.33)
  322.     write("2")
  323.     for i=1,2 do
  324.         sleep(.33)
  325.         write(".")
  326.     end
  327.     sleep(0.33)
  328.     write("1")
  329.     for i=1,2 do
  330.         sleep(.33)
  331.         write(".")
  332.     end
  333.     sleep(0.33)
  334. end
  335.  
  336. sandbox(dir)
  337.  
  338. os.shutdown=function()
  339.     restore()
  340.     term.setTextColor(colors.white)
  341.     term.setBackgroundColor(colors.black)
  342.     term.clear()
  343.     local mx,my=term.getSize()
  344.     term.setCursorPos(1, math.floor(my/2))
  345.     centerPrint("Done loading OS, rebooting...")
  346.     pcall(sleep,1.5)
  347.     os.reboot()
  348. end
  349.  
  350.  
  351. term.setBackgroundColor(colors.black)
  352. term.setTextColor(colors.white)
  353. term.clear()
  354. term.setCursorPos(1,1)
  355. if firstTime and filepath then shell.run(filepath) end
  356. os.run({},"rom/programs/shell")
  357. end
Advertisement
Add Comment
Please, Sign In to add comment