[package] dropbear: Add RootLogin parameter to control whether ssh logins as root...
[openwrt.git] / package / 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 NAME=dropbear
6 PROG=/usr/sbin/dropbear
7 START=50
8 STOP=50
9 PIDCOUNT=0
10 EXTRA_COMMANDS="killclients"
11 EXTRA_HELP="    killclients Kill ${NAME} processes except servers and yourself"
12
13 dropbear_start()
14 {
15         local section="$1"
16
17         # check if section is enabled (default)
18         local enabled
19         config_get_bool enabled "${section}" enable 1
20         [ "${enabled}" -eq 0 ] && return 1
21
22         # verbose parameter
23         local verbosed
24         config_get_bool verbosed "${section}" verbose 0
25
26         # increase pid file count to handle multiple instances correctly
27         PIDCOUNT="$(( ${PIDCOUNT} + 1))"
28
29         # prepare parameters
30         # A) password authentication
31         local nopasswd
32         local passauth
33         config_get_bool passauth "${section}" PasswordAuth 1
34         [ "${passauth}" -eq 0 ] && nopasswd=1
35         # B) listen interface and port
36         local port
37         local interface
38         local address
39         config_get port "${section}" Port
40         config_get interface "${section}" Interface
41         config_get address "${interface}" ipaddr
42         port="${address:+${address}:}${port}"
43         # C) banner file
44         local bannerfile
45         config_get bannerfile "${section}" BannerFile
46         [ -f "$bannerfile" ] || bannerfile=''
47         # D) gatewayports
48         local gatewayports
49         config_get_bool gatewayports "${section}" GatewayPorts 0
50         [ "${gatewayports}" -eq 1 ] || gatewayports=''
51         # E) root password authentication
52         local norootpasswd
53         local rootpassauth
54         config_get_bool rootpassauth "${section}" RootPasswordAuth 1
55         [ "${rootpassauth}" -eq 0 ] && norootpasswd=1
56         local rootloginallowed
57         local norootlogin
58         config_get_bool rootloginallowed "${section}" RootLogin 1
59         [ "${rootloginallowed}" -eq 0 ] && norootlogin=1
60         # concatenate parameters
61         local args
62         args="${nopasswd:+-s }${norootpasswd:+-g }${norootlogin:+-w }${port:+-p ${port} }${bannerfile:+-b $bannerfile }${gatewayports:+-a }-P /var/run/${NAME}.${PIDCOUNT}.pid"
63
64         # execute program and return its exit code
65         [ "${verbosed}" -ne 0 ] && echo "${initscript}: section ${section} starting ${PROG} ${args}"
66         ${PROG} ${args}
67         return $?
68 }
69
70 keygen()
71 {
72         for keytype in rsa dss; do
73                 # check for keys
74                 key=dropbear/dropbear_${keytype}_host_key
75                 [ -f /tmp/$key -o -s /etc/$key ] || {
76                         # generate missing keys
77                         mkdir -p /tmp/dropbear
78                         [ -x /usr/bin/dropbearkey ] && {
79                                 /usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start
80                         } &
81                 exit 0
82                 }
83         done
84
85         lock /tmp/.switch2jffs
86         mkdir -p /etc/dropbear
87         mv /tmp/dropbear/dropbear_* /etc/dropbear/
88         lock -u /tmp/.switch2jffs
89         chown root /etc/dropbear
90         chmod 0700 /etc/dropbear
91 }
92
93 start()
94 {
95         [ -s /etc/dropbear/dropbear_rsa_host_key -a \
96           -s /etc/dropbear/dropbear_dss_host_key ] || keygen
97
98         include /lib/network
99         scan_interfaces
100         config_load "${NAME}"
101         config_foreach dropbear_start dropbear
102 }
103
104 stop()
105 {
106         # killing all server processes
107         local pidfile
108         for pidfile in `ls /var/run/${NAME}.*.pid`
109          do
110                 start-stop-daemon -q -K -s KILL -p "${pidfile}" -n "${NAME}"
111                 rm -f "${pidfile}"
112         done
113         [ -z "${pidfile}" ] && echo "${initscript}: no pid files, if you get problems with start then try killclients"
114 }
115
116 killclients()
117 {
118         local ignore=''
119         local server
120         local pid
121
122         # if this script is run from inside a client session, then ignore that session
123         pid="$$"
124         while [ "${pid}" -ne 0 ]
125          do
126                 # get parent process id
127                 pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
128                 [ "${pid}" -eq 0 ] && break
129
130                 # check if client connection
131                 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
132                         append ignore "${pid}"
133                         break
134                 }
135         done
136
137         # get all server pids that should be ignored
138         for server in `cat /var/run/${NAME}.*.pid`
139          do
140                 append ignore "${server}"
141         done
142
143         # get all running pids and kill client connections
144         local skip
145         for pid in `pidof "${NAME}"`
146          do
147                 # check if correct program, otherwise process next pid
148                 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
149                         continue
150                 }
151
152                 # check if pid should be ignored (servers, ourself)
153                 skip=0
154                 for server in ${ignore}
155                  do
156                         if [ "${pid}" == "${server}" ]
157                          then
158                                 skip=1
159                                 break
160                         fi
161                 done
162                 [ "${skip}" -ne 0 ] && continue
163
164                 # kill process
165                 echo "${initscript}: Killing ${pid}..."
166                 kill -KILL ${pid}
167         done
168 }