Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# Authors: # Jason Gerard DeRose <jderose@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, either version 3 of the License, or # (at your option) any later version. # # 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, see <http://www.gnu.org/licenses/>.
RPC server.
Also see the `ipalib.rpc` module. """
<head> <title>404 Not Found</title> </head> <body> <h1>Not Found</h1> <p> The requested URL <strong>%(url)s</strong> was not found on this server. </p> </body> </html>"""
<head> <title>400 Bad Request</title> </head> <body> <h1>Bad Request</h1> <p> <strong>%(message)s</strong> </p> </body> </html>"""
<head> <title>500 Internal Server Error</title> </head> <body> <h1>Internal Server Error</h1> <p> <strong>%(message)s</strong> </p> </body> </html>"""
<head> <title>401 Unauthorized</title> </head> <body> <h1>Invalid Authentication</h1> <p> <strong>%(message)s</strong> </p> </body> </html>"""
<head> <title>200 Success</title> </head> <body> <h1>%(title)s</h1> <p> <strong>%(message)s</strong> </p> </body> </html>"""
""" Return a 404 Not Found error. """
""" Return a 400 Bad Request error. """
""" Return a 500 Internal Server Error. """
""" Return a 401 Unauthorized error. """
""" Read the request body from environ['wsgi.input']. """ except (ValueError, TypeError): return
if not query: return for (key, value) in query.iteritems(): if len(value) == 0: yield (key, None) elif len(value) == 1: yield (key, value[0].decode(encoding)) else: yield (key, tuple(v.decode(encoding) for v in value))
""" Return the query as a ``dict``, or ``None`` if no query is presest. """ qstr = None if environ['REQUEST_METHOD'] == 'POST': if environ['CONTENT_TYPE'] == 'application/x-www-form-urlencoded': qstr = read_input(environ) elif environ['REQUEST_METHOD'] == 'GET': qstr = environ['QUERY_STRING'] if qstr: query = dict(nicify_query( urlparse.parse_qs(qstr)#, keep_blank_values=True) )) else: query = {} environ['wsgi.query'] = query return query
""" WSGI routing middleware and entry point into IPA server.
The `wsgi_dispatch` plugin is the entry point into the IPA server. It dispatchs the request to the appropriate wsgi application handler which is specific to the authentication and RPC mechanism. """
return key in self.__apps
finally:
url = environ['SCRIPT_NAME'] + environ['PATH_INFO'] return self.not_found(environ, start_response, url, 'URL fragment "%s" does not have a handler' % (key))
""" Mount the WSGI application *app* at *key*. """ # if self.__islocked__(): # raise StandardError('%s.mount(): locked, cannot mount %r at %r' % ( # self.name, app, key) # ) self.name, self.__apps[key], app, key) )
""" Base class for execution backends with a WSGI application interface. """
return self.marshal(result, RefererError(referer='missing'), _id) return self.marshal(result, RefererError(referer=environ['HTTP_REFERER']), _id) else: reg = lang_.upper() environ.get('CONTENT_TYPE', '').startswith(self.content_type) and environ['REQUEST_METHOD'] == 'POST' ): else: (name, args, options, _id) = self.simple_unmarshal(environ) raise CommandError(name=name) except StandardError, e: self.exception( 'non-public: %s: %s', e.__class__.__name__, str(e) ) error = InternalError() finally: except Exception, e: self.info( 'exception %s caught when converting options: %s', e.__class__.__name__, str(e) ) # get at least some context of what is going on params = options else: else: self.info('%s: %s', context.principal, e.__class__.__name__)
name = environ['PATH_INFO'].strip('/') options = extract_query(environ) return (name, tuple(), options, None)
""" WSGI application for execution. """
except StandardError, e: self.exception('WSGI %s.__call__():', self.name) status = HTTP_STATUS_SERVER_ERROR response = status headers = [('Content-Type', 'text/plain; charset=utf-8')]
# Send session cookie back and store session data # FIXME: the URL path should be retreived from somewhere (but where?), not hardcoded session_cookie = session_mgr.generate_cookie('/ipa', session_data['session_id'], session_data['session_expiration_timestamp']) headers.append(('Set-Cookie', session_cookie))
raise NotImplementedError('%s.unmarshal()' % self.fullname)
raise NotImplementedError('%s.marshal()' % self.fullname)
''' JSON cannot encode binary values. We encode binary values in Python str objects and text in Python unicode objects. In order to allow a binary object to be passed through JSON we base64 encode it thus converting it to text which JSON can transport. To assure we recognize the value is a base64 encoded representation of the original binary value and not confuse it with other text we convert the binary value to a dict in this form:
{'__base64__' : base64_encoding_of_binary_value}
This modification of the original input value cannot be done "in place" as one might first assume (e.g. replacing any binary items in a container (e.g. list, tuple, dict) with the base64 dict because the container might be an immutable object (i.e. a tuple). Therefore this function returns a copy of any container objects it encounters with tuples replaced by lists. This is O.K. because the JSON encoding will map both lists and tuples to JSON arrays. '''
if isinstance(val, dict): new_dict = {} for k,v in val.items(): new_dict[k] = json_encode_binary(v) return new_dict elif isinstance(val, (list, tuple)): new_list = [json_encode_binary(v) for v in val] return new_list elif isinstance(val, str): return {'__base64__' : base64.b64encode(val)} elif isinstance(val, Decimal): return {'__base64__' : base64.b64encode(str(val))} elif isinstance(val, DN): return str(val) else: return val
''' JSON cannot transport binary data. In order to transport binary data we convert binary data to a form like this:
{'__base64__' : base64_encoding_of_binary_value}
see json_encode_binary()
After JSON had decoded the JSON stream back into a Python object we must recursively scan the object looking for any dicts which might represent binary values and replace the dict containing the base64 encoding of the binary value with the decoded binary value. Unlike the encoding problem where the input might consist of immutable object, all JSON decoded container are mutable so the conversion could be done in place. However we don't modify objects in place because of side effects which may be dangerous. Thus we elect to spend a few more cycles and avoid the possibility of unintended side effects in favor of robustness. '''
return base64.b64decode(val['__base64__']) else: new_dict[k] = base64.b64decode(v['__base64__']) else: binary_val = base64.b64decode(v['__base64__']) new_list.append(binary_val) else: else: except UnicodeDecodeError: raise ConversionError( name=val, error='incorrect type' ) else:
""" JSON RPC server.
For information on the JSON-RPC spec, see:
http://json-rpc.org/wiki/specification """
''' '''
self.debug('WSGI jsonserver.__call__:')
response = super(jsonserver, self).__call__(environ, start_response) return response
if error: assert isinstance(error, PublicError) error = dict( code=error.errno, message=error.strerror, name=error.__class__.__name__, ) principal = getattr(context, 'principal', 'UNKNOWN') response = dict( result=result, error=error, id=_id, principal=unicode(principal), version=unicode(VERSION), ) response = json_encode_binary(response) return json.dumps(response, sort_keys=True, indent=4)
''' Instances of the AuthManger class are used to handle authentication events delivered by the SessionManager. This class specifcally handles the management of Kerbeos credentials which may be stored in the session. '''
''' The current user has requested to be logged out. To accomplish this we remove the user's kerberos credentials from their session. This does not destroy the session, it just prevents it from being used for fast authentication. Because the credentials are no longer in the session cache any future attempt will require the acquisition of credentials using one of the login mechanisms. '''
if session_data.has_key('ccache_data'): self.debug('AuthManager.logout.%s: deleting ccache_data', self.name) del session_data['ccache_data'] else: self.error('AuthManager.logout.%s: session_data does not contain ccache_data', self.name)
''' Functionally shared by all RPC handlers using both sessions and Kerberos. This class must be implemented as a mixin class rather than the more obvious technique of subclassing because the classes needing this do not share a common base class. '''
''' Initialize values from the Env configuration.
Why do it this way and not simply reference api.env.session_auth_duration? Because that config item cannot be used directly, it must be parsed and converted to an integer. It would be inefficient to reparse it on every request. So we parse it once and store the result in the class instance. ''' # Set the session expiration time except Exception, e: self.session_auth_duration = default_max_session_duration self.error('unable to parse session_auth_duration, defaulting to %d: %s', self.session_auth_duration, e)
''' Each time a session is created or accessed we need to update it's expiration time. The expiration time is set inside the session_data.
:parameters: session_data The session data whose expiration is being updatded. krb_endtime The UNIX timestamp for when the Kerberos credentials expire. :returns: None '''
# Account for clock skew and/or give us some time leeway krb_expiration = krb_endtime - krb_ticket_expiration_threshold
# Set the session expiration time session_mgr.set_session_expiration_time(session_data, duration=self.session_auth_duration, max_age=krb_expiration, duration_type=self.api.env.session_duration_type)
if headers is None: headers = []
# Retrieve the session data (or newly create) session_data = session_mgr.load_session_data(environ.get('HTTP_COOKIE')) session_id = session_data['session_id']
self.debug('finalize_kerberos_acquisition: %s ccache_name="%s" session_id="%s"', who, ccache_name, session_id)
# Copy the ccache file contents into the session data session_data['ccache_data'] = load_ccache_data(ccache_name)
# Set when the session will expire cc = KRB5_CCache(ccache_name) endtime = cc.endtime(self.api.env.host, self.api.env.realm) self.update_session_expiration(session_data, endtime)
# Store the session data now that it's been updated with the ccache session_mgr.store_session_data(session_data)
# The request is finished with the ccache, destroy it. release_ipa_ccache(ccache_name)
# Return success and set session cookie session_cookie = session_mgr.generate_cookie('/ipa', session_id, session_data['session_expiration_timestamp']) headers.append(('Set-Cookie', session_cookie))
start_response(HTTP_STATUS_SUCCESS, headers) return ['']
""" Execution backend plugin for XML-RPC server.
Also see the `ipalib.rpc.xmlclient` plugin. """
'system.listMethods': self.listMethods, 'system.methodSignature': self.methodSignature, 'system.methodHelp': self.methodHelp, }
''' '''
self.internal_error(environ, start_response, 'xmlserver.__call__: KRB5CCNAME not defined in HTTP request environment') return self.marshal(None, CCacheError()) self.env.context != 'lite': self.finalize_kerberos_acquisition('xmlserver', user_ccache, environ, start_response) except PublicError, e: status = HTTP_STATUS_SUCCESS response = status headers = [('Content-Type', 'text/plain; charset=utf-8')] start_response(status, headers) return self.marshal(None, e) finally:
return tuple(name.decode('UTF-8') for name in self.Command)
return u'methodSignature not implemented'
return u'methodHelp not implemented'
# Keep backwards compatibility with client containing # bug https://fedorahosted.org/freeipa/ticket/3294: # If `version` is not given in XML-RPC, assume an old version
else:
""" JSON RPC server protected with session auth. """
status = '401 Unauthorized' headers = [] response = ''
self.debug('jsonserver_session: %s need login', status)
start_response(status, headers) return [response]
''' '''
self.debug('WSGI jsonserver_session.__call__:')
# Load the session data session_data = session_mgr.load_session_data(environ.get('HTTP_COOKIE')) session_id = session_data['session_id']
self.debug('jsonserver_session.__call__: session_id=%s start_timestamp=%s access_timestamp=%s expiration_timestamp=%s', session_id, fmt_time(session_data['session_start_timestamp']), fmt_time(session_data['session_access_timestamp']), fmt_time(session_data['session_expiration_timestamp']))
ccache_data = session_data.get('ccache_data')
# Redirect to login if no Kerberos credentials if ccache_data is None: self.debug('no ccache, need login') return self.need_login(start_response)
ipa_ccache_name = bind_ipa_ccache(ccache_data)
# Redirect to login if Kerberos credentials are expired cc = KRB5_CCache(ipa_ccache_name) if not cc.valid(self.api.env.host, self.api.env.realm): self.debug('ccache expired, deleting session, need login') # The request is finished with the ccache, destroy it. release_ipa_ccache(ipa_ccache_name) return self.need_login(start_response)
# Update the session expiration based on the Kerberos expiration endtime = cc.endtime(self.api.env.host, self.api.env.realm) self.update_session_expiration(session_data, endtime)
# Store the session data in the per-thread context setattr(context, 'session_data', session_data)
# This may fail if a ticket from wrong realm was handled via browser try: self.create_context(ccache=ipa_ccache_name) except ACIError, e: return self.unauthorized(environ, start_response, str(e), 'denied')
try: response = super(jsonserver_session, self).__call__(environ, start_response) finally: # Kerberos may have updated the ccache data during the # execution of the command therefore we need refresh our # copy of it in the session data so the next command sees # the same state of the ccache. # # However we must be careful not to restore the ccache # data in the session data if it was explicitly deleted # during the execution of the command. For example the # logout command removes the ccache data from the session # data to invalidate the session credentials.
if session_data.has_key('ccache_data'): session_data['ccache_data'] = load_ccache_data(ipa_ccache_name)
# The request is finished with the ccache, destroy it. release_ipa_ccache(ipa_ccache_name) # Store the session data. session_mgr.store_session_data(session_data) destroy_context()
return response
""" JSON RPC server protected with kerberos auth. """
''' '''
self.debug('WSGI jsonserver_kerb.__call__:')
user_ccache=environ.get('KRB5CCNAME') if user_ccache is None: self.internal_error(environ, start_response, 'jsonserver_kerb.__call__: KRB5CCNAME not defined in HTTP request environment') return self.marshal(None, CCacheError()) self.create_context(ccache=user_ccache)
try: response = super(jsonserver_kerb, self).__call__(environ, start_response) finally: destroy_context()
return response
self.debug('WSGI login_kerberos.__call__:')
# Get the ccache created by mod_auth_kerb user_ccache_name=environ.get('KRB5CCNAME') if user_ccache_name is None: return self.internal_error(environ, start_response, 'login_kerberos: KRB5CCNAME not defined in HTTP request environment')
return self.finalize_kerberos_acquisition('login_kerberos', user_ccache_name, environ, start_response)
self.debug('WSGI login_password.__call__:')
# Get the user and password parameters from the request content_type = environ.get('CONTENT_TYPE', '').lower() if not content_type.startswith('application/x-www-form-urlencoded'): return self.bad_request(environ, start_response, "Content-Type must be application/x-www-form-urlencoded")
method = environ.get('REQUEST_METHOD', '').upper() if method == 'POST': query_string = read_input(environ) else: return self.bad_request(environ, start_response, "HTTP request method must be POST")
try: query_dict = urlparse.parse_qs(query_string) except Exception, e: return self.bad_request(environ, start_response, "cannot parse query data")
user = query_dict.get('user', None) if user is not None: if len(user) == 1: user = user[0] else: return self.bad_request(environ, start_response, "more than one user parameter") else: return self.bad_request(environ, start_response, "no user specified")
# allows login in the form user@SERVER_REALM or user@server_realm # FIXME: uppercasing may be removed when better handling of UPN # is introduced
parts = normalize_name(user)
if "domain" in parts: # username is of the form user@SERVER_REALM or user@server_realm
# check whether the realm is server's realm # Users from other realms are not supported # (they do not have necessary LDAP entry, LDAP connect will fail)
if parts["domain"].upper()==self.api.env.realm: user=parts["name"] else: return self.unauthorized(environ, start_response, '', 'denied')
elif "flatname" in parts: # username is of the form NetBIOS\user return self.unauthorized(environ, start_response, '', 'denied')
else: # username is of the form user or of some wild form, e.g. # user@REALM1@REALM2 or NetBIOS1\NetBIOS2\user (see normalize_name)
# wild form username will fail at kinit, so nothing needs to be done pass
password = query_dict.get('password', None) if password is not None: if len(password) == 1: password = password[0] else: return self.bad_request(environ, start_response, "more than one password parameter") else: return self.bad_request(environ, start_response, "no password specified")
# Get the ccache we'll use and attempt to get credentials in it with user,password ipa_ccache_name = get_ipa_ccache_name() reason = 'invalid-password' try: self.kinit(user, self.api.env.realm, password, ipa_ccache_name) except InvalidSessionPassword, e: # Ok, now why is this bad. Is the password simply bad or is the # password expired? try: dn = DN(('uid', user), self.api.env.container_user, self.api.env.basedn) conn = ldap2(shared_instance=False, ldap_uri=self.api.env.ldap_uri) conn.connect(bind_dn=dn, bind_pw=password)
# password is ok, must be expired, lets double-check (userdn, entry_attrs) = conn.get_entry(dn, ['krbpasswordexpiration']) if 'krbpasswordexpiration' in entry_attrs: expiration = entry_attrs['krbpasswordexpiration'][0] try: exp = time.strptime(expiration, '%Y%m%d%H%M%SZ') if exp <= time.gmtime(): reason = 'password-expired' except ValueError, v: self.error('Unable to convert %s to a time string' % expiration)
except Exception: # It doesn't really matter how we got here but the user's # password is not accepted or the user is unknown. pass finally: if conn.isconnected(): conn.destroy_connection()
return self.unauthorized(environ, start_response, str(e), reason)
return self.finalize_kerberos_acquisition('login_password', ipa_ccache_name, environ, start_response)
# Format the user as a kerberos principal principal = krb5_format_principal_name(user, realm)
(stdout, stderr, returncode) = ipautil.run(['/usr/bin/kinit', principal], env={'KRB5CCNAME':ccache_name}, stdin=password, raiseonerr=False) self.debug('kinit: principal=%s returncode=%s, stderr="%s"', principal, returncode, stderr)
if returncode != 0: raise InvalidSessionPassword(principal=principal, message=unicode(stderr))
self.info('WSGI change_password.__call__:')
# Get the user and password parameters from the request content_type = environ.get('CONTENT_TYPE', '').lower() if not content_type.startswith('application/x-www-form-urlencoded'): return self.bad_request(environ, start_response, "Content-Type must be application/x-www-form-urlencoded")
method = environ.get('REQUEST_METHOD', '').upper() if method == 'POST': query_string = read_input(environ) else: return self.bad_request(environ, start_response, "HTTP request method must be POST")
try: query_dict = urlparse.parse_qs(query_string) except Exception, e: return self.bad_request(environ, start_response, "cannot parse query data")
data = {} for field in ('user', 'old_password', 'new_password'): value = query_dict.get(field, None) if value is not None: if len(value) == 1: data[field] = value[0] else: return self.bad_request(environ, start_response, "more than one %s parameter" % field) else: return self.bad_request(environ, start_response, "no %s specified" % field)
# start building the response self.info("WSGI change_password: start password change of user '%s'", data['user']) status = HTTP_STATUS_SUCCESS response_headers = [('Content-Type', 'text/html; charset=utf-8')] title = 'Password change rejected' result = 'error' policy_error = None
bind_dn = DN((self.api.Object.user.primary_key.name, data['user']), self.api.env.container_user, self.api.env.basedn)
try: conn = ldap2(shared_instance=False, ldap_uri=self.api.env.ldap_uri) conn.connect(bind_dn=bind_dn, bind_pw=data['old_password']) except (NotFound, ACIError): result = 'invalid-password' message = 'The old password or username is not correct.' except Exception, e: message = "Could not connect to LDAP server." self.error("change_password: cannot authenticate '%s' to LDAP server: %s", data['user'], str(e)) else: try: conn.modify_password(bind_dn, data['new_password'], data['old_password']) except ExecutionError, e: result = 'policy-error' policy_error = escape(str(e)) message = "Password change was rejected: %s" % escape(str(e)) except Exception, e: message = "Could not change the password" self.error("change_password: cannot change password of '%s': %s", data['user'], str(e)) else: result = 'ok' title = "Password change successful" message = "Password was changed." finally: if conn.isconnected(): conn.destroy_connection()
self.info('%s: %s', status, message)
response_headers.append(('X-IPA-Pwchange-Result', result)) if policy_error: response_headers.append(('X-IPA-Pwchange-Policy-Error', policy_error))
start_response(status, response_headers) output = _pwchange_template % dict(title=str(title), message=str(message)) return [output]
""" XML RPC server protected with session auth. """
status = '401 Unauthorized' headers = [] response = ''
self.debug('xmlserver_session: %s need login', status)
start_response(status, headers) return [response]
''' '''
self.debug('WSGI xmlserver_session.__call__:')
# Load the session data session_data = session_mgr.load_session_data(environ.get('HTTP_COOKIE')) session_id = session_data['session_id']
self.debug('xmlserver_session.__call__: session_id=%s start_timestamp=%s access_timestamp=%s expiration_timestamp=%s', session_id, fmt_time(session_data['session_start_timestamp']), fmt_time(session_data['session_access_timestamp']), fmt_time(session_data['session_expiration_timestamp']))
ccache_data = session_data.get('ccache_data')
# Redirect to /ipa/xml if no Kerberos credentials if ccache_data is None: self.debug('xmlserver_session.__call_: no ccache, need TGT') return self.need_login(start_response)
ipa_ccache_name = bind_ipa_ccache(ccache_data)
# Redirect to /ipa/xml if Kerberos credentials are expired cc = KRB5_CCache(ipa_ccache_name) if not cc.valid(self.api.env.host, self.api.env.realm): self.debug('xmlserver_session.__call_: ccache expired, deleting session, need login') # The request is finished with the ccache, destroy it. release_ipa_ccache(ipa_ccache_name) return self.need_login(start_response)
# Update the session expiration based on the Kerberos expiration endtime = cc.endtime(self.api.env.host, self.api.env.realm) self.update_session_expiration(session_data, endtime)
# Store the session data in the per-thread context setattr(context, 'session_data', session_data)
environ['KRB5CCNAME'] = ipa_ccache_name
try: response = super(xmlserver_session, self).__call__(environ, start_response) finally: # Kerberos may have updated the ccache data during the # execution of the command therefore we need refresh our # copy of it in the session data so the next command sees # the same state of the ccache. # # However we must be careful not to restore the ccache # data in the session data if it was explicitly deleted # during the execution of the command. For example the # logout command removes the ccache data from the session # data to invalidate the session credentials.
if session_data.has_key('ccache_data'): session_data['ccache_data'] = load_ccache_data(ipa_ccache_name)
# The request is finished with the ccache, destroy it. release_ipa_ccache(ipa_ccache_name) # Store the session data. session_mgr.store_session_data(session_data) destroy_context()
return response |