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
21RETVAL=0
22prog="sshd"
23
24# Some functions to make the below more readable
25SSHD=/usr/sbin/sshd
26PID_FILE=/var/run/sshd.pid
27
28do_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
38start()
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_rsa_key.pub
44		/sbin/restorecon /etc/ssh/ssh_host_dsa_key.pub
45		/sbin/restorecon /etc/ssh/ssh_host_ecdsa_key.pub
46	fi
47
48	echo -n $"Starting $prog:"
49	$SSHD $OPTIONS && success || failure
50	RETVAL=$?
51	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/sshd
52	echo
53}
54
55stop()
56{
57	echo -n $"Stopping $prog:"
58	killproc $SSHD -TERM
59	RETVAL=$?
60	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/sshd
61	echo
62}
63
64reload()
65{
66	echo -n $"Reloading $prog:"
67	killproc $SSHD -HUP
68	RETVAL=$?
69	echo
70}
71
72case "$1" in
73	start)
74		start
75		;;
76	stop)
77		stop
78		;;
79	restart)
80		stop
81		start
82		;;
83	reload)
84		reload
85		;;
86	condrestart)
87		if [ -f /var/lock/subsys/sshd ] ; then
88			do_restart_sanity_check
89			if [ $RETVAL -eq 0 ] ; then
90				stop
91				# avoid race
92				sleep 3
93				start
94			fi
95		fi
96		;;
97	status)
98		status $SSHD
99		RETVAL=$?
100		;;
101	*)
102		echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
103		RETVAL=1
104esac
105exit $RETVAL
106