Qt,Linux,检查给定用户是否具有 sudo 权限
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11281503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Qt, Linux, check if a given user has sudo privileges
提问by Ivan
I am running Fedora 17 KDE x64 and Qt 4.8.1.
我正在运行 Fedora 17 KDE x64 和 Qt 4.8.1。
In contrast with Ubuntu, Fedora doesn't give the first created user sudo privileges and it doesn't add the first created user to the /etc/sudoers
file. Therefore when installing programs on Fedora 17 KDE (haven't tested the Gnome and etc. spins) it requires root
(not sudo
) privileges. So, we have three levels of privileges (listed in descending order according to the level of privileges):
与 Ubuntu 不同的是,Fedora 不会授予第一个创建的用户 sudo 权限,也不会将第一个创建的用户添加到/etc/sudoers
文件中。因此,在 Fedora 17 KDE 上安装程序时(尚未测试 Gnome 等),它需要root
(不是sudo
)特权。所以,我们有三级权限(按权限级别降序排列):
1) root 2) sudo 3) user
1) 根 2) 须藤 3) 用户
In Fedora 17 KDE if you have access to the root
user account you can give sudo
privileges to any other user you want simply by editing the /etc/sudoers
file and adding the line:
在 Fedora 17 KDE 中,如果您有权访问root
用户帐户,则可以sudo
通过编辑/etc/sudoers
文件并添加以下行来为您想要的任何其他用户授予权限:
user ALL = (ALL) ALL
… below the line:
... 线下:
root ALL = (ALL) ALL
Replace user
with the name of the account you wish to give sudo access to.
替换user
为您希望授予 sudo 访问权限的帐户的名称。
But not every user has access to the root
user's account. That's why the root
user could give super-user (sudo
) privileges to some user accounts.
但并非每个用户都可以访问root
用户的帐户。这就是root
用户可以sudo
为某些用户帐户授予超级用户 ( ) 权限的原因。
What I want to to is to check if the current user running the application is registered as super-user. If so I will make the /usr/bin/kdesu
tool use the /usr/bin/sudo
tool which asks for sudo
password.
我想要的是检查运行该应用程序的当前用户是否已注册为超级用户。如果是这样,我将使该/usr/bin/kdesu
工具使用/usr/bin/sudo
要求输入sudo
密码的工具。
If the user is not super-user I leave /usr/bin/kdesu
behave as it does by default – it uses the /usr/bin/su
tool that requires root
password.
如果用户不是超级用户,我会保持/usr/bin/kdesu
默认行为 - 它使用/usr/bin/su
需要root
密码的工具。
Currently, I am using getenv('USER')
(“USERNAME” on Windows but I need this functionality on Linux only) to get the current user. The current user's name could be acquired by traversing QProcess::systemEnvironment()
where HOSTNAME and USER variables are listed.
目前,我正在使用getenv('USER')
(Windows 上的“USERNAME”,但我只在 Linux 上需要此功能)来获取当前用户。可以通过遍历QProcess::systemEnvironment()
列出 HOSTNAME 和 USER 变量的位置来获取当前用户的名称。
Parsing the /etc/sudoers
file is not an option since opening the file requires sudo
or root
privileges.
解析/etc/sudoers
文件不是一个选项,因为打开文件需要sudo
或root
特权。
回答by n. 'pronouns' m.
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.
UpdateYou need a (pseudo)terminal in order to run sudo
. Here's one way to do it:
更新您需要一个(伪)终端才能运行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");
}
}
Run this: runsudo -l
and runsudo -l /foo/bar/baz
for example.
运行此:runsudo -l
和runsudo -l /foo/bar/baz
的例子。