【发布时间】:2009-09-22 12:05:26
【问题描述】:
如果本地 unix 用户传递的用户名和密码正确,我想检查一个 shell 脚本。最简单的方法是什么?
我在谷歌搜索时发现的唯一一件事是使用“expect”和“su”,然后以某种方式检查“su”是否成功。
【问题讨论】:
-
更多关于你想要完成什么以及你的脚本正在做什么的细节可能会有所帮助。
如果本地 unix 用户传递的用户名和密码正确,我想检查一个 shell 脚本。最简单的方法是什么?
我在谷歌搜索时发现的唯一一件事是使用“expect”和“su”,然后以某种方式检查“su”是否成功。
【问题讨论】:
用户名和密码写在/etc/shadow 文件中。
只需从那里获取用户和密码散列(sed 会有所帮助),散列您自己的密码并检查。
使用 mkpasswd 生成哈希。
您必须查看您的版本使用的是哪种盐。最新的影子正在使用sha-512 所以:
mkpasswd -m sha-512 password salt
手册页可以为您提供很多帮助。
更容易使用 php 和 pam-aut 模块。在那里你可以在组访问密码用户上查看 vie php。
【讨论】:
/etc/shadow时这不起作用
好的,现在这是我用来解决问题的脚本。我首先尝试按照 Aaron Digulla 的建议编写一个小型 c 程序,但事实证明这太难了。
也许这个脚本对其他人有用。
#!/bin/bash
#
# login.sh $USERNAME $PASSWORD
#this script doesn't work if it is run as root, since then we don't have to specify a pw for 'su'
if [ $(id -u) -eq 0 ]; then
echo "This script can't be run as root." 1>&2
exit 1
fi
if [ ! $# -eq 2 ]; then
echo "Wrong Number of Arguments (expected 2, got $#)" 1>&2
exit 1
fi
USERNAME=$1
PASSWORD=$2
# Setting the language to English for the expected "Password:" string, see http://askubuntu.com/a/264709/18014
export LC_ALL=C
#since we use expect inside a bash-script, we have to escape tcl-$.
expect << EOF
spawn su $USERNAME -c "exit"
expect "Password:"
send "$PASSWORD\r"
#expect eof
set wait_result [wait]
# check if it is an OS error or a return code from our command
# index 2 should be -1 for OS erro, 0 for command return code
if {[lindex \$wait_result 2] == 0} {
exit [lindex \$wait_result 3]
}
else {
exit 1
}
EOF
【讨论】:
export LC_ALL=C 会使其适用于具有不同默认语言的系统,它会将此脚本执行的语言设置为英语,因为它需要字符串@ 987654324@,见askubuntu.com/a/264709/18014。
在 Linux 上,您将需要编写一个调用 pam_authenticate() 的小型 C 程序。如果调用返回PAM_SUCCESS,则登录名和密码正确。
【讨论】:
部分答案是检查用户名,它是否在 /etc 的 passwd/shadow 文件中定义 然后用盐计算密码MD5。如果您通过 SSL(或至少某些服务器终端服务)发送用户密码。
这只是一个提示,因为我不知道你实际上需要什么。 因为“su”主要用于身份验证。
您可能会关注的其他主题是 kerberos/LDAP 服务,但这些都是很难的主题。
【讨论】: