/
我需要知道如何检查给定用户的有效文件访问权限,但从目标文件或目录开始、检查组等手动执行此操作需要很长时间。
答案1
据我所知,唯一的方法是要么执行您所描述的操作,然后根据有效用户/组检查每个权限集。或者您可以尝试设置 sudo 以便能够执行test(1)
.
sudo -u luser test -x ~juser/bin/myprogram
就像你说的,检查有效的用户/组权限:
:
# called as $0 usertocheck pathname {r|w|x}
# for example, permcheck luser ~juser/bin/myprogrm x
# displays either "root", "user", "groups", "other" or "none"
user=$1
file=$2
smode=$3
# if user has no access from state, an empty string is returned, fuid,
# fgid and fmode would become empty strings as well; the end result is
# always showing 'none' even if $user has access (except $user == 'root')
set -- $(stat -L -c '%u %g %a' $file 2>&-)
awk -f $tmpawk \
-veuid="$(id -u $user)" \
-vgrp="$(id -G $user)" \
-vfuid="$1" \
-vfgid="$2" \
-vfmode="$(echo ibase=8\;$3 | bc)" \
-vsmode="$smode" \
'BEGIN {
if (euid == 0) { print "root"; exit; }
split(grp,Groups);
omode = fmode % 8; gmode = int(fmode / 8 % 8);
umode = int(fmode / 64 % 8);
# set up tests
# these could be function, but not all version of awk has a function
# statement
if (smode == "r") {
utest = int(umode / 4);
gtest = int(gmode / 4);
otest = int(omode / 4);
}
if (smode == "w") {
utest = int(umode / 2 % 2);
gtest = int(gmode / 2 % 2);
otest = int(omode / 2 % 2);
}
if (smode == "x") {
utest = (int(umode >= 4) && umode % 2);
gtest = (int(gmode >= 4) && gmode % 2);
otest = (int(omode >= 4) && omode % 2);
}
if (utest && fuid == euid) { print "user"; exit; }
for (idx in Groups) {
if (gtest && Groups[idx] == fgid) { print "group"; exit; }
}
if (otest) { print "other"; exit; }
print "none";
}
'
在我的 Ubuntu 11.04 系统上,运行此脚本平均需要大约 16 毫秒。此外,stat 不需要读取/执行
答案2
该stat
命令会打勾吗?