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"
20 grep -qs "^ *$ifname:" /proc/net/dev || {
21 append args "-p $port"
26 ifconfig "$ifname" | sed -ne '
27 /addr: *fe[89ab][0-9a-f]:/d
28 s/.* addr: *\([0-9a-f:\.]*\).*/\1/p
31 append args "-p $addr:$port"
38 # check if section is enabled (default)
40 config_get_bool enabled "${section}" enable 1
41 [ "${enabled}" -eq 0 ] && return 1
45 config_get_bool verbosed "${section}" verbose 0
47 # increase pid file count to handle multiple instances correctly
48 PIDCOUNT="$(( ${PIDCOUNT} + 1))"
50 # prepare parameters (initialise with pid file)
51 local args="-P /var/run/${NAME}.${PIDCOUNT}.pid"
53 # A) password authentication
54 config_get_bool val "${section}" PasswordAuth 1
55 [ "${val}" -eq 0 ] && append args "-s"
56 # B) listen interface and port
59 config_get interface "${section}" Interface
60 config_get interface "${interface}" ifname "$interface"
61 config_get port "${section}" Port 22
62 append_ports "$interface" "$port"
64 config_get val "${section}" BannerFile
65 [ -f "${val}" ] && append args "-b ${val}"
67 config_get_bool val "${section}" GatewayPorts 0
68 [ "${val}" -eq 1 ] && append args "-a"
69 # E) root password authentication
70 config_get_bool val "${section}" RootPasswordAuth 1
71 [ "${val}" -eq 0 ] && append args "-g"
73 config_get_bool val "${section}" RootLogin 1
74 [ "${val}" -eq 0 ] && append args "-w"
76 config_get val "${section}" rsakeyfile
77 [ -f "${val}" ] && append args "-r ${val}"
78 config_get val "${section}" dsskeyfile
79 [ -f "${val}" ] && append args "-d ${val}"
81 # execute program and return its exit code
82 [ "${verbosed}" -ne 0 ] && echo "${initscript}: section ${section} starting ${PROG} ${args}"
89 for keytype in rsa dss; do
91 key=dropbear/dropbear_${keytype}_host_key
92 [ -f /tmp/$key -o -s /etc/$key ] || {
93 # generate missing keys
94 mkdir -p /tmp/dropbear
95 [ -x /usr/bin/dropbearkey ] && {
96 /usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start
102 lock /tmp/.switch2jffs
103 mkdir -p /etc/dropbear
104 mv /tmp/dropbear/dropbear_* /etc/dropbear/
105 lock -u /tmp/.switch2jffs
106 chown root /etc/dropbear
107 chmod 0700 /etc/dropbear
112 [ -s /etc/dropbear/dropbear_rsa_host_key -a \
113 -s /etc/dropbear/dropbear_dss_host_key ] || keygen
117 config_load "${NAME}"
118 config_foreach dropbear_start dropbear
123 # killing all server processes
125 for pidfile in `ls /var/run/${NAME}.*.pid`
127 start-stop-daemon -q -K -s KILL -p "${pidfile}" -n "${NAME}"
130 [ -z "${pidfile}" ] && echo "${initscript}: no pid files, if you get problems with start then try killclients"
139 # if this script is run from inside a client session, then ignore that session
141 while [ "${pid}" -ne 0 ]
143 # get parent process id
144 pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
145 [ "${pid}" -eq 0 ] && break
147 # check if client connection
148 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
149 append ignore "${pid}"
154 # get all server pids that should be ignored
155 for server in `cat /var/run/${NAME}.*.pid`
157 append ignore "${server}"
160 # get all running pids and kill client connections
162 for pid in `pidof "${NAME}"`
164 # check if correct program, otherwise process next pid
165 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
169 # check if pid should be ignored (servers, ourself)
171 for server in ${ignore}
173 if [ "${pid}" == "${server}" ]
179 [ "${skip}" -ne 0 ] && continue
182 echo "${initscript}: Killing ${pid}..."