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 IPSEC_CONNECTION_CONFIG_SETTINGS
="AUTH_MODE INACTIVITY_TIMEOUT LOCAL_ID LOCAL_PREFIX"
23 IPSEC_CONNECTION_CONFIG_SETTINGS
="${IPSEC_CONNECTION_CONFIG_SETTINGS} MODE PEER PSK"
24 IPSEC_CONNECTION_CONFIG_SETTINGS
="${IPSEC_CONNECTION_CONFIG_SETTINGS} REMOTE_ID REMOTE_PREFIX"
25 IPSEC_CONNECTION_CONFIG_SETTINGS
="${IPSEC_CONNECTION_CONFIG_SETTINGS} SECURITY_POLICY"
28 IPSEC_DEFAULT_MODE
="tunnel"
29 IPSEC_DEFAULT_AUTH_MODE
="PSK"
30 IPSEC_DEFAULT_INACTIVITY_TIMEOUT
="0"
31 IPSEC_DEFAULT_SECURITY_POLICY
="system"
33 IPSEC_VALID_MODES
="gre-transport tunnel vti"
34 IPSEC_VALID_AUTH_MODES
="PSK"
42 cli_ipsec_connection $@
45 error
"Unrecognized argument: ${action}"
51 cli_ipsec_connection
() {
52 if ipsec_connection_exists
${1}; then
59 authentication|inactivity-timout|
local|mode|peer|remote|security-policy
)
60 ipsec_connection_
${key} ${connection} $@
63 cli_ipsec_connection_show
"${connection}"
67 error
"Unrecognized argument: ${key}"
77 ipsec_connection_new $@
80 ipsec_connection_destroy $@
83 if [ -n "${action}" ]; then
84 error
"Unrecognized argument: '${action}'"
92 cli_ipsec_connection_show
() {
93 local connection
="${1}"
95 # Read the config settings
96 local ${IPSEC_CONNECTION_CONFIG_SETTINGS}
97 if ! ipsec_connection_read_config
"${connection}"; then
98 error
"Could not read the connection configuration"
102 cli_headline
0 "IPsec VPN Connection: ${connection}"
107 cli_print_fmt1
1 "Peer" "${PEER}"
111 cli_print_fmt1
1 "Security Policy" "${SECURITY_POLICY-${IPSEC_DEFAULT_SECURITY_POLICY}}"
114 cli_headline
2 "Authentication"
115 case "${AUTH_MODE^^}" in
117 cli_print_fmt1
2 "Mode" "Pre-Shared-Key"
120 cli_print_fmt1
2 "Pre-Shared-Key" "****"
122 cli_print_fmt1
2 "Pre-Shared-Key" "- is not set -"
132 for i
in LOCAL REMOTE
; do
135 cli_headline
2 "Local"
138 cli_headline
2 "Remote"
142 local id_var
="${i}_ID"
143 if [ -n "${!id_var}" ]; then
144 cli_print_fmt1
2 "ID" "${!id_var}"
147 local prefix_var
="${i}_PREFIX"
148 if isset
${prefix_var}; then
149 cli_headline
3 "Prefix(es)"
152 for prefix
in ${!prefix_var}; do
153 cli_print_fmt1
3 "${prefix}"
160 cli_headline
2 "Misc."
164 cli_print_fmt1
2 "Transport Mode" "GRE Transport"
167 cli_print_fmt1
2 "Transport Mode" "Tunnel"
170 cli_print_fmt1
2 "Transport Mode" "Virtual Tunnel Interface"
173 cli_print_fmt1
2 "Transport Mode" "- Unknown -"
178 if isset INACTIVITY_TIMEOUT
&& [ ${INACTIVITY_TIMEOUT} -gt 0 ]; then
179 cli_print_fmt1
2 "Inactivity Timeout" "$(format_time ${INACTIVITY_TIMEOUT})"
186 # This function writes all values to a via ${connection} specificated VPN IPsec configuration file
187 ipsec_connection_write_config
() {
190 local connection
="${1}"
192 if ! ipsec_connection_exists
"${connection}"; then
193 log ERROR
"No such VPN IPsec connection: ${connection}"
197 local path
="${NETWORK_IPSEC_CONNS_DIR}/${connection}/settings"
199 if ! settings_write
"${path}" ${IPSEC_CONNECTION_CONFIG_SETTINGS}; then
200 log ERROR
"Could not write configuration settings for VPN IPsec connection ${connection}"
204 ipsec_reload
${connection}
207 # This funtion writes the value for one key to a via ${connection} specificated VPN IPsec connection configuration file
208 ipsec_connection_write_config_key
() {
211 local connection
=${1}
217 if ! ipsec_connection_exists
"${connection}"; then
218 log ERROR
"No such VPN ipsec connection: ${connection}"
222 log DEBUG
"Set '${key}' to new value '${value}' in VPN ipsec connection '${connection}'"
224 local ${IPSEC_CONNECTION_CONFIG_SETTINGS}
226 # Read the config settings
227 if ! ipsec_connection_read_config
"${connection}"; then
231 # Set the key to a new value
232 assign
"${key}" "${value}"
234 if ! ipsec_connection_write_config
"${connection}"; then
241 # Reads one or more keys out of a settings file or all if no key is provided.
242 ipsec_connection_read_config
() {
245 local connection
="${1}"
248 if ! ipsec_connection_exists
"${connection}"; then
249 log ERROR
"No such VPN IPsec connection : ${connection}"
255 if [ $# -eq 0 ] && [ -n "${IPSEC_CONNECTION_CONFIG_SETTINGS}" ]; then
256 list_append args
${IPSEC_CONNECTION_CONFIG_SETTINGS}
261 local path
="${NETWORK_IPSEC_CONNS_DIR}/${connection}/settings"
263 if ! settings_read
"${path}" ${args}; then
264 log ERROR
"Could not read settings for VPN IPsec connection ${connection}"
269 # This function checks if a vpn ipsec connection exists
270 # Returns True when yes and false when not
271 ipsec_connection_exists
() {
274 local connection
=${1}
276 local path
="${NETWORK_IPSEC_CONNS_DIR}/${connection}"
278 [ -d "${path}" ] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
281 # Reloads the connection after config changes
286 # Handle the cli after authentification
287 ipsec_connection_authentication() {
288 if [ ! $# -gt 1 ]; then
289 log ERROR "Not enough arguments
"
293 local connection=${1}
299 ipsec_connection_authentication_mode "${connection}" $@
302 ipsec_connection_authentication_psk "${connection}" $@
305 log ERROR "Unrecognized argument
: ${cmd}"
311 # Set the authentification mode
312 ipsec_connection_authentication_mode() {
313 if [ ! $# -eq 2 ]; then
314 log ERROR "Not enough arguments
"
317 local connection=${1}
320 if ! isoneof mode ${IPSEC_VALID_AUTH_MODES}; then
321 log ERROR "Auth mode
'${mode}' is invalid
"
325 if ! ipsec_connection_write_config_key "${connection}" "AUTH_MODE
" ${mode^^}; then
326 log ERROR "Could not
write configuration settings
"
332 ipsec_connection_authentication_psk() {
333 if [ ! $# -eq 2 ]; then
334 log ERROR "Not enough arguments
"
337 local connection=${1}
340 # TODO Check if psk is valid
342 if ! ipsec_connection_write_config_key "${connection}" "PSK
" ${psk}; then
343 log ERROR "Could not
write configuration settings
"
350 # Handle the cli after local
351 ipsec_connection_local() {
352 if [ ! $# -ge 2 ]; then
353 log ERROR "Not enough arguments
"
357 local connection=${1}
363 ipsec_connection_id "${connection}" "LOCAL
" $@
366 ipsec_connection_prefix "${connection}" "LOCAL
" $@
369 log ERROR "Unrecognized argument
: ${cmd}"
377 # Set the connection mode
378 ipsec_connection_mode() {
379 if [ ! $# -eq 2 ]; then
380 log ERROR "Not enough arguments
"
383 local connection=${1}
386 if ! isoneof mode ${IPSEC_VALID_MODES}; then
387 log ERROR "Mode
'${mode}' is invalid
"
391 if ! ipsec_connection_write_config_key "${connection}" "MODE
" ${mode}; then
392 log ERROR "Could not
write configuration settings
"
399 # Set the peer to connect to
400 ipsec_connection_peer() {
401 if [ ! $# -eq 2 ]; then
402 log ERROR "Not enough arguments
"
405 local connection=${1}
408 if ! ipsec_connection_check_peer ${peer}; then
409 log ERROR "Peer
'${peer}' is invalid
"
413 if ! ipsec_connection_write_config_key "${connection}" "PEER
" ${peer}; then
414 log ERROR "Could not
write configuration settings
"
421 #Set the local or remote id
422 ipsec_connection_id() {
423 if [ ! $# -eq 3 ]; then
424 log ERROR "Not enough arguments
"
427 local connection=${1}
431 if ! ipsec_connection_check_id ${id}; then
432 log ERROR "Id
'${id}' is invalid
"
436 if ! ipsec_connection_write_config_key "${connection}" "${type}_ID" ${id}; then
437 log ERROR
"Could not write configuration settings"
444 # Set the local or remote prefix
445 ipsec_connection_prefix
() {
446 if [ ! $# -ge 3 ]; then
447 log ERROR
"Not enough arguments"
450 local connection
=${1}
454 local _prefix
="${type}_PREFIX"
456 if ! ipsec_connection_read_config
"${connection}" "${_prefix}"; then
460 # Remove duplicated entries to proceed the list safely
461 assign
"${_prefix}" "$(list_unique ${!_prefix} )"
464 local prefixes_removed
467 while [ $# -gt 0 ]; do
472 list_append prefixes_added
"${arg:1}"
475 list_append prefixes_removed
"${arg:1}"
478 list_append prefixes_set
"${arg}"
481 error
"Invalid argument: ${arg}"
488 # Check if the user is trying a mixed operation
489 if ! list_is_empty prefixes_set
&& (! list_is_empty prefixes_added ||
! list_is_empty prefixes_removed
); then
490 error
"You cannot reset the prefix list and add or remove prefixes at the same time"
494 # Set new prefix list
495 if ! list_is_empty prefixes_set
; then
496 # Check if all prefixes are valid
498 for prefix
in ${prefixes_set}; do
499 if ! ip_net_is_valid
${prefix}; then
500 error
"Unsupported prefix: ${prefix}"
505 assign
"${_prefix}" "${prefixes_set}"
507 # Perform incremental updates
511 # Perform all removals
512 for prefix
in ${prefixes_removed}; do
513 if ! list_remove
"${_prefix}" ${prefix}; then
514 warning
"${prefix} was not on the list and could not be removed"
519 for prefix
in ${prefixes_added}; do
520 if ip_net_is_valid
${prefix}; then
521 if ! list_append_unique
"${_prefix}" ${prefix}; then
522 warning
"${prefix} is already on the prefix list"
525 warning
"${prefix} is not a valiv IP net and could not be added"
530 # Check if the list contain at least one valid prefix
531 if list_is_empty
${_prefix}; then
532 error
"Cannot save an empty prefix list"
537 if ! ipsec_connection_write_config_key
"${connection}" "${_prefix}" ${!_prefix}; then
538 log ERROR "Could not
write configuration settings
"
544 # Handle the cli after remote
545 ipsec_connection_remote() {
546 if [ ! $# -ge 2 ]; then
547 log ERROR "Not enough arguments
"
551 local connection=${1}
557 ipsec_connection_id "${connection}" "REMOTE
" $@
561 ipsec_connection_prefix "${connection}" "REMOTE
" $@
564 log ERROR "Unrecognized argument
: ${cmd}"
572 # Set the inactivity timeout
573 ipsec_connection_inactivity_timeout() {
574 if [ ! $# -ge 2 ]; then
575 log ERROR "Not enough arguments
"
579 local connection=${1}
583 if ! isinteger value; then
584 value=$(parse_time $@)
585 if [ ! $? -eq 0 ]; then
586 log ERROR "Parsing the passed
time was not sucessful please check the passed values.
"
591 if [ ${value} -le 0 ]; then
592 log ERROR "The passed
time value must be
in the
sum greater zero seconds.
"
596 if ! ipsec_connection_write_config_key "${connection}" "INACTIVITY_TIMEOUT
" ${value}; then
597 log ERROR "Could not
write configuration settings
"
605 # Set the security policy to use
606 ipsec_connection_security_policy() {
607 if [ ! $# -eq 2 ]; then
608 log ERROR "Not enough arguments
"
611 local connection=${1}
612 local security_policy=${2}
614 if ! vpn_security_policy_exists ${security_policy}; then
615 log ERROR "No such vpn security policy
'${security_policy}'"
619 if ! ipsec_connection_write_config_key "${connection}" "SECURITY_POLICY
" ${security_policy}; then
620 log ERROR "Could not
write configuration settings
"
625 # Check if a id is valid
626 ipsec_connection_check_id() {
630 if [[ ${id} =~ ^@[[:alnum:]]+$ ]] || ip_is_valid ${id}; then
637 # Checks if a peer is valid
638 ipsec_connection_check_peer() {
642 # TODO Accept also FQDNs
643 if ip_is_valid ${peer}; then
650 # This function checks if a VPN IPsec connection name is valid
651 # Allowed are only A-Za-z0-9
652 ipsec_connection_check_name() {
655 local connection=${1}
657 [[ "${connection}" =~ [^[:alnum:]$] ]]
660 # Function that creates one VPN IPsec connection
661 ipsec_connection_new() {
662 if [ $# -gt 1 ]; then
663 error "Too many arguments
"
667 local connection="${1}"
668 if ! isset connection; then
669 error "Please provide a connection name
"
673 # Check for duplicates
674 if ipsec_connection_exists "${connection}"; then
675 error "The VPN IPsec connection
${connection} already exists
"
679 # Check if the name of the connection is valid
680 if ipsec_connection_check_name "${connection}"; then
681 error "'${connection}' contains illegal characters
"
685 log DEBUG "Creating VPN IPsec connection
${connection}"
687 if ! mkdir -p "${NETWORK_IPSEC_CONNS_DIR}/${connection}"; then
688 log ERROR "Could not create config directory
for ${connection}"
692 local ${IPSEC_CONNECTION_CONFIG_SETTINGS}
694 MODE=${IPSEC_DEFAULT_MODE}
695 AUTH_MODE=${IPSEC_DEFAULT_AUTH_MODE}
696 INACTIVITY_TIMEOUT=${IPSEC_DEFAULT_INACTIVITY_TIMEOUT}
697 SECURITY_POLICY=${IPSEC_DEFAULT_SECURITY_POLICY}
699 if ! ipsec_connection_write_config "${connection}"; then
700 log ERROR "Could not
write new config
file"
705 # Function that deletes based on the passed parameters one ore more vpn security policies
706 ipsec_connection_destroy() {
708 for connection in $@; do
709 if ! ipsec_connection_exists "${connection}"; then
710 log ERROR "The VPN IPsec connection
${connection} does not exist.
"
714 log DEBUG "Deleting VPN IPsec connection
${connection}"
715 if ! rm -rf "${NETWORK_IPSEC_CONNS_DIR}/${connection}"; then
716 log ERROR "Deleting the VPN IPsec connection
${connection} was not sucessful
"