scripts/duplicati/create.homedir.sh

231 lines
5.3 KiB
Bash
Raw Normal View History

2021-11-19 13:59:39 +01:00
#!/bin/sh
#
# Purpose {{{
# This script will create homedir for members of an LDAP group
# 1. Get members list from LDAP group given as argument
# …
#
# 2021-11-19
# }}}
# Vars {{{
readonly PROGNAME=$(basename "${0}")
readonly PROGDIR=$(readlink -m $(dirname "${0}"))
readonly ARGS="${*}"
readonly NBARGS="${#}"
[ -z "${DEBUG}" ] && DEBUG=1
## Export DEBUG for sub-script
export DEBUG
# Default values for some vars
2021-11-19 14:13:23 +01:00
readonly LDAP_GROUP_BASE_DEFAULT="ou=grouper,dc=univ-rennes1,dc=fr"
2021-11-19 14:19:57 +01:00
readonly LDAP_SERVER_DEFAULT="ldap://ldap.univ-rennes1.fr"
2021-11-19 13:59:39 +01:00
## Colors
readonly PURPLE='\033[1;35m'
readonly RED='\033[0;31m'
readonly RESET='\033[0m'
readonly COLOR_DEBUG="${PURPLE}"
# }}}
usage() { # {{{
cat <<- EOF
2021-11-19 14:19:57 +01:00
usage: $PROGNAME [-b|-d|-g|-h|-s]
2021-11-19 13:59:39 +01:00
2021-11-19 14:13:23 +01:00
Create homedir for members of the given LDAP group.
2021-11-19 13:59:39 +01:00
EXAMPLES:
- Create homedir for members of "ldap:group:my_group"
${PROGNAME} --group "ldap:group:my_group"
OPTIONS:
2021-11-19 14:13:23 +01:00
-b,--base LDAP_BASE
Set different LDAP base (default: ${LDAP_GROUP_BASE_DEFAULT}).
2021-11-19 13:59:39 +01:00
-d,--debug
Enable debug messages.
-g,--group LDAP_GROUP_CN
Required.
LDAP group to parse in order to get the list of homedir
to create.
-h,--help
Print this help message.
2021-11-19 14:19:57 +01:00
-s,--server ldap://ldap.domain.tld
LDAP url to use for ldapsearch request (default: ${LDAP_SERVER_DEFAULT}).
2021-11-19 13:59:39 +01:00
EOF
}
# }}}
debug_message() { # {{{
local_message="${1}"
## Print message if DEBUG is enable (=0)
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG ${PROGNAME}: ${local_message}"
return 0
}
# }}}
error_message() { # {{{
local_error_message="${1}"
local_error_code="${2}"
2021-11-19 14:05:56 +01:00
## Print error message
printf '%b\n' "ERROR ${PROGNAME}: ${RED}${local_error_message}${RESET}"
2021-11-19 13:59:39 +01:00
exit "${local_error_code:=66}"
}
# }}}
define_vars() { # {{{
# If ldap_group_cn wasn't defined (argument) {{{
if [ -z "${ldap_group_cn}" ]; then
2021-11-19 14:05:56 +01:00
## Keep it empty
ldap_group_cn=""
2021-11-19 13:59:39 +01:00
fi
# }}}
2021-11-19 14:13:23 +01:00
# If ldap_group_base wasn't defined (argument) {{{
if [ -z "${ldap_group_base}" ]; then
## Use default value
ldap_group_base="${LDAP_GROUP_BASE_DEFAULT}"
fi
# }}}
2021-11-19 14:19:57 +01:00
# If ldap_server wasn't defined (argument) {{{
if [ -z "${ldap_server}" ]; then
## Use default value
ldap_server="${LDAP_SERVER_DEFAULT}"
fi
# }}}
2021-11-19 13:59:39 +01:00
}
# }}}
2021-11-19 14:05:56 +01:00
is_var_empty() { # {{{
## Return False by default
return_var_empty="1"
## Total number of variables to test
local_total_var="${#}"
loop_count_var_empty="0"
## While it remains a variable to test
while [ "${local_total_var}" -gt "${loop_count_var_empty}" ]; do
debug_message "is_var_empty \
Test var: ${1}."
### Test if this is empty and set return value to True
[ -z "${1}" ] && return_var_empty="0"
### Increase the number of tested variables
loop_count_var_empty=$((loop_count_var_empty+1))
### Shift to the next variable
shift
done
return "${return_var_empty}"
}
# }}}
2021-11-19 13:59:39 +01:00
main() { # {{{
## Define all vars
define_vars
2021-11-19 14:05:56 +01:00
## If ldap_group_cn is empty
### Print help message
### AND exit with message and error
is_var_empty "${ldap_group_cn}" \
&& usage \
&& error_message "Please enter a GROUP with -g|--group option." "1"
2021-11-19 14:13:23 +01:00
## Information message
2021-11-19 14:19:57 +01:00
debug_message "Search for members in ${RED}${ldap_group_cn},${ldap_group_base}${COLOR_DEBUG} group \
on ${RED}${ldap_server}${COLOR_DEBUG} LDAP server."
2021-11-19 14:13:23 +01:00
2021-11-19 13:59:39 +01:00
}
# }}}
# Manage arguments # {{{
# This code can't be in a function due to argument management
if [ ! "${NBARGS}" -eq "0" ]; then
manage_arg="0"
## If the first argument is not an option
if ! printf -- '%s' "${1}" | grep -q -E -- "^-+";
then
## Print help message and exit
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---"
usage
exit 1
fi
# Parse all options (start with a "-") one by one
while printf -- '%s' "${1}" | grep -q -E -- "^-+"; do
case "${1}" in
2021-11-19 14:13:23 +01:00
-b|--base ) ## Define ldap_group_base
## Move to the next argument
shift
## Define var
readonly ldap_group_base="${1}"
;;
2021-11-19 13:59:39 +01:00
-d|--debug ) ## debug
DEBUG=0
;;
-g|--group ) ## Define ldap_group_cn
## Move to the next argument
shift
## Define var
readonly ldap_group_cn="${1}"
;;
-h|--help ) ## help
usage
## Exit after help informations
exit 0
;;
2021-11-19 14:19:57 +01:00
-s|--server) ## Define ldap_server
## Move to the next argument
shift
## Define var
readonly ldap_server="${1}"
;;
2021-11-19 13:59:39 +01:00
* ) ## unknow option
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---"
usage
exit 1
;;
esac
debug_message "Arguments management \
${RED}${1}${COLOR_DEBUG} option managed."
## Move to the next argument
shift
manage_arg=$((manage_arg+1))
done
debug_message "Arguments management \
${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed."
else
debug_message "Arguments management \
No arguments/options to manage."
fi
# }}}
main
exit 255