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