modules.machinery.esx

Package modules :: Package machinery :: Module esx |
[hide private]
[frames] | no frames]

# Source Code for Module modules.machinery.esx

     1  # Copyright (C) 2010-2013 Claudio Guarnieri.
     2  # Copyright (C) 2014-2016 Cuckoo Foundation.
     3  # Copyright (C) 2013 Christopher Schmitt <cschmitt@tankbusta.net>
     4  # This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org
     5  # See the file 'docs/LICENSE' for copying permission.
     6
     7  import libvirt
     8
     9  from lib.cuckoo.common.abstracts import LibVirtMachinery
    10  from lib.cuckoo.common.exceptions import CuckooCriticalError
    11  from lib.cuckoo.common.exceptions import CuckooMachineError
    12
    13 -class ESX(LibVirtMachinery):
    14      """Virtualization layer for ESXi/ESX based on python-libvirt."""
    15
    16 -    def _initialize_check(self):
    17          """Runs all checks when a machine manager is initialized.
    18          @raise CuckooMachineError: if configuration is invalid
    19          """
    20          if not self.options.esx.dsn:
    21              raise CuckooMachineError("ESX(i) DSN is missing, please add it to the config file")
    22          if not self.options.esx.username:
    23              raise CuckooMachineError("ESX(i) username is missing, please add it to the config file")
    24          if not self.options.esx.password:
    25              raise CuckooMachineError("ESX(i) password is missing, please add it to the config file")
    26
    27          self.dsn = self.options.esx.dsn
    28          self.global_conn = self._global_connect()
    29          super(ESX, self)._initialize_check()
    30
    31 -    def _auth_callback(self, credentials, user_data):
    32          for credential in credentials:
    33              if credential[0] == libvirt.VIR_CRED_AUTHNAME:
    34                  credential[4] = self.options.esx.username
    35              elif credential[0] == libvirt.VIR_CRED_NOECHOPROMPT:
    36                  credential[4] = self.options.esx.password
    37              else:
    38                  raise CuckooCriticalError("ESX machinery did not recieve an object to inject a username or password into")
    39
    40          return 0
    41
    42 -    def _connect(self):
    43          """Return the already-connected single connection handle if set, otherwise set it."""
    44          if self.global_conn is None:
    45              self.global_conn = self._global_connect()
    46          return self.global_conn
    47
    48 -    def _global_connect(self):
    49          """Set the single connection handle."""
    50          try:
    51              self.auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_NOECHOPROMPT], self._auth_callback, None]
    52              return libvirt.openAuth(self.dsn, self.auth, 0)
    53          except libvirt.libvirtError as libvex:
    54              raise CuckooCriticalError("libvirt returned an exception on connection: %s" % libvex)
    55
    56 -    def _disconnect(self, conn):
    57          """Using one global connection we now disconnect in the destructor, ignore requests to disconnect."""
    58          pass
    59
    60 -    def __del__(self):
    61          self.global_conn.close()
    62