From 5efa58a92ec6ca6fedff95c975038bd5a7b53582 Mon Sep 17 00:00:00 2001 From: myvesta <38690722+myvesta@users.noreply.github.com> Date: Sat, 15 Aug 2020 22:35:59 +0200 Subject: [PATCH] Create v-make-separated-ip-for-email-domain --- bin/v-make-separated-ip-for-email-domain | 90 ++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 bin/v-make-separated-ip-for-email-domain diff --git a/bin/v-make-separated-ip-for-email-domain b/bin/v-make-separated-ip-for-email-domain new file mode 100644 index 00000000..769139b5 --- /dev/null +++ b/bin/v-make-separated-ip-for-email-domain @@ -0,0 +1,90 @@ +#!/bin/bash + +# info: Switch domain to send emails from desired IP +# options: DOMAIN IP +# +# The function switch domain to send emails from desired IP + +#----------------------------------------------------------# +# Variable&Function # +#----------------------------------------------------------# + +whoami=$(whoami) +if [ "$whoami" != "root" ]; then + echo "You must be root to execute this script" + exit 1 +fi + +# Importing system environment +source /etc/profile + +# Includes +source /usr/local/vesta/func/main.sh + +DOMAIN=$1 +IP=$2 + +#----------------------------------------------------------# +# Verifications # +#----------------------------------------------------------# + +check_args '2' "$#" 'DOMAIN IP' +is_domain_format_valid "$DOMAIN" +is_ip_format_valid "$IP" + +if [ ! -d "/etc/exim4/virtual" ]; then + echo "You must first dedicate IP to some hostname using command v-make-separated-ip-for-email" + exit 1 +fi + +HOST_USER=$($VESTA/bin/v-search-domain-owner "$HOSTNAME") +if [ -z "$HOST_USER" ]; then + echo "Error: hostname $HOSTNAME is not created as web domain" + exit 2 +fi + +HOST_IP=$($VESTA/bin/v-list-web-domain "$HOST_USER" "$HOSTNAME" | grep 'IP:' | awk '{print $2}') + +# echo "HOSTNAME : $HOSTNAME" +# echo "HOSTNAME IP : $HOST_IP" + +check_grep=$(grep -c "^$IP:" /etc/exim4/virtual/helo_data) +if [ "$check_grep" -eq 0 ]; then + echo "You must first dedicate IP to some hostname using command v-make-separated-ip-for-email" + exit 3 +fi + +USER=$($VESTA/bin/v-search-domain-owner "$DOMAIN") +if [ -z "$USER" ]; then + echo "Error: hostname $DOMAIN is not created as web domain" + exit 4 +fi + +echo "=== patching exim4.conf.template" +NEWVALUE=" interface = \${lookup{\$sender_address_domain}lsearch{/etc/exim4/virtual/interfaces} {\$value}{$HOST_IP}}" +sed -i "s#^ interface = .*#$NEWVALUE#g" /etc/exim4/exim4.conf.template +NEWVALUE=" helo_data = \"\${lookup{\$sending_ip_address}lsearch{/etc/exim4/virtual/helo_data}{\$value}{$HOSTNAME}}\"" +sed -i "s#^ helo_data = .*#$NEWVALUE#g" /etc/exim4/exim4.conf.template + +service exim4 restart + +check_grep=$(grep -c "^$DOMAIN:" /etc/exim4/virtual/interfaces) +if [ "$check_grep" -eq 0 ]; then + echo "=== Adding $DOMAIN: $IP to /etc/exim4/virtual/interfaces" + echo "" >> /etc/exim4/virtual/interfaces + echo "$DOMAIN: $IP" >> /etc/exim4/virtual/interfaces + length=$(wc -c /dev/null 2>&1 + echo "=== Done!" +else + echo "=== Domain $DOMAIN is already added" +fi + +#----------------------------------------------------------# +# Vesta # +#----------------------------------------------------------# + +# Logging +log_event "$OK" "$ARGUMENTS" + +exit