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