5186523910d3a36abc950644c87cf05d2bcda6ab
[openwrt/staging/chunkeey.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 append_ports()
16 {
17 local ifname="$1"
18 local port="$2"
19
20 grep -qs "^ *$ifname:" /proc/net/dev || {
21 procd_append_param command -p "$port"
22 return
23 }
24
25 for addr in $(
26 ifconfig "$ifname" | sed -ne '
27 /addr: *fe[89ab][0-9a-f]:/d
28 s/.* addr: *\([0-9a-f:\.]*\).*/\1/p
29 '
30 ); do
31 procd_append_param command -p "$addr:$port"
32 done
33 }
34
35 validate_section_dropbear()
36 {
37 uci_validate_section dropbear dropbear "${1}" \
38 'PasswordAuth:bool:1' \
39 'enable:bool:1' \
40 'Interface:string' \
41 'GatewayPorts:bool:0' \
42 'RootPasswordAuth:bool:1' \
43 'RootLogin:bool:1' \
44 'rsakeyfile:file' \
45 'dsskeyfile:file' \
46 'BannerFile:file' \
47 'Port:list(port):22' \
48 'SSHKeepAlive:uinteger:300' \
49 'IdleTimeout:uinteger:0' \
50 'mdns:uinteger:1'
51 }
52
53 dropbear_instance()
54 {
55 local PasswordAuth enable Interface GatewayPorts \
56 RootPasswordAuth RootLogin rsakeyfile \
57 dsskeyfile BannerFile Port SSHKeepAlive IdleTimeout \
58 mdns
59
60 validate_section_dropbear "${1}" || {
61 echo "validation failed"
62 return 1
63 }
64
65 [ "${enable}" = "0" ] && return 1
66 PIDCOUNT="$(( ${PIDCOUNT} + 1))"
67 local pid_file="/var/run/${NAME}.${PIDCOUNT}.pid"
68
69 procd_open_instance
70 procd_set_param command "$PROG" -F -P "$pid_file"
71 [ "${PasswordAuth}" -eq 0 ] && procd_append_param command -s
72 [ "${GatewayPorts}" -eq 1 ] && procd_append_param command -a
73 [ "${RootPasswordAuth}" -eq 0 ] && procd_append_param command -g
74 [ "${RootLogin}" -eq 0 ] && procd_append_param command -w
75 [ -n "${rsakeyfile}" ] && procd_append_param command -r "${rsakeyfile}"
76 [ -n "${dsskeyfile}" ] && procd_append_param command -d "${dsskeyfile}"
77 [ -n "${BannerFile}" ] && procd_append_param command -b "${BannerFile}"
78 [ -n "${Interface}" ] && network_get_device Interface "${Interface}"
79 append_ports "${Interface}" "${Port}"
80 [ "${IdleTimeout}" -ne 0 ] && procd_append_param command -I "${IdleTimeout}"
81 [ "${SSHKeepAlive}" -ne 0 ] && procd_append_param command -K "${SSHKeepAlive}"
82 [ "${mdns}" -ne 0 ] && procd_add_mdns "ssh" "tcp" "$Port" "daemon=dropbear"
83 procd_close_instance
84 }
85
86 keygen()
87 {
88 for keytype in rsa dss; do
89 # check for keys
90 key=dropbear/dropbear_${keytype}_host_key
91 [ -f /tmp/$key -o -s /etc/$key ] || {
92 # generate missing keys
93 mkdir -p /tmp/dropbear
94 [ -x /usr/bin/dropbearkey ] && {
95 /usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start
96 } &
97 exit 0
98 }
99 done
100
101 lock /tmp/.switch2jffs
102 mkdir -p /etc/dropbear
103 mv /tmp/dropbear/dropbear_* /etc/dropbear/
104 lock -u /tmp/.switch2jffs
105 chown root /etc/dropbear
106 chmod 0700 /etc/dropbear
107 }
108
109 start_service()
110 {
111 [ -s /etc/dropbear/dropbear_rsa_host_key -a \
112 -s /etc/dropbear/dropbear_dss_host_key ] || keygen
113
114 . /lib/functions.sh
115 . /lib/functions/network.sh
116
117 config_load "${NAME}"
118 config_foreach dropbear_instance dropbear
119 }
120
121 service_triggers()
122 {
123 procd_add_reload_trigger "dropbear"
124 procd_add_validation validate_section_dropbear
125 }
126
127 killclients()
128 {
129 local ignore=''
130 local server
131 local pid
132
133 # if this script is run from inside a client session, then ignore that session
134 pid="$$"
135 while [ "${pid}" -ne 0 ]
136 do
137 # get parent process id
138 pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
139 [ "${pid}" -eq 0 ] && break
140
141 # check if client connection
142 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
143 append ignore "${pid}"
144 break
145 }
146 done
147
148 # get all server pids that should be ignored
149 for server in `cat /var/run/${NAME}.*.pid`
150 do
151 append ignore "${server}"
152 done
153
154 # get all running pids and kill client connections
155 local skip
156 for pid in `pidof "${NAME}"`
157 do
158 # check if correct program, otherwise process next pid
159 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
160 continue
161 }
162
163 # check if pid should be ignored (servers, ourself)
164 skip=0
165 for server in ${ignore}
166 do
167 if [ "${pid}" == "${server}" ]
168 then
169 skip=1
170 break
171 fi
172 done
173 [ "${skip}" -ne 0 ] && continue
174
175 # kill process
176 echo "${initscript}: Killing ${pid}..."
177 kill -KILL ${pid}
178 done
179 }