/usr/lib/python2.7/dist-packages/provisioningserver/tftp.py is in python-maas-provisioningserver 1.5+bzr2252-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 | # Copyright 2012-2014 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Twisted Application Plugin for the MAAS TFTP server."""
from __future__ import (
absolute_import,
print_function,
unicode_literals,
)
str = None
__metaclass__ = type
__all__ = [
"TFTPBackend",
"TFTPService",
]
import httplib
from io import BytesIO
import json
from urllib import urlencode
from urlparse import (
parse_qsl,
urlparse,
)
from provisioningserver.boot import BootMethodRegistry
from provisioningserver.cluster_config import get_cluster_uuid
from provisioningserver.driver import ArchitectureRegistry
from provisioningserver.kernel_opts import KernelParameters
from provisioningserver.utils import (
deferred,
get_all_interface_addresses,
)
from tftp.backend import (
FilesystemSynchronousBackend,
IReader,
)
from tftp.errors import FileNotFound
from tftp.protocol import TFTP
from twisted.application import internet
from twisted.application.service import MultiService
from twisted.python.context import get
from twisted.web.client import getPage
import twisted.web.error
from zope.interface import implementer
@implementer(IReader)
class BytesReader:
def __init__(self, data):
super(BytesReader, self).__init__()
self.buffer = BytesIO(data)
self.size = len(data)
def read(self, size):
return self.buffer.read(size)
def finish(self):
self.buffer.close()
class TFTPBackend(FilesystemSynchronousBackend):
"""A partially dynamic read-only TFTP server.
Static files such as kernels and initrds, as well as any non-MAAS files
that the system may already be set up to serve, are served up normally.
But PXE configurations are generated on the fly.
When a PXE configuration file is requested, the server asynchronously
requests the appropriate parameters from the API (at a configurable
"generator URL") and generates a config file based on those.
The regular expressions `re_config_file` and `re_mac_address` specify
which files the server generates on the fly. Any other requests are
passed on to the filesystem.
Passing requests on to the API must be done very selectively, because
failures cause the boot process to halt. This is why the expression for
matching the MAC address is so narrowly defined: PXELINUX attempts to
fetch files at many similar paths which must not be passed on.
"""
get_page = staticmethod(getPage)
def __init__(self, base_path, generator_url):
"""
:param base_path: The root directory for this TFTP server.
:param generator_url: The URL which can be queried for the PXE
config. See `get_generator_url` for the types of queries it is
expected to accept.
"""
super(TFTPBackend, self).__init__(
base_path, can_read=True, can_write=False)
self.generator_url = urlparse(generator_url)
def get_generator_url(self, params):
"""Calculate the URL, including query, from which we can fetch
additional configuration parameters.
:param params: A dict, or iterable suitable for updating a dict, of
additional query parameters.
"""
query = {}
# Merge parameters from the generator URL.
query.update(parse_qsl(self.generator_url.query))
# Merge parameters obtained from the request.
query.update(params)
# Merge updated query into the generator URL.
url = self.generator_url._replace(query=urlencode(query))
# TODO: do something more intelligent with unicode URLs here; see
# apiclient.utils.ascii_url() for inspiration.
return url.geturl().encode("ascii")
def get_boot_method(self, file_name):
"""Finds the correct boot method."""
for _, method in BootMethodRegistry:
params = method.match_config_path(file_name)
if params is not None:
return method, params
return None, None
@deferred
def get_kernel_params(self, params):
"""Return kernel parameters obtained from the API.
:param params: Parameters so far obtained, typically from the file
path requested.
:return: A `KernelParameters` instance.
"""
url = self.get_generator_url(params)
def reassemble(data):
return KernelParameters(**data)
d = self.get_page(url)
d.addCallback(json.loads)
d.addCallback(reassemble)
return d
@deferred
def get_config_reader(self, boot_method, params):
"""Return an `IReader` for a boot method.
:param boot_method: Boot method that is generating the config
:param params: Parameters so far obtained, typically from the file
path requested.
"""
def generate_config(kernel_params):
config = boot_method.render_config(
kernel_params=kernel_params, **params)
return config.encode("utf-8")
d = self.get_kernel_params(params)
d.addCallback(generate_config)
d.addCallback(BytesReader)
return d
@staticmethod
def get_page_errback(failure, file_name):
failure.trap(twisted.web.error.Error)
# This twisted.web.error.Error.status object ends up being a
# string for some reason, but the constants we can compare against
# (both in httplib and twisted.web.http) are ints.
try:
status_int = int(failure.value.status)
except ValueError:
# Assume that it's some other error and propagate it
return failure
if status_int == httplib.NO_CONTENT:
# Convert HTTP No Content to a TFTP file not found
raise FileNotFound(file_name)
else:
# Otherwise propogate the unknown error
return failure
@deferred
def get_reader(self, file_name):
"""See `IBackend.get_reader()`.
If `file_name` matches a boot method then the response is obtained
from that boot method. Otherwise the filesystem is used to service
the response.
"""
boot_method, params = self.get_boot_method(file_name)
if boot_method is None:
return super(TFTPBackend, self).get_reader(file_name)
# Map pxe namespace architecture names to MAAS's.
arch = params.get("arch")
if arch is not None:
maasarch = ArchitectureRegistry.get_by_pxealias(arch)
if maasarch is not None:
params["arch"] = maasarch.name.split("/")[0]
# Send the local and remote endpoint addresses.
local_host, local_port = get("local", (None, None))
params["local"] = local_host
remote_host, remote_port = get("remote", (None, None))
params["remote"] = remote_host
params["cluster_uuid"] = get_cluster_uuid()
d = self.get_config_reader(boot_method, params)
d.addErrback(self.get_page_errback, file_name)
return d
class TFTPService(MultiService, object):
"""An umbrella service representing a set of running TFTP servers.
Creates a UDP server individually for each discovered network
interface, so that we can detect the interface via which we have
received a datagram.
It then periodically updates the servers running in case there's a
change to the host machine's network configuration.
:ivar backend: The :class:`TFTPBackend` being used to service TFTP
requests.
:ivar port: The port on which each server is started.
:ivar refresher: A :class:`TimerService` that calls
``updateServers`` periodically.
"""
def __init__(self, resource_root, port, generator):
"""
:param resource_root: The root directory for this TFTP server.
:param port: The port on which each server should be started.
:param generator: The URL to be queried for PXE configuration.
This will normally point to the `pxeconfig` endpoint on the
region-controller API.
"""
super(TFTPService, self).__init__()
self.backend = TFTPBackend(resource_root, generator)
self.port = port
# Establish a periodic call to self.updateServers() every 45
# seconds, so that this service eventually converges on truth.
# TimerService ensures that a call is made to it's target
# function immediately as it's started, so there's no need to
# call updateServers() from here.
self.refresher = internet.TimerService(45, self.updateServers)
self.refresher.setName("refresher")
self.refresher.setServiceParent(self)
def getServers(self):
"""Return a set of all configured servers.
:rtype: :class:`set` of :class:`internet.UDPServer`
"""
return {
service for service in self
if service is not self.refresher
}
def updateServers(self):
"""Run a server on every interface.
For each configured network interface this will start a TFTP
server. If called later it will bring up servers on newly
configured interfaces and bring down servers on deconfigured
interfaces.
"""
addrs_established = set(service.name for service in self.getServers())
addrs_desired = set(get_all_interface_addresses())
for address in addrs_desired - addrs_established:
tftp_service = internet.UDPServer(
self.port, TFTP(self.backend), interface=address)
tftp_service.setName(address)
tftp_service.setServiceParent(self)
for address in addrs_established - addrs_desired:
tftp_service = self.getServiceNamed(address)
tftp_service.disownServiceParent()
|