#!/sbin/sh
#
# @(#)plumbSunScreen.sh	3.8 01/30/03 Sun Microsystems
#
# Plumb the SunScreen module on EFS style interfaces before they are turned on
# by the rootusr script.  This only plumbs the EFS style interfaces which have
# corresponding /etc/hostname.XXX files as those are the ones that would
# normally be plumbed by the rootusr script.  Any other EFS style interfaces
# must be plumbed by some other means.  Any SPF style interfaces are plumbed at
# activate time.
#
#
# Get the list of network interfaces to configure by breaking /etc/hostname.*
# into separate args by using "." as a shell separator character, then stepping
# through pairs of args.  Run "ifconfig plumb" and ss_plumb_interface on each
# interface.  This sets up the streams plumbing for the interface, both the
# "regular" IP stack and the SunScreen module.
#
# The shcat function emulates /usr/bin/cat for systems that have different
# partitions for /usr - when this script is run only / is mounted.
#
# shcat() exists in /etc/rcS , but this script is exec'd 
# rather than run inside /etc/rcS so its defined in this
# script as well.

shcat() {
        while [ $# -ge 1 ]; do
                while read i; do
                        echo "$i"
                done < $1
                shift
        done
}

OIFS=$IFS

SS_DIR=/etc/opt/SUNWicg/SunScreen/.active

get_shared_mac(){

shcat $SS_DIR/HA | while read A1 A2 REMAINDER
        do if [ "$A1" != "" ]
                then if [ "$A1" = "ETHER_ADDR" ]
                        then echo "$A2"
                        break
                fi
        fi
done

}

if [ -f $SS_DIR/Interface.in ]
then
    interface_names="`echo /etc/hostname.*[!:][0-9] 2>/dev/null`"
    if [ "$interface_names" != "/etc/hostname.*[!:][0-9]" ]
    then
	echo "plumbing SunScreen network interfaces:\c"
	IFS="$IFS."
	set -- `echo /etc/hostname\.*[!:][0-9]`
	while test $# -ge 2
	do shift
	   if [ "$1" != "xx0" ]
	   then /sbin/ifconfig $1 plumb
		/sbin/ss_plumb_interface $1
		echo " $1\c"
	   fi
	   shift
	done
	echo "."
    fi
fi

# Restore the field separator

IFS=$OIFS

# if HA is configured change the MAC address of any ROUTING
# interfaces to the shared MAC stored in the HA configuration.

if [ -f $SS_DIR/HA ]	
	then echo "SunScreen HA configured - setting shared MAC address"
	SHAREMAC=`get_shared_mac`
	if [ -f $SS_DIR/Interface.in ]
		then shcat $SS_DIR/Interface.in | while read B1 B2 B3 REMAINDER
			do if [ "$B3" != "" ]
				then if [ "$B3" = "EFS" ]
					then IFS=\"
					echo "$B1" | while read INTERFACE OTHER
						do echo "Setting shared MAC address of ${INTERFACE} to ${SHAREMAC}"
						/sbin/ifconfig ${INTERFACE} ether ${SHAREMAC}
					done
				IFS=$OIFS
				fi
			fi
		done
	fi
fi
