xref: /illumos-gate/usr/src/cmd/svc/milestone/net-svc (revision 84ab085a13f931bc78e7415e7ce921dbaa14fcb3)
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, Version 1.0 only
7# (the "License").  You may not use this file except in compliance
8# with the License.
9#
10# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11# or http://www.opensolaris.org/os/licensing.
12# See the License for the specific language governing permissions
13# and limitations under the License.
14#
15# When distributing Covered Code, include this CDDL HEADER in each
16# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17# If applicable, add the following below this CDDL HEADER, with the
18# fields enclosed by brackets "[]" replaced with your own identifying
19# information: Portions Copyright [yyyy] [name of copyright owner]
20#
21# CDDL HEADER END
22#
23#
24# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27#ident	"%Z%%M%	%I%	%E% SMI"
28
29#
30# This is third phase of TCP/IP startup/configuration.  This script
31# runs after the NIS/NIS+ startup script.  We run things here that may
32# depend on NIS/NIS+ maps.
33#
34
35case "$1" in
36'start')
37	#
38	# In a zone we need this service to be up, but all of the work
39	# it tries to do is irrelevant (and will actually lead to the service
40	# failing if we try to do it), so just bail out.
41	#
42	if [ `/sbin/zonename` != "global" ]; then
43		exit 0
44	fi
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. /lib/svc/share/smf_include.sh
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# update_files
139# This routine takes as a parameter, the name of the respective file
140# (hosts or ipnodes) to update with the new host name and IP address.
141#
142update_files ()
143{
144	filename=$1;
145	# Delete any old lines added by dhcp.
146	/usr/bin/sed -e '/# Added by DHCP$/d' /etc/inet/${filename} \
147	    > /tmp/${filename}_clear.$$
148
149	shift $#	# Clear $0-9 first in case grep fails
150	set -- `/usr/bin/grep "^[ 	]*$ipaddr[ 	]" \
151	    /tmp/${filename}_clear.$$ 2>/dev/null`
152
153	if [ $# -gt 0 ]; then
154		#
155		# IP address is already in the file. Ensure the
156		# associated hostname is the same as the Hostname
157		# property returned by the DHCP server.
158		#
159		/usr/bin/sed -e "/^[ 	]*${ipaddr}[ 	]/d" \
160		    /tmp/${filename}_clear.$$ >/tmp/${filename}.$$
161		echo "${ipaddr}\t${hostname}\t# Added by DHCP" \
162		    >>/tmp/${filename}.$$
163	else
164		#
165		# IP address is missing from the respective file.  Now check
166		# to see if the hostname is present with a different IP.
167		#
168		shift $#	# Clear $0-9 in case grep fails
169		set -- `/usr/bin/grep -s -v '^#' /tmp/${filename}_clear.$$ | \
170		    /usr/bin/egrep "[	 ]${hostname}([	 ]|$)"`
171
172		if [ $# -gt 0 ]; then
173			#
174			# Hostname is present in the file. Rewrite this line
175			# to have the new IP address and the DHCP comment.
176			#
177			/usr/bin/sed -e "/^[ 	]*${1}[ 	]/d" \
178			    /tmp/${filename}_clear.$$ >/tmp/${filename}.$$
179
180			shift	# Shift off $1 (the old IP)
181
182			echo "$ipaddr $*\c" | /usr/bin/tr ' ' '\t' \
183			    >>/tmp/${filename}.$$
184
185			echo "\t# Added by DHCP" >>/tmp/${filename}.$$
186		else
187			#
188			# Hostname is not present in the named file.
189			# Add a new line for the host at the end of
190			# the new respective file.
191			#
192			/usr/bin/mv /tmp/${filename}_clear.$$ \
193			    /tmp/${filename}.$$
194			echo "${ipaddr}\t${hostname}\t# Added by DHCP" \
195			    >>/tmp/${filename}.$$
196		fi
197	fi
198
199	/usr/bin/rm -f /tmp/${filename}_clear.$$
200	mv_file /tmp/${filename}.$$ /etc/inet/${filename} 444
201}
202
203#
204# We now need to reset the netmask and broadcast address for our network
205# interfaces.  Since this may result in a name service lookup, we want to
206# now wait for NIS to come up if we previously started it.
207#
208domain=`/usr/bin/domainname 2>/dev/null`
209
210[ -z "$domain" ] || [ ! -d /var/yp/binding/$domain ] || wait_nis || \
211    echo "WARNING: Timed out waiting for NIS to come up" >& 2
212
213#
214# Re-set the netmask and broadcast addr for all IP interfaces.  This ifconfig
215# is run here, after waiting for name services, so that "netmask +" will find
216# the netmask if it lives in a NIS map. The 'D' in -auD tells ifconfig NOT to
217# mess with the interface if it is under DHCP control
218#
219/usr/sbin/ifconfig -auD4 netmask + broadcast +
220
221# Uncomment these lines to print complete network interface configuration
222# echo "network interface configuration:"
223# /usr/sbin/ifconfig -a
224
225smf_netstrategy
226
227if [ "$_INIT_NET_STRATEGY" = "dhcp" ]; then
228	dnsservers=`/sbin/dhcpinfo DNSserv`
229else
230	dnsservers=""
231fi
232
233if [ -n "$dnsservers" ]; then
234	#
235	# Go through /etc/resolv.conf and replace any existing
236	# domain or nameserver entries with new ones derived
237	# from DHCP.  Note that it is important to preserve
238	# order of domain entries vs. search entries; the search
239	# entries are reserved for administrator customization
240	# and if placed after the domain entry will override it.
241	# See resolv.conf(4).
242	#
243	if [ ! -f /etc/resolv.conf ]; then
244		/usr/bin/touch /etc/resolv.conf
245	fi
246	dnsdomain=`/sbin/dhcpinfo DNSdmain`
247	export dnsservers dnsdomain
248	/usr/bin/nawk </etc/resolv.conf >/tmp/resolv.conf.$$ '
249		function writedomain() {
250			if (updated == 0) {
251			    	# Use only first domain, not a search list
252			    	split(ENVIRON["dnsdomain"], d)
253				if(length(d[1]) != 0)
254					printf("domain %s\n", d[1])
255			}
256			++updated
257		}
258		$1 == "domain" { writedomain(); next }
259		$1 != "nameserver" { print $0 }
260		END {
261			writedomain()
262			n = split(ENVIRON["dnsservers"], s)
263			for (i = 1; i <= n; ++i)
264				printf("nameserver %s\n", s[i])
265		}'
266	unset dnsservers dnsdomain
267	mv_file /tmp/resolv.conf.$$ /etc/resolv.conf 644
268	#
269	# Add dns to the nsswitch file, if it isn't already there.
270	#
271	update_nss hosts
272	update_nss ipnodes
273
274elif /usr/bin/grep '# Added by DHCP$' /etc/nsswitch.conf >/dev/null 2>&1; then
275
276	# If we added DNS to the hosts and ipnodes policy in the nsswitch,
277	# remove it.
278	/usr/bin/sed \
279	    -e '/# Added by DHCP$/d' \
280	    -e 's/^\(#hosts:\)\(.*[^#]\)\(#.*\)$/hosts: \2/' \
281	    -e 's/^\(#ipnodes:\)\(.*[^#]\)\(#.*\)$/ipnodes: \2/' \
282	    /etc/nsswitch.conf >/tmp/nsswitch.conf.$$
283
284	mv_file /tmp/nsswitch.conf.$$ /etc/nsswitch.conf 644
285fi
286
287if [ "$_INIT_NET_STRATEGY" = "dhcp" ]; then
288
289	hostname=`/usr/bin/uname -n`
290	ipaddr=`/sbin/dhcpinfo Yiaddr`
291	update_files hosts
292	update_files ipnodes
293
294else
295	# We're not using a dhcp strategy, so host entries added by
296	# DHCP should be removed from /etc/inet/hosts and /etc/inet/ipnodes.
297
298	if /usr/bin/grep '# Added by DHCP$' /etc/inet/hosts >/dev/null 2>&1;
299	    then
300		/usr/bin/sed -e '/# Added by DHCP$/d' \
301		    /etc/inet/hosts > /tmp/hosts.$$
302		mv_file /tmp/hosts.$$ /etc/inet/hosts 444
303	fi
304
305	if /usr/bin/grep '# Added by DHCP$' /etc/inet/ipnodes >/dev/null 2>&1;
306	    then
307		/usr/bin/sed -e '/# Added by DHCP$/d' \
308		    /etc/inet/ipnodes > /tmp/ipnodes.$$
309		mv_file /tmp/ipnodes.$$ /etc/inet/ipnodes 444
310	fi
311fi
312
313#
314# Load the IPQoS configuration.
315# This is backgrounded so that any remote hostname lookups it performs
316# don't unduely delay startup. Any messages go via syslog.
317#
318
319if [ -f /usr/sbin/ipqosconf -a -f /etc/inet/ipqosinit.conf ]; then
320	/usr/sbin/ipqosconf -s -a /etc/inet/ipqosinit.conf &
321fi
322
323#
324# Add a static route for multicast packets out our default interface.
325# The default interface is the interface that corresponds to the node name.
326# Run in background subshell to avoid waiting for name service.
327#
328
329(
330if [ "$_INIT_NET_STRATEGY" = "dhcp" ]; then
331	mcastif=`/sbin/dhcpinfo Yiaddr` || mcastif=$_INIT_UTS_NODENAME
332else
333	mcastif=$_INIT_UTS_NODENAME
334fi
335
336echo "Setting default IPv4 interface for multicast:" \
337    "add net 224.0/4: gateway $mcastif"
338
339/usr/sbin/route -n add -interface 224.0/4 -gateway "$mcastif" >/dev/null
340) &
341