]> git.ipfire.org Git - people/ms/network.git/blob - src/functions/functions.vpn-security-policies
ipsec: reload connection when the security policy changes
[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 if ! ipsec_connection_read_config "${connection}" "SECURITY_POLICY"; then
350 continue
351 fi
352
353 if [[ "${SECURITY_POLICY}" = "${name}" ]]; then
354 if ! ipsec_connection_to_strongswan "${connection}"; then
355 log ERROR "Could not generate strongswan config for ${connnection}"
356 fi
357 fi
358 done
359
360 ipsec_strongswan_load
361 }
362
363 # This funtion writes the value for one key to a via ${name} specificated vpn security policy configuration file
364 vpn_security_policies_write_config_key() {
365 assert [ $# -ge 3 ]
366
367 local name=${1}
368 local key=${2}
369 shift 2
370
371 local value="$@"
372
373 if ! vpn_security_policy_exists "${name}"; then
374 log ERROR "No such vpn security policy: ${name}"
375 return ${EXIT_ERROR}
376 fi
377
378 log DEBUG "Set '${key}' to new value '${value}' in vpn security policy ${name}"
379
380 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
381
382 # Read the config settings
383 if ! vpn_security_policies_read_config "${name}"; then
384 return ${EXIT_ERROR}
385 fi
386
387 # Set the key to a new value
388 assign "${key}" "${value}"
389
390 if ! vpn_security_policies_write_config "${name}"; then
391 return ${EXIT_ERROR}
392 fi
393
394 return ${EXIT_TRUE}
395 }
396
397 # Reads one or more keys out of a settings file or all if no key is provided.
398 vpn_security_policies_read_config() {
399 assert [ $# -ge 1 ]
400
401 local name="${1}"
402 shift 1
403
404 if ! vpn_security_policy_exists "${name}"; then
405 log ERROR "No such vpn security policy: ${name}"
406 return ${EXIT_ERROR}
407 fi
408
409
410 local args
411 if [ $# -eq 0 ] && [ -n "${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}" ]; then
412 list_append args ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
413 else
414 list_append args $@
415 fi
416
417 local path="$(vpn_security_policies_path ${name})"
418
419 if ! settings_read "${path}" ${args}; then
420 log ERROR "Could not read settings for vpn security policy ${name}"
421 return ${EXIT_ERROR}
422 fi
423 }
424
425 # Returns the path to a the configuration fora given name
426 vpn_security_policies_path() {
427 assert [ $# -eq 1 ]
428
429 local name=${1}
430
431 if vpn_security_policies_check_readonly "${name}"; then
432 echo "${NETWORK_SHARE_DIR}/vpn/security-policies/${name}"
433 else
434 echo "${NETWORK_CONFIG_DIR}/vpn/security-policies/${name}"
435 fi
436 }
437
438 # Print the content of a vpn security policy configuration file in a nice way
439 vpn_security_policies_show() {
440 assert [ $# -eq 1 ]
441
442 local name=${1}
443
444 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
445 if ! vpn_security_policies_read_config ${name}; then
446 return ${EXIT_ERROR}
447 fi
448
449 cli_print_fmt1 0 "Security Policy: ${name}"
450 cli_space
451
452 # This could be done in a loop but a loop is much more complicated
453 # because we print 'Group Types' but the variable is named 'GROUP_TYPES'
454 cli_print_fmt1 1 "Ciphers:"
455 local cipher
456 for cipher in ${CIPHER}; do
457 cli_print_fmt1 2 "${VPN_SUPPORTED_CIPHERS[${cipher}]-${cipher}}"
458 done
459 cli_space
460
461 cli_print_fmt1 1 "Integrity:"
462 local integrity
463 for integrity in ${INTEGRITY}; do
464 cli_print_fmt1 2 "${VPN_SUPPORTED_INTEGRITY[${integrity}]-${integrity}}"
465 done
466 cli_space
467
468 cli_print_fmt1 1 "Group Types:"
469 local group_type
470 for group_type in ${GROUP_TYPE}; do
471 cli_print_fmt1 2 "${VPN_SUPPORTED_GROUP_TYPES[${group_type}]-${group_type}}"
472 done
473 cli_space
474
475 cli_print_fmt1 1 "Key Exchange:" "${KEY_EXCHANGE}"
476
477 # Key Lifetime
478 if isinteger LIFETIME && [ ${LIFETIME} -gt 0 ]; then
479 cli_print_fmt1 1 "Key Lifetime:" "$(format_time ${LIFETIME})"
480 else
481 log ERROR "The value for Key Lifetime is not a valid integer greater zero."
482 fi
483
484 # PFS
485 if enabled PFS; then
486 cli_print_fmt1 1 "Perfect Forward Secrecy:" "enabled"
487 else
488 cli_print_fmt1 1 "Perfect Forward Secrecy:" "disabled"
489 fi
490 cli_space
491
492 # Compression
493 if enabled COMPRESSION; then
494 cli_print_fmt1 1 "Compression:" "enabled"
495 else
496 cli_print_fmt1 1 "Compression:" "disabled"
497 fi
498 cli_space
499 }
500
501 # This function checks if a vpn security policy exists
502 # Returns True when yes and false when not
503 vpn_security_policy_exists() {
504 assert [ $# -eq 1 ]
505
506 local name=${1}
507
508 local path=$(vpn_security_policies_path "${name}")
509
510 [ -f ${path} ] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
511 }
512
513
514 # This function parses the parameters for the 'cipher' command
515 vpn_security_policies_cipher(){
516 local name=${1}
517 shift
518
519 if [ $# -eq 0 ]; then
520 log ERROR "You must pass at least one value after cipher"
521 return ${EXIT_ERROR}
522 fi
523
524 local CIPHER
525 if ! vpn_security_policies_read_config ${name} "CIPHER"; then
526 return ${EXIT_ERROR}
527 fi
528
529 # Remove duplicated entries to proceed the list safely
530 CIPHER="$(list_unique ${CIPHER})"
531
532 local ciphers_added
533 local ciphers_removed
534 local ciphers_set
535
536 while [ $# -gt 0 ]; do
537 local arg="${1}"
538
539 case "${arg}" in
540 +*)
541 list_append ciphers_added "${arg:1}"
542 ;;
543 -*)
544 list_append ciphers_removed "${arg:1}"
545 ;;
546 [A-Z0-9]*)
547 list_append ciphers_set "${arg}"
548 ;;
549 *)
550 error "Invalid argument: ${arg}"
551 return ${EXIT_ERROR}
552 ;;
553 esac
554 shift
555 done
556
557 # Check if the user is trying a mixed operation
558 if ! list_is_empty ciphers_set && (! list_is_empty ciphers_added || ! list_is_empty ciphers_removed); then
559 error "You cannot reset the cipher list and add or remove ciphers at the same time"
560 return ${EXIT_ERROR}
561 fi
562
563 # Set new cipher list
564 if ! list_is_empty ciphers_set; then
565 # Check if all ciphers are valid
566 local cipher
567 for cipher in ${ciphers_set}; do
568 if ! vpn_security_policies_cipher_supported ${cipher}; then
569 error "Unsupported cipher: ${cipher}"
570 return ${EXIT_ERROR}
571 fi
572 done
573
574 CIPHER="${ciphers_set}"
575
576 # Perform incremental updates
577 else
578 local cipher
579
580 # Perform all removals
581 for cipher in ${ciphers_removed}; do
582 if ! list_remove CIPHER ${cipher}; then
583 warning "${cipher} was not on the list and could not be removed"
584 fi
585 done
586
587 for cipher in ${ciphers_added}; do
588 if vpn_security_policies_cipher_supported ${cipher}; then
589 if ! list_append_unique CIPHER ${cipher}; then
590 warning "${cipher} is already on the cipher list"
591 fi
592 else
593 warning "${cipher} is unknown or unsupported and could not be added"
594 fi
595 done
596 fi
597
598 # Check if the list contain at least one valid cipher
599 if list_is_empty CIPHER; then
600 error "Cannot save an empty cipher list"
601 return ${EXIT_ERROR}
602 fi
603
604 # Save everything
605 if ! vpn_security_policies_write_config_key ${name} "CIPHER" ${CIPHER}; then
606 log ERROR "The changes for the vpn security policy ${name} could not be written."
607 fi
608
609 cli_headline 1 "Current cipher list for ${name}:"
610 for cipher in ${CIPHER}; do
611 cli_print_fmt1 1 "${cipher}" "${VPN_SUPPORTED_CIPHERS[${cipher}]}"
612 done
613 }
614
615 # This function parses the parameters for the 'compression' command
616 vpn_security_policies_compression(){
617 local name=${1}
618 local value=${2}
619
620 # Check if we get only one argument after compression <name>
621 if [ ! $# -eq 2 ]; then
622 log ERROR "The number of arguments do not match. Only one argument after compression is allowed."
623 return ${EXIT_ERROR}
624 fi
625
626 if ! isbool value; then
627 # We suggest only two values to avoid overburding the user.
628 log ERROR "Invalid Argument ${value}"
629 return ${EXIT_ERROR}
630 fi
631
632 vpn_security_policies_write_config_key "${name}" "COMPRESSION" "${value}"
633 }
634
635 # This function parses the parameters for the 'group-type' command
636 vpn_security_policies_group_type(){
637 local name=${1}
638 shift
639
640 if [ $# -eq 0 ]; then
641 log ERROR "You must pass at least one value after group-type"
642 return ${EXIT_ERROR}
643 fi
644
645 local GROUP_TYPE
646 if ! vpn_security_policies_read_config ${name} "GROUP_TYPE"; then
647 return ${EXIT_ERROR}
648 fi
649
650 # Remove duplicated entries to proceed the list safely
651 GROUP_TYPE="$(list_unique ${GROUP_TYPE})"
652
653 while [ $# -gt 0 ]; do
654 case "${1}" in
655 -*)
656 value=${1#-}
657 # Check if the group type is in the list of group types and
658 # check if the list has after removing this group type at leatst one valid value
659 if list_match ${value} ${GROUP_TYPE}; then
660 list_remove GROUP_TYPE ${value}
661 else
662 # We do not break here because this error does not break the processing of the next maybe valid values.
663 log ERROR "Can not remove ${value} from the list of group types because ${value} is not in the list."
664 fi
665 ;;
666 +*)
667 value=${1#+}
668 # Check if the group type is in the list of supported group types.
669 if ! isoneof value ${!VPN_SUPPORTED_GROUP_TYPES[@]}; then
670 # We do not break here because the processing of other maybe valid values are indepent from this error.
671 log ERROR "${value} is not a supported group type and can thats why not added to the list of group types."
672 else
673 if list_match ${value} ${GROUP_TYPE}; then
674 log WARNING "${value} is already in the list of group-types of this policy."
675 else
676 list_append GROUP_TYPE ${value}
677 fi
678 fi
679 ;;
680 esac
681 shift
682 done
683
684 # Check if the list contain at least one valid group-type
685 if [ $(list_length ${GROUP_TYPE}) -ge 1 ]; then
686 if ! vpn_security_policies_write_config_key ${name} "GROUP_TYPE" ${GROUP_TYPE}; then
687 log ERROR "The changes for the vpn security policy ${name} could not be written."
688 fi
689 else
690 log ERROR "After proceding all group types the list is empty and thats why no changes are written."
691 return ${EXIT_ERROR}
692 fi
693 }
694
695 # This function parses the parameters for the 'integrity' command
696 vpn_security_policies_integrity(){
697 local name=${1}
698 shift
699
700 if [ $# -eq 0 ]; then
701 log ERROR "You must pass at least one value after integrity."
702 return ${EXIT_ERROR}
703 fi
704
705 local INTEGRITY
706 if ! vpn_security_policies_read_config ${name} "INTEGRITY"; then
707 return ${EXIT_ERROR}
708 fi
709
710 # Remove duplicated entries to proceed the list safely
711 INTEGRITY="$(list_unique ${INTEGRITY})"
712
713 while [ $# -gt 0 ]; do
714 case "${1}" in
715 -*)
716 value=${1#-}
717 # Check if the integrity hash is in the list of integrity hashes and
718 # check if the list has after removing this integrity hash at least one valid value
719 if list_match ${value} ${INTEGRITY}; then
720 list_remove INTEGRITY ${value}
721 else
722 # We do not break here because the processing of other maybe valid values are indepent from this error.
723 log ERROR "Can not remove ${value} from the list of integrity hashes because ${value} is not in the list."
724 fi
725 ;;
726 +*)
727 value=${1#+}
728 # Check if the Ciphers is in the list of supported integrity hashes.
729 if ! isoneof value ${!VPN_SUPPORTED_INTEGRITY[@]}; then
730 # We do not break here because the processing of other maybe valid values are indepent from this error.
731 log ERROR "${value} is not a supported integrity hash and can thats why not added to the list of integrity hashes."
732 else
733 if list_match ${value} ${INTEGRITY}; then
734 log WARNING "${value} is already in the list of integrety hashes of this policy."
735 else
736 list_append INTEGRITY ${value}
737 fi
738 fi
739 ;;
740 esac
741 shift
742 done
743
744 # Check if the list contain at least one valid group-type
745 if [ $(list_length ${INTEGRITY}) -ge 1 ]; then
746 if ! vpn_security_policies_write_config_key ${name} "INTEGRITY" ${INTEGRITY}; then
747 log ERROR "The changes for the vpn security policy ${name} could not be written."
748 fi
749 else
750 log ERROR "After proceding all integrity hashes the list is empty and thats why no changes are written."
751 return ${EXIT_ERROR}
752 fi
753 }
754
755 # This function parses the parameters for the 'key-exchange' command
756 vpn_security_policies_key_exchange() {
757 local name=${1}
758 local value=${2}
759
760 # Check if we get only one argument after key-exchange <name>
761 if [ ! $# -eq 2 ]; then
762 log ERROR "The number of arguments do not match. Only argument after key-exchange is allowed."
763 return ${EXIT_ERROR}
764 fi
765
766 if ! isoneof value "ikev1" "ikev2" "IKEV1" "IKEV2"; then
767 log ERROR "Invalid Argument ${value}"
768 return ${EXIT_ERROR}
769 fi
770
771 vpn_security_policies_write_config_key "${name}" "KEY_EXCHANGE" "${value,,}"
772 }
773
774 # This function parses the parameters for the 'lifetime' command.
775 vpn_security_policies_lifetime(){
776 local name=${1}
777 shift
778
779 local value=$@
780
781 # Check if we get only one argument after lifetime <name>
782 if [ ! $# -ge 1 ]; then
783 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"
784 return ${EXIT_ERROR}
785 fi
786
787 if ! isinteger value; then
788 value=$(parse_time $@)
789 if [ ! $? -eq 0 ]; then
790 log ERROR "Parsing the passed time was not sucessful please check the passed values."
791 return ${EXIT_ERROR}
792 fi
793 fi
794
795 if [ ${value} -le 0 ]; then
796 log ERROR "The passed time value must be in the sum greater zero seconds."
797 return ${EXIT_ERROR}
798 fi
799
800 vpn_security_policies_write_config_key "${name}" "LIFETIME" "${value}"
801 }
802
803 # This function parses the parameters for the 'pfs' command
804 vpn_security_policies_pfs(){
805 local name=${1}
806 local value=${2}
807
808 # Check if we get only one argument after pfs <name>
809 if [ ! $# -eq 2 ]; then
810 log ERROR "The number of arguments do not match. Only argument after pfs is allowed."
811 return ${EXIT_ERROR}
812 fi
813
814 if [ ! $# -eq 2 ] || ! isbool value; then
815 # We suggest only two values to avoid overburding the user.
816 log ERROR "Invalid Argument ${value}"
817 return ${EXIT_ERROR}
818 fi
819
820 vpn_security_policies_write_config_key "${name}" "PFS" "${value}"
821 }
822
823 # This function checks if a vpn security policy name is valid
824 # Allowed are only A-Za-z0-9
825 vpn_security_policies_check_name() {
826 assert [ $# -eq 1 ]
827
828 local name=${1}
829
830 [[ ${name} =~ [^[:alnum:]$] ]]
831 }
832
833 # Function that creates based on the paramters one ore more new vpn security policies
834 vpn_security_policies_new() {
835 if [ $# -gt 1 ]; then
836 error "Too many arguments"
837 return ${EXIT_ERROR}
838 fi
839
840 local name="${1}"
841 if ! isset name; then
842 error "Please provide a name"
843 return ${EXIT_ERROR}
844 fi
845
846 # Check for duplicates
847 if vpn_security_policy_exists "${name}"; then
848 error "The VPN security policy with name ${name} already exists"
849 return ${EXIT_ERROR}
850 fi
851
852 # Check if name is valid
853 if vpn_security_policies_check_name "${name}"; then
854 error "'${name}' contains illegal characters"
855 return ${EXIT_ERROR}
856 fi
857
858 # Check if we have a read-only policy with the same name
859 if vpn_security_policies_check_readonly "${name}"; then
860 error "The VPN security policy ${name} is read-only"
861 return ${EXIT_ERROR}
862 fi
863
864 # Check if our source policy exists
865 if ! vpn_security_policy_exists "${VPN_DEFAULT_SECURITY_POLICY}"; then
866 error "Default VPN Security Policy '${VPN_DEFAULT_SECURITY_POLICY}' does not exist"
867 return ${EXIT_ERROR}
868 fi
869
870 log DEBUG "Creating VPN Security Policy ${name}"
871
872 if copy "$(vpn_security_policies_path "${VPN_DEFAULT_SECURITY_POLICY}")" \
873 "$(vpn_security_policies_path ${name})"; then
874 log INFO "VPN Security Policy ${name} successfully created"
875 else
876 log ERROR "Could not create VPN Security Policy ${name}"
877 return ${EXIT_ERROR}
878 fi
879
880 # Show the newly created policy
881 vpn_security_policies_show "${name}"
882 }
883
884 # Function that deletes based on the passed parameters one ore more vpn security policies
885 vpn_security_policies_destroy() {
886 local name
887 for name in $@; do
888 if ! vpn_security_policy_exists ${name}; then
889 log ERROR "The vpn security policy ${name} does not exist."
890 continue
891 fi
892
893 if vpn_security_policies_check_readonly ${name}; then
894 log ERROR "The vpn security policy ${name} cannot be deleted."
895 continue
896 fi
897
898 log DEBUG "Deleting vpn security policy ${name}"
899 settings_remove $(vpn_security_policies_path ${name})
900
901 # Delete cache
902 rm -rf "${NETWORK_CACHE_DIR}/vpn/security-policies/${name}"
903 done
904 }
905
906 vpn_security_policies_cipher_supported() {
907 local cipher=${1}
908
909 list_match ${cipher} ${!VPN_SUPPORTED_CIPHERS[@]}
910 }
911
912 vpn_security_policies_cipher_is_aead() {
913 local cipher=${1}
914
915 # All CCM and GCM ciphers are AEAD
916 string_match "[CG]CM" "${cipher}"
917 }
918
919 vpn_security_policies_make_ike_proposal() {
920 local name=${1}
921
922 if ! vpn_security_policy_exists ${name}; then
923 return ${EXIT_ERROR}
924 fi
925
926 local config_path="$(vpn_security_policies_path ${name})"
927 local cache_path="${NETWORK_CACHE_DIR}/vpn/security-policies/${name}/ike-proposal"
928
929 # Get data from cache if possible
930 if file_exists "${cache_path}" && ! file_is_newer_than "${config_path}" "${cache_path}"; then
931 fread "${cache_path}"
932 return ${EXIT_OK}
933 fi
934
935 # No or invalid cache data found
936 local proposal=$(_vpn_security_policies_make_ike_proposal "${name}")
937
938 # Write proposal to cache
939 if ! make_parent_dir "${cache_path}" || ! fwrite "${cache_path}" "${proposal}"; then
940 log WARNING "Could not write to cache: ${cache_path}"
941 fi
942
943 print "${proposal}"
944 }
945
946 _vpn_security_policies_make_ike_proposal() {
947 local name=${1}
948
949 # Read the config settings
950 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
951 if ! vpn_security_policies_read_config "${name}"; then
952 return ${EXIT_ERROR}
953 fi
954
955 local proposals
956
957 local cipher
958 for cipher in ${CIPHER}; do
959 # Translate cipher
960 local _cipher=${CIPHER_TO_STRONGSWAN[${cipher}]}
961
962 if ! isset _cipher; then
963 log WARN "Unsupported cipher: ${cipher}"
964 continue
965 fi
966
967 local integrity
968 for integrity in ${INTEGRITY}; do
969 local _integrity=${INTEGRITY_TO_STRONGSWAN[${integrity}]}
970
971 if ! isset _integrity; then
972 log WARN "Unsupported integrity: ${integrity}"
973 continue
974 fi
975
976 local group_type
977 for group_type in ${GROUP_TYPE}; do
978 local _group_type=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
979
980 if ! isset _group_type; then
981 log WARN "Unsupported group-type: ${group_type}"
982 continue
983 fi
984
985 # Put everything together
986 list_append proposals "${_cipher}-${_integrity}-${_group_type}"
987 done
988 done
989 done
990
991 # Returns as a comma-separated list
992 list_join proposals ,
993 }
994
995 vpn_security_policies_make_esp_proposal() {
996 local name=${1}
997
998 if ! vpn_security_policy_exists ${name}; then
999 return ${EXIT_ERROR}
1000 fi
1001
1002 local config_path="$(vpn_security_policies_path ${name})"
1003 local cache_path="${NETWORK_CACHE_DIR}/vpn/security-policies/${name}/esp-proposal"
1004
1005 # Get data from cache if possible
1006 if file_exists "${cache_path}" && ! file_is_newer_than "${config_path}" "${cache_path}"; then
1007 fread "${cache_path}"
1008 return ${EXIT_OK}
1009 fi
1010
1011 # No or invalid cache data found
1012 local proposal=$(_vpn_security_policies_make_esp_proposal "${name}")
1013
1014 # Write proposal to cache
1015 if ! make_parent_dir "${cache_path}" || ! fwrite "${cache_path}" "${proposal}"; then
1016 log WARNING "Could not write to cache: ${cache_path}"
1017 fi
1018
1019 print "${proposal}"
1020 }
1021
1022 _vpn_security_policies_make_esp_proposal() {
1023 local name=${1}
1024
1025 # Read the config settings
1026 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
1027 if ! vpn_security_policies_read_config "${name}"; then
1028 return ${EXIT_ERROR}
1029 fi
1030
1031 local proposals
1032
1033 local cipher
1034 for cipher in ${CIPHER}; do
1035 # Translate cipher
1036 local _cipher=${CIPHER_TO_STRONGSWAN[${cipher}]}
1037
1038 if ! isset _cipher; then
1039 log WARN "Unsupported cipher: ${cipher}"
1040 continue
1041 fi
1042
1043 if vpn_security_policies_cipher_is_aead ${cipher}; then
1044 local group_type
1045 for group_type in ${GROUP_TYPE}; do
1046 local _group_type=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1047
1048 if ! isset _group_type; then
1049 log WARN "Unsupported group-type: ${group_type}"
1050 continue
1051 fi
1052
1053 # Put everything together
1054 list_append proposals "${_cipher}-${_group_type}"
1055 done
1056 else
1057 local integrity
1058 for integrity in ${INTEGRITY}; do
1059 local _integrity=${INTEGRITY_TO_STRONGSWAN[${integrity}]}
1060
1061 if ! isset _integrity; then
1062 log WARN "Unsupported integrity: ${integrity}"
1063 continue
1064 fi
1065
1066 local group_type
1067 for group_type in ${GROUP_TYPE}; do
1068 local _group_type=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1069
1070 if ! isset _group_type; then
1071 log WARN "Unsupported group-type: ${group_type}"
1072 continue
1073 fi
1074
1075 # Put everything together
1076 list_append proposals "${_cipher}-${_integrity}-${_group_type}"
1077 done
1078 done
1079 fi
1080 done
1081
1082 # Returns as a comma-separated list
1083 list_join proposals ,
1084 }
1085
1086 # List all security policies
1087 vpn_security_policies_list_all() {
1088 local security_policy
1089 for security_policy in ${NETWORK_SHARE_DIR}/vpn/security-policies/*; do
1090 [ -f ${security_policy} ] || continue
1091 basename ${security_policy}
1092 done
1093
1094 for security_policy in ${NETWORK_CONFIG_DIR}/vpn/security-policies/*; do
1095 [ -f ${security_policy} ] || continue
1096 basename ${security_policy}
1097 done
1098 }