gpt4 book ai didi

linux - Qt,Linux,检查给定用户是否有sudo权限

转载 作者:太空狗 更新时间:2023-10-29 11:10:20 27 4
gpt4 key购买 nike

我正在运行 Fedora 17 KDE x64 和 Qt 4.8.1。

与 Ubuntu 不同的是,Fedora 没有给第一个创建的用户 sudo 权限,也没有将第一个创建的用户添加到 /etc/sudoers 文件中。因此,在 Fedora 17 KDE(尚未测试 Gnome 等自旋)上安装程序时,它需要 root(不是 sudo)权限。所以,我们有三级权限(按照权限级别降序排列):

1) 根2)须藤3) 用户

在 Fedora 17 KDE 中,如果您有权访问 root 用户帐户,您可以通过编辑 /etc 将 sudo 权限授予您想要的任何其他用户/sudoers 文件并添加行:

user ALL = (ALL) ALL

...行下方:

root ALL = (ALL) ALL

user 替换为您希望授予 sudo 访问权限的帐户名称。

但并不是每个用户都可以访问 root 用户的帐户。这就是为什么 root 用户可以将 super 用户 (sudo) 权限授予某些用户帐户的原因。

我想做的是检查运行该应用程序的当前用户是否已注册为 super 用户。如果是这样,我将使 /usr/bin/kdesu 工具使用 /usr/bin/sudo 工具,该工具要求输入 sudo 密码。

如果用户不是 super 用户,我会保留 /usr/bin/kdesu 默认情况下的行为——它使用 /usr/bin/su 工具这需要 root 密码。

目前,我正在使用 getenv('USER')(Windows 上的“USERNAME”,但我只需要在 Linux 上使用此功能)来获取当前用户。当前用户的名称可以通过遍历 QProcess::systemEnvironment() 获取,其中列出了 HOSTNAME 和 USER 变量。

解析 /etc/sudoers 文件不是一个选项,因为打开该文件需要 sudoroot 权限。

最佳答案

   man sudo
[...]
-l[l] [command]
If no command is specified, the -l (list)
option will list the allowed (and forbidden)
commands for the invoking user (or the user
specified by the -U option) on the current
host. If a command is specified and is
permitted by sudoers, the fully-qualified
path to the command is displayed along with
any command line arguments. If command is
specified but not allowed, sudo will exit
with a status value of 1. If the -l option
is specified with an l argument (i.e. -ll),
or if -l is specified multiple times, a
longer list format is used.

更新 您需要一个(伪)终端才能运行sudo。这是一种方法:

#include <pty.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>

int main (int argc, char* argv[])
{

int status, master;
pid_t respid, pid = forkpty (&master, 0, 0, 0);
if (pid == 0) {
/* we are child */
argv[0] = "/usr/bin/sudo"; /* I know it's a sin... just for a demo */
execve("/usr/bin/sudo", argv, 0);
}
else if (pid > 0) {
/* we are parent */
respid = waitpid(pid, &status, 0);
fprintf (stderr, "sudo exited with status %d\n",
WEXITSTATUS(status));
}
}
else {
fprintf (stderr, "could not forkpty\n");
}
}

运行此命令:例如 runsudo -lrunsudo -l/foo/bar/baz

关于linux - Qt,Linux,检查给定用户是否有sudo权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11281503/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com