#!/bin/sh
#****************************************************************
# *	Copyright (C) 2003, Wistron Inc.  All Rights Reserved.
# *	File Name:		cimauthconf
# *	Author:			Jerry Zhou
# *	Create Time: 	2003-11-07 10:38:44
# *	Last Update: 	2003-11-07 14:04:41 by Jerry Zhou 
#****************************************************************
pegasus_path=/usr/local/share/pegasus
pegasus_servfile=$pegasus_path/cim_server
pegasus_cimuser=$pegasus_path/bin/cimuser
pegasus_cimauth=$pegasus_path/bin/cimauth
pegasus_conf=$pegasus_path/cimserver_planned.conf

enteruser()
{
	if ps -A | grep cimserver 1>/dev/null 2>/dev/null ; then
		echo ""
	else
		echo "None of pegasus server is running now. Restart pegasus server ..."
		$pegasus_servfile stop
		$pegasus_servfile start
		echo ""
	fi
	if [ $1 = "list" ] ; then
		$pegasus_cimuser -l
		return 0
	fi
	echo "Please enter user name:"
	echo "(ATTENTION: The specified user MUST be a valid user on the local system!)"
	read newauthuser
	if [ -z $newauthuser ] ; then
		echo "Error: Missing required user name! Abort!"
		exit -1
	fi
	$pegasus_cimuser $* $newauthuser
	$pegasus_cimauth $* $newauthuser -n root/cimv2 -R -W 1>/dev/null 2>/dev/null
	$pegasus_cimauth $* $newauthuser -n root/wistron -R -W 1>/dev/null 2>/dev/null
	return 0
}

doauthentication()
{
	cd $pegasus_path/
	cat $pegasus_conf | sed "s/enableAuthentication=$1/enableAuthentication=$2/g" > ./conftmp1
	cat ./conftmp1 | sed "s/enableRemotePrivilegedUserAccess=$1/enableRemotePrivilegedUserAccess=$2/g" > ./conftmp2
	cat ./conftmp2 | sed "s/enableNamespaceAuthorization=$1/enableNamespaceAuthorization=$2/g" > ./conftmp
#	echo "Restarting pegasus server now ..."
#	$pegasus_servfile stop
	rm -f $pegasus_conf ./conftmp?
	mv ./conftmp $pegasus_conf
	echo "NOTICE: You MUST restart pegasus server to validate changes"
#	$pegasus_servfile start
}



case $1 in
	add)
		enteruser -a -u
		;;
	modify)
		enteruser -m -u
		;;
	remove)
		enteruser -r -u
		;;
	list)
		enteruser list
		;;
	enableauth)
		doauthentication false true
		;;
	disableauth)
		doauthentication true false
		;;
	*)
		echo "Usage: cimauthconf add"
		echo "       cimauthconf modify"
		echo "       cimauthconf remove"
		echo "       cimauthconf list"
		echo "       cimauthconf enableauth"
		echo "       cimauthconf disableauth"
		;;
esac
exit 0