dropbear.init 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 port
  30. local port
  31. config_get port "${section}" Port
  32. # C) banner file
  33. local bannerfile
  34. config_get bannerfile ${section} BannerFile
  35. [ -f $bannerfile ] || bannerfile=''
  36. # D) gatewayports
  37. local gatewayports
  38. config_get_bool gatewayports "${section}" GatewayPorts 0
  39. [ "${gatewayports}" -eq 1 ] || gatewayports=''
  40. # concatenate parameters
  41. local args
  42. args="${nopasswd:+-s }${port:+-p ${port} }${bannerfile:+-b $bannerfile }${gatewayports:+-a }-P /var/run/${NAME}.${PIDCOUNT}.pid"
  43. # execute program and return its exit code
  44. [ "${verbosed}" -ne 0 ] && echo "${initscript}: section ${section} starting ${PROG} ${args}"
  45. ${PROG} ${args}
  46. return $?
  47. }
  48. keygen()
  49. {
  50. for keytype in rsa dss; do
  51. # check for keys
  52. key=dropbear/dropbear_${keytype}_host_key
  53. [ -f /tmp/$key -o -s /etc/$key ] || {
  54. # generate missing keys
  55. mkdir -p /tmp/dropbear
  56. [ -x /usr/bin/dropbearkey ] && {
  57. /usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start
  58. } &
  59. exit 0
  60. }
  61. done
  62. lock /tmp/.switch2jffs
  63. mkdir -p /etc/dropbear
  64. mv /tmp/dropbear/dropbear_* /etc/dropbear/
  65. lock -u /tmp/.switch2jffs
  66. chown root /etc/dropbear
  67. chmod 0700 /etc/dropbear
  68. }
  69. start()
  70. {
  71. [ -s /etc/dropbear/dropbear_rsa_host_key -a \
  72. -s /etc/dropbear/dropbear_dss_host_key ] || keygen
  73. config_load "${NAME}"
  74. config_foreach dropbear_start dropbear
  75. }
  76. stop()
  77. {
  78. # killing all server processes
  79. local pidfile
  80. for pidfile in `ls /var/run/${NAME}.*.pid`
  81. do
  82. start-stop-daemon -q -K -s KILL -p "${pidfile}" -n "${NAME}"
  83. rm -f "${pidfile}"
  84. done
  85. [ -z "${pidfile}" ] && echo "${initscript}: no pid files, if you get problems with start then try killclients"
  86. }
  87. killclients()
  88. {
  89. local ignore=''
  90. local server
  91. local pid
  92. # if this script is run from inside a client session, then ignore that session
  93. pid="$$"
  94. while [ "${pid}" -ne 0 ]
  95. do
  96. # get parent process id
  97. pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
  98. [ "${pid}" -eq 0 ] && break
  99. # check if client connection
  100. grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
  101. append ignore "${pid}"
  102. break
  103. }
  104. done
  105. # get all server pids that should be ignored
  106. for server in `cat /var/run/${NAME}.*.pid`
  107. do
  108. append ignore "${server}"
  109. done
  110. # get all running pids and kill client connections
  111. local skip
  112. for pid in `pidof "${NAME}"`
  113. do
  114. # check if correct program, otherwise process next pid
  115. grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
  116. continue
  117. }
  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. }