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

前端之家收集整理的这篇文章主要介绍了shell – 如何知道给定用户是否具有给定路径的读取和/或写入权限前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我应该首先说我认为自己是一个熟练的用户.但是今天我需要自动化这个并且被困住了.

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

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

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

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

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

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

@H_403_16@
给我一个脚本专家!
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
}
原文链接:https://www.f2er.com/bash/384253.html

猜你在找的Bash相关文章