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
|
|
|
|
2024-01-09 00:40:02 +00:00
|
|
|
# most of time real_stat() will be failing
|
|
|
|
# due to missing "stat" binary (by default)
|
|
|
|
real_stat() { env stat -L "$@" 2>/dev/null ; }
|
|
|
|
dumb_stat() { ls -Ldln "$1" | tr -s '\t ' ' ' ; }
|
|
|
|
stat_perm() { real_stat -c '%A' "$1" || dumb_stat "$1" | cut -d ' ' -f 1 ; }
|
|
|
|
stat_owner() { real_stat -c '%u' "$1" || dumb_stat "$1" | cut -d ' ' -f 3 ; }
|
|
|
|
|
2019-03-25 18:42:01 +00:00
|
|
|
_dropbearkey()
|
|
|
|
{
|
2024-01-09 00:40:02 +00:00
|
|
|
/usr/bin/dropbearkey "$@" </dev/null >/dev/null 2>&1
|
2019-03-25 18:42:01 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 00:40:02 +00:00
|
|
|
# $1 - file name (host key or config)
|
|
|
|
file_verify()
|
2019-03-25 18:42:01 +00:00
|
|
|
{
|
|
|
|
[ -f "$1" ] || return 1
|
2024-01-09 00:40:02 +00:00
|
|
|
# checking file ownership
|
|
|
|
[ "$(stat_owner "$1")" = "0" ] || {
|
|
|
|
chown 0 "$1"
|
|
|
|
[ "$(stat_owner "$1")" = "0" ] || return 2
|
|
|
|
}
|
|
|
|
# checking file permissions
|
|
|
|
[ "$(stat_perm "$1")" = "-rw-------" ] || {
|
|
|
|
chmod 0600 "$1"
|
|
|
|
[ "$(stat_perm "$1")" = "-rw-------" ] || return 3
|
|
|
|
}
|
|
|
|
# file is host key or not?
|
|
|
|
# if $2 is empty string - file is "host key"
|
|
|
|
# if $2 is non-empty string - file is "config"
|
|
|
|
[ -z "$2" ] || return 0
|
|
|
|
# checking file contents (finally)
|
|
|
|
[ -s "$1" ] || return 4
|
|
|
|
_dropbearkey -y -f "$1" || return 5
|
2019-03-25 18:42:01 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2024-01-09 00:40:02 +00:00
|
|
|
# $1 - file_verify() return code
|
|
|
|
file_errmsg()
|
2019-03-25 18:42:01 +00:00
|
|
|
{
|
|
|
|
case "$1" in
|
|
|
|
0) ;;
|
|
|
|
1) echo "file does not exist" ;;
|
2024-01-09 00:40:02 +00:00
|
|
|
2) echo "file has wrong owner (must be owned by root)" ;;
|
|
|
|
3) echo "file has wrong permissions (must not have group/other write bit)" ;;
|
|
|
|
4) echo "file has zero length" ;;
|
|
|
|
5) echo "file is not valid host key or not supported" ;;
|
2019-03-25 18:42:01 +00:00
|
|
|
*) echo "unknown error" ;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
# $1 - config option
|
|
|
|
# $2 - host key file name
|
|
|
|
hk_config()
|
|
|
|
{
|
|
|
|
local x m
|
2024-01-09 00:40:02 +00:00
|
|
|
file_verify "$2" ; x=$?
|
|
|
|
if [ "$x" = 0 ] ; then
|
|
|
|
procd_append_param command -r "$2"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
m=$(file_errmsg "$x")
|
|
|
|
logger -s -t "${NAME}" -p daemon.warn \
|
|
|
|
"Option '$1', skipping '$2': $m"
|
2019-03-25 18:42:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# $1 - host key file name
|
2024-01-09 00:40:02 +00:00
|
|
|
hk_config__keyfile() { hk_config keyfile "$1" ; }
|
|
|
|
|
|
|
|
ktype_all='ed25519 ecdsa rsa'
|
2019-03-25 18:42:01 +00:00
|
|
|
|
2019-03-25 18:59:47 +00:00
|
|
|
hk_generate_as_needed()
|
|
|
|
{
|
2024-01-09 00:40:02 +00:00
|
|
|
local hk_cfg_dir kgen ktype kfile hk_tmp_dir
|
|
|
|
hk_cfg_dir='/etc/dropbear'
|
|
|
|
|
|
|
|
[ -d "${hk_cfg_dir}" ] || mkdir -p "${hk_cfg_dir}"
|
2019-03-25 18:59:47 +00:00
|
|
|
|
2024-01-09 00:40:02 +00:00
|
|
|
kgen=
|
|
|
|
for ktype in ${ktype_all} ; do
|
|
|
|
kfile="${hk_cfg_dir}/dropbear_${ktype}_host_key"
|
2019-03-25 18:59:47 +00:00
|
|
|
|
2024-01-09 00:40:02 +00:00
|
|
|
if file_verify "${kfile}" ; then continue ; fi
|
|
|
|
|
|
|
|
kgen="${kgen}${kgen:+ }${ktype}"
|
2019-03-25 18:59:47 +00:00
|
|
|
done
|
|
|
|
|
2024-01-09 00:40:02 +00:00
|
|
|
# all keys are sane?
|
|
|
|
[ -n "${kgen}" ] || return 0
|
|
|
|
|
|
|
|
hk_tmp_dir=$(mktemp -d)
|
|
|
|
# system in bad state?
|
|
|
|
[ -n "${hk_tmp_dir}" ] || return 1
|
2019-03-25 18:59:47 +00:00
|
|
|
|
2024-01-09 00:40:02 +00:00
|
|
|
chmod 0700 "${hk_tmp_dir}"
|
2019-03-25 18:59:47 +00:00
|
|
|
|
2024-01-09 00:40:02 +00:00
|
|
|
for ktype in ${kgen} ; do
|
|
|
|
kfile="${hk_tmp_dir}/dropbear_${ktype}_host_key"
|
2019-03-25 18:59:47 +00:00
|
|
|
|
2024-01-09 00:40:02 +00:00
|
|
|
if ! _dropbearkey -t ${ktype} -f "${kfile}" ; then
|
2019-03-25 18:59:47 +00:00
|
|
|
# unsupported key type
|
2024-01-09 00:40:02 +00:00
|
|
|
rm -f "${kfile}"
|
2019-03-25 18:59:47 +00:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2024-01-09 00:40:02 +00:00
|
|
|
chmod 0600 "${kfile}"
|
|
|
|
done
|
|
|
|
|
|
|
|
kgen=
|
|
|
|
for ktype in ${ktype_all} ; do
|
|
|
|
kfile="${hk_tmp_dir}/dropbear_${ktype}_host_key"
|
|
|
|
|
|
|
|
[ -s "${kfile}" ] || continue
|
|
|
|
|
|
|
|
kgen="${kgen}${kgen:+ }${ktype}"
|
2019-03-25 18:59:47 +00:00
|
|
|
done
|
|
|
|
|
2024-01-09 00:40:02 +00:00
|
|
|
if [ -n "${kgen}" ] ; then
|
|
|
|
for ktype in ${kgen} ; do
|
|
|
|
kfile="${hk_tmp_dir}/dropbear_${ktype}_host_key"
|
|
|
|
[ -s "${kfile}" ] || continue
|
|
|
|
mv -f "${kfile}" "${hk_cfg_dir}/"
|
|
|
|
done
|
2019-03-25 18:59:47 +00:00
|
|
|
fi
|
|
|
|
|
2024-01-09 00:40:02 +00:00
|
|
|
rm -rf "${hk_tmp_dir}"
|
|
|
|
|
|
|
|
# cleanup empty files
|
|
|
|
for ktype in ${ktype_all} ; do
|
|
|
|
kfile="${hk_cfg_dir}/dropbear_${ktype}_host_key"
|
|
|
|
|
|
|
|
[ -s "${kfile}" ] || rm -f "${kfile}"
|
|
|
|
done
|
2019-03-25 18:59:47 +00:00
|
|
|
}
|
|
|
|
|
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' \
|
2023-03-29 12:15:13 +00:00
|
|
|
'ForceCommand:string' \
|
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
|
|
|
|
2023-12-28 22:16:02 +00:00
|
|
|
# Increase default receive window size to increase
|
|
|
|
# throughput on high latency links
|
|
|
|
if [ "${RecvWindowSize}" -eq "0" ]; then
|
|
|
|
RecvWindowSize="262144"
|
|
|
|
fi
|
|
|
|
|
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
|
2023-03-29 12:15:13 +00:00
|
|
|
[ -n "${ForceCommand}" ] && procd_append_param command -c "${ForceCommand}"
|
2013-11-13 10:49:41 +00:00
|
|
|
[ "${RootPasswordAuth}" -eq 0 ] && procd_append_param command -g
|
|
|
|
[ "${RootLogin}" -eq 0 ] && procd_append_param command -w
|
2024-01-09 00:40:02 +00:00
|
|
|
config_list_foreach "$1" 'keyfile' hk_config__keyfile
|
2019-03-25 18:42:01 +00:00
|
|
|
if [ -n "${rsakeyfile}" ]; then
|
2024-01-09 00:40:02 +00:00
|
|
|
logger -s -t "${NAME}" -p daemon.crit \
|
|
|
|
"Option 'rsakeyfile' is considered to be DEPRECATED and will be REMOVED in future releases, use 'keyfile' list instead"
|
|
|
|
sed -i.before-upgrade -E -e 's/option(\s+)rsakeyfile/list keyfile/' \
|
|
|
|
"/etc/config/${NAME}"
|
|
|
|
logger -s -t "${NAME}" -p daemon.crit \
|
|
|
|
"Auto-transition 'option rsakeyfile' => 'list keyfile' in /etc/config/${NAME} is done, please verify your configuration"
|
2019-03-25 18:42:01 +00:00
|
|
|
hk_config 'rsakeyfile' "${rsakeyfile}"
|
|
|
|
fi
|
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
|
2024-01-09 00:40:02 +00:00
|
|
|
file_verify /etc/dropbear/authorized_keys config
|
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
|
|
|
}
|