#!/bin/sh # # qmail-manage.sh - handles qmail's mail users and mailboxes # # Developed by Ondrej Jombik # Copyright (c) 2003 Platon SDG, http://platon.sk/ # Licensed under terms of GNU General Public License. # All rights reserved. # # Changelog: # 20/05/2003 - created # 28/05/2003 - support for startup()/finish() functions # - virtualdomains and rcpthosts are handled # 16/06/2003 - option --help implemented # - extended command line parsing with --alias option # - local aliases implemented # 17/06/2003 - external aliases implemented # # $Platon: scripts/shell/sysadmin/qmail-manage.sh,v 1.5 2003/06/19 09:11:48 nepto Exp $ # # SYSTEM COMMANDS CONFIGURATION # ECHO="/bin/echo"; CAT="/bin/cat"; MV="/bin/mv"; GREP="/usr/bin/grep"; SED="/usr/bin/sed"; HTPASSWD="/usr/local/sbin/htpasswd"; MAILDIRMAKE="/var/qmail/bin/maildirmake"; PRG="` echo \"$0\" | sed 's/^.*\/\([^\/]*\)$/\1/g' `"; # # FILES, PATHS AND DIRECTORIES # file_temp="/tmp/$PRG"; file_default_dotqmail="/var/qmail/users/qmail"; file_virtualdomains="/var/qmail/control/virtualdomains"; file_rcpthosts="/var/qmail/control/rcpthosts"; DIR_DOMAINS="/var/qmail/domains"; DIR_PASSWORDS="/var/qmail/domains.pw"; DIR_MAILBOX="/usr/mailbox"; # # FUNCTIONS # startup() # modify this function to fit your needs { # nothing } finish() # modify this function to fit your needs { /var/qmail/users/passwordrehash; /var/qmail/users/domainsrehash; } # # ***** DO NOT ALTER ANYTHING BELOW THIS LINE ***** # usage() { $ECHO "$PRG [ --help ] [ --alias ] "; exit 1; } error() { $ECHO "$PRG: ERROR: $1"; exit 2; } warning() { $ECHO "$PRG: WARNING: $1"; } notice() { $ECHO "$PRG: NOTICE: $1"; } # check param count if [ $# -lt 1 ]; then usage; fi # command line parsing for option; do # If the previous option needs an argument, assign it. if test -n "$prev"; then eval "$prev=\$option"; prev=; continue; fi case "$option" in -*=*) optarg=`echo "$option" | sed 's/[-_a-zA-Z0-9]*=//'` ;; *) optarg= ;; esac case "$option" in --alias | --alia | --ali | --al | --a | \ -alias | -alia | -ali | -al | -a) prev='alias' ;; --alias=* | --alia=* | --ali=* | --al=* | --a=* | \ -alias=* | -alia=* | -ali=* | -al=* | -a=*) alias="$optarg" ;; --help | --hel | --he | --h | -help | -hel | -he | -h) usage; ;; -*) error "invalid option '$option'; use --help to show usage" 1>&2; exit 1; ;; *) # get e-mail and password if [ -z "$email" ]; then email="$option"; else if [ -z "$password" ]; then password="$option"; else error "too many parameter on command line"; fi fi ;; esac done # alias flag alias_f=''; if [ "$alias" ]; then alias_f='1'; fi # check if e-mail address seems to be valid if [ "` $ECHO \"$email\" | $GREP -E '^[^@ ]+@[^@ ]+\.[^@ ]+$' `" ]; then # do nothing else error "bad e-mail address"; fi # check if alias e-mail address seems to be valid if [ $alias_f ]; then if [ "` $ECHO \"$alias\" | $GREP -E '^[^@ ]+@[^@ ]+\.[^@ ]+$' `" ]; then # do nothing else error "bad alias e-mail address"; fi fi # call startup() function startup; # extract username, domain, domain2, password2 and mailbox # - domain2 has last point substitued for dash # - password is encrypted password username="` $ECHO \"$email\" | $SED 's/@.*$//g' `"; domain="` $ECHO \"$email\" | $SED 's/^.*@//g' `"; domain2="` $ECHO \"$domain\" | $SED 's/^\(.*\)\.\([^\.]*\)$/\1-\2/g' `"; password2="` $HTPASSWD -nb DUMMY \"$password\" | sed 's/DUMMY://g' `"; mailbox="$DIR_MAILBOX/$domain2/$email"; qmail_key="=$domain2-$username"; alias_username="` $ECHO \"$alias\" | $SED 's/@.*$//g' `"; alias_domain="` $ECHO \"$alias\" | $SED 's/^.*@//g' `"; alias_qmail_key="=$domain2-$alias_username"; # sanity check if [ "$domain" = "$domain2" ]; then error 'something went wrong, $domain is equal to $domain2'; fi # alias sanity check and type extraction if [ $alias_f ]; then if [ "$alias_username" = "$username" ]; then error 'alias username is equal to e-mail username'; fi if [ "$alias_domain" != "$domain" ]; then alias_f="E"; # external alias else alias_f="L"; # local alias fi fi # virtualdomains and rcpthosts file lines #pokus.sk:pokus-sk virtualdomains_file_line="$domain:$domain2"; #pokus.sk rcpthosts_file_line="$domain"; # check if domain is already in virtualdomains file if [ "` grep -E \"^$virtualdomains_file_line\$\" \"$file_virtualdomains\" `" ]; then notice "domain already written in virtualdomains"; else notice "domain not written in virtualdomains, appended"; echo "$virtualdomains_file_line" >> $file_virtualdomains; fi # check if domain is already in rcpthosts file if [ "` grep -E \"^$rcpthosts_file_line\$\" \"$file_rcpthosts\" `" ]; then notice "domain already written in rcpthosts"; else notice "domain not written in rcpthosts, appended"; echo "$rcpthosts_file_line" >> $file_rcpthosts; fi # check if domain file already exists if [ -f "$DIR_DOMAINS/$domain" ]; then notice "domain file already exists for '$domain'"; $CAT "$DIR_DOMAINS/$domain" | $GREP -v "^$qmail_key:" > $file_temp; $MV $file_temp "$DIR_DOMAINS/$domain"; else notice "domain file does not exist for '$domain', created"; >"$DIR_DOMAINS/$domain"; fi # check if password file already exists if [ -f "$DIR_PASSWORDS/$domain" ]; then notice "password file already exists for '$domain'"; $CAT "$DIR_PASSWORDS/$domain" | $GREP -v "^$email:" > $file_temp; $MV $file_temp "$DIR_PASSWORDS/$domain"; else notice "password file does not exist for '$domain', created"; >"$DIR_PASSWORDS/$domain"; fi # if we are making an local alias, get appropriate information if [ "$alias_f" = "L" ]; then notice "extracting real mailbox path for local alias"; mailbox="` $GREP "^$alias_qmail_key:" "$DIR_DOMAINS/$domain" \ | sed 's/^[^:]*:[^:]*:[^:]*:[^:]*:\(.[^:]*\):.*$/\1/g' `"; if [ "X$mailbox" = "X" ]; then warning "aliased mailbox not found, however alias was created"; fi fi # domain and password file lines #=pokus-sk-ondrej.jombik:mailbox:888:888:/usr/mailbox/pokus-sk/ondrej.jombik@pokus.sk::: domain_file_line="$qmail_key:mailbox:888:888:$mailbox:::"; #ondrej.jombik@pokus.sk:9tWbzYa9hWOGY:mailbox:/usr/mailbox/pokus-sk/ondrej.jombik@pokus.sk password_file_line="$email:$password2:mailbox:$mailbox"; echo "$domain_file_line" >> "$DIR_DOMAINS/$domain"; if [ ! $alias_f ]; then echo "$password_file_line" >> "$DIR_PASSWORDS/$domain"; fi # nastavit domenovy subor a passwordovy subor vytvorit adresar # /usr/mailbox/domena/usermailbox v nom vytvorit Maildir # (/var/qmail/bin/maildirmake) a do toho isteho adresara nakopirovat # defaultny .qmail subor ktory bude mat niekde zadefinovanu cestu.. if [ "$alias_f" = "L" ]; then # do nothing else if [ ! -d "$mailbox" ]; then # if this fails we have a really big problem... mkdir -p "$mailbox" || error "mailbox '$mailbox' creation failure"; cd "$mailbox" \ && $MAILDIRMAKE ./Maildir/ \ && cd - \ && notice "mailbox '$mailbox' was created" \ && cp -f "$file_default_dotqmail" "$mailbox/.qmail" 2>/dev/null fi; if [ "$alias_f" = "E" ]; then echo "$alias" > "$mailbox/.qmail"; fi fi # call finish() function finish; if :; then else # just comment this line to enable debug information # debug print echo "email: $email"; echo "alias: $alias"; echo "password: $password"; echo "password2: $password2"; echo "username: $username"; echo "domain: $domain"; echo "domain2: $domain2"; echo -e "domain_file_line:\n$domain_file_line"; echo -e "password_file_line:\n$password_file_line"; fi