Support SSHD_OPTS option
[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:         1
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
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 export SSHD_OOM_ADJUST=-17
23 if test -f /etc/default/ssh; then
24     . /etc/default/ssh
25 fi
26
27 # Are we in a virtual environment that doesn't support modifying
28 # /proc/self/oom_adj?
29 if grep -q 'envID:.*[1-9]' /proc/self/status; then
30     unset SSHD_OOM_ADJUST
31 fi
32
33 . /lib/lsb/init-functions
34
35 if [ -n "$2" ]; then
36     SSHD_OPTS="$SSHD_OPTS $2"
37 fi
38
39 # Configurable options:
40 KEYGEN=/usr/bin/ssh-keygen
41 RSA1_KEY=/etc/ssh/ssh_host_key
42 RSA_KEY=/etc/ssh/ssh_host_rsa_key
43 DSA_KEY=/etc/ssh/ssh_host_dsa_key
44
45 # Are we running from init?
46 run_by_init() {
47     ([ "$previous" ] && [ "$runlevel" ]) || [ "$runlevel" = S ]
48 }
49
50 check_for_no_start() {
51     # forget it if we're trying to start, and /etc/ssh/sshd_not_to_be_run exists
52     if [ -e /etc/ssh/sshd_not_to_be_run ]; then 
53         if [ "$1" = log_end_msg ]; then
54             log_end_msg 0
55         fi
56         if ! run_by_init; then
57             log_action_msg "OpenBSD Secure Shell server not in use (/etc/ssh/sshd_not_to_be_run)"
58         fi
59         exit 0
60     fi
61 }
62
63 check_dev_null() {
64     if [ ! -c /dev/null ]; then
65         if [ "$1" = log_end_msg ]; then
66             log_end_msg 1 || true
67         fi
68         if ! run_by_init; then
69             log_action_msg "/dev/null is not a character device!"
70         fi
71         exit 1
72     fi
73 }
74
75 check_privsep_dir() {
76     # Create the PrivSep empty dir if necessary
77     if [ ! -d /var/run/sshd ]; then
78         mkdir /var/run/sshd
79         chmod 0755 /var/run/sshd
80     fi
81 }
82
83 check_config() {
84     if [ ! -e /etc/ssh/sshd_not_to_be_run ]; then
85         /usr/sbin/sshd $SSHD_OPTS -t || exit 1
86     fi
87 }
88
89 export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
90
91 case "$1" in
92   start)
93         check_privsep_dir
94         check_for_no_start
95         check_dev_null
96
97         if ! test -f $RSA1_KEY ; then
98            log_action_msg "Generating SSH1 RSA host key..."
99            $KEYGEN -t rsa1 -f $RSA1_KEY -C '' -N '' || exit 1
100         fi
101
102         if ! test -f $RSA_KEY ; then
103            log_action_msg "Generating SSH RSA host key..."
104            $KEYGEN -t rsa -f $RSA_KEY -C '' -N '' || exit 1
105         fi
106
107         if ! test -f $DSA_KEY ; then
108            log_action_msg "Generating SSH2 DSA host key..."
109            $KEYGEN -t dsa -f $DSA_KEY -C '' -N '' || exit 1
110         fi
111
112         log_daemon_msg "Starting OpenBSD Secure Shell server" "sshd"
113         if start-stop-daemon --start --quiet --oknodo --pidfile /var/run/sshd.pid --exec /usr/sbin/sshd -- $SSHD_OPTS; then
114             log_end_msg 0
115         else
116             log_end_msg 1
117         fi
118         ;;
119   stop)
120         log_daemon_msg "Stopping OpenBSD Secure Shell server" "sshd"
121         if start-stop-daemon --stop --quiet --oknodo --pidfile /var/run/sshd.pid; then
122             log_end_msg 0
123         else
124             log_end_msg 1
125         fi
126         ;;
127
128   reload|force-reload)
129         check_for_no_start
130         check_config
131         log_daemon_msg "Reloading OpenBSD Secure Shell server's configuration" "sshd"
132         if start-stop-daemon --stop --signal 1 --quiet --oknodo --pidfile /var/run/sshd.pid --exec /usr/sbin/sshd; then
133             log_end_msg 0
134         else
135             log_end_msg 1
136         fi
137         ;;
138
139   restart)
140         check_privsep_dir
141         check_config
142         log_daemon_msg "Restarting OpenBSD Secure Shell server" "sshd"
143         start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile /var/run/sshd.pid
144         check_for_no_start log_end_msg
145         check_dev_null log_end_msg
146         if start-stop-daemon --start --quiet --oknodo --pidfile /var/run/sshd.pid --exec /usr/sbin/sshd -- $SSHD_OPTS; then
147             log_end_msg 0
148         else
149             log_end_msg 1
150         fi
151         ;;
152
153   try-restart)
154         check_privsep_dir
155         check_config
156         log_daemon_msg "Restarting OpenBSD Secure Shell server" "sshd"
157         set +e
158         start-stop-daemon --stop --quiet --retry 30 --pidfile /var/run/sshd.pid
159         RET="$?"
160         set -e
161         case $RET in
162             0)
163                 # old daemon stopped
164                 check_for_no_start log_end_msg
165                 check_dev_null log_end_msg
166                 if start-stop-daemon --start --quiet --oknodo --pidfile /var/run/sshd.pid --exec /usr/sbin/sshd -- $SSHD_OPTS; then
167                     log_end_msg 0
168                 else
169                     log_end_msg 1
170                 fi
171                 ;;
172             1)
173                 # daemon not running
174                 log_progress_msg "(not running)"
175                 log_end_msg 0
176                 ;;
177             *)
178                 # failed to stop
179                 log_progress_msg "(failed to stop)"
180                 log_end_msg 1
181                 ;;
182         esac
183         ;;
184
185   status)
186         status_of_proc -p /var/run/sshd.pid /usr/sbin/sshd sshd && exit 0 || exit $?
187         ;;
188
189   *)
190         log_action_msg "Usage: /etc/init.d/ssh {start|stop|reload|force-reload|restart|try-restart|status}"
191         exit 1
192 esac
193
194 exit 0
195
196 ## END OF FILE #################################################################