rpcconfirmation.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #!/usr/bin/python
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2017 boring-stuff <boring-stuff@users.noreply.github.com>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. import pkg_resources
  22. from gi.repository import Gtk, Gdk, GLib
  23. import os
  24. from qubespolicy.gtkhelpers import VMListModeler, FocusStealingHelper
  25. from qubespolicy.utils import sanitize_domain_name, \
  26. sanitize_service_name
  27. class RPCConfirmationWindow:
  28. _source_file = pkg_resources.resource_filename('qubespolicy',
  29. os.path.join('glade', "RPCConfirmationWindow.glade"))
  30. _source_id = {'window': "RPCConfirmationWindow",
  31. 'ok': "okButton",
  32. 'cancel': "cancelButton",
  33. 'source': "sourceEntry",
  34. 'rpc_label': "rpcLabel",
  35. 'target': "TargetCombo",
  36. 'error_bar': "ErrorBar",
  37. 'error_message': "ErrorMessage",
  38. }
  39. def _clicked_ok(self, source):
  40. assert source is not None, \
  41. 'Called the clicked ok callback from no source object'
  42. if self._can_perform_action():
  43. self._confirmed = True
  44. self._close()
  45. def _clicked_cancel(self, button):
  46. assert button == self._rpc_cancel_button, \
  47. 'Called the clicked cancel callback through the wrong button'
  48. if self._can_perform_action():
  49. self._confirmed = False
  50. self._close()
  51. def _key_pressed(self, window, key):
  52. assert window == self._rpc_window, \
  53. 'Key pressed callback called with wrong window'
  54. if self._can_perform_action():
  55. if key.keyval == Gdk.KEY_Escape:
  56. self._confirmed = False
  57. self._close()
  58. def _update_ok_button_sensitivity(self, data):
  59. valid = (data is not None)
  60. if valid:
  61. (self._target_qid, self._target_name) = data
  62. else:
  63. self._target_qid = None
  64. self._target_name = None
  65. self._focus_helper.request_sensitivity(valid)
  66. def _show_error(self, error_message):
  67. self._error_message.set_text(error_message)
  68. self._error_bar.set_visible(True)
  69. def _close_error(self, error_bar, response):
  70. assert error_bar == self._error_bar, \
  71. 'Closed the error bar with the wrong error bar as parameter'
  72. assert response is not None, \
  73. 'Closed the error bar with None as a response'
  74. self._error_bar.set_visible(False)
  75. def _set_initial_target(self, source, target):
  76. if target is not None:
  77. if target == source:
  78. self._show_error(
  79. "Source and target domains must not be the same.")
  80. else:
  81. model = self._rpc_combo_box.get_model()
  82. found = False
  83. for item in model:
  84. if item[1] == target:
  85. found = True
  86. self._rpc_combo_box.set_active_iter(
  87. model.get_iter(item.path))
  88. break
  89. if not found:
  90. self._show_error("Domain '%s' doesn't exist." % target)
  91. def _can_perform_action(self):
  92. return self._focus_helper.can_perform_action()
  93. def _escape_and_format_rpc_text(self, rpc_operation):
  94. escaped = GLib.markup_escape_text(rpc_operation)
  95. partitioned = escaped.partition('.')
  96. formatted = partitioned[0] + partitioned[1]
  97. if len(partitioned[2]) > 0:
  98. formatted += "<b>" + partitioned[2] + "</b>"
  99. else:
  100. formatted = "<b>" + formatted + "</b>"
  101. return formatted
  102. def _connect_events(self):
  103. self._rpc_window.connect("key-press-event", self._key_pressed)
  104. self._rpc_ok_button.connect("clicked", self._clicked_ok)
  105. self._rpc_cancel_button.connect("clicked", self._clicked_cancel)
  106. self._error_bar.connect("response", self._close_error)
  107. def __init__(self, source, rpc_operation, name_whitelist, target=None):
  108. sanitize_domain_name(source, assert_sanitized=True)
  109. sanitize_service_name(source, assert_sanitized=True)
  110. self._gtk_builder = Gtk.Builder()
  111. self._gtk_builder.add_from_file(self._source_file)
  112. self._rpc_window = self._gtk_builder.get_object(
  113. self._source_id['window'])
  114. self._rpc_ok_button = self._gtk_builder.get_object(
  115. self._source_id['ok'])
  116. self._rpc_cancel_button = self._gtk_builder.get_object(
  117. self._source_id['cancel'])
  118. self._rpc_label = self._gtk_builder.get_object(
  119. self._source_id['rpc_label'])
  120. self._source_entry = self._gtk_builder.get_object(
  121. self._source_id['source'])
  122. self._rpc_combo_box = self._gtk_builder.get_object(
  123. self._source_id['target'])
  124. self._error_bar = self._gtk_builder.get_object(
  125. self._source_id['error_bar'])
  126. self._error_message = self._gtk_builder.get_object(
  127. self._source_id['error_message'])
  128. self._target_qid = None
  129. self._target_name = None
  130. self._focus_helper = self._new_focus_stealing_helper()
  131. self._rpc_label.set_markup(
  132. self._escape_and_format_rpc_text(rpc_operation))
  133. list_modeler = self._new_VM_list_modeler()
  134. domain_filters = [VMListModeler.NameWhitelistFilter(name_whitelist)]
  135. list_modeler.apply_model(self._rpc_combo_box, domain_filters,
  136. selection_trigger=self._update_ok_button_sensitivity,
  137. activation_trigger=self._clicked_ok)
  138. self._source_entry.set_text(source)
  139. list_modeler.apply_icon(self._source_entry, source)
  140. self._confirmed = None
  141. self._set_initial_target(source, target)
  142. self._connect_events()
  143. def _close(self):
  144. self._rpc_window.close()
  145. def _show(self):
  146. self._rpc_window.set_keep_above(True)
  147. self._rpc_window.connect("delete-event", Gtk.main_quit)
  148. self._rpc_window.show_all()
  149. Gtk.main()
  150. def _new_VM_list_modeler(self):
  151. return VMListModeler()
  152. def _new_focus_stealing_helper(self):
  153. return FocusStealingHelper(
  154. self._rpc_window,
  155. self._rpc_ok_button,
  156. 1)
  157. def confirm_rpc(self):
  158. self._show()
  159. if self._confirmed:
  160. return {'name': self._target_name, 'qid': self._target_qid,
  161. 'parameters': {}}
  162. else:
  163. return False
  164. def confirm_rpc(source, rpc_operation, name_whitelist, target=None):
  165. window = RPCConfirmationWindow(source, rpc_operation, name_whitelist,
  166. target)
  167. return window.confirm_rpc()