2008-09-12 11:36:04 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2008-10-23 00:00:45 -05:00
|
|
|
# Authors:
|
|
|
|
# Rob Crittenden <rcritten@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2008 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License as
|
|
|
|
# published by the Free Software Foundation; version 2 only
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
"""
|
|
|
|
In-tree XML-RPC server using SimpleXMLRPCServer.
|
|
|
|
"""
|
|
|
|
|
2008-09-12 11:36:04 -05:00
|
|
|
import sys
|
|
|
|
import SimpleXMLRPCServer
|
|
|
|
import logging
|
|
|
|
import xmlrpclib
|
|
|
|
import re
|
|
|
|
import threading
|
|
|
|
import commands
|
2008-09-29 23:48:53 -05:00
|
|
|
from ipalib import api
|
2008-10-14 16:46:36 -05:00
|
|
|
from ipalib import config
|
2009-01-04 19:39:39 -06:00
|
|
|
from ipaserver import conn
|
|
|
|
from ipaserver.servercore import context
|
2008-10-04 00:50:59 -05:00
|
|
|
from ipalib.util import xmlrpc_unmarshal
|
2008-09-25 22:53:53 -05:00
|
|
|
import traceback
|
2008-10-23 10:00:50 -05:00
|
|
|
import krbV
|
2008-09-12 11:36:04 -05:00
|
|
|
|
|
|
|
class StoppableXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer):
|
|
|
|
"""Override of TIME_WAIT"""
|
|
|
|
allow_reuse_address = True
|
|
|
|
|
|
|
|
def serve_forever(self):
|
|
|
|
self.stop = False
|
|
|
|
while not self.stop:
|
|
|
|
self.handle_request()
|
|
|
|
|
|
|
|
class LoggingSimpleXMLRPCRequestHandler(SimpleXMLRPCServer.SimpleXMLRPCRequestHandler):
|
|
|
|
"""Overides the default SimpleXMLRPCRequestHander to support logging.
|
|
|
|
Logs client IP and the XML request and response.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def parse(self, given):
|
|
|
|
"""Convert the incoming arguments into the format IPA expects"""
|
|
|
|
args = []
|
|
|
|
kw = {}
|
|
|
|
for g in given:
|
|
|
|
kw[g] = unicode(given[g])
|
|
|
|
return (args, kw)
|
|
|
|
|
|
|
|
def _dispatch(self, method, params):
|
2008-10-28 03:45:38 -05:00
|
|
|
"""
|
|
|
|
Dispatches the XML-RPC method.
|
2008-09-12 11:36:04 -05:00
|
|
|
|
|
|
|
Methods beginning with an '_' are considered private and will
|
|
|
|
not be called.
|
|
|
|
"""
|
2008-10-28 03:45:38 -05:00
|
|
|
if method not in funcs:
|
|
|
|
logger.error('no such method %r', method)
|
|
|
|
raise Exception('method "%s" is not supported' % method)
|
|
|
|
func = funcs[method]
|
2008-10-23 10:00:50 -05:00
|
|
|
krbccache = krbV.default_context().default_ccache().name
|
2008-10-28 03:45:38 -05:00
|
|
|
context.conn = conn.IPAConn(
|
|
|
|
api.env.ldap_host,
|
|
|
|
api.env.ldap_port,
|
|
|
|
krbccache,
|
|
|
|
)
|
|
|
|
logger.info('calling %s', method)
|
2008-10-28 04:08:32 -05:00
|
|
|
(args, kw) = xmlrpc_unmarshal(*params)
|
2008-10-28 03:45:38 -05:00
|
|
|
return func(*args, **kw)
|
2008-09-12 11:36:04 -05:00
|
|
|
|
2008-09-25 22:53:53 -05:00
|
|
|
def _marshaled_dispatch(self, data, dispatch_method = None):
|
|
|
|
try:
|
|
|
|
params, method = xmlrpclib.loads(data)
|
|
|
|
|
|
|
|
# generate response
|
|
|
|
if dispatch_method is not None:
|
|
|
|
response = dispatch_method(method, params)
|
|
|
|
else:
|
|
|
|
response = self._dispatch(method, params)
|
|
|
|
# wrap response in a singleton tuple
|
|
|
|
response = (response,)
|
|
|
|
response = xmlrpclib.dumps(response, methodresponse=1)
|
|
|
|
except:
|
|
|
|
# report exception back to client. This is needed to report
|
|
|
|
# tracebacks found in server code.
|
|
|
|
e_class, e = sys.exc_info()[:2]
|
|
|
|
# FIXME, need to get this number from somewhere...
|
|
|
|
faultCode = getattr(e_class,'faultCode',1)
|
|
|
|
tb_str = ''.join(traceback.format_exception(*sys.exc_info()))
|
|
|
|
faultString = tb_str
|
|
|
|
response = xmlrpclib.dumps(xmlrpclib.Fault(faultCode, faultString))
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
2008-09-12 11:36:04 -05:00
|
|
|
def do_POST(self):
|
|
|
|
clientIP, port = self.client_address
|
2008-10-28 03:33:35 -05:00
|
|
|
# Log client IP and Port
|
2008-09-12 11:36:04 -05:00
|
|
|
logger.info('Client IP: %s - Port: %s' % (clientIP, port))
|
|
|
|
try:
|
|
|
|
# get arguments
|
|
|
|
data = self.rfile.read(int(self.headers["content-length"]))
|
|
|
|
|
|
|
|
# unmarshal the XML data
|
|
|
|
params, method = xmlrpclib.loads(data)
|
2008-10-31 21:59:44 -05:00
|
|
|
logger.info('Call to %s(%s) from %s:%s', method,
|
|
|
|
', '.join(repr(p) for p in params),
|
|
|
|
clientIP, port
|
|
|
|
)
|
2008-09-12 11:36:04 -05:00
|
|
|
|
|
|
|
# Log client request
|
2008-10-31 21:59:44 -05:00
|
|
|
logger.debug('Client request: \n%s\n' % data)
|
2008-09-12 11:36:04 -05:00
|
|
|
|
2008-09-25 22:53:53 -05:00
|
|
|
response = self._marshaled_dispatch(
|
2008-09-12 11:36:04 -05:00
|
|
|
data, getattr(self, '_dispatch', None))
|
|
|
|
|
2008-10-28 03:33:35 -05:00
|
|
|
# Log server response
|
2008-10-31 21:59:44 -05:00
|
|
|
logger.debug('Server response: \n%s\n' % response)
|
2008-10-28 03:33:35 -05:00
|
|
|
except Exception, e:
|
2008-09-12 11:36:04 -05:00
|
|
|
# This should only happen if the module is buggy
|
|
|
|
# internal error, report as HTTP server error
|
2008-09-25 22:53:53 -05:00
|
|
|
print e
|
2008-09-12 11:36:04 -05:00
|
|
|
self.send_response(500)
|
|
|
|
self.end_headers()
|
|
|
|
else:
|
|
|
|
# got a valid XML-RPC response
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header("Content-type", "text/xml")
|
|
|
|
self.send_header("Content-length", str(len(response)))
|
|
|
|
self.end_headers()
|
|
|
|
self.wfile.write(response)
|
|
|
|
|
|
|
|
# shut down the connection
|
|
|
|
self.wfile.flush()
|
|
|
|
self.connection.shutdown(1)
|
|
|
|
|
|
|
|
|
2008-10-23 00:00:45 -05:00
|
|
|
if __name__ == '__main__':
|
2008-10-31 20:03:07 -05:00
|
|
|
api.bootstrap_with_global_options(context='server')
|
2008-10-28 04:08:32 -05:00
|
|
|
api.finalize()
|
2008-10-30 15:11:24 -05:00
|
|
|
logger = api.log
|
2008-10-23 00:00:45 -05:00
|
|
|
|
|
|
|
# Set up the server
|
2008-10-28 03:10:56 -05:00
|
|
|
XMLRPCServer = StoppableXMLRPCServer(
|
|
|
|
('', api.env.lite_xmlrpc_port),
|
|
|
|
LoggingSimpleXMLRPCRequestHandler
|
|
|
|
)
|
2008-10-23 00:00:45 -05:00
|
|
|
XMLRPCServer.register_introspection_functions()
|
2008-09-12 11:36:04 -05:00
|
|
|
|
2008-10-23 00:00:45 -05:00
|
|
|
# Get and register all the methods
|
2008-10-28 04:08:32 -05:00
|
|
|
|
2008-10-23 00:00:45 -05:00
|
|
|
for cmd in api.Command:
|
2008-10-28 03:10:56 -05:00
|
|
|
logger.debug('registering %s', cmd)
|
2008-10-23 00:00:45 -05:00
|
|
|
XMLRPCServer.register_function(api.Command[cmd], cmd)
|
|
|
|
funcs = XMLRPCServer.funcs
|
2008-09-12 11:36:04 -05:00
|
|
|
|
2008-10-28 04:08:32 -05:00
|
|
|
logger.info('Logging to file %r', api.env.log)
|
2008-10-28 03:10:56 -05:00
|
|
|
logger.info('Listening on port %d', api.env.lite_xmlrpc_port)
|
2008-10-23 00:00:45 -05:00
|
|
|
try:
|
|
|
|
XMLRPCServer.serve_forever()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
XMLRPCServer.server_close()
|
2008-10-28 03:10:56 -05:00
|
|
|
logger.info('Server shutdown.')
|