如何以编程方式使用sudo?即将sudo集成到我的GUI中

时间:2013-03-02 00:31:30

标签: root sudo

我正在编写一个程序,在第一次运行它以执行配置更改时暂时需要root(在/etc中创建一个文件)。

如何通过在图形对话框中询问用户的密码来暂时获得root权限?

该程序使用Qt,如果它仅适用于Ubuntu,我感到相当高兴,但我不想假设它们已安装gksudo或其他任何东西。我也不能使用SUID

我唯一能想到的就是提供我自己的密码对话框,并通过sudo(或其不太不安全的堂兄弟)将其提供给命令行system()二进制文件。

但这看起来很糟糕 - 命令行前端通常是一个非常糟糕的主意,应该不惜一切代价避免。那么还有更好的方法吗?也许有一个libsudo,或者某种使用IPC的方法?

注意:This is not a duplicate.或者至少,那里的答案不会将其视为我提出的问题。

1 个答案:

答案 0 :(得分:0)

来自man sudo

   -A          Normally, if sudo requires a password, it will read it from the
               user's terminal.  If the -A (askpass) option is specified, a
               (possibly graphical) helper program is executed to read the user's
               password and output the password to the standard output.  If the
               SUDO_ASKPASS environment variable is set, it specifies the path to
               the helper program.  Otherwise, if /etc/sudo.conf contains a line
               specifying the askpass program, that value will be used.  For
               example:

                   # Path to askpass helper program
                   Path askpass /usr/X11R6/bin/ssh-askpass

               If no askpass program is available, sudo will exit with an error.

您可以使用安装在许多系统上的ssh-askpass,也可以编写自己的密码提示命令然后将其提供给sudo。这仍然有点笨拙,但是您不必担心将密码传递给sudo。

相关问题