]> git.pld-linux.org Git - packages/openssh.git/blame - opensshd.init
- formatting and cosmetics, unify actions order
[packages/openssh.git] / opensshd.init
CommitLineData
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
cf3b46d6
AF
11# Source function library
12. /etc/rc.d/init.d/functions
13
14# Get network config
15. /etc/sysconfig/network
16
3cd7ffe2
ER
17SSHD_OOM_ADJUST=-1000
18
cf3b46d6
AF
19# Get service config
20[ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd
21
22# Check that networking is up.
7d58fbb0 23if 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
28else
29 exit 0
cf3b46d6 30fi
a1c37c17 31
32322335 32adjust_oom() {
141073f0
ER
33 if [ -e /var/run/sshd.pid ]; then
34 for pid in $(cat /var/run/sshd.pid); do
9172cbe8 35 echo "$SSHD_OOM_ADJUST" 2>/dev/null > /proc/$pid/oom_score_adj
141073f0
ER
36 done
37 fi
32322335
AM
38}
39
945a8076
ER
40checkconfig() {
41 /usr/sbin/sshd -t || exit 1
42}
43
e6635719 44ssh_gen_keys() {
3c573fc0 45 # generate new keys with empty passwords if they do not exist
46 if [ ! -f /etc/ssh/ssh_host_key -o ! -s /etc/ssh/ssh_host_key ]; then
e6635719 47 /usr/bin/ssh-keygen -t rsa1 -f /etc/ssh/ssh_host_key -N '' >&2
7d58fbb0 48 chmod 600 /etc/ssh/ssh_host_key
e6635719 49 [ -x /sbin/restorecon ] && /sbin/restorecon /etc/ssh/ssh_host_key
3c573fc0 50 fi
51 if [ ! -f /etc/ssh/ssh_host_rsa_key -o ! -s /etc/ssh/ssh_host_rsa_key ]; then
e6635719 52 /usr/bin/ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N '' >&2
7d58fbb0 53 chmod 600 /etc/ssh/ssh_host_rsa_key
e6635719 54 [ -x /sbin/restorecon ] && /sbin/restorecon /etc/ssh/ssh_host_rsa_key
3c573fc0 55 fi
56 if [ ! -f /etc/ssh/ssh_host_dsa_key -o ! -s /etc/ssh/ssh_host_dsa_key ]; then
e6635719 57 /usr/bin/ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N '' >&2
7d58fbb0 58 chmod 600 /etc/ssh/ssh_host_dsa_key
e6635719
ER
59 [ -x /sbin/restorecon ] && /sbin/restorecon /etc/ssh/ssh_host_dsa_key
60 fi
7b384e20
AM
61 if [ ! -f /etc/ssh/ssh_host_ecdsa_key -o ! -s /etc/ssh/ssh_host_ecdsa_key ]; then
62 /usr/bin/ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N '' >&2
63 chmod 600 /etc/ssh/ssh_host_ecdsa_key
64 [ -x /sbin/restorecon ] && /sbin/restorecon /etc/ssh/ssh_host_ecdsa_key
65 fi
e6635719
ER
66}
67
68start() {
69 # Check if the service is already running?
70 if [ -f /var/lock/subsys/sshd ]; then
71 msg_already_running "OpenSSH"
72 return
3c573fc0 73 fi
74
e6635719 75 ssh_gen_keys
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
98stop() {
3cd7ffe2 99 if [ ! -f /var/lock/subsys/sshd ]; then
e6635719 100 msg_not_running "OpenSSH"
3cd7ffe2 101 return
a1c37c17 102 fi
3cd7ffe2
ER
103
104 msg_stopping "OpenSSH"
105 # we use start-stop-daemon to stop sshd, as it is unacceptable for such
106 # critical service as sshd to kill it by procname, but unfortunately
107 # rc-scripts does not provide way to kill *only* by pidfile
108 start-stop-daemon --stop --quiet --pidfile /var/run/sshd.pid && ok || fail
109 rm -f /var/lock/subsys/sshd >/dev/null 2>&1
110}
111
112reload() {
113 if [ ! -f /var/lock/subsys/sshd ]; then
114 msg_not_running "OpenSSH"
115 RETVAL=7
116 return
117 fi
118
119 checkconfig
120 msg_reloading "OpenSSH"
121 killproc sshd -HUP
122 RETVAL=$?
b10eed65
ER
123}
124
c0f446a0
JK
125upstart_controlled --except init configtest
126
b10eed65
ER
127RETVAL=0
128# See how we were called.
129case "$1" in
130 start)
131 start
132 ;;
133 stop)
134 stop
cf3b46d6 135 ;;
cbd44157 136 restart)
945a8076 137 checkconfig
b10eed65
ER
138 stop
139 start
cf3b46d6 140 ;;
3cd7ffe2
ER
141 reload|force-reload)
142 reload
143 ;;
144 configtest)
145 checkconfig
cf3b46d6 146 ;;
01d1f289 147 init)
0d883194 148 nls "Now the SSH host key will be generated. Please note, that if you"
149 nls "will use password for the key, you will need to type it on each"
150 nls "reboot."
e6635719 151 ssh_gen_keys
36f63877 152 ;;
3cd7ffe2
ER
153 status)
154 status sshd
155 exit $?
36f63877 156 ;;
cf3b46d6 157 *)
3cd7ffe2 158 msg_usage "$0 {start|stop|restart|reload|force-reload|configtest|init|status}"
bff0c7f8 159 exit 3
cf3b46d6
AF
160esac
161
cbd44157 162exit $RETVAL
This page took 0.069329 seconds and 4 git commands to generate.