-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_auth.py
executable file
·38 lines (32 loc) · 1014 Bytes
/
user_auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# -*- coding=utf-8 -*-
#!/usr/bin/env python
import crypt
crypt_type = ''
crypt_str = ''
crypt_pw = ''
def user_auth(user, password):
try:
with open("/etc/shadow", "r") as sa:
for line in sa:
row = line.split(":")
if row[0] == user:
crypt_pw = row[1]
_, crypt_type, crypt_str = row[1].split("$", 2)
break
salt = "${}${}".format(crypt_type, crypt_str)
res = crypt.crypt(password, salt)
if res == crypt_pw:
return True
else:
return False
except IOError as e:
if e.errno == 2:
print "ERROR: Please confirm if the system is linux!"
return
if e.errno == 13:
print "ERROR: need to be root,please confirm your permission!"
return
if __name__ == '__main__':
res = user_auth("root", "abcd1234")
if res == True or res == False:
print res