dropbear.init 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 ipaddrs="$1"
  15. local port="$2"
  16. [ -z "$ipaddrs" ] && {
  17. procd_append_param command -p "$port"
  18. return
  19. }
  20. for addr in $ipaddrs; do
  21. procd_append_param command -p "$addr:$port"
  22. done
  23. }
  24. validate_section_dropbear()
  25. {
  26. uci_validate_section dropbear dropbear "${1}" \
  27. 'PasswordAuth:bool:1' \
  28. 'enable:bool:1' \
  29. 'Interface:string' \
  30. 'GatewayPorts:bool:0' \
  31. 'RootPasswordAuth:bool:1' \
  32. 'RootLogin:bool:1' \
  33. 'rsakeyfile:file' \
  34. 'BannerFile:file' \
  35. 'Port:list(port):22' \
  36. 'SSHKeepAlive:uinteger:300' \
  37. 'IdleTimeout:uinteger:0' \
  38. 'mdns:uinteger:1'
  39. }
  40. dropbear_instance()
  41. {
  42. local PasswordAuth enable Interface GatewayPorts \
  43. RootPasswordAuth RootLogin rsakeyfile \
  44. BannerFile Port SSHKeepAlive IdleTimeout \
  45. mdns ipaddrs
  46. validate_section_dropbear "${1}" || {
  47. echo "validation failed"
  48. return 1
  49. }
  50. [ -n "${Interface}" ] && {
  51. network_get_ipaddrs_all ipaddrs "${Interface}" || {
  52. echo "interface ${Interface} has no physdev or physdev has no suitable ip"
  53. return 1
  54. }
  55. }
  56. [ "${enable}" = "0" ] && return 1
  57. PIDCOUNT="$(( ${PIDCOUNT} + 1))"
  58. local pid_file="/var/run/${NAME}.${PIDCOUNT}.pid"
  59. procd_open_instance
  60. procd_set_param command "$PROG" -F -P "$pid_file"
  61. [ "${PasswordAuth}" -eq 0 ] && procd_append_param command -s
  62. [ "${GatewayPorts}" -eq 1 ] && procd_append_param command -a
  63. [ "${RootPasswordAuth}" -eq 0 ] && procd_append_param command -g
  64. [ "${RootLogin}" -eq 0 ] && procd_append_param command -w
  65. [ -n "${rsakeyfile}" ] && procd_append_param command -r "${rsakeyfile}"
  66. [ -n "${BannerFile}" ] && procd_append_param command -b "${BannerFile}"
  67. append_ports "${ipaddrs}" "${Port}"
  68. [ "${IdleTimeout}" -ne 0 ] && procd_append_param command -I "${IdleTimeout}"
  69. [ "${SSHKeepAlive}" -ne 0 ] && procd_append_param command -K "${SSHKeepAlive}"
  70. [ "${mdns}" -ne 0 ] && procd_add_mdns "ssh" "tcp" "$Port" "daemon=dropbear"
  71. procd_set_param respawn
  72. procd_close_instance
  73. }
  74. keygen()
  75. {
  76. for keytype in rsa; do
  77. # check for keys
  78. key=dropbear/dropbear_${keytype}_host_key
  79. [ -f /tmp/$key -o -s /etc/$key ] || {
  80. # generate missing keys
  81. mkdir -p /tmp/dropbear
  82. [ -x /usr/bin/dropbearkey ] && {
  83. /usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start
  84. } &
  85. exit 0
  86. }
  87. done
  88. lock /tmp/.switch2jffs
  89. mkdir -p /etc/dropbear
  90. mv /tmp/dropbear/dropbear_* /etc/dropbear/
  91. lock -u /tmp/.switch2jffs
  92. chown root /etc/dropbear
  93. chmod 0700 /etc/dropbear
  94. }
  95. start_service()
  96. {
  97. [ -s /etc/dropbear/dropbear_rsa_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. }