wni.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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
  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. def _get_username(self, vmname = None):
  40. if vmname is None:
  41. vmname = self.vm.name
  42. return "qubes-vm-%s" % vmname
  43. def get_config_params(self):
  44. return {}
  45. def create_on_disk_private_img(self, verbose, source_template = None):
  46. win32api.ShellExecute(None, "runas",
  47. "net", "user %s %s /ADD"
  48. % (self._get_username(), "testpass"),
  49. None, 0)
  50. def create_on_disk_root_img(self, verbose, source_template = None):
  51. pass
  52. def remove_from_disk(self):
  53. win32api.ShellExecute(None, "runas",
  54. "net", "user %s /DELETE" % (self._get_username()),
  55. None, 0)
  56. super(QubesWniVmStorage, self).remove_from_disk()
  57. def rename(self, old_name, new_name):
  58. super(QubesWniVmStorage, self).rename(old_name, new_name)
  59. win32api.ShellExecute(None, "runas",
  60. "wmic", "useraccount where name='%s' rename '%s'"
  61. % (self._get_username(old_name), self._get_username(new_name)),
  62. None, 0)
  63. def verify_files(self):
  64. if not os.path.exists (self.vmdir):
  65. raise QubesException (
  66. "VM directory doesn't exist: {0}".\
  67. format(self.vmdir))
  68. try:
  69. # TemplateVm in WNI is quite virtual, so do not require the user
  70. if not self.vm.is_template():
  71. win32net.NetUserGetInfo(None, self._get_username(), 0)
  72. except pywintypes.error, details:
  73. if details[0] == 2221:
  74. # "The user name cannot be found."
  75. raise QubesException("User %s doesn't exist" % self._get_username())
  76. else:
  77. raise
  78. def reset_volatile_storage(self, verbose = False, source_template = None):
  79. pass
  80. def prepare_for_vm_startup(self):
  81. if self.vm.is_template():
  82. raise QubesException("Starting TemplateVM is not supported")