dropbear.init 3.9 KB

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