package] dropbear: Add bind 'Interface' option (#7149)
[oweals/openwrt.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
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 interface and port
36         local port
37         local interface
38         local address
39         config_get port "${section}" Port
40         config_get interface "${section}" Interface
41         config_get address "${interface}" ipaddr
42         port="${address:+${address}:}${port}"
43         # C) banner file
44         local bannerfile
45         config_get bannerfile "${section}" BannerFile
46         [ -f "$bannerfile" ] || bannerfile=''
47         # D) gatewayports
48         local gatewayports
49         config_get_bool gatewayports "${section}" GatewayPorts 0
50         [ "${gatewayports}" -eq 1 ] || gatewayports=''
51         # E) root password authentication
52         local norootpasswd
53         local rootpassauth
54         config_get_bool rootpassauth "${section}" RootPasswordAuth 1
55         [ "${rootpassauth}" -eq 0 ] && norootpasswd=1
56         # concatenate parameters
57         local args
58         args="${nopasswd:+-s }${norootpasswd:+-g }${port:+-p ${port} }${bannerfile:+-b $bannerfile }${gatewayports:+-a }-P /var/run/${NAME}.${PIDCOUNT}.pid"
59
60         # execute program and return its exit code
61         [ "${verbosed}" -ne 0 ] && echo "${initscript}: section ${section} starting ${PROG} ${args}"
62         ${PROG} ${args}
63         return $?
64 }
65
66 keygen()
67 {
68         for keytype in rsa dss; do
69                 # check for keys
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
76                         } &
77                 exit 0
78                 }
79         done
80
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
87 }
88
89 start()
90 {
91         [ -s /etc/dropbear/dropbear_rsa_host_key -a \
92           -s /etc/dropbear/dropbear_dss_host_key ] || keygen
93
94         include /lib/network
95         scan_interfaces
96         config_load "${NAME}"
97         config_foreach dropbear_start dropbear
98 }
99
100 stop()
101 {
102         # killing all server processes
103         local pidfile
104         for pidfile in `ls /var/run/${NAME}.*.pid`
105          do
106                 start-stop-daemon -q -K -s KILL -p "${pidfile}" -n "${NAME}"
107                 rm -f "${pidfile}"
108         done
109         [ -z "${pidfile}" ] && echo "${initscript}: no pid files, if you get problems with start then try killclients"
110 }
111
112 killclients()
113 {
114         local ignore=''
115         local server
116         local pid
117
118         # if this script is run from inside a client session, then ignore that session
119         pid="$$"
120         while [ "${pid}" -ne 0 ]
121          do
122                 # get parent process id
123                 pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
124                 [ "${pid}" -eq 0 ] && break
125
126                 # check if client connection
127                 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
128                         append ignore "${pid}"
129                         break
130                 }
131         done
132
133         # get all server pids that should be ignored
134         for server in `cat /var/run/${NAME}.*.pid`
135          do
136                 append ignore "${server}"
137         done
138
139         # get all running pids and kill client connections
140         local skip
141         for pid in `pidof "${NAME}"`
142          do
143                 # check if correct program, otherwise process next pid
144                 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
145                         continue
146                 }
147
148                 # check if pid should be ignored (servers, ourself)
149                 skip=0
150                 for server in ${ignore}
151                  do
152                         if [ "${pid}" == "${server}" ]
153                          then
154                                 skip=1
155                                 break
156                         fi
157                 done
158                 [ "${skip}" -ne 0 ] && continue
159
160                 # kill process
161                 echo "${initscript}: Killing ${pid}..."
162                 kill -KILL ${pid}
163         done
164 }