1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2006-2010 OpenWrt.org
3 # Copyright (C) 2006 Carlos Sobrinho
6 PROG
=/usr
/sbin
/dropbear
10 EXTRA_COMMANDS
="killclients"
11 EXTRA_HELP
=" killclients Kill ${NAME} processes except servers and yourself"
17 # check if section is enabled (default)
19 config_get_bool enabled
"${section}" enable 1
20 [ "${enabled}" -eq 0 ] && return 1
24 config_get_bool verbosed
"${section}" verbose
0
26 # increase pid file count to handle multiple instances correctly
27 PIDCOUNT
="$(( ${PIDCOUNT} + 1))"
30 # A) password authentication
33 config_get_bool passauth
"${section}" PasswordAuth
1
34 [ "${passauth}" -eq 0 ] && nopasswd
=1
35 # B) listen interface and port
39 config_get port
"${section}" Port
40 config_get interface
"${section}" Interface
41 config_get address
"${interface}" ipaddr
42 port
="${address:+${address}:}${port}"
45 config_get bannerfile
"${section}" BannerFile
46 [ -f "$bannerfile" ] || bannerfile
=''
49 config_get_bool gatewayports
"${section}" GatewayPorts
0
50 [ "${gatewayports}" -eq 1 ] || gatewayports
=''
51 # E) root password authentication
54 config_get_bool rootpassauth
"${section}" RootPasswordAuth
1
55 [ "${rootpassauth}" -eq 0 ] && norootpasswd
=1
56 local rootloginallowed
58 config_get_bool rootloginallowed
"${section}" RootLogin
1
59 [ "${rootloginallowed}" -eq 0 ] && norootlogin
=1
60 # concatenate parameters
62 args
="${nopasswd:+-s }${norootpasswd:+-g }${norootlogin:+-w }${port:+-p ${port} }${bannerfile:+-b $bannerfile }${gatewayports:+-a }-P /var/run/${NAME}.${PIDCOUNT}.pid"
64 # execute program and return its exit code
65 [ "${verbosed}" -ne 0 ] && echo "${initscript}: section ${section} starting ${PROG} ${args}"
72 for keytype
in rsa dss
; do
74 key
=dropbear
/dropbear_
${keytype}_host_key
75 [ -f /tmp
/$key -o -s /etc
/$key ] ||
{
76 # generate missing keys
77 mkdir
-p /tmp
/dropbear
78 [ -x /usr
/bin
/dropbearkey
] && {
79 /usr
/bin
/dropbearkey
-t $keytype -f /tmp
/$key 2>&- >&- && exec /etc
/rc.common
"$initscript" start
85 lock
/tmp
/.switch2jffs
86 mkdir
-p /etc
/dropbear
87 mv /tmp
/dropbear
/dropbear_
* /etc
/dropbear
/
88 lock
-u /tmp
/.switch2jffs
89 chown root
/etc
/dropbear
90 chmod 0700 /etc
/dropbear
95 [ -s /etc
/dropbear
/dropbear_rsa_host_key
-a \
96 -s /etc
/dropbear
/dropbear_dss_host_key
] || keygen
100 config_load
"${NAME}"
101 config_foreach dropbear_start dropbear
106 # killing all server processes
108 for pidfile
in `ls /var/run/${NAME}.*.pid`
110 start-stop-daemon
-q -K -s KILL
-p "${pidfile}" -n "${NAME}"
113 [ -z "${pidfile}" ] && echo "${initscript}: no pid files, if you get problems with start then try killclients"
122 # if this script is run from inside a client session, then ignore that session
124 while [ "${pid}" -ne 0 ]
126 # get parent process id
127 pid
=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
128 [ "${pid}" -eq 0 ] && break
130 # check if client connection
131 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
132 append ignore
"${pid}"
137 # get all server pids that should be ignored
138 for server
in `cat /var/run/${NAME}.*.pid`
140 append ignore
"${server}"
143 # get all running pids and kill client connections
145 for pid
in `pidof "${NAME}"`
147 # check if correct program, otherwise process next pid
148 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" ||
{
152 # check if pid should be ignored (servers, ourself)
154 for server
in ${ignore}
156 if [ "${pid}" == "${server}" ]
162 [ "${skip}" -ne 0 ] && continue
165 echo "${initscript}: Killing ${pid}..."