Subversion Repositories configs

Rev

Rev 6 | Rev 57 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 - 1
#!/bin/sh
2
# IPsec startup and shutdown script
3
#
4
### BEGIN INIT INFO
5
# Provides: ipsec
6
# Required-Start: $network $remote_fs $syslog $named
7
# Required-Stop: $syslog $remote_fs
8
# Default-Start:
9
# Default-Stop: 0 1 6
10
# Short-Description: Start Libreswan IPsec at boot time
11
# Description: Enable automatic key management for IPsec (KLIPS and NETKEY)
12
### END INIT INFO
13
#
14
### see https://bugzilla.redhat.com/show_bug.cgi?id=636572
15
### Debian and Fedora interpret the LSB differently for Default-Start:
16
 
17
# Copyright (C) 1998, 1999, 2001  Henry Spencer.
18
# Copyright (C) 2002              Michael Richardson <mcr@freeswan.org>
19
# Copyright (C) 2006              Michael Richardson <mcr@xelerance.com>
20
# Copyright (C) 2008              Michael Richardson <mcr@sandelman.ca>
12 - 21
# Copyright (C) 2008-2014         Tuomo Soini <tis@foobar.fi>
3 - 22
# Copyright (C) 2012              Paul Wouters <paul@libreswan.org>
23
#
24
# This program is free software; you can redistribute it and/or modify it
25
# under the terms of the GNU General Public License as published by the
26
# Free Software Foundation; either version 2 of the License, or (at your
27
# option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
28
#
29
# This program is distributed in the hope that it will be useful, but
30
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
31
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
32
# for more details.
33
#
34
# ipsec		sysv style init.d script for starting and stopping
35
#		the IPsec security subsystem (KLIPS and Pluto).
36
#
37
# This script becomes /etc/rc.d/init.d/ipsec
38
# and is also accessible as "ipsec setup"
39
#
40
# The startup and shutdown times are a difficult compromise (in particular,
41
# it is almost impossible to reconcile them with the insanely early/late
42
# times of NFS filesystem startup/shutdown).  Startup is after startup of
43
# syslog and pcmcia support; shutdown is just before shutdown of syslog.
44
#
45
# chkconfig: - 47 76
46
# description: IPsec provides encrypted and authenticated communications; \
47
# NETKEY/KLIPS is the kernel half of it, Pluto is the user-level management daemon.
48
 
49
test ${IPSEC_INIT_SCRIPT_DEBUG} && set -v -x
50
 
51
# Source function library.
52
. /etc/rc.d/init.d/functions
53
 
54
# Check that networking is up.
55
[ "${NETWORKING}" = "no" ] && exit 6
56
 
57
if [ ! -f /etc/sysconfig/network ]; then
58
    exit 6
59
fi
60
 
61
if [ $(id -u) -ne 0 ]; then
62
    echo "permission denied (must be superuser)" | \
63
	logger -s -p daemon.error -t ipsec_setup 2>&1
64
    exit 4
65
fi
66
 
67
if [ $(ip addr list | grep -c cipsec) -ne 0 ]; then
68
    echo "Cisco IPSec client is already loaded, aborting! (cipsec# device found)"
69
    exit 1
70
fi
71
 
72
# where the private directory and the config files are
73
IPSEC_CONF="${IPSEC_CONF:-/etc/ipsec.conf}"
74
IPSEC_EXECDIR="${IPSEC_EXECDIR:-/usr/libexec/ipsec}"
75
IPSEC_SBINDIR="${IPSEC_SBINDIR:-/usr/sbin}"
76
unset PLUTO_OPTIONS
77
 
78
rundir=/var/run/pluto
79
plutopid=${rundir}/pluto.pid
80
plutoctl=${rundir}/pluto.ctl
81
lockdir=/var/lock/subsys
82
lockfile=${lockdir}/ipsec
83
ipsecversion=/proc/net/ipsec_version
84
kamepfkey=/proc/net/pfkey
85
 
86
# /etc/resolv.conf related paths
87
LIBRESWAN_RESOLV_CONF=${rundir}/libreswan-resolv-conf-backup
88
ORIG_RESOLV_CONF=/etc/resolv.conf
89
 
90
# there is some confusion over the name - just do both
91
[ -f /etc/sysconfig/ipsec ] && . /etc/sysconfig/ipsec
92
[ -f /etc/sysconfig/pluto ] && . /etc/sysconfig/pluto
93
 
94
# misc setup
95
umask 022
96
 
97
# standardize PATH, and export it for everything else's benefit
98
PATH="${IPSEC_SBINDIR}:/sbin:/usr/sbin:/usr/local/bin:/bin:/usr/bin"
99
export PATH
100
 
101
mkdir -p ${rundir}
102
chmod 700 ${rundir}
103
 
104
verify_config() {
105
    [ -f ${IPSEC_CONF} ] || exit 6
106
 
107
    config_error=$(ipsec addconn --config ${IPSEC_CONF} --checkconfig 2>&1)
108
    RETVAL=$?
109
    if [ ${RETVAL} -gt 0 ]; then
110
	echo "Configuration error - the following error occured:"
111
	echo ${config_error}
112
	echo "IKE daemon status was not modified"
113
	exit ${RETVAL}
114
    fi
115
}
116
 
117
start() {
118
    echo -n $"Starting pluto IKE daemon for IPsec: "
119
    ipsec _stackmanager start
120
 
121
    # pluto searches the current directory, so this is required for making it selinux compliant
122
    cd /
123
    # This script will enter an endless loop to ensure pluto restarts on crash
124
    ipsec _plutorun --config ${IPSEC_CONF} --nofork ${PLUTO_OPTIONS} &
12 - 125
    [ -d ${lockdir} ] || mkdir -p ${lockdir}
126
    touch ${lockfile}
127
    # Because _plutorun starts pluto at background we need to make sure pluto is started
128
    # before we know if start was successful or not
129
    for waitsec in 1 2 3 4 5; do
130
	if status -p ${plutopid} -l ${lockfile} ${IPSEC_EXECDIR}/pluto >/dev/null; then
131
	    RETVAL=0
132
	    break
133
	else
134
	    echo -n "."
135
	    sleep 1
136
	    RETVAL=1
137
	fi
138
    done
3 - 139
    if [ ${RETVAL} -eq 0 ]; then
140
	success
141
    else
12 - 142
	rm -f ${lockfile}
3 - 143
	failure
144
    fi
145
    echo
146
    return ${RETVAL}
147
}
148
 
149
 
150
stop() {
151
    if [ ! -e ${plutoctl} ]; then
152
	echo "Missing control file ${plutoctl} - is pluto running?"
153
    else
154
	echo $"Shutting down pluto IKE daemon"
155
	ipsec whack --shutdown
156
	# don't use seq, might not exist on embedded
157
	for waitsec in 1 2 3 4 5 6 7 8 9 10; do
158
	    if [ -s ${plutopid} ]; then
159
		echo -n "."
160
		sleep 1
161
	    else
162
		break
163
	    fi
164
	done
165
	echo
166
	rm -f ${plutoctl} # we won't be using this anymore
167
    fi
168
    if [ -s ${plutopid} ]; then
169
	# pluto did not die peacefully
170
	echo "Attempt to shut Pluto down failed!  Trying kill"
171
	killproc -p ${plutopid} ${IPSEC_EXECDIR}/pluto
172
	RETVAL=$?
173
	[ ${RETVAL} -eq 0 ] && rm -f ${plutopid}
174
    fi
175
 
176
    ipsec _stackmanager stop
177
 
178
    # cleaning up backup resolv.conf
179
    if [ -e ${LIBRESWAN_RESOLV_CONF} ]; then
180
	if grep 'Libreswan' ${ORIG_RESOLV_CONF} > /dev/null 2>&1; then
181
	    cp ${LIBRESWAN_RESOLV_CONF} ${ORIG_RESOLV_CONF}
182
	fi
183
	rm -f  ${LIBRESWAN_RESOLV_CONF}
184
    fi
185
 
186
    rm -f ${lockfile}
187
    return ${RETVAL}
188
}
189
 
190
restart() {
191
    verify_config
192
    stop
193
    start
194
    return $?
195
}
196
 
197
condrestart() {
198
    if [ -f ${lockfile} ]; then
199
	restart
200
	return $?
201
    fi
202
}
203
 
204
version() {
205
    ipsec version
206
    return $?
207
}
208
 
209
 
210
# do it
211
case "$1" in
212
    start)
213
	start
214
	RETVAL=$?
215
	;;
216
    stop)
217
	stop
218
	RETVAL=$?
219
	;;
220
    restart)
221
	restart
222
	RETVAL=$?
223
	;;
224
    reload|force-reload)
225
	restart
226
	RETVAL=$?
227
	;;
228
    condrestart|try-restart)
229
	condrestart
230
	RETVAL=$?
231
	;;
232
    status)
233
	status -p ${plutopid} -l ${lockfile} ${IPSEC_EXECDIR}/pluto
234
	RETVAL=$?
235
	;;
236
    version)
237
	version
238
	RETVAL=$?
239
	;;
240
    *)
241
	echo "Usage: $0 {start|stop|restart|reload|force-reload|condrestart|try-restart|status|version}"
242
	RETVAL=2
243
esac
244
 
245
exit ${RETVAL}