Subversion Repositories configs

Rev

Rev 12 | 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>
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() {
155
    if [ ! -e ${plutoctl} ]; then
156
	echo "Missing control file ${plutoctl} - is pluto running?"
157
    else
158
	echo $"Shutting down pluto IKE daemon"
159
	ipsec whack --shutdown
160
	# don't use seq, might not exist on embedded
161
	for waitsec in 1 2 3 4 5 6 7 8 9 10; do
162
	    if [ -s ${plutopid} ]; then
163
		echo -n "."
164
		sleep 1
165
	    else
166
		break
167
	    fi
168
	done
169
	echo
170
	rm -f ${plutoctl} # we won't be using this anymore
171
    fi
172
    if [ -s ${plutopid} ]; then
173
	# pluto did not die peacefully
174
	echo "Attempt to shut Pluto down failed!  Trying kill"
175
	killproc -p ${plutopid} ${IPSEC_EXECDIR}/pluto
176
	RETVAL=$?
177
	[ ${RETVAL} -eq 0 ] && rm -f ${plutopid}
178
    fi
179
 
180
    ipsec _stackmanager stop
57 - 181
    ipsec --stopnflog > /dev/null
3 - 182
 
183
    # cleaning up backup resolv.conf
184
    if [ -e ${LIBRESWAN_RESOLV_CONF} ]; then
185
	if grep 'Libreswan' ${ORIG_RESOLV_CONF} > /dev/null 2>&1; then
186
	    cp ${LIBRESWAN_RESOLV_CONF} ${ORIG_RESOLV_CONF}
187
	fi
188
	rm -f  ${LIBRESWAN_RESOLV_CONF}
189
    fi
190
 
191
    rm -f ${lockfile}
192
    return ${RETVAL}
193
}
194
 
195
restart() {
196
    verify_config
197
    stop
198
    start
199
    return $?
200
}
201
 
202
condrestart() {
203
    if [ -f ${lockfile} ]; then
204
	restart
205
	return $?
206
    fi
207
}
208
 
209
version() {
210
    ipsec version
211
    return $?
212
}
213
 
214
 
215
# do it
216
case "$1" in
217
    start)
218
	start
219
	RETVAL=$?
220
	;;
221
    stop)
222
	stop
223
	RETVAL=$?
224
	;;
225
    restart)
226
	restart
227
	RETVAL=$?
228
	;;
229
    reload|force-reload)
230
	restart
231
	RETVAL=$?
232
	;;
233
    condrestart|try-restart)
234
	condrestart
235
	RETVAL=$?
236
	;;
237
    status)
238
	status -p ${plutopid} -l ${lockfile} ${IPSEC_EXECDIR}/pluto
239
	RETVAL=$?
57 - 240
	${IPSEC_EXECDIR}/whack --status | grep Total | sed 's/^000\ Total\ //'
3 - 241
	;;
242
    version)
243
	version
244
	RETVAL=$?
245
	;;
246
    *)
247
	echo "Usage: $0 {start|stop|restart|reload|force-reload|condrestart|try-restart|status|version}"
248
	RETVAL=2
249
esac
250
 
251
exit ${RETVAL}