pywallter/scripts/set_mail_alias
2023-03-18 23:08:43 +01:00

116 lines
2.1 KiB
Bash
Executable File

#!/bin/sh
. /etc/mailconfig
exit_failure(){
mess=$1
code_exit=$2
echo "$mess";
exit "$code_exit";
}
check_domain()
{
mail=$1
alias=$2
domain_mail=`echo $mail | awk -F '@' '{ print $2 }'`
domain_alias=`echo $alias | awk -F '@' '{ print $2 }'`
[[ "$domain_mail" = `hostname` && "$domain_alias" = `hostname` ]] || exit_failure "Bad domain" 1;
grep -q "$mail" "$PASSWD_FILE" && true || exit_failure "Mail doesn't exist" 1;
}
check_mail_exist()
{
mail=$1
grep -q "$mail" "$PASSWD_FILE" && exit_failure " This e-mail address already exist" 2 || true
}
check_alias()
{
alias=$1
grep -q "$alias" "$ALIAS_FILE" && exit_failure "This e- mail address already exist in alias" 2 || true
}
check_app_mail()
{
alias=$1
grep -q "$alias" "$APP_MAIL" && exit_failure "Address already exist in app mail" 2 || true
}
add_alias()
{
print "$2":" $1" >> "$ALIAS_FILE"
}
check_alias_exist(){
alias_c="$1: $2"
grep -q "$alias_c" "$ALIAS_FILE" && true || exit_failure "This alias doesn't exist" 3
}
del_alias()
{
alias_line="$1: $2"
egrep -v "$alias_line" "$ALIAS_FILE" > /tmp/alias.tmp
mv /tmp/alias.tmp "$ALIAS_FILE"
}
usage(){
print "This program ask 3 arguments : \n"
print "First is email with domain name of this host second is add or del for \n"
print "add or delete an alias "
print "\t$0 email-adresse del alias@`hostname`\n"
print "Other example:\n\t $0 test@`hostname` add myalias@`hostname` "
print "This script require root privilèges"
}
if [ `id -u` -ne 0 ]; then
usage
exit 4;
fi
if [ -z $1 ];
then
usage
exit 3;
fi
if [ -z $2 ];
then
usage
exit 3;
fi
if [ -z $3 ];
then
usage
exit 3;
fi
check_domain $1 $3
case $2 in
"add")
check_mail_exist $3
check_alias $3
check_app_mail $3
add_alias $1 $3
smtpctl update table virtuals
;;
"del")
check_alias_exist $3 $1
del_alias $3 $1
smtpctl update table virtuals
;;
*)
usage
exit 4;
;;
esac