gui.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2010-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
  5. # Copyright (C) 2013-2016 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. # Copyright (C) 2014-2018 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. # Copyright (C) 2019 Frédéric Pierret <frederic.pierret@qubes-os.org>
  9. #
  10. # This library is free software; you can redistribute it and/or
  11. # modify it under the terms of the GNU Lesser General Public
  12. # License as published by the Free Software Foundation; either
  13. # version 2.1 of the License, or (at your option) any later version.
  14. #
  15. # This library is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. # Lesser General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Lesser General Public
  21. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  22. #
  23. import re
  24. import qubes.config
  25. import qubes.ext
  26. import qubes.exc
  27. class GUI(qubes.ext.Extension):
  28. # pylint: disable=too-few-public-methods,unused-argument,no-self-use
  29. @staticmethod
  30. def attached_vms(vm):
  31. for domain in vm.app.domains:
  32. if getattr(domain, 'guivm', None) and domain.guivm == vm:
  33. yield domain
  34. @qubes.ext.handler('domain-pre-shutdown')
  35. def on_domain_pre_shutdown(self, vm, event, **kwargs):
  36. attached_vms = [domain for domain in self.attached_vms(vm) if
  37. domain.is_running()]
  38. if attached_vms and not kwargs.get('force', False):
  39. raise qubes.exc.QubesVMError(
  40. self, 'There are running VMs using this VM as GuiVM: '
  41. '{}'.format(', '.join(vm.name for vm in attached_vms)))
  42. @staticmethod
  43. def send_gui_mode(vm):
  44. vm.run_service('qubes.SetGuiMode',
  45. input=('SEAMLESS'
  46. if vm.features.get('gui-seamless', False)
  47. else 'FULLSCREEN'))
  48. @qubes.ext.handler('domain-init', 'domain-load')
  49. def on_domain_init_load(self, vm, event):
  50. if getattr(vm, 'guivm', None):
  51. if 'guivm-' + vm.guivm.name not in vm.tags:
  52. self.on_property_set(vm, event, name='guivm', newvalue=vm.guivm)
  53. @qubes.ext.handler('property-reset:guivm')
  54. def on_property_reset(self, subject, event, name, oldvalue=None):
  55. newvalue = getattr(subject, 'guivm', None)
  56. self.on_property_set(subject, event, name, newvalue, oldvalue)
  57. @qubes.ext.handler('property-set:guivm')
  58. def on_property_set(self, subject, event, name, newvalue, oldvalue=None):
  59. # Clean other 'guivm-XXX' tags.
  60. # gui-daemon can connect to only one domain
  61. tags_list = list(subject.tags)
  62. for tag in tags_list:
  63. if tag.startswith('guivm-'):
  64. subject.tags.remove(tag)
  65. if newvalue:
  66. guivm = 'guivm-' + newvalue.name
  67. subject.tags.add(guivm)
  68. @qubes.ext.handler('domain-qdb-create')
  69. def on_domain_qdb_create(self, vm, event):
  70. for feature in ('gui-videoram-overhead', 'gui-videoram-min'):
  71. try:
  72. vm.untrusted_qdb.write(
  73. '/qubes-{}'.format(feature),
  74. vm.features.check_with_template_and_adminvm(
  75. feature))
  76. except KeyError:
  77. pass
  78. # Add GuiVM Xen ID for gui-daemon
  79. if getattr(vm, 'guivm', None):
  80. if vm != vm.guivm and vm.guivm.is_running():
  81. vm.untrusted_qdb.write('/qubes-gui-domain-xid',
  82. str(vm.guivm.xid))
  83. # Add keyboard layout from that of GuiVM
  84. kbd_layout = vm.guivm.features.get('keyboard-layout', None)
  85. if kbd_layout:
  86. vm.untrusted_qdb.write('/keyboard-layout', kbd_layout)
  87. # Set GuiVM prefix
  88. guivm_windows_prefix = vm.features.get('guivm-windows-prefix', 'GuiVM')
  89. if vm.features.get('service.guivm-gui-agent', None):
  90. vm.untrusted_qdb.write('/guivm-windows-prefix',
  91. guivm_windows_prefix)
  92. @qubes.ext.handler('property-set:default_guivm', system=True)
  93. def on_property_set_default_guivm(self, app, event, name, newvalue,
  94. oldvalue=None):
  95. for vm in app.domains:
  96. if hasattr(vm, 'guivm') and vm.property_is_default('guivm'):
  97. vm.fire_event('property-set:guivm',
  98. name='guivm', newvalue=newvalue,
  99. oldvalue=oldvalue)
  100. @qubes.ext.handler('domain-start')
  101. def on_domain_start(self, vm, event, **kwargs):
  102. attached_vms = [domain for domain in self.attached_vms(vm) if
  103. domain.is_running()]
  104. for attached_vm in attached_vms:
  105. attached_vm.untrusted_qdb.write('/qubes-gui-domain-xid',
  106. str(vm.xid))
  107. @qubes.ext.handler('domain-feature-pre-set:keyboard-layout')
  108. def on_feature_pre_set(self, subject, event, feature, value, oldvalue=None):
  109. untrusted_xkb_layout = value.split('+')
  110. if len(untrusted_xkb_layout) != 3:
  111. raise qubes.exc.QubesValueError("Invalid number of parameters")
  112. untrusted_layout = untrusted_xkb_layout[0]
  113. untrusted_variant = untrusted_xkb_layout[1]
  114. untrusted_options = untrusted_xkb_layout[2]
  115. re_variant = r'^[a-zA-Z0-9-_]*$'
  116. re_options = r'^[a-zA-Z0-9-_:,]*$'
  117. if not untrusted_layout.isalpha():
  118. raise qubes.exc.QubesValueError("Invalid layout provided")
  119. if not re.match(re_variant, untrusted_variant):
  120. raise qubes.exc.QubesValueError("Invalid variant provided")
  121. if not re.match(re_options, untrusted_options):
  122. raise qubes.exc.QubesValueError("Invalid options provided")