Filesystems

檢查另一個使用者的文件訪問權限的命令

  • January 12, 2013

我需要知道如何檢查給定使用者的有效文件訪問權限,但是從/目標文件或目錄、檢查組等開始手動執行此操作需要很長時間。

據我所知,唯一的方法是按照您的描述進行操作,檢查每個權限集與有效使用者/組。或者您可以嘗試設置 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 不需要每次讀取/執行

引用自:https://unix.stackexchange.com/questions/21399