1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2006-2009 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))"
30 # A) password authentication
33 config_get_bool passauth "${section}" PasswordAuth 1
34 [ "${passauth}" -eq 0 ] && nopasswd=1
37 config_get port "${section}" Port
40 config_get bannerfile ${section} BannerFile
41 [ -f $bannerfile ] || bannerfile=''
44 config_get_bool gatewayports "${section}" GatewayPorts 0
45 [ "${gatewayports}" -eq 1 ] || gatewayports=''
46 # concatenate parameters
48 args="${nopasswd:+-s }${port:+-p ${port} }${bannerfile:+-b $bannerfile }${gatewayports:+-a }-P /var/run/${NAME}.${PIDCOUNT}.pid"
50 # execute program and return its exit code
51 [ "${verbosed}" -ne 0 ] && echo "${initscript}: section ${section} starting ${PROG} ${args}"
58 for keytype in rsa dss; do
60 key=dropbear/dropbear_${keytype}_host_key
61 [ -f /tmp/$key -o -s /etc/$key ] || {
62 # generate missing keys
63 mkdir -p /tmp/dropbear
64 [ -x /usr/bin/dropbearkey ] && {
65 /usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start
71 lock /tmp/.switch2jffs
72 mkdir -p /etc/dropbear
73 mv /tmp/dropbear/dropbear_* /etc/dropbear/
74 lock -u /tmp/.switch2jffs
75 chown root /etc/dropbear
76 chmod 0700 /etc/dropbear
81 [ -s /etc/dropbear/dropbear_rsa_host_key -a \
82 -s /etc/dropbear/dropbear_dss_host_key ] || keygen
85 config_foreach dropbear_start dropbear
90 # killing all server processes
92 for pidfile in `ls /var/run/${NAME}.*.pid`
94 start-stop-daemon -K -s KILL -p "${pidfile}" -n "${NAME}" >/dev/null
97 [ -z "${pidfile}" ] && echo "${initscript}: no pid files, if you get problems with start then try killclients"
106 # if this script is run from inside a client session, then ignore that session
108 while [ "${pid}" -ne 0 ]
110 # get parent process id
111 pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
112 [ "${pid}" -eq 0 ] && break
114 # check if client connection
115 ps | grep -e "^[ ]*${pid} " | grep "${PROG}" >/dev/null
118 append ignore "${pid}"
123 # get all server pids that should be ignored
124 for server in `cat /var/run/${NAME}.*.pid`
126 append ignore "${server}"
129 # get all running pids and kill client connections
131 for pid in `pidof "${NAME}"`
133 # check if correct program
134 ps | grep -e "^[ ]*${pid} " | grep "${PROG}" >/dev/null
135 [ $? -ne 0 ] && continue
137 # check if pid should be ignored (servers, ourself)
139 for server in ${ignore}
141 if [ "${pid}" == "${server}" ]
147 [ "${skip}" -ne 0 ] && continue
150 echo "${initscript}: Killing ${pid}..."