From 4be5a7cb69b7bb41dddf4d3e69b450942897c3ee Mon Sep 17 00:00:00 2001 From: Christoph Date: Tue, 16 Oct 2018 02:46:41 +0200 Subject: [PATCH] Add script 'set_permissions.sh'. This replaces script 'set_directory_permissions.sh', which is now deprecated. --- conf/set_permissions.conf.sample | 17 +++ set_directory_permissions.sh | 3 + set_permissions.sh | 222 +++++++++++++++++++++++++++++++ 3 files changed, 242 insertions(+) create mode 100644 conf/set_permissions.conf.sample create mode 100755 set_permissions.sh diff --git a/conf/set_permissions.conf.sample b/conf/set_permissions.conf.sample new file mode 100644 index 0000000..e55dd71 --- /dev/null +++ b/conf/set_permissions.conf.sample @@ -0,0 +1,17 @@ +# --------------------------------------------- +# - Settings for script set_permissions.sh +# --------------------------------------------- + +# - dir_permissions +# - +# - Recursive set Permissions (group and file- and directory-mode) +# - +# - Multiple options are possible. Use semicolon separated list. +# - +# - Usage: +# - dir_permissions=":::;[:::];[.." +# - +# - Example: +# - dir_permissions="/data/samba/transfer:buero:664:2775;/data/samba/verwaltung:intern:660:2770" +# - +#dir_permissions="" diff --git a/set_directory_permissions.sh b/set_directory_permissions.sh index 13e5c6b..6c50040 100755 --- a/set_directory_permissions.sh +++ b/set_directory_permissions.sh @@ -1,5 +1,6 @@ #!/usr/bin/env bash +script_name="$(basename $(realpath $0))" working_dir="$(dirname $(realpath $0))" conf_file="${working_dir}/conf/set_directory_permissions.conf" @@ -107,6 +108,8 @@ else fi +warn "Script '$script_name' is deprecated. Use 'set_permissions.sh' instead." + # ------------- # - Read Configurations from $conf_file # ------------- diff --git a/set_permissions.sh b/set_permissions.sh new file mode 100755 index 0000000..98f23e6 --- /dev/null +++ b/set_permissions.sh @@ -0,0 +1,222 @@ +#!/usr/bin/env bash + +# --- +# - Replaces script set_directory_permissions.sh +# --- + +script_name="$(basename $(realpath $0))" +working_dir="$(dirname $(realpath $0))" + +conf_file="${working_dir}/conf/${script_name%%.*}.conf" + +LOCK_DIR="/tmp/set_directory_permissions.LOCK" + + +# ------------- +# - Some functions +# ------------- + +clean_up() { + + # Perform program exit housekeeping + rm -rf "$LOCK_DIR" + exit $1 +} + +echononl(){ + if $terminal ; then + echo X\\c > /tmp/shprompt$$ + if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then + echo -e -n "$*\\c" 1>&2 + else + echo -e -n "$*" 1>&2 + fi + rm /tmp/shprompt$$ + fi +} + +fatal(){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mFatal\033[m ]: $*" + echo "" + echo -e " \033[31m\033[1mScript was terminated\033[m!" + else + echo " [ Fatal ]: $*" + echo "" + echo " Script was terminated...." + fi + echo "" + clean_up 1 +} + +error (){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mError\033[m ]: $*" + else + echo "[ Error ]: $*" + fi + echo "" +} + +warn (){ + echo "" + if $terminal ; then + echo -e " [ \033[33m\033[1mWarning\033[m ]: $*" + else + echo "[ Warning ]: $*" + fi + echo "" +} +info (){ + + if $terminal ; then + echo "" + echo -e " [ \033[32m\033[1mInfo\033[m ]: $*" + echo "" + fi +} + +echo_done() { + if $terminal ; then + echo -e "\033[75G[ \033[32mdone\033[m ]" + fi +} +echo_failed(){ + if $terminal && $LOGGING ; then + echo -e "\033[75G[ \033[1;31mfailed\033[m ]" + fi +} +echo_skipped() { + if $terminal && $LOGGING ; then + echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]" + fi +} + +trim() { + local var="$*" + var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters + var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters + echo -n "$var" +} + + +# ------------- +# - Running in a terminal? +# ------------- + +if [[ -t 1 ]] ; then + terminal=true +else + terminal=false +fi + + +# ------------- +# - Read Configurations from $conf_file +# ------------- + +if [[ ! -f "$conf_file" ]]; then + fatal " Configuration file '$(basename ${conf_file})' not found!" +else + source "$conf_file" +fi + +[[ -z "$(trim $dir_permissions)" ]] && fatal "No directory given!" + + +# ------------- +# - Job is already running? +# ------------- + +# - If job already runs, stop execution.. +# - +if mkdir "$LOCK_DIR" 2> /dev/null ; then + + ## - Remove lockdir when the script finishes, or when it receives a signal + trap clean_up SIGHUP SIGINT SIGTERM + +else + + datum="$(date +"%d.%m.%Y %H:%M")" + + msg=" [ Error ]: A previos instance of '$(basename $0)' seems already be running.\n\n Exiting now.." + + error "A previos instance of the script '$(basename $0)' seems already be running." + + exit 1 + +fi + +IFS=';' +for _dir_perm in $dir_permissions ; do + + # - Allow parameter setting like: + # - + # - dir_permissions=" + # - :::; + # - ::: + # - " + # - + _dir_perm="$(trim $_dir_perm)" + [[ -z "$_dir_perm" ]] && continue + + IFS=':' read -a _dir_perm_arr <<< "${_dir_perm}" + + base_dir="${_dir_perm_arr[0]}" + if [[ -z "$(trim "$base_dir")" ]]; then + error "Directory not given!" + continue + fi + if [[ ! -d "$base_dir" ]]; then + error "Cannot find directory '$base_dir'" + continue + fi + + group="${_dir_perm_arr[1]}" + if [[ -z "$(trim "$group")" ]]; then + error "Group not given!" + continue + fi + if ! $(getent group | grep -i -E -q "^${group}:") ; then + error "Group '$group' does not exist!" + fi + + file_perm="${_dir_perm_arr[2]}" + if [[ -z "$(trim "$file_perm")" ]]; then + error "File permissions not given!" + continue + fi + + dir_perm="${_dir_perm_arr[3]}" + if [[ -z "$(trim "$dir_perm")" ]]; then + error "Directory permissions not given!" + continue + fi + + info "Set Permisions on \033[1m${base_dir}\033[m: Group:\033[1m${group}\033[m Perimissions: \033[1m${dir_perm}/${file_perm}\033[m .." + + chmod $dir_perm $base_dir + chgrp $group $base_dir + + while IFS='' read -r -d '' filename ; do + + [[ -f "$filename" ]] && ( chmod $file_perm "$filename"; chgrp $group "$filename") + [[ ! -d "$filename" ]] && continue + + [[ "$filename" =~ .Trash ]] && continue + [[ "$filename" = "lost+found" ]] && continue + + chmod $dir_perm "$filename" ; chgrp $group "$filename" + + find "$filename" ! -group $group -exec chgrp -R $group {} \; + find "$filename" -type d ! -perm $dir_perm -exec chmod -R $dir_perm {} \; + find "$filename" -type f ! -perm $file_perm -exec chmod -R $file_perm {} \; + + + done < <(find $base_dir -mindepth 1 -maxdepth 1 -print0) + +done + +clean_up 0