]> git.ipfire.org Git - people/stevee/network.git/blame - src/functions/functions.ipsec
ipsec-connection: add color support
[people/stevee/network.git] / src / functions / functions.ipsec
CommitLineData
917a1aa0
JS
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2017 IPFire Network Development Team #
6# #
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. #
11# #
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. #
16# #
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/>. #
19# #
20###############################################################################
21
33944dfb
MT
22IPSEC_CONNECTION_CONFIG_SETTINGS="\
23 AUTH_MODE \
24 DPD_ACTION \
25 DPD_DELAY \
26 DPD_TIMEOUT \
27 INACTIVITY_TIMEOUT \
28 LOCAL_ADDRESS \
29 LOCAL_ID \
30 LOCAL_PREFIX \
31 MODE \
32 PEER \
96fdb077 33 POOLS \
33944dfb
MT
34 PSK \
35 REMOTE_ID \
36 REMOTE_PREFIX \
5601f4f5 37 SECURITY_POLICY \
eb6fa666 38 START_ACTION \
89d71d08 39 TYPE \
5601f4f5 40 ENABLED"
917a1aa0
JS
41
42# Default values
ab589039 43IPSEC_DEFAULT_AUTH_MODE="PSK"
bb9fccaf
JS
44IPSEC_DEFAULT_DPD_ACTION="restart"
45IPSEC_DEFAULT_DPD_DELAY="30"
46IPSEC_DEFAULT_DPD_TIMEOUT="120"
5601f4f5 47IPSEC_DEFAULT_ENABLED="true"
917a1aa0 48IPSEC_DEFAULT_INACTIVITY_TIMEOUT="0"
bb9fccaf 49IPSEC_DEFAULT_MODE="tunnel"
917a1aa0 50IPSEC_DEFAULT_SECURITY_POLICY="system"
bb9fccaf 51IPSEC_DEFAULT_START_ACTION="on-demand"
89d71d08 52IPSEC_DEFAULT_TYPE="net-to-net"
917a1aa0
JS
53
54IPSEC_VALID_MODES="gre-transport tunnel vti"
ab589039 55IPSEC_VALID_AUTH_MODES="PSK"
917a1aa0 56
2da98f56
MT
57cli_ipsec() {
58 local action=${1}
59 shift 1
60
61 case "${action}" in
62 connection)
2212045f 63 cli_ipsec_connection "$@"
2da98f56 64 ;;
7c623df2 65 pool)
2212045f 66 cli_ipsec_pool "$@"
7c623df2 67 ;;
2da98f56
MT
68 *)
69 error "Unrecognized argument: ${action}"
70 exit ${EXIT_ERROR}
71 ;;
72 esac
73}
74
75cli_ipsec_connection() {
76 if ipsec_connection_exists ${1}; then
77 local connection=${1}
78 local key=${2}
79 key=${key//-/_}
80 shift 2
81
82 case "${key}" in
96fdb077 83 authentication|down|disable|dpd|enable|inactivity_timeout|local|mode|peer|pool|remote|security_policy|start_action|up)
2212045f 84 ipsec_connection_${key} ${connection} "$@"
2da98f56 85 ;;
5bcadc60
JS
86 color)
87 color_cli "ipsec-connection" "${connection}" "$@"
88 ;;
c1e76e97
MT
89 show)
90 cli_ipsec_connection_show "${connection}"
91 exit $?
92 ;;
2da98f56
MT
93 *)
94 error "Unrecognized argument: ${key}"
95 exit ${EXIT_ERROR}
96 ;;
97 esac
98 else
99 local action=${1}
100 shift
101
102 case "${action}" in
103 new)
2212045f 104 ipsec_connection_new "$@"
2da98f56
MT
105 ;;
106 destroy)
2212045f 107 cli_ipsec_connection_destroy "$@"
2da98f56
MT
108 ;;
109 ""|*)
110 if [ -n "${action}" ]; then
111 error "Unrecognized argument: '${action}'"
112 fi
113 exit ${EXIT_ERROR}
114 ;;
115 esac
116 fi
117}
118
fa33d830
MT
119cli_ipsec_connection_destroy() {
120 local connection="${1}"
121
122 if ! ipsec_connection_destroy "${connection}"; then
123 return ${EXIT_ERROR}
124 fi
125
126 # Inform strongswan about the changes
127 ipsec_strongswan_load
128
129 # Configure strongswan autostart
130 ipsec_strongswan_autostart
131}
132
5bcadc60
JS
133ipsec_connection_get_color() {
134 # This function return the color of a zone
135 assert [ $# -eq 1 ]
136
137 local name=${1}
138 color_read "ipsec-connection" ${name}
139}
140
c1e76e97
MT
141cli_ipsec_connection_show() {
142 local connection="${1}"
143
144 # Read the config settings
145 local ${IPSEC_CONNECTION_CONFIG_SETTINGS}
146 if ! ipsec_connection_read_config "${connection}"; then
147 error "Could not read the connection configuration"
148 return ${EXIT_ERROR}
149 fi
150
151 cli_headline 0 "IPsec VPN Connection: ${connection}"
152 cli_space
153
5bcadc60
JS
154 cli_print_fmt1 1 "Color" "$(cli_color_bar $(ipsec_connection_get_color ${connection}))"
155 cli_space
156
c1e76e97
MT
157 # Peer
158 if isset PEER; then
159 cli_print_fmt1 1 "Peer" "${PEER}"
160 fi
161
162 # Security Policy
163 cli_print_fmt1 1 "Security Policy" "${SECURITY_POLICY-${IPSEC_DEFAULT_SECURITY_POLICY}}"
164 cli_space
165
166 cli_headline 2 "Authentication"
167 case "${AUTH_MODE^^}" in
168 PSK)
169 cli_print_fmt1 2 "Mode" "Pre-Shared-Key"
170
171 if isset PSK; then
172 cli_print_fmt1 2 "Pre-Shared-Key" "****"
173 else
174 cli_print_fmt1 2 "Pre-Shared-Key" "- is not set -"
175 fi
176 ;;
177 X509)
178 : # TODO
179 ;;
180 esac
181 cli_space
182
183 local i
184 for i in LOCAL REMOTE; do
185 case "${i}" in
186 LOCAL)
187 cli_headline 2 "Local"
188 ;;
189 REMOTE)
190 cli_headline 2 "Remote"
191 ;;
192 esac
193
194 local id_var="${i}_ID"
195 if [ -n "${!id_var}" ]; then
196 cli_print_fmt1 2 "ID" "${!id_var}"
197 fi
198
199 local prefix_var="${i}_PREFIX"
200 if isset ${prefix_var}; then
201 cli_headline 3 "Prefix(es)"
202
203 local prefix
204 for prefix in ${!prefix_var}; do
205 cli_print_fmt1 3 "${prefix}"
206 done
207 fi
208
209 cli_space
210 done
211
212 cli_headline 2 "Misc."
213
214 case "${MODE}" in
215 gre-transport)
216 cli_print_fmt1 2 "Transport Mode" "GRE Transport"
217 ;;
218 tunnel)
219 cli_print_fmt1 2 "Transport Mode" "Tunnel"
220 ;;
221 vti)
222 cli_print_fmt1 2 "Transport Mode" "Virtual Tunnel Interface"
223 ;;
224 *)
225 cli_print_fmt1 2 "Transport Mode" "- Unknown -"
226 ;;
227 esac
228
229 # Inactivity timeout
230 if isset INACTIVITY_TIMEOUT && [ ${INACTIVITY_TIMEOUT} -gt 0 ]; then
231 cli_print_fmt1 2 "Inactivity Timeout" "$(format_time ${INACTIVITY_TIMEOUT})"
232 fi
233 cli_space
234
235 return ${EXIT_OK}
236}
237
5601f4f5
JS
238ipsec_connection_disable() {
239 local connection=${1}
240
241 if ! ipsec_connection_write_config_key "${connection}" "ENABLED" "false"; then
242 log ERROR "Could not write configuration settings"
243 return ${EXIT_ERROR}
244 fi
245
c3f31173
MT
246 # Configure strongswan autostart
247 ipsec_strongswan_autostart
5601f4f5
JS
248}
249
250ipsec_connection_enable() {
251 local connection=${1}
252
253 if ! ipsec_connection_write_config_key "${connection}" "ENABLED" "true"; then
254 log ERROR "Could not write configuration settings"
255 return ${EXIT_ERROR}
256 fi
257
c3f31173
MT
258 # Configure strongswan autostart
259 ipsec_strongswan_autostart
5601f4f5
JS
260}
261
917a1aa0
JS
262# This function writes all values to a via ${connection} specificated VPN IPsec configuration file
263ipsec_connection_write_config() {
264 assert [ $# -ge 1 ]
265
266 local connection="${1}"
267
268 if ! ipsec_connection_exists "${connection}"; then
269 log ERROR "No such VPN IPsec connection: ${connection}"
270 return ${EXIT_ERROR}
271 fi
272
cf8685a1 273 local path="${NETWORK_IPSEC_CONNS_DIR}/${connection}/settings"
917a1aa0
JS
274
275 if ! settings_write "${path}" ${IPSEC_CONNECTION_CONFIG_SETTINGS}; then
276 log ERROR "Could not write configuration settings for VPN IPsec connection ${connection}"
277 return ${EXIT_ERROR}
278 fi
279
280 ipsec_reload ${connection}
281}
282
283# This funtion writes the value for one key to a via ${connection} specificated VPN IPsec connection configuration file
284ipsec_connection_write_config_key() {
285 assert [ $# -ge 3 ]
286
287 local connection=${1}
288 local key=${2}
289 shift 2
290
291 local value="$@"
292
293 if ! ipsec_connection_exists "${connection}"; then
294 log ERROR "No such VPN ipsec connection: ${connection}"
295 return ${EXIT_ERROR}
296 fi
297
298 log DEBUG "Set '${key}' to new value '${value}' in VPN ipsec connection '${connection}'"
299
300 local ${IPSEC_CONNECTION_CONFIG_SETTINGS}
301
302 # Read the config settings
303 if ! ipsec_connection_read_config "${connection}"; then
304 return ${EXIT_ERROR}
305 fi
306
307 # Set the key to a new value
308 assign "${key}" "${value}"
309
310 if ! ipsec_connection_write_config "${connection}"; then
311 return ${EXIT_ERROR}
312 fi
313
314 return ${EXIT_TRUE}
315}
316
317# Reads one or more keys out of a settings file or all if no key is provided.
318ipsec_connection_read_config() {
319 assert [ $# -ge 1 ]
320
321 local connection="${1}"
322 shift 1
323
324 if ! ipsec_connection_exists "${connection}"; then
325 log ERROR "No such VPN IPsec connection : ${connection}"
326 return ${EXIT_ERROR}
327 fi
328
329
330 local args
331 if [ $# -eq 0 ] && [ -n "${IPSEC_CONNECTION_CONFIG_SETTINGS}" ]; then
332 list_append args ${IPSEC_CONNECTION_CONFIG_SETTINGS}
333 else
2212045f 334 list_append args "$@"
917a1aa0
JS
335 fi
336
cf8685a1 337 local path="${NETWORK_IPSEC_CONNS_DIR}/${connection}/settings"
917a1aa0
JS
338
339 if ! settings_read "${path}" ${args}; then
340 log ERROR "Could not read settings for VPN IPsec connection ${connection}"
341 return ${EXIT_ERROR}
342 fi
343}
344
917a1aa0
JS
345# This function checks if a vpn ipsec connection exists
346# Returns True when yes and false when not
347ipsec_connection_exists() {
348 assert [ $# -eq 1 ]
349
350 local connection=${1}
351
cf8685a1 352 local path="${NETWORK_IPSEC_CONNS_DIR}/${connection}"
917a1aa0
JS
353
354 [ -d "${path}" ] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
355}
356
c3f31173
MT
357# Determines if strongswan should be automatically started
358# when the system boots up.
359ipsec_strongswan_autostart() {
360 local autostart_needed="false"
361
362 local connection
363 for connection in $(ipsec_list_connections); do
364 local ENABLED
365
366 if ! ipsec_connection_read_config "${connection}" "ENABLED"; then
367 log WARNING "Could not read configuation"
368 continue
369 fi
370
371 if enabled ENABLED; then
372 autostart_needed="true"
373 break
374 fi
375 done
376
377 # Start strongswan when we need it and when it is not yet enabled
b863fe52
MT
378 if ${autostart_needed}; then
379 if ! service_is_enabled "strongswan"; then
380 service_enable "strongswan"
381 fi
382
383 if ! service_is_active "strongswan"; then
384 service_start "strongswan"
385 fi
c3f31173
MT
386
387 # Disable strongswan when we do not need it but it is enabled
b863fe52
MT
388 elif ! ${autostart_needed}; then
389 if service_is_enabled "strongswan"; then
390 service_disable "strongswan"
391 fi
392
393 if service_is_active "strongswan"; then
394 service_stop "strongswan"
395 fi
c3f31173
MT
396 fi
397}
398
f0e91d26 399ipsec_strongswan_load() {
7fc57ebc
MT
400 # Do nothing if strongswan is not running
401 if ! service_is_active "strongswan"; then
402 return ${EXIT_OK}
403 fi
404
f0e91d26
JS
405 if ! cmd swanctl --load-all; then
406 log ERROR "Could not reload strongswan config"
407 return ${EXIT_ERROR}
408 fi
409}
410
917a1aa0
JS
411# Reloads the connection after config changes
412ipsec_reload() {
39d87f20
JS
413 local connection=${1}
414
5601f4f5
JS
415 local ENABLED
416
417 if ! ipsec_connection_read_config "${connection}" "ENABLED"; then
418 log ERROR "Could not read configuration for IPsec connection ${connection}"
39d87f20
JS
419 return ${EXIT_ERROR}
420 fi
421
471f16bc 422 if enabled ENABLED; then
5601f4f5
JS
423 if ! ipsec_connection_to_strongswan ${connection}; then
424 log ERROR "Could not generate strongswan config for ${connnection}"
425 return ${EXIT_ERROR}
426 fi
427 else
ad482897 428 log DEBUG "Deleting strongswan config ${NETWORK_IPSEC_SWANCTL_CONNECTIONS_DIR}/${connection}.conf"
5601f4f5
JS
429 unlink "${NETWORK_IPSEC_SWANCTL_CONNECTIONS_DIR}/${connection}.conf"
430 fi
431
f0e91d26 432 ipsec_strongswan_load
917a1aa0
JS
433}
434
435# Handle the cli after authentification
436ipsec_connection_authentication() {
437 if [ ! $# -gt 1 ]; then
438 log ERROR "Not enough arguments"
439 return ${EXIT_ERROR}
440 fi
441
442 local connection=${1}
443 local cmd=${2}
444 shift 2
445
446 case ${cmd} in
447 mode)
2212045f 448 ipsec_connection_authentication_mode "${connection}" "$@"
917a1aa0
JS
449 ;;
450 pre-shared-key)
2212045f 451 ipsec_connection_authentication_psk "${connection}" "$@"
917a1aa0
JS
452 ;;
453 *)
454 log ERROR "Unrecognized argument: ${cmd}"
455 return ${EXIT_ERROR}
456 ;;
457 esac
458}
459
460# Set the authentification mode
461ipsec_connection_authentication_mode() {
462 if [ ! $# -eq 2 ]; then
463 log ERROR "Not enough arguments"
464 return ${EXIT_ERROR}
465 fi
466 local connection=${1}
467 local mode=${2}
468
469 if ! isoneof mode ${IPSEC_VALID_AUTH_MODES}; then
470 log ERROR "Auth mode '${mode}' is invalid"
471 return ${EXIT_ERROR}
472 fi
473
ab589039 474 if ! ipsec_connection_write_config_key "${connection}" "AUTH_MODE" ${mode^^}; then
917a1aa0
JS
475 log ERROR "Could not write configuration settings"
476 return ${EXIT_ERROR}
477 fi
478}
479
480# Set the psk
481ipsec_connection_authentication_psk() {
db491d1d 482 if [ ! $# -eq 2 ]; then
917a1aa0
JS
483 log ERROR "Not enough arguments"
484 return ${EXIT_ERROR}
485 fi
1bfc4f56 486
917a1aa0
JS
487 local connection=${1}
488 local psk=${2}
489
1bfc4f56
MT
490 local length=${#psk}
491
492 if [ ${length} -lt 4 ]; then
493 error "The PSK must be longer than four characters"
494 return ${EXIT_ERROR}
495 fi
496
497 if [ ${length} -gt 128 ]; then
498 error "The PSK cannot be longer than 128 characters"
499 return ${EXIT_ERROR}
500 fi
917a1aa0 501
1bfc4f56 502 if ! ipsec_connection_write_config_key "${connection}" "PSK" "${psk}"; then
917a1aa0
JS
503 log ERROR "Could not write configuration settings"
504 return ${EXIT_ERROR}
505 fi
506
507 return ${EXIT_OK}
508}
509
3cde31b9
MT
510ipsec_connection_up() {
511 local connection="${1}"
512
513 if ! ipsec_connection_exists "${connection}"; then
514 error "No such VPN IPsec connection: ${connection}"
515 return ${EXIT_ERROR}
516 fi
517
518 cmd swanctl --initiate --child "${connection}"
519}
520
521ipsec_connection_down() {
522 local connection="${1}"
523
524 if ! ipsec_connection_exists "${connection}"; then
525 error "No such VPN IPsec connection: ${connection}"
526 return ${EXIT_ERROR}
527 fi
528
529 cmd swanctl --terminate --ike "${connection}"
530}
bb9fccaf
JS
531
532# Handle the cli after authentification
533ipsec_connection_dpd() {
534 if [ ! $# -gt 1 ]; then
535 log ERROR "Not enough arguments"
536 return ${EXIT_ERROR}
537 fi
538
539 local connection=${1}
540 local cmd=${2}
541 shift 2
542
543 case ${cmd} in
544 action)
2212045f 545 ipsec_connection_dpd_action "${connection}" "$@"
bb9fccaf
JS
546 ;;
547 delay)
2212045f 548 ipsec_connection_dpd_delay "${connection}" "$@"
bb9fccaf
JS
549 ;;
550 timeout)
2212045f 551 ipsec_connection_dpd_timeout "${connection}" "$@"
bb9fccaf
JS
552 ;;
553 *)
554 log ERROR "Unrecognized argument: ${cmd}"
555 return ${EXIT_ERROR}
556 ;;
557 esac
558}
559
560# Set the default dpd action
561ipsec_connection_dpd_action() {
562 if [ ! $# -eq 2 ]; then
563 log ERROR "Not enough arguments"
564 return ${EXIT_ERROR}
565 fi
566 local connection=${1}
567 local action=${2}
568
569 if ! isoneof action "restart" "clear"; then
570 log ERROR "dpd action '${action}' is invalid"
571 return ${EXIT_ERROR}
572 fi
573
574 if ! ipsec_connection_write_config_key "${connection}" "DPD_ACTION" ${action}; then
575 log ERROR "Could not write configuration settings"
576 return ${EXIT_ERROR}
577 fi
578}
579
580# Set the dpd delay
581ipsec_connection_dpd_delay() {
582 if [ ! $# -ge 2 ]; then
583 log ERROR "Not enough arguments"
584 return ${EXIT_ERROR}
585 fi
586
587 local connection=${1}
588 shift 1
589 local value=$@
590
591 if ! isinteger value; then
2212045f 592 value=$(parse_time "$@")
bb9fccaf
JS
593 if [ ! $? -eq 0 ]; then
594 log ERROR "Parsing the passed time was not sucessful please check the passed values."
595 return ${EXIT_ERROR}
596 fi
597 fi
598
599 if [ ${value} -lt 0 ]; then
600 log ERROR "The passed time value must be in the sum greater or equal zero seconds."
601 return ${EXIT_ERROR}
602 fi
603
604 if ! ipsec_connection_write_config_key "${connection}" "DPD_DELAY" ${value}; then
605 log ERROR "Could not write configuration settings"
606 return ${EXIT_ERROR}
607 fi
608
609 return ${EXIT_OK}
610}
611
612# Set the dpd timeout
613ipsec_connection_dpd_timeout() {
614 if [ ! $# -ge 2 ]; then
615 log ERROR "Not enough arguments"
616 return ${EXIT_ERROR}
617 fi
618
619 local connection=${1}
620 shift 1
621 local value=$@
622
623 if ! isinteger value; then
2212045f 624 value=$(parse_time "$@")
bb9fccaf
JS
625 if [ ! $? -eq 0 ]; then
626 log ERROR "Parsing the passed time was not sucessful please check the passed values."
627 return ${EXIT_ERROR}
628 fi
629 fi
630
631 if [ ${value} -le 0 ]; then
632 log ERROR "The passed time value must be in the sum greater or equal zero seconds."
633 return ${EXIT_ERROR}
634 fi
635
636 if ! ipsec_connection_write_config_key "${connection}" "DPD_TIMEOUT" ${value}; then
637 log ERROR "Could not write configuration settings"
638 return ${EXIT_ERROR}
639 fi
640
641 return ${EXIT_OK}
642}
643
917a1aa0
JS
644# Handle the cli after local
645ipsec_connection_local() {
646 if [ ! $# -ge 2 ]; then
647 log ERROR "Not enough arguments"
648 return ${EXIT_ERROR}
649 fi
650
651 local connection=${1}
652 local cmd=${2}
653 shift 2
654
655 case ${cmd} in
bb9fccaf 656 address)
2212045f 657 ipsec_connection_local_address "${connection}" "$@"
bb9fccaf 658 ;;
917a1aa0 659 id)
2212045f 660 ipsec_connection_id "${connection}" "LOCAL" "$@"
917a1aa0
JS
661 ;;
662 prefix)
2212045f 663 ipsec_connection_prefix "${connection}" "LOCAL" "$@"
917a1aa0
JS
664 ;;
665 *)
666 log ERROR "Unrecognized argument: ${cmd}"
667 return ${EXIT_ERROR}
668 ;;
669 esac
670
671 return ${EXIT_OK}
672}
673
674# Set the connection mode
675ipsec_connection_mode() {
5bdbc2ee 676 if [ ! $# -eq 2 ]; then
917a1aa0
JS
677 log ERROR "Not enough arguments"
678 return ${EXIT_ERROR}
679 fi
680 local connection=${1}
681 local mode=${2}
682
683 if ! isoneof mode ${IPSEC_VALID_MODES}; then
684 log ERROR "Mode '${mode}' is invalid"
685 return ${EXIT_ERROR}
686 fi
687
688 if ! ipsec_connection_write_config_key "${connection}" "MODE" ${mode}; then
689 log ERROR "Could not write configuration settings"
690 return ${EXIT_ERROR}
691 fi
692
693 return ${EXIT_OK}
694}
695
bb9fccaf
JS
696# Set the local address
697ipsec_connection_local_address() {
698 if [ ! $# -eq 2 ]; then
699 log ERROR "Not enough arguments"
700 return ${EXIT_ERROR}
701 fi
702 local connection=${1}
703 local local_address=${2}
704
705 if ! ipsec_connection_check_peer ${local_address}; then
706 log ERROR "Local address '${local_address}' is invalid"
707 return ${EXIT_ERROR}
708 fi
709
710 if ! ipsec_connection_write_config_key "${connection}" "LOCAL_ADDRESS" ${local_address}; then
711 log ERROR "Could not write configuration settings"
712 return ${EXIT_ERROR}
713 fi
714
715 return ${EXIT_OK}
716}
717
917a1aa0
JS
718# Set the peer to connect to
719ipsec_connection_peer() {
0b962a64 720 if [ ! $# -eq 2 ]; then
917a1aa0
JS
721 log ERROR "Not enough arguments"
722 return ${EXIT_ERROR}
723 fi
724 local connection=${1}
725 local peer=${2}
726
727 if ! ipsec_connection_check_peer ${peer}; then
728 log ERROR "Peer '${peer}' is invalid"
729 return ${EXIT_ERROR}
730 fi
731
732 if ! ipsec_connection_write_config_key "${connection}" "PEER" ${peer}; then
733 log ERROR "Could not write configuration settings"
734 return ${EXIT_ERROR}
735 fi
736
737 return ${EXIT_OK}
738}
739
740#Set the local or remote id
741ipsec_connection_id() {
742 if [ ! $# -eq 3 ]; then
743 log ERROR "Not enough arguments"
744 return ${EXIT_ERROR}
745 fi
746 local connection=${1}
747 local type=${2}
748 local id=${3}
749
750 if ! ipsec_connection_check_id ${id}; then
751 log ERROR "Id '${id}' is invalid"
752 return ${EXIT_ERROR}
753 fi
aaa72eef 754
917a1aa0
JS
755 if ! ipsec_connection_write_config_key "${connection}" "${type}_ID" ${id}; then
756 log ERROR "Could not write configuration settings"
757 return ${EXIT_ERROR}
758 fi
aaa72eef 759
917a1aa0
JS
760 return ${EXIT_OK}
761}
762
aaa72eef 763# Set the local or remote prefix
917a1aa0
JS
764ipsec_connection_prefix() {
765 if [ ! $# -ge 3 ]; then
766 log ERROR "Not enough arguments"
767 return ${EXIT_ERROR}
768 fi
769 local connection=${1}
770 local type=${2}
771 shift 2
aaa72eef 772
917a1aa0
JS
773 local _prefix="${type}_PREFIX"
774 local "${_prefix}"
775 if ! ipsec_connection_read_config "${connection}" "${_prefix}"; then
776 return ${EXIT_ERROR}
777 fi
778
779 # Remove duplicated entries to proceed the list safely
780 assign "${_prefix}" "$(list_unique ${!_prefix} )"
781
782 local prefixes_added
783 local prefixes_removed
784 local prefixes_set
785
786 while [ $# -gt 0 ]; do
787 local arg="${1}"
788
789 case "${arg}" in
790 +*)
791 list_append prefixes_added "${arg:1}"
792 ;;
793 -*)
794 list_append prefixes_removed "${arg:1}"
795 ;;
796 [A-Fa-f0-9]*)
797 list_append prefixes_set "${arg}"
798 ;;
799 *)
800 error "Invalid argument: ${arg}"
801 return ${EXIT_ERROR}
802 ;;
803 esac
804 shift
805 done
806
807 # Check if the user is trying a mixed operation
808 if ! list_is_empty prefixes_set && (! list_is_empty prefixes_added || ! list_is_empty prefixes_removed); then
809 error "You cannot reset the prefix list and add or remove prefixes at the same time"
810 return ${EXIT_ERROR}
811 fi
812
813 # Set new prefix list
814 if ! list_is_empty prefixes_set; then
815 # Check if all prefixes are valid
816 local prefix
817 for prefix in ${prefixes_set}; do
818 if ! ip_net_is_valid ${prefix}; then
819 error "Unsupported prefix: ${prefix}"
820 return ${EXIT_ERROR}
821 fi
822 done
823
824 assign "${_prefix}" "${prefixes_set}"
825
826 # Perform incremental updates
827 else
828 local prefix
829
830 # Perform all removals
831 for prefix in ${prefixes_removed}; do
832 if ! list_remove "${_prefix}" ${prefix}; then
833 warning "${prefix} was not on the list and could not be removed"
834 fi
835 done
836
837
838 for prefix in ${prefixes_added}; do
839 if ip_net_is_valid ${prefix}; then
840 if ! list_append_unique "${_prefix}" ${prefix}; then
841 warning "${prefix} is already on the prefix list"
842 fi
843 else
f03f29b7 844 warning "${prefix} is not a valid IP network and could not be added"
917a1aa0
JS
845 fi
846 done
847 fi
848
849 # Check if the list contain at least one valid prefix
850 if list_is_empty ${_prefix}; then
851 error "Cannot save an empty prefix list"
852 return ${EXIT_ERROR}
853 fi
854
855 # Save everything
856 if ! ipsec_connection_write_config_key "${connection}" "${_prefix}" ${!_prefix}; then
857 log ERROR "Could not write configuration settings"
858 fi
859
860 return ${EXIT_OK}
861}
862
96fdb077
JS
863# Set the pools to use
864ipsec_connection_pool() {
865 if [ ! $# -ge 2 ]; then
866 log ERROR "Not enough arguments"
867 return ${EXIT_ERROR}
868 fi
869 local connection=${1}
870 shift
871
872 local POOLS
873 if ! ipsec_connection_read_config "${connection}" "POOLS"; then
874 return ${EXIT_ERROR}
875 fi
876
877 # Remove duplicated entries to proceed the list safely
878 assign "POOLS" "$(list_unique ${POOLS})"
879
880 local pools_added
881 local pools_removed
882 local pools_set
883
884 while [ $# -gt 0 ]; do
885 local arg="${1}"
886
887 case "${arg}" in
888 +*)
889 list_append pools_added "${arg:1}"
890 ;;
891 -*)
892 list_append pools_removed "${arg:1}"
893 ;;
894 [A-Za-z0-9]*)
895 list_append pools_set "${arg}"
896 ;;
897 *)
898 error "Invalid argument: ${arg}"
899 return ${EXIT_ERROR}
900 ;;
901 esac
902 shift
903 done
904
905 # Check if the user is trying a mixed operation
906 if ! list_is_empty pools_set && (! list_is_empty pools_added || ! list_is_empty pools_removed); then
907 error "You cannot reset the pools list and add or remove pools at the same time"
908 return ${EXIT_ERROR}
909 fi
910
911 # Set new pools list
912 if ! list_is_empty pools_set; then
913 # Check if all pools are valid
914 local pool
915 for pool in ${pools_set}; do
916 if ! ipsec_pool_exists ${pool} || ! ipsec_pool_check_config ${pool}; then
917 error "Pool ${pool} is not valid"
918 return ${EXIT_ERROR}
919 fi
920 done
921
922 assign "POOLS" "${pools_set}"
923
924 # Perform incremental updates
925 else
926 local pool
927
928 # Perform all removals
929 for pool in ${pools_removed}; do
930 if ! list_remove "POOLS" ${pool}; then
931 warning "${pool} was not on the list and could not be removed"
932 fi
933 done
934
935
936 for pool in ${pools_added}; do
beb0ebbb 937 if ipsec_pool_exists ${pool} && ipsec_pool_check_config ${pool}; then
96fdb077
JS
938 if ! list_append_unique "POOLS" ${pool}; then
939 warning "${pool} is already on the prefix list"
940 fi
941 else
942 warning "${pool} is not a valid pool"
943 fi
944 done
945 fi
946
947 # Check if the list contain at least one valid pool
948 if list_is_empty POOLS; then
949 error "Cannot save an empty pool list"
950 return ${EXIT_ERROR}
951 fi
952
953 # Save everything
954 if ! ipsec_connection_write_config_key "${connection}" "POOLS" ${POOLS}; then
955 log ERROR "Could not write configuration settings"
956 fi
957
958 return ${EXIT_OK}
959}
960
917a1aa0
JS
961# Handle the cli after remote
962ipsec_connection_remote() {
963 if [ ! $# -ge 2 ]; then
964 log ERROR "Not enough arguments"
965 return ${EXIT_ERROR}
966 fi
967
968 local connection=${1}
969 local cmd=${2}
970 shift 2
971
972 case ${cmd} in
973 id)
2212045f 974 ipsec_connection_id "${connection}" "REMOTE" "$@"
917a1aa0
JS
975 ;;
976
977 prefix)
2212045f 978 ipsec_connection_prefix "${connection}" "REMOTE" "$@"
917a1aa0
JS
979 ;;
980 *)
981 log ERROR "Unrecognized argument: ${cmd}"
982 return ${EXIT_ERROR}
983 ;;
984 esac
985
986 return ${EXIT_OK}
987}
988
989# Set the inactivity timeout
990ipsec_connection_inactivity_timeout() {
991 if [ ! $# -ge 2 ]; then
992 log ERROR "Not enough arguments"
993 return ${EXIT_ERROR}
994 fi
995
996 local connection=${1}
997 shift 1
998 local value=$@
999
1000 if ! isinteger value; then
2212045f 1001 value=$(parse_time "$@")
917a1aa0
JS
1002 if [ ! $? -eq 0 ]; then
1003 log ERROR "Parsing the passed time was not sucessful please check the passed values."
1004 return ${EXIT_ERROR}
1005 fi
1006 fi
1007
1008 if [ ${value} -le 0 ]; then
1009 log ERROR "The passed time value must be in the sum greater zero seconds."
1010 return ${EXIT_ERROR}
1011 fi
1012
1013 if ! ipsec_connection_write_config_key "${connection}" "INACTIVITY_TIMEOUT" ${value}; then
1014 log ERROR "Could not write configuration settings"
1015 return ${EXIT_ERROR}
1016 fi
1017
1018 return ${EXIT_OK}
1019}
1020
bb9fccaf
JS
1021# Set the default start action
1022ipsec_connection_start_action() {
1023 if [ ! $# -eq 2 ]; then
1024 log ERROR "Not enough arguments"
1025 return ${EXIT_ERROR}
1026 fi
1027 local connection=${1}
1028 local action=${2}
1029
1030 if ! isoneof action "on-demand" "always-on"; then
1031 log ERROR "Start action '${action}' is invalid"
1032 return ${EXIT_ERROR}
1033 fi
1034
1035 if ! ipsec_connection_write_config_key "${connection}" "START_ACTION" ${action}; then
1036 log ERROR "Could not write configuration settings"
1037 return ${EXIT_ERROR}
1038 fi
1039}
917a1aa0
JS
1040
1041# Set the security policy to use
1042ipsec_connection_security_policy() {
1043 if [ ! $# -eq 2 ]; then
1044 log ERROR "Not enough arguments"
1045 return ${EXIT_ERROR}
1046 fi
1047 local connection=${1}
1048 local security_policy=${2}
1049
1050 if ! vpn_security_policy_exists ${security_policy}; then
1051 log ERROR "No such vpn security policy '${security_policy}'"
1052 return ${EXIT_ERROR}
1053 fi
1054
1055 if ! ipsec_connection_write_config_key "${connection}" "SECURITY_POLICY" ${security_policy}; then
1056 log ERROR "Could not write configuration settings"
1057 return ${EXIT_ERROR}
1058 fi
1059}
1060
1061# Check if a id is valid
1062ipsec_connection_check_id() {
1063 assert [ $# -eq 1 ]
1064 local id=${1}
1065
1066 if [[ ${id} =~ ^@[[:alnum:]]+$ ]] || ip_is_valid ${id}; then
1067 return ${EXIT_TRUE}
1068 else
1069 return ${EXIT_FALSE}
1070 fi
1071}
1072
1073# Checks if a peer is valid
1074ipsec_connection_check_peer() {
1075 assert [ $# -eq 1 ]
1076 local peer=${1}
1077
1078 # TODO Accept also FQDNs
1079 if ip_is_valid ${peer}; then
1080 return ${EXIT_TRUE}
1081 else
1082 return ${EXIT_FALSE}
1083 fi
1084}
1085
1086# This function checks if a VPN IPsec connection name is valid
1087# Allowed are only A-Za-z0-9
1088ipsec_connection_check_name() {
1089 assert [ $# -eq 1 ]
1090
1091 local connection=${1}
1092
1093 [[ "${connection}" =~ [^[:alnum:]$] ]]
1094}
1095
1096# Function that creates one VPN IPsec connection
1097ipsec_connection_new() {
89d71d08 1098 if [ $# -gt 2 ]; then
917a1aa0
JS
1099 error "Too many arguments"
1100 return ${EXIT_ERROR}
1101 fi
1102
1103 local connection="${1}"
89d71d08
JS
1104 local type="${2}"
1105
917a1aa0
JS
1106 if ! isset connection; then
1107 error "Please provide a connection name"
1108 return ${EXIT_ERROR}
1109 fi
1110
1111 # Check for duplicates
1112 if ipsec_connection_exists "${connection}"; then
1113 error "The VPN IPsec connection ${connection} already exists"
1114 return ${EXIT_ERROR}
1115 fi
1116
1117 # Check if the name of the connection is valid
1118 if ipsec_connection_check_name "${connection}"; then
1119 error "'${connection}' contains illegal characters"
1120 return ${EXIT_ERROR}
1121 fi
1122
89d71d08
JS
1123 # Set TYPE to default if not set by the user
1124 if ! isset type; then
1125 type="${IPSEC_DEFAULT_TYPE}"
1126 fi
1127
1128 if ! isoneof "type" "net-to-net" "host-to-net"; then
1129 error "Type is invalid"
1130 return ${EXIT_ERROR}
1131 fi
1132
917a1aa0
JS
1133 log DEBUG "Creating VPN IPsec connection ${connection}"
1134
cf8685a1 1135 if ! mkdir -p "${NETWORK_IPSEC_CONNS_DIR}/${connection}"; then
917a1aa0
JS
1136 log ERROR "Could not create config directory for ${connection}"
1137 return ${EXIT_ERROR}
1138 fi
1139
1140 local ${IPSEC_CONNECTION_CONFIG_SETTINGS}
1141
917a1aa0 1142 AUTH_MODE=${IPSEC_DEFAULT_AUTH_MODE}
bb9fccaf
JS
1143 DPD_ACTION=${IPSEC_DEFAULT_DPD_ACTION}
1144 DPD_DELAY=${IPSEC_DEFAULT_DPD_DELAY}
1145 DPD_TIMEOUT=${IPSEC_DEFAULT_DPD_TIMEOUT}
5601f4f5 1146 ENABLED=${IPSEC_DEFAULT_ENABLED}
bb9fccaf
JS
1147 MODE=${IPSEC_DEFAULT_MODE}
1148 START_ACTION=${IPSEC_DEFAULT_START_ACTION}
89d71d08 1149 TYPE="${type}"
bb9fccaf 1150
917a1aa0
JS
1151 INACTIVITY_TIMEOUT=${IPSEC_DEFAULT_INACTIVITY_TIMEOUT}
1152 SECURITY_POLICY=${IPSEC_DEFAULT_SECURITY_POLICY}
1153
1154 if ! ipsec_connection_write_config "${connection}"; then
1155 log ERROR "Could not write new config file"
1156 return ${EXIT_ERROR}
1157 fi
c3f31173
MT
1158
1159 # Configure strongswan autostart
1160 ipsec_strongswan_autostart
917a1aa0
JS
1161}
1162
1163# Function that deletes based on the passed parameters one ore more vpn security policies
1164ipsec_connection_destroy() {
1165 local connection
2212045f 1166 for connection in "$@"; do
917a1aa0
JS
1167 if ! ipsec_connection_exists "${connection}"; then
1168 log ERROR "The VPN IPsec connection ${connection} does not exist."
1169 continue
1170 fi
1171
1172 log DEBUG "Deleting VPN IPsec connection ${connection}"
fa33d830
MT
1173
1174 # Delete strongswan configuration file
1175 file_delete "${NETWORK_IPSEC_SWANCTL_CONNECTIONS_DIR}/${connection}.conf"
1176
cf8685a1 1177 if ! rm -rf "${NETWORK_IPSEC_CONNS_DIR}/${connection}"; then
917a1aa0
JS
1178 log ERROR "Deleting the VPN IPsec connection ${connection} was not sucessful"
1179 return ${EXIT_ERROR}
1180 fi
c3f31173 1181
fa33d830 1182 done
917a1aa0 1183}
d6c852b8
JS
1184
1185# List all ipsec connections
1186ipsec_list_connections() {
1187 local connection
1188 for connection in ${NETWORK_IPSEC_CONNS_DIR}/*; do
1189 [ -d ${connection} ] || continue
1190 basename ${connection}
1191 done
1192}
67baa452
MT
1193
1194ipsec_connection_to_strongswan() {
1195 local connection="${1}"
aaa72eef 1196 log DEBUG "Generating IPsec configuration for ${connection}"
67baa452
MT
1197
1198 # Read the config settings
1199 local ${IPSEC_CONNECTION_CONFIG_SETTINGS}
1200 if ! ipsec_connection_read_config "${connection}"; then
1201 error "Could not read the connection ${connection}"
1202 return ${EXIT_ERROR}
1203 fi
1204
1205 local path="${NETWORK_IPSEC_SWANCTL_CONNECTIONS_DIR}/${connection}.conf"
1206
1207 (
1208 # Write the connection section
1209 _ipsec_connection_to_strongswan_connection "${connection}"
1210
1211 # Write the secrets section
1212 _ipsec_connection_to_strongswan_secrets "${connection}"
1213
1214 ) > ${path}
1215}
1216
1217_ipsec_connection_to_strongswan_connection() {
1218 local connection="${1}"
1219
1220 # Read the security policy
1221 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
1222 if ! vpn_security_policies_read_config "${SECURITY_POLICY}"; then
1223 return ${EXIT_ERROR}
1224 fi
1225
4e271faa
MT
1226 # Is DPD enabled?
1227 local dpd="false"
1228 if isset DPD_DELAY && isinteger DPD_DELAY && [ ${DPD_DELAY} -gt 0 ]; then
1229 dpd="true"
1230 fi
1231
b21fb175
MT
1232 # Write configuration header
1233 config_header "strongSwan configuration for ${connection}"
1234
67baa452
MT
1235 print_indent 0 "connections {"
1236 print_indent 1 "${connection} {"
1237
1238 # IKE Version
1239 print_indent 2 "# IKE Version"
1240 case "${KEY_EXCHANGE^^}" in
1241 IKEV1)
1242 print_indent 2 "version = 1"
1243 ;;
1244
1245 # Fall back to IKEv2 for any random values
1246 IKEV2|*)
1247 print_indent 2 "version = 2"
1248 ;;
1249 esac
1250 print # empty line
1251
4609d6b4
MT
1252 # Always only keep one connection open at a time
1253 print_indent 2 "# Unique IDs"
1254 print_indent 2 "unique = replace"
1255 print
1256
3e8ad776
MT
1257 # Local Address
1258 print_indent 2 "# Local Address"
1259 if isset LOCAL_ADDRESS; then
1260 print_indent 2 "local_addrs = ${LOCAL_ADDRESS}"
1261 else
1262 print_indent 2 "local_addrs = %any"
1263 fi
1264 print
67baa452
MT
1265
1266 # Remote Address
1267 print_indent 2 "# Remote Address"
1268 if isset PEER; then
1269 print_indent 2 "remote_addrs = ${PEER}"
1270 else
1271 print_indent 2 "remote_addrs = %any"
1272 fi
1273 print
1274
1275 # IKE Proposals
1276 print_indent 2 "# IKE Proposals"
e3ffacf7 1277 print_indent 2 "proposals = $(vpn_security_policies_make_ike_proposal ${SECURITY_POLICY})"
67baa452
MT
1278 print
1279
117278c3 1280 # DPD Settings
4e271faa 1281 if enabled dpd; then
117278c3 1282 print_indent 2 "# Dead Peer Detection"
117278c3
MT
1283 print_indent 2 "dpd_delay = ${DPD_DELAY}"
1284
1285 if isset DPD_TIMEOUT; then
1286 print_indent 2 "dpd_timeout = ${DPD_TIMEOUT}"
1287 fi
1288
1289 print
1290 fi
67baa452
MT
1291
1292 # Fragmentation
1293 print_indent 2 "# Fragmentation"
1294 print_indent 2 "fragmentation = yes"
1295 print
1296
dd66c192
MT
1297
1298 # Host-to-Net specific settings
1299 case "${TYPE}" in
1300 host-to-net)
1301 # Pools
1302 if isset POOLS; then
1303 print_indent 2 "# Pools"
1304 print_indent 2 "pools = $(list_join POOLS ", ")"
1305 print
1306 fi
1307 ;;
1308 esac
96fdb077 1309
67baa452
MT
1310 # Local
1311 print_indent 2 "local {"
1312
1313 # Local ID
1314 if isset LOCAL_ID; then
1315 print_indent 3 "id = ${LOCAL_ID}"
1316 fi
1317
1318 # Authentication
1319 case "${AUTH_MODE}" in
1320 PSK)
1321 print_indent 3 "auth = psk"
1322 ;;
1323 esac
1324
1325 print_indent 2 "}"
1326 print
1327
1328 # Remote
1329 print_indent 2 "remote {"
1330
1331 # Remote ID
1332 if isset REMOTE_ID; then
1333 print_indent 3 "id = ${REMOTE_ID}"
1334 fi
1335
1336 # Authentication
1337 case "${AUTH_MODE}" in
1338 PSK)
1339 print_indent 3 "auth = psk"
1340 ;;
1341 esac
1342
1343 print_indent 2 "}"
1344 print
1345
1346 # Children
1347
1348 print_indent 2 "children {"
1349 print_indent 3 "${connection} {"
1350
1351 print_indent 4 "# ESP Proposals"
e3d8f3f6 1352 print_indent 4 "esp_proposals = $(vpn_security_policies_make_esp_proposal ${SECURITY_POLICY})"
67baa452
MT
1353 print
1354
1355 # Traffic Selectors
1356
95835d23
MT
1357 case "${MODE}" in
1358 gre-*)
1359 print_indent 4 "local_ts = dynamic[gre]"
1360 print_indent 4 "remote_ts = dynamic[gre]"
1361 ;;
1362 *)
1363 # Local Prefixes
1364 if isset LOCAL_PREFIX; then
1365 print_indent 4 "local_ts = $(list_join LOCAL_PREFIX ,)"
1366 else
1367 print_indent 4 "local_ts = dynamic"
1368 fi
67baa452 1369
95835d23
MT
1370 # Remote Prefixes
1371 if isset REMOTE_PREFIX; then
1372 print_indent 4 "remote_ts = $(list_join REMOTE_PREFIX ,)"
1373 else
1374 print_indent 4 "remote_ts = dynamic"
1375 fi
1376 ;;
1377 esac
67baa452
MT
1378 print
1379
82fac748 1380 # Netfilter Marks
8af22236
MT
1381 case "${MODE}" in
1382 vti)
1383 print_indent 4 "# Netfilter Marks"
1384 print_indent 4 "mark_in = %unique"
1385 print_indent 4 "mark_out = %unique"
1386 print
1387 ;;
1388 esac
82fac748 1389
4e271faa
MT
1390 # Dead Peer Detection
1391 if enabled dpd; then
1392 print_indent 4 "# Dead Peer Detection"
1393 print_indent 4 "dpd_action = ${DPD_ACTION}"
1394 print
1395 fi
1396
67baa452
MT
1397 # Rekeying
1398 if isset LIFETIME; then
1399 print_indent 4 "# Rekey Time"
1400 print_indent 4 "rekey_time = ${LIFETIME}"
1401 print
1402 fi
1403
1404 # Updown Script
1405 print_indent 4 "updown = ${NETWORK_HELPERS_DIR}/ipsec-updown"
1406 print
1407
1408 # Mode
1409 print_indent 4 "# Mode"
1410 case "${MODE}" in
1411 gre-transport)
1412 print_indent 4 "mode = transport"
1413 ;;
1414 tunnel|vti|*)
1415 print_indent 4 "mode = tunnel"
1416 ;;
1417 esac
1418 print
1419
1420 # Compression
1421 print_indent 4 "# Compression"
1422 if enabled COMPRESSION; then
1423 print_indent 4 "ipcomp = yes"
1424 else
1425 print_indent 4 "ipcomp = no"
1426 fi
1427 print
1428
1429 # Inactivity Timeout
1430 if isset INACTIVITY_TIMEOUT; then
1431 print_indent 4 "# Inactivity Timeout"
1432 print_indent 4 "inactivity = ${INACTIVITY_TIMEOUT}"
1433 print
1434 fi
1435
dd66c192
MT
1436 # Net-to-Net specific settings
1437 case "${TYPE}" in
1438 net-to-net)
1439 # Start Action
1440 print_indent 4 "# Start Action"
1441 case "${START_ACTION}" in
1442 on-demand)
1443 print_indent 4 "start_action = trap"
1444 print_indent 4 "close_action = trap"
1445 ;;
1446 wait)
1447 print_indent 4 "start_action = none"
1448 print_indent 4 "close_action = none"
1449 ;;
1450 always-on|*)
1451 print_indent 4 "start_action = start"
1452 print_indent 4 "close_action = start"
1453 ;;
1454 esac
1455 print
37317b3e
MT
1456 ;;
1457 esac
67baa452
MT
1458
1459 print_indent 3 "}"
1460 print_indent 2 "}"
1461 print
1462
1463 print_indent 1 "}"
1464 print_indent 0 "}"
1465 print
1466}
1467
1468_ipsec_connection_to_strongswan_secrets() {
1469 local connection="${1}"
1470
1471 print_indent 0 "secrets {"
1472
1473 case "${AUTH_MODE}" in
1474 PSK)
1475 print_indent 1 "ike {"
1476
1477 # Secret
1478 print_indent 2 "secret = ${PSK}"
1479
1480 # ID
1481 if isset REMOTE_ID; then
1482 print_indent 2 "id = ${REMOTE_ID}"
1483 fi
1484
1485 print_indent 1 "}"
1486 ;;
1487 esac
1488
1489 print_indent 0 "}"
1490}