dropbear.init 4.7 KB

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