jesusthekiller

jLogin login

Jun 22nd, 2013
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.79 KB | None | 0 0
  1. --[[
  2.     Project info:
  3.    
  4.     Name: jLogin
  5.     Creator: Jesusthekiller
  6.     Language: Lua (CC)
  7.     Website: None
  8.     License: GNU GPL
  9.         License file can be fount at www.jesusthekiller.com/license-gpl.html
  10.  
  11.     Version: 1.0
  12. ]]--
  13.  
  14. --[[
  15.     Changelog:
  16.       1.0:
  17.         Initial Release
  18. ]]--
  19.  
  20. --[[
  21.     LICENSE:
  22.    
  23.     jLogin
  24.     Copyright (c) 2013 Jesusthekiller
  25.  
  26.     This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  27.  
  28.     This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  29.  
  30.     See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. ]]--
  32.  
  33. if not sha256 then
  34.     os.loadAPI("sha256")
  35. end
  36.  
  37. local args = {...}
  38.  
  39. local function usage()
  40.     print("Usage:")
  41.     print("  login lock")
  42.     print("  login adduser <login> <password>")
  43.     print("  login deluser <login> <password>")
  44.     print("  login passwd <login> <oldpass> <newpass>")
  45.     return
  46. end
  47.  
  48. if not fs.exists("login_users") then
  49.     local f = fs.open("login_users", "w")
  50.     f.close()
  51. end
  52.  
  53. args[1] = args[1] and args[1]:lower() -- check for no args, lower command
  54.  
  55. if args[1] == "lock" then
  56.     shell.run("startup")
  57. elseif args[1] == "adduser" and args[2] and args[3] then
  58.     local login = args[2]:lower():gsub("%s", "") -- Format user
  59.     local pass = sha256.sha256(args[3]) -- Encode password
  60.    
  61.     local f = fs.open("login_users", "r") or error("Can not open file login_users! Please reboot!", 0)
  62.    
  63.     for token in string.gmatch(f.readAll(), "[^\n]+") do -- Check is user does not exist
  64.         local p1, p2 = token:find(login)
  65.        
  66.         if p1 == 1 and p2 == #token then
  67.             f.close()
  68.             error("User "..login.." exists!", 0)
  69.         end
  70.     end
  71.    
  72.     f.close()
  73.    
  74.     -- Add users
  75.     local f = fs.open("login_users", "a") or error("Can not open file login_users! Please reboot!", 0)
  76.     f.writeLine(login)
  77.     f.writeLine(pass)
  78.     f.close()
  79.    
  80.     print("User "..login.." added!")
  81. elseif args[1] == "deluser" and args[2] and args[3] then
  82.     local login = args[2]:lower():gsub("%s", "") -- Format user
  83.     local pass = sha256.sha256(args[3]) -- Encode password
  84.    
  85.     local f = fs.open("login_users", "r") or error("Can not open file login_users! Please reboot!", 0)
  86.     local file = f.readAll()
  87.     f.close()
  88.    
  89.     if file:find(login.."\n"..pass) == 1 then
  90.         file = file:gsub(login.."\n"..pass, "")
  91.     elseif file:find("\n"..login.."\n"..pass) then
  92.         file = file:gsub("\n"..login.."\n"..pass, "")
  93.     else
  94.         error("Wrong user and password combo!", 0)
  95.     end
  96.    
  97.     local f = fs.open("login_users", "w") or error("Can not open file login_users! Please reboot!", 0)
  98.     f.write(file)
  99.     f.close()
  100.    
  101.     print("User "..login.." deleted!")
  102. elseif args[1] == "passwd" and args[2] and args[3] and args[4] then
  103.     local login = args[2]:lower():gsub("%s", "") -- Format user
  104.     local pass = sha256.sha256(args[3]) -- Encode password
  105.     local npass = sha256.sha256(args[4]) -- Encode password
  106.    
  107. local f = fs.open("login_users", "r") or error("Can not open file login_users! Please reboot!", 0)
  108.     local file = f.readAll()
  109.     f.close()
  110.    
  111.     if file:find(login.."\n"..pass) == 1 then
  112.         file = file:gsub(login.."\n"..pass, login.."\n"..npass)
  113.     elseif file:find("\n"..login.."\n"..pass) then
  114.         file = file:gsub("\n"..login.."\n"..pass, "\n"..login.."\n"..npass)
  115.     else
  116.         error("Wrong user and password combo!", 0)
  117.     end
  118.    
  119.     local f = fs.open("login_users", "w") or error("Can not open file login_users! Please reboot!", 0)
  120.     f.write(file)
  121.     f.close()
  122.    
  123.     print("Password for user "..login.." changed!")
  124. else
  125.     usage()
  126. end
  127.  
  128. os.unloadAPI("sha256")
Advertisement
Add Comment
Please, Sign In to add comment