dropbear.init 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. SERVICE_USE_PID=1
  7. NAME=dropbear
  8. PROG=/usr/sbin/dropbear
  9. PIDCOUNT=0
  10. EXTRA_COMMANDS="killclients"
  11. EXTRA_HELP=" killclients Kill ${NAME} processes except servers and yourself"
  12. dropbear_start()
  13. {
  14. append_ports()
  15. {
  16. local ifname="$1"
  17. local port="$2"
  18. grep -qs "^ *$ifname:" /proc/net/dev || {
  19. append args "-p $port"
  20. return
  21. }
  22. for addr in $(
  23. ifconfig "$ifname" | sed -ne '
  24. /addr: *fe[89ab][0-9a-f]:/d
  25. s/.* addr: *\([0-9a-f:\.]*\).*/\1/p
  26. '
  27. ); do
  28. append args "-p $addr:$port"
  29. done
  30. }
  31. local section="$1"
  32. # check if section is enabled (default)
  33. local enabled
  34. config_get_bool enabled "${section}" enable 1
  35. [ "${enabled}" -eq 0 ] && return 1
  36. # verbose parameter
  37. local verbosed
  38. config_get_bool verbosed "${section}" verbose 0
  39. # increase pid file count to handle multiple instances correctly
  40. PIDCOUNT="$(( ${PIDCOUNT} + 1))"
  41. # prepare parameters (initialise with pid file)
  42. local pid_file="/var/run/${NAME}.${PIDCOUNT}.pid"
  43. local args="-P $pid_file"
  44. local val
  45. # A) password authentication
  46. config_get_bool val "${section}" PasswordAuth 1
  47. [ "${val}" -eq 0 ] && append args "-s"
  48. # B) listen interface and port
  49. local port
  50. local interface
  51. config_get interface "${section}" Interface
  52. config_get interface "${interface}" ifname "$interface"
  53. config_get port "${section}" Port 22
  54. append_ports "$interface" "$port"
  55. # C) banner file
  56. config_get val "${section}" BannerFile
  57. [ -f "${val}" ] && append args "-b ${val}"
  58. # D) gatewayports
  59. config_get_bool val "${section}" GatewayPorts 0
  60. [ "${val}" -eq 1 ] && append args "-a"
  61. # E) root password authentication
  62. config_get_bool val "${section}" RootPasswordAuth 1
  63. [ "${val}" -eq 0 ] && append args "-g"
  64. # F) root login
  65. config_get_bool val "${section}" RootLogin 1
  66. [ "${val}" -eq 0 ] && append args "-w"
  67. # G) host keys
  68. config_get val "${section}" rsakeyfile
  69. [ -f "${val}" ] && append args "-r ${val}"
  70. config_get val "${section}" dsskeyfile
  71. [ -f "${val}" ] && append args "-d ${val}"
  72. # execute program and return its exit code
  73. [ "${verbosed}" -ne 0 ] && echo "${initscript}: section ${section} starting ${PROG} ${args}"
  74. SERVICE_PID_FILE="$pid_file" service_start ${PROG} ${args}
  75. }
  76. keygen()
  77. {
  78. for keytype in rsa dss; 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. start()
  98. {
  99. [ -s /etc/dropbear/dropbear_rsa_host_key -a \
  100. -s /etc/dropbear/dropbear_dss_host_key ] || keygen
  101. include /lib/network
  102. scan_interfaces
  103. config_load "${NAME}"
  104. config_foreach dropbear_start dropbear
  105. }
  106. stop()
  107. {
  108. local pid_file pid_files
  109. pid_files=`ls /var/run/${NAME}.*.pid 2>/dev/null`
  110. [ -z "$pid_files" ] && return 1
  111. for pid_file in $pid_files; do
  112. SERVICE_PID_FILE="$pid_file" service_stop ${PROG} && {
  113. rm -f ${pid_file}
  114. }
  115. done
  116. }
  117. killclients()
  118. {
  119. local ignore=''
  120. local server
  121. local pid
  122. # if this script is run from inside a client session, then ignore that session
  123. pid="$$"
  124. while [ "${pid}" -ne 0 ]
  125. do
  126. # get parent process id
  127. pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
  128. [ "${pid}" -eq 0 ] && break
  129. # check if client connection
  130. grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && {
  131. append ignore "${pid}"
  132. break
  133. }
  134. done
  135. # get all server pids that should be ignored
  136. for server in `cat /var/run/${NAME}.*.pid`
  137. do
  138. append ignore "${server}"
  139. done
  140. # get all running pids and kill client connections
  141. local skip
  142. for pid in `pidof "${NAME}"`
  143. do
  144. # check if correct program, otherwise process next pid
  145. grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
  146. continue
  147. }
  148. # check if pid should be ignored (servers, ourself)
  149. skip=0
  150. for server in ${ignore}
  151. do
  152. if [ "${pid}" == "${server}" ]
  153. then
  154. skip=1
  155. break
  156. fi
  157. done
  158. [ "${skip}" -ne 0 ] && continue
  159. # kill process
  160. echo "${initscript}: Killing ${pid}..."
  161. kill -KILL ${pid}
  162. done
  163. }