xref: /titanic_51/usr/src/cmd/svc/milestone/net-svc (revision 6ba597c56d749c61b4f783157f63196d7b2445f0)
17c478bd9Sstevel@tonic-gate#!/sbin/sh
27c478bd9Sstevel@tonic-gate#
37c478bd9Sstevel@tonic-gate# CDDL HEADER START
47c478bd9Sstevel@tonic-gate#
57c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the
66927f468Sdp# Common Development and Distribution License (the "License").
76927f468Sdp# You may not use this file except in compliance with the License.
87c478bd9Sstevel@tonic-gate#
97c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate# and limitations under the License.
137c478bd9Sstevel@tonic-gate#
147c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate#
207c478bd9Sstevel@tonic-gate# CDDL HEADER END
217c478bd9Sstevel@tonic-gate#
227c478bd9Sstevel@tonic-gate#
23*6ba597c5SAnurag S. Maskey# Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate# Use is subject to license terms.
257c478bd9Sstevel@tonic-gate#
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate#
287c478bd9Sstevel@tonic-gate# This is third phase of TCP/IP startup/configuration.  This script
2936e852a1SRaja Andra# runs after the NIS startup script.  We run things here that may
3036e852a1SRaja Andra# depend on NIS maps.
317c478bd9Sstevel@tonic-gate#
327c478bd9Sstevel@tonic-gate
336927f468Sdp. /lib/svc/share/smf_include.sh
34*6ba597c5SAnurag S. Maskey. /lib/svc/share/net_include.sh
35*6ba597c5SAnurag S. Maskey
36*6ba597c5SAnurag S. MaskeyNWAM_FMRI="svc:/network/physical:nwam"
376927f468Sdp
387c478bd9Sstevel@tonic-gatecase "$1" in
397c478bd9Sstevel@tonic-gate'start')
407c478bd9Sstevel@tonic-gate	#
41f4b3ec61Sdh155122	# In a shared-IP zone we need this service to be up, but all of the
42f4b3ec61Sdh155122	# work it tries to do is irrelevant (and will actually lead to the
43f4b3ec61Sdh155122	# service failing if we try to do it), so just bail out.
44f4b3ec61Sdh155122	# In the global zone and exclusive-IP zones we proceed.
457c478bd9Sstevel@tonic-gate	#
46*6ba597c5SAnurag S. Maskey	smf_configure_ip || exit $SMF_EXIT_OK
47*6ba597c5SAnurag S. Maskey
48*6ba597c5SAnurag S. Maskey	#
49*6ba597c5SAnurag S. Maskey	# If nwam is enabled, the nwam service will handle the tasks performed
50*6ba597c5SAnurag S. Maskey	# by this service, so just bail out.
51*6ba597c5SAnurag S. Maskey	#
52*6ba597c5SAnurag S. Maskey	service_is_enabled $NWAM_FMRI && exit $SMF_EXIT_OK
53*6ba597c5SAnurag S. Maskey	;; # fall through -- rest of script is the initialization code
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate'stop')
56*6ba597c5SAnurag S. Maskey	exit $SMF_EXIT_OK
577c478bd9Sstevel@tonic-gate	;;
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate*)
607c478bd9Sstevel@tonic-gate	echo "Usage: $0 { start | stop }"
617c478bd9Sstevel@tonic-gate	exit 1
627c478bd9Sstevel@tonic-gate	;;
637c478bd9Sstevel@tonic-gateesac
647c478bd9Sstevel@tonic-gate
65d71dbb73Sjbeckinterface=$2
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate# If boot variables are not set, set variables we use
687c478bd9Sstevel@tonic-gate[ -z "$_INIT_UTS_NODENAME" ] && _INIT_UTS_NODENAME=`/usr/bin/uname -n`
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate#
717c478bd9Sstevel@tonic-gate# This function takes two file names and the file mode as input. The two
727c478bd9Sstevel@tonic-gate# files are compared for differences (using cmp(1)) and if different, the
737c478bd9Sstevel@tonic-gate# second file is over written with the first. A chmod is done with the file
747c478bd9Sstevel@tonic-gate# mode passed in. If the files are equal, the first file passed
757c478bd9Sstevel@tonic-gate# in (the /tmp file) is deleted.
767c478bd9Sstevel@tonic-gate#
777c478bd9Sstevel@tonic-gatemv_file ()
787c478bd9Sstevel@tonic-gate{
797c478bd9Sstevel@tonic-gate	/usr/bin/cmp -s $1 $2
807c478bd9Sstevel@tonic-gate	if [ $? -eq 1 ]; then
817c478bd9Sstevel@tonic-gate		/usr/bin/mv $1 $2
827c478bd9Sstevel@tonic-gate		#
837c478bd9Sstevel@tonic-gate		# The umask during boot is configurable, which requires
847c478bd9Sstevel@tonic-gate		# explicit setting of file permission modes when we
857c478bd9Sstevel@tonic-gate		# create files.
867c478bd9Sstevel@tonic-gate		#
877c478bd9Sstevel@tonic-gate		/usr/bin/chmod $3 $2
887c478bd9Sstevel@tonic-gate	else
897c478bd9Sstevel@tonic-gate		/usr/bin/rm $1
907c478bd9Sstevel@tonic-gate	fi
917c478bd9Sstevel@tonic-gate}
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate#
943cf1b3e0Sokie# This function takes a DHCP parameter (as defined in /etc/dhcp/inittab)
953cf1b3e0Sokie# and returns the value for that parameter returned by the DHCP server.
963cf1b3e0Sokie# If the global 'interface' is defined, it will request the value learned
973cf1b3e0Sokie# on that interface, else it will request the value learned on the primary
983cf1b3e0Sokie# interface.
993cf1b3e0Sokie#
1003cf1b3e0Sokieget_dhcp_var ()
1013cf1b3e0Sokie{
1023cf1b3e0Sokie	if [ -n "$interface" ]; then
1033cf1b3e0Sokie		/sbin/dhcpinfo -i $interface $1
1043cf1b3e0Sokie	else
1053cf1b3e0Sokie		/sbin/dhcpinfo $1
1063cf1b3e0Sokie	fi
1073cf1b3e0Sokie}
1083cf1b3e0Sokie
1093cf1b3e0Sokie#
1103cf1b3e0Sokie# This function returns true if the string "# Added by DHCP$" occurs in
1113cf1b3e0Sokie# the passed-in file, false otherwise.
1123cf1b3e0Sokie#
1133cf1b3e0Sokiedhcp_edits ()
1143cf1b3e0Sokie{
1153cf1b3e0Sokie	/usr/bin/grep '# Added by DHCP$' $1 >/dev/null 2>&1
1163cf1b3e0Sokie	return $?
1173cf1b3e0Sokie}
1183cf1b3e0Sokie
1193cf1b3e0Sokie#
1203cf1b3e0Sokie# update_resolv()
1213cf1b3e0Sokie# Go through /etc/resolv.conf and replace any existing domain or
1223cf1b3e0Sokie# nameserver entries with new ones derived from DHCP.  Note that
1233cf1b3e0Sokie# it is important to preserve order of domain entries vs. search
1243cf1b3e0Sokie# entries; the search entries are reserved for administrator
1253cf1b3e0Sokie# customization and if placed after the domain entry will override
1263cf1b3e0Sokie# it.  See resolv.conf(4).
1273cf1b3e0Sokie#
1283cf1b3e0Sokie# The first arg should be the dns servers string, the second
1293cf1b3e0Sokie# should be the dns domain.
1303cf1b3e0Sokie#
1313cf1b3e0Sokieupdate_resolv ()
1323cf1b3e0Sokie{
1333cf1b3e0Sokie	dnsservers=$1
1343cf1b3e0Sokie	dnsdomain=$2
1353cf1b3e0Sokie
1363cf1b3e0Sokie	if [ ! -f /etc/resolv.conf ]; then
1373cf1b3e0Sokie		/usr/bin/touch /etc/resolv.conf
1383cf1b3e0Sokie	fi
1393cf1b3e0Sokie	export dnsservers dnsdomain
1403cf1b3e0Sokie	/usr/bin/nawk </etc/resolv.conf >/tmp/resolv.conf.$$ '
1413cf1b3e0Sokie		function writedomain() {
1423cf1b3e0Sokie			if (updated == 0) {
1433cf1b3e0Sokie			    	# Use only first domain, not a search list
1443cf1b3e0Sokie			    	split(ENVIRON["dnsdomain"], d)
1453cf1b3e0Sokie				if(length(d[1]) != 0)
1463cf1b3e0Sokie					printf("domain %s\n", d[1])
1473cf1b3e0Sokie			}
1483cf1b3e0Sokie			++updated
1493cf1b3e0Sokie		}
1503cf1b3e0Sokie		$1 == "domain" { writedomain(); next }
1513cf1b3e0Sokie		$1 != "nameserver" { print $0 }
1523cf1b3e0Sokie		END {
1533cf1b3e0Sokie			writedomain()
1543cf1b3e0Sokie			n = split(ENVIRON["dnsservers"], s)
1553cf1b3e0Sokie			for (i = 1; i <= n; ++i)
1563cf1b3e0Sokie				printf("nameserver %s\n", s[i])
1573cf1b3e0Sokie		}'
1583cf1b3e0Sokie	unset dnsservers dnsdomain
1593cf1b3e0Sokie	mv_file /tmp/resolv.conf.$$ /etc/resolv.conf 644
1603cf1b3e0Sokie}
1613cf1b3e0Sokie
1623cf1b3e0Sokie#
163*6ba597c5SAnurag S. Maskey# update_nss()
1647c478bd9Sstevel@tonic-gate# This routine takes as a parameter, the name of the respective policy
1657c478bd9Sstevel@tonic-gate# to change in the nsswitch.conf (hosts or ipnodes) to update with dns.
1667c478bd9Sstevel@tonic-gate#
1677c478bd9Sstevel@tonic-gateupdate_nss ()
1687c478bd9Sstevel@tonic-gate{
1697c478bd9Sstevel@tonic-gate	policy=$1;
1707c478bd9Sstevel@tonic-gate	# Add dns to the nsswitch file, if it isn't already there.
1717c478bd9Sstevel@tonic-gate	/usr/bin/awk ' $1 ~ /^'${policy}':/ {
1727c478bd9Sstevel@tonic-gate		n = split($0, a);
1737c478bd9Sstevel@tonic-gate		newl = a[1];
1747c478bd9Sstevel@tonic-gate		if ($0 !~ /dns/) {
1757c478bd9Sstevel@tonic-gate			printf("#%s # Commented out by DHCP\n", $0);
1767c478bd9Sstevel@tonic-gate			updated = 0;
1777c478bd9Sstevel@tonic-gate			for (i = 2; i <= n; i++) {
1787c478bd9Sstevel@tonic-gate				if (updated == 0 && index(a[i], "[") == 1) {
1797c478bd9Sstevel@tonic-gate					newl = newl" dns";
1807c478bd9Sstevel@tonic-gate					updated++;
1817c478bd9Sstevel@tonic-gate				}
1827c478bd9Sstevel@tonic-gate				newl = newl" "a[i];
1837c478bd9Sstevel@tonic-gate			}
1847c478bd9Sstevel@tonic-gate			if (updated == 0) {
1857c478bd9Sstevel@tonic-gate				newl = newl" dns";
1867c478bd9Sstevel@tonic-gate				updated++;
1877c478bd9Sstevel@tonic-gate			}
1887c478bd9Sstevel@tonic-gate			if (updated != 0)
1897c478bd9Sstevel@tonic-gate				newl = newl" # Added by DHCP";
1907c478bd9Sstevel@tonic-gate			else
1917c478bd9Sstevel@tonic-gate				newl = $0;
1927c478bd9Sstevel@tonic-gate			printf("%s\n", newl);
1937c478bd9Sstevel@tonic-gate		} else
1947c478bd9Sstevel@tonic-gate			printf("%s\n", $0);
1957c478bd9Sstevel@tonic-gate	} $1 !~ /^'${policy}':/ { printf("%s\n", $0); }' /etc/nsswitch.conf \
1967c478bd9Sstevel@tonic-gate	    >/tmp/nsswitch.conf.$$
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate	mv_file /tmp/nsswitch.conf.$$ /etc/nsswitch.conf 644
1997c478bd9Sstevel@tonic-gate}
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate#
2023cf1b3e0Sokie# Remove any lines with the "# Added by DHCP" tag from /etc/nsswitch.conf;
2033cf1b3e0Sokie# also uncomment hosts and ipnodes entries which were previously commented
2043cf1b3e0Sokie# out by this script.
2057c478bd9Sstevel@tonic-gate#
2063cf1b3e0Sokiecleanup_nss ()
2073cf1b3e0Sokie{
2087c478bd9Sstevel@tonic-gate	/usr/bin/sed \
2097c478bd9Sstevel@tonic-gate	    -e '/# Added by DHCP$/d' \
2107c478bd9Sstevel@tonic-gate	    -e 's/^\(#hosts:\)\(.*[^#]\)\(#.*\)$/hosts: \2/' \
2117c478bd9Sstevel@tonic-gate	    -e 's/^\(#ipnodes:\)\(.*[^#]\)\(#.*\)$/ipnodes: \2/' \
2127c478bd9Sstevel@tonic-gate	    /etc/nsswitch.conf >/tmp/nsswitch.conf.$$
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate	mv_file /tmp/nsswitch.conf.$$ /etc/nsswitch.conf 644
2153cf1b3e0Sokie}
2167c478bd9Sstevel@tonic-gate
2173cf1b3e0Sokie#
2183cf1b3e0Sokie# Remove any lines with the "# Added by DHCP" tag from /etc/inet/hosts.
2193cf1b3e0Sokie#
2203cf1b3e0Sokiecleanup_hosts ()
2213cf1b3e0Sokie{
222d71dbb73Sjbeck	/usr/bin/nawk '{
223d71dbb73Sjbeck		if (index($0, "# Added by DHCP") == 0 ||
224d71dbb73Sjbeck		    $1 == "127.0.0.1" || $1 == "::1") {
225d71dbb73Sjbeck			print $0
226d71dbb73Sjbeck		}
227d71dbb73Sjbeck	}' /etc/inet/hosts > /tmp/hosts.$$
2287c478bd9Sstevel@tonic-gate	mv_file /tmp/hosts.$$ /etc/inet/hosts 444
2293cf1b3e0Sokie}
2303cf1b3e0Sokie
2313cf1b3e0Sokie#
2323cf1b3e0Sokie# If our network configuration strategy is DHCP, check for DNS
2333cf1b3e0Sokie# configuration parameters obtained from the DHCP server.
2343cf1b3e0Sokie#
235*6ba597c5SAnurag S. Maskey# Script execution starts here.
2363cf1b3e0Sokie#
2373cf1b3e0Sokiesmf_netstrategy
2383cf1b3e0Sokie
2393cf1b3e0Sokieif [ "$_INIT_NET_STRATEGY" = "dhcp" ]; then
2403cf1b3e0Sokie	dnsservers=`get_dhcp_var DNSserv`
2413cf1b3e0Sokie	dnsdomain=`get_dhcp_var DNSdmain`
2423cf1b3e0Sokieelse
2433cf1b3e0Sokie	dnsservers=""
2443cf1b3e0Sokie	dnsdomain=""
2453cf1b3e0Sokiefi
2463cf1b3e0Sokie
2473cf1b3e0Sokieif [ -n "$dnsservers" ]; then
2483cf1b3e0Sokie	#
2493cf1b3e0Sokie	# add settings retrieved from dhcp server to /etc/resolv.conf
2503cf1b3e0Sokie	#
2513cf1b3e0Sokie	update_resolv "$dnsservers" "$dnsdomain"
2523cf1b3e0Sokie
2533cf1b3e0Sokie	#
2543cf1b3e0Sokie	# Add dns to the nsswitch file, if it isn't already there.
2553cf1b3e0Sokie	#
2563cf1b3e0Sokie	update_nss hosts
2573cf1b3e0Sokie	update_nss ipnodes
2583cf1b3e0Sokie
2593cf1b3e0Sokieelif dhcp_edits /etc/nsswitch.conf; then
2603cf1b3e0Sokie	# If we added DNS to the hosts and ipnodes
2613cf1b3e0Sokie	# policy in the nsswitch, remove it.
2623cf1b3e0Sokie	cleanup_nss
2633cf1b3e0Sokiefi
2643cf1b3e0Sokie
2653cf1b3e0Sokieif dhcp_edits /etc/inet/hosts; then
2663cf1b3e0Sokie	# Clean up any old DHCP-added entries
2673cf1b3e0Sokie	# (except loopback) in the hosts file.
2683cf1b3e0Sokie	cleanup_hosts
2693cf1b3e0Sokiefi
2703cf1b3e0Sokie
271