summaryrefslogtreecommitdiff
path: root/python/vyos/ifconfig/dhcp.py
blob: d3e75c292d8f5b3013bba60e5059da08dbd2adfc (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
# Copyright 2020 VyOS maintainers and contributors <maintainers@vyos.io>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.  If not, see <http://www.gnu.org/licenses/>.

import os

from vyos.dicts import FixedDict
from vyos.ifconfig.control import Control
from vyos.template import render


class _DHCPv4 (Control):
    def __init__(self, ifname):
        super().__init__()
        config_base = r'/run/dhclient'
        self.options = FixedDict(**{
            'ifname': ifname,
            'hostname': '',
            'client_id': '',
            'vendor_class_id': '',
            'conf_file': config_base + f'/{ifname}.conf',
            'options_file': config_base + f'/{ifname}.options',
            'pid_file': config_base + f'/{ifname}.pid',
            'lease_file': config_base + f'/{ifname}.leases',
        })

    # replace dhcpv4/v6 with systemd.networkd?
    def set(self):
        """
        Configure interface as DHCP client. The dhclient binary is automatically
        started in background!

        Example:

        >>> from vyos.ifconfig import Interface
        >>> j = Interface('eth0')
        >>> j.dhcp.v4.set()
        """
        if not self.options['hostname']:
            # read configured system hostname.
            # maybe change to vyos hostd client ???
            with open('/etc/hostname', 'r') as f:
                self.options['hostname'] = f.read().rstrip('\n')

        render(self.options['options_file'], 'dhcp-client/daemon-options.tmpl', self.options)
        render(self.options['conf_file'], 'dhcp-client/ipv4.tmpl', self.options)

        return self._cmd('systemctl restart dhclient@{ifname}.service'.format(**self.options))

    def delete(self):
        """
        De-configure interface as DHCP clinet. All auto generated files like
        pid, config and lease will be removed.

        Example:

        >>> from vyos.ifconfig import Interface
        >>> j = Interface('eth0')
        >>> j.dhcp.v4.delete()
        """
        if not os.path.isfile(self.options['pid_file']):
            self._debug_msg('No DHCP client PID found')
            return None

        self._cmd('systemctl stop dhclient@{ifname}.service'.format(**self.options))

        # cleanup old config files
        for name in ('conf_file', 'options_file', 'pid_file', 'lease_file'):
            if os.path.isfile(self.options[name]):
                os.remove(self.options[name])

class _DHCPv6 (Control):
    def __init__(self, ifname):
        super().__init__()
        config_base = r'/run/dhclient6'
        self.options = FixedDict(**{
            'ifname': ifname,
            'conf_file': config_base + f'/{ifname}.conf',
            'options_file': config_base + f'/{ifname}.options',
            'pid_file': config_base + f'/{ifname}.pid',
            'lease_file': config_base + f'/{ifname}.leases',
            'dhcpv6_prm_only': False,
            'dhcpv6_temporary': False,
        })

    def set(self):
        """
        Configure interface as DHCPv6 client. The dhclient binary is automatically
        started in background!

        Example:

        >>> from vyos.ifconfig import Interface
        >>> j = Interface('eth0')
        >>> j.dhcp.v6.set()
        """

        # better save then sorry .. should be checked in interface script
        # but if you missed it we are safe!
        if self.options['dhcpv6_prm_only'] and self.options['dhcpv6_temporary']:
            raise Exception(
                'DHCPv6 temporary and parameters-only options are mutually exclusive!')

        render(self.options['options_file'], 'dhcp-client/daemon-options.tmpl', self.options)
        render(self.options['conf_file'], 'dhcp-client/ipv6.tmpl', self.options)

        # no longer accept router announcements on this interface
        self._write_sysfs('/proc/sys/net/ipv6/conf/{ifname}/accept_ra'.format(**self.options), 0)

        return self._cmd('systemctl restart dhclient6@{ifname}.service'.format(**self.options))

    def delete(self):
        """
        De-configure interface as DHCPv6 clinet. All auto generated files like
        pid, config and lease will be removed.

        Example:

        >>> from vyos.ifconfig import Interface
        >>> j = Interface('eth0')
        >>> j.dhcp.v6.delete()
        """
        if not os.path.isfile(self.options['pid_file']):
            self._debug_msg('No DHCPv6 client PID found')
            return None

        self._cmd('systemctl stop dhclient6@{ifname}.service'.format(**self.options))

        # accept router announcements on this interface
        self._write_sysfs('/proc/sys/net/ipv6/conf/{ifname}/accept_ra'.format(**self.options), 1)

        # cleanup old config files
        for name in ('conf_file', 'options_file', 'pid_file', 'lease_file'):
            if os.path.isfile(self.options[name]):
                os.remove(self.options[name])


class DHCP(object):
    def __init__(self, ifname):
        self.v4 = _DHCPv4(ifname)
        self.v6 = _DHCPv6(ifname)