src-diagnostics/check-ob-perms.sh
author Asier Lostalé <asier.lostale@openbravo.com>
Wed, 19 Nov 2008 14:58:27 +0000
changeset 2022 1fdbf09cd9ae
child 2023 589e1fdfac93
permissions -rwxr-xr-x
diagnostics: added file permissions and log4j.lcf availability checks, missing file
asier@2022
     1
#!/bin/sh
asier@2022
     2
# Check a user's permissions in a directory
asier@2022
     3
asier@2022
     4
if [ $# -lt 1 ]; then
asier@2022
     5
  echo "Usage: check-ob-perms.sh directory [username]"
asier@2022
     6
  exit 1
asier@2022
     7
elif [ ! -d "$1" ]; then
asier@2022
     8
  echo "Error: specified directory does not exist: $2"
asier@2022
     9
  exit 1
asier@2022
    10
fi
asier@2022
    11
asier@2022
    12
if [ $# -lt 2 ]; then
asier@2022
    13
  USER=${whoami}
asier@2022
    14
else
asier@2022
    15
  USER=$2
asier@2022
    16
fi
asier@2022
    17
asier@2022
    18
USERID=$(id -u $USER)
asier@2022
    19
USERGROUPS="$(id -G $USER)"
asier@2022
    20
DIRFILES=$(find "$1")
asier@2022
    21
asier@2022
    22
# If find fails, there is no read permission or the file does not exist
asier@2022
    23
if [ $? -eq 1 ]; then
asier@2022
    24
  echo "Error: user $USER does not have read permissions in some files or directories"
asier@2022
    25
  exit 1
asier@2022
    26
fi
asier@2022
    27
asier@2022
    28
IFS='
asier@2022
    29
'
asier@2022
    30
asier@2022
    31
for FILE in $DIRFILES; do
asier@2022
    32
asier@2022
    33
  OWNER=$(stat -t --format=%u "$FILE")
asier@2022
    34
  PERM=$(stat -t --format=%a "$FILE")
asier@2022
    35
  GROUP="$(stat -t --format=%g "$FILE")"
asier@2022
    36
asier@2022
    37
  if [ $USERID -ne $OWNER ]; then
asier@2022
    38
    
asier@2022
    39
    # Check if user belongs to the file's group
asier@2022
    40
    echo $USERGROUPS | grep -q $GROUP
asier@2022
    41
    [ $? -eq 0 ] && BELONGS=1 || BELONGS=0
asier@2022
    42
asier@2022
    43
    # Significant bit
asier@2022
    44
    if [ $BELONGS -eq 1 ]; then
asier@2022
    45
      SBIT=$(echo $PERM | sed 's/.\(.\)./\1/')
asier@2022
    46
    else
asier@2022
    47
      SBIT=$(echo $PERM | sed 's/..\(.\)/\1/')
asier@2022
    48
    fi
asier@2022
    49
asier@2022
    50
    if [ $SBIT -ne 6 ] && [ $SBIT -ne 7 ]; then
asier@2022
    51
      echo "Error: user $USER does not have read or write permissions in file or directory:"
asier@2022
    52
      echo "$FILE"
asier@2022
    53
      exit 1
asier@2022
    54
    fi
asier@2022
    55
asier@2022
    56
  fi
asier@2022
    57
asier@2022
    58
done