Beispiel #1
0
    def login(self, password, *ident):
        # send login packet
        resp = self.sendRequest(C.MANAGER_ID, []).result()
        challenge = resp[0][1] # get password challenge

        # send password response
        if password is None:
            password = support.get_password(self.host, self.port)
        m = hashlib.md5()
        m.update(challenge)
        m.update(password)
        try:
            resp = self.sendRequest(C.MANAGER_ID, [(0L, m.digest())]).result()
        except Exception:
            raise LoginFailedError('Incorrect password.')
        support.cache_password(self.host, self.port, password)
        self.password = password
        self.loginMessage = resp[0][1] # get welcome message

        # send identification
        try:
            resp = self.sendRequest(C.MANAGER_ID, [(0L, (1L,) + ident)]).result()
        except Exception:
            raise LoginFailedError('Bad identification.')
        return resp[0][1] # get assigned ID
Beispiel #2
0
def parseServerOptions(name, exit_on_failure=True, options=None):
    """Parse standard command line options for a server.

    Args:
        name (string): The default server name to use if no name is specified
            on the command line.
        exit_on_failure (boolean): If True, we call sys.exit when we fail
            to parse the command line options. Otherwise, we raise UsageError.
        options (list(string)): If given, parse options from the given strings.
            Otherwise, will parse options from the command line in sys.argv.

    Returns:
        A ServerOptions instance initialized from the command line arguments.
        This is a dict-like object containing these string-valued keys:
            'name', 'node', 'host', 'port', 'password', 'tls'
    """
    from twisted.python import usage

    class ServerOptions(usage.Options):
        optParameters = [
            ['name', 'n', name, 'Server name.'],
            ['node', 'd', getNodeName(), 'Node name.'],
            ['host', 'h', C.MANAGER_HOST, 'Manager location.'],
            ['port', 'p', None, 'Manager port.', int],
            ['password', 'w', None, 'Login password.'],
            ['tls', 's', C.MANAGER_TLS,
             'TLS mode for connecting to manager (on/starttls/off)']]

    config = ServerOptions()
    config['tls'] = C.check_tls_mode(config['tls'])
    try:
        config.parseOptions(options=options)
        if config['password'] is None:
            config['password'] = support.get_password(host=config['host'],
                                                      port=config['port'],
                                                      prompt=False)
        if config['port'] is None:
            tls_on = config['tls'] == 'on'
            config['port'] = C.MANAGER_PORT_TLS if tls_on else C.MANAGER_PORT
    except usage.UsageError, errortext:
        print '%s: %s' % (sys.argv[0], errortext)
        print '%s: Try --help for usage details.' % (sys.argv[0])
        if exit_on_failure:
            sys.exit(1)
        else:
            raise
Beispiel #3
0
    def authenticate(self, password):
        """Authenticate to the manager using the given password."""
        # send login packet
        resp = yield self.sendRequest(C.MANAGER_ID, [])
        challenge = resp[0][1] # get password challenge

        if password is None:
            password = support.get_password(self.host, self.port)

        # send password response
        m = hashlib.md5()
        m.update(challenge)
        m.update(password)
        try:
            resp = yield self.sendRequest(C.MANAGER_ID, [(0L, m.digest())])
        except Exception:
            raise errors.LoginFailedError('Incorrect password.')
        support.cache_password(self.host, self.port, password)
        self.password = password
        self.loginMessage = resp[0][1] # get welcome message