Alternc  latest
Alternc logiel libre pour l'hébergement
functions_dns.sh
Go to the documentation of this file.
1 #!/bin/bash
2 # dns.sh next-gen by Fufroma
3 
4 # Init some vars
5 . /etc/alternc/local.sh
6 . /usr/lib/alternc/functions.sh
7 
8 # Init some other vars
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"
12 RNDC="/usr/sbin/rndc"
13 
14 dns_zone_file() {
15  echo "/var/lib/alternc/bind/zones/$1"
16 }
17 
18 dns_is_locked() {
19  local domain=$1
20  if [ ! -r "$(dns_zone_file $domain)" ] ; then
21  return 1
22  fi
23  grep "LOCKED:YES" "$(dns_zone_file $domain)"
24  return $?
25 }
26 
27 dns_get_serial() {
28  local domain=$1
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
32  echo $serial
33  else
34  echo $serial2
35  fi
36 }
37 
38 dns_get_zonettl() {
39  local domain=$1
40  local zonettl=$(
41  $MYSQL_DO "SELECT zonettl FROM domaines d WHERE d.domaine='$domain';"
42  )
43  # default value
44  if [ "$zonettl" == "" ] ; then
45  zonettl="86400"
46  fi
47  if [ "$zonettl" -eq "0" ] ; then
48  zonettl="86400"
49  fi
50  echo $zonettl
51 }
52 
53 dns_chmod() {
54  local domain=$1
55  chgrp bind $(dns_zone_file $domain)
56  chmod 640 $(dns_zone_file $domain)
57  return 0
58 }
59 
60 dns_named_conf() {
61  local domain=$1
62 
63  if [ ! -f "$(dns_zone_file $domain)" ] ; then
64  echo Error : no file $(dns_zone_file $domain)
65  return 1
66  fi
67 
68  # Add the entry
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)
77  $RNDC reconfig
78  # Hook it !
79  run-parts --arg=dns_reconfig --arg="$domain" /usr/lib/alternc/reload.d
80  fi
81 
82 }
83 
84 dns_delete() {
85  local domain=$1
86 
87  # Delete the zone file
88  if [ -w "$(dns_zone_file $domain)" ] ; then
89  rm -f "$(dns_zone_file $domain)"
90  fi
91 
92  local reg_domain=${domain/./\\.}
93 
94  # Remove from the named conf
95  local file=$(cat "$NAMED_CONF")
96  echo -e "$file" |grep -v "\"$reg_domain\"" > "$NAMED_CONF"
97 
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
103 
104  # Ask the dns server for restart
105  $RNDC reconfig
106  # Hook it !
107  run-parts --arg=dns_reconfig --arg="$domain" /usr/lib/alternc/reload.d
108 }
109 
110 # DNS regenerate
111 dns_regenerate() {
112  local domain=$1
113  local manual_tag=";;; END ALTERNC AUTOGENERATE CONFIGURATION"
114  local zone_file=$(dns_zone_file $domain)
115 
116  # Check if locked
117  dns_is_locked "$domain"
118  if [ $? -eq 0 ]; then
119  echo "DNS $domain LOCKED"
120  return 1
121  fi
122 
123  # Get the serial number if there is one
124  local serial=$(dns_get_serial "$domain")
125 
126  # Get the zone ttl
127  local zonettl=$(dns_get_zonettl "$domain")
128 
129  # Generate the headers with the template
130  local file=$(cat "$ZONE_TEMPLATE")
131 
132  # Add the entry
133  file=$(
134  echo -e "$file"
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 ;"
136  )
137 
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")"
144  fi
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")"
148  fi
149  fi # End if containt DEFAULT_MX
150  ##### Mail autodetect for thunderbird / outlook - END
151 
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"
158 
159  pushd "/etc/opendkim/keys/$domain" >/dev/null
160  opendkim-genkey -r -d "$domain" -s "alternc"
161  chown opendkim:opendkim alternc.private
162  popd
163 
164  local reg_domain=${domain/./\\.}
165 
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
169  fi
170  # we add alternc._domainkey with the proper key
171 
172  if [ -r "/etc/opendkim/keys/$domain/alternc.txt" ] ; then
173  file="$(echo -e "$file" ; cat "/etc/opendkim/keys/$domain/alternc.txt")"
174  fi
175  fi
176  ##### OpenDKIM signature management - END
177 
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 "
181  s/%%fqdn%%/$FQDN/g;
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;
186  s/@@fqdn@@/$FQDN/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;
195  " )
196 
197  # Add the manually entered resource records (after the special tag ;;; END ALTERNC AUTOGENERATE CONFIGURATION)
198  if [ -r "$zone_file" ] ; then
199  file=$(
200  echo -e "$file"
201  grep -A 10000 "$manual_tag" "$zone_file"
202  )
203  fi
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"
206  then
207  file=$(echo -e "$file"; echo "$manual_tag")
208  fi
209 
210  # Init the file
211  echo -e "$file" > "$zone_file"
212 
213  # And set his rights
214  dns_chmod $domain
215  # Add it to named conf
216  dns_named_conf $domain
217 
218  # Hook it !
219  run-parts --arg=dns_reload_zone --arg="$domain" /usr/lib/alternc/reload.d
220 
221  # ask bind to reload the zone
222  $RNDC reload $domain
223 }