From: Frank Brehm Date: Wed, 22 Jan 2025 10:38:25 +0000 (+0100) Subject: Starting with get-last-login-times X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=fa33d4b9d6ed11a74ce68b7a4fe2a8e5585cdabe;p=pixelpark%2Fpp-admin-tools.git Starting with get-last-login-times --- diff --git a/scripts/functions.rc b/scripts/functions.rc index 958c636..7937ac6 100644 --- a/scripts/functions.rc +++ b/scripts/functions.rc @@ -44,12 +44,42 @@ NFS_HOMEDIR_PARENT='/mnt/nfs/home' DESCRIPTION="${DESCRIPTION:-Failing script description}" # LDAP Defaults -LDAP_USR="cn=admin" -LDAP_PWD_FILE="${HOME}/.private/dirsrv-prd-dpx-admin-pwd-wonl.txt" -LDAP_BASE="o=isp" -LDAP_HOST="ldap.pixelpark.com" -LDAP_PORT= -LDAP_SSL="y" +if [[ -v DEFAULT_LDAP_USR && -n "${DEFAULT_LDAP_USR}" ]] ; then + LDAP_USR="${DEFAULT_LDAP_USR}" +else + LDAP_USR="cn=admin" +fi + +if [[ -v DEFAULT_LDAP_PWD_FILE && -n "${DEFAULT_LDAP_PWD_FILE}" ]] ; then + LDAP_PWD_FILE="${DEFAULT_LDAP_PWD_FILE}" +else + LDAP_PWD_FILE="${HOME}/.private/dirsrv-prd-dpx-admin-pwd-wonl.txt" +fi + +if [[ -v DEFAULT_LDAP_BASE && -n "${DEFAULT_LDAP_BASE}" ]] ; then + LDAP_BASE="${DEFAULT_LDAP_BASE}" +else + LDAP_BASE="o=isp" +fi + +if [[ -v DEFAULT_LDAP_HOST && -n "${DEFAULT_LDAP_HOST}" ]] ; then + LDAP_HOST="${DEFAULT_LDAP_HOST}" +else + LDAP_HOST="ldap.pixelpark.com" +fi + +if [[ -v DEFAULT_LDAP_PORT && -n "${DEFAULT_LDAP_PORT}" ]] ; then + LDAP_PORT="${DEFAULT_LDAP_PORT}" +else + LDAP_PORT= +fi + +if [[ -v DEFAULT_LDAP_SSL && -n "${DEFAULT_LDAP_SSL}" ]] ; then + LDAP_SSL="${DEFAULT_LDAP_SSL}" +else + LDAP_SSL="y" +fi + LDAP_URL= # shellcheck disable=SC2034 @@ -216,8 +246,13 @@ eval_ldap_options() { REMAINING_ARGS=() REMAINING_OPTS=() + debug "Evaluating LDAP options." + if [[ "$#" -gt 0 ]] ; then while true ; do + if [[ "$#" -le 0 ]] ; then + break + fi case "$1" in -D|--bind-dn) LDAP_USR="$2" diff --git a/scripts/get-last-login-times b/scripts/get-last-login-times new file mode 100755 index 0000000..34fd0fe --- /dev/null +++ b/scripts/get-last-login-times @@ -0,0 +1,223 @@ +#!/bin/bash + +set -e +set -u + + +BASE_NAME=$( basename "${0}" ) +MY_REAL_NAME=$( readlink -f $0 ) +BIN_DIR=$( dirname "${MY_REAL_NAME}" ) +BASE_DIR=$( dirname "${BIN_DIR}" ) +LIB_DIR="${BASE_DIR}/lib" +CONF_DIR="${BASE_DIR}/etc" + +DEFAULT_LDAP_USR="uid=readonly,ou=People,o=isp" +DEFAULT_LDAP_PWD_FILE="${HOME}/.private/dirsrv-prd-dpx-readonly-pwd-wonl.txt" +DEFAULT_LDAP_HOST="prd-ldap.pixelpark.com" + +if [[ -f "${BIN_DIR}/functions.rc" ]] ; then + . "${BIN_DIR}/functions.rc" +else + echo "Bash resource file '${BIN_DIR}/functions.rc' not found" >&2 + exit 5 +fi + +detect_color + +VERBOSE="n" +DEBUG="n" +BRIEF= + +DEFAULT_LAST_MONTHS="3" +LAST_MONTHS= +LAST_WEEKS= +LAST_DAYS= + +LAST_SECONDS=$(( DEFAULT_LAST_MONTHS * 30 * 24 * 3600 )) + +DESCRIPTION=$( cat <<-EOF + Searching for the last login time of accounts in Digitas LDAP." + + EOF +) + +export TZ='UTC' + +#------------------------------------------------------------------------------ +usage() { + cat <<-EOF + Usage: ${BASE_NAME} [Common Options] [LDAP Options] [[--months MONTHS] | [--weeks WEEKS] | [--days DAYS]] [-B|--brief] + ${BASE_NAME} [-h|--help] + ${BASE_NAME} [-V|--version] + + Optional Parameters: + --months MONTHS + Display all accounts, which last login time is older than MONTHS months. + This is the default - ${DEFAULT_LAST_MONTHS} months. + Mutually exclusive to --weeks and --days. + --weeks WEEKS + Display all accounts, which last login time is older than WEEKS weeks. + Mutually exclusive to --months and --days. + --days DAYS Display all accounts, which last login time is older than DAYS days. + Mutually exclusive to --months and --weeks. + -B|--brief Dont't display the list of all outdated accounts, show only the summary. + + LDAP Options: + EOF + + echo "${LDAP_USAGE_MSG}" + echo + echo " Common Options:" + echo "${STD_USAGE_MSG}" + +} + +#------------------------------------------------------------------------------ +eval_my_options() { + + debug "Evaluating my options." + + REMAINING_ARGS=() + REMAINING_OPTS=() + + if [[ "$#" -gt 0 ]] ; then + while true ; do + if [[ "$#" -le 0 ]] ; then + break + fi + case "$1" in + --months) + LAST_MONTHS="$2" + shift + shift + ;; + --weeks) + LAST_WEEKS="$2" + shift + shift + ;; + --days) + LAST_DAYS="$2" + shift + shift + ;; + -B|--brief) + BRIEF="y" + shift + ;; + --) shift + break + ;; + *) REMAINING_OPTS+=($1) + shift + ;; + esac + done + fi + + # debug "Checking for remaining ${CYAN}$#${NORMAL} opts: '$*'." + if [[ "$#" -gt "0" ]] ; then + error "Invalid positional arguments given: ${RED}$*${NORMAL}" + echo "" >&2 + echo -e "$( usage )" >&2 + exit 1 + fi + + # debug "Checking for REMAINING ${CYAN}${#REMAINING_OPTS[*]}${NORMAL} OPTS: '${REMAINING_OPTS[*]}'." + if [[ "${#REMAINING_OPTS[*]}" -gt 0 ]] ; then + error "Invalid options given: ${RED}${REMAINING_OPTS[*]}${NORMAL}" + echo "" >&2 + echo -e "$( usage )" >&2 + exit 1 + fi + + if [[ "${LAST_MONTHS}" ]] ; then + if [[ "${LAST_WEEKS}" || "${LAST_DAYS}" ]] ; then + error "Option '${RED}--months${NORMAL}' is mutually exclusive to '${RED}--weeks${NORMAL}' and '${RED}--days${NORMAL}'." + echo "" >&2 + echo -e "$( usage )" >&2 + exit 1 + fi + LAST_SECONDS=$( echo "${LAST_MONTHS} * 30 * 24 * 3600" | bc | sed -e 's/\..*//' ) + fi + + if [[ "${LAST_WEEKS}" ]] ; then + if [[ "${LAST_MONTHS}" || "${LAST_DAYS}" ]] ; then + error "Option '${RED}--weeks${NORMAL}' is mutually exclusive to '${RED}--months${NORMAL}' and '${RED}--days${NORMAL}'." + echo "" >&2 + echo -e "$( usage )" >&2 + exit 1 + fi + LAST_SECONDS=$( echo "${LAST_WEEKS} * 7 * 24 * 3600" | bc | sed -e 's/\..*//' ) + fi + + if [[ "${LAST_DAYS}" ]] ; then + if [[ "${LAST_MONTHS}" || "${LAST_WEEKS}" ]] ; then + error "Option '${RED}--days${NORMAL}' is mutually exclusive to '${RED}--months${NORMAL}' and '${RED}--weeks${NORMAL}'." + echo "" >&2 + echo -e "$( usage )" >&2 + exit 1 + fi + LAST_SECONDS=$( echo "${LAST_DAYS} * 24 * 3600" | bc | sed -e 's/\..*//' ) + fi + +} + +#------------------------------------------------------------------------------ +get_options() { + + local tmp= + local base_dir= + local short_options="${LDAP_STD_OPTS_SHORT}${STD_SHORT_OPTIONS}B" + local long_options="${LDAP_STD_OPTS_LONG},${STD_LONG_OPTIONS},months:,weeks:,days:,brief" + + set +e + tmp=$( getopt -o "${short_options}" --long "${long_options}" -n "${BASE_NAME}" -- "$@" ) + if [[ $? != 0 ]] ; then + echo "" >&2 + usage >&2 + exit 1 + fi + set -e + + # Note the quotes around `$TEMP': they are essential! + eval set -- "${tmp}" + eval_common_options "$@" + if [[ "${DEBUG}" == 'y' ]] ; then + declare -p REMAINING_OPTS + declare -p REMAINING_ARGS + fi + +# local -a rest_args_common=() +# for tmp in "${REMAINING_ARGS[@]}" ; do +# rest_args_common+=(${tmp}) +# done + + eval_ldap_options "${REMAINING_OPTS[@]}" "${REMAINING_ARGS[@]}" + + eval_my_options "${REMAINING_OPTS[@]}" -- "${REMAINING_ARGS[@]}" + + if [[ "${DEBUG}" == 'y' ]] ; then + declare -p REMAINING_OPTS + declare -p REMAINING_ARGS + fi + + info "Limit for becoming locked of an account is ${CYAN}${LAST_SECONDS} seconds${NORMAL}." + +} + +#------------------------------------------------ +main() { + + get_options "$@" + + empty_line + info "Finished." + +} + +main "$@" + +exit 0 + +# vim: et list filetype=sh