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