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))"
30 # A) password authentication
33 config_get_bool passauth "${section}" PasswordAuth 1
34 [ "${passauth}" -eq 0 ] && nopasswd=1
35 # B) listen interface and port
39 config_get port "${section}" Port
40 config_get interface "${section}" Interface
41 config_get address "${interface}" ipaddr
42 port="${address:+${address}:}${port}"
45 config_get bannerfile "${section}" BannerFile
46 [ -f "$bannerfile" ] || bannerfile=''
49 config_get_bool gatewayports "${section}" GatewayPorts 0
50 [ "${gatewayports}" -eq 1 ] || gatewayports=''
51 # E) root password authentication
54 config_get_bool rootpassauth "${section}" RootPasswordAuth 1
55 [ "${rootpassauth}" -eq 0 ] && norootpasswd=1
56 # concatenate parameters
58 args="${nopasswd:+-s }${norootpasswd:+-g }${port:+-p ${port} }${bannerfile:+-b $bannerfile }${gatewayports:+-a }-P /var/run/${NAME}.${PIDCOUNT}.pid"
60 # execute program and return its exit code
61 [ "${verbosed}" -ne 0 ] && echo "${initscript}: section ${section} starting ${PROG} ${args}"
68 for keytype in rsa dss; do
70 key=dropbear/dropbear_${keytype}_host_key
71 [ -f /tmp/$key -o -s /etc/$key ] || {
72 # generate missing keys
73 mkdir -p /tmp/dropbear
74 [ -x /usr/bin/dropbearkey ] && {
75 /usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start
81 lock /tmp/.switch2jffs
82 mkdir -p /etc/dropbear
83 mv /tmp/dropbear/dropbear_* /etc/dropbear/
84 lock -u /tmp/.switch2jffs
85 chown root /etc/dropbear
86 chmod 0700 /etc/dropbear
91 [ -s /etc/dropbear/dropbear_rsa_host_key -a \
92 -s /etc/dropbear/dropbear_dss_host_key ] || keygen
97 config_foreach dropbear_start dropbear
102 # killing all server processes
104 for pidfile in `ls /var/run/${NAME}.*.pid`
106 start-stop-daemon -q -K -s KILL -p "${pidfile}" -n "${NAME}"
109 [ -z "${pidfile}" ] && echo "${initscript}: no pid files, if you get problems with start then try killclients"
118 # if this script is run from inside a client session, then ignore that session
120 while [ "${pid}" -ne 0 ]
122 # get parent process id
123 pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
124 [ "${pid}" -eq 0 ] && break
126 # check if client connection
127 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
128 append ignore "${pid}"
133 # get all server pids that should be ignored
134 for server in `cat /var/run/${NAME}.*.pid`
136 append ignore "${server}"
139 # get all running pids and kill client connections
141 for pid in `pidof "${NAME}"`
143 # check if correct program, otherwise process next pid
144 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
148 # check if pid should be ignored (servers, ourself)
150 for server in ${ignore}
152 if [ "${pid}" == "${server}" ]
158 [ "${skip}" -ne 0 ] && continue
161 echo "${initscript}: Killing ${pid}..."