【问题标题】:How can I check if permissions are not more permissible than a certain level in multiple files?如何检查权限是否不超过多个文件中的某个级别?
【发布时间】:2021-08-10 05:20:56
【问题描述】:

我想确保 /etc/file1 和 /etc/file2 不超过 644。我已经尝试过

if [[ `stat /etc/file1 --format=%a | cut -b 1` -le "6" -a `stat /etc/file2 --format=%a|cut -b 1` -le 6 ]]; then echo "good"; else echo "bad"; fi;

但我得到“bash:条件表达式中的语法错误 bash: `-a' 附近的语法错误"

我的上述计划只是重复 3 次(每个权限字节一个)。

我也不确定任何一个测试是否正常工作,因为我无法让它们在测试之外运行。

【问题讨论】:

  • “比 666 更允许”只是意味着“设置了执行位”不是吗?
  • 我想是的。我需要的实际数字是 644。我会改变它以反映这一点。

标签: linux bash centos


【解决方案1】:

我假设“更宽松”是指 file1 或 file2 具有超过 644 的任何额外权限。

一些例子是:

  • 664 更宽松,因为“组”现在可以写入文件。
  • 700 比 644 更宽松,因为即使“组”和“其他”拥有较少的权限,“所有者”拥有更多
  • 640 不那么宽松,因为“其他”无法再读取,之前可以

这基本上意味着与 644 相比,在 file1 或 file2 中至少设置了一个额外的位。因此,如果您将 644 和 file1 权限与,结果是两者共有的位。

假设 file1 是 640。644 AND 640 将导致 640。因为结果与 file1 的权限相同,所以您知道 file1 的权限至少是 644 的子集。

说file2是700。644 AND 700会得到600。600和700不一样,所以file2中必须有644没有的位设置(即不同的权限)。

#!/bin/bash

BASEPERM=644

for FILE in /etc/file1 /etc/file2; do
        COMPARISON=$(stat -c '%a' "$FILE")
        RESULT=$(printf '%o' $((8#$BASEPERM & 8#$COMPARISON))) # AND the permissions
        if [[ $RESULT -eq $COMPARISON ]]; then # If the result is the same as the file's permissions...
                echo "Not more permissive" # ...then you know the file has no more permissions than the base permission
        else
                echo "More permissive"
        fi
done

【讨论】:

    【解决方案2】:

    假设“比 666 更允许”你的意思是“设置了执行位”,那么我相信

    find $path -perm /111
    

    做你想做的。

    扩展到644 权限使 find 命令类似于:

    find $path -perm /100 -o \( -perm /044 -a -perm /033 \)
    

    我认为。

    我觉得可能有一种聪明的方法可以从所需的权限获得查找模式,但我必须多加考虑。

    【讨论】:

      【解决方案3】:

      你可以用一种更简单的方式来做到这一点:

      maxperms="666"

      if [ `stat /etc/profile --format=%a` -gt $maxperms ]
      then
         echo "TOO PERMISSIVE"
      else
         echo "FINE"
      fi
      

      糟糕,第二次剪辑:

      for d in `stat test.txt --format=0%a | fold -w1`
      do 
         if [ $d -gt "6" ] 
         then 
            echo "TOO PERMISSIVE"
            exit
         else
            echo "FINE"
         fi
      done
      

      【讨论】:

      • 077 的问题是小于 666,但更宽松。
      【解决方案4】:

      你可以一点一点比较:

      #!/bin/bash
      
      function has_good_perm {
          local FILE STAT X
          for FILE; do
              STAT=$(exec stat -c '%a' "$FILE")
              X=${STAT:0:1}
              (( X & 1 )) && return 1               ## False if first number has executable bit e.g. 7, 5, 1
              X=${STAT:1:1}
              (( (X & 1) || (X & 2) )) && return 1  ## False if second number has executable bit or writable bit e.g. 7, 6, 5, 3, 1
              X=${STAT:2:1}
              (( (X & 1) || (X & 2) )) && return 1
          done
          return 0
      }
      
      if has_good_perm /etc/file1 /etc/file2; then
          echo "All files are good!"
      else
          echo "Something's bad."
      fi
      

      或者

      function has_good_perm {
          local STAT X
          STAT=$(exec stat -c '%a' "$1")
          X=${STAT:0:1}
          (( X & 1 )) && return 1
          X=${STAT:1:1}
          (( (X & 1) || (X & 2) )) && return 1
          X=${STAT:2:1}
          (( (X & 1) || (X & 2) )) && return 1
          return 0
      }
      
      for FILE in /etc/file1 /etc/file2; do
          if has_good_perm "$FILE"; then
              echo "Good file: $FILE"
          else
              echo "Bad file: $FILE"
          fi
      done
      

      【讨论】:

        【解决方案5】:

        如果您需要“比 644 更允许”,您实际上搜索 1338 中的 any 位(即:6448 XOR 7778) 已设置。

        那么,根据man find

           -perm /mode
                  **Any** of the permission bits mode are set for the file.   Symbolic
                  modes  are  accepted in this form.  You must specify `u', `g' or
                  `o' if you use a symbolic mode.  See the  EXAMPLES  section  for
                  some  illustrative  examples.  If no permission bits in mode are
                  set, this test matches any file (the idea here is to be  consis‐
                  tent with the behaviour of -perm -000).
        

        所以,这可能会完成这项工作:

        find $path -perm /133
        

        或者更准确地说是关于您的具体需求:

        BADFILES=$(find /etc/file1 /etc/file2 -perm /133)
        if [ -n "$BADFILES" ]
        then
            echo 'Bad!' 1>&2
        fi
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-03-28
          • 2018-01-09
          • 2013-05-05
          • 2016-12-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多