2017-07-12 14:08:34 +02:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, https://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012 Agnieszka Kostrzewa <agnieszka.kostrzewa@gmail.com>
|
|
|
|
# Copyright (C) 2012 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2017 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
2019-07-31 02:18:05 +02:00
|
|
|
import itertools
|
2017-07-12 14:08:34 +02:00
|
|
|
import os
|
2017-09-08 22:43:43 +02:00
|
|
|
import re
|
2017-07-12 14:08:34 +02:00
|
|
|
import qubesadmin
|
2019-09-26 22:31:39 +02:00
|
|
|
import traceback
|
|
|
|
import asyncio
|
|
|
|
from contextlib import suppress
|
|
|
|
import sys
|
|
|
|
import quamash
|
|
|
|
from qubesadmin import events
|
|
|
|
|
2020-01-29 19:27:17 +01:00
|
|
|
from PyQt5 import QtWidgets, QtCore, QtGui # pylint: disable=import-error
|
2019-05-22 23:10:09 +02:00
|
|
|
|
2017-07-12 14:08:34 +02:00
|
|
|
|
|
|
|
def _filter_internal(vm):
|
2017-10-07 00:22:41 +02:00
|
|
|
return (not vm.klass == 'AdminVM'
|
2019-05-22 23:10:09 +02:00
|
|
|
and not vm.features.get('internal', False))
|
|
|
|
|
2017-07-12 14:08:34 +02:00
|
|
|
|
2020-01-29 19:27:17 +01:00
|
|
|
class SizeSpinBox(QtWidgets.QSpinBox):
|
|
|
|
# pylint: disable=invalid-name, no-self-use
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(SizeSpinBox, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
self.pattern = r'(\d+\.?\d?) ?(GB|MB)'
|
|
|
|
self.regex = re.compile(self.pattern)
|
|
|
|
self.validator = QtGui.QRegExpValidator(QtCore.QRegExp(
|
|
|
|
self.pattern), self)
|
|
|
|
|
|
|
|
def textFromValue(self, v: int) -> str:
|
|
|
|
if v > 1024:
|
|
|
|
return '{:.1f} GB'.format(v / 1024)
|
|
|
|
|
|
|
|
return '{} MB'.format(v)
|
|
|
|
|
|
|
|
def validate(self, text: str, pos: int):
|
|
|
|
return self.validator.validate(text, pos)
|
|
|
|
|
|
|
|
def valueFromText(self, text: str) -> int:
|
|
|
|
value, unit = self.regex.fullmatch(text.strip()).groups()
|
|
|
|
|
|
|
|
if unit == 'GB':
|
|
|
|
multiplier = 1024
|
|
|
|
else:
|
|
|
|
multiplier = 1
|
|
|
|
|
|
|
|
return int(float(value) * multiplier)
|
|
|
|
|
|
|
|
|
2017-07-12 14:08:34 +02:00
|
|
|
def prepare_choice(widget, holder, propname, choice, default,
|
2019-05-22 23:10:09 +02:00
|
|
|
filter_function=None, *,
|
|
|
|
icon_getter=None, allow_internal=None, allow_default=False,
|
|
|
|
allow_none=False, transform=None):
|
2017-07-12 14:08:34 +02:00
|
|
|
# for newly created vms, set propname to None
|
|
|
|
|
2020-01-08 21:20:27 +01:00
|
|
|
# clear the widget, so that prepare_choice functions can be used
|
|
|
|
# to refresh widget values
|
|
|
|
while widget.count() > 0:
|
|
|
|
widget.removeItem(0)
|
|
|
|
|
2017-07-12 14:08:34 +02:00
|
|
|
debug(
|
|
|
|
'prepare_choice(widget={widget!r}, '
|
|
|
|
'holder={holder!r}, '
|
|
|
|
'propname={propname!r}, '
|
|
|
|
'choice={choice!r}, '
|
|
|
|
'default={default!r}, '
|
|
|
|
'filter_function={filter_function!r}, '
|
|
|
|
'icon_getter={icon_getter!r}, '
|
|
|
|
'allow_internal={allow_internal!r}, '
|
|
|
|
'allow_default={allow_default!r}, '
|
|
|
|
'allow_none={allow_none!r})'.format(**locals()))
|
|
|
|
|
2018-01-19 01:14:05 +01:00
|
|
|
if propname is not None and allow_default:
|
|
|
|
default = holder.property_get_default(propname)
|
|
|
|
|
2017-07-12 14:08:34 +02:00
|
|
|
if allow_internal is None:
|
|
|
|
allow_internal = propname is None or not propname.endswith('vm')
|
|
|
|
|
|
|
|
if propname is not None:
|
2018-01-19 01:14:05 +01:00
|
|
|
if holder.property_is_default(propname):
|
|
|
|
oldvalue = qubesadmin.DEFAULT
|
|
|
|
else:
|
|
|
|
oldvalue = getattr(holder, propname)
|
2018-07-16 17:35:35 +02:00
|
|
|
if oldvalue == '':
|
|
|
|
oldvalue = None
|
2018-02-06 15:31:17 +01:00
|
|
|
if transform is not None and oldvalue is not None:
|
|
|
|
oldvalue = transform(oldvalue)
|
2017-07-12 14:08:34 +02:00
|
|
|
else:
|
|
|
|
oldvalue = object() # won't match for identity
|
|
|
|
idx = 0
|
|
|
|
|
|
|
|
choice_list = list(choice)[:]
|
|
|
|
if not allow_internal:
|
|
|
|
choice_list = filter(_filter_internal, choice_list)
|
|
|
|
if filter_function is not None:
|
|
|
|
choice_list = filter(filter_function, choice_list)
|
|
|
|
choice_list = list(choice_list)
|
|
|
|
|
|
|
|
if allow_default:
|
|
|
|
choice_list.insert(0, qubesadmin.DEFAULT)
|
|
|
|
if allow_none:
|
|
|
|
choice_list.append(None)
|
|
|
|
|
|
|
|
for i, item in enumerate(choice_list):
|
|
|
|
debug('i={} item={}'.format(i, item))
|
|
|
|
# 0: default (unset)
|
|
|
|
if item is qubesadmin.DEFAULT:
|
2018-01-19 01:14:05 +01:00
|
|
|
default_string = str(default) if default is not None else 'none'
|
|
|
|
if transform is not None:
|
|
|
|
default_string = transform(default_string)
|
2019-10-23 18:18:39 +02:00
|
|
|
text = QtCore.QCoreApplication.translate(
|
|
|
|
"ManagerUtils", 'default ({})').format(default_string)
|
2017-07-12 14:08:34 +02:00
|
|
|
# N+1: explicit None
|
|
|
|
elif item is None:
|
2019-10-23 18:18:39 +02:00
|
|
|
text = QtCore.QCoreApplication.translate("ManagerUtils", '(none)')
|
2017-07-12 14:08:34 +02:00
|
|
|
# 1..N: choices
|
|
|
|
else:
|
|
|
|
text = str(item)
|
2018-01-19 01:14:05 +01:00
|
|
|
if transform is not None:
|
|
|
|
text = transform(text)
|
2017-07-12 14:08:34 +02:00
|
|
|
|
2018-01-19 01:14:05 +01:00
|
|
|
if item == oldvalue:
|
2019-10-23 18:18:39 +02:00
|
|
|
text += QtCore.QCoreApplication.translate(
|
|
|
|
"ManagerUtils", ' (current)')
|
2017-07-12 14:08:34 +02:00
|
|
|
idx = i
|
|
|
|
|
|
|
|
widget.insertItem(i, text)
|
|
|
|
|
|
|
|
if icon_getter is not None:
|
|
|
|
icon = icon_getter(item)
|
|
|
|
if icon is not None:
|
|
|
|
widget.setItemIcon(i, icon)
|
|
|
|
|
|
|
|
widget.setCurrentIndex(idx)
|
|
|
|
|
|
|
|
return choice_list, idx
|
|
|
|
|
2019-06-10 00:27:09 +02:00
|
|
|
|
|
|
|
class KernelVersion: # pylint: disable=too-few-public-methods
|
|
|
|
# Cannot use distutils.version.LooseVersion, because it fails at handling
|
|
|
|
# versions that have no numbers in them
|
|
|
|
def __init__(self, string):
|
|
|
|
self.string = string
|
2019-07-31 02:18:05 +02:00
|
|
|
self.groups = re.compile(r'(\d+)').split(self.string)
|
2019-06-10 00:27:09 +02:00
|
|
|
|
|
|
|
def __lt__(self, other):
|
2019-07-31 02:18:05 +02:00
|
|
|
for (self_content, other_content) in itertools.zip_longest(
|
|
|
|
self.groups, other.groups):
|
|
|
|
if self_content == other_content:
|
|
|
|
continue
|
2019-11-01 21:27:14 +01:00
|
|
|
if self_content is None:
|
|
|
|
return True
|
|
|
|
if other_content is None:
|
|
|
|
return False
|
2019-07-31 02:18:05 +02:00
|
|
|
if self_content.isdigit() and other_content.isdigit():
|
|
|
|
return int(self_content) < int(other_content)
|
|
|
|
return self_content < other_content
|
|
|
|
|
2019-06-10 00:27:09 +02:00
|
|
|
|
2017-07-12 14:08:34 +02:00
|
|
|
def prepare_kernel_choice(widget, holder, propname, default, *args, **kwargs):
|
2019-11-10 10:16:08 +01:00
|
|
|
try:
|
|
|
|
app = holder.app
|
|
|
|
except AttributeError:
|
|
|
|
app = holder
|
|
|
|
kernels = [kernel.vid for kernel in app.pools['linux-kernel'].volumes]
|
|
|
|
kernels = sorted(kernels, key=KernelVersion)
|
|
|
|
|
2019-06-10 00:27:09 +02:00
|
|
|
return prepare_choice(
|
|
|
|
widget, holder, propname, kernels, default, *args, **kwargs)
|
2017-07-12 14:08:34 +02:00
|
|
|
|
2019-11-10 10:16:47 +01:00
|
|
|
|
2017-07-12 14:08:34 +02:00
|
|
|
def prepare_label_choice(widget, holder, propname, default, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
app = holder.app
|
|
|
|
except AttributeError:
|
|
|
|
app = holder
|
|
|
|
|
|
|
|
return prepare_choice(widget, holder, propname,
|
2019-05-22 23:10:09 +02:00
|
|
|
sorted(app.labels.values(), key=lambda l: l.index),
|
|
|
|
default, *args,
|
|
|
|
icon_getter=(lambda label:
|
2020-01-29 19:27:17 +01:00
|
|
|
QtGui.QIcon.fromTheme(label.icon)),
|
2019-05-22 23:10:09 +02:00
|
|
|
**kwargs)
|
|
|
|
|
2017-07-12 14:08:34 +02:00
|
|
|
|
|
|
|
def prepare_vm_choice(widget, holder, propname, default, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
app = holder.app
|
|
|
|
except AttributeError:
|
|
|
|
app = holder
|
|
|
|
|
|
|
|
return prepare_choice(widget, holder, propname, app.domains, default,
|
2019-05-22 23:10:09 +02:00
|
|
|
*args, **kwargs)
|
|
|
|
|
2017-07-12 14:08:34 +02:00
|
|
|
|
|
|
|
def is_debug():
|
|
|
|
return os.getenv('QUBES_MANAGER_DEBUG', '') not in ('', '0')
|
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
|
2017-07-12 14:08:34 +02:00
|
|
|
def debug(*args, **kwargs):
|
|
|
|
if not is_debug():
|
|
|
|
return
|
|
|
|
print(*args, **kwargs)
|
2017-09-08 22:43:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_path_from_vm(vm, service_name):
|
|
|
|
"""
|
|
|
|
Displays a file/directory selection window for the given VM.
|
|
|
|
|
|
|
|
:param vm: vm from which to select path
|
|
|
|
:param service_name: qubes.SelectFile or qubes.SelectDirectory
|
|
|
|
:return: path to file, checked for validity
|
|
|
|
"""
|
|
|
|
|
|
|
|
path_re = re.compile(r"[a-zA-Z0-9/:.,_+=() -]*")
|
|
|
|
path_max_len = 512
|
|
|
|
|
|
|
|
if not vm:
|
|
|
|
return None
|
2017-11-09 16:26:05 +01:00
|
|
|
stdout, _stderr = vm.run_service_for_stdio(service_name)
|
2017-09-08 22:43:43 +02:00
|
|
|
|
2019-03-27 18:31:26 +01:00
|
|
|
stdout = stdout.strip()
|
|
|
|
|
2017-09-08 22:43:43 +02:00
|
|
|
untrusted_path = stdout.decode(encoding='ascii')[:path_max_len]
|
|
|
|
|
2017-11-14 15:29:57 +01:00
|
|
|
if not untrusted_path:
|
2017-09-08 22:43:43 +02:00
|
|
|
return None
|
2019-02-28 06:20:50 +01:00
|
|
|
if path_re.fullmatch(untrusted_path):
|
2017-09-08 22:43:43 +02:00
|
|
|
assert '../' not in untrusted_path
|
|
|
|
assert '\0' not in untrusted_path
|
|
|
|
return untrusted_path.strip()
|
2019-10-23 18:18:39 +02:00
|
|
|
raise ValueError(QtCore.QCoreApplication.translate(
|
|
|
|
"ManagerUtils", 'Unexpected characters in path.'))
|
2018-07-20 00:03:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
def format_dependencies_list(dependencies):
|
|
|
|
"""Given a list of tuples representing properties, formats them in
|
|
|
|
a readable list."""
|
|
|
|
|
|
|
|
list_text = ""
|
|
|
|
for (holder, prop) in dependencies:
|
|
|
|
if holder is None:
|
2019-10-23 18:18:39 +02:00
|
|
|
list_text += QtCore.QCoreApplication.translate(
|
|
|
|
"ManagerUtils", "- Global property <b>{}</b> <br>").format(prop)
|
2018-07-20 00:03:37 +02:00
|
|
|
else:
|
2019-10-23 18:18:39 +02:00
|
|
|
list_text += QtCore.QCoreApplication.translate(
|
|
|
|
"ManagerUtils", "- <b>{0}</b> for qube <b>{1}</b> <br>").format(
|
2018-07-20 00:03:37 +02:00
|
|
|
prop, holder.name)
|
|
|
|
|
|
|
|
return list_text
|
2019-09-26 22:31:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
def loop_shutdown():
|
|
|
|
pending = asyncio.Task.all_tasks()
|
|
|
|
for task in pending:
|
|
|
|
with suppress(asyncio.CancelledError):
|
|
|
|
task.cancel()
|
|
|
|
|
|
|
|
|
|
|
|
# Bases on the original code by:
|
|
|
|
# Copyright (c) 2002-2007 Pascal Varet <p.varet@gmail.com>
|
|
|
|
def handle_exception(exc_type, exc_value, exc_traceback):
|
2019-09-26 22:40:40 +02:00
|
|
|
filename, line, _, _ = traceback.extract_tb(exc_traceback).pop()
|
2019-09-26 22:31:39 +02:00
|
|
|
filename = os.path.basename(filename)
|
|
|
|
error = "%s: %s" % (exc_type.__name__, exc_value)
|
|
|
|
|
|
|
|
strace = ""
|
|
|
|
stacktrace = traceback.extract_tb(exc_traceback)
|
|
|
|
while stacktrace:
|
|
|
|
(filename, line, func, txt) = stacktrace.pop()
|
|
|
|
strace += "----\n"
|
|
|
|
strace += "line: %s\n" % txt
|
|
|
|
strace += "func: %s\n" % func
|
|
|
|
strace += "line no.: %d\n" % line
|
|
|
|
strace += "file: %s\n" % filename
|
|
|
|
|
|
|
|
msg_box = QtWidgets.QMessageBox()
|
|
|
|
msg_box.setDetailedText(strace)
|
|
|
|
msg_box.setIcon(QtWidgets.QMessageBox.Critical)
|
2019-10-23 18:18:39 +02:00
|
|
|
msg_box.setWindowTitle(QtCore.QCoreApplication.translate(
|
|
|
|
"ManagerUtils", "Houston, we have a problem..."))
|
|
|
|
msg_box.setText(QtCore.QCoreApplication.translate(
|
|
|
|
"ManagerUtils", "Whoops. A critical error has occured. "
|
2019-11-10 10:16:47 +01:00
|
|
|
"This is most likely a bug in Qubes Manager.<br><br>"
|
|
|
|
"<b><i>{0}</i></b><br/>at line <b>{1}</b><br/>of file "
|
2019-10-23 18:18:39 +02:00
|
|
|
"{2}.<br/><br/>").format(error, line, filename))
|
2019-09-26 22:31:39 +02:00
|
|
|
|
|
|
|
msg_box.exec_()
|
|
|
|
|
|
|
|
|
2019-11-08 23:35:26 +01:00
|
|
|
def run_asynchronous(window_class):
|
2019-09-26 22:31:39 +02:00
|
|
|
qt_app = QtWidgets.QApplication(sys.argv)
|
2019-10-23 15:18:32 +02:00
|
|
|
|
|
|
|
translator = QtCore.QTranslator(qt_app)
|
|
|
|
locale = QtCore.QLocale.system().name()
|
|
|
|
i18n_dir = os.path.join(
|
|
|
|
os.path.dirname(os.path.realpath(__file__)),
|
|
|
|
'i18n')
|
|
|
|
translator.load("qubesmanager_{!s}.qm".format(locale), i18n_dir)
|
|
|
|
qt_app.installTranslator(translator)
|
2019-11-08 23:35:26 +01:00
|
|
|
QtCore.QCoreApplication.installTranslator(translator)
|
2019-10-23 15:18:32 +02:00
|
|
|
|
2019-09-26 22:31:39 +02:00
|
|
|
qt_app.setOrganizationName("The Qubes Project")
|
|
|
|
qt_app.setOrganizationDomain("http://qubes-os.org")
|
|
|
|
qt_app.lastWindowClosed.connect(loop_shutdown)
|
|
|
|
|
|
|
|
qubes_app = qubesadmin.Qubes()
|
|
|
|
|
|
|
|
loop = quamash.QEventLoop(qt_app)
|
|
|
|
asyncio.set_event_loop(loop)
|
|
|
|
dispatcher = events.EventsDispatcher(qubes_app)
|
|
|
|
|
|
|
|
window = window_class(qt_app, qubes_app, dispatcher)
|
2019-11-08 23:35:26 +01:00
|
|
|
|
|
|
|
if hasattr(window, "setup_application"):
|
|
|
|
window.setup_application()
|
|
|
|
|
2019-09-26 22:31:39 +02:00
|
|
|
window.show()
|
|
|
|
|
|
|
|
try:
|
|
|
|
loop.run_until_complete(
|
|
|
|
asyncio.ensure_future(dispatcher.listen_for_events()))
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
pass
|
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
loop_shutdown()
|
|
|
|
exc_type, exc_value, exc_traceback = sys.exc_info()[:3]
|
|
|
|
handle_exception(exc_type, exc_value, exc_traceback)
|
|
|
|
|
|
|
|
|
2019-11-08 23:35:26 +01:00
|
|
|
def run_synchronous(window_class):
|
2019-09-26 22:31:39 +02:00
|
|
|
qt_app = QtWidgets.QApplication(sys.argv)
|
2019-10-23 15:18:32 +02:00
|
|
|
|
|
|
|
translator = QtCore.QTranslator(qt_app)
|
|
|
|
locale = QtCore.QLocale.system().name()
|
|
|
|
i18n_dir = os.path.join(
|
|
|
|
os.path.dirname(os.path.realpath(__file__)),
|
|
|
|
'i18n')
|
|
|
|
translator.load("qubesmanager_{!s}.qm".format(locale), i18n_dir)
|
|
|
|
qt_app.installTranslator(translator)
|
2019-11-08 23:35:26 +01:00
|
|
|
QtCore.QCoreApplication.installTranslator(translator)
|
2019-10-23 15:18:32 +02:00
|
|
|
|
2019-09-26 22:31:39 +02:00
|
|
|
qt_app.setOrganizationName("The Qubes Project")
|
|
|
|
qt_app.setOrganizationDomain("http://qubes-os.org")
|
|
|
|
|
|
|
|
sys.excepthook = handle_exception
|
|
|
|
|
|
|
|
qubes_app = qubesadmin.Qubes()
|
|
|
|
|
|
|
|
window = window_class(qt_app, qubes_app)
|
|
|
|
|
2019-11-08 23:35:26 +01:00
|
|
|
if hasattr(window, "setup_application"):
|
|
|
|
window.setup_application()
|
|
|
|
|
2019-09-26 22:31:39 +02:00
|
|
|
window.show()
|
|
|
|
|
|
|
|
qt_app.exec_()
|
|
|
|
qt_app.exit()
|