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