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