eef2643c14ac6b916bebb49e835186502afecce1
[openwrt/staging/jogo.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=19
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 append_ports()
16 {
17 local ipaddrs="$1"
18 local port="$2"
19
20 [ -z "$ipaddrs" ] && {
21 procd_append_param command -p "$port"
22 return
23 }
24
25 for addr in $ipaddrs; do
26 procd_append_param command -p "$addr:$port"
27 done
28 }
29
30 validate_section_dropbear()
31 {
32 uci_validate_section dropbear dropbear "${1}" \
33 'PasswordAuth:bool:1' \
34 'enable:bool:1' \
35 'Interface:string' \
36 'GatewayPorts:bool:0' \
37 'RootPasswordAuth:bool:1' \
38 'RootLogin:bool:1' \
39 'rsakeyfile:file' \
40 'BannerFile:file' \
41 'Port:list(port):22' \
42 'SSHKeepAlive:uinteger:300' \
43 'IdleTimeout:uinteger:0' \
44 'MaxAuthTries:uinteger:3' \
45 'RecvWindowSize:uinteger:0' \
46 'mdns:bool:1'
47 }
48
49 dropbear_instance()
50 {
51 local PasswordAuth enable Interface GatewayPorts \
52 RootPasswordAuth RootLogin rsakeyfile \
53 BannerFile Port SSHKeepAlive IdleTimeout \
54 MaxAuthTries RecvWindowSize mdns ipaddrs
55
56 validate_section_dropbear "${1}" || {
57 echo "validation failed"
58 return 1
59 }
60
61 [ -n "${Interface}" ] && {
62 [ -n "${BOOT}" ] && return 0
63
64 network_get_ipaddrs_all ipaddrs "${Interface}" || {
65 echo "interface ${Interface} has no physdev or physdev has no suitable ip"
66 return 1
67 }
68 }
69
70 [ "${enable}" = "0" ] && return 1
71 PIDCOUNT="$(( ${PIDCOUNT} + 1))"
72 local pid_file="/var/run/${NAME}.${PIDCOUNT}.pid"
73
74 procd_open_instance
75 procd_set_param command "$PROG" -F -P "$pid_file"
76 [ "${PasswordAuth}" -eq 0 ] && procd_append_param command -s
77 [ "${GatewayPorts}" -eq 1 ] && procd_append_param command -a
78 [ "${RootPasswordAuth}" -eq 0 ] && procd_append_param command -g
79 [ "${RootLogin}" -eq 0 ] && procd_append_param command -w
80 [ -n "${rsakeyfile}" ] && procd_append_param command -r "${rsakeyfile}"
81 [ -n "${BannerFile}" ] && procd_append_param command -b "${BannerFile}"
82 append_ports "${ipaddrs}" "${Port}"
83 [ "${IdleTimeout}" -ne 0 ] && procd_append_param command -I "${IdleTimeout}"
84 [ "${SSHKeepAlive}" -ne 0 ] && procd_append_param command -K "${SSHKeepAlive}"
85 [ "${MaxAuthTries}" -ne 0 ] && procd_append_param command -T "${MaxAuthTries}"
86 [ "${RecvWindowSize}" -gt 0 -a "${RecvWindowSize}" -le 1048576 ] && \
87 procd_append_param command -W "${RecvWindowSize}"
88 [ "${mdns}" -ne 0 ] && procd_add_mdns "ssh" "tcp" "$Port" "daemon=dropbear"
89 procd_set_param respawn
90 procd_close_instance
91 }
92
93 keygen()
94 {
95 for keytype in rsa; do
96 # check for keys
97 key=dropbear/dropbear_${keytype}_host_key
98 [ -f /tmp/$key -o -s /etc/$key ] || {
99 # generate missing keys
100 mkdir -p /tmp/dropbear
101 [ -x /usr/bin/dropbearkey ] && {
102 /usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start
103 } &
104 exit 0
105 }
106 done
107
108 lock /tmp/.switch2jffs
109 mkdir -p /etc/dropbear
110 mv /tmp/dropbear/dropbear_* /etc/dropbear/
111 lock -u /tmp/.switch2jffs
112 chown root /etc/dropbear
113 chmod 0700 /etc/dropbear
114 }
115
116 load_interfaces()
117 {
118 config_get interface "$1" Interface
119 config_get enable "$1" enable 1
120
121 [ "${enable}" = "1" ] && interfaces=" ${interface} ${interfaces}"
122 }
123
124 boot()
125 {
126 BOOT=1
127 start "$@"
128 }
129
130 start_service()
131 {
132 [ -s /etc/dropbear/dropbear_rsa_host_key ] || keygen
133
134 . /lib/functions.sh
135 . /lib/functions/network.sh
136
137 config_load "${NAME}"
138 config_foreach dropbear_instance dropbear
139 }
140
141 service_triggers()
142 {
143 local interfaces
144
145 procd_add_config_trigger "config.change" "dropbear" /etc/init.d/dropbear reload
146
147 config_load "${NAME}"
148 config_foreach load_interfaces dropbear
149
150 [ -n "${interfaces}" ] && {
151 for n in $interfaces ; do
152 procd_add_interface_trigger "interface.*" $n /etc/init.d/dropbear reload
153 done
154 }
155
156 procd_add_validation validate_section_dropbear
157 }
158
159 shutdown() {
160 # close all open connections
161 killall dropbear
162 }
163
164 killclients()
165 {
166 local ignore=''
167 local server
168 local pid
169
170 # if this script is run from inside a client session, then ignore that session
171 pid="$$"
172 while [ "${pid}" -ne 0 ]
173 do
174 # get parent process id
175 pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
176 [ "${pid}" -eq 0 ] && break
177
178 # check if client connection
179 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
180 append ignore "${pid}"
181 break
182 }
183 done
184
185 # get all server pids that should be ignored
186 for server in `cat /var/run/${NAME}.*.pid`
187 do
188 append ignore "${server}"
189 done
190
191 # get all running pids and kill client connections
192 local skip
193 for pid in `pidof "${NAME}"`
194 do
195 # check if correct program, otherwise process next pid
196 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
197 continue
198 }
199
200 # check if pid should be ignored (servers, ourself)
201 skip=0
202 for server in ${ignore}
203 do
204 if [ "${pid}" = "${server}" ]
205 then
206 skip=1
207 break
208 fi
209 done
210 [ "${skip}" -ne 0 ] && continue
211
212 # kill process
213 echo "${initscript}: Killing ${pid}..."
214 kill -KILL ${pid}
215 done
216 }