Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # IMPORTANT : Every Function of Users.class should take String as arguments
- #TODO
- ## DELETE
- # Should it have a security ? This is really violent, all destructive and subject to errors
- # Should avec a DOUBLE check : delete url/:name/:uid or url/:name/:uid or something like that, or take absolutely the username as a check or a string as a check
- # Files deleting behavior should be in config file
- ## CREATE
- # The directories to create should be in config file
- # =?> Should it take XML for user creation ???
- ## ALL
- # Result XML should have a <CODE> and not only a message. Find a way for libs to pass code appart from message
- # Verify repeated bunch of codes
- require 'etc'
- require 'fileutils'
- class Users
- attr_accessor :username, :uid, :gid, :homedir, :shell
- def initialize(username,uid,gid,homedir,shell)
- @username = username
- @uid = uid
- @gid = gid
- @homedir = homedir
- @shell = shell
- end
- def self.get(uid)
- begin
- if uid =~ /^\d+$/
- user = Etc.getpwuid(uid.to_i)
- else
- user = Etc.getpwnam(uid)
- end
- rescue
- puts "[ERR] User #{uid} doesn't exist"
- return "404 : User doesn't exist"
- end
- @uid = user.uid
- if @uid < $minuid
- puts "[ERR] #{@uid} is below minuid of #{$minuid}"
- return "403 : Unauthorized Access to user"
- end
- @username = user.name
- @gid = user.gid
- @homedir = user.dir
- @shell = user.shell
- if @username == ""
- return 404
- else
- puts "[OK] Done returning #{@username} info"
- return self.new(@username,@uid,@gid,@homedir,@shell)
- end
- end
- def self.all
- @users = Array.new
- Etc.passwd do |user|
- uid = user.uid
- unless uid < $minuid or uid > 65000
- @users.push(self.get(uid.to_s))
- end
- end
- return @users
- end
- def self.create(username,password,shell)
- if username == nil
- puts "[ERR] No username provided"
- return "503 : No username provided"
- end
- unless username =~ /^\w+$/i and username.length > $minuserlength-1
- puts "[ERR] Username #{username} is malformed or too short"
- return "503 : Username should be alphanumerical and at least #{$minuserlength} characters long"
- end
- if shell == nil or shell == ""
- shell = "bash"
- end
- unless shell == "bash" or shell == "false"
- puts "[ERR] Shell #{shell} is malformed shell"
- return "503 : Shell should be wether bash or false"
- end
- Etc.passwd do |check|
- checkprecedence = check.name
- if checkprecedence =~ /^#{username.downcase}$/
- puts "[ERR] User #{username.downcase} already exists"
- return "503 : User already exists"
- end
- end
- @username = username.downcase
- if password == nil
- @password = Apicommon.password
- else
- if password.length < $minpwdlength
- puts "[ERR] Password #{password} is too short"
- return "503 : Password should be at least #{$minpwdlength} characters"
- end
- @password = password
- end
- @crypted = @password.crypt(Apicommon.salt)
- @shell = shell
- @creation = `useradd -m -s /bin/#{@shell} -gusers -p #{@crypted} #{@username}`
- @uid = Etc.getpwnam(@username).uid
- FileUtils.mkdir("/home/#{@username}/www")
- FileUtils.mkdir("/home/#{@username}/etc")
- FileUtils.mkdir("/home/#{@username}/logs")
- FileUtils.mkdir("/home/#{@username}/cgi-bin")
- FileUtils.mkdir("/home/#{@username}/sd")
- FileUtils.chown_R(@username,"users","/home/#{@username}")
- puts "[OK] Done creating : #{@username} created with /bin/#{@shell}, password #{@password}, UID #{@uid}"
- @result = "#{@username} created with /bin/#{@shell}, password #{@password}, UID #{@uid}"
- end
- def self.update(uid,password,shell)
- begin
- if uid =~ /^\d+$/
- user = Etc.getpwuid(uid.to_i)
- else
- user = Etc.getpwnam(uid)
- end
- rescue
- puts "[ERR] User doesn't exist #{uid}"
- return "404 : User doesn't exist"
- end
- if user.uid < $minuid
- puts "[ERR] Unauthorized update #{user.uid} below minuid of #{$minuid}"
- return "403 : Unauthorized update"
- end
- unless shell == nil
- unless shell == "bash" or shell == "false"
- puts "[ERR] Malformed shell #{shell}"
- return "503 : Shell should be wether bash or false"
- end
- changeshell = 1
- end
- unless password == nil
- if password.length < $minpwdlength
- puts "[ERR] Password too short #{password}"
- return "503 : Password should be at least #{$minpwdlength} characters"
- end
- changepassword = 1
- end
- @username = user.name
- @password = password
- @shell = shell
- if changeshell == 1
- puts "[OK] Updated shell with #{@shell} for #{@username}"
- @updateshell = `usermod -s /bin/#{@shell} #{@username}`
- end
- if changepassword == 1
- puts "[OK] Updated password with #{@password} for #{@username}"
- @crypted = @password.crypt(Apicommon.salt)
- @updatepassword = `usermod -p #{@crypted} #{@username}`
- end
- if changeshell == 1 and changepassword == 1
- return "Updated #{@username} with password #{@password} and shell /bin/#{@shell}"
- elsif changeshell == 1 and changepassword != 1
- return "Updated #{@username} with shell /bin/#{@shell}"
- elsif changeshell != 1 and changepassword == 1
- return "Updated #{@username} with password #{@password}"
- else
- puts "[OK-BUT] There was nothing to do for #{@username}"
- return "Did nothing to #{@username}"
- end
- end
- def self.delete(uid)
- begin
- if uid =~ /^\d+$/
- user = Etc.getpwuid(uid.to_i)
- else
- user = Etc.getpwnam(uid)
- end
- rescue
- puts "[ERR] User #{uid} doesn't exist"
- return "404 : User doesn't exist"
- end
- if user.uid < $minuid
- puts "[ERR] #{user.uid} is below minuid of #{$minuid}"
- return "403 : Unauthorized delete"
- end
- @username = user.name
- FileUtils.rmtree("/home/#{@username}")
- @delete = `userdel #{@username}`
- puts "[OK] User #{@username} and all his files were deleted"
- return "#{@username} and all his files were successfully deleted. Enjoy your fat powers"
- end
- end
Add Comment
Please, Sign In to add comment