Exemplo n.º 1
0
    def HandleDataStoreService(self):
        """Initiate a conversation for handling data store commands."""
        if self.data_server:
            # If the data server is connected, this does not make sense.
            self._EmptyResponse(constants.RESPONSE_NOT_A_CLIENT)
            return
        # We never return anything for this request.
        # Simply use the socket and serve database requests.
        sock = self.connection
        sock.setblocking(1)

        # But first we need to validate the client by reading the token.
        token = rdf_data_server.DataStoreAuthToken(self.post_data)
        perms = self.NONCE_STORE.ValidateAuthTokenClient(token)
        if not perms:
            sock.sendall("IP\n")
            sock.close()
            self.close_connection = 1
            return

        logging.info("Client %s has started using the data server",
                     self.client_address)
        try:
            # Send handshake.
            sock.settimeout(self.LOGIN_TIMEOUT)  # 10 seconds to login.
            sock.sendall("OK\n")
        except (socket.error, socket.timeout):
            logging.warning("Could not login client %s", self.client_address)
            self.close_connection = 1
            return

        while True:
            # Handle requests
            replybody = self.HandleClient(sock, perms)

            if not replybody:
                # Client probably died or there was an error in the connection.
                # Force the client to reconnect and send the command again.
                sock.close()
                self.close_connection = 1
                return

            try:
                sock.settimeout(self.SEND_TIMEOUT)  # 1 minute timeout.
                sock.sendall(replybody)
            except (socket.error, socket.timeout):
                # At this point, there is no way to know how much data was actually
                # sent. Therefore, we close the connection and force the client to
                # reconnect. When the client gets an error, he should assume that
                # the command was not successful
                sock.close()
                self.close_connection = 1
                return
Exemplo n.º 2
0
 def GenerateAuthToken(cls, nonce, username, password):
     hsh = cls._GenerateAuthHash(nonce, username, password)
     return data_server.DataStoreAuthToken(nonce=nonce,
                                           hash=hsh,
                                           username=username)