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