domain.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python2
  2. # -*- encoding: utf8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2016 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
  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, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. ''' Manages block devices in a domain '''
  23. import string # pylint: disable=deprecated-module
  24. from qubes.storage import Pool, Volume
  25. class DomainPool(Pool):
  26. ''' This pool manages all the block devices of a domain.
  27. The devices are queried through :py:module:`qubesdb`
  28. '''
  29. driver = 'domain'
  30. def __init__(self, vm):
  31. self.vm = vm
  32. super(DomainPool, self).__init__(name='p_' + vm.name)
  33. @property
  34. def volumes(self):
  35. ''' Queries qubesdb and returns volumes for `self.vm` '''
  36. qdb = self.vm.qdb
  37. safe_set = set(string.letters + string.digits + string.punctuation)
  38. allowed_attributes = {'desc': string.printable,
  39. 'mode': string.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(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.split('/', 4)
  56. if untrusted_atr in allowed_attributes.keys():
  57. atr = untrusted_atr
  58. else:
  59. msg = ('{!s} has an unknown qubes-block-device atr {!s} '
  60. 'Skipping it')
  61. self.vm.log.error(msg.format(self.vm.name, untrusted_atr))
  62. continue
  63. untrusted_value = qdb.read(untrusted_device_path)
  64. allowed_characters = allowed_attributes[atr]
  65. if all(c in allowed_characters for c in untrusted_value):
  66. value = untrusted_value
  67. else:
  68. msg = ("{!s} vm's device path {!s} contains unsafe characters")
  69. self.vm.log.error(msg.format(self.vm.name, atr))
  70. continue
  71. if name not in devices.keys():
  72. devices[name] = {}
  73. devices[name][atr] = value
  74. return [DomainVolume(n, self.vm, self.name, **atrs)
  75. for n, atrs in devices.items()]
  76. def clone(self, source, target):
  77. raise NotImplementedError
  78. def __xml__(self):
  79. return None
  80. class DomainVolume(Volume):
  81. ''' A volume provided by a block device in an domain '''
  82. def __init__(self, vm, name, pool, desc, mode, **kwargs):
  83. rw = (mode == 'w')
  84. super(DomainVolume, self).__init__(desc, pool, vid=name, removable=True,
  85. rw=rw, **kwargs)
  86. self.domain = vm
  87. @property
  88. def revisions(self):
  89. return {}