dropbear: rewrite init script startup logic to handle both host key files
[openwrt/staging/dedeckeh.git] / package / network / services / 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 START=50
6 STOP=50
7
8 USE_PROCD=1
9 PROG=/usr/sbin/dropbear
10 NAME=dropbear
11 PIDCOUNT=0
12 EXTRA_COMMANDS="killclients"
13 EXTRA_HELP=" killclients Kill ${NAME} processes except servers and yourself"
14
15 _dropbearkey()
16 {
17 /usr/bin/dropbearkey "$@" 0<&- 1>&- 2>&-
18 }
19
20 # $1 - host key file name
21 hk_verify()
22 {
23 [ -f "$1" ] || return 1
24 [ -s "$1" ] || return 2
25 _dropbearkey -y -f "$1" || return 3
26 return 0
27 }
28
29 # $1 - hk_verify() return code
30 hk_errmsg()
31 {
32 case "$1" in
33 0) ;;
34 1) echo "file does not exist" ;;
35 2) echo "file has zero length" ;;
36 3) echo "file is not valid host key or not supported" ;;
37 *) echo "unknown error" ;;
38 esac
39 }
40
41 # $1 - config option
42 # $2 - host key file name
43 hk_config()
44 {
45 local x m
46 hk_verify "$2"; x=$?
47 case "$x" in
48 0) procd_append_param command -r "$2"
49 ;;
50 *) m=$(hk_errmsg "$x")
51 logger -t "${NAME}" -p daemon.warn \
52 "option '$1', value '$2': $m, skipping"
53 ;;
54 esac
55 }
56
57 # $1 - host key file name
58 hk_config__keyfile()
59 {
60 hk_config 'keyfile' "$1"
61 }
62
63 hk_generate_as_needed()
64 {
65 local kdir kgen ktype tdir kcount tfile tlock
66 kdir='/etc/dropbear'
67
68 kgen=''
69 for ktype in ecdsa rsa; do
70 hk_verify "${kdir}/dropbear_${ktype}_host_key" && continue
71
72 kgen="${kgen} ${ktype}"
73 done
74
75 [ -z "${kgen}" ] && return
76
77 tdir=$(mktemp -d); chmod 0700 "${tdir}"
78
79 kcount=0
80 for ktype in ${kgen}; do
81 tfile="${tdir}/dropbear_${ktype}_host_key"
82
83 if ! _dropbearkey -t ${ktype} -f "${tfile}"; then
84 # unsupported key type
85 rm -f "${tfile}"
86 continue
87 fi
88
89 kcount=$((kcount+1))
90 done
91
92 if [ ${kcount} -ne 0 ]; then
93 tlock='/tmp/.switch2jffs'
94 lock "${tlock}"
95 mkdir -p "${kdir}"; chmod 0700 "${kdir}"; chown root "${kdir}"
96 mv -f "${tdir}/"* "${kdir}/"
97 lock -u "${tlock}"
98 fi
99
100 rm -rf "${tdir}"
101 }
102
103 append_ports()
104 {
105 local ipaddrs="$1"
106 local port="$2"
107
108 [ -z "$ipaddrs" ] && {
109 procd_append_param command -p "$port"
110 return
111 }
112
113 for addr in $ipaddrs; do
114 procd_append_param command -p "$addr:$port"
115 done
116 }
117
118 validate_section_dropbear()
119 {
120 uci_validate_section dropbear dropbear "${1}" \
121 'PasswordAuth:bool:1' \
122 'enable:bool:1' \
123 'Interface:string' \
124 'GatewayPorts:bool:0' \
125 'RootPasswordAuth:bool:1' \
126 'RootLogin:bool:1' \
127 'rsakeyfile:file' \
128 'keyfile:list(file)' \
129 'BannerFile:file' \
130 'Port:port:22' \
131 'SSHKeepAlive:uinteger:300' \
132 'IdleTimeout:uinteger:0' \
133 'MaxAuthTries:uinteger:3' \
134 'RecvWindowSize:uinteger:0' \
135 'mdns:bool:1'
136 }
137
138 dropbear_instance()
139 {
140 local PasswordAuth enable Interface GatewayPorts \
141 RootPasswordAuth RootLogin rsakeyfile keyfile \
142 BannerFile Port SSHKeepAlive IdleTimeout \
143 MaxAuthTries RecvWindowSize mdns ipaddrs
144
145 validate_section_dropbear "${1}" || {
146 echo "validation failed"
147 return 1
148 }
149
150 [ -n "${Interface}" ] && {
151 network_get_ipaddrs_all ipaddrs "${Interface}" || {
152 echo "interface ${Interface} has no physdev or physdev has no suitable ip"
153 return 1
154 }
155 }
156
157 [ "${enable}" = "0" ] && return 1
158 PIDCOUNT="$(( ${PIDCOUNT} + 1))"
159 local pid_file="/var/run/${NAME}.${PIDCOUNT}.pid"
160
161 procd_open_instance
162 procd_set_param command "$PROG" -F -P "$pid_file"
163 [ "${PasswordAuth}" -eq 0 ] && procd_append_param command -s
164 [ "${GatewayPorts}" -eq 1 ] && procd_append_param command -a
165 [ "${RootPasswordAuth}" -eq 0 ] && procd_append_param command -g
166 [ "${RootLogin}" -eq 0 ] && procd_append_param command -w
167 if [ -n "${rsakeyfile}" ]; then
168 logger -t ${NAME} -p daemon.warn \
169 "option 'rsakeyfile' is considered to be deprecated and" \
170 "will be removed in future releases, use 'keyfile' instead"
171 hk_config 'rsakeyfile' "${rsakeyfile}"
172 fi
173 config_list_foreach "$1" "keyfile" hk_config__keyfile
174 [ -n "${BannerFile}" ] && procd_append_param command -b "${BannerFile}"
175 append_ports "${ipaddrs}" "${Port}"
176 [ "${IdleTimeout}" -ne 0 ] && procd_append_param command -I "${IdleTimeout}"
177 [ "${SSHKeepAlive}" -ne 0 ] && procd_append_param command -K "${SSHKeepAlive}"
178 [ "${MaxAuthTries}" -ne 0 ] && procd_append_param command -T "${MaxAuthTries}"
179 [ "${RecvWindowSize}" -gt 0 -a "${RecvWindowSize}" -le 1048576 ] && \
180 procd_append_param command -W "${RecvWindowSize}"
181 [ "${mdns}" -ne 0 ] && procd_add_mdns "ssh" "tcp" "$Port" "daemon=dropbear"
182 procd_set_param respawn
183 procd_close_instance
184 }
185
186 load_interfaces()
187 {
188 config_get interface "$1" Interface
189 config_get enable "$1" enable 1
190
191 [ "${enable}" = "1" ] && interfaces=" ${interface} ${interfaces}"
192 }
193
194 start_service()
195 {
196 hk_generate_as_needed
197
198 . /lib/functions.sh
199 . /lib/functions/network.sh
200
201 config_load "${NAME}"
202 config_foreach dropbear_instance dropbear
203 }
204
205 service_triggers()
206 {
207 local interfaces
208
209 procd_add_config_trigger "config.change" "dropbear" /etc/init.d/dropbear reload
210
211 config_load "${NAME}"
212 config_foreach load_interfaces dropbear
213
214 [ -n "${interfaces}" ] && {
215 for n in $interfaces ; do
216 procd_add_interface_trigger "interface.*" $n /etc/init.d/dropbear reload
217 done
218 }
219
220 procd_add_validation validate_section_dropbear
221 }
222
223 shutdown() {
224 # close all open connections
225 killall dropbear
226 }
227
228 killclients()
229 {
230 local ignore=''
231 local server
232 local pid
233
234 # if this script is run from inside a client session, then ignore that session
235 pid="$$"
236 while [ "${pid}" -ne 0 ]
237 do
238 # get parent process id
239 pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
240 [ "${pid}" -eq 0 ] && break
241
242 # check if client connection
243 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
244 append ignore "${pid}"
245 break
246 }
247 done
248
249 # get all server pids that should be ignored
250 for server in `cat /var/run/${NAME}.*.pid`
251 do
252 append ignore "${server}"
253 done
254
255 # get all running pids and kill client connections
256 local skip
257 for pid in `pidof "${NAME}"`
258 do
259 # check if correct program, otherwise process next pid
260 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
261 continue
262 }
263
264 # check if pid should be ignored (servers, ourself)
265 skip=0
266 for server in ${ignore}
267 do
268 if [ "${pid}" = "${server}" ]
269 then
270 skip=1
271 break
272 fi
273 done
274 [ "${skip}" -ne 0 ] && continue
275
276 # kill process
277 echo "${initscript}: Killing ${pid}..."
278 kill -KILL ${pid}
279 done
280 }