检查用户是否是root

发布于 2024-10-12 13:09:38 字数 289 浏览 4 评论 0原文

如何在 PHP 脚本中验证用户是否为 root 用户?命令是什么? 我尝试了类似的操作:

exec("su -l login < `echo password`");

但是 su 命令无法接收密码...

请注意,该机器与互联网隔离,因此如果需要,我可以以 root 身份运行 PHP。

编辑: 我不想知道运行脚本的当前用户是否是root。 我的 PHP 脚本中有一个用户列表,我想知道每次登录时他是否具有 root 权限。

How can i verify if a user is root in a PHP script ? What is the command ?
I tried something like that :

exec("su -l login < `echo password`");

but the su command can not receive password...

Note that the machine is isolated from internet so I can run PHP as root if needed.

EDIT:
I don't want to know if the current user who run the script is root or not.
I have a list of users in my PHP script and I want to know for each login if he has root privileges.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

旧伤还要旧人安 2024-10-19 13:09:38

posix_getuid() 怎么样? ?

if (0 == posix_getuid()) {
    echo "Running as root.";
} else {
    echo "not root";
}

How about posix_getuid()?

if (0 == posix_getuid()) {
    echo "Running as root.";
} else {
    echo "not root";
}
梦途 2024-10-19 13:09:38

首先,问问自己“具有 root 权限”的登录的确切定义是什么。 AFAICT 有 2 个基本解决方案。

系统管理员使用 uid 0 创建多个帐户的老式方式,我(而且我当然不是唯一这样做的人)认为这是一场噩梦。在这种情况下,您可以使用 posix_getpwnam 检查列表中的所有用户并查看他们的 uid 是否匹配 0。

以下代码片段就是这样做的,$privileged 将包含具有 root 权限的用户:

$logins = array('root', 'john', 'guest', 'foo');
$privileged = array();
foreach($logins as $login) {
    $userInfo = posix_getpwnam($login);
    if ($userInfo !== FALSE) {
        if ($userInfo['uid'] == 0) {
          $privileged[] = $login;
        }
    }
}

另一种(恕我直言,只有明智的)方法是将所有具有 root/管理权限的用户添加到特定组(wheeladmin 已在不同的 Linux 发行版中使用,找出哪一个适合您)。这种情况更简单,因为您可以使用 posix_getgrnam 来获取特定组中的所有成员。

以下代码片段将匹配您提供的登录名数组,并查看谁是具有特定权限的成员,$privileged 将再次包含结果(即列表中属于您指定组的成员的用户):

$logins = array('root', 'john', 'guest', 'foo');
$privileged = array();
$groupInfo = posix_getgrnam('admins');
if ($groupInfo !== FALSE) {
    $privileged = array_intersect($logins, $groupInfo['members']);
}

First, ask yourself what exactly defines a login "having root privileges". AFAICT there are 2 basic solutions.

The old-school way, where sysadmins create multiple accounts with uid 0, which I - and I'm certainly not alone in this - consider to be a nightmare. In this scenario you could check all users in your list using posix_getpwnam and see if their uid matches 0.

The following code snippet does just that, $privileged will contain the users with root privileges :

$logins = array('root', 'john', 'guest', 'foo');
$privileged = array();
foreach($logins as $login) {
    $userInfo = posix_getpwnam($login);
    if ($userInfo !== FALSE) {
        if ($userInfo['uid'] == 0) {
          $privileged[] = $login;
        }
    }
}

The other (and imho only sane) way to do this is to add all users with root/administrative privileges to a specific group (wheel or admin are already used in different Linux distributions, find out which one works for you). This scenario is even simpler, since you can use posix_getgrnam to fetch all members in a specific group.

The following code snippet will match an array of logins you provide, and see who's a member with specific privileges, again $privileged will contain the result (ie. the users in your list that are a member of the group you specified) :

$logins = array('root', 'john', 'guest', 'foo');
$privileged = array();
$groupInfo = posix_getgrnam('admins');
if ($groupInfo !== FALSE) {
    $privileged = array_intersect($logins, $groupInfo['members']);
}
伊面 2024-10-19 13:09:38

试试这个。

<?php
    if (posix_getuid() == 0){
        echo "This is root !";
        // add more for root
    } else {
        echo "This is non-root";
    }
?>

Try This .

<?php
    if (posix_getuid() == 0){
        echo "This is root !";
        // add more for root
    } else {
        echo "This is non-root";
    }
?>
抚你发端 2024-10-19 13:09:38

如果你的意思确实是“is root”,那么你可以检查 whoami,看看它是否出现“root”。

exec("whoami");

如果这是一个明智的做法,那就是另一个问题了;)

If you really mean "is root", then you can just check for whoami, and see if it comes up 'root'

exec("whoami");

If it's a smart thing to do is a different question ;)

娜些时光,永不杰束 2024-10-19 13:09:38

评论后重写

“我不想知道用户是否
运行脚本是否是root。我有
检查 PHP 脚本中的某些内容
登录名/密码(如果是 root 或)
不是”

我现在明白你需要在 PHP 中执行 root 身份验证

我建议使用 pwauth 而不是 whoami/login 等。

这里是一个关于如何从 PHP 使用它的示例,而我相信可以有更简单的测试方法。

Rewritten after your comment

"I don't want to know if the user who
run the script is root or no. I have
to check in a PHP script for some
login/password if they are root or
not"

I now understood that you need to perform a root authentication in PHP.

I suggest to use pwauth instead of whoami/login, etc.

Here is an example on how to use it from PHP, while I believe there can be a simpler way of testing.

野味少女 2024-10-19 13:09:38

如果您想查看某个系统用户(而不是Web服务用户)是否具有root权限,您可以使用posix_getpwnam() 函数:

<?php
    $u = posix_getpwnam("username");

    if ($u == FALSE) {
        print "no-such-user";
    } elseif ($u["uid"] == 0) {
        print "root";
    } else {
        print "non-root";
    };
?>

请记住,这仅检查用户名。如果您还需要检查密码,您实际上必须尝试通过系统进行身份验证。

If you want to see if a certain system user (as opposed to a web-service user) has root priviliedges, you can use the posix_getpwnam() function:

<?php
    $u = posix_getpwnam("username");

    if ($u == FALSE) {
        print "no-such-user";
    } elseif ($u["uid"] == 0) {
        print "root";
    } else {
        print "non-root";
    };
?>

Keep in mind that this only checks the username. If you also need to check the password you actually have to try to authenticate with the system.

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