summaryrefslogtreecommitdiffstats
path: root/src/lib/Component.py
blob: b76b1bd33eeb7d16cbb3b086f758b184f470ea6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
'''Cobalt component base classes'''
__revision__ = '$Revision$'

import logging, select, signal, socket, sys, urlparse, xmlrpclib, cPickle, os, traceback
from base64 import decodestring

import BaseHTTPServer, SimpleXMLRPCServer
import Bcfg2.tlslite.errors
import Bcfg2.tlslite.api
from Bcfg2.tlslite.TLSConnection import TLSConnection

log = logging.getLogger('Component')

class ComponentInitError(Exception):
    '''Raised in case of component initialization failure'''
    pass

class ComponentKeyError(Exception):
    '''raised in case of key parse fails'''
    pass

class ForkedChild(Exception):
    '''raised after child has been forked'''
    pass

class CobaltXMLRPCRequestHandler(SimpleXMLRPCServer.SimpleXMLRPCRequestHandler):
    '''CobaltXMLRPCRequestHandler takes care of ssl xmlrpc requests'''
    masterpid = os.getpid()

    def __init__(self, request, client_address, server):
        self.cleanup = True
        SimpleXMLRPCServer.SimpleXMLRPCRequestHandler.__init__(self, request,
                                                               client_address,
                                                               server)

    def do_POST(self):
        '''Overload do_POST to pass through client address information'''
        try:
            # get arguments
            data = self.rfile.read(int(self.headers["content-length"]))

            authenticated = False
            #try x509 cert auth (will have been completed, just checking status)
            authenticated = self.request.authenticated
            #TLSConnection can be accessed by self.request?
            
            #try httpauth
            if not authenticated and "Authorization" in self.headers:
                binauth = self.headers['Authorization'].replace("Basic ", "")
                namepass = decodestring(binauth).split(':')
                if self.server._authenticate_connection("bogus-method",
                                                        namepass[0],
                                                        namepass[1],
                                                        self.client_address):
                    authenticated = True

            response = self.server._cobalt_marshalled_dispatch(data, self.client_address, authenticated)
        except ForkedChild:
            self.cleanup = False
            return
        except: # This should only happen if the module is buggy
            # internal error, report as HTTP server error
            log.error("Unexcepted handler failure in do_POST", exc_info=1)
            self.send_response(500)
            self.end_headers()
        else:
            # got a valid XML RPC response
            if os.getpid() != self.masterpid:
                pid = os.fork()
                if pid:
                    self.cleanup = False
                    return
            try:
                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()
            except socket.error:
                pass

    def setup(self):
        '''Setup a working connection'''
        self.cleanup = True
        self.connection = self.request
        self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
        self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)

class TLSServer(Bcfg2.tlslite.api.TLSSocketServerMixIn,
                BaseHTTPServer.HTTPServer):
    '''This class is an tlslite-using SSLServer'''
    def __init__(self, address, keyfile, certfile, handler, checker=None,
                 reqCert=False):
        print keyfile, certfile
        self.sc = Bcfg2.tlslite.api.SessionCache()
        self.rc = reqCert
        self.master = os.getpid()
        x509 = Bcfg2.tlslite.api.X509()
        cdata = open(certfile).read()
        x509.parse(cdata)
        self.checker = checker
        kdata = open(keyfile).read()
        try:
            self.key = Bcfg2.tlslite.api.parsePEMKey(kdata, private=True)
        except:
            raise ComponentKeyError
        self.chain = Bcfg2.tlslite.api.X509CertChain([x509])
        BaseHTTPServer.HTTPServer.__init__(self, address, handler)

    def finish_request(self, sock, address):
        sock.settimeout(90)
        tlsConnection = TLSConnection(sock)
        if self.handshake(tlsConnection) == True:
            req = self.RequestHandlerClass(tlsConnection, address, self)
            if req.cleanup:
                tlsConnection.close()
            if os.getpid() != self.master:
                os._exit(0)

    def handshake(self, tlsConnection):
        try:
            tlsConnection.handshakeServer(certChain=self.chain,
                                          privateKey=self.key,
                                          sessionCache=self.sc,
                                          checker=self.checker,
                                          reqCert=self.rc)
            tlsConnection.ignoreAbruptClose = True
            #Connection authenticated during TLS handshake, no need for passwords
            if not self.checker == None:
                tlsConnection.authenticated = True
            else:
                tlsConnection.authenticated = False
            return True
        except Bcfg2.tlslite.errors.TLSError, error:
            return False
        except socket.error:
            return False
                
class Component(TLSServer,
                SimpleXMLRPCServer.SimpleXMLRPCDispatcher):
    """Cobalt component providing XML-RPC access"""
    __name__ = 'Component'
    __implementation__ = 'Generic'
    __statefields__ = []
    async_funcs = []
    fork_funcs = []
    child_limit = 32

    def __init__(self, keyfile, certfile, password, location):
        # need to get addr
        self.shut = False
        signal.signal(signal.SIGINT, self.start_shutdown)
        signal.signal(signal.SIGTERM, self.start_shutdown)
        self.logger = logging.getLogger('Component')
        self.children = []
        self.static = True
        uparsed = urlparse.urlparse(location)[1].split(':')
        sock_loc = (uparsed[0], int(uparsed[1]))

        self.password = password

        try:
            TLSServer.__init__(self, sock_loc, keyfile, certfile,
                               CobaltXMLRPCRequestHandler)
        except socket.error:
            self.logger.error("Failed to bind to socket")
            raise ComponentInitError
        except ComponentKeyError:
            self.logger.error("Failed to parse key" % (keyfile))
            raise ComponentInitError
        except:
            self.logger.error("Failed to load ssl key '%s'" % (keyfile), exc_info=1)
            raise ComponentInitError
        try:
            SimpleXMLRPCServer.SimpleXMLRPCDispatcher.__init__(self)
        except TypeError:
            SimpleXMLRPCServer.SimpleXMLRPCDispatcher.__init__(self, False, None)
        self.logRequests = 0
        self.port = self.socket.getsockname()[1]
        self.url = "https://%s:%s" % (socket.gethostname(), self.port)
        self.logger.info("Bound to port %s" % self.port)
        self.funcs.update({'system.listMethods':self.addr_system_listMethods})
        self.atime = 0

    def _cobalt_marshalled_dispatch(self, data, address, authenticated=False):
        """Decode and dispatch XMLRPC requests. Overloaded to pass through
        client address information
        """
        try:
            rawparams, method = xmlrpclib.loads(data)
        except:
            self.logger.error("Failed to parse request from %s" \
                              % (address[0]))
            #open('/tmp/badreq', 'w').write(data)
            return xmlrpclib.dumps(xmlrpclib.Fault(4, "Bad Request"))
        if not authenticated:
            if len(rawparams) < 2:
                self.logger.error("No authentication included with request from %s" % address[0])
                return xmlrpclib.dumps(xmlrpclib.Fault(2, "No Authentication Info"))
            user = rawparams[0]
            password = rawparams[1]
            params = rawparams[2:]
            # check authentication
            if not self._authenticate_connection(method, user, password, address):
                return xmlrpclib.dumps(xmlrpclib.Fault(3, "Authentication Failure"))
        else:
            #there is no prefixed auth info in this case
            params = rawparams[0:]
        # generate response
        try:
            # need to add waitpid code here to enforce maxchild
            if method in self.fork_funcs:
                self.clean_up_children()
                self.check_for_free_slot()
                pid = os.fork()
                if pid:
                    self.children.append(pid)
                    raise ForkedChild
            # all handlers must take address as the first argument
            response = self._dispatch(method, (address, ) + params)
            # wrap response in a singleton tuple
            response = (response,)
            response = xmlrpclib.dumps(response, methodresponse=1)
        except xmlrpclib.Fault, fault:
            response = xmlrpclib.dumps(fault)
        except TypeError, terror:
            self.logger.error("Client %s called function %s with wrong argument count" %
                           (address[0], method), exc_info=1)
            response = xmlrpclib.dumps(xmlrpclib.Fault(4, terror.args[0]))
        except ForkedChild:
            raise
        except:
            self.logger.error("Unexpected handler failure")
            trace = sys.exc_info()
            self.logger.error("%s : %s" % (str(trace[0]), str(trace[1])))
            for line in traceback.format_exc().splitlines():
                self.logger.error(line)
            del trace
            # report exception back to server
            response = xmlrpclib.dumps(xmlrpclib.Fault(1,
                                   "%s:%s" % (sys.exc_type, sys.exc_value)))
        return response

    def clean_up_children(self):
        while True:
            try:
                pid = os.waitpid(0, os.WNOHANG)[0]
                if pid:
                    if pid in self.children:
                        self.children.remove(pid)
                else:
                    break
            except OSError:
                break

    def check_for_free_slot(self):
        if len(self.children) >= self.child_limit:
            self.logger.info("Reached child_limit; waiting for child exit")
            pid = os.waitpid(0, 0)[0]
            self.children.remove(pid)

    def _authenticate_connection(self, method, user, password, address):
        '''Authenticate new connection'''
        (user, address, method)
        return password == self.password

    def save_state(self):
        '''Save fields defined in __statefields__ in /var/spool/cobalt/__implementation__'''
        if self.__statefields__:
            savedata = tuple([getattr(self, field) for field in self.__statefields__])
        try:
            statefile = open("/var/spool/cobalt/%s" % self.__implementation__, 'w')
            # need to flock here
            statefile.write(cPickle.dumps(savedata))
        except:
            self.logger.info("Statefile save failed; data persistence disabled")
            self.__statefields__ = []

    def load_state(self):
        '''Load fields defined in __statefields__ from /var/spool/cobalt/__implementation__'''
        if self.__statefields__:
            try:
                loaddata = cPickle.loads(open("/var/spool/cobalt/%s" % self.__implementation__).read())
            except:
                self.logger.info("Statefile load failed")
                return
            for field in self.__statefields__:
                setattr(self, field, loaddata[self.__statefields__.index(field)])
                
    def addr_system_listMethods(self, address):
        """get rid of the address argument and call the underlying dispatcher method"""
        return SimpleXMLRPCServer.SimpleXMLRPCDispatcher.system_listMethods(self)

    def get_request(self):
        '''We need to do work between requests, so select with timeout instead of blocking in accept'''
        rsockinfo = []
        while self.socket not in rsockinfo:
            if self.shut:
                raise socket.error
            for funcname in self.async_funcs:
                func = getattr(self, funcname, False)
                if callable(func):
                    func()
                else:
                    self.logger.error("Cannot call uncallable method %s" % (funcname))
            try:
                rsockinfo = select.select([self.socket], [], [], 10)[0]
            except select.error:
                continue
            if self.socket in rsockinfo:
                return self.socket.accept()

    def serve_forever(self):
        """Handle one request at a time until doomsday."""
        while not self.shut:
            self.handle_request()

    def start_shutdown(self, signum, frame):
        '''Shutdown on unexpected signals'''
        self.shut = True