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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:11:33  来源:igfitidea点击:

Qt, Linux, check if a given user has sudo privileges

linuxqtqt4sudosudoers

提问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/sudoersfile. 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 rootuser account you can give sudoprivileges to any other user you want simply by editing the /etc/sudoersfile and adding the line:

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

user ALL = (ALL) ALL

… below the line:

... 线下:

root ALL = (ALL) ALL

Replace userwith the name of the account you wish to give sudo access to.

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

But not every user has access to the rootuser's account. That's why the rootuser 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/kdesutool use the /usr/bin/sudotool which asks for sudopassword.

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

If the user is not super-user I leave /usr/bin/kdesubehave as it does by default – it uses the /usr/bin/sutool that requires rootpassword.

如果用户不是超级用户,我会保持/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/sudoersfile is not an option since opening the file requires sudoor rootprivileges.

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

回答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 -land runsudo -l /foo/bar/bazfor example.

运行此:runsudo -lrunsudo -l /foo/bar/baz的例子。