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 [ -z "$ipaddrs" ] && {
21 procd_append_param command -p "$port"
25 for addr in $ipaddrs; do
26 procd_append_param command -p "$addr:$port"
30 validate_section_dropbear()
32 uci_validate_section dropbear dropbear "${1}" \
33 'PasswordAuth:bool:1' \
36 'GatewayPorts:bool:0' \
37 'RootPasswordAuth:bool:1' \
42 'Port:list(port):22' \
43 'SSHKeepAlive:uinteger:300' \
44 'IdleTimeout:uinteger:0' \
50 local PasswordAuth enable Interface GatewayPorts \
51 RootPasswordAuth RootLogin rsakeyfile \
52 dsskeyfile BannerFile Port SSHKeepAlive IdleTimeout \
55 validate_section_dropbear "${1}" || {
56 echo "validation failed"
60 [ -n "${Interface}" ] && {
61 network_get_ipaddrs_all ipaddrs "${Interface}" || {
62 echo "interface ${Interface} has no physdev or physdev has no suitable ip"
67 [ "${enable}" = "0" ] && return 1
68 PIDCOUNT="$(( ${PIDCOUNT} + 1))"
69 local pid_file="/var/run/${NAME}.${PIDCOUNT}.pid"
72 procd_set_param command "$PROG" -F -P "$pid_file"
73 [ "${PasswordAuth}" -eq 0 ] && procd_append_param command -s
74 [ "${GatewayPorts}" -eq 1 ] && procd_append_param command -a
75 [ "${RootPasswordAuth}" -eq 0 ] && procd_append_param command -g
76 [ "${RootLogin}" -eq 0 ] && procd_append_param command -w
77 [ -n "${rsakeyfile}" ] && procd_append_param command -r "${rsakeyfile}"
78 [ -n "${dsskeyfile}" ] && procd_append_param command -d "${dsskeyfile}"
79 [ -n "${BannerFile}" ] && procd_append_param command -b "${BannerFile}"
80 append_ports "${ipaddrs}" "${Port}"
81 [ "${IdleTimeout}" -ne 0 ] && procd_append_param command -I "${IdleTimeout}"
82 [ "${SSHKeepAlive}" -ne 0 ] && procd_append_param command -K "${SSHKeepAlive}"
83 [ "${mdns}" -ne 0 ] && procd_add_mdns "ssh" "tcp" "$Port" "daemon=dropbear"
89 for keytype in rsa dss; do
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
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
112 [ -s /etc/dropbear/dropbear_rsa_host_key -a \
113 -s /etc/dropbear/dropbear_dss_host_key ] || keygen
116 . /lib/functions/network.sh
118 config_load "${NAME}"
119 config_foreach dropbear_instance dropbear
124 procd_add_reload_trigger "dropbear"
125 procd_add_validation validate_section_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}..."