4ee130c3a570cb5bbe05a33b1ff3db553191a1a6
[openwrt/svn-archive/archive.git] / package / dropbear / files / dropbear.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2006-2009 OpenWrt.org
3 # Copyright (C) 2006 Carlos Sobrinho
4
5 NAME=dropbear
6 PROG=/usr/sbin/dropbear
7 START=50
8 STOP=50
9 PIDCOUNT=0
10 EXTRA_COMMANDS="killclients"
11 EXTRA_HELP=" killclients Kill ${NAME} processes except servers and yourself"
12
13 dropbear_start()
14 {
15 local section="$1"
16
17 # check if section is enabled (default)
18 local enabled
19 config_get_bool enabled "${section}" enable 1
20 [ "${enabled}" -eq 0 ] && return 1
21
22 # verbose parameter
23 local verbosed
24 config_get_bool verbosed "${section}" verbose 0
25
26 # increase pid file count to handle multiple instances correctly
27 PIDCOUNT="$(( ${PIDCOUNT} + 1))"
28
29 # prepare parameters
30 # A) password authentication
31 local nopasswd
32 local passauth
33 config_get_bool passauth "${section}" PasswordAuth 1
34 [ "${passauth}" -eq 0 ] && nopasswd=1
35 # B) listen port
36 local port
37 config_get port "${section}" Port
38 # C) banner file
39 local bannerfile
40 config_get bannerfile ${section} BannerFile
41 [ -f $bannerfile ] || bannerfile=''
42 # D) gatewayports
43 local gatewayports
44 config_get_bool gatewayports "${section}" GatewayPorts 0
45 [ "${gatewayports}" -eq 1 ] || gatewayports=''
46 # concatenate parameters
47 local args
48 args="${nopasswd:+-s }${port:+-p ${port} }${bannerfile:+-b $bannerfile }${gatewayports:+-a }-P /var/run/${NAME}.${PIDCOUNT}.pid"
49
50 # execute program and return its exit code
51 [ "${verbosed}" -ne 0 ] && echo "${initscript}: section ${section} starting ${PROG} ${args}"
52 ${PROG} ${args}
53 return $?
54 }
55
56 keygen()
57 {
58 for keytype in rsa dss; do
59 # check for keys
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
66 } &
67 exit 0
68 }
69 done
70
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
77 }
78
79 start()
80 {
81 [ -s /etc/dropbear/dropbear_rsa_host_key -a \
82 -s /etc/dropbear/dropbear_dss_host_key ] || keygen
83
84 config_load "${NAME}"
85 config_foreach dropbear_start dropbear
86 }
87
88 stop()
89 {
90 # killing all server processes
91 local pidfile
92 for pidfile in `ls /var/run/${NAME}.*.pid`
93 do
94 start-stop-daemon -K -s KILL -p "${pidfile}" -n "${NAME}" >/dev/null
95 rm -f "${pidfile}"
96 done
97 [ -z "${pidfile}" ] && echo "${initscript}: no pid files, if you get problems with start then try killclients"
98 }
99
100 killclients()
101 {
102 local ignore=''
103 local server
104 local pid
105
106 # if this script is run from inside a client session, then ignore that session
107 pid="$$"
108 while [ "${pid}" -ne 0 ]
109 do
110 # get parent process id
111 pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
112 [ "${pid}" -eq 0 ] && break
113
114 # check if client connection
115 ps | grep -e "^[ ]*${pid} " | grep "${PROG}" >/dev/null
116 if [ $? -eq 0 ]
117 then
118 append ignore "${pid}"
119 break
120 fi
121 done
122
123 # get all server pids that should be ignored
124 for server in `cat /var/run/${NAME}.*.pid`
125 do
126 append ignore "${server}"
127 done
128
129 # get all running pids and kill client connections
130 local skip
131 for pid in `pidof "${NAME}"`
132 do
133 # check if correct program
134 ps | grep -e "^[ ]*${pid} " | grep "${PROG}" >/dev/null
135 [ $? -ne 0 ] && continue
136
137 # check if pid should be ignored (servers, ourself)
138 skip=0
139 for server in ${ignore}
140 do
141 if [ "${pid}" == "${server}" ]
142 then
143 skip=1
144 break
145 fi
146 done
147 [ "${skip}" -ne 0 ] && continue
148
149 # kill process
150 echo "${initscript}: Killing ${pid}..."
151 kill -KILL ${pid}
152 done
153 }