#!/bin/ksh
#
# 
# Copyright 06/15/98 Sun Microsystems, Inc. All Rights Reserved
#
# automatically updates the nis.mapping file with all the maps present on the
# system
#
trapfunc()
{
  rm -f /tmp/sed.$$ /var/yp/Makefile.$$ /etc/opt/SUNWconn/ldap/current/dsserv.conf.$$
	mv /var/yp/Makefile.pre-SUNWsds /var/yp/Makefile >/dev/null 2>&1 
	echo "Installation aborted"
	exit 1
}
trap trapfunc INT QUIT
create_mapping()
{
  MAP_NAME=$1
  cat <<% >>$MAP_NAME.tmpmap


  #
  # $MAP_NAME map 
  #

  Table: $MAP_NAME

    Common:
      MAP_NAME=$MAP_NAME
      CASE_SENSITIVE=yes

    Dynamic:
      LINE =>\$nisKeyT \$nisValueT
      nisKeyT=\$NIS_KEY
      MATCH_FILTER=(&(objectClass=nisSunObject)(sunNisKey=\$nisKeyT))
      ALL_FILTER=(&(objectClass=nisSunObject)(sunNisKey=*))
      DC_NAMING=split(\$DOMAIN_NAME, ".", "dc=", ",", left2right)
      rootTreeT=ou=Services,\$NAMING_CONTEXT||ou=Services,\$DC_NAMING
      BASE_DN=ou=\$MAP_NAME,\$rootTreeT

    Export:
      Build:
	NIS_KEY=\$sunNisKey
	NIS_VALUE=\$nisMapEntry
	LINE=\$sunNisKey \$nisMapEntry

    Import:
      Extract:
	LINE      =>\$nisKeyT \$nisValueT
      Condense:
        trimNisValueT=trim(\$nisValueT)
        CaseNisKeyT =dprint(\$nisKeyT)
      Build: 
	dn=cn="\$CaseNisKeyT",\$BASE_DN
	sunNisKey=\$nisKeyT
	nisMapEntry=\$trimNisValueT
	nisMapName=\$MAP_NAME
	objectClass=nisSunObject
%
}

change_nis_mapping()
{
MAPPING_FILE=/etc/opt/SUNWconn/ldap/current/mapping/nis.mapping
TMP_MAP=$MAPPING_FILE.tmp
sed "s/DOMAIN_NAME=.*/DOMAIN_NAME=$NIS_DOMAIN/;s/ADMIN_SUFFIX=.*/ADMIN_SUFFIX=ou=admin,ou=Services,$NAMING_CONTEXT/;/BEGIN AUTOMATICALLY GENERATED MAPPING/,/END AUTOMATICALLY GENERATED MAPPING/d" <$MAPPING_FILE >$TMP_MAP
KNOWN_MAPS=`grep MAP_NAME= $TMP_MAP | grep -v \# | sed "s/.*=//;s/\$//;s/\.by.*//"`
KNOWN_MAPS=`echo $KNOWN_MAPS | sed "s/ /|/g"`
MAP_LIST=`for i in $* ; do echo $i; done | egrep -v "$KNOWN_MAPS"`
if [ "$MAP_LIST" != "" ]
then
	for i in $MAP_LIST
	do
		echo "Creating mapping for $i"
		create_mapping $i
	done
	cat <<% >>$TMP_MAP
# BEGIN AUTOMATICALLY GENERATED MAPPING
# This part of the file is replaced automatically
# please place your maps before this line or after the end line
%
	cat *.tmpmap >>$TMP_MAP
	rm *.tmpmap
fi
	cat <<% >>$TMP_MAP
# This part of the file is replaced automatically
# please place your maps after the end line
# END AUTOMATICALLY GENERATED MAPPING
%
mv $TMP_MAP $MAPPING_FILE
echo "Mapping file update completed"
}

ask()
{
	OK=n
	while [ "$OK" = n ]
	do
		echo >/dev/tty
		echo "$1 [$2]: \c" >/dev/tty
		read ans
		if [ "$ans" = "" ]
		then
			ans=$2
		else
			if [ "$2" = "y" -o "$2" = "n" ]
			then
				case $ans in
				"Y"|"y"|"YES"|"yes"|"Yes") ans=y;;
				"N"|"n"|"NO"|"no"|"No") ans=n;;
				esac
			fi
		fi
		if [ "$ans" = y -o "$ans" = n ]
		then
			OK=y
		else
			echo $ans >/dev/tty
			echo "is this correct (y/n) [y] \c" >/dev/tty
			read OK
			case $OK in
			""|"Y"|"y"|"YES"|"yes"|"Yes") OK=y;;
			"N"|"n"|"NO"|"no"|"No") OK=n;;
			esac
		fi
	done
	echo $ans
}

echo "This script initializes the SunDS NIS daemon"
echo "It automatically imports the existing NIS data"
if [ $(ask "Do you want to continue" y) = "n" ]
then
	exit 1
fi

CURDIR=`pwd`
BASEDIR=$(pkgparam SUNWsds BASEDIR)
PATH=$BASEDIR/SUNWconn/ldap/sbin:$BASEDIR/SUNWconn/ldap/bin:$BASEDIR/SUNWconn/ldap/lib:/usr/ccs/bin:/bin:$PATH
export PATH
if [ ! -f /etc/defaultdomain ]
then
	/usr/sbin/sysidnis
fi
NIS_DOMAIN=`domainname`
if [ ! -f /var/yp/Makefile ]
then
	#
	# no server was installed before
	# take the default one
	#
	cp $BASEDIR/SUNWconn/ldap/default/yp/Makefile.tmp /var/yp/Makefile
else
	if [ ! -f /var/yp/Makefile.pre-SUNWsds ]
	then
		cp /var/yp/Makefile /var/yp/Makefile.pre-SUNWsds
	fi
fi
#
# get the domain they want to work with
#
echo 
NIS_DOMAIN=`ask "Please enter the Nis domain managed by this server\n" $NIS_DOMAIN`
#
# Check that this domain is ok for the dsserv config if we want to use
# dc naming
#
NAMING_CONTEXT=`grep "suffix.*dc=" /etc/opt/SUNWconn/ldap/current/dsserv.conf | sed "s/.*\"dc=/dc=/;s/\".*//;s/ //g"`
#
#
#
MAPPING_FILE=/etc/opt/SUNWconn/ldap/current/mapping/nis.mapping
NAMING_CONTEXT=`egrep "^[ 	]*NAMING_CONTEXT" $MAPPING_FILE | sed "s/[ 	]*NAMING_CONTEXT=//"`
if [ "$NAMING_CONTEXT" = "" ]
then
	NAMING_CONTEXT=`echo $NIS_DOMAIN |  sed "s/^/dc=/;s/\./,dc=/g"`
fi
#
# test if NAMING_CONTEXT is a child of any defined datastore (we have suppressed the 
# spaces for a pseudo normalization, aliases normalization is not done)
#
ok=0
for i in `egrep "suffix.*=" /etc/opt/SUNWconn/ldap/current/dsserv.conf | sed "s/.*\"\\(.*\\)\"/\\1/;s/ //g"`
do
  if `echo $NAMING_CONTEXT | grep -i $i >/dev/null`
  then
    ok=1
    break
  fi
done
if [ $ok -eq 0 ]
then
  echo
  echo "Warning, the SunDS configuration doesn't contain a"
  echo "naming context for domain $NIS_DOMAIN \"$NAMING_CONTEXT\""
  echo "Please modify the naming context before so that"
  echo "it is at least a parent of $NAMING_CONTEXT"
  echo "you can perform this task with the Java administration tool"
  echo
  echo "The current naming contexts are"
  egrep "suffix.*=" /etc/opt/SUNWconn/ldap/current/dsserv.conf | sed "s/.*\"\\(.*\\)\"/\\1/;s/ //g"
  echo
	echo "If you want to set your own naming context for NIS mapping"
	echo "edit the $MAPPING_FILE file"
	echo "and uncomment the NAMING_CONTEXT variable"
	echo
  trapfunc
fi
#
# ask if we are master or slave
#
HOSTNAME=`uname -n`
MASTER_NAME=""
MASTER=`ask "Is this host the master of the $NIS_DOMAIN domain (y/n)" y`
if [ "$MASTER" = "y" ]
then
	MASTER_NAME=$HOSTNAME
fi
#
# edit the Makefile to call our scripts/binaries
#
cat <<% >/tmp/sed.$$
/\$(MULTI)/s/^/#/
/^YPDBDIR=/s!.*!YPDBDIR=$BASEDIR/SUNWconn/ldap/yp!
/^YPPUSH=/s!.*!YPPUSH=$BASEDIR/SUNWconn/ldap/sbin/dsyppush!
/^MAKEDBM=/s!.*!MAKEDBM=$BASEDIR/SUNWconn/ldap/lib/dsmakedbm!
/^	@cp \$(ALIASES)/{
  i\\
	\@mkdir -p \$(YPDBDIR)\/\$(DOM)
  p
  s!cp \$(ALIASES)!sed "s/:/ /" \$(ALIASES) > !
  s!mail.aliases!mail.aliases.toimp!
}
/^	@rm \$(YPDBDIR)\/\$(DOM)\/mail.aliases/{
  s!mail.aliases!mail.aliases\.\*!
  p
  s!mail.aliases!mail.byaddr!
}
/^	\$(MKALIAS)/{
  p
  i\\
	\$(SBINDIR)/makedbm -u \$(YPDBDIR)/\$(DOM)/mail.byaddr | grep -v YP_ > \$(YPDBDIR)/\$(DOM)/mail.byaddr.toimp;
  s!\$(MKALIAS)!\$(MAKEDBM)!
  s!mail.byaddr!mail.aliases!
  s!mail.aliases!mail.aliases.toimp!
  p
  s!mail.aliases!mail.byaddr!g
}
%
cd /var/yp
sed -f /tmp/sed.$$ <Makefile >Makefile.tmp
mv Makefile.tmp Makefile
rm /tmp/sed.$$
#
# First delete the .time files 
rm -f *.time
#
# we need to build a list of maps to manage
#
ALL_MAPS=
echo "Getting the list of maps"
if [ -d /var/yp/binding/$NIS_DOMAIN ]
then
	#
	# ask the server for all the maps
	# only for slaves.
	#
	if [ "$MASTER" != "y" ]
	then
		ALL_MAPS=`ypwhich -d $NIS_DOMAIN -m | sed "s/ .*//" | sort`
	fi
fi
if [ "$ALL_MAPS" = "" ]
then
  #
	# get the maps from /var/yp/Makefile
	#
	if [ "$MASTER" != "y" ]
	then
		echo
		echo "Can't get list of maps from NIS, getting them from NIS Makefile instead..."
	fi
	ALL_MAPS=`make -n 2>/dev/null | egrep "makedbm|mkalias|sendmail" | sed "s/.*\///;s/;.*$//" | egrep -v "dsmakedbm|mail.*  *mail" | sort -u`
fi
#
# update the mapping file with the maps
# the maps that we don't know will have the default mapping
#
change_nis_mapping $ALL_MAPS
while test "$MASTER_NAME" = ""
do
	if [ -d /var/yp/$NIS_DOMAIN -o -d /var/yp/binding/$NIS_DOMAIN ]
	then
		MASTER_NAME=`ypwhich -d $NIS_DOMAIN -m ypservers`
	else
		MASTER_NAME=`ask "Please enter the name of the master NIS server" $HOSTNAME`
	fi
done
if [ "$MASTER_NAME" = $HOSTNAME ]
then
	#
	# we are the master, ask for the location of the source files
	#
	SRC_FILE=`grep ^DIR Makefile | sed "s/.*= *//"`
	NSRC_FILE=`ask "Please enter the location of the maps source files" $SRC_FILE`
	if [ "$NSRC_FILE" != "$SRC_FILE" ]
	then
		LA_SRCFILE=`echo $NSRC_FILE | sed 's/\//\\\\\//g'`
		sed "s!^DIR *=.*!DIR=$LA_SRCFILE!" <Makefile >/var/yp/Makefile.$$
		mv /var/yp/Makefile.$$ /var/yp/Makefile
	fi
	PASS_FILE=`ask "Please enter the location of the passwd source file" $NSRC_FILE`
	if [ "$PASS_FILE" != "$SRC_FILE" ]
        then
                LA_SRCFILE=`echo $PASS_FILE | sed 's/\//\\\\\//g'`
                sed "s!^PWDIR *=.*!PWDIR=$LA_SRCFILE!" <Makefile >/var/yp/Makefile.$$
                mv /var/yp/Makefile.$$ /var/yp/Makefile
        fi
	DNSINTER=`ask "Do you want to use DNS for lookups of hosts not found in the maps (y/n)" n`
	if [ "$DNSINTER" = "y" ]
	then
	  DNSINTER="-b"
	else 
	  unset DNSINTER
	fi
fi
#
# Edit the dsserv configuration to enable the NIS frontend
#
sed "s/frontend nis off/frontend nis on/" </etc/opt/SUNWconn/ldap/current/dsserv.conf >/etc/opt/SUNWconn/ldap/current/dsserv.conf.$$
mv /etc/opt/SUNWconn/ldap/current/dsserv.conf.$$ /etc/opt/SUNWconn/ldap/current/dsserv.conf
if [ -f /usr/lib/netsvc/yp/ypstop ]
then
	#
	# Stop the regular Yp server
	#
	echo "Stopping NIS services"
	/usr/lib/netsvc/yp/ypstop
fi
#
# restart the dsserv server
#
echo "Restarting SunDS server"
/etc/init.d/dsserv stop
/etc/init.d/dsserv start
#
# Make sure the SunDS server is up and running
#
sleep 5
dsservpid=`ps -fe | grep lib/dsservd | grep -v grep|awk '{print $2}'`
if [ -n "$dsservpid" ]
then
	echo "Enabling maps"
else
	echo "dsypinstall failed (can't start the SunDS server)."
	echo "Please restart the legacy NIS server if needed."
	trapfunc
fi
#
# and call the dsypinit on all the maps
#
if [ "$MASTER_NAME" = $HOSTNAME ]
then
	#
	# we are master
	#
	dsypinit -m $DNSINTER -d $NIS_DOMAIN 
else
	#
	# we are a slave
	#
	YPDIR=/usr/lib/netsvc/yp
	YPSRV=/var/yp/binding/$NIS_DOMAIN
	if [ -x $YPDIR/ypbind ]; then
		if [ -d $YPSRV -a -f $YPSRV/ypservers ]; then
			$YPDIR/ypbind > /dev/null 2>&1
		elif [ -d $YPSRV ]; then
			$YPDIR/ypbind -broadcast > /dev/null 2>&1
		fi
		# do a ypwhich to force ypbind to get bound
		ypwhich > /dev/null 2>&1
	fi
	dsypinit -s $MASTER_NAME 
fi
if [ $? -ne 0 ]
then
	echo "dsypinit failed, restoring previous configuration"
  trapfunc
fi
if [ -f /usr/lib/netsvc/yp/ypstart ]
then
	#
	# Starting the regular Yp bind
	#
	echo "Starting NIS services"
	/usr/lib/netsvc/yp/ypstart rpcstart
	echo " Done"
fi
#
# That's all folks
#
exit 0
