dropbear.init 3.3 KB

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