From c5919b9c699f6c05cca8e253cc915be39ebdaeaa Mon Sep 17 00:00:00 2001 From: Christoph Niethammer Date: Thu, 8 Mar 2012 14:46:41 +0000 Subject: [PATCH] Extended script by automatic permissions fixing. --- check_permissions.sh | 45 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/check_permissions.sh b/check_permissions.sh index a8fbf9c..82a1d3d 100755 --- a/check_permissions.sh +++ b/check_permissions.sh @@ -3,7 +3,7 @@ # Script checking the right permissions for software installations: # Ordinary files must at least have permissions 664 and directories 755. # -# Christoph Niethammer (C) 2010-2011 +# Christoph Niethammer (C) 2010-2012 # declare -r APP_NAME="${0##*/}" @@ -12,6 +12,9 @@ declare SEARCH_DIRS= declare VERBOSE=0 declare PRINT_FILES=0 declare PRINT_DIRS=0 +declare FIX_PERMISSIONS=0 +declare -i NUM_FIEX_DIR_PERMISSIONS=0 +declare -i NUM_FIEX_FILE_PERMISSIONS=0 declare -r FILE_PERMS=664 declare -r DIR_PERMS=775 @@ -28,6 +31,7 @@ Usage: $APP_NAME DIR -h, --help Print this usage -v, --verbose Be more verbose (print list of found directories and files) + --fixit Fix permissions if possible (only working in verbose mode) Checks permissions in the given directories. Permissions for files must be at least set to 664 and for directories must be exactly 775. @@ -40,6 +44,14 @@ function die { exit 1 } +function file_add_perm() { + local perm=$1 + local oldperm=$(stat -c %a $file) + local filename=$2 + local newperm="$(( ${oldperm:0:1} | ${perm:0:1} ))$(( ${oldperm:1:1} | ${perm:1:1} ))$(( ${oldperm:2:1} | ${perm:2:1} ))" + chmod $newperm $filename +} + if [ $# -lt 1 ]; then print_usage exit 0 @@ -47,6 +59,9 @@ fi for arg in $@; do case $arg in + --fixit) + FIX_PERMISSIONS=1 + ;; --help|-h) print_usage exit 0 @@ -76,20 +91,40 @@ WRONG_FILES=(`find $SEARCH_DIRS -type f ! -perm -$FILE_PERMS -print`) if [[ $PRINT_FILES -eq 1 ]]; then echo "# ** directories with wrong permissions:" for dir in ${WRONG_DIRS[@]}; do - echo -e "$dir\t\t$(stat -c "%a" $dir)" + if [[ $FIX_PERMISSIONS -eq 1 ]]; then + echo -en "$dir\t\t$(stat -c "%a" $dir)" + if chmod $DIR_PERMS $dir ; then + NUM_FIXED_DIR_PERMISSIONS=$(($NUM_DIRD_FILE_PERMISSIONS + 1)) + echo " ... (fixed)" + else + echo " ... (not fixed)" + fi + else + echo -e "$dir\t\t$(stat -c "%a" $dir)" + fi done echo "# ** files with wrong permissions:" for file in ${WRONG_FILES[@]}; do - echo -e "$file\t\t$(stat -c "%a" $file)" + if [[ $FIX_PERMISSIONS -eq 1 ]]; then + echo -en "'$file'\t\t$(stat -c "%a" $file)" + if file_add_perm $FILE_PERMS $file ; then + NUM_FIXED_FILE_PERMISSIONS=$(($NUM_FIXED_FILE_PERMISSIONS + 1)) + echo " ... (fixed)" + else + echo " ... (not fixed)" + fi + else + echo -e "$file\t\t$(stat -c "%a" $file)" + fi done fi echo "#" echo "# ------------------------------" echo "# Summary:" -echo "# directories with wrong permissions: ${#WRONG_DIRS[@]}" -echo "# files with wrong permissions: ${#WRONG_FILES[@]}" +echo "# directories with wrong permissions: ${#WRONG_DIRS[@]}${NUM_FIXED_DIR_PERMISSIONS:+ (fixed $NUM_FIXED_DIR_PERMISSIONS)}" +echo "# files with wrong permissions: ${#WRONG_FILES[@]}${NUM_FIXED_FILE_PERMISSIONS:+ (fixed $NUM_FIXED_FILE_PERMISSIONS)}" echo "# ------------------------------" echo "#"