misc.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. SOCKNAME = '/var/run/qubesd.misc.sock'
  29. @qubes.api.method('qubes.FeaturesRequest', no_payload=True)
  30. @asyncio.coroutine
  31. def qubes_features_request(self):
  32. ''' qubes.FeaturesRequest handler
  33. VM (mostly templates) can request some features from dom0 for itself.
  34. Then dom0 (qubesd extension) may respect this request or ignore it.
  35. Technically, VM first write requested features into QubesDB in
  36. `/features-request/` subtree, then call this method. The method will
  37. dispatch 'features-request' event, which may be handled by
  38. appropriate extensions. Requests not explicitly handled by some
  39. extension are ignored.
  40. '''
  41. assert self.dest.name == 'dom0'
  42. assert not self.arg
  43. prefix = '/features-request/'
  44. keys = self.src.untrusted_qdb.list(prefix)
  45. untrusted_features = {key[len(prefix):]:
  46. self.src.untrusted_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.untrusted_qdb.read(
  68. '/qubes-tools/' + feature)
  69. if untrusted_value:
  70. untrusted_value = untrusted_value.decode('ascii',
  71. errors='strict')
  72. assert all((c in safe_set) for c in untrusted_value)
  73. untrusted_features[feature] = untrusted_value
  74. del untrusted_value
  75. self.src.fire_event('features-request',
  76. untrusted_features=untrusted_features)
  77. self.app.save()
  78. @qubes.api.method('qubes.NotifyUpdates')
  79. @asyncio.coroutine
  80. def qubes_notify_updates(self, untrusted_payload):
  81. '''
  82. Receive VM notification about updates availability
  83. Payload contains a single integer - either 0 (no updates) or some
  84. positive value (some updates).
  85. '''
  86. untrusted_update_count = untrusted_payload.strip()
  87. assert untrusted_update_count.isdigit()
  88. # now sanitized
  89. update_count = int(untrusted_update_count)
  90. del untrusted_update_count
  91. if self.src.updateable:
  92. # Just trust information from VM itself
  93. self.src.features['updates-available'] = bool(update_count)
  94. self.app.save()
  95. elif getattr(self.src, 'template', None) is not None:
  96. # Hint about updates availability in template
  97. # If template is running - it will notify about updates itself
  98. if self.src.template.is_running():
  99. return
  100. # Ignore no-updates info
  101. if update_count > 0:
  102. # If VM is outdated, updates were probably already installed
  103. # in the template - ignore info
  104. if self.src.storage.outdated_volumes:
  105. return
  106. self.src.template.features['updates-available'] = bool(
  107. update_count)
  108. self.app.save()