gui.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. # property-del <=> property-reset-to-default
  54. @qubes.ext.handler('property-del:guivm')
  55. def on_property_del(self, subject, event, name, oldvalue=None):
  56. newvalue = getattr(subject, 'guivm', None)
  57. self.on_property_set(subject, event, name, newvalue, oldvalue)
  58. @qubes.ext.handler('property-set:guivm')
  59. def on_property_set(self, subject, event, name, newvalue, oldvalue=None):
  60. # Clean other 'guivm-XXX' tags.
  61. # gui-daemon can connect to only one domain
  62. tags_list = list(subject.tags)
  63. for tag in tags_list:
  64. if tag.startswith('guivm-'):
  65. subject.tags.remove(tag)
  66. if newvalue:
  67. guivm = 'guivm-' + newvalue.name
  68. subject.tags.add(guivm)
  69. @qubes.ext.handler('domain-qdb-create')
  70. def on_domain_qdb_create(self, vm, event):
  71. for feature in ('gui-videoram-overhead', 'gui-videoram-min'):
  72. try:
  73. vm.untrusted_qdb.write(
  74. '/qubes-{}'.format(feature),
  75. vm.features.check_with_template_and_adminvm(
  76. feature))
  77. except KeyError:
  78. pass
  79. # Add GuiVM Xen ID for gui-daemon
  80. if getattr(vm, 'guivm', None):
  81. if vm != vm.guivm and vm.guivm.is_running():
  82. vm.untrusted_qdb.write('/qubes-gui-domain-xid',
  83. str(vm.guivm.xid))
  84. # Add keyboard layout from that of GuiVM
  85. kbd_layout = vm.guivm.features.get('keyboard-layout', None)
  86. if kbd_layout:
  87. vm.untrusted_qdb.write('/keyboard-layout', kbd_layout)
  88. # Set GuiVM prefix
  89. guivm_windows_prefix = vm.features.get('guivm-windows-prefix', 'GuiVM')
  90. if vm.features.get('service.guivm-gui-agent', None):
  91. vm.untrusted_qdb.write('/guivm-windows-prefix',
  92. guivm_windows_prefix)
  93. @qubes.ext.handler('property-set:default_guivm', system=True)
  94. def on_property_set_default_guivm(self, app, event, name, newvalue,
  95. oldvalue=None):
  96. for vm in app.domains:
  97. if hasattr(vm, 'guivm') and vm.property_is_default('guivm'):
  98. vm.fire_event('property-set:guivm',
  99. name='guivm', newvalue=newvalue,
  100. oldvalue=oldvalue)
  101. @qubes.ext.handler('domain-start')
  102. def on_domain_start(self, vm, event, **kwargs):
  103. attached_vms = [domain for domain in self.attached_vms(vm) if
  104. domain.is_running()]
  105. for attached_vm in attached_vms:
  106. attached_vm.untrusted_qdb.write('/qubes-gui-domain-xid',
  107. str(vm.xid))
  108. @qubes.ext.handler('domain-feature-pre-set:keyboard-layout')
  109. def on_feature_pre_set(self, subject, event, feature, value, oldvalue=None):
  110. untrusted_xkb_layout = value.split('+')
  111. if len(untrusted_xkb_layout) != 3:
  112. raise qubes.exc.QubesValueError("Invalid number of parameters")
  113. untrusted_layout = untrusted_xkb_layout[0]
  114. untrusted_variant = untrusted_xkb_layout[1]
  115. untrusted_options = untrusted_xkb_layout[2]
  116. re_variant = r'^[a-zA-Z0-9-_]*$'
  117. re_options = r'^[a-zA-Z0-9-_:,]*$'
  118. if not untrusted_layout.isalpha():
  119. raise qubes.exc.QubesValueError("Invalid layout provided")
  120. if not re.match(re_variant, untrusted_variant):
  121. raise qubes.exc.QubesValueError("Invalid variant provided")
  122. if not re.match(re_options, untrusted_options):
  123. raise qubes.exc.QubesValueError("Invalid options provided")