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 declare -A VPN_SUPPORTED_CIPHERS
=(
28 [AES256-CBC
]="256 bit AES-CBC"
29 [AES192-CBC
]="192 bit AES-CBC"
30 [AES128-CBC
]="128 bit AES-CBC"
33 VPN_SUPPORTED_INTEGRITY
="SHA512 SHA256 SHA128"
34 VPN_SUPPORTED_GROUP_TYPES
="MODP8192 MODP4096"
36 # This functions checks if a policy is readonly
37 # returns true when yes and false when no
38 vpn_security_policies_check_readonly
() {
39 if isoneof name
${VPN_SECURITY_POLICIES_READONLY}; then
46 # This function writes all values to a via ${name} specificated vpn security policy configuration file
47 vpn_security_policies_write_config
() {
52 if ! vpn_security_policy_exists
"${name}"; then
53 log ERROR
"No such vpn security policy: ${name}"
57 if vpn_security_policies_check_readonly
"${name}"; then
58 log ERROR
"The ${name} vpn security policy cannot be changed."
62 local path
="$(vpn_security_policies_path "${name}")"
63 if [ ! -w ${path} ]; then
64 log ERROR
"${path} is not writeable"
68 if ! settings_write
"${path}" ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}; then
69 log ERROR
"Could not write configuration settings for vpn security policy ${name}"
73 # TODO everytime we successfully write a config we should call some trigger to take the changes into effect
76 # This funtion writes the value for one key to a via ${name} specificated vpn security policy configuration file
77 vpn_security_policies_write_config_key
() {
86 if ! vpn_security_policy_exists
"${name}"; then
87 log ERROR
"No such vpn security policy: ${name}"
91 log DEBUG
"Set '${key}' to new value '${value}' in vpn security policy ${name}"
93 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
95 # Read the config settings
96 if ! vpn_security_policies_read_config
"${name}"; then
100 # Set the key to a new value
101 assign
"${key}" "${value}"
103 if ! vpn_security_policies_write_config
"${name}"; then
110 # Reads one or more keys out of a settings file or all if no key is provided.
111 vpn_security_policies_read_config
() {
117 if ! vpn_security_policy_exists
"${name}"; then
118 log ERROR
"No such vpn security policy: ${name}"
124 if [ $# -eq 0 ] && [ -n "${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}" ]; then
125 list_append args
${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
130 local path
="$(vpn_security_policies_path ${name})"
132 if ! settings_read
"${path}" ${args}; then
133 log ERROR
"Could not read settings for vpn security policy ${name}"
138 # Returns the path to a the configuration fora given name
139 vpn_security_policies_path
() {
144 if vpn_security_policies_check_readonly
"${name}"; then
145 echo "${NETWORK_SHARE_DIR}/vpn/security-policies/${name}"
147 echo "${NETWORK_CONFIG_DIR}/vpn/security-policies/${name}"
151 # Print the content of a vpn security policy configuration file in a nice way
152 vpn_security_policies_show
() {
157 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
158 if ! vpn_security_policies_read_config
${name}; then
162 cli_print_fmt1
0 "Security Policy: ${name}"
165 # This could be done in a loop but a loop is much more complicated
166 # because we print 'Group Types' but the variable is named 'GROUP_TYPES'
167 cli_print_fmt1
1 "Ciphers:"
168 cli_print_fmt1
2 "${CIPHER}"
171 cli_print_fmt1
1 "Integrity:"
172 cli_print_fmt1
2 "${INTEGRITY}"
175 cli_print_fmt1
1 "Group Types:"
176 cli_print_fmt1
2 "${GROUP_TYPE}"
179 cli_print_fmt1
1 "Key Exchange:" "${KEY_EXCHANGE}"
182 if isinteger LIFETIME
&& [ ${LIFETIME} -gt 0 ]; then
183 cli_print_fmt1
1 "Key Lifetime:" "$(format_time ${LIFETIME})"
185 log ERROR
"The value for Key Lifetime is not a valid integer greater zero."
190 cli_print_fmt1
1 "Perfect Forward Secrecy:" "enabled"
192 cli_print_fmt1
1 "Perfect Forward Secrecy:" "disabled"
197 if enabled COMPRESSION
; then
198 cli_print_fmt1
1 "Compression:" "enabled"
200 cli_print_fmt1
1 "Compression:" "disabled"
205 # This function checks if a vpn security policy exists
206 # Returns True when yes and false when not
207 vpn_security_policy_exists
() {
212 local path
=$
(vpn_security_policies_path
"${name}")
214 [ -f ${path} ] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
218 # This function parses the parameters for the 'cipher' command
219 vpn_security_policies_cipher
(){
223 if [ $# -eq 0 ]; then
224 log ERROR
"You must pass at least one value after cipher"
229 if ! vpn_security_policies_read_config
${name} "CIPHER"; then
233 # Remove duplicated entries to proceed the list safely
234 CIPHER
="$(list_unique ${CIPHER})"
236 while [ $# -gt 0 ]; do
240 # Check if the cipher is in the list of ciphers and
241 # check if the list has after removing this cipher at least one valid value
242 if list_match
${value} ${CIPHER}; then
243 list_remove CIPHER
${value}
245 # We do not break here because this error does not break the processing of the next maybe valid values.
246 log ERROR
"Can not remove ${value} from the list of Ciphers because ${value} is not in the list."
251 # Check if the Ciphers is in the list of supported ciphers.
252 if ! isoneof value
${!VPN_SUPPORTED_CIPHERS[@]}; then
253 # We do not break here because this error does not break the processing of the next maybe valid values.
254 log ERROR
"${value} is not a supported cipher and can thats why not added to the list of ciphers."
256 if list_match
${value} ${CIPHER}; then
257 log WARNING
"${value} is already in the list of ciphers of this policy."
259 list_append CIPHER
${value}
267 # Check if the list contain at least one valid cipher
268 if [ $
(list_length
${CIPHER}) -ge 1 ]; then
269 if ! vpn_security_policies_write_config_key
${name} "CIPHER" ${CIPHER}; then
270 log ERROR
"The changes for the vpn security policy ${name} could not be written."
273 log ERROR
"After proceding all ciphers the list is empty and thats why no changes are written."
278 # This function parses the parameters for the 'compression' command
279 vpn_security_policies_compression
(){
283 # Check if we get only one argument after compression <name>
284 if [ ! $# -eq 2 ]; then
285 log ERROR
"The number of arguments do not match. Only one argument after compression is allowed."
289 if ! isbool value
; then
290 # We suggest only two values to avoid overburding the user.
291 log ERROR
"Invalid Argument ${value}"
295 vpn_security_policies_write_config_key
"${name}" "COMPRESSION" "${value}"
298 # This function parses the parameters for the 'group-type' command
299 vpn_security_policies_group_type
(){
303 if [ $# -eq 0 ]; then
304 log ERROR
"You must pass at least one value after group-type"
309 if ! vpn_security_policies_read_config
${name} "GROUP_TYPE"; then
313 # Remove duplicated entries to proceed the list safely
314 GROUP_TYPE
="$(list_unique ${GROUP_TYPE})"
316 while [ $# -gt 0 ]; do
320 # Check if the group type is in the list of group types and
321 # check if the list has after removing this group type at leatst one valid value
322 if list_match
${value} ${GROUP_TYPE}; then
323 list_remove GROUP_TYPE
${value}
325 # We do not break here because this error does not break the processing of the next maybe valid values.
326 log ERROR
"Can not remove ${value} from the list of group types because ${value} is not in the list."
331 # Check if the group type is in the list of supported group types.
332 if ! isoneof value
${VPN_SUPPORTED_GROUP_TYPES}; then
333 # We do not break here because the processing of other maybe valid values are indepent from this error.
334 log ERROR
"${value} is not a supported group type and can thats why not added to the list of group types."
336 if list_match
${value} ${GROUP_TYPE}; then
337 log WARNING
"${value} is already in the list of group-types of this policy."
339 list_append GROUP_TYPE
${value}
347 # Check if the list contain at least one valid group-type
348 if [ $
(list_length
${GROUP_TYPE}) -ge 1 ]; then
349 if ! vpn_security_policies_write_config_key
${name} "GROUP_TYPE" ${GROUP_TYPE}; then
350 log ERROR
"The changes for the vpn security policy ${name} could not be written."
353 log ERROR
"After proceding all group types the list is empty and thats why no changes are written."
358 # This function parses the parameters for the 'integrity' command
359 vpn_security_policies_integrity
(){
363 if [ $# -eq 0 ]; then
364 log ERROR
"You must pass at least one value after integrity."
369 if ! vpn_security_policies_read_config
${name} "INTEGRITY"; then
373 # Remove duplicated entries to proceed the list safely
374 INTEGRITY
="$(list_unique ${INTEGRITY})"
376 while [ $# -gt 0 ]; do
380 # Check if the integrity hash is in the list of integrity hashes and
381 # check if the list has after removing this integrity hash at least one valid value
382 if list_match
${value} ${INTEGRITY}; then
383 list_remove INTEGRITY
${value}
385 # We do not break here because the processing of other maybe valid values are indepent from this error.
386 log ERROR
"Can not remove ${value} from the list of integrity hashes because ${value} is not in the list."
391 # Check if the Ciphers is in the list of supported integrity hashes.
392 if ! isoneof value
${VPN_SUPPORTED_INTEGRITY}; then
393 # We do not break here because the processing of other maybe valid values are indepent from this error.
394 log ERROR
"${value} is not a supported integrity hash and can thats why not added to the list of integrity hashes."
396 if list_match
${value} ${INTEGRITY}; then
397 log WARNING
"${value} is already in the list of integrety hashes of this policy."
399 list_append INTEGRITY
${value}
407 # Check if the list contain at least one valid group-type
408 if [ $
(list_length
${INTEGRITY}) -ge 1 ]; then
409 if ! vpn_security_policies_write_config_key
${name} "INTEGRITY" ${INTEGRITY}; then
410 log ERROR
"The changes for the vpn security policy ${name} could not be written."
413 log ERROR
"After proceding all integrity hashes the list is empty and thats why no changes are written."
418 # This function parses the parameters for the 'key-exchange' command
419 vpn_security_policies_key_exchange
() {
423 # Check if we get only one argument after key-exchange <name>
424 if [ ! $# -eq 2 ]; then
425 log ERROR
"The number of arguments do not match. Only argument after key-exchange is allowed."
429 if ! isoneof value
"ikev1" "ikev2" "IKEV1" "IKEV2"; then
430 log ERROR
"Invalid Argument ${value}"
434 vpn_security_policies_write_config_key
"${name}" "KEY_EXCHANGE" "${value,,}"
437 # This function parses the parameters for the 'lifetime' command.
438 vpn_security_policies_lifetime
(){
444 # Check if we get only one argument after lifetime <name>
445 if [ ! $# -ge 1 ]; then
446 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"
450 if ! isinteger value
; then
451 value
=$
(parse_time $@
)
452 if [ ! $?
-eq 0 ]; then
453 log ERROR
"Parsing the passed time was not sucessful please check the passed values."
458 if [ ${value} -le 0 ]; then
459 log ERROR
"The passed time value must be in the sum greater zero seconds."
463 vpn_security_policies_write_config_key
"${name}" "LIFETIME" "${value}"
466 # This function parses the parameters for the 'pfs' command
467 vpn_security_policies_pfs
(){
471 # Check if we get only one argument after pfs <name>
472 if [ ! $# -eq 2 ]; then
473 log ERROR
"The number of arguments do not match. Only argument after pfs is allowed."
477 if [ ! $# -eq 2 ] ||
! isbool value
; then
478 # We suggest only two values to avoid overburding the user.
479 log ERROR
"Invalid Argument ${value}"
483 vpn_security_policies_write_config_key
"${name}" "PFS" "${value}"
486 # This function checks if a vpn security policy name is valid
487 # Allowed are only A-Za-z0-9
488 vpn_security_policies_check_name
() {
493 [[ ${name} =~
[^
[:alnum
:]$
] ]]
496 # Function that creates based on the paramters one ore more new vpn security policies
497 vpn_security_policies_new
() {
498 if [ $# -gt 1 ]; then
499 error
"Too many arguments"
504 if ! isset name
; then
505 error
"Please provide a name"
509 # Check for duplicates
510 if vpn_security_policy_exists
"${name}"; then
511 error
"The VPN security policy with name ${name} already exists"
515 # Check if name is valid
516 if vpn_security_policies_check_name
"${name}"; then
517 error
"'${name}' contains illegal characters"
521 # Check if we have a read-only policy with the same name
522 if vpn_security_policies_check_readonly
"${name}"; then
523 error
"The VPN security policy ${name} is read-only"
527 # Check if our source policy exists
528 if ! vpn_security_policy_exists
"${VPN_DEFAULT_SECURITY_POLICY}"; then
529 error
"Default VPN Security Policy '${VPN_DEFAULT_SECURITY_POLICY}' does not exist"
533 log DEBUG
"Creating VPN Security Policy ${name}"
535 if copy
"$(vpn_security_policies_path "${VPN_DEFAULT_SECURITY_POLICY}")"
536 "$(vpn_security_policies_path ${name})"; then
537 log INFO
"VPN Security Policy ${name} successfully created"
539 log ERROR
"Could not create VPN Security Policy ${name}"
543 # Show the newly created policy
544 vpn_security_policies_show
"${name}"
547 # Function that deletes based on the passed parameters one ore more vpn security policies
548 vpn_security_policies_destroy
() {
551 if ! vpn_security_policy_exists
${name}; then
552 log ERROR
"The vpn security policy ${name} does not exist."
556 if vpn_security_policies_check_readonly
${name}; then
557 log ERROR
"The vpn security policy ${name} cannot be deleted."
561 log DEBUG
"Deleting vpn security policy ${name}"
562 settings_remove $
(vpn_security_policies_path
${name})