Debian Codegraph @

ipaserver/custodia/httpd/server.py

raw
1 # Copyright (C) 2015 Custodia Project Contributors - see LICENSE file
2 from __future__ import absolute_import
3
4 import atexit
5 import errno
6 import os
7 import shutil
8 import socket
9 import struct
10 import sys
11 import warnings
12
13 from http.server import BaseHTTPRequestHandler
14 from socketserver import ForkingTCPServer, BaseServer
15 from urllib.parse import parse_qs, unquote, urlparse
16
17 import six
18
19 from ipaserver.custodia import log
20 from ipaserver.custodia.plugin import HTTPError
21
22 try:
23 from systemd import daemon as sd
24 except ImportError:
25 sd = None
26 if 'NOTIFY_SOCKET' in os.environ:
27 warnings.warn(
28 "NOTIFY_SOCKET env var is set but python-systemd bindings are "
29 "not available!",
30 category=RuntimeWarning
31 )
32 if 'LISTEN_FDS' in os.environ:
33 warnings.warn(
34 "LISTEN_FDS env var is set, but python-systemd bindings are"
35 "not available!",
36 category=RuntimeWarning
37 )
38
39
40 logger = log.getLogger(__name__)
41
42 SO_PEERCRED = getattr(socket, 'SO_PEERCRED', 17)
43 SO_PEERSEC = getattr(socket, 'SO_PEERSEC', 31)
44 SELINUX_CONTEXT_LEN = 256
45 MAX_REQUEST_SIZE = 10 * 1024 * 1024 # For now limit body to 10MiB
46
47
48 class ForkingHTTPServer(ForkingTCPServer):
49 """
50 A forking HTTP Server.
51 Each request runs into a forked server so that the whole environment
52 is clean and isolated, and parallel requests cannot unintentionally
53 influence one another.
54
55 When a request is received it is parsed by the handler_class provided
56 at server initialization.
57 """
58 server_string = "Custodia/0.1"
59 allow_reuse_address = True
60 socket_file = None
61
62 def __init__(self, server_address, handler_class, config,
63 bind_and_activate=True):
64 # pylint: disable=non-parent-init-called
65 # Init just BaseServer, TCPServer creates a socket.
66 BaseServer.__init__(self, server_address, handler_class)
67
68 if isinstance(server_address, socket.socket):
69 # It's a bound and activates socket from systemd.
70 self.socket = server_address
71 bind_and_activate = False
72 else:
73 self.socket = socket.socket(self.address_family,
74 self.socket_type)
75
76 # copied from TCPServer
77 if bind_and_activate:
78 try:
79 self.server_bind()
80 self.server_activate()
81 except BaseException:
82 self.server_close()
83 raise
84
85 if self.socket.family == socket.AF_UNIX:
86 self.socket_file = self.socket.getsockname()
87
88 if 'consumers' not in config:
89 raise ValueError('Configuration does not provide any consumer')
90 self.config = config
91 if 'server_string' in self.config:
92 self.server_string = self.config['server_string']
93 self.auditlog = log.auditlog
94
95
96 class ForkingUnixHTTPServer(ForkingHTTPServer):
97 address_family = socket.AF_UNIX
98
99 def server_bind(self):
100 self.unlink()
101 # Remove on exit
102 atexit.register(self.unlink)
103 basedir = os.path.dirname(self.server_address)
104 if not os.path.isdir(basedir):
105 os.makedirs(basedir, mode=0o755)
106 ForkingHTTPServer.server_bind(self)
107 os.chmod(self.server_address, 0o666)
108
109 def unlink(self):
110 try:
111 os.unlink(self.server_address)
112 except OSError:
113 pass
114
115
116 class HTTPRequestHandler(BaseHTTPRequestHandler):
117
118 """
119 This request handler is a slight modification of BaseHTTPRequestHandler
120 where the per-request handler is replaced.
121
122 When a request comes in it is parsed and the 'request' dictionary is
123 populated accordingly. Additionally a 'creds' structure is added to the
124 request.
125
126 The 'creds' structure contains the data retrieved via a call to
127 getsockopt with the SO_PEERCRED option. This retrieves via kernel assist
128 the uid,gid and pid of the process on the other side of the unix socket
129 on which the request has been made. This can be used for authentication
130 and/or authorization purposes.
131 The 'creds' structure is further augmented with a 'context' option
132 containing the Selinux Context string for the calling process, if
133 available.
134
135 after the request is parsed the server's pipeline() function is invoked
136 in order to handle it. The pipeline() should return a response object,
137 where te return 'code', the 'output' and 'headers' may be found.
138
139 If no 'code' is present the request is assumed to be successful and a
140 '200 OK' status code will be sent back to the client.
141
142 The 'output' parameter can be a string or a file like object.
143
144 The 'headers' objct must be a dictionary where keys are headers names.
145
146 By default we assume HTTP1.0
147 """
148
149 protocol_version = "HTTP/1.0"
150
151 def __init__(self, request, client_address, server):
152 self.requestline = ''
153 self.request_version = ''
154 self.command = ''
155 self.raw_requestline = None
156 self.close_connection = 0
157 self.path = None # quoted, raw path
158 self.path_chain = None # tuple of unquoted segments
159 self.query = None
160 self.url = None
161 self.body = None
162 self.loginuid = None
163 self._creds = False
164 BaseHTTPRequestHandler.__init__(self, request, client_address, server)
165
166 def version_string(self):
167 return self.server.server_string
168
169 def _get_loginuid(self, pid):
170 loginuid = None
171 # NOTE: Using proc to find the login uid is not reliable
172 # this is why login uid is fetched separately and not stored
173 # into 'creds', to avoid giving the false impression it can be
174 # used to perform access control decisions
175 try:
176 with open("/proc/%i/loginuid" % pid, "r") as f:
177 loginuid = int(f.read())
178 except IOError as e:
179 if e.errno != errno.ENOENT:
180 raise
181 if loginuid == -1:
182 loginuid = None
183 return loginuid
184
185 @property
186 def peer_creds(self):
187 if self._creds is not False:
188 return self._creds
189 # works only for unix sockets
190 if self.request.family != socket.AF_UNIX:
191 self._creds = None
192 return self._creds
193 # pid_t: signed int32, uid_t/gid_t: unsigned int32
194 fmt = 'iII'
195 creds = self.request.getsockopt(socket.SOL_SOCKET, SO_PEERCRED,
196 struct.calcsize(fmt))
197 pid, uid, gid = struct.unpack(fmt, creds)
198 try:
199 creds = self.request.getsockopt(socket.SOL_SOCKET, SO_PEERSEC,
200 SELINUX_CONTEXT_LEN)
201 context = creds.rstrip(b'\x00').decode('utf-8')
202 except Exception: # pylint: disable=broad-except
203 logger.debug("Couldn't retrieve SELinux Context", exc_info=True)
204 context = None
205
206 self._creds = {'pid': pid, 'uid': uid, 'gid': gid, 'context': context}
207 return self._creds
208
209 @property
210 def peer_info(self):
211 if self.peer_creds is not None:
212 return self._creds['pid']
213 elif self.request.family in {socket.AF_INET, socket.AF_INET6}:
214 return self.request.getpeername()
215 return None
216
217 @property
218 def peer_cert(self):
219 if not hasattr(self.request, 'getpeercert'):
220 return None
221 return self.request.getpeercert()
222
223 def parse_request(self):
224 if not BaseHTTPRequestHandler.parse_request(self):
225 return False
226
227 # grab the loginuid from `/proc` as soon as possible
228 creds = self.peer_creds
229 if creds is not None:
230 self.loginuid = self._get_loginuid(creds['pid'])
231
232 # after basic parsing also use urlparse to retrieve individual
233 # elements of a request.
234 url = urlparse(self.path)
235
236 # Yes, override path with the path part only
237 self.path = url.path
238 self.path_chain = self._parse_path(url)
239
240 # Create dict out of query
241 self.query = parse_qs(url.query)
242
243 # keep the rest into the 'url' element in case someone needs it
244 self.url = url
245
246 return True
247
248 def _parse_path(self, url):
249 path_chain = []
250 for segment in url.path.split('/'):
251 # unquote URL path encoding
252 segment = unquote(segment)
253 path_chain.append(segment)
254 return tuple(path_chain)
255
256 def parse_body(self):
257 length = int(self.headers.get('content-length', 0))
258 if length > MAX_REQUEST_SIZE:
259 raise HTTPError(413)
260 if length == 0:
261 self.body = None
262 else:
263 self.body = self.rfile.read(length)
264
265 def handle_one_request(self):
266 if self.request.family == socket.AF_UNIX:
267 # Set a fake client address to make log functions happy
268 self.client_address = ['127.0.0.1', 0]
269 try:
270 if not self.server.config:
271 self.close_connection = 1
272 return
273 self.raw_requestline = self.rfile.readline(65537)
274 if not self.raw_requestline:
275 self.close_connection = 1
276 return
277 if len(self.raw_requestline) > 65536:
278 self.requestline = ''
279 self.request_version = ''
280 self.command = ''
281 self.send_error(414)
282 self.wfile.flush()
283 return
284 if not self.parse_request():
285 self.close_connection = 1
286 return
287 try:
288 self.parse_body()
289 except HTTPError as e:
290 self.send_error(e.code, e.mesg)
291 self.wfile.flush()
292 return
293 request = {'creds': self.peer_creds,
294 'client_cert': self.peer_cert,
295 'client_id': self.peer_info,
296 'command': self.command,
297 'path': self.path,
298 'path_chain': self.path_chain,
299 'query': self.query,
300 'url': self.url,
301 'version': self.request_version,
302 'headers': self.headers,
303 'body': self.body}
304 logger.debug(
305 "REQUEST: %s %s, query: %r, cred: %r, client_id: %s, "
306 "headers: %r, body: %r",
307 request['command'], request['path_chain'], request['query'],
308 request['creds'], request['client_id'],
309 dict(request['headers']), request['body']
310 )
311 try:
312 response = self.pipeline(self.server.config, request)
313 if response is None:
314 raise HTTPError(500)
315 except HTTPError as e:
316 self.send_error(e.code, e.mesg)
317 self.wfile.flush()
318 return
319 except socket.timeout as e:
320 self.log_error("Request timed out: %r", e)
321 self.close_connection = 1
322 return
323 except Exception as e: # pylint: disable=broad-except
324 self.log_error("Handler failed: %r", e, exc_info=True)
325 self.send_error(500)
326 self.wfile.flush()
327 return
328
329 self.send_response(response.get('code', 200))
330 for header, value in six.iteritems(response.get('headers', {})):
331 self.send_header(header, value)
332 self.end_headers()
333
334 output = response.get('output', None)
335 if hasattr(output, 'read'):
336 shutil.copyfileobj(output, self.wfile)
337 output.close()
338 elif output is not None:
339 self.wfile.write(output)
340 else:
341 self.close_connection = 1
342 self.wfile.flush()
343 return
344 except socket.timeout as e:
345 self.log_error("Request timed out: %r", e)
346 self.close_connection = 1
347 return
348
349 # pylint: disable=arguments-differ
350 def log_error(self, fmtstr, *args, **kwargs):
351 logger.error(fmtstr, *args, **kwargs)
352
353 def pipeline(self, config, request):
354 """
355 The pipeline() function handles authentication and invocation of
356 the correct consumer based on the server configuration, that is
357 provided at initialization time.
358
359 When authentication is performed all the authenticators are
360 executed. If any returns False, authentication fails and a 403
361 error is raised. If none of them positively succeeds and they all
362 return None then also authentication fails and a 403 error is
363 raised. Authentication plugins can add attributes to the request
364 object for use of authorization or other plugins.
365
366 When authorization is performed and positive result will cause the
367 operation to be accepted and any negative result will cause it to
368 fail. If no authorization plugin returns a positive result a 403
369 error is returned.
370
371 Once authentication and authorization are successful the pipeline
372 will parse the path component and find the consumer plugin that
373 handles the provided path walking up the path component by
374 component until a consumer is found.
375
376 Paths are walked up from the leaf to the root, so if two consumers
377 hang on the same tree, the one closer to the leaf will be used. If
378 there is a trailing path when the conumer is selected then it will
379 be stored in the request dicstionary named 'trail'. The 'trail' is
380 an ordered list of the path components below the consumer entry
381 point.
382 """
383 path_chain = request['path_chain']
384 if not path_chain or path_chain[0] != '':
385 # no path or not an absolute path
386 raise HTTPError(400)
387
388 # auth framework here
389 authers = config.get('authenticators')
390 if authers is None:
391 raise HTTPError(403)
392 valid_once = False
393 for auth in authers:
394 valid = authers[auth].handle(request)
395 if valid is False:
396 raise HTTPError(403)
397 elif valid is True:
398 valid_once = True
399 if valid_once is not True:
400 self.server.auditlog.svc_access(self.__class__.__name__,
401 log.AUDIT_SVC_AUTH_FAIL,
402 request['client_id'], 'No auth')
403 raise HTTPError(403)
404
405 # auhz framework here
406 authzers = config.get('authorizers')
407 if authzers is None:
408 raise HTTPError(403)
409 authz_ok = None
410 for authz in authzers:
411 valid = authzers[authz].handle(request)
412 if valid is True:
413 authz_ok = True
414 elif valid is False:
415 authz_ok = False
416 break
417 if authz_ok is not True:
418 self.server.auditlog.svc_access(self.__class__.__name__,
419 log.AUDIT_SVC_AUTHZ_FAIL,
420 request['client_id'],
421 path_chain)
422 raise HTTPError(403)
423
424 # Select consumer
425 trail = []
426 while path_chain:
427 if path_chain in config['consumers']:
428 con = config['consumers'][path_chain]
429 if len(trail) != 0:
430 request['trail'] = trail
431 return con.handle(request)
432 trail.insert(0, path_chain[-1])
433 path_chain = path_chain[:-1]
434
435 raise HTTPError(404)
436
437
438 class HTTPServer:
439 handler = HTTPRequestHandler
440
441 def __init__(self, srvurl, config):
442 url = urlparse(srvurl)
443 serverclass, address = self._get_serverclass(url)
444 if sd is not None:
445 address = self._get_systemd_socket(address)
446 self.httpd = serverclass(address, self.handler, config)
447
448 def _get_serverclass(self, url):
449 if url.scheme == 'http+unix':
450 # Unix socket
451 address = unquote(url.netloc)
452 if not address:
453 raise ValueError('Empty address {}'.format(url))
454 logger.info('Serving on Unix socket %s', address)
455 serverclass = ForkingUnixHTTPServer
456 elif url.scheme == 'http':
457 host, port = url.netloc.split(":")
458 address = (host, int(port))
459 logger.info('Serving on %s (HTTP)', url.netloc)
460 serverclass = ForkingHTTPServer
461 else:
462 raise ValueError('Unknown URL Scheme: %s' % url.scheme)
463 return serverclass, address
464
465 def _get_systemd_socket(self, address):
466 fds = sd.listen_fds()
467 if not fds:
468 return address
469 elif len(fds) > 1:
470 raise ValueError('Too many listening sockets', fds)
471
472 if isinstance(address, tuple):
473 port = address[1]
474 # systemd uses IPv6
475 if not sd.is_socket_inet(fds[0], family=socket.AF_INET6,
476 type=socket.SOCK_STREAM,
477 listening=True, port=port):
478 raise ValueError(
479 "FD {} is not TCP IPv6 socket on port {}".format(
480 fds[0], port
481 )
482 )
483 logger.info('Using systemd socket activation on port %i', port)
484 sock = socket.fromfd(fds[0], socket.AF_INET6, socket.SOCK_STREAM)
485 else:
486 if not sd.is_socket_unix(fds[0], socket.SOCK_STREAM,
487 listening=True, path=address):
488 raise ValueError(
489 "FD {} is not Unix stream socket on path {}".format(
490 fds[0], address
491 )
492 )
493 logger.info('Using systemd socket activation on path %s', address)
494 sock = socket.fromfd(fds[0], socket.AF_UNIX, socket.SOCK_STREAM)
495
496 if sys.version_info[0] < 3:
497 # Python 2.7's socket.fromfd() returns _socket.socket
498 sock = socket.socket(_sock=sock)
499 return sock
500
501 def get_socket(self):
502 return (self.httpd.socket, self.httpd.socket_file)
503
504 def serve(self):
505 if sd is not None and sd.booted():
506 sd.notify("READY=1")
507 return self.httpd.serve_forever()