domain.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2016 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program; if not, write to the Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. #
  20. ''' Manages block devices in a domain '''
  21. import itertools
  22. import string # pylint: disable=deprecated-module
  23. from qubes.storage import Pool, Volume
  24. class DomainPool(Pool):
  25. ''' This pool manages all the block devices of a domain.
  26. The devices are queried through :py:module:`qubesdb`
  27. '''
  28. driver = 'domain'
  29. def __init__(self, vm):
  30. self.vm = vm
  31. super(DomainPool, self).__init__(name='p_' + vm.name)
  32. @property
  33. def volumes(self):
  34. ''' Queries qubesdb and returns volumes for `self.vm` '''
  35. qdb = self.vm.qdb
  36. safe_set = set(itertools.chain(
  37. string.ascii_letters, string.digits, string.punctuation))
  38. allowed_attributes = {'desc': string.printable,
  39. 'mode': string.ascii_letters,
  40. 'size': string.digits}
  41. if not self.vm.is_running():
  42. return []
  43. untrusted_qubes_devices = qdb.list('/qubes-block-devices/')
  44. # because we get each path 3 x times as
  45. # /qubes-block-devices/foo/{desc,mode,size} we need to merge this
  46. devices = {}
  47. for untrusted_device_path in untrusted_qubes_devices:
  48. if not all(chr(c) in safe_set for c in untrusted_device_path):
  49. msg = ("%s vm's device path name contains unsafe characters. "
  50. "Skipping it.")
  51. self.vm.log.warning(msg % self.vm.name)
  52. continue
  53. # name can be trusted because it was checked as a part of
  54. # untrusted_device_path check above
  55. _, _, name, untrusted_atr = untrusted_device_path.\
  56. decode('ascii').split('/', 4)
  57. if untrusted_atr in allowed_attributes.keys():
  58. atr = untrusted_atr
  59. else:
  60. msg = ('{!s} has an unknown qubes-block-device atr {!s} '
  61. 'Skipping it')
  62. self.vm.log.error(msg.format(self.vm.name, untrusted_atr))
  63. continue
  64. untrusted_value = qdb.read(untrusted_device_path)
  65. allowed_characters = allowed_attributes[atr]
  66. if all(chr(c) in allowed_characters for c in untrusted_value):
  67. value = untrusted_value.decode('ascii')
  68. else:
  69. msg = ("{!s} vm's device path {!s} contains unsafe characters")
  70. self.vm.log.error(msg.format(self.vm.name, atr))
  71. continue
  72. if name not in devices.keys():
  73. devices[name] = {}
  74. devices[name][atr] = value
  75. return [DomainVolume(self.vm, _name, self.name, **atrs)
  76. for _name, atrs in devices.items()]
  77. def clone(self, source, target):
  78. raise NotImplementedError
  79. def __xml__(self):
  80. return None
  81. class DomainVolume(Volume):
  82. ''' A volume provided by a block device in an domain '''
  83. def __init__(self, vm, name, pool, desc, mode, **kwargs):
  84. rw = (mode == 'w')
  85. super(DomainVolume, self).__init__(desc, pool, vid=name, removable=True,
  86. rw=rw, **kwargs)
  87. self.domain = vm
  88. @property
  89. def revisions(self):
  90. return {}