source: alternc/trunk/src/dns.sh @ 2821

Revision 2821, 3.3 KB checked in by fufroma, 2 years ago (diff)

Update domains, l'aventure continue

  • Property svn:executable set to *
Line 
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
9ZONE_TEMPLATE="/etc/alternc/templates/bind/templates/zone.template"
10NAMED_TEMPLATE="/etc/alternc/templates/bind/templates/named.template"
11NAMED_CONF="/var/alternc/bind/automatic.conf"
12
13dns_zone_file() {
14    echo "$ALTERNC_LOC/bind/zones/$1"
15}
16
17dns_is_locked() {
18    local domain=$1
19    if [ ! -r "$(dns_zone_file $domain)" ] ; then
20      return 1
21    fi
22    grep "LOCKED:YES" "$(dns_zone_file $domain)"
23    return $?
24}
25
26dns_get_serial() {
27    local domain=$1
28    local serial=$(( $(grep "; serial" $(dns_zone_file $domain) 2>/dev/null|awk '{ print $1;}') + 1 ))
29    local serial2=$(date +%Y%m%d00)
30    if [ $serial -gt $serial2 ] ; then
31        echo $serial
32    else
33        echo $serial2
34    fi
35}
36
37dns_chmod() {
38    local domain=$1
39    chgrp bind $(dns_zone_file $domain)
40    chmod 640 $(dns_zone_file $domain)
41    return 0
42}
43
44dns_named_conf() {
45  local domain=$1
46
47  if [ ! -f "$(dns_zone_file $domain)" ] ; then
48    echo Error : no file $(dns_zone_file $domain)
49    return 1
50  fi
51
52  grep -q "$domain" "$NAMED_CONF"
53  if [ $? -ne 0 ] ; then
54    local tempo=$(cat "$NAMED_TEMPLATE")
55    tempo=${tempo/@@DOMAINE@@/$domain}
56    tempo=${tempo/@@ZONE_FILE@@/$(dns_zone_file $domain)}
57    echo $tempo >> "$NAMED_CONF"
58  fi
59}
60
61dns_delete() {
62  local domain=$1
63
64  # Delete the zone file
65  if [ -w $(dns_zone_file $domain) ] ; then
66    rm -f $(dns_zone_file $domain)
67  fi
68
69  # Remove from the named conf
70  local file=$(cat "$NAMED_CONF")
71  echo -e "$file" |grep -v "\"$domain\"" > "$NAMED_CONF"
72}
73
74# DNS regenerate
75dns_regenerate() {
76    local domain=$1
77    local manual_tag=";;; END ALTERNC AUTOGENERATE CONFIGURATION"
78    local zone_file=$(dns_zone_file $domain)
79
80    # Check if locked
81    dns_is_locked "$domain"
82    if [ $? -eq 0 ]; then
83        echo "DNS $domain LOCKED" 
84        return 1
85    fi
86
87    # Get the serial number if there is one
88    local serial=$(dns_get_serial "$domain")
89
90    # Generate the headers with the template
91    local file=$(cat "$ZONE_TEMPLATE")
92
93    # Add the entry
94    file=$(
95        echo -e "$file"
96        $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 ;"
97    )
98
99    # Get some usefull vars
100    local mx=$( $MYSQL_DO "select mx from domaines where domaine='$domain' limit 1;")
101
102    # Replace the vars by their values
103    # Here we can add dynamic value for the default MX
104    file=$( echo -e "$file" | sed -e "
105            s/%%fqdn%%/$FQDN/g;
106            s/%%ns1%%/$NS1_HOSTNAME/g;
107            s/%%ns2%%/$NS2_HOSTNAME/g;
108            s/%%mx%%/$mx/g;
109            s/@@DOMAINE@@/$domain/g;
110            s/@@SERIAL@@/$serial/g;
111            s/@@PUBLIC_IP@@/$PUBLIC_IP/g")
112   
113    # Add the manual lines
114    if [ -r "$zone_file" ] ; then
115        file=$(
116            echo -e "$file"
117            grep -A 10000 "$manual_tag" "$zone_file"
118            )
119    else
120        file=$(echo -e "$file"; echo "$manual_tag")
121    fi
122
123    # Init the file
124    echo -e "$file" > "$zone_file"
125
126    # And set his rights
127    dns_chmod $domain
128    # Add it to named conf
129    dns_named_conf $domain
130}
Note: See TracBrowser for help on using the repository browser.