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"
17 /usr
/bin
/dropbearkey
"$@" 0<&- 1>&- 2>&-
20 # $1 - host key file name
23 [ -f "$1" ] ||
return 1
24 [ -s "$1" ] ||
return 2
25 _dropbearkey
-y -f "$1" ||
return 3
29 # $1 - hk_verify() return code
34 1) echo "file does not exist" ;;
35 2) echo "file has zero length" ;;
36 3) echo "file is not valid host key or not supported" ;;
37 *) echo "unknown error" ;;
42 # $2 - host key file name
48 0) procd_append_param
command -r "$2"
50 *) m
=$
(hk_errmsg
"$x")
51 logger
-t "${NAME}" -p daemon.warn \
52 "option '$1', value '$2': $m, skipping"
57 # $1 - host key file name
60 hk_config
'keyfile' "$1"
63 hk_generate_as_needed
()
65 local kdir kgen ktype tdir kcount tfile tlock
69 for ktype
in ecdsa rsa
; do
70 hk_verify
"${kdir}/dropbear_${ktype}_host_key" && continue
72 kgen
="${kgen} ${ktype}"
75 [ -z "${kgen}" ] && return
77 tdir
=$
(mktemp
-d); chmod 0700 "${tdir}"
80 for ktype
in ${kgen}; do
81 tfile
="${tdir}/dropbear_${ktype}_host_key"
83 if ! _dropbearkey
-t ${ktype} -f "${tfile}"; then
84 # unsupported key type
92 if [ ${kcount} -ne 0 ]; then
93 tlock
='/tmp/.switch2jffs'
95 mkdir
-p "${kdir}"; chmod 0700 "${kdir}"; chown root "${kdir}"
96 mv -f "${tdir}/"* "${kdir}/"
108 [ -z "$ipaddrs" ] && {
109 procd_append_param
command -p "$port"
113 for addr
in $ipaddrs; do
114 procd_append_param
command -p "$addr:$port"
118 validate_section_dropbear
()
120 uci_validate_section dropbear dropbear
"${1}" \
121 'PasswordAuth:bool:1' \
124 'GatewayPorts:bool:0' \
125 'RootPasswordAuth:bool:1' \
128 'keyfile:list(file)' \
131 'SSHKeepAlive:uinteger:300' \
132 'IdleTimeout:uinteger:0' \
133 'MaxAuthTries:uinteger:3' \
134 'RecvWindowSize:uinteger:0' \
140 local PasswordAuth
enable Interface GatewayPorts \
141 RootPasswordAuth RootLogin rsakeyfile keyfile \
142 BannerFile Port SSHKeepAlive IdleTimeout \
143 MaxAuthTries RecvWindowSize mdns ipaddrs
145 validate_section_dropbear
"${1}" ||
{
146 echo "validation failed"
150 [ -n "${Interface}" ] && {
151 network_get_ipaddrs_all ipaddrs
"${Interface}" ||
{
152 echo "interface ${Interface} has no physdev or physdev has no suitable ip"
157 [ "${enable}" = "0" ] && return 1
158 PIDCOUNT
="$(( ${PIDCOUNT} + 1))"
159 local pid_file
="/var/run/${NAME}.${PIDCOUNT}.pid"
162 procd_set_param
command "$PROG" -F -P "$pid_file"
163 [ "${PasswordAuth}" -eq 0 ] && procd_append_param
command -s
164 [ "${GatewayPorts}" -eq 1 ] && procd_append_param
command -a
165 [ "${RootPasswordAuth}" -eq 0 ] && procd_append_param
command -g
166 [ "${RootLogin}" -eq 0 ] && procd_append_param
command -w
167 if [ -n "${rsakeyfile}" ]; then
168 logger
-t ${NAME} -p daemon.warn \
169 "option 'rsakeyfile' is considered to be deprecated and" \
170 "will be removed in future releases, use 'keyfile' instead"
171 hk_config
'rsakeyfile' "${rsakeyfile}"
173 config_list_foreach
"$1" "keyfile" hk_config__keyfile
174 [ -n "${BannerFile}" ] && procd_append_param
command -b "${BannerFile}"
175 append_ports
"${ipaddrs}" "${Port}"
176 [ "${IdleTimeout}" -ne 0 ] && procd_append_param
command -I "${IdleTimeout}"
177 [ "${SSHKeepAlive}" -ne 0 ] && procd_append_param
command -K "${SSHKeepAlive}"
178 [ "${MaxAuthTries}" -ne 0 ] && procd_append_param
command -T "${MaxAuthTries}"
179 [ "${RecvWindowSize}" -gt 0 -a "${RecvWindowSize}" -le 1048576 ] && \
180 procd_append_param
command -W "${RecvWindowSize}"
181 [ "${mdns}" -ne 0 ] && procd_add_mdns
"ssh" "tcp" "$Port" "daemon=dropbear"
182 procd_set_param respawn
188 config_get interface
"$1" Interface
189 config_get
enable "$1" enable 1
191 [ "${enable}" = "1" ] && interfaces=" ${interface} ${interfaces}"
196 hk_generate_as_needed
199 .
/lib
/functions
/network.sh
201 config_load
"${NAME}"
202 config_foreach dropbear_instance dropbear
209 procd_add_config_trigger
"config.change" "dropbear" /etc
/init.d
/dropbear reload
211 config_load
"${NAME}"
212 config_foreach load_interfaces dropbear
214 [ -n "${interfaces}" ] && {
215 for n
in $interfaces ; do
216 procd_add_interface_trigger
"interface.*" $n /etc
/init.d
/dropbear reload
220 procd_add_validation validate_section_dropbear
224 # close all open connections
234 # if this script is run from inside a client session, then ignore that session
236 while [ "${pid}" -ne 0 ]
238 # get parent process id
239 pid
=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
240 [ "${pid}" -eq 0 ] && break
242 # check if client connection
243 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
244 append ignore
"${pid}"
249 # get all server pids that should be ignored
250 for server
in `cat /var/run/${NAME}.*.pid`
252 append ignore
"${server}"
255 # get all running pids and kill client connections
257 for pid
in `pidof "${NAME}"`
259 # check if correct program, otherwise process next pid
260 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" ||
{
264 # check if pid should be ignored (servers, ourself)
266 for server
in ${ignore}
268 if [ "${pid}" = "${server}" ]
274 [ "${skip}" -ne 0 ] && continue
277 echo "${initscript}: Killing ${pid}..."