96c7b3e30b2e696fea599ba011de3351836789a5
[grml-etc.git] / etc / init.d / ssh
1 #!/bin/sh
2
3 ### BEGIN INIT INFO
4 # Provides:             sshd
5 # Required-Start:       $network $local_fs $remote_fs
6 # Required-Stop:
7 # Default-Start:        2 3 4 5
8 # Default-Stop:         0 1 6
9 # Short-Description:    OpenBSD Secure Shell server
10 ### END INIT INFO
11
12 # Notice: this file has been adjusted by the grml team so
13 # the script supports key-generation for ssh as well
14
15 set -e
16
17 # /etc/init.d/ssh: start and stop the OpenBSD "secure shell(tm)" daemon
18
19 test -x /usr/sbin/sshd || exit 0
20 ( /usr/sbin/sshd -\? 2>&1 | grep -q OpenSSH ) 2>/dev/null || exit 0
21
22 if test -f /etc/default/ssh; then
23     . /etc/default/ssh
24 fi
25
26 . /lib/lsb/init-functions
27
28 # Configurable options:
29 KEYGEN=/usr/bin/ssh-keygen
30 RSA1_KEY=/etc/ssh/ssh_host_key
31 RSA_KEY=/etc/ssh/ssh_host_rsa_key
32 DSA_KEY=/etc/ssh/ssh_host_dsa_key
33
34 # Are we running from init?
35 run_by_init() {
36     ([ "$previous" ] && [ "$runlevel" ]) || [ "$runlevel" = S ]
37 }
38
39 check_for_no_start() {
40     # forget it if we're trying to start, and /etc/ssh/sshd_not_to_be_run exists
41     if [ -e /etc/ssh/sshd_not_to_be_run ]; then
42         if [ "$1" = log_end_msg ]; then
43             log_end_msg 0
44         fi
45         if ! run_by_init; then
46             log_action_msg "OpenBSD Secure Shell server not in use (/etc/ssh/sshd_not_to_be_run)"
47         fi
48         exit 0
49     fi
50 }
51
52 check_dev_null() {
53     if [ ! -c /dev/null ]; then
54         if [ "$1" = log_end_msg ]; then
55             log_end_msg 1 || true
56         fi
57         if ! run_by_init; then
58             log_action_msg "/dev/null is not a character device!"
59         fi
60         exit 1
61     fi
62 }
63
64 check_privsep_dir() {
65     # Create the PrivSep empty dir if necessary
66     if [ ! -d /var/run/sshd ]; then
67         mkdir /var/run/sshd
68         chmod 0755 /var/run/sshd
69     fi
70 }
71
72 check_config() {
73     if [ ! -e /etc/ssh/sshd_not_to_be_run ]; then
74         /usr/sbin/sshd -t || exit 1
75     fi
76 }
77
78 export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
79
80 case "$1" in
81   start)
82         check_for_no_start
83         check_dev_null
84         if ! test -f $RSA1_KEY ; then
85            echo "Generating SSH1 RSA host key..."
86            $KEYGEN -t rsa1 -f $RSA1_KEY -C '' -N '' || exit 1
87         fi
88         if ! test -f $RSA_KEY ; then
89            echo "Generating SSH RSA host key..."
90            $KEYGEN -t rsa -f $RSA_KEY -C '' -N '' || exit 1
91         fi
92         if ! test -f $DSA_KEY ; then
93            echo "Generating SSH2 DSA host key..."
94            $KEYGEN -t dsa -f $DSA_KEY -C '' -N '' || exit 1
95         fi
96         log_daemon_msg "Starting OpenBSD Secure Shell server" "sshd"
97         check_privsep_dir
98         if start-stop-daemon --start --quiet --oknodo --pidfile /var/run/sshd.pid --exec /usr/sbin/sshd -- $SSHD_OPTS; then
99             log_end_msg 0
100         else
101             log_end_msg 1
102         fi
103         ;;
104   stop)
105         log_daemon_msg "Stopping OpenBSD Secure Shell server" "sshd"
106         if start-stop-daemon --stop --quiet --oknodo --pidfile /var/run/sshd.pid; then
107             log_end_msg 0
108         else
109             log_end_msg 1
110         fi
111         ;;
112
113   reload|force-reload)
114         check_for_no_start
115         check_config
116         log_daemon_msg "Reloading OpenBSD Secure Shell server's configuration" "sshd"
117         if start-stop-daemon --stop --signal 1 --quiet --oknodo --pidfile /var/run/sshd.pid --exec /usr/sbin/sshd; then
118             log_end_msg 0
119         else
120             log_end_msg 1
121         fi
122         ;;
123
124   restart)
125         check_privsep_dir
126         check_config
127         log_daemon_msg "Restarting OpenBSD Secure Shell server" "sshd"
128         start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile /var/run/sshd.pid
129         check_for_no_start log_end_msg
130         check_dev_null log_end_msg
131         if start-stop-daemon --start --quiet --oknodo --pidfile /var/run/sshd.pid --exec /usr/sbin/sshd -- $SSHD_OPTS; then
132             log_end_msg 0
133         else
134             log_end_msg 1
135         fi
136         ;;
137
138   try-restart)
139         check_privsep_dir
140         check_config
141         log_daemon_msg "Restarting OpenBSD Secure Shell server" "sshd"
142         set +e
143         start-stop-daemon --stop --quiet --retry 30 --pidfile /var/run/sshd.pid
144         RET="$?"
145         set -e
146         case $RET in
147             0)
148                 # old daemon stopped
149                 check_for_no_start log_end_msg
150                 check_dev_null log_end_msg
151                 if start-stop-daemon --start --quiet --oknodo --pidfile /var/run/sshd.pid --exec /usr/sbin/sshd -- $SSHD_OPTS; then
152                     log_end_msg 0
153                 else
154                     log_end_msg 1
155                 fi
156                 ;;
157             1)
158                 # daemon not running
159                 log_progress_msg "(not running)"
160                 log_end_msg 0
161                 ;;
162             *)
163                 # failed to stop
164                 log_progress_msg "(failed to stop)"
165                 log_end_msg 1
166                 ;;
167         esac
168         ;;
169
170   *)
171         log_action_msg "Usage: /etc/init.d/ssh {start|stop|reload|force-reload|restart|try-restart}"
172         exit 1
173 esac
174
175 exit 0
176
177 ## END OF FILE #################################################################