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/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
2017-09-08 22:43:43 +02:00
|
|
|
import re
|
2017-07-12 14:08:34 +02:00
|
|
|
import qubesadmin
|
|
|
|
|
2017-11-14 15:29:57 +01:00
|
|
|
from PyQt4.QtGui import QIcon # pylint: disable=import-error
|
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'
|
2017-07-12 14:08:34 +02:00
|
|
|
and not vm.features.get('internal', False))
|
|
|
|
|
|
|
|
def prepare_choice(widget, holder, propname, choice, default,
|
|
|
|
filter_function=None, *,
|
|
|
|
icon_getter=None, allow_internal=None, allow_default=False,
|
2018-01-16 19:56:45 +01:00
|
|
|
allow_none=False, transform=None):
|
2017-07-12 14:08:34 +02:00
|
|
|
|
|
|
|
# for newly created vms, set propname to None
|
|
|
|
|
|
|
|
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)
|
|
|
|
text = 'default ({})'.format(default_string)
|
2017-07-12 14:08:34 +02:00
|
|
|
# N+1: explicit None
|
|
|
|
elif item is None:
|
2017-09-05 00:51:25 +02:00
|
|
|
text = '(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:
|
2017-07-12 14:08:34 +02:00
|
|
|
text += ' (current)'
|
|
|
|
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
|
|
|
|
|
|
|
|
def prepare_kernel_choice(widget, holder, propname, default, *args, **kwargs):
|
|
|
|
# TODO get from storage API (pool 'linux-kernel') (suggested by @marmarta)
|
|
|
|
return prepare_choice(widget, holder, propname,
|
|
|
|
os.listdir('/var/lib/qubes/vm-kernels'), default, *args, **kwargs)
|
|
|
|
|
|
|
|
def prepare_label_choice(widget, holder, propname, default, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
app = holder.app
|
|
|
|
except AttributeError:
|
|
|
|
app = holder
|
|
|
|
|
|
|
|
return prepare_choice(widget, holder, propname,
|
2018-01-18 23:49:17 +01:00
|
|
|
sorted(app.labels.values(), key=lambda l: l.index),
|
2017-07-12 14:08:34 +02:00
|
|
|
default, *args,
|
|
|
|
icon_getter=(lambda label: QIcon.fromTheme(label.icon)),
|
|
|
|
**kwargs)
|
|
|
|
|
|
|
|
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,
|
|
|
|
*args, **kwargs)
|
|
|
|
|
|
|
|
def is_debug():
|
|
|
|
return os.getenv('QUBES_MANAGER_DEBUG', '') not in ('', '0')
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
if path_re.match(untrusted_path):
|
|
|
|
assert '../' not in untrusted_path
|
|
|
|
assert '\0' not in untrusted_path
|
|
|
|
return untrusted_path.strip()
|
2018-07-16 02:00:28 +02:00
|
|
|
raise ValueError('Unexpected characters in path.')
|