sshd.init 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #!/bin/bash
  2. #
  3. # sshd Start up the OpenSSH server daemon
  4. #
  5. # chkconfig: 2345 55 25
  6. # description: SSH is a protocol for secure remote shell access. \
  7. # This service starts up the OpenSSH server daemon.
  8. #
  9. # processname: sshd
  10. # config: /etc/ssh/ssh_host_key
  11. # config: /etc/ssh/ssh_host_key.pub
  12. # config: /etc/ssh/ssh_random_seed
  13. # config: /etc/ssh/sshd_config
  14. # pidfile: /var/run/sshd.pid
  15. ### BEGIN INIT INFO
  16. # Provides: sshd
  17. # Required-Start: $local_fs $network $syslog
  18. # Required-Stop: $local_fs $syslog
  19. # Should-Start: $syslog
  20. # Should-Stop: $network $syslog
  21. # Default-Start: 2 3 4 5
  22. # Default-Stop: 0 1 6
  23. # Short-Description: Start up the OpenSSH server daemon
  24. # Description: SSH is a protocol for secure remote shell access.
  25. # This service starts up the OpenSSH server daemon.
  26. ### END INIT INFO
  27. # source function library
  28. . /etc/rc.d/init.d/functions
  29. # pull in sysconfig settings
  30. [ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd
  31. RETVAL=0
  32. prog="sshd"
  33. lockfile=/var/lock/subsys/$prog
  34. # Some functions to make the below more readable
  35. KEYGEN=/usr/bin/ssh-keygen
  36. SSHD=/usr/sbin/sshd
  37. RSA1_KEY=/etc/ssh/ssh_host_key
  38. RSA_KEY=/etc/ssh/ssh_host_rsa_key
  39. DSA_KEY=/etc/ssh/ssh_host_dsa_key
  40. ECDSA_KEY=/etc/ssh/ssh_host_ecdsa_key
  41. PID_FILE=/var/run/sshd.pid
  42. runlevel=$(set -- $(runlevel); eval "echo \$$#" )
  43. do_rsa1_keygen() {
  44. if [ ! -s $RSA1_KEY ]; then
  45. echo -n $"Generating SSH1 RSA host key: "
  46. rm -f $RSA1_KEY
  47. if test ! -f $RSA1_KEY && $KEYGEN -q -t rsa1 -f $RSA1_KEY -C '' -N '' >&/dev/null; then
  48. chmod 600 $RSA1_KEY
  49. chmod 644 $RSA1_KEY.pub
  50. if [ -x /sbin/restorecon ]; then
  51. /sbin/restorecon $RSA1_KEY.pub
  52. fi
  53. success $"RSA1 key generation"
  54. echo
  55. else
  56. failure $"RSA1 key generation"
  57. echo
  58. exit 1
  59. fi
  60. fi
  61. }
  62. do_rsa_keygen() {
  63. if [ ! -s $RSA_KEY ]; then
  64. echo -n $"Generating SSH2 RSA host key: "
  65. rm -f $RSA_KEY
  66. if test ! -f $RSA_KEY && $KEYGEN -q -t rsa -f $RSA_KEY -C '' -N '' >&/dev/null; then
  67. chmod 600 $RSA_KEY
  68. chmod 644 $RSA_KEY.pub
  69. if [ -x /sbin/restorecon ]; then
  70. /sbin/restorecon $RSA_KEY.pub
  71. fi
  72. success $"RSA key generation"
  73. echo
  74. else
  75. failure $"RSA key generation"
  76. echo
  77. exit 1
  78. fi
  79. fi
  80. }
  81. do_dsa_keygen() {
  82. if [ ! -s $DSA_KEY ]; then
  83. echo -n $"Generating SSH2 DSA host key: "
  84. rm -f $DSA_KEY
  85. if test ! -f $DSA_KEY && $KEYGEN -q -t dsa -f $DSA_KEY -C '' -N '' >&/dev/null; then
  86. chmod 600 $DSA_KEY
  87. chmod 644 $DSA_KEY.pub
  88. if [ -x /sbin/restorecon ]; then
  89. /sbin/restorecon $DSA_KEY.pub
  90. fi
  91. success $"DSA key generation"
  92. echo
  93. else
  94. failure $"DSA key generation"
  95. echo
  96. exit 1
  97. fi
  98. fi
  99. }
  100. do_ecdsa_keygen() {
  101. if [ ! -s $ECDSA_KEY ]; then
  102. echo -n $"Generating SSH2 ECDSA host key: "
  103. rm -f $ECDSA_KEY
  104. if test ! -f $ECDSA_KEY && $KEYGEN -q -t ecdsa -f $ECDSA_KEY -C '' -N '' >&/dev/null; then
  105. chmod 600 $ECDSA_KEY
  106. chmod 644 $ECDSA_KEY.pub
  107. if [ -x /sbin/restorecon ]; then
  108. /sbin/restorecon $ECDSA_KEY.pub
  109. fi
  110. success $"ECDSA key generation"
  111. echo
  112. else
  113. failure $"ECDSA key generation"
  114. echo
  115. exit 1
  116. fi
  117. fi
  118. }
  119. do_restart_sanity_check()
  120. {
  121. $SSHD -t
  122. RETVAL=$?
  123. if [ $RETVAL -ne 0 ]; then
  124. failure $"Configuration file or keys are invalid"
  125. echo
  126. fi
  127. }
  128. start() {
  129. [ -x $SSHD ] || exit 5
  130. [ -f /etc/ssh/sshd_config ] || exit 6
  131. # Create keys if necessary
  132. if [ "x${AUTOCREATE_SERVER_KEYS}" != xNO ]; then
  133. do_rsa_keygen
  134. if [ "x${AUTOCREATE_SERVER_KEYS}" != xRSAONLY ]; then
  135. do_rsa1_keygen
  136. do_dsa_keygen
  137. do_ecdsa_keygen
  138. fi
  139. fi
  140. cp -af /etc/localtime /var/empty/sshd/etc
  141. echo -n $"Starting $prog: "
  142. $SSHD $OPTIONS && success || failure
  143. RETVAL=$?
  144. [ $RETVAL -eq 0 ] && touch $lockfile
  145. echo
  146. return $RETVAL
  147. }
  148. stop() {
  149. echo -n $"Stopping $prog: "
  150. if [ -n "`pidfileofproc $SSHD`" ] ; then
  151. killproc $SSHD
  152. else
  153. failure $"Stopping $prog"
  154. fi
  155. RETVAL=$?
  156. # if we are in halt or reboot runlevel kill all running sessions
  157. # so the TCP connections are closed cleanly
  158. if [ "x$runlevel" = x0 -o "x$runlevel" = x6 ] ; then
  159. trap '' TERM
  160. killall $prog 2>/dev/null
  161. trap TERM
  162. fi
  163. [ $RETVAL -eq 0 ] && rm -f $lockfile
  164. echo
  165. }
  166. reload() {
  167. echo -n $"Reloading $prog: "
  168. if [ -n "`pidfileofproc $SSHD`" ] ; then
  169. killproc $SSHD -HUP
  170. else
  171. failure $"Reloading $prog"
  172. fi
  173. RETVAL=$?
  174. echo
  175. }
  176. restart() {
  177. stop
  178. start
  179. }
  180. force_reload() {
  181. restart
  182. }
  183. do_status() {
  184. status -p $PID_FILE openssh-daemon
  185. }
  186. is_running() {
  187. do_status >/dev/null 2>&1
  188. }
  189. case "$1" in
  190. start)
  191. is_running && exit 0
  192. start
  193. ;;
  194. stop)
  195. if ! is_running; then
  196. rm -f $lockfile
  197. exit 0
  198. fi
  199. stop
  200. ;;
  201. restart)
  202. restart
  203. ;;
  204. reload)
  205. is_running || exit 7
  206. reload
  207. ;;
  208. force-reload)
  209. force_reload
  210. ;;
  211. condrestart|try-restart)
  212. is_running || exit 0
  213. if [ -f $lockfile ] ; then
  214. do_restart_sanity_check
  215. if [ $RETVAL -eq 0 ] ; then
  216. stop
  217. # avoid race
  218. sleep 3
  219. start
  220. else
  221. RETVAL=6
  222. fi
  223. fi
  224. ;;
  225. status)
  226. do_status
  227. RETVAL=$?
  228. if [ $RETVAL -eq 3 -a -f $lockfile ] ; then
  229. RETVAL=2
  230. fi
  231. ;;
  232. *)
  233. echo $"Usage: $0 {start|stop|restart|reload|force-reload|condrestart|try-restart|status}"
  234. RETVAL=2
  235. esac
  236. exit $RETVAL