【发布时间】:2014-06-10 17:12:13
【问题描述】:
有没有办法从命令行根据 Apache 提供的工具 htpasswd 创建的文件检查用户和密码?
【问题讨论】:
有没有办法从命令行根据 Apache 提供的工具 htpasswd 创建的文件检查用户和密码?
【问题讨论】:
您可以为此使用htpasswd 工具。
# create htpasswd_file with user:password
$ htpasswd -cb htpasswd_file user password
Adding password for user user
# verify password for user
$ htpasswd -vb htpasswd_file user wrongpassword
password verification failed
$ htpasswd -vb htpasswd_file user password
Password for user user correct.
退出状态为 0 表示成功,3 表示失败。
【讨论】:
-b 并在提示中输入密码通常更安全。使用上面的命令,明文密码可能会出现在你的.bash_history中。
假设您使用以下命令创建密码,并将“myPassword”作为密码
htpasswd -c /usr/local/apache/passwd/passwords username
这将创建一个看起来像这样的文件
username:$apr1$sr15veBe$cwxJZHTVLHBkZKUoTHV.k.
$apr1$ 是哈希方法,sr15veBe 是盐,最后一个字符串是哈希密码。您可以使用 openssl 使用
对其进行验证openssl passwd -apr1 -salt sr15veBe myPassword
哪个会输出
$apr1$sr15veBe$cwxJZHTVLHBkZKUoTHV.k.
您可以使用的管道是:
username="something"
htpasswd -c /usr/local/apache/passwd/passwords $username
****Enter password:****
salt=$($(cat passwords | cut -d$ -f3)
password=$(openssl passwd -apr1 -salt $salt)
****Enter password:****
grep -q $username:$password passwords
if [ $? -eq 0 ]
then echo "password is valid"
else
echo "password is invalid"
fi
您可能需要更改您的 openssl 命令,因为 Apache 的 htpasswd 命令在每个系统上的加密方式略有不同。
有关更多信息,请访问 Apache 的主题页面http://httpd.apache.org/docs/2.2/misc/password_encryptions.html
【讨论】: