dropbear.init 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2006-2010 OpenWrt.org
  3. # Copyright (C) 2006 Carlos Sobrinho
  4. START=19
  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_load_validate dropbear dropbear "$1" "$2" \
  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. 'MaxAuthTries:uinteger:3' \
  39. 'RecvWindowSize:uinteger:0' \
  40. 'mdns:bool:1'
  41. }
  42. dropbear_instance()
  43. {
  44. local ipaddrs
  45. [ "$2" = 0 ] || {
  46. echo "validation failed"
  47. return 1
  48. }
  49. [ -n "${Interface}" ] && {
  50. [ -n "${BOOT}" ] && return 0
  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. [ "${MaxAuthTries}" -ne 0 ] && procd_append_param command -T "${MaxAuthTries}"
  71. [ "${RecvWindowSize}" -gt 0 -a "${RecvWindowSize}" -le 1048576 ] && \
  72. procd_append_param command -W "${RecvWindowSize}"
  73. [ "${mdns}" -ne 0 ] && procd_add_mdns "ssh" "tcp" "$Port" "daemon=dropbear"
  74. procd_set_param respawn
  75. procd_close_instance
  76. }
  77. keygen()
  78. {
  79. for keytype in rsa; do
  80. # check for keys
  81. key=dropbear/dropbear_${keytype}_host_key
  82. [ -f /tmp/$key -o -s /etc/$key ] || {
  83. # generate missing keys
  84. mkdir -p /tmp/dropbear
  85. [ -x /usr/bin/dropbearkey ] && {
  86. /usr/bin/dropbearkey -t $keytype -f /tmp/$key 2>&- >&- && exec /etc/rc.common "$initscript" start
  87. } &
  88. exit 0
  89. }
  90. done
  91. lock /tmp/.switch2jffs
  92. mkdir -p /etc/dropbear
  93. mv /tmp/dropbear/dropbear_* /etc/dropbear/
  94. lock -u /tmp/.switch2jffs
  95. chown root /etc/dropbear
  96. chmod 0700 /etc/dropbear
  97. }
  98. load_interfaces()
  99. {
  100. config_get interface "$1" Interface
  101. config_get enable "$1" enable 1
  102. [ "${enable}" = "1" ] && interfaces=" ${interface} ${interfaces}"
  103. }
  104. boot()
  105. {
  106. BOOT=1
  107. start "$@"
  108. }
  109. start_service()
  110. {
  111. [ -s /etc/dropbear/dropbear_rsa_host_key ] || keygen
  112. . /lib/functions.sh
  113. . /lib/functions/network.sh
  114. config_load "${NAME}"
  115. config_foreach validate_section_dropbear dropbear dropbear_instance
  116. }
  117. service_triggers()
  118. {
  119. local interfaces
  120. procd_add_config_trigger "config.change" "dropbear" /etc/init.d/dropbear reload
  121. config_load "${NAME}"
  122. config_foreach load_interfaces dropbear
  123. [ -n "${interfaces}" ] && {
  124. for n in $interfaces ; do
  125. procd_add_interface_trigger "interface.*" $n /etc/init.d/dropbear reload
  126. done
  127. }
  128. procd_add_validation validate_section_dropbear
  129. }
  130. shutdown() {
  131. # close all open connections
  132. killall dropbear
  133. }
  134. killclients()
  135. {
  136. local ignore=''
  137. local server
  138. local pid
  139. # if this script is run from inside a client session, then ignore that session
  140. pid="$$"
  141. while [ "${pid}" -ne 0 ]
  142. do
  143. # get parent process id
  144. pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
  145. [ "${pid}" -eq 0 ] && break
  146. # check if client connection
  147. grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
  148. append ignore "${pid}"
  149. break
  150. }
  151. done
  152. # get all server pids that should be ignored
  153. for server in `cat /var/run/${NAME}.*.pid`
  154. do
  155. append ignore "${server}"
  156. done
  157. # get all running pids and kill client connections
  158. local skip
  159. for pid in `pidof "${NAME}"`
  160. do
  161. # check if correct program, otherwise process next pid
  162. grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
  163. continue
  164. }
  165. # check if pid should be ignored (servers, ourself)
  166. skip=0
  167. for server in ${ignore}
  168. do
  169. if [ "${pid}" = "${server}" ]
  170. then
  171. skip=1
  172. break
  173. fi
  174. done
  175. [ "${skip}" -ne 0 ] && continue
  176. # kill process
  177. echo "${initscript}: Killing ${pid}..."
  178. kill -KILL ${pid}
  179. done
  180. }