更改用户 PHP
我需要在运行时更改 PHP 脚本的用户。我查看了 posix_setuid,但它看起来不安全并且需要 root 权限。更好的方法是使用用户密码更改脚本的用户 ID(例如 posix_setuid($username, $password)),并避免以 root 身份运行脚本。
我对其他方法持开放态度,并且脚本不一定需要是 PHP。然而,它将被 apache 调用。
该场景的一个很好的类比是 cPanel su 如何在其文件管理器上显示当前登录的用户。
我需要更改用户,因为我正在为多用户设置创建文件管理器。目前,我已经设置了文件管理器,以便 apache 以 root 身份为我的 PHP 文件管理器提供服务。然而,这是不合理的,因为如果代码中存在安全错误,一个用户可以编辑整个服务器上的文件。
我正在寻找一种将脚本 SU 给登录用户的方法,以便在出现安全缺陷的情况下,用户只能访问自己的文件。
I need to change the user of a PHP script at runtime. I've looked at posix_setuid
, but it looks unsecure and requires root privaledges. What would be preferable is changing the user id of the script with the users password (something like posix_setuid($username, $password)
), and avoiding running the script as root.
I'm open to other methods, and the script doesn't necessarily need to be PHP. However, it is going to be called from apache.
A good anology to the scenario would be how cPanel su's to the currently logged in user on it's file manager.
I need to change the user because I'm creating a file manager for a multi-user setup. Currently, I have the file manager set up so that apache serves my PHP file manager as root. However, this is not reasonable, because in case of a security bug in the code, one user can edit files on the entire server.
I'm looking for a way to SU the script to the logged in user so that in case of a security flaw, the user is only restricted to their own files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不久前需要这样做。我基本上创建了一个 bash 脚本,它访问只有 root 有权访问的资源。这就是我所做的:
使用
visudo
命令更改您的/etc/sudoers
:我添加了允许
apache
用户运行的行/sbin/myscript.sh
使用 sudo 命令,无需输入密码。之后,您可以将以下内容放入您的 php 文件中以获得输出:
I needed to do this a while ago. I basically created a bash script which accessed resources that only root had access to. Here's what I did:
Use the
visudo
command to change your/etc/sudoers
:I have added the line which allows the
apache
user to run/sbin/myscript.sh
with the sudo command without entering the password.After that, you can just put the following in your php file to get the output:
这个最近的问题< /a> 讨论使用 PHP 执行此操作的两个选项:
This recent question discusses two options for doing this with PHP: