Guest User

Untitled

a guest
Oct 16th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. def check_credentials(user, password):
  2. """Checks a given user and password against /etc/shadow (or /etc/passwd if /etc/shadow doesn't exist).
  3. Returns None on success and an error message on failure."""
  4. from crypt import crypt
  5. shadow_hash = ''
  6. salt_regex = re.compile(r'\$.*\$.*\$')
  7. if os.path.exists('/etc/shadow'):
  8. password_file = '/etc/shadow'
  9. else:
  10. password_file = '/etc/passwd'
  11. shadow = open(password_file, 'r').readlines()
  12. for line in shadow:
  13. cols = line.split(':')
  14. if cols[0] == user:
  15. shadow_hash = cols[1]
  16. if salt_regex.match(shadow_hash):
  17. salt = salt_regex.match(shadow_hash).group()
  18. hashed_pass = crypt(password, salt)
  19. if hashed_pass == shadow_hash:
  20. return True
  21. # Now check the case where OpenWRT was just started up for the first time and there's no root password set yet...
  22. # If the root password hasn't been set the telnet daemon will be running with /bin/login.sh (i.e. no-password login)
  23. retcode = getstatusoutput('ps aux | grep -v grep | grep "telnetd -l /bin/login.sh"')[0]
  24. re.purge()
  25. if retcode == 0: # A return code of 0 means grep actually grepped something (as opposed to no output)
  26. return True # Let them through
  27. else:
  28. return _("Invalid user and/or password")
Add Comment
Please, Sign In to add comment