Hi all,

Here's another dialog based management tool for dbmail inspired by Alejandro's posting of dbmail.sh yesterday. I decided to follow up on it by writing my own version to satisfy my own tastes, needs and coding style, and probably be useful to some of you as well.

It doesn't require postfix to run. Nor does it try to be smart about multiple domain issues. I'm not convinced those issues belong in dbmail proper given that a dbmail instance might well be on a host other than the relavant mta host. It does however try to give a friendlier face to all aspects of dbmail-adduser and dbmail-maintenance.


Hope you like it.

give it a whirl if you please :-)


--
  ________________________________________________________________
  Paul Stevens                                  mailto:[EMAIL PROTECTED]
  NET FACILITIES GROUP                     PGP: finger [EMAIL PROTECTED]
  The Netherlands________________________________http://www.nfg.nl
#!/bin/sh

# dbmailctl 
#
# Simple dialog based wrapper for the dbmail-adduser usermanagement 
# utility for dbmail (www.dbmail.org)
#
# Licence: GPL (www.gnu.org)
# Author: Paul J Stevens <[EMAIL PROTECTED]>
#
# (c) 2003, NFG Net Facilities Group BV, Utrecht NL. 
# 
# $Id: dbmailctl,v 1.1.2.10 2003/03/13 23:38:01 paul Exp $
umask 0266

LOGFILE="/var/log/dbmail/dbmailctl.log"

[ -d `dirname "$LOGFILE"` ] || install -d -m 0700 -o root -g root `dirname 
$LOGFILE` &>/dev/null
[ -e "$LOGFILE" ] || install -m 0600 -o root -g root /dev/null $LOGFILE 
&>/dev/null

# some basic helpers
call() {
        _cmd="$*"
        dolog "$_cmd"
        eval "$_cmd"
}       
dolog() {
        # record all transactions
        [ -w "$LOGFILE" ] || abort "$LOGFILE in unwriteable"
        _msg="$*"
        now=`date +"%b %d %H:%M:%S"`
        echo "$now $HOST "`basename $0`"[$$] $_msg" >> $LOGFILE
}
abort() {
        echo "ABORT: $*"; exit 
}
simple_password() {
        # very weak password (more like a PIN)
        tmpfile=`tempfile`
        date +%N > $tmpfile; md5sum $tmpfile|sed 's/[0-9]//g'|cut -b -4
        rm -f $tmpfile
}

init_password() {
        # select a nice password generator.
        # first sucessfull one wins, fallback to
        # internal gobbledeegook generator.
        call "pwgen -nac 7 1" 2>/dev/null && return
        call "mkpasswd --chars 6" 2>/dev/null && return
        # fallback
        simple_password
}       

## dialog GUI functions
set_title() { 
        export TITLE="$*" 
}
get_dialog_opts() {
        OPTS="--backtitle \"DBMAIL Management\" --cr-wrap"
        [ -n "$TITLE" ] && OPTS="$OPTS --title \"$TITLE\""
        export OPTS
}       
# widgets
input() {
        _var=$1; shift; _msg="$*"
        get_dialog_opts
        result=`tempfile`
        eval "old_var=\$$_var"
        eval "dialog $OPTS --inputbox \"\n$_msg\n\" 10 70 \"$old_var\"" 
2>$result || return 1
        _new=`cat $result`
        eval "$_var=\"$_new\""
        rm -f $result
}       
confirm() {
        _msg="$*"
        get_dialog_opts
        [ -e "$_msg" ] && _msg=`cat $_msg`
        eval "dialog $OPTS --yesno \"\n$_msg\n\" 10 70"
        return $?
}       
message() {
        _msg="$*"
        get_dialog_opts
        boxtype="--msgbox \"\n$_msg\n\""
        [ -e "$_msg" ] && boxtype="--tailbox $_msg"
        eval "dialog $OPTS $boxtype 25 70"
}
menu() {
        _var=$1; shift; 
        _opt=""
        _idx=1
        eval "text=\$TEXT"
        while `true`; do
                label="$1"; shift
                [ -n "$label" ] || break
                help=""
                eval "help=\$HELP_$label"
                _opt="$_opt $_idx $label \"$help\""
                eval "item[$_idx]=$label"
                let _idx=_idx+1
        done    
        get_dialog_opts
        result=`tempfile`
        eval "dialog --item-help $OPTS --menu \"\n$text\n\" 10 70 0 $_opt" 
2>$result || return 1
        _sel=`cat $result`
        rm -f $result
        eval "$_var=${item[$_sel]}"
}

## DBMAIL
main_menu() {
        dolog "Start dbmailctl session: `whoami`"
        dbmail-adduser quiet s &>/dev/null || abort "Unable to communicate with 
the dbmail server"
        HELP_maintenance="Do integrity checking and cleaning up of tables"
        HELP_users="Perform all sorts of user maintenance"
        while `true`; do
                TITLE="DBMAIL Main Menu"
                TEXT="Welcome to dbmailctl"
                menu "manage" maintenance users || break
                eval "manage_$manage"
        done
        dolog "Finish dbmailctl session: `whoami`"
}
manage_maintenance() {
        HELP_vacuum="performs internal cleaning-up on the tables"
        HELP_integrity="checks integrity"
        HELP_repair="checks integrity and tries to fix problems"
        HELP_purge="purge messages have the DELETE status set"
        HELP_delete="set DELETE status for deleted messages"
        while `true`; do
                TITLE="DBMAIL Maintenance"
                TEXT=$HELP_maintenance
                menu "maintain" vacuum integrity repair purge delete || return 1
                eval "maintain_$maintain" || return 1
        done
}       
maintain_vacuum() {
        tmpfile=`tempfile`
        TITLE=$HELP_vacuum
        dbmail-maintenance -c &> $tmpfile
        message $tmpfile
}
maintain_integrity() {
        tmpfile=`tempfile`
        TITLE=$HELP_integrity
        dbmail-maintenance -i &> $tmpfile
        message $tmpfile
}
maintain_repair() {
        tmpfile=`tempfile`
        TITLE=$HELP_repair
        dbmail-maintenance -f &> $tmpfile
        message $tmpfile
}
maintain_purge() {
        tmpfile=`tempfile`
        TITLE=$HELP_purge
        dbmail-maintenance -p &> $tmpfile
        message $tmpfile
}
maintain_delete() {
        tmpfile=`tempfile`
        TITLE=$HELP_delete
        dbmail-maintenance -d &> $tmpfile
        message $tmpfile
}

manage_users() {
        HELP_add="add a user"
        HELP_edit="change user attributes"
        HELP_delete="remove a user and associated mailboxes"
        while `true`; do
                TITLE="DBMAIL Usermanagement"
                TEXT=$HELP_users
                menu "users" add edit delete || break
                eval "users_$users"
        done
}
users_add() {
        username=""
        aliases=""
        password=`init_password`
        clientid=0; maxmail=0
        while `true`; do
                while `true`; do
                        input username "Username" || return 1
                        valid_username "$username" && break
                        message "$username is already in use"
                done    
                input password "Password" || return 1
                input clientid "Client id" || return 1
                input maxmail "Email quota in MB. Use 0 for no quota limit." || 
return 1
                maxmail=`echo $maxmail|sed 's/[ [:alpha:]]//g'`M
                input aliases "Email address" || return 1
                msg=`tempfile`
                cat >> $msg << EOM

-----------------------------------------------------------------
confirm dbmail user setup:
-----------------------------------------------------------------
login    : $username
password : $password
clientid : $clientid
maxmail  : $maxmail
aliases  : $aliases
-----------------------------------------------------------------

EOM
                confirm $msg && break
                confirm "re-edit ?" && continue
                return 1
        done
        call dbmail-adduser quiet a "$username" "{crypt:}$password" "$clientid" 
"$maxmail" "$aliases"
        case "$?" in
                0) message "user succesfully added"; break;;
                1) message "adding user failed"; break;;
        esac
        return 0
}

users_edit() {
        user_select
        [ -n "$username" ] || return 1
        HELP_username="change username"
        HELP_password="change password"
        HELP_clientid="change clientid"
        HELP_maxmail="change maximum mail quotum"
        HELP_aliases="change aliases"
        while `true`; do
                TITLE="Manage user"
                TEXT=$HELP_edit
                menu "attributes" username password clientid maxmail aliases || 
break
                eval "edit_${attributes} $username" || return 1
        done
}
users_delete() {
        user_select
        [ -n "$username" ] || return 1
        confirm "empty mailboxes owned by user: $username?" && call 
dbmail-adduser quiet e "$username"
        confirm "delete user: $username?" && call dbmail-adduser quiet d 
"$username"
}       



edit_username() {
        username="$1"
        oldname=$username
        while `true`; do
                input username "new username" || return 1
                valid_username "$username" && break
                message "$username already in use"
        done
        call dbmail-adduser quiet c "$oldname" -u "$username"
}
edit_password() {
        username="$1"
        password=`init_password`
        input password "new password" || return 1
        call dbmail-adduser quiet c "$username" +p "$password"
}
edit_clientid() {
        username="$1"
        clientid=`get_client_id $username`
        input clientid "new clientid" || return 1
        call dbmail-adduser quiet c "$username" -c "$clientid"
        
}
edit_maxmail() {
        username="$1"
        maxmail=`get_max_mail $username`
        input maxmail "new mail quota in MB" || return 1
        maxmail=`echo $mailmail | sed 's/[ [:alpha:]]//g'`
        call dbmail-adduser quiet c "$username" -q "${maxmail}M"
}
edit_aliases() {
        username="$1"
        HELP_add="add alias for $username"
        HELP_delete="Remove alias for $username"
        while `true`; do
                TITLE="Manage aliases"
                TEXT=$HELP_aliases
                menu "aliases" add delete || break
                eval "alias_$aliases $username"
        done    
}               
alias_add() {
        username="$1"
        input newalias "new alias for $username" || return 1
        call dbmail-adduser quiet c "$username" +a "$newalias"
}
alias_delete() {
        username="$1"
        menu "delete_alias" `get_aliases $username` || return 1
        confirm "delete address [$delete_alias] for $username ?" || return 1
        call dbmail-adduser quiet c "$username" -a "$delete_alias"
}

user_select() {
        username=""
        TEXT="Select a username"
        menu "username" `get_user_list` || return 1
}
        
# some query functions
get_user_list() {
        dbmail-adduser s | tail +7 | sed 's/\[\(.*\)\]/\1/g'
}
get_client_id() {
        username="$1"
        dbmail-adduser s $username | grep -i 'client id' | cut -f2 -d: | sed 
's/ //g'
}
get_max_mail() {
        username="$1"
        dbmail-adduser s "$username" | grep -i '^max.' | cut -f2 -d: | sed 's/[ 
[:alpha:]]//g'
}
get_aliases() {
        username="$1"
        dbmail-adduser s "$username" | sed --silent '/^Aliases/,//p' | tail 
+2|grep -v '^$'
}

valid_username() {
        username="$1"
        err_username=""
        for u in `get_user_list`; do
                if [ "$u" == "$username" ]; then
                        err_username="$username"
                        break;
                fi
        done
        [ -n "$err_username" ] && return 1
        return 0
}       


[ `basename $0` == "dbmailctl" ] && main_menu

exit 0

# gui testcode
menu "simple" itemA itemB itemC
name="test"; input name "new name"
confirm "show file?" && file="$0"; message $file

#EOF

Reply via email to