dropbear.init 4.1 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. PROG=/usr/sbin/dropbear
  8. NAME=dropbear
  9. PIDCOUNT=0
  10. EXTRA_COMMANDS="killclients"
  11. EXTRA_HELP=" killclients Kill ${NAME} processes except servers and yourself"
  12. append_ports()
  13. {
  14. local ifname="$1"
  15. local port="$2"
  16. grep -qs "^ *$ifname:" /proc/net/dev || {
  17. procd_append_param command -p "$port"
  18. return
  19. }
  20. for addr in $(
  21. ifconfig "$ifname" | sed -ne '
  22. /addr: *fe[89ab][0-9a-f]:/d
  23. s/.* addr: *\([0-9a-f:\.]*\).*/\1/p
  24. '
  25. ); do
  26. procd_append_param command -p "$addr:$port"
  27. done
  28. }
  29. validate_section_dropbear()
  30. {
  31. uci_validate_section dropbear dropbear "${1}" \
  32. 'PasswordAuth:bool:1' \
  33. 'enable:bool:1' \
  34. 'Interface:string' \
  35. 'GatewayPorts:bool:0' \
  36. 'RootPasswordAuth:bool:1' \
  37. 'RootLogin:bool:1' \
  38. 'rsakeyfile:file' \
  39. 'dsskeyfile:file' \
  40. 'BannerFile:file' \
  41. 'Port:list(port):22' \
  42. 'SSHKeepAlive:uinteger:300' \
  43. 'IdleTimeout:uinteger:0'
  44. }
  45. dropbear_instance()
  46. {
  47. local PasswordAuth enable Interface GatewayPorts \
  48. RootPasswordAuth RootLogin rsakeyfile \
  49. dsskeyfile BannerFile Port SSHKeepAlive IdleTimeout
  50. validate_section_dropbear "${1}" || {
  51. echo "validation failed"
  52. return 1
  53. }
  54. [ "${enable}" = "0" ] && return 1
  55. PIDCOUNT="$(( ${PIDCOUNT} + 1))"
  56. local pid_file="/var/run/${NAME}.${PIDCOUNT}.pid"
  57. procd_open_instance
  58. procd_set_param command "$PROG" -F -P "$pid_file"
  59. [ "${PasswordAuth}" -eq 0 ] && procd_append_param command -s
  60. [ "${GatewayPorts}" -eq 1 ] && procd_append_param command -a
  61. [ "${RootPasswordAuth}" -eq 0 ] && procd_append_param command -g
  62. [ "${RootLogin}" -eq 0 ] && procd_append_param command -w
  63. [ -n "${rsakeyfile}" ] && procd_append_param command -r "${rsakeyfile}"
  64. [ -n "${dsskeyfile}" ] && procd_append_param command -d "${dsskeyfile}"
  65. [ -n "${BannerFile}" ] && procd_append_param command -b "${BannerFile}"
  66. [ -n "${Interface}" ] && network_get_device Interface "${Interface}"
  67. append_ports "${Interface}" "${Port}"
  68. [ "${IdleTimeout}" -ne 0 ] && procd_append_param command -I "${IdleTimeout}"
  69. [ "${SSHKeepAlive}" -ne 0 ] && procd_append_param command -K "${SSHKeepAlive}"
  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. service_triggers()
  103. {
  104. procd_add_reload_trigger "dropbear"
  105. procd_add_validation validate_section_dropbear
  106. }
  107. killclients()
  108. {
  109. local ignore=''
  110. local server
  111. local pid
  112. # if this script is run from inside a client session, then ignore that session
  113. pid="$$"
  114. while [ "${pid}" -ne 0 ]
  115. do
  116. # get parent process id
  117. pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
  118. [ "${pid}" -eq 0 ] && break
  119. # check if client connection
  120. grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
  121. append ignore "${pid}"
  122. break
  123. }
  124. done
  125. # get all server pids that should be ignored
  126. for server in `cat /var/run/${NAME}.*.pid`
  127. do
  128. append ignore "${server}"
  129. done
  130. # get all running pids and kill client connections
  131. local skip
  132. for pid in `pidof "${NAME}"`
  133. do
  134. # check if correct program, otherwise process next pid
  135. grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
  136. continue
  137. }
  138. # check if pid should be ignored (servers, ourself)
  139. skip=0
  140. for server in ${ignore}
  141. do
  142. if [ "${pid}" == "${server}" ]
  143. then
  144. skip=1
  145. break
  146. fi
  147. done
  148. [ "${skip}" -ne 0 ] && continue
  149. # kill process
  150. echo "${initscript}: Killing ${pid}..."
  151. kill -KILL ${pid}
  152. done
  153. }