blob: efedbfb85dd41af162ca61bf1fd96964e7aa69fc [file] [log] [blame]
Damien Miller06230761999-10-28 14:03:14 +10001#!/bin/bash
2
3# Init file for OpenSSH server daemon
4#
5# chkconfig: 2345 55 25
6# description: OpenSSH server daemon
7#
Damien Millera37010e1999-10-29 09:18:29 +10008# 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
Damien Miller06230761999-10-28 14:03:14 +100014
15# source function library
16. /etc/rc.d/init.d/functions
17
Ben Lindstrom26f33892001-04-27 00:46:17 +000018[ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd
19
Damien Miller06230761999-10-28 14:03:14 +100020RETVAL=0
21
Damien Millerab8d1922000-08-08 16:53:28 +100022# Some functions to make the below more readable
23KEYGEN=/usr/bin/ssh-keygen
Damien Miller0bc1bd82000-11-13 22:57:25 +110024RSA1_KEY=/etc/ssh/ssh_host_key
25RSA_KEY=/etc/ssh/ssh_host_rsa_key
Damien Millerab8d1922000-08-08 16:53:28 +100026DSA_KEY=/etc/ssh/ssh_host_dsa_key
27PID_FILE=/var/run/sshd.pid
Ben Lindstrom0c100872001-02-26 20:38:53 +000028my_success() {
29 local msg
30 if [ $# -gt 1 ]; then
31 msg="$2"
32 else
33 msg="done"
34 fi
35 case "`type -type success`" in
36 function)
37 success "$1"
38 ;;
39 *)
40 echo -n "${msg}"
41 ;;
42 esac
43}
44my_failure() {
45 local msg
46 if [ $# -gt 1 ]; then
47 msg="$2"
48 else
49 msg="FAILED"
50 fi
51 case "`type -type failure`" in
52 function)
53 failure "$1"
54 ;;
55 *)
56 echo -n "${msg}"
57 ;;
58 esac
59}
Damien Miller0bc1bd82000-11-13 22:57:25 +110060do_rsa1_keygen() {
61 if ! test -f $RSA1_KEY ; then
62 echo -n "Generating SSH1 RSA host key: "
63 if $KEYGEN -q -t rsa1 -f $RSA1_KEY -C '' -N '' >&/dev/null; then
Ben Lindstrom0c100872001-02-26 20:38:53 +000064 my_success "RSA1 key generation"
Damien Miller0bc1bd82000-11-13 22:57:25 +110065 echo
66 else
Ben Lindstrom0c100872001-02-26 20:38:53 +000067 my_failure "RSA1 key generation"
Damien Miller0bc1bd82000-11-13 22:57:25 +110068 echo
69 exit 1
70 fi
71 fi
72}
Damien Millerab8d1922000-08-08 16:53:28 +100073do_rsa_keygen() {
Damien Miller0bc1bd82000-11-13 22:57:25 +110074 if ! test -f $RSA_KEY ; then
75 echo -n "Generating SSH2 RSA host key: "
76 if $KEYGEN -q -t rsa -f $RSA_KEY -C '' -N '' >&/dev/null; then
Ben Lindstrom0c100872001-02-26 20:38:53 +000077 my_success "RSA key generation"
Damien Millerab8d1922000-08-08 16:53:28 +100078 echo
79 else
Ben Lindstrom0c100872001-02-26 20:38:53 +000080 my_failure "RSA key generation"
Damien Millerab8d1922000-08-08 16:53:28 +100081 echo
82 exit 1
83 fi
84 fi
85}
86do_dsa_keygen() {
87 if ! test -f $DSA_KEY ; then
Damien Miller0bc1bd82000-11-13 22:57:25 +110088 echo -n "Generating SSH2 DSA host key: "
89 if $KEYGEN -q -t dsa -f $DSA_KEY -C '' -N '' >&/dev/null; then
Ben Lindstrom0c100872001-02-26 20:38:53 +000090 my_success "DSA key generation"
Damien Millerab8d1922000-08-08 16:53:28 +100091 echo
92 else
Ben Lindstrom0c100872001-02-26 20:38:53 +000093 my_failure "DSA key generation"
Damien Millerab8d1922000-08-08 16:53:28 +100094 echo
95 exit 1
96 fi
97 fi
98}
99
Damien Miller06230761999-10-28 14:03:14 +1000100case "$1" in
Damien Millerab8d1922000-08-08 16:53:28 +1000101 start)
102 # Create keys if necessary
Damien Miller0bc1bd82000-11-13 22:57:25 +1100103 do_rsa1_keygen;
Damien Millerab8d1922000-08-08 16:53:28 +1000104 do_rsa_keygen;
105 do_dsa_keygen;
106
107 echo -n "Starting sshd: "
108 if [ ! -f $PID_FILE ] ; then
Ben Lindstrom26f33892001-04-27 00:46:17 +0000109 sshd $OPTIONS
Damien Millerab8d1922000-08-08 16:53:28 +1000110 RETVAL=$?
Damien Miller123cbe82000-09-03 19:14:58 +1100111 if [ "$RETVAL" = "0" ] ; then
Ben Lindstrom0c100872001-02-26 20:38:53 +0000112 my_success "sshd startup" "sshd"
Damien Miller123cbe82000-09-03 19:14:58 +1100113 touch /var/lock/subsys/sshd
114 else
Ben Lindstrom0c100872001-02-26 20:38:53 +0000115 my_failure "sshd startup" ""
Damien Miller123cbe82000-09-03 19:14:58 +1100116 fi
Damien Millerab8d1922000-08-08 16:53:28 +1000117 fi
118 echo
119 ;;
120 stop)
121 echo -n "Shutting down sshd: "
122 if [ -f $PID_FILE ] ; then
123 killproc sshd
Ben Lindstrom89ef41a2000-11-07 16:41:41 +0000124 RETVAL=$?
Damien Millerab8d1922000-08-08 16:53:28 +1000125 [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/sshd
126 fi
127 echo
128 ;;
129 restart)
130 $0 stop
131 $0 start
132 RETVAL=$?
133 ;;
Damien Miller2b2cf522000-10-16 12:25:17 +1100134 condrestart)
135 if [ -f /var/lock/subsys/sshd ] ; then
136 $0 stop
137 $0 start
138 RETVAL=$?
139 fi
140 ;;
Damien Millerab8d1922000-08-08 16:53:28 +1000141 status)
142 status sshd
143 RETVAL=$?
144 ;;
145 *)
Damien Miller2b2cf522000-10-16 12:25:17 +1100146 echo "Usage: sshd {start|stop|restart|status|condrestart}"
Damien Millerab8d1922000-08-08 16:53:28 +1000147 exit 1
148 ;;
Damien Miller06230761999-10-28 14:03:14 +1000149esac
150
151exit $RETVAL