[5a71bac] | 1 | #!/usr/bin/python |
---|
| 2 | |
---|
| 3 | from BaseHTTPServer import BaseHTTPRequestHandler |
---|
| 4 | |
---|
| 5 | from M2Crypto import SSL |
---|
| 6 | from M2Crypto.SSL.SSLServer import ForkingSSLServer |
---|
| 7 | import xmlrpclib |
---|
| 8 | |
---|
| 9 | # The SSL server here is based on the implementation described at |
---|
| 10 | # http://www.xml.com/pub/a/ws/2004/01/20/salz.html |
---|
| 11 | |
---|
| 12 | # Turn off the matching of hostname to certificate ID |
---|
| 13 | SSL.Connection.clientPostConnectionCheck = None |
---|
| 14 | |
---|
| 15 | class server(ForkingSSLServer): |
---|
| 16 | """ |
---|
| 17 | Interface the fedd services to the XMLRPC and SOAP interfaces |
---|
| 18 | """ |
---|
| 19 | def __init__(self, ME, ssl_ctx): |
---|
| 20 | """ |
---|
| 21 | Create an SSL server that handles the transport in handler using the |
---|
| 22 | credentials in ssl_ctx. ME is the host port pair on which to bind. |
---|
| 23 | """ |
---|
| 24 | |
---|
| 25 | ForkingSSLServer.__init__(self, ME, xmlrpc_handler, ssl_ctx) |
---|
| 26 | |
---|
| 27 | # def handle_error(self, request=None, address=None): |
---|
| 28 | # """ |
---|
| 29 | # The default SSLServer prints a stack trace here. This is a little |
---|
| 30 | # friendlier. |
---|
| 31 | # """ |
---|
| 32 | # if request or address: |
---|
| 33 | # print "[partialprover] Error on incoming connection: %s %s" % \ |
---|
| 34 | # (request or "", address or "") |
---|
| 35 | # else: |
---|
| 36 | # print "[partialprover] Error on incoming connection " + \ |
---|
| 37 | # "(Likely SSL error)" |
---|
| 38 | |
---|
| 39 | class xmlrpc_handler(BaseHTTPRequestHandler): |
---|
| 40 | """ |
---|
| 41 | Standard connection between XMLRPC and the fedd services in impl. |
---|
| 42 | |
---|
| 43 | Much of this is boilerplate from |
---|
| 44 | http://www.xml.com/pub/a/ws/2004/01/20/salz.html |
---|
| 45 | """ |
---|
[ab52de1] | 46 | server_version = "libabac/0.2 " + BaseHTTPRequestHandler.server_version |
---|
[5a71bac] | 47 | |
---|
| 48 | def send_xml(self, text, code=200): |
---|
| 49 | """Send an XML document as reply""" |
---|
| 50 | self.send_response(code) |
---|
| 51 | self.send_header('Content-type', 'text/xml; charset="utf-8"') |
---|
| 52 | self.send_header('Content-Length', str(len(text))) |
---|
| 53 | self.end_headers() |
---|
| 54 | self.wfile.write(text) |
---|
| 55 | self.wfile.flush() |
---|
| 56 | # Make sure to close the socket when we're done |
---|
| 57 | self.request.socket.close() |
---|
| 58 | |
---|
| 59 | def do_POST(self): |
---|
| 60 | """Treat an HTTP POST request as an XMLRPC service call""" |
---|
| 61 | # NB: XMLRPC faults are not HTTP errors, so the code is always 200, |
---|
| 62 | # unless an HTTP error occurs, which we don't handle. |
---|
| 63 | |
---|
| 64 | resp = None |
---|
| 65 | data = None |
---|
| 66 | method = None |
---|
| 67 | cl = int(self.headers['content-length']) |
---|
| 68 | data = self.rfile.read(cl) |
---|
| 69 | |
---|
| 70 | try: |
---|
| 71 | params, method = xmlrpclib.loads(data) |
---|
| 72 | except xmlrpclib.ResponseError: |
---|
| 73 | data = xmlrpclib.dumps(xmlrpclib.Fault("Client", |
---|
| 74 | "Malformed request"), methodresponse=True) |
---|
| 75 | |
---|
| 76 | if method != None: |
---|
| 77 | try: |
---|
| 78 | resp = self.xmlrpc_dispatch(method, params, |
---|
| 79 | fedid(cert=self.request.get_peer_cert())) |
---|
| 80 | data = xmlrpclib.dumps((resp,), encoding='UTF-8', |
---|
| 81 | methodresponse=True) |
---|
| 82 | except xmlrpclib.Fault, f: |
---|
| 83 | data = xmlrpclib.dumps(f, methodresponse=True) |
---|
| 84 | resp = None |
---|
| 85 | |
---|
| 86 | self.send_xml(data) |
---|
| 87 | |
---|
| 88 | def xmlrpc_dispatch(self, method, req, fid): |
---|
| 89 | """ |
---|
| 90 | The connection to the implementation, using the method maps |
---|
| 91 | |
---|
| 92 | The implementation provides a mapping from XMLRPC method name to the |
---|
| 93 | method in the implementation that provides the service. |
---|
| 94 | """ |
---|
| 95 | if self.server.xmlrpc_methods.has_key(method): |
---|
| 96 | try: |
---|
| 97 | return self.server.xmlrpc_methods[method](req, fid) |
---|
| 98 | except service_error, e: |
---|
| 99 | raise xmlrpclib.Fault(e.code_string(), e.desc) |
---|
| 100 | else: |
---|
| 101 | raise xmlrpclib.Fault(100, "Unknown method: %s" % method) |
---|