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 vpn_security_policies_check_readonly
() {
30 # This functions checks if a policy is readonly
31 # returns true when yes and false when no
33 if isoneof name
${VPN_SECURITY_POLICIES_READONLY}; then
40 vpn_security_policies_write_config
() {
41 # This function writes all values to a via ${name} specificated vpn security policy configuration file
46 if ! vpn_security_policy_exists
${name}; then
47 log ERROR
"No such vpn security policy: ${name}"
51 if vpn_security_policies_check_readonly
${name}; then
52 log ERROR
"The ${name} vpn security policy cannot be changed."
56 local path
="$(vpn_security_policies_path ${name})"
57 if [ ! -w ${path} ]; then
58 log ERROR
"${path} is not writeable"
62 if ! settings_write
"${path}" ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}; then
63 log ERROR
"Could not write configuration settings for vpn security policy ${name}"
67 # TODO everytime we successfully write a config we should call some trigger to take the changes into effect
70 vpn_security_policies_write_config_key
() {
71 # This funtion writes the value for one key to a via ${name} specificated vpn security policy configuration file
78 if ! vpn_security_policy_exists
${name}; then
79 log ERROR
"No such vpn security policy: ${name}"
83 log DEBUG
"Set '${key}' to new value '${value}' in vpn security policy ${name}"
85 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
87 # Read the config settings
88 if ! vpn_security_policies_read_config
${name}; then
92 # Set the key to a new value
93 assign
"${key}" "${value}"
95 if ! vpn_security_policies_write_config
${name}; then
103 vpn_security_policies_read_config
() {
104 # Reads one or more keys out of a settings file or all if no key is provided.
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 vpn_security_policies_path
() {
132 # Returns the path to a the configuration fora given name
136 if vpn_security_policies_check_readonly
${name}; then
137 echo "${NETWORK_SHARE_DIR}/vpn/security-policies/${name}"
139 echo "${NETWORK_CONFIG_DIR}/vpn/security-policies/${name}"
143 vpn_security_policies_show
() {
144 # Print the content of a vpn security policy configuration file in a nice way
148 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
150 # Break if read fails
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}"
163 cli_print_fmt1
1 "Integrity:"
164 cli_print_fmt1
2 "${INTEGRITY}"
166 cli_print_fmt1
1 "Group Types:"
167 cli_print_fmt1
2 "${GROUP_TYPE}"
170 cli_print_fmt1
1 "Key Exchange:" "${KEY_EXCHANGE}"
171 # Check if lifetime is an integer
172 if isinteger LIFETIME
&& [ ${LIFETIME} -gt 0 ]; then
173 cli_print_fmt1
1 "Key Lifetime:" "$(format_time ${LIFETIME})"
175 log ERROR
"The value for Key Lifetime is not a valid integer greater zero."
178 cli_print_fmt1
1 "Perfect Forward Secrecy:" "enabled"
180 cli_print_fmt1
1 "Perfect Forward Secrecy:" "disabled"
183 if enabled COMPRESSION
; then
184 cli_print_fmt1
1 "Compression:" "enabled"
186 cli_print_fmt1
1 "Compression:" "disabled"
191 vpn_security_policy_exists
() {
192 # This function checks if a vpn security policy exists
193 # Returns True when yes and false when not
197 local path
=$
(vpn_security_policies_path
${name})
202 vpn_security_policies_cipher
(){
203 # This function parses the parameters for the 'cipher' command
207 if [ $# -eq 0 ]; then
208 log ERROR
"You must pass at least one value after cipher"
214 if ! vpn_security_policies_read_config
${name} "CIPHER"; then
218 # Remove duplicated entries to proceed the list safely
219 CIPHER
="$(list_unique ${CIPHER})"
221 while [ $# -gt 0 ]; do
225 # Check if the cipher is in the list of ciphers and
226 # check if the list has after removing this cipher at least one valid value
227 if list_match
${value} ${CIPHER}; then
228 list_remove CIPHER
${value}
230 # We do not break here because this error does not break the processing of the next maybe valid values.
231 log ERROR
"Can not remove ${value} from the list of Ciphers because ${value} is not in the list."
236 # Check if the Ciphers is in the list of supported ciphers.
237 if ! isoneof value
${VPN_SUPPORTED_CIPHERS}; then
238 # We do not break here because this error does not break the processing of the next maybe valid values.
239 log ERROR
"${value} is not a supported cipher and can thats why not added to the list of ciphers."
241 if list_match
${value} ${CIPHER}; then
242 log WARNING
"${value} is already in the list of ciphers of this policy."
244 list_append CIPHER
${value}
252 # Check if the list contain at least one valid cipher
253 if [ $
(list_length
${CIPHER}) -ge 1 ]; then
254 if ! vpn_security_policies_write_config_key
${name} "CIPHER" ${CIPHER}; then
255 log ERROR
"The changes for the vpn security policy ${name} could not be written."
258 log ERROR
"After proceding all ciphers the list is empty and thats why no changes are written."
263 vpn_security_policies_compression
(){
264 # This function parses the parameters for the 'compression' command
268 # Check if we get only one argument after compression <name>
269 if [ ! $# -eq 2 ]; then
270 log ERROR
"The number of arguments do not match. Only one argument after compression is allowed."
274 if ! isbool value
; then
275 # We suggest only two values to avoid overburding the user.
276 log ERROR
"Invalid Argument ${value}"
280 vpn_security_policies_write_config_key
"${name}" "COMPRESSION" "${value}"
283 vpn_security_policies_group_type
(){
284 # This function parses the parameters for the 'group-type' command.
288 if [ $# -eq 0 ]; then
289 log ERROR
"You must pass at least one value after group-type"
295 if ! vpn_security_policies_read_config
${name} "GROUP_TYPE"; then
299 # Remove duplicated entries to proceed the list safely
300 GROUP_TYPE
="$(list_unique ${GROUP_TYPE})"
302 while [ $# -gt 0 ]; do
306 # Check if the group type is in the list of group types and
307 # check if the list has after removing this group type at leatst one valid value
308 if list_match
${value} ${GROUP_TYPE}; then
309 list_remove GROUP_TYPE
${value}
311 # We do not break here because this error does not break the processing of the next maybe valid values.
312 log ERROR
"Can not remove ${value} from the list of group types because ${value} is not in the list."
317 # Check if the group type is in the list of supported group types.
318 if ! isoneof value
${VPN_SUPPORTED_GROUP_TYPES}; then
319 # We do not break here because the processing of other maybe valid values are indepent from this error.
320 log ERROR
"${value} is not a supported group type and can thats why not added to the list of group types."
322 if list_match
${value} ${GROUP_TYPE}; then
323 log WARNING
"${value} is already in the list of group-types of this policy."
325 list_append GROUP_TYPE
${value}
333 # Check if the list contain at least one valid group-type
334 if [ $
(list_length
${GROUP_TYPE}) -ge 1 ]; then
335 if ! vpn_security_policies_write_config_key
${name} "GROUP_TYPE" ${GROUP_TYPE}; then
336 log ERROR
"The changes for the vpn security policy ${name} could not be written."
339 log ERROR
"After proceding all group types the list is empty and thats why no changes are written."
343 vpn_security_policies_integrity
(){
344 # This function parses the parameters for the 'integrity' command
348 if [ $# -eq 0 ]; then
349 log ERROR
"You must pass at least one value after integrity."
355 if ! vpn_security_policies_read_config
${name} "INTEGRITY"; then
359 # Remove duplicated entries to proceed the list safely
360 INTEGRITY
="$(list_unique ${INTEGRITY})"
362 while [ $# -gt 0 ]; do
366 # Check if the integrity hash is in the list of integrity hashes and
367 # check if the list has after removing this integrity hash at least one valid value
368 if list_match
${value} ${INTEGRITY}; then
369 list_remove INTEGRITY
${value}
371 # We do not break here because the processing of other maybe valid values are indepent from this error.
372 log ERROR
"Can not remove ${value} from the list of integrity hashes because ${value} is not in the list."
377 # Check if the Ciphers is in the list of supported integrity hashes.
378 if ! isoneof value
${VPN_SUPPORTED_INTEGRITY}; then
379 # We do not break here because the processing of other maybe valid values are indepent from this error.
380 log ERROR
"${value} is not a supported integrity hash and can thats why not added to the list of integrity hashes."
382 if list_match
${value} ${INTEGRITY}; then
383 log WARNING
"${value} is already in the list of integrety hashes of this policy."
385 list_append INTEGRITY
${value}
393 # Check if the list contain at least one valid group-type
394 if [ $
(list_length
${INTEGRITY}) -ge 1 ]; then
395 if ! vpn_security_policies_write_config_key
${name} "INTEGRITY" ${INTEGRITY}; then
396 log ERROR
"The changes for the vpn security policy ${name} could not be written."
399 log ERROR
"After proceding all integrity hashes the list is empty and thats why no changes are written."
405 vpn_security_policies_key_exchange
() {
406 # This function parses the parameters for the 'key-exchange' command
409 # Check if we get only one argument after key-exchange <name>
410 if [ ! $# -eq 2 ]; then
411 log ERROR
"The number of arguments do not match. Only argument after key-exchange is allowed."
416 if ! isoneof value
"ikev1" "ikev2" "IKEV1" "IKEV2"; then
417 log ERROR
"Invalid Argument ${value}"
421 vpn_security_policies_write_config_key
"${name}" "KEY_EXCHANGE" "${value,,}"
424 vpn_security_policies_lifetime
(){
425 # This function parses the parameters for the 'lifetime' command.
430 # Check if we get only one argument after lifetime <name>
431 if [ ! $# -ge 1 ]; then
432 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"
436 if ! isinteger value
; then
437 value
=$
(parse_time $@
)
438 if [ ! $?
-eq 0 ]; then
439 log ERROR
"Parsing the passed time was not sucessful please check the passed values."
444 if [ ${value} -le 0 ]; then
445 log ERROR
"The passed time value must be in the sum greater zero seconds."
449 vpn_security_policies_write_config_key
"${name}" "LIFETIME" "${value}"
452 vpn_security_policies_pfs
(){
453 # This function parses the parameters for the 'pfs' command
457 # Check if we get only one argument after pfs <name>
458 if [ ! $# -eq 2 ]; then
459 log ERROR
"The number of arguments do not match. Only argument after pfs is allowed."
463 if [ ! $# -eq 2 ] ||
! isbool value
; then
464 # We suggest only two values to avoid overburding the user.
465 log ERROR
"Invalid Argument ${value}"
469 vpn_security_policies_write_config_key
"${name}" "PFS" "${value}"
472 vpn_security_policies_check_name
() {
473 # This function checks if a vpn security policy name is valid
474 # Allowed are only A-Za-z0-9
477 [[ ${name} =~
[^
[:alnum
:]$
] ]]
480 vpn_security_policies_new
() {
481 # Function that creates based on the paramters one ore more new vpn security policies
484 log ERROR
"No name provided."
489 if vpn_security_policy_exists
${name}; then
490 log ERROR
"The vpn security policy ${name} does already exist."
494 if vpn_security_policies_check_name
${name}; then
495 log ERROR
"'${name}' contains illegal characters. Allowed are only A-Za-z0-9"
499 if vpn_security_policies_check_readonly
${name}; then
500 log ERROR
"The vpn security policy ${name} is readonly and can thats why not created."
504 log DEBUG
"Creating vpn security policy ${name}"
505 copy
"$(vpn_security_policies_path "system
")" "$(vpn_security_policies_path ${name})"
510 vpn_security_policies_destroy
() {
511 # Function that deletes based on the passed parameters one ore more vpn security policies
514 if ! vpn_security_policy_exists
${name}; then
515 log ERROR
"The vpn security policy ${name} does not exist."
519 if vpn_security_policies_check_readonly
${name}; then
520 log ERROR
"The vpn security policy ${name} cannot be deleted."
524 log DEBUG
"Deleting vpn security policy ${name}"
525 settings_remove $
(vpn_security_policies_path
${name})