1 #!/usr/bin/ksh -p
2 #
3 # CDDL HEADER START
4 #
5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License (the "License").
7 # You may not use this file except in compliance with the License.
8 #
9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 # or http://www.opensolaris.org/os/licensing.
11 # See the License for the specific language governing permissions
12 # and limitations under the License.
13 #
14 # When distributing Covered Code, include this CDDL HEADER in each
15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 # If applicable, add the following below this CDDL HEADER, with the
17 # fields enclosed by brackets "[]" replaced with your own identifying
18 # information: Portions Copyright [yyyy] [name of copyright owner]
19 #
20 # CDDL HEADER END
21 #
22
23 #
24 # Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25 # Use is subject to license terms.
26 #
27 # dngcfg.ksh - sets up DNS server on the local host, and modifies
28 # /etc/resolv.conf to make the host use the local DNS server to
29 # resolve domain name.
30 #
31 # The server's domain is "dns.test.nfs". The server has _nfsv4idmapdomain
32 # defined, its initial value is "dns.test.nfs".
33 #
34 # Notes:
35 # The script modifies some files on the system and it doesn't back up
36 # them. The caller is responsible to do that and restore these files
37 # when necessary.
38
39 [[ -n "$DEBUG" ]] && [[ $DEBUG != 0 ]] && set -x
40
41 # set up script execution environment
42 . ./dom_env
43
44 trap "rm -f $LOGFILE" EXIT
45
46 is_root "$NAME:" "All tests for domain affected"
47
48 # get IPv4 address of first network interface
49 ip_addr=$(ifconfig -a4 | egrep inet | egrep -v '127.0.0.1' | \
50 egrep -v '0.0.0.0' | awk '{print $2}' | head -1 \
51 2> $LOGFILE)
52 ckreturn $? "failed to get the host IP address" $LOGFILE "ERROR" \
53 || return $FAIL
54
55 set -A ip_levels $(echo $ip_addr | sed -e "s/\./ /g")
56
57 # generate the string for reverse zone name
58 revzone="in-addr.arpa"
59 for i in 0 1 2; do
60 revzone=${ip_levels[$i]}.$revzone
61 done
62
63 # populate named.conf file
64 sed -e "s/REVZONE/$revzone/g" ./named.conf.tmpl >/etc/named.conf 2> $LOGFILE
65 ckreturn $? "failed to create /etc/named.conf" $LOGFILE "ERROR" \
66 || return $FAIL
67
68 # clean up /var/named directory
69 rm -rf /var/named 2>/dev/null
70 mkdir -m 0755 /var/named
71
72 node_revip=${ip_levels[3]}
73 node_name=$(uname -n | cut -d. -f1)
74 node_ipaddr=$ip_addr
75
76 # cycle through all zone template files to generate zone files, and
77 # copy them under /var/named
78 for tmpl in $(ls ./*.master.tmpl); do
79 dest=$(echo $tmpl | sed 's/\.tmpl//')
80 sed -e "s/NODE_REVIP/$node_revip/g" \
81 -e "s/NODE_NAME/$node_name/g" \
82 -e "s/NODE_IPADDR/$node_ipaddr/g" \
83 ./$tmpl >/var/named/$dest 2>$LOGFILE
84 ckreturn $? "failed to create /var/named/$dest" $LOGFILE "ERROR" \
85 || return $FAIL
86 done
87
88 # finally, populate /etc/resolv.conf accordingly
89 cat > /etc/resolv.conf << EOF
90 domain dns.test.nfs
91 nameserver $node_ipaddr
92 EOF
93 ckreturn $? "failed to create /etc/resolv.conf" $LOGFILE "ERROR" \
94 || return $FAIL
95
96 # (re)start named server
97 dns_service restart 6 "failed to restart dns service" "ERROR" || return $FAIL
98
99 # check if the DNS server works properly. To do that, we call
100 # get_domain_txt_record() to get the value of _nfsv4idmapdomain on local
101 # DNS server.
102 i=0
103 while true; do
104 sleep 1
105 txt_rr=$(get_domain_txt_record "dns.test.nfs" 2>$LOGFILE)
106 ckreturn $? "failed to get TXT RR from local DNS server" $LOGFILE \
107 "ERROR" || return $FAIL
108 [[ "$txt_rr" == "domain.from.txt" ]] && exit
109 (($i < 12)) || exit 1
110 i=$((i + 1))
111 done