From f17c03e4d9786ea9657cc64a5f6ff70a9522b08d Mon Sep 17 00:00:00 2001 From: Jon Fautley Date: Tue, 30 Oct 2018 13:56:40 +0000 Subject: [PATCH] Rebase to master, rework logic and simplify --- ldapauthenticator/ldapauthenticator.py | 330 ++++++++++++++----------- 1 file changed, 181 insertions(+), 149 deletions(-) diff --git a/ldapauthenticator/ldapauthenticator.py b/ldapauthenticator/ldapauthenticator.py index 719caa2..5fc5434 100644 --- a/ldapauthenticator/ldapauthenticator.py +++ b/ldapauthenticator/ldapauthenticator.py @@ -60,9 +60,9 @@ def _server_port_default(self): List Example: [ - uid={username},ou=people,dc=wikimedia,dc=org, - uid={username},ou=Developers,dc=wikimedia,dc=org - ] + uid={username},ou=people,dc=wikimedia,dc=org, + uid={username},ou=Developers,dc=wikimedia,dc=org + ] Active Directory Example: DOMAIN\{login} @@ -143,10 +143,10 @@ def _server_port_default(self): group_search_base = Unicode( config=True, - default=None, + default=user_search_base, allow_none=True, help=""" - Base for looking up groups in the directory. Used if `activedirectory` is True. + Base for looking up groups in the directory. Defaults to the value of user_search_base if unset. For example: ``` @@ -211,17 +211,17 @@ def _server_port_default(self): Example Active Directory configuration: ``` c.LDAPAuthenticator.bind_dn_template = 'DOMAIN\{login}' - c.LDAPAuthenticator.lookup_dn = True + c.LDAPAuthenticator.lookup_dn = False c.LDAPAuthenticator.activedirectory = True c.LDAPAuthenticator.get_groups_from_user = True - c.LDAPAuthenticator.lookup_dn_user_dn_attribute = 'cn' + c.LDAPAuthenticator.lookup_dn_user_dn_attribute = 'distinguishedName' c.LDAPAuthenticator.lookup_dn_search_filter = '({login_attr}={login})' c.LDAPAuthenticator.lookup_dn_search_user = 'readonly' c.LDAPAuthenticator.lookup_dn_search_password = 'notarealpassword' c.LDAPAuthenticator.user_attribute = 'sAMAccountName' c.LDAPAuthenticator.user_search_base = 'OU=Users,DC=example,DC=org' c.LDAPAuthenticator.group_search_base = 'OU=Groups,DC=example,DC=org' - + c.LDAPAuthenticator.admin_users = {'Administrator'} c.LDAPAuthenticator.allowed_groups = [ 'CN=JupyterHub_Users,OU=Groups,DC=example,DC=org'] @@ -263,7 +263,7 @@ def _server_port_default(self): default_value=None, allow_none=True, help=""" - Attribute containing user's name needed for building DN string, if `lookup_dn` is set to True. + Attribute containing user's name needed for building DN string, if `lookup_dn` is set to True. See `user_search_base` for info on how this attribute is used. @@ -282,56 +282,6 @@ def _server_port_default(self): """ ) - def resolve_username(self, username_supplied_by_user, want_dn = False): - if self.lookup_dn: - server = ldap3.Server( - self.server_address, - port=self.server_port, - use_ssl=self.use_ssl - ) - - search_filter = self.lookup_dn_search_filter.format( - login_attr=self.user_attribute, - login=username_supplied_by_user - ) - self.log.debug( - "Looking up user with search_base={search_base}, search_filter='{search_filter}', attributes={attributes}".format( - search_base=self.user_search_base, - search_filter=search_filter, - attributes=self.user_attribute - ) - ) - - conn = ldap3.Connection(server, user=self.escape_userdn_if_needed(self.lookup_dn_search_user), password=self.lookup_dn_search_password) - is_bound = conn.bind() - if not is_bound: - self.log.warn("Can't connect to LDAP") - return None - - conn.search( - search_base=self.user_search_base, - search_scope=ldap3.SUBTREE, - search_filter=search_filter, - attributes=[self.lookup_dn_user_dn_attribute] - ) - - if len(conn.response) == 0 or 'attributes' not in conn.response[0].keys(): - self.log.warn('username:%s No such user entry found when looking up with attribute %s', username_supplied_by_user, - self.user_attribute) - return None - if want_dn: - return conn.response[0]['dn'] - else: - return conn.response[0]['attributes'][self.lookup_dn_user_dn_attribute] - else: - return username_supplied_by_user - - def escape_userdn_if_needed(self, userdn): - if self.escape_userdn: - return escape_filter_chars(userdn) - else: - return userdn - search_filter = Unicode( config=True, help="LDAP3 Search Filter whose results are allowed access" @@ -342,35 +292,83 @@ def escape_userdn_if_needed(self, userdn): help="List of attributes to be searched" ) + def resolve_username(self, username_supplied_by_user): + search_dn = self.lookup_dn_search_user + if self.escape_userdn: + search_dn = escape_filter_chars(search_dn) + conn = self.get_connection( + userdn=search_dn, + password=self.lookup_dn_search_password, + ) + is_bound = conn.bind() + if not is_bound: + msg = "Failed to connect to LDAP server with search user '{search_dn}'" + self.log.warn(msg.format(search_dn=search_dn)) + return None + + search_filter = self.lookup_dn_search_filter.format( + login_attr=self.user_attribute, + login=username_supplied_by_user, + ) + msg = '\n'.join([ + "Looking up user with:", + " search_base = '{search_base}'", + " search_filter = '{search_filter}'", + " attributes = '{attributes}'", + ]) + self.log.debug(msg.format( + search_base=self.user_search_base, + search_filter=search_filter, + attributes=self.user_attribute, + )) + conn.search( + search_base=self.user_search_base, + search_scope=ldap3.SUBTREE, + search_filter=search_filter, + attributes=[self.lookup_dn_user_dn_attribute], + ) + response = conn.response + if len(response) == 0 or 'attributes' not in response[0].keys(): + msg = ( + "No entry found for user '{username}' " + "when looking up attribute '{attribute}'" + ) + self.log.warn(msg.format( + username=username_supplied_by_user, + attribute=self.user_attribute, + )) + return None + return conn.response[0]['attributes'][self.lookup_dn_user_dn_attribute] + + def get_connection(self, userdn, password): + server = ldap3.Server( + self.server_address, + port=self.server_port, + use_ssl=self.use_ssl + ) + auto_bind = ( + self.use_ssl + and ldap3.AUTO_BIND_TLS_BEFORE_BIND + or ldap3.AUTO_BIND_NO_TLS + ) + conn = ldap3.Connection( + server, + user=userdn, + password=password, + auto_bind=auto_bind, + ) + return conn @gen.coroutine def authenticate(self, handler, data): username = data['username'] password = data['password'] - # Get LDAP Connection - def getConnection(userdn, username, password): - server = ldap3.Server( - self.server_address, - port=self.server_port, - use_ssl=self.use_ssl - ) - self.log.debug('Attempting to bind {username} with {userdn}'.format( - username=username, - userdn=userdn - )) - conn = ldap3.Connection( - server, - user=self.escape_userdn_if_needed(userdn), - password=password, - auto_bind=self.use_ssl and ldap3.AUTO_BIND_TLS_BEFORE_BIND or ldap3.AUTO_BIND_NO_TLS, - ) - return conn def get_user_groups(username): if self.activedirectory: self.log.debug('Active Directory enabled') - user_dn = self.resolve_username(username, want_dn=True) - search_filter='(member:1.2.840.113556.1.4.1941:={dn})'.format(dn=self.escape_userdn_if_needed(user_dn)) + user_dn = self.resolve_username(username) + search_filter='(member:1.2.840.113556.1.4.1941:={dn})'.format(dn=escape_filter_chars(user_dn)) search_attribs=['cn'] # We don't actually care, we just want the DN search_base=self.group_search_base, self.log.debug('LDAP Group query: user_dn:[%s] filter:[%s]', user_dn, search_filter) @@ -381,7 +379,7 @@ def get_user_groups(username): self.log.debug('LDAP Group query: username:[%s] filter:[%s]', username, search_filter) conn.search( - search_base=search_base, + search_base=self.group_search_base, search_scope=ldap3.SUBTREE, search_filter=search_filter, attributes=search_attribs) @@ -404,112 +402,146 @@ def get_user_groups(username): # Protect against invalid usernames as well as LDAP injection attacks if not re.match(self.valid_username_regex, username): - self.log.warn('username:%s Illegal characters in username, must match regex %s', username, self.valid_username_regex) + self.log.warn( + 'username:%s Illegal characters in username, must match regex %s', + username, self.valid_username_regex + ) return None + # Allow us to reference the actual username the user typed (rather than + # what we might resolve it to later) + login = username + # No empty passwords! if password is None or password.strip() == '': self.log.warn('username:%s Login denied for blank password', username) return None - isBound = False - self.log.debug("TYPE= '%s'",isinstance(self.bind_dn_template, list)) - - resolved_username = self.resolve_username(username) - if resolved_username is None: - return None + if self.lookup_dn: + username = self.resolve_username(login) + if not username: + return None if self.lookup_dn: if str(self.lookup_dn_user_dn_attribute).upper() == 'CN': # Only escape commas if the lookup attribute is CN - resolved_username = re.subn(r"([^\\]),", r"\1\,", resolved_username)[0] + username = re.subn(r"([^\\]),", r"\1\,", username)[0] bind_dn_template = self.bind_dn_template if isinstance(bind_dn_template, str): # bind_dn_template should be of type List[str] bind_dn_template = [bind_dn_template] + is_bound = False for dn in bind_dn_template: - userdn = dn.format(username=resolved_username, login=username) - msg = 'Status of user bind {username} with {userdn} : {isBound}' + if not dn: + self.log.warn("Ignoring blank 'bind_dn_template' entry!") + continue + userdn = dn.format(username=username, login=login) + if self.escape_userdn: + userdn = escape_filter_chars(userdn) + msg = 'Attempting to bind {username} with {userdn}' + self.log.debug(msg.format(username=username, userdn=userdn)) + msg = "Status of user bind {username} with {userdn} : {is_bound}" try: - conn = getConnection(userdn, username, password) + conn = self.get_connection(userdn, password) except ldap3.core.exceptions.LDAPBindError as exc: - isBound = False + is_bound = False msg += '\n{exc_type}: {exc_msg}'.format( exc_type=exc.__class__.__name__, exc_msg=exc.args[0] if exc.args else '' ) else: - isBound = conn.bind() + is_bound = conn.bind() msg = msg.format( username=username, userdn=userdn, - isBound=isBound + is_bound=is_bound ) self.log.debug(msg) - if isBound: + if is_bound: break - if isBound: - if self.allowed_groups: - self.log.debug('username:%s Using dn %s', username, userdn) - - if self.get_groups_from_user: - user_groups = get_user_groups(username) - if user_groups is None: - self.log.debug('Username %s has no group membership', username) - return None - else: - self.log.debug('Username %s is a member of %d groups', username, len(user_groups)) - for group in self.allowed_groups: - if group in user_groups: - self.log.info('User %s is a member of permitted group %s', username, group) - return username - else: - for group in self.allowed_groups: - groupfilter = ( - '(|' - '(member={userdn})' - '(uniqueMember={userdn})' - '(memberUid={uid})' - ')' - ).format(userdn=escape_filter_chars(userdn), uid=escape_filter_chars(username)) - groupattributes = ['member', 'uniqueMember', 'memberUid'] - if conn.search( - group, - search_scope=ldap3.BASE, - search_filter=groupfilter, - attributes=groupattributes - ): - return username + if not is_bound: + msg = "Invalid password for user '{username}'" + self.log.warn(msg.format(username=username)) + return None - # If we reach here, then none of the groups matched - self.log.warn('username:%s User not in any of the allowed groups', username) + if self.search_filter: + search_filter = self.search_filter.format( + userattr=self.user_attribute, + username=username, + ) + conn.search( + search_base=self.user_search_base, + search_scope=ldap3.SUBTREE, + search_filter=search_filter, + attributes=self.attributes + ) + n_users = len(conn.response) + if n_users == 0: + msg = "User with '{userattr}={username}' not found in directory" + self.log.warn(msg.format( + userattr=self.user_attribute, + username=username) + ) return None - elif self.search_filter: - conn.search( - search_base=self.user_search_base, - search_scope=ldap3.SUBTREE, - search_filter=self.search_filter.format(userattr=self.user_attribute,username=username), - attributes=self.attributes + if n_users > 1: + msg = ( + "Duplicate users found! " + "{n_users} users found with '{userattr}={username}'" ) - if len(conn.response) == 0: - self.log.warn('User with {userattr}={username} not found in directory'.format( - userattr=self.user_attribute, username=username)) - return None - elif len(conn.response) > 1: - self.log.warn('User with {userattr}={username} found more than {len}-fold in directory'.format( - userattr=self.user_attribute, username=username, len=len(conn.response))) + self.log.warn(msg.format( + userattr=self.user_attribute, + username=username, + n_users=n_users) + ) + return None + + if self.allowed_groups: + self.log.debug('username:%s Using dn %s', username, userdn) + found = False + if self.get_groups_from_user: + user_groups = get_user_groups(login) + if user_groups is None: + self.log.debug('Username %s has no group membership', username) return None - return username + else: + self.log.debug('Username %s is a member of %d groups', username, len(user_groups)) + for group in self.allowed_groups: + if group in user_groups: + self.log.info('User %s is a member of permitted group %s', username, group) + return username else: - return username - else: - self.log.warn('Invalid password for user {username}'.format( - username=username, - )) - return None + for group in self.allowed_groups: + group_filter = ( + '(|' + '(member={userdn})' + '(uniqueMember={userdn})' + '(memberUid={uid})' + ')' + ) + group_filter = group_filter.format( + userdn=userdn, + uid=username + ) + group_attributes = ['member', 'uniqueMember', 'memberUid'] + found = conn.search( + group, + search_scope=ldap3.BASE, + search_filter=group_filter, + attributes=group_attributes + ) + if found: + break + + if not found: + # If we reach here, then none of the groups matched + msg = 'username:{username} User not in any of the allowed groups' + self.log.warn(msg.format(username=username)) + return None + + return username if __name__ == "__main__":