Sxw1212

chroot

Jul 25th, 2013
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.38 KB | None | 0 0
  1. local chrooted=false
  2. local chrootpath=""
  3. local chrootpass=nil
  4. local fscombine=fs.combine
  5. local stringsub=string.sub
  6. local stringlen=string.len
  7. local tableinsert=table.insert
  8. local rootfs = {}
  9.  
  10. for k,v in pairs(fs) do
  11.   rootfs[k] = v
  12. end
  13.  
  14. function chroot(path, pass)
  15. if chrooted then
  16. error("Cannot chroot from pre-existing chroot enviroment without unchrooting")
  17. end
  18. if not pass then
  19. chrootpass=nil
  20. else
  21. chrootpass=pass
  22. end
  23. if fs.isDir(path) then
  24. chrootpath=rootfs.combine("", path)
  25. chrooted=true
  26. else
  27. error("Could not find chroot path or is not a directory")
  28. end
  29. end
  30. function unchroot(pass)
  31. if chrooted then
  32. if chrootpass==pass then
  33. chrooted=false
  34. chrootpath=""
  35. return true
  36. else
  37. error("Pass incorrect")
  38. end
  39. else
  40. error("Not chrooted.")
  41. end
  42. end
  43. function isChrooted()
  44. return chrooted
  45. end
  46. --From lua users wiki
  47. local function starts(String,Start)
  48.    return stringsub(String,1,stringlen(Start))==Start
  49. end
  50. --BEGIN
  51. local function valid(path)
  52.     if starts(fscombine("", path), "rom") then
  53.         return path
  54.     end
  55.     local xpath=fscombine(chrootpath, path)
  56.     if starts(xpath, chrootpath) then
  57.         return xpath
  58.     else
  59.         error("No escaping the chroot jail! The xpath is "..xpath)
  60.     end
  61. end
  62.  
  63. local function root(path)
  64.     if fscombine(chrootpath, "") == fscombine(path, "") then
  65.         return true
  66.     end
  67. end
  68.  
  69. fs.list=function(path)
  70.     if root(valid(path)) then
  71.         local list = rootfs.list(valid(path))
  72.         tableinsert(list, "rom")
  73.         return list
  74.     else
  75.         return rootfs.list(valid(path))
  76.     end
  77. end
  78.  
  79. fs.exists=function(path)
  80. return rootfs.exists(valid(path))
  81. end
  82. fs.isDir=function(path)
  83. return rootfs.isDir(valid(path))
  84. end
  85. fs.isReadOnly=function(path)
  86. return rootfs.isReadOnly(valid(path))
  87. end
  88. fs.getDrive=function(path)
  89. return rootfs.getDrive(valid(path))
  90. end
  91. fs.getSize=function(path)
  92. return rootfs.getSize(chrootpath..path)
  93. end
  94. fs.makeDir=function(path)
  95. return rootfs.makeDir(valid(path))
  96. end
  97. fs.move=function(path, cpath)
  98. return rootfs.move(valid(path), valid(cpath))
  99. end
  100. fs.copy=function(path, cpath)
  101. return rootfs.copy(valid(path), valid(cpath))
  102. end
  103. fs.delete=function(path)
  104. return rootfs.delete(valid(path))
  105. end
  106. fs.open=function(path, m)
  107. return rootfs.open(valid(path), m)
  108. end
  109.  
  110. fs.secret=function()
  111.     local longtimeago=rootfs.open("/rom/programs/secret/alongtimeago","r")
  112.     local prog=loadstring(longtimeago.readAll())
  113.     longtimeago.close()
  114.     return prog
  115. end
Advertisement
Add Comment
Please, Sign In to add comment