dropbear.init 3.5 KB

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