vmail-stack/bin/vmail.sh

197 lines
5.0 KiB
Bash
Raw Normal View History

2021-04-02 12:02:50 -07:00
#!/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
2021-02-10 16:16:23 -08:00
2021-03-01 12:04:51 -08:00
# any script that includes this conf file will force user to be vmail
if [ "$USER" != "vmail" ]; then
2021-03-30 12:32:56 -07:00
exec sudo -u vmail --shell /bin/bash $0 $@
2021-03-01 12:04:51 -08:00
fi
2021-02-10 16:16:23 -08:00
# constants
readonly VMAIL_DIR=/var/vmail
2021-03-31 09:59:35 -07:00
readonly MYSQL_CONNECTION_INFO_FILE=$VMAIL_DIR/.my.cnf
2021-02-10 16:16:23 -08:00
2021-03-19 11:58:35 -07:00
# 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
2021-02-10 16:16:23 -08:00
# functions
# crude but good enough domain name format validation
function vmail::validate_domain () {
local my_domain=$1
2021-10-15 15:17:29 -07:00
if [[ $my_domain =~ ^(([a-zA-Z0-9](-?[a-zA-Z0-9])*)\.)+[a-zA-Z]{2,}$ ]] ; then
2021-02-10 16:16:23 -08:00
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
2021-03-01 12:04:51 -08:00
}
2021-10-15 15:17:29 -07:00
function vmail:getoptions () {
local OPTIND
while getopts "ha:b:d:e:f:gcp:q:s:tkf:gl: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_to =~ "@" ]] ; then
forward_to_mbox=${forward_to%@*}
forward_to_domain=${forward_to##*@}
if [ -z $forward_to_mbox ] ; then
echo "ERROR: No local part in $forward_to."
exit 1
elif [ -z $forward_to_domain ] ; then
echo "ERROR: No domain in $forward_to."
exit 1
elif ! vmail::validate_domain $forward_to_domain; then
echo "ERROR: $forward_to_domain is not a valid domain name."
exit 1
fi
else
echo "ERROR: $forward_to is not a valid email."
exit 1
fi
;;
g ) # glob (wildcard) search
glob=${OPTARG,,}
;;
k ) # keep
if [[ $keep != "0" ]] && [[ $keep != "1" ]]; then
echo "ERROR: Invalid save keep setting: -k $keep."
exit 1
fi
;;
l ) # limit
limit=${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))
}