1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2006-2010 OpenWrt.org
3 # Copyright (C) 2006 Carlos Sobrinho
9 PROG=/usr/sbin/dropbear
12 EXTRA_COMMANDS="killclients"
13 EXTRA_HELP=" killclients Kill ${NAME} processes except servers and yourself"
22 grep -qs "^ *$ifname:" /proc/net/dev || {
23 procd_append_param command -p "$port"
28 ifconfig "$ifname" | sed -ne '
29 /addr: *fe[89ab][0-9a-f]:/d
30 s/.* addr: *\([0-9a-f:\.]*\).*/\1/p
33 procd_append_param command -p "$addr:$port"
40 # check if section is enabled (default)
42 config_get_bool enabled "${section}" enable 1
43 [ "${enabled}" -eq 0 ] && return 1
45 # increase pid file count to handle multiple instances correctly
46 PIDCOUNT="$(( ${PIDCOUNT} + 1))"
48 local pid_file="/var/run/${NAME}.${PIDCOUNT}.pid"
51 procd_set_param command "$PROG" -F -P "$pid_file"
53 # prepare parameters (initialise with pid file)
56 # A) password authentication
57 config_get_bool val "${section}" PasswordAuth 1
58 [ "${val}" -eq 0 ] && procd_append_param command -s
60 # B) listen interface and port
63 config_get interface "${section}" Interface
64 [ -n "$interface" ] && network_get_device interface "$interface"
65 config_get port "${section}" Port 22
66 append_ports "$interface" "$port"
68 config_get val "${section}" BannerFile
69 [ -f "${val}" ] && procd_append_param command -b "${val}"
71 config_get_bool val "${section}" GatewayPorts 0
72 [ "${val}" -eq 1 ] && procd_append_param command -a
73 # E) root password authentication
74 config_get_bool val "${section}" RootPasswordAuth 1
75 [ "${val}" -eq 0 ] && procd_append_param command -g
77 config_get_bool val "${section}" RootLogin 1
78 [ "${val}" -eq 0 ] && procd_append_param command -w
80 config_get val "${section}" rsakeyfile
81 [ -f "${val}" ] && procd_append_param command -r "${val}"
82 config_get val "${section}" dsskeyfile
83 [ -f "${val}" ] && procd_append_param command -d "${val}"
90 for keytype in rsa dss; do
92 key=dropbear/dropbear_${keytype}_host_key
93 [ -f /tmp/$key -o -s /etc/$key ] || {
94 # generate missing keys
95 mkdir -p /tmp/dropbear
96 [ -x /usr/bin/dropbearkey ] && {
97 /usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start
103 lock /tmp/.switch2jffs
104 mkdir -p /etc/dropbear
105 mv /tmp/dropbear/dropbear_* /etc/dropbear/
106 lock -u /tmp/.switch2jffs
107 chown root /etc/dropbear
108 chmod 0700 /etc/dropbear
113 [ -s /etc/dropbear/dropbear_rsa_host_key -a \
114 -s /etc/dropbear/dropbear_dss_host_key ] || keygen
117 . /lib/functions/network.sh
119 config_load "${NAME}"
120 config_foreach dropbear_instance dropbear
125 procd_add_reload_trigger "dropbear"
134 # if this script is run from inside a client session, then ignore that session
136 while [ "${pid}" -ne 0 ]
138 # get parent process id
139 pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
140 [ "${pid}" -eq 0 ] && break
142 # check if client connection
143 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
144 append ignore "${pid}"
149 # get all server pids that should be ignored
150 for server in `cat /var/run/${NAME}.*.pid`
152 append ignore "${server}"
155 # get all running pids and kill client connections
157 for pid in `pidof "${NAME}"`
159 # check if correct program, otherwise process next pid
160 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
164 # check if pid should be ignored (servers, ourself)
166 for server in ${ignore}
168 if [ "${pid}" == "${server}" ]
174 [ "${skip}" -ne 0 ] && continue
177 echo "${initscript}: Killing ${pid}..."