11#! /bin/bash
22# info: add mail domain
3- # options: user domain [antispam] [antivirus]
3+ # options: user domain [antispam] [antivirus] [dkim] [dkim_size]
44#
55# The function adds MAIL domain.
66
@@ -15,7 +15,9 @@ domain=$(idn -t --quiet -u "$2" )
1515domain=$( echo $domain | tr ' [:upper:]' ' [:lower:]' )
1616domain_idn=$( idn -t --quiet -a " $domain " )
1717antispam=${3-yes}
18- antivirus=${3-yes}
18+ antivirus=${4-yes}
19+ dkim=${5-yes}
20+ dkim_size=${6-512}
1921
2022# Importing variables
2123source $VESTA /conf/vars.conf
@@ -29,10 +31,10 @@ source $V_FUNC/domain.func
2931# ----------------------------------------------------------#
3032
3133# Checking arg number
32- check_args ' 2' " $# " ' user domain [antispam] [antivirus]'
34+ check_args ' 2' " $# " ' user domain [antispam] [antivirus] [dkim] [dkim_size] '
3335
3436# Checking argument format
35- format_validation ' user' ' domain' ' antispam' ' antivirus'
37+ format_validation ' user' ' domain' ' antispam' ' antivirus' ' dkim ' ' dkim_size '
3638
3739# Checking dns system is enabled
3840is_system_enabled ' MAIL_SYSTEM'
@@ -50,70 +52,72 @@ is_domain_new 'mail'
5052is_package_full ' MAIL_DOMAINS'
5153
5254
53- exit
54-
5555# ----------------------------------------------------------#
5656# Action #
5757# ----------------------------------------------------------#
5858
59- # Defining variables
60- i=1
61- ns=$( get_user_value ' $NS' )
62- for nameserver in ${ns// ,/ } ; do
63- eval ns$i =$nameserver
64- i=$(( i + 1 ))
65- done
59+ # Adding domain directory
60+ mkdir $V_HOME /$user /conf/mail/$domain
61+ touch $V_HOME /$user /conf/mail/$domain /aliases
62+ touch $V_HOME /$user /conf/mail/$domain /protection
63+ touch $V_HOME /$user /conf/mail/$domain /passwd
64+ chown -R root:mail $V_HOME /$user /conf/mail/$domain
65+ chmod 770 $V_HOME /$user /conf/mail/$domain
66+ chmod 660 $V_HOME /$user /conf/mail/$domain *
67+
68+ # Adding antispam protection
69+ if [ " $antispam " = ' yes' ]; then
70+ echo ' antispam' >> $V_HOME /$user /conf/mail/$domain /protection
71+ fi
72+
73+ # Adding antivirus protection
74+ if [ " $antivirus " = ' yes' ]; then
75+ echo ' antivirus' >> $V_HOME /$user /conf/mail/$domain /protection
76+ fi
6677
67- if [ -z " $soa " ]; then
68- soa=" $ns1 "
78+ # Adding dkim
79+ if [ " $dkim " = ' yes' ]; then
80+ openssl genrsa -out $V_USERS /$user /mail/$domain .pem $dkim_size 2> /dev/null
81+ openssl rsa -pubout -in $V_USERS /$user /mail/$domain .pem \
82+ -out $V_USERS /$user /mail/$domain .pub 2> /dev/null
83+ chmod 660 $V_USERS /$user /mail/$domain .*
84+
85+ cp $V_USERS /$user /mail/$domain .pem $V_HOME /$user /conf/mail/$domain /dkim.pem
86+ chown root:mail $V_HOME /$user /conf/mail/$domain /dkim.pem
87+ chmod 660 $V_HOME /$user /conf/mail/$domain /dkim.pem
88+
89+ # Adding dkim dns records
90+ check_dns_domain=$( is_domain_valid ' dns' )
91+ if [ " $? " -eq 0 ]; then
92+ p=$( cat $V_USERS /$user /mail/$domain .pub| grep -v ' KEY---' | tr -d ' \n' )
93+ record=' _domainkey'
94+ policy=" \" t=y; o=~;\" "
95+ $V_BIN /v_add_dns_domain_record $user $domain $record TXT " $policy "
96+
97+ record=' mail._domainkey'
98+ selector=" \" k=rsa\; p=$p \" "
99+ $V_BIN /v_add_dns_domain_record $user $domain $record TXT " $selector "
100+ fi
69101fi
70102
71- # Adding zone to dns dir
72- cat $V_DNSTPL /$template .tpl | \
73- sed -e " s/%ip%/$ip /g" \
74- -e " s/%domain_idn%/$domain_idn /g" \
75- -e " s/%domain%/$domain /g" \
76- -e " s/%ns1%/$ns1 /g" \
77- -e " s/%ns2%/$ns2 /g" \
78- -e " s/%ns3%/$ns3 /g" \
79- -e " s/%ns4%/$ns4 /g" \
80- -e " s/%ns5%/$ns5 /g" \
81- -e " s/%ns6%/$ns6 /g" \
82- -e " s/%ns7%/$ns7 /g" \
83- -e " s/%ns8%/$ns8 /g" \
84- -e " s/%date%/$V_DATE /g" > $V_USERS /$user /dns/$domain
85-
86- # Adding dns.conf record
87- dns_rec=" DOMAIN='$domain ' IP='$ip ' TPL='$template ' TTL='$ttl ' EXP='$exp '"
88- dns_rec=" $dns_rec SOA='$soa ' SUSPENDED='no' DATE='$V_DATE '"
89- echo " $dns_rec " >> $V_USERS /$user /dns.conf
90- chmod 660 $V_USERS /$user /dns.conf
91-
92- # Adding zone in named.conf
93- named=" zone \" $domain_idn \" {type master; file"
94- named=" $named \" $V_HOME /$user /conf/dns/$domain .db\" ;};"
95- echo " $named " >> /etc/named.conf
96-
97- # Updating domain dns zone
98- conf=" $V_HOME /$user /conf/dns/$domain .db"
99- update_domain_zone
100-
101- chmod 640 $conf
102- chown root:named $conf
103+ # Adding domain to vesta db
104+ s=" DOMAIN='$domain ' ANTIVIRUS='$antivirus ' ANTISPAM='$antispam ' DKIM='$dkim '"
105+ s=" $s ACCOUNTS='0' U_DISK='0' CATCHALL='' SUSPENDED='no' DATE='$V_DATE '"
106+ echo $s >> $V_USERS /$user /mail.conf
107+ touch $V_USERS /$user /mail/$domain
108+ chmod 660 $V_USERS /$user /mail.conf
109+ chmod 660 $V_USERS /$user /mail/$domain
103110
104111
105112# ----------------------------------------------------------#
106113# Vesta #
107114# ----------------------------------------------------------#
108115
109116# Increasing domain value
110- increase_user_value " $user " ' $U_DNS_DOMAINS'
111-
112- # Adding task to the vesta pipe
113- restart_schedule ' dns'
117+ increase_user_value " $user " ' $U_MAIL_DOMAINS'
114118
115119# Logging
116- log_history " $V_EVENT " " v_delete_dns_domain $user $domain "
120+ log_history " $V_EVENT " " v_delete_mail_domain $user $domain "
117121log_event ' system' " $V_EVENT "
118122
119123exit
0 commit comments