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_timeout|
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}
742 ipsec_connection_to_strongswan() {
743 local connection="${1}"
745 # Read the config settings
746 local ${IPSEC_CONNECTION_CONFIG_SETTINGS}
747 if ! ipsec_connection_read_config "${connection}"; then
748 error "Could not
read the connection
${connection}"
752 local path="${NETWORK_IPSEC_SWANCTL_CONNECTIONS_DIR}/${connection}.conf
"
755 # Write the connection section
756 _ipsec_connection_to_strongswan_connection "${connection}"
758 # Write the secrets section
759 _ipsec_connection_to_strongswan_secrets "${connection}"
764 _ipsec_connection_to_strongswan_connection() {
765 local connection="${1}"
767 # Read the security policy
768 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
769 if ! vpn_security_policies_read_config "${SECURITY_POLICY}"; then
773 print_indent 0 "connections
{"
774 print_indent 1 "${connection} {"
777 print_indent 2 "# IKE Version"
778 case "${KEY_EXCHANGE^^}" in
780 print_indent
2 "version = 1"
783 # Fall back to IKEv2 for any random values
785 print_indent
2 "version = 2"
791 print_indent
2 "# Local Address"
792 if isset LOCAL_ADDRESS
; then
793 print_indent
2 "local_addrs = ${LOCAL_ADDRESS}"
795 print_indent
2 "local_addrs = %any"
800 print_indent
2 "# Remote Address"
802 print_indent
2 "remote_addrs = ${PEER}"
804 print_indent
2 "remote_addrs = %any"
809 print_indent
2 "# IKE Proposals"
810 print_indent
2 "proposals = $(vpn_security_policies_make_ah_proposal ${SECURITY_POLICY})"
816 print_indent
2 "# Fragmentation"
817 print_indent
2 "fragmentation = yes"
821 print_indent
2 "local {"
824 if isset LOCAL_ID
; then
825 print_indent
3 "id = ${LOCAL_ID}"
829 case "${AUTH_MODE}" in
831 print_indent
3 "auth = psk"
839 print_indent
2 "remote {"
842 if isset REMOTE_ID
; then
843 print_indent
3 "id = ${REMOTE_ID}"
847 case "${AUTH_MODE}" in
849 print_indent
3 "auth = psk"
858 print_indent
2 "children {"
859 print_indent
3 "${connection} {"
861 print_indent
4 "# ESP Proposals"
862 print_indent
4 "esp_proposals = $(vpn_security_policies_make_esp_proposal ${SECURITY_POLICY})"
868 if isset LOCAL_PREFIX
; then
869 print_indent
4 "local_ts = $(list_join LOCAL_PREFIX ,)"
871 print_indent
4 "local_ts = dynamic"
875 if isset REMOTE_PREFIX
; then
876 print_indent
4 "remote_ts = $(list_join REMOTE_PREFIX ,)"
878 print_indent
4 "remote_ts = dynamic"
883 if isset LIFETIME
; then
884 print_indent
4 "# Rekey Time"
885 print_indent
4 "rekey_time = ${LIFETIME}"
890 print_indent
4 "updown = ${NETWORK_HELPERS_DIR}/ipsec-updown"
894 print_indent
4 "# Mode"
897 print_indent
4 "mode = transport"
900 print_indent
4 "mode = tunnel"
906 print_indent
4 "# Compression"
907 if enabled COMPRESSION
; then
908 print_indent
4 "ipcomp = yes"
910 print_indent
4 "ipcomp = no"
915 if isset INACTIVITY_TIMEOUT
; then
916 print_indent
4 "# Inactivity Timeout"
917 print_indent
4 "inactivity = ${INACTIVITY_TIMEOUT}"
922 print_indent
4 "start_action = start"
923 print_indent
4 "close_action = start"
934 _ipsec_connection_to_strongswan_secrets
() {
935 local connection
="${1}"
937 print_indent
0 "secrets {"
939 case "${AUTH_MODE}" in
941 print_indent
1 "ike {"
944 print_indent
2 "secret = ${PSK}"
947 if isset REMOTE_ID
; then
948 print_indent
2 "id = ${REMOTE_ID}"