functions_dns.sh 4.55 KB
Newer Older
1
2
3
#!/bin/bash
# dns.sh next-gen by Fufroma

4
# Init some vars
5
. /etc/alternc/local.sh
6
. /usr/lib/alternc/functions.sh
7
8
9

# Init some other vars
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
19
}

dns_is_locked() {
    local domain=$1
20
21
22
23
    if [ ! -r "$(dns_zone_file $domain)" ] ; then
      return 1
    fi
    grep "LOCKED:YES" "$(dns_zone_file $domain)"
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    return $?
}

dns_get_serial() {
    local domain=$1
    local serial=$(( $(grep "; serial" $(dns_zone_file $domain) 2>/dev/null|awk '{ print $1;}') + 1 ))
    local serial2=$(date +%Y%m%d00)
    if [ $serial -gt $serial2 ] ; then
        echo $serial
    else
        echo $serial2
    fi
}

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
dns_get_zonettl() {
    local domain=$1
    local zonettl=$(
        $MYSQL_DO "SELECT zonettl FROM domaines d WHERE d.domaine='$domain';"
        )
    # default value
    if [ "$zonettl" == "" ] ; then
        zonettl="86400"
    fi
    if [ "$zonettl" -eq "0" ] ; then
        zonettl="86400"
    fi
    echo $zonettl
}

53
54
55
56
57
58
59
dns_chmod() {
    local domain=$1
    chgrp bind $(dns_zone_file $domain)
    chmod 640 $(dns_zone_file $domain)
    return 0
}

60
61
62
63
64
65
66
67
dns_named_conf() {
  local domain=$1

  if [ ! -f "$(dns_zone_file $domain)" ] ; then
    echo Error : no file $(dns_zone_file $domain)
    return 1
  fi

68
  # Add the entry
Alan Garcia's avatar
Alan Garcia committed
69
  grep -q "\"$domain\"" "$NAMED_CONF"
70
71
72
73
74
  if [ $? -ne 0 ] ; then
    local tempo=$(cat "$NAMED_TEMPLATE")
    tempo=${tempo/@@DOMAINE@@/$domain}
    tempo=${tempo/@@ZONE_FILE@@/$(dns_zone_file $domain)}
    echo $tempo >> "$NAMED_CONF"
75
76
77
78
79
    # Kindly ask Bind to reload his configuration
    # (the zone file is allready created and populate)
    $RNDC reconfig
    # Hook it !
    run-parts --arg=dns_reconfig  /usr/lib/alternc/reload.d
80
  fi
81

82
83
84
85
86
87
}

dns_delete() {
  local domain=$1

  # Delete the zone file
88
89
  if [ -w "$(dns_zone_file $domain)" ] ; then
    rm -f "$(dns_zone_file $domain)"
90
91
92
93
94
  fi

  # Remove from the named conf
  local file=$(cat "$NAMED_CONF")
  echo -e "$file" |grep -v "\"$domain\"" > "$NAMED_CONF"
95
96

  # Ask for restart of dns server
97
98
99
  $RNDC reconfig
  # Hook it !
  run-parts --arg=dns_reconfig  /usr/lib/alternc/reload.d
100
101
}

102
103
104
105
106
107
# DNS regenerate
dns_regenerate() {
    local domain=$1
    local manual_tag=";;; END ALTERNC AUTOGENERATE CONFIGURATION"
    local zone_file=$(dns_zone_file $domain)

108
    # Check if locked
109
110
111
112
113
114
115
116
117
    dns_is_locked "$domain"
    if [ $? -eq 0 ]; then
        echo "DNS $domain LOCKED" 
        return 1
    fi

    # Get the serial number if there is one
    local serial=$(dns_get_serial "$domain")

118
119
120
    # Get the zone ttl
    local zonettl=$(dns_get_zonettl "$domain")

121
122
123
124
125
126
    # Generate the headers with the template
    local file=$(cat "$ZONE_TEMPLATE")

    # Add the entry
    file=$(
        echo -e "$file"
127
        $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 ;"
128
129
    )

130
    # Get some usefull vars
131
132
133

# Deprecated ?
#    local mx=$( $MYSQL_DO "select mx from domaines where domaine='$domain' limit 1;")
134
135
136
137
138
139
140

    # Replace the vars by their values
    # Here we can add dynamic value for the default MX
    file=$( echo -e "$file" | sed -e "
            s/%%fqdn%%/$FQDN/g;
            s/%%ns1%%/$NS1_HOSTNAME/g;
            s/%%ns2%%/$NS2_HOSTNAME/g;
141
142
            s/%%DEFAULT_MX%%/$DEFAULT_MX/g;
            s/%%DEFAULT_SECONDARY_MX%%/$DEFAULT_SECONDARY_MX/g;
Benjamin Sonntag's avatar
   
Benjamin Sonntag committed
143
144
145
146
147
            s/@@fqdn@@/$FQDN/g;
            s/@@ns1@@/$NS1_HOSTNAME/g;
            s/@@ns2@@/$NS2_HOSTNAME/g;
            s/@@DEFAULT_MX@@/$DEFAULT_MX/g;
            s/@@DEFAULT_SECONDARY_MX@@/$DEFAULT_SECONDARY_MX/g;
148
149
            s/@@DOMAINE@@/$domain/g;
            s/@@SERIAL@@/$serial/g;
150
151
152
            s/@@PUBLIC_IP@@/$PUBLIC_IP/g;
            s/@@ZONETTL@@/$zonettl/g;
            " )
153
154
155
156
157
158
159
160
161
162
163
    
    # Add the manual lines
    if [ -r "$zone_file" ] ; then
        file=$(
            echo -e "$file"
            grep -A 10000 "$manual_tag" "$zone_file"
            )
    else
        file=$(echo -e "$file"; echo "$manual_tag")
    fi

164
    # Init the file
165
    echo -e "$file" > "$zone_file"
166

167
168
    # And set his rights
    dns_chmod $domain
169
170
    # Add it to named conf
    dns_named_conf $domain
171
172
173

    # Hook it !
    run-parts --arg=dns_reload_zone --arg="$domain" /usr/lib/alternc/reload.d
174
175
176

    # Kindly bind to reload the zone
    $RNDC reload $domain
177
}