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' \
41 'Port:list(port):22' \
42 'SSHKeepAlive:uinteger:300' \
43 'IdleTimeout:uinteger:0' \
49 local PasswordAuth enable Interface GatewayPorts \
50 RootPasswordAuth RootLogin rsakeyfile \
51 BannerFile Port SSHKeepAlive IdleTimeout \
54 validate_section_dropbear "${1}" || {
55 echo "validation failed"
59 [ -n "${Interface}" ] && {
60 network_get_ipaddrs_all ipaddrs "${Interface}" || {
61 echo "interface ${Interface} has no physdev or physdev has no suitable ip"
66 [ "${enable}" = "0" ] && return 1
67 PIDCOUNT="$(( ${PIDCOUNT} + 1))"
68 local pid_file="/var/run/${NAME}.${PIDCOUNT}.pid"
71 procd_set_param command "$PROG" -F -P "$pid_file"
72 [ "${PasswordAuth}" -eq 0 ] && procd_append_param command -s
73 [ "${GatewayPorts}" -eq 1 ] && procd_append_param command -a
74 [ "${RootPasswordAuth}" -eq 0 ] && procd_append_param command -g
75 [ "${RootLogin}" -eq 0 ] && procd_append_param command -w
76 [ -n "${rsakeyfile}" ] && procd_append_param command -r "${rsakeyfile}"
77 [ -n "${BannerFile}" ] && procd_append_param command -b "${BannerFile}"
78 append_ports "${ipaddrs}" "${Port}"
79 [ "${IdleTimeout}" -ne 0 ] && procd_append_param command -I "${IdleTimeout}"
80 [ "${SSHKeepAlive}" -ne 0 ] && procd_append_param command -K "${SSHKeepAlive}"
81 [ "${mdns}" -ne 0 ] && procd_add_mdns "ssh" "tcp" "$Port" "daemon=dropbear"
82 procd_set_param respawn
88 for keytype in rsa; do
90 key=dropbear/dropbear_${keytype}_host_key
91 [ -f /tmp/$key -o -s /etc/$key ] || {
92 # generate missing keys
93 mkdir -p /tmp/dropbear
94 [ -x /usr/bin/dropbearkey ] && {
95 /usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start
101 lock /tmp/.switch2jffs
102 mkdir -p /etc/dropbear
103 mv /tmp/dropbear/dropbear_* /etc/dropbear/
104 lock -u /tmp/.switch2jffs
105 chown root /etc/dropbear
106 chmod 0700 /etc/dropbear
111 config_get interface "$1" Interface
112 interfaces=" ${interface} ${interfaces}"
117 [ -s /etc/dropbear/dropbear_rsa_host_key ] || keygen
120 . /lib/functions/network.sh
122 config_load "${NAME}"
123 config_foreach dropbear_instance dropbear
130 procd_add_config_trigger "config.change" "dropbear" /etc/init.d/dropbear reload
132 config_load "${NAME}"
133 config_foreach load_interfaces dropbear
135 [ -n "${interfaces}" ] & {
136 for n in $interfaces ; do
137 procd_add_interface_trigger "interface.*" $n /etc/init.d/dropbear reload
141 procd_add_validation validate_section_dropbear
150 # if this script is run from inside a client session, then ignore that session
152 while [ "${pid}" -ne 0 ]
154 # get parent process id
155 pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
156 [ "${pid}" -eq 0 ] && break
158 # check if client connection
159 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
160 append ignore "${pid}"
165 # get all server pids that should be ignored
166 for server in `cat /var/run/${NAME}.*.pid`
168 append ignore "${server}"
171 # get all running pids and kill client connections
173 for pid in `pidof "${NAME}"`
175 # check if correct program, otherwise process next pid
176 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
180 # check if pid should be ignored (servers, ourself)
182 for server in ${ignore}
184 if [ "${pid}" = "${server}" ]
190 [ "${skip}" -ne 0 ] && continue
193 echo "${initscript}: Killing ${pid}..."