services.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- encoding: utf-8 -*-
  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. '''Extension responsible for qvm-service framework'''
  21. import qubes.ext
  22. class ServicesExtension(qubes.ext.Extension):
  23. '''This extension export features with 'service.' prefix to QubesDB in
  24. /qubes-service/ tree.
  25. '''
  26. # pylint: disable=no-self-use
  27. @qubes.ext.handler('domain-qdb-create')
  28. def on_domain_qdb_create(self, vm, event):
  29. '''Actually export features'''
  30. # pylint: disable=unused-argument
  31. for feature, value in vm.features.items():
  32. if not feature.startswith('service.'):
  33. continue
  34. service = feature[len('service.'):]
  35. # forcefully convert to '0' or '1'
  36. vm.untrusted_qdb.write('/qubes-service/{}'.format(service),
  37. str(int(bool(value))))
  38. @qubes.ext.handler('domain-feature-set:*')
  39. def on_domain_feature_set(self, vm, event, feature, value, oldvalue=None):
  40. '''Update /qubes-service/ QubesDB tree in runtime'''
  41. # pylint: disable=unused-argument
  42. if not vm.is_running():
  43. return
  44. if not feature.startswith('service.'):
  45. return
  46. service = feature[len('service.'):]
  47. # forcefully convert to '0' or '1'
  48. vm.untrusted_qdb.write('/qubes-service/{}'.format(service),
  49. str(int(bool(value))))
  50. @qubes.ext.handler('domain-feature-delete:*')
  51. def on_domain_feature_delete(self, vm, event, feature):
  52. '''Update /qubes-service/ QubesDB tree in runtime'''
  53. # pylint: disable=unused-argument
  54. if not vm.is_running():
  55. return
  56. if not feature.startswith('service.'):
  57. return
  58. service = feature[len('service.'):]
  59. vm.untrusted_qdb.rm('/qubes-service/{}'.format(service))
  60. @qubes.ext.handler('features-request')
  61. def supported_services(self, vm, event, untrusted_features):
  62. '''Handle advertisement of supported services'''
  63. # pylint: disable=no-self-use,unused-argument
  64. if getattr(vm, 'template', None):
  65. vm.log.warning(
  66. 'Ignoring qubes.FeaturesRequest from template-based VM')
  67. return
  68. new_supported_services = set()
  69. for requested_service in untrusted_features:
  70. if not requested_service.startswith('supported-service.'):
  71. continue
  72. if untrusted_features[requested_service] == '1':
  73. # only allow to advertise service as supported, lack of entry
  74. # means service is not supported
  75. new_supported_services.add(requested_service)
  76. del untrusted_features
  77. # if no service is supported, ignore the whole thing - do not clear
  78. # all services in case of empty request (manual or such)
  79. if not new_supported_services:
  80. return
  81. old_supported_services = set(
  82. feat for feat in vm.features
  83. if feat.startswith('supported-service.') and vm.features[feat])
  84. for feature in new_supported_services.difference(
  85. old_supported_services):
  86. vm.features[feature] = True
  87. for feature in old_supported_services.difference(
  88. new_supported_services):
  89. del vm.features[feature]