2022-08-06 18:22:24 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
. /etc/mailconfig
|
|
|
|
|
2023-03-18 23:08:43 +01:00
|
|
|
exit_failure(){
|
|
|
|
mess=$1
|
|
|
|
code_exit=$2
|
|
|
|
echo "$mess";
|
|
|
|
exit "$code_exit";
|
|
|
|
}
|
|
|
|
|
2022-08-12 16:18:46 +02:00
|
|
|
check_domain()
|
2022-08-06 18:22:24 +02:00
|
|
|
{
|
|
|
|
mail=$1
|
2023-03-18 23:08:43 +01:00
|
|
|
alias=$2
|
|
|
|
|
|
|
|
domain_mail=`echo $mail | awk -F '@' '{ print $2 }'`
|
|
|
|
domain_alias=`echo $alias | awk -F '@' '{ print $2 }'`
|
2022-08-12 16:18:46 +02:00
|
|
|
|
2023-03-18 23:08:43 +01:00
|
|
|
[[ "$domain_mail" = `hostname` && "$domain_alias" = `hostname` ]] || exit_failure "Bad domain" 1;
|
2022-08-06 18:22:24 +02:00
|
|
|
|
2023-03-18 23:08:43 +01:00
|
|
|
grep -q "$mail" "$PASSWD_FILE" && true || exit_failure "Mail doesn't exist" 1;
|
2022-08-06 18:22:24 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-18 23:08:43 +01:00
|
|
|
check_mail_exist()
|
|
|
|
{
|
|
|
|
mail=$1
|
|
|
|
grep -q "$mail" "$PASSWD_FILE" && exit_failure " This e-mail address already exist" 2 || true
|
|
|
|
}
|
|
|
|
|
2022-08-06 18:22:24 +02:00
|
|
|
check_alias()
|
|
|
|
{
|
2022-08-12 16:18:46 +02:00
|
|
|
alias=$1
|
2023-03-18 23:08:43 +01:00
|
|
|
grep -q "$alias" "$ALIAS_FILE" && exit_failure "This e- mail address already exist in alias" 2 || true
|
2022-08-06 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
check_app_mail()
|
|
|
|
{
|
2023-03-18 23:08:43 +01:00
|
|
|
alias=$1
|
|
|
|
grep -q "$alias" "$APP_MAIL" && exit_failure "Address already exist in app mail" 2 || true
|
2022-08-06 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
add_alias()
|
|
|
|
{
|
2022-08-07 04:03:55 +02:00
|
|
|
print "$2":" $1" >> "$ALIAS_FILE"
|
2022-08-06 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
2022-08-12 16:18:46 +02:00
|
|
|
check_alias_exist(){
|
|
|
|
alias_c="$1: $2"
|
2023-03-18 23:08:43 +01:00
|
|
|
grep -q "$alias_c" "$ALIAS_FILE" && true || exit_failure "This alias doesn't exist" 3
|
2022-08-12 16:18:46 +02:00
|
|
|
}
|
|
|
|
|
2022-08-06 18:22:24 +02:00
|
|
|
del_alias()
|
|
|
|
{
|
2022-08-12 16:18:46 +02:00
|
|
|
alias_line="$1: $2"
|
|
|
|
egrep -v "$alias_line" "$ALIAS_FILE" > /tmp/alias.tmp
|
|
|
|
mv /tmp/alias.tmp "$ALIAS_FILE"
|
2022-08-06 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-03-18 23:08:43 +01:00
|
|
|
if [ -z $3 ];
|
|
|
|
then
|
|
|
|
usage
|
|
|
|
exit 3;
|
|
|
|
fi
|
|
|
|
|
2022-08-06 18:22:24 +02:00
|
|
|
|
2023-03-18 23:08:43 +01:00
|
|
|
check_domain $1 $3
|
2022-08-06 18:22:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
case $2 in
|
|
|
|
"add")
|
2023-03-18 23:08:43 +01:00
|
|
|
check_mail_exist $3
|
|
|
|
check_alias $3
|
|
|
|
check_app_mail $3
|
2022-08-06 18:22:24 +02:00
|
|
|
add_alias $1 $3
|
2023-03-18 23:08:43 +01:00
|
|
|
smtpctl update table virtuals
|
2022-08-06 18:22:24 +02:00
|
|
|
;;
|
|
|
|
"del")
|
2023-03-18 23:08:43 +01:00
|
|
|
check_alias_exist $3 $1
|
2022-08-06 18:22:24 +02:00
|
|
|
del_alias $3 $1
|
2023-03-18 23:08:43 +01:00
|
|
|
smtpctl update table virtuals
|
2022-08-06 18:22:24 +02:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
usage
|
|
|
|
exit 4;
|
|
|
|
;;
|
|
|
|
esac
|