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