From ba9cbf6b281137307e237a78604fe00e0561db6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Sat, 11 May 2024 00:19:27 +0200 Subject: [PATCH] Get local name from qubesdb Do not depend on hostname set inside the VM, especially since Whonix changes it. This also adds direct dependency on python3-qubesdb - previously it depended on it only indirectly (via core-agent-linux). Do include a fallback to work without qubesdb python module to allow running tests or rendering sphinx documentation without it. --- debian/control | 1 + qubesadmin/app.py | 19 ++++++++++++++++++- rpm_spec/qubes-core-admin-client.spec.in | 1 + test-packages/qubesdb.py | 6 ++++++ 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 test-packages/qubesdb.py diff --git a/debian/control b/debian/control index 6a9d1e13..4cb75513 100644 --- a/debian/control +++ b/debian/control @@ -42,6 +42,7 @@ Architecture: any Depends: python3-docutils, python3-lxml, + python3-qubesdb, python3-rpm, python3-tqdm, ${python3:Depends}, diff --git a/qubesadmin/app.py b/qubesadmin/app.py index 5c36fc08..987dbc5c 100644 --- a/qubesadmin/app.py +++ b/qubesadmin/app.py @@ -41,6 +41,12 @@ import qubesadmin.config import qubesadmin.devices +try: + import qubesdb + has_qubesdb = True +except ImportError: + has_qubesdb = False + class VMCollection(object): """Collection of VMs objects""" @@ -257,7 +263,18 @@ def remove_pool(self, name): def local_name(self): """ Get localhost name """ if not self._local_name: - self._local_name = os.uname()[1] + local_name = None + if has_qubesdb: + try: + local_qdb = qubesdb.QubesDB() + local_name_b = local_qdb.read('/name') + if local_name_b: + local_name = local_name_b.decode() + except qubesdb.Error: + pass + if local_name is None: + local_name = os.uname()[1] + self._local_name = local_name return self._local_name diff --git a/rpm_spec/qubes-core-admin-client.spec.in b/rpm_spec/qubes-core-admin-client.spec.in index 02b470e0..a0f279b2 100644 --- a/rpm_spec/qubes-core-admin-client.spec.in +++ b/rpm_spec/qubes-core-admin-client.spec.in @@ -39,6 +39,7 @@ Requires: python%{python3_pkgversion}-daemon Requires: python%{python3_pkgversion}-docutils Requires: python%{python3_pkgversion}-lxml Requires: python%{python3_pkgversion}-xcffib +Requires: python%{python3_pkgversion}-qubesdb Requires: python%{python3_pkgversion}-rpm Requires: python%{python3_pkgversion}-tqdm Requires: python%{python3_pkgversion}-pyxdg diff --git a/test-packages/qubesdb.py b/test-packages/qubesdb.py new file mode 100644 index 00000000..85ad9a10 --- /dev/null +++ b/test-packages/qubesdb.py @@ -0,0 +1,6 @@ +class QubesDB: + def read(self, key): + return b'testvm' + +class Error(Exception): + pass