Guest User

Untitled

a guest
May 15th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. def add_user(name=None, password=None, admin='no', apache='no', skip='yes'):
  2. """
  3. Add a new user named ``name`` to the remote system.
  4.  
  5. If ``name`` or ``password`` are unspecified, you will be prompted for them.
  6.  
  7. To have a user added to the admin/wheel group, specify ``admin=yes``.
  8.  
  9. To add the user to the appropriate Apache group, specify ``apache=yes``.
  10.  
  11. To force recreation / re-passwd'ing of user even if it already exists,
  12. specify ``skip=no``.
  13. """
  14. apache_user = 'www-data' if is_debian() else 'apache'
  15. # TODO: make this sort of crap work better
  16. usermod = '/usr/sbin/usermod '
  17. # Get name
  18. if not name:
  19. name = prompt("New user's username: ")
  20. # Check for user, short circuit if necessary
  21. if user_exists(name) and skip.lower() in ['y', 'yes']:
  22. print("User %s already exists, skipping" % name)
  23. return
  24. # Add user
  25. with settings(warn_only=True):
  26. sudo('/usr/sbin/useradd -m %s -s /bin/bash' % name)
  27. # Set password if necessary
  28. while not password:
  29. password = getpass.getpass("New user's password: ")
  30. password_confirmation = getpass.getpass("Again: ")
  31. if password != password_confirmation:
  32. print("Sorry, passwords do not match.")
  33. password = None
  34. # Since we're getpass-ing, it'd be silly to print out the password here.
  35. # So hide the running line. No, this is still very insecure on the remote end :(
  36. with hide('running'):
  37. sudo('echo "%s\n%s" | /usr/bin/passwd %s' % (password, password, name))
  38. # Handle Apache if necessary
  39. if apache.lower() in ['yes', 'y']:
  40. # Make apache group their login group (for ease of collaboration)
  41. sudo(usermod + '-g %s %s' % (apache_user, name))
  42. # Add them back to their own personal group
  43. sudo(usermod + '-a -G %s %s' % (name, name))
  44. # If admin, add to the admin group
  45. if admin.lower() in ['yes', 'y']:
  46. if is_really_debian():
  47. admin_name = 'sudo'
  48. elif is_debian():
  49. admin_name = 'admin'
  50. else:
  51. admin_name = 'wheel'
  52. sudo(usermod + '-a -G %s %s' % (admin_name, name))
Add Comment
Please, Sign In to add comment