如何知道给定用户是否具有给定路径的读取和/或写入权限

Edu*_*duo 8 unix shell directory-permissions user-permissions

我应该首先说我认为自己是一个熟练的用户.但是今天我需要自动化这个并且被困住了.

让我们假设我是root用户,因此我能够遍历整个文件系统,但我不能运行"sudo"或"su".

我有一个给定的用户和一个给定的路径.

如何通过CLI检查用户是否能够读取和/或写入路径?

我知道这听起来很容易,但请记住:

  • 我们不是,也不能成为用户.
  • 我们不能仅仅依赖最终目录权限,因为路径上方的权限可能阻止访问最终目录.
  • 在这个继承组的时代,我们不能仅仅依赖目录组权限.

我假设这不能通过任何命令完成,我需要首先收集所有用户组,然后遍历路径的整个层次结构,检查路径上的读取权限,然后读取和写入最终目录.听起来非常昂贵.

kwo*_*orr 3

标记我为脚本大师!

check_access() {
  checked_file=$1
  target_user=$2
  result=PASS

  groups=`id -G $target_user | sed -e 's| | -o -group |g' -e 's|^|\\( -group |' -e 's|$| \\)|'`

  while [ $checked_file != / ]; do 
    find $checked_file -maxdepth 0 \
      -type f \( \
        \( -user $target_user -perm 0400 \) \
        -o \( $groups -perm 0040 \) \
        -o -perm 0004 \
      \) -o -type d \( \
        \( -user $target_user -perm 0100 \) \
        -o \( $groups -perm 0010 \) \
        -o -perm 0001 \
      \) >/dev/null 2>&1 || result=FAIL
    checked_file=`dirname $checked_file`
  done
  echo $result
}
Run Code Online (Sandbox Code Playgroud)