dropbear.init 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. return $?
  45. }
  46. dropbear_instance()
  47. {
  48. local PasswordAuth enable Interface GatewayPorts \
  49. RootPasswordAuth RootLogin rsakeyfile \
  50. dsskeyfile BannerFile Port SSHKeepAlive IdleTimeout
  51. validate_section_dropbear "${1}" || {
  52. echo "validation failed"
  53. return 1
  54. }
  55. [ "${enable}" = "0" ] && return 1
  56. PIDCOUNT="$(( ${PIDCOUNT} + 1))"
  57. local pid_file="/var/run/${NAME}.${PIDCOUNT}.pid"
  58. procd_open_instance
  59. procd_set_param command "$PROG" -F -P "$pid_file"
  60. [ "${PasswordAuth}" -eq 0 ] && procd_append_param command -s
  61. [ "${GatewayPorts}" -eq 1 ] && procd_append_param command -a
  62. [ "${RootPasswordAuth}" -eq 0 ] && procd_append_param command -g
  63. [ "${RootLogin}" -eq 0 ] && procd_append_param command -w
  64. [ -n "${rsakeyfile}" ] && procd_append_param command -r "${rsakeyfile}"
  65. [ -n "${dsskeyfile}" ] && procd_append_param command -d "${dsskeyfile}"
  66. [ -n "${BannerFile}" ] && procd_append_param command -b "${BannerFile}"
  67. [ -n "${Interface}" ] && network_get_device Interface "${Interface}"
  68. append_ports "${Interface}" "${Port}"
  69. [ "${IdleTimeout}" -ne 0 ] && procd_append_param command -I "${IdleTimeout}"
  70. [ "${SSHKeepAlive}" -ne 0 ] && procd_append_param command -K "${SSHKeepAlive}"
  71. procd_close_instance
  72. }
  73. keygen()
  74. {
  75. for keytype in rsa dss; do
  76. # check for keys
  77. key=dropbear/dropbear_${keytype}_host_key
  78. [ -f /tmp/$key -o -s /etc/$key ] || {
  79. # generate missing keys
  80. mkdir -p /tmp/dropbear
  81. [ -x /usr/bin/dropbearkey ] && {
  82. /usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start
  83. } &
  84. exit 0
  85. }
  86. done
  87. lock /tmp/.switch2jffs
  88. mkdir -p /etc/dropbear
  89. mv /tmp/dropbear/dropbear_* /etc/dropbear/
  90. lock -u /tmp/.switch2jffs
  91. chown root /etc/dropbear
  92. chmod 0700 /etc/dropbear
  93. }
  94. start_service()
  95. {
  96. [ -s /etc/dropbear/dropbear_rsa_host_key -a \
  97. -s /etc/dropbear/dropbear_dss_host_key ] || keygen
  98. . /lib/functions.sh
  99. . /lib/functions/network.sh
  100. config_load "${NAME}"
  101. config_foreach dropbear_instance dropbear
  102. }
  103. service_triggers()
  104. {
  105. procd_add_reload_trigger "dropbear"
  106. procd_add_validation validate_section_dropbear
  107. }
  108. killclients()
  109. {
  110. local ignore=''
  111. local server
  112. local pid
  113. # if this script is run from inside a client session, then ignore that session
  114. pid="$$"
  115. while [ "${pid}" -ne 0 ]
  116. do
  117. # get parent process id
  118. pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
  119. [ "${pid}" -eq 0 ] && break
  120. # check if client connection
  121. grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
  122. append ignore "${pid}"
  123. break
  124. }
  125. done
  126. # get all server pids that should be ignored
  127. for server in `cat /var/run/${NAME}.*.pid`
  128. do
  129. append ignore "${server}"
  130. done
  131. # get all running pids and kill client connections
  132. local skip
  133. for pid in `pidof "${NAME}"`
  134. do
  135. # check if correct program, otherwise process next pid
  136. grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
  137. continue
  138. }
  139. # check if pid should be ignored (servers, ourself)
  140. skip=0
  141. for server in ${ignore}
  142. do
  143. if [ "${pid}" == "${server}" ]
  144. then
  145. skip=1
  146. break
  147. fi
  148. done
  149. [ "${skip}" -ne 0 ] && continue
  150. # kill process
  151. echo "${initscript}: Killing ${pid}..."
  152. kill -KILL ${pid}
  153. done
  154. }