dropbear.init 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. load_interfaces()
  96. {
  97. config_get interface "$1" Interface
  98. interfaces=" ${interface} ${interfaces}"
  99. }
  100. start_service()
  101. {
  102. [ -s /etc/dropbear/dropbear_rsa_host_key ] || keygen
  103. . /lib/functions.sh
  104. . /lib/functions/network.sh
  105. config_load "${NAME}"
  106. config_foreach dropbear_instance dropbear
  107. }
  108. service_triggers()
  109. {
  110. local interfaces
  111. procd_open_trigger
  112. procd_add_config_trigger "config.change" "dropbear" /etc/init.d/dropbear reload
  113. config_load "${NAME}"
  114. config_foreach load_interfaces dropbear
  115. [ -n "${interfaces}" ] & {
  116. for n in $interfaces ; do
  117. procd_add_interface_trigger "interface.*" $n /etc/init.d/dropbear reload
  118. done
  119. }
  120. procd_close_trigger
  121. procd_add_validation validate_section_dropbear
  122. }
  123. killclients()
  124. {
  125. local ignore=''
  126. local server
  127. local pid
  128. # if this script is run from inside a client session, then ignore that session
  129. pid="$$"
  130. while [ "${pid}" -ne 0 ]
  131. do
  132. # get parent process id
  133. pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
  134. [ "${pid}" -eq 0 ] && break
  135. # check if client connection
  136. grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
  137. append ignore "${pid}"
  138. break
  139. }
  140. done
  141. # get all server pids that should be ignored
  142. for server in `cat /var/run/${NAME}.*.pid`
  143. do
  144. append ignore "${server}"
  145. done
  146. # get all running pids and kill client connections
  147. local skip
  148. for pid in `pidof "${NAME}"`
  149. do
  150. # check if correct program, otherwise process next pid
  151. grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
  152. continue
  153. }
  154. # check if pid should be ignored (servers, ourself)
  155. skip=0
  156. for server in ${ignore}
  157. do
  158. if [ "${pid}" = "${server}" ]
  159. then
  160. skip=1
  161. break
  162. fi
  163. done
  164. [ "${skip}" -ne 0 ] && continue
  165. # kill process
  166. echo "${initscript}: Killing ${pid}..."
  167. kill -KILL ${pid}
  168. done
  169. }