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