thislooksfun

security

Nov 24th, 2012
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.06 KB | None | 0 0
  1. --[[tlfOS Security API]]--
  2.  
  3. function login()
  4.   local verify = false
  5.   local admin = false
  6.   local Users = loadUsers()
  7.   local user = ""
  8.   local pass = ""
  9.   local title = "Login"
  10.  
  11.   local login = {
  12.  
  13.   "Username:",
  14.   "",
  15.   "",
  16.   "Password:",
  17.   ""
  18.  
  19.   }
  20.  
  21.   local w,h = term.getSize()
  22.   table.insert(login, title)
  23.   local max = text.getLongestString(login) + 2
  24.   table.remove(login, #login)
  25.  
  26.   ejectAll()
  27.  
  28.   for i = 1, #Users do
  29.     if not fs.isDir("&SYS&tlf/users/"..Users[i].name) then
  30.       fs.delete("&SYS&tlf/users/"..Users[i].name)
  31.     end
  32.   end
  33.  
  34.  
  35.   if #Users == 0 then
  36.     user = makeUser("true")
  37.     verify = true
  38.     admin = true
  39.   else
  40.     term.clear()
  41.     screen.displayWindow(title, login, (w-max)/2, (h-#login-2)/2)
  42.     user = text.readWithBoundaries((w-max)/2 + 1, (w-max)/2 + max - 1, (h-#login-2)/2 + 2)
  43.     pass = text.readWithBoundaries((w-max)/2 + 1, (w-max)/2 + max - 1, (h-#login-2)/2 + 5, "*")
  44.     for i = 1, #Users do
  45.       if user == Users[i].name then
  46.         if pass == Users[i].password then
  47.           verify = true
  48.           if Users[i].admin == "true" then
  49.             admin = true
  50.           end
  51.         end
  52.       end
  53.     end
  54.   end
  55.  
  56.   if verify then
  57.     screen.clear(true, true, user)
  58.     return user, admin
  59.   else
  60.     text.printCentered("Incorrect username or password", (h-#login-2)/2 + 7)
  61.     sleep(1.5)
  62.     os.shutdown()
  63.   end
  64. end
  65.  
  66. function makeUser(admin)
  67.   if admin ~= "true" then
  68.     admin = "false"
  69.   end
  70.  
  71.   local user = ""
  72.   local pass = ""
  73.   local title = "Create User"
  74.  
  75.   local login = {
  76.  
  77.   "Enter a username and password",
  78.   "Username:",
  79.   "",
  80.   "",
  81.   "Password:",
  82.   ""
  83.  
  84.   }
  85.  
  86.   local w,h = term.getSize()
  87.   table.insert(login, title)
  88.   local max = text.getLongestString(login) + 2
  89.   table.remove(login, #login)
  90.  
  91.   user = ""
  92.   pass = ""
  93.  
  94.   screen.displayWindow(title, login, (w-max)/2, ((h-#login)-2)/2)
  95.   user = text.readWithBoundaries((w-max)/2 + 1, (w-max)/2 + max - 1, ((h-#login)-2)/2 + 3)
  96.   pass = text.readWithBoundaries((w-max)/2 + 1, (w-max)/2 + max - 1, ((h-#login)-2)/2 + 6, "*")
  97.  
  98.   if not fs.exists("&SYS&tlf/users") then
  99.     fs.makeDir("&SYS&tlf/users")
  100.   end
  101.  
  102.   if fs.exists("&SYS&tlf/users/"..user) and fs.isDir("&SYS&tlf/users/"..user) then
  103.     print("User already exists")
  104.     sleep(1)
  105.   elseif not fs.isDir("&SYS&tlf/users/"..user) then
  106.     fs.delete("&SYS&tlf/users/"..user)
  107.   else
  108.     fs.makeDir("&SYS&tlf/users/"..user)
  109.     temp = fs.open("&SYS&tlf/users/"..user.."/account", "w")
  110.     temp.write("password:"..pass.."\nadmin:"..admin)
  111.     temp.close()
  112.   end
  113.  
  114.   return user
  115. end
  116.  
  117. function changePass()
  118.   local user = ""
  119.   local passOld = ""
  120.   local passNew = ""
  121.   local title = "Change Password"
  122.   local Users = loadUsers()
  123.   local verify = false
  124.   local admin = "false"
  125.  
  126.   local login = {
  127.  
  128.   "Username:",
  129.   "",
  130.   "",
  131.   "Current Password:",
  132.   "",
  133.   "",
  134.   "New Password:",
  135.   ""
  136.  
  137.  
  138.   }
  139.  
  140.   local w,h = term.getSize()
  141.   table.insert(login, title)
  142.   local max = text.getLongestString(login) + 2
  143.   table.remove(login, #login)
  144.    
  145.   screen.displayWindow(title, login, (w-max)/2, ((h-#login)-2)/2)
  146.   user = text.readWithBoundaries((w-max)/2 + 1, (w-max)/2 + max - 1, ((h-#login)-2)/2 + 2)
  147.   passOld = text.readWithBoundaries((w-max)/2 + 1, (w-max)/2 + max - 1, ((h-#login)-2)/2 + 5, "*")
  148.   passNew = text.readWithBoundaries((w-max)/2 + 1, (w-max)/2 + max - 1, ((h-#login)-2)/2 + 8, "*")
  149.  
  150.   for i = 1, #Users do
  151.     if user == Users[i].name then
  152.       if passOld == Users[i].password then
  153.         verify = true
  154.         if Users[i].admin == "true" then
  155.           admin = "true"
  156.         end
  157.       end
  158.     end
  159.   end
  160.  
  161.   if not verify then
  162.     print("User does not exist or password is incorrect")
  163.     sleep(1)
  164.   else
  165.     fs.delete("&SYS&tlf/users/"..user.."/account")
  166.     temp = fs.open("&SYS&tlf/users/"..user.."/account", "w")
  167.     temp.write("password:"..passNew.."\nadmin:"..admin)
  168.     temp.close()
  169.   end
  170. end
  171.  
  172. function loadUsers()
  173.   local tUsers = {};
  174.   local file = nil
  175.   if not fs.exists("&SYS&tlf/users") or not fs.isDir("&SYS&tlf/users") then
  176.     fs.makeDir("&SYS&tlf/users")
  177.   end
  178.   local tFiles = fs.list("&SYS&tlf/users")
  179.   for i=1, #tFiles do
  180.     file = fs.open("&SYS&tlf/users/"..tFiles[i].."/account", "r")
  181.    
  182.     if file ~= nil then
  183.       line1 = file.readLine()
  184.      
  185.       if string.find(line1, "password:(.+)") == nil then
  186.         file.close()
  187.         fs.delete("&SYS&tlf/users/"..tFiles[i].."/account")
  188.       else
  189.        
  190.         for string in string.gmatch(line1, "password:(.+)") do
  191.           string1 = string
  192.         end
  193.        
  194.         for string in string.gmatch(file.readLine(), "admin:(.+)") do
  195.           string2 = string
  196.         end
  197.        
  198.         tUsers[i] = {name = tFiles[i], password = string1, admin = string2}
  199.        
  200.         file.close()
  201.       end
  202.     end
  203.   end
  204.   return tUsers
  205. end
  206.  
  207. function ejectAll()
  208.   disk.eject("top")
  209.   disk.eject("bottom")
  210.   disk.eject("left")
  211.   disk.eject("right")
  212.   disk.eject("front")
  213.   disk.eject("back")
  214. end
Advertisement
Add Comment
Please, Sign In to add comment