Skip to content

Commit

Permalink
refactor: refactor check_permissions.sh
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dgibbs64 committed Oct 10, 2023
1 parent f5496c7 commit db8f5ba
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions lgsm/modules/check_permissions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@ 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
if [ "$(find "${serverfiles}" -not -user "$(whoami)" | wc -l)" -ne "0" ]; then
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"
Expand All @@ -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"
Expand All @@ -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}"
Expand All @@ -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}")
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit db8f5ba

Please sign in to comment.