Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | ### BEGIN INIT INFO |
| 3 | # Provides: sshd |
| 4 | # Required-Start: $remote_fs $syslog $networking |
| 5 | # Required-Stop: $remote_fs $syslog |
| 6 | # Default-Start: 2 3 4 5 |
| 7 | # Default-Stop: 1 |
| 8 | # Short-Description: Dropbear Secure Shell server |
| 9 | ### END INIT INFO |
| 10 | # |
| 11 | # Do not configure this file. Edit /etc/default/dropbear instead! |
| 12 | # |
| 13 | |
| 14 | PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin |
| 15 | DAEMON=/usr/sbin/dropbear |
| 16 | NAME=dropbear |
| 17 | DESC="Dropbear SSH server" |
| 18 | PIDFILE=/var/run/dropbear.pid |
| 19 | |
| 20 | DROPBEAR_PORT=22 |
| 21 | DROPBEAR_EXTRA_ARGS= |
| 22 | NO_START=0 |
| 23 | |
| 24 | set -e |
| 25 | |
| 26 | test ! -r /etc/default/dropbear || . /etc/default/dropbear |
| 27 | test "$NO_START" = "0" || exit 0 |
| 28 | test -x "$DAEMON" || exit 0 |
| 29 | test ! -h /var/service/dropbear || exit 0 |
| 30 | |
| 31 | readonly_rootfs=0 |
| 32 | for flag in `awk '{ if ($2 == "/") { split($4,FLAGS,",") } }; END { for (f in FLAGS) print FLAGS[f] }' </proc/mounts`; do |
| 33 | case $flag in |
| 34 | ro) |
| 35 | readonly_rootfs=1 |
| 36 | ;; |
| 37 | esac |
| 38 | done |
| 39 | |
| 40 | if [ $readonly_rootfs = "1" ]; then |
| 41 | mkdir -p /var/lib/dropbear |
| 42 | DROPBEAR_RSAKEY_DEFAULT="/var/lib/dropbear/dropbear_rsa_host_key" |
| 43 | DROPBEAR_DSSKEY_DEFAULT="/var/lib/dropbear/dropbear_dss_host_key" |
| 44 | else |
| 45 | DROPBEAR_RSAKEY_DEFAULT="/etc/dropbear/dropbear_rsa_host_key" |
| 46 | DROPBEAR_DSSKEY_DEFAULT="/etc/dropbear/dropbear_dss_host_key" |
| 47 | fi |
| 48 | |
| 49 | test -z "$DROPBEAR_BANNER" || \ |
| 50 | DROPBEAR_EXTRA_ARGS="$DROPBEAR_EXTRA_ARGS -b $DROPBEAR_BANNER" |
| 51 | test -n "$DROPBEAR_RSAKEY" || \ |
| 52 | DROPBEAR_RSAKEY=$DROPBEAR_RSAKEY_DEFAULT |
| 53 | test -n "$DROPBEAR_DSSKEY" || \ |
| 54 | DROPBEAR_DSSKEY=$DROPBEAR_DSSKEY_DEFAULT |
| 55 | test -n "$DROPBEAR_KEYTYPES" || \ |
| 56 | DROPBEAR_KEYTYPES="rsa" |
| 57 | |
| 58 | gen_keys() { |
| 59 | for t in $DROPBEAR_KEYTYPES; do |
| 60 | case $t in |
| 61 | rsa) |
| 62 | if [ -f "$DROPBEAR_RSAKEY" -a ! -s "$DROPBEAR_RSAKEY" ]; then |
| 63 | rm $DROPBEAR_RSAKEY || true |
| 64 | fi |
| 65 | test -f $DROPBEAR_RSAKEY || dropbearkey -t rsa -f $DROPBEAR_RSAKEY |
| 66 | ;; |
| 67 | dsa) |
| 68 | if [ -f "$DROPBEAR_DSSKEY" -a ! -s "$DROPBEAR_DSSKEY" ]; then |
| 69 | rm $DROPBEAR_DSSKEY || true |
| 70 | fi |
| 71 | test -f $DROPBEAR_DSSKEY || dropbearkey -t dss -f $DROPBEAR_DSSKEY |
| 72 | ;; |
| 73 | esac |
| 74 | done |
| 75 | } |
| 76 | |
| 77 | case "$1" in |
| 78 | start) |
| 79 | echo -n "Starting $DESC: " |
| 80 | gen_keys |
| 81 | KEY_ARGS="" |
| 82 | test -f $DROPBEAR_DSSKEY && KEY_ARGS="$KEY_ARGS -d $DROPBEAR_DSSKEY" |
| 83 | test -f $DROPBEAR_RSAKEY && KEY_ARGS="$KEY_ARGS -r $DROPBEAR_RSAKEY" |
| 84 | start-stop-daemon -S -p $PIDFILE \ |
| 85 | -x "$DAEMON" -- $KEY_ARGS \ |
| 86 | -p "$DROPBEAR_PORT" $DROPBEAR_EXTRA_ARGS |
| 87 | echo "$NAME." |
| 88 | ;; |
| 89 | stop) |
| 90 | echo -n "Stopping $DESC: " |
| 91 | start-stop-daemon -K -x "$DAEMON" -p $PIDFILE |
| 92 | echo "$NAME." |
| 93 | ;; |
| 94 | restart|force-reload) |
| 95 | echo -n "Restarting $DESC: " |
| 96 | start-stop-daemon -K -x "$DAEMON" -p $PIDFILE |
| 97 | sleep 1 |
| 98 | KEY_ARGS="" |
| 99 | test -f $DROPBEAR_DSSKEY && KEY_ARGS="$KEY_ARGS -d $DROPBEAR_DSSKEY" |
| 100 | test -f $DROPBEAR_RSAKEY && KEY_ARGS="$KEY_ARGS -r $DROPBEAR_RSAKEY" |
| 101 | start-stop-daemon -S -p $PIDFILE \ |
| 102 | -x "$DAEMON" -- $KEY_ARGS \ |
| 103 | -p "$DROPBEAR_PORT" $DROPBEAR_EXTRA_ARGS |
| 104 | echo "$NAME." |
| 105 | ;; |
| 106 | *) |
| 107 | N=/etc/init.d/$NAME |
| 108 | echo "Usage: $N {start|stop|restart|force-reload}" >&2 |
| 109 | exit 1 |
| 110 | ;; |
| 111 | esac |
| 112 | |
| 113 | exit 0 |