SecureXMLRPCServer.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """
  2. SecureXMLRPCServer module using pyOpenSSL 0.5
  3. Written 0907.2002
  4. by Michal Wallace
  5. http://www.sabren.net/
  6. This acts exactly like SimpleXMLRPCServer
  7. from the standard python library, but
  8. uses secure connections. The technique
  9. and classes should work for any SocketServer
  10. style server. However, the code has not
  11. been extensively tested.
  12. This code is in the public domain.
  13. It is provided AS-IS WITH NO WARRANTY WHATSOEVER.
  14. """
  15. import SocketServer
  16. import os, socket
  17. import SimpleXMLRPCServer
  18. from OpenSSL import SSL
  19. class SSLWrapper:
  20. """
  21. This whole class exists just to filter out a parameter
  22. passed in to the shutdown() method in SimpleXMLRPC.doPOST()
  23. """
  24. def __init__(self, conn):
  25. """
  26. Connection is not yet a new-style class,
  27. so I'm making a proxy instead of subclassing.
  28. """
  29. self.__dict__["conn"] = conn
  30. def __getattr__(self,name):
  31. return getattr(self.__dict__["conn"], name)
  32. def __setattr__(self,name, value):
  33. setattr(self.__dict__["conn"], name, value)
  34. def shutdown(self, how=1):
  35. """
  36. SimpleXMLRpcServer.doPOST calls shutdown(1),
  37. and Connection.shutdown() doesn't take
  38. an argument. So we just discard the argument.
  39. """
  40. self.__dict__["conn"].shutdown()
  41. def accept(self):
  42. """
  43. This is the other part of the shutdown() workaround.
  44. Since servers create new sockets, we have to infect
  45. them with our magic. :)
  46. """
  47. c, a = self.__dict__["conn"].accept()
  48. return (SSLWrapper(c), a)
  49. class SecureTCPServer(SocketServer.TCPServer):
  50. """
  51. Just like TCPServer, but use a socket.
  52. This really ought to let you specify the key and certificate files.
  53. """
  54. def __init__(self, server_address, RequestHandlerClass):
  55. SocketServer.BaseServer.__init__(self, server_address, RequestHandlerClass)
  56. ## Same as normal, but make it secure:
  57. ctx = SSL.Context(SSL.SSLv23_METHOD)
  58. ctx.set_options(SSL.OP_NO_SSLv2)
  59. dir = os.curdir
  60. ctx.use_privatekey_file (os.path.join(dir, 'server.pkey'))
  61. ctx.use_certificate_file(os.path.join(dir, 'server.cert'))
  62. self.socket = SSLWrapper(SSL.Connection(ctx, socket.socket(self.address_family,
  63. self.socket_type)))
  64. self.server_bind()
  65. self.server_activate()
  66. class SecureXMLRPCRequestHandler(SimpleXMLRPCServer.SimpleXMLRPCRequestHandler):
  67. def setup(self):
  68. """
  69. We need to use socket._fileobject Because SSL.Connection
  70. doesn't have a 'dup'. Not exactly sure WHY this is, but
  71. this is backed up by comments in socket.py and SSL/connection.c
  72. """
  73. self.connection = self.request # for doPOST
  74. self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
  75. self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
  76. class SecureXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer, SecureTCPServer):
  77. def __init__(self, addr,
  78. requestHandler=SecureXMLRPCRequestHandler,
  79. logRequests=1):
  80. """
  81. This is the exact same code as SimpleXMLRPCServer.__init__
  82. except it calls SecureTCPServer.__init__ instead of plain
  83. old TCPServer.__init__
  84. """
  85. self.funcs = {}
  86. self.logRequests = logRequests
  87. self.instance = None
  88. SecureTCPServer.__init__(self, addr, requestHandler)