Guest User

Untitled

a guest
Jan 15th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. # IMPORTANT : Every Function of Users.class should take String as arguments
  2.  
  3. #TODO
  4. ## DELETE
  5. # Should it have a security ? This is really violent, all destructive and subject to errors
  6. # 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
  7. # Files deleting behavior should be in config file
  8. ## CREATE
  9. # The directories to create should be in config file
  10. # =?> Should it take XML for user creation ???
  11. ## ALL
  12. # Result XML should have a <CODE> and not only a message. Find a way for libs to pass code appart from message
  13. # Verify repeated bunch of codes
  14.  
  15. require 'etc'
  16. require 'fileutils'
  17.  
  18. class Users
  19. attr_accessor :username, :uid, :gid, :homedir, :shell
  20.  
  21. def initialize(username,uid,gid,homedir,shell)
  22. @username = username
  23. @uid = uid
  24. @gid = gid
  25. @homedir = homedir
  26. @shell = shell
  27. end
  28.  
  29. def self.get(uid)
  30. begin
  31. if uid =~ /^\d+$/
  32. user = Etc.getpwuid(uid.to_i)
  33. else
  34. user = Etc.getpwnam(uid)
  35. end
  36. rescue
  37. puts "[ERR] User #{uid} doesn't exist"
  38. return "404 : User doesn't exist"
  39. end
  40.  
  41. @uid = user.uid
  42. if @uid < $minuid
  43. puts "[ERR] #{@uid} is below minuid of #{$minuid}"
  44. return "403 : Unauthorized Access to user"
  45. end
  46. @username = user.name
  47. @gid = user.gid
  48. @homedir = user.dir
  49. @shell = user.shell
  50.  
  51. if @username == ""
  52. return 404
  53. else
  54. puts "[OK] Done returning #{@username} info"
  55. return self.new(@username,@uid,@gid,@homedir,@shell)
  56. end
  57. end
  58.  
  59. def self.all
  60. @users = Array.new
  61. Etc.passwd do |user|
  62. uid = user.uid
  63. unless uid < $minuid or uid > 65000
  64. @users.push(self.get(uid.to_s))
  65. end
  66. end
  67. return @users
  68. end
  69.  
  70. def self.create(username,password,shell)
  71. if username == nil
  72. puts "[ERR] No username provided"
  73. return "503 : No username provided"
  74. end
  75. unless username =~ /^\w+$/i and username.length > $minuserlength-1
  76. puts "[ERR] Username #{username} is malformed or too short"
  77. return "503 : Username should be alphanumerical and at least #{$minuserlength} characters long"
  78. end
  79. if shell == nil or shell == ""
  80. shell = "bash"
  81. end
  82. unless shell == "bash" or shell == "false"
  83. puts "[ERR] Shell #{shell} is malformed shell"
  84. return "503 : Shell should be wether bash or false"
  85. end
  86.  
  87. Etc.passwd do |check|
  88. checkprecedence = check.name
  89. if checkprecedence =~ /^#{username.downcase}$/
  90. puts "[ERR] User #{username.downcase} already exists"
  91. return "503 : User already exists"
  92. end
  93. end
  94.  
  95. @username = username.downcase
  96. if password == nil
  97. @password = Apicommon.password
  98. else
  99. if password.length < $minpwdlength
  100. puts "[ERR] Password #{password} is too short"
  101. return "503 : Password should be at least #{$minpwdlength} characters"
  102. end
  103. @password = password
  104. end
  105. @crypted = @password.crypt(Apicommon.salt)
  106. @shell = shell
  107.  
  108. @creation = `useradd -m -s /bin/#{@shell} -gusers -p #{@crypted} #{@username}`
  109. @uid = Etc.getpwnam(@username).uid
  110. FileUtils.mkdir("/home/#{@username}/www")
  111. FileUtils.mkdir("/home/#{@username}/etc")
  112. FileUtils.mkdir("/home/#{@username}/logs")
  113. FileUtils.mkdir("/home/#{@username}/cgi-bin")
  114. FileUtils.mkdir("/home/#{@username}/sd")
  115. FileUtils.chown_R(@username,"users","/home/#{@username}")
  116. puts "[OK] Done creating : #{@username} created with /bin/#{@shell}, password #{@password}, UID #{@uid}"
  117. @result = "#{@username} created with /bin/#{@shell}, password #{@password}, UID #{@uid}"
  118. end
  119.  
  120. def self.update(uid,password,shell)
  121.  
  122. begin
  123. if uid =~ /^\d+$/
  124. user = Etc.getpwuid(uid.to_i)
  125. else
  126. user = Etc.getpwnam(uid)
  127. end
  128. rescue
  129. puts "[ERR] User doesn't exist #{uid}"
  130. return "404 : User doesn't exist"
  131. end
  132. if user.uid < $minuid
  133. puts "[ERR] Unauthorized update #{user.uid} below minuid of #{$minuid}"
  134. return "403 : Unauthorized update"
  135. end
  136. unless shell == nil
  137. unless shell == "bash" or shell == "false"
  138. puts "[ERR] Malformed shell #{shell}"
  139. return "503 : Shell should be wether bash or false"
  140. end
  141. changeshell = 1
  142. end
  143. unless password == nil
  144. if password.length < $minpwdlength
  145. puts "[ERR] Password too short #{password}"
  146. return "503 : Password should be at least #{$minpwdlength} characters"
  147. end
  148. changepassword = 1
  149. end
  150.  
  151. @username = user.name
  152. @password = password
  153. @shell = shell
  154.  
  155. if changeshell == 1
  156. puts "[OK] Updated shell with #{@shell} for #{@username}"
  157. @updateshell = `usermod -s /bin/#{@shell} #{@username}`
  158. end
  159. if changepassword == 1
  160. puts "[OK] Updated password with #{@password} for #{@username}"
  161. @crypted = @password.crypt(Apicommon.salt)
  162. @updatepassword = `usermod -p #{@crypted} #{@username}`
  163. end
  164.  
  165. if changeshell == 1 and changepassword == 1
  166. return "Updated #{@username} with password #{@password} and shell /bin/#{@shell}"
  167. elsif changeshell == 1 and changepassword != 1
  168. return "Updated #{@username} with shell /bin/#{@shell}"
  169. elsif changeshell != 1 and changepassword == 1
  170. return "Updated #{@username} with password #{@password}"
  171. else
  172. puts "[OK-BUT] There was nothing to do for #{@username}"
  173. return "Did nothing to #{@username}"
  174. end
  175.  
  176. end
  177.  
  178. def self.delete(uid)
  179.  
  180. begin
  181. if uid =~ /^\d+$/
  182. user = Etc.getpwuid(uid.to_i)
  183. else
  184. user = Etc.getpwnam(uid)
  185. end
  186. rescue
  187. puts "[ERR] User #{uid} doesn't exist"
  188. return "404 : User doesn't exist"
  189. end
  190. if user.uid < $minuid
  191. puts "[ERR] #{user.uid} is below minuid of #{$minuid}"
  192. return "403 : Unauthorized delete"
  193. end
  194.  
  195. @username = user.name
  196.  
  197. FileUtils.rmtree("/home/#{@username}")
  198. @delete = `userdel #{@username}`
  199.  
  200. puts "[OK] User #{@username} and all his files were deleted"
  201. return "#{@username} and all his files were successfully deleted. Enjoy your fat powers"
  202.  
  203. end
  204.  
  205. end
Add Comment
Please, Sign In to add comment