MudkipTheEpic

Sandbox API [For Drag]

Jun 28th, 2013
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.83 KB | None | 0 0
  1.     local args={...}
  2.     local dir=args[1].."/"
  3.     local Perms=args[2]
  4.     --local filepath=args[3]
  5.     --local urlinstaller=args[4]
  6.     local Disks
  7.     local oSides=rs.getSides
  8.     local pErr=printError
  9.     local oIP=ipairs
  10.     local oInsert=table.insert
  11. --[[
  12.     if not fs.exists(".System/UserPerms/"..OS) then
  13.         local writeFirst=fs.open(".System/UserPerms/"..OS,"w")
  14.         writeFirst.write(textutils.serialize({}))
  15.         writeFirst.close()
  16.     end
  17.     local readPerms=fs.open(".System/UserPerms/"..OS,"r")
  18.     local Perms=textutils.unserialize(readPerms.readAll())
  19.     readPerms.close()
  20. --]]
  21.  
  22.     local firstTime=false
  23.  
  24.     local oCombine=fs.combine
  25.     local oSub=string.sub
  26.     local oLen=string.len
  27.     local function queue(word)
  28.         for p=1,#word do
  29.             os.queueEvent("char",word:sub(p,p))
  30.         end
  31.         os.queueEvent("key",keys.enter)
  32.     end
  33.  
  34.     local function activateInstaller(filepath)
  35.         if filepath then
  36.             shell.run(filepath)
  37.         end
  38.     end
  39.  
  40.  
  41.     sandbox=nil
  42.  local printStuff=false --Change to see printed stuff or not.
  43.  
  44. --Backup FS declaration...
  45. local oFs={}
  46. for k, v in pairs(fs) do
  47.     oFs[k]=v
  48. end
  49.  
  50. --Backup peripherals declaration...
  51. local oldP={}
  52. for k,v in pairs(peripheral) do
  53.     oldP[k]=v
  54. end
  55. --Backup others declaration...
  56. local oldDfile=dofile
  57. local oldLfile=loadfile
  58. local err = error
  59. local oldReboot=os.reboot
  60. local oldShutdown=os.shutdown
  61. local oMountPath=disk.getMountPath
  62. local oPresent=disk.isPresent
  63.  
  64.  
  65. local oldsenv=setfenv
  66. local oldmeta=setmetatable
  67. local orset=rawset
  68. local bPair=pairs
  69.  
  70. local function centerPrint(text, ny)
  71.     if type(text) == "table" then for _, v in pairs(text) do centerPrint(v) end
  72. else
  73.     local x, y = term.getCursorPos()
  74.     local w, h = term.getSize()
  75.     term.setCursorPos(w/2 - text:len()/2, ny or y)
  76.     print(text)
  77. end
  78. end
  79.  
  80. local function centerWrite(text, ny)
  81.     if type(text) == "table" then for _, v in pairs(text) do centerWrite(v) end
  82. else
  83.     local x, y = term.getCursorPos()
  84.     local w, h = term.getSize()
  85.     term.setCursorPos(w/2 - text:len()/2, ny or y)
  86.     write(text)
  87. end
  88. end
  89.  
  90. local function md(d)
  91.     os.queueEvent(" ")
  92.     os.pullEventRaw()
  93.     return fs.makeDir(dir.."/"..d)
  94. end
  95.  
  96. local function cp(f,ftwo)
  97.     ftwo=ftwo or dir.."/"..f
  98.     os.queueEvent(" ")
  99.     os.pullEventRaw()
  100.     return pcall(fs.copy,f,ftwo)
  101. end
  102.  
  103. local function clear()
  104.     term.clear()
  105.     term.setCursorPos(1,1)
  106. end
  107.  
  108. local function sandbox(directory)
  109.     local sandboxed = directory
  110.  
  111.     local rT={[1] = sandboxed, [2] = sandboxed.."rom"}
  112.     Disks = {}
  113.  
  114.     local function getDisks()
  115.         local Disks = {}
  116.        
  117.         for i,v in bPair(oldP.getNames()) do
  118.             if oldP.getType(v)=="drive" and oldP.call(v,"hasData") and (not Perms or Perms[v]) then
  119.                 local der = {}
  120.                 der[1] = v
  121.                 der[2] = oldP.call(v,"getMountPath")
  122.                 der[3] = "disk"
  123.                 if oFs.exists(sandboxed .. "/disk") or (#Disks>0 and Disks[#Disks][3]=="disk") then
  124.                     local on = 1
  125.                     while true do
  126.                         local on = on + 1
  127.                         if not oFs.exists(sandboxed .. "disk" .. on) and not (#Disks>0 and Disks[#Disks][3]=="disk" .. on) then
  128.                             der[3] = "disk" .. on
  129.                             break
  130.                         end
  131.                     end
  132.                 end
  133.                 table.insert(Disks, der)
  134.             end
  135.         end
  136. return Disks
  137.     end
  138.  
  139.     local function check(filename)
  140.         if oSub(filename,1,oLen(sandboxed)+4) == (sandboxed.."rom/" or sandboxed.."rom\\") then return true end
  141.         for k,v in bPair(rT) do
  142.             if v==filename then return true end
  143.         end
  144.         return false
  145.     end
  146.  
  147.     local function isAllowed()
  148.         return false
  149.     end
  150.  
  151.     local function errorout()
  152.         err("Access denied",3)
  153.     end
  154.  
  155.     local function returnResolved(path)
  156.         if path==nil then return sandboxed end
  157.         local nPath=oCombine("",path)
  158.         if (oSub(nPath,1,2)==".." and oLen(nPath)==2) or (oSub(nPath,1,3)=="../") then return nil end
  159.         local Disks=getDisks()
  160.         for i,v in bPair(Disks) do
  161.             if oSub(nPath, 1, oLen(v[3]) + 1) == v[3] .. "/" or nPath == v[3] then
  162.                 return "/" .. v[2] .. "/" .. (oSub(nPath, oLen(v[3]) + 2) or "")
  163.             end
  164.         end
  165.         return sandboxed..path
  166.     end
  167.  
  168.     if Perms then
  169.  
  170.  
  171.     function peripheral.call(side, method, ...)
  172.         local tA={...}
  173.         if not type(side)=="string" then err("Expected string",2) end
  174.         if not (oldP.isPresent(side) and Perms[side]) or method==nil then return nil end
  175.         if oldP.getType(side)=="modem" and not oldP.call(side,"isWireless") then
  176.             --Ugh....
  177.             if method=="getNamesRemote" then
  178.                 local tFakeNames={}
  179.                 local tNames=oldP.call(side,"getNamesRemote")
  180.                 for k,v in bPair(tNames) do
  181.                     if Perms[v] then tFakeNames[#tFakeNames+1]=v end
  182.                 end
  183.                 return tFakeNames
  184.             elseif method=="isPresentRemote" then
  185.                 if not type(tA[1])=="string" then err("Expected string",2) end
  186.                 return Perms[tA[1]] and oldP.call(side,"isPresentRemote",tA[1])
  187.             elseif method=="getTypeRemote" then
  188.                 if not type(tA[1])=="string" then err("Expected string",2) end
  189.                 return Perms[tA[1]] and oldP.call(side,"getTypeRemote",tA[1])
  190.             elseif method=="getMethodsRemote" then
  191.                 if not type(tA[1])=="string" then err("Expected string",2) end
  192.                 return Perms[tA[1]] and oldP.call(side,"getMethodsRemote",tA[1])
  193.             elseif method=="callRemote" then
  194.                 if not type(tA[1])=="string" then err("Expected string",2) end
  195.                 return Perms[tA[1]] and oldP.call(side,"callRemote",unpack(tA))
  196.             end
  197.         end
  198.         return oldP.call(side,method,unpack(tA))
  199.     end
  200.  
  201.     local newCall=peripheral.call
  202.  
  203.     function peripheral.getNames()
  204.         local tResults = {}
  205.         for n,sSide in oIP( rs.getSides() ) do
  206.             if oldP.isPresent( sSide ) and Perms[sSide] then
  207.                 table.insert( tResults, sSide )
  208.                 if oldP.getType( sSide ) == "modem" and not oldP.call( sSide, "isWireless" ) then
  209.                     local tRemote = oldP.call( sSide, "getNamesRemote" )
  210.                     for n,sName in oIP( tRemote ) do
  211.                         if Perms[sName] then table.insert( tResults, sName ) end
  212.                     end
  213.                 end
  214.             end
  215.         end
  216.         return tResults
  217.     end
  218.     function peripheral.isPresent( _sSide )
  219.         if oldP.isPresent( _sSide ) and Perms[_sSide] then
  220.             return true
  221.         end
  222.         for n,sSide in oIP( oSides() ) do
  223.             if Perms[sSide] and oldP.getType( sSide ) == "modem" and not oldP.call( sSide, "isWireless" ) then
  224.                 if oldP.call( sSide, "isPresentRemote", _sSide ) and Perms[_sSide]  then
  225.                     return true
  226.                 end
  227.             end
  228.         end
  229.         return false
  230.     end
  231.  
  232.     function peripheral.getType( _sSide )
  233.         if oldP.isPresent( _sSide ) and Perms[_sSide] then
  234.             return oldP.getType( _sSide )
  235.         end
  236.         for n,sSide in oIP( oSides() ) do
  237.             if Perms[sSide] and oldP.getType( sSide ) == "modem" and not oldP.call( sSide, "isWireless" ) then
  238.                 if oldP.call( sSide, "isPresentRemote", _sSide ) and Perms[_sSide] then
  239.                     return oldP.call( sSide, "getTypeRemote", _sSide )
  240.                 end
  241.             end
  242.         end
  243.         return nil
  244.     end
  245.  
  246.     function peripheral.getMethods( _sSide )
  247.         if oldP.isPresent( _sSide ) and Perms[_sSide] then
  248.             return oldP.getMethods( _sSide )
  249.         end
  250.         for n,sSide in oIP( oSides() ) do
  251.             if Perms[sSide] and oldP.getType( sSide ) == "modem" and not oldP.call( sSide, "isWireless" ) then
  252.                 if Perms[_sSide] and oldP.call( sSide, "isPresentRemote", _sSide )  then
  253.                     return oldP.call( sSide, "getMethodsRemote", _sSide )
  254.                 end
  255.             end
  256.         end
  257.         return nil
  258.     end
  259.  
  260.     local newP={}
  261.     for k,v in pairs(peripheral) do
  262.         newP[k]=v
  263.     end
  264.  
  265.     function peripheral.wrap( _sSide )
  266.         if newP.isPresent( _sSide ) then
  267.             local tMethods = newP.getMethods( _sSide )
  268.             local tResult = {}
  269.             for n,sMethod in oIP( tMethods ) do
  270.                 tResult[sMethod] = function( ... )
  271.                     return newP.call( _sSide, sMethod, ... )
  272.                 end
  273.             end
  274.             return tResult
  275.         end
  276.         return nil
  277.     end
  278.  
  279.     --[[function peripheral.isPresent(side)
  280.         if not type(side)=="string" then err("Expected string",2) end
  281.         if not (side=="top" or side=="bottom" or side=="front" or side=="back" or side=="left" or side=="right" or oldP.isPresent(side)) then
  282.             err("Invalid side.",2)
  283.         end
  284.         if not (oldP.isPresent(side) and Perms[side]) then return false end
  285.         return oldP.isPresent(side)
  286.     end
  287.     --]]
  288.  
  289.     --[[function peripheral.getType(side)
  290.         if not type(side)=="string" then err("Expected string",2) end
  291.         if not (side=="top" or side=="bottom" or side=="front" or side=="back" or side=="left" or side=="right") then
  292.             err("Invalid side.",2)
  293.         end
  294.         if not (oldP.isPresent(side) and Perms[side]) then return nil end
  295.         return oldP.getType(side)
  296.     end
  297.     --]]
  298.  
  299.     --[[function peripheral.getMethods(side)
  300.         if not type(side)=="string" then err("Expected string",2) end
  301.         if not (side=="top" or side=="bottom" or side=="front" or side=="back" or side=="left" or side=="right") then
  302.             err("Invalid side.",2)
  303.         end
  304.         if not (oldP.isPresent(side) and Perms[side]) then return nil end
  305.         return oldP.getMethods(side)
  306.     end
  307.     --]]
  308.  
  309.     end
  310.  
  311.     function fs.open(f,v)
  312.         f=returnResolved(f)
  313.         if f==nil then return nil end
  314.         if isAllowed() then return oFs.open(f,v) end
  315.         if v ~= ("r" or "rb" or nil) then
  316.             if check(f) then
  317.                 errorout()
  318.             end
  319.         end
  320.         return oFs.open(f,v)
  321.     end
  322.  
  323.     function fs.exists(f)
  324.         f=returnResolved(f)
  325.         if f==nil then return false end
  326.         return oFs.exists(f)
  327.     end
  328.  
  329.     function fs.isDir(f)
  330.         f=returnResolved(f)
  331.         if f==nil then return false end
  332.         return oFs.isDir(f)
  333.     end
  334.  
  335.     function fs.delete(f)
  336.         f=returnResolved(f)
  337.         if f==nil then err("Invalid Path",2) return nil end
  338.         if isAllowed() then return oFs.delete(f) end
  339.         if check(f) then
  340.             errorout()
  341.         end
  342.         return oFs.delete(f)
  343.     end
  344.  
  345.     function fs.copy(f,v)
  346.         f=returnResolved(f)
  347.         v=returnResolved(v)
  348.         if f==nil or v==nil then err("Invalid Path",2) end
  349.         if isAllowed() then return oFs.copy(f,v) end
  350.         if check(v) then
  351.             errorout()
  352.         end
  353.         return oFs.copy(f,v)
  354.     end
  355.  
  356.     function fs.move(f,v)
  357.         f=returnResolved(f)
  358.         v=returnResolved(v)
  359.         if f==nil or v==nil then err("Invalid Path",2) end
  360.         if isAllowed() then return oFs.move(f,v) end
  361.         if check(f) or check(v) then
  362.             errorout()
  363.         end
  364.         return oFs.move(f,v)
  365.     end
  366.  
  367.     function fs.isReadOnly(f)
  368.         f=returnResolved(f)
  369.         if f==nil then return false end
  370.         if check(f) then
  371.             return true
  372.         end
  373.         return oFs.isReadOnly(f)
  374.     end
  375.  
  376.  
  377.     function fs.list(rawDir)
  378.         dir=returnResolved(rawDir)
  379.         if dir==nil then err("Invalid Path",2) end
  380.         local Disks=getDisks()
  381.         local res = oFs.list(dir)
  382.         if oCombine("", rawDir) == "" then
  383.             for i,v in bPair(Disks) do
  384.                 table.insert(res, v[3])
  385.             end
  386.         end
  387.         return res
  388.     end
  389.  
  390.     function fs.getName(file)
  391.         file=returnResolved(file)
  392.         if file==nil then err("Invalid Path",2) end
  393.         if file==sandboxed then return "root" end
  394.         return oFs.getName(file)
  395.     end
  396.  
  397.     function fs.makeDir(dir)
  398.         dir=returnResolved(dir)
  399.         if dir==nil then err("Invalid Path",2) end
  400.         return oFs.makeDir(dir)
  401.     end
  402.  
  403.     function fs.getSize(filepath)
  404.         filepath=returnResolved(filepath)
  405.         if filepath==nil then err("Invalid Path",2) end
  406.         return oFs.getSize(filepath)
  407.     end
  408.  
  409.     function fs.getDrive(filepath)
  410.         filepath=returnResolved(filepath)
  411.         if filepath==nil then err("Invalid Path",2) end
  412.         if check(filepath) then return "rom" end
  413.         return oFs.getDrive(filepath)
  414.     end
  415.  
  416.     --[[
  417.     function setfenv(func,...)
  418.         for k,v in bPair(protectedFuncs) do
  419.             if v==func then errorout() end
  420.         end
  421.         return oldsenv(func,...)
  422.     end
  423.  
  424.     function setmetatable(table,...)
  425.         if table==fs or table==_G then errorout() end
  426.         return oldmeta(table,...)
  427.     end
  428.  
  429.     function rawset(table,...)
  430.         if table==fs then errorout() end
  431.         return orset(table,...)
  432.     end
  433.     function os.reboot()
  434.         local writeReboot=oFs.open(".OSRunner/rebootPath","w")
  435.         writeReboot.write(sandboxed:sub(1,#sandboxed-1))
  436.         writeReboot.close()
  437.         oldReboot()
  438.     end
  439.     local protectedFuncs={setfenv,setmetatable,rawset,bPair}
  440.     for k,v in bPair(fs) do
  441.         table.insert(protectedFuncs,v)
  442.     end
  443.     ]]
  444. end
  445.  
  446. local function restore()
  447.     _G.fs={}
  448.     for k,v in bPair(oFs) do
  449.         _G.fs[k]=v
  450.     end
  451.     for k,v in bPair(oldP) do
  452.         _G.peripheral[k]=v
  453.     end
  454.     _G.setfenv=oldsenv
  455.     _G.os.reboot=oldReboot
  456.     _G.os.shutdown=oldShutdown
  457.     _G.setmetatable=oldmeta
  458.     _G.rawset=orset
  459. end
  460.  
  461. --Main program begins here...
  462.  
  463.  
  464. if not fs.isDir(dir) or not fs.isDir(dir.."/rom") then local firstTime=true
  465.     fs.delete(dir) fs.makeDir(dir)
  466.     --term.setBackgroundColor(colors.black)
  467.     --term.setTextColor(colors.white)
  468.     --clear()
  469.     local mx,my=term.getSize()
  470.     --term.setCursorPos(1, math.floor(my/2))
  471.     --centerPrint("Creating User's Virtual Environment")
  472.     --centerPrint("Please wait...")
  473.     --if printStuff then centerPrint("Creating OS Environment for directory "..dir..".") end
  474.  
  475.     md("rom")
  476.     md("rom/programs")
  477.     md("rom/programs/secret")
  478.     md("rom/apis")
  479.     md("rom/help")
  480.     cp("rom/startup")
  481.     for k,v in pairs(fs.list("rom/programs")) do
  482.         cp("rom/programs/"..v)
  483.     end
  484.     for k,v in pairs(fs.list("rom/apis")) do
  485.         cp("rom/apis/"..v)
  486.     end
  487.     for k,v in pairs(fs.list("rom/help")) do
  488.         cp("rom/help/"..v)
  489.     end
  490.     if fs.isDir(".System/DefaultUserFiles/") then
  491.         for k,v in pairs(fs.list("System/DefaultUserFiles")) do
  492.             cp(".System/DefaultUserFiles/"..v,dir..v)
  493.         end
  494.     end
  495.  
  496.     --[[if urlinstaller then
  497.         local gI=http.get(urlinstaller)
  498.         local text=gI.readAll()
  499.         gI.close()
  500.         local wI=fs.open(dir..filepath,"w")
  501.         wI.write(text)
  502.         wI.close()
  503.     end
  504. --]]
  505.  
  506.  
  507. else
  508.     --print("Loading User Environment for user "..OS..".")
  509.  
  510. end
  511.  
  512. --term.setBackgroundColor(colors.black)
  513. --term.setTextColor(colors.white)
  514. --term.clear()
  515. --term.setCursorPos(1,1)
  516.  
  517.  
  518.  
  519. --[[if printStuff then write("Running environment for User: \""..OS.."\" in 3")
  520.     for i=1,2 do
  521.         sleep(.33)
  522.         write(".")
  523.     end
  524.     sleep(0.33)
  525.     write("2")
  526.     for i=1,2 do
  527.         sleep(.33)
  528.         write(".")
  529.     end
  530.     sleep(0.33)
  531.     write("1")
  532.     for i=1,2 do
  533.         sleep(.33)
  534.         write(".")
  535.     end
  536.     sleep(0.33)
  537. end
  538. --]]
  539. sandbox(dir)
  540.  
  541. os.shutdown=function()
  542.     restore()
  543.     --term.setTextColor(colors.white)
  544.     --term.setBackgroundColor(colors.black)
  545.     --term.clear()
  546.     --local mx,my=term.getSize()
  547.     --term.setCursorPos(1, math.floor(my/2))
  548.     --centerPrint("Done loading user, rebooting...")
  549.     --pcall(sleep,1.5)
  550.     oldShutdown()
  551. end
  552.  
  553.  
  554. --term.setBackgroundColor(colors.black)
  555. --term.setTextColor(colors.white)
  556. --term.clear()
  557. --term.setCursorPos(1,1)
  558.  
  559. --os.run({},"rom/programs/shell")
Advertisement
Add Comment
Please, Sign In to add comment