gui.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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-2016 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. import os
  24. import re
  25. import subprocess
  26. import asyncio
  27. import qubes.config
  28. import qubes.ext
  29. # "LVDS connected 1024x768+0+0 (normal left inverted right) 304mm x 228mm"
  30. REGEX_OUTPUT = re.compile(r'''
  31. (?x) # ignore whitespace
  32. ^ # start of string
  33. (?P<output>[A-Za-z0-9\-]*)[ ] # LVDS VGA etc
  34. (?P<connect>(dis)?connected) # dis/connected
  35. ([ ]
  36. (?P<primary>(primary)?)[ ]?
  37. (( # a group
  38. (?P<width>\d+)x # either 1024x768+0+0
  39. (?P<height>\d+)[+]
  40. (?P<x>\d+)[+]
  41. (?P<y>\d+)
  42. )|[\D]) # or not a digit
  43. ([ ]\(.*\))?[ ]? # ignore options
  44. ( # 304mm x 228mm
  45. (?P<width_mm>\d+)mm[ ]x[ ]
  46. (?P<height_mm>\d+)mm
  47. )?
  48. .* # ignore rest of line
  49. )? # everything after (dis)connect is optional
  50. ''')
  51. def get_monitor_layout():
  52. outputs = []
  53. for line in subprocess.Popen(
  54. ['xrandr', '-q'], stdout=subprocess.PIPE).stdout:
  55. line = line.decode()
  56. if not line.startswith("Screen") and not line.startswith(" "):
  57. output_params = REGEX_OUTPUT.match(line).groupdict()
  58. if output_params['width']:
  59. phys_size = ""
  60. if output_params['width_mm']:
  61. # don't provide real values for privacy reasons - see
  62. # #1951 for details
  63. dpi = (int(output_params['width']) * 254 /
  64. int(output_params['width_mm']) / 10)
  65. if dpi > 300:
  66. dpi = 300
  67. elif dpi > 200:
  68. dpi = 200
  69. elif dpi > 150:
  70. dpi = 150
  71. else:
  72. # if lower, don't provide this info to the VM at all
  73. dpi = 0
  74. if dpi:
  75. # now calculate dimensions based on approximate DPI
  76. phys_size = " {} {}".format(
  77. int(output_params['width']) * 254 / dpi / 10,
  78. int(output_params['height']) * 254 / dpi / 10,
  79. )
  80. outputs.append("%s %s %s %s%s\n" % (
  81. output_params['width'],
  82. output_params['height'],
  83. output_params['x'],
  84. output_params['y'],
  85. phys_size,
  86. ))
  87. return outputs
  88. class GUI(qubes.ext.Extension):
  89. @staticmethod
  90. def kde_guid_args(vm):
  91. '''Return KDE-specific arguments for guid, if applicable'''
  92. guid_cmd = []
  93. # Avoid using environment variables for checking the current session,
  94. # because this script may be called with cleared env (like with sudo).
  95. if subprocess.check_output(
  96. ['xprop', '-root', '-notype', 'KDE_SESSION_VERSION']) == \
  97. 'KDE_SESSION_VERSION = 5\n':
  98. # native decoration plugins is used, so adjust window properties
  99. # accordingly
  100. guid_cmd += ['-T'] # prefix window titles with VM name
  101. # get owner of X11 session
  102. session_owner = None
  103. for line in subprocess.check_output(['xhost']).splitlines():
  104. if line == b'SI:localuser:root':
  105. pass
  106. elif line.startswith(b'SI:localuser:'):
  107. session_owner = line.split(":")[2]
  108. if session_owner is not None:
  109. data_dir = os.path.expanduser(
  110. '~{}/.local/share'.format(session_owner))
  111. else:
  112. # fallback to current user
  113. data_dir = os.path.expanduser('~/.local/share')
  114. guid_cmd += ['-p',
  115. '_KDE_NET_WM_COLOR_SCHEME=s:{}'.format(
  116. os.path.join(data_dir,
  117. 'qubes-kde', vm.label.name + '.colors'))]
  118. return guid_cmd
  119. @qubes.ext.handler('domain-start', 'domain-cmd-pre-run')
  120. def start_guid(self, vm, event, preparing_dvm=False, start_guid=True,
  121. extra_guid_args=None, **kwargs):
  122. '''Launch gui daemon.
  123. GUI daemon securely displays windows from domain.
  124. ''' # pylint: disable=no-self-use,unused-argument
  125. if not start_guid or preparing_dvm:
  126. return
  127. if self.is_guid_running(vm):
  128. return
  129. if not vm.features.check_with_template('gui', not vm.hvm):
  130. vm.log.debug('Not starting gui daemon, disabled by features')
  131. return
  132. if not os.getenv('DISPLAY'):
  133. vm.log.error('Not starting gui daemon, no DISPLAY set')
  134. return
  135. display = os.getenv('DISPLAY')
  136. if not display.startswith(':'):
  137. vm.log.error('Expected local $DISPLAY, got \'{}\''.format(display))
  138. return
  139. display_num = display[1:].partition('.')[0]
  140. shmid_path = '/var/run/qubes/shm.id.{}'.format(display_num)
  141. if not os.path.exists(shmid_path):
  142. vm.log.error(
  143. 'Not starting gui daemon, no {} file'.format(shmid_path))
  144. return
  145. vm.log.info('Starting gui daemon')
  146. guid_cmd = [qubes.config.system_path['qubes_guid_path'],
  147. '-d', str(vm.xid), '-N', vm.name,
  148. '-c', vm.label.color,
  149. '-i', vm.label.icon_path,
  150. '-l', str(vm.label.index)]
  151. if extra_guid_args is not None:
  152. guid_cmd += extra_guid_args
  153. if vm.debug:
  154. guid_cmd += ['-v', '-v']
  155. # elif not verbose:
  156. else:
  157. guid_cmd += ['-q']
  158. if vm.hvm:
  159. guid_cmd += ['-Q', '-n']
  160. stubdom_guid_pidfile = '/var/run/qubes/guid-running.{}'.format(
  161. self.get_stubdom_xid(vm))
  162. if not vm.debug and os.path.exists(stubdom_guid_pidfile):
  163. # Terminate stubdom guid once "real" gui agent connects
  164. stubdom_guid_pid = \
  165. open(stubdom_guid_pidfile, 'r').read().strip()
  166. guid_cmd += ['-K', stubdom_guid_pid]
  167. guid_cmd += self.kde_guid_args(vm)
  168. @asyncio.coroutine
  169. def coro():
  170. try:
  171. yield from vm.start_daemon(guid_cmd)
  172. except subprocess.CalledProcessError:
  173. raise qubes.exc.QubesVMError(vm,
  174. 'Cannot start qubes-guid for domain {!r}'.format(vm.name))
  175. vm.fire_event('monitor-layout-change')
  176. asyncio.ensure_future(coro())
  177. @staticmethod
  178. def get_stubdom_xid(vm):
  179. if vm.xid < 0:
  180. return -1
  181. if vm.app.vmm.xs is None:
  182. return -1
  183. stubdom_xid_str = vm.app.vmm.xs.read('',
  184. '/local/domain/{}/image/device-model-domid'.format(vm.xid))
  185. if stubdom_xid_str is None or not stubdom_xid_str.isdigit():
  186. return -1
  187. return int(stubdom_xid_str)
  188. @staticmethod
  189. def send_gui_mode(vm):
  190. vm.run_service('qubes.SetGuiMode',
  191. input=('SEAMLESS'
  192. if vm.features.get('gui-seamless', False)
  193. else 'FULLSCREEN'))
  194. @qubes.ext.handler('domain-spawn')
  195. def on_domain_spawn(self, vm, event, start_guid=True, **kwargs):
  196. # pylint: disable=unused-argument
  197. if not start_guid:
  198. return
  199. if not vm.hvm:
  200. return
  201. if not os.getenv('DISPLAY'):
  202. vm.log.error('Not starting gui daemon, no DISPLAY set')
  203. return
  204. guid_cmd = [qubes.config.system_path['qubes_guid_path'],
  205. '-d', str(self.get_stubdom_xid(vm)),
  206. '-t', str(vm.xid),
  207. '-N', vm.name,
  208. '-c', vm.label.color,
  209. '-i', vm.label.icon_path,
  210. '-l', str(vm.label.index),
  211. ]
  212. if vm.debug:
  213. guid_cmd += ['-v', '-v']
  214. else:
  215. guid_cmd += ['-q']
  216. guid_cmd += self.kde_guid_args(vm)
  217. try:
  218. vm.start_daemon(guid_cmd)
  219. except subprocess.CalledProcessError:
  220. raise qubes.exc.QubesVMError(vm, 'Cannot start gui daemon')
  221. @qubes.ext.handler('monitor-layout-change')
  222. def on_monitor_layout_change(self, vm, event, layout=None):
  223. # pylint: disable=no-self-use,unused-argument
  224. if vm.features.check_with_template('no-monitor-layout', False) \
  225. or not vm.is_running():
  226. return
  227. if layout is None:
  228. layout = get_monitor_layout()
  229. if not layout:
  230. return
  231. pipe = vm.run('QUBESRPC qubes.SetMonitorLayout dom0',
  232. passio_popen=True, wait=True)
  233. pipe.stdin.write(''.join(layout).encode())
  234. pipe.stdin.close()
  235. pipe.wait()
  236. @staticmethod
  237. def is_guid_running(vm):
  238. '''Check whether gui daemon for this domain is available.
  239. :returns: :py:obj:`True` if guid is running, \
  240. :py:obj:`False` otherwise.
  241. :rtype: bool
  242. '''
  243. xid = vm.xid
  244. if xid < 0:
  245. return False
  246. if not os.path.exists('/var/run/qubes/guid-running.{}'.format(xid)):
  247. return False
  248. return True
  249. @qubes.ext.handler('domain-is-fully-usable')
  250. def on_domain_is_fully_usable(self, vm, event):
  251. # pylint: disable=unused-argument
  252. if not self.is_guid_running(vm):
  253. yield False