#!/usr/bin/sh #---------------------------------------------------------------------------- # /var/install/bin/apache2-config-modules-webalizer-webalizer-web-user # # Creation: 2014-03-23 hb # Last Update: $Id$ # # Copyright (c) 2014-@@YEAR@@ Holger Bruenjes, holgerbruenjes(at)gmx(dot)net # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #---------------------------------------------------------------------------- # include eislib . /var/install/include/eislib # include config . /etc/config.d/apache2 . /etc/config.d/webalizer # include passwdlib . /var/install/include/passwdlib #debug=true if ${debug:-false} then exec 2>/tmp/$(basename ${0})-trace$$.log set -x ask_debug=true export ask_debug fi ASK="/var/install/bin/ask" CHOOSE='/var/install/bin/choose' MKTEMP='/usr/bin/mktemp' webalizer_path='/srv/www/webalizer' # set echo line, for allways equal echo_line=" ----------------------------" # --------------------------------------------------------------------------- # select project # --------------------------------------------------------------------------- select_project() { clrhome mecho --info "Please select the project to change the user settings," echo AW_ROWS=0 if [ "${WEBALIZER_VHOSTS_RUN_ALL}" = 'yes' ] then vhost_ac_auth=${WEBALIZER_VHOSTS_ACCESS_AUTH} if [ "${vhost_ac_auth}" = "yes" ] then idx=1 while [ ${idx} -le ${APACHE2_VHOST_N:-0} ] do eval active='${APACHE2_VHOST_'${idx}'_ACTIVE}' if [ "${active}" != "yes" ] then idx=$((${idx} + 1)) continue fi AW_ROWS=$( expr ${AW_ROWS} + 1 ) eval srv_${AW_ROWS}='"${APACHE2_VHOST_'${idx}'_SERVER_NAME}"' eval alias_${AW_ROWS}="${WEBALIZER_VHOSTS_ALIAS}" eval srv='"${APACHE2_VHOST_'${idx}'_SERVER_NAME}"' eval alias="${WEBALIZER_VHOSTS_ALIAS}" if [ -z "${active_project}" ] then active_project="${srv}:${alias}" else active_project=$( echo "${active_project}"; \ echo "${srv}:${alias}" ) fi idx=$((${idx} + 1)) done fi fi idx=1 while [ ${idx} -le ${WEBALIZER_HOST_N:-0} ] do eval active='${WEBALIZER_HOST_'${idx}'_ACTIVE}' if [ "${active}" != "yes" ] then idx=$((${idx} + 1)) continue fi AW_ROWS=$((${AW_ROWS} + 1)) eval srv_${AW_ROWS}='"${WEBALIZER_HOST_'${idx}'_HOST_NAME}"' eval alias_${AW_ROWS}='"${WEBALIZER_HOST_'${idx}'_ALIAS}"' eval srv='"${WEBALIZER_HOST_'${idx}'_HOST_NAME}"' eval alias='"${WEBALIZER_HOST_'${idx}'_ALIAS}"' if [ -z "${active_project}" ] then active_project="${srv}:${alias}" else active_project=$( echo "${active_project}"; \ echo "${srv}:${alias}" ) fi idx=$((${idx} + 1)) done techo --begin '3 3r 30 10' techo --row "" "" --info Hostname --info alias mecho "${echo_line}" idx=1 echo "${active_project}" | while read line do _srv="$( echo "${line}" | awk -F':' '{print $1}' )" _alias="$( echo "${line}" | awk -F':' '{print $2}' )" techo --row "" "${idx}." "${_srv}" "${_alias}" idx=$((${idx} + 1)) done mecho "${echo_line}" techo --end echo _ask_tmpfile=$(${MKTEMP} -t .XXXXXXXXXXXXX) ${ASK} "Select" "" "1-${AW_ROWS}" "^$=Return" "0=Exit" >${_ask_tmpfile} rc=${?} read answer < ${_ask_tmpfile} rm -f ${_ask_tmpfile} # if ask break, ask returned 255 if [ ${rc} = 255 ] then answer=0 fi case ${answer} in '') exit 0 ;; 0) exit 127 ;; *) eval server='$srv_'${answer} eval alias='$alias_'${answer} _selected_project="${server}.${alias}" _show_selected_project="${server}/${alias}" ;; esac select_to_work } # --------------------------------------------------------------------------- # select to work # --------------------------------------------------------------------------- select_to_work() { clrhome # entry:action:message:para action_entry="show user:user_list:yes add user:user_action:Add user to:-b remove user:user_action:Remove user from:-D alter password:user_action:Alter password for user from:-a" mecho "Please select the action," mecho -n "for project " mecho --info "'${_show_selected_project}'" echo techo --begin '3 3r 2 32' techo --row "" --info No "" --info Action mecho "${echo_line}" idx=1 echo "${action_entry}" | while read line #entry action message para do entry="$( echo "${line}" | awk -F':' '{print $1}' )" techo --row "" "${idx}." "" "${entry}" idx=$((${idx} + 1)) done mecho "${echo_line}" techo --end echo _ask_tmpfile=$(${MKTEMP} -t .XXXXXXXXXXXXX) ${ASK} "Select" "" "1-$(echo "${action_entry}" | wc -l)" "^$=Return" "0=Exit" >${_ask_tmpfile} rc=${?} read action_to_do < ${_ask_tmpfile} rm -f ${_ask_tmpfile} if [ ${rc} = 255 ] then action_to_do=0 fi case ${action_to_do} in '') active_project='' select_project ;; 0) exit 0 ;; *) # get action line action_line="$(echo "${action_entry}" | sed -n "${action_to_do}p" | sed 's/^ *//')" action="$( echo "${action_line}" | awk -F':' '{print $2}' )" message="$( echo "${action_line}" | awk -F':' '{print $3}' )" para="$( echo "${action_line}" | awk -F':' '{print $4}' )" # user_action # user_list # ${2} ${3} ${4} ${action} "${message}" "${para}" ;; esac } # --------------------------------------------------------------------------- # user add, remove or alter password to htpasswd file # --------------------------------------------------------------------------- user_action() { action_string="${1}" action_flag="${2}" # write existing user to tty, without answer_line user_list "no" echo mecho -n "${action_string} project " mecho --info "'${_show_selected_project}'" echo _ask_tmpfile=$(${MKTEMP} -t XXXXXXXXXXXXX) ${ASK} "Name of user (ENTER=Return, 0=Exit)" "" "*" >${_ask_tmpfile} rc=${?} read UserName < ${_ask_tmpfile} rm -f ${_ask_tmpfile} if [ ${rc} = 255 ] then UserName=0 fi # if ! echo "${action_string}" | grep -q "Remove" # then _used_project=${_selected_project} # fi case ${UserName} in '') select_to_work ;; 0) exit 0 ;; *) # add, remove or alter password select_pw_action ${action_flag} \ --file ${webalizer_path}/${_selected_project}.htpasswd \ --realm ${_used_project} \ --user ${UserName} # set rights # chown wwwrun ${trac_path}/${_selected_project}.htpasswd # chmod 0600 ${trac_path}/${_selected_project}.htpasswd ;; esac } # --------------------------------------------------------------------------- # user list # --------------------------------------------------------------------------- user_list() { answer_line=${1} clrhome # write user list to tty mecho -n "Existing user in project " mecho --info "'${_show_selected_project}'" echo # read project password file techo --begin '3 32' techo --row "" --info User mecho "${echo_line}" cat ${webalizer_path}/${_selected_project}.htpasswd | sort | while read line #user passwd do user="$( echo "${line}" | awk -F':' '{print $1}' )" passwwd="$( echo "${line}" | awk -F':' '{print $2}' )" techo --row "" "${user}" done mecho "${echo_line}" techo --end if [ ${answer_line} != no ] then echo _ask_tmpfile=$(${MKTEMP} -t .XXXXXXXXXXXXX) ${ASK} "" "" "^$=Return" "0=Exit" >${_ask_tmpfile} rc=${?} read user_line < ${_ask_tmpfile} rm -f ${_ask_tmpfile} if [ ${rc} = 255 ] then user_line=0 fi case ${user_line} in '') select_to_work ;; 0) exit 0 ;; esac fi } # --------------------------------------------------------------------------- # select passwort action # --------------------------------------------------------------------------- select_pw_action() { _create_pw_file='' _batch='' _alter='' _remove='' _entry_line='' _realm='' _pw_file='' _user='' while [ ${#} -gt 0 ] do case "${1}" in -c) _create_pw_file=true shift ;; -b) _batch=true shift ;; -a) _alter=true shift ;; -D) _remove=true shift ;; -e|--entry) _entry_line="${2}" shift 2 ;; -r|--realm) _realm="${2}" shift; shift ;; -f|--file) _pw_file="${2}" shift; shift ;; -u|--user) _user="${2}" shift; shift ;; esac done if ! ${_remove:-false} then _ask_tmpfile=$(${MKTEMP} -t .XXXXXXXXXXXXX ) ${ASK} "Create password with pwgen" "yes" >${_ask_tmpfile} rc=${?} read _passwd_pwgen < ${_ask_tmpfile} ${RM} -f ${_ask_tmpfile} if [ ${rc} = 255 ] then exit 127 fi if [ "${_passwd_pwgen}" = "yes" ] then # entry in passwdlib # create password # pwgen --help # return password get_pwgen_passwd else # entry in passwdlib # get password from cmdline get_passwd --check fi # entry in passwdlib # create the password-file entry line # return _passwd_entry get_digest_entry --user ${UserName} \ --realm ${_selected_project} \ --passwd ${password} fi # entry in passwdlib set_passwd ${action_flag} \ --user ${_user} \ --file ${_pw_file} \ --entry ${_passwd_entry:-dummy} if ! ${_remove:-false} then if [ -n "${WEBALIZER_ACCESS_AUTH_SEND_MAIL_FROM}" ] then _ask_tmpfile=$(${MKTEMP} -t .XXXXXXXXXXXXX) ${ASK} "Send e-mail to user" "${_user}" "*" >${_ask_tmpfile} rc=${?} read _send_mail < ${_ask_tmpfile} ${RM} -f ${_ask_tmpfile} if [ ${rc} = 255 ] then exit 127 fi # entry in passwdlib send_passwd --user ${UserName} \ --realm ${_show_selected_project} \ --passwd ${password} \ --mail ${_send_mail:-root} \ --sender ${WEBALIZER_ACCESS_AUTH_SEND_MAIL_FROM:-root} fi fi } # --------------------------------------------------------------------------- # main # --------------------------------------------------------------------------- while true do clrhome active_project='' select_project echo _ask_tmpfile=$(${MKTEMP} -t .XXXXXXXXXXXXX) ${ASK} "One more action?" "no" > ${_ask_tmpfile} rc=${?} read more_action < ${_ask_tmpfile} rm -f ${_ask_tmpfile} if [ ${rc} = 255 ] then more_action=no fi case ${more_action} in no) exit 0 ;; esac done exit 0 # --------------------------------------------------------------------------- # end # ---------------------------------------------------------------------------