201 lines
5.0 KiB
Bash
Executable File
201 lines
5.0 KiB
Bash
Executable File
#!/bin/bash
|
|
#
|
|
# vmail-stack
|
|
# https://git.stack-source.com/msb/vmail-stack
|
|
# MIT License Copyright (c) 2021 Matthew Saunders Brown
|
|
#
|
|
# vmail include file, used by other vmail bash scripts
|
|
|
|
# any script that includes this conf file will force user to be vmail
|
|
if [ "$USER" != "vmail" ]; then
|
|
exec sudo -u vmail --shell /bin/bash $0 $@
|
|
fi
|
|
|
|
# constants
|
|
readonly VMAIL_DIR=/var/vmail
|
|
readonly MYSQL_CONNECTION_INFO_FILE=$VMAIL_DIR/.my.cnf
|
|
|
|
# check that MYSQL_CONNECTION_INFO_FILE exists and is readable
|
|
if [ ! -f "$MYSQL_CONNECTION_INFO_FILE" ]; then
|
|
echo "ERROR: MySQL connection info file ($MYSQL_CONNECTION_INFO_FILE) does not exist or is not readable."
|
|
exit
|
|
fi
|
|
|
|
# functions
|
|
|
|
# crude but good enough domain name format validation
|
|
function vmail::validate_domain () {
|
|
local my_domain=$1
|
|
if [[ $my_domain =~ ^(([a-zA-Z0-9](-?[a-zA-Z0-9])*)\.)+[a-zA-Z]{2,}$ ]] ; then
|
|
return 0
|
|
else
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# yesno prompt
|
|
#
|
|
# Examples:
|
|
# loop until y or n: if vmail::yesno "Continue?"; then
|
|
# default y: if vmail::yesno "Continue?" Y; then
|
|
# default n: if vmail::yesno "Continue?" N; then
|
|
function vmail::yesno() {
|
|
|
|
local prompt default reply
|
|
|
|
if [ "${2:-}" = "Y" ]; then
|
|
prompt="Y/n"
|
|
default=Y
|
|
elif [ "${2:-}" = "N" ]; then
|
|
prompt="y/N"
|
|
default=N
|
|
else
|
|
prompt="y/n"
|
|
default=
|
|
fi
|
|
|
|
while true; do
|
|
|
|
read -p "$1 [$prompt] " -n 1 -r reply
|
|
|
|
# Default?
|
|
if [ -z "$reply" ]; then
|
|
reply=$default
|
|
fi
|
|
|
|
# Check if the reply is valid
|
|
case "$reply" in
|
|
Y*|y*) return 0 ;;
|
|
N*|n*) return 1 ;;
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
function vmail:getoptions () {
|
|
local OPTIND
|
|
while getopts "ha:b:d:e:f:gcp:q:s:tk:f:gl:m:vx" opt ; do
|
|
case "${opt}" in
|
|
h ) # display help and exit
|
|
help
|
|
exit
|
|
;;
|
|
a ) # alias
|
|
alias=${OPTARG,,}
|
|
if [[ $alias =~ "@" ]] ; then
|
|
domain=${alias##*@}
|
|
if vmail::validate_domain $domain; then
|
|
alias=${alias%@*}
|
|
else
|
|
echo "ERROR: $domain is not a valid domain name."
|
|
exit
|
|
fi
|
|
fi
|
|
;;
|
|
b ) # mbox - local part of email address
|
|
mbox=${OPTARG,,}
|
|
;;
|
|
c ) # cvs - output in cvs format
|
|
cvs="| sed 's/\t/,/g'"
|
|
;;
|
|
d ) # domain name (virtualhost) to act on
|
|
domain=${OPTARG,,}
|
|
if ! vmail::validate_domain $domain; then
|
|
echo "ERROR: $domain is not a valid domain name."
|
|
exit
|
|
fi
|
|
;;
|
|
e ) # email address
|
|
email=${OPTARG,,}
|
|
if [[ $email =~ "@" ]] ; then
|
|
mbox=${email%@*}
|
|
domain=${email##*@}
|
|
if [ -z $mbox ] ; then
|
|
echo "ERROR: No local part in $email."
|
|
exit 1
|
|
elif [ -z $domain ] ; then
|
|
echo "ERROR: No domain in $email."
|
|
exit 1
|
|
elif ! vmail::validate_domain $domain; then
|
|
echo "ERROR: $domain is not a valid domain name."
|
|
exit 1
|
|
fi
|
|
else
|
|
echo "ERROR: $email is not a valid email."
|
|
exit 1
|
|
fi
|
|
;;
|
|
f ) # forward to email address
|
|
forward=${OPTARG,,}
|
|
if [[ $forward =~ "@" ]] ; then
|
|
forward_mbox=${forward%@*}
|
|
forward_domain=${forward##*@}
|
|
if [ -z $forward_mbox ] ; then
|
|
echo "ERROR: No local part in $forward."
|
|
exit 1
|
|
elif [ -z $forward_domain ] ; then
|
|
echo "ERROR: No domain in $forward."
|
|
exit 1
|
|
elif ! vmail::validate_domain $forward_domain; then
|
|
echo "ERROR: $forward_domain is not a valid domain name."
|
|
exit 1
|
|
fi
|
|
else
|
|
echo "ERROR: $forward is not a valid email."
|
|
exit 1
|
|
fi
|
|
;;
|
|
g ) # glob (wildcard) search
|
|
glob=${OPTARG,,}
|
|
;;
|
|
k ) # keep
|
|
keep=${OPTARG}
|
|
if [[ $keep != "0" ]] && [[ $keep != "1" ]]; then
|
|
echo "ERROR: Invalid save keep setting: -k $keep."
|
|
exit 1
|
|
fi
|
|
;;
|
|
l ) # limit
|
|
limit=${OPTARG}
|
|
;;
|
|
m ) # mbox
|
|
mbox=${OPTARG}
|
|
;;
|
|
p ) # password
|
|
password=${OPTARG}
|
|
;;
|
|
q ) # quota
|
|
quota=${OPTARG}
|
|
;;
|
|
s ) # status - 0 or 1
|
|
status=${OPTARG}
|
|
;;
|
|
t ) # tab - Use tabs instead of tables for output, do not display column headers
|
|
tab=true
|
|
;;
|
|
n ) # dry-run
|
|
dryrun=true
|
|
;;
|
|
v ) # verbose
|
|
verbose=true
|
|
;;
|
|
w ) # write - store data in file
|
|
write=true
|
|
;;
|
|
x ) # eXecute - don't prompt for confirmation
|
|
execute=true
|
|
;;
|
|
\? )
|
|
echo "Invalid option: $OPTARG"
|
|
exit 1
|
|
;;
|
|
: )
|
|
echo "Invalid option: $OPTARG requires an argument"
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
shift $((OPTIND-1))
|
|
}
|