-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy path__init__.py
639 lines (541 loc) · 20.8 KB
/
__init__.py
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
#
# The Qubes OS Project, https://www.qubes-os.org/
#
# Copyright (C) 2010-2015 Joanna Rutkowska <[email protected]>
# Copyright (C) 2011-2015 Marek Marczykowski-Górecki
# Copyright (C) 2014-2015 Wojtek Porczyk <[email protected]>
#
# 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 <https://www.gnu.org/licenses/>.
#
"""Qubes Virtual Machines
"""
import asyncio
import re
import string
import uuid
from typing import List
import lxml.etree
import qubes
import qubes.devices
import qubes.device_protocol
import qubes.events
import qubes.features
import qubes.log
VM_ENTRY_POINT = "qubes.vm"
def validate_name(holder, prop, value):
"""Check if value is syntactically correct VM name"""
if not isinstance(value, str):
raise TypeError(
"VM name must be string, {!r} found".format(type(value).__name__)
)
if len(value) > 31:
if holder is not None and prop is not None:
raise qubes.exc.QubesPropertyValueError(
holder,
prop,
value,
"{} value must be shorter than 32 characters".format(
prop.__name__
),
)
raise qubes.exc.QubesValueError(
"VM name must be shorter than 32 characters"
)
if re.match(r"\A[0-9_.-].*\Z", value) is not None:
if holder is not None and prop is not None:
raise qubes.exc.QubesPropertyValueError(
holder,
prop,
value,
"{} cannot start with hyphen, underscore, dot "
"or numbers".format(prop.__name__),
)
raise qubes.exc.QubesValueError(
"VM name cannot start with hyphen, underscore, dot or numbers"
)
# this regexp does not contain '+'; if it had it, we should specifically
# disallow 'lost+found' #1440
if re.match(r"\A[a-zA-Z][a-zA-Z0-9_.-]*\Z", value) is None:
if holder is not None and prop is not None:
raise qubes.exc.QubesPropertyValueError(
holder,
prop,
value,
"{} value contains illegal characters".format(prop.__name__),
)
raise qubes.exc.QubesValueError("VM name contains illegal characters")
if value == "Domain-0":
raise qubes.exc.QubesValueError("VM name cannot be 'Domain-0'")
if value in ("none", "default"):
raise qubes.exc.QubesValueError(
"VM name cannot be 'none' nor 'default'"
)
if value.endswith("-dm"):
raise qubes.exc.QubesValueError("VM name cannot end with -dm")
def setter_label(self, prop, value):
"""Helper for setting the domain label"""
# pylint: disable=unused-argument
if isinstance(value, qubes.Label):
return value
if isinstance(value, str) and value.startswith("label-"):
return self.app.labels[int(value.split("-", 1)[1])]
return self.app.get_label(value)
def _setter_qid(self, prop, value):
"""Helper for setting the domain qid"""
# pylint: disable=unused-argument
value = int(value)
if not 0 <= value <= qubes.config.max_qid:
raise ValueError(
"{} value must be between 0 and qubes.config.max_qid".format(
prop.__name__
)
)
return value
class Tags(set):
"""Manager of the tags.
Tags are simple: tag either can be present on qube or not. Tag is a
simple string consisting of ASCII alphanumeric characters, plus `_` and
`-`.
This class inherits from set, but has most of the methods that manipulate
the item disarmed (they raise NotImplementedError). The ones that are left
fire appropriate events on the qube that owns an instance of this class.
"""
#
# Those are the methods that affect contents. Either disarm them or make
# them report appropriate events. Good approach is to rewrite them carefully
# using official documentation, but use only our (overloaded) methods.
#
def __init__(self, vm, seq=()):
super().__init__()
self.vm = vm
self.update(seq)
def clear(self):
"""Remove all tags"""
for item in tuple(self):
self.remove(item)
def symmetric_difference_update(self, *args, **kwargs):
"""Not implemented
:raises: NotImplementedError
"""
raise NotImplementedError()
def intersection_update(self, *args, **kwargs):
"""Not implemented
:raises: NotImplementedError
"""
raise NotImplementedError()
def pop(self):
"""Not implemented
:raises: NotImplementedError
"""
raise NotImplementedError()
def discard(self, elem):
"""Remove a tag if present"""
if elem in self:
self.remove(elem)
def update(self, *others):
"""Add tags from iterable(s)"""
for other in others:
for elem in other:
self.add(elem)
def add(self, elem):
"""Add a tag"""
allowed_chars = string.ascii_letters + string.digits + "_-"
if any(i not in allowed_chars for i in elem):
raise ValueError("Invalid character in tag")
if elem in self:
return
super().add(elem)
self.vm.fire_event("domain-tag-add:" + elem, tag=elem)
def remove(self, elem):
"""Remove a tag"""
super().remove(elem)
self.vm.fire_event("domain-tag-delete:" + elem, tag=elem)
#
# end of overriding
#
@staticmethod
def validate_tag(tag):
safe_set = string.ascii_letters + string.digits + "-_"
if not all((x in safe_set) for x in tag):
raise ValueError("disallowed characters")
class BaseVM(qubes.PropertyHolder):
"""Base class for all VMs
:param app: Qubes application context
:type app: :py:class:`qubes.Qubes`
:param xml: xml node from which to deserialise
:type xml: :py:class:`lxml.etree._Element` or :py:obj:`None`
This class is responsible for serializing and deserialising machines and
provides basic framework. It contains no management logic. For that, see
:py:class:`qubes.vm.qubesvm.QubesVM`.
"""
# pylint: disable=no-member
uuid = qubes.property(
"uuid",
type=uuid.UUID,
write_once=True,
clone=False,
doc="UUID from libvirt.",
)
name = qubes.property(
"name",
type=str,
write_once=True,
clone=False,
doc="User-specified name of the domain.",
)
qid = qubes.property(
"qid",
type=int,
write_once=True,
setter=_setter_qid,
clone=False,
doc="""Internal, persistent identificator of particular domain. Note
this is different from Xen domid.""",
)
label = qubes.property(
"label",
setter=setter_label,
doc="""Colourful label assigned to VM. This is where the colour of the
padlock is set.""",
)
def __init__(
self, app, xml, features=None, devices=None, tags=None, **kwargs
):
# pylint: disable=redefined-outer-name
self._qdb_watch_paths = set()
self._qdb_connection_watch = None
# self.app must be set before super().__init__, because some property
# setters need working .app attribute
#: mother :py:class:`qubes.Qubes` object
self.app = app
super().__init__(xml, **kwargs)
#: dictionary of features of this qube
self.features = qubes.features.Features(self, features)
#: :py:class:`DeviceManager` object keeping devices that are attached to
#: this domain
self.devices = devices or qubes.devices.DeviceManager(self)
#: user-specified tags
self.tags = Tags(self, tags or ())
#: logger instance for logging messages related to this VM
self.log = None
#: storage volumes
self.volumes = {}
#: storage manager
self.storage = None
if hasattr(self, "name"):
self.init_log()
def close(self):
super().close()
if self._qdb_connection_watch is not None:
asyncio.get_event_loop().remove_reader(
self._qdb_connection_watch.watch_fd()
)
self._qdb_connection_watch.close()
del self._qdb_connection_watch
del self.app
del self.features
del self.storage
# TODO storage may have circ references, but it doesn't leak fds
del self.devices
del self.tags
def load_extras(self):
if self.xml is None:
return
# features
for node in self.xml.xpath("./features/feature"):
self.features[node.get("name")] = node.text
# devices (pci, usb, ...)
for parent in self.xml.xpath("./devices"):
devclass = parent.get("class")
for node in parent.xpath("./device"):
options = {}
for option in node.xpath("./option"):
options[option.get("name")] = str(option.text)
try:
# backward compatibility: persistent~>required=True
legacy_required = node.get("required", "absent")
if legacy_required == "absent":
mode_str = node.get("mode", "required")
try:
mode = qubes.device_protocol.AssignmentMode(
mode_str
)
except ValueError:
self.log.error(
"Unrecognized assignment mode, ignoring."
)
continue
else:
required = qubes.property.bool(
None, None, legacy_required
)
if required:
mode = qubes.device_protocol.AssignmentMode.REQUIRED
else:
mode = qubes.device_protocol.AssignmentMode.AUTO
if "identity" in options:
identity = options.get("identity")
del options["identity"]
else:
identity = node.get("identity", "*")
backend_name = node.get("backend-domain", None)
backend = (
self.app.domains[backend_name] if backend_name else None
)
device_assignment = qubes.device_protocol.DeviceAssignment(
qubes.device_protocol.VirtualDevice(
qubes.device_protocol.Port(
backend_domain=backend,
port_id=node.get("id", "*"),
devclass=devclass,
),
device_id=identity,
),
options=options,
mode=mode,
)
self.devices[devclass].load_assignment(device_assignment)
except KeyError:
msg = (
"{}: Cannot find backend domain '{}' "
"for device type {} '{}'".format(
self.name,
node.get("backend-domain"),
devclass,
node.get("id"),
)
)
self.log.info(msg)
continue
# tags
for node in self.xml.xpath("./tags/tag"):
self.tags.add(node.get("name"))
# SEE:1815 firewall, policy.
def get_provided_assignments(
self, required_only: bool = False
) -> List["qubes.device_protocol.DeviceAssignment"]:
"""
List device assignments from this VM.
"""
assignments = []
for domain in self.app.domains:
if domain == self:
continue
for device_collection in domain.devices.values():
for assignment in device_collection.get_assigned_devices(
required_only
):
if assignment.backend_domain == self:
assignments.append(assignment)
return assignments
def init_log(self):
"""Initialise logger for this domain."""
self.log = qubes.log.get_vm_logger(self.name)
def __xml__(self):
element = lxml.etree.Element("domain")
element.set("id", "domain-" + str(self.qid))
element.set("class", self.__class__.__name__)
element.append(self.xml_properties())
features = lxml.etree.Element("features")
for feature in self.features:
node = lxml.etree.Element("feature", name=feature)
node.text = self.features[feature]
features.append(node)
element.append(features)
for devclass in self.devices:
devices = lxml.etree.Element("devices")
devices.set("class", devclass)
for assignment in self.devices[devclass].get_assigned_devices():
node = lxml.etree.Element("device")
node.set("backend-domain", str(assignment.backend_name))
node.set("id", assignment.port_id)
node.set("mode", assignment.mode.value)
identity = assignment.device_id or "*"
node.set("identity", identity)
for key, val in assignment.options.items():
option_node = lxml.etree.Element("option")
option_node.set("name", key)
option_node.text = val
node.append(option_node)
devices.append(node)
element.append(devices)
tags = lxml.etree.Element("tags")
for tag in self.tags:
node = lxml.etree.Element("tag", name=tag)
tags.append(node)
element.append(tags)
return element
def __repr__(self):
proprepr = []
for prop in self.property_list():
if prop.__name__ in ("name", "qid"):
continue
try:
proprepr.append(
"{}={!s}".format(
prop.__name__, getattr(self, prop.__name__)
)
)
except AttributeError:
continue
return "<{} at {:#x} name={!r} qid={!r} {}>".format(
type(self).__name__,
id(self),
self.name,
self.qid,
" ".join(proprepr),
)
#
# xml serialising methods
#
def create_config_file(self):
"""Create libvirt's XML domain config file"""
def bug(msg, *args):
raise AssertionError(msg % args if args else msg)
domain_config = self.app.env.select_template(
[
"libvirt/xen/by-name/{}.xml".format(self.name),
"libvirt/xen-user.xml",
"libvirt/xen-dist.xml",
"libvirt/xen.xml",
]
).render(vm=self, bug=bug)
return domain_config
def watch_qdb_path(self, path):
"""Add a QubesDB path to be watched.
Each change to the path will cause `domain-qdb-change:path` event to be
fired.
You can call this method for example in response to
`domain-init` and `domain-load` events.
"""
if path not in self._qdb_watch_paths:
self._qdb_watch_paths.add(path)
if self._qdb_connection_watch:
self._qdb_connection_watch.watch(path)
def _qdb_watch_reader(self, loop):
"""Callback when self._qdb_connection_watch.watch_fd() FD is
readable.
Read reported event (watched path change) and fire appropriate event.
"""
import qubesdb # pylint: disable=import-error
try:
path = self._qdb_connection_watch.read_watch()
for watched_path in self._qdb_watch_paths:
if watched_path == path or (
watched_path.endswith("/") and path.startswith(watched_path)
):
self.fire_event(
"domain-qdb-change:" + watched_path, path=path
)
except qubesdb.DisconnectedError:
loop.remove_reader(self._qdb_connection_watch.watch_fd())
self._qdb_connection_watch.close()
self._qdb_connection_watch = None
def start_qdb_watch(self, loop=None):
"""Start watching QubesDB
Calling this method in appropriate time is responsibility of child
class.
"""
# cleanup old watch connection first, if any
if self._qdb_connection_watch is not None:
asyncio.get_event_loop().remove_reader(
self._qdb_connection_watch.watch_fd()
)
self._qdb_connection_watch.close()
import qubesdb # pylint: disable=import-error
self._qdb_connection_watch = qubesdb.QubesDB(self.name)
if loop is None:
loop = asyncio.get_event_loop()
loop.add_reader(
self._qdb_connection_watch.watch_fd(), self._qdb_watch_reader, loop
)
for path in self._qdb_watch_paths:
self._qdb_connection_watch.watch(path)
@qubes.stateless_property
def klass(self):
"""Domain class name"""
return type(self).__name__
class VMProperty(qubes.property):
"""Property that is referring to a VM
:param type vmclass: class that returned VM is supposed to be instance of
and all supported by :py:class:`property` with the exception of ``type`` \
and ``setter``
"""
_none_value = ""
def __init__(self, name, vmclass=BaseVM, allow_none=False, **kwargs):
if "type" in kwargs:
raise TypeError(
"'type' keyword parameter is unsupported in {}".format(
self.__class__.__name__
)
)
if not issubclass(vmclass, BaseVM):
raise TypeError(
"'vmclass' should specify a subclass of qubes.vm.BaseVM"
)
super().__init__(
name,
saver=(
lambda self_, prop, value: (
self._none_value if value is None else value.name
)
),
**kwargs
)
self.vmclass = vmclass
self.allow_none = allow_none
def __set__(self, instance, value):
if value is self.__class__.DEFAULT:
self.__delete__(instance)
return
if value == self._none_value:
value = None
if value is None:
if self.allow_none:
super().__set__(instance, value)
return
raise qubes.exc.QubesPropertyValueError(instance, self, value)
app = instance if isinstance(instance, qubes.Qubes) else instance.app
try:
vm = app.domains[value]
except KeyError:
raise qubes.exc.QubesPropertyValueError(
instance,
self,
value,
"Can't set {!s} to non-existing qube {!s}".format(self, value),
)
if not isinstance(vm, self.vmclass):
raise qubes.exc.QubesPropertyValueError(
instance,
self,
value,
"wrong VM class: domains[{!r}] is of type {!s} "
"and not {!s}".format(
value, vm.__class__.__name__, self.vmclass.__name__
),
)
super().__set__(instance, vm)
def sanitize(self, *, untrusted_newvalue):
try:
untrusted_vmname = untrusted_newvalue.decode("ascii")
except UnicodeDecodeError:
raise qubes.exc.QubesValueError
if untrusted_vmname == "":
# allow empty VM name for setting VMProperty value, because it's
# string representation of None (see self._none_value)
return untrusted_vmname
validate_name(None, self, untrusted_vmname)
return untrusted_vmname