Root/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
5START=50
6STOP=50
7
8USE_PROCD=1
9
10NAME=dropbear
11PROG=/usr/sbin/dropbear
12PIDCOUNT=0
13EXTRA_COMMANDS="killclients"
14EXTRA_HELP=" killclients Kill ${NAME} processes except servers and yourself"
15
16dropbear_instance()
17{
18    append_ports()
19    {
20        local ifname="$1"
21        local port="$2"
22
23        grep -qs "^ *$ifname:" /proc/net/dev || {
24            procd_append_param command -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            procd_append_param command -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    # increase pid file count to handle multiple instances correctly
47    PIDCOUNT="$(( ${PIDCOUNT} + 1))"
48
49    local pid_file="/var/run/${NAME}.${PIDCOUNT}.pid"
50
51    procd_open_instance
52    procd_set_param command "$PROG" -F -P "$pid_file"
53
54    # prepare parameters (initialise with pid file)
55    local val
56
57    # A) password authentication
58    config_get_bool val "${section}" PasswordAuth 1
59    [ "${val}" -eq 0 ] && procd_append_param command -s
60
61    # B) listen interface and port
62    local port
63    local interface
64    config_get interface "${section}" Interface
65    [ -n "$interface" ] && network_get_device interface "$interface"
66    config_get port "${section}" Port 22
67    append_ports "$interface" "$port"
68    # C) banner file
69    config_get val "${section}" BannerFile
70    [ -f "${val}" ] && procd_append_param command -b "${val}"
71    # D) gatewayports
72    config_get_bool val "${section}" GatewayPorts 0
73    [ "${val}" -eq 1 ] && procd_append_param command -a
74    # E) root password authentication
75    config_get_bool val "${section}" RootPasswordAuth 1
76    [ "${val}" -eq 0 ] && procd_append_param command -g
77    # F) root login
78    config_get_bool val "${section}" RootLogin 1
79    [ "${val}" -eq 0 ] && procd_append_param command -w
80    # G) host keys
81    config_get val "${section}" rsakeyfile
82    [ -f "${val}" ] && procd_append_param command -r "${val}"
83    config_get val "${section}" dsskeyfile
84    [ -f "${val}" ] && procd_append_param command -d "${val}"
85
86    procd_close_instance
87}
88
89keygen()
90{
91    for keytype in rsa dss; do
92        # check for keys
93        key=dropbear/dropbear_${keytype}_host_key
94        [ -f /tmp/$key -o -s /etc/$key ] || {
95            # generate missing keys
96            mkdir -p /tmp/dropbear
97            [ -x /usr/bin/dropbearkey ] && {
98                /usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start
99            } &
100        exit 0
101        }
102    done
103
104    lock /tmp/.switch2jffs
105    mkdir -p /etc/dropbear
106    mv /tmp/dropbear/dropbear_* /etc/dropbear/
107    lock -u /tmp/.switch2jffs
108    chown root /etc/dropbear
109    chmod 0700 /etc/dropbear
110}
111
112start_service()
113{
114    [ -s /etc/dropbear/dropbear_rsa_host_key -a \
115      -s /etc/dropbear/dropbear_dss_host_key ] || keygen
116
117    . /lib/functions.sh
118    . /lib/functions/network.sh
119
120    config_load "${NAME}"
121    config_foreach dropbear_instance dropbear
122}
123
124killclients()
125{
126    local ignore=''
127    local server
128    local pid
129
130    # if this script is run from inside a client session, then ignore that session
131    pid="$$"
132    while [ "${pid}" -ne 0 ]
133     do
134        # get parent process id
135        pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
136        [ "${pid}" -eq 0 ] && break
137
138        # check if client connection
139        grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
140            append ignore "${pid}"
141            break
142        }
143    done
144
145    # get all server pids that should be ignored
146    for server in `cat /var/run/${NAME}.*.pid`
147     do
148        append ignore "${server}"
149    done
150
151    # get all running pids and kill client connections
152    local skip
153    for pid in `pidof "${NAME}"`
154     do
155        # check if correct program, otherwise process next pid
156        grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
157            continue
158        }
159
160        # check if pid should be ignored (servers, ourself)
161        skip=0
162        for server in ${ignore}
163         do
164            if [ "${pid}" == "${server}" ]
165             then
166                skip=1
167                break
168            fi
169        done
170        [ "${skip}" -ne 0 ] && continue
171
172        # kill process
173        echo "${initscript}: Killing ${pid}..."
174        kill -KILL ${pid}
175    done
176}
177

Archive Download this file



interactive