xref: /onnv-gate/usr/src/cmd/svc/milestone/net-svc (revision 11767:8f30d0e611c6)
10Sstevel@tonic-gate#!/sbin/sh
20Sstevel@tonic-gate#
30Sstevel@tonic-gate# CDDL HEADER START
40Sstevel@tonic-gate#
50Sstevel@tonic-gate# The contents of this file are subject to the terms of the
61573Sdp# Common Development and Distribution License (the "License").
71573Sdp# You may not use this file except in compliance with the License.
80Sstevel@tonic-gate#
90Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate# See the License for the specific language governing permissions
120Sstevel@tonic-gate# and limitations under the License.
130Sstevel@tonic-gate#
140Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate#
200Sstevel@tonic-gate# CDDL HEADER END
210Sstevel@tonic-gate#
220Sstevel@tonic-gate#
23*11767SAnurag.Maskey@Sun.COM# Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate# Use is subject to license terms.
250Sstevel@tonic-gate#
260Sstevel@tonic-gate
270Sstevel@tonic-gate#
280Sstevel@tonic-gate# This is third phase of TCP/IP startup/configuration.  This script
2911262SRajagopal.Andra@Sun.COM# runs after the NIS startup script.  We run things here that may
3011262SRajagopal.Andra@Sun.COM# depend on NIS maps.
310Sstevel@tonic-gate#
320Sstevel@tonic-gate
331573Sdp. /lib/svc/share/smf_include.sh
34*11767SAnurag.Maskey@Sun.COM. /lib/svc/share/net_include.sh
35*11767SAnurag.Maskey@Sun.COM
36*11767SAnurag.Maskey@Sun.COMNWAM_FMRI="svc:/network/physical:nwam"
371573Sdp
380Sstevel@tonic-gatecase "$1" in
390Sstevel@tonic-gate'start')
400Sstevel@tonic-gate	#
413448Sdh155122	# In a shared-IP zone we need this service to be up, but all of the
423448Sdh155122	# work it tries to do is irrelevant (and will actually lead to the
433448Sdh155122	# service failing if we try to do it), so just bail out.
443448Sdh155122	# In the global zone and exclusive-IP zones we proceed.
450Sstevel@tonic-gate	#
46*11767SAnurag.Maskey@Sun.COM	smf_configure_ip || exit $SMF_EXIT_OK
47*11767SAnurag.Maskey@Sun.COM
48*11767SAnurag.Maskey@Sun.COM	#
49*11767SAnurag.Maskey@Sun.COM	# If nwam is enabled, the nwam service will handle the tasks performed
50*11767SAnurag.Maskey@Sun.COM	# by this service, so just bail out.
51*11767SAnurag.Maskey@Sun.COM	#
52*11767SAnurag.Maskey@Sun.COM	service_is_enabled $NWAM_FMRI && exit $SMF_EXIT_OK
53*11767SAnurag.Maskey@Sun.COM	;; # fall through -- rest of script is the initialization code
540Sstevel@tonic-gate
550Sstevel@tonic-gate'stop')
56*11767SAnurag.Maskey@Sun.COM	exit $SMF_EXIT_OK
570Sstevel@tonic-gate	;;
580Sstevel@tonic-gate
590Sstevel@tonic-gate*)
600Sstevel@tonic-gate	echo "Usage: $0 { start | stop }"
610Sstevel@tonic-gate	exit 1
620Sstevel@tonic-gate	;;
630Sstevel@tonic-gateesac
640Sstevel@tonic-gate
653938Sjbeckinterface=$2
660Sstevel@tonic-gate
670Sstevel@tonic-gate# If boot variables are not set, set variables we use
680Sstevel@tonic-gate[ -z "$_INIT_UTS_NODENAME" ] && _INIT_UTS_NODENAME=`/usr/bin/uname -n`
690Sstevel@tonic-gate
700Sstevel@tonic-gate#
710Sstevel@tonic-gate# This function takes two file names and the file mode as input. The two
720Sstevel@tonic-gate# files are compared for differences (using cmp(1)) and if different, the
730Sstevel@tonic-gate# second file is over written with the first. A chmod is done with the file
740Sstevel@tonic-gate# mode passed in. If the files are equal, the first file passed
750Sstevel@tonic-gate# in (the /tmp file) is deleted.
760Sstevel@tonic-gate#
770Sstevel@tonic-gatemv_file ()
780Sstevel@tonic-gate{
790Sstevel@tonic-gate	/usr/bin/cmp -s $1 $2
800Sstevel@tonic-gate	if [ $? -eq 1 ]; then
810Sstevel@tonic-gate		/usr/bin/mv $1 $2
820Sstevel@tonic-gate		#
830Sstevel@tonic-gate		# The umask during boot is configurable, which requires
840Sstevel@tonic-gate		# explicit setting of file permission modes when we
850Sstevel@tonic-gate		# create files.
860Sstevel@tonic-gate		#
870Sstevel@tonic-gate		/usr/bin/chmod $3 $2
880Sstevel@tonic-gate	else
890Sstevel@tonic-gate		/usr/bin/rm $1
900Sstevel@tonic-gate	fi
910Sstevel@tonic-gate}
920Sstevel@tonic-gate
930Sstevel@tonic-gate#
947045Sokie# This function takes a DHCP parameter (as defined in /etc/dhcp/inittab)
957045Sokie# and returns the value for that parameter returned by the DHCP server.
967045Sokie# If the global 'interface' is defined, it will request the value learned
977045Sokie# on that interface, else it will request the value learned on the primary
987045Sokie# interface.
997045Sokie#
1007045Sokieget_dhcp_var ()
1017045Sokie{
1027045Sokie	if [ -n "$interface" ]; then
1037045Sokie		/sbin/dhcpinfo -i $interface $1
1047045Sokie	else
1057045Sokie		/sbin/dhcpinfo $1
1067045Sokie	fi
1077045Sokie}
1087045Sokie
1097045Sokie#
1107045Sokie# This function returns true if the string "# Added by DHCP$" occurs in
1117045Sokie# the passed-in file, false otherwise.
1127045Sokie#
1137045Sokiedhcp_edits ()
1147045Sokie{
1157045Sokie	/usr/bin/grep '# Added by DHCP$' $1 >/dev/null 2>&1
1167045Sokie	return $?
1177045Sokie}
1187045Sokie
1197045Sokie#
1207045Sokie# update_resolv()
1217045Sokie# Go through /etc/resolv.conf and replace any existing domain or
1227045Sokie# nameserver entries with new ones derived from DHCP.  Note that
1237045Sokie# it is important to preserve order of domain entries vs. search
1247045Sokie# entries; the search entries are reserved for administrator
1257045Sokie# customization and if placed after the domain entry will override
1267045Sokie# it.  See resolv.conf(4).
1277045Sokie#
1287045Sokie# The first arg should be the dns servers string, the second
1297045Sokie# should be the dns domain.
1307045Sokie#
1317045Sokieupdate_resolv ()
1327045Sokie{
1337045Sokie	dnsservers=$1
1347045Sokie	dnsdomain=$2
1357045Sokie
1367045Sokie	if [ ! -f /etc/resolv.conf ]; then
1377045Sokie		/usr/bin/touch /etc/resolv.conf
1387045Sokie	fi
1397045Sokie	export dnsservers dnsdomain
1407045Sokie	/usr/bin/nawk </etc/resolv.conf >/tmp/resolv.conf.$$ '
1417045Sokie		function writedomain() {
1427045Sokie			if (updated == 0) {
1437045Sokie			    	# Use only first domain, not a search list
1447045Sokie			    	split(ENVIRON["dnsdomain"], d)
1457045Sokie				if(length(d[1]) != 0)
1467045Sokie					printf("domain %s\n", d[1])
1477045Sokie			}
1487045Sokie			++updated
1497045Sokie		}
1507045Sokie		$1 == "domain" { writedomain(); next }
1517045Sokie		$1 != "nameserver" { print $0 }
1527045Sokie		END {
1537045Sokie			writedomain()
1547045Sokie			n = split(ENVIRON["dnsservers"], s)
1557045Sokie			for (i = 1; i <= n; ++i)
1567045Sokie				printf("nameserver %s\n", s[i])
1577045Sokie		}'
1587045Sokie	unset dnsservers dnsdomain
1597045Sokie	mv_file /tmp/resolv.conf.$$ /etc/resolv.conf 644
1607045Sokie}
1617045Sokie
1627045Sokie#
163*11767SAnurag.Maskey@Sun.COM# update_nss()
1640Sstevel@tonic-gate# This routine takes as a parameter, the name of the respective policy
1650Sstevel@tonic-gate# to change in the nsswitch.conf (hosts or ipnodes) to update with dns.
1660Sstevel@tonic-gate#
1670Sstevel@tonic-gateupdate_nss ()
1680Sstevel@tonic-gate{
1690Sstevel@tonic-gate	policy=$1;
1700Sstevel@tonic-gate	# Add dns to the nsswitch file, if it isn't already there.
1710Sstevel@tonic-gate	/usr/bin/awk ' $1 ~ /^'${policy}':/ {
1720Sstevel@tonic-gate		n = split($0, a);
1730Sstevel@tonic-gate		newl = a[1];
1740Sstevel@tonic-gate		if ($0 !~ /dns/) {
1750Sstevel@tonic-gate			printf("#%s # Commented out by DHCP\n", $0);
1760Sstevel@tonic-gate			updated = 0;
1770Sstevel@tonic-gate			for (i = 2; i <= n; i++) {
1780Sstevel@tonic-gate				if (updated == 0 && index(a[i], "[") == 1) {
1790Sstevel@tonic-gate					newl = newl" dns";
1800Sstevel@tonic-gate					updated++;
1810Sstevel@tonic-gate				}
1820Sstevel@tonic-gate				newl = newl" "a[i];
1830Sstevel@tonic-gate			}
1840Sstevel@tonic-gate			if (updated == 0) {
1850Sstevel@tonic-gate				newl = newl" dns";
1860Sstevel@tonic-gate				updated++;
1870Sstevel@tonic-gate			}
1880Sstevel@tonic-gate			if (updated != 0)
1890Sstevel@tonic-gate				newl = newl" # Added by DHCP";
1900Sstevel@tonic-gate			else
1910Sstevel@tonic-gate				newl = $0;
1920Sstevel@tonic-gate			printf("%s\n", newl);
1930Sstevel@tonic-gate		} else
1940Sstevel@tonic-gate			printf("%s\n", $0);
1950Sstevel@tonic-gate	} $1 !~ /^'${policy}':/ { printf("%s\n", $0); }' /etc/nsswitch.conf \
1960Sstevel@tonic-gate	    >/tmp/nsswitch.conf.$$
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate	mv_file /tmp/nsswitch.conf.$$ /etc/nsswitch.conf 644
1990Sstevel@tonic-gate}
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate#
2027045Sokie# Remove any lines with the "# Added by DHCP" tag from /etc/nsswitch.conf;
2037045Sokie# also uncomment hosts and ipnodes entries which were previously commented
2047045Sokie# out by this script.
2050Sstevel@tonic-gate#
2067045Sokiecleanup_nss ()
2077045Sokie{
2080Sstevel@tonic-gate	/usr/bin/sed \
2090Sstevel@tonic-gate	    -e '/# Added by DHCP$/d' \
2100Sstevel@tonic-gate	    -e 's/^\(#hosts:\)\(.*[^#]\)\(#.*\)$/hosts: \2/' \
2110Sstevel@tonic-gate	    -e 's/^\(#ipnodes:\)\(.*[^#]\)\(#.*\)$/ipnodes: \2/' \
2120Sstevel@tonic-gate	    /etc/nsswitch.conf >/tmp/nsswitch.conf.$$
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate	mv_file /tmp/nsswitch.conf.$$ /etc/nsswitch.conf 644
2157045Sokie}
2160Sstevel@tonic-gate
2177045Sokie#
2187045Sokie# Remove any lines with the "# Added by DHCP" tag from /etc/inet/hosts.
2197045Sokie#
2207045Sokiecleanup_hosts ()
2217045Sokie{
2223938Sjbeck	/usr/bin/nawk '{
2233938Sjbeck		if (index($0, "# Added by DHCP") == 0 ||
2243938Sjbeck		    $1 == "127.0.0.1" || $1 == "::1") {
2253938Sjbeck			print $0
2263938Sjbeck		}
2273938Sjbeck	}' /etc/inet/hosts > /tmp/hosts.$$
2283938Sjbeck	mv_file /tmp/hosts.$$ /etc/inet/hosts 444
2297045Sokie}
2307045Sokie
2317045Sokie#
2327045Sokie# If our network configuration strategy is DHCP, check for DNS
2337045Sokie# configuration parameters obtained from the DHCP server.
2347045Sokie#
235*11767SAnurag.Maskey@Sun.COM# Script execution starts here.
2367045Sokie#
237*11767SAnurag.Maskey@Sun.COMsmf_netstrategy
2387045Sokie
239*11767SAnurag.Maskey@Sun.COMif [ "$_INIT_NET_STRATEGY" = "dhcp" ]; then
240*11767SAnurag.Maskey@Sun.COM	dnsservers=`get_dhcp_var DNSserv`
241*11767SAnurag.Maskey@Sun.COM	dnsdomain=`get_dhcp_var DNSdmain`
242*11767SAnurag.Maskey@Sun.COMelse
243*11767SAnurag.Maskey@Sun.COM	dnsservers=""
244*11767SAnurag.Maskey@Sun.COM	dnsdomain=""
245*11767SAnurag.Maskey@Sun.COMfi
2467045Sokie
247*11767SAnurag.Maskey@Sun.COMif [ -n "$dnsservers" ]; then
248*11767SAnurag.Maskey@Sun.COM	#
249*11767SAnurag.Maskey@Sun.COM	# add settings retrieved from dhcp server to /etc/resolv.conf
250*11767SAnurag.Maskey@Sun.COM	#
251*11767SAnurag.Maskey@Sun.COM	update_resolv "$dnsservers" "$dnsdomain"
2527045Sokie
253*11767SAnurag.Maskey@Sun.COM	#
254*11767SAnurag.Maskey@Sun.COM	# Add dns to the nsswitch file, if it isn't already there.
255*11767SAnurag.Maskey@Sun.COM	#
256*11767SAnurag.Maskey@Sun.COM	update_nss hosts
257*11767SAnurag.Maskey@Sun.COM	update_nss ipnodes
2587045Sokie
259*11767SAnurag.Maskey@Sun.COMelif dhcp_edits /etc/nsswitch.conf; then
260*11767SAnurag.Maskey@Sun.COM	# If we added DNS to the hosts and ipnodes
261*11767SAnurag.Maskey@Sun.COM	# policy in the nsswitch, remove it.
262*11767SAnurag.Maskey@Sun.COM	cleanup_nss
2637045Sokiefi
2647045Sokie
2657045Sokieif dhcp_edits /etc/inet/hosts; then
2667045Sokie	# Clean up any old DHCP-added entries
2677045Sokie	# (except loopback) in the hosts file.
2687045Sokie	cleanup_hosts
2697045Sokiefi
2707045Sokie
271