Greygraphics

SecureOS

Jun 14th, 2016
474
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Set up everything
  2. local args = {...}
  3. local data = {}
  4. local files = 0
  5. local cursor = 0
  6.  
  7. --Open file and read contens into array
  8. local file = fs.open(args[1],"r")
  9. local ln = ""
  10.  
  11. while true do
  12.     ln = file.readLine()
  13.     if ln then
  14.         data[#data+1] = ln
  15.     else
  16.         break
  17.     end
  18. end
  19.  
  20. file.close()
  21. file = nil
  22. ln = nil
  23.  
  24. --Get amount of files stored
  25. for i=1,#data,1 do
  26.     if data[i] == "NEWFILE" then
  27.         files = files+1
  28.     end
  29. end
  30. print("Files found: "..files)
  31.  
  32. --Dump function
  33. local function dmp(s,c)
  34.     local newfile = fs.open(s,"w")
  35.     for i=1,#c,1 do
  36.         newfile.writeLine(c[i])
  37.     end
  38.     newfile.close()
  39.     print("Created file \""..s.."\"")
  40.     return
  41. end
  42.  
  43. --Goto file function
  44. local function nextfile()
  45.     repeat
  46.     cursor = cursor +1
  47.     until data[cursor] == "NEWFILE"
  48. end
  49.  
  50. --Read specific file
  51. for i=1,files,1 do
  52.     nextfile() --Goto next file
  53.     local cfile = {}
  54.     cursor = cursor +1
  55.     local name = data[cursor]
  56.     cursor = cursor +1
  57.     repeat
  58.         cfile[#cfile+1] = data[cursor]
  59.         cursor = cursor +1
  60.     until data[cursor] == "EOF"
  61.     dmp(name,cfile)
  62. end
  63.  
  64. --[[
  65. NEWFILE
  66. startup
  67. shell.run("SecOS")
  68. EOF
  69. NEWFILE
  70. SecOS
  71. os.pullEvent = os.pullEventRaw
  72. local username, passwd = ""
  73. local run = true
  74. local status = ""
  75. local w,h = term.getSize()
  76. os.loadAPI("/secure")
  77.  
  78. local function drawHeader()
  79.     local w,h = term.getSize()
  80.    
  81.     term.setBackgroundColor(colors.white)
  82.     term.setTextColor(colors.black)
  83.    
  84.     term.setCursorPos(1,1)
  85.     term.clearLine()
  86.     term.write("SecOS running")
  87.     local s = textutils.formatTime(os.time())
  88.     term.setCursorPos(w-string.len(s)+1,1)
  89.    
  90.     term.write(s)
  91.     term.setCursorPos(1,3)
  92. end
  93.  
  94. local function drawStatus()
  95.     local x,y = term.getCursorPos()
  96.    
  97.     term.setBackgroundColor(colors.white)
  98.     term.setTextColor(colors.black)
  99.    
  100.     term.setCursorPos(math.floor((w/2)-(string.len(status)/2)),h)
  101.     term.clearLine()
  102.     term.write(status)
  103.    
  104.     term.setBackgroundColor(colors.black)
  105.     term.setTextColor(colors.white)
  106.     term.setCursorPos(x,y)
  107. end
  108.  
  109. local function updateStatus()
  110.     drawStatus()
  111.     if (not (status == "")) then
  112.         sleep(1)
  113.         status = ""
  114.         drawStatus()
  115.     end
  116. end
  117.  
  118. local function login()
  119.     term.setBackgroundColor(colors.black)
  120.     term.setTextColor(colors.white)
  121.    
  122.     term.setCursorPos(1,3)
  123.     term.clearLine()
  124.     term.write("Username: ")
  125.     username = read(nil,nil,nil)
  126.     if (username == "exit") then   
  127.         run = false
  128.         return
  129.     end
  130.    
  131.     term.setCursorPos(1,4)
  132.     term.clearLine()
  133.     term.write("Password: ")
  134.     passwd = read("*",nil,nil)
  135.    
  136.     if (secure.resolvePass(username,passwd)) then
  137.         term.setBackgroundColor(colors.black)
  138.         term.setTextColor(colors.white)
  139.        
  140.         status = "Login successful!"
  141.         run = false
  142.         return
  143.     else
  144.         term.setBackgroundColor(colors.black)
  145.         term.setTextColor(colors.white)
  146.        
  147.         term.setCursorPos(1,4)
  148.         term.clearLine()
  149.         status = "Login failed!"
  150.         run = true
  151.         return
  152.     end
  153. end
  154.  
  155. term.clear()
  156. drawStatus()
  157. while (run) do
  158.     parallel.waitForAll(drawHeader,updateStatus,login)
  159. end
  160. updateStatus()
  161.  
  162. term.setCursorPos(1,1)
  163. term.clear()
  164. os.unloadAPI("secure")
  165.  
  166. usernamee = nil
  167. passwd = nil
  168. EOF
  169. NEWFILE
  170. secure
  171. function encode(sInput)
  172.     local encoded = ""
  173.     local value = 0
  174.    
  175.     for i=1,string.len(sInput),1 do
  176.         value = value+string.byte(string.sub(sInput,i,i))
  177.     end
  178.    
  179.     for i=1,string.len(sInput),1 do
  180.         encoded = encoded..tostring(string.byte(string.sub(sInput,i,i))*value)
  181.     end
  182.  
  183.     return encoded
  184. end
  185.  
  186. function resolvePass(username2, passwd2)
  187.     if (not (fs.exists("/users/"..username2..".usr"))) then
  188.         return false
  189.     end
  190.     local file = fs.open("/users/"..username2..".usr","r")
  191.     chars = tostring(file.readLine())
  192.     file.close()
  193.    
  194.     if (chars == secure.encode(passwd2)) then
  195.         return true
  196.     else
  197.         return false
  198.     end
  199. end
  200. EOF
  201. NEWFILE
  202. mkusr
  203. os.loadAPI("secure")
  204.  
  205. term.write("Root password: ")
  206. local pass = read("*",nil,nil)
  207.  
  208. local file = fs.open("/users/root.usr","r")
  209. local code = file.readLine()
  210. file.close()
  211.  
  212. if (code == secure.encode(pass)) then
  213.    
  214. else
  215.     print("Password incorrect!")
  216.     return
  217. end
  218.  
  219. term.write("New user: ")
  220. user = read(nil,nil,nil)
  221.  
  222. term.write("New password: ")
  223. pass = read("*",nil,nil)
  224.  
  225. if (fs.exists("/users/"..user..".usr")) then
  226.     print("This user already exists!")
  227.     return
  228. end
  229.  
  230. local file = fs.open("/users/"..user..".usr","w")
  231. file.write(secure.encode(pass))
  232. file.close()
  233.  
  234. print("User successfully created!")
  235.  
  236. os.unloadAPI("secure")
  237. return
  238. EOF
  239. NEWFILE
  240. passwd
  241. local args = {...}
  242. local name, pass = ""
  243.  
  244. os.loadAPI("secure")
  245.  
  246. term.write("Username: ")
  247. name = read(nil,nil,nil)
  248. term.write("Password: ")
  249. pass = read("*",nil,nil)
  250.  
  251. if (not fs.exists("/users/"..name..".usr")) then
  252.     print("User not found!")
  253.     return
  254. end
  255.  
  256. local file = fs.open("/users/"..name..".usr","r")
  257. local code = file.readLine()
  258. file.close()
  259.  
  260. if (code == secure.encode(pass)) then
  261.     print("Password correct!")
  262. else
  263.     print("Password incorrect!")
  264.     return
  265. end
  266.  
  267. term.write("New password: ")
  268. pass = read("*",nil,nil)
  269.  
  270. local file = fs.open("/users/"..name..".usr","w")
  271. file.write(secure.encode(pass))
  272. file.close()
  273.  
  274. print("Password successfully changed!")
  275.  
  276. os.unloadAPI("secure")
  277. return
  278.  
  279. 51528501725017252432
  280. EOF
  281. NEWFILE
  282. /users/root.usr
  283. 51528501725017252432
  284. EOF
  285. ]]--
RAW Paste Data