2 # dns.sh next-gen by Fufroma
5 . /etc/alternc/local.sh
6 . /usr/lib/alternc/functions.sh
9 ZONE_TEMPLATE="/etc/alternc/templates/bind/templates/zone.template"
10 NAMED_TEMPLATE="/etc/alternc/templates/bind/templates/named.template"
11 NAMED_CONF="/var/lib/alternc/bind/automatic.conf"
15 echo "/var/lib/alternc/bind/zones/$1"
20 if [ ! -r "$(dns_zone_file $domain)" ] ; then
23 grep "LOCKED:YES" "$(dns_zone_file $domain)"
29 local serial=$(( $(grep "; serial" $(dns_zone_file $domain) 2>/dev/null|awk '{ print $1;}') + 1 ))
30 local serial2=$(date +%Y%m%d00)
31 if [ $serial -gt $serial2 ] ; then
41 $MYSQL_DO "SELECT zonettl FROM domaines d WHERE d.domaine='$domain';"
44 if [ "$zonettl" == "" ] ; then
47 if [ "$zonettl" -eq "0" ] ; then
55 chgrp bind $(dns_zone_file $domain)
56 chmod 640 $(dns_zone_file $domain)
63 if [ ! -f "$(dns_zone_file $domain)" ] ; then
64 echo Error : no file $(dns_zone_file $domain)
69 grep -q "\"${domain/./\\.}\"" "$NAMED_CONF"
70 if [ $? -ne 0 ] ; then
71 local tempo=$(cat "$NAMED_TEMPLATE")
72 tempo=${tempo/@@DOMAINE@@/$domain}
73 tempo=${tempo/@@ZONE_FILE@@/$(dns_zone_file $domain)}
74 echo $tempo >> "$NAMED_CONF"
75 # Kindly ask Bind to reload its configuration
76 # (the zone file is already created and populated)
79 run-parts --arg=dns_reconfig --arg="$domain" /usr/lib/alternc/reload.d
87 # Delete the zone file
88 if [ -w "$(dns_zone_file $domain)" ] ; then
89 rm -f "$(dns_zone_file $domain)"
92 local reg_domain=${domain/./\\.}
94 # Remove from the named conf
95 local file=$(cat "$NAMED_CONF")
96 echo -e "$file" |grep -v "\"$reg_domain\"" > "$NAMED_CONF"
98 # Remove the conf from openDKIM
99 rm -rf "/etc/opendkim/keys/$domain"
100 grep -v "^$reg_domain\$" /etc/opendkim/TrustedHosts >/etc/opendkim/TrustedHosts.alternc-tmp && mv /etc/opendkim/TrustedHosts.alternc-tmp /etc/opendkim/TrustedHosts
101 grep -v "^alternc\._domainkey\.$reg_domain " /etc/opendkim/KeyTable >/etc/opendkim/KeyTable.alternc-tmp && mv /etc/opendkim/KeyTable.alternc-tmp /etc/opendkim/KeyTable
102 grep -v "^$domain alternc\._domainkey\.$reg_domain\$" /etc/opendkim/SigningTable >/etc/opendkim/SigningTable.alternc-tmp && mv /etc/opendkim/SigningTable.alternc-tmp /etc/opendkim/SigningTable
104 # Ask the dns server for restart
107 run-parts --arg=dns_reconfig --arg="$domain" /usr/lib/alternc/reload.d
113 local manual_tag=";;; END ALTERNC AUTOGENERATE CONFIGURATION"
114 local zone_file=$(dns_zone_file $domain)
117 dns_is_locked "$domain"
118 if [ $? -eq 0 ]; then
119 echo "DNS $domain LOCKED"
123 # Get the serial number if there is one
124 local serial=$(dns_get_serial "$domain")
127 local zonettl=$(dns_get_zonettl "$domain")
129 # Generate the headers with the template
130 local file=$(cat "$ZONE_TEMPLATE")
135 $MYSQL_DO "select distinct replace(replace(dt.entry,'%TARGET%',sd.valeur), '%SUB%', if(length(sd.sub)>0,sd.sub,'@')) as entry from sub_domaines sd,domaines_type dt where sd.type=dt.name and sd.domaine='$domain' and sd.enable in ('ENABLE', 'ENABLED') order by entry ;"
138 ##### Mail autodetect for thunderbird / outlook - START
139 # If $file contain DEFAULT_MX
140 if [ ! -z "$(echo -e "$file" |egrep 'DEFAULT_MX' )" ] ; then
141 # If $file ! contain autoconfig -> add entry
142 if [ -z "$(echo -e "$file" |egrep '^autoconfig' )" ] ; then
143 file="$(echo -e "$file" ; echo -e "autoconfig IN CNAME $FQDN.\n")"
145 # if $file ! contain autodiscover -> add entry
146 if [ -z "$(echo -e "$file" |egrep '^autodiscover' )" ] ; then
147 file="$(echo -e "$file" ; echo -e "autodiscover IN CNAME $FQDN.\n")"
149 fi # End if containt DEFAULT_MX
150 ##### Mail autodetect for thunderbird / outlook - END
152 ##### OpenDKIM signature management - START
153 # If $file contain DEFAULT_MX
154 if [ ! -z "$(echo -e "$file" |egrep 'DEFAULT_MX' )" ] ; then
155 # If necessary, we generate the key:
156 if [ ! -d "/etc/opendkim/keys/$domain" ] ; then
157 mkdir -p "/etc/opendkim/keys/$domain"
159 pushd "/etc/opendkim/keys/$domain" >/dev/null
160 opendkim-genkey -r -d "$domain" -s "alternc"
161 chown opendkim:opendkim alternc.private
164 local reg_domain=${domain/./\\.}
166 grep -q "^$reg_domain\$" /etc/opendkim/TrustedHosts || echo "$domain" >>/etc/opendkim/TrustedHosts
167 grep -q "^alternc\._domainkey\.$reg_domain " /etc/opendkim/KeyTable || echo "alternc._domainkey.$domain $domain:alternc:/etc/opendkim/keys/$domain/alternc.private" >> /etc/opendkim/KeyTable
168 grep -q "^$domain alternc\._domainkey\.$reg_domain\$" /etc/opendkim/SigningTable || echo "$domain alternc._domainkey.$domain" >> /etc/opendkim/SigningTable
170 # we add alternc._domainkey with the proper key
172 if [ -r "/etc/opendkim/keys/$domain/alternc.txt" ] ; then
173 file="$(echo -e "$file" ; cat "/etc/opendkim/keys/$domain/alternc.txt")"
176 ##### OpenDKIM signature management - END
178 # Replace the vars by their values
179 # Here we can add dynamic value for the default MX
180 file=$( echo -e "$file" | sed -e "
182 s/%%ns1%%/$NS1_HOSTNAME/g;
183 s/%%ns2%%/$NS2_HOSTNAME/g;
184 s/%%DEFAULT_MX%%/$DEFAULT_MX/g;
185 s/%%DEFAULT_SECONDARY_MX%%/$DEFAULT_SECONDARY_MX/g;
187 s/@@ns1@@/$NS1_HOSTNAME/g;
188 s/@@ns2@@/$NS2_HOSTNAME/g;
189 s/@@DEFAULT_MX@@/$DEFAULT_MX/g;
190 s/@@DEFAULT_SECONDARY_MX@@/$DEFAULT_SECONDARY_MX/g;
191 s/@@DOMAINE@@/$domain/g;
192 s/@@SERIAL@@/$serial/g;
193 s/@@PUBLIC_IP@@/$PUBLIC_IP/g;
194 s/@@ZONETTL@@/$zonettl/g;
197 # Add the manually entered resource records (after the special tag ;;; END ALTERNC AUTOGENERATE CONFIGURATION)
198 if [ -r "$zone_file" ] ; then
201 grep -A 10000 "$manual_tag" "$zone_file"
204 # Add the special tag at the end of the zone, if it is not here yet:
205 if ! echo -e "$file" | grep -q "$manual_tag"
207 file=$(echo -e "$file"; echo "$manual_tag")
211 echo -e "$file" > "$zone_file"
215 # Add it to named conf
216 dns_named_conf $domain
219 run-parts --arg=dns_reload_zone --arg="$domain" /usr/lib/alternc/reload.d
221 # ask bind to reload the zone