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