From 825cf90ec07edd4bbf6dc1833189b607a7073ef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gardais=20J=C3=A9r=C3=A9my?= Date: Fri, 26 Nov 2021 14:14:47 +0100 Subject: [PATCH] Manage --home option and verify home base dir --- duplicati/create.homedir.sh | 60 +++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/duplicati/create.homedir.sh b/duplicati/create.homedir.sh index 5abec15..3eb5785 100755 --- a/duplicati/create.homedir.sh +++ b/duplicati/create.homedir.sh @@ -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_USER_CMD_DEFAULT=$(sed -n 's/\(^binddn \)\(.*\)/\2/p' /etc/nslcd.conf) fi +readonly HOME_BASE_DEFAULT="/home" ## Colors readonly PURPLE='\033[1;35m' @@ -56,7 +57,11 @@ OPTIONS : LDAP group to parse in order to get the list of homedir 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. -p,--password,--password-cmd "sed -n … /etc/…" @@ -64,7 +69,8 @@ OPTIONS : works with NSLCD − /etc/nslcd.conf). -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/…" 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." 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}" +} +# }}} +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() { # {{{ @@ -222,19 +257,26 @@ main() { # {{{ ### AND exit with message and error is_var_empty "${ldap_group_cn}" \ && 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 {{{ ### Print help message ### AND exit with message and error is_var_empty_silent "${ldap_user}" "${ldap_passwd}" \ && 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 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 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 ## Exit after help informations exit 0