__init__.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # -*- encoding: utf8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. '''Event handling implementation, require Python >=3.5.2 for asyncio.'''
  21. import asyncio
  22. import subprocess
  23. import qubesadmin.config
  24. import qubesadmin.exc
  25. class EventsDispatcher(object):
  26. ''' Events dispatcher, responsible for receiving events and calling
  27. appropriate handlers'''
  28. def __init__(self, app):
  29. '''Initialize EventsDispatcher'''
  30. #: Qubes() object
  31. self.app = app
  32. #: event handlers - dict of event -> handlers
  33. self.handlers = {}
  34. def add_handler(self, event, handler):
  35. '''Register handler for event
  36. Use '*' as event to register a handler for all events.
  37. Handler function is called with:
  38. * subject (VM object or None)
  39. * event name (str)
  40. * keyword arguments related to the event, if any - all values as str
  41. :param event Event name, or '*' for all events
  42. :param handler Handler function'''
  43. self.handlers.setdefault(event, set()).add(handler)
  44. def remove_handler(self, event, handler):
  45. '''Remove previously registered event handler
  46. :param event Event name
  47. :param handler Handler function
  48. '''
  49. self.handlers[event].remove(handler)
  50. @asyncio.coroutine
  51. def _get_events_reader(self, vm=None) -> (asyncio.StreamReader, callable):
  52. '''Make connection to qubesd and return stream to read events from
  53. :param vm: Specific VM for which events should be handled, use None
  54. to handle events from all VMs (and non-VM objects)
  55. :return stream to read events from and a cleanup function
  56. (call it to terminate qubesd connection)'''
  57. if vm is not None:
  58. dest = vm.name
  59. else:
  60. dest = 'dom0'
  61. if self.app.qubesd_connection_type == 'socket':
  62. reader, writer = yield from asyncio.open_unix_connection(
  63. qubesadmin.config.QUBESD_SOCKET)
  64. writer.write(b'dom0\0') # source
  65. writer.write(b'admin.Events\0') # method
  66. writer.write(dest.encode('ascii') + b'\0') # dest
  67. writer.write(b'\0') # arg
  68. writer.write_eof()
  69. def cleanup_func():
  70. '''Close connection to qubesd'''
  71. writer.close()
  72. elif self.app.qubesd_connection_type == 'qrexec':
  73. proc = yield from asyncio.create_subprocess_exec(
  74. 'qrexec-client-vm', dest, 'admin.Events',
  75. stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  76. proc.stdin.write_eof()
  77. reader = proc.stdout
  78. def cleanup_func():
  79. '''Close connection to qubesd'''
  80. try:
  81. proc.kill()
  82. except ProcessLookupError:
  83. pass
  84. else:
  85. raise NotImplementedError('Unsupported qubesd connection type: '
  86. + self.app.qubesd_connection_type)
  87. return reader, cleanup_func
  88. @asyncio.coroutine
  89. def listen_for_events(self, vm=None, reconnect=True):
  90. '''
  91. Listen for events and call appropriate handlers.
  92. This function do not exit until manually terminated.
  93. This is coroutine.
  94. :param vm: Listen for events only for this VM, use None to listen for
  95. events about all VMs and not related to any particular VM.
  96. :param reconnect: should reconnect to qubesd if connection is
  97. interrupted?
  98. :rtype: None
  99. '''
  100. while True:
  101. try:
  102. yield from self._listen_for_events(vm)
  103. except (ConnectionRefusedError, ConnectionResetError,
  104. FileNotFoundError):
  105. pass
  106. if not reconnect:
  107. break
  108. self.app.log.warning(
  109. 'Connection to qubesd terminated, reconnecting in {} '
  110. 'seconds'.format(qubesadmin.config.QUBESD_RECONNECT_DELAY))
  111. # avoid busy-loop if qubesd is dead
  112. yield from asyncio.sleep(qubesadmin.config.QUBESD_RECONNECT_DELAY)
  113. @asyncio.coroutine
  114. def _listen_for_events(self, vm=None):
  115. '''
  116. Listen for events and call appropriate handlers.
  117. This function do not exit until manually terminated.
  118. This is coroutine.
  119. :param vm: Listen for events only for this VM, use None to listen for
  120. events about all VMs and not related to any particular VM.
  121. :return: True if any event was received, otherwise False
  122. :rtype: bool
  123. '''
  124. reader, cleanup_func = yield from self._get_events_reader(vm)
  125. try:
  126. some_event_received = False
  127. while not reader.at_eof():
  128. try:
  129. event_header = yield from reader.readuntil(b'\0')
  130. if event_header != b'1\0':
  131. raise qubesadmin.exc.QubesDaemonCommunicationError(
  132. 'Non-event received on events connection: '
  133. + repr(event_header))
  134. subject = (yield from reader.readuntil(b'\0'))[:-1].decode(
  135. 'utf-8')
  136. event = (yield from reader.readuntil(b'\0'))[:-1].decode(
  137. 'utf-8')
  138. kwargs = {}
  139. while True:
  140. key = (yield from reader.readuntil(b'\0'))[:-1].decode(
  141. 'utf-8')
  142. if not key:
  143. break
  144. value = (yield from reader.readuntil(b'\0'))[:-1].\
  145. decode('utf-8')
  146. kwargs[key] = value
  147. except asyncio.IncompleteReadError as err:
  148. if err.partial == b'':
  149. break
  150. else:
  151. raise
  152. if not subject:
  153. subject = None
  154. self.handle(subject, event, **kwargs)
  155. some_event_received = True
  156. finally:
  157. cleanup_func()
  158. return some_event_received
  159. def handle(self, subject, event, **kwargs):
  160. '''Call handlers for given event'''
  161. if subject:
  162. if event in ['property-set:name']:
  163. self.app.domains.clear_cache()
  164. subject = self.app.domains[subject]
  165. else:
  166. # handle cache refreshing on best-effort basis
  167. if event in ['domain-add', 'domain-delete']:
  168. self.app.domains.clear_cache()
  169. subject = None
  170. for handler in self.handlers.get(event, []):
  171. handler(subject, event, **kwargs)
  172. for handler in self.handlers.get('*', []):
  173. handler(subject, event, **kwargs)