01d88afd17d563acf4ffc7580133e69e9194e507
[openwrt/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_close_instance
83 }
84
85 keygen()
86 {
87 for keytype in rsa; do
88 # check for keys
89 key=dropbear/dropbear_${keytype}_host_key
90 [ -f /tmp/$key -o -s /etc/$key ] || {
91 # generate missing keys
92 mkdir -p /tmp/dropbear
93 [ -x /usr/bin/dropbearkey ] && {
94 /usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start
95 } &
96 exit 0
97 }
98 done
99
100 lock /tmp/.switch2jffs
101 mkdir -p /etc/dropbear
102 mv /tmp/dropbear/dropbear_* /etc/dropbear/
103 lock -u /tmp/.switch2jffs
104 chown root /etc/dropbear
105 chmod 0700 /etc/dropbear
106 }
107
108 start_service()
109 {
110 [ -s /etc/dropbear/dropbear_rsa_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 }