2015-01-19 18:03:23 +01:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2011-2015 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
|
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# 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.
|
2015-01-19 18:03:23 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2015-01-19 18:03:23 +01:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2017-10-12 00:11:50 +02:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
2015-01-19 18:03:23 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# 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/>.
|
2015-01-19 18:03:23 +01:00
|
|
|
#
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2014-11-13 18:10:27 +01:00
|
|
|
'''
|
|
|
|
Qubes OS
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-19 18:03:23 +01:00
|
|
|
:copyright: © 2010-2015 Invisible Things Lab
|
2014-11-13 18:10:27 +01:00
|
|
|
'''
|
|
|
|
|
2017-01-18 22:16:46 +01:00
|
|
|
import builtins
|
2020-04-07 23:30:21 +02:00
|
|
|
import collections.abc
|
2014-12-05 14:58:05 +01:00
|
|
|
import os
|
|
|
|
import os.path
|
2017-03-28 20:43:53 +02:00
|
|
|
import string
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
import lxml.etree
|
2015-06-23 22:27:20 +02:00
|
|
|
import qubes.config
|
2015-10-14 22:02:11 +02:00
|
|
|
import qubes.events
|
|
|
|
import qubes.exc
|
2014-11-17 19:09:25 +01:00
|
|
|
|
2016-06-02 22:02:06 +02:00
|
|
|
__author__ = 'Invisible Things Lab'
|
|
|
|
__license__ = 'GPLv2 or later'
|
|
|
|
__version__ = 'R3'
|
|
|
|
|
2014-11-17 19:09:25 +01:00
|
|
|
|
2018-07-15 23:08:23 +02:00
|
|
|
class Label:
|
2014-11-17 19:09:25 +01:00
|
|
|
'''Label definition for virtual machines
|
|
|
|
|
|
|
|
Label specifies colour of the padlock displayed next to VM's name.
|
|
|
|
When this is a :py:class:`qubes.vm.dispvm.DispVM`, padlock is overlayed
|
|
|
|
with recycling pictogram.
|
|
|
|
|
|
|
|
:param int index: numeric identificator of label
|
|
|
|
:param str color: colour specification as in HTML (``#abcdef``)
|
|
|
|
:param str name: label's name like "red" or "green"
|
|
|
|
'''
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
def __init__(self, index, color, name):
|
2014-11-17 19:09:25 +01:00
|
|
|
#: numeric identificator of label
|
|
|
|
self.index = index
|
|
|
|
|
|
|
|
#: colour specification as in HTML (``#abcdef``)
|
|
|
|
self.color = color
|
|
|
|
|
|
|
|
#: label's name like "red" or "green"
|
|
|
|
self.name = name
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
#: freedesktop icon name, suitable for use in
|
|
|
|
#: :py:meth:`PyQt4.QtGui.QIcon.fromTheme`
|
2014-12-05 14:58:05 +01:00
|
|
|
self.icon = 'appvm-' + name
|
2014-11-17 19:09:25 +01:00
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
#: freedesktop icon name, suitable for use in
|
|
|
|
#: :py:meth:`PyQt4.QtGui.QIcon.fromTheme` on DispVMs
|
2014-12-05 14:58:05 +01:00
|
|
|
self.icon_dispvm = 'dispvm-' + name
|
|
|
|
|
2014-11-17 19:09:25 +01:00
|
|
|
|
2014-11-21 16:51:59 +01:00
|
|
|
@classmethod
|
|
|
|
def fromxml(cls, xml):
|
|
|
|
'''Create label definition from XML node
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
:param lxml.etree._Element xml: XML node reference
|
|
|
|
:rtype: :py:class:`qubes.Label`
|
2014-11-21 16:51:59 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
index = int(xml.get('id').split('-', 1)[1])
|
|
|
|
color = xml.get('color')
|
|
|
|
name = xml.text
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
return cls(index, color, name)
|
|
|
|
|
|
|
|
|
|
|
|
def __xml__(self):
|
2015-01-07 14:22:12 +01:00
|
|
|
element = lxml.etree.Element(
|
2015-06-23 22:46:56 +02:00
|
|
|
'label', id='label-{}'.format(self.index), color=self.color)
|
2014-12-05 14:58:05 +01:00
|
|
|
element.text = self.name
|
|
|
|
return element
|
|
|
|
|
2016-02-11 02:52:06 +01:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
2014-11-21 16:51:59 +01:00
|
|
|
|
2014-11-17 19:09:25 +01:00
|
|
|
def __repr__(self):
|
2015-05-31 11:10:12 +02:00
|
|
|
return '{}({!r}, {!r}, {!r})'.format(
|
2014-11-17 19:09:25 +01:00
|
|
|
self.__class__.__name__,
|
|
|
|
self.index,
|
|
|
|
self.color,
|
2014-12-05 14:58:05 +01:00
|
|
|
self.name)
|
2014-11-17 19:09:25 +01:00
|
|
|
|
2017-03-13 15:13:20 +01:00
|
|
|
def __eq__(self, other):
|
|
|
|
if isinstance(other, Label):
|
|
|
|
return self.name == other.name
|
|
|
|
return NotImplemented
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2017-04-25 22:50:12 +02:00
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.name)
|
|
|
|
|
2017-01-18 22:16:46 +01:00
|
|
|
@builtins.property
|
2014-11-17 19:09:25 +01:00
|
|
|
def icon_path(self):
|
|
|
|
'''Icon path
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
.. deprecated:: 2.0
|
|
|
|
use :py:meth:`PyQt4.QtGui.QIcon.fromTheme` and :py:attr:`icon`
|
|
|
|
'''
|
2015-01-20 14:09:47 +01:00
|
|
|
return os.path.join(qubes.config.system_path['qubes_icon_dir'],
|
|
|
|
self.icon) + ".png"
|
2014-11-17 19:09:25 +01:00
|
|
|
|
|
|
|
|
2017-01-18 22:16:46 +01:00
|
|
|
@builtins.property
|
2014-12-05 14:58:05 +01:00
|
|
|
def icon_path_dispvm(self):
|
|
|
|
'''Icon path
|
|
|
|
|
|
|
|
.. deprecated:: 2.0
|
|
|
|
use :py:meth:`PyQt4.QtGui.QIcon.fromTheme` and :py:attr:`icon_dispvm`
|
|
|
|
'''
|
2015-01-20 14:09:47 +01:00
|
|
|
return os.path.join(qubes.config.system_path['qubes_icon_dir'],
|
|
|
|
self.icon_dispvm) + ".png"
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
|
2018-07-15 23:08:23 +02:00
|
|
|
class property: # pylint: disable=redefined-builtin,invalid-name
|
2014-12-05 14:58:05 +01:00
|
|
|
'''Qubes property.
|
|
|
|
|
|
|
|
This class holds one property that can be saved to and loaded from
|
|
|
|
:file:`qubes.xml`. It is used for both global and per-VM properties.
|
|
|
|
|
2015-01-08 17:42:34 +01:00
|
|
|
Property can be unset by ordinary ``del`` statement or assigning
|
|
|
|
:py:attr:`DEFAULT` special value to it. After deletion (or before first
|
|
|
|
assignment/load) attempting to read a property will get its default value
|
|
|
|
or, when no default, py:class:`exceptions.AttributeError`.
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
:param str name: name of the property
|
2020-04-07 23:30:21 +02:00
|
|
|
:param collections.abc.Callable setter: if not :py:obj:`None`, this is \
|
|
|
|
used to initialise value; first parameter to the function is holder \
|
|
|
|
instance and the second is value; this is called before ``type``
|
|
|
|
:param collections.abc.Callable saver: function to coerce value to \
|
|
|
|
something readable by setter
|
2014-12-05 14:58:05 +01:00
|
|
|
:param type type: if not :py:obj:`None`, value is coerced to this type
|
2015-01-19 17:06:30 +01:00
|
|
|
:param object default: default value; if callable, will be called with \
|
|
|
|
holder as first argument
|
|
|
|
:param int load_stage: stage when property should be loaded (see \
|
|
|
|
:py:class:`Qubes` for description of stages)
|
2014-12-05 14:58:05 +01:00
|
|
|
:param int order: order of evaluation (bigger order values are later)
|
2016-02-24 01:08:32 +01:00
|
|
|
:param bool clone: :py:meth:`PropertyHolder.clone_properties` will not \
|
|
|
|
include this property by default if :py:obj:`False`
|
2015-01-19 17:06:30 +01:00
|
|
|
:param str doc: docstring; this should be one paragraph of plain RST, no \
|
|
|
|
sphinx-specific features
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
Setters and savers have following signatures:
|
|
|
|
|
|
|
|
.. :py:function:: setter(self, prop, value)
|
|
|
|
:noindex:
|
|
|
|
|
|
|
|
:param self: instance of object that is holding property
|
|
|
|
:param prop: property object
|
|
|
|
:param value: value being assigned
|
|
|
|
|
|
|
|
.. :py:function:: saver(self, prop, value)
|
|
|
|
:noindex:
|
|
|
|
|
|
|
|
:param self: instance of object that is holding property
|
|
|
|
:param prop: property object
|
|
|
|
:param value: value being saved
|
|
|
|
:rtype: str
|
|
|
|
:raises property.DontSave: when property should not be saved at all
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
'''
|
|
|
|
|
2015-01-08 17:42:34 +01:00
|
|
|
#: Assigning this value to property means setting it to its default value.
|
|
|
|
#: If property has no default value, this will unset it.
|
|
|
|
DEFAULT = object()
|
|
|
|
|
2015-01-08 19:35:59 +01:00
|
|
|
# internal use only
|
|
|
|
_NO_DEFAULT = object()
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
def __init__(self, name, setter=None, saver=None, type=None,
|
2015-09-25 21:36:35 +02:00
|
|
|
default=_NO_DEFAULT, write_once=False, load_stage=2, order=0,
|
2016-02-24 01:08:32 +01:00
|
|
|
save_via_ref=False, clone=True,
|
2017-04-10 03:39:11 +02:00
|
|
|
doc=None):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=redefined-builtin
|
2014-12-05 14:58:05 +01:00
|
|
|
self.__name__ = name
|
2019-02-18 19:43:34 +01:00
|
|
|
if setter is None and type is bool:
|
|
|
|
setter = qubes.property.bool
|
2014-12-05 14:58:05 +01:00
|
|
|
self._setter = setter
|
2015-01-07 14:22:12 +01:00
|
|
|
self._saver = saver if saver is not None else (
|
|
|
|
lambda self, prop, value: str(value))
|
2017-03-11 19:04:50 +01:00
|
|
|
self.type = type
|
2014-12-05 14:58:05 +01:00
|
|
|
self._default = default
|
2017-11-09 18:17:58 +01:00
|
|
|
self._default_function = None
|
2020-04-07 23:30:21 +02:00
|
|
|
if isinstance(default, collections.abc.Callable):
|
2017-11-09 18:17:58 +01:00
|
|
|
self._default_function = default
|
|
|
|
|
2015-09-25 21:36:35 +02:00
|
|
|
self._write_once = write_once
|
2014-12-05 14:58:05 +01:00
|
|
|
self.order = order
|
|
|
|
self.load_stage = load_stage
|
|
|
|
self.save_via_ref = save_via_ref
|
2016-02-24 01:08:32 +01:00
|
|
|
self.clone = clone
|
2014-12-05 14:58:05 +01:00
|
|
|
self.__doc__ = doc
|
|
|
|
self._attr_name = '_qubesprop_' + name
|
|
|
|
|
|
|
|
def __get__(self, instance, owner):
|
|
|
|
if instance is None:
|
|
|
|
return self
|
|
|
|
|
|
|
|
# XXX this violates duck typing, shall we keep it?
|
|
|
|
if not isinstance(instance, PropertyHolder):
|
2015-01-19 17:06:30 +01:00
|
|
|
raise AttributeError('qubes.property should be used on '
|
|
|
|
'qubes.PropertyHolder instances only')
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
return getattr(instance, self._attr_name)
|
|
|
|
|
|
|
|
except AttributeError:
|
2017-10-20 01:24:43 +02:00
|
|
|
return self.get_default(instance)
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2017-10-20 01:24:43 +02:00
|
|
|
def get_default(self, instance):
|
|
|
|
if self._default is self._NO_DEFAULT:
|
|
|
|
raise AttributeError(
|
|
|
|
'property {!r} have no default'.format(self.__name__))
|
2019-02-27 16:19:29 +01:00
|
|
|
if self._default_function:
|
2017-11-09 18:17:58 +01:00
|
|
|
return self._default_function(instance)
|
2019-02-27 16:19:29 +01:00
|
|
|
return self._default
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
def __set__(self, instance, value):
|
2015-09-25 21:36:35 +02:00
|
|
|
self._enforce_write_once(instance)
|
|
|
|
|
2015-01-08 17:42:34 +01:00
|
|
|
if value is self.__class__.DEFAULT:
|
|
|
|
self.__delete__(instance)
|
|
|
|
return
|
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
try:
|
|
|
|
oldvalue = getattr(instance, self.__name__)
|
|
|
|
has_oldvalue = True
|
|
|
|
except AttributeError:
|
|
|
|
has_oldvalue = False
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
if self._setter is not None:
|
|
|
|
value = self._setter(instance, self, value)
|
2017-03-11 19:04:50 +01:00
|
|
|
if self.type not in (None, type(value)):
|
|
|
|
value = self.type(value)
|
2014-12-09 18:34:00 +01:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
if has_oldvalue:
|
2017-06-23 17:29:09 +02:00
|
|
|
instance.fire_event('property-pre-set:' + self.__name__,
|
|
|
|
pre_event=True,
|
2017-02-21 14:09:06 +01:00
|
|
|
name=self.__name__, newvalue=value, oldvalue=oldvalue)
|
2014-12-29 12:46:16 +01:00
|
|
|
else:
|
2017-06-23 17:29:09 +02:00
|
|
|
instance.fire_event('property-pre-set:' + self.__name__,
|
|
|
|
pre_event=True,
|
2017-02-21 14:09:06 +01:00
|
|
|
name=self.__name__, newvalue=value)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2017-10-20 01:06:06 +02:00
|
|
|
instance._property_init(self, value) # pylint: disable=protected-access
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
if has_oldvalue:
|
2017-02-21 14:09:06 +01:00
|
|
|
instance.fire_event('property-set:' + self.__name__,
|
|
|
|
name=self.__name__, newvalue=value, oldvalue=oldvalue)
|
2014-12-09 18:34:00 +01:00
|
|
|
else:
|
2017-02-21 14:09:06 +01:00
|
|
|
instance.fire_event('property-set:' + self.__name__,
|
|
|
|
name=self.__name__, newvalue=value)
|
2014-12-09 18:34:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
def __delete__(self, instance):
|
2015-09-25 21:36:35 +02:00
|
|
|
self._enforce_write_once(instance)
|
|
|
|
|
2015-01-08 17:45:34 +01:00
|
|
|
try:
|
2017-10-20 01:38:44 +02:00
|
|
|
oldvalue = getattr(instance, self.__name__)
|
2015-01-08 17:45:34 +01:00
|
|
|
has_oldvalue = True
|
|
|
|
except AttributeError:
|
|
|
|
has_oldvalue = False
|
|
|
|
|
|
|
|
if has_oldvalue:
|
2017-06-23 17:29:09 +02:00
|
|
|
instance.fire_event('property-pre-del:' + self.__name__,
|
|
|
|
pre_event=True,
|
2017-02-21 14:09:06 +01:00
|
|
|
name=self.__name__, oldvalue=oldvalue)
|
2017-10-20 01:38:44 +02:00
|
|
|
try:
|
|
|
|
delattr(instance, self._attr_name)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2016-03-04 18:04:39 +01:00
|
|
|
instance.fire_event('property-del:' + self.__name__,
|
2017-02-21 14:09:06 +01:00
|
|
|
name=self.__name__, oldvalue=oldvalue)
|
2016-04-06 18:09:57 +02:00
|
|
|
|
2015-01-08 17:45:34 +01:00
|
|
|
else:
|
2017-06-23 17:29:09 +02:00
|
|
|
instance.fire_event('property-pre-del:' + self.__name__,
|
|
|
|
pre_event=True,
|
2017-02-21 14:09:06 +01:00
|
|
|
name=self.__name__)
|
2016-03-04 18:04:39 +01:00
|
|
|
instance.fire_event('property-del:' + self.__name__,
|
2017-02-21 14:09:06 +01:00
|
|
|
name=self.__name__)
|
2015-01-08 17:45:34 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
def __repr__(self):
|
2015-07-03 17:06:24 +02:00
|
|
|
default = ' default={!r}'.format(self._default) \
|
|
|
|
if self._default is not self._NO_DEFAULT \
|
|
|
|
else ''
|
|
|
|
return '<{} object at {:#x} name={!r}{}>'.format(
|
2017-05-19 18:42:03 +02:00
|
|
|
self.__class__.__name__, id(self), self.__name__, default)
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2017-05-19 18:42:03 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.__name__
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.__name__)
|
|
|
|
|
2017-02-19 00:04:23 +01:00
|
|
|
def __lt__(self, other):
|
|
|
|
if isinstance(other, property):
|
2017-02-23 22:30:01 +01:00
|
|
|
return (self.load_stage, self.order, self.__name__) <\
|
|
|
|
(other.load_stage, other.order, other.__name__)
|
|
|
|
return NotImplemented
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
2017-02-27 21:57:56 +01:00
|
|
|
if isinstance(other, str):
|
|
|
|
return self.__name__ == other
|
2016-02-10 16:49:46 +01:00
|
|
|
return isinstance(other, property) and self.__name__ == other.__name__
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
|
2015-09-25 21:36:35 +02:00
|
|
|
def _enforce_write_once(self, instance):
|
|
|
|
if self._write_once and not instance.property_is_default(self):
|
|
|
|
raise AttributeError(
|
|
|
|
'property {!r} is write-once and already set'.format(
|
|
|
|
self.__name__))
|
|
|
|
|
2017-03-28 20:43:53 +02:00
|
|
|
def sanitize(self, *, untrusted_newvalue):
|
|
|
|
'''Coarse sanitization of value to be set, before sending it to a
|
|
|
|
setter. Can raise QubesValueError if the value is invalid.
|
|
|
|
|
|
|
|
:param untrusted_newvalue: value to be validated
|
2017-06-27 02:57:50 +02:00
|
|
|
:return: sanitized value
|
|
|
|
:raises: qubes.exc.QubesValueError
|
2017-03-28 20:43:53 +02:00
|
|
|
'''
|
|
|
|
# do not treat type='str' as sufficient validation
|
|
|
|
if self.type is not None and self.type is not str:
|
|
|
|
# assume specific type will preform enough validation
|
2017-06-26 12:55:44 +02:00
|
|
|
try:
|
|
|
|
untrusted_newvalue = untrusted_newvalue.decode('ascii',
|
|
|
|
errors='strict')
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
raise qubes.exc.QubesValueError
|
2017-03-28 20:43:53 +02:00
|
|
|
if self.type is bool:
|
|
|
|
return self.bool(None, None, untrusted_newvalue)
|
2018-07-15 23:08:23 +02:00
|
|
|
try:
|
|
|
|
return self.type(untrusted_newvalue)
|
|
|
|
except ValueError:
|
|
|
|
raise qubes.exc.QubesValueError
|
2017-03-28 20:43:53 +02:00
|
|
|
else:
|
|
|
|
# 'str' or not specified type
|
|
|
|
try:
|
|
|
|
untrusted_newvalue = untrusted_newvalue.decode('ascii',
|
|
|
|
errors='strict')
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
raise qubes.exc.QubesValueError
|
|
|
|
allowed_set = string.printable
|
|
|
|
if not all(x in allowed_set for x in untrusted_newvalue):
|
|
|
|
raise qubes.exc.QubesValueError(
|
|
|
|
'Invalid characters in property value')
|
|
|
|
return untrusted_newvalue
|
|
|
|
|
2015-09-25 21:36:35 +02:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
#
|
|
|
|
# exceptions
|
|
|
|
#
|
|
|
|
|
|
|
|
class DontSave(Exception):
|
2015-09-17 12:08:03 +02:00
|
|
|
'''This exception may be raised from saver to sign that property should
|
2014-12-29 12:46:16 +01:00
|
|
|
not be saved.
|
|
|
|
'''
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def dontsave(self, prop, value):
|
|
|
|
'''Dummy saver that never saves anything.'''
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=bad-staticmethod-argument,unused-argument
|
2015-01-20 14:09:47 +01:00
|
|
|
raise property.DontSave()
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
#
|
|
|
|
# some setters provided
|
|
|
|
#
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def forbidden(self, prop, value):
|
2014-12-29 12:46:16 +01:00
|
|
|
'''Property setter that forbids loading a property.
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
This is used to effectively disable property in classes which inherit
|
|
|
|
unwanted property. When someone attempts to load such a property, it
|
|
|
|
|
|
|
|
:throws AttributeError: always
|
2015-01-19 19:02:28 +01:00
|
|
|
''' # pylint: disable=bad-staticmethod-argument,unused-argument
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
raise AttributeError(
|
|
|
|
'setting {} property on {} instance is forbidden'.format(
|
|
|
|
prop.__name__, self.__class__.__name__))
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
@staticmethod
|
|
|
|
def bool(self, prop, value):
|
|
|
|
'''Property setter for boolean properties.
|
|
|
|
|
|
|
|
It accepts (case-insensitive) ``'0'``, ``'no'`` and ``false`` as
|
|
|
|
:py:obj:`False` and ``'1'``, ``'yes'`` and ``'true'`` as
|
|
|
|
:py:obj:`True`.
|
2015-01-19 19:02:28 +01:00
|
|
|
''' # pylint: disable=bad-staticmethod-argument,unused-argument
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2017-01-18 22:16:46 +01:00
|
|
|
if isinstance(value, str):
|
2015-09-25 21:49:59 +02:00
|
|
|
lcvalue = value.lower()
|
|
|
|
if lcvalue in ('0', 'no', 'false', 'off'):
|
|
|
|
return False
|
|
|
|
if lcvalue in ('1', 'yes', 'true', 'on'):
|
|
|
|
return True
|
2017-03-15 18:23:09 +01:00
|
|
|
raise qubes.exc.QubesValueError(
|
2015-09-25 21:49:59 +02:00
|
|
|
'Invalid literal for boolean property: {!r}'.format(value))
|
|
|
|
|
|
|
|
return bool(value)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
2017-04-10 04:02:13 +02:00
|
|
|
def stateless_property(func):
|
|
|
|
'''Decorator similar to :py:class:`builtins.property`, but for properties
|
|
|
|
exposed through management API (including qvm-prefs etc)'''
|
|
|
|
return property(func.__name__,
|
|
|
|
setter=property.forbidden,
|
2020-01-05 22:11:00 +01:00
|
|
|
saver=property.dontsave,
|
2017-04-10 04:02:13 +02:00
|
|
|
default=func,
|
|
|
|
doc=func.__doc__)
|
|
|
|
|
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
class PropertyHolder(qubes.events.Emitter):
|
|
|
|
'''Abstract class for holding :py:class:`qubes.property`
|
|
|
|
|
|
|
|
Events fired by instances of this class:
|
|
|
|
|
|
|
|
.. event:: property-load (subject, event)
|
|
|
|
|
|
|
|
Fired once after all properties are loaded from XML. Individual
|
|
|
|
``property-set`` events are not fired.
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
.. event:: property-set:<propname> \
|
|
|
|
(subject, event, name, newvalue[, oldvalue])
|
2014-12-09 18:34:00 +01:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
Fired when property changes state. Signature is variable,
|
|
|
|
*oldvalue* is present only if there was an old value.
|
|
|
|
|
|
|
|
:param name: Property name
|
|
|
|
:param newvalue: New value of the property
|
|
|
|
:param oldvalue: Old value of the property
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
.. event:: property-pre-set:<propname> \
|
|
|
|
(subject, event, name, newvalue[, oldvalue])
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
Fired before property changes state. Signature is variable,
|
|
|
|
*oldvalue* is present only if there was an old value.
|
2014-12-09 18:34:00 +01:00
|
|
|
|
|
|
|
:param name: Property name
|
|
|
|
:param newvalue: New value of the property
|
|
|
|
:param oldvalue: Old value of the property
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
.. event:: property-del:<propname> \
|
|
|
|
(subject, event, name[, oldvalue])
|
2015-01-08 17:45:34 +01:00
|
|
|
|
|
|
|
Fired when property gets deleted (is set to default). Signature is
|
|
|
|
variable, *oldvalue* is present only if there was an old value.
|
|
|
|
|
|
|
|
:param name: Property name
|
|
|
|
:param oldvalue: Old value of the property
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
.. event:: property-pre-del:<propname> \
|
|
|
|
(subject, event, name[, oldvalue])
|
2015-01-08 17:45:34 +01:00
|
|
|
|
|
|
|
Fired before property gets deleted (is set to default). Signature
|
|
|
|
is variable, *oldvalue* is present only if there was an old value.
|
|
|
|
|
|
|
|
:param name: Property name
|
|
|
|
:param oldvalue: Old value of the property
|
|
|
|
|
2016-10-25 17:11:38 +02:00
|
|
|
.. event:: clone-properties (subject, event, src, proplist)
|
|
|
|
|
|
|
|
:param src: object, from which we are cloning
|
|
|
|
:param proplist: list of properties
|
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
Members:
|
|
|
|
'''
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-21 15:24:29 +01:00
|
|
|
def __init__(self, xml, **kwargs):
|
2014-12-05 14:58:05 +01:00
|
|
|
self.xml = xml
|
|
|
|
|
2015-09-25 21:36:35 +02:00
|
|
|
propvalues = {}
|
2015-01-15 12:57:44 +01:00
|
|
|
|
2017-11-09 03:15:37 +01:00
|
|
|
all_names = self.property_dict()
|
2016-07-13 20:38:46 +02:00
|
|
|
for key in list(kwargs):
|
2015-01-21 15:24:29 +01:00
|
|
|
if not key in all_names:
|
|
|
|
continue
|
2015-09-25 21:36:35 +02:00
|
|
|
propvalues[key] = kwargs.pop(key)
|
|
|
|
|
|
|
|
super(PropertyHolder, self).__init__(**kwargs)
|
|
|
|
|
|
|
|
for key, value in propvalues.items():
|
|
|
|
setattr(self, key, value)
|
2015-01-21 15:24:29 +01:00
|
|
|
|
2016-06-26 02:18:13 +02:00
|
|
|
if self.xml is not None:
|
|
|
|
# check if properties are appropriate
|
|
|
|
for node in self.xml.xpath('./properties/property'):
|
|
|
|
name = node.get('name')
|
|
|
|
if name not in all_names:
|
|
|
|
raise TypeError(
|
|
|
|
'property {!r} not applicable to {!r}'.format(
|
|
|
|
name, self.__class__.__name__))
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2017-11-09 03:15:37 +01:00
|
|
|
# pylint: disable=too-many-nested-blocks
|
|
|
|
@classmethod
|
|
|
|
def property_dict(cls, load_stage=None):
|
|
|
|
'''List all properties attached to this VM's class
|
|
|
|
|
|
|
|
:param load_stage: Filter by load stage
|
|
|
|
:type load_stage: :py:func:`int` or :py:obj:`None`
|
|
|
|
'''
|
|
|
|
|
|
|
|
# use cls.__dict__ since we must not look at parent classes
|
|
|
|
if "_property_dict" not in cls.__dict__:
|
|
|
|
cls._property_dict = {}
|
|
|
|
memo = cls._property_dict
|
|
|
|
|
|
|
|
if load_stage not in memo:
|
|
|
|
props = dict()
|
|
|
|
if load_stage is None:
|
|
|
|
for class_ in cls.__mro__:
|
|
|
|
for name in class_.__dict__:
|
|
|
|
# don't overwrite props with those from base classes
|
|
|
|
if name not in props:
|
|
|
|
prop = class_.__dict__[name]
|
|
|
|
if isinstance(prop, property):
|
|
|
|
assert name == prop.__name__
|
|
|
|
props[name] = prop
|
|
|
|
else:
|
|
|
|
for prop in cls.property_dict().values():
|
|
|
|
if prop.load_stage == load_stage:
|
|
|
|
props[prop.__name__] = prop
|
|
|
|
memo[load_stage] = props
|
|
|
|
|
|
|
|
return memo[load_stage]
|
|
|
|
|
2015-01-09 15:09:56 +01:00
|
|
|
@classmethod
|
2015-01-21 12:50:00 +01:00
|
|
|
def property_list(cls, load_stage=None):
|
2015-01-09 15:09:56 +01:00
|
|
|
'''List all properties attached to this VM's class
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
:param load_stage: Filter by load stage
|
|
|
|
:type load_stage: :py:func:`int` or :py:obj:`None`
|
|
|
|
'''
|
|
|
|
|
2017-11-09 03:15:37 +01:00
|
|
|
# use cls.__dict__ since we must not look at parent classes
|
|
|
|
if "_property_list" not in cls.__dict__:
|
|
|
|
cls._property_list = {}
|
|
|
|
memo = cls._property_list
|
|
|
|
|
|
|
|
if load_stage not in memo:
|
|
|
|
memo[load_stage] = sorted(cls.property_dict(load_stage).values())
|
|
|
|
|
|
|
|
return memo[load_stage]
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-21 12:50:00 +01:00
|
|
|
def _property_init(self, prop, value):
|
2014-12-05 14:58:05 +01:00
|
|
|
'''Initialise property to a given value, without side effects.
|
|
|
|
|
|
|
|
:param qubes.property prop: property object of particular interest
|
|
|
|
:param value: value
|
|
|
|
'''
|
|
|
|
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=protected-access
|
2015-01-21 12:50:00 +01:00
|
|
|
setattr(self, self.property_get_def(prop)._attr_name, value)
|
2015-01-08 19:35:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
def property_is_default(self, prop):
|
|
|
|
'''Check whether property is in it's default value.
|
|
|
|
|
|
|
|
Properties when unset may return some default value, so
|
|
|
|
``hasattr(vm, prop.__name__)`` is wrong in some circumstances. This
|
|
|
|
method allows for checking if the value returned is in fact it's
|
|
|
|
default value.
|
|
|
|
|
|
|
|
:param qubes.property prop: property object of particular interest
|
|
|
|
:rtype: bool
|
2015-07-03 17:06:24 +02:00
|
|
|
''' # pylint: disable=protected-access
|
2015-01-08 19:35:59 +01:00
|
|
|
|
2015-07-03 17:06:24 +02:00
|
|
|
# both property_get_def() and ._attr_name may throw AttributeError,
|
|
|
|
# which we don't want to catch
|
|
|
|
attrname = self.property_get_def(prop)._attr_name
|
|
|
|
return not hasattr(self, attrname)
|
2015-01-08 19:35:59 +01:00
|
|
|
|
2017-10-20 01:24:43 +02:00
|
|
|
def property_get_default(self, prop):
|
|
|
|
'''Get property default value.
|
|
|
|
|
|
|
|
:param qubes.property or str prop: property object of particular
|
|
|
|
interest
|
|
|
|
'''
|
|
|
|
|
|
|
|
return self.property_get_def(prop).get_default(self)
|
|
|
|
|
2015-01-08 19:35:59 +01:00
|
|
|
|
2015-01-09 15:09:56 +01:00
|
|
|
@classmethod
|
2015-01-21 12:50:00 +01:00
|
|
|
def property_get_def(cls, prop):
|
2015-01-08 19:35:59 +01:00
|
|
|
'''Return property definition object.
|
|
|
|
|
|
|
|
If prop is already :py:class:`qubes.property` instance, return the same
|
|
|
|
object.
|
|
|
|
|
|
|
|
:param prop: property object or name
|
|
|
|
:type prop: qubes.property or str
|
|
|
|
:rtype: qubes.property
|
|
|
|
'''
|
|
|
|
|
|
|
|
if isinstance(prop, qubes.property):
|
|
|
|
return prop
|
|
|
|
|
2017-11-09 03:15:37 +01:00
|
|
|
props = cls.property_dict()
|
|
|
|
if prop in props:
|
|
|
|
return props[prop]
|
2015-01-08 19:35:59 +01:00
|
|
|
|
|
|
|
raise AttributeError('No property {!r} found in {!r}'.format(
|
2015-01-09 15:09:56 +01:00
|
|
|
prop, cls))
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
def load_properties(self, load_stage=None):
|
|
|
|
'''Load properties from immediate children of XML node.
|
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
``property-set`` events are not fired for each individual property.
|
|
|
|
|
2015-01-21 15:24:29 +01:00
|
|
|
:param int load_stage: Stage of loading.
|
2014-12-05 14:58:05 +01:00
|
|
|
'''
|
|
|
|
|
2015-12-23 14:17:23 +01:00
|
|
|
if self.xml is None:
|
|
|
|
return
|
2015-01-07 14:22:12 +01:00
|
|
|
all_names = set(
|
2015-01-21 12:50:00 +01:00
|
|
|
prop.__name__ for prop in self.property_list(load_stage))
|
2014-12-05 14:58:05 +01:00
|
|
|
for node in self.xml.xpath('./properties/property'):
|
|
|
|
name = node.get('name')
|
|
|
|
value = node.get('ref') or node.text
|
|
|
|
|
|
|
|
if not name in all_names:
|
2015-01-21 15:24:29 +01:00
|
|
|
continue
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
setattr(self, name, value)
|
2014-12-09 18:34:00 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-13 15:56:10 +01:00
|
|
|
def xml_properties(self, with_defaults=False):
|
2014-12-05 14:58:05 +01:00
|
|
|
'''Iterator that yields XML nodes representing set properties.
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
:param bool with_defaults: If :py:obj:`True`, then it also includes \
|
|
|
|
properties which were not set explicite, but have default values \
|
|
|
|
filled.
|
2014-12-05 14:58:05 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
properties = lxml.etree.Element('properties')
|
|
|
|
|
2015-01-21 12:50:00 +01:00
|
|
|
for prop in self.property_list():
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=protected-access
|
2014-12-05 14:58:05 +01:00
|
|
|
try:
|
2015-01-07 14:22:12 +01:00
|
|
|
value = getattr(
|
|
|
|
self, (prop.__name__ if with_defaults else prop._attr_name))
|
2015-01-20 14:41:19 +01:00
|
|
|
except AttributeError:
|
2014-12-05 14:58:05 +01:00
|
|
|
continue
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
try:
|
|
|
|
value = prop._saver(self, prop, value)
|
|
|
|
except property.DontSave:
|
|
|
|
continue
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
element = lxml.etree.Element('property', name=prop.__name__)
|
|
|
|
if prop.save_via_ref:
|
|
|
|
element.set('ref', value)
|
|
|
|
else:
|
|
|
|
element.text = value
|
|
|
|
properties.append(element)
|
|
|
|
|
|
|
|
return properties
|
|
|
|
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
# this was clone_attrs
|
|
|
|
def clone_properties(self, src, proplist=None):
|
|
|
|
'''Clone properties from other object.
|
|
|
|
|
|
|
|
:param PropertyHolder src: source object
|
2017-09-03 03:04:56 +02:00
|
|
|
:param iterable proplist: list of properties \
|
2016-02-24 01:08:32 +01:00
|
|
|
(:py:obj:`None` or omit for all properties except those with \
|
|
|
|
:py:attr:`property.clone` set to :py:obj:`False`)
|
2014-12-29 12:46:16 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
if proplist is None:
|
2016-02-24 01:08:32 +01:00
|
|
|
proplist = [prop for prop in self.property_list()
|
|
|
|
if prop.clone]
|
2014-12-29 12:46:16 +01:00
|
|
|
else:
|
2015-01-21 12:50:00 +01:00
|
|
|
proplist = [prop for prop in self.property_list()
|
2014-12-29 12:46:16 +01:00
|
|
|
if prop.__name__ in proplist or prop in proplist]
|
|
|
|
|
2016-02-10 16:49:08 +01:00
|
|
|
for prop in proplist:
|
2014-12-29 12:46:16 +01:00
|
|
|
try:
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=protected-access
|
2015-01-21 12:50:00 +01:00
|
|
|
self._property_init(prop, getattr(src, prop._attr_name))
|
2014-12-29 12:46:16 +01:00
|
|
|
except AttributeError:
|
|
|
|
continue
|
|
|
|
|
2017-02-21 14:09:06 +01:00
|
|
|
self.fire_event('clone-properties', src=src, proplist=proplist)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
2015-01-21 12:50:00 +01:00
|
|
|
def property_require(self, prop, allow_none=False, hard=False):
|
2015-01-08 19:35:59 +01:00
|
|
|
'''Complain badly when property is not set.
|
|
|
|
|
|
|
|
:param prop: property name or object
|
|
|
|
:type prop: qubes.property or str
|
2015-01-19 17:06:30 +01:00
|
|
|
:param bool allow_none: if :py:obj:`True`, don't complain if \
|
|
|
|
:py:obj:`None` is found
|
|
|
|
:param bool hard: if :py:obj:`True`, raise :py:class:`AssertionError`; \
|
|
|
|
if :py:obj:`False`, log warning instead
|
2015-01-08 19:35:59 +01:00
|
|
|
'''
|
|
|
|
|
2015-06-24 15:45:19 +02:00
|
|
|
if isinstance(prop, qubes.property):
|
2015-01-08 19:35:59 +01:00
|
|
|
prop = prop.__name__
|
|
|
|
|
|
|
|
try:
|
|
|
|
value = getattr(self, prop)
|
|
|
|
if value is None and not allow_none:
|
2019-09-19 20:20:36 +02:00
|
|
|
msg = 'Property {!r} cannot be None'.format(prop)
|
|
|
|
if hard:
|
|
|
|
raise ValueError(msg)
|
|
|
|
self.log.fatal(msg)
|
2015-01-08 19:35:59 +01:00
|
|
|
except AttributeError:
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=no-member
|
2015-01-08 19:35:59 +01:00
|
|
|
msg = 'Required property {!r} not set on {!r}'.format(prop, self)
|
|
|
|
if hard:
|
2018-10-29 20:16:41 +01:00
|
|
|
raise ValueError(msg)
|
2019-02-27 16:19:29 +01:00
|
|
|
# pylint: disable=no-member
|
|
|
|
self.log.fatal(msg)
|
2015-01-08 19:35:59 +01:00
|
|
|
|
2017-08-28 14:24:48 +02:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
super().close()
|
|
|
|
|
|
|
|
# Remove all properties -- somewhere in them there are cyclic
|
|
|
|
# references. This just removes all the properties, just in case.
|
|
|
|
# They are removed directly, bypassing write_once.
|
|
|
|
for prop in self.property_list():
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
try:
|
|
|
|
delattr(self, prop._attr_name)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2016-06-02 22:02:06 +02:00
|
|
|
# pylint: disable=wrong-import-position
|
2016-04-28 16:00:29 +02:00
|
|
|
from qubes.vm import VMProperty
|
|
|
|
from qubes.app import Qubes
|
2015-01-08 19:35:59 +01:00
|
|
|
|
2016-04-28 16:00:29 +02:00
|
|
|
__all__ = [
|
|
|
|
'Label',
|
|
|
|
'PropertyHolder',
|
|
|
|
'Qubes',
|
|
|
|
'VMProperty',
|
|
|
|
'property',
|
|
|
|
]
|