Subversion Repositories configs

Rev

Rev 57 | 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>
57 - 21
# Copyright (C) 2008-2015         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 /
57 - 123
    # Create nss db or convert from old format to new sql format
124
    ipsec --checknss
125
    # Enable nflog if configured
126
    ipsec --checknflog > /dev/null
3 - 127
    # This script will enter an endless loop to ensure pluto restarts on crash
128
    ipsec _plutorun --config ${IPSEC_CONF} --nofork ${PLUTO_OPTIONS} &
12 - 129
    [ -d ${lockdir} ] || mkdir -p ${lockdir}
130
    touch ${lockfile}
131
    # Because _plutorun starts pluto at background we need to make sure pluto is started
132
    # before we know if start was successful or not
133
    for waitsec in 1 2 3 4 5; do
134
	if status -p ${plutopid} -l ${lockfile} ${IPSEC_EXECDIR}/pluto >/dev/null; then
135
	    RETVAL=0
136
	    break
137
	else
138
	    echo -n "."
139
	    sleep 1
140
	    RETVAL=1
141
	fi
142
    done
3 - 143
    if [ ${RETVAL} -eq 0 ]; then
144
	success
145
    else
12 - 146
	rm -f ${lockfile}
3 - 147
	failure
148
    fi
149
    echo
150
    return ${RETVAL}
151
}
152
 
153
 
154
stop() {
95 - 155
    if [ -e ${plutoctl} ]; then
3 - 156
	echo $"Shutting down pluto IKE daemon"
95 - 157
	ipsec whack --shutdown 2>/dev/null
3 - 158
	# don't use seq, might not exist on embedded
159
	for waitsec in 1 2 3 4 5 6 7 8 9 10; do
160
	    if [ -s ${plutopid} ]; then
161
		echo -n "."
162
		sleep 1
163
	    else
164
		break
165
	    fi
166
	done
167
	echo
168
	rm -f ${plutoctl} # we won't be using this anymore
169
    fi
170
    if [ -s ${plutopid} ]; then
171
	# pluto did not die peacefully
172
	killproc -p ${plutopid} ${IPSEC_EXECDIR}/pluto
173
	RETVAL=$?
174
	[ ${RETVAL} -eq 0 ] && rm -f ${plutopid}
175
    fi
176
 
177
    ipsec _stackmanager stop
57 - 178
    ipsec --stopnflog > /dev/null
3 - 179
 
180
    # cleaning up backup resolv.conf
181
    if [ -e ${LIBRESWAN_RESOLV_CONF} ]; then
182
	if grep 'Libreswan' ${ORIG_RESOLV_CONF} > /dev/null 2>&1; then
183
	    cp ${LIBRESWAN_RESOLV_CONF} ${ORIG_RESOLV_CONF}
184
	fi
185
	rm -f  ${LIBRESWAN_RESOLV_CONF}
186
    fi
187
 
188
    rm -f ${lockfile}
189
    return ${RETVAL}
190
}
191
 
192
restart() {
193
    verify_config
194
    stop
195
    start
196
    return $?
197
}
198
 
199
condrestart() {
200
    if [ -f ${lockfile} ]; then
201
	restart
202
	return $?
203
    fi
204
}
205
 
206
version() {
207
    ipsec version
208
    return $?
209
}
210
 
211
 
212
# do it
213
case "$1" in
214
    start)
215
	start
216
	RETVAL=$?
217
	;;
218
    stop)
219
	stop
220
	RETVAL=$?
221
	;;
222
    restart)
223
	restart
224
	RETVAL=$?
225
	;;
226
    reload|force-reload)
227
	restart
228
	RETVAL=$?
229
	;;
230
    condrestart|try-restart)
231
	condrestart
232
	RETVAL=$?
233
	;;
234
    status)
235
	status -p ${plutopid} -l ${lockfile} ${IPSEC_EXECDIR}/pluto
236
	RETVAL=$?
57 - 237
	${IPSEC_EXECDIR}/whack --status | grep Total | sed 's/^000\ Total\ //'
3 - 238
	;;
239
    version)
240
	version
241
	RETVAL=$?
242
	;;
243
    *)
244
	echo "Usage: $0 {start|stop|restart|reload|force-reload|condrestart|try-restart|status|version}"
245
	RETVAL=2
246
esac
247
 
248
exit ${RETVAL}