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_SUPPORTED_CIPHERS
="AES192 AES256 AES512"
26 VPN_SUPPORTED_INTEGRITY
="SHA512 SHA256 SHA128"
27 VPN_SUPPORTED_GROUP_TYPES
="MODP8192 MODP4096"
29 # This functions checks if a policy is readonly
30 # returns true when yes and false when no
31 vpn_security_policies_check_readonly
() {
32 if isoneof name
${VPN_SECURITY_POLICIES_READONLY}; then
39 # This function writes all values to a via ${name} specificated vpn security policy configuration file
40 vpn_security_policies_write_config
() {
45 if ! vpn_security_policy_exists
"${name}"; then
46 log ERROR
"No such vpn security policy: ${name}"
50 if vpn_security_policies_check_readonly
"${name}"; then
51 log ERROR
"The ${name} vpn security policy cannot be changed."
55 local path
="$(vpn_security_policies_path "${name}")"
56 if [ ! -w ${path} ]; then
57 log ERROR
"${path} is not writeable"
61 if ! settings_write
"${path}" ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}; then
62 log ERROR
"Could not write configuration settings for vpn security policy ${name}"
66 # TODO everytime we successfully write a config we should call some trigger to take the changes into effect
69 # This funtion writes the value for one key to a via ${name} specificated vpn security policy configuration file
70 vpn_security_policies_write_config_key
() {
79 if ! vpn_security_policy_exists
"${name}"; then
80 log ERROR
"No such vpn security policy: ${name}"
84 log DEBUG
"Set '${key}' to new value '${value}' in vpn security policy ${name}"
86 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
88 # Read the config settings
89 if ! vpn_security_policies_read_config
"${name}"; then
93 # Set the key to a new value
94 assign
"${key}" "${value}"
96 if ! vpn_security_policies_write_config
"${name}"; then
103 # Reads one or more keys out of a settings file or all if no key is provided.
104 vpn_security_policies_read_config
() {
110 if ! vpn_security_policy_exists
"${name}"; then
111 log ERROR
"No such vpn security policy: ${name}"
117 if [ $# -eq 0 ] && [ -n "${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}" ]; then
118 list_append args
${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
123 local path
="$(vpn_security_policies_path ${name})"
125 if ! settings_read
"${path}" ${args}; then
126 log ERROR
"Could not read settings for vpn security policy ${name}"
131 # Returns the path to a the configuration fora given name
132 vpn_security_policies_path
() {
137 if vpn_security_policies_check_readonly
"${name}"; then
138 echo "${NETWORK_SHARE_DIR}/vpn/security-policies/${name}"
140 echo "${NETWORK_CONFIG_DIR}/vpn/security-policies/${name}"
144 # Print the content of a vpn security policy configuration file in a nice way
145 vpn_security_policies_show
() {
150 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
151 if ! vpn_security_policies_read_config
${name}; then
155 cli_print_fmt1
0 "Security Policy: ${name}"
158 # This could be done in a loop but a loop is much more complicated
159 # because we print 'Group Types' but the variable is named 'GROUP_TYPES'
160 cli_print_fmt1
1 "Ciphers:"
161 cli_print_fmt1
2 "${CIPHER}"
164 cli_print_fmt1
1 "Integrity:"
165 cli_print_fmt1
2 "${INTEGRITY}"
168 cli_print_fmt1
1 "Group Types:"
169 cli_print_fmt1
2 "${GROUP_TYPE}"
172 cli_print_fmt1
1 "Key Exchange:" "${KEY_EXCHANGE}"
175 if isinteger LIFETIME
&& [ ${LIFETIME} -gt 0 ]; then
176 cli_print_fmt1
1 "Key Lifetime:" "$(format_time ${LIFETIME})"
178 log ERROR
"The value for Key Lifetime is not a valid integer greater zero."
183 cli_print_fmt1
1 "Perfect Forward Secrecy:" "enabled"
185 cli_print_fmt1
1 "Perfect Forward Secrecy:" "disabled"
190 if enabled COMPRESSION
; then
191 cli_print_fmt1
1 "Compression:" "enabled"
193 cli_print_fmt1
1 "Compression:" "disabled"
198 # This function checks if a vpn security policy exists
199 # Returns True when yes and false when not
200 vpn_security_policy_exists
() {
205 local path
=$
(vpn_security_policies_path
"${name}")
207 [ -f ${path} ] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
211 # This function parses the parameters for the 'cipher' command
212 vpn_security_policies_cipher
(){
216 if [ $# -eq 0 ]; then
217 log ERROR
"You must pass at least one value after cipher"
222 if ! vpn_security_policies_read_config
${name} "CIPHER"; then
226 # Remove duplicated entries to proceed the list safely
227 CIPHER
="$(list_unique ${CIPHER})"
229 while [ $# -gt 0 ]; do
233 # Check if the cipher is in the list of ciphers and
234 # check if the list has after removing this cipher at least one valid value
235 if list_match
${value} ${CIPHER}; then
236 list_remove CIPHER
${value}
238 # We do not break here because this error does not break the processing of the next maybe valid values.
239 log ERROR
"Can not remove ${value} from the list of Ciphers because ${value} is not in the list."
244 # Check if the Ciphers is in the list of supported ciphers.
245 if ! isoneof value
${VPN_SUPPORTED_CIPHERS}; then
246 # We do not break here because this error does not break the processing of the next maybe valid values.
247 log ERROR
"${value} is not a supported cipher and can thats why not added to the list of ciphers."
249 if list_match
${value} ${CIPHER}; then
250 log WARNING
"${value} is already in the list of ciphers of this policy."
252 list_append CIPHER
${value}
260 # Check if the list contain at least one valid cipher
261 if [ $
(list_length
${CIPHER}) -ge 1 ]; then
262 if ! vpn_security_policies_write_config_key
${name} "CIPHER" ${CIPHER}; then
263 log ERROR
"The changes for the vpn security policy ${name} could not be written."
266 log ERROR
"After proceding all ciphers the list is empty and thats why no changes are written."
271 # This function parses the parameters for the 'compression' command
272 vpn_security_policies_compression
(){
276 # Check if we get only one argument after compression <name>
277 if [ ! $# -eq 2 ]; then
278 log ERROR
"The number of arguments do not match. Only one argument after compression is allowed."
282 if ! isbool value
; then
283 # We suggest only two values to avoid overburding the user.
284 log ERROR
"Invalid Argument ${value}"
288 vpn_security_policies_write_config_key
"${name}" "COMPRESSION" "${value}"
291 # This function parses the parameters for the 'group-type' command
292 vpn_security_policies_group_type
(){
296 if [ $# -eq 0 ]; then
297 log ERROR
"You must pass at least one value after group-type"
302 if ! vpn_security_policies_read_config
${name} "GROUP_TYPE"; then
306 # Remove duplicated entries to proceed the list safely
307 GROUP_TYPE
="$(list_unique ${GROUP_TYPE})"
309 while [ $# -gt 0 ]; do
313 # Check if the group type is in the list of group types and
314 # check if the list has after removing this group type at leatst one valid value
315 if list_match
${value} ${GROUP_TYPE}; then
316 list_remove GROUP_TYPE
${value}
318 # We do not break here because this error does not break the processing of the next maybe valid values.
319 log ERROR
"Can not remove ${value} from the list of group types because ${value} is not in the list."
324 # Check if the group type is in the list of supported group types.
325 if ! isoneof value
${VPN_SUPPORTED_GROUP_TYPES}; then
326 # We do not break here because the processing of other maybe valid values are indepent from this error.
327 log ERROR
"${value} is not a supported group type and can thats why not added to the list of group types."
329 if list_match
${value} ${GROUP_TYPE}; then
330 log WARNING
"${value} is already in the list of group-types of this policy."
332 list_append GROUP_TYPE
${value}
340 # Check if the list contain at least one valid group-type
341 if [ $
(list_length
${GROUP_TYPE}) -ge 1 ]; then
342 if ! vpn_security_policies_write_config_key
${name} "GROUP_TYPE" ${GROUP_TYPE}; then
343 log ERROR
"The changes for the vpn security policy ${name} could not be written."
346 log ERROR
"After proceding all group types the list is empty and thats why no changes are written."
351 # This function parses the parameters for the 'integrity' command
352 vpn_security_policies_integrity
(){
356 if [ $# -eq 0 ]; then
357 log ERROR
"You must pass at least one value after integrity."
362 if ! vpn_security_policies_read_config
${name} "INTEGRITY"; then
366 # Remove duplicated entries to proceed the list safely
367 INTEGRITY
="$(list_unique ${INTEGRITY})"
369 while [ $# -gt 0 ]; do
373 # Check if the integrity hash is in the list of integrity hashes and
374 # check if the list has after removing this integrity hash at least one valid value
375 if list_match
${value} ${INTEGRITY}; then
376 list_remove INTEGRITY
${value}
378 # We do not break here because the processing of other maybe valid values are indepent from this error.
379 log ERROR
"Can not remove ${value} from the list of integrity hashes because ${value} is not in the list."
384 # Check if the Ciphers is in the list of supported integrity hashes.
385 if ! isoneof value
${VPN_SUPPORTED_INTEGRITY}; then
386 # We do not break here because the processing of other maybe valid values are indepent from this error.
387 log ERROR
"${value} is not a supported integrity hash and can thats why not added to the list of integrity hashes."
389 if list_match
${value} ${INTEGRITY}; then
390 log WARNING
"${value} is already in the list of integrety hashes of this policy."
392 list_append INTEGRITY
${value}
400 # Check if the list contain at least one valid group-type
401 if [ $
(list_length
${INTEGRITY}) -ge 1 ]; then
402 if ! vpn_security_policies_write_config_key
${name} "INTEGRITY" ${INTEGRITY}; then
403 log ERROR
"The changes for the vpn security policy ${name} could not be written."
406 log ERROR
"After proceding all integrity hashes the list is empty and thats why no changes are written."
411 # This function parses the parameters for the 'key-exchange' command
412 vpn_security_policies_key_exchange
() {
416 # Check if we get only one argument after key-exchange <name>
417 if [ ! $# -eq 2 ]; then
418 log ERROR
"The number of arguments do not match. Only argument after key-exchange is allowed."
422 if ! isoneof value
"ikev1" "ikev2" "IKEV1" "IKEV2"; then
423 log ERROR
"Invalid Argument ${value}"
427 vpn_security_policies_write_config_key
"${name}" "KEY_EXCHANGE" "${value,,}"
430 # This function parses the parameters for the 'lifetime' command.
431 vpn_security_policies_lifetime
(){
437 # Check if we get only one argument after lifetime <name>
438 if [ ! $# -ge 1 ]; then
439 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"
443 if ! isinteger value
; then
444 value
=$
(parse_time $@
)
445 if [ ! $?
-eq 0 ]; then
446 log ERROR
"Parsing the passed time was not sucessful please check the passed values."
451 if [ ${value} -le 0 ]; then
452 log ERROR
"The passed time value must be in the sum greater zero seconds."
456 vpn_security_policies_write_config_key
"${name}" "LIFETIME" "${value}"
459 # This function parses the parameters for the 'pfs' command
460 vpn_security_policies_pfs
(){
464 # Check if we get only one argument after pfs <name>
465 if [ ! $# -eq 2 ]; then
466 log ERROR
"The number of arguments do not match. Only argument after pfs is allowed."
470 if [ ! $# -eq 2 ] ||
! isbool value
; then
471 # We suggest only two values to avoid overburding the user.
472 log ERROR
"Invalid Argument ${value}"
476 vpn_security_policies_write_config_key
"${name}" "PFS" "${value}"
479 # This function checks if a vpn security policy name is valid
480 # Allowed are only A-Za-z0-9
481 vpn_security_policies_check_name
() {
486 [[ ${name} =~
[^
[:alnum
:]$
] ]]
489 # Function that creates based on the paramters one ore more new vpn security policies
490 vpn_security_policies_new
() {
491 if [ $# -gt 1 ]; then
492 error
"Too many arguments"
497 if ! isset name
; then
498 error
"Please provide a name"
502 # Check for duplicates
503 if vpn_security_policy_exists
"${name}"; then
504 error
"The VPN security policy with name ${name} already exists"
508 # Check if name is valid
509 if vpn_security_policies_check_name
"${name}"; then
510 error
"'${name}' contains illegal characters"
514 # Check if we have a read-only policy with the same name
515 if vpn_security_policies_check_readonly
"${name}"; then
516 error
"The VPN security policy ${name} is read-only"
520 log DEBUG
"Creating VPN Security Policy ${name}"
522 if copy
"$(vpn_security_policies_path "system
")" "$(vpn_security_policies_path ${name})"; then
523 log INFO
"VPN Security Policy ${name} successfully created"
525 log ERROR
"Could not create VPN Security Policy ${name}"
530 # Function that deletes based on the passed parameters one ore more vpn security policies
531 vpn_security_policies_destroy
() {
534 if ! vpn_security_policy_exists
${name}; then
535 log ERROR
"The vpn security policy ${name} does not exist."
539 if vpn_security_policies_check_readonly
${name}; then
540 log ERROR
"The vpn security policy ${name} cannot be deleted."
544 log DEBUG
"Deleting vpn security policy ${name}"
545 settings_remove $
(vpn_security_policies_path
${name})