]>
Commit | Line | Data |
---|---|---|
3eae7bed 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 | ||
22 | VPN_SECURITY_POLICIES_CONFIG_SETTINGS="CIPHER COMPRESSION GROUP_TYPE INTEGRITY KEY_EXCHANGE LIFETIME PFS" | |
23 | VPN_SECURITY_POLICIES_READONLY="system" | |
24 | ||
6fdbda80 MT |
25 | VPN_DEFAULT_SECURITY_POLICY="system" |
26 | ||
b116ad92 | 27 | declare -A VPN_SUPPORTED_CIPHERS=( |
b68fed1f MT |
28 | # 3DES-CBC |
29 | [3DES-CBC]="168 bit 3DES-EDE-CBC" | |
30 | ||
31 | # AES-CBC | |
b116ad92 MT |
32 | [AES256-CBC]="256 bit AES-CBC" |
33 | [AES192-CBC]="192 bit AES-CBC" | |
34 | [AES128-CBC]="128 bit AES-CBC" | |
b68fed1f MT |
35 | |
36 | # AES-CTR | |
37 | [AES256-CTR]="256 bit AES-COUNTER" | |
38 | [AES192-CTR]="192 bit AES-COUNTER" | |
39 | [AES128-CTR]="128 bit AES-COUNTER" | |
40 | ||
41 | # AES-GCM | |
42 | [AES256-GCM128]="256 bit AES-GCM with 128 bit ICV" | |
43 | [AES192-GCM128]="192 bit AES-GCM with 128 bit ICV" | |
44 | [AES128-GCM128]="128 bit AES-GCM with 128 bit ICV" | |
45 | [AES256-GCM96]="256 bit AES-GCM with 96 bit ICV" | |
46 | [AES192-GCM96]="192 bit AES-GCM with 96 bit ICV" | |
47 | [AES128-GCM96]="128 bit AES-GCM with 96 bit ICV" | |
48 | [AES256-GCM64]="256 bit AES-GCM with 64 bit ICV" | |
49 | [AES192-GCM64]="192 bit AES-GCM with 64 bit ICV" | |
50 | [AES128-GCM64]="128 bit AES-GCM with 64 bit ICV" | |
51 | ||
52 | # AES-CCM | |
53 | [AES256-CCM128]="256 bit AES-CCM with 128 bit ICV" | |
54 | [AES192-CCM128]="192 bit AES-CCM with 128 bit ICV" | |
55 | [AES128-CCM128]="128 bit AES-CCM with 128 bit ICV" | |
56 | [AES256-CCM96]="256 bit AES-CCM with 96 bit ICV" | |
57 | [AES192-CCM96]="192 bit AES-CCM with 96 bit ICV" | |
58 | [AES128-CCM96]="128 bit AES-CCM with 96 bit ICV" | |
59 | [AES256-CCM64]="256 bit AES-CCM with 64 bit ICV" | |
60 | [AES192-CCM64]="192 bit AES-CCM with 64 bit ICV" | |
61 | [AES128-CCM64]="128 bit AES-CCM with 64 bit ICV" | |
62 | ||
63 | # CAMELLIA-CBC | |
64 | [CAMELLIA256-CBC]="256 bit CAMELLIA-CBC" | |
65 | [CAMELLIA192-CBC]="192 bit CAMELLIA-CBC" | |
66 | [CAMELLIA128-CBC]="128 bit CAMELLIA-CBC" | |
67 | ||
68 | # CAMELLIA-CTR | |
69 | [CAMELLIA256-CTR]="256 bit CAMELLIA-COUNTER" | |
70 | [CAMELLIA192-CTR]="192 bit CAMELLIA-COUNTER" | |
71 | [CAMELLIA128-CTR]="128 bit CAMELLIA-COUNTER" | |
72 | ||
73 | # CAMELLIA-GCM | |
74 | [CAMELLIA256-GCM128]="256 bit CAMELLIA-GCM with 128 bit ICV" | |
75 | [CAMELLIA192-GCM128]="192 bit CAMELLIA-GCM with 128 bit ICV" | |
76 | [CAMELLIA128-GCM128]="128 bit CAMELLIA-GCM with 128 bit ICV" | |
77 | [CAMELLIA256-GCM96]="256 bit CAMELLIA-GCM with 96 bit ICV" | |
78 | [CAMELLIA192-GCM96]="192 bit CAMELLIA-GCM with 96 bit ICV" | |
79 | [CAMELLIA128-GCM96]="128 bit CAMELLIA-GCM with 96 bit ICV" | |
80 | [CAMELLIA256-GCM64]="256 bit CAMELLIA-GCM with 64 bit ICV" | |
81 | [CAMELLIA192-GCM64]="192 bit CAMELLIA-GCM with 64 bit ICV" | |
82 | [CAMELLIA128-GCM64]="128 bit CAMELLIA-GCM with 64 bit ICV" | |
83 | ||
84 | # CAMELLIA-CCM | |
85 | [CAMELLIA256-CCM128]="256 bit CAMELLIA-CCM with 128 bit ICV" | |
86 | [CAMELLIA192-CCM128]="192 bit CAMELLIA-CCM with 128 bit ICV" | |
87 | [CAMELLIA128-CCM128]="128 bit CAMELLIA-CCM with 128 bit ICV" | |
88 | [CAMELLIA256-CCM96]="256 bit CAMELLIA-CCM with 96 bit ICV" | |
89 | [CAMELLIA192-CCM96]="192 bit CAMELLIA-CCM with 96 bit ICV" | |
90 | [CAMELLIA128-CCM96]="128 bit CAMELLIA-CCM with 96 bit ICV" | |
91 | [CAMELLIA256-CCM64]="256 bit CAMELLIA-CCM with 64 bit ICV" | |
92 | [CAMELLIA192-CCM64]="192 bit CAMELLIA-CCM with 64 bit ICV" | |
93 | [CAMELLIA128-CCM64]="128 bit CAMELLIA-CCM with 64 bit ICV" | |
b116ad92 MT |
94 | ) |
95 | ||
3a0c376c MT |
96 | declare -A VPN_SUPPORTED_INTEGRITY=( |
97 | [MD5]="MD5-HMAC" | |
98 | ||
99 | # SHA | |
100 | [SHA1]="SHA1-HMAC" | |
3003747b | 101 | [SHA512]="512 bit SHA2-HMAC" |
3a0c376c MT |
102 | [SHA384]="384 bit SHA2-HMAC" |
103 | [SHA256]="256 bit SHA2-HMAC" | |
104 | ||
105 | # AES | |
106 | [AES-XCBC]="AES-XCBC" | |
107 | [AES-CMAC]="AES-CMAC" | |
108 | [AES256-GMAC]="256 bit AES-GMAC" | |
109 | [AES192-GMAC]="192 bit AES-GMAC" | |
110 | [AES128-GMAC]="128 bit AES-GMAC" | |
111 | ) | |
112 | ||
54fc6c3f MT |
113 | declare -A VPN_SUPPORTED_GROUP_TYPES=( |
114 | # Regular Groups | |
115 | [MODP768]="768 bit Modulo Prime Group" | |
116 | [MODP1024]="1024 bit Modulo Prime Group" | |
117 | [MODP1536]="1536 bit Modulo Prime Group" | |
118 | [MODP2048]="2048 bit Modulo Prime Group" | |
119 | [MODP3072]="3072 bit Modulo Prime Group" | |
120 | [MODP4096]="4096 bit Modulo Prime Group" | |
121 | [MODP6144]="6144 bit Modulo Prime Group" | |
122 | [MODP8192]="8192 bit Modulo Prime Group" | |
123 | ||
124 | # NIST Elliptic Curve Groups | |
125 | [ECP192]="192 bit NIST Elliptic Curve Group" | |
126 | [ECP224]="224 bit NIST Elliptic Curve Group" | |
127 | [ECP256]="256 bit NIST Elliptic Curve Group" | |
128 | [ECP384]="384 bit NIST Elliptic Curve Group" | |
129 | [ECP521]="521 bit NIST Elliptic Curve Group" | |
130 | ||
131 | # Brainpool Elliptic Curve Groups | |
132 | [ECP224BP]="224 bit Brainpool Elliptic Curve Group" | |
133 | [ECP256BP]="256 bit Brainpool Elliptic Curve Group" | |
134 | [ECP384BP]="384 bit Brainpool Elliptic Curve Group" | |
135 | [ECP512BP]="512 bit Brainpool Elliptic Curve Group" | |
136 | ||
137 | # Curve25519 | |
138 | [CURVE25519]="256 bit Elliptic Curve 25519" | |
139 | ) | |
3eae7bed | 140 | |
6740c9c5 MT |
141 | # This functions checks if a policy is readonly |
142 | # returns true when yes and false when no | |
3eae7bed | 143 | vpn_security_policies_check_readonly() { |
3eae7bed JS |
144 | if isoneof name ${VPN_SECURITY_POLICIES_READONLY}; then |
145 | return ${EXIT_TRUE} | |
146 | else | |
147 | return ${EXIT_FALSE} | |
148 | fi | |
149 | } | |
150 | ||
6740c9c5 | 151 | # This function writes all values to a via ${name} specificated vpn security policy configuration file |
3eae7bed | 152 | vpn_security_policies_write_config() { |
3eae7bed JS |
153 | assert [ $# -ge 1 ] |
154 | ||
155 | local name="${1}" | |
156 | ||
6740c9c5 | 157 | if ! vpn_security_policy_exists "${name}"; then |
3eae7bed JS |
158 | log ERROR "No such vpn security policy: ${name}" |
159 | return ${EXIT_ERROR} | |
160 | fi | |
161 | ||
6740c9c5 | 162 | if vpn_security_policies_check_readonly "${name}"; then |
3eae7bed JS |
163 | log ERROR "The ${name} vpn security policy cannot be changed." |
164 | return ${EXIT_ERROR} | |
165 | fi | |
166 | ||
6740c9c5 | 167 | local path="$(vpn_security_policies_path "${name}")" |
3eae7bed JS |
168 | if [ ! -w ${path} ]; then |
169 | log ERROR "${path} is not writeable" | |
170 | return ${EXIT_ERROR} | |
171 | fi | |
172 | ||
173 | if ! settings_write "${path}" ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}; then | |
174 | log ERROR "Could not write configuration settings for vpn security policy ${name}" | |
175 | return ${EXIT_ERROR} | |
176 | fi | |
177 | ||
178 | # TODO everytime we successfully write a config we should call some trigger to take the changes into effect | |
179 | } | |
180 | ||
6740c9c5 | 181 | # This funtion writes the value for one key to a via ${name} specificated vpn security policy configuration file |
3eae7bed | 182 | vpn_security_policies_write_config_key() { |
3eae7bed | 183 | assert [ $# -ge 3 ] |
6740c9c5 | 184 | |
3eae7bed JS |
185 | local name=${1} |
186 | local key=${2} | |
187 | shift 2 | |
6740c9c5 | 188 | |
3eae7bed JS |
189 | local value="$@" |
190 | ||
6740c9c5 | 191 | if ! vpn_security_policy_exists "${name}"; then |
3eae7bed JS |
192 | log ERROR "No such vpn security policy: ${name}" |
193 | return ${EXIT_ERROR} | |
194 | fi | |
195 | ||
196 | log DEBUG "Set '${key}' to new value '${value}' in vpn security policy ${name}" | |
197 | ||
198 | local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS} | |
199 | ||
200 | # Read the config settings | |
6740c9c5 | 201 | if ! vpn_security_policies_read_config "${name}"; then |
3eae7bed JS |
202 | return ${EXIT_ERROR} |
203 | fi | |
204 | ||
205 | # Set the key to a new value | |
206 | assign "${key}" "${value}" | |
207 | ||
6740c9c5 | 208 | if ! vpn_security_policies_write_config "${name}"; then |
3eae7bed JS |
209 | return ${EXIT_ERROR} |
210 | fi | |
211 | ||
212 | return ${EXIT_TRUE} | |
3eae7bed JS |
213 | } |
214 | ||
6740c9c5 | 215 | # Reads one or more keys out of a settings file or all if no key is provided. |
3eae7bed | 216 | vpn_security_policies_read_config() { |
3eae7bed JS |
217 | assert [ $# -ge 1 ] |
218 | ||
219 | local name="${1}" | |
220 | shift 1 | |
221 | ||
6740c9c5 | 222 | if ! vpn_security_policy_exists "${name}"; then |
3eae7bed JS |
223 | log ERROR "No such vpn security policy: ${name}" |
224 | return ${EXIT_ERROR} | |
225 | fi | |
226 | ||
227 | ||
228 | local args | |
229 | if [ $# -eq 0 ] && [ -n "${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}" ]; then | |
230 | list_append args ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS} | |
231 | else | |
232 | list_append args $@ | |
233 | fi | |
234 | ||
235 | local path="$(vpn_security_policies_path ${name})" | |
236 | ||
237 | if ! settings_read "${path}" ${args}; then | |
238 | log ERROR "Could not read settings for vpn security policy ${name}" | |
239 | return ${EXIT_ERROR} | |
240 | fi | |
241 | } | |
242 | ||
6740c9c5 | 243 | # Returns the path to a the configuration fora given name |
3eae7bed | 244 | vpn_security_policies_path() { |
3eae7bed | 245 | assert [ $# -eq 1 ] |
6740c9c5 | 246 | |
3eae7bed JS |
247 | local name=${1} |
248 | ||
6740c9c5 | 249 | if vpn_security_policies_check_readonly "${name}"; then |
3eae7bed JS |
250 | echo "${NETWORK_SHARE_DIR}/vpn/security-policies/${name}" |
251 | else | |
252 | echo "${NETWORK_CONFIG_DIR}/vpn/security-policies/${name}" | |
253 | fi | |
254 | } | |
255 | ||
6740c9c5 | 256 | # Print the content of a vpn security policy configuration file in a nice way |
3eae7bed | 257 | vpn_security_policies_show() { |
3eae7bed | 258 | assert [ $# -eq 1 ] |
6740c9c5 | 259 | |
3eae7bed JS |
260 | local name=${1} |
261 | ||
262 | local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS} | |
3eae7bed JS |
263 | if ! vpn_security_policies_read_config ${name}; then |
264 | return ${EXIT_ERROR} | |
265 | fi | |
266 | ||
267 | cli_print_fmt1 0 "Security Policy: ${name}" | |
268 | cli_space | |
269 | ||
270 | # This could be done in a loop but a loop is much more complicated | |
271 | # because we print 'Group Types' but the variable is named 'GROUP_TYPES' | |
272 | cli_print_fmt1 1 "Ciphers:" | |
d21db419 MT |
273 | local cipher |
274 | for cipher in ${CIPHER}; do | |
275 | cli_print_fmt1 2 "${VPN_SUPPORTED_CIPHERS[${cipher}]-${cipher}}" | |
276 | done | |
3eae7bed | 277 | cli_space |
6740c9c5 | 278 | |
3eae7bed | 279 | cli_print_fmt1 1 "Integrity:" |
f3098f76 MT |
280 | local integrity |
281 | for integrity in ${INTEGRITY}; do | |
282 | cli_print_fmt1 2 "${VPN_SUPPORTED_INTEGRITY[${integrity}]-${integrity}}" | |
283 | done | |
3eae7bed | 284 | cli_space |
6740c9c5 | 285 | |
3eae7bed | 286 | cli_print_fmt1 1 "Group Types:" |
84c1cc17 MT |
287 | local group_type |
288 | for group_type in ${GROUP_TYPE}; do | |
289 | cli_print_fmt1 2 "${VPN_SUPPORTED_GROUP_TYPES[${group_type}]-${group_type}}" | |
290 | done | |
3eae7bed JS |
291 | cli_space |
292 | ||
293 | cli_print_fmt1 1 "Key Exchange:" "${KEY_EXCHANGE}" | |
6740c9c5 MT |
294 | |
295 | # Key Lifetime | |
3eae7bed JS |
296 | if isinteger LIFETIME && [ ${LIFETIME} -gt 0 ]; then |
297 | cli_print_fmt1 1 "Key Lifetime:" "$(format_time ${LIFETIME})" | |
298 | else | |
299 | log ERROR "The value for Key Lifetime is not a valid integer greater zero." | |
300 | fi | |
6740c9c5 MT |
301 | |
302 | # PFS | |
3eae7bed JS |
303 | if enabled PFS; then |
304 | cli_print_fmt1 1 "Perfect Forward Secrecy:" "enabled" | |
305 | else | |
306 | cli_print_fmt1 1 "Perfect Forward Secrecy:" "disabled" | |
307 | fi | |
308 | cli_space | |
6740c9c5 MT |
309 | |
310 | # Compression | |
3eae7bed JS |
311 | if enabled COMPRESSION; then |
312 | cli_print_fmt1 1 "Compression:" "enabled" | |
313 | else | |
314 | cli_print_fmt1 1 "Compression:" "disabled" | |
315 | fi | |
316 | cli_space | |
317 | } | |
318 | ||
6740c9c5 MT |
319 | # This function checks if a vpn security policy exists |
320 | # Returns True when yes and false when not | |
3eae7bed | 321 | vpn_security_policy_exists() { |
3eae7bed | 322 | assert [ $# -eq 1 ] |
6740c9c5 | 323 | |
3eae7bed JS |
324 | local name=${1} |
325 | ||
6740c9c5 MT |
326 | local path=$(vpn_security_policies_path "${name}") |
327 | ||
328 | [ -f ${path} ] && return ${EXIT_TRUE} || return ${EXIT_FALSE} | |
3eae7bed JS |
329 | } |
330 | ||
331 | ||
6740c9c5 | 332 | # This function parses the parameters for the 'cipher' command |
3eae7bed | 333 | vpn_security_policies_cipher(){ |
3eae7bed JS |
334 | local name=${1} |
335 | shift | |
336 | ||
337 | if [ $# -eq 0 ]; then | |
338 | log ERROR "You must pass at least one value after cipher" | |
339 | return ${EXIT_ERROR} | |
340 | fi | |
341 | ||
342 | local CIPHER | |
3eae7bed JS |
343 | if ! vpn_security_policies_read_config ${name} "CIPHER"; then |
344 | return ${EXIT_ERROR} | |
345 | fi | |
346 | ||
347 | # Remove duplicated entries to proceed the list safely | |
348 | CIPHER="$(list_unique ${CIPHER})" | |
349 | ||
350 | while [ $# -gt 0 ]; do | |
351 | case "${1}" in | |
352 | -*) | |
353 | value=${1#-} | |
354 | # Check if the cipher is in the list of ciphers and | |
355 | # check if the list has after removing this cipher at least one valid value | |
356 | if list_match ${value} ${CIPHER}; then | |
357 | list_remove CIPHER ${value} | |
358 | else | |
359 | # We do not break here because this error does not break the processing of the next maybe valid values. | |
360 | log ERROR "Can not remove ${value} from the list of Ciphers because ${value} is not in the list." | |
361 | fi | |
362 | ;; | |
363 | +*) | |
364 | value=${1#+} | |
365 | # Check if the Ciphers is in the list of supported ciphers. | |
b116ad92 | 366 | if ! isoneof value ${!VPN_SUPPORTED_CIPHERS[@]}; then |
3eae7bed JS |
367 | # We do not break here because this error does not break the processing of the next maybe valid values. |
368 | log ERROR "${value} is not a supported cipher and can thats why not added to the list of ciphers." | |
369 | else | |
370 | if list_match ${value} ${CIPHER}; then | |
371 | log WARNING "${value} is already in the list of ciphers of this policy." | |
372 | else | |
373 | list_append CIPHER ${value} | |
374 | fi | |
375 | fi | |
376 | ;; | |
377 | esac | |
378 | shift | |
379 | done | |
380 | ||
381 | # Check if the list contain at least one valid cipher | |
382 | if [ $(list_length ${CIPHER}) -ge 1 ]; then | |
383 | if ! vpn_security_policies_write_config_key ${name} "CIPHER" ${CIPHER}; then | |
384 | log ERROR "The changes for the vpn security policy ${name} could not be written." | |
385 | fi | |
386 | else | |
387 | log ERROR "After proceding all ciphers the list is empty and thats why no changes are written." | |
388 | return ${EXIT_ERROR} | |
389 | fi | |
390 | } | |
391 | ||
6740c9c5 | 392 | # This function parses the parameters for the 'compression' command |
3eae7bed | 393 | vpn_security_policies_compression(){ |
3eae7bed JS |
394 | local name=${1} |
395 | local value=${2} | |
396 | ||
397 | # Check if we get only one argument after compression <name> | |
398 | if [ ! $# -eq 2 ]; then | |
399 | log ERROR "The number of arguments do not match. Only one argument after compression is allowed." | |
400 | return ${EXIT_ERROR} | |
401 | fi | |
402 | ||
403 | if ! isbool value; then | |
404 | # We suggest only two values to avoid overburding the user. | |
405 | log ERROR "Invalid Argument ${value}" | |
406 | return ${EXIT_ERROR} | |
407 | fi | |
408 | ||
409 | vpn_security_policies_write_config_key "${name}" "COMPRESSION" "${value}" | |
410 | } | |
411 | ||
6740c9c5 | 412 | # This function parses the parameters for the 'group-type' command |
3eae7bed | 413 | vpn_security_policies_group_type(){ |
3eae7bed JS |
414 | local name=${1} |
415 | shift | |
416 | ||
417 | if [ $# -eq 0 ]; then | |
418 | log ERROR "You must pass at least one value after group-type" | |
419 | return ${EXIT_ERROR} | |
420 | fi | |
421 | ||
422 | local GROUP_TYPE | |
3eae7bed JS |
423 | if ! vpn_security_policies_read_config ${name} "GROUP_TYPE"; then |
424 | return ${EXIT_ERROR} | |
425 | fi | |
426 | ||
427 | # Remove duplicated entries to proceed the list safely | |
428 | GROUP_TYPE="$(list_unique ${GROUP_TYPE})" | |
429 | ||
430 | while [ $# -gt 0 ]; do | |
431 | case "${1}" in | |
432 | -*) | |
433 | value=${1#-} | |
434 | # Check if the group type is in the list of group types and | |
435 | # check if the list has after removing this group type at leatst one valid value | |
436 | if list_match ${value} ${GROUP_TYPE}; then | |
437 | list_remove GROUP_TYPE ${value} | |
438 | else | |
439 | # We do not break here because this error does not break the processing of the next maybe valid values. | |
440 | log ERROR "Can not remove ${value} from the list of group types because ${value} is not in the list." | |
441 | fi | |
442 | ;; | |
443 | +*) | |
444 | value=${1#+} | |
445 | # Check if the group type is in the list of supported group types. | |
54fc6c3f | 446 | if ! isoneof value ${!VPN_SUPPORTED_GROUP_TYPES[@]}; then |
3eae7bed JS |
447 | # We do not break here because the processing of other maybe valid values are indepent from this error. |
448 | log ERROR "${value} is not a supported group type and can thats why not added to the list of group types." | |
449 | else | |
450 | if list_match ${value} ${GROUP_TYPE}; then | |
451 | log WARNING "${value} is already in the list of group-types of this policy." | |
452 | else | |
453 | list_append GROUP_TYPE ${value} | |
454 | fi | |
455 | fi | |
456 | ;; | |
457 | esac | |
458 | shift | |
459 | done | |
460 | ||
461 | # Check if the list contain at least one valid group-type | |
462 | if [ $(list_length ${GROUP_TYPE}) -ge 1 ]; then | |
463 | if ! vpn_security_policies_write_config_key ${name} "GROUP_TYPE" ${GROUP_TYPE}; then | |
464 | log ERROR "The changes for the vpn security policy ${name} could not be written." | |
465 | fi | |
466 | else | |
467 | log ERROR "After proceding all group types the list is empty and thats why no changes are written." | |
468 | return ${EXIT_ERROR} | |
469 | fi | |
470 | } | |
6740c9c5 MT |
471 | |
472 | # This function parses the parameters for the 'integrity' command | |
3eae7bed | 473 | vpn_security_policies_integrity(){ |
3eae7bed JS |
474 | local name=${1} |
475 | shift | |
476 | ||
477 | if [ $# -eq 0 ]; then | |
478 | log ERROR "You must pass at least one value after integrity." | |
479 | return ${EXIT_ERROR} | |
480 | fi | |
481 | ||
482 | local INTEGRITY | |
3eae7bed JS |
483 | if ! vpn_security_policies_read_config ${name} "INTEGRITY"; then |
484 | return ${EXIT_ERROR} | |
485 | fi | |
486 | ||
487 | # Remove duplicated entries to proceed the list safely | |
488 | INTEGRITY="$(list_unique ${INTEGRITY})" | |
489 | ||
490 | while [ $# -gt 0 ]; do | |
491 | case "${1}" in | |
492 | -*) | |
493 | value=${1#-} | |
494 | # Check if the integrity hash is in the list of integrity hashes and | |
495 | # check if the list has after removing this integrity hash at least one valid value | |
496 | if list_match ${value} ${INTEGRITY}; then | |
497 | list_remove INTEGRITY ${value} | |
498 | else | |
499 | # We do not break here because the processing of other maybe valid values are indepent from this error. | |
500 | log ERROR "Can not remove ${value} from the list of integrity hashes because ${value} is not in the list." | |
501 | fi | |
502 | ;; | |
503 | +*) | |
504 | value=${1#+} | |
505 | # Check if the Ciphers is in the list of supported integrity hashes. | |
3a0c376c | 506 | if ! isoneof value ${!VPN_SUPPORTED_INTEGRITY[@]}; then |
3eae7bed JS |
507 | # We do not break here because the processing of other maybe valid values are indepent from this error. |
508 | log ERROR "${value} is not a supported integrity hash and can thats why not added to the list of integrity hashes." | |
509 | else | |
510 | if list_match ${value} ${INTEGRITY}; then | |
511 | log WARNING "${value} is already in the list of integrety hashes of this policy." | |
512 | else | |
513 | list_append INTEGRITY ${value} | |
514 | fi | |
515 | fi | |
516 | ;; | |
517 | esac | |
518 | shift | |
519 | done | |
520 | ||
521 | # Check if the list contain at least one valid group-type | |
522 | if [ $(list_length ${INTEGRITY}) -ge 1 ]; then | |
523 | if ! vpn_security_policies_write_config_key ${name} "INTEGRITY" ${INTEGRITY}; then | |
524 | log ERROR "The changes for the vpn security policy ${name} could not be written." | |
525 | fi | |
526 | else | |
527 | log ERROR "After proceding all integrity hashes the list is empty and thats why no changes are written." | |
528 | return ${EXIT_ERROR} | |
529 | fi | |
3eae7bed JS |
530 | } |
531 | ||
6740c9c5 | 532 | # This function parses the parameters for the 'key-exchange' command |
3eae7bed | 533 | vpn_security_policies_key_exchange() { |
3eae7bed JS |
534 | local name=${1} |
535 | local value=${2} | |
6740c9c5 | 536 | |
3eae7bed JS |
537 | # Check if we get only one argument after key-exchange <name> |
538 | if [ ! $# -eq 2 ]; then | |
539 | log ERROR "The number of arguments do not match. Only argument after key-exchange is allowed." | |
540 | return ${EXIT_ERROR} | |
541 | fi | |
542 | ||
3eae7bed JS |
543 | if ! isoneof value "ikev1" "ikev2" "IKEV1" "IKEV2"; then |
544 | log ERROR "Invalid Argument ${value}" | |
545 | return ${EXIT_ERROR} | |
546 | fi | |
547 | ||
548 | vpn_security_policies_write_config_key "${name}" "KEY_EXCHANGE" "${value,,}" | |
549 | } | |
550 | ||
6740c9c5 | 551 | # This function parses the parameters for the 'lifetime' command. |
3eae7bed | 552 | vpn_security_policies_lifetime(){ |
3eae7bed JS |
553 | local name=${1} |
554 | shift | |
6740c9c5 | 555 | |
3eae7bed JS |
556 | local value=$@ |
557 | ||
558 | # Check if we get only one argument after lifetime <name> | |
559 | if [ ! $# -ge 1 ]; then | |
560 | 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" | |
561 | return ${EXIT_ERROR} | |
562 | fi | |
563 | ||
564 | if ! isinteger value; then | |
565 | value=$(parse_time $@) | |
566 | if [ ! $? -eq 0 ]; then | |
567 | log ERROR "Parsing the passed time was not sucessful please check the passed values." | |
568 | return ${EXIT_ERROR} | |
569 | fi | |
570 | fi | |
571 | ||
572 | if [ ${value} -le 0 ]; then | |
573 | log ERROR "The passed time value must be in the sum greater zero seconds." | |
574 | return ${EXIT_ERROR} | |
575 | fi | |
576 | ||
577 | vpn_security_policies_write_config_key "${name}" "LIFETIME" "${value}" | |
578 | } | |
579 | ||
6740c9c5 | 580 | # This function parses the parameters for the 'pfs' command |
3eae7bed | 581 | vpn_security_policies_pfs(){ |
3eae7bed JS |
582 | local name=${1} |
583 | local value=${2} | |
584 | ||
585 | # Check if we get only one argument after pfs <name> | |
586 | if [ ! $# -eq 2 ]; then | |
587 | log ERROR "The number of arguments do not match. Only argument after pfs is allowed." | |
588 | return ${EXIT_ERROR} | |
589 | fi | |
590 | ||
591 | if [ ! $# -eq 2 ] || ! isbool value; then | |
592 | # We suggest only two values to avoid overburding the user. | |
593 | log ERROR "Invalid Argument ${value}" | |
594 | return ${EXIT_ERROR} | |
595 | fi | |
596 | ||
597 | vpn_security_policies_write_config_key "${name}" "PFS" "${value}" | |
598 | } | |
599 | ||
6740c9c5 MT |
600 | # This function checks if a vpn security policy name is valid |
601 | # Allowed are only A-Za-z0-9 | |
3eae7bed | 602 | vpn_security_policies_check_name() { |
3eae7bed | 603 | assert [ $# -eq 1 ] |
6740c9c5 | 604 | |
3eae7bed | 605 | local name=${1} |
6740c9c5 | 606 | |
3eae7bed JS |
607 | [[ ${name} =~ [^[:alnum:]$] ]] |
608 | } | |
609 | ||
6740c9c5 | 610 | # Function that creates based on the paramters one ore more new vpn security policies |
3eae7bed | 611 | vpn_security_policies_new() { |
58aa0edf MT |
612 | if [ $# -gt 1 ]; then |
613 | error "Too many arguments" | |
3eae7bed JS |
614 | return ${EXIT_ERROR} |
615 | fi | |
616 | ||
58aa0edf MT |
617 | local name="${1}" |
618 | if ! isset name; then | |
619 | error "Please provide a name" | |
620 | return ${EXIT_ERROR} | |
621 | fi | |
3eae7bed | 622 | |
58aa0edf MT |
623 | # Check for duplicates |
624 | if vpn_security_policy_exists "${name}"; then | |
625 | error "The VPN security policy with name ${name} already exists" | |
626 | return ${EXIT_ERROR} | |
627 | fi | |
3eae7bed | 628 | |
58aa0edf MT |
629 | # Check if name is valid |
630 | if vpn_security_policies_check_name "${name}"; then | |
631 | error "'${name}' contains illegal characters" | |
632 | return ${EXIT_ERROR} | |
633 | fi | |
3eae7bed | 634 | |
58aa0edf MT |
635 | # Check if we have a read-only policy with the same name |
636 | if vpn_security_policies_check_readonly "${name}"; then | |
637 | error "The VPN security policy ${name} is read-only" | |
638 | return ${EXIT_ERROR} | |
639 | fi | |
640 | ||
6fdbda80 MT |
641 | # Check if our source policy exists |
642 | if ! vpn_security_policy_exists "${VPN_DEFAULT_SECURITY_POLICY}"; then | |
643 | error "Default VPN Security Policy '${VPN_DEFAULT_SECURITY_POLICY}' does not exist" | |
644 | return ${EXIT_ERROR} | |
645 | fi | |
646 | ||
58aa0edf MT |
647 | log DEBUG "Creating VPN Security Policy ${name}" |
648 | ||
aab75c48 MT |
649 | if copy "$(vpn_security_policies_path "${VPN_DEFAULT_SECURITY_POLICY}")" |
650 | "$(vpn_security_policies_path ${name})"; then | |
58aa0edf MT |
651 | log INFO "VPN Security Policy ${name} successfully created" |
652 | else | |
653 | log ERROR "Could not create VPN Security Policy ${name}" | |
654 | return ${EXIT_ERROR} | |
655 | fi | |
c787fea5 MT |
656 | |
657 | # Show the newly created policy | |
658 | vpn_security_policies_show "${name}" | |
3eae7bed JS |
659 | } |
660 | ||
6740c9c5 | 661 | # Function that deletes based on the passed parameters one ore more vpn security policies |
3eae7bed | 662 | vpn_security_policies_destroy() { |
3eae7bed JS |
663 | local name |
664 | for name in $@; do | |
665 | if ! vpn_security_policy_exists ${name}; then | |
666 | log ERROR "The vpn security policy ${name} does not exist." | |
667 | continue | |
668 | fi | |
669 | ||
670 | if vpn_security_policies_check_readonly ${name}; then | |
671 | log ERROR "The vpn security policy ${name} cannot be deleted." | |
672 | continue | |
673 | fi | |
674 | ||
675 | log DEBUG "Deleting vpn security policy ${name}" | |
676 | settings_remove $(vpn_security_policies_path ${name}) | |
677 | done | |
678 | } |