src-util/diagnostic/check-ob-perms.sh
author Antonio Moreno <antonio.moreno@openbravo.com>
Tue, 30 Mar 2010 19:20:38 +0200
changeset 6901 43a7191bdeb6
parent 6886 60a98816806d
child 9072 79e55dfc8af0
permissions -rw-r--r--
[validation] Corrected build.xml
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. 
ggi@6701
    14
# * The Initial Developer of the Original Code is Openbravo SLU 
ggi@6701
    15
# * All portions are Copyright (C) 2008 Openbravo SLU 
asier@2023
    16
# * All Rights Reserved. 
asier@2023
    17
# * Contributor(s):  ______________________________________.
asier@2023
    18
# ************************************************************************
asier@2023
    19
asier@2023
    20
asier@2022
    21
# Check a user's permissions in a directory
asier@2022
    22
asier@2022
    23
if [ $# -lt 1 ]; then
asier@2022
    24
  echo "Usage: check-ob-perms.sh directory [username]"
asier@2022
    25
  exit 1
asier@2022
    26
elif [ ! -d "$1" ]; then
asier@2022
    27
  echo "Error: specified directory does not exist: $2"
asier@2022
    28
  exit 1
asier@2022
    29
fi
asier@2022
    30
asier@2022
    31
if [ $# -lt 2 ]; then
asier@2022
    32
  USER=${whoami}
asier@2022
    33
else
asier@2022
    34
  USER=$2
asier@2022
    35
fi
asier@2022
    36
asier@2022
    37
USERID=$(id -u $USER)
asier@2022
    38
USERGROUPS="$(id -G $USER)"
asier@2029
    39
DIRFILES=$(find "$1" -printf "%U-%G+%m_%p\n")
asier@2022
    40
asier@2022
    41
# If find fails, there is no read permission or the file does not exist
asier@2022
    42
if [ $? -eq 1 ]; then
asier@2022
    43
  echo "Error: user $USER does not have read permissions in some files or directories"
asier@2022
    44
  exit 1
asier@2022
    45
fi
asier@2022
    46
asier@2022
    47
IFS='
asier@2022
    48
'
asier@2022
    49
asier@2029
    50
for FILEDATA in $DIRFILES; do
asier@2029
    51
  # get part until first -
asier@2029
    52
  OWNER=${FILEDATA%%-*}
asier@2029
    53
  # get part until first +, but after -
asier@2029
    54
  mytemp=${FILEDATA%%+*}
asier@2029
    55
  GROUP=${mytemp#*-}
asier@2029
    56
  # get part until first -, but after +
asier@2029
    57
  mytemp=${FILEDATA%%_*}
asier@2029
    58
  PERM=${mytemp#*+}
asier@2029
    59
  # get part after first _
asier@2029
    60
  FILE=${FILEDATA#*_}
asier@2022
    61
asier@2022
    62
  if [ $USERID -ne $OWNER ]; then
asier@2022
    63
    
asier@2022
    64
    # Check if user belongs to the file's group
asier@2022
    65
    echo $USERGROUPS | grep -q $GROUP
asier@2022
    66
    [ $? -eq 0 ] && BELONGS=1 || BELONGS=0
asier@2022
    67
asier@2022
    68
    # Significant bit
asier@2022
    69
    if [ $BELONGS -eq 1 ]; then
asier@2022
    70
      SBIT=$(echo $PERM | sed 's/.\(.\)./\1/')
asier@2022
    71
    else
asier@2022
    72
      SBIT=$(echo $PERM | sed 's/..\(.\)/\1/')
asier@2022
    73
    fi
asier@2022
    74
asier@2022
    75
    if [ $SBIT -ne 6 ] && [ $SBIT -ne 7 ]; then
asier@2022
    76
      echo "Error: user $USER does not have read or write permissions in file or directory:"
asier@2022
    77
      echo "$FILE"
asier@2022
    78
      exit 1
asier@2022
    79
    fi
asier@2022
    80
asier@2022
    81
  fi
asier@2022
    82
asier@2022
    83
done