private Dn getUserBindDN(LDAPConfiguration config, String username)
            throws GuacamoleException {

        // If a search DN is provided, search the LDAP directory for the DN
        // corresponding to the given username
        String searchBindLogon = config.getSearchBindDN();
        if (searchBindLogon != null) {

            // Create an LDAP connection using the search account
            LdapNetworkConnection searchConnection = ldapService.bindAs(config,
                searchBindLogon, config.getSearchBindPassword());

            // Warn of failure to find
            if (searchConnection == null) {
                logger.error("Unable to bind using search DN \"{}\"",
                        searchBindLogon);
                return null;
            }

            try {

                // Retrieve all DNs associated with the given username
                List<Dn> userDNs = userService.getUserDNs(config, searchConnection, username);
                if (userDNs.isEmpty())
                    return null;

                // Warn if multiple DNs exist for the same user
                if (userDNs.size() != 1) {
                    logger.warn("Multiple DNs possible for user \"{}\": {}", username, userDNs);
                    return null;
                }

                // Return the single possible DN
                return userDNs.get(0);

            }

            // Always disconnect
            finally {
                searchConnection.close();
            }

        }

        // Otherwise, derive user DN from base DN
        return userService.deriveUserDN(config, username);

    }
Determines the DN which corresponds to the user having the given
username. The DN will either be derived directly from the user base DN,
or queried from the LDAP server, depending on how LDAP authentication
has been configured.

@param config
    The configuration of the LDAP server being queried.

@param username
    The username of the user whose corresponding DN should be returned.

@return
    The DN which corresponds to the user having the given username.

@throws GuacamoleException
    If required properties are missing, and thus the user DN cannot be
    determined.
extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/AuthenticationProviderService.java