#!/bin/sh
#
# svcgssd      Start up and shut down RPCSEC GSS daemon
#
# chkconfig: - 61 29
# description: Starts user-level daemon that manages RPCSEC GSS contexts \
#	       for the NFS server.
# processname: rpc.svcgssd
# pidfile: /var/run/rpc.svcgssd.pid

### BEGIN INIT INFO
# Provides: svcgssd
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Stop: 0 1 6
# Short-Description: Starts the RPCSEC GSS server daemon
# Description: NFS is a popular protocol for file sharing across \
#          networks. This deamon manages RPCSEC GSS contexts on the
#          server used by secure NFS mounts
### END INIT INFO

WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

# Source networking configuration.
# Check that networking is up.
SourceIfNotEmpty /etc/sysconfig/network

is_yes "$NETWORKING" || exit 0

SVCGSSD=/usr/sbin/rpc.svcgssd
RPCPIPEFS=/var/lib/nfs/rpc_pipefs
KEYTAB=/etc/krb5.keytab
GSSAPI_MECH=/etc/gssapi_mech.conf

PIDFILE=/var/run/rpc.svcgssd.pid
LOCKFILE=/var/lock/subsys/rpc.svcgssd
RETVAL=0

start()
{
    [ -f "$KEYTAB" -a -f "$GSSAPI_MECH" ] || return 1

    [ -d "$RPCPIPEFS/nfs" ] || {
	modprobe sunrpc ||:
	mount -t rpc_pipefs rpc_pipefs "$RPCPIPEFS"
	RETVAL=$?
	[ $RETVAL -eq 0 ] || return $RETVAL
    }

    [ -f /proc/net/rpc/auth.rpcsec.context/channel ] || {
	modprobe des ||:
	modprobe auth_rpcgss ||:
	modprobe rpcsec_gss_krb5 ||:
    }

    start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user root $SVCGSSD
    RETVAL=$?
    return $RETVAL
}

stop()
{
    stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user root $SVCGSSD
    RETVAL=$?
    return $RETVAL
}

restart()
{
    stop
    start
}

# See how we were called.
case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    restart|reload)
	restart
	;;
    condstop)
	if [ -e "$LOCKFILE" ]; then
	    stop
	fi
	;;
    condrestart|condreload)
	if [ -e "$LOCKFILE" ]; then
	    restart
	fi
	;;
    status)
	status --pidfile "$PIDFILE" --expect-user root $SVCGSSD
	RETVAL=$?
	;;
    *)
	msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
	RETVAL=1
esac

exit $RETVAL
