misc.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. untrusted_features = {key[len(prefix):]:
  44. self.src.qdb.read(key).decode('ascii', errors='strict')
  45. for key in self.src.qdb.list(prefix)}
  46. safe_set = string.ascii_letters + string.digits
  47. for untrusted_key in untrusted_features:
  48. untrusted_value = untrusted_features[untrusted_key]
  49. assert all((c in safe_set) for c in untrusted_value)
  50. self.src.fire_event('features-request',
  51. untrusted_features=untrusted_features)
  52. self.app.save()
  53. @qubes.api.method('qubes.NotifyTools', no_payload=True)
  54. @asyncio.coroutine
  55. def qubes_notify_tools(self):
  56. '''
  57. Legacy version of qubes.FeaturesRequest, used by Qubes Windows Tools
  58. '''
  59. assert self.dest.name == 'dom0'
  60. assert not self.arg
  61. untrusted_features = {}
  62. safe_set = string.ascii_letters + string.digits
  63. expected_features = ('version', 'qrexec', 'gui', 'default-user')
  64. for feature in expected_features:
  65. untrusted_value = self.src.qdb.read('/qubes-tools/' + feature)
  66. if untrusted_value:
  67. untrusted_value = untrusted_value.decode('ascii',
  68. errors='strict')
  69. assert all((c in safe_set) for c in untrusted_value)
  70. untrusted_features[feature] = untrusted_value
  71. del untrusted_value
  72. self.src.fire_event('features-request',
  73. untrusted_features=untrusted_features)
  74. self.app.save()
  75. @qubes.api.method('qubes.NotifyUpdates')
  76. @asyncio.coroutine
  77. def qubes_notify_updates(self, untrusted_payload):
  78. '''
  79. Receive VM notification about updates availability
  80. Payload contains a single integer - either 0 (no updates) or some
  81. positive value (some updates).
  82. '''
  83. untrusted_update_count = untrusted_payload.strip()
  84. assert untrusted_update_count.isdigit()
  85. # now sanitized
  86. update_count = int(untrusted_update_count)
  87. del untrusted_update_count
  88. if self.src.updateable:
  89. # Just trust information from VM itself
  90. self.src.updates_available = bool(update_count)
  91. self.app.save()
  92. elif getattr(self.src, 'template', None) is not None:
  93. # Hint about updates availability in template
  94. # If template is running - it will notify about updates itself
  95. if self.src.template.is_running():
  96. return
  97. # Ignore no-updates info
  98. if update_count > 0:
  99. # If VM is outdated, updates were probably already installed
  100. # in the template - ignore info
  101. if self.src.storage.outdated_volumes:
  102. return
  103. self.src.template.updates_available = bool(update_count)
  104. self.app.save()