src-diagnostics/check-ob-perms.sh
author Asier Lostalé <asier.lostale@openbravo.com>
Wed, 19 Nov 2008 15:11:00 +0000
changeset 2023 589e1fdfac93
parent 2022 1fdbf09cd9ae
child 2029 6824b2063fc2
permissions -rwxr-xr-x
Diagnostics add license text to bash file
asier@2022
     1
#!/bin/sh
asier@2023
     2
asier@2023
     3
# *************************************************************************
asier@2023
     4
# * The contents of this file are subject to the Openbravo  Public  License
asier@2023
     5
# * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
asier@2023
     6
# * Version 1.1  with a permitted attribution clause; you may not  use this
asier@2023
     7
# * file except in compliance with the License. You  may  obtain  a copy of
asier@2023
     8
# * the License at http://www.openbravo.com/legal/license.html 
asier@2023
     9
# * Software distributed under the License  is  distributed  on  an "AS IS"
asier@2023
    10
# * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
asier@2023
    11
# * License for the specific  language  governing  rights  and  limitations
asier@2023
    12
# * under the License. 
asier@2023
    13
# * The Original Code is Openbravo ERP. 
asier@2023
    14
# * The Initial Developer of the Original Code is Openbravo SL 
asier@2023
    15
# * All portions are Copyright (C) 2008 Openbravo SL 
asier@2023
    16
# * All Rights Reserved. 
asier@2023
    17
# * Contributor(s):  ______________________________________.
asier@2023
    18
# ************************************************************************
asier@2023
    19
asier@2023
    20
asier@2023
    21
asier@2022
    22
# Check a user's permissions in a directory
asier@2022
    23
asier@2022
    24
if [ $# -lt 1 ]; then
asier@2022
    25
  echo "Usage: check-ob-perms.sh directory [username]"
asier@2022
    26
  exit 1
asier@2022
    27
elif [ ! -d "$1" ]; then
asier@2022
    28
  echo "Error: specified directory does not exist: $2"
asier@2022
    29
  exit 1
asier@2022
    30
fi
asier@2022
    31
asier@2022
    32
if [ $# -lt 2 ]; then
asier@2022
    33
  USER=${whoami}
asier@2022
    34
else
asier@2022
    35
  USER=$2
asier@2022
    36
fi
asier@2022
    37
asier@2022
    38
USERID=$(id -u $USER)
asier@2022
    39
USERGROUPS="$(id -G $USER)"
asier@2022
    40
DIRFILES=$(find "$1")
asier@2022
    41
asier@2022
    42
# If find fails, there is no read permission or the file does not exist
asier@2022
    43
if [ $? -eq 1 ]; then
asier@2022
    44
  echo "Error: user $USER does not have read permissions in some files or directories"
asier@2022
    45
  exit 1
asier@2022
    46
fi
asier@2022
    47
asier@2022
    48
IFS='
asier@2022
    49
'
asier@2022
    50
asier@2022
    51
for FILE in $DIRFILES; do
asier@2022
    52
asier@2022
    53
  OWNER=$(stat -t --format=%u "$FILE")
asier@2022
    54
  PERM=$(stat -t --format=%a "$FILE")
asier@2022
    55
  GROUP="$(stat -t --format=%g "$FILE")"
asier@2022
    56
asier@2022
    57
  if [ $USERID -ne $OWNER ]; then
asier@2022
    58
    
asier@2022
    59
    # Check if user belongs to the file's group
asier@2022
    60
    echo $USERGROUPS | grep -q $GROUP
asier@2022
    61
    [ $? -eq 0 ] && BELONGS=1 || BELONGS=0
asier@2022
    62
asier@2022
    63
    # Significant bit
asier@2022
    64
    if [ $BELONGS -eq 1 ]; then
asier@2022
    65
      SBIT=$(echo $PERM | sed 's/.\(.\)./\1/')
asier@2022
    66
    else
asier@2022
    67
      SBIT=$(echo $PERM | sed 's/..\(.\)/\1/')
asier@2022
    68
    fi
asier@2022
    69
asier@2022
    70
    if [ $SBIT -ne 6 ] && [ $SBIT -ne 7 ]; then
asier@2022
    71
      echo "Error: user $USER does not have read or write permissions in file or directory:"
asier@2022
    72
      echo "$FILE"
asier@2022
    73
      exit 1
asier@2022
    74
    fi
asier@2022
    75
asier@2022
    76
  fi
asier@2022
    77
asier@2022
    78
done