wni.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. import md5
  30. from qubes.storage import QubesVmStorage
  31. from qubes.qubes import QubesException,system_path
  32. class QubesWniVmStorage(QubesVmStorage):
  33. """
  34. Class for VM storage of WNI VMs.
  35. """
  36. def __init__(self, *args, **kwargs):
  37. super(QubesWniVmStorage, self).__init__(*args, **kwargs)
  38. # Use the user profile as "private.img"
  39. self.private_img = os.path.join("c:\\Users", self._get_username())
  40. # Pass paths for WNI libvirt driver
  41. os.putenv("WNI_DRIVER_QUBESDB_PATH", system_path['qubesdb_daemon_path'])
  42. os.putenv("WNI_DRIVER_QREXEC_PATH", system_path['qrexec_agent_path'])
  43. def _get_secret(self):
  44. # TODO: some machine-specific secret (accessible only to Administrator)
  45. return ""
  46. def _get_username(self, vmname = None):
  47. if vmname is None:
  48. vmname = self.vm.name
  49. return "qubes-vm-%s" % vmname
  50. def _get_password(self, vmname = None):
  51. if vmname is None:
  52. vmname = self.vm.name
  53. return md5.md5("%s-%s" % (vmname, self._get_secret())).hexdigest()
  54. def get_config_params(self):
  55. return {}
  56. def create_on_disk_private_img(self, verbose, source_template = None):
  57. win32api.ShellExecute(None, "runas",
  58. "net", "user %s %s /ADD"
  59. % (self._get_username(), self._get_password()),
  60. None, 0)
  61. def create_on_disk_root_img(self, verbose, source_template = None):
  62. pass
  63. def remove_from_disk(self):
  64. win32api.ShellExecute(None, "runas",
  65. "net", "user %s /DELETE" % (self._get_username()),
  66. None, 0)
  67. super(QubesWniVmStorage, self).remove_from_disk()
  68. def rename(self, old_name, new_name):
  69. super(QubesWniVmStorage, self).rename(old_name, new_name)
  70. win32api.ShellExecute(None, "runas",
  71. "wmic", "useraccount where name='%s' rename '%s'"
  72. % (self._get_username(old_name), self._get_username(new_name)),
  73. None, 0)
  74. # TODO: update password
  75. def verify_files(self):
  76. if not os.path.exists (self.vmdir):
  77. raise QubesException (
  78. "VM directory doesn't exist: {0}".\
  79. format(self.vmdir))
  80. try:
  81. # TemplateVm in WNI is quite virtual, so do not require the user
  82. if not self.vm.is_template():
  83. win32net.NetUserGetInfo(None, self._get_username(), 0)
  84. except pywintypes.error, details:
  85. if details[0] == 2221:
  86. # "The user name cannot be found."
  87. raise QubesException("User %s doesn't exist" % self._get_username())
  88. else:
  89. raise
  90. def reset_volatile_storage(self, verbose = False, source_template = None):
  91. pass
  92. def prepare_for_vm_startup(self, verbose = False):
  93. if self.vm.is_template():
  94. raise QubesException("Starting TemplateVM is not supported")