| 1 | #!/bin/sh /etc/rc.common |
| 2 | # Copyright (C) 2006-2010 OpenWrt.org |
| 3 | # Copyright (C) 2006 Carlos Sobrinho |
| 4 | |
| 5 | NAME=dropbear |
| 6 | PROG=/usr/sbin/dropbear |
| 7 | START=50 |
| 8 | STOP=50 |
| 9 | PIDCOUNT=0 |
| 10 | EXTRA_COMMANDS="killclients" |
| 11 | EXTRA_HELP=" killclients Kill ${NAME} processes except servers and yourself" |
| 12 | |
| 13 | dropbear_start() |
| 14 | { |
| 15 | append_ports() |
| 16 | { |
| 17 | local ifname="$1" |
| 18 | local port="$2" |
| 19 | |
| 20 | grep -qs "^ *$ifname:" /proc/net/dev || { |
| 21 | append args "-p $port" |
| 22 | return |
| 23 | } |
| 24 | |
| 25 | for addr in $( |
| 26 | ifconfig "$ifname" | sed -ne ' |
| 27 | /addr: *fe[89ab][0-9a-f]:/d |
| 28 | s/.* addr: *\([0-9a-f:\.]*\).*/\1/p |
| 29 | ' |
| 30 | ); do |
| 31 | append args "-p $addr:$port" |
| 32 | done |
| 33 | } |
| 34 | |
| 35 | |
| 36 | local section="$1" |
| 37 | |
| 38 | # check if section is enabled (default) |
| 39 | local enabled |
| 40 | config_get_bool enabled "${section}" enable 1 |
| 41 | [ "${enabled}" -eq 0 ] && return 1 |
| 42 | |
| 43 | # verbose parameter |
| 44 | local verbosed |
| 45 | config_get_bool verbosed "${section}" verbose 0 |
| 46 | |
| 47 | # increase pid file count to handle multiple instances correctly |
| 48 | PIDCOUNT="$(( ${PIDCOUNT} + 1))" |
| 49 | |
| 50 | # prepare parameters (initialise with pid file) |
| 51 | local args="-P /var/run/${NAME}.${PIDCOUNT}.pid" |
| 52 | local val |
| 53 | # A) password authentication |
| 54 | config_get_bool val "${section}" PasswordAuth 1 |
| 55 | [ "${val}" -eq 0 ] && append args "-s" |
| 56 | # B) listen interface and port |
| 57 | local port |
| 58 | local interface |
| 59 | config_get interface "${section}" Interface |
| 60 | config_get interface "${interface}" ifname "$interface" |
| 61 | config_get port "${section}" Port 22 |
| 62 | append_ports "$interface" "$port" |
| 63 | # C) banner file |
| 64 | config_get val "${section}" BannerFile |
| 65 | [ -f "${val}" ] && append args "-b ${val}" |
| 66 | # D) gatewayports |
| 67 | config_get_bool val "${section}" GatewayPorts 0 |
| 68 | [ "${val}" -eq 1 ] && append args "-a" |
| 69 | # E) root password authentication |
| 70 | config_get_bool val "${section}" RootPasswordAuth 1 |
| 71 | [ "${val}" -eq 0 ] && append args "-g" |
| 72 | # F) root login |
| 73 | config_get_bool val "${section}" RootLogin 1 |
| 74 | [ "${val}" -eq 0 ] && append args "-w" |
| 75 | # G) host keys |
| 76 | config_get val "${section}" rsakeyfile |
| 77 | [ -f "${val}" ] && append args "-r ${val}" |
| 78 | config_get val "${section}" dsskeyfile |
| 79 | [ -f "${val}" ] && append args "-d ${val}" |
| 80 | |
| 81 | # execute program and return its exit code |
| 82 | [ "${verbosed}" -ne 0 ] && echo "${initscript}: section ${section} starting ${PROG} ${args}" |
| 83 | ${PROG} ${args} |
| 84 | return $? |
| 85 | } |
| 86 | |
| 87 | keygen() |
| 88 | { |
| 89 | for keytype in rsa dss; do |
| 90 | # check for keys |
| 91 | key=dropbear/dropbear_${keytype}_host_key |
| 92 | [ -f /tmp/$key -o -s /etc/$key ] || { |
| 93 | # generate missing keys |
| 94 | mkdir -p /tmp/dropbear |
| 95 | [ -x /usr/bin/dropbearkey ] && { |
| 96 | /usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start |
| 97 | } & |
| 98 | exit 0 |
| 99 | } |
| 100 | done |
| 101 | |
| 102 | lock /tmp/.switch2jffs |
| 103 | mkdir -p /etc/dropbear |
| 104 | mv /tmp/dropbear/dropbear_* /etc/dropbear/ |
| 105 | lock -u /tmp/.switch2jffs |
| 106 | chown root /etc/dropbear |
| 107 | chmod 0700 /etc/dropbear |
| 108 | } |
| 109 | |
| 110 | start() |
| 111 | { |
| 112 | [ -s /etc/dropbear/dropbear_rsa_host_key -a \ |
| 113 | -s /etc/dropbear/dropbear_dss_host_key ] || keygen |
| 114 | |
| 115 | include /lib/network |
| 116 | scan_interfaces |
| 117 | config_load "${NAME}" |
| 118 | config_foreach dropbear_start dropbear |
| 119 | } |
| 120 | |
| 121 | stop() |
| 122 | { |
| 123 | # killing all server processes |
| 124 | local pidfile |
| 125 | for pidfile in `ls /var/run/${NAME}.*.pid` |
| 126 | do |
| 127 | start-stop-daemon -q -K -s KILL -p "${pidfile}" -n "${NAME}" |
| 128 | rm -f "${pidfile}" |
| 129 | done |
| 130 | [ -z "${pidfile}" ] && echo "${initscript}: no pid files, if you get problems with start then try killclients" |
| 131 | } |
| 132 | |
| 133 | killclients() |
| 134 | { |
| 135 | local ignore='' |
| 136 | local server |
| 137 | local pid |
| 138 | |
| 139 | # if this script is run from inside a client session, then ignore that session |
| 140 | pid="$$" |
| 141 | while [ "${pid}" -ne 0 ] |
| 142 | do |
| 143 | # get parent process id |
| 144 | pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"` |
| 145 | [ "${pid}" -eq 0 ] && break |
| 146 | |
| 147 | # check if client connection |
| 148 | grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && { |
| 149 | append ignore "${pid}" |
| 150 | break |
| 151 | } |
| 152 | done |
| 153 | |
| 154 | # get all server pids that should be ignored |
| 155 | for server in `cat /var/run/${NAME}.*.pid` |
| 156 | do |
| 157 | append ignore "${server}" |
| 158 | done |
| 159 | |
| 160 | # get all running pids and kill client connections |
| 161 | local skip |
| 162 | for pid in `pidof "${NAME}"` |
| 163 | do |
| 164 | # check if correct program, otherwise process next pid |
| 165 | grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || { |
| 166 | continue |
| 167 | } |
| 168 | |
| 169 | # check if pid should be ignored (servers, ourself) |
| 170 | skip=0 |
| 171 | for server in ${ignore} |
| 172 | do |
| 173 | if [ "${pid}" == "${server}" ] |
| 174 | then |
| 175 | skip=1 |
| 176 | break |
| 177 | fi |
| 178 | done |
| 179 | [ "${skip}" -ne 0 ] && continue |
| 180 | |
| 181 | # kill process |
| 182 | echo "${initscript}: Killing ${pid}..." |
| 183 | kill -KILL ${pid} |
| 184 | done |
| 185 | } |
| 186 | |