|
| 1 | +# |
| 2 | +# The Qubes OS Project, https://www.qubes-os.org/ |
| 3 | +# |
| 4 | +# Copyright (C) 2024 Frédéric Pierret <frederic.pierret@qubes-os.org> |
| 5 | +# |
| 6 | +# This library is free software; you can redistribute it and/or |
| 7 | +# modify it under the terms of the GNU Lesser General Public |
| 8 | +# License as published by the Free Software Foundation; either |
| 9 | +# version 2.1 of the License, or (at your option) any later version. |
| 10 | +# |
| 11 | +# This library is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | +# Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public |
| 17 | +# License along with this library; if not, see <https://www.gnu.org/licenses/>. |
| 18 | +# |
| 19 | + |
| 20 | +import qubes.ext |
| 21 | +import qubes.vm.remotevm |
| 22 | + |
| 23 | + |
| 24 | +class Relay(qubes.ext.Extension): |
| 25 | + # pylint: disable=unused-argument |
| 26 | + @qubes.ext.handler("domain-init", "domain-load") |
| 27 | + def on_domain_init_load(self, vm, event): |
| 28 | + if ( |
| 29 | + getattr(vm, "relayvm", None) |
| 30 | + and "relayvm-" + vm.relayvm.name not in vm.tags |
| 31 | + ): |
| 32 | + self.on_property_set(vm, event, name="relayvm", newvalue=vm.relayvm) |
| 33 | + |
| 34 | + @qubes.ext.handler("domain-start") |
| 35 | + def on_domain_start(self, vm, event, **kwargs): |
| 36 | + if not vm.untrusted_qdb: |
| 37 | + return |
| 38 | + for domain in vm.app.domains: |
| 39 | + if getattr(domain, "relayvm", None) and domain.relayvm == vm: |
| 40 | + vm.untrusted_qdb.write( |
| 41 | + f"/remote/{domain.name}", domain.remote_name or domain.name |
| 42 | + ) |
| 43 | + |
| 44 | + @qubes.ext.handler("property-reset:relayvm", vm=qubes.vm.remotevm.RemoteVM) |
| 45 | + def on_property_reset(self, subject, event, name, oldvalue=None): |
| 46 | + newvalue = getattr(subject, "relayvm", None) |
| 47 | + self.on_property_set(subject, event, name, newvalue, oldvalue) |
| 48 | + |
| 49 | + @qubes.ext.handler("property-set:relayvm", vm=qubes.vm.remotevm.RemoteVM) |
| 50 | + def on_property_set(self, subject, event, name, newvalue, oldvalue=None): |
| 51 | + # Clean other 'relayvm-XXX' tags. |
| 52 | + # qrexec-client-vm can connect to only one domain |
| 53 | + tags_list = list(subject.tags) |
| 54 | + for tag in tags_list: |
| 55 | + if tag.startswith("relayvm-"): |
| 56 | + subject.tags.remove(tag) |
| 57 | + |
| 58 | + if newvalue: |
| 59 | + relayvm_tag = "relayvm-" + newvalue.name |
| 60 | + subject.tags.add(relayvm_tag) |
| 61 | + if newvalue.untrusted_qdb: |
| 62 | + remote_name = subject.remote_name or subject.name |
| 63 | + newvalue.untrusted_qdb.write( |
| 64 | + f"/remote/{subject.name}", remote_name |
| 65 | + ) |
0 commit comments