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"
20 grep -qs "^ *$ifname:" /proc/net/dev || {
21 procd_append_param command -p "$port"
26 ifconfig "$ifname" | sed -ne '
27 /addr: *fe[89ab][0-9a-f]:/d
28 s/.* addr: *\([0-9a-f:\.]*\).*/\1/p
31 procd_append_param command -p "$addr:$port"
35 validate_section_dropbear()
37 uci_validate_section dropbear dropbear "${1}" \
38 'PasswordAuth:bool:1' \
41 'GatewayPorts:bool:0' \
42 'RootPasswordAuth:bool:1' \
47 'Port:list(port):22' \
48 'SSHKeepAlive:uinteger:300' \
49 'IdleTimeout:uinteger:0'
55 local PasswordAuth enable Interface GatewayPorts \
56 RootPasswordAuth RootLogin rsakeyfile \
57 dsskeyfile BannerFile Port SSHKeepAlive IdleTimeout
59 validate_section_dropbear "${1}" || {
60 echo "validation failed"
64 [ "${enable}" = "0" ] && return 1
65 PIDCOUNT="$(( ${PIDCOUNT} + 1))"
66 local pid_file="/var/run/${NAME}.${PIDCOUNT}.pid"
69 procd_set_param command "$PROG" -F -P "$pid_file"
70 [ "${PasswordAuth}" -eq 0 ] && procd_append_param command -s
71 [ "${GatewayPorts}" -eq 1 ] && procd_append_param command -a
72 [ "${RootPasswordAuth}" -eq 0 ] && procd_append_param command -g
73 [ "${RootLogin}" -eq 0 ] && procd_append_param command -w
74 [ -n "${rsakeyfile}" ] && procd_append_param command -r "${rsakeyfile}"
75 [ -n "${dsskeyfile}" ] && procd_append_param command -d "${dsskeyfile}"
76 [ -n "${BannerFile}" ] && procd_append_param command -b "${BannerFile}"
77 [ -n "${Interface}" ] && network_get_device Interface "${Interface}"
78 append_ports "${Interface}" "${Port}"
79 [ "${IdleTimeout}" -ne 0 ] && procd_append_param command -I "${IdleTimeout}"
80 [ "${SSHKeepAlive}" -ne 0 ] && procd_append_param command -K "${SSHKeepAlive}"
86 for keytype in rsa dss; do
88 key=dropbear/dropbear_${keytype}_host_key
89 [ -f /tmp/$key -o -s /etc/$key ] || {
90 # generate missing keys
91 mkdir -p /tmp/dropbear
92 [ -x /usr/bin/dropbearkey ] && {
93 /usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start
99 lock /tmp/.switch2jffs
100 mkdir -p /etc/dropbear
101 mv /tmp/dropbear/dropbear_* /etc/dropbear/
102 lock -u /tmp/.switch2jffs
103 chown root /etc/dropbear
104 chmod 0700 /etc/dropbear
109 [ -s /etc/dropbear/dropbear_rsa_host_key -a \
110 -s /etc/dropbear/dropbear_dss_host_key ] || keygen
113 . /lib/functions/network.sh
115 config_load "${NAME}"
116 config_foreach dropbear_instance dropbear
121 procd_add_reload_trigger "dropbear"
122 procd_add_validation validate_section_dropbear
131 # if this script is run from inside a client session, then ignore that session
133 while [ "${pid}" -ne 0 ]
135 # get parent process id
136 pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
137 [ "${pid}" -eq 0 ] && break
139 # check if client connection
140 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
141 append ignore "${pid}"
146 # get all server pids that should be ignored
147 for server in `cat /var/run/${NAME}.*.pid`
149 append ignore "${server}"
152 # get all running pids and kill client connections
154 for pid in `pidof "${NAME}"`
156 # check if correct program, otherwise process next pid
157 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
161 # check if pid should be ignored (servers, ourself)
163 for server in ${ignore}
165 if [ "${pid}" == "${server}" ]
171 [ "${skip}" -ne 0 ] && continue
174 echo "${initscript}: Killing ${pid}..."