[KLUG Members] Samba wins-to-dns

members@kalamazoolinux.org members@kalamazoolinux.org
Mon, 19 Aug 2002 07:40:01 -0400


>>Yep.  All sequences to nsupdate must end in a blank line,  the blank
>>line means "nsupdate, now process everything I've sent you so far."  A
>>nice undocumented requirement that kept me stumped for hours.
><I can feel my blood pressure cycling... up... down... up... down...>

Ah! That senseation!

My dns_update script called by wins hook is as follows:

#!/bin/sh
#

DOMAIN=morrison.iserv.net.

#
# The DNS TTL to give the records (in seconds)
#
TTL=3600
#
# NETBIOS name types that we want to create DNS records for:
#
20 is server
#
00 is workstation
#
03 is user
#
USEFUL_TYPES="20 00"
#
# The name of a cache file to use to avoid continual updates
# of the same name and IP addresses.  If you comment this out
# then the cache is not kept at all.
#
#CACHE_FILE=/usr/local/samba/var/wins_update.cache

if [ $# -lt 4 ]; then
	echo "Usage: $0 op name type ttl [ip_addr ...]" 1>&2
	echo "       op is one of add, refresh, delete" 1>&2
	echo "       name is the NETBIOS name" 1>&2
	echo "       type is the NETBIOS name type" 1>&2
	echo "       ttl is the NETBIOS time to live" 1>&2
	echo "       ip_addr's are the remaining IP addresses for this name" 1>&2
	exit 1
fi

NSUPDATE=`which nsupdate`
[ -x "$NSUPDATE" ] || NSUPDATE=/usr/bin/nsupdate
[ -x "$NSUPDATE" ] || NSUPDATE=/sbin/nsupdate
[ -x "$NSUPDATE" ] || NSUPDATE=/usr/sbin/nsupdate
[ -x "$NSUPDATE" ] || {
	echo "Cannot find nsupdate." 1>&2
	exit 1
}

OP=$1
NAME=$2
TYPE=$3
WINS_TTL=$4
shift 4
IP_ADDRS="$@"

do_update=0
for i in $USEFUL_TYPES
do
	[ "$TYPE" = "$i" ] && do_update=1
done
[ $do_update = 1 ] || exit 0

if [ -n "$CACHE_FILE" ]; then
	if [ -r "$CACHE_FILE" ]; then
		fgrep -q -x -i "$NAME $IP_ADDRS" "$CACHE_FILE" &&
			exit 0
		grep -v -i "^$NAME " "$CACHE_FILE" >"$CACHE_FILE".$$
	fi
	echo "$NAME $IP_ADDRS" >>"$CACHE_FILE".$$
	mv "$CACHE_FILE" "$CACHE_FILE".old 2>/dev/null
	mv "$CACHE_FILE".$$ "$CACHE_FILE"
fi

{
	echo update delete $NAME.$DOMAIN
	for i in $IP_ADDRS
	do
		echo update add $NAME.$DOMAIN $TTL A $i
	done
	echo

        for i in $IP_ADDRS
        do
                IP1=`echo $i | cut -f1 -d"."`
                IP2=`echo $i | cut -f2 -d"."`
                IP3=`echo $i | cut -f3 -d"."`
                IP4=`echo $i | cut -f4 -d"."`
                i="$IP4.$IP3.$IP2.$IP1"
                echo update delete $i.in-addr.arpa
                echo update add $i.in-addr.arpa $TTL IN PTR $NAME.$DOMAIN
        done
        echo
} 2>/dev/null | $NSUPDATE >/dev/null 2>&1 &

exit 0