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