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))"
29 # prepare parameters (initialise with pid file)
30 local args
="-P /var/run/${NAME}.${PIDCOUNT}.pid"
32 # A) password authentication
33 config_get_bool val
"${section}" PasswordAuth
1
34 [ "${val}" -eq 0 ] && append args
"-s"
35 # B) listen interface and port
38 config_get interface
"${section}" Interface
39 config_get address
"${interface}" ipaddr
40 config_get val
"${section}" Port
41 val
="${address:+${address}:}${val}"
42 [ -n "${val}" ] && append args
"-p ${val}"
44 config_get val
"${section}" BannerFile
45 [ -f "${val}" ] && append args
"-b ${val}"
47 config_get_bool val
"${section}" GatewayPorts
0
48 [ "${val}" -eq 1 ] && append args
"-a"
49 # E) root password authentication
50 config_get_bool val
"${section}" RootPasswordAuth
1
51 [ "${val}" -eq 0 ] && append args
"-g"
53 config_get_bool val
"${section}" RootLogin
1
54 [ "${val}" -eq 0 ] && append args
"-w"
56 # execute program and return its exit code
57 [ "${verbosed}" -ne 0 ] && echo "${initscript}: section ${section} starting ${PROG} ${args}"
64 for keytype
in rsa dss
; do
66 key
=dropbear
/dropbear_
${keytype}_host_key
67 [ -f /tmp
/$key -o -s /etc
/$key ] ||
{
68 # generate missing keys
69 mkdir
-p /tmp
/dropbear
70 [ -x /usr
/bin
/dropbearkey
] && {
71 /usr
/bin
/dropbearkey
-t $keytype -f /tmp
/$key 2>&- >&- && exec /etc
/rc.common
"$initscript" start
77 lock
/tmp
/.switch2jffs
78 mkdir
-p /etc
/dropbear
79 mv /tmp
/dropbear
/dropbear_
* /etc
/dropbear
/
80 lock
-u /tmp
/.switch2jffs
81 chown root
/etc
/dropbear
82 chmod 0700 /etc
/dropbear
87 [ -s /etc
/dropbear
/dropbear_rsa_host_key
-a \
88 -s /etc
/dropbear
/dropbear_dss_host_key
] || keygen
93 config_foreach dropbear_start dropbear
98 # killing all server processes
100 for pidfile
in `ls /var/run/${NAME}.*.pid`
102 start-stop-daemon
-q -K -s KILL
-p "${pidfile}" -n "${NAME}"
105 [ -z "${pidfile}" ] && echo "${initscript}: no pid files, if you get problems with start then try killclients"
114 # if this script is run from inside a client session, then ignore that session
116 while [ "${pid}" -ne 0 ]
118 # get parent process id
119 pid
=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
120 [ "${pid}" -eq 0 ] && break
122 # check if client connection
123 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
124 append ignore
"${pid}"
129 # get all server pids that should be ignored
130 for server
in `cat /var/run/${NAME}.*.pid`
132 append ignore
"${server}"
135 # get all running pids and kill client connections
137 for pid
in `pidof "${NAME}"`
139 # check if correct program, otherwise process next pid
140 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" ||
{
144 # check if pid should be ignored (servers, ourself)
146 for server
in ${ignore}
148 if [ "${pid}" == "${server}" ]
154 [ "${skip}" -ne 0 ] && continue
157 echo "${initscript}: Killing ${pid}..."