2014-07-09 77 views
0

我要确保在/ etc /文件1和/ etc /文件2是不是更比允许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; 

,但我得到“庆典:在条件表达式语法错误 庆典:语法错误附近`-a'“​​

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

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

+0

“更多permi可能比666“只是意味着”有一个执行位集“不是吗? –

+0

我想是的。但我需要的实际数字是644。我会改变它来反映这一点。 – michaelAdam

回答

1

你可以在一个更简单的方式做到这一点:

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 
+0

问题是077小于666,但更宽容。 – michaelAdam

1

假设通过“比666更容易”你st意味着“有一个执行位集”,那么我相信

find $path -perm /111 

做你想做的。

延伸到644权限,使该find命令是这样的:

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

我想。

我觉得有可能是一个聪明的方式来获得所需的权限,以找到模式,但我不得不给更多的想法。

+0

谢谢,我会稍后检查它是否有效。 – michaelAdam

0

您可以通过点点滴滴对它们进行比较:

#!/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 
0

如果你需要 “比644更允许”,你其实搜索如果在133 8任位(ie:644 XOR 777 )。

然后,根据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 
相关问题