我正在运行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用户帐户,则只需编辑sudo文件并添加以下行,即可将/etc/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上需要此功能)来获取当前用户。可以通过遍历列出了HOSTNAME和USER变量的QProcess::systemEnvironment()来获取当前用户的名称。

不能解析/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/

10-10 21:36
查看更多