root/alternc-slavedns/trunk/alternc-slavedns

Revision 2061, 3.5 kB (checked in by anarcat, 7 months ago)

don't invalidate a complete master for a single error

allow dashes in hostnames and numerals at the start of labels (even
though RFC1035 explicitely forbids it, 1984comic.com is alive and well)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 #!/bin/sh
2 # Synchronize a dns server with alternc's remote server :
3 # the configuration files contains definitions for remote masters.
4
5 CONFDIR=/etc/alternc/slavedns
6 CACHEDIR=/var/cache/slavedns
7 BINDDIR=/etc/bind/slavedns
8 BINDINCLUDE=/etc/bind/slavedns.conf
9 WGETRC=${HOME}/.wgetrc
10 WGET=wget
11 WGETFLAGS="-q"
12 NAMED="/etc/init.d/bind restart"
13 DEFAULTS="defaults.conf"
14 DEFAULTSFILE="${CONFDIR}/${DEFAULTS}"
15 DEBUG=false
16
17 for i
18 do
19     case "$i"
20     in
21          -f)
22              FORCE=yes
23              ;;
24          -d)
25              DEBUG=true
26              ;;
27          *)
28              CONFIGS="${CONFIGS} ${i}"
29              ;;
30     esac
31 done
32
33 cd $CONFDIR
34
35 if [ -z "$CONFIGS" ]
36 then
37     CONFIGS=*[^~]
38 fi
39
40 TIMEOUT=5
41
42 CreateBindConf() {
43     CFILE="$1"
44     # create a new config for this host, in a tempfile
45     while read domain
46     do
47       # check if the data is valid, this will also display the domain in debug mode
48       if ! echo $domain | grep -qi '^\([a-z0-9]\([-a-z0-9]*[a-z0-9]\)\?\.\)*[a-z0-9]\([-a-z0-9]*[a-z0-9]\)$'
49       then
50           echo invalid domain listing: $domain, skipping >&2
51           continue
52       fi
53       cat >> ${BINDDIR}/${CFILE}.$$ <<EOF
54 zone "$domain" {
55     type slave;
56     allow-query { any; };
57     file "$domain";
58     masters { ${MASTERIP}; };
59 };
60 EOF
61     done < ${CACHEDIR}/${CFILE}
62     mv ${BINDDIR}/${CFILE}.$$ ${BINDDIR}/${CFILE}
63     INCLUDE_STR="include \"${BINDDIR}/${CFILE}\";"
64
65     grep -q "${INCLUDE_STR}" ${BINDINCLUDE} || echo ${INCLUDE_STR} >>${BINDINCLUDE}
66 }
67
68 SetWgetPass() {
69     USER="$1"
70     PASS="$2"
71     if [ -e ${WGETRC} ]; then
72         mv ${WGETRC} ${WGETRC}.$$
73     fi
74     touch ${WGETRC}
75     chmod og-r ${WGETRC}
76     cat >> ${WGETRC} <<EOF
77 http_user = ${USER}
78 http_passwd = ${PASS}
79 EOF
80 }
81
82 ResetWgetConf() {
83     mv -f ${WGETRC}.$$ ${WGETRC} 2>/dev/null || rm -f ${WGETRC}
84 }
85
86 MaybeCat() {
87     if $DEBUG
88     then
89         cat
90     else
91         cat > /dev/null
92     fi
93     return 0
94 }
95
96 # Main procedure : parse each config file and download the raw slave list.
97 # if something changed in a list, call CreateBindConf $i
98
99 RELOAD=""
100
101 for conf in ${CONFIGS}
102 do
103   [ "${DEFAULTS}" = "${conf}" -o "slavedns.conf" = "${conf}" ] && continue
104   URL=""
105   # source defaults
106   . ${DEFAULTSFILE}
107   # source this site's config
108   . ${conf}
109   if [ -z "$URL" ]
110   then
111       if [ -z "$PROTOCOL" ]
112       then
113           if [ "$SSL" ]
114           then
115               PROTOCOL=https
116           else
117               PROTOCOL=http
118           fi
119       fi
120       URL=${PROTOCOL}://${HOST}/admin/domlist.php
121   fi
122
123   if [ -z "$URL" -a -z "$HOST" -o -z "$MASTERIP" ]
124   then
125       echo "error in the config file '${conf}'" >&2
126   else
127       touch ${CACHEDIR}/${conf}
128       rm -f ${CACHEDIR}/${conf}.temp
129       [ "${LOGIN}" ] && SetWgetPass ${LOGIN} ${PASSWORD}
130       ${WGET} ${URL} ${WGETFLAGS} -O ${CACHEDIR}/${conf}.temp -t 1 -T ${TIMEOUT} 2>&1 | MaybeCat
131       [ "${LOGIN}" ] && ResetWgetConf
132       if [ -s "${CACHEDIR}/${conf}.temp" ]
133       then
134           # If the slave file has changed, synchronize it.
135           if ! [ "${FORCE}" ] && cmp ${CACHEDIR}/${conf}.temp ${CACHEDIR}/${conf} > /dev/null
136           then
137               echo "no change found for '${conf}'"
138           else
139               echo "change detected for '${conf}', applying"
140               mv -f ${CACHEDIR}/${conf}.temp ${CACHEDIR}/${conf}
141               # Now parse the slave file and send it to /etc/bind/slavedns
142               CreateBindConf ${conf}
143               RELOAD="yes"
144           fi
145       else
146           echo "error: downloaded file for '${conf}' has zero size" >&2
147       fi
148   fi
149 done     # Main loop on config files
150
151 if [ "$RELOAD" ]
152 then
153     ${NAMED} >/dev/null
154 fi
Note: See TracBrowser for help on using the browser.