]>
Commit | Line | Data |
---|---|---|
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 | ||
22 | VPN_SECURITY_POLICIES_CONFIG_SETTINGS="CIPHER COMPRESSION GROUP_TYPE \ | |
23 | INTEGRITY PSEUDO_RANDOM_FUNCTION KEY_EXCHANGE LIFETIME PFS" | |
24 | VPN_SECURITY_POLICIES_READONLY="system performance" | |
25 | ||
26 | VPN_DEFAULT_SECURITY_POLICY="system" | |
27 | ||
28 | declare -A VPN_SUPPORTED_CIPHERS=( | |
29 | # 3DES-CBC | |
30 | [3DES-CBC]="168 bit 3DES-EDE-CBC" | |
31 | ||
32 | # AES-CBC | |
33 | [AES256-CBC]="256 bit AES-CBC" | |
34 | [AES192-CBC]="192 bit AES-CBC" | |
35 | [AES128-CBC]="128 bit AES-CBC" | |
36 | ||
37 | # AES-CTR | |
38 | [AES256-CTR]="256 bit AES-COUNTER" | |
39 | [AES192-CTR]="192 bit AES-COUNTER" | |
40 | [AES128-CTR]="128 bit AES-COUNTER" | |
41 | ||
42 | # AES-GCM | |
43 | [AES256-GCM128]="256 bit AES-GCM with 128 bit ICV" | |
44 | [AES192-GCM128]="192 bit AES-GCM with 128 bit ICV" | |
45 | [AES128-GCM128]="128 bit AES-GCM with 128 bit ICV" | |
46 | [AES256-GCM96]="256 bit AES-GCM with 96 bit ICV" | |
47 | [AES192-GCM96]="192 bit AES-GCM with 96 bit ICV" | |
48 | [AES128-GCM96]="128 bit AES-GCM with 96 bit ICV" | |
49 | [AES256-GCM64]="256 bit AES-GCM with 64 bit ICV" | |
50 | [AES192-GCM64]="192 bit AES-GCM with 64 bit ICV" | |
51 | [AES128-GCM64]="128 bit AES-GCM with 64 bit ICV" | |
52 | ||
53 | # AES-CCM | |
54 | [AES256-CCM128]="256 bit AES-CCM with 128 bit ICV" | |
55 | [AES192-CCM128]="192 bit AES-CCM with 128 bit ICV" | |
56 | [AES128-CCM128]="128 bit AES-CCM with 128 bit ICV" | |
57 | [AES256-CCM96]="256 bit AES-CCM with 96 bit ICV" | |
58 | [AES192-CCM96]="192 bit AES-CCM with 96 bit ICV" | |
59 | [AES128-CCM96]="128 bit AES-CCM with 96 bit ICV" | |
60 | [AES256-CCM64]="256 bit AES-CCM with 64 bit ICV" | |
61 | [AES192-CCM64]="192 bit AES-CCM with 64 bit ICV" | |
62 | [AES128-CCM64]="128 bit AES-CCM with 64 bit ICV" | |
63 | ||
64 | # CAMELLIA-CBC | |
65 | [CAMELLIA256-CBC]="256 bit CAMELLIA-CBC" | |
66 | [CAMELLIA192-CBC]="192 bit CAMELLIA-CBC" | |
67 | [CAMELLIA128-CBC]="128 bit CAMELLIA-CBC" | |
68 | ||
69 | # CAMELLIA-CTR | |
70 | [CAMELLIA256-CTR]="256 bit CAMELLIA-COUNTER" | |
71 | [CAMELLIA192-CTR]="192 bit CAMELLIA-COUNTER" | |
72 | [CAMELLIA128-CTR]="128 bit CAMELLIA-COUNTER" | |
73 | ||
74 | # CAMELLIA-GCM | |
75 | [CAMELLIA256-GCM128]="256 bit CAMELLIA-GCM with 128 bit ICV" | |
76 | [CAMELLIA192-GCM128]="192 bit CAMELLIA-GCM with 128 bit ICV" | |
77 | [CAMELLIA128-GCM128]="128 bit CAMELLIA-GCM with 128 bit ICV" | |
78 | [CAMELLIA256-GCM96]="256 bit CAMELLIA-GCM with 96 bit ICV" | |
79 | [CAMELLIA192-GCM96]="192 bit CAMELLIA-GCM with 96 bit ICV" | |
80 | [CAMELLIA128-GCM96]="128 bit CAMELLIA-GCM with 96 bit ICV" | |
81 | [CAMELLIA256-GCM64]="256 bit CAMELLIA-GCM with 64 bit ICV" | |
82 | [CAMELLIA192-GCM64]="192 bit CAMELLIA-GCM with 64 bit ICV" | |
83 | [CAMELLIA128-GCM64]="128 bit CAMELLIA-GCM with 64 bit ICV" | |
84 | ||
85 | # CAMELLIA-CCM | |
86 | [CAMELLIA256-CCM128]="256 bit CAMELLIA-CCM with 128 bit ICV" | |
87 | [CAMELLIA192-CCM128]="192 bit CAMELLIA-CCM with 128 bit ICV" | |
88 | [CAMELLIA128-CCM128]="128 bit CAMELLIA-CCM with 128 bit ICV" | |
89 | [CAMELLIA256-CCM96]="256 bit CAMELLIA-CCM with 96 bit ICV" | |
90 | [CAMELLIA192-CCM96]="192 bit CAMELLIA-CCM with 96 bit ICV" | |
91 | [CAMELLIA128-CCM96]="128 bit CAMELLIA-CCM with 96 bit ICV" | |
92 | [CAMELLIA256-CCM64]="256 bit CAMELLIA-CCM with 64 bit ICV" | |
93 | [CAMELLIA192-CCM64]="192 bit CAMELLIA-CCM with 64 bit ICV" | |
94 | [CAMELLIA128-CCM64]="128 bit CAMELLIA-CCM with 64 bit ICV" | |
95 | ||
96 | # DJB | |
97 | [CHACHA20-POLY1305]="256 bit ChaCha20/Poly1305 with 128 bit ICV" | |
98 | ||
99 | # No Encryption | |
100 | [NULL]="No Encryption" | |
101 | ) | |
102 | ||
103 | declare -A CIPHER_TO_STRONGSWAN=( | |
104 | # 3DES-CBC | |
105 | [3DES-CBC]="3des" | |
106 | ||
107 | # AES-CBC | |
108 | [AES256-CBC]="aes256" | |
109 | [AES192-CBC]="aes192" | |
110 | [AES128-CBC]="aes128" | |
111 | ||
112 | # AES-CTR | |
113 | [AES256-CTR]="aes256ctr" | |
114 | [AES192-CTR]="aes192ctr" | |
115 | [AES128-CTR]="aes128ctr" | |
116 | ||
117 | # AES-GCM | |
118 | [AES256-GCM128]="aes256gcm128" | |
119 | [AES192-GCM128]="aes192gcm128" | |
120 | [AES128-GCM128]="aes128gcm128" | |
121 | [AES256-GCM96]="aes256gcm96" | |
122 | [AES192-GCM96]="aes192gcm96" | |
123 | [AES128-GCM96]="aes128gcm96" | |
124 | [AES256-GCM64]="aes256gcm64" | |
125 | [AES192-GCM64]="aes192gcm64" | |
126 | [AES128-GCM64]="aes128gcm64" | |
127 | ||
128 | # AES-CCM | |
129 | [AES256-CCM128]="aes256ccm128" | |
130 | [AES192-CCM128]="aes192ccm128" | |
131 | [AES128-CCM128]="aes128ccm128" | |
132 | [AES256-CCM96]="aes256ccm96" | |
133 | [AES192-CCM96]="aes192ccm96" | |
134 | [AES128-CCM96]="aes128ccm96" | |
135 | [AES256-CCM64]="aes256ccm64" | |
136 | [AES192-CCM64]="aes192ccm64" | |
137 | [AES128-CCM64]="aes128ccm64" | |
138 | ||
139 | # CAMELLIA-CBC | |
140 | [CAMELLIA256-CBC]="camellia256" | |
141 | [CAMELLIA192-CBC]="camellia192" | |
142 | [CAMELLIA128-CBC]="camellia128" | |
143 | ||
144 | # CAMELLIA-CTR | |
145 | [CAMELLIA256-CTR]="camellia256ctr" | |
146 | [CAMELLIA192-CTR]="camellia192ctr" | |
147 | [CAMELLIA128-CTR]="camellia128ctr" | |
148 | ||
149 | # CAMELLIA-GCM | |
150 | [CAMELLIA256-GCM128]="camellia256gcm128" | |
151 | [CAMELLIA192-GCM128]="camellia192gcm128" | |
152 | [CAMELLIA128-GCM128]="camellia128gcm128" | |
153 | [CAMELLIA256-GCM96]="camellia256gcm96" | |
154 | [CAMELLIA192-GCM96]="camellia192gcm96" | |
155 | [CAMELLIA128-GCM96]="camellia128gcm96" | |
156 | [CAMELLIA256-GCM64]="camellia256gcm64" | |
157 | [CAMELLIA192-GCM64]="camellia192gcm64" | |
158 | [CAMELLIA128-GCM64]="camellia128gcm64" | |
159 | ||
160 | # CAMELLIA-CCM | |
161 | [CAMELLIA256-CCM128]="camellia256ccm128" | |
162 | [CAMELLIA192-CCM128]="camellia192ccm128" | |
163 | [CAMELLIA128-CCM128]="camellia128ccm128" | |
164 | [CAMELLIA256-CCM96]="camellia256ccm96" | |
165 | [CAMELLIA192-CCM96]="camellia192ccm96" | |
166 | [CAMELLIA128-CCM96]="camellia128ccm96" | |
167 | [CAMELLIA256-CCM64]="camellia256ccm64" | |
168 | [CAMELLIA192-CCM64]="camellia192ccm64" | |
169 | [CAMELLIA128-CCM64]="camellia128ccm64" | |
170 | ||
171 | # DJB | |
172 | [CHACHA20-POLY1305]="chacha20poly1305" | |
173 | ||
174 | # No Encryption | |
175 | [NULL]="null" | |
176 | ) | |
177 | ||
178 | declare -A VPN_SUPPORTED_PSEUDO_RANDOM_FUNCTION=( | |
179 | [MD5]="MD5" | |
180 | ||
181 | # SHA | |
182 | [SHA1]="SHA1" | |
183 | [SHA256]="SHA256" | |
184 | [SHA384]="SHA384" | |
185 | [SHA512]="SHA512" | |
186 | ||
187 | # AES | |
188 | [AES-XCBC]="AES-XCBC" | |
189 | [AES-CMAC]="AES-CMAC" | |
190 | ) | |
191 | ||
192 | declare -A PSEUDO_RANDOM_FUNCTION_TO_STRONGSWAN=( | |
193 | [MD5]="prfmd5" | |
194 | ||
195 | # SHA | |
196 | [SHA1]="prfsha1" | |
197 | [SHA256]="prfsha256" | |
198 | [SHA384]="prfsha384" | |
199 | [SHA512]="prfsha512" | |
200 | ||
201 | # AES | |
202 | [AES-XCBC]="prfaesxcbc" | |
203 | [AES-CMAC]="prfaescmac" | |
204 | ) | |
205 | ||
206 | declare -A VPN_SUPPORTED_INTEGRITY=( | |
207 | [MD5]="MD5-HMAC" | |
208 | ||
209 | # SHA | |
210 | [SHA1]="SHA1-HMAC" | |
211 | [SHA512]="512 bit SHA2-HMAC" | |
212 | [SHA384]="384 bit SHA2-HMAC" | |
213 | [SHA256]="256 bit SHA2-HMAC" | |
214 | ||
215 | # AES | |
216 | [AES-XCBC]="AES-XCBC" | |
217 | [AES-CMAC]="AES-CMAC" | |
218 | [AES256-GMAC]="256 bit AES-GMAC" | |
219 | [AES192-GMAC]="192 bit AES-GMAC" | |
220 | [AES128-GMAC]="128 bit AES-GMAC" | |
221 | ) | |
222 | ||
223 | declare -A INTEGRITY_TO_STRONGSWAN=( | |
224 | [MD5]="md5" | |
225 | ||
226 | # SHA | |
227 | [SHA1]="sha1" | |
228 | [SHA512]="sha512" | |
229 | [SHA384]="sha384" | |
230 | [SHA256]="sha256" | |
231 | ||
232 | # AES | |
233 | [AES-XCBC]="aesxcbc" | |
234 | [AES-CMAC]="aescmac" | |
235 | [AES256-GMAC]="aes256gmac" | |
236 | [AES192-GMAC]="aes192gmac" | |
237 | [AES128-GMAC]="aes128gmac" | |
238 | ) | |
239 | ||
240 | declare -A VPN_SUPPORTED_GROUP_TYPES=( | |
241 | # Regular Groups | |
242 | [MODP768]="768 bit Modulo Prime Group" | |
243 | [MODP1024]="1024 bit Modulo Prime Group" | |
244 | [MODP1536]="1536 bit Modulo Prime Group" | |
245 | [MODP2048]="2048 bit Modulo Prime Group" | |
246 | [MODP3072]="3072 bit Modulo Prime Group" | |
247 | [MODP4096]="4096 bit Modulo Prime Group" | |
248 | [MODP6144]="6144 bit Modulo Prime Group" | |
249 | [MODP8192]="8192 bit Modulo Prime Group" | |
250 | ||
251 | # NIST Elliptic Curve Groups | |
252 | [ECP192]="192 bit NIST Elliptic Curve Group" | |
253 | [ECP224]="224 bit NIST Elliptic Curve Group" | |
254 | [ECP256]="256 bit NIST Elliptic Curve Group" | |
255 | [ECP384]="384 bit NIST Elliptic Curve Group" | |
256 | [ECP521]="521 bit NIST Elliptic Curve Group" | |
257 | ||
258 | # Brainpool Elliptic Curve Groups | |
259 | [ECP224BP]="224 bit Brainpool Elliptic Curve Group" | |
260 | [ECP256BP]="256 bit Brainpool Elliptic Curve Group" | |
261 | [ECP384BP]="384 bit Brainpool Elliptic Curve Group" | |
262 | [ECP512BP]="512 bit Brainpool Elliptic Curve Group" | |
263 | ||
264 | # Curve25519 | |
265 | [CURVE25519]="256 bit Elliptic Curve 25519" | |
266 | ) | |
267 | ||
268 | declare -A GROUP_TYPE_TO_STRONGSWAN=( | |
269 | # Regular Groups | |
270 | [MODP768]="modp768" | |
271 | [MODP1024]="modp1024" | |
272 | [MODP1536]="modp1536" | |
273 | [MODP2048]="modp2048" | |
274 | [MODP3072]="modp3072" | |
275 | [MODP4096]="modp4096" | |
276 | [MODP6144]="modp6144" | |
277 | [MODP8192]="modp8192" | |
278 | ||
279 | # NIST Elliptic Curve Groups | |
280 | [ECP192]="ecp192" | |
281 | [ECP224]="ecp224" | |
282 | [ECP256]="ecp256" | |
283 | [ECP384]="ecp384" | |
284 | [ECP521]="ecp521" | |
285 | ||
286 | # Brainpool Elliptic Curve Groups | |
287 | [ECP224BP]="ecp224bp" | |
288 | [ECP256BP]="ecp256bp" | |
289 | [ECP384BP]="ecp384bp" | |
290 | [ECP512BP]="ecp512bp" | |
291 | ||
292 | # Curve25519 | |
293 | [CURVE25519]="curve25519" | |
294 | ) | |
295 | ||
296 | cli_vpn_security_policies() { | |
297 | local action | |
298 | local security_policy | |
299 | ||
300 | if vpn_security_policy_exists ${1}; then | |
301 | security_policy=${1} | |
302 | key=${2} | |
303 | shift 2 | |
304 | ||
305 | case "${key}" in | |
306 | cipher|compression|integrity|lifetime|pfs|show) | |
307 | vpn_security_policies_${key} ${security_policy} "$@" | |
308 | ;; | |
309 | pseudo-random-functions) | |
310 | vpn_security_policies_pseudo_random_functions "${security_policy}" "$@" | |
311 | ;; | |
312 | group-type) | |
313 | vpn_security_policies_group_type ${security_policy} "$@" | |
314 | ;; | |
315 | key-exchange) | |
316 | vpn_security_policies_key_exchange ${security_policy} "$@" | |
317 | ;; | |
318 | *) | |
319 | error "Unrecognized argument: ${key}" | |
320 | exit ${EXIT_ERROR} | |
321 | ;; | |
322 | esac | |
323 | else | |
324 | action=${1} | |
325 | shift | |
326 | ||
327 | case "${action}" in | |
328 | new) | |
329 | vpn_security_policies_new "$@" | |
330 | ;; | |
331 | destroy) | |
332 | vpn_security_policies_destroy "$@" | |
333 | ;; | |
334 | ""|*) | |
335 | if [ -n "${action}" ]; then | |
336 | error "Unrecognized argument: '${action}'" | |
337 | fi | |
338 | exit ${EXIT_ERROR} | |
339 | ;; | |
340 | esac | |
341 | fi | |
342 | } | |
343 | ||
344 | # This functions checks if a policy is readonly | |
345 | # returns true when yes and false when no | |
346 | vpn_security_policies_check_readonly() { | |
347 | if isoneof name ${VPN_SECURITY_POLICIES_READONLY}; then | |
348 | return ${EXIT_TRUE} | |
349 | else | |
350 | return ${EXIT_FALSE} | |
351 | fi | |
352 | } | |
353 | ||
354 | # This function writes all values to a via ${name} specificated vpn security policy configuration file | |
355 | vpn_security_policies_write_config() { | |
356 | assert [ $# -ge 1 ] | |
357 | ||
358 | local name="${1}" | |
359 | ||
360 | if ! vpn_security_policy_exists "${name}"; then | |
361 | log ERROR "No such vpn security policy: ${name}" | |
362 | return ${EXIT_ERROR} | |
363 | fi | |
364 | ||
365 | if vpn_security_policies_check_readonly "${name}"; then | |
366 | log ERROR "The ${name} vpn security policy cannot be changed." | |
367 | return ${EXIT_ERROR} | |
368 | fi | |
369 | ||
370 | local path="$(vpn_security_policies_path "${name}")" | |
371 | if [ ! -w ${path} ]; then | |
372 | log ERROR "${path} is not writeable" | |
373 | return ${EXIT_ERROR} | |
374 | fi | |
375 | ||
376 | if ! settings_write "${path}" ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}; then | |
377 | log ERROR "Could not write configuration settings for vpn security policy ${name}" | |
378 | return ${EXIT_ERROR} | |
379 | fi | |
380 | ||
381 | if ! vpn_security_policies_reload ${name}; then | |
382 | log WARNING "Could not reload the IPsec connection using this security policy" | |
383 | return ${EXIT_ERROR} | |
384 | fi | |
385 | } | |
386 | ||
387 | # reload IPsec connections using a special policy | |
388 | vpn_security_policies_reload() { | |
389 | local name=${1} | |
390 | ||
391 | local connection | |
392 | for connection in $(ipsec_list_connections); do | |
393 | local SECURITY_POLICY ENABLED | |
394 | ||
395 | if ! ipsec_connection_read_config "${connection}" "SECURITY_POLICY"; then | |
396 | continue | |
397 | fi | |
398 | ||
399 | if [[ "${SECURITY_POLICY}" = "${name}" ]] && enabled ENABLED; then | |
400 | if ! ipsec_connection_to_strongswan "${connection}"; then | |
401 | log ERROR "Could not generate strongswan config for ${connnection}" | |
402 | fi | |
403 | fi | |
404 | done | |
405 | ||
406 | ipsec_strongswan_load | |
407 | } | |
408 | ||
409 | # This funtion writes the value for one key to a via ${name} specificated vpn security policy configuration file | |
410 | vpn_security_policies_write_config_key() { | |
411 | assert [ $# -ge 3 ] | |
412 | ||
413 | local name=${1} | |
414 | local key=${2} | |
415 | shift 2 | |
416 | ||
417 | local value="$@" | |
418 | ||
419 | if ! vpn_security_policy_exists "${name}"; then | |
420 | log ERROR "No such vpn security policy: ${name}" | |
421 | return ${EXIT_ERROR} | |
422 | fi | |
423 | ||
424 | log DEBUG "Set '${key}' to new value '${value}' in vpn security policy ${name}" | |
425 | ||
426 | local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS} | |
427 | ||
428 | # Read the config settings | |
429 | if ! vpn_security_policies_read_config "${name}"; then | |
430 | return ${EXIT_ERROR} | |
431 | fi | |
432 | ||
433 | # Set the key to a new value | |
434 | assign "${key}" "${value}" | |
435 | ||
436 | if ! vpn_security_policies_write_config "${name}"; then | |
437 | return ${EXIT_ERROR} | |
438 | fi | |
439 | ||
440 | return ${EXIT_TRUE} | |
441 | } | |
442 | ||
443 | # Reads one or more keys out of a settings file or all if no key is provided. | |
444 | vpn_security_policies_read_config() { | |
445 | assert [ $# -ge 1 ] | |
446 | ||
447 | local name="${1}" | |
448 | shift 1 | |
449 | ||
450 | if ! vpn_security_policy_exists "${name}"; then | |
451 | log ERROR "No such vpn security policy: ${name}" | |
452 | return ${EXIT_ERROR} | |
453 | fi | |
454 | ||
455 | ||
456 | local args | |
457 | if [ $# -eq 0 ] && [ -n "${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}" ]; then | |
458 | list_append args ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS} | |
459 | else | |
460 | list_append args "$@" | |
461 | fi | |
462 | ||
463 | local path="$(vpn_security_policies_path ${name})" | |
464 | ||
465 | if ! settings_read "${path}" ${args}; then | |
466 | log ERROR "Could not read settings for vpn security policy ${name}" | |
467 | return ${EXIT_ERROR} | |
468 | fi | |
469 | } | |
470 | ||
471 | # Returns the path to a the configuration fora given name | |
472 | vpn_security_policies_path() { | |
473 | assert [ $# -eq 1 ] | |
474 | ||
475 | local name=${1} | |
476 | ||
477 | if vpn_security_policies_check_readonly "${name}"; then | |
478 | echo "${NETWORK_SHARE_DIR}/vpn/security-policies/${name}" | |
479 | else | |
480 | echo "${NETWORK_CONFIG_DIR}/vpn/security-policies/${name}" | |
481 | fi | |
482 | } | |
483 | ||
484 | # Print the content of a vpn security policy configuration file in a nice way | |
485 | vpn_security_policies_show() { | |
486 | assert [ $# -eq 1 ] | |
487 | ||
488 | local name=${1} | |
489 | ||
490 | local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS} | |
491 | if ! vpn_security_policies_read_config ${name}; then | |
492 | return ${EXIT_ERROR} | |
493 | fi | |
494 | ||
495 | cli_print_fmt1 0 "Security Policy: ${name}" | |
496 | cli_space | |
497 | ||
498 | # This could be done in a loop but a loop is much more complicated | |
499 | # because we print 'Group Types' but the variable is named 'GROUP_TYPES' | |
500 | cli_print_fmt1 1 "Ciphers:" | |
501 | local cipher | |
502 | for cipher in ${CIPHER}; do | |
503 | cli_print_fmt1 2 "${VPN_SUPPORTED_CIPHERS[${cipher}]-${cipher}}" | |
504 | done | |
505 | cli_space | |
506 | ||
507 | cli_print_fmt1 1 "Integrity:" | |
508 | local integrity | |
509 | for integrity in ${INTEGRITY}; do | |
510 | cli_print_fmt1 2 "${VPN_SUPPORTED_INTEGRITY[${integrity}]-${integrity}}" | |
511 | done | |
512 | cli_space | |
513 | ||
514 | cli_print_fmt1 1 "Group Types:" | |
515 | local group_type | |
516 | for group_type in ${GROUP_TYPE}; do | |
517 | cli_print_fmt1 2 "${VPN_SUPPORTED_GROUP_TYPES[${group_type}]-${group_type}}" | |
518 | done | |
519 | cli_space | |
520 | ||
521 | cli_print_fmt1 1 "Key Exchange:" "${KEY_EXCHANGE}" | |
522 | ||
523 | # Key Lifetime | |
524 | if isinteger LIFETIME && [ ${LIFETIME} -gt 0 ]; then | |
525 | cli_print_fmt1 1 "Key Lifetime:" "$(format_time ${LIFETIME})" | |
526 | else | |
527 | log ERROR "The value for Key Lifetime is not a valid integer greater zero." | |
528 | fi | |
529 | ||
530 | # PFS | |
531 | if enabled PFS; then | |
532 | cli_print_fmt1 1 "Perfect Forward Secrecy:" "enabled" | |
533 | else | |
534 | cli_print_fmt1 1 "Perfect Forward Secrecy:" "disabled" | |
535 | fi | |
536 | cli_space | |
537 | ||
538 | # Compression | |
539 | if enabled COMPRESSION; then | |
540 | cli_print_fmt1 1 "Compression:" "enabled" | |
541 | else | |
542 | cli_print_fmt1 1 "Compression:" "disabled" | |
543 | fi | |
544 | cli_space | |
545 | } | |
546 | ||
547 | # This function checks if a vpn security policy exists | |
548 | # Returns True when yes and false when not | |
549 | vpn_security_policy_exists() { | |
550 | assert [ $# -eq 1 ] | |
551 | ||
552 | local name=${1} | |
553 | ||
554 | local path=$(vpn_security_policies_path "${name}") | |
555 | ||
556 | [ -f ${path} ] && return ${EXIT_TRUE} || return ${EXIT_FALSE} | |
557 | } | |
558 | ||
559 | ||
560 | # This function parses the parameters for the 'cipher' command | |
561 | vpn_security_policies_cipher(){ | |
562 | local name=${1} | |
563 | shift | |
564 | ||
565 | if [ $# -eq 0 ]; then | |
566 | log ERROR "You must pass at least one value after cipher" | |
567 | return ${EXIT_ERROR} | |
568 | fi | |
569 | ||
570 | local CIPHER | |
571 | if ! vpn_security_policies_read_config ${name} "CIPHER"; then | |
572 | return ${EXIT_ERROR} | |
573 | fi | |
574 | ||
575 | # Remove duplicated entries to proceed the list safely | |
576 | CIPHER="$(list_unique ${CIPHER})" | |
577 | ||
578 | local ciphers_added | |
579 | local ciphers_removed | |
580 | local ciphers_set | |
581 | ||
582 | while [ $# -gt 0 ]; do | |
583 | local arg="${1}" | |
584 | ||
585 | case "${arg}" in | |
586 | +*) | |
587 | list_append ciphers_added "${arg:1}" | |
588 | ;; | |
589 | -*) | |
590 | list_append ciphers_removed "${arg:1}" | |
591 | ;; | |
592 | [A-Z0-9]*) | |
593 | list_append ciphers_set "${arg}" | |
594 | ;; | |
595 | *) | |
596 | error "Invalid argument: ${arg}" | |
597 | return ${EXIT_ERROR} | |
598 | ;; | |
599 | esac | |
600 | shift | |
601 | done | |
602 | ||
603 | # Check if the user is trying a mixed operation | |
604 | if ! list_is_empty ciphers_set && (! list_is_empty ciphers_added || ! list_is_empty ciphers_removed); then | |
605 | error "You cannot reset the cipher list and add or remove ciphers at the same time" | |
606 | return ${EXIT_ERROR} | |
607 | fi | |
608 | ||
609 | # Set new cipher list | |
610 | if ! list_is_empty ciphers_set; then | |
611 | # Check if all ciphers are valid | |
612 | local cipher | |
613 | for cipher in ${ciphers_set}; do | |
614 | if ! vpn_security_policies_cipher_supported ${cipher}; then | |
615 | error "Unsupported cipher: ${cipher}" | |
616 | return ${EXIT_ERROR} | |
617 | fi | |
618 | done | |
619 | ||
620 | CIPHER="${ciphers_set}" | |
621 | ||
622 | # Perform incremental updates | |
623 | else | |
624 | local cipher | |
625 | ||
626 | # Perform all removals | |
627 | for cipher in ${ciphers_removed}; do | |
628 | if ! list_remove CIPHER ${cipher}; then | |
629 | warning "${cipher} was not on the list and could not be removed" | |
630 | fi | |
631 | done | |
632 | ||
633 | for cipher in ${ciphers_added}; do | |
634 | if vpn_security_policies_cipher_supported ${cipher}; then | |
635 | if ! list_append_unique CIPHER ${cipher}; then | |
636 | warning "${cipher} is already on the cipher list" | |
637 | fi | |
638 | else | |
639 | warning "${cipher} is unknown or unsupported and could not be added" | |
640 | fi | |
641 | done | |
642 | fi | |
643 | ||
644 | # Check if the list contain at least one valid cipher | |
645 | if list_is_empty CIPHER; then | |
646 | error "Cannot save an empty cipher list" | |
647 | return ${EXIT_ERROR} | |
648 | fi | |
649 | ||
650 | # Save everything | |
651 | if ! vpn_security_policies_write_config_key ${name} "CIPHER" ${CIPHER}; then | |
652 | log ERROR "The changes for the vpn security policy ${name} could not be written." | |
653 | fi | |
654 | ||
655 | cli_headline 1 "Current cipher list for ${name}:" | |
656 | for cipher in ${CIPHER}; do | |
657 | cli_print_fmt1 1 "${cipher}" "${VPN_SUPPORTED_CIPHERS[${cipher}]}" | |
658 | done | |
659 | } | |
660 | ||
661 | # This function parses the parameters for the 'compression' command | |
662 | vpn_security_policies_compression(){ | |
663 | local name=${1} | |
664 | local value=${2} | |
665 | ||
666 | # Check if we get only one argument after compression <name> | |
667 | if [ ! $# -eq 2 ]; then | |
668 | log ERROR "The number of arguments do not match. Only one argument after compression is allowed." | |
669 | return ${EXIT_ERROR} | |
670 | fi | |
671 | ||
672 | if ! isbool value; then | |
673 | # We suggest only two values to avoid overburding the user. | |
674 | log ERROR "Invalid Argument ${value}" | |
675 | return ${EXIT_ERROR} | |
676 | fi | |
677 | ||
678 | vpn_security_policies_write_config_key "${name}" "COMPRESSION" "${value}" | |
679 | } | |
680 | ||
681 | # This function parses the parameters for the 'group-type' command | |
682 | vpn_security_policies_group_type(){ | |
683 | local name=${1} | |
684 | shift | |
685 | ||
686 | if [ $# -eq 0 ]; then | |
687 | log ERROR "You must pass at least one value after group-type" | |
688 | return ${EXIT_ERROR} | |
689 | fi | |
690 | ||
691 | local GROUP_TYPE | |
692 | if ! vpn_security_policies_read_config ${name} "GROUP_TYPE"; then | |
693 | return ${EXIT_ERROR} | |
694 | fi | |
695 | ||
696 | # Remove duplicated entries to proceed the list safely | |
697 | GROUP_TYPE="$(list_unique ${GROUP_TYPE})" | |
698 | ||
699 | local group_types_added | |
700 | local group_types_removed | |
701 | local group_types_set | |
702 | ||
703 | while [ $# -gt 0 ]; do | |
704 | local arg="${1}" | |
705 | ||
706 | case "${arg}" in | |
707 | +*) | |
708 | list_append group_types_added "${arg:1}" | |
709 | ;; | |
710 | -*) | |
711 | list_append group_types_removed "${arg:1}" | |
712 | ;; | |
713 | [A-Z0-9]*) | |
714 | list_append group_types_set "${arg}" | |
715 | ;; | |
716 | *) | |
717 | error "Invalid argument: ${arg}" | |
718 | return ${EXIT_ERROR} | |
719 | ;; | |
720 | esac | |
721 | shift | |
722 | done | |
723 | ||
724 | # Check if the user is trying a mixed operation | |
725 | if ! list_is_empty group_types_set && (! list_is_empty group_types_added || ! list_is_empty group_types_removed); then | |
726 | error "You cannot reset the group type list and add or remove group types at the same time" | |
727 | return ${EXIT_ERROR} | |
728 | fi | |
729 | ||
730 | # Set new group type list | |
731 | if ! list_is_empty group_types_set; then | |
732 | # Check if all group types are valid | |
733 | local group_type | |
734 | for group_type in ${group_types_set}; do | |
735 | if ! vpn_security_policies_group_type_supported ${group_type}; then | |
736 | error "Unsupported group type: ${group_type}" | |
737 | return ${EXIT_ERROR} | |
738 | fi | |
739 | done | |
740 | ||
741 | GROUP_TYPE="${group_types_set}" | |
742 | ||
743 | # Perform incremental updates | |
744 | else | |
745 | local group_type | |
746 | ||
747 | # Perform all removals | |
748 | for group_type in ${group_types_removed}; do | |
749 | if ! list_remove GROUP_TYPE ${group_type}; then | |
750 | warning "${group_type} was not on the list and could not be removed" | |
751 | fi | |
752 | done | |
753 | ||
754 | for group_type in ${group_types_added}; do | |
755 | if vpn_security_policies_group_type_supported ${group_type}; then | |
756 | if ! list_append_unique GROUP_TYPE ${group_type}; then | |
757 | warning "${group_type} is already on the group type list" | |
758 | fi | |
759 | else | |
760 | warning "${group_type} is unknown or unsupported and could not be added" | |
761 | fi | |
762 | done | |
763 | fi | |
764 | ||
765 | # Check if the list contain at least one valid group_type | |
766 | if list_is_empty GROUP_TYPE; then | |
767 | error "Cannot save an empty group type list" | |
768 | return ${EXIT_ERROR} | |
769 | fi | |
770 | ||
771 | # Save everything | |
772 | if ! vpn_security_policies_write_config_key ${name} "GROUP_TYPE" ${GROUP_TYPE}; then | |
773 | log ERROR "The changes for the vpn security policy ${name} could not be written." | |
774 | fi | |
775 | ||
776 | cli_headline 1 "Current group type list for ${name}:" | |
777 | for group_type in ${GROUP_TYPE}; do | |
778 | cli_print_fmt1 1 "${group_type}" "${VPN_SUPPORTED_GROUP_TYPES[${group_type}]}" | |
779 | done | |
780 | } | |
781 | ||
782 | # This function parses the parameters for the 'integrity' command | |
783 | vpn_security_policies_integrity(){ | |
784 | local name=${1} | |
785 | shift | |
786 | ||
787 | if [ $# -eq 0 ]; then | |
788 | log ERROR "You must pass at least one value after integrity" | |
789 | return ${EXIT_ERROR} | |
790 | fi | |
791 | ||
792 | local INTEGRITY | |
793 | if ! vpn_security_policies_read_config ${name} "INTEGRITY"; then | |
794 | return ${EXIT_ERROR} | |
795 | fi | |
796 | ||
797 | # Remove duplicated entries to proceed the list safely | |
798 | INTEGRITY="$(list_unique ${INTEGRITY})" | |
799 | ||
800 | local integritys_added | |
801 | local integritys_removed | |
802 | local integritys_set | |
803 | ||
804 | while [ $# -gt 0 ]; do | |
805 | local arg="${1}" | |
806 | ||
807 | case "${arg}" in | |
808 | +*) | |
809 | list_append integritys_added "${arg:1}" | |
810 | ;; | |
811 | -*) | |
812 | list_append integritys_removed "${arg:1}" | |
813 | ;; | |
814 | [A-Z0-9]*) | |
815 | list_append integritys_set "${arg}" | |
816 | ;; | |
817 | *) | |
818 | error "Invalid argument: ${arg}" | |
819 | return ${EXIT_ERROR} | |
820 | ;; | |
821 | esac | |
822 | shift | |
823 | done | |
824 | ||
825 | # Check if the user is trying a mixed operation | |
826 | if ! list_is_empty integritys_set && (! list_is_empty integritys_added || ! list_is_empty integritys_removed); then | |
827 | error "You cannot reset the integrity hashes list and add or remove integrity hashes at the same time" | |
828 | return ${EXIT_ERROR} | |
829 | fi | |
830 | ||
831 | # Set new integrity list | |
832 | if ! list_is_empty integritys_set; then | |
833 | # Check if all integrity hashes are valid | |
834 | local integrity | |
835 | for integrity in ${integritys_set}; do | |
836 | if ! vpn_security_policies_integrity_supported ${integrity}; then | |
837 | error "Unsupported integrity hash: ${integrity}" | |
838 | return ${EXIT_ERROR} | |
839 | fi | |
840 | done | |
841 | ||
842 | INTEGRITY="${integritys_set}" | |
843 | ||
844 | # Perform incremental updates | |
845 | else | |
846 | local integrity | |
847 | ||
848 | # Perform all removals | |
849 | for integrity in ${integritys_removed}; do | |
850 | if ! list_remove INTEGRITY ${integrity}; then | |
851 | warning "${integrity} was not on the list and could not be removed" | |
852 | fi | |
853 | done | |
854 | ||
855 | for integrity in ${integritys_added}; do | |
856 | if vpn_security_policies_integrity_supported ${integrity}; then | |
857 | if ! list_append_unique INTEGRITY ${integrity}; then | |
858 | warning "${integrity} is already on the integrity list" | |
859 | fi | |
860 | else | |
861 | warning "${integrity} is unknown or unsupported and could not be added" | |
862 | fi | |
863 | done | |
864 | fi | |
865 | ||
866 | # Check if the list contain at least one valid integrity | |
867 | if list_is_empty INTEGRITY; then | |
868 | error "Cannot save an empty integrity hashes list" | |
869 | return ${EXIT_ERROR} | |
870 | fi | |
871 | ||
872 | # Save everything | |
873 | if ! vpn_security_policies_write_config_key ${name} "INTEGRITY" ${INTEGRITY}; then | |
874 | log ERROR "The changes for the vpn security policy ${name} could not be written." | |
875 | fi | |
876 | ||
877 | cli_headline 1 "Current integrity hashes list for ${name}:" | |
878 | for integrity in ${INTEGRITY}; do | |
879 | cli_print_fmt1 1 "${integrity}" "${VPN_SUPPORTED_INTEGRITY[${integrity}]}" | |
880 | done | |
881 | } | |
882 | ||
883 | # This function parses the parameters for the 'pseudo-random-functions' command | |
884 | vpn_security_policies_pseudo_random_functions() { | |
885 | local name=${1} | |
886 | shift | |
887 | ||
888 | if [ $# -eq 0 ]; then | |
889 | log ERROR "You must pass at least one value" | |
890 | return ${EXIT_ERROR} | |
891 | fi | |
892 | ||
893 | local PSEUDO_RANDOM_FUNCTION | |
894 | if ! vpn_security_policies_read_config ${name} "PSEUDO_RANDOM_FUNCTION"; then | |
895 | return ${EXIT_ERROR} | |
896 | fi | |
897 | ||
898 | # Remove duplicated entries to proceed the list safely | |
899 | PSEUDO_RANDOM_FUNCTION="$(list_unique ${PSEUDO_RANDOM_FUNCTION})" | |
900 | ||
901 | local prfs_added | |
902 | local prfs_removed | |
903 | local prfs_set | |
904 | ||
905 | while [ $# -gt 0 ]; do | |
906 | local arg="${1}" | |
907 | ||
908 | case "${arg}" in | |
909 | +*) | |
910 | list_append prfs_added "${arg:1}" | |
911 | ;; | |
912 | -*) | |
913 | list_append prfs_removed "${arg:1}" | |
914 | ;; | |
915 | [A-Z0-9]*) | |
916 | list_append prfs_set "${arg}" | |
917 | ;; | |
918 | *) | |
919 | error "Invalid argument: ${arg}" | |
920 | return ${EXIT_ERROR} | |
921 | ;; | |
922 | esac | |
923 | shift | |
924 | done | |
925 | ||
926 | # Check if the user is trying a mixed operation | |
927 | if ! list_is_empty prfs_set && (! list_is_empty prfs_added || ! list_is_empty prfs_removed); then | |
928 | error "You cannot reset the pseudo random function list and add or remove functions at the same time" | |
929 | return ${EXIT_ERROR} | |
930 | fi | |
931 | ||
932 | # Set new psudo random function list | |
933 | if ! list_is_empty prfs_set; then | |
934 | # Check if all PRFs are valid | |
935 | local prf | |
936 | for prf in ${prfs_set}; do | |
937 | if ! vpn_security_policies_pseudo_random_function_supported "${prf}"; then | |
938 | error "Unsupported pseudo random function: ${prf}" | |
939 | return ${EXIT_ERROR} | |
940 | fi | |
941 | done | |
942 | ||
943 | PSEUDO_RANDOM_FUNCTION="${prfs_set}" | |
944 | ||
945 | # Perform incremental updates | |
946 | else | |
947 | local prf | |
948 | ||
949 | # Perform all removals | |
950 | for prf in ${prfs_removed}; do | |
951 | if ! list_remove PSEUDO_RANDOM_FUNCTION "${prf}"; then | |
952 | warning "${prf} was not on the list and could not be removed" | |
953 | fi | |
954 | done | |
955 | ||
956 | for prf in ${prfs_added}; do | |
957 | if vpn_security_policies_pseudo_random_function_supported "${prf}"; then | |
958 | if ! list_append_unique PSEUDO_RANDOM_FUNCTION "${prf}"; then | |
959 | warning "${prf} is already on the list" | |
960 | fi | |
961 | else | |
962 | warning "${prf} is unknown or unsupported and could not be added" | |
963 | fi | |
964 | done | |
965 | fi | |
966 | ||
967 | # Check if the list contain at least one valid value | |
968 | if list_is_empty PSEUDO_RANDOM_FUNCTION; then | |
969 | error "Cannot save an empty list of pseudo random functions" | |
970 | return ${EXIT_ERROR} | |
971 | fi | |
972 | ||
973 | # Save everything | |
974 | if ! vpn_security_policies_write_config_key "${name}" "PSEUDO_RANDOM_FUNCTION" "${PSEUDO_RANDOM_FUNCTION}"; then | |
975 | log ERROR "The changes for the VPN security policy ${name} could not be written" | |
976 | fi | |
977 | ||
978 | cli_headline 1 "Current pseudo random function list for ${name}:" | |
979 | for prf in ${PSEUDO_RANDOM_FUNCTION}; do | |
980 | cli_print_fmt1 1 "${prf}" "${VPN_SUPPORTED_PSEUDO_RANDOM_FUNCTION[${prf}]}" | |
981 | done | |
982 | } | |
983 | ||
984 | # This function parses the parameters for the 'key-exchange' command | |
985 | vpn_security_policies_key_exchange() { | |
986 | local name=${1} | |
987 | local value=${2} | |
988 | ||
989 | # Check if we get only one argument after key-exchange <name> | |
990 | if [ ! $# -eq 2 ]; then | |
991 | log ERROR "The number of arguments do not match. Only argument after key-exchange is allowed." | |
992 | return ${EXIT_ERROR} | |
993 | fi | |
994 | ||
995 | if ! isoneof value "ikev1" "ikev2" "IKEV1" "IKEV2"; then | |
996 | log ERROR "Invalid Argument ${value}" | |
997 | return ${EXIT_ERROR} | |
998 | fi | |
999 | ||
1000 | vpn_security_policies_write_config_key "${name}" "KEY_EXCHANGE" "${value,,}" | |
1001 | } | |
1002 | ||
1003 | # This function parses the parameters for the 'lifetime' command. | |
1004 | vpn_security_policies_lifetime(){ | |
1005 | local name=${1} | |
1006 | shift | |
1007 | ||
1008 | local value=$@ | |
1009 | ||
1010 | # Check if we get only one argument after lifetime <name> | |
1011 | if [ ! $# -ge 1 ]; then | |
1012 | log ERROR "The number of arguments do not match you must provide at least one integer value or a valid time with the format <hours>h <minutes>m <seconds>s" | |
1013 | return ${EXIT_ERROR} | |
1014 | fi | |
1015 | ||
1016 | if ! isinteger value; then | |
1017 | value=$(parse_time "$@") | |
1018 | if [ ! $? -eq 0 ]; then | |
1019 | log ERROR "Parsing the passed time was not sucessful please check the passed values." | |
1020 | return ${EXIT_ERROR} | |
1021 | fi | |
1022 | fi | |
1023 | ||
1024 | if [ ${value} -le 0 ]; then | |
1025 | log ERROR "The passed time value must be in the sum greater zero seconds." | |
1026 | return ${EXIT_ERROR} | |
1027 | fi | |
1028 | ||
1029 | vpn_security_policies_write_config_key "${name}" "LIFETIME" "${value}" | |
1030 | } | |
1031 | ||
1032 | # This function parses the parameters for the 'pfs' command | |
1033 | vpn_security_policies_pfs(){ | |
1034 | local name=${1} | |
1035 | local value=${2} | |
1036 | ||
1037 | # Check if we get only one argument after pfs <name> | |
1038 | if [ ! $# -eq 2 ]; then | |
1039 | log ERROR "The number of arguments do not match. Only argument after pfs is allowed." | |
1040 | return ${EXIT_ERROR} | |
1041 | fi | |
1042 | ||
1043 | if [ ! $# -eq 2 ] || ! isbool value; then | |
1044 | # We suggest only two values to avoid overburding the user. | |
1045 | log ERROR "Invalid Argument ${value}" | |
1046 | return ${EXIT_ERROR} | |
1047 | fi | |
1048 | ||
1049 | vpn_security_policies_write_config_key "${name}" "PFS" "${value}" | |
1050 | } | |
1051 | ||
1052 | # This function checks if a vpn security policy name is valid | |
1053 | # Allowed are only A-Za-z0-9 | |
1054 | vpn_security_policies_check_name() { | |
1055 | assert [ $# -eq 1 ] | |
1056 | ||
1057 | local name=${1} | |
1058 | ||
1059 | [[ ${name} =~ [^[:alnum:]$] ]] | |
1060 | } | |
1061 | ||
1062 | # Function that creates based on the paramters one ore more new vpn security policies | |
1063 | vpn_security_policies_new() { | |
1064 | if [ $# -gt 1 ]; then | |
1065 | error "Too many arguments" | |
1066 | return ${EXIT_ERROR} | |
1067 | fi | |
1068 | ||
1069 | local name="${1}" | |
1070 | if ! isset name; then | |
1071 | error "Please provide a name" | |
1072 | return ${EXIT_ERROR} | |
1073 | fi | |
1074 | ||
1075 | # Check for duplicates | |
1076 | if vpn_security_policy_exists "${name}"; then | |
1077 | error "The VPN security policy with name ${name} already exists" | |
1078 | return ${EXIT_ERROR} | |
1079 | fi | |
1080 | ||
1081 | # Check if name is valid | |
1082 | if vpn_security_policies_check_name "${name}"; then | |
1083 | error "'${name}' contains illegal characters" | |
1084 | return ${EXIT_ERROR} | |
1085 | fi | |
1086 | ||
1087 | # Check if we have a read-only policy with the same name | |
1088 | if vpn_security_policies_check_readonly "${name}"; then | |
1089 | error "The VPN security policy ${name} is read-only" | |
1090 | return ${EXIT_ERROR} | |
1091 | fi | |
1092 | ||
1093 | # Check if our source policy exists | |
1094 | if ! vpn_security_policy_exists "${VPN_DEFAULT_SECURITY_POLICY}"; then | |
1095 | error "Default VPN Security Policy '${VPN_DEFAULT_SECURITY_POLICY}' does not exist" | |
1096 | return ${EXIT_ERROR} | |
1097 | fi | |
1098 | ||
1099 | log DEBUG "Creating VPN Security Policy ${name}" | |
1100 | ||
1101 | if copy "$(vpn_security_policies_path "${VPN_DEFAULT_SECURITY_POLICY}")" \ | |
1102 | "$(vpn_security_policies_path ${name})"; then | |
1103 | log INFO "VPN Security Policy ${name} successfully created" | |
1104 | else | |
1105 | log ERROR "Could not create VPN Security Policy ${name}" | |
1106 | return ${EXIT_ERROR} | |
1107 | fi | |
1108 | ||
1109 | # Show the newly created policy | |
1110 | vpn_security_policies_show "${name}" | |
1111 | } | |
1112 | ||
1113 | # Function that deletes based on the passed parameters one ore more vpn security policies | |
1114 | vpn_security_policies_destroy() { | |
1115 | local name | |
1116 | for name in "$@"; do | |
1117 | if ! vpn_security_policy_exists ${name}; then | |
1118 | log ERROR "The vpn security policy ${name} does not exist." | |
1119 | continue | |
1120 | fi | |
1121 | ||
1122 | if vpn_security_policies_check_readonly ${name}; then | |
1123 | log ERROR "The vpn security policy ${name} cannot be deleted." | |
1124 | continue | |
1125 | fi | |
1126 | ||
1127 | log DEBUG "Deleting vpn security policy ${name}" | |
1128 | settings_remove $(vpn_security_policies_path ${name}) | |
1129 | ||
1130 | # Delete cache | |
1131 | rm -rf "${NETWORK_CACHE_DIR}/vpn/security-policies/${name}" | |
1132 | done | |
1133 | } | |
1134 | ||
1135 | vpn_security_policies_cipher_supported() { | |
1136 | local cipher=${1} | |
1137 | ||
1138 | list_match ${cipher} ${!VPN_SUPPORTED_CIPHERS[@]} | |
1139 | } | |
1140 | ||
1141 | ||
1142 | vpn_security_policies_group_type_supported() { | |
1143 | local group_type=${1} | |
1144 | ||
1145 | list_match ${group_type} ${!VPN_SUPPORTED_GROUP_TYPES[@]} | |
1146 | } | |
1147 | ||
1148 | vpn_security_policies_integrity_supported() { | |
1149 | local integrity=${1} | |
1150 | ||
1151 | list_match ${integrity} ${!VPN_SUPPORTED_INTEGRITY[@]} | |
1152 | } | |
1153 | ||
1154 | vpn_security_policies_pseudo_random_function_supported() { | |
1155 | local prf="${1}" | |
1156 | ||
1157 | list_match "${prf}" ${!VPN_SUPPORTED_PSEUDO_RANDOM_FUNCTION[@]} | |
1158 | } | |
1159 | ||
1160 | vpn_security_policies_cipher_is_aead() { | |
1161 | local cipher=${1} | |
1162 | ||
1163 | # All CCM and GCM ciphers are AEAD | |
1164 | if string_match "[CG]CM" "${cipher}"; then | |
1165 | return ${EXIT_TRUE} | |
1166 | fi | |
1167 | ||
1168 | # Poly1305 is AEAD | |
1169 | if string_match "POLY1305" "${cipher}"; then | |
1170 | return ${EXIT_TRUE} | |
1171 | fi | |
1172 | ||
1173 | return ${EXIT_FALSE} | |
1174 | } | |
1175 | ||
1176 | vpn_security_policies_make_ike_proposal() { | |
1177 | local name=${1} | |
1178 | ||
1179 | if ! vpn_security_policy_exists ${name}; then | |
1180 | return ${EXIT_ERROR} | |
1181 | fi | |
1182 | ||
1183 | local config_path="$(vpn_security_policies_path ${name})" | |
1184 | local cache_path="${NETWORK_CACHE_DIR}/vpn/security-policies/${name}/ike-proposal" | |
1185 | ||
1186 | # Get data from cache if possible | |
1187 | if file_exists "${cache_path}" && ! file_is_newer_than "${config_path}" "${cache_path}"; then | |
1188 | fread "${cache_path}" | |
1189 | return ${EXIT_OK} | |
1190 | fi | |
1191 | ||
1192 | # No or invalid cache data found | |
1193 | local proposal=$(_vpn_security_policies_make_ike_proposal "${name}") | |
1194 | ||
1195 | # Write proposal to cache | |
1196 | if ! make_parent_directory "${cache_path}" || ! fwrite "${cache_path}" "${proposal}"; then | |
1197 | log WARNING "Could not write to cache: ${cache_path}" | |
1198 | fi | |
1199 | ||
1200 | print "${proposal}" | |
1201 | } | |
1202 | ||
1203 | _vpn_security_policies_make_ike_proposal() { | |
1204 | local name=${1} | |
1205 | ||
1206 | # Read the config settings | |
1207 | local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS} | |
1208 | if ! vpn_security_policies_read_config "${name}"; then | |
1209 | return ${EXIT_ERROR} | |
1210 | fi | |
1211 | ||
1212 | local proposals | |
1213 | ||
1214 | local cipher | |
1215 | for cipher in ${CIPHER}; do | |
1216 | # Translate cipher | |
1217 | local _cipher=${CIPHER_TO_STRONGSWAN[${cipher}]} | |
1218 | ||
1219 | if ! isset _cipher; then | |
1220 | log WARN "Unsupported cipher: ${cipher}" | |
1221 | continue | |
1222 | fi | |
1223 | ||
1224 | if vpn_security_policies_cipher_is_aead "${cipher}"; then | |
1225 | local prf | |
1226 | for prf in ${PSEUDO_RANDOM_FUNCTION}; do | |
1227 | local _prf="${PSEUDO_RANDOM_FUNCTION_TO_STRONGSWAN[${prf}]}" | |
1228 | ||
1229 | if ! isset _prf; then | |
1230 | log WARN "Unsupported pseudo random function: ${prf}" | |
1231 | continue | |
1232 | fi | |
1233 | ||
1234 | local group_type | |
1235 | for group_type in ${GROUP_TYPE}; do | |
1236 | local _group_type=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]} | |
1237 | ||
1238 | if ! isset _group_type; then | |
1239 | log WARN "Unsupported group-type: ${group_type}" | |
1240 | continue | |
1241 | fi | |
1242 | ||
1243 | # Put everything together | |
1244 | list_append proposals "${_cipher}-${_prf}-${_group_type}" | |
1245 | done | |
1246 | done | |
1247 | else | |
1248 | local integrity | |
1249 | for integrity in ${INTEGRITY}; do | |
1250 | local _integrity=${INTEGRITY_TO_STRONGSWAN[${integrity}]} | |
1251 | ||
1252 | if ! isset _integrity; then | |
1253 | log WARN "Unsupported integrity: ${integrity}" | |
1254 | continue | |
1255 | fi | |
1256 | ||
1257 | local group_type | |
1258 | for group_type in ${GROUP_TYPE}; do | |
1259 | local _group_type=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]} | |
1260 | ||
1261 | if ! isset _group_type; then | |
1262 | log WARN "Unsupported group-type: ${group_type}" | |
1263 | continue | |
1264 | fi | |
1265 | ||
1266 | # Put everything together | |
1267 | list_append proposals "${_cipher}-${_integrity}-${_group_type}" | |
1268 | done | |
1269 | done | |
1270 | fi | |
1271 | done | |
1272 | ||
1273 | # Returns as a comma-separated list | |
1274 | list_join proposals , | |
1275 | } | |
1276 | ||
1277 | vpn_security_policies_make_esp_proposal() { | |
1278 | local name=${1} | |
1279 | ||
1280 | if ! vpn_security_policy_exists ${name}; then | |
1281 | return ${EXIT_ERROR} | |
1282 | fi | |
1283 | ||
1284 | local config_path="$(vpn_security_policies_path ${name})" | |
1285 | local cache_path="${NETWORK_CACHE_DIR}/vpn/security-policies/${name}/esp-proposal" | |
1286 | ||
1287 | # Get data from cache if possible | |
1288 | if file_exists "${cache_path}" && ! file_is_newer_than "${config_path}" "${cache_path}"; then | |
1289 | fread "${cache_path}" | |
1290 | return ${EXIT_OK} | |
1291 | fi | |
1292 | ||
1293 | # No or invalid cache data found | |
1294 | local proposal=$(_vpn_security_policies_make_esp_proposal "${name}") | |
1295 | ||
1296 | # Write proposal to cache | |
1297 | if ! make_parent_directory "${cache_path}" || ! fwrite "${cache_path}" "${proposal}"; then | |
1298 | log WARNING "Could not write to cache: ${cache_path}" | |
1299 | fi | |
1300 | ||
1301 | print "${proposal}" | |
1302 | } | |
1303 | ||
1304 | _vpn_security_policies_make_esp_proposal() { | |
1305 | local name=${1} | |
1306 | ||
1307 | # Read the config settings | |
1308 | local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS} | |
1309 | if ! vpn_security_policies_read_config "${name}"; then | |
1310 | return ${EXIT_ERROR} | |
1311 | fi | |
1312 | ||
1313 | local proposals | |
1314 | ||
1315 | local cipher | |
1316 | for cipher in ${CIPHER}; do | |
1317 | # Translate cipher | |
1318 | local _cipher=${CIPHER_TO_STRONGSWAN[${cipher}]} | |
1319 | ||
1320 | if ! isset _cipher; then | |
1321 | log WARN "Unsupported cipher: ${cipher}" | |
1322 | continue | |
1323 | fi | |
1324 | ||
1325 | if vpn_security_policies_cipher_is_aead ${cipher}; then | |
1326 | local group_type | |
1327 | for group_type in ${GROUP_TYPE}; do | |
1328 | local _group_type=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]} | |
1329 | ||
1330 | if ! isset _group_type; then | |
1331 | log WARN "Unsupported group-type: ${group_type}" | |
1332 | continue | |
1333 | fi | |
1334 | ||
1335 | # Put everything together | |
1336 | list_append proposals "${_cipher}-${_group_type}" | |
1337 | done | |
1338 | else | |
1339 | local integrity | |
1340 | for integrity in ${INTEGRITY}; do | |
1341 | local _integrity=${INTEGRITY_TO_STRONGSWAN[${integrity}]} | |
1342 | ||
1343 | if ! isset _integrity; then | |
1344 | log WARN "Unsupported integrity: ${integrity}" | |
1345 | continue | |
1346 | fi | |
1347 | ||
1348 | local group_type | |
1349 | for group_type in ${GROUP_TYPE}; do | |
1350 | local _group_type=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]} | |
1351 | ||
1352 | if ! isset _group_type; then | |
1353 | log WARN "Unsupported group-type: ${group_type}" | |
1354 | continue | |
1355 | fi | |
1356 | ||
1357 | # Put everything together | |
1358 | list_append proposals "${_cipher}-${_integrity}-${_group_type}" | |
1359 | done | |
1360 | done | |
1361 | fi | |
1362 | done | |
1363 | ||
1364 | # Returns as a comma-separated list | |
1365 | list_join proposals , | |
1366 | } | |
1367 | ||
1368 | # List all security policies | |
1369 | vpn_security_policies_list_all() { | |
1370 | list_directory "${NETWORK_SHARE_DIR}/vpn/security-policies" | |
1371 | ||
1372 | # Add all user-defined policies | |
1373 | vpn_security_policies_list_user | |
1374 | } | |
1375 | ||
1376 | vpn_security_policies_list_user() { | |
1377 | list_directory "${NETWORK_CONFIG_DIR}/vpn/security-policies" | |
1378 | } |