我正在编写一个小型实用程序。sudo如果需要,我希望它尝试运行某些东西。
也就是说:如果文件权限不允许当前用户对特定文件进行操作(并且sudo规则允许),我希望我的实用程序以sudo文件所有者的身份运行某些内容。
我希望事先检查这种能力,因为我希望系统日志不会被失败的sudo尝试所带来的噪音填满。正如sudo它自己在失败时报告的那样:“将报告此事件”。
所以,我希望以编程方式检查:可以通过?user <x>运行。command <y>sudo
问题是:虽然/etc/sudoers包含该映射,但它是 root 拥有的,普通用户无法读取。
我正在考虑生成一个子进程来运行sudo -l(它输出当前用户可以 sudo 运行的命令)。然后我会解析这个的输出。然而,这似乎有点脆弱。输出包含我想要的信息,但它似乎是为人类阅读而设计的(而不是用于程序化消费)。我不知道是否有任何保证将来输出或跨平台输出将遵循相同的格式。
sudo -l输出的编程解析被认为是安全的吗?如果没有,是否有更好的选择来提前确定 sudo 命令是否会成功?
(X/Y 的背景:此实用程序供受限访问角色帐户使用。我希望其他一些用户能够有效地选择允许受限访问帐户通过 sudo 规则操作他们的文件。但是,我赢了不知道哪些其他用户有相关的 sudo 规则)
Arc*_*mar 13
根据 sudo 手册页:
-l, --list If no command is specified, list the allowed (and forbidden)
commands for the invoking user (or the user specified by the
-U option) on the current host. A longer list format is used
if this option is specified multiple times and the security
policy supports a verbose output format.
If a command is specified and is permitted by the security
policy, 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.
Run Code Online (Sandbox Code Playgroud)
所以这将归结为
if sudo -l -U $USER shutdown > /dev/null
then
## $USER can
else
## $USER cannot
fi
Run Code Online (Sandbox Code Playgroud)
正如穆鲁,兼用指出,-U $USER或-U $USERTOTEST有或全无,这取决于你的需要。
| 归档时间: |
|
| 查看次数: |
570 次 |
| 最近记录: |