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