]> git.ipfire.org Git - people/stevee/network.git/blame - src/functions/functions.vpn-security-policies
network fix parameter passing when using ""
[people/stevee/network.git] / src / functions / functions.vpn-security-policies
CommitLineData
3eae7bed
JS
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2017 IPFire Network Development Team #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
22VPN_SECURITY_POLICIES_CONFIG_SETTINGS="CIPHER COMPRESSION GROUP_TYPE INTEGRITY KEY_EXCHANGE LIFETIME PFS"
878cfa3e 23VPN_SECURITY_POLICIES_READONLY="system performance"
3eae7bed 24
6fdbda80
MT
25VPN_DEFAULT_SECURITY_POLICY="system"
26
b116ad92 27declare -A VPN_SUPPORTED_CIPHERS=(
b68fed1f
MT
28 # 3DES-CBC
29 [3DES-CBC]="168 bit 3DES-EDE-CBC"
30
31 # AES-CBC
b116ad92
MT
32 [AES256-CBC]="256 bit AES-CBC"
33 [AES192-CBC]="192 bit AES-CBC"
34 [AES128-CBC]="128 bit AES-CBC"
b68fed1f
MT
35
36 # AES-CTR
37 [AES256-CTR]="256 bit AES-COUNTER"
38 [AES192-CTR]="192 bit AES-COUNTER"
39 [AES128-CTR]="128 bit AES-COUNTER"
40
41 # AES-GCM
42 [AES256-GCM128]="256 bit AES-GCM with 128 bit ICV"
43 [AES192-GCM128]="192 bit AES-GCM with 128 bit ICV"
44 [AES128-GCM128]="128 bit AES-GCM with 128 bit ICV"
45 [AES256-GCM96]="256 bit AES-GCM with 96 bit ICV"
46 [AES192-GCM96]="192 bit AES-GCM with 96 bit ICV"
47 [AES128-GCM96]="128 bit AES-GCM with 96 bit ICV"
48 [AES256-GCM64]="256 bit AES-GCM with 64 bit ICV"
49 [AES192-GCM64]="192 bit AES-GCM with 64 bit ICV"
50 [AES128-GCM64]="128 bit AES-GCM with 64 bit ICV"
51
52 # AES-CCM
53 [AES256-CCM128]="256 bit AES-CCM with 128 bit ICV"
54 [AES192-CCM128]="192 bit AES-CCM with 128 bit ICV"
55 [AES128-CCM128]="128 bit AES-CCM with 128 bit ICV"
56 [AES256-CCM96]="256 bit AES-CCM with 96 bit ICV"
57 [AES192-CCM96]="192 bit AES-CCM with 96 bit ICV"
58 [AES128-CCM96]="128 bit AES-CCM with 96 bit ICV"
59 [AES256-CCM64]="256 bit AES-CCM with 64 bit ICV"
60 [AES192-CCM64]="192 bit AES-CCM with 64 bit ICV"
61 [AES128-CCM64]="128 bit AES-CCM with 64 bit ICV"
62
63 # CAMELLIA-CBC
64 [CAMELLIA256-CBC]="256 bit CAMELLIA-CBC"
65 [CAMELLIA192-CBC]="192 bit CAMELLIA-CBC"
66 [CAMELLIA128-CBC]="128 bit CAMELLIA-CBC"
67
68 # CAMELLIA-CTR
69 [CAMELLIA256-CTR]="256 bit CAMELLIA-COUNTER"
70 [CAMELLIA192-CTR]="192 bit CAMELLIA-COUNTER"
71 [CAMELLIA128-CTR]="128 bit CAMELLIA-COUNTER"
72
73 # CAMELLIA-GCM
74 [CAMELLIA256-GCM128]="256 bit CAMELLIA-GCM with 128 bit ICV"
75 [CAMELLIA192-GCM128]="192 bit CAMELLIA-GCM with 128 bit ICV"
76 [CAMELLIA128-GCM128]="128 bit CAMELLIA-GCM with 128 bit ICV"
77 [CAMELLIA256-GCM96]="256 bit CAMELLIA-GCM with 96 bit ICV"
78 [CAMELLIA192-GCM96]="192 bit CAMELLIA-GCM with 96 bit ICV"
79 [CAMELLIA128-GCM96]="128 bit CAMELLIA-GCM with 96 bit ICV"
80 [CAMELLIA256-GCM64]="256 bit CAMELLIA-GCM with 64 bit ICV"
81 [CAMELLIA192-GCM64]="192 bit CAMELLIA-GCM with 64 bit ICV"
82 [CAMELLIA128-GCM64]="128 bit CAMELLIA-GCM with 64 bit ICV"
83
84 # CAMELLIA-CCM
85 [CAMELLIA256-CCM128]="256 bit CAMELLIA-CCM with 128 bit ICV"
86 [CAMELLIA192-CCM128]="192 bit CAMELLIA-CCM with 128 bit ICV"
87 [CAMELLIA128-CCM128]="128 bit CAMELLIA-CCM with 128 bit ICV"
88 [CAMELLIA256-CCM96]="256 bit CAMELLIA-CCM with 96 bit ICV"
89 [CAMELLIA192-CCM96]="192 bit CAMELLIA-CCM with 96 bit ICV"
90 [CAMELLIA128-CCM96]="128 bit CAMELLIA-CCM with 96 bit ICV"
91 [CAMELLIA256-CCM64]="256 bit CAMELLIA-CCM with 64 bit ICV"
92 [CAMELLIA192-CCM64]="192 bit CAMELLIA-CCM with 64 bit ICV"
93 [CAMELLIA128-CCM64]="128 bit CAMELLIA-CCM with 64 bit ICV"
d9a8af16
MT
94
95 # No Encryption
96 [NULL]="No Encryption"
b116ad92
MT
97)
98
d69af00f
MT
99declare -A CIPHER_TO_STRONGSWAN=(
100 # 3DES-CBC
101 [3DES-CBC]="3des"
102
103 # AES-CBC
104 [AES256-CBC]="aes256"
105 [AES192-CBC]="aes192"
106 [AES128-CBC]="aes128"
107
108 # AES-CTR
109 [AES256-CTR]="aes256ctr"
110 [AES192-CTR]="aes192ctr"
111 [AES128-CTR]="aes128ctr"
112
113 # AES-GCM
114 [AES256-GCM128]="aes256gcm128"
115 [AES192-GCM128]="aes192gcm128"
116 [AES128-GCM128]="aes128gcm128"
117 [AES256-GCM96]="aes256gcm96"
118 [AES192-GCM96]="aes192gcm96"
119 [AES128-GCM96]="aes128gcm96"
120 [AES256-GCM64]="aes256gcm64"
121 [AES192-GCM64]="aes192gcm64"
122 [AES128-GCM64]="aes128gcm64"
123
124 # AES-CCM
125 [AES256-CCM128]="aes256ccm128"
126 [AES192-CCM128]="aes192ccm128"
127 [AES128-CCM128]="aes128ccm128"
128 [AES256-CCM96]="aes256ccm96"
129 [AES192-CCM96]="aes192ccm96"
130 [AES128-CCM96]="aes128ccm96"
131 [AES256-CCM64]="aes256ccm64"
132 [AES192-CCM64]="aes192ccm64"
133 [AES128-CCM64]="aes128ccm64"
134
135 # CAMELLIA-CBC
136 [CAMELLIA256-CBC]="camellia256"
137 [CAMELLIA192-CBC]="camellia192"
138 [CAMELLIA128-CBC]="camellia128"
139
140 # CAMELLIA-CTR
141 [CAMELLIA256-CTR]="camellia256ctr"
142 [CAMELLIA192-CTR]="camellia192ctr"
143 [CAMELLIA128-CTR]="camellia128ctr"
144
145 # CAMELLIA-GCM
146 [CAMELLIA256-GCM128]="camellia256gcm128"
147 [CAMELLIA192-GCM128]="camellia192gcm128"
148 [CAMELLIA128-GCM128]="camellia128gcm128"
149 [CAMELLIA256-GCM96]="camellia256gcm96"
150 [CAMELLIA192-GCM96]="camellia192gcm96"
151 [CAMELLIA128-GCM96]="camellia128gcm96"
152 [CAMELLIA256-GCM64]="camellia256gcm64"
153 [CAMELLIA192-GCM64]="camellia192gcm64"
154 [CAMELLIA128-GCM64]="camellia128gcm64"
155
156 # CAMELLIA-CCM
157 [CAMELLIA256-CCM128]="camellia256ccm128"
158 [CAMELLIA192-CCM128]="camellia192ccm128"
159 [CAMELLIA128-CCM128]="camellia128ccm128"
160 [CAMELLIA256-CCM96]="camellia256ccm96"
161 [CAMELLIA192-CCM96]="camellia192ccm96"
162 [CAMELLIA128-CCM96]="camellia128ccm96"
163 [CAMELLIA256-CCM64]="camellia256ccm64"
164 [CAMELLIA192-CCM64]="camellia192ccm64"
165 [CAMELLIA128-CCM64]="camellia128ccm64"
d9a8af16
MT
166
167 # No Encryption
168 [NULL]="null"
d69af00f
MT
169)
170
3a0c376c
MT
171declare -A VPN_SUPPORTED_INTEGRITY=(
172 [MD5]="MD5-HMAC"
173
174 # SHA
175 [SHA1]="SHA1-HMAC"
3003747b 176 [SHA512]="512 bit SHA2-HMAC"
3a0c376c
MT
177 [SHA384]="384 bit SHA2-HMAC"
178 [SHA256]="256 bit SHA2-HMAC"
179
180 # AES
181 [AES-XCBC]="AES-XCBC"
182 [AES-CMAC]="AES-CMAC"
183 [AES256-GMAC]="256 bit AES-GMAC"
184 [AES192-GMAC]="192 bit AES-GMAC"
185 [AES128-GMAC]="128 bit AES-GMAC"
186)
187
d69af00f
MT
188declare -A INTEGRITY_TO_STRONGSWAN=(
189 [MD5]="md5"
190
191 # SHA
192 [SHA1]="sha1"
193 [SHA512]="sha512"
194 [SHA384]="sha384"
195 [SHA256]="sha256"
196
197 # AES
198 [AES-XCBC]="aesxcbc"
199 [AES-CMAC]="aescmac"
200 [AES256-GMAC]="aes256gmac"
201 [AES192-GMAC]="aes192gmac"
202 [AES128-GMAC]="aes128gmac"
203)
204
54fc6c3f
MT
205declare -A VPN_SUPPORTED_GROUP_TYPES=(
206 # Regular Groups
207 [MODP768]="768 bit Modulo Prime Group"
208 [MODP1024]="1024 bit Modulo Prime Group"
209 [MODP1536]="1536 bit Modulo Prime Group"
210 [MODP2048]="2048 bit Modulo Prime Group"
211 [MODP3072]="3072 bit Modulo Prime Group"
212 [MODP4096]="4096 bit Modulo Prime Group"
213 [MODP6144]="6144 bit Modulo Prime Group"
214 [MODP8192]="8192 bit Modulo Prime Group"
215
216 # NIST Elliptic Curve Groups
217 [ECP192]="192 bit NIST Elliptic Curve Group"
218 [ECP224]="224 bit NIST Elliptic Curve Group"
219 [ECP256]="256 bit NIST Elliptic Curve Group"
220 [ECP384]="384 bit NIST Elliptic Curve Group"
221 [ECP521]="521 bit NIST Elliptic Curve Group"
222
223 # Brainpool Elliptic Curve Groups
224 [ECP224BP]="224 bit Brainpool Elliptic Curve Group"
225 [ECP256BP]="256 bit Brainpool Elliptic Curve Group"
226 [ECP384BP]="384 bit Brainpool Elliptic Curve Group"
227 [ECP512BP]="512 bit Brainpool Elliptic Curve Group"
228
229 # Curve25519
230 [CURVE25519]="256 bit Elliptic Curve 25519"
231)
3eae7bed 232
d69af00f
MT
233declare -A GROUP_TYPE_TO_STRONGSWAN=(
234 # Regular Groups
235 [MODP768]="modp768"
236 [MODP1024]="modp1024"
237 [MODP1536]="modp1536"
238 [MODP2048]="modp2048"
239 [MODP3072]="modp3072"
240 [MODP4096]="modp4096"
241 [MODP6144]="modp6144"
242 [MODP8192]="modp8192"
243
244 # NIST Elliptic Curve Groups
245 [ECP192]="ecp192"
246 [ECP224]="ecp224"
247 [ECP256]="ecp256"
248 [ECP384]="ecp384"
249 [ECP521]="ecp521"
250
251 # Brainpool Elliptic Curve Groups
252 [ECP224BP]="ecp224bp"
253 [ECP256BP]="ecp256bp"
254 [ECP384BP]="ecp384bp"
255 [ECP512BP]="ecp512bp"
256
257 # Curve25519
258 [CURVE25519]="curve25519"
259)
260
2da98f56
MT
261cli_vpn_security_policies() {
262 local action
263 local security_policy
264
265 if vpn_security_policy_exists ${1}; then
266 security_policy=${1}
267 key=${2}
268 shift 2
269
270 case "${key}" in
271 cipher|compression|integrity|lifetime|pfs|show)
2212045f 272 vpn_security_policies_${key} ${security_policy} "$@"
2da98f56
MT
273 ;;
274 group-type)
2212045f 275 vpn_security_policies_group_type ${security_policy} "$@"
2da98f56
MT
276 ;;
277 key-exchange)
2212045f 278 vpn_security_policies_key_exchange ${security_policy} "$@"
2da98f56
MT
279 ;;
280 *)
281 error "Unrecognized argument: ${key}"
282 exit ${EXIT_ERROR}
283 ;;
284 esac
285 else
286 action=${1}
287 shift
288
289 case "${action}" in
290 new)
2212045f 291 vpn_security_policies_new "$@"
2da98f56
MT
292 ;;
293 destroy)
2212045f 294 vpn_security_policies_destroy "$@"
2da98f56
MT
295 ;;
296 ""|*)
297 if [ -n "${action}" ]; then
298 error "Unrecognized argument: '${action}'"
299 fi
300 exit ${EXIT_ERROR}
301 ;;
302 esac
303 fi
304}
305
6740c9c5
MT
306# This functions checks if a policy is readonly
307# returns true when yes and false when no
3eae7bed 308vpn_security_policies_check_readonly() {
3eae7bed
JS
309 if isoneof name ${VPN_SECURITY_POLICIES_READONLY}; then
310 return ${EXIT_TRUE}
311 else
312 return ${EXIT_FALSE}
313 fi
314}
315
6740c9c5 316# This function writes all values to a via ${name} specificated vpn security policy configuration file
3eae7bed 317vpn_security_policies_write_config() {
3eae7bed
JS
318 assert [ $# -ge 1 ]
319
320 local name="${1}"
321
6740c9c5 322 if ! vpn_security_policy_exists "${name}"; then
3eae7bed
JS
323 log ERROR "No such vpn security policy: ${name}"
324 return ${EXIT_ERROR}
325 fi
326
6740c9c5 327 if vpn_security_policies_check_readonly "${name}"; then
3eae7bed
JS
328 log ERROR "The ${name} vpn security policy cannot be changed."
329 return ${EXIT_ERROR}
330 fi
331
6740c9c5 332 local path="$(vpn_security_policies_path "${name}")"
3eae7bed
JS
333 if [ ! -w ${path} ]; then
334 log ERROR "${path} is not writeable"
335 return ${EXIT_ERROR}
336 fi
337
338 if ! settings_write "${path}" ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}; then
339 log ERROR "Could not write configuration settings for vpn security policy ${name}"
340 return ${EXIT_ERROR}
341 fi
342
f0e91d26
JS
343 if ! vpn_security_policies_reload ${name}; then
344 log WARNING "Could not reload the IPsec connection using this security policy"
345 return ${EXIT_ERROR}
346 fi
347}
348
349# reload IPsec connections using a special policy
350vpn_security_policies_reload() {
351 local name=${1}
352
353 local connection
354 for connection in $(ipsec_list_connections); do
5601f4f5
JS
355 local SECURITY_POLICY ENABLED
356
f0e91d26
JS
357 if ! ipsec_connection_read_config "${connection}" "SECURITY_POLICY"; then
358 continue
359 fi
360
5601f4f5 361 if [[ "${SECURITY_POLICY}" = "${name}" ]] && enabled ENABLED; then
f0e91d26
JS
362 if ! ipsec_connection_to_strongswan "${connection}"; then
363 log ERROR "Could not generate strongswan config for ${connnection}"
364 fi
365 fi
366 done
367
368 ipsec_strongswan_load
3eae7bed
JS
369}
370
6740c9c5 371# This funtion writes the value for one key to a via ${name} specificated vpn security policy configuration file
3eae7bed 372vpn_security_policies_write_config_key() {
3eae7bed 373 assert [ $# -ge 3 ]
6740c9c5 374
3eae7bed
JS
375 local name=${1}
376 local key=${2}
377 shift 2
6740c9c5 378
3eae7bed
JS
379 local value="$@"
380
6740c9c5 381 if ! vpn_security_policy_exists "${name}"; then
3eae7bed
JS
382 log ERROR "No such vpn security policy: ${name}"
383 return ${EXIT_ERROR}
384 fi
385
386 log DEBUG "Set '${key}' to new value '${value}' in vpn security policy ${name}"
387
388 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
389
390 # Read the config settings
6740c9c5 391 if ! vpn_security_policies_read_config "${name}"; then
3eae7bed
JS
392 return ${EXIT_ERROR}
393 fi
394
395 # Set the key to a new value
396 assign "${key}" "${value}"
397
6740c9c5 398 if ! vpn_security_policies_write_config "${name}"; then
3eae7bed
JS
399 return ${EXIT_ERROR}
400 fi
401
402 return ${EXIT_TRUE}
3eae7bed
JS
403}
404
6740c9c5 405# Reads one or more keys out of a settings file or all if no key is provided.
3eae7bed 406vpn_security_policies_read_config() {
3eae7bed
JS
407 assert [ $# -ge 1 ]
408
409 local name="${1}"
410 shift 1
411
6740c9c5 412 if ! vpn_security_policy_exists "${name}"; then
3eae7bed
JS
413 log ERROR "No such vpn security policy: ${name}"
414 return ${EXIT_ERROR}
415 fi
416
417
418 local args
419 if [ $# -eq 0 ] && [ -n "${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}" ]; then
420 list_append args ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
421 else
2212045f 422 list_append args "$@"
3eae7bed
JS
423 fi
424
425 local path="$(vpn_security_policies_path ${name})"
426
427 if ! settings_read "${path}" ${args}; then
428 log ERROR "Could not read settings for vpn security policy ${name}"
429 return ${EXIT_ERROR}
430 fi
431}
432
6740c9c5 433# Returns the path to a the configuration fora given name
3eae7bed 434vpn_security_policies_path() {
3eae7bed 435 assert [ $# -eq 1 ]
6740c9c5 436
3eae7bed
JS
437 local name=${1}
438
6740c9c5 439 if vpn_security_policies_check_readonly "${name}"; then
3eae7bed
JS
440 echo "${NETWORK_SHARE_DIR}/vpn/security-policies/${name}"
441 else
442 echo "${NETWORK_CONFIG_DIR}/vpn/security-policies/${name}"
443 fi
444}
445
6740c9c5 446# Print the content of a vpn security policy configuration file in a nice way
3eae7bed 447vpn_security_policies_show() {
3eae7bed 448 assert [ $# -eq 1 ]
6740c9c5 449
3eae7bed
JS
450 local name=${1}
451
452 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
3eae7bed
JS
453 if ! vpn_security_policies_read_config ${name}; then
454 return ${EXIT_ERROR}
455 fi
456
457 cli_print_fmt1 0 "Security Policy: ${name}"
458 cli_space
459
460 # This could be done in a loop but a loop is much more complicated
461 # because we print 'Group Types' but the variable is named 'GROUP_TYPES'
462 cli_print_fmt1 1 "Ciphers:"
d21db419
MT
463 local cipher
464 for cipher in ${CIPHER}; do
465 cli_print_fmt1 2 "${VPN_SUPPORTED_CIPHERS[${cipher}]-${cipher}}"
466 done
3eae7bed 467 cli_space
6740c9c5 468
3eae7bed 469 cli_print_fmt1 1 "Integrity:"
f3098f76
MT
470 local integrity
471 for integrity in ${INTEGRITY}; do
472 cli_print_fmt1 2 "${VPN_SUPPORTED_INTEGRITY[${integrity}]-${integrity}}"
473 done
3eae7bed 474 cli_space
6740c9c5 475
3eae7bed 476 cli_print_fmt1 1 "Group Types:"
84c1cc17
MT
477 local group_type
478 for group_type in ${GROUP_TYPE}; do
479 cli_print_fmt1 2 "${VPN_SUPPORTED_GROUP_TYPES[${group_type}]-${group_type}}"
480 done
3eae7bed
JS
481 cli_space
482
483 cli_print_fmt1 1 "Key Exchange:" "${KEY_EXCHANGE}"
6740c9c5
MT
484
485 # Key Lifetime
3eae7bed
JS
486 if isinteger LIFETIME && [ ${LIFETIME} -gt 0 ]; then
487 cli_print_fmt1 1 "Key Lifetime:" "$(format_time ${LIFETIME})"
488 else
489 log ERROR "The value for Key Lifetime is not a valid integer greater zero."
490 fi
6740c9c5
MT
491
492 # PFS
3eae7bed
JS
493 if enabled PFS; then
494 cli_print_fmt1 1 "Perfect Forward Secrecy:" "enabled"
495 else
496 cli_print_fmt1 1 "Perfect Forward Secrecy:" "disabled"
497 fi
498 cli_space
6740c9c5
MT
499
500 # Compression
3eae7bed
JS
501 if enabled COMPRESSION; then
502 cli_print_fmt1 1 "Compression:" "enabled"
503 else
504 cli_print_fmt1 1 "Compression:" "disabled"
505 fi
506 cli_space
507}
508
6740c9c5
MT
509# This function checks if a vpn security policy exists
510# Returns True when yes and false when not
3eae7bed 511vpn_security_policy_exists() {
3eae7bed 512 assert [ $# -eq 1 ]
6740c9c5 513
3eae7bed
JS
514 local name=${1}
515
6740c9c5
MT
516 local path=$(vpn_security_policies_path "${name}")
517
518 [ -f ${path} ] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
3eae7bed
JS
519}
520
521
6740c9c5 522# This function parses the parameters for the 'cipher' command
3eae7bed 523vpn_security_policies_cipher(){
3eae7bed
JS
524 local name=${1}
525 shift
526
527 if [ $# -eq 0 ]; then
528 log ERROR "You must pass at least one value after cipher"
529 return ${EXIT_ERROR}
530 fi
531
532 local CIPHER
3eae7bed
JS
533 if ! vpn_security_policies_read_config ${name} "CIPHER"; then
534 return ${EXIT_ERROR}
535 fi
536
537 # Remove duplicated entries to proceed the list safely
538 CIPHER="$(list_unique ${CIPHER})"
539
d9a6c183
MT
540 local ciphers_added
541 local ciphers_removed
542 local ciphers_set
543
3eae7bed 544 while [ $# -gt 0 ]; do
d9a6c183
MT
545 local arg="${1}"
546
547 case "${arg}" in
548 +*)
549 list_append ciphers_added "${arg:1}"
550 ;;
3eae7bed 551 -*)
d9a6c183 552 list_append ciphers_removed "${arg:1}"
3eae7bed 553 ;;
d9a6c183
MT
554 [A-Z0-9]*)
555 list_append ciphers_set "${arg}"
556 ;;
557 *)
558 error "Invalid argument: ${arg}"
559 return ${EXIT_ERROR}
3eae7bed
JS
560 ;;
561 esac
562 shift
563 done
564
d9a6c183
MT
565 # Check if the user is trying a mixed operation
566 if ! list_is_empty ciphers_set && (! list_is_empty ciphers_added || ! list_is_empty ciphers_removed); then
567 error "You cannot reset the cipher list and add or remove ciphers at the same time"
568 return ${EXIT_ERROR}
569 fi
570
571 # Set new cipher list
572 if ! list_is_empty ciphers_set; then
573 # Check if all ciphers are valid
574 local cipher
575 for cipher in ${ciphers_set}; do
576 if ! vpn_security_policies_cipher_supported ${cipher}; then
577 error "Unsupported cipher: ${cipher}"
578 return ${EXIT_ERROR}
579 fi
580 done
581
6eb85c57 582 CIPHER="${ciphers_set}"
d9a6c183
MT
583
584 # Perform incremental updates
3eae7bed 585 else
d9a6c183
MT
586 local cipher
587
588 # Perform all removals
589 for cipher in ${ciphers_removed}; do
590 if ! list_remove CIPHER ${cipher}; then
591 warning "${cipher} was not on the list and could not be removed"
592 fi
593 done
594
595 for cipher in ${ciphers_added}; do
596 if vpn_security_policies_cipher_supported ${cipher}; then
597 if ! list_append_unique CIPHER ${cipher}; then
598 warning "${cipher} is already on the cipher list"
599 fi
600 else
601 warning "${cipher} is unknown or unsupported and could not be added"
602 fi
603 done
604 fi
605
606 # Check if the list contain at least one valid cipher
607 if list_is_empty CIPHER; then
608 error "Cannot save an empty cipher list"
3eae7bed
JS
609 return ${EXIT_ERROR}
610 fi
d9a6c183
MT
611
612 # Save everything
613 if ! vpn_security_policies_write_config_key ${name} "CIPHER" ${CIPHER}; then
614 log ERROR "The changes for the vpn security policy ${name} could not be written."
615 fi
616
617 cli_headline 1 "Current cipher list for ${name}:"
618 for cipher in ${CIPHER}; do
619 cli_print_fmt1 1 "${cipher}" "${VPN_SUPPORTED_CIPHERS[${cipher}]}"
620 done
3eae7bed
JS
621}
622
6740c9c5 623# This function parses the parameters for the 'compression' command
3eae7bed 624vpn_security_policies_compression(){
3eae7bed
JS
625 local name=${1}
626 local value=${2}
627
628 # Check if we get only one argument after compression <name>
629 if [ ! $# -eq 2 ]; then
630 log ERROR "The number of arguments do not match. Only one argument after compression is allowed."
631 return ${EXIT_ERROR}
632 fi
633
634 if ! isbool value; then
635 # We suggest only two values to avoid overburding the user.
636 log ERROR "Invalid Argument ${value}"
637 return ${EXIT_ERROR}
638 fi
639
640 vpn_security_policies_write_config_key "${name}" "COMPRESSION" "${value}"
641}
642
6740c9c5 643# This function parses the parameters for the 'group-type' command
3eae7bed 644vpn_security_policies_group_type(){
3eae7bed
JS
645 local name=${1}
646 shift
647
648 if [ $# -eq 0 ]; then
649 log ERROR "You must pass at least one value after group-type"
650 return ${EXIT_ERROR}
651 fi
652
653 local GROUP_TYPE
3eae7bed
JS
654 if ! vpn_security_policies_read_config ${name} "GROUP_TYPE"; then
655 return ${EXIT_ERROR}
656 fi
657
658 # Remove duplicated entries to proceed the list safely
659 GROUP_TYPE="$(list_unique ${GROUP_TYPE})"
660
1b58f970
JS
661 local group_types_added
662 local group_types_removed
663 local group_types_set
664
3eae7bed 665 while [ $# -gt 0 ]; do
1b58f970
JS
666 local arg="${1}"
667
668 case "${arg}" in
669 +*)
670 list_append group_types_added "${arg:1}"
671 ;;
3eae7bed 672 -*)
1b58f970 673 list_append group_types_removed "${arg:1}"
3eae7bed 674 ;;
1b58f970
JS
675 [A-Z0-9]*)
676 list_append group_types_set "${arg}"
677 ;;
678 *)
679 error "Invalid argument: ${arg}"
680 return ${EXIT_ERROR}
3eae7bed
JS
681 ;;
682 esac
683 shift
684 done
685
1b58f970
JS
686 # Check if the user is trying a mixed operation
687 if ! list_is_empty group_types_set && (! list_is_empty group_types_added || ! list_is_empty group_types_removed); then
688 error "You cannot reset the group type list and add or remove group types at the same time"
689 return ${EXIT_ERROR}
690 fi
691
692 # Set new group type list
693 if ! list_is_empty group_types_set; then
694 # Check if all group types are valid
695 local group_type
696 for group_type in ${group_types_set}; do
697 if ! vpn_security_policies_group_type_supported ${group_type}; then
698 error "Unsupported group type: ${group_type}"
699 return ${EXIT_ERROR}
700 fi
701 done
702
703 GROUP_TYPE="${group_types_set}"
704
705 # Perform incremental updates
3eae7bed 706 else
1b58f970
JS
707 local group_type
708
709 # Perform all removals
710 for group_type in ${group_types_removed}; do
711 if ! list_remove GROUP_TYPE ${group_type}; then
712 warning "${group_type} was not on the list and could not be removed"
713 fi
714 done
715
716 for group_type in ${group_types_added}; do
717 if vpn_security_policies_group_type_supported ${group_type}; then
718 if ! list_append_unique GROUP_TYPE ${group_type}; then
719 warning "${group_type} is already on the group type list"
720 fi
721 else
722 warning "${group_type} is unknown or unsupported and could not be added"
723 fi
724 done
725 fi
726
727 # Check if the list contain at least one valid group_type
728 if list_is_empty GROUP_TYPE; then
729 error "Cannot save an empty group type list"
3eae7bed
JS
730 return ${EXIT_ERROR}
731 fi
1b58f970
JS
732
733 # Save everything
734 if ! vpn_security_policies_write_config_key ${name} "GROUP_TYPE" ${GROUP_TYPE}; then
735 log ERROR "The changes for the vpn security policy ${name} could not be written."
736 fi
737
738 cli_headline 1 "Current group type list for ${name}:"
739 for group_type in ${GROUP_TYPE}; do
740 cli_print_fmt1 1 "${group_type}" "${VPN_SUPPORTED_GROUP_TYPES[${group_type}]}"
741 done
3eae7bed 742}
6740c9c5
MT
743
744# This function parses the parameters for the 'integrity' command
3eae7bed 745vpn_security_policies_integrity(){
3eae7bed
JS
746 local name=${1}
747 shift
748
749 if [ $# -eq 0 ]; then
1b58f970 750 log ERROR "You must pass at least one value after integrity"
3eae7bed
JS
751 return ${EXIT_ERROR}
752 fi
753
754 local INTEGRITY
3eae7bed
JS
755 if ! vpn_security_policies_read_config ${name} "INTEGRITY"; then
756 return ${EXIT_ERROR}
757 fi
758
759 # Remove duplicated entries to proceed the list safely
760 INTEGRITY="$(list_unique ${INTEGRITY})"
761
1b58f970
JS
762 local integritys_added
763 local integritys_removed
764 local integritys_set
765
3eae7bed 766 while [ $# -gt 0 ]; do
1b58f970
JS
767 local arg="${1}"
768
769 case "${arg}" in
770 +*)
771 list_append integritys_added "${arg:1}"
772 ;;
3eae7bed 773 -*)
1b58f970 774 list_append integritys_removed "${arg:1}"
3eae7bed 775 ;;
1b58f970
JS
776 [A-Z0-9]*)
777 list_append integritys_set "${arg}"
778 ;;
779 *)
780 error "Invalid argument: ${arg}"
781 return ${EXIT_ERROR}
3eae7bed
JS
782 ;;
783 esac
784 shift
785 done
786
1b58f970
JS
787 # Check if the user is trying a mixed operation
788 if ! list_is_empty integritys_set && (! list_is_empty integritys_added || ! list_is_empty integritys_removed); then
789 error "You cannot reset the integrity hashes list and add or remove integrity hashes at the same time"
790 return ${EXIT_ERROR}
791 fi
792
793 # Set new integrity list
794 if ! list_is_empty integritys_set; then
795 # Check if all integrity hashes are valid
796 local integrity
797 for integrity in ${integritys_set}; do
798 if ! vpn_security_policies_integrity_supported ${integrity}; then
799 error "Unsupported integrity hash: ${integrity}"
800 return ${EXIT_ERROR}
801 fi
802 done
803
804 INTEGRITY="${integritys_set}"
805
806 # Perform incremental updates
3eae7bed 807 else
1b58f970
JS
808 local integrity
809
810 # Perform all removals
811 for integrity in ${integritys_removed}; do
812 if ! list_remove INTEGRITY ${integrity}; then
813 warning "${integrity} was not on the list and could not be removed"
814 fi
815 done
816
817 for integrity in ${integritys_added}; do
818 if vpn_security_policies_integrity_supported ${integrity}; then
819 if ! list_append_unique INTEGRITY ${integrity}; then
820 warning "${integrity} is already on the integrity list"
821 fi
822 else
823 warning "${integrity} is unknown or unsupported and could not be added"
824 fi
825 done
826 fi
827
828 # Check if the list contain at least one valid integrity
829 if list_is_empty INTEGRITY; then
830 error "Cannot save an empty integrity hashes list"
3eae7bed
JS
831 return ${EXIT_ERROR}
832 fi
1b58f970
JS
833
834 # Save everything
835 if ! vpn_security_policies_write_config_key ${name} "INTEGRITY" ${INTEGRITY}; then
836 log ERROR "The changes for the vpn security policy ${name} could not be written."
837 fi
838
839 cli_headline 1 "Current integrity hashes list for ${name}:"
840 for integrity in ${INTEGRITY}; do
841 cli_print_fmt1 1 "${integrity}" "${VPN_SUPPORTED_INTEGRITY[${integrity}]}"
842 done
3eae7bed
JS
843}
844
6740c9c5 845# This function parses the parameters for the 'key-exchange' command
3eae7bed 846vpn_security_policies_key_exchange() {
3eae7bed
JS
847 local name=${1}
848 local value=${2}
6740c9c5 849
3eae7bed
JS
850 # Check if we get only one argument after key-exchange <name>
851 if [ ! $# -eq 2 ]; then
852 log ERROR "The number of arguments do not match. Only argument after key-exchange is allowed."
853 return ${EXIT_ERROR}
854 fi
855
3eae7bed
JS
856 if ! isoneof value "ikev1" "ikev2" "IKEV1" "IKEV2"; then
857 log ERROR "Invalid Argument ${value}"
858 return ${EXIT_ERROR}
859 fi
860
861 vpn_security_policies_write_config_key "${name}" "KEY_EXCHANGE" "${value,,}"
862}
863
6740c9c5 864# This function parses the parameters for the 'lifetime' command.
3eae7bed 865vpn_security_policies_lifetime(){
3eae7bed
JS
866 local name=${1}
867 shift
6740c9c5 868
3eae7bed
JS
869 local value=$@
870
871 # Check if we get only one argument after lifetime <name>
872 if [ ! $# -ge 1 ]; then
873 log ERROR "The number of arguments do not match you must provide at least one integer value or a valid time with the format <hours>h <minutes>m <seconds>s"
874 return ${EXIT_ERROR}
875 fi
876
877 if ! isinteger value; then
2212045f 878 value=$(parse_time "$@")
3eae7bed
JS
879 if [ ! $? -eq 0 ]; then
880 log ERROR "Parsing the passed time was not sucessful please check the passed values."
881 return ${EXIT_ERROR}
882 fi
883 fi
884
885 if [ ${value} -le 0 ]; then
886 log ERROR "The passed time value must be in the sum greater zero seconds."
887 return ${EXIT_ERROR}
888 fi
889
890 vpn_security_policies_write_config_key "${name}" "LIFETIME" "${value}"
891}
892
6740c9c5 893# This function parses the parameters for the 'pfs' command
3eae7bed 894vpn_security_policies_pfs(){
3eae7bed
JS
895 local name=${1}
896 local value=${2}
897
898 # Check if we get only one argument after pfs <name>
899 if [ ! $# -eq 2 ]; then
900 log ERROR "The number of arguments do not match. Only argument after pfs is allowed."
901 return ${EXIT_ERROR}
902 fi
903
904 if [ ! $# -eq 2 ] || ! isbool value; then
905 # We suggest only two values to avoid overburding the user.
906 log ERROR "Invalid Argument ${value}"
907 return ${EXIT_ERROR}
908 fi
909
910 vpn_security_policies_write_config_key "${name}" "PFS" "${value}"
911}
912
6740c9c5
MT
913# This function checks if a vpn security policy name is valid
914# Allowed are only A-Za-z0-9
3eae7bed 915vpn_security_policies_check_name() {
3eae7bed 916 assert [ $# -eq 1 ]
6740c9c5 917
3eae7bed 918 local name=${1}
6740c9c5 919
3eae7bed
JS
920 [[ ${name} =~ [^[:alnum:]$] ]]
921}
922
6740c9c5 923# Function that creates based on the paramters one ore more new vpn security policies
3eae7bed 924vpn_security_policies_new() {
58aa0edf
MT
925 if [ $# -gt 1 ]; then
926 error "Too many arguments"
3eae7bed
JS
927 return ${EXIT_ERROR}
928 fi
929
58aa0edf
MT
930 local name="${1}"
931 if ! isset name; then
932 error "Please provide a name"
933 return ${EXIT_ERROR}
934 fi
3eae7bed 935
58aa0edf
MT
936 # Check for duplicates
937 if vpn_security_policy_exists "${name}"; then
938 error "The VPN security policy with name ${name} already exists"
939 return ${EXIT_ERROR}
940 fi
3eae7bed 941
58aa0edf
MT
942 # Check if name is valid
943 if vpn_security_policies_check_name "${name}"; then
944 error "'${name}' contains illegal characters"
945 return ${EXIT_ERROR}
946 fi
3eae7bed 947
58aa0edf
MT
948 # Check if we have a read-only policy with the same name
949 if vpn_security_policies_check_readonly "${name}"; then
950 error "The VPN security policy ${name} is read-only"
951 return ${EXIT_ERROR}
952 fi
953
6fdbda80
MT
954 # Check if our source policy exists
955 if ! vpn_security_policy_exists "${VPN_DEFAULT_SECURITY_POLICY}"; then
956 error "Default VPN Security Policy '${VPN_DEFAULT_SECURITY_POLICY}' does not exist"
957 return ${EXIT_ERROR}
958 fi
959
58aa0edf
MT
960 log DEBUG "Creating VPN Security Policy ${name}"
961
88013fd8 962 if copy "$(vpn_security_policies_path "${VPN_DEFAULT_SECURITY_POLICY}")" \
aab75c48 963 "$(vpn_security_policies_path ${name})"; then
58aa0edf
MT
964 log INFO "VPN Security Policy ${name} successfully created"
965 else
966 log ERROR "Could not create VPN Security Policy ${name}"
967 return ${EXIT_ERROR}
968 fi
c787fea5
MT
969
970 # Show the newly created policy
971 vpn_security_policies_show "${name}"
3eae7bed
JS
972}
973
6740c9c5 974# Function that deletes based on the passed parameters one ore more vpn security policies
3eae7bed 975vpn_security_policies_destroy() {
3eae7bed 976 local name
2212045f 977 for name in "$@"; do
3eae7bed
JS
978 if ! vpn_security_policy_exists ${name}; then
979 log ERROR "The vpn security policy ${name} does not exist."
980 continue
981 fi
982
983 if vpn_security_policies_check_readonly ${name}; then
984 log ERROR "The vpn security policy ${name} cannot be deleted."
985 continue
986 fi
987
988 log DEBUG "Deleting vpn security policy ${name}"
989 settings_remove $(vpn_security_policies_path ${name})
26b47a99
MT
990
991 # Delete cache
992 rm -rf "${NETWORK_CACHE_DIR}/vpn/security-policies/${name}"
3eae7bed
JS
993 done
994}
d69af00f 995
d9a6c183
MT
996vpn_security_policies_cipher_supported() {
997 local cipher=${1}
998
999 list_match ${cipher} ${!VPN_SUPPORTED_CIPHERS[@]}
1000}
1001
1b58f970
JS
1002
1003vpn_security_policies_group_type_supported() {
1004 local group_type=${1}
1005
1006 list_match ${group_type} ${!VPN_SUPPORTED_GROUP_TYPES[@]}
1007}
1008
1009vpn_security_policies_integrity_supported() {
1010 local integrity=${1}
1011
1012 list_match ${integrity} ${!VPN_SUPPORTED_INTEGRITY[@]}
1013}
1014
0d645497
MT
1015vpn_security_policies_cipher_is_aead() {
1016 local cipher=${1}
1017
1018 # All CCM and GCM ciphers are AEAD
1019 string_match "[CG]CM" "${cipher}"
1020}
1021
e3ffacf7 1022vpn_security_policies_make_ike_proposal() {
d69af00f
MT
1023 local name=${1}
1024
e1947a76
MT
1025 if ! vpn_security_policy_exists ${name}; then
1026 return ${EXIT_ERROR}
1027 fi
1028
1029 local config_path="$(vpn_security_policies_path ${name})"
e3ffacf7 1030 local cache_path="${NETWORK_CACHE_DIR}/vpn/security-policies/${name}/ike-proposal"
e1947a76
MT
1031
1032 # Get data from cache if possible
1033 if file_exists "${cache_path}" && ! file_is_newer_than "${config_path}" "${cache_path}"; then
1034 fread "${cache_path}"
1035 return ${EXIT_OK}
1036 fi
1037
1038 # No or invalid cache data found
e3ffacf7 1039 local proposal=$(_vpn_security_policies_make_ike_proposal "${name}")
e1947a76
MT
1040
1041 # Write proposal to cache
1042 if ! make_parent_dir "${cache_path}" || ! fwrite "${cache_path}" "${proposal}"; then
1043 log WARNING "Could not write to cache: ${cache_path}"
1044 fi
1045
1046 print "${proposal}"
1047}
1048
e3ffacf7 1049_vpn_security_policies_make_ike_proposal() {
e1947a76
MT
1050 local name=${1}
1051
d69af00f
MT
1052 # Read the config settings
1053 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
1054 if ! vpn_security_policies_read_config "${name}"; then
1055 return ${EXIT_ERROR}
1056 fi
1057
1058 local proposals
1059
1060 local cipher
1061 for cipher in ${CIPHER}; do
1062 # Translate cipher
1063 local _cipher=${CIPHER_TO_STRONGSWAN[${cipher}]}
1064
1065 if ! isset _cipher; then
1066 log WARN "Unsupported cipher: ${cipher}"
1067 continue
1068 fi
1069
1070 local integrity
1071 for integrity in ${INTEGRITY}; do
1072 local _integrity=${INTEGRITY_TO_STRONGSWAN[${integrity}]}
1073
1074 if ! isset _integrity; then
1075 log WARN "Unsupported integrity: ${integrity}"
1076 continue
1077 fi
1078
1079 local group_type
1080 for group_type in ${GROUP_TYPE}; do
1081 local _group_type=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1082
1083 if ! isset _group_type; then
1084 log WARN "Unsupported group-type: ${group_type}"
1085 continue
1086 fi
1087
1088 # Put everything together
1089 list_append proposals "${_cipher}-${_integrity}-${_group_type}"
1090 done
1091 done
1092 done
1093
1094 # Returns as a comma-separated list
1095 list_join proposals ,
1096}
0d645497
MT
1097
1098vpn_security_policies_make_esp_proposal() {
1099 local name=${1}
1100
e1947a76
MT
1101 if ! vpn_security_policy_exists ${name}; then
1102 return ${EXIT_ERROR}
1103 fi
1104
1105 local config_path="$(vpn_security_policies_path ${name})"
1106 local cache_path="${NETWORK_CACHE_DIR}/vpn/security-policies/${name}/esp-proposal"
1107
1108 # Get data from cache if possible
1109 if file_exists "${cache_path}" && ! file_is_newer_than "${config_path}" "${cache_path}"; then
1110 fread "${cache_path}"
1111 return ${EXIT_OK}
1112 fi
1113
1114 # No or invalid cache data found
1115 local proposal=$(_vpn_security_policies_make_esp_proposal "${name}")
1116
1117 # Write proposal to cache
1118 if ! make_parent_dir "${cache_path}" || ! fwrite "${cache_path}" "${proposal}"; then
1119 log WARNING "Could not write to cache: ${cache_path}"
1120 fi
1121
1122 print "${proposal}"
1123}
1124
1125_vpn_security_policies_make_esp_proposal() {
1126 local name=${1}
1127
0d645497
MT
1128 # Read the config settings
1129 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
1130 if ! vpn_security_policies_read_config "${name}"; then
1131 return ${EXIT_ERROR}
1132 fi
1133
1134 local proposals
1135
1136 local cipher
1137 for cipher in ${CIPHER}; do
1138 # Translate cipher
1139 local _cipher=${CIPHER_TO_STRONGSWAN[${cipher}]}
1140
1141 if ! isset _cipher; then
1142 log WARN "Unsupported cipher: ${cipher}"
1143 continue
1144 fi
1145
1146 if vpn_security_policies_cipher_is_aead ${cipher}; then
1147 local group_type
1148 for group_type in ${GROUP_TYPE}; do
1149 local _group_type=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1150
1151 if ! isset _group_type; then
1152 log WARN "Unsupported group-type: ${group_type}"
1153 continue
1154 fi
1155
1156 # Put everything together
1157 list_append proposals "${_cipher}-${_group_type}"
1158 done
1159 else
1160 local integrity
1161 for integrity in ${INTEGRITY}; do
1162 local _integrity=${INTEGRITY_TO_STRONGSWAN[${integrity}]}
1163
1164 if ! isset _integrity; then
1165 log WARN "Unsupported integrity: ${integrity}"
1166 continue
1167 fi
1168
1169 local group_type
1170 for group_type in ${GROUP_TYPE}; do
1171 local _group_type=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1172
1173 if ! isset _group_type; then
1174 log WARN "Unsupported group-type: ${group_type}"
1175 continue
1176 fi
1177
1178 # Put everything together
1179 list_append proposals "${_cipher}-${_integrity}-${_group_type}"
1180 done
1181 done
1182 fi
1183 done
1184
1185 # Returns as a comma-separated list
1186 list_join proposals ,
1187}
a00e7803
JS
1188
1189# List all security policies
1190vpn_security_policies_list_all() {
1191 local security_policy
1192 for security_policy in ${NETWORK_SHARE_DIR}/vpn/security-policies/*; do
1193 [ -f ${security_policy} ] || continue
1194 basename ${security_policy}
1195 done
1196
3cac4fcd
MT
1197 # Add all user-defined policies
1198 vpn_security_policies_list_user
1199}
1200
1201vpn_security_policies_list_user() {
1202 local security_policy
a00e7803
JS
1203 for security_policy in ${NETWORK_CONFIG_DIR}/vpn/security-policies/*; do
1204 [ -f ${security_policy} ] || continue
3cac4fcd 1205
a00e7803
JS
1206 basename ${security_policy}
1207 done
1208}