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>
|
|
|
21 |
# Copyright (C) 2008-2011, 2013 Tuomo Soini <tis@foobar.fi>
|
|
|
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} &
|
|
|
125 |
RETVAL=$?
|
|
|
126 |
if [ ${RETVAL} -eq 0 ]; then
|
|
|
127 |
success
|
|
|
128 |
[ -d ${lockdir} ] || mkdir -p ${lockdir}
|
|
|
129 |
touch ${lockfile}
|
|
|
130 |
else
|
|
|
131 |
failure
|
|
|
132 |
fi
|
|
|
133 |
echo
|
|
|
134 |
return ${RETVAL}
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
|
|
|
138 |
stop() {
|
|
|
139 |
if [ ! -e ${plutoctl} ]; then
|
|
|
140 |
echo "Missing control file ${plutoctl} - is pluto running?"
|
|
|
141 |
else
|
|
|
142 |
echo $"Shutting down pluto IKE daemon"
|
|
|
143 |
ipsec whack --shutdown
|
|
|
144 |
# don't use seq, might not exist on embedded
|
|
|
145 |
for waitsec in 1 2 3 4 5 6 7 8 9 10; do
|
|
|
146 |
if [ -s ${plutopid} ]; then
|
|
|
147 |
echo -n "."
|
|
|
148 |
sleep 1
|
|
|
149 |
else
|
|
|
150 |
break
|
|
|
151 |
fi
|
|
|
152 |
done
|
|
|
153 |
echo
|
|
|
154 |
rm -f ${plutoctl} # we won't be using this anymore
|
|
|
155 |
fi
|
|
|
156 |
if [ -s ${plutopid} ]; then
|
|
|
157 |
# pluto did not die peacefully
|
|
|
158 |
echo "Attempt to shut Pluto down failed! Trying kill"
|
|
|
159 |
killproc -p ${plutopid} ${IPSEC_EXECDIR}/pluto
|
|
|
160 |
RETVAL=$?
|
|
|
161 |
[ ${RETVAL} -eq 0 ] && rm -f ${plutopid}
|
|
|
162 |
fi
|
|
|
163 |
|
|
|
164 |
ipsec _stackmanager stop
|
|
|
165 |
|
|
|
166 |
# cleaning up backup resolv.conf
|
|
|
167 |
if [ -e ${LIBRESWAN_RESOLV_CONF} ]; then
|
|
|
168 |
if grep 'Libreswan' ${ORIG_RESOLV_CONF} > /dev/null 2>&1; then
|
|
|
169 |
cp ${LIBRESWAN_RESOLV_CONF} ${ORIG_RESOLV_CONF}
|
|
|
170 |
fi
|
|
|
171 |
rm -f ${LIBRESWAN_RESOLV_CONF}
|
|
|
172 |
fi
|
|
|
173 |
|
|
|
174 |
rm -f ${lockfile}
|
|
|
175 |
return ${RETVAL}
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
restart() {
|
|
|
179 |
verify_config
|
|
|
180 |
stop
|
|
|
181 |
start
|
|
|
182 |
return $?
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
condrestart() {
|
|
|
186 |
if [ -f ${lockfile} ]; then
|
|
|
187 |
restart
|
|
|
188 |
return $?
|
|
|
189 |
fi
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
version() {
|
|
|
193 |
ipsec version
|
|
|
194 |
return $?
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
|
|
|
198 |
# do it
|
|
|
199 |
case "$1" in
|
|
|
200 |
start)
|
|
|
201 |
start
|
|
|
202 |
RETVAL=$?
|
|
|
203 |
;;
|
|
|
204 |
stop)
|
|
|
205 |
stop
|
|
|
206 |
RETVAL=$?
|
|
|
207 |
;;
|
|
|
208 |
restart)
|
|
|
209 |
restart
|
|
|
210 |
RETVAL=$?
|
|
|
211 |
;;
|
|
|
212 |
reload|force-reload)
|
|
|
213 |
restart
|
|
|
214 |
RETVAL=$?
|
|
|
215 |
;;
|
|
|
216 |
condrestart|try-restart)
|
|
|
217 |
condrestart
|
|
|
218 |
RETVAL=$?
|
|
|
219 |
;;
|
|
|
220 |
status)
|
|
|
221 |
status -p ${plutopid} -l ${lockfile} ${IPSEC_EXECDIR}/pluto
|
|
|
222 |
RETVAL=$?
|
|
|
223 |
;;
|
|
|
224 |
version)
|
|
|
225 |
version
|
|
|
226 |
RETVAL=$?
|
|
|
227 |
;;
|
|
|
228 |
*)
|
|
|
229 |
echo "Usage: $0 {start|stop|restart|reload|force-reload|condrestart|try-restart|status|version}"
|
|
|
230 |
RETVAL=2
|
|
|
231 |
esac
|
|
|
232 |
|
|
|
233 |
exit ${RETVAL}
|