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