]>
Commit | Line | Data |
---|---|---|
cf3b46d6 AF |
1 | #!/bin/sh |
2 | # | |
3 | # sshd sshd (secure shell daemon) | |
4 | # | |
fd04e715 | 5 | # chkconfig: 345 21 89 |
cf3b46d6 | 6 | # |
4a9f24b4 | 7 | # description: sshd (secure shell daemon) is a server part of the ssh suite. \ |
8 | # Ssh can be used for remote login, remote file copying, TCP port \ | |
cf3b46d6 AF |
9 | # forwarding etc. Ssh offers strong encryption and authentication. |
10 | ||
32322335 | 11 | SSHD_OOM_ADJUST=-17 |
cf3b46d6 AF |
12 | |
13 | # Source function library | |
14 | . /etc/rc.d/init.d/functions | |
15 | ||
16 | # Get network config | |
17 | . /etc/sysconfig/network | |
18 | ||
19 | # Get service config | |
20 | [ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd | |
21 | ||
22 | # Check that networking is up. | |
7d58fbb0 | 23 | if is_yes "${NETWORKING}"; then |
224aaee1 | 24 | if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status -a "$1" != init ]; then |
e6635719 | 25 | msg_network_down "OpenSSH" |
7d58fbb0 | 26 | exit 1 |
27 | fi | |
28 | else | |
29 | exit 0 | |
cf3b46d6 | 30 | fi |
a1c37c17 | 31 | |
32322335 | 32 | adjust_oom() { |
141073f0 ER |
33 | if [ -e /var/run/sshd.pid ]; then |
34 | for pid in $(cat /var/run/sshd.pid); do | |
bde01d30 AM |
35 | if [ -e /proc/$pid/oom_score_adj ]; then |
36 | echo "$SSHD_OOM_ADJUST" 2>/dev/null > /proc/$pid/oom_score_adj | |
37 | else | |
38 | echo "$SSHD_OOM_ADJUST" 2>/dev/null > /proc/$pid/oom_adj | |
39 | fi | |
141073f0 ER |
40 | done |
41 | fi | |
32322335 AM |
42 | } |
43 | ||
945a8076 ER |
44 | checkconfig() { |
45 | /usr/sbin/sshd -t || exit 1 | |
46 | } | |
47 | ||
e6635719 | 48 | ssh_gen_keys() { |
3c573fc0 | 49 | # generate new keys with empty passwords if they do not exist |
50 | if [ ! -f /etc/ssh/ssh_host_key -o ! -s /etc/ssh/ssh_host_key ]; then | |
e6635719 | 51 | /usr/bin/ssh-keygen -t rsa1 -f /etc/ssh/ssh_host_key -N '' >&2 |
7d58fbb0 | 52 | chmod 600 /etc/ssh/ssh_host_key |
e6635719 | 53 | [ -x /sbin/restorecon ] && /sbin/restorecon /etc/ssh/ssh_host_key |
3c573fc0 | 54 | fi |
55 | if [ ! -f /etc/ssh/ssh_host_rsa_key -o ! -s /etc/ssh/ssh_host_rsa_key ]; then | |
e6635719 | 56 | /usr/bin/ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N '' >&2 |
7d58fbb0 | 57 | chmod 600 /etc/ssh/ssh_host_rsa_key |
e6635719 | 58 | [ -x /sbin/restorecon ] && /sbin/restorecon /etc/ssh/ssh_host_rsa_key |
3c573fc0 | 59 | fi |
60 | if [ ! -f /etc/ssh/ssh_host_dsa_key -o ! -s /etc/ssh/ssh_host_dsa_key ]; then | |
e6635719 | 61 | /usr/bin/ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N '' >&2 |
7d58fbb0 | 62 | chmod 600 /etc/ssh/ssh_host_dsa_key |
e6635719 ER |
63 | [ -x /sbin/restorecon ] && /sbin/restorecon /etc/ssh/ssh_host_dsa_key |
64 | fi | |
e6635719 ER |
65 | } |
66 | ||
67 | start() { | |
68 | # Check if the service is already running? | |
69 | if [ -f /var/lock/subsys/sshd ]; then | |
70 | msg_already_running "OpenSSH" | |
71 | return | |
3c573fc0 | 72 | fi |
73 | ||
e6635719 ER |
74 | ssh_gen_keys |
75 | ||
945a8076 ER |
76 | checkconfig |
77 | ||
e6635719 ER |
78 | if [ ! -s /etc/ssh/ssh_host_key ]; then |
79 | msg_not_running "OpenSSH" | |
7d58fbb0 | 80 | nls "No SSH host key found! You must run \"%s init\" first." "$0" |
01d1f289 | 81 | exit 1 |
82 | fi | |
3c573fc0 | 83 | |
1292c55e ER |
84 | if is_yes "$IPV4_NETWORKING" && is_no "$IPV6_NETWORKING"; then |
85 | OPTIONS="$OPTIONS -4" | |
86 | fi | |
87 | if is_yes "$IPV6_NETWORKING" && is_no "$IPV4_NETWORKING"; then | |
88 | OPTIONS="$OPTIONS -6" | |
89 | fi | |
90 | ||
e6635719 | 91 | msg_starting "OpenSSH" |
1292c55e | 92 | daemon --pidfile /var/run/sshd.pid /usr/sbin/sshd $OPTIONS |
e6635719 ER |
93 | RETVAL=$? |
94 | adjust_oom | |
95 | [ $RETVAL -eq 0 ] && touch /var/lock/subsys/sshd | |
b10eed65 ER |
96 | } |
97 | ||
98 | stop() { | |
7d58fbb0 | 99 | if [ -f /var/lock/subsys/sshd ]; then |
e6635719 | 100 | msg_stopping "OpenSSH" |
3007cef9 ER |
101 | # we use start-stop-daemon to stop sshd, as it is unacceptable for such |
102 | # critical service as sshd to kill it by procname, but unfortunately | |
103 | # rc-scripts does not provide way to kill *only* by pidfile | |
104 | start-stop-daemon --stop --quiet --pidfile /var/run/sshd.pid && ok || fail | |
105 | rm -f /var/lock/subsys/sshd >/dev/null 2>&1 | |
cbd44157 | 106 | else |
e6635719 | 107 | msg_not_running "OpenSSH" |
a1c37c17 | 108 | fi |
b10eed65 ER |
109 | } |
110 | ||
c0f446a0 JK |
111 | upstart_controlled --except init configtest |
112 | ||
b10eed65 ER |
113 | RETVAL=0 |
114 | # See how we were called. | |
115 | case "$1" in | |
116 | start) | |
117 | start | |
118 | ;; | |
119 | stop) | |
120 | stop | |
cf3b46d6 | 121 | ;; |
cbd44157 | 122 | restart) |
945a8076 | 123 | checkconfig |
b10eed65 ER |
124 | stop |
125 | start | |
cf3b46d6 AF |
126 | ;; |
127 | status) | |
128 | status sshd | |
cbd44157 | 129 | exit $? |
cf3b46d6 | 130 | ;; |
01d1f289 | 131 | init) |
0d883194 | 132 | nls "Now the SSH host key will be generated. Please note, that if you" |
133 | nls "will use password for the key, you will need to type it on each" | |
134 | nls "reboot." | |
e6635719 | 135 | ssh_gen_keys |
36f63877 | 136 | ;; |
c0f446a0 JK |
137 | configtest) |
138 | checkconfig | |
139 | ;; | |
bff0c7f8 | 140 | reload|force-reload) |
0d883194 | 141 | if [ -f /var/lock/subsys/sshd ]; then |
d1017959 | 142 | checkconfig |
e6635719 | 143 | msg_reloading "OpenSSH" |
0d883194 | 144 | killproc sshd -HUP |
145 | RETVAL=$? | |
0d883194 | 146 | else |
e6635719 | 147 | msg_not_running "OpenSSH" |
bff0c7f8 | 148 | exit 7 |
0d883194 | 149 | fi |
36f63877 | 150 | ;; |
cf3b46d6 | 151 | *) |
bff0c7f8 | 152 | msg_usage "$0 {start|stop|init|restart|reload|force-reload|status}" |
153 | exit 3 | |
cf3b46d6 AF |
154 | esac |
155 | ||
cbd44157 | 156 | exit $RETVAL |