dropbear.init 4.4 KB

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