2011-02-12 03:55:14 +00:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# Script checking the right permissions for software installations:
|
|
|
|
# Ordinary files must at least have permissions 664 and directories 755.
|
2015-11-05 11:03:02 +00:00
|
|
|
#
|
2016-08-12 12:51:25 +00:00
|
|
|
# Copyright (c) 2010-2016 Christoph Niethammer <niethammer@hlrs.de>
|
2011-02-12 03:55:14 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
declare -r APP_NAME="${0##*/}"
|
|
|
|
declare -r VERSION="0.8"
|
|
|
|
declare SEARCH_DIRS=
|
|
|
|
declare VERBOSE=0
|
|
|
|
declare PRINT_FILES=0
|
|
|
|
declare PRINT_DIRS=0
|
2012-03-08 14:46:41 +00:00
|
|
|
declare FIX_PERMISSIONS=0
|
|
|
|
declare -i NUM_FIEX_DIR_PERMISSIONS=0
|
|
|
|
declare -i NUM_FIEX_FILE_PERMISSIONS=0
|
2012-03-06 09:50:10 +00:00
|
|
|
declare -r FILE_PERMS=664
|
|
|
|
declare -r DIR_PERMS=775
|
2011-02-12 03:55:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
function print_usage {
|
|
|
|
|
|
|
|
binary=`basename $0`
|
|
|
|
cat <<EOF
|
|
|
|
${APP_NAME}: (${VERSION})
|
|
|
|
|
2011-03-04 15:35:09 +00:00
|
|
|
Copyright (C) 2010-2011 Christoph Niethammer <niethammer@hlrs.de>
|
2011-02-12 03:55:14 +00:00
|
|
|
|
|
|
|
Usage: $APP_NAME DIR
|
|
|
|
|
|
|
|
-h, --help Print this usage
|
|
|
|
-v, --verbose Be more verbose (print list of found directories and files)
|
2012-03-08 14:46:41 +00:00
|
|
|
--fixit Fix permissions if possible (only working in verbose mode)
|
2011-02-12 03:55:14 +00:00
|
|
|
|
2015-11-05 11:03:02 +00:00
|
|
|
Checks permissions in the given directories. Permissions for files must be
|
2011-02-12 03:55:14 +00:00
|
|
|
at least set to 664 and for directories must be exactly 775.
|
|
|
|
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
function die {
|
|
|
|
echo $1
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2012-03-08 14:46:41 +00:00
|
|
|
function file_add_perm() {
|
|
|
|
local perm=$1
|
2016-08-12 12:51:25 +00:00
|
|
|
shift
|
|
|
|
local filename="$@"
|
|
|
|
local oldperm=$(stat -c '%a' "${filename[@]}")
|
2012-03-08 14:46:41 +00:00
|
|
|
local newperm="$(( ${oldperm:0:1} | ${perm:0:1} ))$(( ${oldperm:1:1} | ${perm:1:1} ))$(( ${oldperm:2:1} | ${perm:2:1} ))"
|
2016-08-12 12:51:25 +00:00
|
|
|
chmod $newperm "${filename[@]}"
|
2012-03-08 14:46:41 +00:00
|
|
|
}
|
|
|
|
|
2011-02-12 03:55:14 +00:00
|
|
|
if [ $# -lt 1 ]; then
|
|
|
|
print_usage
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2015-11-05 11:03:02 +00:00
|
|
|
for arg in $@; do
|
2011-02-12 03:55:14 +00:00
|
|
|
case $arg in
|
2012-03-08 14:46:41 +00:00
|
|
|
--fixit)
|
|
|
|
FIX_PERMISSIONS=1
|
|
|
|
;;
|
2011-02-12 03:55:14 +00:00
|
|
|
--help|-h)
|
|
|
|
print_usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
--verbose|-v)
|
|
|
|
VERBOSE=1
|
|
|
|
PRINT_FILES=1
|
|
|
|
PRINT_DIRS=1
|
|
|
|
;;
|
2012-07-05 07:41:51 +00:00
|
|
|
--no-print-files)
|
|
|
|
PRINT_FILES=0
|
|
|
|
;;
|
|
|
|
--no-print-dirs)
|
|
|
|
PRINT_DIRS=0
|
|
|
|
;;
|
2011-02-12 03:55:14 +00:00
|
|
|
*)
|
|
|
|
if [[ ! -d $arg ]]; then
|
|
|
|
echo "# Skipped $arg"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
SEARCH_DIRS+="${IFS}${arg}"
|
|
|
|
#echo "# Added $arg"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2016-08-12 12:51:25 +00:00
|
|
|
declare -a WRONG_DIRS
|
|
|
|
declare -a WRONG_FILES
|
2015-11-05 11:03:02 +00:00
|
|
|
echo "# Searching for directories which do not match '$DIR_PERMS' ..."
|
2016-08-12 12:51:25 +00:00
|
|
|
while IFS= read -r -d '' n; do
|
|
|
|
WRONG_DIRS+=( $n )
|
|
|
|
done < <(find $SEARCH_DIRS -type d ! -perm $DIR_PERMS -print0)
|
2011-02-12 03:55:14 +00:00
|
|
|
|
2016-08-12 12:51:25 +00:00
|
|
|
echo "# Searching for files which do not match at least '$FILE_PERMS' ..."
|
|
|
|
while IFS= read -r -d '' n; do
|
|
|
|
WRONG_FILES+=( "$n" )
|
|
|
|
done < <(find $SEARCH_DIRS -type f ! -perm -$FILE_PERMS -print0)
|
|
|
|
for (( i=1; i <= ${#WRONG_FILES[@]}; i++ ))
|
|
|
|
do
|
|
|
|
echo ${WRONG_FILES[$i]}
|
|
|
|
done
|
2011-02-12 03:55:14 +00:00
|
|
|
|
2012-07-05 07:41:51 +00:00
|
|
|
if [[ $PRINT_DIRS -eq 1 ]]; then
|
2011-02-12 03:55:14 +00:00
|
|
|
echo "# ** directories with wrong permissions:"
|
2016-08-12 12:51:25 +00:00
|
|
|
for dir in "${WRONG_DIRS[@]}"; do
|
2012-03-08 14:46:41 +00:00
|
|
|
if [[ $FIX_PERMISSIONS -eq 1 ]]; then
|
2016-08-12 12:51:25 +00:00
|
|
|
echo -en "$dir\t\t"$(stat -c '%a' "$dir")
|
|
|
|
if chmod $DIR_PERMS "$dir" ; then
|
2012-03-08 14:46:41 +00:00
|
|
|
NUM_FIXED_DIR_PERMISSIONS=$(($NUM_DIRD_FILE_PERMISSIONS + 1))
|
|
|
|
echo " ... (fixed)"
|
|
|
|
else
|
|
|
|
echo " ... (not fixed)"
|
|
|
|
fi
|
|
|
|
else
|
2016-08-12 12:51:25 +00:00
|
|
|
echo -e "$dir\t\t$(stat -c '%a' \"$dir\")"
|
2012-03-08 14:46:41 +00:00
|
|
|
fi
|
2011-02-12 03:55:14 +00:00
|
|
|
done
|
2012-07-05 07:41:51 +00:00
|
|
|
fi
|
2011-02-12 03:55:14 +00:00
|
|
|
|
2012-07-05 07:41:51 +00:00
|
|
|
if [[ $PRINT_FILES -eq 1 ]]; then
|
2011-02-12 03:55:14 +00:00
|
|
|
echo "# ** files with wrong permissions:"
|
2016-08-12 12:51:25 +00:00
|
|
|
for file in "${WRONG_FILES[@]}"; do
|
2012-03-08 14:46:41 +00:00
|
|
|
if [[ $FIX_PERMISSIONS -eq 1 ]]; then
|
2016-08-12 12:51:25 +00:00
|
|
|
echo -en "$file\t\t"$(stat -c '%a' "$file")
|
|
|
|
if file_add_perm $FILE_PERMS "$file" ; then
|
2012-03-08 14:46:41 +00:00
|
|
|
NUM_FIXED_FILE_PERMISSIONS=$(($NUM_FIXED_FILE_PERMISSIONS + 1))
|
|
|
|
echo " ... (fixed)"
|
|
|
|
else
|
|
|
|
echo " ... (not fixed)"
|
|
|
|
fi
|
|
|
|
else
|
2016-08-12 12:51:25 +00:00
|
|
|
echo -e "$file\t\t$(stat -c '%a' \"$file\")"
|
2012-03-08 14:46:41 +00:00
|
|
|
fi
|
2011-02-12 03:55:14 +00:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "#"
|
|
|
|
echo "# ------------------------------"
|
|
|
|
echo "# Summary:"
|
2012-03-08 14:46:41 +00:00
|
|
|
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)}"
|
2011-02-12 03:55:14 +00:00
|
|
|
echo "# ------------------------------"
|
|
|
|
echo "#"
|
|
|
|
|
|
|
|
# clean up temporary files
|
|
|
|
#rm $WRONG_DIR_FILE
|
|
|
|
#rm $WRONG_FILES_FILE
|