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' \
|
2017-06-27 20:30:01 +00:00
|
|
|
'MaxAuthTries:uinteger:3' \
|
2018-02-18 00:15:58 +00:00
|
|
|
'RecvWindowSize:uinteger:0' \
|
2016-08-25 11:48:30 +00:00
|
|
|
'mdns:bool: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 \
|
2018-02-18 00:15:58 +00:00
|
|
|
MaxAuthTries RecvWindowSize 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}"
|
2017-06-27 20:30:01 +00:00
|
|
|
[ "${MaxAuthTries}" -ne 0 ] && procd_append_param command -T "${MaxAuthTries}"
|
2018-02-18 00:15:58 +00:00
|
|
|
[ "${RecvWindowSize}" -gt 0 -a "${RecvWindowSize}" -le 1048576 ] && \
|
|
|
|
procd_append_param command -W "${RecvWindowSize}"
|
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
|
2017-04-26 14:44:02 +00:00
|
|
|
config_get enable "$1" enable 1
|
|
|
|
|
|
|
|
[ "${enable}" = "1" ] && interfaces=" ${interface} ${interfaces}"
|
2016-04-27 09:14:00 +00:00
|
|
|
}
|
|
|
|
|
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_add_config_trigger "config.change" "dropbear" /etc/init.d/dropbear reload
|
|
|
|
|
|
|
|
config_load "${NAME}"
|
|
|
|
config_foreach load_interfaces dropbear
|
|
|
|
|
2017-06-15 11:58:25 +00:00
|
|
|
[ -n "${interfaces}" ] && {
|
2016-04-27 09:14:00 +00:00
|
|
|
for n in $interfaces ; do
|
|
|
|
procd_add_interface_trigger "interface.*" $n /etc/init.d/dropbear reload
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2013-11-13 10:49:41 +00:00
|
|
|
procd_add_validation validate_section_dropbear
|
2013-07-11 17:00:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 02:36:03 +00:00
|
|
|
shutdown() {
|
|
|
|
# close all open connections
|
|
|
|
killall dropbear
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|