wni.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/python2
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2013 Marek Marczykowski <marmarek@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; either version 2
  10. # of the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. #
  21. #
  22. from __future__ import absolute_import
  23. import sys
  24. import os
  25. import os.path
  26. import win32api
  27. import win32net
  28. import pywintypes
  29. from qubes.storage import QubesVmStorage
  30. from qubes.qubes import QubesException,system_path
  31. class QubesWniVmStorage(QubesVmStorage):
  32. """
  33. Class for VM storage of WNI VMs.
  34. """
  35. def __init__(self, *args, **kwargs):
  36. super(QubesWniVmStorage, self).__init__(*args, **kwargs)
  37. # Use the user profile as "private.img"
  38. self.private_img = os.path.join("c:\\Users", self._get_username())
  39. # Pass paths for WNI libvirt driver
  40. os.putenv("WNI_DRIVER_QUBESDB_PATH", system_path['qubesdb_daemon_path'])
  41. os.putenv("WNI_DRIVER_QREXEC_PATH", system_path['qrexec_agent_path'])
  42. def _get_username(self, vmname = None):
  43. if vmname is None:
  44. vmname = self.vm.name
  45. return "qubes-vm-%s" % vmname
  46. def get_config_params(self):
  47. return {}
  48. def create_on_disk_private_img(self, verbose, source_template = None):
  49. win32api.ShellExecute(None, "runas",
  50. "net", "user %s %s /ADD"
  51. % (self._get_username(), "testpass"),
  52. None, 0)
  53. def create_on_disk_root_img(self, verbose, source_template = None):
  54. pass
  55. def remove_from_disk(self):
  56. win32api.ShellExecute(None, "runas",
  57. "net", "user %s /DELETE" % (self._get_username()),
  58. None, 0)
  59. super(QubesWniVmStorage, self).remove_from_disk()
  60. def rename(self, old_name, new_name):
  61. super(QubesWniVmStorage, self).rename(old_name, new_name)
  62. win32api.ShellExecute(None, "runas",
  63. "wmic", "useraccount where name='%s' rename '%s'"
  64. % (self._get_username(old_name), self._get_username(new_name)),
  65. None, 0)
  66. def verify_files(self):
  67. if not os.path.exists (self.vmdir):
  68. raise QubesException (
  69. "VM directory doesn't exist: {0}".\
  70. format(self.vmdir))
  71. try:
  72. # TemplateVm in WNI is quite virtual, so do not require the user
  73. if not self.vm.is_template():
  74. win32net.NetUserGetInfo(None, self._get_username(), 0)
  75. except pywintypes.error, details:
  76. if details[0] == 2221:
  77. # "The user name cannot be found."
  78. raise QubesException("User %s doesn't exist" % self._get_username())
  79. else:
  80. raise
  81. def reset_volatile_storage(self, verbose = False, source_template = None):
  82. pass
  83. def prepare_for_vm_startup(self):
  84. if self.vm.is_template():
  85. raise QubesException("Starting TemplateVM is not supported")