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