Rya*_*ggs 6 php passwords sudo pam
我有一个"简单"的问题:如何在PHP脚本中安全地更改用户密码,而不授予Apache root权限或引入其他疯狂的安全漏洞?
背景:CentOS 6,Apache 2.2.13,PHP 5.3.3
我知道pam_chpasswd()命令,它是PECL PAM库的一部分.但是,除非主机进程(httpd)具有对/ etc/shadow文件的读访问权限,否则此函数将失败.(坏想法!如果它需要如此高的权限,不确定这个库如何帮助......)
据我所知,理想的情况是让PHP调用shell脚本,使用'sudo -u [用户更改密码的用户名]'这将运行脚本"AS"用户,所以他应该有权使用改变自己的密码.并且sudo将要求用户发送其现有密码以进行身份验证,从而防止一个用户更改另一个用户的密码.
但是由于某种原因这不起作用......当用popen打开进程时,进程永远不会执行.我将shell脚本设置为将一些文本转储到/ tmp中的公共可写文件中.但它永远不会达到这一点.
$cmd = "/usr/bin/sudo -S -u$username /file_to_execute.sh";
$handle = popen ($cmd, "w"); // Open the process for writing
fwrite ($handle, "$current_password\n"); // Send the user's current password to sudo (-S option)
fwrite .... (write the username, current password, and new password, so the script can change it)
$result = pclose($handle);
Run Code Online (Sandbox Code Playgroud)
如果我访问这个php脚本(http://server/script.php),函数会立即失败,$ result = 1
如果我修改sudoers文件(visudo)并添加一行:
$Defaults:apache!requiretty
该脚本冻结约10秒钟,然后失败($ result = 1)
任何建议,非常感谢!