UserEmail

#!/bin/ksh
# create users for dovecot & smtpd email server without doing it manually! hope this helps out!
# note to users that have email server running with our guide that vusers is the virtuals is what the virtual    
# users is listed in table virtuals file:/etc/mail/virtuals on the guide setup for smtpd! vusers is what i got   
# them setup on nastycode vps!
# have any questions feel free to email me at support@nastycode.com for more help or catch me on irc on          # irc.nastycode.com on channel #nastycode
# make sure you chmode +x create_user befor you run the script with this command ./create_user

DOMAIN=nastycode.com
MAIL_DOMAIN=mail.nastycode.com
VMAIL_USER=vmail
VMAIL_ROOT=/var/vmail
VMAIL_UID="$(id -ru $VMAIL_USER)"
VMAIL_GID="$(id -rg $VMAIL_USER)"
MAIL_CONF_DIR=/etc/mail
MAIL_CONF=$MAIL_CONF_DIR/smtpd.conf
CERT_DIR="/etc/ssl"
PASSWD=$MAIL_CONF_DIR/passwd
DOMAINS=$MAIL_CONF_DIR/domains
HOSTS=$MAIL_CONF_DIR/hosts
USERS=$MAIL_CONF_DIR/users
VUSERS=$MAIL_CONF_DIR/vusers
ALIASES=$MAIL_CONF_DIR/aliases
DOVECOT=/etc/dovecot/users.txt

export MAIL_CONF_DIR MAIL_CONF CERT_DIR ALIASES PASSWD DOMAINS HOSTS USERS VUSERS DOVECOT VMAIL_USE
prompt() {
    prompt="$1"
    default="$2"
    result="$3"
    echo "${PURPLE}${BOLD}$prompt [$default] ${NORM}\c"
    read "$result"
    [ -z "$(eval echo \$$result)" ] && eval "$result=$default"
}

prompt_bool() {
    prompt="$1"
    default="$2"
    echo "${PURPLE}${BOLD}$prompt (y/n) [$default] ${NORM}\c"
    read res
    case "${res:-$default}" in
        y | yes | Y | YES) return 0;;
    esac
    return 1
}

prompt_password() {
    prompt="$1"
    result="$2"
    echo "${PURPLE}${BOLD}$prompt ${NORM}\c"
    stty -echo
    read "$result"
    stty echo
    echo
}

panic() {
    msg="$1"
    echo "${RED}${BOLD}$msg${NORM}"
    exit 1
}

log_file() {
    exec 3>&1
    tee /dev/fd/3 | perl -pe 's/\033[^m]+m//g' >>"$1" # strip control characters
    exec 3>&-
}

postinstall() {
    log_file "$POSTINSTALL"
}

log() {
    log_file "$LOGS"
}

# Colors
RED="\033[0;31m"
YELLOW="\033[0;33m"
BOLD="\033[1m"
PURPLE="\033[0;35m"
GREEN="\033[0;32m"
NORM="\033[0m"
export RED YELLOW BOLD PURPLE GREEN NORM

[ -z "$USER_NAME" ] && USER_NAME="$(whoami)"
[ -z "$DOMAIN_NAME" ] && DOMAIN_NAME="nastycode.com"
[ -z "$MAIL_DOMAIN" ] && MAIL_DOMAIN="mail.nastycode.com"
[ -z "$VPN_DOMAIN" ] && VPN_DOMAIN="vpn.nastycode.com"
export USER_NAME DOMAIN_NAME MAIL_DOMAIN VPN_DOMAIN

[ -z "$POSTINSTALL" ] && POSTINSTALL="$(dirname $0)/post-install.txt"
[ -z "$LOGS" ] && LOGS="$(dirname $0)/logs.txt"
export POSTINSTALL LOGS

# Creates a virtual user for mail
# Usage: create_user <username> [<password>]
# Environment: DOMAIN_NAME, VMAIL_USER, VMAIL_UID, VMAIL_GID, VMAIL_ROOT, 

username="$1"
password="$2"
[ -z "$username" ] && prompt "Specify a user" "$USER_NAME " username
[ -z "$username" ] && panic "Username cannot be empty"

[ -z "$password" ] && prompt_password "Enter password for user $username:" password
[ -z "$password" ] && panic "Password cannot be empty"
encrypted_password=$(smtpctl encrypt "$password")
unset password

echo "${YELLOW}Creating user $username for domain $DOMAIN_NAME${NORM}"

echo "${username}@${DOMAIN_NAME}:${encrypted_password}" | doas tee -a "$PASSWD" >/dev/null
echo "${username}@${DOMAIN_NAME}:${encrypted_password}" | doas tee -a "/etc/dovecot/users.txt" >/dev/null
echo "${username}@${DOMAIN_NAME} $VMAIL_USER" | doas tee -a "$VUSERS" >/dev/nullecho "${username}@${DOMAIN_NAME} $VMAIL_USER" | doas tee -a "$VUSERS" >/dev/null
echo "${username}@${DOMAIN_NAME}: "${username}@${DOMAIN_NAME} | doas tee -a "$USERS" >/dev/null

doas rcctl reload dovecot || panic "Failed to reload dovecot"
doas smtpctl update table domains || panic "Failed to update table 'domains'"
doas smtpctl update table passwd || panic "Failed to update table 'passwd'"
doas smtpctl update table users || panic "Failed to update table 'users'"
doas smtpctl update table vusers || panic "Failed to update table 'vusers'"
doas smtpctl update table hosts || panic "Failed to update table 'hosts'"
doas rcctl restart dovecot smtpd relayd httpd unbound

echo "${GREEN}User created${NORM}"