| 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 | usage() { |
|---|
| 18 | cat <<EOF |
|---|
| 19 | $0 [ -f ] [ -d ] [ -h ] [ config ] |
|---|
| 20 | |
|---|
| 21 | Performs a sync of the list of domains to replicate from master. |
|---|
| 22 | |
|---|
| 23 | Multiple master servers can be used, one per file in $CONFDIR |
|---|
| 24 | If a config file is specified on the command line, only that server |
|---|
| 25 | will be synced. The $BINDINCLUDE file will also be generated to include |
|---|
| 26 | the 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 |
|---|
| 31 | EOF |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | for 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 |
|---|
| 50 | done |
|---|
| 51 | |
|---|
| 52 | cd $CONFDIR |
|---|
| 53 | |
|---|
| 54 | if [ -z "$CONFIGS" ]; then |
|---|
| 55 | CONFIGS=`ls $CONFDIR | grep -v ~` |
|---|
| 56 | fi |
|---|
| 57 | |
|---|
| 58 | TIMEOUT=5 |
|---|
| 59 | |
|---|
| 60 | CreateBindConf() { |
|---|
| 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 |
|---|
| 73 | zone "$domain" { |
|---|
| 74 | type slave; |
|---|
| 75 | allow-query { any; }; |
|---|
| 76 | file "$domain"; |
|---|
| 77 | masters { ${MASTERIP}; }; |
|---|
| 78 | }; |
|---|
| 79 | EOF |
|---|
| 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 | |
|---|
| 87 | SetWgetPass() { |
|---|
| 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 |
|---|
| 96 | http_user = ${USER} |
|---|
| 97 | http_passwd = ${PASS} |
|---|
| 98 | EOF |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | ResetWgetConf() { |
|---|
| 102 | mv -f ${WGETRC}.$$ ${WGETRC} 2>/dev/null || rm -f ${WGETRC} |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | MaybeCat() { |
|---|
| 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 | |
|---|
| 117 | RELOAD="" |
|---|
| 118 | |
|---|
| 119 | for 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 |
|---|
| 165 | done # Main loop on config files |
|---|
| 166 | |
|---|
| 167 | if [ "$RELOAD" ]; then |
|---|
| 168 | ${NAMED} >/dev/null |
|---|
| 169 | fi |
|---|