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