[6ff0b91] | 1 | #!/usr/local/bin/python |
---|
| 2 | |
---|
| 3 | import sys |
---|
| 4 | |
---|
| 5 | from BaseHTTPServer import BaseHTTPRequestHandler |
---|
| 6 | |
---|
| 7 | from ZSI import Fault, ParseException, FaultFromNotUnderstood, \ |
---|
| 8 | FaultFromZSIException, FaultFromException, ParsedSoap, SoapWriter |
---|
| 9 | |
---|
| 10 | from M2Crypto import SSL |
---|
| 11 | from M2Crypto.SSL.SSLServer import SSLServer |
---|
[329f61d] | 12 | import xmlrpclib |
---|
[6ff0b91] | 13 | |
---|
| 14 | from optparse import OptionParser |
---|
| 15 | |
---|
| 16 | from fedd_util import fedd_ssl_context, fedid |
---|
| 17 | from fedd_proj import new_feddservice |
---|
| 18 | |
---|
| 19 | # The SSL server here is based on the implementation described at |
---|
| 20 | # http://www.xml.com/pub/a/ws/2004/01/20/salz.html |
---|
| 21 | |
---|
| 22 | # Turn off the matching of hostname to certificate ID |
---|
| 23 | SSL.Connection.clientPostConnectionCheck = None |
---|
| 24 | |
---|
| 25 | class fedd_server(SSLServer): |
---|
| 26 | def __init__(self, ME, handler, ssl_ctx, impl): |
---|
| 27 | SSLServer.__init__(self, ME, handler, ssl_ctx) |
---|
| 28 | self.impl = impl |
---|
| 29 | |
---|
[329f61d] | 30 | class fedd_soap_handler(BaseHTTPRequestHandler): |
---|
[6ff0b91] | 31 | server_version = "ZSI/2.0 fedd/0.1 " + BaseHTTPRequestHandler.server_version |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | def send_xml(self, text, code=200): |
---|
| 35 | """Send an XML document as reply""" |
---|
| 36 | self.send_response(code) |
---|
| 37 | self.send_header('Content-type', 'text/xml; charset="utf-8"') |
---|
| 38 | self.send_header('Content-Length', str(len(text))) |
---|
| 39 | self.end_headers() |
---|
| 40 | self.wfile.write(text) |
---|
| 41 | self.wfile.flush() |
---|
| 42 | |
---|
| 43 | def send_fault(self, f, code=500): |
---|
| 44 | """Send a SOAP encoded fault as reply""" |
---|
[0a47d52] | 45 | self.send_xml(f.AsSOAP(processContents="lax"), code) |
---|
[6ff0b91] | 46 | |
---|
| 47 | def check_headers(self, ps): |
---|
| 48 | """Send a fault for any required envelope headers""" |
---|
| 49 | for (uri, localname) in ps.WhatMustIUnderstand(): |
---|
| 50 | self.send_fault(FaultFromNotUnderstood(uri, lname, 'fedd')) |
---|
| 51 | return False |
---|
| 52 | return True |
---|
| 53 | |
---|
| 54 | def check_method(self, ps): |
---|
| 55 | """Confirm that this class implements the namespace and SOAP method""" |
---|
| 56 | root = ps.body_root |
---|
[7aec37d] | 57 | if root.namespaceURI not in self.server.impl.soap_namespaces: |
---|
[6ff0b91] | 58 | self.send_fault(Fault(Fault.Client, |
---|
| 59 | 'Unknown namespace "%s"' % root.namespaceURI)) |
---|
| 60 | return False |
---|
[329f61d] | 61 | |
---|
| 62 | if getattr(root, 'localName', None) == None: |
---|
| 63 | self.send_fault(Fault(Fault.Client, 'No method"')) |
---|
[6ff0b91] | 64 | return False |
---|
| 65 | return True |
---|
| 66 | |
---|
| 67 | def do_POST(self): |
---|
| 68 | """Treat an HTTP POST request as a SOAP service call""" |
---|
| 69 | try: |
---|
| 70 | cl = int(self.headers['content-length']) |
---|
| 71 | data = self.rfile.read(cl) |
---|
| 72 | ps = ParsedSoap(data) |
---|
| 73 | except ParseException, e: |
---|
[0a47d52] | 74 | self.send_fault(Fault(Fault.Client, str(e))) |
---|
[6ff0b91] | 75 | return |
---|
| 76 | except Exception, e: |
---|
| 77 | self.send_fault(FaultFromException(e, 0, sys.exc_info()[2])) |
---|
| 78 | return |
---|
| 79 | if not self.check_headers(ps): return |
---|
| 80 | if not self.check_method(ps): return |
---|
| 81 | try: |
---|
[329f61d] | 82 | resp = self.server.impl.soap_dispatch(ps.body_root.localName, ps, |
---|
| 83 | fedid(cert=self.request.get_peer_cert())) |
---|
[6ff0b91] | 84 | except Fault, f: |
---|
| 85 | self.send_fault(f) |
---|
| 86 | resp = None |
---|
| 87 | |
---|
| 88 | if resp != None: |
---|
| 89 | sw = SoapWriter() |
---|
| 90 | sw.serialize(resp) |
---|
| 91 | self.send_xml(str(sw)) |
---|
| 92 | |
---|
[329f61d] | 93 | class fedd_xmlrpc_handler(BaseHTTPRequestHandler): |
---|
| 94 | server_version = "ZSI/2.0 fedd/0.1 " + BaseHTTPRequestHandler.server_version |
---|
| 95 | |
---|
| 96 | def send_xml(self, text, code=200): |
---|
| 97 | """Send an XML document as reply""" |
---|
| 98 | self.send_response(code) |
---|
| 99 | self.send_header('Content-type', 'text/xml; charset="utf-8"') |
---|
| 100 | self.send_header('Content-Length', str(len(text))) |
---|
| 101 | self.end_headers() |
---|
| 102 | self.wfile.write(text) |
---|
| 103 | self.wfile.flush() |
---|
| 104 | |
---|
| 105 | def do_POST(self): |
---|
| 106 | """Treat an HTTP POST request as an XMLRPC service call""" |
---|
| 107 | |
---|
| 108 | resp = None |
---|
| 109 | data = None |
---|
[0a47d52] | 110 | method = None |
---|
[329f61d] | 111 | cl = int(self.headers['content-length']) |
---|
| 112 | data = self.rfile.read(cl) |
---|
| 113 | |
---|
| 114 | try: |
---|
[0a47d52] | 115 | params, method = xmlrpclib.loads(data) |
---|
| 116 | except xmlrpclib.ResponseError: |
---|
| 117 | data = xmlrpclib.dumps(xmlrpclib.Fault("Client", |
---|
| 118 | "Malformed request"), methodresponse=True) |
---|
| 119 | |
---|
| 120 | if method != None: |
---|
| 121 | try: |
---|
| 122 | resp = self.server.impl.xmlrpc_dispatch(method, params, |
---|
| 123 | fedid(cert=self.request.get_peer_cert())) |
---|
| 124 | data = xmlrpclib.dumps((resp,), methodresponse=True) |
---|
| 125 | except xmlrpclib.Fault, f: |
---|
| 126 | data = xmlrpclib.dumps(f, methodresponse=True) |
---|
| 127 | resp = None |
---|
[329f61d] | 128 | self.send_xml(data) |
---|
| 129 | |
---|
| 130 | |
---|
[6ff0b91] | 131 | class fedd_opts(OptionParser): |
---|
| 132 | """Encapsulate option processing in this class, rather than in main""" |
---|
| 133 | def __init__(self): |
---|
| 134 | OptionParser.__init__(self, usage="%prog [opts] (--help for details)", |
---|
| 135 | version="0.1") |
---|
| 136 | |
---|
[329f61d] | 137 | self.set_defaults(host="localhost", port=23235, |
---|
| 138 | transport="soap",debug=0) |
---|
[6ff0b91] | 139 | |
---|
| 140 | self.add_option("-d", "--debug", action="count", dest="debug", |
---|
| 141 | help="Set debug. Repeat for more information") |
---|
| 142 | self.add_option("-f", "--configfile", action="store", |
---|
| 143 | dest="configfile", help="Configuration file (required)") |
---|
| 144 | self.add_option("-H", "--host", action="store", type="string", |
---|
| 145 | dest="host", help="Hostname to listen on (default %default)") |
---|
| 146 | self.add_option("-p", "--port", action="store", type="int", |
---|
| 147 | dest="port", help="Port to listen on (default %default)") |
---|
[329f61d] | 148 | self.add_option("-x","--transport", action="store", type="choice", |
---|
| 149 | choices=("xmlrpc", "soap"), |
---|
| 150 | help="Transport for request (xmlrpc|soap) (Default: %default)") |
---|
[6ff0b91] | 151 | self.add_option("--trace", action="store_const", dest="tracefile", |
---|
| 152 | const=sys.stderr, help="Print SOAP exchange to stderr") |
---|
| 153 | |
---|
| 154 | opts, args = fedd_opts().parse_args() |
---|
| 155 | |
---|
| 156 | if opts.configfile != None: |
---|
| 157 | try: |
---|
| 158 | impl = new_feddservice(opts.configfile) |
---|
| 159 | except RuntimeError, e: |
---|
[b234bb9] | 160 | str = getattr(e, 'desc', None) or getattr(e,'message', None) or \ |
---|
| 161 | "No message" |
---|
| 162 | sys.exit("Error configuring fedd: %s" % str) |
---|
[6ff0b91] | 163 | else: |
---|
| 164 | sys.exit("--configfile is required") |
---|
| 165 | |
---|
| 166 | SOAP_port = (opts.host, opts.port) |
---|
| 167 | |
---|
| 168 | if impl.cert_file == None: |
---|
| 169 | sys.exit("Must supply certificate file (probably in config)") |
---|
| 170 | |
---|
[2106ed1] | 171 | ctx = None |
---|
| 172 | while ctx == None: |
---|
[6ff0b91] | 173 | try: |
---|
| 174 | ctx = fedd_ssl_context(impl.cert_file, impl.trusted_certs, |
---|
| 175 | password=impl.cert_pwd) |
---|
| 176 | except SSL.SSLError, e: |
---|
[2106ed1] | 177 | if str(e) != "bad decrypt" or impl.cert_pwd != None: |
---|
[6ff0b91] | 178 | raise |
---|
| 179 | |
---|
[329f61d] | 180 | if opts.transport == "soap": |
---|
| 181 | s = fedd_server(SOAP_port, fedd_soap_handler, ctx, impl) |
---|
| 182 | elif opts.transport == "xmlrpc": |
---|
| 183 | s = fedd_server(SOAP_port, fedd_xmlrpc_handler, ctx, impl) |
---|
| 184 | else: |
---|
| 185 | s = None |
---|
| 186 | |
---|
[6ff0b91] | 187 | s.serve_forever() |
---|