2 # This module implements a general-purpose server layer for geni.
3 # The same basic server should be usable on the registry, component, or
6 # TODO: investigate ways to combine this with existing PLC server?
9 import SimpleXMLRPCServer
14 import BaseHTTPServer
\r
15 import SimpleHTTPServer
\r
16 import SimpleXMLRPCServer
\r
20 from credential import *
\r
23 from OpenSSL import SSL
\r
26 # Verification callback for pyOpenSSL. We do our own checking of keys because
\r
27 # we have our own authentication spec. Thus we disable several of the normal
\r
28 # prohibitions that OpenSSL places on certificates
\r
30 def verify_callback(conn, x509, err, depth, preverify):
\r
31 # if the cert has been preverified, then it is ok
\r
36 # we're only passing single certificates, not chains
38 #print " depth > 0 in verify_callback"
41 # create a Certificate object and load it from the client's x509
42 ctx = conn.get_context()
43 server = ctx.get_app_data()
44 server.peer_cert = Certificate()
45 server.peer_cert.load_from_pyopenssl_x509(x509)
47 # the certificate verification done by openssl checks a number of things
48 # that we aren't interested in, so we look out for those error messages
51 # allow self-signed certificates
53 #print " X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT"
56 # allow certs that don't have an issuer
58 #print " X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY"
61 # allow certs that are untrusted
63 #print " X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE"
66 # allow certs that are untrusted
68 #print " X509_V_ERR_CERT_UNTRUSTED"
71 print " error", err, "in verify_callback"
76 # Taken from the web (XXX find reference). Implements an HTTPS xmlrpc server
\r
78 class SecureXMLRPCServer(BaseHTTPServer.HTTPServer,SimpleXMLRPCServer.SimpleXMLRPCDispatcher):
\r
79 def __init__(self, server_address, HandlerClass, key_file, cert_file, logRequests=True):
\r
80 """Secure XML-RPC server.
\r
82 It it very similar to SimpleXMLRPCServer but it uses HTTPS for transporting XML data.
\r
84 self.logRequests = logRequests
\r
86 SimpleXMLRPCServer.SimpleXMLRPCDispatcher.__init__(self, True, None)
\r
87 SocketServer.BaseServer.__init__(self, server_address, HandlerClass)
\r
88 ctx = SSL.Context(SSL.SSLv23_METHOD)
\r
89 ctx.use_privatekey_file(key_file)
\r
90 ctx.use_certificate_file(cert_file)
\r
91 ctx.set_verify(SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT, verify_callback)
\r
92 ctx.set_app_data(self)
\r
93 self.socket = SSL.Connection(ctx, socket.socket(self.address_family,
\r
96 self.server_activate()
\r
100 # Convert an exception on the server to a full stack trace and send it to
103 def _dispatch(self, method, params):
\r
105 return SimpleXMLRPCServer.SimpleXMLRPCDispatcher._dispatch(self, method, params)
\r
107 # can't use format_exc() as it is not available in jython yet (even
\r
109 type, value, tb = sys.exc_info()
\r
110 raise xmlrpclib.Fault(1,''.join(traceback.format_exception(type, value, tb)))
\r
113 # taken from the web (XXX find reference). Implents HTTPS xmlrpc request handler
\r
115 class SecureXMLRpcRequestHandler(SimpleXMLRPCServer.SimpleXMLRPCRequestHandler):
\r
116 """Secure XML-RPC request handler class.
\r
118 It it very similar to SimpleXMLRPCRequestHandler but it uses HTTPS for transporting XML data.
\r
121 self.connection = self.request
\r
122 self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
\r
123 self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
\r
126 """Handles the HTTPS POST request.
\r
128 It was copied out from SimpleXMLRPCServer.py and modified to shutdown the socket cleanly.
\r
133 data = self.rfile.read(int(self.headers["content-length"]))
\r
134 # In previous versions of SimpleXMLRPCServer, _dispatch
\r
135 # could be overridden in this class, instead of in
\r
136 # SimpleXMLRPCDispatcher. To maintain backwards compatibility,
\r
137 # check to see if a subclass implements _dispatch and dispatch
\r
138 # using that method if present.
\r
139 response = self.server._marshaled_dispatch(
\r
140 data, getattr(self, '_dispatch', None)
\r
142 except: # This should only happen if the module is buggy
\r
143 # internal error, report as HTTP server error
\r
144 self.send_response(500)
\r
148 # got a valid XML RPC response
\r
149 self.send_response(200)
\r
150 self.send_header("Content-type", "text/xml")
\r
151 self.send_header("Content-length", str(len(response)))
\r
153 self.wfile.write(response)
\r
155 # shut down the connection
\r
157 self.connection.shutdown() # Modified here!
\r
160 # Implements an HTTPS XML-RPC server. Generally it is expected that GENI
161 # functions will take a credential string, which is passed to
162 # decode_authentication. Decode_authentication() will verify the validity of
163 # the credential, and verify that the user is using the key that matches the
164 # GID supplied in the credential.
169 # Create a new GeniServer object.
171 # @param ip the ip address to listen on
172 # @param port the port to listen on
173 # @param key_file private key filename of registry
174 # @param cert_file certificate filename containing public key (could be a GID file)
176 def __init__(self, ip, port, key_file, cert_file):
177 self.key = Keypair(filename = key_file)
178 self.cert = Certificate(filename = cert_file)
179 self.server = SecureXMLRPCServer((ip, port), SecureXMLRpcRequestHandler, key_file, cert_file)
180 self.trusted_cert_list = None
181 self.register_functions()
184 # Decode the credential string that was submitted by the caller. Several
185 # checks are performed to ensure that the credential is valid, and that the
186 # callerGID included in the credential matches the caller that is
187 # connected to the HTTPS connection.
189 def decode_authentication(self, cred_string, operation):
190 self.client_cred = Credential(string = cred_string)
191 self.client_gid = self.client_cred.get_gid_caller()
192 self.object_gid = self.client_cred.get_gid_object()
194 # make sure the client_gid is not blank
195 if not self.client_gid:
196 raise MissingCallerGID(self.client_cred.get_subject())
198 # make sure the client_gid matches the certificate that the client is using
199 peer_cert = self.server.peer_cert
200 if not peer_cert.is_pubkey(self.client_gid.get_pubkey()):
201 raise ConnectionKeyGIDMismatch(self.client_gid.get_subject())
203 # make sure the client is allowed to perform the operation
204 if not self.client_cred.can_perform(operation):
205 raise InsufficientRights(operation)
207 if self.trusted_cert_list:
208 self.client_cred.verify_chain(self.trusted_cert_list)
210 self.client_gid.verify_chain(self.trusted_cert_list)
212 self.object_gid.verify_chain(self.trusted_cert_list)
215 # Register functions that will be served by the XMLRPC server. This
216 # function should be overrided by each descendant class.
218 def register_functions(self):
219 self.server.register_function(self.noop)
222 # Sample no-op server function. The no-op function decodes the credential
223 # that was passed to it.
225 def noop(self, cred, anything):
226 self.decode_authentication(cred)
231 # Execute the server, serving requests forever.
234 self.server.serve_forever()