如果我有一个用户名和密码对,我如何在Linux系统中验证它们实际上是正确的?我知道我可以使用passwd来做这件事,但我想用C编程来做这件事。
我不需要root权限(所以读取卷影文件不是一个选项)。
谢谢。
发布于 2017-11-21 03:28:49
如果您使用的是PAM,则可以使用checkpassword-pam。
该手册有一个示例命令(带有调试),它应该给你一个很好的起点。
echo -e "username\0password\0timestamp\0" \
| checkpassword-pam -s SERVICE \
--debug --stdout -- /usr/bin/id 3<&0发布于 2021-08-20 13:56:31
这是一个用python代码检查密码的简单示例。不需要root权限。
#!/usr/bin/python3
# simple linux password checker with
# standard python
import os, pty
def check_pass(user, passw):
# returns: 0 if check ok
# 1 check failed
# 2 account locked
if type(passw) is str:
passw = passw.encode()
pid, fd = pty.fork()
# try to su a fake shell which returns '-c OK' on ok
if not pid:
# child
argv = ('su', '-c', 'OK', '-s', '/bin/echo', user)
os.execlp(argv[0], *argv)
return # SHOULD NEVER REACHED
okflg = False
locked = False
while True:
try:
data = os.read(fd, 1024)
##print('data:', data, flush=True)
except OSError:
break
if not data:
break
data = data.strip()
if data == b'Password:':
os.write(fd, passw + b'\r\n')
elif data.endswith(b'OK'):
okflg = True
break
elif data.find(b'locked') > -1:
# show that account is locked
locked = True
print(data, flush=True)
break
os.close(fd)
# check result from su and okflg
if (not os.waitpid(pid, 0)[1]) and okflg:
return 0
return 2 if locked else 1
if __name__ == '__main__':
print(check_pass('xx', 'yy'))https://stackoverflow.com/questions/47399530
复制相似问题