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}"
1214 # Read the config settings
1215 local ${IPSEC_CONNECTION_CONFIG_SETTINGS}
1216 if ! ipsec_connection_read_config "${connection}"; then
1217 error "Could not
read the connection
${connection}"
1218 return ${EXIT_ERROR}
1221 local path="${NETWORK_IPSEC_SWANCTL_CONNECTIONS_DIR}/${connection}.conf
"
1224 # Write the connection section
1225 _ipsec_connection_to_strongswan_connection "${connection}"
1227 # Write the secrets section
1228 _ipsec_connection_to_strongswan_secrets "${connection}"
1233 _ipsec_connection_to_strongswan_connection() {
1234 local connection="${1}"
1236 # Read the security policy
1237 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
1238 if ! vpn_security_policies_read_config "${SECURITY_POLICY}"; then
1239 return ${EXIT_ERROR}
1244 if isset DPD_DELAY && isinteger DPD_DELAY && [ ${DPD_DELAY} -gt 0 ]; then
1248 # Write configuration header
1249 config_header "strongSwan configuration
for ${connection}"
1251 print_indent 0 "connections
{"
1252 print_indent 1 "${connection} {"
1255 print_indent 2 "# IKE Version"
1256 case "${KEY_EXCHANGE^^}" in
1258 print_indent
2 "version = 1"
1261 # Fall back to IKEv2 for any random values
1263 print_indent
2 "version = 2"
1268 # Always only keep one connection open at a time
1269 print_indent
2 "# Unique IDs"
1270 print_indent
2 "unique = replace"
1274 print_indent
2 "# Local Address"
1275 if isset LOCAL_ADDRESS
; then
1276 print_indent
2 "local_addrs = ${LOCAL_ADDRESS}"
1278 print_indent
2 "local_addrs = %any"
1283 print_indent
2 "# Remote Address"
1285 print_indent
2 "remote_addrs = ${PEER}"
1287 print_indent
2 "remote_addrs = %any"
1292 print_indent
2 "# IKE Proposals"
1293 print_indent
2 "proposals = $(vpn_security_policies_make_ike_proposal ${SECURITY_POLICY})"
1297 if enabled dpd
; then
1298 print_indent
2 "# Dead Peer Detection"
1299 print_indent
2 "dpd_delay = ${DPD_DELAY}"
1301 if isset DPD_TIMEOUT
; then
1302 print_indent
2 "dpd_timeout = ${DPD_TIMEOUT}"
1309 print_indent
2 "# Fragmentation"
1310 print_indent
2 "fragmentation = yes"
1314 if isset POOLS
; then
1315 print_indent
2 "# Pools"
1316 print_indent
2 "pools = $(list_join POOLS ", ")"
1321 print_indent
2 "local {"
1324 if isset LOCAL_ID
; then
1325 print_indent
3 "id = ${LOCAL_ID}"
1329 case "${AUTH_MODE}" in
1331 print_indent
3 "auth = psk"
1339 print_indent
2 "remote {"
1342 if isset REMOTE_ID
; then
1343 print_indent
3 "id = ${REMOTE_ID}"
1347 case "${AUTH_MODE}" in
1349 print_indent
3 "auth = psk"
1358 print_indent
2 "children {"
1359 print_indent
3 "${connection} {"
1361 print_indent
4 "# ESP Proposals"
1362 print_indent
4 "esp_proposals = $(vpn_security_policies_make_esp_proposal ${SECURITY_POLICY})"
1369 print_indent
4 "local_ts = dynamic[gre]"
1370 print_indent
4 "remote_ts = dynamic[gre]"
1374 if isset LOCAL_PREFIX
; then
1375 print_indent
4 "local_ts = $(list_join LOCAL_PREFIX ,)"
1377 print_indent
4 "local_ts = dynamic"
1381 if isset REMOTE_PREFIX
; then
1382 print_indent
4 "remote_ts = $(list_join REMOTE_PREFIX ,)"
1384 print_indent
4 "remote_ts = dynamic"
1393 print_indent
4 "# Netfilter Marks"
1394 print_indent
4 "mark_in = %unique"
1395 print_indent
4 "mark_out = %unique"
1400 # Dead Peer Detection
1401 if enabled dpd
; then
1402 print_indent
4 "# Dead Peer Detection"
1403 print_indent
4 "dpd_action = ${DPD_ACTION}"
1408 if isset LIFETIME
; then
1409 print_indent
4 "# Rekey Time"
1410 print_indent
4 "rekey_time = ${LIFETIME}"
1415 print_indent
4 "updown = ${NETWORK_HELPERS_DIR}/ipsec-updown"
1419 print_indent
4 "# Mode"
1422 print_indent
4 "mode = transport"
1425 print_indent
4 "mode = tunnel"
1431 print_indent
4 "# Compression"
1432 if enabled COMPRESSION
; then
1433 print_indent
4 "ipcomp = yes"
1435 print_indent
4 "ipcomp = no"
1439 # Inactivity Timeout
1440 if isset INACTIVITY_TIMEOUT
; then
1441 print_indent
4 "# Inactivity Timeout"
1442 print_indent
4 "inactivity = ${INACTIVITY_TIMEOUT}"
1447 print_indent
4 "# Start Action"
1448 case "${START_ACTION}" in
1450 print_indent
4 "start_action = trap"
1451 print_indent
4 "close_action = trap"
1454 print_indent
4 "start_action = none"
1455 print_indent
4 "close_action = none"
1458 print_indent
4 "start_action = start"
1459 print_indent
4 "close_action = start"
1473 _ipsec_connection_to_strongswan_secrets
() {
1474 local connection
="${1}"
1476 print_indent
0 "secrets {"
1478 case "${AUTH_MODE}" in
1480 print_indent
1 "ike {"
1483 print_indent
2 "secret = ${PSK}"
1486 if isset REMOTE_ID
; then
1487 print_indent
2 "id = ${REMOTE_ID}"
1497 # This function writes all values to a via ${pool} specificated VPN IPsec pool configuration file
1498 ipsec_pool_write_config
() {
1503 if ! ipsec_pool_exists
"${pool}"; then
1504 log ERROR
"No such VPN IPsec pool: ${pool}"
1505 return ${EXIT_ERROR}
1508 local path
="${NETWORK_IPSEC_POOLS_DIR}/${pool}/settings"
1510 if ! settings_write
"${path}" ${IPSEC_POOL_CONFIG_SETTINGS}; then
1511 log ERROR
"Could not write configuration settings for VPN IPsec pool ${pool}"
1512 return ${EXIT_ERROR}
1515 if ! ipsec_pool_reload
${pool}; then
1516 log WARNING
"Could not reload IPsec pool ${pool}"
1519 # When we get here the writing of the config file was successful
1523 # This funtion writes the value for one key to a via ${connection} specificated
1524 # VPN IPsec pool configuration file
1525 ipsec_pool_write_config_key
() {
1534 if ! ipsec_pool_exists
"${pool}"; then
1535 log ERROR
"No such VPN IPsec pool: ${pool}"
1536 return ${EXIT_ERROR}
1539 log DEBUG
"Set '${key}' to new value '${value}' in VPN IPsec pool '${pool}'"
1541 local ${IPSEC_POOL_CONFIG_SETTINGS}
1543 # Read the config settings
1544 if ! ipsec_pool_read_config
"${pool}"; then
1545 return ${EXIT_ERROR}
1548 # Set the key to a new value
1549 assign
"${key}" "${value}"
1551 if ! ipsec_pool_write_config
"${pool}"; then
1552 return ${EXIT_ERROR}
1558 # Reads one or more keys out of a settings file or all if no key is provided.
1559 ipsec_pool_read_config
() {
1565 if ! ipsec_pool_exists
"${pool}"; then
1566 log ERROR
"No such VPN IPsec pool : ${pool}"
1567 return ${EXIT_ERROR}
1571 if [ $# -eq 0 ] && [ -n "${IPSEC_POOL_CONFIG_SETTINGS}" ]; then
1572 list_append args
${IPSEC_POOL_CONFIG_SETTINGS}
1577 local path
="${NETWORK_IPSEC_POOLS_DIR}/${pool}/settings"
1579 if ! settings_read
"${path}" ${args}; then
1580 log ERROR
"Could not read settings for VPN IPsec pool ${pool}"
1581 return ${EXIT_ERROR}
1585 # This function checks if a vpn IPsec pool exists
1586 # Returns True when yes and false when not
1587 ipsec_pool_exists
() {
1592 local path
="${NETWORK_IPSEC_POOLS_DIR}/${pool}"
1594 [ -d "${path}" ] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
1597 # This function checks if a VPN IPsec pool name is valid
1598 # Allowed are only A-Za-z0-9
1599 ipsec_pool_check_name() {
1604 # These are special words in strongswan
1605 if isoneof pool dhcp radius; then
1606 return ${EXIT_ERROR}
1609 [[ "${pool}" =~ [^[:alnum:]$] ]]
1613 if [ $# -gt 1 ]; then
1614 error "Too many arguments
"
1615 return ${EXIT_ERROR}
1619 if ! isset pool; then
1620 error "Please provide a pool name
"
1621 return ${EXIT_ERROR}
1624 # Check for duplicates
1625 if ipsec_pool_exists "${pool}"; then
1626 error "The VPN IPsec pool
${pool} already exists
"
1627 return ${EXIT_ERROR}
1630 # Check if the name of the connection is valid
1631 if ipsec_pool_check_name "${pool}"; then
1632 error "'${pool}' contains illegal characters
"
1633 return ${EXIT_ERROR}
1636 log DEBUG "Creating VPN IPsec pool
${pool}"
1638 if ! mkdir -p "${NETWORK_IPSEC_POOLS_DIR}/${pool}"; then
1639 log ERROR "Could not create config directory
for ${pool}"
1640 return ${EXIT_ERROR}
1643 local ${IPSEC_POOL_CONFIG_SETTINGS}
1645 if ! ipsec_pool_write_config "${pool}"; then
1646 log ERROR "Could not
write new config
file"
1647 return ${EXIT_ERROR}
1651 # Function that deletes based on the passed parameters
1652 # one ore more vpn ipsec pools
1653 ipsec_pool_destroy() {
1656 if ! ipsec_pool_exists "${pool}"; then
1657 log ERROR "The VPN IPsec pool
${pool} does not exist.
"
1661 log DEBUG "Deleting VPN IPsec pool
${pool}"
1663 if ! rm -rf "${NETWORK_IPSEC_POOLS_DIR}/${pool}"; then
1664 log ERROR "Deleting the VPN IPsec pool
${pool} was not sucessful
"
1665 return ${EXIT_ERROR}
1670 ipsec_pool_set_type() {
1676 local type=$(ip_detect_protocol ${ip})
1678 if ! isset type; then
1679 error "Cannot detect IP protocol of
${ip}"
1680 return ${EXIT_ERROR}
1682 log DEBUG "IP protocol of
${ip} is
${type}"
1683 if ! ipsec_pool_write_config_key "${pool}" "TYPE
" ${type}; then
1684 log ERROR "Could not
write configuration settings
"
1685 return ${EXIT_ERROR}
1690 ipsec_pool_network() {
1691 if [ ! $# -eq 2 ]; then
1692 log ERROR "Not enough arguments
"
1693 return ${EXIT_ERROR}
1699 if ! ipsec_pool_read_config ${pool} "TYPE
"; then
1700 error "Failed to
read configuration settings
for pool
'${pool}'"
1701 return ${EXIT_ERROR}
1704 if ! isset TYPE; then
1705 if ! ip_net_is_valid ${network}; then
1706 log ERROR "Network
'${network}' is invalid
"
1707 return ${EXIT_ERROR}
1710 if ! ipsec_pool_set_type ${pool} ${network}; then
1711 log ERROR "Could not
set type for IPsec pool
${pool}"
1712 return ${EXIT_ERROR}
1715 if ! ${TYPE}_net_is_valid ${network}; then
1716 log ERROR "Network
'${network}' is invalid
"
1717 return ${EXIT_ERROR}
1721 if ! ipsec_pool_write_config_key "${pool}" "NETWORK
" ${network}; then
1722 log ERROR "Could not
write configuration settings
"
1723 return ${EXIT_ERROR}
1727 ipsec_pool_dns_server() {
1728 if [ ! $# -eq 2 ]; then
1729 log ERROR "Not enough arguments
"
1730 return ${EXIT_ERROR}
1733 local dns_server=${2}
1736 if ! ipsec_pool_read_config ${pool} "TYPE
"; then
1737 error "Failed to
read configuration settings
for pool
'${pool}'"
1738 return ${EXIT_ERROR}
1741 if ! isset TYPE; then
1742 if ! ip_is_valid ${dns_server}; then
1743 log ERROR "DNS server
'${dns_server}' is invalid
"
1744 return ${EXIT_ERROR}
1747 if ! ipsec_pool_set_type ${pool} ${dns_server}; then
1748 log ERROR "Could not
set type for IPsec pool
${pool}"
1749 return ${EXIT_ERROR}
1752 if ! ${TYPE}_is_valid ${dns_server}; then
1753 log ERROR "DNS server
'${dns_server}' is invalid
"
1754 return ${EXIT_ERROR}
1758 if ! ipsec_pool_write_config_key "${pool}" "DNS_SERVER
" ${dns_server}; then
1759 log ERROR "Could not
write configuration settings
"
1760 return ${EXIT_ERROR}
1764 ipsec_pool_check_config() {
1768 local ${IPSEC_POOL_CONFIG_SETTINGS}
1769 if ! ipsec_pool_read_config "${pool}"; then
1770 log ERROR "Could not
read configuration settings
"
1771 return ${EXIT_ERROR}
1774 if ! isset NETWORK; then
1775 log ERROR "Network
for IPSec pool
${pool} is not
set"
1776 return ${EXIT_ERROR}
1779 if ! isset TYPE; then
1780 TYPE=$(ip_detect_protocol ${NETWORK})
1781 log DEBUG "IP protocol of
${NETWORK} is
${TYPE}"
1782 if ! isset TYPE; then
1783 error "Cannot detect IP protocol of
${NETWORK}"
1784 return ${EXIT_ERROR}
1786 if ! ipsec_pool_write_config_key "${pool}" "TYPE
" ${TYPE}; then
1787 log ERROR "Could not
write configuration settings
"
1788 return ${EXIT_ERROR}
1792 if ! ${TYPE}_net_is_valid ${NETWORK}; then
1793 log ERROR "NETWORK
'${NETWORK}' is invalid
"
1794 return ${EXIT_ERROR}
1797 if isset DNS_SERVER && ! ${TYPE}_is_valid ${DNS_SERVER}; then
1798 log ERROR "DNS server
'${DNS_SERVER}' is invalid
"
1799 return ${EXIT_ERROR}
1806 ipsec_pool_reload() {
1809 if ! ipsec_pool_to_strongswan ${pool}; then
1810 log ERROR "Could not generate strongswan config
for ${pool}"
1811 return ${EXIT_ERROR}
1814 ipsec_strongswan_load
1817 ipsec_pool_to_strongswan() {
1820 log DEBUG "Generating IPsec pool config
for ${pool}"
1822 local ${IPSEC_POOL_CONFIG_SETTINGS}
1823 if ! ipsec_pool_read_config "${pool}"; then
1824 return ${EXIT_ERROR}
1827 if isset NETWORK && ! ipsec_pool_check_config "${pool}"; then
1828 log ERROR "Configuration of
${pool} seems to be invalid
"
1829 return ${EXIT_ERROR}
1832 local path="${NETWORK_IPSEC_SWANCTL_POOLS_DIR}/${pool}.conf
"
1835 config_header "strongSwan pool configuration
"
1837 if isset NETWORK; then
1838 print_indent 0 "pools
{"
1840 print_indent 1 "${pool} {"
1841 print_indent 2 "addrs
= ${NETWORK}"
1843 if isset DNS_SERVER; then
1844 print_indent 2 "dns
= ${DNS_SERVER}"