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