2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2017 IPFire Network Development Team #
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. #
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. #
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/>. #
20 ###############################################################################
22 VPN_SECURITY_POLICIES_CONFIG_SETTINGS
="CIPHER COMPRESSION GROUP_TYPE INTEGRITY KEY_EXCHANGE LIFETIME PFS"
23 VPN_SECURITY_POLICIES_READONLY
="system"
25 VPN_DEFAULT_SECURITY_POLICY
="system"
27 VPN_SUPPORTED_CIPHERS
="AES192 AES256 AES512"
28 VPN_SUPPORTED_INTEGRITY
="SHA512 SHA256 SHA128"
29 VPN_SUPPORTED_GROUP_TYPES
="MODP8192 MODP4096"
31 # This functions checks if a policy is readonly
32 # returns true when yes and false when no
33 vpn_security_policies_check_readonly
() {
34 if isoneof name
${VPN_SECURITY_POLICIES_READONLY}; then
41 # This function writes all values to a via ${name} specificated vpn security policy configuration file
42 vpn_security_policies_write_config
() {
47 if ! vpn_security_policy_exists
"${name}"; then
48 log ERROR
"No such vpn security policy: ${name}"
52 if vpn_security_policies_check_readonly
"${name}"; then
53 log ERROR
"The ${name} vpn security policy cannot be changed."
57 local path
="$(vpn_security_policies_path "${name}")"
58 if [ ! -w ${path} ]; then
59 log ERROR
"${path} is not writeable"
63 if ! settings_write
"${path}" ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}; then
64 log ERROR
"Could not write configuration settings for vpn security policy ${name}"
68 # TODO everytime we successfully write a config we should call some trigger to take the changes into effect
71 # This funtion writes the value for one key to a via ${name} specificated vpn security policy configuration file
72 vpn_security_policies_write_config_key
() {
81 if ! vpn_security_policy_exists
"${name}"; then
82 log ERROR
"No such vpn security policy: ${name}"
86 log DEBUG
"Set '${key}' to new value '${value}' in vpn security policy ${name}"
88 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
90 # Read the config settings
91 if ! vpn_security_policies_read_config
"${name}"; then
95 # Set the key to a new value
96 assign
"${key}" "${value}"
98 if ! vpn_security_policies_write_config
"${name}"; then
105 # Reads one or more keys out of a settings file or all if no key is provided.
106 vpn_security_policies_read_config
() {
112 if ! vpn_security_policy_exists
"${name}"; then
113 log ERROR
"No such vpn security policy: ${name}"
119 if [ $# -eq 0 ] && [ -n "${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}" ]; then
120 list_append args
${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
125 local path
="$(vpn_security_policies_path ${name})"
127 if ! settings_read
"${path}" ${args}; then
128 log ERROR
"Could not read settings for vpn security policy ${name}"
133 # Returns the path to a the configuration fora given name
134 vpn_security_policies_path
() {
139 if vpn_security_policies_check_readonly
"${name}"; then
140 echo "${NETWORK_SHARE_DIR}/vpn/security-policies/${name}"
142 echo "${NETWORK_CONFIG_DIR}/vpn/security-policies/${name}"
146 # Print the content of a vpn security policy configuration file in a nice way
147 vpn_security_policies_show
() {
152 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
153 if ! vpn_security_policies_read_config
${name}; then
157 cli_print_fmt1
0 "Security Policy: ${name}"
160 # This could be done in a loop but a loop is much more complicated
161 # because we print 'Group Types' but the variable is named 'GROUP_TYPES'
162 cli_print_fmt1
1 "Ciphers:"
163 cli_print_fmt1
2 "${CIPHER}"
166 cli_print_fmt1
1 "Integrity:"
167 cli_print_fmt1
2 "${INTEGRITY}"
170 cli_print_fmt1
1 "Group Types:"
171 cli_print_fmt1
2 "${GROUP_TYPE}"
174 cli_print_fmt1
1 "Key Exchange:" "${KEY_EXCHANGE}"
177 if isinteger LIFETIME
&& [ ${LIFETIME} -gt 0 ]; then
178 cli_print_fmt1
1 "Key Lifetime:" "$(format_time ${LIFETIME})"
180 log ERROR
"The value for Key Lifetime is not a valid integer greater zero."
185 cli_print_fmt1
1 "Perfect Forward Secrecy:" "enabled"
187 cli_print_fmt1
1 "Perfect Forward Secrecy:" "disabled"
192 if enabled COMPRESSION
; then
193 cli_print_fmt1
1 "Compression:" "enabled"
195 cli_print_fmt1
1 "Compression:" "disabled"
200 # This function checks if a vpn security policy exists
201 # Returns True when yes and false when not
202 vpn_security_policy_exists
() {
207 local path
=$
(vpn_security_policies_path
"${name}")
209 [ -f ${path} ] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
213 # This function parses the parameters for the 'cipher' command
214 vpn_security_policies_cipher
(){
218 if [ $# -eq 0 ]; then
219 log ERROR
"You must pass at least one value after cipher"
224 if ! vpn_security_policies_read_config
${name} "CIPHER"; then
228 # Remove duplicated entries to proceed the list safely
229 CIPHER
="$(list_unique ${CIPHER})"
231 while [ $# -gt 0 ]; do
235 # Check if the cipher is in the list of ciphers and
236 # check if the list has after removing this cipher at least one valid value
237 if list_match
${value} ${CIPHER}; then
238 list_remove CIPHER
${value}
240 # We do not break here because this error does not break the processing of the next maybe valid values.
241 log ERROR
"Can not remove ${value} from the list of Ciphers because ${value} is not in the list."
246 # Check if the Ciphers is in the list of supported ciphers.
247 if ! isoneof value
${VPN_SUPPORTED_CIPHERS}; then
248 # We do not break here because this error does not break the processing of the next maybe valid values.
249 log ERROR
"${value} is not a supported cipher and can thats why not added to the list of ciphers."
251 if list_match
${value} ${CIPHER}; then
252 log WARNING
"${value} is already in the list of ciphers of this policy."
254 list_append CIPHER
${value}
262 # Check if the list contain at least one valid cipher
263 if [ $
(list_length
${CIPHER}) -ge 1 ]; then
264 if ! vpn_security_policies_write_config_key
${name} "CIPHER" ${CIPHER}; then
265 log ERROR
"The changes for the vpn security policy ${name} could not be written."
268 log ERROR
"After proceding all ciphers the list is empty and thats why no changes are written."
273 # This function parses the parameters for the 'compression' command
274 vpn_security_policies_compression
(){
278 # Check if we get only one argument after compression <name>
279 if [ ! $# -eq 2 ]; then
280 log ERROR
"The number of arguments do not match. Only one argument after compression is allowed."
284 if ! isbool value
; then
285 # We suggest only two values to avoid overburding the user.
286 log ERROR
"Invalid Argument ${value}"
290 vpn_security_policies_write_config_key
"${name}" "COMPRESSION" "${value}"
293 # This function parses the parameters for the 'group-type' command
294 vpn_security_policies_group_type
(){
298 if [ $# -eq 0 ]; then
299 log ERROR
"You must pass at least one value after group-type"
304 if ! vpn_security_policies_read_config
${name} "GROUP_TYPE"; then
308 # Remove duplicated entries to proceed the list safely
309 GROUP_TYPE
="$(list_unique ${GROUP_TYPE})"
311 while [ $# -gt 0 ]; do
315 # Check if the group type is in the list of group types and
316 # check if the list has after removing this group type at leatst one valid value
317 if list_match
${value} ${GROUP_TYPE}; then
318 list_remove GROUP_TYPE
${value}
320 # We do not break here because this error does not break the processing of the next maybe valid values.
321 log ERROR
"Can not remove ${value} from the list of group types because ${value} is not in the list."
326 # Check if the group type is in the list of supported group types.
327 if ! isoneof value
${VPN_SUPPORTED_GROUP_TYPES}; then
328 # We do not break here because the processing of other maybe valid values are indepent from this error.
329 log ERROR
"${value} is not a supported group type and can thats why not added to the list of group types."
331 if list_match
${value} ${GROUP_TYPE}; then
332 log WARNING
"${value} is already in the list of group-types of this policy."
334 list_append GROUP_TYPE
${value}
342 # Check if the list contain at least one valid group-type
343 if [ $
(list_length
${GROUP_TYPE}) -ge 1 ]; then
344 if ! vpn_security_policies_write_config_key
${name} "GROUP_TYPE" ${GROUP_TYPE}; then
345 log ERROR
"The changes for the vpn security policy ${name} could not be written."
348 log ERROR
"After proceding all group types the list is empty and thats why no changes are written."
353 # This function parses the parameters for the 'integrity' command
354 vpn_security_policies_integrity
(){
358 if [ $# -eq 0 ]; then
359 log ERROR
"You must pass at least one value after integrity."
364 if ! vpn_security_policies_read_config
${name} "INTEGRITY"; then
368 # Remove duplicated entries to proceed the list safely
369 INTEGRITY
="$(list_unique ${INTEGRITY})"
371 while [ $# -gt 0 ]; do
375 # Check if the integrity hash is in the list of integrity hashes and
376 # check if the list has after removing this integrity hash at least one valid value
377 if list_match
${value} ${INTEGRITY}; then
378 list_remove INTEGRITY
${value}
380 # We do not break here because the processing of other maybe valid values are indepent from this error.
381 log ERROR
"Can not remove ${value} from the list of integrity hashes because ${value} is not in the list."
386 # Check if the Ciphers is in the list of supported integrity hashes.
387 if ! isoneof value
${VPN_SUPPORTED_INTEGRITY}; then
388 # We do not break here because the processing of other maybe valid values are indepent from this error.
389 log ERROR
"${value} is not a supported integrity hash and can thats why not added to the list of integrity hashes."
391 if list_match
${value} ${INTEGRITY}; then
392 log WARNING
"${value} is already in the list of integrety hashes of this policy."
394 list_append INTEGRITY
${value}
402 # Check if the list contain at least one valid group-type
403 if [ $
(list_length
${INTEGRITY}) -ge 1 ]; then
404 if ! vpn_security_policies_write_config_key
${name} "INTEGRITY" ${INTEGRITY}; then
405 log ERROR
"The changes for the vpn security policy ${name} could not be written."
408 log ERROR
"After proceding all integrity hashes the list is empty and thats why no changes are written."
413 # This function parses the parameters for the 'key-exchange' command
414 vpn_security_policies_key_exchange
() {
418 # Check if we get only one argument after key-exchange <name>
419 if [ ! $# -eq 2 ]; then
420 log ERROR
"The number of arguments do not match. Only argument after key-exchange is allowed."
424 if ! isoneof value
"ikev1" "ikev2" "IKEV1" "IKEV2"; then
425 log ERROR
"Invalid Argument ${value}"
429 vpn_security_policies_write_config_key
"${name}" "KEY_EXCHANGE" "${value,,}"
432 # This function parses the parameters for the 'lifetime' command.
433 vpn_security_policies_lifetime
(){
439 # Check if we get only one argument after lifetime <name>
440 if [ ! $# -ge 1 ]; then
441 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"
445 if ! isinteger value
; then
446 value
=$
(parse_time $@
)
447 if [ ! $?
-eq 0 ]; then
448 log ERROR
"Parsing the passed time was not sucessful please check the passed values."
453 if [ ${value} -le 0 ]; then
454 log ERROR
"The passed time value must be in the sum greater zero seconds."
458 vpn_security_policies_write_config_key
"${name}" "LIFETIME" "${value}"
461 # This function parses the parameters for the 'pfs' command
462 vpn_security_policies_pfs
(){
466 # Check if we get only one argument after pfs <name>
467 if [ ! $# -eq 2 ]; then
468 log ERROR
"The number of arguments do not match. Only argument after pfs is allowed."
472 if [ ! $# -eq 2 ] ||
! isbool value
; then
473 # We suggest only two values to avoid overburding the user.
474 log ERROR
"Invalid Argument ${value}"
478 vpn_security_policies_write_config_key
"${name}" "PFS" "${value}"
481 # This function checks if a vpn security policy name is valid
482 # Allowed are only A-Za-z0-9
483 vpn_security_policies_check_name
() {
488 [[ ${name} =~
[^
[:alnum
:]$
] ]]
491 # Function that creates based on the paramters one ore more new vpn security policies
492 vpn_security_policies_new
() {
493 if [ $# -gt 1 ]; then
494 error
"Too many arguments"
499 if ! isset name
; then
500 error
"Please provide a name"
504 # Check for duplicates
505 if vpn_security_policy_exists
"${name}"; then
506 error
"The VPN security policy with name ${name} already exists"
510 # Check if name is valid
511 if vpn_security_policies_check_name
"${name}"; then
512 error
"'${name}' contains illegal characters"
516 # Check if we have a read-only policy with the same name
517 if vpn_security_policies_check_readonly
"${name}"; then
518 error
"The VPN security policy ${name} is read-only"
522 # Check if our source policy exists
523 if ! vpn_security_policy_exists
"${VPN_DEFAULT_SECURITY_POLICY}"; then
524 error
"Default VPN Security Policy '${VPN_DEFAULT_SECURITY_POLICY}' does not exist"
528 log DEBUG
"Creating VPN Security Policy ${name}"
530 if copy
"$(vpn_security_policies_path "${VPN_DEFAULT_SECURITY_POLICY}")" "$(vpn_security_policies_path ${name})"; then
531 log INFO
"VPN Security Policy ${name} successfully created"
533 log ERROR
"Could not create VPN Security Policy ${name}"
538 # Function that deletes based on the passed parameters one ore more vpn security policies
539 vpn_security_policies_destroy
() {
542 if ! vpn_security_policy_exists
${name}; then
543 log ERROR
"The vpn security policy ${name} does not exist."
547 if vpn_security_policies_check_readonly
${name}; then
548 log ERROR
"The vpn security policy ${name} cannot be deleted."
552 log DEBUG
"Deleting vpn security policy ${name}"
553 settings_remove $
(vpn_security_policies_path
${name})