From db8f5ba975b6ca299acb6e4f9f426d8761643638 Mon Sep 17 00:00:00 2001 From: Daniel Gibbs Date: Tue, 10 Oct 2023 20:16:33 +0100 Subject: [PATCH] refactor: refactor check_permissions.sh Refactored the code in check_permissions.sh to improve readability and maintainability. Made changes to variable names for clarity and removed unnecessary comments. Also, refactored the fn_sys_perm_errors_detect function name for consistency. --- lgsm/modules/check_permissions.sh | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/lgsm/modules/check_permissions.sh b/lgsm/modules/check_permissions.sh index a6a37ce5c6..cc24596d29 100644 --- a/lgsm/modules/check_permissions.sh +++ b/lgsm/modules/check_permissions.sh @@ -13,9 +13,9 @@ fn_check_ownership() { selfownissue=1 fi fi - if [ -d "${modulesdir}" ]; then - if [ "$(find "${modulesdir}" -not -user "$(whoami)" | wc -l)" -ne "0" ]; then - funcownissue=1 + if [ -d "${lgsmdir}" ]; then + if [ "$(find "${lgsmdir}" -not -user "$(whoami)" | wc -l)" -ne "0" ]; then + lgsmownissue=1 fi fi if [ -d "${serverfiles}" ]; then @@ -23,18 +23,18 @@ fn_check_ownership() { filesownissue=1 fi fi - if [ "${selfownissue}" == "1" ] || [ "${funcownissue}" == "1" ] || [ "${filesownissue}" == "1" ]; then + if [ "${selfownissue}" == "1" ] || [ "${lgsmownissue}" == "1" ] || [ "${filesownissue}" == "1" ]; then fn_print_fail_nl "Ownership issues found" fn_script_log_fail "Ownership issues found" fn_print_information_nl "The current user ($(whoami)) does not have ownership of the following files:" fn_script_log_info "The current user ($(whoami)) does not have ownership of the following files:" { - echo -e "User\tGroup\tFile\n" + echo -en "User\tGroup\tFile:" if [ "${selfownissue}" == "1" ]; then find "${rootdir}/${selfname}" -not -user "$(whoami)" -printf "%u\t%g\t%p\n" fi - if [ "${funcownissue}" == "1" ]; then - find "${modulesdir}" -not -user "$(whoami)" -printf "%u\t%g\t%p\n" + if [ "${lgsmownissue}" == "1" ]; then + find "${lgsmdir}" -not -user "$(whoami)" -printf "%u\t%g\t%p\n" fi if [ "${filesownissue}" == "1" ]; then find "${serverfiles}" -not -user "$(whoami)" -printf "%u\t%g\t%p\n" @@ -53,15 +53,18 @@ fn_check_ownership() { } fn_check_permissions() { + # Check modules files are executable. if [ -d "${modulesdir}" ]; then - if [ "$(find "${modulesdir}" -type f -not -executable | wc -l)" -ne "0" ]; then + findnotexecutable="$(find "${modulesdir}" -type f -not -executable)" + findnotexecutablewc="$(echo "${findnotexecutable}" | wc -l)" + if [ "${findnotexecutablewc}" -ne "0" ]; then fn_print_fail_nl "Permissions issues found" fn_script_log_fail "Permissions issues found" fn_print_information_nl "The following files are not executable:" fn_script_log_info "The following files are not executable:" { - echo -e "File\n" - find "${modulesdir}" -type f -not -executable -printf "%p\n" + echo -en "File:" + echo -en "${findnotexecutable}" } | column -s $'\t' -t | tee -a "${lgsmlog}" if [ "${monitorflag}" == 1 ]; then alert="permissions" @@ -72,8 +75,8 @@ fn_check_permissions() { fi # Check rootdir permissions. - if [ "${rootdir}" ]; then - # Get permission numbers on directory under the form 775. + if [ -d "${rootdir}" ]; then + # Get permission numbers on directory should return 775. rootdirperm=$(stat -c %a "${rootdir}") # Grab the first and second digit for user and group permission. userrootdirperm="${rootdirperm:0:1}" @@ -92,6 +95,7 @@ fn_check_permissions() { core_exit.sh fi fi + # Check if executable is executable and attempt to fix it. # First get executable name. execname=$(basename "${executable}") @@ -141,7 +145,7 @@ fn_check_permissions() { fi } -## The following fn_sys_perm_* modules checks for permission errors in /sys directory. +## The following fn_sys_perm_* function checks for permission errors in /sys directory. # Checks for permission errors in /sys directory. fn_sys_perm_errors_detect() {