misc.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. ''' Interface for methods not being part of Admin API, but still handled by
  21. qubesd. '''
  22. import asyncio
  23. import string
  24. import qubes.api
  25. import qubes.api.admin
  26. import qubes.vm.dispvm
  27. class QubesMiscAPI(qubes.api.AbstractQubesAPI):
  28. @qubes.api.method('qubes.FeaturesRequest', no_payload=True)
  29. @asyncio.coroutine
  30. def qubes_features_request(self):
  31. ''' qubes.FeaturesRequest handler
  32. VM (mostly templates) can request some features from dom0 for itself.
  33. Then dom0 (qubesd extension) may respect this request or ignore it.
  34. Technically, VM first write requested features into QubesDB in
  35. `/features-request/` subtree, then call this method. The method will
  36. dispatch 'features-request' event, which may be handled by
  37. appropriate extensions. Requests not explicitly handled by some
  38. extension are ignored.
  39. '''
  40. assert self.dest.name == 'dom0'
  41. assert not self.arg
  42. prefix = '/features-request/'
  43. keys = [key.decode('ascii', errors='strict')
  44. for key in self.src.qdb.list(prefix)]
  45. untrusted_features = {key[len(prefix):]:
  46. self.src.qdb.read(key).decode('ascii', errors='strict')
  47. for key in keys}
  48. safe_set = string.ascii_letters + string.digits
  49. for untrusted_key in untrusted_features:
  50. untrusted_value = untrusted_features[untrusted_key]
  51. assert all((c in safe_set) for c in untrusted_value)
  52. self.src.fire_event('features-request',
  53. untrusted_features=untrusted_features)
  54. self.app.save()
  55. @qubes.api.method('qubes.NotifyTools', no_payload=True)
  56. @asyncio.coroutine
  57. def qubes_notify_tools(self):
  58. '''
  59. Legacy version of qubes.FeaturesRequest, used by Qubes Windows Tools
  60. '''
  61. assert self.dest.name == 'dom0'
  62. assert not self.arg
  63. untrusted_features = {}
  64. safe_set = string.ascii_letters + string.digits
  65. expected_features = ('qrexec', 'gui', 'default-user')
  66. for feature in expected_features:
  67. untrusted_value = self.src.qdb.read('/qubes-tools/' + feature)
  68. if untrusted_value:
  69. untrusted_value = untrusted_value.decode('ascii',
  70. errors='strict')
  71. assert all((c in safe_set) for c in untrusted_value)
  72. untrusted_features[feature] = untrusted_value
  73. del untrusted_value
  74. self.src.fire_event('features-request',
  75. untrusted_features=untrusted_features)
  76. self.app.save()
  77. @qubes.api.method('qubes.NotifyUpdates')
  78. @asyncio.coroutine
  79. def qubes_notify_updates(self, untrusted_payload):
  80. '''
  81. Receive VM notification about updates availability
  82. Payload contains a single integer - either 0 (no updates) or some
  83. positive value (some updates).
  84. '''
  85. untrusted_update_count = untrusted_payload.strip()
  86. assert untrusted_update_count.isdigit()
  87. # now sanitized
  88. update_count = int(untrusted_update_count)
  89. del untrusted_update_count
  90. if self.src.updateable:
  91. # Just trust information from VM itself
  92. self.src.features['updates-available'] = bool(update_count)
  93. self.app.save()
  94. elif getattr(self.src, 'template', None) is not None:
  95. # Hint about updates availability in template
  96. # If template is running - it will notify about updates itself
  97. if self.src.template.is_running():
  98. return
  99. # Ignore no-updates info
  100. if update_count > 0:
  101. # If VM is outdated, updates were probably already installed
  102. # in the template - ignore info
  103. if self.src.storage.outdated_volumes:
  104. return
  105. self.src.template.features['updates-available'] = bool(
  106. update_count)
  107. self.app.save()