2014-11-13 14:38:41 +01:00
|
|
|
#!/usr/bin/python2 -O
|
2015-01-19 17:06:30 +01:00
|
|
|
# vim: fileencoding=utf-8
|
|
|
|
|
|
|
|
#
|
|
|
|
# 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>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
#
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2014-11-13 18:10:27 +01:00
|
|
|
'''Qubes Virtual Machines
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
2014-11-18 17:35:05 +01:00
|
|
|
import ast
|
2014-11-13 14:38:41 +01:00
|
|
|
import collections
|
2015-01-19 18:14:15 +01:00
|
|
|
import datetime
|
2014-11-13 14:38:41 +01:00
|
|
|
import functools
|
2015-01-19 18:14:15 +01:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
2014-11-13 14:38:41 +01:00
|
|
|
import sys
|
2015-01-19 18:14:15 +01:00
|
|
|
import xml.parsers.expat
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
import lxml.etree
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
import qubes
|
2014-12-09 14:14:24 +01:00
|
|
|
import qubes.events
|
2014-11-13 14:38:41 +01:00
|
|
|
import qubes.plugins
|
|
|
|
|
|
|
|
|
2014-12-09 14:14:24 +01:00
|
|
|
class BaseVMMeta(qubes.plugins.Plugin, qubes.events.EmitterMeta):
|
2014-11-13 18:10:27 +01:00
|
|
|
'''Metaclass for :py:class:`.BaseVM`'''
|
2014-11-13 14:38:41 +01:00
|
|
|
def __init__(cls, name, bases, dict_):
|
2014-12-09 14:14:24 +01:00
|
|
|
super(BaseVMMeta, cls).__init__(name, bases, dict_)
|
2014-11-13 14:38:41 +01:00
|
|
|
cls.__hooks__ = collections.defaultdict(list)
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2014-12-17 13:29:03 +01:00
|
|
|
class DeviceCollection(object):
|
|
|
|
'''Bag for devices.
|
|
|
|
|
|
|
|
Used as default value for :py:meth:`DeviceManager.__missing__` factory.
|
|
|
|
|
|
|
|
:param vm: VM for which we manage devices
|
|
|
|
:param class_: device class
|
|
|
|
'''
|
|
|
|
|
|
|
|
def __init__(self, vm, class_):
|
|
|
|
self._vm = vm
|
|
|
|
self._class = class_
|
|
|
|
self._set = set()
|
|
|
|
|
|
|
|
|
|
|
|
def attach(self, device):
|
|
|
|
'''Attach (add) device to domain.
|
|
|
|
|
|
|
|
:param str device: device identifier (format is class-dependent)
|
|
|
|
'''
|
|
|
|
|
|
|
|
if device in self:
|
|
|
|
raise KeyError(
|
|
|
|
'device {!r} of class {} already attached to {!r}'.format(
|
|
|
|
device, self._class, self._vm))
|
2015-01-19 17:06:30 +01:00
|
|
|
self._vm.fire_event_pre('device-pre-attached:{}'.format(self._class),
|
|
|
|
device)
|
2014-12-17 13:29:03 +01:00
|
|
|
self._set.add(device)
|
|
|
|
self._vm.fire_event('device-attached:{}'.format(self._class), device)
|
|
|
|
|
|
|
|
|
|
|
|
def detach(self, device):
|
|
|
|
'''Detach (remove) device from domain.
|
|
|
|
|
|
|
|
:param str device: device identifier (format is class-dependent)
|
|
|
|
'''
|
|
|
|
|
|
|
|
if device not in self:
|
|
|
|
raise KeyError(
|
|
|
|
'device {!r} of class {} not attached to {!r}'.format(
|
|
|
|
device, self._class, self._vm))
|
2015-01-19 17:06:30 +01:00
|
|
|
self._vm.fire_event_pre('device-pre-detached:{}'.format(self._class),
|
|
|
|
device)
|
2014-12-17 13:29:03 +01:00
|
|
|
self._set.remove(device)
|
|
|
|
self._vm.fire_event('device-detached:{}'.format(self._class), device)
|
|
|
|
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self._set)
|
|
|
|
|
|
|
|
|
|
|
|
def __contains__(self, item):
|
|
|
|
return item in self._set
|
|
|
|
|
|
|
|
|
2015-01-15 12:57:44 +01:00
|
|
|
def __len__(self):
|
|
|
|
return len(self._set)
|
|
|
|
|
|
|
|
|
2014-12-17 13:29:03 +01:00
|
|
|
class DeviceManager(dict):
|
|
|
|
'''Device manager that hold all devices by their classess.
|
|
|
|
|
|
|
|
:param vm: VM for which we manage devices
|
|
|
|
'''
|
|
|
|
|
|
|
|
def __init__(self, vm):
|
|
|
|
super(DeviceManager, self).__init__()
|
|
|
|
self._vm = vm
|
|
|
|
|
|
|
|
def __missing__(self, key):
|
|
|
|
return DeviceCollection(self._vm, key)
|
|
|
|
|
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
class BaseVM(qubes.PropertyHolder):
|
2014-11-18 17:35:05 +01:00
|
|
|
'''Base class for all VMs
|
|
|
|
|
2014-12-09 14:14:24 +01:00
|
|
|
:param app: Qubes application context
|
|
|
|
:type app: :py:class:`qubes.Qubes`
|
2014-11-18 17:35:05 +01:00
|
|
|
:param xml: xml node from which to deserialise
|
|
|
|
:type xml: :py:class:`lxml.etree._Element` or :py:obj:`None`
|
|
|
|
|
2014-12-18 14:36:09 +01:00
|
|
|
This class is responsible for serializing and deserialising machines and
|
2014-11-18 17:35:05 +01:00
|
|
|
provides basic framework. It contains no management logic. For that, see
|
|
|
|
:py:class:`qubes.vm.qubesvm.QubesVM`.
|
|
|
|
'''
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=no-member
|
2014-11-18 17:35:05 +01:00
|
|
|
|
2014-12-09 14:14:24 +01:00
|
|
|
__metaclass__ = BaseVMMeta
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2014-12-09 14:14:24 +01:00
|
|
|
def __init__(self, app, xml, load_stage=2, services={}, devices=None,
|
|
|
|
tags={}, *args, **kwargs):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=redefined-outer-name
|
2015-01-19 18:03:23 +01:00
|
|
|
#: mother :py:class:`qubes.Qubes` object
|
2014-12-05 14:58:05 +01:00
|
|
|
self.app = app
|
2015-01-19 18:03:23 +01:00
|
|
|
|
|
|
|
#: dictionary of services that are run on this domain
|
2014-12-05 14:58:05 +01:00
|
|
|
self.services = services
|
2015-01-19 18:03:23 +01:00
|
|
|
|
|
|
|
#: :py:class`DeviceManager` object keeping devices that are attached to
|
|
|
|
#: this domain
|
2014-12-17 13:29:03 +01:00
|
|
|
self.devices = DeviceManager(self) if devices is None else devices
|
2015-01-19 18:03:23 +01:00
|
|
|
|
|
|
|
#: user-specified tags
|
2014-12-05 14:58:05 +01:00
|
|
|
self.tags = tags
|
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
self.events_enabled = False
|
2015-01-19 17:06:30 +01:00
|
|
|
all_names = set(prop.__name__
|
|
|
|
for prop in self.get_props_list(load_stage=2))
|
2014-12-09 14:14:24 +01:00
|
|
|
for key in list(kwargs.keys()):
|
2014-12-05 14:58:05 +01:00
|
|
|
if not key in all_names:
|
|
|
|
raise AttributeError(
|
|
|
|
'No property {!r} found in {!r}'.format(
|
|
|
|
key, self.__class__))
|
|
|
|
setattr(self, key, kwargs[key])
|
2014-12-09 14:14:24 +01:00
|
|
|
del kwargs[key]
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2014-12-09 14:14:24 +01:00
|
|
|
super(BaseVM, self).__init__(xml, *args, **kwargs)
|
2014-11-18 17:35:05 +01:00
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
self.events_enabled = True
|
|
|
|
self.fire_event('property-load')
|
|
|
|
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
def add_new_vm(self, vm):
|
|
|
|
'''Add new Virtual Machine to colletion
|
2014-11-18 17:35:05 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
'''
|
2014-11-18 17:35:05 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
vm_cls = QubesVmClasses[vm_type]
|
|
|
|
if 'template' in kwargs:
|
|
|
|
if not vm_cls.is_template_compatible(kwargs['template']):
|
2015-01-19 17:06:30 +01:00
|
|
|
raise QubesException(
|
|
|
|
'Template not compatible with selected VM type')
|
2014-11-18 17:35:05 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
vm = vm_cls(qid=qid, collection=self, **kwargs)
|
|
|
|
if not self.verify_new_vm(vm):
|
|
|
|
raise QubesException("Wrong VM description!")
|
|
|
|
self[vm.qid] = vm
|
2014-11-18 17:35:05 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
# make first created NetVM the default one
|
|
|
|
if self.default_fw_netvm_qid is None and vm.is_netvm():
|
|
|
|
self.set_default_fw_netvm(vm)
|
2014-11-18 17:35:05 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
if self.default_netvm_qid is None and vm.is_proxyvm():
|
|
|
|
self.set_default_netvm(vm)
|
|
|
|
|
|
|
|
# make first created TemplateVM the default one
|
|
|
|
if self.default_template_qid is None and vm.is_template():
|
|
|
|
self.set_default_template(vm)
|
|
|
|
|
|
|
|
# make first created ProxyVM the UpdateVM
|
|
|
|
if self.updatevm_qid is None and vm.is_proxyvm():
|
|
|
|
self.set_updatevm_vm(vm)
|
|
|
|
|
|
|
|
# by default ClockVM is the first NetVM
|
|
|
|
if self.clockvm_qid is None and vm.is_netvm():
|
|
|
|
self.set_clockvm_vm(vm)
|
|
|
|
|
|
|
|
return vm
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def fromxml(cls, app, xml, load_stage=2):
|
|
|
|
'''Create VM from XML node
|
|
|
|
|
|
|
|
:param qubes.Qubes app: :py:class:`qubes.Qubes` application instance
|
|
|
|
:param lxml.etree._Element xml: XML node reference
|
2015-01-19 17:06:30 +01:00
|
|
|
:param int load_stage: do not change the default (2) unless you know, \
|
|
|
|
what you are doing
|
2015-01-19 19:02:28 +01:00
|
|
|
''' # pylint: disable=redefined-outer-name
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
if xml is None:
|
|
|
|
return cls(app)
|
|
|
|
|
|
|
|
services = {}
|
|
|
|
devices = collections.defaultdict(list)
|
|
|
|
tags = {}
|
2014-11-18 17:35:05 +01:00
|
|
|
|
|
|
|
# services
|
2014-12-05 14:58:05 +01:00
|
|
|
for node in xml.xpath('./services/service'):
|
2015-01-19 17:06:30 +01:00
|
|
|
services[node.text] = bool(
|
|
|
|
ast.literal_eval(node.get('enabled', 'True')))
|
2014-11-18 17:35:05 +01:00
|
|
|
|
|
|
|
# devices (pci, usb, ...)
|
2014-12-05 14:58:05 +01:00
|
|
|
for parent in xml.xpath('./devices'):
|
2014-11-18 17:35:05 +01:00
|
|
|
devclass = parent.get('class')
|
|
|
|
for node in parent.xpath('./device'):
|
2014-12-05 14:58:05 +01:00
|
|
|
devices[devclass].append(node.text)
|
|
|
|
|
|
|
|
# tags
|
|
|
|
for node in xml.xpath('./tags/tag'):
|
|
|
|
tags[node.get('name')] = node.text
|
2014-11-18 17:35:05 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
# properties
|
|
|
|
self = cls(app, xml=xml, services=services, devices=devices, tags=tags)
|
|
|
|
self.load_properties(load_stage=load_stage)
|
|
|
|
|
|
|
|
# TODO: firewall, policy
|
|
|
|
|
|
|
|
# sys.stderr.write('{}.fromxml return\n'.format(cls.__name__))
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
def __xml__(self):
|
2015-01-12 18:57:37 +01:00
|
|
|
element = lxml.etree.Element('domain')
|
|
|
|
element.set('id', 'domain-' + str(self.qid))
|
|
|
|
element.set('class', self.__class__.__name__)
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-13 15:56:10 +01:00
|
|
|
element.append(self.xml_properties())
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
services = lxml.etree.Element('services')
|
|
|
|
for service in self.services:
|
|
|
|
node = lxml.etree.Element('service')
|
|
|
|
node.text = service
|
|
|
|
if not self.services[service]:
|
2015-01-12 18:57:37 +01:00
|
|
|
node.set('enabled', 'false')
|
2014-12-05 14:58:05 +01:00
|
|
|
services.append(node)
|
|
|
|
element.append(services)
|
|
|
|
|
|
|
|
for devclass in self.devices:
|
|
|
|
devices = lxml.etree.Element('devices')
|
|
|
|
devices.set('class', devclass)
|
|
|
|
for device in self.devices[devclass]:
|
|
|
|
node = lxml.etree.Element('device')
|
|
|
|
node.text = device
|
|
|
|
devices.append(node)
|
|
|
|
element.append(devices)
|
|
|
|
|
|
|
|
tags = lxml.etree.Element('tags')
|
|
|
|
for tag in self.tags:
|
|
|
|
node = lxml.etree.Element('tag', name=tag)
|
|
|
|
node.text = self.tags[tag]
|
|
|
|
tags.append(node)
|
|
|
|
element.append(tags)
|
|
|
|
|
|
|
|
return element
|
2014-11-13 14:38:41 +01:00
|
|
|
|
|
|
|
def __repr__(self):
|
2014-12-17 13:32:58 +01:00
|
|
|
proprepr = []
|
|
|
|
for prop in self.get_props_list():
|
|
|
|
try:
|
|
|
|
proprepr.append('{}={!r}'.format(
|
|
|
|
prop.__name__, getattr(self, prop.__name__)))
|
|
|
|
except AttributeError:
|
|
|
|
continue
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2014-12-17 13:32:58 +01:00
|
|
|
return '<{} object at {:#x} {}>'.format(
|
|
|
|
self.__class__.__name__, id(self), ' '.join(proprepr))
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
#
|
|
|
|
# xml serialising methods
|
|
|
|
#
|
|
|
|
|
|
|
|
@staticmethod
|
2015-01-13 15:56:10 +01:00
|
|
|
def lvxml_net_dev(ip, mac, backend):
|
2014-12-29 12:46:16 +01:00
|
|
|
'''Return ``<interface>`` node for libvirt xml.
|
|
|
|
|
|
|
|
This was previously _format_net_dev
|
|
|
|
|
|
|
|
:param str ip: IP address of the frontend
|
|
|
|
:param str mac: MAC (Ethernet) address of the frontend
|
|
|
|
:param qubes.vm.QubesVM backend: Backend domain
|
|
|
|
:rtype: lxml.etree._Element
|
|
|
|
'''
|
|
|
|
|
|
|
|
interface = lxml.etree.Element('interface', type='ethernet')
|
|
|
|
interface.append(lxml.etree.Element('mac', address=mac))
|
|
|
|
interface.append(lxml.etree.Element('ip', address=ip))
|
|
|
|
interface.append(lxml.etree.Element('domain', name=backend.name))
|
|
|
|
|
|
|
|
return interface
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2015-01-13 15:56:10 +01:00
|
|
|
def lvxml_pci_dev(address):
|
2014-12-29 12:46:16 +01:00
|
|
|
'''Return ``<hostdev>`` node for libvirt xml.
|
|
|
|
|
|
|
|
This was previously _format_pci_dev
|
|
|
|
|
|
|
|
:param str ip: IP address of the frontend
|
|
|
|
:param str mac: MAC (Ethernet) address of the frontend
|
|
|
|
:param qubes.vm.QubesVM backend: Backend domain
|
|
|
|
:rtype: lxml.etree._Element
|
|
|
|
'''
|
|
|
|
|
2015-01-20 14:41:19 +01:00
|
|
|
dev_match = re.match(r'([0-9a-f]+):([0-9a-f]+)\.([0-9a-f]+)', address)
|
2014-12-29 12:46:16 +01:00
|
|
|
if not dev_match:
|
2015-01-20 14:09:47 +01:00
|
|
|
raise qubes.QubesException(
|
|
|
|
'Invalid PCI device address: {}'.format(address))
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
hostdev = lxml.etree.Element('hostdev', type='pci', managed='yes')
|
|
|
|
source = lxml.etree.Element('source')
|
|
|
|
source.append(lxml.etree.Element('address',
|
|
|
|
bus='0x' + dev_match.group(1),
|
|
|
|
slot='0x' + dev_match.group(2),
|
|
|
|
function='0x' + dev_match.group(3)))
|
|
|
|
hostdev.append(source)
|
|
|
|
return hostdev
|
|
|
|
|
|
|
|
#
|
|
|
|
# old libvirt XML
|
|
|
|
# TODO rewrite it to do proper XML synthesis via lxml.etree
|
|
|
|
#
|
|
|
|
|
|
|
|
def get_config_params(self):
|
|
|
|
'''Return parameters for libvirt's XML domain config
|
|
|
|
|
|
|
|
.. deprecated:: 3.0-alpha This will go away.
|
|
|
|
'''
|
|
|
|
|
|
|
|
args = {}
|
|
|
|
args['name'] = self.name
|
|
|
|
if hasattr(self, 'kernels_dir'):
|
|
|
|
args['kerneldir'] = self.kernels_dir
|
|
|
|
args['uuidnode'] = '<uuid>{!r}</uuid>'.format(self.uuid) \
|
|
|
|
if hasattr(self, 'uuid') else ''
|
|
|
|
args['vmdir'] = self.dir_path
|
2015-01-13 15:56:10 +01:00
|
|
|
args['pcidevs'] = ''.join(lxml.etree.tostring(self.lvxml_pci_dev(dev))
|
2014-12-29 12:46:16 +01:00
|
|
|
for dev in self.devices['pci'])
|
|
|
|
args['maxmem'] = str(self.maxmem)
|
|
|
|
args['vcpus'] = str(self.vcpus)
|
|
|
|
args['mem'] = str(max(self.memory, self.maxmem))
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
if 'meminfo-writer' in self.services \
|
|
|
|
and not self.services['meminfo-writer']:
|
2014-12-29 12:46:16 +01:00
|
|
|
# If dynamic memory management disabled, set maxmem=mem
|
|
|
|
args['maxmem'] = args['mem']
|
|
|
|
|
|
|
|
if self.netvm is not None:
|
|
|
|
args['ip'] = self.ip
|
|
|
|
args['mac'] = self.mac
|
|
|
|
args['gateway'] = self.netvm.gateway
|
|
|
|
args['dns1'] = self.netvm.gateway
|
|
|
|
args['dns2'] = self.secondary_dns
|
|
|
|
args['netmask'] = self.netmask
|
2015-01-19 17:06:30 +01:00
|
|
|
args['netdev'] = lxml.etree.tostring(
|
|
|
|
self.lvxml_net_dev(self.ip, self.mac, self.netvm))
|
|
|
|
args['disable_network1'] = ''
|
|
|
|
args['disable_network2'] = ''
|
2014-12-29 12:46:16 +01:00
|
|
|
else:
|
|
|
|
args['ip'] = ''
|
|
|
|
args['mac'] = ''
|
|
|
|
args['gateway'] = ''
|
|
|
|
args['dns1'] = ''
|
|
|
|
args['dns2'] = ''
|
|
|
|
args['netmask'] = ''
|
|
|
|
args['netdev'] = ''
|
2015-01-19 17:06:30 +01:00
|
|
|
args['disable_network1'] = '<!--'
|
|
|
|
args['disable_network2'] = '-->'
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
args.update(self.storage.get_config_params())
|
|
|
|
|
|
|
|
if hasattr(self, 'kernelopts'):
|
|
|
|
args['kernelopts'] = self.kernelopts
|
|
|
|
if self.debug:
|
2015-01-19 17:06:30 +01:00
|
|
|
self.log.info(
|
|
|
|
"Debug mode: adding 'earlyprintk=xen' to kernel opts")
|
2014-12-29 12:46:16 +01:00
|
|
|
args['kernelopts'] += ' earlyprintk=xen'
|
|
|
|
|
|
|
|
|
|
|
|
def create_config_file(self, file_path=None, prepare_dvm=False):
|
|
|
|
'''Create libvirt's XML domain config file
|
|
|
|
|
|
|
|
If :py:attr:`qubes.vm.qubesvm.QubesVM.uses_custom_config` is true, this
|
|
|
|
does nothing.
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
:param str file_path: Path to file to create \
|
|
|
|
(default: :py:attr:`qubes.vm.qubesvm.QubesVM.conf_file`)
|
|
|
|
:param bool prepare_dvm: If we are in the process of preparing \
|
|
|
|
DisposableVM
|
2014-12-29 12:46:16 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
if file_path is None:
|
|
|
|
file_path = self.conf_file
|
|
|
|
if self.uses_custom_config:
|
|
|
|
conf_appvm = open(file_path, "r")
|
|
|
|
domain_config = conf_appvm.read()
|
|
|
|
conf_appvm.close()
|
|
|
|
return domain_config
|
|
|
|
|
|
|
|
f_conf_template = open(self.config_file_template, 'r')
|
|
|
|
conf_template = f_conf_template.read()
|
|
|
|
f_conf_template.close()
|
|
|
|
|
|
|
|
template_params = self.get_config_params()
|
|
|
|
if prepare_dvm:
|
|
|
|
template_params['name'] = '%NAME%'
|
|
|
|
template_params['privatedev'] = ''
|
2015-01-19 17:06:30 +01:00
|
|
|
template_params['netdev'] = re.sub(r"address='[0-9.]*'",
|
|
|
|
"address='%IP%'", template_params['netdev'])
|
2014-12-29 12:46:16 +01:00
|
|
|
domain_config = conf_template.format(**template_params)
|
|
|
|
|
|
|
|
# FIXME: This is only for debugging purposes
|
|
|
|
old_umask = os.umask(002)
|
|
|
|
try:
|
|
|
|
conf_appvm = open(file_path, "w")
|
|
|
|
conf_appvm.write(domain_config)
|
|
|
|
conf_appvm.close()
|
|
|
|
except:
|
|
|
|
# Ignore errors
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
os.umask(old_umask)
|
|
|
|
|
|
|
|
return domain_config
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# firewall
|
|
|
|
# TODO rewrite it, have <firewall/> node under <domain/>
|
|
|
|
# and possibly integrate with generic policy framework
|
|
|
|
#
|
|
|
|
|
|
|
|
def write_firewall_conf(self, conf):
|
|
|
|
'''Write firewall config file.
|
|
|
|
'''
|
|
|
|
defaults = self.get_firewall_conf()
|
|
|
|
expiring_rules_present = False
|
|
|
|
for item in defaults.keys():
|
|
|
|
if item not in conf:
|
|
|
|
conf[item] = defaults[item]
|
|
|
|
|
|
|
|
root = lxml.etree.Element(
|
|
|
|
"QubesFirewallRules",
|
2015-01-19 17:06:30 +01:00
|
|
|
policy=("allow" if conf["allow"] else "deny"),
|
|
|
|
dns=("allow" if conf["allowDns"] else "deny"),
|
|
|
|
icmp=("allow" if conf["allowIcmp"] else "deny"),
|
|
|
|
yumProxy=("allow" if conf["allowYumProxy"] else "deny"))
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
for rule in conf["rules"]:
|
|
|
|
# For backward compatibility
|
|
|
|
if "proto" not in rule:
|
|
|
|
if rule["portBegin"] is not None and rule["portBegin"] > 0:
|
|
|
|
rule["proto"] = "tcp"
|
|
|
|
else:
|
|
|
|
rule["proto"] = "any"
|
|
|
|
element = lxml.etree.Element(
|
|
|
|
"rule",
|
|
|
|
address=rule["address"],
|
|
|
|
proto=str(rule["proto"]),
|
|
|
|
)
|
|
|
|
if rule["netmask"] is not None and rule["netmask"] != 32:
|
|
|
|
element.set("netmask", str(rule["netmask"]))
|
|
|
|
if rule.get("portBegin", None) is not None and \
|
|
|
|
rule["portBegin"] > 0:
|
|
|
|
element.set("port", str(rule["portBegin"]))
|
|
|
|
if rule.get("portEnd", None) is not None and rule["portEnd"] > 0:
|
|
|
|
element.set("toport", str(rule["portEnd"]))
|
|
|
|
if "expire" in rule:
|
|
|
|
element.set("expire", str(rule["expire"]))
|
|
|
|
expiring_rules_present = True
|
|
|
|
|
|
|
|
root.append(element)
|
|
|
|
|
|
|
|
tree = lxml.etree.ElementTree(root)
|
|
|
|
|
|
|
|
try:
|
|
|
|
old_umask = os.umask(002)
|
2015-01-20 14:41:19 +01:00
|
|
|
with open(self.firewall_conf, 'w') as fd:
|
|
|
|
tree.write(fd, encoding="UTF-8", pretty_print=True)
|
|
|
|
fd.close()
|
2014-12-29 12:46:16 +01:00
|
|
|
os.umask(old_umask)
|
|
|
|
except EnvironmentError as err:
|
|
|
|
print >> sys.stderr, "{0}: save error: {1}".format(
|
|
|
|
os.path.basename(sys.argv[0]), err)
|
|
|
|
return False
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
# Automatically enable/disable 'yum-proxy-setup' service based on
|
|
|
|
# allowYumProxy
|
2014-12-29 12:46:16 +01:00
|
|
|
if conf['allowYumProxy']:
|
|
|
|
self.services['yum-proxy-setup'] = True
|
|
|
|
else:
|
|
|
|
if self.services.has_key('yum-proxy-setup'):
|
|
|
|
self.services.pop('yum-proxy-setup')
|
|
|
|
|
|
|
|
if expiring_rules_present:
|
|
|
|
subprocess.call(["sudo", "systemctl", "start",
|
|
|
|
"qubes-reload-firewall@%s.timer" % self.name])
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def has_firewall(self):
|
2015-01-19 17:06:30 +01:00
|
|
|
return os.path.exists(self.firewall_conf)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
def get_firewall_defaults(self):
|
2015-01-19 17:06:30 +01:00
|
|
|
return {
|
|
|
|
'rules': list(),
|
|
|
|
'allow': True,
|
|
|
|
'allowDns': True,
|
|
|
|
'allowIcmp': True,
|
|
|
|
'allowYumProxy': False}
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
def get_firewall_conf(self):
|
|
|
|
conf = self.get_firewall_defaults()
|
|
|
|
|
|
|
|
try:
|
|
|
|
tree = lxml.etree.parse(self.firewall_conf)
|
|
|
|
root = tree.getroot()
|
|
|
|
|
|
|
|
conf["allow"] = (root.get("policy") == "allow")
|
|
|
|
conf["allowDns"] = (root.get("dns") == "allow")
|
|
|
|
conf["allowIcmp"] = (root.get("icmp") == "allow")
|
|
|
|
conf["allowYumProxy"] = (root.get("yumProxy") == "allow")
|
|
|
|
|
|
|
|
for element in root:
|
|
|
|
rule = {}
|
|
|
|
attr_list = ("address", "netmask", "proto", "port", "toport",
|
|
|
|
"expire")
|
|
|
|
|
|
|
|
for attribute in attr_list:
|
|
|
|
rule[attribute] = element.get(attribute)
|
|
|
|
|
|
|
|
if rule["netmask"] is not None:
|
|
|
|
rule["netmask"] = int(rule["netmask"])
|
|
|
|
else:
|
|
|
|
rule["netmask"] = 32
|
|
|
|
|
|
|
|
if rule["port"] is not None:
|
|
|
|
rule["portBegin"] = int(rule["port"])
|
|
|
|
else:
|
|
|
|
# backward compatibility
|
|
|
|
rule["portBegin"] = 0
|
|
|
|
|
|
|
|
# For backward compatibility
|
|
|
|
if rule["proto"] is None:
|
|
|
|
if rule["portBegin"] > 0:
|
|
|
|
rule["proto"] = "tcp"
|
|
|
|
else:
|
|
|
|
rule["proto"] = "any"
|
|
|
|
|
|
|
|
if rule["toport"] is not None:
|
|
|
|
rule["portEnd"] = int(rule["toport"])
|
|
|
|
else:
|
|
|
|
rule["portEnd"] = None
|
|
|
|
|
|
|
|
if rule["expire"] is not None:
|
|
|
|
rule["expire"] = int(rule["expire"])
|
|
|
|
if rule["expire"] <= int(datetime.datetime.now().strftime(
|
|
|
|
"%s")):
|
|
|
|
continue
|
|
|
|
else:
|
2015-01-19 17:06:30 +01:00
|
|
|
del rule["expire"]
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
del rule["port"]
|
|
|
|
del rule["toport"]
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
conf["rules"].append(rule)
|
|
|
|
|
|
|
|
except EnvironmentError as err:
|
2015-01-13 15:40:43 +01:00
|
|
|
# problem accessing file, like ENOTFOUND, EPERM or sth
|
|
|
|
# return default config
|
2014-12-29 12:46:16 +01:00
|
|
|
return conf
|
2015-01-13 15:40:43 +01:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
except (xml.parsers.expat.ExpatError,
|
|
|
|
ValueError, LookupError) as err:
|
2015-01-13 15:40:43 +01:00
|
|
|
# config is invalid
|
2014-12-29 12:46:16 +01:00
|
|
|
print("{0}: load error: {1}".format(
|
|
|
|
os.path.basename(sys.argv[0]), err))
|
|
|
|
return None
|
|
|
|
|
|
|
|
return conf
|
2014-11-13 14:38:41 +01:00
|
|
|
|
|
|
|
def load(class_, D):
|
|
|
|
cls = BaseVM[class_]
|
|
|
|
return cls(D)
|
|
|
|
|
2015-01-19 18:03:23 +01:00
|
|
|
__all__ = ['BaseVMMeta', 'DeviceCollection', 'DeviceManager', 'BaseVM'] \
|
|
|
|
+ qubes.plugins.load(__file__)
|