1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2006-2009 OpenWrt.org
3 # Copyright (C) 2006 Carlos Sobrinho
6 PROG
=/usr
/sbin
/dropbear
9 EXTRA_COMMANDS
="killclients"
10 EXTRA_HELP
=" killclients Kill ${NAME} processes except servers and yourself"
16 # check if section is enabled (default)
18 config_get_bool enabled
"${section}" enable 1
19 [ "${enabled}" -eq 0 ] && return 1
23 config_get_bool verbosed
"${section}" verbose
0
25 # increase pid file count to handle multiple instances correctly
26 PIDCOUNT
="$(( ${PIDCOUNT} + 1))"
29 # A) password authentication
32 config_get_bool passauth
"${section}" PasswordAuth
1
33 [ "${passauth}" -eq 0 ] && nopasswd
=1
36 config_get port
"${section}" Port
38 # concatenate parameters
40 args
="${nopasswd:+-s }${port:+-p ${port}} -P /var/run/${NAME}.${PIDCOUNT}.pid"
42 # execute program and return its exit code
43 [ "${verbosed}" -ne 0 ] && echo "${initscript}: section ${section} starting ${PROG} ${args}"
50 for keytype
in rsa dss
; do
52 key
=dropbear
/dropbear_
${keytype}_host_key
53 [ -f /tmp
/$key -o -s /etc
/$key ] ||
{
54 # generate missing keys
55 mkdir
-p /tmp
/dropbear
56 [ -x /usr
/bin
/dropbearkey
] && {
57 /usr
/bin
/dropbearkey
-t $keytype -f /tmp
/$key 2>&- >&- && exec /etc
/rc.common
"$initscript" start
63 lock
/tmp
/.switch2jffs
64 mkdir
-p /etc
/dropbear
65 mv /tmp
/dropbear
/dropbear_
* /etc
/dropbear
/
66 lock
-u /tmp
/.switch2jffs
67 chown root
/etc
/dropbear
68 chmod 0700 /etc
/dropbear
73 [ -s /etc
/dropbear
/dropbear_rsa_host_key
-a \
74 -s /etc
/dropbear
/dropbear_dss_host_key
] || keygen
77 config_foreach dropbear_start dropbear
82 # killing all server processes
84 for pidfile
in `ls /var/run/${NAME}.*.pid`
86 start-stop-daemon
-K -s KILL
-p "${pidfile}" -n "${NAME}" >/dev
/null
89 [ -z "${pidfile}" ] && echo "${initscript}: no pid files, if you get problems with start then try killclients"
98 # if this script is run from inside a client session, then ignore that session
100 while [ "${pid}" -ne 0 ]
102 # get parent process id
103 pid
=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
104 [ "${pid}" -eq 0 ] && break
106 # check if client connection
107 ps |
grep -e "^[ ]*${pid} " |
grep "${PROG}" >/dev
/null
110 append ignore
"${pid}"
115 # get all server pids that should be ignored
116 for server
in `cat /var/run/${NAME}.*.pid`
118 append ignore
"${server}"
121 # get all running pids and kill client connections
123 for pid
in `pidof "${NAME}"`
125 # check if correct program
126 ps |
grep -e "^[ ]*${pid} " |
grep "${PROG}" >/dev
/null
127 [ $?
-ne 0 ] && continue
129 # check if pid should be ignored (servers, ourself)
131 for server
in ${ignore}
133 if [ "${pid}" == "${server}" ]
139 [ "${skip}" -ne 0 ] && continue
142 echo "${initscript}: Killing ${pid}..."