我的 C/C++ 怎么办?应用程序确定root用户是否正在执行命令?
我正在编写一个需要 root 用户权限才能执行的应用程序。如果由非 root 用户执行,它将退出并终止并显示错误消息,例如:
pthread_getschedparam: Operation not permitted
我想让应用程序对用户更加友好。作为其早期初始化的一部分,我希望它检查它是否由 root 执行。如果不是 root,它会显示一条消息,指示它只能由 root 运行,然后终止。
I am writing an application that requires root user privileges to execute. If executed by a non root user, it exits and terminates with a perror message such as:
pthread_getschedparam: Operation not permitted
I would like to make the application more user friendly. As part of its early initialization I would like it to check if it is being executed by root or not. And if not root, it would present a message indicating that it can only be run by root, and then terminate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
getuid
或geteuid
将是显而易见的选择。getuid
检查实际用户的凭据。geteuid
中添加的e
代表effective
。它检查有效凭证。举例来说,如果您使用 sudo 以 root(超级用户)身份运行程序,您的实际凭据仍然是您自己的帐户,但您的有效凭据是 root 帐户(或某个组织的成员)的凭据。轮组等)
例如,考虑这样的代码:
如果正常运行,
getuid()
和geteuid()
将返回相同的值,因此它'会说“以自我身份运行”。如果您执行sudo ./a.out
,getuid()
仍会返回您的用户 ID,但geteuid()
将返回凭据对于root或wheel,所以它会说“以其他人的身份运行”。getuid
orgeteuid
would be the obvious choices.getuid
checks the credentials of the actual user.The added
e
ingeteuid
stands foreffective
. It checks the effective credentials.Just for example, if you use
sudo
to run a program as root (superuser), your actual credentials are still your own account, but your effective credentials are those of the root account (or a member of the wheel group, etc.)For example, consider code like this:
If you run this normally,
getuid()
andgeteuid()
will return the same value, so it'll say "running as self". If you dosudo ./a.out
instead,getuid()
will still return your user ID, butgeteuid()
will return the credentials for root or wheel, so it'll say "Running as somebody else".我建议不要进行此更改,而是改进您的错误消息。您的应用程序是否真的需要“root”是值得怀疑的;相反,它需要 root 拥有的某些特权,但具有细粒度安全控制的操作系统可能能够向应用程序授予权限,而无需授予其完全的 root 访问权限。即使现在不可能,6 个月或 2 年后也可能是可能的,如果您的程序拒绝基于权限模型的向后假设运行而不是仅仅检查它是否成功执行,用户将会感到恼火。它需要的特权操作。
I would recommend NOT making this change, but instead improving your error message. It's doubtful that your application actually needs to "be root"; instead it needs certain privileges which root has, but which operating systems with fine-grained security controls might be able to grant to the application without giving it full root access. Even if that's not possible now, it may be possible 6 months or 2 years from now, and users are going to be irritated if your program is refusing to run based on backwards assumptions about the permission model rather than just checking that it succeeds in performing the privileged operations it needs to.
您真正想要检查的是您是否拥有正确的功能集(
CAP_SYS_NICE
我认为这是您需要的功能),请参阅手册页功能 (7)
和capget (2)
这样,如果您有能力做您想做的事情,但您不是 root,则不会出错。What you really want to check for is if you have the right capability set (
CAP_SYS_NICE
I think is the capability you need) see man pagescapabilities (7)
andcapget (2)
this way it won't error out if you have the ability to do what you want, but you aren't root.