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

Revision 3179, 4.4 KB checked in by anarcat, 12 months ago (diff)

hotfix: fix syntax error in 1.2

  • 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
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}/admin/domlist.php
135    fi
136
137    if [ -z "$URL" -a -z "$HOST" -o -z "$MASTERIP" ]; then
138        echo "error in the config file '${conf}'" >&2
139    else
140        touch ${CACHEDIR}/${conf}
141        rm -f ${CACHEDIR}/${conf}.temp
142        [ "${LOGIN}" ] && SetWgetPass ${LOGIN} ${PASSWORD}
143        ${WGET} ${URL} ${WGETFLAGS} -O ${CACHEDIR}/${conf}.temp -t 1 -T ${TIMEOUT} 2>&1 | MaybeCat
144        [ "${LOGIN}" ] && ResetWgetConf
145        if [ -s "${CACHEDIR}/${conf}.temp" ]; then
146            # If the slave file has changed, synchronize it.
147            if ! [ "${FORCE}" ] && cmp ${CACHEDIR}/${conf}.temp ${CACHEDIR}/${conf} > /dev/null; then
148                echo "no change found for '${conf}'"
149            else
150                echo "change detected for '${conf}', applying"
151                mv -f ${CACHEDIR}/${conf}.temp ${CACHEDIR}/${conf}
152                # Now parse the slave file and send it to /etc/bind/slavedns
153                CreateBindConf ${conf}
154                if /usr/sbin/named-checkconf ${BINDDIR}/${conf}; then
155                    RELOAD="yes"
156                else
157                    echo "error: file ${conf} is not correct"
158                fi
159            fi
160        else
161            echo "downloaded file for '${conf}' has zero size"
162            rm -f ${CACHEDIR}/${conf}.temp
163        fi
164    fi
165done # Main loop on config files
166
167if [ "$RELOAD" ]; then
168    ${NAMED} >/dev/null
169fi
Note: See TracBrowser for help on using the repository browser.