Home | History | Annotate | Download | only in redhat
      1 #!/bin/bash
      2 #
      3 # Init file for OpenSSH server daemon
      4 #
      5 # chkconfig: 2345 55 25
      6 # description: OpenSSH server daemon
      7 #
      8 # processname: sshd
      9 # config: /etc/ssh/ssh_host_key
     10 # config: /etc/ssh/ssh_host_key.pub
     11 # config: /etc/ssh/ssh_random_seed
     12 # config: /etc/ssh/sshd_config
     13 # pidfile: /var/run/sshd.pid
     14 
     15 # source function library
     16 . /etc/rc.d/init.d/functions
     17 
     18 # pull in sysconfig settings
     19 [ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd
     20 
     21 RETVAL=0
     22 prog="sshd"
     23 
     24 # Some functions to make the below more readable
     25 SSHD=/usr/sbin/sshd
     26 PID_FILE=/var/run/sshd.pid
     27 
     28 do_restart_sanity_check()
     29 {
     30 	$SSHD -t
     31 	RETVAL=$?
     32 	if [ $RETVAL -ne 0 ]; then
     33 		failure $"Configuration file or keys are invalid"
     34 		echo
     35 	fi
     36 }
     37 
     38 start()
     39 {
     40 	# Create keys if necessary
     41 	/usr/bin/ssh-keygen -A
     42 	if [ -x /sbin/restorecon ]; then
     43 		/sbin/restorecon /etc/ssh/ssh_host_key.pub
     44 		/sbin/restorecon /etc/ssh/ssh_host_rsa_key.pub
     45 		/sbin/restorecon /etc/ssh/ssh_host_dsa_key.pub
     46 		/sbin/restorecon /etc/ssh/ssh_host_ecdsa_key.pub
     47 	fi
     48 
     49 	echo -n $"Starting $prog:"
     50 	$SSHD $OPTIONS && success || failure
     51 	RETVAL=$?
     52 	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/sshd
     53 	echo
     54 }
     55 
     56 stop()
     57 {
     58 	echo -n $"Stopping $prog:"
     59 	killproc $SSHD -TERM
     60 	RETVAL=$?
     61 	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/sshd
     62 	echo
     63 }
     64 
     65 reload()
     66 {
     67 	echo -n $"Reloading $prog:"
     68 	killproc $SSHD -HUP
     69 	RETVAL=$?
     70 	echo
     71 }
     72 
     73 case "$1" in
     74 	start)
     75 		start
     76 		;;
     77 	stop)
     78 		stop
     79 		;;
     80 	restart)
     81 		stop
     82 		start
     83 		;;
     84 	reload)
     85 		reload
     86 		;;
     87 	condrestart)
     88 		if [ -f /var/lock/subsys/sshd ] ; then
     89 			do_restart_sanity_check
     90 			if [ $RETVAL -eq 0 ] ; then
     91 				stop
     92 				# avoid race
     93 				sleep 3
     94 				start
     95 			fi
     96 		fi
     97 		;;
     98 	status)
     99 		status $SSHD
    100 		RETVAL=$?
    101 		;;
    102 	*)
    103 		echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
    104 		RETVAL=1
    105 esac
    106 exit $RETVAL
    107