summaryrefslogtreecommitdiffstats
path: root/net/libreswan/files/ipsec.init
blob: 6e3026308632ea0235007cfe39dfde4d1f14f64c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/sh /etc/rc.common

START=90
STOP=10

#USE_PROCD=1

. $IPKG_INSTROOT/lib/functions.sh

EXTRA_COMMANDS=status
EXTRA_HELP="	status	Show the status of the service"

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 6

if [ $(id -u) -ne 0 ]; then
    echo "permission denied (must be superuser)" | \
	logger -s -p daemon.error -t ipsec_setup 2>&1
    exit 4
fi

# where the private directory and the config files are
IPSEC_EXECDIR="${IPSEC_EXECDIR-/usr/libexec/ipsec}"
IPSEC_SBINDIR="${IPSEC_SBINDIR-/usr/sbin}"
IPSEC_CONF="${IPSEC_CONF-/etc/ipsec.conf}"
unset PLUTO_OPTIONS

rundir=/var/run/pluto
plutopid=${rundir}/pluto.pid
plutoctl=${rundir}/pluto.ctl
lockdir=/var/lock
lockfile=${lockdir}/ipsec
ipsecversion=/proc/net/ipsec_version
kamepfkey=/proc/net/pfkey

# /etc/resolv.conf related paths
LIBRESWAN_RESOLV_CONF=${rundir}/libreswan-resolv-conf-backup
ORIG_RESOLV_CONF=/etc/resolv.conf

# misc setup
umask 022

# standardize PATH, and export it for everything else's benefit
PATH="${IPSEC_SBINDIR}":/sbin:/usr/sbin:/usr/local/bin:/bin:/usr/bin
export PATH

mkdir -p ${rundir}
chmod 700 ${rundir}

verify_config() {
	[ -f ${IPSEC_CONF} ] || exit 6
	config_error=$(ipsec addconn --config ${IPSEC_CONF} --checkconfig 2>&1)
	RETVAL=$?
	if [ ${RETVAL} -gt 0 ]; then
		echo "Configuration error - the following error occurred:"
		echo ${config_error}
		echo "IKE daemon status was not modified"
		exit ${RETVAL}
	fi
}

start() {
	echo -n "Starting pluto IKE daemon for IPsec: "
	ipsec _stackmanager start
	# pluto searches the current directory, so this is required for making it selinux compliant
	cd /
	# Create nss db or convert from old format to new sql format
	ipsec --checknss
	# Enable nflog if configured
	ipsec --checknflog > /dev/null
	# This script will enter an endless loop to ensure pluto restarts on crash
	ipsec _plutorun --config ${IPSEC_CONF} --nofork ${PLUTO_OPTIONS} & [ -d ${lockdir} ] || mkdir -p ${lockdir}
	touch ${lockfile}
	# Because _plutorun starts pluto at background we need to make sure pluto is started
	# before we know if start was successful or not
	for waitsec in 1 2 3 4 5; do
	    if status >/dev/null; then
		RETVAL=0
		break
		else
		echo -n "."
		sleep 1
		RETVAL=1
	    fi
	done
	if [ ${RETVAL} -ge 1 ]; then
	    rm -f ${lockfile}
	fi
	echo
	return ${RETVAL}
}

stop() {
    if [ -e ${plutoctl} ]; then
	echo "Shutting down pluto IKE daemon"
	ipsec whack --shutdown 2>/dev/null
	# don't use seq, might not exist on embedded
	for waitsec in 1 2 3 4 5 6 7 8 9 10; do
	    if [ -s ${plutopid} ]; then
		echo -n "."
		sleep 1
	    else
		break
	    fi
	done
	echo
	rm -f ${plutoctl} # we won't be using this anymore
    fi
    if [ -s ${plutopid} ]; then
	# pluto did not die peacefully
	pid=$(cat ${plutopid})
	if [ -d /proc/${pid} ]; then
	    kill -TERM ${pid}
	    RETVAL=$?
	    sleep 5;
	    if [ -d /proc/${pid} ]; then
		kill -KILL ${pid}
		RETVAL=$?
	    fi
	    if [ ${RETVAL} -ne 0 ]; then
		echo "Kill failed - removing orphaned ${plutopid}"
	    fi
	else
	    echo "Removing orphaned ${plutopid}"
	fi
	rm -f ${plutopid}
    fi

    ipsec _stackmanager stop
    ipsec --stopnflog > /dev/null

    # cleaning up backup resolv.conf
    if [ -e ${LIBRESWAN_RESOLV_CONF} ]; then
	if grep 'Libreswan' ${ORIG_RESOLV_CONF} > /dev/null 2>&1; then
	    cp ${LIBRESWAN_RESOLV_CONF} ${ORIG_RESOLV_CONF}
	fi
	rm -f  ${LIBRESWAN_RESOLV_CONF}
    fi

    rm -f ${lockfile}
    return ${RETVAL}
}

restart() {
    verify_config
    stop
    start
    return $?
}

status() {
    local RC
    if [ -f ${plutopid} ]; then
	if [ -r ${plutopid} ]; then
	    pid=$(cat ${plutopid})
	    if [ -n "$pid" -a -d /proc/${pid} ]; then
		RC=0    # running
	    else
		RC=1    # not running but pid exists
	    fi
	else
	    RC=4        # insufficient privileges
	fi
    fi
    if [ -z "${RC}" ]; then
	if [ -f ${lockfile} ]; then
	    RC=2
	else
	    RC=3
	fi
    fi
    case "${RC}" in
	0)
	    echo "ipsec: pluto (pid ${pid}) is running..."
	    return 0
	    ;;
	1)
	    echo "ipsec: pluto dead but pid file exits"
	    return 1
	    ;;
	2)
	    echo "ipsec: pluto dead but subsys locked"
	    return 2
	    ;;
	4)
	    echo "ipsec: pluto status unknown due to insufficient privileges."
	    return 4
	    ;;
    esac
    echo "ipsec: pluto is stopped"
    return 3
}

condrestart() {
    verify_config
    RETVAL=$?
    if [ -f ${lockfile} ]; then
	restart
	RETVAL=$?
    fi
    return ${RETVAL}
}

version() {
    ipsec version
    return $?
}