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
="\
41 IPSEC_POOL_CONFIG_SETTINGS
="\
47 IPSEC_DEFAULT_AUTH_MODE
="PSK"
48 IPSEC_DEFAULT_DPD_ACTION
="restart"
49 IPSEC_DEFAULT_DPD_DELAY
="30"
50 IPSEC_DEFAULT_DPD_TIMEOUT
="120"
51 IPSEC_DEFAULT_ENABLED
="true"
52 IPSEC_DEFAULT_INACTIVITY_TIMEOUT
="0"
53 IPSEC_DEFAULT_MODE
="tunnel"
54 IPSEC_DEFAULT_SECURITY_POLICY
="system"
55 IPSEC_DEFAULT_START_ACTION
="on-demand"
57 IPSEC_VALID_MODES
="gre-transport tunnel vti"
58 IPSEC_VALID_AUTH_MODES
="PSK"
66 cli_ipsec_connection $@
72 error
"Unrecognized argument: ${action}"
78 cli_ipsec_connection
() {
79 if ipsec_connection_exists
${1}; then
86 authentication|down|disable|dpd|
enable|inactivity_timeout|
local|mode|peer|pool|remote|security_policy|start_action|up
)
87 ipsec_connection_
${key} ${connection} $@
90 cli_ipsec_connection_show
"${connection}"
94 error
"Unrecognized argument: ${key}"
104 ipsec_connection_new $@
107 cli_ipsec_connection_destroy $@
110 if [ -n "${action}" ]; then
111 error
"Unrecognized argument: '${action}'"
120 if ipsec_pool_exists
${1}; then
128 ipsec_pool_
${key} ${pool} $@
131 cli_ipsec_pool_show
"${pool}"
135 error
"Unrecognized argument: ${key}"
148 ipsec_pool_destroy $@
151 if [ -n "${action}" ]; then
152 error
"Unrecognized argument: '${action}'"
160 cli_ipsec_connection_destroy
() {
161 local connection
="${1}"
163 if ! ipsec_connection_destroy
"${connection}"; then
167 # Inform strongswan about the changes
168 ipsec_strongswan_load
170 # Configure strongswan autostart
171 ipsec_strongswan_autostart
174 cli_ipsec_connection_show
() {
175 local connection
="${1}"
177 # Read the config settings
178 local ${IPSEC_CONNECTION_CONFIG_SETTINGS}
179 if ! ipsec_connection_read_config
"${connection}"; then
180 error
"Could not read the connection configuration"
184 cli_headline
0 "IPsec VPN Connection: ${connection}"
189 cli_print_fmt1
1 "Peer" "${PEER}"
193 cli_print_fmt1
1 "Security Policy" "${SECURITY_POLICY-${IPSEC_DEFAULT_SECURITY_POLICY}}"
196 cli_headline
2 "Authentication"
197 case "${AUTH_MODE^^}" in
199 cli_print_fmt1
2 "Mode" "Pre-Shared-Key"
202 cli_print_fmt1
2 "Pre-Shared-Key" "****"
204 cli_print_fmt1
2 "Pre-Shared-Key" "- is not set -"
214 for i
in LOCAL REMOTE
; do
217 cli_headline
2 "Local"
220 cli_headline
2 "Remote"
224 local id_var
="${i}_ID"
225 if [ -n "${!id_var}" ]; then
226 cli_print_fmt1
2 "ID" "${!id_var}"
229 local prefix_var
="${i}_PREFIX"
230 if isset
${prefix_var}; then
231 cli_headline
3 "Prefix(es)"
234 for prefix
in ${!prefix_var}; do
235 cli_print_fmt1
3 "${prefix}"
242 cli_headline
2 "Misc."
246 cli_print_fmt1
2 "Transport Mode" "GRE Transport"
249 cli_print_fmt1
2 "Transport Mode" "Tunnel"
252 cli_print_fmt1
2 "Transport Mode" "Virtual Tunnel Interface"
255 cli_print_fmt1
2 "Transport Mode" "- Unknown -"
260 if isset INACTIVITY_TIMEOUT
&& [ ${INACTIVITY_TIMEOUT} -gt 0 ]; then
261 cli_print_fmt1
2 "Inactivity Timeout" "$(format_time ${INACTIVITY_TIMEOUT})"
268 ipsec_connection_disable
() {
269 local connection
=${1}
271 if ! ipsec_connection_write_config_key
"${connection}" "ENABLED" "false"; then
272 log ERROR
"Could not write configuration settings"
276 # Configure strongswan autostart
277 ipsec_strongswan_autostart
280 ipsec_connection_enable
() {
281 local connection
=${1}
283 if ! ipsec_connection_write_config_key
"${connection}" "ENABLED" "true"; then
284 log ERROR
"Could not write configuration settings"
288 # Configure strongswan autostart
289 ipsec_strongswan_autostart
292 # This function writes all values to a via ${connection} specificated VPN IPsec configuration file
293 ipsec_connection_write_config
() {
296 local connection
="${1}"
298 if ! ipsec_connection_exists
"${connection}"; then
299 log ERROR
"No such VPN IPsec connection: ${connection}"
303 local path
="${NETWORK_IPSEC_CONNS_DIR}/${connection}/settings"
305 if ! settings_write
"${path}" ${IPSEC_CONNECTION_CONFIG_SETTINGS}; then
306 log ERROR
"Could not write configuration settings for VPN IPsec connection ${connection}"
310 ipsec_reload
${connection}
313 # This funtion writes the value for one key to a via ${connection} specificated VPN IPsec connection configuration file
314 ipsec_connection_write_config_key
() {
317 local connection
=${1}
323 if ! ipsec_connection_exists
"${connection}"; then
324 log ERROR
"No such VPN ipsec connection: ${connection}"
328 log DEBUG
"Set '${key}' to new value '${value}' in VPN ipsec connection '${connection}'"
330 local ${IPSEC_CONNECTION_CONFIG_SETTINGS}
332 # Read the config settings
333 if ! ipsec_connection_read_config
"${connection}"; then
337 # Set the key to a new value
338 assign
"${key}" "${value}"
340 if ! ipsec_connection_write_config
"${connection}"; then
347 # Reads one or more keys out of a settings file or all if no key is provided.
348 ipsec_connection_read_config
() {
351 local connection
="${1}"
354 if ! ipsec_connection_exists
"${connection}"; then
355 log ERROR
"No such VPN IPsec connection : ${connection}"
361 if [ $# -eq 0 ] && [ -n "${IPSEC_CONNECTION_CONFIG_SETTINGS}" ]; then
362 list_append args
${IPSEC_CONNECTION_CONFIG_SETTINGS}
367 local path
="${NETWORK_IPSEC_CONNS_DIR}/${connection}/settings"
369 if ! settings_read
"${path}" ${args}; then
370 log ERROR
"Could not read settings for VPN IPsec connection ${connection}"
375 # This function checks if a vpn ipsec connection exists
376 # Returns True when yes and false when not
377 ipsec_connection_exists
() {
380 local connection
=${1}
382 local path
="${NETWORK_IPSEC_CONNS_DIR}/${connection}"
384 [ -d "${path}" ] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
387 # Determines if strongswan should be automatically started
388 # when the system boots up.
389 ipsec_strongswan_autostart() {
390 local autostart_needed="false
"
393 for connection in $(ipsec_list_connections); do
396 if ! ipsec_connection_read_config "${connection}" "ENABLED
"; then
397 log WARNING "Could not
read configuation
"
401 if enabled ENABLED; then
402 autostart_needed="true
"
407 # Start strongswan when we need it and when it is not yet enabled
408 if ${autostart_needed}; then
409 if ! service_is_enabled "strongswan
"; then
410 service_enable "strongswan
"
413 if ! service_is_active "strongswan
"; then
414 service_start "strongswan
"
417 # Disable strongswan when we do not need it but it is enabled
418 elif ! ${autostart_needed}; then
419 if service_is_enabled "strongswan
"; then
420 service_disable "strongswan
"
423 if service_is_active "strongswan
"; then
424 service_stop "strongswan
"
429 ipsec_strongswan_load() {
430 # Do nothing if strongswan is not running
431 if ! service_is_active "strongswan
"; then
435 if ! cmd swanctl --load-all; then
436 log ERROR "Could not reload strongswan config
"
441 # Reloads the connection after config changes
443 local connection=${1}
447 if ! ipsec_connection_read_config "${connection}" "ENABLED
"; then
448 log ERROR "Could not
read configuration
for IPsec connection
${connection}"
452 if enabled ENABLED; then
453 if ! ipsec_connection_to_strongswan ${connection}; then
454 log ERROR "Could not generate strongswan config
for ${connnection}"
458 log DEBUG "Deleting strongswan config
${NETWORK_IPSEC_SWANCTL_CONNECTIONS_DIR}/${connection}.conf
"
459 unlink "${NETWORK_IPSEC_SWANCTL_CONNECTIONS_DIR}/${connection}.conf
"
462 ipsec_strongswan_load
465 # Handle the cli after authentification
466 ipsec_connection_authentication() {
467 if [ ! $# -gt 1 ]; then
468 log ERROR "Not enough arguments
"
472 local connection=${1}
478 ipsec_connection_authentication_mode "${connection}" $@
481 ipsec_connection_authentication_psk "${connection}" $@
484 log ERROR "Unrecognized argument
: ${cmd}"
490 # Set the authentification mode
491 ipsec_connection_authentication_mode() {
492 if [ ! $# -eq 2 ]; then
493 log ERROR "Not enough arguments
"
496 local connection=${1}
499 if ! isoneof mode ${IPSEC_VALID_AUTH_MODES}; then
500 log ERROR "Auth mode
'${mode}' is invalid
"
504 if ! ipsec_connection_write_config_key "${connection}" "AUTH_MODE
" ${mode^^}; then
505 log ERROR "Could not
write configuration settings
"
511 ipsec_connection_authentication_psk() {
512 if [ ! $# -eq 2 ]; then
513 log ERROR "Not enough arguments
"
517 local connection=${1}
522 if [ ${length} -lt 4 ]; then
523 error "The PSK must be longer than four characters
"
527 if [ ${length} -gt 128 ]; then
528 error "The PSK cannot be longer than
128 characters
"
532 if ! ipsec_connection_write_config_key "${connection}" "PSK
" "${psk}"; then
533 log ERROR "Could not
write configuration settings
"
540 ipsec_connection_up() {
541 local connection="${1}"
543 if ! ipsec_connection_exists "${connection}"; then
544 error "No such VPN IPsec connection
: ${connection}"
548 cmd swanctl --initiate --child "${connection}"
551 ipsec_connection_down() {
552 local connection="${1}"
554 if ! ipsec_connection_exists "${connection}"; then
555 error "No such VPN IPsec connection
: ${connection}"
559 cmd swanctl --terminate --ike "${connection}"
562 # Handle the cli after authentification
563 ipsec_connection_dpd() {
564 if [ ! $# -gt 1 ]; then
565 log ERROR "Not enough arguments
"
569 local connection=${1}
575 ipsec_connection_dpd_action "${connection}" $@
578 ipsec_connection_dpd_delay "${connection}" $@
581 ipsec_connection_dpd_timeout "${connection}" $@
584 log ERROR "Unrecognized argument
: ${cmd}"
590 # Set the default dpd action
591 ipsec_connection_dpd_action() {
592 if [ ! $# -eq 2 ]; then
593 log ERROR "Not enough arguments
"
596 local connection=${1}
599 if ! isoneof action "restart
" "clear"; then
600 log ERROR "dpd action
'${action}' is invalid
"
604 if ! ipsec_connection_write_config_key "${connection}" "DPD_ACTION
" ${action}; then
605 log ERROR "Could not
write configuration settings
"
611 ipsec_connection_dpd_delay() {
612 if [ ! $# -ge 2 ]; then
613 log ERROR "Not enough arguments
"
617 local connection=${1}
621 if ! isinteger value; then
622 value=$(parse_time $@)
623 if [ ! $? -eq 0 ]; then
624 log ERROR "Parsing the passed
time was not sucessful please check the passed values.
"
629 if [ ${value} -lt 0 ]; then
630 log ERROR "The passed
time value must be
in the
sum greater or equal zero seconds.
"
634 if ! ipsec_connection_write_config_key "${connection}" "DPD_DELAY
" ${value}; then
635 log ERROR "Could not
write configuration settings
"
642 # Set the dpd timeout
643 ipsec_connection_dpd_timeout() {
644 if [ ! $# -ge 2 ]; then
645 log ERROR "Not enough arguments
"
649 local connection=${1}
653 if ! isinteger value; then
654 value=$(parse_time $@)
655 if [ ! $? -eq 0 ]; then
656 log ERROR "Parsing the passed
time was not sucessful please check the passed values.
"
661 if [ ${value} -le 0 ]; then
662 log ERROR "The passed
time value must be
in the
sum greater or equal zero seconds.
"
666 if ! ipsec_connection_write_config_key "${connection}" "DPD_TIMEOUT
" ${value}; then
667 log ERROR "Could not
write configuration settings
"
674 # Handle the cli after local
675 ipsec_connection_local() {
676 if [ ! $# -ge 2 ]; then
677 log ERROR "Not enough arguments
"
681 local connection=${1}
687 ipsec_connection_local_address "${connection}" $@
690 ipsec_connection_id "${connection}" "LOCAL
" $@
693 ipsec_connection_prefix "${connection}" "LOCAL
" $@
696 log ERROR "Unrecognized argument
: ${cmd}"
704 # Set the connection mode
705 ipsec_connection_mode() {
706 if [ ! $# -eq 2 ]; then
707 log ERROR "Not enough arguments
"
710 local connection=${1}
713 if ! isoneof mode ${IPSEC_VALID_MODES}; then
714 log ERROR "Mode
'${mode}' is invalid
"
718 if ! ipsec_connection_write_config_key "${connection}" "MODE
" ${mode}; then
719 log ERROR "Could not
write configuration settings
"
726 # Set the local address
727 ipsec_connection_local_address() {
728 if [ ! $# -eq 2 ]; then
729 log ERROR "Not enough arguments
"
732 local connection=${1}
733 local local_address=${2}
735 if ! ipsec_connection_check_peer ${local_address}; then
736 log ERROR "Local address
'${local_address}' is invalid
"
740 if ! ipsec_connection_write_config_key "${connection}" "LOCAL_ADDRESS
" ${local_address}; then
741 log ERROR "Could not
write configuration settings
"
748 # Set the peer to connect to
749 ipsec_connection_peer() {
750 if [ ! $# -eq 2 ]; then
751 log ERROR "Not enough arguments
"
754 local connection=${1}
757 if ! ipsec_connection_check_peer ${peer}; then
758 log ERROR "Peer
'${peer}' is invalid
"
762 if ! ipsec_connection_write_config_key "${connection}" "PEER
" ${peer}; then
763 log ERROR "Could not
write configuration settings
"
770 #Set the local or remote id
771 ipsec_connection_id() {
772 if [ ! $# -eq 3 ]; then
773 log ERROR "Not enough arguments
"
776 local connection=${1}
780 if ! ipsec_connection_check_id ${id}; then
781 log ERROR "Id
'${id}' is invalid
"
785 if ! ipsec_connection_write_config_key "${connection}" "${type}_ID" ${id}; then
786 log ERROR
"Could not write configuration settings"
793 # Set the local or remote prefix
794 ipsec_connection_prefix
() {
795 if [ ! $# -ge 3 ]; then
796 log ERROR
"Not enough arguments"
799 local connection
=${1}
803 local _prefix
="${type}_PREFIX"
805 if ! ipsec_connection_read_config
"${connection}" "${_prefix}"; then
809 # Remove duplicated entries to proceed the list safely
810 assign
"${_prefix}" "$(list_unique ${!_prefix} )"
813 local prefixes_removed
816 while [ $# -gt 0 ]; do
821 list_append prefixes_added
"${arg:1}"
824 list_append prefixes_removed
"${arg:1}"
827 list_append prefixes_set
"${arg}"
830 error
"Invalid argument: ${arg}"
837 # Check if the user is trying a mixed operation
838 if ! list_is_empty prefixes_set
&& (! list_is_empty prefixes_added ||
! list_is_empty prefixes_removed
); then
839 error
"You cannot reset the prefix list and add or remove prefixes at the same time"
843 # Set new prefix list
844 if ! list_is_empty prefixes_set
; then
845 # Check if all prefixes are valid
847 for prefix
in ${prefixes_set}; do
848 if ! ip_net_is_valid
${prefix}; then
849 error
"Unsupported prefix: ${prefix}"
854 assign
"${_prefix}" "${prefixes_set}"
856 # Perform incremental updates
860 # Perform all removals
861 for prefix
in ${prefixes_removed}; do
862 if ! list_remove
"${_prefix}" ${prefix}; then
863 warning
"${prefix} was not on the list and could not be removed"
868 for prefix
in ${prefixes_added}; do
869 if ip_net_is_valid
${prefix}; then
870 if ! list_append_unique
"${_prefix}" ${prefix}; then
871 warning
"${prefix} is already on the prefix list"
874 warning
"${prefix} is not a valid IP network and could not be added"
879 # Check if the list contain at least one valid prefix
880 if list_is_empty
${_prefix}; then
881 error
"Cannot save an empty prefix list"
886 if ! ipsec_connection_write_config_key
"${connection}" "${_prefix}" ${!_prefix}; then
887 log ERROR "Could not
write configuration settings
"
893 # Set the pools to use
894 ipsec_connection_pool() {
895 if [ ! $# -ge 2 ]; then
896 log ERROR "Not enough arguments
"
899 local connection=${1}
903 if ! ipsec_connection_read_config "${connection}" "POOLS
"; then
907 # Remove duplicated entries to proceed the list safely
908 assign "POOLS
" "$
(list_unique
${POOLS})"
914 while [ $# -gt 0 ]; do
919 list_append pools_added "${arg:1}"
922 list_append pools_removed "${arg:1}"
925 list_append pools_set "${arg}"
928 error "Invalid argument
: ${arg}"
935 # Check if the user is trying a mixed operation
936 if ! list_is_empty pools_set && (! list_is_empty pools_added || ! list_is_empty pools_removed); then
937 error "You cannot
reset the pools list and add or remove pools
at the same
time"
942 if ! list_is_empty pools_set; then
943 # Check if all pools are valid
945 for pool in ${pools_set}; do
946 if ! ipsec_pool_exists ${pool} || ! ipsec_pool_check_config ${pool}; then
947 error "Pool
${pool} is not valid
"
952 assign "POOLS
" "${pools_set}"
954 # Perform incremental updates
958 # Perform all removals
959 for pool in ${pools_removed}; do
960 if ! list_remove "POOLS
" ${pool}; then
961 warning "${pool} was not on the list and could not be removed
"
966 for pool in ${pools_added}; do
967 if ! ipsec_pool_exists ${pool} && ! ipsec_pool_check_config ${pool}; then
968 if ! list_append_unique "POOLS
" ${pool}; then
969 warning "${pool} is already on the prefix list
"
972 warning "${pool} is not a valid pool
"
977 # Check if the list contain at least one valid pool
978 if list_is_empty POOLS; then
979 error "Cannot save an empty pool list
"
984 if ! ipsec_connection_write_config_key "${connection}" "POOLS
" ${POOLS}; then
985 log ERROR "Could not
write configuration settings
"
991 # Handle the cli after remote
992 ipsec_connection_remote() {
993 if [ ! $# -ge 2 ]; then
994 log ERROR "Not enough arguments
"
998 local connection=${1}
1004 ipsec_connection_id "${connection}" "REMOTE
" $@
1008 ipsec_connection_prefix "${connection}" "REMOTE
" $@
1011 log ERROR "Unrecognized argument
: ${cmd}"
1012 return ${EXIT_ERROR}
1019 # Set the inactivity timeout
1020 ipsec_connection_inactivity_timeout() {
1021 if [ ! $# -ge 2 ]; then
1022 log ERROR "Not enough arguments
"
1023 return ${EXIT_ERROR}
1026 local connection=${1}
1030 if ! isinteger value; then
1031 value=$(parse_time $@)
1032 if [ ! $? -eq 0 ]; then
1033 log ERROR "Parsing the passed
time was not sucessful please check the passed values.
"
1034 return ${EXIT_ERROR}
1038 if [ ${value} -le 0 ]; then
1039 log ERROR "The passed
time value must be
in the
sum greater zero seconds.
"
1040 return ${EXIT_ERROR}
1043 if ! ipsec_connection_write_config_key "${connection}" "INACTIVITY_TIMEOUT
" ${value}; then
1044 log ERROR "Could not
write configuration settings
"
1045 return ${EXIT_ERROR}
1051 # Set the default start action
1052 ipsec_connection_start_action() {
1053 if [ ! $# -eq 2 ]; then
1054 log ERROR "Not enough arguments
"
1055 return ${EXIT_ERROR}
1057 local connection=${1}
1060 if ! isoneof action "on-demand
" "always-on
"; then
1061 log ERROR "Start action
'${action}' is invalid
"
1062 return ${EXIT_ERROR}
1065 if ! ipsec_connection_write_config_key "${connection}" "START_ACTION
" ${action}; then
1066 log ERROR "Could not
write configuration settings
"
1067 return ${EXIT_ERROR}
1071 # Set the security policy to use
1072 ipsec_connection_security_policy() {
1073 if [ ! $# -eq 2 ]; then
1074 log ERROR "Not enough arguments
"
1075 return ${EXIT_ERROR}
1077 local connection=${1}
1078 local security_policy=${2}
1080 if ! vpn_security_policy_exists ${security_policy}; then
1081 log ERROR "No such vpn security policy
'${security_policy}'"
1082 return ${EXIT_ERROR}
1085 if ! ipsec_connection_write_config_key "${connection}" "SECURITY_POLICY
" ${security_policy}; then
1086 log ERROR "Could not
write configuration settings
"
1087 return ${EXIT_ERROR}
1091 # Check if a id is valid
1092 ipsec_connection_check_id() {
1096 if [[ ${id} =~ ^@[[:alnum:]]+$ ]] || ip_is_valid ${id}; then
1099 return ${EXIT_FALSE}
1103 # Checks if a peer is valid
1104 ipsec_connection_check_peer() {
1108 # TODO Accept also FQDNs
1109 if ip_is_valid ${peer}; then
1112 return ${EXIT_FALSE}
1116 # This function checks if a VPN IPsec connection name is valid
1117 # Allowed are only A-Za-z0-9
1118 ipsec_connection_check_name() {
1121 local connection=${1}
1123 [[ "${connection}" =~ [^[:alnum:]$] ]]
1126 # Function that creates one VPN IPsec connection
1127 ipsec_connection_new() {
1128 if [ $# -gt 1 ]; then
1129 error "Too many arguments
"
1130 return ${EXIT_ERROR}
1133 local connection="${1}"
1134 if ! isset connection; then
1135 error "Please provide a connection name
"
1136 return ${EXIT_ERROR}
1139 # Check for duplicates
1140 if ipsec_connection_exists "${connection}"; then
1141 error "The VPN IPsec connection
${connection} already exists
"
1142 return ${EXIT_ERROR}
1145 # Check if the name of the connection is valid
1146 if ipsec_connection_check_name "${connection}"; then
1147 error "'${connection}' contains illegal characters
"
1148 return ${EXIT_ERROR}
1151 log DEBUG "Creating VPN IPsec connection
${connection}"
1153 if ! mkdir -p "${NETWORK_IPSEC_CONNS_DIR}/${connection}"; then
1154 log ERROR "Could not create config directory
for ${connection}"
1155 return ${EXIT_ERROR}
1158 local ${IPSEC_CONNECTION_CONFIG_SETTINGS}
1160 AUTH_MODE=${IPSEC_DEFAULT_AUTH_MODE}
1161 DPD_ACTION=${IPSEC_DEFAULT_DPD_ACTION}
1162 DPD_DELAY=${IPSEC_DEFAULT_DPD_DELAY}
1163 DPD_TIMEOUT=${IPSEC_DEFAULT_DPD_TIMEOUT}
1164 ENABLED=${IPSEC_DEFAULT_ENABLED}
1165 MODE=${IPSEC_DEFAULT_MODE}
1166 START_ACTION=${IPSEC_DEFAULT_START_ACTION}
1168 INACTIVITY_TIMEOUT=${IPSEC_DEFAULT_INACTIVITY_TIMEOUT}
1169 SECURITY_POLICY=${IPSEC_DEFAULT_SECURITY_POLICY}
1171 if ! ipsec_connection_write_config "${connection}"; then
1172 log ERROR "Could not
write new config
file"
1173 return ${EXIT_ERROR}
1176 # Configure strongswan autostart
1177 ipsec_strongswan_autostart
1180 # Function that deletes based on the passed parameters one ore more vpn security policies
1181 ipsec_connection_destroy() {
1183 for connection in $@; do
1184 if ! ipsec_connection_exists "${connection}"; then
1185 log ERROR "The VPN IPsec connection
${connection} does not exist.
"
1189 log DEBUG "Deleting VPN IPsec connection
${connection}"
1191 # Delete strongswan configuration file
1192 file_delete "${NETWORK_IPSEC_SWANCTL_CONNECTIONS_DIR}/${connection}.conf
"
1194 if ! rm -rf "${NETWORK_IPSEC_CONNS_DIR}/${connection}"; then
1195 log ERROR "Deleting the VPN IPsec connection
${connection} was not sucessful
"
1196 return ${EXIT_ERROR}
1202 # List all ipsec connections
1203 ipsec_list_connections() {
1205 for connection in ${NETWORK_IPSEC_CONNS_DIR}/*; do
1206 [ -d ${connection} ] || continue
1207 basename ${connection}
1211 ipsec_connection_to_strongswan() {
1212 local connection="${1}"
1213 log DEBUG "Generating IPsec configuration
for ${connection}"
1215 # Read the config settings
1216 local ${IPSEC_CONNECTION_CONFIG_SETTINGS}
1217 if ! ipsec_connection_read_config "${connection}"; then
1218 error "Could not
read the connection
${connection}"
1219 return ${EXIT_ERROR}
1222 local path="${NETWORK_IPSEC_SWANCTL_CONNECTIONS_DIR}/${connection}.conf
"
1225 # Write the connection section
1226 _ipsec_connection_to_strongswan_connection "${connection}"
1228 # Write the secrets section
1229 _ipsec_connection_to_strongswan_secrets "${connection}"
1234 _ipsec_connection_to_strongswan_connection() {
1235 local connection="${1}"
1237 # Read the security policy
1238 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
1239 if ! vpn_security_policies_read_config "${SECURITY_POLICY}"; then
1240 return ${EXIT_ERROR}
1245 if isset DPD_DELAY && isinteger DPD_DELAY && [ ${DPD_DELAY} -gt 0 ]; then
1249 # Write configuration header
1250 config_header "strongSwan configuration
for ${connection}"
1252 print_indent 0 "connections
{"
1253 print_indent 1 "${connection} {"
1256 print_indent 2 "# IKE Version"
1257 case "${KEY_EXCHANGE^^}" in
1259 print_indent
2 "version = 1"
1262 # Fall back to IKEv2 for any random values
1264 print_indent
2 "version = 2"
1269 # Always only keep one connection open at a time
1270 print_indent
2 "# Unique IDs"
1271 print_indent
2 "unique = replace"
1275 print_indent
2 "# Local Address"
1276 if isset LOCAL_ADDRESS
; then
1277 print_indent
2 "local_addrs = ${LOCAL_ADDRESS}"
1279 print_indent
2 "local_addrs = %any"
1284 print_indent
2 "# Remote Address"
1286 print_indent
2 "remote_addrs = ${PEER}"
1288 print_indent
2 "remote_addrs = %any"
1293 print_indent
2 "# IKE Proposals"
1294 print_indent
2 "proposals = $(vpn_security_policies_make_ike_proposal ${SECURITY_POLICY})"
1298 if enabled dpd
; then
1299 print_indent
2 "# Dead Peer Detection"
1300 print_indent
2 "dpd_delay = ${DPD_DELAY}"
1302 if isset DPD_TIMEOUT
; then
1303 print_indent
2 "dpd_timeout = ${DPD_TIMEOUT}"
1310 print_indent
2 "# Fragmentation"
1311 print_indent
2 "fragmentation = yes"
1315 if isset POOLS
; then
1316 print_indent
2 "# Pools"
1317 print_indent
2 "pools = $(list_join POOLS ", ")"
1322 print_indent
2 "local {"
1325 if isset LOCAL_ID
; then
1326 print_indent
3 "id = ${LOCAL_ID}"
1330 case "${AUTH_MODE}" in
1332 print_indent
3 "auth = psk"
1340 print_indent
2 "remote {"
1343 if isset REMOTE_ID
; then
1344 print_indent
3 "id = ${REMOTE_ID}"
1348 case "${AUTH_MODE}" in
1350 print_indent
3 "auth = psk"
1359 print_indent
2 "children {"
1360 print_indent
3 "${connection} {"
1362 print_indent
4 "# ESP Proposals"
1363 print_indent
4 "esp_proposals = $(vpn_security_policies_make_esp_proposal ${SECURITY_POLICY})"
1370 print_indent
4 "local_ts = dynamic[gre]"
1371 print_indent
4 "remote_ts = dynamic[gre]"
1375 if isset LOCAL_PREFIX
; then
1376 print_indent
4 "local_ts = $(list_join LOCAL_PREFIX ,)"
1378 print_indent
4 "local_ts = dynamic"
1382 if isset REMOTE_PREFIX
; then
1383 print_indent
4 "remote_ts = $(list_join REMOTE_PREFIX ,)"
1385 print_indent
4 "remote_ts = dynamic"
1394 print_indent
4 "# Netfilter Marks"
1395 print_indent
4 "mark_in = %unique"
1396 print_indent
4 "mark_out = %unique"
1401 # Dead Peer Detection
1402 if enabled dpd
; then
1403 print_indent
4 "# Dead Peer Detection"
1404 print_indent
4 "dpd_action = ${DPD_ACTION}"
1409 if isset LIFETIME
; then
1410 print_indent
4 "# Rekey Time"
1411 print_indent
4 "rekey_time = ${LIFETIME}"
1416 print_indent
4 "updown = ${NETWORK_HELPERS_DIR}/ipsec-updown"
1420 print_indent
4 "# Mode"
1423 print_indent
4 "mode = transport"
1426 print_indent
4 "mode = tunnel"
1432 print_indent
4 "# Compression"
1433 if enabled COMPRESSION
; then
1434 print_indent
4 "ipcomp = yes"
1436 print_indent
4 "ipcomp = no"
1440 # Inactivity Timeout
1441 if isset INACTIVITY_TIMEOUT
; then
1442 print_indent
4 "# Inactivity Timeout"
1443 print_indent
4 "inactivity = ${INACTIVITY_TIMEOUT}"
1448 print_indent
4 "# Start Action"
1449 case "${START_ACTION}" in
1451 print_indent
4 "start_action = trap"
1452 print_indent
4 "close_action = trap"
1455 print_indent
4 "start_action = none"
1456 print_indent
4 "close_action = none"
1459 print_indent
4 "start_action = start"
1460 print_indent
4 "close_action = start"
1474 _ipsec_connection_to_strongswan_secrets
() {
1475 local connection
="${1}"
1477 print_indent
0 "secrets {"
1479 case "${AUTH_MODE}" in
1481 print_indent
1 "ike {"
1484 print_indent
2 "secret = ${PSK}"
1487 if isset REMOTE_ID
; then
1488 print_indent
2 "id = ${REMOTE_ID}"
1498 # This function writes all values to a via ${pool} specificated VPN IPsec pool configuration file
1499 ipsec_pool_write_config
() {
1504 if ! ipsec_pool_exists
"${pool}"; then
1505 log ERROR
"No such VPN IPsec pool: ${pool}"
1506 return ${EXIT_ERROR}
1509 local path
="${NETWORK_IPSEC_POOLS_DIR}/${pool}/settings"
1511 if ! settings_write
"${path}" ${IPSEC_POOL_CONFIG_SETTINGS}; then
1512 log ERROR
"Could not write configuration settings for VPN IPsec pool ${pool}"
1513 return ${EXIT_ERROR}
1516 if ! ipsec_pool_reload
${pool}; then
1517 log WARNING
"Could not reload IPsec pool ${pool}"
1520 # When we get here the writing of the config file was successful
1524 # This funtion writes the value for one key to a via ${connection} specificated
1525 # VPN IPsec pool configuration file
1526 ipsec_pool_write_config_key
() {
1535 if ! ipsec_pool_exists
"${pool}"; then
1536 log ERROR
"No such VPN IPsec pool: ${pool}"
1537 return ${EXIT_ERROR}
1540 log DEBUG
"Set '${key}' to new value '${value}' in VPN IPsec pool '${pool}'"
1542 local ${IPSEC_POOL_CONFIG_SETTINGS}
1544 # Read the config settings
1545 if ! ipsec_pool_read_config
"${pool}"; then
1546 return ${EXIT_ERROR}
1549 # Set the key to a new value
1550 assign
"${key}" "${value}"
1552 if ! ipsec_pool_write_config
"${pool}"; then
1553 return ${EXIT_ERROR}
1559 # Reads one or more keys out of a settings file or all if no key is provided.
1560 ipsec_pool_read_config
() {
1566 if ! ipsec_pool_exists
"${pool}"; then
1567 log ERROR
"No such VPN IPsec pool : ${pool}"
1568 return ${EXIT_ERROR}
1572 if [ $# -eq 0 ] && [ -n "${IPSEC_POOL_CONFIG_SETTINGS}" ]; then
1573 list_append args
${IPSEC_POOL_CONFIG_SETTINGS}
1578 local path
="${NETWORK_IPSEC_POOLS_DIR}/${pool}/settings"
1580 if ! settings_read
"${path}" ${args}; then
1581 log ERROR
"Could not read settings for VPN IPsec pool ${pool}"
1582 return ${EXIT_ERROR}
1586 # This function checks if a vpn IPsec pool exists
1587 # Returns True when yes and false when not
1588 ipsec_pool_exists
() {
1593 local path
="${NETWORK_IPSEC_POOLS_DIR}/${pool}"
1595 [ -d "${path}" ] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
1598 # This function checks if a VPN IPsec pool name is valid
1599 # Allowed are only A-Za-z0-9
1600 ipsec_pool_check_name() {
1605 # These are special words in strongswan
1606 if isoneof pool dhcp radius; then
1607 return ${EXIT_ERROR}
1610 [[ "${pool}" =~ [^[:alnum:]$] ]]
1614 if [ $# -gt 1 ]; then
1615 error "Too many arguments
"
1616 return ${EXIT_ERROR}
1620 if ! isset pool; then
1621 error "Please provide a pool name
"
1622 return ${EXIT_ERROR}
1625 # Check for duplicates
1626 if ipsec_pool_exists "${pool}"; then
1627 error "The VPN IPsec pool
${pool} already exists
"
1628 return ${EXIT_ERROR}
1631 # Check if the name of the connection is valid
1632 if ipsec_pool_check_name "${pool}"; then
1633 error "'${pool}' contains illegal characters
"
1634 return ${EXIT_ERROR}
1637 log DEBUG "Creating VPN IPsec pool
${pool}"
1639 if ! mkdir -p "${NETWORK_IPSEC_POOLS_DIR}/${pool}"; then
1640 log ERROR "Could not create config directory
for ${pool}"
1641 return ${EXIT_ERROR}
1644 local ${IPSEC_POOL_CONFIG_SETTINGS}
1646 if ! ipsec_pool_write_config "${pool}"; then
1647 log ERROR "Could not
write new config
file"
1648 return ${EXIT_ERROR}
1652 # Function that deletes based on the passed parameters
1653 # one ore more vpn ipsec pools
1654 ipsec_pool_destroy() {
1657 if ! ipsec_pool_exists "${pool}"; then
1658 log ERROR "The VPN IPsec pool
${pool} does not exist.
"
1662 log DEBUG "Deleting VPN IPsec pool
${pool}"
1664 if ! rm -rf "${NETWORK_IPSEC_POOLS_DIR}/${pool}"; then
1665 log ERROR "Deleting the VPN IPsec pool
${pool} was not sucessful
"
1666 return ${EXIT_ERROR}
1671 ipsec_pool_set_type() {
1677 local type=$(ip_detect_protocol ${ip})
1679 if ! isset type; then
1680 error "Cannot detect IP protocol of
${ip}"
1681 return ${EXIT_ERROR}
1683 log DEBUG "IP protocol of
${ip} is
${type}"
1684 if ! ipsec_pool_write_config_key "${pool}" "TYPE
" ${type}; then
1685 log ERROR "Could not
write configuration settings
"
1686 return ${EXIT_ERROR}
1691 ipsec_pool_network() {
1692 if [ ! $# -eq 2 ]; then
1693 log ERROR "Not enough arguments
"
1694 return ${EXIT_ERROR}
1700 if ! ipsec_pool_read_config ${pool} "TYPE
"; then
1701 error "Failed to
read configuration settings
for pool
'${pool}'"
1702 return ${EXIT_ERROR}
1705 if ! isset TYPE; then
1706 if ! ip_net_is_valid ${network}; then
1707 log ERROR "Network
'${network}' is invalid
"
1708 return ${EXIT_ERROR}
1711 if ! ipsec_pool_set_type ${pool} ${network}; then
1712 log ERROR "Could not
set type for IPsec pool
${pool}"
1713 return ${EXIT_ERROR}
1716 if ! ${TYPE}_net_is_valid ${network}; then
1717 log ERROR "Network
'${network}' is invalid
"
1718 return ${EXIT_ERROR}
1722 if ! ipsec_pool_write_config_key "${pool}" "NETWORK
" ${network}; then
1723 log ERROR "Could not
write configuration settings
"
1724 return ${EXIT_ERROR}
1728 ipsec_pool_dns_server() {
1729 if [ ! $# -eq 2 ]; then
1730 log ERROR "Not enough arguments
"
1731 return ${EXIT_ERROR}
1734 local dns_server=${2}
1737 if ! ipsec_pool_read_config ${pool} "TYPE
"; then
1738 error "Failed to
read configuration settings
for pool
'${pool}'"
1739 return ${EXIT_ERROR}
1742 if ! isset TYPE; then
1743 if ! ip_is_valid ${dns_server}; then
1744 log ERROR "DNS server
'${dns_server}' is invalid
"
1745 return ${EXIT_ERROR}
1748 if ! ipsec_pool_set_type ${pool} ${dns_server}; then
1749 log ERROR "Could not
set type for IPsec pool
${pool}"
1750 return ${EXIT_ERROR}
1753 if ! ${TYPE}_is_valid ${dns_server}; then
1754 log ERROR "DNS server
'${dns_server}' is invalid
"
1755 return ${EXIT_ERROR}
1759 if ! ipsec_pool_write_config_key "${pool}" "DNS_SERVER
" ${dns_server}; then
1760 log ERROR "Could not
write configuration settings
"
1761 return ${EXIT_ERROR}
1765 ipsec_pool_check_config() {
1769 local ${IPSEC_POOL_CONFIG_SETTINGS}
1770 if ! ipsec_pool_read_config "${pool}"; then
1771 log ERROR "Could not
read configuration settings
"
1772 return ${EXIT_ERROR}
1775 if ! isset NETWORK; then
1776 log ERROR "Network
for IPSec pool
${pool} is not
set"
1777 return ${EXIT_ERROR}
1780 if ! isset TYPE; then
1781 TYPE=$(ip_detect_protocol ${NETWORK})
1782 log DEBUG "IP protocol of
${NETWORK} is
${TYPE}"
1783 if ! isset TYPE; then
1784 error "Cannot detect IP protocol of
${NETWORK}"
1785 return ${EXIT_ERROR}
1787 if ! ipsec_pool_write_config_key "${pool}" "TYPE
" ${TYPE}; then
1788 log ERROR "Could not
write configuration settings
"
1789 return ${EXIT_ERROR}
1793 if ! ${TYPE}_net_is_valid ${NETWORK}; then
1794 log ERROR "NETWORK
'${NETWORK}' is invalid
"
1795 return ${EXIT_ERROR}
1798 if isset DNS_SERVER && ! ${TYPE}_is_valid ${DNS_SERVER}; then
1799 log ERROR "DNS server
'${DNS_SERVER}' is invalid
"
1800 return ${EXIT_ERROR}
1807 ipsec_pool_reload() {
1810 if ! ipsec_pool_to_strongswan ${pool}; then
1811 log ERROR "Could not generate strongswan config
for ${pool}"
1812 return ${EXIT_ERROR}
1815 ipsec_strongswan_load
1818 ipsec_pool_to_strongswan() {
1821 log DEBUG "Generating IPsec pool config
for ${pool}"
1823 local ${IPSEC_POOL_CONFIG_SETTINGS}
1824 if ! ipsec_pool_read_config "${pool}"; then
1825 return ${EXIT_ERROR}
1828 if isset NETWORK && ! ipsec_pool_check_config "${pool}"; then
1829 log ERROR "Configuration of
${pool} seems to be invalid
"
1830 return ${EXIT_ERROR}
1833 local path="${NETWORK_IPSEC_SWANCTL_POOLS_DIR}/${pool}.conf
"
1836 config_header "strongSwan pool configuration
"
1838 if isset NETWORK; then
1839 print_indent 0 "pools
{"
1841 print_indent 1 "${pool} {"
1842 print_indent 2 "addrs
= ${NETWORK}"
1844 if isset DNS_SERVER; then
1845 print_indent 2 "dns
= ${DNS_SERVER}"