package/dropbear: simplify & reduce init script size (closes #7985)
[openwrt/svn-archive/archive.git] / package / dropbear / files / dropbear.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2006-2010 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 (initialise with pid file)
30 local args="-P /var/run/${NAME}.${PIDCOUNT}.pid"
31 local val
32 # A) password authentication
33 config_get_bool val "${section}" PasswordAuth 1
34 [ "${val}" -eq 0 ] && append args "-s"
35 # B) listen interface and port
36 local interface
37 local address
38 config_get interface "${section}" Interface
39 config_get address "${interface}" ipaddr
40 config_get val "${section}" Port
41 val="${address:+${address}:}${val}"
42 [ -n "${val}" ] && append args "-p ${val}"
43 # C) banner file
44 config_get val "${section}" BannerFile
45 [ -f "${val}" ] && append args "-b ${val}"
46 # D) gatewayports
47 config_get_bool val "${section}" GatewayPorts 0
48 [ "${val}" -eq 1 ] && append args "-a"
49 # E) root password authentication
50 config_get_bool val "${section}" RootPasswordAuth 1
51 [ "${val}" -eq 0 ] && append args "-g"
52 # F) root login
53 config_get_bool val "${section}" RootLogin 1
54 [ "${val}" -eq 0 ] && append args "-w"
55
56 # execute program and return its exit code
57 [ "${verbosed}" -ne 0 ] && echo "${initscript}: section ${section} starting ${PROG} ${args}"
58 ${PROG} ${args}
59 return $?
60 }
61
62 keygen()
63 {
64 for keytype in rsa dss; do
65 # check for keys
66 key=dropbear/dropbear_${keytype}_host_key
67 [ -f /tmp/$key -o -s /etc/$key ] || {
68 # generate missing keys
69 mkdir -p /tmp/dropbear
70 [ -x /usr/bin/dropbearkey ] && {
71 /usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start
72 } &
73 exit 0
74 }
75 done
76
77 lock /tmp/.switch2jffs
78 mkdir -p /etc/dropbear
79 mv /tmp/dropbear/dropbear_* /etc/dropbear/
80 lock -u /tmp/.switch2jffs
81 chown root /etc/dropbear
82 chmod 0700 /etc/dropbear
83 }
84
85 start()
86 {
87 [ -s /etc/dropbear/dropbear_rsa_host_key -a \
88 -s /etc/dropbear/dropbear_dss_host_key ] || keygen
89
90 include /lib/network
91 scan_interfaces
92 config_load "${NAME}"
93 config_foreach dropbear_start dropbear
94 }
95
96 stop()
97 {
98 # killing all server processes
99 local pidfile
100 for pidfile in `ls /var/run/${NAME}.*.pid`
101 do
102 start-stop-daemon -q -K -s KILL -p "${pidfile}" -n "${NAME}"
103 rm -f "${pidfile}"
104 done
105 [ -z "${pidfile}" ] && echo "${initscript}: no pid files, if you get problems with start then try killclients"
106 }
107
108 killclients()
109 {
110 local ignore=''
111 local server
112 local pid
113
114 # if this script is run from inside a client session, then ignore that session
115 pid="$$"
116 while [ "${pid}" -ne 0 ]
117 do
118 # get parent process id
119 pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
120 [ "${pid}" -eq 0 ] && break
121
122 # check if client connection
123 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
124 append ignore "${pid}"
125 break
126 }
127 done
128
129 # get all server pids that should be ignored
130 for server in `cat /var/run/${NAME}.*.pid`
131 do
132 append ignore "${server}"
133 done
134
135 # get all running pids and kill client connections
136 local skip
137 for pid in `pidof "${NAME}"`
138 do
139 # check if correct program, otherwise process next pid
140 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
141 continue
142 }
143
144 # check if pid should be ignored (servers, ourself)
145 skip=0
146 for server in ${ignore}
147 do
148 if [ "${pid}" == "${server}" ]
149 then
150 skip=1
151 break
152 fi
153 done
154 [ "${skip}" -ne 0 ] && continue
155
156 # kill process
157 echo "${initscript}: Killing ${pid}..."
158 kill -KILL ${pid}
159 done
160 }