0N/A/*
2362N/A * Copyright (c) 1999, 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.jndi.ldap.sasl;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.Vector;
0N/Aimport java.util.Hashtable;
0N/Aimport java.util.StringTokenizer;
0N/A
0N/Aimport javax.naming.AuthenticationException;
0N/Aimport javax.naming.AuthenticationNotSupportedException;
0N/Aimport javax.naming.NamingException;
0N/A
0N/Aimport javax.naming.ldap.Control;
0N/A
0N/Aimport javax.security.auth.callback.CallbackHandler;
0N/Aimport javax.security.sasl.*;
0N/Aimport com.sun.jndi.ldap.Connection;
0N/Aimport com.sun.jndi.ldap.LdapClient;
0N/Aimport com.sun.jndi.ldap.LdapResult;
0N/A
0N/A/**
0N/A * Handles SASL support.
0N/A *
0N/A * @author Vincent Ryan
0N/A * @author Rosanna Lee
0N/A */
0N/A
0N/Afinal public class LdapSasl {
0N/A // SASL stuff
0N/A private static final String SASL_CALLBACK = "java.naming.security.sasl.callback";
0N/A private static final String SASL_AUTHZ_ID =
0N/A "java.naming.security.sasl.authorizationId";
0N/A private static final String SASL_REALM =
0N/A "java.naming.security.sasl.realm";
0N/A
0N/A private static final int LDAP_SUCCESS = 0;
0N/A private static final int LDAP_SASL_BIND_IN_PROGRESS = 14; // LDAPv3
0N/A
0N/A private LdapSasl() {
0N/A }
0N/A
0N/A /**
0N/A * Performs SASL bind.
0N/A * Creates a SaslClient by using a default CallbackHandler
0N/A * that uses the Context.SECURITY_PRINCIPAL and Context.SECURITY_CREDENTIALS
0N/A * properties to satisfy the callbacks, and by using the
0N/A * SASL_AUTHZ_ID property as the authorization id. If the SASL_AUTHZ_ID
0N/A * property has not been set, Context.SECURITY_PRINCIPAL is used.
0N/A * If SASL_CALLBACK has been set, use that instead of the default
0N/A * CallbackHandler.
0N/A *<p>
0N/A * If bind is successful and the selected SASL mechanism has a security
0N/A * layer, set inStream and outStream to be filter streams that use
0N/A * the security layer. These will be used for subsequent communication
0N/A * with the server.
0N/A *<p>
0N/A * @param conn The non-null connection to use for sending an LDAP BIND
0N/A * @param server Non-null string name of host to connect to
0N/A * @param dn Non-null DN to bind as; also used as authentication ID
0N/A * @param pw Possibly null password; can be byte[], char[] or String
0N/A * @param authMech A non-null space-separated list of SASL authentication
0N/A * mechanisms.
0N/A * @param env The possibly null environment of the context, possibly containing
0N/A * properties for used by SASL mechanisms
0N/A * @param bindCtls The possibly null controls to accompany the bind
0N/A * @return LdapResult containing status of the bind
0N/A */
0N/A public static LdapResult saslBind(LdapClient clnt, Connection conn,
0N/A String server, String dn, Object pw,
0N/A String authMech, Hashtable env, Control[] bindCtls)
0N/A throws IOException, NamingException {
0N/A
0N/A SaslClient saslClnt = null;
0N/A boolean cleanupHandler = false;
0N/A
0N/A // Use supplied callback handler or create default
0N/A CallbackHandler cbh =
0N/A (env != null) ? (CallbackHandler)env.get(SASL_CALLBACK) : null;
0N/A if (cbh == null) {
0N/A cbh = new DefaultCallbackHandler(dn, pw, (String)env.get(SASL_REALM));
0N/A cleanupHandler = true;
0N/A }
0N/A
0N/A // Prepare parameters for creating SASL client
0N/A String authzId = (env != null) ? (String)env.get(SASL_AUTHZ_ID) : null;
0N/A String[] mechs = getSaslMechanismNames(authMech);
0N/A
0N/A try {
0N/A // Create SASL client to use using SASL package
0N/A saslClnt = Sasl.createSaslClient(
0N/A mechs, authzId, "ldap", server, env, cbh);
0N/A
0N/A if (saslClnt == null) {
0N/A throw new AuthenticationNotSupportedException(authMech);
0N/A }
0N/A
0N/A LdapResult res;
0N/A String mechName = saslClnt.getMechanismName();
0N/A byte[] response = saslClnt.hasInitialResponse() ?
0N/A saslClnt.evaluateChallenge(NO_BYTES) : null;
0N/A
0N/A res = clnt.ldapBind(null, response, bindCtls, mechName, true);
0N/A
0N/A while (!saslClnt.isComplete() &&
0N/A (res.status == LDAP_SASL_BIND_IN_PROGRESS ||
0N/A res.status == LDAP_SUCCESS)) {
0N/A
0N/A response = saslClnt.evaluateChallenge(
0N/A res.serverCreds != null? res.serverCreds : NO_BYTES);
0N/A if (res.status == LDAP_SUCCESS) {
0N/A if (response != null) {
0N/A throw new AuthenticationException(
0N/A "SASL client generated response after success");
0N/A }
0N/A break;
0N/A }
0N/A res = clnt.ldapBind(null, response, bindCtls, mechName, true);
0N/A }
0N/A
0N/A if (res.status == LDAP_SUCCESS) {
0N/A if (!saslClnt.isComplete()) {
0N/A throw new AuthenticationException(
0N/A "SASL authentication not complete despite server claims");
0N/A }
0N/A
0N/A String qop = (String) saslClnt.getNegotiatedProperty(Sasl.QOP);
0N/A
0N/A // If negotiated integrity or privacy,
0N/A if (qop != null && (qop.equalsIgnoreCase("auth-int")
0N/A || qop.equalsIgnoreCase("auth-conf"))) {
0N/A
0N/A InputStream newIn = new SaslInputStream(saslClnt,
0N/A conn.inStream);
0N/A OutputStream newOut = new SaslOutputStream(saslClnt,
0N/A conn.outStream);
0N/A
0N/A conn.replaceStreams(newIn, newOut);
0N/A } else {
0N/A saslClnt.dispose();
0N/A }
0N/A }
0N/A return res;
0N/A } catch (SaslException e) {
0N/A NamingException ne = new AuthenticationException(
0N/A authMech);
0N/A ne.setRootCause(e);
0N/A throw ne;
0N/A } finally {
0N/A if (cleanupHandler) {
0N/A ((DefaultCallbackHandler)cbh).clearPassword();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of SASL mechanisms given a string of space
0N/A * separated SASL mechanism names.
0N/A * @param The non-null string containing the mechanism names
0N/A * @return A non-null array of String; each element of the array
0N/A * contains a single mechanism name.
0N/A */
0N/A private static String[] getSaslMechanismNames(String str) {
0N/A StringTokenizer parser = new StringTokenizer(str);
0N/A Vector mechs = new Vector(10);
0N/A while (parser.hasMoreTokens()) {
0N/A mechs.addElement(parser.nextToken());
0N/A }
0N/A String[] mechNames = new String[mechs.size()];
0N/A for (int i = 0; i < mechs.size(); i++) {
0N/A mechNames[i] = (String)mechs.elementAt(i);
0N/A }
0N/A return mechNames;
0N/A }
0N/A
0N/A private static final byte[] NO_BYTES = new byte[0];
0N/A}