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