1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2006-2010 OpenWrt.org
3 # Copyright (C) 2006 Carlos Sobrinho
11 PROG
=/usr
/sbin
/dropbear
13 EXTRA_COMMANDS
="killclients"
14 EXTRA_HELP
=" killclients Kill ${NAME} processes except servers and yourself"
23 grep -qs "^ *$ifname:" /proc
/net
/dev ||
{
24 append args
"-p $port"
29 ifconfig
"$ifname" |
sed -ne '
30 /addr: *fe[89ab][0-9a-f]:/d
31 s/.* addr: *\([0-9a-f:\.]*\).*/\1/p
34 append args
"-p $addr:$port"
41 # check if section is enabled (default)
43 config_get_bool enabled
"${section}" enable 1
44 [ "${enabled}" -eq 0 ] && return 1
48 config_get_bool verbosed
"${section}" verbose
0
50 # increase pid file count to handle multiple instances correctly
51 PIDCOUNT
="$(( ${PIDCOUNT} + 1))"
53 # prepare parameters (initialise with pid file)
54 local pid_file
="/var/run/${NAME}.${PIDCOUNT}.pid"
55 local args
="-P $pid_file"
57 # A) password authentication
58 config_get_bool val
"${section}" PasswordAuth
1
59 [ "${val}" -eq 0 ] && append args
"-s"
60 # B) listen interface and port
63 config_get interface
"${section}" Interface
64 config_get interface
"${interface}" ifname
"$interface"
65 config_get port
"${section}" Port
22
66 append_ports
"$interface" "$port"
68 config_get val
"${section}" BannerFile
69 [ -f "${val}" ] && append args
"-b ${val}"
71 config_get_bool val
"${section}" GatewayPorts
0
72 [ "${val}" -eq 1 ] && append args
"-a"
73 # E) root password authentication
74 config_get_bool val
"${section}" RootPasswordAuth
1
75 [ "${val}" -eq 0 ] && append args
"-g"
77 config_get_bool val
"${section}" RootLogin
1
78 [ "${val}" -eq 0 ] && append args
"-w"
80 config_get val
"${section}" rsakeyfile
81 [ -f "${val}" ] && append args
"-r ${val}"
82 config_get val
"${section}" dsskeyfile
83 [ -f "${val}" ] && append args
"-d ${val}"
85 # execute program and return its exit code
86 [ "${verbosed}" -ne 0 ] && echo "${initscript}: section ${section} starting ${PROG} ${args}"
87 SERVICE_PID_FILE
="$pid_file" service_start
${PROG} ${args}
92 for keytype
in rsa dss
; do
94 key
=dropbear
/dropbear_
${keytype}_host_key
95 [ -f /tmp
/$key -o -s /etc
/$key ] ||
{
96 # generate missing keys
97 mkdir
-p /tmp
/dropbear
98 [ -x /usr
/bin
/dropbearkey
] && {
99 /usr
/bin
/dropbearkey
-t $keytype -f /tmp
/$key 2>&- >&- && exec /etc
/rc.common
"$initscript" start
105 lock
/tmp
/.switch2jffs
106 mkdir
-p /etc
/dropbear
107 mv /tmp
/dropbear
/dropbear_
* /etc
/dropbear
/
108 lock
-u /tmp
/.switch2jffs
109 chown root
/etc
/dropbear
110 chmod 0700 /etc
/dropbear
115 [ -s /etc
/dropbear
/dropbear_rsa_host_key
-a \
116 -s /etc
/dropbear
/dropbear_dss_host_key
] || keygen
120 config_load
"${NAME}"
121 config_foreach dropbear_start dropbear
126 local pid_file pid_files
128 pid_files
=`ls /var/run/${NAME}.*.pid 2>/dev/null`
130 [ -z "$pid_files" ] && return 1
132 for pid_file
in $pid_files; do
133 SERVICE_PID_FILE
="$pid_file" service_stop
${PROG} && {
145 # if this script is run from inside a client session, then ignore that session
147 while [ "${pid}" -ne 0 ]
149 # get parent process id
150 pid
=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
151 [ "${pid}" -eq 0 ] && break
153 # check if client connection
154 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
155 append ignore
"${pid}"
160 # get all server pids that should be ignored
161 for server
in `cat /var/run/${NAME}.*.pid`
163 append ignore
"${server}"
166 # get all running pids and kill client connections
168 for pid
in `pidof "${NAME}"`
170 # check if correct program, otherwise process next pid
171 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" ||
{
175 # check if pid should be ignored (servers, ourself)
177 for server
in ${ignore}
179 if [ "${pid}" == "${server}" ]
185 [ "${skip}" -ne 0 ] && continue
188 echo "${initscript}: Killing ${pid}..."