summaryrefslogtreecommitdiff
path: root/src/conf_mode/interfaces-openvpn.py
blob: 6918c46e043ba6b4916c6a2a5083f6b375112d12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#!/usr/bin/env python3
#
# Copyright (C) 2019-2020 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import re
import tempfile

from cryptography.hazmat.primitives.asymmetric import ec
from glob import glob
from sys import exit
from ipaddress import IPv4Address
from ipaddress import IPv4Network
from ipaddress import IPv6Address
from ipaddress import IPv6Network
from ipaddress import summarize_address_range
from pathlib import Path
from netifaces import interfaces
from secrets import SystemRandom
from shutil import rmtree

from vyos.config import Config
from vyos.configdict import get_interface_dict
from vyos.configverify import verify_vrf
from vyos.configverify import verify_bridge_delete
from vyos.ifconfig import VTunIf
from vyos.pki import load_dh_parameters
from vyos.pki import load_private_key
from vyos.pki import wrap_certificate
from vyos.pki import wrap_crl
from vyos.pki import wrap_dh_parameters
from vyos.pki import wrap_openvpn_key
from vyos.pki import wrap_private_key
from vyos.template import render
from vyos.template import is_ipv4
from vyos.template import is_ipv6
from vyos.util import call
from vyos.util import chown
from vyos.util import dict_search
from vyos.util import dict_search_args
from vyos.util import write_file
from vyos.validate import is_addr_assigned

from vyos import ConfigError
from vyos import airbag
airbag.enable()

user = 'openvpn'
group = 'openvpn'

cfg_dir = '/run/openvpn'
cfg_file = '/run/openvpn/{ifname}.conf'
otp_path = '/config/auth/openvpn'
otp_file = '/config/auth/openvpn/{ifname}-otp-secrets'
secret_chars = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567')

def get_config(config=None):
    """
    Retrive CLI config as dictionary. Dictionary can never be empty, as at least the
    interface name will be added or a deleted flag
    """
    if config:
        conf = config
    else:
        conf = Config()
    base = ['interfaces', 'openvpn']

    tmp_pki = conf.get_config_dict(['pki'], key_mangling=('-', '_'),
                                get_first_key=True, no_tag_node_value_mangle=True)

    # We have to get the dict using 'get_config_dict' instead of 'get_interface_dict'
    # as 'get_interface_dict' merges the defaults in, so we can not check for defaults in there.
    tmp_openvpn = conf.get_config_dict(base + [os.environ['VYOS_TAGNODE_VALUE']], key_mangling=('-', '_'),
                                get_first_key=True, no_tag_node_value_mangle=True)

    openvpn = get_interface_dict(conf, base)

    if 'deleted' not in openvpn:
        openvpn['pki'] = tmp_pki

    openvpn['auth_user_pass_file'] = '/run/openvpn/{ifname}.pw'.format(**openvpn)
    openvpn['daemon_user'] = user
    openvpn['daemon_group'] = group

    # We have to cleanup the config dict, as default values could enable features
    # which are not explicitly enabled on the CLI. Example: server mfa totp
    # originate comes with defaults, which will enable the
    # totp plugin, even when not set via CLI so we
    # need to check this first and drop those keys
    if 'totp' not in tmp_openvpn['server']:
        del openvpn['server']['mfa']['totp']

    return openvpn

def is_ec_private_key(pki, cert_name):
    if not pki or 'certificate' not in pki:
        return False
    if cert_name not in pki['certificate']:
        return False

    pki_cert = pki['certificate'][cert_name]
    if 'private' not in pki_cert or 'key' not in pki_cert['private']:
        return False

    key = load_private_key(pki_cert['private']['key'])
    return isinstance(key, ec.EllipticCurvePrivateKey)

def verify_pki(openvpn):
    pki = openvpn['pki']
    interface = openvpn['ifname']
    mode = openvpn['mode']
    shared_secret_key = dict_search_args(openvpn, 'shared_secret_key')
    tls = dict_search_args(openvpn, 'tls')

    if not bool(shared_secret_key) ^ bool(tls): #  xor check if only one is set
        raise ConfigError('Must specify only one of "shared-secret-key" and "tls"')

    if mode in ['server', 'client'] and not tls:
        raise ConfigError('Must specify "tls" for server and client modes')

    if not pki:
        raise ConfigError('PKI is not configured')

    if shared_secret_key:
        if not dict_search_args(pki, 'openvpn', 'shared_secret'):
            raise ConfigError('There are no openvpn shared-secrets in PKI configuration')

        if shared_secret_key not in pki['openvpn']['shared_secret']:
            raise ConfigError(f'Invalid shared-secret on openvpn interface {interface}')

    if tls:
        if 'ca_certificate' not in tls:
            raise ConfigError(f'Must specify "tls ca-certificate" on openvpn interface {interface}')

        if tls['ca_certificate'] not in pki['ca']:
            raise ConfigError(f'Invalid CA certificate on openvpn interface {interface}')

        if not (mode == 'client' and 'auth_key' in tls):
            if 'certificate' not in tls:
                raise ConfigError(f'Missing "tls certificate" on openvpn interface {interface}')

        if 'certificate' in tls:
            if tls['certificate'] not in pki['certificate']:
                raise ConfigError(f'Invalid certificate on openvpn interface {interface}')

            if dict_search_args(pki, 'certificate', tls['certificate'], 'private', 'password_protected'):
                raise ConfigError(f'Cannot use encrypted private key on openvpn interface {interface}')

            if mode == 'server' and 'dh_params' not in tls and not is_ec_private_key(pki, tls['certificate']):
                raise ConfigError('Must specify "tls dh-params" when not using EC keys in server mode')

        if 'dh_params' in tls:
            if 'dh' not in pki:
                raise ConfigError('There are no DH parameters in PKI configuration')

            if tls['dh_params'] not in pki['dh']:
                raise ConfigError(f'Invalid dh-params on openvpn interface {interface}')

            pki_dh = pki['dh'][tls['dh_params']]
            dh_params = load_dh_parameters(pki_dh['parameters'])
            dh_numbers = dh_params.parameter_numbers()
            dh_bits = dh_numbers.p.bit_length()

            if dh_bits < 2048:
                raise ConfigError(f'Minimum DH key-size is 2048 bits')

        if 'auth_key' in tls or 'crypt_key' in tls:
            if not dict_search_args(pki, 'openvpn', 'shared_secret'):
                raise ConfigError('There are no openvpn shared-secrets in PKI configuration')

        if 'auth_key' in tls:
            if tls['auth_key'] not in pki['openvpn']['shared_secret']:
                raise ConfigError(f'Invalid auth-key on openvpn interface {interface}')

        if 'crypt_key' in tls:
            if tls['crypt_key'] not in pki['openvpn']['shared_secret']:
                raise ConfigError(f'Invalid crypt-key on openvpn interface {interface}')

def verify(openvpn):
    if 'deleted' in openvpn:
        # remove totp secrets file if totp is not configured
        if os.path.isfile(otp_file.format(**openvpn)):
            os.remove(otp_file.format(**openvpn))

        verify_bridge_delete(openvpn)
        return None

    if 'mode' not in openvpn:
        raise ConfigError('Must specify OpenVPN operation mode!')

    #
    # OpenVPN client mode - VERIFY
    #
    if openvpn['mode'] == 'client':
        if 'local_port' in openvpn:
            raise ConfigError('Cannot specify "local-port" in client mode')

        if 'local_host' in openvpn:
            raise ConfigError('Cannot specify "local-host" in client mode')

        if 'remote_host' not in openvpn:
            raise ConfigError('Must specify "remote-host" in client mode')

        if openvpn['protocol'] == 'tcp-passive':
            raise ConfigError('Protocol "tcp-passive" is not valid in client mode')

        if dict_search('tls.dh_params', openvpn):
            raise ConfigError('Cannot specify "tls dh-params" in client mode')

    #
    # OpenVPN site-to-site - VERIFY
    #
    elif openvpn['mode'] == 'site-to-site':
        if 'local_address' not in openvpn and 'is_bridge_member' not in openvpn:
            raise ConfigError('Must specify "local-address" or add interface to bridge')

        if len([addr for addr in openvpn['local_address'] if is_ipv4(addr)]) > 1:
            raise ConfigError('Only one IPv4 local-address can be specified')

        if len([addr for addr in openvpn['local_address'] if is_ipv6(addr)]) > 1:
            raise ConfigError('Only one IPv6 local-address can be specified')

        if openvpn['device_type'] == 'tun':
            if 'remote_address' not in openvpn:
                raise ConfigError('Must specify "remote-address"')

        if 'remote_address' in openvpn:
            if len([addr for addr in openvpn['remote_address'] if is_ipv4(addr)]) > 1:
                raise ConfigError('Only one IPv4 remote-address can be specified')

            if len([addr for addr in openvpn['remote_address'] if is_ipv6(addr)]) > 1:
                raise ConfigError('Only one IPv6 remote-address can be specified')

            if not 'local_address' in openvpn:
                raise ConfigError('"remote-address" requires "local-address"')

            v4loAddr = [addr for addr in openvpn['local_address'] if is_ipv4(addr)]
            v4remAddr = [addr for addr in openvpn['remote_address'] if is_ipv4(addr)]
            if v4loAddr and not v4remAddr:
                raise ConfigError('IPv4 "local-address" requires IPv4 "remote-address"')
            elif v4remAddr and not v4loAddr:
                raise ConfigError('IPv4 "remote-address" requires IPv4 "local-address"')

            v6remAddr = [addr for addr in openvpn['remote_address'] if is_ipv6(addr)]
            v6loAddr = [addr for addr in openvpn['local_address'] if is_ipv6(addr)]
            if v6loAddr and not v6remAddr:
                raise ConfigError('IPv6 "local-address" requires IPv6 "remote-address"')
            elif v6remAddr and not v6loAddr:
                raise ConfigError('IPv6 "remote-address" requires IPv6 "local-address"')

            if (v4loAddr == v4remAddr) or (v6remAddr == v4remAddr):
                raise ConfigError('"local-address" and "remote-address" cannot be the same')

            if dict_search('local_host', openvpn) in dict_search('local_address', openvpn):
                raise ConfigError('"local-address" cannot be the same as "local-host"')

            if dict_search('remote_host', openvpn) in dict_search('remote_address', openvpn):
                raise ConfigError('"remote-address" and "remote-host" can not be the same')

        if openvpn['device_type'] == 'tap':
            # we can only have one local_address, this is ensured above
            v4addr = None
            for laddr in openvpn['local_address']:
                if is_ipv4(laddr):
                    v4addr = laddr
                    break

            if v4addr in openvpn['local_address'] and 'subnet_mask' not in openvpn['local_address'][v4addr]:
                raise ConfigError('Must specify IPv4 "subnet-mask" for local-address')

        if dict_search('encryption.ncp_ciphers', openvpn):
            raise ConfigError('NCP ciphers can only be used in client or server mode')

    else:
        # checks for client-server or site-to-site bridged
        if 'local_address' in openvpn or 'remote_address' in openvpn:
            raise ConfigError('Cannot specify "local-address" or "remote-address" ' \
                              'in client/server or bridge mode')

    #
    # OpenVPN server mode - VERIFY
    #
    if openvpn['mode'] == 'server':
        if openvpn['protocol'] == 'tcp-active':
            raise ConfigError('Protocol "tcp-active" is not valid in server mode')

        if 'remote_port' in openvpn:
            raise ConfigError('Cannot specify "remote-port" in server mode')

        if 'remote_host' in openvpn:
            raise ConfigError('Cannot specify "remote-host" in server mode')

        tmp = dict_search('server.subnet', openvpn)
        if tmp:
            v4_subnets = len([subnet for subnet in tmp if is_ipv4(subnet)])
            v6_subnets = len([subnet for subnet in tmp if is_ipv6(subnet)])
            if v4_subnets > 1:
                raise ConfigError('Cannot specify more than 1 IPv4 server subnet')
            if v6_subnets > 1:
                raise ConfigError('Cannot specify more than 1 IPv6 server subnet')

            if v6_subnets > 0 and v4_subnets == 0:
                raise ConfigError('IPv6 server requires an IPv4 server subnet')

            for subnet in tmp:
                if is_ipv4(subnet):
                    subnet = IPv4Network(subnet)

                    if openvpn['device_type'] == 'tun' and subnet.prefixlen > 29:
                        raise ConfigError('Server subnets smaller than /29 with device type "tun" are not supported')
                    elif openvpn['device_type'] == 'tap' and subnet.prefixlen > 30:
                        raise ConfigError('Server subnets smaller than /30 with device type "tap" are not supported')

                    for client in (dict_search('client', openvpn) or []):
                        if client['ip'] and not IPv4Address(client['ip'][0]) in subnet:
                            raise ConfigError(f'Client "{client["name"]}" IP {client["ip"][0]} not in server subnet {subnet}')

        else:
            if 'is_bridge_member' not in openvpn:
                raise ConfigError('Must specify "server subnet" or add interface to bridge in server mode')

        if hasattr(dict_search('server.client', openvpn), '__iter__'):
            for client_k, client_v in dict_search('server.client', openvpn).items():
                if (client_v.get('ip') and len(client_v['ip']) > 1) or (client_v.get('ipv6_ip') and len(client_v['ipv6_ip']) > 1):
                    raise ConfigError(f'Server client "{client_k}": cannot specify more than 1 IPv4 and 1 IPv6 IP')

        if dict_search('server.client_ip_pool', openvpn):
            if not (dict_search('server.client_ip_pool.start', openvpn) and dict_search('server.client_ip_pool.stop', openvpn)):
                raise ConfigError('Server client-ip-pool requires both start and stop addresses')
            else:
                v4PoolStart = IPv4Address(dict_search('server.client_ip_pool.start', openvpn))
                v4PoolStop = IPv4Address(dict_search('server.client_ip_pool.stop', openvpn))
                if v4PoolStart > v4PoolStop:
                    raise ConfigError(f'Server client-ip-pool start address {v4PoolStart} is larger than stop address {v4PoolStop}')

                v4PoolSize = int(v4PoolStop) - int(v4PoolStart)
                if v4PoolSize >= 65536:
                    raise ConfigError(f'Server client-ip-pool is too large [{v4PoolStart} -> {v4PoolStop} = {v4PoolSize}], maximum is 65536 addresses.')

                v4PoolNets = list(summarize_address_range(v4PoolStart, v4PoolStop))
                for client in (dict_search('client', openvpn) or []):
                    if client['ip']:
                        for v4PoolNet in v4PoolNets:
                            if IPv4Address(client['ip'][0]) in v4PoolNet:
                                print(f'Warning: Client "{client["name"]}" IP {client["ip"][0]} is in server IP pool, it is not reserved for this client.')

        for subnet in (dict_search('server.subnet', openvpn) or []):
            if is_ipv6(subnet):
                tmp = dict_search('client_ipv6_pool.base', openvpn)
                if tmp:
                    if not dict_search('server.client_ip_pool', openvpn):
                        raise ConfigError('IPv6 server pool requires an IPv4 server pool')

                    if int(tmp.split('/')[1]) >= 112:
                        raise ConfigError('IPv6 server pool must be larger than /112')

                    #
                    # todo - weird logic
                    #
                    v6PoolStart = IPv6Address(tmp)
                    v6PoolStop = IPv6Network((v6PoolStart, openvpn['server_ipv6_pool_prefixlen']), strict=False)[-1] # don't remove the parentheses, it's a 2-tuple
                    v6PoolSize = int(v6PoolStop) - int(v6PoolStart) if int(openvpn['server_ipv6_pool_prefixlen']) > 96 else 65536
                    if v6PoolSize < v4PoolSize:
                        raise ConfigError(f'IPv6 server pool must be at least as large as the IPv4 pool (current sizes: IPv6={v6PoolSize} IPv4={v4PoolSize})')

                    v6PoolNets = list(summarize_address_range(v6PoolStart, v6PoolStop))
                    for client in (dict_search('client', openvpn) or []):
                        if client['ipv6_ip']:
                            for v6PoolNet in v6PoolNets:
                                if IPv6Address(client['ipv6_ip'][0]) in v6PoolNet:
                                    print(f'Warning: Client "{client["name"]}" IP {client["ipv6_ip"][0]} is in server IP pool, it is not reserved for this client.')

        # add mfa users to the file the mfa plugin uses
        if dict_search('server.mfa.totp', openvpn):
            if not Path(otp_file.format(**openvpn)).is_file():
                Path(otp_path).mkdir(parents=True, exist_ok=True)
                Path(otp_file.format(**openvpn)).touch()

            with tempfile.TemporaryFile(mode='w+') as fp:
                with open(otp_file.format(**openvpn), 'r+') as f:
                    ovpn_users = f.readlines()
                    for client in (dict_search('server.client', openvpn) or []):
                        exists = None
                        for ovpn_user in ovpn_users:
                            if re.search('^' + client + ' ', ovpn_user):
                                fp.write(ovpn_user)
                                exists = 'true'

                        if not exists:
                            random = SystemRandom()
                            totp_secret = ''.join(random.choice(secret_chars) for _ in range(16))
                            fp.write("{0} otp totp:sha1:base32:{1}::xxx *\n".format(client, totp_secret))

                    f.seek(0)
                    fp.seek(0)
                    for tmp_user in fp.readlines():
                        f.write(tmp_user)
                    f.truncate()

            chown(otp_file.format(**openvpn), user, group)

    else:
        # checks for both client and site-to-site go here
        if dict_search('server.reject_unconfigured_clients', openvpn):
            raise ConfigError('Option reject-unconfigured-clients only supported in server mode')

        if 'replace_default_route' in openvpn and 'remote_host' not in openvpn:
            raise ConfigError('Cannot set "replace-default-route" without "remote-host"')

    #
    # OpenVPN common verification section
    # not depending on any operation mode
    #

    # verify specified IP address is present on any interface on this system
    if 'local_host' in openvpn:
        if not is_addr_assigned(openvpn['local_host']):
            raise ConfigError('local-host IP address "{local_host}" not assigned' \
                              ' to any interface'.format(**openvpn))

    # TCP active
    if openvpn['protocol'] == 'tcp-active':
        if 'local_port' in openvpn:
            raise ConfigError('Cannot specify "local-port" with "tcp-active"')

        if 'remote_host' not in openvpn:
            raise ConfigError('Must specify "remote-host" with "tcp-active"')

    #
    # TLS/encryption
    #
    if 'shared_secret_key' in openvpn:
        if dict_search('encryption.cipher', openvpn) in ['aes128gcm', 'aes192gcm', 'aes256gcm']:
            raise ConfigError('GCM encryption with shared-secret-key not supported')

    if 'tls' in openvpn:
        if {'auth_key', 'crypt_key'} <= set(openvpn['tls']):
            raise ConfigError('TLS auth and crypt keys are mutually exclusive')

        tmp = dict_search('tls.role', openvpn)
        if tmp:
            if openvpn['mode'] in ['client', 'server']:
                if not dict_search('tls.auth_key', openvpn):
                    raise ConfigError('Cannot specify "tls role" in client-server mode')

            if tmp == 'active':
                if openvpn['protocol'] == 'tcp-passive':
                    raise ConfigError('Cannot specify "tcp-passive" when "tls role" is "active"')

                if dict_search('tls.dh_params', openvpn):
                    raise ConfigError('Cannot specify "tls dh-params" when "tls role" is "active"')

            elif tmp == 'passive':
                if openvpn['protocol'] == 'tcp-active':
                    raise ConfigError('Cannot specify "tcp-active" when "tls role" is "passive"')

                if not dict_search('tls.dh_params', openvpn):
                    raise ConfigError('Must specify "tls dh-params" when "tls role" is "passive"')

        if 'certificate' in openvpn['tls'] and is_ec_private_key(openvpn['pki'], openvpn['tls']['certificate']):
            if 'dh_params' in openvpn['tls']:
                print('Warning: using dh-params and EC keys simultaneously will ' \
                      'lead to DH ciphers being used instead of ECDH')

    if dict_search('encryption.cipher', openvpn) == 'none':
        print('Warning: "encryption none" was specified!')
        print('No encryption will be performed and data is transmitted in ' \
              'plain text over the network!')

    verify_pki(openvpn)

    #
    # Auth user/pass
    #
    if (dict_search('authentication.username', openvpn) and not
        dict_search('authentication.password', openvpn)):
            raise ConfigError('Password for authentication is missing')

    if (dict_search('authentication.password', openvpn) and not
        dict_search('authentication.username', openvpn)):
            raise ConfigError('Username for authentication is missing')

    verify_vrf(openvpn)

    return None

def generate_pki_files(openvpn):
    pki = openvpn['pki']
    if not pki:
        return None

    interface = openvpn['ifname']
    shared_secret_key = dict_search_args(openvpn, 'shared_secret_key')
    tls = dict_search_args(openvpn, 'tls')

    if shared_secret_key:
        pki_key = pki['openvpn']['shared_secret'][shared_secret_key]
        key_path = os.path.join(cfg_dir, f'{interface}_shared.key')
        write_file(key_path, wrap_openvpn_key(pki_key['key']),
                   user=user, group=group)

    if tls:
        if 'ca_certificate' in tls:
            cert_name = tls['ca_certificate']
            pki_ca = pki['ca'][cert_name]

            if 'certificate' in pki_ca:
                cert_path = os.path.join(cfg_dir, f'{interface}_ca.pem')
                write_file(cert_path, wrap_certificate(pki_ca['certificate']),
                           user=user, group=group, mode=0o600)

            if 'crl' in pki_ca:
                for crl in pki_ca['crl']:
                    crl_path = os.path.join(cfg_dir, f'{interface}_crl.pem')
                    write_file(crl_path, wrap_crl(crl), user=user, group=group,
                               mode=0o600)

                openvpn['tls']['crl'] = True

        if 'certificate' in tls:
            cert_name = tls['certificate']
            pki_cert = pki['certificate'][cert_name]

            if 'certificate' in pki_cert:
                cert_path = os.path.join(cfg_dir, f'{interface}_cert.pem')
                write_file(cert_path, wrap_certificate(pki_cert['certificate']),
                           user=user, group=group, mode=0o600)

            if 'private' in pki_cert and 'key' in pki_cert['private']:
                key_path = os.path.join(cfg_dir, f'{interface}_cert.key')
                write_file(key_path, wrap_private_key(pki_cert['private']['key']),
                           user=user, group=group, mode=0o600)

                openvpn['tls']['private_key'] = True

        if 'dh_params' in tls:
            dh_name = tls['dh_params']
            pki_dh = pki['dh'][dh_name]

            if 'parameters' in pki_dh:
                dh_path = os.path.join(cfg_dir, f'{interface}_dh.pem')
                write_file(dh_path, wrap_dh_parameters(pki_dh['parameters']),
                           user=user, group=group, mode=0o600)

        if 'auth_key' in tls:
            key_name = tls['auth_key']
            pki_key = pki['openvpn']['shared_secret'][key_name]

            if 'key' in pki_key:
                key_path = os.path.join(cfg_dir, f'{interface}_auth.key')
                write_file(key_path, wrap_openvpn_key(pki_key['key']),
                           user=user, group=group, mode=0o600)

        if 'crypt_key' in tls:
            key_name = tls['crypt_key']
            pki_key = pki['openvpn']['shared_secret'][key_name]

            if 'key' in pki_key:
                key_path = os.path.join(cfg_dir, f'{interface}_crypt.key')

                with open(key_path, 'w') as f:
                    f.write(wrap_openvpn_key(pki_key['key']))

                files.append(key_path)

    return files


def generate(openvpn):
    interface = openvpn['ifname']
    directory = os.path.dirname(cfg_file.format(**openvpn))
    plugin_dir = '/usr/lib/openvpn'

    # we can't know in advance which clients have been removed,
    # thus all client configs will be removed and re-added on demand
    ccd_dir = os.path.join(directory, 'ccd', interface)
    if os.path.isdir(ccd_dir):
        rmtree(ccd_dir, ignore_errors=True)

    if 'deleted' in openvpn or 'disable' in openvpn:
        return None

    # create client config directory on demand
    if not os.path.exists(ccd_dir):
        os.makedirs(ccd_dir, 0o755)
        chown(ccd_dir, user, group)

    # Fix file permissons for keys
    generate_pki_files(openvpn)

    # Generate User/Password authentication file
    if 'authentication' in openvpn:
        render(openvpn['auth_user_pass_file'], 'openvpn/auth.pw.tmpl', openvpn,
               user=user, group=group, permission=0o600)
    else:
        # delete old auth file if present
        if os.path.isfile(openvpn['auth_user_pass_file']):
            os.remove(openvpn['auth_user_pass_file'])

    # Generate client specific configuration
    server_client = dict_search_args(openvpn, 'server', 'client')
    if server_client:
        for client, client_config in server_client.items():
            client_file = os.path.join(ccd_dir, client)

            # Our client need's to know its subnet mask ...
            client_config['server_subnet'] = dict_search('server.subnet', openvpn)

            render(client_file, 'openvpn/client.conf.tmpl', client_config,
                   user=user, group=group)

    # we need to support quoting of raw parameters from OpenVPN CLI
    # see https://phabricator.vyos.net/T1632
    render(cfg_file.format(**openvpn), 'openvpn/server.conf.tmpl', openvpn,
           formater=lambda _: _.replace("&quot;", '"'), user=user, group=group)

    return None

def apply(openvpn):
    interface = openvpn['ifname']
    call(f'systemctl stop openvpn@{interface}.service')

    # Do some cleanup when OpenVPN is disabled/deleted
    if 'deleted' in openvpn or 'disable' in openvpn:
        for cleanup_file in glob(f'/run/openvpn/{interface}.*'):
            if os.path.isfile(cleanup_file):
                os.unlink(cleanup_file)

        if interface in interfaces():
            VTunIf(interface).remove()

        return None

    # No matching OpenVPN process running - maybe it got killed or none
    # existed - nevertheless, spawn new OpenVPN process
    call(f'systemctl start openvpn@{interface}.service')

    o = VTunIf(**openvpn)
    o.update(openvpn)

    return None


if __name__ == '__main__':
    try:
        c = get_config()
        verify(c)
        generate(c)
        apply(c)
    except ConfigError as e:
        print(e)
        exit(1)