在 Linux 中以编程方式请求提升权限
(此问题具有相同的标题,但问题正文从脚本角度询问它,例如 su -c
,不要将其欺骗)
我有一个 Qt GUI 应用程序,需要基于 /etc 执行一些文件操作用户输入。一种选择可能是将 system()
与 sudo
一起使用,但即使这样也需要在某些时候弄乱 sudoers
文件。我也不想使用 system()
加脚本 hack 来修改文件,而是进行正确的文件操作。
以编程方式提升我的应用程序权限以执行此操作的最佳方式是什么?
编辑:作为奖励,如果它也适用于 Maemo/Meego/其他手持设备,那就太好了(据我所知,PolicyKit 不是那里不可用..)
(This question has identical title, but question body asks it in scripting point of view, e.g. su -c
, don't dupe this to that)
I have a Qt GUI app that needs to perform some file operations in /etc based on user input. One option would probably to use system()
with sudo
, but even that requires messing with sudoers
file in some point. I also would like not to do system()
plus script hacks to modify the files, but proper file operations.
What is the best way to programmatically elevate my applications rights to do this?
Edit: as a bonus, it'd be nice if it would work on Maemo/Meego/other handhelds too (afaik PolicyKit isn't available there..)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会完全编写一个单独的程序。与这个哲学类似。基本上 - 编写一个简单的程序来完全满足您的需要,并通过文件系统上的文件权限控制其行为。主要是,
和,
I would write a separate program altogether. Something along the lines of this philosophy. Basically - write a simple program that does exactly what you need, and control its behaviour with file permissions on the filesystem. Mainly,
And,
您可以使用PolicyKit,它正在逐渐取代gksu/su/sudo,特别是在Ubuntu上,因为它提高了操作而不是整个程序,因此具有更高的安全性和细粒度的控制。
You could use PolicyKit, which is gradually superseding gksu/su/sudo, especially on Ubuntu, for its higher security and fine-grained control because of elevating actions, not the whole program.
创建一个帮助器setuid 程序,它只执行您想做的事情,并且fork/exec 您的应用程序。然后删除子进程中的权限。两个应用程序都可以通过管道、套接字或类似的东西进行通信。
请记住,setuid 程序存在安全风险,因此在实施该程序时应非常小心。
Create a helper setuid program that does only the things you want to do, and fork / exec your application from it. Then drop privileges in the child process. Both applications could communicate over pipes, sockets, or something like that.
Have in mind that setuid programs are a security risk and so you should be very careful when implementing one.