Пример #1
0
    def start(self):
        """
        Start TLS negotiation.

        This checks if the receiving entity requires TLS, the SSL library is
        available and uses the C{required} and C{wanted} instance variables to
        determine what to do in the various different cases.

        For example, if the SSL library is not available, and wanted and
        required by the user, it raises an exception. However if it is not
        required by both parties, initialization silently succeeds, moving
        on to the next step.
        """
        if self.wanted:
            if ssl is None:
                if self.required:
                    return defer.fail(TLSNotSupported())
                else:
                    return defer.succeed(None)
            else:
                pass
        elif self.xmlstream.features[self.feature].required:
            return defer.fail(TLSRequired())
        else:
            return defer.succeed(None)

        self._deferred = defer.Deferred()
        self.xmlstream.addOnetimeObserver("/proceed", self.onProceed)
        self.xmlstream.addOnetimeObserver("/failure", self.onFailure)
        self.xmlstream.send(domish.Element((NS_XMPP_TLS, "starttls")))
        return self._deferred
Пример #2
0
 def _get(self, keys, withIdentifier, multiple):
     """
     Helper method for C{get} and C{getMultiple}.
     """
     if self._disconnected:
         return fail(RuntimeError("not connected"))
     for key in keys:
         if not isinstance(key, str):
             return fail(
                 ClientError(
                     "Invalid type for key: %s, expecting a string" %
                     (type(key), )))
         if len(key) > self.MAX_KEY_LENGTH:
             return fail(ClientError("Key too long"))
     if withIdentifier:
         cmd = "gets"
     else:
         cmd = "get"
     fullcmd = "%s %s" % (cmd, " ".join(keys))
     self.sendLine(fullcmd)
     if multiple:
         values = dict([(key, (0, "", None)) for key in keys])
         cmdObj = Command(cmd, keys=keys, values=values, multiple=True)
     else:
         cmdObj = Command(cmd,
                          key=keys[0],
                          value=None,
                          flags=0,
                          cas="",
                          multiple=False)
     self._current.append(cmdObj)
     return cmdObj._deferred
Пример #3
0
 def _set(self, cmd, key, val, flags, expireTime, cas):
     """
     Internal wrapper for setting values.
     """
     if self._disconnected:
         return fail(RuntimeError("not connected"))
     if not isinstance(key, str):
         return fail(ClientError(
             "Invalid type for key: %s, expecting a string" % (type(key),)))
     if len(key) > self.MAX_KEY_LENGTH:
         return fail(ClientError("Key too long"))
     if not isinstance(val, str):
         return fail(ClientError(
             "Invalid type for value: %s, expecting a string" %
             (type(val),)))
     if cas:
         cas = " " + cas
     length = len(val)
     fullcmd = "%s %s %d %d %d%s" % (
         cmd, key, flags, expireTime, length, cas)
     self.sendLine(fullcmd)
     self.sendLine(val)
     cmdObj = Command(cmd, key=key, flags=flags, length=length)
     self._current.append(cmdObj)
     return cmdObj._deferred
Пример #4
0
 def _set(self, cmd, key, val, flags, expireTime, cas):
     """
     Internal wrapper for setting values.
     """
     if self._disconnected:
         return fail(RuntimeError("not connected"))
     if not isinstance(key, str):
         return fail(
             ClientError("Invalid type for key: %s, expecting a string" %
                         (type(key), )))
     if len(key) > self.MAX_KEY_LENGTH:
         return fail(ClientError("Key too long"))
     if not isinstance(val, str):
         return fail(
             ClientError("Invalid type for value: %s, expecting a string" %
                         (type(val), )))
     if cas:
         cas = " " + cas
     length = len(val)
     fullcmd = "%s %s %d %d %d%s" % (cmd, key, flags, expireTime, length,
                                     cas)
     self.sendLine(fullcmd)
     self.sendLine(val)
     cmdObj = Command(cmd, key=key, flags=flags, length=length)
     self._current.append(cmdObj)
     return cmdObj._deferred
Пример #5
0
 def _get(self, keys, withIdentifier, multiple):
     """
     Helper method for C{get} and C{getMultiple}.
     """
     if self._disconnected:
         return fail(RuntimeError("not connected"))
     for key in keys:
         if not isinstance(key, str):
             return fail(ClientError(
                 "Invalid type for key: %s, expecting a string" % (type(key),)))
         if len(key) > self.MAX_KEY_LENGTH:
             return fail(ClientError("Key too long"))
     if withIdentifier:
         cmd = "gets"
     else:
         cmd = "get"
     fullcmd = "%s %s" % (cmd, " ".join(keys))
     self.sendLine(fullcmd)
     if multiple:
         values = dict([(key, (0, "", None)) for key in keys])
         cmdObj = Command(cmd, keys=keys, values=values, multiple=True)
     else:
         cmdObj = Command(cmd, key=keys[0], value=None, flags=0, cas="",
                          multiple=False)
     self._current.append(cmdObj)
     return cmdObj._deferred
Пример #6
0
 def getRegistrationInfo(self, userURI):
     if userURI.host != self.domain:
         return defer.fail(LookupError("unknown domain"))
     if self.users.has_key(userURI.username):
         dc, url = self.users[userURI.username]
         return defer.succeed(Registration(int(dc.getTime() - time.time()), url))
     else:
         return defer.fail(LookupError("no such user"))
Пример #7
0
 def getAddress(self, userURI):
     if userURI.host != self.domain:
         return defer.fail(LookupError("unknown domain"))
     if self.users.has_key(userURI.username):
         dc, url = self.users[userURI.username]
         return defer.succeed(url)
     else:
         return defer.fail(LookupError("no such user"))
Пример #8
0
 def getAddress(self, userURI):
     if userURI.host != self.domain:
         return defer.fail(LookupError("unknown domain"))
     if self.users.has_key(userURI.username):
         dc, url = self.users[userURI.username]
         return defer.succeed(url)
     else:
         return defer.fail(LookupError("no such user"))
Пример #9
0
 def getRegistrationInfo(self, userURI):
     if userURI.host != self.domain:
         return defer.fail(LookupError("unknown domain"))
     if self.users.has_key(userURI.username):
         dc, url = self.users[userURI.username]
         return defer.succeed(
             Registration(int(dc.getTime() - time.time()), url))
     else:
         return defer.fail(LookupError("no such user"))
Пример #10
0
 def registerAddress(self, domainURL, logicalURL, physicalURL):
     if domainURL.host != self.domain:
         log.msg("Registration for domain we don't handle.")
         return defer.fail(RegistrationError(404))
     if logicalURL.host != self.domain:
         log.msg("Registration for domain we don't handle.")
         return defer.fail(RegistrationError(404))
     if self.users.has_key(logicalURL.username):
         dc, old = self.users[logicalURL.username]
         dc.reset(3600)
     else:
         dc = reactor.callLater(3600, self._expireRegistration, logicalURL.username)
     log.msg("Registered %s at %s" % (logicalURL.toString(), physicalURL.toString()))
     self.users[logicalURL.username] = (dc, physicalURL)
     return defer.succeed(Registration(int(dc.getTime() - time.time()), physicalURL))
Пример #11
0
 def _incrdecr(self, cmd, key, val):
     """
     Internal wrapper for incr/decr.
     """
     if self._disconnected:
         return fail(RuntimeError("not connected"))
     if not isinstance(key, str):
         return fail(ClientError(
             "Invalid type for key: %s, expecting a string" % (type(key),)))
     if len(key) > self.MAX_KEY_LENGTH:
         return fail(ClientError("Key too long"))
     fullcmd = "%s %s %d" % (cmd, key, int(val))
     self.sendLine(fullcmd)
     cmdObj = Command(cmd, key=key)
     self._current.append(cmdObj)
     return cmdObj._deferred
Пример #12
0
 def _incrdecr(self, cmd, key, val):
     """
     Internal wrapper for incr/decr.
     """
     if self._disconnected:
         return fail(RuntimeError("not connected"))
     if not isinstance(key, str):
         return fail(
             ClientError("Invalid type for key: %s, expecting a string" %
                         (type(key), )))
     if len(key) > self.MAX_KEY_LENGTH:
         return fail(ClientError("Key too long"))
     fullcmd = "%s %s %d" % (cmd, key, int(val))
     self.sendLine(fullcmd)
     cmdObj = Command(cmd, key=key)
     self._current.append(cmdObj)
     return cmdObj._deferred
Пример #13
0
 def getHostByName(self, name, timeout=(1, 3, 11, 45)):
     try:
         address = socket.gethostbyname(name)
     except socket.error:
         msg = "address %r not found" % (name, )
         err = error.DNSLookupError(msg)
         return defer.fail(err)
     else:
         return defer.succeed(address)
Пример #14
0
 def _expireRegistration(self, username):
     try:
         dc, url = self.users[username]
     except KeyError:
         return defer.fail(LookupError("no such user"))
     else:
         dc.cancel()
         del self.users[username]
     return defer.succeed(Registration(0, url))
Пример #15
0
 def registerAddress(self, domainURL, logicalURL, physicalURL):
     if domainURL.host != self.domain:
         log.msg("Registration for domain we don't handle.")
         return defer.fail(RegistrationError(404))
     if logicalURL.host != self.domain:
         log.msg("Registration for domain we don't handle.")
         return defer.fail(RegistrationError(404))
     if self.users.has_key(logicalURL.username):
         dc, old = self.users[logicalURL.username]
         dc.reset(3600)
     else:
         dc = reactor.callLater(3600, self._expireRegistration,
                                logicalURL.username)
     log.msg("Registered %s at %s" %
             (logicalURL.toString(), physicalURL.toString()))
     self.users[logicalURL.username] = (dc, physicalURL)
     return defer.succeed(
         Registration(int(dc.getTime() - time.time()), physicalURL))
Пример #16
0
 def _expireRegistration(self, username):
     try:
         dc, url = self.users[username]
     except KeyError:
         return defer.fail(LookupError("no such user"))
     else:
         dc.cancel()
         del self.users[username]
     return defer.succeed(Registration(0, url))
Пример #17
0
 def lookupGroup(self, name):
     assert isinstance(name, unicode)
     name = name.lower()
     try:
         group = self.groups[name]
     except KeyError:
         return defer.fail(failure.Failure(ewords.NoSuchGroup(name)))
     else:
         return defer.succeed(group)
Пример #18
0
 def lookupUser(self, name):
     assert isinstance(name, unicode)
     name = name.lower()
     try:
         user = self.users[name]
     except KeyError:
         return defer.fail(failure.Failure(ewords.NoSuchUser(name)))
     else:
         return defer.succeed(user)
Пример #19
0
 def getHostByName(self, name, timeout = (1, 3, 11, 45)):
     try:
         address = socket.gethostbyname(name)
     except socket.error:
         msg = "address %r not found" % (name,)
         err = error.DNSLookupError(msg)
         return defer.fail(err)
     else:
         return defer.succeed(address)
Пример #20
0
    def delete(self, key):
        """
        Delete an existing C{key}.

        @param key: the key to delete.
        @type key: C{str}

        @return: a deferred that will be called back with C{True} if the key
            was successfully deleted, or C{False} if not.
        @rtype: L{Deferred}
        """
        if self._disconnected:
            return fail(RuntimeError("not connected"))
        if not isinstance(key, str):
            return fail(ClientError(
                "Invalid type for key: %s, expecting a string" % (type(key),)))
        self.sendLine("delete %s" % key)
        cmdObj = Command("delete", key=key)
        self._current.append(cmdObj)
        return cmdObj._deferred
Пример #21
0
    def delete(self, key):
        """
        Delete an existing C{key}.

        @param key: the key to delete.
        @type key: C{str}

        @return: a deferred that will be called back with C{True} if the key
            was successfully deleted, or C{False} if not.
        @rtype: L{Deferred}
        """
        if self._disconnected:
            return fail(RuntimeError("not connected"))
        if not isinstance(key, str):
            return fail(
                ClientError("Invalid type for key: %s, expecting a string" %
                            (type(key), )))
        self.sendLine("delete %s" % key)
        cmdObj = Command("delete", key=key)
        self._current.append(cmdObj)
        return cmdObj._deferred
Пример #22
0
    def version(self):
        """
        Get the version of the server.

        @return: a deferred that will fire with the string value of the
            version.
        @rtype: L{Deferred}
        """
        if self._disconnected:
            return fail(RuntimeError("not connected"))
        self.sendLine("version")
        cmdObj = Command("version")
        self._current.append(cmdObj)
        return cmdObj._deferred
Пример #23
0
    def flushAll(self):
        """
        Flush all cached values.

        @return: a deferred that will be called back with C{True} when the
            operation has succeeded.
        @rtype: L{Deferred}
        """
        if self._disconnected:
            return fail(RuntimeError("not connected"))
        self.sendLine("flush_all")
        cmdObj = Command("flush_all")
        self._current.append(cmdObj)
        return cmdObj._deferred
Пример #24
0
    def version(self):
        """
        Get the version of the server.

        @return: a deferred that will fire with the string value of the
            version.
        @rtype: L{Deferred}
        """
        if self._disconnected:
            return fail(RuntimeError("not connected"))
        self.sendLine("version")
        cmdObj = Command("version")
        self._current.append(cmdObj)
        return cmdObj._deferred
Пример #25
0
    def flushAll(self):
        """
        Flush all cached values.

        @return: a deferred that will be called back with C{True} when the
            operation has succeeded.
        @rtype: L{Deferred}
        """
        if self._disconnected:
            return fail(RuntimeError("not connected"))
        self.sendLine("flush_all")
        cmdObj = Command("flush_all")
        self._current.append(cmdObj)
        return cmdObj._deferred
Пример #26
0
    def connect(self, protocolFactory):
        """
        Implement L{IStreamClientEndpoint.connect} to connect via TCP.
        """
        def _canceller(deferred):
            connector.stopConnecting()
            deferred.errback(
                error.ConnectingCancelledError(connector.getDestination()))

        try:
            wf = _WrappingFactory(protocolFactory, _canceller)
            connector = self._reactor.connectTCP(
                self._host, self._port, wf,
                timeout=self._timeout, bindAddress=self._bindAddress)
            return wf._onConnection
        except:
            return defer.fail()
Пример #27
0
    def connect(self, protocolFactory):
        """
        Implement L{IStreamClientEndpoint.connect} to connect via a
        UNIX Socket
        """
        def _canceller(deferred):
            connector.stopConnecting()
            deferred.errback(
                error.ConnectingCancelledError(connector.getDestination()))

        try:
            wf = _WrappingFactory(protocolFactory, _canceller)
            connector = self._reactor.connectUNIX(
                self._path, wf,
                timeout=self._timeout,
                checkPID=self._checkPID)
            return wf._onConnection
        except:
            return defer.fail()
Пример #28
0
    def stats(self, arg=None):
        """
        Get some stats from the server. It will be available as a dict.

        @param arg: An optional additional string which will be sent along
            with the I{stats} command.  The interpretation of this value by
            the server is left undefined by the memcache protocol
            specification.
        @type arg: L{NoneType} or L{str}

        @return: a deferred that will fire with a C{dict} of the available
            statistics.
        @rtype: L{Deferred}
        """
        if arg:
            cmd = "stats " + arg
        else:
            cmd = "stats"
        if self._disconnected:
            return fail(RuntimeError("not connected"))
        self.sendLine(cmd)
        cmdObj = Command("stats", values={})
        self._current.append(cmdObj)
        return cmdObj._deferred
Пример #29
0
    def stats(self, arg=None):
        """
        Get some stats from the server. It will be available as a dict.

        @param arg: An optional additional string which will be sent along
            with the I{stats} command.  The interpretation of this value by
            the server is left undefined by the memcache protocol
            specification.
        @type arg: L{NoneType} or L{str}

        @return: a deferred that will fire with a C{dict} of the available
            statistics.
        @rtype: L{Deferred}
        """
        if arg:
            cmd = "stats " + arg
        else:
            cmd = "stats"
        if self._disconnected:
            return fail(RuntimeError("not connected"))
        self.sendLine(cmd)
        cmdObj = Command("stats", values={})
        self._current.append(cmdObj)
        return cmdObj._deferred
Пример #30
0
 def getHostByName(self, name, timeout):
     try:
         return succeed(self.names[name])
     except KeyError:
         return fail(DNSLookupError("FakeResolver couldn't find " + name))
Пример #31
0
 def addGroup(self, group):
     if group.name in self.groups:
         return defer.fail(failure.Failure(ewords.DuplicateGroup()))
     self.groups[group.name] = group
     return defer.succeed(group)
Пример #32
0
 def addUser(self, user):
     if user.name in self.users:
         return defer.fail(failure.Failure(ewords.DuplicateUser()))
     self.users[user.name] = user
     return defer.succeed(user)