dropbear.init 4.0 KB

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