#! /bin/sh # /etc/rc.d/rc.vhostnames: Update DynDNS from apache configs # Copyright 2013 Sergey Poznyakoff # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . ### Configurable part begins ### # Set these: HOSTNAME= ZONE= NSKEY= #TTL= ### Configurable part ends ### CNAMESLIST=/var/run/nssetup get_cnames() { awk -v pattern='.*\\.'"$ZONE" ' BEGIN { npat = split(pattern,apat); } { sub(/^[ \t][ \t]*/,"") } /#/ { sub(/#.*/, "") } NF==0 { next } /^ServerName/ { for (j = 1; j <= npat; j++) if (match($2, apat[j])) print $2 } /^ServerAlias/ { for (i = 2; i <= NF; i++) for (j = 1; j <= npat; j++) if (match($i, apat[j])) print $i }' /etc/httpd/sites-enabled/* | tr ' ' '\n' | sed '/^$/d' | sort | uniq } update_dns() { if test ${dry_run:-no} = yes; then echo "$0: ignoring DNS updates:" cat else nsupdate -k $NSKEY 2>&1 | grep -v "update failed: NXRRSET" fi } nscleanup() { if test -s "$CNAMESLIST"; then echo "$0: Removing DNS CNAME records" while read cname do test -z "$cname" && continue cat < $CNAMESLIST for cname do test -z "$cname" && continue cat </dev/null; then return fi else if test -z "$cnames"; then return fi fi echo "$cnames" | diff - $CNAMESLIST nssetup $cnames } if test -n "$DIRCOND_GENEV_NAME"; then case $(pwd) in /etc/httpd/sites-available) for file in /etc/httpd/sites-enabled/* do if test -h $file; then s=$(readlink -f $file) if test "$s" = /etc/httpd/sites-available/$DIRCOND_FILE; then update_cnames break fi fi done ;; /etc/httpd/sites-enabled) update_cnames esac else case $1 in start|restart|force-restart|reload) cnames=$(get_cnames) if test -n "$cnames"; then nssetup $cnames fi ;; stop) nscleanup;; status) echo >&2 "$0: status ignored";; *) echo >&2 "usage: $0 {start|restart|force-restart|reload|stop}" echo >&2 " can be called as dircond(1) handler" esac fi