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

Tim*_*mmm 7 sudo root

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

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

该程序使用 Qt,如果它只能在 Ubuntu 上运行,我会相当高兴,但我不想假设它们已经安装gksudo或安装了任何东西。我也无法使用SUID

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

但这看起来很老套——命令行前端通常是一个可怕的想法,应该不惜一切代价避免。那么有没有更好的办法呢?也许有一个 libsudo,或者某种使用 IPC 的方法?

注意:这不是重复的。或者至少,那里的答案没有将其视为我要问的问题。

nem*_*emo 2

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.
Run Code Online (Sandbox Code Playgroud)

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