#!/bin/ksh
#
# 2013/05/03 Damien BENOIST
#
# see:
#
# http://notallmicrosoft.blogspot.co.uk/2013/05/setting-up-oracle-solaris-11-with-dhcp.html
#

typeset -i vrbCmd_cnt=0
typeset vrbCmd_bgn="${1:-0}"

function				vrbCmd
{
	typeset ret

	if [[ "$vrbCmd_cnt" -ge "${vrbCmd_bgn}" ]]
	then
		echo "### [$vrbCmd_cnt] $@"
		$@
		ret=$?
		echo "--- [$vrbCmd_cnt] $ret"
		[[ "$ret" == "0" ]] || exit 1
	fi
	vrbCmd_cnt=$vrbCmd_cnt+1
}

function				dhcp_setup
{
	vrbCmd netadm enable -p ncp DefaultFixed
	vrbCmd /usr/sbin/netadm list
	vrbCmd dladm show-phys
	vrbCmd ipadm show-if
	# vrbCmd ipadm delete-ip net0
	vrbCmd ipadm create-ip net0
	vrbCmd ipadm show-if
	vrbCmd ipadm create-addr -T dhcp -h my-host net0/v4
	vrbCmd ipadm show-if
	vrbCmd ipadm show-addr
	vrbCmd netstat -rn 
}

function				ldap_setup
{
	vrbCmd ldapclient manual \
		-a credentialLevel=proxy \
		-a authenticationMethod=simple \
		-a proxyDN=cn=srv_ldapproxy,cn=Users,dc=dcs,dc=bbk,dc=ac,dc=uk \
		-a proxyPassword=ClearTextPassword \
		-a defaultSearchBase=dc=dcs,dc=bbk,dc=ac,dc=uk \
		-a domainName=DCS.BBK.AC.UK \
		-a followReferrals=false \
		-a defaultServerList="193.61.1.1,193.61.2.2" \
		-a attributeMap=group:userpassword=userPassword \
		-a attributeMap=group:memberuid=memberUid \
		-a attributeMap=group:gidnumber=gidNumber \
		-a attributeMap=passwd:gecos=description \
		-a attributeMap=passwd:gidnumber=gidNumber \
		-a attributeMap=passwd:uidnumber=uidNumber \
		-a attributeMap=passwd:homedirectory=unixHomeDirectory \
		-a attributeMap=passwd:loginshell=loginShell \
		-a attributeMap=shadow:shadowflag=shadowFlag \
		-a attributeMap=shadow:userpassword=userPassword \
		-a objectClassMap=group:posixGroup=group \
		-a objectClassMap=passwd:posixAccount=user \
		-a objectClassMap=shadow:shadowAccount=user \
		-a serviceSearchDescriptor=passwd:dc=dcs,dc=bbk,dc=ac,dc=uk?sub \
		-a serviceSearchDescriptor=group:dc=dcs,dc=bbk,dc=ac,dc=uk?sub

}

function				nsswitch_setup
{
	vrbCmd svccfg << END
select name-service/switch
setprop config/password = "files ldap"
setprop config/group = "files ldap"
setprop config/host = "files [SUCCESS=return] dns"
setprop config/network = "files"
setprop config/protocol = "files"
setprop config/rpc = "files"
setprop config/ether = "files"
setprop config/netmask = "files"
setprop config/bootparam = "files"
setprop config/publickey = "files"
setprop config/netgroup= "files"
setprop config/automount = "files ldap"
setprop config/alias = "files"
setprop config/service = "files"
setprop config/project = "files"
setprop config/auth_attr = "files"
setprop config/prof_attr = "files"
setprop config/tnrhtp = "files"
setprop config/tnrhdb = "files"
setprop config/printer = "user files"
select system/name-service/switch:default
refresh
validate
exit
END
	vrbCmd svcadm restart svc:/milestone/name-services:default
	vrbCmd egrep "ldap|dns" /etc/nsswitch.conf
}

function				all_setup
{
	dhcp_setup
	nsswitch_setup
	ldap_setup
	nsswitch_setup
}

all_setup 2>&1 | tee dhcpAndLdapSetup.out
