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
|
|
|
|
|
2018-12-18 18:40:31 +00:00
|
|
|
START=19
|
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
|
2020-07-24 07:33:59 +00:00
|
|
|
|
|
|
|
extra_command "killclients" "Kill ${NAME} processes except servers and yourself"
|
2009-08-03 22:19:51 +00:00
|
|
|
|
2019-03-25 18:42:01 +00:00
|
|
|
_dropbearkey()
|
|
|
|
{
|
|
|
|
/usr/bin/dropbearkey "$@" 0<&- 1>&- 2>&-
|
|
|
|
}
|
|
|
|
|
|
|
|
# $1 - host key file name
|
|
|
|
hk_verify()
|
|
|
|
{
|
|
|
|
[ -f "$1" ] || return 1
|
|
|
|
[ -s "$1" ] || return 2
|
|
|
|
_dropbearkey -y -f "$1" || return 3
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
# $1 - hk_verify() return code
|
|
|
|
hk_errmsg()
|
|
|
|
{
|
|
|
|
case "$1" in
|
|
|
|
0) ;;
|
|
|
|
1) echo "file does not exist" ;;
|
|
|
|
2) echo "file has zero length" ;;
|
|
|
|
3) echo "file is not valid host key or not supported" ;;
|
|
|
|
*) echo "unknown error" ;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
# $1 - config option
|
|
|
|
# $2 - host key file name
|
|
|
|
hk_config()
|
|
|
|
{
|
|
|
|
local x m
|
|
|
|
hk_verify "$2"; x=$?
|
|
|
|
case "$x" in
|
|
|
|
0) procd_append_param command -r "$2"
|
|
|
|
;;
|
|
|
|
*) m=$(hk_errmsg "$x")
|
|
|
|
logger -t "${NAME}" -p daemon.warn \
|
|
|
|
"option '$1', value '$2': $m, skipping"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
# $1 - host key file name
|
|
|
|
hk_config__keyfile()
|
|
|
|
{
|
|
|
|
hk_config 'keyfile' "$1"
|
|
|
|
}
|
|
|
|
|
2019-03-25 18:59:47 +00:00
|
|
|
hk_generate_as_needed()
|
|
|
|
{
|
|
|
|
local kdir kgen ktype tdir kcount tfile
|
|
|
|
kdir='/etc/dropbear'
|
|
|
|
|
|
|
|
kgen=''
|
2020-04-12 22:24:28 +00:00
|
|
|
for ktype in ed25519 ecdsa rsa; do
|
2019-03-25 18:59:47 +00:00
|
|
|
hk_verify "${kdir}/dropbear_${ktype}_host_key" && continue
|
|
|
|
|
|
|
|
kgen="${kgen} ${ktype}"
|
|
|
|
done
|
|
|
|
|
|
|
|
[ -z "${kgen}" ] && return
|
|
|
|
|
|
|
|
tdir=$(mktemp -d); chmod 0700 "${tdir}"
|
|
|
|
|
|
|
|
kcount=0
|
|
|
|
for ktype in ${kgen}; do
|
|
|
|
tfile="${tdir}/dropbear_${ktype}_host_key"
|
|
|
|
|
|
|
|
if ! _dropbearkey -t ${ktype} -f "${tfile}"; then
|
|
|
|
# unsupported key type
|
|
|
|
rm -f "${tfile}"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
kcount=$((kcount+1))
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ ${kcount} -ne 0 ]; then
|
|
|
|
mkdir -p "${kdir}"; chmod 0700 "${kdir}"; chown root "${kdir}"
|
|
|
|
mv -f "${tdir}/"* "${kdir}/"
|
|
|
|
fi
|
|
|
|
|
|
|
|
rm -rf "${tdir}"
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
2019-01-12 18:14:22 +00:00
|
|
|
uci_load_validate dropbear dropbear "$1" "$2" \
|
2013-11-13 10:49:41 +00:00
|
|
|
'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' \
|
2019-03-25 18:42:01 +00:00
|
|
|
'keyfile:list(file)' \
|
2013-11-13 10:49:41 +00:00
|
|
|
'BannerFile:file' \
|
2019-03-25 18:42:09 +00:00
|
|
|
'Port:port:22' \
|
2014-03-29 17:05:35 +00:00
|
|
|
'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()
|
|
|
|
{
|
2019-01-12 18:14:22 +00:00
|
|
|
local ipaddrs
|
2009-08-03 22:19:51 +00:00
|
|
|
|
2019-01-12 18:14:22 +00:00
|
|
|
[ "$2" = 0 ] || {
|
2013-11-13 10:49:41 +00:00
|
|
|
echo "validation failed"
|
|
|
|
return 1
|
|
|
|
}
|
2009-08-03 22:19:51 +00:00
|
|
|
|
2014-10-09 07:16:35 +00:00
|
|
|
[ -n "${Interface}" ] && {
|
2018-12-18 18:40:31 +00:00
|
|
|
[ -n "${BOOT}" ] && return 0
|
|
|
|
|
2014-10-09 07:16:35 +00:00
|
|
|
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
|
2019-03-25 18:42:01 +00:00
|
|
|
if [ -n "${rsakeyfile}" ]; then
|
|
|
|
logger -t ${NAME} -p daemon.warn \
|
|
|
|
"option 'rsakeyfile' is considered to be deprecated and" \
|
|
|
|
"will be removed in future releases, use 'keyfile' instead"
|
|
|
|
hk_config 'rsakeyfile' "${rsakeyfile}"
|
|
|
|
fi
|
|
|
|
config_list_foreach "$1" "keyfile" hk_config__keyfile
|
2013-11-13 10:49:41 +00:00
|
|
|
[ -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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-12-18 18:40:31 +00:00
|
|
|
boot()
|
|
|
|
{
|
|
|
|
BOOT=1
|
|
|
|
start "$@"
|
|
|
|
}
|
|
|
|
|
2012-12-22 18:56:13 +00:00
|
|
|
start_service()
|
2009-08-03 22:19:51 +00:00
|
|
|
{
|
2019-03-25 18:59:47 +00:00
|
|
|
hk_generate_as_needed
|
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}"
|
2019-01-12 18:14:22 +00:00
|
|
|
config_foreach validate_section_dropbear dropbear dropbear_instance
|
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
|
2020-06-30 16:22:10 +00:00
|
|
|
pid=$(cut -d ' ' -f 4 "/proc/${pid}/stat")
|
2009-08-03 22:19:51 +00:00
|
|
|
[ "${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
|
2020-06-30 16:22:10 +00:00
|
|
|
for server in $(cat /var/run/${NAME}.*.pid)
|
2009-08-03 22:19:51 +00:00
|
|
|
do
|
|
|
|
append ignore "${server}"
|
|
|
|
done
|
|
|
|
|
|
|
|
# get all running pids and kill client connections
|
|
|
|
local skip
|
2020-06-30 16:22:10 +00:00
|
|
|
for pid in $(pidof "${NAME}")
|
2009-08-03 22:19:51 +00:00
|
|
|
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
|
|
|
}
|