MudkipTheEpic

Sandbox 2.0 (Supported)

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