03745c995b23c691b6de8a3b82893fa1b339d483
[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 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                 'mdns:uinteger:1'
45 }
46
47 dropbear_instance()
48 {
49         local PasswordAuth enable Interface GatewayPorts \
50                 RootPasswordAuth RootLogin rsakeyfile \
51                 BannerFile Port SSHKeepAlive IdleTimeout \
52                 mdns ipaddrs
53
54         validate_section_dropbear "${1}" || {
55                 echo "validation failed"
56                 return 1
57         }
58
59         [ -n "${Interface}" ] && {
60                 network_get_ipaddrs_all ipaddrs "${Interface}" || {
61                         echo "interface ${Interface} has no physdev or physdev has no suitable ip"
62                         return 1
63                 }
64         }
65
66         [ "${enable}" = "0" ] && return 1
67         PIDCOUNT="$(( ${PIDCOUNT} + 1))"
68         local pid_file="/var/run/${NAME}.${PIDCOUNT}.pid"
69
70         procd_open_instance
71         procd_set_param command "$PROG" -F -P "$pid_file"
72         [ "${PasswordAuth}" -eq 0 ] && procd_append_param command -s
73         [ "${GatewayPorts}" -eq 1 ] && procd_append_param command -a
74         [ "${RootPasswordAuth}" -eq 0 ] && procd_append_param command -g
75         [ "${RootLogin}" -eq 0 ] && procd_append_param command -w
76         [ -n "${rsakeyfile}" ] && procd_append_param command -r "${rsakeyfile}"
77         [ -n "${BannerFile}" ] && procd_append_param command -b "${BannerFile}"
78         append_ports "${ipaddrs}" "${Port}"
79         [ "${IdleTimeout}" -ne 0 ] && procd_append_param command -I "${IdleTimeout}"
80         [ "${SSHKeepAlive}" -ne 0 ] && procd_append_param command -K "${SSHKeepAlive}"
81         [ "${mdns}" -ne 0 ] && procd_add_mdns "ssh" "tcp" "$Port" "daemon=dropbear"
82         procd_set_param respawn
83         procd_close_instance
84 }
85
86 keygen()
87 {
88         for keytype in rsa; 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 ] || keygen
112
113         . /lib/functions.sh
114         . /lib/functions/network.sh
115
116         config_load "${NAME}"
117         config_foreach dropbear_instance dropbear
118 }
119
120 service_triggers()
121 {
122         procd_add_reload_trigger "dropbear"
123         procd_add_validation validate_section_dropbear
124 }
125
126 killclients()
127 {
128         local ignore=''
129         local server
130         local pid
131
132         # if this script is run from inside a client session, then ignore that session
133         pid="$$"
134         while [ "${pid}" -ne 0 ]
135          do
136                 # get parent process id
137                 pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
138                 [ "${pid}" -eq 0 ] && break
139
140                 # check if client connection
141                 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
142                         append ignore "${pid}"
143                         break
144                 }
145         done
146
147         # get all server pids that should be ignored
148         for server in `cat /var/run/${NAME}.*.pid`
149          do
150                 append ignore "${server}"
151         done
152
153         # get all running pids and kill client connections
154         local skip
155         for pid in `pidof "${NAME}"`
156          do
157                 # check if correct program, otherwise process next pid
158                 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
159                         continue
160                 }
161
162                 # check if pid should be ignored (servers, ourself)
163                 skip=0
164                 for server in ${ignore}
165                  do
166                         if [ "${pid}" = "${server}" ]
167                          then
168                                 skip=1
169                                 break
170                         fi
171                 done
172                 [ "${skip}" -ne 0 ] && continue
173
174                 # kill process
175                 echo "${initscript}: Killing ${pid}..."
176                 kill -KILL ${pid}
177         done
178 }