xref: /illumos-gate/usr/src/cmd/svc/milestone/net-svc (revision 63360950109af2ce85a962ca61f40b8782f11100)
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
57
58# If boot variables are not set, set variables we use
59[ -z "$_INIT_UTS_NODENAME" ] && _INIT_UTS_NODENAME=`/usr/bin/uname -n`
60
61#
62# wait_nis
63# Wait up to 5 seconds for ypbind to obtain a binding.
64#
65wait_nis ()
66{
67	for i in 1 2 3 4 5; do
68		server=`/usr/bin/ypwhich 2>/dev/null`
69		[ $? -eq 0 -a -n "$server" ] && return 0 || sleep 1
70	done
71	return 1
72}
73
74#
75# This function takes two file names and the file mode as input. The two
76# files are compared for differences (using cmp(1)) and if different, the
77# second file is over written with the first. A chmod is done with the file
78# mode passed in. If the files are equal, the first file passed
79# in (the /tmp file) is deleted.
80#
81mv_file ()
82{
83	/usr/bin/cmp -s $1 $2
84	if [ $? -eq 1 ]; then
85		/usr/bin/mv $1 $2
86		#
87		# The umask during boot is configurable, which requires
88		# explicit setting of file permission modes when we
89		# create files.
90		#
91		/usr/bin/chmod $3 $2
92	else
93		/usr/bin/rm $1
94	fi
95}
96
97#
98# update_nss
99# This routine takes as a parameter, the name of the respective policy
100# to change in the nsswitch.conf (hosts or ipnodes) to update with dns.
101#
102update_nss ()
103{
104	policy=$1;
105	# Add dns to the nsswitch file, if it isn't already there.
106	/usr/bin/awk ' $1 ~ /^'${policy}':/ {
107		n = split($0, a);
108		newl = a[1];
109		if ($0 !~ /dns/) {
110			printf("#%s # Commented out by DHCP\n", $0);
111			updated = 0;
112			for (i = 2; i <= n; i++) {
113				if (updated == 0 && index(a[i], "[") == 1) {
114					newl = newl" dns";
115					updated++;
116				}
117				newl = newl" "a[i];
118			}
119			if (updated == 0) {
120				newl = newl" dns";
121				updated++;
122			}
123			if (updated != 0)
124				newl = newl" # Added by DHCP";
125			else
126				newl = $0;
127			printf("%s\n", newl);
128		} else
129			printf("%s\n", $0);
130	} $1 !~ /^'${policy}':/ { printf("%s\n", $0); }' /etc/nsswitch.conf \
131	    >/tmp/nsswitch.conf.$$
132
133	mv_file /tmp/nsswitch.conf.$$ /etc/nsswitch.conf 644
134}
135
136#
137# update_hosts_file
138# This routine updates the /etc/inet/hosts file with the hostname and IP
139# address that was obtained via DHCP.
140#
141update_hosts_file ()
142{
143	filename=hosts
144	# Delete any old lines added by dhcp.
145	/usr/bin/sed -e '/# Added by DHCP$/d' /etc/inet/${filename} \
146	    > /tmp/${filename}_clear.$$
147
148	shift $#	# Clear $0-9 first in case grep fails
149	set -- `/usr/bin/grep "^[ 	]*$ipaddr[ 	]" \
150	    /tmp/${filename}_clear.$$ 2>/dev/null`
151
152	if [ $# -gt 0 ]; then
153		#
154		# IP address is already in the file. Ensure the
155		# associated hostname is the same as the Hostname
156		# property returned by the DHCP server.
157		#
158		/usr/bin/sed -e "/^[ 	]*${ipaddr}[ 	]/d" \
159		    /tmp/${filename}_clear.$$ >/tmp/${filename}.$$
160		echo "${ipaddr}\t${hostname}\t# Added by DHCP" \
161		    >>/tmp/${filename}.$$
162	else
163		#
164		# IP address is missing from the respective file.  Now check
165		# to see if the hostname is present with a different IP.
166		#
167		shift $#	# Clear $0-9 in case grep fails
168		set -- `/usr/bin/grep -s -v '^#' /tmp/${filename}_clear.$$ | \
169		    /usr/bin/egrep "[	 ]${hostname}([	 ]|$)"`
170
171		if [ $# -gt 0 ]; then
172			#
173			# Hostname is present in the file. Rewrite this line
174			# to have the new IP address and the DHCP comment.
175			#
176			/usr/bin/sed -e "/^[ 	]*${1}[ 	]/d" \
177			    /tmp/${filename}_clear.$$ >/tmp/${filename}.$$
178
179			shift	# Shift off $1 (the old IP)
180
181			echo "$ipaddr $*\c" | /usr/bin/tr ' ' '\t' \
182			    >>/tmp/${filename}.$$
183
184			echo "\t# Added by DHCP" >>/tmp/${filename}.$$
185		else
186			#
187			# Hostname is not present in the named file.
188			# Add a new line for the host at the end of
189			# the new respective file.
190			#
191			/usr/bin/mv /tmp/${filename}_clear.$$ \
192			    /tmp/${filename}.$$
193			echo "${ipaddr}\t${hostname}\t# Added by DHCP" \
194			    >>/tmp/${filename}.$$
195		fi
196	fi
197
198	/usr/bin/rm -f /tmp/${filename}_clear.$$
199	mv_file /tmp/${filename}.$$ /etc/inet/${filename} 444
200}
201
202#
203# We now need to reset the netmask and broadcast address for our network
204# interfaces.  Since this may result in a name service lookup, we want to
205# now wait for NIS to come up if we previously started it.
206#
207domain=`/usr/bin/domainname 2>/dev/null`
208
209[ -z "$domain" ] || [ ! -d /var/yp/binding/$domain ] || wait_nis || \
210    echo "WARNING: Timed out waiting for NIS to come up" >& 2
211
212#
213# Re-set the netmask and broadcast addr for all IP interfaces.  This ifconfig
214# is run here, after waiting for name services, so that "netmask +" will find
215# the netmask if it lives in a NIS map. The 'D' in -auD tells ifconfig NOT to
216# mess with the interface if it is under DHCP control
217#
218/usr/sbin/ifconfig -auD4 netmask + broadcast +
219
220# Uncomment these lines to print complete network interface configuration
221# echo "network interface configuration:"
222# /usr/sbin/ifconfig -a
223
224smf_netstrategy
225
226if [ "$_INIT_NET_STRATEGY" = "dhcp" ]; then
227	dnsservers=`/sbin/dhcpinfo DNSserv`
228else
229	dnsservers=""
230fi
231
232if [ -n "$dnsservers" ]; then
233	#
234	# Go through /etc/resolv.conf and replace any existing
235	# domain or nameserver entries with new ones derived
236	# from DHCP.  Note that it is important to preserve
237	# order of domain entries vs. search entries; the search
238	# entries are reserved for administrator customization
239	# and if placed after the domain entry will override it.
240	# See resolv.conf(4).
241	#
242	if [ ! -f /etc/resolv.conf ]; then
243		/usr/bin/touch /etc/resolv.conf
244	fi
245	dnsdomain=`/sbin/dhcpinfo DNSdmain`
246	export dnsservers dnsdomain
247	/usr/bin/nawk </etc/resolv.conf >/tmp/resolv.conf.$$ '
248		function writedomain() {
249			if (updated == 0) {
250			    	# Use only first domain, not a search list
251			    	split(ENVIRON["dnsdomain"], d)
252				if(length(d[1]) != 0)
253					printf("domain %s\n", d[1])
254			}
255			++updated
256		}
257		$1 == "domain" { writedomain(); next }
258		$1 != "nameserver" { print $0 }
259		END {
260			writedomain()
261			n = split(ENVIRON["dnsservers"], s)
262			for (i = 1; i <= n; ++i)
263				printf("nameserver %s\n", s[i])
264		}'
265	unset dnsservers dnsdomain
266	mv_file /tmp/resolv.conf.$$ /etc/resolv.conf 644
267	#
268	# Add dns to the nsswitch file, if it isn't already there.
269	#
270	update_nss hosts
271	update_nss ipnodes
272
273elif /usr/bin/grep '# Added by DHCP$' /etc/nsswitch.conf >/dev/null 2>&1; then
274
275	# If we added DNS to the hosts and ipnodes policy in the nsswitch,
276	# remove it.
277	/usr/bin/sed \
278	    -e '/# Added by DHCP$/d' \
279	    -e 's/^\(#hosts:\)\(.*[^#]\)\(#.*\)$/hosts: \2/' \
280	    -e 's/^\(#ipnodes:\)\(.*[^#]\)\(#.*\)$/ipnodes: \2/' \
281	    /etc/nsswitch.conf >/tmp/nsswitch.conf.$$
282
283	mv_file /tmp/nsswitch.conf.$$ /etc/nsswitch.conf 644
284fi
285
286if [ "$_INIT_NET_STRATEGY" = "dhcp" ]; then
287
288	hostname=`/usr/bin/uname -n`
289	ipaddr=`/sbin/dhcpinfo Yiaddr`
290	update_hosts_file
291
292else
293	# We're not using a dhcp strategy, so host entries added by
294	# DHCP should be removed from /etc/inet/hosts.
295
296	if /usr/bin/grep '# Added by DHCP$' /etc/inet/hosts >/dev/null 2>&1;
297	    then
298		/usr/bin/sed -e '/# Added by DHCP$/d' \
299		    /etc/inet/hosts > /tmp/hosts.$$
300		mv_file /tmp/hosts.$$ /etc/inet/hosts 444
301	fi
302fi
303
304#
305# Load the IPQoS configuration.
306# This is backgrounded so that any remote hostname lookups it performs
307# don't unduely delay startup. Any messages go via syslog.
308#
309
310if [ -f /usr/sbin/ipqosconf -a -f /etc/inet/ipqosinit.conf ]; then
311	/usr/sbin/ipqosconf -s -a /etc/inet/ipqosinit.conf &
312fi
313
314#
315# Add a static route for multicast packets out our default interface.
316# The default interface is the interface that corresponds to the node name.
317# Run in background subshell to avoid waiting for name service.
318#
319
320(
321if [ "$_INIT_NET_STRATEGY" = "dhcp" ]; then
322	mcastif=`/sbin/dhcpinfo Yiaddr` || mcastif=$_INIT_UTS_NODENAME
323else
324	mcastif=$_INIT_UTS_NODENAME
325fi
326
327echo "Setting default IPv4 interface for multicast:" \
328    "add net 224.0/4: gateway $mcastif"
329
330/usr/sbin/route -n add -interface 224.0/4 -gateway "$mcastif" >/dev/null
331) &
332