dropbear.init 4.2 KB

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