agent.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 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 General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. ''' Agent running in user session, responsible for asking the user about policy
  21. decisions.'''
  22. import pydbus
  23. # pylint: disable=import-error,wrong-import-position
  24. import gi
  25. gi.require_version('Gtk', '3.0')
  26. from gi.repository import GLib
  27. # pylint: enable=import-error
  28. import qubespolicy.rpcconfirmation
  29. # pylint: enable=wrong-import-position
  30. class PolicyAgent(object):
  31. # pylint: disable=too-few-public-methods
  32. dbus = """
  33. <node>
  34. <interface name='org.qubesos.PolicyAgent'>
  35. <method name='Ask'>
  36. <arg type='s' name='source' direction='in'/>
  37. <arg type='s' name='service_name' direction='in'/>
  38. <arg type='as' name='targets' direction='in'/>
  39. <arg type='s' name='default_target' direction='in'/>
  40. <arg type='a{ss}' name='icons' direction='in'/>
  41. <arg type='s' name='response' direction='out'/>
  42. </method>
  43. </interface>
  44. </node>
  45. """
  46. @staticmethod
  47. def Ask(source, service_name, targets, default_target,
  48. icons):
  49. # pylint: disable=invalid-name
  50. entries_info = {}
  51. for target in targets:
  52. entries_info[target] = {}
  53. entries_info[target]['icon'] = icons.get(target, None)
  54. response = qubespolicy.rpcconfirmation.confirm_rpc(
  55. entries_info, source, service_name,
  56. targets, default_target or None)
  57. return response or ''
  58. def main():
  59. loop = GLib.MainLoop()
  60. bus = pydbus.SystemBus()
  61. obj = PolicyAgent()
  62. bus.publish('org.qubesos.PolicyAgent', obj)
  63. loop.run()
  64. if __name__ == '__main__':
  65. main()