Initial import
[samba] / examples / scripts / wins_hook / dns_update
1 #!/bin/sh
2 #
3 # Example script for "wins hook".  This attempts to update the DNS with
4 # new A records for the NETBIOS name that Samba passes us. We do this
5 # the simple way, by deleting all DNS records for the name and then
6 # readding all the expected 'A' records.
7 #
8 # Written by Stephen Rothwell <sfr@linuxcare.com>
9 #
10
11 #
12 # Configurable things
13 #
14 # The domain in which to create names
15 #   YOU MUST CHANGE THIS
16 # N.B. include the trailing dot
17 #
18 # It is a good idea to use a subdomain of your primary domain to ensure
19 # that rogue machines can't take over (or delete) important names on
20 # your network.
21 DOMAIN=wins.example.com.
22
23 #
24 # The DNS TTL to give the records (in seconds)
25 #
26 TTL=3600
27 #
28 # NETBIOS name types that we want to create DNS records for:
29 #       20 is server
30 #       00 is workstation
31 #       03 is user
32 #
33 USEFUL_TYPES="20 00 03"
34 #
35 # The name of a cache file to use to avoid continual updates
36 # of the same name and IP addresses.  If you comment this out
37 # then the cache is not kept at all.
38 #
39 #CACHE_FILE=/usr/local/samba/var/wins_update.cache
40
41 if [ $# -lt 4 ]; then
42         echo "Usage: $0 op name type ttl [ip_addr ...]" 1>&2
43         echo "       op is one of add, refresh, delete" 1>&2
44         echo "       name is the NETBIOS name" 1>&2
45         echo "       type is the NETBIOS name type" 1>&2
46         echo "       ttl is the NETBIOS time to live" 1>&2
47         echo "       ip_addr's are the remaining IP addresses for this name" 1>&2
48         exit 1
49 fi
50
51 NSUPDATE=`which nsupdate`
52 [ -x "$NSUPDATE" ] || NSUPDATE=/usr/bin/nsupdate
53 [ -x "$NSUPDATE" ] || NSUPDATE=/sbin/nsupdate
54 [ -x "$NSUPDATE" ] || NSUPDATE=/usr/sbin/nsupdate
55 [ -x "$NSUPDATE" ] || {
56         echo "Cannot find nsupdate." 1>&2
57         exit 1
58 }
59
60 OP=$1
61 NAME=$2
62 TYPE=$3
63 WINS_TTL=$4
64 shift 4
65 IP_ADDRS="$@"
66
67 do_update=0
68 for i in $USEFUL_TYPES
69 do
70         [ "$TYPE" = "$i" ] && do_update=1
71 done
72 [ $do_update = 1 ] || exit 0
73
74 if [ -n "$CACHE_FILE" ]; then
75         if [ -r "$CACHE_FILE" ]; then
76                 fgrep -q -x -i "$NAME $IP_ADDRS" "$CACHE_FILE" &&
77                         exit 0
78                 grep -v -i "^$NAME " "$CACHE_FILE" >"$CACHE_FILE".$$
79         fi
80         echo "$NAME $IP_ADDRS" >>"$CACHE_FILE".$$
81         mv "$CACHE_FILE" "$CACHE_FILE".old 2>/dev/null
82         mv "$CACHE_FILE".$$ "$CACHE_FILE"
83 fi
84
85 {
86         echo update delete $NAME.$DOMAIN
87         for i in $IP_ADDRS
88         do
89                 echo update add $NAME.$DOMAIN $TTL A $i
90         done
91         echo
92 } 2>/dev/null | $NSUPDATE >/dev/null 2>&1 &
93
94 exit 0