2017-06-03 04:52:30 +02:00
|
|
|
# -*- encoding: utf8 -*-
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2017 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@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.
|
2017-06-03 04:52:30 +02:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2017-06-03 04:52:30 +02: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.
|
2017-06-03 04:52:30 +02: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/>.
|
2017-06-03 04:52:30 +02:00
|
|
|
|
|
|
|
''' Interface for methods not being part of Admin API, but still handled by
|
|
|
|
qubesd. '''
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import string
|
|
|
|
|
|
|
|
import qubes.api
|
|
|
|
import qubes.api.admin
|
|
|
|
import qubes.vm.dispvm
|
|
|
|
|
|
|
|
|
|
|
|
class QubesMiscAPI(qubes.api.AbstractQubesAPI):
|
2017-06-06 15:49:19 +02:00
|
|
|
SOCKNAME = '/var/run/qubesd.misc.sock'
|
|
|
|
|
2017-06-03 04:52:30 +02:00
|
|
|
@qubes.api.method('qubes.FeaturesRequest', no_payload=True)
|
|
|
|
@asyncio.coroutine
|
|
|
|
def qubes_features_request(self):
|
|
|
|
''' qubes.FeaturesRequest handler
|
|
|
|
|
|
|
|
VM (mostly templates) can request some features from dom0 for itself.
|
|
|
|
Then dom0 (qubesd extension) may respect this request or ignore it.
|
|
|
|
|
|
|
|
Technically, VM first write requested features into QubesDB in
|
|
|
|
`/features-request/` subtree, then call this method. The method will
|
|
|
|
dispatch 'features-request' event, which may be handled by
|
|
|
|
appropriate extensions. Requests not explicitly handled by some
|
|
|
|
extension are ignored.
|
|
|
|
'''
|
|
|
|
assert self.dest.name == 'dom0'
|
|
|
|
assert not self.arg
|
|
|
|
|
|
|
|
prefix = '/features-request/'
|
|
|
|
|
2017-07-24 13:02:41 +02:00
|
|
|
keys = self.src.untrusted_qdb.list(prefix)
|
2017-06-03 04:52:30 +02:00
|
|
|
untrusted_features = {key[len(prefix):]:
|
2017-07-21 23:11:24 +02:00
|
|
|
self.src.untrusted_qdb.read(key).decode('ascii', errors='strict')
|
2017-06-14 03:45:08 +02:00
|
|
|
for key in keys}
|
2017-06-03 04:52:30 +02:00
|
|
|
|
|
|
|
safe_set = string.ascii_letters + string.digits
|
|
|
|
for untrusted_key in untrusted_features:
|
|
|
|
untrusted_value = untrusted_features[untrusted_key]
|
|
|
|
assert all((c in safe_set) for c in untrusted_value)
|
|
|
|
|
2018-02-28 05:33:03 +01:00
|
|
|
yield from self.src.fire_event_async('features-request',
|
2017-06-03 04:52:30 +02:00
|
|
|
untrusted_features=untrusted_features)
|
|
|
|
self.app.save()
|
|
|
|
|
|
|
|
@qubes.api.method('qubes.NotifyTools', no_payload=True)
|
|
|
|
@asyncio.coroutine
|
|
|
|
def qubes_notify_tools(self):
|
|
|
|
'''
|
|
|
|
Legacy version of qubes.FeaturesRequest, used by Qubes Windows Tools
|
|
|
|
'''
|
|
|
|
assert self.dest.name == 'dom0'
|
|
|
|
assert not self.arg
|
|
|
|
|
2017-06-12 12:22:39 +02:00
|
|
|
untrusted_features = {}
|
|
|
|
safe_set = string.ascii_letters + string.digits
|
2017-06-14 03:58:55 +02:00
|
|
|
expected_features = ('qrexec', 'gui', 'default-user')
|
2017-06-12 12:22:39 +02:00
|
|
|
for feature in expected_features:
|
2017-07-21 23:11:24 +02:00
|
|
|
untrusted_value = self.src.untrusted_qdb.read(
|
|
|
|
'/qubes-tools/' + feature)
|
2017-06-12 12:22:39 +02:00
|
|
|
if untrusted_value:
|
|
|
|
untrusted_value = untrusted_value.decode('ascii',
|
|
|
|
errors='strict')
|
|
|
|
assert all((c in safe_set) for c in untrusted_value)
|
|
|
|
untrusted_features[feature] = untrusted_value
|
|
|
|
del untrusted_value
|
2017-06-03 04:52:30 +02:00
|
|
|
|
2018-02-28 05:33:03 +01:00
|
|
|
yield from self.src.fire_event_async('features-request',
|
2017-06-12 12:22:39 +02:00
|
|
|
untrusted_features=untrusted_features)
|
2017-06-03 04:52:30 +02:00
|
|
|
self.app.save()
|
|
|
|
|
|
|
|
@qubes.api.method('qubes.NotifyUpdates')
|
|
|
|
@asyncio.coroutine
|
|
|
|
def qubes_notify_updates(self, untrusted_payload):
|
|
|
|
'''
|
|
|
|
Receive VM notification about updates availability
|
|
|
|
|
|
|
|
Payload contains a single integer - either 0 (no updates) or some
|
|
|
|
positive value (some updates).
|
|
|
|
'''
|
|
|
|
|
|
|
|
untrusted_update_count = untrusted_payload.strip()
|
|
|
|
assert untrusted_update_count.isdigit()
|
|
|
|
# now sanitized
|
|
|
|
update_count = int(untrusted_update_count)
|
|
|
|
del untrusted_update_count
|
|
|
|
|
|
|
|
if self.src.updateable:
|
|
|
|
# Just trust information from VM itself
|
2017-06-12 12:34:23 +02:00
|
|
|
self.src.features['updates-available'] = bool(update_count)
|
2017-06-03 04:52:30 +02:00
|
|
|
self.app.save()
|
|
|
|
elif getattr(self.src, 'template', None) is not None:
|
|
|
|
# Hint about updates availability in template
|
|
|
|
# If template is running - it will notify about updates itself
|
|
|
|
if self.src.template.is_running():
|
|
|
|
return
|
|
|
|
# Ignore no-updates info
|
|
|
|
if update_count > 0:
|
|
|
|
# If VM is outdated, updates were probably already installed
|
|
|
|
# in the template - ignore info
|
|
|
|
if self.src.storage.outdated_volumes:
|
|
|
|
return
|
2017-06-12 12:34:23 +02:00
|
|
|
self.src.template.features['updates-available'] = bool(
|
|
|
|
update_count)
|
2017-06-03 04:52:30 +02:00
|
|
|
self.app.save()
|