|
| 1 | +#!/bin/bash |
| 2 | +# info: rebuild mail domains |
| 3 | +# options: user |
| 4 | +# |
| 5 | +# The function rebuilds EXIM configuration files for all mail domains. |
| 6 | + |
| 7 | + |
| 8 | +#----------------------------------------------------------# |
| 9 | +# Variable&Function # |
| 10 | +#----------------------------------------------------------# |
| 11 | + |
| 12 | +# Argument defenition |
| 13 | +user=$1 |
| 14 | + |
| 15 | +# Importing variables |
| 16 | +source $VESTA/conf/vars.conf |
| 17 | +source $V_CONF/vesta.conf |
| 18 | +source $V_FUNC/shared.func |
| 19 | +source $V_FUNC/domain.func |
| 20 | + |
| 21 | + |
| 22 | +#----------------------------------------------------------# |
| 23 | +# Verifications # |
| 24 | +#----------------------------------------------------------# |
| 25 | + |
| 26 | +# Checking arg number |
| 27 | +check_args '1' "$#" 'user' |
| 28 | + |
| 29 | +# Checking argument format |
| 30 | +format_validation 'user' |
| 31 | + |
| 32 | +# Checking mail system is enabled |
| 33 | +is_system_enabled 'MAIL_SYSTEM' |
| 34 | + |
| 35 | +# Checking user |
| 36 | +is_user_valid |
| 37 | + |
| 38 | +# Checking user is active |
| 39 | +is_user_suspended |
| 40 | + |
| 41 | + |
| 42 | +#----------------------------------------------------------# |
| 43 | +# Action # |
| 44 | +#----------------------------------------------------------# |
| 45 | + |
| 46 | +# Reset counters |
| 47 | +U_MAIL_DOMAINS='0' |
| 48 | +U_MAIL_ACCOUNTS='0' |
| 49 | +SUSPENDED_MAIL='0' |
| 50 | +U_DISK_MAIL='0' |
| 51 | + |
| 52 | +# Checking mail folder |
| 53 | +if [ ! -d "$V_USERS/$user/mail" ]; then |
| 54 | + rm -f $V_USERS/$user/mail |
| 55 | + mkdir $V_USERS/$user/mail |
| 56 | +fi |
| 57 | + |
| 58 | +# Defining config |
| 59 | +conf="$V_USERS/$user/mail.conf" |
| 60 | +search_string="DOMAIN" |
| 61 | +field='$DOMAIN' |
| 62 | +domains=$(dom_clear_search) |
| 63 | + |
| 64 | +# Starting loop |
| 65 | +for domain in $domains; do |
| 66 | + |
| 67 | + # Defining variables |
| 68 | + get_domain_values 'mail' |
| 69 | + |
| 70 | + # Rebuilding config structure |
| 71 | + mkdir -p $V_HOME/$user/conf/mail/$domain |
| 72 | + rm -f $V_HOME/$user/conf/mail/$domain/aliases |
| 73 | + rm -f $V_HOME/$user/conf/mail/$domain/protection |
| 74 | + rm -f $V_HOME/$user/conf/mail/$domain/passwd |
| 75 | + touch $V_HOME/$user/conf/mail/$domain/aliases |
| 76 | + touch $V_HOME/$user/conf/mail/$domain/protection |
| 77 | + touch $V_HOME/$user/conf/mail/$domain/passwd |
| 78 | + chown -R root:mail $V_HOME/$user/conf/mail/$domain |
| 79 | + chmod 770 $V_HOME/$user/conf/mail/$domain |
| 80 | + chmod 660 $V_HOME/$user/conf/mail/$domain* |
| 81 | + |
| 82 | + # Adding antispam protection |
| 83 | + if [ "$ANTISPAM" = 'yes' ]; then |
| 84 | + echo 'antispam' >> $V_HOME/$user/conf/mail/$domain/protection |
| 85 | + fi |
| 86 | + |
| 87 | + # Adding antivirus protection |
| 88 | + if [ "$ANTIVIRUS" = 'yes' ]; then |
| 89 | + echo 'antivirus' >> $V_HOME/$user/conf/mail/$domain/protection |
| 90 | + fi |
| 91 | + |
| 92 | + # Adding dkim |
| 93 | + if [ "$DKIM" = 'yes' ]; then |
| 94 | + pem="$V_USERS/$user/mail/$domain.pem" |
| 95 | + pub="$V_USERS/$user/mail/$domain.pub" |
| 96 | + openssl genrsa -out $pem 512 2>/dev/null |
| 97 | + openssl rsa -pubout -in $pem -out $pub 2>/dev/null |
| 98 | + chmod 660 $V_USERS/$user/mail/$domain.* |
| 99 | + |
| 100 | + cp $pem $V_HOME/$user/conf/mail/$domain/dkim.pem |
| 101 | + chown root:mail $V_HOME/$user/conf/mail/$domain/dkim.pem |
| 102 | + chmod 660 $V_HOME/$user/conf/mail/$domain/dkim.pem |
| 103 | + |
| 104 | + # Deleting old dkim records |
| 105 | + records=$($V_BIN/v_list_dns_domain_records $user $domain plain) |
| 106 | + dkim_records=$(echo "$records" |grep -w '_domainkey'|cut -f 1 -d ' ') |
| 107 | + for id in $dkim_records; do |
| 108 | + $V_BIN/v_delete_dns_domain_record $user $domain $id |
| 109 | + done |
| 110 | + |
| 111 | + # Adding dkim dns records |
| 112 | + check_dns_domain=$(is_domain_valid 'dns') |
| 113 | + if [ "$?" -eq 0 ]; then |
| 114 | + p=$(cat $pub|grep -v ' KEY---'|tr -d '\n') |
| 115 | + record='_domainkey' |
| 116 | + policy="\"t=y; o=~;\"" |
| 117 | + $V_BIN/v_add_dns_domain_record $user $domain $record TXT "$policy" |
| 118 | + |
| 119 | + record='mail._domainkey' |
| 120 | + slct="\"k=rsa\; p=$p\"" |
| 121 | + $V_BIN/v_add_dns_domain_record $user $domain $record TXT "$slct" |
| 122 | + fi |
| 123 | + fi |
| 124 | + |
| 125 | + # Rebuild counters |
| 126 | + U_MAIL_DOMAINS=$((U_MAIL_DOMAINS + 1)) |
| 127 | + U_DISK_MAIL=$((U_DISK_MAIL + U_DISK)) |
| 128 | +done |
| 129 | + |
| 130 | + |
| 131 | +#----------------------------------------------------------# |
| 132 | +# Vesta # |
| 133 | +#----------------------------------------------------------# |
| 134 | + |
| 135 | +# Updating counters |
| 136 | +U_MAIL_DOMAINS='0' |
| 137 | +U_MAIL_ACCOUNTS='0' |
| 138 | +SUSPENDED_MAIL='0' |
| 139 | +U_DISK_MAIL='0' |
| 140 | + |
| 141 | +update_user_value "$user" '$U_MAIL_DOMAINS' "$U_MAIL_DOMAINS" |
| 142 | +update_user_value "$user" '$U_MAIL_ACCOUNTS' "$U_MAIL_ACCOUNTS" |
| 143 | +update_user_value "$user" '$SUSPENDED_MAIL' "$SUSPENDED_MAIL" |
| 144 | +update_user_value "$user" '$U_DISK_MAIL' "$U_DISK_MAIL" |
| 145 | + |
| 146 | +# Adding task to the vesta pipe |
| 147 | +restart_schedule 'mail' |
| 148 | + |
| 149 | +# Logging |
| 150 | +log_event 'system' "$V_EVENT" |
| 151 | + |
| 152 | +exit |
0 commit comments