This file is indexed.

/usr/lib/python3/dist-packages/provisioningserver/rpc/dhcp.py is in python3-maas-provisioningserver 2.4.0~beta2-6865-gec43e47e6-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
# Copyright 2014-2016 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""RPC helpers relating to DHCP."""

__all__ = [
    "configure",
    "DHCPv4Server",
    "DHCPv6Server",
    "downgrade_shared_networks",
    "upgrade_shared_networks",
]

from collections import namedtuple
from operator import itemgetter
import os
import re
from tempfile import NamedTemporaryFile

from netaddr import IPAddress
from provisioningserver.dhcp import (
    DHCPv4Server,
    DHCPv6Server,
)
from provisioningserver.dhcp.config import get_config
from provisioningserver.dhcp.omshell import Omshell
from provisioningserver.logger import get_maas_logger
from provisioningserver.rpc.exceptions import (
    CannotConfigureDHCP,
    CannotCreateHostMap,
    CannotModifyHostMap,
    CannotRemoveHostMap,
)
from provisioningserver.service_monitor import service_monitor
from provisioningserver.utils.fs import (
    sudo_delete_file,
    sudo_write_file,
)
from provisioningserver.utils.service_monitor import (
    SERVICE_STATE,
    ServiceActionError,
)
from provisioningserver.utils.shell import (
    call_and_check,
    ExternalProcessError,
)
from provisioningserver.utils.text import split_string_list
from provisioningserver.utils.twisted import (
    asynchronous,
    synchronous,
)
from twisted.internet.defer import (
    inlineCallbacks,
    maybeDeferred,
)
from twisted.internet.threads import deferToThread


maaslog = get_maas_logger("dhcp")


# Holds the current state of DHCPv4 and DHCPv6.
_current_server_state = {}


DHCPStateBase = namedtuple("DHCPStateBase", [
    "omapi_key",
    "failover_peers",
    "shared_networks",
    "hosts",
    "interfaces",
    "global_dhcp_snippets",
])


class DHCPState(DHCPStateBase):
    """Holds the current known state of the DHCP server."""

    def __new__(
            cls, omapi_key, failover_peers,
            shared_networks, hosts, interfaces, global_dhcp_snippets):
        failover_peers = sorted(failover_peers, key=itemgetter("name"))
        shared_networks = sorted(shared_networks, key=itemgetter("name"))
        hosts = {
            host["mac"]: host
            for host in hosts
        }
        interfaces = sorted(
            interface["name"]
            for interface in interfaces
        )
        global_dhcp_snippets = sorted(
            global_dhcp_snippets, key=itemgetter("name"))
        return DHCPStateBase.__new__(
            cls,
            omapi_key=omapi_key,
            failover_peers=failover_peers,
            shared_networks=shared_networks,
            hosts=hosts, interfaces=interfaces,
            global_dhcp_snippets=global_dhcp_snippets)

    def requires_restart(self, other_state):
        """Return True when this state differs from `other_state` enough to
        require a restart."""
        def gather_hosts_dhcp_snippets(hosts):
            hosts_dhcp_snippets = list()
            for _, host in hosts.items():
                for dhcp_snippet in host['dhcp_snippets']:
                    hosts_dhcp_snippets.append(dhcp_snippet)
            return sorted(hosts_dhcp_snippets, key=itemgetter('name'))

        # Currently the OMAPI doesn't allow you to add or remove arbitrary
        # config options. So gather a list of DHCP snippets from
        hosts_dhcp_snippets = gather_hosts_dhcp_snippets(self.hosts)
        other_hosts_dhcp_snippets = gather_hosts_dhcp_snippets(
            other_state.hosts)
        return (
            self.omapi_key != other_state.omapi_key or
            self.failover_peers != other_state.failover_peers or
            self.shared_networks != other_state.shared_networks or
            self.interfaces != other_state.interfaces or
            self.global_dhcp_snippets != other_state.global_dhcp_snippets or
            hosts_dhcp_snippets != other_hosts_dhcp_snippets)

    def host_diff(self, other_state):
        """Return tuple with the hosts that need to be removed, need to be
        added, and need be updated."""
        remove, add, modify = [], [], []
        for mac, host in self.hosts.items():
            if mac not in other_state.hosts:
                add.append(host)
            elif host['ip'] != other_state.hosts[mac]['ip']:
                modify.append(host)
        for mac, host in other_state.hosts.items():
            if mac not in self.hosts:
                remove.append(host)
        return remove, add, modify

    def get_config(self, server):
        """Return the configuration for `server`."""
        dhcpd_config = get_config(
            server.template_basename, omapi_key=self.omapi_key,
            failover_peers=self.failover_peers, ipv6=server.ipv6,
            shared_networks=self.shared_networks,
            hosts=sorted(self.hosts.values(), key=itemgetter("host")),
            global_dhcp_snippets=sorted(
                self.global_dhcp_snippets, key=itemgetter("name")))
        return dhcpd_config, " ".join(self.interfaces)


@synchronous
def _write_config(server, state):
    """Write the configuration file."""
    dhcpd_config, interfaces_config = state.get_config(server)
    try:
        sudo_write_file(
            server.config_filename, dhcpd_config.encode("utf-8"))
        sudo_write_file(
            server.interfaces_filename,
            interfaces_config.encode("utf-8"))
    except ExternalProcessError as e:
        # ExternalProcessError.__str__ contains a generic failure message
        # as well as the command and its error output. On the other hand,
        # ExternalProcessError.output_as_unicode contains just the error
        # output which is probably the best information on what went wrong.
        # Log the full error information, but keep the exception message
        # short and to the point.
        maaslog.error(
            "Could not rewrite %s server configuration (for network "
            "interfaces %s): %s", server.descriptive_name,
            interfaces_config, str(e))
        raise CannotConfigureDHCP(
            "Could not rewrite %s server configuration: %s" % (
                server.descriptive_name, e.output_as_unicode))


@synchronous
def _delete_config(server):
    """Delete the server config."""
    if os.path.exists(server.config_filename):
        sudo_delete_file(server.config_filename)


def _remove_host_map(omshell, mac):
    """Remove host by `mac`."""
    try:
        omshell.remove(mac)
    except ExternalProcessError as e:
        if 'not connected.' in e.output_as_unicode:
            msg = "The DHCP server could not be reached."
        else:
            msg = str(e)
        err = "Could not remove host map for %s: %s" % (mac, msg)
        maaslog.error(err)
        raise CannotRemoveHostMap(err)


def _create_host_map(omshell, mac, ip_address):
    """Create host with `mac` -> `ip_address`."""
    try:
        omshell.create(ip_address, mac)
    except ExternalProcessError as e:
        if 'not connected.' in e.output_as_unicode:
            msg = "The DHCP server could not be reached."
        else:
            msg = str(e)
        err = "Could not create host map for %s -> %s: %s" % (
            mac, ip_address, msg)
        maaslog.error(err)
        raise CannotCreateHostMap(err)


def _modify_host_map(omshell, mac, ip_address):
    """Modify host with `mac` -> `ip_address`."""
    try:
        omshell.modify(ip_address, mac)
    except ExternalProcessError as e:
        if 'not connected.' in e.output_as_unicode:
            msg = "The DHCP server could not be reached."
        else:
            msg = str(e)
        err = "Could not modify host map for %s -> %s: %s" % (
            mac, ip_address, msg)
        maaslog.error(err)
        raise CannotModifyHostMap(err)


@synchronous
def _update_hosts(server, remove, add, modify):
    """Update the hosts using the OMAPI."""
    omshell = Omshell(
        server_address='127.0.0.1', shared_key=server.omapi_key,
        ipv6=server.ipv6)
    for host in remove:
        _remove_host_map(omshell, host["mac"])
    for host in add:
        _create_host_map(omshell, host["mac"], host["ip"])
    for host in modify:
        _modify_host_map(omshell, host["mac"], host["ip"])


@asynchronous
def _catch_service_error(server, action, call, *args, **kwargs):
    """Helper to catch `ServiceActionError` and `Exception` when performing
    `call`."""

    def eb(failure):
        message = "%s server failed to %s: %s" % (
            server.descriptive_name, action, failure.value)
        # A ServiceActionError will have already been logged by the
        # service monitor, so don't log a second time.
        if not failure.check(ServiceActionError):
            maaslog.error(message)
        # Squash everything into CannotConfigureDHCP.
        raise CannotConfigureDHCP(message) from failure.value

    return maybeDeferred(call, *args, **kwargs).addErrback(eb)


@asynchronous
@inlineCallbacks
def configure(
        server, failover_peers, shared_networks, hosts, interfaces,
        global_dhcp_snippets=None):
    """Configure the DHCPv6/DHCPv4 server, and restart it as appropriate.

    This method is not safe to call concurrently. The clusterserver ensures
    that this method is not called concurrently.

    :param server: A `DHCPServer` instance.
    :param failover_peers: List of dicts with failover parameters for each
        subnet where HA is enabled.
    :param shared_networks: List of dicts with shared network parameters that
        contain a list of subnets when the DHCP should server shared.
        If no shared network are defined, the DHCP server will be stopped.
    :param hosts: List of dicts with host parameters that
        contain a list of hosts the DHCP should statically.
    :param interfaces: List of interfaces that DHCP should use.
    :param global_dhcp_snippets: List of all global DHCP snippets
    """
    stopping = len(shared_networks) == 0

    if global_dhcp_snippets is None:
        global_dhcp_snippets = []

    if stopping:
        # Remove the config so that the even an administrator cannot turn it on
        # accidently when it should be off.
        yield deferToThread(_delete_config, server)

        # Ensure that the service is off and is staying off.
        service = service_monitor.getServiceByName(server.dhcp_service)
        service.off()
        yield _catch_service_error(
            server, "stop",
            service_monitor.ensureService, server.dhcp_service)
        _current_server_state[server.dhcp_service] = None
    else:
        # Get the new state for the DHCP server.
        new_state = DHCPState(
            server.omapi_key, failover_peers, shared_networks,
            hosts, interfaces, global_dhcp_snippets)

        # Always write the config, that way its always up-to-date. Even if
        # we are not going to restart the services. This makes sure that even
        # the comments in the file are updated.
        yield deferToThread(_write_config, server, new_state)

        # Service should always be on if shared_networks exists.
        service = service_monitor.getServiceByName(server.dhcp_service)
        service.on()

        # Perform the required action based on the state change.
        current_state = _current_server_state.get(server.dhcp_service, None)
        if current_state is None:
            yield _catch_service_error(
                server, "restart",
                service_monitor.restartService, server.dhcp_service)
        elif new_state.requires_restart(current_state):
            yield _catch_service_error(
                server, "restart",
                service_monitor.restartService, server.dhcp_service)
        else:
            # No restart required update the host mappings if needed.
            remove, add, modify = new_state.host_diff(current_state)
            if len(remove) + len(add) + len(modify) == 0:
                # Nothing has changed, do nothing but make sure its running.
                yield _catch_service_error(
                    server, "start",
                    service_monitor.ensureService, server.dhcp_service)
            else:
                # Check the state of the service. Only if the services was on
                # should the host maps be updated over the OMAPI.
                before_state = yield service_monitor.getServiceState(
                    server.dhcp_service, now=True)
                yield _catch_service_error(
                    server, "start",
                    service_monitor.ensureService, server.dhcp_service)
                if before_state.active_state == SERVICE_STATE.ON:
                    # Was already running, so update host maps over OMAPI
                    # instead of performing a full restart.
                    try:
                        yield deferToThread(
                            _update_hosts, server, remove, add, modify)
                    except:
                        # Error updating the host maps over the OMAPI.
                        # Restart the DHCP service so that the host maps
                        # are in-sync with what MAAS expects.
                        maaslog.warning(
                            "Failed to update all host maps. Restarting %s "
                            "service to ensure host maps are in-sync." % (
                                server.descriptive_name))
                        yield _catch_service_error(
                            server, "restart",
                            service_monitor.restartService,
                            server.dhcp_service)

        # Update the current state to the new state.
        _current_server_state[server.dhcp_service] = new_state


def _parse_dhcpd_errors(error_str):
    """Parse the output of dhcpd -t -cf <file> into a list of dictionaries

    dhcpd-4.3.3-5ubuntu11 -t -cf outputs each syntax error on three lines.
    First contains the filename, line number, and what the error is. Second
    outputs the line which has the syntax error and third is a pointer to the
    where on the previous line the error was detected. """
    processing_config = False
    errors = []
    error = {}
    error_regex = re.compile('line (?P<line_num>[0-9]+): (?P<error>.+)')
    for line in error_str.splitlines():
        m = error_regex.search(line)
        # Don't start processing till we get past header and end processing
        # once we get to the footer.
        if not processing_config and m is None:
            continue
        elif not processing_config and m is not None:
            processing_config = True
        elif line.startswith('Configuration file errors encountered'):
            break
        if m is not None:
            # New error, append previous error to the list of errors
            if error.get('error') is not None:
                errors.append(error)
                error = {}
            error['error'] = m.group('error')
            error['line_num'] = int(m.group('line_num'))
        elif m is None and line.strip() == '^':
            error['position'] = line
        else:
            error['line'] = line

    if error != {}:
        errors.append(error)

    return errors


def validate(
        server, failover_peers, shared_networks, hosts, interfaces,
        global_dhcp_snippets=None):
    """Validate the DHCPv6/DHCPv4 configuration.

    :param server: A `DHCPServer` instance.
    :param failover_peers: List of dicts with failover parameters for each
        subnet where HA is enabled.
    :param shared_networks: List of dicts with shared network parameters that
        contain a list of subnets when the DHCP should server shared.
        If no shared network are defined, the DHCP server will be stopped.
    :param hosts: List of dicts with host parameters that
        contain a list of hosts the DHCP should statically.
    :param interfaces: List of interfaces that DHCP should use.
    :param global_dhcp_snippets: List of all global DHCP snippets
    """
    if global_dhcp_snippets is None:
        global_dhcp_snippets = []
    state = DHCPState(
        server.omapi_key, failover_peers, shared_networks,
        hosts, interfaces, global_dhcp_snippets)
    dhcpd_config, _ = state.get_config(server)
    with NamedTemporaryFile(prefix='maas-dhcpd-') as tmp_dhcpd:
        tmp_dhcpd.file.write(dhcpd_config.encode('utf-8'))
        tmp_dhcpd.file.flush()
        try:
            call_and_check(['dhcpd', '-t', '-cf', tmp_dhcpd.name])
        except ExternalProcessError as e:
            return _parse_dhcpd_errors(e.output_as_unicode)
    return None


def upgrade_shared_networks(shared_networks):
    """Update the `shared_networks` structure to match the V2 calls.

    Mutates `shared_networks` in place.
    """
    for shared_network in shared_networks:
        for subnet in shared_network["subnets"]:
            dns_servers = subnet["dns_servers"]
            if isinstance(dns_servers, str):
                dns_servers = map(IPAddress, split_string_list(dns_servers))
                subnet["dns_servers"] = list(dns_servers)
            if "ntp_server" in subnet:  # Note singular.
                ntp_servers = split_string_list(subnet.pop("ntp_server"))
                subnet["ntp_servers"] = list(ntp_servers)


def downgrade_shared_networks(shared_networks):
    """Downgrade the `shared_networks` structure to match the V1 calls.

    Mutates `shared_networks` in place.
    """
    for shared_network in shared_networks:
        for subnet in shared_network["subnets"]:
            dns_servers = subnet["dns_servers"]
            if not isinstance(dns_servers, str):
                subnet["dns_servers"] = ", ".join(map(str, dns_servers))
            if "ntp_servers" in subnet:
                subnet["ntp_server"] = ", ".join(subnet.pop("ntp_servers"))