dropbear.init 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2006-2010 OpenWrt.org
  3. # Copyright (C) 2006 Carlos Sobrinho
  4. START=50
  5. STOP=50
  6. USE_PROCD=1
  7. NAME=dropbear
  8. PROG=/usr/sbin/dropbear
  9. PIDCOUNT=0
  10. EXTRA_COMMANDS="killclients"
  11. EXTRA_HELP=" killclients Kill ${NAME} processes except servers and yourself"
  12. dropbear_instance()
  13. {
  14. append_ports()
  15. {
  16. local ifname="$1"
  17. local port="$2"
  18. grep -qs "^ *$ifname:" /proc/net/dev || {
  19. procd_append_param command -p "$port"
  20. return
  21. }
  22. for addr in $(
  23. ifconfig "$ifname" | sed -ne '
  24. /addr: *fe[89ab][0-9a-f]:/d
  25. s/.* addr: *\([0-9a-f:\.]*\).*/\1/p
  26. '
  27. ); do
  28. procd_append_param command -p "$addr:$port"
  29. done
  30. }
  31. local section="$1"
  32. # check if section is enabled (default)
  33. local enabled
  34. config_get_bool enabled "${section}" enable 1
  35. [ "${enabled}" -eq 0 ] && return 1
  36. # increase pid file count to handle multiple instances correctly
  37. PIDCOUNT="$(( ${PIDCOUNT} + 1))"
  38. local pid_file="/var/run/${NAME}.${PIDCOUNT}.pid"
  39. procd_open_instance
  40. procd_set_param command "$PROG" -F -P "$pid_file"
  41. # prepare parameters (initialise with pid file)
  42. local val
  43. # A) password authentication
  44. config_get_bool val "${section}" PasswordAuth 1
  45. [ "${val}" -eq 0 ] && procd_append_param command -s
  46. # B) listen interface and port
  47. local port
  48. local interface
  49. config_get interface "${section}" Interface
  50. [ -n "$interface" ] && network_get_device interface "$interface"
  51. config_get port "${section}" Port 22
  52. append_ports "$interface" "$port"
  53. # C) banner file
  54. config_get val "${section}" BannerFile
  55. [ -f "${val}" ] && procd_append_param command -b "${val}"
  56. # D) gatewayports
  57. config_get_bool val "${section}" GatewayPorts 0
  58. [ "${val}" -eq 1 ] && procd_append_param command -a
  59. # E) root password authentication
  60. config_get_bool val "${section}" RootPasswordAuth 1
  61. [ "${val}" -eq 0 ] && procd_append_param command -g
  62. # F) root login
  63. config_get_bool val "${section}" RootLogin 1
  64. [ "${val}" -eq 0 ] && procd_append_param command -w
  65. # G) host keys
  66. config_get val "${section}" rsakeyfile
  67. [ -f "${val}" ] && procd_append_param command -r "${val}"
  68. config_get val "${section}" dsskeyfile
  69. [ -f "${val}" ] && procd_append_param command -d "${val}"
  70. procd_close_instance
  71. }
  72. keygen()
  73. {
  74. for keytype in rsa dss; do
  75. # check for keys
  76. key=dropbear/dropbear_${keytype}_host_key
  77. [ -f /tmp/$key -o -s /etc/$key ] || {
  78. # generate missing keys
  79. mkdir -p /tmp/dropbear
  80. [ -x /usr/bin/dropbearkey ] && {
  81. /usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start
  82. } &
  83. exit 0
  84. }
  85. done
  86. lock /tmp/.switch2jffs
  87. mkdir -p /etc/dropbear
  88. mv /tmp/dropbear/dropbear_* /etc/dropbear/
  89. lock -u /tmp/.switch2jffs
  90. chown root /etc/dropbear
  91. chmod 0700 /etc/dropbear
  92. }
  93. start_service()
  94. {
  95. [ -s /etc/dropbear/dropbear_rsa_host_key -a \
  96. -s /etc/dropbear/dropbear_dss_host_key ] || keygen
  97. . /lib/functions.sh
  98. . /lib/functions/network.sh
  99. config_load "${NAME}"
  100. config_foreach dropbear_instance dropbear
  101. }
  102. killclients()
  103. {
  104. local ignore=''
  105. local server
  106. local pid
  107. # if this script is run from inside a client session, then ignore that session
  108. pid="$$"
  109. while [ "${pid}" -ne 0 ]
  110. do
  111. # get parent process id
  112. pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
  113. [ "${pid}" -eq 0 ] && break
  114. # check if client connection
  115. grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
  116. append ignore "${pid}"
  117. break
  118. }
  119. done
  120. # get all server pids that should be ignored
  121. for server in `cat /var/run/${NAME}.*.pid`
  122. do
  123. append ignore "${server}"
  124. done
  125. # get all running pids and kill client connections
  126. local skip
  127. for pid in `pidof "${NAME}"`
  128. do
  129. # check if correct program, otherwise process next pid
  130. grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
  131. continue
  132. }
  133. # check if pid should be ignored (servers, ourself)
  134. skip=0
  135. for server in ${ignore}
  136. do
  137. if [ "${pid}" == "${server}" ]
  138. then
  139. skip=1
  140. break
  141. fi
  142. done
  143. [ "${skip}" -ne 0 ] && continue
  144. # kill process
  145. echo "${initscript}: Killing ${pid}..."
  146. kill -KILL ${pid}
  147. done
  148. }