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