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
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 # E) root password authentication
49 config_get_bool rootpassauth "${section}" RootPasswordAuth 1
50 [ "${rootpassauth}" -eq 0 ] && norootpasswd=1
51 # concatenate parameters
53 args="${nopasswd:+-s }${norootpasswd:+-g }${port:+-p ${port} }${bannerfile:+-b $bannerfile }${gatewayports:+-a }-P /var/run/${NAME}.${PIDCOUNT}.pid"
55 # execute program and return its exit code
56 [ "${verbosed}" -ne 0 ] && echo "${initscript}: section ${section} starting ${PROG} ${args}"
63 for keytype in rsa dss; do
65 key=dropbear/dropbear_${keytype}_host_key
66 [ -f /tmp/$key -o -s /etc/$key ] || {
67 # generate missing keys
68 mkdir -p /tmp/dropbear
69 [ -x /usr/bin/dropbearkey ] && {
70 /usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start
76 lock /tmp/.switch2jffs
77 mkdir -p /etc/dropbear
78 mv /tmp/dropbear/dropbear_* /etc/dropbear/
79 lock -u /tmp/.switch2jffs
80 chown root /etc/dropbear
81 chmod 0700 /etc/dropbear
86 [ -s /etc/dropbear/dropbear_rsa_host_key -a \
87 -s /etc/dropbear/dropbear_dss_host_key ] || keygen
90 config_foreach dropbear_start dropbear
95 # killing all server processes
97 for pidfile in `ls /var/run/${NAME}.*.pid`
99 start-stop-daemon -q -K -s KILL -p "${pidfile}" -n "${NAME}"
102 [ -z "${pidfile}" ] && echo "${initscript}: no pid files, if you get problems with start then try killclients"
111 # if this script is run from inside a client session, then ignore that session
113 while [ "${pid}" -ne 0 ]
115 # get parent process id
116 pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
117 [ "${pid}" -eq 0 ] && break
119 # check if client connection
120 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
121 append ignore "${pid}"
126 # get all server pids that should be ignored
127 for server in `cat /var/run/${NAME}.*.pid`
129 append ignore "${server}"
132 # get all running pids and kill client connections
134 for pid in `pidof "${NAME}"`
136 # check if correct program, otherwise process next pid
137 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
141 # check if pid should be ignored (servers, ourself)
143 for server in ${ignore}
145 if [ "${pid}" == "${server}" ]
151 [ "${skip}" -ne 0 ] && continue
154 echo "${initscript}: Killing ${pid}..."