1#!/sbin/sh
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# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24#
25
26#
27# Install DNS client service
28#
29
30. /lib/svc/share/smf_include.sh
31. /lib/svc/share/net_include.sh
32
33SVCCFG=/usr/sbin/svccfg
34SVCPROP=/usr/bin/svcprop
35SVCADM=/usr/sbin/svcadm
36
37DNS_NWAM_FMRI="svc:/network/physical:nwam"
38DNS_INSTALL_FMRI=$SMF_FMRI
39
40DNS_INSTALL_PG="install_props"
41
42DNS_UNDEFINED_STRING_PROP="\"\""
43
44dns_install_debug=0
45
46unset dns_install_domain dns_install_servers dns_install_search
47
48dns_process_install_pg()
49{
50	dns_install_domain=""
51	dns_install_servers=""
52	dns_install_search=""
53	config=0
54
55	#
56	# Retrieve the name server property values.
57	#
58	prop=`$SVCPROP -p $DNS_INSTALL_PG/nameserver $DNS_INSTALL_FMRI`
59	if [ $? -eq 0 -a "$prop" != "$NET_INADDR_ANY" ]; then
60		dns_install_servers=$prop
61		config=1
62	fi
63
64	#
65	# Retrieve the name service domain.
66	#
67	prop=`$SVCPROP -p $DNS_INSTALL_PG/domain $DNS_INSTALL_FMRI`
68	if [ $? -eq 0 -a "$prop" != "$DNS_UNDEFINED_STRING_PROP" ]; then
69		dns_install_domain=$prop
70		config=1
71	fi
72
73	#
74	# Retrieve the search list.
75	#
76	prop=`$SVCPROP -p $DNS_INSTALL_PG/search $DNS_INSTALL_FMRI`
77	if [ $? -eq 0 -a "$prop" != "$DNS_UNDEFINED_STRING_PROP" ]; then
78		dns_install_search=$prop
79		config=1
80	fi
81
82	[ $config -ne 0 ] || return $SMF_EXIT_OK
83
84	#
85	# Create the resolv.conf file.
86	#
87	/usr/bin/touch /etc/resolv.conf.$$
88	if [ $? -ne 0 ]; then
89		net_record_err "Error creating \"/etc/resolv.conf.$$\"" $?
90		return $SMF_EXIT_ERR_FATAL
91	fi
92
93	for j in $dns_install_servers
94	do
95		server=`echo $j | /usr/bin/sed s/\"//g`
96		echo "nameserver $server" >>/etc/resolv.conf.$$
97	done
98
99	if [ "$dns_install_domain" != "" ]; then
100		echo "domain $dns_install_domain" >>/etc/resolv.conf.$$
101	fi
102
103	if [ "$dns_install_search" != "" ]; then
104		list="search"
105		for j in $dns_install_search
106		do
107			domain=`echo $j | /usr/bin/sed s/\"//g`
108			list="$list $domain"
109		done
110		echo $list >>/etc/resolv.conf.$$
111	fi
112
113	/usr/bin/mv /etc/resolv.conf.$$ /etc/resolv.conf
114	if [ $? -ne 0 ]; then
115		err=$?
116		msg="Error moving /etc/resolv.conf.$$ to \"/etc/resolv.conf\""
117		net_record_err "$msg" $err
118		return $SMF_EXIT_ERR_FATAL
119	fi
120
121	/usr/bin/chmod 644 /etc/resolv.conf
122	if [ $? -ne 0 ]; then
123		err=$?
124		msg="Error setting permissions on \"/etc/resolv.conf\""
125		net_record_err "$msg" $err
126		return $SMF_EXIT_ERR_FATAL
127	fi
128
129	#
130	# Create the nsswitch.conf file
131	#
132	/usr/bin/cp -f /etc/nsswitch.dns /etc/nsswitch.conf
133	if [ $? -ne 0 ]; then
134		err=$?
135		msg="Error copying /etc/nsswitch.dns to \"/etc/nsswitch.conf\""
136		net_record_err "$msg" $err
137		return $SMF_EXIT_ERR_FATAL
138	fi
139
140	/usr/bin/chmod 644 /etc/nsswitch.conf
141	if [ $? -ne 0 ]; then
142		err=$?
143		msg="Error setting permissions on \"/etc/nsswitch.conf\""
144		net_record_err "$msg" $err
145		return $SMF_EXIT_ERR_FATAL
146	fi
147
148	return $SMF_EXIT_OK
149}
150
151dns_process_install()
152{
153	vout=`$SVCCFG -s $DNS_INSTALL_FMRI validate 2>&1`
154	if [ "$vout" != "" ]; then
155		msg="Validation errors in $DNS_INSTALL_FMRI:\n$vout"
156		net_record_err "$msg" 0
157		return $SMF_EXIT_ERR_CONFIG
158	fi
159
160	ecode=$SMF_EXIT_OK
161	errs=0
162	cnt=0
163	pg=`$SVCPROP -p $DNS_INSTALL_PG $DNS_INSTALL_FMRI`
164	if [ $? -eq 0 ]; then
165		if service_is_enabled $DNS_NWAM_FMRI; then
166			echo "NWAM enabled. Install static" \
167			    "DNS configuration ignored." | smf_console
168			errs=`expr $errs +  1`
169			ecode=$SMF_EXIT_ERR_CONFIG
170		else
171			dns_process_install_pg
172			if [ $? -ne $SMF_EXIT_OK ]; then
173				ecode=$?
174				errs=`expr $errs +  1`
175			else
176				cnt=`expr $cnt +  1`
177			fi
178
179		fi
180		$SVCCFG -s $DNS_INSTALL_FMRI delpg $DNS_INSTALL_PG
181		$SVCCFG -s $DNS_INSTALL_FMRI refresh
182	fi
183
184	if [ $dns_install_debug -eq 1 ]; then
185		if [ $errs -ne 0 ]; then
186			echo "$errs errors encountered" \
187			    "configuring DNS on behalf of install"
188		fi
189
190		if [ $cntf -ne 0 ]; then
191			echo "DNS configured on behalf of install"
192		fi
193	fi
194
195	return $ecode
196}
197
198#
199# Script execution starts here.
200#
201dns_process_install || exit $?
202
203$SVCADM disable $DNS_INSTALL_FMRI
204exit $SMF_EXIT_OK
205