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
"
338 local connection=${1}
343 if [ ${length} -lt 4 ]; then
344 error "The PSK must be longer than four characters
"
348 if [ ${length} -gt 128 ]; then
349 error "The PSK cannot be longer than
128 characters
"
353 if ! ipsec_connection_write_config_key "${connection}" "PSK
" "${psk}"; then
354 log ERROR "Could not
write configuration settings
"
361 # Handle the cli after local
362 ipsec_connection_local() {
363 if [ ! $# -ge 2 ]; then
364 log ERROR "Not enough arguments
"
368 local connection=${1}
374 ipsec_connection_id "${connection}" "LOCAL
" $@
377 ipsec_connection_prefix "${connection}" "LOCAL
" $@
380 log ERROR "Unrecognized argument
: ${cmd}"
388 # Set the connection mode
389 ipsec_connection_mode() {
390 if [ ! $# -eq 2 ]; then
391 log ERROR "Not enough arguments
"
394 local connection=${1}
397 if ! isoneof mode ${IPSEC_VALID_MODES}; then
398 log ERROR "Mode
'${mode}' is invalid
"
402 if ! ipsec_connection_write_config_key "${connection}" "MODE
" ${mode}; then
403 log ERROR "Could not
write configuration settings
"
410 # Set the peer to connect to
411 ipsec_connection_peer() {
412 if [ ! $# -eq 2 ]; then
413 log ERROR "Not enough arguments
"
416 local connection=${1}
419 if ! ipsec_connection_check_peer ${peer}; then
420 log ERROR "Peer
'${peer}' is invalid
"
424 if ! ipsec_connection_write_config_key "${connection}" "PEER
" ${peer}; then
425 log ERROR "Could not
write configuration settings
"
432 #Set the local or remote id
433 ipsec_connection_id() {
434 if [ ! $# -eq 3 ]; then
435 log ERROR "Not enough arguments
"
438 local connection=${1}
442 if ! ipsec_connection_check_id ${id}; then
443 log ERROR "Id
'${id}' is invalid
"
447 if ! ipsec_connection_write_config_key "${connection}" "${type}_ID" ${id}; then
448 log ERROR
"Could not write configuration settings"
455 # Set the local or remote prefix
456 ipsec_connection_prefix
() {
457 if [ ! $# -ge 3 ]; then
458 log ERROR
"Not enough arguments"
461 local connection
=${1}
465 local _prefix
="${type}_PREFIX"
467 if ! ipsec_connection_read_config
"${connection}" "${_prefix}"; then
471 # Remove duplicated entries to proceed the list safely
472 assign
"${_prefix}" "$(list_unique ${!_prefix} )"
475 local prefixes_removed
478 while [ $# -gt 0 ]; do
483 list_append prefixes_added
"${arg:1}"
486 list_append prefixes_removed
"${arg:1}"
489 list_append prefixes_set
"${arg}"
492 error
"Invalid argument: ${arg}"
499 # Check if the user is trying a mixed operation
500 if ! list_is_empty prefixes_set
&& (! list_is_empty prefixes_added ||
! list_is_empty prefixes_removed
); then
501 error
"You cannot reset the prefix list and add or remove prefixes at the same time"
505 # Set new prefix list
506 if ! list_is_empty prefixes_set
; then
507 # Check if all prefixes are valid
509 for prefix
in ${prefixes_set}; do
510 if ! ip_net_is_valid
${prefix}; then
511 error
"Unsupported prefix: ${prefix}"
516 assign
"${_prefix}" "${prefixes_set}"
518 # Perform incremental updates
522 # Perform all removals
523 for prefix
in ${prefixes_removed}; do
524 if ! list_remove
"${_prefix}" ${prefix}; then
525 warning
"${prefix} was not on the list and could not be removed"
530 for prefix
in ${prefixes_added}; do
531 if ip_net_is_valid
${prefix}; then
532 if ! list_append_unique
"${_prefix}" ${prefix}; then
533 warning
"${prefix} is already on the prefix list"
536 warning
"${prefix} is not a valid IP network and could not be added"
541 # Check if the list contain at least one valid prefix
542 if list_is_empty
${_prefix}; then
543 error
"Cannot save an empty prefix list"
548 if ! ipsec_connection_write_config_key
"${connection}" "${_prefix}" ${!_prefix}; then
549 log ERROR "Could not
write configuration settings
"
555 # Handle the cli after remote
556 ipsec_connection_remote() {
557 if [ ! $# -ge 2 ]; then
558 log ERROR "Not enough arguments
"
562 local connection=${1}
568 ipsec_connection_id "${connection}" "REMOTE
" $@
572 ipsec_connection_prefix "${connection}" "REMOTE
" $@
575 log ERROR "Unrecognized argument
: ${cmd}"
583 # Set the inactivity timeout
584 ipsec_connection_inactivity_timeout() {
585 if [ ! $# -ge 2 ]; then
586 log ERROR "Not enough arguments
"
590 local connection=${1}
594 if ! isinteger value; then
595 value=$(parse_time $@)
596 if [ ! $? -eq 0 ]; then
597 log ERROR "Parsing the passed
time was not sucessful please check the passed values.
"
602 if [ ${value} -le 0 ]; then
603 log ERROR "The passed
time value must be
in the
sum greater zero seconds.
"
607 if ! ipsec_connection_write_config_key "${connection}" "INACTIVITY_TIMEOUT
" ${value}; then
608 log ERROR "Could not
write configuration settings
"
616 # Set the security policy to use
617 ipsec_connection_security_policy() {
618 if [ ! $# -eq 2 ]; then
619 log ERROR "Not enough arguments
"
622 local connection=${1}
623 local security_policy=${2}
625 if ! vpn_security_policy_exists ${security_policy}; then
626 log ERROR "No such vpn security policy
'${security_policy}'"
630 if ! ipsec_connection_write_config_key "${connection}" "SECURITY_POLICY
" ${security_policy}; then
631 log ERROR "Could not
write configuration settings
"
636 # Check if a id is valid
637 ipsec_connection_check_id() {
641 if [[ ${id} =~ ^@[[:alnum:]]+$ ]] || ip_is_valid ${id}; then
648 # Checks if a peer is valid
649 ipsec_connection_check_peer() {
653 # TODO Accept also FQDNs
654 if ip_is_valid ${peer}; then
661 # This function checks if a VPN IPsec connection name is valid
662 # Allowed are only A-Za-z0-9
663 ipsec_connection_check_name() {
666 local connection=${1}
668 [[ "${connection}" =~ [^[:alnum:]$] ]]
671 # Function that creates one VPN IPsec connection
672 ipsec_connection_new() {
673 if [ $# -gt 1 ]; then
674 error "Too many arguments
"
678 local connection="${1}"
679 if ! isset connection; then
680 error "Please provide a connection name
"
684 # Check for duplicates
685 if ipsec_connection_exists "${connection}"; then
686 error "The VPN IPsec connection
${connection} already exists
"
690 # Check if the name of the connection is valid
691 if ipsec_connection_check_name "${connection}"; then
692 error "'${connection}' contains illegal characters
"
696 log DEBUG "Creating VPN IPsec connection
${connection}"
698 if ! mkdir -p "${NETWORK_IPSEC_CONNS_DIR}/${connection}"; then
699 log ERROR "Could not create config directory
for ${connection}"
703 local ${IPSEC_CONNECTION_CONFIG_SETTINGS}
705 MODE=${IPSEC_DEFAULT_MODE}
706 AUTH_MODE=${IPSEC_DEFAULT_AUTH_MODE}
707 INACTIVITY_TIMEOUT=${IPSEC_DEFAULT_INACTIVITY_TIMEOUT}
708 SECURITY_POLICY=${IPSEC_DEFAULT_SECURITY_POLICY}
710 if ! ipsec_connection_write_config "${connection}"; then
711 log ERROR "Could not
write new config
file"
716 # Function that deletes based on the passed parameters one ore more vpn security policies
717 ipsec_connection_destroy() {
719 for connection in $@; do
720 if ! ipsec_connection_exists "${connection}"; then
721 log ERROR "The VPN IPsec connection
${connection} does not exist.
"
725 log DEBUG "Deleting VPN IPsec connection
${connection}"
726 if ! rm -rf "${NETWORK_IPSEC_CONNS_DIR}/${connection}"; then
727 log ERROR "Deleting the VPN IPsec connection
${connection} was not sucessful
"
733 # List all ipsec connections
734 ipsec_list_connections() {
736 for connection in ${NETWORK_IPSEC_CONNS_DIR}/*; do
737 [ -d ${connection} ] || continue
738 basename ${connection}