| 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/alternc/bind/automatic.conf" |
|---|
| 12 | |
|---|
| 13 | dns_zone_file() { |
|---|
| 14 | echo "$ALTERNC_LOC/bind/zones/$1" |
|---|
| 15 | } |
|---|
| 16 | |
|---|
| 17 | dns_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 | |
|---|
| 26 | dns_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 | |
|---|
| 37 | dns_chmod() { |
|---|
| 38 | local domain=$1 |
|---|
| 39 | chgrp bind $(dns_zone_file $domain) |
|---|
| 40 | chmod 640 $(dns_zone_file $domain) |
|---|
| 41 | return 0 |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | dns_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 | |
|---|
| 61 | dns_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 |
|---|
| 75 | dns_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 | |
|---|
| 101 | # Deprecated ? |
|---|
| 102 | # local mx=$( $MYSQL_DO "select mx from domaines where domaine='$domain' limit 1;") |
|---|
| 103 | |
|---|
| 104 | # Replace the vars by their values |
|---|
| 105 | # Here we can add dynamic value for the default MX |
|---|
| 106 | file=$( echo -e "$file" | sed -e " |
|---|
| 107 | s/%%fqdn%%/$FQDN/g; |
|---|
| 108 | s/%%ns1%%/$NS1_HOSTNAME/g; |
|---|
| 109 | s/%%ns2%%/$NS2_HOSTNAME/g; |
|---|
| 110 | s/%%DEFAULT_MX%%/$DEFAULT_MX/g; |
|---|
| 111 | s/%%DEFAULT_SECONDARY_MX%%/$DEFAULT_SECONDARY_MX/g; |
|---|
| 112 | s/@@DOMAINE@@/$domain/g; |
|---|
| 113 | s/@@SERIAL@@/$serial/g; |
|---|
| 114 | s/@@PUBLIC_IP@@/$PUBLIC_IP/g") |
|---|
| 115 | |
|---|
| 116 | # Add the manual lines |
|---|
| 117 | if [ -r "$zone_file" ] ; then |
|---|
| 118 | file=$( |
|---|
| 119 | echo -e "$file" |
|---|
| 120 | grep -A 10000 "$manual_tag" "$zone_file" |
|---|
| 121 | ) |
|---|
| 122 | else |
|---|
| 123 | file=$(echo -e "$file"; echo "$manual_tag") |
|---|
| 124 | fi |
|---|
| 125 | |
|---|
| 126 | # Init the file |
|---|
| 127 | echo -e "$file" > "$zone_file" |
|---|
| 128 | |
|---|
| 129 | # And set his rights |
|---|
| 130 | dns_chmod $domain |
|---|
| 131 | # Add it to named conf |
|---|
| 132 | dns_named_conf $domain |
|---|
| 133 | } |
|---|