vmail-stack/bin/vmail.sh

232 lines
6.2 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
# 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-11-10 20:25:55 -08:00
# switch to required user
if [[ $(basename $0) == "vmail-dkim-"* ]]; then
if [[ "$USER" != "Debian-exim" ]]; then
2022-05-23 15:49:49 -07:00
exec sudo -u Debian-exim -g ssl-cert $0 "$@"
2021-11-10 20:25:55 -08:00
fi
elif [[ $(basename $0) == "vmail-purge-spool.sh" ]]; then
if [[ "$USER" != "Debian-exim" ]]; then
2022-05-23 15:49:49 -07:00
exec sudo -u Debian-exim $0 "$@"
2021-11-10 20:25:55 -08:00
fi
else
if [[ "$USER" != "vmail" ]]; then
2022-05-23 15:49:49 -07:00
exec sudo -u vmail $0 "$@"
2021-11-10 20:25:55 -08:00
else
# 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."
2022-05-10 09:53:40 -07:00
exit 1
2021-11-10 20:25:55 -08:00
fi
fi
2021-03-19 11:58:35 -07:00
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
2022-05-23 15:49:49 -07:00
while getopts "ha:b:d:e:f:gj:cp:q:r:s:tk:gl:m:o:u:vx" opt ; do
2021-10-15 15:17:29 -07:00
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."
2022-05-10 09:53:40 -07:00
exit 1
2021-10-15 15:17:29 -07:00
fi
fi
;;
2022-05-23 15:49:49 -07:00
b ) # body - Body for Autoresponder emails
body="${OPTARG}"
2021-10-15 15:17:29 -07:00
;;
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."
2022-05-10 09:53:40 -07:00
exit 1
2021-10-15 15:17:29 -07:00
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,,}
2021-10-15 16:22:23 -07:00
if [[ $forward =~ "@" ]] ; then
forward_mbox=${forward%@*}
forward_domain=${forward##*@}
if [ -z $forward_mbox ] ; then
echo "ERROR: No local part in $forward."
2021-10-15 15:17:29 -07:00
exit 1
2021-10-15 16:22:23 -07:00
elif [ -z $forward_domain ] ; then
echo "ERROR: No domain in $forward."
2021-10-15 15:17:29 -07:00
exit 1
2021-10-15 16:22:23 -07:00
elif ! vmail::validate_domain $forward_domain; then
echo "ERROR: $forward_domain is not a valid domain name."
2021-10-15 15:17:29 -07:00
exit 1
fi
else
2021-10-15 16:22:23 -07:00
echo "ERROR: $forward is not a valid email."
2021-10-15 15:17:29 -07:00
exit 1
fi
;;
g ) # glob (wildcard) search
glob=${OPTARG,,}
;;
2021-11-20 15:20:24 -08:00
j ) # Filter Junk
filter=${OPTARG}
;;
2021-10-15 15:17:29 -07:00
k ) # keep
2021-10-15 16:22:23 -07:00
keep=${OPTARG}
2021-10-15 15:17:29 -07:00
if [[ $keep != "0" ]] && [[ $keep != "1" ]]; then
echo "ERROR: Invalid save keep setting: -k $keep."
exit 1
fi
;;
l ) # limit
limit=${OPTARG}
;;
2021-10-15 16:04:29 -07:00
m ) # mbox
mbox=${OPTARG}
;;
2021-10-15 15:17:29 -07:00
p ) # password
password=${OPTARG}
;;
2022-05-23 15:49:49 -07:00
o ) # mode for autoresponder - must be Vacation or Autoresponder
mode=${OPTARG,,} # first force lower case
mode=${mode^} # then capitlize first letter
if [[ $mode != "Vacation" ]] && [[ $mode != "Autoresponder" ]]; then
echo "ERROR: Invalid mode setting: -o $mode. Must be either Vacation or Autoresponder"
exit 1
fi
;;
2021-10-15 15:17:29 -07:00
q ) # quota
quota=${OPTARG}
;;
2021-12-30 16:50:23 -08:00
r ) # ratelimit - hourly limit for sending, multiplied by 10 for daily limit
ratelimit=${OPTARG}
;;
2022-06-30 08:24:35 -07:00
s ) # status - 0 or 1 or 2
2021-10-15 15:17:29 -07:00
status=${OPTARG}
2022-06-30 08:24:35 -07:00
if [[ $status != "0" ]] && [[ $status != "1" ]] && [[ $status != "2" ]]; then
2022-05-23 15:49:49 -07:00
echo "ERROR: Invalid status setting: -s $status"
exit 1
fi
2021-10-15 15:17:29 -07:00
;;
t ) # tab - Use tabs instead of tables for output, do not display column headers
tab=true
;;
2022-05-23 15:49:49 -07:00
u ) # subject - Subject for Autoresponder emails
subject="${OPTARG}"
;;
2021-10-15 15:17:29 -07:00
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))
}