sshd.init.old 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/bin/bash
  2. #
  3. # Init file for OpenSSH server daemon
  4. #
  5. # chkconfig: 2345 55 25
  6. # description: OpenSSH server daemon
  7. #
  8. # processname: sshd
  9. # config: /etc/ssh/ssh_host_key
  10. # config: /etc/ssh/ssh_host_key.pub
  11. # config: /etc/ssh/ssh_random_seed
  12. # config: /etc/ssh/sshd_config
  13. # pidfile: /var/run/sshd.pid
  14. # source function library
  15. . /etc/rc.d/init.d/functions
  16. # pull in sysconfig settings
  17. [ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd
  18. RETVAL=0
  19. prog="sshd"
  20. # Some functions to make the below more readable
  21. KEYGEN=/usr/bin/ssh-keygen
  22. SSHD=/usr/sbin/sshd
  23. RSA_KEY=/etc/ssh/ssh_host_rsa_key
  24. DSA_KEY=/etc/ssh/ssh_host_dsa_key
  25. PID_FILE=/var/run/sshd.pid
  26. my_success() {
  27. local msg
  28. if [ $# -gt 1 ]; then
  29. msg="$2"
  30. else
  31. msg="done"
  32. fi
  33. case "`type -type success`" in
  34. function)
  35. success "$1"
  36. ;;
  37. *)
  38. echo -n "${msg}"
  39. ;;
  40. esac
  41. }
  42. my_failure() {
  43. local msg
  44. if [ $# -gt 1 ]; then
  45. msg="$2"
  46. else
  47. msg="FAILED"
  48. fi
  49. case "`type -type failure`" in
  50. function)
  51. failure "$1"
  52. ;;
  53. *)
  54. echo -n "${msg}"
  55. ;;
  56. esac
  57. }
  58. do_rsa_keygen() {
  59. if [ ! -s $RSA_KEY ]; then
  60. echo -n "Generating SSH2 RSA host key: "
  61. if $KEYGEN -q -t rsa -f $RSA_KEY -C '' -N '' >&/dev/null; then
  62. chmod 600 $RSA_KEY
  63. chmod 644 $RSA_KEY.pub
  64. my_success "RSA key generation"
  65. echo
  66. else
  67. my_failure "RSA key generation"
  68. echo
  69. exit 1
  70. fi
  71. fi
  72. }
  73. do_dsa_keygen() {
  74. if [ ! -s $DSA_KEY ]; then
  75. echo -n "Generating SSH2 DSA host key: "
  76. if $KEYGEN -q -t dsa -f $DSA_KEY -C '' -N '' >&/dev/null; then
  77. chmod 600 $DSA_KEY
  78. chmod 644 $DSA_KEY.pub
  79. my_success "DSA key generation"
  80. echo
  81. else
  82. my_failure "DSA key generation"
  83. echo
  84. exit 1
  85. fi
  86. fi
  87. }
  88. do_restart_sanity_check() {
  89. $SSHD -t
  90. RETVAL=$?
  91. if [ ! "$RETVAL" = 0 ]; then
  92. my_failure "Configuration file or keys"
  93. echo
  94. fi
  95. }
  96. case "$1" in
  97. start)
  98. # Create keys if necessary
  99. do_rsa_keygen;
  100. do_dsa_keygen;
  101. echo -n "Starting sshd: "
  102. if [ ! -f $PID_FILE ] ; then
  103. sshd $OPTIONS
  104. RETVAL=$?
  105. if [ "$RETVAL" = "0" ] ; then
  106. my_success "sshd startup" "sshd"
  107. touch /var/lock/subsys/sshd
  108. else
  109. my_failure "sshd startup" ""
  110. fi
  111. fi
  112. echo
  113. ;;
  114. stop)
  115. echo -n "Shutting down sshd: "
  116. if [ -f $PID_FILE ] ; then
  117. killproc sshd
  118. RETVAL=$?
  119. [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/sshd
  120. fi
  121. echo
  122. ;;
  123. restart)
  124. do_restart_sanity_check
  125. $0 stop
  126. $0 start
  127. RETVAL=$?
  128. ;;
  129. condrestart)
  130. if [ -f /var/lock/subsys/sshd ] ; then
  131. do_restart_sanity_check
  132. $0 stop
  133. $0 start
  134. RETVAL=$?
  135. fi
  136. ;;
  137. status)
  138. status sshd
  139. RETVAL=$?
  140. ;;
  141. *)
  142. echo "Usage: sshd {start|stop|restart|status|condrestart}"
  143. exit 1
  144. ;;
  145. esac
  146. exit $RETVAL