fix path to libgcc.a in LIBGCC_S
[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 port
36 local port
37 config_get port "${section}" Port
38 # C) banner file
39 local bannerfile
40 config_get bannerfile "${section}" BannerFile
41 [ -f "$bannerfile" ] || bannerfile=''
42 # D) gatewayports
43 local gatewayports
44 config_get_bool gatewayports "${section}" GatewayPorts 0
45 [ "${gatewayports}" -eq 1 ] || gatewayports=''
46 # E) root password authentication
47 local norootpasswd
48 local rootpassauth
49 config_get_bool rootpassauth "${section}" RootPasswordAuth 1
50 [ "${rootpassauth}" -eq 0 ] && norootpasswd=1
51 # concatenate parameters
52 local args
53 args="${nopasswd:+-s }${norootpasswd:+-g }${port:+-p ${port} }${bannerfile:+-b $bannerfile }${gatewayports:+-a }-P /var/run/${NAME}.${PIDCOUNT}.pid"
54
55 # execute program and return its exit code
56 [ "${verbosed}" -ne 0 ] && echo "${initscript}: section ${section} starting ${PROG} ${args}"
57 ${PROG} ${args}
58 return $?
59 }
60
61 keygen()
62 {
63 for keytype in rsa dss; do
64 # check for keys
65 key=dropbear/dropbear_${keytype}_host_key
66 [ -f /tmp/$key -o -s /etc/$key ] || {
67 # generate missing keys
68 mkdir -p /tmp/dropbear
69 [ -x /usr/bin/dropbearkey ] && {
70 /usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start
71 } &
72 exit 0
73 }
74 done
75
76 lock /tmp/.switch2jffs
77 mkdir -p /etc/dropbear
78 mv /tmp/dropbear/dropbear_* /etc/dropbear/
79 lock -u /tmp/.switch2jffs
80 chown root /etc/dropbear
81 chmod 0700 /etc/dropbear
82 }
83
84 start()
85 {
86 [ -s /etc/dropbear/dropbear_rsa_host_key -a \
87 -s /etc/dropbear/dropbear_dss_host_key ] || keygen
88
89 config_load "${NAME}"
90 config_foreach dropbear_start dropbear
91 }
92
93 stop()
94 {
95 # killing all server processes
96 local pidfile
97 for pidfile in `ls /var/run/${NAME}.*.pid`
98 do
99 start-stop-daemon -q -K -s KILL -p "${pidfile}" -n "${NAME}"
100 rm -f "${pidfile}"
101 done
102 [ -z "${pidfile}" ] && echo "${initscript}: no pid files, if you get problems with start then try killclients"
103 }
104
105 killclients()
106 {
107 local ignore=''
108 local server
109 local pid
110
111 # if this script is run from inside a client session, then ignore that session
112 pid="$$"
113 while [ "${pid}" -ne 0 ]
114 do
115 # get parent process id
116 pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
117 [ "${pid}" -eq 0 ] && break
118
119 # check if client connection
120 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
121 append ignore "${pid}"
122 break
123 }
124 done
125
126 # get all server pids that should be ignored
127 for server in `cat /var/run/${NAME}.*.pid`
128 do
129 append ignore "${server}"
130 done
131
132 # get all running pids and kill client connections
133 local skip
134 for pid in `pidof "${NAME}"`
135 do
136 # check if correct program, otherwise process next pid
137 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
138 continue
139 }
140
141 # check if pid should be ignored (servers, ourself)
142 skip=0
143 for server in ${ignore}
144 do
145 if [ "${pid}" == "${server}" ]
146 then
147 skip=1
148 break
149 fi
150 done
151 [ "${skip}" -ne 0 ] && continue
152
153 # kill process
154 echo "${initscript}: Killing ${pid}..."
155 kill -KILL ${pid}
156 done
157 }
This page took 0.043702 seconds and 5 git commands to generate.