xref: /titanic_41/usr/src/cmd/svc/milestone/net-svc (revision ba2be53024c0b999e74ba9adcd7d80fec5df8c57)
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 2007 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26# ident	"%Z%%M%	%I%	%E% SMI"
27
28#
29# This is third phase of TCP/IP startup/configuration.  This script
30# runs after the NIS/NIS+ startup script.  We run things here that may
31# depend on NIS/NIS+ maps.
32#
33
34. /lib/svc/share/smf_include.sh
35
36case "$1" in
37'start')
38	#
39	# In a shared-IP zone we need this service to be up, but all of the
40	# work it tries to do is irrelevant (and will actually lead to the
41	# service failing if we try to do it), so just bail out.
42	# In the global zone and exclusive-IP zones we proceed.
43	#
44	smf_configure_ip || exit 0
45	;; # Fall through -- rest of script is the initialization code
46
47'stop')
48	exit 0
49	;;
50
51*)
52	echo "Usage: $0 { start | stop }"
53	exit 1
54	;;
55esac
56
57interface=$2
58
59# If boot variables are not set, set variables we use
60[ -z "$_INIT_UTS_NODENAME" ] && _INIT_UTS_NODENAME=`/usr/bin/uname -n`
61
62#
63# wait_nis
64# Wait up to 5 seconds for ypbind to obtain a binding.
65#
66wait_nis ()
67{
68	for i in 1 2 3 4 5; do
69		server=`/usr/bin/ypwhich 2>/dev/null`
70		[ $? -eq 0 -a -n "$server" ] && return 0 || sleep 1
71	done
72	return 1
73}
74
75#
76# This function takes two file names and the file mode as input. The two
77# files are compared for differences (using cmp(1)) and if different, the
78# second file is over written with the first. A chmod is done with the file
79# mode passed in. If the files are equal, the first file passed
80# in (the /tmp file) is deleted.
81#
82mv_file ()
83{
84	/usr/bin/cmp -s $1 $2
85	if [ $? -eq 1 ]; then
86		/usr/bin/mv $1 $2
87		#
88		# The umask during boot is configurable, which requires
89		# explicit setting of file permission modes when we
90		# create files.
91		#
92		/usr/bin/chmod $3 $2
93	else
94		/usr/bin/rm $1
95	fi
96}
97
98#
99# update_nss
100# This routine takes as a parameter, the name of the respective policy
101# to change in the nsswitch.conf (hosts or ipnodes) to update with dns.
102#
103update_nss ()
104{
105	policy=$1;
106	# Add dns to the nsswitch file, if it isn't already there.
107	/usr/bin/awk ' $1 ~ /^'${policy}':/ {
108		n = split($0, a);
109		newl = a[1];
110		if ($0 !~ /dns/) {
111			printf("#%s # Commented out by DHCP\n", $0);
112			updated = 0;
113			for (i = 2; i <= n; i++) {
114				if (updated == 0 && index(a[i], "[") == 1) {
115					newl = newl" dns";
116					updated++;
117				}
118				newl = newl" "a[i];
119			}
120			if (updated == 0) {
121				newl = newl" dns";
122				updated++;
123			}
124			if (updated != 0)
125				newl = newl" # Added by DHCP";
126			else
127				newl = $0;
128			printf("%s\n", newl);
129		} else
130			printf("%s\n", $0);
131	} $1 !~ /^'${policy}':/ { printf("%s\n", $0); }' /etc/nsswitch.conf \
132	    >/tmp/nsswitch.conf.$$
133
134	mv_file /tmp/nsswitch.conf.$$ /etc/nsswitch.conf 644
135}
136
137#
138# We now need to reset the netmask and broadcast address for our network
139# interfaces.  Since this may result in a name service lookup, we want to
140# now wait for NIS to come up if we previously started it.
141#
142domain=`/usr/bin/domainname 2>/dev/null`
143
144[ -z "$domain" ] || [ ! -d /var/yp/binding/$domain ] || wait_nis || \
145    echo "WARNING: Timed out waiting for NIS to come up" >& 2
146
147#
148# Re-set the netmask and broadcast addr for all IP interfaces.  This ifconfig
149# is run here, after waiting for name services, so that "netmask +" will find
150# the netmask if it lives in a NIS map. The 'D' in -auD tells ifconfig NOT to
151# mess with the interface if it is under DHCP control
152#
153/usr/sbin/ifconfig -auD4 netmask + broadcast +
154
155# Uncomment these lines to print complete network interface configuration
156# echo "network interface configuration:"
157# /usr/sbin/ifconfig -a
158
159smf_netstrategy
160
161if [ "$_INIT_NET_STRATEGY" = "dhcp" ]; then
162	if [ -n "$interface" ]; then
163		dnsservers=`/sbin/dhcpinfo -i $interface DNSserv`
164	else
165		dnsservers=`/sbin/dhcpinfo DNSserv`
166	fi
167else
168	dnsservers=""
169fi
170
171if [ -n "$dnsservers" ]; then
172	#
173	# Go through /etc/resolv.conf and replace any existing
174	# domain or nameserver entries with new ones derived
175	# from DHCP.  Note that it is important to preserve
176	# order of domain entries vs. search entries; the search
177	# entries are reserved for administrator customization
178	# and if placed after the domain entry will override it.
179	# See resolv.conf(4).
180	#
181	if [ ! -f /etc/resolv.conf ]; then
182		/usr/bin/touch /etc/resolv.conf
183	fi
184	if [ -n "$interface" ]; then
185		dnsdomain=`/sbin/dhcpinfo -i $interface DNSdmain`
186	else
187		dnsdomain=`/sbin/dhcpinfo DNSdmain`
188	fi
189	export dnsservers dnsdomain
190	/usr/bin/nawk </etc/resolv.conf >/tmp/resolv.conf.$$ '
191		function writedomain() {
192			if (updated == 0) {
193			    	# Use only first domain, not a search list
194			    	split(ENVIRON["dnsdomain"], d)
195				if(length(d[1]) != 0)
196					printf("domain %s\n", d[1])
197			}
198			++updated
199		}
200		$1 == "domain" { writedomain(); next }
201		$1 != "nameserver" { print $0 }
202		END {
203			writedomain()
204			n = split(ENVIRON["dnsservers"], s)
205			for (i = 1; i <= n; ++i)
206				printf("nameserver %s\n", s[i])
207		}'
208	unset dnsservers dnsdomain
209	mv_file /tmp/resolv.conf.$$ /etc/resolv.conf 644
210	#
211	# Add dns to the nsswitch file, if it isn't already there.
212	#
213	update_nss hosts
214	update_nss ipnodes
215
216elif /usr/bin/grep '# Added by DHCP$' /etc/nsswitch.conf >/dev/null 2>&1; then
217
218	# If we added DNS to the hosts and ipnodes policy in the nsswitch,
219	# remove it.
220	/usr/bin/sed \
221	    -e '/# Added by DHCP$/d' \
222	    -e 's/^\(#hosts:\)\(.*[^#]\)\(#.*\)$/hosts: \2/' \
223	    -e 's/^\(#ipnodes:\)\(.*[^#]\)\(#.*\)$/ipnodes: \2/' \
224	    /etc/nsswitch.conf >/tmp/nsswitch.conf.$$
225
226	mv_file /tmp/nsswitch.conf.$$ /etc/nsswitch.conf 644
227fi
228
229# Clean up any old DHCP-added entries (except loopback) in the hosts file.
230if /usr/bin/grep '# Added by DHCP$' /etc/inet/hosts >/dev/null 2>&1; then
231	/usr/bin/nawk '{
232		if (index($0, "# Added by DHCP") == 0 ||
233		    $1 == "127.0.0.1" || $1 == "::1") {
234			print $0
235		}
236	}' /etc/inet/hosts > /tmp/hosts.$$
237	mv_file /tmp/hosts.$$ /etc/inet/hosts 444
238fi
239
240if [ -z "$SMF_FMRI" ] || [ "$SMF_FMRI" = "svc:/network/physical:nwam" ]; then
241        exit 0
242fi
243
244#
245# Load the IPQoS configuration.
246# This is backgrounded so that any remote hostname lookups it performs
247# don't unduely delay startup. Any messages go via syslog.
248#
249
250if [ -f /usr/sbin/ipqosconf -a -f /etc/inet/ipqosinit.conf ]; then
251        /usr/sbin/ipqosconf -s -a /etc/inet/ipqosinit.conf &
252fi
253