#!/bin/bash
#
# kadmind      Start and stop the Kerberos 5 administrative server.
#
# chkconfig:   - 35 65
# description: Kerberos 5 is a trusted third-party authentication system.  \
#	       This script starts and stops the Kerberos 5 administrative \
#              server, which should only be run on the master server for a \
#              realm.
# processname: kadmind
#

# Get config.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

# Source function library.
. /etc/init.d/functions
prog="Kerberos 5 Admin Server"
krb_daemon=/usr/sbin/kadmind
krb5kdc_path=/var/lib/kerberos/krb5kdc
lockfile=/var/lock/subsys/kadmin
RETVAL=0

# Shell functions to cut down on useless shell instances.
start() {
  	if [ ! -f $krb5kdc_path/principal ] ; then
	    exit 0
	fi
  	if [ -f $krb5kdc_path/kpropd.acl ] ; then
	    exit 0
	else
  	    if [ ! -f $krb5kdc_path/kadm5.keytab ] ; then
		echo -n $"Extracting kadm5 Service Keys: "
		/usr/sbin/kadmin.local -q "ktadd -k $krb5kdc_path/kadm5.keytab kadmin/admin kadmin/changepw" && success || fail
		echo
	    fi
	fi
	echo -n $"Starting $prog: "
	start_daemon --lockfile "$lockfile" ${krb_daemon}
	RETVAL=$?
	echo
}
stop() {
	echo -n $"Stopping $prog: "
	stop_daemon --lockfile "$lockfile" ${krb_daemon}
	RETVAL=$?
	echo
}
reload() {
	echo -n $"Reopening $prog log file: "
	stop_daemon --lockfile "$lockfile" -HUP ${krb_daemon}
	RETVAL=$?
	echo
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	stop
	start
	;;
  status)
	status ${krb_daemon}
	RETVAL=$?
	;;
  reload)
	reload
	;;
  condrestart)
	if [ -f "$lockfile" ] ; then
		stop
		start
	fi
	;;
  *)
	echo $"Usage: $0 {start|stop|status|condrestart|reload|restart}"
	RETVAL=1
	;;
esac

exit $RETVAL
