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