qt编程获取linux管理员权限,Qt,Linux,检查给定用户是否具有sudo特权

我正在运行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 / sudoers文件并添加以下行,即可将sudo特权授予所需的任何其他用户:

user ALL = (ALL) ALL

……在这行之下:

root ALL = (ALL) ALL

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

但是,并非每个用户都可以访问root用户的帐户.因此,root用户可以为某些用户帐户授予超级用户(sudo)特权.

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

如果用户不是超级用户,那么我将默认情况下保留/usr/bin/kdesu的行为-它使用需要root密码的/usr/bin/su工具.

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

不能解析/ etc / sudoers文件,因为打开文件需要sudo或root特权.

解决方法:

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

#include

#include

#include

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 -l和runsudo -l / foo / bar / baz.

标签:qt4,sudo,sudoers,linux,qt

来源: https://codeday.me/bug/20191201/2078427.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值