connectors/PYRO/PSK_Adapter.py
author Edouard Tisserant
Mon, 15 Oct 2018 16:26:59 +0200
changeset 2312 84b3cc18893b
child 2313 2eaf235270f8
permissions -rw-r--r--
Replaced PYROSSL with PYROPSK.
Secrets are files named $ID.secret in project's /psk directory.
Connect with URI formated PYROPSK://host[:port]#ID
2312
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
     1
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
     2
import sslpsk
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
     3
import Pyro
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
     4
from Pyro.protocol import _connect_socket,TCPConnection,PYROAdapter
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
     5
from Pyro.errors import ConnectionDeniedError, ProtocolError
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
     6
from Pyro.util import Log
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
     7
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
     8
#
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
     9
# The TLS-PSK adapter that handles SSL connections instead of regular sockets,
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    10
# but using Pre Shared Keys instead of Certificates
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    11
#
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    12
class PYROPSKAdapter(PYROAdapter):
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    13
    # This is essentialy the same as in Pyro/protocol.py
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    14
    # only raw_sock wrapping into sock through sslpsk.wrap_socket was added
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    15
    # Pyro unfortunately doesn't allow cleaner customization
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    16
    def bindToURI(self,URI):
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    17
        with self.lock:   # only 1 thread at a time can bind the URI
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    18
            try:
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    19
                self.URI=URI
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    20
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    21
                # This are the statements that differ from Pyro/protocol.py
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    22
                raw_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    23
                _connect_socket(raw_sock, URI.address, URI.port, self.timeout)
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    24
                sock = sslpsk.wrap_socket(
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    25
                    raw_sock, psk=Pyro.config.PYROPSK, server_side=False)
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    26
                # all the rest is the same as in Pyro/protocol.py 
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    27
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    28
                conn=TCPConnection(sock, sock.getpeername())
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    29
                # receive the authentication challenge string, and use that to build the actual identification string.
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    30
                try:
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    31
                    authChallenge=self.recvAuthChallenge(conn)
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    32
                except ProtocolError,x:
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    33
                    # check if we were denied
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    34
                    if hasattr(x,"partialMsg") and x.partialMsg[:len(self.denyMSG)]==self.denyMSG:
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    35
                        raise ConnectionDeniedError(Pyro.constants.deniedReasons[int(x.partialMsg[-1])])
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    36
                    else:
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    37
                        raise
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    38
                # reply with our ident token, generated from the ident passphrase and the challenge
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    39
                msg = self._sendConnect(sock,self.newConnValidator.createAuthToken(self.ident, authChallenge, conn.addr, self.URI, None) )
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    40
                if msg==self.acceptMSG:
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    41
                    self.conn=conn
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    42
                    self.conn.connected=1
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    43
                    Log.msg('PYROAdapter','connected to',str(URI))
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    44
                    if URI.protocol=='PYROLOC':
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    45
                        self.resolvePYROLOC_URI("PYRO") # updates self.URI
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    46
                elif msg[:len(self.denyMSG)]==self.denyMSG:
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    47
                    try:
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    48
                        raise ConnectionDeniedError(Pyro.constants.deniedReasons[int(msg[-1])])
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    49
                    except (KeyError,ValueError):
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    50
                        raise ConnectionDeniedError('invalid response')
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    51
            except socket.error:
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    52
                Log.msg('PYROAdapter','connection failed to URI',str(URI))
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    53
                raise ProtocolError('connection failed')
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    54
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    55
_getProtocolAdapter = Pyro.protocol.getProtocolAdapter
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    56
def getProtocolAdapter(protocol):
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    57
    if protocol in ('PYROPSK', 'PYROLOCPSK'):
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    58
        return PYROPSKAdapter()
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    59
    _getProtocolAdapter(protocol)
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    60
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    61
Pyro.protocol.getProtocolAdapter = getProtocolAdapter
84b3cc18893b Replaced PYROSSL with PYROPSK.
Edouard Tisserant
parents:
diff changeset
    62