Manage --home option and verify home base dir

This commit is contained in:
Jeremy Gardais 2021-11-26 14:14:47 +01:00
parent 3d14412124
commit 825cf90ec0
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 54 additions and 6 deletions

View File

@ -23,6 +23,7 @@ if [ -f /etc/nslcd.conf ]; then
readonly LDAP_PASSWD_CMD_DEFAULT=$(sed -n 's/\(^bindpw \)\(.*\)/\2/p' /etc/nslcd.conf) readonly LDAP_PASSWD_CMD_DEFAULT=$(sed -n 's/\(^bindpw \)\(.*\)/\2/p' /etc/nslcd.conf)
readonly LDAP_USER_CMD_DEFAULT=$(sed -n 's/\(^binddn \)\(.*\)/\2/p' /etc/nslcd.conf) readonly LDAP_USER_CMD_DEFAULT=$(sed -n 's/\(^binddn \)\(.*\)/\2/p' /etc/nslcd.conf)
fi fi
readonly HOME_BASE_DEFAULT="/home"
## Colors ## Colors
readonly PURPLE='\033[1;35m' readonly PURPLE='\033[1;35m'
@ -56,7 +57,11 @@ OPTIONS:
LDAP group to parse in order to get the list of homedir LDAP group to parse in order to get the list of homedir
to create. to create.
-h,--help -h,--home,--home-base
Base directory to use for user's home directory
(default: ${HOME_BASE_DEFAULT}).
--help
Print this help message. Print this help message.
-p,--password,--password-cmd "sed -n … /etc/…" -p,--password,--password-cmd "sed -n … /etc/…"
@ -64,7 +69,8 @@ OPTIONS:
works with NSLCD /etc/nslcd.conf). works with NSLCD /etc/nslcd.conf).
-s,--server ldap://ldap.domain.tld -s,--server ldap://ldap.domain.tld
LDAP url to use for ldapsearch request (default: ${LDAP_SERVER_DEFAULT}). LDAP url to use for ldapsearch request
(default: ${LDAP_SERVER_DEFAULT}).
-u,--user,--user-cmd "sed -n … /etc/…" -u,--user,--user-cmd "sed -n … /etc/…"
Command to get LDAP bind user from a file (by default, Command to get LDAP bind user from a file (by default,
@ -129,6 +135,12 @@ define_vars() { # {{{
debug_message "Use default command to get LDAP user." debug_message "Use default command to get LDAP user."
fi fi
# }}} # }}}
# If home_base wasn't defined (argument) {{{
if [ -z "${home_base}" ]; then
## Use default value
home_base="${HOME_BASE_DEFAULT}"
fi
# }}}
} }
# }}} # }}}
@ -204,6 +216,29 @@ ${RED}${local_command_available_cmd}${COLOR_DEBUG} is not available on this host
return "${return_command_available}" return "${return_command_available}"
}
# }}}
is_directory_absent() { # {{{
local_directory_absent="${1}"
## Directory doesn't exists by default
return_is_directory_absent="0"
### Check if the directory exists
# shellcheck disable=SC2086
if test -d "${local_directory_absent}"; then
return_is_directory_absent="1"
debug_message "is_directory_absent \
The directory ${RED}${local_directory_absent}${COLOR_DEBUG} exists."
else
return_is_directory_absent="0"
debug_message "is_directory_absent \
The directory ${RED}${local_directory_absent}${COLOR_DEBUG} doesn't exist."
fi
return "${return_is_directory_absent}"
} }
# }}} # }}}
main() { # {{{ main() { # {{{
@ -222,19 +257,26 @@ main() { # {{{
### AND exit with message and error ### AND exit with message and error
is_var_empty "${ldap_group_cn}" \ is_var_empty "${ldap_group_cn}" \
&& usage \ && usage \
&& error_message "Please enter a GROUP with -g|--group option." "1" && error_message "Please enter a GROUP with -g|--group option." 1
## }}} ## }}}
## If ldap_user or ldap_passwd is empty {{{ ## If ldap_user or ldap_passwd is empty {{{
### Print help message ### Print help message
### AND exit with message and error ### AND exit with message and error
is_var_empty_silent "${ldap_user}" "${ldap_passwd}" \ is_var_empty_silent "${ldap_user}" "${ldap_passwd}" \
&& usage \ && usage \
&& error_message "LDAP user or password is empty. Please verify your configuration or the --user-cmd|--passwd-cmd options." "2" && error_message "LDAP user or password is empty. Please verify your configuration or the --user-cmd|--passwd-cmd options." 2
## }}} ## }}}
## If home_base directory doesn't exists {{{
### AND exit with message and error
is_directory_absent "${home_base}" \
&& error_message "Home base directory (${home_base}) doesn't exists. Check your configuration or use -h|--home option." 3
## }}}
## Information message ## Information message
debug_message "Search for members in ${RED}${ldap_group_cn},${ldap_group_base}${COLOR_DEBUG} group \ debug_message "Search for members in ${RED}${ldap_group_cn},${ldap_group_base}${COLOR_DEBUG} group \
on ${RED}${ldap_server}${COLOR_DEBUG} LDAP server." on ${RED}${ldap_server}${COLOR_DEBUG} LDAP server in ${RED}${home_base}${COLOR_DEBUG}."
} }
# }}} # }}}
@ -276,7 +318,13 @@ if [ ! "${NBARGS}" -eq "0" ]; then
## Define var ## Define var
readonly ldap_group_cn="${1}" readonly ldap_group_cn="${1}"
;; ;;
-h|--help ) ## help -h|--home|--home-base ) ## Define home_base
## Move to the next argument
shift
## Define var
readonly home_base="${1}"
;;
--help ) ## help
usage usage
## Exit after help informations ## Exit after help informations
exit 0 exit 0