dropbear.init 4.6 KB

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