1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2006-2010 OpenWrt.org
3 # Copyright (C) 2006 Carlos Sobrinho
11 PROG=/usr/sbin/dropbear
13 EXTRA_COMMANDS="killclients"
14 EXTRA_HELP=" killclients Kill ${NAME} processes except servers and yourself"
23 grep -qs "^ *$ifname:" /proc/net/dev || {
24 procd_append_param command -p "$port"
29 ifconfig "$ifname" | sed -ne '
30 /addr: *fe[89ab][0-9a-f]:/d
31 s/.* addr: *\([0-9a-f:\.]*\).*/\1/p
34 procd_append_param command -p "$addr:$port"
41 # check if section is enabled (default)
43 config_get_bool enabled "${section}" enable 1
44 [ "${enabled}" -eq 0 ] && return 1
46 # increase pid file count to handle multiple instances correctly
47 PIDCOUNT="$(( ${PIDCOUNT} + 1))"
49 local pid_file="/var/run/${NAME}.${PIDCOUNT}.pid"
52 procd_set_param command "$PROG" -F -P "$pid_file"
54 # prepare parameters (initialise with pid file)
57 # A) password authentication
58 config_get_bool val "${section}" PasswordAuth 1
59 [ "${val}" -eq 0 ] && procd_append_param command -s
61 # B) listen interface and port
64 config_get interface "${section}" Interface
65 [ -n "$interface" ] && network_get_device interface "$interface"
66 config_get port "${section}" Port 22
67 append_ports "$interface" "$port"
69 config_get val "${section}" BannerFile
70 [ -f "${val}" ] && procd_append_param command -b "${val}"
72 config_get_bool val "${section}" GatewayPorts 0
73 [ "${val}" -eq 1 ] && procd_append_param command -a
74 # E) root password authentication
75 config_get_bool val "${section}" RootPasswordAuth 1
76 [ "${val}" -eq 0 ] && procd_append_param command -g
78 config_get_bool val "${section}" RootLogin 1
79 [ "${val}" -eq 0 ] && procd_append_param command -w
81 config_get val "${section}" rsakeyfile
82 [ -f "${val}" ] && procd_append_param command -r "${val}"
83 config_get val "${section}" dsskeyfile
84 [ -f "${val}" ] && procd_append_param command -d "${val}"
91 for keytype in rsa dss; do
93 key=dropbear/dropbear_${keytype}_host_key
94 [ -f /tmp/$key -o -s /etc/$key ] || {
95 # generate missing keys
96 mkdir -p /tmp/dropbear
97 [ -x /usr/bin/dropbearkey ] && {
98 /usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start
104 lock /tmp/.switch2jffs
105 mkdir -p /etc/dropbear
106 mv /tmp/dropbear/dropbear_* /etc/dropbear/
107 lock -u /tmp/.switch2jffs
108 chown root /etc/dropbear
109 chmod 0700 /etc/dropbear
114 [ -s /etc/dropbear/dropbear_rsa_host_key -a \
115 -s /etc/dropbear/dropbear_dss_host_key ] || keygen
118 . /lib/functions/network.sh
120 config_load "${NAME}"
121 config_foreach dropbear_instance dropbear
126 procd_add_config_trigger "dropbear" "/etc/init.d/dropbear" "restart"
135 # if this script is run from inside a client session, then ignore that session
137 while [ "${pid}" -ne 0 ]
139 # get parent process id
140 pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
141 [ "${pid}" -eq 0 ] && break
143 # check if client connection
144 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
145 append ignore "${pid}"
150 # get all server pids that should be ignored
151 for server in `cat /var/run/${NAME}.*.pid`
153 append ignore "${server}"
156 # get all running pids and kill client connections
158 for pid in `pidof "${NAME}"`
160 # check if correct program, otherwise process next pid
161 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
165 # check if pid should be ignored (servers, ourself)
167 for server in ${ignore}
169 if [ "${pid}" == "${server}" ]
175 [ "${skip}" -ne 0 ] && continue
178 echo "${initscript}: Killing ${pid}..."