首页 > 解决方案 > 如何以 www-data 的身份向其他用户发送 sudo 或 su

问题描述

我正在尝试设置一个运行脚本的网页。问题是,这些脚本必须使用另一个用户运行。这不是权限问题,我可以将脚本作为 www-data 物理运行,但脚本依赖于用户设置的整个框架和环境变量。

我希望向用户发送 sudo 或 su 并在之后运行脚本。

该网页调用一个运行的 php 文件:

$cmd1 = "sudo login -f otheruser";
$cmd2 = "whoami";
echo exec(escapeshellcmd($cmd1), $output, $status);
foreach($output as $line) echo "$line\n";
echo exec(escapeshellcmd($cmd2), $output, $status);
foreach($output as $line) echo "$line\n";

我已经尝试使用“sudo login -f otheruser”和“su - otheruser”并设置它以便从 www-data 登录到 otheruser 不需要密码(/etc/pam.d/su)

第一个命令什么都不返回,第二个返回: www-data 很明显它没有登录到其他用户。

关于如何做到这一点的任何想法,或者我如何以其他用户身份运行脚本的替代方案?

标签: phpsudouser-permissionssu

解决方案


编辑: 所以在看了这个之后,这是因为很多事情。

首先,让我说,最有可能比授予 Apache HTTPD sudoers 访问权限更好的解决方案。

其次,如果您的用户是www-data.

所以这里是如何使它工作......我首先查看的是日志文件。journalctl -u apache2 -f尾随 httpd 日志文件。你会看到很多这样的条目:

Nov 02 15:27:17 ubuntu sudo[23541]: pam_unix(sudo:auth): conversation failed
Nov 02 15:27:17 ubuntu sudo[23541]: pam_unix(sudo:auth): auth could not identify password for [www-data]
Nov 02 15:27:17 ubuntu sudo[23541]: www-data : command not allowed ; TTY=unknown ; PWD=/var/www/html ; USER=postgres ; COMMAND=/var/www/html/test2.sh

因此,让我们首先创建几个文件进行测试。我们将以尽可能确保这一点的心态进入这一点。因此,我将创建两个测试文件,一个我们想要授予执行权限,一个我们不授予执行权限。

cd /var/www/html
echo -e "#\x21/bin/bash\necho \"Running command: \$0\"\n"  | tee test.sh test2.sh
chown postgres:postgres test*.sh
chmod 700 test*.sh

现在我们要让www-data用户访问sudo. 但是,我们只希望它能够执行命令/var/www/html/test.sh而不是/var/www/html/test2.sh. 因此,让我们创建以下文件:

www-data ALL=(ALL) NOPASSWD:/var/www/html/test.sh
echo 'www-data ALL=(ALL) NOPASSWD:/var/www/html/test.sh' > /etc/sudoers.d/www-data

我稍微更新了您的示例 PHP 脚本以提供更多输出:

<?php
$cmd1 = "sudo -u postgres /var/www/html/test.sh";
$cmd2 = "sudo -u postgres /var/www/html/test2.sh";
$cmd3 = "whoami";
echo "Executing command 1: $cmd1\n";
exec(escapeshellcmd($cmd1), $output, $status);
foreach($output as $line) echo "$line\n";
unset($output);
echo "\nExecuting command 2: $cmd2\n";
exec(escapeshellcmd($cmd2), $output, $status);
foreach($output as $line) echo "$line\n";
unset($output);
echo "\nExecuting command 3: $cmd3\n";
exec(escapeshellcmd($cmd3), $output, $status);
foreach($output as $line) echo "$line\n";
?>

现在,我得到以下信息:

$ curl localhost
Executing command 1: sudo -u postgres /var/www/html/test.sh
Running command: /var/www/html/test.sh

Executing command 2: sudo -u postgres /var/www/html/test2.sh

Executing command 3: whoami
www-data

命令 2 不生成任何输出,因为 www-data 用户没有执行它的权限。您应该在日志条目中看到以下内容:

Nov 02 15:27:17 ubuntu sudo[23541]: pam_unix(sudo:auth): conversation failed
Nov 02 15:27:17 ubuntu sudo[23541]: pam_unix(sudo:auth): auth could not identify password for [www-data]
Nov 02 15:27:17 ubuntu sudo[23541]: www-data : command not allowed ; TTY=unknown ; PWD=/var/www/html ; USER=postgres ; COMMAND=/var/www/html/test2.sh

原答案:

我使用sudo su -c并且能够获得预期的输出。请参阅以下内容:

$cmd1 = "sudo su -c 'whoami' postgres";
$cmd2 = "whoami";
echo exec(escapeshellcmd($cmd1), $output, $status);
foreach($output as $line) echo "$line\n";
echo exec(escapeshellcmd($cmd2), $output, $status);
foreach($output as $line) echo "$line\n";

输出

$ ./test.php
postgrespostgres
myuserpostgres
myuser

推荐阅读