File: ipsec.in

package info (click to toggle)
libreswan 5.2-2.3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 81,644 kB
  • sloc: ansic: 129,988; sh: 32,018; xml: 20,646; python: 10,303; makefile: 3,022; javascript: 1,506; sed: 574; yacc: 511; perl: 264; awk: 52
file content (1345 lines) | stat: -rwxr-xr-x 31,244 bytes parent folder | download | duplicates (2)
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
#!@@SHELL_BINARY@@
# -*- mode: sh; sh-shell: sh -*-
#
# prefix command to run stuff from our programs directory
#
# Copyright (C) 1998-2002  Henry Spencer.
# Copyright (C) 2013-2023  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:-@@IPSEC_CONF@@}"
IPSEC_EXECDIR="${IPSEC_EXECDIR:-@@IPSEC_EXECDIR@@}"
IPSEC_SBINDIR="${IPSEC_SBINDIR:-@@SBINDIR@@}"
IPSEC_CONFDDIR="${IPSEC_CONFDDIR:-@@IPSEC_CONFDDIR@@}"
IPSEC_RUNDIR="${IPSEC_RUNDIR:-@@RUNDIR@@}"
IPSEC_NSSDIR="${IPSEC_NSSDIR:-@@IPSEC_NSSDIR@@}"
IPSEC_NSSDIR_SQL="sql:${IPSEC_NSSDIR}"
IPSEC_NSSPW="${IPSEC_CONFDDIR}/nsspassword"
USE_NFLOG="${USE_NFLOG:-@@USE_NFLOG@@}"
DBPW=""
CACERTDIR="${IPSEC_CONFDDIR}/cacerts"
CRLDIR="${IPSEC_CONFDDIR}/crls"
CTLSOCKET="${IPSEC_RUNDIR}/pluto.ctl"
NSS_BINDIR="${NSS_BINDIR:-@@NSS_BINDIR@@}"

export IPSEC_EXECDIR IPSEC_CONF IPSEC_RUNDIR CTLSOCKET

# standardize PATH, and export it for everything else's benefit;
# should config.mk generate this?
PATH="${NSS_BINDIR}:${PATH#${NSS_BINDIR}:}"
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='^(_.*|.*\.old|.*\.orig|.*~)$'

# version numbering (details filled in by build)
export IPSEC_VERSION="@@IPSECVERSION@@"

ipsec_add() {
    #
    # ipsec add/replace command
    #
    local conn_name usage
    local verbose

    while [ ${#} -gt 0 ]; do
	case "${1}" in
	    add|--add)
		usage="Usage: ipsec add connectionname|--chekcconfig \\
		[--config ${IPSEC_CONF}] [--ctlsocket ${CTLSOCKET}] [--verbose]"
		shift
		;;
	    replace|--replace)
		usage="Usage: ipsec replace connectionname \\
		[--config ${IPSEC_CONF}] [--ctlsocket ${CTLSOCKET}] [--verbose]"
		shift
		;;
	    checkconfig)
		usage="Usage: ipsec checkconfig [--config ${IPSEC_CONF}] [--verbose]"
		checkconfig="--checkconfig"
		shift
		;;
	    help|--help|-h)
		echo "${usage}"
		shift
		exit 0
		;;
	    --config)
		IPSEC_CONF="${2}"
		export IPSEC_CONF
		shift
		shift
		;;
	    --ctlsocket)
		CTLSOCKET="${2}"
		export CTLSOCKET
		shift
		shift
		;;
	    --dry-run|-n)
		dry_run="echo"
		shift
		;;
	    --verbose)
		verbose="${1}"
		shift
		;;
	    --checkconfig)
		checkconfig="${1}"
		shift
		;;
	    -*)
		echo "Unknown option \"${1}\"" >&2
		echo >&2
		echo "${usage}" >&2
		exit 2
		;;
	    *)
		conn_name="${1}"
		shift
		;;
	esac
    done
    # addconn performs a replace.  FIrst it deletes all connections
    # and aliases matching ${conn_name} and then creates a new
    # connection or aliases.
    ${dry_run} "${IPSEC_EXECDIR}/addconn" --config "${IPSEC_CONF}" --ctlsocket "${CTLSOCKET}" ${checkconfig} ${verbose} ${conn_name}
    exit $?
}

ipsec_combined() {
    #
    # ipsec start and ipsec ondemand commands
    #
    local cmd conn_name usage
    local verbose

    while [ ${#} -gt 0 ]; do
	case "${1}" in
	    ondemand|--ondemand)
		cmd="--route"
		usage="Usage: ipsec ondemand [--asynchronous] connectionname \\
		[--remote-host ipaddr] [--config ${IPSEC_CONF}] \\
		[--ctlsocket ${CTLSOCKET}] [--verbose]"
		shift
		case ${1} in
		    --asynchronous)
			asynchronous="${1}"
			shift
			;;
		esac
		case ${1} in
		    "")
			echo "${usage}" >&2
			exit 2
			;;
		esac
		;;
	    start|--start)
		cmd="--initiate"
		usage="Usage: ipsec start [--asynchronous] connectionname \\
		[--remote-host ipaddr] [--config ${IPSEC_CONF}] \\
		[--ctlsocket ${CTLSOCKET}] [--verbose]"
		shift
		case ${1} in
		    --asynchronous)
			asynchronous="${1}"
			shift
			;;
		esac
		case ${1} in
		    "")
			echo "${usage}" >&2
			exit 2
			;;
		esac
		;;
	    help|--help|-h)
		echo "${usage}"
		exit 0
		;;
	    --asynchronous)
		asynchronous="${1}"
		shift
		;;
	    --config)
		IPSEC_CONF="${2}"
		export IPSEC_CONF
		shift
		shift
		;;
	    --ctlsocket)
		CTLSOCKET="${2}"
		export CTLSOCKET
		shift
		shift
		;;
	    --dry-run|-n)
		dry_run="echo"
		shift
		;;
	    --verbose)
		verbose="${1}"
		shift
		;;
	    --remote-host)
		remote_host="--remote-host ${2}"
		shift
		shift
		;;
	    -*)
		echo "Unknown option \"${1}\"" >&2
		echo >&2
		echo "${usage}" >&2
		exit 2
		;;
	    *)
		conn_name="${1}"
		shift
		;;
	esac
    done
    ${dry_run} "${IPSEC_EXECDIR}/addconn" --config "${IPSEC_CONF}" --ctlsocket "${CTLSOCKET}" ${verbose} ${conn_name}
    ${dry_run} "${IPSEC_EXECDIR}/whack" --ctlsocket "${CTLSOCKET}" ${asynchronous} --name ${conn_name} ${cmd} ${remote_host}
    exit $?
}

ipsec_redirect() {
    #
    # ipsec redirect command
    #
    local cmd conn_name redirect_to usage

    while [ ${#} -gt 0 ]; do
	case "${1}" in
	    redirect)
		cmd="--redirect-to"
		usage="Usage: ipsec redirect [connectionname] --redirect-to {ip-address,...} \\
		[--ctlsocket ${CTLSOCKET}]"
		shift
		case ${1} in
		    "")
			echo "${usage}" >&2
			exit 2
			;;
		esac
		;;
	    help|--help|-h)
		echo "${usage}"
		exit 0
		;;
	    --config)
		IPSEC_CONF="${2}"
		export IPSEC_CONF
		shift
		shift
		;;
	    --redirect-to)
		redirect_to="${2}"
		shift
		shift
		;;
	    --ctlsocket)
		CTLSOCKET="${2}"
		export CTLSOCKET
		shift
		shift
		;;
	    --dry-run|-n)
		dry_run="echo"
		shift
		;;
	    -*|"")
		echo "Unknown option \"${1}\"" >&2
		echo  >&2
		echo "${usage}" >&2
		exit 2
		;;
	    *)
		conn_name="${1}"
		shift
		;;
	esac
    done
    ${dry_run} "${IPSEC_EXECDIR}/whack" --ctlsocket "${CTLSOCKET}" --name ${conn_name} ${cmd} "${redirect_to}"
    exit $?
}

ipsec_whack() {
    #
    # ipsec whack commands with per-command help
    #
    local cmd conn_name usage
    local verbose

    while [ ${#} -gt 0 ]; do
	case "${1}" in
	    briefstatus|--briefstatus)
		cmd="--briefstatus"
		usage="Usage: ipsec briefstatus [--ctlsocket ${CTLSOCKET}]"
		shift
		;;
	    checkpubkeys|--checkpubkeys)
		cmd="--checkpubkeys"
		usage="Usage: ipsec checkpubkeys [--ctlsocket ${CTLSOCKET}]"
		shift
		;;
	    connectionstatus|--connectionstatus)
		cmd="--connectionstatus"
		usage="Usage: ipsec connectionstatus [connnectionname] [--ctlsocket ${CTLSOCKET}]"
		shift
		;;
	    briefconnectionstatus|--briefconnectionstatus)
		cmd="--briefconnectionstatus"
		usage="Usage: ipsec briefconnectionstatus [connnectionname] [--ctlsocket ${CTLSOCKET}]"
		shift
		;;
	    delete|--delete)
		cmd="--delete"
		usage="Usage: ipsec delete connectionname [--ctlsocket ${CTLSOCKET}]"
		shift
		case ${1} in
		    "")
			echo "${usage}" >&2
			exit 2
			;;
		esac
		;;
	    down|--down|terminate)
		cmd="--terminate" # or --down
		usage="Usage: ipsec down connectionname \\
		[--remote-host ipaddr] [--ctlsocket ${CTLSOCKET}]"
		case ${1} in
		    "")
			echo "${usage}" >&2
			exit 2
			;;
		esac
		shift
		;;
	    fetchcrls|--fetchcrls|crls)
		cmd="--fetchcrls"
		usage="Usage: ipsec fetchcrls [--ctlsocket ${CTLSOCKET}]"
		shift
		;;
	    fipsstatus|--fipsstatus|fips)
		cmd="--fipsstatus"
		usage="Usage: ipsec fipsstatus [--ctlsocket ${CTLSOCKET}]"
		shift
		;;
	    globalstatus|--globalstatus)
		cmd="--globalstatus"
		usage="Usage: ipsec globalstatus [--ctlsocket ${CTLSOCKET}]"
		shift
		;;
	    listall|--listall)
		cmd="--listall"
		usage="Usage: ipsec listall [--utc] [--ctlsocket ${CTLSOCKET}]"
		shift
		;;
	    listcacerts|--listcacerts)
		cmd="--listcacerts"
		usage="Usage: ipsec listcacerts [--utc] [--ctlsocket ${CTLSOCKET}]"
		shift
		;;
	    listcerts|--listcerts)
		cmd="--listcerts"
		usage="Usage: ipsec listcerts [--utc] [--ctlsocket ${CTLSOCKET}]"
		shift
		;;
	    listcrls|--listcrls)
		cmd="--listcrls"
		usage="Usage: ipsec listcrls [--utc] [--ctlsocket ${CTLSOCKET}]"
		shift
		;;
	    listen|--listen|--ready|--rereadgroups)
		cmd="--listen"
		usage="Usage: ipsec listen [--ctlsocket ${CTLSOCKET}]"
		shift
		;;
	    listpubkeys|--listpubkeys)
		cmd="--listpubkeys"
		usage="Usage: ipsec listpubkeys [--utc] [--ctlsocket ${CTLSOCKET}]"
		shift
		;;
	    purgeocsp|--purgeocsp)
		cmd="--purgeocsp"
		usage="Usage: ipsec purgeocsp [--ctlsocket ${CTLSOCKET}]"
		shift
		;;
	    rereadall|--rereadall)
		cmd="--rereadall"
		usage="Usage: ipsec rereadall [--ctlsocket ${CTLSOCKET}]"
		shift
		;;
	    rereadcerts|--rereadcerts)
		cmd="--rereadcerts"
		usage="Usage: ipsec rereadcerts [--ctlsocket ${CTLSOCKET}]"
		shift
		;;
	    rereadsecrets|--rereadsecrets|secrets)
		cmd="--rereadsecrets"
		usage="Usage: ipsec rereadsecrets|secrets [--ctlsocket ${CTLSOCKET}]"
		shift
		;;
	    route|--route)
		cmd="--route"
		usage="Usage: ipsec route connectionname [--ctlsocket ${CTLSOCKET}]"
		shift
		case ${1} in
		    "")
			echo "${usage}" >&2
			exit 2
			;;
		esac
		;;
	    showstates|--showstates|--statestatus)
		cmd="--showstates"
		usage="Usage: ipsec showstates [--ctlsocket ${CTLSOCKET}]"
		shift
		;;
	    shuntstatus|--shuntstatus)
		cmd="--shuntstatus"
		usage="Usage: ipsec shuntstatus [--ctlsocket ${CTLSOCKET}]"
		shift
		;;
	    status|--status)
		cmd="--status"
		usage="Usage: ipsec status [--ctlsocket ${CTLSOCKET}]"
		shift
		;;
	    trafficstatus|--trafficstatus|traffic)
		cmd="--trafficstatus"
		usage="Usage: ipsec trafficstatus [connectionname] [--ctlsocket ${CTLSOCKET}]"
		shift
		;;
	    unroute|--unroute)
		cmd="--unroute"
		usage="Usage: ipsec unroute connectionname [--ctlsocket ${CTLSOCKET}]"
		shift
		case ${1} in
		    "")
			echo "${usage}" >&2
			exit 2
			;;
		esac
		;;
	    up|--up|initiate|--initiate)
		cmd="--initiate"
		usage="Usage: ipsec up [--asynchronous] connectionname \\
		[--remote-host ipaddr] [--ctlsocket ${CTLSOCKET}]"
		shift
		case ${1} in
		    --asynchronous)
			asynchronous="${1}"
			shift
			;;
		esac
		case ${1} in
		    "")
			echo "${usage}" >&2
			exit 2
			;;
		esac
		;;
	    help|--help|-h)
		echo "${usage}"
		exit 0
		;;
	    --asynchronous)
		asynchronous="${1}"
		shift
		;;
	    --config)
		IPSEC_CONF="${2}"
		export IPSEC_CONF
		shift
		shift
		;;
	    --ctlsocket)
		CTLSOCKET="${2}"
		export CTLSOCKET
		shift
		shift
		;;
	    --remote-host)
		remote_host="--remote-host ${2}"
		shift
		shift
		;;
	    --dry-run|-n)
		dry_run="echo"
		shift
		;;
	    --utc)
		utc="${1}"
		shift
		;;
	    --verbose)
		verbose="${1}"
		shift
		;;
	    -*)
		echo "Unknown option \"${1}\"" >&2
		echo >&2
		echo "${usage}" >&2
		exit 2
		;;
	    *)
		conn_name="--name ${1}"
		shift
		;;
	esac
    done
    ${dry_run} "${IPSEC_EXECDIR}/whack" --ctlsocket "${CTLSOCKET}" ${asynchronous} ${conn_name} ${utc} ${cmd} ${remote_host}
    exit $?
}

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 "See also: man ipsec, ipsec help or ipsec {command} help"
    echo
    echo "See <https://libreswan.org> for more general info."
    echo "Libreswan ${IPSEC_VERSION}"
    exit 2
}

ipsec_help() {
    echo "Usage: ipsec {command} [argument] ...>"
    echo "where {command} is one of:"
    echo
    GOTTWO=""
    for f in \
	add \
	briefstatus \
	certutil \
	checkconfig \
	checknflog \
	checknss \
	connectionstatus \
	briefconnectionstatus \
	delete \
	down \
	fetchcrls \
	fipsstatus \
	import \
	initnss \
	listall \
	listcacerts \
	listcerts \
	listcrls \
	listen \
	listpubkeys \
	ondemand \
	pk12util \
	purgeocsp \
	replace \
	rereadall \
	rereadcerts \
	rereadsecrets \
	restart \
	route \
	showstates \
	shuntstatus \
	start \
	status \
	stop \
	trafficstatus \
	unroute \
	up \
	$(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\n" "${GOTTWO}"
    fi
    echo
    echo "See also: man ipsec {command} or ipsec {command} help"
    echo "See <https://libreswan.org/> for more general info."
    echo "Libreswan ${IPSEC_VERSION}"
    exit 0
}

# add nflog-all
nflog_nftables_add() {
	nft -f - <<EOF

table inet ipsec-log {
	chain input {
		type filter hook input priority filter; policy accept;
		meta ipsec exists log prefix "all-ipsec-input" group 50
	}

	chain output {
		type filter hook output priority filter; policy accept;
		rt ipsec exists log prefix "all-ipsec-output" group 50
	}
}

EOF
}

nflog_nftables_delete_table() {
    local table=$1

    t=$(nft list table inet $table 2>/dev/null | wc -l)
    [ ${t} -eq 2 ] && nft delete table inet ${table}
}

nflog_nftables_delete_chain() {
    local chain=$1

    t=$(nft list table inet ipsec-log 2>/dev/null | wc -l)
    if [ ${t} -gt 0 ]; then
	c=$(nft list chain inet ipsec-log ${chain} 2>/dev/null | wc -l)
	if [ ${c} -gt 0 ]; then
	    nft delete chain inet ipsec-log ${chain}
	    # assumed nothing else used this chain.
	    # one day check if something else used this chain.
	    # if yes only delete the rule this script added and warn?
	    # something like
	    # nft -a list chain inet ipsec-log input | grep -v "all-ipsec-input" | wc -l
	    # count -eq 5 . chagpti's idea of finding empty nftable chain
	fi
    fi
}

nflog_nftables_delete() {
    nflog_nftables_delete_chain input
    nflog_nftables_delete_chain output
    nflog_nftables_delete_table ipsec-log
}

nflog_iptables_delete() {
    local GROUP=$1
    iptables -D INPUT  -m policy --dir in  --pol ipsec -j NFLOG --nflog-group "${GROUP}" --nflog-prefix all-ipsec
    iptables -D OUTPUT -m policy --dir out --pol ipsec -j NFLOG --nflog-group "${GROUP}" --nflog-prefix all-ipsec
}

ipsec_stopnflog() {
    if [ "${USE_NFLOG}" != "true" ]; then
	echo "NFLOG support has been disabled" >&2
	exit 0
    fi
    if [ -s "${dry_run}" ]; then
	echo "ipsec stopnflog does not support --dry-run" 1>&2
	exit 1
    fi
    local usage
    usage="Usage: ipsec stopnflog"
    while [ ${#} -gt 0 ]; do
	case ${1} in
	    help|--help|-h)
		echo "${usage}"
		exit 0
		;;
	    *)
		echo "ipsec stopnflog: Unknown option \"${1}\"" >&2
		echo >&2
		echo "${usage}" >&2
		exit 2
		;;
	esac
	shift
    done

    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

    firewall_cmd

    if [ -n "${FIREWALL}" ]; then
	nflog_${FIREWALL}_delete "${NFGROUP}"
    fi

    exit 0
}

checknflog_iptables() {
    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}"
		nflog_iptables_delete "${OLDNFGROUP}"
	    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}"
	    nflog_iptables_delete "${OLDNFGROUP}"
	fi
	echo "nflog ipsec capture disabled"
    fi
}

checknflog_nftables() {
    if [ -n "${NFGROUP}" ]; then
	f=$(nft list table inet ipsec-log 2>/dev/null | wc -l)
	if [ ${f} -gt 0 ]; then
	    f=$(nft list chain inet ipsec-log input 2>/dev/null | wc -l)
	    if [ ${f} -gt 0 ]; then
		echo "found chain ipsec all-input"
		# this is incomplete need more work here to check both rules.
	    else
		nflog_nftables_add
	    fi
	else
	    nflog_nftables_add
	fi
    else
	nflog_nftables_delete
    fi
}

firewall_cmd() {
    USE_IPTABLES=@@USE_IPTABLES@@
    USE_NFTABLES=@@USE_NFTABLES@@

    if [ "${USE_NFTABLES}" = true ]; then
	FIREWALL=nftables
    elif [ "${USE_IPTABLES}" = true ]; then
	FIREWALL=iptables
    else
	FIREWALL=
    fi
}

ipsec_checknflog() {
    if [ "${USE_NFLOG}" != "true" ]; then
	echo "NFLOG support has been disabled" >&2
	exit 0
    fi
    if [ -s "${dry_run}" ]; then
	echo "ipsec checknflog does not support --dry-run" 1>&2
	exit 1
    fi
    local usage
    usage="Usage: ipsec checknflog"
    while [ ${#} -gt 0 ]; do
	case ${1} in
	    help|--help|-h)
		echo "${usage}"
		exit 0
		;;
	    *)
		echo "ipsec checknflog: Unknown option \"${1}\"" >&2
		echo >&2
		echo "${usage}" >&2
		exit 2
		;;
	esac
	shift
    done

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

    firewall_cmd

    if [ -n "${FIREWALL}" ]; then
	checknflog_${FIREWALL}
    fi

    exit 0
}

ipsec_sniff() {
    if [ "${USE_NFLOG}" != "true" ]; then
	echo "NFLOG support has been disabled" >&2
	exit 0
    fi
    if [ -s "${dry_run}" ]; then
	echo "ipsec sniff does not support --dry-run" 1>&2
	exit 1
    fi
    local usage
    usage="Usage: ipsec sniff"
    while [ ${#} -gt 0 ]; do
	case ${1} in
	    help|--help|-h)
		echo "${usage}"
		exit 0
		;;
	    *)
		echo "ipsec sniff: Unknown option \"${1}\"" >&2
		echo >&2
		echo "${usage}" >&2
		exit 2
		;;
	esac
	shift
    done

    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
    if [ -s "${dry_run}" ]; then
	echo "ipsec import does not support --dry-run" 1>&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
}

ipsec_nsscmd() {
    #
    # run nss-tools utilities with correct nss directory
    #

    if [ -z "${1}" ]; then
	echo "Usage: ipsec {certutil|crlutil||pk12util|vfychain} ..." >&2
	exit 1
    fi

    # vfychain on Fedora isn't in the default path.
    if [ -x "/usr/lib64/nss/unsupported-tools/${1}" ] ; then
	cmd="/usr/lib64/nss/unsupported-tools/${1}"
    else
	cmd="${1}"
    fi
    shift	# drop the command

    # reject -d and --nssdir outright
    case " $* " in
	*' -d '*|*' --nssdir '*)
	    echo "ERROR: ipsec {certutil|crlutil|pk12util|vfychain} do not accept --nssdir" >&2
	    exit 2
	    ;;
	*' --dry-run '*)
	    echo "ERROR: ipsec {certutil|crlutil|modutil|pk12util|vfychain} do not accept --dry-run" >&2
	    exit 2
	    ;;
	*' -dbdir '*)
	    echo "ERROR: ipsec {modutil} does not accept -dbdir" >&2
	    exit 2
	    ;;
    esac

    # modutil's nssdir option is different, ulgn
    case "${cmd}" in
	modutil)
	    exec ${dry_run} ${cmd} -dbdir ${IPSEC_NSSDIR} "${@}"
	    ;;
	*)
	    exec ${dry_run} ${cmd} -d ${IPSEC_NSSDIR} "${@}"
	    ;;
    esac
    # exec failed?
    exit $?
}

ipsec_setup() {
    #
    # ipsec setup commands with per-command help
    #
    local cmd usage

    while [ ${#} -gt 0 ]; do
	case "${1}" in
	    restart|--restart)
		cmd="--restart"
		usage="Usage: ipsec restart"
		;;
	    start|--start)
		cmd="--start"
		usage="Usage: ipsec start"
		;;
	    stop|--stop)
		cmd="--stop"
		usage="Usage: ipsec stop"
		;;
	    help|--help|-h)
		echo "${usage}"
		exit 0
		;;
	    --dry-run|-n)
		dry_run="echo"
		;;
	    -*)
		echo "Unknown option \"${1}\"" >&2
		echo >&2
		echo "${usage}" >&2
		exit 2
		;;
	esac
	shift
    done
    ${dry_run} "${IPSEC_EXECDIR}/setup" ${cmd}
    exit $?
}

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

# Make sure these variables are empty before parsing command line
verbose=
asynchronous=
dry_run=
remote_host=
utc=

while [ ${#} -gt 0 ]; do
    case "${1}" in
	--rundir)
	    RUNDIR="${2}"
	    CTLSOCKET="${RUNDIR}/pluto.ctl"
	    export CTLSOCKET
	    shift
	    shift
	    ;;
	--config)
	    IPSEC_CONF="${2}"
	    export IPSEC_CONF
	    shift
	    shift
	    ;;
	--ctlsocket)
	    CTLSOCKET="${2}"
	    export CTLSOCKET
	    shift
	    shift
	    ;;
	auto)
	    # ipsec_auto deprecated at 5.0, we eat this option for now
	    # so that ipsec auto <command> continues to work.
	    echo "WARNING: ipsec auto has been deprecated" >&2
	    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
	    case " ${@} " in
		*" --ctlsocket "*)
		    exec ${dry_run} "${IPSEC_EXECDIR}/whack" "${@}"
		    ;;
		*)
		    exec ${dry_run} "${IPSEC_EXECDIR}/whack" --ctlsocket "${CTLSOCKET}" "${@}"
		    ;;
	    esac
	    # exec failed?
	    exit $?
	    ;;
	add|--add|checkconfig|replace|--replace)
	    ipsec_add "${@}"
	    ;;
	briefstatus|--briefstatus|\
	checkpubkeys|--checkpubkeys|\
	connectionstatus|--connectionstatus|\
	briefconnectionstatus|--briefconnectionstatus|\
	delete|--delete|\
	down|--down|terminate|\
	fetchcrls|--fetchcrls|crls|\
	fipsstatus|--fipsstatus|fips|\
	globalstatus|--globalstatus|\
	listall|--listall|\
	listcacerts|--listcacerts|\
	listcerts|--listcerts|\
	listcrls|--listcrls|\
	listen|--listen|--ready|--rereadgroups|\
	listpubkeys|--listpubkeys|\
	purgeocsp|--purgeocsp|\
	rereadall|--rereadall|\
	rereadcerts|--rereadcerts|\
	rereadsecrets|--rereadsecrets|secrets|\
	route|--route|\
	showstates|--showstates|--statestatus|\
	shuntstatus|--shuntstatus|\
	status|--status|\
	trafficstatus|--trafficstatus|traffic|\
	unroute|--unroute|\
	up|--up|initiate|--initiate)
	    ipsec_whack "${@}"
	    ;;
	ondemand|--ondemand)
	    ipsec_combined "${@}"
	    ;;
	redirect)
	    ipsec_redirect "${@}"
	    ;;
	restart|--restart)
	    ipsec_setup "${@}"
	    ;;
	start|--start)
	    if [ -z "${2}" ]; then
		ipsec_setup "${@}"
	    else
	        ipsec_combined "${@}"
	    fi
	    ;;
	stop|--stop)
	    ipsec_setup "${@}"
	    ;;
	letsencrypt)
	    shift
	    exec ${dry_run} "${IPSEC_EXECDIR}/letsencrypt" "${@}"
	    # exec failed?
	    exit $?
	    ;;
	help|--help|-h)
	    ipsec_help
	    ;;
    	version|--version|--versioncode)
	    # some ubuntu/debian scripts use --versioncode, so let's keep the alias
	    echo "Libreswan ${IPSEC_VERSION}"
	    exit 0
	    ;;
	directory|--directory)
	    printf "%s\n" "${IPSEC_EXECDIR}"
	    exit 0
	    ;;
	stopnflog|--stopnflog)
	    shift
	    ipsec_stopnflog "${@}"
	    ;;
	checknflog|--checknflog|nflog)
	    shift
	    ipsec_checknflog "${@}"
	    ;;
	sniff|--sniff)
	    shift
	    ipsec_sniff "${@}"
	    ;;
	import|--import)
	    shift
	    ipsec_import "${@}"
	    ;;
	checknss|--checknss|initnss|--initnss)
	    ipsec_checknss "${@}"
	    ;;
	certutil|crlutil|modutil|pk12util|vfychain)
	    ipsec_nsscmd "${@}"
	    ;;
	unbound|--unbound)
	    # activate the unbound ipsec module
	    exec ${dry_run} unbound-control set_option ipsecmod-enabled: yes
	    # exec failed?
	    exit $?
	    ;;
	--asynchronous)
	    asynchronous="${1}"
	    shift
	    ;;
	--remote-host)
	    remote_host="--remote-host ${2}"
	    shift
	    shift
	    ;;
	--dry-run|-n)
	    dry_run="echo"
	    shift
	    ;;
	--utc)
	    utc="${1}"
	    shift
	    ;;
	-*)
	    printf "%s: unknown option \"%s\" (perhaps command name was omitted?)\n" "${0}" "${1}" >&2
	    exit 2
	    ;;
	*)
	    cmd="${1}"
	    shift
	    if [ -x "${IPSEC_EXECDIR}/${cmd}" ]; then
		exec ${dry_run} "${IPSEC_EXECDIR}/${cmd}" "${@}"
		# exec failed?
		exit $?
	    fi
	    printf "%s: unknown IPsec command \"%s\" (\"ipsec help\" for list)\n" "${0}" "${cmd}" >&2
	    exit 2
	    ;;
    esac
done