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