agent.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. #
  13. # This library 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 GNU
  16. # Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this library; if not, see <https://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. import qubespolicy.policycreateconfirmation
  30. # pylint: enable=wrong-import-position
  31. class PolicyAgent(object):
  32. # pylint: disable=too-few-public-methods
  33. dbus = """
  34. <node>
  35. <interface name='org.qubesos.PolicyAgent'>
  36. <method name='Ask'>
  37. <arg type='s' name='source' direction='in'/>
  38. <arg type='s' name='service_name' direction='in'/>
  39. <arg type='as' name='targets' direction='in'/>
  40. <arg type='s' name='default_target' direction='in'/>
  41. <arg type='a{ss}' name='icons' direction='in'/>
  42. <arg type='s' name='response' direction='out'/>
  43. </method>
  44. <method name='ConfirmPolicyCreate'>
  45. <arg type='s' name='source' direction='in'/>
  46. <arg type='s' name='service_name' direction='in'/>
  47. <arg type='b' name='response' direction='out'/>
  48. </method>
  49. </interface>
  50. </node>
  51. """
  52. @staticmethod
  53. def Ask(source, service_name, targets, default_target,
  54. icons):
  55. # pylint: disable=invalid-name
  56. entries_info = {}
  57. for entry in icons:
  58. entries_info[entry] = {}
  59. entries_info[entry]['icon'] = icons.get(entry, None)
  60. response = qubespolicy.rpcconfirmation.confirm_rpc(
  61. entries_info, source, service_name,
  62. targets, default_target or None)
  63. return response or ''
  64. @staticmethod
  65. def ConfirmPolicyCreate(source, service_name):
  66. # pylint: disable=invalid-name
  67. response = qubespolicy.policycreateconfirmation.confirm(
  68. source, service_name)
  69. return response
  70. def main():
  71. loop = GLib.MainLoop()
  72. bus = pydbus.SystemBus()
  73. obj = PolicyAgent()
  74. bus.publish('org.qubesos.PolicyAgent', obj)
  75. loop.run()
  76. if __name__ == '__main__':
  77. main()