lib.cuckoo.core.rooter

Package lib :: Package cuckoo :: Package core :: Module rooter |
[hide private]
[frames] | no frames]

# Source Code for Module lib.cuckoo.core.rooter

     1  # Copyright (C) 2010-2013 Claudio Guarnieri.
     2  # Copyright (C) 2014-2016 Cuckoo Foundation.
     3  # This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org
     4  # See the file 'docs/LICENSE' for copying permission.
     5
     6  import json
     7  import logging
     8  import os.path
     9  import socket
    10  import tempfile
    11  import threading
    12
    13  from lib.cuckoo.common.config import Config
    14
    15  cfg = Config()
    16  log = logging.getLogger(__name__)
    17  unixpath = tempfile.mktemp()
    18  lock = threading.Lock()
    19
    20  vpns = {}
    21
    22 -def rooter(command, *args, **kwargs):
    23      if not os.path.exists(cfg.cuckoo.rooter):
    24          log.critical("Unable to passthrough root command (%s) as the rooter "
    25                       "unix socket doesn't exist.", command)
    26          return
    27
    28      lock.acquire()
    29
    30      s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
    31
    32      if os.path.exists(unixpath):
    33          os.remove(unixpath)
    34
    35      s.bind(unixpath)
    36
    37      try:
    38          s.connect(cfg.cuckoo.rooter)
    39      except socket.error as e:
    40          log.critical("Unable to passthrough root command as we're unable to "
    41                       "connect to the rooter unix socket: %s.", e)
    42          return
    43
    44      s.send(json.dumps({
    45          "command": command,
    46          "args": args,
    47          "kwargs": kwargs,
    48      }))
    49
    50      ret = json.loads(s.recv(0x10000))
    51
    52      lock.release()
    53
    54      if ret["exception"]:
    55          log.warning("Rooter returned error: %s", ret["exception"])
    56
    57      return ret["output"]
    58