2006-10-04 20:05:48 +00:00
|
|
|
#!/bin/sh /etc/rc.common
|
2010-02-10 00:34:32 +00:00
|
|
|
# Copyright (C) 2006-2010 OpenWrt.org
|
2006-10-29 05:53:13 +00:00
|
|
|
# Copyright (C) 2006 Carlos Sobrinho
|
|
|
|
|
2009-08-03 22:19:51 +00:00
|
|
|
START=50
|
2010-01-30 18:19:42 +00:00
|
|
|
STOP=50
|
2011-11-07 22:58:50 +00:00
|
|
|
|
2012-12-22 18:56:13 +00:00
|
|
|
USE_PROCD=1
|
2011-11-07 22:58:50 +00:00
|
|
|
PROG=/usr/sbin/dropbear
|
2013-09-23 21:58:36 +00:00
|
|
|
NAME=dropbear
|
2009-08-03 22:19:51 +00:00
|
|
|
PIDCOUNT=0
|
|
|
|
EXTRA_COMMANDS="killclients"
|
|
|
|
EXTRA_HELP=" killclients Kill ${NAME} processes except servers and yourself"
|
|
|
|
|
2013-11-13 10:49:41 +00:00
|
|
|
append_ports()
|
2009-08-03 22:19:51 +00:00
|
|
|
{
|
2014-10-09 07:16:35 +00:00
|
|
|
local ipaddrs="$1"
|
2013-11-13 10:49:41 +00:00
|
|
|
local port="$2"
|
2011-07-31 12:05:54 +00:00
|
|
|
|
2014-10-09 07:16:35 +00:00
|
|
|
[ -z "$ipaddrs" ] && {
|
2013-11-13 10:49:41 +00:00
|
|
|
procd_append_param command -p "$port"
|
|
|
|
return
|
2011-07-31 12:05:54 +00:00
|
|
|
}
|
|
|
|
|
2014-10-09 07:16:35 +00:00
|
|
|
for addr in $ipaddrs; do
|
2013-11-13 10:49:41 +00:00
|
|
|
procd_append_param command -p "$addr:$port"
|
|
|
|
done
|
|
|
|
}
|
2011-07-31 12:05:54 +00:00
|
|
|
|
2013-11-13 10:49:41 +00:00
|
|
|
validate_section_dropbear()
|
|
|
|
{
|
|
|
|
uci_validate_section dropbear dropbear "${1}" \
|
|
|
|
'PasswordAuth:bool:1' \
|
|
|
|
'enable:bool:1' \
|
|
|
|
'Interface:string' \
|
2013-11-13 11:07:24 +00:00
|
|
|
'GatewayPorts:bool:0' \
|
2013-11-13 10:49:41 +00:00
|
|
|
'RootPasswordAuth:bool:1' \
|
|
|
|
'RootLogin:bool:1' \
|
|
|
|
'rsakeyfile:file' \
|
|
|
|
'BannerFile:file' \
|
2014-03-29 17:05:35 +00:00
|
|
|
'Port:list(port):22' \
|
|
|
|
'SSHKeepAlive:uinteger:300' \
|
2014-08-29 18:16:41 +00:00
|
|
|
'IdleTimeout:uinteger:0' \
|
|
|
|
'mdns:uinteger:1'
|
2013-11-13 10:49:41 +00:00
|
|
|
}
|
2009-08-03 22:19:51 +00:00
|
|
|
|
2013-11-13 10:49:41 +00:00
|
|
|
dropbear_instance()
|
|
|
|
{
|
|
|
|
local PasswordAuth enable Interface GatewayPorts \
|
|
|
|
RootPasswordAuth RootLogin rsakeyfile \
|
2015-09-08 08:59:40 +00:00
|
|
|
BannerFile Port SSHKeepAlive IdleTimeout \
|
2014-10-09 07:16:35 +00:00
|
|
|
mdns ipaddrs
|
2009-08-03 22:19:51 +00:00
|
|
|
|
2013-11-13 10:49:41 +00:00
|
|
|
validate_section_dropbear "${1}" || {
|
|
|
|
echo "validation failed"
|
|
|
|
return 1
|
|
|
|
}
|
2009-08-03 22:19:51 +00:00
|
|
|
|
2014-10-09 07:16:35 +00:00
|
|
|
[ -n "${Interface}" ] && {
|
|
|
|
network_get_ipaddrs_all ipaddrs "${Interface}" || {
|
|
|
|
echo "interface ${Interface} has no physdev or physdev has no suitable ip"
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-13 10:49:41 +00:00
|
|
|
[ "${enable}" = "0" ] && return 1
|
|
|
|
PIDCOUNT="$(( ${PIDCOUNT} + 1))"
|
2011-11-07 22:58:50 +00:00
|
|
|
local pid_file="/var/run/${NAME}.${PIDCOUNT}.pid"
|
2012-12-22 18:56:13 +00:00
|
|
|
|
|
|
|
procd_open_instance
|
|
|
|
procd_set_param command "$PROG" -F -P "$pid_file"
|
2013-11-13 10:49:41 +00:00
|
|
|
[ "${PasswordAuth}" -eq 0 ] && procd_append_param command -s
|
|
|
|
[ "${GatewayPorts}" -eq 1 ] && procd_append_param command -a
|
|
|
|
[ "${RootPasswordAuth}" -eq 0 ] && procd_append_param command -g
|
|
|
|
[ "${RootLogin}" -eq 0 ] && procd_append_param command -w
|
|
|
|
[ -n "${rsakeyfile}" ] && procd_append_param command -r "${rsakeyfile}"
|
|
|
|
[ -n "${BannerFile}" ] && procd_append_param command -b "${BannerFile}"
|
2014-10-09 07:16:35 +00:00
|
|
|
append_ports "${ipaddrs}" "${Port}"
|
2014-03-29 17:05:35 +00:00
|
|
|
[ "${IdleTimeout}" -ne 0 ] && procd_append_param command -I "${IdleTimeout}"
|
|
|
|
[ "${SSHKeepAlive}" -ne 0 ] && procd_append_param command -K "${SSHKeepAlive}"
|
2014-08-29 18:16:41 +00:00
|
|
|
[ "${mdns}" -ne 0 ] && procd_add_mdns "ssh" "tcp" "$Port" "daemon=dropbear"
|
2015-09-24 08:37:40 +00:00
|
|
|
procd_set_param respawn
|
2012-12-22 18:56:13 +00:00
|
|
|
procd_close_instance
|
2006-10-29 05:53:13 +00:00
|
|
|
}
|
2005-03-06 03:53:29 +00:00
|
|
|
|
2009-08-03 22:19:51 +00:00
|
|
|
keygen()
|
|
|
|
{
|
2015-09-08 08:59:40 +00:00
|
|
|
for keytype in rsa; do
|
2006-10-29 05:53:13 +00:00
|
|
|
# check for keys
|
2006-10-31 10:29:41 +00:00
|
|
|
key=dropbear/dropbear_${keytype}_host_key
|
2009-02-03 22:28:16 +00:00
|
|
|
[ -f /tmp/$key -o -s /etc/$key ] || {
|
2006-10-29 05:53:13 +00:00
|
|
|
# generate missing keys
|
|
|
|
mkdir -p /tmp/dropbear
|
|
|
|
[ -x /usr/bin/dropbearkey ] && {
|
2006-11-01 18:11:44 +00:00
|
|
|
/usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start
|
2006-10-29 05:53:13 +00:00
|
|
|
} &
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
done
|
|
|
|
|
|
|
|
lock /tmp/.switch2jffs
|
|
|
|
mkdir -p /etc/dropbear
|
|
|
|
mv /tmp/dropbear/dropbear_* /etc/dropbear/
|
|
|
|
lock -u /tmp/.switch2jffs
|
|
|
|
chown root /etc/dropbear
|
|
|
|
chmod 0700 /etc/dropbear
|
2006-10-31 10:29:41 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 09:14:00 +00:00
|
|
|
load_interfaces()
|
|
|
|
{
|
|
|
|
config_get interface "$1" Interface
|
|
|
|
interfaces=" ${interface} ${interfaces}"
|
|
|
|
}
|
|
|
|
|
2012-12-22 18:56:13 +00:00
|
|
|
start_service()
|
2009-08-03 22:19:51 +00:00
|
|
|
{
|
2015-09-08 08:59:40 +00:00
|
|
|
[ -s /etc/dropbear/dropbear_rsa_host_key ] || keygen
|
2009-08-03 22:19:51 +00:00
|
|
|
|
2012-12-22 18:55:57 +00:00
|
|
|
. /lib/functions.sh
|
|
|
|
. /lib/functions/network.sh
|
|
|
|
|
2009-08-03 22:19:51 +00:00
|
|
|
config_load "${NAME}"
|
2012-12-22 18:56:13 +00:00
|
|
|
config_foreach dropbear_instance dropbear
|
2006-10-04 20:05:48 +00:00
|
|
|
}
|
2006-10-07 19:11:19 +00:00
|
|
|
|
2013-07-11 17:00:40 +00:00
|
|
|
service_triggers()
|
|
|
|
{
|
2016-04-27 09:14:00 +00:00
|
|
|
local interfaces
|
|
|
|
|
|
|
|
procd_open_trigger
|
|
|
|
procd_add_config_trigger "config.change" "dropbear" /etc/init.d/dropbear reload
|
|
|
|
|
|
|
|
config_load "${NAME}"
|
|
|
|
config_foreach load_interfaces dropbear
|
|
|
|
|
|
|
|
[ -n "${interfaces}" ] & {
|
|
|
|
for n in $interfaces ; do
|
|
|
|
procd_add_interface_trigger "interface.*" $n /etc/init.d/dropbear reload
|
|
|
|
done
|
|
|
|
}
|
|
|
|
procd_close_trigger
|
|
|
|
|
2013-11-13 10:49:41 +00:00
|
|
|
procd_add_validation validate_section_dropbear
|
2013-07-11 17:00:40 +00:00
|
|
|
}
|
|
|
|
|
2009-08-03 22:19:51 +00:00
|
|
|
killclients()
|
|
|
|
{
|
|
|
|
local ignore=''
|
|
|
|
local server
|
|
|
|
local pid
|
|
|
|
|
|
|
|
# if this script is run from inside a client session, then ignore that session
|
|
|
|
pid="$$"
|
|
|
|
while [ "${pid}" -ne 0 ]
|
|
|
|
do
|
|
|
|
# get parent process id
|
|
|
|
pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
|
|
|
|
[ "${pid}" -eq 0 ] && break
|
|
|
|
|
|
|
|
# check if client connection
|
2010-02-10 00:34:32 +00:00
|
|
|
grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
|
2009-08-03 22:19:51 +00:00
|
|
|
append ignore "${pid}"
|
|
|
|
break
|
2010-02-10 00:34:32 +00:00
|
|
|
}
|
2009-08-03 22:19:51 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
# get all server pids that should be ignored
|
|
|
|
for server in `cat /var/run/${NAME}.*.pid`
|
|
|
|
do
|
|
|
|
append ignore "${server}"
|
|
|
|
done
|
|
|
|
|
|
|
|
# get all running pids and kill client connections
|
|
|
|
local skip
|
|
|
|
for pid in `pidof "${NAME}"`
|
|
|
|
do
|
2010-02-10 00:34:32 +00:00
|
|
|
# check if correct program, otherwise process next pid
|
|
|
|
grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
|
|
|
|
continue
|
|
|
|
}
|
2009-08-03 22:19:51 +00:00
|
|
|
|
|
|
|
# check if pid should be ignored (servers, ourself)
|
|
|
|
skip=0
|
|
|
|
for server in ${ignore}
|
|
|
|
do
|
2014-10-14 12:21:11 +00:00
|
|
|
if [ "${pid}" = "${server}" ]
|
2009-08-03 22:19:51 +00:00
|
|
|
then
|
|
|
|
skip=1
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
[ "${skip}" -ne 0 ] && continue
|
|
|
|
|
|
|
|
# kill process
|
|
|
|
echo "${initscript}: Killing ${pid}..."
|
|
|
|
kill -KILL ${pid}
|
|
|
|
done
|
2006-10-07 19:11:19 +00:00
|
|
|
}
|