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