File: ipsec.in

package info (click to toggle)
libreswan 4.3-1%2Bdeb11u4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 62,688 kB
  • sloc: ansic: 108,293; sh: 25,973; xml: 11,756; python: 10,230; makefile: 1,580; javascript: 1,353; yacc: 825; sed: 647; perl: 584; lex: 159; awk: 156
file content (581 lines) | stat: -rwxr-xr-x 16,782 bytes parent folder | download
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#!@SHELL_BINARY@
#
# prefix command to run stuff from our programs directory
#
# Copyright (C) 1998-2002  Henry Spencer.
# Copyright (C) 2013-2018  Tuomo Soini <tis@foobar.fi>
# Copyright (C) 2013-2016  Paul Wouters <pwouters@redhat.com>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.  See <https://www.gnu.org/licenses/gpl2.txt>.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#

test "${IPSEC_INIT_SCRIPT_DEBUG}" && set -v -x

# where the private directory and the config files are
IPSEC_CONF="${IPSEC_CONF:-@FINALCONFFILE@}"
IPSEC_EXECDIR="${IPSEC_EXECDIR:-@IPSEC_EXECDIR@}"
IPSEC_SBINDIR="${IPSEC_SBINDIR:-@IPSEC_SBINDIR@}"
IPSEC_CONFDDIR="${IPSEC_CONFDDIR:-@IPSEC_CONFDDIR@}"
IPSEC_RUNDIR="${IPSEC_RUNDIR:-@IPSEC_RUNDIR@}"
IPSEC_NSSDIR="${IPSEC_NSSDIR:-@IPSEC_NSSDIR@}"
IPSEC_NSSDIR_SQL="sql:${IPSEC_NSSDIR}"
IPSEC_NSSPW="${IPSEC_CONFDDIR}/nsspassword"
DBPW=""
CACERTDIR="${IPSEC_CONFDDIR}/cacerts"
CRLDIR="${IPSEC_CONFDDIR}/crls"
CTLSOCKET="${IPSEC_RUNDIR}/pluto.ctl"

export IPSEC_EXECDIR IPSEC_CONF IPSEC_RUNDIR CTLSOCKET

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

# suppress ElectricFence banner changing our reference testing output
export EF_DISABLE_BANNER=1

# things not to be listed in --help command list
DONTMENTION='^(ipsec|_.*|.*\.old|.*\.orig|.*~)$'

# version numbering (details filled in by build)
# Possibly should call a C program to invoke the version_code() function
# instead, but for performance's sake, we inline it here (and only here).
version="@IPSECVERSION@"

# export the version information
IPSEC_VERSION="${version}"
export IPSEC_VERSION

# function for the funky user/kernel version stuff
fixversion() {
    if [ -f /proc/net/ipsec_version ]; then
	stack=" (klips)"
	kv="$(awk '{print $NF}' /proc/net/ipsec_version)"
    else
	if [ -f /proc/sys/net/core/xfrm_acq_expires ]; then
	    stack=" (netkey)"
	    kv="${version}"
	else
	    if [ -f /mach_kernel ]; then
		stack=" (OSX)"
		kv="$(uname -r)"
	    else
		kv="(no kernel code presently loaded)"
	    fi
	fi
    fi
    if [ "${kv}" != "${version}" ]; then
	version="U${version}/K${kv}"
    fi
    version="${version}${stack} on $(uname -r)"
}

set_nss_db_trusts() {
    # has to handle a NSS nick with spaces
    certutil -L -d "${IPSEC_NSSDIR_SQL}" | \
	grep -E -v '(Trust Attributes|SSL,S/MIME,JAR/XPI|^)$' | \
	awk '{$NF=""; print $0}' | \
	while read -r cert
    do
	if certutil -L -d "${IPSEC_NSSDIR_SQL}" -n "${cert}" | \
	    grep -q 'Is a CA' && \
	    [ $(certutil -L -d "${IPSEC_NSSDIR_SQL}" -n "${cert}" | grep -i -A3 'ssl flags' | grep -i 'trusted' | wc -l) -ne 2 ]
	then
	    echo "correcting trust bits for ${cert}"
	    certutil -M -d "${IPSEC_NSSDIR_SQL}" -n "${cert}" -t 'CT,,'
	fi
    done
}

ipsec_usage() {
    echo "Usage: ipsec {command} [argument] ...>"
    echo "Use --help for a list of commands, or see the ipsec(8) manual page"
    echo "Most commands have their own manual pages, e.g. ipsec_auto(8)."
    echo "See <https://libreswan.org> for more general info."
    fixversion
    echo "Linux Libreswan ${version}"
    exit 2
}

ipsec_help() {
    echo "Usage: ipsec {command} [argument] ...>"
    echo "where {command} is one of:"
    echo ""
    GOTTWO=""
    for f in start stop restart status trafficstatus traffic globalstatus shuntstatus briefstatus showstates fips import initnss checknss checknflog \
	$(ls "${IPSEC_EXECDIR}" | grep -E -v -i "${DONTMENTION}"); do
	if [ -z "${GOTTWO}" ]; then
	    # first of two entries
	    GOTTWO="${f}"
	else
	    # second of two entries, we can print
	    printf "\t%s" "${GOTTWO}"
	    if [ "${#GOTTWO}" -ge 16 ]; then
		printf  "\t"
	    elif [ "${#GOTTWO}" -ge 8 ]; then
		printf "\t\t"
	    else
		printf "\t\t\t"
	    fi
	    printf "%s\n" "${f}"
	    GOTTWO=""
	fi
    done
    if [ -n "${GOTTWO}" ]; then
	# leftover entry
	printf "\t%s" "${GOTTWO}"
    fi
    echo
    echo "See also: man ipsec <command> or ipsec <command> --help"
    echo "See <https://libreswan.org/> for more general info."
    fixversion
    echo "Linux Libreswan ${version}"
    exit 2
}

ipsec_stopnflog() {
    NFGROUP=$(ASAN_OPTIONS=detect_leaks=0 "${IPSEC_EXECDIR}/addconn" --ctlsocket "${CTLSOCKET}" --configsetup | grep -v "#" | grep nflog | sed -e "s/^.*=//" -e "s/'//g");
    if [ -z "${NFGROUP}" ]; then
	exit 0
    fi
    iptables -D INPUT  -m policy --dir in  --pol ipsec -j NFLOG --nflog-group "${NFGROUP}" --nflog-prefix all-ipsec
    iptables -D OUTPUT -m policy --dir out --pol ipsec -j NFLOG --nflog-group "${NFGROUP}" --nflog-prefix all-ipsec
    exit 0
}

ipsec_checknflog() {
    NFGROUP=$(ASAN_OPTIONS=detect_leaks=0 "${IPSEC_EXECDIR}/addconn" --ctlsocket "${CTLSOCKET}" --configsetup | grep -v "#" | grep nflog| sed -e "s/^.*=//" -e "s/'//g");
    if [ -n "${NFGROUP}" ]; then
	OLDNFGROUP=$(iptables -L -n | grep "all-ipsec nflog-group" | sed "s/^.* //" | tail -1);
	if [ -n "${OLDNFGROUP}" ]; then
	    if [ "${NFGROUP}" = "${OLDNFGROUP}" ]; then
		# nothing to do
		echo "nflog ipsec capture enabled on nflog:${NFGROUP}"
		exit 0
	    else
		# delete rules with old group number
		echo "deleting rules with old nflog group ${OLDNFGROUP}"
		iptables -D INPUT  -m policy --dir in  --pol ipsec -j NFLOG --nflog-group ${OLDNFGROUP} --nflog-prefix all-ipsec
		iptables -D OUTPUT -m policy --dir out --pol ipsec -j NFLOG --nflog-group ${OLDNFGROUP} --nflog-prefix all-ipsec
	    fi
	fi
	# insert rules with current group number
	iptables -I INPUT  -m policy --dir in  --pol ipsec -j NFLOG --nflog-group ${NFGROUP} --nflog-prefix all-ipsec
	iptables -I OUTPUT -m policy --dir out --pol ipsec -j NFLOG --nflog-group ${NFGROUP} --nflog-prefix all-ipsec
	echo "nflog ipsec capture enabled on nflog:${NFGROUP}"
    else
	OLDNFGROUP=$(iptables -L -n | grep "all-ipsec nflog-group" | sed "s/^.* //" | tail -1);
	if [ -n "${OLDNFGROUP}" ]; then
	    echo "deleting rules with old nflog group ${OLDNFGROUP}"
	    iptables -D INPUT  -m policy --dir in  --pol ipsec -j NFLOG --nflog-group "${OLDNFGROUP}" --nflog-prefix all-ipsec
	    iptables -D OUTPUT -m policy --dir out --pol ipsec -j NFLOG --nflog-group "${OLDNFGROUP}" --nflog-prefix all-ipsec
	fi
	echo "nflog ipsec capture disabled"
    fi
    exit 0
}

ipsec_sniff() {
    NFGROUP=$(ASAN_OPTIONS=detect_leaks=0 "${IPSEC_EXECDIR}/addconn" --ctlsocket "${CTLSOCKET}" --configsetup | grep -v "#" | grep nflog | sed -e "s/^.*=//" -e "s/'//g");
    tcpdump -n -i nflog:${NFGROUP}
    exit 0
}

ipsec_import() {
    if [ -z "${1}" ]; then
	echo "Usage: ipsec import [--nssdir ${IPSEC_NSSDIR}] /path/to/pkcs.12" >&2
	exit 1
    fi

    while [ ${#} -gt 0 ]; do
	case "${1}" in
	    --configdir)
		echo "ipsec import warning: --configdir is obsoleted, use --nssdir" >&2
		if [ -d "${2}" ]; then
		    IPSEC_NSSDIR="${2}"
		fi
		shift
		;;
	    -d|--nssdir)
		if [ -d "${2}" ]; then
		    IPSEC_NSSDIR="${2}"
		fi
		shift
		# A lot of nss commands use -d to specify NSS db location.
		# We use --nssdir.
		;;
	    *)
		if [ -f "${1}" ]; then
		    pkcs12bundle="${1}"
		else
		    echo "Usage: ipsec import [--nssdir ${IPSEC_NSSDIR}] /path/to/pkcs.12" >&2
		    exit 1
		fi
		;;
	esac
	shift
    done

    if [ -d "${IPSEC_NSSDIR}" -a -w "${IPSEC_NSSDIR}" ]; then
	if [ -f "${IPSEC_NSSDIR}/key4.db" -a \
	    -f "${IPSEC_NSSDIR}/cert9.db" ]
	then
	    IPSEC_NSSDIR_SQL="sql:${IPSEC_NSSDIR}"
	else
	    echo "ERROR: NSS database files are missing, import aborted." >&2
	    echo "Initialize database with command \"ipsec checknss\"." >&2
	    exit 1
	fi

	pk12util -i "${pkcs12bundle}" -d "${IPSEC_NSSDIR_SQL}"
	# check and correct trust bits
	set_nss_db_trusts
	exit 0
    else
	echo "ERROR: destination directory \"${IPSEC_NSSDIR}\" is missing or permission denied" >&2
	exit 1
    fi
}

ipsec_checknss() {
    local file
    while [ ${#} -gt 0 ]; do
	case "${1}" in
	    --checknss|checknss)
		cmd=checknss
		;;
	    --initnss|initnss)
		cmd=initnss
		;;
	    --settrusts)
	        set_trusts=yes
		;;
	    --configdir)
		echo "ipsec ${cmd} warning: --configdir is obsoleted, use --nssdir" >&2
		IPSEC_NSSDIR="${2}"
		shift
		;;
	    -d|--nssdir)
		# A lot of nss commands use -d to specify NSS db location.
		# We use --nssdir.
		IPSEC_NSSDIR="${2}"
		shift
		;;
	    *)
		echo "Usage: ipsec ${cmd} [--nssdir ${IPSEC_NSSDIR}]" >&2
		exit 1
		;;
	esac
	shift
    done

    if [ -d "${IPSEC_NSSDIR}" -a -w "${IPSEC_NSSDIR}" ]; then
	IPSEC_NSSDIR_SQL="sql:${IPSEC_NSSDIR}"
	# Handle nssdir default change from /etc/ipsec.d to /var/lib/ipsec/nss
	if [ "${IPSEC_CONFDDIR}" != "${IPSEC_NSSDIR}" -a \
	    "${cmd}" = "checknss" ]
	then
	    # Check for legacy nss db format in old location and give failure
	    # We can't handle two operations at same time.
	    if [ -f "${IPSEC_CONFDDIR}/cert8.db" -a \
		! -f "${IPSEC_CONFDDIR}/cert9.db" -a \
		! -f "${IPSEC_NSSDIR}/cert9.db" ]
	    then
		echo "Failure - we cannot handle both nss db format conversion and nss db move to new location in one run." >&2
		echo "Run \"ipsec checknss --nssdir ${IPSEC_CONFDDIR}\" manually first to convert db format" >&2
		exit 4
	    fi
	    for file in cert9.db key4.db pkcs11.txt; do
		if [ -f "${IPSEC_CONFDDIR}/${file}" -a \
		    ! -f "${IPSEC_NSSDIR}/${file}" ]
		then
		    if ! mv "${IPSEC_CONFDDIR}/${file}" \
			"${IPSEC_NSSDIR}/${file}"
		    then
			echo "Failed to mv ${IPSEC_CONFDDIR}/${file} ${IPSEC_NSSDIR}/${file}" >&2
			exit 4
		    fi
		fi
	    done
	fi
	# if we have old database
	if [ -f "${IPSEC_NSSDIR}/cert8.db" -o \
	    -f "${IPSEC_NSSDIR}/key3.db" -o \
	    -f "${IPSEC_NSSDIR}/secmod.db" ]
	then
	    if [ ! -f "${IPSEC_NSSDIR}/cert9.db" -o \
		! -f "${IPSEC_NSSDIR}/key4.db" ]; then
		IMPORTDBPW=""
		NSSTMP=$(mktemp -d /tmp/ipsec_nss_tmp.XXXXXXXXXX)
		if [ $? -gt 0 ]; then
		    echo "Failed to create temporary directory for NSS db migration" >&2
		    exit 4
		fi
		# save current umask
		umask=$(umask)
		# set safe umask
		umask 077
		echo "Migrating NSS db to ${IPSEC_NSSDIR_SQL}"
		# this section works around a few certutil quirks
		# to maintain the current password and merge keys
		certutil -N -d sql:"${NSSTMP}" --empty-password
		if [ $? -gt 0 ]; then
		    echo "Failed to initialize nss database sql:${NSSTMP}" >&2
		    exit 4
		fi
		if [ -f "${IPSEC_NSSPW}" ]; then
		    # Look for FIPS format of token:pw, or just the pw
		    grep -q ':' "${IPSEC_NSSPW}"
		    if [ $? -eq 0 ]; then
			cut -d':' -f2 "${IPSEC_NSSPW}" \
			    > "${NSSTMP}/nsspassword.txt"
			cut -d':' -f2 "${IPSEC_NSSPW}" \
			    >> "${NSSTMP}/nsspassword.txt"
		    else
			cat "${IPSEC_NSSPW}" > "${NSSTMP}/nsspassword.txt"
			cat "${IPSEC_NSSPW}" >> "${NSSTMP}/nsspassword.txt"
		    fi
		    # For the empty password prompt:
		    printf "\n\n" > "${NSSTMP}/nsspassword2.txt"
		    # Change blank pw to the current, and use
		    # for certutil --upgrade-merge
		    certutil -W -d sql:"${NSSTMP}" \
			-f "${NSSTMP}/nsspassword2.txt" \
			-@ "${NSSTMP}/nsspassword.txt"
		    DBPW="-f ${NSSTMP}/nsspassword.txt -@ ${NSSTMP}/nsspassword.txt"
		    IMPORTDBPW="-f ${NSSTMP}/nsspassword.txt"
		fi
		# restore umask
		umask ${umask}
		certutil --upgrade-merge --source-dir "${IPSEC_NSSDIR}" \
		    -d sql:"${NSSTMP}" --upgrade-id pluto ${DBPW}
		rc=$?
		if [ ${rc} -ne 0 ]; then
		    echo "NSS upgrade failed. You should run certutil --upgrade-merge manually against ${IPSEC_NSSDIR_SQL}"
		    exit ${rc}
		fi
		# import cacerts and crls
		if [ -d "${CACERTDIR}" ]; then
		    for file in "${CACERTDIR}"/*; do
			if [ -f "${file}" ]; then
			    filename=$(basename "${file}")
			    name=${filename%%.*}
			    certutil -A -i "${file}" -d sql:"${NSSTMP}" -n "${name}" -t 'CT,,' ${IMPORTDBPW}
			    [ $? -eq 0 ] || printf "%s\n" "${file}"
			fi
		    done
		fi
		if [ -d "${CRLDIR}" ]; then
		    for file in "${CRLDIR}"/*; do
			if [ -f "${file}" ]; then
			    crlutil -I -i "${file}" -d sql:"${NSSTMP}" -B ${IMPORTDBPW}
			    [ $? -eq 0 ] || printf "%s\n" "${file}"
			fi
		    done
		fi
		cp "${NSSTMP}"/*.db "${NSSTMP}"/*.txt "${IPSEC_NSSDIR}"
		rm -f "${NSSTMP}"/*.txt "${NSSTMP}"/*.db
		rmdir "${NSSTMP}"
		echo "NSS upgrade complete"
	    fi
	    exit 0
	fi	# old database
	if [ -f "${IPSEC_NSSDIR}/cert9.db" -o -f "${IPSEC_NSSDIR}/key4.db" ]; then
	    if [ "${cmd}" = "initnss" ]; then
		echo "NSS database already initialised - aborted"
		echo "To wipe the old NSS database, issue: rm ${IPSEC_NSSDIR}/*.db"
		exit 42
	    else
		if [ "${set_trusts}" = "yes" ]; then
		    set_nss_db_trusts
		fi
		exit 0
	    fi
	fi
	echo "Initializing NSS database"
	echo ""
	certutil -N -d "${IPSEC_NSSDIR_SQL}" --empty-password
	if [ $? -gt 0 ]; then
	    echo "Failed to initialize nss database ${IPSEC_NSSDIR_SQL}" >&2
	    exit 4
	fi
	restorecon="$(which restorecon 2>/dev/null)"
	if [ -n "${restorecon}" -a -x "${restorecon}" ]; then
	    "${restorecon}" -Rv "${IPSEC_NSSDIR}"
	fi
	exit 0
    else
	echo "ERROR: destination directory \"${IPSEC_NSSDIR}\" is missing or permission denied" >&2
	exit 1
    fi
}

# Check for no options at all and return usage.
if [ -z "${1}" ]; then
    ipsec_usage
fi

while [ ${#} -gt 0 ]; do
    case "${1}" in
	--rundir)
	    RUNDIR="${2}"
	    CTLSOCKET="${RUNDIR}/pluto.ctl"
	    export CTLSOCKET
	    shift;
	    shift;
	    ;;
	--ctlsocket)
	    CTLSOCKET="${2}"
	    export CTLSOCKET
	    shift;
	    shift;
	    ;;
	whack)
	    # Whack command is special because --ctlsocket is a whack cmdline option
	    # We need to make sure we don't give it twice
	    shift
	    if echo "${@}" | grep -q -- --ctlsocket; then
		exec "${IPSEC_EXECDIR}/whack" "${@}"
	    else
		exec "${IPSEC_EXECDIR}/whack" --ctlsocket "${CTLSOCKET}" "${@}"
	    fi
	    exit 0
	    ;;
	status|--status)
	    cmd="whack"
	    whackoption="--status"
	    shift
	    ;;
	trafficstatus|--trafficstatus|traffic)
	    cmd="whack"
	    whackoption="--trafficstatus"
	    shift
	    ;;
	fipsstatus|--fipsstatus|fips)
	    cmd="whack"
	    whackoption="--fipsstatus"
	    shift
	    ;;
	globalstatus|--globalstatus)
	    cmd="whack"
	    whackoption="--globalstatus"
	    shift
	    ;;
	shuntstatus|--shuntstatus)
	    cmd="whack"
	    whackoption="--shuntstatus"
	    shift
	    ;;
	briefstatus|--briefstatus)
	    cmd="whack"
	    whackoption="--briefstatus"
	    shift
	    ;;
	showstates|--showstates)
	    cmd="whack"
	    whackoption="--showstates"
	    shift
	    ;;
	start|--start)
	    cmd="setup"
	    setupoption="--start"
	    shift
	    ;;
	stop|--stop)
	    cmd="setup"
	    setupoption="--stop"
	    shift
	    ;;
	letsencrypt)
	    shift
	    exec "${IPSEC_EXECDIR}/letsencrypt" "${@}"
	    ;;
	restart|--restart)
	    cmd="setup"
	    setupoption="--restart"
	    shift
	    ;;
	help|--help)
	    # if cmd is unset, this is help for ipsec command
	    if [ -z "${cmd}" ]; then
		ipsec_help
	    fi
	    ;;
    	version|--version|--versioncode)
	    # some ubuntu/debian scripts use --versioncode, so let's keep the alias
	    fixversion
	    echo "Linux Libreswan ${version}"
	    exit 0
	    ;;
	--directory)
	    printf "%s\n" "${IPSEC_EXECDIR}"
	    exit 0
	    ;;
	--stopnflog|stopnflog)
	    ipsec_stopnflog
	    ;;
	--checknflog|checknflog|nflog)
	    ipsec_checknflog
	    ;;
	sniff|--sniff)
	    ipsec_sniff
	    ;;
	import|--import)
	    shift
	    ipsec_import "${@}"
	    ;;
	checknss|--checknss|initnss|--initnss)
	    ipsec_checknss "${@}"
	    ;;
	secrets)
	    cmd="whack"
	    whackoption="--rereadsecrets"
	    shift
	    ;;
	rereadcrls|--rereadcrls)
	    echo "ipsec auto: --rereadcrls and /etc/ipsec.d/crls/ are obsoleted. did you mean: ipsec crls"
	    exit 1
	    ;;
	crls|fetchcrls)
	    cmd="whack"
	    whackoption="--fetchcrls"
	    shift
	    ;;
	unbound|--unbound)
	    # activate the unbound ipsec module
	    unbound-control set_option ipsecmod-enabled: yes
	    exit $?
	    ;;
	--*)
	    printf "%s: unknown option \"%s\" (perhaps command name was omitted?)\n" "${0}" "${1}" >&2
	    exit 1
	    ;;
	*)
	    cmd="${1}"
	    shift
	    if [ -x "${IPSEC_EXECDIR}/${cmd}" ]; then
		exec "${IPSEC_EXECDIR}/${cmd}" "${@}"
	    else
		printf "%s: unknown IPsec command \"%s\" (\"ipsec --help\" for list)\n" "${0}" "${cmd}" >&2
		exit 1
	    fi
	    ;;
    esac
done

case "${cmd}" in
    setup)
	exec "${IPSEC_EXECDIR}/setup" "${setupoption}"
	;;
    whack)
	exec "${IPSEC_EXECDIR}/whack" --ctlsocket "${CTLSOCKET}" "${whackoption}"
	;;
esac