source: alternc-slavedns/trunk/alternc-slavedns @ 4309

Revision 4309, 5.9 KB checked in by fufroma, 4 weeks ago (diff)

Check sur les donnée téléchargées (évite les mauvaises surprise en cas de warning php ou autre ;) )

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#!/bin/bash
2# Synchronize a dns server with alternc's remote server :
3# the configuration files contains definitions for remote masters.
4
5CONFDIR=/etc/alternc/slavedns
6CACHEDIR=/var/cache/slavedns
7BINDDIR=/etc/bind/slavedns
8BINDINCLUDE=/etc/bind/slavedns.conf
9WGETRC=${HOME}/.wgetrc
10WGET=wget
11WGETFLAGS="-q"
12NAMED="/etc/init.d/bind restart"
13DEFAULTS="defaults.conf"
14DEFAULTSFILE="${CONFDIR}/${DEFAULTS}"
15DEBUG=false
16
17usage() {
18    cat <<EOF
19$0 [ -f ] [ -d ] [ -h ] [ config ]
20
21Performs a sync of the list of domains to replicate from master.
22
23Multiple master servers can be used, one per file in $CONFDIR
24If a config file is specified on the command line, only that server
25will be synced. The $BINDINCLUDE file will also be generated to include
26the right configuration.
27
28-f: refresh domain list even if it hasn't changed
29-d: show everything we're doing
30-h: this help
31EOF
32}
33
34for i; do
35    case "$i" in
36        -f)
37            FORCE=yes
38            ;;
39        -d)
40            DEBUG=true
41            ;;
42        -h)
43            usage
44            exit 0
45            ;;
46        *)
47            CONFIGS="${CONFIGS} ${i}"
48            ;;
49    esac
50done
51
52cd $CONFDIR
53
54if [ -z "$CONFIGS" ]; then
55    CONFIGS=`ls $CONFDIR | grep -v ~`
56fi
57
58TIMEOUT=5
59
60CreateBindConf() {
61    CFILE="$1"
62    # create a new config for this host, in a tempfile
63    while read domain; do
64        # check if the data is valid, this will also display the domain in debug mode
65        if echo $domain | grep -i '^\([a-z0-9]\([-a-z0-9]*[a-z0-9]\)\?\.\)*[a-z0-9]\([-a-z0-9]*[a-z0-9]\)$'; then
66            echo "validated domain $domain" | MaybeCat
67        else
68            echo invalid domain listing: $domain, skipping file $CFILE >&2
69            rm -f ${BINDDIR}/${CFILE}.$$
70            return
71        fi
72        cat >> ${BINDDIR}/${CFILE}.$$ <<EOF
73zone "$domain" {
74    type slave;
75    allow-query { any; };
76    file "$domain";
77    masters { ${MASTERIP}; };
78};
79EOF
80    done < ${CACHEDIR}/${CFILE}
81    mv ${BINDDIR}/${CFILE}.$$ ${BINDDIR}/${CFILE}
82    INCLUDE_STR="include \"${BINDDIR}/${CFILE}\";"
83
84    grep -q "${INCLUDE_STR}" ${BINDINCLUDE} || echo ${INCLUDE_STR} >>${BINDINCLUDE}
85}
86
87SetWgetPass() {
88    USER="$1"
89    PASS="$2"
90    if [ -e ${WGETRC} ]; then
91        mv ${WGETRC} ${WGETRC}.$$
92    fi
93    touch ${WGETRC}
94    chmod og-r ${WGETRC}
95    cat >> ${WGETRC} <<EOF
96http_user = ${USER}
97http_passwd = ${PASS}
98EOF
99}
100
101ResetWgetConf() {
102    mv -f ${WGETRC}.$$ ${WGETRC} 2>/dev/null || rm -f ${WGETRC}
103}
104
105MaybeCat() {
106    if $DEBUG; then
107        cat
108    else
109        cat > /dev/null
110    fi
111    return 0
112}
113
114# Main procedure : parse each config file and download the raw slave list.
115# if something changed in a list, call CreateBindConf $i
116
117RELOAD=""
118
119for conf in ${CONFIGS}; do
120    [ "${DEFAULTS}" = "${conf}" -o "slavedns.conf" = "${conf}" ] && continue
121    URL=""
122    # source defaults
123    . ${DEFAULTSFILE}
124    # source this site's config
125    . $CONFDIR/${conf}
126    if [ -z "$URL" ]; then
127        if [ -z "$PROTOCOL" ]; then
128            if [ "$SSL" ]; then
129                PROTOCOL=https
130            else
131                PROTOCOL=http
132            fi
133        fi
134        URL=${PROTOCOL}://${HOST}/domlist.php
135    fi
136
137    if [ ! -z "$INTEGRITY" ] ; then
138      if [ $INTEGRITY -eq 1 ] ; then
139        URL="$URL?integrity=1"
140      fi
141    fi
142
143    if [ -z "$URL" -a -z "$HOST" -o -z "$MASTERIP" ]; then
144        echo "error in the config file '${conf}'" >&2
145    else
146        touch ${CACHEDIR}/${conf}
147        rm -f ${CACHEDIR}/${conf}.temp
148        [ "${LOGIN}" ] && SetWgetPass ${LOGIN} ${PASSWORD}
149        ${WGET} ${URL} ${WGETFLAGS} -O ${CACHEDIR}/${conf}.temp -t 1 -T ${TIMEOUT} 2>&1 | MaybeCat
150        [ "${LOGIN}" ] && ResetWgetConf
151        if [ -s "${CACHEDIR}/${conf}.temp" ]; then
152            # If there are integrity check setup,
153            # Get of the checksum of the file and compare it
154            if [ ! -z "$INTEGRITY" ] ; then
155              if [ $INTEGRITY -eq 1 ] ; then
156                # Get the checksum
157                checksum="$( tail -1 "${CACHEDIR}/${conf}.temp" )"
158                echo "$checksum  ${CACHEDIR}/${conf}.temp" > "${CACHEDIR}/${conf}.temp.md5sum"
159                # Get it of the file
160                tmpff="$( cat "${CACHEDIR}/${conf}.temp" |grep -v "$checksum")"
161                echo -e "$tmpff" > "${CACHEDIR}/${conf}.temp"
162                # Calculate checksum
163                md5sum --warn --check --status "${CACHEDIR}/${conf}.temp.md5sum"
164                checkmd=$?
165                if [ "x$checkmd" != "x0" ] ; then
166                  echo "Error: bad checksum for $conf"
167                  echo "+++ BEGIN +++"
168                  echo -e "$tmpff"
169                  echo "+++  END  +++"
170                  echo -n "Local checksum: "
171                  cat "${CACHEDIR}/${conf}.temp.md5sum"
172                  # Clean the file and do the next conf file
173                  rm "${CACHEDIR}/${conf}.temp.md5sum" "${CACHEDIR}/${conf}.temp"
174                  continue
175                fi
176              fi
177            fi
178
179            test -e "${CACHEDIR}/${conf}.temp.md5sum" && rm "${CACHEDIR}/${conf}.temp.md5sum"
180
181
182            # If the slave file has changed, synchronize it.
183            if ! [ "${FORCE}" ] && cmp ${CACHEDIR}/${conf}.temp ${CACHEDIR}/${conf} > /dev/null; then
184                echo "no change found for '${conf}'"
185            else
186                echo "change detected for '${conf}', applying"
187                mv -f ${CACHEDIR}/${conf}.temp ${CACHEDIR}/${conf}
188                # Now parse the slave file and send it to /etc/bind/slavedns
189                CreateBindConf ${conf}
190                if /usr/sbin/named-checkconf ${BINDDIR}/${conf}; then
191                    RELOAD="yes"
192                else
193                    echo "error: file ${conf} is not correct"
194                fi
195            fi
196        else
197            echo "downloaded file for '${conf}' has zero size"
198            rm -f ${CACHEDIR}/${conf}.temp
199        fi
200    fi
201done # Main loop on config files
202
203if [ "$RELOAD" ]; then
204    ${NAMED} >/dev/null
205fi
Note: See TracBrowser for help on using the repository browser.