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 config_get val "${section}" rsakeyfile
57 [ -f "${val}" ] && append args "-r ${val}"
58 config_get val "${section}" dsskeyfile
59 [ -f "${val}" ] && append args "-d ${val}"
61 # execute program and return its exit code
62 [ "${verbosed}" -ne 0 ] && echo "${initscript}: section ${section} starting ${PROG} ${args}"
69 for keytype in rsa dss; do
71 key=dropbear/dropbear_${keytype}_host_key
72 [ -f /tmp/$key -o -s /etc/$key ] || {
73 # generate missing keys
74 mkdir -p /tmp/dropbear
75 [ -x /usr/bin/dropbearkey ] && {
76 /usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start
82 lock /tmp/.switch2jffs
83 mkdir -p /etc/dropbear
84 mv /tmp/dropbear/dropbear_* /etc/dropbear/
85 lock -u /tmp/.switch2jffs
86 chown root /etc/dropbear
87 chmod 0700 /etc/dropbear
92 [ -s /etc/dropbear/dropbear_rsa_host_key -a \
93 -s /etc/dropbear/dropbear_dss_host_key ] || keygen
98 config_foreach dropbear_start dropbear
103 # killing all server processes
105 for pidfile in `ls /var/run/${NAME}.*.pid`
107 start-stop-daemon -q -K -s KILL -p "${pidfile}" -n "${NAME}"
110 [ -z "${pidfile}" ] && echo "${initscript}: no pid files, if you get problems with start then try killclients"
119 # if this script is run from inside a client session, then ignore that session
121 while [ "${pid}" -ne 0 ]
123 # get parent process id
124 pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
125 [ "${pid}" -eq 0 ] && break
127 # check if client connection
128 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
129 append ignore "${pid}"
134 # get all server pids that should be ignored
135 for server in `cat /var/run/${NAME}.*.pid`
137 append ignore "${server}"
140 # get all running pids and kill client connections
142 for pid in `pidof "${NAME}"`
144 # check if correct program, otherwise process next pid
145 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
149 # check if pid should be ignored (servers, ourself)
151 for server in ${ignore}
153 if [ "${pid}" == "${server}" ]
159 [ "${skip}" -ne 0 ] && continue
162 echo "${initscript}: Killing ${pid}..."