更改用户 PHP

发布于 2024-11-06 06:39:01 字数 438 浏览 3 评论 0原文

我需要在运行时更改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森末i 2024-11-13 06:39:01

我不久前需要这样做。我基本上创建了一个 bash 脚本,它访问只有 root 有权访问的资源。这就是我所做的:

使用 visudo 命令更改您的 /etc/sudoers

## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
apache ALL= NOPASSWD: /sbin/myscript.sh

我添加了允许 apache 用户运行的行/sbin/myscript.sh 使用 sudo 命令,无需输入密码。

之后,您可以将以下内容放入您的 php 文件中以获得输出:

$output = shell_exec("sudo /sbin/myscript.sh");

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:

## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
apache ALL= NOPASSWD: /sbin/myscript.sh

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:

$output = shell_exec("sudo /sbin/myscript.sh");
小苏打饼 2024-11-13 06:39:01

This recent question discusses two options for doing this with PHP:

  • PHP + FastCGI with suexec
  • suPHP
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文