authenticators.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. """
  2. authenticators: the server instance accepts an authenticator object,
  3. which is basically any callable (i.e., a function) that takes the newly
  4. connected socket and "authenticates" it.
  5. the authenticator should return a socket-like object with its associated
  6. credentials (a tuple), or raise AuthenticationError if it fails.
  7. a very trivial authenticator might be
  8. def magic_word_authenticator(sock):
  9. if sock.recv(5) != "Ma6ik":
  10. raise AuthenticationError("wrong magic word")
  11. return sock, None
  12. s = ThreadedServer(...., authenticator = magic_word_authenticator)
  13. your authenticator can return any socket-like object. for instance, it may
  14. authenticate the client and return a TLS/SSL-wrapped socket object that
  15. encrypts the transport.
  16. the credentials returned alongside with the new socket can be any object.
  17. it will be stored in the rpyc connection configruation under the key
  18. "credentials", and may be used later by the service logic. if no credentials
  19. are applicable, just return None as in the example above.
  20. rpyc includes integration with tlslite, a TLS/SSL library:
  21. the VdbAuthenticator class authenticates clients based on username-password
  22. pairs.
  23. """
  24. import os
  25. import sys
  26. import anydbm
  27. from rpyc.lib import safe_import
  28. tlsapi = safe_import("tlslite.api")
  29. ssl = safe_import("ssl")
  30. class AuthenticationError(Exception):
  31. pass
  32. class SSLAuthenticator(object):
  33. def __init__(self, keyfile, certfile, ca_certs = None, ssl_version = None):
  34. self.keyfile = keyfile
  35. self.certfile = certfile
  36. self.ca_certs = ca_certs
  37. if ca_certs:
  38. self.cert_reqs = ssl.CERT_REQUIRED
  39. else:
  40. self.cert_reqs = ssl.CERT_NONE
  41. if ssl_version:
  42. self.ssl_version = ssl_version
  43. else:
  44. self.ssl_version = ssl.PROTOCOL_TLSv1
  45. def __call__(self, sock):
  46. try:
  47. sock2 = ssl.wrap_socket(sock, keyfile = self.keyfile, certfile = self.certfile,
  48. server_side = True, ssl_version = self.ssl_version, ca_certs = self.ca_certs,
  49. cert_reqs = self.cert_reqs)
  50. except ssl.SSLError:
  51. ex = sys.exc_info()[1]
  52. raise AuthenticationError(str(ex))
  53. return sock2, sock2.getpeercert()
  54. class TlsliteVdbAuthenticator(object):
  55. __slots__ = ["vdb"]
  56. BITS = 2048
  57. def __init__(self, vdb):
  58. self.vdb = vdb
  59. @classmethod
  60. def from_dict(cls, users):
  61. inst = cls(tlsapi.VerifierDB())
  62. for username, password in users.iteritems():
  63. inst.set_user(username, password)
  64. return inst
  65. @classmethod
  66. def _load_vdb_with_mode(cls, vdb, mode):
  67. """taken from tlslite/BaseDB.py -- patched for file mode"""
  68. # {{
  69. db = anydbm.open(vdb.filename, mode)
  70. try:
  71. if db["--Reserved--type"] != vdb.type:
  72. raise ValueError("Not a %s database" % (vdb.type,))
  73. except KeyError:
  74. raise ValueError("Not a recognized database")
  75. vdb.db = db
  76. # }}
  77. @classmethod
  78. def from_file(cls, filename, mode = "w"):
  79. vdb = tlsapi.VerifierDB(filename)
  80. if os.path.exists(filename):
  81. cls._load_vdb_with_mode(vdb, mode)
  82. else:
  83. if mode not in "ncw":
  84. raise ValueError("%s does not exist but mode does not allow "
  85. "writing (%r)" % (filename, mode))
  86. vdb.create()
  87. return cls(vdb)
  88. def sync(self):
  89. self.vdb.db.sync()
  90. def set_user(self, username, password):
  91. self.vdb[username] = self.vdb.makeVerifier(username, password, self.BITS)
  92. def del_user(self, username):
  93. del self.vdb[username]
  94. def list_users(self):
  95. return self.vdb.keys()
  96. def __call__(self, sock):
  97. sock2 = tlsapi.TLSConnection(sock)
  98. sock2.fileno = lambda fd = sock.fileno(): fd # tlslite omitted fileno
  99. try:
  100. sock2.handshakeServer(verifierDB = self.vdb)
  101. except Exception:
  102. ex = sys.exc_info()[1]
  103. raise AuthenticationError(str(ex))
  104. return sock2, sock2.allegedSrpUsername