root/alternc-slavedns/tags/1.0/alternc-slavedns

Revision 1984, 3.5 kB (checked in by anarcat, 1 year ago)

move everything to alternc-slavedns instead of just slavedns, there might be other slavedns packages out there

  • 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 -i '^\([a-z][a-z0-9]*\.\)*[a-z][a-z0-9]*$' | MaybeCat
49       then
50           echo invalid domain listing: $domain, skipping file $CFILE >&2
51           rm -f ${BINDDIR}/${CFILE}.$$
52           return
53       fi
54       cat >> ${BINDDIR}/${CFILE}.$$ <<EOF
55 zone "$domain" {
56     type slave;
57     allow-query { any; };
58     file "$domain";
59     masters { ${MASTERIP}; };
60 };
61 EOF
62     done < ${CACHEDIR}/${CFILE}
63     mv ${BINDDIR}/${CFILE}.$$ ${BINDDIR}/${CFILE}
64     INCLUDE_STR="include \"${BINDDIR}/${CFILE}\";"
65
66     grep -q "${INCLUDE_STR}" ${BINDINCLUDE} || echo ${INCLUDE_STR} >>${BINDINCLUDE}
67 }
68
69 SetWgetPass() {
70     USER="$1"
71     PASS="$2"
72     if [ -e ${WGETRC} ]; then
73         mv ${WGETRC} ${WGETRC}.$$
74     fi
75     touch ${WGETRC}
76     chmod og-r ${WGETRC}
77     cat >> ${WGETRC} <<EOF
78 http_user = ${USER}
79 http_passwd = ${PASS}
80 EOF
81 }
82
83 ResetWgetConf() {
84     mv -f ${WGETRC}.$$ ${WGETRC} 2>/dev/null || rm -f ${WGETRC}
85 }
86
87 MaybeCat() {
88     if $DEBUG
89     then
90         cat
91     else
92         cat > /dev/null
93     fi
94     return 0
95 }
96
97 # Main procedure : parse each config file and download the raw slave list.
98 # if something changed in a list, call CreateBindConf $i
99
100 RELOAD=""
101
102 for conf in ${CONFIGS}
103 do
104   [ "${DEFAULTS}" = "${conf}" -o "slavedns.conf" = "${conf}" ] && continue
105   URL=""
106   # source defaults
107   . ${DEFAULTSFILE}
108   # source this site's config
109   . ${conf}
110   if [ -z "$URL" ]
111   then
112       if [ -z "$PROTOCOL" ]
113       then
114           if [ "$SSL" ]
115           then
116               PROTOCOL=https
117           else
118               PROTOCOL=http
119           fi
120       fi
121       URL=${PROTOCOL}://${HOST}/admin/domlist.php
122   fi
123
124   if [ -z "$URL" -a -z "$HOST" -o -z "$MASTERIP" ]
125   then
126       echo "error in the config file '${conf}'" >&2
127   else
128       touch ${CACHEDIR}/${conf}
129       rm -f ${CACHEDIR}/${conf}.temp
130       [ "${LOGIN}" ] && SetWgetPass ${LOGIN} ${PASSWORD}
131       ${WGET} ${URL} ${WGETFLAGS} -O ${CACHEDIR}/${conf}.temp -t 1 -T ${TIMEOUT} 2>&1 | MaybeCat
132       [ "${LOGIN}" ] && ResetWgetConf
133       if [ -s "${CACHEDIR}/${conf}.temp" ]
134       then
135           # If the slave file has changed, synchronize it.
136           if ! [ "${FORCE}" ] && cmp ${CACHEDIR}/${conf}.temp ${CACHEDIR}/${conf} > /dev/null
137           then
138               echo "no change found for '${conf}'"
139           else
140               echo "change detected for '${conf}', applying"
141               mv -f ${CACHEDIR}/${conf}.temp ${CACHEDIR}/${conf}
142               # Now parse the slave file and send it to /etc/bind/slavedns
143               CreateBindConf ${conf}
144               RELOAD="yes"
145           fi
146       else
147           echo "error: downloaded file for '${conf}' has zero size" >&2
148       fi
149   fi
150 done     # Main loop on config files
151
152 if [ "$RELOAD" ]
153 then
154     ${NAMED} >/dev/null
155 fi
Note: See TracBrowser for help on using the browser.