#
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

SMBCONF=${WINBIND_CFGDIR}/lib/smb.conf
WINBINDLOCKF=${WINBIND_CFGDIR}/var/locks/winbindd.pid
NMBLOOKUP=${WINBIND_BINDIR}/nmblookup
WINBINDD=${WINBIND_SBINDIR}/winbindd
NSS=/etc/nsswitch.conf
NODEID_LOCAL=`/usr/cluster/bin/scha_cluster_get -O NODEID_LOCAL`
STARTWAIT=`expr ${NODEID_LOCAL} \* 2`

SCLOGGER=/usr/cluster/lib/sc/scds_syslog
PKG=SUNWscsmb.winbind
METHOD=`basename $0`

syslog_tag()
{
        #
        # Data Service message format
        #

        $SET_DEBUG

        print "SC[${PKG:-??}.${METHOD:-??}]:${RESOURCEGROUP:-??}:${RESOURCE:-??}"
}

scds_syslog()
{

        #
        # Log a message
        #

        $SET_DEBUG

        $SCLOGGER "$@" &
}

debug_message()
{
        #
        # Output a debug message to syslog if required
        #

        if [ "$DEBUG" = "$RESOURCE" -o "$DEBUG" = "ALL" ]
        then
                SET_DEBUG="set -x"

                DEBUG_TEXT=$1

                scds_syslog -p daemon.debug -t $(syslog_tag) -m \
                        "%s" "$DEBUG_TEXT"
        else
                SET_DEBUG=
        fi
}

stripfunc() {
         s1=1
         s2=`echo $1 | /bin/wc -c`
         s3=""

        while [ $s1 -lt ${s2} ]
        do
 
                s4=`echo $1 | /bin/cut -c ${s1}`

                if [[ "${s4}" != @([0-9]) ]]; then
                break
                else
                s3=${s3}${s4}
                fi

                s1=$(($s1+1))

        done

        echo $s3
}


validate_options()
{
        #
        # Ensure all options are set
        #

        for i in RESOURCE RESOURCEGROUP WINBIND_BINDIR WINBIND_CFGDIR WINBIND_NTUSER WINBIND_SBINDIR
        do
                case $i in
                        RESOURCE)
                        if [ -z $RESOURCE ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -R not set"
                                exit 1
                        fi;;

                        RESOURCEGROUP)
                        if [ -z $RESOURCEGROUP ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -G not set"
                                exit 1
                        fi;;

                        WINBIND_BINDIR)
                        if [ -z $WINBIND_BINDIR ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -B not set"
                                exit 1
                        fi;;

                        WINBIND_CFGDIR)
                        if [ -z $WINBIND_CFGDIR ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -C not set"
                                exit 1
                        fi;;

                        WINBIND_NTUSER)
                        if [ -z $WINBIND_NTUSER ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -U not set"
                                exit 1
                        fi;;

                        WINBIND_SBINDIR)
                        if [ -z $WINBIND_BINDIR ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -S not set"
                                exit 1
                        fi;;

                esac
        done

        #
        # Set LD_LIBRARY_PATH if WINBIND_LD_PATH is being used.
        #

        if [ ! -z "${WINBIND_LD_PATH}" ]; then
           export LD_LIBRARY_PATH=${WINBIND_LD_PATH}
        fi


}

validate()
{
	#
	# Validate Winbind
	#

        debug_message "Function: validate - Begin"
	$SET_DEBUG

	rc_validate=0

	#
	# Validate the bin directory
	#

	if [ ! -d "${WINBIND_BINDIR}" ]
	then
   		scds_syslog -p daemon.error -t $(syslog_tag) -m \
			"Validate - Winbind bin directory %s does not exist" \
			"${WINBIND_BINDIR}"
		rc_validate=1
	else
		debug_message "Validate - Winbind bin directory ${WINBIND_BINDIR} exists"
	fi

	#
	# Validate the sbin directory
	#

	if [ ! -d "${WINBIND_SBINDIR}" ]
	then
   		scds_syslog -p daemon.error -t $(syslog_tag) -m \
			"Validate - Winbind sbin directory %s does not exist" \
			"${WINBIND_SBINDIR}"
		rc_validate=1
	else
		debug_message "Validate - Winbind sbin directory ${WINBIND_BINDIR} exists"
	fi

	#
	# Validate the configuration directory
	#

	if [ ! -d "${WINBIND_CFGDIR}" ]
	then
   		scds_syslog -p daemon.error -t $(syslog_tag) -m \
			"Validate - Winbind configuration directory %s does not exist" \
			"${WINBIND_CFGDIR}"
		rc_validate=1
	else
		debug_message "Validate - Winbind configuration directory ${WINBIND_CFGDIR} exists"
	fi

	#
	# Validate the smb.conf file exists
	#

	if [ ! -f "${SMBCONF}" ]
	then
   		scds_syslog -p daemon.error -t $(syslog_tag) -m \
			"Validate - smbconf %s does not exist" \
			"${SMBCONF}"
		rc_validate=1
	else
		debug_message "Validate - smbconf ${SMBCONF} exists"
	fi

	#
	# Validate that the winbindd is executable
	#

	if [ ! -x "${WINBINDD}" ]
	then
  		scds_syslog -p daemon.error -t $(syslog_tag) -m \
			"Validate - winbindd %s non-existent executable" \
			"${WINBINDD}"
		rc_validate=1
	else
		debug_message "Validate - winbindd ${WINBINDD} exists and is executable"
	fi

	#
	# Validate that winbind is in /etc/nsswitch.conf (passwd)
	#

	if [ -z "`grep passwd: ${NSS} | grep -v '#' | grep winbind`" ]
	then
   		scds_syslog -p daemon.error -t $(syslog_tag) -m \
			"Validate - winbind is not defined in %s in the passwd section" \
			"${NSS}"
		rc_validate=1
	else
		debug_message "Validate - winbind defined in ${NSS} in the passwd section"
	fi

	#
	# Validate that winbind is in /etc/nsswitch.conf (group)
	#

	if [ -z "`grep group: ${NSS} | grep -v '#' | grep winbind`" ]
	then
   		scds_syslog -p daemon.error -t $(syslog_tag) -m \
			"Validate - winbind is not defined in %s in the group section" \
			"${NSS}"
		rc_validate=1
	else
		debug_message "Validate - winbind defined in ${NSS} in the group section"
	fi

	#
	# Check the samba version
	#

	# Is 2.2.x being used

        ${NMBLOOKUP} -h >/dev/null 2>&1

        if [ $? -eq 0 ]; then
           VERSION=`${NMBLOOKUP} -h | grep 'Version ' | awk '{print $2}'`
        else
           VERSION=`${NMBLOOKUP} -V | awk '{print $2}'`
        fi

	SAMBA_VERSION=`echo ${VERSION} | cut -d'.' -f1`
	SAMBA_RELEASE=`echo ${VERSION} | cut -d'.' -f2`
	SAMBA_UPDATE=` echo ${VERSION} | cut -d'.' -f3`

	debug_message "Validate - Samba version <${SAMBA_VERSION}.${SAMBA_RELEASE}.${SAMBA_UPDATE}> is being used"

        # Strip off any non-digits

        SAMBA_VERSION=`stripfunc ${SAMBA_VERSION}`
        SAMBA_RELEASE=`stripfunc ${SAMBA_RELEASE}`
        SAMBA_UPDATE=`stripfunc ${SAMBA_UPDATE}`

	rc_validate_version=0

        if [ -z "${VERSION}" ]
        then
                scds_syslog -p daemon.error -t $(syslog_tag) -m \
                        "Validate - Couldn't retrieve Samba version number"
		rc_validate=1
        elif [ "${SAMBA_VERSION}" -lt 2 ]; then
             rc_validate_version=1
        elif [ "${SAMBA_VERSION}" -eq 2 -a "${SAMBA_RELEASE}" -le 2 -a "${SAMBA_UPDATE}" -lt 4 ]; then
             rc_validate_version=1
	fi

	if [ "${rc_validate_version}" -gt 0 ]; then

                scds_syslog -p daemon.error -t $(syslog_tag) -m \
                        "This version of samba <%s> is not supported with this dataservice" \
                        `printf ${SAMBA_VERSION}.${SAMBA_RELEASE}.${SAMBA_UPDATE}`
		rc_validate=1
	fi

        debug_message "Function: validate - End "

}

start_winbind()
{
	#
	# Start Winbind
	#

        debug_message "Function: start_winbind - Begin"
	$SET_DEBUG

	#
	# Wait X seconds to prevent file problems when starting 
	# winbindd with an empty database in scalable mode
	#


	sleep ${STARTWAIT}

	if [ -z "${WINBIND_DISCACHE}" ]; then
           WINBIND_DISCACHE=""
        fi

	if [ -z "${WINBIND_SINGLEMODE}" ]; then
           WINBIND_SINGLEMODE=""
        fi

	${WINBINDD} -s ${SMBCONF} ${WINBIND_DISCACHE} ${WINBIND_SINGLEMODE}
	winst=$?

        debug_message "Function: start_winbind - End"
}

stop_winbind()
{
	# 
	# Stop Winbindd
	# 

        debug_message "Function: stop_winbind - Begin"
	$SET_DEBUG

	# 
	# Kill winbindd pid else pkill winbindd
	# 

	if [ -f "${WINBINDLOCKF}" ]
	then
 		pid=`cat ${WINBINDLOCKF}`

		debug_message "stop_winbind - winbindd lockfile found send TERM to ${pid} and remove lockfile"

 		/bin/kill -TERM ${pid}
 		/bin/rm -f ${WINBINDLOCKF}
	else
		debug_message "stop_winbind - winbindd lockfile not found send TERM to winbindd"
	
   		pkill -TERM winbindd
	fi

        debug_message "Function: stop_winbind - End"
}

check_winbind()
{
	# 
	# Probe winbind
	#

	
        debug_message "Function: check_winbind - Begin"
	$SET_DEBUG

	rc_check_winbind=0
	# 
	# Test if winbind is starting
	#
	
	if [ -z "`pgrep winbindd`" ]
	then
		debug_message "check_winbind - winbindd in start phase (Exit 100)"
		rc_check_winbind=1
		return
	fi

	# 
	# Retrieve an NT user with getent
	#

	
	if [ -z "`getent passwd ${WINBIND_NTUSER}`" ]
	then
   		scds_syslog -p daemon.error -t $(syslog_tag) -m \
		"check_winbind - User <%s> can't be retrieved by the nameservice" \
		"${WINBIND_NTUSER}"

		rc_check_winbind=1
		return
	fi

        debug_message "Function: check_winbind - End"
}


